1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2013 - 2018 Intel Corporation. */ 3 4 #include "fm10k.h" 5 #include "fm10k_vf.h" 6 #include "fm10k_pf.h" 7 8 static s32 fm10k_iov_msg_error(struct fm10k_hw *hw, u32 **results, 9 struct fm10k_mbx_info *mbx) 10 { 11 struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx; 12 struct fm10k_intfc *interface = hw->back; 13 struct pci_dev *pdev = interface->pdev; 14 15 dev_err(&pdev->dev, "Unknown message ID %u on VF %d\n", 16 **results & FM10K_TLV_ID_MASK, vf_info->vf_idx); 17 18 return fm10k_tlv_msg_error(hw, results, mbx); 19 } 20 21 /** 22 * fm10k_iov_msg_queue_mac_vlan - Message handler for MAC/VLAN request from VF 23 * @hw: Pointer to hardware structure 24 * @results: Pointer array to message, results[0] is pointer to message 25 * @mbx: Pointer to mailbox information structure 26 * 27 * This function is a custom handler for MAC/VLAN requests from the VF. The 28 * assumption is that it is acceptable to directly hand off the message from 29 * the VF to the PF's switch manager. However, we use a MAC/VLAN message 30 * queue to avoid overloading the mailbox when a large number of requests 31 * come in. 32 **/ 33 static s32 fm10k_iov_msg_queue_mac_vlan(struct fm10k_hw *hw, u32 **results, 34 struct fm10k_mbx_info *mbx) 35 { 36 struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx; 37 struct fm10k_intfc *interface = hw->back; 38 u8 mac[ETH_ALEN]; 39 u32 *result; 40 int err = 0; 41 bool set; 42 u16 vlan; 43 u32 vid; 44 45 /* we shouldn't be updating rules on a disabled interface */ 46 if (!FM10K_VF_FLAG_ENABLED(vf_info)) 47 err = FM10K_ERR_PARAM; 48 49 if (!err && !!results[FM10K_MAC_VLAN_MSG_VLAN]) { 50 result = results[FM10K_MAC_VLAN_MSG_VLAN]; 51 52 /* record VLAN id requested */ 53 err = fm10k_tlv_attr_get_u32(result, &vid); 54 if (err) 55 return err; 56 57 set = !(vid & FM10K_VLAN_CLEAR); 58 vid &= ~FM10K_VLAN_CLEAR; 59 60 /* if the length field has been set, this is a multi-bit 61 * update request. For multi-bit requests, simply disallow 62 * them when the pf_vid has been set. In this case, the PF 63 * should have already cleared the VLAN_TABLE, and if we 64 * allowed them, it could allow a rogue VF to receive traffic 65 * on a VLAN it was not assigned. In the single-bit case, we 66 * need to modify requests for VLAN 0 to use the default PF or 67 * SW vid when assigned. 68 */ 69 70 if (vid >> 16) { 71 /* prevent multi-bit requests when PF has 72 * administratively set the VLAN for this VF 73 */ 74 if (vf_info->pf_vid) 75 return FM10K_ERR_PARAM; 76 } else { 77 err = fm10k_iov_select_vid(vf_info, (u16)vid); 78 if (err < 0) 79 return err; 80 81 vid = err; 82 } 83 84 /* update VSI info for VF in regards to VLAN table */ 85 err = hw->mac.ops.update_vlan(hw, vid, vf_info->vsi, set); 86 } 87 88 if (!err && !!results[FM10K_MAC_VLAN_MSG_MAC]) { 89 result = results[FM10K_MAC_VLAN_MSG_MAC]; 90 91 /* record unicast MAC address requested */ 92 err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan); 93 if (err) 94 return err; 95 96 /* block attempts to set MAC for a locked device */ 97 if (is_valid_ether_addr(vf_info->mac) && 98 !ether_addr_equal(mac, vf_info->mac)) 99 return FM10K_ERR_PARAM; 100 101 set = !(vlan & FM10K_VLAN_CLEAR); 102 vlan &= ~FM10K_VLAN_CLEAR; 103 104 err = fm10k_iov_select_vid(vf_info, vlan); 105 if (err < 0) 106 return err; 107 108 vlan = (u16)err; 109 110 /* Add this request to the MAC/VLAN queue */ 111 err = fm10k_queue_mac_request(interface, vf_info->glort, 112 mac, vlan, set); 113 } 114 115 if (!err && !!results[FM10K_MAC_VLAN_MSG_MULTICAST]) { 116 result = results[FM10K_MAC_VLAN_MSG_MULTICAST]; 117 118 /* record multicast MAC address requested */ 119 err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan); 120 if (err) 121 return err; 122 123 /* verify that the VF is allowed to request multicast */ 124 if (!(vf_info->vf_flags & FM10K_VF_FLAG_MULTI_ENABLED)) 125 return FM10K_ERR_PARAM; 126 127 set = !(vlan & FM10K_VLAN_CLEAR); 128 vlan &= ~FM10K_VLAN_CLEAR; 129 130 err = fm10k_iov_select_vid(vf_info, vlan); 131 if (err < 0) 132 return err; 133 134 vlan = (u16)err; 135 136 /* Add this request to the MAC/VLAN queue */ 137 err = fm10k_queue_mac_request(interface, vf_info->glort, 138 mac, vlan, set); 139 } 140 141 return err; 142 } 143 144 static const struct fm10k_msg_data iov_mbx_data[] = { 145 FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test), 146 FM10K_VF_MSG_MSIX_HANDLER(fm10k_iov_msg_msix_pf), 147 FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_iov_msg_queue_mac_vlan), 148 FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_iov_msg_lport_state_pf), 149 FM10K_TLV_MSG_ERROR_HANDLER(fm10k_iov_msg_error), 150 }; 151 152 s32 fm10k_iov_event(struct fm10k_intfc *interface) 153 { 154 struct fm10k_hw *hw = &interface->hw; 155 struct fm10k_iov_data *iov_data; 156 s64 vflre; 157 int i; 158 159 /* if there is no iov_data then there is no mailbox to process */ 160 if (!READ_ONCE(interface->iov_data)) 161 return 0; 162 163 rcu_read_lock(); 164 165 iov_data = interface->iov_data; 166 167 /* check again now that we are in the RCU block */ 168 if (!iov_data) 169 goto read_unlock; 170 171 if (!(fm10k_read_reg(hw, FM10K_EICR) & FM10K_EICR_VFLR)) 172 goto read_unlock; 173 174 /* read VFLRE to determine if any VFs have been reset */ 175 vflre = fm10k_read_reg(hw, FM10K_PFVFLRE(1)); 176 vflre <<= 32; 177 vflre |= fm10k_read_reg(hw, FM10K_PFVFLRE(0)); 178 179 i = iov_data->num_vfs; 180 181 for (vflre <<= 64 - i; vflre && i--; vflre += vflre) { 182 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i]; 183 184 if (vflre >= 0) 185 continue; 186 187 hw->iov.ops.reset_resources(hw, vf_info); 188 vf_info->mbx.ops.connect(hw, &vf_info->mbx); 189 } 190 191 read_unlock: 192 rcu_read_unlock(); 193 194 return 0; 195 } 196 197 s32 fm10k_iov_mbx(struct fm10k_intfc *interface) 198 { 199 struct fm10k_hw *hw = &interface->hw; 200 struct fm10k_iov_data *iov_data; 201 int i; 202 203 /* if there is no iov_data then there is no mailbox to process */ 204 if (!READ_ONCE(interface->iov_data)) 205 return 0; 206 207 rcu_read_lock(); 208 209 iov_data = interface->iov_data; 210 211 /* check again now that we are in the RCU block */ 212 if (!iov_data) 213 goto read_unlock; 214 215 /* lock the mailbox for transmit and receive */ 216 fm10k_mbx_lock(interface); 217 218 /* Most VF messages sent to the PF cause the PF to respond by 219 * requesting from the SM mailbox. This means that too many VF 220 * messages processed at once could cause a mailbox timeout on the PF. 221 * To prevent this, store a pointer to the next VF mbx to process. Use 222 * that as the start of the loop so that we don't starve whichever VF 223 * got ignored on the previous run. 224 */ 225 process_mbx: 226 for (i = iov_data->next_vf_mbx ? : iov_data->num_vfs; i--;) { 227 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i]; 228 struct fm10k_mbx_info *mbx = &vf_info->mbx; 229 u16 glort = vf_info->glort; 230 231 /* process the SM mailbox first to drain outgoing messages */ 232 hw->mbx.ops.process(hw, &hw->mbx); 233 234 /* verify port mapping is valid, if not reset port */ 235 if (vf_info->vf_flags && !fm10k_glort_valid_pf(hw, glort)) { 236 hw->iov.ops.reset_lport(hw, vf_info); 237 fm10k_clear_macvlan_queue(interface, glort, false); 238 } 239 240 /* reset VFs that have mailbox timed out */ 241 if (!mbx->timeout) { 242 hw->iov.ops.reset_resources(hw, vf_info); 243 mbx->ops.connect(hw, mbx); 244 } 245 246 /* guarantee we have free space in the SM mailbox */ 247 if (!hw->mbx.ops.tx_ready(&hw->mbx, FM10K_VFMBX_MSG_MTU)) { 248 /* keep track of how many times this occurs */ 249 interface->hw_sm_mbx_full++; 250 251 /* make sure we try again momentarily */ 252 fm10k_service_event_schedule(interface); 253 254 break; 255 } 256 257 /* cleanup mailbox and process received messages */ 258 mbx->ops.process(hw, mbx); 259 } 260 261 /* if we stopped processing mailboxes early, update next_vf_mbx. 262 * Otherwise, reset next_vf_mbx, and restart loop so that we process 263 * the remaining mailboxes we skipped at the start. 264 */ 265 if (i >= 0) { 266 iov_data->next_vf_mbx = i + 1; 267 } else if (iov_data->next_vf_mbx) { 268 iov_data->next_vf_mbx = 0; 269 goto process_mbx; 270 } 271 272 /* free the lock */ 273 fm10k_mbx_unlock(interface); 274 275 read_unlock: 276 rcu_read_unlock(); 277 278 return 0; 279 } 280 281 void fm10k_iov_suspend(struct pci_dev *pdev) 282 { 283 struct fm10k_intfc *interface = pci_get_drvdata(pdev); 284 struct fm10k_iov_data *iov_data = interface->iov_data; 285 struct fm10k_hw *hw = &interface->hw; 286 int num_vfs, i; 287 288 /* pull out num_vfs from iov_data */ 289 num_vfs = iov_data ? iov_data->num_vfs : 0; 290 291 /* shut down queue mapping for VFs */ 292 fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_vf_rss), 293 FM10K_DGLORTMAP_NONE); 294 295 /* Stop any active VFs and reset their resources */ 296 for (i = 0; i < num_vfs; i++) { 297 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i]; 298 299 hw->iov.ops.reset_resources(hw, vf_info); 300 hw->iov.ops.reset_lport(hw, vf_info); 301 fm10k_clear_macvlan_queue(interface, vf_info->glort, false); 302 } 303 } 304 305 int fm10k_iov_resume(struct pci_dev *pdev) 306 { 307 struct fm10k_intfc *interface = pci_get_drvdata(pdev); 308 struct fm10k_iov_data *iov_data = interface->iov_data; 309 struct fm10k_dglort_cfg dglort = { 0 }; 310 struct fm10k_hw *hw = &interface->hw; 311 int num_vfs, i; 312 313 /* pull out num_vfs from iov_data */ 314 num_vfs = iov_data ? iov_data->num_vfs : 0; 315 316 /* return error if iov_data is not already populated */ 317 if (!iov_data) 318 return -ENOMEM; 319 320 /* allocate hardware resources for the VFs */ 321 hw->iov.ops.assign_resources(hw, num_vfs, num_vfs); 322 323 /* configure DGLORT mapping for RSS */ 324 dglort.glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE; 325 dglort.idx = fm10k_dglort_vf_rss; 326 dglort.inner_rss = 1; 327 dglort.rss_l = fls(fm10k_queues_per_pool(hw) - 1); 328 dglort.queue_b = fm10k_vf_queue_index(hw, 0); 329 dglort.vsi_l = fls(hw->iov.total_vfs - 1); 330 dglort.vsi_b = 1; 331 332 hw->mac.ops.configure_dglort_map(hw, &dglort); 333 334 /* assign resources to the device */ 335 for (i = 0; i < num_vfs; i++) { 336 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i]; 337 338 /* allocate all but the last GLORT to the VFs */ 339 if (i == (~hw->mac.dglort_map >> FM10K_DGLORTMAP_MASK_SHIFT)) 340 break; 341 342 /* assign GLORT to VF, and restrict it to multicast */ 343 hw->iov.ops.set_lport(hw, vf_info, i, 344 FM10K_VF_FLAG_MULTI_CAPABLE); 345 346 /* mailbox is disconnected so we don't send a message */ 347 hw->iov.ops.assign_default_mac_vlan(hw, vf_info); 348 349 /* now we are ready so we can connect */ 350 vf_info->mbx.ops.connect(hw, &vf_info->mbx); 351 } 352 353 return 0; 354 } 355 356 s32 fm10k_iov_update_pvid(struct fm10k_intfc *interface, u16 glort, u16 pvid) 357 { 358 struct fm10k_iov_data *iov_data = interface->iov_data; 359 struct fm10k_hw *hw = &interface->hw; 360 struct fm10k_vf_info *vf_info; 361 u16 vf_idx = (glort - hw->mac.dglort_map) & FM10K_DGLORTMAP_NONE; 362 363 /* no IOV support, not our message to process */ 364 if (!iov_data) 365 return FM10K_ERR_PARAM; 366 367 /* glort outside our range, not our message to process */ 368 if (vf_idx >= iov_data->num_vfs) 369 return FM10K_ERR_PARAM; 370 371 /* determine if an update has occurred and if so notify the VF */ 372 vf_info = &iov_data->vf_info[vf_idx]; 373 if (vf_info->sw_vid != pvid) { 374 vf_info->sw_vid = pvid; 375 hw->iov.ops.assign_default_mac_vlan(hw, vf_info); 376 } 377 378 return 0; 379 } 380 381 static void fm10k_iov_free_data(struct pci_dev *pdev) 382 { 383 struct fm10k_intfc *interface = pci_get_drvdata(pdev); 384 385 if (!interface->iov_data) 386 return; 387 388 /* reclaim hardware resources */ 389 fm10k_iov_suspend(pdev); 390 391 /* drop iov_data from interface */ 392 kfree_rcu(interface->iov_data, rcu); 393 interface->iov_data = NULL; 394 } 395 396 static s32 fm10k_iov_alloc_data(struct pci_dev *pdev, int num_vfs) 397 { 398 struct fm10k_intfc *interface = pci_get_drvdata(pdev); 399 struct fm10k_iov_data *iov_data = interface->iov_data; 400 struct fm10k_hw *hw = &interface->hw; 401 size_t size; 402 int i, err; 403 404 /* return error if iov_data is already populated */ 405 if (iov_data) 406 return -EBUSY; 407 408 /* The PF should always be able to assign resources */ 409 if (!hw->iov.ops.assign_resources) 410 return -ENODEV; 411 412 /* nothing to do if no VFs are requested */ 413 if (!num_vfs) 414 return 0; 415 416 /* allocate memory for VF storage */ 417 size = offsetof(struct fm10k_iov_data, vf_info[num_vfs]); 418 iov_data = kzalloc(size, GFP_KERNEL); 419 if (!iov_data) 420 return -ENOMEM; 421 422 /* record number of VFs */ 423 iov_data->num_vfs = num_vfs; 424 425 /* loop through vf_info structures initializing each entry */ 426 for (i = 0; i < num_vfs; i++) { 427 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i]; 428 429 /* Record VF VSI value */ 430 vf_info->vsi = i + 1; 431 vf_info->vf_idx = i; 432 433 /* initialize mailbox memory */ 434 err = fm10k_pfvf_mbx_init(hw, &vf_info->mbx, iov_mbx_data, i); 435 if (err) { 436 dev_err(&pdev->dev, 437 "Unable to initialize SR-IOV mailbox\n"); 438 kfree(iov_data); 439 return err; 440 } 441 } 442 443 /* assign iov_data to interface */ 444 interface->iov_data = iov_data; 445 446 /* allocate hardware resources for the VFs */ 447 fm10k_iov_resume(pdev); 448 449 return 0; 450 } 451 452 void fm10k_iov_disable(struct pci_dev *pdev) 453 { 454 if (pci_num_vf(pdev) && pci_vfs_assigned(pdev)) 455 dev_err(&pdev->dev, 456 "Cannot disable SR-IOV while VFs are assigned\n"); 457 else 458 pci_disable_sriov(pdev); 459 460 fm10k_iov_free_data(pdev); 461 } 462 463 static void fm10k_disable_aer_comp_abort(struct pci_dev *pdev) 464 { 465 u32 err_sev; 466 int pos; 467 468 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR); 469 if (!pos) 470 return; 471 472 pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &err_sev); 473 err_sev &= ~PCI_ERR_UNC_COMP_ABORT; 474 pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, err_sev); 475 } 476 477 int fm10k_iov_configure(struct pci_dev *pdev, int num_vfs) 478 { 479 int current_vfs = pci_num_vf(pdev); 480 int err = 0; 481 482 if (current_vfs && pci_vfs_assigned(pdev)) { 483 dev_err(&pdev->dev, 484 "Cannot modify SR-IOV while VFs are assigned\n"); 485 num_vfs = current_vfs; 486 } else { 487 pci_disable_sriov(pdev); 488 fm10k_iov_free_data(pdev); 489 } 490 491 /* allocate resources for the VFs */ 492 err = fm10k_iov_alloc_data(pdev, num_vfs); 493 if (err) 494 return err; 495 496 /* allocate VFs if not already allocated */ 497 if (num_vfs && num_vfs != current_vfs) { 498 /* Disable completer abort error reporting as 499 * the VFs can trigger this any time they read a queue 500 * that they don't own. 501 */ 502 fm10k_disable_aer_comp_abort(pdev); 503 504 err = pci_enable_sriov(pdev, num_vfs); 505 if (err) { 506 dev_err(&pdev->dev, 507 "Enable PCI SR-IOV failed: %d\n", err); 508 return err; 509 } 510 } 511 512 return num_vfs; 513 } 514 515 static inline void fm10k_reset_vf_info(struct fm10k_intfc *interface, 516 struct fm10k_vf_info *vf_info) 517 { 518 struct fm10k_hw *hw = &interface->hw; 519 520 /* assigning the MAC address will send a mailbox message */ 521 fm10k_mbx_lock(interface); 522 523 /* disable LPORT for this VF which clears switch rules */ 524 hw->iov.ops.reset_lport(hw, vf_info); 525 526 fm10k_clear_macvlan_queue(interface, vf_info->glort, false); 527 528 /* assign new MAC+VLAN for this VF */ 529 hw->iov.ops.assign_default_mac_vlan(hw, vf_info); 530 531 /* re-enable the LPORT for this VF */ 532 hw->iov.ops.set_lport(hw, vf_info, vf_info->vf_idx, 533 FM10K_VF_FLAG_MULTI_CAPABLE); 534 535 fm10k_mbx_unlock(interface); 536 } 537 538 int fm10k_ndo_set_vf_mac(struct net_device *netdev, int vf_idx, u8 *mac) 539 { 540 struct fm10k_intfc *interface = netdev_priv(netdev); 541 struct fm10k_iov_data *iov_data = interface->iov_data; 542 struct fm10k_vf_info *vf_info; 543 544 /* verify SR-IOV is active and that vf idx is valid */ 545 if (!iov_data || vf_idx >= iov_data->num_vfs) 546 return -EINVAL; 547 548 /* verify MAC addr is valid */ 549 if (!is_zero_ether_addr(mac) && !is_valid_ether_addr(mac)) 550 return -EINVAL; 551 552 /* record new MAC address */ 553 vf_info = &iov_data->vf_info[vf_idx]; 554 ether_addr_copy(vf_info->mac, mac); 555 556 fm10k_reset_vf_info(interface, vf_info); 557 558 return 0; 559 } 560 561 int fm10k_ndo_set_vf_vlan(struct net_device *netdev, int vf_idx, u16 vid, 562 u8 qos, __be16 vlan_proto) 563 { 564 struct fm10k_intfc *interface = netdev_priv(netdev); 565 struct fm10k_iov_data *iov_data = interface->iov_data; 566 struct fm10k_hw *hw = &interface->hw; 567 struct fm10k_vf_info *vf_info; 568 569 /* verify SR-IOV is active and that vf idx is valid */ 570 if (!iov_data || vf_idx >= iov_data->num_vfs) 571 return -EINVAL; 572 573 /* QOS is unsupported and VLAN IDs accepted range 0-4094 */ 574 if (qos || (vid > (VLAN_VID_MASK - 1))) 575 return -EINVAL; 576 577 /* VF VLAN Protocol part to default is unsupported */ 578 if (vlan_proto != htons(ETH_P_8021Q)) 579 return -EPROTONOSUPPORT; 580 581 vf_info = &iov_data->vf_info[vf_idx]; 582 583 /* exit if there is nothing to do */ 584 if (vf_info->pf_vid == vid) 585 return 0; 586 587 /* record default VLAN ID for VF */ 588 vf_info->pf_vid = vid; 589 590 /* Clear the VLAN table for the VF */ 591 hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, vf_info->vsi, false); 592 593 fm10k_reset_vf_info(interface, vf_info); 594 595 return 0; 596 } 597 598 int fm10k_ndo_set_vf_bw(struct net_device *netdev, int vf_idx, 599 int __always_unused min_rate, int max_rate) 600 { 601 struct fm10k_intfc *interface = netdev_priv(netdev); 602 struct fm10k_iov_data *iov_data = interface->iov_data; 603 struct fm10k_hw *hw = &interface->hw; 604 605 /* verify SR-IOV is active and that vf idx is valid */ 606 if (!iov_data || vf_idx >= iov_data->num_vfs) 607 return -EINVAL; 608 609 /* rate limit cannot be less than 10Mbs or greater than link speed */ 610 if (max_rate && 611 (max_rate < FM10K_VF_TC_MIN || max_rate > FM10K_VF_TC_MAX)) 612 return -EINVAL; 613 614 /* store values */ 615 iov_data->vf_info[vf_idx].rate = max_rate; 616 617 /* update hardware configuration */ 618 hw->iov.ops.configure_tc(hw, vf_idx, max_rate); 619 620 return 0; 621 } 622 623 int fm10k_ndo_get_vf_config(struct net_device *netdev, 624 int vf_idx, struct ifla_vf_info *ivi) 625 { 626 struct fm10k_intfc *interface = netdev_priv(netdev); 627 struct fm10k_iov_data *iov_data = interface->iov_data; 628 struct fm10k_vf_info *vf_info; 629 630 /* verify SR-IOV is active and that vf idx is valid */ 631 if (!iov_data || vf_idx >= iov_data->num_vfs) 632 return -EINVAL; 633 634 vf_info = &iov_data->vf_info[vf_idx]; 635 636 ivi->vf = vf_idx; 637 ivi->max_tx_rate = vf_info->rate; 638 ivi->min_tx_rate = 0; 639 ether_addr_copy(ivi->mac, vf_info->mac); 640 ivi->vlan = vf_info->pf_vid; 641 ivi->qos = 0; 642 643 return 0; 644 } 645