1 /*******************************************************************************
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2011 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
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".
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>
36 #include <linux/tcp.h>
37 #include <linux/ipv6.h>
38 #ifdef NETIF_F_HW_VLAN_TX
39 #include <linux/if_vlan.h>
44 #include "ixgbe_sriov.h"
46 static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
47 int entries, u16 *hash_list, u32 vf)
49 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
50 struct ixgbe_hw *hw = &adapter->hw;
56 /* only so many hash values supported */
57 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
60 * salt away the number of multi cast addresses assigned
61 * to this VF for later use to restore when the PF multi cast
64 vfinfo->num_vf_mc_hashes = entries;
67 * VFs are limited to using the MTA hash table for their multicast
70 for (i = 0; i < entries; i++) {
71 vfinfo->vf_mc_hashes[i] = hash_list[i];
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);
85 static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
87 struct ixgbe_hw *hw = &adapter->hw;
88 struct list_head *pos;
89 struct vf_macvlans *entry;
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,
96 entry->vf, IXGBE_RAH_AV);
100 void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
102 struct ixgbe_hw *hw = &adapter->hw;
103 struct vf_data_storage *vfinfo;
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);
121 /* Restore any VF macvlans */
122 ixgbe_restore_vf_macvlans(adapter);
125 static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
128 return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
131 static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
133 struct ixgbe_hw *hw = &adapter->hw;
134 int new_mtu = msgbuf[1];
136 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
138 /* Only X540 supports jumbo frames in IOV mode */
139 if (adapter->hw.mac.type != ixgbe_mac_X540)
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);
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);
155 e_info(hw, "VF requests change max MTU to %d\n", new_mtu);
158 static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
160 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
161 vmolr |= (IXGBE_VMOLR_ROMPE |
164 vmolr |= IXGBE_VMOLR_AUPE;
166 vmolr &= ~IXGBE_VMOLR_AUPE;
167 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
170 static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
172 struct ixgbe_hw *hw = &adapter->hw;
175 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
176 (vid | IXGBE_VMVIR_VLANA_DEFAULT));
178 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
181 static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
183 struct ixgbe_hw *hw = &adapter->hw;
184 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
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);
196 ixgbe_set_vmvir(adapter, 0, vf);
197 ixgbe_set_vmolr(hw, vf, true);
200 /* reset multicast table array for vf */
201 adapter->vfinfo[vf].num_vf_mc_hashes = 0;
203 /* Flush and reset the mta with the new values */
204 ixgbe_set_rx_mode(adapter->netdev);
206 hw->mac.ops.clear_rar(hw, rar_entry);
209 static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
210 int vf, unsigned char *mac_addr)
212 struct ixgbe_hw *hw = &adapter->hw;
213 int rar_entry = hw->mac.num_rar_entries - (vf + 1);
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);
221 static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
222 int vf, int index, unsigned char *mac_addr)
224 struct ixgbe_hw *hw = &adapter->hw;
225 struct list_head *pos;
226 struct vf_macvlans *entry;
229 list_for_each(pos, &adapter->vf_mvs.l) {
230 entry = list_entry(pos, struct vf_macvlans, l);
231 if (entry->vf == vf) {
234 entry->is_macvlan = false;
235 hw->mac.ops.clear_rar(hw, entry->rar_entry);
241 * If index was zero then we were asked to clear the uc list
242 * for the VF. We're done.
249 list_for_each(pos, &adapter->vf_mvs.l) {
250 entry = list_entry(pos, struct vf_macvlans, l);
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.
263 if (!entry || !entry->free)
267 entry->is_macvlan = true;
269 memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
271 hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
276 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
278 unsigned char vf_mac_addr[6];
279 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
280 unsigned int vfn = (event_mask & 0x3f);
282 bool enable = ((event_mask & 0x10000000U) != 0);
285 random_ether_addr(vf_mac_addr);
286 e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
289 * Store away the VF "permananet" MAC address, it will ask
292 memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
298 static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
300 struct ixgbe_hw *hw = &adapter->hw;
302 u32 reg_offset, vf_shift;
305 reg_offset = vf / 32;
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);
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);
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);
321 ixgbe_vf_reset_event(adapter, vf);
324 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
326 u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
327 u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
328 struct ixgbe_hw *hw = &adapter->hw;
335 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
338 pr_err("Error receiving message from VF\n");
340 /* this is a message we already processed, do nothing */
341 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
345 * until the vf completes a virtual function reset it should not be
346 * allowed to start any configuration.
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;
357 if (is_valid_ether_addr(new_mac) &&
358 !adapter->vfinfo[vf].pf_set_mac)
359 ixgbe_set_vf_mac(adapter, vf, vf_mac);
361 ixgbe_set_vf_mac(adapter,
362 vf, adapter->vfinfo[vf].vf_mac_addresses);
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);
368 * Piggyback the multicast filter type so VF can compute the
371 msgbuf[3] = hw->mac.mc_filter_type;
372 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
377 if (!adapter->vfinfo[vf].clear_to_send) {
378 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
379 ixgbe_write_mbx(hw, msgbuf, 1, vf);
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);
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,
404 case IXGBE_VF_SET_LPE:
405 ixgbe_set_vf_lpe(adapter, msgbuf);
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",
418 retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
421 case IXGBE_VF_SET_MACVLAN:
422 index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
423 IXGBE_VT_MSGINFO_SHIFT;
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.
430 if (index > 0 && adapter->antispoofing_enabled) {
431 hw->mac.ops.set_mac_anti_spoofing(hw, false,
433 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
434 adapter->antispoofing_enabled = false;
436 retval = ixgbe_set_vf_macvlan(adapter, vf, index,
437 (unsigned char *)(&msgbuf[1]));
440 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
441 retval = IXGBE_ERR_MBX;
445 /* notify the VF of the results of what it sent us */
447 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
449 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
451 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
453 ixgbe_write_mbx(hw, msgbuf, 1, vf);
458 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
460 struct ixgbe_hw *hw = &adapter->hw;
461 u32 msg = IXGBE_VT_MSGTYPE_NACK;
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);
468 void ixgbe_msg_task(struct ixgbe_adapter *adapter)
470 struct ixgbe_hw *hw = &adapter->hw;
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);
478 /* process any messages pending */
479 if (!ixgbe_check_for_msg(hw, vf))
480 ixgbe_rcv_msg_from_vf(adapter, vf);
482 /* process any acks */
483 if (!ixgbe_check_for_ack(hw, vf))
484 ixgbe_rcv_ack_from_vf(adapter, vf);
488 void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
490 struct ixgbe_hw *hw = &adapter->hw;
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);
496 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
497 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
500 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
502 struct ixgbe_hw *hw = &adapter->hw;
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);
514 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
516 struct ixgbe_adapter *adapter = netdev_priv(netdev);
517 if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
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");
529 return ixgbe_set_vf_mac(adapter, vf, mac);
532 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
535 struct ixgbe_adapter *adapter = netdev_priv(netdev);
536 struct ixgbe_hw *hw = &adapter->hw;
538 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
541 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
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");
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;
573 static int ixgbe_link_mbps(int internal_link_speed)
575 switch (internal_link_speed) {
576 case IXGBE_LINK_SPEED_100_FULL:
578 case IXGBE_LINK_SPEED_1GB_FULL:
580 case IXGBE_LINK_SPEED_10GB_FULL:
587 static void ixgbe_set_vf_rate_limit(struct ixgbe_hw *hw, int vf, int tx_rate,
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;
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);
607 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, 2*vf); /* vf Y uses queue 2*Y */
608 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
611 void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
613 int actual_link_speed, i;
614 bool reset_rate = false;
616 /* VF Tx rate limit was not set */
617 if (adapter->vf_rate_link_speed == 0)
620 actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
621 if (actual_link_speed != adapter->vf_rate_link_speed) {
623 adapter->vf_rate_link_speed = 0;
624 dev_info(&adapter->pdev->dev,
625 "Link speed has been changed. VF Transmit rate "
629 for (i = 0; i < adapter->num_vfs; i++) {
631 adapter->vfinfo[i].tx_rate = 0;
633 ixgbe_set_vf_rate_limit(&adapter->hw, i,
634 adapter->vfinfo[i].tx_rate,
639 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
641 struct ixgbe_adapter *adapter = netdev_priv(netdev);
642 struct ixgbe_hw *hw = &adapter->hw;
643 int actual_link_speed;
645 actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
646 if ((vf >= adapter->num_vfs) || (!adapter->link_up) ||
647 (tx_rate > actual_link_speed) || (actual_link_speed != 10000) ||
648 ((tx_rate != 0) && (tx_rate <= 10)))
649 /* rate limit cannot be set to 10Mb or less in 10Gb adapters */
652 adapter->vf_rate_link_speed = actual_link_speed;
653 adapter->vfinfo[vf].tx_rate = (u16)tx_rate;
654 ixgbe_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
659 int ixgbe_ndo_get_vf_config(struct net_device *netdev,
660 int vf, struct ifla_vf_info *ivi)
662 struct ixgbe_adapter *adapter = netdev_priv(netdev);
663 if (vf >= adapter->num_vfs)
666 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
667 ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
668 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
669 ivi->qos = adapter->vfinfo[vf].pf_qos;