Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / net / ethernet / intel / ixgbe / ixgbe_sriov.c
blobb01ecb4d2bb1aaea3cc94f6ad64cd66bada6c38e
1 /*******************************************************************************
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2012 Intel Corporation.
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.
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.
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.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
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
26 *******************************************************************************/
28 #include <linux/types.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/netdevice.h>
32 #include <linux/vmalloc.h>
33 #include <linux/string.h>
34 #include <linux/in.h>
35 #include <linux/ip.h>
36 #include <linux/tcp.h>
37 #include <linux/ipv6.h>
38 #ifdef NETIF_F_HW_VLAN_TX
39 #include <linux/if_vlan.h>
40 #endif
42 #include "ixgbe.h"
43 #include "ixgbe_type.h"
44 #include "ixgbe_sriov.h"
46 #ifdef CONFIG_PCI_IOV
47 static int ixgbe_find_enabled_vfs(struct ixgbe_adapter *adapter)
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;
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;
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 (pvfdev->bus->number >= pdev->bus->number))
72 vfs_found++;
73 vf_devfn += 2;
74 pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
75 device_id, pvfdev);
78 return vfs_found;
81 void ixgbe_enable_sriov(struct ixgbe_adapter *adapter,
82 const struct ixgbe_info *ii)
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;
90 pre_existing_vfs = ixgbe_find_enabled_vfs(adapter);
91 if (!pre_existing_vfs && !adapter->num_vfs)
92 return;
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.
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);
111 if (err) {
112 e_err(probe, "Failed to enable PCI sriov: %d\n", err);
113 goto err_novfs;
115 adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
117 e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);
119 num_vf_macvlans = hw->mac.num_rar_entries -
120 (IXGBE_MAX_PF_MACVLANS + 1 + adapter->num_vfs);
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++;
138 /* If call to enable VFs succeeded then allocate memory
139 * for per VF control structures.
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
148 ixgbe_init_mbx_params_pf(hw);
149 memcpy(&hw->mbx.ops, ii->mbx_ops,
150 sizeof(hw->mbx.ops));
152 /* Disable RSC when in SR-IOV mode */
153 adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
154 IXGBE_FLAG2_RSC_ENABLED);
155 for (i = 0; i < adapter->num_vfs; i++)
156 adapter->vfinfo[i].spoofchk_enabled = true;
157 return;
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);
165 err_novfs:
166 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
167 adapter->num_vfs = 0;
169 #endif /* #ifdef CONFIG_PCI_IOV */
171 void ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
173 struct ixgbe_hw *hw = &adapter->hw;
174 u32 gcr;
175 u32 gpie;
176 u32 vmdctl;
177 int i;
179 #ifdef CONFIG_PCI_IOV
180 /* disable iov and allow time for transactions to clear */
181 pci_disable_sriov(adapter->pdev);
182 #endif
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);
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);
198 /* take a breather then clean up driver data */
199 msleep(100);
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);
206 kfree(adapter->vfinfo);
207 kfree(adapter->mv_list);
208 adapter->vfinfo = NULL;
210 adapter->num_vfs = 0;
211 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
214 static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
215 int entries, u16 *hash_list, u32 vf)
217 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
218 struct ixgbe_hw *hw = &adapter->hw;
219 int i;
220 u32 vector_bit;
221 u32 vector_reg;
222 u32 mta_reg;
224 /* only so many hash values supported */
225 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
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
232 vfinfo->num_vf_mc_hashes = entries;
235 * VFs are limited to using the MTA hash table for their multicast
236 * addresses
238 for (i = 0; i < entries; i++) {
239 vfinfo->vf_mc_hashes[i] = hash_list[i];
242 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);
250 return 0;
253 static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
255 struct ixgbe_hw *hw = &adapter->hw;
256 struct list_head *pos;
257 struct vf_macvlans *entry;
259 list_for_each(pos, &adapter->vf_mvs.l) {
260 entry = list_entry(pos, struct vf_macvlans, l);
261 if (entry->free == false)
262 hw->mac.ops.set_rar(hw, entry->rar_entry,
263 entry->vf_macvlan,
264 entry->vf, IXGBE_RAH_AV);
268 void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
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;
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);
289 /* Restore any VF macvlans */
290 ixgbe_restore_vf_macvlans(adapter);
293 static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
294 u32 vf)
296 return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
299 static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
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;
306 /* Only X540 supports jumbo frames in IOV mode */
307 if (adapter->hw.mac.type != ixgbe_mac_X540)
308 return;
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;
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);
323 e_info(hw, "VF requests change max MTU to %d\n", new_mtu);
326 static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
328 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
329 vmolr |= (IXGBE_VMOLR_ROMPE |
330 IXGBE_VMOLR_BAM);
331 if (aupe)
332 vmolr |= IXGBE_VMOLR_AUPE;
333 else
334 vmolr &= ~IXGBE_VMOLR_AUPE;
335 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
338 static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
340 struct ixgbe_hw *hw = &adapter->hw;
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);
349 static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
351 struct ixgbe_hw *hw = &adapter->hw;
352 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
354 /* reset offloads to defaults */
355 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);
368 /* reset multicast table array for vf */
369 adapter->vfinfo[vf].num_vf_mc_hashes = 0;
371 /* Flush and reset the mta with the new values */
372 ixgbe_set_rx_mode(adapter->netdev);
374 hw->mac.ops.clear_rar(hw, rar_entry);
377 static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
378 int vf, unsigned char *mac_addr)
380 struct ixgbe_hw *hw = &adapter->hw;
381 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
383 memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
384 hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
386 return 0;
389 static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
390 int vf, int index, unsigned char *mac_addr)
392 struct ixgbe_hw *hw = &adapter->hw;
393 struct list_head *pos;
394 struct vf_macvlans *entry;
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);
409 * If index was zero then we were asked to clear the uc list
410 * for the VF. We're done.
412 if (!index)
413 return 0;
415 entry = NULL;
417 list_for_each(pos, &adapter->vf_mvs.l) {
418 entry = list_entry(pos, struct vf_macvlans, l);
419 if (entry->free)
420 break;
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.
431 if (!entry || !entry->free)
432 return -ENOSPC;
434 entry->free = false;
435 entry->is_macvlan = true;
436 entry->vf = vf;
437 memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
439 hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
441 return 0;
444 int ixgbe_check_vf_assignment(struct ixgbe_adapter *adapter)
446 #ifdef CONFIG_PCI_IOV
447 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;
453 #endif
454 return false;
457 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
459 unsigned char vf_mac_addr[6];
460 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
461 unsigned int vfn = (event_mask & 0x3f);
462 struct pci_dev *pvfdev;
463 unsigned int device_id;
464 u16 thisvf_devfn = (pdev->devfn + 0x80 + (vfn << 1)) |
465 (pdev->devfn & 1);
467 bool enable = ((event_mask & 0x10000000U) != 0);
469 if (enable) {
470 random_ether_addr(vf_mac_addr);
471 e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
472 vfn, vf_mac_addr);
474 * Store away the VF "permananet" MAC address, it will ask
475 * for it later.
477 memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
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;
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);
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);
505 return 0;
508 static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
510 struct ixgbe_hw *hw = &adapter->hw;
511 u32 reg;
512 u32 reg_offset, vf_shift;
514 vf_shift = vf % 32;
515 reg_offset = vf / 32;
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);
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);
526 /* 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);
531 ixgbe_vf_reset_event(adapter, vf);
534 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
536 u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
537 u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
538 struct ixgbe_hw *hw = &adapter->hw;
539 s32 retval;
540 int entries;
541 u16 *hash_list;
542 int add, vid, index;
543 u8 *new_mac;
545 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
547 if (retval)
548 pr_err("Error receiving message from VF\n");
550 /* this is a message we already processed, do nothing */
551 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
552 return retval;
555 * until the vf completes a virtual function reset it should not be
556 * allowed to start any configuration.
559 if (msgbuf[0] == IXGBE_VF_RESET) {
560 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
561 new_mac = (u8 *)(&msgbuf[1]);
562 e_info(probe, "VF Reset msg received from vf %d\n", vf);
563 adapter->vfinfo[vf].clear_to_send = false;
564 ixgbe_vf_reset_msg(adapter, vf);
565 adapter->vfinfo[vf].clear_to_send = true;
567 if (is_valid_ether_addr(new_mac) &&
568 !adapter->vfinfo[vf].pf_set_mac)
569 ixgbe_set_vf_mac(adapter, vf, vf_mac);
570 else
571 ixgbe_set_vf_mac(adapter,
572 vf, adapter->vfinfo[vf].vf_mac_addresses);
574 /* reply to reset with ack and vf mac address */
575 msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
576 memcpy(new_mac, vf_mac, ETH_ALEN);
578 * Piggyback the multicast filter type so VF can compute the
579 * correct vectors
581 msgbuf[3] = hw->mac.mc_filter_type;
582 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
584 return retval;
587 if (!adapter->vfinfo[vf].clear_to_send) {
588 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
589 ixgbe_write_mbx(hw, msgbuf, 1, vf);
590 return retval;
593 switch ((msgbuf[0] & 0xFFFF)) {
594 case IXGBE_VF_SET_MAC_ADDR:
595 new_mac = ((u8 *)(&msgbuf[1]));
596 if (is_valid_ether_addr(new_mac) &&
597 !adapter->vfinfo[vf].pf_set_mac) {
598 ixgbe_set_vf_mac(adapter, vf, new_mac);
599 } else if (memcmp(adapter->vfinfo[vf].vf_mac_addresses,
600 new_mac, ETH_ALEN)) {
601 e_warn(drv, "VF %d attempted to override "
602 "administratively set MAC address\nReload "
603 "the VF driver to resume operations\n", vf);
604 retval = -1;
606 break;
607 case IXGBE_VF_SET_MULTICAST:
608 entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
609 >> IXGBE_VT_MSGINFO_SHIFT;
610 hash_list = (u16 *)&msgbuf[1];
611 retval = ixgbe_set_vf_multicasts(adapter, entries,
612 hash_list, vf);
613 break;
614 case IXGBE_VF_SET_LPE:
615 ixgbe_set_vf_lpe(adapter, msgbuf);
616 break;
617 case IXGBE_VF_SET_VLAN:
618 add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
619 >> IXGBE_VT_MSGINFO_SHIFT;
620 vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
621 if (adapter->vfinfo[vf].pf_vlan) {
622 e_warn(drv, "VF %d attempted to override "
623 "administratively set VLAN configuration\n"
624 "Reload the VF driver to resume operations\n",
625 vf);
626 retval = -1;
627 } else {
628 if (add)
629 adapter->vfinfo[vf].vlan_count++;
630 else if (adapter->vfinfo[vf].vlan_count)
631 adapter->vfinfo[vf].vlan_count--;
632 retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
633 if (!retval && adapter->vfinfo[vf].spoofchk_enabled)
634 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
636 break;
637 case IXGBE_VF_SET_MACVLAN:
638 index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
639 IXGBE_VT_MSGINFO_SHIFT;
641 * If the VF is allowed to set MAC filters then turn off
642 * anti-spoofing to avoid false positives. An index
643 * greater than 0 will indicate the VF is setting a
644 * macvlan MAC filter.
646 if (index > 0 && adapter->vfinfo[vf].spoofchk_enabled)
647 ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
648 retval = ixgbe_set_vf_macvlan(adapter, vf, index,
649 (unsigned char *)(&msgbuf[1]));
650 if (retval == -ENOSPC)
651 e_warn(drv, "VF %d has requested a MACVLAN filter "
652 "but there is no space for it\n", vf);
653 break;
654 default:
655 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
656 retval = IXGBE_ERR_MBX;
657 break;
660 /* notify the VF of the results of what it sent us */
661 if (retval)
662 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
663 else
664 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
666 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
668 ixgbe_write_mbx(hw, msgbuf, 1, vf);
670 return retval;
673 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
675 struct ixgbe_hw *hw = &adapter->hw;
676 u32 msg = IXGBE_VT_MSGTYPE_NACK;
678 /* if device isn't clear to send it shouldn't be reading either */
679 if (!adapter->vfinfo[vf].clear_to_send)
680 ixgbe_write_mbx(hw, &msg, 1, vf);
683 void ixgbe_msg_task(struct ixgbe_adapter *adapter)
685 struct ixgbe_hw *hw = &adapter->hw;
686 u32 vf;
688 for (vf = 0; vf < adapter->num_vfs; vf++) {
689 /* process any reset requests */
690 if (!ixgbe_check_for_rst(hw, vf))
691 ixgbe_vf_reset_event(adapter, vf);
693 /* process any messages pending */
694 if (!ixgbe_check_for_msg(hw, vf))
695 ixgbe_rcv_msg_from_vf(adapter, vf);
697 /* process any acks */
698 if (!ixgbe_check_for_ack(hw, vf))
699 ixgbe_rcv_ack_from_vf(adapter, vf);
703 void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
705 struct ixgbe_hw *hw = &adapter->hw;
707 /* disable transmit and receive for all vfs */
708 IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
709 IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
711 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
712 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
715 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
717 struct ixgbe_hw *hw = &adapter->hw;
718 u32 ping;
719 int i;
721 for (i = 0 ; i < adapter->num_vfs; i++) {
722 ping = IXGBE_PF_CONTROL_MSG;
723 if (adapter->vfinfo[i].clear_to_send)
724 ping |= IXGBE_VT_MSGTYPE_CTS;
725 ixgbe_write_mbx(hw, &ping, 1, i);
729 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
731 struct ixgbe_adapter *adapter = netdev_priv(netdev);
732 if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
733 return -EINVAL;
734 adapter->vfinfo[vf].pf_set_mac = true;
735 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
736 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
737 " change effective.");
738 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
739 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
740 " but the PF device is not up.\n");
741 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
742 " attempting to use the VF device.\n");
744 return ixgbe_set_vf_mac(adapter, vf, mac);
747 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
749 int err = 0;
750 struct ixgbe_adapter *adapter = netdev_priv(netdev);
751 struct ixgbe_hw *hw = &adapter->hw;
753 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
754 return -EINVAL;
755 if (vlan || qos) {
756 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
757 if (err)
758 goto out;
759 ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
760 ixgbe_set_vmolr(hw, vf, false);
761 if (adapter->vfinfo[vf].spoofchk_enabled)
762 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
763 adapter->vfinfo[vf].vlan_count++;
764 adapter->vfinfo[vf].pf_vlan = vlan;
765 adapter->vfinfo[vf].pf_qos = qos;
766 dev_info(&adapter->pdev->dev,
767 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
768 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
769 dev_warn(&adapter->pdev->dev,
770 "The VF VLAN has been set,"
771 " but the PF device is not up.\n");
772 dev_warn(&adapter->pdev->dev,
773 "Bring the PF device up before"
774 " attempting to use the VF device.\n");
776 } else {
777 err = ixgbe_set_vf_vlan(adapter, false,
778 adapter->vfinfo[vf].pf_vlan, vf);
779 ixgbe_set_vmvir(adapter, vlan, vf);
780 ixgbe_set_vmolr(hw, vf, true);
781 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
782 if (adapter->vfinfo[vf].vlan_count)
783 adapter->vfinfo[vf].vlan_count--;
784 adapter->vfinfo[vf].pf_vlan = 0;
785 adapter->vfinfo[vf].pf_qos = 0;
787 out:
788 return err;
791 static int ixgbe_link_mbps(int internal_link_speed)
793 switch (internal_link_speed) {
794 case IXGBE_LINK_SPEED_100_FULL:
795 return 100;
796 case IXGBE_LINK_SPEED_1GB_FULL:
797 return 1000;
798 case IXGBE_LINK_SPEED_10GB_FULL:
799 return 10000;
800 default:
801 return 0;
805 static void ixgbe_set_vf_rate_limit(struct ixgbe_hw *hw, int vf, int tx_rate,
806 int link_speed)
808 int rf_dec, rf_int;
809 u32 bcnrc_val;
811 if (tx_rate != 0) {
812 /* Calculate the rate factor values to set */
813 rf_int = link_speed / tx_rate;
814 rf_dec = (link_speed - (rf_int * tx_rate));
815 rf_dec = (rf_dec * (1<<IXGBE_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
817 bcnrc_val = IXGBE_RTTBCNRC_RS_ENA;
818 bcnrc_val |= ((rf_int<<IXGBE_RTTBCNRC_RF_INT_SHIFT) &
819 IXGBE_RTTBCNRC_RF_INT_MASK);
820 bcnrc_val |= (rf_dec & IXGBE_RTTBCNRC_RF_DEC_MASK);
821 } else {
822 bcnrc_val = 0;
825 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, 2*vf); /* vf Y uses queue 2*Y */
827 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
828 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
829 * and 0x004 otherwise.
831 switch (hw->mac.type) {
832 case ixgbe_mac_82599EB:
833 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
834 break;
835 case ixgbe_mac_X540:
836 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
837 break;
838 default:
839 break;
842 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
845 void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
847 int actual_link_speed, i;
848 bool reset_rate = false;
850 /* VF Tx rate limit was not set */
851 if (adapter->vf_rate_link_speed == 0)
852 return;
854 actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
855 if (actual_link_speed != adapter->vf_rate_link_speed) {
856 reset_rate = true;
857 adapter->vf_rate_link_speed = 0;
858 dev_info(&adapter->pdev->dev,
859 "Link speed has been changed. VF Transmit rate "
860 "is disabled\n");
863 for (i = 0; i < adapter->num_vfs; i++) {
864 if (reset_rate)
865 adapter->vfinfo[i].tx_rate = 0;
867 ixgbe_set_vf_rate_limit(&adapter->hw, i,
868 adapter->vfinfo[i].tx_rate,
869 actual_link_speed);
873 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
875 struct ixgbe_adapter *adapter = netdev_priv(netdev);
876 struct ixgbe_hw *hw = &adapter->hw;
877 int actual_link_speed;
879 actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
880 if ((vf >= adapter->num_vfs) || (!adapter->link_up) ||
881 (tx_rate > actual_link_speed) || (actual_link_speed != 10000) ||
882 ((tx_rate != 0) && (tx_rate <= 10)))
883 /* rate limit cannot be set to 10Mb or less in 10Gb adapters */
884 return -EINVAL;
886 adapter->vf_rate_link_speed = actual_link_speed;
887 adapter->vfinfo[vf].tx_rate = (u16)tx_rate;
888 ixgbe_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
890 return 0;
893 int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
895 struct ixgbe_adapter *adapter = netdev_priv(netdev);
896 int vf_target_reg = vf >> 3;
897 int vf_target_shift = vf % 8;
898 struct ixgbe_hw *hw = &adapter->hw;
899 u32 regval;
901 adapter->vfinfo[vf].spoofchk_enabled = setting;
903 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
904 regval &= ~(1 << vf_target_shift);
905 regval |= (setting << vf_target_shift);
906 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
908 if (adapter->vfinfo[vf].vlan_count) {
909 vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
910 regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
911 regval &= ~(1 << vf_target_shift);
912 regval |= (setting << vf_target_shift);
913 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
916 return 0;
919 int ixgbe_ndo_get_vf_config(struct net_device *netdev,
920 int vf, struct ifla_vf_info *ivi)
922 struct ixgbe_adapter *adapter = netdev_priv(netdev);
923 if (vf >= adapter->num_vfs)
924 return -EINVAL;
925 ivi->vf = vf;
926 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
927 ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
928 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
929 ivi->qos = adapter->vfinfo[vf].pf_qos;
930 ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
931 return 0;