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