Merge branch 'omap-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tmlind...
[pandora-kernel.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         struct ixgbe_hw *hw = &adapter->hw;
52         int i;
53         u32 vector_bit;
54         u32 vector_reg;
55         u32 mta_reg;
56
57         /* only so many hash values supported */
58         entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
59
60         /*
61          * salt away the number of multi cast addresses assigned
62          * to this VF for later use to restore when the PF multi cast
63          * list changes
64          */
65         vfinfo->num_vf_mc_hashes = entries;
66
67         /*
68          * VFs are limited to using the MTA hash table for their multicast
69          * addresses
70          */
71         for (i = 0; i < entries; i++) {
72                 vfinfo->vf_mc_hashes[i] = hash_list[i];;
73         }
74
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);
81         }
82
83         return 0;
84 }
85
86 void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
87 {
88         struct ixgbe_hw *hw = &adapter->hw;
89         struct vf_data_storage *vfinfo;
90         int i, j;
91         u32 vector_bit;
92         u32 vector_reg;
93         u32 mta_reg;
94
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);
104                 }
105         }
106 }
107
108 int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid, u32 vf)
109 {
110         return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
111 }
112
113
114 void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
115 {
116         u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
117         vmolr |= (IXGBE_VMOLR_ROMPE |
118                   IXGBE_VMOLR_ROPE |
119                   IXGBE_VMOLR_BAM);
120         if (aupe)
121                 vmolr |= IXGBE_VMOLR_AUPE;
122         else
123                 vmolr &= ~IXGBE_VMOLR_AUPE;
124         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
125 }
126
127 static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
128 {
129         struct ixgbe_hw *hw = &adapter->hw;
130
131         if (vid)
132                 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
133                                 (vid | IXGBE_VMVIR_VLANA_DEFAULT));
134         else
135                 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
136 }
137
138 inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
139 {
140         struct ixgbe_hw *hw = &adapter->hw;
141
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);
151         } else {
152                 ixgbe_set_vmvir(adapter, 0, vf);
153                 ixgbe_set_vmolr(hw, vf, true);
154         }
155
156         /* reset multicast table array for vf */
157         adapter->vfinfo[vf].num_vf_mc_hashes = 0;
158
159         /* Flush and reset the mta with the new values */
160         ixgbe_set_rx_mode(adapter->netdev);
161
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;
166         }
167 }
168
169 int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
170                           int vf, unsigned char *mac_addr)
171 {
172         struct ixgbe_hw *hw = &adapter->hw;
173
174         adapter->vfinfo[vf].rar = hw->mac.ops.set_rar(hw, vf + 1, mac_addr,
175                                                       vf, IXGBE_RAH_AV);
176         if (adapter->vfinfo[vf].rar < 0) {
177                 DPRINTK(DRV, ERR, "Could not set MAC Filter for VF %d\n", vf);
178                 return -1;
179         }
180
181         memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
182
183         return 0;
184 }
185
186 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
187 {
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);
192
193         bool enable = ((event_mask & 0x10000000U) != 0);
194
195         if (enable) {
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",
199                        vfn,
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]);
202                 /*
203                  * Store away the VF "permananet" MAC address, it will ask
204                  * for it later.
205                  */
206                 memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
207         }
208
209         return 0;
210 }
211
212 inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
213 {
214         struct ixgbe_hw *hw = &adapter->hw;
215         u32 reg;
216         u32 reg_offset, vf_shift;
217
218         vf_shift = vf % 32;
219         reg_offset = vf / 32;
220
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);
225
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);
229
230         ixgbe_vf_reset_event(adapter, vf);
231 }
232
233 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
234 {
235         u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
236         u32 msgbuf[mbx_size];
237         struct ixgbe_hw *hw = &adapter->hw;
238         s32 retval;
239         int entries;
240         u16 *hash_list;
241         int add, vid;
242
243         retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
244
245         if (retval)
246                 printk(KERN_ERR "Error receiving message from VF\n");
247
248         /* this is a message we already processed, do nothing */
249         if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
250                 return retval;
251
252         /*
253          * until the vf completes a virtual function reset it should not be
254          * allowed to start any configuration.
255          */
256
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;
264
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);
268                 /*
269                  * Piggyback the multicast filter type so VF can compute the
270                  * correct vectors
271                  */
272                 msgbuf[3] = hw->mac.mc_filter_type;
273                 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
274
275                 return retval;
276         }
277
278         if (!adapter->vfinfo[vf].clear_to_send) {
279                 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
280                 ixgbe_write_mbx(hw, msgbuf, 1, vf);
281                 return retval;
282         }
283
284         switch ((msgbuf[0] & 0xFFFF)) {
285         case IXGBE_VF_SET_MAC_ADDR:
286                 {
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);
291                         else
292                                 ixgbe_set_vf_mac(adapter,
293                                   vf, adapter->vfinfo[vf].vf_mac_addresses);
294                 }
295                 break;
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,
301                                                  hash_list, vf);
302                 break;
303         case IXGBE_VF_SET_LPE:
304                 WARN_ON((msgbuf[0] & 0xFFFF) == IXGBE_VF_SET_LPE);
305                 break;
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);
311                 break;
312         default:
313                 DPRINTK(DRV, ERR, "Unhandled Msg %8.8x\n", msgbuf[0]);
314                 retval = IXGBE_ERR_MBX;
315                 break;
316         }
317
318         /* notify the VF of the results of what it sent us */
319         if (retval)
320                 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
321         else
322                 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
323
324         msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
325
326         ixgbe_write_mbx(hw, msgbuf, 1, vf);
327
328         return retval;
329 }
330
331 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
332 {
333         struct ixgbe_hw *hw = &adapter->hw;
334         u32 msg = IXGBE_VT_MSGTYPE_NACK;
335
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);
339 }
340
341 void ixgbe_msg_task(struct ixgbe_adapter *adapter)
342 {
343         struct ixgbe_hw *hw = &adapter->hw;
344         u32 vf;
345
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);
350
351                 /* process any messages pending */
352                 if (!ixgbe_check_for_msg(hw, vf))
353                         ixgbe_rcv_msg_from_vf(adapter, vf);
354
355                 /* process any acks */
356                 if (!ixgbe_check_for_ack(hw, vf))
357                         ixgbe_rcv_ack_from_vf(adapter, vf);
358         }
359 }
360
361 void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
362 {
363         struct ixgbe_hw *hw = &adapter->hw;
364
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);
368
369         IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
370         IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
371 }
372
373 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
374 {
375         struct ixgbe_hw *hw = &adapter->hw;
376         u32 ping;
377         int i;
378
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);
384         }
385 }
386
387 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
388 {
389         struct ixgbe_adapter *adapter = netdev_priv(netdev);
390         if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
391                 return -EINVAL;
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");
401         }
402         return ixgbe_set_vf_mac(adapter, vf, mac);
403 }
404
405 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
406 {
407         int err = 0;
408         struct ixgbe_adapter *adapter = netdev_priv(netdev);
409
410         if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
411                 return -EINVAL;
412         if (vlan || qos) {
413                 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
414                 if (err)
415                         goto out;
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");
429                 }
430         } else {
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;
437        }
438 out:
439        return err;
440 }
441
442 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
443 {
444         return -EOPNOTSUPP;
445 }
446
447 int ixgbe_ndo_get_vf_config(struct net_device *netdev,
448                             int vf, struct ifla_vf_info *ivi)
449 {
450         struct ixgbe_adapter *adapter = netdev_priv(netdev);
451         if (vf >= adapter->num_vfs)
452                 return -EINVAL;
453         ivi->vf = vf;
454         memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
455         ivi->tx_rate = 0;
456         ivi->vlan = adapter->vfinfo[vf].pf_vlan;
457         ivi->qos = adapter->vfinfo[vf].pf_qos;
458         return 0;
459 }