vmxnet3: Enable GRO support.
[linux-2.6.git] / drivers / net / vmxnet3 / vmxnet3_drv.c
1 /*
2  * Linux driver for VMware's vmxnet3 ethernet NIC.
3  *
4  * Copyright (C) 2008-2009, VMware, Inc. All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; version 2 of the License and no later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13  * NON INFRINGEMENT. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * The full GNU General Public License is included in this distribution in
21  * the file called "COPYING".
22  *
23  * Maintained by: Shreyas Bhatewara <pv-drivers@vmware.com>
24  *
25  */
26
27 #include <net/ip6_checksum.h>
28
29 #include "vmxnet3_int.h"
30
31 char vmxnet3_driver_name[] = "vmxnet3";
32 #define VMXNET3_DRIVER_DESC "VMware vmxnet3 virtual NIC driver"
33
34 /*
35  * PCI Device ID Table
36  * Last entry must be all 0s
37  */
38 static DEFINE_PCI_DEVICE_TABLE(vmxnet3_pciid_table) = {
39         {PCI_VDEVICE(VMWARE, PCI_DEVICE_ID_VMWARE_VMXNET3)},
40         {0}
41 };
42
43 MODULE_DEVICE_TABLE(pci, vmxnet3_pciid_table);
44
45 static atomic_t devices_found;
46
47 #define VMXNET3_MAX_DEVICES 10
48 static int enable_mq = 1;
49 static int irq_share_mode;
50
51 static void
52 vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac);
53
54 /*
55  *    Enable/Disable the given intr
56  */
57 static void
58 vmxnet3_enable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
59 {
60         VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 0);
61 }
62
63
64 static void
65 vmxnet3_disable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
66 {
67         VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 1);
68 }
69
70
71 /*
72  *    Enable/Disable all intrs used by the device
73  */
74 static void
75 vmxnet3_enable_all_intrs(struct vmxnet3_adapter *adapter)
76 {
77         int i;
78
79         for (i = 0; i < adapter->intr.num_intrs; i++)
80                 vmxnet3_enable_intr(adapter, i);
81         adapter->shared->devRead.intrConf.intrCtrl &=
82                                         cpu_to_le32(~VMXNET3_IC_DISABLE_ALL);
83 }
84
85
86 static void
87 vmxnet3_disable_all_intrs(struct vmxnet3_adapter *adapter)
88 {
89         int i;
90
91         adapter->shared->devRead.intrConf.intrCtrl |=
92                                         cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
93         for (i = 0; i < adapter->intr.num_intrs; i++)
94                 vmxnet3_disable_intr(adapter, i);
95 }
96
97
98 static void
99 vmxnet3_ack_events(struct vmxnet3_adapter *adapter, u32 events)
100 {
101         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_ECR, events);
102 }
103
104
105 static bool
106 vmxnet3_tq_stopped(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
107 {
108         return tq->stopped;
109 }
110
111
112 static void
113 vmxnet3_tq_start(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
114 {
115         tq->stopped = false;
116         netif_start_subqueue(adapter->netdev, tq - adapter->tx_queue);
117 }
118
119
120 static void
121 vmxnet3_tq_wake(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
122 {
123         tq->stopped = false;
124         netif_wake_subqueue(adapter->netdev, (tq - adapter->tx_queue));
125 }
126
127
128 static void
129 vmxnet3_tq_stop(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
130 {
131         tq->stopped = true;
132         tq->num_stop++;
133         netif_stop_subqueue(adapter->netdev, (tq - adapter->tx_queue));
134 }
135
136
137 /*
138  * Check the link state. This may start or stop the tx queue.
139  */
140 static void
141 vmxnet3_check_link(struct vmxnet3_adapter *adapter, bool affectTxQueue)
142 {
143         u32 ret;
144         int i;
145         unsigned long flags;
146
147         spin_lock_irqsave(&adapter->cmd_lock, flags);
148         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
149         ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
150         spin_unlock_irqrestore(&adapter->cmd_lock, flags);
151
152         adapter->link_speed = ret >> 16;
153         if (ret & 1) { /* Link is up. */
154                 printk(KERN_INFO "%s: NIC Link is Up %d Mbps\n",
155                        adapter->netdev->name, adapter->link_speed);
156                 if (!netif_carrier_ok(adapter->netdev))
157                         netif_carrier_on(adapter->netdev);
158
159                 if (affectTxQueue) {
160                         for (i = 0; i < adapter->num_tx_queues; i++)
161                                 vmxnet3_tq_start(&adapter->tx_queue[i],
162                                                  adapter);
163                 }
164         } else {
165                 printk(KERN_INFO "%s: NIC Link is Down\n",
166                        adapter->netdev->name);
167                 if (netif_carrier_ok(adapter->netdev))
168                         netif_carrier_off(adapter->netdev);
169
170                 if (affectTxQueue) {
171                         for (i = 0; i < adapter->num_tx_queues; i++)
172                                 vmxnet3_tq_stop(&adapter->tx_queue[i], adapter);
173                 }
174         }
175 }
176
177 static void
178 vmxnet3_process_events(struct vmxnet3_adapter *adapter)
179 {
180         int i;
181         unsigned long flags;
182         u32 events = le32_to_cpu(adapter->shared->ecr);
183         if (!events)
184                 return;
185
186         vmxnet3_ack_events(adapter, events);
187
188         /* Check if link state has changed */
189         if (events & VMXNET3_ECR_LINK)
190                 vmxnet3_check_link(adapter, true);
191
192         /* Check if there is an error on xmit/recv queues */
193         if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
194                 spin_lock_irqsave(&adapter->cmd_lock, flags);
195                 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
196                                        VMXNET3_CMD_GET_QUEUE_STATUS);
197                 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
198
199                 for (i = 0; i < adapter->num_tx_queues; i++)
200                         if (adapter->tqd_start[i].status.stopped)
201                                 dev_err(&adapter->netdev->dev,
202                                         "%s: tq[%d] error 0x%x\n",
203                                         adapter->netdev->name, i, le32_to_cpu(
204                                         adapter->tqd_start[i].status.error));
205                 for (i = 0; i < adapter->num_rx_queues; i++)
206                         if (adapter->rqd_start[i].status.stopped)
207                                 dev_err(&adapter->netdev->dev,
208                                         "%s: rq[%d] error 0x%x\n",
209                                         adapter->netdev->name, i,
210                                         adapter->rqd_start[i].status.error);
211
212                 schedule_work(&adapter->work);
213         }
214 }
215
216 #ifdef __BIG_ENDIAN_BITFIELD
217 /*
218  * The device expects the bitfields in shared structures to be written in
219  * little endian. When CPU is big endian, the following routines are used to
220  * correctly read and write into ABI.
221  * The general technique used here is : double word bitfields are defined in
222  * opposite order for big endian architecture. Then before reading them in
223  * driver the complete double word is translated using le32_to_cpu. Similarly
224  * After the driver writes into bitfields, cpu_to_le32 is used to translate the
225  * double words into required format.
226  * In order to avoid touching bits in shared structure more than once, temporary
227  * descriptors are used. These are passed as srcDesc to following functions.
228  */
229 static void vmxnet3_RxDescToCPU(const struct Vmxnet3_RxDesc *srcDesc,
230                                 struct Vmxnet3_RxDesc *dstDesc)
231 {
232         u32 *src = (u32 *)srcDesc + 2;
233         u32 *dst = (u32 *)dstDesc + 2;
234         dstDesc->addr = le64_to_cpu(srcDesc->addr);
235         *dst = le32_to_cpu(*src);
236         dstDesc->ext1 = le32_to_cpu(srcDesc->ext1);
237 }
238
239 static void vmxnet3_TxDescToLe(const struct Vmxnet3_TxDesc *srcDesc,
240                                struct Vmxnet3_TxDesc *dstDesc)
241 {
242         int i;
243         u32 *src = (u32 *)(srcDesc + 1);
244         u32 *dst = (u32 *)(dstDesc + 1);
245
246         /* Working backwards so that the gen bit is set at the end. */
247         for (i = 2; i > 0; i--) {
248                 src--;
249                 dst--;
250                 *dst = cpu_to_le32(*src);
251         }
252 }
253
254
255 static void vmxnet3_RxCompToCPU(const struct Vmxnet3_RxCompDesc *srcDesc,
256                                 struct Vmxnet3_RxCompDesc *dstDesc)
257 {
258         int i = 0;
259         u32 *src = (u32 *)srcDesc;
260         u32 *dst = (u32 *)dstDesc;
261         for (i = 0; i < sizeof(struct Vmxnet3_RxCompDesc) / sizeof(u32); i++) {
262                 *dst = le32_to_cpu(*src);
263                 src++;
264                 dst++;
265         }
266 }
267
268
269 /* Used to read bitfield values from double words. */
270 static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
271 {
272         u32 temp = le32_to_cpu(*bitfield);
273         u32 mask = ((1 << size) - 1) << pos;
274         temp &= mask;
275         temp >>= pos;
276         return temp;
277 }
278
279
280
281 #endif  /* __BIG_ENDIAN_BITFIELD */
282
283 #ifdef __BIG_ENDIAN_BITFIELD
284
285 #   define VMXNET3_TXDESC_GET_GEN(txdesc) get_bitfield32(((const __le32 *) \
286                         txdesc) + VMXNET3_TXD_GEN_DWORD_SHIFT, \
287                         VMXNET3_TXD_GEN_SHIFT, VMXNET3_TXD_GEN_SIZE)
288 #   define VMXNET3_TXDESC_GET_EOP(txdesc) get_bitfield32(((const __le32 *) \
289                         txdesc) + VMXNET3_TXD_EOP_DWORD_SHIFT, \
290                         VMXNET3_TXD_EOP_SHIFT, VMXNET3_TXD_EOP_SIZE)
291 #   define VMXNET3_TCD_GET_GEN(tcd) get_bitfield32(((const __le32 *)tcd) + \
292                         VMXNET3_TCD_GEN_DWORD_SHIFT, VMXNET3_TCD_GEN_SHIFT, \
293                         VMXNET3_TCD_GEN_SIZE)
294 #   define VMXNET3_TCD_GET_TXIDX(tcd) get_bitfield32((const __le32 *)tcd, \
295                         VMXNET3_TCD_TXIDX_SHIFT, VMXNET3_TCD_TXIDX_SIZE)
296 #   define vmxnet3_getRxComp(dstrcd, rcd, tmp) do { \
297                         (dstrcd) = (tmp); \
298                         vmxnet3_RxCompToCPU((rcd), (tmp)); \
299                 } while (0)
300 #   define vmxnet3_getRxDesc(dstrxd, rxd, tmp) do { \
301                         (dstrxd) = (tmp); \
302                         vmxnet3_RxDescToCPU((rxd), (tmp)); \
303                 } while (0)
304
305 #else
306
307 #   define VMXNET3_TXDESC_GET_GEN(txdesc) ((txdesc)->gen)
308 #   define VMXNET3_TXDESC_GET_EOP(txdesc) ((txdesc)->eop)
309 #   define VMXNET3_TCD_GET_GEN(tcd) ((tcd)->gen)
310 #   define VMXNET3_TCD_GET_TXIDX(tcd) ((tcd)->txdIdx)
311 #   define vmxnet3_getRxComp(dstrcd, rcd, tmp) (dstrcd) = (rcd)
312 #   define vmxnet3_getRxDesc(dstrxd, rxd, tmp) (dstrxd) = (rxd)
313
314 #endif /* __BIG_ENDIAN_BITFIELD  */
315
316
317 static void
318 vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
319                      struct pci_dev *pdev)
320 {
321         if (tbi->map_type == VMXNET3_MAP_SINGLE)
322                 pci_unmap_single(pdev, tbi->dma_addr, tbi->len,
323                                  PCI_DMA_TODEVICE);
324         else if (tbi->map_type == VMXNET3_MAP_PAGE)
325                 pci_unmap_page(pdev, tbi->dma_addr, tbi->len,
326                                PCI_DMA_TODEVICE);
327         else
328                 BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
329
330         tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
331 }
332
333
334 static int
335 vmxnet3_unmap_pkt(u32 eop_idx, struct vmxnet3_tx_queue *tq,
336                   struct pci_dev *pdev, struct vmxnet3_adapter *adapter)
337 {
338         struct sk_buff *skb;
339         int entries = 0;
340
341         /* no out of order completion */
342         BUG_ON(tq->buf_info[eop_idx].sop_idx != tq->tx_ring.next2comp);
343         BUG_ON(VMXNET3_TXDESC_GET_EOP(&(tq->tx_ring.base[eop_idx].txd)) != 1);
344
345         skb = tq->buf_info[eop_idx].skb;
346         BUG_ON(skb == NULL);
347         tq->buf_info[eop_idx].skb = NULL;
348
349         VMXNET3_INC_RING_IDX_ONLY(eop_idx, tq->tx_ring.size);
350
351         while (tq->tx_ring.next2comp != eop_idx) {
352                 vmxnet3_unmap_tx_buf(tq->buf_info + tq->tx_ring.next2comp,
353                                      pdev);
354
355                 /* update next2comp w/o tx_lock. Since we are marking more,
356                  * instead of less, tx ring entries avail, the worst case is
357                  * that the tx routine incorrectly re-queues a pkt due to
358                  * insufficient tx ring entries.
359                  */
360                 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
361                 entries++;
362         }
363
364         dev_kfree_skb_any(skb);
365         return entries;
366 }
367
368
369 static int
370 vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue *tq,
371                         struct vmxnet3_adapter *adapter)
372 {
373         int completed = 0;
374         union Vmxnet3_GenericDesc *gdesc;
375
376         gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
377         while (VMXNET3_TCD_GET_GEN(&gdesc->tcd) == tq->comp_ring.gen) {
378                 completed += vmxnet3_unmap_pkt(VMXNET3_TCD_GET_TXIDX(
379                                                &gdesc->tcd), tq, adapter->pdev,
380                                                adapter);
381
382                 vmxnet3_comp_ring_adv_next2proc(&tq->comp_ring);
383                 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
384         }
385
386         if (completed) {
387                 spin_lock(&tq->tx_lock);
388                 if (unlikely(vmxnet3_tq_stopped(tq, adapter) &&
389                              vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) >
390                              VMXNET3_WAKE_QUEUE_THRESHOLD(tq) &&
391                              netif_carrier_ok(adapter->netdev))) {
392                         vmxnet3_tq_wake(tq, adapter);
393                 }
394                 spin_unlock(&tq->tx_lock);
395         }
396         return completed;
397 }
398
399
400 static void
401 vmxnet3_tq_cleanup(struct vmxnet3_tx_queue *tq,
402                    struct vmxnet3_adapter *adapter)
403 {
404         int i;
405
406         while (tq->tx_ring.next2comp != tq->tx_ring.next2fill) {
407                 struct vmxnet3_tx_buf_info *tbi;
408
409                 tbi = tq->buf_info + tq->tx_ring.next2comp;
410
411                 vmxnet3_unmap_tx_buf(tbi, adapter->pdev);
412                 if (tbi->skb) {
413                         dev_kfree_skb_any(tbi->skb);
414                         tbi->skb = NULL;
415                 }
416                 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
417         }
418
419         /* sanity check, verify all buffers are indeed unmapped and freed */
420         for (i = 0; i < tq->tx_ring.size; i++) {
421                 BUG_ON(tq->buf_info[i].skb != NULL ||
422                        tq->buf_info[i].map_type != VMXNET3_MAP_NONE);
423         }
424
425         tq->tx_ring.gen = VMXNET3_INIT_GEN;
426         tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
427
428         tq->comp_ring.gen = VMXNET3_INIT_GEN;
429         tq->comp_ring.next2proc = 0;
430 }
431
432
433 static void
434 vmxnet3_tq_destroy(struct vmxnet3_tx_queue *tq,
435                    struct vmxnet3_adapter *adapter)
436 {
437         if (tq->tx_ring.base) {
438                 pci_free_consistent(adapter->pdev, tq->tx_ring.size *
439                                     sizeof(struct Vmxnet3_TxDesc),
440                                     tq->tx_ring.base, tq->tx_ring.basePA);
441                 tq->tx_ring.base = NULL;
442         }
443         if (tq->data_ring.base) {
444                 pci_free_consistent(adapter->pdev, tq->data_ring.size *
445                                     sizeof(struct Vmxnet3_TxDataDesc),
446                                     tq->data_ring.base, tq->data_ring.basePA);
447                 tq->data_ring.base = NULL;
448         }
449         if (tq->comp_ring.base) {
450                 pci_free_consistent(adapter->pdev, tq->comp_ring.size *
451                                     sizeof(struct Vmxnet3_TxCompDesc),
452                                     tq->comp_ring.base, tq->comp_ring.basePA);
453                 tq->comp_ring.base = NULL;
454         }
455         kfree(tq->buf_info);
456         tq->buf_info = NULL;
457 }
458
459
460 /* Destroy all tx queues */
461 void
462 vmxnet3_tq_destroy_all(struct vmxnet3_adapter *adapter)
463 {
464         int i;
465
466         for (i = 0; i < adapter->num_tx_queues; i++)
467                 vmxnet3_tq_destroy(&adapter->tx_queue[i], adapter);
468 }
469
470
471 static void
472 vmxnet3_tq_init(struct vmxnet3_tx_queue *tq,
473                 struct vmxnet3_adapter *adapter)
474 {
475         int i;
476
477         /* reset the tx ring contents to 0 and reset the tx ring states */
478         memset(tq->tx_ring.base, 0, tq->tx_ring.size *
479                sizeof(struct Vmxnet3_TxDesc));
480         tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
481         tq->tx_ring.gen = VMXNET3_INIT_GEN;
482
483         memset(tq->data_ring.base, 0, tq->data_ring.size *
484                sizeof(struct Vmxnet3_TxDataDesc));
485
486         /* reset the tx comp ring contents to 0 and reset comp ring states */
487         memset(tq->comp_ring.base, 0, tq->comp_ring.size *
488                sizeof(struct Vmxnet3_TxCompDesc));
489         tq->comp_ring.next2proc = 0;
490         tq->comp_ring.gen = VMXNET3_INIT_GEN;
491
492         /* reset the bookkeeping data */
493         memset(tq->buf_info, 0, sizeof(tq->buf_info[0]) * tq->tx_ring.size);
494         for (i = 0; i < tq->tx_ring.size; i++)
495                 tq->buf_info[i].map_type = VMXNET3_MAP_NONE;
496
497         /* stats are not reset */
498 }
499
500
501 static int
502 vmxnet3_tq_create(struct vmxnet3_tx_queue *tq,
503                   struct vmxnet3_adapter *adapter)
504 {
505         BUG_ON(tq->tx_ring.base || tq->data_ring.base ||
506                tq->comp_ring.base || tq->buf_info);
507
508         tq->tx_ring.base = pci_alloc_consistent(adapter->pdev, tq->tx_ring.size
509                            * sizeof(struct Vmxnet3_TxDesc),
510                            &tq->tx_ring.basePA);
511         if (!tq->tx_ring.base) {
512                 printk(KERN_ERR "%s: failed to allocate tx ring\n",
513                        adapter->netdev->name);
514                 goto err;
515         }
516
517         tq->data_ring.base = pci_alloc_consistent(adapter->pdev,
518                              tq->data_ring.size *
519                              sizeof(struct Vmxnet3_TxDataDesc),
520                              &tq->data_ring.basePA);
521         if (!tq->data_ring.base) {
522                 printk(KERN_ERR "%s: failed to allocate data ring\n",
523                        adapter->netdev->name);
524                 goto err;
525         }
526
527         tq->comp_ring.base = pci_alloc_consistent(adapter->pdev,
528                              tq->comp_ring.size *
529                              sizeof(struct Vmxnet3_TxCompDesc),
530                              &tq->comp_ring.basePA);
531         if (!tq->comp_ring.base) {
532                 printk(KERN_ERR "%s: failed to allocate tx comp ring\n",
533                        adapter->netdev->name);
534                 goto err;
535         }
536
537         tq->buf_info = kcalloc(tq->tx_ring.size, sizeof(tq->buf_info[0]),
538                                GFP_KERNEL);
539         if (!tq->buf_info) {
540                 printk(KERN_ERR "%s: failed to allocate tx bufinfo\n",
541                        adapter->netdev->name);
542                 goto err;
543         }
544
545         return 0;
546
547 err:
548         vmxnet3_tq_destroy(tq, adapter);
549         return -ENOMEM;
550 }
551
552 static void
553 vmxnet3_tq_cleanup_all(struct vmxnet3_adapter *adapter)
554 {
555         int i;
556
557         for (i = 0; i < adapter->num_tx_queues; i++)
558                 vmxnet3_tq_cleanup(&adapter->tx_queue[i], adapter);
559 }
560
561 /*
562  *    starting from ring->next2fill, allocate rx buffers for the given ring
563  *    of the rx queue and update the rx desc. stop after @num_to_alloc buffers
564  *    are allocated or allocation fails
565  */
566
567 static int
568 vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
569                         int num_to_alloc, struct vmxnet3_adapter *adapter)
570 {
571         int num_allocated = 0;
572         struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
573         struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
574         u32 val;
575
576         while (num_allocated < num_to_alloc) {
577                 struct vmxnet3_rx_buf_info *rbi;
578                 union Vmxnet3_GenericDesc *gd;
579
580                 rbi = rbi_base + ring->next2fill;
581                 gd = ring->base + ring->next2fill;
582
583                 if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
584                         if (rbi->skb == NULL) {
585                                 rbi->skb = dev_alloc_skb(rbi->len +
586                                                          NET_IP_ALIGN);
587                                 if (unlikely(rbi->skb == NULL)) {
588                                         rq->stats.rx_buf_alloc_failure++;
589                                         break;
590                                 }
591                                 rbi->skb->dev = adapter->netdev;
592
593                                 skb_reserve(rbi->skb, NET_IP_ALIGN);
594                                 rbi->dma_addr = pci_map_single(adapter->pdev,
595                                                 rbi->skb->data, rbi->len,
596                                                 PCI_DMA_FROMDEVICE);
597                         } else {
598                                 /* rx buffer skipped by the device */
599                         }
600                         val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
601                 } else {
602                         BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
603                                rbi->len  != PAGE_SIZE);
604
605                         if (rbi->page == NULL) {
606                                 rbi->page = alloc_page(GFP_ATOMIC);
607                                 if (unlikely(rbi->page == NULL)) {
608                                         rq->stats.rx_buf_alloc_failure++;
609                                         break;
610                                 }
611                                 rbi->dma_addr = pci_map_page(adapter->pdev,
612                                                 rbi->page, 0, PAGE_SIZE,
613                                                 PCI_DMA_FROMDEVICE);
614                         } else {
615                                 /* rx buffers skipped by the device */
616                         }
617                         val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
618                 }
619
620                 BUG_ON(rbi->dma_addr == 0);
621                 gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
622                 gd->dword[2] = cpu_to_le32((ring->gen << VMXNET3_RXD_GEN_SHIFT)
623                                            | val | rbi->len);
624
625                 num_allocated++;
626                 vmxnet3_cmd_ring_adv_next2fill(ring);
627         }
628         rq->uncommitted[ring_idx] += num_allocated;
629
630         dev_dbg(&adapter->netdev->dev,
631                 "alloc_rx_buf: %d allocated, next2fill %u, next2comp "
632                 "%u, uncommited %u\n", num_allocated, ring->next2fill,
633                 ring->next2comp, rq->uncommitted[ring_idx]);
634
635         /* so that the device can distinguish a full ring and an empty ring */
636         BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
637
638         return num_allocated;
639 }
640
641
642 static void
643 vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
644                     struct vmxnet3_rx_buf_info *rbi)
645 {
646         struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
647                 skb_shinfo(skb)->nr_frags;
648
649         BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
650
651         frag->page = rbi->page;
652         frag->page_offset = 0;
653         frag->size = rcd->len;
654         skb->data_len += frag->size;
655         skb_shinfo(skb)->nr_frags++;
656 }
657
658
659 static void
660 vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
661                 struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
662                 struct vmxnet3_adapter *adapter)
663 {
664         u32 dw2, len;
665         unsigned long buf_offset;
666         int i;
667         union Vmxnet3_GenericDesc *gdesc;
668         struct vmxnet3_tx_buf_info *tbi = NULL;
669
670         BUG_ON(ctx->copy_size > skb_headlen(skb));
671
672         /* use the previous gen bit for the SOP desc */
673         dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
674
675         ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
676         gdesc = ctx->sop_txd; /* both loops below can be skipped */
677
678         /* no need to map the buffer if headers are copied */
679         if (ctx->copy_size) {
680                 ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
681                                         tq->tx_ring.next2fill *
682                                         sizeof(struct Vmxnet3_TxDataDesc));
683                 ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
684                 ctx->sop_txd->dword[3] = 0;
685
686                 tbi = tq->buf_info + tq->tx_ring.next2fill;
687                 tbi->map_type = VMXNET3_MAP_NONE;
688
689                 dev_dbg(&adapter->netdev->dev,
690                         "txd[%u]: 0x%Lx 0x%x 0x%x\n",
691                         tq->tx_ring.next2fill,
692                         le64_to_cpu(ctx->sop_txd->txd.addr),
693                         ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
694                 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
695
696                 /* use the right gen for non-SOP desc */
697                 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
698         }
699
700         /* linear part can use multiple tx desc if it's big */
701         len = skb_headlen(skb) - ctx->copy_size;
702         buf_offset = ctx->copy_size;
703         while (len) {
704                 u32 buf_size;
705
706                 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
707                         buf_size = len;
708                         dw2 |= len;
709                 } else {
710                         buf_size = VMXNET3_MAX_TX_BUF_SIZE;
711                         /* spec says that for TxDesc.len, 0 == 2^14 */
712                 }
713
714                 tbi = tq->buf_info + tq->tx_ring.next2fill;
715                 tbi->map_type = VMXNET3_MAP_SINGLE;
716                 tbi->dma_addr = pci_map_single(adapter->pdev,
717                                 skb->data + buf_offset, buf_size,
718                                 PCI_DMA_TODEVICE);
719
720                 tbi->len = buf_size;
721
722                 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
723                 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
724
725                 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
726                 gdesc->dword[2] = cpu_to_le32(dw2);
727                 gdesc->dword[3] = 0;
728
729                 dev_dbg(&adapter->netdev->dev,
730                         "txd[%u]: 0x%Lx 0x%x 0x%x\n",
731                         tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
732                         le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
733                 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
734                 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
735
736                 len -= buf_size;
737                 buf_offset += buf_size;
738         }
739
740         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
741                 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
742
743                 tbi = tq->buf_info + tq->tx_ring.next2fill;
744                 tbi->map_type = VMXNET3_MAP_PAGE;
745                 tbi->dma_addr = pci_map_page(adapter->pdev, frag->page,
746                                              frag->page_offset, frag->size,
747                                              PCI_DMA_TODEVICE);
748
749                 tbi->len = frag->size;
750
751                 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
752                 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
753
754                 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
755                 gdesc->dword[2] = cpu_to_le32(dw2 | frag->size);
756                 gdesc->dword[3] = 0;
757
758                 dev_dbg(&adapter->netdev->dev,
759                         "txd[%u]: 0x%llu %u %u\n",
760                         tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
761                         le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
762                 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
763                 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
764         }
765
766         ctx->eop_txd = gdesc;
767
768         /* set the last buf_info for the pkt */
769         tbi->skb = skb;
770         tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
771 }
772
773
774 /* Init all tx queues */
775 static void
776 vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
777 {
778         int i;
779
780         for (i = 0; i < adapter->num_tx_queues; i++)
781                 vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
782 }
783
784
785 /*
786  *    parse and copy relevant protocol headers:
787  *      For a tso pkt, relevant headers are L2/3/4 including options
788  *      For a pkt requesting csum offloading, they are L2/3 and may include L4
789  *      if it's a TCP/UDP pkt
790  *
791  * Returns:
792  *    -1:  error happens during parsing
793  *     0:  protocol headers parsed, but too big to be copied
794  *     1:  protocol headers parsed and copied
795  *
796  * Other effects:
797  *    1. related *ctx fields are updated.
798  *    2. ctx->copy_size is # of bytes copied
799  *    3. the portion copied is guaranteed to be in the linear part
800  *
801  */
802 static int
803 vmxnet3_parse_and_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
804                            struct vmxnet3_tx_ctx *ctx,
805                            struct vmxnet3_adapter *adapter)
806 {
807         struct Vmxnet3_TxDataDesc *tdd;
808
809         if (ctx->mss) { /* TSO */
810                 ctx->eth_ip_hdr_size = skb_transport_offset(skb);
811                 ctx->l4_hdr_size = ((struct tcphdr *)
812                                    skb_transport_header(skb))->doff * 4;
813                 ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
814         } else {
815                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
816                         ctx->eth_ip_hdr_size = skb_checksum_start_offset(skb);
817
818                         if (ctx->ipv4) {
819                                 struct iphdr *iph = (struct iphdr *)
820                                                     skb_network_header(skb);
821                                 if (iph->protocol == IPPROTO_TCP)
822                                         ctx->l4_hdr_size = ((struct tcphdr *)
823                                            skb_transport_header(skb))->doff * 4;
824                                 else if (iph->protocol == IPPROTO_UDP)
825                                         /*
826                                          * Use tcp header size so that bytes to
827                                          * be copied are more than required by
828                                          * the device.
829                                          */
830                                         ctx->l4_hdr_size =
831                                                         sizeof(struct tcphdr);
832                                 else
833                                         ctx->l4_hdr_size = 0;
834                         } else {
835                                 /* for simplicity, don't copy L4 headers */
836                                 ctx->l4_hdr_size = 0;
837                         }
838                         ctx->copy_size = ctx->eth_ip_hdr_size +
839                                          ctx->l4_hdr_size;
840                 } else {
841                         ctx->eth_ip_hdr_size = 0;
842                         ctx->l4_hdr_size = 0;
843                         /* copy as much as allowed */
844                         ctx->copy_size = min((unsigned int)VMXNET3_HDR_COPY_SIZE
845                                              , skb_headlen(skb));
846                 }
847
848                 /* make sure headers are accessible directly */
849                 if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
850                         goto err;
851         }
852
853         if (unlikely(ctx->copy_size > VMXNET3_HDR_COPY_SIZE)) {
854                 tq->stats.oversized_hdr++;
855                 ctx->copy_size = 0;
856                 return 0;
857         }
858
859         tdd = tq->data_ring.base + tq->tx_ring.next2fill;
860
861         memcpy(tdd->data, skb->data, ctx->copy_size);
862         dev_dbg(&adapter->netdev->dev,
863                 "copy %u bytes to dataRing[%u]\n",
864                 ctx->copy_size, tq->tx_ring.next2fill);
865         return 1;
866
867 err:
868         return -1;
869 }
870
871
872 static void
873 vmxnet3_prepare_tso(struct sk_buff *skb,
874                     struct vmxnet3_tx_ctx *ctx)
875 {
876         struct tcphdr *tcph = (struct tcphdr *)skb_transport_header(skb);
877         if (ctx->ipv4) {
878                 struct iphdr *iph = (struct iphdr *)skb_network_header(skb);
879                 iph->check = 0;
880                 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
881                                                  IPPROTO_TCP, 0);
882         } else {
883                 struct ipv6hdr *iph = (struct ipv6hdr *)skb_network_header(skb);
884                 tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
885                                                IPPROTO_TCP, 0);
886         }
887 }
888
889
890 /*
891  * Transmits a pkt thru a given tq
892  * Returns:
893  *    NETDEV_TX_OK:      descriptors are setup successfully
894  *    NETDEV_TX_OK:      error occurred, the pkt is dropped
895  *    NETDEV_TX_BUSY:    tx ring is full, queue is stopped
896  *
897  * Side-effects:
898  *    1. tx ring may be changed
899  *    2. tq stats may be updated accordingly
900  *    3. shared->txNumDeferred may be updated
901  */
902
903 static int
904 vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
905                 struct vmxnet3_adapter *adapter, struct net_device *netdev)
906 {
907         int ret;
908         u32 count;
909         unsigned long flags;
910         struct vmxnet3_tx_ctx ctx;
911         union Vmxnet3_GenericDesc *gdesc;
912 #ifdef __BIG_ENDIAN_BITFIELD
913         /* Use temporary descriptor to avoid touching bits multiple times */
914         union Vmxnet3_GenericDesc tempTxDesc;
915 #endif
916
917         /* conservatively estimate # of descriptors to use */
918         count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) +
919                 skb_shinfo(skb)->nr_frags + 1;
920
921         ctx.ipv4 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IP));
922
923         ctx.mss = skb_shinfo(skb)->gso_size;
924         if (ctx.mss) {
925                 if (skb_header_cloned(skb)) {
926                         if (unlikely(pskb_expand_head(skb, 0, 0,
927                                                       GFP_ATOMIC) != 0)) {
928                                 tq->stats.drop_tso++;
929                                 goto drop_pkt;
930                         }
931                         tq->stats.copy_skb_header++;
932                 }
933                 vmxnet3_prepare_tso(skb, &ctx);
934         } else {
935                 if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
936
937                         /* non-tso pkts must not use more than
938                          * VMXNET3_MAX_TXD_PER_PKT entries
939                          */
940                         if (skb_linearize(skb) != 0) {
941                                 tq->stats.drop_too_many_frags++;
942                                 goto drop_pkt;
943                         }
944                         tq->stats.linearized++;
945
946                         /* recalculate the # of descriptors to use */
947                         count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
948                 }
949         }
950
951         spin_lock_irqsave(&tq->tx_lock, flags);
952
953         if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
954                 tq->stats.tx_ring_full++;
955                 dev_dbg(&adapter->netdev->dev,
956                         "tx queue stopped on %s, next2comp %u"
957                         " next2fill %u\n", adapter->netdev->name,
958                         tq->tx_ring.next2comp, tq->tx_ring.next2fill);
959
960                 vmxnet3_tq_stop(tq, adapter);
961                 spin_unlock_irqrestore(&tq->tx_lock, flags);
962                 return NETDEV_TX_BUSY;
963         }
964
965
966         ret = vmxnet3_parse_and_copy_hdr(skb, tq, &ctx, adapter);
967         if (ret >= 0) {
968                 BUG_ON(ret <= 0 && ctx.copy_size != 0);
969                 /* hdrs parsed, check against other limits */
970                 if (ctx.mss) {
971                         if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
972                                      VMXNET3_MAX_TX_BUF_SIZE)) {
973                                 goto hdr_too_big;
974                         }
975                 } else {
976                         if (skb->ip_summed == CHECKSUM_PARTIAL) {
977                                 if (unlikely(ctx.eth_ip_hdr_size +
978                                              skb->csum_offset >
979                                              VMXNET3_MAX_CSUM_OFFSET)) {
980                                         goto hdr_too_big;
981                                 }
982                         }
983                 }
984         } else {
985                 tq->stats.drop_hdr_inspect_err++;
986                 goto unlock_drop_pkt;
987         }
988
989         /* fill tx descs related to addr & len */
990         vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter);
991
992         /* setup the EOP desc */
993         ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
994
995         /* setup the SOP desc */
996 #ifdef __BIG_ENDIAN_BITFIELD
997         gdesc = &tempTxDesc;
998         gdesc->dword[2] = ctx.sop_txd->dword[2];
999         gdesc->dword[3] = ctx.sop_txd->dword[3];
1000 #else
1001         gdesc = ctx.sop_txd;
1002 #endif
1003         if (ctx.mss) {
1004                 gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
1005                 gdesc->txd.om = VMXNET3_OM_TSO;
1006                 gdesc->txd.msscof = ctx.mss;
1007                 le32_add_cpu(&tq->shared->txNumDeferred, (skb->len -
1008                              gdesc->txd.hlen + ctx.mss - 1) / ctx.mss);
1009         } else {
1010                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1011                         gdesc->txd.hlen = ctx.eth_ip_hdr_size;
1012                         gdesc->txd.om = VMXNET3_OM_CSUM;
1013                         gdesc->txd.msscof = ctx.eth_ip_hdr_size +
1014                                             skb->csum_offset;
1015                 } else {
1016                         gdesc->txd.om = 0;
1017                         gdesc->txd.msscof = 0;
1018                 }
1019                 le32_add_cpu(&tq->shared->txNumDeferred, 1);
1020         }
1021
1022         if (vlan_tx_tag_present(skb)) {
1023                 gdesc->txd.ti = 1;
1024                 gdesc->txd.tci = vlan_tx_tag_get(skb);
1025         }
1026
1027         /* finally flips the GEN bit of the SOP desc. */
1028         gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
1029                                                   VMXNET3_TXD_GEN);
1030 #ifdef __BIG_ENDIAN_BITFIELD
1031         /* Finished updating in bitfields of Tx Desc, so write them in original
1032          * place.
1033          */
1034         vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
1035                            (struct Vmxnet3_TxDesc *)ctx.sop_txd);
1036         gdesc = ctx.sop_txd;
1037 #endif
1038         dev_dbg(&adapter->netdev->dev,
1039                 "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
1040                 (u32)((union Vmxnet3_GenericDesc *)ctx.sop_txd -
1041                 tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
1042                 le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
1043
1044         spin_unlock_irqrestore(&tq->tx_lock, flags);
1045
1046         if (le32_to_cpu(tq->shared->txNumDeferred) >=
1047                                         le32_to_cpu(tq->shared->txThreshold)) {
1048                 tq->shared->txNumDeferred = 0;
1049                 VMXNET3_WRITE_BAR0_REG(adapter,
1050                                        VMXNET3_REG_TXPROD + tq->qid * 8,
1051                                        tq->tx_ring.next2fill);
1052         }
1053
1054         return NETDEV_TX_OK;
1055
1056 hdr_too_big:
1057         tq->stats.drop_oversized_hdr++;
1058 unlock_drop_pkt:
1059         spin_unlock_irqrestore(&tq->tx_lock, flags);
1060 drop_pkt:
1061         tq->stats.drop_total++;
1062         dev_kfree_skb(skb);
1063         return NETDEV_TX_OK;
1064 }
1065
1066
1067 static netdev_tx_t
1068 vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1069 {
1070         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1071
1072                 BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
1073                 return vmxnet3_tq_xmit(skb,
1074                                        &adapter->tx_queue[skb->queue_mapping],
1075                                        adapter, netdev);
1076 }
1077
1078
1079 static void
1080 vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
1081                 struct sk_buff *skb,
1082                 union Vmxnet3_GenericDesc *gdesc)
1083 {
1084         if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
1085                 /* typical case: TCP/UDP over IP and both csums are correct */
1086                 if ((le32_to_cpu(gdesc->dword[3]) & VMXNET3_RCD_CSUM_OK) ==
1087                                                         VMXNET3_RCD_CSUM_OK) {
1088                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1089                         BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
1090                         BUG_ON(!(gdesc->rcd.v4  || gdesc->rcd.v6));
1091                         BUG_ON(gdesc->rcd.frg);
1092                 } else {
1093                         if (gdesc->rcd.csum) {
1094                                 skb->csum = htons(gdesc->rcd.csum);
1095                                 skb->ip_summed = CHECKSUM_PARTIAL;
1096                         } else {
1097                                 skb_checksum_none_assert(skb);
1098                         }
1099                 }
1100         } else {
1101                 skb_checksum_none_assert(skb);
1102         }
1103 }
1104
1105
1106 static void
1107 vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
1108                  struct vmxnet3_rx_ctx *ctx,  struct vmxnet3_adapter *adapter)
1109 {
1110         rq->stats.drop_err++;
1111         if (!rcd->fcs)
1112                 rq->stats.drop_fcs++;
1113
1114         rq->stats.drop_total++;
1115
1116         /*
1117          * We do not unmap and chain the rx buffer to the skb.
1118          * We basically pretend this buffer is not used and will be recycled
1119          * by vmxnet3_rq_alloc_rx_buf()
1120          */
1121
1122         /*
1123          * ctx->skb may be NULL if this is the first and the only one
1124          * desc for the pkt
1125          */
1126         if (ctx->skb)
1127                 dev_kfree_skb_irq(ctx->skb);
1128
1129         ctx->skb = NULL;
1130 }
1131
1132
1133 static int
1134 vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
1135                        struct vmxnet3_adapter *adapter, int quota)
1136 {
1137         static const u32 rxprod_reg[2] = {
1138                 VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2
1139         };
1140         u32 num_rxd = 0;
1141         struct Vmxnet3_RxCompDesc *rcd;
1142         struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
1143 #ifdef __BIG_ENDIAN_BITFIELD
1144         struct Vmxnet3_RxDesc rxCmdDesc;
1145         struct Vmxnet3_RxCompDesc rxComp;
1146 #endif
1147         vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
1148                           &rxComp);
1149         while (rcd->gen == rq->comp_ring.gen) {
1150                 struct vmxnet3_rx_buf_info *rbi;
1151                 struct sk_buff *skb;
1152                 int num_to_alloc;
1153                 struct Vmxnet3_RxDesc *rxd;
1154                 u32 idx, ring_idx;
1155
1156                 if (num_rxd >= quota) {
1157                         /* we may stop even before we see the EOP desc of
1158                          * the current pkt
1159                          */
1160                         break;
1161                 }
1162                 num_rxd++;
1163                 BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2);
1164                 idx = rcd->rxdIdx;
1165                 ring_idx = rcd->rqID < adapter->num_rx_queues ? 0 : 1;
1166                 vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
1167                                   &rxCmdDesc);
1168                 rbi = rq->buf_info[ring_idx] + idx;
1169
1170                 BUG_ON(rxd->addr != rbi->dma_addr ||
1171                        rxd->len != rbi->len);
1172
1173                 if (unlikely(rcd->eop && rcd->err)) {
1174                         vmxnet3_rx_error(rq, rcd, ctx, adapter);
1175                         goto rcd_done;
1176                 }
1177
1178                 if (rcd->sop) { /* first buf of the pkt */
1179                         BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
1180                                rcd->rqID != rq->qid);
1181
1182                         BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
1183                         BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
1184
1185                         if (unlikely(rcd->len == 0)) {
1186                                 /* Pretend the rx buffer is skipped. */
1187                                 BUG_ON(!(rcd->sop && rcd->eop));
1188                                 dev_dbg(&adapter->netdev->dev,
1189                                         "rxRing[%u][%u] 0 length\n",
1190                                         ring_idx, idx);
1191                                 goto rcd_done;
1192                         }
1193
1194                         ctx->skb = rbi->skb;
1195                         rbi->skb = NULL;
1196
1197                         pci_unmap_single(adapter->pdev, rbi->dma_addr, rbi->len,
1198                                          PCI_DMA_FROMDEVICE);
1199
1200                         skb_put(ctx->skb, rcd->len);
1201                 } else {
1202                         BUG_ON(ctx->skb == NULL);
1203                         /* non SOP buffer must be type 1 in most cases */
1204                         if (rbi->buf_type == VMXNET3_RX_BUF_PAGE) {
1205                                 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
1206
1207                                 if (rcd->len) {
1208                                         pci_unmap_page(adapter->pdev,
1209                                                        rbi->dma_addr, rbi->len,
1210                                                        PCI_DMA_FROMDEVICE);
1211
1212                                         vmxnet3_append_frag(ctx->skb, rcd, rbi);
1213                                         rbi->page = NULL;
1214                                 }
1215                         } else {
1216                                 /*
1217                                  * The only time a non-SOP buffer is type 0 is
1218                                  * when it's EOP and error flag is raised, which
1219                                  * has already been handled.
1220                                  */
1221                                 BUG_ON(true);
1222                         }
1223                 }
1224
1225                 skb = ctx->skb;
1226                 if (rcd->eop) {
1227                         skb->len += skb->data_len;
1228                         skb->truesize += skb->data_len;
1229
1230                         vmxnet3_rx_csum(adapter, skb,
1231                                         (union Vmxnet3_GenericDesc *)rcd);
1232                         skb->protocol = eth_type_trans(skb, adapter->netdev);
1233
1234                         if (unlikely(rcd->ts))
1235                                 __vlan_hwaccel_put_tag(skb, rcd->tci);
1236
1237                         if (adapter->netdev->features & NETIF_F_LRO)
1238                                 netif_receive_skb(skb);
1239                         else
1240                                 napi_gro_receive(&rq->napi, skb);
1241
1242                         ctx->skb = NULL;
1243                 }
1244
1245 rcd_done:
1246                 /* device may skip some rx descs */
1247                 rq->rx_ring[ring_idx].next2comp = idx;
1248                 VMXNET3_INC_RING_IDX_ONLY(rq->rx_ring[ring_idx].next2comp,
1249                                           rq->rx_ring[ring_idx].size);
1250
1251                 /* refill rx buffers frequently to avoid starving the h/w */
1252                 num_to_alloc = vmxnet3_cmd_ring_desc_avail(rq->rx_ring +
1253                                                            ring_idx);
1254                 if (unlikely(num_to_alloc > VMXNET3_RX_ALLOC_THRESHOLD(rq,
1255                                                         ring_idx, adapter))) {
1256                         vmxnet3_rq_alloc_rx_buf(rq, ring_idx, num_to_alloc,
1257                                                 adapter);
1258
1259                         /* if needed, update the register */
1260                         if (unlikely(rq->shared->updateRxProd)) {
1261                                 VMXNET3_WRITE_BAR0_REG(adapter,
1262                                         rxprod_reg[ring_idx] + rq->qid * 8,
1263                                         rq->rx_ring[ring_idx].next2fill);
1264                                 rq->uncommitted[ring_idx] = 0;
1265                         }
1266                 }
1267
1268                 vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
1269                 vmxnet3_getRxComp(rcd,
1270                      &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
1271         }
1272
1273         return num_rxd;
1274 }
1275
1276
1277 static void
1278 vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
1279                    struct vmxnet3_adapter *adapter)
1280 {
1281         u32 i, ring_idx;
1282         struct Vmxnet3_RxDesc *rxd;
1283
1284         for (ring_idx = 0; ring_idx < 2; ring_idx++) {
1285                 for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
1286 #ifdef __BIG_ENDIAN_BITFIELD
1287                         struct Vmxnet3_RxDesc rxDesc;
1288 #endif
1289                         vmxnet3_getRxDesc(rxd,
1290                                 &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
1291
1292                         if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
1293                                         rq->buf_info[ring_idx][i].skb) {
1294                                 pci_unmap_single(adapter->pdev, rxd->addr,
1295                                                  rxd->len, PCI_DMA_FROMDEVICE);
1296                                 dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
1297                                 rq->buf_info[ring_idx][i].skb = NULL;
1298                         } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
1299                                         rq->buf_info[ring_idx][i].page) {
1300                                 pci_unmap_page(adapter->pdev, rxd->addr,
1301                                                rxd->len, PCI_DMA_FROMDEVICE);
1302                                 put_page(rq->buf_info[ring_idx][i].page);
1303                                 rq->buf_info[ring_idx][i].page = NULL;
1304                         }
1305                 }
1306
1307                 rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
1308                 rq->rx_ring[ring_idx].next2fill =
1309                                         rq->rx_ring[ring_idx].next2comp = 0;
1310                 rq->uncommitted[ring_idx] = 0;
1311         }
1312
1313         rq->comp_ring.gen = VMXNET3_INIT_GEN;
1314         rq->comp_ring.next2proc = 0;
1315 }
1316
1317
1318 static void
1319 vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
1320 {
1321         int i;
1322
1323         for (i = 0; i < adapter->num_rx_queues; i++)
1324                 vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
1325 }
1326
1327
1328 void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
1329                         struct vmxnet3_adapter *adapter)
1330 {
1331         int i;
1332         int j;
1333
1334         /* all rx buffers must have already been freed */
1335         for (i = 0; i < 2; i++) {
1336                 if (rq->buf_info[i]) {
1337                         for (j = 0; j < rq->rx_ring[i].size; j++)
1338                                 BUG_ON(rq->buf_info[i][j].page != NULL);
1339                 }
1340         }
1341
1342
1343         kfree(rq->buf_info[0]);
1344
1345         for (i = 0; i < 2; i++) {
1346                 if (rq->rx_ring[i].base) {
1347                         pci_free_consistent(adapter->pdev, rq->rx_ring[i].size
1348                                             * sizeof(struct Vmxnet3_RxDesc),
1349                                             rq->rx_ring[i].base,
1350                                             rq->rx_ring[i].basePA);
1351                         rq->rx_ring[i].base = NULL;
1352                 }
1353                 rq->buf_info[i] = NULL;
1354         }
1355
1356         if (rq->comp_ring.base) {
1357                 pci_free_consistent(adapter->pdev, rq->comp_ring.size *
1358                                     sizeof(struct Vmxnet3_RxCompDesc),
1359                                     rq->comp_ring.base, rq->comp_ring.basePA);
1360                 rq->comp_ring.base = NULL;
1361         }
1362 }
1363
1364
1365 static int
1366 vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
1367                 struct vmxnet3_adapter  *adapter)
1368 {
1369         int i;
1370
1371         /* initialize buf_info */
1372         for (i = 0; i < rq->rx_ring[0].size; i++) {
1373
1374                 /* 1st buf for a pkt is skbuff */
1375                 if (i % adapter->rx_buf_per_pkt == 0) {
1376                         rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
1377                         rq->buf_info[0][i].len = adapter->skb_buf_size;
1378                 } else { /* subsequent bufs for a pkt is frag */
1379                         rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
1380                         rq->buf_info[0][i].len = PAGE_SIZE;
1381                 }
1382         }
1383         for (i = 0; i < rq->rx_ring[1].size; i++) {
1384                 rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
1385                 rq->buf_info[1][i].len = PAGE_SIZE;
1386         }
1387
1388         /* reset internal state and allocate buffers for both rings */
1389         for (i = 0; i < 2; i++) {
1390                 rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
1391                 rq->uncommitted[i] = 0;
1392
1393                 memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
1394                        sizeof(struct Vmxnet3_RxDesc));
1395                 rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
1396         }
1397         if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
1398                                     adapter) == 0) {
1399                 /* at least has 1 rx buffer for the 1st ring */
1400                 return -ENOMEM;
1401         }
1402         vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
1403
1404         /* reset the comp ring */
1405         rq->comp_ring.next2proc = 0;
1406         memset(rq->comp_ring.base, 0, rq->comp_ring.size *
1407                sizeof(struct Vmxnet3_RxCompDesc));
1408         rq->comp_ring.gen = VMXNET3_INIT_GEN;
1409
1410         /* reset rxctx */
1411         rq->rx_ctx.skb = NULL;
1412
1413         /* stats are not reset */
1414         return 0;
1415 }
1416
1417
1418 static int
1419 vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
1420 {
1421         int i, err = 0;
1422
1423         for (i = 0; i < adapter->num_rx_queues; i++) {
1424                 err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
1425                 if (unlikely(err)) {
1426                         dev_err(&adapter->netdev->dev, "%s: failed to "
1427                                 "initialize rx queue%i\n",
1428                                 adapter->netdev->name, i);
1429                         break;
1430                 }
1431         }
1432         return err;
1433
1434 }
1435
1436
1437 static int
1438 vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
1439 {
1440         int i;
1441         size_t sz;
1442         struct vmxnet3_rx_buf_info *bi;
1443
1444         for (i = 0; i < 2; i++) {
1445
1446                 sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
1447                 rq->rx_ring[i].base = pci_alloc_consistent(adapter->pdev, sz,
1448                                                         &rq->rx_ring[i].basePA);
1449                 if (!rq->rx_ring[i].base) {
1450                         printk(KERN_ERR "%s: failed to allocate rx ring %d\n",
1451                                adapter->netdev->name, i);
1452                         goto err;
1453                 }
1454         }
1455
1456         sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
1457         rq->comp_ring.base = pci_alloc_consistent(adapter->pdev, sz,
1458                                                   &rq->comp_ring.basePA);
1459         if (!rq->comp_ring.base) {
1460                 printk(KERN_ERR "%s: failed to allocate rx comp ring\n",
1461                        adapter->netdev->name);
1462                 goto err;
1463         }
1464
1465         sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
1466                                                    rq->rx_ring[1].size);
1467         bi = kzalloc(sz, GFP_KERNEL);
1468         if (!bi) {
1469                 printk(KERN_ERR "%s: failed to allocate rx bufinfo\n",
1470                        adapter->netdev->name);
1471                 goto err;
1472         }
1473         rq->buf_info[0] = bi;
1474         rq->buf_info[1] = bi + rq->rx_ring[0].size;
1475
1476         return 0;
1477
1478 err:
1479         vmxnet3_rq_destroy(rq, adapter);
1480         return -ENOMEM;
1481 }
1482
1483
1484 static int
1485 vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
1486 {
1487         int i, err = 0;
1488
1489         for (i = 0; i < adapter->num_rx_queues; i++) {
1490                 err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
1491                 if (unlikely(err)) {
1492                         dev_err(&adapter->netdev->dev,
1493                                 "%s: failed to create rx queue%i\n",
1494                                 adapter->netdev->name, i);
1495                         goto err_out;
1496                 }
1497         }
1498         return err;
1499 err_out:
1500         vmxnet3_rq_destroy_all(adapter);
1501         return err;
1502
1503 }
1504
1505 /* Multiple queue aware polling function for tx and rx */
1506
1507 static int
1508 vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
1509 {
1510         int rcd_done = 0, i;
1511         if (unlikely(adapter->shared->ecr))
1512                 vmxnet3_process_events(adapter);
1513         for (i = 0; i < adapter->num_tx_queues; i++)
1514                 vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
1515
1516         for (i = 0; i < adapter->num_rx_queues; i++)
1517                 rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
1518                                                    adapter, budget);
1519         return rcd_done;
1520 }
1521
1522
1523 static int
1524 vmxnet3_poll(struct napi_struct *napi, int budget)
1525 {
1526         struct vmxnet3_rx_queue *rx_queue = container_of(napi,
1527                                           struct vmxnet3_rx_queue, napi);
1528         int rxd_done;
1529
1530         rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
1531
1532         if (rxd_done < budget) {
1533                 napi_complete(napi);
1534                 vmxnet3_enable_all_intrs(rx_queue->adapter);
1535         }
1536         return rxd_done;
1537 }
1538
1539 /*
1540  * NAPI polling function for MSI-X mode with multiple Rx queues
1541  * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
1542  */
1543
1544 static int
1545 vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
1546 {
1547         struct vmxnet3_rx_queue *rq = container_of(napi,
1548                                                 struct vmxnet3_rx_queue, napi);
1549         struct vmxnet3_adapter *adapter = rq->adapter;
1550         int rxd_done;
1551
1552         /* When sharing interrupt with corresponding tx queue, process
1553          * tx completions in that queue as well
1554          */
1555         if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
1556                 struct vmxnet3_tx_queue *tq =
1557                                 &adapter->tx_queue[rq - adapter->rx_queue];
1558                 vmxnet3_tq_tx_complete(tq, adapter);
1559         }
1560
1561         rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
1562
1563         if (rxd_done < budget) {
1564                 napi_complete(napi);
1565                 vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
1566         }
1567         return rxd_done;
1568 }
1569
1570
1571 #ifdef CONFIG_PCI_MSI
1572
1573 /*
1574  * Handle completion interrupts on tx queues
1575  * Returns whether or not the intr is handled
1576  */
1577
1578 static irqreturn_t
1579 vmxnet3_msix_tx(int irq, void *data)
1580 {
1581         struct vmxnet3_tx_queue *tq = data;
1582         struct vmxnet3_adapter *adapter = tq->adapter;
1583
1584         if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1585                 vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
1586
1587         /* Handle the case where only one irq is allocate for all tx queues */
1588         if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1589                 int i;
1590                 for (i = 0; i < adapter->num_tx_queues; i++) {
1591                         struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
1592                         vmxnet3_tq_tx_complete(txq, adapter);
1593                 }
1594         } else {
1595                 vmxnet3_tq_tx_complete(tq, adapter);
1596         }
1597         vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
1598
1599         return IRQ_HANDLED;
1600 }
1601
1602
1603 /*
1604  * Handle completion interrupts on rx queues. Returns whether or not the
1605  * intr is handled
1606  */
1607
1608 static irqreturn_t
1609 vmxnet3_msix_rx(int irq, void *data)
1610 {
1611         struct vmxnet3_rx_queue *rq = data;
1612         struct vmxnet3_adapter *adapter = rq->adapter;
1613
1614         /* disable intr if needed */
1615         if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1616                 vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
1617         napi_schedule(&rq->napi);
1618
1619         return IRQ_HANDLED;
1620 }
1621
1622 /*
1623  *----------------------------------------------------------------------------
1624  *
1625  * vmxnet3_msix_event --
1626  *
1627  *    vmxnet3 msix event intr handler
1628  *
1629  * Result:
1630  *    whether or not the intr is handled
1631  *
1632  *----------------------------------------------------------------------------
1633  */
1634
1635 static irqreturn_t
1636 vmxnet3_msix_event(int irq, void *data)
1637 {
1638         struct net_device *dev = data;
1639         struct vmxnet3_adapter *adapter = netdev_priv(dev);
1640
1641         /* disable intr if needed */
1642         if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1643                 vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
1644
1645         if (adapter->shared->ecr)
1646                 vmxnet3_process_events(adapter);
1647
1648         vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
1649
1650         return IRQ_HANDLED;
1651 }
1652
1653 #endif /* CONFIG_PCI_MSI  */
1654
1655
1656 /* Interrupt handler for vmxnet3  */
1657 static irqreturn_t
1658 vmxnet3_intr(int irq, void *dev_id)
1659 {
1660         struct net_device *dev = dev_id;
1661         struct vmxnet3_adapter *adapter = netdev_priv(dev);
1662
1663         if (adapter->intr.type == VMXNET3_IT_INTX) {
1664                 u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
1665                 if (unlikely(icr == 0))
1666                         /* not ours */
1667                         return IRQ_NONE;
1668         }
1669
1670
1671         /* disable intr if needed */
1672         if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1673                 vmxnet3_disable_all_intrs(adapter);
1674
1675         napi_schedule(&adapter->rx_queue[0].napi);
1676
1677         return IRQ_HANDLED;
1678 }
1679
1680 #ifdef CONFIG_NET_POLL_CONTROLLER
1681
1682 /* netpoll callback. */
1683 static void
1684 vmxnet3_netpoll(struct net_device *netdev)
1685 {
1686         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1687
1688         if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1689                 vmxnet3_disable_all_intrs(adapter);
1690
1691         vmxnet3_do_poll(adapter, adapter->rx_queue[0].rx_ring[0].size);
1692         vmxnet3_enable_all_intrs(adapter);
1693
1694 }
1695 #endif  /* CONFIG_NET_POLL_CONTROLLER */
1696
1697 static int
1698 vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
1699 {
1700         struct vmxnet3_intr *intr = &adapter->intr;
1701         int err = 0, i;
1702         int vector = 0;
1703
1704 #ifdef CONFIG_PCI_MSI
1705         if (adapter->intr.type == VMXNET3_IT_MSIX) {
1706                 for (i = 0; i < adapter->num_tx_queues; i++) {
1707                         if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1708                                 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
1709                                         adapter->netdev->name, vector);
1710                                 err = request_irq(
1711                                               intr->msix_entries[vector].vector,
1712                                               vmxnet3_msix_tx, 0,
1713                                               adapter->tx_queue[i].name,
1714                                               &adapter->tx_queue[i]);
1715                         } else {
1716                                 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
1717                                         adapter->netdev->name, vector);
1718                         }
1719                         if (err) {
1720                                 dev_err(&adapter->netdev->dev,
1721                                         "Failed to request irq for MSIX, %s, "
1722                                         "error %d\n",
1723                                         adapter->tx_queue[i].name, err);
1724                                 return err;
1725                         }
1726
1727                         /* Handle the case where only 1 MSIx was allocated for
1728                          * all tx queues */
1729                         if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1730                                 for (; i < adapter->num_tx_queues; i++)
1731                                         adapter->tx_queue[i].comp_ring.intr_idx
1732                                                                 = vector;
1733                                 vector++;
1734                                 break;
1735                         } else {
1736                                 adapter->tx_queue[i].comp_ring.intr_idx
1737                                                                 = vector++;
1738                         }
1739                 }
1740                 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
1741                         vector = 0;
1742
1743                 for (i = 0; i < adapter->num_rx_queues; i++) {
1744                         if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
1745                                 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
1746                                         adapter->netdev->name, vector);
1747                         else
1748                                 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
1749                                         adapter->netdev->name, vector);
1750                         err = request_irq(intr->msix_entries[vector].vector,
1751                                           vmxnet3_msix_rx, 0,
1752                                           adapter->rx_queue[i].name,
1753                                           &(adapter->rx_queue[i]));
1754                         if (err) {
1755                                 printk(KERN_ERR "Failed to request irq for MSIX"
1756                                        ", %s, error %d\n",
1757                                        adapter->rx_queue[i].name, err);
1758                                 return err;
1759                         }
1760
1761                         adapter->rx_queue[i].comp_ring.intr_idx = vector++;
1762                 }
1763
1764                 sprintf(intr->event_msi_vector_name, "%s-event-%d",
1765                         adapter->netdev->name, vector);
1766                 err = request_irq(intr->msix_entries[vector].vector,
1767                                   vmxnet3_msix_event, 0,
1768                                   intr->event_msi_vector_name, adapter->netdev);
1769                 intr->event_intr_idx = vector;
1770
1771         } else if (intr->type == VMXNET3_IT_MSI) {
1772                 adapter->num_rx_queues = 1;
1773                 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
1774                                   adapter->netdev->name, adapter->netdev);
1775         } else {
1776 #endif
1777                 adapter->num_rx_queues = 1;
1778                 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
1779                                   IRQF_SHARED, adapter->netdev->name,
1780                                   adapter->netdev);
1781 #ifdef CONFIG_PCI_MSI
1782         }
1783 #endif
1784         intr->num_intrs = vector + 1;
1785         if (err) {
1786                 printk(KERN_ERR "Failed to request irq %s (intr type:%d), error"
1787                        ":%d\n", adapter->netdev->name, intr->type, err);
1788         } else {
1789                 /* Number of rx queues will not change after this */
1790                 for (i = 0; i < adapter->num_rx_queues; i++) {
1791                         struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
1792                         rq->qid = i;
1793                         rq->qid2 = i + adapter->num_rx_queues;
1794                 }
1795
1796
1797
1798                 /* init our intr settings */
1799                 for (i = 0; i < intr->num_intrs; i++)
1800                         intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
1801                 if (adapter->intr.type != VMXNET3_IT_MSIX) {
1802                         adapter->intr.event_intr_idx = 0;
1803                         for (i = 0; i < adapter->num_tx_queues; i++)
1804                                 adapter->tx_queue[i].comp_ring.intr_idx = 0;
1805                         adapter->rx_queue[0].comp_ring.intr_idx = 0;
1806                 }
1807
1808                 printk(KERN_INFO "%s: intr type %u, mode %u, %u vectors "
1809                        "allocated\n", adapter->netdev->name, intr->type,
1810                        intr->mask_mode, intr->num_intrs);
1811         }
1812
1813         return err;
1814 }
1815
1816
1817 static void
1818 vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
1819 {
1820         struct vmxnet3_intr *intr = &adapter->intr;
1821         BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
1822
1823         switch (intr->type) {
1824 #ifdef CONFIG_PCI_MSI
1825         case VMXNET3_IT_MSIX:
1826         {
1827                 int i, vector = 0;
1828
1829                 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1830                         for (i = 0; i < adapter->num_tx_queues; i++) {
1831                                 free_irq(intr->msix_entries[vector++].vector,
1832                                          &(adapter->tx_queue[i]));
1833                                 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
1834                                         break;
1835                         }
1836                 }
1837
1838                 for (i = 0; i < adapter->num_rx_queues; i++) {
1839                         free_irq(intr->msix_entries[vector++].vector,
1840                                  &(adapter->rx_queue[i]));
1841                 }
1842
1843                 free_irq(intr->msix_entries[vector].vector,
1844                          adapter->netdev);
1845                 BUG_ON(vector >= intr->num_intrs);
1846                 break;
1847         }
1848 #endif
1849         case VMXNET3_IT_MSI:
1850                 free_irq(adapter->pdev->irq, adapter->netdev);
1851                 break;
1852         case VMXNET3_IT_INTX:
1853                 free_irq(adapter->pdev->irq, adapter->netdev);
1854                 break;
1855         default:
1856                 BUG_ON(true);
1857         }
1858 }
1859
1860
1861 static void
1862 vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
1863 {
1864         u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1865         u16 vid;
1866
1867         /* allow untagged pkts */
1868         VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1869
1870         for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1871                 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1872 }
1873
1874
1875 static void
1876 vmxnet3_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1877 {
1878         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1879         u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1880         unsigned long flags;
1881
1882         VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1883         spin_lock_irqsave(&adapter->cmd_lock, flags);
1884         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1885                                VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1886         spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1887
1888         set_bit(vid, adapter->active_vlans);
1889 }
1890
1891
1892 static void
1893 vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1894 {
1895         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1896         u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1897         unsigned long flags;
1898
1899         VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
1900         spin_lock_irqsave(&adapter->cmd_lock, flags);
1901         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1902                                VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1903         spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1904
1905         clear_bit(vid, adapter->active_vlans);
1906 }
1907
1908
1909 static u8 *
1910 vmxnet3_copy_mc(struct net_device *netdev)
1911 {
1912         u8 *buf = NULL;
1913         u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
1914
1915         /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
1916         if (sz <= 0xffff) {
1917                 /* We may be called with BH disabled */
1918                 buf = kmalloc(sz, GFP_ATOMIC);
1919                 if (buf) {
1920                         struct netdev_hw_addr *ha;
1921                         int i = 0;
1922
1923                         netdev_for_each_mc_addr(ha, netdev)
1924                                 memcpy(buf + i++ * ETH_ALEN, ha->addr,
1925                                        ETH_ALEN);
1926                 }
1927         }
1928         return buf;
1929 }
1930
1931
1932 static void
1933 vmxnet3_set_mc(struct net_device *netdev)
1934 {
1935         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1936         unsigned long flags;
1937         struct Vmxnet3_RxFilterConf *rxConf =
1938                                         &adapter->shared->devRead.rxFilterConf;
1939         u8 *new_table = NULL;
1940         u32 new_mode = VMXNET3_RXM_UCAST;
1941
1942         if (netdev->flags & IFF_PROMISC) {
1943                 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1944                 memset(vfTable, 0, VMXNET3_VFT_SIZE * sizeof(*vfTable));
1945
1946                 new_mode |= VMXNET3_RXM_PROMISC;
1947         } else {
1948                 vmxnet3_restore_vlan(adapter);
1949         }
1950
1951         if (netdev->flags & IFF_BROADCAST)
1952                 new_mode |= VMXNET3_RXM_BCAST;
1953
1954         if (netdev->flags & IFF_ALLMULTI)
1955                 new_mode |= VMXNET3_RXM_ALL_MULTI;
1956         else
1957                 if (!netdev_mc_empty(netdev)) {
1958                         new_table = vmxnet3_copy_mc(netdev);
1959                         if (new_table) {
1960                                 new_mode |= VMXNET3_RXM_MCAST;
1961                                 rxConf->mfTableLen = cpu_to_le16(
1962                                         netdev_mc_count(netdev) * ETH_ALEN);
1963                                 rxConf->mfTablePA = cpu_to_le64(virt_to_phys(
1964                                                     new_table));
1965                         } else {
1966                                 printk(KERN_INFO "%s: failed to copy mcast list"
1967                                        ", setting ALL_MULTI\n", netdev->name);
1968                                 new_mode |= VMXNET3_RXM_ALL_MULTI;
1969                         }
1970                 }
1971
1972
1973         if (!(new_mode & VMXNET3_RXM_MCAST)) {
1974                 rxConf->mfTableLen = 0;
1975                 rxConf->mfTablePA = 0;
1976         }
1977
1978         spin_lock_irqsave(&adapter->cmd_lock, flags);
1979         if (new_mode != rxConf->rxMode) {
1980                 rxConf->rxMode = cpu_to_le32(new_mode);
1981                 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1982                                        VMXNET3_CMD_UPDATE_RX_MODE);
1983                 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1984                                        VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1985         }
1986
1987         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1988                                VMXNET3_CMD_UPDATE_MAC_FILTERS);
1989         spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1990
1991         kfree(new_table);
1992 }
1993
1994 void
1995 vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
1996 {
1997         int i;
1998
1999         for (i = 0; i < adapter->num_rx_queues; i++)
2000                 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2001 }
2002
2003
2004 /*
2005  *   Set up driver_shared based on settings in adapter.
2006  */
2007
2008 static void
2009 vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2010 {
2011         struct Vmxnet3_DriverShared *shared = adapter->shared;
2012         struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2013         struct Vmxnet3_TxQueueConf *tqc;
2014         struct Vmxnet3_RxQueueConf *rqc;
2015         int i;
2016
2017         memset(shared, 0, sizeof(*shared));
2018
2019         /* driver settings */
2020         shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2021         devRead->misc.driverInfo.version = cpu_to_le32(
2022                                                 VMXNET3_DRIVER_VERSION_NUM);
2023         devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2024                                 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2025         devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
2026         *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2027                                 *((u32 *)&devRead->misc.driverInfo.gos));
2028         devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2029         devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
2030
2031         devRead->misc.ddPA = cpu_to_le64(virt_to_phys(adapter));
2032         devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
2033
2034         /* set up feature flags */
2035         if (adapter->netdev->features & NETIF_F_RXCSUM)
2036                 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
2037
2038         if (adapter->netdev->features & NETIF_F_LRO) {
2039                 devRead->misc.uptFeatures |= UPT1_F_LRO;
2040                 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
2041         }
2042         if (adapter->netdev->features & NETIF_F_HW_VLAN_RX)
2043                 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
2044
2045         devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2046         devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2047         devRead->misc.queueDescLen = cpu_to_le32(
2048                 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2049                 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
2050
2051         /* tx queue settings */
2052         devRead->misc.numTxQueues =  adapter->num_tx_queues;
2053         for (i = 0; i < adapter->num_tx_queues; i++) {
2054                 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2055                 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2056                 tqc = &adapter->tqd_start[i].conf;
2057                 tqc->txRingBasePA   = cpu_to_le64(tq->tx_ring.basePA);
2058                 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2059                 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
2060                 tqc->ddPA           = cpu_to_le64(virt_to_phys(tq->buf_info));
2061                 tqc->txRingSize     = cpu_to_le32(tq->tx_ring.size);
2062                 tqc->dataRingSize   = cpu_to_le32(tq->data_ring.size);
2063                 tqc->compRingSize   = cpu_to_le32(tq->comp_ring.size);
2064                 tqc->ddLen          = cpu_to_le32(
2065                                         sizeof(struct vmxnet3_tx_buf_info) *
2066                                         tqc->txRingSize);
2067                 tqc->intrIdx        = tq->comp_ring.intr_idx;
2068         }
2069
2070         /* rx queue settings */
2071         devRead->misc.numRxQueues = adapter->num_rx_queues;
2072         for (i = 0; i < adapter->num_rx_queues; i++) {
2073                 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2074                 rqc = &adapter->rqd_start[i].conf;
2075                 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2076                 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2077                 rqc->compRingBasePA  = cpu_to_le64(rq->comp_ring.basePA);
2078                 rqc->ddPA            = cpu_to_le64(virt_to_phys(
2079                                                         rq->buf_info));
2080                 rqc->rxRingSize[0]   = cpu_to_le32(rq->rx_ring[0].size);
2081                 rqc->rxRingSize[1]   = cpu_to_le32(rq->rx_ring[1].size);
2082                 rqc->compRingSize    = cpu_to_le32(rq->comp_ring.size);
2083                 rqc->ddLen           = cpu_to_le32(
2084                                         sizeof(struct vmxnet3_rx_buf_info) *
2085                                         (rqc->rxRingSize[0] +
2086                                          rqc->rxRingSize[1]));
2087                 rqc->intrIdx         = rq->comp_ring.intr_idx;
2088         }
2089
2090 #ifdef VMXNET3_RSS
2091         memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2092
2093         if (adapter->rss) {
2094                 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
2095                 devRead->misc.uptFeatures |= UPT1_F_RSS;
2096                 devRead->misc.numRxQueues = adapter->num_rx_queues;
2097                 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2098                                     UPT1_RSS_HASH_TYPE_IPV4 |
2099                                     UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2100                                     UPT1_RSS_HASH_TYPE_IPV6;
2101                 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2102                 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2103                 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
2104                 get_random_bytes(&rssConf->hashKey[0], rssConf->hashKeySize);
2105                 for (i = 0; i < rssConf->indTableSize; i++)
2106                         rssConf->indTable[i] = i % adapter->num_rx_queues;
2107
2108                 devRead->rssConfDesc.confVer = 1;
2109                 devRead->rssConfDesc.confLen = sizeof(*rssConf);
2110                 devRead->rssConfDesc.confPA  = virt_to_phys(rssConf);
2111         }
2112
2113 #endif /* VMXNET3_RSS */
2114
2115         /* intr settings */
2116         devRead->intrConf.autoMask = adapter->intr.mask_mode ==
2117                                      VMXNET3_IMM_AUTO;
2118         devRead->intrConf.numIntrs = adapter->intr.num_intrs;
2119         for (i = 0; i < adapter->intr.num_intrs; i++)
2120                 devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
2121
2122         devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
2123         devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
2124
2125         /* rx filter settings */
2126         devRead->rxFilterConf.rxMode = 0;
2127         vmxnet3_restore_vlan(adapter);
2128         vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
2129
2130         /* the rest are already zeroed */
2131 }
2132
2133
2134 int
2135 vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
2136 {
2137         int err, i;
2138         u32 ret;
2139         unsigned long flags;
2140
2141         dev_dbg(&adapter->netdev->dev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
2142                 " ring sizes %u %u %u\n", adapter->netdev->name,
2143                 adapter->skb_buf_size, adapter->rx_buf_per_pkt,
2144                 adapter->tx_queue[0].tx_ring.size,
2145                 adapter->rx_queue[0].rx_ring[0].size,
2146                 adapter->rx_queue[0].rx_ring[1].size);
2147
2148         vmxnet3_tq_init_all(adapter);
2149         err = vmxnet3_rq_init_all(adapter);
2150         if (err) {
2151                 printk(KERN_ERR "Failed to init rx queue for %s: error %d\n",
2152                        adapter->netdev->name, err);
2153                 goto rq_err;
2154         }
2155
2156         err = vmxnet3_request_irqs(adapter);
2157         if (err) {
2158                 printk(KERN_ERR "Failed to setup irq for %s: error %d\n",
2159                        adapter->netdev->name, err);
2160                 goto irq_err;
2161         }
2162
2163         vmxnet3_setup_driver_shared(adapter);
2164
2165         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
2166                                adapter->shared_pa));
2167         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
2168                                adapter->shared_pa));
2169         spin_lock_irqsave(&adapter->cmd_lock, flags);
2170         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2171                                VMXNET3_CMD_ACTIVATE_DEV);
2172         ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
2173         spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2174
2175         if (ret != 0) {
2176                 printk(KERN_ERR "Failed to activate dev %s: error %u\n",
2177                        adapter->netdev->name, ret);
2178                 err = -EINVAL;
2179                 goto activate_err;
2180         }
2181
2182         for (i = 0; i < adapter->num_rx_queues; i++) {
2183                 VMXNET3_WRITE_BAR0_REG(adapter,
2184                                 VMXNET3_REG_RXPROD + i * VMXNET3_REG_ALIGN,
2185                                 adapter->rx_queue[i].rx_ring[0].next2fill);
2186                 VMXNET3_WRITE_BAR0_REG(adapter, (VMXNET3_REG_RXPROD2 +
2187                                 (i * VMXNET3_REG_ALIGN)),
2188                                 adapter->rx_queue[i].rx_ring[1].next2fill);
2189         }
2190
2191         /* Apply the rx filter settins last. */
2192         vmxnet3_set_mc(adapter->netdev);
2193
2194         /*
2195          * Check link state when first activating device. It will start the
2196          * tx queue if the link is up.
2197          */
2198         vmxnet3_check_link(adapter, true);
2199         for (i = 0; i < adapter->num_rx_queues; i++)
2200                 napi_enable(&adapter->rx_queue[i].napi);
2201         vmxnet3_enable_all_intrs(adapter);
2202         clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
2203         return 0;
2204
2205 activate_err:
2206         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
2207         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
2208         vmxnet3_free_irqs(adapter);
2209 irq_err:
2210 rq_err:
2211         /* free up buffers we allocated */
2212         vmxnet3_rq_cleanup_all(adapter);
2213         return err;
2214 }
2215
2216
2217 void
2218 vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
2219 {
2220         unsigned long flags;
2221         spin_lock_irqsave(&adapter->cmd_lock, flags);
2222         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
2223         spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2224 }
2225
2226
2227 int
2228 vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
2229 {
2230         int i;
2231         unsigned long flags;
2232         if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
2233                 return 0;
2234
2235
2236         spin_lock_irqsave(&adapter->cmd_lock, flags);
2237         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2238                                VMXNET3_CMD_QUIESCE_DEV);
2239         spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2240         vmxnet3_disable_all_intrs(adapter);
2241
2242         for (i = 0; i < adapter->num_rx_queues; i++)
2243                 napi_disable(&adapter->rx_queue[i].napi);
2244         netif_tx_disable(adapter->netdev);
2245         adapter->link_speed = 0;
2246         netif_carrier_off(adapter->netdev);
2247
2248         vmxnet3_tq_cleanup_all(adapter);
2249         vmxnet3_rq_cleanup_all(adapter);
2250         vmxnet3_free_irqs(adapter);
2251         return 0;
2252 }
2253
2254
2255 static void
2256 vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2257 {
2258         u32 tmp;
2259
2260         tmp = *(u32 *)mac;
2261         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
2262
2263         tmp = (mac[5] << 8) | mac[4];
2264         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
2265 }
2266
2267
2268 static int
2269 vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
2270 {
2271         struct sockaddr *addr = p;
2272         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2273
2274         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2275         vmxnet3_write_mac_addr(adapter, addr->sa_data);
2276
2277         return 0;
2278 }
2279
2280
2281 /* ==================== initialization and cleanup routines ============ */
2282
2283 static int
2284 vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
2285 {
2286         int err;
2287         unsigned long mmio_start, mmio_len;
2288         struct pci_dev *pdev = adapter->pdev;
2289
2290         err = pci_enable_device(pdev);
2291         if (err) {
2292                 printk(KERN_ERR "Failed to enable adapter %s: error %d\n",
2293                        pci_name(pdev), err);
2294                 return err;
2295         }
2296
2297         if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
2298                 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
2299                         printk(KERN_ERR "pci_set_consistent_dma_mask failed "
2300                                "for adapter %s\n", pci_name(pdev));
2301                         err = -EIO;
2302                         goto err_set_mask;
2303                 }
2304                 *dma64 = true;
2305         } else {
2306                 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
2307                         printk(KERN_ERR "pci_set_dma_mask failed for adapter "
2308                                "%s\n",  pci_name(pdev));
2309                         err = -EIO;
2310                         goto err_set_mask;
2311                 }
2312                 *dma64 = false;
2313         }
2314
2315         err = pci_request_selected_regions(pdev, (1 << 2) - 1,
2316                                            vmxnet3_driver_name);
2317         if (err) {
2318                 printk(KERN_ERR "Failed to request region for adapter %s: "
2319                        "error %d\n", pci_name(pdev), err);
2320                 goto err_set_mask;
2321         }
2322
2323         pci_set_master(pdev);
2324
2325         mmio_start = pci_resource_start(pdev, 0);
2326         mmio_len = pci_resource_len(pdev, 0);
2327         adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
2328         if (!adapter->hw_addr0) {
2329                 printk(KERN_ERR "Failed to map bar0 for adapter %s\n",
2330                        pci_name(pdev));
2331                 err = -EIO;
2332                 goto err_ioremap;
2333         }
2334
2335         mmio_start = pci_resource_start(pdev, 1);
2336         mmio_len = pci_resource_len(pdev, 1);
2337         adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
2338         if (!adapter->hw_addr1) {
2339                 printk(KERN_ERR "Failed to map bar1 for adapter %s\n",
2340                        pci_name(pdev));
2341                 err = -EIO;
2342                 goto err_bar1;
2343         }
2344         return 0;
2345
2346 err_bar1:
2347         iounmap(adapter->hw_addr0);
2348 err_ioremap:
2349         pci_release_selected_regions(pdev, (1 << 2) - 1);
2350 err_set_mask:
2351         pci_disable_device(pdev);
2352         return err;
2353 }
2354
2355
2356 static void
2357 vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
2358 {
2359         BUG_ON(!adapter->pdev);
2360
2361         iounmap(adapter->hw_addr0);
2362         iounmap(adapter->hw_addr1);
2363         pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
2364         pci_disable_device(adapter->pdev);
2365 }
2366
2367
2368 static void
2369 vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
2370 {
2371         size_t sz, i, ring0_size, ring1_size, comp_size;
2372         struct vmxnet3_rx_queue *rq = &adapter->rx_queue[0];
2373
2374
2375         if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
2376                                     VMXNET3_MAX_ETH_HDR_SIZE) {
2377                 adapter->skb_buf_size = adapter->netdev->mtu +
2378                                         VMXNET3_MAX_ETH_HDR_SIZE;
2379                 if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
2380                         adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
2381
2382                 adapter->rx_buf_per_pkt = 1;
2383         } else {
2384                 adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
2385                 sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
2386                                             VMXNET3_MAX_ETH_HDR_SIZE;
2387                 adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
2388         }
2389
2390         /*
2391          * for simplicity, force the ring0 size to be a multiple of
2392          * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
2393          */
2394         sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
2395         ring0_size = adapter->rx_queue[0].rx_ring[0].size;
2396         ring0_size = (ring0_size + sz - 1) / sz * sz;
2397         ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
2398                            sz * sz);
2399         ring1_size = adapter->rx_queue[0].rx_ring[1].size;
2400         comp_size = ring0_size + ring1_size;
2401
2402         for (i = 0; i < adapter->num_rx_queues; i++) {
2403                 rq = &adapter->rx_queue[i];
2404                 rq->rx_ring[0].size = ring0_size;
2405                 rq->rx_ring[1].size = ring1_size;
2406                 rq->comp_ring.size = comp_size;
2407         }
2408 }
2409
2410
2411 int
2412 vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
2413                       u32 rx_ring_size, u32 rx_ring2_size)
2414 {
2415         int err = 0, i;
2416
2417         for (i = 0; i < adapter->num_tx_queues; i++) {
2418                 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2419                 tq->tx_ring.size   = tx_ring_size;
2420                 tq->data_ring.size = tx_ring_size;
2421                 tq->comp_ring.size = tx_ring_size;
2422                 tq->shared = &adapter->tqd_start[i].ctrl;
2423                 tq->stopped = true;
2424                 tq->adapter = adapter;
2425                 tq->qid = i;
2426                 err = vmxnet3_tq_create(tq, adapter);
2427                 /*
2428                  * Too late to change num_tx_queues. We cannot do away with
2429                  * lesser number of queues than what we asked for
2430                  */
2431                 if (err)
2432                         goto queue_err;
2433         }
2434
2435         adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
2436         adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
2437         vmxnet3_adjust_rx_ring_size(adapter);
2438         for (i = 0; i < adapter->num_rx_queues; i++) {
2439                 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2440                 /* qid and qid2 for rx queues will be assigned later when num
2441                  * of rx queues is finalized after allocating intrs */
2442                 rq->shared = &adapter->rqd_start[i].ctrl;
2443                 rq->adapter = adapter;
2444                 err = vmxnet3_rq_create(rq, adapter);
2445                 if (err) {
2446                         if (i == 0) {
2447                                 printk(KERN_ERR "Could not allocate any rx"
2448                                        "queues. Aborting.\n");
2449                                 goto queue_err;
2450                         } else {
2451                                 printk(KERN_INFO "Number of rx queues changed "
2452                                        "to : %d.\n", i);
2453                                 adapter->num_rx_queues = i;
2454                                 err = 0;
2455                                 break;
2456                         }
2457                 }
2458         }
2459         return err;
2460 queue_err:
2461         vmxnet3_tq_destroy_all(adapter);
2462         return err;
2463 }
2464
2465 static int
2466 vmxnet3_open(struct net_device *netdev)
2467 {
2468         struct vmxnet3_adapter *adapter;
2469         int err, i;
2470
2471         adapter = netdev_priv(netdev);
2472
2473         for (i = 0; i < adapter->num_tx_queues; i++)
2474                 spin_lock_init(&adapter->tx_queue[i].tx_lock);
2475
2476         err = vmxnet3_create_queues(adapter, VMXNET3_DEF_TX_RING_SIZE,
2477                                     VMXNET3_DEF_RX_RING_SIZE,
2478                                     VMXNET3_DEF_RX_RING_SIZE);
2479         if (err)
2480                 goto queue_err;
2481
2482         err = vmxnet3_activate_dev(adapter);
2483         if (err)
2484                 goto activate_err;
2485
2486         return 0;
2487
2488 activate_err:
2489         vmxnet3_rq_destroy_all(adapter);
2490         vmxnet3_tq_destroy_all(adapter);
2491 queue_err:
2492         return err;
2493 }
2494
2495
2496 static int
2497 vmxnet3_close(struct net_device *netdev)
2498 {
2499         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2500
2501         /*
2502          * Reset_work may be in the middle of resetting the device, wait for its
2503          * completion.
2504          */
2505         while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2506                 msleep(1);
2507
2508         vmxnet3_quiesce_dev(adapter);
2509
2510         vmxnet3_rq_destroy_all(adapter);
2511         vmxnet3_tq_destroy_all(adapter);
2512
2513         clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2514
2515
2516         return 0;
2517 }
2518
2519
2520 void
2521 vmxnet3_force_close(struct vmxnet3_adapter *adapter)
2522 {
2523         int i;
2524
2525         /*
2526          * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
2527          * vmxnet3_close() will deadlock.
2528          */
2529         BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
2530
2531         /* we need to enable NAPI, otherwise dev_close will deadlock */
2532         for (i = 0; i < adapter->num_rx_queues; i++)
2533                 napi_enable(&adapter->rx_queue[i].napi);
2534         dev_close(adapter->netdev);
2535 }
2536
2537
2538 static int
2539 vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
2540 {
2541         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2542         int err = 0;
2543
2544         if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
2545                 return -EINVAL;
2546
2547         netdev->mtu = new_mtu;
2548
2549         /*
2550          * Reset_work may be in the middle of resetting the device, wait for its
2551          * completion.
2552          */
2553         while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2554                 msleep(1);
2555
2556         if (netif_running(netdev)) {
2557                 vmxnet3_quiesce_dev(adapter);
2558                 vmxnet3_reset_dev(adapter);
2559
2560                 /* we need to re-create the rx queue based on the new mtu */
2561                 vmxnet3_rq_destroy_all(adapter);
2562                 vmxnet3_adjust_rx_ring_size(adapter);
2563                 err = vmxnet3_rq_create_all(adapter);
2564                 if (err) {
2565                         printk(KERN_ERR "%s: failed to re-create rx queues,"
2566                                 " error %d. Closing it.\n", netdev->name, err);
2567                         goto out;
2568                 }
2569
2570                 err = vmxnet3_activate_dev(adapter);
2571                 if (err) {
2572                         printk(KERN_ERR "%s: failed to re-activate, error %d. "
2573                                 "Closing it\n", netdev->name, err);
2574                         goto out;
2575                 }
2576         }
2577
2578 out:
2579         clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2580         if (err)
2581                 vmxnet3_force_close(adapter);
2582
2583         return err;
2584 }
2585
2586
2587 static void
2588 vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
2589 {
2590         struct net_device *netdev = adapter->netdev;
2591
2592         netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
2593                 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX |
2594                 NETIF_F_HW_VLAN_RX | NETIF_F_TSO | NETIF_F_TSO6 |
2595                 NETIF_F_LRO;
2596         if (dma64)
2597                 netdev->features |= NETIF_F_HIGHDMA;
2598         netdev->vlan_features = netdev->hw_features &
2599                                 ~(NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
2600         netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_FILTER;
2601
2602         netdev_info(adapter->netdev,
2603                 "features: sg csum vlan jf tso tsoIPv6 lro%s\n",
2604                 dma64 ? " highDMA" : "");
2605 }
2606
2607
2608 static void
2609 vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2610 {
2611         u32 tmp;
2612
2613         tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
2614         *(u32 *)mac = tmp;
2615
2616         tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
2617         mac[4] = tmp & 0xff;
2618         mac[5] = (tmp >> 8) & 0xff;
2619 }
2620
2621 #ifdef CONFIG_PCI_MSI
2622
2623 /*
2624  * Enable MSIx vectors.
2625  * Returns :
2626  *      0 on successful enabling of required vectors,
2627  *      VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
2628  *       could be enabled.
2629  *      number of vectors which can be enabled otherwise (this number is smaller
2630  *       than VMXNET3_LINUX_MIN_MSIX_VECT)
2631  */
2632
2633 static int
2634 vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter,
2635                              int vectors)
2636 {
2637         int err = 0, vector_threshold;
2638         vector_threshold = VMXNET3_LINUX_MIN_MSIX_VECT;
2639
2640         while (vectors >= vector_threshold) {
2641                 err = pci_enable_msix(adapter->pdev, adapter->intr.msix_entries,
2642                                       vectors);
2643                 if (!err) {
2644                         adapter->intr.num_intrs = vectors;
2645                         return 0;
2646                 } else if (err < 0) {
2647                         printk(KERN_ERR "Failed to enable MSI-X for %s, error"
2648                                " %d\n", adapter->netdev->name, err);
2649                         vectors = 0;
2650                 } else if (err < vector_threshold) {
2651                         break;
2652                 } else {
2653                         /* If fails to enable required number of MSI-x vectors
2654                          * try enabling minimum number of vectors required.
2655                          */
2656                         vectors = vector_threshold;
2657                         printk(KERN_ERR "Failed to enable %d MSI-X for %s, try"
2658                                " %d instead\n", vectors, adapter->netdev->name,
2659                                vector_threshold);
2660                 }
2661         }
2662
2663         printk(KERN_INFO "Number of MSI-X interrupts which can be allocatedi"
2664                " are lower than min threshold required.\n");
2665         return err;
2666 }
2667
2668
2669 #endif /* CONFIG_PCI_MSI */
2670
2671 static void
2672 vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
2673 {
2674         u32 cfg;
2675         unsigned long flags;
2676
2677         /* intr settings */
2678         spin_lock_irqsave(&adapter->cmd_lock, flags);
2679         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2680                                VMXNET3_CMD_GET_CONF_INTR);
2681         cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
2682         spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2683         adapter->intr.type = cfg & 0x3;
2684         adapter->intr.mask_mode = (cfg >> 2) & 0x3;
2685
2686         if (adapter->intr.type == VMXNET3_IT_AUTO) {
2687                 adapter->intr.type = VMXNET3_IT_MSIX;
2688         }
2689
2690 #ifdef CONFIG_PCI_MSI
2691         if (adapter->intr.type == VMXNET3_IT_MSIX) {
2692                 int vector, err = 0;
2693
2694                 adapter->intr.num_intrs = (adapter->share_intr ==
2695                                            VMXNET3_INTR_TXSHARE) ? 1 :
2696                                            adapter->num_tx_queues;
2697                 adapter->intr.num_intrs += (adapter->share_intr ==
2698                                            VMXNET3_INTR_BUDDYSHARE) ? 0 :
2699                                            adapter->num_rx_queues;
2700                 adapter->intr.num_intrs += 1;           /* for link event */
2701
2702                 adapter->intr.num_intrs = (adapter->intr.num_intrs >
2703                                            VMXNET3_LINUX_MIN_MSIX_VECT
2704                                            ? adapter->intr.num_intrs :
2705                                            VMXNET3_LINUX_MIN_MSIX_VECT);
2706
2707                 for (vector = 0; vector < adapter->intr.num_intrs; vector++)
2708                         adapter->intr.msix_entries[vector].entry = vector;
2709
2710                 err = vmxnet3_acquire_msix_vectors(adapter,
2711                                                    adapter->intr.num_intrs);
2712                 /* If we cannot allocate one MSIx vector per queue
2713                  * then limit the number of rx queues to 1
2714                  */
2715                 if (err == VMXNET3_LINUX_MIN_MSIX_VECT) {
2716                         if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
2717                             || adapter->num_rx_queues != 1) {
2718                                 adapter->share_intr = VMXNET3_INTR_TXSHARE;
2719                                 printk(KERN_ERR "Number of rx queues : 1\n");
2720                                 adapter->num_rx_queues = 1;
2721                                 adapter->intr.num_intrs =
2722                                                 VMXNET3_LINUX_MIN_MSIX_VECT;
2723                         }
2724                         return;
2725                 }
2726                 if (!err)
2727                         return;
2728
2729                 /* If we cannot allocate MSIx vectors use only one rx queue */
2730                 printk(KERN_INFO "Failed to enable MSI-X for %s, error %d."
2731                        "#rx queues : 1, try MSI\n", adapter->netdev->name, err);
2732
2733                 adapter->intr.type = VMXNET3_IT_MSI;
2734         }
2735
2736         if (adapter->intr.type == VMXNET3_IT_MSI) {
2737                 int err;
2738                 err = pci_enable_msi(adapter->pdev);
2739                 if (!err) {
2740                         adapter->num_rx_queues = 1;
2741                         adapter->intr.num_intrs = 1;
2742                         return;
2743                 }
2744         }
2745 #endif /* CONFIG_PCI_MSI */
2746
2747         adapter->num_rx_queues = 1;
2748         printk(KERN_INFO "Using INTx interrupt, #Rx queues: 1.\n");
2749         adapter->intr.type = VMXNET3_IT_INTX;
2750
2751         /* INT-X related setting */
2752         adapter->intr.num_intrs = 1;
2753 }
2754
2755
2756 static void
2757 vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
2758 {
2759         if (adapter->intr.type == VMXNET3_IT_MSIX)
2760                 pci_disable_msix(adapter->pdev);
2761         else if (adapter->intr.type == VMXNET3_IT_MSI)
2762                 pci_disable_msi(adapter->pdev);
2763         else
2764                 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
2765 }
2766
2767
2768 static void
2769 vmxnet3_tx_timeout(struct net_device *netdev)
2770 {
2771         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2772         adapter->tx_timeout_count++;
2773
2774         printk(KERN_ERR "%s: tx hang\n", adapter->netdev->name);
2775         schedule_work(&adapter->work);
2776         netif_wake_queue(adapter->netdev);
2777 }
2778
2779
2780 static void
2781 vmxnet3_reset_work(struct work_struct *data)
2782 {
2783         struct vmxnet3_adapter *adapter;
2784
2785         adapter = container_of(data, struct vmxnet3_adapter, work);
2786
2787         /* if another thread is resetting the device, no need to proceed */
2788         if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2789                 return;
2790
2791         /* if the device is closed, we must leave it alone */
2792         rtnl_lock();
2793         if (netif_running(adapter->netdev)) {
2794                 printk(KERN_INFO "%s: resetting\n", adapter->netdev->name);
2795                 vmxnet3_quiesce_dev(adapter);
2796                 vmxnet3_reset_dev(adapter);
2797                 vmxnet3_activate_dev(adapter);
2798         } else {
2799                 printk(KERN_INFO "%s: already closed\n", adapter->netdev->name);
2800         }
2801         rtnl_unlock();
2802
2803         clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2804 }
2805
2806
2807 static int __devinit
2808 vmxnet3_probe_device(struct pci_dev *pdev,
2809                      const struct pci_device_id *id)
2810 {
2811         static const struct net_device_ops vmxnet3_netdev_ops = {
2812                 .ndo_open = vmxnet3_open,
2813                 .ndo_stop = vmxnet3_close,
2814                 .ndo_start_xmit = vmxnet3_xmit_frame,
2815                 .ndo_set_mac_address = vmxnet3_set_mac_addr,
2816                 .ndo_change_mtu = vmxnet3_change_mtu,
2817                 .ndo_set_features = vmxnet3_set_features,
2818                 .ndo_get_stats64 = vmxnet3_get_stats64,
2819                 .ndo_tx_timeout = vmxnet3_tx_timeout,
2820                 .ndo_set_multicast_list = vmxnet3_set_mc,
2821                 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
2822                 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
2823 #ifdef CONFIG_NET_POLL_CONTROLLER
2824                 .ndo_poll_controller = vmxnet3_netpoll,
2825 #endif
2826         };
2827         int err;
2828         bool dma64 = false; /* stupid gcc */
2829         u32 ver;
2830         struct net_device *netdev;
2831         struct vmxnet3_adapter *adapter;
2832         u8 mac[ETH_ALEN];
2833         int size;
2834         int num_tx_queues;
2835         int num_rx_queues;
2836
2837         if (!pci_msi_enabled())
2838                 enable_mq = 0;
2839
2840 #ifdef VMXNET3_RSS
2841         if (enable_mq)
2842                 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
2843                                     (int)num_online_cpus());
2844         else
2845 #endif
2846                 num_rx_queues = 1;
2847
2848         if (enable_mq)
2849                 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
2850                                     (int)num_online_cpus());
2851         else
2852                 num_tx_queues = 1;
2853
2854         netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
2855                                    max(num_tx_queues, num_rx_queues));
2856         printk(KERN_INFO "# of Tx queues : %d, # of Rx queues : %d\n",
2857                num_tx_queues, num_rx_queues);
2858
2859         if (!netdev) {
2860                 printk(KERN_ERR "Failed to alloc ethernet device for adapter "
2861                         "%s\n", pci_name(pdev));
2862                 return -ENOMEM;
2863         }
2864
2865         pci_set_drvdata(pdev, netdev);
2866         adapter = netdev_priv(netdev);
2867         adapter->netdev = netdev;
2868         adapter->pdev = pdev;
2869
2870         spin_lock_init(&adapter->cmd_lock);
2871         adapter->shared = pci_alloc_consistent(adapter->pdev,
2872                           sizeof(struct Vmxnet3_DriverShared),
2873                           &adapter->shared_pa);
2874         if (!adapter->shared) {
2875                 printk(KERN_ERR "Failed to allocate memory for %s\n",
2876                         pci_name(pdev));
2877                 err = -ENOMEM;
2878                 goto err_alloc_shared;
2879         }
2880
2881         adapter->num_rx_queues = num_rx_queues;
2882         adapter->num_tx_queues = num_tx_queues;
2883
2884         size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
2885         size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
2886         adapter->tqd_start = pci_alloc_consistent(adapter->pdev, size,
2887                              &adapter->queue_desc_pa);
2888
2889         if (!adapter->tqd_start) {
2890                 printk(KERN_ERR "Failed to allocate memory for %s\n",
2891                         pci_name(pdev));
2892                 err = -ENOMEM;
2893                 goto err_alloc_queue_desc;
2894         }
2895         adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
2896                                                         adapter->num_tx_queues);
2897
2898         adapter->pm_conf = kmalloc(sizeof(struct Vmxnet3_PMConf), GFP_KERNEL);
2899         if (adapter->pm_conf == NULL) {
2900                 printk(KERN_ERR "Failed to allocate memory for %s\n",
2901                         pci_name(pdev));
2902                 err = -ENOMEM;
2903                 goto err_alloc_pm;
2904         }
2905
2906 #ifdef VMXNET3_RSS
2907
2908         adapter->rss_conf = kmalloc(sizeof(struct UPT1_RSSConf), GFP_KERNEL);
2909         if (adapter->rss_conf == NULL) {
2910                 printk(KERN_ERR "Failed to allocate memory for %s\n",
2911                        pci_name(pdev));
2912                 err = -ENOMEM;
2913                 goto err_alloc_rss;
2914         }
2915 #endif /* VMXNET3_RSS */
2916
2917         err = vmxnet3_alloc_pci_resources(adapter, &dma64);
2918         if (err < 0)
2919                 goto err_alloc_pci;
2920
2921         ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
2922         if (ver & 1) {
2923                 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
2924         } else {
2925                 printk(KERN_ERR "Incompatible h/w version (0x%x) for adapter"
2926                        " %s\n", ver, pci_name(pdev));
2927                 err = -EBUSY;
2928                 goto err_ver;
2929         }
2930
2931         ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
2932         if (ver & 1) {
2933                 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
2934         } else {
2935                 printk(KERN_ERR "Incompatible upt version (0x%x) for "
2936                        "adapter %s\n", ver, pci_name(pdev));
2937                 err = -EBUSY;
2938                 goto err_ver;
2939         }
2940
2941         vmxnet3_declare_features(adapter, dma64);
2942
2943         adapter->dev_number = atomic_read(&devices_found);
2944
2945          adapter->share_intr = irq_share_mode;
2946         if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE &&
2947             adapter->num_tx_queues != adapter->num_rx_queues)
2948                 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
2949
2950         vmxnet3_alloc_intr_resources(adapter);
2951
2952 #ifdef VMXNET3_RSS
2953         if (adapter->num_rx_queues > 1 &&
2954             adapter->intr.type == VMXNET3_IT_MSIX) {
2955                 adapter->rss = true;
2956                 printk(KERN_INFO "RSS is enabled.\n");
2957         } else {
2958                 adapter->rss = false;
2959         }
2960 #endif
2961
2962         vmxnet3_read_mac_addr(adapter, mac);
2963         memcpy(netdev->dev_addr,  mac, netdev->addr_len);
2964
2965         netdev->netdev_ops = &vmxnet3_netdev_ops;
2966         vmxnet3_set_ethtool_ops(netdev);
2967         netdev->watchdog_timeo = 5 * HZ;
2968
2969         INIT_WORK(&adapter->work, vmxnet3_reset_work);
2970
2971         if (adapter->intr.type == VMXNET3_IT_MSIX) {
2972                 int i;
2973                 for (i = 0; i < adapter->num_rx_queues; i++) {
2974                         netif_napi_add(adapter->netdev,
2975                                        &adapter->rx_queue[i].napi,
2976                                        vmxnet3_poll_rx_only, 64);
2977                 }
2978         } else {
2979                 netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
2980                                vmxnet3_poll, 64);
2981         }
2982
2983         netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
2984         netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
2985
2986         SET_NETDEV_DEV(netdev, &pdev->dev);
2987         err = register_netdev(netdev);
2988
2989         if (err) {
2990                 printk(KERN_ERR "Failed to register adapter %s\n",
2991                         pci_name(pdev));
2992                 goto err_register;
2993         }
2994
2995         set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
2996         vmxnet3_check_link(adapter, false);
2997         atomic_inc(&devices_found);
2998         return 0;
2999
3000 err_register:
3001         vmxnet3_free_intr_resources(adapter);
3002 err_ver:
3003         vmxnet3_free_pci_resources(adapter);
3004 err_alloc_pci:
3005 #ifdef VMXNET3_RSS
3006         kfree(adapter->rss_conf);
3007 err_alloc_rss:
3008 #endif
3009         kfree(adapter->pm_conf);
3010 err_alloc_pm:
3011         pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3012                             adapter->queue_desc_pa);
3013 err_alloc_queue_desc:
3014         pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3015                             adapter->shared, adapter->shared_pa);
3016 err_alloc_shared:
3017         pci_set_drvdata(pdev, NULL);
3018         free_netdev(netdev);
3019         return err;
3020 }
3021
3022
3023 static void __devexit
3024 vmxnet3_remove_device(struct pci_dev *pdev)
3025 {
3026         struct net_device *netdev = pci_get_drvdata(pdev);
3027         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3028         int size = 0;
3029         int num_rx_queues;
3030
3031 #ifdef VMXNET3_RSS
3032         if (enable_mq)
3033                 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3034                                     (int)num_online_cpus());
3035         else
3036 #endif
3037                 num_rx_queues = 1;
3038
3039         cancel_work_sync(&adapter->work);
3040
3041         unregister_netdev(netdev);
3042
3043         vmxnet3_free_intr_resources(adapter);
3044         vmxnet3_free_pci_resources(adapter);
3045 #ifdef VMXNET3_RSS
3046         kfree(adapter->rss_conf);
3047 #endif
3048         kfree(adapter->pm_conf);
3049
3050         size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3051         size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
3052         pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3053                             adapter->queue_desc_pa);
3054         pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3055                             adapter->shared, adapter->shared_pa);
3056         free_netdev(netdev);
3057 }
3058
3059
3060 #ifdef CONFIG_PM
3061
3062 static int
3063 vmxnet3_suspend(struct device *device)
3064 {
3065         struct pci_dev *pdev = to_pci_dev(device);
3066         struct net_device *netdev = pci_get_drvdata(pdev);
3067         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3068         struct Vmxnet3_PMConf *pmConf;
3069         struct ethhdr *ehdr;
3070         struct arphdr *ahdr;
3071         u8 *arpreq;
3072         struct in_device *in_dev;
3073         struct in_ifaddr *ifa;
3074         unsigned long flags;
3075         int i = 0;
3076
3077         if (!netif_running(netdev))
3078                 return 0;
3079
3080         for (i = 0; i < adapter->num_rx_queues; i++)
3081                 napi_disable(&adapter->rx_queue[i].napi);
3082
3083         vmxnet3_disable_all_intrs(adapter);
3084         vmxnet3_free_irqs(adapter);
3085         vmxnet3_free_intr_resources(adapter);
3086
3087         netif_device_detach(netdev);
3088         netif_tx_stop_all_queues(netdev);
3089
3090         /* Create wake-up filters. */
3091         pmConf = adapter->pm_conf;
3092         memset(pmConf, 0, sizeof(*pmConf));
3093
3094         if (adapter->wol & WAKE_UCAST) {
3095                 pmConf->filters[i].patternSize = ETH_ALEN;
3096                 pmConf->filters[i].maskSize = 1;
3097                 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
3098                 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
3099
3100                 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
3101                 i++;
3102         }
3103
3104         if (adapter->wol & WAKE_ARP) {
3105                 in_dev = in_dev_get(netdev);
3106                 if (!in_dev)
3107                         goto skip_arp;
3108
3109                 ifa = (struct in_ifaddr *)in_dev->ifa_list;
3110                 if (!ifa)
3111                         goto skip_arp;
3112
3113                 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
3114                         sizeof(struct arphdr) +         /* ARP header */
3115                         2 * ETH_ALEN +          /* 2 Ethernet addresses*/
3116                         2 * sizeof(u32);        /*2 IPv4 addresses */
3117                 pmConf->filters[i].maskSize =
3118                         (pmConf->filters[i].patternSize - 1) / 8 + 1;
3119
3120                 /* ETH_P_ARP in Ethernet header. */
3121                 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
3122                 ehdr->h_proto = htons(ETH_P_ARP);
3123
3124                 /* ARPOP_REQUEST in ARP header. */
3125                 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
3126                 ahdr->ar_op = htons(ARPOP_REQUEST);
3127                 arpreq = (u8 *)(ahdr + 1);
3128
3129                 /* The Unicast IPv4 address in 'tip' field. */
3130                 arpreq += 2 * ETH_ALEN + sizeof(u32);
3131                 *(u32 *)arpreq = ifa->ifa_address;
3132
3133                 /* The mask for the relevant bits. */
3134                 pmConf->filters[i].mask[0] = 0x00;
3135                 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
3136                 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
3137                 pmConf->filters[i].mask[3] = 0x00;
3138                 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
3139                 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
3140                 in_dev_put(in_dev);
3141
3142                 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
3143                 i++;
3144         }
3145
3146 skip_arp:
3147         if (adapter->wol & WAKE_MAGIC)
3148                 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
3149
3150         pmConf->numFilters = i;
3151
3152         adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3153         adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3154                                                                   *pmConf));
3155         adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3156                                                                  pmConf));
3157
3158         spin_lock_irqsave(&adapter->cmd_lock, flags);
3159         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3160                                VMXNET3_CMD_UPDATE_PMCFG);
3161         spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3162
3163         pci_save_state(pdev);
3164         pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
3165                         adapter->wol);
3166         pci_disable_device(pdev);
3167         pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
3168
3169         return 0;
3170 }
3171
3172
3173 static int
3174 vmxnet3_resume(struct device *device)
3175 {
3176         int err, i = 0;
3177         unsigned long flags;
3178         struct pci_dev *pdev = to_pci_dev(device);
3179         struct net_device *netdev = pci_get_drvdata(pdev);
3180         struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3181         struct Vmxnet3_PMConf *pmConf;
3182
3183         if (!netif_running(netdev))
3184                 return 0;
3185
3186         /* Destroy wake-up filters. */
3187         pmConf = adapter->pm_conf;
3188         memset(pmConf, 0, sizeof(*pmConf));
3189
3190         adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3191         adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3192                                                                   *pmConf));
3193         adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3194                                                                  pmConf));
3195
3196         netif_device_attach(netdev);
3197         pci_set_power_state(pdev, PCI_D0);
3198         pci_restore_state(pdev);
3199         err = pci_enable_device_mem(pdev);
3200         if (err != 0)
3201                 return err;
3202
3203         pci_enable_wake(pdev, PCI_D0, 0);
3204
3205         spin_lock_irqsave(&adapter->cmd_lock, flags);
3206         VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3207                                VMXNET3_CMD_UPDATE_PMCFG);
3208         spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3209         vmxnet3_alloc_intr_resources(adapter);
3210         vmxnet3_request_irqs(adapter);
3211         for (i = 0; i < adapter->num_rx_queues; i++)
3212                 napi_enable(&adapter->rx_queue[i].napi);
3213         vmxnet3_enable_all_intrs(adapter);
3214
3215         return 0;
3216 }
3217
3218 static const struct dev_pm_ops vmxnet3_pm_ops = {
3219         .suspend = vmxnet3_suspend,
3220         .resume = vmxnet3_resume,
3221 };
3222 #endif
3223
3224 static struct pci_driver vmxnet3_driver = {
3225         .name           = vmxnet3_driver_name,
3226         .id_table       = vmxnet3_pciid_table,
3227         .probe          = vmxnet3_probe_device,
3228         .remove         = __devexit_p(vmxnet3_remove_device),
3229 #ifdef CONFIG_PM
3230         .driver.pm      = &vmxnet3_pm_ops,
3231 #endif
3232 };
3233
3234
3235 static int __init
3236 vmxnet3_init_module(void)
3237 {
3238         printk(KERN_INFO "%s - version %s\n", VMXNET3_DRIVER_DESC,
3239                 VMXNET3_DRIVER_VERSION_REPORT);
3240         return pci_register_driver(&vmxnet3_driver);
3241 }
3242
3243 module_init(vmxnet3_init_module);
3244
3245
3246 static void
3247 vmxnet3_exit_module(void)
3248 {
3249         pci_unregister_driver(&vmxnet3_driver);
3250 }
3251
3252 module_exit(vmxnet3_exit_module);
3253
3254 MODULE_AUTHOR("VMware, Inc.");
3255 MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
3256 MODULE_LICENSE("GPL v2");
3257 MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);