blob: d43df93dc83a4aab838d2177fb8d7534731a8042 [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);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001731
Neil Horman6b4741d2014-03-10 06:55:55 -04001732 switch (adapter->intr.type) {
Arnd Bergmann5e764c52014-03-13 10:44:34 +01001733#ifdef CONFIG_PCI_MSI
1734 case VMXNET3_IT_MSIX: {
1735 int i;
Neil Horman6b4741d2014-03-10 06:55:55 -04001736 for (i = 0; i < adapter->num_rx_queues; i++)
1737 vmxnet3_msix_rx(0, &adapter->rx_queue[i]);
1738 break;
Arnd Bergmann5e764c52014-03-13 10:44:34 +01001739 }
1740#endif
Neil Horman6b4741d2014-03-10 06:55:55 -04001741 case VMXNET3_IT_MSI:
1742 default:
1743 vmxnet3_intr(0, adapter->netdev);
1744 break;
1745 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001746
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001747}
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001748#endif /* CONFIG_NET_POLL_CONTROLLER */
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001749
1750static int
1751vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
1752{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001753 struct vmxnet3_intr *intr = &adapter->intr;
1754 int err = 0, i;
1755 int vector = 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001756
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001757#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001758 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001759 for (i = 0; i < adapter->num_tx_queues; i++) {
1760 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1761 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
1762 adapter->netdev->name, vector);
1763 err = request_irq(
1764 intr->msix_entries[vector].vector,
1765 vmxnet3_msix_tx, 0,
1766 adapter->tx_queue[i].name,
1767 &adapter->tx_queue[i]);
1768 } else {
1769 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
1770 adapter->netdev->name, vector);
1771 }
1772 if (err) {
1773 dev_err(&adapter->netdev->dev,
1774 "Failed to request irq for MSIX, %s, "
1775 "error %d\n",
1776 adapter->tx_queue[i].name, err);
1777 return err;
1778 }
1779
1780 /* Handle the case where only 1 MSIx was allocated for
1781 * all tx queues */
1782 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1783 for (; i < adapter->num_tx_queues; i++)
1784 adapter->tx_queue[i].comp_ring.intr_idx
1785 = vector;
1786 vector++;
1787 break;
1788 } else {
1789 adapter->tx_queue[i].comp_ring.intr_idx
1790 = vector++;
1791 }
1792 }
1793 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
1794 vector = 0;
1795
1796 for (i = 0; i < adapter->num_rx_queues; i++) {
1797 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
1798 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
1799 adapter->netdev->name, vector);
1800 else
1801 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
1802 adapter->netdev->name, vector);
1803 err = request_irq(intr->msix_entries[vector].vector,
1804 vmxnet3_msix_rx, 0,
1805 adapter->rx_queue[i].name,
1806 &(adapter->rx_queue[i]));
1807 if (err) {
1808 printk(KERN_ERR "Failed to request irq for MSIX"
1809 ", %s, error %d\n",
1810 adapter->rx_queue[i].name, err);
1811 return err;
1812 }
1813
1814 adapter->rx_queue[i].comp_ring.intr_idx = vector++;
1815 }
1816
1817 sprintf(intr->event_msi_vector_name, "%s-event-%d",
1818 adapter->netdev->name, vector);
1819 err = request_irq(intr->msix_entries[vector].vector,
1820 vmxnet3_msix_event, 0,
1821 intr->event_msi_vector_name, adapter->netdev);
1822 intr->event_intr_idx = vector;
1823
1824 } else if (intr->type == VMXNET3_IT_MSI) {
1825 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001826 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
1827 adapter->netdev->name, adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001828 } else {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001829#endif
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001830 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001831 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
1832 IRQF_SHARED, adapter->netdev->name,
1833 adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001834#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001835 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001836#endif
1837 intr->num_intrs = vector + 1;
1838 if (err) {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001839 printk(KERN_ERR "Failed to request irq %s (intr type:%d), error"
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001840 ":%d\n", adapter->netdev->name, intr->type, err);
1841 } else {
1842 /* Number of rx queues will not change after this */
1843 for (i = 0; i < adapter->num_rx_queues; i++) {
1844 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
1845 rq->qid = i;
1846 rq->qid2 = i + adapter->num_rx_queues;
1847 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001848
1849
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001850
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001851 /* init our intr settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001852 for (i = 0; i < intr->num_intrs; i++)
1853 intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
1854 if (adapter->intr.type != VMXNET3_IT_MSIX) {
1855 adapter->intr.event_intr_idx = 0;
1856 for (i = 0; i < adapter->num_tx_queues; i++)
1857 adapter->tx_queue[i].comp_ring.intr_idx = 0;
1858 adapter->rx_queue[0].comp_ring.intr_idx = 0;
1859 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001860
1861 printk(KERN_INFO "%s: intr type %u, mode %u, %u vectors "
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001862 "allocated\n", adapter->netdev->name, intr->type,
1863 intr->mask_mode, intr->num_intrs);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001864 }
1865
1866 return err;
1867}
1868
1869
1870static void
1871vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
1872{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001873 struct vmxnet3_intr *intr = &adapter->intr;
1874 BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001875
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001876 switch (intr->type) {
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001877#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001878 case VMXNET3_IT_MSIX:
1879 {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001880 int i, vector = 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001881
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001882 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1883 for (i = 0; i < adapter->num_tx_queues; i++) {
1884 free_irq(intr->msix_entries[vector++].vector,
1885 &(adapter->tx_queue[i]));
1886 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
1887 break;
1888 }
1889 }
1890
1891 for (i = 0; i < adapter->num_rx_queues; i++) {
1892 free_irq(intr->msix_entries[vector++].vector,
1893 &(adapter->rx_queue[i]));
1894 }
1895
1896 free_irq(intr->msix_entries[vector].vector,
1897 adapter->netdev);
1898 BUG_ON(vector >= intr->num_intrs);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001899 break;
1900 }
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001901#endif
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001902 case VMXNET3_IT_MSI:
1903 free_irq(adapter->pdev->irq, adapter->netdev);
1904 break;
1905 case VMXNET3_IT_INTX:
1906 free_irq(adapter->pdev->irq, adapter->netdev);
1907 break;
1908 default:
1909 BUG_ON(true);
1910 }
1911}
1912
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001913
1914static void
1915vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
1916{
Jesse Gross72e85c42011-06-23 13:04:39 +00001917 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1918 u16 vid;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001919
Jesse Gross72e85c42011-06-23 13:04:39 +00001920 /* allow untagged pkts */
1921 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1922
1923 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1924 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001925}
1926
1927
Jiri Pirko8e586132011-12-08 19:52:37 -05001928static int
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001929vmxnet3_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1930{
1931 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001932
Jesse Grossf6957f82011-08-07 23:15:47 +00001933 if (!(netdev->flags & IFF_PROMISC)) {
1934 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1935 unsigned long flags;
1936
1937 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1938 spin_lock_irqsave(&adapter->cmd_lock, flags);
1939 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1940 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1941 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1942 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001943
1944 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001945
1946 return 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001947}
1948
1949
Jiri Pirko8e586132011-12-08 19:52:37 -05001950static int
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001951vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1952{
1953 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001954
Jesse Grossf6957f82011-08-07 23:15:47 +00001955 if (!(netdev->flags & IFF_PROMISC)) {
1956 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1957 unsigned long flags;
1958
1959 VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
1960 spin_lock_irqsave(&adapter->cmd_lock, flags);
1961 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1962 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1963 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1964 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001965
1966 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001967
1968 return 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001969}
1970
1971
1972static u8 *
1973vmxnet3_copy_mc(struct net_device *netdev)
1974{
1975 u8 *buf = NULL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001976 u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001977
1978 /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
1979 if (sz <= 0xffff) {
1980 /* We may be called with BH disabled */
1981 buf = kmalloc(sz, GFP_ATOMIC);
1982 if (buf) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00001983 struct netdev_hw_addr *ha;
Jiri Pirko567ec872010-02-23 23:17:07 +00001984 int i = 0;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001985
Jiri Pirko22bedad2010-04-01 21:22:57 +00001986 netdev_for_each_mc_addr(ha, netdev)
1987 memcpy(buf + i++ * ETH_ALEN, ha->addr,
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001988 ETH_ALEN);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07001989 }
1990 }
1991 return buf;
1992}
1993
1994
1995static void
1996vmxnet3_set_mc(struct net_device *netdev)
1997{
1998 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00001999 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002000 struct Vmxnet3_RxFilterConf *rxConf =
2001 &adapter->shared->devRead.rxFilterConf;
2002 u8 *new_table = NULL;
2003 u32 new_mode = VMXNET3_RXM_UCAST;
2004
Jesse Gross72e85c42011-06-23 13:04:39 +00002005 if (netdev->flags & IFF_PROMISC) {
2006 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2007 memset(vfTable, 0, VMXNET3_VFT_SIZE * sizeof(*vfTable));
2008
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002009 new_mode |= VMXNET3_RXM_PROMISC;
Jesse Gross72e85c42011-06-23 13:04:39 +00002010 } else {
2011 vmxnet3_restore_vlan(adapter);
2012 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002013
2014 if (netdev->flags & IFF_BROADCAST)
2015 new_mode |= VMXNET3_RXM_BCAST;
2016
2017 if (netdev->flags & IFF_ALLMULTI)
2018 new_mode |= VMXNET3_RXM_ALL_MULTI;
2019 else
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002020 if (!netdev_mc_empty(netdev)) {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002021 new_table = vmxnet3_copy_mc(netdev);
2022 if (new_table) {
2023 new_mode |= VMXNET3_RXM_MCAST;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002024 rxConf->mfTableLen = cpu_to_le16(
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002025 netdev_mc_count(netdev) * ETH_ALEN);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002026 rxConf->mfTablePA = cpu_to_le64(virt_to_phys(
2027 new_table));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002028 } else {
2029 printk(KERN_INFO "%s: failed to copy mcast list"
2030 ", setting ALL_MULTI\n", netdev->name);
2031 new_mode |= VMXNET3_RXM_ALL_MULTI;
2032 }
2033 }
2034
2035
2036 if (!(new_mode & VMXNET3_RXM_MCAST)) {
2037 rxConf->mfTableLen = 0;
2038 rxConf->mfTablePA = 0;
2039 }
2040
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002041 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002042 if (new_mode != rxConf->rxMode) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002043 rxConf->rxMode = cpu_to_le32(new_mode);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002044 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2045 VMXNET3_CMD_UPDATE_RX_MODE);
Jesse Gross72e85c42011-06-23 13:04:39 +00002046 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2047 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002048 }
2049
2050 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2051 VMXNET3_CMD_UPDATE_MAC_FILTERS);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002052 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002053
2054 kfree(new_table);
2055}
2056
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002057void
2058vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
2059{
2060 int i;
2061
2062 for (i = 0; i < adapter->num_rx_queues; i++)
2063 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2064}
2065
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002066
2067/*
2068 * Set up driver_shared based on settings in adapter.
2069 */
2070
2071static void
2072vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2073{
2074 struct Vmxnet3_DriverShared *shared = adapter->shared;
2075 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2076 struct Vmxnet3_TxQueueConf *tqc;
2077 struct Vmxnet3_RxQueueConf *rqc;
2078 int i;
2079
2080 memset(shared, 0, sizeof(*shared));
2081
2082 /* driver settings */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002083 shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2084 devRead->misc.driverInfo.version = cpu_to_le32(
2085 VMXNET3_DRIVER_VERSION_NUM);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002086 devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2087 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2088 devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002089 *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2090 *((u32 *)&devRead->misc.driverInfo.gos));
2091 devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2092 devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002093
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002094 devRead->misc.ddPA = cpu_to_le64(virt_to_phys(adapter));
2095 devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002096
2097 /* set up feature flags */
Michał Mirosława0d27302011-04-18 13:31:21 +00002098 if (adapter->netdev->features & NETIF_F_RXCSUM)
Harvey Harrison3843e512010-10-21 18:05:32 +00002099 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002100
Michał Mirosława0d27302011-04-18 13:31:21 +00002101 if (adapter->netdev->features & NETIF_F_LRO) {
Harvey Harrison3843e512010-10-21 18:05:32 +00002102 devRead->misc.uptFeatures |= UPT1_F_LRO;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002103 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002104 }
Shreyas Bhatewara54da3d02011-01-14 14:59:36 +00002105 if (adapter->netdev->features & NETIF_F_HW_VLAN_RX)
Harvey Harrison3843e512010-10-21 18:05:32 +00002106 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002107
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002108 devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2109 devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2110 devRead->misc.queueDescLen = cpu_to_le32(
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002111 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2112 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002113
2114 /* tx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002115 devRead->misc.numTxQueues = adapter->num_tx_queues;
2116 for (i = 0; i < adapter->num_tx_queues; i++) {
2117 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2118 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2119 tqc = &adapter->tqd_start[i].conf;
2120 tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
2121 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2122 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
2123 tqc->ddPA = cpu_to_le64(virt_to_phys(tq->buf_info));
2124 tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
2125 tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
2126 tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
2127 tqc->ddLen = cpu_to_le32(
2128 sizeof(struct vmxnet3_tx_buf_info) *
2129 tqc->txRingSize);
2130 tqc->intrIdx = tq->comp_ring.intr_idx;
2131 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002132
2133 /* rx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002134 devRead->misc.numRxQueues = adapter->num_rx_queues;
2135 for (i = 0; i < adapter->num_rx_queues; i++) {
2136 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2137 rqc = &adapter->rqd_start[i].conf;
2138 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2139 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2140 rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
2141 rqc->ddPA = cpu_to_le64(virt_to_phys(
2142 rq->buf_info));
2143 rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
2144 rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
2145 rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
2146 rqc->ddLen = cpu_to_le32(
2147 sizeof(struct vmxnet3_rx_buf_info) *
2148 (rqc->rxRingSize[0] +
2149 rqc->rxRingSize[1]));
2150 rqc->intrIdx = rq->comp_ring.intr_idx;
2151 }
2152
2153#ifdef VMXNET3_RSS
2154 memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2155
2156 if (adapter->rss) {
2157 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
2158 devRead->misc.uptFeatures |= UPT1_F_RSS;
2159 devRead->misc.numRxQueues = adapter->num_rx_queues;
2160 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2161 UPT1_RSS_HASH_TYPE_IPV4 |
2162 UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2163 UPT1_RSS_HASH_TYPE_IPV6;
2164 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2165 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2166 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
2167 get_random_bytes(&rssConf->hashKey[0], rssConf->hashKeySize);
2168 for (i = 0; i < rssConf->indTableSize; i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00002169 rssConf->indTable[i] = ethtool_rxfh_indir_default(
2170 i, adapter->num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002171
2172 devRead->rssConfDesc.confVer = 1;
2173 devRead->rssConfDesc.confLen = sizeof(*rssConf);
2174 devRead->rssConfDesc.confPA = virt_to_phys(rssConf);
2175 }
2176
2177#endif /* VMXNET3_RSS */
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002178
2179 /* intr settings */
2180 devRead->intrConf.autoMask = adapter->intr.mask_mode ==
2181 VMXNET3_IMM_AUTO;
2182 devRead->intrConf.numIntrs = adapter->intr.num_intrs;
2183 for (i = 0; i < adapter->intr.num_intrs; i++)
2184 devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
2185
2186 devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
Ronghua Zang6929fe82010-07-15 22:18:47 -07002187 devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002188
2189 /* rx filter settings */
2190 devRead->rxFilterConf.rxMode = 0;
2191 vmxnet3_restore_vlan(adapter);
Shreyas Bhatewaraf9f25022011-01-14 14:59:31 +00002192 vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
2193
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002194 /* the rest are already zeroed */
2195}
2196
2197
2198int
2199vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
2200{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002201 int err, i;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002202 u32 ret;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002203 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002204
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002205 dev_dbg(&adapter->netdev->dev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
2206 " ring sizes %u %u %u\n", adapter->netdev->name,
2207 adapter->skb_buf_size, adapter->rx_buf_per_pkt,
2208 adapter->tx_queue[0].tx_ring.size,
2209 adapter->rx_queue[0].rx_ring[0].size,
2210 adapter->rx_queue[0].rx_ring[1].size);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002211
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002212 vmxnet3_tq_init_all(adapter);
2213 err = vmxnet3_rq_init_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002214 if (err) {
2215 printk(KERN_ERR "Failed to init rx queue for %s: error %d\n",
2216 adapter->netdev->name, err);
2217 goto rq_err;
2218 }
2219
2220 err = vmxnet3_request_irqs(adapter);
2221 if (err) {
2222 printk(KERN_ERR "Failed to setup irq for %s: error %d\n",
2223 adapter->netdev->name, err);
2224 goto irq_err;
2225 }
2226
2227 vmxnet3_setup_driver_shared(adapter);
2228
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002229 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
2230 adapter->shared_pa));
2231 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
2232 adapter->shared_pa));
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002233 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002234 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2235 VMXNET3_CMD_ACTIVATE_DEV);
2236 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002237 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002238
2239 if (ret != 0) {
2240 printk(KERN_ERR "Failed to activate dev %s: error %u\n",
2241 adapter->netdev->name, ret);
2242 err = -EINVAL;
2243 goto activate_err;
2244 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002245
2246 for (i = 0; i < adapter->num_rx_queues; i++) {
2247 VMXNET3_WRITE_BAR0_REG(adapter,
2248 VMXNET3_REG_RXPROD + i * VMXNET3_REG_ALIGN,
2249 adapter->rx_queue[i].rx_ring[0].next2fill);
2250 VMXNET3_WRITE_BAR0_REG(adapter, (VMXNET3_REG_RXPROD2 +
2251 (i * VMXNET3_REG_ALIGN)),
2252 adapter->rx_queue[i].rx_ring[1].next2fill);
2253 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002254
2255 /* Apply the rx filter settins last. */
2256 vmxnet3_set_mc(adapter->netdev);
2257
2258 /*
2259 * Check link state when first activating device. It will start the
2260 * tx queue if the link is up.
2261 */
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00002262 vmxnet3_check_link(adapter, true);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002263 for (i = 0; i < adapter->num_rx_queues; i++)
2264 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002265 vmxnet3_enable_all_intrs(adapter);
2266 clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
2267 return 0;
2268
2269activate_err:
2270 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
2271 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
2272 vmxnet3_free_irqs(adapter);
2273irq_err:
2274rq_err:
2275 /* free up buffers we allocated */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002276 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002277 return err;
2278}
2279
2280
2281void
2282vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
2283{
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002284 unsigned long flags;
2285 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002286 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002287 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002288}
2289
2290
2291int
2292vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
2293{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002294 int i;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002295 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002296 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
2297 return 0;
2298
2299
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002300 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002301 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2302 VMXNET3_CMD_QUIESCE_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002303 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002304 vmxnet3_disable_all_intrs(adapter);
2305
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002306 for (i = 0; i < adapter->num_rx_queues; i++)
2307 napi_disable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002308 netif_tx_disable(adapter->netdev);
2309 adapter->link_speed = 0;
2310 netif_carrier_off(adapter->netdev);
2311
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002312 vmxnet3_tq_cleanup_all(adapter);
2313 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002314 vmxnet3_free_irqs(adapter);
2315 return 0;
2316}
2317
2318
2319static void
2320vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2321{
2322 u32 tmp;
2323
2324 tmp = *(u32 *)mac;
2325 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
2326
2327 tmp = (mac[5] << 8) | mac[4];
2328 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
2329}
2330
2331
2332static int
2333vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
2334{
2335 struct sockaddr *addr = p;
2336 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2337
2338 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2339 vmxnet3_write_mac_addr(adapter, addr->sa_data);
2340
2341 return 0;
2342}
2343
2344
2345/* ==================== initialization and cleanup routines ============ */
2346
2347static int
2348vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
2349{
2350 int err;
2351 unsigned long mmio_start, mmio_len;
2352 struct pci_dev *pdev = adapter->pdev;
2353
2354 err = pci_enable_device(pdev);
2355 if (err) {
2356 printk(KERN_ERR "Failed to enable adapter %s: error %d\n",
2357 pci_name(pdev), err);
2358 return err;
2359 }
2360
2361 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
2362 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
2363 printk(KERN_ERR "pci_set_consistent_dma_mask failed "
2364 "for adapter %s\n", pci_name(pdev));
2365 err = -EIO;
2366 goto err_set_mask;
2367 }
2368 *dma64 = true;
2369 } else {
2370 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
2371 printk(KERN_ERR "pci_set_dma_mask failed for adapter "
2372 "%s\n", pci_name(pdev));
2373 err = -EIO;
2374 goto err_set_mask;
2375 }
2376 *dma64 = false;
2377 }
2378
2379 err = pci_request_selected_regions(pdev, (1 << 2) - 1,
2380 vmxnet3_driver_name);
2381 if (err) {
2382 printk(KERN_ERR "Failed to request region for adapter %s: "
2383 "error %d\n", pci_name(pdev), err);
2384 goto err_set_mask;
2385 }
2386
2387 pci_set_master(pdev);
2388
2389 mmio_start = pci_resource_start(pdev, 0);
2390 mmio_len = pci_resource_len(pdev, 0);
2391 adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
2392 if (!adapter->hw_addr0) {
2393 printk(KERN_ERR "Failed to map bar0 for adapter %s\n",
2394 pci_name(pdev));
2395 err = -EIO;
2396 goto err_ioremap;
2397 }
2398
2399 mmio_start = pci_resource_start(pdev, 1);
2400 mmio_len = pci_resource_len(pdev, 1);
2401 adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
2402 if (!adapter->hw_addr1) {
2403 printk(KERN_ERR "Failed to map bar1 for adapter %s\n",
2404 pci_name(pdev));
2405 err = -EIO;
2406 goto err_bar1;
2407 }
2408 return 0;
2409
2410err_bar1:
2411 iounmap(adapter->hw_addr0);
2412err_ioremap:
2413 pci_release_selected_regions(pdev, (1 << 2) - 1);
2414err_set_mask:
2415 pci_disable_device(pdev);
2416 return err;
2417}
2418
2419
2420static void
2421vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
2422{
2423 BUG_ON(!adapter->pdev);
2424
2425 iounmap(adapter->hw_addr0);
2426 iounmap(adapter->hw_addr1);
2427 pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
2428 pci_disable_device(adapter->pdev);
2429}
2430
2431
2432static void
2433vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
2434{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002435 size_t sz, i, ring0_size, ring1_size, comp_size;
2436 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[0];
2437
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002438
2439 if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
2440 VMXNET3_MAX_ETH_HDR_SIZE) {
2441 adapter->skb_buf_size = adapter->netdev->mtu +
2442 VMXNET3_MAX_ETH_HDR_SIZE;
2443 if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
2444 adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
2445
2446 adapter->rx_buf_per_pkt = 1;
2447 } else {
2448 adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
2449 sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
2450 VMXNET3_MAX_ETH_HDR_SIZE;
2451 adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
2452 }
2453
2454 /*
2455 * for simplicity, force the ring0 size to be a multiple of
2456 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
2457 */
2458 sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002459 ring0_size = adapter->rx_queue[0].rx_ring[0].size;
2460 ring0_size = (ring0_size + sz - 1) / sz * sz;
Shreyas Bhatewaraa53255d2011-01-14 14:59:25 +00002461 ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002462 sz * sz);
2463 ring1_size = adapter->rx_queue[0].rx_ring[1].size;
2464 comp_size = ring0_size + ring1_size;
2465
2466 for (i = 0; i < adapter->num_rx_queues; i++) {
2467 rq = &adapter->rx_queue[i];
2468 rq->rx_ring[0].size = ring0_size;
2469 rq->rx_ring[1].size = ring1_size;
2470 rq->comp_ring.size = comp_size;
2471 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002472}
2473
2474
2475int
2476vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
2477 u32 rx_ring_size, u32 rx_ring2_size)
2478{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002479 int err = 0, i;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002480
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002481 for (i = 0; i < adapter->num_tx_queues; i++) {
2482 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2483 tq->tx_ring.size = tx_ring_size;
2484 tq->data_ring.size = tx_ring_size;
2485 tq->comp_ring.size = tx_ring_size;
2486 tq->shared = &adapter->tqd_start[i].ctrl;
2487 tq->stopped = true;
2488 tq->adapter = adapter;
2489 tq->qid = i;
2490 err = vmxnet3_tq_create(tq, adapter);
2491 /*
2492 * Too late to change num_tx_queues. We cannot do away with
2493 * lesser number of queues than what we asked for
2494 */
2495 if (err)
2496 goto queue_err;
2497 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002498
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002499 adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
2500 adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002501 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002502 for (i = 0; i < adapter->num_rx_queues; i++) {
2503 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2504 /* qid and qid2 for rx queues will be assigned later when num
2505 * of rx queues is finalized after allocating intrs */
2506 rq->shared = &adapter->rqd_start[i].ctrl;
2507 rq->adapter = adapter;
2508 err = vmxnet3_rq_create(rq, adapter);
2509 if (err) {
2510 if (i == 0) {
2511 printk(KERN_ERR "Could not allocate any rx"
2512 "queues. Aborting.\n");
2513 goto queue_err;
2514 } else {
2515 printk(KERN_INFO "Number of rx queues changed "
2516 "to : %d.\n", i);
2517 adapter->num_rx_queues = i;
2518 err = 0;
2519 break;
2520 }
2521 }
2522 }
2523 return err;
2524queue_err:
2525 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002526 return err;
2527}
2528
2529static int
2530vmxnet3_open(struct net_device *netdev)
2531{
2532 struct vmxnet3_adapter *adapter;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002533 int err, i;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002534
2535 adapter = netdev_priv(netdev);
2536
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002537 for (i = 0; i < adapter->num_tx_queues; i++)
2538 spin_lock_init(&adapter->tx_queue[i].tx_lock);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002539
2540 err = vmxnet3_create_queues(adapter, VMXNET3_DEF_TX_RING_SIZE,
2541 VMXNET3_DEF_RX_RING_SIZE,
2542 VMXNET3_DEF_RX_RING_SIZE);
2543 if (err)
2544 goto queue_err;
2545
2546 err = vmxnet3_activate_dev(adapter);
2547 if (err)
2548 goto activate_err;
2549
2550 return 0;
2551
2552activate_err:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002553 vmxnet3_rq_destroy_all(adapter);
2554 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002555queue_err:
2556 return err;
2557}
2558
2559
2560static int
2561vmxnet3_close(struct net_device *netdev)
2562{
2563 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2564
2565 /*
2566 * Reset_work may be in the middle of resetting the device, wait for its
2567 * completion.
2568 */
2569 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2570 msleep(1);
2571
2572 vmxnet3_quiesce_dev(adapter);
2573
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002574 vmxnet3_rq_destroy_all(adapter);
2575 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002576
2577 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2578
2579
2580 return 0;
2581}
2582
2583
2584void
2585vmxnet3_force_close(struct vmxnet3_adapter *adapter)
2586{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002587 int i;
2588
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002589 /*
2590 * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
2591 * vmxnet3_close() will deadlock.
2592 */
2593 BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
2594
2595 /* we need to enable NAPI, otherwise dev_close will deadlock */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002596 for (i = 0; i < adapter->num_rx_queues; i++)
2597 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002598 dev_close(adapter->netdev);
2599}
2600
2601
2602static int
2603vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
2604{
2605 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2606 int err = 0;
2607
2608 if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
2609 return -EINVAL;
2610
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002611 netdev->mtu = new_mtu;
2612
2613 /*
2614 * Reset_work may be in the middle of resetting the device, wait for its
2615 * completion.
2616 */
2617 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2618 msleep(1);
2619
2620 if (netif_running(netdev)) {
2621 vmxnet3_quiesce_dev(adapter);
2622 vmxnet3_reset_dev(adapter);
2623
2624 /* we need to re-create the rx queue based on the new mtu */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002625 vmxnet3_rq_destroy_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002626 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002627 err = vmxnet3_rq_create_all(adapter);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002628 if (err) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002629 printk(KERN_ERR "%s: failed to re-create rx queues,"
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002630 " error %d. Closing it.\n", netdev->name, err);
2631 goto out;
2632 }
2633
2634 err = vmxnet3_activate_dev(adapter);
2635 if (err) {
2636 printk(KERN_ERR "%s: failed to re-activate, error %d. "
2637 "Closing it\n", netdev->name, err);
2638 goto out;
2639 }
2640 }
2641
2642out:
2643 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2644 if (err)
2645 vmxnet3_force_close(adapter);
2646
2647 return err;
2648}
2649
2650
2651static void
2652vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
2653{
2654 struct net_device *netdev = adapter->netdev;
2655
Michał Mirosława0d27302011-04-18 13:31:21 +00002656 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
2657 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX |
Jesse Gross72e85c42011-06-23 13:04:39 +00002658 NETIF_F_HW_VLAN_RX | NETIF_F_TSO | NETIF_F_TSO6 |
2659 NETIF_F_LRO;
Michał Mirosława0d27302011-04-18 13:31:21 +00002660 if (dma64)
Shreyas Bhatewaraebbf9292011-07-20 17:21:51 +00002661 netdev->hw_features |= NETIF_F_HIGHDMA;
Jesse Gross72e85c42011-06-23 13:04:39 +00002662 netdev->vlan_features = netdev->hw_features &
2663 ~(NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
2664 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_FILTER;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002665
Michał Mirosława0d27302011-04-18 13:31:21 +00002666 netdev_info(adapter->netdev,
2667 "features: sg csum vlan jf tso tsoIPv6 lro%s\n",
2668 dma64 ? " highDMA" : "");
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002669}
2670
2671
2672static void
2673vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2674{
2675 u32 tmp;
2676
2677 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
2678 *(u32 *)mac = tmp;
2679
2680 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
2681 mac[4] = tmp & 0xff;
2682 mac[5] = (tmp >> 8) & 0xff;
2683}
2684
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002685#ifdef CONFIG_PCI_MSI
2686
2687/*
2688 * Enable MSIx vectors.
2689 * Returns :
2690 * 0 on successful enabling of required vectors,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002691 * VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002692 * could be enabled.
2693 * number of vectors which can be enabled otherwise (this number is smaller
2694 * than VMXNET3_LINUX_MIN_MSIX_VECT)
2695 */
2696
2697static int
2698vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter,
2699 int vectors)
2700{
2701 int err = 0, vector_threshold;
2702 vector_threshold = VMXNET3_LINUX_MIN_MSIX_VECT;
2703
2704 while (vectors >= vector_threshold) {
2705 err = pci_enable_msix(adapter->pdev, adapter->intr.msix_entries,
2706 vectors);
2707 if (!err) {
2708 adapter->intr.num_intrs = vectors;
2709 return 0;
2710 } else if (err < 0) {
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002711 netdev_err(adapter->netdev,
2712 "Failed to enable MSI-X, error: %d\n", err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002713 vectors = 0;
2714 } else if (err < vector_threshold) {
2715 break;
2716 } else {
2717 /* If fails to enable required number of MSI-x vectors
Shreyas Bhatewara7e96fbf2011-01-14 15:00:03 +00002718 * try enabling minimum number of vectors required.
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002719 */
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002720 netdev_err(adapter->netdev,
2721 "Failed to enable %d MSI-X, trying %d instead\n",
2722 vectors, vector_threshold);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002723 vectors = vector_threshold;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002724 }
2725 }
2726
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002727 netdev_info(adapter->netdev,
2728 "Number of MSI-X interrupts which can be allocated are lower than min threshold required.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002729 return err;
2730}
2731
2732
2733#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002734
2735static void
2736vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
2737{
2738 u32 cfg;
Roland Dreiere328d412011-05-06 08:32:53 +00002739 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002740
2741 /* intr settings */
Roland Dreiere328d412011-05-06 08:32:53 +00002742 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002743 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2744 VMXNET3_CMD_GET_CONF_INTR);
2745 cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Roland Dreiere328d412011-05-06 08:32:53 +00002746 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002747 adapter->intr.type = cfg & 0x3;
2748 adapter->intr.mask_mode = (cfg >> 2) & 0x3;
2749
2750 if (adapter->intr.type == VMXNET3_IT_AUTO) {
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002751 adapter->intr.type = VMXNET3_IT_MSIX;
2752 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002753
Randy Dunlap8f7e5242009-10-14 20:38:58 -07002754#ifdef CONFIG_PCI_MSI
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002755 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002756 int vector, err = 0;
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002757
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002758 adapter->intr.num_intrs = (adapter->share_intr ==
2759 VMXNET3_INTR_TXSHARE) ? 1 :
2760 adapter->num_tx_queues;
2761 adapter->intr.num_intrs += (adapter->share_intr ==
2762 VMXNET3_INTR_BUDDYSHARE) ? 0 :
2763 adapter->num_rx_queues;
2764 adapter->intr.num_intrs += 1; /* for link event */
2765
2766 adapter->intr.num_intrs = (adapter->intr.num_intrs >
2767 VMXNET3_LINUX_MIN_MSIX_VECT
2768 ? adapter->intr.num_intrs :
2769 VMXNET3_LINUX_MIN_MSIX_VECT);
2770
2771 for (vector = 0; vector < adapter->intr.num_intrs; vector++)
2772 adapter->intr.msix_entries[vector].entry = vector;
2773
2774 err = vmxnet3_acquire_msix_vectors(adapter,
2775 adapter->intr.num_intrs);
2776 /* If we cannot allocate one MSIx vector per queue
2777 * then limit the number of rx queues to 1
2778 */
2779 if (err == VMXNET3_LINUX_MIN_MSIX_VECT) {
2780 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
Shreyas Bhatewara7e96fbf2011-01-14 15:00:03 +00002781 || adapter->num_rx_queues != 1) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002782 adapter->share_intr = VMXNET3_INTR_TXSHARE;
2783 printk(KERN_ERR "Number of rx queues : 1\n");
2784 adapter->num_rx_queues = 1;
2785 adapter->intr.num_intrs =
2786 VMXNET3_LINUX_MIN_MSIX_VECT;
2787 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002788 return;
2789 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002790 if (!err)
2791 return;
2792
2793 /* If we cannot allocate MSIx vectors use only one rx queue */
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002794 netdev_info(adapter->netdev,
2795 "Failed to enable MSI-X, error %d . Limiting #rx queues to 1, try MSI.\n",
2796 err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002797
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002798 adapter->intr.type = VMXNET3_IT_MSI;
2799 }
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002800
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002801 if (adapter->intr.type == VMXNET3_IT_MSI) {
2802 int err;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002803 err = pci_enable_msi(adapter->pdev);
2804 if (!err) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002805 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002806 adapter->intr.num_intrs = 1;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002807 return;
2808 }
2809 }
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002810#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002811
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002812 adapter->num_rx_queues = 1;
2813 printk(KERN_INFO "Using INTx interrupt, #Rx queues: 1.\n");
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002814 adapter->intr.type = VMXNET3_IT_INTX;
2815
2816 /* INT-X related setting */
2817 adapter->intr.num_intrs = 1;
2818}
2819
2820
2821static void
2822vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
2823{
2824 if (adapter->intr.type == VMXNET3_IT_MSIX)
2825 pci_disable_msix(adapter->pdev);
2826 else if (adapter->intr.type == VMXNET3_IT_MSI)
2827 pci_disable_msi(adapter->pdev);
2828 else
2829 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
2830}
2831
2832
2833static void
2834vmxnet3_tx_timeout(struct net_device *netdev)
2835{
2836 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2837 adapter->tx_timeout_count++;
2838
2839 printk(KERN_ERR "%s: tx hang\n", adapter->netdev->name);
2840 schedule_work(&adapter->work);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002841 netif_wake_queue(adapter->netdev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002842}
2843
2844
2845static void
2846vmxnet3_reset_work(struct work_struct *data)
2847{
2848 struct vmxnet3_adapter *adapter;
2849
2850 adapter = container_of(data, struct vmxnet3_adapter, work);
2851
2852 /* if another thread is resetting the device, no need to proceed */
2853 if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2854 return;
2855
2856 /* if the device is closed, we must leave it alone */
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002857 rtnl_lock();
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002858 if (netif_running(adapter->netdev)) {
2859 printk(KERN_INFO "%s: resetting\n", adapter->netdev->name);
2860 vmxnet3_quiesce_dev(adapter);
2861 vmxnet3_reset_dev(adapter);
2862 vmxnet3_activate_dev(adapter);
2863 } else {
2864 printk(KERN_INFO "%s: already closed\n", adapter->netdev->name);
2865 }
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002866 rtnl_unlock();
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002867
2868 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2869}
2870
2871
2872static int __devinit
2873vmxnet3_probe_device(struct pci_dev *pdev,
2874 const struct pci_device_id *id)
2875{
2876 static const struct net_device_ops vmxnet3_netdev_ops = {
2877 .ndo_open = vmxnet3_open,
2878 .ndo_stop = vmxnet3_close,
2879 .ndo_start_xmit = vmxnet3_xmit_frame,
2880 .ndo_set_mac_address = vmxnet3_set_mac_addr,
2881 .ndo_change_mtu = vmxnet3_change_mtu,
Michał Mirosława0d27302011-04-18 13:31:21 +00002882 .ndo_set_features = vmxnet3_set_features,
stephen hemminger95305f62011-06-08 14:53:57 +00002883 .ndo_get_stats64 = vmxnet3_get_stats64,
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002884 .ndo_tx_timeout = vmxnet3_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002885 .ndo_set_rx_mode = vmxnet3_set_mc,
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002886 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
2887 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
2888#ifdef CONFIG_NET_POLL_CONTROLLER
2889 .ndo_poll_controller = vmxnet3_netpoll,
2890#endif
2891 };
2892 int err;
2893 bool dma64 = false; /* stupid gcc */
2894 u32 ver;
2895 struct net_device *netdev;
2896 struct vmxnet3_adapter *adapter;
2897 u8 mac[ETH_ALEN];
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002898 int size;
2899 int num_tx_queues;
2900 int num_rx_queues;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002901
Shreyas Bhatewarae154b632011-05-10 06:13:56 +00002902 if (!pci_msi_enabled())
2903 enable_mq = 0;
2904
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002905#ifdef VMXNET3_RSS
2906 if (enable_mq)
2907 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
2908 (int)num_online_cpus());
2909 else
2910#endif
2911 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002912 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002913
2914 if (enable_mq)
2915 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
2916 (int)num_online_cpus());
2917 else
2918 num_tx_queues = 1;
2919
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002920 num_tx_queues = rounddown_pow_of_two(num_tx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002921 netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
2922 max(num_tx_queues, num_rx_queues));
2923 printk(KERN_INFO "# of Tx queues : %d, # of Rx queues : %d\n",
2924 num_tx_queues, num_rx_queues);
2925
Joe Perches41de8d42012-01-29 13:47:52 +00002926 if (!netdev)
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002927 return -ENOMEM;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002928
2929 pci_set_drvdata(pdev, netdev);
2930 adapter = netdev_priv(netdev);
2931 adapter->netdev = netdev;
2932 adapter->pdev = pdev;
2933
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002934 spin_lock_init(&adapter->cmd_lock);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002935 adapter->shared = pci_alloc_consistent(adapter->pdev,
2936 sizeof(struct Vmxnet3_DriverShared),
2937 &adapter->shared_pa);
2938 if (!adapter->shared) {
2939 printk(KERN_ERR "Failed to allocate memory for %s\n",
2940 pci_name(pdev));
2941 err = -ENOMEM;
2942 goto err_alloc_shared;
2943 }
2944
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002945 adapter->num_rx_queues = num_rx_queues;
2946 adapter->num_tx_queues = num_tx_queues;
2947
2948 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
2949 size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
2950 adapter->tqd_start = pci_alloc_consistent(adapter->pdev, size,
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002951 &adapter->queue_desc_pa);
2952
2953 if (!adapter->tqd_start) {
2954 printk(KERN_ERR "Failed to allocate memory for %s\n",
2955 pci_name(pdev));
2956 err = -ENOMEM;
2957 goto err_alloc_queue_desc;
2958 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002959 adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
2960 adapter->num_tx_queues);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002961
2962 adapter->pm_conf = kmalloc(sizeof(struct Vmxnet3_PMConf), GFP_KERNEL);
2963 if (adapter->pm_conf == NULL) {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002964 err = -ENOMEM;
2965 goto err_alloc_pm;
2966 }
2967
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002968#ifdef VMXNET3_RSS
2969
2970 adapter->rss_conf = kmalloc(sizeof(struct UPT1_RSSConf), GFP_KERNEL);
2971 if (adapter->rss_conf == NULL) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002972 err = -ENOMEM;
2973 goto err_alloc_rss;
2974 }
2975#endif /* VMXNET3_RSS */
2976
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07002977 err = vmxnet3_alloc_pci_resources(adapter, &dma64);
2978 if (err < 0)
2979 goto err_alloc_pci;
2980
2981 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
2982 if (ver & 1) {
2983 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
2984 } else {
2985 printk(KERN_ERR "Incompatible h/w version (0x%x) for adapter"
2986 " %s\n", ver, pci_name(pdev));
2987 err = -EBUSY;
2988 goto err_ver;
2989 }
2990
2991 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
2992 if (ver & 1) {
2993 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
2994 } else {
2995 printk(KERN_ERR "Incompatible upt version (0x%x) for "
2996 "adapter %s\n", ver, pci_name(pdev));
2997 err = -EBUSY;
2998 goto err_ver;
2999 }
3000
Shreyas Bhatewarae101e7d2011-07-20 16:01:11 +00003001 SET_NETDEV_DEV(netdev, &pdev->dev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003002 vmxnet3_declare_features(adapter, dma64);
3003
3004 adapter->dev_number = atomic_read(&devices_found);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003005
3006 adapter->share_intr = irq_share_mode;
3007 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE &&
3008 adapter->num_tx_queues != adapter->num_rx_queues)
3009 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
3010
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003011 vmxnet3_alloc_intr_resources(adapter);
3012
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003013#ifdef VMXNET3_RSS
3014 if (adapter->num_rx_queues > 1 &&
3015 adapter->intr.type == VMXNET3_IT_MSIX) {
3016 adapter->rss = true;
3017 printk(KERN_INFO "RSS is enabled.\n");
3018 } else {
3019 adapter->rss = false;
3020 }
3021#endif
3022
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003023 vmxnet3_read_mac_addr(adapter, mac);
3024 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3025
3026 netdev->netdev_ops = &vmxnet3_netdev_ops;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003027 vmxnet3_set_ethtool_ops(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003028 netdev->watchdog_timeo = 5 * HZ;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003029
3030 INIT_WORK(&adapter->work, vmxnet3_reset_work);
3031
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003032 if (adapter->intr.type == VMXNET3_IT_MSIX) {
3033 int i;
3034 for (i = 0; i < adapter->num_rx_queues; i++) {
3035 netif_napi_add(adapter->netdev,
3036 &adapter->rx_queue[i].napi,
3037 vmxnet3_poll_rx_only, 64);
3038 }
3039 } else {
3040 netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
3041 vmxnet3_poll, 64);
3042 }
3043
3044 netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
3045 netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
3046
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003047 err = register_netdev(netdev);
3048
3049 if (err) {
3050 printk(KERN_ERR "Failed to register adapter %s\n",
3051 pci_name(pdev));
3052 goto err_register;
3053 }
3054
3055 set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00003056 vmxnet3_check_link(adapter, false);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003057 atomic_inc(&devices_found);
3058 return 0;
3059
3060err_register:
3061 vmxnet3_free_intr_resources(adapter);
3062err_ver:
3063 vmxnet3_free_pci_resources(adapter);
3064err_alloc_pci:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003065#ifdef VMXNET3_RSS
3066 kfree(adapter->rss_conf);
3067err_alloc_rss:
3068#endif
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003069 kfree(adapter->pm_conf);
3070err_alloc_pm:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003071 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3072 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003073err_alloc_queue_desc:
3074 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3075 adapter->shared, adapter->shared_pa);
3076err_alloc_shared:
3077 pci_set_drvdata(pdev, NULL);
3078 free_netdev(netdev);
3079 return err;
3080}
3081
3082
3083static void __devexit
3084vmxnet3_remove_device(struct pci_dev *pdev)
3085{
3086 struct net_device *netdev = pci_get_drvdata(pdev);
3087 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003088 int size = 0;
3089 int num_rx_queues;
3090
3091#ifdef VMXNET3_RSS
3092 if (enable_mq)
3093 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3094 (int)num_online_cpus());
3095 else
3096#endif
3097 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07003098 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003099
Tejun Heo23f333a2010-12-12 16:45:14 +01003100 cancel_work_sync(&adapter->work);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003101
3102 unregister_netdev(netdev);
3103
3104 vmxnet3_free_intr_resources(adapter);
3105 vmxnet3_free_pci_resources(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003106#ifdef VMXNET3_RSS
3107 kfree(adapter->rss_conf);
3108#endif
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003109 kfree(adapter->pm_conf);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003110
3111 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3112 size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
3113 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3114 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003115 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3116 adapter->shared, adapter->shared_pa);
3117 free_netdev(netdev);
3118}
3119
3120
3121#ifdef CONFIG_PM
3122
3123static int
3124vmxnet3_suspend(struct device *device)
3125{
3126 struct pci_dev *pdev = to_pci_dev(device);
3127 struct net_device *netdev = pci_get_drvdata(pdev);
3128 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3129 struct Vmxnet3_PMConf *pmConf;
3130 struct ethhdr *ehdr;
3131 struct arphdr *ahdr;
3132 u8 *arpreq;
3133 struct in_device *in_dev;
3134 struct in_ifaddr *ifa;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003135 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003136 int i = 0;
3137
3138 if (!netif_running(netdev))
3139 return 0;
3140
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003141 for (i = 0; i < adapter->num_rx_queues; i++)
3142 napi_disable(&adapter->rx_queue[i].napi);
3143
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003144 vmxnet3_disable_all_intrs(adapter);
3145 vmxnet3_free_irqs(adapter);
3146 vmxnet3_free_intr_resources(adapter);
3147
3148 netif_device_detach(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003149 netif_tx_stop_all_queues(netdev);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003150
3151 /* Create wake-up filters. */
3152 pmConf = adapter->pm_conf;
3153 memset(pmConf, 0, sizeof(*pmConf));
3154
3155 if (adapter->wol & WAKE_UCAST) {
3156 pmConf->filters[i].patternSize = ETH_ALEN;
3157 pmConf->filters[i].maskSize = 1;
3158 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
3159 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
3160
Harvey Harrison3843e512010-10-21 18:05:32 +00003161 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003162 i++;
3163 }
3164
3165 if (adapter->wol & WAKE_ARP) {
3166 in_dev = in_dev_get(netdev);
3167 if (!in_dev)
3168 goto skip_arp;
3169
3170 ifa = (struct in_ifaddr *)in_dev->ifa_list;
3171 if (!ifa)
3172 goto skip_arp;
3173
3174 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
3175 sizeof(struct arphdr) + /* ARP header */
3176 2 * ETH_ALEN + /* 2 Ethernet addresses*/
3177 2 * sizeof(u32); /*2 IPv4 addresses */
3178 pmConf->filters[i].maskSize =
3179 (pmConf->filters[i].patternSize - 1) / 8 + 1;
3180
3181 /* ETH_P_ARP in Ethernet header. */
3182 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
3183 ehdr->h_proto = htons(ETH_P_ARP);
3184
3185 /* ARPOP_REQUEST in ARP header. */
3186 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
3187 ahdr->ar_op = htons(ARPOP_REQUEST);
3188 arpreq = (u8 *)(ahdr + 1);
3189
3190 /* The Unicast IPv4 address in 'tip' field. */
3191 arpreq += 2 * ETH_ALEN + sizeof(u32);
3192 *(u32 *)arpreq = ifa->ifa_address;
3193
3194 /* The mask for the relevant bits. */
3195 pmConf->filters[i].mask[0] = 0x00;
3196 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
3197 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
3198 pmConf->filters[i].mask[3] = 0x00;
3199 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
3200 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
3201 in_dev_put(in_dev);
3202
Harvey Harrison3843e512010-10-21 18:05:32 +00003203 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003204 i++;
3205 }
3206
3207skip_arp:
3208 if (adapter->wol & WAKE_MAGIC)
Harvey Harrison3843e512010-10-21 18:05:32 +00003209 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003210
3211 pmConf->numFilters = i;
3212
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003213 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3214 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3215 *pmConf));
3216 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3217 pmConf));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003218
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003219 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003220 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3221 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003222 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003223
3224 pci_save_state(pdev);
3225 pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
3226 adapter->wol);
3227 pci_disable_device(pdev);
3228 pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
3229
3230 return 0;
3231}
3232
3233
3234static int
3235vmxnet3_resume(struct device *device)
3236{
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003237 int err, i = 0;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003238 unsigned long flags;
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003239 struct pci_dev *pdev = to_pci_dev(device);
3240 struct net_device *netdev = pci_get_drvdata(pdev);
3241 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3242 struct Vmxnet3_PMConf *pmConf;
3243
3244 if (!netif_running(netdev))
3245 return 0;
3246
3247 /* Destroy wake-up filters. */
3248 pmConf = adapter->pm_conf;
3249 memset(pmConf, 0, sizeof(*pmConf));
3250
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003251 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3252 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3253 *pmConf));
Harvey Harrison0561cf32010-10-21 18:05:34 +00003254 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003255 pmConf));
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003256
3257 netif_device_attach(netdev);
3258 pci_set_power_state(pdev, PCI_D0);
3259 pci_restore_state(pdev);
3260 err = pci_enable_device_mem(pdev);
3261 if (err != 0)
3262 return err;
3263
3264 pci_enable_wake(pdev, PCI_D0, 0);
3265
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003266 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003267 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3268 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003269 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003270 vmxnet3_alloc_intr_resources(adapter);
3271 vmxnet3_request_irqs(adapter);
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003272 for (i = 0; i < adapter->num_rx_queues; i++)
3273 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003274 vmxnet3_enable_all_intrs(adapter);
3275
3276 return 0;
3277}
3278
Alexey Dobriyan47145212009-12-14 18:00:08 -08003279static const struct dev_pm_ops vmxnet3_pm_ops = {
Shreyas Bhatewarad1a890f2009-10-13 00:15:51 -07003280 .suspend = vmxnet3_suspend,
3281 .resume = vmxnet3_resume,
3282};
3283#endif
3284
3285static struct pci_driver vmxnet3_driver = {
3286 .name = vmxnet3_driver_name,
3287 .id_table = vmxnet3_pciid_table,
3288 .probe = vmxnet3_probe_device,
3289 .remove = __devexit_p(vmxnet3_remove_device),
3290#ifdef CONFIG_PM
3291 .driver.pm = &vmxnet3_pm_ops,
3292#endif
3293};
3294
3295
3296static int __init
3297vmxnet3_init_module(void)
3298{
3299 printk(KERN_INFO "%s - version %s\n", VMXNET3_DRIVER_DESC,
3300 VMXNET3_DRIVER_VERSION_REPORT);
3301 return pci_register_driver(&vmxnet3_driver);
3302}
3303
3304module_init(vmxnet3_init_module);
3305
3306
3307static void
3308vmxnet3_exit_module(void)
3309{
3310 pci_unregister_driver(&vmxnet3_driver);
3311}
3312
3313module_exit(vmxnet3_exit_module);
3314
3315MODULE_AUTHOR("VMware, Inc.");
3316MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
3317MODULE_LICENSE("GPL v2");
3318MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);