blob: db95731863d75bf470ce660b13de951f50670eed [file] [log] [blame]
Greg Rose17367272010-01-09 02:25:48 +00001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
Don Skidmorea52055e2011-02-23 09:58:39 +00004 Copyright(c) 1999 - 2011 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) {
70 if (pvfdev->devfn == vf_devfn)
71 vfs_found++;
72 vf_devfn += 2;
73 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
74 device_id, pvfdev);
75 }
76
77 return vfs_found;
78}
79
80void ixgbe_enable_sriov(struct ixgbe_adapter *adapter,
81 const struct ixgbe_info *ii)
82{
83 struct ixgbe_hw *hw = &adapter->hw;
84 int err = 0;
85 int num_vf_macvlans, i;
86 struct vf_macvlans *mv_list;
87 int pre_existing_vfs = 0;
88
89 pre_existing_vfs = ixgbe_find_enabled_vfs(adapter);
90 if (!pre_existing_vfs && !adapter->num_vfs)
91 return;
92
93 /* If there are pre-existing VFs then we have to force
94 * use of that many because they were not deleted the last
95 * time someone removed the PF driver. That would have
96 * been because they were allocated to guest VMs and can't
97 * be removed. Go ahead and just re-enable the old amount.
98 * If the user wants to change the number of VFs they can
99 * use ethtool while making sure no VFs are allocated to
100 * guest VMs... i.e. the right way.
101 */
102 if (pre_existing_vfs) {
103 adapter->num_vfs = pre_existing_vfs;
104 dev_warn(&adapter->pdev->dev, "Virtual Functions already "
105 "enabled for this device - Please reload all "
106 "VF drivers to avoid spoofed packet errors\n");
107 } else {
108 err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
109 }
110 if (err) {
111 e_err(probe, "Failed to enable PCI sriov: %d\n", err);
112 goto err_novfs;
113 }
114 adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
115
116 e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);
117
118 num_vf_macvlans = hw->mac.num_rar_entries -
119 (IXGBE_MAX_PF_MACVLANS + 1 + adapter->num_vfs);
120
121 adapter->mv_list = mv_list = kcalloc(num_vf_macvlans,
122 sizeof(struct vf_macvlans),
123 GFP_KERNEL);
124 if (mv_list) {
125 /* Initialize list of VF macvlans */
126 INIT_LIST_HEAD(&adapter->vf_mvs.l);
127 for (i = 0; i < num_vf_macvlans; i++) {
128 mv_list->vf = -1;
129 mv_list->free = true;
130 mv_list->rar_entry = hw->mac.num_rar_entries -
131 (i + adapter->num_vfs + 1);
132 list_add(&mv_list->l, &adapter->vf_mvs.l);
133 mv_list++;
134 }
135 }
136
137 /* If call to enable VFs succeeded then allocate memory
138 * for per VF control structures.
139 */
140 adapter->vfinfo =
141 kcalloc(adapter->num_vfs,
142 sizeof(struct vf_data_storage), GFP_KERNEL);
143 if (adapter->vfinfo) {
144 /* Now that we're sure SR-IOV is enabled
145 * and memory allocated set up the mailbox parameters
146 */
147 ixgbe_init_mbx_params_pf(hw);
148 memcpy(&hw->mbx.ops, ii->mbx_ops,
149 sizeof(hw->mbx.ops));
150
151 /* Disable RSC when in SR-IOV mode */
152 adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
153 IXGBE_FLAG2_RSC_ENABLED);
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");
162 pci_disable_sriov(adapter->pdev);
163
164err_novfs:
165 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
166 adapter->num_vfs = 0;
167}
168#endif /* #ifdef CONFIG_PCI_IOV */
169
170void ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
171{
172 struct ixgbe_hw *hw = &adapter->hw;
173 u32 gcr;
174 u32 gpie;
175 u32 vmdctl;
176 int i;
177
178#ifdef CONFIG_PCI_IOV
179 /* disable iov and allow time for transactions to clear */
180 pci_disable_sriov(adapter->pdev);
181#endif
182
183 /* turn off device IOV mode */
184 gcr = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
185 gcr &= ~(IXGBE_GCR_EXT_SRIOV);
186 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr);
187 gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
188 gpie &= ~IXGBE_GPIE_VTMODE_MASK;
189 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
190
191 /* set default pool back to 0 */
192 vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
193 vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
194 IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
195 IXGBE_WRITE_FLUSH(hw);
196
197 /* take a breather then clean up driver data */
198 msleep(100);
199
200 /* Release reference to VF devices */
201 for (i = 0; i < adapter->num_vfs; i++) {
202 if (adapter->vfinfo[i].vfdev)
203 pci_dev_put(adapter->vfinfo[i].vfdev);
204 }
205 kfree(adapter->vfinfo);
206 kfree(adapter->mv_list);
207 adapter->vfinfo = NULL;
208
209 adapter->num_vfs = 0;
210 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
211}
212
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000213static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
214 int entries, u16 *hash_list, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000215{
216 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
Greg Rose8a07a222010-05-05 19:57:30 +0000217 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose17367272010-01-09 02:25:48 +0000218 int i;
Greg Rose8a07a222010-05-05 19:57:30 +0000219 u32 vector_bit;
220 u32 vector_reg;
221 u32 mta_reg;
Greg Rose17367272010-01-09 02:25:48 +0000222
223 /* only so many hash values supported */
224 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
225
226 /*
227 * salt away the number of multi cast addresses assigned
228 * to this VF for later use to restore when the PF multi cast
229 * list changes
230 */
231 vfinfo->num_vf_mc_hashes = entries;
232
233 /*
234 * VFs are limited to using the MTA hash table for their multicast
235 * addresses
236 */
237 for (i = 0; i < entries; i++) {
Joe Perchese81a1ba2010-11-14 17:04:33 +0000238 vfinfo->vf_mc_hashes[i] = hash_list[i];
Greg Rose17367272010-01-09 02:25:48 +0000239 }
240
Greg Rose8a07a222010-05-05 19:57:30 +0000241 for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
242 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
243 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
244 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
245 mta_reg |= (1 << vector_bit);
246 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
247 }
Greg Rose17367272010-01-09 02:25:48 +0000248
249 return 0;
250}
251
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000252static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
253{
254 struct ixgbe_hw *hw = &adapter->hw;
255 struct list_head *pos;
256 struct vf_macvlans *entry;
257
258 list_for_each(pos, &adapter->vf_mvs.l) {
259 entry = list_entry(pos, struct vf_macvlans, l);
260 if (entry->free == false)
261 hw->mac.ops.set_rar(hw, entry->rar_entry,
262 entry->vf_macvlan,
263 entry->vf, IXGBE_RAH_AV);
264 }
265}
266
Greg Rose17367272010-01-09 02:25:48 +0000267void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
268{
269 struct ixgbe_hw *hw = &adapter->hw;
270 struct vf_data_storage *vfinfo;
271 int i, j;
272 u32 vector_bit;
273 u32 vector_reg;
274 u32 mta_reg;
275
276 for (i = 0; i < adapter->num_vfs; i++) {
277 vfinfo = &adapter->vfinfo[i];
278 for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
279 hw->addr_ctrl.mta_in_use++;
280 vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
281 vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
282 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
283 mta_reg |= (1 << vector_bit);
284 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
285 }
286 }
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000287
288 /* Restore any VF macvlans */
289 ixgbe_restore_vf_macvlans(adapter);
Greg Rose17367272010-01-09 02:25:48 +0000290}
291
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000292static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
293 u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000294{
Greg Rose17367272010-01-09 02:25:48 +0000295 return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
296}
297
John Fastabendb32c8dc2011-04-12 02:44:55 +0000298static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
Greg Rosee9f98072011-01-26 01:06:07 +0000299{
300 struct ixgbe_hw *hw = &adapter->hw;
301 int new_mtu = msgbuf[1];
302 u32 max_frs;
303 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
304
305 /* Only X540 supports jumbo frames in IOV mode */
306 if (adapter->hw.mac.type != ixgbe_mac_X540)
307 return;
308
309 /* MTU < 68 is an error and causes problems on some kernels */
310 if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE)) {
311 e_err(drv, "VF mtu %d out of range\n", new_mtu);
312 return;
313 }
314
315 max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
316 IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
317 if (max_frs < new_mtu) {
318 max_frs = new_mtu << IXGBE_MHADD_MFS_SHIFT;
319 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
320 }
321
322 e_info(hw, "VF requests change max MTU to %d\n", new_mtu);
323}
324
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000325static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
Greg Rose17367272010-01-09 02:25:48 +0000326{
327 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
Greg Rosef0412772010-05-04 22:11:46 +0000328 vmolr |= (IXGBE_VMOLR_ROMPE |
Greg Rose17367272010-01-09 02:25:48 +0000329 IXGBE_VMOLR_BAM);
Greg Rosef0412772010-05-04 22:11:46 +0000330 if (aupe)
331 vmolr |= IXGBE_VMOLR_AUPE;
332 else
333 vmolr &= ~IXGBE_VMOLR_AUPE;
Greg Rose17367272010-01-09 02:25:48 +0000334 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
335}
336
Greg Rose7f016482010-05-04 22:12:06 +0000337static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
338{
339 struct ixgbe_hw *hw = &adapter->hw;
340
341 if (vid)
342 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
343 (vid | IXGBE_VMVIR_VLANA_DEFAULT));
344 else
345 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
346}
347
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000348static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000349{
350 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck28500622010-06-15 09:25:48 +0000351 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
Greg Rose17367272010-01-09 02:25:48 +0000352
353 /* reset offloads to defaults */
Greg Rose7f016482010-05-04 22:12:06 +0000354 if (adapter->vfinfo[vf].pf_vlan) {
355 ixgbe_set_vf_vlan(adapter, true,
356 adapter->vfinfo[vf].pf_vlan, vf);
357 ixgbe_set_vmvir(adapter,
358 (adapter->vfinfo[vf].pf_vlan |
359 (adapter->vfinfo[vf].pf_qos <<
360 VLAN_PRIO_SHIFT)), vf);
361 ixgbe_set_vmolr(hw, vf, false);
362 } else {
363 ixgbe_set_vmvir(adapter, 0, vf);
364 ixgbe_set_vmolr(hw, vf, true);
365 }
Greg Rose17367272010-01-09 02:25:48 +0000366
367 /* reset multicast table array for vf */
368 adapter->vfinfo[vf].num_vf_mc_hashes = 0;
369
370 /* Flush and reset the mta with the new values */
371 ixgbe_set_rx_mode(adapter->netdev);
372
Alexander Duyck28500622010-06-15 09:25:48 +0000373 hw->mac.ops.clear_rar(hw, rar_entry);
Greg Rose17367272010-01-09 02:25:48 +0000374}
375
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000376static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
377 int vf, unsigned char *mac_addr)
Greg Rose17367272010-01-09 02:25:48 +0000378{
379 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck28500622010-06-15 09:25:48 +0000380 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
Greg Rose17367272010-01-09 02:25:48 +0000381
382 memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
Alexander Duyck28500622010-06-15 09:25:48 +0000383 hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
Greg Rose17367272010-01-09 02:25:48 +0000384
385 return 0;
386}
387
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000388static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
389 int vf, int index, unsigned char *mac_addr)
390{
391 struct ixgbe_hw *hw = &adapter->hw;
392 struct list_head *pos;
393 struct vf_macvlans *entry;
394
395 if (index <= 1) {
396 list_for_each(pos, &adapter->vf_mvs.l) {
397 entry = list_entry(pos, struct vf_macvlans, l);
398 if (entry->vf == vf) {
399 entry->vf = -1;
400 entry->free = true;
401 entry->is_macvlan = false;
402 hw->mac.ops.clear_rar(hw, entry->rar_entry);
403 }
404 }
405 }
406
407 /*
408 * If index was zero then we were asked to clear the uc list
409 * for the VF. We're done.
410 */
411 if (!index)
412 return 0;
413
414 entry = NULL;
415
416 list_for_each(pos, &adapter->vf_mvs.l) {
417 entry = list_entry(pos, struct vf_macvlans, l);
418 if (entry->free)
419 break;
420 }
421
422 /*
423 * If we traversed the entire list and didn't find a free entry
424 * then we're out of space on the RAR table. Also entry may
425 * be NULL because the original memory allocation for the list
426 * failed, which is not fatal but does mean we can't support
427 * VF requests for MACVLAN because we couldn't allocate
428 * memory for the list management required.
429 */
430 if (!entry || !entry->free)
431 return -ENOSPC;
432
433 entry->free = false;
434 entry->is_macvlan = true;
435 entry->vf = vf;
436 memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
437
438 hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
439
440 return 0;
441}
442
Greg Rosec6bda302011-08-24 02:37:55 +0000443int ixgbe_check_vf_assignment(struct ixgbe_adapter *adapter)
444{
445 int i;
446 for (i = 0; i < adapter->num_vfs; i++) {
447 if (adapter->vfinfo[i].vfdev->dev_flags &
448 PCI_DEV_FLAGS_ASSIGNED)
449 return true;
450 }
451 return false;
452}
453
Greg Rose17367272010-01-09 02:25:48 +0000454int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
455{
456 unsigned char vf_mac_addr[6];
Alexander Duyckc60fbb02010-11-16 19:26:54 -0800457 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
Greg Rose17367272010-01-09 02:25:48 +0000458 unsigned int vfn = (event_mask & 0x3f);
Greg Rosec6bda302011-08-24 02:37:55 +0000459 struct pci_dev *pvfdev;
460 unsigned int device_id;
461 u16 thisvf_devfn = (pdev->devfn + 0x80 + (vfn << 1)) |
462 (pdev->devfn & 1);
Greg Rose17367272010-01-09 02:25:48 +0000463
464 bool enable = ((event_mask & 0x10000000U) != 0);
465
466 if (enable) {
467 random_ether_addr(vf_mac_addr);
Emil Tantilov396e7992010-07-01 20:05:12 +0000468 e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
469 vfn, vf_mac_addr);
Greg Rose17367272010-01-09 02:25:48 +0000470 /*
471 * Store away the VF "permananet" MAC address, it will ask
472 * for it later.
473 */
474 memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
Greg Rosec6bda302011-08-24 02:37:55 +0000475
476 switch (adapter->hw.mac.type) {
477 case ixgbe_mac_82599EB:
478 device_id = IXGBE_DEV_ID_82599_VF;
479 break;
480 case ixgbe_mac_X540:
481 device_id = IXGBE_DEV_ID_X540_VF;
482 break;
483 default:
484 device_id = 0;
485 break;
486 }
487
488 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID, device_id, NULL);
489 while (pvfdev) {
490 if (pvfdev->devfn == thisvf_devfn)
491 break;
492 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
493 device_id, pvfdev);
494 }
495 if (pvfdev)
496 adapter->vfinfo[vfn].vfdev = pvfdev;
497 else
498 e_err(drv, "Couldn't find pci dev ptr for VF %4.4x\n",
499 thisvf_devfn);
Greg Rose17367272010-01-09 02:25:48 +0000500 }
501
502 return 0;
503}
504
Emil Tantilov5d5b7c32010-10-12 22:20:59 +0000505static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
Greg Rose17367272010-01-09 02:25:48 +0000506{
507 struct ixgbe_hw *hw = &adapter->hw;
508 u32 reg;
509 u32 reg_offset, vf_shift;
510
511 vf_shift = vf % 32;
512 reg_offset = vf / 32;
513
514 /* enable transmit and receive for vf */
515 reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
516 reg |= (reg | (1 << vf_shift));
517 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
518
519 reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
520 reg |= (reg | (1 << vf_shift));
521 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
522
Greg Rosea985b6c32010-11-18 03:02:52 +0000523 /* Enable counting of spoofed packets in the SSVPC register */
524 reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
525 reg |= (1 << vf_shift);
526 IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
527
Greg Rose17367272010-01-09 02:25:48 +0000528 ixgbe_vf_reset_event(adapter, vf);
529}
530
531static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
532{
533 u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
Emil Tantilovc0509992011-05-07 06:49:18 +0000534 u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
Greg Rose17367272010-01-09 02:25:48 +0000535 struct ixgbe_hw *hw = &adapter->hw;
536 s32 retval;
537 int entries;
538 u16 *hash_list;
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000539 int add, vid, index;
Greg Rosed3306c22010-11-18 03:03:23 +0000540 u8 *new_mac;
Greg Rose17367272010-01-09 02:25:48 +0000541
542 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
543
544 if (retval)
Emil Tantilov849c4542010-06-03 16:53:41 +0000545 pr_err("Error receiving message from VF\n");
Greg Rose17367272010-01-09 02:25:48 +0000546
547 /* this is a message we already processed, do nothing */
548 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
549 return retval;
550
551 /*
552 * until the vf completes a virtual function reset it should not be
553 * allowed to start any configuration.
554 */
555
556 if (msgbuf[0] == IXGBE_VF_RESET) {
557 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
Greg Rosed3306c22010-11-18 03:03:23 +0000558 new_mac = (u8 *)(&msgbuf[1]);
Emil Tantilov396e7992010-07-01 20:05:12 +0000559 e_info(probe, "VF Reset msg received from vf %d\n", vf);
Greg Rose17367272010-01-09 02:25:48 +0000560 adapter->vfinfo[vf].clear_to_send = false;
561 ixgbe_vf_reset_msg(adapter, vf);
562 adapter->vfinfo[vf].clear_to_send = true;
563
Greg Rosed3306c22010-11-18 03:03:23 +0000564 if (is_valid_ether_addr(new_mac) &&
565 !adapter->vfinfo[vf].pf_set_mac)
566 ixgbe_set_vf_mac(adapter, vf, vf_mac);
567 else
568 ixgbe_set_vf_mac(adapter,
569 vf, adapter->vfinfo[vf].vf_mac_addresses);
570
Greg Rose17367272010-01-09 02:25:48 +0000571 /* reply to reset with ack and vf mac address */
572 msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
Greg Rosed3306c22010-11-18 03:03:23 +0000573 memcpy(new_mac, vf_mac, IXGBE_ETH_LENGTH_OF_ADDRESS);
Greg Rose17367272010-01-09 02:25:48 +0000574 /*
575 * Piggyback the multicast filter type so VF can compute the
576 * correct vectors
577 */
578 msgbuf[3] = hw->mac.mc_filter_type;
579 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
580
581 return retval;
582 }
583
584 if (!adapter->vfinfo[vf].clear_to_send) {
585 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
586 ixgbe_write_mbx(hw, msgbuf, 1, vf);
587 return retval;
588 }
589
590 switch ((msgbuf[0] & 0xFFFF)) {
591 case IXGBE_VF_SET_MAC_ADDR:
Greg Rosed3306c22010-11-18 03:03:23 +0000592 new_mac = ((u8 *)(&msgbuf[1]));
593 if (is_valid_ether_addr(new_mac) &&
594 !adapter->vfinfo[vf].pf_set_mac) {
595 ixgbe_set_vf_mac(adapter, vf, new_mac);
596 } else if (memcmp(adapter->vfinfo[vf].vf_mac_addresses,
597 new_mac, ETH_ALEN)) {
598 e_warn(drv, "VF %d attempted to override "
599 "administratively set MAC address\nReload "
600 "the VF driver to resume operations\n", vf);
601 retval = -1;
Greg Rose17367272010-01-09 02:25:48 +0000602 }
603 break;
604 case IXGBE_VF_SET_MULTICAST:
605 entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
606 >> IXGBE_VT_MSGINFO_SHIFT;
607 hash_list = (u16 *)&msgbuf[1];
608 retval = ixgbe_set_vf_multicasts(adapter, entries,
609 hash_list, vf);
610 break;
611 case IXGBE_VF_SET_LPE:
Greg Rosee9f98072011-01-26 01:06:07 +0000612 ixgbe_set_vf_lpe(adapter, msgbuf);
Greg Rose17367272010-01-09 02:25:48 +0000613 break;
614 case IXGBE_VF_SET_VLAN:
615 add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
616 >> IXGBE_VT_MSGINFO_SHIFT;
617 vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
Greg Rosed3306c22010-11-18 03:03:23 +0000618 if (adapter->vfinfo[vf].pf_vlan) {
619 e_warn(drv, "VF %d attempted to override "
620 "administratively set VLAN configuration\n"
621 "Reload the VF driver to resume operations\n",
622 vf);
623 retval = -1;
624 } else {
Greg Rosede4c7f62011-09-29 05:57:33 +0000625 if (add)
626 adapter->vfinfo[vf].vlan_count++;
627 else if (adapter->vfinfo[vf].vlan_count)
628 adapter->vfinfo[vf].vlan_count--;
Greg Rosed3306c22010-11-18 03:03:23 +0000629 retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
Greg Rosede4c7f62011-09-29 05:57:33 +0000630 if (!retval && adapter->vfinfo[vf].spoofchk_enabled)
631 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
Greg Rosed3306c22010-11-18 03:03:23 +0000632 }
Greg Rose17367272010-01-09 02:25:48 +0000633 break;
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000634 case IXGBE_VF_SET_MACVLAN:
635 index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
636 IXGBE_VT_MSGINFO_SHIFT;
637 /*
638 * If the VF is allowed to set MAC filters then turn off
639 * anti-spoofing to avoid false positives. An index
640 * greater than 0 will indicate the VF is setting a
641 * macvlan MAC filter.
642 */
Greg Rosede4c7f62011-09-29 05:57:33 +0000643 if (index > 0 && adapter->vfinfo[vf].spoofchk_enabled)
644 ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000645 retval = ixgbe_set_vf_macvlan(adapter, vf, index,
646 (unsigned char *)(&msgbuf[1]));
647 break;
Greg Rose17367272010-01-09 02:25:48 +0000648 default:
Emil Tantilov396e7992010-07-01 20:05:12 +0000649 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
Greg Rose17367272010-01-09 02:25:48 +0000650 retval = IXGBE_ERR_MBX;
651 break;
652 }
653
654 /* notify the VF of the results of what it sent us */
655 if (retval)
656 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
657 else
658 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
659
660 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
661
662 ixgbe_write_mbx(hw, msgbuf, 1, vf);
663
664 return retval;
665}
666
667static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
668{
669 struct ixgbe_hw *hw = &adapter->hw;
670 u32 msg = IXGBE_VT_MSGTYPE_NACK;
671
672 /* if device isn't clear to send it shouldn't be reading either */
673 if (!adapter->vfinfo[vf].clear_to_send)
674 ixgbe_write_mbx(hw, &msg, 1, vf);
675}
676
677void ixgbe_msg_task(struct ixgbe_adapter *adapter)
678{
679 struct ixgbe_hw *hw = &adapter->hw;
680 u32 vf;
681
682 for (vf = 0; vf < adapter->num_vfs; vf++) {
683 /* process any reset requests */
684 if (!ixgbe_check_for_rst(hw, vf))
685 ixgbe_vf_reset_event(adapter, vf);
686
687 /* process any messages pending */
688 if (!ixgbe_check_for_msg(hw, vf))
689 ixgbe_rcv_msg_from_vf(adapter, vf);
690
691 /* process any acks */
692 if (!ixgbe_check_for_ack(hw, vf))
693 ixgbe_rcv_ack_from_vf(adapter, vf);
694 }
695}
696
Greg Rose767081a2010-01-22 22:46:40 +0000697void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
698{
699 struct ixgbe_hw *hw = &adapter->hw;
700
701 /* disable transmit and receive for all vfs */
702 IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
703 IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
704
705 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
706 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
707}
708
709void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
710{
711 struct ixgbe_hw *hw = &adapter->hw;
712 u32 ping;
713 int i;
714
715 for (i = 0 ; i < adapter->num_vfs; i++) {
716 ping = IXGBE_PF_CONTROL_MSG;
717 if (adapter->vfinfo[i].clear_to_send)
718 ping |= IXGBE_VT_MSGTYPE_CTS;
719 ixgbe_write_mbx(hw, &ping, 1, i);
720 }
721}
722
Greg Rose7f016482010-05-04 22:12:06 +0000723int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
724{
725 struct ixgbe_adapter *adapter = netdev_priv(netdev);
726 if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
727 return -EINVAL;
728 adapter->vfinfo[vf].pf_set_mac = true;
729 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
730 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
731 " change effective.");
732 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
733 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
734 " but the PF device is not up.\n");
735 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
736 " attempting to use the VF device.\n");
737 }
738 return ixgbe_set_vf_mac(adapter, vf, mac);
739}
740
741int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
742{
743 int err = 0;
744 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Greg Rosea985b6c32010-11-18 03:02:52 +0000745 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose7f016482010-05-04 22:12:06 +0000746
747 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
748 return -EINVAL;
749 if (vlan || qos) {
750 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
751 if (err)
752 goto out;
753 ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
Greg Rosea985b6c32010-11-18 03:02:52 +0000754 ixgbe_set_vmolr(hw, vf, false);
Greg Rosede4c7f62011-09-29 05:57:33 +0000755 if (adapter->vfinfo[vf].spoofchk_enabled)
Greg Rosea1cbb15c2011-05-13 01:33:48 +0000756 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
Greg Rosede4c7f62011-09-29 05:57:33 +0000757 adapter->vfinfo[vf].vlan_count++;
Greg Rose7f016482010-05-04 22:12:06 +0000758 adapter->vfinfo[vf].pf_vlan = vlan;
759 adapter->vfinfo[vf].pf_qos = qos;
760 dev_info(&adapter->pdev->dev,
761 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
762 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
763 dev_warn(&adapter->pdev->dev,
764 "The VF VLAN has been set,"
765 " but the PF device is not up.\n");
766 dev_warn(&adapter->pdev->dev,
767 "Bring the PF device up before"
768 " attempting to use the VF device.\n");
769 }
770 } else {
771 err = ixgbe_set_vf_vlan(adapter, false,
772 adapter->vfinfo[vf].pf_vlan, vf);
773 ixgbe_set_vmvir(adapter, vlan, vf);
Greg Rosea985b6c32010-11-18 03:02:52 +0000774 ixgbe_set_vmolr(hw, vf, true);
775 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
Greg Rosede4c7f62011-09-29 05:57:33 +0000776 if (adapter->vfinfo[vf].vlan_count)
777 adapter->vfinfo[vf].vlan_count--;
Greg Rose7f016482010-05-04 22:12:06 +0000778 adapter->vfinfo[vf].pf_vlan = 0;
779 adapter->vfinfo[vf].pf_qos = 0;
780 }
781out:
782 return err;
783}
784
Lior Levyff4ab202011-03-11 02:03:07 +0000785static int ixgbe_link_mbps(int internal_link_speed)
786{
787 switch (internal_link_speed) {
788 case IXGBE_LINK_SPEED_100_FULL:
789 return 100;
790 case IXGBE_LINK_SPEED_1GB_FULL:
791 return 1000;
792 case IXGBE_LINK_SPEED_10GB_FULL:
793 return 10000;
794 default:
795 return 0;
796 }
797}
798
799static void ixgbe_set_vf_rate_limit(struct ixgbe_hw *hw, int vf, int tx_rate,
800 int link_speed)
801{
802 int rf_dec, rf_int;
803 u32 bcnrc_val;
804
805 if (tx_rate != 0) {
806 /* Calculate the rate factor values to set */
807 rf_int = link_speed / tx_rate;
808 rf_dec = (link_speed - (rf_int * tx_rate));
809 rf_dec = (rf_dec * (1<<IXGBE_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
810
811 bcnrc_val = IXGBE_RTTBCNRC_RS_ENA;
812 bcnrc_val |= ((rf_int<<IXGBE_RTTBCNRC_RF_INT_SHIFT) &
813 IXGBE_RTTBCNRC_RF_INT_MASK);
814 bcnrc_val |= (rf_dec & IXGBE_RTTBCNRC_RF_DEC_MASK);
815 } else {
816 bcnrc_val = 0;
817 }
818
819 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, 2*vf); /* vf Y uses queue 2*Y */
Lior Levy7555e832011-06-25 00:09:08 -0700820 /*
821 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
822 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
823 * and 0x004 otherwise.
824 */
825 switch (hw->mac.type) {
826 case ixgbe_mac_82599EB:
827 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
828 break;
829 case ixgbe_mac_X540:
830 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
831 break;
832 default:
833 break;
834 }
835
Lior Levyff4ab202011-03-11 02:03:07 +0000836 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
837}
838
839void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
840{
841 int actual_link_speed, i;
842 bool reset_rate = false;
843
844 /* VF Tx rate limit was not set */
845 if (adapter->vf_rate_link_speed == 0)
846 return;
847
848 actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
849 if (actual_link_speed != adapter->vf_rate_link_speed) {
850 reset_rate = true;
851 adapter->vf_rate_link_speed = 0;
852 dev_info(&adapter->pdev->dev,
853 "Link speed has been changed. VF Transmit rate "
854 "is disabled\n");
855 }
856
857 for (i = 0; i < adapter->num_vfs; i++) {
858 if (reset_rate)
859 adapter->vfinfo[i].tx_rate = 0;
860
861 ixgbe_set_vf_rate_limit(&adapter->hw, i,
862 adapter->vfinfo[i].tx_rate,
863 actual_link_speed);
864 }
865}
866
Greg Rose7f016482010-05-04 22:12:06 +0000867int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
868{
Lior Levyff4ab202011-03-11 02:03:07 +0000869 struct ixgbe_adapter *adapter = netdev_priv(netdev);
870 struct ixgbe_hw *hw = &adapter->hw;
871 int actual_link_speed;
872
873 actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
874 if ((vf >= adapter->num_vfs) || (!adapter->link_up) ||
875 (tx_rate > actual_link_speed) || (actual_link_speed != 10000) ||
876 ((tx_rate != 0) && (tx_rate <= 10)))
877 /* rate limit cannot be set to 10Mb or less in 10Gb adapters */
878 return -EINVAL;
879
880 adapter->vf_rate_link_speed = actual_link_speed;
881 adapter->vfinfo[vf].tx_rate = (u16)tx_rate;
882 ixgbe_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
883
884 return 0;
Greg Rose7f016482010-05-04 22:12:06 +0000885}
886
Greg Rosede4c7f62011-09-29 05:57:33 +0000887int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
888{
889 struct ixgbe_adapter *adapter = netdev_priv(netdev);
890 int vf_target_reg = vf >> 3;
891 int vf_target_shift = vf % 8;
892 struct ixgbe_hw *hw = &adapter->hw;
893 u32 regval;
894
895 adapter->vfinfo[vf].spoofchk_enabled = setting;
896
897 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
898 regval &= ~(1 << vf_target_shift);
899 regval |= (setting << vf_target_shift);
900 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
901
902 if (adapter->vfinfo[vf].vlan_count) {
903 vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
904 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
905 regval &= ~(1 << vf_target_shift);
906 regval |= (setting << vf_target_shift);
907 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
908 }
909
910 return 0;
911}
912
Greg Rose7f016482010-05-04 22:12:06 +0000913int ixgbe_ndo_get_vf_config(struct net_device *netdev,
914 int vf, struct ifla_vf_info *ivi)
915{
916 struct ixgbe_adapter *adapter = netdev_priv(netdev);
917 if (vf >= adapter->num_vfs)
918 return -EINVAL;
919 ivi->vf = vf;
920 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
Lior Levyff4ab202011-03-11 02:03:07 +0000921 ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
Greg Rose7f016482010-05-04 22:12:06 +0000922 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
923 ivi->qos = adapter->vfinfo[vf].pf_qos;
Greg Rosede4c7f62011-09-29 05:57:33 +0000924 ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
Greg Rose7f016482010-05-04 22:12:06 +0000925 return 0;
926}