fm10k: force LPORT delete when updating VLAN or MAC address
[linux/fpc-iii.git] / drivers / net / ethernet / intel / fm10k / fm10k_iov.c
blob94571e6e790c55f9d204535fd4cc364857551e75
1 /* Intel Ethernet Switch Host Interface Driver
2 * Copyright(c) 2013 - 2015 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
11 * more details.
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
21 #include "fm10k.h"
22 #include "fm10k_vf.h"
23 #include "fm10k_pf.h"
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;
50 s64 vflre;
51 int i;
53 /* if there is no iov_data then there is no mailboxes to process */
54 if (!ACCESS_ONCE(interface->iov_data))
55 return 0;
57 rcu_read_lock();
59 iov_data = interface->iov_data;
61 /* check again now that we are in the RCU block */
62 if (!iov_data)
63 goto read_unlock;
65 if (!(fm10k_read_reg(hw, FM10K_EICR) & FM10K_EICR_VFLR))
66 goto read_unlock;
68 /* read VFLRE to determine if any VFs have been reset */
69 do {
70 vflre = fm10k_read_reg(hw, FM10K_PFVFLRE(0));
71 vflre <<= 32;
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];
81 if (vflre >= 0)
82 continue;
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);
89 read_unlock:
90 rcu_read_unlock();
92 return 0;
95 s32 fm10k_iov_mbx(struct fm10k_intfc *interface)
97 struct fm10k_hw *hw = &interface->hw;
98 struct fm10k_iov_data *iov_data;
99 int i;
101 /* if there is no iov_data then there is no mailboxes to process */
102 if (!ACCESS_ONCE(interface->iov_data))
103 return 0;
105 rcu_read_lock();
107 iov_data = interface->iov_data;
109 /* check again now that we are in the RCU block */
110 if (!iov_data)
111 goto read_unlock;
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.
123 process_mbx:
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 /* verify port mapping is valid, if not reset port */
130 if (vf_info->vf_flags && !fm10k_glort_valid_pf(hw, glort))
131 hw->iov.ops.reset_lport(hw, vf_info);
133 /* reset VFs that have mailbox timed out */
134 if (!mbx->timeout) {
135 hw->iov.ops.reset_resources(hw, vf_info);
136 mbx->ops.connect(hw, mbx);
139 /* guarantee we have free space in the SM mailbox */
140 if (!hw->mbx.ops.tx_ready(&hw->mbx, FM10K_VFMBX_MSG_MTU))
141 break;
143 /* cleanup mailbox and process received messages */
144 mbx->ops.process(hw, mbx);
147 /* if we stopped processing mailboxes early, update next_vf_mbx.
148 * Otherwise, reset next_vf_mbx, and restart loop so that we process
149 * the remaining mailboxes we skipped at the start.
151 if (i >= 0) {
152 iov_data->next_vf_mbx = i + 1;
153 } else if (iov_data->next_vf_mbx) {
154 iov_data->next_vf_mbx = 0;
155 goto process_mbx;
158 /* free the lock */
159 fm10k_mbx_unlock(interface);
161 read_unlock:
162 rcu_read_unlock();
164 return 0;
167 void fm10k_iov_suspend(struct pci_dev *pdev)
169 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
170 struct fm10k_iov_data *iov_data = interface->iov_data;
171 struct fm10k_hw *hw = &interface->hw;
172 int num_vfs, i;
174 /* pull out num_vfs from iov_data */
175 num_vfs = iov_data ? iov_data->num_vfs : 0;
177 /* shut down queue mapping for VFs */
178 fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_vf_rss),
179 FM10K_DGLORTMAP_NONE);
181 /* Stop any active VFs and reset their resources */
182 for (i = 0; i < num_vfs; i++) {
183 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
185 hw->iov.ops.reset_resources(hw, vf_info);
186 hw->iov.ops.reset_lport(hw, vf_info);
190 int fm10k_iov_resume(struct pci_dev *pdev)
192 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
193 struct fm10k_iov_data *iov_data = interface->iov_data;
194 struct fm10k_dglort_cfg dglort = { 0 };
195 struct fm10k_hw *hw = &interface->hw;
196 int num_vfs, i;
198 /* pull out num_vfs from iov_data */
199 num_vfs = iov_data ? iov_data->num_vfs : 0;
201 /* return error if iov_data is not already populated */
202 if (!iov_data)
203 return -ENOMEM;
205 /* allocate hardware resources for the VFs */
206 hw->iov.ops.assign_resources(hw, num_vfs, num_vfs);
208 /* configure DGLORT mapping for RSS */
209 dglort.glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
210 dglort.idx = fm10k_dglort_vf_rss;
211 dglort.inner_rss = 1;
212 dglort.rss_l = fls(fm10k_queues_per_pool(hw) - 1);
213 dglort.queue_b = fm10k_vf_queue_index(hw, 0);
214 dglort.vsi_l = fls(hw->iov.total_vfs - 1);
215 dglort.vsi_b = 1;
217 hw->mac.ops.configure_dglort_map(hw, &dglort);
219 /* assign resources to the device */
220 for (i = 0; i < num_vfs; i++) {
221 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
223 /* allocate all but the last GLORT to the VFs */
224 if (i == ((~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT))
225 break;
227 /* assign GLORT to VF, and restrict it to multicast */
228 hw->iov.ops.set_lport(hw, vf_info, i,
229 FM10K_VF_FLAG_MULTI_CAPABLE);
231 /* assign our default vid to the VF following reset */
232 vf_info->sw_vid = hw->mac.default_vid;
234 /* mailbox is disconnected so we don't send a message */
235 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
237 /* now we are ready so we can connect */
238 vf_info->mbx.ops.connect(hw, &vf_info->mbx);
241 return 0;
244 s32 fm10k_iov_update_pvid(struct fm10k_intfc *interface, u16 glort, u16 pvid)
246 struct fm10k_iov_data *iov_data = interface->iov_data;
247 struct fm10k_hw *hw = &interface->hw;
248 struct fm10k_vf_info *vf_info;
249 u16 vf_idx = (glort - hw->mac.dglort_map) & FM10K_DGLORTMAP_NONE;
251 /* no IOV support, not our message to process */
252 if (!iov_data)
253 return FM10K_ERR_PARAM;
255 /* glort outside our range, not our message to process */
256 if (vf_idx >= iov_data->num_vfs)
257 return FM10K_ERR_PARAM;
259 /* determine if an update has occurred and if so notify the VF */
260 vf_info = &iov_data->vf_info[vf_idx];
261 if (vf_info->sw_vid != pvid) {
262 vf_info->sw_vid = pvid;
263 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
266 return 0;
269 static void fm10k_iov_free_data(struct pci_dev *pdev)
271 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
273 if (!interface->iov_data)
274 return;
276 /* reclaim hardware resources */
277 fm10k_iov_suspend(pdev);
279 /* drop iov_data from interface */
280 kfree_rcu(interface->iov_data, rcu);
281 interface->iov_data = NULL;
284 static s32 fm10k_iov_alloc_data(struct pci_dev *pdev, int num_vfs)
286 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
287 struct fm10k_iov_data *iov_data = interface->iov_data;
288 struct fm10k_hw *hw = &interface->hw;
289 size_t size;
290 int i, err;
292 /* return error if iov_data is already populated */
293 if (iov_data)
294 return -EBUSY;
296 /* The PF should always be able to assign resources */
297 if (!hw->iov.ops.assign_resources)
298 return -ENODEV;
300 /* nothing to do if no VFs are requested */
301 if (!num_vfs)
302 return 0;
304 /* allocate memory for VF storage */
305 size = offsetof(struct fm10k_iov_data, vf_info[num_vfs]);
306 iov_data = kzalloc(size, GFP_KERNEL);
307 if (!iov_data)
308 return -ENOMEM;
310 /* record number of VFs */
311 iov_data->num_vfs = num_vfs;
313 /* loop through vf_info structures initializing each entry */
314 for (i = 0; i < num_vfs; i++) {
315 struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
317 /* Record VF VSI value */
318 vf_info->vsi = i + 1;
319 vf_info->vf_idx = i;
321 /* initialize mailbox memory */
322 err = fm10k_pfvf_mbx_init(hw, &vf_info->mbx, iov_mbx_data, i);
323 if (err) {
324 dev_err(&pdev->dev,
325 "Unable to initialize SR-IOV mailbox\n");
326 kfree(iov_data);
327 return err;
331 /* assign iov_data to interface */
332 interface->iov_data = iov_data;
334 /* allocate hardware resources for the VFs */
335 fm10k_iov_resume(pdev);
337 return 0;
340 void fm10k_iov_disable(struct pci_dev *pdev)
342 if (pci_num_vf(pdev) && pci_vfs_assigned(pdev))
343 dev_err(&pdev->dev,
344 "Cannot disable SR-IOV while VFs are assigned\n");
345 else
346 pci_disable_sriov(pdev);
348 fm10k_iov_free_data(pdev);
351 static void fm10k_disable_aer_comp_abort(struct pci_dev *pdev)
353 u32 err_sev;
354 int pos;
356 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
357 if (!pos)
358 return;
360 pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &err_sev);
361 err_sev &= ~PCI_ERR_UNC_COMP_ABORT;
362 pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, err_sev);
365 int fm10k_iov_configure(struct pci_dev *pdev, int num_vfs)
367 int current_vfs = pci_num_vf(pdev);
368 int err = 0;
370 if (current_vfs && pci_vfs_assigned(pdev)) {
371 dev_err(&pdev->dev,
372 "Cannot modify SR-IOV while VFs are assigned\n");
373 num_vfs = current_vfs;
374 } else {
375 pci_disable_sriov(pdev);
376 fm10k_iov_free_data(pdev);
379 /* allocate resources for the VFs */
380 err = fm10k_iov_alloc_data(pdev, num_vfs);
381 if (err)
382 return err;
384 /* allocate VFs if not already allocated */
385 if (num_vfs && (num_vfs != current_vfs)) {
386 /* Disable completer abort error reporting as
387 * the VFs can trigger this any time they read a queue
388 * that they don't own.
390 fm10k_disable_aer_comp_abort(pdev);
392 err = pci_enable_sriov(pdev, num_vfs);
393 if (err) {
394 dev_err(&pdev->dev,
395 "Enable PCI SR-IOV failed: %d\n", err);
396 return err;
400 return num_vfs;
403 static inline void fm10k_reset_vf_info(struct fm10k_intfc *interface,
404 struct fm10k_vf_info *vf_info)
406 struct fm10k_hw *hw = &interface->hw;
408 /* assigning the MAC address will send a mailbox message */
409 fm10k_mbx_lock(interface);
411 /* disable LPORT for this VF which clears switch rules */
412 hw->iov.ops.reset_lport(hw, vf_info);
414 /* assign new MAC+VLAN for this VF */
415 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
417 /* re-enable the LPORT for this VF */
418 hw->iov.ops.set_lport(hw, vf_info, vf_info->vf_idx,
419 FM10K_VF_FLAG_MULTI_CAPABLE);
421 fm10k_mbx_unlock(interface);
424 int fm10k_ndo_set_vf_mac(struct net_device *netdev, int vf_idx, u8 *mac)
426 struct fm10k_intfc *interface = netdev_priv(netdev);
427 struct fm10k_iov_data *iov_data = interface->iov_data;
428 struct fm10k_vf_info *vf_info;
430 /* verify SR-IOV is active and that vf idx is valid */
431 if (!iov_data || vf_idx >= iov_data->num_vfs)
432 return -EINVAL;
434 /* verify MAC addr is valid */
435 if (!is_zero_ether_addr(mac) && !is_valid_ether_addr(mac))
436 return -EINVAL;
438 /* record new MAC address */
439 vf_info = &iov_data->vf_info[vf_idx];
440 ether_addr_copy(vf_info->mac, mac);
442 fm10k_reset_vf_info(interface, vf_info);
444 return 0;
447 int fm10k_ndo_set_vf_vlan(struct net_device *netdev, int vf_idx, u16 vid,
448 u8 qos)
450 struct fm10k_intfc *interface = netdev_priv(netdev);
451 struct fm10k_iov_data *iov_data = interface->iov_data;
452 struct fm10k_hw *hw = &interface->hw;
453 struct fm10k_vf_info *vf_info;
455 /* verify SR-IOV is active and that vf idx is valid */
456 if (!iov_data || vf_idx >= iov_data->num_vfs)
457 return -EINVAL;
459 /* QOS is unsupported and VLAN IDs accepted range 0-4094 */
460 if (qos || (vid > (VLAN_VID_MASK - 1)))
461 return -EINVAL;
463 vf_info = &iov_data->vf_info[vf_idx];
465 /* exit if there is nothing to do */
466 if (vf_info->pf_vid == vid)
467 return 0;
469 /* record default VLAN ID for VF */
470 vf_info->pf_vid = vid;
472 /* Clear the VLAN table for the VF */
473 hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, vf_info->vsi, false);
475 fm10k_reset_vf_info(interface, vf_info);
477 return 0;
480 int fm10k_ndo_set_vf_bw(struct net_device *netdev, int vf_idx,
481 int __always_unused unused, int rate)
483 struct fm10k_intfc *interface = netdev_priv(netdev);
484 struct fm10k_iov_data *iov_data = interface->iov_data;
485 struct fm10k_hw *hw = &interface->hw;
487 /* verify SR-IOV is active and that vf idx is valid */
488 if (!iov_data || vf_idx >= iov_data->num_vfs)
489 return -EINVAL;
491 /* rate limit cannot be less than 10Mbs or greater than link speed */
492 if (rate && ((rate < FM10K_VF_TC_MIN) || rate > FM10K_VF_TC_MAX))
493 return -EINVAL;
495 /* store values */
496 iov_data->vf_info[vf_idx].rate = rate;
498 /* update hardware configuration */
499 hw->iov.ops.configure_tc(hw, vf_idx, rate);
501 return 0;
504 int fm10k_ndo_get_vf_config(struct net_device *netdev,
505 int vf_idx, struct ifla_vf_info *ivi)
507 struct fm10k_intfc *interface = netdev_priv(netdev);
508 struct fm10k_iov_data *iov_data = interface->iov_data;
509 struct fm10k_vf_info *vf_info;
511 /* verify SR-IOV is active and that vf idx is valid */
512 if (!iov_data || vf_idx >= iov_data->num_vfs)
513 return -EINVAL;
515 vf_info = &iov_data->vf_info[vf_idx];
517 ivi->vf = vf_idx;
518 ivi->max_tx_rate = vf_info->rate;
519 ivi->min_tx_rate = 0;
520 ether_addr_copy(ivi->mac, vf_info->mac);
521 ivi->vlan = vf_info->pf_vid;
522 ivi->qos = 0;
524 return 0;