1
2
3
4
5
6#include <linux/pci.h>
7#include <linux/if_vlan.h>
8#include <linux/interrupt.h>
9#include <linux/etherdevice.h>
10#include <linux/netdevice.h>
11
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"
18
19static unsigned char set_vf_link_state;
20module_param(set_vf_link_state, byte, 0444);
21MODULE_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.");
22
23#define HINIC_VLAN_PRIORITY_SHIFT 13
24#define HINIC_ADD_VLAN_IN_MAC 0x8000
25#define HINIC_TX_RATE_TABLE_FULL 12
26
27static int hinic_set_mac(struct hinic_hwdev *hwdev, const u8 *mac_addr,
28 u16 vlan_id, u16 func_id)
29{
30 struct hinic_port_mac_cmd mac_info = {0};
31 u16 out_size = sizeof(mac_info);
32 int err;
33
34 mac_info.func_idx = func_id;
35 mac_info.vlan_id = vlan_id;
36 memcpy(mac_info.mac, mac_addr, ETH_ALEN);
37
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);
44 return -EIO;
45 }
46
47 return 0;
48}
49
50static void hinic_notify_vf_link_status(struct hinic_hwdev *hwdev, u16 vf_id,
51 u8 link_status)
52{
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);
56 int err;
57
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,
63 &link, sizeof(link),
64 &link, &out_size, 0);
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);
70 }
71}
72
73
74void hinic_notify_all_vfs_link_changed(struct hinic_hwdev *hwdev,
75 u8 link_status)
76{
77 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
78 u16 i;
79
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);
84 }
85}
86
87static u16 hinic_vf_info_vlanprio(struct hinic_hwdev *hwdev, int vf_id)
88{
89 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
90 u16 pf_vlan, vlanprio;
91 u8 pf_qos;
92
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;
96
97 return vlanprio;
98}
99
100static int hinic_set_vf_vlan(struct hinic_hwdev *hwdev, bool add, u16 vid,
101 u8 qos, int vf_id)
102{
103 struct hinic_vf_vlan_config vf_vlan = {0};
104 u16 out_size = sizeof(vf_vlan);
105 int err;
106 u8 cmd;
107
108
109 if (!vid && !add)
110 return 0;
111
112 vf_vlan.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
113 vf_vlan.vlan_id = vid;
114 vf_vlan.qos = qos;
115
116 if (add)
117 cmd = HINIC_PORT_CMD_SET_VF_VLAN;
118 else
119 cmd = HINIC_PORT_CMD_CLR_VF_VLAN;
120
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);
126 return -EFAULT;
127 }
128
129 return 0;
130}
131
132static int hinic_set_vf_tx_rate_max_min(struct hinic_hwdev *hwdev, u16 vf_id,
133 u32 max_rate, u32 min_rate)
134{
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);
138 int err;
139
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,
145 &out_size);
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);
151 return -EIO;
152 }
153
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;
157 }
158
159 return rate_cfg.status;
160}
161
162static int hinic_set_vf_rate_limit(struct hinic_hwdev *hwdev, u16 vf_id,
163 u32 tx_rate)
164{
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);
168 int err;
169
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,
174 &out_size);
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,
178 out_size);
179 if (rate_cfg.status)
180 return rate_cfg.status;
181
182 return -EIO;
183 }
184
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;
187
188 return 0;
189}
190
191static int hinic_set_vf_tx_rate(struct hinic_hwdev *hwdev, u16 vf_id,
192 u32 max_rate, u32 min_rate)
193{
194 int err;
195
196 err = hinic_set_vf_tx_rate_max_min(hwdev, vf_id, max_rate, min_rate);
197 if (err != HINIC_MGMT_CMD_UNSUPPORTED)
198 return err;
199
200 if (min_rate) {
201 dev_err(&hwdev->hwif->pdev->dev, "Current firmware doesn't support to set min tx rate\n");
202 return -EOPNOTSUPP;
203 }
204
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");
206
207 return hinic_set_vf_rate_limit(hwdev, vf_id, max_rate);
208}
209
210static int hinic_init_vf_config(struct hinic_hwdev *hwdev, u16 vf_id)
211{
212 struct vf_data_storage *vf_info;
213 u16 func_id, vlan_id;
214 int err = 0;
215
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;
219
220 vlan_id = 0;
221
222 err = hinic_set_mac(hwdev, vf_info->vf_mac_addr, vlan_id,
223 func_id);
224 if (err) {
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));
227 return err;
228 }
229 }
230
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);
234 if (err) {
235 dev_err(&hwdev->hwif->pdev->dev, "Failed to add VF %d VLAN_QOS\n",
236 HW_VF_ID_TO_OS(vf_id));
237 return err;
238 }
239 }
240
241 if (vf_info->max_rate) {
242 err = hinic_set_vf_tx_rate(hwdev, vf_id, vf_info->max_rate,
243 vf_info->min_rate);
244 if (err) {
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,
247 vf_info->min_rate);
248 return err;
249 }
250 }
251
252 return 0;
253}
254
255static 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)
258{
259 struct hinic_register_vf *register_info = buf_out;
260 struct hinic_hwdev *hw_dev = hwdev;
261 struct hinic_func_to_io *nic_io;
262 int err;
263
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;
269 return -EFAULT;
270 }
271
272 *out_size = sizeof(*register_info);
273 err = hinic_init_vf_config(hw_dev, vf_id);
274 if (err) {
275 register_info->status = EFAULT;
276 return err;
277 }
278
279 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].registered = true;
280
281 return 0;
282}
283
284static 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)
287{
288 struct hinic_hwdev *hw_dev = hwdev;
289 struct hinic_func_to_io *nic_io;
290
291 nic_io = &hw_dev->func_to_io;
292 *out_size = 0;
293 if (vf_id > nic_io->max_vfs)
294 return 0;
295
296 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].registered = false;
297
298 return 0;
299}
300
301static 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)
304{
305 struct hinic_hwdev *hw_dev = hwdev;
306 int err;
307
308 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_CHANGE_MTU, buf_in,
309 in_size, buf_out, out_size);
310 if (err) {
311 dev_err(&hw_dev->hwif->pdev->dev, "Failed to set VF %u mtu\n",
312 vf_id);
313 return err;
314 }
315
316 return 0;
317}
318
319static 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)
322{
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;
327
328 nic_io = &dev->func_to_io;
329 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id);
330
331 memcpy(mac_info->mac, vf_info->vf_mac_addr, ETH_ALEN);
332 mac_info->status = 0;
333 *out_size = sizeof(*mac_info);
334
335 return 0;
336}
337
338static 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)
341{
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;
347 int err;
348
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);
357 return 0;
358 }
359
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);
366 return -EFAULT;
367 }
368
369 return err;
370}
371
372static 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)
375{
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;
381 int err;
382
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);
390 return 0;
391 }
392
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);
398 return -EFAULT;
399 }
400
401 return err;
402}
403
404static 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)
407{
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;
413
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;
418
419 if (link_forced)
420 get_link->state = link_up ?
421 HINIC_LINK_STATE_UP : HINIC_LINK_STATE_DOWN;
422 else
423 get_link->state = nic_io->link_status;
424
425 get_link->status = 0;
426 *out_size = sizeof(*get_link);
427
428 return 0;
429}
430
431static bool check_func_table(struct hinic_hwdev *hwdev, u16 func_idx,
432 void *buf_in, u16 in_size)
433{
434 struct hinic_cmd_fw_ctxt *function_table = buf_in;
435
436 if (!hinic_mbox_check_func_id_8B(hwdev, func_idx, buf_in, in_size) ||
437 !function_table->rx_buf_sz)
438 return false;
439
440 return true;
441}
442
443static 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},
451};
452
453static 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},
490};
491
492#define CHECK_IPSU_15BIT 0X8000
493
494static
495struct hinic_sriov_info *hinic_get_sriov_info_by_pcidev(struct pci_dev *pdev)
496{
497 struct net_device *netdev = pci_get_drvdata(pdev);
498 struct hinic_dev *nic_dev = netdev_priv(netdev);
499
500 return &nic_dev->sriov_info;
501}
502
503static int hinic_check_mac_info(u8 status, u16 vlan_id)
504{
505 if ((status && status != HINIC_MGMT_STATUS_EXIST) ||
506 (vlan_id & CHECK_IPSU_15BIT &&
507 status == HINIC_MGMT_STATUS_EXIST))
508 return -EINVAL;
509
510 return 0;
511}
512
513#define HINIC_VLAN_ID_MASK 0x7FFF
514
515static int hinic_update_mac(struct hinic_hwdev *hwdev, u8 *old_mac,
516 u8 *new_mac, u16 vlan_id, u16 func_id)
517{
518 struct hinic_port_mac_update mac_info = {0};
519 u16 out_size = sizeof(mac_info);
520 int err;
521
522 if (!hwdev || !old_mac || !new_mac)
523 return -EINVAL;
524
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));
528 return -EINVAL;
529 }
530
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);
535
536 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_UPDATE_MAC, &mac_info,
537 sizeof(mac_info), &mac_info, &out_size);
538
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);
544 return -EINVAL;
545 }
546
547 if (mac_info.status == HINIC_MGMT_STATUS_EXIST)
548 dev_warn(&hwdev->hwif->pdev->dev, "MAC is repeated. Ignore update operation\n");
549
550 return 0;
551}
552
553static void hinic_get_vf_config(struct hinic_hwdev *hwdev, u16 vf_id,
554 struct ifla_vf_info *ivi)
555{
556 struct vf_data_storage *vfinfo;
557
558 vfinfo = hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
559
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;
568
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;
573 else
574 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
575}
576
577int hinic_ndo_get_vf_config(struct net_device *netdev,
578 int vf, struct ifla_vf_info *ivi)
579{
580 struct hinic_dev *nic_dev = netdev_priv(netdev);
581 struct hinic_sriov_info *sriov_info;
582
583 sriov_info = &nic_dev->sriov_info;
584 if (vf >= sriov_info->num_vfs)
585 return -EINVAL;
586
587 hinic_get_vf_config(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), ivi);
588
589 return 0;
590}
591
592static int hinic_set_vf_mac(struct hinic_hwdev *hwdev, int vf,
593 unsigned char *mac_addr)
594{
595 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
596 struct vf_data_storage *vf_info;
597 u16 func_id;
598 int err;
599
600 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf);
601
602
603 if (vf_info->pf_set_mac &&
604 !memcmp(vf_info->vf_mac_addr, mac_addr, ETH_ALEN))
605 return 0;
606
607 vf_info->pf_set_mac = true;
608
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);
612 if (err) {
613 vf_info->pf_set_mac = false;
614 return err;
615 }
616
617 memcpy(vf_info->vf_mac_addr, mac_addr, ETH_ALEN);
618
619 return 0;
620}
621
622int hinic_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
623{
624 struct hinic_dev *nic_dev = netdev_priv(netdev);
625 struct hinic_sriov_info *sriov_info;
626 int err;
627
628 sriov_info = &nic_dev->sriov_info;
629 if (!is_valid_ether_addr(mac) || vf >= sriov_info->num_vfs)
630 return -EINVAL;
631
632 err = hinic_set_vf_mac(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), mac);
633 if (err)
634 return err;
635
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.");
638
639 return 0;
640}
641
642static int hinic_add_vf_vlan(struct hinic_hwdev *hwdev, int vf_id,
643 u16 vlan, u8 qos)
644{
645 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
646 int err;
647
648 err = hinic_set_vf_vlan(hwdev, true, vlan, qos, vf_id);
649 if (err)
650 return err;
651
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;
654
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));
657 return 0;
658}
659
660static int hinic_kill_vf_vlan(struct hinic_hwdev *hwdev, int vf_id)
661{
662 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
663 int err;
664
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,
668 vf_id);
669 if (err)
670 return err;
671
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));
675
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;
678
679 return 0;
680}
681
682static int hinic_update_mac_vlan(struct hinic_dev *nic_dev, u16 old_vlan,
683 u16 new_vlan, int vf_id)
684{
685 struct vf_data_storage *vf_info;
686 u16 vlan_id;
687 int err;
688
689 if (!nic_dev || old_vlan >= VLAN_N_VID || new_vlan >= VLAN_N_VID)
690 return -EINVAL;
691
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)
694 return 0;
695
696 vlan_id = old_vlan;
697 if (vlan_id)
698 vlan_id |= HINIC_ADD_VLAN_IN_MAC;
699
700 err = hinic_port_del_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
701 if (err) {
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);
704 return err;
705 }
706
707 vlan_id = new_vlan;
708 if (vlan_id)
709 vlan_id |= HINIC_ADD_VLAN_IN_MAC;
710
711 err = hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
712 if (err) {
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);
715 goto out;
716 }
717
718 return 0;
719
720out:
721 vlan_id = old_vlan;
722 if (vlan_id)
723 vlan_id |= HINIC_ADD_VLAN_IN_MAC;
724 hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
725
726 return err;
727}
728
729static int set_hw_vf_vlan(struct hinic_dev *nic_dev,
730 u16 cur_vlanprio, int vf, u16 vlan, u8 qos)
731{
732 u16 old_vlan = cur_vlanprio & VLAN_VID_MASK;
733 int err = 0;
734
735 if (vlan || qos) {
736 if (cur_vlanprio) {
737 err = hinic_kill_vf_vlan(nic_dev->hwdev,
738 OS_VF_ID_TO_HW(vf));
739 if (err) {
740 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d old vlan %d\n",
741 vf, old_vlan);
742 goto out;
743 }
744 }
745 err = hinic_add_vf_vlan(nic_dev->hwdev,
746 OS_VF_ID_TO_HW(vf), vlan, qos);
747 if (err) {
748 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to add vf %d new vlan %d\n",
749 vf, vlan);
750 goto out;
751 }
752 } else {
753 err = hinic_kill_vf_vlan(nic_dev->hwdev, OS_VF_ID_TO_HW(vf));
754 if (err) {
755 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d vlan %d\n",
756 vf, old_vlan);
757 goto out;
758 }
759 }
760
761 err = hinic_update_mac_vlan(nic_dev, old_vlan, vlan,
762 OS_VF_ID_TO_HW(vf));
763
764out:
765 return err;
766}
767
768int hinic_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos,
769 __be16 vlan_proto)
770{
771 struct hinic_dev *nic_dev = netdev_priv(netdev);
772 struct hinic_sriov_info *sriov_info;
773 u16 vlanprio, cur_vlanprio;
774
775 sriov_info = &nic_dev->sriov_info;
776 if (vf >= sriov_info->num_vfs || vlan > 4095 || qos > 7)
777 return -EINVAL;
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,
782 OS_VF_ID_TO_HW(vf));
783
784 if (vlanprio == cur_vlanprio)
785 return 0;
786
787 return set_hw_vf_vlan(nic_dev, cur_vlanprio, vf, vlan, qos);
788}
789
790static int hinic_set_vf_trust(struct hinic_hwdev *hwdev, u16 vf_id,
791 bool trust)
792{
793 struct vf_data_storage *vf_infos;
794 struct hinic_func_to_io *nic_io;
795
796 if (!hwdev)
797 return -EINVAL;
798
799 nic_io = &hwdev->func_to_io;
800 vf_infos = nic_io->vf_infos;
801 vf_infos[vf_id].trust = trust;
802
803 return 0;
804}
805
806int hinic_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting)
807{
808 struct hinic_dev *adapter = netdev_priv(netdev);
809 struct hinic_sriov_info *sriov_info;
810 struct hinic_func_to_io *nic_io;
811 bool cur_trust;
812 int err;
813
814 sriov_info = &adapter->sriov_info;
815 nic_io = &adapter->hwdev->func_to_io;
816
817 if (vf >= sriov_info->num_vfs)
818 return -EINVAL;
819
820 cur_trust = nic_io->vf_infos[vf].trust;
821
822 if ((setting && cur_trust) || (!setting && !cur_trust))
823 return 0;
824
825 err = hinic_set_vf_trust(adapter->hwdev, vf, setting);
826 if (!err)
827 dev_info(&sriov_info->pdev->dev, "Set VF %d trusted %s succeed\n",
828 vf, setting ? "on" : "off");
829 else
830 dev_err(&sriov_info->pdev->dev, "Failed set VF %d trusted %s\n",
831 vf, setting ? "on" : "off");
832
833 return err;
834}
835
836int hinic_ndo_set_vf_bw(struct net_device *netdev,
837 int vf, int min_tx_rate, int max_tx_rate)
838{
839 static const u32 speeds[] = {
840 SPEED_10, SPEED_100, SPEED_1000, SPEED_10000,
841 SPEED_25000, SPEED_40000, SPEED_100000
842 };
843 struct hinic_dev *nic_dev = netdev_priv(netdev);
844 struct hinic_port_cap port_cap = { 0 };
845 enum hinic_port_link_state link_state;
846 int err;
847
848 if (vf >= nic_dev->sriov_info.num_vfs) {
849 netif_err(nic_dev, drv, netdev, "VF number must be less than %d\n",
850 nic_dev->sriov_info.num_vfs);
851 return -EINVAL;
852 }
853
854 if (max_tx_rate < min_tx_rate) {
855 netif_err(nic_dev, drv, netdev, "Max rate %d must be greater than or equal to min rate %d\n",
856 max_tx_rate, min_tx_rate);
857 return -EINVAL;
858 }
859
860 err = hinic_port_link_state(nic_dev, &link_state);
861 if (err) {
862 netif_err(nic_dev, drv, netdev,
863 "Get link status failed when setting vf tx rate\n");
864 return -EIO;
865 }
866
867 if (link_state == HINIC_LINK_STATE_DOWN) {
868 netif_err(nic_dev, drv, netdev,
869 "Link status must be up when setting vf tx rate\n");
870 return -EPERM;
871 }
872
873 err = hinic_port_get_cap(nic_dev, &port_cap);
874 if (err || port_cap.speed > LINK_SPEED_100GB)
875 return -EIO;
876
877
878 if (max_tx_rate < 0 || max_tx_rate > speeds[port_cap.speed]) {
879 netif_err(nic_dev, drv, netdev, "Max tx rate must be in [0 - %d]\n",
880 speeds[port_cap.speed]);
881 return -EINVAL;
882 }
883
884 err = hinic_set_vf_tx_rate(nic_dev->hwdev, OS_VF_ID_TO_HW(vf),
885 max_tx_rate, min_tx_rate);
886 if (err) {
887 netif_err(nic_dev, drv, netdev,
888 "Unable to set VF %d max rate %d min rate %d%s\n",
889 vf, max_tx_rate, min_tx_rate,
890 err == HINIC_TX_RATE_TABLE_FULL ?
891 ", tx rate profile is full" : "");
892 return -EIO;
893 }
894
895 netif_info(nic_dev, drv, netdev,
896 "Set VF %d max tx rate %d min tx rate %d successfully\n",
897 vf, max_tx_rate, min_tx_rate);
898
899 return 0;
900}
901
902static int hinic_set_vf_spoofchk(struct hinic_hwdev *hwdev, u16 vf_id,
903 bool spoofchk)
904{
905 struct hinic_spoofchk_set spoofchk_cfg = {0};
906 struct vf_data_storage *vf_infos = NULL;
907 u16 out_size = sizeof(spoofchk_cfg);
908 int err;
909
910 if (!hwdev)
911 return -EINVAL;
912
913 vf_infos = hwdev->func_to_io.vf_infos;
914
915 spoofchk_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
916 spoofchk_cfg.state = spoofchk ? 1 : 0;
917 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_ENABLE_SPOOFCHK,
918 &spoofchk_cfg, sizeof(spoofchk_cfg),
919 &spoofchk_cfg, &out_size);
920 if (spoofchk_cfg.status == HINIC_MGMT_CMD_UNSUPPORTED) {
921 err = HINIC_MGMT_CMD_UNSUPPORTED;
922 } else if (err || !out_size || spoofchk_cfg.status) {
923 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) spoofchk, err: %d, status: 0x%x, out size: 0x%x\n",
924 HW_VF_ID_TO_OS(vf_id), err, spoofchk_cfg.status,
925 out_size);
926 err = -EIO;
927 }
928
929 vf_infos[HW_VF_ID_TO_OS(vf_id)].spoofchk = spoofchk;
930
931 return err;
932}
933
934int hinic_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
935{
936 struct hinic_dev *nic_dev = netdev_priv(netdev);
937 struct hinic_sriov_info *sriov_info;
938 bool cur_spoofchk;
939 int err;
940
941 sriov_info = &nic_dev->sriov_info;
942 if (vf >= sriov_info->num_vfs)
943 return -EINVAL;
944
945 cur_spoofchk = nic_dev->hwdev->func_to_io.vf_infos[vf].spoofchk;
946
947
948 if ((setting && cur_spoofchk) || (!setting && !cur_spoofchk))
949 return 0;
950
951 err = hinic_set_vf_spoofchk(sriov_info->hwdev,
952 OS_VF_ID_TO_HW(vf), setting);
953 if (!err) {
954 netif_info(nic_dev, drv, netdev, "Set VF %d spoofchk %s successfully\n",
955 vf, setting ? "on" : "off");
956 } else if (err == HINIC_MGMT_CMD_UNSUPPORTED) {
957 netif_err(nic_dev, drv, netdev,
958 "Current firmware doesn't support to set vf spoofchk, need to upgrade latest firmware version\n");
959 err = -EOPNOTSUPP;
960 }
961
962 return err;
963}
964
965static int hinic_set_vf_link_state(struct hinic_hwdev *hwdev, u16 vf_id,
966 int link)
967{
968 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
969 struct vf_data_storage *vf_infos = nic_io->vf_infos;
970 u8 link_status = 0;
971
972 switch (link) {
973 case HINIC_IFLA_VF_LINK_STATE_AUTO:
974 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = false;
975 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = nic_io->link_status ?
976 true : false;
977 link_status = nic_io->link_status;
978 break;
979 case HINIC_IFLA_VF_LINK_STATE_ENABLE:
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 = true;
982 link_status = HINIC_LINK_UP;
983 break;
984 case HINIC_IFLA_VF_LINK_STATE_DISABLE:
985 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
986 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = false;
987 link_status = HINIC_LINK_DOWN;
988 break;
989 default:
990 return -EINVAL;
991 }
992
993
994 hinic_notify_vf_link_status(hwdev, vf_id, link_status);
995
996 return 0;
997}
998
999int hinic_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
1000{
1001 struct hinic_dev *nic_dev = netdev_priv(netdev);
1002 struct hinic_sriov_info *sriov_info;
1003
1004 sriov_info = &nic_dev->sriov_info;
1005
1006 if (vf_id >= sriov_info->num_vfs) {
1007 netif_err(nic_dev, drv, netdev,
1008 "Invalid VF Identifier %d\n", vf_id);
1009 return -EINVAL;
1010 }
1011
1012 return hinic_set_vf_link_state(sriov_info->hwdev,
1013 OS_VF_ID_TO_HW(vf_id), link);
1014}
1015
1016
1017static int nic_pf_mbox_handler(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1018 u16 in_size, void *buf_out, u16 *out_size)
1019{
1020 u8 size = ARRAY_SIZE(nic_cmd_support_vf);
1021 struct vf_cmd_msg_handle *vf_msg_handle;
1022 struct hinic_hwdev *dev = hwdev;
1023 struct hinic_func_to_io *nic_io;
1024 struct hinic_pfhwdev *pfhwdev;
1025 int err = 0;
1026 u32 i;
1027
1028 if (!hwdev)
1029 return -EINVAL;
1030
1031 if (!hinic_mbox_check_cmd_valid(hwdev, nic_cmd_support_vf, vf_id, cmd,
1032 buf_in, in_size, size)) {
1033 dev_err(&dev->hwif->pdev->dev,
1034 "PF Receive VF nic cmd: 0x%x, mbox len: 0x%x is invalid\n",
1035 cmd, in_size);
1036 return HINIC_MBOX_VF_CMD_ERROR;
1037 }
1038
1039 pfhwdev = container_of(dev, struct hinic_pfhwdev, hwdev);
1040 nic_io = &dev->func_to_io;
1041 for (i = 0; i < ARRAY_SIZE(nic_vf_cmd_msg_handler); i++) {
1042 vf_msg_handle = &nic_vf_cmd_msg_handler[i];
1043 if (cmd == vf_msg_handle->cmd &&
1044 vf_msg_handle->cmd_msg_handler) {
1045 err = vf_msg_handle->cmd_msg_handler(hwdev, vf_id,
1046 buf_in, in_size,
1047 buf_out,
1048 out_size);
1049 break;
1050 }
1051 }
1052 if (i == ARRAY_SIZE(nic_vf_cmd_msg_handler))
1053 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC,
1054 cmd, buf_in, in_size, buf_out,
1055 out_size, HINIC_MGMT_MSG_SYNC);
1056
1057 if (err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW)
1058 dev_err(&nic_io->hwif->pdev->dev, "PF receive VF L2NIC cmd: %d process error, err:%d\n",
1059 cmd, err);
1060 return err;
1061}
1062
1063static int cfg_mbx_pf_proc_vf_msg(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1064 u16 in_size, void *buf_out, u16 *out_size)
1065{
1066 struct hinic_dev_cap *dev_cap = buf_out;
1067 struct hinic_hwdev *dev = hwdev;
1068 struct hinic_cap *cap;
1069
1070 cap = &dev->nic_cap;
1071 memset(dev_cap, 0, sizeof(*dev_cap));
1072
1073 dev_cap->max_vf = cap->max_vf;
1074 dev_cap->max_sqs = cap->max_vf_qps;
1075 dev_cap->max_rqs = cap->max_vf_qps;
1076 dev_cap->port_id = dev->port_id;
1077
1078 *out_size = sizeof(*dev_cap);
1079
1080 return 0;
1081}
1082
1083static int hinic_init_vf_infos(struct hinic_func_to_io *nic_io, u16 vf_id)
1084{
1085 struct vf_data_storage *vf_infos = nic_io->vf_infos;
1086
1087 if (set_vf_link_state > HINIC_IFLA_VF_LINK_STATE_DISABLE) {
1088 dev_warn(&nic_io->hwif->pdev->dev, "Module Parameter set_vf_link_state value %d is out of range, resetting to %d\n",
1089 set_vf_link_state, HINIC_IFLA_VF_LINK_STATE_AUTO);
1090 set_vf_link_state = HINIC_IFLA_VF_LINK_STATE_AUTO;
1091 }
1092
1093 switch (set_vf_link_state) {
1094 case HINIC_IFLA_VF_LINK_STATE_AUTO:
1095 vf_infos[vf_id].link_forced = false;
1096 break;
1097 case HINIC_IFLA_VF_LINK_STATE_ENABLE:
1098 vf_infos[vf_id].link_forced = true;
1099 vf_infos[vf_id].link_up = true;
1100 break;
1101 case HINIC_IFLA_VF_LINK_STATE_DISABLE:
1102 vf_infos[vf_id].link_forced = true;
1103 vf_infos[vf_id].link_up = false;
1104 break;
1105 default:
1106 dev_err(&nic_io->hwif->pdev->dev, "Invalid input parameter set_vf_link_state: %d\n",
1107 set_vf_link_state);
1108 return -EINVAL;
1109 }
1110
1111 return 0;
1112}
1113
1114static void hinic_clear_vf_infos(struct hinic_dev *nic_dev, u16 vf_id)
1115{
1116 struct vf_data_storage *vf_infos;
1117
1118 vf_infos = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
1119 if (vf_infos->pf_set_mac)
1120 hinic_port_del_mac(nic_dev, vf_infos->vf_mac_addr, 0);
1121
1122 if (hinic_vf_info_vlanprio(nic_dev->hwdev, vf_id))
1123 hinic_kill_vf_vlan(nic_dev->hwdev, vf_id);
1124
1125 if (vf_infos->max_rate)
1126 hinic_set_vf_tx_rate(nic_dev->hwdev, vf_id, 0, 0);
1127
1128 if (vf_infos->spoofchk)
1129 hinic_set_vf_spoofchk(nic_dev->hwdev, vf_id, false);
1130
1131 if (vf_infos->trust)
1132 hinic_set_vf_trust(nic_dev->hwdev, vf_id, false);
1133
1134 memset(vf_infos, 0, sizeof(*vf_infos));
1135
1136 hinic_init_vf_infos(&nic_dev->hwdev->func_to_io, HW_VF_ID_TO_OS(vf_id));
1137}
1138
1139static int hinic_deinit_vf_hw(struct hinic_sriov_info *sriov_info,
1140 u16 start_vf_id, u16 end_vf_id)
1141{
1142 struct hinic_dev *nic_dev;
1143 u16 func_idx, idx;
1144
1145 nic_dev = container_of(sriov_info, struct hinic_dev, sriov_info);
1146
1147 for (idx = start_vf_id; idx <= end_vf_id; idx++) {
1148 func_idx = hinic_glb_pf_vf_offset(nic_dev->hwdev->hwif) + idx;
1149 hinic_set_wq_page_size(nic_dev->hwdev, func_idx,
1150 HINIC_HW_WQ_PAGE_SIZE);
1151 hinic_clear_vf_infos(nic_dev, idx);
1152 }
1153
1154 return 0;
1155}
1156
1157int hinic_vf_func_init(struct hinic_hwdev *hwdev)
1158{
1159 struct hinic_register_vf register_info = {0};
1160 u16 out_size = sizeof(register_info);
1161 struct hinic_func_to_io *nic_io;
1162 int err = 0;
1163 u32 size, i;
1164
1165 err = hinic_vf_mbox_random_id_init(hwdev);
1166 if (err) {
1167 dev_err(&hwdev->hwif->pdev->dev, "Failed to init vf mbox random id, err: %d\n",
1168 err);
1169 return err;
1170 }
1171
1172 nic_io = &hwdev->func_to_io;
1173
1174 if (HINIC_IS_VF(hwdev->hwif)) {
1175 err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1176 HINIC_PORT_CMD_VF_REGISTER,
1177 ®ister_info, sizeof(register_info),
1178 ®ister_info, &out_size, 0);
1179 if (err || register_info.status || !out_size) {
1180 dev_err(&hwdev->hwif->pdev->dev,
1181 "Failed to register VF, err: %d, status: 0x%x, out size: 0x%x\n",
1182 err, register_info.status, out_size);
1183 hinic_unregister_vf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1184 return -EIO;
1185 }
1186 } else {
1187 err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_CFGM,
1188 cfg_mbx_pf_proc_vf_msg);
1189 if (err) {
1190 dev_err(&hwdev->hwif->pdev->dev,
1191 "Register PF mailbox callback failed\n");
1192 return err;
1193 }
1194 nic_io->max_vfs = hwdev->nic_cap.max_vf;
1195 size = sizeof(*nic_io->vf_infos) * nic_io->max_vfs;
1196 if (size != 0) {
1197 nic_io->vf_infos = kzalloc(size, GFP_KERNEL);
1198 if (!nic_io->vf_infos) {
1199 err = -ENOMEM;
1200 goto out_free_nic_io;
1201 }
1202
1203 for (i = 0; i < nic_io->max_vfs; i++) {
1204 err = hinic_init_vf_infos(nic_io, i);
1205 if (err)
1206 goto err_init_vf_infos;
1207 }
1208
1209 err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC,
1210 nic_pf_mbox_handler);
1211 if (err)
1212 goto err_register_pf_mbox_cb;
1213 }
1214 }
1215
1216 return 0;
1217
1218err_register_pf_mbox_cb:
1219err_init_vf_infos:
1220 kfree(nic_io->vf_infos);
1221out_free_nic_io:
1222 return err;
1223}
1224
1225void hinic_vf_func_free(struct hinic_hwdev *hwdev)
1226{
1227 struct hinic_register_vf unregister = {0};
1228 u16 out_size = sizeof(unregister);
1229 int err;
1230
1231 if (HINIC_IS_VF(hwdev->hwif)) {
1232 err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1233 HINIC_PORT_CMD_VF_UNREGISTER,
1234 &unregister, sizeof(unregister),
1235 &unregister, &out_size, 0);
1236 if (err || !out_size || unregister.status)
1237 dev_err(&hwdev->hwif->pdev->dev, "Failed to unregister VF, err: %d, status: 0x%x, out_size: 0x%x\n",
1238 err, unregister.status, out_size);
1239 } else {
1240 if (hwdev->func_to_io.vf_infos) {
1241 hinic_unregister_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1242 kfree(hwdev->func_to_io.vf_infos);
1243 }
1244 }
1245}
1246
1247static int hinic_init_vf_hw(struct hinic_hwdev *hwdev, u16 start_vf_id,
1248 u16 end_vf_id)
1249{
1250 u16 i, func_idx;
1251 int err;
1252
1253
1254 for (i = start_vf_id; i <= end_vf_id; i++) {
1255 func_idx = hinic_glb_pf_vf_offset(hwdev->hwif) + i;
1256 err = hinic_set_wq_page_size(hwdev, func_idx,
1257 HINIC_DEFAULT_WQ_PAGE_SIZE);
1258 if (err)
1259 return err;
1260 }
1261
1262 return 0;
1263}
1264
1265int hinic_pci_sriov_disable(struct pci_dev *pdev)
1266{
1267 struct hinic_sriov_info *sriov_info;
1268 u16 tmp_vfs;
1269
1270 sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1271
1272 if (!sriov_info->sriov_enabled)
1273 return 0;
1274
1275 set_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1276
1277
1278
1279
1280
1281 if (pci_vfs_assigned(sriov_info->pdev)) {
1282 clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1283 dev_warn(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
1284 return -EPERM;
1285 }
1286 sriov_info->sriov_enabled = false;
1287
1288
1289 pci_disable_sriov(sriov_info->pdev);
1290
1291 tmp_vfs = (u16)sriov_info->num_vfs;
1292 sriov_info->num_vfs = 0;
1293 hinic_deinit_vf_hw(sriov_info, OS_VF_ID_TO_HW(0),
1294 OS_VF_ID_TO_HW(tmp_vfs - 1));
1295
1296 clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1297
1298 return 0;
1299}
1300
1301int hinic_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1302{
1303 struct hinic_sriov_info *sriov_info;
1304 int err;
1305
1306 sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1307
1308 if (test_and_set_bit(HINIC_SRIOV_ENABLE, &sriov_info->state)) {
1309 dev_err(&pdev->dev,
1310 "SR-IOV enable in process, please wait, num_vfs %d\n",
1311 num_vfs);
1312 return -EPERM;
1313 }
1314
1315 err = hinic_init_vf_hw(sriov_info->hwdev, OS_VF_ID_TO_HW(0),
1316 OS_VF_ID_TO_HW((u16)num_vfs - 1));
1317 if (err) {
1318 dev_err(&sriov_info->pdev->dev,
1319 "Failed to init vf in hardware before enable sriov, error %d\n",
1320 err);
1321 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1322 return err;
1323 }
1324
1325 err = pci_enable_sriov(sriov_info->pdev, num_vfs);
1326 if (err) {
1327 dev_err(&pdev->dev,
1328 "Failed to enable SR-IOV, error %d\n", err);
1329 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1330 return err;
1331 }
1332
1333 sriov_info->sriov_enabled = true;
1334 sriov_info->num_vfs = num_vfs;
1335 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1336
1337 return num_vfs;
1338}
1339
1340int hinic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1341{
1342 struct hinic_sriov_info *sriov_info;
1343
1344 sriov_info = hinic_get_sriov_info_by_pcidev(dev);
1345
1346 if (test_bit(HINIC_FUNC_REMOVE, &sriov_info->state))
1347 return -EBUSY;
1348
1349 if (!num_vfs)
1350 return hinic_pci_sriov_disable(dev);
1351 else
1352 return hinic_pci_sriov_enable(dev, num_vfs);
1353}
1354