Merge remote-tracking branch 'cleancache/linux-next'
[linux-2.6/next.git] / drivers / net / vmxnet3 / vmxnet3_drv.c
blob7a494f79c88f0ee6d276df87f234af3ba642a212
1 /*
2 * Linux driver for VMware's vmxnet3 ethernet NIC.
4 * Copyright (C) 2008-2009, VMware, Inc. All Rights Reserved.
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.
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.
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.
20 * The full GNU General Public License is included in this distribution in
21 * the file called "COPYING".
23 * Maintained by: Shreyas Bhatewara <pv-drivers@vmware.com>
27 #include <net/ip6_checksum.h>
29 #include "vmxnet3_int.h"
31 char vmxnet3_driver_name[] = "vmxnet3";
32 #define VMXNET3_DRIVER_DESC "VMware vmxnet3 virtual NIC driver"
35 * PCI Device ID Table
36 * Last entry must be all 0s
38 static DEFINE_PCI_DEVICE_TABLE(vmxnet3_pciid_table) = {
39 {PCI_VDEVICE(VMWARE, PCI_DEVICE_ID_VMWARE_VMXNET3)},
40 {0}
43 MODULE_DEVICE_TABLE(pci, vmxnet3_pciid_table);
45 static atomic_t devices_found;
47 #define VMXNET3_MAX_DEVICES 10
48 static int enable_mq = 1;
49 static int irq_share_mode;
51 static void
52 vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac);
55 * Enable/Disable the given intr
57 static void
58 vmxnet3_enable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
60 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 0);
64 static void
65 vmxnet3_disable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
67 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 1);
72 * Enable/Disable all intrs used by the device
74 static void
75 vmxnet3_enable_all_intrs(struct vmxnet3_adapter *adapter)
77 int i;
79 for (i = 0; i < adapter->intr.num_intrs; i++)
80 vmxnet3_enable_intr(adapter, i);
81 adapter->shared->devRead.intrConf.intrCtrl &=
82 cpu_to_le32(~VMXNET3_IC_DISABLE_ALL);
86 static void
87 vmxnet3_disable_all_intrs(struct vmxnet3_adapter *adapter)
89 int i;
91 adapter->shared->devRead.intrConf.intrCtrl |=
92 cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
93 for (i = 0; i < adapter->intr.num_intrs; i++)
94 vmxnet3_disable_intr(adapter, i);
98 static void
99 vmxnet3_ack_events(struct vmxnet3_adapter *adapter, u32 events)
101 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_ECR, events);
105 static bool
106 vmxnet3_tq_stopped(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
108 return tq->stopped;
112 static void
113 vmxnet3_tq_start(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
115 tq->stopped = false;
116 netif_start_subqueue(adapter->netdev, tq - adapter->tx_queue);
120 static void
121 vmxnet3_tq_wake(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
123 tq->stopped = false;
124 netif_wake_subqueue(adapter->netdev, (tq - adapter->tx_queue));
128 static void
129 vmxnet3_tq_stop(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
131 tq->stopped = true;
132 tq->num_stop++;
133 netif_stop_subqueue(adapter->netdev, (tq - adapter->tx_queue));
138 * Check the link state. This may start or stop the tx queue.
140 static void
141 vmxnet3_check_link(struct vmxnet3_adapter *adapter, bool affectTxQueue)
143 u32 ret;
144 int i;
145 unsigned long flags;
147 spin_lock_irqsave(&adapter->cmd_lock, flags);
148 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
149 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
150 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
152 adapter->link_speed = ret >> 16;
153 if (ret & 1) { /* Link is up. */
154 printk(KERN_INFO "%s: NIC Link is Up %d Mbps\n",
155 adapter->netdev->name, adapter->link_speed);
156 if (!netif_carrier_ok(adapter->netdev))
157 netif_carrier_on(adapter->netdev);
159 if (affectTxQueue) {
160 for (i = 0; i < adapter->num_tx_queues; i++)
161 vmxnet3_tq_start(&adapter->tx_queue[i],
162 adapter);
164 } else {
165 printk(KERN_INFO "%s: NIC Link is Down\n",
166 adapter->netdev->name);
167 if (netif_carrier_ok(adapter->netdev))
168 netif_carrier_off(adapter->netdev);
170 if (affectTxQueue) {
171 for (i = 0; i < adapter->num_tx_queues; i++)
172 vmxnet3_tq_stop(&adapter->tx_queue[i], adapter);
177 static void
178 vmxnet3_process_events(struct vmxnet3_adapter *adapter)
180 int i;
181 u32 events = le32_to_cpu(adapter->shared->ecr);
182 if (!events)
183 return;
185 vmxnet3_ack_events(adapter, events);
187 /* Check if link state has changed */
188 if (events & VMXNET3_ECR_LINK)
189 vmxnet3_check_link(adapter, true);
191 /* Check if there is an error on xmit/recv queues */
192 if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
193 spin_lock(&adapter->cmd_lock);
194 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
195 VMXNET3_CMD_GET_QUEUE_STATUS);
196 spin_unlock(&adapter->cmd_lock);
198 for (i = 0; i < adapter->num_tx_queues; i++)
199 if (adapter->tqd_start[i].status.stopped)
200 dev_err(&adapter->netdev->dev,
201 "%s: tq[%d] error 0x%x\n",
202 adapter->netdev->name, i, le32_to_cpu(
203 adapter->tqd_start[i].status.error));
204 for (i = 0; i < adapter->num_rx_queues; i++)
205 if (adapter->rqd_start[i].status.stopped)
206 dev_err(&adapter->netdev->dev,
207 "%s: rq[%d] error 0x%x\n",
208 adapter->netdev->name, i,
209 adapter->rqd_start[i].status.error);
211 schedule_work(&adapter->work);
215 #ifdef __BIG_ENDIAN_BITFIELD
217 * The device expects the bitfields in shared structures to be written in
218 * little endian. When CPU is big endian, the following routines are used to
219 * correctly read and write into ABI.
220 * The general technique used here is : double word bitfields are defined in
221 * opposite order for big endian architecture. Then before reading them in
222 * driver the complete double word is translated using le32_to_cpu. Similarly
223 * After the driver writes into bitfields, cpu_to_le32 is used to translate the
224 * double words into required format.
225 * In order to avoid touching bits in shared structure more than once, temporary
226 * descriptors are used. These are passed as srcDesc to following functions.
228 static void vmxnet3_RxDescToCPU(const struct Vmxnet3_RxDesc *srcDesc,
229 struct Vmxnet3_RxDesc *dstDesc)
231 u32 *src = (u32 *)srcDesc + 2;
232 u32 *dst = (u32 *)dstDesc + 2;
233 dstDesc->addr = le64_to_cpu(srcDesc->addr);
234 *dst = le32_to_cpu(*src);
235 dstDesc->ext1 = le32_to_cpu(srcDesc->ext1);
238 static void vmxnet3_TxDescToLe(const struct Vmxnet3_TxDesc *srcDesc,
239 struct Vmxnet3_TxDesc *dstDesc)
241 int i;
242 u32 *src = (u32 *)(srcDesc + 1);
243 u32 *dst = (u32 *)(dstDesc + 1);
245 /* Working backwards so that the gen bit is set at the end. */
246 for (i = 2; i > 0; i--) {
247 src--;
248 dst--;
249 *dst = cpu_to_le32(*src);
254 static void vmxnet3_RxCompToCPU(const struct Vmxnet3_RxCompDesc *srcDesc,
255 struct Vmxnet3_RxCompDesc *dstDesc)
257 int i = 0;
258 u32 *src = (u32 *)srcDesc;
259 u32 *dst = (u32 *)dstDesc;
260 for (i = 0; i < sizeof(struct Vmxnet3_RxCompDesc) / sizeof(u32); i++) {
261 *dst = le32_to_cpu(*src);
262 src++;
263 dst++;
268 /* Used to read bitfield values from double words. */
269 static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
271 u32 temp = le32_to_cpu(*bitfield);
272 u32 mask = ((1 << size) - 1) << pos;
273 temp &= mask;
274 temp >>= pos;
275 return temp;
280 #endif /* __BIG_ENDIAN_BITFIELD */
282 #ifdef __BIG_ENDIAN_BITFIELD
284 # define VMXNET3_TXDESC_GET_GEN(txdesc) get_bitfield32(((const __le32 *) \
285 txdesc) + VMXNET3_TXD_GEN_DWORD_SHIFT, \
286 VMXNET3_TXD_GEN_SHIFT, VMXNET3_TXD_GEN_SIZE)
287 # define VMXNET3_TXDESC_GET_EOP(txdesc) get_bitfield32(((const __le32 *) \
288 txdesc) + VMXNET3_TXD_EOP_DWORD_SHIFT, \
289 VMXNET3_TXD_EOP_SHIFT, VMXNET3_TXD_EOP_SIZE)
290 # define VMXNET3_TCD_GET_GEN(tcd) get_bitfield32(((const __le32 *)tcd) + \
291 VMXNET3_TCD_GEN_DWORD_SHIFT, VMXNET3_TCD_GEN_SHIFT, \
292 VMXNET3_TCD_GEN_SIZE)
293 # define VMXNET3_TCD_GET_TXIDX(tcd) get_bitfield32((const __le32 *)tcd, \
294 VMXNET3_TCD_TXIDX_SHIFT, VMXNET3_TCD_TXIDX_SIZE)
295 # define vmxnet3_getRxComp(dstrcd, rcd, tmp) do { \
296 (dstrcd) = (tmp); \
297 vmxnet3_RxCompToCPU((rcd), (tmp)); \
298 } while (0)
299 # define vmxnet3_getRxDesc(dstrxd, rxd, tmp) do { \
300 (dstrxd) = (tmp); \
301 vmxnet3_RxDescToCPU((rxd), (tmp)); \
302 } while (0)
304 #else
306 # define VMXNET3_TXDESC_GET_GEN(txdesc) ((txdesc)->gen)
307 # define VMXNET3_TXDESC_GET_EOP(txdesc) ((txdesc)->eop)
308 # define VMXNET3_TCD_GET_GEN(tcd) ((tcd)->gen)
309 # define VMXNET3_TCD_GET_TXIDX(tcd) ((tcd)->txdIdx)
310 # define vmxnet3_getRxComp(dstrcd, rcd, tmp) (dstrcd) = (rcd)
311 # define vmxnet3_getRxDesc(dstrxd, rxd, tmp) (dstrxd) = (rxd)
313 #endif /* __BIG_ENDIAN_BITFIELD */
316 static void
317 vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
318 struct pci_dev *pdev)
320 if (tbi->map_type == VMXNET3_MAP_SINGLE)
321 pci_unmap_single(pdev, tbi->dma_addr, tbi->len,
322 PCI_DMA_TODEVICE);
323 else if (tbi->map_type == VMXNET3_MAP_PAGE)
324 pci_unmap_page(pdev, tbi->dma_addr, tbi->len,
325 PCI_DMA_TODEVICE);
326 else
327 BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
329 tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
333 static int
334 vmxnet3_unmap_pkt(u32 eop_idx, struct vmxnet3_tx_queue *tq,
335 struct pci_dev *pdev, struct vmxnet3_adapter *adapter)
337 struct sk_buff *skb;
338 int entries = 0;
340 /* no out of order completion */
341 BUG_ON(tq->buf_info[eop_idx].sop_idx != tq->tx_ring.next2comp);
342 BUG_ON(VMXNET3_TXDESC_GET_EOP(&(tq->tx_ring.base[eop_idx].txd)) != 1);
344 skb = tq->buf_info[eop_idx].skb;
345 BUG_ON(skb == NULL);
346 tq->buf_info[eop_idx].skb = NULL;
348 VMXNET3_INC_RING_IDX_ONLY(eop_idx, tq->tx_ring.size);
350 while (tq->tx_ring.next2comp != eop_idx) {
351 vmxnet3_unmap_tx_buf(tq->buf_info + tq->tx_ring.next2comp,
352 pdev);
354 /* update next2comp w/o tx_lock. Since we are marking more,
355 * instead of less, tx ring entries avail, the worst case is
356 * that the tx routine incorrectly re-queues a pkt due to
357 * insufficient tx ring entries.
359 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
360 entries++;
363 dev_kfree_skb_any(skb);
364 return entries;
368 static int
369 vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue *tq,
370 struct vmxnet3_adapter *adapter)
372 int completed = 0;
373 union Vmxnet3_GenericDesc *gdesc;
375 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
376 while (VMXNET3_TCD_GET_GEN(&gdesc->tcd) == tq->comp_ring.gen) {
377 completed += vmxnet3_unmap_pkt(VMXNET3_TCD_GET_TXIDX(
378 &gdesc->tcd), tq, adapter->pdev,
379 adapter);
381 vmxnet3_comp_ring_adv_next2proc(&tq->comp_ring);
382 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
385 if (completed) {
386 spin_lock(&tq->tx_lock);
387 if (unlikely(vmxnet3_tq_stopped(tq, adapter) &&
388 vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) >
389 VMXNET3_WAKE_QUEUE_THRESHOLD(tq) &&
390 netif_carrier_ok(adapter->netdev))) {
391 vmxnet3_tq_wake(tq, adapter);
393 spin_unlock(&tq->tx_lock);
395 return completed;
399 static void
400 vmxnet3_tq_cleanup(struct vmxnet3_tx_queue *tq,
401 struct vmxnet3_adapter *adapter)
403 int i;
405 while (tq->tx_ring.next2comp != tq->tx_ring.next2fill) {
406 struct vmxnet3_tx_buf_info *tbi;
407 union Vmxnet3_GenericDesc *gdesc;
409 tbi = tq->buf_info + tq->tx_ring.next2comp;
410 gdesc = tq->tx_ring.base + tq->tx_ring.next2comp;
412 vmxnet3_unmap_tx_buf(tbi, adapter->pdev);
413 if (tbi->skb) {
414 dev_kfree_skb_any(tbi->skb);
415 tbi->skb = NULL;
417 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
420 /* sanity check, verify all buffers are indeed unmapped and freed */
421 for (i = 0; i < tq->tx_ring.size; i++) {
422 BUG_ON(tq->buf_info[i].skb != NULL ||
423 tq->buf_info[i].map_type != VMXNET3_MAP_NONE);
426 tq->tx_ring.gen = VMXNET3_INIT_GEN;
427 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
429 tq->comp_ring.gen = VMXNET3_INIT_GEN;
430 tq->comp_ring.next2proc = 0;
434 static void
435 vmxnet3_tq_destroy(struct vmxnet3_tx_queue *tq,
436 struct vmxnet3_adapter *adapter)
438 if (tq->tx_ring.base) {
439 pci_free_consistent(adapter->pdev, tq->tx_ring.size *
440 sizeof(struct Vmxnet3_TxDesc),
441 tq->tx_ring.base, tq->tx_ring.basePA);
442 tq->tx_ring.base = NULL;
444 if (tq->data_ring.base) {
445 pci_free_consistent(adapter->pdev, tq->data_ring.size *
446 sizeof(struct Vmxnet3_TxDataDesc),
447 tq->data_ring.base, tq->data_ring.basePA);
448 tq->data_ring.base = NULL;
450 if (tq->comp_ring.base) {
451 pci_free_consistent(adapter->pdev, tq->comp_ring.size *
452 sizeof(struct Vmxnet3_TxCompDesc),
453 tq->comp_ring.base, tq->comp_ring.basePA);
454 tq->comp_ring.base = NULL;
456 kfree(tq->buf_info);
457 tq->buf_info = NULL;
461 /* Destroy all tx queues */
462 void
463 vmxnet3_tq_destroy_all(struct vmxnet3_adapter *adapter)
465 int i;
467 for (i = 0; i < adapter->num_tx_queues; i++)
468 vmxnet3_tq_destroy(&adapter->tx_queue[i], adapter);
472 static void
473 vmxnet3_tq_init(struct vmxnet3_tx_queue *tq,
474 struct vmxnet3_adapter *adapter)
476 int i;
478 /* reset the tx ring contents to 0 and reset the tx ring states */
479 memset(tq->tx_ring.base, 0, tq->tx_ring.size *
480 sizeof(struct Vmxnet3_TxDesc));
481 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
482 tq->tx_ring.gen = VMXNET3_INIT_GEN;
484 memset(tq->data_ring.base, 0, tq->data_ring.size *
485 sizeof(struct Vmxnet3_TxDataDesc));
487 /* reset the tx comp ring contents to 0 and reset comp ring states */
488 memset(tq->comp_ring.base, 0, tq->comp_ring.size *
489 sizeof(struct Vmxnet3_TxCompDesc));
490 tq->comp_ring.next2proc = 0;
491 tq->comp_ring.gen = VMXNET3_INIT_GEN;
493 /* reset the bookkeeping data */
494 memset(tq->buf_info, 0, sizeof(tq->buf_info[0]) * tq->tx_ring.size);
495 for (i = 0; i < tq->tx_ring.size; i++)
496 tq->buf_info[i].map_type = VMXNET3_MAP_NONE;
498 /* stats are not reset */
502 static int
503 vmxnet3_tq_create(struct vmxnet3_tx_queue *tq,
504 struct vmxnet3_adapter *adapter)
506 BUG_ON(tq->tx_ring.base || tq->data_ring.base ||
507 tq->comp_ring.base || tq->buf_info);
509 tq->tx_ring.base = pci_alloc_consistent(adapter->pdev, tq->tx_ring.size
510 * sizeof(struct Vmxnet3_TxDesc),
511 &tq->tx_ring.basePA);
512 if (!tq->tx_ring.base) {
513 printk(KERN_ERR "%s: failed to allocate tx ring\n",
514 adapter->netdev->name);
515 goto err;
518 tq->data_ring.base = pci_alloc_consistent(adapter->pdev,
519 tq->data_ring.size *
520 sizeof(struct Vmxnet3_TxDataDesc),
521 &tq->data_ring.basePA);
522 if (!tq->data_ring.base) {
523 printk(KERN_ERR "%s: failed to allocate data ring\n",
524 adapter->netdev->name);
525 goto err;
528 tq->comp_ring.base = pci_alloc_consistent(adapter->pdev,
529 tq->comp_ring.size *
530 sizeof(struct Vmxnet3_TxCompDesc),
531 &tq->comp_ring.basePA);
532 if (!tq->comp_ring.base) {
533 printk(KERN_ERR "%s: failed to allocate tx comp ring\n",
534 adapter->netdev->name);
535 goto err;
538 tq->buf_info = kcalloc(tq->tx_ring.size, sizeof(tq->buf_info[0]),
539 GFP_KERNEL);
540 if (!tq->buf_info) {
541 printk(KERN_ERR "%s: failed to allocate tx bufinfo\n",
542 adapter->netdev->name);
543 goto err;
546 return 0;
548 err:
549 vmxnet3_tq_destroy(tq, adapter);
550 return -ENOMEM;
553 static void
554 vmxnet3_tq_cleanup_all(struct vmxnet3_adapter *adapter)
556 int i;
558 for (i = 0; i < adapter->num_tx_queues; i++)
559 vmxnet3_tq_cleanup(&adapter->tx_queue[i], adapter);
563 * starting from ring->next2fill, allocate rx buffers for the given ring
564 * of the rx queue and update the rx desc. stop after @num_to_alloc buffers
565 * are allocated or allocation fails
568 static int
569 vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
570 int num_to_alloc, struct vmxnet3_adapter *adapter)
572 int num_allocated = 0;
573 struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
574 struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
575 u32 val;
577 while (num_allocated < num_to_alloc) {
578 struct vmxnet3_rx_buf_info *rbi;
579 union Vmxnet3_GenericDesc *gd;
581 rbi = rbi_base + ring->next2fill;
582 gd = ring->base + ring->next2fill;
584 if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
585 if (rbi->skb == NULL) {
586 rbi->skb = dev_alloc_skb(rbi->len +
587 NET_IP_ALIGN);
588 if (unlikely(rbi->skb == NULL)) {
589 rq->stats.rx_buf_alloc_failure++;
590 break;
592 rbi->skb->dev = adapter->netdev;
594 skb_reserve(rbi->skb, NET_IP_ALIGN);
595 rbi->dma_addr = pci_map_single(adapter->pdev,
596 rbi->skb->data, rbi->len,
597 PCI_DMA_FROMDEVICE);
598 } else {
599 /* rx buffer skipped by the device */
601 val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
602 } else {
603 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
604 rbi->len != PAGE_SIZE);
606 if (rbi->page == NULL) {
607 rbi->page = alloc_page(GFP_ATOMIC);
608 if (unlikely(rbi->page == NULL)) {
609 rq->stats.rx_buf_alloc_failure++;
610 break;
612 rbi->dma_addr = pci_map_page(adapter->pdev,
613 rbi->page, 0, PAGE_SIZE,
614 PCI_DMA_FROMDEVICE);
615 } else {
616 /* rx buffers skipped by the device */
618 val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
621 BUG_ON(rbi->dma_addr == 0);
622 gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
623 gd->dword[2] = cpu_to_le32((ring->gen << VMXNET3_RXD_GEN_SHIFT)
624 | val | rbi->len);
626 num_allocated++;
627 vmxnet3_cmd_ring_adv_next2fill(ring);
629 rq->uncommitted[ring_idx] += num_allocated;
631 dev_dbg(&adapter->netdev->dev,
632 "alloc_rx_buf: %d allocated, next2fill %u, next2comp "
633 "%u, uncommited %u\n", num_allocated, ring->next2fill,
634 ring->next2comp, rq->uncommitted[ring_idx]);
636 /* so that the device can distinguish a full ring and an empty ring */
637 BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
639 return num_allocated;
643 static void
644 vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
645 struct vmxnet3_rx_buf_info *rbi)
647 struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
648 skb_shinfo(skb)->nr_frags;
650 BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
652 frag->page = rbi->page;
653 frag->page_offset = 0;
654 frag->size = rcd->len;
655 skb->data_len += frag->size;
656 skb_shinfo(skb)->nr_frags++;
660 static void
661 vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
662 struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
663 struct vmxnet3_adapter *adapter)
665 u32 dw2, len;
666 unsigned long buf_offset;
667 int i;
668 union Vmxnet3_GenericDesc *gdesc;
669 struct vmxnet3_tx_buf_info *tbi = NULL;
671 BUG_ON(ctx->copy_size > skb_headlen(skb));
673 /* use the previous gen bit for the SOP desc */
674 dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
676 ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
677 gdesc = ctx->sop_txd; /* both loops below can be skipped */
679 /* no need to map the buffer if headers are copied */
680 if (ctx->copy_size) {
681 ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
682 tq->tx_ring.next2fill *
683 sizeof(struct Vmxnet3_TxDataDesc));
684 ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
685 ctx->sop_txd->dword[3] = 0;
687 tbi = tq->buf_info + tq->tx_ring.next2fill;
688 tbi->map_type = VMXNET3_MAP_NONE;
690 dev_dbg(&adapter->netdev->dev,
691 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
692 tq->tx_ring.next2fill,
693 le64_to_cpu(ctx->sop_txd->txd.addr),
694 ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
695 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
697 /* use the right gen for non-SOP desc */
698 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
701 /* linear part can use multiple tx desc if it's big */
702 len = skb_headlen(skb) - ctx->copy_size;
703 buf_offset = ctx->copy_size;
704 while (len) {
705 u32 buf_size;
707 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
708 buf_size = len;
709 dw2 |= len;
710 } else {
711 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
712 /* spec says that for TxDesc.len, 0 == 2^14 */
715 tbi = tq->buf_info + tq->tx_ring.next2fill;
716 tbi->map_type = VMXNET3_MAP_SINGLE;
717 tbi->dma_addr = pci_map_single(adapter->pdev,
718 skb->data + buf_offset, buf_size,
719 PCI_DMA_TODEVICE);
721 tbi->len = buf_size;
723 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
724 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
726 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
727 gdesc->dword[2] = cpu_to_le32(dw2);
728 gdesc->dword[3] = 0;
730 dev_dbg(&adapter->netdev->dev,
731 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
732 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
733 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
734 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
735 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
737 len -= buf_size;
738 buf_offset += buf_size;
741 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
742 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
744 tbi = tq->buf_info + tq->tx_ring.next2fill;
745 tbi->map_type = VMXNET3_MAP_PAGE;
746 tbi->dma_addr = pci_map_page(adapter->pdev, frag->page,
747 frag->page_offset, frag->size,
748 PCI_DMA_TODEVICE);
750 tbi->len = frag->size;
752 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
753 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
755 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
756 gdesc->dword[2] = cpu_to_le32(dw2 | frag->size);
757 gdesc->dword[3] = 0;
759 dev_dbg(&adapter->netdev->dev,
760 "txd[%u]: 0x%llu %u %u\n",
761 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
762 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
763 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
764 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
767 ctx->eop_txd = gdesc;
769 /* set the last buf_info for the pkt */
770 tbi->skb = skb;
771 tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
775 /* Init all tx queues */
776 static void
777 vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
779 int i;
781 for (i = 0; i < adapter->num_tx_queues; i++)
782 vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
787 * parse and copy relevant protocol headers:
788 * For a tso pkt, relevant headers are L2/3/4 including options
789 * For a pkt requesting csum offloading, they are L2/3 and may include L4
790 * if it's a TCP/UDP pkt
792 * Returns:
793 * -1: error happens during parsing
794 * 0: protocol headers parsed, but too big to be copied
795 * 1: protocol headers parsed and copied
797 * Other effects:
798 * 1. related *ctx fields are updated.
799 * 2. ctx->copy_size is # of bytes copied
800 * 3. the portion copied is guaranteed to be in the linear part
803 static int
804 vmxnet3_parse_and_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
805 struct vmxnet3_tx_ctx *ctx,
806 struct vmxnet3_adapter *adapter)
808 struct Vmxnet3_TxDataDesc *tdd;
810 if (ctx->mss) { /* TSO */
811 ctx->eth_ip_hdr_size = skb_transport_offset(skb);
812 ctx->l4_hdr_size = ((struct tcphdr *)
813 skb_transport_header(skb))->doff * 4;
814 ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
815 } else {
816 if (skb->ip_summed == CHECKSUM_PARTIAL) {
817 ctx->eth_ip_hdr_size = skb_checksum_start_offset(skb);
819 if (ctx->ipv4) {
820 struct iphdr *iph = (struct iphdr *)
821 skb_network_header(skb);
822 if (iph->protocol == IPPROTO_TCP)
823 ctx->l4_hdr_size = ((struct tcphdr *)
824 skb_transport_header(skb))->doff * 4;
825 else if (iph->protocol == IPPROTO_UDP)
827 * Use tcp header size so that bytes to
828 * be copied are more than required by
829 * the device.
831 ctx->l4_hdr_size =
832 sizeof(struct tcphdr);
833 else
834 ctx->l4_hdr_size = 0;
835 } else {
836 /* for simplicity, don't copy L4 headers */
837 ctx->l4_hdr_size = 0;
839 ctx->copy_size = ctx->eth_ip_hdr_size +
840 ctx->l4_hdr_size;
841 } else {
842 ctx->eth_ip_hdr_size = 0;
843 ctx->l4_hdr_size = 0;
844 /* copy as much as allowed */
845 ctx->copy_size = min((unsigned int)VMXNET3_HDR_COPY_SIZE
846 , skb_headlen(skb));
849 /* make sure headers are accessible directly */
850 if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
851 goto err;
854 if (unlikely(ctx->copy_size > VMXNET3_HDR_COPY_SIZE)) {
855 tq->stats.oversized_hdr++;
856 ctx->copy_size = 0;
857 return 0;
860 tdd = tq->data_ring.base + tq->tx_ring.next2fill;
862 memcpy(tdd->data, skb->data, ctx->copy_size);
863 dev_dbg(&adapter->netdev->dev,
864 "copy %u bytes to dataRing[%u]\n",
865 ctx->copy_size, tq->tx_ring.next2fill);
866 return 1;
868 err:
869 return -1;
873 static void
874 vmxnet3_prepare_tso(struct sk_buff *skb,
875 struct vmxnet3_tx_ctx *ctx)
877 struct tcphdr *tcph = (struct tcphdr *)skb_transport_header(skb);
878 if (ctx->ipv4) {
879 struct iphdr *iph = (struct iphdr *)skb_network_header(skb);
880 iph->check = 0;
881 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
882 IPPROTO_TCP, 0);
883 } else {
884 struct ipv6hdr *iph = (struct ipv6hdr *)skb_network_header(skb);
885 tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
886 IPPROTO_TCP, 0);
892 * Transmits a pkt thru a given tq
893 * Returns:
894 * NETDEV_TX_OK: descriptors are setup successfully
895 * NETDEV_TX_OK: error occurred, the pkt is dropped
896 * NETDEV_TX_BUSY: tx ring is full, queue is stopped
898 * Side-effects:
899 * 1. tx ring may be changed
900 * 2. tq stats may be updated accordingly
901 * 3. shared->txNumDeferred may be updated
904 static int
905 vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
906 struct vmxnet3_adapter *adapter, struct net_device *netdev)
908 int ret;
909 u32 count;
910 unsigned long flags;
911 struct vmxnet3_tx_ctx ctx;
912 union Vmxnet3_GenericDesc *gdesc;
913 #ifdef __BIG_ENDIAN_BITFIELD
914 /* Use temporary descriptor to avoid touching bits multiple times */
915 union Vmxnet3_GenericDesc tempTxDesc;
916 #endif
918 /* conservatively estimate # of descriptors to use */
919 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) +
920 skb_shinfo(skb)->nr_frags + 1;
922 ctx.ipv4 = (skb->protocol == cpu_to_be16(ETH_P_IP));
924 ctx.mss = skb_shinfo(skb)->gso_size;
925 if (ctx.mss) {
926 if (skb_header_cloned(skb)) {
927 if (unlikely(pskb_expand_head(skb, 0, 0,
928 GFP_ATOMIC) != 0)) {
929 tq->stats.drop_tso++;
930 goto drop_pkt;
932 tq->stats.copy_skb_header++;
934 vmxnet3_prepare_tso(skb, &ctx);
935 } else {
936 if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
938 /* non-tso pkts must not use more than
939 * VMXNET3_MAX_TXD_PER_PKT entries
941 if (skb_linearize(skb) != 0) {
942 tq->stats.drop_too_many_frags++;
943 goto drop_pkt;
945 tq->stats.linearized++;
947 /* recalculate the # of descriptors to use */
948 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
952 spin_lock_irqsave(&tq->tx_lock, flags);
954 if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
955 tq->stats.tx_ring_full++;
956 dev_dbg(&adapter->netdev->dev,
957 "tx queue stopped on %s, next2comp %u"
958 " next2fill %u\n", adapter->netdev->name,
959 tq->tx_ring.next2comp, tq->tx_ring.next2fill);
961 vmxnet3_tq_stop(tq, adapter);
962 spin_unlock_irqrestore(&tq->tx_lock, flags);
963 return NETDEV_TX_BUSY;
967 ret = vmxnet3_parse_and_copy_hdr(skb, tq, &ctx, adapter);
968 if (ret >= 0) {
969 BUG_ON(ret <= 0 && ctx.copy_size != 0);
970 /* hdrs parsed, check against other limits */
971 if (ctx.mss) {
972 if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
973 VMXNET3_MAX_TX_BUF_SIZE)) {
974 goto hdr_too_big;
976 } else {
977 if (skb->ip_summed == CHECKSUM_PARTIAL) {
978 if (unlikely(ctx.eth_ip_hdr_size +
979 skb->csum_offset >
980 VMXNET3_MAX_CSUM_OFFSET)) {
981 goto hdr_too_big;
985 } else {
986 tq->stats.drop_hdr_inspect_err++;
987 goto unlock_drop_pkt;
990 /* fill tx descs related to addr & len */
991 vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter);
993 /* setup the EOP desc */
994 ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
996 /* setup the SOP desc */
997 #ifdef __BIG_ENDIAN_BITFIELD
998 gdesc = &tempTxDesc;
999 gdesc->dword[2] = ctx.sop_txd->dword[2];
1000 gdesc->dword[3] = ctx.sop_txd->dword[3];
1001 #else
1002 gdesc = ctx.sop_txd;
1003 #endif
1004 if (ctx.mss) {
1005 gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
1006 gdesc->txd.om = VMXNET3_OM_TSO;
1007 gdesc->txd.msscof = ctx.mss;
1008 le32_add_cpu(&tq->shared->txNumDeferred, (skb->len -
1009 gdesc->txd.hlen + ctx.mss - 1) / ctx.mss);
1010 } else {
1011 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1012 gdesc->txd.hlen = ctx.eth_ip_hdr_size;
1013 gdesc->txd.om = VMXNET3_OM_CSUM;
1014 gdesc->txd.msscof = ctx.eth_ip_hdr_size +
1015 skb->csum_offset;
1016 } else {
1017 gdesc->txd.om = 0;
1018 gdesc->txd.msscof = 0;
1020 le32_add_cpu(&tq->shared->txNumDeferred, 1);
1023 if (vlan_tx_tag_present(skb)) {
1024 gdesc->txd.ti = 1;
1025 gdesc->txd.tci = vlan_tx_tag_get(skb);
1028 /* finally flips the GEN bit of the SOP desc. */
1029 gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
1030 VMXNET3_TXD_GEN);
1031 #ifdef __BIG_ENDIAN_BITFIELD
1032 /* Finished updating in bitfields of Tx Desc, so write them in original
1033 * place.
1035 vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
1036 (struct Vmxnet3_TxDesc *)ctx.sop_txd);
1037 gdesc = ctx.sop_txd;
1038 #endif
1039 dev_dbg(&adapter->netdev->dev,
1040 "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
1041 (u32)((union Vmxnet3_GenericDesc *)ctx.sop_txd -
1042 tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
1043 le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
1045 spin_unlock_irqrestore(&tq->tx_lock, flags);
1047 if (le32_to_cpu(tq->shared->txNumDeferred) >=
1048 le32_to_cpu(tq->shared->txThreshold)) {
1049 tq->shared->txNumDeferred = 0;
1050 VMXNET3_WRITE_BAR0_REG(adapter,
1051 VMXNET3_REG_TXPROD + tq->qid * 8,
1052 tq->tx_ring.next2fill);
1055 return NETDEV_TX_OK;
1057 hdr_too_big:
1058 tq->stats.drop_oversized_hdr++;
1059 unlock_drop_pkt:
1060 spin_unlock_irqrestore(&tq->tx_lock, flags);
1061 drop_pkt:
1062 tq->stats.drop_total++;
1063 dev_kfree_skb(skb);
1064 return NETDEV_TX_OK;
1068 static netdev_tx_t
1069 vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1071 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1073 BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
1074 return vmxnet3_tq_xmit(skb,
1075 &adapter->tx_queue[skb->queue_mapping],
1076 adapter, netdev);
1080 static void
1081 vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
1082 struct sk_buff *skb,
1083 union Vmxnet3_GenericDesc *gdesc)
1085 if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
1086 /* typical case: TCP/UDP over IP and both csums are correct */
1087 if ((le32_to_cpu(gdesc->dword[3]) & VMXNET3_RCD_CSUM_OK) ==
1088 VMXNET3_RCD_CSUM_OK) {
1089 skb->ip_summed = CHECKSUM_UNNECESSARY;
1090 BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
1091 BUG_ON(!(gdesc->rcd.v4 || gdesc->rcd.v6));
1092 BUG_ON(gdesc->rcd.frg);
1093 } else {
1094 if (gdesc->rcd.csum) {
1095 skb->csum = htons(gdesc->rcd.csum);
1096 skb->ip_summed = CHECKSUM_PARTIAL;
1097 } else {
1098 skb_checksum_none_assert(skb);
1101 } else {
1102 skb_checksum_none_assert(skb);
1107 static void
1108 vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
1109 struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
1111 rq->stats.drop_err++;
1112 if (!rcd->fcs)
1113 rq->stats.drop_fcs++;
1115 rq->stats.drop_total++;
1118 * We do not unmap and chain the rx buffer to the skb.
1119 * We basically pretend this buffer is not used and will be recycled
1120 * by vmxnet3_rq_alloc_rx_buf()
1124 * ctx->skb may be NULL if this is the first and the only one
1125 * desc for the pkt
1127 if (ctx->skb)
1128 dev_kfree_skb_irq(ctx->skb);
1130 ctx->skb = NULL;
1134 static int
1135 vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
1136 struct vmxnet3_adapter *adapter, int quota)
1138 static const u32 rxprod_reg[2] = {
1139 VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2
1141 u32 num_rxd = 0;
1142 struct Vmxnet3_RxCompDesc *rcd;
1143 struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
1144 #ifdef __BIG_ENDIAN_BITFIELD
1145 struct Vmxnet3_RxDesc rxCmdDesc;
1146 struct Vmxnet3_RxCompDesc rxComp;
1147 #endif
1148 vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
1149 &rxComp);
1150 while (rcd->gen == rq->comp_ring.gen) {
1151 struct vmxnet3_rx_buf_info *rbi;
1152 struct sk_buff *skb;
1153 int num_to_alloc;
1154 struct Vmxnet3_RxDesc *rxd;
1155 u32 idx, ring_idx;
1157 if (num_rxd >= quota) {
1158 /* we may stop even before we see the EOP desc of
1159 * the current pkt
1161 break;
1163 num_rxd++;
1164 BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2);
1165 idx = rcd->rxdIdx;
1166 ring_idx = rcd->rqID < adapter->num_rx_queues ? 0 : 1;
1167 vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
1168 &rxCmdDesc);
1169 rbi = rq->buf_info[ring_idx] + idx;
1171 BUG_ON(rxd->addr != rbi->dma_addr ||
1172 rxd->len != rbi->len);
1174 if (unlikely(rcd->eop && rcd->err)) {
1175 vmxnet3_rx_error(rq, rcd, ctx, adapter);
1176 goto rcd_done;
1179 if (rcd->sop) { /* first buf of the pkt */
1180 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
1181 rcd->rqID != rq->qid);
1183 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
1184 BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
1186 if (unlikely(rcd->len == 0)) {
1187 /* Pretend the rx buffer is skipped. */
1188 BUG_ON(!(rcd->sop && rcd->eop));
1189 dev_dbg(&adapter->netdev->dev,
1190 "rxRing[%u][%u] 0 length\n",
1191 ring_idx, idx);
1192 goto rcd_done;
1195 ctx->skb = rbi->skb;
1196 rbi->skb = NULL;
1198 pci_unmap_single(adapter->pdev, rbi->dma_addr, rbi->len,
1199 PCI_DMA_FROMDEVICE);
1201 skb_put(ctx->skb, rcd->len);
1202 } else {
1203 BUG_ON(ctx->skb == NULL);
1204 /* non SOP buffer must be type 1 in most cases */
1205 if (rbi->buf_type == VMXNET3_RX_BUF_PAGE) {
1206 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
1208 if (rcd->len) {
1209 pci_unmap_page(adapter->pdev,
1210 rbi->dma_addr, rbi->len,
1211 PCI_DMA_FROMDEVICE);
1213 vmxnet3_append_frag(ctx->skb, rcd, rbi);
1214 rbi->page = NULL;
1216 } else {
1218 * The only time a non-SOP buffer is type 0 is
1219 * when it's EOP and error flag is raised, which
1220 * has already been handled.
1222 BUG_ON(true);
1226 skb = ctx->skb;
1227 if (rcd->eop) {
1228 skb->len += skb->data_len;
1229 skb->truesize += skb->data_len;
1231 vmxnet3_rx_csum(adapter, skb,
1232 (union Vmxnet3_GenericDesc *)rcd);
1233 skb->protocol = eth_type_trans(skb, adapter->netdev);
1235 if (unlikely(adapter->vlan_grp && rcd->ts)) {
1236 vlan_hwaccel_receive_skb(skb,
1237 adapter->vlan_grp, rcd->tci);
1238 } else {
1239 netif_receive_skb(skb);
1242 ctx->skb = NULL;
1245 rcd_done:
1246 /* device may skip some rx descs */
1247 rq->rx_ring[ring_idx].next2comp = idx;
1248 VMXNET3_INC_RING_IDX_ONLY(rq->rx_ring[ring_idx].next2comp,
1249 rq->rx_ring[ring_idx].size);
1251 /* refill rx buffers frequently to avoid starving the h/w */
1252 num_to_alloc = vmxnet3_cmd_ring_desc_avail(rq->rx_ring +
1253 ring_idx);
1254 if (unlikely(num_to_alloc > VMXNET3_RX_ALLOC_THRESHOLD(rq,
1255 ring_idx, adapter))) {
1256 vmxnet3_rq_alloc_rx_buf(rq, ring_idx, num_to_alloc,
1257 adapter);
1259 /* if needed, update the register */
1260 if (unlikely(rq->shared->updateRxProd)) {
1261 VMXNET3_WRITE_BAR0_REG(adapter,
1262 rxprod_reg[ring_idx] + rq->qid * 8,
1263 rq->rx_ring[ring_idx].next2fill);
1264 rq->uncommitted[ring_idx] = 0;
1268 vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
1269 vmxnet3_getRxComp(rcd,
1270 &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
1273 return num_rxd;
1277 static void
1278 vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
1279 struct vmxnet3_adapter *adapter)
1281 u32 i, ring_idx;
1282 struct Vmxnet3_RxDesc *rxd;
1284 for (ring_idx = 0; ring_idx < 2; ring_idx++) {
1285 for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
1286 #ifdef __BIG_ENDIAN_BITFIELD
1287 struct Vmxnet3_RxDesc rxDesc;
1288 #endif
1289 vmxnet3_getRxDesc(rxd,
1290 &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
1292 if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
1293 rq->buf_info[ring_idx][i].skb) {
1294 pci_unmap_single(adapter->pdev, rxd->addr,
1295 rxd->len, PCI_DMA_FROMDEVICE);
1296 dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
1297 rq->buf_info[ring_idx][i].skb = NULL;
1298 } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
1299 rq->buf_info[ring_idx][i].page) {
1300 pci_unmap_page(adapter->pdev, rxd->addr,
1301 rxd->len, PCI_DMA_FROMDEVICE);
1302 put_page(rq->buf_info[ring_idx][i].page);
1303 rq->buf_info[ring_idx][i].page = NULL;
1307 rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
1308 rq->rx_ring[ring_idx].next2fill =
1309 rq->rx_ring[ring_idx].next2comp = 0;
1310 rq->uncommitted[ring_idx] = 0;
1313 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1314 rq->comp_ring.next2proc = 0;
1318 static void
1319 vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
1321 int i;
1323 for (i = 0; i < adapter->num_rx_queues; i++)
1324 vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
1328 void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
1329 struct vmxnet3_adapter *adapter)
1331 int i;
1332 int j;
1334 /* all rx buffers must have already been freed */
1335 for (i = 0; i < 2; i++) {
1336 if (rq->buf_info[i]) {
1337 for (j = 0; j < rq->rx_ring[i].size; j++)
1338 BUG_ON(rq->buf_info[i][j].page != NULL);
1343 kfree(rq->buf_info[0]);
1345 for (i = 0; i < 2; i++) {
1346 if (rq->rx_ring[i].base) {
1347 pci_free_consistent(adapter->pdev, rq->rx_ring[i].size
1348 * sizeof(struct Vmxnet3_RxDesc),
1349 rq->rx_ring[i].base,
1350 rq->rx_ring[i].basePA);
1351 rq->rx_ring[i].base = NULL;
1353 rq->buf_info[i] = NULL;
1356 if (rq->comp_ring.base) {
1357 pci_free_consistent(adapter->pdev, rq->comp_ring.size *
1358 sizeof(struct Vmxnet3_RxCompDesc),
1359 rq->comp_ring.base, rq->comp_ring.basePA);
1360 rq->comp_ring.base = NULL;
1365 static int
1366 vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
1367 struct vmxnet3_adapter *adapter)
1369 int i;
1371 /* initialize buf_info */
1372 for (i = 0; i < rq->rx_ring[0].size; i++) {
1374 /* 1st buf for a pkt is skbuff */
1375 if (i % adapter->rx_buf_per_pkt == 0) {
1376 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
1377 rq->buf_info[0][i].len = adapter->skb_buf_size;
1378 } else { /* subsequent bufs for a pkt is frag */
1379 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
1380 rq->buf_info[0][i].len = PAGE_SIZE;
1383 for (i = 0; i < rq->rx_ring[1].size; i++) {
1384 rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
1385 rq->buf_info[1][i].len = PAGE_SIZE;
1388 /* reset internal state and allocate buffers for both rings */
1389 for (i = 0; i < 2; i++) {
1390 rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
1391 rq->uncommitted[i] = 0;
1393 memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
1394 sizeof(struct Vmxnet3_RxDesc));
1395 rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
1397 if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
1398 adapter) == 0) {
1399 /* at least has 1 rx buffer for the 1st ring */
1400 return -ENOMEM;
1402 vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
1404 /* reset the comp ring */
1405 rq->comp_ring.next2proc = 0;
1406 memset(rq->comp_ring.base, 0, rq->comp_ring.size *
1407 sizeof(struct Vmxnet3_RxCompDesc));
1408 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1410 /* reset rxctx */
1411 rq->rx_ctx.skb = NULL;
1413 /* stats are not reset */
1414 return 0;
1418 static int
1419 vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
1421 int i, err = 0;
1423 for (i = 0; i < adapter->num_rx_queues; i++) {
1424 err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
1425 if (unlikely(err)) {
1426 dev_err(&adapter->netdev->dev, "%s: failed to "
1427 "initialize rx queue%i\n",
1428 adapter->netdev->name, i);
1429 break;
1432 return err;
1437 static int
1438 vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
1440 int i;
1441 size_t sz;
1442 struct vmxnet3_rx_buf_info *bi;
1444 for (i = 0; i < 2; i++) {
1446 sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
1447 rq->rx_ring[i].base = pci_alloc_consistent(adapter->pdev, sz,
1448 &rq->rx_ring[i].basePA);
1449 if (!rq->rx_ring[i].base) {
1450 printk(KERN_ERR "%s: failed to allocate rx ring %d\n",
1451 adapter->netdev->name, i);
1452 goto err;
1456 sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
1457 rq->comp_ring.base = pci_alloc_consistent(adapter->pdev, sz,
1458 &rq->comp_ring.basePA);
1459 if (!rq->comp_ring.base) {
1460 printk(KERN_ERR "%s: failed to allocate rx comp ring\n",
1461 adapter->netdev->name);
1462 goto err;
1465 sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
1466 rq->rx_ring[1].size);
1467 bi = kzalloc(sz, GFP_KERNEL);
1468 if (!bi) {
1469 printk(KERN_ERR "%s: failed to allocate rx bufinfo\n",
1470 adapter->netdev->name);
1471 goto err;
1473 rq->buf_info[0] = bi;
1474 rq->buf_info[1] = bi + rq->rx_ring[0].size;
1476 return 0;
1478 err:
1479 vmxnet3_rq_destroy(rq, adapter);
1480 return -ENOMEM;
1484 static int
1485 vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
1487 int i, err = 0;
1489 for (i = 0; i < adapter->num_rx_queues; i++) {
1490 err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
1491 if (unlikely(err)) {
1492 dev_err(&adapter->netdev->dev,
1493 "%s: failed to create rx queue%i\n",
1494 adapter->netdev->name, i);
1495 goto err_out;
1498 return err;
1499 err_out:
1500 vmxnet3_rq_destroy_all(adapter);
1501 return err;
1505 /* Multiple queue aware polling function for tx and rx */
1507 static int
1508 vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
1510 int rcd_done = 0, i;
1511 if (unlikely(adapter->shared->ecr))
1512 vmxnet3_process_events(adapter);
1513 for (i = 0; i < adapter->num_tx_queues; i++)
1514 vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
1516 for (i = 0; i < adapter->num_rx_queues; i++)
1517 rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
1518 adapter, budget);
1519 return rcd_done;
1523 static int
1524 vmxnet3_poll(struct napi_struct *napi, int budget)
1526 struct vmxnet3_rx_queue *rx_queue = container_of(napi,
1527 struct vmxnet3_rx_queue, napi);
1528 int rxd_done;
1530 rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
1532 if (rxd_done < budget) {
1533 napi_complete(napi);
1534 vmxnet3_enable_all_intrs(rx_queue->adapter);
1536 return rxd_done;
1540 * NAPI polling function for MSI-X mode with multiple Rx queues
1541 * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
1544 static int
1545 vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
1547 struct vmxnet3_rx_queue *rq = container_of(napi,
1548 struct vmxnet3_rx_queue, napi);
1549 struct vmxnet3_adapter *adapter = rq->adapter;
1550 int rxd_done;
1552 /* When sharing interrupt with corresponding tx queue, process
1553 * tx completions in that queue as well
1555 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
1556 struct vmxnet3_tx_queue *tq =
1557 &adapter->tx_queue[rq - adapter->rx_queue];
1558 vmxnet3_tq_tx_complete(tq, adapter);
1561 rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
1563 if (rxd_done < budget) {
1564 napi_complete(napi);
1565 vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
1567 return rxd_done;
1571 #ifdef CONFIG_PCI_MSI
1574 * Handle completion interrupts on tx queues
1575 * Returns whether or not the intr is handled
1578 static irqreturn_t
1579 vmxnet3_msix_tx(int irq, void *data)
1581 struct vmxnet3_tx_queue *tq = data;
1582 struct vmxnet3_adapter *adapter = tq->adapter;
1584 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1585 vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
1587 /* Handle the case where only one irq is allocate for all tx queues */
1588 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1589 int i;
1590 for (i = 0; i < adapter->num_tx_queues; i++) {
1591 struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
1592 vmxnet3_tq_tx_complete(txq, adapter);
1594 } else {
1595 vmxnet3_tq_tx_complete(tq, adapter);
1597 vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
1599 return IRQ_HANDLED;
1604 * Handle completion interrupts on rx queues. Returns whether or not the
1605 * intr is handled
1608 static irqreturn_t
1609 vmxnet3_msix_rx(int irq, void *data)
1611 struct vmxnet3_rx_queue *rq = data;
1612 struct vmxnet3_adapter *adapter = rq->adapter;
1614 /* disable intr if needed */
1615 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1616 vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
1617 napi_schedule(&rq->napi);
1619 return IRQ_HANDLED;
1623 *----------------------------------------------------------------------------
1625 * vmxnet3_msix_event --
1627 * vmxnet3 msix event intr handler
1629 * Result:
1630 * whether or not the intr is handled
1632 *----------------------------------------------------------------------------
1635 static irqreturn_t
1636 vmxnet3_msix_event(int irq, void *data)
1638 struct net_device *dev = data;
1639 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1641 /* disable intr if needed */
1642 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1643 vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
1645 if (adapter->shared->ecr)
1646 vmxnet3_process_events(adapter);
1648 vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
1650 return IRQ_HANDLED;
1653 #endif /* CONFIG_PCI_MSI */
1656 /* Interrupt handler for vmxnet3 */
1657 static irqreturn_t
1658 vmxnet3_intr(int irq, void *dev_id)
1660 struct net_device *dev = dev_id;
1661 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1663 if (adapter->intr.type == VMXNET3_IT_INTX) {
1664 u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
1665 if (unlikely(icr == 0))
1666 /* not ours */
1667 return IRQ_NONE;
1671 /* disable intr if needed */
1672 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1673 vmxnet3_disable_all_intrs(adapter);
1675 napi_schedule(&adapter->rx_queue[0].napi);
1677 return IRQ_HANDLED;
1680 #ifdef CONFIG_NET_POLL_CONTROLLER
1682 /* netpoll callback. */
1683 static void
1684 vmxnet3_netpoll(struct net_device *netdev)
1686 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1688 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1689 vmxnet3_disable_all_intrs(adapter);
1691 vmxnet3_do_poll(adapter, adapter->rx_queue[0].rx_ring[0].size);
1692 vmxnet3_enable_all_intrs(adapter);
1695 #endif /* CONFIG_NET_POLL_CONTROLLER */
1697 static int
1698 vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
1700 struct vmxnet3_intr *intr = &adapter->intr;
1701 int err = 0, i;
1702 int vector = 0;
1704 #ifdef CONFIG_PCI_MSI
1705 if (adapter->intr.type == VMXNET3_IT_MSIX) {
1706 for (i = 0; i < adapter->num_tx_queues; i++) {
1707 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1708 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
1709 adapter->netdev->name, vector);
1710 err = request_irq(
1711 intr->msix_entries[vector].vector,
1712 vmxnet3_msix_tx, 0,
1713 adapter->tx_queue[i].name,
1714 &adapter->tx_queue[i]);
1715 } else {
1716 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
1717 adapter->netdev->name, vector);
1719 if (err) {
1720 dev_err(&adapter->netdev->dev,
1721 "Failed to request irq for MSIX, %s, "
1722 "error %d\n",
1723 adapter->tx_queue[i].name, err);
1724 return err;
1727 /* Handle the case where only 1 MSIx was allocated for
1728 * all tx queues */
1729 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1730 for (; i < adapter->num_tx_queues; i++)
1731 adapter->tx_queue[i].comp_ring.intr_idx
1732 = vector;
1733 vector++;
1734 break;
1735 } else {
1736 adapter->tx_queue[i].comp_ring.intr_idx
1737 = vector++;
1740 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
1741 vector = 0;
1743 for (i = 0; i < adapter->num_rx_queues; i++) {
1744 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
1745 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
1746 adapter->netdev->name, vector);
1747 else
1748 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
1749 adapter->netdev->name, vector);
1750 err = request_irq(intr->msix_entries[vector].vector,
1751 vmxnet3_msix_rx, 0,
1752 adapter->rx_queue[i].name,
1753 &(adapter->rx_queue[i]));
1754 if (err) {
1755 printk(KERN_ERR "Failed to request irq for MSIX"
1756 ", %s, error %d\n",
1757 adapter->rx_queue[i].name, err);
1758 return err;
1761 adapter->rx_queue[i].comp_ring.intr_idx = vector++;
1764 sprintf(intr->event_msi_vector_name, "%s-event-%d",
1765 adapter->netdev->name, vector);
1766 err = request_irq(intr->msix_entries[vector].vector,
1767 vmxnet3_msix_event, 0,
1768 intr->event_msi_vector_name, adapter->netdev);
1769 intr->event_intr_idx = vector;
1771 } else if (intr->type == VMXNET3_IT_MSI) {
1772 adapter->num_rx_queues = 1;
1773 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
1774 adapter->netdev->name, adapter->netdev);
1775 } else {
1776 #endif
1777 adapter->num_rx_queues = 1;
1778 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
1779 IRQF_SHARED, adapter->netdev->name,
1780 adapter->netdev);
1781 #ifdef CONFIG_PCI_MSI
1783 #endif
1784 intr->num_intrs = vector + 1;
1785 if (err) {
1786 printk(KERN_ERR "Failed to request irq %s (intr type:%d), error"
1787 ":%d\n", adapter->netdev->name, intr->type, err);
1788 } else {
1789 /* Number of rx queues will not change after this */
1790 for (i = 0; i < adapter->num_rx_queues; i++) {
1791 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
1792 rq->qid = i;
1793 rq->qid2 = i + adapter->num_rx_queues;
1798 /* init our intr settings */
1799 for (i = 0; i < intr->num_intrs; i++)
1800 intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
1801 if (adapter->intr.type != VMXNET3_IT_MSIX) {
1802 adapter->intr.event_intr_idx = 0;
1803 for (i = 0; i < adapter->num_tx_queues; i++)
1804 adapter->tx_queue[i].comp_ring.intr_idx = 0;
1805 adapter->rx_queue[0].comp_ring.intr_idx = 0;
1808 printk(KERN_INFO "%s: intr type %u, mode %u, %u vectors "
1809 "allocated\n", adapter->netdev->name, intr->type,
1810 intr->mask_mode, intr->num_intrs);
1813 return err;
1817 static void
1818 vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
1820 struct vmxnet3_intr *intr = &adapter->intr;
1821 BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
1823 switch (intr->type) {
1824 #ifdef CONFIG_PCI_MSI
1825 case VMXNET3_IT_MSIX:
1827 int i, vector = 0;
1829 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1830 for (i = 0; i < adapter->num_tx_queues; i++) {
1831 free_irq(intr->msix_entries[vector++].vector,
1832 &(adapter->tx_queue[i]));
1833 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
1834 break;
1838 for (i = 0; i < adapter->num_rx_queues; i++) {
1839 free_irq(intr->msix_entries[vector++].vector,
1840 &(adapter->rx_queue[i]));
1843 free_irq(intr->msix_entries[vector].vector,
1844 adapter->netdev);
1845 BUG_ON(vector >= intr->num_intrs);
1846 break;
1848 #endif
1849 case VMXNET3_IT_MSI:
1850 free_irq(adapter->pdev->irq, adapter->netdev);
1851 break;
1852 case VMXNET3_IT_INTX:
1853 free_irq(adapter->pdev->irq, adapter->netdev);
1854 break;
1855 default:
1856 BUG_ON(true);
1860 static void
1861 vmxnet3_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
1863 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1864 struct Vmxnet3_DriverShared *shared = adapter->shared;
1865 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1866 unsigned long flags;
1868 if (grp) {
1869 /* add vlan rx stripping. */
1870 if (adapter->netdev->features & NETIF_F_HW_VLAN_RX) {
1871 int i;
1872 adapter->vlan_grp = grp;
1875 * Clear entire vfTable; then enable untagged pkts.
1876 * Note: setting one entry in vfTable to non-zero turns
1877 * on VLAN rx filtering.
1879 for (i = 0; i < VMXNET3_VFT_SIZE; i++)
1880 vfTable[i] = 0;
1882 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1883 spin_lock_irqsave(&adapter->cmd_lock, flags);
1884 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1885 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1886 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1887 } else {
1888 printk(KERN_ERR "%s: vlan_rx_register when device has "
1889 "no NETIF_F_HW_VLAN_RX\n", netdev->name);
1891 } else {
1892 /* remove vlan rx stripping. */
1893 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
1894 adapter->vlan_grp = NULL;
1896 if (devRead->misc.uptFeatures & UPT1_F_RXVLAN) {
1897 int i;
1899 for (i = 0; i < VMXNET3_VFT_SIZE; i++) {
1900 /* clear entire vfTable; this also disables
1901 * VLAN rx filtering
1903 vfTable[i] = 0;
1905 spin_lock_irqsave(&adapter->cmd_lock, flags);
1906 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1907 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1908 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1914 static void
1915 vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
1917 if (adapter->vlan_grp) {
1918 u16 vid;
1919 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1920 bool activeVlan = false;
1922 for (vid = 0; vid < VLAN_N_VID; vid++) {
1923 if (vlan_group_get_device(adapter->vlan_grp, vid)) {
1924 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1925 activeVlan = true;
1928 if (activeVlan) {
1929 /* continue to allow untagged pkts */
1930 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1936 static void
1937 vmxnet3_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1939 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1940 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1941 unsigned long flags;
1943 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1944 spin_lock_irqsave(&adapter->cmd_lock, flags);
1945 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1946 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1947 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1951 static void
1952 vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1954 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1955 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1956 unsigned long flags;
1958 VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
1959 spin_lock_irqsave(&adapter->cmd_lock, flags);
1960 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1961 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1962 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1966 static u8 *
1967 vmxnet3_copy_mc(struct net_device *netdev)
1969 u8 *buf = NULL;
1970 u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
1972 /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
1973 if (sz <= 0xffff) {
1974 /* We may be called with BH disabled */
1975 buf = kmalloc(sz, GFP_ATOMIC);
1976 if (buf) {
1977 struct netdev_hw_addr *ha;
1978 int i = 0;
1980 netdev_for_each_mc_addr(ha, netdev)
1981 memcpy(buf + i++ * ETH_ALEN, ha->addr,
1982 ETH_ALEN);
1985 return buf;
1989 static void
1990 vmxnet3_set_mc(struct net_device *netdev)
1992 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1993 unsigned long flags;
1994 struct Vmxnet3_RxFilterConf *rxConf =
1995 &adapter->shared->devRead.rxFilterConf;
1996 u8 *new_table = NULL;
1997 u32 new_mode = VMXNET3_RXM_UCAST;
1999 if (netdev->flags & IFF_PROMISC)
2000 new_mode |= VMXNET3_RXM_PROMISC;
2002 if (netdev->flags & IFF_BROADCAST)
2003 new_mode |= VMXNET3_RXM_BCAST;
2005 if (netdev->flags & IFF_ALLMULTI)
2006 new_mode |= VMXNET3_RXM_ALL_MULTI;
2007 else
2008 if (!netdev_mc_empty(netdev)) {
2009 new_table = vmxnet3_copy_mc(netdev);
2010 if (new_table) {
2011 new_mode |= VMXNET3_RXM_MCAST;
2012 rxConf->mfTableLen = cpu_to_le16(
2013 netdev_mc_count(netdev) * ETH_ALEN);
2014 rxConf->mfTablePA = cpu_to_le64(virt_to_phys(
2015 new_table));
2016 } else {
2017 printk(KERN_INFO "%s: failed to copy mcast list"
2018 ", setting ALL_MULTI\n", netdev->name);
2019 new_mode |= VMXNET3_RXM_ALL_MULTI;
2024 if (!(new_mode & VMXNET3_RXM_MCAST)) {
2025 rxConf->mfTableLen = 0;
2026 rxConf->mfTablePA = 0;
2029 spin_lock_irqsave(&adapter->cmd_lock, flags);
2030 if (new_mode != rxConf->rxMode) {
2031 rxConf->rxMode = cpu_to_le32(new_mode);
2032 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2033 VMXNET3_CMD_UPDATE_RX_MODE);
2036 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2037 VMXNET3_CMD_UPDATE_MAC_FILTERS);
2038 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2040 kfree(new_table);
2043 void
2044 vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
2046 int i;
2048 for (i = 0; i < adapter->num_rx_queues; i++)
2049 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2054 * Set up driver_shared based on settings in adapter.
2057 static void
2058 vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2060 struct Vmxnet3_DriverShared *shared = adapter->shared;
2061 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2062 struct Vmxnet3_TxQueueConf *tqc;
2063 struct Vmxnet3_RxQueueConf *rqc;
2064 int i;
2066 memset(shared, 0, sizeof(*shared));
2068 /* driver settings */
2069 shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2070 devRead->misc.driverInfo.version = cpu_to_le32(
2071 VMXNET3_DRIVER_VERSION_NUM);
2072 devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2073 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2074 devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
2075 *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2076 *((u32 *)&devRead->misc.driverInfo.gos));
2077 devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2078 devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
2080 devRead->misc.ddPA = cpu_to_le64(virt_to_phys(adapter));
2081 devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
2083 /* set up feature flags */
2084 if (adapter->netdev->features & NETIF_F_RXCSUM)
2085 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
2087 if (adapter->netdev->features & NETIF_F_LRO) {
2088 devRead->misc.uptFeatures |= UPT1_F_LRO;
2089 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
2091 if (adapter->netdev->features & NETIF_F_HW_VLAN_RX)
2092 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
2094 devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2095 devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2096 devRead->misc.queueDescLen = cpu_to_le32(
2097 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2098 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
2100 /* tx queue settings */
2101 devRead->misc.numTxQueues = adapter->num_tx_queues;
2102 for (i = 0; i < adapter->num_tx_queues; i++) {
2103 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2104 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2105 tqc = &adapter->tqd_start[i].conf;
2106 tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
2107 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2108 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
2109 tqc->ddPA = cpu_to_le64(virt_to_phys(tq->buf_info));
2110 tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
2111 tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
2112 tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
2113 tqc->ddLen = cpu_to_le32(
2114 sizeof(struct vmxnet3_tx_buf_info) *
2115 tqc->txRingSize);
2116 tqc->intrIdx = tq->comp_ring.intr_idx;
2119 /* rx queue settings */
2120 devRead->misc.numRxQueues = adapter->num_rx_queues;
2121 for (i = 0; i < adapter->num_rx_queues; i++) {
2122 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2123 rqc = &adapter->rqd_start[i].conf;
2124 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2125 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2126 rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
2127 rqc->ddPA = cpu_to_le64(virt_to_phys(
2128 rq->buf_info));
2129 rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
2130 rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
2131 rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
2132 rqc->ddLen = cpu_to_le32(
2133 sizeof(struct vmxnet3_rx_buf_info) *
2134 (rqc->rxRingSize[0] +
2135 rqc->rxRingSize[1]));
2136 rqc->intrIdx = rq->comp_ring.intr_idx;
2139 #ifdef VMXNET3_RSS
2140 memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2142 if (adapter->rss) {
2143 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
2144 devRead->misc.uptFeatures |= UPT1_F_RSS;
2145 devRead->misc.numRxQueues = adapter->num_rx_queues;
2146 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2147 UPT1_RSS_HASH_TYPE_IPV4 |
2148 UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2149 UPT1_RSS_HASH_TYPE_IPV6;
2150 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2151 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2152 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
2153 get_random_bytes(&rssConf->hashKey[0], rssConf->hashKeySize);
2154 for (i = 0; i < rssConf->indTableSize; i++)
2155 rssConf->indTable[i] = i % adapter->num_rx_queues;
2157 devRead->rssConfDesc.confVer = 1;
2158 devRead->rssConfDesc.confLen = sizeof(*rssConf);
2159 devRead->rssConfDesc.confPA = virt_to_phys(rssConf);
2162 #endif /* VMXNET3_RSS */
2164 /* intr settings */
2165 devRead->intrConf.autoMask = adapter->intr.mask_mode ==
2166 VMXNET3_IMM_AUTO;
2167 devRead->intrConf.numIntrs = adapter->intr.num_intrs;
2168 for (i = 0; i < adapter->intr.num_intrs; i++)
2169 devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
2171 devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
2172 devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
2174 /* rx filter settings */
2175 devRead->rxFilterConf.rxMode = 0;
2176 vmxnet3_restore_vlan(adapter);
2177 vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
2179 /* the rest are already zeroed */
2184 vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
2186 int err, i;
2187 u32 ret;
2188 unsigned long flags;
2190 dev_dbg(&adapter->netdev->dev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
2191 " ring sizes %u %u %u\n", adapter->netdev->name,
2192 adapter->skb_buf_size, adapter->rx_buf_per_pkt,
2193 adapter->tx_queue[0].tx_ring.size,
2194 adapter->rx_queue[0].rx_ring[0].size,
2195 adapter->rx_queue[0].rx_ring[1].size);
2197 vmxnet3_tq_init_all(adapter);
2198 err = vmxnet3_rq_init_all(adapter);
2199 if (err) {
2200 printk(KERN_ERR "Failed to init rx queue for %s: error %d\n",
2201 adapter->netdev->name, err);
2202 goto rq_err;
2205 err = vmxnet3_request_irqs(adapter);
2206 if (err) {
2207 printk(KERN_ERR "Failed to setup irq for %s: error %d\n",
2208 adapter->netdev->name, err);
2209 goto irq_err;
2212 vmxnet3_setup_driver_shared(adapter);
2214 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
2215 adapter->shared_pa));
2216 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
2217 adapter->shared_pa));
2218 spin_lock_irqsave(&adapter->cmd_lock, flags);
2219 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2220 VMXNET3_CMD_ACTIVATE_DEV);
2221 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
2222 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2224 if (ret != 0) {
2225 printk(KERN_ERR "Failed to activate dev %s: error %u\n",
2226 adapter->netdev->name, ret);
2227 err = -EINVAL;
2228 goto activate_err;
2231 for (i = 0; i < adapter->num_rx_queues; i++) {
2232 VMXNET3_WRITE_BAR0_REG(adapter,
2233 VMXNET3_REG_RXPROD + i * VMXNET3_REG_ALIGN,
2234 adapter->rx_queue[i].rx_ring[0].next2fill);
2235 VMXNET3_WRITE_BAR0_REG(adapter, (VMXNET3_REG_RXPROD2 +
2236 (i * VMXNET3_REG_ALIGN)),
2237 adapter->rx_queue[i].rx_ring[1].next2fill);
2240 /* Apply the rx filter settins last. */
2241 vmxnet3_set_mc(adapter->netdev);
2244 * Check link state when first activating device. It will start the
2245 * tx queue if the link is up.
2247 vmxnet3_check_link(adapter, true);
2248 for (i = 0; i < adapter->num_rx_queues; i++)
2249 napi_enable(&adapter->rx_queue[i].napi);
2250 vmxnet3_enable_all_intrs(adapter);
2251 clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
2252 return 0;
2254 activate_err:
2255 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
2256 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
2257 vmxnet3_free_irqs(adapter);
2258 irq_err:
2259 rq_err:
2260 /* free up buffers we allocated */
2261 vmxnet3_rq_cleanup_all(adapter);
2262 return err;
2266 void
2267 vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
2269 unsigned long flags;
2270 spin_lock_irqsave(&adapter->cmd_lock, flags);
2271 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
2272 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2277 vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
2279 int i;
2280 unsigned long flags;
2281 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
2282 return 0;
2285 spin_lock_irqsave(&adapter->cmd_lock, flags);
2286 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2287 VMXNET3_CMD_QUIESCE_DEV);
2288 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2289 vmxnet3_disable_all_intrs(adapter);
2291 for (i = 0; i < adapter->num_rx_queues; i++)
2292 napi_disable(&adapter->rx_queue[i].napi);
2293 netif_tx_disable(adapter->netdev);
2294 adapter->link_speed = 0;
2295 netif_carrier_off(adapter->netdev);
2297 vmxnet3_tq_cleanup_all(adapter);
2298 vmxnet3_rq_cleanup_all(adapter);
2299 vmxnet3_free_irqs(adapter);
2300 return 0;
2304 static void
2305 vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2307 u32 tmp;
2309 tmp = *(u32 *)mac;
2310 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
2312 tmp = (mac[5] << 8) | mac[4];
2313 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
2317 static int
2318 vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
2320 struct sockaddr *addr = p;
2321 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2323 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2324 vmxnet3_write_mac_addr(adapter, addr->sa_data);
2326 return 0;
2330 /* ==================== initialization and cleanup routines ============ */
2332 static int
2333 vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
2335 int err;
2336 unsigned long mmio_start, mmio_len;
2337 struct pci_dev *pdev = adapter->pdev;
2339 err = pci_enable_device(pdev);
2340 if (err) {
2341 printk(KERN_ERR "Failed to enable adapter %s: error %d\n",
2342 pci_name(pdev), err);
2343 return err;
2346 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
2347 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
2348 printk(KERN_ERR "pci_set_consistent_dma_mask failed "
2349 "for adapter %s\n", pci_name(pdev));
2350 err = -EIO;
2351 goto err_set_mask;
2353 *dma64 = true;
2354 } else {
2355 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
2356 printk(KERN_ERR "pci_set_dma_mask failed for adapter "
2357 "%s\n", pci_name(pdev));
2358 err = -EIO;
2359 goto err_set_mask;
2361 *dma64 = false;
2364 err = pci_request_selected_regions(pdev, (1 << 2) - 1,
2365 vmxnet3_driver_name);
2366 if (err) {
2367 printk(KERN_ERR "Failed to request region for adapter %s: "
2368 "error %d\n", pci_name(pdev), err);
2369 goto err_set_mask;
2372 pci_set_master(pdev);
2374 mmio_start = pci_resource_start(pdev, 0);
2375 mmio_len = pci_resource_len(pdev, 0);
2376 adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
2377 if (!adapter->hw_addr0) {
2378 printk(KERN_ERR "Failed to map bar0 for adapter %s\n",
2379 pci_name(pdev));
2380 err = -EIO;
2381 goto err_ioremap;
2384 mmio_start = pci_resource_start(pdev, 1);
2385 mmio_len = pci_resource_len(pdev, 1);
2386 adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
2387 if (!adapter->hw_addr1) {
2388 printk(KERN_ERR "Failed to map bar1 for adapter %s\n",
2389 pci_name(pdev));
2390 err = -EIO;
2391 goto err_bar1;
2393 return 0;
2395 err_bar1:
2396 iounmap(adapter->hw_addr0);
2397 err_ioremap:
2398 pci_release_selected_regions(pdev, (1 << 2) - 1);
2399 err_set_mask:
2400 pci_disable_device(pdev);
2401 return err;
2405 static void
2406 vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
2408 BUG_ON(!adapter->pdev);
2410 iounmap(adapter->hw_addr0);
2411 iounmap(adapter->hw_addr1);
2412 pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
2413 pci_disable_device(adapter->pdev);
2417 static void
2418 vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
2420 size_t sz, i, ring0_size, ring1_size, comp_size;
2421 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[0];
2424 if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
2425 VMXNET3_MAX_ETH_HDR_SIZE) {
2426 adapter->skb_buf_size = adapter->netdev->mtu +
2427 VMXNET3_MAX_ETH_HDR_SIZE;
2428 if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
2429 adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
2431 adapter->rx_buf_per_pkt = 1;
2432 } else {
2433 adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
2434 sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
2435 VMXNET3_MAX_ETH_HDR_SIZE;
2436 adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
2440 * for simplicity, force the ring0 size to be a multiple of
2441 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
2443 sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
2444 ring0_size = adapter->rx_queue[0].rx_ring[0].size;
2445 ring0_size = (ring0_size + sz - 1) / sz * sz;
2446 ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
2447 sz * sz);
2448 ring1_size = adapter->rx_queue[0].rx_ring[1].size;
2449 comp_size = ring0_size + ring1_size;
2451 for (i = 0; i < adapter->num_rx_queues; i++) {
2452 rq = &adapter->rx_queue[i];
2453 rq->rx_ring[0].size = ring0_size;
2454 rq->rx_ring[1].size = ring1_size;
2455 rq->comp_ring.size = comp_size;
2461 vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
2462 u32 rx_ring_size, u32 rx_ring2_size)
2464 int err = 0, i;
2466 for (i = 0; i < adapter->num_tx_queues; i++) {
2467 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2468 tq->tx_ring.size = tx_ring_size;
2469 tq->data_ring.size = tx_ring_size;
2470 tq->comp_ring.size = tx_ring_size;
2471 tq->shared = &adapter->tqd_start[i].ctrl;
2472 tq->stopped = true;
2473 tq->adapter = adapter;
2474 tq->qid = i;
2475 err = vmxnet3_tq_create(tq, adapter);
2477 * Too late to change num_tx_queues. We cannot do away with
2478 * lesser number of queues than what we asked for
2480 if (err)
2481 goto queue_err;
2484 adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
2485 adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
2486 vmxnet3_adjust_rx_ring_size(adapter);
2487 for (i = 0; i < adapter->num_rx_queues; i++) {
2488 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2489 /* qid and qid2 for rx queues will be assigned later when num
2490 * of rx queues is finalized after allocating intrs */
2491 rq->shared = &adapter->rqd_start[i].ctrl;
2492 rq->adapter = adapter;
2493 err = vmxnet3_rq_create(rq, adapter);
2494 if (err) {
2495 if (i == 0) {
2496 printk(KERN_ERR "Could not allocate any rx"
2497 "queues. Aborting.\n");
2498 goto queue_err;
2499 } else {
2500 printk(KERN_INFO "Number of rx queues changed "
2501 "to : %d.\n", i);
2502 adapter->num_rx_queues = i;
2503 err = 0;
2504 break;
2508 return err;
2509 queue_err:
2510 vmxnet3_tq_destroy_all(adapter);
2511 return err;
2514 static int
2515 vmxnet3_open(struct net_device *netdev)
2517 struct vmxnet3_adapter *adapter;
2518 int err, i;
2520 adapter = netdev_priv(netdev);
2522 for (i = 0; i < adapter->num_tx_queues; i++)
2523 spin_lock_init(&adapter->tx_queue[i].tx_lock);
2525 err = vmxnet3_create_queues(adapter, VMXNET3_DEF_TX_RING_SIZE,
2526 VMXNET3_DEF_RX_RING_SIZE,
2527 VMXNET3_DEF_RX_RING_SIZE);
2528 if (err)
2529 goto queue_err;
2531 err = vmxnet3_activate_dev(adapter);
2532 if (err)
2533 goto activate_err;
2535 return 0;
2537 activate_err:
2538 vmxnet3_rq_destroy_all(adapter);
2539 vmxnet3_tq_destroy_all(adapter);
2540 queue_err:
2541 return err;
2545 static int
2546 vmxnet3_close(struct net_device *netdev)
2548 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2551 * Reset_work may be in the middle of resetting the device, wait for its
2552 * completion.
2554 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2555 msleep(1);
2557 vmxnet3_quiesce_dev(adapter);
2559 vmxnet3_rq_destroy_all(adapter);
2560 vmxnet3_tq_destroy_all(adapter);
2562 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2565 return 0;
2569 void
2570 vmxnet3_force_close(struct vmxnet3_adapter *adapter)
2572 int i;
2575 * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
2576 * vmxnet3_close() will deadlock.
2578 BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
2580 /* we need to enable NAPI, otherwise dev_close will deadlock */
2581 for (i = 0; i < adapter->num_rx_queues; i++)
2582 napi_enable(&adapter->rx_queue[i].napi);
2583 dev_close(adapter->netdev);
2587 static int
2588 vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
2590 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2591 int err = 0;
2593 if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
2594 return -EINVAL;
2596 netdev->mtu = new_mtu;
2599 * Reset_work may be in the middle of resetting the device, wait for its
2600 * completion.
2602 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2603 msleep(1);
2605 if (netif_running(netdev)) {
2606 vmxnet3_quiesce_dev(adapter);
2607 vmxnet3_reset_dev(adapter);
2609 /* we need to re-create the rx queue based on the new mtu */
2610 vmxnet3_rq_destroy_all(adapter);
2611 vmxnet3_adjust_rx_ring_size(adapter);
2612 err = vmxnet3_rq_create_all(adapter);
2613 if (err) {
2614 printk(KERN_ERR "%s: failed to re-create rx queues,"
2615 " error %d. Closing it.\n", netdev->name, err);
2616 goto out;
2619 err = vmxnet3_activate_dev(adapter);
2620 if (err) {
2621 printk(KERN_ERR "%s: failed to re-activate, error %d. "
2622 "Closing it\n", netdev->name, err);
2623 goto out;
2627 out:
2628 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2629 if (err)
2630 vmxnet3_force_close(adapter);
2632 return err;
2636 static void
2637 vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
2639 struct net_device *netdev = adapter->netdev;
2641 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
2642 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX |
2643 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_LRO;
2644 if (dma64)
2645 netdev->features |= NETIF_F_HIGHDMA;
2646 netdev->vlan_features = netdev->hw_features & ~NETIF_F_HW_VLAN_TX;
2647 netdev->features = netdev->hw_features |
2648 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
2650 netdev_info(adapter->netdev,
2651 "features: sg csum vlan jf tso tsoIPv6 lro%s\n",
2652 dma64 ? " highDMA" : "");
2656 static void
2657 vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2659 u32 tmp;
2661 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
2662 *(u32 *)mac = tmp;
2664 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
2665 mac[4] = tmp & 0xff;
2666 mac[5] = (tmp >> 8) & 0xff;
2669 #ifdef CONFIG_PCI_MSI
2672 * Enable MSIx vectors.
2673 * Returns :
2674 * 0 on successful enabling of required vectors,
2675 * VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
2676 * could be enabled.
2677 * number of vectors which can be enabled otherwise (this number is smaller
2678 * than VMXNET3_LINUX_MIN_MSIX_VECT)
2681 static int
2682 vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter,
2683 int vectors)
2685 int err = 0, vector_threshold;
2686 vector_threshold = VMXNET3_LINUX_MIN_MSIX_VECT;
2688 while (vectors >= vector_threshold) {
2689 err = pci_enable_msix(adapter->pdev, adapter->intr.msix_entries,
2690 vectors);
2691 if (!err) {
2692 adapter->intr.num_intrs = vectors;
2693 return 0;
2694 } else if (err < 0) {
2695 printk(KERN_ERR "Failed to enable MSI-X for %s, error"
2696 " %d\n", adapter->netdev->name, err);
2697 vectors = 0;
2698 } else if (err < vector_threshold) {
2699 break;
2700 } else {
2701 /* If fails to enable required number of MSI-x vectors
2702 * try enabling minimum number of vectors required.
2704 vectors = vector_threshold;
2705 printk(KERN_ERR "Failed to enable %d MSI-X for %s, try"
2706 " %d instead\n", vectors, adapter->netdev->name,
2707 vector_threshold);
2711 printk(KERN_INFO "Number of MSI-X interrupts which can be allocatedi"
2712 " are lower than min threshold required.\n");
2713 return err;
2717 #endif /* CONFIG_PCI_MSI */
2719 static void
2720 vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
2722 u32 cfg;
2724 /* intr settings */
2725 spin_lock(&adapter->cmd_lock);
2726 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2727 VMXNET3_CMD_GET_CONF_INTR);
2728 cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
2729 spin_unlock(&adapter->cmd_lock);
2730 adapter->intr.type = cfg & 0x3;
2731 adapter->intr.mask_mode = (cfg >> 2) & 0x3;
2733 if (adapter->intr.type == VMXNET3_IT_AUTO) {
2734 adapter->intr.type = VMXNET3_IT_MSIX;
2737 #ifdef CONFIG_PCI_MSI
2738 if (adapter->intr.type == VMXNET3_IT_MSIX) {
2739 int vector, err = 0;
2741 adapter->intr.num_intrs = (adapter->share_intr ==
2742 VMXNET3_INTR_TXSHARE) ? 1 :
2743 adapter->num_tx_queues;
2744 adapter->intr.num_intrs += (adapter->share_intr ==
2745 VMXNET3_INTR_BUDDYSHARE) ? 0 :
2746 adapter->num_rx_queues;
2747 adapter->intr.num_intrs += 1; /* for link event */
2749 adapter->intr.num_intrs = (adapter->intr.num_intrs >
2750 VMXNET3_LINUX_MIN_MSIX_VECT
2751 ? adapter->intr.num_intrs :
2752 VMXNET3_LINUX_MIN_MSIX_VECT);
2754 for (vector = 0; vector < adapter->intr.num_intrs; vector++)
2755 adapter->intr.msix_entries[vector].entry = vector;
2757 err = vmxnet3_acquire_msix_vectors(adapter,
2758 adapter->intr.num_intrs);
2759 /* If we cannot allocate one MSIx vector per queue
2760 * then limit the number of rx queues to 1
2762 if (err == VMXNET3_LINUX_MIN_MSIX_VECT) {
2763 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
2764 || adapter->num_rx_queues != 1) {
2765 adapter->share_intr = VMXNET3_INTR_TXSHARE;
2766 printk(KERN_ERR "Number of rx queues : 1\n");
2767 adapter->num_rx_queues = 1;
2768 adapter->intr.num_intrs =
2769 VMXNET3_LINUX_MIN_MSIX_VECT;
2771 return;
2773 if (!err)
2774 return;
2776 /* If we cannot allocate MSIx vectors use only one rx queue */
2777 printk(KERN_INFO "Failed to enable MSI-X for %s, error %d."
2778 "#rx queues : 1, try MSI\n", adapter->netdev->name, err);
2780 adapter->intr.type = VMXNET3_IT_MSI;
2783 if (adapter->intr.type == VMXNET3_IT_MSI) {
2784 int err;
2785 err = pci_enable_msi(adapter->pdev);
2786 if (!err) {
2787 adapter->num_rx_queues = 1;
2788 adapter->intr.num_intrs = 1;
2789 return;
2792 #endif /* CONFIG_PCI_MSI */
2794 adapter->num_rx_queues = 1;
2795 printk(KERN_INFO "Using INTx interrupt, #Rx queues: 1.\n");
2796 adapter->intr.type = VMXNET3_IT_INTX;
2798 /* INT-X related setting */
2799 adapter->intr.num_intrs = 1;
2803 static void
2804 vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
2806 if (adapter->intr.type == VMXNET3_IT_MSIX)
2807 pci_disable_msix(adapter->pdev);
2808 else if (adapter->intr.type == VMXNET3_IT_MSI)
2809 pci_disable_msi(adapter->pdev);
2810 else
2811 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
2815 static void
2816 vmxnet3_tx_timeout(struct net_device *netdev)
2818 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2819 adapter->tx_timeout_count++;
2821 printk(KERN_ERR "%s: tx hang\n", adapter->netdev->name);
2822 schedule_work(&adapter->work);
2823 netif_wake_queue(adapter->netdev);
2827 static void
2828 vmxnet3_reset_work(struct work_struct *data)
2830 struct vmxnet3_adapter *adapter;
2832 adapter = container_of(data, struct vmxnet3_adapter, work);
2834 /* if another thread is resetting the device, no need to proceed */
2835 if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2836 return;
2838 /* if the device is closed, we must leave it alone */
2839 rtnl_lock();
2840 if (netif_running(adapter->netdev)) {
2841 printk(KERN_INFO "%s: resetting\n", adapter->netdev->name);
2842 vmxnet3_quiesce_dev(adapter);
2843 vmxnet3_reset_dev(adapter);
2844 vmxnet3_activate_dev(adapter);
2845 } else {
2846 printk(KERN_INFO "%s: already closed\n", adapter->netdev->name);
2848 rtnl_unlock();
2850 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2854 static int __devinit
2855 vmxnet3_probe_device(struct pci_dev *pdev,
2856 const struct pci_device_id *id)
2858 static const struct net_device_ops vmxnet3_netdev_ops = {
2859 .ndo_open = vmxnet3_open,
2860 .ndo_stop = vmxnet3_close,
2861 .ndo_start_xmit = vmxnet3_xmit_frame,
2862 .ndo_set_mac_address = vmxnet3_set_mac_addr,
2863 .ndo_change_mtu = vmxnet3_change_mtu,
2864 .ndo_set_features = vmxnet3_set_features,
2865 .ndo_get_stats = vmxnet3_get_stats,
2866 .ndo_tx_timeout = vmxnet3_tx_timeout,
2867 .ndo_set_multicast_list = vmxnet3_set_mc,
2868 .ndo_vlan_rx_register = vmxnet3_vlan_rx_register,
2869 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
2870 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
2871 #ifdef CONFIG_NET_POLL_CONTROLLER
2872 .ndo_poll_controller = vmxnet3_netpoll,
2873 #endif
2875 int err;
2876 bool dma64 = false; /* stupid gcc */
2877 u32 ver;
2878 struct net_device *netdev;
2879 struct vmxnet3_adapter *adapter;
2880 u8 mac[ETH_ALEN];
2881 int size;
2882 int num_tx_queues;
2883 int num_rx_queues;
2885 #ifdef VMXNET3_RSS
2886 if (enable_mq)
2887 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
2888 (int)num_online_cpus());
2889 else
2890 #endif
2891 num_rx_queues = 1;
2893 if (enable_mq)
2894 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
2895 (int)num_online_cpus());
2896 else
2897 num_tx_queues = 1;
2899 netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
2900 max(num_tx_queues, num_rx_queues));
2901 printk(KERN_INFO "# of Tx queues : %d, # of Rx queues : %d\n",
2902 num_tx_queues, num_rx_queues);
2904 if (!netdev) {
2905 printk(KERN_ERR "Failed to alloc ethernet device for adapter "
2906 "%s\n", pci_name(pdev));
2907 return -ENOMEM;
2910 pci_set_drvdata(pdev, netdev);
2911 adapter = netdev_priv(netdev);
2912 adapter->netdev = netdev;
2913 adapter->pdev = pdev;
2915 spin_lock_init(&adapter->cmd_lock);
2916 adapter->shared = pci_alloc_consistent(adapter->pdev,
2917 sizeof(struct Vmxnet3_DriverShared),
2918 &adapter->shared_pa);
2919 if (!adapter->shared) {
2920 printk(KERN_ERR "Failed to allocate memory for %s\n",
2921 pci_name(pdev));
2922 err = -ENOMEM;
2923 goto err_alloc_shared;
2926 adapter->num_rx_queues = num_rx_queues;
2927 adapter->num_tx_queues = num_tx_queues;
2929 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
2930 size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
2931 adapter->tqd_start = pci_alloc_consistent(adapter->pdev, size,
2932 &adapter->queue_desc_pa);
2934 if (!adapter->tqd_start) {
2935 printk(KERN_ERR "Failed to allocate memory for %s\n",
2936 pci_name(pdev));
2937 err = -ENOMEM;
2938 goto err_alloc_queue_desc;
2940 adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
2941 adapter->num_tx_queues);
2943 adapter->pm_conf = kmalloc(sizeof(struct Vmxnet3_PMConf), GFP_KERNEL);
2944 if (adapter->pm_conf == NULL) {
2945 printk(KERN_ERR "Failed to allocate memory for %s\n",
2946 pci_name(pdev));
2947 err = -ENOMEM;
2948 goto err_alloc_pm;
2951 #ifdef VMXNET3_RSS
2953 adapter->rss_conf = kmalloc(sizeof(struct UPT1_RSSConf), GFP_KERNEL);
2954 if (adapter->rss_conf == NULL) {
2955 printk(KERN_ERR "Failed to allocate memory for %s\n",
2956 pci_name(pdev));
2957 err = -ENOMEM;
2958 goto err_alloc_rss;
2960 #endif /* VMXNET3_RSS */
2962 err = vmxnet3_alloc_pci_resources(adapter, &dma64);
2963 if (err < 0)
2964 goto err_alloc_pci;
2966 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
2967 if (ver & 1) {
2968 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
2969 } else {
2970 printk(KERN_ERR "Incompatible h/w version (0x%x) for adapter"
2971 " %s\n", ver, pci_name(pdev));
2972 err = -EBUSY;
2973 goto err_ver;
2976 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
2977 if (ver & 1) {
2978 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
2979 } else {
2980 printk(KERN_ERR "Incompatible upt version (0x%x) for "
2981 "adapter %s\n", ver, pci_name(pdev));
2982 err = -EBUSY;
2983 goto err_ver;
2986 vmxnet3_declare_features(adapter, dma64);
2988 adapter->dev_number = atomic_read(&devices_found);
2990 adapter->share_intr = irq_share_mode;
2991 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE &&
2992 adapter->num_tx_queues != adapter->num_rx_queues)
2993 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
2995 vmxnet3_alloc_intr_resources(adapter);
2997 #ifdef VMXNET3_RSS
2998 if (adapter->num_rx_queues > 1 &&
2999 adapter->intr.type == VMXNET3_IT_MSIX) {
3000 adapter->rss = true;
3001 printk(KERN_INFO "RSS is enabled.\n");
3002 } else {
3003 adapter->rss = false;
3005 #endif
3007 vmxnet3_read_mac_addr(adapter, mac);
3008 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3010 netdev->netdev_ops = &vmxnet3_netdev_ops;
3011 vmxnet3_set_ethtool_ops(netdev);
3012 netdev->watchdog_timeo = 5 * HZ;
3014 INIT_WORK(&adapter->work, vmxnet3_reset_work);
3016 if (adapter->intr.type == VMXNET3_IT_MSIX) {
3017 int i;
3018 for (i = 0; i < adapter->num_rx_queues; i++) {
3019 netif_napi_add(adapter->netdev,
3020 &adapter->rx_queue[i].napi,
3021 vmxnet3_poll_rx_only, 64);
3023 } else {
3024 netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
3025 vmxnet3_poll, 64);
3028 netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
3029 netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
3031 SET_NETDEV_DEV(netdev, &pdev->dev);
3032 err = register_netdev(netdev);
3034 if (err) {
3035 printk(KERN_ERR "Failed to register adapter %s\n",
3036 pci_name(pdev));
3037 goto err_register;
3040 set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
3041 vmxnet3_check_link(adapter, false);
3042 atomic_inc(&devices_found);
3043 return 0;
3045 err_register:
3046 vmxnet3_free_intr_resources(adapter);
3047 err_ver:
3048 vmxnet3_free_pci_resources(adapter);
3049 err_alloc_pci:
3050 #ifdef VMXNET3_RSS
3051 kfree(adapter->rss_conf);
3052 err_alloc_rss:
3053 #endif
3054 kfree(adapter->pm_conf);
3055 err_alloc_pm:
3056 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3057 adapter->queue_desc_pa);
3058 err_alloc_queue_desc:
3059 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3060 adapter->shared, adapter->shared_pa);
3061 err_alloc_shared:
3062 pci_set_drvdata(pdev, NULL);
3063 free_netdev(netdev);
3064 return err;
3068 static void __devexit
3069 vmxnet3_remove_device(struct pci_dev *pdev)
3071 struct net_device *netdev = pci_get_drvdata(pdev);
3072 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3073 int size = 0;
3074 int num_rx_queues;
3076 #ifdef VMXNET3_RSS
3077 if (enable_mq)
3078 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3079 (int)num_online_cpus());
3080 else
3081 #endif
3082 num_rx_queues = 1;
3084 cancel_work_sync(&adapter->work);
3086 unregister_netdev(netdev);
3088 vmxnet3_free_intr_resources(adapter);
3089 vmxnet3_free_pci_resources(adapter);
3090 #ifdef VMXNET3_RSS
3091 kfree(adapter->rss_conf);
3092 #endif
3093 kfree(adapter->pm_conf);
3095 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3096 size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
3097 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3098 adapter->queue_desc_pa);
3099 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3100 adapter->shared, adapter->shared_pa);
3101 free_netdev(netdev);
3105 #ifdef CONFIG_PM
3107 static int
3108 vmxnet3_suspend(struct device *device)
3110 struct pci_dev *pdev = to_pci_dev(device);
3111 struct net_device *netdev = pci_get_drvdata(pdev);
3112 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3113 struct Vmxnet3_PMConf *pmConf;
3114 struct ethhdr *ehdr;
3115 struct arphdr *ahdr;
3116 u8 *arpreq;
3117 struct in_device *in_dev;
3118 struct in_ifaddr *ifa;
3119 unsigned long flags;
3120 int i = 0;
3122 if (!netif_running(netdev))
3123 return 0;
3125 for (i = 0; i < adapter->num_rx_queues; i++)
3126 napi_disable(&adapter->rx_queue[i].napi);
3128 vmxnet3_disable_all_intrs(adapter);
3129 vmxnet3_free_irqs(adapter);
3130 vmxnet3_free_intr_resources(adapter);
3132 netif_device_detach(netdev);
3133 netif_tx_stop_all_queues(netdev);
3135 /* Create wake-up filters. */
3136 pmConf = adapter->pm_conf;
3137 memset(pmConf, 0, sizeof(*pmConf));
3139 if (adapter->wol & WAKE_UCAST) {
3140 pmConf->filters[i].patternSize = ETH_ALEN;
3141 pmConf->filters[i].maskSize = 1;
3142 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
3143 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
3145 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
3146 i++;
3149 if (adapter->wol & WAKE_ARP) {
3150 in_dev = in_dev_get(netdev);
3151 if (!in_dev)
3152 goto skip_arp;
3154 ifa = (struct in_ifaddr *)in_dev->ifa_list;
3155 if (!ifa)
3156 goto skip_arp;
3158 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
3159 sizeof(struct arphdr) + /* ARP header */
3160 2 * ETH_ALEN + /* 2 Ethernet addresses*/
3161 2 * sizeof(u32); /*2 IPv4 addresses */
3162 pmConf->filters[i].maskSize =
3163 (pmConf->filters[i].patternSize - 1) / 8 + 1;
3165 /* ETH_P_ARP in Ethernet header. */
3166 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
3167 ehdr->h_proto = htons(ETH_P_ARP);
3169 /* ARPOP_REQUEST in ARP header. */
3170 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
3171 ahdr->ar_op = htons(ARPOP_REQUEST);
3172 arpreq = (u8 *)(ahdr + 1);
3174 /* The Unicast IPv4 address in 'tip' field. */
3175 arpreq += 2 * ETH_ALEN + sizeof(u32);
3176 *(u32 *)arpreq = ifa->ifa_address;
3178 /* The mask for the relevant bits. */
3179 pmConf->filters[i].mask[0] = 0x00;
3180 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
3181 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
3182 pmConf->filters[i].mask[3] = 0x00;
3183 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
3184 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
3185 in_dev_put(in_dev);
3187 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
3188 i++;
3191 skip_arp:
3192 if (adapter->wol & WAKE_MAGIC)
3193 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
3195 pmConf->numFilters = i;
3197 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3198 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3199 *pmConf));
3200 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3201 pmConf));
3203 spin_lock_irqsave(&adapter->cmd_lock, flags);
3204 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3205 VMXNET3_CMD_UPDATE_PMCFG);
3206 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3208 pci_save_state(pdev);
3209 pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
3210 adapter->wol);
3211 pci_disable_device(pdev);
3212 pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
3214 return 0;
3218 static int
3219 vmxnet3_resume(struct device *device)
3221 int err, i = 0;
3222 unsigned long flags;
3223 struct pci_dev *pdev = to_pci_dev(device);
3224 struct net_device *netdev = pci_get_drvdata(pdev);
3225 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3226 struct Vmxnet3_PMConf *pmConf;
3228 if (!netif_running(netdev))
3229 return 0;
3231 /* Destroy wake-up filters. */
3232 pmConf = adapter->pm_conf;
3233 memset(pmConf, 0, sizeof(*pmConf));
3235 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3236 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3237 *pmConf));
3238 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3239 pmConf));
3241 netif_device_attach(netdev);
3242 pci_set_power_state(pdev, PCI_D0);
3243 pci_restore_state(pdev);
3244 err = pci_enable_device_mem(pdev);
3245 if (err != 0)
3246 return err;
3248 pci_enable_wake(pdev, PCI_D0, 0);
3250 spin_lock_irqsave(&adapter->cmd_lock, flags);
3251 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3252 VMXNET3_CMD_UPDATE_PMCFG);
3253 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3254 vmxnet3_alloc_intr_resources(adapter);
3255 vmxnet3_request_irqs(adapter);
3256 for (i = 0; i < adapter->num_rx_queues; i++)
3257 napi_enable(&adapter->rx_queue[i].napi);
3258 vmxnet3_enable_all_intrs(adapter);
3260 return 0;
3263 static const struct dev_pm_ops vmxnet3_pm_ops = {
3264 .suspend = vmxnet3_suspend,
3265 .resume = vmxnet3_resume,
3267 #endif
3269 static struct pci_driver vmxnet3_driver = {
3270 .name = vmxnet3_driver_name,
3271 .id_table = vmxnet3_pciid_table,
3272 .probe = vmxnet3_probe_device,
3273 .remove = __devexit_p(vmxnet3_remove_device),
3274 #ifdef CONFIG_PM
3275 .driver.pm = &vmxnet3_pm_ops,
3276 #endif
3280 static int __init
3281 vmxnet3_init_module(void)
3283 printk(KERN_INFO "%s - version %s\n", VMXNET3_DRIVER_DESC,
3284 VMXNET3_DRIVER_VERSION_REPORT);
3285 return pci_register_driver(&vmxnet3_driver);
3288 module_init(vmxnet3_init_module);
3291 static void
3292 vmxnet3_exit_module(void)
3294 pci_unregister_driver(&vmxnet3_driver);
3297 module_exit(vmxnet3_exit_module);
3299 MODULE_AUTHOR("VMware, Inc.");
3300 MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
3301 MODULE_LICENSE("GPL v2");
3302 MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);