ethernet: Use eth_random_addr
[linux-2.6/libata-dev.git] / drivers / net / ethernet / intel / ixgbe / ixgbe_sriov.c
blobeb3f67c854a74f165f7f7c0d7f84eddad9ee0420
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)
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 eth_random_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");
549 return retval;
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;
556 /* flush the ack before we write any messages back */
557 IXGBE_WRITE_FLUSH(hw);
560 * until the vf completes a virtual function reset it should not be
561 * allowed to start any configuration.
564 if (msgbuf[0] == IXGBE_VF_RESET) {
565 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
566 new_mac = (u8 *)(&msgbuf[1]);
567 e_info(probe, "VF Reset msg received from vf %d\n", vf);
568 adapter->vfinfo[vf].clear_to_send = false;
569 ixgbe_vf_reset_msg(adapter, vf);
570 adapter->vfinfo[vf].clear_to_send = true;
572 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);
579 /* reply to reset with ack and vf mac address */
580 msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
581 memcpy(new_mac, vf_mac, ETH_ALEN);
583 * Piggyback the multicast filter type so VF can compute the
584 * correct vectors
586 msgbuf[3] = hw->mac.mc_filter_type;
587 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
589 return retval;
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;
598 switch ((msgbuf[0] & 0xFFFF)) {
599 case IXGBE_VF_SET_MAC_ADDR:
600 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;
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:
620 ixgbe_set_vf_lpe(adapter, msgbuf);
621 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);
626 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 {
633 if (add)
634 adapter->vfinfo[vf].vlan_count++;
635 else if (adapter->vfinfo[vf].vlan_count)
636 adapter->vfinfo[vf].vlan_count--;
637 retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
638 if (!retval && adapter->vfinfo[vf].spoofchk_enabled)
639 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
641 break;
642 case IXGBE_VF_SET_MACVLAN:
643 index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
644 IXGBE_VT_MSGINFO_SHIFT;
645 if (adapter->vfinfo[vf].pf_set_mac && index > 0) {
646 e_warn(drv, "VF %d requested MACVLAN filter but is "
647 "administratively denied\n", vf);
648 retval = -1;
649 break;
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.
657 if (index > 0 && adapter->vfinfo[vf].spoofchk_enabled)
658 ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
659 retval = ixgbe_set_vf_macvlan(adapter, vf, index,
660 (unsigned char *)(&msgbuf[1]));
661 if (retval == -ENOSPC)
662 e_warn(drv, "VF %d has requested a MACVLAN filter "
663 "but there is no space for it\n", vf);
664 break;
665 default:
666 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
667 retval = IXGBE_ERR_MBX;
668 break;
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;
677 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
679 ixgbe_write_mbx(hw, msgbuf, 1, vf);
681 return retval;
684 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
686 struct ixgbe_hw *hw = &adapter->hw;
687 u32 msg = IXGBE_VT_MSGTYPE_NACK;
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);
694 void ixgbe_msg_task(struct ixgbe_adapter *adapter)
696 struct ixgbe_hw *hw = &adapter->hw;
697 u32 vf;
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);
704 /* process any messages pending */
705 if (!ixgbe_check_for_msg(hw, vf))
706 ixgbe_rcv_msg_from_vf(adapter, vf);
708 /* process any acks */
709 if (!ixgbe_check_for_ack(hw, vf))
710 ixgbe_rcv_ack_from_vf(adapter, vf);
714 void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
716 struct ixgbe_hw *hw = &adapter->hw;
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);
722 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
723 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
726 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
728 struct ixgbe_hw *hw = &adapter->hw;
729 u32 ping;
730 int i;
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);
740 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
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");
755 return ixgbe_set_vf_mac(adapter, vf, mac);
758 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
760 int err = 0;
761 struct ixgbe_adapter *adapter = netdev_priv(netdev);
762 struct ixgbe_hw *hw = &adapter->hw;
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);
771 ixgbe_set_vmolr(hw, vf, false);
772 if (adapter->vfinfo[vf].spoofchk_enabled)
773 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
774 adapter->vfinfo[vf].vlan_count++;
775 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");
787 } else {
788 err = ixgbe_set_vf_vlan(adapter, false,
789 adapter->vfinfo[vf].pf_vlan, vf);
790 ixgbe_set_vmvir(adapter, vlan, vf);
791 ixgbe_set_vmolr(hw, vf, true);
792 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
793 if (adapter->vfinfo[vf].vlan_count)
794 adapter->vfinfo[vf].vlan_count--;
795 adapter->vfinfo[vf].pf_vlan = 0;
796 adapter->vfinfo[vf].pf_qos = 0;
798 out:
799 return err;
802 static int ixgbe_link_mbps(int internal_link_speed)
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;
816 static void ixgbe_set_vf_rate_limit(struct ixgbe_hw *hw, int vf, int tx_rate,
817 int link_speed)
819 int rf_dec, rf_int;
820 u32 bcnrc_val;
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;
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;
836 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, 2*vf); /* vf Y uses queue 2*Y */
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.
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;
853 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
856 void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
858 int actual_link_speed, i;
859 bool reset_rate = false;
861 /* VF Tx rate limit was not set */
862 if (adapter->vf_rate_link_speed == 0)
863 return;
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");
874 for (i = 0; i < adapter->num_vfs; i++) {
875 if (reset_rate)
876 adapter->vfinfo[i].tx_rate = 0;
878 ixgbe_set_vf_rate_limit(&adapter->hw, i,
879 adapter->vfinfo[i].tx_rate,
880 actual_link_speed);
884 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
886 struct ixgbe_adapter *adapter = netdev_priv(netdev);
887 struct ixgbe_hw *hw = &adapter->hw;
888 int actual_link_speed;
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;
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);
901 return 0;
904 int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
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;
912 adapter->vfinfo[vf].spoofchk_enabled = setting;
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);
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);
927 return 0;
930 int ixgbe_ndo_get_vf_config(struct net_device *netdev,
931 int vf, struct ifla_vf_info *ivi)
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);
938 ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
939 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
940 ivi->qos = adapter->vfinfo[vf].pf_qos;
941 ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
942 return 0;