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
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"
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)},
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;
52 vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac);
55 * Enable/Disable the given intr
58 vmxnet3_enable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
60 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 0);
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
75 vmxnet3_enable_all_intrs(struct vmxnet3_adapter *adapter)
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);
87 vmxnet3_disable_all_intrs(struct vmxnet3_adapter *adapter)
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);
99 vmxnet3_ack_events(struct vmxnet3_adapter *adapter, u32 events)
101 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_ECR, events);
106 vmxnet3_tq_stopped(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
113 vmxnet3_tq_start(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
116 netif_start_subqueue(adapter->netdev, tq - adapter->tx_queue);
121 vmxnet3_tq_wake(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
124 netif_wake_subqueue(adapter->netdev, (tq - adapter->tx_queue));
129 vmxnet3_tq_stop(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
133 netif_stop_subqueue(adapter->netdev, (tq - adapter->tx_queue));
138 * Check the link state. This may start or stop the tx queue.
141 vmxnet3_check_link(struct vmxnet3_adapter *adapter, bool affectTxQueue)
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);
160 for (i = 0; i < adapter->num_tx_queues; i++)
161 vmxnet3_tq_start(&adapter->tx_queue[i],
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);
171 for (i = 0; i < adapter->num_tx_queues; i++)
172 vmxnet3_tq_stop(&adapter->tx_queue[i], adapter);
178 vmxnet3_process_events(struct vmxnet3_adapter *adapter)
182 u32 events = le32_to_cpu(adapter->shared->ecr);
186 vmxnet3_ack_events(adapter, events);
188 /* Check if link state has changed */
189 if (events & VMXNET3_ECR_LINK)
190 vmxnet3_check_link(adapter, true);
192 /* Check if there is an error on xmit/recv queues */
193 if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
194 spin_lock_irqsave(&adapter->cmd_lock, flags);
195 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
196 VMXNET3_CMD_GET_QUEUE_STATUS);
197 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
199 for (i = 0; i < adapter->num_tx_queues; i++)
200 if (adapter->tqd_start[i].status.stopped)
201 dev_err(&adapter->netdev->dev,
202 "%s: tq[%d] error 0x%x\n",
203 adapter->netdev->name, i, le32_to_cpu(
204 adapter->tqd_start[i].status.error));
205 for (i = 0; i < adapter->num_rx_queues; i++)
206 if (adapter->rqd_start[i].status.stopped)
207 dev_err(&adapter->netdev->dev,
208 "%s: rq[%d] error 0x%x\n",
209 adapter->netdev->name, i,
210 adapter->rqd_start[i].status.error);
212 schedule_work(&adapter->work);
216 #ifdef __BIG_ENDIAN_BITFIELD
218 * The device expects the bitfields in shared structures to be written in
219 * little endian. When CPU is big endian, the following routines are used to
220 * correctly read and write into ABI.
221 * The general technique used here is : double word bitfields are defined in
222 * opposite order for big endian architecture. Then before reading them in
223 * driver the complete double word is translated using le32_to_cpu. Similarly
224 * After the driver writes into bitfields, cpu_to_le32 is used to translate the
225 * double words into required format.
226 * In order to avoid touching bits in shared structure more than once, temporary
227 * descriptors are used. These are passed as srcDesc to following functions.
229 static void vmxnet3_RxDescToCPU(const struct Vmxnet3_RxDesc *srcDesc,
230 struct Vmxnet3_RxDesc *dstDesc)
232 u32 *src = (u32 *)srcDesc + 2;
233 u32 *dst = (u32 *)dstDesc + 2;
234 dstDesc->addr = le64_to_cpu(srcDesc->addr);
235 *dst = le32_to_cpu(*src);
236 dstDesc->ext1 = le32_to_cpu(srcDesc->ext1);
239 static void vmxnet3_TxDescToLe(const struct Vmxnet3_TxDesc *srcDesc,
240 struct Vmxnet3_TxDesc *dstDesc)
243 u32 *src = (u32 *)(srcDesc + 1);
244 u32 *dst = (u32 *)(dstDesc + 1);
246 /* Working backwards so that the gen bit is set at the end. */
247 for (i = 2; i > 0; i--) {
250 *dst = cpu_to_le32(*src);
255 static void vmxnet3_RxCompToCPU(const struct Vmxnet3_RxCompDesc *srcDesc,
256 struct Vmxnet3_RxCompDesc *dstDesc)
259 u32 *src = (u32 *)srcDesc;
260 u32 *dst = (u32 *)dstDesc;
261 for (i = 0; i < sizeof(struct Vmxnet3_RxCompDesc) / sizeof(u32); i++) {
262 *dst = le32_to_cpu(*src);
269 /* Used to read bitfield values from double words. */
270 static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
272 u32 temp = le32_to_cpu(*bitfield);
273 u32 mask = ((1 << size) - 1) << pos;
281 #endif /* __BIG_ENDIAN_BITFIELD */
283 #ifdef __BIG_ENDIAN_BITFIELD
285 # define VMXNET3_TXDESC_GET_GEN(txdesc) get_bitfield32(((const __le32 *) \
286 txdesc) + VMXNET3_TXD_GEN_DWORD_SHIFT, \
287 VMXNET3_TXD_GEN_SHIFT, VMXNET3_TXD_GEN_SIZE)
288 # define VMXNET3_TXDESC_GET_EOP(txdesc) get_bitfield32(((const __le32 *) \
289 txdesc) + VMXNET3_TXD_EOP_DWORD_SHIFT, \
290 VMXNET3_TXD_EOP_SHIFT, VMXNET3_TXD_EOP_SIZE)
291 # define VMXNET3_TCD_GET_GEN(tcd) get_bitfield32(((const __le32 *)tcd) + \
292 VMXNET3_TCD_GEN_DWORD_SHIFT, VMXNET3_TCD_GEN_SHIFT, \
293 VMXNET3_TCD_GEN_SIZE)
294 # define VMXNET3_TCD_GET_TXIDX(tcd) get_bitfield32((const __le32 *)tcd, \
295 VMXNET3_TCD_TXIDX_SHIFT, VMXNET3_TCD_TXIDX_SIZE)
296 # define vmxnet3_getRxComp(dstrcd, rcd, tmp) do { \
298 vmxnet3_RxCompToCPU((rcd), (tmp)); \
300 # define vmxnet3_getRxDesc(dstrxd, rxd, tmp) do { \
302 vmxnet3_RxDescToCPU((rxd), (tmp)); \
307 # define VMXNET3_TXDESC_GET_GEN(txdesc) ((txdesc)->gen)
308 # define VMXNET3_TXDESC_GET_EOP(txdesc) ((txdesc)->eop)
309 # define VMXNET3_TCD_GET_GEN(tcd) ((tcd)->gen)
310 # define VMXNET3_TCD_GET_TXIDX(tcd) ((tcd)->txdIdx)
311 # define vmxnet3_getRxComp(dstrcd, rcd, tmp) (dstrcd) = (rcd)
312 # define vmxnet3_getRxDesc(dstrxd, rxd, tmp) (dstrxd) = (rxd)
314 #endif /* __BIG_ENDIAN_BITFIELD */
318 vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
319 struct pci_dev *pdev)
321 if (tbi->map_type == VMXNET3_MAP_SINGLE)
322 pci_unmap_single(pdev, tbi->dma_addr, tbi->len,
324 else if (tbi->map_type == VMXNET3_MAP_PAGE)
325 pci_unmap_page(pdev, tbi->dma_addr, tbi->len,
328 BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
330 tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
335 vmxnet3_unmap_pkt(u32 eop_idx, struct vmxnet3_tx_queue *tq,
336 struct pci_dev *pdev, struct vmxnet3_adapter *adapter)
341 /* no out of order completion */
342 BUG_ON(tq->buf_info[eop_idx].sop_idx != tq->tx_ring.next2comp);
343 BUG_ON(VMXNET3_TXDESC_GET_EOP(&(tq->tx_ring.base[eop_idx].txd)) != 1);
345 skb = tq->buf_info[eop_idx].skb;
347 tq->buf_info[eop_idx].skb = NULL;
349 VMXNET3_INC_RING_IDX_ONLY(eop_idx, tq->tx_ring.size);
351 while (tq->tx_ring.next2comp != eop_idx) {
352 vmxnet3_unmap_tx_buf(tq->buf_info + tq->tx_ring.next2comp,
355 /* update next2comp w/o tx_lock. Since we are marking more,
356 * instead of less, tx ring entries avail, the worst case is
357 * that the tx routine incorrectly re-queues a pkt due to
358 * insufficient tx ring entries.
360 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
364 dev_kfree_skb_any(skb);
370 vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue *tq,
371 struct vmxnet3_adapter *adapter)
374 union Vmxnet3_GenericDesc *gdesc;
376 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
377 while (VMXNET3_TCD_GET_GEN(&gdesc->tcd) == tq->comp_ring.gen) {
378 completed += vmxnet3_unmap_pkt(VMXNET3_TCD_GET_TXIDX(
379 &gdesc->tcd), tq, adapter->pdev,
382 vmxnet3_comp_ring_adv_next2proc(&tq->comp_ring);
383 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
387 spin_lock(&tq->tx_lock);
388 if (unlikely(vmxnet3_tq_stopped(tq, adapter) &&
389 vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) >
390 VMXNET3_WAKE_QUEUE_THRESHOLD(tq) &&
391 netif_carrier_ok(adapter->netdev))) {
392 vmxnet3_tq_wake(tq, adapter);
394 spin_unlock(&tq->tx_lock);
401 vmxnet3_tq_cleanup(struct vmxnet3_tx_queue *tq,
402 struct vmxnet3_adapter *adapter)
406 while (tq->tx_ring.next2comp != tq->tx_ring.next2fill) {
407 struct vmxnet3_tx_buf_info *tbi;
408 union Vmxnet3_GenericDesc *gdesc;
410 tbi = tq->buf_info + tq->tx_ring.next2comp;
411 gdesc = tq->tx_ring.base + tq->tx_ring.next2comp;
413 vmxnet3_unmap_tx_buf(tbi, adapter->pdev);
415 dev_kfree_skb_any(tbi->skb);
418 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
421 /* sanity check, verify all buffers are indeed unmapped and freed */
422 for (i = 0; i < tq->tx_ring.size; i++) {
423 BUG_ON(tq->buf_info[i].skb != NULL ||
424 tq->buf_info[i].map_type != VMXNET3_MAP_NONE);
427 tq->tx_ring.gen = VMXNET3_INIT_GEN;
428 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
430 tq->comp_ring.gen = VMXNET3_INIT_GEN;
431 tq->comp_ring.next2proc = 0;
436 vmxnet3_tq_destroy(struct vmxnet3_tx_queue *tq,
437 struct vmxnet3_adapter *adapter)
439 if (tq->tx_ring.base) {
440 pci_free_consistent(adapter->pdev, tq->tx_ring.size *
441 sizeof(struct Vmxnet3_TxDesc),
442 tq->tx_ring.base, tq->tx_ring.basePA);
443 tq->tx_ring.base = NULL;
445 if (tq->data_ring.base) {
446 pci_free_consistent(adapter->pdev, tq->data_ring.size *
447 sizeof(struct Vmxnet3_TxDataDesc),
448 tq->data_ring.base, tq->data_ring.basePA);
449 tq->data_ring.base = NULL;
451 if (tq->comp_ring.base) {
452 pci_free_consistent(adapter->pdev, tq->comp_ring.size *
453 sizeof(struct Vmxnet3_TxCompDesc),
454 tq->comp_ring.base, tq->comp_ring.basePA);
455 tq->comp_ring.base = NULL;
462 /* Destroy all tx queues */
464 vmxnet3_tq_destroy_all(struct vmxnet3_adapter *adapter)
468 for (i = 0; i < adapter->num_tx_queues; i++)
469 vmxnet3_tq_destroy(&adapter->tx_queue[i], adapter);
474 vmxnet3_tq_init(struct vmxnet3_tx_queue *tq,
475 struct vmxnet3_adapter *adapter)
479 /* reset the tx ring contents to 0 and reset the tx ring states */
480 memset(tq->tx_ring.base, 0, tq->tx_ring.size *
481 sizeof(struct Vmxnet3_TxDesc));
482 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
483 tq->tx_ring.gen = VMXNET3_INIT_GEN;
485 memset(tq->data_ring.base, 0, tq->data_ring.size *
486 sizeof(struct Vmxnet3_TxDataDesc));
488 /* reset the tx comp ring contents to 0 and reset comp ring states */
489 memset(tq->comp_ring.base, 0, tq->comp_ring.size *
490 sizeof(struct Vmxnet3_TxCompDesc));
491 tq->comp_ring.next2proc = 0;
492 tq->comp_ring.gen = VMXNET3_INIT_GEN;
494 /* reset the bookkeeping data */
495 memset(tq->buf_info, 0, sizeof(tq->buf_info[0]) * tq->tx_ring.size);
496 for (i = 0; i < tq->tx_ring.size; i++)
497 tq->buf_info[i].map_type = VMXNET3_MAP_NONE;
499 /* stats are not reset */
504 vmxnet3_tq_create(struct vmxnet3_tx_queue *tq,
505 struct vmxnet3_adapter *adapter)
507 BUG_ON(tq->tx_ring.base || tq->data_ring.base ||
508 tq->comp_ring.base || tq->buf_info);
510 tq->tx_ring.base = pci_alloc_consistent(adapter->pdev, tq->tx_ring.size
511 * sizeof(struct Vmxnet3_TxDesc),
512 &tq->tx_ring.basePA);
513 if (!tq->tx_ring.base) {
514 printk(KERN_ERR "%s: failed to allocate tx ring\n",
515 adapter->netdev->name);
519 tq->data_ring.base = pci_alloc_consistent(adapter->pdev,
521 sizeof(struct Vmxnet3_TxDataDesc),
522 &tq->data_ring.basePA);
523 if (!tq->data_ring.base) {
524 printk(KERN_ERR "%s: failed to allocate data ring\n",
525 adapter->netdev->name);
529 tq->comp_ring.base = pci_alloc_consistent(adapter->pdev,
531 sizeof(struct Vmxnet3_TxCompDesc),
532 &tq->comp_ring.basePA);
533 if (!tq->comp_ring.base) {
534 printk(KERN_ERR "%s: failed to allocate tx comp ring\n",
535 adapter->netdev->name);
539 tq->buf_info = kcalloc(tq->tx_ring.size, sizeof(tq->buf_info[0]),
542 printk(KERN_ERR "%s: failed to allocate tx bufinfo\n",
543 adapter->netdev->name);
550 vmxnet3_tq_destroy(tq, adapter);
555 vmxnet3_tq_cleanup_all(struct vmxnet3_adapter *adapter)
559 for (i = 0; i < adapter->num_tx_queues; i++)
560 vmxnet3_tq_cleanup(&adapter->tx_queue[i], adapter);
564 * starting from ring->next2fill, allocate rx buffers for the given ring
565 * of the rx queue and update the rx desc. stop after @num_to_alloc buffers
566 * are allocated or allocation fails
570 vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
571 int num_to_alloc, struct vmxnet3_adapter *adapter)
573 int num_allocated = 0;
574 struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
575 struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
578 while (num_allocated <= num_to_alloc) {
579 struct vmxnet3_rx_buf_info *rbi;
580 union Vmxnet3_GenericDesc *gd;
582 rbi = rbi_base + ring->next2fill;
583 gd = ring->base + ring->next2fill;
585 if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
586 if (rbi->skb == NULL) {
587 rbi->skb = dev_alloc_skb(rbi->len +
589 if (unlikely(rbi->skb == NULL)) {
590 rq->stats.rx_buf_alloc_failure++;
593 rbi->skb->dev = adapter->netdev;
595 skb_reserve(rbi->skb, NET_IP_ALIGN);
596 rbi->dma_addr = pci_map_single(adapter->pdev,
597 rbi->skb->data, rbi->len,
600 /* rx buffer skipped by the device */
602 val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
604 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
605 rbi->len != PAGE_SIZE);
607 if (rbi->page == NULL) {
608 rbi->page = alloc_page(GFP_ATOMIC);
609 if (unlikely(rbi->page == NULL)) {
610 rq->stats.rx_buf_alloc_failure++;
613 rbi->dma_addr = pci_map_page(adapter->pdev,
614 rbi->page, 0, PAGE_SIZE,
617 /* rx buffers skipped by the device */
619 val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
622 BUG_ON(rbi->dma_addr == 0);
623 gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
624 gd->dword[2] = cpu_to_le32((!ring->gen << VMXNET3_RXD_GEN_SHIFT)
627 /* Fill the last buffer but dont mark it ready, or else the
628 * device will think that the queue is full */
629 if (num_allocated == num_to_alloc)
632 gd->dword[2] |= cpu_to_le32(ring->gen << VMXNET3_RXD_GEN_SHIFT);
634 vmxnet3_cmd_ring_adv_next2fill(ring);
636 rq->uncommitted[ring_idx] += num_allocated;
638 dev_dbg(&adapter->netdev->dev,
639 "alloc_rx_buf: %d allocated, next2fill %u, next2comp "
640 "%u, uncommited %u\n", num_allocated, ring->next2fill,
641 ring->next2comp, rq->uncommitted[ring_idx]);
643 /* so that the device can distinguish a full ring and an empty ring */
644 BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
646 return num_allocated;
651 vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
652 struct vmxnet3_rx_buf_info *rbi)
654 struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
655 skb_shinfo(skb)->nr_frags;
657 BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
659 frag->page = rbi->page;
660 frag->page_offset = 0;
661 frag->size = rcd->len;
662 skb->data_len += frag->size;
663 skb_shinfo(skb)->nr_frags++;
668 vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
669 struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
670 struct vmxnet3_adapter *adapter)
673 unsigned long buf_offset;
675 union Vmxnet3_GenericDesc *gdesc;
676 struct vmxnet3_tx_buf_info *tbi = NULL;
678 BUG_ON(ctx->copy_size > skb_headlen(skb));
680 /* use the previous gen bit for the SOP desc */
681 dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
683 ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
684 gdesc = ctx->sop_txd; /* both loops below can be skipped */
686 /* no need to map the buffer if headers are copied */
687 if (ctx->copy_size) {
688 ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
689 tq->tx_ring.next2fill *
690 sizeof(struct Vmxnet3_TxDataDesc));
691 ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
692 ctx->sop_txd->dword[3] = 0;
694 tbi = tq->buf_info + tq->tx_ring.next2fill;
695 tbi->map_type = VMXNET3_MAP_NONE;
697 dev_dbg(&adapter->netdev->dev,
698 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
699 tq->tx_ring.next2fill,
700 le64_to_cpu(ctx->sop_txd->txd.addr),
701 ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
702 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
704 /* use the right gen for non-SOP desc */
705 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
708 /* linear part can use multiple tx desc if it's big */
709 len = skb_headlen(skb) - ctx->copy_size;
710 buf_offset = ctx->copy_size;
714 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
718 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
719 /* spec says that for TxDesc.len, 0 == 2^14 */
722 tbi = tq->buf_info + tq->tx_ring.next2fill;
723 tbi->map_type = VMXNET3_MAP_SINGLE;
724 tbi->dma_addr = pci_map_single(adapter->pdev,
725 skb->data + buf_offset, buf_size,
730 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
731 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
733 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
734 gdesc->dword[2] = cpu_to_le32(dw2);
737 dev_dbg(&adapter->netdev->dev,
738 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
739 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
740 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
741 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
742 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
745 buf_offset += buf_size;
748 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
749 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
751 tbi = tq->buf_info + tq->tx_ring.next2fill;
752 tbi->map_type = VMXNET3_MAP_PAGE;
753 tbi->dma_addr = pci_map_page(adapter->pdev, frag->page,
754 frag->page_offset, frag->size,
757 tbi->len = frag->size;
759 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
760 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
762 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
763 gdesc->dword[2] = cpu_to_le32(dw2 | frag->size);
766 dev_dbg(&adapter->netdev->dev,
767 "txd[%u]: 0x%llu %u %u\n",
768 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
769 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
770 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
771 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
774 ctx->eop_txd = gdesc;
776 /* set the last buf_info for the pkt */
778 tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
782 /* Init all tx queues */
784 vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
788 for (i = 0; i < adapter->num_tx_queues; i++)
789 vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
794 * parse and copy relevant protocol headers:
795 * For a tso pkt, relevant headers are L2/3/4 including options
796 * For a pkt requesting csum offloading, they are L2/3 and may include L4
797 * if it's a TCP/UDP pkt
800 * -1: error happens during parsing
801 * 0: protocol headers parsed, but too big to be copied
802 * 1: protocol headers parsed and copied
805 * 1. related *ctx fields are updated.
806 * 2. ctx->copy_size is # of bytes copied
807 * 3. the portion copied is guaranteed to be in the linear part
811 vmxnet3_parse_and_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
812 struct vmxnet3_tx_ctx *ctx,
813 struct vmxnet3_adapter *adapter)
815 struct Vmxnet3_TxDataDesc *tdd;
817 if (ctx->mss) { /* TSO */
818 ctx->eth_ip_hdr_size = skb_transport_offset(skb);
819 ctx->l4_hdr_size = ((struct tcphdr *)
820 skb_transport_header(skb))->doff * 4;
821 ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
823 if (skb->ip_summed == CHECKSUM_PARTIAL) {
824 ctx->eth_ip_hdr_size = skb_checksum_start_offset(skb);
827 struct iphdr *iph = (struct iphdr *)
828 skb_network_header(skb);
829 if (iph->protocol == IPPROTO_TCP)
830 ctx->l4_hdr_size = ((struct tcphdr *)
831 skb_transport_header(skb))->doff * 4;
832 else if (iph->protocol == IPPROTO_UDP)
834 * Use tcp header size so that bytes to
835 * be copied are more than required by
839 sizeof(struct tcphdr);
841 ctx->l4_hdr_size = 0;
843 /* for simplicity, don't copy L4 headers */
844 ctx->l4_hdr_size = 0;
846 ctx->copy_size = ctx->eth_ip_hdr_size +
849 ctx->eth_ip_hdr_size = 0;
850 ctx->l4_hdr_size = 0;
851 /* copy as much as allowed */
852 ctx->copy_size = min((unsigned int)VMXNET3_HDR_COPY_SIZE
856 /* make sure headers are accessible directly */
857 if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
861 if (unlikely(ctx->copy_size > VMXNET3_HDR_COPY_SIZE)) {
862 tq->stats.oversized_hdr++;
867 tdd = tq->data_ring.base + tq->tx_ring.next2fill;
869 memcpy(tdd->data, skb->data, ctx->copy_size);
870 dev_dbg(&adapter->netdev->dev,
871 "copy %u bytes to dataRing[%u]\n",
872 ctx->copy_size, tq->tx_ring.next2fill);
881 vmxnet3_prepare_tso(struct sk_buff *skb,
882 struct vmxnet3_tx_ctx *ctx)
884 struct tcphdr *tcph = (struct tcphdr *)skb_transport_header(skb);
886 struct iphdr *iph = (struct iphdr *)skb_network_header(skb);
888 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
891 struct ipv6hdr *iph = (struct ipv6hdr *)skb_network_header(skb);
892 tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
899 * Transmits a pkt thru a given tq
901 * NETDEV_TX_OK: descriptors are setup successfully
902 * NETDEV_TX_OK: error occurred, the pkt is dropped
903 * NETDEV_TX_BUSY: tx ring is full, queue is stopped
906 * 1. tx ring may be changed
907 * 2. tq stats may be updated accordingly
908 * 3. shared->txNumDeferred may be updated
912 vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
913 struct vmxnet3_adapter *adapter, struct net_device *netdev)
918 struct vmxnet3_tx_ctx ctx;
919 union Vmxnet3_GenericDesc *gdesc;
920 #ifdef __BIG_ENDIAN_BITFIELD
921 /* Use temporary descriptor to avoid touching bits multiple times */
922 union Vmxnet3_GenericDesc tempTxDesc;
925 /* conservatively estimate # of descriptors to use */
926 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) +
927 skb_shinfo(skb)->nr_frags + 1;
929 ctx.ipv4 = (skb->protocol == cpu_to_be16(ETH_P_IP));
931 ctx.mss = skb_shinfo(skb)->gso_size;
933 if (skb_header_cloned(skb)) {
934 if (unlikely(pskb_expand_head(skb, 0, 0,
936 tq->stats.drop_tso++;
939 tq->stats.copy_skb_header++;
941 vmxnet3_prepare_tso(skb, &ctx);
943 if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
945 /* non-tso pkts must not use more than
946 * VMXNET3_MAX_TXD_PER_PKT entries
948 if (skb_linearize(skb) != 0) {
949 tq->stats.drop_too_many_frags++;
952 tq->stats.linearized++;
954 /* recalculate the # of descriptors to use */
955 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
959 spin_lock_irqsave(&tq->tx_lock, flags);
961 if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
962 tq->stats.tx_ring_full++;
963 dev_dbg(&adapter->netdev->dev,
964 "tx queue stopped on %s, next2comp %u"
965 " next2fill %u\n", adapter->netdev->name,
966 tq->tx_ring.next2comp, tq->tx_ring.next2fill);
968 vmxnet3_tq_stop(tq, adapter);
969 spin_unlock_irqrestore(&tq->tx_lock, flags);
970 return NETDEV_TX_BUSY;
974 ret = vmxnet3_parse_and_copy_hdr(skb, tq, &ctx, adapter);
976 BUG_ON(ret <= 0 && ctx.copy_size != 0);
977 /* hdrs parsed, check against other limits */
979 if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
980 VMXNET3_MAX_TX_BUF_SIZE)) {
984 if (skb->ip_summed == CHECKSUM_PARTIAL) {
985 if (unlikely(ctx.eth_ip_hdr_size +
987 VMXNET3_MAX_CSUM_OFFSET)) {
993 tq->stats.drop_hdr_inspect_err++;
994 goto unlock_drop_pkt;
997 /* fill tx descs related to addr & len */
998 vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter);
1000 /* setup the EOP desc */
1001 ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
1003 /* setup the SOP desc */
1004 #ifdef __BIG_ENDIAN_BITFIELD
1005 gdesc = &tempTxDesc;
1006 gdesc->dword[2] = ctx.sop_txd->dword[2];
1007 gdesc->dword[3] = ctx.sop_txd->dword[3];
1009 gdesc = ctx.sop_txd;
1012 gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
1013 gdesc->txd.om = VMXNET3_OM_TSO;
1014 gdesc->txd.msscof = ctx.mss;
1015 le32_add_cpu(&tq->shared->txNumDeferred, (skb->len -
1016 gdesc->txd.hlen + ctx.mss - 1) / ctx.mss);
1018 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1019 gdesc->txd.hlen = ctx.eth_ip_hdr_size;
1020 gdesc->txd.om = VMXNET3_OM_CSUM;
1021 gdesc->txd.msscof = ctx.eth_ip_hdr_size +
1025 gdesc->txd.msscof = 0;
1027 le32_add_cpu(&tq->shared->txNumDeferred, 1);
1030 if (vlan_tx_tag_present(skb)) {
1032 gdesc->txd.tci = vlan_tx_tag_get(skb);
1035 /* finally flips the GEN bit of the SOP desc. */
1036 gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
1038 #ifdef __BIG_ENDIAN_BITFIELD
1039 /* Finished updating in bitfields of Tx Desc, so write them in original
1042 vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
1043 (struct Vmxnet3_TxDesc *)ctx.sop_txd);
1044 gdesc = ctx.sop_txd;
1046 dev_dbg(&adapter->netdev->dev,
1047 "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
1048 (u32)((union Vmxnet3_GenericDesc *)ctx.sop_txd -
1049 tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
1050 le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
1052 spin_unlock_irqrestore(&tq->tx_lock, flags);
1054 if (le32_to_cpu(tq->shared->txNumDeferred) >=
1055 le32_to_cpu(tq->shared->txThreshold)) {
1056 tq->shared->txNumDeferred = 0;
1057 VMXNET3_WRITE_BAR0_REG(adapter,
1058 VMXNET3_REG_TXPROD + tq->qid * 8,
1059 tq->tx_ring.next2fill);
1062 return NETDEV_TX_OK;
1065 tq->stats.drop_oversized_hdr++;
1067 spin_unlock_irqrestore(&tq->tx_lock, flags);
1069 tq->stats.drop_total++;
1071 return NETDEV_TX_OK;
1076 vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1078 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1080 BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
1081 return vmxnet3_tq_xmit(skb,
1082 &adapter->tx_queue[skb->queue_mapping],
1088 vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
1089 struct sk_buff *skb,
1090 union Vmxnet3_GenericDesc *gdesc)
1092 if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
1093 /* typical case: TCP/UDP over IP and both csums are correct */
1094 if ((le32_to_cpu(gdesc->dword[3]) & VMXNET3_RCD_CSUM_OK) ==
1095 VMXNET3_RCD_CSUM_OK) {
1096 skb->ip_summed = CHECKSUM_UNNECESSARY;
1097 BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
1098 BUG_ON(!(gdesc->rcd.v4 || gdesc->rcd.v6));
1099 BUG_ON(gdesc->rcd.frg);
1101 if (gdesc->rcd.csum) {
1102 skb->csum = htons(gdesc->rcd.csum);
1103 skb->ip_summed = CHECKSUM_PARTIAL;
1105 skb_checksum_none_assert(skb);
1109 skb_checksum_none_assert(skb);
1115 vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
1116 struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
1118 rq->stats.drop_err++;
1120 rq->stats.drop_fcs++;
1122 rq->stats.drop_total++;
1125 * We do not unmap and chain the rx buffer to the skb.
1126 * We basically pretend this buffer is not used and will be recycled
1127 * by vmxnet3_rq_alloc_rx_buf()
1131 * ctx->skb may be NULL if this is the first and the only one
1135 dev_kfree_skb_irq(ctx->skb);
1142 vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
1143 struct vmxnet3_adapter *adapter, int quota)
1145 static const u32 rxprod_reg[2] = {
1146 VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2
1149 bool skip_page_frags = false;
1150 struct Vmxnet3_RxCompDesc *rcd;
1151 struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
1152 #ifdef __BIG_ENDIAN_BITFIELD
1153 struct Vmxnet3_RxDesc rxCmdDesc;
1154 struct Vmxnet3_RxCompDesc rxComp;
1156 vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
1158 while (rcd->gen == rq->comp_ring.gen) {
1159 struct vmxnet3_rx_buf_info *rbi;
1160 struct sk_buff *skb, *new_skb = NULL;
1161 struct page *new_page = NULL;
1163 struct Vmxnet3_RxDesc *rxd;
1165 struct vmxnet3_cmd_ring *ring = NULL;
1166 if (num_rxd >= quota) {
1167 /* we may stop even before we see the EOP desc of
1173 BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2);
1175 ring_idx = rcd->rqID < adapter->num_rx_queues ? 0 : 1;
1176 ring = rq->rx_ring + ring_idx;
1177 vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
1179 rbi = rq->buf_info[ring_idx] + idx;
1181 BUG_ON(rxd->addr != rbi->dma_addr ||
1182 rxd->len != rbi->len);
1184 if (unlikely(rcd->eop && rcd->err)) {
1185 vmxnet3_rx_error(rq, rcd, ctx, adapter);
1189 if (rcd->sop) { /* first buf of the pkt */
1190 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
1191 rcd->rqID != rq->qid);
1193 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
1194 BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
1196 if (unlikely(rcd->len == 0)) {
1197 /* Pretend the rx buffer is skipped. */
1198 BUG_ON(!(rcd->sop && rcd->eop));
1199 dev_dbg(&adapter->netdev->dev,
1200 "rxRing[%u][%u] 0 length\n",
1205 skip_page_frags = false;
1206 ctx->skb = rbi->skb;
1207 new_skb = dev_alloc_skb(rbi->len + NET_IP_ALIGN);
1208 if (new_skb == NULL) {
1209 /* Skb allocation failed, do not handover this
1210 * skb to stack. Reuse it. Drop the existing pkt
1212 rq->stats.rx_buf_alloc_failure++;
1214 rq->stats.drop_total++;
1215 skip_page_frags = true;
1219 pci_unmap_single(adapter->pdev, rbi->dma_addr, rbi->len,
1220 PCI_DMA_FROMDEVICE);
1222 skb_put(ctx->skb, rcd->len);
1224 /* Immediate refill */
1225 new_skb->dev = adapter->netdev;
1226 skb_reserve(new_skb, NET_IP_ALIGN);
1228 rbi->dma_addr = pci_map_single(adapter->pdev,
1229 rbi->skb->data, rbi->len,
1230 PCI_DMA_FROMDEVICE);
1231 rxd->addr = cpu_to_le64(rbi->dma_addr);
1232 rxd->len = rbi->len;
1235 BUG_ON(ctx->skb == NULL && !skip_page_frags);
1237 /* non SOP buffer must be type 1 in most cases */
1238 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE);
1239 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
1241 /* If an sop buffer was dropped, skip all
1242 * following non-sop fragments. They will be reused.
1244 if (skip_page_frags)
1247 new_page = alloc_page(GFP_ATOMIC);
1248 if (unlikely(new_page == NULL)) {
1249 /* Replacement page frag could not be allocated.
1250 * Reuse this page. Drop the pkt and free the
1251 * skb which contained this page as a frag. Skip
1252 * processing all the following non-sop frags.
1254 rq->stats.rx_buf_alloc_failure++;
1255 dev_kfree_skb(ctx->skb);
1257 skip_page_frags = true;
1262 pci_unmap_page(adapter->pdev,
1263 rbi->dma_addr, rbi->len,
1264 PCI_DMA_FROMDEVICE);
1266 vmxnet3_append_frag(ctx->skb, rcd, rbi);
1269 /* Immediate refill */
1270 rbi->page = new_page;
1271 rbi->dma_addr = pci_map_page(adapter->pdev, rbi->page,
1273 PCI_DMA_FROMDEVICE);
1274 rxd->addr = cpu_to_le64(rbi->dma_addr);
1275 rxd->len = rbi->len;
1281 skb->len += skb->data_len;
1282 skb->truesize += skb->data_len;
1284 vmxnet3_rx_csum(adapter, skb,
1285 (union Vmxnet3_GenericDesc *)rcd);
1286 skb->protocol = eth_type_trans(skb, adapter->netdev);
1288 if (unlikely(adapter->vlan_grp && rcd->ts)) {
1289 vlan_hwaccel_receive_skb(skb,
1290 adapter->vlan_grp, rcd->tci);
1292 netif_receive_skb(skb);
1299 /* device may have skipped some rx descs */
1300 ring->next2comp = idx;
1301 num_to_alloc = vmxnet3_cmd_ring_desc_avail(ring);
1302 ring = rq->rx_ring + ring_idx;
1303 while (num_to_alloc) {
1304 vmxnet3_getRxDesc(rxd, &ring->base[ring->next2fill].rxd,
1308 /* Recv desc is ready to be used by the device */
1309 rxd->gen = ring->gen;
1310 vmxnet3_cmd_ring_adv_next2fill(ring);
1314 /* if needed, update the register */
1315 if (unlikely(rq->shared->updateRxProd)) {
1316 VMXNET3_WRITE_BAR0_REG(adapter,
1317 rxprod_reg[ring_idx] + rq->qid * 8,
1319 rq->uncommitted[ring_idx] = 0;
1322 vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
1323 vmxnet3_getRxComp(rcd,
1324 &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
1332 vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
1333 struct vmxnet3_adapter *adapter)
1336 struct Vmxnet3_RxDesc *rxd;
1338 for (ring_idx = 0; ring_idx < 2; ring_idx++) {
1339 for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
1340 #ifdef __BIG_ENDIAN_BITFIELD
1341 struct Vmxnet3_RxDesc rxDesc;
1343 vmxnet3_getRxDesc(rxd,
1344 &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
1346 if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
1347 rq->buf_info[ring_idx][i].skb) {
1348 pci_unmap_single(adapter->pdev, rxd->addr,
1349 rxd->len, PCI_DMA_FROMDEVICE);
1350 dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
1351 rq->buf_info[ring_idx][i].skb = NULL;
1352 } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
1353 rq->buf_info[ring_idx][i].page) {
1354 pci_unmap_page(adapter->pdev, rxd->addr,
1355 rxd->len, PCI_DMA_FROMDEVICE);
1356 put_page(rq->buf_info[ring_idx][i].page);
1357 rq->buf_info[ring_idx][i].page = NULL;
1361 rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
1362 rq->rx_ring[ring_idx].next2fill =
1363 rq->rx_ring[ring_idx].next2comp = 0;
1364 rq->uncommitted[ring_idx] = 0;
1367 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1368 rq->comp_ring.next2proc = 0;
1373 vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
1377 for (i = 0; i < adapter->num_rx_queues; i++)
1378 vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
1382 void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
1383 struct vmxnet3_adapter *adapter)
1388 /* all rx buffers must have already been freed */
1389 for (i = 0; i < 2; i++) {
1390 if (rq->buf_info[i]) {
1391 for (j = 0; j < rq->rx_ring[i].size; j++)
1392 BUG_ON(rq->buf_info[i][j].page != NULL);
1397 kfree(rq->buf_info[0]);
1399 for (i = 0; i < 2; i++) {
1400 if (rq->rx_ring[i].base) {
1401 pci_free_consistent(adapter->pdev, rq->rx_ring[i].size
1402 * sizeof(struct Vmxnet3_RxDesc),
1403 rq->rx_ring[i].base,
1404 rq->rx_ring[i].basePA);
1405 rq->rx_ring[i].base = NULL;
1407 rq->buf_info[i] = NULL;
1410 if (rq->comp_ring.base) {
1411 pci_free_consistent(adapter->pdev, rq->comp_ring.size *
1412 sizeof(struct Vmxnet3_RxCompDesc),
1413 rq->comp_ring.base, rq->comp_ring.basePA);
1414 rq->comp_ring.base = NULL;
1420 vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
1421 struct vmxnet3_adapter *adapter)
1425 /* initialize buf_info */
1426 for (i = 0; i < rq->rx_ring[0].size; i++) {
1428 /* 1st buf for a pkt is skbuff */
1429 if (i % adapter->rx_buf_per_pkt == 0) {
1430 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
1431 rq->buf_info[0][i].len = adapter->skb_buf_size;
1432 } else { /* subsequent bufs for a pkt is frag */
1433 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
1434 rq->buf_info[0][i].len = PAGE_SIZE;
1437 for (i = 0; i < rq->rx_ring[1].size; i++) {
1438 rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
1439 rq->buf_info[1][i].len = PAGE_SIZE;
1442 /* reset internal state and allocate buffers for both rings */
1443 for (i = 0; i < 2; i++) {
1444 rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
1445 rq->uncommitted[i] = 0;
1447 memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
1448 sizeof(struct Vmxnet3_RxDesc));
1449 rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
1451 if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
1453 /* at least has 1 rx buffer for the 1st ring */
1456 vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
1458 /* reset the comp ring */
1459 rq->comp_ring.next2proc = 0;
1460 memset(rq->comp_ring.base, 0, rq->comp_ring.size *
1461 sizeof(struct Vmxnet3_RxCompDesc));
1462 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1465 rq->rx_ctx.skb = NULL;
1467 /* stats are not reset */
1473 vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
1477 for (i = 0; i < adapter->num_rx_queues; i++) {
1478 err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
1479 if (unlikely(err)) {
1480 dev_err(&adapter->netdev->dev, "%s: failed to "
1481 "initialize rx queue%i\n",
1482 adapter->netdev->name, i);
1492 vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
1496 struct vmxnet3_rx_buf_info *bi;
1498 for (i = 0; i < 2; i++) {
1500 sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
1501 rq->rx_ring[i].base = pci_alloc_consistent(adapter->pdev, sz,
1502 &rq->rx_ring[i].basePA);
1503 if (!rq->rx_ring[i].base) {
1504 printk(KERN_ERR "%s: failed to allocate rx ring %d\n",
1505 adapter->netdev->name, i);
1510 sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
1511 rq->comp_ring.base = pci_alloc_consistent(adapter->pdev, sz,
1512 &rq->comp_ring.basePA);
1513 if (!rq->comp_ring.base) {
1514 printk(KERN_ERR "%s: failed to allocate rx comp ring\n",
1515 adapter->netdev->name);
1519 sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
1520 rq->rx_ring[1].size);
1521 bi = kzalloc(sz, GFP_KERNEL);
1523 printk(KERN_ERR "%s: failed to allocate rx bufinfo\n",
1524 adapter->netdev->name);
1527 rq->buf_info[0] = bi;
1528 rq->buf_info[1] = bi + rq->rx_ring[0].size;
1533 vmxnet3_rq_destroy(rq, adapter);
1539 vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
1543 for (i = 0; i < adapter->num_rx_queues; i++) {
1544 err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
1545 if (unlikely(err)) {
1546 dev_err(&adapter->netdev->dev,
1547 "%s: failed to create rx queue%i\n",
1548 adapter->netdev->name, i);
1554 vmxnet3_rq_destroy_all(adapter);
1559 /* Multiple queue aware polling function for tx and rx */
1562 vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
1564 int rcd_done = 0, i;
1565 if (unlikely(adapter->shared->ecr))
1566 vmxnet3_process_events(adapter);
1567 for (i = 0; i < adapter->num_tx_queues; i++)
1568 vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
1570 for (i = 0; i < adapter->num_rx_queues; i++)
1571 rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
1578 vmxnet3_poll(struct napi_struct *napi, int budget)
1580 struct vmxnet3_rx_queue *rx_queue = container_of(napi,
1581 struct vmxnet3_rx_queue, napi);
1584 rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
1586 if (rxd_done < budget) {
1587 napi_complete(napi);
1588 vmxnet3_enable_all_intrs(rx_queue->adapter);
1594 * NAPI polling function for MSI-X mode with multiple Rx queues
1595 * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
1599 vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
1601 struct vmxnet3_rx_queue *rq = container_of(napi,
1602 struct vmxnet3_rx_queue, napi);
1603 struct vmxnet3_adapter *adapter = rq->adapter;
1606 /* When sharing interrupt with corresponding tx queue, process
1607 * tx completions in that queue as well
1609 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
1610 struct vmxnet3_tx_queue *tq =
1611 &adapter->tx_queue[rq - adapter->rx_queue];
1612 vmxnet3_tq_tx_complete(tq, adapter);
1615 rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
1617 if (rxd_done < budget) {
1618 napi_complete(napi);
1619 vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
1625 #ifdef CONFIG_PCI_MSI
1628 * Handle completion interrupts on tx queues
1629 * Returns whether or not the intr is handled
1633 vmxnet3_msix_tx(int irq, void *data)
1635 struct vmxnet3_tx_queue *tq = data;
1636 struct vmxnet3_adapter *adapter = tq->adapter;
1638 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1639 vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
1641 /* Handle the case where only one irq is allocate for all tx queues */
1642 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1644 for (i = 0; i < adapter->num_tx_queues; i++) {
1645 struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
1646 vmxnet3_tq_tx_complete(txq, adapter);
1649 vmxnet3_tq_tx_complete(tq, adapter);
1651 vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
1658 * Handle completion interrupts on rx queues. Returns whether or not the
1663 vmxnet3_msix_rx(int irq, void *data)
1665 struct vmxnet3_rx_queue *rq = data;
1666 struct vmxnet3_adapter *adapter = rq->adapter;
1668 /* disable intr if needed */
1669 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1670 vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
1671 napi_schedule(&rq->napi);
1677 *----------------------------------------------------------------------------
1679 * vmxnet3_msix_event --
1681 * vmxnet3 msix event intr handler
1684 * whether or not the intr is handled
1686 *----------------------------------------------------------------------------
1690 vmxnet3_msix_event(int irq, void *data)
1692 struct net_device *dev = data;
1693 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1695 /* disable intr if needed */
1696 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1697 vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
1699 if (adapter->shared->ecr)
1700 vmxnet3_process_events(adapter);
1702 vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
1707 #endif /* CONFIG_PCI_MSI */
1710 /* Interrupt handler for vmxnet3 */
1712 vmxnet3_intr(int irq, void *dev_id)
1714 struct net_device *dev = dev_id;
1715 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1717 if (adapter->intr.type == VMXNET3_IT_INTX) {
1718 u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
1719 if (unlikely(icr == 0))
1725 /* disable intr if needed */
1726 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1727 vmxnet3_disable_all_intrs(adapter);
1729 napi_schedule(&adapter->rx_queue[0].napi);
1734 #ifdef CONFIG_NET_POLL_CONTROLLER
1736 /* netpoll callback. */
1738 vmxnet3_netpoll(struct net_device *netdev)
1740 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1742 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1743 vmxnet3_disable_all_intrs(adapter);
1745 vmxnet3_do_poll(adapter, adapter->rx_queue[0].rx_ring[0].size);
1746 vmxnet3_enable_all_intrs(adapter);
1749 #endif /* CONFIG_NET_POLL_CONTROLLER */
1752 vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
1754 struct vmxnet3_intr *intr = &adapter->intr;
1758 #ifdef CONFIG_PCI_MSI
1759 if (adapter->intr.type == VMXNET3_IT_MSIX) {
1760 for (i = 0; i < adapter->num_tx_queues; i++) {
1761 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1762 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
1763 adapter->netdev->name, vector);
1765 intr->msix_entries[vector].vector,
1767 adapter->tx_queue[i].name,
1768 &adapter->tx_queue[i]);
1770 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
1771 adapter->netdev->name, vector);
1774 dev_err(&adapter->netdev->dev,
1775 "Failed to request irq for MSIX, %s, "
1777 adapter->tx_queue[i].name, err);
1781 /* Handle the case where only 1 MSIx was allocated for
1783 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1784 for (; i < adapter->num_tx_queues; i++)
1785 adapter->tx_queue[i].comp_ring.intr_idx
1790 adapter->tx_queue[i].comp_ring.intr_idx
1794 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
1797 for (i = 0; i < adapter->num_rx_queues; i++) {
1798 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
1799 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
1800 adapter->netdev->name, vector);
1802 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
1803 adapter->netdev->name, vector);
1804 err = request_irq(intr->msix_entries[vector].vector,
1806 adapter->rx_queue[i].name,
1807 &(adapter->rx_queue[i]));
1809 printk(KERN_ERR "Failed to request irq for MSIX"
1811 adapter->rx_queue[i].name, err);
1815 adapter->rx_queue[i].comp_ring.intr_idx = vector++;
1818 sprintf(intr->event_msi_vector_name, "%s-event-%d",
1819 adapter->netdev->name, vector);
1820 err = request_irq(intr->msix_entries[vector].vector,
1821 vmxnet3_msix_event, 0,
1822 intr->event_msi_vector_name, adapter->netdev);
1823 intr->event_intr_idx = vector;
1825 } else if (intr->type == VMXNET3_IT_MSI) {
1826 adapter->num_rx_queues = 1;
1827 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
1828 adapter->netdev->name, adapter->netdev);
1831 adapter->num_rx_queues = 1;
1832 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
1833 IRQF_SHARED, adapter->netdev->name,
1835 #ifdef CONFIG_PCI_MSI
1838 intr->num_intrs = vector + 1;
1840 printk(KERN_ERR "Failed to request irq %s (intr type:%d), error"
1841 ":%d\n", adapter->netdev->name, intr->type, err);
1843 /* Number of rx queues will not change after this */
1844 for (i = 0; i < adapter->num_rx_queues; i++) {
1845 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
1847 rq->qid2 = i + adapter->num_rx_queues;
1852 /* init our intr settings */
1853 for (i = 0; i < intr->num_intrs; i++)
1854 intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
1855 if (adapter->intr.type != VMXNET3_IT_MSIX) {
1856 adapter->intr.event_intr_idx = 0;
1857 for (i = 0; i < adapter->num_tx_queues; i++)
1858 adapter->tx_queue[i].comp_ring.intr_idx = 0;
1859 adapter->rx_queue[0].comp_ring.intr_idx = 0;
1862 printk(KERN_INFO "%s: intr type %u, mode %u, %u vectors "
1863 "allocated\n", adapter->netdev->name, intr->type,
1864 intr->mask_mode, intr->num_intrs);
1872 vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
1874 struct vmxnet3_intr *intr = &adapter->intr;
1875 BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
1877 switch (intr->type) {
1878 #ifdef CONFIG_PCI_MSI
1879 case VMXNET3_IT_MSIX:
1883 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1884 for (i = 0; i < adapter->num_tx_queues; i++) {
1885 free_irq(intr->msix_entries[vector++].vector,
1886 &(adapter->tx_queue[i]));
1887 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
1892 for (i = 0; i < adapter->num_rx_queues; i++) {
1893 free_irq(intr->msix_entries[vector++].vector,
1894 &(adapter->rx_queue[i]));
1897 free_irq(intr->msix_entries[vector].vector,
1899 BUG_ON(vector >= intr->num_intrs);
1903 case VMXNET3_IT_MSI:
1904 free_irq(adapter->pdev->irq, adapter->netdev);
1906 case VMXNET3_IT_INTX:
1907 free_irq(adapter->pdev->irq, adapter->netdev);
1915 vmxnet3_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
1917 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1918 struct Vmxnet3_DriverShared *shared = adapter->shared;
1919 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1920 unsigned long flags;
1923 /* add vlan rx stripping. */
1924 if (adapter->netdev->features & NETIF_F_HW_VLAN_RX) {
1926 adapter->vlan_grp = grp;
1929 * Clear entire vfTable; then enable untagged pkts.
1930 * Note: setting one entry in vfTable to non-zero turns
1931 * on VLAN rx filtering.
1933 for (i = 0; i < VMXNET3_VFT_SIZE; i++)
1936 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1937 spin_lock_irqsave(&adapter->cmd_lock, flags);
1938 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1939 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1940 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1942 printk(KERN_ERR "%s: vlan_rx_register when device has "
1943 "no NETIF_F_HW_VLAN_RX\n", netdev->name);
1946 /* remove vlan rx stripping. */
1947 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
1948 adapter->vlan_grp = NULL;
1950 if (devRead->misc.uptFeatures & UPT1_F_RXVLAN) {
1953 for (i = 0; i < VMXNET3_VFT_SIZE; i++) {
1954 /* clear entire vfTable; this also disables
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);
1969 vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
1971 if (adapter->vlan_grp) {
1973 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1974 bool activeVlan = false;
1976 for (vid = 0; vid < VLAN_N_VID; vid++) {
1977 if (vlan_group_get_device(adapter->vlan_grp, vid)) {
1978 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1983 /* continue to allow untagged pkts */
1984 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1991 vmxnet3_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1993 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1994 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1995 unsigned long flags;
1997 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1998 spin_lock_irqsave(&adapter->cmd_lock, flags);
1999 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2000 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
2001 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2006 vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
2008 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2009 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2010 unsigned long flags;
2012 VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
2013 spin_lock_irqsave(&adapter->cmd_lock, flags);
2014 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2015 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
2016 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2021 vmxnet3_copy_mc(struct net_device *netdev)
2024 u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
2026 /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
2028 /* We may be called with BH disabled */
2029 buf = kmalloc(sz, GFP_ATOMIC);
2031 struct netdev_hw_addr *ha;
2034 netdev_for_each_mc_addr(ha, netdev)
2035 memcpy(buf + i++ * ETH_ALEN, ha->addr,
2044 vmxnet3_set_mc(struct net_device *netdev)
2046 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2047 unsigned long flags;
2048 struct Vmxnet3_RxFilterConf *rxConf =
2049 &adapter->shared->devRead.rxFilterConf;
2050 u8 *new_table = NULL;
2051 u32 new_mode = VMXNET3_RXM_UCAST;
2053 if (netdev->flags & IFF_PROMISC)
2054 new_mode |= VMXNET3_RXM_PROMISC;
2056 if (netdev->flags & IFF_BROADCAST)
2057 new_mode |= VMXNET3_RXM_BCAST;
2059 if (netdev->flags & IFF_ALLMULTI)
2060 new_mode |= VMXNET3_RXM_ALL_MULTI;
2062 if (!netdev_mc_empty(netdev)) {
2063 new_table = vmxnet3_copy_mc(netdev);
2065 new_mode |= VMXNET3_RXM_MCAST;
2066 rxConf->mfTableLen = cpu_to_le16(
2067 netdev_mc_count(netdev) * ETH_ALEN);
2068 rxConf->mfTablePA = cpu_to_le64(virt_to_phys(
2071 printk(KERN_INFO "%s: failed to copy mcast list"
2072 ", setting ALL_MULTI\n", netdev->name);
2073 new_mode |= VMXNET3_RXM_ALL_MULTI;
2078 if (!(new_mode & VMXNET3_RXM_MCAST)) {
2079 rxConf->mfTableLen = 0;
2080 rxConf->mfTablePA = 0;
2083 spin_lock_irqsave(&adapter->cmd_lock, flags);
2084 if (new_mode != rxConf->rxMode) {
2085 rxConf->rxMode = cpu_to_le32(new_mode);
2086 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2087 VMXNET3_CMD_UPDATE_RX_MODE);
2090 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2091 VMXNET3_CMD_UPDATE_MAC_FILTERS);
2092 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2098 vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
2102 for (i = 0; i < adapter->num_rx_queues; i++)
2103 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2108 * Set up driver_shared based on settings in adapter.
2112 vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2114 struct Vmxnet3_DriverShared *shared = adapter->shared;
2115 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2116 struct Vmxnet3_TxQueueConf *tqc;
2117 struct Vmxnet3_RxQueueConf *rqc;
2120 memset(shared, 0, sizeof(*shared));
2122 /* driver settings */
2123 shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2124 devRead->misc.driverInfo.version = cpu_to_le32(
2125 VMXNET3_DRIVER_VERSION_NUM);
2126 devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2127 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2128 devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
2129 *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2130 *((u32 *)&devRead->misc.driverInfo.gos));
2131 devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2132 devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
2134 devRead->misc.ddPA = cpu_to_le64(virt_to_phys(adapter));
2135 devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
2137 /* set up feature flags */
2138 if (adapter->netdev->features & NETIF_F_RXCSUM)
2139 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
2141 if (adapter->netdev->features & NETIF_F_LRO) {
2142 devRead->misc.uptFeatures |= UPT1_F_LRO;
2143 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
2145 if (adapter->netdev->features & NETIF_F_HW_VLAN_RX)
2146 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
2148 devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2149 devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2150 devRead->misc.queueDescLen = cpu_to_le32(
2151 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2152 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
2154 /* tx queue settings */
2155 devRead->misc.numTxQueues = adapter->num_tx_queues;
2156 for (i = 0; i < adapter->num_tx_queues; i++) {
2157 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2158 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2159 tqc = &adapter->tqd_start[i].conf;
2160 tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
2161 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2162 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
2163 tqc->ddPA = cpu_to_le64(virt_to_phys(tq->buf_info));
2164 tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
2165 tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
2166 tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
2167 tqc->ddLen = cpu_to_le32(
2168 sizeof(struct vmxnet3_tx_buf_info) *
2170 tqc->intrIdx = tq->comp_ring.intr_idx;
2173 /* rx queue settings */
2174 devRead->misc.numRxQueues = adapter->num_rx_queues;
2175 for (i = 0; i < adapter->num_rx_queues; i++) {
2176 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2177 rqc = &adapter->rqd_start[i].conf;
2178 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2179 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2180 rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
2181 rqc->ddPA = cpu_to_le64(virt_to_phys(
2183 rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
2184 rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
2185 rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
2186 rqc->ddLen = cpu_to_le32(
2187 sizeof(struct vmxnet3_rx_buf_info) *
2188 (rqc->rxRingSize[0] +
2189 rqc->rxRingSize[1]));
2190 rqc->intrIdx = rq->comp_ring.intr_idx;
2194 memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2197 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
2198 devRead->misc.uptFeatures |= UPT1_F_RSS;
2199 devRead->misc.numRxQueues = adapter->num_rx_queues;
2200 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2201 UPT1_RSS_HASH_TYPE_IPV4 |
2202 UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2203 UPT1_RSS_HASH_TYPE_IPV6;
2204 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2205 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2206 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
2207 get_random_bytes(&rssConf->hashKey[0], rssConf->hashKeySize);
2208 for (i = 0; i < rssConf->indTableSize; i++)
2209 rssConf->indTable[i] = i % adapter->num_rx_queues;
2211 devRead->rssConfDesc.confVer = 1;
2212 devRead->rssConfDesc.confLen = sizeof(*rssConf);
2213 devRead->rssConfDesc.confPA = virt_to_phys(rssConf);
2216 #endif /* VMXNET3_RSS */
2219 devRead->intrConf.autoMask = adapter->intr.mask_mode ==
2221 devRead->intrConf.numIntrs = adapter->intr.num_intrs;
2222 for (i = 0; i < adapter->intr.num_intrs; i++)
2223 devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
2225 devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
2226 devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
2228 /* rx filter settings */
2229 devRead->rxFilterConf.rxMode = 0;
2230 vmxnet3_restore_vlan(adapter);
2231 vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
2233 /* the rest are already zeroed */
2238 vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
2242 unsigned long flags;
2244 dev_dbg(&adapter->netdev->dev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
2245 " ring sizes %u %u %u\n", adapter->netdev->name,
2246 adapter->skb_buf_size, adapter->rx_buf_per_pkt,
2247 adapter->tx_queue[0].tx_ring.size,
2248 adapter->rx_queue[0].rx_ring[0].size,
2249 adapter->rx_queue[0].rx_ring[1].size);
2251 vmxnet3_tq_init_all(adapter);
2252 err = vmxnet3_rq_init_all(adapter);
2254 printk(KERN_ERR "Failed to init rx queue for %s: error %d\n",
2255 adapter->netdev->name, err);
2259 err = vmxnet3_request_irqs(adapter);
2261 printk(KERN_ERR "Failed to setup irq for %s: error %d\n",
2262 adapter->netdev->name, err);
2266 vmxnet3_setup_driver_shared(adapter);
2268 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
2269 adapter->shared_pa));
2270 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
2271 adapter->shared_pa));
2272 spin_lock_irqsave(&adapter->cmd_lock, flags);
2273 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2274 VMXNET3_CMD_ACTIVATE_DEV);
2275 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
2276 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2279 printk(KERN_ERR "Failed to activate dev %s: error %u\n",
2280 adapter->netdev->name, ret);
2285 for (i = 0; i < adapter->num_rx_queues; i++) {
2286 VMXNET3_WRITE_BAR0_REG(adapter,
2287 VMXNET3_REG_RXPROD + i * VMXNET3_REG_ALIGN,
2288 adapter->rx_queue[i].rx_ring[0].next2fill);
2289 VMXNET3_WRITE_BAR0_REG(adapter, (VMXNET3_REG_RXPROD2 +
2290 (i * VMXNET3_REG_ALIGN)),
2291 adapter->rx_queue[i].rx_ring[1].next2fill);
2294 /* Apply the rx filter settins last. */
2295 vmxnet3_set_mc(adapter->netdev);
2298 * Check link state when first activating device. It will start the
2299 * tx queue if the link is up.
2301 vmxnet3_check_link(adapter, true);
2302 for (i = 0; i < adapter->num_rx_queues; i++)
2303 napi_enable(&adapter->rx_queue[i].napi);
2304 vmxnet3_enable_all_intrs(adapter);
2305 clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
2309 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
2310 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
2311 vmxnet3_free_irqs(adapter);
2314 /* free up buffers we allocated */
2315 vmxnet3_rq_cleanup_all(adapter);
2321 vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
2323 unsigned long flags;
2324 spin_lock_irqsave(&adapter->cmd_lock, flags);
2325 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
2326 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2331 vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
2334 unsigned long flags;
2335 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
2339 spin_lock_irqsave(&adapter->cmd_lock, flags);
2340 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2341 VMXNET3_CMD_QUIESCE_DEV);
2342 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2343 vmxnet3_disable_all_intrs(adapter);
2345 for (i = 0; i < adapter->num_rx_queues; i++)
2346 napi_disable(&adapter->rx_queue[i].napi);
2347 netif_tx_disable(adapter->netdev);
2348 adapter->link_speed = 0;
2349 netif_carrier_off(adapter->netdev);
2351 vmxnet3_tq_cleanup_all(adapter);
2352 vmxnet3_rq_cleanup_all(adapter);
2353 vmxnet3_free_irqs(adapter);
2359 vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2364 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
2366 tmp = (mac[5] << 8) | mac[4];
2367 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
2372 vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
2374 struct sockaddr *addr = p;
2375 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2377 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2378 vmxnet3_write_mac_addr(adapter, addr->sa_data);
2384 /* ==================== initialization and cleanup routines ============ */
2387 vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
2390 unsigned long mmio_start, mmio_len;
2391 struct pci_dev *pdev = adapter->pdev;
2393 err = pci_enable_device(pdev);
2395 printk(KERN_ERR "Failed to enable adapter %s: error %d\n",
2396 pci_name(pdev), err);
2400 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
2401 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
2402 printk(KERN_ERR "pci_set_consistent_dma_mask failed "
2403 "for adapter %s\n", pci_name(pdev));
2409 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
2410 printk(KERN_ERR "pci_set_dma_mask failed for adapter "
2411 "%s\n", pci_name(pdev));
2418 err = pci_request_selected_regions(pdev, (1 << 2) - 1,
2419 vmxnet3_driver_name);
2421 printk(KERN_ERR "Failed to request region for adapter %s: "
2422 "error %d\n", pci_name(pdev), err);
2426 pci_set_master(pdev);
2428 mmio_start = pci_resource_start(pdev, 0);
2429 mmio_len = pci_resource_len(pdev, 0);
2430 adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
2431 if (!adapter->hw_addr0) {
2432 printk(KERN_ERR "Failed to map bar0 for adapter %s\n",
2438 mmio_start = pci_resource_start(pdev, 1);
2439 mmio_len = pci_resource_len(pdev, 1);
2440 adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
2441 if (!adapter->hw_addr1) {
2442 printk(KERN_ERR "Failed to map bar1 for adapter %s\n",
2450 iounmap(adapter->hw_addr0);
2452 pci_release_selected_regions(pdev, (1 << 2) - 1);
2454 pci_disable_device(pdev);
2460 vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
2462 BUG_ON(!adapter->pdev);
2464 iounmap(adapter->hw_addr0);
2465 iounmap(adapter->hw_addr1);
2466 pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
2467 pci_disable_device(adapter->pdev);
2472 vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
2474 size_t sz, i, ring0_size, ring1_size, comp_size;
2475 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[0];
2478 if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
2479 VMXNET3_MAX_ETH_HDR_SIZE) {
2480 adapter->skb_buf_size = adapter->netdev->mtu +
2481 VMXNET3_MAX_ETH_HDR_SIZE;
2482 if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
2483 adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
2485 adapter->rx_buf_per_pkt = 1;
2487 adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
2488 sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
2489 VMXNET3_MAX_ETH_HDR_SIZE;
2490 adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
2494 * for simplicity, force the ring0 size to be a multiple of
2495 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
2497 sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
2498 ring0_size = adapter->rx_queue[0].rx_ring[0].size;
2499 ring0_size = (ring0_size + sz - 1) / sz * sz;
2500 ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
2502 ring1_size = adapter->rx_queue[0].rx_ring[1].size;
2503 comp_size = ring0_size + ring1_size;
2505 for (i = 0; i < adapter->num_rx_queues; i++) {
2506 rq = &adapter->rx_queue[i];
2507 rq->rx_ring[0].size = ring0_size;
2508 rq->rx_ring[1].size = ring1_size;
2509 rq->comp_ring.size = comp_size;
2515 vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
2516 u32 rx_ring_size, u32 rx_ring2_size)
2520 for (i = 0; i < adapter->num_tx_queues; i++) {
2521 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2522 tq->tx_ring.size = tx_ring_size;
2523 tq->data_ring.size = tx_ring_size;
2524 tq->comp_ring.size = tx_ring_size;
2525 tq->shared = &adapter->tqd_start[i].ctrl;
2527 tq->adapter = adapter;
2529 err = vmxnet3_tq_create(tq, adapter);
2531 * Too late to change num_tx_queues. We cannot do away with
2532 * lesser number of queues than what we asked for
2538 adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
2539 adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
2540 vmxnet3_adjust_rx_ring_size(adapter);
2541 for (i = 0; i < adapter->num_rx_queues; i++) {
2542 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2543 /* qid and qid2 for rx queues will be assigned later when num
2544 * of rx queues is finalized after allocating intrs */
2545 rq->shared = &adapter->rqd_start[i].ctrl;
2546 rq->adapter = adapter;
2547 err = vmxnet3_rq_create(rq, adapter);
2550 printk(KERN_ERR "Could not allocate any rx"
2551 "queues. Aborting.\n");
2554 printk(KERN_INFO "Number of rx queues changed "
2556 adapter->num_rx_queues = i;
2564 vmxnet3_tq_destroy_all(adapter);
2569 vmxnet3_open(struct net_device *netdev)
2571 struct vmxnet3_adapter *adapter;
2574 adapter = netdev_priv(netdev);
2576 for (i = 0; i < adapter->num_tx_queues; i++)
2577 spin_lock_init(&adapter->tx_queue[i].tx_lock);
2579 err = vmxnet3_create_queues(adapter, VMXNET3_DEF_TX_RING_SIZE,
2580 VMXNET3_DEF_RX_RING_SIZE,
2581 VMXNET3_DEF_RX_RING_SIZE);
2585 err = vmxnet3_activate_dev(adapter);
2592 vmxnet3_rq_destroy_all(adapter);
2593 vmxnet3_tq_destroy_all(adapter);
2600 vmxnet3_close(struct net_device *netdev)
2602 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2605 * Reset_work may be in the middle of resetting the device, wait for its
2608 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2611 vmxnet3_quiesce_dev(adapter);
2613 vmxnet3_rq_destroy_all(adapter);
2614 vmxnet3_tq_destroy_all(adapter);
2616 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2624 vmxnet3_force_close(struct vmxnet3_adapter *adapter)
2629 * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
2630 * vmxnet3_close() will deadlock.
2632 BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
2634 /* we need to enable NAPI, otherwise dev_close will deadlock */
2635 for (i = 0; i < adapter->num_rx_queues; i++)
2636 napi_enable(&adapter->rx_queue[i].napi);
2637 dev_close(adapter->netdev);
2642 vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
2644 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2647 if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
2650 netdev->mtu = new_mtu;
2653 * Reset_work may be in the middle of resetting the device, wait for its
2656 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2659 if (netif_running(netdev)) {
2660 vmxnet3_quiesce_dev(adapter);
2661 vmxnet3_reset_dev(adapter);
2663 /* we need to re-create the rx queue based on the new mtu */
2664 vmxnet3_rq_destroy_all(adapter);
2665 vmxnet3_adjust_rx_ring_size(adapter);
2666 err = vmxnet3_rq_create_all(adapter);
2668 printk(KERN_ERR "%s: failed to re-create rx queues,"
2669 " error %d. Closing it.\n", netdev->name, err);
2673 err = vmxnet3_activate_dev(adapter);
2675 printk(KERN_ERR "%s: failed to re-activate, error %d. "
2676 "Closing it\n", netdev->name, err);
2682 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2684 vmxnet3_force_close(adapter);
2691 vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
2693 struct net_device *netdev = adapter->netdev;
2695 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
2696 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX |
2697 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_LRO;
2699 netdev->features |= NETIF_F_HIGHDMA;
2700 netdev->vlan_features = netdev->hw_features & ~NETIF_F_HW_VLAN_TX;
2701 netdev->features = netdev->hw_features |
2702 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
2704 netdev_info(adapter->netdev,
2705 "features: sg csum vlan jf tso tsoIPv6 lro%s\n",
2706 dma64 ? " highDMA" : "");
2711 vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2715 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
2718 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
2719 mac[4] = tmp & 0xff;
2720 mac[5] = (tmp >> 8) & 0xff;
2723 #ifdef CONFIG_PCI_MSI
2726 * Enable MSIx vectors.
2728 * 0 on successful enabling of required vectors,
2729 * VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
2731 * number of vectors which can be enabled otherwise (this number is smaller
2732 * than VMXNET3_LINUX_MIN_MSIX_VECT)
2736 vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter,
2739 int err = 0, vector_threshold;
2740 vector_threshold = VMXNET3_LINUX_MIN_MSIX_VECT;
2742 while (vectors >= vector_threshold) {
2743 err = pci_enable_msix(adapter->pdev, adapter->intr.msix_entries,
2746 adapter->intr.num_intrs = vectors;
2748 } else if (err < 0) {
2749 printk(KERN_ERR "Failed to enable MSI-X for %s, error"
2750 " %d\n", adapter->netdev->name, err);
2752 } else if (err < vector_threshold) {
2755 /* If fails to enable required number of MSI-x vectors
2756 * try enabling minimum number of vectors required.
2758 vectors = vector_threshold;
2759 printk(KERN_ERR "Failed to enable %d MSI-X for %s, try"
2760 " %d instead\n", vectors, adapter->netdev->name,
2765 printk(KERN_INFO "Number of MSI-X interrupts which can be allocatedi"
2766 " are lower than min threshold required.\n");
2771 #endif /* CONFIG_PCI_MSI */
2774 vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
2777 unsigned long flags;
2780 spin_lock_irqsave(&adapter->cmd_lock, flags);
2781 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2782 VMXNET3_CMD_GET_CONF_INTR);
2783 cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
2784 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2785 adapter->intr.type = cfg & 0x3;
2786 adapter->intr.mask_mode = (cfg >> 2) & 0x3;
2788 if (adapter->intr.type == VMXNET3_IT_AUTO) {
2789 adapter->intr.type = VMXNET3_IT_MSIX;
2792 #ifdef CONFIG_PCI_MSI
2793 if (adapter->intr.type == VMXNET3_IT_MSIX) {
2794 int vector, err = 0;
2796 adapter->intr.num_intrs = (adapter->share_intr ==
2797 VMXNET3_INTR_TXSHARE) ? 1 :
2798 adapter->num_tx_queues;
2799 adapter->intr.num_intrs += (adapter->share_intr ==
2800 VMXNET3_INTR_BUDDYSHARE) ? 0 :
2801 adapter->num_rx_queues;
2802 adapter->intr.num_intrs += 1; /* for link event */
2804 adapter->intr.num_intrs = (adapter->intr.num_intrs >
2805 VMXNET3_LINUX_MIN_MSIX_VECT
2806 ? adapter->intr.num_intrs :
2807 VMXNET3_LINUX_MIN_MSIX_VECT);
2809 for (vector = 0; vector < adapter->intr.num_intrs; vector++)
2810 adapter->intr.msix_entries[vector].entry = vector;
2812 err = vmxnet3_acquire_msix_vectors(adapter,
2813 adapter->intr.num_intrs);
2814 /* If we cannot allocate one MSIx vector per queue
2815 * then limit the number of rx queues to 1
2817 if (err == VMXNET3_LINUX_MIN_MSIX_VECT) {
2818 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
2819 || adapter->num_rx_queues != 1) {
2820 adapter->share_intr = VMXNET3_INTR_TXSHARE;
2821 printk(KERN_ERR "Number of rx queues : 1\n");
2822 adapter->num_rx_queues = 1;
2823 adapter->intr.num_intrs =
2824 VMXNET3_LINUX_MIN_MSIX_VECT;
2831 /* If we cannot allocate MSIx vectors use only one rx queue */
2832 printk(KERN_INFO "Failed to enable MSI-X for %s, error %d."
2833 "#rx queues : 1, try MSI\n", adapter->netdev->name, err);
2835 adapter->intr.type = VMXNET3_IT_MSI;
2838 if (adapter->intr.type == VMXNET3_IT_MSI) {
2840 err = pci_enable_msi(adapter->pdev);
2842 adapter->num_rx_queues = 1;
2843 adapter->intr.num_intrs = 1;
2847 #endif /* CONFIG_PCI_MSI */
2849 adapter->num_rx_queues = 1;
2850 printk(KERN_INFO "Using INTx interrupt, #Rx queues: 1.\n");
2851 adapter->intr.type = VMXNET3_IT_INTX;
2853 /* INT-X related setting */
2854 adapter->intr.num_intrs = 1;
2859 vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
2861 if (adapter->intr.type == VMXNET3_IT_MSIX)
2862 pci_disable_msix(adapter->pdev);
2863 else if (adapter->intr.type == VMXNET3_IT_MSI)
2864 pci_disable_msi(adapter->pdev);
2866 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
2871 vmxnet3_tx_timeout(struct net_device *netdev)
2873 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2874 adapter->tx_timeout_count++;
2876 printk(KERN_ERR "%s: tx hang\n", adapter->netdev->name);
2877 schedule_work(&adapter->work);
2878 netif_wake_queue(adapter->netdev);
2883 vmxnet3_reset_work(struct work_struct *data)
2885 struct vmxnet3_adapter *adapter;
2887 adapter = container_of(data, struct vmxnet3_adapter, work);
2889 /* if another thread is resetting the device, no need to proceed */
2890 if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2893 /* if the device is closed, we must leave it alone */
2895 if (netif_running(adapter->netdev)) {
2896 printk(KERN_INFO "%s: resetting\n", adapter->netdev->name);
2897 vmxnet3_quiesce_dev(adapter);
2898 vmxnet3_reset_dev(adapter);
2899 vmxnet3_activate_dev(adapter);
2901 printk(KERN_INFO "%s: already closed\n", adapter->netdev->name);
2905 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2909 static int __devinit
2910 vmxnet3_probe_device(struct pci_dev *pdev,
2911 const struct pci_device_id *id)
2913 static const struct net_device_ops vmxnet3_netdev_ops = {
2914 .ndo_open = vmxnet3_open,
2915 .ndo_stop = vmxnet3_close,
2916 .ndo_start_xmit = vmxnet3_xmit_frame,
2917 .ndo_set_mac_address = vmxnet3_set_mac_addr,
2918 .ndo_change_mtu = vmxnet3_change_mtu,
2919 .ndo_set_features = vmxnet3_set_features,
2920 .ndo_get_stats = vmxnet3_get_stats,
2921 .ndo_tx_timeout = vmxnet3_tx_timeout,
2922 .ndo_set_multicast_list = vmxnet3_set_mc,
2923 .ndo_vlan_rx_register = vmxnet3_vlan_rx_register,
2924 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
2925 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
2926 #ifdef CONFIG_NET_POLL_CONTROLLER
2927 .ndo_poll_controller = vmxnet3_netpoll,
2931 bool dma64 = false; /* stupid gcc */
2933 struct net_device *netdev;
2934 struct vmxnet3_adapter *adapter;
2940 if (!pci_msi_enabled())
2945 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
2946 (int)num_online_cpus());
2950 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
2953 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
2954 (int)num_online_cpus());
2958 num_tx_queues = rounddown_pow_of_two(num_tx_queues);
2959 netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
2960 max(num_tx_queues, num_rx_queues));
2961 printk(KERN_INFO "# of Tx queues : %d, # of Rx queues : %d\n",
2962 num_tx_queues, num_rx_queues);
2965 printk(KERN_ERR "Failed to alloc ethernet device for adapter "
2966 "%s\n", pci_name(pdev));
2970 pci_set_drvdata(pdev, netdev);
2971 adapter = netdev_priv(netdev);
2972 adapter->netdev = netdev;
2973 adapter->pdev = pdev;
2975 spin_lock_init(&adapter->cmd_lock);
2976 adapter->shared = pci_alloc_consistent(adapter->pdev,
2977 sizeof(struct Vmxnet3_DriverShared),
2978 &adapter->shared_pa);
2979 if (!adapter->shared) {
2980 printk(KERN_ERR "Failed to allocate memory for %s\n",
2983 goto err_alloc_shared;
2986 adapter->num_rx_queues = num_rx_queues;
2987 adapter->num_tx_queues = num_tx_queues;
2989 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
2990 size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
2991 adapter->tqd_start = pci_alloc_consistent(adapter->pdev, size,
2992 &adapter->queue_desc_pa);
2994 if (!adapter->tqd_start) {
2995 printk(KERN_ERR "Failed to allocate memory for %s\n",
2998 goto err_alloc_queue_desc;
3000 adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
3001 adapter->num_tx_queues);
3003 adapter->pm_conf = kmalloc(sizeof(struct Vmxnet3_PMConf), GFP_KERNEL);
3004 if (adapter->pm_conf == NULL) {
3005 printk(KERN_ERR "Failed to allocate memory for %s\n",
3013 adapter->rss_conf = kmalloc(sizeof(struct UPT1_RSSConf), GFP_KERNEL);
3014 if (adapter->rss_conf == NULL) {
3015 printk(KERN_ERR "Failed to allocate memory for %s\n",
3020 #endif /* VMXNET3_RSS */
3022 err = vmxnet3_alloc_pci_resources(adapter, &dma64);
3026 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
3028 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
3030 printk(KERN_ERR "Incompatible h/w version (0x%x) for adapter"
3031 " %s\n", ver, pci_name(pdev));
3036 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
3038 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
3040 printk(KERN_ERR "Incompatible upt version (0x%x) for "
3041 "adapter %s\n", ver, pci_name(pdev));
3046 vmxnet3_declare_features(adapter, dma64);
3048 adapter->dev_number = atomic_read(&devices_found);
3050 adapter->share_intr = irq_share_mode;
3051 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE &&
3052 adapter->num_tx_queues != adapter->num_rx_queues)
3053 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
3055 vmxnet3_alloc_intr_resources(adapter);
3058 if (adapter->num_rx_queues > 1 &&
3059 adapter->intr.type == VMXNET3_IT_MSIX) {
3060 adapter->rss = true;
3061 printk(KERN_INFO "RSS is enabled.\n");
3063 adapter->rss = false;
3067 vmxnet3_read_mac_addr(adapter, mac);
3068 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3070 netdev->netdev_ops = &vmxnet3_netdev_ops;
3071 vmxnet3_set_ethtool_ops(netdev);
3072 netdev->watchdog_timeo = 5 * HZ;
3074 INIT_WORK(&adapter->work, vmxnet3_reset_work);
3076 if (adapter->intr.type == VMXNET3_IT_MSIX) {
3078 for (i = 0; i < adapter->num_rx_queues; i++) {
3079 netif_napi_add(adapter->netdev,
3080 &adapter->rx_queue[i].napi,
3081 vmxnet3_poll_rx_only, 64);
3084 netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
3088 netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
3089 netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
3091 SET_NETDEV_DEV(netdev, &pdev->dev);
3092 err = register_netdev(netdev);
3095 printk(KERN_ERR "Failed to register adapter %s\n",
3100 set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
3101 vmxnet3_check_link(adapter, false);
3102 atomic_inc(&devices_found);
3106 vmxnet3_free_intr_resources(adapter);
3108 vmxnet3_free_pci_resources(adapter);
3111 kfree(adapter->rss_conf);
3114 kfree(adapter->pm_conf);
3116 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3117 adapter->queue_desc_pa);
3118 err_alloc_queue_desc:
3119 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3120 adapter->shared, adapter->shared_pa);
3122 pci_set_drvdata(pdev, NULL);
3123 free_netdev(netdev);
3128 static void __devexit
3129 vmxnet3_remove_device(struct pci_dev *pdev)
3131 struct net_device *netdev = pci_get_drvdata(pdev);
3132 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3138 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3139 (int)num_online_cpus());
3143 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
3145 cancel_work_sync(&adapter->work);
3147 unregister_netdev(netdev);
3149 vmxnet3_free_intr_resources(adapter);
3150 vmxnet3_free_pci_resources(adapter);
3152 kfree(adapter->rss_conf);
3154 kfree(adapter->pm_conf);
3156 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3157 size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
3158 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3159 adapter->queue_desc_pa);
3160 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3161 adapter->shared, adapter->shared_pa);
3162 free_netdev(netdev);
3169 vmxnet3_suspend(struct device *device)
3171 struct pci_dev *pdev = to_pci_dev(device);
3172 struct net_device *netdev = pci_get_drvdata(pdev);
3173 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3174 struct Vmxnet3_PMConf *pmConf;
3175 struct ethhdr *ehdr;
3176 struct arphdr *ahdr;
3178 struct in_device *in_dev;
3179 struct in_ifaddr *ifa;
3180 unsigned long flags;
3183 if (!netif_running(netdev))
3186 for (i = 0; i < adapter->num_rx_queues; i++)
3187 napi_disable(&adapter->rx_queue[i].napi);
3189 vmxnet3_disable_all_intrs(adapter);
3190 vmxnet3_free_irqs(adapter);
3191 vmxnet3_free_intr_resources(adapter);
3193 netif_device_detach(netdev);
3194 netif_tx_stop_all_queues(netdev);
3196 /* Create wake-up filters. */
3197 pmConf = adapter->pm_conf;
3198 memset(pmConf, 0, sizeof(*pmConf));
3200 if (adapter->wol & WAKE_UCAST) {
3201 pmConf->filters[i].patternSize = ETH_ALEN;
3202 pmConf->filters[i].maskSize = 1;
3203 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
3204 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
3206 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
3210 if (adapter->wol & WAKE_ARP) {
3211 in_dev = in_dev_get(netdev);
3215 ifa = (struct in_ifaddr *)in_dev->ifa_list;
3219 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
3220 sizeof(struct arphdr) + /* ARP header */
3221 2 * ETH_ALEN + /* 2 Ethernet addresses*/
3222 2 * sizeof(u32); /*2 IPv4 addresses */
3223 pmConf->filters[i].maskSize =
3224 (pmConf->filters[i].patternSize - 1) / 8 + 1;
3226 /* ETH_P_ARP in Ethernet header. */
3227 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
3228 ehdr->h_proto = htons(ETH_P_ARP);
3230 /* ARPOP_REQUEST in ARP header. */
3231 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
3232 ahdr->ar_op = htons(ARPOP_REQUEST);
3233 arpreq = (u8 *)(ahdr + 1);
3235 /* The Unicast IPv4 address in 'tip' field. */
3236 arpreq += 2 * ETH_ALEN + sizeof(u32);
3237 *(u32 *)arpreq = ifa->ifa_address;
3239 /* The mask for the relevant bits. */
3240 pmConf->filters[i].mask[0] = 0x00;
3241 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
3242 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
3243 pmConf->filters[i].mask[3] = 0x00;
3244 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
3245 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
3248 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
3253 if (adapter->wol & WAKE_MAGIC)
3254 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
3256 pmConf->numFilters = i;
3258 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3259 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3261 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3264 spin_lock_irqsave(&adapter->cmd_lock, flags);
3265 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3266 VMXNET3_CMD_UPDATE_PMCFG);
3267 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3269 pci_save_state(pdev);
3270 pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
3272 pci_disable_device(pdev);
3273 pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
3280 vmxnet3_resume(struct device *device)
3283 unsigned long flags;
3284 struct pci_dev *pdev = to_pci_dev(device);
3285 struct net_device *netdev = pci_get_drvdata(pdev);
3286 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3287 struct Vmxnet3_PMConf *pmConf;
3289 if (!netif_running(netdev))
3292 /* Destroy wake-up filters. */
3293 pmConf = adapter->pm_conf;
3294 memset(pmConf, 0, sizeof(*pmConf));
3296 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3297 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3299 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3302 netif_device_attach(netdev);
3303 pci_set_power_state(pdev, PCI_D0);
3304 pci_restore_state(pdev);
3305 err = pci_enable_device_mem(pdev);
3309 pci_enable_wake(pdev, PCI_D0, 0);
3311 spin_lock_irqsave(&adapter->cmd_lock, flags);
3312 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3313 VMXNET3_CMD_UPDATE_PMCFG);
3314 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3315 vmxnet3_alloc_intr_resources(adapter);
3316 vmxnet3_request_irqs(adapter);
3317 for (i = 0; i < adapter->num_rx_queues; i++)
3318 napi_enable(&adapter->rx_queue[i].napi);
3319 vmxnet3_enable_all_intrs(adapter);
3324 static const struct dev_pm_ops vmxnet3_pm_ops = {
3325 .suspend = vmxnet3_suspend,
3326 .resume = vmxnet3_resume,
3330 static struct pci_driver vmxnet3_driver = {
3331 .name = vmxnet3_driver_name,
3332 .id_table = vmxnet3_pciid_table,
3333 .probe = vmxnet3_probe_device,
3334 .remove = __devexit_p(vmxnet3_remove_device),
3336 .driver.pm = &vmxnet3_pm_ops,
3342 vmxnet3_init_module(void)
3344 printk(KERN_INFO "%s - version %s\n", VMXNET3_DRIVER_DESC,
3345 VMXNET3_DRIVER_VERSION_REPORT);
3346 return pci_register_driver(&vmxnet3_driver);
3349 module_init(vmxnet3_init_module);
3353 vmxnet3_exit_module(void)
3355 pci_unregister_driver(&vmxnet3_driver);
3358 module_exit(vmxnet3_exit_module);
3360 MODULE_AUTHOR("VMware, Inc.");
3361 MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
3362 MODULE_LICENSE("GPL v2");
3363 MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);