]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/ethernet/intel/ixgbe/ixgbe_sriov.c
net:drivers/net: Miscellaneous conversions to ETH_ALEN
[mirror_ubuntu-artful-kernel.git] / drivers / net / ethernet / intel / ixgbe / ixgbe_sriov.c
CommitLineData
17367270
GR
1/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
434c5e39 4 Copyright(c) 1999 - 2013 Intel Corporation.
17367270
GR
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26*******************************************************************************/
27
17367270
GR
28#include <linux/types.h>
29#include <linux/module.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/vmalloc.h>
33#include <linux/string.h>
34#include <linux/in.h>
35#include <linux/ip.h>
36#include <linux/tcp.h>
37#include <linux/ipv6.h>
f646968f 38#ifdef NETIF_F_HW_VLAN_CTAG_TX
17367270
GR
39#include <linux/if_vlan.h>
40#endif
41
42#include "ixgbe.h"
c6bda30a 43#include "ixgbe_type.h"
17367270
GR
44#include "ixgbe_sriov.h"
45
c6bda30a 46#ifdef CONFIG_PCI_IOV
66dcfd75 47static int __ixgbe_enable_sriov(struct ixgbe_adapter *adapter)
c6bda30a
GR
48{
49 struct ixgbe_hw *hw = &adapter->hw;
c6bda30a
GR
50 int num_vf_macvlans, i;
51 struct vf_macvlans *mv_list;
c6bda30a 52
73079ea0 53 adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
c6bda30a
GR
54 e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);
55
73079ea0
AD
56 /* Enable VMDq flag so device will be set in VM mode */
57 adapter->flags |= IXGBE_FLAG_VMDQ_ENABLED;
58 if (!adapter->ring_feature[RING_F_VMDQ].limit)
59 adapter->ring_feature[RING_F_VMDQ].limit = 1;
60 adapter->ring_feature[RING_F_VMDQ].offset = adapter->num_vfs;
61
c6bda30a
GR
62 num_vf_macvlans = hw->mac.num_rar_entries -
63 (IXGBE_MAX_PF_MACVLANS + 1 + adapter->num_vfs);
64
65 adapter->mv_list = mv_list = kcalloc(num_vf_macvlans,
66 sizeof(struct vf_macvlans),
67 GFP_KERNEL);
68 if (mv_list) {
69 /* Initialize list of VF macvlans */
70 INIT_LIST_HEAD(&adapter->vf_mvs.l);
71 for (i = 0; i < num_vf_macvlans; i++) {
72 mv_list->vf = -1;
73 mv_list->free = true;
74 mv_list->rar_entry = hw->mac.num_rar_entries -
75 (i + adapter->num_vfs + 1);
76 list_add(&mv_list->l, &adapter->vf_mvs.l);
77 mv_list++;
78 }
79 }
80
815cccbf
JF
81 /* Initialize default switching mode VEB */
82 IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
9b735984 83 adapter->flags2 |= IXGBE_FLAG2_BRIDGE_MODE_VEB;
815cccbf 84
c6bda30a
GR
85 /* If call to enable VFs succeeded then allocate memory
86 * for per VF control structures.
87 */
88 adapter->vfinfo =
89 kcalloc(adapter->num_vfs,
90 sizeof(struct vf_data_storage), GFP_KERNEL);
91 if (adapter->vfinfo) {
73079ea0
AD
92 /* limit trafffic classes based on VFs enabled */
93 if ((adapter->hw.mac.type == ixgbe_mac_82599EB) &&
94 (adapter->num_vfs < 16)) {
95 adapter->dcb_cfg.num_tcs.pg_tcs = MAX_TRAFFIC_CLASS;
96 adapter->dcb_cfg.num_tcs.pfc_tcs = MAX_TRAFFIC_CLASS;
97 } else if (adapter->num_vfs < 32) {
98 adapter->dcb_cfg.num_tcs.pg_tcs = 4;
99 adapter->dcb_cfg.num_tcs.pfc_tcs = 4;
100 } else {
101 adapter->dcb_cfg.num_tcs.pg_tcs = 1;
102 adapter->dcb_cfg.num_tcs.pfc_tcs = 1;
103 }
104
105 /* We do not support RSS w/ SR-IOV */
106 adapter->ring_feature[RING_F_RSS].limit = 1;
c6bda30a
GR
107
108 /* Disable RSC when in SR-IOV mode */
109 adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
110 IXGBE_FLAG2_RSC_ENABLED);
73079ea0 111
73079ea0 112 /* enable spoof checking for all VFs */
de4c7f65
GR
113 for (i = 0; i < adapter->num_vfs; i++)
114 adapter->vfinfo[i].spoofchk_enabled = true;
66dcfd75
GR
115 return 0;
116 }
117
118 return -ENOMEM;
119}
120
121/* Note this function is called when the user wants to enable SR-IOV
122 * VFs using the now deprecated module parameter
123 */
124void ixgbe_enable_sriov(struct ixgbe_adapter *adapter)
125{
126 int pre_existing_vfs = 0;
127
128 pre_existing_vfs = pci_num_vf(adapter->pdev);
129 if (!pre_existing_vfs && !adapter->num_vfs)
c6bda30a 130 return;
66dcfd75
GR
131
132 if (!pre_existing_vfs)
133 dev_warn(&adapter->pdev->dev,
134 "Enabling SR-IOV VFs using the module parameter is deprecated - please use the pci sysfs interface.\n");
135
136 /* If there are pre-existing VFs then we have to force
137 * use of that many - over ride any module parameter value.
138 * This may result from the user unloading the PF driver
139 * while VFs were assigned to guest VMs or because the VFs
140 * have been created via the new PCI SR-IOV sysfs interface.
141 */
142 if (pre_existing_vfs) {
143 adapter->num_vfs = pre_existing_vfs;
144 dev_warn(&adapter->pdev->dev,
145 "Virtual Functions already enabled for this device - Please reload all VF drivers to avoid spoofed packet errors\n");
146 } else {
147 int err;
148 /*
149 * The 82599 supports up to 64 VFs per physical function
150 * but this implementation limits allocation to 63 so that
151 * basic networking resources are still available to the
152 * physical function. If the user requests greater thn
153 * 63 VFs then it is an error - reset to default of zero.
154 */
155 adapter->num_vfs = min_t(unsigned int, adapter->num_vfs, 63);
156
157 err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
158 if (err) {
159 e_err(probe, "Failed to enable PCI sriov: %d\n", err);
160 adapter->num_vfs = 0;
161 return;
162 }
c6bda30a
GR
163 }
164
66dcfd75
GR
165 if (!__ixgbe_enable_sriov(adapter))
166 return;
167
168 /* If we have gotten to this point then there is no memory available
169 * to manage the VF devices - print message and bail.
170 */
c6bda30a
GR
171 e_err(probe, "Unable to allocate memory for VF Data Storage - "
172 "SRIOV disabled\n");
99d74487 173 ixgbe_disable_sriov(adapter);
c6bda30a 174}
c6bda30a 175
9297127b 176#endif /* #ifdef CONFIG_PCI_IOV */
da36b647 177int ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
c6bda30a
GR
178{
179 struct ixgbe_hw *hw = &adapter->hw;
c6bda30a
GR
180 u32 gpie;
181 u32 vmdctl;
da36b647 182 int rss;
c6bda30a 183
d773d131
AD
184 /* set num VFs to 0 to prevent access to vfinfo */
185 adapter->num_vfs = 0;
186
187 /* free VF control structures */
188 kfree(adapter->vfinfo);
189 adapter->vfinfo = NULL;
190
191 /* free macvlan list */
192 kfree(adapter->mv_list);
193 adapter->mv_list = NULL;
194
99d74487
AD
195 /* if SR-IOV is already disabled then there is nothing to do */
196 if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
da36b647 197 return 0;
99d74487 198
c6bda30a 199#ifdef CONFIG_PCI_IOV
9297127b
AD
200 /*
201 * If our VFs are assigned we cannot shut down SR-IOV
202 * without causing issues, so just leave the hardware
203 * available but disabled
204 */
e507d0cd 205 if (pci_vfs_assigned(adapter->pdev)) {
9297127b 206 e_dev_warn("Unloading driver while VFs are assigned - VFs will not be deallocated\n");
da36b647 207 return -EPERM;
d47e12d6 208 }
c6bda30a
GR
209 /* disable iov and allow time for transactions to clear */
210 pci_disable_sriov(adapter->pdev);
211#endif
212
213 /* turn off device IOV mode */
73079ea0 214 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, 0);
c6bda30a
GR
215 gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
216 gpie &= ~IXGBE_GPIE_VTMODE_MASK;
217 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
218
219 /* set default pool back to 0 */
220 vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
221 vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
222 IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
223 IXGBE_WRITE_FLUSH(hw);
224
1d9c0bfd
AD
225 /* Disable VMDq flag so device will be set in VM mode */
226 if (adapter->ring_feature[RING_F_VMDQ].limit == 1)
227 adapter->flags &= ~IXGBE_FLAG_VMDQ_ENABLED;
228 adapter->ring_feature[RING_F_VMDQ].offset = 0;
229
da36b647
GR
230 rss = min_t(int, IXGBE_MAX_RSS_INDICES, num_online_cpus());
231 adapter->ring_feature[RING_F_RSS].limit = rss;
232
c6bda30a
GR
233 /* take a breather then clean up driver data */
234 msleep(100);
235
c6bda30a 236 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
da36b647
GR
237 return 0;
238}
239
240static int ixgbe_pci_sriov_enable(struct pci_dev *dev, int num_vfs)
241{
242#ifdef CONFIG_PCI_IOV
243 struct ixgbe_adapter *adapter = pci_get_drvdata(dev);
244 int err = 0;
245 int i;
246 int pre_existing_vfs = pci_num_vf(dev);
247
248 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
249 err = ixgbe_disable_sriov(adapter);
250 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
251 goto out;
252
253 if (err)
254 goto err_out;
255
256 /* While the SR-IOV capability structure reports total VFs to be
257 * 64 we limit the actual number that can be allocated to 63 so
258 * that some transmit/receive resources can be reserved to the
259 * PF. The PCI bus driver already checks for other values out of
260 * range.
261 */
262 if (num_vfs > 63) {
263 err = -EPERM;
264 goto err_out;
265 }
266
267 adapter->num_vfs = num_vfs;
268
269 err = __ixgbe_enable_sriov(adapter);
270 if (err)
271 goto err_out;
272
273 for (i = 0; i < adapter->num_vfs; i++)
274 ixgbe_vf_configuration(dev, (i | 0x10000000));
275
276 err = pci_enable_sriov(dev, num_vfs);
277 if (err) {
278 e_dev_warn("Failed to enable PCI sriov: %d\n", err);
279 goto err_out;
280 }
281 ixgbe_sriov_reinit(adapter);
282
283out:
284 return num_vfs;
285
286err_out:
287 return err;
288#endif
289 return 0;
290}
291
292static int ixgbe_pci_sriov_disable(struct pci_dev *dev)
293{
294 struct ixgbe_adapter *adapter = pci_get_drvdata(dev);
295 int err;
296 u32 current_flags = adapter->flags;
297
298 err = ixgbe_disable_sriov(adapter);
299
300 /* Only reinit if no error and state changed */
301 if (!err && current_flags != adapter->flags) {
302 /* ixgbe_disable_sriov() doesn't clear VMDQ flag */
303 adapter->flags &= ~IXGBE_FLAG_VMDQ_ENABLED;
304#ifdef CONFIG_PCI_IOV
305 ixgbe_sriov_reinit(adapter);
306#endif
307 }
308
309 return err;
310}
311
312int ixgbe_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
313{
314 if (num_vfs == 0)
315 return ixgbe_pci_sriov_disable(dev);
316 else
317 return ixgbe_pci_sriov_enable(dev, num_vfs);
c6bda30a
GR
318}
319
5d5b7c39 320static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
58a02bee 321 u32 *msgbuf, u32 vf)
17367270 322{
58a02bee
AD
323 int entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
324 >> IXGBE_VT_MSGINFO_SHIFT;
325 u16 *hash_list = (u16 *)&msgbuf[1];
17367270 326 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
8a07a22d 327 struct ixgbe_hw *hw = &adapter->hw;
17367270 328 int i;
8a07a22d
GR
329 u32 vector_bit;
330 u32 vector_reg;
331 u32 mta_reg;
17367270
GR
332
333 /* only so many hash values supported */
334 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
335
336 /*
337 * salt away the number of multi cast addresses assigned
338 * to this VF for later use to restore when the PF multi cast
339 * list changes
340 */
341 vfinfo->num_vf_mc_hashes = entries;
342
343 /*
344 * VFs are limited to using the MTA hash table for their multicast
345 * addresses
346 */
347 for (i = 0; i < entries; i++) {
e81a1ba8 348 vfinfo->vf_mc_hashes[i] = hash_list[i];
17367270
GR
349 }
350
8a07a22d
GR
351 for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
352 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
353 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
354 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
355 mta_reg |= (1 << vector_bit);
356 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
357 }
17367270
GR
358
359 return 0;
360}
361
a1cbb15c
GR
362static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
363{
364 struct ixgbe_hw *hw = &adapter->hw;
365 struct list_head *pos;
366 struct vf_macvlans *entry;
367
368 list_for_each(pos, &adapter->vf_mvs.l) {
369 entry = list_entry(pos, struct vf_macvlans, l);
23677ce3 370 if (!entry->free)
a1cbb15c
GR
371 hw->mac.ops.set_rar(hw, entry->rar_entry,
372 entry->vf_macvlan,
373 entry->vf, IXGBE_RAH_AV);
374 }
375}
376
17367270
GR
377void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
378{
379 struct ixgbe_hw *hw = &adapter->hw;
380 struct vf_data_storage *vfinfo;
381 int i, j;
382 u32 vector_bit;
383 u32 vector_reg;
384 u32 mta_reg;
385
386 for (i = 0; i < adapter->num_vfs; i++) {
387 vfinfo = &adapter->vfinfo[i];
388 for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
389 hw->addr_ctrl.mta_in_use++;
390 vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
391 vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
392 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
393 mta_reg |= (1 << vector_bit);
394 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
395 }
396 }
a1cbb15c
GR
397
398 /* Restore any VF macvlans */
399 ixgbe_restore_vf_macvlans(adapter);
17367270
GR
400}
401
5d5b7c39
ET
402static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
403 u32 vf)
17367270 404{
b35d4d42
AD
405 /* VLAN 0 is a special case, don't allow it to be removed */
406 if (!vid && !add)
407 return 0;
408
17367270
GR
409 return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
410}
411
872844dd 412static s32 ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
e9f98072
GR
413{
414 struct ixgbe_hw *hw = &adapter->hw;
872844dd 415 int max_frame = msgbuf[1];
e9f98072 416 u32 max_frs;
e9f98072 417
872844dd
AD
418 /*
419 * For 82599EB we have to keep all PFs and VFs operating with
420 * the same max_frame value in order to avoid sending an oversize
421 * frame to a VF. In order to guarantee this is handled correctly
422 * for all cases we have several special exceptions to take into
423 * account before we can enable the VF for receive
424 */
425 if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
426 struct net_device *dev = adapter->netdev;
427 int pf_max_frame = dev->mtu + ETH_HLEN;
428 u32 reg_offset, vf_shift, vfre;
429 s32 err = 0;
430
431#ifdef CONFIG_FCOE
432 if (dev->features & NETIF_F_FCOE_MTU)
433 pf_max_frame = max_t(int, pf_max_frame,
434 IXGBE_FCOE_JUMBO_FRAME_SIZE);
435
436#endif /* CONFIG_FCOE */
bffb3bc9
AD
437 switch (adapter->vfinfo[vf].vf_api) {
438 case ixgbe_mbox_api_11:
439 /*
440 * Version 1.1 supports jumbo frames on VFs if PF has
441 * jumbo frames enabled which means legacy VFs are
442 * disabled
443 */
444 if (pf_max_frame > ETH_FRAME_LEN)
445 break;
446 default:
447 /*
448 * If the PF or VF are running w/ jumbo frames enabled
449 * we need to shut down the VF Rx path as we cannot
450 * support jumbo frames on legacy VFs
451 */
452 if ((pf_max_frame > ETH_FRAME_LEN) ||
453 (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)))
454 err = -EINVAL;
455 break;
456 }
872844dd
AD
457
458 /* determine VF receive enable location */
459 vf_shift = vf % 32;
460 reg_offset = vf / 32;
461
462 /* enable or disable receive depending on error */
463 vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
464 if (err)
465 vfre &= ~(1 << vf_shift);
466 else
467 vfre |= 1 << vf_shift;
468 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), vfre);
469
470 if (err) {
471 e_err(drv, "VF max_frame %d out of range\n", max_frame);
472 return err;
473 }
474 }
e9f98072
GR
475
476 /* MTU < 68 is an error and causes problems on some kernels */
872844dd
AD
477 if (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE) {
478 e_err(drv, "VF max_frame %d out of range\n", max_frame);
479 return -EINVAL;
e9f98072
GR
480 }
481
872844dd
AD
482 /* pull current max frame size from hardware */
483 max_frs = IXGBE_READ_REG(hw, IXGBE_MAXFRS);
484 max_frs &= IXGBE_MHADD_MFS_MASK;
485 max_frs >>= IXGBE_MHADD_MFS_SHIFT;
486
487 if (max_frs < max_frame) {
488 max_frs = max_frame << IXGBE_MHADD_MFS_SHIFT;
e9f98072
GR
489 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
490 }
491
872844dd
AD
492 e_info(hw, "VF requests change max MTU to %d\n", max_frame);
493
494 return 0;
e9f98072
GR
495}
496
5d5b7c39 497static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
17367270
GR
498{
499 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
f0412776 500 vmolr |= (IXGBE_VMOLR_ROMPE |
17367270 501 IXGBE_VMOLR_BAM);
f0412776
GR
502 if (aupe)
503 vmolr |= IXGBE_VMOLR_AUPE;
504 else
505 vmolr &= ~IXGBE_VMOLR_AUPE;
17367270
GR
506 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
507}
508
107d3018
AD
509static void ixgbe_clear_vmvir(struct ixgbe_adapter *adapter, u32 vf)
510{
511 struct ixgbe_hw *hw = &adapter->hw;
512
513 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
514}
5d5b7c39 515static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
17367270
GR
516{
517 struct ixgbe_hw *hw = &adapter->hw;
107d3018 518 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
2850062a 519 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
107d3018
AD
520 u8 num_tcs = netdev_get_num_tc(adapter->netdev);
521
522 /* add PF assigned VLAN or VLAN 0 */
523 ixgbe_set_vf_vlan(adapter, true, vfinfo->pf_vlan, vf);
17367270
GR
524
525 /* reset offloads to defaults */
107d3018
AD
526 ixgbe_set_vmolr(hw, vf, !vfinfo->pf_vlan);
527
528 /* set outgoing tags for VFs */
529 if (!vfinfo->pf_vlan && !vfinfo->pf_qos && !num_tcs) {
530 ixgbe_clear_vmvir(adapter, vf);
7f01648a 531 } else {
107d3018
AD
532 if (vfinfo->pf_qos || !num_tcs)
533 ixgbe_set_vmvir(adapter, vfinfo->pf_vlan,
534 vfinfo->pf_qos, vf);
535 else
536 ixgbe_set_vmvir(adapter, vfinfo->pf_vlan,
537 adapter->default_up, vf);
538
539 if (vfinfo->spoofchk_enabled)
540 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
7f01648a 541 }
17367270
GR
542
543 /* reset multicast table array for vf */
544 adapter->vfinfo[vf].num_vf_mc_hashes = 0;
545
546 /* Flush and reset the mta with the new values */
547 ixgbe_set_rx_mode(adapter->netdev);
548
2850062a 549 hw->mac.ops.clear_rar(hw, rar_entry);
374c65d6
AD
550
551 /* reset VF api back to unknown */
552 adapter->vfinfo[vf].vf_api = ixgbe_mbox_api_10;
17367270
GR
553}
554
5d5b7c39
ET
555static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
556 int vf, unsigned char *mac_addr)
17367270
GR
557{
558 struct ixgbe_hw *hw = &adapter->hw;
2850062a 559 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
17367270 560
d458cdf7 561 memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
2850062a 562 hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
17367270
GR
563
564 return 0;
565}
566
a1cbb15c
GR
567static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
568 int vf, int index, unsigned char *mac_addr)
569{
570 struct ixgbe_hw *hw = &adapter->hw;
571 struct list_head *pos;
572 struct vf_macvlans *entry;
573
574 if (index <= 1) {
575 list_for_each(pos, &adapter->vf_mvs.l) {
576 entry = list_entry(pos, struct vf_macvlans, l);
577 if (entry->vf == vf) {
578 entry->vf = -1;
579 entry->free = true;
580 entry->is_macvlan = false;
581 hw->mac.ops.clear_rar(hw, entry->rar_entry);
582 }
583 }
584 }
585
586 /*
587 * If index was zero then we were asked to clear the uc list
588 * for the VF. We're done.
589 */
590 if (!index)
591 return 0;
592
593 entry = NULL;
594
595 list_for_each(pos, &adapter->vf_mvs.l) {
596 entry = list_entry(pos, struct vf_macvlans, l);
597 if (entry->free)
598 break;
599 }
600
601 /*
602 * If we traversed the entire list and didn't find a free entry
603 * then we're out of space on the RAR table. Also entry may
604 * be NULL because the original memory allocation for the list
605 * failed, which is not fatal but does mean we can't support
606 * VF requests for MACVLAN because we couldn't allocate
607 * memory for the list management required.
608 */
609 if (!entry || !entry->free)
610 return -ENOSPC;
611
612 entry->free = false;
613 entry->is_macvlan = true;
614 entry->vf = vf;
615 memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
616
617 hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
618
619 return 0;
620}
621
17367270
GR
622int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
623{
c60fbb00 624 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
17367270
GR
625 unsigned int vfn = (event_mask & 0x3f);
626
627 bool enable = ((event_mask & 0x10000000U) != 0);
628
d458cdf7
JP
629 if (enable)
630 eth_zero_addr(adapter->vfinfo[vfn].vf_mac_addresses);
17367270
GR
631
632 return 0;
633}
634
58a02bee 635static int ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
17367270
GR
636{
637 struct ixgbe_hw *hw = &adapter->hw;
58a02bee 638 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
b08e1ed9
ET
639 u32 reg, reg_offset, vf_shift;
640 u32 msgbuf[4] = {0, 0, 0, 0};
58a02bee
AD
641 u8 *addr = (u8 *)(&msgbuf[1]);
642
643 e_info(probe, "VF Reset msg received from vf %d\n", vf);
644
645 /* reset the filters for the device */
646 ixgbe_vf_reset_event(adapter, vf);
647
648 /* set vf mac address */
35055928
GR
649 if (!is_zero_ether_addr(vf_mac))
650 ixgbe_set_vf_mac(adapter, vf, vf_mac);
17367270
GR
651
652 vf_shift = vf % 32;
653 reg_offset = vf / 32;
654
58a02bee 655 /* enable transmit for vf */
17367270 656 reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
872844dd 657 reg |= 1 << vf_shift;
17367270
GR
658 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
659
58a02bee 660 /* enable receive for vf */
17367270 661 reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
872844dd
AD
662 reg |= 1 << vf_shift;
663 /*
664 * The 82599 cannot support a mix of jumbo and non-jumbo PF/VFs.
665 * For more info take a look at ixgbe_set_vf_lpe
666 */
667 if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
668 struct net_device *dev = adapter->netdev;
669 int pf_max_frame = dev->mtu + ETH_HLEN;
670
671#ifdef CONFIG_FCOE
672 if (dev->features & NETIF_F_FCOE_MTU)
673 pf_max_frame = max_t(int, pf_max_frame,
674 IXGBE_FCOE_JUMBO_FRAME_SIZE);
675
676#endif /* CONFIG_FCOE */
677 if (pf_max_frame > ETH_FRAME_LEN)
678 reg &= ~(1 << vf_shift);
679 }
17367270
GR
680 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
681
58a02bee
AD
682 /* enable VF mailbox for further messages */
683 adapter->vfinfo[vf].clear_to_send = true;
684
a985b6c3
GR
685 /* Enable counting of spoofed packets in the SSVPC register */
686 reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
687 reg |= (1 << vf_shift);
688 IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
689
58a02bee 690 /* reply to reset with ack and vf mac address */
35055928
GR
691 msgbuf[0] = IXGBE_VF_RESET;
692 if (!is_zero_ether_addr(vf_mac)) {
693 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
694 memcpy(addr, vf_mac, ETH_ALEN);
695 } else {
696 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
697 dev_warn(&adapter->pdev->dev,
698 "VF %d has no MAC address assigned, you may have to assign one manually\n",
699 vf);
700 }
58a02bee
AD
701
702 /*
703 * Piggyback the multicast filter type so VF can compute the
704 * correct vectors
705 */
706 msgbuf[3] = hw->mac.mc_filter_type;
707 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
708
709 return 0;
710}
711
712static int ixgbe_set_vf_mac_addr(struct ixgbe_adapter *adapter,
713 u32 *msgbuf, u32 vf)
714{
715 u8 *new_mac = ((u8 *)(&msgbuf[1]));
716
717 if (!is_valid_ether_addr(new_mac)) {
718 e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
719 return -1;
720 }
721
722 if (adapter->vfinfo[vf].pf_set_mac &&
723 memcmp(adapter->vfinfo[vf].vf_mac_addresses, new_mac,
724 ETH_ALEN)) {
725 e_warn(drv,
726 "VF %d attempted to override administratively set MAC address\n"
727 "Reload the VF driver to resume operations\n",
728 vf);
729 return -1;
730 }
731
3970c323 732 return ixgbe_set_vf_mac(adapter, vf, new_mac) < 0;
58a02bee
AD
733}
734
670224f1
GR
735static int ixgbe_find_vlvf_entry(struct ixgbe_hw *hw, u32 vlan)
736{
737 u32 vlvf;
738 s32 regindex;
739
740 /* short cut the special case */
741 if (vlan == 0)
742 return 0;
743
744 /* Search for the vlan id in the VLVF entries */
745 for (regindex = 1; regindex < IXGBE_VLVF_ENTRIES; regindex++) {
746 vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(regindex));
747 if ((vlvf & VLAN_VID_MASK) == vlan)
748 break;
749 }
750
751 /* Return a negative value if not found */
752 if (regindex >= IXGBE_VLVF_ENTRIES)
753 regindex = -1;
754
755 return regindex;
756}
757
58a02bee
AD
758static int ixgbe_set_vf_vlan_msg(struct ixgbe_adapter *adapter,
759 u32 *msgbuf, u32 vf)
760{
761 struct ixgbe_hw *hw = &adapter->hw;
762 int add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >> IXGBE_VT_MSGINFO_SHIFT;
763 int vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
764 int err;
670224f1
GR
765 s32 reg_ndx;
766 u32 vlvf;
767 u32 bits;
107d3018 768 u8 tcs = netdev_get_num_tc(adapter->netdev);
58a02bee 769
107d3018 770 if (adapter->vfinfo[vf].pf_vlan || tcs) {
58a02bee
AD
771 e_warn(drv,
772 "VF %d attempted to override administratively set VLAN configuration\n"
773 "Reload the VF driver to resume operations\n",
774 vf);
775 return -1;
776 }
777
778 if (add)
779 adapter->vfinfo[vf].vlan_count++;
780 else if (adapter->vfinfo[vf].vlan_count)
781 adapter->vfinfo[vf].vlan_count--;
782
670224f1
GR
783 /* in case of promiscuous mode any VLAN filter set for a VF must
784 * also have the PF pool added to it.
785 */
786 if (add && adapter->netdev->flags & IFF_PROMISC)
787 err = ixgbe_set_vf_vlan(adapter, add, vid, VMDQ_P(0));
788
58a02bee
AD
789 err = ixgbe_set_vf_vlan(adapter, add, vid, vf);
790 if (!err && adapter->vfinfo[vf].spoofchk_enabled)
791 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
792
670224f1
GR
793 /* Go through all the checks to see if the VLAN filter should
794 * be wiped completely.
795 */
796 if (!add && adapter->netdev->flags & IFF_PROMISC) {
797 reg_ndx = ixgbe_find_vlvf_entry(hw, vid);
798 if (reg_ndx < 0)
799 goto out;
800 vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(reg_ndx));
801 /* See if any other pools are set for this VLAN filter
802 * entry other than the PF.
803 */
804 if (VMDQ_P(0) < 32) {
805 bits = IXGBE_READ_REG(hw, IXGBE_VLVFB(reg_ndx * 2));
806 bits &= ~(1 << VMDQ_P(0));
807 bits |= IXGBE_READ_REG(hw,
808 IXGBE_VLVFB(reg_ndx * 2) + 1);
809 } else {
810 bits = IXGBE_READ_REG(hw,
811 IXGBE_VLVFB(reg_ndx * 2) + 1);
812 bits &= ~(1 << (VMDQ_P(0) - 32));
813 bits |= IXGBE_READ_REG(hw, IXGBE_VLVFB(reg_ndx * 2));
814 }
815
816 /* If the filter was removed then ensure PF pool bit
817 * is cleared if the PF only added itself to the pool
818 * because the PF is in promiscuous mode.
819 */
820 if ((vlvf & VLAN_VID_MASK) == vid &&
821 !test_bit(vid, adapter->active_vlans) && !bits)
822 ixgbe_set_vf_vlan(adapter, add, vid, VMDQ_P(0));
823 }
824
825out:
826
58a02bee
AD
827 return err;
828}
829
830static int ixgbe_set_vf_macvlan_msg(struct ixgbe_adapter *adapter,
831 u32 *msgbuf, u32 vf)
832{
833 u8 *new_mac = ((u8 *)(&msgbuf[1]));
834 int index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
835 IXGBE_VT_MSGINFO_SHIFT;
836 int err;
837
838 if (adapter->vfinfo[vf].pf_set_mac && index > 0) {
839 e_warn(drv,
840 "VF %d requested MACVLAN filter but is administratively denied\n",
841 vf);
842 return -1;
843 }
844
845 /* An non-zero index indicates the VF is setting a filter */
846 if (index) {
847 if (!is_valid_ether_addr(new_mac)) {
848 e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
849 return -1;
850 }
851
852 /*
853 * If the VF is allowed to set MAC filters then turn off
854 * anti-spoofing to avoid false positives.
855 */
856 if (adapter->vfinfo[vf].spoofchk_enabled)
857 ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
858 }
859
860 err = ixgbe_set_vf_macvlan(adapter, vf, index, new_mac);
861 if (err == -ENOSPC)
862 e_warn(drv,
863 "VF %d has requested a MACVLAN filter but there is no space for it\n",
864 vf);
a3013405
GR
865
866 return err < 0;
17367270
GR
867}
868
374c65d6
AD
869static int ixgbe_negotiate_vf_api(struct ixgbe_adapter *adapter,
870 u32 *msgbuf, u32 vf)
871{
872 int api = msgbuf[1];
873
874 switch (api) {
875 case ixgbe_mbox_api_10:
bffb3bc9 876 case ixgbe_mbox_api_11:
374c65d6
AD
877 adapter->vfinfo[vf].vf_api = api;
878 return 0;
879 default:
880 break;
881 }
882
883 e_info(drv, "VF %d requested invalid api version %u\n", vf, api);
884
885 return -1;
886}
887
f591cd9d
AD
888static int ixgbe_get_vf_queues(struct ixgbe_adapter *adapter,
889 u32 *msgbuf, u32 vf)
890{
891 struct net_device *dev = adapter->netdev;
892 struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
893 unsigned int default_tc = 0;
894 u8 num_tcs = netdev_get_num_tc(dev);
895
896 /* verify the PF is supporting the correct APIs */
897 switch (adapter->vfinfo[vf].vf_api) {
898 case ixgbe_mbox_api_20:
899 case ixgbe_mbox_api_11:
900 break;
901 default:
902 return -1;
903 }
904
905 /* only allow 1 Tx queue for bandwidth limiting */
906 msgbuf[IXGBE_VF_TX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
907 msgbuf[IXGBE_VF_RX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
908
909 /* if TCs > 1 determine which TC belongs to default user priority */
910 if (num_tcs > 1)
911 default_tc = netdev_get_prio_tc_map(dev, adapter->default_up);
912
913 /* notify VF of need for VLAN tag stripping, and correct queue */
914 if (num_tcs)
915 msgbuf[IXGBE_VF_TRANS_VLAN] = num_tcs;
916 else if (adapter->vfinfo[vf].pf_vlan || adapter->vfinfo[vf].pf_qos)
917 msgbuf[IXGBE_VF_TRANS_VLAN] = 1;
918 else
919 msgbuf[IXGBE_VF_TRANS_VLAN] = 0;
920
921 /* notify VF of default queue */
922 msgbuf[IXGBE_VF_DEF_QUEUE] = default_tc;
923
924 return 0;
925}
926
17367270
GR
927static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
928{
929 u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
c050999e 930 u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
17367270
GR
931 struct ixgbe_hw *hw = &adapter->hw;
932 s32 retval;
17367270
GR
933
934 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
935
dcaccc82 936 if (retval) {
849c4542 937 pr_err("Error receiving message from VF\n");
dcaccc82
AD
938 return retval;
939 }
17367270
GR
940
941 /* this is a message we already processed, do nothing */
942 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
943 return retval;
944
dcaccc82
AD
945 /* flush the ack before we write any messages back */
946 IXGBE_WRITE_FLUSH(hw);
947
374c65d6
AD
948 if (msgbuf[0] == IXGBE_VF_RESET)
949 return ixgbe_vf_reset_msg(adapter, vf);
950
17367270
GR
951 /*
952 * until the vf completes a virtual function reset it should not be
953 * allowed to start any configuration.
954 */
17367270
GR
955 if (!adapter->vfinfo[vf].clear_to_send) {
956 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
957 ixgbe_write_mbx(hw, msgbuf, 1, vf);
958 return retval;
959 }
960
961 switch ((msgbuf[0] & 0xFFFF)) {
962 case IXGBE_VF_SET_MAC_ADDR:
58a02bee 963 retval = ixgbe_set_vf_mac_addr(adapter, msgbuf, vf);
17367270
GR
964 break;
965 case IXGBE_VF_SET_MULTICAST:
58a02bee
AD
966 retval = ixgbe_set_vf_multicasts(adapter, msgbuf, vf);
967 break;
968 case IXGBE_VF_SET_VLAN:
969 retval = ixgbe_set_vf_vlan_msg(adapter, msgbuf, vf);
17367270
GR
970 break;
971 case IXGBE_VF_SET_LPE:
872844dd 972 retval = ixgbe_set_vf_lpe(adapter, msgbuf, vf);
17367270 973 break;
a1cbb15c 974 case IXGBE_VF_SET_MACVLAN:
58a02bee 975 retval = ixgbe_set_vf_macvlan_msg(adapter, msgbuf, vf);
a1cbb15c 976 break;
374c65d6
AD
977 case IXGBE_VF_API_NEGOTIATE:
978 retval = ixgbe_negotiate_vf_api(adapter, msgbuf, vf);
979 break;
f591cd9d
AD
980 case IXGBE_VF_GET_QUEUES:
981 retval = ixgbe_get_vf_queues(adapter, msgbuf, vf);
982 break;
17367270 983 default:
396e799c 984 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
17367270
GR
985 retval = IXGBE_ERR_MBX;
986 break;
987 }
988
989 /* notify the VF of the results of what it sent us */
990 if (retval)
991 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
992 else
993 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
994
995 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
996
374c65d6 997 ixgbe_write_mbx(hw, msgbuf, mbx_size, vf);
17367270
GR
998
999 return retval;
1000}
1001
1002static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
1003{
1004 struct ixgbe_hw *hw = &adapter->hw;
1005 u32 msg = IXGBE_VT_MSGTYPE_NACK;
1006
1007 /* if device isn't clear to send it shouldn't be reading either */
1008 if (!adapter->vfinfo[vf].clear_to_send)
1009 ixgbe_write_mbx(hw, &msg, 1, vf);
1010}
1011
1012void ixgbe_msg_task(struct ixgbe_adapter *adapter)
1013{
1014 struct ixgbe_hw *hw = &adapter->hw;
1015 u32 vf;
1016
1017 for (vf = 0; vf < adapter->num_vfs; vf++) {
1018 /* process any reset requests */
1019 if (!ixgbe_check_for_rst(hw, vf))
1020 ixgbe_vf_reset_event(adapter, vf);
1021
1022 /* process any messages pending */
1023 if (!ixgbe_check_for_msg(hw, vf))
1024 ixgbe_rcv_msg_from_vf(adapter, vf);
1025
1026 /* process any acks */
1027 if (!ixgbe_check_for_ack(hw, vf))
1028 ixgbe_rcv_ack_from_vf(adapter, vf);
1029 }
1030}
1031
767081ad
GR
1032void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
1033{
1034 struct ixgbe_hw *hw = &adapter->hw;
1035
1036 /* disable transmit and receive for all vfs */
1037 IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
1038 IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
1039
1040 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
1041 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
1042}
1043
1044void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
1045{
1046 struct ixgbe_hw *hw = &adapter->hw;
1047 u32 ping;
1048 int i;
1049
1050 for (i = 0 ; i < adapter->num_vfs; i++) {
1051 ping = IXGBE_PF_CONTROL_MSG;
1052 if (adapter->vfinfo[i].clear_to_send)
1053 ping |= IXGBE_VT_MSGTYPE_CTS;
1054 ixgbe_write_mbx(hw, &ping, 1, i);
1055 }
1056}
1057
7f01648a
GR
1058int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1059{
1060 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1061 if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
1062 return -EINVAL;
1063 adapter->vfinfo[vf].pf_set_mac = true;
1064 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
1065 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
1066 " change effective.");
1067 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
1068 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
1069 " but the PF device is not up.\n");
1070 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
1071 " attempting to use the VF device.\n");
1072 }
1073 return ixgbe_set_vf_mac(adapter, vf, mac);
1074}
1075
1076int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
1077{
1078 int err = 0;
1079 struct ixgbe_adapter *adapter = netdev_priv(netdev);
a985b6c3 1080 struct ixgbe_hw *hw = &adapter->hw;
7f01648a
GR
1081
1082 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
1083 return -EINVAL;
1084 if (vlan || qos) {
026ac677
GR
1085 if (adapter->vfinfo[vf].pf_vlan)
1086 err = ixgbe_set_vf_vlan(adapter, false,
1087 adapter->vfinfo[vf].pf_vlan,
1088 vf);
1089 if (err)
1090 goto out;
7f01648a
GR
1091 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
1092 if (err)
1093 goto out;
107d3018 1094 ixgbe_set_vmvir(adapter, vlan, qos, vf);
a985b6c3 1095 ixgbe_set_vmolr(hw, vf, false);
de4c7f65 1096 if (adapter->vfinfo[vf].spoofchk_enabled)
a1cbb15c 1097 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
de4c7f65 1098 adapter->vfinfo[vf].vlan_count++;
7f01648a
GR
1099 adapter->vfinfo[vf].pf_vlan = vlan;
1100 adapter->vfinfo[vf].pf_qos = qos;
1101 dev_info(&adapter->pdev->dev,
1102 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
1103 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
1104 dev_warn(&adapter->pdev->dev,
1105 "The VF VLAN has been set,"
1106 " but the PF device is not up.\n");
1107 dev_warn(&adapter->pdev->dev,
1108 "Bring the PF device up before"
1109 " attempting to use the VF device.\n");
1110 }
1111 } else {
1112 err = ixgbe_set_vf_vlan(adapter, false,
1113 adapter->vfinfo[vf].pf_vlan, vf);
107d3018 1114 ixgbe_clear_vmvir(adapter, vf);
a985b6c3
GR
1115 ixgbe_set_vmolr(hw, vf, true);
1116 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
de4c7f65
GR
1117 if (adapter->vfinfo[vf].vlan_count)
1118 adapter->vfinfo[vf].vlan_count--;
7f01648a
GR
1119 adapter->vfinfo[vf].pf_vlan = 0;
1120 adapter->vfinfo[vf].pf_qos = 0;
1121 }
1122out:
1123 return err;
1124}
1125
9f66d3ee 1126static int ixgbe_link_mbps(struct ixgbe_adapter *adapter)
ff4ab206 1127{
9f66d3ee 1128 switch (adapter->link_speed) {
ff4ab206
LL
1129 case IXGBE_LINK_SPEED_100_FULL:
1130 return 100;
1131 case IXGBE_LINK_SPEED_1GB_FULL:
1132 return 1000;
1133 case IXGBE_LINK_SPEED_10GB_FULL:
1134 return 10000;
1135 default:
1136 return 0;
1137 }
1138}
1139
9f66d3ee 1140static void ixgbe_set_vf_rate_limit(struct ixgbe_adapter *adapter, int vf)
ff4ab206 1141{
9f66d3ee
AD
1142 struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
1143 struct ixgbe_hw *hw = &adapter->hw;
1144 u32 bcnrc_val = 0;
1145 u16 queue, queues_per_pool;
1146 u16 tx_rate = adapter->vfinfo[vf].tx_rate;
1147
1148 if (tx_rate) {
1149 /* start with base link speed value */
1150 bcnrc_val = adapter->vf_rate_link_speed;
ff4ab206 1151
ff4ab206 1152 /* Calculate the rate factor values to set */
9f66d3ee
AD
1153 bcnrc_val <<= IXGBE_RTTBCNRC_RF_INT_SHIFT;
1154 bcnrc_val /= tx_rate;
1155
1156 /* clear everything but the rate factor */
1157 bcnrc_val &= IXGBE_RTTBCNRC_RF_INT_MASK |
1158 IXGBE_RTTBCNRC_RF_DEC_MASK;
1159
1160 /* enable the rate scheduler */
1161 bcnrc_val |= IXGBE_RTTBCNRC_RS_ENA;
ff4ab206
LL
1162 }
1163
7555e83d
LL
1164 /*
1165 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
1166 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
1167 * and 0x004 otherwise.
1168 */
1169 switch (hw->mac.type) {
1170 case ixgbe_mac_82599EB:
1171 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
1172 break;
1173 case ixgbe_mac_X540:
1174 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
1175 break;
1176 default:
1177 break;
1178 }
1179
9f66d3ee
AD
1180 /* determine how many queues per pool based on VMDq mask */
1181 queues_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
1182
1183 /* write value for all Tx queues belonging to VF */
1184 for (queue = 0; queue < queues_per_pool; queue++) {
1185 unsigned int reg_idx = (vf * queues_per_pool) + queue;
1186
1187 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, reg_idx);
1188 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
1189 }
ff4ab206
LL
1190}
1191
1192void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
1193{
9f66d3ee 1194 int i;
ff4ab206
LL
1195
1196 /* VF Tx rate limit was not set */
9f66d3ee 1197 if (!adapter->vf_rate_link_speed)
ff4ab206
LL
1198 return;
1199
9f66d3ee 1200 if (ixgbe_link_mbps(adapter) != adapter->vf_rate_link_speed) {
ff4ab206
LL
1201 adapter->vf_rate_link_speed = 0;
1202 dev_info(&adapter->pdev->dev,
9f66d3ee 1203 "Link speed has been changed. VF Transmit rate is disabled\n");
ff4ab206
LL
1204 }
1205
1206 for (i = 0; i < adapter->num_vfs; i++) {
9f66d3ee 1207 if (!adapter->vf_rate_link_speed)
ff4ab206
LL
1208 adapter->vfinfo[i].tx_rate = 0;
1209
9f66d3ee 1210 ixgbe_set_vf_rate_limit(adapter, i);
ff4ab206
LL
1211 }
1212}
1213
7f01648a
GR
1214int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
1215{
ff4ab206 1216 struct ixgbe_adapter *adapter = netdev_priv(netdev);
9f66d3ee
AD
1217 int link_speed;
1218
1219 /* verify VF is active */
1220 if (vf >= adapter->num_vfs)
1221 return -EINVAL;
1222
1223 /* verify link is up */
1224 if (!adapter->link_up)
1225 return -EINVAL;
1226
1227 /* verify we are linked at 10Gbps */
1228 link_speed = ixgbe_link_mbps(adapter);
1229 if (link_speed != 10000)
1230 return -EINVAL;
ff4ab206 1231
9f66d3ee
AD
1232 /* rate limit cannot be less than 10Mbs or greater than link speed */
1233 if (tx_rate && ((tx_rate <= 10) || (tx_rate > link_speed)))
ff4ab206
LL
1234 return -EINVAL;
1235
9f66d3ee
AD
1236 /* store values */
1237 adapter->vf_rate_link_speed = link_speed;
1238 adapter->vfinfo[vf].tx_rate = tx_rate;
1239
1240 /* update hardware configuration */
1241 ixgbe_set_vf_rate_limit(adapter, vf);
ff4ab206
LL
1242
1243 return 0;
7f01648a
GR
1244}
1245
de4c7f65
GR
1246int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
1247{
1248 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1249 int vf_target_reg = vf >> 3;
1250 int vf_target_shift = vf % 8;
1251 struct ixgbe_hw *hw = &adapter->hw;
1252 u32 regval;
1253
1254 adapter->vfinfo[vf].spoofchk_enabled = setting;
1255
1256 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
1257 regval &= ~(1 << vf_target_shift);
1258 regval |= (setting << vf_target_shift);
1259 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
1260
1261 if (adapter->vfinfo[vf].vlan_count) {
1262 vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
1263 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
1264 regval &= ~(1 << vf_target_shift);
1265 regval |= (setting << vf_target_shift);
1266 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
1267 }
1268
1269 return 0;
1270}
1271
7f01648a
GR
1272int ixgbe_ndo_get_vf_config(struct net_device *netdev,
1273 int vf, struct ifla_vf_info *ivi)
1274{
1275 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1276 if (vf >= adapter->num_vfs)
1277 return -EINVAL;
1278 ivi->vf = vf;
1279 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
ff4ab206 1280 ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
7f01648a
GR
1281 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
1282 ivi->qos = adapter->vfinfo[vf].pf_qos;
de4c7f65 1283 ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
7f01648a
GR
1284 return 0;
1285}