blob: 2d971d18696e569d59b522ad4304f57ce52c408a [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
47static int ixgbe_find_enabled_vfs(struct ixgbe_adapter *adapter)
48{
49 struct pci_dev *pdev = adapter->pdev;
50 struct pci_dev *pvfdev;
51 u16 vf_devfn = 0;
52 int device_id;
53 int vfs_found = 0;
54
55 switch (adapter->hw.mac.type) {
56 case ixgbe_mac_82599EB:
57 device_id = IXGBE_DEV_ID_82599_VF;
58 break;
59 case ixgbe_mac_X540:
60 device_id = IXGBE_DEV_ID_X540_VF;
61 break;
62 default:
63 device_id = 0;
64 break;
65 }
66
67 vf_devfn = pdev->devfn + 0x80;
68 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID, device_id, NULL);
69 while (pvfdev) {
Greg Rosea4b08322012-02-03 00:54:13 +000070 if (pvfdev->devfn == vf_devfn &&
71 (pvfdev->bus->number >= pdev->bus->number))
Greg Rosec6bda302011-08-24 02:37:55 +000072 vfs_found++;
73 vf_devfn += 2;
74 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
75 device_id, pvfdev);
76 }
77
78 return vfs_found;
79}
80
81void ixgbe_enable_sriov(struct ixgbe_adapter *adapter,
82 const struct ixgbe_info *ii)
83{
84 struct ixgbe_hw *hw = &adapter->hw;
85 int err = 0;
86 int num_vf_macvlans, i;
87 struct vf_macvlans *mv_list;
88 int pre_existing_vfs = 0;
89
90 pre_existing_vfs = ixgbe_find_enabled_vfs(adapter);
91 if (!pre_existing_vfs && !adapter->num_vfs)
92 return;
93
94 /* If there are pre-existing VFs then we have to force
95 * use of that many because they were not deleted the last
96 * time someone removed the PF driver. That would have
97 * been because they were allocated to guest VMs and can't
98 * be removed. Go ahead and just re-enable the old amount.
99 * If the user wants to change the number of VFs they can
100 * use ethtool while making sure no VFs are allocated to
101 * guest VMs... i.e. the right way.
102 */
103 if (pre_existing_vfs) {
104 adapter->num_vfs = pre_existing_vfs;
105 dev_warn(&adapter->pdev->dev, "Virtual Functions already "
106 "enabled for this device - Please reload all "
107 "VF drivers to avoid spoofed packet errors\n");
108 } else {
109 err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
110 }
111 if (err) {
112 e_err(probe, "Failed to enable PCI sriov: %d\n", err);
113 goto err_novfs;
114 }
115 adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
116
117 e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);
118
119 num_vf_macvlans = hw->mac.num_rar_entries -
120 (IXGBE_MAX_PF_MACVLANS + 1 + adapter->num_vfs);
121
122 adapter->mv_list = mv_list = kcalloc(num_vf_macvlans,
123 sizeof(struct vf_macvlans),
124 GFP_KERNEL);
125 if (mv_list) {
126 /* Initialize list of VF macvlans */
127 INIT_LIST_HEAD(&adapter->vf_mvs.l);
128 for (i = 0; i < num_vf_macvlans; i++) {
129 mv_list->vf = -1;
130 mv_list->free = true;
131 mv_list->rar_entry = hw->mac.num_rar_entries -
132 (i + adapter->num_vfs + 1);
133 list_add(&mv_list->l, &adapter->vf_mvs.l);
134 mv_list++;
135 }
136 }
137
138 /* If call to enable VFs succeeded then allocate memory
139 * for per VF control structures.
140 */
141 adapter->vfinfo =
142 kcalloc(adapter->num_vfs,
143 sizeof(struct vf_data_storage), GFP_KERNEL);
144 if (adapter->vfinfo) {
145 /* Now that we're sure SR-IOV is enabled
146 * and memory allocated set up the mailbox parameters
147 */
148 ixgbe_init_mbx_params_pf(hw);
149 memcpy(&hw->mbx.ops, ii->mbx_ops,
150 sizeof(hw->mbx.ops));
151
152 /* Disable RSC when in SR-IOV mode */
153 adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
154 IXGBE_FLAG2_RSC_ENABLED);
Greg Rosede4c7f62011-09-29 05:57:33 +0000155 for (i = 0; i < adapter->num_vfs; i++)
156 adapter->vfinfo[i].spoofchk_enabled = true;
Greg Rosec6bda302011-08-24 02:37:55 +0000157 return;
158 }
159
160 /* Oh oh */
161 e_err(probe, "Unable to allocate memory for VF Data Storage - "
162 "SRIOV disabled\n");
163 pci_disable_sriov(adapter->pdev);
164
165err_novfs:
166 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
167 adapter->num_vfs = 0;
168}
169#endif /* #ifdef CONFIG_PCI_IOV */
170
171void ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
172{
173 struct ixgbe_hw *hw = &adapter->hw;
174 u32 gcr;
175 u32 gpie;
176 u32 vmdctl;
177 int i;
178
179#ifdef CONFIG_PCI_IOV
180 /* disable iov and allow time for transactions to clear */
181 pci_disable_sriov(adapter->pdev);
182#endif
183
184 /* turn off device IOV mode */
185 gcr = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
186 gcr &= ~(IXGBE_GCR_EXT_SRIOV);
187 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr);
188 gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
189 gpie &= ~IXGBE_GPIE_VTMODE_MASK;
190 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
191
192 /* set default pool back to 0 */
193 vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
194 vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
195 IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
196 IXGBE_WRITE_FLUSH(hw);
197
198 /* take a breather then clean up driver data */
199 msleep(100);
200
201 /* Release reference to VF devices */
202 for (i = 0; i < adapter->num_vfs; i++) {
203 if (adapter->vfinfo[i].vfdev)
204 pci_dev_put(adapter->vfinfo[i].vfdev);
205 }
206 kfree(adapter->vfinfo);
207 kfree(adapter->mv_list);
208 adapter->vfinfo = NULL;
209
210 adapter->num_vfs = 0;
211 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
212}
213
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000214static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
215 int entries, u16 *hash_list, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000216{
217 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
Greg Rose8a07a222010-05-05 19:57:30 +0000218 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose17367272010-01-09 02:25:48 +0000219 int i;
Greg Rose8a07a222010-05-05 19:57:30 +0000220 u32 vector_bit;
221 u32 vector_reg;
222 u32 mta_reg;
Greg Rose17367272010-01-09 02:25:48 +0000223
224 /* only so many hash values supported */
225 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
226
227 /*
228 * salt away the number of multi cast addresses assigned
229 * to this VF for later use to restore when the PF multi cast
230 * list changes
231 */
232 vfinfo->num_vf_mc_hashes = entries;
233
234 /*
235 * VFs are limited to using the MTA hash table for their multicast
236 * addresses
237 */
238 for (i = 0; i < entries; i++) {
Joe Perchese81a1ba2010-11-14 17:04:33 +0000239 vfinfo->vf_mc_hashes[i] = hash_list[i];
Greg Rose17367272010-01-09 02:25:48 +0000240 }
241
Greg Rose8a07a222010-05-05 19:57:30 +0000242 for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
243 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
244 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
245 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
246 mta_reg |= (1 << vector_bit);
247 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
248 }
Greg Rose17367272010-01-09 02:25:48 +0000249
250 return 0;
251}
252
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000253static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
254{
255 struct ixgbe_hw *hw = &adapter->hw;
256 struct list_head *pos;
257 struct vf_macvlans *entry;
258
259 list_for_each(pos, &adapter->vf_mvs.l) {
260 entry = list_entry(pos, struct vf_macvlans, l);
Joe Perches23677ce2012-02-09 11:17:23 +0000261 if (!entry->free)
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000262 hw->mac.ops.set_rar(hw, entry->rar_entry,
263 entry->vf_macvlan,
264 entry->vf, IXGBE_RAH_AV);
265 }
266}
267
Greg Rose17367272010-01-09 02:25:48 +0000268void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
269{
270 struct ixgbe_hw *hw = &adapter->hw;
271 struct vf_data_storage *vfinfo;
272 int i, j;
273 u32 vector_bit;
274 u32 vector_reg;
275 u32 mta_reg;
276
277 for (i = 0; i < adapter->num_vfs; i++) {
278 vfinfo = &adapter->vfinfo[i];
279 for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
280 hw->addr_ctrl.mta_in_use++;
281 vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
282 vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
283 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
284 mta_reg |= (1 << vector_bit);
285 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
286 }
287 }
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000288
289 /* Restore any VF macvlans */
290 ixgbe_restore_vf_macvlans(adapter);
Greg Rose17367272010-01-09 02:25:48 +0000291}
292
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000293static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
294 u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000295{
Greg Rose17367272010-01-09 02:25:48 +0000296 return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
297}
298
John Fastabendb32c8dc2011-04-12 02:44:55 +0000299static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
Greg Rosee9f98072011-01-26 01:06:07 +0000300{
301 struct ixgbe_hw *hw = &adapter->hw;
302 int new_mtu = msgbuf[1];
303 u32 max_frs;
304 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
305
306 /* Only X540 supports jumbo frames in IOV mode */
307 if (adapter->hw.mac.type != ixgbe_mac_X540)
308 return;
309
310 /* MTU < 68 is an error and causes problems on some kernels */
311 if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE)) {
312 e_err(drv, "VF mtu %d out of range\n", new_mtu);
313 return;
314 }
315
316 max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
317 IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
318 if (max_frs < new_mtu) {
319 max_frs = new_mtu << IXGBE_MHADD_MFS_SHIFT;
320 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
321 }
322
323 e_info(hw, "VF requests change max MTU to %d\n", new_mtu);
324}
325
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000326static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
Greg Rose17367272010-01-09 02:25:48 +0000327{
328 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
Greg Rosef0412772010-05-04 22:11:46 +0000329 vmolr |= (IXGBE_VMOLR_ROMPE |
Greg Rose17367272010-01-09 02:25:48 +0000330 IXGBE_VMOLR_BAM);
Greg Rosef0412772010-05-04 22:11:46 +0000331 if (aupe)
332 vmolr |= IXGBE_VMOLR_AUPE;
333 else
334 vmolr &= ~IXGBE_VMOLR_AUPE;
Greg Rose17367272010-01-09 02:25:48 +0000335 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
336}
337
Greg Rose7f016482010-05-04 22:12:06 +0000338static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
339{
340 struct ixgbe_hw *hw = &adapter->hw;
341
342 if (vid)
343 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
344 (vid | IXGBE_VMVIR_VLANA_DEFAULT));
345 else
346 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
347}
348
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000349static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000350{
351 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck28500622010-06-15 09:25:48 +0000352 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
Greg Rose17367272010-01-09 02:25:48 +0000353
354 /* reset offloads to defaults */
Greg Rose7f016482010-05-04 22:12:06 +0000355 if (adapter->vfinfo[vf].pf_vlan) {
356 ixgbe_set_vf_vlan(adapter, true,
357 adapter->vfinfo[vf].pf_vlan, vf);
358 ixgbe_set_vmvir(adapter,
359 (adapter->vfinfo[vf].pf_vlan |
360 (adapter->vfinfo[vf].pf_qos <<
361 VLAN_PRIO_SHIFT)), vf);
362 ixgbe_set_vmolr(hw, vf, false);
363 } else {
364 ixgbe_set_vmvir(adapter, 0, vf);
365 ixgbe_set_vmolr(hw, vf, true);
366 }
Greg Rose17367272010-01-09 02:25:48 +0000367
368 /* reset multicast table array for vf */
369 adapter->vfinfo[vf].num_vf_mc_hashes = 0;
370
371 /* Flush and reset the mta with the new values */
372 ixgbe_set_rx_mode(adapter->netdev);
373
Alexander Duyck28500622010-06-15 09:25:48 +0000374 hw->mac.ops.clear_rar(hw, rar_entry);
Greg Rose17367272010-01-09 02:25:48 +0000375}
376
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000377static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
378 int vf, unsigned char *mac_addr)
Greg Rose17367272010-01-09 02:25:48 +0000379{
380 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck28500622010-06-15 09:25:48 +0000381 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
Greg Rose17367272010-01-09 02:25:48 +0000382
383 memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
Alexander Duyck28500622010-06-15 09:25:48 +0000384 hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
Greg Rose17367272010-01-09 02:25:48 +0000385
386 return 0;
387}
388
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000389static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
390 int vf, int index, unsigned char *mac_addr)
391{
392 struct ixgbe_hw *hw = &adapter->hw;
393 struct list_head *pos;
394 struct vf_macvlans *entry;
395
396 if (index <= 1) {
397 list_for_each(pos, &adapter->vf_mvs.l) {
398 entry = list_entry(pos, struct vf_macvlans, l);
399 if (entry->vf == vf) {
400 entry->vf = -1;
401 entry->free = true;
402 entry->is_macvlan = false;
403 hw->mac.ops.clear_rar(hw, entry->rar_entry);
404 }
405 }
406 }
407
408 /*
409 * If index was zero then we were asked to clear the uc list
410 * for the VF. We're done.
411 */
412 if (!index)
413 return 0;
414
415 entry = NULL;
416
417 list_for_each(pos, &adapter->vf_mvs.l) {
418 entry = list_entry(pos, struct vf_macvlans, l);
419 if (entry->free)
420 break;
421 }
422
423 /*
424 * If we traversed the entire list and didn't find a free entry
425 * then we're out of space on the RAR table. Also entry may
426 * be NULL because the original memory allocation for the list
427 * failed, which is not fatal but does mean we can't support
428 * VF requests for MACVLAN because we couldn't allocate
429 * memory for the list management required.
430 */
431 if (!entry || !entry->free)
432 return -ENOSPC;
433
434 entry->free = false;
435 entry->is_macvlan = true;
436 entry->vf = vf;
437 memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
438
439 hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
440
441 return 0;
442}
443
Greg Rosec6bda302011-08-24 02:37:55 +0000444int ixgbe_check_vf_assignment(struct ixgbe_adapter *adapter)
445{
Rose, Gregory V01264102011-11-07 07:44:17 +0000446#ifdef CONFIG_PCI_IOV
Greg Rosec6bda302011-08-24 02:37:55 +0000447 int i;
448 for (i = 0; i < adapter->num_vfs; i++) {
449 if (adapter->vfinfo[i].vfdev->dev_flags &
450 PCI_DEV_FLAGS_ASSIGNED)
451 return true;
452 }
Rose, Gregory V01264102011-11-07 07:44:17 +0000453#endif
Greg Rosec6bda302011-08-24 02:37:55 +0000454 return false;
455}
456
Greg Rose17367272010-01-09 02:25:48 +0000457int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
458{
459 unsigned char vf_mac_addr[6];
Alexander Duyckc60fbb02010-11-16 19:26:54 -0800460 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
Greg Rose17367272010-01-09 02:25:48 +0000461 unsigned int vfn = (event_mask & 0x3f);
Greg Rosec6bda302011-08-24 02:37:55 +0000462 struct pci_dev *pvfdev;
463 unsigned int device_id;
464 u16 thisvf_devfn = (pdev->devfn + 0x80 + (vfn << 1)) |
465 (pdev->devfn & 1);
Greg Rose17367272010-01-09 02:25:48 +0000466
467 bool enable = ((event_mask & 0x10000000U) != 0);
468
469 if (enable) {
470 random_ether_addr(vf_mac_addr);
Emil Tantilov396e7992010-07-01 20:05:12 +0000471 e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
472 vfn, vf_mac_addr);
Greg Rose17367272010-01-09 02:25:48 +0000473 /*
474 * Store away the VF "permananet" MAC address, it will ask
475 * for it later.
476 */
477 memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
Greg Rosec6bda302011-08-24 02:37:55 +0000478
479 switch (adapter->hw.mac.type) {
480 case ixgbe_mac_82599EB:
481 device_id = IXGBE_DEV_ID_82599_VF;
482 break;
483 case ixgbe_mac_X540:
484 device_id = IXGBE_DEV_ID_X540_VF;
485 break;
486 default:
487 device_id = 0;
488 break;
489 }
490
491 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID, device_id, NULL);
492 while (pvfdev) {
493 if (pvfdev->devfn == thisvf_devfn)
494 break;
495 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
496 device_id, pvfdev);
497 }
498 if (pvfdev)
499 adapter->vfinfo[vfn].vfdev = pvfdev;
500 else
501 e_err(drv, "Couldn't find pci dev ptr for VF %4.4x\n",
502 thisvf_devfn);
Greg Rose17367272010-01-09 02:25:48 +0000503 }
504
505 return 0;
506}
507
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000508static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000509{
510 struct ixgbe_hw *hw = &adapter->hw;
511 u32 reg;
512 u32 reg_offset, vf_shift;
513
514 vf_shift = vf % 32;
515 reg_offset = vf / 32;
516
517 /* enable transmit and receive for vf */
518 reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
519 reg |= (reg | (1 << vf_shift));
520 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
521
522 reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
523 reg |= (reg | (1 << vf_shift));
524 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
525
Greg Rosea985b6c32010-11-18 03:02:52 +0000526 /* Enable counting of spoofed packets in the SSVPC register */
527 reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
528 reg |= (1 << vf_shift);
529 IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
530
Greg Rose17367272010-01-09 02:25:48 +0000531 ixgbe_vf_reset_event(adapter, vf);
532}
533
534static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
535{
536 u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
Emil Tantilovc0509992011-05-07 06:49:18 +0000537 u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
Greg Rose17367272010-01-09 02:25:48 +0000538 struct ixgbe_hw *hw = &adapter->hw;
539 s32 retval;
540 int entries;
541 u16 *hash_list;
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000542 int add, vid, index;
Greg Rosed3306c22010-11-18 03:03:23 +0000543 u8 *new_mac;
Greg Rose17367272010-01-09 02:25:48 +0000544
545 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
546
Alexander Duyckdcaccc82012-03-28 08:03:38 +0000547 if (retval) {
Emil Tantilov849c4542010-06-03 16:53:41 +0000548 pr_err("Error receiving message from VF\n");
Alexander Duyckdcaccc82012-03-28 08:03:38 +0000549 return retval;
550 }
Greg Rose17367272010-01-09 02:25:48 +0000551
552 /* this is a message we already processed, do nothing */
553 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
554 return retval;
555
Alexander Duyckdcaccc82012-03-28 08:03:38 +0000556 /* flush the ack before we write any messages back */
557 IXGBE_WRITE_FLUSH(hw);
558
Greg Rose17367272010-01-09 02:25:48 +0000559 /*
560 * until the vf completes a virtual function reset it should not be
561 * allowed to start any configuration.
562 */
563
564 if (msgbuf[0] == IXGBE_VF_RESET) {
565 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
Greg Rosed3306c22010-11-18 03:03:23 +0000566 new_mac = (u8 *)(&msgbuf[1]);
Emil Tantilov396e7992010-07-01 20:05:12 +0000567 e_info(probe, "VF Reset msg received from vf %d\n", vf);
Greg Rose17367272010-01-09 02:25:48 +0000568 adapter->vfinfo[vf].clear_to_send = false;
569 ixgbe_vf_reset_msg(adapter, vf);
570 adapter->vfinfo[vf].clear_to_send = true;
571
Greg Rosed3306c22010-11-18 03:03:23 +0000572 if (is_valid_ether_addr(new_mac) &&
573 !adapter->vfinfo[vf].pf_set_mac)
574 ixgbe_set_vf_mac(adapter, vf, vf_mac);
575 else
576 ixgbe_set_vf_mac(adapter,
577 vf, adapter->vfinfo[vf].vf_mac_addresses);
578
Greg Rose17367272010-01-09 02:25:48 +0000579 /* reply to reset with ack and vf mac address */
580 msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
Joe Perchesea99d832011-09-20 15:32:52 +0000581 memcpy(new_mac, vf_mac, ETH_ALEN);
Greg Rose17367272010-01-09 02:25:48 +0000582 /*
583 * Piggyback the multicast filter type so VF can compute the
584 * correct vectors
585 */
586 msgbuf[3] = hw->mac.mc_filter_type;
587 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
588
589 return retval;
590 }
591
592 if (!adapter->vfinfo[vf].clear_to_send) {
593 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
594 ixgbe_write_mbx(hw, msgbuf, 1, vf);
595 return retval;
596 }
597
598 switch ((msgbuf[0] & 0xFFFF)) {
599 case IXGBE_VF_SET_MAC_ADDR:
Greg Rosed3306c22010-11-18 03:03:23 +0000600 new_mac = ((u8 *)(&msgbuf[1]));
601 if (is_valid_ether_addr(new_mac) &&
602 !adapter->vfinfo[vf].pf_set_mac) {
603 ixgbe_set_vf_mac(adapter, vf, new_mac);
604 } else if (memcmp(adapter->vfinfo[vf].vf_mac_addresses,
605 new_mac, ETH_ALEN)) {
606 e_warn(drv, "VF %d attempted to override "
607 "administratively set MAC address\nReload "
608 "the VF driver to resume operations\n", vf);
609 retval = -1;
Greg Rose17367272010-01-09 02:25:48 +0000610 }
611 break;
612 case IXGBE_VF_SET_MULTICAST:
613 entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
614 >> IXGBE_VT_MSGINFO_SHIFT;
615 hash_list = (u16 *)&msgbuf[1];
616 retval = ixgbe_set_vf_multicasts(adapter, entries,
617 hash_list, vf);
618 break;
619 case IXGBE_VF_SET_LPE:
Greg Rosee9f98072011-01-26 01:06:07 +0000620 ixgbe_set_vf_lpe(adapter, msgbuf);
Greg Rose17367272010-01-09 02:25:48 +0000621 break;
622 case IXGBE_VF_SET_VLAN:
623 add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
624 >> IXGBE_VT_MSGINFO_SHIFT;
625 vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
Greg Rosed3306c22010-11-18 03:03:23 +0000626 if (adapter->vfinfo[vf].pf_vlan) {
627 e_warn(drv, "VF %d attempted to override "
628 "administratively set VLAN configuration\n"
629 "Reload the VF driver to resume operations\n",
630 vf);
631 retval = -1;
632 } else {
Greg Rosede4c7f62011-09-29 05:57:33 +0000633 if (add)
634 adapter->vfinfo[vf].vlan_count++;
635 else if (adapter->vfinfo[vf].vlan_count)
636 adapter->vfinfo[vf].vlan_count--;
Greg Rosed3306c22010-11-18 03:03:23 +0000637 retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
Greg Rosede4c7f62011-09-29 05:57:33 +0000638 if (!retval && adapter->vfinfo[vf].spoofchk_enabled)
639 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
Greg Rosed3306c22010-11-18 03:03:23 +0000640 }
Greg Rose17367272010-01-09 02:25:48 +0000641 break;
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000642 case IXGBE_VF_SET_MACVLAN:
Greg Rose44b82dd2012-04-21 00:54:28 +0000643 index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
644 IXGBE_VT_MSGINFO_SHIFT;
645 if (adapter->vfinfo[vf].pf_set_mac && index > 0) {
Greg Rose2ee70652012-03-24 00:26:44 +0000646 e_warn(drv, "VF %d requested MACVLAN filter but is "
647 "administratively denied\n", vf);
648 retval = -1;
649 break;
650 }
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000651 /*
652 * If the VF is allowed to set MAC filters then turn off
653 * anti-spoofing to avoid false positives. An index
654 * greater than 0 will indicate the VF is setting a
655 * macvlan MAC filter.
656 */
Greg Rosede4c7f62011-09-29 05:57:33 +0000657 if (index > 0 && adapter->vfinfo[vf].spoofchk_enabled)
658 ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000659 retval = ixgbe_set_vf_macvlan(adapter, vf, index,
660 (unsigned char *)(&msgbuf[1]));
Greg Rose68d6d4a2012-01-05 07:58:11 +0000661 if (retval == -ENOSPC)
662 e_warn(drv, "VF %d has requested a MACVLAN filter "
663 "but there is no space for it\n", vf);
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000664 break;
Greg Rose17367272010-01-09 02:25:48 +0000665 default:
Emil Tantilov396e7992010-07-01 20:05:12 +0000666 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
Greg Rose17367272010-01-09 02:25:48 +0000667 retval = IXGBE_ERR_MBX;
668 break;
669 }
670
671 /* notify the VF of the results of what it sent us */
672 if (retval)
673 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
674 else
675 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
676
677 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
678
679 ixgbe_write_mbx(hw, msgbuf, 1, vf);
680
681 return retval;
682}
683
684static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
685{
686 struct ixgbe_hw *hw = &adapter->hw;
687 u32 msg = IXGBE_VT_MSGTYPE_NACK;
688
689 /* if device isn't clear to send it shouldn't be reading either */
690 if (!adapter->vfinfo[vf].clear_to_send)
691 ixgbe_write_mbx(hw, &msg, 1, vf);
692}
693
694void ixgbe_msg_task(struct ixgbe_adapter *adapter)
695{
696 struct ixgbe_hw *hw = &adapter->hw;
697 u32 vf;
698
699 for (vf = 0; vf < adapter->num_vfs; vf++) {
700 /* process any reset requests */
701 if (!ixgbe_check_for_rst(hw, vf))
702 ixgbe_vf_reset_event(adapter, vf);
703
704 /* process any messages pending */
705 if (!ixgbe_check_for_msg(hw, vf))
706 ixgbe_rcv_msg_from_vf(adapter, vf);
707
708 /* process any acks */
709 if (!ixgbe_check_for_ack(hw, vf))
710 ixgbe_rcv_ack_from_vf(adapter, vf);
711 }
712}
713
Greg Rose767081a2010-01-22 22:46:40 +0000714void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
715{
716 struct ixgbe_hw *hw = &adapter->hw;
717
718 /* disable transmit and receive for all vfs */
719 IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
720 IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
721
722 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
723 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
724}
725
726void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
727{
728 struct ixgbe_hw *hw = &adapter->hw;
729 u32 ping;
730 int i;
731
732 for (i = 0 ; i < adapter->num_vfs; i++) {
733 ping = IXGBE_PF_CONTROL_MSG;
734 if (adapter->vfinfo[i].clear_to_send)
735 ping |= IXGBE_VT_MSGTYPE_CTS;
736 ixgbe_write_mbx(hw, &ping, 1, i);
737 }
738}
739
Greg Rose7f016482010-05-04 22:12:06 +0000740int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
741{
742 struct ixgbe_adapter *adapter = netdev_priv(netdev);
743 if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
744 return -EINVAL;
745 adapter->vfinfo[vf].pf_set_mac = true;
746 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
747 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
748 " change effective.");
749 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
750 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
751 " but the PF device is not up.\n");
752 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
753 " attempting to use the VF device.\n");
754 }
755 return ixgbe_set_vf_mac(adapter, vf, mac);
756}
757
758int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
759{
760 int err = 0;
761 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Greg Rosea985b6c32010-11-18 03:02:52 +0000762 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose7f016482010-05-04 22:12:06 +0000763
764 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
765 return -EINVAL;
766 if (vlan || qos) {
767 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
768 if (err)
769 goto out;
770 ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
Greg Rosea985b6c32010-11-18 03:02:52 +0000771 ixgbe_set_vmolr(hw, vf, false);
Greg Rosede4c7f62011-09-29 05:57:33 +0000772 if (adapter->vfinfo[vf].spoofchk_enabled)
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000773 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
Greg Rosede4c7f62011-09-29 05:57:33 +0000774 adapter->vfinfo[vf].vlan_count++;
Greg Rose7f016482010-05-04 22:12:06 +0000775 adapter->vfinfo[vf].pf_vlan = vlan;
776 adapter->vfinfo[vf].pf_qos = qos;
777 dev_info(&adapter->pdev->dev,
778 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
779 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
780 dev_warn(&adapter->pdev->dev,
781 "The VF VLAN has been set,"
782 " but the PF device is not up.\n");
783 dev_warn(&adapter->pdev->dev,
784 "Bring the PF device up before"
785 " attempting to use the VF device.\n");
786 }
787 } else {
788 err = ixgbe_set_vf_vlan(adapter, false,
789 adapter->vfinfo[vf].pf_vlan, vf);
790 ixgbe_set_vmvir(adapter, vlan, vf);
Greg Rosea985b6c32010-11-18 03:02:52 +0000791 ixgbe_set_vmolr(hw, vf, true);
792 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
Greg Rosede4c7f62011-09-29 05:57:33 +0000793 if (adapter->vfinfo[vf].vlan_count)
794 adapter->vfinfo[vf].vlan_count--;
Greg Rose7f016482010-05-04 22:12:06 +0000795 adapter->vfinfo[vf].pf_vlan = 0;
796 adapter->vfinfo[vf].pf_qos = 0;
797 }
798out:
799 return err;
800}
801
Lior Levyff4ab202011-03-11 02:03:07 +0000802static int ixgbe_link_mbps(int internal_link_speed)
803{
804 switch (internal_link_speed) {
805 case IXGBE_LINK_SPEED_100_FULL:
806 return 100;
807 case IXGBE_LINK_SPEED_1GB_FULL:
808 return 1000;
809 case IXGBE_LINK_SPEED_10GB_FULL:
810 return 10000;
811 default:
812 return 0;
813 }
814}
815
816static void ixgbe_set_vf_rate_limit(struct ixgbe_hw *hw, int vf, int tx_rate,
817 int link_speed)
818{
819 int rf_dec, rf_int;
820 u32 bcnrc_val;
821
822 if (tx_rate != 0) {
823 /* Calculate the rate factor values to set */
824 rf_int = link_speed / tx_rate;
825 rf_dec = (link_speed - (rf_int * tx_rate));
826 rf_dec = (rf_dec * (1<<IXGBE_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
827
828 bcnrc_val = IXGBE_RTTBCNRC_RS_ENA;
829 bcnrc_val |= ((rf_int<<IXGBE_RTTBCNRC_RF_INT_SHIFT) &
830 IXGBE_RTTBCNRC_RF_INT_MASK);
831 bcnrc_val |= (rf_dec & IXGBE_RTTBCNRC_RF_DEC_MASK);
832 } else {
833 bcnrc_val = 0;
834 }
835
836 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, 2*vf); /* vf Y uses queue 2*Y */
Lior Levy7555e832011-06-25 00:09:08 -0700837 /*
838 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
839 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
840 * and 0x004 otherwise.
841 */
842 switch (hw->mac.type) {
843 case ixgbe_mac_82599EB:
844 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
845 break;
846 case ixgbe_mac_X540:
847 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
848 break;
849 default:
850 break;
851 }
852
Lior Levyff4ab202011-03-11 02:03:07 +0000853 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
854}
855
856void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
857{
858 int actual_link_speed, i;
859 bool reset_rate = false;
860
861 /* VF Tx rate limit was not set */
862 if (adapter->vf_rate_link_speed == 0)
863 return;
864
865 actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
866 if (actual_link_speed != adapter->vf_rate_link_speed) {
867 reset_rate = true;
868 adapter->vf_rate_link_speed = 0;
869 dev_info(&adapter->pdev->dev,
870 "Link speed has been changed. VF Transmit rate "
871 "is disabled\n");
872 }
873
874 for (i = 0; i < adapter->num_vfs; i++) {
875 if (reset_rate)
876 adapter->vfinfo[i].tx_rate = 0;
877
878 ixgbe_set_vf_rate_limit(&adapter->hw, i,
879 adapter->vfinfo[i].tx_rate,
880 actual_link_speed);
881 }
882}
883
Greg Rose7f016482010-05-04 22:12:06 +0000884int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
885{
Lior Levyff4ab202011-03-11 02:03:07 +0000886 struct ixgbe_adapter *adapter = netdev_priv(netdev);
887 struct ixgbe_hw *hw = &adapter->hw;
888 int actual_link_speed;
889
890 actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
891 if ((vf >= adapter->num_vfs) || (!adapter->link_up) ||
892 (tx_rate > actual_link_speed) || (actual_link_speed != 10000) ||
893 ((tx_rate != 0) && (tx_rate <= 10)))
894 /* rate limit cannot be set to 10Mb or less in 10Gb adapters */
895 return -EINVAL;
896
897 adapter->vf_rate_link_speed = actual_link_speed;
898 adapter->vfinfo[vf].tx_rate = (u16)tx_rate;
899 ixgbe_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
900
901 return 0;
Greg Rose7f016482010-05-04 22:12:06 +0000902}
903
Greg Rosede4c7f62011-09-29 05:57:33 +0000904int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
905{
906 struct ixgbe_adapter *adapter = netdev_priv(netdev);
907 int vf_target_reg = vf >> 3;
908 int vf_target_shift = vf % 8;
909 struct ixgbe_hw *hw = &adapter->hw;
910 u32 regval;
911
912 adapter->vfinfo[vf].spoofchk_enabled = setting;
913
914 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
915 regval &= ~(1 << vf_target_shift);
916 regval |= (setting << vf_target_shift);
917 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
918
919 if (adapter->vfinfo[vf].vlan_count) {
920 vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
921 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
922 regval &= ~(1 << vf_target_shift);
923 regval |= (setting << vf_target_shift);
924 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
925 }
926
927 return 0;
928}
929
Greg Rose7f016482010-05-04 22:12:06 +0000930int ixgbe_ndo_get_vf_config(struct net_device *netdev,
931 int vf, struct ifla_vf_info *ivi)
932{
933 struct ixgbe_adapter *adapter = netdev_priv(netdev);
934 if (vf >= adapter->num_vfs)
935 return -EINVAL;
936 ivi->vf = vf;
937 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
Lior Levyff4ab202011-03-11 02:03:07 +0000938 ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
Greg Rose7f016482010-05-04 22:12:06 +0000939 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
940 ivi->qos = adapter->vfinfo[vf].pf_qos;
Greg Rosede4c7f62011-09-29 05:57:33 +0000941 ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
Greg Rose7f016482010-05-04 22:12:06 +0000942 return 0;
943}