net/hns3: fix VF mailbox head field
[dpdk.git] / drivers / net / hns3 / hns3_mbx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2021 HiSilicon Limited.
3  */
4
5 #include <ethdev_driver.h>
6 #include <rte_io.h>
7
8 #include "hns3_ethdev.h"
9 #include "hns3_regs.h"
10 #include "hns3_logs.h"
11 #include "hns3_intr.h"
12 #include "hns3_rxtx.h"
13
14 #define HNS3_CMD_CODE_OFFSET            2
15
16 static const struct errno_respcode_map err_code_map[] = {
17         {0, 0},
18         {1, -EPERM},
19         {2, -ENOENT},
20         {5, -EIO},
21         {11, -EAGAIN},
22         {12, -ENOMEM},
23         {16, -EBUSY},
24         {22, -EINVAL},
25         {28, -ENOSPC},
26         {95, -EOPNOTSUPP},
27 };
28
29 static int
30 hns3_resp_to_errno(uint16_t resp_code)
31 {
32         uint32_t i, num;
33
34         num = sizeof(err_code_map) / sizeof(struct errno_respcode_map);
35         for (i = 0; i < num; i++) {
36                 if (err_code_map[i].resp_code == resp_code)
37                         return err_code_map[i].err_no;
38         }
39
40         return -EIO;
41 }
42
43 static int
44 hns3_get_mbx_resp(struct hns3_hw *hw, uint16_t code0, uint16_t code1,
45                   uint8_t *resp_data, uint16_t resp_len)
46 {
47 #define HNS3_MAX_RETRY_MS       500
48 #define HNS3_WAIT_RESP_US       100
49         struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
50         struct hns3_mbx_resp_status *mbx_resp;
51         uint64_t now;
52         uint64_t end;
53
54         if (resp_len > HNS3_MBX_MAX_RESP_DATA_SIZE) {
55                 hns3_err(hw, "VF mbx response len(=%u) exceeds maximum(=%d)",
56                          resp_len, HNS3_MBX_MAX_RESP_DATA_SIZE);
57                 return -EINVAL;
58         }
59
60         now = get_timeofday_ms();
61         end = now + HNS3_MAX_RETRY_MS;
62         while ((hw->mbx_resp.head != hw->mbx_resp.tail + hw->mbx_resp.lost) &&
63                (now < end)) {
64                 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED)) {
65                         hns3_err(hw, "Don't wait for mbx respone because of "
66                                  "disable_cmd");
67                         return -EBUSY;
68                 }
69
70                 if (is_reset_pending(hns)) {
71                         hw->mbx_resp.req_msg_data = 0;
72                         hns3_err(hw, "Don't wait for mbx respone because of "
73                                  "reset pending");
74                         return -EIO;
75                 }
76
77                 hns3_dev_handle_mbx_msg(hw);
78                 rte_delay_us(HNS3_WAIT_RESP_US);
79
80                 now = get_timeofday_ms();
81         }
82         hw->mbx_resp.req_msg_data = 0;
83         if (now >= end) {
84                 hw->mbx_resp.lost++;
85                 hns3_err(hw,
86                          "VF could not get mbx(%u,%u) head(%u) tail(%u) "
87                          "lost(%u) from PF",
88                          code0, code1, hw->mbx_resp.head, hw->mbx_resp.tail,
89                          hw->mbx_resp.lost);
90                 return -ETIME;
91         }
92         rte_io_rmb();
93         mbx_resp = &hw->mbx_resp;
94
95         if (mbx_resp->resp_status)
96                 return mbx_resp->resp_status;
97
98         if (resp_data)
99                 memcpy(resp_data, &mbx_resp->additional_info[0], resp_len);
100
101         return 0;
102 }
103
104 int
105 hns3_send_mbx_msg(struct hns3_hw *hw, uint16_t code, uint16_t subcode,
106                   const uint8_t *msg_data, uint8_t msg_len, bool need_resp,
107                   uint8_t *resp_data, uint16_t resp_len)
108 {
109         struct hns3_mbx_vf_to_pf_cmd *req;
110         struct hns3_cmd_desc desc;
111         bool is_ring_vector_msg;
112         int offset;
113         int ret;
114
115         req = (struct hns3_mbx_vf_to_pf_cmd *)desc.data;
116
117         /* first two bytes are reserved for code & subcode */
118         if (msg_len > (HNS3_MBX_MAX_MSG_SIZE - HNS3_CMD_CODE_OFFSET)) {
119                 hns3_err(hw,
120                          "VF send mbx msg fail, msg len %u exceeds max payload len %d",
121                          msg_len, HNS3_MBX_MAX_MSG_SIZE - HNS3_CMD_CODE_OFFSET);
122                 return -EINVAL;
123         }
124
125         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MBX_VF_TO_PF, false);
126         req->msg[0] = code;
127         is_ring_vector_msg = (code == HNS3_MBX_MAP_RING_TO_VECTOR) ||
128                              (code == HNS3_MBX_UNMAP_RING_TO_VECTOR) ||
129                              (code == HNS3_MBX_GET_RING_VECTOR_MAP);
130         if (!is_ring_vector_msg)
131                 req->msg[1] = subcode;
132         if (msg_data) {
133                 offset = is_ring_vector_msg ? 1 : HNS3_CMD_CODE_OFFSET;
134                 memcpy(&req->msg[offset], msg_data, msg_len);
135         }
136
137         /* synchronous send */
138         if (need_resp) {
139                 req->mbx_need_resp |= HNS3_MBX_NEED_RESP_BIT;
140                 rte_spinlock_lock(&hw->mbx_resp.lock);
141                 hw->mbx_resp.req_msg_data = (uint32_t)code << 16 | subcode;
142                 hw->mbx_resp.head++;
143                 ret = hns3_cmd_send(hw, &desc, 1);
144                 if (ret) {
145                         hw->mbx_resp.head--;
146                         rte_spinlock_unlock(&hw->mbx_resp.lock);
147                         hns3_err(hw, "VF failed(=%d) to send mbx message to PF",
148                                  ret);
149                         return ret;
150                 }
151
152                 ret = hns3_get_mbx_resp(hw, code, subcode, resp_data, resp_len);
153                 rte_spinlock_unlock(&hw->mbx_resp.lock);
154         } else {
155                 /* asynchronous send */
156                 ret = hns3_cmd_send(hw, &desc, 1);
157                 if (ret) {
158                         hns3_err(hw, "VF failed(=%d) to send mbx message to PF",
159                                  ret);
160                         return ret;
161                 }
162         }
163
164         return ret;
165 }
166
167 static bool
168 hns3_cmd_crq_empty(struct hns3_hw *hw)
169 {
170         uint32_t tail = hns3_read_dev(hw, HNS3_CMDQ_RX_TAIL_REG);
171
172         return tail == hw->cmq.crq.next_to_use;
173 }
174
175 static void
176 hns3_mbx_handler(struct hns3_hw *hw)
177 {
178         enum hns3_reset_level reset_level;
179         uint8_t link_status, link_duplex;
180         uint32_t link_speed;
181         uint16_t *msg_q;
182         uint8_t opcode;
183         uint32_t tail;
184
185         tail = hw->arq.tail;
186
187         /* process all the async queue messages */
188         while (tail != hw->arq.head) {
189                 msg_q = hw->arq.msg_q[hw->arq.head];
190
191                 opcode = msg_q[0] & 0xff;
192                 switch (opcode) {
193                 case HNS3_MBX_LINK_STAT_CHANGE:
194                         memcpy(&link_speed, &msg_q[2], sizeof(link_speed));
195                         link_status = rte_le_to_cpu_16(msg_q[1]);
196                         link_duplex = (uint8_t)rte_le_to_cpu_16(msg_q[4]);
197                         hns3vf_update_link_status(hw, link_status, link_speed,
198                                                   link_duplex);
199                         break;
200                 case HNS3_MBX_ASSERTING_RESET:
201                         /* PF has asserted reset hence VF should go in pending
202                          * state and poll for the hardware reset status till it
203                          * has been completely reset. After this stack should
204                          * eventually be re-initialized.
205                          */
206                         reset_level = rte_le_to_cpu_16(msg_q[1]);
207                         hns3_atomic_set_bit(reset_level, &hw->reset.pending);
208
209                         hns3_warn(hw, "PF inform reset level %d", reset_level);
210                         hw->reset.stats.request_cnt++;
211                         hns3_schedule_reset(HNS3_DEV_HW_TO_ADAPTER(hw));
212                         break;
213                 default:
214                         hns3_err(hw, "Fetched unsupported(%u) message from arq",
215                                  opcode);
216                         break;
217                 }
218
219                 hns3_mbx_head_ptr_move_arq(hw->arq);
220                 msg_q = hw->arq.msg_q[hw->arq.head];
221         }
222 }
223
224 /*
225  * Case1: receive response after timeout, req_msg_data
226  *        is 0, not equal resp_msg, do lost--
227  * Case2: receive last response during new send_mbx_msg,
228  *        req_msg_data is different with resp_msg, let
229  *        lost--, continue to wait for response.
230  */
231 static void
232 hns3_update_resp_position(struct hns3_hw *hw, uint32_t resp_msg)
233 {
234         struct hns3_mbx_resp_status *resp = &hw->mbx_resp;
235         uint32_t tail = resp->tail + 1;
236
237         if (tail > resp->head)
238                 tail = resp->head;
239         if (resp->req_msg_data != resp_msg) {
240                 if (resp->lost)
241                         resp->lost--;
242                 hns3_warn(hw, "Received a mismatched response req_msg(%x) "
243                           "resp_msg(%x) head(%u) tail(%u) lost(%u)",
244                           resp->req_msg_data, resp_msg, resp->head, tail,
245                           resp->lost);
246         } else if (tail + resp->lost > resp->head) {
247                 resp->lost--;
248                 hns3_warn(hw, "Received a new response again resp_msg(%x) "
249                           "head(%u) tail(%u) lost(%u)", resp_msg,
250                           resp->head, tail, resp->lost);
251         }
252         rte_io_wmb();
253         resp->tail = tail;
254 }
255
256 static void
257 hns3_link_fail_parse(struct hns3_hw *hw, uint8_t link_fail_code)
258 {
259         switch (link_fail_code) {
260         case HNS3_MBX_LF_NORMAL:
261                 break;
262         case HNS3_MBX_LF_REF_CLOCK_LOST:
263                 hns3_warn(hw, "Reference clock lost!");
264                 break;
265         case HNS3_MBX_LF_XSFP_TX_DISABLE:
266                 hns3_warn(hw, "SFP tx is disabled!");
267                 break;
268         case HNS3_MBX_LF_XSFP_ABSENT:
269                 hns3_warn(hw, "SFP is absent!");
270                 break;
271         default:
272                 hns3_warn(hw, "Unknown fail code:%u!", link_fail_code);
273                 break;
274         }
275 }
276
277 static void
278 hns3_handle_link_change_event(struct hns3_hw *hw,
279                               struct hns3_mbx_pf_to_vf_cmd *req)
280 {
281 #define LINK_STATUS_OFFSET     1
282 #define LINK_FAIL_CODE_OFFSET  2
283
284         if (!req->msg[LINK_STATUS_OFFSET])
285                 hns3_link_fail_parse(hw, req->msg[LINK_FAIL_CODE_OFFSET]);
286
287         hns3_update_link_status_and_event(hw);
288 }
289
290 static void
291 hns3_update_port_base_vlan_info(struct hns3_hw *hw,
292                                 struct hns3_mbx_pf_to_vf_cmd *req)
293 {
294 #define PVID_STATE_OFFSET       1
295         uint16_t new_pvid_state = req->msg[PVID_STATE_OFFSET] ?
296                 HNS3_PORT_BASE_VLAN_ENABLE : HNS3_PORT_BASE_VLAN_DISABLE;
297         /*
298          * Currently, hardware doesn't support more than two layers VLAN offload
299          * based on hns3 network engine, which would cause packets loss or wrong
300          * packets for these types of packets. If the hns3 PF kernel ethdev
301          * driver sets the PVID for VF device after initialization of the
302          * related VF device, the PF driver will notify VF driver to update the
303          * PVID configuration state. The VF driver will update the PVID
304          * configuration state immediately to ensure that the VLAN process in Tx
305          * and Rx is correct. But in the window period of this state transition,
306          * packets loss or packets with wrong VLAN may occur.
307          */
308         if (hw->port_base_vlan_cfg.state != new_pvid_state) {
309                 hw->port_base_vlan_cfg.state = new_pvid_state;
310                 hns3_update_all_queues_pvid_proc_en(hw);
311         }
312 }
313
314 static void
315 hns3_handle_promisc_info(struct hns3_hw *hw, uint16_t promisc_en)
316 {
317         if (!promisc_en) {
318                 /*
319                  * When promisc/allmulti mode is closed by the hns3 PF kernel
320                  * ethdev driver for untrusted, modify VF's related status.
321                  */
322                 hns3_warn(hw, "Promisc mode will be closed by host for being "
323                               "untrusted.");
324                 hw->data->promiscuous = 0;
325                 hw->data->all_multicast = 0;
326         }
327 }
328
329 void
330 hns3_dev_handle_mbx_msg(struct hns3_hw *hw)
331 {
332         struct hns3_mbx_resp_status *resp = &hw->mbx_resp;
333         struct hns3_cmq_ring *crq = &hw->cmq.crq;
334         struct hns3_mbx_pf_to_vf_cmd *req;
335         struct hns3_cmd_desc *desc;
336         uint32_t msg_data;
337         uint16_t *msg_q;
338         uint8_t opcode;
339         uint16_t flag;
340         uint8_t *temp;
341         int i;
342
343         rte_spinlock_lock(&hw->cmq.crq.lock);
344
345         while (!hns3_cmd_crq_empty(hw)) {
346                 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED)) {
347                         rte_spinlock_unlock(&hw->cmq.crq.lock);
348                         return;
349                 }
350
351                 desc = &crq->desc[crq->next_to_use];
352                 req = (struct hns3_mbx_pf_to_vf_cmd *)desc->data;
353                 opcode = req->msg[0] & 0xff;
354
355                 flag = rte_le_to_cpu_16(crq->desc[crq->next_to_use].flag);
356                 if (unlikely(!hns3_get_bit(flag, HNS3_CMDQ_RX_OUTVLD_B))) {
357                         hns3_warn(hw,
358                                   "dropped invalid mailbox message, code = %u",
359                                   opcode);
360
361                         /* dropping/not processing this invalid message */
362                         crq->desc[crq->next_to_use].flag = 0;
363                         hns3_mbx_ring_ptr_move_crq(crq);
364                         continue;
365                 }
366
367                 switch (opcode) {
368                 case HNS3_MBX_PF_VF_RESP:
369                         resp->resp_status = hns3_resp_to_errno(req->msg[3]);
370
371                         temp = (uint8_t *)&req->msg[4];
372                         for (i = 0; i < HNS3_MBX_MAX_RESP_DATA_SIZE; i++) {
373                                 resp->additional_info[i] = *temp;
374                                 temp++;
375                         }
376                         msg_data = (uint32_t)req->msg[1] << 16 | req->msg[2];
377                         hns3_update_resp_position(hw, msg_data);
378                         break;
379                 case HNS3_MBX_LINK_STAT_CHANGE:
380                 case HNS3_MBX_ASSERTING_RESET:
381                         msg_q = hw->arq.msg_q[hw->arq.tail];
382                         memcpy(&msg_q[0], req->msg,
383                                HNS3_MBX_MAX_ARQ_MSG_SIZE * sizeof(uint16_t));
384                         hns3_mbx_tail_ptr_move_arq(hw->arq);
385
386                         hns3_mbx_handler(hw);
387                         break;
388                 case HNS3_MBX_PUSH_LINK_STATUS:
389                         hns3_handle_link_change_event(hw, req);
390                         break;
391                 case HNS3_MBX_PUSH_VLAN_INFO:
392                         /*
393                          * When the PVID configuration status of VF device is
394                          * changed by the hns3 PF kernel driver, VF driver will
395                          * receive this mailbox message from PF driver.
396                          */
397                         hns3_update_port_base_vlan_info(hw, req);
398                         break;
399                 case HNS3_MBX_PUSH_PROMISC_INFO:
400                         /*
401                          * When the trust status of VF device changed by the
402                          * hns3 PF kernel driver, VF driver will receive this
403                          * mailbox message from PF driver.
404                          */
405                         hns3_handle_promisc_info(hw, req->msg[1]);
406                         break;
407                 default:
408                         hns3_err(hw,
409                                  "VF received unsupported(%u) mbx msg from PF",
410                                  req->msg[0]);
411                         break;
412                 }
413
414                 crq->desc[crq->next_to_use].flag = 0;
415                 hns3_mbx_ring_ptr_move_crq(crq);
416         }
417
418         /* Write back CMDQ_RQ header pointer, IMP need this pointer */
419         hns3_write_dev(hw, HNS3_CMDQ_RX_HEAD_REG, crq->next_to_use);
420
421         rte_spinlock_unlock(&hw->cmq.crq.lock);
422 }