1 /* Intel(R) Ethernet Switch Host Interface Driver
2 * Copyright(c) 2013 - 2016 Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
16 * Contact Information:
17 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 static s32 fm10k_iov_msg_error(struct fm10k_hw *hw, u32 **results,
26 struct fm10k_mbx_info *mbx)
28 struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
29 struct fm10k_intfc *interface = hw->back;
30 struct pci_dev *pdev = interface->pdev;
32 dev_err(&pdev->dev, "Unknown message ID %u on VF %d\n",
33 **results & FM10K_TLV_ID_MASK, vf_info->vf_idx);
35 return fm10k_tlv_msg_error(hw, results, mbx);
38 static const struct fm10k_msg_data iov_mbx_data[] = {
39 FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
40 FM10K_VF_MSG_MSIX_HANDLER(fm10k_iov_msg_msix_pf),
41 FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_iov_msg_mac_vlan_pf),
42 FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_iov_msg_lport_state_pf),
43 FM10K_TLV_MSG_ERROR_HANDLER(fm10k_iov_msg_error),
46 s32 fm10k_iov_event(struct fm10k_intfc *interface)
48 struct fm10k_hw *hw = &interface->hw;
49 struct fm10k_iov_data *iov_data;
53 /* if there is no iov_data then there is no mailbox to process */
54 if (!READ_ONCE(interface->iov_data))
59 iov_data = interface->iov_data;
61 /* check again now that we are in the RCU block */
65 if (!(fm10k_read_reg(hw, FM10K_EICR) & FM10K_EICR_VFLR))
68 /* read VFLRE to determine if any VFs have been reset */
70 vflre = fm10k_read_reg(hw, FM10K_PFVFLRE(0));
72 vflre |= fm10k_read_reg(hw, FM10K_PFVFLRE(1));
73 vflre = (vflre << 32) | (vflre >> 32);
74 vflre |= fm10k_read_reg(hw, FM10K_PFVFLRE(0));
76 i = iov_data->num_vfs;
78 for (vflre <<= 64 - i; vflre && i--; vflre += vflre) {
79 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
84 hw->iov.ops.reset_resources(hw, vf_info);
85 vf_info->mbx.ops.connect(hw, &vf_info->mbx);
87 } while (i != iov_data->num_vfs);
95 s32 fm10k_iov_mbx(struct fm10k_intfc *interface)
97 struct fm10k_hw *hw = &interface->hw;
98 struct fm10k_iov_data *iov_data;
101 /* if there is no iov_data then there is no mailbox to process */
102 if (!READ_ONCE(interface->iov_data))
107 iov_data = interface->iov_data;
109 /* check again now that we are in the RCU block */
113 /* lock the mailbox for transmit and receive */
114 fm10k_mbx_lock(interface);
116 /* Most VF messages sent to the PF cause the PF to respond by
117 * requesting from the SM mailbox. This means that too many VF
118 * messages processed at once could cause a mailbox timeout on the PF.
119 * To prevent this, store a pointer to the next VF mbx to process. Use
120 * that as the start of the loop so that we don't starve whichever VF
121 * got ignored on the previous run.
124 for (i = iov_data->next_vf_mbx ? : iov_data->num_vfs; i--;) {
125 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
126 struct fm10k_mbx_info *mbx = &vf_info->mbx;
127 u16 glort = vf_info->glort;
129 /* process the SM mailbox first to drain outgoing messages */
130 hw->mbx.ops.process(hw, &hw->mbx);
132 /* verify port mapping is valid, if not reset port */
133 if (vf_info->vf_flags && !fm10k_glort_valid_pf(hw, glort))
134 hw->iov.ops.reset_lport(hw, vf_info);
136 /* reset VFs that have mailbox timed out */
138 hw->iov.ops.reset_resources(hw, vf_info);
139 mbx->ops.connect(hw, mbx);
142 /* guarantee we have free space in the SM mailbox */
143 if (!hw->mbx.ops.tx_ready(&hw->mbx, FM10K_VFMBX_MSG_MTU)) {
144 /* keep track of how many times this occurs */
145 interface->hw_sm_mbx_full++;
149 /* cleanup mailbox and process received messages */
150 mbx->ops.process(hw, mbx);
153 /* if we stopped processing mailboxes early, update next_vf_mbx.
154 * Otherwise, reset next_vf_mbx, and restart loop so that we process
155 * the remaining mailboxes we skipped at the start.
158 iov_data->next_vf_mbx = i + 1;
159 } else if (iov_data->next_vf_mbx) {
160 iov_data->next_vf_mbx = 0;
165 fm10k_mbx_unlock(interface);
173 void fm10k_iov_suspend(struct pci_dev *pdev)
175 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
176 struct fm10k_iov_data *iov_data = interface->iov_data;
177 struct fm10k_hw *hw = &interface->hw;
180 /* pull out num_vfs from iov_data */
181 num_vfs = iov_data ? iov_data->num_vfs : 0;
183 /* shut down queue mapping for VFs */
184 fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_vf_rss),
185 FM10K_DGLORTMAP_NONE);
187 /* Stop any active VFs and reset their resources */
188 for (i = 0; i < num_vfs; i++) {
189 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
191 hw->iov.ops.reset_resources(hw, vf_info);
192 hw->iov.ops.reset_lport(hw, vf_info);
196 int fm10k_iov_resume(struct pci_dev *pdev)
198 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
199 struct fm10k_iov_data *iov_data = interface->iov_data;
200 struct fm10k_dglort_cfg dglort = { 0 };
201 struct fm10k_hw *hw = &interface->hw;
204 /* pull out num_vfs from iov_data */
205 num_vfs = iov_data ? iov_data->num_vfs : 0;
207 /* return error if iov_data is not already populated */
211 /* allocate hardware resources for the VFs */
212 hw->iov.ops.assign_resources(hw, num_vfs, num_vfs);
214 /* configure DGLORT mapping for RSS */
215 dglort.glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
216 dglort.idx = fm10k_dglort_vf_rss;
217 dglort.inner_rss = 1;
218 dglort.rss_l = fls(fm10k_queues_per_pool(hw) - 1);
219 dglort.queue_b = fm10k_vf_queue_index(hw, 0);
220 dglort.vsi_l = fls(hw->iov.total_vfs - 1);
223 hw->mac.ops.configure_dglort_map(hw, &dglort);
225 /* assign resources to the device */
226 for (i = 0; i < num_vfs; i++) {
227 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
229 /* allocate all but the last GLORT to the VFs */
230 if (i == ((~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT))
233 /* assign GLORT to VF, and restrict it to multicast */
234 hw->iov.ops.set_lport(hw, vf_info, i,
235 FM10K_VF_FLAG_MULTI_CAPABLE);
237 /* mailbox is disconnected so we don't send a message */
238 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
240 /* now we are ready so we can connect */
241 vf_info->mbx.ops.connect(hw, &vf_info->mbx);
247 s32 fm10k_iov_update_pvid(struct fm10k_intfc *interface, u16 glort, u16 pvid)
249 struct fm10k_iov_data *iov_data = interface->iov_data;
250 struct fm10k_hw *hw = &interface->hw;
251 struct fm10k_vf_info *vf_info;
252 u16 vf_idx = (glort - hw->mac.dglort_map) & FM10K_DGLORTMAP_NONE;
254 /* no IOV support, not our message to process */
256 return FM10K_ERR_PARAM;
258 /* glort outside our range, not our message to process */
259 if (vf_idx >= iov_data->num_vfs)
260 return FM10K_ERR_PARAM;
262 /* determine if an update has occurred and if so notify the VF */
263 vf_info = &iov_data->vf_info[vf_idx];
264 if (vf_info->sw_vid != pvid) {
265 vf_info->sw_vid = pvid;
266 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
272 static void fm10k_iov_free_data(struct pci_dev *pdev)
274 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
276 if (!interface->iov_data)
279 /* reclaim hardware resources */
280 fm10k_iov_suspend(pdev);
282 /* drop iov_data from interface */
283 kfree_rcu(interface->iov_data, rcu);
284 interface->iov_data = NULL;
287 static s32 fm10k_iov_alloc_data(struct pci_dev *pdev, int num_vfs)
289 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
290 struct fm10k_iov_data *iov_data = interface->iov_data;
291 struct fm10k_hw *hw = &interface->hw;
295 /* return error if iov_data is already populated */
299 /* The PF should always be able to assign resources */
300 if (!hw->iov.ops.assign_resources)
303 /* nothing to do if no VFs are requested */
307 /* allocate memory for VF storage */
308 size = offsetof(struct fm10k_iov_data, vf_info[num_vfs]);
309 iov_data = kzalloc(size, GFP_KERNEL);
313 /* record number of VFs */
314 iov_data->num_vfs = num_vfs;
316 /* loop through vf_info structures initializing each entry */
317 for (i = 0; i < num_vfs; i++) {
318 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
320 /* Record VF VSI value */
321 vf_info->vsi = i + 1;
324 /* initialize mailbox memory */
325 err = fm10k_pfvf_mbx_init(hw, &vf_info->mbx, iov_mbx_data, i);
328 "Unable to initialize SR-IOV mailbox\n");
334 /* assign iov_data to interface */
335 interface->iov_data = iov_data;
337 /* allocate hardware resources for the VFs */
338 fm10k_iov_resume(pdev);
343 void fm10k_iov_disable(struct pci_dev *pdev)
345 if (pci_num_vf(pdev) && pci_vfs_assigned(pdev))
347 "Cannot disable SR-IOV while VFs are assigned\n");
349 pci_disable_sriov(pdev);
351 fm10k_iov_free_data(pdev);
354 static void fm10k_disable_aer_comp_abort(struct pci_dev *pdev)
359 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
363 pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &err_sev);
364 err_sev &= ~PCI_ERR_UNC_COMP_ABORT;
365 pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, err_sev);
368 int fm10k_iov_configure(struct pci_dev *pdev, int num_vfs)
370 int current_vfs = pci_num_vf(pdev);
373 if (current_vfs && pci_vfs_assigned(pdev)) {
375 "Cannot modify SR-IOV while VFs are assigned\n");
376 num_vfs = current_vfs;
378 pci_disable_sriov(pdev);
379 fm10k_iov_free_data(pdev);
382 /* allocate resources for the VFs */
383 err = fm10k_iov_alloc_data(pdev, num_vfs);
387 /* allocate VFs if not already allocated */
388 if (num_vfs && (num_vfs != current_vfs)) {
389 /* Disable completer abort error reporting as
390 * the VFs can trigger this any time they read a queue
391 * that they don't own.
393 fm10k_disable_aer_comp_abort(pdev);
395 err = pci_enable_sriov(pdev, num_vfs);
398 "Enable PCI SR-IOV failed: %d\n", err);
406 static inline void fm10k_reset_vf_info(struct fm10k_intfc *interface,
407 struct fm10k_vf_info *vf_info)
409 struct fm10k_hw *hw = &interface->hw;
411 /* assigning the MAC address will send a mailbox message */
412 fm10k_mbx_lock(interface);
414 /* disable LPORT for this VF which clears switch rules */
415 hw->iov.ops.reset_lport(hw, vf_info);
417 /* assign new MAC+VLAN for this VF */
418 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
420 /* re-enable the LPORT for this VF */
421 hw->iov.ops.set_lport(hw, vf_info, vf_info->vf_idx,
422 FM10K_VF_FLAG_MULTI_CAPABLE);
424 fm10k_mbx_unlock(interface);
427 int fm10k_ndo_set_vf_mac(struct net_device *netdev, int vf_idx, u8 *mac)
429 struct fm10k_intfc *interface = netdev_priv(netdev);
430 struct fm10k_iov_data *iov_data = interface->iov_data;
431 struct fm10k_vf_info *vf_info;
433 /* verify SR-IOV is active and that vf idx is valid */
434 if (!iov_data || vf_idx >= iov_data->num_vfs)
437 /* verify MAC addr is valid */
438 if (!is_zero_ether_addr(mac) && !is_valid_ether_addr(mac))
441 /* record new MAC address */
442 vf_info = &iov_data->vf_info[vf_idx];
443 ether_addr_copy(vf_info->mac, mac);
445 fm10k_reset_vf_info(interface, vf_info);
450 int fm10k_ndo_set_vf_vlan(struct net_device *netdev, int vf_idx, u16 vid,
451 u8 qos, __be16 vlan_proto)
453 struct fm10k_intfc *interface = netdev_priv(netdev);
454 struct fm10k_iov_data *iov_data = interface->iov_data;
455 struct fm10k_hw *hw = &interface->hw;
456 struct fm10k_vf_info *vf_info;
458 /* verify SR-IOV is active and that vf idx is valid */
459 if (!iov_data || vf_idx >= iov_data->num_vfs)
462 /* QOS is unsupported and VLAN IDs accepted range 0-4094 */
463 if (qos || (vid > (VLAN_VID_MASK - 1)))
466 /* VF VLAN Protocol part to default is unsupported */
467 if (vlan_proto != htons(ETH_P_8021Q))
468 return -EPROTONOSUPPORT;
470 vf_info = &iov_data->vf_info[vf_idx];
472 /* exit if there is nothing to do */
473 if (vf_info->pf_vid == vid)
476 /* record default VLAN ID for VF */
477 vf_info->pf_vid = vid;
479 /* Clear the VLAN table for the VF */
480 hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, vf_info->vsi, false);
482 fm10k_reset_vf_info(interface, vf_info);
487 int fm10k_ndo_set_vf_bw(struct net_device *netdev, int vf_idx,
488 int __always_unused min_rate, int max_rate)
490 struct fm10k_intfc *interface = netdev_priv(netdev);
491 struct fm10k_iov_data *iov_data = interface->iov_data;
492 struct fm10k_hw *hw = &interface->hw;
494 /* verify SR-IOV is active and that vf idx is valid */
495 if (!iov_data || vf_idx >= iov_data->num_vfs)
498 /* rate limit cannot be less than 10Mbs or greater than link speed */
500 (max_rate < FM10K_VF_TC_MIN || max_rate > FM10K_VF_TC_MAX))
504 iov_data->vf_info[vf_idx].rate = max_rate;
506 /* update hardware configuration */
507 hw->iov.ops.configure_tc(hw, vf_idx, max_rate);
512 int fm10k_ndo_get_vf_config(struct net_device *netdev,
513 int vf_idx, struct ifla_vf_info *ivi)
515 struct fm10k_intfc *interface = netdev_priv(netdev);
516 struct fm10k_iov_data *iov_data = interface->iov_data;
517 struct fm10k_vf_info *vf_info;
519 /* verify SR-IOV is active and that vf idx is valid */
520 if (!iov_data || vf_idx >= iov_data->num_vfs)
523 vf_info = &iov_data->vf_info[vf_idx];
526 ivi->max_tx_rate = vf_info->rate;
527 ivi->min_tx_rate = 0;
528 ether_addr_copy(ivi->mac, vf_info->mac);
529 ivi->vlan = vf_info->pf_vid;