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