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