1 /* Intel Ethernet Switch Host Interface Driver
2 * Copyright(c) 2013 - 2014 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 mailboxes to process */
54 if (!ACCESS_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);
90 /* read MBICR to determine which VFs require attention */
91 mbicr = fm10k_read_reg(hw, FM10K_MBICR(1));
93 mbicr |= fm10k_read_reg(hw, FM10K_MBICR(0));
95 i = iov_data->next_vf_mbx ? : iov_data->num_vfs;
97 for (mbicr <<= 64 - i; i--; mbicr += mbicr) {
98 struct fm10k_mbx_info *mbx = &iov_data->vf_info[i].mbx;
103 if (!hw->mbx.ops.tx_ready(&hw->mbx, FM10K_VFMBX_MSG_MTU))
106 mbx->ops.process(hw, mbx);
110 iov_data->next_vf_mbx = i + 1;
111 } else if (iov_data->next_vf_mbx) {
112 iov_data->next_vf_mbx = 0;
121 s32 fm10k_iov_mbx(struct fm10k_intfc *interface)
123 struct fm10k_hw *hw = &interface->hw;
124 struct fm10k_iov_data *iov_data;
127 /* if there is no iov_data then there is no mailboxes to process */
128 if (!ACCESS_ONCE(interface->iov_data))
133 iov_data = interface->iov_data;
135 /* check again now that we are in the RCU block */
139 /* lock the mailbox for transmit and receive */
140 fm10k_mbx_lock(interface);
143 for (i = iov_data->next_vf_mbx ? : iov_data->num_vfs; i--;) {
144 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
145 struct fm10k_mbx_info *mbx = &vf_info->mbx;
146 u16 glort = vf_info->glort;
148 /* verify port mapping is valid, if not reset port */
149 if (vf_info->vf_flags && !fm10k_glort_valid_pf(hw, glort))
150 hw->iov.ops.reset_lport(hw, vf_info);
152 /* reset VFs that have mailbox timed out */
154 hw->iov.ops.reset_resources(hw, vf_info);
155 mbx->ops.connect(hw, mbx);
158 /* no work pending, then just continue */
159 if (mbx->ops.tx_complete(mbx) && !mbx->ops.rx_ready(mbx))
162 /* guarantee we have free space in the SM mailbox */
163 if (!hw->mbx.ops.tx_ready(&hw->mbx, FM10K_VFMBX_MSG_MTU))
166 /* cleanup mailbox and process received messages */
167 mbx->ops.process(hw, mbx);
171 iov_data->next_vf_mbx = i + 1;
172 } else if (iov_data->next_vf_mbx) {
173 iov_data->next_vf_mbx = 0;
178 fm10k_mbx_unlock(interface);
186 void fm10k_iov_suspend(struct pci_dev *pdev)
188 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
189 struct fm10k_iov_data *iov_data = interface->iov_data;
190 struct fm10k_hw *hw = &interface->hw;
193 /* pull out num_vfs from iov_data */
194 num_vfs = iov_data ? iov_data->num_vfs : 0;
196 /* shut down queue mapping for VFs */
197 fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_vf_rss),
198 FM10K_DGLORTMAP_NONE);
200 /* Stop any active VFs and reset their resources */
201 for (i = 0; i < num_vfs; i++) {
202 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
204 hw->iov.ops.reset_resources(hw, vf_info);
205 hw->iov.ops.reset_lport(hw, vf_info);
209 int fm10k_iov_resume(struct pci_dev *pdev)
211 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
212 struct fm10k_iov_data *iov_data = interface->iov_data;
213 struct fm10k_dglort_cfg dglort = { 0 };
214 struct fm10k_hw *hw = &interface->hw;
217 /* pull out num_vfs from iov_data */
218 num_vfs = iov_data ? iov_data->num_vfs : 0;
220 /* return error if iov_data is not already populated */
224 /* allocate hardware resources for the VFs */
225 hw->iov.ops.assign_resources(hw, num_vfs, num_vfs);
227 /* configure DGLORT mapping for RSS */
228 dglort.glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
229 dglort.idx = fm10k_dglort_vf_rss;
230 dglort.inner_rss = 1;
231 dglort.rss_l = fls(fm10k_queues_per_pool(hw) - 1);
232 dglort.queue_b = fm10k_vf_queue_index(hw, 0);
233 dglort.vsi_l = fls(hw->iov.total_vfs - 1);
236 hw->mac.ops.configure_dglort_map(hw, &dglort);
238 /* assign resources to the device */
239 for (i = 0; i < num_vfs; i++) {
240 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
242 /* allocate all but the last GLORT to the VFs */
243 if (i == ((~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT))
246 /* assign GLORT to VF, and restrict it to multicast */
247 hw->iov.ops.set_lport(hw, vf_info, i,
248 FM10K_VF_FLAG_MULTI_CAPABLE);
250 /* assign our default vid to the VF following reset */
251 vf_info->sw_vid = hw->mac.default_vid;
253 /* mailbox is disconnected so we don't send a message */
254 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
256 /* now we are ready so we can connect */
257 vf_info->mbx.ops.connect(hw, &vf_info->mbx);
263 s32 fm10k_iov_update_pvid(struct fm10k_intfc *interface, u16 glort, u16 pvid)
265 struct fm10k_iov_data *iov_data = interface->iov_data;
266 struct fm10k_hw *hw = &interface->hw;
267 struct fm10k_vf_info *vf_info;
268 u16 vf_idx = (glort - hw->mac.dglort_map) & FM10K_DGLORTMAP_NONE;
270 /* no IOV support, not our message to process */
272 return FM10K_ERR_PARAM;
274 /* glort outside our range, not our message to process */
275 if (vf_idx >= iov_data->num_vfs)
276 return FM10K_ERR_PARAM;
278 /* determine if an update has occured and if so notify the VF */
279 vf_info = &iov_data->vf_info[vf_idx];
280 if (vf_info->sw_vid != pvid) {
281 vf_info->sw_vid = pvid;
282 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
288 static void fm10k_iov_free_data(struct pci_dev *pdev)
290 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
292 if (!interface->iov_data)
295 /* reclaim hardware resources */
296 fm10k_iov_suspend(pdev);
298 /* drop iov_data from interface */
299 kfree_rcu(interface->iov_data, rcu);
300 interface->iov_data = NULL;
303 static s32 fm10k_iov_alloc_data(struct pci_dev *pdev, int num_vfs)
305 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
306 struct fm10k_iov_data *iov_data = interface->iov_data;
307 struct fm10k_hw *hw = &interface->hw;
311 /* return error if iov_data is already populated */
315 /* The PF should always be able to assign resources */
316 if (!hw->iov.ops.assign_resources)
319 /* nothing to do if no VFs are requested */
323 /* allocate memory for VF storage */
324 size = offsetof(struct fm10k_iov_data, vf_info[num_vfs]);
325 iov_data = kzalloc(size, GFP_KERNEL);
329 /* record number of VFs */
330 iov_data->num_vfs = num_vfs;
332 /* loop through vf_info structures initializing each entry */
333 for (i = 0; i < num_vfs; i++) {
334 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
336 /* Record VF VSI value */
337 vf_info->vsi = i + 1;
340 /* initialize mailbox memory */
341 err = fm10k_pfvf_mbx_init(hw, &vf_info->mbx, iov_mbx_data, i);
344 "Unable to initialize SR-IOV mailbox\n");
350 /* assign iov_data to interface */
351 interface->iov_data = iov_data;
353 /* allocate hardware resources for the VFs */
354 fm10k_iov_resume(pdev);
359 void fm10k_iov_disable(struct pci_dev *pdev)
361 if (pci_num_vf(pdev) && pci_vfs_assigned(pdev))
363 "Cannot disable SR-IOV while VFs are assigned\n");
365 pci_disable_sriov(pdev);
367 fm10k_iov_free_data(pdev);
370 static void fm10k_disable_aer_comp_abort(struct pci_dev *pdev)
375 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
379 pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &err_sev);
380 err_sev &= ~PCI_ERR_UNC_COMP_ABORT;
381 pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, err_sev);
384 int fm10k_iov_configure(struct pci_dev *pdev, int num_vfs)
386 int current_vfs = pci_num_vf(pdev);
389 if (current_vfs && pci_vfs_assigned(pdev)) {
391 "Cannot modify SR-IOV while VFs are assigned\n");
392 num_vfs = current_vfs;
394 pci_disable_sriov(pdev);
395 fm10k_iov_free_data(pdev);
398 /* allocate resources for the VFs */
399 err = fm10k_iov_alloc_data(pdev, num_vfs);
403 /* allocate VFs if not already allocated */
404 if (num_vfs && (num_vfs != current_vfs)) {
405 /* Disable completer abort error reporting as
406 * the VFs can trigger this any time they read a queue
407 * that they don't own.
409 fm10k_disable_aer_comp_abort(pdev);
411 err = pci_enable_sriov(pdev, num_vfs);
414 "Enable PCI SR-IOV failed: %d\n", err);
422 int fm10k_ndo_set_vf_mac(struct net_device *netdev, int vf_idx, u8 *mac)
424 struct fm10k_intfc *interface = netdev_priv(netdev);
425 struct fm10k_iov_data *iov_data = interface->iov_data;
426 struct fm10k_hw *hw = &interface->hw;
427 struct fm10k_vf_info *vf_info;
429 /* verify SR-IOV is active and that vf idx is valid */
430 if (!iov_data || vf_idx >= iov_data->num_vfs)
433 /* verify MAC addr is valid */
434 if (!is_zero_ether_addr(mac) && !is_valid_ether_addr(mac))
437 /* record new MAC address */
438 vf_info = &iov_data->vf_info[vf_idx];
439 ether_addr_copy(vf_info->mac, mac);
441 /* assigning the MAC will send a mailbox message so lock is needed */
442 fm10k_mbx_lock(interface);
444 /* assign MAC address to VF */
445 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
447 fm10k_mbx_unlock(interface);
452 int fm10k_ndo_set_vf_vlan(struct net_device *netdev, int vf_idx, u16 vid,
455 struct fm10k_intfc *interface = netdev_priv(netdev);
456 struct fm10k_iov_data *iov_data = interface->iov_data;
457 struct fm10k_hw *hw = &interface->hw;
458 struct fm10k_vf_info *vf_info;
460 /* verify SR-IOV is active and that vf idx is valid */
461 if (!iov_data || vf_idx >= iov_data->num_vfs)
464 /* QOS is unsupported and VLAN IDs accepted range 0-4094 */
465 if (qos || (vid > (VLAN_VID_MASK - 1)))
468 vf_info = &iov_data->vf_info[vf_idx];
470 /* exit if there is nothing to do */
471 if (vf_info->pf_vid == vid)
474 /* record default VLAN ID for VF */
475 vf_info->pf_vid = vid;
477 /* assigning the VLAN will send a mailbox message so lock is needed */
478 fm10k_mbx_lock(interface);
480 /* Clear the VLAN table for the VF */
481 hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, vf_info->vsi, false);
483 /* Update VF assignment and trigger reset */
484 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
486 fm10k_mbx_unlock(interface);
491 int fm10k_ndo_set_vf_bw(struct net_device *netdev, int vf_idx, int unused,
494 struct fm10k_intfc *interface = netdev_priv(netdev);
495 struct fm10k_iov_data *iov_data = interface->iov_data;
496 struct fm10k_hw *hw = &interface->hw;
498 /* verify SR-IOV is active and that vf idx is valid */
499 if (!iov_data || vf_idx >= iov_data->num_vfs)
502 /* rate limit cannot be less than 10Mbs or greater than link speed */
503 if (rate && ((rate < FM10K_VF_TC_MIN) || rate > FM10K_VF_TC_MAX))
507 iov_data->vf_info[vf_idx].rate = rate;
509 /* update hardware configuration */
510 hw->iov.ops.configure_tc(hw, vf_idx, rate);
515 int fm10k_ndo_get_vf_config(struct net_device *netdev,
516 int vf_idx, struct ifla_vf_info *ivi)
518 struct fm10k_intfc *interface = netdev_priv(netdev);
519 struct fm10k_iov_data *iov_data = interface->iov_data;
520 struct fm10k_vf_info *vf_info;
522 /* verify SR-IOV is active and that vf idx is valid */
523 if (!iov_data || vf_idx >= iov_data->num_vfs)
526 vf_info = &iov_data->vf_info[vf_idx];
529 ivi->max_tx_rate = vf_info->rate;
530 ivi->min_tx_rate = 0;
531 ether_addr_copy(ivi->mac, vf_info->mac);
532 ivi->vlan = vf_info->pf_vid;