ixgbe: Add support for VF MAC and VLAN configuration
[deliverable/linux.git] / drivers / net / ixgbe / ixgbe_sriov.c
1 /*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2009 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
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>
35 #include <linux/in.h>
36 #include <linux/ip.h>
37 #include <linux/tcp.h>
38 #include <linux/ipv6.h>
39 #ifdef NETIF_F_HW_VLAN_TX
40 #include <linux/if_vlan.h>
41 #endif
42
43 #include "ixgbe.h"
44
45 #include "ixgbe_sriov.h"
46
47 int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
48 int entries, u16 *hash_list, u32 vf)
49 {
50 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
51 int i;
52
53 /* only so many hash values supported */
54 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
55
56 /*
57 * salt away the number of multi cast addresses assigned
58 * to this VF for later use to restore when the PF multi cast
59 * list changes
60 */
61 vfinfo->num_vf_mc_hashes = entries;
62
63 /*
64 * VFs are limited to using the MTA hash table for their multicast
65 * addresses
66 */
67 for (i = 0; i < entries; i++) {
68 vfinfo->vf_mc_hashes[i] = hash_list[i];;
69 }
70
71 /* Flush and reset the mta with the new values */
72 ixgbe_set_rx_mode(adapter->netdev);
73
74 return 0;
75 }
76
77 void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
78 {
79 struct ixgbe_hw *hw = &adapter->hw;
80 struct vf_data_storage *vfinfo;
81 int i, j;
82 u32 vector_bit;
83 u32 vector_reg;
84 u32 mta_reg;
85
86 for (i = 0; i < adapter->num_vfs; i++) {
87 vfinfo = &adapter->vfinfo[i];
88 for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
89 hw->addr_ctrl.mta_in_use++;
90 vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
91 vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
92 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
93 mta_reg |= (1 << vector_bit);
94 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
95 }
96 }
97 }
98
99 int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid, u32 vf)
100 {
101 u32 ctrl;
102
103 /* Check if global VLAN already set, if not set it */
104 ctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_VLNCTRL);
105 if (!(ctrl & IXGBE_VLNCTRL_VFE)) {
106 /* enable VLAN tag insert/strip */
107 ctrl |= IXGBE_VLNCTRL_VFE;
108 ctrl &= ~IXGBE_VLNCTRL_CFIEN;
109 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VLNCTRL, ctrl);
110 }
111
112 return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
113 }
114
115
116 void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
117 {
118 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
119 vmolr |= (IXGBE_VMOLR_ROMPE |
120 IXGBE_VMOLR_ROPE |
121 IXGBE_VMOLR_BAM);
122 if (aupe)
123 vmolr |= IXGBE_VMOLR_AUPE;
124 else
125 vmolr &= ~IXGBE_VMOLR_AUPE;
126 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
127 }
128
129 static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
130 {
131 struct ixgbe_hw *hw = &adapter->hw;
132
133 if (vid)
134 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
135 (vid | IXGBE_VMVIR_VLANA_DEFAULT));
136 else
137 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
138 }
139
140 inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
141 {
142 struct ixgbe_hw *hw = &adapter->hw;
143
144 /* reset offloads to defaults */
145 if (adapter->vfinfo[vf].pf_vlan) {
146 ixgbe_set_vf_vlan(adapter, true,
147 adapter->vfinfo[vf].pf_vlan, vf);
148 ixgbe_set_vmvir(adapter,
149 (adapter->vfinfo[vf].pf_vlan |
150 (adapter->vfinfo[vf].pf_qos <<
151 VLAN_PRIO_SHIFT)), vf);
152 ixgbe_set_vmolr(hw, vf, false);
153 } else {
154 ixgbe_set_vmvir(adapter, 0, vf);
155 ixgbe_set_vmolr(hw, vf, true);
156 }
157
158 /* reset multicast table array for vf */
159 adapter->vfinfo[vf].num_vf_mc_hashes = 0;
160
161 /* Flush and reset the mta with the new values */
162 ixgbe_set_rx_mode(adapter->netdev);
163
164 if (adapter->vfinfo[vf].rar > 0) {
165 adapter->hw.mac.ops.clear_rar(&adapter->hw,
166 adapter->vfinfo[vf].rar);
167 adapter->vfinfo[vf].rar = -1;
168 }
169 }
170
171 int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
172 int vf, unsigned char *mac_addr)
173 {
174 struct ixgbe_hw *hw = &adapter->hw;
175
176 adapter->vfinfo[vf].rar = hw->mac.ops.set_rar(hw, vf + 1, mac_addr,
177 vf, IXGBE_RAH_AV);
178 if (adapter->vfinfo[vf].rar < 0) {
179 DPRINTK(DRV, ERR, "Could not set MAC Filter for VF %d\n", vf);
180 return -1;
181 }
182
183 memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
184
185 return 0;
186 }
187
188 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
189 {
190 unsigned char vf_mac_addr[6];
191 struct net_device *netdev = pci_get_drvdata(pdev);
192 struct ixgbe_adapter *adapter = netdev_priv(netdev);
193 unsigned int vfn = (event_mask & 0x3f);
194
195 bool enable = ((event_mask & 0x10000000U) != 0);
196
197 if (enable) {
198 random_ether_addr(vf_mac_addr);
199 DPRINTK(PROBE, INFO, "IOV: VF %d is enabled "
200 "mac %02X:%02X:%02X:%02X:%02X:%02X\n",
201 vfn,
202 vf_mac_addr[0], vf_mac_addr[1], vf_mac_addr[2],
203 vf_mac_addr[3], vf_mac_addr[4], vf_mac_addr[5]);
204 /*
205 * Store away the VF "permananet" MAC address, it will ask
206 * for it later.
207 */
208 memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
209 }
210
211 return 0;
212 }
213
214 inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
215 {
216 struct ixgbe_hw *hw = &adapter->hw;
217 u32 reg;
218 u32 reg_offset, vf_shift;
219
220 vf_shift = vf % 32;
221 reg_offset = vf / 32;
222
223 /* enable transmit and receive for vf */
224 reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
225 reg |= (reg | (1 << vf_shift));
226 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
227
228 reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
229 reg |= (reg | (1 << vf_shift));
230 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
231
232 ixgbe_vf_reset_event(adapter, vf);
233 }
234
235 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
236 {
237 u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
238 u32 msgbuf[mbx_size];
239 struct ixgbe_hw *hw = &adapter->hw;
240 s32 retval;
241 int entries;
242 u16 *hash_list;
243 int add, vid;
244
245 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
246
247 if (retval)
248 printk(KERN_ERR "Error receiving message from VF\n");
249
250 /* this is a message we already processed, do nothing */
251 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
252 return retval;
253
254 /*
255 * until the vf completes a virtual function reset it should not be
256 * allowed to start any configuration.
257 */
258
259 if (msgbuf[0] == IXGBE_VF_RESET) {
260 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
261 u8 *addr = (u8 *)(&msgbuf[1]);
262 DPRINTK(PROBE, INFO, "VF Reset msg received from vf %d\n", vf);
263 adapter->vfinfo[vf].clear_to_send = false;
264 ixgbe_vf_reset_msg(adapter, vf);
265 adapter->vfinfo[vf].clear_to_send = true;
266
267 /* reply to reset with ack and vf mac address */
268 msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
269 memcpy(addr, vf_mac, IXGBE_ETH_LENGTH_OF_ADDRESS);
270 /*
271 * Piggyback the multicast filter type so VF can compute the
272 * correct vectors
273 */
274 msgbuf[3] = hw->mac.mc_filter_type;
275 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
276
277 return retval;
278 }
279
280 if (!adapter->vfinfo[vf].clear_to_send) {
281 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
282 ixgbe_write_mbx(hw, msgbuf, 1, vf);
283 return retval;
284 }
285
286 switch ((msgbuf[0] & 0xFFFF)) {
287 case IXGBE_VF_SET_MAC_ADDR:
288 {
289 u8 *new_mac = ((u8 *)(&msgbuf[1]));
290 if (is_valid_ether_addr(new_mac) &&
291 !adapter->vfinfo[vf].pf_set_mac)
292 ixgbe_set_vf_mac(adapter, vf, new_mac);
293 else
294 ixgbe_set_vf_mac(adapter,
295 vf, adapter->vfinfo[vf].vf_mac_addresses);
296 }
297 break;
298 case IXGBE_VF_SET_MULTICAST:
299 entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
300 >> IXGBE_VT_MSGINFO_SHIFT;
301 hash_list = (u16 *)&msgbuf[1];
302 retval = ixgbe_set_vf_multicasts(adapter, entries,
303 hash_list, vf);
304 break;
305 case IXGBE_VF_SET_LPE:
306 WARN_ON((msgbuf[0] & 0xFFFF) == IXGBE_VF_SET_LPE);
307 break;
308 case IXGBE_VF_SET_VLAN:
309 add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
310 >> IXGBE_VT_MSGINFO_SHIFT;
311 vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
312 retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
313 break;
314 default:
315 DPRINTK(DRV, ERR, "Unhandled Msg %8.8x\n", msgbuf[0]);
316 retval = IXGBE_ERR_MBX;
317 break;
318 }
319
320 /* notify the VF of the results of what it sent us */
321 if (retval)
322 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
323 else
324 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
325
326 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
327
328 ixgbe_write_mbx(hw, msgbuf, 1, vf);
329
330 return retval;
331 }
332
333 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
334 {
335 struct ixgbe_hw *hw = &adapter->hw;
336 u32 msg = IXGBE_VT_MSGTYPE_NACK;
337
338 /* if device isn't clear to send it shouldn't be reading either */
339 if (!adapter->vfinfo[vf].clear_to_send)
340 ixgbe_write_mbx(hw, &msg, 1, vf);
341 }
342
343 void ixgbe_msg_task(struct ixgbe_adapter *adapter)
344 {
345 struct ixgbe_hw *hw = &adapter->hw;
346 u32 vf;
347
348 for (vf = 0; vf < adapter->num_vfs; vf++) {
349 /* process any reset requests */
350 if (!ixgbe_check_for_rst(hw, vf))
351 ixgbe_vf_reset_event(adapter, vf);
352
353 /* process any messages pending */
354 if (!ixgbe_check_for_msg(hw, vf))
355 ixgbe_rcv_msg_from_vf(adapter, vf);
356
357 /* process any acks */
358 if (!ixgbe_check_for_ack(hw, vf))
359 ixgbe_rcv_ack_from_vf(adapter, vf);
360 }
361 }
362
363 void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
364 {
365 struct ixgbe_hw *hw = &adapter->hw;
366
367 /* disable transmit and receive for all vfs */
368 IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
369 IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
370
371 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
372 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
373 }
374
375 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
376 {
377 struct ixgbe_hw *hw = &adapter->hw;
378 u32 ping;
379 int i;
380
381 for (i = 0 ; i < adapter->num_vfs; i++) {
382 ping = IXGBE_PF_CONTROL_MSG;
383 if (adapter->vfinfo[i].clear_to_send)
384 ping |= IXGBE_VT_MSGTYPE_CTS;
385 ixgbe_write_mbx(hw, &ping, 1, i);
386 }
387 }
388
389 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
390 {
391 struct ixgbe_adapter *adapter = netdev_priv(netdev);
392 if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
393 return -EINVAL;
394 adapter->vfinfo[vf].pf_set_mac = true;
395 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
396 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
397 " change effective.");
398 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
399 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
400 " but the PF device is not up.\n");
401 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
402 " attempting to use the VF device.\n");
403 }
404 return ixgbe_set_vf_mac(adapter, vf, mac);
405 }
406
407 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
408 {
409 int err = 0;
410 struct ixgbe_adapter *adapter = netdev_priv(netdev);
411
412 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
413 return -EINVAL;
414 if (vlan || qos) {
415 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
416 if (err)
417 goto out;
418 ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
419 ixgbe_set_vmolr(&adapter->hw, vf, false);
420 adapter->vfinfo[vf].pf_vlan = vlan;
421 adapter->vfinfo[vf].pf_qos = qos;
422 dev_info(&adapter->pdev->dev,
423 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
424 if (test_bit(__IXGBE_DOWN, &adapter->state)) {
425 dev_warn(&adapter->pdev->dev,
426 "The VF VLAN has been set,"
427 " but the PF device is not up.\n");
428 dev_warn(&adapter->pdev->dev,
429 "Bring the PF device up before"
430 " attempting to use the VF device.\n");
431 }
432 } else {
433 err = ixgbe_set_vf_vlan(adapter, false,
434 adapter->vfinfo[vf].pf_vlan, vf);
435 ixgbe_set_vmvir(adapter, vlan, vf);
436 ixgbe_set_vmolr(&adapter->hw, vf, true);
437 adapter->vfinfo[vf].pf_vlan = 0;
438 adapter->vfinfo[vf].pf_qos = 0;
439 }
440 out:
441 return err;
442 }
443
444 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
445 {
446 return -EOPNOTSUPP;
447 }
448
449 int ixgbe_ndo_get_vf_config(struct net_device *netdev,
450 int vf, struct ifla_vf_info *ivi)
451 {
452 struct ixgbe_adapter *adapter = netdev_priv(netdev);
453 if (vf >= adapter->num_vfs)
454 return -EINVAL;
455 ivi->vf = vf;
456 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
457 ivi->tx_rate = 0;
458 ivi->vlan = adapter->vfinfo[vf].pf_vlan;
459 ivi->qos = adapter->vfinfo[vf].pf_qos;
460 return 0;
461 }
This page took 0.040844 seconds and 5 git commands to generate.