net: remove interrupt.h inclusion from netdevice.h
[linux-2.6-block.git] / drivers / net / enic / enic_main.c
1 /*
2  * Copyright 2008-2010 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * This program is free software; you may redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16  * SOFTWARE.
17  *
18  */
19
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/pci.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/if_ether.h>
32 #include <linux/if_vlan.h>
33 #include <linux/ethtool.h>
34 #include <linux/in.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/tcp.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/prefetch.h>
40 #include <net/ip6_checksum.h>
41
42 #include "cq_enet_desc.h"
43 #include "vnic_dev.h"
44 #include "vnic_intr.h"
45 #include "vnic_stats.h"
46 #include "vnic_vic.h"
47 #include "enic_res.h"
48 #include "enic.h"
49 #include "enic_dev.h"
50 #include "enic_pp.h"
51
52 #define ENIC_NOTIFY_TIMER_PERIOD        (2 * HZ)
53 #define WQ_ENET_MAX_DESC_LEN            (1 << WQ_ENET_LEN_BITS)
54 #define MAX_TSO                         (1 << 16)
55 #define ENIC_DESC_MAX_SPLITS            (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
56
57 #define PCI_DEVICE_ID_CISCO_VIC_ENET         0x0043  /* ethernet vnic */
58 #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN     0x0044  /* enet dynamic vnic */
59
60 /* Supported devices */
61 static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
62         { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
63         { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
64         { 0, }  /* end of table */
65 };
66
67 MODULE_DESCRIPTION(DRV_DESCRIPTION);
68 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
69 MODULE_LICENSE("GPL");
70 MODULE_VERSION(DRV_VERSION);
71 MODULE_DEVICE_TABLE(pci, enic_id_table);
72
73 struct enic_stat {
74         char name[ETH_GSTRING_LEN];
75         unsigned int offset;
76 };
77
78 #define ENIC_TX_STAT(stat)      \
79         { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
80 #define ENIC_RX_STAT(stat)      \
81         { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
82
83 static const struct enic_stat enic_tx_stats[] = {
84         ENIC_TX_STAT(tx_frames_ok),
85         ENIC_TX_STAT(tx_unicast_frames_ok),
86         ENIC_TX_STAT(tx_multicast_frames_ok),
87         ENIC_TX_STAT(tx_broadcast_frames_ok),
88         ENIC_TX_STAT(tx_bytes_ok),
89         ENIC_TX_STAT(tx_unicast_bytes_ok),
90         ENIC_TX_STAT(tx_multicast_bytes_ok),
91         ENIC_TX_STAT(tx_broadcast_bytes_ok),
92         ENIC_TX_STAT(tx_drops),
93         ENIC_TX_STAT(tx_errors),
94         ENIC_TX_STAT(tx_tso),
95 };
96
97 static const struct enic_stat enic_rx_stats[] = {
98         ENIC_RX_STAT(rx_frames_ok),
99         ENIC_RX_STAT(rx_frames_total),
100         ENIC_RX_STAT(rx_unicast_frames_ok),
101         ENIC_RX_STAT(rx_multicast_frames_ok),
102         ENIC_RX_STAT(rx_broadcast_frames_ok),
103         ENIC_RX_STAT(rx_bytes_ok),
104         ENIC_RX_STAT(rx_unicast_bytes_ok),
105         ENIC_RX_STAT(rx_multicast_bytes_ok),
106         ENIC_RX_STAT(rx_broadcast_bytes_ok),
107         ENIC_RX_STAT(rx_drop),
108         ENIC_RX_STAT(rx_no_bufs),
109         ENIC_RX_STAT(rx_errors),
110         ENIC_RX_STAT(rx_rss),
111         ENIC_RX_STAT(rx_crc_errors),
112         ENIC_RX_STAT(rx_frames_64),
113         ENIC_RX_STAT(rx_frames_127),
114         ENIC_RX_STAT(rx_frames_255),
115         ENIC_RX_STAT(rx_frames_511),
116         ENIC_RX_STAT(rx_frames_1023),
117         ENIC_RX_STAT(rx_frames_1518),
118         ENIC_RX_STAT(rx_frames_to_max),
119 };
120
121 static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
122 static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
123
124 static int enic_is_dynamic(struct enic *enic)
125 {
126         return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
127 }
128
129 static inline unsigned int enic_cq_rq(struct enic *enic, unsigned int rq)
130 {
131         return rq;
132 }
133
134 static inline unsigned int enic_cq_wq(struct enic *enic, unsigned int wq)
135 {
136         return enic->rq_count + wq;
137 }
138
139 static inline unsigned int enic_legacy_io_intr(void)
140 {
141         return 0;
142 }
143
144 static inline unsigned int enic_legacy_err_intr(void)
145 {
146         return 1;
147 }
148
149 static inline unsigned int enic_legacy_notify_intr(void)
150 {
151         return 2;
152 }
153
154 static inline unsigned int enic_msix_rq_intr(struct enic *enic, unsigned int rq)
155 {
156         return rq;
157 }
158
159 static inline unsigned int enic_msix_wq_intr(struct enic *enic, unsigned int wq)
160 {
161         return enic->rq_count + wq;
162 }
163
164 static inline unsigned int enic_msix_err_intr(struct enic *enic)
165 {
166         return enic->rq_count + enic->wq_count;
167 }
168
169 static inline unsigned int enic_msix_notify_intr(struct enic *enic)
170 {
171         return enic->rq_count + enic->wq_count + 1;
172 }
173
174 static int enic_get_settings(struct net_device *netdev,
175         struct ethtool_cmd *ecmd)
176 {
177         struct enic *enic = netdev_priv(netdev);
178
179         ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
180         ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
181         ecmd->port = PORT_FIBRE;
182         ecmd->transceiver = XCVR_EXTERNAL;
183
184         if (netif_carrier_ok(netdev)) {
185                 ethtool_cmd_speed_set(ecmd, vnic_dev_port_speed(enic->vdev));
186                 ecmd->duplex = DUPLEX_FULL;
187         } else {
188                 ethtool_cmd_speed_set(ecmd, -1);
189                 ecmd->duplex = -1;
190         }
191
192         ecmd->autoneg = AUTONEG_DISABLE;
193
194         return 0;
195 }
196
197 static void enic_get_drvinfo(struct net_device *netdev,
198         struct ethtool_drvinfo *drvinfo)
199 {
200         struct enic *enic = netdev_priv(netdev);
201         struct vnic_devcmd_fw_info *fw_info;
202
203         enic_dev_fw_info(enic, &fw_info);
204
205         strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
206         strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
207         strncpy(drvinfo->fw_version, fw_info->fw_version,
208                 sizeof(drvinfo->fw_version));
209         strncpy(drvinfo->bus_info, pci_name(enic->pdev),
210                 sizeof(drvinfo->bus_info));
211 }
212
213 static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
214 {
215         unsigned int i;
216
217         switch (stringset) {
218         case ETH_SS_STATS:
219                 for (i = 0; i < enic_n_tx_stats; i++) {
220                         memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
221                         data += ETH_GSTRING_LEN;
222                 }
223                 for (i = 0; i < enic_n_rx_stats; i++) {
224                         memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
225                         data += ETH_GSTRING_LEN;
226                 }
227                 break;
228         }
229 }
230
231 static int enic_get_sset_count(struct net_device *netdev, int sset)
232 {
233         switch (sset) {
234         case ETH_SS_STATS:
235                 return enic_n_tx_stats + enic_n_rx_stats;
236         default:
237                 return -EOPNOTSUPP;
238         }
239 }
240
241 static void enic_get_ethtool_stats(struct net_device *netdev,
242         struct ethtool_stats *stats, u64 *data)
243 {
244         struct enic *enic = netdev_priv(netdev);
245         struct vnic_stats *vstats;
246         unsigned int i;
247
248         enic_dev_stats_dump(enic, &vstats);
249
250         for (i = 0; i < enic_n_tx_stats; i++)
251                 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
252         for (i = 0; i < enic_n_rx_stats; i++)
253                 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
254 }
255
256 static u32 enic_get_msglevel(struct net_device *netdev)
257 {
258         struct enic *enic = netdev_priv(netdev);
259         return enic->msg_enable;
260 }
261
262 static void enic_set_msglevel(struct net_device *netdev, u32 value)
263 {
264         struct enic *enic = netdev_priv(netdev);
265         enic->msg_enable = value;
266 }
267
268 static int enic_get_coalesce(struct net_device *netdev,
269         struct ethtool_coalesce *ecmd)
270 {
271         struct enic *enic = netdev_priv(netdev);
272
273         ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
274         ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
275
276         return 0;
277 }
278
279 static int enic_set_coalesce(struct net_device *netdev,
280         struct ethtool_coalesce *ecmd)
281 {
282         struct enic *enic = netdev_priv(netdev);
283         u32 tx_coalesce_usecs;
284         u32 rx_coalesce_usecs;
285         unsigned int i, intr;
286
287         tx_coalesce_usecs = min_t(u32,
288                 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
289                 ecmd->tx_coalesce_usecs);
290         rx_coalesce_usecs = min_t(u32,
291                 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
292                 ecmd->rx_coalesce_usecs);
293
294         switch (vnic_dev_get_intr_mode(enic->vdev)) {
295         case VNIC_DEV_INTR_MODE_INTX:
296                 if (tx_coalesce_usecs != rx_coalesce_usecs)
297                         return -EINVAL;
298
299                 intr = enic_legacy_io_intr();
300                 vnic_intr_coalescing_timer_set(&enic->intr[intr],
301                         INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
302                 break;
303         case VNIC_DEV_INTR_MODE_MSI:
304                 if (tx_coalesce_usecs != rx_coalesce_usecs)
305                         return -EINVAL;
306
307                 vnic_intr_coalescing_timer_set(&enic->intr[0],
308                         INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
309                 break;
310         case VNIC_DEV_INTR_MODE_MSIX:
311                 for (i = 0; i < enic->wq_count; i++) {
312                         intr = enic_msix_wq_intr(enic, i);
313                         vnic_intr_coalescing_timer_set(&enic->intr[intr],
314                                 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
315                 }
316
317                 for (i = 0; i < enic->rq_count; i++) {
318                         intr = enic_msix_rq_intr(enic, i);
319                         vnic_intr_coalescing_timer_set(&enic->intr[intr],
320                                 INTR_COALESCE_USEC_TO_HW(rx_coalesce_usecs));
321                 }
322
323                 break;
324         default:
325                 break;
326         }
327
328         enic->tx_coalesce_usecs = tx_coalesce_usecs;
329         enic->rx_coalesce_usecs = rx_coalesce_usecs;
330
331         return 0;
332 }
333
334 static const struct ethtool_ops enic_ethtool_ops = {
335         .get_settings = enic_get_settings,
336         .get_drvinfo = enic_get_drvinfo,
337         .get_msglevel = enic_get_msglevel,
338         .set_msglevel = enic_set_msglevel,
339         .get_link = ethtool_op_get_link,
340         .get_strings = enic_get_strings,
341         .get_sset_count = enic_get_sset_count,
342         .get_ethtool_stats = enic_get_ethtool_stats,
343         .get_coalesce = enic_get_coalesce,
344         .set_coalesce = enic_set_coalesce,
345 };
346
347 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
348 {
349         struct enic *enic = vnic_dev_priv(wq->vdev);
350
351         if (buf->sop)
352                 pci_unmap_single(enic->pdev, buf->dma_addr,
353                         buf->len, PCI_DMA_TODEVICE);
354         else
355                 pci_unmap_page(enic->pdev, buf->dma_addr,
356                         buf->len, PCI_DMA_TODEVICE);
357
358         if (buf->os_buf)
359                 dev_kfree_skb_any(buf->os_buf);
360 }
361
362 static void enic_wq_free_buf(struct vnic_wq *wq,
363         struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
364 {
365         enic_free_wq_buf(wq, buf);
366 }
367
368 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
369         u8 type, u16 q_number, u16 completed_index, void *opaque)
370 {
371         struct enic *enic = vnic_dev_priv(vdev);
372
373         spin_lock(&enic->wq_lock[q_number]);
374
375         vnic_wq_service(&enic->wq[q_number], cq_desc,
376                 completed_index, enic_wq_free_buf,
377                 opaque);
378
379         if (netif_queue_stopped(enic->netdev) &&
380             vnic_wq_desc_avail(&enic->wq[q_number]) >=
381             (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
382                 netif_wake_queue(enic->netdev);
383
384         spin_unlock(&enic->wq_lock[q_number]);
385
386         return 0;
387 }
388
389 static void enic_log_q_error(struct enic *enic)
390 {
391         unsigned int i;
392         u32 error_status;
393
394         for (i = 0; i < enic->wq_count; i++) {
395                 error_status = vnic_wq_error_status(&enic->wq[i]);
396                 if (error_status)
397                         netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
398                                 i, error_status);
399         }
400
401         for (i = 0; i < enic->rq_count; i++) {
402                 error_status = vnic_rq_error_status(&enic->rq[i]);
403                 if (error_status)
404                         netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
405                                 i, error_status);
406         }
407 }
408
409 static void enic_msglvl_check(struct enic *enic)
410 {
411         u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
412
413         if (msg_enable != enic->msg_enable) {
414                 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
415                         enic->msg_enable, msg_enable);
416                 enic->msg_enable = msg_enable;
417         }
418 }
419
420 static void enic_mtu_check(struct enic *enic)
421 {
422         u32 mtu = vnic_dev_mtu(enic->vdev);
423         struct net_device *netdev = enic->netdev;
424
425         if (mtu && mtu != enic->port_mtu) {
426                 enic->port_mtu = mtu;
427                 if (enic_is_dynamic(enic)) {
428                         mtu = max_t(int, ENIC_MIN_MTU,
429                                 min_t(int, ENIC_MAX_MTU, mtu));
430                         if (mtu != netdev->mtu)
431                                 schedule_work(&enic->change_mtu_work);
432                 } else {
433                         if (mtu < netdev->mtu)
434                                 netdev_warn(netdev,
435                                         "interface MTU (%d) set higher "
436                                         "than switch port MTU (%d)\n",
437                                         netdev->mtu, mtu);
438                 }
439         }
440 }
441
442 static void enic_link_check(struct enic *enic)
443 {
444         int link_status = vnic_dev_link_status(enic->vdev);
445         int carrier_ok = netif_carrier_ok(enic->netdev);
446
447         if (link_status && !carrier_ok) {
448                 netdev_info(enic->netdev, "Link UP\n");
449                 netif_carrier_on(enic->netdev);
450         } else if (!link_status && carrier_ok) {
451                 netdev_info(enic->netdev, "Link DOWN\n");
452                 netif_carrier_off(enic->netdev);
453         }
454 }
455
456 static void enic_notify_check(struct enic *enic)
457 {
458         enic_msglvl_check(enic);
459         enic_mtu_check(enic);
460         enic_link_check(enic);
461 }
462
463 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
464
465 static irqreturn_t enic_isr_legacy(int irq, void *data)
466 {
467         struct net_device *netdev = data;
468         struct enic *enic = netdev_priv(netdev);
469         unsigned int io_intr = enic_legacy_io_intr();
470         unsigned int err_intr = enic_legacy_err_intr();
471         unsigned int notify_intr = enic_legacy_notify_intr();
472         u32 pba;
473
474         vnic_intr_mask(&enic->intr[io_intr]);
475
476         pba = vnic_intr_legacy_pba(enic->legacy_pba);
477         if (!pba) {
478                 vnic_intr_unmask(&enic->intr[io_intr]);
479                 return IRQ_NONE;        /* not our interrupt */
480         }
481
482         if (ENIC_TEST_INTR(pba, notify_intr)) {
483                 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
484                 enic_notify_check(enic);
485         }
486
487         if (ENIC_TEST_INTR(pba, err_intr)) {
488                 vnic_intr_return_all_credits(&enic->intr[err_intr]);
489                 enic_log_q_error(enic);
490                 /* schedule recovery from WQ/RQ error */
491                 schedule_work(&enic->reset);
492                 return IRQ_HANDLED;
493         }
494
495         if (ENIC_TEST_INTR(pba, io_intr)) {
496                 if (napi_schedule_prep(&enic->napi[0]))
497                         __napi_schedule(&enic->napi[0]);
498         } else {
499                 vnic_intr_unmask(&enic->intr[io_intr]);
500         }
501
502         return IRQ_HANDLED;
503 }
504
505 static irqreturn_t enic_isr_msi(int irq, void *data)
506 {
507         struct enic *enic = data;
508
509         /* With MSI, there is no sharing of interrupts, so this is
510          * our interrupt and there is no need to ack it.  The device
511          * is not providing per-vector masking, so the OS will not
512          * write to PCI config space to mask/unmask the interrupt.
513          * We're using mask_on_assertion for MSI, so the device
514          * automatically masks the interrupt when the interrupt is
515          * generated.  Later, when exiting polling, the interrupt
516          * will be unmasked (see enic_poll).
517          *
518          * Also, the device uses the same PCIe Traffic Class (TC)
519          * for Memory Write data and MSI, so there are no ordering
520          * issues; the MSI will always arrive at the Root Complex
521          * _after_ corresponding Memory Writes (i.e. descriptor
522          * writes).
523          */
524
525         napi_schedule(&enic->napi[0]);
526
527         return IRQ_HANDLED;
528 }
529
530 static irqreturn_t enic_isr_msix_rq(int irq, void *data)
531 {
532         struct napi_struct *napi = data;
533
534         /* schedule NAPI polling for RQ cleanup */
535         napi_schedule(napi);
536
537         return IRQ_HANDLED;
538 }
539
540 static irqreturn_t enic_isr_msix_wq(int irq, void *data)
541 {
542         struct enic *enic = data;
543         unsigned int cq = enic_cq_wq(enic, 0);
544         unsigned int intr = enic_msix_wq_intr(enic, 0);
545         unsigned int wq_work_to_do = -1; /* no limit */
546         unsigned int wq_work_done;
547
548         wq_work_done = vnic_cq_service(&enic->cq[cq],
549                 wq_work_to_do, enic_wq_service, NULL);
550
551         vnic_intr_return_credits(&enic->intr[intr],
552                 wq_work_done,
553                 1 /* unmask intr */,
554                 1 /* reset intr timer */);
555
556         return IRQ_HANDLED;
557 }
558
559 static irqreturn_t enic_isr_msix_err(int irq, void *data)
560 {
561         struct enic *enic = data;
562         unsigned int intr = enic_msix_err_intr(enic);
563
564         vnic_intr_return_all_credits(&enic->intr[intr]);
565
566         enic_log_q_error(enic);
567
568         /* schedule recovery from WQ/RQ error */
569         schedule_work(&enic->reset);
570
571         return IRQ_HANDLED;
572 }
573
574 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
575 {
576         struct enic *enic = data;
577         unsigned int intr = enic_msix_notify_intr(enic);
578
579         vnic_intr_return_all_credits(&enic->intr[intr]);
580         enic_notify_check(enic);
581
582         return IRQ_HANDLED;
583 }
584
585 static inline void enic_queue_wq_skb_cont(struct enic *enic,
586         struct vnic_wq *wq, struct sk_buff *skb,
587         unsigned int len_left, int loopback)
588 {
589         skb_frag_t *frag;
590
591         /* Queue additional data fragments */
592         for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
593                 len_left -= frag->size;
594                 enic_queue_wq_desc_cont(wq, skb,
595                         pci_map_page(enic->pdev, frag->page,
596                                 frag->page_offset, frag->size,
597                                 PCI_DMA_TODEVICE),
598                         frag->size,
599                         (len_left == 0),        /* EOP? */
600                         loopback);
601         }
602 }
603
604 static inline void enic_queue_wq_skb_vlan(struct enic *enic,
605         struct vnic_wq *wq, struct sk_buff *skb,
606         int vlan_tag_insert, unsigned int vlan_tag, int loopback)
607 {
608         unsigned int head_len = skb_headlen(skb);
609         unsigned int len_left = skb->len - head_len;
610         int eop = (len_left == 0);
611
612         /* Queue the main skb fragment. The fragments are no larger
613          * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
614          * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
615          * per fragment is queued.
616          */
617         enic_queue_wq_desc(wq, skb,
618                 pci_map_single(enic->pdev, skb->data,
619                         head_len, PCI_DMA_TODEVICE),
620                 head_len,
621                 vlan_tag_insert, vlan_tag,
622                 eop, loopback);
623
624         if (!eop)
625                 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
626 }
627
628 static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
629         struct vnic_wq *wq, struct sk_buff *skb,
630         int vlan_tag_insert, unsigned int vlan_tag, int loopback)
631 {
632         unsigned int head_len = skb_headlen(skb);
633         unsigned int len_left = skb->len - head_len;
634         unsigned int hdr_len = skb_checksum_start_offset(skb);
635         unsigned int csum_offset = hdr_len + skb->csum_offset;
636         int eop = (len_left == 0);
637
638         /* Queue the main skb fragment. The fragments are no larger
639          * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
640          * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
641          * per fragment is queued.
642          */
643         enic_queue_wq_desc_csum_l4(wq, skb,
644                 pci_map_single(enic->pdev, skb->data,
645                         head_len, PCI_DMA_TODEVICE),
646                 head_len,
647                 csum_offset,
648                 hdr_len,
649                 vlan_tag_insert, vlan_tag,
650                 eop, loopback);
651
652         if (!eop)
653                 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
654 }
655
656 static inline void enic_queue_wq_skb_tso(struct enic *enic,
657         struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
658         int vlan_tag_insert, unsigned int vlan_tag, int loopback)
659 {
660         unsigned int frag_len_left = skb_headlen(skb);
661         unsigned int len_left = skb->len - frag_len_left;
662         unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
663         int eop = (len_left == 0);
664         unsigned int len;
665         dma_addr_t dma_addr;
666         unsigned int offset = 0;
667         skb_frag_t *frag;
668
669         /* Preload TCP csum field with IP pseudo hdr calculated
670          * with IP length set to zero.  HW will later add in length
671          * to each TCP segment resulting from the TSO.
672          */
673
674         if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
675                 ip_hdr(skb)->check = 0;
676                 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
677                         ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
678         } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
679                 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
680                         &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
681         }
682
683         /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
684          * for the main skb fragment
685          */
686         while (frag_len_left) {
687                 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
688                 dma_addr = pci_map_single(enic->pdev, skb->data + offset,
689                                 len, PCI_DMA_TODEVICE);
690                 enic_queue_wq_desc_tso(wq, skb,
691                         dma_addr,
692                         len,
693                         mss, hdr_len,
694                         vlan_tag_insert, vlan_tag,
695                         eop && (len == frag_len_left), loopback);
696                 frag_len_left -= len;
697                 offset += len;
698         }
699
700         if (eop)
701                 return;
702
703         /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
704          * for additional data fragments
705          */
706         for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
707                 len_left -= frag->size;
708                 frag_len_left = frag->size;
709                 offset = frag->page_offset;
710
711                 while (frag_len_left) {
712                         len = min(frag_len_left,
713                                 (unsigned int)WQ_ENET_MAX_DESC_LEN);
714                         dma_addr = pci_map_page(enic->pdev, frag->page,
715                                 offset, len,
716                                 PCI_DMA_TODEVICE);
717                         enic_queue_wq_desc_cont(wq, skb,
718                                 dma_addr,
719                                 len,
720                                 (len_left == 0) &&
721                                 (len == frag_len_left),         /* EOP? */
722                                 loopback);
723                         frag_len_left -= len;
724                         offset += len;
725                 }
726         }
727 }
728
729 static inline void enic_queue_wq_skb(struct enic *enic,
730         struct vnic_wq *wq, struct sk_buff *skb)
731 {
732         unsigned int mss = skb_shinfo(skb)->gso_size;
733         unsigned int vlan_tag = 0;
734         int vlan_tag_insert = 0;
735         int loopback = 0;
736
737         if (vlan_tx_tag_present(skb)) {
738                 /* VLAN tag from trunking driver */
739                 vlan_tag_insert = 1;
740                 vlan_tag = vlan_tx_tag_get(skb);
741         } else if (enic->loop_enable) {
742                 vlan_tag = enic->loop_tag;
743                 loopback = 1;
744         }
745
746         if (mss)
747                 enic_queue_wq_skb_tso(enic, wq, skb, mss,
748                         vlan_tag_insert, vlan_tag, loopback);
749         else if (skb->ip_summed == CHECKSUM_PARTIAL)
750                 enic_queue_wq_skb_csum_l4(enic, wq, skb,
751                         vlan_tag_insert, vlan_tag, loopback);
752         else
753                 enic_queue_wq_skb_vlan(enic, wq, skb,
754                         vlan_tag_insert, vlan_tag, loopback);
755 }
756
757 /* netif_tx_lock held, process context with BHs disabled, or BH */
758 static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
759         struct net_device *netdev)
760 {
761         struct enic *enic = netdev_priv(netdev);
762         struct vnic_wq *wq = &enic->wq[0];
763         unsigned long flags;
764
765         if (skb->len <= 0) {
766                 dev_kfree_skb(skb);
767                 return NETDEV_TX_OK;
768         }
769
770         /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
771          * which is very likely.  In the off chance it's going to take
772          * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
773          */
774
775         if (skb_shinfo(skb)->gso_size == 0 &&
776             skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
777             skb_linearize(skb)) {
778                 dev_kfree_skb(skb);
779                 return NETDEV_TX_OK;
780         }
781
782         spin_lock_irqsave(&enic->wq_lock[0], flags);
783
784         if (vnic_wq_desc_avail(wq) <
785             skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
786                 netif_stop_queue(netdev);
787                 /* This is a hard error, log it */
788                 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
789                 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
790                 return NETDEV_TX_BUSY;
791         }
792
793         enic_queue_wq_skb(enic, wq, skb);
794
795         if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
796                 netif_stop_queue(netdev);
797
798         spin_unlock_irqrestore(&enic->wq_lock[0], flags);
799
800         return NETDEV_TX_OK;
801 }
802
803 /* dev_base_lock rwlock held, nominally process context */
804 static struct net_device_stats *enic_get_stats(struct net_device *netdev)
805 {
806         struct enic *enic = netdev_priv(netdev);
807         struct net_device_stats *net_stats = &netdev->stats;
808         struct vnic_stats *stats;
809
810         enic_dev_stats_dump(enic, &stats);
811
812         net_stats->tx_packets = stats->tx.tx_frames_ok;
813         net_stats->tx_bytes = stats->tx.tx_bytes_ok;
814         net_stats->tx_errors = stats->tx.tx_errors;
815         net_stats->tx_dropped = stats->tx.tx_drops;
816
817         net_stats->rx_packets = stats->rx.rx_frames_ok;
818         net_stats->rx_bytes = stats->rx.rx_bytes_ok;
819         net_stats->rx_errors = stats->rx.rx_errors;
820         net_stats->multicast = stats->rx.rx_multicast_frames_ok;
821         net_stats->rx_over_errors = enic->rq_truncated_pkts;
822         net_stats->rx_crc_errors = enic->rq_bad_fcs;
823         net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
824
825         return net_stats;
826 }
827
828 void enic_reset_addr_lists(struct enic *enic)
829 {
830         enic->mc_count = 0;
831         enic->uc_count = 0;
832         enic->flags = 0;
833 }
834
835 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
836 {
837         struct enic *enic = netdev_priv(netdev);
838
839         if (enic_is_dynamic(enic)) {
840                 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
841                         return -EADDRNOTAVAIL;
842         } else {
843                 if (!is_valid_ether_addr(addr))
844                         return -EADDRNOTAVAIL;
845         }
846
847         memcpy(netdev->dev_addr, addr, netdev->addr_len);
848
849         return 0;
850 }
851
852 static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
853 {
854         struct enic *enic = netdev_priv(netdev);
855         struct sockaddr *saddr = p;
856         char *addr = saddr->sa_data;
857         int err;
858
859         if (netif_running(enic->netdev)) {
860                 err = enic_dev_del_station_addr(enic);
861                 if (err)
862                         return err;
863         }
864
865         err = enic_set_mac_addr(netdev, addr);
866         if (err)
867                 return err;
868
869         if (netif_running(enic->netdev)) {
870                 err = enic_dev_add_station_addr(enic);
871                 if (err)
872                         return err;
873         }
874
875         return err;
876 }
877
878 static int enic_set_mac_address(struct net_device *netdev, void *p)
879 {
880         struct sockaddr *saddr = p;
881         char *addr = saddr->sa_data;
882         struct enic *enic = netdev_priv(netdev);
883         int err;
884
885         err = enic_dev_del_station_addr(enic);
886         if (err)
887                 return err;
888
889         err = enic_set_mac_addr(netdev, addr);
890         if (err)
891                 return err;
892
893         return enic_dev_add_station_addr(enic);
894 }
895
896 static void enic_update_multicast_addr_list(struct enic *enic)
897 {
898         struct net_device *netdev = enic->netdev;
899         struct netdev_hw_addr *ha;
900         unsigned int mc_count = netdev_mc_count(netdev);
901         u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
902         unsigned int i, j;
903
904         if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) {
905                 netdev_warn(netdev, "Registering only %d out of %d "
906                         "multicast addresses\n",
907                         ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
908                 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
909         }
910
911         /* Is there an easier way?  Trying to minimize to
912          * calls to add/del multicast addrs.  We keep the
913          * addrs from the last call in enic->mc_addr and
914          * look for changes to add/del.
915          */
916
917         i = 0;
918         netdev_for_each_mc_addr(ha, netdev) {
919                 if (i == mc_count)
920                         break;
921                 memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
922         }
923
924         for (i = 0; i < enic->mc_count; i++) {
925                 for (j = 0; j < mc_count; j++)
926                         if (compare_ether_addr(enic->mc_addr[i],
927                                 mc_addr[j]) == 0)
928                                 break;
929                 if (j == mc_count)
930                         enic_dev_del_addr(enic, enic->mc_addr[i]);
931         }
932
933         for (i = 0; i < mc_count; i++) {
934                 for (j = 0; j < enic->mc_count; j++)
935                         if (compare_ether_addr(mc_addr[i],
936                                 enic->mc_addr[j]) == 0)
937                                 break;
938                 if (j == enic->mc_count)
939                         enic_dev_add_addr(enic, mc_addr[i]);
940         }
941
942         /* Save the list to compare against next time
943          */
944
945         for (i = 0; i < mc_count; i++)
946                 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
947
948         enic->mc_count = mc_count;
949 }
950
951 static void enic_update_unicast_addr_list(struct enic *enic)
952 {
953         struct net_device *netdev = enic->netdev;
954         struct netdev_hw_addr *ha;
955         unsigned int uc_count = netdev_uc_count(netdev);
956         u8 uc_addr[ENIC_UNICAST_PERFECT_FILTERS][ETH_ALEN];
957         unsigned int i, j;
958
959         if (uc_count > ENIC_UNICAST_PERFECT_FILTERS) {
960                 netdev_warn(netdev, "Registering only %d out of %d "
961                         "unicast addresses\n",
962                         ENIC_UNICAST_PERFECT_FILTERS, uc_count);
963                 uc_count = ENIC_UNICAST_PERFECT_FILTERS;
964         }
965
966         /* Is there an easier way?  Trying to minimize to
967          * calls to add/del unicast addrs.  We keep the
968          * addrs from the last call in enic->uc_addr and
969          * look for changes to add/del.
970          */
971
972         i = 0;
973         netdev_for_each_uc_addr(ha, netdev) {
974                 if (i == uc_count)
975                         break;
976                 memcpy(uc_addr[i++], ha->addr, ETH_ALEN);
977         }
978
979         for (i = 0; i < enic->uc_count; i++) {
980                 for (j = 0; j < uc_count; j++)
981                         if (compare_ether_addr(enic->uc_addr[i],
982                                 uc_addr[j]) == 0)
983                                 break;
984                 if (j == uc_count)
985                         enic_dev_del_addr(enic, enic->uc_addr[i]);
986         }
987
988         for (i = 0; i < uc_count; i++) {
989                 for (j = 0; j < enic->uc_count; j++)
990                         if (compare_ether_addr(uc_addr[i],
991                                 enic->uc_addr[j]) == 0)
992                                 break;
993                 if (j == enic->uc_count)
994                         enic_dev_add_addr(enic, uc_addr[i]);
995         }
996
997         /* Save the list to compare against next time
998          */
999
1000         for (i = 0; i < uc_count; i++)
1001                 memcpy(enic->uc_addr[i], uc_addr[i], ETH_ALEN);
1002
1003         enic->uc_count = uc_count;
1004 }
1005
1006 /* netif_tx_lock held, BHs disabled */
1007 static void enic_set_rx_mode(struct net_device *netdev)
1008 {
1009         struct enic *enic = netdev_priv(netdev);
1010         int directed = 1;
1011         int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1012         int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1013         int promisc = (netdev->flags & IFF_PROMISC) ||
1014                 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1015         int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1016                 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1017         unsigned int flags = netdev->flags |
1018                 (allmulti ? IFF_ALLMULTI : 0) |
1019                 (promisc ? IFF_PROMISC : 0);
1020
1021         if (enic->flags != flags) {
1022                 enic->flags = flags;
1023                 enic_dev_packet_filter(enic, directed,
1024                         multicast, broadcast, promisc, allmulti);
1025         }
1026
1027         if (!promisc) {
1028                 enic_update_unicast_addr_list(enic);
1029                 if (!allmulti)
1030                         enic_update_multicast_addr_list(enic);
1031         }
1032 }
1033
1034 /* rtnl lock is held */
1035 static void enic_vlan_rx_register(struct net_device *netdev,
1036         struct vlan_group *vlan_group)
1037 {
1038         struct enic *enic = netdev_priv(netdev);
1039         enic->vlan_group = vlan_group;
1040 }
1041
1042 /* netif_tx_lock held, BHs disabled */
1043 static void enic_tx_timeout(struct net_device *netdev)
1044 {
1045         struct enic *enic = netdev_priv(netdev);
1046         schedule_work(&enic->reset);
1047 }
1048
1049 static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1050 {
1051         struct enic *enic = netdev_priv(netdev);
1052
1053         if (vf != PORT_SELF_VF)
1054                 return -EOPNOTSUPP;
1055
1056         /* Ignore the vf argument for now. We can assume the request
1057          * is coming on a vf.
1058          */
1059         if (is_valid_ether_addr(mac)) {
1060                 memcpy(enic->pp.vf_mac, mac, ETH_ALEN);
1061                 return 0;
1062         } else
1063                 return -EINVAL;
1064 }
1065
1066 static int enic_set_vf_port(struct net_device *netdev, int vf,
1067         struct nlattr *port[])
1068 {
1069         struct enic *enic = netdev_priv(netdev);
1070         struct enic_port_profile prev_pp;
1071         int err = 0, restore_pp = 1;
1072
1073         /* don't support VFs, yet */
1074         if (vf != PORT_SELF_VF)
1075                 return -EOPNOTSUPP;
1076
1077         if (!port[IFLA_PORT_REQUEST])
1078                 return -EOPNOTSUPP;
1079
1080         memcpy(&prev_pp, &enic->pp, sizeof(enic->pp));
1081         memset(&enic->pp, 0, sizeof(enic->pp));
1082
1083         enic->pp.set |= ENIC_SET_REQUEST;
1084         enic->pp.request = nla_get_u8(port[IFLA_PORT_REQUEST]);
1085
1086         if (port[IFLA_PORT_PROFILE]) {
1087                 enic->pp.set |= ENIC_SET_NAME;
1088                 memcpy(enic->pp.name, nla_data(port[IFLA_PORT_PROFILE]),
1089                         PORT_PROFILE_MAX);
1090         }
1091
1092         if (port[IFLA_PORT_INSTANCE_UUID]) {
1093                 enic->pp.set |= ENIC_SET_INSTANCE;
1094                 memcpy(enic->pp.instance_uuid,
1095                         nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1096         }
1097
1098         if (port[IFLA_PORT_HOST_UUID]) {
1099                 enic->pp.set |= ENIC_SET_HOST;
1100                 memcpy(enic->pp.host_uuid,
1101                         nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1102         }
1103
1104         /* Special case handling: mac came from IFLA_VF_MAC */
1105         if (!is_zero_ether_addr(prev_pp.vf_mac))
1106                 memcpy(enic->pp.mac_addr, prev_pp.vf_mac, ETH_ALEN);
1107
1108                 if (is_zero_ether_addr(netdev->dev_addr))
1109                         random_ether_addr(netdev->dev_addr);
1110
1111         err = enic_process_set_pp_request(enic, &prev_pp, &restore_pp);
1112         if (err) {
1113                 if (restore_pp) {
1114                         /* Things are still the way they were: Implicit
1115                          * DISASSOCIATE failed
1116                          */
1117                         memcpy(&enic->pp, &prev_pp, sizeof(enic->pp));
1118                 } else {
1119                         memset(&enic->pp, 0, sizeof(enic->pp));
1120                         memset(netdev->dev_addr, 0, ETH_ALEN);
1121                 }
1122         } else {
1123                 /* Set flag to indicate that the port assoc/disassoc
1124                  * request has been sent out to fw
1125                  */
1126                 enic->pp.set |= ENIC_PORT_REQUEST_APPLIED;
1127
1128                 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
1129                 if (enic->pp.request == PORT_REQUEST_DISASSOCIATE) {
1130                         memset(enic->pp.mac_addr, 0, ETH_ALEN);
1131                         memset(netdev->dev_addr, 0, ETH_ALEN);
1132                 }
1133         }
1134
1135         memset(enic->pp.vf_mac, 0, ETH_ALEN);
1136
1137         return err;
1138 }
1139
1140 static int enic_get_vf_port(struct net_device *netdev, int vf,
1141         struct sk_buff *skb)
1142 {
1143         struct enic *enic = netdev_priv(netdev);
1144         u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
1145         int err;
1146
1147         if (!(enic->pp.set & ENIC_PORT_REQUEST_APPLIED))
1148                 return -ENODATA;
1149
1150         err = enic_process_get_pp_request(enic, enic->pp.request, &response);
1151         if (err)
1152                 return err;
1153
1154         NLA_PUT_U16(skb, IFLA_PORT_REQUEST, enic->pp.request);
1155         NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
1156         if (enic->pp.set & ENIC_SET_NAME)
1157                 NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
1158                         enic->pp.name);
1159         if (enic->pp.set & ENIC_SET_INSTANCE)
1160                 NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1161                         enic->pp.instance_uuid);
1162         if (enic->pp.set & ENIC_SET_HOST)
1163                 NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
1164                         enic->pp.host_uuid);
1165
1166         return 0;
1167
1168 nla_put_failure:
1169         return -EMSGSIZE;
1170 }
1171
1172 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1173 {
1174         struct enic *enic = vnic_dev_priv(rq->vdev);
1175
1176         if (!buf->os_buf)
1177                 return;
1178
1179         pci_unmap_single(enic->pdev, buf->dma_addr,
1180                 buf->len, PCI_DMA_FROMDEVICE);
1181         dev_kfree_skb_any(buf->os_buf);
1182 }
1183
1184 static int enic_rq_alloc_buf(struct vnic_rq *rq)
1185 {
1186         struct enic *enic = vnic_dev_priv(rq->vdev);
1187         struct net_device *netdev = enic->netdev;
1188         struct sk_buff *skb;
1189         unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
1190         unsigned int os_buf_index = 0;
1191         dma_addr_t dma_addr;
1192
1193         skb = netdev_alloc_skb_ip_align(netdev, len);
1194         if (!skb)
1195                 return -ENOMEM;
1196
1197         dma_addr = pci_map_single(enic->pdev, skb->data,
1198                 len, PCI_DMA_FROMDEVICE);
1199
1200         enic_queue_rq_desc(rq, skb, os_buf_index,
1201                 dma_addr, len);
1202
1203         return 0;
1204 }
1205
1206 static void enic_rq_indicate_buf(struct vnic_rq *rq,
1207         struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1208         int skipped, void *opaque)
1209 {
1210         struct enic *enic = vnic_dev_priv(rq->vdev);
1211         struct net_device *netdev = enic->netdev;
1212         struct sk_buff *skb;
1213
1214         u8 type, color, eop, sop, ingress_port, vlan_stripped;
1215         u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1216         u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1217         u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1218         u8 packet_error;
1219         u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
1220         u32 rss_hash;
1221
1222         if (skipped)
1223                 return;
1224
1225         skb = buf->os_buf;
1226         prefetch(skb->data - NET_IP_ALIGN);
1227         pci_unmap_single(enic->pdev, buf->dma_addr,
1228                 buf->len, PCI_DMA_FROMDEVICE);
1229
1230         cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1231                 &type, &color, &q_number, &completed_index,
1232                 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1233                 &csum_not_calc, &rss_hash, &bytes_written,
1234                 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
1235                 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1236                 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1237                 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1238                 &fcs_ok);
1239
1240         if (packet_error) {
1241
1242                 if (!fcs_ok) {
1243                         if (bytes_written > 0)
1244                                 enic->rq_bad_fcs++;
1245                         else if (bytes_written == 0)
1246                                 enic->rq_truncated_pkts++;
1247                 }
1248
1249                 dev_kfree_skb_any(skb);
1250
1251                 return;
1252         }
1253
1254         if (eop && bytes_written > 0) {
1255
1256                 /* Good receive
1257                  */
1258
1259                 skb_put(skb, bytes_written);
1260                 skb->protocol = eth_type_trans(skb, netdev);
1261
1262                 if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc) {
1263                         skb->csum = htons(checksum);
1264                         skb->ip_summed = CHECKSUM_COMPLETE;
1265                 }
1266
1267                 skb->dev = netdev;
1268
1269                 if (enic->vlan_group && vlan_stripped &&
1270                         (vlan_tci & CQ_ENET_RQ_DESC_VLAN_TCI_VLAN_MASK)) {
1271
1272                         if (netdev->features & NETIF_F_GRO)
1273                                 vlan_gro_receive(&enic->napi[q_number],
1274                                         enic->vlan_group, vlan_tci, skb);
1275                         else
1276                                 vlan_hwaccel_receive_skb(skb,
1277                                         enic->vlan_group, vlan_tci);
1278
1279                 } else {
1280
1281                         if (netdev->features & NETIF_F_GRO)
1282                                 napi_gro_receive(&enic->napi[q_number], skb);
1283                         else
1284                                 netif_receive_skb(skb);
1285
1286                 }
1287         } else {
1288
1289                 /* Buffer overflow
1290                  */
1291
1292                 dev_kfree_skb_any(skb);
1293         }
1294 }
1295
1296 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1297         u8 type, u16 q_number, u16 completed_index, void *opaque)
1298 {
1299         struct enic *enic = vnic_dev_priv(vdev);
1300
1301         vnic_rq_service(&enic->rq[q_number], cq_desc,
1302                 completed_index, VNIC_RQ_RETURN_DESC,
1303                 enic_rq_indicate_buf, opaque);
1304
1305         return 0;
1306 }
1307
1308 static int enic_poll(struct napi_struct *napi, int budget)
1309 {
1310         struct net_device *netdev = napi->dev;
1311         struct enic *enic = netdev_priv(netdev);
1312         unsigned int cq_rq = enic_cq_rq(enic, 0);
1313         unsigned int cq_wq = enic_cq_wq(enic, 0);
1314         unsigned int intr = enic_legacy_io_intr();
1315         unsigned int rq_work_to_do = budget;
1316         unsigned int wq_work_to_do = -1; /* no limit */
1317         unsigned int  work_done, rq_work_done, wq_work_done;
1318         int err;
1319
1320         /* Service RQ (first) and WQ
1321          */
1322
1323         rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
1324                 rq_work_to_do, enic_rq_service, NULL);
1325
1326         wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
1327                 wq_work_to_do, enic_wq_service, NULL);
1328
1329         /* Accumulate intr event credits for this polling
1330          * cycle.  An intr event is the completion of a
1331          * a WQ or RQ packet.
1332          */
1333
1334         work_done = rq_work_done + wq_work_done;
1335
1336         if (work_done > 0)
1337                 vnic_intr_return_credits(&enic->intr[intr],
1338                         work_done,
1339                         0 /* don't unmask intr */,
1340                         0 /* don't reset intr timer */);
1341
1342         err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1343
1344         /* Buffer allocation failed. Stay in polling
1345          * mode so we can try to fill the ring again.
1346          */
1347
1348         if (err)
1349                 rq_work_done = rq_work_to_do;
1350
1351         if (rq_work_done < rq_work_to_do) {
1352
1353                 /* Some work done, but not enough to stay in polling,
1354                  * exit polling
1355                  */
1356
1357                 napi_complete(napi);
1358                 vnic_intr_unmask(&enic->intr[intr]);
1359         }
1360
1361         return rq_work_done;
1362 }
1363
1364 static int enic_poll_msix(struct napi_struct *napi, int budget)
1365 {
1366         struct net_device *netdev = napi->dev;
1367         struct enic *enic = netdev_priv(netdev);
1368         unsigned int rq = (napi - &enic->napi[0]);
1369         unsigned int cq = enic_cq_rq(enic, rq);
1370         unsigned int intr = enic_msix_rq_intr(enic, rq);
1371         unsigned int work_to_do = budget;
1372         unsigned int work_done;
1373         int err;
1374
1375         /* Service RQ
1376          */
1377
1378         work_done = vnic_cq_service(&enic->cq[cq],
1379                 work_to_do, enic_rq_service, NULL);
1380
1381         /* Return intr event credits for this polling
1382          * cycle.  An intr event is the completion of a
1383          * RQ packet.
1384          */
1385
1386         if (work_done > 0)
1387                 vnic_intr_return_credits(&enic->intr[intr],
1388                         work_done,
1389                         0 /* don't unmask intr */,
1390                         0 /* don't reset intr timer */);
1391
1392         err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
1393
1394         /* Buffer allocation failed. Stay in polling mode
1395          * so we can try to fill the ring again.
1396          */
1397
1398         if (err)
1399                 work_done = work_to_do;
1400
1401         if (work_done < work_to_do) {
1402
1403                 /* Some work done, but not enough to stay in polling,
1404                  * exit polling
1405                  */
1406
1407                 napi_complete(napi);
1408                 vnic_intr_unmask(&enic->intr[intr]);
1409         }
1410
1411         return work_done;
1412 }
1413
1414 static void enic_notify_timer(unsigned long data)
1415 {
1416         struct enic *enic = (struct enic *)data;
1417
1418         enic_notify_check(enic);
1419
1420         mod_timer(&enic->notify_timer,
1421                 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1422 }
1423
1424 static void enic_free_intr(struct enic *enic)
1425 {
1426         struct net_device *netdev = enic->netdev;
1427         unsigned int i;
1428
1429         switch (vnic_dev_get_intr_mode(enic->vdev)) {
1430         case VNIC_DEV_INTR_MODE_INTX:
1431                 free_irq(enic->pdev->irq, netdev);
1432                 break;
1433         case VNIC_DEV_INTR_MODE_MSI:
1434                 free_irq(enic->pdev->irq, enic);
1435                 break;
1436         case VNIC_DEV_INTR_MODE_MSIX:
1437                 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1438                         if (enic->msix[i].requested)
1439                                 free_irq(enic->msix_entry[i].vector,
1440                                         enic->msix[i].devid);
1441                 break;
1442         default:
1443                 break;
1444         }
1445 }
1446
1447 static int enic_request_intr(struct enic *enic)
1448 {
1449         struct net_device *netdev = enic->netdev;
1450         unsigned int i, intr;
1451         int err = 0;
1452
1453         switch (vnic_dev_get_intr_mode(enic->vdev)) {
1454
1455         case VNIC_DEV_INTR_MODE_INTX:
1456
1457                 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1458                         IRQF_SHARED, netdev->name, netdev);
1459                 break;
1460
1461         case VNIC_DEV_INTR_MODE_MSI:
1462
1463                 err = request_irq(enic->pdev->irq, enic_isr_msi,
1464                         0, netdev->name, enic);
1465                 break;
1466
1467         case VNIC_DEV_INTR_MODE_MSIX:
1468
1469                 for (i = 0; i < enic->rq_count; i++) {
1470                         intr = enic_msix_rq_intr(enic, i);
1471                         sprintf(enic->msix[intr].devname,
1472                                 "%.11s-rx-%d", netdev->name, i);
1473                         enic->msix[intr].isr = enic_isr_msix_rq;
1474                         enic->msix[intr].devid = &enic->napi[i];
1475                 }
1476
1477                 for (i = 0; i < enic->wq_count; i++) {
1478                         intr = enic_msix_wq_intr(enic, i);
1479                         sprintf(enic->msix[intr].devname,
1480                                 "%.11s-tx-%d", netdev->name, i);
1481                         enic->msix[intr].isr = enic_isr_msix_wq;
1482                         enic->msix[intr].devid = enic;
1483                 }
1484
1485                 intr = enic_msix_err_intr(enic);
1486                 sprintf(enic->msix[intr].devname,
1487                         "%.11s-err", netdev->name);
1488                 enic->msix[intr].isr = enic_isr_msix_err;
1489                 enic->msix[intr].devid = enic;
1490
1491                 intr = enic_msix_notify_intr(enic);
1492                 sprintf(enic->msix[intr].devname,
1493                         "%.11s-notify", netdev->name);
1494                 enic->msix[intr].isr = enic_isr_msix_notify;
1495                 enic->msix[intr].devid = enic;
1496
1497                 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1498                         enic->msix[i].requested = 0;
1499
1500                 for (i = 0; i < enic->intr_count; i++) {
1501                         err = request_irq(enic->msix_entry[i].vector,
1502                                 enic->msix[i].isr, 0,
1503                                 enic->msix[i].devname,
1504                                 enic->msix[i].devid);
1505                         if (err) {
1506                                 enic_free_intr(enic);
1507                                 break;
1508                         }
1509                         enic->msix[i].requested = 1;
1510                 }
1511
1512                 break;
1513
1514         default:
1515                 break;
1516         }
1517
1518         return err;
1519 }
1520
1521 static void enic_synchronize_irqs(struct enic *enic)
1522 {
1523         unsigned int i;
1524
1525         switch (vnic_dev_get_intr_mode(enic->vdev)) {
1526         case VNIC_DEV_INTR_MODE_INTX:
1527         case VNIC_DEV_INTR_MODE_MSI:
1528                 synchronize_irq(enic->pdev->irq);
1529                 break;
1530         case VNIC_DEV_INTR_MODE_MSIX:
1531                 for (i = 0; i < enic->intr_count; i++)
1532                         synchronize_irq(enic->msix_entry[i].vector);
1533                 break;
1534         default:
1535                 break;
1536         }
1537 }
1538
1539 static int enic_dev_notify_set(struct enic *enic)
1540 {
1541         int err;
1542
1543         spin_lock(&enic->devcmd_lock);
1544         switch (vnic_dev_get_intr_mode(enic->vdev)) {
1545         case VNIC_DEV_INTR_MODE_INTX:
1546                 err = vnic_dev_notify_set(enic->vdev,
1547                         enic_legacy_notify_intr());
1548                 break;
1549         case VNIC_DEV_INTR_MODE_MSIX:
1550                 err = vnic_dev_notify_set(enic->vdev,
1551                         enic_msix_notify_intr(enic));
1552                 break;
1553         default:
1554                 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1555                 break;
1556         }
1557         spin_unlock(&enic->devcmd_lock);
1558
1559         return err;
1560 }
1561
1562 static void enic_notify_timer_start(struct enic *enic)
1563 {
1564         switch (vnic_dev_get_intr_mode(enic->vdev)) {
1565         case VNIC_DEV_INTR_MODE_MSI:
1566                 mod_timer(&enic->notify_timer, jiffies);
1567                 break;
1568         default:
1569                 /* Using intr for notification for INTx/MSI-X */
1570                 break;
1571         }
1572 }
1573
1574 /* rtnl lock is held, process context */
1575 static int enic_open(struct net_device *netdev)
1576 {
1577         struct enic *enic = netdev_priv(netdev);
1578         unsigned int i;
1579         int err;
1580
1581         err = enic_request_intr(enic);
1582         if (err) {
1583                 netdev_err(netdev, "Unable to request irq.\n");
1584                 return err;
1585         }
1586
1587         err = enic_dev_notify_set(enic);
1588         if (err) {
1589                 netdev_err(netdev,
1590                         "Failed to alloc notify buffer, aborting.\n");
1591                 goto err_out_free_intr;
1592         }
1593
1594         for (i = 0; i < enic->rq_count; i++) {
1595                 vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
1596                 /* Need at least one buffer on ring to get going */
1597                 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
1598                         netdev_err(netdev, "Unable to alloc receive buffers\n");
1599                         err = -ENOMEM;
1600                         goto err_out_notify_unset;
1601                 }
1602         }
1603
1604         for (i = 0; i < enic->wq_count; i++)
1605                 vnic_wq_enable(&enic->wq[i]);
1606         for (i = 0; i < enic->rq_count; i++)
1607                 vnic_rq_enable(&enic->rq[i]);
1608
1609         if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1610                 enic_dev_add_addr(enic, enic->pp.mac_addr);
1611         else
1612                 enic_dev_add_station_addr(enic);
1613         enic_set_rx_mode(netdev);
1614
1615         netif_wake_queue(netdev);
1616
1617         for (i = 0; i < enic->rq_count; i++)
1618                 napi_enable(&enic->napi[i]);
1619
1620         enic_dev_enable(enic);
1621
1622         for (i = 0; i < enic->intr_count; i++)
1623                 vnic_intr_unmask(&enic->intr[i]);
1624
1625         enic_notify_timer_start(enic);
1626
1627         return 0;
1628
1629 err_out_notify_unset:
1630         enic_dev_notify_unset(enic);
1631 err_out_free_intr:
1632         enic_free_intr(enic);
1633
1634         return err;
1635 }
1636
1637 /* rtnl lock is held, process context */
1638 static int enic_stop(struct net_device *netdev)
1639 {
1640         struct enic *enic = netdev_priv(netdev);
1641         unsigned int i;
1642         int err;
1643
1644         for (i = 0; i < enic->intr_count; i++) {
1645                 vnic_intr_mask(&enic->intr[i]);
1646                 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1647         }
1648
1649         enic_synchronize_irqs(enic);
1650
1651         del_timer_sync(&enic->notify_timer);
1652
1653         enic_dev_disable(enic);
1654
1655         for (i = 0; i < enic->rq_count; i++)
1656                 napi_disable(&enic->napi[i]);
1657
1658         netif_carrier_off(netdev);
1659         netif_tx_disable(netdev);
1660         if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1661                 enic_dev_del_addr(enic, enic->pp.mac_addr);
1662         else
1663                 enic_dev_del_station_addr(enic);
1664
1665         for (i = 0; i < enic->wq_count; i++) {
1666                 err = vnic_wq_disable(&enic->wq[i]);
1667                 if (err)
1668                         return err;
1669         }
1670         for (i = 0; i < enic->rq_count; i++) {
1671                 err = vnic_rq_disable(&enic->rq[i]);
1672                 if (err)
1673                         return err;
1674         }
1675
1676         enic_dev_notify_unset(enic);
1677         enic_free_intr(enic);
1678
1679         for (i = 0; i < enic->wq_count; i++)
1680                 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1681         for (i = 0; i < enic->rq_count; i++)
1682                 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1683         for (i = 0; i < enic->cq_count; i++)
1684                 vnic_cq_clean(&enic->cq[i]);
1685         for (i = 0; i < enic->intr_count; i++)
1686                 vnic_intr_clean(&enic->intr[i]);
1687
1688         return 0;
1689 }
1690
1691 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1692 {
1693         struct enic *enic = netdev_priv(netdev);
1694         int running = netif_running(netdev);
1695
1696         if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1697                 return -EINVAL;
1698
1699         if (enic_is_dynamic(enic))
1700                 return -EOPNOTSUPP;
1701
1702         if (running)
1703                 enic_stop(netdev);
1704
1705         netdev->mtu = new_mtu;
1706
1707         if (netdev->mtu > enic->port_mtu)
1708                 netdev_warn(netdev,
1709                         "interface MTU (%d) set higher than port MTU (%d)\n",
1710                         netdev->mtu, enic->port_mtu);
1711
1712         if (running)
1713                 enic_open(netdev);
1714
1715         return 0;
1716 }
1717
1718 static void enic_change_mtu_work(struct work_struct *work)
1719 {
1720         struct enic *enic = container_of(work, struct enic, change_mtu_work);
1721         struct net_device *netdev = enic->netdev;
1722         int new_mtu = vnic_dev_mtu(enic->vdev);
1723         int err;
1724         unsigned int i;
1725
1726         new_mtu = max_t(int, ENIC_MIN_MTU, min_t(int, ENIC_MAX_MTU, new_mtu));
1727
1728         rtnl_lock();
1729
1730         /* Stop RQ */
1731         del_timer_sync(&enic->notify_timer);
1732
1733         for (i = 0; i < enic->rq_count; i++)
1734                 napi_disable(&enic->napi[i]);
1735
1736         vnic_intr_mask(&enic->intr[0]);
1737         enic_synchronize_irqs(enic);
1738         err = vnic_rq_disable(&enic->rq[0]);
1739         if (err) {
1740                 netdev_err(netdev, "Unable to disable RQ.\n");
1741                 return;
1742         }
1743         vnic_rq_clean(&enic->rq[0], enic_free_rq_buf);
1744         vnic_cq_clean(&enic->cq[0]);
1745         vnic_intr_clean(&enic->intr[0]);
1746
1747         /* Fill RQ with new_mtu-sized buffers */
1748         netdev->mtu = new_mtu;
1749         vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1750         /* Need at least one buffer on ring to get going */
1751         if (vnic_rq_desc_used(&enic->rq[0]) == 0) {
1752                 netdev_err(netdev, "Unable to alloc receive buffers.\n");
1753                 return;
1754         }
1755
1756         /* Start RQ */
1757         vnic_rq_enable(&enic->rq[0]);
1758         napi_enable(&enic->napi[0]);
1759         vnic_intr_unmask(&enic->intr[0]);
1760         enic_notify_timer_start(enic);
1761
1762         rtnl_unlock();
1763
1764         netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
1765 }
1766
1767 #ifdef CONFIG_NET_POLL_CONTROLLER
1768 static void enic_poll_controller(struct net_device *netdev)
1769 {
1770         struct enic *enic = netdev_priv(netdev);
1771         struct vnic_dev *vdev = enic->vdev;
1772         unsigned int i, intr;
1773
1774         switch (vnic_dev_get_intr_mode(vdev)) {
1775         case VNIC_DEV_INTR_MODE_MSIX:
1776                 for (i = 0; i < enic->rq_count; i++) {
1777                         intr = enic_msix_rq_intr(enic, i);
1778                         enic_isr_msix_rq(enic->msix_entry[intr].vector,
1779                                 &enic->napi[i]);
1780                 }
1781                 intr = enic_msix_wq_intr(enic, i);
1782                 enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
1783                 break;
1784         case VNIC_DEV_INTR_MODE_MSI:
1785                 enic_isr_msi(enic->pdev->irq, enic);
1786                 break;
1787         case VNIC_DEV_INTR_MODE_INTX:
1788                 enic_isr_legacy(enic->pdev->irq, netdev);
1789                 break;
1790         default:
1791                 break;
1792         }
1793 }
1794 #endif
1795
1796 static int enic_dev_wait(struct vnic_dev *vdev,
1797         int (*start)(struct vnic_dev *, int),
1798         int (*finished)(struct vnic_dev *, int *),
1799         int arg)
1800 {
1801         unsigned long time;
1802         int done;
1803         int err;
1804
1805         BUG_ON(in_interrupt());
1806
1807         err = start(vdev, arg);
1808         if (err)
1809                 return err;
1810
1811         /* Wait for func to complete...2 seconds max
1812          */
1813
1814         time = jiffies + (HZ * 2);
1815         do {
1816
1817                 err = finished(vdev, &done);
1818                 if (err)
1819                         return err;
1820
1821                 if (done)
1822                         return 0;
1823
1824                 schedule_timeout_uninterruptible(HZ / 10);
1825
1826         } while (time_after(time, jiffies));
1827
1828         return -ETIMEDOUT;
1829 }
1830
1831 static int enic_dev_open(struct enic *enic)
1832 {
1833         int err;
1834
1835         err = enic_dev_wait(enic->vdev, vnic_dev_open,
1836                 vnic_dev_open_done, 0);
1837         if (err)
1838                 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1839                         err);
1840
1841         return err;
1842 }
1843
1844 static int enic_dev_hang_reset(struct enic *enic)
1845 {
1846         int err;
1847
1848         err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1849                 vnic_dev_hang_reset_done, 0);
1850         if (err)
1851                 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1852                         err);
1853
1854         return err;
1855 }
1856
1857 static int enic_set_rsskey(struct enic *enic)
1858 {
1859         dma_addr_t rss_key_buf_pa;
1860         union vnic_rss_key *rss_key_buf_va = NULL;
1861         union vnic_rss_key rss_key = {
1862                 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
1863                 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
1864                 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
1865                 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
1866         };
1867         int err;
1868
1869         rss_key_buf_va = pci_alloc_consistent(enic->pdev,
1870                 sizeof(union vnic_rss_key), &rss_key_buf_pa);
1871         if (!rss_key_buf_va)
1872                 return -ENOMEM;
1873
1874         memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
1875
1876         spin_lock(&enic->devcmd_lock);
1877         err = enic_set_rss_key(enic,
1878                 rss_key_buf_pa,
1879                 sizeof(union vnic_rss_key));
1880         spin_unlock(&enic->devcmd_lock);
1881
1882         pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1883                 rss_key_buf_va, rss_key_buf_pa);
1884
1885         return err;
1886 }
1887
1888 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1889 {
1890         dma_addr_t rss_cpu_buf_pa;
1891         union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1892         unsigned int i;
1893         int err;
1894
1895         rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1896                 sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1897         if (!rss_cpu_buf_va)
1898                 return -ENOMEM;
1899
1900         for (i = 0; i < (1 << rss_hash_bits); i++)
1901                 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1902
1903         spin_lock(&enic->devcmd_lock);
1904         err = enic_set_rss_cpu(enic,
1905                 rss_cpu_buf_pa,
1906                 sizeof(union vnic_rss_cpu));
1907         spin_unlock(&enic->devcmd_lock);
1908
1909         pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1910                 rss_cpu_buf_va, rss_cpu_buf_pa);
1911
1912         return err;
1913 }
1914
1915 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1916         u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
1917 {
1918         const u8 tso_ipid_split_en = 0;
1919         const u8 ig_vlan_strip_en = 1;
1920         int err;
1921
1922         /* Enable VLAN tag stripping.
1923         */
1924
1925         spin_lock(&enic->devcmd_lock);
1926         err = enic_set_nic_cfg(enic,
1927                 rss_default_cpu, rss_hash_type,
1928                 rss_hash_bits, rss_base_cpu,
1929                 rss_enable, tso_ipid_split_en,
1930                 ig_vlan_strip_en);
1931         spin_unlock(&enic->devcmd_lock);
1932
1933         return err;
1934 }
1935
1936 static int enic_set_rss_nic_cfg(struct enic *enic)
1937 {
1938         struct device *dev = enic_get_dev(enic);
1939         const u8 rss_default_cpu = 0;
1940         const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1941                 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1942                 NIC_CFG_RSS_HASH_TYPE_IPV6 |
1943                 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1944         const u8 rss_hash_bits = 7;
1945         const u8 rss_base_cpu = 0;
1946         u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1947
1948         if (rss_enable) {
1949                 if (!enic_set_rsskey(enic)) {
1950                         if (enic_set_rsscpu(enic, rss_hash_bits)) {
1951                                 rss_enable = 0;
1952                                 dev_warn(dev, "RSS disabled, "
1953                                         "Failed to set RSS cpu indirection table.");
1954                         }
1955                 } else {
1956                         rss_enable = 0;
1957                         dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
1958                 }
1959         }
1960
1961         return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1962                 rss_hash_bits, rss_base_cpu, rss_enable);
1963 }
1964
1965 static void enic_reset(struct work_struct *work)
1966 {
1967         struct enic *enic = container_of(work, struct enic, reset);
1968
1969         if (!netif_running(enic->netdev))
1970                 return;
1971
1972         rtnl_lock();
1973
1974         enic_dev_hang_notify(enic);
1975         enic_stop(enic->netdev);
1976         enic_dev_hang_reset(enic);
1977         enic_reset_addr_lists(enic);
1978         enic_init_vnic_resources(enic);
1979         enic_set_rss_nic_cfg(enic);
1980         enic_dev_set_ig_vlan_rewrite_mode(enic);
1981         enic_open(enic->netdev);
1982
1983         rtnl_unlock();
1984 }
1985
1986 static int enic_set_intr_mode(struct enic *enic)
1987 {
1988         unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
1989         unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
1990         unsigned int i;
1991
1992         /* Set interrupt mode (INTx, MSI, MSI-X) depending
1993          * on system capabilities.
1994          *
1995          * Try MSI-X first
1996          *
1997          * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
1998          * (the second to last INTR is used for WQ/RQ errors)
1999          * (the last INTR is used for notifications)
2000          */
2001
2002         BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2003         for (i = 0; i < n + m + 2; i++)
2004                 enic->msix_entry[i].entry = i;
2005
2006         /* Use multiple RQs if RSS is enabled
2007          */
2008
2009         if (ENIC_SETTING(enic, RSS) &&
2010             enic->config.intr_mode < 1 &&
2011             enic->rq_count >= n &&
2012             enic->wq_count >= m &&
2013             enic->cq_count >= n + m &&
2014             enic->intr_count >= n + m + 2) {
2015
2016                 if (!pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
2017
2018                         enic->rq_count = n;
2019                         enic->wq_count = m;
2020                         enic->cq_count = n + m;
2021                         enic->intr_count = n + m + 2;
2022
2023                         vnic_dev_set_intr_mode(enic->vdev,
2024                                 VNIC_DEV_INTR_MODE_MSIX);
2025
2026                         return 0;
2027                 }
2028         }
2029
2030         if (enic->config.intr_mode < 1 &&
2031             enic->rq_count >= 1 &&
2032             enic->wq_count >= m &&
2033             enic->cq_count >= 1 + m &&
2034             enic->intr_count >= 1 + m + 2) {
2035                 if (!pci_enable_msix(enic->pdev, enic->msix_entry, 1 + m + 2)) {
2036
2037                         enic->rq_count = 1;
2038                         enic->wq_count = m;
2039                         enic->cq_count = 1 + m;
2040                         enic->intr_count = 1 + m + 2;
2041
2042                         vnic_dev_set_intr_mode(enic->vdev,
2043                                 VNIC_DEV_INTR_MODE_MSIX);
2044
2045                         return 0;
2046                 }
2047         }
2048
2049         /* Next try MSI
2050          *
2051          * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2052          */
2053
2054         if (enic->config.intr_mode < 2 &&
2055             enic->rq_count >= 1 &&
2056             enic->wq_count >= 1 &&
2057             enic->cq_count >= 2 &&
2058             enic->intr_count >= 1 &&
2059             !pci_enable_msi(enic->pdev)) {
2060
2061                 enic->rq_count = 1;
2062                 enic->wq_count = 1;
2063                 enic->cq_count = 2;
2064                 enic->intr_count = 1;
2065
2066                 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2067
2068                 return 0;
2069         }
2070
2071         /* Next try INTx
2072          *
2073          * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2074          * (the first INTR is used for WQ/RQ)
2075          * (the second INTR is used for WQ/RQ errors)
2076          * (the last INTR is used for notifications)
2077          */
2078
2079         if (enic->config.intr_mode < 3 &&
2080             enic->rq_count >= 1 &&
2081             enic->wq_count >= 1 &&
2082             enic->cq_count >= 2 &&
2083             enic->intr_count >= 3) {
2084
2085                 enic->rq_count = 1;
2086                 enic->wq_count = 1;
2087                 enic->cq_count = 2;
2088                 enic->intr_count = 3;
2089
2090                 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2091
2092                 return 0;
2093         }
2094
2095         vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2096
2097         return -EINVAL;
2098 }
2099
2100 static void enic_clear_intr_mode(struct enic *enic)
2101 {
2102         switch (vnic_dev_get_intr_mode(enic->vdev)) {
2103         case VNIC_DEV_INTR_MODE_MSIX:
2104                 pci_disable_msix(enic->pdev);
2105                 break;
2106         case VNIC_DEV_INTR_MODE_MSI:
2107                 pci_disable_msi(enic->pdev);
2108                 break;
2109         default:
2110                 break;
2111         }
2112
2113         vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2114 }
2115
2116 static const struct net_device_ops enic_netdev_dynamic_ops = {
2117         .ndo_open               = enic_open,
2118         .ndo_stop               = enic_stop,
2119         .ndo_start_xmit         = enic_hard_start_xmit,
2120         .ndo_get_stats          = enic_get_stats,
2121         .ndo_validate_addr      = eth_validate_addr,
2122         .ndo_set_rx_mode        = enic_set_rx_mode,
2123         .ndo_set_multicast_list = enic_set_rx_mode,
2124         .ndo_set_mac_address    = enic_set_mac_address_dynamic,
2125         .ndo_change_mtu         = enic_change_mtu,
2126         .ndo_vlan_rx_register   = enic_vlan_rx_register,
2127         .ndo_vlan_rx_add_vid    = enic_vlan_rx_add_vid,
2128         .ndo_vlan_rx_kill_vid   = enic_vlan_rx_kill_vid,
2129         .ndo_tx_timeout         = enic_tx_timeout,
2130         .ndo_set_vf_port        = enic_set_vf_port,
2131         .ndo_get_vf_port        = enic_get_vf_port,
2132         .ndo_set_vf_mac         = enic_set_vf_mac,
2133 #ifdef CONFIG_NET_POLL_CONTROLLER
2134         .ndo_poll_controller    = enic_poll_controller,
2135 #endif
2136 };
2137
2138 static const struct net_device_ops enic_netdev_ops = {
2139         .ndo_open               = enic_open,
2140         .ndo_stop               = enic_stop,
2141         .ndo_start_xmit         = enic_hard_start_xmit,
2142         .ndo_get_stats          = enic_get_stats,
2143         .ndo_validate_addr      = eth_validate_addr,
2144         .ndo_set_mac_address    = enic_set_mac_address,
2145         .ndo_set_rx_mode        = enic_set_rx_mode,
2146         .ndo_set_multicast_list = enic_set_rx_mode,
2147         .ndo_change_mtu         = enic_change_mtu,
2148         .ndo_vlan_rx_register   = enic_vlan_rx_register,
2149         .ndo_vlan_rx_add_vid    = enic_vlan_rx_add_vid,
2150         .ndo_vlan_rx_kill_vid   = enic_vlan_rx_kill_vid,
2151         .ndo_tx_timeout         = enic_tx_timeout,
2152 #ifdef CONFIG_NET_POLL_CONTROLLER
2153         .ndo_poll_controller    = enic_poll_controller,
2154 #endif
2155 };
2156
2157 static void enic_dev_deinit(struct enic *enic)
2158 {
2159         unsigned int i;
2160
2161         for (i = 0; i < enic->rq_count; i++)
2162                 netif_napi_del(&enic->napi[i]);
2163
2164         enic_free_vnic_resources(enic);
2165         enic_clear_intr_mode(enic);
2166 }
2167
2168 static int enic_dev_init(struct enic *enic)
2169 {
2170         struct device *dev = enic_get_dev(enic);
2171         struct net_device *netdev = enic->netdev;
2172         unsigned int i;
2173         int err;
2174
2175         /* Get vNIC configuration
2176          */
2177
2178         err = enic_get_vnic_config(enic);
2179         if (err) {
2180                 dev_err(dev, "Get vNIC configuration failed, aborting\n");
2181                 return err;
2182         }
2183
2184         /* Get available resource counts
2185          */
2186
2187         enic_get_res_counts(enic);
2188
2189         /* Set interrupt mode based on resource counts and system
2190          * capabilities
2191          */
2192
2193         err = enic_set_intr_mode(enic);
2194         if (err) {
2195                 dev_err(dev, "Failed to set intr mode based on resource "
2196                         "counts and system capabilities, aborting\n");
2197                 return err;
2198         }
2199
2200         /* Allocate and configure vNIC resources
2201          */
2202
2203         err = enic_alloc_vnic_resources(enic);
2204         if (err) {
2205                 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
2206                 goto err_out_free_vnic_resources;
2207         }
2208
2209         enic_init_vnic_resources(enic);
2210
2211         err = enic_set_rss_nic_cfg(enic);
2212         if (err) {
2213                 dev_err(dev, "Failed to config nic, aborting\n");
2214                 goto err_out_free_vnic_resources;
2215         }
2216
2217         switch (vnic_dev_get_intr_mode(enic->vdev)) {
2218         default:
2219                 netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
2220                 break;
2221         case VNIC_DEV_INTR_MODE_MSIX:
2222                 for (i = 0; i < enic->rq_count; i++)
2223                         netif_napi_add(netdev, &enic->napi[i],
2224                                 enic_poll_msix, 64);
2225                 break;
2226         }
2227
2228         return 0;
2229
2230 err_out_free_vnic_resources:
2231         enic_clear_intr_mode(enic);
2232         enic_free_vnic_resources(enic);
2233
2234         return err;
2235 }
2236
2237 static void enic_iounmap(struct enic *enic)
2238 {
2239         unsigned int i;
2240
2241         for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2242                 if (enic->bar[i].vaddr)
2243                         iounmap(enic->bar[i].vaddr);
2244 }
2245
2246 static int __devinit enic_probe(struct pci_dev *pdev,
2247         const struct pci_device_id *ent)
2248 {
2249         struct device *dev = &pdev->dev;
2250         struct net_device *netdev;
2251         struct enic *enic;
2252         int using_dac = 0;
2253         unsigned int i;
2254         int err;
2255
2256         /* Allocate net device structure and initialize.  Private
2257          * instance data is initialized to zero.
2258          */
2259
2260         netdev = alloc_etherdev(sizeof(struct enic));
2261         if (!netdev) {
2262                 pr_err("Etherdev alloc failed, aborting\n");
2263                 return -ENOMEM;
2264         }
2265
2266         pci_set_drvdata(pdev, netdev);
2267
2268         SET_NETDEV_DEV(netdev, &pdev->dev);
2269
2270         enic = netdev_priv(netdev);
2271         enic->netdev = netdev;
2272         enic->pdev = pdev;
2273
2274         /* Setup PCI resources
2275          */
2276
2277         err = pci_enable_device_mem(pdev);
2278         if (err) {
2279                 dev_err(dev, "Cannot enable PCI device, aborting\n");
2280                 goto err_out_free_netdev;
2281         }
2282
2283         err = pci_request_regions(pdev, DRV_NAME);
2284         if (err) {
2285                 dev_err(dev, "Cannot request PCI regions, aborting\n");
2286                 goto err_out_disable_device;
2287         }
2288
2289         pci_set_master(pdev);
2290
2291         /* Query PCI controller on system for DMA addressing
2292          * limitation for the device.  Try 40-bit first, and
2293          * fail to 32-bit.
2294          */
2295
2296         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
2297         if (err) {
2298                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2299                 if (err) {
2300                         dev_err(dev, "No usable DMA configuration, aborting\n");
2301                         goto err_out_release_regions;
2302                 }
2303                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2304                 if (err) {
2305                         dev_err(dev, "Unable to obtain %u-bit DMA "
2306                                 "for consistent allocations, aborting\n", 32);
2307                         goto err_out_release_regions;
2308                 }
2309         } else {
2310                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
2311                 if (err) {
2312                         dev_err(dev, "Unable to obtain %u-bit DMA "
2313                                 "for consistent allocations, aborting\n", 40);
2314                         goto err_out_release_regions;
2315                 }
2316                 using_dac = 1;
2317         }
2318
2319         /* Map vNIC resources from BAR0-5
2320          */
2321
2322         for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2323                 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2324                         continue;
2325                 enic->bar[i].len = pci_resource_len(pdev, i);
2326                 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2327                 if (!enic->bar[i].vaddr) {
2328                         dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
2329                         err = -ENODEV;
2330                         goto err_out_iounmap;
2331                 }
2332                 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
2333         }
2334
2335         /* Register vNIC device
2336          */
2337
2338         enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2339                 ARRAY_SIZE(enic->bar));
2340         if (!enic->vdev) {
2341                 dev_err(dev, "vNIC registration failed, aborting\n");
2342                 err = -ENODEV;
2343                 goto err_out_iounmap;
2344         }
2345
2346         /* Issue device open to get device in known state
2347          */
2348
2349         err = enic_dev_open(enic);
2350         if (err) {
2351                 dev_err(dev, "vNIC dev open failed, aborting\n");
2352                 goto err_out_vnic_unregister;
2353         }
2354
2355         /* Setup devcmd lock
2356          */
2357
2358         spin_lock_init(&enic->devcmd_lock);
2359
2360         /*
2361          * Set ingress vlan rewrite mode before vnic initialization
2362          */
2363
2364         err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2365         if (err) {
2366                 dev_err(dev,
2367                         "Failed to set ingress vlan rewrite mode, aborting.\n");
2368                 goto err_out_dev_close;
2369         }
2370
2371         /* Issue device init to initialize the vnic-to-switch link.
2372          * We'll start with carrier off and wait for link UP
2373          * notification later to turn on carrier.  We don't need
2374          * to wait here for the vnic-to-switch link initialization
2375          * to complete; link UP notification is the indication that
2376          * the process is complete.
2377          */
2378
2379         netif_carrier_off(netdev);
2380
2381         /* Do not call dev_init for a dynamic vnic.
2382          * For a dynamic vnic, init_prov_info will be
2383          * called later by an upper layer.
2384          */
2385
2386         if (!enic_is_dynamic(enic)) {
2387                 err = vnic_dev_init(enic->vdev, 0);
2388                 if (err) {
2389                         dev_err(dev, "vNIC dev init failed, aborting\n");
2390                         goto err_out_dev_close;
2391                 }
2392         }
2393
2394         err = enic_dev_init(enic);
2395         if (err) {
2396                 dev_err(dev, "Device initialization failed, aborting\n");
2397                 goto err_out_dev_close;
2398         }
2399
2400         /* Setup notification timer, HW reset task, and wq locks
2401          */
2402
2403         init_timer(&enic->notify_timer);
2404         enic->notify_timer.function = enic_notify_timer;
2405         enic->notify_timer.data = (unsigned long)enic;
2406
2407         INIT_WORK(&enic->reset, enic_reset);
2408         INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
2409
2410         for (i = 0; i < enic->wq_count; i++)
2411                 spin_lock_init(&enic->wq_lock[i]);
2412
2413         /* Register net device
2414          */
2415
2416         enic->port_mtu = enic->config.mtu;
2417         (void)enic_change_mtu(netdev, enic->port_mtu);
2418
2419         err = enic_set_mac_addr(netdev, enic->mac_addr);
2420         if (err) {
2421                 dev_err(dev, "Invalid MAC address, aborting\n");
2422                 goto err_out_dev_deinit;
2423         }
2424
2425         enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2426         enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2427
2428         if (enic_is_dynamic(enic))
2429                 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2430         else
2431                 netdev->netdev_ops = &enic_netdev_ops;
2432
2433         netdev->watchdog_timeo = 2 * HZ;
2434         netdev->ethtool_ops = &enic_ethtool_ops;
2435
2436         netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
2437         if (ENIC_SETTING(enic, LOOP)) {
2438                 netdev->features &= ~NETIF_F_HW_VLAN_TX;
2439                 enic->loop_enable = 1;
2440                 enic->loop_tag = enic->config.loop_tag;
2441                 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2442         }
2443         if (ENIC_SETTING(enic, TXCSUM))
2444                 netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2445         if (ENIC_SETTING(enic, TSO))
2446                 netdev->hw_features |= NETIF_F_TSO |
2447                         NETIF_F_TSO6 | NETIF_F_TSO_ECN;
2448         if (ENIC_SETTING(enic, RXCSUM))
2449                 netdev->hw_features |= NETIF_F_RXCSUM;
2450
2451         netdev->features |= netdev->hw_features;
2452
2453         if (using_dac)
2454                 netdev->features |= NETIF_F_HIGHDMA;
2455
2456         err = register_netdev(netdev);
2457         if (err) {
2458                 dev_err(dev, "Cannot register net device, aborting\n");
2459                 goto err_out_dev_deinit;
2460         }
2461
2462         return 0;
2463
2464 err_out_dev_deinit:
2465         enic_dev_deinit(enic);
2466 err_out_dev_close:
2467         vnic_dev_close(enic->vdev);
2468 err_out_vnic_unregister:
2469         vnic_dev_unregister(enic->vdev);
2470 err_out_iounmap:
2471         enic_iounmap(enic);
2472 err_out_release_regions:
2473         pci_release_regions(pdev);
2474 err_out_disable_device:
2475         pci_disable_device(pdev);
2476 err_out_free_netdev:
2477         pci_set_drvdata(pdev, NULL);
2478         free_netdev(netdev);
2479
2480         return err;
2481 }
2482
2483 static void __devexit enic_remove(struct pci_dev *pdev)
2484 {
2485         struct net_device *netdev = pci_get_drvdata(pdev);
2486
2487         if (netdev) {
2488                 struct enic *enic = netdev_priv(netdev);
2489
2490                 cancel_work_sync(&enic->reset);
2491                 cancel_work_sync(&enic->change_mtu_work);
2492                 unregister_netdev(netdev);
2493                 enic_dev_deinit(enic);
2494                 vnic_dev_close(enic->vdev);
2495                 vnic_dev_unregister(enic->vdev);
2496                 enic_iounmap(enic);
2497                 pci_release_regions(pdev);
2498                 pci_disable_device(pdev);
2499                 pci_set_drvdata(pdev, NULL);
2500                 free_netdev(netdev);
2501         }
2502 }
2503
2504 static struct pci_driver enic_driver = {
2505         .name = DRV_NAME,
2506         .id_table = enic_id_table,
2507         .probe = enic_probe,
2508         .remove = __devexit_p(enic_remove),
2509 };
2510
2511 static int __init enic_init_module(void)
2512 {
2513         pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
2514
2515         return pci_register_driver(&enic_driver);
2516 }
2517
2518 static void __exit enic_cleanup_module(void)
2519 {
2520         pci_unregister_driver(&enic_driver);
2521 }
2522
2523 module_init(enic_init_module);
2524 module_exit(enic_cleanup_module);