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