1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2019 Hisilicon Limited.
12 #include <rte_byteorder.h>
13 #include <rte_common.h>
14 #include <rte_cycles.h>
16 #include <rte_ethdev_driver.h>
18 #include <rte_spinlock.h>
20 #include <rte_bus_pci.h>
22 #include "hns3_ethdev.h"
23 #include "hns3_regs.h"
24 #include "hns3_logs.h"
25 #include "hns3_intr.h"
27 #define HNS3_CMD_CODE_OFFSET 2
29 static const struct errno_respcode_map err_code_map[] = {
43 hns3_resp_to_errno(uint16_t resp_code)
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;
57 hns3_poll_all_sync_msg(void)
59 struct rte_eth_dev *eth_dev;
60 struct hns3_adapter *adapter;
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"))
69 adapter = eth_dev->data->dev_private;
70 if (!adapter || adapter->hw.adapter_state == HNS3_NIC_CLOSED)
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);
79 hns3_get_mbx_resp(struct hns3_hw *hw, uint16_t code0, uint16_t code1,
80 uint8_t *resp_data, uint16_t resp_len)
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;
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);
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) &&
99 if (rte_atomic16_read(&hw->reset.disable_cmd)) {
100 hns3_err(hw, "Don't wait for mbx respone because of "
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 "
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.
117 if (pthread_equal(hw->irq_thread_id, pthread_self())) {
119 hns3_poll_all_sync_msg();
121 rte_delay_ms(HNS3_POLL_RESPONE_MS);
123 now = get_timeofday_ms();
125 hw->mbx_resp.req_msg_data = 0;
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);
135 mbx_resp = &hw->mbx_resp;
137 if (mbx_resp->resp_status)
138 return mbx_resp->resp_status;
141 memcpy(resp_data, &mbx_resp->additional_info[0], resp_len);
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)
151 struct hns3_mbx_vf_to_pf_cmd *req;
152 struct hns3_cmd_desc desc;
153 bool is_ring_vector_msg;
157 req = (struct hns3_mbx_vf_to_pf_cmd *)desc.data;
159 /* first two bytes are reserved for code & subcode */
160 if (msg_len > (HNS3_MBX_MAX_MSG_SIZE - HNS3_CMD_CODE_OFFSET)) {
162 "VF send mbx msg fail, msg len %d exceeds max payload len %d",
163 msg_len, HNS3_MBX_MAX_MSG_SIZE - HNS3_CMD_CODE_OFFSET);
167 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MBX_VF_TO_PF, false);
169 is_ring_vector_msg = (code == HNS3_MBX_MAP_RING_TO_VECTOR) ||
170 (code == HNS3_MBX_UNMAP_RING_TO_VECTOR) ||
171 (code == HNS3_MBX_GET_RING_VECTOR_MAP);
172 if (!is_ring_vector_msg)
173 req->msg[1] = subcode;
175 offset = is_ring_vector_msg ? 1 : HNS3_CMD_CODE_OFFSET;
176 memcpy(&req->msg[offset], msg_data, msg_len);
179 /* synchronous send */
181 req->mbx_need_resp |= HNS3_MBX_NEED_RESP_BIT;
182 rte_spinlock_lock(&hw->mbx_resp.lock);
183 hw->mbx_resp.req_msg_data = (uint32_t)code << 16 | subcode;
185 ret = hns3_cmd_send(hw, &desc, 1);
187 rte_spinlock_unlock(&hw->mbx_resp.lock);
188 hns3_err(hw, "VF failed(=%d) to send mbx message to PF",
193 ret = hns3_get_mbx_resp(hw, code, subcode, resp_data, resp_len);
194 rte_spinlock_unlock(&hw->mbx_resp.lock);
196 /* asynchronous send */
197 ret = hns3_cmd_send(hw, &desc, 1);
199 hns3_err(hw, "VF failed(=%d) to send mbx message to PF",
209 hns3_cmd_crq_empty(struct hns3_hw *hw)
211 uint32_t tail = hns3_read_dev(hw, HNS3_CMDQ_RX_TAIL_REG);
213 return tail == hw->cmq.crq.next_to_use;
217 hns3_mbx_handler(struct hns3_hw *hw)
219 struct hns3_mac *mac = &hw->mac;
220 enum hns3_reset_level reset_level;
227 /* process all the async queue messages */
228 while (tail != hw->arq.head) {
229 msg_q = hw->arq.msg_q[hw->arq.head];
231 opcode = msg_q[0] & 0xff;
233 case HNS3_MBX_LINK_STAT_CHANGE:
234 memcpy(&mac->link_speed, &msg_q[2],
235 sizeof(mac->link_speed));
236 mac->link_status = rte_le_to_cpu_16(msg_q[1]);
237 mac->link_duplex = (uint8_t)rte_le_to_cpu_16(msg_q[4]);
239 case HNS3_MBX_ASSERTING_RESET:
240 /* PF has asserted reset hence VF should go in pending
241 * state and poll for the hardware reset status till it
242 * has been completely reset. After this stack should
243 * eventually be re-initialized.
245 reset_level = rte_le_to_cpu_16(msg_q[1]);
246 hns3_atomic_set_bit(reset_level, &hw->reset.pending);
248 hns3_warn(hw, "PF inform reset level %d", reset_level);
249 hw->reset.stats.request_cnt++;
250 hns3_schedule_reset(HNS3_DEV_HW_TO_ADAPTER(hw));
253 hns3_err(hw, "Fetched unsupported(%d) message from arq",
258 hns3_mbx_head_ptr_move_arq(hw->arq);
259 msg_q = hw->arq.msg_q[hw->arq.head];
264 * Case1: receive response after timeout, req_msg_data
265 * is 0, not equal resp_msg, do lost--
266 * Case2: receive last response during new send_mbx_msg,
267 * req_msg_data is different with resp_msg, let
268 * lost--, continue to wait for response.
271 hns3_update_resp_position(struct hns3_hw *hw, uint32_t resp_msg)
273 struct hns3_mbx_resp_status *resp = &hw->mbx_resp;
274 uint32_t tail = resp->tail + 1;
276 if (tail > resp->head)
278 if (resp->req_msg_data != resp_msg) {
281 hns3_warn(hw, "Received a mismatched response req_msg(%x) "
282 "resp_msg(%x) head(%d) tail(%d) lost(%d)",
283 resp->req_msg_data, resp_msg, resp->head, tail,
285 } else if (tail + resp->lost > resp->head) {
287 hns3_warn(hw, "Received a new response again resp_msg(%x) "
288 "head(%d) tail(%d) lost(%d)", resp_msg,
289 resp->head, tail, resp->lost);
296 hns3_link_fail_parse(struct hns3_hw *hw, uint8_t link_fail_code)
298 switch (link_fail_code) {
299 case HNS3_MBX_LF_NORMAL:
301 case HNS3_MBX_LF_REF_CLOCK_LOST:
302 hns3_warn(hw, "Reference clock lost!");
304 case HNS3_MBX_LF_XSFP_TX_DISABLE:
305 hns3_warn(hw, "SFP tx is disabled!");
307 case HNS3_MBX_LF_XSFP_ABSENT:
308 hns3_warn(hw, "SFP is absent!");
311 hns3_warn(hw, "Unknown fail code:%u!", link_fail_code);
317 hns3_handle_link_change_event(struct hns3_hw *hw,
318 struct hns3_mbx_pf_to_vf_cmd *req)
320 #define LINK_STATUS_OFFSET 1
321 #define LINK_FAIL_CODE_OFFSET 2
323 if (!req->msg[LINK_STATUS_OFFSET])
324 hns3_link_fail_parse(hw, req->msg[LINK_FAIL_CODE_OFFSET]);
326 hns3_update_link_status(hw);
330 hns3_handle_promisc_info(struct hns3_hw *hw, uint16_t promisc_en)
334 * When promisc/allmulti mode is closed by the hns3 PF kernel
335 * ethdev driver for untrusted, modify VF's related status.
337 hns3_warn(hw, "Promisc mode will be closed by host for being "
339 hw->data->promiscuous = 0;
340 hw->data->all_multicast = 0;
345 hns3_dev_handle_mbx_msg(struct hns3_hw *hw)
347 struct hns3_mbx_resp_status *resp = &hw->mbx_resp;
348 struct hns3_cmq_ring *crq = &hw->cmq.crq;
349 struct hns3_mbx_pf_to_vf_cmd *req;
350 struct hns3_cmd_desc *desc;
358 while (!hns3_cmd_crq_empty(hw)) {
359 if (rte_atomic16_read(&hw->reset.disable_cmd))
362 desc = &crq->desc[crq->next_to_use];
363 req = (struct hns3_mbx_pf_to_vf_cmd *)desc->data;
364 opcode = req->msg[0] & 0xff;
366 flag = rte_le_to_cpu_16(crq->desc[crq->next_to_use].flag);
367 if (unlikely(!hns3_get_bit(flag, HNS3_CMDQ_RX_OUTVLD_B))) {
369 "dropped invalid mailbox message, code = %d",
372 /* dropping/not processing this invalid message */
373 crq->desc[crq->next_to_use].flag = 0;
374 hns3_mbx_ring_ptr_move_crq(crq);
379 case HNS3_MBX_PF_VF_RESP:
380 resp->resp_status = hns3_resp_to_errno(req->msg[3]);
382 temp = (uint8_t *)&req->msg[4];
383 for (i = 0; i < HNS3_MBX_MAX_RESP_DATA_SIZE; i++) {
384 resp->additional_info[i] = *temp;
387 msg_data = (uint32_t)req->msg[1] << 16 | req->msg[2];
388 hns3_update_resp_position(hw, msg_data);
390 case HNS3_MBX_LINK_STAT_CHANGE:
391 case HNS3_MBX_ASSERTING_RESET:
392 msg_q = hw->arq.msg_q[hw->arq.tail];
393 memcpy(&msg_q[0], req->msg,
394 HNS3_MBX_MAX_ARQ_MSG_SIZE * sizeof(uint16_t));
395 hns3_mbx_tail_ptr_move_arq(hw->arq);
397 hns3_mbx_handler(hw);
399 case HNS3_MBX_PUSH_LINK_STATUS:
400 hns3_handle_link_change_event(hw, req);
402 case HNS3_MBX_PUSH_PROMISC_INFO:
404 * When the trust status of VF device changed by the
405 * hns3 PF kernel driver, VF driver will receive this
406 * mailbox message from PF driver.
408 hns3_handle_promisc_info(hw, req->msg[1]);
412 "VF received unsupported(%d) mbx msg from PF",
417 crq->desc[crq->next_to_use].flag = 0;
418 hns3_mbx_ring_ptr_move_crq(crq);
421 /* Write back CMDQ_RQ header pointer, IMP need this pointer */
422 hns3_write_dev(hw, HNS3_CMDQ_RX_HEAD_REG, crq->next_to_use);