blob: 2f07e261bd7400a9069add890923d52db3e1b09b [file] [log] [blame]
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001/*
2 * Linux driver for VMware's vmxnet3 ethernet NIC.
3 *
4 * Copyright (C) 2008-2009, VMware, Inc. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License and no later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
14 * details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * The full GNU General Public License is included in this distribution in
21 * the file called "COPYING".
22 *
23 * Maintained by: Shreyas Bhatewara <pv-drivers@vmware.com>
24 *
25 */
26
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040027#include <linux/module.h>
Stephen Rothwellb038b042009-11-17 23:04:59 -080028#include <net/ip6_checksum.h>
29
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -070030#include "vmxnet3_int.h"
31
32char vmxnet3_driver_name[] = "vmxnet3";
33#define VMXNET3_DRIVER_DESC "VMware vmxnet3 virtual NIC driver"
34
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -070035/*
36 * PCI Device ID Table
37 * Last entry must be all 0s
38 */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000039static DEFINE_PCI_DEVICE_TABLE(vmxnet3_pciid_table) = {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -070040 {PCI_VDEVICE(VMWARE, PCI_DEVICE_ID_VMWARE_VMXNET3)},
41 {0}
42};
43
44MODULE_DEVICE_TABLE(pci, vmxnet3_pciid_table);
45
46static atomic_t devices_found;
47
Shreyas Bhatewara09c50882010-11-19 10:55:24 +000048#define VMXNET3_MAX_DEVICES 10
49static int enable_mq = 1;
50static int irq_share_mode;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -070051
Shreyas Bhatewaraf9f25022011-01-14 14:59:31 +000052static void
53vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac);
54
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -070055/*
56 * Enable/Disable the given intr
57 */
58static void
59vmxnet3_enable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
60{
61 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 0);
62}
63
64
65static void
66vmxnet3_disable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
67{
68 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 1);
69}
70
71
72/*
73 * Enable/Disable all intrs used by the device
74 */
75static void
76vmxnet3_enable_all_intrs(struct vmxnet3_adapter *adapter)
77{
78 int i;
79
80 for (i = 0; i < adapter->intr.num_intrs; i++)
81 vmxnet3_enable_intr(adapter, i);
Ronghua Zang6929fe82010-07-15 22:18:47 -070082 adapter->shared->devRead.intrConf.intrCtrl &=
83 cpu_to_le32(~VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -070084}
85
86
87static void
88vmxnet3_disable_all_intrs(struct vmxnet3_adapter *adapter)
89{
90 int i;
91
Ronghua Zang6929fe82010-07-15 22:18:47 -070092 adapter->shared->devRead.intrConf.intrCtrl |=
93 cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -070094 for (i = 0; i < adapter->intr.num_intrs; i++)
95 vmxnet3_disable_intr(adapter, i);
96}
97
98
99static void
100vmxnet3_ack_events(struct vmxnet3_adapter *adapter, u32 events)
101{
102 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_ECR, events);
103}
104
105
106static bool
107vmxnet3_tq_stopped(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
108{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000109 return tq->stopped;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700110}
111
112
113static void
114vmxnet3_tq_start(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
115{
116 tq->stopped = false;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000117 netif_start_subqueue(adapter->netdev, tq - adapter->tx_queue);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700118}
119
120
121static void
122vmxnet3_tq_wake(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
123{
124 tq->stopped = false;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000125 netif_wake_subqueue(adapter->netdev, (tq - adapter->tx_queue));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700126}
127
128
129static void
130vmxnet3_tq_stop(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
131{
132 tq->stopped = true;
133 tq->num_stop++;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000134 netif_stop_subqueue(adapter->netdev, (tq - adapter->tx_queue));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700135}
136
137
138/*
139 * Check the link state. This may start or stop the tx queue.
140 */
141static void
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +0000142vmxnet3_check_link(struct vmxnet3_adapter *adapter, bool affectTxQueue)
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700143{
144 u32 ret;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000145 int i;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +0000146 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700147
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +0000148 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700149 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
150 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +0000151 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
152
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700153 adapter->link_speed = ret >> 16;
154 if (ret & 1) { /* Link is up. */
155 printk(KERN_INFO "%s: NIC Link is Up %d Mbps\n",
156 adapter->netdev->name, adapter->link_speed);
157 if (!netif_carrier_ok(adapter->netdev))
158 netif_carrier_on(adapter->netdev);
159
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000160 if (affectTxQueue) {
161 for (i = 0; i < adapter->num_tx_queues; i++)
162 vmxnet3_tq_start(&adapter->tx_queue[i],
163 adapter);
164 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700165 } else {
166 printk(KERN_INFO "%s: NIC Link is Down\n",
167 adapter->netdev->name);
168 if (netif_carrier_ok(adapter->netdev))
169 netif_carrier_off(adapter->netdev);
170
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000171 if (affectTxQueue) {
172 for (i = 0; i < adapter->num_tx_queues; i++)
173 vmxnet3_tq_stop(&adapter->tx_queue[i], adapter);
174 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700175 }
176}
177
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700178static void
179vmxnet3_process_events(struct vmxnet3_adapter *adapter)
180{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000181 int i;
Roland Dreiere328d412011-05-06 08:32:53 +0000182 unsigned long flags;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000183 u32 events = le32_to_cpu(adapter->shared->ecr);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700184 if (!events)
185 return;
186
187 vmxnet3_ack_events(adapter, events);
188
189 /* Check if link state has changed */
190 if (events & VMXNET3_ECR_LINK)
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +0000191 vmxnet3_check_link(adapter, true);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700192
193 /* Check if there is an error on xmit/recv queues */
194 if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
Roland Dreiere328d412011-05-06 08:32:53 +0000195 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700196 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
197 VMXNET3_CMD_GET_QUEUE_STATUS);
Roland Dreiere328d412011-05-06 08:32:53 +0000198 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700199
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000200 for (i = 0; i < adapter->num_tx_queues; i++)
201 if (adapter->tqd_start[i].status.stopped)
202 dev_err(&adapter->netdev->dev,
203 "%s: tq[%d] error 0x%x\n",
204 adapter->netdev->name, i, le32_to_cpu(
205 adapter->tqd_start[i].status.error));
206 for (i = 0; i < adapter->num_rx_queues; i++)
207 if (adapter->rqd_start[i].status.stopped)
208 dev_err(&adapter->netdev->dev,
209 "%s: rq[%d] error 0x%x\n",
210 adapter->netdev->name, i,
211 adapter->rqd_start[i].status.error);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700212
213 schedule_work(&adapter->work);
214 }
215}
216
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000217#ifdef __BIG_ENDIAN_BITFIELD
218/*
219 * The device expects the bitfields in shared structures to be written in
220 * little endian. When CPU is big endian, the following routines are used to
221 * correctly read and write into ABI.
222 * The general technique used here is : double word bitfields are defined in
223 * opposite order for big endian architecture. Then before reading them in
224 * driver the complete double word is translated using le32_to_cpu. Similarly
225 * After the driver writes into bitfields, cpu_to_le32 is used to translate the
226 * double words into required format.
227 * In order to avoid touching bits in shared structure more than once, temporary
228 * descriptors are used. These are passed as srcDesc to following functions.
229 */
230static void vmxnet3_RxDescToCPU(const struct Vmxnet3_RxDesc *srcDesc,
231 struct Vmxnet3_RxDesc *dstDesc)
232{
233 u32 *src = (u32 *)srcDesc + 2;
234 u32 *dst = (u32 *)dstDesc + 2;
235 dstDesc->addr = le64_to_cpu(srcDesc->addr);
236 *dst = le32_to_cpu(*src);
237 dstDesc->ext1 = le32_to_cpu(srcDesc->ext1);
238}
239
240static void vmxnet3_TxDescToLe(const struct Vmxnet3_TxDesc *srcDesc,
241 struct Vmxnet3_TxDesc *dstDesc)
242{
243 int i;
244 u32 *src = (u32 *)(srcDesc + 1);
245 u32 *dst = (u32 *)(dstDesc + 1);
246
247 /* Working backwards so that the gen bit is set at the end. */
248 for (i = 2; i > 0; i--) {
249 src--;
250 dst--;
251 *dst = cpu_to_le32(*src);
252 }
253}
254
255
256static void vmxnet3_RxCompToCPU(const struct Vmxnet3_RxCompDesc *srcDesc,
257 struct Vmxnet3_RxCompDesc *dstDesc)
258{
259 int i = 0;
260 u32 *src = (u32 *)srcDesc;
261 u32 *dst = (u32 *)dstDesc;
262 for (i = 0; i < sizeof(struct Vmxnet3_RxCompDesc) / sizeof(u32); i++) {
263 *dst = le32_to_cpu(*src);
264 src++;
265 dst++;
266 }
267}
268
269
270/* Used to read bitfield values from double words. */
271static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
272{
273 u32 temp = le32_to_cpu(*bitfield);
274 u32 mask = ((1 << size) - 1) << pos;
275 temp &= mask;
276 temp >>= pos;
277 return temp;
278}
279
280
281
282#endif /* __BIG_ENDIAN_BITFIELD */
283
284#ifdef __BIG_ENDIAN_BITFIELD
285
286# define VMXNET3_TXDESC_GET_GEN(txdesc) get_bitfield32(((const __le32 *) \
287 txdesc) + VMXNET3_TXD_GEN_DWORD_SHIFT, \
288 VMXNET3_TXD_GEN_SHIFT, VMXNET3_TXD_GEN_SIZE)
289# define VMXNET3_TXDESC_GET_EOP(txdesc) get_bitfield32(((const __le32 *) \
290 txdesc) + VMXNET3_TXD_EOP_DWORD_SHIFT, \
291 VMXNET3_TXD_EOP_SHIFT, VMXNET3_TXD_EOP_SIZE)
292# define VMXNET3_TCD_GET_GEN(tcd) get_bitfield32(((const __le32 *)tcd) + \
293 VMXNET3_TCD_GEN_DWORD_SHIFT, VMXNET3_TCD_GEN_SHIFT, \
294 VMXNET3_TCD_GEN_SIZE)
295# define VMXNET3_TCD_GET_TXIDX(tcd) get_bitfield32((const __le32 *)tcd, \
296 VMXNET3_TCD_TXIDX_SHIFT, VMXNET3_TCD_TXIDX_SIZE)
297# define vmxnet3_getRxComp(dstrcd, rcd, tmp) do { \
298 (dstrcd) = (tmp); \
299 vmxnet3_RxCompToCPU((rcd), (tmp)); \
300 } while (0)
301# define vmxnet3_getRxDesc(dstrxd, rxd, tmp) do { \
302 (dstrxd) = (tmp); \
303 vmxnet3_RxDescToCPU((rxd), (tmp)); \
304 } while (0)
305
306#else
307
308# define VMXNET3_TXDESC_GET_GEN(txdesc) ((txdesc)->gen)
309# define VMXNET3_TXDESC_GET_EOP(txdesc) ((txdesc)->eop)
310# define VMXNET3_TCD_GET_GEN(tcd) ((tcd)->gen)
311# define VMXNET3_TCD_GET_TXIDX(tcd) ((tcd)->txdIdx)
312# define vmxnet3_getRxComp(dstrcd, rcd, tmp) (dstrcd) = (rcd)
313# define vmxnet3_getRxDesc(dstrxd, rxd, tmp) (dstrxd) = (rxd)
314
315#endif /* __BIG_ENDIAN_BITFIELD */
316
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700317
318static void
319vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
320 struct pci_dev *pdev)
321{
322 if (tbi->map_type == VMXNET3_MAP_SINGLE)
323 pci_unmap_single(pdev, tbi->dma_addr, tbi->len,
324 PCI_DMA_TODEVICE);
325 else if (tbi->map_type == VMXNET3_MAP_PAGE)
326 pci_unmap_page(pdev, tbi->dma_addr, tbi->len,
327 PCI_DMA_TODEVICE);
328 else
329 BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
330
331 tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
332}
333
334
335static int
336vmxnet3_unmap_pkt(u32 eop_idx, struct vmxnet3_tx_queue *tq,
337 struct pci_dev *pdev, struct vmxnet3_adapter *adapter)
338{
339 struct sk_buff *skb;
340 int entries = 0;
341
342 /* no out of order completion */
343 BUG_ON(tq->buf_info[eop_idx].sop_idx != tq->tx_ring.next2comp);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000344 BUG_ON(VMXNET3_TXDESC_GET_EOP(&(tq->tx_ring.base[eop_idx].txd)) != 1);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700345
346 skb = tq->buf_info[eop_idx].skb;
347 BUG_ON(skb == NULL);
348 tq->buf_info[eop_idx].skb = NULL;
349
350 VMXNET3_INC_RING_IDX_ONLY(eop_idx, tq->tx_ring.size);
351
352 while (tq->tx_ring.next2comp != eop_idx) {
353 vmxnet3_unmap_tx_buf(tq->buf_info + tq->tx_ring.next2comp,
354 pdev);
355
356 /* update next2comp w/o tx_lock. Since we are marking more,
357 * instead of less, tx ring entries avail, the worst case is
358 * that the tx routine incorrectly re-queues a pkt due to
359 * insufficient tx ring entries.
360 */
361 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
362 entries++;
363 }
364
365 dev_kfree_skb_any(skb);
366 return entries;
367}
368
369
370static int
371vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue *tq,
372 struct vmxnet3_adapter *adapter)
373{
374 int completed = 0;
375 union Vmxnet3_GenericDesc *gdesc;
376
377 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000378 while (VMXNET3_TCD_GET_GEN(&gdesc->tcd) == tq->comp_ring.gen) {
379 completed += vmxnet3_unmap_pkt(VMXNET3_TCD_GET_TXIDX(
380 &gdesc->tcd), tq, adapter->pdev,
381 adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700382
383 vmxnet3_comp_ring_adv_next2proc(&tq->comp_ring);
384 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
385 }
386
387 if (completed) {
388 spin_lock(&tq->tx_lock);
389 if (unlikely(vmxnet3_tq_stopped(tq, adapter) &&
390 vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) >
391 VMXNET3_WAKE_QUEUE_THRESHOLD(tq) &&
392 netif_carrier_ok(adapter->netdev))) {
393 vmxnet3_tq_wake(tq, adapter);
394 }
395 spin_unlock(&tq->tx_lock);
396 }
397 return completed;
398}
399
400
401static void
402vmxnet3_tq_cleanup(struct vmxnet3_tx_queue *tq,
403 struct vmxnet3_adapter *adapter)
404{
405 int i;
406
407 while (tq->tx_ring.next2comp != tq->tx_ring.next2fill) {
408 struct vmxnet3_tx_buf_info *tbi;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700409
410 tbi = tq->buf_info + tq->tx_ring.next2comp;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700411
412 vmxnet3_unmap_tx_buf(tbi, adapter->pdev);
413 if (tbi->skb) {
414 dev_kfree_skb_any(tbi->skb);
415 tbi->skb = NULL;
416 }
417 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
418 }
419
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);
424 }
425
426 tq->tx_ring.gen = VMXNET3_INIT_GEN;
427 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
428
429 tq->comp_ring.gen = VMXNET3_INIT_GEN;
430 tq->comp_ring.next2proc = 0;
431}
432
433
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000434static void
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700435vmxnet3_tq_destroy(struct vmxnet3_tx_queue *tq,
436 struct vmxnet3_adapter *adapter)
437{
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;
443 }
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;
449 }
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;
455 }
456 kfree(tq->buf_info);
457 tq->buf_info = NULL;
458}
459
460
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000461/* Destroy all tx queues */
462void
463vmxnet3_tq_destroy_all(struct vmxnet3_adapter *adapter)
464{
465 int i;
466
467 for (i = 0; i < adapter->num_tx_queues; i++)
468 vmxnet3_tq_destroy(&adapter->tx_queue[i], adapter);
469}
470
471
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700472static void
473vmxnet3_tq_init(struct vmxnet3_tx_queue *tq,
474 struct vmxnet3_adapter *adapter)
475{
476 int i;
477
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;
483
484 memset(tq->data_ring.base, 0, tq->data_ring.size *
485 sizeof(struct Vmxnet3_TxDataDesc));
486
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;
492
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;
497
498 /* stats are not reset */
499}
500
501
502static int
503vmxnet3_tq_create(struct vmxnet3_tx_queue *tq,
504 struct vmxnet3_adapter *adapter)
505{
506 BUG_ON(tq->tx_ring.base || tq->data_ring.base ||
507 tq->comp_ring.base || tq->buf_info);
508
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;
516 }
517
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;
526 }
527
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;
536 }
537
538 tq->buf_info = kcalloc(tq->tx_ring.size, sizeof(tq->buf_info[0]),
539 GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000540 if (!tq->buf_info)
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700541 goto err;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700542
543 return 0;
544
545err:
546 vmxnet3_tq_destroy(tq, adapter);
547 return -ENOMEM;
548}
549
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000550static void
551vmxnet3_tq_cleanup_all(struct vmxnet3_adapter *adapter)
552{
553 int i;
554
555 for (i = 0; i < adapter->num_tx_queues; i++)
556 vmxnet3_tq_cleanup(&adapter->tx_queue[i], adapter);
557}
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700558
559/*
560 * starting from ring->next2fill, allocate rx buffers for the given ring
561 * of the rx queue and update the rx desc. stop after @num_to_alloc buffers
562 * are allocated or allocation fails
563 */
564
565static int
566vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
567 int num_to_alloc, struct vmxnet3_adapter *adapter)
568{
569 int num_allocated = 0;
570 struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
571 struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
572 u32 val;
573
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000574 while (num_allocated <= num_to_alloc) {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700575 struct vmxnet3_rx_buf_info *rbi;
576 union Vmxnet3_GenericDesc *gd;
577
578 rbi = rbi_base + ring->next2fill;
579 gd = ring->base + ring->next2fill;
580
581 if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
582 if (rbi->skb == NULL) {
583 rbi->skb = dev_alloc_skb(rbi->len +
584 NET_IP_ALIGN);
585 if (unlikely(rbi->skb == NULL)) {
586 rq->stats.rx_buf_alloc_failure++;
587 break;
588 }
589 rbi->skb->dev = adapter->netdev;
590
591 skb_reserve(rbi->skb, NET_IP_ALIGN);
592 rbi->dma_addr = pci_map_single(adapter->pdev,
593 rbi->skb->data, rbi->len,
594 PCI_DMA_FROMDEVICE);
595 } else {
596 /* rx buffer skipped by the device */
597 }
598 val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
599 } else {
600 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
601 rbi->len != PAGE_SIZE);
602
603 if (rbi->page == NULL) {
604 rbi->page = alloc_page(GFP_ATOMIC);
605 if (unlikely(rbi->page == NULL)) {
606 rq->stats.rx_buf_alloc_failure++;
607 break;
608 }
609 rbi->dma_addr = pci_map_page(adapter->pdev,
610 rbi->page, 0, PAGE_SIZE,
611 PCI_DMA_FROMDEVICE);
612 } else {
613 /* rx buffers skipped by the device */
614 }
615 val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
616 }
617
618 BUG_ON(rbi->dma_addr == 0);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000619 gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000620 gd->dword[2] = cpu_to_le32((!ring->gen << VMXNET3_RXD_GEN_SHIFT)
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000621 | val | rbi->len);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700622
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000623 /* Fill the last buffer but dont mark it ready, or else the
624 * device will think that the queue is full */
625 if (num_allocated == num_to_alloc)
626 break;
627
628 gd->dword[2] |= cpu_to_le32(ring->gen << VMXNET3_RXD_GEN_SHIFT);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700629 num_allocated++;
630 vmxnet3_cmd_ring_adv_next2fill(ring);
631 }
632 rq->uncommitted[ring_idx] += num_allocated;
633
Randy Dunlapf69655822009-10-16 17:54:34 -0700634 dev_dbg(&adapter->netdev->dev,
635 "alloc_rx_buf: %d allocated, next2fill %u, next2comp "
Masanari Iidac3ca8812012-02-01 03:13:59 +0000636 "%u, uncommitted %u\n", num_allocated, ring->next2fill,
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700637 ring->next2comp, rq->uncommitted[ring_idx]);
638
639 /* so that the device can distinguish a full ring and an empty ring */
640 BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
641
642 return num_allocated;
643}
644
645
646static void
647vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
648 struct vmxnet3_rx_buf_info *rbi)
649{
650 struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
651 skb_shinfo(skb)->nr_frags;
652
653 BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
654
Ian Campbell0e0634d2011-09-21 21:53:28 +0000655 __skb_frag_set_page(frag, rbi->page);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700656 frag->page_offset = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000657 skb_frag_size_set(frag, rcd->len);
658 skb->data_len += rcd->len;
Eric Dumazet5e6c3552011-10-13 11:38:17 +0000659 skb->truesize += PAGE_SIZE;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700660 skb_shinfo(skb)->nr_frags++;
661}
662
663
664static void
665vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
666 struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
667 struct vmxnet3_adapter *adapter)
668{
669 u32 dw2, len;
670 unsigned long buf_offset;
671 int i;
672 union Vmxnet3_GenericDesc *gdesc;
673 struct vmxnet3_tx_buf_info *tbi = NULL;
674
675 BUG_ON(ctx->copy_size > skb_headlen(skb));
676
677 /* use the previous gen bit for the SOP desc */
678 dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
679
680 ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
681 gdesc = ctx->sop_txd; /* both loops below can be skipped */
682
683 /* no need to map the buffer if headers are copied */
684 if (ctx->copy_size) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000685 ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700686 tq->tx_ring.next2fill *
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000687 sizeof(struct Vmxnet3_TxDataDesc));
688 ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700689 ctx->sop_txd->dword[3] = 0;
690
691 tbi = tq->buf_info + tq->tx_ring.next2fill;
692 tbi->map_type = VMXNET3_MAP_NONE;
693
Randy Dunlapf69655822009-10-16 17:54:34 -0700694 dev_dbg(&adapter->netdev->dev,
695 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000696 tq->tx_ring.next2fill,
697 le64_to_cpu(ctx->sop_txd->txd.addr),
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700698 ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
699 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
700
701 /* use the right gen for non-SOP desc */
702 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
703 }
704
705 /* linear part can use multiple tx desc if it's big */
706 len = skb_headlen(skb) - ctx->copy_size;
707 buf_offset = ctx->copy_size;
708 while (len) {
709 u32 buf_size;
710
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000711 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
712 buf_size = len;
713 dw2 |= len;
714 } else {
715 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
716 /* spec says that for TxDesc.len, 0 == 2^14 */
717 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700718
719 tbi = tq->buf_info + tq->tx_ring.next2fill;
720 tbi->map_type = VMXNET3_MAP_SINGLE;
721 tbi->dma_addr = pci_map_single(adapter->pdev,
722 skb->data + buf_offset, buf_size,
723 PCI_DMA_TODEVICE);
724
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000725 tbi->len = buf_size;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700726
727 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
728 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
729
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000730 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000731 gdesc->dword[2] = cpu_to_le32(dw2);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700732 gdesc->dword[3] = 0;
733
Randy Dunlapf69655822009-10-16 17:54:34 -0700734 dev_dbg(&adapter->netdev->dev,
735 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000736 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
737 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700738 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
739 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
740
741 len -= buf_size;
742 buf_offset += buf_size;
743 }
744
745 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000746 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700747
748 tbi = tq->buf_info + tq->tx_ring.next2fill;
749 tbi->map_type = VMXNET3_MAP_PAGE;
Ian Campbell0e0634d2011-09-21 21:53:28 +0000750 tbi->dma_addr = skb_frag_dma_map(&adapter->pdev->dev, frag,
Eric Dumazet9e903e02011-10-18 21:00:24 +0000751 0, skb_frag_size(frag),
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100752 DMA_TO_DEVICE);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700753
Eric Dumazet9e903e02011-10-18 21:00:24 +0000754 tbi->len = skb_frag_size(frag);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700755
756 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
757 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
758
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000759 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000760 gdesc->dword[2] = cpu_to_le32(dw2 | skb_frag_size(frag));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700761 gdesc->dword[3] = 0;
762
Randy Dunlapf69655822009-10-16 17:54:34 -0700763 dev_dbg(&adapter->netdev->dev,
764 "txd[%u]: 0x%llu %u %u\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000765 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
766 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700767 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
768 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
769 }
770
771 ctx->eop_txd = gdesc;
772
773 /* set the last buf_info for the pkt */
774 tbi->skb = skb;
775 tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
776}
777
778
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000779/* Init all tx queues */
780static void
781vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
782{
783 int i;
784
785 for (i = 0; i < adapter->num_tx_queues; i++)
786 vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
787}
788
789
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700790/*
791 * parse and copy relevant protocol headers:
792 * For a tso pkt, relevant headers are L2/3/4 including options
793 * For a pkt requesting csum offloading, they are L2/3 and may include L4
794 * if it's a TCP/UDP pkt
795 *
796 * Returns:
797 * -1: error happens during parsing
798 * 0: protocol headers parsed, but too big to be copied
799 * 1: protocol headers parsed and copied
800 *
801 * Other effects:
802 * 1. related *ctx fields are updated.
803 * 2. ctx->copy_size is # of bytes copied
804 * 3. the portion copied is guaranteed to be in the linear part
805 *
806 */
807static int
808vmxnet3_parse_and_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
809 struct vmxnet3_tx_ctx *ctx,
810 struct vmxnet3_adapter *adapter)
811{
812 struct Vmxnet3_TxDataDesc *tdd;
813
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000814 if (ctx->mss) { /* TSO */
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700815 ctx->eth_ip_hdr_size = skb_transport_offset(skb);
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000816 ctx->l4_hdr_size = tcp_hdrlen(skb);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700817 ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
818 } else {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700819 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000820 ctx->eth_ip_hdr_size = skb_checksum_start_offset(skb);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700821
822 if (ctx->ipv4) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000823 const struct iphdr *iph = ip_hdr(skb);
824
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000825 if (iph->protocol == IPPROTO_TCP)
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000826 ctx->l4_hdr_size = tcp_hdrlen(skb);
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000827 else if (iph->protocol == IPPROTO_UDP)
David S. Millerf6a1ad42012-03-05 21:16:26 -0500828 ctx->l4_hdr_size = sizeof(struct udphdr);
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000829 else
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700830 ctx->l4_hdr_size = 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700831 } else {
832 /* for simplicity, don't copy L4 headers */
833 ctx->l4_hdr_size = 0;
834 }
Neil Hormanb2032622012-02-16 01:48:56 +0000835 ctx->copy_size = min(ctx->eth_ip_hdr_size +
836 ctx->l4_hdr_size, skb->len);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700837 } else {
838 ctx->eth_ip_hdr_size = 0;
839 ctx->l4_hdr_size = 0;
840 /* copy as much as allowed */
841 ctx->copy_size = min((unsigned int)VMXNET3_HDR_COPY_SIZE
842 , skb_headlen(skb));
843 }
844
845 /* make sure headers are accessible directly */
846 if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
847 goto err;
848 }
849
850 if (unlikely(ctx->copy_size > VMXNET3_HDR_COPY_SIZE)) {
851 tq->stats.oversized_hdr++;
852 ctx->copy_size = 0;
853 return 0;
854 }
855
856 tdd = tq->data_ring.base + tq->tx_ring.next2fill;
857
858 memcpy(tdd->data, skb->data, ctx->copy_size);
Randy Dunlapf69655822009-10-16 17:54:34 -0700859 dev_dbg(&adapter->netdev->dev,
860 "copy %u bytes to dataRing[%u]\n",
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700861 ctx->copy_size, tq->tx_ring.next2fill);
862 return 1;
863
864err:
865 return -1;
866}
867
868
869static void
870vmxnet3_prepare_tso(struct sk_buff *skb,
871 struct vmxnet3_tx_ctx *ctx)
872{
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000873 struct tcphdr *tcph = tcp_hdr(skb);
874
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700875 if (ctx->ipv4) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000876 struct iphdr *iph = ip_hdr(skb);
877
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700878 iph->check = 0;
879 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
880 IPPROTO_TCP, 0);
881 } else {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000882 struct ipv6hdr *iph = ipv6_hdr(skb);
883
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700884 tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
885 IPPROTO_TCP, 0);
886 }
887}
888
889
890/*
891 * Transmits a pkt thru a given tq
892 * Returns:
893 * NETDEV_TX_OK: descriptors are setup successfully
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300894 * NETDEV_TX_OK: error occurred, the pkt is dropped
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700895 * NETDEV_TX_BUSY: tx ring is full, queue is stopped
896 *
897 * Side-effects:
898 * 1. tx ring may be changed
899 * 2. tq stats may be updated accordingly
900 * 3. shared->txNumDeferred may be updated
901 */
902
903static int
904vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
905 struct vmxnet3_adapter *adapter, struct net_device *netdev)
906{
907 int ret;
908 u32 count;
909 unsigned long flags;
910 struct vmxnet3_tx_ctx ctx;
911 union Vmxnet3_GenericDesc *gdesc;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000912#ifdef __BIG_ENDIAN_BITFIELD
913 /* Use temporary descriptor to avoid touching bits multiple times */
914 union Vmxnet3_GenericDesc tempTxDesc;
915#endif
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700916
917 /* conservatively estimate # of descriptors to use */
918 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) +
919 skb_shinfo(skb)->nr_frags + 1;
920
Jesse Gross72e85c42011-06-23 13:04:39 +0000921 ctx.ipv4 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IP));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700922
923 ctx.mss = skb_shinfo(skb)->gso_size;
924 if (ctx.mss) {
925 if (skb_header_cloned(skb)) {
926 if (unlikely(pskb_expand_head(skb, 0, 0,
927 GFP_ATOMIC) != 0)) {
928 tq->stats.drop_tso++;
929 goto drop_pkt;
930 }
931 tq->stats.copy_skb_header++;
932 }
933 vmxnet3_prepare_tso(skb, &ctx);
934 } else {
935 if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
936
937 /* non-tso pkts must not use more than
938 * VMXNET3_MAX_TXD_PER_PKT entries
939 */
940 if (skb_linearize(skb) != 0) {
941 tq->stats.drop_too_many_frags++;
942 goto drop_pkt;
943 }
944 tq->stats.linearized++;
945
946 /* recalculate the # of descriptors to use */
947 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
948 }
949 }
950
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000951 spin_lock_irqsave(&tq->tx_lock, flags);
952
953 if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
954 tq->stats.tx_ring_full++;
955 dev_dbg(&adapter->netdev->dev,
956 "tx queue stopped on %s, next2comp %u"
957 " next2fill %u\n", adapter->netdev->name,
958 tq->tx_ring.next2comp, tq->tx_ring.next2fill);
959
960 vmxnet3_tq_stop(tq, adapter);
961 spin_unlock_irqrestore(&tq->tx_lock, flags);
962 return NETDEV_TX_BUSY;
963 }
964
965
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700966 ret = vmxnet3_parse_and_copy_hdr(skb, tq, &ctx, adapter);
967 if (ret >= 0) {
968 BUG_ON(ret <= 0 && ctx.copy_size != 0);
969 /* hdrs parsed, check against other limits */
970 if (ctx.mss) {
971 if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
972 VMXNET3_MAX_TX_BUF_SIZE)) {
973 goto hdr_too_big;
974 }
975 } else {
976 if (skb->ip_summed == CHECKSUM_PARTIAL) {
977 if (unlikely(ctx.eth_ip_hdr_size +
978 skb->csum_offset >
979 VMXNET3_MAX_CSUM_OFFSET)) {
980 goto hdr_too_big;
981 }
982 }
983 }
984 } else {
985 tq->stats.drop_hdr_inspect_err++;
Dan Carpenterf955e142010-12-20 03:03:15 +0000986 goto unlock_drop_pkt;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700987 }
988
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700989 /* fill tx descs related to addr & len */
990 vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter);
991
992 /* setup the EOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000993 ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -0700994
995 /* setup the SOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000996#ifdef __BIG_ENDIAN_BITFIELD
997 gdesc = &tempTxDesc;
998 gdesc->dword[2] = ctx.sop_txd->dword[2];
999 gdesc->dword[3] = ctx.sop_txd->dword[3];
1000#else
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001001 gdesc = ctx.sop_txd;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001002#endif
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001003 if (ctx.mss) {
1004 gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
1005 gdesc->txd.om = VMXNET3_OM_TSO;
1006 gdesc->txd.msscof = ctx.mss;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001007 le32_add_cpu(&tq->shared->txNumDeferred, (skb->len -
1008 gdesc->txd.hlen + ctx.mss - 1) / ctx.mss);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001009 } else {
1010 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1011 gdesc->txd.hlen = ctx.eth_ip_hdr_size;
1012 gdesc->txd.om = VMXNET3_OM_CSUM;
1013 gdesc->txd.msscof = ctx.eth_ip_hdr_size +
1014 skb->csum_offset;
1015 } else {
1016 gdesc->txd.om = 0;
1017 gdesc->txd.msscof = 0;
1018 }
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001019 le32_add_cpu(&tq->shared->txNumDeferred, 1);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001020 }
1021
1022 if (vlan_tx_tag_present(skb)) {
1023 gdesc->txd.ti = 1;
1024 gdesc->txd.tci = vlan_tx_tag_get(skb);
1025 }
1026
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001027 /* finally flips the GEN bit of the SOP desc. */
1028 gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
1029 VMXNET3_TXD_GEN);
1030#ifdef __BIG_ENDIAN_BITFIELD
1031 /* Finished updating in bitfields of Tx Desc, so write them in original
1032 * place.
1033 */
1034 vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
1035 (struct Vmxnet3_TxDesc *)ctx.sop_txd);
1036 gdesc = ctx.sop_txd;
1037#endif
Randy Dunlapf69655822009-10-16 17:54:34 -07001038 dev_dbg(&adapter->netdev->dev,
1039 "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001040 (u32)((union Vmxnet3_GenericDesc *)ctx.sop_txd -
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001041 tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
1042 le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001043
1044 spin_unlock_irqrestore(&tq->tx_lock, flags);
1045
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001046 if (le32_to_cpu(tq->shared->txNumDeferred) >=
1047 le32_to_cpu(tq->shared->txThreshold)) {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001048 tq->shared->txNumDeferred = 0;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001049 VMXNET3_WRITE_BAR0_REG(adapter,
1050 VMXNET3_REG_TXPROD + tq->qid * 8,
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001051 tq->tx_ring.next2fill);
1052 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001053
1054 return NETDEV_TX_OK;
1055
1056hdr_too_big:
1057 tq->stats.drop_oversized_hdr++;
Dan Carpenterf955e142010-12-20 03:03:15 +00001058unlock_drop_pkt:
1059 spin_unlock_irqrestore(&tq->tx_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001060drop_pkt:
1061 tq->stats.drop_total++;
1062 dev_kfree_skb(skb);
1063 return NETDEV_TX_OK;
1064}
1065
1066
1067static netdev_tx_t
1068vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1069{
1070 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001071
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001072 BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
1073 return vmxnet3_tq_xmit(skb,
1074 &adapter->tx_queue[skb->queue_mapping],
1075 adapter, netdev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001076}
1077
1078
1079static void
1080vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
1081 struct sk_buff *skb,
1082 union Vmxnet3_GenericDesc *gdesc)
1083{
Michał Mirosława0d27302011-04-18 13:31:21 +00001084 if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001085 /* typical case: TCP/UDP over IP and both csums are correct */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001086 if ((le32_to_cpu(gdesc->dword[3]) & VMXNET3_RCD_CSUM_OK) ==
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001087 VMXNET3_RCD_CSUM_OK) {
1088 skb->ip_summed = CHECKSUM_UNNECESSARY;
1089 BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
1090 BUG_ON(!(gdesc->rcd.v4 || gdesc->rcd.v6));
1091 BUG_ON(gdesc->rcd.frg);
1092 } else {
1093 if (gdesc->rcd.csum) {
1094 skb->csum = htons(gdesc->rcd.csum);
1095 skb->ip_summed = CHECKSUM_PARTIAL;
1096 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001097 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001098 }
1099 }
1100 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001101 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001102 }
1103}
1104
1105
1106static void
1107vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
1108 struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
1109{
1110 rq->stats.drop_err++;
1111 if (!rcd->fcs)
1112 rq->stats.drop_fcs++;
1113
1114 rq->stats.drop_total++;
1115
1116 /*
1117 * We do not unmap and chain the rx buffer to the skb.
1118 * We basically pretend this buffer is not used and will be recycled
1119 * by vmxnet3_rq_alloc_rx_buf()
1120 */
1121
1122 /*
1123 * ctx->skb may be NULL if this is the first and the only one
1124 * desc for the pkt
1125 */
1126 if (ctx->skb)
1127 dev_kfree_skb_irq(ctx->skb);
1128
1129 ctx->skb = NULL;
1130}
1131
1132
1133static int
1134vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
1135 struct vmxnet3_adapter *adapter, int quota)
1136{
Joe Perches215faf92010-12-21 02:16:10 -08001137 static const u32 rxprod_reg[2] = {
1138 VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2
1139 };
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001140 u32 num_rxd = 0;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001141 bool skip_page_frags = false;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001142 struct Vmxnet3_RxCompDesc *rcd;
1143 struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001144#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);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001150 while (rcd->gen == rq->comp_ring.gen) {
1151 struct vmxnet3_rx_buf_info *rbi;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001152 struct sk_buff *skb, *new_skb = NULL;
1153 struct page *new_page = NULL;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001154 int num_to_alloc;
1155 struct Vmxnet3_RxDesc *rxd;
1156 u32 idx, ring_idx;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001157 struct vmxnet3_cmd_ring *ring = NULL;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001158 if (num_rxd >= quota) {
1159 /* we may stop even before we see the EOP desc of
1160 * the current pkt
1161 */
1162 break;
1163 }
1164 num_rxd++;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001165 BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001166 idx = rcd->rxdIdx;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001167 ring_idx = rcd->rqID < adapter->num_rx_queues ? 0 : 1;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001168 ring = rq->rx_ring + ring_idx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001169 vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
1170 &rxCmdDesc);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001171 rbi = rq->buf_info[ring_idx] + idx;
1172
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001173 BUG_ON(rxd->addr != rbi->dma_addr ||
1174 rxd->len != rbi->len);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001175
1176 if (unlikely(rcd->eop && rcd->err)) {
1177 vmxnet3_rx_error(rq, rcd, ctx, adapter);
1178 goto rcd_done;
1179 }
1180
1181 if (rcd->sop) { /* first buf of the pkt */
1182 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
1183 rcd->rqID != rq->qid);
1184
1185 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
1186 BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
1187
1188 if (unlikely(rcd->len == 0)) {
1189 /* Pretend the rx buffer is skipped. */
1190 BUG_ON(!(rcd->sop && rcd->eop));
Randy Dunlapf69655822009-10-16 17:54:34 -07001191 dev_dbg(&adapter->netdev->dev,
1192 "rxRing[%u][%u] 0 length\n",
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001193 ring_idx, idx);
1194 goto rcd_done;
1195 }
1196
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001197 skip_page_frags = false;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001198 ctx->skb = rbi->skb;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001199 new_skb = dev_alloc_skb(rbi->len + NET_IP_ALIGN);
1200 if (new_skb == NULL) {
1201 /* Skb allocation failed, do not handover this
1202 * skb to stack. Reuse it. Drop the existing pkt
1203 */
1204 rq->stats.rx_buf_alloc_failure++;
1205 ctx->skb = NULL;
1206 rq->stats.drop_total++;
1207 skip_page_frags = true;
1208 goto rcd_done;
1209 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001210
1211 pci_unmap_single(adapter->pdev, rbi->dma_addr, rbi->len,
1212 PCI_DMA_FROMDEVICE);
1213
1214 skb_put(ctx->skb, rcd->len);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001215
1216 /* Immediate refill */
1217 new_skb->dev = adapter->netdev;
1218 skb_reserve(new_skb, NET_IP_ALIGN);
1219 rbi->skb = new_skb;
1220 rbi->dma_addr = pci_map_single(adapter->pdev,
1221 rbi->skb->data, rbi->len,
1222 PCI_DMA_FROMDEVICE);
1223 rxd->addr = cpu_to_le64(rbi->dma_addr);
1224 rxd->len = rbi->len;
1225
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001226 } else {
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001227 BUG_ON(ctx->skb == NULL && !skip_page_frags);
1228
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001229 /* non SOP buffer must be type 1 in most cases */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001230 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE);
1231 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001232
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001233 /* If an sop buffer was dropped, skip all
1234 * following non-sop fragments. They will be reused.
1235 */
1236 if (skip_page_frags)
1237 goto rcd_done;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001238
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001239 new_page = alloc_page(GFP_ATOMIC);
1240 if (unlikely(new_page == NULL)) {
1241 /* Replacement page frag could not be allocated.
1242 * Reuse this page. Drop the pkt and free the
1243 * skb which contained this page as a frag. Skip
1244 * processing all the following non-sop frags.
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001245 */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001246 rq->stats.rx_buf_alloc_failure++;
1247 dev_kfree_skb(ctx->skb);
1248 ctx->skb = NULL;
1249 skip_page_frags = true;
1250 goto rcd_done;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001251 }
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001252
1253 if (rcd->len) {
1254 pci_unmap_page(adapter->pdev,
1255 rbi->dma_addr, rbi->len,
1256 PCI_DMA_FROMDEVICE);
1257
1258 vmxnet3_append_frag(ctx->skb, rcd, rbi);
1259 }
1260
1261 /* Immediate refill */
1262 rbi->page = new_page;
1263 rbi->dma_addr = pci_map_page(adapter->pdev, rbi->page,
1264 0, PAGE_SIZE,
1265 PCI_DMA_FROMDEVICE);
1266 rxd->addr = cpu_to_le64(rbi->dma_addr);
1267 rxd->len = rbi->len;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001268 }
1269
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001270
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001271 skb = ctx->skb;
1272 if (rcd->eop) {
1273 skb->len += skb->data_len;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001274
1275 vmxnet3_rx_csum(adapter, skb,
1276 (union Vmxnet3_GenericDesc *)rcd);
1277 skb->protocol = eth_type_trans(skb, adapter->netdev);
1278
Jesse Gross72e85c42011-06-23 13:04:39 +00001279 if (unlikely(rcd->ts))
1280 __vlan_hwaccel_put_tag(skb, rcd->tci);
1281
Jesse Gross213ade82011-06-24 14:24:35 +00001282 if (adapter->netdev->features & NETIF_F_LRO)
1283 netif_receive_skb(skb);
1284 else
1285 napi_gro_receive(&rq->napi, skb);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001286
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001287 ctx->skb = NULL;
1288 }
1289
1290rcd_done:
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001291 /* device may have skipped some rx descs */
1292 ring->next2comp = idx;
1293 num_to_alloc = vmxnet3_cmd_ring_desc_avail(ring);
1294 ring = rq->rx_ring + ring_idx;
1295 while (num_to_alloc) {
1296 vmxnet3_getRxDesc(rxd, &ring->base[ring->next2fill].rxd,
1297 &rxCmdDesc);
1298 BUG_ON(!rxd->addr);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001299
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001300 /* Recv desc is ready to be used by the device */
1301 rxd->gen = ring->gen;
1302 vmxnet3_cmd_ring_adv_next2fill(ring);
1303 num_to_alloc--;
1304 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001305
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001306 /* if needed, update the register */
1307 if (unlikely(rq->shared->updateRxProd)) {
1308 VMXNET3_WRITE_BAR0_REG(adapter,
1309 rxprod_reg[ring_idx] + rq->qid * 8,
1310 ring->next2fill);
1311 rq->uncommitted[ring_idx] = 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001312 }
1313
1314 vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001315 vmxnet3_getRxComp(rcd,
1316 &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001317 }
1318
1319 return num_rxd;
1320}
1321
1322
1323static void
1324vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
1325 struct vmxnet3_adapter *adapter)
1326{
1327 u32 i, ring_idx;
1328 struct Vmxnet3_RxDesc *rxd;
1329
1330 for (ring_idx = 0; ring_idx < 2; ring_idx++) {
1331 for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001332#ifdef __BIG_ENDIAN_BITFIELD
1333 struct Vmxnet3_RxDesc rxDesc;
1334#endif
1335 vmxnet3_getRxDesc(rxd,
1336 &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001337
1338 if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
1339 rq->buf_info[ring_idx][i].skb) {
1340 pci_unmap_single(adapter->pdev, rxd->addr,
1341 rxd->len, PCI_DMA_FROMDEVICE);
1342 dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
1343 rq->buf_info[ring_idx][i].skb = NULL;
1344 } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
1345 rq->buf_info[ring_idx][i].page) {
1346 pci_unmap_page(adapter->pdev, rxd->addr,
1347 rxd->len, PCI_DMA_FROMDEVICE);
1348 put_page(rq->buf_info[ring_idx][i].page);
1349 rq->buf_info[ring_idx][i].page = NULL;
1350 }
1351 }
1352
1353 rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
1354 rq->rx_ring[ring_idx].next2fill =
1355 rq->rx_ring[ring_idx].next2comp = 0;
1356 rq->uncommitted[ring_idx] = 0;
1357 }
1358
1359 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1360 rq->comp_ring.next2proc = 0;
1361}
1362
1363
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001364static void
1365vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
1366{
1367 int i;
1368
1369 for (i = 0; i < adapter->num_rx_queues; i++)
1370 vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
1371}
1372
1373
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001374void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
1375 struct vmxnet3_adapter *adapter)
1376{
1377 int i;
1378 int j;
1379
1380 /* all rx buffers must have already been freed */
1381 for (i = 0; i < 2; i++) {
1382 if (rq->buf_info[i]) {
1383 for (j = 0; j < rq->rx_ring[i].size; j++)
1384 BUG_ON(rq->buf_info[i][j].page != NULL);
1385 }
1386 }
1387
1388
1389 kfree(rq->buf_info[0]);
1390
1391 for (i = 0; i < 2; i++) {
1392 if (rq->rx_ring[i].base) {
1393 pci_free_consistent(adapter->pdev, rq->rx_ring[i].size
1394 * sizeof(struct Vmxnet3_RxDesc),
1395 rq->rx_ring[i].base,
1396 rq->rx_ring[i].basePA);
1397 rq->rx_ring[i].base = NULL;
1398 }
1399 rq->buf_info[i] = NULL;
1400 }
1401
1402 if (rq->comp_ring.base) {
1403 pci_free_consistent(adapter->pdev, rq->comp_ring.size *
1404 sizeof(struct Vmxnet3_RxCompDesc),
1405 rq->comp_ring.base, rq->comp_ring.basePA);
1406 rq->comp_ring.base = NULL;
1407 }
1408}
1409
1410
1411static int
1412vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
1413 struct vmxnet3_adapter *adapter)
1414{
1415 int i;
1416
1417 /* initialize buf_info */
1418 for (i = 0; i < rq->rx_ring[0].size; i++) {
1419
1420 /* 1st buf for a pkt is skbuff */
1421 if (i % adapter->rx_buf_per_pkt == 0) {
1422 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
1423 rq->buf_info[0][i].len = adapter->skb_buf_size;
1424 } else { /* subsequent bufs for a pkt is frag */
1425 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
1426 rq->buf_info[0][i].len = PAGE_SIZE;
1427 }
1428 }
1429 for (i = 0; i < rq->rx_ring[1].size; i++) {
1430 rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
1431 rq->buf_info[1][i].len = PAGE_SIZE;
1432 }
1433
1434 /* reset internal state and allocate buffers for both rings */
1435 for (i = 0; i < 2; i++) {
1436 rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
1437 rq->uncommitted[i] = 0;
1438
1439 memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
1440 sizeof(struct Vmxnet3_RxDesc));
1441 rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
1442 }
1443 if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
1444 adapter) == 0) {
1445 /* at least has 1 rx buffer for the 1st ring */
1446 return -ENOMEM;
1447 }
1448 vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
1449
1450 /* reset the comp ring */
1451 rq->comp_ring.next2proc = 0;
1452 memset(rq->comp_ring.base, 0, rq->comp_ring.size *
1453 sizeof(struct Vmxnet3_RxCompDesc));
1454 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1455
1456 /* reset rxctx */
1457 rq->rx_ctx.skb = NULL;
1458
1459 /* stats are not reset */
1460 return 0;
1461}
1462
1463
1464static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001465vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
1466{
1467 int i, err = 0;
1468
1469 for (i = 0; i < adapter->num_rx_queues; i++) {
1470 err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
1471 if (unlikely(err)) {
1472 dev_err(&adapter->netdev->dev, "%s: failed to "
1473 "initialize rx queue%i\n",
1474 adapter->netdev->name, i);
1475 break;
1476 }
1477 }
1478 return err;
1479
1480}
1481
1482
1483static int
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001484vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
1485{
1486 int i;
1487 size_t sz;
1488 struct vmxnet3_rx_buf_info *bi;
1489
1490 for (i = 0; i < 2; i++) {
1491
1492 sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
1493 rq->rx_ring[i].base = pci_alloc_consistent(adapter->pdev, sz,
1494 &rq->rx_ring[i].basePA);
1495 if (!rq->rx_ring[i].base) {
1496 printk(KERN_ERR "%s: failed to allocate rx ring %d\n",
1497 adapter->netdev->name, i);
1498 goto err;
1499 }
1500 }
1501
1502 sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
1503 rq->comp_ring.base = pci_alloc_consistent(adapter->pdev, sz,
1504 &rq->comp_ring.basePA);
1505 if (!rq->comp_ring.base) {
1506 printk(KERN_ERR "%s: failed to allocate rx comp ring\n",
1507 adapter->netdev->name);
1508 goto err;
1509 }
1510
1511 sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
1512 rq->rx_ring[1].size);
Julia Lawall476c6092010-05-13 10:05:40 +00001513 bi = kzalloc(sz, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001514 if (!bi)
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001515 goto err;
Joe Perchese404dec2012-01-29 12:56:23 +00001516
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001517 rq->buf_info[0] = bi;
1518 rq->buf_info[1] = bi + rq->rx_ring[0].size;
1519
1520 return 0;
1521
1522err:
1523 vmxnet3_rq_destroy(rq, adapter);
1524 return -ENOMEM;
1525}
1526
1527
1528static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001529vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
1530{
1531 int i, err = 0;
1532
1533 for (i = 0; i < adapter->num_rx_queues; i++) {
1534 err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
1535 if (unlikely(err)) {
1536 dev_err(&adapter->netdev->dev,
1537 "%s: failed to create rx queue%i\n",
1538 adapter->netdev->name, i);
1539 goto err_out;
1540 }
1541 }
1542 return err;
1543err_out:
1544 vmxnet3_rq_destroy_all(adapter);
1545 return err;
1546
1547}
1548
1549/* Multiple queue aware polling function for tx and rx */
1550
1551static int
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001552vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
1553{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001554 int rcd_done = 0, i;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001555 if (unlikely(adapter->shared->ecr))
1556 vmxnet3_process_events(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001557 for (i = 0; i < adapter->num_tx_queues; i++)
1558 vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001559
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001560 for (i = 0; i < adapter->num_rx_queues; i++)
1561 rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
1562 adapter, budget);
1563 return rcd_done;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001564}
1565
1566
1567static int
1568vmxnet3_poll(struct napi_struct *napi, int budget)
1569{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001570 struct vmxnet3_rx_queue *rx_queue = container_of(napi,
1571 struct vmxnet3_rx_queue, napi);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001572 int rxd_done;
1573
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001574 rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001575
1576 if (rxd_done < budget) {
1577 napi_complete(napi);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001578 vmxnet3_enable_all_intrs(rx_queue->adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001579 }
1580 return rxd_done;
1581}
1582
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001583/*
1584 * NAPI polling function for MSI-X mode with multiple Rx queues
1585 * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
1586 */
1587
1588static int
1589vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
1590{
1591 struct vmxnet3_rx_queue *rq = container_of(napi,
1592 struct vmxnet3_rx_queue, napi);
1593 struct vmxnet3_adapter *adapter = rq->adapter;
1594 int rxd_done;
1595
1596 /* When sharing interrupt with corresponding tx queue, process
1597 * tx completions in that queue as well
1598 */
1599 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
1600 struct vmxnet3_tx_queue *tq =
1601 &adapter->tx_queue[rq - adapter->rx_queue];
1602 vmxnet3_tq_tx_complete(tq, adapter);
1603 }
1604
1605 rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
1606
1607 if (rxd_done < budget) {
1608 napi_complete(napi);
1609 vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
1610 }
1611 return rxd_done;
1612}
1613
1614
1615#ifdef CONFIG_PCI_MSI
1616
1617/*
1618 * Handle completion interrupts on tx queues
1619 * Returns whether or not the intr is handled
1620 */
1621
1622static irqreturn_t
1623vmxnet3_msix_tx(int irq, void *data)
1624{
1625 struct vmxnet3_tx_queue *tq = data;
1626 struct vmxnet3_adapter *adapter = tq->adapter;
1627
1628 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1629 vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
1630
1631 /* Handle the case where only one irq is allocate for all tx queues */
1632 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1633 int i;
1634 for (i = 0; i < adapter->num_tx_queues; i++) {
1635 struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
1636 vmxnet3_tq_tx_complete(txq, adapter);
1637 }
1638 } else {
1639 vmxnet3_tq_tx_complete(tq, adapter);
1640 }
1641 vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
1642
1643 return IRQ_HANDLED;
1644}
1645
1646
1647/*
1648 * Handle completion interrupts on rx queues. Returns whether or not the
1649 * intr is handled
1650 */
1651
1652static irqreturn_t
1653vmxnet3_msix_rx(int irq, void *data)
1654{
1655 struct vmxnet3_rx_queue *rq = data;
1656 struct vmxnet3_adapter *adapter = rq->adapter;
1657
1658 /* disable intr if needed */
1659 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1660 vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
1661 napi_schedule(&rq->napi);
1662
1663 return IRQ_HANDLED;
1664}
1665
1666/*
1667 *----------------------------------------------------------------------------
1668 *
1669 * vmxnet3_msix_event --
1670 *
1671 * vmxnet3 msix event intr handler
1672 *
1673 * Result:
1674 * whether or not the intr is handled
1675 *
1676 *----------------------------------------------------------------------------
1677 */
1678
1679static irqreturn_t
1680vmxnet3_msix_event(int irq, void *data)
1681{
1682 struct net_device *dev = data;
1683 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1684
1685 /* disable intr if needed */
1686 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1687 vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
1688
1689 if (adapter->shared->ecr)
1690 vmxnet3_process_events(adapter);
1691
1692 vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
1693
1694 return IRQ_HANDLED;
1695}
1696
1697#endif /* CONFIG_PCI_MSI */
1698
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001699
1700/* Interrupt handler for vmxnet3 */
1701static irqreturn_t
1702vmxnet3_intr(int irq, void *dev_id)
1703{
1704 struct net_device *dev = dev_id;
1705 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1706
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001707 if (adapter->intr.type == VMXNET3_IT_INTX) {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001708 u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
1709 if (unlikely(icr == 0))
1710 /* not ours */
1711 return IRQ_NONE;
1712 }
1713
1714
1715 /* disable intr if needed */
1716 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001717 vmxnet3_disable_all_intrs(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001718
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001719 napi_schedule(&adapter->rx_queue[0].napi);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001720
1721 return IRQ_HANDLED;
1722}
1723
1724#ifdef CONFIG_NET_POLL_CONTROLLER
1725
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001726/* netpoll callback. */
1727static void
1728vmxnet3_netpoll(struct net_device *netdev)
1729{
1730 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Neil Horman6b4741d2014-03-10 06:55:55 -04001731 int i;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001732
Neil Horman6b4741d2014-03-10 06:55:55 -04001733 switch (adapter->intr.type) {
1734 case VMXNET3_IT_MSIX:
1735 for (i = 0; i < adapter->num_rx_queues; i++)
1736 vmxnet3_msix_rx(0, &adapter->rx_queue[i]);
1737 break;
1738 case VMXNET3_IT_MSI:
1739 default:
1740 vmxnet3_intr(0, adapter->netdev);
1741 break;
1742 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001743
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001744}
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001745#endif /* CONFIG_NET_POLL_CONTROLLER */
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001746
1747static int
1748vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
1749{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001750 struct vmxnet3_intr *intr = &adapter->intr;
1751 int err = 0, i;
1752 int vector = 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001753
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001754#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001755 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001756 for (i = 0; i < adapter->num_tx_queues; i++) {
1757 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1758 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
1759 adapter->netdev->name, vector);
1760 err = request_irq(
1761 intr->msix_entries[vector].vector,
1762 vmxnet3_msix_tx, 0,
1763 adapter->tx_queue[i].name,
1764 &adapter->tx_queue[i]);
1765 } else {
1766 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
1767 adapter->netdev->name, vector);
1768 }
1769 if (err) {
1770 dev_err(&adapter->netdev->dev,
1771 "Failed to request irq for MSIX, %s, "
1772 "error %d\n",
1773 adapter->tx_queue[i].name, err);
1774 return err;
1775 }
1776
1777 /* Handle the case where only 1 MSIx was allocated for
1778 * all tx queues */
1779 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1780 for (; i < adapter->num_tx_queues; i++)
1781 adapter->tx_queue[i].comp_ring.intr_idx
1782 = vector;
1783 vector++;
1784 break;
1785 } else {
1786 adapter->tx_queue[i].comp_ring.intr_idx
1787 = vector++;
1788 }
1789 }
1790 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
1791 vector = 0;
1792
1793 for (i = 0; i < adapter->num_rx_queues; i++) {
1794 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
1795 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
1796 adapter->netdev->name, vector);
1797 else
1798 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
1799 adapter->netdev->name, vector);
1800 err = request_irq(intr->msix_entries[vector].vector,
1801 vmxnet3_msix_rx, 0,
1802 adapter->rx_queue[i].name,
1803 &(adapter->rx_queue[i]));
1804 if (err) {
1805 printk(KERN_ERR "Failed to request irq for MSIX"
1806 ", %s, error %d\n",
1807 adapter->rx_queue[i].name, err);
1808 return err;
1809 }
1810
1811 adapter->rx_queue[i].comp_ring.intr_idx = vector++;
1812 }
1813
1814 sprintf(intr->event_msi_vector_name, "%s-event-%d",
1815 adapter->netdev->name, vector);
1816 err = request_irq(intr->msix_entries[vector].vector,
1817 vmxnet3_msix_event, 0,
1818 intr->event_msi_vector_name, adapter->netdev);
1819 intr->event_intr_idx = vector;
1820
1821 } else if (intr->type == VMXNET3_IT_MSI) {
1822 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001823 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
1824 adapter->netdev->name, adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001825 } else {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001826#endif
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001827 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001828 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
1829 IRQF_SHARED, adapter->netdev->name,
1830 adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001831#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001832 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001833#endif
1834 intr->num_intrs = vector + 1;
1835 if (err) {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001836 printk(KERN_ERR "Failed to request irq %s (intr type:%d), error"
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001837 ":%d\n", adapter->netdev->name, intr->type, err);
1838 } else {
1839 /* Number of rx queues will not change after this */
1840 for (i = 0; i < adapter->num_rx_queues; i++) {
1841 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
1842 rq->qid = i;
1843 rq->qid2 = i + adapter->num_rx_queues;
1844 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001845
1846
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001847
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001848 /* init our intr settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001849 for (i = 0; i < intr->num_intrs; i++)
1850 intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
1851 if (adapter->intr.type != VMXNET3_IT_MSIX) {
1852 adapter->intr.event_intr_idx = 0;
1853 for (i = 0; i < adapter->num_tx_queues; i++)
1854 adapter->tx_queue[i].comp_ring.intr_idx = 0;
1855 adapter->rx_queue[0].comp_ring.intr_idx = 0;
1856 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001857
1858 printk(KERN_INFO "%s: intr type %u, mode %u, %u vectors "
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001859 "allocated\n", adapter->netdev->name, intr->type,
1860 intr->mask_mode, intr->num_intrs);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001861 }
1862
1863 return err;
1864}
1865
1866
1867static void
1868vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
1869{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001870 struct vmxnet3_intr *intr = &adapter->intr;
1871 BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001872
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001873 switch (intr->type) {
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001874#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001875 case VMXNET3_IT_MSIX:
1876 {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001877 int i, vector = 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001878
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001879 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1880 for (i = 0; i < adapter->num_tx_queues; i++) {
1881 free_irq(intr->msix_entries[vector++].vector,
1882 &(adapter->tx_queue[i]));
1883 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
1884 break;
1885 }
1886 }
1887
1888 for (i = 0; i < adapter->num_rx_queues; i++) {
1889 free_irq(intr->msix_entries[vector++].vector,
1890 &(adapter->rx_queue[i]));
1891 }
1892
1893 free_irq(intr->msix_entries[vector].vector,
1894 adapter->netdev);
1895 BUG_ON(vector >= intr->num_intrs);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001896 break;
1897 }
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001898#endif
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001899 case VMXNET3_IT_MSI:
1900 free_irq(adapter->pdev->irq, adapter->netdev);
1901 break;
1902 case VMXNET3_IT_INTX:
1903 free_irq(adapter->pdev->irq, adapter->netdev);
1904 break;
1905 default:
1906 BUG_ON(true);
1907 }
1908}
1909
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001910
1911static void
1912vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
1913{
Jesse Gross72e85c42011-06-23 13:04:39 +00001914 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1915 u16 vid;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001916
Jesse Gross72e85c42011-06-23 13:04:39 +00001917 /* allow untagged pkts */
1918 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1919
1920 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1921 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001922}
1923
1924
Jiri Pirko8e586132011-12-08 19:52:37 -05001925static int
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001926vmxnet3_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1927{
1928 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001929
Jesse Grossf6957f82011-08-07 23:15:47 +00001930 if (!(netdev->flags & IFF_PROMISC)) {
1931 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1932 unsigned long flags;
1933
1934 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1935 spin_lock_irqsave(&adapter->cmd_lock, flags);
1936 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1937 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1938 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1939 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001940
1941 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001942
1943 return 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001944}
1945
1946
Jiri Pirko8e586132011-12-08 19:52:37 -05001947static int
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001948vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1949{
1950 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001951
Jesse Grossf6957f82011-08-07 23:15:47 +00001952 if (!(netdev->flags & IFF_PROMISC)) {
1953 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1954 unsigned long flags;
1955
1956 VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
1957 spin_lock_irqsave(&adapter->cmd_lock, flags);
1958 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1959 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1960 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1961 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001962
1963 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001964
1965 return 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001966}
1967
1968
1969static u8 *
1970vmxnet3_copy_mc(struct net_device *netdev)
1971{
1972 u8 *buf = NULL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001973 u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001974
1975 /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
1976 if (sz <= 0xffff) {
1977 /* We may be called with BH disabled */
1978 buf = kmalloc(sz, GFP_ATOMIC);
1979 if (buf) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00001980 struct netdev_hw_addr *ha;
Jiri Pirko567ec872010-02-23 23:17:07 +00001981 int i = 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001982
Jiri Pirko22bedad2010-04-01 21:22:57 +00001983 netdev_for_each_mc_addr(ha, netdev)
1984 memcpy(buf + i++ * ETH_ALEN, ha->addr,
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001985 ETH_ALEN);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001986 }
1987 }
1988 return buf;
1989}
1990
1991
1992static void
1993vmxnet3_set_mc(struct net_device *netdev)
1994{
1995 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00001996 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001997 struct Vmxnet3_RxFilterConf *rxConf =
1998 &adapter->shared->devRead.rxFilterConf;
1999 u8 *new_table = NULL;
2000 u32 new_mode = VMXNET3_RXM_UCAST;
2001
Jesse Gross72e85c42011-06-23 13:04:39 +00002002 if (netdev->flags & IFF_PROMISC) {
2003 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2004 memset(vfTable, 0, VMXNET3_VFT_SIZE * sizeof(*vfTable));
2005
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002006 new_mode |= VMXNET3_RXM_PROMISC;
Jesse Gross72e85c42011-06-23 13:04:39 +00002007 } else {
2008 vmxnet3_restore_vlan(adapter);
2009 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002010
2011 if (netdev->flags & IFF_BROADCAST)
2012 new_mode |= VMXNET3_RXM_BCAST;
2013
2014 if (netdev->flags & IFF_ALLMULTI)
2015 new_mode |= VMXNET3_RXM_ALL_MULTI;
2016 else
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002017 if (!netdev_mc_empty(netdev)) {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002018 new_table = vmxnet3_copy_mc(netdev);
2019 if (new_table) {
2020 new_mode |= VMXNET3_RXM_MCAST;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002021 rxConf->mfTableLen = cpu_to_le16(
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002022 netdev_mc_count(netdev) * ETH_ALEN);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002023 rxConf->mfTablePA = cpu_to_le64(virt_to_phys(
2024 new_table));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002025 } else {
2026 printk(KERN_INFO "%s: failed to copy mcast list"
2027 ", setting ALL_MULTI\n", netdev->name);
2028 new_mode |= VMXNET3_RXM_ALL_MULTI;
2029 }
2030 }
2031
2032
2033 if (!(new_mode & VMXNET3_RXM_MCAST)) {
2034 rxConf->mfTableLen = 0;
2035 rxConf->mfTablePA = 0;
2036 }
2037
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002038 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002039 if (new_mode != rxConf->rxMode) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002040 rxConf->rxMode = cpu_to_le32(new_mode);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002041 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2042 VMXNET3_CMD_UPDATE_RX_MODE);
Jesse Gross72e85c42011-06-23 13:04:39 +00002043 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2044 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002045 }
2046
2047 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2048 VMXNET3_CMD_UPDATE_MAC_FILTERS);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002049 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002050
2051 kfree(new_table);
2052}
2053
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002054void
2055vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
2056{
2057 int i;
2058
2059 for (i = 0; i < adapter->num_rx_queues; i++)
2060 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2061}
2062
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002063
2064/*
2065 * Set up driver_shared based on settings in adapter.
2066 */
2067
2068static void
2069vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2070{
2071 struct Vmxnet3_DriverShared *shared = adapter->shared;
2072 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2073 struct Vmxnet3_TxQueueConf *tqc;
2074 struct Vmxnet3_RxQueueConf *rqc;
2075 int i;
2076
2077 memset(shared, 0, sizeof(*shared));
2078
2079 /* driver settings */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002080 shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2081 devRead->misc.driverInfo.version = cpu_to_le32(
2082 VMXNET3_DRIVER_VERSION_NUM);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002083 devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2084 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2085 devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002086 *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2087 *((u32 *)&devRead->misc.driverInfo.gos));
2088 devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2089 devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002090
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002091 devRead->misc.ddPA = cpu_to_le64(virt_to_phys(adapter));
2092 devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002093
2094 /* set up feature flags */
Michał Mirosława0d27302011-04-18 13:31:21 +00002095 if (adapter->netdev->features & NETIF_F_RXCSUM)
Harvey Harrison3843e512010-10-21 18:05:32 +00002096 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002097
Michał Mirosława0d27302011-04-18 13:31:21 +00002098 if (adapter->netdev->features & NETIF_F_LRO) {
Harvey Harrison3843e512010-10-21 18:05:32 +00002099 devRead->misc.uptFeatures |= UPT1_F_LRO;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002100 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002101 }
Shreyas Bhatewara54da3d02011-01-14 14:59:36 +00002102 if (adapter->netdev->features & NETIF_F_HW_VLAN_RX)
Harvey Harrison3843e512010-10-21 18:05:32 +00002103 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002104
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002105 devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2106 devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2107 devRead->misc.queueDescLen = cpu_to_le32(
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002108 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2109 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002110
2111 /* tx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002112 devRead->misc.numTxQueues = adapter->num_tx_queues;
2113 for (i = 0; i < adapter->num_tx_queues; i++) {
2114 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2115 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2116 tqc = &adapter->tqd_start[i].conf;
2117 tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
2118 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2119 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
2120 tqc->ddPA = cpu_to_le64(virt_to_phys(tq->buf_info));
2121 tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
2122 tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
2123 tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
2124 tqc->ddLen = cpu_to_le32(
2125 sizeof(struct vmxnet3_tx_buf_info) *
2126 tqc->txRingSize);
2127 tqc->intrIdx = tq->comp_ring.intr_idx;
2128 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002129
2130 /* rx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002131 devRead->misc.numRxQueues = adapter->num_rx_queues;
2132 for (i = 0; i < adapter->num_rx_queues; i++) {
2133 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2134 rqc = &adapter->rqd_start[i].conf;
2135 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2136 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2137 rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
2138 rqc->ddPA = cpu_to_le64(virt_to_phys(
2139 rq->buf_info));
2140 rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
2141 rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
2142 rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
2143 rqc->ddLen = cpu_to_le32(
2144 sizeof(struct vmxnet3_rx_buf_info) *
2145 (rqc->rxRingSize[0] +
2146 rqc->rxRingSize[1]));
2147 rqc->intrIdx = rq->comp_ring.intr_idx;
2148 }
2149
2150#ifdef VMXNET3_RSS
2151 memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2152
2153 if (adapter->rss) {
2154 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
2155 devRead->misc.uptFeatures |= UPT1_F_RSS;
2156 devRead->misc.numRxQueues = adapter->num_rx_queues;
2157 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2158 UPT1_RSS_HASH_TYPE_IPV4 |
2159 UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2160 UPT1_RSS_HASH_TYPE_IPV6;
2161 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2162 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2163 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
2164 get_random_bytes(&rssConf->hashKey[0], rssConf->hashKeySize);
2165 for (i = 0; i < rssConf->indTableSize; i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00002166 rssConf->indTable[i] = ethtool_rxfh_indir_default(
2167 i, adapter->num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002168
2169 devRead->rssConfDesc.confVer = 1;
2170 devRead->rssConfDesc.confLen = sizeof(*rssConf);
2171 devRead->rssConfDesc.confPA = virt_to_phys(rssConf);
2172 }
2173
2174#endif /* VMXNET3_RSS */
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002175
2176 /* intr settings */
2177 devRead->intrConf.autoMask = adapter->intr.mask_mode ==
2178 VMXNET3_IMM_AUTO;
2179 devRead->intrConf.numIntrs = adapter->intr.num_intrs;
2180 for (i = 0; i < adapter->intr.num_intrs; i++)
2181 devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
2182
2183 devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
Ronghua Zang6929fe82010-07-15 22:18:47 -07002184 devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002185
2186 /* rx filter settings */
2187 devRead->rxFilterConf.rxMode = 0;
2188 vmxnet3_restore_vlan(adapter);
Shreyas Bhatewaraf9f25022011-01-14 14:59:31 +00002189 vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
2190
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002191 /* the rest are already zeroed */
2192}
2193
2194
2195int
2196vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
2197{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002198 int err, i;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002199 u32 ret;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002200 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002201
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002202 dev_dbg(&adapter->netdev->dev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
2203 " ring sizes %u %u %u\n", adapter->netdev->name,
2204 adapter->skb_buf_size, adapter->rx_buf_per_pkt,
2205 adapter->tx_queue[0].tx_ring.size,
2206 adapter->rx_queue[0].rx_ring[0].size,
2207 adapter->rx_queue[0].rx_ring[1].size);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002208
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002209 vmxnet3_tq_init_all(adapter);
2210 err = vmxnet3_rq_init_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002211 if (err) {
2212 printk(KERN_ERR "Failed to init rx queue for %s: error %d\n",
2213 adapter->netdev->name, err);
2214 goto rq_err;
2215 }
2216
2217 err = vmxnet3_request_irqs(adapter);
2218 if (err) {
2219 printk(KERN_ERR "Failed to setup irq for %s: error %d\n",
2220 adapter->netdev->name, err);
2221 goto irq_err;
2222 }
2223
2224 vmxnet3_setup_driver_shared(adapter);
2225
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002226 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
2227 adapter->shared_pa));
2228 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
2229 adapter->shared_pa));
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002230 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002231 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2232 VMXNET3_CMD_ACTIVATE_DEV);
2233 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002234 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002235
2236 if (ret != 0) {
2237 printk(KERN_ERR "Failed to activate dev %s: error %u\n",
2238 adapter->netdev->name, ret);
2239 err = -EINVAL;
2240 goto activate_err;
2241 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002242
2243 for (i = 0; i < adapter->num_rx_queues; i++) {
2244 VMXNET3_WRITE_BAR0_REG(adapter,
2245 VMXNET3_REG_RXPROD + i * VMXNET3_REG_ALIGN,
2246 adapter->rx_queue[i].rx_ring[0].next2fill);
2247 VMXNET3_WRITE_BAR0_REG(adapter, (VMXNET3_REG_RXPROD2 +
2248 (i * VMXNET3_REG_ALIGN)),
2249 adapter->rx_queue[i].rx_ring[1].next2fill);
2250 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002251
2252 /* Apply the rx filter settins last. */
2253 vmxnet3_set_mc(adapter->netdev);
2254
2255 /*
2256 * Check link state when first activating device. It will start the
2257 * tx queue if the link is up.
2258 */
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00002259 vmxnet3_check_link(adapter, true);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002260 for (i = 0; i < adapter->num_rx_queues; i++)
2261 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002262 vmxnet3_enable_all_intrs(adapter);
2263 clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
2264 return 0;
2265
2266activate_err:
2267 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
2268 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
2269 vmxnet3_free_irqs(adapter);
2270irq_err:
2271rq_err:
2272 /* free up buffers we allocated */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002273 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002274 return err;
2275}
2276
2277
2278void
2279vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
2280{
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002281 unsigned long flags;
2282 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002283 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002284 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002285}
2286
2287
2288int
2289vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
2290{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002291 int i;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002292 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002293 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
2294 return 0;
2295
2296
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002297 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002298 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2299 VMXNET3_CMD_QUIESCE_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002300 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002301 vmxnet3_disable_all_intrs(adapter);
2302
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002303 for (i = 0; i < adapter->num_rx_queues; i++)
2304 napi_disable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002305 netif_tx_disable(adapter->netdev);
2306 adapter->link_speed = 0;
2307 netif_carrier_off(adapter->netdev);
2308
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002309 vmxnet3_tq_cleanup_all(adapter);
2310 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002311 vmxnet3_free_irqs(adapter);
2312 return 0;
2313}
2314
2315
2316static void
2317vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2318{
2319 u32 tmp;
2320
2321 tmp = *(u32 *)mac;
2322 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
2323
2324 tmp = (mac[5] << 8) | mac[4];
2325 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
2326}
2327
2328
2329static int
2330vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
2331{
2332 struct sockaddr *addr = p;
2333 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2334
2335 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2336 vmxnet3_write_mac_addr(adapter, addr->sa_data);
2337
2338 return 0;
2339}
2340
2341
2342/* ==================== initialization and cleanup routines ============ */
2343
2344static int
2345vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
2346{
2347 int err;
2348 unsigned long mmio_start, mmio_len;
2349 struct pci_dev *pdev = adapter->pdev;
2350
2351 err = pci_enable_device(pdev);
2352 if (err) {
2353 printk(KERN_ERR "Failed to enable adapter %s: error %d\n",
2354 pci_name(pdev), err);
2355 return err;
2356 }
2357
2358 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
2359 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
2360 printk(KERN_ERR "pci_set_consistent_dma_mask failed "
2361 "for adapter %s\n", pci_name(pdev));
2362 err = -EIO;
2363 goto err_set_mask;
2364 }
2365 *dma64 = true;
2366 } else {
2367 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
2368 printk(KERN_ERR "pci_set_dma_mask failed for adapter "
2369 "%s\n", pci_name(pdev));
2370 err = -EIO;
2371 goto err_set_mask;
2372 }
2373 *dma64 = false;
2374 }
2375
2376 err = pci_request_selected_regions(pdev, (1 << 2) - 1,
2377 vmxnet3_driver_name);
2378 if (err) {
2379 printk(KERN_ERR "Failed to request region for adapter %s: "
2380 "error %d\n", pci_name(pdev), err);
2381 goto err_set_mask;
2382 }
2383
2384 pci_set_master(pdev);
2385
2386 mmio_start = pci_resource_start(pdev, 0);
2387 mmio_len = pci_resource_len(pdev, 0);
2388 adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
2389 if (!adapter->hw_addr0) {
2390 printk(KERN_ERR "Failed to map bar0 for adapter %s\n",
2391 pci_name(pdev));
2392 err = -EIO;
2393 goto err_ioremap;
2394 }
2395
2396 mmio_start = pci_resource_start(pdev, 1);
2397 mmio_len = pci_resource_len(pdev, 1);
2398 adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
2399 if (!adapter->hw_addr1) {
2400 printk(KERN_ERR "Failed to map bar1 for adapter %s\n",
2401 pci_name(pdev));
2402 err = -EIO;
2403 goto err_bar1;
2404 }
2405 return 0;
2406
2407err_bar1:
2408 iounmap(adapter->hw_addr0);
2409err_ioremap:
2410 pci_release_selected_regions(pdev, (1 << 2) - 1);
2411err_set_mask:
2412 pci_disable_device(pdev);
2413 return err;
2414}
2415
2416
2417static void
2418vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
2419{
2420 BUG_ON(!adapter->pdev);
2421
2422 iounmap(adapter->hw_addr0);
2423 iounmap(adapter->hw_addr1);
2424 pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
2425 pci_disable_device(adapter->pdev);
2426}
2427
2428
2429static void
2430vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
2431{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002432 size_t sz, i, ring0_size, ring1_size, comp_size;
2433 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[0];
2434
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002435
2436 if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
2437 VMXNET3_MAX_ETH_HDR_SIZE) {
2438 adapter->skb_buf_size = adapter->netdev->mtu +
2439 VMXNET3_MAX_ETH_HDR_SIZE;
2440 if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
2441 adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
2442
2443 adapter->rx_buf_per_pkt = 1;
2444 } else {
2445 adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
2446 sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
2447 VMXNET3_MAX_ETH_HDR_SIZE;
2448 adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
2449 }
2450
2451 /*
2452 * for simplicity, force the ring0 size to be a multiple of
2453 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
2454 */
2455 sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002456 ring0_size = adapter->rx_queue[0].rx_ring[0].size;
2457 ring0_size = (ring0_size + sz - 1) / sz * sz;
Shreyas Bhatewaraa53255d2011-01-14 14:59:25 +00002458 ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002459 sz * sz);
2460 ring1_size = adapter->rx_queue[0].rx_ring[1].size;
2461 comp_size = ring0_size + ring1_size;
2462
2463 for (i = 0; i < adapter->num_rx_queues; i++) {
2464 rq = &adapter->rx_queue[i];
2465 rq->rx_ring[0].size = ring0_size;
2466 rq->rx_ring[1].size = ring1_size;
2467 rq->comp_ring.size = comp_size;
2468 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002469}
2470
2471
2472int
2473vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
2474 u32 rx_ring_size, u32 rx_ring2_size)
2475{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002476 int err = 0, i;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002477
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002478 for (i = 0; i < adapter->num_tx_queues; i++) {
2479 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2480 tq->tx_ring.size = tx_ring_size;
2481 tq->data_ring.size = tx_ring_size;
2482 tq->comp_ring.size = tx_ring_size;
2483 tq->shared = &adapter->tqd_start[i].ctrl;
2484 tq->stopped = true;
2485 tq->adapter = adapter;
2486 tq->qid = i;
2487 err = vmxnet3_tq_create(tq, adapter);
2488 /*
2489 * Too late to change num_tx_queues. We cannot do away with
2490 * lesser number of queues than what we asked for
2491 */
2492 if (err)
2493 goto queue_err;
2494 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002495
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002496 adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
2497 adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002498 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002499 for (i = 0; i < adapter->num_rx_queues; i++) {
2500 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2501 /* qid and qid2 for rx queues will be assigned later when num
2502 * of rx queues is finalized after allocating intrs */
2503 rq->shared = &adapter->rqd_start[i].ctrl;
2504 rq->adapter = adapter;
2505 err = vmxnet3_rq_create(rq, adapter);
2506 if (err) {
2507 if (i == 0) {
2508 printk(KERN_ERR "Could not allocate any rx"
2509 "queues. Aborting.\n");
2510 goto queue_err;
2511 } else {
2512 printk(KERN_INFO "Number of rx queues changed "
2513 "to : %d.\n", i);
2514 adapter->num_rx_queues = i;
2515 err = 0;
2516 break;
2517 }
2518 }
2519 }
2520 return err;
2521queue_err:
2522 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002523 return err;
2524}
2525
2526static int
2527vmxnet3_open(struct net_device *netdev)
2528{
2529 struct vmxnet3_adapter *adapter;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002530 int err, i;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002531
2532 adapter = netdev_priv(netdev);
2533
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002534 for (i = 0; i < adapter->num_tx_queues; i++)
2535 spin_lock_init(&adapter->tx_queue[i].tx_lock);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002536
2537 err = vmxnet3_create_queues(adapter, VMXNET3_DEF_TX_RING_SIZE,
2538 VMXNET3_DEF_RX_RING_SIZE,
2539 VMXNET3_DEF_RX_RING_SIZE);
2540 if (err)
2541 goto queue_err;
2542
2543 err = vmxnet3_activate_dev(adapter);
2544 if (err)
2545 goto activate_err;
2546
2547 return 0;
2548
2549activate_err:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002550 vmxnet3_rq_destroy_all(adapter);
2551 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002552queue_err:
2553 return err;
2554}
2555
2556
2557static int
2558vmxnet3_close(struct net_device *netdev)
2559{
2560 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2561
2562 /*
2563 * Reset_work may be in the middle of resetting the device, wait for its
2564 * completion.
2565 */
2566 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2567 msleep(1);
2568
2569 vmxnet3_quiesce_dev(adapter);
2570
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002571 vmxnet3_rq_destroy_all(adapter);
2572 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002573
2574 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2575
2576
2577 return 0;
2578}
2579
2580
2581void
2582vmxnet3_force_close(struct vmxnet3_adapter *adapter)
2583{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002584 int i;
2585
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002586 /*
2587 * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
2588 * vmxnet3_close() will deadlock.
2589 */
2590 BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
2591
2592 /* we need to enable NAPI, otherwise dev_close will deadlock */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002593 for (i = 0; i < adapter->num_rx_queues; i++)
2594 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002595 dev_close(adapter->netdev);
2596}
2597
2598
2599static int
2600vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
2601{
2602 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2603 int err = 0;
2604
2605 if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
2606 return -EINVAL;
2607
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002608 netdev->mtu = new_mtu;
2609
2610 /*
2611 * Reset_work may be in the middle of resetting the device, wait for its
2612 * completion.
2613 */
2614 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2615 msleep(1);
2616
2617 if (netif_running(netdev)) {
2618 vmxnet3_quiesce_dev(adapter);
2619 vmxnet3_reset_dev(adapter);
2620
2621 /* we need to re-create the rx queue based on the new mtu */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002622 vmxnet3_rq_destroy_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002623 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002624 err = vmxnet3_rq_create_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002625 if (err) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002626 printk(KERN_ERR "%s: failed to re-create rx queues,"
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002627 " error %d. Closing it.\n", netdev->name, err);
2628 goto out;
2629 }
2630
2631 err = vmxnet3_activate_dev(adapter);
2632 if (err) {
2633 printk(KERN_ERR "%s: failed to re-activate, error %d. "
2634 "Closing it\n", netdev->name, err);
2635 goto out;
2636 }
2637 }
2638
2639out:
2640 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2641 if (err)
2642 vmxnet3_force_close(adapter);
2643
2644 return err;
2645}
2646
2647
2648static void
2649vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
2650{
2651 struct net_device *netdev = adapter->netdev;
2652
Michał Mirosława0d27302011-04-18 13:31:21 +00002653 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
2654 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX |
Jesse Gross72e85c42011-06-23 13:04:39 +00002655 NETIF_F_HW_VLAN_RX | NETIF_F_TSO | NETIF_F_TSO6 |
2656 NETIF_F_LRO;
Michał Mirosława0d27302011-04-18 13:31:21 +00002657 if (dma64)
Shreyas Bhatewaraebbf9292011-07-20 17:21:51 +00002658 netdev->hw_features |= NETIF_F_HIGHDMA;
Jesse Gross72e85c42011-06-23 13:04:39 +00002659 netdev->vlan_features = netdev->hw_features &
2660 ~(NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
2661 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_FILTER;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002662
Michał Mirosława0d27302011-04-18 13:31:21 +00002663 netdev_info(adapter->netdev,
2664 "features: sg csum vlan jf tso tsoIPv6 lro%s\n",
2665 dma64 ? " highDMA" : "");
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002666}
2667
2668
2669static void
2670vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2671{
2672 u32 tmp;
2673
2674 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
2675 *(u32 *)mac = tmp;
2676
2677 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
2678 mac[4] = tmp & 0xff;
2679 mac[5] = (tmp >> 8) & 0xff;
2680}
2681
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002682#ifdef CONFIG_PCI_MSI
2683
2684/*
2685 * Enable MSIx vectors.
2686 * Returns :
2687 * 0 on successful enabling of required vectors,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002688 * VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002689 * could be enabled.
2690 * number of vectors which can be enabled otherwise (this number is smaller
2691 * than VMXNET3_LINUX_MIN_MSIX_VECT)
2692 */
2693
2694static int
2695vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter,
2696 int vectors)
2697{
2698 int err = 0, vector_threshold;
2699 vector_threshold = VMXNET3_LINUX_MIN_MSIX_VECT;
2700
2701 while (vectors >= vector_threshold) {
2702 err = pci_enable_msix(adapter->pdev, adapter->intr.msix_entries,
2703 vectors);
2704 if (!err) {
2705 adapter->intr.num_intrs = vectors;
2706 return 0;
2707 } else if (err < 0) {
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002708 netdev_err(adapter->netdev,
2709 "Failed to enable MSI-X, error: %d\n", err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002710 vectors = 0;
2711 } else if (err < vector_threshold) {
2712 break;
2713 } else {
2714 /* If fails to enable required number of MSI-x vectors
Shreyas Bhatewara7e96fbf2011-01-14 15:00:03 +00002715 * try enabling minimum number of vectors required.
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002716 */
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002717 netdev_err(adapter->netdev,
2718 "Failed to enable %d MSI-X, trying %d instead\n",
2719 vectors, vector_threshold);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002720 vectors = vector_threshold;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002721 }
2722 }
2723
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002724 netdev_info(adapter->netdev,
2725 "Number of MSI-X interrupts which can be allocated are lower than min threshold required.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002726 return err;
2727}
2728
2729
2730#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002731
2732static void
2733vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
2734{
2735 u32 cfg;
Roland Dreiere328d412011-05-06 08:32:53 +00002736 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002737
2738 /* intr settings */
Roland Dreiere328d412011-05-06 08:32:53 +00002739 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002740 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2741 VMXNET3_CMD_GET_CONF_INTR);
2742 cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Roland Dreiere328d412011-05-06 08:32:53 +00002743 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002744 adapter->intr.type = cfg & 0x3;
2745 adapter->intr.mask_mode = (cfg >> 2) & 0x3;
2746
2747 if (adapter->intr.type == VMXNET3_IT_AUTO) {
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002748 adapter->intr.type = VMXNET3_IT_MSIX;
2749 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002750
Randy Dunlap8f7e5242009-10-14 20:38:58 -07002751#ifdef CONFIG_PCI_MSI
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002752 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002753 int vector, err = 0;
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002754
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002755 adapter->intr.num_intrs = (adapter->share_intr ==
2756 VMXNET3_INTR_TXSHARE) ? 1 :
2757 adapter->num_tx_queues;
2758 adapter->intr.num_intrs += (adapter->share_intr ==
2759 VMXNET3_INTR_BUDDYSHARE) ? 0 :
2760 adapter->num_rx_queues;
2761 adapter->intr.num_intrs += 1; /* for link event */
2762
2763 adapter->intr.num_intrs = (adapter->intr.num_intrs >
2764 VMXNET3_LINUX_MIN_MSIX_VECT
2765 ? adapter->intr.num_intrs :
2766 VMXNET3_LINUX_MIN_MSIX_VECT);
2767
2768 for (vector = 0; vector < adapter->intr.num_intrs; vector++)
2769 adapter->intr.msix_entries[vector].entry = vector;
2770
2771 err = vmxnet3_acquire_msix_vectors(adapter,
2772 adapter->intr.num_intrs);
2773 /* If we cannot allocate one MSIx vector per queue
2774 * then limit the number of rx queues to 1
2775 */
2776 if (err == VMXNET3_LINUX_MIN_MSIX_VECT) {
2777 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
Shreyas Bhatewara7e96fbf2011-01-14 15:00:03 +00002778 || adapter->num_rx_queues != 1) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002779 adapter->share_intr = VMXNET3_INTR_TXSHARE;
2780 printk(KERN_ERR "Number of rx queues : 1\n");
2781 adapter->num_rx_queues = 1;
2782 adapter->intr.num_intrs =
2783 VMXNET3_LINUX_MIN_MSIX_VECT;
2784 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002785 return;
2786 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002787 if (!err)
2788 return;
2789
2790 /* If we cannot allocate MSIx vectors use only one rx queue */
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002791 netdev_info(adapter->netdev,
2792 "Failed to enable MSI-X, error %d . Limiting #rx queues to 1, try MSI.\n",
2793 err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002794
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002795 adapter->intr.type = VMXNET3_IT_MSI;
2796 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002797
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002798 if (adapter->intr.type == VMXNET3_IT_MSI) {
2799 int err;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002800 err = pci_enable_msi(adapter->pdev);
2801 if (!err) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002802 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002803 adapter->intr.num_intrs = 1;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002804 return;
2805 }
2806 }
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002807#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002808
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002809 adapter->num_rx_queues = 1;
2810 printk(KERN_INFO "Using INTx interrupt, #Rx queues: 1.\n");
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002811 adapter->intr.type = VMXNET3_IT_INTX;
2812
2813 /* INT-X related setting */
2814 adapter->intr.num_intrs = 1;
2815}
2816
2817
2818static void
2819vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
2820{
2821 if (adapter->intr.type == VMXNET3_IT_MSIX)
2822 pci_disable_msix(adapter->pdev);
2823 else if (adapter->intr.type == VMXNET3_IT_MSI)
2824 pci_disable_msi(adapter->pdev);
2825 else
2826 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
2827}
2828
2829
2830static void
2831vmxnet3_tx_timeout(struct net_device *netdev)
2832{
2833 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2834 adapter->tx_timeout_count++;
2835
2836 printk(KERN_ERR "%s: tx hang\n", adapter->netdev->name);
2837 schedule_work(&adapter->work);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002838 netif_wake_queue(adapter->netdev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002839}
2840
2841
2842static void
2843vmxnet3_reset_work(struct work_struct *data)
2844{
2845 struct vmxnet3_adapter *adapter;
2846
2847 adapter = container_of(data, struct vmxnet3_adapter, work);
2848
2849 /* if another thread is resetting the device, no need to proceed */
2850 if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2851 return;
2852
2853 /* if the device is closed, we must leave it alone */
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002854 rtnl_lock();
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002855 if (netif_running(adapter->netdev)) {
2856 printk(KERN_INFO "%s: resetting\n", adapter->netdev->name);
2857 vmxnet3_quiesce_dev(adapter);
2858 vmxnet3_reset_dev(adapter);
2859 vmxnet3_activate_dev(adapter);
2860 } else {
2861 printk(KERN_INFO "%s: already closed\n", adapter->netdev->name);
2862 }
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002863 rtnl_unlock();
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002864
2865 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2866}
2867
2868
2869static int __devinit
2870vmxnet3_probe_device(struct pci_dev *pdev,
2871 const struct pci_device_id *id)
2872{
2873 static const struct net_device_ops vmxnet3_netdev_ops = {
2874 .ndo_open = vmxnet3_open,
2875 .ndo_stop = vmxnet3_close,
2876 .ndo_start_xmit = vmxnet3_xmit_frame,
2877 .ndo_set_mac_address = vmxnet3_set_mac_addr,
2878 .ndo_change_mtu = vmxnet3_change_mtu,
Michał Mirosława0d27302011-04-18 13:31:21 +00002879 .ndo_set_features = vmxnet3_set_features,
stephen hemminger95305f62011-06-08 14:53:57 +00002880 .ndo_get_stats64 = vmxnet3_get_stats64,
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002881 .ndo_tx_timeout = vmxnet3_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002882 .ndo_set_rx_mode = vmxnet3_set_mc,
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002883 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
2884 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
2885#ifdef CONFIG_NET_POLL_CONTROLLER
2886 .ndo_poll_controller = vmxnet3_netpoll,
2887#endif
2888 };
2889 int err;
2890 bool dma64 = false; /* stupid gcc */
2891 u32 ver;
2892 struct net_device *netdev;
2893 struct vmxnet3_adapter *adapter;
2894 u8 mac[ETH_ALEN];
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002895 int size;
2896 int num_tx_queues;
2897 int num_rx_queues;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002898
Shreyas Bhatewarae154b632011-05-10 06:13:56 +00002899 if (!pci_msi_enabled())
2900 enable_mq = 0;
2901
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002902#ifdef VMXNET3_RSS
2903 if (enable_mq)
2904 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
2905 (int)num_online_cpus());
2906 else
2907#endif
2908 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002909 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002910
2911 if (enable_mq)
2912 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
2913 (int)num_online_cpus());
2914 else
2915 num_tx_queues = 1;
2916
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002917 num_tx_queues = rounddown_pow_of_two(num_tx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002918 netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
2919 max(num_tx_queues, num_rx_queues));
2920 printk(KERN_INFO "# of Tx queues : %d, # of Rx queues : %d\n",
2921 num_tx_queues, num_rx_queues);
2922
Joe Perches41de8d42012-01-29 13:47:52 +00002923 if (!netdev)
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002924 return -ENOMEM;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002925
2926 pci_set_drvdata(pdev, netdev);
2927 adapter = netdev_priv(netdev);
2928 adapter->netdev = netdev;
2929 adapter->pdev = pdev;
2930
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002931 spin_lock_init(&adapter->cmd_lock);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002932 adapter->shared = pci_alloc_consistent(adapter->pdev,
2933 sizeof(struct Vmxnet3_DriverShared),
2934 &adapter->shared_pa);
2935 if (!adapter->shared) {
2936 printk(KERN_ERR "Failed to allocate memory for %s\n",
2937 pci_name(pdev));
2938 err = -ENOMEM;
2939 goto err_alloc_shared;
2940 }
2941
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002942 adapter->num_rx_queues = num_rx_queues;
2943 adapter->num_tx_queues = num_tx_queues;
2944
2945 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
2946 size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
2947 adapter->tqd_start = pci_alloc_consistent(adapter->pdev, size,
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002948 &adapter->queue_desc_pa);
2949
2950 if (!adapter->tqd_start) {
2951 printk(KERN_ERR "Failed to allocate memory for %s\n",
2952 pci_name(pdev));
2953 err = -ENOMEM;
2954 goto err_alloc_queue_desc;
2955 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002956 adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
2957 adapter->num_tx_queues);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002958
2959 adapter->pm_conf = kmalloc(sizeof(struct Vmxnet3_PMConf), GFP_KERNEL);
2960 if (adapter->pm_conf == NULL) {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002961 err = -ENOMEM;
2962 goto err_alloc_pm;
2963 }
2964
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002965#ifdef VMXNET3_RSS
2966
2967 adapter->rss_conf = kmalloc(sizeof(struct UPT1_RSSConf), GFP_KERNEL);
2968 if (adapter->rss_conf == NULL) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002969 err = -ENOMEM;
2970 goto err_alloc_rss;
2971 }
2972#endif /* VMXNET3_RSS */
2973
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002974 err = vmxnet3_alloc_pci_resources(adapter, &dma64);
2975 if (err < 0)
2976 goto err_alloc_pci;
2977
2978 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
2979 if (ver & 1) {
2980 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
2981 } else {
2982 printk(KERN_ERR "Incompatible h/w version (0x%x) for adapter"
2983 " %s\n", ver, pci_name(pdev));
2984 err = -EBUSY;
2985 goto err_ver;
2986 }
2987
2988 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
2989 if (ver & 1) {
2990 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
2991 } else {
2992 printk(KERN_ERR "Incompatible upt version (0x%x) for "
2993 "adapter %s\n", ver, pci_name(pdev));
2994 err = -EBUSY;
2995 goto err_ver;
2996 }
2997
Shreyas Bhatewarae101e7d2011-07-20 16:01:11 +00002998 SET_NETDEV_DEV(netdev, &pdev->dev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002999 vmxnet3_declare_features(adapter, dma64);
3000
3001 adapter->dev_number = atomic_read(&devices_found);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003002
3003 adapter->share_intr = irq_share_mode;
3004 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE &&
3005 adapter->num_tx_queues != adapter->num_rx_queues)
3006 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
3007
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003008 vmxnet3_alloc_intr_resources(adapter);
3009
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003010#ifdef VMXNET3_RSS
3011 if (adapter->num_rx_queues > 1 &&
3012 adapter->intr.type == VMXNET3_IT_MSIX) {
3013 adapter->rss = true;
3014 printk(KERN_INFO "RSS is enabled.\n");
3015 } else {
3016 adapter->rss = false;
3017 }
3018#endif
3019
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003020 vmxnet3_read_mac_addr(adapter, mac);
3021 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3022
3023 netdev->netdev_ops = &vmxnet3_netdev_ops;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003024 vmxnet3_set_ethtool_ops(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003025 netdev->watchdog_timeo = 5 * HZ;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003026
3027 INIT_WORK(&adapter->work, vmxnet3_reset_work);
3028
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003029 if (adapter->intr.type == VMXNET3_IT_MSIX) {
3030 int i;
3031 for (i = 0; i < adapter->num_rx_queues; i++) {
3032 netif_napi_add(adapter->netdev,
3033 &adapter->rx_queue[i].napi,
3034 vmxnet3_poll_rx_only, 64);
3035 }
3036 } else {
3037 netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
3038 vmxnet3_poll, 64);
3039 }
3040
3041 netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
3042 netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
3043
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003044 err = register_netdev(netdev);
3045
3046 if (err) {
3047 printk(KERN_ERR "Failed to register adapter %s\n",
3048 pci_name(pdev));
3049 goto err_register;
3050 }
3051
3052 set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00003053 vmxnet3_check_link(adapter, false);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003054 atomic_inc(&devices_found);
3055 return 0;
3056
3057err_register:
3058 vmxnet3_free_intr_resources(adapter);
3059err_ver:
3060 vmxnet3_free_pci_resources(adapter);
3061err_alloc_pci:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003062#ifdef VMXNET3_RSS
3063 kfree(adapter->rss_conf);
3064err_alloc_rss:
3065#endif
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003066 kfree(adapter->pm_conf);
3067err_alloc_pm:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003068 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3069 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003070err_alloc_queue_desc:
3071 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3072 adapter->shared, adapter->shared_pa);
3073err_alloc_shared:
3074 pci_set_drvdata(pdev, NULL);
3075 free_netdev(netdev);
3076 return err;
3077}
3078
3079
3080static void __devexit
3081vmxnet3_remove_device(struct pci_dev *pdev)
3082{
3083 struct net_device *netdev = pci_get_drvdata(pdev);
3084 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003085 int size = 0;
3086 int num_rx_queues;
3087
3088#ifdef VMXNET3_RSS
3089 if (enable_mq)
3090 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3091 (int)num_online_cpus());
3092 else
3093#endif
3094 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07003095 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003096
Tejun Heo23f333a2010-12-12 16:45:14 +01003097 cancel_work_sync(&adapter->work);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003098
3099 unregister_netdev(netdev);
3100
3101 vmxnet3_free_intr_resources(adapter);
3102 vmxnet3_free_pci_resources(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003103#ifdef VMXNET3_RSS
3104 kfree(adapter->rss_conf);
3105#endif
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003106 kfree(adapter->pm_conf);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003107
3108 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3109 size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
3110 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3111 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003112 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3113 adapter->shared, adapter->shared_pa);
3114 free_netdev(netdev);
3115}
3116
3117
3118#ifdef CONFIG_PM
3119
3120static int
3121vmxnet3_suspend(struct device *device)
3122{
3123 struct pci_dev *pdev = to_pci_dev(device);
3124 struct net_device *netdev = pci_get_drvdata(pdev);
3125 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3126 struct Vmxnet3_PMConf *pmConf;
3127 struct ethhdr *ehdr;
3128 struct arphdr *ahdr;
3129 u8 *arpreq;
3130 struct in_device *in_dev;
3131 struct in_ifaddr *ifa;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003132 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003133 int i = 0;
3134
3135 if (!netif_running(netdev))
3136 return 0;
3137
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003138 for (i = 0; i < adapter->num_rx_queues; i++)
3139 napi_disable(&adapter->rx_queue[i].napi);
3140
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003141 vmxnet3_disable_all_intrs(adapter);
3142 vmxnet3_free_irqs(adapter);
3143 vmxnet3_free_intr_resources(adapter);
3144
3145 netif_device_detach(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003146 netif_tx_stop_all_queues(netdev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003147
3148 /* Create wake-up filters. */
3149 pmConf = adapter->pm_conf;
3150 memset(pmConf, 0, sizeof(*pmConf));
3151
3152 if (adapter->wol & WAKE_UCAST) {
3153 pmConf->filters[i].patternSize = ETH_ALEN;
3154 pmConf->filters[i].maskSize = 1;
3155 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
3156 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
3157
Harvey Harrison3843e512010-10-21 18:05:32 +00003158 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003159 i++;
3160 }
3161
3162 if (adapter->wol & WAKE_ARP) {
3163 in_dev = in_dev_get(netdev);
3164 if (!in_dev)
3165 goto skip_arp;
3166
3167 ifa = (struct in_ifaddr *)in_dev->ifa_list;
3168 if (!ifa)
3169 goto skip_arp;
3170
3171 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
3172 sizeof(struct arphdr) + /* ARP header */
3173 2 * ETH_ALEN + /* 2 Ethernet addresses*/
3174 2 * sizeof(u32); /*2 IPv4 addresses */
3175 pmConf->filters[i].maskSize =
3176 (pmConf->filters[i].patternSize - 1) / 8 + 1;
3177
3178 /* ETH_P_ARP in Ethernet header. */
3179 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
3180 ehdr->h_proto = htons(ETH_P_ARP);
3181
3182 /* ARPOP_REQUEST in ARP header. */
3183 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
3184 ahdr->ar_op = htons(ARPOP_REQUEST);
3185 arpreq = (u8 *)(ahdr + 1);
3186
3187 /* The Unicast IPv4 address in 'tip' field. */
3188 arpreq += 2 * ETH_ALEN + sizeof(u32);
3189 *(u32 *)arpreq = ifa->ifa_address;
3190
3191 /* The mask for the relevant bits. */
3192 pmConf->filters[i].mask[0] = 0x00;
3193 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
3194 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
3195 pmConf->filters[i].mask[3] = 0x00;
3196 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
3197 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
3198 in_dev_put(in_dev);
3199
Harvey Harrison3843e512010-10-21 18:05:32 +00003200 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003201 i++;
3202 }
3203
3204skip_arp:
3205 if (adapter->wol & WAKE_MAGIC)
Harvey Harrison3843e512010-10-21 18:05:32 +00003206 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003207
3208 pmConf->numFilters = i;
3209
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003210 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3211 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3212 *pmConf));
3213 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3214 pmConf));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003215
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003216 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003217 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3218 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003219 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003220
3221 pci_save_state(pdev);
3222 pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
3223 adapter->wol);
3224 pci_disable_device(pdev);
3225 pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
3226
3227 return 0;
3228}
3229
3230
3231static int
3232vmxnet3_resume(struct device *device)
3233{
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003234 int err, i = 0;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003235 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003236 struct pci_dev *pdev = to_pci_dev(device);
3237 struct net_device *netdev = pci_get_drvdata(pdev);
3238 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3239 struct Vmxnet3_PMConf *pmConf;
3240
3241 if (!netif_running(netdev))
3242 return 0;
3243
3244 /* Destroy wake-up filters. */
3245 pmConf = adapter->pm_conf;
3246 memset(pmConf, 0, sizeof(*pmConf));
3247
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003248 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3249 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3250 *pmConf));
Harvey Harrison0561cf32010-10-21 18:05:34 +00003251 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003252 pmConf));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003253
3254 netif_device_attach(netdev);
3255 pci_set_power_state(pdev, PCI_D0);
3256 pci_restore_state(pdev);
3257 err = pci_enable_device_mem(pdev);
3258 if (err != 0)
3259 return err;
3260
3261 pci_enable_wake(pdev, PCI_D0, 0);
3262
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003263 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003264 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3265 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003266 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003267 vmxnet3_alloc_intr_resources(adapter);
3268 vmxnet3_request_irqs(adapter);
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003269 for (i = 0; i < adapter->num_rx_queues; i++)
3270 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003271 vmxnet3_enable_all_intrs(adapter);
3272
3273 return 0;
3274}
3275
Alexey Dobriyan47145212009-12-14 18:00:08 -08003276static const struct dev_pm_ops vmxnet3_pm_ops = {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003277 .suspend = vmxnet3_suspend,
3278 .resume = vmxnet3_resume,
3279};
3280#endif
3281
3282static struct pci_driver vmxnet3_driver = {
3283 .name = vmxnet3_driver_name,
3284 .id_table = vmxnet3_pciid_table,
3285 .probe = vmxnet3_probe_device,
3286 .remove = __devexit_p(vmxnet3_remove_device),
3287#ifdef CONFIG_PM
3288 .driver.pm = &vmxnet3_pm_ops,
3289#endif
3290};
3291
3292
3293static int __init
3294vmxnet3_init_module(void)
3295{
3296 printk(KERN_INFO "%s - version %s\n", VMXNET3_DRIVER_DESC,
3297 VMXNET3_DRIVER_VERSION_REPORT);
3298 return pci_register_driver(&vmxnet3_driver);
3299}
3300
3301module_init(vmxnet3_init_module);
3302
3303
3304static void
3305vmxnet3_exit_module(void)
3306{
3307 pci_unregister_driver(&vmxnet3_driver);
3308}
3309
3310module_exit(vmxnet3_exit_module);
3311
3312MODULE_AUTHOR("VMware, Inc.");
3313MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
3314MODULE_LICENSE("GPL v2");
3315MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);