blob: a8571e488ea467187219d45e3098c9a9ca2872fe [file] [log] [blame]
Greg Rose17367272010-01-09 02:25:48 +00001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
Don Skidmore434c5e32013-01-08 05:02:28 +00004 Copyright(c) 1999 - 2013 Intel Corporation.
Greg Rose17367272010-01-09 02:25:48 +00005
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
Greg Rose17367272010-01-09 02:25:48 +000028#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>
Patrick McHardyf6469682013-04-19 02:04:27 +000038#ifdef NETIF_F_HW_VLAN_CTAG_TX
Greg Rose17367272010-01-09 02:25:48 +000039#include <linux/if_vlan.h>
40#endif
41
42#include "ixgbe.h"
Greg Rosec6bda302011-08-24 02:37:55 +000043#include "ixgbe_type.h"
Greg Rose17367272010-01-09 02:25:48 +000044#include "ixgbe_sriov.h"
45
Greg Rosec6bda302011-08-24 02:37:55 +000046#ifdef CONFIG_PCI_IOV
Greg Rose66dcfd72012-12-11 08:26:38 +000047static int __ixgbe_enable_sriov(struct ixgbe_adapter *adapter)
Greg Rosec6bda302011-08-24 02:37:55 +000048{
49 struct ixgbe_hw *hw = &adapter->hw;
Greg Rosec6bda302011-08-24 02:37:55 +000050 int num_vf_macvlans, i;
51 struct vf_macvlans *mv_list;
Greg Rosec6bda302011-08-24 02:37:55 +000052
Alexander Duyck73079ea2012-07-14 06:48:49 +000053 adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
Greg Rosec6bda302011-08-24 02:37:55 +000054 e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);
55
Alexander Duyck73079ea2012-07-14 06:48:49 +000056 /* 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
Greg Rosec6bda302011-08-24 02:37:55 +000062 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
John Fastabend815cccb2012-10-24 08:13:09 +000081 /* Initialize default switching mode VEB */
82 IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
Greg Rose9b735982012-11-08 02:41:35 +000083 adapter->flags2 |= IXGBE_FLAG2_BRIDGE_MODE_VEB;
John Fastabend815cccb2012-10-24 08:13:09 +000084
Greg Rosec6bda302011-08-24 02:37:55 +000085 /* 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) {
Alexander Duyck73079ea2012-07-14 06:48:49 +000092 /* 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;
Greg Rosec6bda302011-08-24 02:37:55 +0000107
108 /* Disable RSC when in SR-IOV mode */
109 adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
110 IXGBE_FLAG2_RSC_ENABLED);
Alexander Duyck73079ea2012-07-14 06:48:49 +0000111
Alexander Duyck73079ea2012-07-14 06:48:49 +0000112 /* enable spoof checking for all VFs */
Greg Rosede4c7f62011-09-29 05:57:33 +0000113 for (i = 0; i < adapter->num_vfs; i++)
114 adapter->vfinfo[i].spoofchk_enabled = true;
Greg Rose66dcfd72012-12-11 08:26:38 +0000115 return 0;
Greg Rosec6bda302011-08-24 02:37:55 +0000116 }
117
Greg Rose66dcfd72012-12-11 08:26:38 +0000118 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)
130 return;
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 }
163 }
164
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 */
Greg Rosec6bda302011-08-24 02:37:55 +0000171 e_err(probe, "Unable to allocate memory for VF Data Storage - "
172 "SRIOV disabled\n");
Alexander Duyck99d74482012-05-09 08:09:25 +0000173 ixgbe_disable_sriov(adapter);
Greg Rosec6bda302011-08-24 02:37:55 +0000174}
Greg Rosec6bda302011-08-24 02:37:55 +0000175
Alexander Duyck92971272012-05-23 02:58:40 +0000176#endif /* #ifdef CONFIG_PCI_IOV */
Greg Roseda36b642012-12-11 08:26:43 +0000177int ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
Greg Rosec6bda302011-08-24 02:37:55 +0000178{
179 struct ixgbe_hw *hw = &adapter->hw;
Greg Rosec6bda302011-08-24 02:37:55 +0000180 u32 gpie;
181 u32 vmdctl;
Greg Roseda36b642012-12-11 08:26:43 +0000182 int rss;
Greg Rosec6bda302011-08-24 02:37:55 +0000183
Alexander Duyckd773d132012-05-05 05:32:26 +0000184 /* 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
Alexander Duyck99d74482012-05-09 08:09:25 +0000195 /* if SR-IOV is already disabled then there is nothing to do */
196 if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
Greg Roseda36b642012-12-11 08:26:43 +0000197 return 0;
Alexander Duyck99d74482012-05-09 08:09:25 +0000198
Greg Rosec6bda302011-08-24 02:37:55 +0000199#ifdef CONFIG_PCI_IOV
Alexander Duyck92971272012-05-23 02:58:40 +0000200 /*
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 */
Alexander Duycke507d0c2013-03-26 00:03:21 +0000205 if (pci_vfs_assigned(adapter->pdev)) {
Alexander Duyck92971272012-05-23 02:58:40 +0000206 e_dev_warn("Unloading driver while VFs are assigned - VFs will not be deallocated\n");
Greg Roseda36b642012-12-11 08:26:43 +0000207 return -EPERM;
David S. Millerd47e12d2012-07-22 12:36:41 -0700208 }
Greg Rosec6bda302011-08-24 02:37:55 +0000209 /* disable iov and allow time for transactions to clear */
210 pci_disable_sriov(adapter->pdev);
211#endif
212
213 /* turn off device IOV mode */
Alexander Duyck73079ea2012-07-14 06:48:49 +0000214 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, 0);
Greg Rosec6bda302011-08-24 02:37:55 +0000215 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
Alexander Duyck1d9c0bf2012-05-05 05:32:21 +0000225 /* Disable VMDq flag so device will be set in VM mode */
John Fastabend2a47fa42013-11-06 09:54:52 -0800226 if (adapter->ring_feature[RING_F_VMDQ].limit == 1) {
Alexander Duyck1d9c0bf2012-05-05 05:32:21 +0000227 adapter->flags &= ~IXGBE_FLAG_VMDQ_ENABLED;
John Fastabend2a47fa42013-11-06 09:54:52 -0800228 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
229 rss = min_t(int, IXGBE_MAX_RSS_INDICES, num_online_cpus());
230 } else {
231 rss = min_t(int, IXGBE_MAX_L2A_QUEUES, num_online_cpus());
232 }
Alexander Duyck1d9c0bf2012-05-05 05:32:21 +0000233
John Fastabend2a47fa42013-11-06 09:54:52 -0800234 adapter->ring_feature[RING_F_VMDQ].offset = 0;
Greg Roseda36b642012-12-11 08:26:43 +0000235 adapter->ring_feature[RING_F_RSS].limit = rss;
236
Greg Rosec6bda302011-08-24 02:37:55 +0000237 /* take a breather then clean up driver data */
238 msleep(100);
Greg Roseda36b642012-12-11 08:26:43 +0000239 return 0;
240}
241
242static int ixgbe_pci_sriov_enable(struct pci_dev *dev, int num_vfs)
243{
244#ifdef CONFIG_PCI_IOV
245 struct ixgbe_adapter *adapter = pci_get_drvdata(dev);
246 int err = 0;
247 int i;
248 int pre_existing_vfs = pci_num_vf(dev);
249
250 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
251 err = ixgbe_disable_sriov(adapter);
252 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
253 goto out;
254
255 if (err)
256 goto err_out;
257
258 /* While the SR-IOV capability structure reports total VFs to be
259 * 64 we limit the actual number that can be allocated to 63 so
260 * that some transmit/receive resources can be reserved to the
261 * PF. The PCI bus driver already checks for other values out of
262 * range.
263 */
264 if (num_vfs > 63) {
265 err = -EPERM;
266 goto err_out;
267 }
268
269 adapter->num_vfs = num_vfs;
270
271 err = __ixgbe_enable_sriov(adapter);
272 if (err)
273 goto err_out;
274
275 for (i = 0; i < adapter->num_vfs; i++)
276 ixgbe_vf_configuration(dev, (i | 0x10000000));
277
278 err = pci_enable_sriov(dev, num_vfs);
279 if (err) {
280 e_dev_warn("Failed to enable PCI sriov: %d\n", err);
281 goto err_out;
282 }
283 ixgbe_sriov_reinit(adapter);
284
285out:
286 return num_vfs;
287
288err_out:
289 return err;
290#endif
291 return 0;
292}
293
294static int ixgbe_pci_sriov_disable(struct pci_dev *dev)
295{
296 struct ixgbe_adapter *adapter = pci_get_drvdata(dev);
297 int err;
298 u32 current_flags = adapter->flags;
299
300 err = ixgbe_disable_sriov(adapter);
301
302 /* Only reinit if no error and state changed */
Greg Roseda36b642012-12-11 08:26:43 +0000303#ifdef CONFIG_PCI_IOV
John Fastabend2a47fa42013-11-06 09:54:52 -0800304 if (!err && current_flags != adapter->flags)
Greg Roseda36b642012-12-11 08:26:43 +0000305 ixgbe_sriov_reinit(adapter);
306#endif
Greg Roseda36b642012-12-11 08:26:43 +0000307
308 return err;
309}
310
311int ixgbe_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
312{
313 if (num_vfs == 0)
314 return ixgbe_pci_sriov_disable(dev);
315 else
316 return ixgbe_pci_sriov_enable(dev, num_vfs);
Greg Rosec6bda302011-08-24 02:37:55 +0000317}
318
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000319static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
Alexander Duyck58a02be2012-07-20 08:09:17 +0000320 u32 *msgbuf, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000321{
Alexander Duyck58a02be2012-07-20 08:09:17 +0000322 int entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
323 >> IXGBE_VT_MSGINFO_SHIFT;
324 u16 *hash_list = (u16 *)&msgbuf[1];
Greg Rose17367272010-01-09 02:25:48 +0000325 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
Greg Rose8a07a222010-05-05 19:57:30 +0000326 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose17367272010-01-09 02:25:48 +0000327 int i;
Greg Rose8a07a222010-05-05 19:57:30 +0000328 u32 vector_bit;
329 u32 vector_reg;
330 u32 mta_reg;
Greg Rose17367272010-01-09 02:25:48 +0000331
332 /* only so many hash values supported */
333 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
334
335 /*
336 * salt away the number of multi cast addresses assigned
337 * to this VF for later use to restore when the PF multi cast
338 * list changes
339 */
340 vfinfo->num_vf_mc_hashes = entries;
341
342 /*
343 * VFs are limited to using the MTA hash table for their multicast
344 * addresses
345 */
346 for (i = 0; i < entries; i++) {
Joe Perchese81a1ba2010-11-14 17:04:33 +0000347 vfinfo->vf_mc_hashes[i] = hash_list[i];
Greg Rose17367272010-01-09 02:25:48 +0000348 }
349
Greg Rose8a07a222010-05-05 19:57:30 +0000350 for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
351 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
352 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
353 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
354 mta_reg |= (1 << vector_bit);
355 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
356 }
Greg Rose17367272010-01-09 02:25:48 +0000357
358 return 0;
359}
360
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000361static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
362{
363 struct ixgbe_hw *hw = &adapter->hw;
364 struct list_head *pos;
365 struct vf_macvlans *entry;
366
367 list_for_each(pos, &adapter->vf_mvs.l) {
368 entry = list_entry(pos, struct vf_macvlans, l);
Joe Perches23677ce2012-02-09 11:17:23 +0000369 if (!entry->free)
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000370 hw->mac.ops.set_rar(hw, entry->rar_entry,
371 entry->vf_macvlan,
372 entry->vf, IXGBE_RAH_AV);
373 }
374}
375
Greg Rose17367272010-01-09 02:25:48 +0000376void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
377{
378 struct ixgbe_hw *hw = &adapter->hw;
379 struct vf_data_storage *vfinfo;
380 int i, j;
381 u32 vector_bit;
382 u32 vector_reg;
383 u32 mta_reg;
384
385 for (i = 0; i < adapter->num_vfs; i++) {
386 vfinfo = &adapter->vfinfo[i];
387 for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
388 hw->addr_ctrl.mta_in_use++;
389 vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
390 vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
391 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
392 mta_reg |= (1 << vector_bit);
393 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
394 }
395 }
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000396
397 /* Restore any VF macvlans */
398 ixgbe_restore_vf_macvlans(adapter);
Greg Rose17367272010-01-09 02:25:48 +0000399}
400
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000401static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
402 u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000403{
Alexander Duyckb35d4d42012-05-23 05:39:25 +0000404 /* VLAN 0 is a special case, don't allow it to be removed */
405 if (!vid && !add)
406 return 0;
407
Greg Rose17367272010-01-09 02:25:48 +0000408 return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
409}
410
Alexander Duyck872844d2012-08-15 02:10:43 +0000411static s32 ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
Greg Rosee9f98072011-01-26 01:06:07 +0000412{
413 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck872844d2012-08-15 02:10:43 +0000414 int max_frame = msgbuf[1];
Greg Rosee9f98072011-01-26 01:06:07 +0000415 u32 max_frs;
Greg Rosee9f98072011-01-26 01:06:07 +0000416
Alexander Duyck872844d2012-08-15 02:10:43 +0000417 /*
418 * For 82599EB we have to keep all PFs and VFs operating with
419 * the same max_frame value in order to avoid sending an oversize
420 * frame to a VF. In order to guarantee this is handled correctly
421 * for all cases we have several special exceptions to take into
422 * account before we can enable the VF for receive
423 */
424 if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
425 struct net_device *dev = adapter->netdev;
426 int pf_max_frame = dev->mtu + ETH_HLEN;
427 u32 reg_offset, vf_shift, vfre;
428 s32 err = 0;
Greg Rosee9f98072011-01-26 01:06:07 +0000429
Alexander Duyck872844d2012-08-15 02:10:43 +0000430#ifdef CONFIG_FCOE
431 if (dev->features & NETIF_F_FCOE_MTU)
432 pf_max_frame = max_t(int, pf_max_frame,
433 IXGBE_FCOE_JUMBO_FRAME_SIZE);
434
435#endif /* CONFIG_FCOE */
Alexander Duyckbffb3bc2012-07-20 08:09:37 +0000436 switch (adapter->vfinfo[vf].vf_api) {
437 case ixgbe_mbox_api_11:
438 /*
439 * Version 1.1 supports jumbo frames on VFs if PF has
440 * jumbo frames enabled which means legacy VFs are
441 * disabled
442 */
443 if (pf_max_frame > ETH_FRAME_LEN)
444 break;
445 default:
446 /*
447 * If the PF or VF are running w/ jumbo frames enabled
448 * we need to shut down the VF Rx path as we cannot
449 * support jumbo frames on legacy VFs
450 */
451 if ((pf_max_frame > ETH_FRAME_LEN) ||
452 (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)))
453 err = -EINVAL;
454 break;
455 }
Alexander Duyck872844d2012-08-15 02:10:43 +0000456
457 /* determine VF receive enable location */
458 vf_shift = vf % 32;
459 reg_offset = vf / 32;
460
461 /* enable or disable receive depending on error */
462 vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
463 if (err)
464 vfre &= ~(1 << vf_shift);
465 else
466 vfre |= 1 << vf_shift;
467 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), vfre);
468
469 if (err) {
470 e_err(drv, "VF max_frame %d out of range\n", max_frame);
471 return err;
472 }
Greg Rosee9f98072011-01-26 01:06:07 +0000473 }
474
Alexander Duyck872844d2012-08-15 02:10:43 +0000475 /* MTU < 68 is an error and causes problems on some kernels */
476 if (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE) {
477 e_err(drv, "VF max_frame %d out of range\n", max_frame);
478 return -EINVAL;
479 }
480
481 /* pull current max frame size from hardware */
482 max_frs = IXGBE_READ_REG(hw, IXGBE_MAXFRS);
483 max_frs &= IXGBE_MHADD_MFS_MASK;
484 max_frs >>= IXGBE_MHADD_MFS_SHIFT;
485
486 if (max_frs < max_frame) {
487 max_frs = max_frame << IXGBE_MHADD_MFS_SHIFT;
Greg Rosee9f98072011-01-26 01:06:07 +0000488 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
489 }
490
Alexander Duyck872844d2012-08-15 02:10:43 +0000491 e_info(hw, "VF requests change max MTU to %d\n", max_frame);
492
493 return 0;
Greg Rosee9f98072011-01-26 01:06:07 +0000494}
495
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000496static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
Greg Rose17367272010-01-09 02:25:48 +0000497{
498 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
Greg Rosef0412772010-05-04 22:11:46 +0000499 vmolr |= (IXGBE_VMOLR_ROMPE |
Greg Rose17367272010-01-09 02:25:48 +0000500 IXGBE_VMOLR_BAM);
Greg Rosef0412772010-05-04 22:11:46 +0000501 if (aupe)
502 vmolr |= IXGBE_VMOLR_AUPE;
503 else
504 vmolr &= ~IXGBE_VMOLR_AUPE;
Greg Rose17367272010-01-09 02:25:48 +0000505 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
506}
507
Alexander Duyck107d3012012-10-02 00:17:03 +0000508static void ixgbe_clear_vmvir(struct ixgbe_adapter *adapter, u32 vf)
Greg Rose7f016482010-05-04 22:12:06 +0000509{
510 struct ixgbe_hw *hw = &adapter->hw;
511
Alexander Duyck107d3012012-10-02 00:17:03 +0000512 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
Greg Rose7f016482010-05-04 22:12:06 +0000513}
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000514static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000515{
516 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck107d3012012-10-02 00:17:03 +0000517 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
Alexander Duyck28500622010-06-15 09:25:48 +0000518 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
Alexander Duyck107d3012012-10-02 00:17:03 +0000519 u8 num_tcs = netdev_get_num_tc(adapter->netdev);
520
521 /* add PF assigned VLAN or VLAN 0 */
522 ixgbe_set_vf_vlan(adapter, true, vfinfo->pf_vlan, vf);
Greg Rose17367272010-01-09 02:25:48 +0000523
524 /* reset offloads to defaults */
Alexander Duyck107d3012012-10-02 00:17:03 +0000525 ixgbe_set_vmolr(hw, vf, !vfinfo->pf_vlan);
526
527 /* set outgoing tags for VFs */
528 if (!vfinfo->pf_vlan && !vfinfo->pf_qos && !num_tcs) {
529 ixgbe_clear_vmvir(adapter, vf);
Greg Rose7f016482010-05-04 22:12:06 +0000530 } else {
Alexander Duyck107d3012012-10-02 00:17:03 +0000531 if (vfinfo->pf_qos || !num_tcs)
532 ixgbe_set_vmvir(adapter, vfinfo->pf_vlan,
533 vfinfo->pf_qos, vf);
534 else
535 ixgbe_set_vmvir(adapter, vfinfo->pf_vlan,
536 adapter->default_up, vf);
537
538 if (vfinfo->spoofchk_enabled)
539 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
Greg Rose7f016482010-05-04 22:12:06 +0000540 }
Greg Rose17367272010-01-09 02:25:48 +0000541
542 /* reset multicast table array for vf */
543 adapter->vfinfo[vf].num_vf_mc_hashes = 0;
544
545 /* Flush and reset the mta with the new values */
546 ixgbe_set_rx_mode(adapter->netdev);
547
Alexander Duyck28500622010-06-15 09:25:48 +0000548 hw->mac.ops.clear_rar(hw, rar_entry);
Alexander Duyck374c65d2012-07-20 08:09:22 +0000549
550 /* reset VF api back to unknown */
551 adapter->vfinfo[vf].vf_api = ixgbe_mbox_api_10;
Greg Rose17367272010-01-09 02:25:48 +0000552}
553
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000554static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
555 int vf, unsigned char *mac_addr)
Greg Rose17367272010-01-09 02:25:48 +0000556{
557 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck28500622010-06-15 09:25:48 +0000558 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
Greg Rose17367272010-01-09 02:25:48 +0000559
Joe Perchesd458cdf2013-10-01 19:04:40 -0700560 memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
Alexander Duyck28500622010-06-15 09:25:48 +0000561 hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
Greg Rose17367272010-01-09 02:25:48 +0000562
563 return 0;
564}
565
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000566static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
567 int vf, int index, unsigned char *mac_addr)
568{
569 struct ixgbe_hw *hw = &adapter->hw;
570 struct list_head *pos;
571 struct vf_macvlans *entry;
572
573 if (index <= 1) {
574 list_for_each(pos, &adapter->vf_mvs.l) {
575 entry = list_entry(pos, struct vf_macvlans, l);
576 if (entry->vf == vf) {
577 entry->vf = -1;
578 entry->free = true;
579 entry->is_macvlan = false;
580 hw->mac.ops.clear_rar(hw, entry->rar_entry);
581 }
582 }
583 }
584
585 /*
586 * If index was zero then we were asked to clear the uc list
587 * for the VF. We're done.
588 */
589 if (!index)
590 return 0;
591
592 entry = NULL;
593
594 list_for_each(pos, &adapter->vf_mvs.l) {
595 entry = list_entry(pos, struct vf_macvlans, l);
596 if (entry->free)
597 break;
598 }
599
600 /*
601 * If we traversed the entire list and didn't find a free entry
602 * then we're out of space on the RAR table. Also entry may
603 * be NULL because the original memory allocation for the list
604 * failed, which is not fatal but does mean we can't support
605 * VF requests for MACVLAN because we couldn't allocate
606 * memory for the list management required.
607 */
608 if (!entry || !entry->free)
609 return -ENOSPC;
610
611 entry->free = false;
612 entry->is_macvlan = true;
613 entry->vf = vf;
614 memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
615
616 hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
617
618 return 0;
619}
620
Greg Rose17367272010-01-09 02:25:48 +0000621int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
622{
Alexander Duyckc60fbb02010-11-16 19:26:54 -0800623 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
Greg Rose17367272010-01-09 02:25:48 +0000624 unsigned int vfn = (event_mask & 0x3f);
625
626 bool enable = ((event_mask & 0x10000000U) != 0);
627
Joe Perchesd458cdf2013-10-01 19:04:40 -0700628 if (enable)
629 eth_zero_addr(adapter->vfinfo[vfn].vf_mac_addresses);
Greg Rose17367272010-01-09 02:25:48 +0000630
631 return 0;
632}
633
Alexander Duyck58a02be2012-07-20 08:09:17 +0000634static int ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000635{
636 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck58a02be2012-07-20 08:09:17 +0000637 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
Emil Tantilovb08e1ed2013-07-26 07:34:54 +0000638 u32 reg, reg_offset, vf_shift;
639 u32 msgbuf[4] = {0, 0, 0, 0};
Alexander Duyck58a02be2012-07-20 08:09:17 +0000640 u8 *addr = (u8 *)(&msgbuf[1]);
641
642 e_info(probe, "VF Reset msg received from vf %d\n", vf);
643
644 /* reset the filters for the device */
645 ixgbe_vf_reset_event(adapter, vf);
646
647 /* set vf mac address */
Greg Rose35055922013-02-15 05:20:09 +0000648 if (!is_zero_ether_addr(vf_mac))
649 ixgbe_set_vf_mac(adapter, vf, vf_mac);
Greg Rose17367272010-01-09 02:25:48 +0000650
651 vf_shift = vf % 32;
652 reg_offset = vf / 32;
653
Alexander Duyck58a02be2012-07-20 08:09:17 +0000654 /* enable transmit for vf */
Greg Rose17367272010-01-09 02:25:48 +0000655 reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
Alexander Duyck872844d2012-08-15 02:10:43 +0000656 reg |= 1 << vf_shift;
Greg Rose17367272010-01-09 02:25:48 +0000657 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
658
Alexander Duyck58a02be2012-07-20 08:09:17 +0000659 /* enable receive for vf */
Greg Rose17367272010-01-09 02:25:48 +0000660 reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
Alexander Duyck872844d2012-08-15 02:10:43 +0000661 reg |= 1 << vf_shift;
662 /*
663 * The 82599 cannot support a mix of jumbo and non-jumbo PF/VFs.
664 * For more info take a look at ixgbe_set_vf_lpe
665 */
666 if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
667 struct net_device *dev = adapter->netdev;
668 int pf_max_frame = dev->mtu + ETH_HLEN;
669
670#ifdef CONFIG_FCOE
671 if (dev->features & NETIF_F_FCOE_MTU)
672 pf_max_frame = max_t(int, pf_max_frame,
673 IXGBE_FCOE_JUMBO_FRAME_SIZE);
674
675#endif /* CONFIG_FCOE */
676 if (pf_max_frame > ETH_FRAME_LEN)
677 reg &= ~(1 << vf_shift);
678 }
Greg Rose17367272010-01-09 02:25:48 +0000679 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
680
Alexander Duyck58a02be2012-07-20 08:09:17 +0000681 /* enable VF mailbox for further messages */
682 adapter->vfinfo[vf].clear_to_send = true;
683
Greg Rosea985b6c32010-11-18 03:02:52 +0000684 /* Enable counting of spoofed packets in the SSVPC register */
685 reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
686 reg |= (1 << vf_shift);
687 IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
688
Alexander Duyck58a02be2012-07-20 08:09:17 +0000689 /* reply to reset with ack and vf mac address */
Greg Rose35055922013-02-15 05:20:09 +0000690 msgbuf[0] = IXGBE_VF_RESET;
691 if (!is_zero_ether_addr(vf_mac)) {
692 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
693 memcpy(addr, vf_mac, ETH_ALEN);
694 } else {
695 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
696 dev_warn(&adapter->pdev->dev,
697 "VF %d has no MAC address assigned, you may have to assign one manually\n",
698 vf);
699 }
Alexander Duyck58a02be2012-07-20 08:09:17 +0000700
701 /*
702 * Piggyback the multicast filter type so VF can compute the
703 * correct vectors
704 */
705 msgbuf[3] = hw->mac.mc_filter_type;
706 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
707
708 return 0;
709}
710
711static int ixgbe_set_vf_mac_addr(struct ixgbe_adapter *adapter,
712 u32 *msgbuf, u32 vf)
713{
714 u8 *new_mac = ((u8 *)(&msgbuf[1]));
715
716 if (!is_valid_ether_addr(new_mac)) {
717 e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
718 return -1;
719 }
720
721 if (adapter->vfinfo[vf].pf_set_mac &&
722 memcmp(adapter->vfinfo[vf].vf_mac_addresses, new_mac,
723 ETH_ALEN)) {
724 e_warn(drv,
725 "VF %d attempted to override administratively set MAC address\n"
726 "Reload the VF driver to resume operations\n",
727 vf);
728 return -1;
729 }
730
Greg Rose3970c322012-09-25 02:25:30 +0000731 return ixgbe_set_vf_mac(adapter, vf, new_mac) < 0;
Alexander Duyck58a02be2012-07-20 08:09:17 +0000732}
733
Greg Rose670224f2013-02-22 02:14:39 +0000734static int ixgbe_find_vlvf_entry(struct ixgbe_hw *hw, u32 vlan)
735{
736 u32 vlvf;
737 s32 regindex;
738
739 /* short cut the special case */
740 if (vlan == 0)
741 return 0;
742
743 /* Search for the vlan id in the VLVF entries */
744 for (regindex = 1; regindex < IXGBE_VLVF_ENTRIES; regindex++) {
745 vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(regindex));
746 if ((vlvf & VLAN_VID_MASK) == vlan)
747 break;
748 }
749
750 /* Return a negative value if not found */
751 if (regindex >= IXGBE_VLVF_ENTRIES)
752 regindex = -1;
753
754 return regindex;
755}
756
Alexander Duyck58a02be2012-07-20 08:09:17 +0000757static int ixgbe_set_vf_vlan_msg(struct ixgbe_adapter *adapter,
758 u32 *msgbuf, u32 vf)
759{
760 struct ixgbe_hw *hw = &adapter->hw;
761 int add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >> IXGBE_VT_MSGINFO_SHIFT;
762 int vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
763 int err;
Greg Rose670224f2013-02-22 02:14:39 +0000764 s32 reg_ndx;
765 u32 vlvf;
766 u32 bits;
Alexander Duyck107d3012012-10-02 00:17:03 +0000767 u8 tcs = netdev_get_num_tc(adapter->netdev);
Alexander Duyck58a02be2012-07-20 08:09:17 +0000768
Alexander Duyck107d3012012-10-02 00:17:03 +0000769 if (adapter->vfinfo[vf].pf_vlan || tcs) {
Alexander Duyck58a02be2012-07-20 08:09:17 +0000770 e_warn(drv,
771 "VF %d attempted to override administratively set VLAN configuration\n"
772 "Reload the VF driver to resume operations\n",
773 vf);
774 return -1;
775 }
776
777 if (add)
778 adapter->vfinfo[vf].vlan_count++;
779 else if (adapter->vfinfo[vf].vlan_count)
780 adapter->vfinfo[vf].vlan_count--;
781
Greg Rose670224f2013-02-22 02:14:39 +0000782 /* in case of promiscuous mode any VLAN filter set for a VF must
783 * also have the PF pool added to it.
784 */
785 if (add && adapter->netdev->flags & IFF_PROMISC)
786 err = ixgbe_set_vf_vlan(adapter, add, vid, VMDQ_P(0));
787
Alexander Duyck58a02be2012-07-20 08:09:17 +0000788 err = ixgbe_set_vf_vlan(adapter, add, vid, vf);
789 if (!err && adapter->vfinfo[vf].spoofchk_enabled)
790 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
791
Greg Rose670224f2013-02-22 02:14:39 +0000792 /* Go through all the checks to see if the VLAN filter should
793 * be wiped completely.
794 */
795 if (!add && adapter->netdev->flags & IFF_PROMISC) {
796 reg_ndx = ixgbe_find_vlvf_entry(hw, vid);
797 if (reg_ndx < 0)
798 goto out;
799 vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(reg_ndx));
800 /* See if any other pools are set for this VLAN filter
801 * entry other than the PF.
802 */
803 if (VMDQ_P(0) < 32) {
804 bits = IXGBE_READ_REG(hw, IXGBE_VLVFB(reg_ndx * 2));
805 bits &= ~(1 << VMDQ_P(0));
806 bits |= IXGBE_READ_REG(hw,
807 IXGBE_VLVFB(reg_ndx * 2) + 1);
808 } else {
809 bits = IXGBE_READ_REG(hw,
810 IXGBE_VLVFB(reg_ndx * 2) + 1);
811 bits &= ~(1 << (VMDQ_P(0) - 32));
812 bits |= IXGBE_READ_REG(hw, IXGBE_VLVFB(reg_ndx * 2));
813 }
814
815 /* If the filter was removed then ensure PF pool bit
816 * is cleared if the PF only added itself to the pool
817 * because the PF is in promiscuous mode.
818 */
819 if ((vlvf & VLAN_VID_MASK) == vid &&
820 !test_bit(vid, adapter->active_vlans) && !bits)
821 ixgbe_set_vf_vlan(adapter, add, vid, VMDQ_P(0));
822 }
823
824out:
825
Alexander Duyck58a02be2012-07-20 08:09:17 +0000826 return err;
827}
828
829static int ixgbe_set_vf_macvlan_msg(struct ixgbe_adapter *adapter,
830 u32 *msgbuf, u32 vf)
831{
832 u8 *new_mac = ((u8 *)(&msgbuf[1]));
833 int index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
834 IXGBE_VT_MSGINFO_SHIFT;
835 int err;
836
837 if (adapter->vfinfo[vf].pf_set_mac && index > 0) {
838 e_warn(drv,
839 "VF %d requested MACVLAN filter but is administratively denied\n",
840 vf);
841 return -1;
842 }
843
844 /* An non-zero index indicates the VF is setting a filter */
845 if (index) {
846 if (!is_valid_ether_addr(new_mac)) {
847 e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
848 return -1;
849 }
850
851 /*
852 * If the VF is allowed to set MAC filters then turn off
853 * anti-spoofing to avoid false positives.
854 */
855 if (adapter->vfinfo[vf].spoofchk_enabled)
856 ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
857 }
858
859 err = ixgbe_set_vf_macvlan(adapter, vf, index, new_mac);
860 if (err == -ENOSPC)
861 e_warn(drv,
862 "VF %d has requested a MACVLAN filter but there is no space for it\n",
863 vf);
Greg Rosea3013402012-10-30 00:40:02 +0000864
865 return err < 0;
Greg Rose17367272010-01-09 02:25:48 +0000866}
867
Alexander Duyck374c65d2012-07-20 08:09:22 +0000868static int ixgbe_negotiate_vf_api(struct ixgbe_adapter *adapter,
869 u32 *msgbuf, u32 vf)
870{
871 int api = msgbuf[1];
872
873 switch (api) {
874 case ixgbe_mbox_api_10:
Alexander Duyckbffb3bc2012-07-20 08:09:37 +0000875 case ixgbe_mbox_api_11:
Alexander Duyck374c65d2012-07-20 08:09:22 +0000876 adapter->vfinfo[vf].vf_api = api;
877 return 0;
878 default:
879 break;
880 }
881
882 e_info(drv, "VF %d requested invalid api version %u\n", vf, api);
883
884 return -1;
885}
886
Alexander Duyckf591cd92012-07-20 08:09:32 +0000887static int ixgbe_get_vf_queues(struct ixgbe_adapter *adapter,
888 u32 *msgbuf, u32 vf)
889{
890 struct net_device *dev = adapter->netdev;
891 struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
892 unsigned int default_tc = 0;
893 u8 num_tcs = netdev_get_num_tc(dev);
894
895 /* verify the PF is supporting the correct APIs */
896 switch (adapter->vfinfo[vf].vf_api) {
897 case ixgbe_mbox_api_20:
898 case ixgbe_mbox_api_11:
899 break;
900 default:
901 return -1;
902 }
903
904 /* only allow 1 Tx queue for bandwidth limiting */
905 msgbuf[IXGBE_VF_TX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
906 msgbuf[IXGBE_VF_RX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask);
907
908 /* if TCs > 1 determine which TC belongs to default user priority */
909 if (num_tcs > 1)
910 default_tc = netdev_get_prio_tc_map(dev, adapter->default_up);
911
912 /* notify VF of need for VLAN tag stripping, and correct queue */
913 if (num_tcs)
914 msgbuf[IXGBE_VF_TRANS_VLAN] = num_tcs;
915 else if (adapter->vfinfo[vf].pf_vlan || adapter->vfinfo[vf].pf_qos)
916 msgbuf[IXGBE_VF_TRANS_VLAN] = 1;
917 else
918 msgbuf[IXGBE_VF_TRANS_VLAN] = 0;
919
920 /* notify VF of default queue */
921 msgbuf[IXGBE_VF_DEF_QUEUE] = default_tc;
922
923 return 0;
924}
925
Greg Rose17367272010-01-09 02:25:48 +0000926static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
927{
928 u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
Emil Tantilovc0509992011-05-07 06:49:18 +0000929 u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
Greg Rose17367272010-01-09 02:25:48 +0000930 struct ixgbe_hw *hw = &adapter->hw;
931 s32 retval;
Greg Rose17367272010-01-09 02:25:48 +0000932
933 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
934
Alexander Duyckdcaccc82012-03-28 08:03:38 +0000935 if (retval) {
Emil Tantilov849c4542010-06-03 16:53:41 +0000936 pr_err("Error receiving message from VF\n");
Alexander Duyckdcaccc82012-03-28 08:03:38 +0000937 return retval;
938 }
Greg Rose17367272010-01-09 02:25:48 +0000939
940 /* this is a message we already processed, do nothing */
941 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
942 return retval;
943
Alexander Duyckdcaccc82012-03-28 08:03:38 +0000944 /* flush the ack before we write any messages back */
945 IXGBE_WRITE_FLUSH(hw);
946
Alexander Duyck374c65d2012-07-20 08:09:22 +0000947 if (msgbuf[0] == IXGBE_VF_RESET)
948 return ixgbe_vf_reset_msg(adapter, vf);
949
Greg Rose17367272010-01-09 02:25:48 +0000950 /*
951 * until the vf completes a virtual function reset it should not be
952 * allowed to start any configuration.
953 */
Greg Rose17367272010-01-09 02:25:48 +0000954 if (!adapter->vfinfo[vf].clear_to_send) {
955 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
956 ixgbe_write_mbx(hw, msgbuf, 1, vf);
957 return retval;
958 }
959
960 switch ((msgbuf[0] & 0xFFFF)) {
961 case IXGBE_VF_SET_MAC_ADDR:
Alexander Duyck58a02be2012-07-20 08:09:17 +0000962 retval = ixgbe_set_vf_mac_addr(adapter, msgbuf, vf);
Greg Rose17367272010-01-09 02:25:48 +0000963 break;
964 case IXGBE_VF_SET_MULTICAST:
Alexander Duyck58a02be2012-07-20 08:09:17 +0000965 retval = ixgbe_set_vf_multicasts(adapter, msgbuf, vf);
966 break;
967 case IXGBE_VF_SET_VLAN:
968 retval = ixgbe_set_vf_vlan_msg(adapter, msgbuf, vf);
Greg Rose17367272010-01-09 02:25:48 +0000969 break;
970 case IXGBE_VF_SET_LPE:
Alexander Duyck872844d2012-08-15 02:10:43 +0000971 retval = ixgbe_set_vf_lpe(adapter, msgbuf, vf);
Greg Rose17367272010-01-09 02:25:48 +0000972 break;
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000973 case IXGBE_VF_SET_MACVLAN:
Alexander Duyck58a02be2012-07-20 08:09:17 +0000974 retval = ixgbe_set_vf_macvlan_msg(adapter, msgbuf, vf);
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000975 break;
Alexander Duyck374c65d2012-07-20 08:09:22 +0000976 case IXGBE_VF_API_NEGOTIATE:
977 retval = ixgbe_negotiate_vf_api(adapter, msgbuf, vf);
978 break;
Alexander Duyckf591cd92012-07-20 08:09:32 +0000979 case IXGBE_VF_GET_QUEUES:
980 retval = ixgbe_get_vf_queues(adapter, msgbuf, vf);
981 break;
Greg Rose17367272010-01-09 02:25:48 +0000982 default:
Emil Tantilov396e7992010-07-01 20:05:12 +0000983 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
Greg Rose17367272010-01-09 02:25:48 +0000984 retval = IXGBE_ERR_MBX;
985 break;
986 }
987
988 /* notify the VF of the results of what it sent us */
989 if (retval)
990 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
991 else
992 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
993
994 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
995
Alexander Duyck374c65d2012-07-20 08:09:22 +0000996 ixgbe_write_mbx(hw, msgbuf, mbx_size, vf);
Greg Rose17367272010-01-09 02:25:48 +0000997
998 return retval;
999}
1000
1001static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
1002{
1003 struct ixgbe_hw *hw = &adapter->hw;
1004 u32 msg = IXGBE_VT_MSGTYPE_NACK;
1005
1006 /* if device isn't clear to send it shouldn't be reading either */
1007 if (!adapter->vfinfo[vf].clear_to_send)
1008 ixgbe_write_mbx(hw, &msg, 1, vf);
1009}
1010
1011void ixgbe_msg_task(struct ixgbe_adapter *adapter)
1012{
1013 struct ixgbe_hw *hw = &adapter->hw;
1014 u32 vf;
1015
1016 for (vf = 0; vf < adapter->num_vfs; vf++) {
1017 /* process any reset requests */
1018 if (!ixgbe_check_for_rst(hw, vf))
1019 ixgbe_vf_reset_event(adapter, vf);
1020
1021 /* process any messages pending */
1022 if (!ixgbe_check_for_msg(hw, vf))
1023 ixgbe_rcv_msg_from_vf(adapter, vf);
1024
1025 /* process any acks */
1026 if (!ixgbe_check_for_ack(hw, vf))
1027 ixgbe_rcv_ack_from_vf(adapter, vf);
1028 }
1029}
1030
Greg Rose767081a2010-01-22 22:46:40 +00001031void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
1032{
1033 struct ixgbe_hw *hw = &adapter->hw;
1034
1035 /* disable transmit and receive for all vfs */
1036 IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
1037 IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
1038
1039 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
1040 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
1041}
1042
1043void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
1044{
1045 struct ixgbe_hw *hw = &adapter->hw;
1046 u32 ping;
1047 int i;
1048
1049 for (i = 0 ; i < adapter->num_vfs; i++) {
1050 ping = IXGBE_PF_CONTROL_MSG;
1051 if (adapter->vfinfo[i].clear_to_send)
1052 ping |= IXGBE_VT_MSGTYPE_CTS;
1053 ixgbe_write_mbx(hw, &ping, 1, i);
1054 }
1055}
1056
Greg Rose7f016482010-05-04 22:12:06 +00001057int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1058{
1059 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1060 if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
1061 return -EINVAL;
1062 adapter->vfinfo[vf].pf_set_mac = true;
1063 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
1064 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
1065 " change effective.");
1066 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
1067 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
1068 " but the PF device is not up.\n");
1069 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
1070 " attempting to use the VF device.\n");
1071 }
1072 return ixgbe_set_vf_mac(adapter, vf, mac);
1073}
1074
1075int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
1076{
1077 int err = 0;
1078 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Greg Rosea985b6c32010-11-18 03:02:52 +00001079 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose7f016482010-05-04 22:12:06 +00001080
1081 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
1082 return -EINVAL;
1083 if (vlan || qos) {
Greg Rose026ac672013-04-17 20:41:35 +00001084 if (adapter->vfinfo[vf].pf_vlan)
1085 err = ixgbe_set_vf_vlan(adapter, false,
1086 adapter->vfinfo[vf].pf_vlan,
1087 vf);
1088 if (err)
1089 goto out;
Greg Rose7f016482010-05-04 22:12:06 +00001090 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
1091 if (err)
1092 goto out;
Alexander Duyck107d3012012-10-02 00:17:03 +00001093 ixgbe_set_vmvir(adapter, vlan, qos, vf);
Greg Rosea985b6c32010-11-18 03:02:52 +00001094 ixgbe_set_vmolr(hw, vf, false);
Greg Rosede4c7f62011-09-29 05:57:33 +00001095 if (adapter->vfinfo[vf].spoofchk_enabled)
Greg Rosea1cbb15c2011-05-13 01:33:48 +00001096 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
Greg Rosede4c7f62011-09-29 05:57:33 +00001097 adapter->vfinfo[vf].vlan_count++;
Greg Rose7f016482010-05-04 22:12:06 +00001098 adapter->vfinfo[vf].pf_vlan = vlan;
1099 adapter->vfinfo[vf].pf_qos = qos;
1100 dev_info(&adapter->pdev->dev,
1101 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
1102 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
1103 dev_warn(&adapter->pdev->dev,
1104 "The VF VLAN has been set,"
1105 " but the PF device is not up.\n");
1106 dev_warn(&adapter->pdev->dev,
1107 "Bring the PF device up before"
1108 " attempting to use the VF device.\n");
1109 }
1110 } else {
1111 err = ixgbe_set_vf_vlan(adapter, false,
1112 adapter->vfinfo[vf].pf_vlan, vf);
Alexander Duyck107d3012012-10-02 00:17:03 +00001113 ixgbe_clear_vmvir(adapter, vf);
Greg Rosea985b6c32010-11-18 03:02:52 +00001114 ixgbe_set_vmolr(hw, vf, true);
1115 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
Greg Rosede4c7f62011-09-29 05:57:33 +00001116 if (adapter->vfinfo[vf].vlan_count)
1117 adapter->vfinfo[vf].vlan_count--;
Greg Rose7f016482010-05-04 22:12:06 +00001118 adapter->vfinfo[vf].pf_vlan = 0;
1119 adapter->vfinfo[vf].pf_qos = 0;
1120 }
1121out:
1122 return err;
1123}
1124
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001125static int ixgbe_link_mbps(struct ixgbe_adapter *adapter)
Lior Levyff4ab202011-03-11 02:03:07 +00001126{
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001127 switch (adapter->link_speed) {
Lior Levyff4ab202011-03-11 02:03:07 +00001128 case IXGBE_LINK_SPEED_100_FULL:
1129 return 100;
1130 case IXGBE_LINK_SPEED_1GB_FULL:
1131 return 1000;
1132 case IXGBE_LINK_SPEED_10GB_FULL:
1133 return 10000;
1134 default:
1135 return 0;
1136 }
1137}
1138
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001139static void ixgbe_set_vf_rate_limit(struct ixgbe_adapter *adapter, int vf)
Lior Levyff4ab202011-03-11 02:03:07 +00001140{
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001141 struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
1142 struct ixgbe_hw *hw = &adapter->hw;
1143 u32 bcnrc_val = 0;
1144 u16 queue, queues_per_pool;
1145 u16 tx_rate = adapter->vfinfo[vf].tx_rate;
Lior Levyff4ab202011-03-11 02:03:07 +00001146
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001147 if (tx_rate) {
1148 /* start with base link speed value */
1149 bcnrc_val = adapter->vf_rate_link_speed;
1150
Lior Levyff4ab202011-03-11 02:03:07 +00001151 /* Calculate the rate factor values to set */
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001152 bcnrc_val <<= IXGBE_RTTBCNRC_RF_INT_SHIFT;
1153 bcnrc_val /= tx_rate;
Lior Levyff4ab202011-03-11 02:03:07 +00001154
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001155 /* clear everything but the rate factor */
1156 bcnrc_val &= IXGBE_RTTBCNRC_RF_INT_MASK |
1157 IXGBE_RTTBCNRC_RF_DEC_MASK;
1158
1159 /* enable the rate scheduler */
1160 bcnrc_val |= IXGBE_RTTBCNRC_RS_ENA;
Lior Levyff4ab202011-03-11 02:03:07 +00001161 }
1162
Lior Levy7555e832011-06-25 00:09:08 -07001163 /*
1164 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
1165 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
1166 * and 0x004 otherwise.
1167 */
1168 switch (hw->mac.type) {
1169 case ixgbe_mac_82599EB:
1170 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
1171 break;
1172 case ixgbe_mac_X540:
1173 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
1174 break;
1175 default:
1176 break;
1177 }
1178
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001179 /* determine how many queues per pool based on VMDq mask */
1180 queues_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
1181
1182 /* write value for all Tx queues belonging to VF */
1183 for (queue = 0; queue < queues_per_pool; queue++) {
1184 unsigned int reg_idx = (vf * queues_per_pool) + queue;
1185
1186 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, reg_idx);
1187 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
1188 }
Lior Levyff4ab202011-03-11 02:03:07 +00001189}
1190
1191void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
1192{
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001193 int i;
Lior Levyff4ab202011-03-11 02:03:07 +00001194
1195 /* VF Tx rate limit was not set */
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001196 if (!adapter->vf_rate_link_speed)
Lior Levyff4ab202011-03-11 02:03:07 +00001197 return;
1198
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001199 if (ixgbe_link_mbps(adapter) != adapter->vf_rate_link_speed) {
Lior Levyff4ab202011-03-11 02:03:07 +00001200 adapter->vf_rate_link_speed = 0;
1201 dev_info(&adapter->pdev->dev,
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001202 "Link speed has been changed. VF Transmit rate is disabled\n");
Lior Levyff4ab202011-03-11 02:03:07 +00001203 }
1204
1205 for (i = 0; i < adapter->num_vfs; i++) {
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001206 if (!adapter->vf_rate_link_speed)
Lior Levyff4ab202011-03-11 02:03:07 +00001207 adapter->vfinfo[i].tx_rate = 0;
1208
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001209 ixgbe_set_vf_rate_limit(adapter, i);
Lior Levyff4ab202011-03-11 02:03:07 +00001210 }
1211}
1212
Greg Rose7f016482010-05-04 22:12:06 +00001213int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
1214{
Lior Levyff4ab202011-03-11 02:03:07 +00001215 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001216 int link_speed;
Lior Levyff4ab202011-03-11 02:03:07 +00001217
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001218 /* verify VF is active */
1219 if (vf >= adapter->num_vfs)
Lior Levyff4ab202011-03-11 02:03:07 +00001220 return -EINVAL;
1221
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001222 /* verify link is up */
1223 if (!adapter->link_up)
1224 return -EINVAL;
1225
1226 /* verify we are linked at 10Gbps */
1227 link_speed = ixgbe_link_mbps(adapter);
1228 if (link_speed != 10000)
1229 return -EINVAL;
1230
1231 /* rate limit cannot be less than 10Mbs or greater than link speed */
1232 if (tx_rate && ((tx_rate <= 10) || (tx_rate > link_speed)))
1233 return -EINVAL;
1234
1235 /* store values */
1236 adapter->vf_rate_link_speed = link_speed;
1237 adapter->vfinfo[vf].tx_rate = tx_rate;
1238
1239 /* update hardware configuration */
1240 ixgbe_set_vf_rate_limit(adapter, vf);
Lior Levyff4ab202011-03-11 02:03:07 +00001241
1242 return 0;
Greg Rose7f016482010-05-04 22:12:06 +00001243}
1244
Greg Rosede4c7f62011-09-29 05:57:33 +00001245int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
1246{
1247 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1248 int vf_target_reg = vf >> 3;
1249 int vf_target_shift = vf % 8;
1250 struct ixgbe_hw *hw = &adapter->hw;
1251 u32 regval;
1252
1253 adapter->vfinfo[vf].spoofchk_enabled = setting;
1254
1255 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
1256 regval &= ~(1 << vf_target_shift);
1257 regval |= (setting << vf_target_shift);
1258 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
1259
1260 if (adapter->vfinfo[vf].vlan_count) {
1261 vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
1262 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
1263 regval &= ~(1 << vf_target_shift);
1264 regval |= (setting << vf_target_shift);
1265 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
1266 }
1267
1268 return 0;
1269}
1270
Greg Rose7f016482010-05-04 22:12:06 +00001271int ixgbe_ndo_get_vf_config(struct net_device *netdev,
1272 int vf, struct ifla_vf_info *ivi)
1273{
1274 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1275 if (vf >= adapter->num_vfs)
1276 return -EINVAL;
1277 ivi->vf = vf;
1278 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
Lior Levyff4ab202011-03-11 02:03:07 +00001279 ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
Greg Rose7f016482010-05-04 22:12:06 +00001280 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
1281 ivi->qos = adapter->vfinfo[vf].pf_qos;
Greg Rosede4c7f62011-09-29 05:57:33 +00001282 ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
Greg Rose7f016482010-05-04 22:12:06 +00001283 return 0;
1284}