net/hns3: get link state change through mailbox
[dpdk.git] / drivers / net / hns3 / hns3_mbx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2019 Hisilicon Limited.
3  */
4
5 #include <errno.h>
6 #include <stdbool.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <inttypes.h>
11 #include <unistd.h>
12 #include <rte_byteorder.h>
13 #include <rte_common.h>
14 #include <rte_cycles.h>
15 #include <rte_dev.h>
16 #include <rte_ethdev_driver.h>
17 #include <rte_io.h>
18 #include <rte_spinlock.h>
19 #include <rte_pci.h>
20 #include <rte_bus_pci.h>
21
22 #include "hns3_ethdev.h"
23 #include "hns3_regs.h"
24 #include "hns3_logs.h"
25 #include "hns3_intr.h"
26
27 #define HNS3_CMD_CODE_OFFSET            2
28
29 static const struct errno_respcode_map err_code_map[] = {
30         {0, 0},
31         {1, -EPERM},
32         {2, -ENOENT},
33         {5, -EIO},
34         {11, -EAGAIN},
35         {12, -ENOMEM},
36         {16, -EBUSY},
37         {22, -EINVAL},
38         {28, -ENOSPC},
39         {95, -EOPNOTSUPP},
40 };
41
42 static int
43 hns3_resp_to_errno(uint16_t resp_code)
44 {
45         uint32_t i, num;
46
47         num = sizeof(err_code_map) / sizeof(struct errno_respcode_map);
48         for (i = 0; i < num; i++) {
49                 if (err_code_map[i].resp_code == resp_code)
50                         return err_code_map[i].err_no;
51         }
52
53         return -EIO;
54 }
55
56 static void
57 hns3_poll_all_sync_msg(void)
58 {
59         struct rte_eth_dev *eth_dev;
60         struct hns3_adapter *adapter;
61         const char *name;
62         uint16_t port_id;
63
64         RTE_ETH_FOREACH_DEV(port_id) {
65                 eth_dev = &rte_eth_devices[port_id];
66                 name = eth_dev->device->driver->name;
67                 if (strcmp(name, "net_hns3") && strcmp(name, "net_hns3_vf"))
68                         continue;
69                 adapter = eth_dev->data->dev_private;
70                 if (!adapter || adapter->hw.adapter_state == HNS3_NIC_CLOSED)
71                         continue;
72                 /* Synchronous msg, the mbx_resp.req_msg_data is non-zero */
73                 if (adapter->hw.mbx_resp.req_msg_data)
74                         hns3_dev_handle_mbx_msg(&adapter->hw);
75         }
76 }
77
78 static int
79 hns3_get_mbx_resp(struct hns3_hw *hw, uint16_t code0, uint16_t code1,
80                   uint8_t *resp_data, uint16_t resp_len)
81 {
82 #define HNS3_MAX_RETRY_MS       500
83         struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
84         struct hns3_mbx_resp_status *mbx_resp;
85         bool in_irq = false;
86         uint64_t now;
87         uint64_t end;
88
89         if (resp_len > HNS3_MBX_MAX_RESP_DATA_SIZE) {
90                 hns3_err(hw, "VF mbx response len(=%d) exceeds maximum(=%d)",
91                          resp_len, HNS3_MBX_MAX_RESP_DATA_SIZE);
92                 return -EINVAL;
93         }
94
95         now = get_timeofday_ms();
96         end = now + HNS3_MAX_RETRY_MS;
97         while ((hw->mbx_resp.head != hw->mbx_resp.tail + hw->mbx_resp.lost) &&
98                (now < end)) {
99                 if (rte_atomic16_read(&hw->reset.disable_cmd)) {
100                         hns3_err(hw, "Don't wait for mbx respone because of "
101                                  "disable_cmd");
102                         return -EBUSY;
103                 }
104
105                 if (is_reset_pending(hns)) {
106                         hw->mbx_resp.req_msg_data = 0;
107                         hns3_err(hw, "Don't wait for mbx respone because of "
108                                  "reset pending");
109                         return -EIO;
110                 }
111
112                 /*
113                  * The mbox response is running on the interrupt thread.
114                  * Sending mbox in the interrupt thread cannot wait for the
115                  * response, so polling the mbox response on the irq thread.
116                  */
117                 if (pthread_equal(hw->irq_thread_id, pthread_self())) {
118                         in_irq = true;
119                         hns3_poll_all_sync_msg();
120                 } else {
121                         rte_delay_ms(HNS3_POLL_RESPONE_MS);
122                 }
123                 now = get_timeofday_ms();
124         }
125         hw->mbx_resp.req_msg_data = 0;
126         if (now >= end) {
127                 hw->mbx_resp.lost++;
128                 hns3_err(hw,
129                          "VF could not get mbx(%d,%d) head(%d) tail(%d) lost(%d) from PF in_irq:%d",
130                          code0, code1, hw->mbx_resp.head, hw->mbx_resp.tail,
131                          hw->mbx_resp.lost, in_irq);
132                 return -ETIME;
133         }
134         rte_io_rmb();
135         mbx_resp = &hw->mbx_resp;
136
137         if (mbx_resp->resp_status)
138                 return mbx_resp->resp_status;
139
140         if (resp_data)
141                 memcpy(resp_data, &mbx_resp->additional_info[0], resp_len);
142
143         return 0;
144 }
145
146 int
147 hns3_send_mbx_msg(struct hns3_hw *hw, uint16_t code, uint16_t subcode,
148                   const uint8_t *msg_data, uint8_t msg_len, bool need_resp,
149                   uint8_t *resp_data, uint16_t resp_len)
150 {
151         struct hns3_mbx_vf_to_pf_cmd *req;
152         struct hns3_cmd_desc desc;
153         int ret;
154
155         req = (struct hns3_mbx_vf_to_pf_cmd *)desc.data;
156
157         /* first two bytes are reserved for code & subcode */
158         if (msg_len > (HNS3_MBX_MAX_MSG_SIZE - HNS3_CMD_CODE_OFFSET)) {
159                 hns3_err(hw,
160                          "VF send mbx msg fail, msg len %d exceeds max payload len %d",
161                          msg_len, HNS3_MBX_MAX_MSG_SIZE - HNS3_CMD_CODE_OFFSET);
162                 return -EINVAL;
163         }
164
165         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MBX_VF_TO_PF, false);
166         req->msg[0] = code;
167         req->msg[1] = subcode;
168         if (msg_data)
169                 memcpy(&req->msg[HNS3_CMD_CODE_OFFSET], msg_data, msg_len);
170
171         /* synchronous send */
172         if (need_resp) {
173                 req->mbx_need_resp |= HNS3_MBX_NEED_RESP_BIT;
174                 rte_spinlock_lock(&hw->mbx_resp.lock);
175                 hw->mbx_resp.req_msg_data = (uint32_t)code << 16 | subcode;
176                 hw->mbx_resp.head++;
177                 ret = hns3_cmd_send(hw, &desc, 1);
178                 if (ret) {
179                         rte_spinlock_unlock(&hw->mbx_resp.lock);
180                         hns3_err(hw, "VF failed(=%d) to send mbx message to PF",
181                                  ret);
182                         return ret;
183                 }
184
185                 ret = hns3_get_mbx_resp(hw, code, subcode, resp_data, resp_len);
186                 rte_spinlock_unlock(&hw->mbx_resp.lock);
187         } else {
188                 /* asynchronous send */
189                 ret = hns3_cmd_send(hw, &desc, 1);
190                 if (ret) {
191                         hns3_err(hw, "VF failed(=%d) to send mbx message to PF",
192                                  ret);
193                         return ret;
194                 }
195         }
196
197         return ret;
198 }
199
200 static bool
201 hns3_cmd_crq_empty(struct hns3_hw *hw)
202 {
203         uint32_t tail = hns3_read_dev(hw, HNS3_CMDQ_RX_TAIL_REG);
204
205         return tail == hw->cmq.crq.next_to_use;
206 }
207
208 static void
209 hns3_mbx_handler(struct hns3_hw *hw)
210 {
211         struct hns3_mac *mac = &hw->mac;
212         enum hns3_reset_level reset_level;
213         uint16_t *msg_q;
214         uint32_t tail;
215
216         tail = hw->arq.tail;
217
218         /* process all the async queue messages */
219         while (tail != hw->arq.head) {
220                 msg_q = hw->arq.msg_q[hw->arq.head];
221
222                 switch (msg_q[0]) {
223                 case HNS3_MBX_LINK_STAT_CHANGE:
224                         memcpy(&mac->link_speed, &msg_q[2],
225                                    sizeof(mac->link_speed));
226                         mac->link_status = rte_le_to_cpu_16(msg_q[1]);
227                         mac->link_duplex = (uint8_t)rte_le_to_cpu_16(msg_q[4]);
228                         break;
229                 case HNS3_MBX_ASSERTING_RESET:
230                         /* PF has asserted reset hence VF should go in pending
231                          * state and poll for the hardware reset status till it
232                          * has been completely reset. After this stack should
233                          * eventually be re-initialized.
234                          */
235                         reset_level = rte_le_to_cpu_16(msg_q[1]);
236                         hns3_atomic_set_bit(reset_level, &hw->reset.pending);
237
238                         hns3_warn(hw, "PF inform reset level %d", reset_level);
239                         hw->reset.stats.request_cnt++;
240                         hns3_schedule_reset(HNS3_DEV_HW_TO_ADAPTER(hw));
241                         break;
242                 default:
243                         hns3_err(hw, "Fetched unsupported(%d) message from arq",
244                                  msg_q[0]);
245                         break;
246                 }
247
248                 hns3_mbx_head_ptr_move_arq(hw->arq);
249                 msg_q = hw->arq.msg_q[hw->arq.head];
250         }
251 }
252
253 /*
254  * Case1: receive response after timeout, req_msg_data
255  *        is 0, not equal resp_msg, do lost--
256  * Case2: receive last response during new send_mbx_msg,
257  *        req_msg_data is different with resp_msg, let
258  *        lost--, continue to wait for response.
259  */
260 static void
261 hns3_update_resp_position(struct hns3_hw *hw, uint32_t resp_msg)
262 {
263         struct hns3_mbx_resp_status *resp = &hw->mbx_resp;
264         uint32_t tail = resp->tail + 1;
265
266         if (tail > resp->head)
267                 tail = resp->head;
268         if (resp->req_msg_data != resp_msg) {
269                 if (resp->lost)
270                         resp->lost--;
271                 hns3_warn(hw, "Received a mismatched response req_msg(%x) "
272                           "resp_msg(%x) head(%d) tail(%d) lost(%d)",
273                           resp->req_msg_data, resp_msg, resp->head, tail,
274                           resp->lost);
275         } else if (tail + resp->lost > resp->head) {
276                 resp->lost--;
277                 hns3_warn(hw, "Received a new response again resp_msg(%x) "
278                           "head(%d) tail(%d) lost(%d)", resp_msg,
279                           resp->head, tail, resp->lost);
280         }
281         rte_io_wmb();
282         resp->tail = tail;
283 }
284
285 static void
286 hns3_link_fail_parse(struct hns3_hw *hw, uint8_t link_fail_code)
287 {
288         switch (link_fail_code) {
289         case HNS3_MBX_LF_NORMAL:
290                 break;
291         case HNS3_MBX_LF_REF_CLOCK_LOST:
292                 hns3_warn(hw, "Reference clock lost!");
293                 break;
294         case HNS3_MBX_LF_XSFP_TX_DISABLE:
295                 hns3_warn(hw, "SFP tx is disabled!");
296                 break;
297         case HNS3_MBX_LF_XSFP_ABSENT:
298                 hns3_warn(hw, "SFP is absent!");
299                 break;
300         default:
301                 hns3_warn(hw, "Unknown fail code:%u!", link_fail_code);
302                 break;
303         }
304 }
305
306 static void
307 hns3_handle_link_change_event(struct hns3_hw *hw,
308                               struct hns3_mbx_pf_to_vf_cmd *req)
309 {
310 #define LINK_STATUS_OFFSET     1
311 #define LINK_FAIL_CODE_OFFSET  2
312
313         if (!req->msg[LINK_STATUS_OFFSET])
314                 hns3_link_fail_parse(hw, req->msg[LINK_FAIL_CODE_OFFSET]);
315
316         hns3_update_link_status(hw);
317 }
318
319 void
320 hns3_dev_handle_mbx_msg(struct hns3_hw *hw)
321 {
322         struct hns3_mbx_resp_status *resp = &hw->mbx_resp;
323         struct hns3_cmq_ring *crq = &hw->cmq.crq;
324         struct hns3_mbx_pf_to_vf_cmd *req;
325         struct hns3_cmd_desc *desc;
326         uint32_t msg_data;
327         uint16_t *msg_q;
328         uint16_t flag;
329         uint8_t *temp;
330         int i;
331
332         while (!hns3_cmd_crq_empty(hw)) {
333                 if (rte_atomic16_read(&hw->reset.disable_cmd))
334                         return;
335
336                 desc = &crq->desc[crq->next_to_use];
337                 req = (struct hns3_mbx_pf_to_vf_cmd *)desc->data;
338
339                 flag = rte_le_to_cpu_16(crq->desc[crq->next_to_use].flag);
340                 if (unlikely(!hns3_get_bit(flag, HNS3_CMDQ_RX_OUTVLD_B))) {
341                         hns3_warn(hw,
342                                   "dropped invalid mailbox message, code = %d",
343                                   req->msg[0]);
344
345                         /* dropping/not processing this invalid message */
346                         crq->desc[crq->next_to_use].flag = 0;
347                         hns3_mbx_ring_ptr_move_crq(crq);
348                         continue;
349                 }
350
351                 switch (req->msg[0]) {
352                 case HNS3_MBX_PF_VF_RESP:
353                         resp->resp_status = hns3_resp_to_errno(req->msg[3]);
354
355                         temp = (uint8_t *)&req->msg[4];
356                         for (i = 0; i < HNS3_MBX_MAX_RESP_DATA_SIZE; i++) {
357                                 resp->additional_info[i] = *temp;
358                                 temp++;
359                         }
360                         msg_data = (uint32_t)req->msg[1] << 16 | req->msg[2];
361                         hns3_update_resp_position(hw, msg_data);
362                         break;
363                 case HNS3_MBX_LINK_STAT_CHANGE:
364                 case HNS3_MBX_ASSERTING_RESET:
365                         msg_q = hw->arq.msg_q[hw->arq.tail];
366                         memcpy(&msg_q[0], req->msg,
367                                HNS3_MBX_MAX_ARQ_MSG_SIZE * sizeof(uint16_t));
368                         hns3_mbx_tail_ptr_move_arq(hw->arq);
369
370                         hns3_mbx_handler(hw);
371                         break;
372                 case HNS3_MBX_PUSH_LINK_STATUS:
373                         hns3_handle_link_change_event(hw, req);
374                         break;
375                 default:
376                         hns3_err(hw,
377                                  "VF received unsupported(%d) mbx msg from PF",
378                                  req->msg[0]);
379                         break;
380                 }
381
382                 crq->desc[crq->next_to_use].flag = 0;
383                 hns3_mbx_ring_ptr_move_crq(crq);
384         }
385
386         /* Write back CMDQ_RQ header pointer, IMP need this pointer */
387         hns3_write_dev(hw, HNS3_CMDQ_RX_HEAD_REG, crq->next_to_use);
388 }