blob: 1c2ae11a9e3562052e02c2ff278de057f4c69113 [file] [log] [blame]
Shreyas Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-10-13 00:15:51 -0700147
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +0000148 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-10-13 00:15:51 -0700175 }
176}
177
Shreyas Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-10-13 00:15:51 -0700409
410 tbi = tq->buf_info + tq->tx_ring.next2comp;
Shreyas Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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);
540 if (!tq->buf_info) {
541 printk(KERN_ERR "%s: failed to allocate tx bufinfo\n",
542 adapter->netdev->name);
543 goto err;
544 }
545
546 return 0;
547
548err:
549 vmxnet3_tq_destroy(tq, adapter);
550 return -ENOMEM;
551}
552
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000553static void
554vmxnet3_tq_cleanup_all(struct vmxnet3_adapter *adapter)
555{
556 int i;
557
558 for (i = 0; i < adapter->num_tx_queues; i++)
559 vmxnet3_tq_cleanup(&adapter->tx_queue[i], adapter);
560}
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700561
562/*
563 * starting from ring->next2fill, allocate rx buffers for the given ring
564 * of the rx queue and update the rx desc. stop after @num_to_alloc buffers
565 * are allocated or allocation fails
566 */
567
568static int
569vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
570 int num_to_alloc, struct vmxnet3_adapter *adapter)
571{
572 int num_allocated = 0;
573 struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
574 struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
575 u32 val;
576
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000577 while (num_allocated <= num_to_alloc) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700578 struct vmxnet3_rx_buf_info *rbi;
579 union Vmxnet3_GenericDesc *gd;
580
581 rbi = rbi_base + ring->next2fill;
582 gd = ring->base + ring->next2fill;
583
584 if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
585 if (rbi->skb == NULL) {
586 rbi->skb = dev_alloc_skb(rbi->len +
587 NET_IP_ALIGN);
588 if (unlikely(rbi->skb == NULL)) {
589 rq->stats.rx_buf_alloc_failure++;
590 break;
591 }
592 rbi->skb->dev = adapter->netdev;
593
594 skb_reserve(rbi->skb, NET_IP_ALIGN);
595 rbi->dma_addr = pci_map_single(adapter->pdev,
596 rbi->skb->data, rbi->len,
597 PCI_DMA_FROMDEVICE);
598 } else {
599 /* rx buffer skipped by the device */
600 }
601 val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
602 } else {
603 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
604 rbi->len != PAGE_SIZE);
605
606 if (rbi->page == NULL) {
607 rbi->page = alloc_page(GFP_ATOMIC);
608 if (unlikely(rbi->page == NULL)) {
609 rq->stats.rx_buf_alloc_failure++;
610 break;
611 }
612 rbi->dma_addr = pci_map_page(adapter->pdev,
613 rbi->page, 0, PAGE_SIZE,
614 PCI_DMA_FROMDEVICE);
615 } else {
616 /* rx buffers skipped by the device */
617 }
618 val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
619 }
620
621 BUG_ON(rbi->dma_addr == 0);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000622 gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000623 gd->dword[2] = cpu_to_le32((!ring->gen << VMXNET3_RXD_GEN_SHIFT)
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000624 | val | rbi->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700625
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000626 /* Fill the last buffer but dont mark it ready, or else the
627 * device will think that the queue is full */
628 if (num_allocated == num_to_alloc)
629 break;
630
631 gd->dword[2] |= cpu_to_le32(ring->gen << VMXNET3_RXD_GEN_SHIFT);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700632 num_allocated++;
633 vmxnet3_cmd_ring_adv_next2fill(ring);
634 }
635 rq->uncommitted[ring_idx] += num_allocated;
636
Randy Dunlapf69655822009-10-16 17:54:34 -0700637 dev_dbg(&adapter->netdev->dev,
638 "alloc_rx_buf: %d allocated, next2fill %u, next2comp "
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700639 "%u, uncommited %u\n", num_allocated, ring->next2fill,
640 ring->next2comp, rq->uncommitted[ring_idx]);
641
642 /* so that the device can distinguish a full ring and an empty ring */
643 BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
644
645 return num_allocated;
646}
647
648
649static void
650vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
651 struct vmxnet3_rx_buf_info *rbi)
652{
653 struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
654 skb_shinfo(skb)->nr_frags;
655
656 BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
657
Ian Campbell0e0634d2011-09-21 21:53:28 +0000658 __skb_frag_set_page(frag, rbi->page);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700659 frag->page_offset = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000660 skb_frag_size_set(frag, rcd->len);
661 skb->data_len += rcd->len;
Eric Dumazet5e6c3552011-10-13 11:38:17 +0000662 skb->truesize += PAGE_SIZE;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700663 skb_shinfo(skb)->nr_frags++;
664}
665
666
667static void
668vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
669 struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
670 struct vmxnet3_adapter *adapter)
671{
672 u32 dw2, len;
673 unsigned long buf_offset;
674 int i;
675 union Vmxnet3_GenericDesc *gdesc;
676 struct vmxnet3_tx_buf_info *tbi = NULL;
677
678 BUG_ON(ctx->copy_size > skb_headlen(skb));
679
680 /* use the previous gen bit for the SOP desc */
681 dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
682
683 ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
684 gdesc = ctx->sop_txd; /* both loops below can be skipped */
685
686 /* no need to map the buffer if headers are copied */
687 if (ctx->copy_size) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000688 ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700689 tq->tx_ring.next2fill *
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000690 sizeof(struct Vmxnet3_TxDataDesc));
691 ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700692 ctx->sop_txd->dword[3] = 0;
693
694 tbi = tq->buf_info + tq->tx_ring.next2fill;
695 tbi->map_type = VMXNET3_MAP_NONE;
696
Randy Dunlapf69655822009-10-16 17:54:34 -0700697 dev_dbg(&adapter->netdev->dev,
698 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000699 tq->tx_ring.next2fill,
700 le64_to_cpu(ctx->sop_txd->txd.addr),
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700701 ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
702 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
703
704 /* use the right gen for non-SOP desc */
705 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
706 }
707
708 /* linear part can use multiple tx desc if it's big */
709 len = skb_headlen(skb) - ctx->copy_size;
710 buf_offset = ctx->copy_size;
711 while (len) {
712 u32 buf_size;
713
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000714 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
715 buf_size = len;
716 dw2 |= len;
717 } else {
718 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
719 /* spec says that for TxDesc.len, 0 == 2^14 */
720 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700721
722 tbi = tq->buf_info + tq->tx_ring.next2fill;
723 tbi->map_type = VMXNET3_MAP_SINGLE;
724 tbi->dma_addr = pci_map_single(adapter->pdev,
725 skb->data + buf_offset, buf_size,
726 PCI_DMA_TODEVICE);
727
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000728 tbi->len = buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700729
730 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
731 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
732
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000733 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000734 gdesc->dword[2] = cpu_to_le32(dw2);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700735 gdesc->dword[3] = 0;
736
Randy Dunlapf69655822009-10-16 17:54:34 -0700737 dev_dbg(&adapter->netdev->dev,
738 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000739 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
740 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700741 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
742 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
743
744 len -= buf_size;
745 buf_offset += buf_size;
746 }
747
748 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000749 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700750
751 tbi = tq->buf_info + tq->tx_ring.next2fill;
752 tbi->map_type = VMXNET3_MAP_PAGE;
Ian Campbell0e0634d2011-09-21 21:53:28 +0000753 tbi->dma_addr = skb_frag_dma_map(&adapter->pdev->dev, frag,
Eric Dumazet9e903e02011-10-18 21:00:24 +0000754 0, skb_frag_size(frag),
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100755 DMA_TO_DEVICE);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700756
Eric Dumazet9e903e02011-10-18 21:00:24 +0000757 tbi->len = skb_frag_size(frag);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700758
759 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
760 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
761
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000762 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000763 gdesc->dword[2] = cpu_to_le32(dw2 | skb_frag_size(frag));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700764 gdesc->dword[3] = 0;
765
Randy Dunlapf69655822009-10-16 17:54:34 -0700766 dev_dbg(&adapter->netdev->dev,
767 "txd[%u]: 0x%llu %u %u\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000768 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
769 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700770 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
771 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
772 }
773
774 ctx->eop_txd = gdesc;
775
776 /* set the last buf_info for the pkt */
777 tbi->skb = skb;
778 tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
779}
780
781
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000782/* Init all tx queues */
783static void
784vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
785{
786 int i;
787
788 for (i = 0; i < adapter->num_tx_queues; i++)
789 vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
790}
791
792
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700793/*
794 * parse and copy relevant protocol headers:
795 * For a tso pkt, relevant headers are L2/3/4 including options
796 * For a pkt requesting csum offloading, they are L2/3 and may include L4
797 * if it's a TCP/UDP pkt
798 *
799 * Returns:
800 * -1: error happens during parsing
801 * 0: protocol headers parsed, but too big to be copied
802 * 1: protocol headers parsed and copied
803 *
804 * Other effects:
805 * 1. related *ctx fields are updated.
806 * 2. ctx->copy_size is # of bytes copied
807 * 3. the portion copied is guaranteed to be in the linear part
808 *
809 */
810static int
811vmxnet3_parse_and_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
812 struct vmxnet3_tx_ctx *ctx,
813 struct vmxnet3_adapter *adapter)
814{
815 struct Vmxnet3_TxDataDesc *tdd;
816
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000817 if (ctx->mss) { /* TSO */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700818 ctx->eth_ip_hdr_size = skb_transport_offset(skb);
819 ctx->l4_hdr_size = ((struct tcphdr *)
820 skb_transport_header(skb))->doff * 4;
821 ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
822 } else {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700823 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000824 ctx->eth_ip_hdr_size = skb_checksum_start_offset(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700825
826 if (ctx->ipv4) {
827 struct iphdr *iph = (struct iphdr *)
828 skb_network_header(skb);
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000829 if (iph->protocol == IPPROTO_TCP)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700830 ctx->l4_hdr_size = ((struct tcphdr *)
831 skb_transport_header(skb))->doff * 4;
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000832 else if (iph->protocol == IPPROTO_UDP)
833 /*
834 * Use tcp header size so that bytes to
835 * be copied are more than required by
836 * the device.
837 */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700838 ctx->l4_hdr_size =
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000839 sizeof(struct tcphdr);
840 else
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700841 ctx->l4_hdr_size = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700842 } else {
843 /* for simplicity, don't copy L4 headers */
844 ctx->l4_hdr_size = 0;
845 }
846 ctx->copy_size = ctx->eth_ip_hdr_size +
847 ctx->l4_hdr_size;
848 } else {
849 ctx->eth_ip_hdr_size = 0;
850 ctx->l4_hdr_size = 0;
851 /* copy as much as allowed */
852 ctx->copy_size = min((unsigned int)VMXNET3_HDR_COPY_SIZE
853 , skb_headlen(skb));
854 }
855
856 /* make sure headers are accessible directly */
857 if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
858 goto err;
859 }
860
861 if (unlikely(ctx->copy_size > VMXNET3_HDR_COPY_SIZE)) {
862 tq->stats.oversized_hdr++;
863 ctx->copy_size = 0;
864 return 0;
865 }
866
867 tdd = tq->data_ring.base + tq->tx_ring.next2fill;
868
869 memcpy(tdd->data, skb->data, ctx->copy_size);
Randy Dunlapf69655822009-10-16 17:54:34 -0700870 dev_dbg(&adapter->netdev->dev,
871 "copy %u bytes to dataRing[%u]\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700872 ctx->copy_size, tq->tx_ring.next2fill);
873 return 1;
874
875err:
876 return -1;
877}
878
879
880static void
881vmxnet3_prepare_tso(struct sk_buff *skb,
882 struct vmxnet3_tx_ctx *ctx)
883{
884 struct tcphdr *tcph = (struct tcphdr *)skb_transport_header(skb);
885 if (ctx->ipv4) {
886 struct iphdr *iph = (struct iphdr *)skb_network_header(skb);
887 iph->check = 0;
888 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
889 IPPROTO_TCP, 0);
890 } else {
891 struct ipv6hdr *iph = (struct ipv6hdr *)skb_network_header(skb);
892 tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
893 IPPROTO_TCP, 0);
894 }
895}
896
897
898/*
899 * Transmits a pkt thru a given tq
900 * Returns:
901 * NETDEV_TX_OK: descriptors are setup successfully
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300902 * NETDEV_TX_OK: error occurred, the pkt is dropped
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700903 * NETDEV_TX_BUSY: tx ring is full, queue is stopped
904 *
905 * Side-effects:
906 * 1. tx ring may be changed
907 * 2. tq stats may be updated accordingly
908 * 3. shared->txNumDeferred may be updated
909 */
910
911static int
912vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
913 struct vmxnet3_adapter *adapter, struct net_device *netdev)
914{
915 int ret;
916 u32 count;
917 unsigned long flags;
918 struct vmxnet3_tx_ctx ctx;
919 union Vmxnet3_GenericDesc *gdesc;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000920#ifdef __BIG_ENDIAN_BITFIELD
921 /* Use temporary descriptor to avoid touching bits multiple times */
922 union Vmxnet3_GenericDesc tempTxDesc;
923#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700924
925 /* conservatively estimate # of descriptors to use */
926 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) +
927 skb_shinfo(skb)->nr_frags + 1;
928
Jesse Gross72e85c42011-06-23 13:04:39 +0000929 ctx.ipv4 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IP));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700930
931 ctx.mss = skb_shinfo(skb)->gso_size;
932 if (ctx.mss) {
933 if (skb_header_cloned(skb)) {
934 if (unlikely(pskb_expand_head(skb, 0, 0,
935 GFP_ATOMIC) != 0)) {
936 tq->stats.drop_tso++;
937 goto drop_pkt;
938 }
939 tq->stats.copy_skb_header++;
940 }
941 vmxnet3_prepare_tso(skb, &ctx);
942 } else {
943 if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
944
945 /* non-tso pkts must not use more than
946 * VMXNET3_MAX_TXD_PER_PKT entries
947 */
948 if (skb_linearize(skb) != 0) {
949 tq->stats.drop_too_many_frags++;
950 goto drop_pkt;
951 }
952 tq->stats.linearized++;
953
954 /* recalculate the # of descriptors to use */
955 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
956 }
957 }
958
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000959 spin_lock_irqsave(&tq->tx_lock, flags);
960
961 if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
962 tq->stats.tx_ring_full++;
963 dev_dbg(&adapter->netdev->dev,
964 "tx queue stopped on %s, next2comp %u"
965 " next2fill %u\n", adapter->netdev->name,
966 tq->tx_ring.next2comp, tq->tx_ring.next2fill);
967
968 vmxnet3_tq_stop(tq, adapter);
969 spin_unlock_irqrestore(&tq->tx_lock, flags);
970 return NETDEV_TX_BUSY;
971 }
972
973
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700974 ret = vmxnet3_parse_and_copy_hdr(skb, tq, &ctx, adapter);
975 if (ret >= 0) {
976 BUG_ON(ret <= 0 && ctx.copy_size != 0);
977 /* hdrs parsed, check against other limits */
978 if (ctx.mss) {
979 if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
980 VMXNET3_MAX_TX_BUF_SIZE)) {
981 goto hdr_too_big;
982 }
983 } else {
984 if (skb->ip_summed == CHECKSUM_PARTIAL) {
985 if (unlikely(ctx.eth_ip_hdr_size +
986 skb->csum_offset >
987 VMXNET3_MAX_CSUM_OFFSET)) {
988 goto hdr_too_big;
989 }
990 }
991 }
992 } else {
993 tq->stats.drop_hdr_inspect_err++;
Dan Carpenterf955e142010-12-20 03:03:15 +0000994 goto unlock_drop_pkt;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700995 }
996
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700997 /* fill tx descs related to addr & len */
998 vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter);
999
1000 /* setup the EOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001001 ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001002
1003 /* setup the SOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001004#ifdef __BIG_ENDIAN_BITFIELD
1005 gdesc = &tempTxDesc;
1006 gdesc->dword[2] = ctx.sop_txd->dword[2];
1007 gdesc->dword[3] = ctx.sop_txd->dword[3];
1008#else
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001009 gdesc = ctx.sop_txd;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001010#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001011 if (ctx.mss) {
1012 gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
1013 gdesc->txd.om = VMXNET3_OM_TSO;
1014 gdesc->txd.msscof = ctx.mss;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001015 le32_add_cpu(&tq->shared->txNumDeferred, (skb->len -
1016 gdesc->txd.hlen + ctx.mss - 1) / ctx.mss);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001017 } else {
1018 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1019 gdesc->txd.hlen = ctx.eth_ip_hdr_size;
1020 gdesc->txd.om = VMXNET3_OM_CSUM;
1021 gdesc->txd.msscof = ctx.eth_ip_hdr_size +
1022 skb->csum_offset;
1023 } else {
1024 gdesc->txd.om = 0;
1025 gdesc->txd.msscof = 0;
1026 }
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001027 le32_add_cpu(&tq->shared->txNumDeferred, 1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001028 }
1029
1030 if (vlan_tx_tag_present(skb)) {
1031 gdesc->txd.ti = 1;
1032 gdesc->txd.tci = vlan_tx_tag_get(skb);
1033 }
1034
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001035 /* finally flips the GEN bit of the SOP desc. */
1036 gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
1037 VMXNET3_TXD_GEN);
1038#ifdef __BIG_ENDIAN_BITFIELD
1039 /* Finished updating in bitfields of Tx Desc, so write them in original
1040 * place.
1041 */
1042 vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
1043 (struct Vmxnet3_TxDesc *)ctx.sop_txd);
1044 gdesc = ctx.sop_txd;
1045#endif
Randy Dunlapf69655822009-10-16 17:54:34 -07001046 dev_dbg(&adapter->netdev->dev,
1047 "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001048 (u32)((union Vmxnet3_GenericDesc *)ctx.sop_txd -
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001049 tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
1050 le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001051
1052 spin_unlock_irqrestore(&tq->tx_lock, flags);
1053
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001054 if (le32_to_cpu(tq->shared->txNumDeferred) >=
1055 le32_to_cpu(tq->shared->txThreshold)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001056 tq->shared->txNumDeferred = 0;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001057 VMXNET3_WRITE_BAR0_REG(adapter,
1058 VMXNET3_REG_TXPROD + tq->qid * 8,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001059 tq->tx_ring.next2fill);
1060 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001061
1062 return NETDEV_TX_OK;
1063
1064hdr_too_big:
1065 tq->stats.drop_oversized_hdr++;
Dan Carpenterf955e142010-12-20 03:03:15 +00001066unlock_drop_pkt:
1067 spin_unlock_irqrestore(&tq->tx_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001068drop_pkt:
1069 tq->stats.drop_total++;
1070 dev_kfree_skb(skb);
1071 return NETDEV_TX_OK;
1072}
1073
1074
1075static netdev_tx_t
1076vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1077{
1078 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001079
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001080 BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
1081 return vmxnet3_tq_xmit(skb,
1082 &adapter->tx_queue[skb->queue_mapping],
1083 adapter, netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001084}
1085
1086
1087static void
1088vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
1089 struct sk_buff *skb,
1090 union Vmxnet3_GenericDesc *gdesc)
1091{
Michał Mirosława0d27302011-04-18 13:31:21 +00001092 if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001093 /* typical case: TCP/UDP over IP and both csums are correct */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001094 if ((le32_to_cpu(gdesc->dword[3]) & VMXNET3_RCD_CSUM_OK) ==
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001095 VMXNET3_RCD_CSUM_OK) {
1096 skb->ip_summed = CHECKSUM_UNNECESSARY;
1097 BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
1098 BUG_ON(!(gdesc->rcd.v4 || gdesc->rcd.v6));
1099 BUG_ON(gdesc->rcd.frg);
1100 } else {
1101 if (gdesc->rcd.csum) {
1102 skb->csum = htons(gdesc->rcd.csum);
1103 skb->ip_summed = CHECKSUM_PARTIAL;
1104 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001105 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001106 }
1107 }
1108 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001109 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001110 }
1111}
1112
1113
1114static void
1115vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
1116 struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
1117{
1118 rq->stats.drop_err++;
1119 if (!rcd->fcs)
1120 rq->stats.drop_fcs++;
1121
1122 rq->stats.drop_total++;
1123
1124 /*
1125 * We do not unmap and chain the rx buffer to the skb.
1126 * We basically pretend this buffer is not used and will be recycled
1127 * by vmxnet3_rq_alloc_rx_buf()
1128 */
1129
1130 /*
1131 * ctx->skb may be NULL if this is the first and the only one
1132 * desc for the pkt
1133 */
1134 if (ctx->skb)
1135 dev_kfree_skb_irq(ctx->skb);
1136
1137 ctx->skb = NULL;
1138}
1139
1140
1141static int
1142vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
1143 struct vmxnet3_adapter *adapter, int quota)
1144{
Joe Perches215faf92010-12-21 02:16:10 -08001145 static const u32 rxprod_reg[2] = {
1146 VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2
1147 };
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001148 u32 num_rxd = 0;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001149 bool skip_page_frags = false;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001150 struct Vmxnet3_RxCompDesc *rcd;
1151 struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001152#ifdef __BIG_ENDIAN_BITFIELD
1153 struct Vmxnet3_RxDesc rxCmdDesc;
1154 struct Vmxnet3_RxCompDesc rxComp;
1155#endif
1156 vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
1157 &rxComp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001158 while (rcd->gen == rq->comp_ring.gen) {
1159 struct vmxnet3_rx_buf_info *rbi;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001160 struct sk_buff *skb, *new_skb = NULL;
1161 struct page *new_page = NULL;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001162 int num_to_alloc;
1163 struct Vmxnet3_RxDesc *rxd;
1164 u32 idx, ring_idx;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001165 struct vmxnet3_cmd_ring *ring = NULL;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001166 if (num_rxd >= quota) {
1167 /* we may stop even before we see the EOP desc of
1168 * the current pkt
1169 */
1170 break;
1171 }
1172 num_rxd++;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001173 BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001174 idx = rcd->rxdIdx;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001175 ring_idx = rcd->rqID < adapter->num_rx_queues ? 0 : 1;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001176 ring = rq->rx_ring + ring_idx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001177 vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
1178 &rxCmdDesc);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001179 rbi = rq->buf_info[ring_idx] + idx;
1180
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001181 BUG_ON(rxd->addr != rbi->dma_addr ||
1182 rxd->len != rbi->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001183
1184 if (unlikely(rcd->eop && rcd->err)) {
1185 vmxnet3_rx_error(rq, rcd, ctx, adapter);
1186 goto rcd_done;
1187 }
1188
1189 if (rcd->sop) { /* first buf of the pkt */
1190 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
1191 rcd->rqID != rq->qid);
1192
1193 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
1194 BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
1195
1196 if (unlikely(rcd->len == 0)) {
1197 /* Pretend the rx buffer is skipped. */
1198 BUG_ON(!(rcd->sop && rcd->eop));
Randy Dunlapf69655822009-10-16 17:54:34 -07001199 dev_dbg(&adapter->netdev->dev,
1200 "rxRing[%u][%u] 0 length\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001201 ring_idx, idx);
1202 goto rcd_done;
1203 }
1204
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001205 skip_page_frags = false;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001206 ctx->skb = rbi->skb;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001207 new_skb = dev_alloc_skb(rbi->len + NET_IP_ALIGN);
1208 if (new_skb == NULL) {
1209 /* Skb allocation failed, do not handover this
1210 * skb to stack. Reuse it. Drop the existing pkt
1211 */
1212 rq->stats.rx_buf_alloc_failure++;
1213 ctx->skb = NULL;
1214 rq->stats.drop_total++;
1215 skip_page_frags = true;
1216 goto rcd_done;
1217 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001218
1219 pci_unmap_single(adapter->pdev, rbi->dma_addr, rbi->len,
1220 PCI_DMA_FROMDEVICE);
1221
1222 skb_put(ctx->skb, rcd->len);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001223
1224 /* Immediate refill */
1225 new_skb->dev = adapter->netdev;
1226 skb_reserve(new_skb, NET_IP_ALIGN);
1227 rbi->skb = new_skb;
1228 rbi->dma_addr = pci_map_single(adapter->pdev,
1229 rbi->skb->data, rbi->len,
1230 PCI_DMA_FROMDEVICE);
1231 rxd->addr = cpu_to_le64(rbi->dma_addr);
1232 rxd->len = rbi->len;
1233
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001234 } else {
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001235 BUG_ON(ctx->skb == NULL && !skip_page_frags);
1236
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001237 /* non SOP buffer must be type 1 in most cases */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001238 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE);
1239 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001240
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001241 /* If an sop buffer was dropped, skip all
1242 * following non-sop fragments. They will be reused.
1243 */
1244 if (skip_page_frags)
1245 goto rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001246
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001247 new_page = alloc_page(GFP_ATOMIC);
1248 if (unlikely(new_page == NULL)) {
1249 /* Replacement page frag could not be allocated.
1250 * Reuse this page. Drop the pkt and free the
1251 * skb which contained this page as a frag. Skip
1252 * processing all the following non-sop frags.
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001253 */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001254 rq->stats.rx_buf_alloc_failure++;
1255 dev_kfree_skb(ctx->skb);
1256 ctx->skb = NULL;
1257 skip_page_frags = true;
1258 goto rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001259 }
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001260
1261 if (rcd->len) {
1262 pci_unmap_page(adapter->pdev,
1263 rbi->dma_addr, rbi->len,
1264 PCI_DMA_FROMDEVICE);
1265
1266 vmxnet3_append_frag(ctx->skb, rcd, rbi);
1267 }
1268
1269 /* Immediate refill */
1270 rbi->page = new_page;
1271 rbi->dma_addr = pci_map_page(adapter->pdev, rbi->page,
1272 0, PAGE_SIZE,
1273 PCI_DMA_FROMDEVICE);
1274 rxd->addr = cpu_to_le64(rbi->dma_addr);
1275 rxd->len = rbi->len;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001276 }
1277
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001278
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001279 skb = ctx->skb;
1280 if (rcd->eop) {
1281 skb->len += skb->data_len;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001282
1283 vmxnet3_rx_csum(adapter, skb,
1284 (union Vmxnet3_GenericDesc *)rcd);
1285 skb->protocol = eth_type_trans(skb, adapter->netdev);
1286
Jesse Gross72e85c42011-06-23 13:04:39 +00001287 if (unlikely(rcd->ts))
1288 __vlan_hwaccel_put_tag(skb, rcd->tci);
1289
Jesse Gross213ade82011-06-24 14:24:35 +00001290 if (adapter->netdev->features & NETIF_F_LRO)
1291 netif_receive_skb(skb);
1292 else
1293 napi_gro_receive(&rq->napi, skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001294
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001295 ctx->skb = NULL;
1296 }
1297
1298rcd_done:
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001299 /* device may have skipped some rx descs */
1300 ring->next2comp = idx;
1301 num_to_alloc = vmxnet3_cmd_ring_desc_avail(ring);
1302 ring = rq->rx_ring + ring_idx;
1303 while (num_to_alloc) {
1304 vmxnet3_getRxDesc(rxd, &ring->base[ring->next2fill].rxd,
1305 &rxCmdDesc);
1306 BUG_ON(!rxd->addr);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001307
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001308 /* Recv desc is ready to be used by the device */
1309 rxd->gen = ring->gen;
1310 vmxnet3_cmd_ring_adv_next2fill(ring);
1311 num_to_alloc--;
1312 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001313
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001314 /* if needed, update the register */
1315 if (unlikely(rq->shared->updateRxProd)) {
1316 VMXNET3_WRITE_BAR0_REG(adapter,
1317 rxprod_reg[ring_idx] + rq->qid * 8,
1318 ring->next2fill);
1319 rq->uncommitted[ring_idx] = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001320 }
1321
1322 vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001323 vmxnet3_getRxComp(rcd,
1324 &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001325 }
1326
1327 return num_rxd;
1328}
1329
1330
1331static void
1332vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
1333 struct vmxnet3_adapter *adapter)
1334{
1335 u32 i, ring_idx;
1336 struct Vmxnet3_RxDesc *rxd;
1337
1338 for (ring_idx = 0; ring_idx < 2; ring_idx++) {
1339 for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001340#ifdef __BIG_ENDIAN_BITFIELD
1341 struct Vmxnet3_RxDesc rxDesc;
1342#endif
1343 vmxnet3_getRxDesc(rxd,
1344 &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001345
1346 if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
1347 rq->buf_info[ring_idx][i].skb) {
1348 pci_unmap_single(adapter->pdev, rxd->addr,
1349 rxd->len, PCI_DMA_FROMDEVICE);
1350 dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
1351 rq->buf_info[ring_idx][i].skb = NULL;
1352 } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
1353 rq->buf_info[ring_idx][i].page) {
1354 pci_unmap_page(adapter->pdev, rxd->addr,
1355 rxd->len, PCI_DMA_FROMDEVICE);
1356 put_page(rq->buf_info[ring_idx][i].page);
1357 rq->buf_info[ring_idx][i].page = NULL;
1358 }
1359 }
1360
1361 rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
1362 rq->rx_ring[ring_idx].next2fill =
1363 rq->rx_ring[ring_idx].next2comp = 0;
1364 rq->uncommitted[ring_idx] = 0;
1365 }
1366
1367 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1368 rq->comp_ring.next2proc = 0;
1369}
1370
1371
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001372static void
1373vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
1374{
1375 int i;
1376
1377 for (i = 0; i < adapter->num_rx_queues; i++)
1378 vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
1379}
1380
1381
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001382void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
1383 struct vmxnet3_adapter *adapter)
1384{
1385 int i;
1386 int j;
1387
1388 /* all rx buffers must have already been freed */
1389 for (i = 0; i < 2; i++) {
1390 if (rq->buf_info[i]) {
1391 for (j = 0; j < rq->rx_ring[i].size; j++)
1392 BUG_ON(rq->buf_info[i][j].page != NULL);
1393 }
1394 }
1395
1396
1397 kfree(rq->buf_info[0]);
1398
1399 for (i = 0; i < 2; i++) {
1400 if (rq->rx_ring[i].base) {
1401 pci_free_consistent(adapter->pdev, rq->rx_ring[i].size
1402 * sizeof(struct Vmxnet3_RxDesc),
1403 rq->rx_ring[i].base,
1404 rq->rx_ring[i].basePA);
1405 rq->rx_ring[i].base = NULL;
1406 }
1407 rq->buf_info[i] = NULL;
1408 }
1409
1410 if (rq->comp_ring.base) {
1411 pci_free_consistent(adapter->pdev, rq->comp_ring.size *
1412 sizeof(struct Vmxnet3_RxCompDesc),
1413 rq->comp_ring.base, rq->comp_ring.basePA);
1414 rq->comp_ring.base = NULL;
1415 }
1416}
1417
1418
1419static int
1420vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
1421 struct vmxnet3_adapter *adapter)
1422{
1423 int i;
1424
1425 /* initialize buf_info */
1426 for (i = 0; i < rq->rx_ring[0].size; i++) {
1427
1428 /* 1st buf for a pkt is skbuff */
1429 if (i % adapter->rx_buf_per_pkt == 0) {
1430 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
1431 rq->buf_info[0][i].len = adapter->skb_buf_size;
1432 } else { /* subsequent bufs for a pkt is frag */
1433 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
1434 rq->buf_info[0][i].len = PAGE_SIZE;
1435 }
1436 }
1437 for (i = 0; i < rq->rx_ring[1].size; i++) {
1438 rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
1439 rq->buf_info[1][i].len = PAGE_SIZE;
1440 }
1441
1442 /* reset internal state and allocate buffers for both rings */
1443 for (i = 0; i < 2; i++) {
1444 rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
1445 rq->uncommitted[i] = 0;
1446
1447 memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
1448 sizeof(struct Vmxnet3_RxDesc));
1449 rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
1450 }
1451 if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
1452 adapter) == 0) {
1453 /* at least has 1 rx buffer for the 1st ring */
1454 return -ENOMEM;
1455 }
1456 vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
1457
1458 /* reset the comp ring */
1459 rq->comp_ring.next2proc = 0;
1460 memset(rq->comp_ring.base, 0, rq->comp_ring.size *
1461 sizeof(struct Vmxnet3_RxCompDesc));
1462 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1463
1464 /* reset rxctx */
1465 rq->rx_ctx.skb = NULL;
1466
1467 /* stats are not reset */
1468 return 0;
1469}
1470
1471
1472static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001473vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
1474{
1475 int i, err = 0;
1476
1477 for (i = 0; i < adapter->num_rx_queues; i++) {
1478 err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
1479 if (unlikely(err)) {
1480 dev_err(&adapter->netdev->dev, "%s: failed to "
1481 "initialize rx queue%i\n",
1482 adapter->netdev->name, i);
1483 break;
1484 }
1485 }
1486 return err;
1487
1488}
1489
1490
1491static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001492vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
1493{
1494 int i;
1495 size_t sz;
1496 struct vmxnet3_rx_buf_info *bi;
1497
1498 for (i = 0; i < 2; i++) {
1499
1500 sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
1501 rq->rx_ring[i].base = pci_alloc_consistent(adapter->pdev, sz,
1502 &rq->rx_ring[i].basePA);
1503 if (!rq->rx_ring[i].base) {
1504 printk(KERN_ERR "%s: failed to allocate rx ring %d\n",
1505 adapter->netdev->name, i);
1506 goto err;
1507 }
1508 }
1509
1510 sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
1511 rq->comp_ring.base = pci_alloc_consistent(adapter->pdev, sz,
1512 &rq->comp_ring.basePA);
1513 if (!rq->comp_ring.base) {
1514 printk(KERN_ERR "%s: failed to allocate rx comp ring\n",
1515 adapter->netdev->name);
1516 goto err;
1517 }
1518
1519 sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
1520 rq->rx_ring[1].size);
Julia Lawall476c6092010-05-13 10:05:40 +00001521 bi = kzalloc(sz, GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001522 if (!bi) {
1523 printk(KERN_ERR "%s: failed to allocate rx bufinfo\n",
1524 adapter->netdev->name);
1525 goto err;
1526 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001527 rq->buf_info[0] = bi;
1528 rq->buf_info[1] = bi + rq->rx_ring[0].size;
1529
1530 return 0;
1531
1532err:
1533 vmxnet3_rq_destroy(rq, adapter);
1534 return -ENOMEM;
1535}
1536
1537
1538static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001539vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
1540{
1541 int i, err = 0;
1542
1543 for (i = 0; i < adapter->num_rx_queues; i++) {
1544 err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
1545 if (unlikely(err)) {
1546 dev_err(&adapter->netdev->dev,
1547 "%s: failed to create rx queue%i\n",
1548 adapter->netdev->name, i);
1549 goto err_out;
1550 }
1551 }
1552 return err;
1553err_out:
1554 vmxnet3_rq_destroy_all(adapter);
1555 return err;
1556
1557}
1558
1559/* Multiple queue aware polling function for tx and rx */
1560
1561static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001562vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
1563{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001564 int rcd_done = 0, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001565 if (unlikely(adapter->shared->ecr))
1566 vmxnet3_process_events(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001567 for (i = 0; i < adapter->num_tx_queues; i++)
1568 vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001569
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001570 for (i = 0; i < adapter->num_rx_queues; i++)
1571 rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
1572 adapter, budget);
1573 return rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001574}
1575
1576
1577static int
1578vmxnet3_poll(struct napi_struct *napi, int budget)
1579{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001580 struct vmxnet3_rx_queue *rx_queue = container_of(napi,
1581 struct vmxnet3_rx_queue, napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001582 int rxd_done;
1583
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001584 rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001585
1586 if (rxd_done < budget) {
1587 napi_complete(napi);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001588 vmxnet3_enable_all_intrs(rx_queue->adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001589 }
1590 return rxd_done;
1591}
1592
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001593/*
1594 * NAPI polling function for MSI-X mode with multiple Rx queues
1595 * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
1596 */
1597
1598static int
1599vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
1600{
1601 struct vmxnet3_rx_queue *rq = container_of(napi,
1602 struct vmxnet3_rx_queue, napi);
1603 struct vmxnet3_adapter *adapter = rq->adapter;
1604 int rxd_done;
1605
1606 /* When sharing interrupt with corresponding tx queue, process
1607 * tx completions in that queue as well
1608 */
1609 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
1610 struct vmxnet3_tx_queue *tq =
1611 &adapter->tx_queue[rq - adapter->rx_queue];
1612 vmxnet3_tq_tx_complete(tq, adapter);
1613 }
1614
1615 rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
1616
1617 if (rxd_done < budget) {
1618 napi_complete(napi);
1619 vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
1620 }
1621 return rxd_done;
1622}
1623
1624
1625#ifdef CONFIG_PCI_MSI
1626
1627/*
1628 * Handle completion interrupts on tx queues
1629 * Returns whether or not the intr is handled
1630 */
1631
1632static irqreturn_t
1633vmxnet3_msix_tx(int irq, void *data)
1634{
1635 struct vmxnet3_tx_queue *tq = data;
1636 struct vmxnet3_adapter *adapter = tq->adapter;
1637
1638 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1639 vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
1640
1641 /* Handle the case where only one irq is allocate for all tx queues */
1642 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1643 int i;
1644 for (i = 0; i < adapter->num_tx_queues; i++) {
1645 struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
1646 vmxnet3_tq_tx_complete(txq, adapter);
1647 }
1648 } else {
1649 vmxnet3_tq_tx_complete(tq, adapter);
1650 }
1651 vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
1652
1653 return IRQ_HANDLED;
1654}
1655
1656
1657/*
1658 * Handle completion interrupts on rx queues. Returns whether or not the
1659 * intr is handled
1660 */
1661
1662static irqreturn_t
1663vmxnet3_msix_rx(int irq, void *data)
1664{
1665 struct vmxnet3_rx_queue *rq = data;
1666 struct vmxnet3_adapter *adapter = rq->adapter;
1667
1668 /* disable intr if needed */
1669 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1670 vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
1671 napi_schedule(&rq->napi);
1672
1673 return IRQ_HANDLED;
1674}
1675
1676/*
1677 *----------------------------------------------------------------------------
1678 *
1679 * vmxnet3_msix_event --
1680 *
1681 * vmxnet3 msix event intr handler
1682 *
1683 * Result:
1684 * whether or not the intr is handled
1685 *
1686 *----------------------------------------------------------------------------
1687 */
1688
1689static irqreturn_t
1690vmxnet3_msix_event(int irq, void *data)
1691{
1692 struct net_device *dev = data;
1693 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1694
1695 /* disable intr if needed */
1696 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1697 vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
1698
1699 if (adapter->shared->ecr)
1700 vmxnet3_process_events(adapter);
1701
1702 vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
1703
1704 return IRQ_HANDLED;
1705}
1706
1707#endif /* CONFIG_PCI_MSI */
1708
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001709
1710/* Interrupt handler for vmxnet3 */
1711static irqreturn_t
1712vmxnet3_intr(int irq, void *dev_id)
1713{
1714 struct net_device *dev = dev_id;
1715 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1716
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001717 if (adapter->intr.type == VMXNET3_IT_INTX) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001718 u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
1719 if (unlikely(icr == 0))
1720 /* not ours */
1721 return IRQ_NONE;
1722 }
1723
1724
1725 /* disable intr if needed */
1726 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001727 vmxnet3_disable_all_intrs(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001728
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001729 napi_schedule(&adapter->rx_queue[0].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001730
1731 return IRQ_HANDLED;
1732}
1733
1734#ifdef CONFIG_NET_POLL_CONTROLLER
1735
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001736/* netpoll callback. */
1737static void
1738vmxnet3_netpoll(struct net_device *netdev)
1739{
1740 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001741
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001742 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1743 vmxnet3_disable_all_intrs(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001744
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001745 vmxnet3_do_poll(adapter, adapter->rx_queue[0].rx_ring[0].size);
1746 vmxnet3_enable_all_intrs(adapter);
1747
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001748}
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001749#endif /* CONFIG_NET_POLL_CONTROLLER */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001750
1751static int
1752vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
1753{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001754 struct vmxnet3_intr *intr = &adapter->intr;
1755 int err = 0, i;
1756 int vector = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001757
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001758#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001759 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001760 for (i = 0; i < adapter->num_tx_queues; i++) {
1761 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1762 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
1763 adapter->netdev->name, vector);
1764 err = request_irq(
1765 intr->msix_entries[vector].vector,
1766 vmxnet3_msix_tx, 0,
1767 adapter->tx_queue[i].name,
1768 &adapter->tx_queue[i]);
1769 } else {
1770 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
1771 adapter->netdev->name, vector);
1772 }
1773 if (err) {
1774 dev_err(&adapter->netdev->dev,
1775 "Failed to request irq for MSIX, %s, "
1776 "error %d\n",
1777 adapter->tx_queue[i].name, err);
1778 return err;
1779 }
1780
1781 /* Handle the case where only 1 MSIx was allocated for
1782 * all tx queues */
1783 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1784 for (; i < adapter->num_tx_queues; i++)
1785 adapter->tx_queue[i].comp_ring.intr_idx
1786 = vector;
1787 vector++;
1788 break;
1789 } else {
1790 adapter->tx_queue[i].comp_ring.intr_idx
1791 = vector++;
1792 }
1793 }
1794 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
1795 vector = 0;
1796
1797 for (i = 0; i < adapter->num_rx_queues; i++) {
1798 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
1799 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
1800 adapter->netdev->name, vector);
1801 else
1802 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
1803 adapter->netdev->name, vector);
1804 err = request_irq(intr->msix_entries[vector].vector,
1805 vmxnet3_msix_rx, 0,
1806 adapter->rx_queue[i].name,
1807 &(adapter->rx_queue[i]));
1808 if (err) {
1809 printk(KERN_ERR "Failed to request irq for MSIX"
1810 ", %s, error %d\n",
1811 adapter->rx_queue[i].name, err);
1812 return err;
1813 }
1814
1815 adapter->rx_queue[i].comp_ring.intr_idx = vector++;
1816 }
1817
1818 sprintf(intr->event_msi_vector_name, "%s-event-%d",
1819 adapter->netdev->name, vector);
1820 err = request_irq(intr->msix_entries[vector].vector,
1821 vmxnet3_msix_event, 0,
1822 intr->event_msi_vector_name, adapter->netdev);
1823 intr->event_intr_idx = vector;
1824
1825 } else if (intr->type == VMXNET3_IT_MSI) {
1826 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001827 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
1828 adapter->netdev->name, adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001829 } else {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001830#endif
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001831 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001832 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
1833 IRQF_SHARED, adapter->netdev->name,
1834 adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001835#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001836 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001837#endif
1838 intr->num_intrs = vector + 1;
1839 if (err) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001840 printk(KERN_ERR "Failed to request irq %s (intr type:%d), error"
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001841 ":%d\n", adapter->netdev->name, intr->type, err);
1842 } else {
1843 /* Number of rx queues will not change after this */
1844 for (i = 0; i < adapter->num_rx_queues; i++) {
1845 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
1846 rq->qid = i;
1847 rq->qid2 = i + adapter->num_rx_queues;
1848 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001849
1850
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001851
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001852 /* init our intr settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001853 for (i = 0; i < intr->num_intrs; i++)
1854 intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
1855 if (adapter->intr.type != VMXNET3_IT_MSIX) {
1856 adapter->intr.event_intr_idx = 0;
1857 for (i = 0; i < adapter->num_tx_queues; i++)
1858 adapter->tx_queue[i].comp_ring.intr_idx = 0;
1859 adapter->rx_queue[0].comp_ring.intr_idx = 0;
1860 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001861
1862 printk(KERN_INFO "%s: intr type %u, mode %u, %u vectors "
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001863 "allocated\n", adapter->netdev->name, intr->type,
1864 intr->mask_mode, intr->num_intrs);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001865 }
1866
1867 return err;
1868}
1869
1870
1871static void
1872vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
1873{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001874 struct vmxnet3_intr *intr = &adapter->intr;
1875 BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001876
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001877 switch (intr->type) {
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001878#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001879 case VMXNET3_IT_MSIX:
1880 {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001881 int i, vector = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001882
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001883 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1884 for (i = 0; i < adapter->num_tx_queues; i++) {
1885 free_irq(intr->msix_entries[vector++].vector,
1886 &(adapter->tx_queue[i]));
1887 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
1888 break;
1889 }
1890 }
1891
1892 for (i = 0; i < adapter->num_rx_queues; i++) {
1893 free_irq(intr->msix_entries[vector++].vector,
1894 &(adapter->rx_queue[i]));
1895 }
1896
1897 free_irq(intr->msix_entries[vector].vector,
1898 adapter->netdev);
1899 BUG_ON(vector >= intr->num_intrs);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001900 break;
1901 }
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001902#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001903 case VMXNET3_IT_MSI:
1904 free_irq(adapter->pdev->irq, adapter->netdev);
1905 break;
1906 case VMXNET3_IT_INTX:
1907 free_irq(adapter->pdev->irq, adapter->netdev);
1908 break;
1909 default:
1910 BUG_ON(true);
1911 }
1912}
1913
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001914
1915static void
1916vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
1917{
Jesse Gross72e85c42011-06-23 13:04:39 +00001918 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1919 u16 vid;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001920
Jesse Gross72e85c42011-06-23 13:04:39 +00001921 /* allow untagged pkts */
1922 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1923
1924 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1925 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001926}
1927
1928
Jiri Pirko8e586132011-12-08 19:52:37 -05001929static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001930vmxnet3_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1931{
1932 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001933
Jesse Grossf6957f82011-08-07 23:15:47 +00001934 if (!(netdev->flags & IFF_PROMISC)) {
1935 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1936 unsigned long flags;
1937
1938 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1939 spin_lock_irqsave(&adapter->cmd_lock, flags);
1940 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1941 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1942 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1943 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001944
1945 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001946
1947 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001948}
1949
1950
Jiri Pirko8e586132011-12-08 19:52:37 -05001951static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001952vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1953{
1954 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001955
Jesse Grossf6957f82011-08-07 23:15:47 +00001956 if (!(netdev->flags & IFF_PROMISC)) {
1957 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1958 unsigned long flags;
1959
1960 VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
1961 spin_lock_irqsave(&adapter->cmd_lock, flags);
1962 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1963 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1964 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1965 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001966
1967 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001968
1969 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001970}
1971
1972
1973static u8 *
1974vmxnet3_copy_mc(struct net_device *netdev)
1975{
1976 u8 *buf = NULL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001977 u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001978
1979 /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
1980 if (sz <= 0xffff) {
1981 /* We may be called with BH disabled */
1982 buf = kmalloc(sz, GFP_ATOMIC);
1983 if (buf) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001984 struct netdev_hw_addr *ha;
Jiri Pirko567ec872010-02-23 23:17:07 +00001985 int i = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001986
Jiri Pirko22bedad32010-04-01 21:22:57 +00001987 netdev_for_each_mc_addr(ha, netdev)
1988 memcpy(buf + i++ * ETH_ALEN, ha->addr,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001989 ETH_ALEN);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001990 }
1991 }
1992 return buf;
1993}
1994
1995
1996static void
1997vmxnet3_set_mc(struct net_device *netdev)
1998{
1999 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002000 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002001 struct Vmxnet3_RxFilterConf *rxConf =
2002 &adapter->shared->devRead.rxFilterConf;
2003 u8 *new_table = NULL;
2004 u32 new_mode = VMXNET3_RXM_UCAST;
2005
Jesse Gross72e85c42011-06-23 13:04:39 +00002006 if (netdev->flags & IFF_PROMISC) {
2007 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2008 memset(vfTable, 0, VMXNET3_VFT_SIZE * sizeof(*vfTable));
2009
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002010 new_mode |= VMXNET3_RXM_PROMISC;
Jesse Gross72e85c42011-06-23 13:04:39 +00002011 } else {
2012 vmxnet3_restore_vlan(adapter);
2013 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002014
2015 if (netdev->flags & IFF_BROADCAST)
2016 new_mode |= VMXNET3_RXM_BCAST;
2017
2018 if (netdev->flags & IFF_ALLMULTI)
2019 new_mode |= VMXNET3_RXM_ALL_MULTI;
2020 else
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002021 if (!netdev_mc_empty(netdev)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002022 new_table = vmxnet3_copy_mc(netdev);
2023 if (new_table) {
2024 new_mode |= VMXNET3_RXM_MCAST;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002025 rxConf->mfTableLen = cpu_to_le16(
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002026 netdev_mc_count(netdev) * ETH_ALEN);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002027 rxConf->mfTablePA = cpu_to_le64(virt_to_phys(
2028 new_table));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002029 } else {
2030 printk(KERN_INFO "%s: failed to copy mcast list"
2031 ", setting ALL_MULTI\n", netdev->name);
2032 new_mode |= VMXNET3_RXM_ALL_MULTI;
2033 }
2034 }
2035
2036
2037 if (!(new_mode & VMXNET3_RXM_MCAST)) {
2038 rxConf->mfTableLen = 0;
2039 rxConf->mfTablePA = 0;
2040 }
2041
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002042 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002043 if (new_mode != rxConf->rxMode) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002044 rxConf->rxMode = cpu_to_le32(new_mode);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002045 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2046 VMXNET3_CMD_UPDATE_RX_MODE);
Jesse Gross72e85c42011-06-23 13:04:39 +00002047 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2048 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002049 }
2050
2051 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2052 VMXNET3_CMD_UPDATE_MAC_FILTERS);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002053 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002054
2055 kfree(new_table);
2056}
2057
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002058void
2059vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
2060{
2061 int i;
2062
2063 for (i = 0; i < adapter->num_rx_queues; i++)
2064 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2065}
2066
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002067
2068/*
2069 * Set up driver_shared based on settings in adapter.
2070 */
2071
2072static void
2073vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2074{
2075 struct Vmxnet3_DriverShared *shared = adapter->shared;
2076 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2077 struct Vmxnet3_TxQueueConf *tqc;
2078 struct Vmxnet3_RxQueueConf *rqc;
2079 int i;
2080
2081 memset(shared, 0, sizeof(*shared));
2082
2083 /* driver settings */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002084 shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2085 devRead->misc.driverInfo.version = cpu_to_le32(
2086 VMXNET3_DRIVER_VERSION_NUM);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002087 devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2088 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2089 devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002090 *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2091 *((u32 *)&devRead->misc.driverInfo.gos));
2092 devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2093 devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002094
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002095 devRead->misc.ddPA = cpu_to_le64(virt_to_phys(adapter));
2096 devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002097
2098 /* set up feature flags */
Michał Mirosława0d27302011-04-18 13:31:21 +00002099 if (adapter->netdev->features & NETIF_F_RXCSUM)
Harvey Harrison3843e512010-10-21 18:05:32 +00002100 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002101
Michał Mirosława0d27302011-04-18 13:31:21 +00002102 if (adapter->netdev->features & NETIF_F_LRO) {
Harvey Harrison3843e512010-10-21 18:05:32 +00002103 devRead->misc.uptFeatures |= UPT1_F_LRO;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002104 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002105 }
Shreyas Bhatewara54da3d02011-01-14 14:59:36 +00002106 if (adapter->netdev->features & NETIF_F_HW_VLAN_RX)
Harvey Harrison3843e512010-10-21 18:05:32 +00002107 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002108
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002109 devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2110 devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2111 devRead->misc.queueDescLen = cpu_to_le32(
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002112 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2113 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002114
2115 /* tx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002116 devRead->misc.numTxQueues = adapter->num_tx_queues;
2117 for (i = 0; i < adapter->num_tx_queues; i++) {
2118 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2119 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2120 tqc = &adapter->tqd_start[i].conf;
2121 tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
2122 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2123 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
2124 tqc->ddPA = cpu_to_le64(virt_to_phys(tq->buf_info));
2125 tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
2126 tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
2127 tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
2128 tqc->ddLen = cpu_to_le32(
2129 sizeof(struct vmxnet3_tx_buf_info) *
2130 tqc->txRingSize);
2131 tqc->intrIdx = tq->comp_ring.intr_idx;
2132 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002133
2134 /* rx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002135 devRead->misc.numRxQueues = adapter->num_rx_queues;
2136 for (i = 0; i < adapter->num_rx_queues; i++) {
2137 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2138 rqc = &adapter->rqd_start[i].conf;
2139 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2140 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2141 rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
2142 rqc->ddPA = cpu_to_le64(virt_to_phys(
2143 rq->buf_info));
2144 rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
2145 rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
2146 rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
2147 rqc->ddLen = cpu_to_le32(
2148 sizeof(struct vmxnet3_rx_buf_info) *
2149 (rqc->rxRingSize[0] +
2150 rqc->rxRingSize[1]));
2151 rqc->intrIdx = rq->comp_ring.intr_idx;
2152 }
2153
2154#ifdef VMXNET3_RSS
2155 memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2156
2157 if (adapter->rss) {
2158 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
2159 devRead->misc.uptFeatures |= UPT1_F_RSS;
2160 devRead->misc.numRxQueues = adapter->num_rx_queues;
2161 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2162 UPT1_RSS_HASH_TYPE_IPV4 |
2163 UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2164 UPT1_RSS_HASH_TYPE_IPV6;
2165 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2166 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2167 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
2168 get_random_bytes(&rssConf->hashKey[0], rssConf->hashKeySize);
2169 for (i = 0; i < rssConf->indTableSize; i++)
2170 rssConf->indTable[i] = i % adapter->num_rx_queues;
2171
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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-10-13 00:15:51 -07002202 u32 ret;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002203 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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) {
2711 printk(KERN_ERR "Failed to enable MSI-X for %s, error"
2712 " %d\n", adapter->netdev->name, err);
2713 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 */
2720 vectors = vector_threshold;
2721 printk(KERN_ERR "Failed to enable %d MSI-X for %s, try"
2722 " %d instead\n", vectors, adapter->netdev->name,
2723 vector_threshold);
2724 }
2725 }
2726
2727 printk(KERN_INFO "Number of MSI-X interrupts which can be allocatedi"
2728 " are lower than min threshold required.\n");
2729 return err;
2730}
2731
2732
2733#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 Bhatewarad1a890fa2009-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 */
2794 printk(KERN_INFO "Failed to enable MSI-X for %s, error %d."
2795 "#rx queues : 1, try MSI\n", adapter->netdev->name, err);
2796
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002797 adapter->intr.type = VMXNET3_IT_MSI;
2798 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002799
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002800 if (adapter->intr.type == VMXNET3_IT_MSI) {
2801 int err;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002802 err = pci_enable_msi(adapter->pdev);
2803 if (!err) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002804 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002805 adapter->intr.num_intrs = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002806 return;
2807 }
2808 }
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002809#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002810
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002811 adapter->num_rx_queues = 1;
2812 printk(KERN_INFO "Using INTx interrupt, #Rx queues: 1.\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002813 adapter->intr.type = VMXNET3_IT_INTX;
2814
2815 /* INT-X related setting */
2816 adapter->intr.num_intrs = 1;
2817}
2818
2819
2820static void
2821vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
2822{
2823 if (adapter->intr.type == VMXNET3_IT_MSIX)
2824 pci_disable_msix(adapter->pdev);
2825 else if (adapter->intr.type == VMXNET3_IT_MSI)
2826 pci_disable_msi(adapter->pdev);
2827 else
2828 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
2829}
2830
2831
2832static void
2833vmxnet3_tx_timeout(struct net_device *netdev)
2834{
2835 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2836 adapter->tx_timeout_count++;
2837
2838 printk(KERN_ERR "%s: tx hang\n", adapter->netdev->name);
2839 schedule_work(&adapter->work);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002840 netif_wake_queue(adapter->netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002841}
2842
2843
2844static void
2845vmxnet3_reset_work(struct work_struct *data)
2846{
2847 struct vmxnet3_adapter *adapter;
2848
2849 adapter = container_of(data, struct vmxnet3_adapter, work);
2850
2851 /* if another thread is resetting the device, no need to proceed */
2852 if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2853 return;
2854
2855 /* if the device is closed, we must leave it alone */
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002856 rtnl_lock();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002857 if (netif_running(adapter->netdev)) {
2858 printk(KERN_INFO "%s: resetting\n", adapter->netdev->name);
2859 vmxnet3_quiesce_dev(adapter);
2860 vmxnet3_reset_dev(adapter);
2861 vmxnet3_activate_dev(adapter);
2862 } else {
2863 printk(KERN_INFO "%s: already closed\n", adapter->netdev->name);
2864 }
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002865 rtnl_unlock();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002866
2867 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2868}
2869
2870
2871static int __devinit
2872vmxnet3_probe_device(struct pci_dev *pdev,
2873 const struct pci_device_id *id)
2874{
2875 static const struct net_device_ops vmxnet3_netdev_ops = {
2876 .ndo_open = vmxnet3_open,
2877 .ndo_stop = vmxnet3_close,
2878 .ndo_start_xmit = vmxnet3_xmit_frame,
2879 .ndo_set_mac_address = vmxnet3_set_mac_addr,
2880 .ndo_change_mtu = vmxnet3_change_mtu,
Michał Mirosława0d27302011-04-18 13:31:21 +00002881 .ndo_set_features = vmxnet3_set_features,
stephen hemminger95305f62011-06-08 14:53:57 +00002882 .ndo_get_stats64 = vmxnet3_get_stats64,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002883 .ndo_tx_timeout = vmxnet3_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002884 .ndo_set_rx_mode = vmxnet3_set_mc,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002885 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
2886 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
2887#ifdef CONFIG_NET_POLL_CONTROLLER
2888 .ndo_poll_controller = vmxnet3_netpoll,
2889#endif
2890 };
2891 int err;
2892 bool dma64 = false; /* stupid gcc */
2893 u32 ver;
2894 struct net_device *netdev;
2895 struct vmxnet3_adapter *adapter;
2896 u8 mac[ETH_ALEN];
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002897 int size;
2898 int num_tx_queues;
2899 int num_rx_queues;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002900
Shreyas Bhatewarae154b632011-05-10 06:13:56 +00002901 if (!pci_msi_enabled())
2902 enable_mq = 0;
2903
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002904#ifdef VMXNET3_RSS
2905 if (enable_mq)
2906 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
2907 (int)num_online_cpus());
2908 else
2909#endif
2910 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002911 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002912
2913 if (enable_mq)
2914 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
2915 (int)num_online_cpus());
2916 else
2917 num_tx_queues = 1;
2918
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002919 num_tx_queues = rounddown_pow_of_two(num_tx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002920 netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
2921 max(num_tx_queues, num_rx_queues));
2922 printk(KERN_INFO "# of Tx queues : %d, # of Rx queues : %d\n",
2923 num_tx_queues, num_rx_queues);
2924
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002925 if (!netdev) {
2926 printk(KERN_ERR "Failed to alloc ethernet device for adapter "
2927 "%s\n", pci_name(pdev));
2928 return -ENOMEM;
2929 }
2930
2931 pci_set_drvdata(pdev, netdev);
2932 adapter = netdev_priv(netdev);
2933 adapter->netdev = netdev;
2934 adapter->pdev = pdev;
2935
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002936 spin_lock_init(&adapter->cmd_lock);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002937 adapter->shared = pci_alloc_consistent(adapter->pdev,
2938 sizeof(struct Vmxnet3_DriverShared),
2939 &adapter->shared_pa);
2940 if (!adapter->shared) {
2941 printk(KERN_ERR "Failed to allocate memory for %s\n",
2942 pci_name(pdev));
2943 err = -ENOMEM;
2944 goto err_alloc_shared;
2945 }
2946
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002947 adapter->num_rx_queues = num_rx_queues;
2948 adapter->num_tx_queues = num_tx_queues;
2949
2950 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
2951 size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
2952 adapter->tqd_start = pci_alloc_consistent(adapter->pdev, size,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002953 &adapter->queue_desc_pa);
2954
2955 if (!adapter->tqd_start) {
2956 printk(KERN_ERR "Failed to allocate memory for %s\n",
2957 pci_name(pdev));
2958 err = -ENOMEM;
2959 goto err_alloc_queue_desc;
2960 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002961 adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
2962 adapter->num_tx_queues);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002963
2964 adapter->pm_conf = kmalloc(sizeof(struct Vmxnet3_PMConf), GFP_KERNEL);
2965 if (adapter->pm_conf == NULL) {
2966 printk(KERN_ERR "Failed to allocate memory for %s\n",
2967 pci_name(pdev));
2968 err = -ENOMEM;
2969 goto err_alloc_pm;
2970 }
2971
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002972#ifdef VMXNET3_RSS
2973
2974 adapter->rss_conf = kmalloc(sizeof(struct UPT1_RSSConf), GFP_KERNEL);
2975 if (adapter->rss_conf == NULL) {
2976 printk(KERN_ERR "Failed to allocate memory for %s\n",
2977 pci_name(pdev));
2978 err = -ENOMEM;
2979 goto err_alloc_rss;
2980 }
2981#endif /* VMXNET3_RSS */
2982
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002983 err = vmxnet3_alloc_pci_resources(adapter, &dma64);
2984 if (err < 0)
2985 goto err_alloc_pci;
2986
2987 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
2988 if (ver & 1) {
2989 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
2990 } else {
2991 printk(KERN_ERR "Incompatible h/w version (0x%x) for adapter"
2992 " %s\n", ver, pci_name(pdev));
2993 err = -EBUSY;
2994 goto err_ver;
2995 }
2996
2997 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
2998 if (ver & 1) {
2999 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
3000 } else {
3001 printk(KERN_ERR "Incompatible upt version (0x%x) for "
3002 "adapter %s\n", ver, pci_name(pdev));
3003 err = -EBUSY;
3004 goto err_ver;
3005 }
3006
Shreyas Bhatewarae101e7d2011-07-20 16:01:11 +00003007 SET_NETDEV_DEV(netdev, &pdev->dev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003008 vmxnet3_declare_features(adapter, dma64);
3009
3010 adapter->dev_number = atomic_read(&devices_found);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003011
3012 adapter->share_intr = irq_share_mode;
3013 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE &&
3014 adapter->num_tx_queues != adapter->num_rx_queues)
3015 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
3016
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003017 vmxnet3_alloc_intr_resources(adapter);
3018
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003019#ifdef VMXNET3_RSS
3020 if (adapter->num_rx_queues > 1 &&
3021 adapter->intr.type == VMXNET3_IT_MSIX) {
3022 adapter->rss = true;
3023 printk(KERN_INFO "RSS is enabled.\n");
3024 } else {
3025 adapter->rss = false;
3026 }
3027#endif
3028
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003029 vmxnet3_read_mac_addr(adapter, mac);
3030 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3031
3032 netdev->netdev_ops = &vmxnet3_netdev_ops;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003033 vmxnet3_set_ethtool_ops(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003034 netdev->watchdog_timeo = 5 * HZ;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003035
3036 INIT_WORK(&adapter->work, vmxnet3_reset_work);
3037
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003038 if (adapter->intr.type == VMXNET3_IT_MSIX) {
3039 int i;
3040 for (i = 0; i < adapter->num_rx_queues; i++) {
3041 netif_napi_add(adapter->netdev,
3042 &adapter->rx_queue[i].napi,
3043 vmxnet3_poll_rx_only, 64);
3044 }
3045 } else {
3046 netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
3047 vmxnet3_poll, 64);
3048 }
3049
3050 netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
3051 netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
3052
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003053 err = register_netdev(netdev);
3054
3055 if (err) {
3056 printk(KERN_ERR "Failed to register adapter %s\n",
3057 pci_name(pdev));
3058 goto err_register;
3059 }
3060
3061 set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00003062 vmxnet3_check_link(adapter, false);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003063 atomic_inc(&devices_found);
3064 return 0;
3065
3066err_register:
3067 vmxnet3_free_intr_resources(adapter);
3068err_ver:
3069 vmxnet3_free_pci_resources(adapter);
3070err_alloc_pci:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003071#ifdef VMXNET3_RSS
3072 kfree(adapter->rss_conf);
3073err_alloc_rss:
3074#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003075 kfree(adapter->pm_conf);
3076err_alloc_pm:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003077 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3078 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003079err_alloc_queue_desc:
3080 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3081 adapter->shared, adapter->shared_pa);
3082err_alloc_shared:
3083 pci_set_drvdata(pdev, NULL);
3084 free_netdev(netdev);
3085 return err;
3086}
3087
3088
3089static void __devexit
3090vmxnet3_remove_device(struct pci_dev *pdev)
3091{
3092 struct net_device *netdev = pci_get_drvdata(pdev);
3093 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003094 int size = 0;
3095 int num_rx_queues;
3096
3097#ifdef VMXNET3_RSS
3098 if (enable_mq)
3099 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3100 (int)num_online_cpus());
3101 else
3102#endif
3103 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07003104 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003105
Tejun Heo23f333a2010-12-12 16:45:14 +01003106 cancel_work_sync(&adapter->work);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003107
3108 unregister_netdev(netdev);
3109
3110 vmxnet3_free_intr_resources(adapter);
3111 vmxnet3_free_pci_resources(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003112#ifdef VMXNET3_RSS
3113 kfree(adapter->rss_conf);
3114#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003115 kfree(adapter->pm_conf);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003116
3117 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3118 size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
3119 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3120 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003121 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3122 adapter->shared, adapter->shared_pa);
3123 free_netdev(netdev);
3124}
3125
3126
3127#ifdef CONFIG_PM
3128
3129static int
3130vmxnet3_suspend(struct device *device)
3131{
3132 struct pci_dev *pdev = to_pci_dev(device);
3133 struct net_device *netdev = pci_get_drvdata(pdev);
3134 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3135 struct Vmxnet3_PMConf *pmConf;
3136 struct ethhdr *ehdr;
3137 struct arphdr *ahdr;
3138 u8 *arpreq;
3139 struct in_device *in_dev;
3140 struct in_ifaddr *ifa;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003141 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003142 int i = 0;
3143
3144 if (!netif_running(netdev))
3145 return 0;
3146
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003147 for (i = 0; i < adapter->num_rx_queues; i++)
3148 napi_disable(&adapter->rx_queue[i].napi);
3149
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003150 vmxnet3_disable_all_intrs(adapter);
3151 vmxnet3_free_irqs(adapter);
3152 vmxnet3_free_intr_resources(adapter);
3153
3154 netif_device_detach(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003155 netif_tx_stop_all_queues(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003156
3157 /* Create wake-up filters. */
3158 pmConf = adapter->pm_conf;
3159 memset(pmConf, 0, sizeof(*pmConf));
3160
3161 if (adapter->wol & WAKE_UCAST) {
3162 pmConf->filters[i].patternSize = ETH_ALEN;
3163 pmConf->filters[i].maskSize = 1;
3164 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
3165 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
3166
Harvey Harrison3843e512010-10-21 18:05:32 +00003167 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003168 i++;
3169 }
3170
3171 if (adapter->wol & WAKE_ARP) {
3172 in_dev = in_dev_get(netdev);
3173 if (!in_dev)
3174 goto skip_arp;
3175
3176 ifa = (struct in_ifaddr *)in_dev->ifa_list;
3177 if (!ifa)
3178 goto skip_arp;
3179
3180 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
3181 sizeof(struct arphdr) + /* ARP header */
3182 2 * ETH_ALEN + /* 2 Ethernet addresses*/
3183 2 * sizeof(u32); /*2 IPv4 addresses */
3184 pmConf->filters[i].maskSize =
3185 (pmConf->filters[i].patternSize - 1) / 8 + 1;
3186
3187 /* ETH_P_ARP in Ethernet header. */
3188 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
3189 ehdr->h_proto = htons(ETH_P_ARP);
3190
3191 /* ARPOP_REQUEST in ARP header. */
3192 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
3193 ahdr->ar_op = htons(ARPOP_REQUEST);
3194 arpreq = (u8 *)(ahdr + 1);
3195
3196 /* The Unicast IPv4 address in 'tip' field. */
3197 arpreq += 2 * ETH_ALEN + sizeof(u32);
3198 *(u32 *)arpreq = ifa->ifa_address;
3199
3200 /* The mask for the relevant bits. */
3201 pmConf->filters[i].mask[0] = 0x00;
3202 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
3203 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
3204 pmConf->filters[i].mask[3] = 0x00;
3205 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
3206 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
3207 in_dev_put(in_dev);
3208
Harvey Harrison3843e512010-10-21 18:05:32 +00003209 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003210 i++;
3211 }
3212
3213skip_arp:
3214 if (adapter->wol & WAKE_MAGIC)
Harvey Harrison3843e512010-10-21 18:05:32 +00003215 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003216
3217 pmConf->numFilters = i;
3218
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003219 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3220 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3221 *pmConf));
3222 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3223 pmConf));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003224
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003225 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003226 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3227 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003228 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003229
3230 pci_save_state(pdev);
3231 pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
3232 adapter->wol);
3233 pci_disable_device(pdev);
3234 pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
3235
3236 return 0;
3237}
3238
3239
3240static int
3241vmxnet3_resume(struct device *device)
3242{
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003243 int err, i = 0;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003244 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003245 struct pci_dev *pdev = to_pci_dev(device);
3246 struct net_device *netdev = pci_get_drvdata(pdev);
3247 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3248 struct Vmxnet3_PMConf *pmConf;
3249
3250 if (!netif_running(netdev))
3251 return 0;
3252
3253 /* Destroy wake-up filters. */
3254 pmConf = adapter->pm_conf;
3255 memset(pmConf, 0, sizeof(*pmConf));
3256
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003257 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3258 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3259 *pmConf));
Harvey Harrison0561cf32010-10-21 18:05:34 +00003260 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003261 pmConf));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003262
3263 netif_device_attach(netdev);
3264 pci_set_power_state(pdev, PCI_D0);
3265 pci_restore_state(pdev);
3266 err = pci_enable_device_mem(pdev);
3267 if (err != 0)
3268 return err;
3269
3270 pci_enable_wake(pdev, PCI_D0, 0);
3271
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003272 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003273 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3274 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003275 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003276 vmxnet3_alloc_intr_resources(adapter);
3277 vmxnet3_request_irqs(adapter);
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003278 for (i = 0; i < adapter->num_rx_queues; i++)
3279 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003280 vmxnet3_enable_all_intrs(adapter);
3281
3282 return 0;
3283}
3284
Alexey Dobriyan47145212009-12-14 18:00:08 -08003285static const struct dev_pm_ops vmxnet3_pm_ops = {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003286 .suspend = vmxnet3_suspend,
3287 .resume = vmxnet3_resume,
3288};
3289#endif
3290
3291static struct pci_driver vmxnet3_driver = {
3292 .name = vmxnet3_driver_name,
3293 .id_table = vmxnet3_pciid_table,
3294 .probe = vmxnet3_probe_device,
3295 .remove = __devexit_p(vmxnet3_remove_device),
3296#ifdef CONFIG_PM
3297 .driver.pm = &vmxnet3_pm_ops,
3298#endif
3299};
3300
3301
3302static int __init
3303vmxnet3_init_module(void)
3304{
3305 printk(KERN_INFO "%s - version %s\n", VMXNET3_DRIVER_DESC,
3306 VMXNET3_DRIVER_VERSION_REPORT);
3307 return pci_register_driver(&vmxnet3_driver);
3308}
3309
3310module_init(vmxnet3_init_module);
3311
3312
3313static void
3314vmxnet3_exit_module(void)
3315{
3316 pci_unregister_driver(&vmxnet3_driver);
3317}
3318
3319module_exit(vmxnet3_exit_module);
3320
3321MODULE_AUTHOR("VMware, Inc.");
3322MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
3323MODULE_LICENSE("GPL v2");
3324MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);