1 /* 2 * QLogic qlcnic NIC Driver 3 * Copyright (c) 2009-2013 QLogic Corporation 4 * 5 * See LICENSE.qlcnic for copyright and licensing details. 6 */ 7 8 #include "qlcnic_sriov.h" 9 #include "qlcnic.h" 10 #include <linux/types.h> 11 12 #define QLCNIC_SRIOV_VF_MAX_MAC 1 13 #define QLC_VF_MIN_TX_RATE 100 14 #define QLC_VF_MAX_TX_RATE 9999 15 16 static int qlcnic_sriov_pf_get_vport_handle(struct qlcnic_adapter *, u8); 17 18 struct qlcnic_sriov_cmd_handler { 19 int (*fn) (struct qlcnic_bc_trans *, struct qlcnic_cmd_args *); 20 }; 21 22 struct qlcnic_sriov_fw_cmd_handler { 23 u32 cmd; 24 int (*fn) (struct qlcnic_bc_trans *, struct qlcnic_cmd_args *); 25 }; 26 27 static int qlcnic_sriov_pf_set_vport_info(struct qlcnic_adapter *adapter, 28 struct qlcnic_info *npar_info, 29 u16 vport_id) 30 { 31 struct qlcnic_cmd_args cmd; 32 int err; 33 34 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO)) 35 return -ENOMEM; 36 37 cmd.req.arg[1] = (vport_id << 16) | 0x1; 38 cmd.req.arg[2] = npar_info->bit_offsets; 39 cmd.req.arg[2] |= npar_info->min_tx_bw << 16; 40 cmd.req.arg[3] = npar_info->max_tx_bw | (npar_info->max_tx_ques << 16); 41 cmd.req.arg[4] = npar_info->max_tx_mac_filters; 42 cmd.req.arg[4] |= npar_info->max_rx_mcast_mac_filters << 16; 43 cmd.req.arg[5] = npar_info->max_rx_ucast_mac_filters | 44 (npar_info->max_rx_ip_addr << 16); 45 cmd.req.arg[6] = npar_info->max_rx_lro_flow | 46 (npar_info->max_rx_status_rings << 16); 47 cmd.req.arg[7] = npar_info->max_rx_buf_rings | 48 (npar_info->max_rx_ques << 16); 49 cmd.req.arg[8] = npar_info->max_tx_vlan_keys; 50 cmd.req.arg[8] |= npar_info->max_local_ipv6_addrs << 16; 51 cmd.req.arg[9] = npar_info->max_remote_ipv6_addrs; 52 53 err = qlcnic_issue_cmd(adapter, &cmd); 54 if (err) 55 dev_err(&adapter->pdev->dev, 56 "Failed to set vport info, err=%d\n", err); 57 58 qlcnic_free_mbx_args(&cmd); 59 return err; 60 } 61 62 static int qlcnic_sriov_pf_cal_res_limit(struct qlcnic_adapter *adapter, 63 struct qlcnic_info *info, u16 func) 64 { 65 struct qlcnic_sriov *sriov = adapter->ahw->sriov; 66 struct qlcnic_resources *res = &sriov->ff_max; 67 u32 temp, num_vf_macs, num_vfs, max; 68 int ret = -EIO, vpid, id; 69 struct qlcnic_vport *vp; 70 71 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func); 72 if (vpid < 0) 73 return -EINVAL; 74 75 num_vfs = sriov->num_vfs; 76 max = num_vfs + 1; 77 info->bit_offsets = 0xffff; 78 info->max_tx_ques = res->num_tx_queues / max; 79 info->max_rx_mcast_mac_filters = res->num_rx_mcast_mac_filters; 80 num_vf_macs = QLCNIC_SRIOV_VF_MAX_MAC; 81 82 if (adapter->ahw->pci_func == func) { 83 temp = res->num_rx_mcast_mac_filters - (num_vfs * num_vf_macs); 84 info->max_rx_ucast_mac_filters = temp; 85 temp = res->num_tx_mac_filters - (num_vfs * num_vf_macs); 86 info->max_tx_mac_filters = temp; 87 info->min_tx_bw = 0; 88 info->max_tx_bw = MAX_BW; 89 } else { 90 id = qlcnic_sriov_func_to_index(adapter, func); 91 if (id < 0) 92 return id; 93 vp = sriov->vf_info[id].vp; 94 info->min_tx_bw = vp->min_tx_bw; 95 info->max_tx_bw = vp->max_tx_bw; 96 info->max_rx_ucast_mac_filters = num_vf_macs; 97 info->max_tx_mac_filters = num_vf_macs; 98 } 99 100 info->max_rx_ip_addr = res->num_destip / max; 101 info->max_rx_status_rings = res->num_rx_status_rings / max; 102 info->max_rx_buf_rings = res->num_rx_buf_rings / max; 103 info->max_rx_ques = res->num_rx_queues / max; 104 info->max_rx_lro_flow = res->num_lro_flows_supported / max; 105 info->max_tx_vlan_keys = res->num_txvlan_keys; 106 info->max_local_ipv6_addrs = res->max_local_ipv6_addrs; 107 info->max_remote_ipv6_addrs = res->max_remote_ipv6_addrs; 108 109 ret = qlcnic_sriov_pf_set_vport_info(adapter, info, vpid); 110 if (ret) 111 return ret; 112 113 return 0; 114 } 115 116 static void qlcnic_sriov_pf_set_ff_max_res(struct qlcnic_adapter *adapter, 117 struct qlcnic_info *info) 118 { 119 struct qlcnic_resources *ff_max = &adapter->ahw->sriov->ff_max; 120 121 ff_max->num_tx_mac_filters = info->max_tx_mac_filters; 122 ff_max->num_rx_ucast_mac_filters = info->max_rx_ucast_mac_filters; 123 ff_max->num_rx_mcast_mac_filters = info->max_rx_mcast_mac_filters; 124 ff_max->num_txvlan_keys = info->max_tx_vlan_keys; 125 ff_max->num_rx_queues = info->max_rx_ques; 126 ff_max->num_tx_queues = info->max_tx_ques; 127 ff_max->num_lro_flows_supported = info->max_rx_lro_flow; 128 ff_max->num_destip = info->max_rx_ip_addr; 129 ff_max->num_rx_buf_rings = info->max_rx_buf_rings; 130 ff_max->num_rx_status_rings = info->max_rx_status_rings; 131 ff_max->max_remote_ipv6_addrs = info->max_remote_ipv6_addrs; 132 ff_max->max_local_ipv6_addrs = info->max_local_ipv6_addrs; 133 } 134 135 static int qlcnic_sriov_get_pf_info(struct qlcnic_adapter *adapter, 136 struct qlcnic_info *npar_info) 137 { 138 int err; 139 struct qlcnic_cmd_args cmd; 140 141 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_GET_NIC_INFO)) 142 return -ENOMEM; 143 144 cmd.req.arg[1] = 0x2; 145 err = qlcnic_issue_cmd(adapter, &cmd); 146 if (err) { 147 dev_err(&adapter->pdev->dev, 148 "Failed to get PF info, err=%d\n", err); 149 goto out; 150 } 151 152 npar_info->total_pf = cmd.rsp.arg[2] & 0xff; 153 npar_info->total_rss_engines = (cmd.rsp.arg[2] >> 8) & 0xff; 154 npar_info->max_vports = MSW(cmd.rsp.arg[2]); 155 npar_info->max_tx_ques = LSW(cmd.rsp.arg[3]); 156 npar_info->max_tx_mac_filters = MSW(cmd.rsp.arg[3]); 157 npar_info->max_rx_mcast_mac_filters = LSW(cmd.rsp.arg[4]); 158 npar_info->max_rx_ucast_mac_filters = MSW(cmd.rsp.arg[4]); 159 npar_info->max_rx_ip_addr = LSW(cmd.rsp.arg[5]); 160 npar_info->max_rx_lro_flow = MSW(cmd.rsp.arg[5]); 161 npar_info->max_rx_status_rings = LSW(cmd.rsp.arg[6]); 162 npar_info->max_rx_buf_rings = MSW(cmd.rsp.arg[6]); 163 npar_info->max_rx_ques = LSW(cmd.rsp.arg[7]); 164 npar_info->max_tx_vlan_keys = MSW(cmd.rsp.arg[7]); 165 npar_info->max_local_ipv6_addrs = LSW(cmd.rsp.arg[8]); 166 npar_info->max_remote_ipv6_addrs = MSW(cmd.rsp.arg[8]); 167 168 qlcnic_sriov_pf_set_ff_max_res(adapter, npar_info); 169 dev_info(&adapter->pdev->dev, 170 "\n\ttotal_pf: %d,\n" 171 "\n\ttotal_rss_engines: %d max_vports: %d max_tx_ques %d,\n" 172 "\tmax_tx_mac_filters: %d max_rx_mcast_mac_filters: %d,\n" 173 "\tmax_rx_ucast_mac_filters: 0x%x, max_rx_ip_addr: %d,\n" 174 "\tmax_rx_lro_flow: %d max_rx_status_rings: %d,\n" 175 "\tmax_rx_buf_rings: %d, max_rx_ques: %d, max_tx_vlan_keys %d\n" 176 "\tmax_local_ipv6_addrs: %d, max_remote_ipv6_addrs: %d\n", 177 npar_info->total_pf, npar_info->total_rss_engines, 178 npar_info->max_vports, npar_info->max_tx_ques, 179 npar_info->max_tx_mac_filters, 180 npar_info->max_rx_mcast_mac_filters, 181 npar_info->max_rx_ucast_mac_filters, npar_info->max_rx_ip_addr, 182 npar_info->max_rx_lro_flow, npar_info->max_rx_status_rings, 183 npar_info->max_rx_buf_rings, npar_info->max_rx_ques, 184 npar_info->max_tx_vlan_keys, npar_info->max_local_ipv6_addrs, 185 npar_info->max_remote_ipv6_addrs); 186 187 out: 188 qlcnic_free_mbx_args(&cmd); 189 return err; 190 } 191 192 static void qlcnic_sriov_pf_reset_vport_handle(struct qlcnic_adapter *adapter, 193 u8 func) 194 { 195 struct qlcnic_sriov *sriov = adapter->ahw->sriov; 196 struct qlcnic_vport *vp; 197 int index; 198 199 if (adapter->ahw->pci_func == func) { 200 sriov->vp_handle = 0; 201 } else { 202 index = qlcnic_sriov_func_to_index(adapter, func); 203 if (index < 0) 204 return; 205 vp = sriov->vf_info[index].vp; 206 vp->handle = 0; 207 } 208 } 209 210 static void qlcnic_sriov_pf_set_vport_handle(struct qlcnic_adapter *adapter, 211 u16 vport_handle, u8 func) 212 { 213 struct qlcnic_sriov *sriov = adapter->ahw->sriov; 214 struct qlcnic_vport *vp; 215 int index; 216 217 if (adapter->ahw->pci_func == func) { 218 sriov->vp_handle = vport_handle; 219 } else { 220 index = qlcnic_sriov_func_to_index(adapter, func); 221 if (index < 0) 222 return; 223 vp = sriov->vf_info[index].vp; 224 vp->handle = vport_handle; 225 } 226 } 227 228 static int qlcnic_sriov_pf_get_vport_handle(struct qlcnic_adapter *adapter, 229 u8 func) 230 { 231 struct qlcnic_sriov *sriov = adapter->ahw->sriov; 232 struct qlcnic_vf_info *vf_info; 233 int index; 234 235 if (adapter->ahw->pci_func == func) { 236 return sriov->vp_handle; 237 } else { 238 index = qlcnic_sriov_func_to_index(adapter, func); 239 if (index >= 0) { 240 vf_info = &sriov->vf_info[index]; 241 return vf_info->vp->handle; 242 } 243 } 244 245 return -EINVAL; 246 } 247 248 static int qlcnic_sriov_pf_config_vport(struct qlcnic_adapter *adapter, 249 u8 flag, u16 func) 250 { 251 struct qlcnic_cmd_args cmd; 252 int ret; 253 int vpid; 254 255 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_CONFIG_VPORT)) 256 return -ENOMEM; 257 258 if (flag) { 259 cmd.req.arg[3] = func << 8; 260 } else { 261 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func); 262 if (vpid < 0) { 263 ret = -EINVAL; 264 goto out; 265 } 266 cmd.req.arg[3] = ((vpid & 0xffff) << 8) | 1; 267 } 268 269 ret = qlcnic_issue_cmd(adapter, &cmd); 270 if (ret) { 271 dev_err(&adapter->pdev->dev, 272 "Failed %s vport, err %d for func 0x%x\n", 273 (flag ? "enable" : "disable"), ret, func); 274 goto out; 275 } 276 277 if (flag) { 278 vpid = cmd.rsp.arg[2] & 0xffff; 279 qlcnic_sriov_pf_set_vport_handle(adapter, vpid, func); 280 } else { 281 qlcnic_sriov_pf_reset_vport_handle(adapter, func); 282 } 283 284 out: 285 qlcnic_free_mbx_args(&cmd); 286 return ret; 287 } 288 289 static int qlcnic_sriov_pf_cfg_vlan_filtering(struct qlcnic_adapter *adapter, 290 u8 enable) 291 { 292 struct qlcnic_cmd_args cmd; 293 int err; 294 295 err = qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO); 296 if (err) 297 return err; 298 299 cmd.req.arg[1] = 0x4; 300 if (enable) 301 cmd.req.arg[1] |= BIT_16; 302 303 err = qlcnic_issue_cmd(adapter, &cmd); 304 if (err) 305 dev_err(&adapter->pdev->dev, 306 "Failed to configure VLAN filtering, err=%d\n", err); 307 308 qlcnic_free_mbx_args(&cmd); 309 return err; 310 } 311 312 static int qlcnic_sriov_pf_cfg_eswitch(struct qlcnic_adapter *adapter, 313 u8 func, u8 enable) 314 { 315 struct qlcnic_cmd_args cmd; 316 int err = -EIO; 317 318 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_TOGGLE_ESWITCH)) 319 return -ENOMEM; 320 321 cmd.req.arg[0] |= (3 << 29); 322 cmd.req.arg[1] = ((func & 0xf) << 2) | BIT_6 | BIT_1; 323 if (enable) 324 cmd.req.arg[1] |= BIT_0; 325 326 err = qlcnic_issue_cmd(adapter, &cmd); 327 328 if (err != QLCNIC_RCODE_SUCCESS) { 329 dev_err(&adapter->pdev->dev, 330 "Failed to enable sriov eswitch%d\n", err); 331 err = -EIO; 332 } 333 334 qlcnic_free_mbx_args(&cmd); 335 return err; 336 } 337 338 static void qlcnic_sriov_pf_del_flr_queue(struct qlcnic_adapter *adapter) 339 { 340 struct qlcnic_sriov *sriov = adapter->ahw->sriov; 341 struct qlcnic_back_channel *bc = &sriov->bc; 342 int i; 343 344 for (i = 0; i < sriov->num_vfs; i++) 345 cancel_work_sync(&sriov->vf_info[i].flr_work); 346 347 destroy_workqueue(bc->bc_flr_wq); 348 } 349 350 static int qlcnic_sriov_pf_create_flr_queue(struct qlcnic_adapter *adapter) 351 { 352 struct qlcnic_back_channel *bc = &adapter->ahw->sriov->bc; 353 struct workqueue_struct *wq; 354 355 wq = create_singlethread_workqueue("qlcnic-flr"); 356 if (wq == NULL) { 357 dev_err(&adapter->pdev->dev, "Cannot create FLR workqueue\n"); 358 return -ENOMEM; 359 } 360 361 bc->bc_flr_wq = wq; 362 return 0; 363 } 364 365 void qlcnic_sriov_pf_cleanup(struct qlcnic_adapter *adapter) 366 { 367 u8 func = adapter->ahw->pci_func; 368 369 if (!qlcnic_sriov_enable_check(adapter)) 370 return; 371 372 qlcnic_sriov_pf_del_flr_queue(adapter); 373 qlcnic_sriov_cfg_bc_intr(adapter, 0); 374 qlcnic_sriov_pf_config_vport(adapter, 0, func); 375 qlcnic_sriov_pf_cfg_eswitch(adapter, func, 0); 376 qlcnic_sriov_pf_cfg_vlan_filtering(adapter, 0); 377 __qlcnic_sriov_cleanup(adapter); 378 adapter->ahw->op_mode = QLCNIC_MGMT_FUNC; 379 clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state); 380 } 381 382 void qlcnic_sriov_pf_disable(struct qlcnic_adapter *adapter) 383 { 384 if (!qlcnic_sriov_pf_check(adapter)) 385 return; 386 387 if (!qlcnic_sriov_enable_check(adapter)) 388 return; 389 390 pci_disable_sriov(adapter->pdev); 391 netdev_info(adapter->netdev, 392 "SR-IOV is disabled successfully on port %d\n", 393 adapter->portnum); 394 } 395 396 static int qlcnic_pci_sriov_disable(struct qlcnic_adapter *adapter) 397 { 398 struct net_device *netdev = adapter->netdev; 399 400 if (netif_running(netdev)) 401 __qlcnic_down(adapter, netdev); 402 403 qlcnic_sriov_pf_disable(adapter); 404 405 qlcnic_sriov_pf_cleanup(adapter); 406 407 /* After disabling SRIOV re-init the driver in default mode 408 configure opmode based on op_mode of function 409 */ 410 if (qlcnic_83xx_configure_opmode(adapter)) 411 return -EIO; 412 413 if (netif_running(netdev)) 414 __qlcnic_up(adapter, netdev); 415 416 return 0; 417 } 418 419 static int qlcnic_sriov_pf_init(struct qlcnic_adapter *adapter) 420 { 421 struct qlcnic_hardware_context *ahw = adapter->ahw; 422 struct qlcnic_info nic_info, pf_info, vp_info; 423 int err; 424 u8 func = ahw->pci_func; 425 426 if (!qlcnic_sriov_enable_check(adapter)) 427 return 0; 428 429 err = qlcnic_sriov_pf_cfg_vlan_filtering(adapter, 1); 430 if (err) 431 return err; 432 433 err = qlcnic_sriov_pf_cfg_eswitch(adapter, func, 1); 434 if (err) 435 goto disable_vlan_filtering; 436 437 err = qlcnic_sriov_pf_config_vport(adapter, 1, func); 438 if (err) 439 goto disable_eswitch; 440 441 err = qlcnic_sriov_get_pf_info(adapter, &pf_info); 442 if (err) 443 goto delete_vport; 444 445 err = qlcnic_get_nic_info(adapter, &nic_info, func); 446 if (err) 447 goto delete_vport; 448 449 err = qlcnic_sriov_pf_cal_res_limit(adapter, &vp_info, func); 450 if (err) 451 goto delete_vport; 452 453 err = qlcnic_sriov_cfg_bc_intr(adapter, 1); 454 if (err) 455 goto delete_vport; 456 457 ahw->physical_port = (u8) nic_info.phys_port; 458 ahw->switch_mode = nic_info.switch_mode; 459 ahw->max_mtu = nic_info.max_mtu; 460 ahw->capabilities = nic_info.capabilities; 461 ahw->nic_mode = QLC_83XX_SRIOV_MODE; 462 return err; 463 464 delete_vport: 465 qlcnic_sriov_pf_config_vport(adapter, 0, func); 466 467 disable_eswitch: 468 qlcnic_sriov_pf_cfg_eswitch(adapter, func, 0); 469 470 disable_vlan_filtering: 471 qlcnic_sriov_pf_cfg_vlan_filtering(adapter, 0); 472 473 return err; 474 } 475 476 static int qlcnic_sriov_pf_enable(struct qlcnic_adapter *adapter, int num_vfs) 477 { 478 int err; 479 480 if (!qlcnic_sriov_enable_check(adapter)) 481 return 0; 482 483 err = pci_enable_sriov(adapter->pdev, num_vfs); 484 if (err) 485 qlcnic_sriov_pf_cleanup(adapter); 486 487 return err; 488 } 489 490 static int __qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter, 491 int num_vfs) 492 { 493 int err = 0; 494 495 set_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state); 496 adapter->ahw->op_mode = QLCNIC_SRIOV_PF_FUNC; 497 498 err = qlcnic_sriov_init(adapter, num_vfs); 499 if (err) 500 goto clear_op_mode; 501 502 err = qlcnic_sriov_pf_create_flr_queue(adapter); 503 if (err) 504 goto sriov_cleanup; 505 506 err = qlcnic_sriov_pf_init(adapter); 507 if (err) 508 goto del_flr_queue; 509 510 err = qlcnic_sriov_pf_enable(adapter, num_vfs); 511 return err; 512 513 del_flr_queue: 514 qlcnic_sriov_pf_del_flr_queue(adapter); 515 516 sriov_cleanup: 517 __qlcnic_sriov_cleanup(adapter); 518 519 clear_op_mode: 520 clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state); 521 adapter->ahw->op_mode = QLCNIC_MGMT_FUNC; 522 return err; 523 } 524 525 static int qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter, int num_vfs) 526 { 527 struct net_device *netdev = adapter->netdev; 528 int err; 529 530 if (!(adapter->flags & QLCNIC_MSIX_ENABLED)) { 531 netdev_err(netdev, 532 "SR-IOV cannot be enabled, when legacy interrupts are enabled\n"); 533 return -EIO; 534 } 535 536 if (netif_running(netdev)) 537 __qlcnic_down(adapter, netdev); 538 539 err = __qlcnic_pci_sriov_enable(adapter, num_vfs); 540 if (err) { 541 netdev_info(netdev, "Failed to enable SR-IOV on port %d\n", 542 adapter->portnum); 543 544 err = -EIO; 545 if (qlcnic_83xx_configure_opmode(adapter)) 546 goto error; 547 } else { 548 netdev_info(netdev, 549 "SR-IOV is enabled successfully on port %d\n", 550 adapter->portnum); 551 /* Return number of vfs enabled */ 552 err = num_vfs; 553 } 554 if (netif_running(netdev)) 555 __qlcnic_up(adapter, netdev); 556 557 error: 558 return err; 559 } 560 561 int qlcnic_pci_sriov_configure(struct pci_dev *dev, int num_vfs) 562 { 563 struct qlcnic_adapter *adapter = pci_get_drvdata(dev); 564 int err; 565 566 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) 567 return -EBUSY; 568 569 if (num_vfs == 0) 570 err = qlcnic_pci_sriov_disable(adapter); 571 else 572 err = qlcnic_pci_sriov_enable(adapter, num_vfs); 573 574 clear_bit(__QLCNIC_RESETTING, &adapter->state); 575 return err; 576 } 577 578 static int qlcnic_sriov_set_vf_acl(struct qlcnic_adapter *adapter, u8 func) 579 { 580 struct qlcnic_cmd_args cmd; 581 struct qlcnic_vport *vp; 582 int err, id; 583 584 id = qlcnic_sriov_func_to_index(adapter, func); 585 if (id < 0) 586 return id; 587 588 vp = adapter->ahw->sriov->vf_info[id].vp; 589 err = qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO); 590 if (err) 591 return err; 592 593 cmd.req.arg[1] = 0x3 | func << 16; 594 if (vp->vlan_mode == QLC_PVID_MODE) { 595 cmd.req.arg[2] |= BIT_6; 596 cmd.req.arg[3] |= vp->vlan << 8; 597 } 598 599 err = qlcnic_issue_cmd(adapter, &cmd); 600 if (err) 601 dev_err(&adapter->pdev->dev, "Failed to set ACL, err=%d\n", 602 err); 603 604 qlcnic_free_mbx_args(&cmd); 605 return err; 606 } 607 608 static int qlcnic_sriov_set_vf_vport_info(struct qlcnic_adapter *adapter, 609 u16 func) 610 { 611 struct qlcnic_info defvp_info; 612 int err; 613 614 err = qlcnic_sriov_pf_cal_res_limit(adapter, &defvp_info, func); 615 if (err) 616 return -EIO; 617 618 err = qlcnic_sriov_set_vf_acl(adapter, func); 619 if (err) 620 return err; 621 622 return 0; 623 } 624 625 static int qlcnic_sriov_pf_channel_cfg_cmd(struct qlcnic_bc_trans *trans, 626 struct qlcnic_cmd_args *cmd) 627 { 628 struct qlcnic_vf_info *vf = trans->vf; 629 struct qlcnic_adapter *adapter = vf->adapter; 630 int err; 631 u16 func = vf->pci_func; 632 633 cmd->rsp.arg[0] = trans->req_hdr->cmd_op; 634 cmd->rsp.arg[0] |= (1 << 16); 635 636 if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) { 637 err = qlcnic_sriov_pf_config_vport(adapter, 1, func); 638 if (!err) { 639 err = qlcnic_sriov_set_vf_vport_info(adapter, func); 640 if (err) 641 qlcnic_sriov_pf_config_vport(adapter, 0, func); 642 } 643 } else { 644 err = qlcnic_sriov_pf_config_vport(adapter, 0, func); 645 } 646 647 if (err) 648 goto err_out; 649 650 cmd->rsp.arg[0] |= (1 << 25); 651 652 if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) 653 set_bit(QLC_BC_VF_STATE, &vf->state); 654 else 655 clear_bit(QLC_BC_VF_STATE, &vf->state); 656 657 return err; 658 659 err_out: 660 cmd->rsp.arg[0] |= (2 << 25); 661 return err; 662 } 663 664 static int qlcnic_sriov_cfg_vf_def_mac(struct qlcnic_adapter *adapter, 665 struct qlcnic_vport *vp, 666 u16 func, u16 vlan, u8 op) 667 { 668 struct qlcnic_cmd_args cmd; 669 struct qlcnic_macvlan_mbx mv; 670 u8 *addr; 671 int err; 672 u32 *buf; 673 int vpid; 674 675 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_CONFIG_MAC_VLAN)) 676 return -ENOMEM; 677 678 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func); 679 if (vpid < 0) { 680 err = -EINVAL; 681 goto out; 682 } 683 684 if (vlan) 685 op = ((op == QLCNIC_MAC_ADD || op == QLCNIC_MAC_VLAN_ADD) ? 686 QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_VLAN_DEL); 687 688 cmd.req.arg[1] = op | (1 << 8) | (3 << 6); 689 cmd.req.arg[1] |= ((vpid & 0xffff) << 16) | BIT_31; 690 691 addr = vp->mac; 692 mv.vlan = vlan; 693 mv.mac_addr0 = addr[0]; 694 mv.mac_addr1 = addr[1]; 695 mv.mac_addr2 = addr[2]; 696 mv.mac_addr3 = addr[3]; 697 mv.mac_addr4 = addr[4]; 698 mv.mac_addr5 = addr[5]; 699 buf = &cmd.req.arg[2]; 700 memcpy(buf, &mv, sizeof(struct qlcnic_macvlan_mbx)); 701 702 err = qlcnic_issue_cmd(adapter, &cmd); 703 704 if (err) 705 dev_err(&adapter->pdev->dev, 706 "MAC-VLAN %s to CAM failed, err=%d.\n", 707 ((op == 1) ? "add " : "delete "), err); 708 709 out: 710 qlcnic_free_mbx_args(&cmd); 711 return err; 712 } 713 714 static int qlcnic_sriov_validate_create_rx_ctx(struct qlcnic_cmd_args *cmd) 715 { 716 if ((cmd->req.arg[0] >> 29) != 0x3) 717 return -EINVAL; 718 719 return 0; 720 } 721 722 static int qlcnic_sriov_pf_create_rx_ctx_cmd(struct qlcnic_bc_trans *tran, 723 struct qlcnic_cmd_args *cmd) 724 { 725 struct qlcnic_vf_info *vf = tran->vf; 726 struct qlcnic_adapter *adapter = vf->adapter; 727 struct qlcnic_rcv_mbx_out *mbx_out; 728 int err; 729 u16 vlan; 730 731 err = qlcnic_sriov_validate_create_rx_ctx(cmd); 732 if (err) { 733 cmd->rsp.arg[0] |= (0x6 << 25); 734 return err; 735 } 736 737 cmd->req.arg[6] = vf->vp->handle; 738 err = qlcnic_issue_cmd(adapter, cmd); 739 740 vlan = vf->vp->vlan; 741 if (!err) { 742 mbx_out = (struct qlcnic_rcv_mbx_out *)&cmd->rsp.arg[1]; 743 vf->rx_ctx_id = mbx_out->ctx_id; 744 qlcnic_sriov_cfg_vf_def_mac(adapter, vf->vp, vf->pci_func, 745 vlan, QLCNIC_MAC_ADD); 746 } else { 747 vf->rx_ctx_id = 0; 748 } 749 750 return err; 751 } 752 753 static int qlcnic_sriov_pf_mac_address_cmd(struct qlcnic_bc_trans *trans, 754 struct qlcnic_cmd_args *cmd) 755 { 756 struct qlcnic_vf_info *vf = trans->vf; 757 u8 type, *mac; 758 759 type = cmd->req.arg[1]; 760 switch (type) { 761 case QLCNIC_SET_STATION_MAC: 762 case QLCNIC_SET_FAC_DEF_MAC: 763 cmd->rsp.arg[0] = (2 << 25); 764 break; 765 case QLCNIC_GET_CURRENT_MAC: 766 cmd->rsp.arg[0] = (1 << 25); 767 mac = vf->vp->mac; 768 cmd->rsp.arg[2] = mac[1] | ((mac[0] << 8) & 0xff00); 769 cmd->rsp.arg[1] = mac[5] | ((mac[4] << 8) & 0xff00) | 770 ((mac[3]) << 16 & 0xff0000) | 771 ((mac[2]) << 24 & 0xff000000); 772 } 773 774 return 0; 775 } 776 777 static int qlcnic_sriov_validate_create_tx_ctx(struct qlcnic_cmd_args *cmd) 778 { 779 if ((cmd->req.arg[0] >> 29) != 0x3) 780 return -EINVAL; 781 782 return 0; 783 } 784 785 static int qlcnic_sriov_pf_create_tx_ctx_cmd(struct qlcnic_bc_trans *trans, 786 struct qlcnic_cmd_args *cmd) 787 { 788 struct qlcnic_vf_info *vf = trans->vf; 789 struct qlcnic_adapter *adapter = vf->adapter; 790 struct qlcnic_tx_mbx_out *mbx_out; 791 int err; 792 793 err = qlcnic_sriov_validate_create_tx_ctx(cmd); 794 if (err) { 795 cmd->rsp.arg[0] |= (0x6 << 25); 796 return err; 797 } 798 799 cmd->req.arg[5] |= vf->vp->handle << 16; 800 err = qlcnic_issue_cmd(adapter, cmd); 801 if (!err) { 802 mbx_out = (struct qlcnic_tx_mbx_out *)&cmd->rsp.arg[2]; 803 vf->tx_ctx_id = mbx_out->ctx_id; 804 } else { 805 vf->tx_ctx_id = 0; 806 } 807 808 return err; 809 } 810 811 static int qlcnic_sriov_validate_del_rx_ctx(struct qlcnic_vf_info *vf, 812 struct qlcnic_cmd_args *cmd) 813 { 814 if ((cmd->req.arg[0] >> 29) != 0x3) 815 return -EINVAL; 816 817 if ((cmd->req.arg[1] & 0xffff) != vf->rx_ctx_id) 818 return -EINVAL; 819 820 return 0; 821 } 822 823 static int qlcnic_sriov_pf_del_rx_ctx_cmd(struct qlcnic_bc_trans *trans, 824 struct qlcnic_cmd_args *cmd) 825 { 826 struct qlcnic_vf_info *vf = trans->vf; 827 struct qlcnic_adapter *adapter = vf->adapter; 828 int err; 829 u16 vlan; 830 831 err = qlcnic_sriov_validate_del_rx_ctx(vf, cmd); 832 if (err) { 833 cmd->rsp.arg[0] |= (0x6 << 25); 834 return err; 835 } 836 837 vlan = vf->vp->vlan; 838 qlcnic_sriov_cfg_vf_def_mac(adapter, vf->vp, vf->pci_func, 839 vlan, QLCNIC_MAC_DEL); 840 cmd->req.arg[1] |= vf->vp->handle << 16; 841 err = qlcnic_issue_cmd(adapter, cmd); 842 843 if (!err) 844 vf->rx_ctx_id = 0; 845 846 return err; 847 } 848 849 static int qlcnic_sriov_validate_del_tx_ctx(struct qlcnic_vf_info *vf, 850 struct qlcnic_cmd_args *cmd) 851 { 852 if ((cmd->req.arg[0] >> 29) != 0x3) 853 return -EINVAL; 854 855 if ((cmd->req.arg[1] & 0xffff) != vf->tx_ctx_id) 856 return -EINVAL; 857 858 return 0; 859 } 860 861 static int qlcnic_sriov_pf_del_tx_ctx_cmd(struct qlcnic_bc_trans *trans, 862 struct qlcnic_cmd_args *cmd) 863 { 864 struct qlcnic_vf_info *vf = trans->vf; 865 struct qlcnic_adapter *adapter = vf->adapter; 866 int err; 867 868 err = qlcnic_sriov_validate_del_tx_ctx(vf, cmd); 869 if (err) { 870 cmd->rsp.arg[0] |= (0x6 << 25); 871 return err; 872 } 873 874 cmd->req.arg[1] |= vf->vp->handle << 16; 875 err = qlcnic_issue_cmd(adapter, cmd); 876 877 if (!err) 878 vf->tx_ctx_id = 0; 879 880 return err; 881 } 882 883 static int qlcnic_sriov_validate_cfg_lro(struct qlcnic_vf_info *vf, 884 struct qlcnic_cmd_args *cmd) 885 { 886 if ((cmd->req.arg[1] >> 16) != vf->rx_ctx_id) 887 return -EINVAL; 888 889 return 0; 890 } 891 892 static int qlcnic_sriov_pf_cfg_lro_cmd(struct qlcnic_bc_trans *trans, 893 struct qlcnic_cmd_args *cmd) 894 { 895 struct qlcnic_vf_info *vf = trans->vf; 896 struct qlcnic_adapter *adapter = vf->adapter; 897 int err; 898 899 err = qlcnic_sriov_validate_cfg_lro(vf, cmd); 900 if (err) { 901 cmd->rsp.arg[0] |= (0x6 << 25); 902 return err; 903 } 904 905 err = qlcnic_issue_cmd(adapter, cmd); 906 return err; 907 } 908 909 static int qlcnic_sriov_pf_cfg_ip_cmd(struct qlcnic_bc_trans *trans, 910 struct qlcnic_cmd_args *cmd) 911 { 912 struct qlcnic_vf_info *vf = trans->vf; 913 struct qlcnic_adapter *adapter = vf->adapter; 914 int err = -EIO; 915 u8 op; 916 917 op = cmd->req.arg[1] & 0xff; 918 919 cmd->req.arg[1] |= vf->vp->handle << 16; 920 cmd->req.arg[1] |= BIT_31; 921 922 err = qlcnic_issue_cmd(adapter, cmd); 923 return err; 924 } 925 926 static int qlcnic_sriov_validate_cfg_intrpt(struct qlcnic_vf_info *vf, 927 struct qlcnic_cmd_args *cmd) 928 { 929 if (((cmd->req.arg[1] >> 8) & 0xff) != vf->pci_func) 930 return -EINVAL; 931 932 if (!(cmd->req.arg[1] & BIT_16)) 933 return -EINVAL; 934 935 if ((cmd->req.arg[1] & 0xff) != 0x1) 936 return -EINVAL; 937 938 return 0; 939 } 940 941 static int qlcnic_sriov_pf_cfg_intrpt_cmd(struct qlcnic_bc_trans *trans, 942 struct qlcnic_cmd_args *cmd) 943 { 944 struct qlcnic_vf_info *vf = trans->vf; 945 struct qlcnic_adapter *adapter = vf->adapter; 946 int err; 947 948 err = qlcnic_sriov_validate_cfg_intrpt(vf, cmd); 949 if (err) 950 cmd->rsp.arg[0] |= (0x6 << 25); 951 else 952 err = qlcnic_issue_cmd(adapter, cmd); 953 954 return err; 955 } 956 957 static int qlcnic_sriov_validate_mtu(struct qlcnic_adapter *adapter, 958 struct qlcnic_vf_info *vf, 959 struct qlcnic_cmd_args *cmd) 960 { 961 if (cmd->req.arg[1] != vf->rx_ctx_id) 962 return -EINVAL; 963 964 if (cmd->req.arg[2] > adapter->ahw->max_mtu) 965 return -EINVAL; 966 967 return 0; 968 } 969 970 static int qlcnic_sriov_pf_set_mtu_cmd(struct qlcnic_bc_trans *trans, 971 struct qlcnic_cmd_args *cmd) 972 { 973 struct qlcnic_vf_info *vf = trans->vf; 974 struct qlcnic_adapter *adapter = vf->adapter; 975 int err; 976 977 err = qlcnic_sriov_validate_mtu(adapter, vf, cmd); 978 if (err) 979 cmd->rsp.arg[0] |= (0x6 << 25); 980 else 981 err = qlcnic_issue_cmd(adapter, cmd); 982 983 return err; 984 } 985 986 static int qlcnic_sriov_validate_get_nic_info(struct qlcnic_vf_info *vf, 987 struct qlcnic_cmd_args *cmd) 988 { 989 if (cmd->req.arg[1] & BIT_31) { 990 if (((cmd->req.arg[1] >> 16) & 0x7fff) != vf->pci_func) 991 return -EINVAL; 992 } else { 993 cmd->req.arg[1] |= vf->vp->handle << 16; 994 } 995 996 return 0; 997 } 998 999 static int qlcnic_sriov_pf_get_nic_info_cmd(struct qlcnic_bc_trans *trans, 1000 struct qlcnic_cmd_args *cmd) 1001 { 1002 struct qlcnic_vf_info *vf = trans->vf; 1003 struct qlcnic_adapter *adapter = vf->adapter; 1004 int err; 1005 1006 err = qlcnic_sriov_validate_get_nic_info(vf, cmd); 1007 if (err) { 1008 cmd->rsp.arg[0] |= (0x6 << 25); 1009 return err; 1010 } 1011 1012 err = qlcnic_issue_cmd(adapter, cmd); 1013 return err; 1014 } 1015 1016 static int qlcnic_sriov_validate_cfg_rss(struct qlcnic_vf_info *vf, 1017 struct qlcnic_cmd_args *cmd) 1018 { 1019 if (cmd->req.arg[1] != vf->rx_ctx_id) 1020 return -EINVAL; 1021 1022 return 0; 1023 } 1024 1025 static int qlcnic_sriov_pf_cfg_rss_cmd(struct qlcnic_bc_trans *trans, 1026 struct qlcnic_cmd_args *cmd) 1027 { 1028 struct qlcnic_vf_info *vf = trans->vf; 1029 struct qlcnic_adapter *adapter = vf->adapter; 1030 int err; 1031 1032 err = qlcnic_sriov_validate_cfg_rss(vf, cmd); 1033 if (err) 1034 cmd->rsp.arg[0] |= (0x6 << 25); 1035 else 1036 err = qlcnic_issue_cmd(adapter, cmd); 1037 1038 return err; 1039 } 1040 1041 static int qlcnic_sriov_validate_cfg_intrcoal(struct qlcnic_adapter *adapter, 1042 struct qlcnic_vf_info *vf, 1043 struct qlcnic_cmd_args *cmd) 1044 { 1045 struct qlcnic_nic_intr_coalesce *coal = &adapter->ahw->coal; 1046 u16 ctx_id, pkts, time; 1047 1048 ctx_id = cmd->req.arg[1] >> 16; 1049 pkts = cmd->req.arg[2] & 0xffff; 1050 time = cmd->req.arg[2] >> 16; 1051 1052 if (ctx_id != vf->rx_ctx_id) 1053 return -EINVAL; 1054 if (pkts > coal->rx_packets) 1055 return -EINVAL; 1056 if (time < coal->rx_time_us) 1057 return -EINVAL; 1058 1059 return 0; 1060 } 1061 1062 static int qlcnic_sriov_pf_cfg_intrcoal_cmd(struct qlcnic_bc_trans *tran, 1063 struct qlcnic_cmd_args *cmd) 1064 { 1065 struct qlcnic_vf_info *vf = tran->vf; 1066 struct qlcnic_adapter *adapter = vf->adapter; 1067 int err; 1068 1069 err = qlcnic_sriov_validate_cfg_intrcoal(adapter, vf, cmd); 1070 if (err) { 1071 cmd->rsp.arg[0] |= (0x6 << 25); 1072 return err; 1073 } 1074 1075 err = qlcnic_issue_cmd(adapter, cmd); 1076 return err; 1077 } 1078 1079 static int qlcnic_sriov_validate_cfg_macvlan(struct qlcnic_adapter *adapter, 1080 struct qlcnic_vf_info *vf, 1081 struct qlcnic_cmd_args *cmd) 1082 { 1083 struct qlcnic_macvlan_mbx *macvlan; 1084 struct qlcnic_vport *vp = vf->vp; 1085 u8 op, new_op; 1086 1087 if (!(cmd->req.arg[1] & BIT_8)) 1088 return -EINVAL; 1089 1090 cmd->req.arg[1] |= (vf->vp->handle << 16); 1091 cmd->req.arg[1] |= BIT_31; 1092 1093 macvlan = (struct qlcnic_macvlan_mbx *)&cmd->req.arg[2]; 1094 if (!(macvlan->mac_addr0 & BIT_0)) { 1095 dev_err(&adapter->pdev->dev, 1096 "MAC address change is not allowed from VF %d", 1097 vf->pci_func); 1098 return -EINVAL; 1099 } 1100 1101 if (vp->vlan_mode == QLC_PVID_MODE) { 1102 op = cmd->req.arg[1] & 0x7; 1103 cmd->req.arg[1] &= ~0x7; 1104 new_op = (op == QLCNIC_MAC_ADD || op == QLCNIC_MAC_VLAN_ADD) ? 1105 QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_VLAN_DEL; 1106 cmd->req.arg[3] |= vp->vlan << 16; 1107 cmd->req.arg[1] |= new_op; 1108 } 1109 1110 return 0; 1111 } 1112 1113 static int qlcnic_sriov_pf_cfg_macvlan_cmd(struct qlcnic_bc_trans *trans, 1114 struct qlcnic_cmd_args *cmd) 1115 { 1116 struct qlcnic_vf_info *vf = trans->vf; 1117 struct qlcnic_adapter *adapter = vf->adapter; 1118 int err; 1119 1120 err = qlcnic_sriov_validate_cfg_macvlan(adapter, vf, cmd); 1121 if (err) { 1122 cmd->rsp.arg[0] |= (0x6 << 25); 1123 return err; 1124 } 1125 1126 err = qlcnic_issue_cmd(adapter, cmd); 1127 return err; 1128 } 1129 1130 static int qlcnic_sriov_validate_linkevent(struct qlcnic_vf_info *vf, 1131 struct qlcnic_cmd_args *cmd) 1132 { 1133 if ((cmd->req.arg[1] >> 16) != vf->rx_ctx_id) 1134 return -EINVAL; 1135 1136 return 0; 1137 } 1138 1139 static int qlcnic_sriov_pf_linkevent_cmd(struct qlcnic_bc_trans *trans, 1140 struct qlcnic_cmd_args *cmd) 1141 { 1142 struct qlcnic_vf_info *vf = trans->vf; 1143 struct qlcnic_adapter *adapter = vf->adapter; 1144 int err; 1145 1146 err = qlcnic_sriov_validate_linkevent(vf, cmd); 1147 if (err) { 1148 cmd->rsp.arg[0] |= (0x6 << 25); 1149 return err; 1150 } 1151 1152 err = qlcnic_issue_cmd(adapter, cmd); 1153 return err; 1154 } 1155 1156 static int qlcnic_sriov_pf_cfg_promisc_cmd(struct qlcnic_bc_trans *trans, 1157 struct qlcnic_cmd_args *cmd) 1158 { 1159 struct qlcnic_vf_info *vf = trans->vf; 1160 struct qlcnic_adapter *adapter = vf->adapter; 1161 int err; 1162 1163 cmd->req.arg[1] |= vf->vp->handle << 16; 1164 cmd->req.arg[1] |= BIT_31; 1165 err = qlcnic_issue_cmd(adapter, cmd); 1166 return err; 1167 } 1168 1169 static int qlcnic_sriov_pf_get_acl_cmd(struct qlcnic_bc_trans *trans, 1170 struct qlcnic_cmd_args *cmd) 1171 { 1172 struct qlcnic_vf_info *vf = trans->vf; 1173 struct qlcnic_vport *vp = vf->vp; 1174 u8 cmd_op, mode = vp->vlan_mode; 1175 1176 cmd_op = trans->req_hdr->cmd_op; 1177 cmd->rsp.arg[0] = (cmd_op & 0xffff) | 14 << 16 | 1 << 25; 1178 1179 switch (mode) { 1180 case QLC_GUEST_VLAN_MODE: 1181 cmd->rsp.arg[1] = mode | 1 << 8; 1182 cmd->rsp.arg[2] = 1 << 16; 1183 break; 1184 case QLC_PVID_MODE: 1185 cmd->rsp.arg[1] = mode | 1 << 8 | vp->vlan << 16; 1186 break; 1187 } 1188 1189 return 0; 1190 } 1191 1192 static int qlcnic_sriov_pf_del_guest_vlan(struct qlcnic_adapter *adapter, 1193 struct qlcnic_vf_info *vf) 1194 1195 { 1196 struct qlcnic_vport *vp = vf->vp; 1197 1198 if (!vp->vlan) 1199 return -EINVAL; 1200 1201 if (!vf->rx_ctx_id) { 1202 vp->vlan = 0; 1203 return 0; 1204 } 1205 1206 qlcnic_sriov_cfg_vf_def_mac(adapter, vp, vf->pci_func, 1207 vp->vlan, QLCNIC_MAC_DEL); 1208 vp->vlan = 0; 1209 qlcnic_sriov_cfg_vf_def_mac(adapter, vp, vf->pci_func, 1210 0, QLCNIC_MAC_ADD); 1211 return 0; 1212 } 1213 1214 static int qlcnic_sriov_pf_add_guest_vlan(struct qlcnic_adapter *adapter, 1215 struct qlcnic_vf_info *vf, 1216 struct qlcnic_cmd_args *cmd) 1217 { 1218 struct qlcnic_vport *vp = vf->vp; 1219 int err = -EIO; 1220 1221 if (vp->vlan) 1222 return err; 1223 1224 if (!vf->rx_ctx_id) { 1225 vp->vlan = cmd->req.arg[1] >> 16; 1226 return 0; 1227 } 1228 1229 err = qlcnic_sriov_cfg_vf_def_mac(adapter, vp, vf->pci_func, 1230 0, QLCNIC_MAC_DEL); 1231 if (err) 1232 return err; 1233 1234 vp->vlan = cmd->req.arg[1] >> 16; 1235 err = qlcnic_sriov_cfg_vf_def_mac(adapter, vp, vf->pci_func, 1236 vp->vlan, QLCNIC_MAC_ADD); 1237 1238 if (err) { 1239 qlcnic_sriov_cfg_vf_def_mac(adapter, vp, vf->pci_func, 1240 0, QLCNIC_MAC_ADD); 1241 vp->vlan = 0; 1242 } 1243 1244 return err; 1245 } 1246 1247 static int qlcnic_sriov_pf_cfg_guest_vlan_cmd(struct qlcnic_bc_trans *tran, 1248 struct qlcnic_cmd_args *cmd) 1249 { 1250 struct qlcnic_vf_info *vf = tran->vf; 1251 struct qlcnic_adapter *adapter = vf->adapter; 1252 struct qlcnic_vport *vp = vf->vp; 1253 int err = -EIO; 1254 u8 op; 1255 1256 if (vp->vlan_mode != QLC_GUEST_VLAN_MODE) { 1257 cmd->rsp.arg[0] |= 2 << 25; 1258 return err; 1259 } 1260 1261 op = cmd->req.arg[1] & 0xf; 1262 1263 if (op) 1264 err = qlcnic_sriov_pf_add_guest_vlan(adapter, vf, cmd); 1265 else 1266 err = qlcnic_sriov_pf_del_guest_vlan(adapter, vf); 1267 1268 cmd->rsp.arg[0] |= err ? 2 << 25 : 1 << 25; 1269 return err; 1270 } 1271 1272 static const int qlcnic_pf_passthru_supp_cmds[] = { 1273 QLCNIC_CMD_GET_STATISTICS, 1274 QLCNIC_CMD_GET_PORT_CONFIG, 1275 QLCNIC_CMD_GET_LINK_STATUS, 1276 }; 1277 1278 static const struct qlcnic_sriov_cmd_handler qlcnic_pf_bc_cmd_hdlr[] = { 1279 [QLCNIC_BC_CMD_CHANNEL_INIT] = {&qlcnic_sriov_pf_channel_cfg_cmd}, 1280 [QLCNIC_BC_CMD_CHANNEL_TERM] = {&qlcnic_sriov_pf_channel_cfg_cmd}, 1281 [QLCNIC_BC_CMD_GET_ACL] = {&qlcnic_sriov_pf_get_acl_cmd}, 1282 [QLCNIC_BC_CMD_CFG_GUEST_VLAN] = {&qlcnic_sriov_pf_cfg_guest_vlan_cmd}, 1283 }; 1284 1285 static const struct qlcnic_sriov_fw_cmd_handler qlcnic_pf_fw_cmd_hdlr[] = { 1286 {QLCNIC_CMD_CREATE_RX_CTX, qlcnic_sriov_pf_create_rx_ctx_cmd}, 1287 {QLCNIC_CMD_CREATE_TX_CTX, qlcnic_sriov_pf_create_tx_ctx_cmd}, 1288 {QLCNIC_CMD_MAC_ADDRESS, qlcnic_sriov_pf_mac_address_cmd}, 1289 {QLCNIC_CMD_DESTROY_RX_CTX, qlcnic_sriov_pf_del_rx_ctx_cmd}, 1290 {QLCNIC_CMD_DESTROY_TX_CTX, qlcnic_sriov_pf_del_tx_ctx_cmd}, 1291 {QLCNIC_CMD_CONFIGURE_HW_LRO, qlcnic_sriov_pf_cfg_lro_cmd}, 1292 {QLCNIC_CMD_CONFIGURE_IP_ADDR, qlcnic_sriov_pf_cfg_ip_cmd}, 1293 {QLCNIC_CMD_CONFIG_INTRPT, qlcnic_sriov_pf_cfg_intrpt_cmd}, 1294 {QLCNIC_CMD_SET_MTU, qlcnic_sriov_pf_set_mtu_cmd}, 1295 {QLCNIC_CMD_GET_NIC_INFO, qlcnic_sriov_pf_get_nic_info_cmd}, 1296 {QLCNIC_CMD_CONFIGURE_RSS, qlcnic_sriov_pf_cfg_rss_cmd}, 1297 {QLCNIC_CMD_CONFIG_INTR_COAL, qlcnic_sriov_pf_cfg_intrcoal_cmd}, 1298 {QLCNIC_CMD_CONFIG_MAC_VLAN, qlcnic_sriov_pf_cfg_macvlan_cmd}, 1299 {QLCNIC_CMD_GET_LINK_EVENT, qlcnic_sriov_pf_linkevent_cmd}, 1300 {QLCNIC_CMD_CONFIGURE_MAC_RX_MODE, qlcnic_sriov_pf_cfg_promisc_cmd}, 1301 }; 1302 1303 void qlcnic_sriov_pf_process_bc_cmd(struct qlcnic_adapter *adapter, 1304 struct qlcnic_bc_trans *trans, 1305 struct qlcnic_cmd_args *cmd) 1306 { 1307 u8 size, cmd_op; 1308 1309 cmd_op = trans->req_hdr->cmd_op; 1310 1311 if (trans->req_hdr->op_type == QLC_BC_CMD) { 1312 size = ARRAY_SIZE(qlcnic_pf_bc_cmd_hdlr); 1313 if (cmd_op < size) { 1314 qlcnic_pf_bc_cmd_hdlr[cmd_op].fn(trans, cmd); 1315 return; 1316 } 1317 } else { 1318 int i; 1319 size = ARRAY_SIZE(qlcnic_pf_fw_cmd_hdlr); 1320 for (i = 0; i < size; i++) { 1321 if (cmd_op == qlcnic_pf_fw_cmd_hdlr[i].cmd) { 1322 qlcnic_pf_fw_cmd_hdlr[i].fn(trans, cmd); 1323 return; 1324 } 1325 } 1326 1327 size = ARRAY_SIZE(qlcnic_pf_passthru_supp_cmds); 1328 for (i = 0; i < size; i++) { 1329 if (cmd_op == qlcnic_pf_passthru_supp_cmds[i]) { 1330 qlcnic_issue_cmd(adapter, cmd); 1331 return; 1332 } 1333 } 1334 } 1335 1336 cmd->rsp.arg[0] |= (0x9 << 25); 1337 } 1338 1339 void qlcnic_pf_set_interface_id_create_rx_ctx(struct qlcnic_adapter *adapter, 1340 u32 *int_id) 1341 { 1342 u16 vpid; 1343 1344 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, 1345 adapter->ahw->pci_func); 1346 *int_id |= vpid; 1347 } 1348 1349 void qlcnic_pf_set_interface_id_del_rx_ctx(struct qlcnic_adapter *adapter, 1350 u32 *int_id) 1351 { 1352 u16 vpid; 1353 1354 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, 1355 adapter->ahw->pci_func); 1356 *int_id |= vpid << 16; 1357 } 1358 1359 void qlcnic_pf_set_interface_id_create_tx_ctx(struct qlcnic_adapter *adapter, 1360 u32 *int_id) 1361 { 1362 int vpid; 1363 1364 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, 1365 adapter->ahw->pci_func); 1366 *int_id |= vpid << 16; 1367 } 1368 1369 void qlcnic_pf_set_interface_id_del_tx_ctx(struct qlcnic_adapter *adapter, 1370 u32 *int_id) 1371 { 1372 u16 vpid; 1373 1374 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, 1375 adapter->ahw->pci_func); 1376 *int_id |= vpid << 16; 1377 } 1378 1379 void qlcnic_pf_set_interface_id_promisc(struct qlcnic_adapter *adapter, 1380 u32 *int_id) 1381 { 1382 u16 vpid; 1383 1384 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, 1385 adapter->ahw->pci_func); 1386 *int_id |= (vpid << 16) | BIT_31; 1387 } 1388 1389 void qlcnic_pf_set_interface_id_ipaddr(struct qlcnic_adapter *adapter, 1390 u32 *int_id) 1391 { 1392 u16 vpid; 1393 1394 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, 1395 adapter->ahw->pci_func); 1396 *int_id |= (vpid << 16) | BIT_31; 1397 } 1398 1399 void qlcnic_pf_set_interface_id_macaddr(struct qlcnic_adapter *adapter, 1400 u32 *int_id) 1401 { 1402 u16 vpid; 1403 1404 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, 1405 adapter->ahw->pci_func); 1406 *int_id |= (vpid << 16) | BIT_31; 1407 } 1408 1409 static void qlcnic_sriov_del_rx_ctx(struct qlcnic_adapter *adapter, 1410 struct qlcnic_vf_info *vf) 1411 { 1412 struct qlcnic_cmd_args cmd; 1413 int vpid; 1414 1415 if (!vf->rx_ctx_id) 1416 return; 1417 1418 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_DESTROY_RX_CTX)) 1419 return; 1420 1421 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func); 1422 if (vpid >= 0) { 1423 cmd.req.arg[1] = vf->rx_ctx_id | (vpid & 0xffff) << 16; 1424 if (qlcnic_issue_cmd(adapter, &cmd)) 1425 dev_err(&adapter->pdev->dev, 1426 "Failed to delete Tx ctx in firmware for func 0x%x\n", 1427 vf->pci_func); 1428 else 1429 vf->rx_ctx_id = 0; 1430 } 1431 1432 qlcnic_free_mbx_args(&cmd); 1433 } 1434 1435 static void qlcnic_sriov_del_tx_ctx(struct qlcnic_adapter *adapter, 1436 struct qlcnic_vf_info *vf) 1437 { 1438 struct qlcnic_cmd_args cmd; 1439 int vpid; 1440 1441 if (!vf->tx_ctx_id) 1442 return; 1443 1444 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_DESTROY_TX_CTX)) 1445 return; 1446 1447 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func); 1448 if (vpid >= 0) { 1449 cmd.req.arg[1] |= vf->tx_ctx_id | (vpid & 0xffff) << 16; 1450 if (qlcnic_issue_cmd(adapter, &cmd)) 1451 dev_err(&adapter->pdev->dev, 1452 "Failed to delete Tx ctx in firmware for func 0x%x\n", 1453 vf->pci_func); 1454 else 1455 vf->tx_ctx_id = 0; 1456 } 1457 1458 qlcnic_free_mbx_args(&cmd); 1459 } 1460 1461 static int qlcnic_sriov_add_act_list_irqsave(struct qlcnic_sriov *sriov, 1462 struct qlcnic_vf_info *vf, 1463 struct qlcnic_bc_trans *trans) 1464 { 1465 struct qlcnic_trans_list *t_list = &vf->rcv_act; 1466 unsigned long flag; 1467 1468 spin_lock_irqsave(&t_list->lock, flag); 1469 1470 __qlcnic_sriov_add_act_list(sriov, vf, trans); 1471 1472 spin_unlock_irqrestore(&t_list->lock, flag); 1473 return 0; 1474 } 1475 1476 static void __qlcnic_sriov_process_flr(struct qlcnic_vf_info *vf) 1477 { 1478 struct qlcnic_adapter *adapter = vf->adapter; 1479 1480 qlcnic_sriov_cleanup_list(&vf->rcv_pend); 1481 cancel_work_sync(&vf->trans_work); 1482 qlcnic_sriov_cleanup_list(&vf->rcv_act); 1483 1484 if (test_bit(QLC_BC_VF_SOFT_FLR, &vf->state)) { 1485 qlcnic_sriov_del_tx_ctx(adapter, vf); 1486 qlcnic_sriov_del_rx_ctx(adapter, vf); 1487 } 1488 1489 qlcnic_sriov_pf_config_vport(adapter, 0, vf->pci_func); 1490 1491 clear_bit(QLC_BC_VF_FLR, &vf->state); 1492 if (test_bit(QLC_BC_VF_SOFT_FLR, &vf->state)) { 1493 qlcnic_sriov_add_act_list_irqsave(adapter->ahw->sriov, vf, 1494 vf->flr_trans); 1495 clear_bit(QLC_BC_VF_SOFT_FLR, &vf->state); 1496 vf->flr_trans = NULL; 1497 } 1498 } 1499 1500 static void qlcnic_sriov_pf_process_flr(struct work_struct *work) 1501 { 1502 struct qlcnic_vf_info *vf; 1503 1504 vf = container_of(work, struct qlcnic_vf_info, flr_work); 1505 __qlcnic_sriov_process_flr(vf); 1506 return; 1507 } 1508 1509 static void qlcnic_sriov_schedule_flr(struct qlcnic_sriov *sriov, 1510 struct qlcnic_vf_info *vf, 1511 work_func_t func) 1512 { 1513 if (test_bit(__QLCNIC_RESETTING, &vf->adapter->state)) 1514 return; 1515 1516 INIT_WORK(&vf->flr_work, func); 1517 queue_work(sriov->bc.bc_flr_wq, &vf->flr_work); 1518 } 1519 1520 static void qlcnic_sriov_handle_soft_flr(struct qlcnic_adapter *adapter, 1521 struct qlcnic_bc_trans *trans, 1522 struct qlcnic_vf_info *vf) 1523 { 1524 struct qlcnic_sriov *sriov = adapter->ahw->sriov; 1525 1526 set_bit(QLC_BC_VF_FLR, &vf->state); 1527 clear_bit(QLC_BC_VF_STATE, &vf->state); 1528 set_bit(QLC_BC_VF_SOFT_FLR, &vf->state); 1529 vf->flr_trans = trans; 1530 qlcnic_sriov_schedule_flr(sriov, vf, qlcnic_sriov_pf_process_flr); 1531 netdev_info(adapter->netdev, "Software FLR for PCI func %d\n", 1532 vf->pci_func); 1533 } 1534 1535 bool qlcnic_sriov_soft_flr_check(struct qlcnic_adapter *adapter, 1536 struct qlcnic_bc_trans *trans, 1537 struct qlcnic_vf_info *vf) 1538 { 1539 struct qlcnic_bc_hdr *hdr = trans->req_hdr; 1540 1541 if ((hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) && 1542 (hdr->op_type == QLC_BC_CMD) && 1543 test_bit(QLC_BC_VF_STATE, &vf->state)) { 1544 qlcnic_sriov_handle_soft_flr(adapter, trans, vf); 1545 return true; 1546 } 1547 1548 return false; 1549 } 1550 1551 void qlcnic_sriov_pf_handle_flr(struct qlcnic_sriov *sriov, 1552 struct qlcnic_vf_info *vf) 1553 { 1554 struct net_device *dev = vf->adapter->netdev; 1555 1556 if (!test_and_clear_bit(QLC_BC_VF_STATE, &vf->state)) { 1557 clear_bit(QLC_BC_VF_FLR, &vf->state); 1558 return; 1559 } 1560 1561 if (test_and_set_bit(QLC_BC_VF_FLR, &vf->state)) { 1562 netdev_info(dev, "FLR for PCI func %d in progress\n", 1563 vf->pci_func); 1564 return; 1565 } 1566 1567 qlcnic_sriov_schedule_flr(sriov, vf, qlcnic_sriov_pf_process_flr); 1568 netdev_info(dev, "FLR received for PCI func %d\n", vf->pci_func); 1569 } 1570 1571 void qlcnic_sriov_pf_reset(struct qlcnic_adapter *adapter) 1572 { 1573 struct qlcnic_hardware_context *ahw = adapter->ahw; 1574 struct qlcnic_sriov *sriov = ahw->sriov; 1575 struct qlcnic_vf_info *vf; 1576 u16 num_vfs = sriov->num_vfs; 1577 int i; 1578 1579 for (i = 0; i < num_vfs; i++) { 1580 vf = &sriov->vf_info[i]; 1581 vf->rx_ctx_id = 0; 1582 vf->tx_ctx_id = 0; 1583 cancel_work_sync(&vf->flr_work); 1584 __qlcnic_sriov_process_flr(vf); 1585 clear_bit(QLC_BC_VF_STATE, &vf->state); 1586 } 1587 1588 qlcnic_sriov_pf_reset_vport_handle(adapter, ahw->pci_func); 1589 QLCWRX(ahw, QLCNIC_MBX_INTR_ENBL, (ahw->num_msix - 1) << 8); 1590 } 1591 1592 int qlcnic_sriov_pf_reinit(struct qlcnic_adapter *adapter) 1593 { 1594 struct qlcnic_hardware_context *ahw = adapter->ahw; 1595 int err; 1596 1597 if (!qlcnic_sriov_enable_check(adapter)) 1598 return 0; 1599 1600 ahw->op_mode = QLCNIC_SRIOV_PF_FUNC; 1601 1602 err = qlcnic_sriov_pf_init(adapter); 1603 if (err) 1604 return err; 1605 1606 dev_info(&adapter->pdev->dev, "%s: op_mode %d\n", 1607 __func__, ahw->op_mode); 1608 return err; 1609 } 1610 1611 int qlcnic_sriov_set_vf_mac(struct net_device *netdev, int vf, u8 *mac) 1612 { 1613 struct qlcnic_adapter *adapter = netdev_priv(netdev); 1614 struct qlcnic_sriov *sriov = adapter->ahw->sriov; 1615 int i, num_vfs = sriov->num_vfs; 1616 struct qlcnic_vf_info *vf_info; 1617 u8 *curr_mac; 1618 1619 if (!qlcnic_sriov_pf_check(adapter)) 1620 return -EOPNOTSUPP; 1621 1622 if (!is_valid_ether_addr(mac) || vf >= num_vfs) 1623 return -EINVAL; 1624 1625 if (!compare_ether_addr(adapter->mac_addr, mac)) { 1626 netdev_err(netdev, "MAC address is already in use by the PF\n"); 1627 return -EINVAL; 1628 } 1629 1630 for (i = 0; i < num_vfs; i++) { 1631 vf_info = &sriov->vf_info[i]; 1632 if (!compare_ether_addr(vf_info->vp->mac, mac)) { 1633 netdev_err(netdev, 1634 "MAC address is already in use by VF %d\n", 1635 i); 1636 return -EINVAL; 1637 } 1638 } 1639 1640 vf_info = &sriov->vf_info[vf]; 1641 curr_mac = vf_info->vp->mac; 1642 1643 if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) { 1644 netdev_err(netdev, 1645 "MAC address change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n", 1646 vf); 1647 return -EOPNOTSUPP; 1648 } 1649 1650 memcpy(curr_mac, mac, netdev->addr_len); 1651 netdev_info(netdev, "MAC Address %pM is configured for VF %d\n", 1652 mac, vf); 1653 return 0; 1654 } 1655 1656 int qlcnic_sriov_set_vf_tx_rate(struct net_device *netdev, int vf, int tx_rate) 1657 { 1658 struct qlcnic_adapter *adapter = netdev_priv(netdev); 1659 struct qlcnic_sriov *sriov = adapter->ahw->sriov; 1660 struct qlcnic_vf_info *vf_info; 1661 struct qlcnic_info nic_info; 1662 struct qlcnic_vport *vp; 1663 u16 vpid; 1664 1665 if (!qlcnic_sriov_pf_check(adapter)) 1666 return -EOPNOTSUPP; 1667 1668 if (vf >= sriov->num_vfs) 1669 return -EINVAL; 1670 1671 if (tx_rate >= 10000 || tx_rate < 100) { 1672 netdev_err(netdev, 1673 "Invalid Tx rate, allowed range is [%d - %d]", 1674 QLC_VF_MIN_TX_RATE, QLC_VF_MAX_TX_RATE); 1675 return -EINVAL; 1676 } 1677 1678 if (tx_rate == 0) 1679 tx_rate = 10000; 1680 1681 vf_info = &sriov->vf_info[vf]; 1682 vp = vf_info->vp; 1683 vpid = vp->handle; 1684 1685 if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) { 1686 if (qlcnic_sriov_get_vf_vport_info(adapter, &nic_info, vpid)) 1687 return -EIO; 1688 1689 nic_info.max_tx_bw = tx_rate / 100; 1690 nic_info.bit_offsets = BIT_0; 1691 1692 if (qlcnic_sriov_pf_set_vport_info(adapter, &nic_info, vpid)) 1693 return -EIO; 1694 } 1695 1696 vp->max_tx_bw = tx_rate / 100; 1697 netdev_info(netdev, 1698 "Setting Tx rate %d (Mbps), %d %% of PF bandwidth, for VF %d\n", 1699 tx_rate, vp->max_tx_bw, vf); 1700 return 0; 1701 } 1702 1703 int qlcnic_sriov_set_vf_vlan(struct net_device *netdev, int vf, 1704 u16 vlan, u8 qos) 1705 { 1706 struct qlcnic_adapter *adapter = netdev_priv(netdev); 1707 struct qlcnic_sriov *sriov = adapter->ahw->sriov; 1708 struct qlcnic_vf_info *vf_info; 1709 struct qlcnic_vport *vp; 1710 1711 if (!qlcnic_sriov_pf_check(adapter)) 1712 return -EOPNOTSUPP; 1713 1714 if (vf >= sriov->num_vfs || qos > 7) 1715 return -EINVAL; 1716 1717 if (vlan > MAX_VLAN_ID) { 1718 netdev_err(netdev, 1719 "Invalid VLAN ID, allowed range is [0 - %d]\n", 1720 MAX_VLAN_ID); 1721 return -EINVAL; 1722 } 1723 1724 vf_info = &sriov->vf_info[vf]; 1725 vp = vf_info->vp; 1726 if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) { 1727 netdev_err(netdev, 1728 "VLAN change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n", 1729 vf); 1730 return -EOPNOTSUPP; 1731 } 1732 1733 switch (vlan) { 1734 case 4095: 1735 vp->vlan_mode = QLC_GUEST_VLAN_MODE; 1736 break; 1737 case 0: 1738 vp->vlan_mode = QLC_NO_VLAN_MODE; 1739 vp->vlan = 0; 1740 vp->qos = 0; 1741 break; 1742 default: 1743 vp->vlan_mode = QLC_PVID_MODE; 1744 vp->vlan = vlan; 1745 vp->qos = qos; 1746 } 1747 1748 netdev_info(netdev, "Setting VLAN %d, QoS %d, for VF %d\n", 1749 vlan, qos, vf); 1750 return 0; 1751 } 1752 1753 int qlcnic_sriov_get_vf_config(struct net_device *netdev, 1754 int vf, struct ifla_vf_info *ivi) 1755 { 1756 struct qlcnic_adapter *adapter = netdev_priv(netdev); 1757 struct qlcnic_sriov *sriov = adapter->ahw->sriov; 1758 struct qlcnic_vport *vp; 1759 1760 if (!qlcnic_sriov_pf_check(adapter)) 1761 return -EOPNOTSUPP; 1762 1763 if (vf >= sriov->num_vfs) 1764 return -EINVAL; 1765 1766 vp = sriov->vf_info[vf].vp; 1767 memcpy(&ivi->mac, vp->mac, ETH_ALEN); 1768 ivi->vlan = vp->vlan; 1769 ivi->qos = vp->qos; 1770 if (vp->max_tx_bw == MAX_BW) 1771 ivi->tx_rate = 0; 1772 else 1773 ivi->tx_rate = vp->max_tx_bw * 100; 1774 1775 ivi->vf = vf; 1776 return 0; 1777 } 1778