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>
12 #include "hinic_hw_dev.h"
13 #include "hinic_dev.h"
14 #include "hinic_hw_mbox.h"
15 #include "hinic_hw_cmdq.h"
16 #include "hinic_port.h"
17 #include "hinic_sriov.h"
19 static unsigned char set_vf_link_state
;
20 module_param(set_vf_link_state
, byte
, 0444);
21 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.");
23 #define HINIC_VLAN_PRIORITY_SHIFT 13
24 #define HINIC_ADD_VLAN_IN_MAC 0x8000
25 #define HINIC_TX_RATE_TABLE_FULL 12
27 static int hinic_set_mac(struct hinic_hwdev
*hwdev
, const u8
*mac_addr
,
28 u16 vlan_id
, u16 func_id
)
30 struct hinic_port_mac_cmd mac_info
= {0};
31 u16 out_size
= sizeof(mac_info
);
34 mac_info
.func_idx
= func_id
;
35 mac_info
.vlan_id
= vlan_id
;
36 memcpy(mac_info
.mac
, mac_addr
, ETH_ALEN
);
38 err
= hinic_port_msg_cmd(hwdev
, HINIC_PORT_CMD_SET_MAC
, &mac_info
,
39 sizeof(mac_info
), &mac_info
, &out_size
);
40 if (err
|| out_size
!= sizeof(mac_info
) ||
41 (mac_info
.status
&& mac_info
.status
!= HINIC_MGMT_STATUS_EXIST
)) {
42 dev_err(&hwdev
->func_to_io
.hwif
->pdev
->dev
, "Failed to set MAC, err: %d, status: 0x%x, out size: 0x%x\n",
43 err
, mac_info
.status
, out_size
);
50 static void hinic_notify_vf_link_status(struct hinic_hwdev
*hwdev
, u16 vf_id
,
53 struct vf_data_storage
*vf_infos
= hwdev
->func_to_io
.vf_infos
;
54 struct hinic_port_link_status link
= {0};
55 u16 out_size
= sizeof(link
);
58 if (vf_infos
[HW_VF_ID_TO_OS(vf_id
)].registered
) {
59 link
.link
= link_status
;
60 link
.func_id
= hinic_glb_pf_vf_offset(hwdev
->hwif
) + vf_id
;
61 err
= hinic_mbox_to_vf(hwdev
, HINIC_MOD_L2NIC
,
62 vf_id
, HINIC_PORT_CMD_LINK_STATUS_REPORT
,
65 if (err
|| !out_size
|| link
.status
)
66 dev_err(&hwdev
->hwif
->pdev
->dev
,
67 "Send link change event to VF %d failed, err: %d, status: 0x%x, out_size: 0x%x\n",
68 HW_VF_ID_TO_OS(vf_id
), err
,
69 link
.status
, out_size
);
73 /* send link change event mbox msg to active vfs under the pf */
74 void hinic_notify_all_vfs_link_changed(struct hinic_hwdev
*hwdev
,
77 struct hinic_func_to_io
*nic_io
= &hwdev
->func_to_io
;
80 nic_io
->link_status
= link_status
;
81 for (i
= 1; i
<= nic_io
->max_vfs
; i
++) {
82 if (!nic_io
->vf_infos
[HW_VF_ID_TO_OS(i
)].link_forced
)
83 hinic_notify_vf_link_status(hwdev
, i
, link_status
);
87 static u16
hinic_vf_info_vlanprio(struct hinic_hwdev
*hwdev
, int vf_id
)
89 struct hinic_func_to_io
*nic_io
= &hwdev
->func_to_io
;
90 u16 pf_vlan
, vlanprio
;
93 pf_vlan
= nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].pf_vlan
;
94 pf_qos
= nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].pf_qos
;
95 vlanprio
= pf_vlan
| pf_qos
<< HINIC_VLAN_PRIORITY_SHIFT
;
100 static int hinic_set_vf_vlan(struct hinic_hwdev
*hwdev
, bool add
, u16 vid
,
103 struct hinic_vf_vlan_config vf_vlan
= {0};
104 u16 out_size
= sizeof(vf_vlan
);
108 /* VLAN 0 is a special case, don't allow it to be removed */
112 vf_vlan
.func_id
= hinic_glb_pf_vf_offset(hwdev
->hwif
) + vf_id
;
113 vf_vlan
.vlan_id
= vid
;
117 cmd
= HINIC_PORT_CMD_SET_VF_VLAN
;
119 cmd
= HINIC_PORT_CMD_CLR_VF_VLAN
;
121 err
= hinic_port_msg_cmd(hwdev
, cmd
, &vf_vlan
,
122 sizeof(vf_vlan
), &vf_vlan
, &out_size
);
123 if (err
|| !out_size
|| vf_vlan
.status
) {
124 dev_err(&hwdev
->hwif
->pdev
->dev
, "Failed to set VF %d vlan, err: %d, status: 0x%x, out size: 0x%x\n",
125 HW_VF_ID_TO_OS(vf_id
), err
, vf_vlan
.status
, out_size
);
132 static int hinic_set_vf_tx_rate_max_min(struct hinic_hwdev
*hwdev
, u16 vf_id
,
133 u32 max_rate
, u32 min_rate
)
135 struct hinic_func_to_io
*nic_io
= &hwdev
->func_to_io
;
136 struct hinic_tx_rate_cfg_max_min rate_cfg
= {0};
137 u16 out_size
= sizeof(rate_cfg
);
140 rate_cfg
.func_id
= hinic_glb_pf_vf_offset(hwdev
->hwif
) + vf_id
;
141 rate_cfg
.max_rate
= max_rate
;
142 rate_cfg
.min_rate
= min_rate
;
143 err
= hinic_port_msg_cmd(hwdev
, HINIC_PORT_CMD_SET_VF_MAX_MIN_RATE
,
144 &rate_cfg
, sizeof(rate_cfg
), &rate_cfg
,
146 if ((rate_cfg
.status
!= HINIC_MGMT_CMD_UNSUPPORTED
&&
147 rate_cfg
.status
) || err
|| !out_size
) {
148 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",
149 HW_VF_ID_TO_OS(vf_id
), max_rate
, min_rate
, err
,
150 rate_cfg
.status
, out_size
);
154 if (!rate_cfg
.status
) {
155 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].max_rate
= max_rate
;
156 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].min_rate
= min_rate
;
159 return rate_cfg
.status
;
162 static int hinic_set_vf_rate_limit(struct hinic_hwdev
*hwdev
, u16 vf_id
,
165 struct hinic_func_to_io
*nic_io
= &hwdev
->func_to_io
;
166 struct hinic_tx_rate_cfg rate_cfg
= {0};
167 u16 out_size
= sizeof(rate_cfg
);
170 rate_cfg
.func_id
= hinic_glb_pf_vf_offset(hwdev
->hwif
) + vf_id
;
171 rate_cfg
.tx_rate
= tx_rate
;
172 err
= hinic_port_msg_cmd(hwdev
, HINIC_PORT_CMD_SET_VF_RATE
,
173 &rate_cfg
, sizeof(rate_cfg
), &rate_cfg
,
175 if (err
|| !out_size
|| rate_cfg
.status
) {
176 dev_err(&hwdev
->hwif
->pdev
->dev
, "Failed to set VF(%d) rate(%d), err: %d, status: 0x%x, out size: 0x%x\n",
177 HW_VF_ID_TO_OS(vf_id
), tx_rate
, err
, rate_cfg
.status
,
180 return rate_cfg
.status
;
185 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].max_rate
= tx_rate
;
186 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].min_rate
= 0;
191 static int hinic_set_vf_tx_rate(struct hinic_hwdev
*hwdev
, u16 vf_id
,
192 u32 max_rate
, u32 min_rate
)
196 err
= hinic_set_vf_tx_rate_max_min(hwdev
, vf_id
, max_rate
, min_rate
);
197 if (err
!= HINIC_MGMT_CMD_UNSUPPORTED
)
201 dev_err(&hwdev
->hwif
->pdev
->dev
, "Current firmware doesn't support to set min tx rate\n");
205 dev_info(&hwdev
->hwif
->pdev
->dev
, "Current firmware doesn't support to set min tx rate, force min_tx_rate = max_tx_rate\n");
207 return hinic_set_vf_rate_limit(hwdev
, vf_id
, max_rate
);
210 static int hinic_init_vf_config(struct hinic_hwdev
*hwdev
, u16 vf_id
)
212 struct vf_data_storage
*vf_info
;
213 u16 func_id
, vlan_id
;
216 vf_info
= hwdev
->func_to_io
.vf_infos
+ HW_VF_ID_TO_OS(vf_id
);
217 if (vf_info
->pf_set_mac
) {
218 func_id
= hinic_glb_pf_vf_offset(hwdev
->hwif
) + vf_id
;
222 err
= hinic_set_mac(hwdev
, vf_info
->vf_mac_addr
, vlan_id
,
225 dev_err(&hwdev
->func_to_io
.hwif
->pdev
->dev
, "Failed to set VF %d MAC\n",
226 HW_VF_ID_TO_OS(vf_id
));
231 if (hinic_vf_info_vlanprio(hwdev
, vf_id
)) {
232 err
= hinic_set_vf_vlan(hwdev
, true, vf_info
->pf_vlan
,
233 vf_info
->pf_qos
, vf_id
);
235 dev_err(&hwdev
->hwif
->pdev
->dev
, "Failed to add VF %d VLAN_QOS\n",
236 HW_VF_ID_TO_OS(vf_id
));
241 if (vf_info
->max_rate
) {
242 err
= hinic_set_vf_tx_rate(hwdev
, vf_id
, vf_info
->max_rate
,
245 dev_err(&hwdev
->hwif
->pdev
->dev
, "Failed to set VF %d max rate: %d, min rate: %d\n",
246 HW_VF_ID_TO_OS(vf_id
), vf_info
->max_rate
,
255 static int hinic_register_vf_msg_handler(void *hwdev
, u16 vf_id
,
256 void *buf_in
, u16 in_size
,
257 void *buf_out
, u16
*out_size
)
259 struct hinic_register_vf
*register_info
= buf_out
;
260 struct hinic_hwdev
*hw_dev
= hwdev
;
261 struct hinic_func_to_io
*nic_io
;
264 nic_io
= &hw_dev
->func_to_io
;
265 if (vf_id
> nic_io
->max_vfs
) {
266 dev_err(&hw_dev
->hwif
->pdev
->dev
, "Register VF id %d exceed limit[0-%d]\n",
267 HW_VF_ID_TO_OS(vf_id
), HW_VF_ID_TO_OS(nic_io
->max_vfs
));
268 register_info
->status
= EFAULT
;
272 *out_size
= sizeof(*register_info
);
273 err
= hinic_init_vf_config(hw_dev
, vf_id
);
275 register_info
->status
= EFAULT
;
279 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].registered
= true;
284 static int hinic_unregister_vf_msg_handler(void *hwdev
, u16 vf_id
,
285 void *buf_in
, u16 in_size
,
286 void *buf_out
, u16
*out_size
)
288 struct hinic_hwdev
*hw_dev
= hwdev
;
289 struct hinic_func_to_io
*nic_io
;
291 nic_io
= &hw_dev
->func_to_io
;
293 if (vf_id
> nic_io
->max_vfs
)
296 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].registered
= false;
301 static int hinic_change_vf_mtu_msg_handler(void *hwdev
, u16 vf_id
,
302 void *buf_in
, u16 in_size
,
303 void *buf_out
, u16
*out_size
)
305 struct hinic_hwdev
*hw_dev
= hwdev
;
308 err
= hinic_port_msg_cmd(hwdev
, HINIC_PORT_CMD_CHANGE_MTU
, buf_in
,
309 in_size
, buf_out
, out_size
);
311 dev_err(&hw_dev
->hwif
->pdev
->dev
, "Failed to set VF %u mtu\n",
319 static int hinic_get_vf_mac_msg_handler(void *hwdev
, u16 vf_id
,
320 void *buf_in
, u16 in_size
,
321 void *buf_out
, u16
*out_size
)
323 struct hinic_port_mac_cmd
*mac_info
= buf_out
;
324 struct hinic_hwdev
*dev
= hwdev
;
325 struct hinic_func_to_io
*nic_io
;
326 struct vf_data_storage
*vf_info
;
328 nic_io
= &dev
->func_to_io
;
329 vf_info
= nic_io
->vf_infos
+ HW_VF_ID_TO_OS(vf_id
);
331 memcpy(mac_info
->mac
, vf_info
->vf_mac_addr
, ETH_ALEN
);
332 mac_info
->status
= 0;
333 *out_size
= sizeof(*mac_info
);
338 static int hinic_set_vf_mac_msg_handler(void *hwdev
, u16 vf_id
,
339 void *buf_in
, u16 in_size
,
340 void *buf_out
, u16
*out_size
)
342 struct hinic_port_mac_cmd
*mac_out
= buf_out
;
343 struct hinic_port_mac_cmd
*mac_in
= buf_in
;
344 struct hinic_hwdev
*hw_dev
= hwdev
;
345 struct hinic_func_to_io
*nic_io
;
346 struct vf_data_storage
*vf_info
;
349 nic_io
= &hw_dev
->func_to_io
;
350 vf_info
= nic_io
->vf_infos
+ HW_VF_ID_TO_OS(vf_id
);
351 if (vf_info
->pf_set_mac
&& !(vf_info
->trust
) &&
352 is_valid_ether_addr(mac_in
->mac
)) {
353 dev_warn(&hw_dev
->hwif
->pdev
->dev
, "PF has already set VF %d MAC address\n",
354 HW_VF_ID_TO_OS(vf_id
));
355 mac_out
->status
= HINIC_PF_SET_VF_ALREADY
;
356 *out_size
= sizeof(*mac_out
);
360 err
= hinic_port_msg_cmd(hw_dev
, HINIC_PORT_CMD_SET_MAC
, buf_in
,
361 in_size
, buf_out
, out_size
);
362 if ((err
&& err
!= HINIC_MBOX_PF_BUSY_ACTIVE_FW
) || !(*out_size
)) {
363 dev_err(&hw_dev
->hwif
->pdev
->dev
,
364 "Failed to set VF %d MAC address, err: %d, status: 0x%x, out size: 0x%x\n",
365 HW_VF_ID_TO_OS(vf_id
), err
, mac_out
->status
, *out_size
);
372 static int hinic_del_vf_mac_msg_handler(void *hwdev
, u16 vf_id
,
373 void *buf_in
, u16 in_size
,
374 void *buf_out
, u16
*out_size
)
376 struct hinic_port_mac_cmd
*mac_out
= buf_out
;
377 struct hinic_port_mac_cmd
*mac_in
= buf_in
;
378 struct hinic_hwdev
*hw_dev
= hwdev
;
379 struct hinic_func_to_io
*nic_io
;
380 struct vf_data_storage
*vf_info
;
383 nic_io
= &hw_dev
->func_to_io
;
384 vf_info
= nic_io
->vf_infos
+ HW_VF_ID_TO_OS(vf_id
);
385 if (vf_info
->pf_set_mac
&& is_valid_ether_addr(mac_in
->mac
) &&
386 !memcmp(vf_info
->vf_mac_addr
, mac_in
->mac
, ETH_ALEN
)) {
387 dev_warn(&hw_dev
->hwif
->pdev
->dev
, "PF has already set VF mac.\n");
388 mac_out
->status
= HINIC_PF_SET_VF_ALREADY
;
389 *out_size
= sizeof(*mac_out
);
393 err
= hinic_port_msg_cmd(hw_dev
, HINIC_PORT_CMD_DEL_MAC
, buf_in
,
394 in_size
, buf_out
, out_size
);
395 if ((err
&& err
!= HINIC_MBOX_PF_BUSY_ACTIVE_FW
) || !(*out_size
)) {
396 dev_err(&hw_dev
->hwif
->pdev
->dev
, "Failed to delete VF %d MAC, err: %d, status: 0x%x, out size: 0x%x\n",
397 HW_VF_ID_TO_OS(vf_id
), err
, mac_out
->status
, *out_size
);
404 static int hinic_get_vf_link_status_msg_handler(void *hwdev
, u16 vf_id
,
405 void *buf_in
, u16 in_size
,
406 void *buf_out
, u16
*out_size
)
408 struct hinic_port_link_cmd
*get_link
= buf_out
;
409 struct hinic_hwdev
*hw_dev
= hwdev
;
410 struct vf_data_storage
*vf_infos
;
411 struct hinic_func_to_io
*nic_io
;
412 bool link_forced
, link_up
;
414 nic_io
= &hw_dev
->func_to_io
;
415 vf_infos
= nic_io
->vf_infos
;
416 link_forced
= vf_infos
[HW_VF_ID_TO_OS(vf_id
)].link_forced
;
417 link_up
= vf_infos
[HW_VF_ID_TO_OS(vf_id
)].link_up
;
420 get_link
->state
= link_up
?
421 HINIC_LINK_STATE_UP
: HINIC_LINK_STATE_DOWN
;
423 get_link
->state
= nic_io
->link_status
;
425 get_link
->status
= 0;
426 *out_size
= sizeof(*get_link
);
431 static bool check_func_table(struct hinic_hwdev
*hwdev
, u16 func_idx
,
432 void *buf_in
, u16 in_size
)
434 struct hinic_cmd_fw_ctxt
*function_table
= buf_in
;
436 if (!hinic_mbox_check_func_id_8B(hwdev
, func_idx
, buf_in
, in_size
) ||
437 !function_table
->rx_buf_sz
)
443 static struct vf_cmd_msg_handle nic_vf_cmd_msg_handler
[] = {
444 {HINIC_PORT_CMD_VF_REGISTER
, hinic_register_vf_msg_handler
},
445 {HINIC_PORT_CMD_VF_UNREGISTER
, hinic_unregister_vf_msg_handler
},
446 {HINIC_PORT_CMD_CHANGE_MTU
, hinic_change_vf_mtu_msg_handler
},
447 {HINIC_PORT_CMD_GET_MAC
, hinic_get_vf_mac_msg_handler
},
448 {HINIC_PORT_CMD_SET_MAC
, hinic_set_vf_mac_msg_handler
},
449 {HINIC_PORT_CMD_DEL_MAC
, hinic_del_vf_mac_msg_handler
},
450 {HINIC_PORT_CMD_GET_LINK_STATE
, hinic_get_vf_link_status_msg_handler
},
453 static struct vf_cmd_check_handle nic_cmd_support_vf
[] = {
454 {HINIC_PORT_CMD_VF_REGISTER
, NULL
},
455 {HINIC_PORT_CMD_VF_UNREGISTER
, NULL
},
456 {HINIC_PORT_CMD_CHANGE_MTU
, hinic_mbox_check_func_id_8B
},
457 {HINIC_PORT_CMD_ADD_VLAN
, hinic_mbox_check_func_id_8B
},
458 {HINIC_PORT_CMD_DEL_VLAN
, hinic_mbox_check_func_id_8B
},
459 {HINIC_PORT_CMD_SET_MAC
, hinic_mbox_check_func_id_8B
},
460 {HINIC_PORT_CMD_GET_MAC
, hinic_mbox_check_func_id_8B
},
461 {HINIC_PORT_CMD_DEL_MAC
, hinic_mbox_check_func_id_8B
},
462 {HINIC_PORT_CMD_SET_RX_MODE
, hinic_mbox_check_func_id_8B
},
463 {HINIC_PORT_CMD_GET_PAUSE_INFO
, hinic_mbox_check_func_id_8B
},
464 {HINIC_PORT_CMD_GET_LINK_STATE
, hinic_mbox_check_func_id_8B
},
465 {HINIC_PORT_CMD_SET_LRO
, hinic_mbox_check_func_id_8B
},
466 {HINIC_PORT_CMD_SET_RX_CSUM
, hinic_mbox_check_func_id_8B
},
467 {HINIC_PORT_CMD_SET_RX_VLAN_OFFLOAD
, hinic_mbox_check_func_id_8B
},
468 {HINIC_PORT_CMD_GET_VPORT_STAT
, hinic_mbox_check_func_id_8B
},
469 {HINIC_PORT_CMD_CLEAN_VPORT_STAT
, hinic_mbox_check_func_id_8B
},
470 {HINIC_PORT_CMD_GET_RSS_TEMPLATE_INDIR_TBL
,
471 hinic_mbox_check_func_id_8B
},
472 {HINIC_PORT_CMD_SET_RSS_TEMPLATE_TBL
, hinic_mbox_check_func_id_8B
},
473 {HINIC_PORT_CMD_GET_RSS_TEMPLATE_TBL
, hinic_mbox_check_func_id_8B
},
474 {HINIC_PORT_CMD_SET_RSS_HASH_ENGINE
, hinic_mbox_check_func_id_8B
},
475 {HINIC_PORT_CMD_GET_RSS_HASH_ENGINE
, hinic_mbox_check_func_id_8B
},
476 {HINIC_PORT_CMD_GET_RSS_CTX_TBL
, hinic_mbox_check_func_id_8B
},
477 {HINIC_PORT_CMD_SET_RSS_CTX_TBL
, hinic_mbox_check_func_id_8B
},
478 {HINIC_PORT_CMD_RSS_TEMP_MGR
, hinic_mbox_check_func_id_8B
},
479 {HINIC_PORT_CMD_RSS_CFG
, hinic_mbox_check_func_id_8B
},
480 {HINIC_PORT_CMD_FWCTXT_INIT
, check_func_table
},
481 {HINIC_PORT_CMD_GET_MGMT_VERSION
, NULL
},
482 {HINIC_PORT_CMD_SET_FUNC_STATE
, hinic_mbox_check_func_id_8B
},
483 {HINIC_PORT_CMD_GET_GLOBAL_QPN
, hinic_mbox_check_func_id_8B
},
484 {HINIC_PORT_CMD_SET_TSO
, hinic_mbox_check_func_id_8B
},
485 {HINIC_PORT_CMD_SET_RQ_IQ_MAP
, hinic_mbox_check_func_id_8B
},
486 {HINIC_PORT_CMD_LINK_STATUS_REPORT
, hinic_mbox_check_func_id_8B
},
487 {HINIC_PORT_CMD_UPDATE_MAC
, hinic_mbox_check_func_id_8B
},
488 {HINIC_PORT_CMD_GET_CAP
, hinic_mbox_check_func_id_8B
},
489 {HINIC_PORT_CMD_GET_LINK_MODE
, hinic_mbox_check_func_id_8B
},
492 #define CHECK_IPSU_15BIT 0X8000
495 struct hinic_sriov_info
*hinic_get_sriov_info_by_pcidev(struct pci_dev
*pdev
)
497 struct net_device
*netdev
= pci_get_drvdata(pdev
);
498 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
500 return &nic_dev
->sriov_info
;
503 static int hinic_check_mac_info(u8 status
, u16 vlan_id
)
505 if ((status
&& status
!= HINIC_MGMT_STATUS_EXIST
) ||
506 (vlan_id
& CHECK_IPSU_15BIT
&&
507 status
== HINIC_MGMT_STATUS_EXIST
))
513 #define HINIC_VLAN_ID_MASK 0x7FFF
515 static int hinic_update_mac(struct hinic_hwdev
*hwdev
, u8
*old_mac
,
516 u8
*new_mac
, u16 vlan_id
, u16 func_id
)
518 struct hinic_port_mac_update mac_info
= {0};
519 u16 out_size
= sizeof(mac_info
);
522 if (!hwdev
|| !old_mac
|| !new_mac
)
525 if ((vlan_id
& HINIC_VLAN_ID_MASK
) >= VLAN_N_VID
) {
526 dev_err(&hwdev
->hwif
->pdev
->dev
, "Invalid VLAN number: %d\n",
527 (vlan_id
& HINIC_VLAN_ID_MASK
));
531 mac_info
.func_id
= func_id
;
532 mac_info
.vlan_id
= vlan_id
;
533 memcpy(mac_info
.old_mac
, old_mac
, ETH_ALEN
);
534 memcpy(mac_info
.new_mac
, new_mac
, ETH_ALEN
);
536 err
= hinic_port_msg_cmd(hwdev
, HINIC_PORT_CMD_UPDATE_MAC
, &mac_info
,
537 sizeof(mac_info
), &mac_info
, &out_size
);
539 if (err
|| !out_size
||
540 hinic_check_mac_info(mac_info
.status
, mac_info
.vlan_id
)) {
541 dev_err(&hwdev
->hwif
->pdev
->dev
,
542 "Failed to update MAC, err: %d, status: 0x%x, out size: 0x%x\n",
543 err
, mac_info
.status
, out_size
);
547 if (mac_info
.status
== HINIC_MGMT_STATUS_EXIST
)
548 dev_warn(&hwdev
->hwif
->pdev
->dev
, "MAC is repeated. Ignore update operation\n");
553 static void hinic_get_vf_config(struct hinic_hwdev
*hwdev
, u16 vf_id
,
554 struct ifla_vf_info
*ivi
)
556 struct vf_data_storage
*vfinfo
;
558 vfinfo
= hwdev
->func_to_io
.vf_infos
+ HW_VF_ID_TO_OS(vf_id
);
560 ivi
->vf
= HW_VF_ID_TO_OS(vf_id
);
561 memcpy(ivi
->mac
, vfinfo
->vf_mac_addr
, ETH_ALEN
);
562 ivi
->vlan
= vfinfo
->pf_vlan
;
563 ivi
->qos
= vfinfo
->pf_qos
;
564 ivi
->spoofchk
= vfinfo
->spoofchk
;
565 ivi
->trusted
= vfinfo
->trust
;
566 ivi
->max_tx_rate
= vfinfo
->max_rate
;
567 ivi
->min_tx_rate
= vfinfo
->min_rate
;
569 if (!vfinfo
->link_forced
)
570 ivi
->linkstate
= IFLA_VF_LINK_STATE_AUTO
;
571 else if (vfinfo
->link_up
)
572 ivi
->linkstate
= IFLA_VF_LINK_STATE_ENABLE
;
574 ivi
->linkstate
= IFLA_VF_LINK_STATE_DISABLE
;
577 int hinic_ndo_get_vf_config(struct net_device
*netdev
,
578 int vf
, struct ifla_vf_info
*ivi
)
580 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
581 struct hinic_sriov_info
*sriov_info
;
583 sriov_info
= &nic_dev
->sriov_info
;
584 if (vf
>= sriov_info
->num_vfs
)
587 hinic_get_vf_config(sriov_info
->hwdev
, OS_VF_ID_TO_HW(vf
), ivi
);
592 static int hinic_set_vf_mac(struct hinic_hwdev
*hwdev
, int vf
,
593 unsigned char *mac_addr
)
595 struct hinic_func_to_io
*nic_io
= &hwdev
->func_to_io
;
596 struct vf_data_storage
*vf_info
;
600 vf_info
= nic_io
->vf_infos
+ HW_VF_ID_TO_OS(vf
);
602 /* duplicate request, so just return success */
603 if (vf_info
->pf_set_mac
&&
604 !memcmp(vf_info
->vf_mac_addr
, mac_addr
, ETH_ALEN
))
607 vf_info
->pf_set_mac
= true;
609 func_id
= hinic_glb_pf_vf_offset(hwdev
->hwif
) + vf
;
610 err
= hinic_update_mac(hwdev
, vf_info
->vf_mac_addr
,
611 mac_addr
, 0, func_id
);
613 vf_info
->pf_set_mac
= false;
617 memcpy(vf_info
->vf_mac_addr
, mac_addr
, ETH_ALEN
);
622 int hinic_ndo_set_vf_mac(struct net_device
*netdev
, int vf
, u8
*mac
)
624 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
625 struct hinic_sriov_info
*sriov_info
;
628 sriov_info
= &nic_dev
->sriov_info
;
629 if (!is_valid_ether_addr(mac
) || vf
>= sriov_info
->num_vfs
)
632 err
= hinic_set_vf_mac(sriov_info
->hwdev
, OS_VF_ID_TO_HW(vf
), mac
);
636 netif_info(nic_dev
, drv
, netdev
, "Setting MAC %pM on VF %d\n", mac
, vf
);
637 netif_info(nic_dev
, drv
, netdev
, "Reload the VF driver to make this change effective.");
642 static int hinic_add_vf_vlan(struct hinic_hwdev
*hwdev
, int vf_id
,
645 struct hinic_func_to_io
*nic_io
= &hwdev
->func_to_io
;
648 err
= hinic_set_vf_vlan(hwdev
, true, vlan
, qos
, vf_id
);
652 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].pf_vlan
= vlan
;
653 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].pf_qos
= qos
;
655 dev_info(&hwdev
->hwif
->pdev
->dev
, "Setting VLAN %d, QOS 0x%x on VF %d\n",
656 vlan
, qos
, HW_VF_ID_TO_OS(vf_id
));
660 static int hinic_kill_vf_vlan(struct hinic_hwdev
*hwdev
, int vf_id
)
662 struct hinic_func_to_io
*nic_io
= &hwdev
->func_to_io
;
665 err
= hinic_set_vf_vlan(hwdev
, false,
666 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].pf_vlan
,
667 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].pf_qos
,
672 dev_info(&hwdev
->hwif
->pdev
->dev
, "Remove VLAN %d on VF %d\n",
673 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].pf_vlan
,
674 HW_VF_ID_TO_OS(vf_id
));
676 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].pf_vlan
= 0;
677 nic_io
->vf_infos
[HW_VF_ID_TO_OS(vf_id
)].pf_qos
= 0;
682 static int hinic_update_mac_vlan(struct hinic_dev
*nic_dev
, u16 old_vlan
,
683 u16 new_vlan
, int vf_id
)
685 struct vf_data_storage
*vf_info
;
689 if (!nic_dev
|| old_vlan
>= VLAN_N_VID
|| new_vlan
>= VLAN_N_VID
)
692 vf_info
= nic_dev
->hwdev
->func_to_io
.vf_infos
+ HW_VF_ID_TO_OS(vf_id
);
693 if (!vf_info
->pf_set_mac
)
698 vlan_id
|= HINIC_ADD_VLAN_IN_MAC
;
700 err
= hinic_port_del_mac(nic_dev
, vf_info
->vf_mac_addr
, vlan_id
);
702 dev_err(&nic_dev
->hwdev
->hwif
->pdev
->dev
, "Failed to delete VF %d MAC %pM vlan %d\n",
703 HW_VF_ID_TO_OS(vf_id
), vf_info
->vf_mac_addr
, old_vlan
);
709 vlan_id
|= HINIC_ADD_VLAN_IN_MAC
;
711 err
= hinic_port_add_mac(nic_dev
, vf_info
->vf_mac_addr
, vlan_id
);
713 dev_err(&nic_dev
->hwdev
->hwif
->pdev
->dev
, "Failed to add VF %d MAC %pM vlan %d\n",
714 HW_VF_ID_TO_OS(vf_id
), vf_info
->vf_mac_addr
, new_vlan
);
723 vlan_id
|= HINIC_ADD_VLAN_IN_MAC
;
724 hinic_port_add_mac(nic_dev
, vf_info
->vf_mac_addr
, vlan_id
);
729 static int set_hw_vf_vlan(struct hinic_dev
*nic_dev
,
730 u16 cur_vlanprio
, int vf
, u16 vlan
, u8 qos
)
732 u16 old_vlan
= cur_vlanprio
& VLAN_VID_MASK
;
737 err
= hinic_kill_vf_vlan(nic_dev
->hwdev
,
740 dev_err(&nic_dev
->sriov_info
.pdev
->dev
, "Failed to delete vf %d old vlan %d\n",
745 err
= hinic_add_vf_vlan(nic_dev
->hwdev
,
746 OS_VF_ID_TO_HW(vf
), vlan
, qos
);
748 dev_err(&nic_dev
->sriov_info
.pdev
->dev
, "Failed to add vf %d new vlan %d\n",
753 err
= hinic_kill_vf_vlan(nic_dev
->hwdev
, OS_VF_ID_TO_HW(vf
));
755 dev_err(&nic_dev
->sriov_info
.pdev
->dev
, "Failed to delete vf %d vlan %d\n",
761 err
= hinic_update_mac_vlan(nic_dev
, old_vlan
, vlan
,
768 int hinic_ndo_set_vf_vlan(struct net_device
*netdev
, int vf
, u16 vlan
, u8 qos
,
771 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
772 struct hinic_sriov_info
*sriov_info
;
773 u16 vlanprio
, cur_vlanprio
;
775 sriov_info
= &nic_dev
->sriov_info
;
776 if (vf
>= sriov_info
->num_vfs
|| vlan
> 4095 || qos
> 7)
778 if (vlan_proto
!= htons(ETH_P_8021Q
))
779 return -EPROTONOSUPPORT
;
780 vlanprio
= vlan
| qos
<< HINIC_VLAN_PRIORITY_SHIFT
;
781 cur_vlanprio
= hinic_vf_info_vlanprio(nic_dev
->hwdev
,
783 /* duplicate request, so just return success */
784 if (vlanprio
== cur_vlanprio
)
787 return set_hw_vf_vlan(nic_dev
, cur_vlanprio
, vf
, vlan
, qos
);
790 static int hinic_set_vf_trust(struct hinic_hwdev
*hwdev
, u16 vf_id
,
793 struct vf_data_storage
*vf_infos
;
794 struct hinic_func_to_io
*nic_io
;
799 nic_io
= &hwdev
->func_to_io
;
800 vf_infos
= nic_io
->vf_infos
;
801 vf_infos
[vf_id
].trust
= trust
;
806 int hinic_ndo_set_vf_trust(struct net_device
*netdev
, int vf
, bool setting
)
808 struct hinic_dev
*adapter
= netdev_priv(netdev
);
809 struct hinic_sriov_info
*sriov_info
;
810 struct hinic_func_to_io
*nic_io
;
814 sriov_info
= &adapter
->sriov_info
;
815 nic_io
= &adapter
->hwdev
->func_to_io
;
817 if (vf
>= sriov_info
->num_vfs
)
820 cur_trust
= nic_io
->vf_infos
[vf
].trust
;
821 /* same request, so just return success */
822 if ((setting
&& cur_trust
) || (!setting
&& !cur_trust
))
825 err
= hinic_set_vf_trust(adapter
->hwdev
, vf
, setting
);
827 dev_info(&sriov_info
->pdev
->dev
, "Set VF %d trusted %s succeed\n",
828 vf
, setting
? "on" : "off");
830 dev_err(&sriov_info
->pdev
->dev
, "Failed set VF %d trusted %s\n",
831 vf
, setting
? "on" : "off");
836 int hinic_ndo_set_vf_bw(struct net_device
*netdev
,
837 int vf
, int min_tx_rate
, int max_tx_rate
)
839 u32 speeds
[] = {SPEED_10
, SPEED_100
, SPEED_1000
, SPEED_10000
,
840 SPEED_25000
, SPEED_40000
, SPEED_100000
};
841 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
842 struct hinic_port_cap port_cap
= { 0 };
843 enum hinic_port_link_state link_state
;
846 if (vf
>= nic_dev
->sriov_info
.num_vfs
) {
847 netif_err(nic_dev
, drv
, netdev
, "VF number must be less than %d\n",
848 nic_dev
->sriov_info
.num_vfs
);
852 if (max_tx_rate
< min_tx_rate
) {
853 netif_err(nic_dev
, drv
, netdev
, "Max rate %d must be greater than or equal to min rate %d\n",
854 max_tx_rate
, min_tx_rate
);
858 err
= hinic_port_link_state(nic_dev
, &link_state
);
860 netif_err(nic_dev
, drv
, netdev
,
861 "Get link status failed when setting vf tx rate\n");
865 if (link_state
== HINIC_LINK_STATE_DOWN
) {
866 netif_err(nic_dev
, drv
, netdev
,
867 "Link status must be up when setting vf tx rate\n");
871 err
= hinic_port_get_cap(nic_dev
, &port_cap
);
872 if (err
|| port_cap
.speed
> LINK_SPEED_100GB
)
875 /* rate limit cannot be less than 0 and greater than link speed */
876 if (max_tx_rate
< 0 || max_tx_rate
> speeds
[port_cap
.speed
]) {
877 netif_err(nic_dev
, drv
, netdev
, "Max tx rate must be in [0 - %d]\n",
878 speeds
[port_cap
.speed
]);
882 err
= hinic_set_vf_tx_rate(nic_dev
->hwdev
, OS_VF_ID_TO_HW(vf
),
883 max_tx_rate
, min_tx_rate
);
885 netif_err(nic_dev
, drv
, netdev
,
886 "Unable to set VF %d max rate %d min rate %d%s\n",
887 vf
, max_tx_rate
, min_tx_rate
,
888 err
== HINIC_TX_RATE_TABLE_FULL
?
889 ", tx rate profile is full" : "");
893 netif_info(nic_dev
, drv
, netdev
,
894 "Set VF %d max tx rate %d min tx rate %d successfully\n",
895 vf
, max_tx_rate
, min_tx_rate
);
900 static int hinic_set_vf_spoofchk(struct hinic_hwdev
*hwdev
, u16 vf_id
,
903 struct hinic_spoofchk_set spoofchk_cfg
= {0};
904 struct vf_data_storage
*vf_infos
= NULL
;
905 u16 out_size
= sizeof(spoofchk_cfg
);
911 vf_infos
= hwdev
->func_to_io
.vf_infos
;
913 spoofchk_cfg
.func_id
= hinic_glb_pf_vf_offset(hwdev
->hwif
) + vf_id
;
914 spoofchk_cfg
.state
= spoofchk
? 1 : 0;
915 err
= hinic_port_msg_cmd(hwdev
, HINIC_PORT_CMD_ENABLE_SPOOFCHK
,
916 &spoofchk_cfg
, sizeof(spoofchk_cfg
),
917 &spoofchk_cfg
, &out_size
);
918 if (spoofchk_cfg
.status
== HINIC_MGMT_CMD_UNSUPPORTED
) {
919 err
= HINIC_MGMT_CMD_UNSUPPORTED
;
920 } else if (err
|| !out_size
|| spoofchk_cfg
.status
) {
921 dev_err(&hwdev
->hwif
->pdev
->dev
, "Failed to set VF(%d) spoofchk, err: %d, status: 0x%x, out size: 0x%x\n",
922 HW_VF_ID_TO_OS(vf_id
), err
, spoofchk_cfg
.status
,
927 vf_infos
[HW_VF_ID_TO_OS(vf_id
)].spoofchk
= spoofchk
;
932 int hinic_ndo_set_vf_spoofchk(struct net_device
*netdev
, int vf
, bool setting
)
934 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
935 struct hinic_sriov_info
*sriov_info
;
939 sriov_info
= &nic_dev
->sriov_info
;
940 if (vf
>= sriov_info
->num_vfs
)
943 cur_spoofchk
= nic_dev
->hwdev
->func_to_io
.vf_infos
[vf
].spoofchk
;
945 /* same request, so just return success */
946 if ((setting
&& cur_spoofchk
) || (!setting
&& !cur_spoofchk
))
949 err
= hinic_set_vf_spoofchk(sriov_info
->hwdev
,
950 OS_VF_ID_TO_HW(vf
), setting
);
952 netif_info(nic_dev
, drv
, netdev
, "Set VF %d spoofchk %s successfully\n",
953 vf
, setting
? "on" : "off");
954 } else if (err
== HINIC_MGMT_CMD_UNSUPPORTED
) {
955 netif_err(nic_dev
, drv
, netdev
,
956 "Current firmware doesn't support to set vf spoofchk, need to upgrade latest firmware version\n");
963 static int hinic_set_vf_link_state(struct hinic_hwdev
*hwdev
, u16 vf_id
,
966 struct hinic_func_to_io
*nic_io
= &hwdev
->func_to_io
;
967 struct vf_data_storage
*vf_infos
= nic_io
->vf_infos
;
971 case HINIC_IFLA_VF_LINK_STATE_AUTO
:
972 vf_infos
[HW_VF_ID_TO_OS(vf_id
)].link_forced
= false;
973 vf_infos
[HW_VF_ID_TO_OS(vf_id
)].link_up
= nic_io
->link_status
?
975 link_status
= nic_io
->link_status
;
977 case HINIC_IFLA_VF_LINK_STATE_ENABLE
:
978 vf_infos
[HW_VF_ID_TO_OS(vf_id
)].link_forced
= true;
979 vf_infos
[HW_VF_ID_TO_OS(vf_id
)].link_up
= true;
980 link_status
= HINIC_LINK_UP
;
982 case HINIC_IFLA_VF_LINK_STATE_DISABLE
:
983 vf_infos
[HW_VF_ID_TO_OS(vf_id
)].link_forced
= true;
984 vf_infos
[HW_VF_ID_TO_OS(vf_id
)].link_up
= false;
985 link_status
= HINIC_LINK_DOWN
;
991 /* Notify the VF of its new link state */
992 hinic_notify_vf_link_status(hwdev
, vf_id
, link_status
);
997 int hinic_ndo_set_vf_link_state(struct net_device
*netdev
, int vf_id
, int link
)
999 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
1000 struct hinic_sriov_info
*sriov_info
;
1002 sriov_info
= &nic_dev
->sriov_info
;
1004 if (vf_id
>= sriov_info
->num_vfs
) {
1005 netif_err(nic_dev
, drv
, netdev
,
1006 "Invalid VF Identifier %d\n", vf_id
);
1010 return hinic_set_vf_link_state(sriov_info
->hwdev
,
1011 OS_VF_ID_TO_HW(vf_id
), link
);
1014 /* pf receive message from vf */
1015 static int nic_pf_mbox_handler(void *hwdev
, u16 vf_id
, u8 cmd
, void *buf_in
,
1016 u16 in_size
, void *buf_out
, u16
*out_size
)
1018 u8 size
= ARRAY_SIZE(nic_cmd_support_vf
);
1019 struct vf_cmd_msg_handle
*vf_msg_handle
;
1020 struct hinic_hwdev
*dev
= hwdev
;
1021 struct hinic_func_to_io
*nic_io
;
1022 struct hinic_pfhwdev
*pfhwdev
;
1029 if (!hinic_mbox_check_cmd_valid(hwdev
, nic_cmd_support_vf
, vf_id
, cmd
,
1030 buf_in
, in_size
, size
)) {
1031 dev_err(&dev
->hwif
->pdev
->dev
,
1032 "PF Receive VF nic cmd: 0x%x, mbox len: 0x%x is invalid\n",
1034 return HINIC_MBOX_VF_CMD_ERROR
;
1037 pfhwdev
= container_of(dev
, struct hinic_pfhwdev
, hwdev
);
1038 nic_io
= &dev
->func_to_io
;
1039 for (i
= 0; i
< ARRAY_SIZE(nic_vf_cmd_msg_handler
); i
++) {
1040 vf_msg_handle
= &nic_vf_cmd_msg_handler
[i
];
1041 if (cmd
== vf_msg_handle
->cmd
&&
1042 vf_msg_handle
->cmd_msg_handler
) {
1043 err
= vf_msg_handle
->cmd_msg_handler(hwdev
, vf_id
,
1050 if (i
== ARRAY_SIZE(nic_vf_cmd_msg_handler
))
1051 err
= hinic_msg_to_mgmt(&pfhwdev
->pf_to_mgmt
, HINIC_MOD_L2NIC
,
1052 cmd
, buf_in
, in_size
, buf_out
,
1053 out_size
, HINIC_MGMT_MSG_SYNC
);
1055 if (err
&& err
!= HINIC_MBOX_PF_BUSY_ACTIVE_FW
)
1056 dev_err(&nic_io
->hwif
->pdev
->dev
, "PF receive VF L2NIC cmd: %d process error, err:%d\n",
1061 static int cfg_mbx_pf_proc_vf_msg(void *hwdev
, u16 vf_id
, u8 cmd
, void *buf_in
,
1062 u16 in_size
, void *buf_out
, u16
*out_size
)
1064 struct hinic_dev_cap
*dev_cap
= buf_out
;
1065 struct hinic_hwdev
*dev
= hwdev
;
1066 struct hinic_cap
*cap
;
1068 cap
= &dev
->nic_cap
;
1069 memset(dev_cap
, 0, sizeof(*dev_cap
));
1071 dev_cap
->max_vf
= cap
->max_vf
;
1072 dev_cap
->max_sqs
= cap
->max_vf_qps
;
1073 dev_cap
->max_rqs
= cap
->max_vf_qps
;
1074 dev_cap
->port_id
= dev
->port_id
;
1076 *out_size
= sizeof(*dev_cap
);
1081 static int hinic_init_vf_infos(struct hinic_func_to_io
*nic_io
, u16 vf_id
)
1083 struct vf_data_storage
*vf_infos
= nic_io
->vf_infos
;
1085 if (set_vf_link_state
> HINIC_IFLA_VF_LINK_STATE_DISABLE
) {
1086 dev_warn(&nic_io
->hwif
->pdev
->dev
, "Module Parameter set_vf_link_state value %d is out of range, resetting to %d\n",
1087 set_vf_link_state
, HINIC_IFLA_VF_LINK_STATE_AUTO
);
1088 set_vf_link_state
= HINIC_IFLA_VF_LINK_STATE_AUTO
;
1091 switch (set_vf_link_state
) {
1092 case HINIC_IFLA_VF_LINK_STATE_AUTO
:
1093 vf_infos
[vf_id
].link_forced
= false;
1095 case HINIC_IFLA_VF_LINK_STATE_ENABLE
:
1096 vf_infos
[vf_id
].link_forced
= true;
1097 vf_infos
[vf_id
].link_up
= true;
1099 case HINIC_IFLA_VF_LINK_STATE_DISABLE
:
1100 vf_infos
[vf_id
].link_forced
= true;
1101 vf_infos
[vf_id
].link_up
= false;
1104 dev_err(&nic_io
->hwif
->pdev
->dev
, "Invalid input parameter set_vf_link_state: %d\n",
1112 static void hinic_clear_vf_infos(struct hinic_dev
*nic_dev
, u16 vf_id
)
1114 struct vf_data_storage
*vf_infos
;
1116 vf_infos
= nic_dev
->hwdev
->func_to_io
.vf_infos
+ HW_VF_ID_TO_OS(vf_id
);
1117 if (vf_infos
->pf_set_mac
)
1118 hinic_port_del_mac(nic_dev
, vf_infos
->vf_mac_addr
, 0);
1120 if (hinic_vf_info_vlanprio(nic_dev
->hwdev
, vf_id
))
1121 hinic_kill_vf_vlan(nic_dev
->hwdev
, vf_id
);
1123 if (vf_infos
->max_rate
)
1124 hinic_set_vf_tx_rate(nic_dev
->hwdev
, vf_id
, 0, 0);
1126 if (vf_infos
->spoofchk
)
1127 hinic_set_vf_spoofchk(nic_dev
->hwdev
, vf_id
, false);
1129 if (vf_infos
->trust
)
1130 hinic_set_vf_trust(nic_dev
->hwdev
, vf_id
, false);
1132 memset(vf_infos
, 0, sizeof(*vf_infos
));
1133 /* set vf_infos to default */
1134 hinic_init_vf_infos(&nic_dev
->hwdev
->func_to_io
, HW_VF_ID_TO_OS(vf_id
));
1137 static int hinic_deinit_vf_hw(struct hinic_sriov_info
*sriov_info
,
1138 u16 start_vf_id
, u16 end_vf_id
)
1140 struct hinic_dev
*nic_dev
;
1143 nic_dev
= container_of(sriov_info
, struct hinic_dev
, sriov_info
);
1145 for (idx
= start_vf_id
; idx
<= end_vf_id
; idx
++) {
1146 func_idx
= hinic_glb_pf_vf_offset(nic_dev
->hwdev
->hwif
) + idx
;
1147 hinic_set_wq_page_size(nic_dev
->hwdev
, func_idx
,
1148 HINIC_HW_WQ_PAGE_SIZE
);
1149 hinic_clear_vf_infos(nic_dev
, idx
);
1155 int hinic_vf_func_init(struct hinic_hwdev
*hwdev
)
1157 struct hinic_register_vf register_info
= {0};
1158 u16 out_size
= sizeof(register_info
);
1159 struct hinic_func_to_io
*nic_io
;
1163 err
= hinic_vf_mbox_random_id_init(hwdev
);
1165 dev_err(&hwdev
->hwif
->pdev
->dev
, "Failed to init vf mbox random id, err: %d\n",
1170 nic_io
= &hwdev
->func_to_io
;
1172 if (HINIC_IS_VF(hwdev
->hwif
)) {
1173 err
= hinic_mbox_to_pf(hwdev
, HINIC_MOD_L2NIC
,
1174 HINIC_PORT_CMD_VF_REGISTER
,
1175 ®ister_info
, sizeof(register_info
),
1176 ®ister_info
, &out_size
, 0);
1177 if (err
|| register_info
.status
|| !out_size
) {
1178 dev_err(&hwdev
->hwif
->pdev
->dev
,
1179 "Failed to register VF, err: %d, status: 0x%x, out size: 0x%x\n",
1180 err
, register_info
.status
, out_size
);
1181 hinic_unregister_vf_mbox_cb(hwdev
, HINIC_MOD_L2NIC
);
1185 err
= hinic_register_pf_mbox_cb(hwdev
, HINIC_MOD_CFGM
,
1186 cfg_mbx_pf_proc_vf_msg
);
1188 dev_err(&hwdev
->hwif
->pdev
->dev
,
1189 "Register PF mailbox callback failed\n");
1192 nic_io
->max_vfs
= hwdev
->nic_cap
.max_vf
;
1193 size
= sizeof(*nic_io
->vf_infos
) * nic_io
->max_vfs
;
1195 nic_io
->vf_infos
= kzalloc(size
, GFP_KERNEL
);
1196 if (!nic_io
->vf_infos
) {
1198 goto out_free_nic_io
;
1201 for (i
= 0; i
< nic_io
->max_vfs
; i
++) {
1202 err
= hinic_init_vf_infos(nic_io
, i
);
1204 goto err_init_vf_infos
;
1207 err
= hinic_register_pf_mbox_cb(hwdev
, HINIC_MOD_L2NIC
,
1208 nic_pf_mbox_handler
);
1210 goto err_register_pf_mbox_cb
;
1216 err_register_pf_mbox_cb
:
1218 kfree(nic_io
->vf_infos
);
1223 void hinic_vf_func_free(struct hinic_hwdev
*hwdev
)
1225 struct hinic_register_vf unregister
= {0};
1226 u16 out_size
= sizeof(unregister
);
1229 if (HINIC_IS_VF(hwdev
->hwif
)) {
1230 err
= hinic_mbox_to_pf(hwdev
, HINIC_MOD_L2NIC
,
1231 HINIC_PORT_CMD_VF_UNREGISTER
,
1232 &unregister
, sizeof(unregister
),
1233 &unregister
, &out_size
, 0);
1234 if (err
|| !out_size
|| unregister
.status
)
1235 dev_err(&hwdev
->hwif
->pdev
->dev
, "Failed to unregister VF, err: %d, status: 0x%x, out_size: 0x%x\n",
1236 err
, unregister
.status
, out_size
);
1238 if (hwdev
->func_to_io
.vf_infos
) {
1239 hinic_unregister_pf_mbox_cb(hwdev
, HINIC_MOD_L2NIC
);
1240 kfree(hwdev
->func_to_io
.vf_infos
);
1245 static int hinic_init_vf_hw(struct hinic_hwdev
*hwdev
, u16 start_vf_id
,
1251 /* vf use 256K as default wq page size, and can't change it */
1252 for (i
= start_vf_id
; i
<= end_vf_id
; i
++) {
1253 func_idx
= hinic_glb_pf_vf_offset(hwdev
->hwif
) + i
;
1254 err
= hinic_set_wq_page_size(hwdev
, func_idx
,
1255 HINIC_DEFAULT_WQ_PAGE_SIZE
);
1263 int hinic_pci_sriov_disable(struct pci_dev
*pdev
)
1265 struct hinic_sriov_info
*sriov_info
;
1268 sriov_info
= hinic_get_sriov_info_by_pcidev(pdev
);
1269 /* if SR-IOV is already disabled then nothing will be done */
1270 if (!sriov_info
->sriov_enabled
)
1273 set_bit(HINIC_SRIOV_DISABLE
, &sriov_info
->state
);
1275 /* If our VFs are assigned we cannot shut down SR-IOV
1276 * without causing issues, so just leave the hardware
1277 * available but disabled
1279 if (pci_vfs_assigned(sriov_info
->pdev
)) {
1280 clear_bit(HINIC_SRIOV_DISABLE
, &sriov_info
->state
);
1281 dev_warn(&pdev
->dev
, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
1284 sriov_info
->sriov_enabled
= false;
1286 /* disable iov and allow time for transactions to clear */
1287 pci_disable_sriov(sriov_info
->pdev
);
1289 tmp_vfs
= (u16
)sriov_info
->num_vfs
;
1290 sriov_info
->num_vfs
= 0;
1291 hinic_deinit_vf_hw(sriov_info
, OS_VF_ID_TO_HW(0),
1292 OS_VF_ID_TO_HW(tmp_vfs
- 1));
1294 clear_bit(HINIC_SRIOV_DISABLE
, &sriov_info
->state
);
1299 int hinic_pci_sriov_enable(struct pci_dev
*pdev
, int num_vfs
)
1301 struct hinic_sriov_info
*sriov_info
;
1304 sriov_info
= hinic_get_sriov_info_by_pcidev(pdev
);
1306 if (test_and_set_bit(HINIC_SRIOV_ENABLE
, &sriov_info
->state
)) {
1308 "SR-IOV enable in process, please wait, num_vfs %d\n",
1313 err
= hinic_init_vf_hw(sriov_info
->hwdev
, OS_VF_ID_TO_HW(0),
1314 OS_VF_ID_TO_HW((u16
)num_vfs
- 1));
1316 dev_err(&sriov_info
->pdev
->dev
,
1317 "Failed to init vf in hardware before enable sriov, error %d\n",
1319 clear_bit(HINIC_SRIOV_ENABLE
, &sriov_info
->state
);
1323 err
= pci_enable_sriov(sriov_info
->pdev
, num_vfs
);
1326 "Failed to enable SR-IOV, error %d\n", err
);
1327 clear_bit(HINIC_SRIOV_ENABLE
, &sriov_info
->state
);
1331 sriov_info
->sriov_enabled
= true;
1332 sriov_info
->num_vfs
= num_vfs
;
1333 clear_bit(HINIC_SRIOV_ENABLE
, &sriov_info
->state
);
1338 int hinic_pci_sriov_configure(struct pci_dev
*dev
, int num_vfs
)
1340 struct hinic_sriov_info
*sriov_info
;
1342 sriov_info
= hinic_get_sriov_info_by_pcidev(dev
);
1344 if (test_bit(HINIC_FUNC_REMOVE
, &sriov_info
->state
))
1348 return hinic_pci_sriov_disable(dev
);
1350 return hinic_pci_sriov_enable(dev
, num_vfs
);