igbvf: convert to ndo_fix_features
[pandora-kernel.git] / drivers / net / ethernet / intel / ixgbe / ixgbe_sriov.c
1 /*******************************************************************************
2
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2011 Intel Corporation.
5
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
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
41
42 #include "ixgbe.h"
43 #include "ixgbe_type.h"
44 #include "ixgbe_sriov.h"
45
46 #ifdef CONFIG_PCI_IOV
47 static 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
80 void 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);
154                 for (i = 0; i < adapter->num_vfs; i++)
155                         adapter->vfinfo[i].spoofchk_enabled = true;
156                 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
164 err_novfs:
165         adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
166         adapter->num_vfs = 0;
167 }
168 #endif /* #ifdef CONFIG_PCI_IOV */
169
170 void 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
213 static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
214                                    int entries, u16 *hash_list, u32 vf)
215 {
216         struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
217         struct ixgbe_hw *hw = &adapter->hw;
218         int i;
219         u32 vector_bit;
220         u32 vector_reg;
221         u32 mta_reg;
222
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++) {
238                 vfinfo->vf_mc_hashes[i] = hash_list[i];
239         }
240
241         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         }
248
249         return 0;
250 }
251
252 static 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
267 void 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         }
287
288         /* Restore any VF macvlans */
289         ixgbe_restore_vf_macvlans(adapter);
290 }
291
292 static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
293                              u32 vf)
294 {
295         return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
296 }
297
298 static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
299 {
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
325 static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
326 {
327         u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
328         vmolr |= (IXGBE_VMOLR_ROMPE |
329                   IXGBE_VMOLR_BAM);
330         if (aupe)
331                 vmolr |= IXGBE_VMOLR_AUPE;
332         else
333                 vmolr &= ~IXGBE_VMOLR_AUPE;
334         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
335 }
336
337 static 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
348 static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
349 {
350         struct ixgbe_hw *hw = &adapter->hw;
351         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
352
353         /* reset offloads to defaults */
354         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         }
366
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
373         hw->mac.ops.clear_rar(hw, rar_entry);
374 }
375
376 static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
377                             int vf, unsigned char *mac_addr)
378 {
379         struct ixgbe_hw *hw = &adapter->hw;
380         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
381
382         memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
383         hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
384
385         return 0;
386 }
387
388 static 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
443 int 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
454 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
455 {
456         unsigned char vf_mac_addr[6];
457         struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
458         unsigned int vfn = (event_mask & 0x3f);
459         struct pci_dev *pvfdev;
460         unsigned int device_id;
461         u16 thisvf_devfn = (pdev->devfn + 0x80 + (vfn << 1)) |
462                                 (pdev->devfn & 1);
463
464         bool enable = ((event_mask & 0x10000000U) != 0);
465
466         if (enable) {
467                 random_ether_addr(vf_mac_addr);
468                 e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
469                        vfn, vf_mac_addr);
470                 /*
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);
475
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);
500         }
501
502         return 0;
503 }
504
505 static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
506 {
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
523         /* 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
528         ixgbe_vf_reset_event(adapter, vf);
529 }
530
531 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
532 {
533         u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
534         u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
535         struct ixgbe_hw *hw = &adapter->hw;
536         s32 retval;
537         int entries;
538         u16 *hash_list;
539         int add, vid, index;
540         u8 *new_mac;
541
542         retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
543
544         if (retval)
545                 pr_err("Error receiving message from VF\n");
546
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;
558                 new_mac = (u8 *)(&msgbuf[1]);
559                 e_info(probe, "VF Reset msg received from vf %d\n", vf);
560                 adapter->vfinfo[vf].clear_to_send = false;
561                 ixgbe_vf_reset_msg(adapter, vf);
562                 adapter->vfinfo[vf].clear_to_send = true;
563
564                 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
571                 /* reply to reset with ack and vf mac address */
572                 msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
573                 memcpy(new_mac, vf_mac, IXGBE_ETH_LENGTH_OF_ADDRESS);
574                 /*
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:
592                 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;
602                 }
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:
612                 ixgbe_set_vf_lpe(adapter, msgbuf);
613                 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);
618                 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 {
625                         if (add)
626                                 adapter->vfinfo[vf].vlan_count++;
627                         else if (adapter->vfinfo[vf].vlan_count)
628                                 adapter->vfinfo[vf].vlan_count--;
629                         retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
630                         if (!retval && adapter->vfinfo[vf].spoofchk_enabled)
631                                 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
632                 }
633                 break;
634         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                  */
643                 if (index > 0 && adapter->vfinfo[vf].spoofchk_enabled)
644                         ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
645                 retval = ixgbe_set_vf_macvlan(adapter, vf, index,
646                                               (unsigned char *)(&msgbuf[1]));
647                 break;
648         default:
649                 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
650                 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
667 static 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
677 void 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
697 void 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
709 void 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
723 int 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
741 int 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);
745         struct ixgbe_hw *hw = &adapter->hw;
746
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);
754                 ixgbe_set_vmolr(hw, vf, false);
755                 if (adapter->vfinfo[vf].spoofchk_enabled)
756                         hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
757                 adapter->vfinfo[vf].vlan_count++;
758                 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);
774                 ixgbe_set_vmolr(hw, vf, true);
775                 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
776                 if (adapter->vfinfo[vf].vlan_count)
777                         adapter->vfinfo[vf].vlan_count--;
778                 adapter->vfinfo[vf].pf_vlan = 0;
779                 adapter->vfinfo[vf].pf_qos = 0;
780        }
781 out:
782        return err;
783 }
784
785 static 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
799 static 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 */
820         /*
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
836         IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
837 }
838
839 void 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
867 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
868 {
869         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;
885 }
886
887 int 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
913 int 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);
921         ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
922         ivi->vlan = adapter->vfinfo[vf].pf_vlan;
923         ivi->qos = adapter->vfinfo[vf].pf_qos;
924         ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
925         return 0;
926 }