Merge branch 'core-rcu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / net / 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
44 #include "ixgbe_sriov.h"
45
46 static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
47                                    int entries, u16 *hash_list, u32 vf)
48 {
49         struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
50         struct ixgbe_hw *hw = &adapter->hw;
51         int i;
52         u32 vector_bit;
53         u32 vector_reg;
54         u32 mta_reg;
55
56         /* only so many hash values supported */
57         entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
58
59         /*
60          * salt away the number of multi cast addresses assigned
61          * to this VF for later use to restore when the PF multi cast
62          * list changes
63          */
64         vfinfo->num_vf_mc_hashes = entries;
65
66         /*
67          * VFs are limited to using the MTA hash table for their multicast
68          * addresses
69          */
70         for (i = 0; i < entries; i++) {
71                 vfinfo->vf_mc_hashes[i] = hash_list[i];
72         }
73
74         for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
75                 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
76                 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
77                 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
78                 mta_reg |= (1 << vector_bit);
79                 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
80         }
81
82         return 0;
83 }
84
85 static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
86 {
87         struct ixgbe_hw *hw = &adapter->hw;
88         struct list_head *pos;
89         struct vf_macvlans *entry;
90
91         list_for_each(pos, &adapter->vf_mvs.l) {
92                 entry = list_entry(pos, struct vf_macvlans, l);
93                 if (entry->free == false)
94                         hw->mac.ops.set_rar(hw, entry->rar_entry,
95                                             entry->vf_macvlan,
96                                             entry->vf, IXGBE_RAH_AV);
97         }
98 }
99
100 void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
101 {
102         struct ixgbe_hw *hw = &adapter->hw;
103         struct vf_data_storage *vfinfo;
104         int i, j;
105         u32 vector_bit;
106         u32 vector_reg;
107         u32 mta_reg;
108
109         for (i = 0; i < adapter->num_vfs; i++) {
110                 vfinfo = &adapter->vfinfo[i];
111                 for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
112                         hw->addr_ctrl.mta_in_use++;
113                         vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
114                         vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
115                         mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
116                         mta_reg |= (1 << vector_bit);
117                         IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
118                 }
119         }
120
121         /* Restore any VF macvlans */
122         ixgbe_restore_vf_macvlans(adapter);
123 }
124
125 static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
126                              u32 vf)
127 {
128         return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
129 }
130
131 static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
132 {
133         struct ixgbe_hw *hw = &adapter->hw;
134         int new_mtu = msgbuf[1];
135         u32 max_frs;
136         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
137
138         /* Only X540 supports jumbo frames in IOV mode */
139         if (adapter->hw.mac.type != ixgbe_mac_X540)
140                 return;
141
142         /* MTU < 68 is an error and causes problems on some kernels */
143         if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE)) {
144                 e_err(drv, "VF mtu %d out of range\n", new_mtu);
145                 return;
146         }
147
148         max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
149                    IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
150         if (max_frs < new_mtu) {
151                 max_frs = new_mtu << IXGBE_MHADD_MFS_SHIFT;
152                 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
153         }
154
155         e_info(hw, "VF requests change max MTU to %d\n", new_mtu);
156 }
157
158 static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
159 {
160         u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
161         vmolr |= (IXGBE_VMOLR_ROMPE |
162                   IXGBE_VMOLR_BAM);
163         if (aupe)
164                 vmolr |= IXGBE_VMOLR_AUPE;
165         else
166                 vmolr &= ~IXGBE_VMOLR_AUPE;
167         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
168 }
169
170 static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
171 {
172         struct ixgbe_hw *hw = &adapter->hw;
173
174         if (vid)
175                 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
176                                 (vid | IXGBE_VMVIR_VLANA_DEFAULT));
177         else
178                 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
179 }
180
181 static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
182 {
183         struct ixgbe_hw *hw = &adapter->hw;
184         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
185
186         /* reset offloads to defaults */
187         if (adapter->vfinfo[vf].pf_vlan) {
188                 ixgbe_set_vf_vlan(adapter, true,
189                                   adapter->vfinfo[vf].pf_vlan, vf);
190                 ixgbe_set_vmvir(adapter,
191                                 (adapter->vfinfo[vf].pf_vlan |
192                                  (adapter->vfinfo[vf].pf_qos <<
193                                   VLAN_PRIO_SHIFT)), vf);
194                 ixgbe_set_vmolr(hw, vf, false);
195         } else {
196                 ixgbe_set_vmvir(adapter, 0, vf);
197                 ixgbe_set_vmolr(hw, vf, true);
198         }
199
200         /* reset multicast table array for vf */
201         adapter->vfinfo[vf].num_vf_mc_hashes = 0;
202
203         /* Flush and reset the mta with the new values */
204         ixgbe_set_rx_mode(adapter->netdev);
205
206         hw->mac.ops.clear_rar(hw, rar_entry);
207 }
208
209 static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
210                             int vf, unsigned char *mac_addr)
211 {
212         struct ixgbe_hw *hw = &adapter->hw;
213         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
214
215         memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
216         hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
217
218         return 0;
219 }
220
221 static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
222                                 int vf, int index, unsigned char *mac_addr)
223 {
224         struct ixgbe_hw *hw = &adapter->hw;
225         struct list_head *pos;
226         struct vf_macvlans *entry;
227
228         if (index <= 1) {
229                 list_for_each(pos, &adapter->vf_mvs.l) {
230                         entry = list_entry(pos, struct vf_macvlans, l);
231                         if (entry->vf == vf) {
232                                 entry->vf = -1;
233                                 entry->free = true;
234                                 entry->is_macvlan = false;
235                                 hw->mac.ops.clear_rar(hw, entry->rar_entry);
236                         }
237                 }
238         }
239
240         /*
241          * If index was zero then we were asked to clear the uc list
242          * for the VF.  We're done.
243          */
244         if (!index)
245                 return 0;
246
247         entry = NULL;
248
249         list_for_each(pos, &adapter->vf_mvs.l) {
250                 entry = list_entry(pos, struct vf_macvlans, l);
251                 if (entry->free)
252                         break;
253         }
254
255         /*
256          * If we traversed the entire list and didn't find a free entry
257          * then we're out of space on the RAR table.  Also entry may
258          * be NULL because the original memory allocation for the list
259          * failed, which is not fatal but does mean we can't support
260          * VF requests for MACVLAN because we couldn't allocate
261          * memory for the list management required.
262          */
263         if (!entry || !entry->free)
264                 return -ENOSPC;
265
266         entry->free = false;
267         entry->is_macvlan = true;
268         entry->vf = vf;
269         memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
270
271         hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
272
273         return 0;
274 }
275
276 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
277 {
278         unsigned char vf_mac_addr[6];
279         struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
280         unsigned int vfn = (event_mask & 0x3f);
281
282         bool enable = ((event_mask & 0x10000000U) != 0);
283
284         if (enable) {
285                 random_ether_addr(vf_mac_addr);
286                 e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
287                        vfn, vf_mac_addr);
288                 /*
289                  * Store away the VF "permananet" MAC address, it will ask
290                  * for it later.
291                  */
292                 memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
293         }
294
295         return 0;
296 }
297
298 static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
299 {
300         struct ixgbe_hw *hw = &adapter->hw;
301         u32 reg;
302         u32 reg_offset, vf_shift;
303
304         vf_shift = vf % 32;
305         reg_offset = vf / 32;
306
307         /* enable transmit and receive for vf */
308         reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
309         reg |= (reg | (1 << vf_shift));
310         IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
311
312         reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
313         reg |= (reg | (1 << vf_shift));
314         IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
315
316         /* Enable counting of spoofed packets in the SSVPC register */
317         reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
318         reg |= (1 << vf_shift);
319         IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
320
321         ixgbe_vf_reset_event(adapter, vf);
322 }
323
324 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
325 {
326         u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
327         u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
328         struct ixgbe_hw *hw = &adapter->hw;
329         s32 retval;
330         int entries;
331         u16 *hash_list;
332         int add, vid, index;
333         u8 *new_mac;
334
335         retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
336
337         if (retval)
338                 pr_err("Error receiving message from VF\n");
339
340         /* this is a message we already processed, do nothing */
341         if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
342                 return retval;
343
344         /*
345          * until the vf completes a virtual function reset it should not be
346          * allowed to start any configuration.
347          */
348
349         if (msgbuf[0] == IXGBE_VF_RESET) {
350                 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
351                 new_mac = (u8 *)(&msgbuf[1]);
352                 e_info(probe, "VF Reset msg received from vf %d\n", vf);
353                 adapter->vfinfo[vf].clear_to_send = false;
354                 ixgbe_vf_reset_msg(adapter, vf);
355                 adapter->vfinfo[vf].clear_to_send = true;
356
357                 if (is_valid_ether_addr(new_mac) &&
358                     !adapter->vfinfo[vf].pf_set_mac)
359                         ixgbe_set_vf_mac(adapter, vf, vf_mac);
360                 else
361                         ixgbe_set_vf_mac(adapter,
362                                  vf, adapter->vfinfo[vf].vf_mac_addresses);
363
364                 /* reply to reset with ack and vf mac address */
365                 msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
366                 memcpy(new_mac, vf_mac, IXGBE_ETH_LENGTH_OF_ADDRESS);
367                 /*
368                  * Piggyback the multicast filter type so VF can compute the
369                  * correct vectors
370                  */
371                 msgbuf[3] = hw->mac.mc_filter_type;
372                 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
373
374                 return retval;
375         }
376
377         if (!adapter->vfinfo[vf].clear_to_send) {
378                 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
379                 ixgbe_write_mbx(hw, msgbuf, 1, vf);
380                 return retval;
381         }
382
383         switch ((msgbuf[0] & 0xFFFF)) {
384         case IXGBE_VF_SET_MAC_ADDR:
385                 new_mac = ((u8 *)(&msgbuf[1]));
386                 if (is_valid_ether_addr(new_mac) &&
387                     !adapter->vfinfo[vf].pf_set_mac) {
388                         ixgbe_set_vf_mac(adapter, vf, new_mac);
389                 } else if (memcmp(adapter->vfinfo[vf].vf_mac_addresses,
390                                   new_mac, ETH_ALEN)) {
391                         e_warn(drv, "VF %d attempted to override "
392                                "administratively set MAC address\nReload "
393                                "the VF driver to resume operations\n", vf);
394                         retval = -1;
395                 }
396                 break;
397         case IXGBE_VF_SET_MULTICAST:
398                 entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
399                           >> IXGBE_VT_MSGINFO_SHIFT;
400                 hash_list = (u16 *)&msgbuf[1];
401                 retval = ixgbe_set_vf_multicasts(adapter, entries,
402                                                  hash_list, vf);
403                 break;
404         case IXGBE_VF_SET_LPE:
405                 ixgbe_set_vf_lpe(adapter, msgbuf);
406                 break;
407         case IXGBE_VF_SET_VLAN:
408                 add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
409                       >> IXGBE_VT_MSGINFO_SHIFT;
410                 vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
411                 if (adapter->vfinfo[vf].pf_vlan) {
412                         e_warn(drv, "VF %d attempted to override "
413                                "administratively set VLAN configuration\n"
414                                "Reload the VF driver to resume operations\n",
415                                vf);
416                         retval = -1;
417                 } else {
418                         retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
419                 }
420                 break;
421         case IXGBE_VF_SET_MACVLAN:
422                 index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
423                         IXGBE_VT_MSGINFO_SHIFT;
424                 /*
425                  * If the VF is allowed to set MAC filters then turn off
426                  * anti-spoofing to avoid false positives.  An index
427                  * greater than 0 will indicate the VF is setting a
428                  * macvlan MAC filter.
429                  */
430                 if (index > 0 && adapter->antispoofing_enabled) {
431                         hw->mac.ops.set_mac_anti_spoofing(hw, false,
432                                                           adapter->num_vfs);
433                         hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
434                         adapter->antispoofing_enabled = false;
435                 }
436                 retval = ixgbe_set_vf_macvlan(adapter, vf, index,
437                                               (unsigned char *)(&msgbuf[1]));
438                 break;
439         default:
440                 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
441                 retval = IXGBE_ERR_MBX;
442                 break;
443         }
444
445         /* notify the VF of the results of what it sent us */
446         if (retval)
447                 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
448         else
449                 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
450
451         msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
452
453         ixgbe_write_mbx(hw, msgbuf, 1, vf);
454
455         return retval;
456 }
457
458 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
459 {
460         struct ixgbe_hw *hw = &adapter->hw;
461         u32 msg = IXGBE_VT_MSGTYPE_NACK;
462
463         /* if device isn't clear to send it shouldn't be reading either */
464         if (!adapter->vfinfo[vf].clear_to_send)
465                 ixgbe_write_mbx(hw, &msg, 1, vf);
466 }
467
468 void ixgbe_msg_task(struct ixgbe_adapter *adapter)
469 {
470         struct ixgbe_hw *hw = &adapter->hw;
471         u32 vf;
472
473         for (vf = 0; vf < adapter->num_vfs; vf++) {
474                 /* process any reset requests */
475                 if (!ixgbe_check_for_rst(hw, vf))
476                         ixgbe_vf_reset_event(adapter, vf);
477
478                 /* process any messages pending */
479                 if (!ixgbe_check_for_msg(hw, vf))
480                         ixgbe_rcv_msg_from_vf(adapter, vf);
481
482                 /* process any acks */
483                 if (!ixgbe_check_for_ack(hw, vf))
484                         ixgbe_rcv_ack_from_vf(adapter, vf);
485         }
486 }
487
488 void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
489 {
490         struct ixgbe_hw *hw = &adapter->hw;
491
492         /* disable transmit and receive for all vfs */
493         IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
494         IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
495
496         IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
497         IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
498 }
499
500 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
501 {
502         struct ixgbe_hw *hw = &adapter->hw;
503         u32 ping;
504         int i;
505
506         for (i = 0 ; i < adapter->num_vfs; i++) {
507                 ping = IXGBE_PF_CONTROL_MSG;
508                 if (adapter->vfinfo[i].clear_to_send)
509                         ping |= IXGBE_VT_MSGTYPE_CTS;
510                 ixgbe_write_mbx(hw, &ping, 1, i);
511         }
512 }
513
514 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
515 {
516         struct ixgbe_adapter *adapter = netdev_priv(netdev);
517         if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
518                 return -EINVAL;
519         adapter->vfinfo[vf].pf_set_mac = true;
520         dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
521         dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
522                                       " change effective.");
523         if (test_bit(__IXGBE_DOWN, &adapter->state)) {
524                 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
525                          " but the PF device is not up.\n");
526                 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
527                          " attempting to use the VF device.\n");
528         }
529         return ixgbe_set_vf_mac(adapter, vf, mac);
530 }
531
532 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
533 {
534         int err = 0;
535         struct ixgbe_adapter *adapter = netdev_priv(netdev);
536         struct ixgbe_hw *hw = &adapter->hw;
537
538         if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
539                 return -EINVAL;
540         if (vlan || qos) {
541                 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
542                 if (err)
543                         goto out;
544                 ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
545                 ixgbe_set_vmolr(hw, vf, false);
546                 if (adapter->antispoofing_enabled)
547                         hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
548                 adapter->vfinfo[vf].pf_vlan = vlan;
549                 adapter->vfinfo[vf].pf_qos = qos;
550                 dev_info(&adapter->pdev->dev,
551                          "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
552                 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
553                         dev_warn(&adapter->pdev->dev,
554                                  "The VF VLAN has been set,"
555                                  " but the PF device is not up.\n");
556                         dev_warn(&adapter->pdev->dev,
557                                  "Bring the PF device up before"
558                                  " attempting to use the VF device.\n");
559                 }
560         } else {
561                 err = ixgbe_set_vf_vlan(adapter, false,
562                                         adapter->vfinfo[vf].pf_vlan, vf);
563                 ixgbe_set_vmvir(adapter, vlan, vf);
564                 ixgbe_set_vmolr(hw, vf, true);
565                 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
566                 adapter->vfinfo[vf].pf_vlan = 0;
567                 adapter->vfinfo[vf].pf_qos = 0;
568        }
569 out:
570        return err;
571 }
572
573 static int ixgbe_link_mbps(int internal_link_speed)
574 {
575         switch (internal_link_speed) {
576         case IXGBE_LINK_SPEED_100_FULL:
577                 return 100;
578         case IXGBE_LINK_SPEED_1GB_FULL:
579                 return 1000;
580         case IXGBE_LINK_SPEED_10GB_FULL:
581                 return 10000;
582         default:
583                 return 0;
584         }
585 }
586
587 static void ixgbe_set_vf_rate_limit(struct ixgbe_hw *hw, int vf, int tx_rate,
588                                     int link_speed)
589 {
590         int rf_dec, rf_int;
591         u32 bcnrc_val;
592
593         if (tx_rate != 0) {
594                 /* Calculate the rate factor values to set */
595                 rf_int = link_speed / tx_rate;
596                 rf_dec = (link_speed - (rf_int * tx_rate));
597                 rf_dec = (rf_dec * (1<<IXGBE_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
598
599                 bcnrc_val = IXGBE_RTTBCNRC_RS_ENA;
600                 bcnrc_val |= ((rf_int<<IXGBE_RTTBCNRC_RF_INT_SHIFT) &
601                                IXGBE_RTTBCNRC_RF_INT_MASK);
602                 bcnrc_val |= (rf_dec & IXGBE_RTTBCNRC_RF_DEC_MASK);
603         } else {
604                 bcnrc_val = 0;
605         }
606
607         IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, 2*vf); /* vf Y uses queue 2*Y */
608         /*
609          * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
610          * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
611          * and 0x004 otherwise.
612          */
613         switch (hw->mac.type) {
614         case ixgbe_mac_82599EB:
615                 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
616                 break;
617         case ixgbe_mac_X540:
618                 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
619                 break;
620         default:
621                 break;
622         }
623
624         IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
625 }
626
627 void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
628 {
629         int actual_link_speed, i;
630         bool reset_rate = false;
631
632         /* VF Tx rate limit was not set */
633         if (adapter->vf_rate_link_speed == 0)
634                 return;
635
636         actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
637         if (actual_link_speed != adapter->vf_rate_link_speed) {
638                 reset_rate = true;
639                 adapter->vf_rate_link_speed = 0;
640                 dev_info(&adapter->pdev->dev,
641                          "Link speed has been changed. VF Transmit rate "
642                          "is disabled\n");
643         }
644
645         for (i = 0; i < adapter->num_vfs; i++) {
646                 if (reset_rate)
647                         adapter->vfinfo[i].tx_rate = 0;
648
649                 ixgbe_set_vf_rate_limit(&adapter->hw, i,
650                                         adapter->vfinfo[i].tx_rate,
651                                         actual_link_speed);
652         }
653 }
654
655 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
656 {
657         struct ixgbe_adapter *adapter = netdev_priv(netdev);
658         struct ixgbe_hw *hw = &adapter->hw;
659         int actual_link_speed;
660
661         actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
662         if ((vf >= adapter->num_vfs) || (!adapter->link_up) ||
663             (tx_rate > actual_link_speed) || (actual_link_speed != 10000) ||
664             ((tx_rate != 0) && (tx_rate <= 10)))
665             /* rate limit cannot be set to 10Mb or less in 10Gb adapters */
666                 return -EINVAL;
667
668         adapter->vf_rate_link_speed = actual_link_speed;
669         adapter->vfinfo[vf].tx_rate = (u16)tx_rate;
670         ixgbe_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
671
672         return 0;
673 }
674
675 int ixgbe_ndo_get_vf_config(struct net_device *netdev,
676                             int vf, struct ifla_vf_info *ivi)
677 {
678         struct ixgbe_adapter *adapter = netdev_priv(netdev);
679         if (vf >= adapter->num_vfs)
680                 return -EINVAL;
681         ivi->vf = vf;
682         memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
683         ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
684         ivi->vlan = adapter->vfinfo[vf].pf_vlan;
685         ivi->qos = adapter->vfinfo[vf].pf_qos;
686         return 0;
687 }