1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Huawei HiNIC PCI Express Linux driver
3 * Copyright(c) 2017 Huawei Technologies Co., Ltd
7 #include <linux/if_vlan.h>
8 #include <linux/interrupt.h>
9 #include <linux/etherdevice.h>
10 #include <linux/netdevice.h>
11 #include <linux/module.h>
13 #include "hinic_hw_dev.h"
14 #include "hinic_dev.h"
15 #include "hinic_hw_mbox.h"
16 #include "hinic_hw_cmdq.h"
17 #include "hinic_port.h"
18 #include "hinic_sriov.h"
20 static unsigned char set_vf_link_state;
21 module_param(set_vf_link_state, byte, 0444);
22 MODULE_PARM_DESC(set_vf_link_state, "Set vf link state, 0 represents link auto, 1 represents link always up, 2 represents link always down. - default is 0.");
24 #define HINIC_VLAN_PRIORITY_SHIFT 13
25 #define HINIC_ADD_VLAN_IN_MAC 0x8000
26 #define HINIC_TX_RATE_TABLE_FULL 12
28 static int hinic_set_mac(struct hinic_hwdev *hwdev, const u8 *mac_addr,
29 u16 vlan_id, u16 func_id)
31 struct hinic_port_mac_cmd mac_info = {0};
32 u16 out_size = sizeof(mac_info);
35 mac_info.func_idx = func_id;
36 mac_info.vlan_id = vlan_id;
37 memcpy(mac_info.mac, mac_addr, ETH_ALEN);
39 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_MAC, &mac_info,
40 sizeof(mac_info), &mac_info, &out_size);
41 if (err || out_size != sizeof(mac_info) ||
42 (mac_info.status && mac_info.status != HINIC_MGMT_STATUS_EXIST)) {
43 dev_err(&hwdev->func_to_io.hwif->pdev->dev, "Failed to set MAC, err: %d, status: 0x%x, out size: 0x%x\n",
44 err, mac_info.status, out_size);
51 static void hinic_notify_vf_link_status(struct hinic_hwdev *hwdev, u16 vf_id,
54 struct vf_data_storage *vf_infos = hwdev->func_to_io.vf_infos;
55 struct hinic_port_link_status link = {0};
56 u16 out_size = sizeof(link);
59 if (vf_infos[HW_VF_ID_TO_OS(vf_id)].registered) {
60 link.link = link_status;
61 link.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
62 err = hinic_mbox_to_vf(hwdev, HINIC_MOD_L2NIC,
63 vf_id, HINIC_PORT_CMD_LINK_STATUS_REPORT,
66 if (err || !out_size || link.status)
67 dev_err(&hwdev->hwif->pdev->dev,
68 "Send link change event to VF %d failed, err: %d, status: 0x%x, out_size: 0x%x\n",
69 HW_VF_ID_TO_OS(vf_id), err,
70 link.status, out_size);
74 /* send link change event mbox msg to active vfs under the pf */
75 void hinic_notify_all_vfs_link_changed(struct hinic_hwdev *hwdev,
78 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
81 nic_io->link_status = link_status;
82 for (i = 1; i <= nic_io->max_vfs; i++) {
83 if (!nic_io->vf_infos[HW_VF_ID_TO_OS(i)].link_forced)
84 hinic_notify_vf_link_status(hwdev, i, link_status);
88 static u16 hinic_vf_info_vlanprio(struct hinic_hwdev *hwdev, int vf_id)
90 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
91 u16 pf_vlan, vlanprio;
94 pf_vlan = nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan;
95 pf_qos = nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos;
96 vlanprio = pf_vlan | pf_qos << HINIC_VLAN_PRIORITY_SHIFT;
101 static int hinic_set_vf_vlan(struct hinic_hwdev *hwdev, bool add, u16 vid,
104 struct hinic_vf_vlan_config vf_vlan = {0};
105 u16 out_size = sizeof(vf_vlan);
109 /* VLAN 0 is a special case, don't allow it to be removed */
113 vf_vlan.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
114 vf_vlan.vlan_id = vid;
118 cmd = HINIC_PORT_CMD_SET_VF_VLAN;
120 cmd = HINIC_PORT_CMD_CLR_VF_VLAN;
122 err = hinic_port_msg_cmd(hwdev, cmd, &vf_vlan,
123 sizeof(vf_vlan), &vf_vlan, &out_size);
124 if (err || !out_size || vf_vlan.status) {
125 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF %d vlan, err: %d, status: 0x%x, out size: 0x%x\n",
126 HW_VF_ID_TO_OS(vf_id), err, vf_vlan.status, out_size);
133 static int hinic_set_vf_tx_rate_max_min(struct hinic_hwdev *hwdev, u16 vf_id,
134 u32 max_rate, u32 min_rate)
136 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
137 struct hinic_tx_rate_cfg_max_min rate_cfg = {0};
138 u16 out_size = sizeof(rate_cfg);
141 rate_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
142 rate_cfg.max_rate = max_rate;
143 rate_cfg.min_rate = min_rate;
144 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_VF_MAX_MIN_RATE,
145 &rate_cfg, sizeof(rate_cfg), &rate_cfg,
147 if ((rate_cfg.status != HINIC_MGMT_CMD_UNSUPPORTED &&
148 rate_cfg.status) || err || !out_size) {
149 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) max rate(%d), min rate(%d), err: %d, status: 0x%x, out size: 0x%x\n",
150 HW_VF_ID_TO_OS(vf_id), max_rate, min_rate, err,
151 rate_cfg.status, out_size);
155 if (!rate_cfg.status) {
156 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].max_rate = max_rate;
157 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].min_rate = min_rate;
160 return rate_cfg.status;
163 static int hinic_set_vf_rate_limit(struct hinic_hwdev *hwdev, u16 vf_id,
166 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
167 struct hinic_tx_rate_cfg rate_cfg = {0};
168 u16 out_size = sizeof(rate_cfg);
171 rate_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
172 rate_cfg.tx_rate = tx_rate;
173 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_VF_RATE,
174 &rate_cfg, sizeof(rate_cfg), &rate_cfg,
176 if (err || !out_size || rate_cfg.status) {
177 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) rate(%d), err: %d, status: 0x%x, out size: 0x%x\n",
178 HW_VF_ID_TO_OS(vf_id), tx_rate, err, rate_cfg.status,
181 return rate_cfg.status;
186 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].max_rate = tx_rate;
187 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].min_rate = 0;
192 static int hinic_set_vf_tx_rate(struct hinic_hwdev *hwdev, u16 vf_id,
193 u32 max_rate, u32 min_rate)
197 err = hinic_set_vf_tx_rate_max_min(hwdev, vf_id, max_rate, min_rate);
198 if (err != HINIC_MGMT_CMD_UNSUPPORTED)
202 dev_err(&hwdev->hwif->pdev->dev, "Current firmware doesn't support to set min tx rate\n");
206 dev_info(&hwdev->hwif->pdev->dev, "Current firmware doesn't support to set min tx rate, force min_tx_rate = max_tx_rate\n");
208 return hinic_set_vf_rate_limit(hwdev, vf_id, max_rate);
211 static int hinic_init_vf_config(struct hinic_hwdev *hwdev, u16 vf_id)
213 struct vf_data_storage *vf_info;
214 u16 func_id, vlan_id;
217 vf_info = hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
218 if (vf_info->pf_set_mac) {
219 func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
223 err = hinic_set_mac(hwdev, vf_info->vf_mac_addr, vlan_id,
226 dev_err(&hwdev->func_to_io.hwif->pdev->dev, "Failed to set VF %d MAC\n",
227 HW_VF_ID_TO_OS(vf_id));
232 if (hinic_vf_info_vlanprio(hwdev, vf_id)) {
233 err = hinic_set_vf_vlan(hwdev, true, vf_info->pf_vlan,
234 vf_info->pf_qos, vf_id);
236 dev_err(&hwdev->hwif->pdev->dev, "Failed to add VF %d VLAN_QOS\n",
237 HW_VF_ID_TO_OS(vf_id));
242 if (vf_info->max_rate) {
243 err = hinic_set_vf_tx_rate(hwdev, vf_id, vf_info->max_rate,
246 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF %d max rate: %d, min rate: %d\n",
247 HW_VF_ID_TO_OS(vf_id), vf_info->max_rate,
256 static int hinic_register_vf_msg_handler(void *hwdev, u16 vf_id,
257 void *buf_in, u16 in_size,
258 void *buf_out, u16 *out_size)
260 struct hinic_register_vf *register_info = buf_out;
261 struct hinic_hwdev *hw_dev = hwdev;
262 struct hinic_func_to_io *nic_io;
265 nic_io = &hw_dev->func_to_io;
266 if (vf_id > nic_io->max_vfs) {
267 dev_err(&hw_dev->hwif->pdev->dev, "Register VF id %d exceed limit[0-%d]\n",
268 HW_VF_ID_TO_OS(vf_id), HW_VF_ID_TO_OS(nic_io->max_vfs));
269 register_info->status = EFAULT;
273 *out_size = sizeof(*register_info);
274 err = hinic_init_vf_config(hw_dev, vf_id);
276 register_info->status = EFAULT;
280 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].registered = true;
285 static int hinic_unregister_vf_msg_handler(void *hwdev, u16 vf_id,
286 void *buf_in, u16 in_size,
287 void *buf_out, u16 *out_size)
289 struct hinic_hwdev *hw_dev = hwdev;
290 struct hinic_func_to_io *nic_io;
292 nic_io = &hw_dev->func_to_io;
294 if (vf_id > nic_io->max_vfs)
297 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].registered = false;
302 static int hinic_change_vf_mtu_msg_handler(void *hwdev, u16 vf_id,
303 void *buf_in, u16 in_size,
304 void *buf_out, u16 *out_size)
306 struct hinic_hwdev *hw_dev = hwdev;
309 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_CHANGE_MTU, buf_in,
310 in_size, buf_out, out_size);
312 dev_err(&hw_dev->hwif->pdev->dev, "Failed to set VF %u mtu\n",
320 static int hinic_get_vf_mac_msg_handler(void *hwdev, u16 vf_id,
321 void *buf_in, u16 in_size,
322 void *buf_out, u16 *out_size)
324 struct hinic_port_mac_cmd *mac_info = buf_out;
325 struct hinic_hwdev *dev = hwdev;
326 struct hinic_func_to_io *nic_io;
327 struct vf_data_storage *vf_info;
329 nic_io = &dev->func_to_io;
330 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id);
332 memcpy(mac_info->mac, vf_info->vf_mac_addr, ETH_ALEN);
333 mac_info->status = 0;
334 *out_size = sizeof(*mac_info);
339 static int hinic_set_vf_mac_msg_handler(void *hwdev, u16 vf_id,
340 void *buf_in, u16 in_size,
341 void *buf_out, u16 *out_size)
343 struct hinic_port_mac_cmd *mac_out = buf_out;
344 struct hinic_port_mac_cmd *mac_in = buf_in;
345 struct hinic_hwdev *hw_dev = hwdev;
346 struct hinic_func_to_io *nic_io;
347 struct vf_data_storage *vf_info;
350 nic_io = &hw_dev->func_to_io;
351 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id);
352 if (vf_info->pf_set_mac && !(vf_info->trust) &&
353 is_valid_ether_addr(mac_in->mac)) {
354 dev_warn(&hw_dev->hwif->pdev->dev, "PF has already set VF %d MAC address\n",
355 HW_VF_ID_TO_OS(vf_id));
356 mac_out->status = HINIC_PF_SET_VF_ALREADY;
357 *out_size = sizeof(*mac_out);
361 err = hinic_port_msg_cmd(hw_dev, HINIC_PORT_CMD_SET_MAC, buf_in,
362 in_size, buf_out, out_size);
363 if ((err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW) || !(*out_size)) {
364 dev_err(&hw_dev->hwif->pdev->dev,
365 "Failed to set VF %d MAC address, err: %d, status: 0x%x, out size: 0x%x\n",
366 HW_VF_ID_TO_OS(vf_id), err, mac_out->status, *out_size);
373 static int hinic_del_vf_mac_msg_handler(void *hwdev, u16 vf_id,
374 void *buf_in, u16 in_size,
375 void *buf_out, u16 *out_size)
377 struct hinic_port_mac_cmd *mac_out = buf_out;
378 struct hinic_port_mac_cmd *mac_in = buf_in;
379 struct hinic_hwdev *hw_dev = hwdev;
380 struct hinic_func_to_io *nic_io;
381 struct vf_data_storage *vf_info;
384 nic_io = &hw_dev->func_to_io;
385 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id);
386 if (vf_info->pf_set_mac && is_valid_ether_addr(mac_in->mac) &&
387 !memcmp(vf_info->vf_mac_addr, mac_in->mac, ETH_ALEN)) {
388 dev_warn(&hw_dev->hwif->pdev->dev, "PF has already set VF mac.\n");
389 mac_out->status = HINIC_PF_SET_VF_ALREADY;
390 *out_size = sizeof(*mac_out);
394 err = hinic_port_msg_cmd(hw_dev, HINIC_PORT_CMD_DEL_MAC, buf_in,
395 in_size, buf_out, out_size);
396 if ((err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW) || !(*out_size)) {
397 dev_err(&hw_dev->hwif->pdev->dev, "Failed to delete VF %d MAC, err: %d, status: 0x%x, out size: 0x%x\n",
398 HW_VF_ID_TO_OS(vf_id), err, mac_out->status, *out_size);
405 static int hinic_get_vf_link_status_msg_handler(void *hwdev, u16 vf_id,
406 void *buf_in, u16 in_size,
407 void *buf_out, u16 *out_size)
409 struct hinic_port_link_cmd *get_link = buf_out;
410 struct hinic_hwdev *hw_dev = hwdev;
411 struct vf_data_storage *vf_infos;
412 struct hinic_func_to_io *nic_io;
413 bool link_forced, link_up;
415 nic_io = &hw_dev->func_to_io;
416 vf_infos = nic_io->vf_infos;
417 link_forced = vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced;
418 link_up = vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up;
421 get_link->state = link_up ?
422 HINIC_LINK_STATE_UP : HINIC_LINK_STATE_DOWN;
424 get_link->state = nic_io->link_status;
426 get_link->status = 0;
427 *out_size = sizeof(*get_link);
432 static bool check_func_table(struct hinic_hwdev *hwdev, u16 func_idx,
433 void *buf_in, u16 in_size)
435 struct hinic_cmd_fw_ctxt *function_table = buf_in;
437 if (!hinic_mbox_check_func_id_8B(hwdev, func_idx, buf_in, in_size) ||
438 !function_table->rx_buf_sz)
444 static struct vf_cmd_msg_handle nic_vf_cmd_msg_handler[] = {
445 {HINIC_PORT_CMD_VF_REGISTER, hinic_register_vf_msg_handler},
446 {HINIC_PORT_CMD_VF_UNREGISTER, hinic_unregister_vf_msg_handler},
447 {HINIC_PORT_CMD_CHANGE_MTU, hinic_change_vf_mtu_msg_handler},
448 {HINIC_PORT_CMD_GET_MAC, hinic_get_vf_mac_msg_handler},
449 {HINIC_PORT_CMD_SET_MAC, hinic_set_vf_mac_msg_handler},
450 {HINIC_PORT_CMD_DEL_MAC, hinic_del_vf_mac_msg_handler},
451 {HINIC_PORT_CMD_GET_LINK_STATE, hinic_get_vf_link_status_msg_handler},
454 static struct vf_cmd_check_handle nic_cmd_support_vf[] = {
455 {HINIC_PORT_CMD_VF_REGISTER, NULL},
456 {HINIC_PORT_CMD_VF_UNREGISTER, NULL},
457 {HINIC_PORT_CMD_CHANGE_MTU, hinic_mbox_check_func_id_8B},
458 {HINIC_PORT_CMD_ADD_VLAN, hinic_mbox_check_func_id_8B},
459 {HINIC_PORT_CMD_DEL_VLAN, hinic_mbox_check_func_id_8B},
460 {HINIC_PORT_CMD_SET_MAC, hinic_mbox_check_func_id_8B},
461 {HINIC_PORT_CMD_GET_MAC, hinic_mbox_check_func_id_8B},
462 {HINIC_PORT_CMD_DEL_MAC, hinic_mbox_check_func_id_8B},
463 {HINIC_PORT_CMD_SET_RX_MODE, hinic_mbox_check_func_id_8B},
464 {HINIC_PORT_CMD_GET_PAUSE_INFO, hinic_mbox_check_func_id_8B},
465 {HINIC_PORT_CMD_GET_LINK_STATE, hinic_mbox_check_func_id_8B},
466 {HINIC_PORT_CMD_SET_LRO, hinic_mbox_check_func_id_8B},
467 {HINIC_PORT_CMD_SET_RX_CSUM, hinic_mbox_check_func_id_8B},
468 {HINIC_PORT_CMD_SET_RX_VLAN_OFFLOAD, hinic_mbox_check_func_id_8B},
469 {HINIC_PORT_CMD_GET_VPORT_STAT, hinic_mbox_check_func_id_8B},
470 {HINIC_PORT_CMD_CLEAN_VPORT_STAT, hinic_mbox_check_func_id_8B},
471 {HINIC_PORT_CMD_GET_RSS_TEMPLATE_INDIR_TBL,
472 hinic_mbox_check_func_id_8B},
473 {HINIC_PORT_CMD_SET_RSS_TEMPLATE_TBL, hinic_mbox_check_func_id_8B},
474 {HINIC_PORT_CMD_GET_RSS_TEMPLATE_TBL, hinic_mbox_check_func_id_8B},
475 {HINIC_PORT_CMD_SET_RSS_HASH_ENGINE, hinic_mbox_check_func_id_8B},
476 {HINIC_PORT_CMD_GET_RSS_HASH_ENGINE, hinic_mbox_check_func_id_8B},
477 {HINIC_PORT_CMD_GET_RSS_CTX_TBL, hinic_mbox_check_func_id_8B},
478 {HINIC_PORT_CMD_SET_RSS_CTX_TBL, hinic_mbox_check_func_id_8B},
479 {HINIC_PORT_CMD_RSS_TEMP_MGR, hinic_mbox_check_func_id_8B},
480 {HINIC_PORT_CMD_RSS_CFG, hinic_mbox_check_func_id_8B},
481 {HINIC_PORT_CMD_FWCTXT_INIT, check_func_table},
482 {HINIC_PORT_CMD_GET_MGMT_VERSION, NULL},
483 {HINIC_PORT_CMD_SET_FUNC_STATE, hinic_mbox_check_func_id_8B},
484 {HINIC_PORT_CMD_GET_GLOBAL_QPN, hinic_mbox_check_func_id_8B},
485 {HINIC_PORT_CMD_SET_TSO, hinic_mbox_check_func_id_8B},
486 {HINIC_PORT_CMD_SET_RQ_IQ_MAP, hinic_mbox_check_func_id_8B},
487 {HINIC_PORT_CMD_LINK_STATUS_REPORT, hinic_mbox_check_func_id_8B},
488 {HINIC_PORT_CMD_UPDATE_MAC, hinic_mbox_check_func_id_8B},
489 {HINIC_PORT_CMD_GET_CAP, hinic_mbox_check_func_id_8B},
490 {HINIC_PORT_CMD_GET_LINK_MODE, hinic_mbox_check_func_id_8B},
493 #define CHECK_IPSU_15BIT 0X8000
496 struct hinic_sriov_info *hinic_get_sriov_info_by_pcidev(struct pci_dev *pdev)
498 struct net_device *netdev = pci_get_drvdata(pdev);
499 struct hinic_dev *nic_dev = netdev_priv(netdev);
501 return &nic_dev->sriov_info;
504 static int hinic_check_mac_info(u8 status, u16 vlan_id)
506 if ((status && status != HINIC_MGMT_STATUS_EXIST) ||
507 (vlan_id & CHECK_IPSU_15BIT &&
508 status == HINIC_MGMT_STATUS_EXIST))
514 #define HINIC_VLAN_ID_MASK 0x7FFF
516 static int hinic_update_mac(struct hinic_hwdev *hwdev, u8 *old_mac,
517 u8 *new_mac, u16 vlan_id, u16 func_id)
519 struct hinic_port_mac_update mac_info = {0};
520 u16 out_size = sizeof(mac_info);
523 if (!hwdev || !old_mac || !new_mac)
526 if ((vlan_id & HINIC_VLAN_ID_MASK) >= VLAN_N_VID) {
527 dev_err(&hwdev->hwif->pdev->dev, "Invalid VLAN number: %d\n",
528 (vlan_id & HINIC_VLAN_ID_MASK));
532 mac_info.func_id = func_id;
533 mac_info.vlan_id = vlan_id;
534 memcpy(mac_info.old_mac, old_mac, ETH_ALEN);
535 memcpy(mac_info.new_mac, new_mac, ETH_ALEN);
537 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_UPDATE_MAC, &mac_info,
538 sizeof(mac_info), &mac_info, &out_size);
540 if (err || !out_size ||
541 hinic_check_mac_info(mac_info.status, mac_info.vlan_id)) {
542 dev_err(&hwdev->hwif->pdev->dev,
543 "Failed to update MAC, err: %d, status: 0x%x, out size: 0x%x\n",
544 err, mac_info.status, out_size);
548 if (mac_info.status == HINIC_MGMT_STATUS_EXIST)
549 dev_warn(&hwdev->hwif->pdev->dev, "MAC is repeated. Ignore update operation\n");
554 static void hinic_get_vf_config(struct hinic_hwdev *hwdev, u16 vf_id,
555 struct ifla_vf_info *ivi)
557 struct vf_data_storage *vfinfo;
559 vfinfo = hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
561 ivi->vf = HW_VF_ID_TO_OS(vf_id);
562 memcpy(ivi->mac, vfinfo->vf_mac_addr, ETH_ALEN);
563 ivi->vlan = vfinfo->pf_vlan;
564 ivi->qos = vfinfo->pf_qos;
565 ivi->spoofchk = vfinfo->spoofchk;
566 ivi->trusted = vfinfo->trust;
567 ivi->max_tx_rate = vfinfo->max_rate;
568 ivi->min_tx_rate = vfinfo->min_rate;
570 if (!vfinfo->link_forced)
571 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
572 else if (vfinfo->link_up)
573 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
575 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
578 int hinic_ndo_get_vf_config(struct net_device *netdev,
579 int vf, struct ifla_vf_info *ivi)
581 struct hinic_dev *nic_dev = netdev_priv(netdev);
582 struct hinic_sriov_info *sriov_info;
584 sriov_info = &nic_dev->sriov_info;
585 if (vf >= sriov_info->num_vfs)
588 hinic_get_vf_config(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), ivi);
593 static int hinic_set_vf_mac(struct hinic_hwdev *hwdev, int vf,
594 unsigned char *mac_addr)
596 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
597 struct vf_data_storage *vf_info;
601 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf);
603 /* duplicate request, so just return success */
604 if (vf_info->pf_set_mac &&
605 !memcmp(vf_info->vf_mac_addr, mac_addr, ETH_ALEN))
608 vf_info->pf_set_mac = true;
610 func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf;
611 err = hinic_update_mac(hwdev, vf_info->vf_mac_addr,
612 mac_addr, 0, func_id);
614 vf_info->pf_set_mac = false;
618 memcpy(vf_info->vf_mac_addr, mac_addr, ETH_ALEN);
623 int hinic_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
625 struct hinic_dev *nic_dev = netdev_priv(netdev);
626 struct hinic_sriov_info *sriov_info;
629 sriov_info = &nic_dev->sriov_info;
630 if (!is_valid_ether_addr(mac) || vf >= sriov_info->num_vfs)
633 err = hinic_set_vf_mac(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), mac);
637 netif_info(nic_dev, drv, netdev, "Setting MAC %pM on VF %d\n", mac, vf);
638 netif_info(nic_dev, drv, netdev, "Reload the VF driver to make this change effective.");
643 static int hinic_add_vf_vlan(struct hinic_hwdev *hwdev, int vf_id,
646 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
649 err = hinic_set_vf_vlan(hwdev, true, vlan, qos, vf_id);
653 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan = vlan;
654 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos = qos;
656 dev_info(&hwdev->hwif->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
657 vlan, qos, HW_VF_ID_TO_OS(vf_id));
661 static int hinic_kill_vf_vlan(struct hinic_hwdev *hwdev, int vf_id)
663 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
666 err = hinic_set_vf_vlan(hwdev, false,
667 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan,
668 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos,
673 dev_info(&hwdev->hwif->pdev->dev, "Remove VLAN %d on VF %d\n",
674 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan,
675 HW_VF_ID_TO_OS(vf_id));
677 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan = 0;
678 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos = 0;
683 static int hinic_update_mac_vlan(struct hinic_dev *nic_dev, u16 old_vlan,
684 u16 new_vlan, int vf_id)
686 struct vf_data_storage *vf_info;
690 if (!nic_dev || old_vlan >= VLAN_N_VID || new_vlan >= VLAN_N_VID)
693 vf_info = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
694 if (!vf_info->pf_set_mac)
699 vlan_id |= HINIC_ADD_VLAN_IN_MAC;
701 err = hinic_port_del_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
703 dev_err(&nic_dev->hwdev->hwif->pdev->dev, "Failed to delete VF %d MAC %pM vlan %d\n",
704 HW_VF_ID_TO_OS(vf_id), vf_info->vf_mac_addr, old_vlan);
710 vlan_id |= HINIC_ADD_VLAN_IN_MAC;
712 err = hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
714 dev_err(&nic_dev->hwdev->hwif->pdev->dev, "Failed to add VF %d MAC %pM vlan %d\n",
715 HW_VF_ID_TO_OS(vf_id), vf_info->vf_mac_addr, new_vlan);
724 vlan_id |= HINIC_ADD_VLAN_IN_MAC;
725 hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
730 static int set_hw_vf_vlan(struct hinic_dev *nic_dev,
731 u16 cur_vlanprio, int vf, u16 vlan, u8 qos)
733 u16 old_vlan = cur_vlanprio & VLAN_VID_MASK;
738 err = hinic_kill_vf_vlan(nic_dev->hwdev,
741 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d old vlan %d\n",
746 err = hinic_add_vf_vlan(nic_dev->hwdev,
747 OS_VF_ID_TO_HW(vf), vlan, qos);
749 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to add vf %d new vlan %d\n",
754 err = hinic_kill_vf_vlan(nic_dev->hwdev, OS_VF_ID_TO_HW(vf));
756 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d vlan %d\n",
762 err = hinic_update_mac_vlan(nic_dev, old_vlan, vlan,
769 int hinic_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos,
772 struct hinic_dev *nic_dev = netdev_priv(netdev);
773 struct hinic_sriov_info *sriov_info;
774 u16 vlanprio, cur_vlanprio;
776 sriov_info = &nic_dev->sriov_info;
777 if (vf >= sriov_info->num_vfs || vlan > 4095 || qos > 7)
779 if (vlan_proto != htons(ETH_P_8021Q))
780 return -EPROTONOSUPPORT;
781 vlanprio = vlan | qos << HINIC_VLAN_PRIORITY_SHIFT;
782 cur_vlanprio = hinic_vf_info_vlanprio(nic_dev->hwdev,
784 /* duplicate request, so just return success */
785 if (vlanprio == cur_vlanprio)
788 return set_hw_vf_vlan(nic_dev, cur_vlanprio, vf, vlan, qos);
791 static int hinic_set_vf_trust(struct hinic_hwdev *hwdev, u16 vf_id,
794 struct vf_data_storage *vf_infos;
795 struct hinic_func_to_io *nic_io;
800 nic_io = &hwdev->func_to_io;
801 vf_infos = nic_io->vf_infos;
802 vf_infos[vf_id].trust = trust;
807 int hinic_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting)
809 struct hinic_dev *adapter = netdev_priv(netdev);
810 struct hinic_sriov_info *sriov_info;
811 struct hinic_func_to_io *nic_io;
815 sriov_info = &adapter->sriov_info;
816 nic_io = &adapter->hwdev->func_to_io;
818 if (vf >= sriov_info->num_vfs)
821 cur_trust = nic_io->vf_infos[vf].trust;
822 /* same request, so just return success */
823 if (setting == cur_trust)
826 err = hinic_set_vf_trust(adapter->hwdev, vf, setting);
828 dev_info(&sriov_info->pdev->dev, "Set VF %d trusted %s succeed\n",
829 vf, setting ? "on" : "off");
831 dev_err(&sriov_info->pdev->dev, "Failed set VF %d trusted %s\n",
832 vf, setting ? "on" : "off");
837 int hinic_ndo_set_vf_bw(struct net_device *netdev,
838 int vf, int min_tx_rate, int max_tx_rate)
840 static const u32 speeds[] = {
841 SPEED_10, SPEED_100, SPEED_1000, SPEED_10000,
842 SPEED_25000, SPEED_40000, SPEED_100000
844 struct hinic_dev *nic_dev = netdev_priv(netdev);
845 struct hinic_port_cap port_cap = { 0 };
846 enum hinic_port_link_state link_state;
849 if (vf >= nic_dev->sriov_info.num_vfs) {
850 netif_err(nic_dev, drv, netdev, "VF number must be less than %d\n",
851 nic_dev->sriov_info.num_vfs);
855 err = hinic_port_link_state(nic_dev, &link_state);
857 netif_err(nic_dev, drv, netdev,
858 "Get link status failed when setting vf tx rate\n");
862 if (link_state == HINIC_LINK_STATE_DOWN) {
863 netif_err(nic_dev, drv, netdev,
864 "Link status must be up when setting vf tx rate\n");
868 err = hinic_port_get_cap(nic_dev, &port_cap);
869 if (err || port_cap.speed > LINK_SPEED_100GB)
872 /* rate limit cannot be less than 0 and greater than link speed */
873 if (max_tx_rate < 0 || max_tx_rate > speeds[port_cap.speed]) {
874 netif_err(nic_dev, drv, netdev, "Max tx rate must be in [0 - %d]\n",
875 speeds[port_cap.speed]);
879 err = hinic_set_vf_tx_rate(nic_dev->hwdev, OS_VF_ID_TO_HW(vf),
880 max_tx_rate, min_tx_rate);
882 netif_err(nic_dev, drv, netdev,
883 "Unable to set VF %d max rate %d min rate %d%s\n",
884 vf, max_tx_rate, min_tx_rate,
885 err == HINIC_TX_RATE_TABLE_FULL ?
886 ", tx rate profile is full" : "");
890 netif_info(nic_dev, drv, netdev,
891 "Set VF %d max tx rate %d min tx rate %d successfully\n",
892 vf, max_tx_rate, min_tx_rate);
897 static int hinic_set_vf_spoofchk(struct hinic_hwdev *hwdev, u16 vf_id,
900 struct hinic_spoofchk_set spoofchk_cfg = {0};
901 struct vf_data_storage *vf_infos = NULL;
902 u16 out_size = sizeof(spoofchk_cfg);
908 vf_infos = hwdev->func_to_io.vf_infos;
910 spoofchk_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
911 spoofchk_cfg.state = spoofchk ? 1 : 0;
912 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_ENABLE_SPOOFCHK,
913 &spoofchk_cfg, sizeof(spoofchk_cfg),
914 &spoofchk_cfg, &out_size);
915 if (spoofchk_cfg.status == HINIC_MGMT_CMD_UNSUPPORTED) {
916 err = HINIC_MGMT_CMD_UNSUPPORTED;
917 } else if (err || !out_size || spoofchk_cfg.status) {
918 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) spoofchk, err: %d, status: 0x%x, out size: 0x%x\n",
919 HW_VF_ID_TO_OS(vf_id), err, spoofchk_cfg.status,
924 vf_infos[HW_VF_ID_TO_OS(vf_id)].spoofchk = spoofchk;
929 int hinic_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
931 struct hinic_dev *nic_dev = netdev_priv(netdev);
932 struct hinic_sriov_info *sriov_info;
936 sriov_info = &nic_dev->sriov_info;
937 if (vf >= sriov_info->num_vfs)
940 cur_spoofchk = nic_dev->hwdev->func_to_io.vf_infos[vf].spoofchk;
942 /* same request, so just return success */
943 if (setting == cur_spoofchk)
946 err = hinic_set_vf_spoofchk(sriov_info->hwdev,
947 OS_VF_ID_TO_HW(vf), setting);
949 netif_info(nic_dev, drv, netdev, "Set VF %d spoofchk %s successfully\n",
950 vf, setting ? "on" : "off");
951 } else if (err == HINIC_MGMT_CMD_UNSUPPORTED) {
952 netif_err(nic_dev, drv, netdev,
953 "Current firmware doesn't support to set vf spoofchk, need to upgrade latest firmware version\n");
960 static int hinic_set_vf_link_state(struct hinic_hwdev *hwdev, u16 vf_id,
963 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
964 struct vf_data_storage *vf_infos = nic_io->vf_infos;
968 case HINIC_IFLA_VF_LINK_STATE_AUTO:
969 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = false;
970 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = nic_io->link_status ?
972 link_status = nic_io->link_status;
974 case HINIC_IFLA_VF_LINK_STATE_ENABLE:
975 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
976 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = true;
977 link_status = HINIC_LINK_UP;
979 case HINIC_IFLA_VF_LINK_STATE_DISABLE:
980 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
981 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = false;
982 link_status = HINIC_LINK_DOWN;
988 /* Notify the VF of its new link state */
989 hinic_notify_vf_link_status(hwdev, vf_id, link_status);
994 int hinic_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
996 struct hinic_dev *nic_dev = netdev_priv(netdev);
997 struct hinic_sriov_info *sriov_info;
999 sriov_info = &nic_dev->sriov_info;
1001 if (vf_id >= sriov_info->num_vfs) {
1002 netif_err(nic_dev, drv, netdev,
1003 "Invalid VF Identifier %d\n", vf_id);
1007 return hinic_set_vf_link_state(sriov_info->hwdev,
1008 OS_VF_ID_TO_HW(vf_id), link);
1011 /* pf receive message from vf */
1012 static int nic_pf_mbox_handler(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1013 u16 in_size, void *buf_out, u16 *out_size)
1015 u8 size = ARRAY_SIZE(nic_cmd_support_vf);
1016 struct vf_cmd_msg_handle *vf_msg_handle;
1017 struct hinic_hwdev *dev = hwdev;
1018 struct hinic_func_to_io *nic_io;
1019 struct hinic_pfhwdev *pfhwdev;
1026 if (!hinic_mbox_check_cmd_valid(hwdev, nic_cmd_support_vf, vf_id, cmd,
1027 buf_in, in_size, size)) {
1028 dev_err(&dev->hwif->pdev->dev,
1029 "PF Receive VF nic cmd: 0x%x, mbox len: 0x%x is invalid\n",
1031 return HINIC_MBOX_VF_CMD_ERROR;
1034 pfhwdev = container_of(dev, struct hinic_pfhwdev, hwdev);
1035 nic_io = &dev->func_to_io;
1036 for (i = 0; i < ARRAY_SIZE(nic_vf_cmd_msg_handler); i++) {
1037 vf_msg_handle = &nic_vf_cmd_msg_handler[i];
1038 if (cmd == vf_msg_handle->cmd &&
1039 vf_msg_handle->cmd_msg_handler) {
1040 err = vf_msg_handle->cmd_msg_handler(hwdev, vf_id,
1047 if (i == ARRAY_SIZE(nic_vf_cmd_msg_handler))
1048 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC,
1049 cmd, buf_in, in_size, buf_out,
1050 out_size, HINIC_MGMT_MSG_SYNC);
1052 if (err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW)
1053 dev_err(&nic_io->hwif->pdev->dev, "PF receive VF L2NIC cmd: %d process error, err:%d\n",
1058 static int cfg_mbx_pf_proc_vf_msg(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1059 u16 in_size, void *buf_out, u16 *out_size)
1061 struct hinic_dev_cap *dev_cap = buf_out;
1062 struct hinic_hwdev *dev = hwdev;
1063 struct hinic_cap *cap;
1065 cap = &dev->nic_cap;
1066 memset(dev_cap, 0, sizeof(*dev_cap));
1068 dev_cap->max_vf = cap->max_vf;
1069 dev_cap->max_sqs = cap->max_vf_qps;
1070 dev_cap->max_rqs = cap->max_vf_qps;
1071 dev_cap->port_id = dev->port_id;
1073 *out_size = sizeof(*dev_cap);
1078 static int hinic_init_vf_infos(struct hinic_func_to_io *nic_io, u16 vf_id)
1080 struct vf_data_storage *vf_infos = nic_io->vf_infos;
1082 if (set_vf_link_state > HINIC_IFLA_VF_LINK_STATE_DISABLE) {
1083 dev_warn(&nic_io->hwif->pdev->dev, "Module Parameter set_vf_link_state value %d is out of range, resetting to %d\n",
1084 set_vf_link_state, HINIC_IFLA_VF_LINK_STATE_AUTO);
1085 set_vf_link_state = HINIC_IFLA_VF_LINK_STATE_AUTO;
1088 switch (set_vf_link_state) {
1089 case HINIC_IFLA_VF_LINK_STATE_AUTO:
1090 vf_infos[vf_id].link_forced = false;
1092 case HINIC_IFLA_VF_LINK_STATE_ENABLE:
1093 vf_infos[vf_id].link_forced = true;
1094 vf_infos[vf_id].link_up = true;
1096 case HINIC_IFLA_VF_LINK_STATE_DISABLE:
1097 vf_infos[vf_id].link_forced = true;
1098 vf_infos[vf_id].link_up = false;
1101 dev_err(&nic_io->hwif->pdev->dev, "Invalid input parameter set_vf_link_state: %d\n",
1109 static void hinic_clear_vf_infos(struct hinic_dev *nic_dev, u16 vf_id)
1111 struct vf_data_storage *vf_infos;
1113 vf_infos = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
1114 if (vf_infos->pf_set_mac)
1115 hinic_port_del_mac(nic_dev, vf_infos->vf_mac_addr, 0);
1117 if (hinic_vf_info_vlanprio(nic_dev->hwdev, vf_id))
1118 hinic_kill_vf_vlan(nic_dev->hwdev, vf_id);
1120 if (vf_infos->max_rate)
1121 hinic_set_vf_tx_rate(nic_dev->hwdev, vf_id, 0, 0);
1123 if (vf_infos->spoofchk)
1124 hinic_set_vf_spoofchk(nic_dev->hwdev, vf_id, false);
1126 if (vf_infos->trust)
1127 hinic_set_vf_trust(nic_dev->hwdev, vf_id, false);
1129 memset(vf_infos, 0, sizeof(*vf_infos));
1130 /* set vf_infos to default */
1131 hinic_init_vf_infos(&nic_dev->hwdev->func_to_io, HW_VF_ID_TO_OS(vf_id));
1134 static void hinic_deinit_vf_hw(struct hinic_sriov_info *sriov_info,
1135 u16 start_vf_id, u16 end_vf_id)
1137 struct hinic_dev *nic_dev;
1140 nic_dev = container_of(sriov_info, struct hinic_dev, sriov_info);
1142 for (idx = start_vf_id; idx <= end_vf_id; idx++) {
1143 func_idx = hinic_glb_pf_vf_offset(nic_dev->hwdev->hwif) + idx;
1144 hinic_set_wq_page_size(nic_dev->hwdev, func_idx,
1145 HINIC_HW_WQ_PAGE_SIZE);
1146 hinic_clear_vf_infos(nic_dev, idx);
1150 int hinic_vf_func_init(struct hinic_hwdev *hwdev)
1152 struct hinic_register_vf register_info = {0};
1153 u16 out_size = sizeof(register_info);
1154 struct hinic_func_to_io *nic_io;
1158 err = hinic_vf_mbox_random_id_init(hwdev);
1160 dev_err(&hwdev->hwif->pdev->dev, "Failed to init vf mbox random id, err: %d\n",
1165 nic_io = &hwdev->func_to_io;
1167 if (HINIC_IS_VF(hwdev->hwif)) {
1168 err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1169 HINIC_PORT_CMD_VF_REGISTER,
1170 ®ister_info, sizeof(register_info),
1171 ®ister_info, &out_size, 0);
1172 if (err || register_info.status || !out_size) {
1173 dev_err(&hwdev->hwif->pdev->dev,
1174 "Failed to register VF, err: %d, status: 0x%x, out size: 0x%x\n",
1175 err, register_info.status, out_size);
1176 hinic_unregister_vf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1180 err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_CFGM,
1181 cfg_mbx_pf_proc_vf_msg);
1183 dev_err(&hwdev->hwif->pdev->dev,
1184 "Register PF mailbox callback failed\n");
1187 nic_io->max_vfs = hwdev->nic_cap.max_vf;
1188 size = sizeof(*nic_io->vf_infos) * nic_io->max_vfs;
1190 nic_io->vf_infos = kzalloc(size, GFP_KERNEL);
1191 if (!nic_io->vf_infos) {
1193 goto out_free_nic_io;
1196 for (i = 0; i < nic_io->max_vfs; i++) {
1197 err = hinic_init_vf_infos(nic_io, i);
1199 goto err_init_vf_infos;
1202 err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC,
1203 nic_pf_mbox_handler);
1205 goto err_register_pf_mbox_cb;
1211 err_register_pf_mbox_cb:
1213 kfree(nic_io->vf_infos);
1218 void hinic_vf_func_free(struct hinic_hwdev *hwdev)
1220 struct hinic_register_vf unregister = {0};
1221 u16 out_size = sizeof(unregister);
1224 if (HINIC_IS_VF(hwdev->hwif)) {
1225 err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1226 HINIC_PORT_CMD_VF_UNREGISTER,
1227 &unregister, sizeof(unregister),
1228 &unregister, &out_size, 0);
1229 if (err || !out_size || unregister.status)
1230 dev_err(&hwdev->hwif->pdev->dev, "Failed to unregister VF, err: %d, status: 0x%x, out_size: 0x%x\n",
1231 err, unregister.status, out_size);
1233 if (hwdev->func_to_io.vf_infos) {
1234 hinic_unregister_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1235 kfree(hwdev->func_to_io.vf_infos);
1240 static int hinic_init_vf_hw(struct hinic_hwdev *hwdev, u16 start_vf_id,
1246 /* vf use 256K as default wq page size, and can't change it */
1247 for (i = start_vf_id; i <= end_vf_id; i++) {
1248 func_idx = hinic_glb_pf_vf_offset(hwdev->hwif) + i;
1249 err = hinic_set_wq_page_size(hwdev, func_idx,
1250 HINIC_DEFAULT_WQ_PAGE_SIZE);
1258 int hinic_pci_sriov_disable(struct pci_dev *pdev)
1260 struct hinic_sriov_info *sriov_info;
1263 sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1264 /* if SR-IOV is already disabled then nothing will be done */
1265 if (!sriov_info->sriov_enabled)
1268 set_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1270 /* If our VFs are assigned we cannot shut down SR-IOV
1271 * without causing issues, so just leave the hardware
1272 * available but disabled
1274 if (pci_vfs_assigned(sriov_info->pdev)) {
1275 clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1276 dev_warn(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
1279 sriov_info->sriov_enabled = false;
1281 /* disable iov and allow time for transactions to clear */
1282 pci_disable_sriov(sriov_info->pdev);
1284 tmp_vfs = (u16)sriov_info->num_vfs;
1285 sriov_info->num_vfs = 0;
1286 hinic_deinit_vf_hw(sriov_info, OS_VF_ID_TO_HW(0),
1287 OS_VF_ID_TO_HW(tmp_vfs - 1));
1289 clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1294 static int hinic_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1296 struct hinic_sriov_info *sriov_info;
1299 sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1301 if (test_and_set_bit(HINIC_SRIOV_ENABLE, &sriov_info->state)) {
1303 "SR-IOV enable in process, please wait, num_vfs %d\n",
1308 err = hinic_init_vf_hw(sriov_info->hwdev, OS_VF_ID_TO_HW(0),
1309 OS_VF_ID_TO_HW((u16)num_vfs - 1));
1311 dev_err(&sriov_info->pdev->dev,
1312 "Failed to init vf in hardware before enable sriov, error %d\n",
1314 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1318 err = pci_enable_sriov(sriov_info->pdev, num_vfs);
1321 "Failed to enable SR-IOV, error %d\n", err);
1322 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1326 sriov_info->sriov_enabled = true;
1327 sriov_info->num_vfs = num_vfs;
1328 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1333 int hinic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1335 struct hinic_sriov_info *sriov_info;
1337 sriov_info = hinic_get_sriov_info_by_pcidev(dev);
1339 if (test_bit(HINIC_FUNC_REMOVE, &sriov_info->state))
1343 return hinic_pci_sriov_disable(dev);
1345 return hinic_pci_sriov_enable(dev, num_vfs);