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
27 #define HINIC_MAX_QOS 7
29 static int hinic_set_mac(struct hinic_hwdev *hwdev, const u8 *mac_addr,
30 u16 vlan_id, u16 func_id)
32 struct hinic_port_mac_cmd mac_info = {0};
33 u16 out_size = sizeof(mac_info);
36 mac_info.func_idx = func_id;
37 mac_info.vlan_id = vlan_id;
38 memcpy(mac_info.mac, mac_addr, ETH_ALEN);
40 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_MAC, &mac_info,
41 sizeof(mac_info), &mac_info, &out_size);
42 if (err || out_size != sizeof(mac_info) ||
43 (mac_info.status && mac_info.status != HINIC_MGMT_STATUS_EXIST)) {
44 dev_err(&hwdev->func_to_io.hwif->pdev->dev, "Failed to set MAC, err: %d, status: 0x%x, out size: 0x%x\n",
45 err, mac_info.status, out_size);
52 static void hinic_notify_vf_link_status(struct hinic_hwdev *hwdev, u16 vf_id,
55 struct vf_data_storage *vf_infos = hwdev->func_to_io.vf_infos;
56 struct hinic_port_link_status link = {0};
57 u16 out_size = sizeof(link);
60 if (vf_infos[HW_VF_ID_TO_OS(vf_id)].registered) {
61 link.link = link_status;
62 link.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
63 err = hinic_mbox_to_vf(hwdev, HINIC_MOD_L2NIC,
64 vf_id, HINIC_PORT_CMD_LINK_STATUS_REPORT,
67 if (err || !out_size || link.status)
68 dev_err(&hwdev->hwif->pdev->dev,
69 "Send link change event to VF %d failed, err: %d, status: 0x%x, out_size: 0x%x\n",
70 HW_VF_ID_TO_OS(vf_id), err,
71 link.status, out_size);
75 /* send link change event mbox msg to active vfs under the pf */
76 void hinic_notify_all_vfs_link_changed(struct hinic_hwdev *hwdev,
79 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
82 nic_io->link_status = link_status;
83 for (i = 1; i <= nic_io->max_vfs; i++) {
84 if (!nic_io->vf_infos[HW_VF_ID_TO_OS(i)].link_forced)
85 hinic_notify_vf_link_status(hwdev, i, link_status);
89 static u16 hinic_vf_info_vlanprio(struct hinic_hwdev *hwdev, int vf_id)
91 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
92 u16 pf_vlan, vlanprio;
95 pf_vlan = nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan;
96 pf_qos = nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos;
97 vlanprio = pf_vlan | pf_qos << HINIC_VLAN_PRIORITY_SHIFT;
102 static int hinic_set_vf_vlan(struct hinic_hwdev *hwdev, bool add, u16 vid,
105 struct hinic_vf_vlan_config vf_vlan = {0};
106 u16 out_size = sizeof(vf_vlan);
110 /* VLAN 0 is a special case, don't allow it to be removed */
114 vf_vlan.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
115 vf_vlan.vlan_id = vid;
119 cmd = HINIC_PORT_CMD_SET_VF_VLAN;
121 cmd = HINIC_PORT_CMD_CLR_VF_VLAN;
123 err = hinic_port_msg_cmd(hwdev, cmd, &vf_vlan,
124 sizeof(vf_vlan), &vf_vlan, &out_size);
125 if (err || !out_size || vf_vlan.status) {
126 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF %d vlan, err: %d, status: 0x%x, out size: 0x%x\n",
127 HW_VF_ID_TO_OS(vf_id), err, vf_vlan.status, out_size);
134 static int hinic_set_vf_tx_rate_max_min(struct hinic_hwdev *hwdev, u16 vf_id,
135 u32 max_rate, u32 min_rate)
137 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
138 struct hinic_tx_rate_cfg_max_min rate_cfg = {0};
139 u16 out_size = sizeof(rate_cfg);
142 rate_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
143 rate_cfg.max_rate = max_rate;
144 rate_cfg.min_rate = min_rate;
145 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_VF_MAX_MIN_RATE,
146 &rate_cfg, sizeof(rate_cfg), &rate_cfg,
148 if ((rate_cfg.status != HINIC_MGMT_CMD_UNSUPPORTED &&
149 rate_cfg.status) || err || !out_size) {
150 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",
151 HW_VF_ID_TO_OS(vf_id), max_rate, min_rate, err,
152 rate_cfg.status, out_size);
156 if (!rate_cfg.status) {
157 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].max_rate = max_rate;
158 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].min_rate = min_rate;
161 return rate_cfg.status;
164 static int hinic_set_vf_rate_limit(struct hinic_hwdev *hwdev, u16 vf_id,
167 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
168 struct hinic_tx_rate_cfg rate_cfg = {0};
169 u16 out_size = sizeof(rate_cfg);
172 rate_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
173 rate_cfg.tx_rate = tx_rate;
174 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_VF_RATE,
175 &rate_cfg, sizeof(rate_cfg), &rate_cfg,
177 if (err || !out_size || rate_cfg.status) {
178 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) rate(%d), err: %d, status: 0x%x, out size: 0x%x\n",
179 HW_VF_ID_TO_OS(vf_id), tx_rate, err, rate_cfg.status,
182 return rate_cfg.status;
187 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].max_rate = tx_rate;
188 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].min_rate = 0;
193 static int hinic_set_vf_tx_rate(struct hinic_hwdev *hwdev, u16 vf_id,
194 u32 max_rate, u32 min_rate)
198 err = hinic_set_vf_tx_rate_max_min(hwdev, vf_id, max_rate, min_rate);
199 if (err != HINIC_MGMT_CMD_UNSUPPORTED)
203 dev_err(&hwdev->hwif->pdev->dev, "Current firmware doesn't support to set min tx rate\n");
207 dev_info(&hwdev->hwif->pdev->dev, "Current firmware doesn't support to set min tx rate, force min_tx_rate = max_tx_rate\n");
209 return hinic_set_vf_rate_limit(hwdev, vf_id, max_rate);
212 static int hinic_init_vf_config(struct hinic_hwdev *hwdev, u16 vf_id)
214 struct vf_data_storage *vf_info;
215 u16 func_id, vlan_id;
218 vf_info = hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
219 if (vf_info->pf_set_mac) {
220 func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
224 err = hinic_set_mac(hwdev, vf_info->vf_mac_addr, vlan_id,
227 dev_err(&hwdev->func_to_io.hwif->pdev->dev, "Failed to set VF %d MAC\n",
228 HW_VF_ID_TO_OS(vf_id));
233 if (hinic_vf_info_vlanprio(hwdev, vf_id)) {
234 err = hinic_set_vf_vlan(hwdev, true, vf_info->pf_vlan,
235 vf_info->pf_qos, vf_id);
237 dev_err(&hwdev->hwif->pdev->dev, "Failed to add VF %d VLAN_QOS\n",
238 HW_VF_ID_TO_OS(vf_id));
243 if (vf_info->max_rate) {
244 err = hinic_set_vf_tx_rate(hwdev, vf_id, vf_info->max_rate,
247 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF %d max rate: %d, min rate: %d\n",
248 HW_VF_ID_TO_OS(vf_id), vf_info->max_rate,
257 static int hinic_register_vf_msg_handler(void *hwdev, u16 vf_id,
258 void *buf_in, u16 in_size,
259 void *buf_out, u16 *out_size)
261 struct hinic_register_vf *register_info = buf_out;
262 struct hinic_hwdev *hw_dev = hwdev;
263 struct hinic_func_to_io *nic_io;
266 nic_io = &hw_dev->func_to_io;
267 if (vf_id > nic_io->max_vfs) {
268 dev_err(&hw_dev->hwif->pdev->dev, "Register VF id %d exceed limit[0-%d]\n",
269 HW_VF_ID_TO_OS(vf_id), HW_VF_ID_TO_OS(nic_io->max_vfs));
270 register_info->status = EFAULT;
274 *out_size = sizeof(*register_info);
275 err = hinic_init_vf_config(hw_dev, vf_id);
277 register_info->status = EFAULT;
281 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].registered = true;
286 static int hinic_unregister_vf_msg_handler(void *hwdev, u16 vf_id,
287 void *buf_in, u16 in_size,
288 void *buf_out, u16 *out_size)
290 struct hinic_hwdev *hw_dev = hwdev;
291 struct hinic_func_to_io *nic_io;
293 nic_io = &hw_dev->func_to_io;
295 if (vf_id > nic_io->max_vfs)
298 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].registered = false;
303 static int hinic_change_vf_mtu_msg_handler(void *hwdev, u16 vf_id,
304 void *buf_in, u16 in_size,
305 void *buf_out, u16 *out_size)
307 struct hinic_hwdev *hw_dev = hwdev;
310 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_CHANGE_MTU, buf_in,
311 in_size, buf_out, out_size);
313 dev_err(&hw_dev->hwif->pdev->dev, "Failed to set VF %u mtu\n",
321 static int hinic_get_vf_mac_msg_handler(void *hwdev, u16 vf_id,
322 void *buf_in, u16 in_size,
323 void *buf_out, u16 *out_size)
325 struct hinic_port_mac_cmd *mac_info = buf_out;
326 struct hinic_hwdev *dev = hwdev;
327 struct hinic_func_to_io *nic_io;
328 struct vf_data_storage *vf_info;
330 nic_io = &dev->func_to_io;
331 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id);
333 memcpy(mac_info->mac, vf_info->vf_mac_addr, ETH_ALEN);
334 mac_info->status = 0;
335 *out_size = sizeof(*mac_info);
340 static int hinic_set_vf_mac_msg_handler(void *hwdev, u16 vf_id,
341 void *buf_in, u16 in_size,
342 void *buf_out, u16 *out_size)
344 struct hinic_port_mac_cmd *mac_out = buf_out;
345 struct hinic_port_mac_cmd *mac_in = buf_in;
346 struct hinic_hwdev *hw_dev = hwdev;
347 struct hinic_func_to_io *nic_io;
348 struct vf_data_storage *vf_info;
351 nic_io = &hw_dev->func_to_io;
352 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id);
353 if (vf_info->pf_set_mac && !(vf_info->trust) &&
354 is_valid_ether_addr(mac_in->mac)) {
355 dev_warn(&hw_dev->hwif->pdev->dev, "PF has already set VF %d MAC address\n",
356 HW_VF_ID_TO_OS(vf_id));
357 mac_out->status = HINIC_PF_SET_VF_ALREADY;
358 *out_size = sizeof(*mac_out);
362 err = hinic_port_msg_cmd(hw_dev, HINIC_PORT_CMD_SET_MAC, buf_in,
363 in_size, buf_out, out_size);
364 if ((err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW) || !(*out_size)) {
365 dev_err(&hw_dev->hwif->pdev->dev,
366 "Failed to set VF %d MAC address, err: %d, status: 0x%x, out size: 0x%x\n",
367 HW_VF_ID_TO_OS(vf_id), err, mac_out->status, *out_size);
374 static int hinic_del_vf_mac_msg_handler(void *hwdev, u16 vf_id,
375 void *buf_in, u16 in_size,
376 void *buf_out, u16 *out_size)
378 struct hinic_port_mac_cmd *mac_out = buf_out;
379 struct hinic_port_mac_cmd *mac_in = buf_in;
380 struct hinic_hwdev *hw_dev = hwdev;
381 struct hinic_func_to_io *nic_io;
382 struct vf_data_storage *vf_info;
385 nic_io = &hw_dev->func_to_io;
386 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id);
387 if (vf_info->pf_set_mac && is_valid_ether_addr(mac_in->mac) &&
388 !memcmp(vf_info->vf_mac_addr, mac_in->mac, ETH_ALEN)) {
389 dev_warn(&hw_dev->hwif->pdev->dev, "PF has already set VF mac.\n");
390 mac_out->status = HINIC_PF_SET_VF_ALREADY;
391 *out_size = sizeof(*mac_out);
395 err = hinic_port_msg_cmd(hw_dev, HINIC_PORT_CMD_DEL_MAC, buf_in,
396 in_size, buf_out, out_size);
397 if ((err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW) || !(*out_size)) {
398 dev_err(&hw_dev->hwif->pdev->dev, "Failed to delete VF %d MAC, err: %d, status: 0x%x, out size: 0x%x\n",
399 HW_VF_ID_TO_OS(vf_id), err, mac_out->status, *out_size);
406 static int hinic_get_vf_link_status_msg_handler(void *hwdev, u16 vf_id,
407 void *buf_in, u16 in_size,
408 void *buf_out, u16 *out_size)
410 struct hinic_port_link_cmd *get_link = buf_out;
411 struct hinic_hwdev *hw_dev = hwdev;
412 struct vf_data_storage *vf_infos;
413 struct hinic_func_to_io *nic_io;
414 bool link_forced, link_up;
416 nic_io = &hw_dev->func_to_io;
417 vf_infos = nic_io->vf_infos;
418 link_forced = vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced;
419 link_up = vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up;
422 get_link->state = link_up ?
423 HINIC_LINK_STATE_UP : HINIC_LINK_STATE_DOWN;
425 get_link->state = nic_io->link_status;
427 get_link->status = 0;
428 *out_size = sizeof(*get_link);
433 static bool check_func_table(struct hinic_hwdev *hwdev, u16 func_idx,
434 void *buf_in, u16 in_size)
436 struct hinic_cmd_fw_ctxt *function_table = buf_in;
438 if (!hinic_mbox_check_func_id_8B(hwdev, func_idx, buf_in, in_size) ||
439 !function_table->rx_buf_sz)
445 static struct vf_cmd_msg_handle nic_vf_cmd_msg_handler[] = {
446 {HINIC_PORT_CMD_VF_REGISTER, hinic_register_vf_msg_handler},
447 {HINIC_PORT_CMD_VF_UNREGISTER, hinic_unregister_vf_msg_handler},
448 {HINIC_PORT_CMD_CHANGE_MTU, hinic_change_vf_mtu_msg_handler},
449 {HINIC_PORT_CMD_GET_MAC, hinic_get_vf_mac_msg_handler},
450 {HINIC_PORT_CMD_SET_MAC, hinic_set_vf_mac_msg_handler},
451 {HINIC_PORT_CMD_DEL_MAC, hinic_del_vf_mac_msg_handler},
452 {HINIC_PORT_CMD_GET_LINK_STATE, hinic_get_vf_link_status_msg_handler},
455 static struct vf_cmd_check_handle nic_cmd_support_vf[] = {
456 {HINIC_PORT_CMD_VF_REGISTER, NULL},
457 {HINIC_PORT_CMD_VF_UNREGISTER, NULL},
458 {HINIC_PORT_CMD_CHANGE_MTU, hinic_mbox_check_func_id_8B},
459 {HINIC_PORT_CMD_ADD_VLAN, hinic_mbox_check_func_id_8B},
460 {HINIC_PORT_CMD_DEL_VLAN, hinic_mbox_check_func_id_8B},
461 {HINIC_PORT_CMD_SET_MAC, hinic_mbox_check_func_id_8B},
462 {HINIC_PORT_CMD_GET_MAC, hinic_mbox_check_func_id_8B},
463 {HINIC_PORT_CMD_DEL_MAC, hinic_mbox_check_func_id_8B},
464 {HINIC_PORT_CMD_SET_RX_MODE, hinic_mbox_check_func_id_8B},
465 {HINIC_PORT_CMD_GET_PAUSE_INFO, hinic_mbox_check_func_id_8B},
466 {HINIC_PORT_CMD_GET_LINK_STATE, hinic_mbox_check_func_id_8B},
467 {HINIC_PORT_CMD_SET_LRO, hinic_mbox_check_func_id_8B},
468 {HINIC_PORT_CMD_SET_RX_CSUM, hinic_mbox_check_func_id_8B},
469 {HINIC_PORT_CMD_SET_RX_VLAN_OFFLOAD, hinic_mbox_check_func_id_8B},
470 {HINIC_PORT_CMD_GET_VPORT_STAT, hinic_mbox_check_func_id_8B},
471 {HINIC_PORT_CMD_CLEAN_VPORT_STAT, hinic_mbox_check_func_id_8B},
472 {HINIC_PORT_CMD_GET_RSS_TEMPLATE_INDIR_TBL,
473 hinic_mbox_check_func_id_8B},
474 {HINIC_PORT_CMD_SET_RSS_TEMPLATE_TBL, hinic_mbox_check_func_id_8B},
475 {HINIC_PORT_CMD_GET_RSS_TEMPLATE_TBL, hinic_mbox_check_func_id_8B},
476 {HINIC_PORT_CMD_SET_RSS_HASH_ENGINE, hinic_mbox_check_func_id_8B},
477 {HINIC_PORT_CMD_GET_RSS_HASH_ENGINE, hinic_mbox_check_func_id_8B},
478 {HINIC_PORT_CMD_GET_RSS_CTX_TBL, hinic_mbox_check_func_id_8B},
479 {HINIC_PORT_CMD_SET_RSS_CTX_TBL, hinic_mbox_check_func_id_8B},
480 {HINIC_PORT_CMD_RSS_TEMP_MGR, hinic_mbox_check_func_id_8B},
481 {HINIC_PORT_CMD_RSS_CFG, hinic_mbox_check_func_id_8B},
482 {HINIC_PORT_CMD_FWCTXT_INIT, check_func_table},
483 {HINIC_PORT_CMD_GET_MGMT_VERSION, NULL},
484 {HINIC_PORT_CMD_SET_FUNC_STATE, hinic_mbox_check_func_id_8B},
485 {HINIC_PORT_CMD_GET_GLOBAL_QPN, hinic_mbox_check_func_id_8B},
486 {HINIC_PORT_CMD_SET_TSO, hinic_mbox_check_func_id_8B},
487 {HINIC_PORT_CMD_SET_RQ_IQ_MAP, hinic_mbox_check_func_id_8B},
488 {HINIC_PORT_CMD_LINK_STATUS_REPORT, hinic_mbox_check_func_id_8B},
489 {HINIC_PORT_CMD_UPDATE_MAC, hinic_mbox_check_func_id_8B},
490 {HINIC_PORT_CMD_GET_CAP, hinic_mbox_check_func_id_8B},
491 {HINIC_PORT_CMD_GET_LINK_MODE, hinic_mbox_check_func_id_8B},
494 #define CHECK_IPSU_15BIT 0X8000
497 struct hinic_sriov_info *hinic_get_sriov_info_by_pcidev(struct pci_dev *pdev)
499 struct net_device *netdev = pci_get_drvdata(pdev);
500 struct hinic_dev *nic_dev = netdev_priv(netdev);
502 return &nic_dev->sriov_info;
505 static int hinic_check_mac_info(u8 status, u16 vlan_id)
507 if ((status && status != HINIC_MGMT_STATUS_EXIST) ||
508 (vlan_id & CHECK_IPSU_15BIT &&
509 status == HINIC_MGMT_STATUS_EXIST))
515 #define HINIC_VLAN_ID_MASK 0x7FFF
517 static int hinic_update_mac(struct hinic_hwdev *hwdev, u8 *old_mac,
518 u8 *new_mac, u16 vlan_id, u16 func_id)
520 struct hinic_port_mac_update mac_info = {0};
521 u16 out_size = sizeof(mac_info);
524 if (!hwdev || !old_mac || !new_mac)
527 if ((vlan_id & HINIC_VLAN_ID_MASK) >= VLAN_N_VID) {
528 dev_err(&hwdev->hwif->pdev->dev, "Invalid VLAN number: %d\n",
529 (vlan_id & HINIC_VLAN_ID_MASK));
533 mac_info.func_id = func_id;
534 mac_info.vlan_id = vlan_id;
535 memcpy(mac_info.old_mac, old_mac, ETH_ALEN);
536 memcpy(mac_info.new_mac, new_mac, ETH_ALEN);
538 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_UPDATE_MAC, &mac_info,
539 sizeof(mac_info), &mac_info, &out_size);
541 if (err || !out_size ||
542 hinic_check_mac_info(mac_info.status, mac_info.vlan_id)) {
543 dev_err(&hwdev->hwif->pdev->dev,
544 "Failed to update MAC, err: %d, status: 0x%x, out size: 0x%x\n",
545 err, mac_info.status, out_size);
549 if (mac_info.status == HINIC_MGMT_STATUS_EXIST)
550 dev_warn(&hwdev->hwif->pdev->dev, "MAC is repeated. Ignore update operation\n");
555 static void hinic_get_vf_config(struct hinic_hwdev *hwdev, u16 vf_id,
556 struct ifla_vf_info *ivi)
558 struct vf_data_storage *vfinfo;
560 vfinfo = hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
562 ivi->vf = HW_VF_ID_TO_OS(vf_id);
563 memcpy(ivi->mac, vfinfo->vf_mac_addr, ETH_ALEN);
564 ivi->vlan = vfinfo->pf_vlan;
565 ivi->qos = vfinfo->pf_qos;
566 ivi->spoofchk = vfinfo->spoofchk;
567 ivi->trusted = vfinfo->trust;
568 ivi->max_tx_rate = vfinfo->max_rate;
569 ivi->min_tx_rate = vfinfo->min_rate;
571 if (!vfinfo->link_forced)
572 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
573 else if (vfinfo->link_up)
574 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
576 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
579 int hinic_ndo_get_vf_config(struct net_device *netdev,
580 int vf, struct ifla_vf_info *ivi)
582 struct hinic_dev *nic_dev = netdev_priv(netdev);
583 struct hinic_sriov_info *sriov_info;
585 sriov_info = &nic_dev->sriov_info;
586 if (vf >= sriov_info->num_vfs)
589 hinic_get_vf_config(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), ivi);
594 static int hinic_set_vf_mac(struct hinic_hwdev *hwdev, int vf,
595 unsigned char *mac_addr)
597 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
598 struct vf_data_storage *vf_info;
602 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf);
604 /* duplicate request, so just return success */
605 if (vf_info->pf_set_mac &&
606 !memcmp(vf_info->vf_mac_addr, mac_addr, ETH_ALEN))
609 vf_info->pf_set_mac = true;
611 func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf;
612 err = hinic_update_mac(hwdev, vf_info->vf_mac_addr,
613 mac_addr, 0, func_id);
615 vf_info->pf_set_mac = false;
619 memcpy(vf_info->vf_mac_addr, mac_addr, ETH_ALEN);
624 int hinic_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
626 struct hinic_dev *nic_dev = netdev_priv(netdev);
627 struct hinic_sriov_info *sriov_info;
630 sriov_info = &nic_dev->sriov_info;
631 if (!is_valid_ether_addr(mac) || vf >= sriov_info->num_vfs)
634 err = hinic_set_vf_mac(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), mac);
638 netif_info(nic_dev, drv, netdev, "Setting MAC %pM on VF %d\n", mac, vf);
639 netif_info(nic_dev, drv, netdev, "Reload the VF driver to make this change effective.");
644 static int hinic_add_vf_vlan(struct hinic_hwdev *hwdev, int vf_id,
647 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
650 err = hinic_set_vf_vlan(hwdev, true, vlan, qos, vf_id);
654 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan = vlan;
655 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos = qos;
657 dev_info(&hwdev->hwif->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
658 vlan, qos, HW_VF_ID_TO_OS(vf_id));
662 static int hinic_kill_vf_vlan(struct hinic_hwdev *hwdev, int vf_id)
664 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
667 err = hinic_set_vf_vlan(hwdev, false,
668 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan,
669 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos,
674 dev_info(&hwdev->hwif->pdev->dev, "Remove VLAN %d on VF %d\n",
675 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan,
676 HW_VF_ID_TO_OS(vf_id));
678 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan = 0;
679 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos = 0;
684 static int hinic_update_mac_vlan(struct hinic_dev *nic_dev, u16 old_vlan,
685 u16 new_vlan, int vf_id)
687 struct vf_data_storage *vf_info;
691 if (!nic_dev || old_vlan >= VLAN_N_VID || new_vlan >= VLAN_N_VID)
694 vf_info = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
695 if (!vf_info->pf_set_mac)
700 vlan_id |= HINIC_ADD_VLAN_IN_MAC;
702 err = hinic_port_del_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
704 dev_err(&nic_dev->hwdev->hwif->pdev->dev, "Failed to delete VF %d MAC %pM vlan %d\n",
705 HW_VF_ID_TO_OS(vf_id), vf_info->vf_mac_addr, old_vlan);
711 vlan_id |= HINIC_ADD_VLAN_IN_MAC;
713 err = hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
715 dev_err(&nic_dev->hwdev->hwif->pdev->dev, "Failed to add VF %d MAC %pM vlan %d\n",
716 HW_VF_ID_TO_OS(vf_id), vf_info->vf_mac_addr, new_vlan);
725 vlan_id |= HINIC_ADD_VLAN_IN_MAC;
726 hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
731 static int set_hw_vf_vlan(struct hinic_dev *nic_dev,
732 u16 cur_vlanprio, int vf, u16 vlan, u8 qos)
734 u16 old_vlan = cur_vlanprio & VLAN_VID_MASK;
739 err = hinic_kill_vf_vlan(nic_dev->hwdev,
742 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d old vlan %d\n",
747 err = hinic_add_vf_vlan(nic_dev->hwdev,
748 OS_VF_ID_TO_HW(vf), vlan, qos);
750 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to add vf %d new vlan %d\n",
755 err = hinic_kill_vf_vlan(nic_dev->hwdev, OS_VF_ID_TO_HW(vf));
757 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d vlan %d\n",
763 err = hinic_update_mac_vlan(nic_dev, old_vlan, vlan,
770 int hinic_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos,
773 struct hinic_dev *nic_dev = netdev_priv(netdev);
774 struct hinic_sriov_info *sriov_info;
775 u16 vlanprio, cur_vlanprio;
777 sriov_info = &nic_dev->sriov_info;
778 if (vf >= sriov_info->num_vfs || vlan >= VLAN_N_VID || qos > HINIC_MAX_QOS)
780 if (vlan_proto != htons(ETH_P_8021Q))
781 return -EPROTONOSUPPORT;
782 vlanprio = vlan | qos << HINIC_VLAN_PRIORITY_SHIFT;
783 cur_vlanprio = hinic_vf_info_vlanprio(nic_dev->hwdev,
785 /* duplicate request, so just return success */
786 if (vlanprio == cur_vlanprio)
789 return set_hw_vf_vlan(nic_dev, cur_vlanprio, vf, vlan, qos);
792 static int hinic_set_vf_trust(struct hinic_hwdev *hwdev, u16 vf_id,
795 struct vf_data_storage *vf_infos;
796 struct hinic_func_to_io *nic_io;
801 nic_io = &hwdev->func_to_io;
802 vf_infos = nic_io->vf_infos;
803 vf_infos[vf_id].trust = trust;
808 int hinic_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting)
810 struct hinic_dev *adapter = netdev_priv(netdev);
811 struct hinic_sriov_info *sriov_info;
812 struct hinic_func_to_io *nic_io;
816 sriov_info = &adapter->sriov_info;
817 nic_io = &adapter->hwdev->func_to_io;
819 if (vf >= sriov_info->num_vfs)
822 cur_trust = nic_io->vf_infos[vf].trust;
823 /* same request, so just return success */
824 if (setting == cur_trust)
827 err = hinic_set_vf_trust(adapter->hwdev, vf, setting);
829 dev_info(&sriov_info->pdev->dev, "Set VF %d trusted %s succeed\n",
830 vf, setting ? "on" : "off");
832 dev_err(&sriov_info->pdev->dev, "Failed set VF %d trusted %s\n",
833 vf, setting ? "on" : "off");
838 int hinic_ndo_set_vf_bw(struct net_device *netdev,
839 int vf, int min_tx_rate, int max_tx_rate)
841 static const u32 speeds[] = {
842 SPEED_10, SPEED_100, SPEED_1000, SPEED_10000,
843 SPEED_25000, SPEED_40000, SPEED_100000
845 struct hinic_dev *nic_dev = netdev_priv(netdev);
846 struct hinic_port_cap port_cap = { 0 };
847 enum hinic_port_link_state link_state;
850 if (vf >= nic_dev->sriov_info.num_vfs) {
851 netif_err(nic_dev, drv, netdev, "VF number must be less than %d\n",
852 nic_dev->sriov_info.num_vfs);
856 err = hinic_port_link_state(nic_dev, &link_state);
858 netif_err(nic_dev, drv, netdev,
859 "Get link status failed when setting vf tx rate\n");
863 if (link_state == HINIC_LINK_STATE_DOWN) {
864 netif_err(nic_dev, drv, netdev,
865 "Link status must be up when setting vf tx rate\n");
869 err = hinic_port_get_cap(nic_dev, &port_cap);
870 if (err || port_cap.speed > LINK_SPEED_100GB)
873 /* rate limit cannot be less than 0 and greater than link speed */
874 if (max_tx_rate < 0 || max_tx_rate > speeds[port_cap.speed]) {
875 netif_err(nic_dev, drv, netdev, "Max tx rate must be in [0 - %d]\n",
876 speeds[port_cap.speed]);
880 err = hinic_set_vf_tx_rate(nic_dev->hwdev, OS_VF_ID_TO_HW(vf),
881 max_tx_rate, min_tx_rate);
883 netif_err(nic_dev, drv, netdev,
884 "Unable to set VF %d max rate %d min rate %d%s\n",
885 vf, max_tx_rate, min_tx_rate,
886 err == HINIC_TX_RATE_TABLE_FULL ?
887 ", tx rate profile is full" : "");
891 netif_info(nic_dev, drv, netdev,
892 "Set VF %d max tx rate %d min tx rate %d successfully\n",
893 vf, max_tx_rate, min_tx_rate);
898 static int hinic_set_vf_spoofchk(struct hinic_hwdev *hwdev, u16 vf_id,
901 struct hinic_spoofchk_set spoofchk_cfg = {0};
902 struct vf_data_storage *vf_infos = NULL;
903 u16 out_size = sizeof(spoofchk_cfg);
909 vf_infos = hwdev->func_to_io.vf_infos;
911 spoofchk_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
912 spoofchk_cfg.state = spoofchk ? 1 : 0;
913 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_ENABLE_SPOOFCHK,
914 &spoofchk_cfg, sizeof(spoofchk_cfg),
915 &spoofchk_cfg, &out_size);
916 if (spoofchk_cfg.status == HINIC_MGMT_CMD_UNSUPPORTED) {
917 err = HINIC_MGMT_CMD_UNSUPPORTED;
918 } else if (err || !out_size || spoofchk_cfg.status) {
919 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) spoofchk, err: %d, status: 0x%x, out size: 0x%x\n",
920 HW_VF_ID_TO_OS(vf_id), err, spoofchk_cfg.status,
925 vf_infos[HW_VF_ID_TO_OS(vf_id)].spoofchk = spoofchk;
930 int hinic_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
932 struct hinic_dev *nic_dev = netdev_priv(netdev);
933 struct hinic_sriov_info *sriov_info;
937 sriov_info = &nic_dev->sriov_info;
938 if (vf >= sriov_info->num_vfs)
941 cur_spoofchk = nic_dev->hwdev->func_to_io.vf_infos[vf].spoofchk;
943 /* same request, so just return success */
944 if (setting == cur_spoofchk)
947 err = hinic_set_vf_spoofchk(sriov_info->hwdev,
948 OS_VF_ID_TO_HW(vf), setting);
950 netif_info(nic_dev, drv, netdev, "Set VF %d spoofchk %s successfully\n",
951 vf, setting ? "on" : "off");
952 } else if (err == HINIC_MGMT_CMD_UNSUPPORTED) {
953 netif_err(nic_dev, drv, netdev,
954 "Current firmware doesn't support to set vf spoofchk, need to upgrade latest firmware version\n");
961 static int hinic_set_vf_link_state(struct hinic_hwdev *hwdev, u16 vf_id,
964 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
965 struct vf_data_storage *vf_infos = nic_io->vf_infos;
969 case HINIC_IFLA_VF_LINK_STATE_AUTO:
970 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = false;
971 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = nic_io->link_status ?
973 link_status = nic_io->link_status;
975 case HINIC_IFLA_VF_LINK_STATE_ENABLE:
976 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
977 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = true;
978 link_status = HINIC_LINK_UP;
980 case HINIC_IFLA_VF_LINK_STATE_DISABLE:
981 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
982 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = false;
983 link_status = HINIC_LINK_DOWN;
989 /* Notify the VF of its new link state */
990 hinic_notify_vf_link_status(hwdev, vf_id, link_status);
995 int hinic_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
997 struct hinic_dev *nic_dev = netdev_priv(netdev);
998 struct hinic_sriov_info *sriov_info;
1000 sriov_info = &nic_dev->sriov_info;
1002 if (vf_id >= sriov_info->num_vfs) {
1003 netif_err(nic_dev, drv, netdev,
1004 "Invalid VF Identifier %d\n", vf_id);
1008 return hinic_set_vf_link_state(sriov_info->hwdev,
1009 OS_VF_ID_TO_HW(vf_id), link);
1012 /* pf receive message from vf */
1013 static int nic_pf_mbox_handler(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1014 u16 in_size, void *buf_out, u16 *out_size)
1016 u8 size = ARRAY_SIZE(nic_cmd_support_vf);
1017 struct vf_cmd_msg_handle *vf_msg_handle;
1018 struct hinic_hwdev *dev = hwdev;
1019 struct hinic_func_to_io *nic_io;
1020 struct hinic_pfhwdev *pfhwdev;
1027 if (!hinic_mbox_check_cmd_valid(hwdev, nic_cmd_support_vf, vf_id, cmd,
1028 buf_in, in_size, size)) {
1029 dev_err(&dev->hwif->pdev->dev,
1030 "PF Receive VF nic cmd: 0x%x, mbox len: 0x%x is invalid\n",
1032 return HINIC_MBOX_VF_CMD_ERROR;
1035 pfhwdev = container_of(dev, struct hinic_pfhwdev, hwdev);
1036 nic_io = &dev->func_to_io;
1037 for (i = 0; i < ARRAY_SIZE(nic_vf_cmd_msg_handler); i++) {
1038 vf_msg_handle = &nic_vf_cmd_msg_handler[i];
1039 if (cmd == vf_msg_handle->cmd &&
1040 vf_msg_handle->cmd_msg_handler) {
1041 err = vf_msg_handle->cmd_msg_handler(hwdev, vf_id,
1048 if (i == ARRAY_SIZE(nic_vf_cmd_msg_handler))
1049 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC,
1050 cmd, buf_in, in_size, buf_out,
1051 out_size, HINIC_MGMT_MSG_SYNC);
1053 if (err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW)
1054 dev_err(&nic_io->hwif->pdev->dev, "PF receive VF L2NIC cmd: %d process error, err:%d\n",
1059 static int cfg_mbx_pf_proc_vf_msg(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1060 u16 in_size, void *buf_out, u16 *out_size)
1062 struct hinic_dev_cap *dev_cap = buf_out;
1063 struct hinic_hwdev *dev = hwdev;
1064 struct hinic_cap *cap;
1066 cap = &dev->nic_cap;
1067 memset(dev_cap, 0, sizeof(*dev_cap));
1069 dev_cap->max_vf = cap->max_vf;
1070 dev_cap->max_sqs = cap->max_vf_qps;
1071 dev_cap->max_rqs = cap->max_vf_qps;
1072 dev_cap->port_id = dev->port_id;
1074 *out_size = sizeof(*dev_cap);
1079 static int hinic_init_vf_infos(struct hinic_func_to_io *nic_io, u16 vf_id)
1081 struct vf_data_storage *vf_infos = nic_io->vf_infos;
1083 if (set_vf_link_state > HINIC_IFLA_VF_LINK_STATE_DISABLE) {
1084 dev_warn(&nic_io->hwif->pdev->dev, "Module Parameter set_vf_link_state value %d is out of range, resetting to %d\n",
1085 set_vf_link_state, HINIC_IFLA_VF_LINK_STATE_AUTO);
1086 set_vf_link_state = HINIC_IFLA_VF_LINK_STATE_AUTO;
1089 switch (set_vf_link_state) {
1090 case HINIC_IFLA_VF_LINK_STATE_AUTO:
1091 vf_infos[vf_id].link_forced = false;
1093 case HINIC_IFLA_VF_LINK_STATE_ENABLE:
1094 vf_infos[vf_id].link_forced = true;
1095 vf_infos[vf_id].link_up = true;
1097 case HINIC_IFLA_VF_LINK_STATE_DISABLE:
1098 vf_infos[vf_id].link_forced = true;
1099 vf_infos[vf_id].link_up = false;
1102 dev_err(&nic_io->hwif->pdev->dev, "Invalid input parameter set_vf_link_state: %d\n",
1110 static void hinic_clear_vf_infos(struct hinic_dev *nic_dev, u16 vf_id)
1112 struct vf_data_storage *vf_infos;
1114 vf_infos = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
1115 if (vf_infos->pf_set_mac)
1116 hinic_port_del_mac(nic_dev, vf_infos->vf_mac_addr, 0);
1118 if (hinic_vf_info_vlanprio(nic_dev->hwdev, vf_id))
1119 hinic_kill_vf_vlan(nic_dev->hwdev, vf_id);
1121 if (vf_infos->max_rate)
1122 hinic_set_vf_tx_rate(nic_dev->hwdev, vf_id, 0, 0);
1124 if (vf_infos->spoofchk)
1125 hinic_set_vf_spoofchk(nic_dev->hwdev, vf_id, false);
1127 if (vf_infos->trust)
1128 hinic_set_vf_trust(nic_dev->hwdev, vf_id, false);
1130 memset(vf_infos, 0, sizeof(*vf_infos));
1131 /* set vf_infos to default */
1132 hinic_init_vf_infos(&nic_dev->hwdev->func_to_io, HW_VF_ID_TO_OS(vf_id));
1135 static void hinic_deinit_vf_hw(struct hinic_sriov_info *sriov_info,
1136 u16 start_vf_id, u16 end_vf_id)
1138 struct hinic_dev *nic_dev;
1141 nic_dev = container_of(sriov_info, struct hinic_dev, sriov_info);
1143 for (idx = start_vf_id; idx <= end_vf_id; idx++) {
1144 func_idx = hinic_glb_pf_vf_offset(nic_dev->hwdev->hwif) + idx;
1145 hinic_set_wq_page_size(nic_dev->hwdev, func_idx,
1146 HINIC_HW_WQ_PAGE_SIZE);
1147 hinic_clear_vf_infos(nic_dev, idx);
1151 int hinic_vf_func_init(struct hinic_hwdev *hwdev)
1153 struct hinic_register_vf register_info = {0};
1154 u16 out_size = sizeof(register_info);
1155 struct hinic_func_to_io *nic_io;
1159 err = hinic_vf_mbox_random_id_init(hwdev);
1161 dev_err(&hwdev->hwif->pdev->dev, "Failed to init vf mbox random id, err: %d\n",
1166 nic_io = &hwdev->func_to_io;
1168 if (HINIC_IS_VF(hwdev->hwif)) {
1169 err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1170 HINIC_PORT_CMD_VF_REGISTER,
1171 ®ister_info, sizeof(register_info),
1172 ®ister_info, &out_size, 0);
1173 if (err || register_info.status || !out_size) {
1174 dev_err(&hwdev->hwif->pdev->dev,
1175 "Failed to register VF, err: %d, status: 0x%x, out size: 0x%x\n",
1176 err, register_info.status, out_size);
1177 hinic_unregister_vf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1181 err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_CFGM,
1182 cfg_mbx_pf_proc_vf_msg);
1184 dev_err(&hwdev->hwif->pdev->dev,
1185 "Register PF mailbox callback failed\n");
1188 nic_io->max_vfs = hwdev->nic_cap.max_vf;
1189 size = sizeof(*nic_io->vf_infos) * nic_io->max_vfs;
1191 nic_io->vf_infos = kzalloc(size, GFP_KERNEL);
1192 if (!nic_io->vf_infos) {
1194 goto out_free_nic_io;
1197 for (i = 0; i < nic_io->max_vfs; i++) {
1198 err = hinic_init_vf_infos(nic_io, i);
1200 goto err_init_vf_infos;
1203 err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC,
1204 nic_pf_mbox_handler);
1206 goto err_register_pf_mbox_cb;
1212 err_register_pf_mbox_cb:
1214 kfree(nic_io->vf_infos);
1219 void hinic_vf_func_free(struct hinic_hwdev *hwdev)
1221 struct hinic_register_vf unregister = {0};
1222 u16 out_size = sizeof(unregister);
1225 if (HINIC_IS_VF(hwdev->hwif)) {
1226 err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1227 HINIC_PORT_CMD_VF_UNREGISTER,
1228 &unregister, sizeof(unregister),
1229 &unregister, &out_size, 0);
1230 if (err || !out_size || unregister.status)
1231 dev_err(&hwdev->hwif->pdev->dev, "Failed to unregister VF, err: %d, status: 0x%x, out_size: 0x%x\n",
1232 err, unregister.status, out_size);
1234 if (hwdev->func_to_io.vf_infos) {
1235 hinic_unregister_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1236 kfree(hwdev->func_to_io.vf_infos);
1241 static int hinic_init_vf_hw(struct hinic_hwdev *hwdev, u16 start_vf_id,
1247 /* vf use 256K as default wq page size, and can't change it */
1248 for (i = start_vf_id; i <= end_vf_id; i++) {
1249 func_idx = hinic_glb_pf_vf_offset(hwdev->hwif) + i;
1250 err = hinic_set_wq_page_size(hwdev, func_idx,
1251 HINIC_DEFAULT_WQ_PAGE_SIZE);
1259 int hinic_pci_sriov_disable(struct pci_dev *pdev)
1261 struct hinic_sriov_info *sriov_info;
1264 sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1265 /* if SR-IOV is already disabled then nothing will be done */
1266 if (!sriov_info->sriov_enabled)
1269 set_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1271 /* If our VFs are assigned we cannot shut down SR-IOV
1272 * without causing issues, so just leave the hardware
1273 * available but disabled
1275 if (pci_vfs_assigned(sriov_info->pdev)) {
1276 clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1277 dev_warn(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
1280 sriov_info->sriov_enabled = false;
1282 /* disable iov and allow time for transactions to clear */
1283 pci_disable_sriov(sriov_info->pdev);
1285 tmp_vfs = (u16)sriov_info->num_vfs;
1286 sriov_info->num_vfs = 0;
1287 hinic_deinit_vf_hw(sriov_info, OS_VF_ID_TO_HW(0),
1288 OS_VF_ID_TO_HW(tmp_vfs - 1));
1290 clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1295 static int hinic_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1297 struct hinic_sriov_info *sriov_info;
1300 sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1302 if (test_and_set_bit(HINIC_SRIOV_ENABLE, &sriov_info->state)) {
1304 "SR-IOV enable in process, please wait, num_vfs %d\n",
1309 err = hinic_init_vf_hw(sriov_info->hwdev, OS_VF_ID_TO_HW(0),
1310 OS_VF_ID_TO_HW((u16)num_vfs - 1));
1312 dev_err(&sriov_info->pdev->dev,
1313 "Failed to init vf in hardware before enable sriov, error %d\n",
1315 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1319 err = pci_enable_sriov(sriov_info->pdev, num_vfs);
1322 "Failed to enable SR-IOV, error %d\n", err);
1323 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1327 sriov_info->sriov_enabled = true;
1328 sriov_info->num_vfs = num_vfs;
1329 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1334 int hinic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1336 struct hinic_sriov_info *sriov_info;
1338 sriov_info = hinic_get_sriov_info_by_pcidev(dev);
1340 if (test_bit(HINIC_FUNC_REMOVE, &sriov_info->state))
1344 return hinic_pci_sriov_disable(dev);
1346 return hinic_pci_sriov_enable(dev, num_vfs);