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