1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
8 static s32 fm10k_iov_msg_error(struct fm10k_hw *hw, u32 **results,
9 struct fm10k_mbx_info *mbx)
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;
15 dev_err(&pdev->dev, "Unknown message ID %u on VF %d\n",
16 **results & FM10K_TLV_ID_MASK, vf_info->vf_idx);
18 return fm10k_tlv_msg_error(hw, results, mbx);
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
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
33 static s32 fm10k_iov_msg_queue_mac_vlan(struct fm10k_hw *hw, u32 **results,
34 struct fm10k_mbx_info *mbx)
36 struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
37 struct fm10k_intfc *interface = hw->back;
45 /* we shouldn't be updating rules on a disabled interface */
46 if (!FM10K_VF_FLAG_ENABLED(vf_info))
47 err = FM10K_ERR_PARAM;
49 if (!err && !!results[FM10K_MAC_VLAN_MSG_VLAN]) {
50 result = results[FM10K_MAC_VLAN_MSG_VLAN];
52 /* record VLAN id requested */
53 err = fm10k_tlv_attr_get_u32(result, &vid);
57 set = !(vid & FM10K_VLAN_CLEAR);
58 vid &= ~FM10K_VLAN_CLEAR;
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.
71 /* prevent multi-bit requests when PF has
72 * administratively set the VLAN for this VF
75 return FM10K_ERR_PARAM;
77 err = fm10k_iov_select_vid(vf_info, (u16)vid);
84 /* update VSI info for VF in regards to VLAN table */
85 err = hw->mac.ops.update_vlan(hw, vid, vf_info->vsi, set);
88 if (!err && !!results[FM10K_MAC_VLAN_MSG_MAC]) {
89 result = results[FM10K_MAC_VLAN_MSG_MAC];
91 /* record unicast MAC address requested */
92 err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan);
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;
101 set = !(vlan & FM10K_VLAN_CLEAR);
102 vlan &= ~FM10K_VLAN_CLEAR;
104 err = fm10k_iov_select_vid(vf_info, vlan);
110 /* Add this request to the MAC/VLAN queue */
111 err = fm10k_queue_mac_request(interface, vf_info->glort,
115 if (!err && !!results[FM10K_MAC_VLAN_MSG_MULTICAST]) {
116 result = results[FM10K_MAC_VLAN_MSG_MULTICAST];
118 /* record multicast MAC address requested */
119 err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan);
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;
127 set = !(vlan & FM10K_VLAN_CLEAR);
128 vlan &= ~FM10K_VLAN_CLEAR;
130 err = fm10k_iov_select_vid(vf_info, vlan);
136 /* Add this request to the MAC/VLAN queue */
137 err = fm10k_queue_mac_request(interface, vf_info->glort,
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),
152 s32 fm10k_iov_event(struct fm10k_intfc *interface)
154 struct fm10k_hw *hw = &interface->hw;
155 struct fm10k_iov_data *iov_data;
159 /* if there is no iov_data then there is no mailbox to process */
160 if (!READ_ONCE(interface->iov_data))
165 iov_data = interface->iov_data;
167 /* check again now that we are in the RCU block */
171 if (!(fm10k_read_reg(hw, FM10K_EICR) & FM10K_EICR_VFLR))
174 /* read VFLRE to determine if any VFs have been reset */
175 vflre = fm10k_read_reg(hw, FM10K_PFVFLRE(1));
177 vflre |= fm10k_read_reg(hw, FM10K_PFVFLRE(0));
179 i = iov_data->num_vfs;
181 for (vflre <<= 64 - i; vflre && i--; vflre += vflre) {
182 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
187 hw->iov.ops.reset_resources(hw, vf_info);
188 vf_info->mbx.ops.connect(hw, &vf_info->mbx);
197 s32 fm10k_iov_mbx(struct fm10k_intfc *interface)
199 struct fm10k_hw *hw = &interface->hw;
200 struct fm10k_iov_data *iov_data;
203 /* if there is no iov_data then there is no mailbox to process */
204 if (!READ_ONCE(interface->iov_data))
209 iov_data = interface->iov_data;
211 /* check again now that we are in the RCU block */
215 /* lock the mailbox for transmit and receive */
216 fm10k_mbx_lock(interface);
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.
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;
231 /* process the SM mailbox first to drain outgoing messages */
232 hw->mbx.ops.process(hw, &hw->mbx);
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);
240 /* reset VFs that have mailbox timed out */
242 hw->iov.ops.reset_resources(hw, vf_info);
243 mbx->ops.connect(hw, mbx);
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++;
251 /* make sure we try again momentarily */
252 fm10k_service_event_schedule(interface);
257 /* cleanup mailbox and process received messages */
258 mbx->ops.process(hw, mbx);
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.
266 iov_data->next_vf_mbx = i + 1;
267 } else if (iov_data->next_vf_mbx) {
268 iov_data->next_vf_mbx = 0;
273 fm10k_mbx_unlock(interface);
281 void fm10k_iov_suspend(struct pci_dev *pdev)
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;
288 /* pull out num_vfs from iov_data */
289 num_vfs = iov_data ? iov_data->num_vfs : 0;
291 /* shut down queue mapping for VFs */
292 fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_vf_rss),
293 FM10K_DGLORTMAP_NONE);
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];
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);
305 static void fm10k_mask_aer_comp_abort(struct pci_dev *pdev)
310 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
314 /* Mask the completion abort bit in the ERR_UNCOR_MASK register,
315 * preventing the device from reporting these errors to the upstream
316 * PCIe root device. This avoids bringing down platforms which upgrade
317 * non-fatal completer aborts into machine check exceptions. Completer
318 * aborts can occur whenever a VF reads a queue it doesn't own.
320 pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_MASK, &err_mask);
321 err_mask |= PCI_ERR_UNC_COMP_ABORT;
322 pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_MASK, err_mask);
327 int fm10k_iov_resume(struct pci_dev *pdev)
329 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
330 struct fm10k_iov_data *iov_data = interface->iov_data;
331 struct fm10k_dglort_cfg dglort = { 0 };
332 struct fm10k_hw *hw = &interface->hw;
335 /* pull out num_vfs from iov_data */
336 num_vfs = iov_data ? iov_data->num_vfs : 0;
338 /* return error if iov_data is not already populated */
342 /* Lower severity of completer abort error reporting as
343 * the VFs can trigger this any time they read a queue
344 * that they don't own.
346 fm10k_mask_aer_comp_abort(pdev);
348 /* allocate hardware resources for the VFs */
349 hw->iov.ops.assign_resources(hw, num_vfs, num_vfs);
351 /* configure DGLORT mapping for RSS */
352 dglort.glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
353 dglort.idx = fm10k_dglort_vf_rss;
354 dglort.inner_rss = 1;
355 dglort.rss_l = fls(fm10k_queues_per_pool(hw) - 1);
356 dglort.queue_b = fm10k_vf_queue_index(hw, 0);
357 dglort.vsi_l = fls(hw->iov.total_vfs - 1);
360 hw->mac.ops.configure_dglort_map(hw, &dglort);
362 /* assign resources to the device */
363 for (i = 0; i < num_vfs; i++) {
364 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
366 /* allocate all but the last GLORT to the VFs */
367 if (i == (~hw->mac.dglort_map >> FM10K_DGLORTMAP_MASK_SHIFT))
370 /* assign GLORT to VF, and restrict it to multicast */
371 hw->iov.ops.set_lport(hw, vf_info, i,
372 FM10K_VF_FLAG_MULTI_CAPABLE);
374 /* mailbox is disconnected so we don't send a message */
375 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
377 /* now we are ready so we can connect */
378 vf_info->mbx.ops.connect(hw, &vf_info->mbx);
384 s32 fm10k_iov_update_pvid(struct fm10k_intfc *interface, u16 glort, u16 pvid)
386 struct fm10k_iov_data *iov_data = interface->iov_data;
387 struct fm10k_hw *hw = &interface->hw;
388 struct fm10k_vf_info *vf_info;
389 u16 vf_idx = (glort - hw->mac.dglort_map) & FM10K_DGLORTMAP_NONE;
391 /* no IOV support, not our message to process */
393 return FM10K_ERR_PARAM;
395 /* glort outside our range, not our message to process */
396 if (vf_idx >= iov_data->num_vfs)
397 return FM10K_ERR_PARAM;
399 /* determine if an update has occurred and if so notify the VF */
400 vf_info = &iov_data->vf_info[vf_idx];
401 if (vf_info->sw_vid != pvid) {
402 vf_info->sw_vid = pvid;
403 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
409 static void fm10k_iov_free_data(struct pci_dev *pdev)
411 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
413 if (!interface->iov_data)
416 /* reclaim hardware resources */
417 fm10k_iov_suspend(pdev);
419 /* drop iov_data from interface */
420 kfree_rcu(interface->iov_data, rcu);
421 interface->iov_data = NULL;
424 static s32 fm10k_iov_alloc_data(struct pci_dev *pdev, int num_vfs)
426 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
427 struct fm10k_iov_data *iov_data = interface->iov_data;
428 struct fm10k_hw *hw = &interface->hw;
432 /* return error if iov_data is already populated */
436 /* The PF should always be able to assign resources */
437 if (!hw->iov.ops.assign_resources)
440 /* nothing to do if no VFs are requested */
444 /* allocate memory for VF storage */
445 size = offsetof(struct fm10k_iov_data, vf_info[num_vfs]);
446 iov_data = kzalloc(size, GFP_KERNEL);
450 /* record number of VFs */
451 iov_data->num_vfs = num_vfs;
453 /* loop through vf_info structures initializing each entry */
454 for (i = 0; i < num_vfs; i++) {
455 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
457 /* Record VF VSI value */
458 vf_info->vsi = i + 1;
461 /* initialize mailbox memory */
462 err = fm10k_pfvf_mbx_init(hw, &vf_info->mbx, iov_mbx_data, i);
465 "Unable to initialize SR-IOV mailbox\n");
471 /* assign iov_data to interface */
472 interface->iov_data = iov_data;
474 /* allocate hardware resources for the VFs */
475 fm10k_iov_resume(pdev);
480 void fm10k_iov_disable(struct pci_dev *pdev)
482 if (pci_num_vf(pdev) && pci_vfs_assigned(pdev))
484 "Cannot disable SR-IOV while VFs are assigned\n");
486 pci_disable_sriov(pdev);
488 fm10k_iov_free_data(pdev);
491 int fm10k_iov_configure(struct pci_dev *pdev, int num_vfs)
493 int current_vfs = pci_num_vf(pdev);
496 if (current_vfs && pci_vfs_assigned(pdev)) {
498 "Cannot modify SR-IOV while VFs are assigned\n");
499 num_vfs = current_vfs;
501 pci_disable_sriov(pdev);
502 fm10k_iov_free_data(pdev);
505 /* allocate resources for the VFs */
506 err = fm10k_iov_alloc_data(pdev, num_vfs);
510 /* allocate VFs if not already allocated */
511 if (num_vfs && num_vfs != current_vfs) {
512 err = pci_enable_sriov(pdev, num_vfs);
515 "Enable PCI SR-IOV failed: %d\n", err);
523 static inline void fm10k_reset_vf_info(struct fm10k_intfc *interface,
524 struct fm10k_vf_info *vf_info)
526 struct fm10k_hw *hw = &interface->hw;
528 /* assigning the MAC address will send a mailbox message */
529 fm10k_mbx_lock(interface);
531 /* disable LPORT for this VF which clears switch rules */
532 hw->iov.ops.reset_lport(hw, vf_info);
534 fm10k_clear_macvlan_queue(interface, vf_info->glort, false);
536 /* assign new MAC+VLAN for this VF */
537 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
539 /* re-enable the LPORT for this VF */
540 hw->iov.ops.set_lport(hw, vf_info, vf_info->vf_idx,
541 FM10K_VF_FLAG_MULTI_CAPABLE);
543 fm10k_mbx_unlock(interface);
546 int fm10k_ndo_set_vf_mac(struct net_device *netdev, int vf_idx, u8 *mac)
548 struct fm10k_intfc *interface = netdev_priv(netdev);
549 struct fm10k_iov_data *iov_data = interface->iov_data;
550 struct fm10k_vf_info *vf_info;
552 /* verify SR-IOV is active and that vf idx is valid */
553 if (!iov_data || vf_idx >= iov_data->num_vfs)
556 /* verify MAC addr is valid */
557 if (!is_zero_ether_addr(mac) && !is_valid_ether_addr(mac))
560 /* record new MAC address */
561 vf_info = &iov_data->vf_info[vf_idx];
562 ether_addr_copy(vf_info->mac, mac);
564 fm10k_reset_vf_info(interface, vf_info);
569 int fm10k_ndo_set_vf_vlan(struct net_device *netdev, int vf_idx, u16 vid,
570 u8 qos, __be16 vlan_proto)
572 struct fm10k_intfc *interface = netdev_priv(netdev);
573 struct fm10k_iov_data *iov_data = interface->iov_data;
574 struct fm10k_hw *hw = &interface->hw;
575 struct fm10k_vf_info *vf_info;
577 /* verify SR-IOV is active and that vf idx is valid */
578 if (!iov_data || vf_idx >= iov_data->num_vfs)
581 /* QOS is unsupported and VLAN IDs accepted range 0-4094 */
582 if (qos || (vid > (VLAN_VID_MASK - 1)))
585 /* VF VLAN Protocol part to default is unsupported */
586 if (vlan_proto != htons(ETH_P_8021Q))
587 return -EPROTONOSUPPORT;
589 vf_info = &iov_data->vf_info[vf_idx];
591 /* exit if there is nothing to do */
592 if (vf_info->pf_vid == vid)
595 /* record default VLAN ID for VF */
596 vf_info->pf_vid = vid;
598 /* Clear the VLAN table for the VF */
599 hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, vf_info->vsi, false);
601 fm10k_reset_vf_info(interface, vf_info);
606 int fm10k_ndo_set_vf_bw(struct net_device *netdev, int vf_idx,
607 int __always_unused min_rate, int max_rate)
609 struct fm10k_intfc *interface = netdev_priv(netdev);
610 struct fm10k_iov_data *iov_data = interface->iov_data;
611 struct fm10k_hw *hw = &interface->hw;
613 /* verify SR-IOV is active and that vf idx is valid */
614 if (!iov_data || vf_idx >= iov_data->num_vfs)
617 /* rate limit cannot be less than 10Mbs or greater than link speed */
619 (max_rate < FM10K_VF_TC_MIN || max_rate > FM10K_VF_TC_MAX))
623 iov_data->vf_info[vf_idx].rate = max_rate;
625 /* update hardware configuration */
626 hw->iov.ops.configure_tc(hw, vf_idx, max_rate);
631 int fm10k_ndo_get_vf_config(struct net_device *netdev,
632 int vf_idx, struct ifla_vf_info *ivi)
634 struct fm10k_intfc *interface = netdev_priv(netdev);
635 struct fm10k_iov_data *iov_data = interface->iov_data;
636 struct fm10k_vf_info *vf_info;
638 /* verify SR-IOV is active and that vf idx is valid */
639 if (!iov_data || vf_idx >= iov_data->num_vfs)
642 vf_info = &iov_data->vf_info[vf_idx];
645 ivi->max_tx_rate = vf_info->rate;
646 ivi->min_tx_rate = 0;
647 ether_addr_copy(ivi->mac, vf_info->mac);
648 ivi->vlan = vf_info->pf_vid;