1 /*******************************************************************************
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2010 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 *******************************************************************************/
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 void ixgbe_restore_vf_multicasts(struct ixgbe_adapter
*adapter
)
87 struct ixgbe_hw
*hw
= &adapter
->hw
;
88 struct vf_data_storage
*vfinfo
;
94 for (i
= 0; i
< adapter
->num_vfs
; i
++) {
95 vfinfo
= &adapter
->vfinfo
[i
];
96 for (j
= 0; j
< vfinfo
->num_vf_mc_hashes
; j
++) {
97 hw
->addr_ctrl
.mta_in_use
++;
98 vector_reg
= (vfinfo
->vf_mc_hashes
[j
] >> 5) & 0x7F;
99 vector_bit
= vfinfo
->vf_mc_hashes
[j
] & 0x1F;
100 mta_reg
= IXGBE_READ_REG(hw
, IXGBE_MTA(vector_reg
));
101 mta_reg
|= (1 << vector_bit
);
102 IXGBE_WRITE_REG(hw
, IXGBE_MTA(vector_reg
), mta_reg
);
107 static int ixgbe_set_vf_vlan(struct ixgbe_adapter
*adapter
, int add
, int vid
,
110 return adapter
->hw
.mac
.ops
.set_vfta(&adapter
->hw
, vid
, vf
, (bool)add
);
113 static void ixgbe_set_vmolr(struct ixgbe_hw
*hw
, u32 vf
, bool aupe
)
115 u32 vmolr
= IXGBE_READ_REG(hw
, IXGBE_VMOLR(vf
));
116 vmolr
|= (IXGBE_VMOLR_ROMPE
|
119 vmolr
|= IXGBE_VMOLR_AUPE
;
121 vmolr
&= ~IXGBE_VMOLR_AUPE
;
122 IXGBE_WRITE_REG(hw
, IXGBE_VMOLR(vf
), vmolr
);
125 static void ixgbe_set_vmvir(struct ixgbe_adapter
*adapter
, u32 vid
, u32 vf
)
127 struct ixgbe_hw
*hw
= &adapter
->hw
;
130 IXGBE_WRITE_REG(hw
, IXGBE_VMVIR(vf
),
131 (vid
| IXGBE_VMVIR_VLANA_DEFAULT
));
133 IXGBE_WRITE_REG(hw
, IXGBE_VMVIR(vf
), 0);
136 static inline void ixgbe_vf_reset_event(struct ixgbe_adapter
*adapter
, u32 vf
)
138 struct ixgbe_hw
*hw
= &adapter
->hw
;
139 int rar_entry
= hw
->mac
.num_rar_entries
- (vf
+ 1);
141 /* reset offloads to defaults */
142 if (adapter
->vfinfo
[vf
].pf_vlan
) {
143 ixgbe_set_vf_vlan(adapter
, true,
144 adapter
->vfinfo
[vf
].pf_vlan
, vf
);
145 ixgbe_set_vmvir(adapter
,
146 (adapter
->vfinfo
[vf
].pf_vlan
|
147 (adapter
->vfinfo
[vf
].pf_qos
<<
148 VLAN_PRIO_SHIFT
)), vf
);
149 ixgbe_set_vmolr(hw
, vf
, false);
151 ixgbe_set_vmvir(adapter
, 0, vf
);
152 ixgbe_set_vmolr(hw
, vf
, true);
155 /* reset multicast table array for vf */
156 adapter
->vfinfo
[vf
].num_vf_mc_hashes
= 0;
158 /* Flush and reset the mta with the new values */
159 ixgbe_set_rx_mode(adapter
->netdev
);
161 hw
->mac
.ops
.clear_rar(hw
, rar_entry
);
164 static int ixgbe_set_vf_mac(struct ixgbe_adapter
*adapter
,
165 int vf
, unsigned char *mac_addr
)
167 struct ixgbe_hw
*hw
= &adapter
->hw
;
168 int rar_entry
= hw
->mac
.num_rar_entries
- (vf
+ 1);
170 memcpy(adapter
->vfinfo
[vf
].vf_mac_addresses
, mac_addr
, 6);
171 hw
->mac
.ops
.set_rar(hw
, rar_entry
, mac_addr
, vf
, IXGBE_RAH_AV
);
176 int ixgbe_vf_configuration(struct pci_dev
*pdev
, unsigned int event_mask
)
178 unsigned char vf_mac_addr
[6];
179 struct ixgbe_adapter
*adapter
= pci_get_drvdata(pdev
);
180 unsigned int vfn
= (event_mask
& 0x3f);
182 bool enable
= ((event_mask
& 0x10000000U
) != 0);
185 random_ether_addr(vf_mac_addr
);
186 e_info(probe
, "IOV: VF %d is enabled MAC %pM\n",
189 * Store away the VF "permananet" MAC address, it will ask
192 memcpy(adapter
->vfinfo
[vfn
].vf_mac_addresses
, vf_mac_addr
, 6);
198 static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter
*adapter
, u32 vf
)
200 struct ixgbe_hw
*hw
= &adapter
->hw
;
202 u32 reg_offset
, vf_shift
;
205 reg_offset
= vf
/ 32;
207 /* enable transmit and receive for vf */
208 reg
= IXGBE_READ_REG(hw
, IXGBE_VFTE(reg_offset
));
209 reg
|= (reg
| (1 << vf_shift
));
210 IXGBE_WRITE_REG(hw
, IXGBE_VFTE(reg_offset
), reg
);
212 reg
= IXGBE_READ_REG(hw
, IXGBE_VFRE(reg_offset
));
213 reg
|= (reg
| (1 << vf_shift
));
214 IXGBE_WRITE_REG(hw
, IXGBE_VFRE(reg_offset
), reg
);
216 /* Enable counting of spoofed packets in the SSVPC register */
217 reg
= IXGBE_READ_REG(hw
, IXGBE_VMECM(reg_offset
));
218 reg
|= (1 << vf_shift
);
219 IXGBE_WRITE_REG(hw
, IXGBE_VMECM(reg_offset
), reg
);
221 ixgbe_vf_reset_event(adapter
, vf
);
224 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter
*adapter
, u32 vf
)
226 u32 mbx_size
= IXGBE_VFMAILBOX_SIZE
;
227 u32 msgbuf
[mbx_size
];
228 struct ixgbe_hw
*hw
= &adapter
->hw
;
235 retval
= ixgbe_read_mbx(hw
, msgbuf
, mbx_size
, vf
);
238 pr_err("Error receiving message from VF\n");
240 /* this is a message we already processed, do nothing */
241 if (msgbuf
[0] & (IXGBE_VT_MSGTYPE_ACK
| IXGBE_VT_MSGTYPE_NACK
))
245 * until the vf completes a virtual function reset it should not be
246 * allowed to start any configuration.
249 if (msgbuf
[0] == IXGBE_VF_RESET
) {
250 unsigned char *vf_mac
= adapter
->vfinfo
[vf
].vf_mac_addresses
;
251 new_mac
= (u8
*)(&msgbuf
[1]);
252 e_info(probe
, "VF Reset msg received from vf %d\n", vf
);
253 adapter
->vfinfo
[vf
].clear_to_send
= false;
254 ixgbe_vf_reset_msg(adapter
, vf
);
255 adapter
->vfinfo
[vf
].clear_to_send
= true;
257 if (is_valid_ether_addr(new_mac
) &&
258 !adapter
->vfinfo
[vf
].pf_set_mac
)
259 ixgbe_set_vf_mac(adapter
, vf
, vf_mac
);
261 ixgbe_set_vf_mac(adapter
,
262 vf
, adapter
->vfinfo
[vf
].vf_mac_addresses
);
264 /* reply to reset with ack and vf mac address */
265 msgbuf
[0] = IXGBE_VF_RESET
| IXGBE_VT_MSGTYPE_ACK
;
266 memcpy(new_mac
, vf_mac
, IXGBE_ETH_LENGTH_OF_ADDRESS
);
268 * Piggyback the multicast filter type so VF can compute the
271 msgbuf
[3] = hw
->mac
.mc_filter_type
;
272 ixgbe_write_mbx(hw
, msgbuf
, IXGBE_VF_PERMADDR_MSG_LEN
, vf
);
277 if (!adapter
->vfinfo
[vf
].clear_to_send
) {
278 msgbuf
[0] |= IXGBE_VT_MSGTYPE_NACK
;
279 ixgbe_write_mbx(hw
, msgbuf
, 1, vf
);
283 switch ((msgbuf
[0] & 0xFFFF)) {
284 case IXGBE_VF_SET_MAC_ADDR
:
285 new_mac
= ((u8
*)(&msgbuf
[1]));
286 if (is_valid_ether_addr(new_mac
) &&
287 !adapter
->vfinfo
[vf
].pf_set_mac
) {
288 ixgbe_set_vf_mac(adapter
, vf
, new_mac
);
289 } else if (memcmp(adapter
->vfinfo
[vf
].vf_mac_addresses
,
290 new_mac
, ETH_ALEN
)) {
291 e_warn(drv
, "VF %d attempted to override "
292 "administratively set MAC address\nReload "
293 "the VF driver to resume operations\n", vf
);
297 case IXGBE_VF_SET_MULTICAST
:
298 entries
= (msgbuf
[0] & IXGBE_VT_MSGINFO_MASK
)
299 >> IXGBE_VT_MSGINFO_SHIFT
;
300 hash_list
= (u16
*)&msgbuf
[1];
301 retval
= ixgbe_set_vf_multicasts(adapter
, entries
,
304 case IXGBE_VF_SET_LPE
:
305 WARN_ON((msgbuf
[0] & 0xFFFF) == IXGBE_VF_SET_LPE
);
307 case IXGBE_VF_SET_VLAN
:
308 add
= (msgbuf
[0] & IXGBE_VT_MSGINFO_MASK
)
309 >> IXGBE_VT_MSGINFO_SHIFT
;
310 vid
= (msgbuf
[1] & IXGBE_VLVF_VLANID_MASK
);
311 if (adapter
->vfinfo
[vf
].pf_vlan
) {
312 e_warn(drv
, "VF %d attempted to override "
313 "administratively set VLAN configuration\n"
314 "Reload the VF driver to resume operations\n",
318 retval
= ixgbe_set_vf_vlan(adapter
, add
, vid
, vf
);
322 e_err(drv
, "Unhandled Msg %8.8x\n", msgbuf
[0]);
323 retval
= IXGBE_ERR_MBX
;
327 /* notify the VF of the results of what it sent us */
329 msgbuf
[0] |= IXGBE_VT_MSGTYPE_NACK
;
331 msgbuf
[0] |= IXGBE_VT_MSGTYPE_ACK
;
333 msgbuf
[0] |= IXGBE_VT_MSGTYPE_CTS
;
335 ixgbe_write_mbx(hw
, msgbuf
, 1, vf
);
340 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter
*adapter
, u32 vf
)
342 struct ixgbe_hw
*hw
= &adapter
->hw
;
343 u32 msg
= IXGBE_VT_MSGTYPE_NACK
;
345 /* if device isn't clear to send it shouldn't be reading either */
346 if (!adapter
->vfinfo
[vf
].clear_to_send
)
347 ixgbe_write_mbx(hw
, &msg
, 1, vf
);
350 void ixgbe_msg_task(struct ixgbe_adapter
*adapter
)
352 struct ixgbe_hw
*hw
= &adapter
->hw
;
355 for (vf
= 0; vf
< adapter
->num_vfs
; vf
++) {
356 /* process any reset requests */
357 if (!ixgbe_check_for_rst(hw
, vf
))
358 ixgbe_vf_reset_event(adapter
, vf
);
360 /* process any messages pending */
361 if (!ixgbe_check_for_msg(hw
, vf
))
362 ixgbe_rcv_msg_from_vf(adapter
, vf
);
364 /* process any acks */
365 if (!ixgbe_check_for_ack(hw
, vf
))
366 ixgbe_rcv_ack_from_vf(adapter
, vf
);
370 void ixgbe_disable_tx_rx(struct ixgbe_adapter
*adapter
)
372 struct ixgbe_hw
*hw
= &adapter
->hw
;
374 /* disable transmit and receive for all vfs */
375 IXGBE_WRITE_REG(hw
, IXGBE_VFTE(0), 0);
376 IXGBE_WRITE_REG(hw
, IXGBE_VFTE(1), 0);
378 IXGBE_WRITE_REG(hw
, IXGBE_VFRE(0), 0);
379 IXGBE_WRITE_REG(hw
, IXGBE_VFRE(1), 0);
382 void ixgbe_ping_all_vfs(struct ixgbe_adapter
*adapter
)
384 struct ixgbe_hw
*hw
= &adapter
->hw
;
388 for (i
= 0 ; i
< adapter
->num_vfs
; i
++) {
389 ping
= IXGBE_PF_CONTROL_MSG
;
390 if (adapter
->vfinfo
[i
].clear_to_send
)
391 ping
|= IXGBE_VT_MSGTYPE_CTS
;
392 ixgbe_write_mbx(hw
, &ping
, 1, i
);
396 int ixgbe_ndo_set_vf_mac(struct net_device
*netdev
, int vf
, u8
*mac
)
398 struct ixgbe_adapter
*adapter
= netdev_priv(netdev
);
399 if (!is_valid_ether_addr(mac
) || (vf
>= adapter
->num_vfs
))
401 adapter
->vfinfo
[vf
].pf_set_mac
= true;
402 dev_info(&adapter
->pdev
->dev
, "setting MAC %pM on VF %d\n", mac
, vf
);
403 dev_info(&adapter
->pdev
->dev
, "Reload the VF driver to make this"
404 " change effective.");
405 if (test_bit(__IXGBE_DOWN
, &adapter
->state
)) {
406 dev_warn(&adapter
->pdev
->dev
, "The VF MAC address has been set,"
407 " but the PF device is not up.\n");
408 dev_warn(&adapter
->pdev
->dev
, "Bring the PF device up before"
409 " attempting to use the VF device.\n");
411 return ixgbe_set_vf_mac(adapter
, vf
, mac
);
414 int ixgbe_ndo_set_vf_vlan(struct net_device
*netdev
, int vf
, u16 vlan
, u8 qos
)
417 struct ixgbe_adapter
*adapter
= netdev_priv(netdev
);
418 struct ixgbe_hw
*hw
= &adapter
->hw
;
420 if ((vf
>= adapter
->num_vfs
) || (vlan
> 4095) || (qos
> 7))
423 err
= ixgbe_set_vf_vlan(adapter
, true, vlan
, vf
);
426 ixgbe_set_vmvir(adapter
, vlan
| (qos
<< VLAN_PRIO_SHIFT
), vf
);
427 ixgbe_set_vmolr(hw
, vf
, false);
428 hw
->mac
.ops
.set_vlan_anti_spoofing(hw
, true, vf
);
429 adapter
->vfinfo
[vf
].pf_vlan
= vlan
;
430 adapter
->vfinfo
[vf
].pf_qos
= qos
;
431 dev_info(&adapter
->pdev
->dev
,
432 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan
, qos
, vf
);
433 if (test_bit(__IXGBE_DOWN
, &adapter
->state
)) {
434 dev_warn(&adapter
->pdev
->dev
,
435 "The VF VLAN has been set,"
436 " but the PF device is not up.\n");
437 dev_warn(&adapter
->pdev
->dev
,
438 "Bring the PF device up before"
439 " attempting to use the VF device.\n");
442 err
= ixgbe_set_vf_vlan(adapter
, false,
443 adapter
->vfinfo
[vf
].pf_vlan
, vf
);
444 ixgbe_set_vmvir(adapter
, vlan
, vf
);
445 ixgbe_set_vmolr(hw
, vf
, true);
446 hw
->mac
.ops
.set_vlan_anti_spoofing(hw
, false, vf
);
447 adapter
->vfinfo
[vf
].pf_vlan
= 0;
448 adapter
->vfinfo
[vf
].pf_qos
= 0;
454 int ixgbe_ndo_set_vf_bw(struct net_device
*netdev
, int vf
, int tx_rate
)
459 int ixgbe_ndo_get_vf_config(struct net_device
*netdev
,
460 int vf
, struct ifla_vf_info
*ivi
)
462 struct ixgbe_adapter
*adapter
= netdev_priv(netdev
);
463 if (vf
>= adapter
->num_vfs
)
466 memcpy(&ivi
->mac
, adapter
->vfinfo
[vf
].vf_mac_addresses
, ETH_ALEN
);
468 ivi
->vlan
= adapter
->vfinfo
[vf
].pf_vlan
;
469 ivi
->qos
= adapter
->vfinfo
[vf
].pf_qos
;