GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / net / ethernet / intel / fm10k / fm10k_iov.c
1 /* Intel(R) Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2016 Intel Corporation.
3  *
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.
7  *
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.
12  *
13  * The full GNU General Public License is included in this distribution in
14  * the file called "COPYING".
15  *
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
19  */
20
21 #include "fm10k.h"
22 #include "fm10k_vf.h"
23 #include "fm10k_pf.h"
24
25 static s32 fm10k_iov_msg_error(struct fm10k_hw *hw, u32 **results,
26                                struct fm10k_mbx_info *mbx)
27 {
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;
31
32         dev_err(&pdev->dev, "Unknown message ID %u on VF %d\n",
33                 **results & FM10K_TLV_ID_MASK, vf_info->vf_idx);
34
35         return fm10k_tlv_msg_error(hw, results, mbx);
36 }
37
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),
44 };
45
46 s32 fm10k_iov_event(struct fm10k_intfc *interface)
47 {
48         struct fm10k_hw *hw = &interface->hw;
49         struct fm10k_iov_data *iov_data;
50         s64 vflre;
51         int i;
52
53         /* if there is no iov_data then there is no mailbox to process */
54         if (!READ_ONCE(interface->iov_data))
55                 return 0;
56
57         rcu_read_lock();
58
59         iov_data = interface->iov_data;
60
61         /* check again now that we are in the RCU block */
62         if (!iov_data)
63                 goto read_unlock;
64
65         if (!(fm10k_read_reg(hw, FM10K_EICR) & FM10K_EICR_VFLR))
66                 goto read_unlock;
67
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));
75
76                 i = iov_data->num_vfs;
77
78                 for (vflre <<= 64 - i; vflre && i--; vflre += vflre) {
79                         struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
80
81                         if (vflre >= 0)
82                                 continue;
83
84                         hw->iov.ops.reset_resources(hw, vf_info);
85                         vf_info->mbx.ops.connect(hw, &vf_info->mbx);
86                 }
87         } while (i != iov_data->num_vfs);
88
89 read_unlock:
90         rcu_read_unlock();
91
92         return 0;
93 }
94
95 s32 fm10k_iov_mbx(struct fm10k_intfc *interface)
96 {
97         struct fm10k_hw *hw = &interface->hw;
98         struct fm10k_iov_data *iov_data;
99         int i;
100
101         /* if there is no iov_data then there is no mailbox to process */
102         if (!READ_ONCE(interface->iov_data))
103                 return 0;
104
105         rcu_read_lock();
106
107         iov_data = interface->iov_data;
108
109         /* check again now that we are in the RCU block */
110         if (!iov_data)
111                 goto read_unlock;
112
113         /* lock the mailbox for transmit and receive */
114         fm10k_mbx_lock(interface);
115
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.
122          */
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;
128
129                 /* process the SM mailbox first to drain outgoing messages */
130                 hw->mbx.ops.process(hw, &hw->mbx);
131
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);
135
136                 /* reset VFs that have mailbox timed out */
137                 if (!mbx->timeout) {
138                         hw->iov.ops.reset_resources(hw, vf_info);
139                         mbx->ops.connect(hw, mbx);
140                 }
141
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++;
146                         break;
147                 }
148
149                 /* cleanup mailbox and process received messages */
150                 mbx->ops.process(hw, mbx);
151         }
152
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.
156          */
157         if (i >= 0) {
158                 iov_data->next_vf_mbx = i + 1;
159         } else if (iov_data->next_vf_mbx) {
160                 iov_data->next_vf_mbx = 0;
161                 goto process_mbx;
162         }
163
164         /* free the lock */
165         fm10k_mbx_unlock(interface);
166
167 read_unlock:
168         rcu_read_unlock();
169
170         return 0;
171 }
172
173 void fm10k_iov_suspend(struct pci_dev *pdev)
174 {
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;
178         int num_vfs, i;
179
180         /* pull out num_vfs from iov_data */
181         num_vfs = iov_data ? iov_data->num_vfs : 0;
182
183         /* shut down queue mapping for VFs */
184         fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_vf_rss),
185                         FM10K_DGLORTMAP_NONE);
186
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];
190
191                 hw->iov.ops.reset_resources(hw, vf_info);
192                 hw->iov.ops.reset_lport(hw, vf_info);
193         }
194 }
195
196 int fm10k_iov_resume(struct pci_dev *pdev)
197 {
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;
202         int num_vfs, i;
203
204         /* pull out num_vfs from iov_data */
205         num_vfs = iov_data ? iov_data->num_vfs : 0;
206
207         /* return error if iov_data is not already populated */
208         if (!iov_data)
209                 return -ENOMEM;
210
211         /* allocate hardware resources for the VFs */
212         hw->iov.ops.assign_resources(hw, num_vfs, num_vfs);
213
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);
221         dglort.vsi_b = 1;
222
223         hw->mac.ops.configure_dglort_map(hw, &dglort);
224
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];
228
229                 /* allocate all but the last GLORT to the VFs */
230                 if (i == ((~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT))
231                         break;
232
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);
236
237                 /* mailbox is disconnected so we don't send a message */
238                 hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
239
240                 /* now we are ready so we can connect */
241                 vf_info->mbx.ops.connect(hw, &vf_info->mbx);
242         }
243
244         return 0;
245 }
246
247 s32 fm10k_iov_update_pvid(struct fm10k_intfc *interface, u16 glort, u16 pvid)
248 {
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;
253
254         /* no IOV support, not our message to process */
255         if (!iov_data)
256                 return FM10K_ERR_PARAM;
257
258         /* glort outside our range, not our message to process */
259         if (vf_idx >= iov_data->num_vfs)
260                 return FM10K_ERR_PARAM;
261
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);
267         }
268
269         return 0;
270 }
271
272 static void fm10k_iov_free_data(struct pci_dev *pdev)
273 {
274         struct fm10k_intfc *interface = pci_get_drvdata(pdev);
275
276         if (!interface->iov_data)
277                 return;
278
279         /* reclaim hardware resources */
280         fm10k_iov_suspend(pdev);
281
282         /* drop iov_data from interface */
283         kfree_rcu(interface->iov_data, rcu);
284         interface->iov_data = NULL;
285 }
286
287 static s32 fm10k_iov_alloc_data(struct pci_dev *pdev, int num_vfs)
288 {
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;
292         size_t size;
293         int i, err;
294
295         /* return error if iov_data is already populated */
296         if (iov_data)
297                 return -EBUSY;
298
299         /* The PF should always be able to assign resources */
300         if (!hw->iov.ops.assign_resources)
301                 return -ENODEV;
302
303         /* nothing to do if no VFs are requested */
304         if (!num_vfs)
305                 return 0;
306
307         /* allocate memory for VF storage */
308         size = offsetof(struct fm10k_iov_data, vf_info[num_vfs]);
309         iov_data = kzalloc(size, GFP_KERNEL);
310         if (!iov_data)
311                 return -ENOMEM;
312
313         /* record number of VFs */
314         iov_data->num_vfs = num_vfs;
315
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];
319
320                 /* Record VF VSI value */
321                 vf_info->vsi = i + 1;
322                 vf_info->vf_idx = i;
323
324                 /* initialize mailbox memory */
325                 err = fm10k_pfvf_mbx_init(hw, &vf_info->mbx, iov_mbx_data, i);
326                 if (err) {
327                         dev_err(&pdev->dev,
328                                 "Unable to initialize SR-IOV mailbox\n");
329                         kfree(iov_data);
330                         return err;
331                 }
332         }
333
334         /* assign iov_data to interface */
335         interface->iov_data = iov_data;
336
337         /* allocate hardware resources for the VFs */
338         fm10k_iov_resume(pdev);
339
340         return 0;
341 }
342
343 void fm10k_iov_disable(struct pci_dev *pdev)
344 {
345         if (pci_num_vf(pdev) && pci_vfs_assigned(pdev))
346                 dev_err(&pdev->dev,
347                         "Cannot disable SR-IOV while VFs are assigned\n");
348         else
349                 pci_disable_sriov(pdev);
350
351         fm10k_iov_free_data(pdev);
352 }
353
354 static void fm10k_disable_aer_comp_abort(struct pci_dev *pdev)
355 {
356         u32 err_sev;
357         int pos;
358
359         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
360         if (!pos)
361                 return;
362
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);
366 }
367
368 int fm10k_iov_configure(struct pci_dev *pdev, int num_vfs)
369 {
370         int current_vfs = pci_num_vf(pdev);
371         int err = 0;
372
373         if (current_vfs && pci_vfs_assigned(pdev)) {
374                 dev_err(&pdev->dev,
375                         "Cannot modify SR-IOV while VFs are assigned\n");
376                 num_vfs = current_vfs;
377         } else {
378                 pci_disable_sriov(pdev);
379                 fm10k_iov_free_data(pdev);
380         }
381
382         /* allocate resources for the VFs */
383         err = fm10k_iov_alloc_data(pdev, num_vfs);
384         if (err)
385                 return err;
386
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.
392                  */
393                 fm10k_disable_aer_comp_abort(pdev);
394
395                 err = pci_enable_sriov(pdev, num_vfs);
396                 if (err) {
397                         dev_err(&pdev->dev,
398                                 "Enable PCI SR-IOV failed: %d\n", err);
399                         return err;
400                 }
401         }
402
403         return num_vfs;
404 }
405
406 static inline void fm10k_reset_vf_info(struct fm10k_intfc *interface,
407                                        struct fm10k_vf_info *vf_info)
408 {
409         struct fm10k_hw *hw = &interface->hw;
410
411         /* assigning the MAC address will send a mailbox message */
412         fm10k_mbx_lock(interface);
413
414         /* disable LPORT for this VF which clears switch rules */
415         hw->iov.ops.reset_lport(hw, vf_info);
416
417         /* assign new MAC+VLAN for this VF */
418         hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
419
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);
423
424         fm10k_mbx_unlock(interface);
425 }
426
427 int fm10k_ndo_set_vf_mac(struct net_device *netdev, int vf_idx, u8 *mac)
428 {
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;
432
433         /* verify SR-IOV is active and that vf idx is valid */
434         if (!iov_data || vf_idx >= iov_data->num_vfs)
435                 return -EINVAL;
436
437         /* verify MAC addr is valid */
438         if (!is_zero_ether_addr(mac) && !is_valid_ether_addr(mac))
439                 return -EINVAL;
440
441         /* record new MAC address */
442         vf_info = &iov_data->vf_info[vf_idx];
443         ether_addr_copy(vf_info->mac, mac);
444
445         fm10k_reset_vf_info(interface, vf_info);
446
447         return 0;
448 }
449
450 int fm10k_ndo_set_vf_vlan(struct net_device *netdev, int vf_idx, u16 vid,
451                           u8 qos, __be16 vlan_proto)
452 {
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;
457
458         /* verify SR-IOV is active and that vf idx is valid */
459         if (!iov_data || vf_idx >= iov_data->num_vfs)
460                 return -EINVAL;
461
462         /* QOS is unsupported and VLAN IDs accepted range 0-4094 */
463         if (qos || (vid > (VLAN_VID_MASK - 1)))
464                 return -EINVAL;
465
466         /* VF VLAN Protocol part to default is unsupported */
467         if (vlan_proto != htons(ETH_P_8021Q))
468                 return -EPROTONOSUPPORT;
469
470         vf_info = &iov_data->vf_info[vf_idx];
471
472         /* exit if there is nothing to do */
473         if (vf_info->pf_vid == vid)
474                 return 0;
475
476         /* record default VLAN ID for VF */
477         vf_info->pf_vid = vid;
478
479         /* Clear the VLAN table for the VF */
480         hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, vf_info->vsi, false);
481
482         fm10k_reset_vf_info(interface, vf_info);
483
484         return 0;
485 }
486
487 int fm10k_ndo_set_vf_bw(struct net_device *netdev, int vf_idx,
488                         int __always_unused min_rate, int max_rate)
489 {
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;
493
494         /* verify SR-IOV is active and that vf idx is valid */
495         if (!iov_data || vf_idx >= iov_data->num_vfs)
496                 return -EINVAL;
497
498         /* rate limit cannot be less than 10Mbs or greater than link speed */
499         if (max_rate &&
500             (max_rate < FM10K_VF_TC_MIN || max_rate > FM10K_VF_TC_MAX))
501                 return -EINVAL;
502
503         /* store values */
504         iov_data->vf_info[vf_idx].rate = max_rate;
505
506         /* update hardware configuration */
507         hw->iov.ops.configure_tc(hw, vf_idx, max_rate);
508
509         return 0;
510 }
511
512 int fm10k_ndo_get_vf_config(struct net_device *netdev,
513                             int vf_idx, struct ifla_vf_info *ivi)
514 {
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;
518
519         /* verify SR-IOV is active and that vf idx is valid */
520         if (!iov_data || vf_idx >= iov_data->num_vfs)
521                 return -EINVAL;
522
523         vf_info = &iov_data->vf_info[vf_idx];
524
525         ivi->vf = 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;
530         ivi->qos = 0;
531
532         return 0;
533 }