1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Huawei HiNIC PCI Express Linux driver
3 * Copyright(c) 2017 Huawei Technologies Co., Ltd
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
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.");
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
hinic_set_mac(struct hinic_hwdev * hwdev,const u8 * mac_addr,u16 vlan_id,u16 func_id)27 static 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
hinic_notify_vf_link_status(struct hinic_hwdev * hwdev,u16 vf_id,u8 link_status)50 static 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 /* send link change event mbox msg to active vfs under the pf */
hinic_notify_all_vfs_link_changed(struct hinic_hwdev * hwdev,u8 link_status)74 void 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
hinic_vf_info_vlanprio(struct hinic_hwdev * hwdev,int vf_id)87 static 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
hinic_set_vf_vlan(struct hinic_hwdev * hwdev,bool add,u16 vid,u8 qos,int vf_id)100 static 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 /* VLAN 0 is a special case, don't allow it to be removed */
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
hinic_set_vf_tx_rate_max_min(struct hinic_hwdev * hwdev,u16 vf_id,u32 max_rate,u32 min_rate)132 static 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
hinic_set_vf_rate_limit(struct hinic_hwdev * hwdev,u16 vf_id,u32 tx_rate)162 static 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
hinic_set_vf_tx_rate(struct hinic_hwdev * hwdev,u16 vf_id,u32 max_rate,u32 min_rate)191 static 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
hinic_init_vf_config(struct hinic_hwdev * hwdev,u16 vf_id)210 static 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
hinic_register_vf_msg_handler(void * hwdev,u16 vf_id,void * buf_in,u16 in_size,void * buf_out,u16 * out_size)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)
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
hinic_unregister_vf_msg_handler(void * hwdev,u16 vf_id,void * buf_in,u16 in_size,void * buf_out,u16 * out_size)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)
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
hinic_change_vf_mtu_msg_handler(void * hwdev,u16 vf_id,void * buf_in,u16 in_size,void * buf_out,u16 * out_size)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)
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
hinic_get_vf_mac_msg_handler(void * hwdev,u16 vf_id,void * buf_in,u16 in_size,void * buf_out,u16 * out_size)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)
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
hinic_set_vf_mac_msg_handler(void * hwdev,u16 vf_id,void * buf_in,u16 in_size,void * buf_out,u16 * out_size)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)
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
hinic_del_vf_mac_msg_handler(void * hwdev,u16 vf_id,void * buf_in,u16 in_size,void * buf_out,u16 * 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)
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
hinic_get_vf_link_status_msg_handler(void * hwdev,u16 vf_id,void * buf_in,u16 in_size,void * buf_out,u16 * 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)
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
check_func_table(struct hinic_hwdev * hwdev,u16 func_idx,void * buf_in,u16 in_size)431 static 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
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},
451 };
452
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},
490 };
491
492 #define CHECK_IPSU_15BIT 0X8000
493
494 static
hinic_get_sriov_info_by_pcidev(struct pci_dev * pdev)495 struct 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
hinic_check_mac_info(u8 status,u16 vlan_id)503 static 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
hinic_update_mac(struct hinic_hwdev * hwdev,u8 * old_mac,u8 * new_mac,u16 vlan_id,u16 func_id)515 static 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
hinic_get_vf_config(struct hinic_hwdev * hwdev,u16 vf_id,struct ifla_vf_info * ivi)553 static 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
hinic_ndo_get_vf_config(struct net_device * netdev,int vf,struct ifla_vf_info * ivi)577 int 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
hinic_set_vf_mac(struct hinic_hwdev * hwdev,int vf,unsigned char * mac_addr)592 static 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 /* duplicate request, so just return success */
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
hinic_ndo_set_vf_mac(struct net_device * netdev,int vf,u8 * mac)622 int 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
hinic_add_vf_vlan(struct hinic_hwdev * hwdev,int vf_id,u16 vlan,u8 qos)642 static 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
hinic_kill_vf_vlan(struct hinic_hwdev * hwdev,int vf_id)660 static 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
hinic_update_mac_vlan(struct hinic_dev * nic_dev,u16 old_vlan,u16 new_vlan,int vf_id)682 static 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
720 out:
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
set_hw_vf_vlan(struct hinic_dev * nic_dev,u16 cur_vlanprio,int vf,u16 vlan,u8 qos)729 static 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
764 out:
765 return err;
766 }
767
hinic_ndo_set_vf_vlan(struct net_device * netdev,int vf,u16 vlan,u8 qos,__be16 vlan_proto)768 int 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 /* duplicate request, so just return success */
784 if (vlanprio == cur_vlanprio)
785 return 0;
786
787 return set_hw_vf_vlan(nic_dev, cur_vlanprio, vf, vlan, qos);
788 }
789
hinic_set_vf_trust(struct hinic_hwdev * hwdev,u16 vf_id,bool trust)790 static 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
hinic_ndo_set_vf_trust(struct net_device * netdev,int vf,bool setting)806 int 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 /* same request, so just return success */
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
hinic_ndo_set_vf_bw(struct net_device * netdev,int vf,int min_tx_rate,int max_tx_rate)836 int hinic_ndo_set_vf_bw(struct net_device *netdev,
837 int vf, int min_tx_rate, int max_tx_rate)
838 {
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;
844 int err;
845
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);
849 return -EINVAL;
850 }
851
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);
855 return -EINVAL;
856 }
857
858 err = hinic_port_link_state(nic_dev, &link_state);
859 if (err) {
860 netif_err(nic_dev, drv, netdev,
861 "Get link status failed when setting vf tx rate\n");
862 return -EIO;
863 }
864
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");
868 return -EPERM;
869 }
870
871 err = hinic_port_get_cap(nic_dev, &port_cap);
872 if (err || port_cap.speed > LINK_SPEED_100GB)
873 return -EIO;
874
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]);
879 return -EINVAL;
880 }
881
882 err = hinic_set_vf_tx_rate(nic_dev->hwdev, OS_VF_ID_TO_HW(vf),
883 max_tx_rate, min_tx_rate);
884 if (err) {
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" : "");
890 return -EIO;
891 }
892
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);
896
897 return 0;
898 }
899
hinic_set_vf_spoofchk(struct hinic_hwdev * hwdev,u16 vf_id,bool spoofchk)900 static int hinic_set_vf_spoofchk(struct hinic_hwdev *hwdev, u16 vf_id,
901 bool spoofchk)
902 {
903 struct hinic_spoofchk_set spoofchk_cfg = {0};
904 struct vf_data_storage *vf_infos = NULL;
905 u16 out_size = sizeof(spoofchk_cfg);
906 int err;
907
908 if (!hwdev)
909 return -EINVAL;
910
911 vf_infos = hwdev->func_to_io.vf_infos;
912
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,
923 out_size);
924 err = -EIO;
925 }
926
927 vf_infos[HW_VF_ID_TO_OS(vf_id)].spoofchk = spoofchk;
928
929 return err;
930 }
931
hinic_ndo_set_vf_spoofchk(struct net_device * netdev,int vf,bool setting)932 int hinic_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
933 {
934 struct hinic_dev *nic_dev = netdev_priv(netdev);
935 struct hinic_sriov_info *sriov_info;
936 bool cur_spoofchk;
937 int err;
938
939 sriov_info = &nic_dev->sriov_info;
940 if (vf >= sriov_info->num_vfs)
941 return -EINVAL;
942
943 cur_spoofchk = nic_dev->hwdev->func_to_io.vf_infos[vf].spoofchk;
944
945 /* same request, so just return success */
946 if ((setting && cur_spoofchk) || (!setting && !cur_spoofchk))
947 return 0;
948
949 err = hinic_set_vf_spoofchk(sriov_info->hwdev,
950 OS_VF_ID_TO_HW(vf), setting);
951 if (!err) {
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");
957 err = -EOPNOTSUPP;
958 }
959
960 return err;
961 }
962
hinic_set_vf_link_state(struct hinic_hwdev * hwdev,u16 vf_id,int link)963 static int hinic_set_vf_link_state(struct hinic_hwdev *hwdev, u16 vf_id,
964 int link)
965 {
966 struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
967 struct vf_data_storage *vf_infos = nic_io->vf_infos;
968 u8 link_status = 0;
969
970 switch (link) {
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 ?
974 true : false;
975 link_status = nic_io->link_status;
976 break;
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;
981 break;
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;
986 break;
987 default:
988 return -EINVAL;
989 }
990
991 /* Notify the VF of its new link state */
992 hinic_notify_vf_link_status(hwdev, vf_id, link_status);
993
994 return 0;
995 }
996
hinic_ndo_set_vf_link_state(struct net_device * netdev,int vf_id,int link)997 int hinic_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
998 {
999 struct hinic_dev *nic_dev = netdev_priv(netdev);
1000 struct hinic_sriov_info *sriov_info;
1001
1002 sriov_info = &nic_dev->sriov_info;
1003
1004 if (vf_id >= sriov_info->num_vfs) {
1005 netif_err(nic_dev, drv, netdev,
1006 "Invalid VF Identifier %d\n", vf_id);
1007 return -EINVAL;
1008 }
1009
1010 return hinic_set_vf_link_state(sriov_info->hwdev,
1011 OS_VF_ID_TO_HW(vf_id), link);
1012 }
1013
1014 /* pf receive message from vf */
nic_pf_mbox_handler(void * hwdev,u16 vf_id,u8 cmd,void * buf_in,u16 in_size,void * buf_out,u16 * out_size)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)
1017 {
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;
1023 int err = 0;
1024 u32 i;
1025
1026 if (!hwdev)
1027 return -EINVAL;
1028
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",
1033 cmd, in_size);
1034 return HINIC_MBOX_VF_CMD_ERROR;
1035 }
1036
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,
1044 buf_in, in_size,
1045 buf_out,
1046 out_size);
1047 break;
1048 }
1049 }
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);
1054
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",
1057 cmd, err);
1058 return err;
1059 }
1060
cfg_mbx_pf_proc_vf_msg(void * hwdev,u16 vf_id,u8 cmd,void * buf_in,u16 in_size,void * buf_out,u16 * out_size)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)
1063 {
1064 struct hinic_dev_cap *dev_cap = buf_out;
1065 struct hinic_hwdev *dev = hwdev;
1066 struct hinic_cap *cap;
1067
1068 cap = &dev->nic_cap;
1069 memset(dev_cap, 0, sizeof(*dev_cap));
1070
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;
1075
1076 *out_size = sizeof(*dev_cap);
1077
1078 return 0;
1079 }
1080
hinic_init_vf_infos(struct hinic_func_to_io * nic_io,u16 vf_id)1081 static int hinic_init_vf_infos(struct hinic_func_to_io *nic_io, u16 vf_id)
1082 {
1083 struct vf_data_storage *vf_infos = nic_io->vf_infos;
1084
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;
1089 }
1090
1091 switch (set_vf_link_state) {
1092 case HINIC_IFLA_VF_LINK_STATE_AUTO:
1093 vf_infos[vf_id].link_forced = false;
1094 break;
1095 case HINIC_IFLA_VF_LINK_STATE_ENABLE:
1096 vf_infos[vf_id].link_forced = true;
1097 vf_infos[vf_id].link_up = true;
1098 break;
1099 case HINIC_IFLA_VF_LINK_STATE_DISABLE:
1100 vf_infos[vf_id].link_forced = true;
1101 vf_infos[vf_id].link_up = false;
1102 break;
1103 default:
1104 dev_err(&nic_io->hwif->pdev->dev, "Invalid input parameter set_vf_link_state: %d\n",
1105 set_vf_link_state);
1106 return -EINVAL;
1107 }
1108
1109 return 0;
1110 }
1111
hinic_clear_vf_infos(struct hinic_dev * nic_dev,u16 vf_id)1112 static void hinic_clear_vf_infos(struct hinic_dev *nic_dev, u16 vf_id)
1113 {
1114 struct vf_data_storage *vf_infos;
1115
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);
1119
1120 if (hinic_vf_info_vlanprio(nic_dev->hwdev, vf_id))
1121 hinic_kill_vf_vlan(nic_dev->hwdev, vf_id);
1122
1123 if (vf_infos->max_rate)
1124 hinic_set_vf_tx_rate(nic_dev->hwdev, vf_id, 0, 0);
1125
1126 if (vf_infos->spoofchk)
1127 hinic_set_vf_spoofchk(nic_dev->hwdev, vf_id, false);
1128
1129 if (vf_infos->trust)
1130 hinic_set_vf_trust(nic_dev->hwdev, vf_id, false);
1131
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));
1135 }
1136
hinic_deinit_vf_hw(struct hinic_sriov_info * sriov_info,u16 start_vf_id,u16 end_vf_id)1137 static int hinic_deinit_vf_hw(struct hinic_sriov_info *sriov_info,
1138 u16 start_vf_id, u16 end_vf_id)
1139 {
1140 struct hinic_dev *nic_dev;
1141 u16 func_idx, idx;
1142
1143 nic_dev = container_of(sriov_info, struct hinic_dev, sriov_info);
1144
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);
1150 }
1151
1152 return 0;
1153 }
1154
hinic_vf_func_init(struct hinic_hwdev * hwdev)1155 int hinic_vf_func_init(struct hinic_hwdev *hwdev)
1156 {
1157 struct hinic_register_vf register_info = {0};
1158 u16 out_size = sizeof(register_info);
1159 struct hinic_func_to_io *nic_io;
1160 int err = 0;
1161 u32 size, i;
1162
1163 err = hinic_vf_mbox_random_id_init(hwdev);
1164 if (err) {
1165 dev_err(&hwdev->hwif->pdev->dev, "Failed to init vf mbox random id, err: %d\n",
1166 err);
1167 return err;
1168 }
1169
1170 nic_io = &hwdev->func_to_io;
1171
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 return -EIO;
1182 }
1183 } else {
1184 err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_CFGM,
1185 cfg_mbx_pf_proc_vf_msg);
1186 if (err) {
1187 dev_err(&hwdev->hwif->pdev->dev,
1188 "Register PF mailbox callback failed\n");
1189 return err;
1190 }
1191 nic_io->max_vfs = hwdev->nic_cap.max_vf;
1192 size = sizeof(*nic_io->vf_infos) * nic_io->max_vfs;
1193 if (size != 0) {
1194 nic_io->vf_infos = kzalloc(size, GFP_KERNEL);
1195 if (!nic_io->vf_infos) {
1196 err = -ENOMEM;
1197 goto out_free_nic_io;
1198 }
1199
1200 for (i = 0; i < nic_io->max_vfs; i++) {
1201 err = hinic_init_vf_infos(nic_io, i);
1202 if (err)
1203 goto err_init_vf_infos;
1204 }
1205
1206 err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC,
1207 nic_pf_mbox_handler);
1208 if (err)
1209 goto err_register_pf_mbox_cb;
1210 }
1211 }
1212
1213 return 0;
1214
1215 err_register_pf_mbox_cb:
1216 err_init_vf_infos:
1217 kfree(nic_io->vf_infos);
1218 out_free_nic_io:
1219 return err;
1220 }
1221
hinic_vf_func_free(struct hinic_hwdev * hwdev)1222 void hinic_vf_func_free(struct hinic_hwdev *hwdev)
1223 {
1224 struct hinic_register_vf unregister = {0};
1225 u16 out_size = sizeof(unregister);
1226 int err;
1227
1228 if (HINIC_IS_VF(hwdev->hwif)) {
1229 err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1230 HINIC_PORT_CMD_VF_UNREGISTER,
1231 &unregister, sizeof(unregister),
1232 &unregister, &out_size, 0);
1233 if (err || !out_size || unregister.status)
1234 dev_err(&hwdev->hwif->pdev->dev, "Failed to unregister VF, err: %d, status: 0x%x, out_size: 0x%x\n",
1235 err, unregister.status, out_size);
1236 } else {
1237 if (hwdev->func_to_io.vf_infos) {
1238 hinic_unregister_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1239 kfree(hwdev->func_to_io.vf_infos);
1240 }
1241 }
1242 }
1243
hinic_init_vf_hw(struct hinic_hwdev * hwdev,u16 start_vf_id,u16 end_vf_id)1244 static int hinic_init_vf_hw(struct hinic_hwdev *hwdev, u16 start_vf_id,
1245 u16 end_vf_id)
1246 {
1247 u16 i, func_idx;
1248 int err;
1249
1250 /* vf use 256K as default wq page size, and can't change it */
1251 for (i = start_vf_id; i <= end_vf_id; i++) {
1252 func_idx = hinic_glb_pf_vf_offset(hwdev->hwif) + i;
1253 err = hinic_set_wq_page_size(hwdev, func_idx,
1254 HINIC_DEFAULT_WQ_PAGE_SIZE);
1255 if (err)
1256 return err;
1257 }
1258
1259 return 0;
1260 }
1261
hinic_pci_sriov_disable(struct pci_dev * pdev)1262 int hinic_pci_sriov_disable(struct pci_dev *pdev)
1263 {
1264 struct hinic_sriov_info *sriov_info;
1265 u16 tmp_vfs;
1266
1267 sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1268 /* if SR-IOV is already disabled then nothing will be done */
1269 if (!sriov_info->sriov_enabled)
1270 return 0;
1271
1272 set_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1273
1274 /* If our VFs are assigned we cannot shut down SR-IOV
1275 * without causing issues, so just leave the hardware
1276 * available but disabled
1277 */
1278 if (pci_vfs_assigned(sriov_info->pdev)) {
1279 clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1280 dev_warn(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
1281 return -EPERM;
1282 }
1283 sriov_info->sriov_enabled = false;
1284
1285 /* disable iov and allow time for transactions to clear */
1286 pci_disable_sriov(sriov_info->pdev);
1287
1288 tmp_vfs = (u16)sriov_info->num_vfs;
1289 sriov_info->num_vfs = 0;
1290 hinic_deinit_vf_hw(sriov_info, OS_VF_ID_TO_HW(0),
1291 OS_VF_ID_TO_HW(tmp_vfs - 1));
1292
1293 clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1294
1295 return 0;
1296 }
1297
hinic_pci_sriov_enable(struct pci_dev * pdev,int num_vfs)1298 int hinic_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1299 {
1300 struct hinic_sriov_info *sriov_info;
1301 int err;
1302
1303 sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1304
1305 if (test_and_set_bit(HINIC_SRIOV_ENABLE, &sriov_info->state)) {
1306 dev_err(&pdev->dev,
1307 "SR-IOV enable in process, please wait, num_vfs %d\n",
1308 num_vfs);
1309 return -EPERM;
1310 }
1311
1312 err = hinic_init_vf_hw(sriov_info->hwdev, OS_VF_ID_TO_HW(0),
1313 OS_VF_ID_TO_HW((u16)num_vfs - 1));
1314 if (err) {
1315 dev_err(&sriov_info->pdev->dev,
1316 "Failed to init vf in hardware before enable sriov, error %d\n",
1317 err);
1318 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1319 return err;
1320 }
1321
1322 err = pci_enable_sriov(sriov_info->pdev, num_vfs);
1323 if (err) {
1324 dev_err(&pdev->dev,
1325 "Failed to enable SR-IOV, error %d\n", err);
1326 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1327 return err;
1328 }
1329
1330 sriov_info->sriov_enabled = true;
1331 sriov_info->num_vfs = num_vfs;
1332 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1333
1334 return num_vfs;
1335 }
1336
hinic_pci_sriov_configure(struct pci_dev * dev,int num_vfs)1337 int hinic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1338 {
1339 struct hinic_sriov_info *sriov_info;
1340
1341 sriov_info = hinic_get_sriov_info_by_pcidev(dev);
1342
1343 if (test_bit(HINIC_FUNC_REMOVE, &sriov_info->state))
1344 return -EBUSY;
1345
1346 if (!num_vfs)
1347 return hinic_pci_sriov_disable(dev);
1348 else
1349 return hinic_pci_sriov_enable(dev, num_vfs);
1350 }
1351