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 #include <linux/module.h> 12 13 #include "hinic_hw_dev.h" 14 #include "hinic_dev.h" 15 #include "hinic_hw_mbox.h" 16 #include "hinic_hw_cmdq.h" 17 #include "hinic_port.h" 18 #include "hinic_sriov.h" 19 20 static unsigned char set_vf_link_state; 21 module_param(set_vf_link_state, byte, 0444); 22 MODULE_PARM_DESC(set_vf_link_state, "Set vf link state, 0 represents link auto, 1 represents link always up, 2 represents link always down. - default is 0."); 23 24 #define HINIC_VLAN_PRIORITY_SHIFT 13 25 #define HINIC_ADD_VLAN_IN_MAC 0x8000 26 #define HINIC_TX_RATE_TABLE_FULL 12 27 #define HINIC_MAX_QOS 7 28 29 static int hinic_set_mac(struct hinic_hwdev *hwdev, const u8 *mac_addr, 30 u16 vlan_id, u16 func_id) 31 { 32 struct hinic_port_mac_cmd mac_info = {0}; 33 u16 out_size = sizeof(mac_info); 34 int err; 35 36 mac_info.func_idx = func_id; 37 mac_info.vlan_id = vlan_id; 38 memcpy(mac_info.mac, mac_addr, ETH_ALEN); 39 40 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_MAC, &mac_info, 41 sizeof(mac_info), &mac_info, &out_size); 42 if (err || out_size != sizeof(mac_info) || 43 (mac_info.status && mac_info.status != HINIC_MGMT_STATUS_EXIST)) { 44 dev_err(&hwdev->func_to_io.hwif->pdev->dev, "Failed to set MAC, err: %d, status: 0x%x, out size: 0x%x\n", 45 err, mac_info.status, out_size); 46 return -EIO; 47 } 48 49 return 0; 50 } 51 52 static void hinic_notify_vf_link_status(struct hinic_hwdev *hwdev, u16 vf_id, 53 u8 link_status) 54 { 55 struct vf_data_storage *vf_infos = hwdev->func_to_io.vf_infos; 56 struct hinic_port_link_status link = {0}; 57 u16 out_size = sizeof(link); 58 int err; 59 60 if (vf_infos[HW_VF_ID_TO_OS(vf_id)].registered) { 61 link.link = link_status; 62 link.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id; 63 err = hinic_mbox_to_vf(hwdev, HINIC_MOD_L2NIC, 64 vf_id, HINIC_PORT_CMD_LINK_STATUS_REPORT, 65 &link, sizeof(link), 66 &link, &out_size, 0); 67 if (err || !out_size || link.status) 68 dev_err(&hwdev->hwif->pdev->dev, 69 "Send link change event to VF %d failed, err: %d, status: 0x%x, out_size: 0x%x\n", 70 HW_VF_ID_TO_OS(vf_id), err, 71 link.status, out_size); 72 } 73 } 74 75 /* send link change event mbox msg to active vfs under the pf */ 76 void hinic_notify_all_vfs_link_changed(struct hinic_hwdev *hwdev, 77 u8 link_status) 78 { 79 struct hinic_func_to_io *nic_io = &hwdev->func_to_io; 80 u16 i; 81 82 nic_io->link_status = link_status; 83 for (i = 1; i <= nic_io->max_vfs; i++) { 84 if (!nic_io->vf_infos[HW_VF_ID_TO_OS(i)].link_forced) 85 hinic_notify_vf_link_status(hwdev, i, link_status); 86 } 87 } 88 89 static u16 hinic_vf_info_vlanprio(struct hinic_hwdev *hwdev, int vf_id) 90 { 91 struct hinic_func_to_io *nic_io = &hwdev->func_to_io; 92 u16 pf_vlan, vlanprio; 93 u8 pf_qos; 94 95 pf_vlan = nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan; 96 pf_qos = nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos; 97 vlanprio = pf_vlan | pf_qos << HINIC_VLAN_PRIORITY_SHIFT; 98 99 return vlanprio; 100 } 101 102 static int hinic_set_vf_vlan(struct hinic_hwdev *hwdev, bool add, u16 vid, 103 u8 qos, int vf_id) 104 { 105 struct hinic_vf_vlan_config vf_vlan = {0}; 106 u16 out_size = sizeof(vf_vlan); 107 int err; 108 u8 cmd; 109 110 /* VLAN 0 is a special case, don't allow it to be removed */ 111 if (!vid && !add) 112 return 0; 113 114 vf_vlan.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id; 115 vf_vlan.vlan_id = vid; 116 vf_vlan.qos = qos; 117 118 if (add) 119 cmd = HINIC_PORT_CMD_SET_VF_VLAN; 120 else 121 cmd = HINIC_PORT_CMD_CLR_VF_VLAN; 122 123 err = hinic_port_msg_cmd(hwdev, cmd, &vf_vlan, 124 sizeof(vf_vlan), &vf_vlan, &out_size); 125 if (err || !out_size || vf_vlan.status) { 126 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF %d vlan, err: %d, status: 0x%x, out size: 0x%x\n", 127 HW_VF_ID_TO_OS(vf_id), err, vf_vlan.status, out_size); 128 return -EFAULT; 129 } 130 131 return 0; 132 } 133 134 static int hinic_set_vf_tx_rate_max_min(struct hinic_hwdev *hwdev, u16 vf_id, 135 u32 max_rate, u32 min_rate) 136 { 137 struct hinic_func_to_io *nic_io = &hwdev->func_to_io; 138 struct hinic_tx_rate_cfg_max_min rate_cfg = {0}; 139 u16 out_size = sizeof(rate_cfg); 140 int err; 141 142 rate_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id; 143 rate_cfg.max_rate = max_rate; 144 rate_cfg.min_rate = min_rate; 145 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_VF_MAX_MIN_RATE, 146 &rate_cfg, sizeof(rate_cfg), &rate_cfg, 147 &out_size); 148 if ((rate_cfg.status != HINIC_MGMT_CMD_UNSUPPORTED && 149 rate_cfg.status) || err || !out_size) { 150 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) max rate(%d), min rate(%d), err: %d, status: 0x%x, out size: 0x%x\n", 151 HW_VF_ID_TO_OS(vf_id), max_rate, min_rate, err, 152 rate_cfg.status, out_size); 153 return -EIO; 154 } 155 156 if (!rate_cfg.status) { 157 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].max_rate = max_rate; 158 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].min_rate = min_rate; 159 } 160 161 return rate_cfg.status; 162 } 163 164 static int hinic_set_vf_rate_limit(struct hinic_hwdev *hwdev, u16 vf_id, 165 u32 tx_rate) 166 { 167 struct hinic_func_to_io *nic_io = &hwdev->func_to_io; 168 struct hinic_tx_rate_cfg rate_cfg = {0}; 169 u16 out_size = sizeof(rate_cfg); 170 int err; 171 172 rate_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id; 173 rate_cfg.tx_rate = tx_rate; 174 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_VF_RATE, 175 &rate_cfg, sizeof(rate_cfg), &rate_cfg, 176 &out_size); 177 if (err || !out_size || rate_cfg.status) { 178 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) rate(%d), err: %d, status: 0x%x, out size: 0x%x\n", 179 HW_VF_ID_TO_OS(vf_id), tx_rate, err, rate_cfg.status, 180 out_size); 181 if (rate_cfg.status) 182 return rate_cfg.status; 183 184 return -EIO; 185 } 186 187 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].max_rate = tx_rate; 188 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].min_rate = 0; 189 190 return 0; 191 } 192 193 static int hinic_set_vf_tx_rate(struct hinic_hwdev *hwdev, u16 vf_id, 194 u32 max_rate, u32 min_rate) 195 { 196 int err; 197 198 err = hinic_set_vf_tx_rate_max_min(hwdev, vf_id, max_rate, min_rate); 199 if (err != HINIC_MGMT_CMD_UNSUPPORTED) 200 return err; 201 202 if (min_rate) { 203 dev_err(&hwdev->hwif->pdev->dev, "Current firmware doesn't support to set min tx rate\n"); 204 return -EOPNOTSUPP; 205 } 206 207 dev_info(&hwdev->hwif->pdev->dev, "Current firmware doesn't support to set min tx rate, force min_tx_rate = max_tx_rate\n"); 208 209 return hinic_set_vf_rate_limit(hwdev, vf_id, max_rate); 210 } 211 212 static int hinic_init_vf_config(struct hinic_hwdev *hwdev, u16 vf_id) 213 { 214 struct vf_data_storage *vf_info; 215 u16 func_id, vlan_id; 216 int err = 0; 217 218 vf_info = hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id); 219 if (vf_info->pf_set_mac) { 220 func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id; 221 222 vlan_id = 0; 223 224 err = hinic_set_mac(hwdev, vf_info->vf_mac_addr, vlan_id, 225 func_id); 226 if (err) { 227 dev_err(&hwdev->func_to_io.hwif->pdev->dev, "Failed to set VF %d MAC\n", 228 HW_VF_ID_TO_OS(vf_id)); 229 return err; 230 } 231 } 232 233 if (hinic_vf_info_vlanprio(hwdev, vf_id)) { 234 err = hinic_set_vf_vlan(hwdev, true, vf_info->pf_vlan, 235 vf_info->pf_qos, vf_id); 236 if (err) { 237 dev_err(&hwdev->hwif->pdev->dev, "Failed to add VF %d VLAN_QOS\n", 238 HW_VF_ID_TO_OS(vf_id)); 239 return err; 240 } 241 } 242 243 if (vf_info->max_rate) { 244 err = hinic_set_vf_tx_rate(hwdev, vf_id, vf_info->max_rate, 245 vf_info->min_rate); 246 if (err) { 247 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF %d max rate: %d, min rate: %d\n", 248 HW_VF_ID_TO_OS(vf_id), vf_info->max_rate, 249 vf_info->min_rate); 250 return err; 251 } 252 } 253 254 return 0; 255 } 256 257 static int hinic_register_vf_msg_handler(void *hwdev, u16 vf_id, 258 void *buf_in, u16 in_size, 259 void *buf_out, u16 *out_size) 260 { 261 struct hinic_register_vf *register_info = buf_out; 262 struct hinic_hwdev *hw_dev = hwdev; 263 struct hinic_func_to_io *nic_io; 264 int err; 265 266 nic_io = &hw_dev->func_to_io; 267 if (vf_id > nic_io->max_vfs) { 268 dev_err(&hw_dev->hwif->pdev->dev, "Register VF id %d exceed limit[0-%d]\n", 269 HW_VF_ID_TO_OS(vf_id), HW_VF_ID_TO_OS(nic_io->max_vfs)); 270 register_info->status = EFAULT; 271 return -EFAULT; 272 } 273 274 *out_size = sizeof(*register_info); 275 err = hinic_init_vf_config(hw_dev, vf_id); 276 if (err) { 277 register_info->status = EFAULT; 278 return err; 279 } 280 281 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].registered = true; 282 283 return 0; 284 } 285 286 static int hinic_unregister_vf_msg_handler(void *hwdev, u16 vf_id, 287 void *buf_in, u16 in_size, 288 void *buf_out, u16 *out_size) 289 { 290 struct hinic_hwdev *hw_dev = hwdev; 291 struct hinic_func_to_io *nic_io; 292 293 nic_io = &hw_dev->func_to_io; 294 *out_size = 0; 295 if (vf_id > nic_io->max_vfs) 296 return 0; 297 298 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].registered = false; 299 300 return 0; 301 } 302 303 static int hinic_change_vf_mtu_msg_handler(void *hwdev, u16 vf_id, 304 void *buf_in, u16 in_size, 305 void *buf_out, u16 *out_size) 306 { 307 struct hinic_hwdev *hw_dev = hwdev; 308 int err; 309 310 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_CHANGE_MTU, buf_in, 311 in_size, buf_out, out_size); 312 if (err) { 313 dev_err(&hw_dev->hwif->pdev->dev, "Failed to set VF %u mtu\n", 314 vf_id); 315 return err; 316 } 317 318 return 0; 319 } 320 321 static int hinic_get_vf_mac_msg_handler(void *hwdev, u16 vf_id, 322 void *buf_in, u16 in_size, 323 void *buf_out, u16 *out_size) 324 { 325 struct hinic_port_mac_cmd *mac_info = buf_out; 326 struct hinic_hwdev *dev = hwdev; 327 struct hinic_func_to_io *nic_io; 328 struct vf_data_storage *vf_info; 329 330 nic_io = &dev->func_to_io; 331 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id); 332 333 memcpy(mac_info->mac, vf_info->vf_mac_addr, ETH_ALEN); 334 mac_info->status = 0; 335 *out_size = sizeof(*mac_info); 336 337 return 0; 338 } 339 340 static int hinic_set_vf_mac_msg_handler(void *hwdev, u16 vf_id, 341 void *buf_in, u16 in_size, 342 void *buf_out, u16 *out_size) 343 { 344 struct hinic_port_mac_cmd *mac_out = buf_out; 345 struct hinic_port_mac_cmd *mac_in = buf_in; 346 struct hinic_hwdev *hw_dev = hwdev; 347 struct hinic_func_to_io *nic_io; 348 struct vf_data_storage *vf_info; 349 int err; 350 351 nic_io = &hw_dev->func_to_io; 352 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id); 353 if (vf_info->pf_set_mac && !(vf_info->trust) && 354 is_valid_ether_addr(mac_in->mac)) { 355 dev_warn(&hw_dev->hwif->pdev->dev, "PF has already set VF %d MAC address\n", 356 HW_VF_ID_TO_OS(vf_id)); 357 mac_out->status = HINIC_PF_SET_VF_ALREADY; 358 *out_size = sizeof(*mac_out); 359 return 0; 360 } 361 362 err = hinic_port_msg_cmd(hw_dev, HINIC_PORT_CMD_SET_MAC, buf_in, 363 in_size, buf_out, out_size); 364 if ((err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW) || !(*out_size)) { 365 dev_err(&hw_dev->hwif->pdev->dev, 366 "Failed to set VF %d MAC address, err: %d, status: 0x%x, out size: 0x%x\n", 367 HW_VF_ID_TO_OS(vf_id), err, mac_out->status, *out_size); 368 return -EFAULT; 369 } 370 371 return err; 372 } 373 374 static int hinic_del_vf_mac_msg_handler(void *hwdev, u16 vf_id, 375 void *buf_in, u16 in_size, 376 void *buf_out, u16 *out_size) 377 { 378 struct hinic_port_mac_cmd *mac_out = buf_out; 379 struct hinic_port_mac_cmd *mac_in = buf_in; 380 struct hinic_hwdev *hw_dev = hwdev; 381 struct hinic_func_to_io *nic_io; 382 struct vf_data_storage *vf_info; 383 int err; 384 385 nic_io = &hw_dev->func_to_io; 386 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id); 387 if (vf_info->pf_set_mac && is_valid_ether_addr(mac_in->mac) && 388 !memcmp(vf_info->vf_mac_addr, mac_in->mac, ETH_ALEN)) { 389 dev_warn(&hw_dev->hwif->pdev->dev, "PF has already set VF mac.\n"); 390 mac_out->status = HINIC_PF_SET_VF_ALREADY; 391 *out_size = sizeof(*mac_out); 392 return 0; 393 } 394 395 err = hinic_port_msg_cmd(hw_dev, HINIC_PORT_CMD_DEL_MAC, buf_in, 396 in_size, buf_out, out_size); 397 if ((err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW) || !(*out_size)) { 398 dev_err(&hw_dev->hwif->pdev->dev, "Failed to delete VF %d MAC, err: %d, status: 0x%x, out size: 0x%x\n", 399 HW_VF_ID_TO_OS(vf_id), err, mac_out->status, *out_size); 400 return -EFAULT; 401 } 402 403 return err; 404 } 405 406 static int hinic_get_vf_link_status_msg_handler(void *hwdev, u16 vf_id, 407 void *buf_in, u16 in_size, 408 void *buf_out, u16 *out_size) 409 { 410 struct hinic_port_link_cmd *get_link = buf_out; 411 struct hinic_hwdev *hw_dev = hwdev; 412 struct vf_data_storage *vf_infos; 413 struct hinic_func_to_io *nic_io; 414 bool link_forced, link_up; 415 416 nic_io = &hw_dev->func_to_io; 417 vf_infos = nic_io->vf_infos; 418 link_forced = vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced; 419 link_up = vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up; 420 421 if (link_forced) 422 get_link->state = link_up ? 423 HINIC_LINK_STATE_UP : HINIC_LINK_STATE_DOWN; 424 else 425 get_link->state = nic_io->link_status; 426 427 get_link->status = 0; 428 *out_size = sizeof(*get_link); 429 430 return 0; 431 } 432 433 static bool check_func_table(struct hinic_hwdev *hwdev, u16 func_idx, 434 void *buf_in, u16 in_size) 435 { 436 struct hinic_cmd_fw_ctxt *function_table = buf_in; 437 438 if (!hinic_mbox_check_func_id_8B(hwdev, func_idx, buf_in, in_size) || 439 !function_table->rx_buf_sz) 440 return false; 441 442 return true; 443 } 444 445 static struct vf_cmd_msg_handle nic_vf_cmd_msg_handler[] = { 446 {HINIC_PORT_CMD_VF_REGISTER, hinic_register_vf_msg_handler}, 447 {HINIC_PORT_CMD_VF_UNREGISTER, hinic_unregister_vf_msg_handler}, 448 {HINIC_PORT_CMD_CHANGE_MTU, hinic_change_vf_mtu_msg_handler}, 449 {HINIC_PORT_CMD_GET_MAC, hinic_get_vf_mac_msg_handler}, 450 {HINIC_PORT_CMD_SET_MAC, hinic_set_vf_mac_msg_handler}, 451 {HINIC_PORT_CMD_DEL_MAC, hinic_del_vf_mac_msg_handler}, 452 {HINIC_PORT_CMD_GET_LINK_STATE, hinic_get_vf_link_status_msg_handler}, 453 }; 454 455 static struct vf_cmd_check_handle nic_cmd_support_vf[] = { 456 {HINIC_PORT_CMD_VF_REGISTER, NULL}, 457 {HINIC_PORT_CMD_VF_UNREGISTER, NULL}, 458 {HINIC_PORT_CMD_CHANGE_MTU, hinic_mbox_check_func_id_8B}, 459 {HINIC_PORT_CMD_ADD_VLAN, hinic_mbox_check_func_id_8B}, 460 {HINIC_PORT_CMD_DEL_VLAN, hinic_mbox_check_func_id_8B}, 461 {HINIC_PORT_CMD_SET_MAC, hinic_mbox_check_func_id_8B}, 462 {HINIC_PORT_CMD_GET_MAC, hinic_mbox_check_func_id_8B}, 463 {HINIC_PORT_CMD_DEL_MAC, hinic_mbox_check_func_id_8B}, 464 {HINIC_PORT_CMD_SET_RX_MODE, hinic_mbox_check_func_id_8B}, 465 {HINIC_PORT_CMD_GET_PAUSE_INFO, hinic_mbox_check_func_id_8B}, 466 {HINIC_PORT_CMD_GET_LINK_STATE, hinic_mbox_check_func_id_8B}, 467 {HINIC_PORT_CMD_SET_LRO, hinic_mbox_check_func_id_8B}, 468 {HINIC_PORT_CMD_SET_RX_CSUM, hinic_mbox_check_func_id_8B}, 469 {HINIC_PORT_CMD_SET_RX_VLAN_OFFLOAD, hinic_mbox_check_func_id_8B}, 470 {HINIC_PORT_CMD_GET_VPORT_STAT, hinic_mbox_check_func_id_8B}, 471 {HINIC_PORT_CMD_CLEAN_VPORT_STAT, hinic_mbox_check_func_id_8B}, 472 {HINIC_PORT_CMD_GET_RSS_TEMPLATE_INDIR_TBL, 473 hinic_mbox_check_func_id_8B}, 474 {HINIC_PORT_CMD_SET_RSS_TEMPLATE_TBL, hinic_mbox_check_func_id_8B}, 475 {HINIC_PORT_CMD_GET_RSS_TEMPLATE_TBL, hinic_mbox_check_func_id_8B}, 476 {HINIC_PORT_CMD_SET_RSS_HASH_ENGINE, hinic_mbox_check_func_id_8B}, 477 {HINIC_PORT_CMD_GET_RSS_HASH_ENGINE, hinic_mbox_check_func_id_8B}, 478 {HINIC_PORT_CMD_GET_RSS_CTX_TBL, hinic_mbox_check_func_id_8B}, 479 {HINIC_PORT_CMD_SET_RSS_CTX_TBL, hinic_mbox_check_func_id_8B}, 480 {HINIC_PORT_CMD_RSS_TEMP_MGR, hinic_mbox_check_func_id_8B}, 481 {HINIC_PORT_CMD_RSS_CFG, hinic_mbox_check_func_id_8B}, 482 {HINIC_PORT_CMD_FWCTXT_INIT, check_func_table}, 483 {HINIC_PORT_CMD_GET_MGMT_VERSION, NULL}, 484 {HINIC_PORT_CMD_SET_FUNC_STATE, hinic_mbox_check_func_id_8B}, 485 {HINIC_PORT_CMD_GET_GLOBAL_QPN, hinic_mbox_check_func_id_8B}, 486 {HINIC_PORT_CMD_SET_TSO, hinic_mbox_check_func_id_8B}, 487 {HINIC_PORT_CMD_SET_RQ_IQ_MAP, hinic_mbox_check_func_id_8B}, 488 {HINIC_PORT_CMD_LINK_STATUS_REPORT, hinic_mbox_check_func_id_8B}, 489 {HINIC_PORT_CMD_UPDATE_MAC, hinic_mbox_check_func_id_8B}, 490 {HINIC_PORT_CMD_GET_CAP, hinic_mbox_check_func_id_8B}, 491 {HINIC_PORT_CMD_GET_LINK_MODE, hinic_mbox_check_func_id_8B}, 492 }; 493 494 #define CHECK_IPSU_15BIT 0X8000 495 496 static 497 struct hinic_sriov_info *hinic_get_sriov_info_by_pcidev(struct pci_dev *pdev) 498 { 499 struct net_device *netdev = pci_get_drvdata(pdev); 500 struct hinic_dev *nic_dev = netdev_priv(netdev); 501 502 return &nic_dev->sriov_info; 503 } 504 505 static int hinic_check_mac_info(u8 status, u16 vlan_id) 506 { 507 if ((status && status != HINIC_MGMT_STATUS_EXIST) || 508 (vlan_id & CHECK_IPSU_15BIT && 509 status == HINIC_MGMT_STATUS_EXIST)) 510 return -EINVAL; 511 512 return 0; 513 } 514 515 #define HINIC_VLAN_ID_MASK 0x7FFF 516 517 static int hinic_update_mac(struct hinic_hwdev *hwdev, u8 *old_mac, 518 u8 *new_mac, u16 vlan_id, u16 func_id) 519 { 520 struct hinic_port_mac_update mac_info = {0}; 521 u16 out_size = sizeof(mac_info); 522 int err; 523 524 if (!hwdev || !old_mac || !new_mac) 525 return -EINVAL; 526 527 if ((vlan_id & HINIC_VLAN_ID_MASK) >= VLAN_N_VID) { 528 dev_err(&hwdev->hwif->pdev->dev, "Invalid VLAN number: %d\n", 529 (vlan_id & HINIC_VLAN_ID_MASK)); 530 return -EINVAL; 531 } 532 533 mac_info.func_id = func_id; 534 mac_info.vlan_id = vlan_id; 535 memcpy(mac_info.old_mac, old_mac, ETH_ALEN); 536 memcpy(mac_info.new_mac, new_mac, ETH_ALEN); 537 538 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_UPDATE_MAC, &mac_info, 539 sizeof(mac_info), &mac_info, &out_size); 540 541 if (err || !out_size || 542 hinic_check_mac_info(mac_info.status, mac_info.vlan_id)) { 543 dev_err(&hwdev->hwif->pdev->dev, 544 "Failed to update MAC, err: %d, status: 0x%x, out size: 0x%x\n", 545 err, mac_info.status, out_size); 546 return -EINVAL; 547 } 548 549 if (mac_info.status == HINIC_MGMT_STATUS_EXIST) 550 dev_warn(&hwdev->hwif->pdev->dev, "MAC is repeated. Ignore update operation\n"); 551 552 return 0; 553 } 554 555 static void hinic_get_vf_config(struct hinic_hwdev *hwdev, u16 vf_id, 556 struct ifla_vf_info *ivi) 557 { 558 struct vf_data_storage *vfinfo; 559 560 vfinfo = hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id); 561 562 ivi->vf = HW_VF_ID_TO_OS(vf_id); 563 memcpy(ivi->mac, vfinfo->vf_mac_addr, ETH_ALEN); 564 ivi->vlan = vfinfo->pf_vlan; 565 ivi->qos = vfinfo->pf_qos; 566 ivi->spoofchk = vfinfo->spoofchk; 567 ivi->trusted = vfinfo->trust; 568 ivi->max_tx_rate = vfinfo->max_rate; 569 ivi->min_tx_rate = vfinfo->min_rate; 570 571 if (!vfinfo->link_forced) 572 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO; 573 else if (vfinfo->link_up) 574 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE; 575 else 576 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE; 577 } 578 579 int hinic_ndo_get_vf_config(struct net_device *netdev, 580 int vf, struct ifla_vf_info *ivi) 581 { 582 struct hinic_dev *nic_dev = netdev_priv(netdev); 583 struct hinic_sriov_info *sriov_info; 584 585 sriov_info = &nic_dev->sriov_info; 586 if (vf >= sriov_info->num_vfs) 587 return -EINVAL; 588 589 hinic_get_vf_config(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), ivi); 590 591 return 0; 592 } 593 594 static int hinic_set_vf_mac(struct hinic_hwdev *hwdev, int vf, 595 unsigned char *mac_addr) 596 { 597 struct hinic_func_to_io *nic_io = &hwdev->func_to_io; 598 struct vf_data_storage *vf_info; 599 u16 func_id; 600 int err; 601 602 vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf); 603 604 /* duplicate request, so just return success */ 605 if (vf_info->pf_set_mac && 606 !memcmp(vf_info->vf_mac_addr, mac_addr, ETH_ALEN)) 607 return 0; 608 609 vf_info->pf_set_mac = true; 610 611 func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf; 612 err = hinic_update_mac(hwdev, vf_info->vf_mac_addr, 613 mac_addr, 0, func_id); 614 if (err) { 615 vf_info->pf_set_mac = false; 616 return err; 617 } 618 619 memcpy(vf_info->vf_mac_addr, mac_addr, ETH_ALEN); 620 621 return 0; 622 } 623 624 int hinic_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac) 625 { 626 struct hinic_dev *nic_dev = netdev_priv(netdev); 627 struct hinic_sriov_info *sriov_info; 628 int err; 629 630 sriov_info = &nic_dev->sriov_info; 631 if (!is_valid_ether_addr(mac) || vf >= sriov_info->num_vfs) 632 return -EINVAL; 633 634 err = hinic_set_vf_mac(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), mac); 635 if (err) 636 return err; 637 638 netif_info(nic_dev, drv, netdev, "Setting MAC %pM on VF %d\n", mac, vf); 639 netif_info(nic_dev, drv, netdev, "Reload the VF driver to make this change effective."); 640 641 return 0; 642 } 643 644 static int hinic_add_vf_vlan(struct hinic_hwdev *hwdev, int vf_id, 645 u16 vlan, u8 qos) 646 { 647 struct hinic_func_to_io *nic_io = &hwdev->func_to_io; 648 int err; 649 650 err = hinic_set_vf_vlan(hwdev, true, vlan, qos, vf_id); 651 if (err) 652 return err; 653 654 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan = vlan; 655 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos = qos; 656 657 dev_info(&hwdev->hwif->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n", 658 vlan, qos, HW_VF_ID_TO_OS(vf_id)); 659 return 0; 660 } 661 662 static int hinic_kill_vf_vlan(struct hinic_hwdev *hwdev, int vf_id) 663 { 664 struct hinic_func_to_io *nic_io = &hwdev->func_to_io; 665 int err; 666 667 err = hinic_set_vf_vlan(hwdev, false, 668 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan, 669 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos, 670 vf_id); 671 if (err) 672 return err; 673 674 dev_info(&hwdev->hwif->pdev->dev, "Remove VLAN %d on VF %d\n", 675 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan, 676 HW_VF_ID_TO_OS(vf_id)); 677 678 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan = 0; 679 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos = 0; 680 681 return 0; 682 } 683 684 static int hinic_update_mac_vlan(struct hinic_dev *nic_dev, u16 old_vlan, 685 u16 new_vlan, int vf_id) 686 { 687 struct vf_data_storage *vf_info; 688 u16 vlan_id; 689 int err; 690 691 if (!nic_dev || old_vlan >= VLAN_N_VID || new_vlan >= VLAN_N_VID) 692 return -EINVAL; 693 694 vf_info = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id); 695 if (!vf_info->pf_set_mac) 696 return 0; 697 698 vlan_id = old_vlan; 699 if (vlan_id) 700 vlan_id |= HINIC_ADD_VLAN_IN_MAC; 701 702 err = hinic_port_del_mac(nic_dev, vf_info->vf_mac_addr, vlan_id); 703 if (err) { 704 dev_err(&nic_dev->hwdev->hwif->pdev->dev, "Failed to delete VF %d MAC %pM vlan %d\n", 705 HW_VF_ID_TO_OS(vf_id), vf_info->vf_mac_addr, old_vlan); 706 return err; 707 } 708 709 vlan_id = new_vlan; 710 if (vlan_id) 711 vlan_id |= HINIC_ADD_VLAN_IN_MAC; 712 713 err = hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id); 714 if (err) { 715 dev_err(&nic_dev->hwdev->hwif->pdev->dev, "Failed to add VF %d MAC %pM vlan %d\n", 716 HW_VF_ID_TO_OS(vf_id), vf_info->vf_mac_addr, new_vlan); 717 goto out; 718 } 719 720 return 0; 721 722 out: 723 vlan_id = old_vlan; 724 if (vlan_id) 725 vlan_id |= HINIC_ADD_VLAN_IN_MAC; 726 hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id); 727 728 return err; 729 } 730 731 static int set_hw_vf_vlan(struct hinic_dev *nic_dev, 732 u16 cur_vlanprio, int vf, u16 vlan, u8 qos) 733 { 734 u16 old_vlan = cur_vlanprio & VLAN_VID_MASK; 735 int err = 0; 736 737 if (vlan || qos) { 738 if (cur_vlanprio) { 739 err = hinic_kill_vf_vlan(nic_dev->hwdev, 740 OS_VF_ID_TO_HW(vf)); 741 if (err) { 742 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d old vlan %d\n", 743 vf, old_vlan); 744 goto out; 745 } 746 } 747 err = hinic_add_vf_vlan(nic_dev->hwdev, 748 OS_VF_ID_TO_HW(vf), vlan, qos); 749 if (err) { 750 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to add vf %d new vlan %d\n", 751 vf, vlan); 752 goto out; 753 } 754 } else { 755 err = hinic_kill_vf_vlan(nic_dev->hwdev, OS_VF_ID_TO_HW(vf)); 756 if (err) { 757 dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d vlan %d\n", 758 vf, old_vlan); 759 goto out; 760 } 761 } 762 763 err = hinic_update_mac_vlan(nic_dev, old_vlan, vlan, 764 OS_VF_ID_TO_HW(vf)); 765 766 out: 767 return err; 768 } 769 770 int hinic_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos, 771 __be16 vlan_proto) 772 { 773 struct hinic_dev *nic_dev = netdev_priv(netdev); 774 struct hinic_sriov_info *sriov_info; 775 u16 vlanprio, cur_vlanprio; 776 777 sriov_info = &nic_dev->sriov_info; 778 if (vf >= sriov_info->num_vfs || vlan >= VLAN_N_VID || qos > HINIC_MAX_QOS) 779 return -EINVAL; 780 if (vlan_proto != htons(ETH_P_8021Q)) 781 return -EPROTONOSUPPORT; 782 vlanprio = vlan | qos << HINIC_VLAN_PRIORITY_SHIFT; 783 cur_vlanprio = hinic_vf_info_vlanprio(nic_dev->hwdev, 784 OS_VF_ID_TO_HW(vf)); 785 /* duplicate request, so just return success */ 786 if (vlanprio == cur_vlanprio) 787 return 0; 788 789 return set_hw_vf_vlan(nic_dev, cur_vlanprio, vf, vlan, qos); 790 } 791 792 static int hinic_set_vf_trust(struct hinic_hwdev *hwdev, u16 vf_id, 793 bool trust) 794 { 795 struct vf_data_storage *vf_infos; 796 struct hinic_func_to_io *nic_io; 797 798 if (!hwdev) 799 return -EINVAL; 800 801 nic_io = &hwdev->func_to_io; 802 vf_infos = nic_io->vf_infos; 803 vf_infos[vf_id].trust = trust; 804 805 return 0; 806 } 807 808 int hinic_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting) 809 { 810 struct hinic_dev *adapter = netdev_priv(netdev); 811 struct hinic_sriov_info *sriov_info; 812 struct hinic_func_to_io *nic_io; 813 bool cur_trust; 814 int err; 815 816 sriov_info = &adapter->sriov_info; 817 nic_io = &adapter->hwdev->func_to_io; 818 819 if (vf >= sriov_info->num_vfs) 820 return -EINVAL; 821 822 cur_trust = nic_io->vf_infos[vf].trust; 823 /* same request, so just return success */ 824 if (setting == cur_trust) 825 return 0; 826 827 err = hinic_set_vf_trust(adapter->hwdev, vf, setting); 828 if (!err) 829 dev_info(&sriov_info->pdev->dev, "Set VF %d trusted %s succeed\n", 830 vf, setting ? "on" : "off"); 831 else 832 dev_err(&sriov_info->pdev->dev, "Failed set VF %d trusted %s\n", 833 vf, setting ? "on" : "off"); 834 835 return err; 836 } 837 838 int hinic_ndo_set_vf_bw(struct net_device *netdev, 839 int vf, int min_tx_rate, int max_tx_rate) 840 { 841 static const u32 speeds[] = { 842 SPEED_10, SPEED_100, SPEED_1000, SPEED_10000, 843 SPEED_25000, SPEED_40000, SPEED_100000 844 }; 845 struct hinic_dev *nic_dev = netdev_priv(netdev); 846 struct hinic_port_cap port_cap = { 0 }; 847 enum hinic_port_link_state link_state; 848 int err; 849 850 if (vf >= nic_dev->sriov_info.num_vfs) { 851 netif_err(nic_dev, drv, netdev, "VF number must be less than %d\n", 852 nic_dev->sriov_info.num_vfs); 853 return -EINVAL; 854 } 855 856 err = hinic_port_link_state(nic_dev, &link_state); 857 if (err) { 858 netif_err(nic_dev, drv, netdev, 859 "Get link status failed when setting vf tx rate\n"); 860 return -EIO; 861 } 862 863 if (link_state == HINIC_LINK_STATE_DOWN) { 864 netif_err(nic_dev, drv, netdev, 865 "Link status must be up when setting vf tx rate\n"); 866 return -EPERM; 867 } 868 869 err = hinic_port_get_cap(nic_dev, &port_cap); 870 if (err || port_cap.speed > LINK_SPEED_100GB) 871 return -EIO; 872 873 /* rate limit cannot be less than 0 and greater than link speed */ 874 if (max_tx_rate < 0 || max_tx_rate > speeds[port_cap.speed]) { 875 netif_err(nic_dev, drv, netdev, "Max tx rate must be in [0 - %d]\n", 876 speeds[port_cap.speed]); 877 return -EINVAL; 878 } 879 880 err = hinic_set_vf_tx_rate(nic_dev->hwdev, OS_VF_ID_TO_HW(vf), 881 max_tx_rate, min_tx_rate); 882 if (err) { 883 netif_err(nic_dev, drv, netdev, 884 "Unable to set VF %d max rate %d min rate %d%s\n", 885 vf, max_tx_rate, min_tx_rate, 886 err == HINIC_TX_RATE_TABLE_FULL ? 887 ", tx rate profile is full" : ""); 888 return -EIO; 889 } 890 891 netif_info(nic_dev, drv, netdev, 892 "Set VF %d max tx rate %d min tx rate %d successfully\n", 893 vf, max_tx_rate, min_tx_rate); 894 895 return 0; 896 } 897 898 static int hinic_set_vf_spoofchk(struct hinic_hwdev *hwdev, u16 vf_id, 899 bool spoofchk) 900 { 901 struct hinic_spoofchk_set spoofchk_cfg = {0}; 902 struct vf_data_storage *vf_infos = NULL; 903 u16 out_size = sizeof(spoofchk_cfg); 904 int err; 905 906 if (!hwdev) 907 return -EINVAL; 908 909 vf_infos = hwdev->func_to_io.vf_infos; 910 911 spoofchk_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id; 912 spoofchk_cfg.state = spoofchk ? 1 : 0; 913 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_ENABLE_SPOOFCHK, 914 &spoofchk_cfg, sizeof(spoofchk_cfg), 915 &spoofchk_cfg, &out_size); 916 if (spoofchk_cfg.status == HINIC_MGMT_CMD_UNSUPPORTED) { 917 err = HINIC_MGMT_CMD_UNSUPPORTED; 918 } else if (err || !out_size || spoofchk_cfg.status) { 919 dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) spoofchk, err: %d, status: 0x%x, out size: 0x%x\n", 920 HW_VF_ID_TO_OS(vf_id), err, spoofchk_cfg.status, 921 out_size); 922 err = -EIO; 923 } 924 925 vf_infos[HW_VF_ID_TO_OS(vf_id)].spoofchk = spoofchk; 926 927 return err; 928 } 929 930 int hinic_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting) 931 { 932 struct hinic_dev *nic_dev = netdev_priv(netdev); 933 struct hinic_sriov_info *sriov_info; 934 bool cur_spoofchk; 935 int err; 936 937 sriov_info = &nic_dev->sriov_info; 938 if (vf >= sriov_info->num_vfs) 939 return -EINVAL; 940 941 cur_spoofchk = nic_dev->hwdev->func_to_io.vf_infos[vf].spoofchk; 942 943 /* same request, so just return success */ 944 if (setting == cur_spoofchk) 945 return 0; 946 947 err = hinic_set_vf_spoofchk(sriov_info->hwdev, 948 OS_VF_ID_TO_HW(vf), setting); 949 if (!err) { 950 netif_info(nic_dev, drv, netdev, "Set VF %d spoofchk %s successfully\n", 951 vf, setting ? "on" : "off"); 952 } else if (err == HINIC_MGMT_CMD_UNSUPPORTED) { 953 netif_err(nic_dev, drv, netdev, 954 "Current firmware doesn't support to set vf spoofchk, need to upgrade latest firmware version\n"); 955 err = -EOPNOTSUPP; 956 } 957 958 return err; 959 } 960 961 static int hinic_set_vf_link_state(struct hinic_hwdev *hwdev, u16 vf_id, 962 int link) 963 { 964 struct hinic_func_to_io *nic_io = &hwdev->func_to_io; 965 struct vf_data_storage *vf_infos = nic_io->vf_infos; 966 u8 link_status = 0; 967 968 switch (link) { 969 case HINIC_IFLA_VF_LINK_STATE_AUTO: 970 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = false; 971 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = nic_io->link_status ? 972 true : false; 973 link_status = nic_io->link_status; 974 break; 975 case HINIC_IFLA_VF_LINK_STATE_ENABLE: 976 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true; 977 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = true; 978 link_status = HINIC_LINK_UP; 979 break; 980 case HINIC_IFLA_VF_LINK_STATE_DISABLE: 981 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true; 982 vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = false; 983 link_status = HINIC_LINK_DOWN; 984 break; 985 default: 986 return -EINVAL; 987 } 988 989 /* Notify the VF of its new link state */ 990 hinic_notify_vf_link_status(hwdev, vf_id, link_status); 991 992 return 0; 993 } 994 995 int hinic_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link) 996 { 997 struct hinic_dev *nic_dev = netdev_priv(netdev); 998 struct hinic_sriov_info *sriov_info; 999 1000 sriov_info = &nic_dev->sriov_info; 1001 1002 if (vf_id >= sriov_info->num_vfs) { 1003 netif_err(nic_dev, drv, netdev, 1004 "Invalid VF Identifier %d\n", vf_id); 1005 return -EINVAL; 1006 } 1007 1008 return hinic_set_vf_link_state(sriov_info->hwdev, 1009 OS_VF_ID_TO_HW(vf_id), link); 1010 } 1011 1012 /* pf receive message from vf */ 1013 static int nic_pf_mbox_handler(void *hwdev, u16 vf_id, u8 cmd, void *buf_in, 1014 u16 in_size, void *buf_out, u16 *out_size) 1015 { 1016 u8 size = ARRAY_SIZE(nic_cmd_support_vf); 1017 struct vf_cmd_msg_handle *vf_msg_handle; 1018 struct hinic_hwdev *dev = hwdev; 1019 struct hinic_func_to_io *nic_io; 1020 struct hinic_pfhwdev *pfhwdev; 1021 int err = 0; 1022 u32 i; 1023 1024 if (!hwdev) 1025 return -EINVAL; 1026 1027 if (!hinic_mbox_check_cmd_valid(hwdev, nic_cmd_support_vf, vf_id, cmd, 1028 buf_in, in_size, size)) { 1029 dev_err(&dev->hwif->pdev->dev, 1030 "PF Receive VF nic cmd: 0x%x, mbox len: 0x%x is invalid\n", 1031 cmd, in_size); 1032 return HINIC_MBOX_VF_CMD_ERROR; 1033 } 1034 1035 pfhwdev = container_of(dev, struct hinic_pfhwdev, hwdev); 1036 nic_io = &dev->func_to_io; 1037 for (i = 0; i < ARRAY_SIZE(nic_vf_cmd_msg_handler); i++) { 1038 vf_msg_handle = &nic_vf_cmd_msg_handler[i]; 1039 if (cmd == vf_msg_handle->cmd && 1040 vf_msg_handle->cmd_msg_handler) { 1041 err = vf_msg_handle->cmd_msg_handler(hwdev, vf_id, 1042 buf_in, in_size, 1043 buf_out, 1044 out_size); 1045 break; 1046 } 1047 } 1048 if (i == ARRAY_SIZE(nic_vf_cmd_msg_handler)) 1049 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC, 1050 cmd, buf_in, in_size, buf_out, 1051 out_size, HINIC_MGMT_MSG_SYNC); 1052 1053 if (err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW) 1054 dev_err(&nic_io->hwif->pdev->dev, "PF receive VF L2NIC cmd: %d process error, err:%d\n", 1055 cmd, err); 1056 return err; 1057 } 1058 1059 static int cfg_mbx_pf_proc_vf_msg(void *hwdev, u16 vf_id, u8 cmd, void *buf_in, 1060 u16 in_size, void *buf_out, u16 *out_size) 1061 { 1062 struct hinic_dev_cap *dev_cap = buf_out; 1063 struct hinic_hwdev *dev = hwdev; 1064 struct hinic_cap *cap; 1065 1066 cap = &dev->nic_cap; 1067 memset(dev_cap, 0, sizeof(*dev_cap)); 1068 1069 dev_cap->max_vf = cap->max_vf; 1070 dev_cap->max_sqs = cap->max_vf_qps; 1071 dev_cap->max_rqs = cap->max_vf_qps; 1072 dev_cap->port_id = dev->port_id; 1073 1074 *out_size = sizeof(*dev_cap); 1075 1076 return 0; 1077 } 1078 1079 static int hinic_init_vf_infos(struct hinic_func_to_io *nic_io, u16 vf_id) 1080 { 1081 struct vf_data_storage *vf_infos = nic_io->vf_infos; 1082 1083 if (set_vf_link_state > HINIC_IFLA_VF_LINK_STATE_DISABLE) { 1084 dev_warn(&nic_io->hwif->pdev->dev, "Module Parameter set_vf_link_state value %d is out of range, resetting to %d\n", 1085 set_vf_link_state, HINIC_IFLA_VF_LINK_STATE_AUTO); 1086 set_vf_link_state = HINIC_IFLA_VF_LINK_STATE_AUTO; 1087 } 1088 1089 switch (set_vf_link_state) { 1090 case HINIC_IFLA_VF_LINK_STATE_AUTO: 1091 vf_infos[vf_id].link_forced = false; 1092 break; 1093 case HINIC_IFLA_VF_LINK_STATE_ENABLE: 1094 vf_infos[vf_id].link_forced = true; 1095 vf_infos[vf_id].link_up = true; 1096 break; 1097 case HINIC_IFLA_VF_LINK_STATE_DISABLE: 1098 vf_infos[vf_id].link_forced = true; 1099 vf_infos[vf_id].link_up = false; 1100 break; 1101 default: 1102 dev_err(&nic_io->hwif->pdev->dev, "Invalid input parameter set_vf_link_state: %d\n", 1103 set_vf_link_state); 1104 return -EINVAL; 1105 } 1106 1107 return 0; 1108 } 1109 1110 static void hinic_clear_vf_infos(struct hinic_dev *nic_dev, u16 vf_id) 1111 { 1112 struct vf_data_storage *vf_infos; 1113 1114 vf_infos = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id); 1115 if (vf_infos->pf_set_mac) 1116 hinic_port_del_mac(nic_dev, vf_infos->vf_mac_addr, 0); 1117 1118 if (hinic_vf_info_vlanprio(nic_dev->hwdev, vf_id)) 1119 hinic_kill_vf_vlan(nic_dev->hwdev, vf_id); 1120 1121 if (vf_infos->max_rate) 1122 hinic_set_vf_tx_rate(nic_dev->hwdev, vf_id, 0, 0); 1123 1124 if (vf_infos->spoofchk) 1125 hinic_set_vf_spoofchk(nic_dev->hwdev, vf_id, false); 1126 1127 if (vf_infos->trust) 1128 hinic_set_vf_trust(nic_dev->hwdev, vf_id, false); 1129 1130 memset(vf_infos, 0, sizeof(*vf_infos)); 1131 /* set vf_infos to default */ 1132 hinic_init_vf_infos(&nic_dev->hwdev->func_to_io, HW_VF_ID_TO_OS(vf_id)); 1133 } 1134 1135 static void hinic_deinit_vf_hw(struct hinic_sriov_info *sriov_info, 1136 u16 start_vf_id, u16 end_vf_id) 1137 { 1138 struct hinic_dev *nic_dev; 1139 u16 func_idx, idx; 1140 1141 nic_dev = container_of(sriov_info, struct hinic_dev, sriov_info); 1142 1143 for (idx = start_vf_id; idx <= end_vf_id; idx++) { 1144 func_idx = hinic_glb_pf_vf_offset(nic_dev->hwdev->hwif) + idx; 1145 hinic_set_wq_page_size(nic_dev->hwdev, func_idx, 1146 HINIC_HW_WQ_PAGE_SIZE); 1147 hinic_clear_vf_infos(nic_dev, idx); 1148 } 1149 } 1150 1151 int hinic_vf_func_init(struct hinic_hwdev *hwdev) 1152 { 1153 struct hinic_register_vf register_info = {0}; 1154 u16 out_size = sizeof(register_info); 1155 struct hinic_func_to_io *nic_io; 1156 int err = 0; 1157 u32 size, i; 1158 1159 err = hinic_vf_mbox_random_id_init(hwdev); 1160 if (err) { 1161 dev_err(&hwdev->hwif->pdev->dev, "Failed to init vf mbox random id, err: %d\n", 1162 err); 1163 return err; 1164 } 1165 1166 nic_io = &hwdev->func_to_io; 1167 1168 if (HINIC_IS_VF(hwdev->hwif)) { 1169 err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC, 1170 HINIC_PORT_CMD_VF_REGISTER, 1171 ®ister_info, sizeof(register_info), 1172 ®ister_info, &out_size, 0); 1173 if (err || register_info.status || !out_size) { 1174 dev_err(&hwdev->hwif->pdev->dev, 1175 "Failed to register VF, err: %d, status: 0x%x, out size: 0x%x\n", 1176 err, register_info.status, out_size); 1177 return -EIO; 1178 } 1179 } else { 1180 err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_CFGM, 1181 cfg_mbx_pf_proc_vf_msg); 1182 if (err) { 1183 dev_err(&hwdev->hwif->pdev->dev, 1184 "Register PF mailbox callback failed\n"); 1185 return err; 1186 } 1187 nic_io->max_vfs = hwdev->nic_cap.max_vf; 1188 size = sizeof(*nic_io->vf_infos) * nic_io->max_vfs; 1189 if (size != 0) { 1190 nic_io->vf_infos = kzalloc(size, GFP_KERNEL); 1191 if (!nic_io->vf_infos) { 1192 err = -ENOMEM; 1193 goto out_free_nic_io; 1194 } 1195 1196 for (i = 0; i < nic_io->max_vfs; i++) { 1197 err = hinic_init_vf_infos(nic_io, i); 1198 if (err) 1199 goto err_init_vf_infos; 1200 } 1201 1202 err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC, 1203 nic_pf_mbox_handler); 1204 if (err) 1205 goto err_register_pf_mbox_cb; 1206 } 1207 } 1208 1209 return 0; 1210 1211 err_register_pf_mbox_cb: 1212 err_init_vf_infos: 1213 kfree(nic_io->vf_infos); 1214 out_free_nic_io: 1215 return err; 1216 } 1217 1218 void hinic_vf_func_free(struct hinic_hwdev *hwdev) 1219 { 1220 struct hinic_register_vf unregister = {0}; 1221 u16 out_size = sizeof(unregister); 1222 int err; 1223 1224 if (HINIC_IS_VF(hwdev->hwif)) { 1225 err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC, 1226 HINIC_PORT_CMD_VF_UNREGISTER, 1227 &unregister, sizeof(unregister), 1228 &unregister, &out_size, 0); 1229 if (err || !out_size || unregister.status) 1230 dev_err(&hwdev->hwif->pdev->dev, "Failed to unregister VF, err: %d, status: 0x%x, out_size: 0x%x\n", 1231 err, unregister.status, out_size); 1232 } else { 1233 if (hwdev->func_to_io.vf_infos) { 1234 hinic_unregister_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC); 1235 kfree(hwdev->func_to_io.vf_infos); 1236 } 1237 } 1238 } 1239 1240 static int hinic_init_vf_hw(struct hinic_hwdev *hwdev, u16 start_vf_id, 1241 u16 end_vf_id) 1242 { 1243 u16 i, func_idx; 1244 int err; 1245 1246 /* vf use 256K as default wq page size, and can't change it */ 1247 for (i = start_vf_id; i <= end_vf_id; i++) { 1248 func_idx = hinic_glb_pf_vf_offset(hwdev->hwif) + i; 1249 err = hinic_set_wq_page_size(hwdev, func_idx, 1250 HINIC_DEFAULT_WQ_PAGE_SIZE); 1251 if (err) 1252 return err; 1253 } 1254 1255 return 0; 1256 } 1257 1258 int hinic_pci_sriov_disable(struct pci_dev *pdev) 1259 { 1260 struct hinic_sriov_info *sriov_info; 1261 u16 tmp_vfs; 1262 1263 sriov_info = hinic_get_sriov_info_by_pcidev(pdev); 1264 /* if SR-IOV is already disabled then nothing will be done */ 1265 if (!sriov_info->sriov_enabled) 1266 return 0; 1267 1268 set_bit(HINIC_SRIOV_DISABLE, &sriov_info->state); 1269 1270 /* If our VFs are assigned we cannot shut down SR-IOV 1271 * without causing issues, so just leave the hardware 1272 * available but disabled 1273 */ 1274 if (pci_vfs_assigned(sriov_info->pdev)) { 1275 clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state); 1276 dev_warn(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n"); 1277 return -EPERM; 1278 } 1279 sriov_info->sriov_enabled = false; 1280 1281 /* disable iov and allow time for transactions to clear */ 1282 pci_disable_sriov(sriov_info->pdev); 1283 1284 tmp_vfs = (u16)sriov_info->num_vfs; 1285 sriov_info->num_vfs = 0; 1286 hinic_deinit_vf_hw(sriov_info, OS_VF_ID_TO_HW(0), 1287 OS_VF_ID_TO_HW(tmp_vfs - 1)); 1288 1289 clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state); 1290 1291 return 0; 1292 } 1293 1294 static int hinic_pci_sriov_enable(struct pci_dev *pdev, int num_vfs) 1295 { 1296 struct hinic_sriov_info *sriov_info; 1297 int err; 1298 1299 sriov_info = hinic_get_sriov_info_by_pcidev(pdev); 1300 1301 if (test_and_set_bit(HINIC_SRIOV_ENABLE, &sriov_info->state)) { 1302 dev_err(&pdev->dev, 1303 "SR-IOV enable in process, please wait, num_vfs %d\n", 1304 num_vfs); 1305 return -EPERM; 1306 } 1307 1308 err = hinic_init_vf_hw(sriov_info->hwdev, OS_VF_ID_TO_HW(0), 1309 OS_VF_ID_TO_HW((u16)num_vfs - 1)); 1310 if (err) { 1311 dev_err(&sriov_info->pdev->dev, 1312 "Failed to init vf in hardware before enable sriov, error %d\n", 1313 err); 1314 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state); 1315 return err; 1316 } 1317 1318 err = pci_enable_sriov(sriov_info->pdev, num_vfs); 1319 if (err) { 1320 dev_err(&pdev->dev, 1321 "Failed to enable SR-IOV, error %d\n", err); 1322 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state); 1323 return err; 1324 } 1325 1326 sriov_info->sriov_enabled = true; 1327 sriov_info->num_vfs = num_vfs; 1328 clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state); 1329 1330 return num_vfs; 1331 } 1332 1333 int hinic_pci_sriov_configure(struct pci_dev *dev, int num_vfs) 1334 { 1335 struct hinic_sriov_info *sriov_info; 1336 1337 sriov_info = hinic_get_sriov_info_by_pcidev(dev); 1338 1339 if (test_bit(HINIC_FUNC_REMOVE, &sriov_info->state)) 1340 return -EBUSY; 1341 1342 if (!num_vfs) 1343 return hinic_pci_sriov_disable(dev); 1344 else 1345 return hinic_pci_sriov_enable(dev, num_vfs); 1346 } 1347