1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
4 #include <linux/etherdevice.h>
5 #include <linux/iopoll.h>
6 #include <net/rtnetlink.h>
7 #include "hclgevf_cmd.h"
8 #include "hclgevf_main.h"
11 #include "hclgevf_devlink.h"
13 #define HCLGEVF_NAME "hclgevf"
15 #define HCLGEVF_RESET_MAX_FAIL_CNT 5
17 static int hclgevf_reset_hdev(struct hclgevf_dev *hdev);
18 static void hclgevf_task_schedule(struct hclgevf_dev *hdev,
21 static struct hnae3_ae_algo ae_algovf;
23 static struct workqueue_struct *hclgevf_wq;
25 static const struct pci_device_id ae_algovf_pci_tbl[] = {
26 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_VF), 0},
27 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_RDMA_DCB_PFC_VF),
28 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
29 /* required last entry */
33 static const u8 hclgevf_hash_key[] = {
34 0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
35 0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
36 0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
37 0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
38 0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA
41 MODULE_DEVICE_TABLE(pci, ae_algovf_pci_tbl);
43 static const u32 cmdq_reg_addr_list[] = {HCLGEVF_NIC_CSQ_BASEADDR_L_REG,
44 HCLGEVF_NIC_CSQ_BASEADDR_H_REG,
45 HCLGEVF_NIC_CSQ_DEPTH_REG,
46 HCLGEVF_NIC_CSQ_TAIL_REG,
47 HCLGEVF_NIC_CSQ_HEAD_REG,
48 HCLGEVF_NIC_CRQ_BASEADDR_L_REG,
49 HCLGEVF_NIC_CRQ_BASEADDR_H_REG,
50 HCLGEVF_NIC_CRQ_DEPTH_REG,
51 HCLGEVF_NIC_CRQ_TAIL_REG,
52 HCLGEVF_NIC_CRQ_HEAD_REG,
53 HCLGEVF_VECTOR0_CMDQ_SRC_REG,
54 HCLGEVF_VECTOR0_CMDQ_STATE_REG,
55 HCLGEVF_CMDQ_INTR_EN_REG,
56 HCLGEVF_CMDQ_INTR_GEN_REG};
58 static const u32 common_reg_addr_list[] = {HCLGEVF_MISC_VECTOR_REG_BASE,
62 static const u32 ring_reg_addr_list[] = {HCLGEVF_RING_RX_ADDR_L_REG,
63 HCLGEVF_RING_RX_ADDR_H_REG,
64 HCLGEVF_RING_RX_BD_NUM_REG,
65 HCLGEVF_RING_RX_BD_LENGTH_REG,
66 HCLGEVF_RING_RX_MERGE_EN_REG,
67 HCLGEVF_RING_RX_TAIL_REG,
68 HCLGEVF_RING_RX_HEAD_REG,
69 HCLGEVF_RING_RX_FBD_NUM_REG,
70 HCLGEVF_RING_RX_OFFSET_REG,
71 HCLGEVF_RING_RX_FBD_OFFSET_REG,
72 HCLGEVF_RING_RX_STASH_REG,
73 HCLGEVF_RING_RX_BD_ERR_REG,
74 HCLGEVF_RING_TX_ADDR_L_REG,
75 HCLGEVF_RING_TX_ADDR_H_REG,
76 HCLGEVF_RING_TX_BD_NUM_REG,
77 HCLGEVF_RING_TX_PRIORITY_REG,
78 HCLGEVF_RING_TX_TC_REG,
79 HCLGEVF_RING_TX_MERGE_EN_REG,
80 HCLGEVF_RING_TX_TAIL_REG,
81 HCLGEVF_RING_TX_HEAD_REG,
82 HCLGEVF_RING_TX_FBD_NUM_REG,
83 HCLGEVF_RING_TX_OFFSET_REG,
84 HCLGEVF_RING_TX_EBD_NUM_REG,
85 HCLGEVF_RING_TX_EBD_OFFSET_REG,
86 HCLGEVF_RING_TX_BD_ERR_REG,
89 static const u32 tqp_intr_reg_addr_list[] = {HCLGEVF_TQP_INTR_CTRL_REG,
90 HCLGEVF_TQP_INTR_GL0_REG,
91 HCLGEVF_TQP_INTR_GL1_REG,
92 HCLGEVF_TQP_INTR_GL2_REG,
93 HCLGEVF_TQP_INTR_RL_REG};
95 static struct hclgevf_dev *hclgevf_ae_get_hdev(struct hnae3_handle *handle)
98 return container_of(handle, struct hclgevf_dev, nic);
99 else if (handle->client->type == HNAE3_CLIENT_ROCE)
100 return container_of(handle, struct hclgevf_dev, roce);
102 return container_of(handle, struct hclgevf_dev, nic);
105 static int hclgevf_tqps_update_stats(struct hnae3_handle *handle)
107 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
108 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
109 struct hclgevf_desc desc;
110 struct hclgevf_tqp *tqp;
114 for (i = 0; i < kinfo->num_tqps; i++) {
115 tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
116 hclgevf_cmd_setup_basic_desc(&desc,
117 HCLGEVF_OPC_QUERY_RX_STATUS,
120 desc.data[0] = cpu_to_le32(tqp->index & 0x1ff);
121 status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
123 dev_err(&hdev->pdev->dev,
124 "Query tqp stat fail, status = %d,queue = %d\n",
128 tqp->tqp_stats.rcb_rx_ring_pktnum_rcd +=
129 le32_to_cpu(desc.data[1]);
131 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_QUERY_TX_STATUS,
134 desc.data[0] = cpu_to_le32(tqp->index & 0x1ff);
135 status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
137 dev_err(&hdev->pdev->dev,
138 "Query tqp stat fail, status = %d,queue = %d\n",
142 tqp->tqp_stats.rcb_tx_ring_pktnum_rcd +=
143 le32_to_cpu(desc.data[1]);
149 static u64 *hclgevf_tqps_get_stats(struct hnae3_handle *handle, u64 *data)
151 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
152 struct hclgevf_tqp *tqp;
156 for (i = 0; i < kinfo->num_tqps; i++) {
157 tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
158 *buff++ = tqp->tqp_stats.rcb_tx_ring_pktnum_rcd;
160 for (i = 0; i < kinfo->num_tqps; i++) {
161 tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
162 *buff++ = tqp->tqp_stats.rcb_rx_ring_pktnum_rcd;
168 static int hclgevf_tqps_get_sset_count(struct hnae3_handle *handle, int strset)
170 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
172 return kinfo->num_tqps * 2;
175 static u8 *hclgevf_tqps_get_strings(struct hnae3_handle *handle, u8 *data)
177 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
181 for (i = 0; i < kinfo->num_tqps; i++) {
182 struct hclgevf_tqp *tqp = container_of(kinfo->tqp[i],
183 struct hclgevf_tqp, q);
184 snprintf(buff, ETH_GSTRING_LEN, "txq%u_pktnum_rcd",
186 buff += ETH_GSTRING_LEN;
189 for (i = 0; i < kinfo->num_tqps; i++) {
190 struct hclgevf_tqp *tqp = container_of(kinfo->tqp[i],
191 struct hclgevf_tqp, q);
192 snprintf(buff, ETH_GSTRING_LEN, "rxq%u_pktnum_rcd",
194 buff += ETH_GSTRING_LEN;
200 static void hclgevf_update_stats(struct hnae3_handle *handle,
201 struct net_device_stats *net_stats)
203 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
206 status = hclgevf_tqps_update_stats(handle);
208 dev_err(&hdev->pdev->dev,
209 "VF update of TQPS stats fail, status = %d.\n",
213 static int hclgevf_get_sset_count(struct hnae3_handle *handle, int strset)
215 if (strset == ETH_SS_TEST)
217 else if (strset == ETH_SS_STATS)
218 return hclgevf_tqps_get_sset_count(handle, strset);
223 static void hclgevf_get_strings(struct hnae3_handle *handle, u32 strset,
226 u8 *p = (char *)data;
228 if (strset == ETH_SS_STATS)
229 p = hclgevf_tqps_get_strings(handle, p);
232 static void hclgevf_get_stats(struct hnae3_handle *handle, u64 *data)
234 hclgevf_tqps_get_stats(handle, data);
237 static void hclgevf_build_send_msg(struct hclge_vf_to_pf_msg *msg, u8 code,
241 memset(msg, 0, sizeof(struct hclge_vf_to_pf_msg));
243 msg->subcode = subcode;
247 static int hclgevf_get_basic_info(struct hclgevf_dev *hdev)
249 struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
250 u8 resp_msg[HCLGE_MBX_MAX_RESP_DATA_SIZE];
251 struct hclge_basic_info *basic_info;
252 struct hclge_vf_to_pf_msg send_msg;
256 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_BASIC_INFO, 0);
257 status = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
260 dev_err(&hdev->pdev->dev,
261 "failed to get basic info from pf, ret = %d", status);
265 basic_info = (struct hclge_basic_info *)resp_msg;
267 hdev->hw_tc_map = basic_info->hw_tc_map;
268 hdev->mbx_api_version = basic_info->mbx_api_version;
269 caps = basic_info->pf_caps;
270 if (test_bit(HNAE3_PF_SUPPORT_VLAN_FLTR_MDF_B, &caps))
271 set_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, ae_dev->caps);
276 static int hclgevf_get_port_base_vlan_filter_state(struct hclgevf_dev *hdev)
278 struct hnae3_handle *nic = &hdev->nic;
279 struct hclge_vf_to_pf_msg send_msg;
283 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
284 HCLGE_MBX_GET_PORT_BASE_VLAN_STATE);
285 ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, &resp_msg,
288 dev_err(&hdev->pdev->dev,
289 "VF request to get port based vlan state failed %d",
294 nic->port_base_vlan_state = resp_msg;
299 static int hclgevf_get_queue_info(struct hclgevf_dev *hdev)
301 #define HCLGEVF_TQPS_RSS_INFO_LEN 6
302 #define HCLGEVF_TQPS_ALLOC_OFFSET 0
303 #define HCLGEVF_TQPS_RSS_SIZE_OFFSET 2
304 #define HCLGEVF_TQPS_RX_BUFFER_LEN_OFFSET 4
306 u8 resp_msg[HCLGEVF_TQPS_RSS_INFO_LEN];
307 struct hclge_vf_to_pf_msg send_msg;
310 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_QINFO, 0);
311 status = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
312 HCLGEVF_TQPS_RSS_INFO_LEN);
314 dev_err(&hdev->pdev->dev,
315 "VF request to get tqp info from PF failed %d",
320 memcpy(&hdev->num_tqps, &resp_msg[HCLGEVF_TQPS_ALLOC_OFFSET],
322 memcpy(&hdev->rss_size_max, &resp_msg[HCLGEVF_TQPS_RSS_SIZE_OFFSET],
324 memcpy(&hdev->rx_buf_len, &resp_msg[HCLGEVF_TQPS_RX_BUFFER_LEN_OFFSET],
330 static int hclgevf_get_queue_depth(struct hclgevf_dev *hdev)
332 #define HCLGEVF_TQPS_DEPTH_INFO_LEN 4
333 #define HCLGEVF_TQPS_NUM_TX_DESC_OFFSET 0
334 #define HCLGEVF_TQPS_NUM_RX_DESC_OFFSET 2
336 u8 resp_msg[HCLGEVF_TQPS_DEPTH_INFO_LEN];
337 struct hclge_vf_to_pf_msg send_msg;
340 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_QDEPTH, 0);
341 ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
342 HCLGEVF_TQPS_DEPTH_INFO_LEN);
344 dev_err(&hdev->pdev->dev,
345 "VF request to get tqp depth info from PF failed %d",
350 memcpy(&hdev->num_tx_desc, &resp_msg[HCLGEVF_TQPS_NUM_TX_DESC_OFFSET],
352 memcpy(&hdev->num_rx_desc, &resp_msg[HCLGEVF_TQPS_NUM_RX_DESC_OFFSET],
358 static u16 hclgevf_get_qid_global(struct hnae3_handle *handle, u16 queue_id)
360 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
361 struct hclge_vf_to_pf_msg send_msg;
366 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_QID_IN_PF, 0);
367 memcpy(send_msg.data, &queue_id, sizeof(queue_id));
368 ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_data,
371 qid_in_pf = *(u16 *)resp_data;
376 static int hclgevf_get_pf_media_type(struct hclgevf_dev *hdev)
378 struct hclge_vf_to_pf_msg send_msg;
382 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_MEDIA_TYPE, 0);
383 ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
386 dev_err(&hdev->pdev->dev,
387 "VF request to get the pf port media type failed %d",
392 hdev->hw.mac.media_type = resp_msg[0];
393 hdev->hw.mac.module_type = resp_msg[1];
398 static int hclgevf_alloc_tqps(struct hclgevf_dev *hdev)
400 struct hclgevf_tqp *tqp;
403 hdev->htqp = devm_kcalloc(&hdev->pdev->dev, hdev->num_tqps,
404 sizeof(struct hclgevf_tqp), GFP_KERNEL);
410 for (i = 0; i < hdev->num_tqps; i++) {
411 tqp->dev = &hdev->pdev->dev;
414 tqp->q.ae_algo = &ae_algovf;
415 tqp->q.buf_size = hdev->rx_buf_len;
416 tqp->q.tx_desc_num = hdev->num_tx_desc;
417 tqp->q.rx_desc_num = hdev->num_rx_desc;
419 /* need an extended offset to configure queues >=
420 * HCLGEVF_TQP_MAX_SIZE_DEV_V2.
422 if (i < HCLGEVF_TQP_MAX_SIZE_DEV_V2)
423 tqp->q.io_base = hdev->hw.io_base +
424 HCLGEVF_TQP_REG_OFFSET +
425 i * HCLGEVF_TQP_REG_SIZE;
427 tqp->q.io_base = hdev->hw.io_base +
428 HCLGEVF_TQP_REG_OFFSET +
429 HCLGEVF_TQP_EXT_REG_OFFSET +
430 (i - HCLGEVF_TQP_MAX_SIZE_DEV_V2) *
431 HCLGEVF_TQP_REG_SIZE;
439 static int hclgevf_knic_setup(struct hclgevf_dev *hdev)
441 struct hnae3_handle *nic = &hdev->nic;
442 struct hnae3_knic_private_info *kinfo;
443 u16 new_tqps = hdev->num_tqps;
448 kinfo->num_tx_desc = hdev->num_tx_desc;
449 kinfo->num_rx_desc = hdev->num_rx_desc;
450 kinfo->rx_buf_len = hdev->rx_buf_len;
451 for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++)
452 if (hdev->hw_tc_map & BIT(i))
455 num_tc = num_tc ? num_tc : 1;
456 kinfo->tc_info.num_tc = num_tc;
457 kinfo->rss_size = min_t(u16, hdev->rss_size_max, new_tqps / num_tc);
458 new_tqps = kinfo->rss_size * num_tc;
459 kinfo->num_tqps = min(new_tqps, hdev->num_tqps);
461 kinfo->tqp = devm_kcalloc(&hdev->pdev->dev, kinfo->num_tqps,
462 sizeof(struct hnae3_queue *), GFP_KERNEL);
466 for (i = 0; i < kinfo->num_tqps; i++) {
467 hdev->htqp[i].q.handle = &hdev->nic;
468 hdev->htqp[i].q.tqp_index = i;
469 kinfo->tqp[i] = &hdev->htqp[i].q;
472 /* after init the max rss_size and tqps, adjust the default tqp numbers
473 * and rss size with the actual vector numbers
475 kinfo->num_tqps = min_t(u16, hdev->num_nic_msix - 1, kinfo->num_tqps);
476 kinfo->rss_size = min_t(u16, kinfo->num_tqps / num_tc,
482 static void hclgevf_request_link_info(struct hclgevf_dev *hdev)
484 struct hclge_vf_to_pf_msg send_msg;
487 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_LINK_STATUS, 0);
488 status = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
490 dev_err(&hdev->pdev->dev,
491 "VF failed to fetch link status(%d) from PF", status);
494 void hclgevf_update_link_status(struct hclgevf_dev *hdev, int link_state)
496 struct hnae3_handle *rhandle = &hdev->roce;
497 struct hnae3_handle *handle = &hdev->nic;
498 struct hnae3_client *rclient;
499 struct hnae3_client *client;
501 if (test_and_set_bit(HCLGEVF_STATE_LINK_UPDATING, &hdev->state))
504 client = handle->client;
505 rclient = hdev->roce_client;
508 test_bit(HCLGEVF_STATE_DOWN, &hdev->state) ? 0 : link_state;
509 if (link_state != hdev->hw.mac.link) {
510 hdev->hw.mac.link = link_state;
511 client->ops->link_status_change(handle, !!link_state);
512 if (rclient && rclient->ops->link_status_change)
513 rclient->ops->link_status_change(rhandle, !!link_state);
516 clear_bit(HCLGEVF_STATE_LINK_UPDATING, &hdev->state);
519 static void hclgevf_update_link_mode(struct hclgevf_dev *hdev)
521 #define HCLGEVF_ADVERTISING 0
522 #define HCLGEVF_SUPPORTED 1
524 struct hclge_vf_to_pf_msg send_msg;
526 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_LINK_MODE, 0);
527 send_msg.data[0] = HCLGEVF_ADVERTISING;
528 hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
529 send_msg.data[0] = HCLGEVF_SUPPORTED;
530 hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
533 static int hclgevf_set_handle_info(struct hclgevf_dev *hdev)
535 struct hnae3_handle *nic = &hdev->nic;
538 nic->ae_algo = &ae_algovf;
539 nic->pdev = hdev->pdev;
540 nic->numa_node_mask = hdev->numa_node_mask;
541 nic->flags |= HNAE3_SUPPORT_VF;
542 nic->kinfo.io_base = hdev->hw.io_base;
544 ret = hclgevf_knic_setup(hdev);
546 dev_err(&hdev->pdev->dev, "VF knic setup failed %d\n",
551 static void hclgevf_free_vector(struct hclgevf_dev *hdev, int vector_id)
553 if (hdev->vector_status[vector_id] == HCLGEVF_INVALID_VPORT) {
554 dev_warn(&hdev->pdev->dev,
555 "vector(vector_id %d) has been freed.\n", vector_id);
559 hdev->vector_status[vector_id] = HCLGEVF_INVALID_VPORT;
560 hdev->num_msi_left += 1;
561 hdev->num_msi_used -= 1;
564 static int hclgevf_get_vector(struct hnae3_handle *handle, u16 vector_num,
565 struct hnae3_vector_info *vector_info)
567 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
568 struct hnae3_vector_info *vector = vector_info;
572 vector_num = min_t(u16, hdev->num_nic_msix - 1, vector_num);
573 vector_num = min(hdev->num_msi_left, vector_num);
575 for (j = 0; j < vector_num; j++) {
576 for (i = HCLGEVF_MISC_VECTOR_NUM + 1; i < hdev->num_msi; i++) {
577 if (hdev->vector_status[i] == HCLGEVF_INVALID_VPORT) {
578 vector->vector = pci_irq_vector(hdev->pdev, i);
579 vector->io_addr = hdev->hw.io_base +
580 HCLGEVF_VECTOR_REG_BASE +
581 (i - 1) * HCLGEVF_VECTOR_REG_OFFSET;
582 hdev->vector_status[i] = 0;
583 hdev->vector_irq[i] = vector->vector;
592 hdev->num_msi_left -= alloc;
593 hdev->num_msi_used += alloc;
598 static int hclgevf_get_vector_index(struct hclgevf_dev *hdev, int vector)
602 for (i = 0; i < hdev->num_msi; i++)
603 if (vector == hdev->vector_irq[i])
609 static int hclgevf_set_rss_algo_key(struct hclgevf_dev *hdev,
610 const u8 hfunc, const u8 *key)
612 struct hclgevf_rss_config_cmd *req;
613 unsigned int key_offset = 0;
614 struct hclgevf_desc desc;
619 key_counts = HCLGEVF_RSS_KEY_SIZE;
620 req = (struct hclgevf_rss_config_cmd *)desc.data;
623 hclgevf_cmd_setup_basic_desc(&desc,
624 HCLGEVF_OPC_RSS_GENERIC_CONFIG,
627 req->hash_config |= (hfunc & HCLGEVF_RSS_HASH_ALGO_MASK);
629 (key_offset << HCLGEVF_RSS_HASH_KEY_OFFSET_B);
631 key_size = min(HCLGEVF_RSS_HASH_KEY_NUM, key_counts);
632 memcpy(req->hash_key,
633 key + key_offset * HCLGEVF_RSS_HASH_KEY_NUM, key_size);
635 key_counts -= key_size;
637 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
639 dev_err(&hdev->pdev->dev,
640 "Configure RSS config fail, status = %d\n",
649 static u32 hclgevf_get_rss_key_size(struct hnae3_handle *handle)
651 return HCLGEVF_RSS_KEY_SIZE;
654 static int hclgevf_set_rss_indir_table(struct hclgevf_dev *hdev)
656 const u8 *indir = hdev->rss_cfg.rss_indirection_tbl;
657 struct hclgevf_rss_indirection_table_cmd *req;
658 struct hclgevf_desc desc;
663 req = (struct hclgevf_rss_indirection_table_cmd *)desc.data;
664 rss_cfg_tbl_num = hdev->ae_dev->dev_specs.rss_ind_tbl_size /
665 HCLGEVF_RSS_CFG_TBL_SIZE;
667 for (i = 0; i < rss_cfg_tbl_num; i++) {
668 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INDIR_TABLE,
670 req->start_table_index =
671 cpu_to_le16(i * HCLGEVF_RSS_CFG_TBL_SIZE);
672 req->rss_set_bitmap = cpu_to_le16(HCLGEVF_RSS_SET_BITMAP_MSK);
673 for (j = 0; j < HCLGEVF_RSS_CFG_TBL_SIZE; j++)
675 indir[i * HCLGEVF_RSS_CFG_TBL_SIZE + j];
677 status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
679 dev_err(&hdev->pdev->dev,
680 "VF failed(=%d) to set RSS indirection table\n",
689 static int hclgevf_set_rss_tc_mode(struct hclgevf_dev *hdev, u16 rss_size)
691 struct hclgevf_rss_tc_mode_cmd *req;
692 u16 tc_offset[HCLGEVF_MAX_TC_NUM];
693 u16 tc_valid[HCLGEVF_MAX_TC_NUM];
694 u16 tc_size[HCLGEVF_MAX_TC_NUM];
695 struct hclgevf_desc desc;
700 req = (struct hclgevf_rss_tc_mode_cmd *)desc.data;
702 roundup_size = roundup_pow_of_two(rss_size);
703 roundup_size = ilog2(roundup_size);
705 for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) {
706 tc_valid[i] = !!(hdev->hw_tc_map & BIT(i));
707 tc_size[i] = roundup_size;
708 tc_offset[i] = rss_size * i;
711 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_TC_MODE, false);
712 for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) {
715 hnae3_set_bit(mode, HCLGEVF_RSS_TC_VALID_B,
716 (tc_valid[i] & 0x1));
717 hnae3_set_field(mode, HCLGEVF_RSS_TC_SIZE_M,
718 HCLGEVF_RSS_TC_SIZE_S, tc_size[i]);
719 hnae3_set_bit(mode, HCLGEVF_RSS_TC_SIZE_MSB_B,
720 tc_size[i] >> HCLGEVF_RSS_TC_SIZE_MSB_OFFSET &
722 hnae3_set_field(mode, HCLGEVF_RSS_TC_OFFSET_M,
723 HCLGEVF_RSS_TC_OFFSET_S, tc_offset[i]);
725 req->rss_tc_mode[i] = cpu_to_le16(mode);
727 status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
729 dev_err(&hdev->pdev->dev,
730 "VF failed(=%d) to set rss tc mode\n", status);
735 /* for revision 0x20, vf shared the same rss config with pf */
736 static int hclgevf_get_rss_hash_key(struct hclgevf_dev *hdev)
738 #define HCLGEVF_RSS_MBX_RESP_LEN 8
739 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
740 u8 resp_msg[HCLGEVF_RSS_MBX_RESP_LEN];
741 struct hclge_vf_to_pf_msg send_msg;
742 u16 msg_num, hash_key_index;
746 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_RSS_KEY, 0);
747 msg_num = (HCLGEVF_RSS_KEY_SIZE + HCLGEVF_RSS_MBX_RESP_LEN - 1) /
748 HCLGEVF_RSS_MBX_RESP_LEN;
749 for (index = 0; index < msg_num; index++) {
750 send_msg.data[0] = index;
751 ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
752 HCLGEVF_RSS_MBX_RESP_LEN);
754 dev_err(&hdev->pdev->dev,
755 "VF get rss hash key from PF failed, ret=%d",
760 hash_key_index = HCLGEVF_RSS_MBX_RESP_LEN * index;
761 if (index == msg_num - 1)
762 memcpy(&rss_cfg->rss_hash_key[hash_key_index],
764 HCLGEVF_RSS_KEY_SIZE - hash_key_index);
766 memcpy(&rss_cfg->rss_hash_key[hash_key_index],
767 &resp_msg[0], HCLGEVF_RSS_MBX_RESP_LEN);
773 static int hclgevf_get_rss(struct hnae3_handle *handle, u32 *indir, u8 *key,
776 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
777 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
780 if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
781 /* Get hash algorithm */
783 switch (rss_cfg->hash_algo) {
784 case HCLGEVF_RSS_HASH_ALGO_TOEPLITZ:
785 *hfunc = ETH_RSS_HASH_TOP;
787 case HCLGEVF_RSS_HASH_ALGO_SIMPLE:
788 *hfunc = ETH_RSS_HASH_XOR;
791 *hfunc = ETH_RSS_HASH_UNKNOWN;
796 /* Get the RSS Key required by the user */
798 memcpy(key, rss_cfg->rss_hash_key,
799 HCLGEVF_RSS_KEY_SIZE);
802 *hfunc = ETH_RSS_HASH_TOP;
804 ret = hclgevf_get_rss_hash_key(hdev);
807 memcpy(key, rss_cfg->rss_hash_key,
808 HCLGEVF_RSS_KEY_SIZE);
813 for (i = 0; i < hdev->ae_dev->dev_specs.rss_ind_tbl_size; i++)
814 indir[i] = rss_cfg->rss_indirection_tbl[i];
819 static int hclgevf_parse_rss_hfunc(struct hclgevf_dev *hdev, const u8 hfunc,
823 case ETH_RSS_HASH_TOP:
824 *hash_algo = HCLGEVF_RSS_HASH_ALGO_TOEPLITZ;
826 case ETH_RSS_HASH_XOR:
827 *hash_algo = HCLGEVF_RSS_HASH_ALGO_SIMPLE;
829 case ETH_RSS_HASH_NO_CHANGE:
830 *hash_algo = hdev->rss_cfg.hash_algo;
837 static int hclgevf_set_rss(struct hnae3_handle *handle, const u32 *indir,
838 const u8 *key, const u8 hfunc)
840 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
841 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
845 if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
846 ret = hclgevf_parse_rss_hfunc(hdev, hfunc, &hash_algo);
850 /* Set the RSS Hash Key if specififed by the user */
852 ret = hclgevf_set_rss_algo_key(hdev, hash_algo, key);
854 dev_err(&hdev->pdev->dev,
855 "invalid hfunc type %u\n", hfunc);
859 /* Update the shadow RSS key with user specified qids */
860 memcpy(rss_cfg->rss_hash_key, key,
861 HCLGEVF_RSS_KEY_SIZE);
863 ret = hclgevf_set_rss_algo_key(hdev, hash_algo,
864 rss_cfg->rss_hash_key);
868 rss_cfg->hash_algo = hash_algo;
871 /* update the shadow RSS table with user specified qids */
872 for (i = 0; i < hdev->ae_dev->dev_specs.rss_ind_tbl_size; i++)
873 rss_cfg->rss_indirection_tbl[i] = indir[i];
875 /* update the hardware */
876 return hclgevf_set_rss_indir_table(hdev);
879 static u8 hclgevf_get_rss_hash_bits(struct ethtool_rxnfc *nfc)
881 u8 hash_sets = nfc->data & RXH_L4_B_0_1 ? HCLGEVF_S_PORT_BIT : 0;
883 if (nfc->data & RXH_L4_B_2_3)
884 hash_sets |= HCLGEVF_D_PORT_BIT;
886 hash_sets &= ~HCLGEVF_D_PORT_BIT;
888 if (nfc->data & RXH_IP_SRC)
889 hash_sets |= HCLGEVF_S_IP_BIT;
891 hash_sets &= ~HCLGEVF_S_IP_BIT;
893 if (nfc->data & RXH_IP_DST)
894 hash_sets |= HCLGEVF_D_IP_BIT;
896 hash_sets &= ~HCLGEVF_D_IP_BIT;
898 if (nfc->flow_type == SCTP_V4_FLOW || nfc->flow_type == SCTP_V6_FLOW)
899 hash_sets |= HCLGEVF_V_TAG_BIT;
904 static int hclgevf_init_rss_tuple_cmd(struct hnae3_handle *handle,
905 struct ethtool_rxnfc *nfc,
906 struct hclgevf_rss_input_tuple_cmd *req)
908 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
909 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
912 req->ipv4_tcp_en = rss_cfg->rss_tuple_sets.ipv4_tcp_en;
913 req->ipv4_udp_en = rss_cfg->rss_tuple_sets.ipv4_udp_en;
914 req->ipv4_sctp_en = rss_cfg->rss_tuple_sets.ipv4_sctp_en;
915 req->ipv4_fragment_en = rss_cfg->rss_tuple_sets.ipv4_fragment_en;
916 req->ipv6_tcp_en = rss_cfg->rss_tuple_sets.ipv6_tcp_en;
917 req->ipv6_udp_en = rss_cfg->rss_tuple_sets.ipv6_udp_en;
918 req->ipv6_sctp_en = rss_cfg->rss_tuple_sets.ipv6_sctp_en;
919 req->ipv6_fragment_en = rss_cfg->rss_tuple_sets.ipv6_fragment_en;
921 tuple_sets = hclgevf_get_rss_hash_bits(nfc);
922 switch (nfc->flow_type) {
924 req->ipv4_tcp_en = tuple_sets;
927 req->ipv6_tcp_en = tuple_sets;
930 req->ipv4_udp_en = tuple_sets;
933 req->ipv6_udp_en = tuple_sets;
936 req->ipv4_sctp_en = tuple_sets;
939 if (hdev->ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2 &&
940 (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)))
943 req->ipv6_sctp_en = tuple_sets;
946 req->ipv4_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
949 req->ipv6_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
958 static int hclgevf_set_rss_tuple(struct hnae3_handle *handle,
959 struct ethtool_rxnfc *nfc)
961 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
962 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
963 struct hclgevf_rss_input_tuple_cmd *req;
964 struct hclgevf_desc desc;
967 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
971 ~(RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3))
974 req = (struct hclgevf_rss_input_tuple_cmd *)desc.data;
975 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INPUT_TUPLE, false);
977 ret = hclgevf_init_rss_tuple_cmd(handle, nfc, req);
979 dev_err(&hdev->pdev->dev,
980 "failed to init rss tuple cmd, ret = %d\n", ret);
984 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
986 dev_err(&hdev->pdev->dev,
987 "Set rss tuple fail, status = %d\n", ret);
991 rss_cfg->rss_tuple_sets.ipv4_tcp_en = req->ipv4_tcp_en;
992 rss_cfg->rss_tuple_sets.ipv4_udp_en = req->ipv4_udp_en;
993 rss_cfg->rss_tuple_sets.ipv4_sctp_en = req->ipv4_sctp_en;
994 rss_cfg->rss_tuple_sets.ipv4_fragment_en = req->ipv4_fragment_en;
995 rss_cfg->rss_tuple_sets.ipv6_tcp_en = req->ipv6_tcp_en;
996 rss_cfg->rss_tuple_sets.ipv6_udp_en = req->ipv6_udp_en;
997 rss_cfg->rss_tuple_sets.ipv6_sctp_en = req->ipv6_sctp_en;
998 rss_cfg->rss_tuple_sets.ipv6_fragment_en = req->ipv6_fragment_en;
1002 static int hclgevf_get_rss_tuple_by_flow_type(struct hclgevf_dev *hdev,
1003 int flow_type, u8 *tuple_sets)
1005 switch (flow_type) {
1007 *tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv4_tcp_en;
1010 *tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv4_udp_en;
1013 *tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv6_tcp_en;
1016 *tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv6_udp_en;
1019 *tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv4_sctp_en;
1022 *tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv6_sctp_en;
1026 *tuple_sets = HCLGEVF_S_IP_BIT | HCLGEVF_D_IP_BIT;
1035 static u64 hclgevf_convert_rss_tuple(u8 tuple_sets)
1039 if (tuple_sets & HCLGEVF_D_PORT_BIT)
1040 tuple_data |= RXH_L4_B_2_3;
1041 if (tuple_sets & HCLGEVF_S_PORT_BIT)
1042 tuple_data |= RXH_L4_B_0_1;
1043 if (tuple_sets & HCLGEVF_D_IP_BIT)
1044 tuple_data |= RXH_IP_DST;
1045 if (tuple_sets & HCLGEVF_S_IP_BIT)
1046 tuple_data |= RXH_IP_SRC;
1051 static int hclgevf_get_rss_tuple(struct hnae3_handle *handle,
1052 struct ethtool_rxnfc *nfc)
1054 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1058 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
1063 ret = hclgevf_get_rss_tuple_by_flow_type(hdev, nfc->flow_type,
1065 if (ret || !tuple_sets)
1068 nfc->data = hclgevf_convert_rss_tuple(tuple_sets);
1073 static int hclgevf_set_rss_input_tuple(struct hclgevf_dev *hdev,
1074 struct hclgevf_rss_cfg *rss_cfg)
1076 struct hclgevf_rss_input_tuple_cmd *req;
1077 struct hclgevf_desc desc;
1080 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INPUT_TUPLE, false);
1082 req = (struct hclgevf_rss_input_tuple_cmd *)desc.data;
1084 req->ipv4_tcp_en = rss_cfg->rss_tuple_sets.ipv4_tcp_en;
1085 req->ipv4_udp_en = rss_cfg->rss_tuple_sets.ipv4_udp_en;
1086 req->ipv4_sctp_en = rss_cfg->rss_tuple_sets.ipv4_sctp_en;
1087 req->ipv4_fragment_en = rss_cfg->rss_tuple_sets.ipv4_fragment_en;
1088 req->ipv6_tcp_en = rss_cfg->rss_tuple_sets.ipv6_tcp_en;
1089 req->ipv6_udp_en = rss_cfg->rss_tuple_sets.ipv6_udp_en;
1090 req->ipv6_sctp_en = rss_cfg->rss_tuple_sets.ipv6_sctp_en;
1091 req->ipv6_fragment_en = rss_cfg->rss_tuple_sets.ipv6_fragment_en;
1093 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
1095 dev_err(&hdev->pdev->dev,
1096 "Configure rss input fail, status = %d\n", ret);
1100 static int hclgevf_get_tc_size(struct hnae3_handle *handle)
1102 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1103 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
1105 return rss_cfg->rss_size;
1108 static int hclgevf_bind_ring_to_vector(struct hnae3_handle *handle, bool en,
1110 struct hnae3_ring_chain_node *ring_chain)
1112 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1113 struct hclge_vf_to_pf_msg send_msg;
1114 struct hnae3_ring_chain_node *node;
1118 memset(&send_msg, 0, sizeof(send_msg));
1119 send_msg.code = en ? HCLGE_MBX_MAP_RING_TO_VECTOR :
1120 HCLGE_MBX_UNMAP_RING_TO_VECTOR;
1121 send_msg.vector_id = vector_id;
1123 for (node = ring_chain; node; node = node->next) {
1124 send_msg.param[i].ring_type =
1125 hnae3_get_bit(node->flag, HNAE3_RING_TYPE_B);
1127 send_msg.param[i].tqp_index = node->tqp_index;
1128 send_msg.param[i].int_gl_index =
1129 hnae3_get_field(node->int_gl_idx,
1130 HNAE3_RING_GL_IDX_M,
1131 HNAE3_RING_GL_IDX_S);
1134 if (i == HCLGE_MBX_MAX_RING_CHAIN_PARAM_NUM || !node->next) {
1135 send_msg.ring_num = i;
1137 status = hclgevf_send_mbx_msg(hdev, &send_msg, false,
1140 dev_err(&hdev->pdev->dev,
1141 "Map TQP fail, status is %d.\n",
1152 static int hclgevf_map_ring_to_vector(struct hnae3_handle *handle, int vector,
1153 struct hnae3_ring_chain_node *ring_chain)
1155 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1158 vector_id = hclgevf_get_vector_index(hdev, vector);
1159 if (vector_id < 0) {
1160 dev_err(&handle->pdev->dev,
1161 "Get vector index fail. ret =%d\n", vector_id);
1165 return hclgevf_bind_ring_to_vector(handle, true, vector_id, ring_chain);
1168 static int hclgevf_unmap_ring_from_vector(
1169 struct hnae3_handle *handle,
1171 struct hnae3_ring_chain_node *ring_chain)
1173 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1176 if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state))
1179 vector_id = hclgevf_get_vector_index(hdev, vector);
1180 if (vector_id < 0) {
1181 dev_err(&handle->pdev->dev,
1182 "Get vector index fail. ret =%d\n", vector_id);
1186 ret = hclgevf_bind_ring_to_vector(handle, false, vector_id, ring_chain);
1188 dev_err(&handle->pdev->dev,
1189 "Unmap ring from vector fail. vector=%d, ret =%d\n",
1196 static int hclgevf_put_vector(struct hnae3_handle *handle, int vector)
1198 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1201 vector_id = hclgevf_get_vector_index(hdev, vector);
1202 if (vector_id < 0) {
1203 dev_err(&handle->pdev->dev,
1204 "hclgevf_put_vector get vector index fail. ret =%d\n",
1209 hclgevf_free_vector(hdev, vector_id);
1214 static int hclgevf_cmd_set_promisc_mode(struct hclgevf_dev *hdev,
1215 bool en_uc_pmc, bool en_mc_pmc,
1218 struct hnae3_handle *handle = &hdev->nic;
1219 struct hclge_vf_to_pf_msg send_msg;
1222 memset(&send_msg, 0, sizeof(send_msg));
1223 send_msg.code = HCLGE_MBX_SET_PROMISC_MODE;
1224 send_msg.en_bc = en_bc_pmc ? 1 : 0;
1225 send_msg.en_uc = en_uc_pmc ? 1 : 0;
1226 send_msg.en_mc = en_mc_pmc ? 1 : 0;
1227 send_msg.en_limit_promisc = test_bit(HNAE3_PFLAG_LIMIT_PROMISC,
1228 &handle->priv_flags) ? 1 : 0;
1230 ret = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
1232 dev_err(&hdev->pdev->dev,
1233 "Set promisc mode fail, status is %d.\n", ret);
1238 static int hclgevf_set_promisc_mode(struct hnae3_handle *handle, bool en_uc_pmc,
1241 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1244 en_bc_pmc = hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2;
1246 return hclgevf_cmd_set_promisc_mode(hdev, en_uc_pmc, en_mc_pmc,
1250 static void hclgevf_request_update_promisc_mode(struct hnae3_handle *handle)
1252 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1254 set_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state);
1255 hclgevf_task_schedule(hdev, 0);
1258 static void hclgevf_sync_promisc_mode(struct hclgevf_dev *hdev)
1260 struct hnae3_handle *handle = &hdev->nic;
1261 bool en_uc_pmc = handle->netdev_flags & HNAE3_UPE;
1262 bool en_mc_pmc = handle->netdev_flags & HNAE3_MPE;
1265 if (test_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state)) {
1266 ret = hclgevf_set_promisc_mode(handle, en_uc_pmc, en_mc_pmc);
1268 clear_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state);
1272 static int hclgevf_tqp_enable_cmd_send(struct hclgevf_dev *hdev, u16 tqp_id,
1273 u16 stream_id, bool enable)
1275 struct hclgevf_cfg_com_tqp_queue_cmd *req;
1276 struct hclgevf_desc desc;
1278 req = (struct hclgevf_cfg_com_tqp_queue_cmd *)desc.data;
1280 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_CFG_COM_TQP_QUEUE,
1282 req->tqp_id = cpu_to_le16(tqp_id & HCLGEVF_RING_ID_MASK);
1283 req->stream_id = cpu_to_le16(stream_id);
1285 req->enable |= 1U << HCLGEVF_TQP_ENABLE_B;
1287 return hclgevf_cmd_send(&hdev->hw, &desc, 1);
1290 static int hclgevf_tqp_enable(struct hnae3_handle *handle, bool enable)
1292 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1296 for (i = 0; i < handle->kinfo.num_tqps; i++) {
1297 ret = hclgevf_tqp_enable_cmd_send(hdev, i, 0, enable);
1305 static void hclgevf_reset_tqp_stats(struct hnae3_handle *handle)
1307 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
1308 struct hclgevf_tqp *tqp;
1311 for (i = 0; i < kinfo->num_tqps; i++) {
1312 tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
1313 memset(&tqp->tqp_stats, 0, sizeof(tqp->tqp_stats));
1317 static int hclgevf_get_host_mac_addr(struct hclgevf_dev *hdev, u8 *p)
1319 struct hclge_vf_to_pf_msg send_msg;
1320 u8 host_mac[ETH_ALEN];
1323 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_MAC_ADDR, 0);
1324 status = hclgevf_send_mbx_msg(hdev, &send_msg, true, host_mac,
1327 dev_err(&hdev->pdev->dev,
1328 "fail to get VF MAC from host %d", status);
1332 ether_addr_copy(p, host_mac);
1337 static void hclgevf_get_mac_addr(struct hnae3_handle *handle, u8 *p)
1339 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1340 u8 host_mac_addr[ETH_ALEN];
1342 if (hclgevf_get_host_mac_addr(hdev, host_mac_addr))
1345 hdev->has_pf_mac = !is_zero_ether_addr(host_mac_addr);
1346 if (hdev->has_pf_mac)
1347 ether_addr_copy(p, host_mac_addr);
1349 ether_addr_copy(p, hdev->hw.mac.mac_addr);
1352 static int hclgevf_set_mac_addr(struct hnae3_handle *handle, void *p,
1355 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1356 u8 *old_mac_addr = (u8 *)hdev->hw.mac.mac_addr;
1357 struct hclge_vf_to_pf_msg send_msg;
1358 u8 *new_mac_addr = (u8 *)p;
1361 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_UNICAST, 0);
1362 send_msg.subcode = HCLGE_MBX_MAC_VLAN_UC_MODIFY;
1363 ether_addr_copy(send_msg.data, new_mac_addr);
1364 if (is_first && !hdev->has_pf_mac)
1365 eth_zero_addr(&send_msg.data[ETH_ALEN]);
1367 ether_addr_copy(&send_msg.data[ETH_ALEN], old_mac_addr);
1368 status = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1370 ether_addr_copy(hdev->hw.mac.mac_addr, new_mac_addr);
1375 static struct hclgevf_mac_addr_node *
1376 hclgevf_find_mac_node(struct list_head *list, const u8 *mac_addr)
1378 struct hclgevf_mac_addr_node *mac_node, *tmp;
1380 list_for_each_entry_safe(mac_node, tmp, list, node)
1381 if (ether_addr_equal(mac_addr, mac_node->mac_addr))
1387 static void hclgevf_update_mac_node(struct hclgevf_mac_addr_node *mac_node,
1388 enum HCLGEVF_MAC_NODE_STATE state)
1391 /* from set_rx_mode or tmp_add_list */
1392 case HCLGEVF_MAC_TO_ADD:
1393 if (mac_node->state == HCLGEVF_MAC_TO_DEL)
1394 mac_node->state = HCLGEVF_MAC_ACTIVE;
1396 /* only from set_rx_mode */
1397 case HCLGEVF_MAC_TO_DEL:
1398 if (mac_node->state == HCLGEVF_MAC_TO_ADD) {
1399 list_del(&mac_node->node);
1402 mac_node->state = HCLGEVF_MAC_TO_DEL;
1405 /* only from tmp_add_list, the mac_node->state won't be
1406 * HCLGEVF_MAC_ACTIVE
1408 case HCLGEVF_MAC_ACTIVE:
1409 if (mac_node->state == HCLGEVF_MAC_TO_ADD)
1410 mac_node->state = HCLGEVF_MAC_ACTIVE;
1415 static int hclgevf_update_mac_list(struct hnae3_handle *handle,
1416 enum HCLGEVF_MAC_NODE_STATE state,
1417 enum HCLGEVF_MAC_ADDR_TYPE mac_type,
1418 const unsigned char *addr)
1420 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1421 struct hclgevf_mac_addr_node *mac_node;
1422 struct list_head *list;
1424 list = (mac_type == HCLGEVF_MAC_ADDR_UC) ?
1425 &hdev->mac_table.uc_mac_list : &hdev->mac_table.mc_mac_list;
1427 spin_lock_bh(&hdev->mac_table.mac_list_lock);
1429 /* if the mac addr is already in the mac list, no need to add a new
1430 * one into it, just check the mac addr state, convert it to a new
1431 * new state, or just remove it, or do nothing.
1433 mac_node = hclgevf_find_mac_node(list, addr);
1435 hclgevf_update_mac_node(mac_node, state);
1436 spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1439 /* if this address is never added, unnecessary to delete */
1440 if (state == HCLGEVF_MAC_TO_DEL) {
1441 spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1445 mac_node = kzalloc(sizeof(*mac_node), GFP_ATOMIC);
1447 spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1451 mac_node->state = state;
1452 ether_addr_copy(mac_node->mac_addr, addr);
1453 list_add_tail(&mac_node->node, list);
1455 spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1459 static int hclgevf_add_uc_addr(struct hnae3_handle *handle,
1460 const unsigned char *addr)
1462 return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_ADD,
1463 HCLGEVF_MAC_ADDR_UC, addr);
1466 static int hclgevf_rm_uc_addr(struct hnae3_handle *handle,
1467 const unsigned char *addr)
1469 return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_DEL,
1470 HCLGEVF_MAC_ADDR_UC, addr);
1473 static int hclgevf_add_mc_addr(struct hnae3_handle *handle,
1474 const unsigned char *addr)
1476 return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_ADD,
1477 HCLGEVF_MAC_ADDR_MC, addr);
1480 static int hclgevf_rm_mc_addr(struct hnae3_handle *handle,
1481 const unsigned char *addr)
1483 return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_DEL,
1484 HCLGEVF_MAC_ADDR_MC, addr);
1487 static int hclgevf_add_del_mac_addr(struct hclgevf_dev *hdev,
1488 struct hclgevf_mac_addr_node *mac_node,
1489 enum HCLGEVF_MAC_ADDR_TYPE mac_type)
1491 struct hclge_vf_to_pf_msg send_msg;
1494 if (mac_type == HCLGEVF_MAC_ADDR_UC) {
1495 code = HCLGE_MBX_SET_UNICAST;
1496 if (mac_node->state == HCLGEVF_MAC_TO_ADD)
1497 subcode = HCLGE_MBX_MAC_VLAN_UC_ADD;
1499 subcode = HCLGE_MBX_MAC_VLAN_UC_REMOVE;
1501 code = HCLGE_MBX_SET_MULTICAST;
1502 if (mac_node->state == HCLGEVF_MAC_TO_ADD)
1503 subcode = HCLGE_MBX_MAC_VLAN_MC_ADD;
1505 subcode = HCLGE_MBX_MAC_VLAN_MC_REMOVE;
1508 hclgevf_build_send_msg(&send_msg, code, subcode);
1509 ether_addr_copy(send_msg.data, mac_node->mac_addr);
1510 return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
1513 static void hclgevf_config_mac_list(struct hclgevf_dev *hdev,
1514 struct list_head *list,
1515 enum HCLGEVF_MAC_ADDR_TYPE mac_type)
1517 struct hclgevf_mac_addr_node *mac_node, *tmp;
1520 list_for_each_entry_safe(mac_node, tmp, list, node) {
1521 ret = hclgevf_add_del_mac_addr(hdev, mac_node, mac_type);
1523 dev_err(&hdev->pdev->dev,
1524 "failed to configure mac %pM, state = %d, ret = %d\n",
1525 mac_node->mac_addr, mac_node->state, ret);
1528 if (mac_node->state == HCLGEVF_MAC_TO_ADD) {
1529 mac_node->state = HCLGEVF_MAC_ACTIVE;
1531 list_del(&mac_node->node);
1537 static void hclgevf_sync_from_add_list(struct list_head *add_list,
1538 struct list_head *mac_list)
1540 struct hclgevf_mac_addr_node *mac_node, *tmp, *new_node;
1542 list_for_each_entry_safe(mac_node, tmp, add_list, node) {
1543 /* if the mac address from tmp_add_list is not in the
1544 * uc/mc_mac_list, it means have received a TO_DEL request
1545 * during the time window of sending mac config request to PF
1546 * If mac_node state is ACTIVE, then change its state to TO_DEL,
1547 * then it will be removed at next time. If is TO_ADD, it means
1548 * send TO_ADD request failed, so just remove the mac node.
1550 new_node = hclgevf_find_mac_node(mac_list, mac_node->mac_addr);
1552 hclgevf_update_mac_node(new_node, mac_node->state);
1553 list_del(&mac_node->node);
1555 } else if (mac_node->state == HCLGEVF_MAC_ACTIVE) {
1556 mac_node->state = HCLGEVF_MAC_TO_DEL;
1557 list_move_tail(&mac_node->node, mac_list);
1559 list_del(&mac_node->node);
1565 static void hclgevf_sync_from_del_list(struct list_head *del_list,
1566 struct list_head *mac_list)
1568 struct hclgevf_mac_addr_node *mac_node, *tmp, *new_node;
1570 list_for_each_entry_safe(mac_node, tmp, del_list, node) {
1571 new_node = hclgevf_find_mac_node(mac_list, mac_node->mac_addr);
1573 /* If the mac addr is exist in the mac list, it means
1574 * received a new request TO_ADD during the time window
1575 * of sending mac addr configurrequest to PF, so just
1576 * change the mac state to ACTIVE.
1578 new_node->state = HCLGEVF_MAC_ACTIVE;
1579 list_del(&mac_node->node);
1582 list_move_tail(&mac_node->node, mac_list);
1587 static void hclgevf_clear_list(struct list_head *list)
1589 struct hclgevf_mac_addr_node *mac_node, *tmp;
1591 list_for_each_entry_safe(mac_node, tmp, list, node) {
1592 list_del(&mac_node->node);
1597 static void hclgevf_sync_mac_list(struct hclgevf_dev *hdev,
1598 enum HCLGEVF_MAC_ADDR_TYPE mac_type)
1600 struct hclgevf_mac_addr_node *mac_node, *tmp, *new_node;
1601 struct list_head tmp_add_list, tmp_del_list;
1602 struct list_head *list;
1604 INIT_LIST_HEAD(&tmp_add_list);
1605 INIT_LIST_HEAD(&tmp_del_list);
1607 /* move the mac addr to the tmp_add_list and tmp_del_list, then
1608 * we can add/delete these mac addr outside the spin lock
1610 list = (mac_type == HCLGEVF_MAC_ADDR_UC) ?
1611 &hdev->mac_table.uc_mac_list : &hdev->mac_table.mc_mac_list;
1613 spin_lock_bh(&hdev->mac_table.mac_list_lock);
1615 list_for_each_entry_safe(mac_node, tmp, list, node) {
1616 switch (mac_node->state) {
1617 case HCLGEVF_MAC_TO_DEL:
1618 list_move_tail(&mac_node->node, &tmp_del_list);
1620 case HCLGEVF_MAC_TO_ADD:
1621 new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
1625 ether_addr_copy(new_node->mac_addr, mac_node->mac_addr);
1626 new_node->state = mac_node->state;
1627 list_add_tail(&new_node->node, &tmp_add_list);
1635 spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1637 /* delete first, in order to get max mac table space for adding */
1638 hclgevf_config_mac_list(hdev, &tmp_del_list, mac_type);
1639 hclgevf_config_mac_list(hdev, &tmp_add_list, mac_type);
1641 /* if some mac addresses were added/deleted fail, move back to the
1642 * mac_list, and retry at next time.
1644 spin_lock_bh(&hdev->mac_table.mac_list_lock);
1646 hclgevf_sync_from_del_list(&tmp_del_list, list);
1647 hclgevf_sync_from_add_list(&tmp_add_list, list);
1649 spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1652 static void hclgevf_sync_mac_table(struct hclgevf_dev *hdev)
1654 hclgevf_sync_mac_list(hdev, HCLGEVF_MAC_ADDR_UC);
1655 hclgevf_sync_mac_list(hdev, HCLGEVF_MAC_ADDR_MC);
1658 static void hclgevf_uninit_mac_list(struct hclgevf_dev *hdev)
1660 spin_lock_bh(&hdev->mac_table.mac_list_lock);
1662 hclgevf_clear_list(&hdev->mac_table.uc_mac_list);
1663 hclgevf_clear_list(&hdev->mac_table.mc_mac_list);
1665 spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1668 static int hclgevf_enable_vlan_filter(struct hnae3_handle *handle, bool enable)
1670 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1671 struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
1672 struct hclge_vf_to_pf_msg send_msg;
1674 if (!test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, ae_dev->caps))
1677 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
1678 HCLGE_MBX_ENABLE_VLAN_FILTER);
1679 send_msg.data[0] = enable ? 1 : 0;
1681 return hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1684 static int hclgevf_set_vlan_filter(struct hnae3_handle *handle,
1685 __be16 proto, u16 vlan_id,
1688 #define HCLGEVF_VLAN_MBX_IS_KILL_OFFSET 0
1689 #define HCLGEVF_VLAN_MBX_VLAN_ID_OFFSET 1
1690 #define HCLGEVF_VLAN_MBX_PROTO_OFFSET 3
1692 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1693 struct hclge_vf_to_pf_msg send_msg;
1696 if (vlan_id > HCLGEVF_MAX_VLAN_ID)
1699 if (proto != htons(ETH_P_8021Q))
1700 return -EPROTONOSUPPORT;
1702 /* When device is resetting or reset failed, firmware is unable to
1703 * handle mailbox. Just record the vlan id, and remove it after
1706 if ((test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state) ||
1707 test_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state)) && is_kill) {
1708 set_bit(vlan_id, hdev->vlan_del_fail_bmap);
1712 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
1713 HCLGE_MBX_VLAN_FILTER);
1714 send_msg.data[HCLGEVF_VLAN_MBX_IS_KILL_OFFSET] = is_kill;
1715 memcpy(&send_msg.data[HCLGEVF_VLAN_MBX_VLAN_ID_OFFSET], &vlan_id,
1717 memcpy(&send_msg.data[HCLGEVF_VLAN_MBX_PROTO_OFFSET], &proto,
1719 /* when remove hw vlan filter failed, record the vlan id,
1720 * and try to remove it from hw later, to be consistence
1723 ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1725 set_bit(vlan_id, hdev->vlan_del_fail_bmap);
1730 static void hclgevf_sync_vlan_filter(struct hclgevf_dev *hdev)
1732 #define HCLGEVF_MAX_SYNC_COUNT 60
1733 struct hnae3_handle *handle = &hdev->nic;
1734 int ret, sync_cnt = 0;
1737 vlan_id = find_first_bit(hdev->vlan_del_fail_bmap, VLAN_N_VID);
1738 while (vlan_id != VLAN_N_VID) {
1739 ret = hclgevf_set_vlan_filter(handle, htons(ETH_P_8021Q),
1744 clear_bit(vlan_id, hdev->vlan_del_fail_bmap);
1746 if (sync_cnt >= HCLGEVF_MAX_SYNC_COUNT)
1749 vlan_id = find_first_bit(hdev->vlan_del_fail_bmap, VLAN_N_VID);
1753 static int hclgevf_en_hw_strip_rxvtag(struct hnae3_handle *handle, bool enable)
1755 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1756 struct hclge_vf_to_pf_msg send_msg;
1758 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
1759 HCLGE_MBX_VLAN_RX_OFF_CFG);
1760 send_msg.data[0] = enable ? 1 : 0;
1761 return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
1764 static int hclgevf_reset_tqp(struct hnae3_handle *handle)
1766 #define HCLGEVF_RESET_ALL_QUEUE_DONE 1U
1767 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1768 struct hclge_vf_to_pf_msg send_msg;
1769 u8 return_status = 0;
1773 /* disable vf queue before send queue reset msg to PF */
1774 ret = hclgevf_tqp_enable(handle, false);
1776 dev_err(&hdev->pdev->dev, "failed to disable tqp, ret = %d\n",
1781 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_QUEUE_RESET, 0);
1783 ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, &return_status,
1784 sizeof(return_status));
1785 if (ret || return_status == HCLGEVF_RESET_ALL_QUEUE_DONE)
1788 for (i = 1; i < handle->kinfo.num_tqps; i++) {
1789 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_QUEUE_RESET, 0);
1790 memcpy(send_msg.data, &i, sizeof(i));
1791 ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1799 static int hclgevf_set_mtu(struct hnae3_handle *handle, int new_mtu)
1801 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1802 struct hclge_vf_to_pf_msg send_msg;
1804 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_MTU, 0);
1805 memcpy(send_msg.data, &new_mtu, sizeof(new_mtu));
1806 return hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1809 static int hclgevf_notify_client(struct hclgevf_dev *hdev,
1810 enum hnae3_reset_notify_type type)
1812 struct hnae3_client *client = hdev->nic_client;
1813 struct hnae3_handle *handle = &hdev->nic;
1816 if (!test_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state) ||
1820 if (!client->ops->reset_notify)
1823 ret = client->ops->reset_notify(handle, type);
1825 dev_err(&hdev->pdev->dev, "notify nic client failed %d(%d)\n",
1831 static int hclgevf_notify_roce_client(struct hclgevf_dev *hdev,
1832 enum hnae3_reset_notify_type type)
1834 struct hnae3_client *client = hdev->roce_client;
1835 struct hnae3_handle *handle = &hdev->roce;
1838 if (!test_bit(HCLGEVF_STATE_ROCE_REGISTERED, &hdev->state) || !client)
1841 if (!client->ops->reset_notify)
1844 ret = client->ops->reset_notify(handle, type);
1846 dev_err(&hdev->pdev->dev, "notify roce client failed %d(%d)",
1851 static int hclgevf_reset_wait(struct hclgevf_dev *hdev)
1853 #define HCLGEVF_RESET_WAIT_US 20000
1854 #define HCLGEVF_RESET_WAIT_CNT 2000
1855 #define HCLGEVF_RESET_WAIT_TIMEOUT_US \
1856 (HCLGEVF_RESET_WAIT_US * HCLGEVF_RESET_WAIT_CNT)
1861 if (hdev->reset_type == HNAE3_VF_RESET)
1862 ret = readl_poll_timeout(hdev->hw.io_base +
1863 HCLGEVF_VF_RST_ING, val,
1864 !(val & HCLGEVF_VF_RST_ING_BIT),
1865 HCLGEVF_RESET_WAIT_US,
1866 HCLGEVF_RESET_WAIT_TIMEOUT_US);
1868 ret = readl_poll_timeout(hdev->hw.io_base +
1869 HCLGEVF_RST_ING, val,
1870 !(val & HCLGEVF_RST_ING_BITS),
1871 HCLGEVF_RESET_WAIT_US,
1872 HCLGEVF_RESET_WAIT_TIMEOUT_US);
1874 /* hardware completion status should be available by this time */
1876 dev_err(&hdev->pdev->dev,
1877 "couldn't get reset done status from h/w, timeout!\n");
1881 /* we will wait a bit more to let reset of the stack to complete. This
1882 * might happen in case reset assertion was made by PF. Yes, this also
1883 * means we might end up waiting bit more even for VF reset.
1890 static void hclgevf_reset_handshake(struct hclgevf_dev *hdev, bool enable)
1894 reg_val = hclgevf_read_dev(&hdev->hw, HCLGEVF_NIC_CSQ_DEPTH_REG);
1896 reg_val |= HCLGEVF_NIC_SW_RST_RDY;
1898 reg_val &= ~HCLGEVF_NIC_SW_RST_RDY;
1900 hclgevf_write_dev(&hdev->hw, HCLGEVF_NIC_CSQ_DEPTH_REG,
1904 static int hclgevf_reset_stack(struct hclgevf_dev *hdev)
1908 /* uninitialize the nic client */
1909 ret = hclgevf_notify_client(hdev, HNAE3_UNINIT_CLIENT);
1913 /* re-initialize the hclge device */
1914 ret = hclgevf_reset_hdev(hdev);
1916 dev_err(&hdev->pdev->dev,
1917 "hclge device re-init failed, VF is disabled!\n");
1921 /* bring up the nic client again */
1922 ret = hclgevf_notify_client(hdev, HNAE3_INIT_CLIENT);
1926 /* clear handshake status with IMP */
1927 hclgevf_reset_handshake(hdev, false);
1929 /* bring up the nic to enable TX/RX again */
1930 return hclgevf_notify_client(hdev, HNAE3_UP_CLIENT);
1933 static int hclgevf_reset_prepare_wait(struct hclgevf_dev *hdev)
1935 #define HCLGEVF_RESET_SYNC_TIME 100
1937 if (hdev->reset_type == HNAE3_VF_FUNC_RESET) {
1938 struct hclge_vf_to_pf_msg send_msg;
1941 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_RESET, 0);
1942 ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1944 dev_err(&hdev->pdev->dev,
1945 "failed to assert VF reset, ret = %d\n", ret);
1948 hdev->rst_stats.vf_func_rst_cnt++;
1951 set_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state);
1952 /* inform hardware that preparatory work is done */
1953 msleep(HCLGEVF_RESET_SYNC_TIME);
1954 hclgevf_reset_handshake(hdev, true);
1955 dev_info(&hdev->pdev->dev, "prepare reset(%d) wait done\n",
1961 static void hclgevf_dump_rst_info(struct hclgevf_dev *hdev)
1963 dev_info(&hdev->pdev->dev, "VF function reset count: %u\n",
1964 hdev->rst_stats.vf_func_rst_cnt);
1965 dev_info(&hdev->pdev->dev, "FLR reset count: %u\n",
1966 hdev->rst_stats.flr_rst_cnt);
1967 dev_info(&hdev->pdev->dev, "VF reset count: %u\n",
1968 hdev->rst_stats.vf_rst_cnt);
1969 dev_info(&hdev->pdev->dev, "reset done count: %u\n",
1970 hdev->rst_stats.rst_done_cnt);
1971 dev_info(&hdev->pdev->dev, "HW reset done count: %u\n",
1972 hdev->rst_stats.hw_rst_done_cnt);
1973 dev_info(&hdev->pdev->dev, "reset count: %u\n",
1974 hdev->rst_stats.rst_cnt);
1975 dev_info(&hdev->pdev->dev, "reset fail count: %u\n",
1976 hdev->rst_stats.rst_fail_cnt);
1977 dev_info(&hdev->pdev->dev, "vector0 interrupt enable status: 0x%x\n",
1978 hclgevf_read_dev(&hdev->hw, HCLGEVF_MISC_VECTOR_REG_BASE));
1979 dev_info(&hdev->pdev->dev, "vector0 interrupt status: 0x%x\n",
1980 hclgevf_read_dev(&hdev->hw, HCLGEVF_VECTOR0_CMDQ_STATE_REG));
1981 dev_info(&hdev->pdev->dev, "handshake status: 0x%x\n",
1982 hclgevf_read_dev(&hdev->hw, HCLGEVF_NIC_CSQ_DEPTH_REG));
1983 dev_info(&hdev->pdev->dev, "function reset status: 0x%x\n",
1984 hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING));
1985 dev_info(&hdev->pdev->dev, "hdev state: 0x%lx\n", hdev->state);
1988 static void hclgevf_reset_err_handle(struct hclgevf_dev *hdev)
1990 /* recover handshake status with IMP when reset fail */
1991 hclgevf_reset_handshake(hdev, true);
1992 hdev->rst_stats.rst_fail_cnt++;
1993 dev_err(&hdev->pdev->dev, "failed to reset VF(%u)\n",
1994 hdev->rst_stats.rst_fail_cnt);
1996 if (hdev->rst_stats.rst_fail_cnt < HCLGEVF_RESET_MAX_FAIL_CNT)
1997 set_bit(hdev->reset_type, &hdev->reset_pending);
1999 if (hclgevf_is_reset_pending(hdev)) {
2000 set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
2001 hclgevf_reset_task_schedule(hdev);
2003 set_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state);
2004 hclgevf_dump_rst_info(hdev);
2008 static int hclgevf_reset_prepare(struct hclgevf_dev *hdev)
2012 hdev->rst_stats.rst_cnt++;
2014 /* perform reset of the stack & ae device for a client */
2015 ret = hclgevf_notify_roce_client(hdev, HNAE3_DOWN_CLIENT);
2020 /* bring down the nic to stop any ongoing TX/RX */
2021 ret = hclgevf_notify_client(hdev, HNAE3_DOWN_CLIENT);
2026 return hclgevf_reset_prepare_wait(hdev);
2029 static int hclgevf_reset_rebuild(struct hclgevf_dev *hdev)
2033 hdev->rst_stats.hw_rst_done_cnt++;
2034 ret = hclgevf_notify_roce_client(hdev, HNAE3_UNINIT_CLIENT);
2039 /* now, re-initialize the nic client and ae device */
2040 ret = hclgevf_reset_stack(hdev);
2043 dev_err(&hdev->pdev->dev, "failed to reset VF stack\n");
2047 ret = hclgevf_notify_roce_client(hdev, HNAE3_INIT_CLIENT);
2048 /* ignore RoCE notify error if it fails HCLGEVF_RESET_MAX_FAIL_CNT - 1
2052 hdev->rst_stats.rst_fail_cnt < HCLGEVF_RESET_MAX_FAIL_CNT - 1)
2055 ret = hclgevf_notify_roce_client(hdev, HNAE3_UP_CLIENT);
2059 hdev->last_reset_time = jiffies;
2060 hdev->rst_stats.rst_done_cnt++;
2061 hdev->rst_stats.rst_fail_cnt = 0;
2062 clear_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state);
2067 static void hclgevf_reset(struct hclgevf_dev *hdev)
2069 if (hclgevf_reset_prepare(hdev))
2072 /* check if VF could successfully fetch the hardware reset completion
2073 * status from the hardware
2075 if (hclgevf_reset_wait(hdev)) {
2076 /* can't do much in this situation, will disable VF */
2077 dev_err(&hdev->pdev->dev,
2078 "failed to fetch H/W reset completion status\n");
2082 if (hclgevf_reset_rebuild(hdev))
2088 hclgevf_reset_err_handle(hdev);
2091 static enum hnae3_reset_type hclgevf_get_reset_level(struct hclgevf_dev *hdev,
2092 unsigned long *addr)
2094 enum hnae3_reset_type rst_level = HNAE3_NONE_RESET;
2096 /* return the highest priority reset level amongst all */
2097 if (test_bit(HNAE3_VF_RESET, addr)) {
2098 rst_level = HNAE3_VF_RESET;
2099 clear_bit(HNAE3_VF_RESET, addr);
2100 clear_bit(HNAE3_VF_PF_FUNC_RESET, addr);
2101 clear_bit(HNAE3_VF_FUNC_RESET, addr);
2102 } else if (test_bit(HNAE3_VF_FULL_RESET, addr)) {
2103 rst_level = HNAE3_VF_FULL_RESET;
2104 clear_bit(HNAE3_VF_FULL_RESET, addr);
2105 clear_bit(HNAE3_VF_FUNC_RESET, addr);
2106 } else if (test_bit(HNAE3_VF_PF_FUNC_RESET, addr)) {
2107 rst_level = HNAE3_VF_PF_FUNC_RESET;
2108 clear_bit(HNAE3_VF_PF_FUNC_RESET, addr);
2109 clear_bit(HNAE3_VF_FUNC_RESET, addr);
2110 } else if (test_bit(HNAE3_VF_FUNC_RESET, addr)) {
2111 rst_level = HNAE3_VF_FUNC_RESET;
2112 clear_bit(HNAE3_VF_FUNC_RESET, addr);
2113 } else if (test_bit(HNAE3_FLR_RESET, addr)) {
2114 rst_level = HNAE3_FLR_RESET;
2115 clear_bit(HNAE3_FLR_RESET, addr);
2121 static void hclgevf_reset_event(struct pci_dev *pdev,
2122 struct hnae3_handle *handle)
2124 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
2125 struct hclgevf_dev *hdev = ae_dev->priv;
2127 dev_info(&hdev->pdev->dev, "received reset request from VF enet\n");
2129 if (hdev->default_reset_request)
2131 hclgevf_get_reset_level(hdev,
2132 &hdev->default_reset_request);
2134 hdev->reset_level = HNAE3_VF_FUNC_RESET;
2136 /* reset of this VF requested */
2137 set_bit(HCLGEVF_RESET_REQUESTED, &hdev->reset_state);
2138 hclgevf_reset_task_schedule(hdev);
2140 hdev->last_reset_time = jiffies;
2143 static void hclgevf_set_def_reset_request(struct hnae3_ae_dev *ae_dev,
2144 enum hnae3_reset_type rst_type)
2146 struct hclgevf_dev *hdev = ae_dev->priv;
2148 set_bit(rst_type, &hdev->default_reset_request);
2151 static void hclgevf_enable_vector(struct hclgevf_misc_vector *vector, bool en)
2153 writel(en ? 1 : 0, vector->addr);
2156 static void hclgevf_reset_prepare_general(struct hnae3_ae_dev *ae_dev,
2157 enum hnae3_reset_type rst_type)
2159 #define HCLGEVF_RESET_RETRY_WAIT_MS 500
2160 #define HCLGEVF_RESET_RETRY_CNT 5
2162 struct hclgevf_dev *hdev = ae_dev->priv;
2167 down(&hdev->reset_sem);
2168 set_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2169 hdev->reset_type = rst_type;
2170 ret = hclgevf_reset_prepare(hdev);
2172 dev_err(&hdev->pdev->dev, "fail to prepare to reset, ret=%d\n",
2174 if (hdev->reset_pending ||
2175 retry_cnt++ < HCLGEVF_RESET_RETRY_CNT) {
2176 dev_err(&hdev->pdev->dev,
2177 "reset_pending:0x%lx, retry_cnt:%d\n",
2178 hdev->reset_pending, retry_cnt);
2179 clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2180 up(&hdev->reset_sem);
2181 msleep(HCLGEVF_RESET_RETRY_WAIT_MS);
2186 /* disable misc vector before reset done */
2187 hclgevf_enable_vector(&hdev->misc_vector, false);
2189 if (hdev->reset_type == HNAE3_FLR_RESET)
2190 hdev->rst_stats.flr_rst_cnt++;
2193 static void hclgevf_reset_done(struct hnae3_ae_dev *ae_dev)
2195 struct hclgevf_dev *hdev = ae_dev->priv;
2198 hclgevf_enable_vector(&hdev->misc_vector, true);
2200 ret = hclgevf_reset_rebuild(hdev);
2202 dev_warn(&hdev->pdev->dev, "fail to rebuild, ret=%d\n",
2205 hdev->reset_type = HNAE3_NONE_RESET;
2206 clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2207 up(&hdev->reset_sem);
2210 static u32 hclgevf_get_fw_version(struct hnae3_handle *handle)
2212 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2214 return hdev->fw_version;
2217 static void hclgevf_get_misc_vector(struct hclgevf_dev *hdev)
2219 struct hclgevf_misc_vector *vector = &hdev->misc_vector;
2221 vector->vector_irq = pci_irq_vector(hdev->pdev,
2222 HCLGEVF_MISC_VECTOR_NUM);
2223 vector->addr = hdev->hw.io_base + HCLGEVF_MISC_VECTOR_REG_BASE;
2224 /* vector status always valid for Vector 0 */
2225 hdev->vector_status[HCLGEVF_MISC_VECTOR_NUM] = 0;
2226 hdev->vector_irq[HCLGEVF_MISC_VECTOR_NUM] = vector->vector_irq;
2228 hdev->num_msi_left -= 1;
2229 hdev->num_msi_used += 1;
2232 void hclgevf_reset_task_schedule(struct hclgevf_dev *hdev)
2234 if (!test_bit(HCLGEVF_STATE_REMOVING, &hdev->state) &&
2235 !test_and_set_bit(HCLGEVF_STATE_RST_SERVICE_SCHED,
2237 mod_delayed_work(hclgevf_wq, &hdev->service_task, 0);
2240 void hclgevf_mbx_task_schedule(struct hclgevf_dev *hdev)
2242 if (!test_bit(HCLGEVF_STATE_REMOVING, &hdev->state) &&
2243 !test_and_set_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED,
2245 mod_delayed_work(hclgevf_wq, &hdev->service_task, 0);
2248 static void hclgevf_task_schedule(struct hclgevf_dev *hdev,
2249 unsigned long delay)
2251 if (!test_bit(HCLGEVF_STATE_REMOVING, &hdev->state) &&
2252 !test_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state))
2253 mod_delayed_work(hclgevf_wq, &hdev->service_task, delay);
2256 static void hclgevf_reset_service_task(struct hclgevf_dev *hdev)
2258 #define HCLGEVF_MAX_RESET_ATTEMPTS_CNT 3
2260 if (!test_and_clear_bit(HCLGEVF_STATE_RST_SERVICE_SCHED, &hdev->state))
2263 down(&hdev->reset_sem);
2264 set_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2266 if (test_and_clear_bit(HCLGEVF_RESET_PENDING,
2267 &hdev->reset_state)) {
2268 /* PF has intimated that it is about to reset the hardware.
2269 * We now have to poll & check if hardware has actually
2270 * completed the reset sequence. On hardware reset completion,
2271 * VF needs to reset the client and ae device.
2273 hdev->reset_attempts = 0;
2275 hdev->last_reset_time = jiffies;
2277 hclgevf_get_reset_level(hdev, &hdev->reset_pending);
2278 if (hdev->reset_type != HNAE3_NONE_RESET)
2279 hclgevf_reset(hdev);
2280 } else if (test_and_clear_bit(HCLGEVF_RESET_REQUESTED,
2281 &hdev->reset_state)) {
2282 /* we could be here when either of below happens:
2283 * 1. reset was initiated due to watchdog timeout caused by
2284 * a. IMP was earlier reset and our TX got choked down and
2285 * which resulted in watchdog reacting and inducing VF
2286 * reset. This also means our cmdq would be unreliable.
2287 * b. problem in TX due to other lower layer(example link
2288 * layer not functioning properly etc.)
2289 * 2. VF reset might have been initiated due to some config
2292 * NOTE: Theres no clear way to detect above cases than to react
2293 * to the response of PF for this reset request. PF will ack the
2294 * 1b and 2. cases but we will not get any intimation about 1a
2295 * from PF as cmdq would be in unreliable state i.e. mailbox
2296 * communication between PF and VF would be broken.
2298 * if we are never geting into pending state it means either:
2299 * 1. PF is not receiving our request which could be due to IMP
2302 * We cannot do much for 2. but to check first we can try reset
2303 * our PCIe + stack and see if it alleviates the problem.
2305 if (hdev->reset_attempts > HCLGEVF_MAX_RESET_ATTEMPTS_CNT) {
2306 /* prepare for full reset of stack + pcie interface */
2307 set_bit(HNAE3_VF_FULL_RESET, &hdev->reset_pending);
2309 /* "defer" schedule the reset task again */
2310 set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
2312 hdev->reset_attempts++;
2314 set_bit(hdev->reset_level, &hdev->reset_pending);
2315 set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
2317 hclgevf_reset_task_schedule(hdev);
2320 hdev->reset_type = HNAE3_NONE_RESET;
2321 clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2322 up(&hdev->reset_sem);
2325 static void hclgevf_mailbox_service_task(struct hclgevf_dev *hdev)
2327 if (!test_and_clear_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state))
2330 if (test_and_set_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state))
2333 hclgevf_mbx_async_handler(hdev);
2335 clear_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state);
2338 static void hclgevf_keep_alive(struct hclgevf_dev *hdev)
2340 struct hclge_vf_to_pf_msg send_msg;
2343 if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state))
2346 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_KEEP_ALIVE, 0);
2347 ret = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
2349 dev_err(&hdev->pdev->dev,
2350 "VF sends keep alive cmd failed(=%d)\n", ret);
2353 static void hclgevf_periodic_service_task(struct hclgevf_dev *hdev)
2355 unsigned long delta = round_jiffies_relative(HZ);
2356 struct hnae3_handle *handle = &hdev->nic;
2358 if (test_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state))
2361 if (time_is_after_jiffies(hdev->last_serv_processed + HZ)) {
2362 delta = jiffies - hdev->last_serv_processed;
2364 if (delta < round_jiffies_relative(HZ)) {
2365 delta = round_jiffies_relative(HZ) - delta;
2370 hdev->serv_processed_cnt++;
2371 if (!(hdev->serv_processed_cnt % HCLGEVF_KEEP_ALIVE_TASK_INTERVAL))
2372 hclgevf_keep_alive(hdev);
2374 if (test_bit(HCLGEVF_STATE_DOWN, &hdev->state)) {
2375 hdev->last_serv_processed = jiffies;
2379 if (!(hdev->serv_processed_cnt % HCLGEVF_STATS_TIMER_INTERVAL))
2380 hclgevf_tqps_update_stats(handle);
2382 /* VF does not need to request link status when this bit is set, because
2383 * PF will push its link status to VFs when link status changed.
2385 if (!test_bit(HCLGEVF_STATE_PF_PUSH_LINK_STATUS, &hdev->state))
2386 hclgevf_request_link_info(hdev);
2388 hclgevf_update_link_mode(hdev);
2390 hclgevf_sync_vlan_filter(hdev);
2392 hclgevf_sync_mac_table(hdev);
2394 hclgevf_sync_promisc_mode(hdev);
2396 hdev->last_serv_processed = jiffies;
2399 hclgevf_task_schedule(hdev, delta);
2402 static void hclgevf_service_task(struct work_struct *work)
2404 struct hclgevf_dev *hdev = container_of(work, struct hclgevf_dev,
2407 hclgevf_reset_service_task(hdev);
2408 hclgevf_mailbox_service_task(hdev);
2409 hclgevf_periodic_service_task(hdev);
2411 /* Handle reset and mbx again in case periodical task delays the
2412 * handling by calling hclgevf_task_schedule() in
2413 * hclgevf_periodic_service_task()
2415 hclgevf_reset_service_task(hdev);
2416 hclgevf_mailbox_service_task(hdev);
2419 static void hclgevf_clear_event_cause(struct hclgevf_dev *hdev, u32 regclr)
2421 hclgevf_write_dev(&hdev->hw, HCLGEVF_VECTOR0_CMDQ_SRC_REG, regclr);
2424 static enum hclgevf_evt_cause hclgevf_check_evt_cause(struct hclgevf_dev *hdev,
2427 u32 val, cmdq_stat_reg, rst_ing_reg;
2429 /* fetch the events from their corresponding regs */
2430 cmdq_stat_reg = hclgevf_read_dev(&hdev->hw,
2431 HCLGEVF_VECTOR0_CMDQ_STATE_REG);
2432 if (BIT(HCLGEVF_VECTOR0_RST_INT_B) & cmdq_stat_reg) {
2433 rst_ing_reg = hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING);
2434 dev_info(&hdev->pdev->dev,
2435 "receive reset interrupt 0x%x!\n", rst_ing_reg);
2436 set_bit(HNAE3_VF_RESET, &hdev->reset_pending);
2437 set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
2438 set_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state);
2439 *clearval = ~(1U << HCLGEVF_VECTOR0_RST_INT_B);
2440 hdev->rst_stats.vf_rst_cnt++;
2441 /* set up VF hardware reset status, its PF will clear
2442 * this status when PF has initialized done.
2444 val = hclgevf_read_dev(&hdev->hw, HCLGEVF_VF_RST_ING);
2445 hclgevf_write_dev(&hdev->hw, HCLGEVF_VF_RST_ING,
2446 val | HCLGEVF_VF_RST_ING_BIT);
2447 return HCLGEVF_VECTOR0_EVENT_RST;
2450 /* check for vector0 mailbox(=CMDQ RX) event source */
2451 if (BIT(HCLGEVF_VECTOR0_RX_CMDQ_INT_B) & cmdq_stat_reg) {
2452 /* for revision 0x21, clearing interrupt is writing bit 0
2453 * to the clear register, writing bit 1 means to keep the
2455 * for revision 0x20, the clear register is a read & write
2456 * register, so we should just write 0 to the bit we are
2457 * handling, and keep other bits as cmdq_stat_reg.
2459 if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
2460 *clearval = ~(1U << HCLGEVF_VECTOR0_RX_CMDQ_INT_B);
2462 *clearval = cmdq_stat_reg &
2463 ~BIT(HCLGEVF_VECTOR0_RX_CMDQ_INT_B);
2465 return HCLGEVF_VECTOR0_EVENT_MBX;
2468 /* print other vector0 event source */
2469 dev_info(&hdev->pdev->dev,
2470 "vector 0 interrupt from unknown source, cmdq_src = %#x\n",
2473 return HCLGEVF_VECTOR0_EVENT_OTHER;
2476 static irqreturn_t hclgevf_misc_irq_handle(int irq, void *data)
2478 enum hclgevf_evt_cause event_cause;
2479 struct hclgevf_dev *hdev = data;
2482 hclgevf_enable_vector(&hdev->misc_vector, false);
2483 event_cause = hclgevf_check_evt_cause(hdev, &clearval);
2484 if (event_cause != HCLGEVF_VECTOR0_EVENT_OTHER)
2485 hclgevf_clear_event_cause(hdev, clearval);
2487 switch (event_cause) {
2488 case HCLGEVF_VECTOR0_EVENT_RST:
2489 hclgevf_reset_task_schedule(hdev);
2491 case HCLGEVF_VECTOR0_EVENT_MBX:
2492 hclgevf_mbx_handler(hdev);
2498 if (event_cause != HCLGEVF_VECTOR0_EVENT_OTHER)
2499 hclgevf_enable_vector(&hdev->misc_vector, true);
2504 static int hclgevf_configure(struct hclgevf_dev *hdev)
2508 hdev->gro_en = true;
2510 ret = hclgevf_get_basic_info(hdev);
2514 /* get current port based vlan state from PF */
2515 ret = hclgevf_get_port_base_vlan_filter_state(hdev);
2519 /* get queue configuration from PF */
2520 ret = hclgevf_get_queue_info(hdev);
2524 /* get queue depth info from PF */
2525 ret = hclgevf_get_queue_depth(hdev);
2529 return hclgevf_get_pf_media_type(hdev);
2532 static int hclgevf_alloc_hdev(struct hnae3_ae_dev *ae_dev)
2534 struct pci_dev *pdev = ae_dev->pdev;
2535 struct hclgevf_dev *hdev;
2537 hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
2542 hdev->ae_dev = ae_dev;
2543 ae_dev->priv = hdev;
2548 static int hclgevf_init_roce_base_info(struct hclgevf_dev *hdev)
2550 struct hnae3_handle *roce = &hdev->roce;
2551 struct hnae3_handle *nic = &hdev->nic;
2553 roce->rinfo.num_vectors = hdev->num_roce_msix;
2555 if (hdev->num_msi_left < roce->rinfo.num_vectors ||
2556 hdev->num_msi_left == 0)
2559 roce->rinfo.base_vector = hdev->roce_base_vector;
2561 roce->rinfo.netdev = nic->kinfo.netdev;
2562 roce->rinfo.roce_io_base = hdev->hw.io_base;
2563 roce->rinfo.roce_mem_base = hdev->hw.mem_base;
2565 roce->pdev = nic->pdev;
2566 roce->ae_algo = nic->ae_algo;
2567 roce->numa_node_mask = nic->numa_node_mask;
2572 static int hclgevf_config_gro(struct hclgevf_dev *hdev)
2574 struct hclgevf_cfg_gro_status_cmd *req;
2575 struct hclgevf_desc desc;
2578 if (!hnae3_dev_gro_supported(hdev))
2581 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_GRO_GENERIC_CONFIG,
2583 req = (struct hclgevf_cfg_gro_status_cmd *)desc.data;
2585 req->gro_en = hdev->gro_en ? 1 : 0;
2587 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
2589 dev_err(&hdev->pdev->dev,
2590 "VF GRO hardware config cmd failed, ret = %d.\n", ret);
2595 static int hclgevf_rss_init_cfg(struct hclgevf_dev *hdev)
2597 u16 rss_ind_tbl_size = hdev->ae_dev->dev_specs.rss_ind_tbl_size;
2598 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
2599 struct hclgevf_rss_tuple_cfg *tuple_sets;
2602 rss_cfg->hash_algo = HCLGEVF_RSS_HASH_ALGO_TOEPLITZ;
2603 rss_cfg->rss_size = hdev->nic.kinfo.rss_size;
2604 tuple_sets = &rss_cfg->rss_tuple_sets;
2605 if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
2608 rss_cfg->hash_algo = HCLGEVF_RSS_HASH_ALGO_SIMPLE;
2610 rss_ind_tbl = devm_kcalloc(&hdev->pdev->dev, rss_ind_tbl_size,
2611 sizeof(*rss_ind_tbl), GFP_KERNEL);
2615 rss_cfg->rss_indirection_tbl = rss_ind_tbl;
2616 memcpy(rss_cfg->rss_hash_key, hclgevf_hash_key,
2617 HCLGEVF_RSS_KEY_SIZE);
2619 tuple_sets->ipv4_tcp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2620 tuple_sets->ipv4_udp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2621 tuple_sets->ipv4_sctp_en = HCLGEVF_RSS_INPUT_TUPLE_SCTP;
2622 tuple_sets->ipv4_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2623 tuple_sets->ipv6_tcp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2624 tuple_sets->ipv6_udp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2625 tuple_sets->ipv6_sctp_en =
2626 hdev->ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2 ?
2627 HCLGEVF_RSS_INPUT_TUPLE_SCTP_NO_PORT :
2628 HCLGEVF_RSS_INPUT_TUPLE_SCTP;
2629 tuple_sets->ipv6_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2632 /* Initialize RSS indirect table */
2633 for (i = 0; i < rss_ind_tbl_size; i++)
2634 rss_cfg->rss_indirection_tbl[i] = i % rss_cfg->rss_size;
2639 static int hclgevf_rss_init_hw(struct hclgevf_dev *hdev)
2641 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
2644 if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
2645 ret = hclgevf_set_rss_algo_key(hdev, rss_cfg->hash_algo,
2646 rss_cfg->rss_hash_key);
2650 ret = hclgevf_set_rss_input_tuple(hdev, rss_cfg);
2655 ret = hclgevf_set_rss_indir_table(hdev);
2659 return hclgevf_set_rss_tc_mode(hdev, rss_cfg->rss_size);
2662 static int hclgevf_init_vlan_config(struct hclgevf_dev *hdev)
2664 struct hnae3_handle *nic = &hdev->nic;
2667 ret = hclgevf_en_hw_strip_rxvtag(nic, true);
2669 dev_err(&hdev->pdev->dev,
2670 "failed to enable rx vlan offload, ret = %d\n", ret);
2674 return hclgevf_set_vlan_filter(&hdev->nic, htons(ETH_P_8021Q), 0,
2678 static void hclgevf_flush_link_update(struct hclgevf_dev *hdev)
2680 #define HCLGEVF_FLUSH_LINK_TIMEOUT 100000
2682 unsigned long last = hdev->serv_processed_cnt;
2685 while (test_bit(HCLGEVF_STATE_LINK_UPDATING, &hdev->state) &&
2686 i++ < HCLGEVF_FLUSH_LINK_TIMEOUT &&
2687 last == hdev->serv_processed_cnt)
2691 static void hclgevf_set_timer_task(struct hnae3_handle *handle, bool enable)
2693 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2696 hclgevf_task_schedule(hdev, 0);
2698 set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2700 /* flush memory to make sure DOWN is seen by service task */
2701 smp_mb__before_atomic();
2702 hclgevf_flush_link_update(hdev);
2706 static int hclgevf_ae_start(struct hnae3_handle *handle)
2708 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2710 clear_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2711 clear_bit(HCLGEVF_STATE_PF_PUSH_LINK_STATUS, &hdev->state);
2713 hclgevf_reset_tqp_stats(handle);
2715 hclgevf_request_link_info(hdev);
2717 hclgevf_update_link_mode(hdev);
2722 static void hclgevf_ae_stop(struct hnae3_handle *handle)
2724 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2726 set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2728 if (hdev->reset_type != HNAE3_VF_RESET)
2729 hclgevf_reset_tqp(handle);
2731 hclgevf_reset_tqp_stats(handle);
2732 hclgevf_update_link_status(hdev, 0);
2735 static int hclgevf_set_alive(struct hnae3_handle *handle, bool alive)
2737 #define HCLGEVF_STATE_ALIVE 1
2738 #define HCLGEVF_STATE_NOT_ALIVE 0
2740 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2741 struct hclge_vf_to_pf_msg send_msg;
2743 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_ALIVE, 0);
2744 send_msg.data[0] = alive ? HCLGEVF_STATE_ALIVE :
2745 HCLGEVF_STATE_NOT_ALIVE;
2746 return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
2749 static int hclgevf_client_start(struct hnae3_handle *handle)
2751 return hclgevf_set_alive(handle, true);
2754 static void hclgevf_client_stop(struct hnae3_handle *handle)
2756 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2759 ret = hclgevf_set_alive(handle, false);
2761 dev_warn(&hdev->pdev->dev,
2762 "%s failed %d\n", __func__, ret);
2765 static void hclgevf_state_init(struct hclgevf_dev *hdev)
2767 clear_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state);
2768 clear_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state);
2769 clear_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state);
2771 INIT_DELAYED_WORK(&hdev->service_task, hclgevf_service_task);
2773 mutex_init(&hdev->mbx_resp.mbx_mutex);
2774 sema_init(&hdev->reset_sem, 1);
2776 spin_lock_init(&hdev->mac_table.mac_list_lock);
2777 INIT_LIST_HEAD(&hdev->mac_table.uc_mac_list);
2778 INIT_LIST_HEAD(&hdev->mac_table.mc_mac_list);
2780 /* bring the device down */
2781 set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2784 static void hclgevf_state_uninit(struct hclgevf_dev *hdev)
2786 set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2787 set_bit(HCLGEVF_STATE_REMOVING, &hdev->state);
2789 if (hdev->service_task.work.func)
2790 cancel_delayed_work_sync(&hdev->service_task);
2792 mutex_destroy(&hdev->mbx_resp.mbx_mutex);
2795 static int hclgevf_init_msi(struct hclgevf_dev *hdev)
2797 struct pci_dev *pdev = hdev->pdev;
2801 if (hnae3_dev_roce_supported(hdev))
2802 vectors = pci_alloc_irq_vectors(pdev,
2803 hdev->roce_base_msix_offset + 1,
2807 vectors = pci_alloc_irq_vectors(pdev, HNAE3_MIN_VECTOR_NUM,
2809 PCI_IRQ_MSI | PCI_IRQ_MSIX);
2813 "failed(%d) to allocate MSI/MSI-X vectors\n",
2817 if (vectors < hdev->num_msi)
2818 dev_warn(&hdev->pdev->dev,
2819 "requested %u MSI/MSI-X, but allocated %d MSI/MSI-X\n",
2820 hdev->num_msi, vectors);
2822 hdev->num_msi = vectors;
2823 hdev->num_msi_left = vectors;
2825 hdev->base_msi_vector = pdev->irq;
2826 hdev->roce_base_vector = pdev->irq + hdev->roce_base_msix_offset;
2828 hdev->vector_status = devm_kcalloc(&pdev->dev, hdev->num_msi,
2829 sizeof(u16), GFP_KERNEL);
2830 if (!hdev->vector_status) {
2831 pci_free_irq_vectors(pdev);
2835 for (i = 0; i < hdev->num_msi; i++)
2836 hdev->vector_status[i] = HCLGEVF_INVALID_VPORT;
2838 hdev->vector_irq = devm_kcalloc(&pdev->dev, hdev->num_msi,
2839 sizeof(int), GFP_KERNEL);
2840 if (!hdev->vector_irq) {
2841 devm_kfree(&pdev->dev, hdev->vector_status);
2842 pci_free_irq_vectors(pdev);
2849 static void hclgevf_uninit_msi(struct hclgevf_dev *hdev)
2851 struct pci_dev *pdev = hdev->pdev;
2853 devm_kfree(&pdev->dev, hdev->vector_status);
2854 devm_kfree(&pdev->dev, hdev->vector_irq);
2855 pci_free_irq_vectors(pdev);
2858 static int hclgevf_misc_irq_init(struct hclgevf_dev *hdev)
2862 hclgevf_get_misc_vector(hdev);
2864 snprintf(hdev->misc_vector.name, HNAE3_INT_NAME_LEN, "%s-misc-%s",
2865 HCLGEVF_NAME, pci_name(hdev->pdev));
2866 ret = request_irq(hdev->misc_vector.vector_irq, hclgevf_misc_irq_handle,
2867 0, hdev->misc_vector.name, hdev);
2869 dev_err(&hdev->pdev->dev, "VF failed to request misc irq(%d)\n",
2870 hdev->misc_vector.vector_irq);
2874 hclgevf_clear_event_cause(hdev, 0);
2876 /* enable misc. vector(vector 0) */
2877 hclgevf_enable_vector(&hdev->misc_vector, true);
2882 static void hclgevf_misc_irq_uninit(struct hclgevf_dev *hdev)
2884 /* disable misc vector(vector 0) */
2885 hclgevf_enable_vector(&hdev->misc_vector, false);
2886 synchronize_irq(hdev->misc_vector.vector_irq);
2887 free_irq(hdev->misc_vector.vector_irq, hdev);
2888 hclgevf_free_vector(hdev, 0);
2891 static void hclgevf_info_show(struct hclgevf_dev *hdev)
2893 struct device *dev = &hdev->pdev->dev;
2895 dev_info(dev, "VF info begin:\n");
2897 dev_info(dev, "Task queue pairs numbers: %u\n", hdev->num_tqps);
2898 dev_info(dev, "Desc num per TX queue: %u\n", hdev->num_tx_desc);
2899 dev_info(dev, "Desc num per RX queue: %u\n", hdev->num_rx_desc);
2900 dev_info(dev, "Numbers of vports: %u\n", hdev->num_alloc_vport);
2901 dev_info(dev, "HW tc map: 0x%x\n", hdev->hw_tc_map);
2902 dev_info(dev, "PF media type of this VF: %u\n",
2903 hdev->hw.mac.media_type);
2905 dev_info(dev, "VF info end.\n");
2908 static int hclgevf_init_nic_client_instance(struct hnae3_ae_dev *ae_dev,
2909 struct hnae3_client *client)
2911 struct hclgevf_dev *hdev = ae_dev->priv;
2912 int rst_cnt = hdev->rst_stats.rst_cnt;
2915 ret = client->ops->init_instance(&hdev->nic);
2919 set_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state);
2920 if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state) ||
2921 rst_cnt != hdev->rst_stats.rst_cnt) {
2922 clear_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state);
2924 client->ops->uninit_instance(&hdev->nic, 0);
2928 hnae3_set_client_init_flag(client, ae_dev, 1);
2930 if (netif_msg_drv(&hdev->nic))
2931 hclgevf_info_show(hdev);
2936 static int hclgevf_init_roce_client_instance(struct hnae3_ae_dev *ae_dev,
2937 struct hnae3_client *client)
2939 struct hclgevf_dev *hdev = ae_dev->priv;
2942 if (!hnae3_dev_roce_supported(hdev) || !hdev->roce_client ||
2946 ret = hclgevf_init_roce_base_info(hdev);
2950 ret = client->ops->init_instance(&hdev->roce);
2954 set_bit(HCLGEVF_STATE_ROCE_REGISTERED, &hdev->state);
2955 hnae3_set_client_init_flag(client, ae_dev, 1);
2960 static int hclgevf_init_client_instance(struct hnae3_client *client,
2961 struct hnae3_ae_dev *ae_dev)
2963 struct hclgevf_dev *hdev = ae_dev->priv;
2966 switch (client->type) {
2967 case HNAE3_CLIENT_KNIC:
2968 hdev->nic_client = client;
2969 hdev->nic.client = client;
2971 ret = hclgevf_init_nic_client_instance(ae_dev, client);
2975 ret = hclgevf_init_roce_client_instance(ae_dev,
2981 case HNAE3_CLIENT_ROCE:
2982 if (hnae3_dev_roce_supported(hdev)) {
2983 hdev->roce_client = client;
2984 hdev->roce.client = client;
2987 ret = hclgevf_init_roce_client_instance(ae_dev, client);
2999 hdev->nic_client = NULL;
3000 hdev->nic.client = NULL;
3003 hdev->roce_client = NULL;
3004 hdev->roce.client = NULL;
3008 static void hclgevf_uninit_client_instance(struct hnae3_client *client,
3009 struct hnae3_ae_dev *ae_dev)
3011 struct hclgevf_dev *hdev = ae_dev->priv;
3013 /* un-init roce, if it exists */
3014 if (hdev->roce_client) {
3015 clear_bit(HCLGEVF_STATE_ROCE_REGISTERED, &hdev->state);
3016 hdev->roce_client->ops->uninit_instance(&hdev->roce, 0);
3017 hdev->roce_client = NULL;
3018 hdev->roce.client = NULL;
3021 /* un-init nic/unic, if this was not called by roce client */
3022 if (client->ops->uninit_instance && hdev->nic_client &&
3023 client->type != HNAE3_CLIENT_ROCE) {
3024 clear_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state);
3026 client->ops->uninit_instance(&hdev->nic, 0);
3027 hdev->nic_client = NULL;
3028 hdev->nic.client = NULL;
3032 static int hclgevf_dev_mem_map(struct hclgevf_dev *hdev)
3034 #define HCLGEVF_MEM_BAR 4
3036 struct pci_dev *pdev = hdev->pdev;
3037 struct hclgevf_hw *hw = &hdev->hw;
3039 /* for device does not have device memory, return directly */
3040 if (!(pci_select_bars(pdev, IORESOURCE_MEM) & BIT(HCLGEVF_MEM_BAR)))
3043 hw->mem_base = devm_ioremap_wc(&pdev->dev,
3044 pci_resource_start(pdev,
3046 pci_resource_len(pdev, HCLGEVF_MEM_BAR));
3047 if (!hw->mem_base) {
3048 dev_err(&pdev->dev, "failed to map device memory\n");
3055 static int hclgevf_pci_init(struct hclgevf_dev *hdev)
3057 struct pci_dev *pdev = hdev->pdev;
3058 struct hclgevf_hw *hw;
3061 ret = pci_enable_device(pdev);
3063 dev_err(&pdev->dev, "failed to enable PCI device\n");
3067 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3069 dev_err(&pdev->dev, "can't set consistent PCI DMA, exiting");
3070 goto err_disable_device;
3073 ret = pci_request_regions(pdev, HCLGEVF_DRIVER_NAME);
3075 dev_err(&pdev->dev, "PCI request regions failed %d\n", ret);
3076 goto err_disable_device;
3079 pci_set_master(pdev);
3082 hw->io_base = pci_iomap(pdev, 2, 0);
3084 dev_err(&pdev->dev, "can't map configuration register space\n");
3086 goto err_clr_master;
3089 ret = hclgevf_dev_mem_map(hdev);
3091 goto err_unmap_io_base;
3096 pci_iounmap(pdev, hdev->hw.io_base);
3098 pci_clear_master(pdev);
3099 pci_release_regions(pdev);
3101 pci_disable_device(pdev);
3106 static void hclgevf_pci_uninit(struct hclgevf_dev *hdev)
3108 struct pci_dev *pdev = hdev->pdev;
3110 if (hdev->hw.mem_base)
3111 devm_iounmap(&pdev->dev, hdev->hw.mem_base);
3113 pci_iounmap(pdev, hdev->hw.io_base);
3114 pci_clear_master(pdev);
3115 pci_release_regions(pdev);
3116 pci_disable_device(pdev);
3119 static int hclgevf_query_vf_resource(struct hclgevf_dev *hdev)
3121 struct hclgevf_query_res_cmd *req;
3122 struct hclgevf_desc desc;
3125 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_QUERY_VF_RSRC, true);
3126 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
3128 dev_err(&hdev->pdev->dev,
3129 "query vf resource failed, ret = %d.\n", ret);
3133 req = (struct hclgevf_query_res_cmd *)desc.data;
3135 if (hnae3_dev_roce_supported(hdev)) {
3136 hdev->roce_base_msix_offset =
3137 hnae3_get_field(le16_to_cpu(req->msixcap_localid_ba_rocee),
3138 HCLGEVF_MSIX_OFT_ROCEE_M,
3139 HCLGEVF_MSIX_OFT_ROCEE_S);
3140 hdev->num_roce_msix =
3141 hnae3_get_field(le16_to_cpu(req->vf_intr_vector_number),
3142 HCLGEVF_VEC_NUM_M, HCLGEVF_VEC_NUM_S);
3144 /* nic's msix numbers is always equals to the roce's. */
3145 hdev->num_nic_msix = hdev->num_roce_msix;
3147 /* VF should have NIC vectors and Roce vectors, NIC vectors
3148 * are queued before Roce vectors. The offset is fixed to 64.
3150 hdev->num_msi = hdev->num_roce_msix +
3151 hdev->roce_base_msix_offset;
3154 hnae3_get_field(le16_to_cpu(req->vf_intr_vector_number),
3155 HCLGEVF_VEC_NUM_M, HCLGEVF_VEC_NUM_S);
3157 hdev->num_nic_msix = hdev->num_msi;
3160 if (hdev->num_nic_msix < HNAE3_MIN_VECTOR_NUM) {
3161 dev_err(&hdev->pdev->dev,
3162 "Just %u msi resources, not enough for vf(min:2).\n",
3163 hdev->num_nic_msix);
3170 static void hclgevf_set_default_dev_specs(struct hclgevf_dev *hdev)
3172 #define HCLGEVF_MAX_NON_TSO_BD_NUM 8U
3174 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
3176 ae_dev->dev_specs.max_non_tso_bd_num =
3177 HCLGEVF_MAX_NON_TSO_BD_NUM;
3178 ae_dev->dev_specs.rss_ind_tbl_size = HCLGEVF_RSS_IND_TBL_SIZE;
3179 ae_dev->dev_specs.rss_key_size = HCLGEVF_RSS_KEY_SIZE;
3180 ae_dev->dev_specs.max_int_gl = HCLGEVF_DEF_MAX_INT_GL;
3181 ae_dev->dev_specs.max_frm_size = HCLGEVF_MAC_MAX_FRAME;
3184 static void hclgevf_parse_dev_specs(struct hclgevf_dev *hdev,
3185 struct hclgevf_desc *desc)
3187 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
3188 struct hclgevf_dev_specs_0_cmd *req0;
3189 struct hclgevf_dev_specs_1_cmd *req1;
3191 req0 = (struct hclgevf_dev_specs_0_cmd *)desc[0].data;
3192 req1 = (struct hclgevf_dev_specs_1_cmd *)desc[1].data;
3194 ae_dev->dev_specs.max_non_tso_bd_num = req0->max_non_tso_bd_num;
3195 ae_dev->dev_specs.rss_ind_tbl_size =
3196 le16_to_cpu(req0->rss_ind_tbl_size);
3197 ae_dev->dev_specs.int_ql_max = le16_to_cpu(req0->int_ql_max);
3198 ae_dev->dev_specs.rss_key_size = le16_to_cpu(req0->rss_key_size);
3199 ae_dev->dev_specs.max_int_gl = le16_to_cpu(req1->max_int_gl);
3200 ae_dev->dev_specs.max_frm_size = le16_to_cpu(req1->max_frm_size);
3203 static void hclgevf_check_dev_specs(struct hclgevf_dev *hdev)
3205 struct hnae3_dev_specs *dev_specs = &hdev->ae_dev->dev_specs;
3207 if (!dev_specs->max_non_tso_bd_num)
3208 dev_specs->max_non_tso_bd_num = HCLGEVF_MAX_NON_TSO_BD_NUM;
3209 if (!dev_specs->rss_ind_tbl_size)
3210 dev_specs->rss_ind_tbl_size = HCLGEVF_RSS_IND_TBL_SIZE;
3211 if (!dev_specs->rss_key_size)
3212 dev_specs->rss_key_size = HCLGEVF_RSS_KEY_SIZE;
3213 if (!dev_specs->max_int_gl)
3214 dev_specs->max_int_gl = HCLGEVF_DEF_MAX_INT_GL;
3215 if (!dev_specs->max_frm_size)
3216 dev_specs->max_frm_size = HCLGEVF_MAC_MAX_FRAME;
3219 static int hclgevf_query_dev_specs(struct hclgevf_dev *hdev)
3221 struct hclgevf_desc desc[HCLGEVF_QUERY_DEV_SPECS_BD_NUM];
3225 /* set default specifications as devices lower than version V3 do not
3226 * support querying specifications from firmware.
3228 if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V3) {
3229 hclgevf_set_default_dev_specs(hdev);
3233 for (i = 0; i < HCLGEVF_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
3234 hclgevf_cmd_setup_basic_desc(&desc[i],
3235 HCLGEVF_OPC_QUERY_DEV_SPECS, true);
3236 desc[i].flag |= cpu_to_le16(HCLGEVF_CMD_FLAG_NEXT);
3238 hclgevf_cmd_setup_basic_desc(&desc[i], HCLGEVF_OPC_QUERY_DEV_SPECS,
3241 ret = hclgevf_cmd_send(&hdev->hw, desc, HCLGEVF_QUERY_DEV_SPECS_BD_NUM);
3245 hclgevf_parse_dev_specs(hdev, desc);
3246 hclgevf_check_dev_specs(hdev);
3251 static int hclgevf_pci_reset(struct hclgevf_dev *hdev)
3253 struct pci_dev *pdev = hdev->pdev;
3256 if (hdev->reset_type == HNAE3_VF_FULL_RESET &&
3257 test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) {
3258 hclgevf_misc_irq_uninit(hdev);
3259 hclgevf_uninit_msi(hdev);
3260 clear_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
3263 if (!test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) {
3264 pci_set_master(pdev);
3265 ret = hclgevf_init_msi(hdev);
3268 "failed(%d) to init MSI/MSI-X\n", ret);
3272 ret = hclgevf_misc_irq_init(hdev);
3274 hclgevf_uninit_msi(hdev);
3275 dev_err(&pdev->dev, "failed(%d) to init Misc IRQ(vector0)\n",
3280 set_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
3286 static int hclgevf_clear_vport_list(struct hclgevf_dev *hdev)
3288 struct hclge_vf_to_pf_msg send_msg;
3290 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_HANDLE_VF_TBL,
3291 HCLGE_MBX_VPORT_LIST_CLEAR);
3292 return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
3295 static void hclgevf_init_rxd_adv_layout(struct hclgevf_dev *hdev)
3297 if (hnae3_ae_dev_rxd_adv_layout_supported(hdev->ae_dev))
3298 hclgevf_write_dev(&hdev->hw, HCLGEVF_RXD_ADV_LAYOUT_EN_REG, 1);
3301 static void hclgevf_uninit_rxd_adv_layout(struct hclgevf_dev *hdev)
3303 if (hnae3_ae_dev_rxd_adv_layout_supported(hdev->ae_dev))
3304 hclgevf_write_dev(&hdev->hw, HCLGEVF_RXD_ADV_LAYOUT_EN_REG, 0);
3307 static int hclgevf_reset_hdev(struct hclgevf_dev *hdev)
3309 struct pci_dev *pdev = hdev->pdev;
3312 ret = hclgevf_pci_reset(hdev);
3314 dev_err(&pdev->dev, "pci reset failed %d\n", ret);
3318 ret = hclgevf_cmd_init(hdev);
3320 dev_err(&pdev->dev, "cmd failed %d\n", ret);
3324 ret = hclgevf_rss_init_hw(hdev);
3326 dev_err(&hdev->pdev->dev,
3327 "failed(%d) to initialize RSS\n", ret);
3331 ret = hclgevf_config_gro(hdev);
3335 ret = hclgevf_init_vlan_config(hdev);
3337 dev_err(&hdev->pdev->dev,
3338 "failed(%d) to initialize VLAN config\n", ret);
3342 set_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state);
3344 hclgevf_init_rxd_adv_layout(hdev);
3346 dev_info(&hdev->pdev->dev, "Reset done\n");
3351 static int hclgevf_init_hdev(struct hclgevf_dev *hdev)
3353 struct pci_dev *pdev = hdev->pdev;
3356 ret = hclgevf_pci_init(hdev);
3360 ret = hclgevf_devlink_init(hdev);
3362 goto err_devlink_init;
3364 ret = hclgevf_cmd_queue_init(hdev);
3366 goto err_cmd_queue_init;
3368 ret = hclgevf_cmd_init(hdev);
3372 /* Get vf resource */
3373 ret = hclgevf_query_vf_resource(hdev);
3377 ret = hclgevf_query_dev_specs(hdev);
3380 "failed to query dev specifications, ret = %d\n", ret);
3384 ret = hclgevf_init_msi(hdev);
3386 dev_err(&pdev->dev, "failed(%d) to init MSI/MSI-X\n", ret);
3390 hclgevf_state_init(hdev);
3391 hdev->reset_level = HNAE3_VF_FUNC_RESET;
3392 hdev->reset_type = HNAE3_NONE_RESET;
3394 ret = hclgevf_misc_irq_init(hdev);
3396 goto err_misc_irq_init;
3398 set_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
3400 ret = hclgevf_configure(hdev);
3402 dev_err(&pdev->dev, "failed(%d) to fetch configuration\n", ret);
3406 ret = hclgevf_alloc_tqps(hdev);
3408 dev_err(&pdev->dev, "failed(%d) to allocate TQPs\n", ret);
3412 ret = hclgevf_set_handle_info(hdev);
3416 ret = hclgevf_config_gro(hdev);
3420 /* Initialize RSS for this VF */
3421 ret = hclgevf_rss_init_cfg(hdev);
3423 dev_err(&pdev->dev, "failed to init rss cfg, ret = %d\n", ret);
3427 ret = hclgevf_rss_init_hw(hdev);
3429 dev_err(&hdev->pdev->dev,
3430 "failed(%d) to initialize RSS\n", ret);
3434 /* ensure vf tbl list as empty before init*/
3435 ret = hclgevf_clear_vport_list(hdev);
3438 "failed to clear tbl list configuration, ret = %d.\n",
3443 ret = hclgevf_init_vlan_config(hdev);
3445 dev_err(&hdev->pdev->dev,
3446 "failed(%d) to initialize VLAN config\n", ret);
3450 hclgevf_init_rxd_adv_layout(hdev);
3452 hdev->last_reset_time = jiffies;
3453 dev_info(&hdev->pdev->dev, "finished initializing %s driver\n",
3454 HCLGEVF_DRIVER_NAME);
3456 hclgevf_task_schedule(hdev, round_jiffies_relative(HZ));
3461 hclgevf_misc_irq_uninit(hdev);
3463 hclgevf_state_uninit(hdev);
3464 hclgevf_uninit_msi(hdev);
3466 hclgevf_cmd_uninit(hdev);
3468 hclgevf_devlink_uninit(hdev);
3470 hclgevf_pci_uninit(hdev);
3471 clear_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
3475 static void hclgevf_uninit_hdev(struct hclgevf_dev *hdev)
3477 struct hclge_vf_to_pf_msg send_msg;
3479 hclgevf_state_uninit(hdev);
3480 hclgevf_uninit_rxd_adv_layout(hdev);
3482 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_VF_UNINIT, 0);
3483 hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
3485 if (test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) {
3486 hclgevf_misc_irq_uninit(hdev);
3487 hclgevf_uninit_msi(hdev);
3490 hclgevf_cmd_uninit(hdev);
3491 hclgevf_devlink_uninit(hdev);
3492 hclgevf_pci_uninit(hdev);
3493 hclgevf_uninit_mac_list(hdev);
3496 static int hclgevf_init_ae_dev(struct hnae3_ae_dev *ae_dev)
3498 struct pci_dev *pdev = ae_dev->pdev;
3501 ret = hclgevf_alloc_hdev(ae_dev);
3503 dev_err(&pdev->dev, "hclge device allocation failed\n");
3507 ret = hclgevf_init_hdev(ae_dev->priv);
3509 dev_err(&pdev->dev, "hclge device initialization failed\n");
3516 static void hclgevf_uninit_ae_dev(struct hnae3_ae_dev *ae_dev)
3518 struct hclgevf_dev *hdev = ae_dev->priv;
3520 hclgevf_uninit_hdev(hdev);
3521 ae_dev->priv = NULL;
3524 static u32 hclgevf_get_max_channels(struct hclgevf_dev *hdev)
3526 struct hnae3_handle *nic = &hdev->nic;
3527 struct hnae3_knic_private_info *kinfo = &nic->kinfo;
3529 return min_t(u32, hdev->rss_size_max,
3530 hdev->num_tqps / kinfo->tc_info.num_tc);
3534 * hclgevf_get_channels - Get the current channels enabled and max supported.
3535 * @handle: hardware information for network interface
3536 * @ch: ethtool channels structure
3538 * We don't support separate tx and rx queues as channels. The other count
3539 * represents how many queues are being used for control. max_combined counts
3540 * how many queue pairs we can support. They may not be mapped 1 to 1 with
3541 * q_vectors since we support a lot more queue pairs than q_vectors.
3543 static void hclgevf_get_channels(struct hnae3_handle *handle,
3544 struct ethtool_channels *ch)
3546 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3548 ch->max_combined = hclgevf_get_max_channels(hdev);
3549 ch->other_count = 0;
3551 ch->combined_count = handle->kinfo.rss_size;
3554 static void hclgevf_get_tqps_and_rss_info(struct hnae3_handle *handle,
3555 u16 *alloc_tqps, u16 *max_rss_size)
3557 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3559 *alloc_tqps = hdev->num_tqps;
3560 *max_rss_size = hdev->rss_size_max;
3563 static void hclgevf_update_rss_size(struct hnae3_handle *handle,
3566 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
3567 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3570 kinfo->req_rss_size = new_tqps_num;
3572 max_rss_size = min_t(u16, hdev->rss_size_max,
3573 hdev->num_tqps / kinfo->tc_info.num_tc);
3575 /* Use the user's configuration when it is not larger than
3576 * max_rss_size, otherwise, use the maximum specification value.
3578 if (kinfo->req_rss_size != kinfo->rss_size && kinfo->req_rss_size &&
3579 kinfo->req_rss_size <= max_rss_size)
3580 kinfo->rss_size = kinfo->req_rss_size;
3581 else if (kinfo->rss_size > max_rss_size ||
3582 (!kinfo->req_rss_size && kinfo->rss_size < max_rss_size))
3583 kinfo->rss_size = max_rss_size;
3585 kinfo->num_tqps = kinfo->tc_info.num_tc * kinfo->rss_size;
3588 static int hclgevf_set_channels(struct hnae3_handle *handle, u32 new_tqps_num,
3589 bool rxfh_configured)
3591 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3592 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
3593 u16 cur_rss_size = kinfo->rss_size;
3594 u16 cur_tqps = kinfo->num_tqps;
3599 hclgevf_update_rss_size(handle, new_tqps_num);
3601 ret = hclgevf_set_rss_tc_mode(hdev, kinfo->rss_size);
3605 /* RSS indirection table has been configured by user */
3606 if (rxfh_configured)
3609 /* Reinitializes the rss indirect table according to the new RSS size */
3610 rss_indir = kcalloc(hdev->ae_dev->dev_specs.rss_ind_tbl_size,
3611 sizeof(u32), GFP_KERNEL);
3615 for (i = 0; i < hdev->ae_dev->dev_specs.rss_ind_tbl_size; i++)
3616 rss_indir[i] = i % kinfo->rss_size;
3618 hdev->rss_cfg.rss_size = kinfo->rss_size;
3620 ret = hclgevf_set_rss(handle, rss_indir, NULL, 0);
3622 dev_err(&hdev->pdev->dev, "set rss indir table fail, ret=%d\n",
3629 dev_info(&hdev->pdev->dev,
3630 "Channels changed, rss_size from %u to %u, tqps from %u to %u",
3631 cur_rss_size, kinfo->rss_size,
3632 cur_tqps, kinfo->rss_size * kinfo->tc_info.num_tc);
3637 static int hclgevf_get_status(struct hnae3_handle *handle)
3639 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3641 return hdev->hw.mac.link;
3644 static void hclgevf_get_ksettings_an_result(struct hnae3_handle *handle,
3645 u8 *auto_neg, u32 *speed,
3648 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3651 *speed = hdev->hw.mac.speed;
3653 *duplex = hdev->hw.mac.duplex;
3655 *auto_neg = AUTONEG_DISABLE;
3658 void hclgevf_update_speed_duplex(struct hclgevf_dev *hdev, u32 speed,
3661 hdev->hw.mac.speed = speed;
3662 hdev->hw.mac.duplex = duplex;
3665 static int hclgevf_gro_en(struct hnae3_handle *handle, bool enable)
3667 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3668 bool gro_en_old = hdev->gro_en;
3671 hdev->gro_en = enable;
3672 ret = hclgevf_config_gro(hdev);
3674 hdev->gro_en = gro_en_old;
3679 static void hclgevf_get_media_type(struct hnae3_handle *handle, u8 *media_type,
3682 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3685 *media_type = hdev->hw.mac.media_type;
3688 *module_type = hdev->hw.mac.module_type;
3691 static bool hclgevf_get_hw_reset_stat(struct hnae3_handle *handle)
3693 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3695 return !!hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING);
3698 static bool hclgevf_get_cmdq_stat(struct hnae3_handle *handle)
3700 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3702 return test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state);
3705 static bool hclgevf_ae_dev_resetting(struct hnae3_handle *handle)
3707 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3709 return test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
3712 static unsigned long hclgevf_ae_dev_reset_cnt(struct hnae3_handle *handle)
3714 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3716 return hdev->rst_stats.hw_rst_done_cnt;
3719 static void hclgevf_get_link_mode(struct hnae3_handle *handle,
3720 unsigned long *supported,
3721 unsigned long *advertising)
3723 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3725 *supported = hdev->hw.mac.supported;
3726 *advertising = hdev->hw.mac.advertising;
3729 #define MAX_SEPARATE_NUM 4
3730 #define SEPARATOR_VALUE 0xFDFCFBFA
3731 #define REG_NUM_PER_LINE 4
3732 #define REG_LEN_PER_LINE (REG_NUM_PER_LINE * sizeof(u32))
3734 static int hclgevf_get_regs_len(struct hnae3_handle *handle)
3736 int cmdq_lines, common_lines, ring_lines, tqp_intr_lines;
3737 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3739 cmdq_lines = sizeof(cmdq_reg_addr_list) / REG_LEN_PER_LINE + 1;
3740 common_lines = sizeof(common_reg_addr_list) / REG_LEN_PER_LINE + 1;
3741 ring_lines = sizeof(ring_reg_addr_list) / REG_LEN_PER_LINE + 1;
3742 tqp_intr_lines = sizeof(tqp_intr_reg_addr_list) / REG_LEN_PER_LINE + 1;
3744 return (cmdq_lines + common_lines + ring_lines * hdev->num_tqps +
3745 tqp_intr_lines * (hdev->num_msi_used - 1)) * REG_LEN_PER_LINE;
3748 static void hclgevf_get_regs(struct hnae3_handle *handle, u32 *version,
3751 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3752 int i, j, reg_um, separator_num;
3755 *version = hdev->fw_version;
3757 /* fetching per-VF registers values from VF PCIe register space */
3758 reg_um = sizeof(cmdq_reg_addr_list) / sizeof(u32);
3759 separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
3760 for (i = 0; i < reg_um; i++)
3761 *reg++ = hclgevf_read_dev(&hdev->hw, cmdq_reg_addr_list[i]);
3762 for (i = 0; i < separator_num; i++)
3763 *reg++ = SEPARATOR_VALUE;
3765 reg_um = sizeof(common_reg_addr_list) / sizeof(u32);
3766 separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
3767 for (i = 0; i < reg_um; i++)
3768 *reg++ = hclgevf_read_dev(&hdev->hw, common_reg_addr_list[i]);
3769 for (i = 0; i < separator_num; i++)
3770 *reg++ = SEPARATOR_VALUE;
3772 reg_um = sizeof(ring_reg_addr_list) / sizeof(u32);
3773 separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
3774 for (j = 0; j < hdev->num_tqps; j++) {
3775 for (i = 0; i < reg_um; i++)
3776 *reg++ = hclgevf_read_dev(&hdev->hw,
3777 ring_reg_addr_list[i] +
3779 for (i = 0; i < separator_num; i++)
3780 *reg++ = SEPARATOR_VALUE;
3783 reg_um = sizeof(tqp_intr_reg_addr_list) / sizeof(u32);
3784 separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
3785 for (j = 0; j < hdev->num_msi_used - 1; j++) {
3786 for (i = 0; i < reg_um; i++)
3787 *reg++ = hclgevf_read_dev(&hdev->hw,
3788 tqp_intr_reg_addr_list[i] +
3790 for (i = 0; i < separator_num; i++)
3791 *reg++ = SEPARATOR_VALUE;
3795 void hclgevf_update_port_base_vlan_info(struct hclgevf_dev *hdev, u16 state,
3796 u8 *port_base_vlan_info, u8 data_size)
3798 struct hnae3_handle *nic = &hdev->nic;
3799 struct hclge_vf_to_pf_msg send_msg;
3804 if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state) ||
3805 test_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state)) {
3806 dev_warn(&hdev->pdev->dev,
3807 "is resetting when updating port based vlan info\n");
3812 ret = hclgevf_notify_client(hdev, HNAE3_DOWN_CLIENT);
3818 /* send msg to PF and wait update port based vlan info */
3819 hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
3820 HCLGE_MBX_PORT_BASE_VLAN_CFG);
3821 memcpy(send_msg.data, port_base_vlan_info, data_size);
3822 ret = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
3824 if (state == HNAE3_PORT_BASE_VLAN_DISABLE)
3825 nic->port_base_vlan_state = state;
3827 nic->port_base_vlan_state = HNAE3_PORT_BASE_VLAN_ENABLE;
3830 hclgevf_notify_client(hdev, HNAE3_UP_CLIENT);
3834 static const struct hnae3_ae_ops hclgevf_ops = {
3835 .init_ae_dev = hclgevf_init_ae_dev,
3836 .uninit_ae_dev = hclgevf_uninit_ae_dev,
3837 .reset_prepare = hclgevf_reset_prepare_general,
3838 .reset_done = hclgevf_reset_done,
3839 .init_client_instance = hclgevf_init_client_instance,
3840 .uninit_client_instance = hclgevf_uninit_client_instance,
3841 .start = hclgevf_ae_start,
3842 .stop = hclgevf_ae_stop,
3843 .client_start = hclgevf_client_start,
3844 .client_stop = hclgevf_client_stop,
3845 .map_ring_to_vector = hclgevf_map_ring_to_vector,
3846 .unmap_ring_from_vector = hclgevf_unmap_ring_from_vector,
3847 .get_vector = hclgevf_get_vector,
3848 .put_vector = hclgevf_put_vector,
3849 .reset_queue = hclgevf_reset_tqp,
3850 .get_mac_addr = hclgevf_get_mac_addr,
3851 .set_mac_addr = hclgevf_set_mac_addr,
3852 .add_uc_addr = hclgevf_add_uc_addr,
3853 .rm_uc_addr = hclgevf_rm_uc_addr,
3854 .add_mc_addr = hclgevf_add_mc_addr,
3855 .rm_mc_addr = hclgevf_rm_mc_addr,
3856 .get_stats = hclgevf_get_stats,
3857 .update_stats = hclgevf_update_stats,
3858 .get_strings = hclgevf_get_strings,
3859 .get_sset_count = hclgevf_get_sset_count,
3860 .get_rss_key_size = hclgevf_get_rss_key_size,
3861 .get_rss = hclgevf_get_rss,
3862 .set_rss = hclgevf_set_rss,
3863 .get_rss_tuple = hclgevf_get_rss_tuple,
3864 .set_rss_tuple = hclgevf_set_rss_tuple,
3865 .get_tc_size = hclgevf_get_tc_size,
3866 .get_fw_version = hclgevf_get_fw_version,
3867 .set_vlan_filter = hclgevf_set_vlan_filter,
3868 .enable_vlan_filter = hclgevf_enable_vlan_filter,
3869 .enable_hw_strip_rxvtag = hclgevf_en_hw_strip_rxvtag,
3870 .reset_event = hclgevf_reset_event,
3871 .set_default_reset_request = hclgevf_set_def_reset_request,
3872 .set_channels = hclgevf_set_channels,
3873 .get_channels = hclgevf_get_channels,
3874 .get_tqps_and_rss_info = hclgevf_get_tqps_and_rss_info,
3875 .get_regs_len = hclgevf_get_regs_len,
3876 .get_regs = hclgevf_get_regs,
3877 .get_status = hclgevf_get_status,
3878 .get_ksettings_an_result = hclgevf_get_ksettings_an_result,
3879 .get_media_type = hclgevf_get_media_type,
3880 .get_hw_reset_stat = hclgevf_get_hw_reset_stat,
3881 .ae_dev_resetting = hclgevf_ae_dev_resetting,
3882 .ae_dev_reset_cnt = hclgevf_ae_dev_reset_cnt,
3883 .set_gro_en = hclgevf_gro_en,
3884 .set_mtu = hclgevf_set_mtu,
3885 .get_global_queue_id = hclgevf_get_qid_global,
3886 .set_timer_task = hclgevf_set_timer_task,
3887 .get_link_mode = hclgevf_get_link_mode,
3888 .set_promisc_mode = hclgevf_set_promisc_mode,
3889 .request_update_promisc_mode = hclgevf_request_update_promisc_mode,
3890 .get_cmdq_stat = hclgevf_get_cmdq_stat,
3893 static struct hnae3_ae_algo ae_algovf = {
3894 .ops = &hclgevf_ops,
3895 .pdev_id_table = ae_algovf_pci_tbl,
3898 static int hclgevf_init(void)
3900 pr_info("%s is initializing\n", HCLGEVF_NAME);
3902 hclgevf_wq = alloc_workqueue("%s", 0, 0, HCLGEVF_NAME);
3904 pr_err("%s: failed to create workqueue\n", HCLGEVF_NAME);
3908 hnae3_register_ae_algo(&ae_algovf);
3913 static void hclgevf_exit(void)
3915 hnae3_unregister_ae_algo(&ae_algovf);
3916 destroy_workqueue(hclgevf_wq);
3918 module_init(hclgevf_init);
3919 module_exit(hclgevf_exit);
3921 MODULE_LICENSE("GPL");
3922 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
3923 MODULE_DESCRIPTION("HCLGEVF Driver");
3924 MODULE_VERSION(HCLGEVF_MOD_VERSION);