1 /*******************************************************************************
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2009 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".
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *******************************************************************************/
29 #include <linux/types.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/netdevice.h>
33 #include <linux/vmalloc.h>
34 #include <linux/string.h>
37 #include <linux/tcp.h>
38 #include <linux/ipv6.h>
39 #ifdef NETIF_F_HW_VLAN_TX
40 #include <linux/if_vlan.h>
45 #include "ixgbe_sriov.h"
47 int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
48 int entries, u16 *hash_list, u32 vf)
50 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
51 struct ixgbe_hw *hw = &adapter->hw;
57 /* only so many hash values supported */
58 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
61 * salt away the number of multi cast addresses assigned
62 * to this VF for later use to restore when the PF multi cast
65 vfinfo->num_vf_mc_hashes = entries;
68 * VFs are limited to using the MTA hash table for their multicast
71 for (i = 0; i < entries; i++) {
72 vfinfo->vf_mc_hashes[i] = hash_list[i];;
75 for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
76 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
77 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
78 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
79 mta_reg |= (1 << vector_bit);
80 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
86 void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
88 struct ixgbe_hw *hw = &adapter->hw;
89 struct vf_data_storage *vfinfo;
95 for (i = 0; i < adapter->num_vfs; i++) {
96 vfinfo = &adapter->vfinfo[i];
97 for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
98 hw->addr_ctrl.mta_in_use++;
99 vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
100 vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
101 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
102 mta_reg |= (1 << vector_bit);
103 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
108 int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid, u32 vf)
110 return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
114 void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
116 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
117 vmolr |= (IXGBE_VMOLR_ROMPE |
121 vmolr |= IXGBE_VMOLR_AUPE;
123 vmolr &= ~IXGBE_VMOLR_AUPE;
124 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
127 static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
129 struct ixgbe_hw *hw = &adapter->hw;
132 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
133 (vid | IXGBE_VMVIR_VLANA_DEFAULT));
135 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
138 inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
140 struct ixgbe_hw *hw = &adapter->hw;
142 /* reset offloads to defaults */
143 if (adapter->vfinfo[vf].pf_vlan) {
144 ixgbe_set_vf_vlan(adapter, true,
145 adapter->vfinfo[vf].pf_vlan, vf);
146 ixgbe_set_vmvir(adapter,
147 (adapter->vfinfo[vf].pf_vlan |
148 (adapter->vfinfo[vf].pf_qos <<
149 VLAN_PRIO_SHIFT)), vf);
150 ixgbe_set_vmolr(hw, vf, false);
152 ixgbe_set_vmvir(adapter, 0, vf);
153 ixgbe_set_vmolr(hw, vf, true);
156 /* reset multicast table array for vf */
157 adapter->vfinfo[vf].num_vf_mc_hashes = 0;
159 /* Flush and reset the mta with the new values */
160 ixgbe_set_rx_mode(adapter->netdev);
162 if (adapter->vfinfo[vf].rar > 0) {
163 adapter->hw.mac.ops.clear_rar(&adapter->hw,
164 adapter->vfinfo[vf].rar);
165 adapter->vfinfo[vf].rar = -1;
169 int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
170 int vf, unsigned char *mac_addr)
172 struct ixgbe_hw *hw = &adapter->hw;
174 adapter->vfinfo[vf].rar = hw->mac.ops.set_rar(hw, vf + 1, mac_addr,
176 if (adapter->vfinfo[vf].rar < 0) {
177 DPRINTK(DRV, ERR, "Could not set MAC Filter for VF %d\n", vf);
181 memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
186 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
188 unsigned char vf_mac_addr[6];
189 struct net_device *netdev = pci_get_drvdata(pdev);
190 struct ixgbe_adapter *adapter = netdev_priv(netdev);
191 unsigned int vfn = (event_mask & 0x3f);
193 bool enable = ((event_mask & 0x10000000U) != 0);
196 random_ether_addr(vf_mac_addr);
197 DPRINTK(PROBE, INFO, "IOV: VF %d is enabled "
198 "mac %02X:%02X:%02X:%02X:%02X:%02X\n",
200 vf_mac_addr[0], vf_mac_addr[1], vf_mac_addr[2],
201 vf_mac_addr[3], vf_mac_addr[4], vf_mac_addr[5]);
203 * Store away the VF "permananet" MAC address, it will ask
206 memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
212 inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
214 struct ixgbe_hw *hw = &adapter->hw;
216 u32 reg_offset, vf_shift;
219 reg_offset = vf / 32;
221 /* enable transmit and receive for vf */
222 reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
223 reg |= (reg | (1 << vf_shift));
224 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
226 reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
227 reg |= (reg | (1 << vf_shift));
228 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
230 ixgbe_vf_reset_event(adapter, vf);
233 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
235 u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
236 u32 msgbuf[mbx_size];
237 struct ixgbe_hw *hw = &adapter->hw;
243 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
246 printk(KERN_ERR "Error receiving message from VF\n");
248 /* this is a message we already processed, do nothing */
249 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
253 * until the vf completes a virtual function reset it should not be
254 * allowed to start any configuration.
257 if (msgbuf[0] == IXGBE_VF_RESET) {
258 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
259 u8 *addr = (u8 *)(&msgbuf[1]);
260 DPRINTK(PROBE, INFO, "VF Reset msg received from vf %d\n", vf);
261 adapter->vfinfo[vf].clear_to_send = false;
262 ixgbe_vf_reset_msg(adapter, vf);
263 adapter->vfinfo[vf].clear_to_send = true;
265 /* reply to reset with ack and vf mac address */
266 msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
267 memcpy(addr, vf_mac, IXGBE_ETH_LENGTH_OF_ADDRESS);
269 * Piggyback the multicast filter type so VF can compute the
272 msgbuf[3] = hw->mac.mc_filter_type;
273 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
278 if (!adapter->vfinfo[vf].clear_to_send) {
279 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
280 ixgbe_write_mbx(hw, msgbuf, 1, vf);
284 switch ((msgbuf[0] & 0xFFFF)) {
285 case IXGBE_VF_SET_MAC_ADDR:
287 u8 *new_mac = ((u8 *)(&msgbuf[1]));
288 if (is_valid_ether_addr(new_mac) &&
289 !adapter->vfinfo[vf].pf_set_mac)
290 ixgbe_set_vf_mac(adapter, vf, new_mac);
292 ixgbe_set_vf_mac(adapter,
293 vf, adapter->vfinfo[vf].vf_mac_addresses);
296 case IXGBE_VF_SET_MULTICAST:
297 entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
298 >> IXGBE_VT_MSGINFO_SHIFT;
299 hash_list = (u16 *)&msgbuf[1];
300 retval = ixgbe_set_vf_multicasts(adapter, entries,
303 case IXGBE_VF_SET_LPE:
304 WARN_ON((msgbuf[0] & 0xFFFF) == IXGBE_VF_SET_LPE);
306 case IXGBE_VF_SET_VLAN:
307 add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
308 >> IXGBE_VT_MSGINFO_SHIFT;
309 vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
310 retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
313 DPRINTK(DRV, ERR, "Unhandled Msg %8.8x\n", msgbuf[0]);
314 retval = IXGBE_ERR_MBX;
318 /* notify the VF of the results of what it sent us */
320 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
322 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
324 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
326 ixgbe_write_mbx(hw, msgbuf, 1, vf);
331 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
333 struct ixgbe_hw *hw = &adapter->hw;
334 u32 msg = IXGBE_VT_MSGTYPE_NACK;
336 /* if device isn't clear to send it shouldn't be reading either */
337 if (!adapter->vfinfo[vf].clear_to_send)
338 ixgbe_write_mbx(hw, &msg, 1, vf);
341 void ixgbe_msg_task(struct ixgbe_adapter *adapter)
343 struct ixgbe_hw *hw = &adapter->hw;
346 for (vf = 0; vf < adapter->num_vfs; vf++) {
347 /* process any reset requests */
348 if (!ixgbe_check_for_rst(hw, vf))
349 ixgbe_vf_reset_event(adapter, vf);
351 /* process any messages pending */
352 if (!ixgbe_check_for_msg(hw, vf))
353 ixgbe_rcv_msg_from_vf(adapter, vf);
355 /* process any acks */
356 if (!ixgbe_check_for_ack(hw, vf))
357 ixgbe_rcv_ack_from_vf(adapter, vf);
361 void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
363 struct ixgbe_hw *hw = &adapter->hw;
365 /* disable transmit and receive for all vfs */
366 IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
367 IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
369 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
370 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
373 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
375 struct ixgbe_hw *hw = &adapter->hw;
379 for (i = 0 ; i < adapter->num_vfs; i++) {
380 ping = IXGBE_PF_CONTROL_MSG;
381 if (adapter->vfinfo[i].clear_to_send)
382 ping |= IXGBE_VT_MSGTYPE_CTS;
383 ixgbe_write_mbx(hw, &ping, 1, i);
387 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
389 struct ixgbe_adapter *adapter = netdev_priv(netdev);
390 if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
392 adapter->vfinfo[vf].pf_set_mac = true;
393 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
394 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
395 " change effective.");
396 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
397 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
398 " but the PF device is not up.\n");
399 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
400 " attempting to use the VF device.\n");
402 return ixgbe_set_vf_mac(adapter, vf, mac);
405 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
408 struct ixgbe_adapter *adapter = netdev_priv(netdev);
410 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
413 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
416 ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
417 ixgbe_set_vmolr(&adapter->hw, vf, false);
418 adapter->vfinfo[vf].pf_vlan = vlan;
419 adapter->vfinfo[vf].pf_qos = qos;
420 dev_info(&adapter->pdev->dev,
421 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
422 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
423 dev_warn(&adapter->pdev->dev,
424 "The VF VLAN has been set,"
425 " but the PF device is not up.\n");
426 dev_warn(&adapter->pdev->dev,
427 "Bring the PF device up before"
428 " attempting to use the VF device.\n");
431 err = ixgbe_set_vf_vlan(adapter, false,
432 adapter->vfinfo[vf].pf_vlan, vf);
433 ixgbe_set_vmvir(adapter, vlan, vf);
434 ixgbe_set_vmolr(&adapter->hw, vf, true);
435 adapter->vfinfo[vf].pf_vlan = 0;
436 adapter->vfinfo[vf].pf_qos = 0;
442 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
447 int ixgbe_ndo_get_vf_config(struct net_device *netdev,
448 int vf, struct ifla_vf_info *ivi)
450 struct ixgbe_adapter *adapter = netdev_priv(netdev);
451 if (vf >= adapter->num_vfs)
454 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
456 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
457 ivi->qos = adapter->vfinfo[vf].pf_qos;