blob: f563625f1f49706196767ac044d7c51c6ac1135e [file] [log] [blame]
Greg Rose17367272010-01-09 02:25:48 +00001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
Don Skidmore94971822012-01-06 03:24:16 +00004 Copyright(c) 1999 - 2012 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>
38#ifdef NETIF_F_HW_VLAN_TX
39#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 Rosec6bda302011-08-24 02:37:55 +000047void ixgbe_enable_sriov(struct ixgbe_adapter *adapter,
48 const struct ixgbe_info *ii)
49{
50 struct ixgbe_hw *hw = &adapter->hw;
Greg Rosec6bda302011-08-24 02:37:55 +000051 int num_vf_macvlans, i;
52 struct vf_macvlans *mv_list;
53 int pre_existing_vfs = 0;
54
Alexander Duyck92971272012-05-23 02:58:40 +000055 pre_existing_vfs = pci_num_vf(adapter->pdev);
Greg Rosec6bda302011-08-24 02:37:55 +000056 if (!pre_existing_vfs && !adapter->num_vfs)
57 return;
58
59 /* If there are pre-existing VFs then we have to force
60 * use of that many because they were not deleted the last
61 * time someone removed the PF driver. That would have
62 * been because they were allocated to guest VMs and can't
63 * be removed. Go ahead and just re-enable the old amount.
64 * If the user wants to change the number of VFs they can
65 * use ethtool while making sure no VFs are allocated to
66 * guest VMs... i.e. the right way.
67 */
68 if (pre_existing_vfs) {
69 adapter->num_vfs = pre_existing_vfs;
70 dev_warn(&adapter->pdev->dev, "Virtual Functions already "
71 "enabled for this device - Please reload all "
72 "VF drivers to avoid spoofed packet errors\n");
73 } else {
Alexander Duyck99d74482012-05-09 08:09:25 +000074 int err;
75 /*
76 * The 82599 supports up to 64 VFs per physical function
77 * but this implementation limits allocation to 63 so that
78 * basic networking resources are still available to the
79 * physical function. If the user requests greater thn
80 * 63 VFs then it is an error - reset to default of zero.
81 */
82 adapter->num_vfs = min_t(unsigned int, adapter->num_vfs, 63);
83
Greg Rosec6bda302011-08-24 02:37:55 +000084 err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
Alexander Duyck73079ea2012-07-14 06:48:49 +000085 if (err) {
86 e_err(probe, "Failed to enable PCI sriov: %d\n", err);
Alexander Duyck99d74482012-05-09 08:09:25 +000087 adapter->num_vfs = 0;
88 return;
Alexander Duyck73079ea2012-07-14 06:48:49 +000089 }
Greg Rosec6bda302011-08-24 02:37:55 +000090 }
Greg Rosec6bda302011-08-24 02:37:55 +000091
Alexander Duyck73079ea2012-07-14 06:48:49 +000092 adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
Greg Rosec6bda302011-08-24 02:37:55 +000093 e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);
94
Alexander Duyck73079ea2012-07-14 06:48:49 +000095 /* Enable VMDq flag so device will be set in VM mode */
96 adapter->flags |= IXGBE_FLAG_VMDQ_ENABLED;
97 if (!adapter->ring_feature[RING_F_VMDQ].limit)
98 adapter->ring_feature[RING_F_VMDQ].limit = 1;
99 adapter->ring_feature[RING_F_VMDQ].offset = adapter->num_vfs;
100
Greg Rosec6bda302011-08-24 02:37:55 +0000101 num_vf_macvlans = hw->mac.num_rar_entries -
102 (IXGBE_MAX_PF_MACVLANS + 1 + adapter->num_vfs);
103
104 adapter->mv_list = mv_list = kcalloc(num_vf_macvlans,
105 sizeof(struct vf_macvlans),
106 GFP_KERNEL);
107 if (mv_list) {
108 /* Initialize list of VF macvlans */
109 INIT_LIST_HEAD(&adapter->vf_mvs.l);
110 for (i = 0; i < num_vf_macvlans; i++) {
111 mv_list->vf = -1;
112 mv_list->free = true;
113 mv_list->rar_entry = hw->mac.num_rar_entries -
114 (i + adapter->num_vfs + 1);
115 list_add(&mv_list->l, &adapter->vf_mvs.l);
116 mv_list++;
117 }
118 }
119
120 /* If call to enable VFs succeeded then allocate memory
121 * for per VF control structures.
122 */
123 adapter->vfinfo =
124 kcalloc(adapter->num_vfs,
125 sizeof(struct vf_data_storage), GFP_KERNEL);
126 if (adapter->vfinfo) {
127 /* Now that we're sure SR-IOV is enabled
128 * and memory allocated set up the mailbox parameters
129 */
130 ixgbe_init_mbx_params_pf(hw);
Alexander Duyck73079ea2012-07-14 06:48:49 +0000131 memcpy(&hw->mbx.ops, ii->mbx_ops, sizeof(hw->mbx.ops));
132
133 /* limit trafffic classes based on VFs enabled */
134 if ((adapter->hw.mac.type == ixgbe_mac_82599EB) &&
135 (adapter->num_vfs < 16)) {
136 adapter->dcb_cfg.num_tcs.pg_tcs = MAX_TRAFFIC_CLASS;
137 adapter->dcb_cfg.num_tcs.pfc_tcs = MAX_TRAFFIC_CLASS;
138 } else if (adapter->num_vfs < 32) {
139 adapter->dcb_cfg.num_tcs.pg_tcs = 4;
140 adapter->dcb_cfg.num_tcs.pfc_tcs = 4;
141 } else {
142 adapter->dcb_cfg.num_tcs.pg_tcs = 1;
143 adapter->dcb_cfg.num_tcs.pfc_tcs = 1;
144 }
145
146 /* We do not support RSS w/ SR-IOV */
147 adapter->ring_feature[RING_F_RSS].limit = 1;
Greg Rosec6bda302011-08-24 02:37:55 +0000148
149 /* Disable RSC when in SR-IOV mode */
150 adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
151 IXGBE_FLAG2_RSC_ENABLED);
Alexander Duyck73079ea2012-07-14 06:48:49 +0000152
Alexander Duyck73079ea2012-07-14 06:48:49 +0000153 /* enable spoof checking for all VFs */
Greg Rosede4c7f62011-09-29 05:57:33 +0000154 for (i = 0; i < adapter->num_vfs; i++)
155 adapter->vfinfo[i].spoofchk_enabled = true;
Greg Rosec6bda302011-08-24 02:37:55 +0000156 return;
157 }
158
159 /* Oh oh */
160 e_err(probe, "Unable to allocate memory for VF Data Storage - "
161 "SRIOV disabled\n");
Alexander Duyck99d74482012-05-09 08:09:25 +0000162 ixgbe_disable_sriov(adapter);
Greg Rosec6bda302011-08-24 02:37:55 +0000163}
Greg Rosec6bda302011-08-24 02:37:55 +0000164
Alexander Duyck92971272012-05-23 02:58:40 +0000165static bool ixgbe_vfs_are_assigned(struct ixgbe_adapter *adapter)
166{
167 struct pci_dev *pdev = adapter->pdev;
168 struct pci_dev *vfdev;
169 int dev_id;
170
171 switch (adapter->hw.mac.type) {
172 case ixgbe_mac_82599EB:
173 dev_id = IXGBE_DEV_ID_82599_VF;
174 break;
175 case ixgbe_mac_X540:
176 dev_id = IXGBE_DEV_ID_X540_VF;
177 break;
178 default:
179 return false;
180 }
181
182 /* loop through all the VFs to see if we own any that are assigned */
183 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
184 while (vfdev) {
185 /* if we don't own it we don't care */
186 if (vfdev->is_virtfn && vfdev->physfn == pdev) {
187 /* if it is assigned we cannot release it */
188 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
189 return true;
190 }
191
192 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, vfdev);
193 }
194
195 return false;
196}
197
198#endif /* #ifdef CONFIG_PCI_IOV */
Greg Rosec6bda302011-08-24 02:37:55 +0000199void ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
200{
201 struct ixgbe_hw *hw = &adapter->hw;
Greg Rosec6bda302011-08-24 02:37:55 +0000202 u32 gpie;
203 u32 vmdctl;
Greg Rosec6bda302011-08-24 02:37:55 +0000204
Alexander Duyckd773d132012-05-05 05:32:26 +0000205 /* set num VFs to 0 to prevent access to vfinfo */
206 adapter->num_vfs = 0;
207
208 /* free VF control structures */
209 kfree(adapter->vfinfo);
210 adapter->vfinfo = NULL;
211
212 /* free macvlan list */
213 kfree(adapter->mv_list);
214 adapter->mv_list = NULL;
215
Alexander Duyck99d74482012-05-09 08:09:25 +0000216 /* if SR-IOV is already disabled then there is nothing to do */
217 if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED))
218 return;
219
Greg Rosec6bda302011-08-24 02:37:55 +0000220#ifdef CONFIG_PCI_IOV
Alexander Duyck92971272012-05-23 02:58:40 +0000221 /*
222 * If our VFs are assigned we cannot shut down SR-IOV
223 * without causing issues, so just leave the hardware
224 * available but disabled
225 */
226 if (ixgbe_vfs_are_assigned(adapter)) {
227 e_dev_warn("Unloading driver while VFs are assigned - VFs will not be deallocated\n");
228 return;
David S. Millerd47e12d2012-07-22 12:36:41 -0700229 }
Greg Rosec6bda302011-08-24 02:37:55 +0000230 /* disable iov and allow time for transactions to clear */
231 pci_disable_sriov(adapter->pdev);
232#endif
233
234 /* turn off device IOV mode */
Alexander Duyck73079ea2012-07-14 06:48:49 +0000235 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, 0);
Greg Rosec6bda302011-08-24 02:37:55 +0000236 gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
237 gpie &= ~IXGBE_GPIE_VTMODE_MASK;
238 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
239
240 /* set default pool back to 0 */
241 vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
242 vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
243 IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
244 IXGBE_WRITE_FLUSH(hw);
245
Alexander Duyck1d9c0bf2012-05-05 05:32:21 +0000246 /* Disable VMDq flag so device will be set in VM mode */
247 if (adapter->ring_feature[RING_F_VMDQ].limit == 1)
248 adapter->flags &= ~IXGBE_FLAG_VMDQ_ENABLED;
249 adapter->ring_feature[RING_F_VMDQ].offset = 0;
250
Greg Rosec6bda302011-08-24 02:37:55 +0000251 /* take a breather then clean up driver data */
252 msleep(100);
253
Greg Rosec6bda302011-08-24 02:37:55 +0000254 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
255}
256
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000257static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
Alexander Duyck58a02be2012-07-20 08:09:17 +0000258 u32 *msgbuf, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000259{
Alexander Duyck58a02be2012-07-20 08:09:17 +0000260 int entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
261 >> IXGBE_VT_MSGINFO_SHIFT;
262 u16 *hash_list = (u16 *)&msgbuf[1];
Greg Rose17367272010-01-09 02:25:48 +0000263 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
Greg Rose8a07a222010-05-05 19:57:30 +0000264 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose17367272010-01-09 02:25:48 +0000265 int i;
Greg Rose8a07a222010-05-05 19:57:30 +0000266 u32 vector_bit;
267 u32 vector_reg;
268 u32 mta_reg;
Greg Rose17367272010-01-09 02:25:48 +0000269
270 /* only so many hash values supported */
271 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
272
273 /*
274 * salt away the number of multi cast addresses assigned
275 * to this VF for later use to restore when the PF multi cast
276 * list changes
277 */
278 vfinfo->num_vf_mc_hashes = entries;
279
280 /*
281 * VFs are limited to using the MTA hash table for their multicast
282 * addresses
283 */
284 for (i = 0; i < entries; i++) {
Joe Perchese81a1ba2010-11-14 17:04:33 +0000285 vfinfo->vf_mc_hashes[i] = hash_list[i];
Greg Rose17367272010-01-09 02:25:48 +0000286 }
287
Greg Rose8a07a222010-05-05 19:57:30 +0000288 for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
289 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
290 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
291 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
292 mta_reg |= (1 << vector_bit);
293 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
294 }
Greg Rose17367272010-01-09 02:25:48 +0000295
296 return 0;
297}
298
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000299static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
300{
301 struct ixgbe_hw *hw = &adapter->hw;
302 struct list_head *pos;
303 struct vf_macvlans *entry;
304
305 list_for_each(pos, &adapter->vf_mvs.l) {
306 entry = list_entry(pos, struct vf_macvlans, l);
Joe Perches23677ce2012-02-09 11:17:23 +0000307 if (!entry->free)
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000308 hw->mac.ops.set_rar(hw, entry->rar_entry,
309 entry->vf_macvlan,
310 entry->vf, IXGBE_RAH_AV);
311 }
312}
313
Greg Rose17367272010-01-09 02:25:48 +0000314void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
315{
316 struct ixgbe_hw *hw = &adapter->hw;
317 struct vf_data_storage *vfinfo;
318 int i, j;
319 u32 vector_bit;
320 u32 vector_reg;
321 u32 mta_reg;
322
323 for (i = 0; i < adapter->num_vfs; i++) {
324 vfinfo = &adapter->vfinfo[i];
325 for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
326 hw->addr_ctrl.mta_in_use++;
327 vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
328 vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
329 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
330 mta_reg |= (1 << vector_bit);
331 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
332 }
333 }
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000334
335 /* Restore any VF macvlans */
336 ixgbe_restore_vf_macvlans(adapter);
Greg Rose17367272010-01-09 02:25:48 +0000337}
338
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000339static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
340 u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000341{
Alexander Duyckb35d4d42012-05-23 05:39:25 +0000342 /* VLAN 0 is a special case, don't allow it to be removed */
343 if (!vid && !add)
344 return 0;
345
Greg Rose17367272010-01-09 02:25:48 +0000346 return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
347}
348
Alexander Duyck872844d2012-08-15 02:10:43 +0000349static s32 ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
Greg Rosee9f98072011-01-26 01:06:07 +0000350{
351 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck872844d2012-08-15 02:10:43 +0000352 int max_frame = msgbuf[1];
Greg Rosee9f98072011-01-26 01:06:07 +0000353 u32 max_frs;
Greg Rosee9f98072011-01-26 01:06:07 +0000354
Alexander Duyck872844d2012-08-15 02:10:43 +0000355 /*
356 * For 82599EB we have to keep all PFs and VFs operating with
357 * the same max_frame value in order to avoid sending an oversize
358 * frame to a VF. In order to guarantee this is handled correctly
359 * for all cases we have several special exceptions to take into
360 * account before we can enable the VF for receive
361 */
362 if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
363 struct net_device *dev = adapter->netdev;
364 int pf_max_frame = dev->mtu + ETH_HLEN;
365 u32 reg_offset, vf_shift, vfre;
366 s32 err = 0;
Greg Rosee9f98072011-01-26 01:06:07 +0000367
Alexander Duyck872844d2012-08-15 02:10:43 +0000368#ifdef CONFIG_FCOE
369 if (dev->features & NETIF_F_FCOE_MTU)
370 pf_max_frame = max_t(int, pf_max_frame,
371 IXGBE_FCOE_JUMBO_FRAME_SIZE);
372
373#endif /* CONFIG_FCOE */
374 /*
375 * If the PF or VF are running w/ jumbo frames enabled we
376 * need to shut down the VF Rx path as we cannot support
377 * jumbo frames on legacy VFs
378 */
379 if ((pf_max_frame > ETH_FRAME_LEN) ||
380 (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)))
381 err = -EINVAL;
382
383 /* determine VF receive enable location */
384 vf_shift = vf % 32;
385 reg_offset = vf / 32;
386
387 /* enable or disable receive depending on error */
388 vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
389 if (err)
390 vfre &= ~(1 << vf_shift);
391 else
392 vfre |= 1 << vf_shift;
393 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), vfre);
394
395 if (err) {
396 e_err(drv, "VF max_frame %d out of range\n", max_frame);
397 return err;
398 }
Greg Rosee9f98072011-01-26 01:06:07 +0000399 }
400
Alexander Duyck872844d2012-08-15 02:10:43 +0000401 /* MTU < 68 is an error and causes problems on some kernels */
402 if (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE) {
403 e_err(drv, "VF max_frame %d out of range\n", max_frame);
404 return -EINVAL;
405 }
406
407 /* pull current max frame size from hardware */
408 max_frs = IXGBE_READ_REG(hw, IXGBE_MAXFRS);
409 max_frs &= IXGBE_MHADD_MFS_MASK;
410 max_frs >>= IXGBE_MHADD_MFS_SHIFT;
411
412 if (max_frs < max_frame) {
413 max_frs = max_frame << IXGBE_MHADD_MFS_SHIFT;
Greg Rosee9f98072011-01-26 01:06:07 +0000414 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
415 }
416
Alexander Duyck872844d2012-08-15 02:10:43 +0000417 e_info(hw, "VF requests change max MTU to %d\n", max_frame);
418
419 return 0;
Greg Rosee9f98072011-01-26 01:06:07 +0000420}
421
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000422static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
Greg Rose17367272010-01-09 02:25:48 +0000423{
424 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
Greg Rosef0412772010-05-04 22:11:46 +0000425 vmolr |= (IXGBE_VMOLR_ROMPE |
Greg Rose17367272010-01-09 02:25:48 +0000426 IXGBE_VMOLR_BAM);
Greg Rosef0412772010-05-04 22:11:46 +0000427 if (aupe)
428 vmolr |= IXGBE_VMOLR_AUPE;
429 else
430 vmolr &= ~IXGBE_VMOLR_AUPE;
Greg Rose17367272010-01-09 02:25:48 +0000431 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
432}
433
Greg Rose7f016482010-05-04 22:12:06 +0000434static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
435{
436 struct ixgbe_hw *hw = &adapter->hw;
437
438 if (vid)
439 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
440 (vid | IXGBE_VMVIR_VLANA_DEFAULT));
441 else
442 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
443}
444
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000445static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000446{
447 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck28500622010-06-15 09:25:48 +0000448 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
Greg Rose17367272010-01-09 02:25:48 +0000449
450 /* reset offloads to defaults */
Greg Rose7f016482010-05-04 22:12:06 +0000451 if (adapter->vfinfo[vf].pf_vlan) {
452 ixgbe_set_vf_vlan(adapter, true,
453 adapter->vfinfo[vf].pf_vlan, vf);
454 ixgbe_set_vmvir(adapter,
455 (adapter->vfinfo[vf].pf_vlan |
456 (adapter->vfinfo[vf].pf_qos <<
457 VLAN_PRIO_SHIFT)), vf);
458 ixgbe_set_vmolr(hw, vf, false);
459 } else {
Alexander Duyckb35d4d42012-05-23 05:39:25 +0000460 ixgbe_set_vf_vlan(adapter, true, 0, vf);
Greg Rose7f016482010-05-04 22:12:06 +0000461 ixgbe_set_vmvir(adapter, 0, vf);
462 ixgbe_set_vmolr(hw, vf, true);
463 }
Greg Rose17367272010-01-09 02:25:48 +0000464
465 /* reset multicast table array for vf */
466 adapter->vfinfo[vf].num_vf_mc_hashes = 0;
467
468 /* Flush and reset the mta with the new values */
469 ixgbe_set_rx_mode(adapter->netdev);
470
Alexander Duyck28500622010-06-15 09:25:48 +0000471 hw->mac.ops.clear_rar(hw, rar_entry);
Alexander Duyck374c65d2012-07-20 08:09:22 +0000472
473 /* reset VF api back to unknown */
474 adapter->vfinfo[vf].vf_api = ixgbe_mbox_api_10;
Greg Rose17367272010-01-09 02:25:48 +0000475}
476
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000477static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
478 int vf, unsigned char *mac_addr)
Greg Rose17367272010-01-09 02:25:48 +0000479{
480 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck28500622010-06-15 09:25:48 +0000481 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
Greg Rose17367272010-01-09 02:25:48 +0000482
483 memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
Alexander Duyck28500622010-06-15 09:25:48 +0000484 hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
Greg Rose17367272010-01-09 02:25:48 +0000485
486 return 0;
487}
488
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000489static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
490 int vf, int index, unsigned char *mac_addr)
491{
492 struct ixgbe_hw *hw = &adapter->hw;
493 struct list_head *pos;
494 struct vf_macvlans *entry;
495
496 if (index <= 1) {
497 list_for_each(pos, &adapter->vf_mvs.l) {
498 entry = list_entry(pos, struct vf_macvlans, l);
499 if (entry->vf == vf) {
500 entry->vf = -1;
501 entry->free = true;
502 entry->is_macvlan = false;
503 hw->mac.ops.clear_rar(hw, entry->rar_entry);
504 }
505 }
506 }
507
508 /*
509 * If index was zero then we were asked to clear the uc list
510 * for the VF. We're done.
511 */
512 if (!index)
513 return 0;
514
515 entry = NULL;
516
517 list_for_each(pos, &adapter->vf_mvs.l) {
518 entry = list_entry(pos, struct vf_macvlans, l);
519 if (entry->free)
520 break;
521 }
522
523 /*
524 * If we traversed the entire list and didn't find a free entry
525 * then we're out of space on the RAR table. Also entry may
526 * be NULL because the original memory allocation for the list
527 * failed, which is not fatal but does mean we can't support
528 * VF requests for MACVLAN because we couldn't allocate
529 * memory for the list management required.
530 */
531 if (!entry || !entry->free)
532 return -ENOSPC;
533
534 entry->free = false;
535 entry->is_macvlan = true;
536 entry->vf = vf;
537 memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
538
539 hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
540
541 return 0;
542}
543
Greg Rose17367272010-01-09 02:25:48 +0000544int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
545{
546 unsigned char vf_mac_addr[6];
Alexander Duyckc60fbb02010-11-16 19:26:54 -0800547 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
Greg Rose17367272010-01-09 02:25:48 +0000548 unsigned int vfn = (event_mask & 0x3f);
549
550 bool enable = ((event_mask & 0x10000000U) != 0);
551
552 if (enable) {
Joe Perches7efd26d2012-07-12 19:33:06 +0000553 eth_random_addr(vf_mac_addr);
Emil Tantilov396e7992010-07-01 20:05:12 +0000554 e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
555 vfn, vf_mac_addr);
Greg Rose17367272010-01-09 02:25:48 +0000556 /*
557 * Store away the VF "permananet" MAC address, it will ask
558 * for it later.
559 */
560 memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
561 }
562
563 return 0;
564}
565
Alexander Duyck58a02be2012-07-20 08:09:17 +0000566static int ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000567{
568 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck58a02be2012-07-20 08:09:17 +0000569 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
570 u32 reg, msgbuf[4];
Greg Rose17367272010-01-09 02:25:48 +0000571 u32 reg_offset, vf_shift;
Alexander Duyck58a02be2012-07-20 08:09:17 +0000572 u8 *addr = (u8 *)(&msgbuf[1]);
573
574 e_info(probe, "VF Reset msg received from vf %d\n", vf);
575
576 /* reset the filters for the device */
577 ixgbe_vf_reset_event(adapter, vf);
578
579 /* set vf mac address */
580 ixgbe_set_vf_mac(adapter, vf, vf_mac);
Greg Rose17367272010-01-09 02:25:48 +0000581
582 vf_shift = vf % 32;
583 reg_offset = vf / 32;
584
Alexander Duyck58a02be2012-07-20 08:09:17 +0000585 /* enable transmit for vf */
Greg Rose17367272010-01-09 02:25:48 +0000586 reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
Alexander Duyck872844d2012-08-15 02:10:43 +0000587 reg |= 1 << vf_shift;
Greg Rose17367272010-01-09 02:25:48 +0000588 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
589
Alexander Duyck58a02be2012-07-20 08:09:17 +0000590 /* enable receive for vf */
Greg Rose17367272010-01-09 02:25:48 +0000591 reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
Alexander Duyck872844d2012-08-15 02:10:43 +0000592 reg |= 1 << vf_shift;
593 /*
594 * The 82599 cannot support a mix of jumbo and non-jumbo PF/VFs.
595 * For more info take a look at ixgbe_set_vf_lpe
596 */
597 if (adapter->hw.mac.type == ixgbe_mac_82599EB) {
598 struct net_device *dev = adapter->netdev;
599 int pf_max_frame = dev->mtu + ETH_HLEN;
600
601#ifdef CONFIG_FCOE
602 if (dev->features & NETIF_F_FCOE_MTU)
603 pf_max_frame = max_t(int, pf_max_frame,
604 IXGBE_FCOE_JUMBO_FRAME_SIZE);
605
606#endif /* CONFIG_FCOE */
607 if (pf_max_frame > ETH_FRAME_LEN)
608 reg &= ~(1 << vf_shift);
609 }
Greg Rose17367272010-01-09 02:25:48 +0000610 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
611
Alexander Duyck58a02be2012-07-20 08:09:17 +0000612 /* enable VF mailbox for further messages */
613 adapter->vfinfo[vf].clear_to_send = true;
614
Greg Rosea985b6c32010-11-18 03:02:52 +0000615 /* Enable counting of spoofed packets in the SSVPC register */
616 reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
617 reg |= (1 << vf_shift);
618 IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
619
Alexander Duyck58a02be2012-07-20 08:09:17 +0000620 /* reply to reset with ack and vf mac address */
621 msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
622 memcpy(addr, vf_mac, ETH_ALEN);
623
624 /*
625 * Piggyback the multicast filter type so VF can compute the
626 * correct vectors
627 */
628 msgbuf[3] = hw->mac.mc_filter_type;
629 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
630
631 return 0;
632}
633
634static int ixgbe_set_vf_mac_addr(struct ixgbe_adapter *adapter,
635 u32 *msgbuf, u32 vf)
636{
637 u8 *new_mac = ((u8 *)(&msgbuf[1]));
638
639 if (!is_valid_ether_addr(new_mac)) {
640 e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
641 return -1;
642 }
643
644 if (adapter->vfinfo[vf].pf_set_mac &&
645 memcmp(adapter->vfinfo[vf].vf_mac_addresses, new_mac,
646 ETH_ALEN)) {
647 e_warn(drv,
648 "VF %d attempted to override administratively set MAC address\n"
649 "Reload the VF driver to resume operations\n",
650 vf);
651 return -1;
652 }
653
654 return ixgbe_set_vf_mac(adapter, vf, new_mac);
655}
656
657static int ixgbe_set_vf_vlan_msg(struct ixgbe_adapter *adapter,
658 u32 *msgbuf, u32 vf)
659{
660 struct ixgbe_hw *hw = &adapter->hw;
661 int add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >> IXGBE_VT_MSGINFO_SHIFT;
662 int vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
663 int err;
664
665 if (adapter->vfinfo[vf].pf_vlan) {
666 e_warn(drv,
667 "VF %d attempted to override administratively set VLAN configuration\n"
668 "Reload the VF driver to resume operations\n",
669 vf);
670 return -1;
671 }
672
673 if (add)
674 adapter->vfinfo[vf].vlan_count++;
675 else if (adapter->vfinfo[vf].vlan_count)
676 adapter->vfinfo[vf].vlan_count--;
677
678 err = ixgbe_set_vf_vlan(adapter, add, vid, vf);
679 if (!err && adapter->vfinfo[vf].spoofchk_enabled)
680 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
681
682 return err;
683}
684
685static int ixgbe_set_vf_macvlan_msg(struct ixgbe_adapter *adapter,
686 u32 *msgbuf, u32 vf)
687{
688 u8 *new_mac = ((u8 *)(&msgbuf[1]));
689 int index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
690 IXGBE_VT_MSGINFO_SHIFT;
691 int err;
692
693 if (adapter->vfinfo[vf].pf_set_mac && index > 0) {
694 e_warn(drv,
695 "VF %d requested MACVLAN filter but is administratively denied\n",
696 vf);
697 return -1;
698 }
699
700 /* An non-zero index indicates the VF is setting a filter */
701 if (index) {
702 if (!is_valid_ether_addr(new_mac)) {
703 e_warn(drv, "VF %d attempted to set invalid mac\n", vf);
704 return -1;
705 }
706
707 /*
708 * If the VF is allowed to set MAC filters then turn off
709 * anti-spoofing to avoid false positives.
710 */
711 if (adapter->vfinfo[vf].spoofchk_enabled)
712 ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
713 }
714
715 err = ixgbe_set_vf_macvlan(adapter, vf, index, new_mac);
716 if (err == -ENOSPC)
717 e_warn(drv,
718 "VF %d has requested a MACVLAN filter but there is no space for it\n",
719 vf);
720 return err;
Greg Rose17367272010-01-09 02:25:48 +0000721}
722
Alexander Duyck374c65d2012-07-20 08:09:22 +0000723static int ixgbe_negotiate_vf_api(struct ixgbe_adapter *adapter,
724 u32 *msgbuf, u32 vf)
725{
726 int api = msgbuf[1];
727
728 switch (api) {
729 case ixgbe_mbox_api_10:
730 adapter->vfinfo[vf].vf_api = api;
731 return 0;
732 default:
733 break;
734 }
735
736 e_info(drv, "VF %d requested invalid api version %u\n", vf, api);
737
738 return -1;
739}
740
Greg Rose17367272010-01-09 02:25:48 +0000741static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
742{
743 u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
Emil Tantilovc0509992011-05-07 06:49:18 +0000744 u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
Greg Rose17367272010-01-09 02:25:48 +0000745 struct ixgbe_hw *hw = &adapter->hw;
746 s32 retval;
Greg Rose17367272010-01-09 02:25:48 +0000747
748 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
749
Alexander Duyckdcaccc82012-03-28 08:03:38 +0000750 if (retval) {
Emil Tantilov849c4542010-06-03 16:53:41 +0000751 pr_err("Error receiving message from VF\n");
Alexander Duyckdcaccc82012-03-28 08:03:38 +0000752 return retval;
753 }
Greg Rose17367272010-01-09 02:25:48 +0000754
755 /* this is a message we already processed, do nothing */
756 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
757 return retval;
758
Alexander Duyckdcaccc82012-03-28 08:03:38 +0000759 /* flush the ack before we write any messages back */
760 IXGBE_WRITE_FLUSH(hw);
761
Alexander Duyck374c65d2012-07-20 08:09:22 +0000762 if (msgbuf[0] == IXGBE_VF_RESET)
763 return ixgbe_vf_reset_msg(adapter, vf);
764
Greg Rose17367272010-01-09 02:25:48 +0000765 /*
766 * until the vf completes a virtual function reset it should not be
767 * allowed to start any configuration.
768 */
Greg Rose17367272010-01-09 02:25:48 +0000769 if (!adapter->vfinfo[vf].clear_to_send) {
770 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
771 ixgbe_write_mbx(hw, msgbuf, 1, vf);
772 return retval;
773 }
774
775 switch ((msgbuf[0] & 0xFFFF)) {
776 case IXGBE_VF_SET_MAC_ADDR:
Alexander Duyck58a02be2012-07-20 08:09:17 +0000777 retval = ixgbe_set_vf_mac_addr(adapter, msgbuf, vf);
Greg Rose17367272010-01-09 02:25:48 +0000778 break;
779 case IXGBE_VF_SET_MULTICAST:
Alexander Duyck58a02be2012-07-20 08:09:17 +0000780 retval = ixgbe_set_vf_multicasts(adapter, msgbuf, vf);
781 break;
782 case IXGBE_VF_SET_VLAN:
783 retval = ixgbe_set_vf_vlan_msg(adapter, msgbuf, vf);
Greg Rose17367272010-01-09 02:25:48 +0000784 break;
785 case IXGBE_VF_SET_LPE:
Alexander Duyck872844d2012-08-15 02:10:43 +0000786 retval = ixgbe_set_vf_lpe(adapter, msgbuf, vf);
Greg Rose17367272010-01-09 02:25:48 +0000787 break;
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000788 case IXGBE_VF_SET_MACVLAN:
Alexander Duyck58a02be2012-07-20 08:09:17 +0000789 retval = ixgbe_set_vf_macvlan_msg(adapter, msgbuf, vf);
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000790 break;
Alexander Duyck374c65d2012-07-20 08:09:22 +0000791 case IXGBE_VF_API_NEGOTIATE:
792 retval = ixgbe_negotiate_vf_api(adapter, msgbuf, vf);
793 break;
Greg Rose17367272010-01-09 02:25:48 +0000794 default:
Emil Tantilov396e7992010-07-01 20:05:12 +0000795 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
Greg Rose17367272010-01-09 02:25:48 +0000796 retval = IXGBE_ERR_MBX;
797 break;
798 }
799
800 /* notify the VF of the results of what it sent us */
801 if (retval)
802 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
803 else
804 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
805
806 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
807
Alexander Duyck374c65d2012-07-20 08:09:22 +0000808 ixgbe_write_mbx(hw, msgbuf, mbx_size, vf);
Greg Rose17367272010-01-09 02:25:48 +0000809
810 return retval;
811}
812
813static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
814{
815 struct ixgbe_hw *hw = &adapter->hw;
816 u32 msg = IXGBE_VT_MSGTYPE_NACK;
817
818 /* if device isn't clear to send it shouldn't be reading either */
819 if (!adapter->vfinfo[vf].clear_to_send)
820 ixgbe_write_mbx(hw, &msg, 1, vf);
821}
822
823void ixgbe_msg_task(struct ixgbe_adapter *adapter)
824{
825 struct ixgbe_hw *hw = &adapter->hw;
826 u32 vf;
827
828 for (vf = 0; vf < adapter->num_vfs; vf++) {
829 /* process any reset requests */
830 if (!ixgbe_check_for_rst(hw, vf))
831 ixgbe_vf_reset_event(adapter, vf);
832
833 /* process any messages pending */
834 if (!ixgbe_check_for_msg(hw, vf))
835 ixgbe_rcv_msg_from_vf(adapter, vf);
836
837 /* process any acks */
838 if (!ixgbe_check_for_ack(hw, vf))
839 ixgbe_rcv_ack_from_vf(adapter, vf);
840 }
841}
842
Greg Rose767081a2010-01-22 22:46:40 +0000843void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
844{
845 struct ixgbe_hw *hw = &adapter->hw;
846
847 /* disable transmit and receive for all vfs */
848 IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
849 IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
850
851 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
852 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
853}
854
855void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
856{
857 struct ixgbe_hw *hw = &adapter->hw;
858 u32 ping;
859 int i;
860
861 for (i = 0 ; i < adapter->num_vfs; i++) {
862 ping = IXGBE_PF_CONTROL_MSG;
863 if (adapter->vfinfo[i].clear_to_send)
864 ping |= IXGBE_VT_MSGTYPE_CTS;
865 ixgbe_write_mbx(hw, &ping, 1, i);
866 }
867}
868
Greg Rose7f016482010-05-04 22:12:06 +0000869int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
870{
871 struct ixgbe_adapter *adapter = netdev_priv(netdev);
872 if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
873 return -EINVAL;
874 adapter->vfinfo[vf].pf_set_mac = true;
875 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
876 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
877 " change effective.");
878 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
879 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
880 " but the PF device is not up.\n");
881 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
882 " attempting to use the VF device.\n");
883 }
884 return ixgbe_set_vf_mac(adapter, vf, mac);
885}
886
887int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
888{
889 int err = 0;
890 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Greg Rosea985b6c32010-11-18 03:02:52 +0000891 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose7f016482010-05-04 22:12:06 +0000892
893 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
894 return -EINVAL;
895 if (vlan || qos) {
896 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
897 if (err)
898 goto out;
899 ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
Greg Rosea985b6c32010-11-18 03:02:52 +0000900 ixgbe_set_vmolr(hw, vf, false);
Greg Rosede4c7f62011-09-29 05:57:33 +0000901 if (adapter->vfinfo[vf].spoofchk_enabled)
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000902 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
Greg Rosede4c7f62011-09-29 05:57:33 +0000903 adapter->vfinfo[vf].vlan_count++;
Greg Rose7f016482010-05-04 22:12:06 +0000904 adapter->vfinfo[vf].pf_vlan = vlan;
905 adapter->vfinfo[vf].pf_qos = qos;
906 dev_info(&adapter->pdev->dev,
907 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
908 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
909 dev_warn(&adapter->pdev->dev,
910 "The VF VLAN has been set,"
911 " but the PF device is not up.\n");
912 dev_warn(&adapter->pdev->dev,
913 "Bring the PF device up before"
914 " attempting to use the VF device.\n");
915 }
916 } else {
917 err = ixgbe_set_vf_vlan(adapter, false,
918 adapter->vfinfo[vf].pf_vlan, vf);
919 ixgbe_set_vmvir(adapter, vlan, vf);
Greg Rosea985b6c32010-11-18 03:02:52 +0000920 ixgbe_set_vmolr(hw, vf, true);
921 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
Greg Rosede4c7f62011-09-29 05:57:33 +0000922 if (adapter->vfinfo[vf].vlan_count)
923 adapter->vfinfo[vf].vlan_count--;
Greg Rose7f016482010-05-04 22:12:06 +0000924 adapter->vfinfo[vf].pf_vlan = 0;
925 adapter->vfinfo[vf].pf_qos = 0;
926 }
927out:
928 return err;
929}
930
Alexander Duyck9f66d3e2012-07-20 08:09:06 +0000931static int ixgbe_link_mbps(struct ixgbe_adapter *adapter)
Lior Levyff4ab202011-03-11 02:03:07 +0000932{
Alexander Duyck9f66d3e2012-07-20 08:09:06 +0000933 switch (adapter->link_speed) {
Lior Levyff4ab202011-03-11 02:03:07 +0000934 case IXGBE_LINK_SPEED_100_FULL:
935 return 100;
936 case IXGBE_LINK_SPEED_1GB_FULL:
937 return 1000;
938 case IXGBE_LINK_SPEED_10GB_FULL:
939 return 10000;
940 default:
941 return 0;
942 }
943}
944
Alexander Duyck9f66d3e2012-07-20 08:09:06 +0000945static void ixgbe_set_vf_rate_limit(struct ixgbe_adapter *adapter, int vf)
Lior Levyff4ab202011-03-11 02:03:07 +0000946{
Alexander Duyck9f66d3e2012-07-20 08:09:06 +0000947 struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ];
948 struct ixgbe_hw *hw = &adapter->hw;
949 u32 bcnrc_val = 0;
950 u16 queue, queues_per_pool;
951 u16 tx_rate = adapter->vfinfo[vf].tx_rate;
Lior Levyff4ab202011-03-11 02:03:07 +0000952
Alexander Duyck9f66d3e2012-07-20 08:09:06 +0000953 if (tx_rate) {
954 /* start with base link speed value */
955 bcnrc_val = adapter->vf_rate_link_speed;
956
Lior Levyff4ab202011-03-11 02:03:07 +0000957 /* Calculate the rate factor values to set */
Alexander Duyck9f66d3e2012-07-20 08:09:06 +0000958 bcnrc_val <<= IXGBE_RTTBCNRC_RF_INT_SHIFT;
959 bcnrc_val /= tx_rate;
Lior Levyff4ab202011-03-11 02:03:07 +0000960
Alexander Duyck9f66d3e2012-07-20 08:09:06 +0000961 /* clear everything but the rate factor */
962 bcnrc_val &= IXGBE_RTTBCNRC_RF_INT_MASK |
963 IXGBE_RTTBCNRC_RF_DEC_MASK;
964
965 /* enable the rate scheduler */
966 bcnrc_val |= IXGBE_RTTBCNRC_RS_ENA;
Lior Levyff4ab202011-03-11 02:03:07 +0000967 }
968
Lior Levy7555e832011-06-25 00:09:08 -0700969 /*
970 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
971 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
972 * and 0x004 otherwise.
973 */
974 switch (hw->mac.type) {
975 case ixgbe_mac_82599EB:
976 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
977 break;
978 case ixgbe_mac_X540:
979 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
980 break;
981 default:
982 break;
983 }
984
Alexander Duyck9f66d3e2012-07-20 08:09:06 +0000985 /* determine how many queues per pool based on VMDq mask */
986 queues_per_pool = __ALIGN_MASK(1, ~vmdq->mask);
987
988 /* write value for all Tx queues belonging to VF */
989 for (queue = 0; queue < queues_per_pool; queue++) {
990 unsigned int reg_idx = (vf * queues_per_pool) + queue;
991
992 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, reg_idx);
993 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
994 }
Lior Levyff4ab202011-03-11 02:03:07 +0000995}
996
997void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
998{
Alexander Duyck9f66d3e2012-07-20 08:09:06 +0000999 int i;
Lior Levyff4ab202011-03-11 02:03:07 +00001000
1001 /* VF Tx rate limit was not set */
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001002 if (!adapter->vf_rate_link_speed)
Lior Levyff4ab202011-03-11 02:03:07 +00001003 return;
1004
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001005 if (ixgbe_link_mbps(adapter) != adapter->vf_rate_link_speed) {
Lior Levyff4ab202011-03-11 02:03:07 +00001006 adapter->vf_rate_link_speed = 0;
1007 dev_info(&adapter->pdev->dev,
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001008 "Link speed has been changed. VF Transmit rate is disabled\n");
Lior Levyff4ab202011-03-11 02:03:07 +00001009 }
1010
1011 for (i = 0; i < adapter->num_vfs; i++) {
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001012 if (!adapter->vf_rate_link_speed)
Lior Levyff4ab202011-03-11 02:03:07 +00001013 adapter->vfinfo[i].tx_rate = 0;
1014
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001015 ixgbe_set_vf_rate_limit(adapter, i);
Lior Levyff4ab202011-03-11 02:03:07 +00001016 }
1017}
1018
Greg Rose7f016482010-05-04 22:12:06 +00001019int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
1020{
Lior Levyff4ab202011-03-11 02:03:07 +00001021 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001022 int link_speed;
Lior Levyff4ab202011-03-11 02:03:07 +00001023
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001024 /* verify VF is active */
1025 if (vf >= adapter->num_vfs)
Lior Levyff4ab202011-03-11 02:03:07 +00001026 return -EINVAL;
1027
Alexander Duyck9f66d3e2012-07-20 08:09:06 +00001028 /* verify link is up */
1029 if (!adapter->link_up)
1030 return -EINVAL;
1031
1032 /* verify we are linked at 10Gbps */
1033 link_speed = ixgbe_link_mbps(adapter);
1034 if (link_speed != 10000)
1035 return -EINVAL;
1036
1037 /* rate limit cannot be less than 10Mbs or greater than link speed */
1038 if (tx_rate && ((tx_rate <= 10) || (tx_rate > link_speed)))
1039 return -EINVAL;
1040
1041 /* store values */
1042 adapter->vf_rate_link_speed = link_speed;
1043 adapter->vfinfo[vf].tx_rate = tx_rate;
1044
1045 /* update hardware configuration */
1046 ixgbe_set_vf_rate_limit(adapter, vf);
Lior Levyff4ab202011-03-11 02:03:07 +00001047
1048 return 0;
Greg Rose7f016482010-05-04 22:12:06 +00001049}
1050
Greg Rosede4c7f62011-09-29 05:57:33 +00001051int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
1052{
1053 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1054 int vf_target_reg = vf >> 3;
1055 int vf_target_shift = vf % 8;
1056 struct ixgbe_hw *hw = &adapter->hw;
1057 u32 regval;
1058
1059 adapter->vfinfo[vf].spoofchk_enabled = setting;
1060
1061 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
1062 regval &= ~(1 << vf_target_shift);
1063 regval |= (setting << vf_target_shift);
1064 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
1065
1066 if (adapter->vfinfo[vf].vlan_count) {
1067 vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
1068 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
1069 regval &= ~(1 << vf_target_shift);
1070 regval |= (setting << vf_target_shift);
1071 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
1072 }
1073
1074 return 0;
1075}
1076
Greg Rose7f016482010-05-04 22:12:06 +00001077int ixgbe_ndo_get_vf_config(struct net_device *netdev,
1078 int vf, struct ifla_vf_info *ivi)
1079{
1080 struct ixgbe_adapter *adapter = netdev_priv(netdev);
1081 if (vf >= adapter->num_vfs)
1082 return -EINVAL;
1083 ivi->vf = vf;
1084 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
Lior Levyff4ab202011-03-11 02:03:07 +00001085 ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
Greg Rose7f016482010-05-04 22:12:06 +00001086 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
1087 ivi->qos = adapter->vfinfo[vf].pf_qos;
Greg Rosede4c7f62011-09-29 05:57:33 +00001088 ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
Greg Rose7f016482010-05-04 22:12:06 +00001089 return 0;
1090}