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