blob: d862937cdc20e508933abda602bde71e6df29b4d [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. */
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000155 netdev_info(adapter->netdev, "NIC Link is Up %d Mbps\n",
156 adapter->link_speed);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700157 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 {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000166 netdev_info(adapter->netdev, "NIC Link is Down\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700167 if (netif_carrier_ok(adapter->netdev))
168 netif_carrier_off(adapter->netdev);
169
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000170 if (affectTxQueue) {
171 for (i = 0; i < adapter->num_tx_queues; i++)
172 vmxnet3_tq_stop(&adapter->tx_queue[i], adapter);
173 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700174 }
175}
176
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700177static void
178vmxnet3_process_events(struct vmxnet3_adapter *adapter)
179{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000180 int i;
Roland Dreiere328d412011-05-06 08:32:53 +0000181 unsigned long flags;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000182 u32 events = le32_to_cpu(adapter->shared->ecr);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700183 if (!events)
184 return;
185
186 vmxnet3_ack_events(adapter, events);
187
188 /* Check if link state has changed */
189 if (events & VMXNET3_ECR_LINK)
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +0000190 vmxnet3_check_link(adapter, true);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700191
192 /* Check if there is an error on xmit/recv queues */
193 if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
Roland Dreiere328d412011-05-06 08:32:53 +0000194 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700195 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
196 VMXNET3_CMD_GET_QUEUE_STATUS);
Roland Dreiere328d412011-05-06 08:32:53 +0000197 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700198
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000199 for (i = 0; i < adapter->num_tx_queues; i++)
200 if (adapter->tqd_start[i].status.stopped)
201 dev_err(&adapter->netdev->dev,
202 "%s: tq[%d] error 0x%x\n",
203 adapter->netdev->name, i, le32_to_cpu(
204 adapter->tqd_start[i].status.error));
205 for (i = 0; i < adapter->num_rx_queues; i++)
206 if (adapter->rqd_start[i].status.stopped)
207 dev_err(&adapter->netdev->dev,
208 "%s: rq[%d] error 0x%x\n",
209 adapter->netdev->name, i,
210 adapter->rqd_start[i].status.error);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700211
212 schedule_work(&adapter->work);
213 }
214}
215
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000216#ifdef __BIG_ENDIAN_BITFIELD
217/*
218 * The device expects the bitfields in shared structures to be written in
219 * little endian. When CPU is big endian, the following routines are used to
220 * correctly read and write into ABI.
221 * The general technique used here is : double word bitfields are defined in
222 * opposite order for big endian architecture. Then before reading them in
223 * driver the complete double word is translated using le32_to_cpu. Similarly
224 * After the driver writes into bitfields, cpu_to_le32 is used to translate the
225 * double words into required format.
226 * In order to avoid touching bits in shared structure more than once, temporary
227 * descriptors are used. These are passed as srcDesc to following functions.
228 */
229static void vmxnet3_RxDescToCPU(const struct Vmxnet3_RxDesc *srcDesc,
230 struct Vmxnet3_RxDesc *dstDesc)
231{
232 u32 *src = (u32 *)srcDesc + 2;
233 u32 *dst = (u32 *)dstDesc + 2;
234 dstDesc->addr = le64_to_cpu(srcDesc->addr);
235 *dst = le32_to_cpu(*src);
236 dstDesc->ext1 = le32_to_cpu(srcDesc->ext1);
237}
238
239static void vmxnet3_TxDescToLe(const struct Vmxnet3_TxDesc *srcDesc,
240 struct Vmxnet3_TxDesc *dstDesc)
241{
242 int i;
243 u32 *src = (u32 *)(srcDesc + 1);
244 u32 *dst = (u32 *)(dstDesc + 1);
245
246 /* Working backwards so that the gen bit is set at the end. */
247 for (i = 2; i > 0; i--) {
248 src--;
249 dst--;
250 *dst = cpu_to_le32(*src);
251 }
252}
253
254
255static void vmxnet3_RxCompToCPU(const struct Vmxnet3_RxCompDesc *srcDesc,
256 struct Vmxnet3_RxCompDesc *dstDesc)
257{
258 int i = 0;
259 u32 *src = (u32 *)srcDesc;
260 u32 *dst = (u32 *)dstDesc;
261 for (i = 0; i < sizeof(struct Vmxnet3_RxCompDesc) / sizeof(u32); i++) {
262 *dst = le32_to_cpu(*src);
263 src++;
264 dst++;
265 }
266}
267
268
269/* Used to read bitfield values from double words. */
270static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
271{
272 u32 temp = le32_to_cpu(*bitfield);
273 u32 mask = ((1 << size) - 1) << pos;
274 temp &= mask;
275 temp >>= pos;
276 return temp;
277}
278
279
280
281#endif /* __BIG_ENDIAN_BITFIELD */
282
283#ifdef __BIG_ENDIAN_BITFIELD
284
285# define VMXNET3_TXDESC_GET_GEN(txdesc) get_bitfield32(((const __le32 *) \
286 txdesc) + VMXNET3_TXD_GEN_DWORD_SHIFT, \
287 VMXNET3_TXD_GEN_SHIFT, VMXNET3_TXD_GEN_SIZE)
288# define VMXNET3_TXDESC_GET_EOP(txdesc) get_bitfield32(((const __le32 *) \
289 txdesc) + VMXNET3_TXD_EOP_DWORD_SHIFT, \
290 VMXNET3_TXD_EOP_SHIFT, VMXNET3_TXD_EOP_SIZE)
291# define VMXNET3_TCD_GET_GEN(tcd) get_bitfield32(((const __le32 *)tcd) + \
292 VMXNET3_TCD_GEN_DWORD_SHIFT, VMXNET3_TCD_GEN_SHIFT, \
293 VMXNET3_TCD_GEN_SIZE)
294# define VMXNET3_TCD_GET_TXIDX(tcd) get_bitfield32((const __le32 *)tcd, \
295 VMXNET3_TCD_TXIDX_SHIFT, VMXNET3_TCD_TXIDX_SIZE)
296# define vmxnet3_getRxComp(dstrcd, rcd, tmp) do { \
297 (dstrcd) = (tmp); \
298 vmxnet3_RxCompToCPU((rcd), (tmp)); \
299 } while (0)
300# define vmxnet3_getRxDesc(dstrxd, rxd, tmp) do { \
301 (dstrxd) = (tmp); \
302 vmxnet3_RxDescToCPU((rxd), (tmp)); \
303 } while (0)
304
305#else
306
307# define VMXNET3_TXDESC_GET_GEN(txdesc) ((txdesc)->gen)
308# define VMXNET3_TXDESC_GET_EOP(txdesc) ((txdesc)->eop)
309# define VMXNET3_TCD_GET_GEN(tcd) ((tcd)->gen)
310# define VMXNET3_TCD_GET_TXIDX(tcd) ((tcd)->txdIdx)
311# define vmxnet3_getRxComp(dstrcd, rcd, tmp) (dstrcd) = (rcd)
312# define vmxnet3_getRxDesc(dstrxd, rxd, tmp) (dstrxd) = (rxd)
313
314#endif /* __BIG_ENDIAN_BITFIELD */
315
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700316
317static void
318vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
319 struct pci_dev *pdev)
320{
321 if (tbi->map_type == VMXNET3_MAP_SINGLE)
322 pci_unmap_single(pdev, tbi->dma_addr, tbi->len,
323 PCI_DMA_TODEVICE);
324 else if (tbi->map_type == VMXNET3_MAP_PAGE)
325 pci_unmap_page(pdev, tbi->dma_addr, tbi->len,
326 PCI_DMA_TODEVICE);
327 else
328 BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
329
330 tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
331}
332
333
334static int
335vmxnet3_unmap_pkt(u32 eop_idx, struct vmxnet3_tx_queue *tq,
336 struct pci_dev *pdev, struct vmxnet3_adapter *adapter)
337{
338 struct sk_buff *skb;
339 int entries = 0;
340
341 /* no out of order completion */
342 BUG_ON(tq->buf_info[eop_idx].sop_idx != tq->tx_ring.next2comp);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000343 BUG_ON(VMXNET3_TXDESC_GET_EOP(&(tq->tx_ring.base[eop_idx].txd)) != 1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700344
345 skb = tq->buf_info[eop_idx].skb;
346 BUG_ON(skb == NULL);
347 tq->buf_info[eop_idx].skb = NULL;
348
349 VMXNET3_INC_RING_IDX_ONLY(eop_idx, tq->tx_ring.size);
350
351 while (tq->tx_ring.next2comp != eop_idx) {
352 vmxnet3_unmap_tx_buf(tq->buf_info + tq->tx_ring.next2comp,
353 pdev);
354
355 /* update next2comp w/o tx_lock. Since we are marking more,
356 * instead of less, tx ring entries avail, the worst case is
357 * that the tx routine incorrectly re-queues a pkt due to
358 * insufficient tx ring entries.
359 */
360 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
361 entries++;
362 }
363
364 dev_kfree_skb_any(skb);
365 return entries;
366}
367
368
369static int
370vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue *tq,
371 struct vmxnet3_adapter *adapter)
372{
373 int completed = 0;
374 union Vmxnet3_GenericDesc *gdesc;
375
376 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000377 while (VMXNET3_TCD_GET_GEN(&gdesc->tcd) == tq->comp_ring.gen) {
378 completed += vmxnet3_unmap_pkt(VMXNET3_TCD_GET_TXIDX(
379 &gdesc->tcd), tq, adapter->pdev,
380 adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700381
382 vmxnet3_comp_ring_adv_next2proc(&tq->comp_ring);
383 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
384 }
385
386 if (completed) {
387 spin_lock(&tq->tx_lock);
388 if (unlikely(vmxnet3_tq_stopped(tq, adapter) &&
389 vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) >
390 VMXNET3_WAKE_QUEUE_THRESHOLD(tq) &&
391 netif_carrier_ok(adapter->netdev))) {
392 vmxnet3_tq_wake(tq, adapter);
393 }
394 spin_unlock(&tq->tx_lock);
395 }
396 return completed;
397}
398
399
400static void
401vmxnet3_tq_cleanup(struct vmxnet3_tx_queue *tq,
402 struct vmxnet3_adapter *adapter)
403{
404 int i;
405
406 while (tq->tx_ring.next2comp != tq->tx_ring.next2fill) {
407 struct vmxnet3_tx_buf_info *tbi;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700408
409 tbi = tq->buf_info + tq->tx_ring.next2comp;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700410
411 vmxnet3_unmap_tx_buf(tbi, adapter->pdev);
412 if (tbi->skb) {
413 dev_kfree_skb_any(tbi->skb);
414 tbi->skb = NULL;
415 }
416 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
417 }
418
419 /* sanity check, verify all buffers are indeed unmapped and freed */
420 for (i = 0; i < tq->tx_ring.size; i++) {
421 BUG_ON(tq->buf_info[i].skb != NULL ||
422 tq->buf_info[i].map_type != VMXNET3_MAP_NONE);
423 }
424
425 tq->tx_ring.gen = VMXNET3_INIT_GEN;
426 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
427
428 tq->comp_ring.gen = VMXNET3_INIT_GEN;
429 tq->comp_ring.next2proc = 0;
430}
431
432
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000433static void
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700434vmxnet3_tq_destroy(struct vmxnet3_tx_queue *tq,
435 struct vmxnet3_adapter *adapter)
436{
437 if (tq->tx_ring.base) {
438 pci_free_consistent(adapter->pdev, tq->tx_ring.size *
439 sizeof(struct Vmxnet3_TxDesc),
440 tq->tx_ring.base, tq->tx_ring.basePA);
441 tq->tx_ring.base = NULL;
442 }
443 if (tq->data_ring.base) {
444 pci_free_consistent(adapter->pdev, tq->data_ring.size *
445 sizeof(struct Vmxnet3_TxDataDesc),
446 tq->data_ring.base, tq->data_ring.basePA);
447 tq->data_ring.base = NULL;
448 }
449 if (tq->comp_ring.base) {
450 pci_free_consistent(adapter->pdev, tq->comp_ring.size *
451 sizeof(struct Vmxnet3_TxCompDesc),
452 tq->comp_ring.base, tq->comp_ring.basePA);
453 tq->comp_ring.base = NULL;
454 }
455 kfree(tq->buf_info);
456 tq->buf_info = NULL;
457}
458
459
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000460/* Destroy all tx queues */
461void
462vmxnet3_tq_destroy_all(struct vmxnet3_adapter *adapter)
463{
464 int i;
465
466 for (i = 0; i < adapter->num_tx_queues; i++)
467 vmxnet3_tq_destroy(&adapter->tx_queue[i], adapter);
468}
469
470
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700471static void
472vmxnet3_tq_init(struct vmxnet3_tx_queue *tq,
473 struct vmxnet3_adapter *adapter)
474{
475 int i;
476
477 /* reset the tx ring contents to 0 and reset the tx ring states */
478 memset(tq->tx_ring.base, 0, tq->tx_ring.size *
479 sizeof(struct Vmxnet3_TxDesc));
480 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
481 tq->tx_ring.gen = VMXNET3_INIT_GEN;
482
483 memset(tq->data_ring.base, 0, tq->data_ring.size *
484 sizeof(struct Vmxnet3_TxDataDesc));
485
486 /* reset the tx comp ring contents to 0 and reset comp ring states */
487 memset(tq->comp_ring.base, 0, tq->comp_ring.size *
488 sizeof(struct Vmxnet3_TxCompDesc));
489 tq->comp_ring.next2proc = 0;
490 tq->comp_ring.gen = VMXNET3_INIT_GEN;
491
492 /* reset the bookkeeping data */
493 memset(tq->buf_info, 0, sizeof(tq->buf_info[0]) * tq->tx_ring.size);
494 for (i = 0; i < tq->tx_ring.size; i++)
495 tq->buf_info[i].map_type = VMXNET3_MAP_NONE;
496
497 /* stats are not reset */
498}
499
500
501static int
502vmxnet3_tq_create(struct vmxnet3_tx_queue *tq,
503 struct vmxnet3_adapter *adapter)
504{
505 BUG_ON(tq->tx_ring.base || tq->data_ring.base ||
506 tq->comp_ring.base || tq->buf_info);
507
508 tq->tx_ring.base = pci_alloc_consistent(adapter->pdev, tq->tx_ring.size
509 * sizeof(struct Vmxnet3_TxDesc),
510 &tq->tx_ring.basePA);
511 if (!tq->tx_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000512 netdev_err(adapter->netdev, "failed to allocate tx ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700513 goto err;
514 }
515
516 tq->data_ring.base = pci_alloc_consistent(adapter->pdev,
517 tq->data_ring.size *
518 sizeof(struct Vmxnet3_TxDataDesc),
519 &tq->data_ring.basePA);
520 if (!tq->data_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000521 netdev_err(adapter->netdev, "failed to allocate data ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700522 goto err;
523 }
524
525 tq->comp_ring.base = pci_alloc_consistent(adapter->pdev,
526 tq->comp_ring.size *
527 sizeof(struct Vmxnet3_TxCompDesc),
528 &tq->comp_ring.basePA);
529 if (!tq->comp_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000530 netdev_err(adapter->netdev, "failed to allocate tx comp ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700531 goto err;
532 }
533
534 tq->buf_info = kcalloc(tq->tx_ring.size, sizeof(tq->buf_info[0]),
535 GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000536 if (!tq->buf_info)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700537 goto err;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700538
539 return 0;
540
541err:
542 vmxnet3_tq_destroy(tq, adapter);
543 return -ENOMEM;
544}
545
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000546static void
547vmxnet3_tq_cleanup_all(struct vmxnet3_adapter *adapter)
548{
549 int i;
550
551 for (i = 0; i < adapter->num_tx_queues; i++)
552 vmxnet3_tq_cleanup(&adapter->tx_queue[i], adapter);
553}
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700554
555/*
556 * starting from ring->next2fill, allocate rx buffers for the given ring
557 * of the rx queue and update the rx desc. stop after @num_to_alloc buffers
558 * are allocated or allocation fails
559 */
560
561static int
562vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
563 int num_to_alloc, struct vmxnet3_adapter *adapter)
564{
565 int num_allocated = 0;
566 struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
567 struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
568 u32 val;
569
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000570 while (num_allocated <= num_to_alloc) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700571 struct vmxnet3_rx_buf_info *rbi;
572 union Vmxnet3_GenericDesc *gd;
573
574 rbi = rbi_base + ring->next2fill;
575 gd = ring->base + ring->next2fill;
576
577 if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
578 if (rbi->skb == NULL) {
Stephen Hemminger0d735f12013-01-15 07:28:26 +0000579 rbi->skb = __netdev_alloc_skb_ip_align(adapter->netdev,
580 rbi->len,
581 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700582 if (unlikely(rbi->skb == NULL)) {
583 rq->stats.rx_buf_alloc_failure++;
584 break;
585 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700586
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700587 rbi->dma_addr = pci_map_single(adapter->pdev,
588 rbi->skb->data, rbi->len,
589 PCI_DMA_FROMDEVICE);
590 } else {
591 /* rx buffer skipped by the device */
592 }
593 val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
594 } else {
595 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
596 rbi->len != PAGE_SIZE);
597
598 if (rbi->page == NULL) {
599 rbi->page = alloc_page(GFP_ATOMIC);
600 if (unlikely(rbi->page == NULL)) {
601 rq->stats.rx_buf_alloc_failure++;
602 break;
603 }
604 rbi->dma_addr = pci_map_page(adapter->pdev,
605 rbi->page, 0, PAGE_SIZE,
606 PCI_DMA_FROMDEVICE);
607 } else {
608 /* rx buffers skipped by the device */
609 }
610 val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
611 }
612
613 BUG_ON(rbi->dma_addr == 0);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000614 gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000615 gd->dword[2] = cpu_to_le32((!ring->gen << VMXNET3_RXD_GEN_SHIFT)
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000616 | val | rbi->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700617
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000618 /* Fill the last buffer but dont mark it ready, or else the
619 * device will think that the queue is full */
620 if (num_allocated == num_to_alloc)
621 break;
622
623 gd->dword[2] |= cpu_to_le32(ring->gen << VMXNET3_RXD_GEN_SHIFT);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700624 num_allocated++;
625 vmxnet3_cmd_ring_adv_next2fill(ring);
626 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700627
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000628 netdev_dbg(adapter->netdev,
Stephen Hemminger69b9a712013-01-15 07:28:27 +0000629 "alloc_rx_buf: %d allocated, next2fill %u, next2comp %u\n",
630 num_allocated, ring->next2fill, ring->next2comp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700631
632 /* so that the device can distinguish a full ring and an empty ring */
633 BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
634
635 return num_allocated;
636}
637
638
639static void
640vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
641 struct vmxnet3_rx_buf_info *rbi)
642{
643 struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
644 skb_shinfo(skb)->nr_frags;
645
646 BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
647
Ian Campbell0e0634d2011-09-21 21:53:28 +0000648 __skb_frag_set_page(frag, rbi->page);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700649 frag->page_offset = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000650 skb_frag_size_set(frag, rcd->len);
651 skb->data_len += rcd->len;
Eric Dumazet5e6c3552011-10-13 11:38:17 +0000652 skb->truesize += PAGE_SIZE;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700653 skb_shinfo(skb)->nr_frags++;
654}
655
656
657static void
658vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
659 struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
660 struct vmxnet3_adapter *adapter)
661{
662 u32 dw2, len;
663 unsigned long buf_offset;
664 int i;
665 union Vmxnet3_GenericDesc *gdesc;
666 struct vmxnet3_tx_buf_info *tbi = NULL;
667
668 BUG_ON(ctx->copy_size > skb_headlen(skb));
669
670 /* use the previous gen bit for the SOP desc */
671 dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
672
673 ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
674 gdesc = ctx->sop_txd; /* both loops below can be skipped */
675
676 /* no need to map the buffer if headers are copied */
677 if (ctx->copy_size) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000678 ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700679 tq->tx_ring.next2fill *
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000680 sizeof(struct Vmxnet3_TxDataDesc));
681 ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700682 ctx->sop_txd->dword[3] = 0;
683
684 tbi = tq->buf_info + tq->tx_ring.next2fill;
685 tbi->map_type = VMXNET3_MAP_NONE;
686
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000687 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700688 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000689 tq->tx_ring.next2fill,
690 le64_to_cpu(ctx->sop_txd->txd.addr),
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700691 ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
692 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
693
694 /* use the right gen for non-SOP desc */
695 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
696 }
697
698 /* linear part can use multiple tx desc if it's big */
699 len = skb_headlen(skb) - ctx->copy_size;
700 buf_offset = ctx->copy_size;
701 while (len) {
702 u32 buf_size;
703
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000704 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
705 buf_size = len;
706 dw2 |= len;
707 } else {
708 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
709 /* spec says that for TxDesc.len, 0 == 2^14 */
710 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700711
712 tbi = tq->buf_info + tq->tx_ring.next2fill;
713 tbi->map_type = VMXNET3_MAP_SINGLE;
714 tbi->dma_addr = pci_map_single(adapter->pdev,
715 skb->data + buf_offset, buf_size,
716 PCI_DMA_TODEVICE);
717
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000718 tbi->len = buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700719
720 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
721 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
722
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000723 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000724 gdesc->dword[2] = cpu_to_le32(dw2);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700725 gdesc->dword[3] = 0;
726
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000727 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700728 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000729 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
730 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700731 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
732 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
733
734 len -= buf_size;
735 buf_offset += buf_size;
736 }
737
738 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000739 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000740 u32 buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700741
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000742 buf_offset = 0;
743 len = skb_frag_size(frag);
744 while (len) {
745 tbi = tq->buf_info + tq->tx_ring.next2fill;
746 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
747 buf_size = len;
748 dw2 |= len;
749 } else {
750 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
751 /* spec says that for TxDesc.len, 0 == 2^14 */
752 }
753 tbi->map_type = VMXNET3_MAP_PAGE;
754 tbi->dma_addr = skb_frag_dma_map(&adapter->pdev->dev, frag,
755 buf_offset, buf_size,
756 DMA_TO_DEVICE);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700757
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000758 tbi->len = buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700759
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000760 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
761 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700762
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000763 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
764 gdesc->dword[2] = cpu_to_le32(dw2);
765 gdesc->dword[3] = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700766
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000767 netdev_dbg(adapter->netdev,
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000768 "txd[%u]: 0x%llu %u %u\n",
769 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
770 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
771 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
772 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
773
774 len -= buf_size;
775 buf_offset += buf_size;
776 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700777 }
778
779 ctx->eop_txd = gdesc;
780
781 /* set the last buf_info for the pkt */
782 tbi->skb = skb;
783 tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
784}
785
786
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000787/* Init all tx queues */
788static void
789vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
790{
791 int i;
792
793 for (i = 0; i < adapter->num_tx_queues; i++)
794 vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
795}
796
797
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700798/*
799 * parse and copy relevant protocol headers:
800 * For a tso pkt, relevant headers are L2/3/4 including options
801 * For a pkt requesting csum offloading, they are L2/3 and may include L4
802 * if it's a TCP/UDP pkt
803 *
804 * Returns:
805 * -1: error happens during parsing
806 * 0: protocol headers parsed, but too big to be copied
807 * 1: protocol headers parsed and copied
808 *
809 * Other effects:
810 * 1. related *ctx fields are updated.
811 * 2. ctx->copy_size is # of bytes copied
812 * 3. the portion copied is guaranteed to be in the linear part
813 *
814 */
815static int
816vmxnet3_parse_and_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
817 struct vmxnet3_tx_ctx *ctx,
818 struct vmxnet3_adapter *adapter)
819{
820 struct Vmxnet3_TxDataDesc *tdd;
821
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000822 if (ctx->mss) { /* TSO */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700823 ctx->eth_ip_hdr_size = skb_transport_offset(skb);
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000824 ctx->l4_hdr_size = tcp_hdrlen(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700825 ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
826 } else {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700827 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000828 ctx->eth_ip_hdr_size = skb_checksum_start_offset(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700829
830 if (ctx->ipv4) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000831 const struct iphdr *iph = ip_hdr(skb);
832
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000833 if (iph->protocol == IPPROTO_TCP)
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000834 ctx->l4_hdr_size = tcp_hdrlen(skb);
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000835 else if (iph->protocol == IPPROTO_UDP)
David S. Millerf6a1ad42012-03-05 21:16:26 -0500836 ctx->l4_hdr_size = sizeof(struct udphdr);
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000837 else
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700838 ctx->l4_hdr_size = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700839 } else {
840 /* for simplicity, don't copy L4 headers */
841 ctx->l4_hdr_size = 0;
842 }
Neil Hormanb2032622012-02-16 01:48:56 +0000843 ctx->copy_size = min(ctx->eth_ip_hdr_size +
844 ctx->l4_hdr_size, skb->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700845 } else {
846 ctx->eth_ip_hdr_size = 0;
847 ctx->l4_hdr_size = 0;
848 /* copy as much as allowed */
849 ctx->copy_size = min((unsigned int)VMXNET3_HDR_COPY_SIZE
850 , skb_headlen(skb));
851 }
852
853 /* make sure headers are accessible directly */
854 if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
855 goto err;
856 }
857
858 if (unlikely(ctx->copy_size > VMXNET3_HDR_COPY_SIZE)) {
859 tq->stats.oversized_hdr++;
860 ctx->copy_size = 0;
861 return 0;
862 }
863
864 tdd = tq->data_ring.base + tq->tx_ring.next2fill;
865
866 memcpy(tdd->data, skb->data, ctx->copy_size);
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000867 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700868 "copy %u bytes to dataRing[%u]\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700869 ctx->copy_size, tq->tx_ring.next2fill);
870 return 1;
871
872err:
873 return -1;
874}
875
876
877static void
878vmxnet3_prepare_tso(struct sk_buff *skb,
879 struct vmxnet3_tx_ctx *ctx)
880{
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000881 struct tcphdr *tcph = tcp_hdr(skb);
882
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700883 if (ctx->ipv4) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000884 struct iphdr *iph = ip_hdr(skb);
885
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700886 iph->check = 0;
887 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
888 IPPROTO_TCP, 0);
889 } else {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000890 struct ipv6hdr *iph = ipv6_hdr(skb);
891
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700892 tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
893 IPPROTO_TCP, 0);
894 }
895}
896
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000897static int txd_estimate(const struct sk_buff *skb)
898{
899 int count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
900 int i;
901
902 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
903 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
904
905 count += VMXNET3_TXD_NEEDED(skb_frag_size(frag));
906 }
907 return count;
908}
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700909
910/*
911 * Transmits a pkt thru a given tq
912 * Returns:
913 * NETDEV_TX_OK: descriptors are setup successfully
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300914 * NETDEV_TX_OK: error occurred, the pkt is dropped
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700915 * NETDEV_TX_BUSY: tx ring is full, queue is stopped
916 *
917 * Side-effects:
918 * 1. tx ring may be changed
919 * 2. tq stats may be updated accordingly
920 * 3. shared->txNumDeferred may be updated
921 */
922
923static int
924vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
925 struct vmxnet3_adapter *adapter, struct net_device *netdev)
926{
927 int ret;
928 u32 count;
929 unsigned long flags;
930 struct vmxnet3_tx_ctx ctx;
931 union Vmxnet3_GenericDesc *gdesc;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000932#ifdef __BIG_ENDIAN_BITFIELD
933 /* Use temporary descriptor to avoid touching bits multiple times */
934 union Vmxnet3_GenericDesc tempTxDesc;
935#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700936
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000937 count = txd_estimate(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700938
Jesse Gross72e85c42011-06-23 13:04:39 +0000939 ctx.ipv4 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IP));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700940
941 ctx.mss = skb_shinfo(skb)->gso_size;
942 if (ctx.mss) {
943 if (skb_header_cloned(skb)) {
944 if (unlikely(pskb_expand_head(skb, 0, 0,
945 GFP_ATOMIC) != 0)) {
946 tq->stats.drop_tso++;
947 goto drop_pkt;
948 }
949 tq->stats.copy_skb_header++;
950 }
951 vmxnet3_prepare_tso(skb, &ctx);
952 } else {
953 if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
954
955 /* non-tso pkts must not use more than
956 * VMXNET3_MAX_TXD_PER_PKT entries
957 */
958 if (skb_linearize(skb) != 0) {
959 tq->stats.drop_too_many_frags++;
960 goto drop_pkt;
961 }
962 tq->stats.linearized++;
963
964 /* recalculate the # of descriptors to use */
965 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
966 }
967 }
968
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000969 spin_lock_irqsave(&tq->tx_lock, flags);
970
971 if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
972 tq->stats.tx_ring_full++;
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000973 netdev_dbg(adapter->netdev,
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000974 "tx queue stopped on %s, next2comp %u"
975 " next2fill %u\n", adapter->netdev->name,
976 tq->tx_ring.next2comp, tq->tx_ring.next2fill);
977
978 vmxnet3_tq_stop(tq, adapter);
979 spin_unlock_irqrestore(&tq->tx_lock, flags);
980 return NETDEV_TX_BUSY;
981 }
982
983
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700984 ret = vmxnet3_parse_and_copy_hdr(skb, tq, &ctx, adapter);
985 if (ret >= 0) {
986 BUG_ON(ret <= 0 && ctx.copy_size != 0);
987 /* hdrs parsed, check against other limits */
988 if (ctx.mss) {
989 if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
990 VMXNET3_MAX_TX_BUF_SIZE)) {
991 goto hdr_too_big;
992 }
993 } else {
994 if (skb->ip_summed == CHECKSUM_PARTIAL) {
995 if (unlikely(ctx.eth_ip_hdr_size +
996 skb->csum_offset >
997 VMXNET3_MAX_CSUM_OFFSET)) {
998 goto hdr_too_big;
999 }
1000 }
1001 }
1002 } else {
1003 tq->stats.drop_hdr_inspect_err++;
Dan Carpenterf955e142010-12-20 03:03:15 +00001004 goto unlock_drop_pkt;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001005 }
1006
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001007 /* fill tx descs related to addr & len */
1008 vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter);
1009
1010 /* setup the EOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001011 ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001012
1013 /* setup the SOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001014#ifdef __BIG_ENDIAN_BITFIELD
1015 gdesc = &tempTxDesc;
1016 gdesc->dword[2] = ctx.sop_txd->dword[2];
1017 gdesc->dword[3] = ctx.sop_txd->dword[3];
1018#else
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001019 gdesc = ctx.sop_txd;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001020#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001021 if (ctx.mss) {
1022 gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
1023 gdesc->txd.om = VMXNET3_OM_TSO;
1024 gdesc->txd.msscof = ctx.mss;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001025 le32_add_cpu(&tq->shared->txNumDeferred, (skb->len -
1026 gdesc->txd.hlen + ctx.mss - 1) / ctx.mss);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001027 } else {
1028 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1029 gdesc->txd.hlen = ctx.eth_ip_hdr_size;
1030 gdesc->txd.om = VMXNET3_OM_CSUM;
1031 gdesc->txd.msscof = ctx.eth_ip_hdr_size +
1032 skb->csum_offset;
1033 } else {
1034 gdesc->txd.om = 0;
1035 gdesc->txd.msscof = 0;
1036 }
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001037 le32_add_cpu(&tq->shared->txNumDeferred, 1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001038 }
1039
1040 if (vlan_tx_tag_present(skb)) {
1041 gdesc->txd.ti = 1;
1042 gdesc->txd.tci = vlan_tx_tag_get(skb);
1043 }
1044
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001045 /* finally flips the GEN bit of the SOP desc. */
1046 gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
1047 VMXNET3_TXD_GEN);
1048#ifdef __BIG_ENDIAN_BITFIELD
1049 /* Finished updating in bitfields of Tx Desc, so write them in original
1050 * place.
1051 */
1052 vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
1053 (struct Vmxnet3_TxDesc *)ctx.sop_txd);
1054 gdesc = ctx.sop_txd;
1055#endif
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00001056 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -07001057 "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
Joe Perchesc2fd03a2012-06-04 12:44:18 +00001058 (u32)(ctx.sop_txd -
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001059 tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
1060 le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001061
1062 spin_unlock_irqrestore(&tq->tx_lock, flags);
1063
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001064 if (le32_to_cpu(tq->shared->txNumDeferred) >=
1065 le32_to_cpu(tq->shared->txThreshold)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001066 tq->shared->txNumDeferred = 0;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001067 VMXNET3_WRITE_BAR0_REG(adapter,
1068 VMXNET3_REG_TXPROD + tq->qid * 8,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001069 tq->tx_ring.next2fill);
1070 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001071
1072 return NETDEV_TX_OK;
1073
1074hdr_too_big:
1075 tq->stats.drop_oversized_hdr++;
Dan Carpenterf955e142010-12-20 03:03:15 +00001076unlock_drop_pkt:
1077 spin_unlock_irqrestore(&tq->tx_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001078drop_pkt:
1079 tq->stats.drop_total++;
1080 dev_kfree_skb(skb);
1081 return NETDEV_TX_OK;
1082}
1083
1084
1085static netdev_tx_t
1086vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1087{
1088 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001089
stephen hemminger96800ee2012-11-13 13:53:28 +00001090 BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
1091 return vmxnet3_tq_xmit(skb,
1092 &adapter->tx_queue[skb->queue_mapping],
1093 adapter, netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001094}
1095
1096
1097static void
1098vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
1099 struct sk_buff *skb,
1100 union Vmxnet3_GenericDesc *gdesc)
1101{
Michał Mirosława0d27302011-04-18 13:31:21 +00001102 if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001103 /* typical case: TCP/UDP over IP and both csums are correct */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001104 if ((le32_to_cpu(gdesc->dword[3]) & VMXNET3_RCD_CSUM_OK) ==
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001105 VMXNET3_RCD_CSUM_OK) {
1106 skb->ip_summed = CHECKSUM_UNNECESSARY;
1107 BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
1108 BUG_ON(!(gdesc->rcd.v4 || gdesc->rcd.v6));
1109 BUG_ON(gdesc->rcd.frg);
1110 } else {
1111 if (gdesc->rcd.csum) {
1112 skb->csum = htons(gdesc->rcd.csum);
1113 skb->ip_summed = CHECKSUM_PARTIAL;
1114 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001115 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001116 }
1117 }
1118 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001119 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001120 }
1121}
1122
1123
1124static void
1125vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
1126 struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
1127{
1128 rq->stats.drop_err++;
1129 if (!rcd->fcs)
1130 rq->stats.drop_fcs++;
1131
1132 rq->stats.drop_total++;
1133
1134 /*
1135 * We do not unmap and chain the rx buffer to the skb.
1136 * We basically pretend this buffer is not used and will be recycled
1137 * by vmxnet3_rq_alloc_rx_buf()
1138 */
1139
1140 /*
1141 * ctx->skb may be NULL if this is the first and the only one
1142 * desc for the pkt
1143 */
1144 if (ctx->skb)
1145 dev_kfree_skb_irq(ctx->skb);
1146
1147 ctx->skb = NULL;
1148}
1149
1150
1151static int
1152vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
1153 struct vmxnet3_adapter *adapter, int quota)
1154{
Joe Perches215faf92010-12-21 02:16:10 -08001155 static const u32 rxprod_reg[2] = {
1156 VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2
1157 };
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001158 u32 num_rxd = 0;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001159 bool skip_page_frags = false;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001160 struct Vmxnet3_RxCompDesc *rcd;
1161 struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001162#ifdef __BIG_ENDIAN_BITFIELD
1163 struct Vmxnet3_RxDesc rxCmdDesc;
1164 struct Vmxnet3_RxCompDesc rxComp;
1165#endif
1166 vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
1167 &rxComp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001168 while (rcd->gen == rq->comp_ring.gen) {
1169 struct vmxnet3_rx_buf_info *rbi;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001170 struct sk_buff *skb, *new_skb = NULL;
1171 struct page *new_page = NULL;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001172 int num_to_alloc;
1173 struct Vmxnet3_RxDesc *rxd;
1174 u32 idx, ring_idx;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001175 struct vmxnet3_cmd_ring *ring = NULL;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001176 if (num_rxd >= quota) {
1177 /* we may stop even before we see the EOP desc of
1178 * the current pkt
1179 */
1180 break;
1181 }
1182 num_rxd++;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001183 BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001184 idx = rcd->rxdIdx;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001185 ring_idx = rcd->rqID < adapter->num_rx_queues ? 0 : 1;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001186 ring = rq->rx_ring + ring_idx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001187 vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
1188 &rxCmdDesc);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001189 rbi = rq->buf_info[ring_idx] + idx;
1190
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001191 BUG_ON(rxd->addr != rbi->dma_addr ||
1192 rxd->len != rbi->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001193
1194 if (unlikely(rcd->eop && rcd->err)) {
1195 vmxnet3_rx_error(rq, rcd, ctx, adapter);
1196 goto rcd_done;
1197 }
1198
1199 if (rcd->sop) { /* first buf of the pkt */
1200 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
1201 rcd->rqID != rq->qid);
1202
1203 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
1204 BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
1205
1206 if (unlikely(rcd->len == 0)) {
1207 /* Pretend the rx buffer is skipped. */
1208 BUG_ON(!(rcd->sop && rcd->eop));
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00001209 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -07001210 "rxRing[%u][%u] 0 length\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001211 ring_idx, idx);
1212 goto rcd_done;
1213 }
1214
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001215 skip_page_frags = false;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001216 ctx->skb = rbi->skb;
Stephen Hemminger0d735f12013-01-15 07:28:26 +00001217 new_skb = netdev_alloc_skb_ip_align(adapter->netdev,
1218 rbi->len);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001219 if (new_skb == NULL) {
1220 /* Skb allocation failed, do not handover this
1221 * skb to stack. Reuse it. Drop the existing pkt
1222 */
1223 rq->stats.rx_buf_alloc_failure++;
1224 ctx->skb = NULL;
1225 rq->stats.drop_total++;
1226 skip_page_frags = true;
1227 goto rcd_done;
1228 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001229
1230 pci_unmap_single(adapter->pdev, rbi->dma_addr, rbi->len,
1231 PCI_DMA_FROMDEVICE);
1232
1233 skb_put(ctx->skb, rcd->len);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001234
1235 /* Immediate refill */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001236 rbi->skb = new_skb;
1237 rbi->dma_addr = pci_map_single(adapter->pdev,
stephen hemminger96800ee2012-11-13 13:53:28 +00001238 rbi->skb->data, rbi->len,
1239 PCI_DMA_FROMDEVICE);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001240 rxd->addr = cpu_to_le64(rbi->dma_addr);
1241 rxd->len = rbi->len;
1242
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001243 } else {
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001244 BUG_ON(ctx->skb == NULL && !skip_page_frags);
1245
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001246 /* non SOP buffer must be type 1 in most cases */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001247 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE);
1248 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001249
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001250 /* If an sop buffer was dropped, skip all
1251 * following non-sop fragments. They will be reused.
1252 */
1253 if (skip_page_frags)
1254 goto rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001255
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001256 new_page = alloc_page(GFP_ATOMIC);
1257 if (unlikely(new_page == NULL)) {
1258 /* Replacement page frag could not be allocated.
1259 * Reuse this page. Drop the pkt and free the
1260 * skb which contained this page as a frag. Skip
1261 * processing all the following non-sop frags.
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001262 */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001263 rq->stats.rx_buf_alloc_failure++;
1264 dev_kfree_skb(ctx->skb);
1265 ctx->skb = NULL;
1266 skip_page_frags = true;
1267 goto rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001268 }
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001269
1270 if (rcd->len) {
1271 pci_unmap_page(adapter->pdev,
1272 rbi->dma_addr, rbi->len,
1273 PCI_DMA_FROMDEVICE);
1274
1275 vmxnet3_append_frag(ctx->skb, rcd, rbi);
1276 }
1277
1278 /* Immediate refill */
1279 rbi->page = new_page;
1280 rbi->dma_addr = pci_map_page(adapter->pdev, rbi->page,
1281 0, PAGE_SIZE,
1282 PCI_DMA_FROMDEVICE);
1283 rxd->addr = cpu_to_le64(rbi->dma_addr);
1284 rxd->len = rbi->len;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001285 }
1286
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001287
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001288 skb = ctx->skb;
1289 if (rcd->eop) {
1290 skb->len += skb->data_len;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001291
1292 vmxnet3_rx_csum(adapter, skb,
1293 (union Vmxnet3_GenericDesc *)rcd);
1294 skb->protocol = eth_type_trans(skb, adapter->netdev);
1295
Jesse Gross72e85c42011-06-23 13:04:39 +00001296 if (unlikely(rcd->ts))
1297 __vlan_hwaccel_put_tag(skb, rcd->tci);
1298
Jesse Gross213ade82011-06-24 14:24:35 +00001299 if (adapter->netdev->features & NETIF_F_LRO)
1300 netif_receive_skb(skb);
1301 else
1302 napi_gro_receive(&rq->napi, skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001303
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001304 ctx->skb = NULL;
1305 }
1306
1307rcd_done:
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001308 /* device may have skipped some rx descs */
1309 ring->next2comp = idx;
1310 num_to_alloc = vmxnet3_cmd_ring_desc_avail(ring);
1311 ring = rq->rx_ring + ring_idx;
1312 while (num_to_alloc) {
1313 vmxnet3_getRxDesc(rxd, &ring->base[ring->next2fill].rxd,
1314 &rxCmdDesc);
1315 BUG_ON(!rxd->addr);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001316
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001317 /* Recv desc is ready to be used by the device */
1318 rxd->gen = ring->gen;
1319 vmxnet3_cmd_ring_adv_next2fill(ring);
1320 num_to_alloc--;
1321 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001322
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001323 /* if needed, update the register */
1324 if (unlikely(rq->shared->updateRxProd)) {
1325 VMXNET3_WRITE_BAR0_REG(adapter,
stephen hemminger96800ee2012-11-13 13:53:28 +00001326 rxprod_reg[ring_idx] + rq->qid * 8,
1327 ring->next2fill);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001328 }
1329
1330 vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001331 vmxnet3_getRxComp(rcd,
stephen hemminger96800ee2012-11-13 13:53:28 +00001332 &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001333 }
1334
1335 return num_rxd;
1336}
1337
1338
1339static void
1340vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
1341 struct vmxnet3_adapter *adapter)
1342{
1343 u32 i, ring_idx;
1344 struct Vmxnet3_RxDesc *rxd;
1345
1346 for (ring_idx = 0; ring_idx < 2; ring_idx++) {
1347 for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001348#ifdef __BIG_ENDIAN_BITFIELD
1349 struct Vmxnet3_RxDesc rxDesc;
1350#endif
1351 vmxnet3_getRxDesc(rxd,
1352 &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001353
1354 if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
1355 rq->buf_info[ring_idx][i].skb) {
1356 pci_unmap_single(adapter->pdev, rxd->addr,
1357 rxd->len, PCI_DMA_FROMDEVICE);
1358 dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
1359 rq->buf_info[ring_idx][i].skb = NULL;
1360 } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
1361 rq->buf_info[ring_idx][i].page) {
1362 pci_unmap_page(adapter->pdev, rxd->addr,
1363 rxd->len, PCI_DMA_FROMDEVICE);
1364 put_page(rq->buf_info[ring_idx][i].page);
1365 rq->buf_info[ring_idx][i].page = NULL;
1366 }
1367 }
1368
1369 rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
1370 rq->rx_ring[ring_idx].next2fill =
1371 rq->rx_ring[ring_idx].next2comp = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001372 }
1373
1374 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1375 rq->comp_ring.next2proc = 0;
1376}
1377
1378
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001379static void
1380vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
1381{
1382 int i;
1383
1384 for (i = 0; i < adapter->num_rx_queues; i++)
1385 vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
1386}
1387
1388
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001389void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
1390 struct vmxnet3_adapter *adapter)
1391{
1392 int i;
1393 int j;
1394
1395 /* all rx buffers must have already been freed */
1396 for (i = 0; i < 2; i++) {
1397 if (rq->buf_info[i]) {
1398 for (j = 0; j < rq->rx_ring[i].size; j++)
1399 BUG_ON(rq->buf_info[i][j].page != NULL);
1400 }
1401 }
1402
1403
1404 kfree(rq->buf_info[0]);
1405
1406 for (i = 0; i < 2; i++) {
1407 if (rq->rx_ring[i].base) {
1408 pci_free_consistent(adapter->pdev, rq->rx_ring[i].size
1409 * sizeof(struct Vmxnet3_RxDesc),
1410 rq->rx_ring[i].base,
1411 rq->rx_ring[i].basePA);
1412 rq->rx_ring[i].base = NULL;
1413 }
1414 rq->buf_info[i] = NULL;
1415 }
1416
1417 if (rq->comp_ring.base) {
1418 pci_free_consistent(adapter->pdev, rq->comp_ring.size *
1419 sizeof(struct Vmxnet3_RxCompDesc),
1420 rq->comp_ring.base, rq->comp_ring.basePA);
1421 rq->comp_ring.base = NULL;
1422 }
1423}
1424
1425
1426static int
1427vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
1428 struct vmxnet3_adapter *adapter)
1429{
1430 int i;
1431
1432 /* initialize buf_info */
1433 for (i = 0; i < rq->rx_ring[0].size; i++) {
1434
1435 /* 1st buf for a pkt is skbuff */
1436 if (i % adapter->rx_buf_per_pkt == 0) {
1437 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
1438 rq->buf_info[0][i].len = adapter->skb_buf_size;
1439 } else { /* subsequent bufs for a pkt is frag */
1440 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
1441 rq->buf_info[0][i].len = PAGE_SIZE;
1442 }
1443 }
1444 for (i = 0; i < rq->rx_ring[1].size; i++) {
1445 rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
1446 rq->buf_info[1][i].len = PAGE_SIZE;
1447 }
1448
1449 /* reset internal state and allocate buffers for both rings */
1450 for (i = 0; i < 2; i++) {
1451 rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001452
1453 memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
1454 sizeof(struct Vmxnet3_RxDesc));
1455 rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
1456 }
1457 if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
1458 adapter) == 0) {
1459 /* at least has 1 rx buffer for the 1st ring */
1460 return -ENOMEM;
1461 }
1462 vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
1463
1464 /* reset the comp ring */
1465 rq->comp_ring.next2proc = 0;
1466 memset(rq->comp_ring.base, 0, rq->comp_ring.size *
1467 sizeof(struct Vmxnet3_RxCompDesc));
1468 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1469
1470 /* reset rxctx */
1471 rq->rx_ctx.skb = NULL;
1472
1473 /* stats are not reset */
1474 return 0;
1475}
1476
1477
1478static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001479vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
1480{
1481 int i, err = 0;
1482
1483 for (i = 0; i < adapter->num_rx_queues; i++) {
1484 err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
1485 if (unlikely(err)) {
1486 dev_err(&adapter->netdev->dev, "%s: failed to "
1487 "initialize rx queue%i\n",
1488 adapter->netdev->name, i);
1489 break;
1490 }
1491 }
1492 return err;
1493
1494}
1495
1496
1497static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001498vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
1499{
1500 int i;
1501 size_t sz;
1502 struct vmxnet3_rx_buf_info *bi;
1503
1504 for (i = 0; i < 2; i++) {
1505
1506 sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
1507 rq->rx_ring[i].base = pci_alloc_consistent(adapter->pdev, sz,
1508 &rq->rx_ring[i].basePA);
1509 if (!rq->rx_ring[i].base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001510 netdev_err(adapter->netdev,
1511 "failed to allocate rx ring %d\n", i);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001512 goto err;
1513 }
1514 }
1515
1516 sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
1517 rq->comp_ring.base = pci_alloc_consistent(adapter->pdev, sz,
1518 &rq->comp_ring.basePA);
1519 if (!rq->comp_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001520 netdev_err(adapter->netdev, "failed to allocate rx comp ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001521 goto err;
1522 }
1523
1524 sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
1525 rq->rx_ring[1].size);
Julia Lawall476c6092010-05-13 10:05:40 +00001526 bi = kzalloc(sz, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001527 if (!bi)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001528 goto err;
Joe Perchese404dec2012-01-29 12:56:23 +00001529
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001530 rq->buf_info[0] = bi;
1531 rq->buf_info[1] = bi + rq->rx_ring[0].size;
1532
1533 return 0;
1534
1535err:
1536 vmxnet3_rq_destroy(rq, adapter);
1537 return -ENOMEM;
1538}
1539
1540
1541static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001542vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
1543{
1544 int i, err = 0;
1545
1546 for (i = 0; i < adapter->num_rx_queues; i++) {
1547 err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
1548 if (unlikely(err)) {
1549 dev_err(&adapter->netdev->dev,
1550 "%s: failed to create rx queue%i\n",
1551 adapter->netdev->name, i);
1552 goto err_out;
1553 }
1554 }
1555 return err;
1556err_out:
1557 vmxnet3_rq_destroy_all(adapter);
1558 return err;
1559
1560}
1561
1562/* Multiple queue aware polling function for tx and rx */
1563
1564static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001565vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
1566{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001567 int rcd_done = 0, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001568 if (unlikely(adapter->shared->ecr))
1569 vmxnet3_process_events(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001570 for (i = 0; i < adapter->num_tx_queues; i++)
1571 vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001572
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001573 for (i = 0; i < adapter->num_rx_queues; i++)
1574 rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
1575 adapter, budget);
1576 return rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001577}
1578
1579
1580static int
1581vmxnet3_poll(struct napi_struct *napi, int budget)
1582{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001583 struct vmxnet3_rx_queue *rx_queue = container_of(napi,
1584 struct vmxnet3_rx_queue, napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001585 int rxd_done;
1586
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001587 rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001588
1589 if (rxd_done < budget) {
1590 napi_complete(napi);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001591 vmxnet3_enable_all_intrs(rx_queue->adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001592 }
1593 return rxd_done;
1594}
1595
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001596/*
1597 * NAPI polling function for MSI-X mode with multiple Rx queues
1598 * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
1599 */
1600
1601static int
1602vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
1603{
1604 struct vmxnet3_rx_queue *rq = container_of(napi,
1605 struct vmxnet3_rx_queue, napi);
1606 struct vmxnet3_adapter *adapter = rq->adapter;
1607 int rxd_done;
1608
1609 /* When sharing interrupt with corresponding tx queue, process
1610 * tx completions in that queue as well
1611 */
1612 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
1613 struct vmxnet3_tx_queue *tq =
1614 &adapter->tx_queue[rq - adapter->rx_queue];
1615 vmxnet3_tq_tx_complete(tq, adapter);
1616 }
1617
1618 rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
1619
1620 if (rxd_done < budget) {
1621 napi_complete(napi);
1622 vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
1623 }
1624 return rxd_done;
1625}
1626
1627
1628#ifdef CONFIG_PCI_MSI
1629
1630/*
1631 * Handle completion interrupts on tx queues
1632 * Returns whether or not the intr is handled
1633 */
1634
1635static irqreturn_t
1636vmxnet3_msix_tx(int irq, void *data)
1637{
1638 struct vmxnet3_tx_queue *tq = data;
1639 struct vmxnet3_adapter *adapter = tq->adapter;
1640
1641 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1642 vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
1643
1644 /* Handle the case where only one irq is allocate for all tx queues */
1645 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1646 int i;
1647 for (i = 0; i < adapter->num_tx_queues; i++) {
1648 struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
1649 vmxnet3_tq_tx_complete(txq, adapter);
1650 }
1651 } else {
1652 vmxnet3_tq_tx_complete(tq, adapter);
1653 }
1654 vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
1655
1656 return IRQ_HANDLED;
1657}
1658
1659
1660/*
1661 * Handle completion interrupts on rx queues. Returns whether or not the
1662 * intr is handled
1663 */
1664
1665static irqreturn_t
1666vmxnet3_msix_rx(int irq, void *data)
1667{
1668 struct vmxnet3_rx_queue *rq = data;
1669 struct vmxnet3_adapter *adapter = rq->adapter;
1670
1671 /* disable intr if needed */
1672 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1673 vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
1674 napi_schedule(&rq->napi);
1675
1676 return IRQ_HANDLED;
1677}
1678
1679/*
1680 *----------------------------------------------------------------------------
1681 *
1682 * vmxnet3_msix_event --
1683 *
1684 * vmxnet3 msix event intr handler
1685 *
1686 * Result:
1687 * whether or not the intr is handled
1688 *
1689 *----------------------------------------------------------------------------
1690 */
1691
1692static irqreturn_t
1693vmxnet3_msix_event(int irq, void *data)
1694{
1695 struct net_device *dev = data;
1696 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1697
1698 /* disable intr if needed */
1699 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1700 vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
1701
1702 if (adapter->shared->ecr)
1703 vmxnet3_process_events(adapter);
1704
1705 vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
1706
1707 return IRQ_HANDLED;
1708}
1709
1710#endif /* CONFIG_PCI_MSI */
1711
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001712
1713/* Interrupt handler for vmxnet3 */
1714static irqreturn_t
1715vmxnet3_intr(int irq, void *dev_id)
1716{
1717 struct net_device *dev = dev_id;
1718 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1719
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001720 if (adapter->intr.type == VMXNET3_IT_INTX) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001721 u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
1722 if (unlikely(icr == 0))
1723 /* not ours */
1724 return IRQ_NONE;
1725 }
1726
1727
1728 /* disable intr if needed */
1729 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001730 vmxnet3_disable_all_intrs(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001731
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001732 napi_schedule(&adapter->rx_queue[0].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001733
1734 return IRQ_HANDLED;
1735}
1736
1737#ifdef CONFIG_NET_POLL_CONTROLLER
1738
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001739/* netpoll callback. */
1740static void
1741vmxnet3_netpoll(struct net_device *netdev)
1742{
1743 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001744
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001745 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1746 vmxnet3_disable_all_intrs(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001747
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001748 vmxnet3_do_poll(adapter, adapter->rx_queue[0].rx_ring[0].size);
1749 vmxnet3_enable_all_intrs(adapter);
1750
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001751}
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001752#endif /* CONFIG_NET_POLL_CONTROLLER */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001753
1754static int
1755vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
1756{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001757 struct vmxnet3_intr *intr = &adapter->intr;
1758 int err = 0, i;
1759 int vector = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001760
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001761#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001762 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001763 for (i = 0; i < adapter->num_tx_queues; i++) {
1764 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1765 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
1766 adapter->netdev->name, vector);
1767 err = request_irq(
1768 intr->msix_entries[vector].vector,
1769 vmxnet3_msix_tx, 0,
1770 adapter->tx_queue[i].name,
1771 &adapter->tx_queue[i]);
1772 } else {
1773 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
1774 adapter->netdev->name, vector);
1775 }
1776 if (err) {
1777 dev_err(&adapter->netdev->dev,
1778 "Failed to request irq for MSIX, %s, "
1779 "error %d\n",
1780 adapter->tx_queue[i].name, err);
1781 return err;
1782 }
1783
1784 /* Handle the case where only 1 MSIx was allocated for
1785 * all tx queues */
1786 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1787 for (; i < adapter->num_tx_queues; i++)
1788 adapter->tx_queue[i].comp_ring.intr_idx
1789 = vector;
1790 vector++;
1791 break;
1792 } else {
1793 adapter->tx_queue[i].comp_ring.intr_idx
1794 = vector++;
1795 }
1796 }
1797 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
1798 vector = 0;
1799
1800 for (i = 0; i < adapter->num_rx_queues; i++) {
1801 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
1802 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
1803 adapter->netdev->name, vector);
1804 else
1805 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
1806 adapter->netdev->name, vector);
1807 err = request_irq(intr->msix_entries[vector].vector,
1808 vmxnet3_msix_rx, 0,
1809 adapter->rx_queue[i].name,
1810 &(adapter->rx_queue[i]));
1811 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001812 netdev_err(adapter->netdev,
1813 "Failed to request irq for MSIX, "
1814 "%s, error %d\n",
1815 adapter->rx_queue[i].name, err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001816 return err;
1817 }
1818
1819 adapter->rx_queue[i].comp_ring.intr_idx = vector++;
1820 }
1821
1822 sprintf(intr->event_msi_vector_name, "%s-event-%d",
1823 adapter->netdev->name, vector);
1824 err = request_irq(intr->msix_entries[vector].vector,
1825 vmxnet3_msix_event, 0,
1826 intr->event_msi_vector_name, adapter->netdev);
1827 intr->event_intr_idx = vector;
1828
1829 } else if (intr->type == VMXNET3_IT_MSI) {
1830 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001831 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
1832 adapter->netdev->name, adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001833 } else {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001834#endif
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001835 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001836 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
1837 IRQF_SHARED, adapter->netdev->name,
1838 adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001839#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001840 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001841#endif
1842 intr->num_intrs = vector + 1;
1843 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001844 netdev_err(adapter->netdev,
1845 "Failed to request irq (intr type:%d), error %d\n",
1846 intr->type, err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001847 } else {
1848 /* Number of rx queues will not change after this */
1849 for (i = 0; i < adapter->num_rx_queues; i++) {
1850 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
1851 rq->qid = i;
1852 rq->qid2 = i + adapter->num_rx_queues;
1853 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001854
1855
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001856
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001857 /* init our intr settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001858 for (i = 0; i < intr->num_intrs; i++)
1859 intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
1860 if (adapter->intr.type != VMXNET3_IT_MSIX) {
1861 adapter->intr.event_intr_idx = 0;
1862 for (i = 0; i < adapter->num_tx_queues; i++)
1863 adapter->tx_queue[i].comp_ring.intr_idx = 0;
1864 adapter->rx_queue[0].comp_ring.intr_idx = 0;
1865 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001866
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001867 netdev_info(adapter->netdev,
1868 "intr type %u, mode %u, %u vectors allocated\n",
1869 intr->type, intr->mask_mode, intr->num_intrs);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001870 }
1871
1872 return err;
1873}
1874
1875
1876static void
1877vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
1878{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001879 struct vmxnet3_intr *intr = &adapter->intr;
1880 BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001881
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001882 switch (intr->type) {
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001883#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001884 case VMXNET3_IT_MSIX:
1885 {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001886 int i, vector = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001887
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001888 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1889 for (i = 0; i < adapter->num_tx_queues; i++) {
1890 free_irq(intr->msix_entries[vector++].vector,
1891 &(adapter->tx_queue[i]));
1892 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
1893 break;
1894 }
1895 }
1896
1897 for (i = 0; i < adapter->num_rx_queues; i++) {
1898 free_irq(intr->msix_entries[vector++].vector,
1899 &(adapter->rx_queue[i]));
1900 }
1901
1902 free_irq(intr->msix_entries[vector].vector,
1903 adapter->netdev);
1904 BUG_ON(vector >= intr->num_intrs);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001905 break;
1906 }
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001907#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001908 case VMXNET3_IT_MSI:
1909 free_irq(adapter->pdev->irq, adapter->netdev);
1910 break;
1911 case VMXNET3_IT_INTX:
1912 free_irq(adapter->pdev->irq, adapter->netdev);
1913 break;
1914 default:
Sasha Levinc068e772012-11-08 10:23:03 +00001915 BUG();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001916 }
1917}
1918
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001919
1920static void
1921vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
1922{
Jesse Gross72e85c42011-06-23 13:04:39 +00001923 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1924 u16 vid;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001925
Jesse Gross72e85c42011-06-23 13:04:39 +00001926 /* allow untagged pkts */
1927 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1928
1929 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1930 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001931}
1932
1933
Jiri Pirko8e586132011-12-08 19:52:37 -05001934static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001935vmxnet3_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1936{
1937 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001938
Jesse Grossf6957f82011-08-07 23:15:47 +00001939 if (!(netdev->flags & IFF_PROMISC)) {
1940 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1941 unsigned long flags;
1942
1943 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1944 spin_lock_irqsave(&adapter->cmd_lock, flags);
1945 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1946 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1947 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1948 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001949
1950 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001951
1952 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001953}
1954
1955
Jiri Pirko8e586132011-12-08 19:52:37 -05001956static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001957vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1958{
1959 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001960
Jesse Grossf6957f82011-08-07 23:15:47 +00001961 if (!(netdev->flags & IFF_PROMISC)) {
1962 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1963 unsigned long flags;
1964
1965 VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
1966 spin_lock_irqsave(&adapter->cmd_lock, flags);
1967 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1968 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1969 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1970 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001971
1972 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001973
1974 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001975}
1976
1977
1978static u8 *
1979vmxnet3_copy_mc(struct net_device *netdev)
1980{
1981 u8 *buf = NULL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001982 u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001983
1984 /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
1985 if (sz <= 0xffff) {
1986 /* We may be called with BH disabled */
1987 buf = kmalloc(sz, GFP_ATOMIC);
1988 if (buf) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001989 struct netdev_hw_addr *ha;
Jiri Pirko567ec872010-02-23 23:17:07 +00001990 int i = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001991
Jiri Pirko22bedad32010-04-01 21:22:57 +00001992 netdev_for_each_mc_addr(ha, netdev)
1993 memcpy(buf + i++ * ETH_ALEN, ha->addr,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001994 ETH_ALEN);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001995 }
1996 }
1997 return buf;
1998}
1999
2000
2001static void
2002vmxnet3_set_mc(struct net_device *netdev)
2003{
2004 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002005 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002006 struct Vmxnet3_RxFilterConf *rxConf =
2007 &adapter->shared->devRead.rxFilterConf;
2008 u8 *new_table = NULL;
2009 u32 new_mode = VMXNET3_RXM_UCAST;
2010
Jesse Gross72e85c42011-06-23 13:04:39 +00002011 if (netdev->flags & IFF_PROMISC) {
2012 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2013 memset(vfTable, 0, VMXNET3_VFT_SIZE * sizeof(*vfTable));
2014
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002015 new_mode |= VMXNET3_RXM_PROMISC;
Jesse Gross72e85c42011-06-23 13:04:39 +00002016 } else {
2017 vmxnet3_restore_vlan(adapter);
2018 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002019
2020 if (netdev->flags & IFF_BROADCAST)
2021 new_mode |= VMXNET3_RXM_BCAST;
2022
2023 if (netdev->flags & IFF_ALLMULTI)
2024 new_mode |= VMXNET3_RXM_ALL_MULTI;
2025 else
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002026 if (!netdev_mc_empty(netdev)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002027 new_table = vmxnet3_copy_mc(netdev);
2028 if (new_table) {
2029 new_mode |= VMXNET3_RXM_MCAST;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002030 rxConf->mfTableLen = cpu_to_le16(
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002031 netdev_mc_count(netdev) * ETH_ALEN);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002032 rxConf->mfTablePA = cpu_to_le64(virt_to_phys(
2033 new_table));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002034 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002035 netdev_info(netdev, "failed to copy mcast list"
2036 ", setting ALL_MULTI\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002037 new_mode |= VMXNET3_RXM_ALL_MULTI;
2038 }
2039 }
2040
2041
2042 if (!(new_mode & VMXNET3_RXM_MCAST)) {
2043 rxConf->mfTableLen = 0;
2044 rxConf->mfTablePA = 0;
2045 }
2046
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002047 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002048 if (new_mode != rxConf->rxMode) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002049 rxConf->rxMode = cpu_to_le32(new_mode);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002050 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2051 VMXNET3_CMD_UPDATE_RX_MODE);
Jesse Gross72e85c42011-06-23 13:04:39 +00002052 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2053 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002054 }
2055
2056 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2057 VMXNET3_CMD_UPDATE_MAC_FILTERS);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002058 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002059
2060 kfree(new_table);
2061}
2062
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002063void
2064vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
2065{
2066 int i;
2067
2068 for (i = 0; i < adapter->num_rx_queues; i++)
2069 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2070}
2071
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002072
2073/*
2074 * Set up driver_shared based on settings in adapter.
2075 */
2076
2077static void
2078vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2079{
2080 struct Vmxnet3_DriverShared *shared = adapter->shared;
2081 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2082 struct Vmxnet3_TxQueueConf *tqc;
2083 struct Vmxnet3_RxQueueConf *rqc;
2084 int i;
2085
2086 memset(shared, 0, sizeof(*shared));
2087
2088 /* driver settings */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002089 shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2090 devRead->misc.driverInfo.version = cpu_to_le32(
2091 VMXNET3_DRIVER_VERSION_NUM);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002092 devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2093 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2094 devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002095 *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2096 *((u32 *)&devRead->misc.driverInfo.gos));
2097 devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2098 devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002099
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002100 devRead->misc.ddPA = cpu_to_le64(virt_to_phys(adapter));
2101 devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002102
2103 /* set up feature flags */
Michał Mirosława0d27302011-04-18 13:31:21 +00002104 if (adapter->netdev->features & NETIF_F_RXCSUM)
Harvey Harrison3843e512010-10-21 18:05:32 +00002105 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002106
Michał Mirosława0d27302011-04-18 13:31:21 +00002107 if (adapter->netdev->features & NETIF_F_LRO) {
Harvey Harrison3843e512010-10-21 18:05:32 +00002108 devRead->misc.uptFeatures |= UPT1_F_LRO;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002109 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002110 }
Shreyas Bhatewara54da3d02011-01-14 14:59:36 +00002111 if (adapter->netdev->features & NETIF_F_HW_VLAN_RX)
Harvey Harrison3843e512010-10-21 18:05:32 +00002112 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002113
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002114 devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2115 devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2116 devRead->misc.queueDescLen = cpu_to_le32(
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002117 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2118 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002119
2120 /* tx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002121 devRead->misc.numTxQueues = adapter->num_tx_queues;
2122 for (i = 0; i < adapter->num_tx_queues; i++) {
2123 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2124 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2125 tqc = &adapter->tqd_start[i].conf;
2126 tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
2127 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2128 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
2129 tqc->ddPA = cpu_to_le64(virt_to_phys(tq->buf_info));
2130 tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
2131 tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
2132 tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
2133 tqc->ddLen = cpu_to_le32(
2134 sizeof(struct vmxnet3_tx_buf_info) *
2135 tqc->txRingSize);
2136 tqc->intrIdx = tq->comp_ring.intr_idx;
2137 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002138
2139 /* rx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002140 devRead->misc.numRxQueues = adapter->num_rx_queues;
2141 for (i = 0; i < adapter->num_rx_queues; i++) {
2142 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2143 rqc = &adapter->rqd_start[i].conf;
2144 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2145 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2146 rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
2147 rqc->ddPA = cpu_to_le64(virt_to_phys(
2148 rq->buf_info));
2149 rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
2150 rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
2151 rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
2152 rqc->ddLen = cpu_to_le32(
2153 sizeof(struct vmxnet3_rx_buf_info) *
2154 (rqc->rxRingSize[0] +
2155 rqc->rxRingSize[1]));
2156 rqc->intrIdx = rq->comp_ring.intr_idx;
2157 }
2158
2159#ifdef VMXNET3_RSS
2160 memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2161
2162 if (adapter->rss) {
2163 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
2164 devRead->misc.uptFeatures |= UPT1_F_RSS;
2165 devRead->misc.numRxQueues = adapter->num_rx_queues;
2166 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2167 UPT1_RSS_HASH_TYPE_IPV4 |
2168 UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2169 UPT1_RSS_HASH_TYPE_IPV6;
2170 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2171 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2172 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
2173 get_random_bytes(&rssConf->hashKey[0], rssConf->hashKeySize);
2174 for (i = 0; i < rssConf->indTableSize; i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00002175 rssConf->indTable[i] = ethtool_rxfh_indir_default(
2176 i, adapter->num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002177
2178 devRead->rssConfDesc.confVer = 1;
2179 devRead->rssConfDesc.confLen = sizeof(*rssConf);
2180 devRead->rssConfDesc.confPA = virt_to_phys(rssConf);
2181 }
2182
2183#endif /* VMXNET3_RSS */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002184
2185 /* intr settings */
2186 devRead->intrConf.autoMask = adapter->intr.mask_mode ==
2187 VMXNET3_IMM_AUTO;
2188 devRead->intrConf.numIntrs = adapter->intr.num_intrs;
2189 for (i = 0; i < adapter->intr.num_intrs; i++)
2190 devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
2191
2192 devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
Ronghua Zang6929fe82010-07-15 22:18:47 -07002193 devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002194
2195 /* rx filter settings */
2196 devRead->rxFilterConf.rxMode = 0;
2197 vmxnet3_restore_vlan(adapter);
Shreyas Bhatewaraf9f25022011-01-14 14:59:31 +00002198 vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
2199
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002200 /* the rest are already zeroed */
2201}
2202
2203
2204int
2205vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
2206{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002207 int err, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002208 u32 ret;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002209 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002210
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00002211 netdev_dbg(adapter->netdev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002212 " ring sizes %u %u %u\n", adapter->netdev->name,
2213 adapter->skb_buf_size, adapter->rx_buf_per_pkt,
2214 adapter->tx_queue[0].tx_ring.size,
2215 adapter->rx_queue[0].rx_ring[0].size,
2216 adapter->rx_queue[0].rx_ring[1].size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002217
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002218 vmxnet3_tq_init_all(adapter);
2219 err = vmxnet3_rq_init_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002220 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002221 netdev_err(adapter->netdev,
2222 "Failed to init rx queue error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002223 goto rq_err;
2224 }
2225
2226 err = vmxnet3_request_irqs(adapter);
2227 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002228 netdev_err(adapter->netdev,
2229 "Failed to setup irq for error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002230 goto irq_err;
2231 }
2232
2233 vmxnet3_setup_driver_shared(adapter);
2234
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002235 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
2236 adapter->shared_pa));
2237 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
2238 adapter->shared_pa));
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002239 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002240 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2241 VMXNET3_CMD_ACTIVATE_DEV);
2242 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002243 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002244
2245 if (ret != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002246 netdev_err(adapter->netdev,
2247 "Failed to activate dev: error %u\n", ret);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002248 err = -EINVAL;
2249 goto activate_err;
2250 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002251
2252 for (i = 0; i < adapter->num_rx_queues; i++) {
2253 VMXNET3_WRITE_BAR0_REG(adapter,
2254 VMXNET3_REG_RXPROD + i * VMXNET3_REG_ALIGN,
2255 adapter->rx_queue[i].rx_ring[0].next2fill);
2256 VMXNET3_WRITE_BAR0_REG(adapter, (VMXNET3_REG_RXPROD2 +
2257 (i * VMXNET3_REG_ALIGN)),
2258 adapter->rx_queue[i].rx_ring[1].next2fill);
2259 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002260
2261 /* Apply the rx filter settins last. */
2262 vmxnet3_set_mc(adapter->netdev);
2263
2264 /*
2265 * Check link state when first activating device. It will start the
2266 * tx queue if the link is up.
2267 */
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00002268 vmxnet3_check_link(adapter, true);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002269 for (i = 0; i < adapter->num_rx_queues; i++)
2270 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002271 vmxnet3_enable_all_intrs(adapter);
2272 clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
2273 return 0;
2274
2275activate_err:
2276 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
2277 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
2278 vmxnet3_free_irqs(adapter);
2279irq_err:
2280rq_err:
2281 /* free up buffers we allocated */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002282 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002283 return err;
2284}
2285
2286
2287void
2288vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
2289{
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002290 unsigned long flags;
2291 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002292 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002293 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002294}
2295
2296
2297int
2298vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
2299{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002300 int i;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002301 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002302 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
2303 return 0;
2304
2305
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002306 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002307 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2308 VMXNET3_CMD_QUIESCE_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002309 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002310 vmxnet3_disable_all_intrs(adapter);
2311
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002312 for (i = 0; i < adapter->num_rx_queues; i++)
2313 napi_disable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002314 netif_tx_disable(adapter->netdev);
2315 adapter->link_speed = 0;
2316 netif_carrier_off(adapter->netdev);
2317
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002318 vmxnet3_tq_cleanup_all(adapter);
2319 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002320 vmxnet3_free_irqs(adapter);
2321 return 0;
2322}
2323
2324
2325static void
2326vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2327{
2328 u32 tmp;
2329
2330 tmp = *(u32 *)mac;
2331 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
2332
2333 tmp = (mac[5] << 8) | mac[4];
2334 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
2335}
2336
2337
2338static int
2339vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
2340{
2341 struct sockaddr *addr = p;
2342 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2343
2344 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2345 vmxnet3_write_mac_addr(adapter, addr->sa_data);
2346
2347 return 0;
2348}
2349
2350
2351/* ==================== initialization and cleanup routines ============ */
2352
2353static int
2354vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
2355{
2356 int err;
2357 unsigned long mmio_start, mmio_len;
2358 struct pci_dev *pdev = adapter->pdev;
2359
2360 err = pci_enable_device(pdev);
2361 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002362 dev_err(&pdev->dev, "Failed to enable adapter: error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002363 return err;
2364 }
2365
2366 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
2367 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002368 dev_err(&pdev->dev,
2369 "pci_set_consistent_dma_mask failed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002370 err = -EIO;
2371 goto err_set_mask;
2372 }
2373 *dma64 = true;
2374 } else {
2375 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002376 dev_err(&pdev->dev,
2377 "pci_set_dma_mask failed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002378 err = -EIO;
2379 goto err_set_mask;
2380 }
2381 *dma64 = false;
2382 }
2383
2384 err = pci_request_selected_regions(pdev, (1 << 2) - 1,
2385 vmxnet3_driver_name);
2386 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002387 dev_err(&pdev->dev,
2388 "Failed to request region for adapter: error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002389 goto err_set_mask;
2390 }
2391
2392 pci_set_master(pdev);
2393
2394 mmio_start = pci_resource_start(pdev, 0);
2395 mmio_len = pci_resource_len(pdev, 0);
2396 adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
2397 if (!adapter->hw_addr0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002398 dev_err(&pdev->dev, "Failed to map bar0\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002399 err = -EIO;
2400 goto err_ioremap;
2401 }
2402
2403 mmio_start = pci_resource_start(pdev, 1);
2404 mmio_len = pci_resource_len(pdev, 1);
2405 adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
2406 if (!adapter->hw_addr1) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002407 dev_err(&pdev->dev, "Failed to map bar1\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002408 err = -EIO;
2409 goto err_bar1;
2410 }
2411 return 0;
2412
2413err_bar1:
2414 iounmap(adapter->hw_addr0);
2415err_ioremap:
2416 pci_release_selected_regions(pdev, (1 << 2) - 1);
2417err_set_mask:
2418 pci_disable_device(pdev);
2419 return err;
2420}
2421
2422
2423static void
2424vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
2425{
2426 BUG_ON(!adapter->pdev);
2427
2428 iounmap(adapter->hw_addr0);
2429 iounmap(adapter->hw_addr1);
2430 pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
2431 pci_disable_device(adapter->pdev);
2432}
2433
2434
2435static void
2436vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
2437{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002438 size_t sz, i, ring0_size, ring1_size, comp_size;
2439 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[0];
2440
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002441
2442 if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
2443 VMXNET3_MAX_ETH_HDR_SIZE) {
2444 adapter->skb_buf_size = adapter->netdev->mtu +
2445 VMXNET3_MAX_ETH_HDR_SIZE;
2446 if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
2447 adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
2448
2449 adapter->rx_buf_per_pkt = 1;
2450 } else {
2451 adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
2452 sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
2453 VMXNET3_MAX_ETH_HDR_SIZE;
2454 adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
2455 }
2456
2457 /*
2458 * for simplicity, force the ring0 size to be a multiple of
2459 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
2460 */
2461 sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002462 ring0_size = adapter->rx_queue[0].rx_ring[0].size;
2463 ring0_size = (ring0_size + sz - 1) / sz * sz;
Shreyas Bhatewaraa53255d2011-01-14 14:59:25 +00002464 ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002465 sz * sz);
2466 ring1_size = adapter->rx_queue[0].rx_ring[1].size;
2467 comp_size = ring0_size + ring1_size;
2468
2469 for (i = 0; i < adapter->num_rx_queues; i++) {
2470 rq = &adapter->rx_queue[i];
2471 rq->rx_ring[0].size = ring0_size;
2472 rq->rx_ring[1].size = ring1_size;
2473 rq->comp_ring.size = comp_size;
2474 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002475}
2476
2477
2478int
2479vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
2480 u32 rx_ring_size, u32 rx_ring2_size)
2481{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002482 int err = 0, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002483
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002484 for (i = 0; i < adapter->num_tx_queues; i++) {
2485 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2486 tq->tx_ring.size = tx_ring_size;
2487 tq->data_ring.size = tx_ring_size;
2488 tq->comp_ring.size = tx_ring_size;
2489 tq->shared = &adapter->tqd_start[i].ctrl;
2490 tq->stopped = true;
2491 tq->adapter = adapter;
2492 tq->qid = i;
2493 err = vmxnet3_tq_create(tq, adapter);
2494 /*
2495 * Too late to change num_tx_queues. We cannot do away with
2496 * lesser number of queues than what we asked for
2497 */
2498 if (err)
2499 goto queue_err;
2500 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002501
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002502 adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
2503 adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002504 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002505 for (i = 0; i < adapter->num_rx_queues; i++) {
2506 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2507 /* qid and qid2 for rx queues will be assigned later when num
2508 * of rx queues is finalized after allocating intrs */
2509 rq->shared = &adapter->rqd_start[i].ctrl;
2510 rq->adapter = adapter;
2511 err = vmxnet3_rq_create(rq, adapter);
2512 if (err) {
2513 if (i == 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002514 netdev_err(adapter->netdev,
2515 "Could not allocate any rx queues. "
2516 "Aborting.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002517 goto queue_err;
2518 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002519 netdev_info(adapter->netdev,
2520 "Number of rx queues changed "
2521 "to : %d.\n", i);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002522 adapter->num_rx_queues = i;
2523 err = 0;
2524 break;
2525 }
2526 }
2527 }
2528 return err;
2529queue_err:
2530 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002531 return err;
2532}
2533
2534static int
2535vmxnet3_open(struct net_device *netdev)
2536{
2537 struct vmxnet3_adapter *adapter;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002538 int err, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002539
2540 adapter = netdev_priv(netdev);
2541
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002542 for (i = 0; i < adapter->num_tx_queues; i++)
2543 spin_lock_init(&adapter->tx_queue[i].tx_lock);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002544
2545 err = vmxnet3_create_queues(adapter, VMXNET3_DEF_TX_RING_SIZE,
2546 VMXNET3_DEF_RX_RING_SIZE,
2547 VMXNET3_DEF_RX_RING_SIZE);
2548 if (err)
2549 goto queue_err;
2550
2551 err = vmxnet3_activate_dev(adapter);
2552 if (err)
2553 goto activate_err;
2554
2555 return 0;
2556
2557activate_err:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002558 vmxnet3_rq_destroy_all(adapter);
2559 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002560queue_err:
2561 return err;
2562}
2563
2564
2565static int
2566vmxnet3_close(struct net_device *netdev)
2567{
2568 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2569
2570 /*
2571 * Reset_work may be in the middle of resetting the device, wait for its
2572 * completion.
2573 */
2574 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2575 msleep(1);
2576
2577 vmxnet3_quiesce_dev(adapter);
2578
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002579 vmxnet3_rq_destroy_all(adapter);
2580 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002581
2582 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2583
2584
2585 return 0;
2586}
2587
2588
2589void
2590vmxnet3_force_close(struct vmxnet3_adapter *adapter)
2591{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002592 int i;
2593
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002594 /*
2595 * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
2596 * vmxnet3_close() will deadlock.
2597 */
2598 BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
2599
2600 /* we need to enable NAPI, otherwise dev_close will deadlock */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002601 for (i = 0; i < adapter->num_rx_queues; i++)
2602 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002603 dev_close(adapter->netdev);
2604}
2605
2606
2607static int
2608vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
2609{
2610 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2611 int err = 0;
2612
2613 if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
2614 return -EINVAL;
2615
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002616 netdev->mtu = new_mtu;
2617
2618 /*
2619 * Reset_work may be in the middle of resetting the device, wait for its
2620 * completion.
2621 */
2622 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2623 msleep(1);
2624
2625 if (netif_running(netdev)) {
2626 vmxnet3_quiesce_dev(adapter);
2627 vmxnet3_reset_dev(adapter);
2628
2629 /* we need to re-create the rx queue based on the new mtu */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002630 vmxnet3_rq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002631 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002632 err = vmxnet3_rq_create_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002633 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002634 netdev_err(netdev,
2635 "failed to re-create rx queues, "
2636 " error %d. Closing it.\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002637 goto out;
2638 }
2639
2640 err = vmxnet3_activate_dev(adapter);
2641 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002642 netdev_err(netdev,
2643 "failed to re-activate, error %d. "
2644 "Closing it\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002645 goto out;
2646 }
2647 }
2648
2649out:
2650 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2651 if (err)
2652 vmxnet3_force_close(adapter);
2653
2654 return err;
2655}
2656
2657
2658static void
2659vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
2660{
2661 struct net_device *netdev = adapter->netdev;
2662
Michał Mirosława0d27302011-04-18 13:31:21 +00002663 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
2664 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX |
Jesse Gross72e85c42011-06-23 13:04:39 +00002665 NETIF_F_HW_VLAN_RX | NETIF_F_TSO | NETIF_F_TSO6 |
2666 NETIF_F_LRO;
Michał Mirosława0d27302011-04-18 13:31:21 +00002667 if (dma64)
Shreyas Bhatewaraebbf9292011-07-20 17:21:51 +00002668 netdev->hw_features |= NETIF_F_HIGHDMA;
Jesse Gross72e85c42011-06-23 13:04:39 +00002669 netdev->vlan_features = netdev->hw_features &
2670 ~(NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
2671 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002672}
2673
2674
2675static void
2676vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2677{
2678 u32 tmp;
2679
2680 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
2681 *(u32 *)mac = tmp;
2682
2683 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
2684 mac[4] = tmp & 0xff;
2685 mac[5] = (tmp >> 8) & 0xff;
2686}
2687
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002688#ifdef CONFIG_PCI_MSI
2689
2690/*
2691 * Enable MSIx vectors.
2692 * Returns :
2693 * 0 on successful enabling of required vectors,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002694 * VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002695 * could be enabled.
2696 * number of vectors which can be enabled otherwise (this number is smaller
2697 * than VMXNET3_LINUX_MIN_MSIX_VECT)
2698 */
2699
2700static int
2701vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter,
2702 int vectors)
2703{
2704 int err = 0, vector_threshold;
2705 vector_threshold = VMXNET3_LINUX_MIN_MSIX_VECT;
2706
2707 while (vectors >= vector_threshold) {
2708 err = pci_enable_msix(adapter->pdev, adapter->intr.msix_entries,
2709 vectors);
2710 if (!err) {
2711 adapter->intr.num_intrs = vectors;
2712 return 0;
2713 } else if (err < 0) {
Stephen Hemminger4bad25f2013-01-15 07:28:28 +00002714 dev_err(&adapter->netdev->dev,
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002715 "Failed to enable MSI-X, error: %d\n", err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002716 vectors = 0;
2717 } else if (err < vector_threshold) {
2718 break;
2719 } else {
2720 /* If fails to enable required number of MSI-x vectors
Shreyas Bhatewara7e96fbf2011-01-14 15:00:03 +00002721 * try enabling minimum number of vectors required.
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002722 */
Stephen Hemminger4bad25f2013-01-15 07:28:28 +00002723 dev_err(&adapter->netdev->dev,
2724 "Failed to enable %d MSI-X, trying %d instead\n",
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002725 vectors, vector_threshold);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002726 vectors = vector_threshold;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002727 }
2728 }
2729
Stephen Hemminger4bad25f2013-01-15 07:28:28 +00002730 dev_info(&adapter->pdev->dev,
2731 "Number of MSI-X interrupts which can be allocated "
2732 "is lower than min threshold required.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002733 return err;
2734}
2735
2736
2737#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002738
2739static void
2740vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
2741{
2742 u32 cfg;
Roland Dreiere328d412011-05-06 08:32:53 +00002743 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002744
2745 /* intr settings */
Roland Dreiere328d412011-05-06 08:32:53 +00002746 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002747 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2748 VMXNET3_CMD_GET_CONF_INTR);
2749 cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Roland Dreiere328d412011-05-06 08:32:53 +00002750 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002751 adapter->intr.type = cfg & 0x3;
2752 adapter->intr.mask_mode = (cfg >> 2) & 0x3;
2753
2754 if (adapter->intr.type == VMXNET3_IT_AUTO) {
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002755 adapter->intr.type = VMXNET3_IT_MSIX;
2756 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002757
Randy Dunlap8f7e5242009-10-14 20:38:58 -07002758#ifdef CONFIG_PCI_MSI
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002759 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002760 int vector, err = 0;
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002761
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002762 adapter->intr.num_intrs = (adapter->share_intr ==
2763 VMXNET3_INTR_TXSHARE) ? 1 :
2764 adapter->num_tx_queues;
2765 adapter->intr.num_intrs += (adapter->share_intr ==
2766 VMXNET3_INTR_BUDDYSHARE) ? 0 :
2767 adapter->num_rx_queues;
2768 adapter->intr.num_intrs += 1; /* for link event */
2769
2770 adapter->intr.num_intrs = (adapter->intr.num_intrs >
2771 VMXNET3_LINUX_MIN_MSIX_VECT
2772 ? adapter->intr.num_intrs :
2773 VMXNET3_LINUX_MIN_MSIX_VECT);
2774
2775 for (vector = 0; vector < adapter->intr.num_intrs; vector++)
2776 adapter->intr.msix_entries[vector].entry = vector;
2777
2778 err = vmxnet3_acquire_msix_vectors(adapter,
2779 adapter->intr.num_intrs);
2780 /* If we cannot allocate one MSIx vector per queue
2781 * then limit the number of rx queues to 1
2782 */
2783 if (err == VMXNET3_LINUX_MIN_MSIX_VECT) {
2784 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
Shreyas Bhatewara7e96fbf2011-01-14 15:00:03 +00002785 || adapter->num_rx_queues != 1) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002786 adapter->share_intr = VMXNET3_INTR_TXSHARE;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002787 netdev_err(adapter->netdev,
2788 "Number of rx queues : 1\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002789 adapter->num_rx_queues = 1;
2790 adapter->intr.num_intrs =
2791 VMXNET3_LINUX_MIN_MSIX_VECT;
2792 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002793 return;
2794 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002795 if (!err)
2796 return;
2797
2798 /* If we cannot allocate MSIx vectors use only one rx queue */
Stephen Hemminger4bad25f2013-01-15 07:28:28 +00002799 dev_info(&adapter->pdev->dev,
2800 "Failed to enable MSI-X, error %d. "
2801 "Limiting #rx queues to 1, try MSI.\n", err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002802
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002803 adapter->intr.type = VMXNET3_IT_MSI;
2804 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002805
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002806 if (adapter->intr.type == VMXNET3_IT_MSI) {
2807 int err;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002808 err = pci_enable_msi(adapter->pdev);
2809 if (!err) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002810 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002811 adapter->intr.num_intrs = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002812 return;
2813 }
2814 }
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002815#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002816
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002817 adapter->num_rx_queues = 1;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002818 dev_info(&adapter->netdev->dev,
2819 "Using INTx interrupt, #Rx queues: 1.\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002820 adapter->intr.type = VMXNET3_IT_INTX;
2821
2822 /* INT-X related setting */
2823 adapter->intr.num_intrs = 1;
2824}
2825
2826
2827static void
2828vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
2829{
2830 if (adapter->intr.type == VMXNET3_IT_MSIX)
2831 pci_disable_msix(adapter->pdev);
2832 else if (adapter->intr.type == VMXNET3_IT_MSI)
2833 pci_disable_msi(adapter->pdev);
2834 else
2835 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
2836}
2837
2838
2839static void
2840vmxnet3_tx_timeout(struct net_device *netdev)
2841{
2842 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2843 adapter->tx_timeout_count++;
2844
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002845 netdev_err(adapter->netdev, "tx hang\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002846 schedule_work(&adapter->work);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002847 netif_wake_queue(adapter->netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002848}
2849
2850
2851static void
2852vmxnet3_reset_work(struct work_struct *data)
2853{
2854 struct vmxnet3_adapter *adapter;
2855
2856 adapter = container_of(data, struct vmxnet3_adapter, work);
2857
2858 /* if another thread is resetting the device, no need to proceed */
2859 if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2860 return;
2861
2862 /* if the device is closed, we must leave it alone */
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002863 rtnl_lock();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002864 if (netif_running(adapter->netdev)) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002865 netdev_notice(adapter->netdev, "resetting\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002866 vmxnet3_quiesce_dev(adapter);
2867 vmxnet3_reset_dev(adapter);
2868 vmxnet3_activate_dev(adapter);
2869 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002870 netdev_info(adapter->netdev, "already closed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002871 }
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002872 rtnl_unlock();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002873
2874 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2875}
2876
2877
Bill Pemberton3a4751a2012-12-03 09:24:16 -05002878static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002879vmxnet3_probe_device(struct pci_dev *pdev,
2880 const struct pci_device_id *id)
2881{
2882 static const struct net_device_ops vmxnet3_netdev_ops = {
2883 .ndo_open = vmxnet3_open,
2884 .ndo_stop = vmxnet3_close,
2885 .ndo_start_xmit = vmxnet3_xmit_frame,
2886 .ndo_set_mac_address = vmxnet3_set_mac_addr,
2887 .ndo_change_mtu = vmxnet3_change_mtu,
Michał Mirosława0d27302011-04-18 13:31:21 +00002888 .ndo_set_features = vmxnet3_set_features,
stephen hemminger95305f62011-06-08 14:53:57 +00002889 .ndo_get_stats64 = vmxnet3_get_stats64,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002890 .ndo_tx_timeout = vmxnet3_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002891 .ndo_set_rx_mode = vmxnet3_set_mc,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002892 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
2893 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
2894#ifdef CONFIG_NET_POLL_CONTROLLER
2895 .ndo_poll_controller = vmxnet3_netpoll,
2896#endif
2897 };
2898 int err;
2899 bool dma64 = false; /* stupid gcc */
2900 u32 ver;
2901 struct net_device *netdev;
2902 struct vmxnet3_adapter *adapter;
2903 u8 mac[ETH_ALEN];
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002904 int size;
2905 int num_tx_queues;
2906 int num_rx_queues;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002907
Shreyas Bhatewarae154b632011-05-10 06:13:56 +00002908 if (!pci_msi_enabled())
2909 enable_mq = 0;
2910
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002911#ifdef VMXNET3_RSS
2912 if (enable_mq)
2913 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
2914 (int)num_online_cpus());
2915 else
2916#endif
2917 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002918 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002919
2920 if (enable_mq)
2921 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
2922 (int)num_online_cpus());
2923 else
2924 num_tx_queues = 1;
2925
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002926 num_tx_queues = rounddown_pow_of_two(num_tx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002927 netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
2928 max(num_tx_queues, num_rx_queues));
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002929 dev_info(&pdev->dev,
2930 "# of Tx queues : %d, # of Rx queues : %d\n",
2931 num_tx_queues, num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002932
Joe Perches41de8d42012-01-29 13:47:52 +00002933 if (!netdev)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002934 return -ENOMEM;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002935
2936 pci_set_drvdata(pdev, netdev);
2937 adapter = netdev_priv(netdev);
2938 adapter->netdev = netdev;
2939 adapter->pdev = pdev;
2940
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002941 spin_lock_init(&adapter->cmd_lock);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002942 adapter->shared = pci_alloc_consistent(adapter->pdev,
stephen hemminger96800ee2012-11-13 13:53:28 +00002943 sizeof(struct Vmxnet3_DriverShared),
2944 &adapter->shared_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002945 if (!adapter->shared) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002946 dev_err(&pdev->dev, "Failed to allocate memory\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002947 err = -ENOMEM;
2948 goto err_alloc_shared;
2949 }
2950
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002951 adapter->num_rx_queues = num_rx_queues;
2952 adapter->num_tx_queues = num_tx_queues;
2953
2954 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
2955 size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
2956 adapter->tqd_start = pci_alloc_consistent(adapter->pdev, size,
stephen hemminger96800ee2012-11-13 13:53:28 +00002957 &adapter->queue_desc_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002958
2959 if (!adapter->tqd_start) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002960 dev_err(&pdev->dev, "Failed to allocate memory\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002961 err = -ENOMEM;
2962 goto err_alloc_queue_desc;
2963 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002964 adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
stephen hemminger96800ee2012-11-13 13:53:28 +00002965 adapter->num_tx_queues);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002966
2967 adapter->pm_conf = kmalloc(sizeof(struct Vmxnet3_PMConf), GFP_KERNEL);
2968 if (adapter->pm_conf == NULL) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002969 err = -ENOMEM;
2970 goto err_alloc_pm;
2971 }
2972
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002973#ifdef VMXNET3_RSS
2974
2975 adapter->rss_conf = kmalloc(sizeof(struct UPT1_RSSConf), GFP_KERNEL);
2976 if (adapter->rss_conf == NULL) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002977 err = -ENOMEM;
2978 goto err_alloc_rss;
2979 }
2980#endif /* VMXNET3_RSS */
2981
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002982 err = vmxnet3_alloc_pci_resources(adapter, &dma64);
2983 if (err < 0)
2984 goto err_alloc_pci;
2985
2986 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
2987 if (ver & 1) {
2988 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
2989 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002990 dev_err(&pdev->dev,
2991 "Incompatible h/w version (0x%x) for adapter\n", ver);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002992 err = -EBUSY;
2993 goto err_ver;
2994 }
2995
2996 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
2997 if (ver & 1) {
2998 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
2999 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003000 dev_err(&pdev->dev,
3001 "Incompatible upt version (0x%x) for adapter\n", ver);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003002 err = -EBUSY;
3003 goto err_ver;
3004 }
3005
Shreyas Bhatewarae101e7d2011-07-20 16:01:11 +00003006 SET_NETDEV_DEV(netdev, &pdev->dev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003007 vmxnet3_declare_features(adapter, dma64);
3008
3009 adapter->dev_number = atomic_read(&devices_found);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003010
stephen hemminger96800ee2012-11-13 13:53:28 +00003011 adapter->share_intr = irq_share_mode;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003012 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE &&
3013 adapter->num_tx_queues != adapter->num_rx_queues)
3014 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
3015
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003016 vmxnet3_alloc_intr_resources(adapter);
3017
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003018#ifdef VMXNET3_RSS
3019 if (adapter->num_rx_queues > 1 &&
3020 adapter->intr.type == VMXNET3_IT_MSIX) {
3021 adapter->rss = true;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003022 dev_dbg(&pdev->dev, "RSS is enabled.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003023 } else {
3024 adapter->rss = false;
3025 }
3026#endif
3027
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003028 vmxnet3_read_mac_addr(adapter, mac);
3029 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3030
3031 netdev->netdev_ops = &vmxnet3_netdev_ops;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003032 vmxnet3_set_ethtool_ops(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003033 netdev->watchdog_timeo = 5 * HZ;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003034
3035 INIT_WORK(&adapter->work, vmxnet3_reset_work);
Steve Hodgsone3bc4ff2012-08-14 17:13:36 +01003036 set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003037
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) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003056 dev_err(&pdev->dev, "Failed to register adapter\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003057 goto err_register;
3058 }
3059
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00003060 vmxnet3_check_link(adapter, false);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003061 atomic_inc(&devices_found);
3062 return 0;
3063
3064err_register:
3065 vmxnet3_free_intr_resources(adapter);
3066err_ver:
3067 vmxnet3_free_pci_resources(adapter);
3068err_alloc_pci:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003069#ifdef VMXNET3_RSS
3070 kfree(adapter->rss_conf);
3071err_alloc_rss:
3072#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003073 kfree(adapter->pm_conf);
3074err_alloc_pm:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003075 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3076 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003077err_alloc_queue_desc:
3078 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3079 adapter->shared, adapter->shared_pa);
3080err_alloc_shared:
3081 pci_set_drvdata(pdev, NULL);
3082 free_netdev(netdev);
3083 return err;
3084}
3085
3086
Bill Pemberton3a4751a2012-12-03 09:24:16 -05003087static void
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003088vmxnet3_remove_device(struct pci_dev *pdev)
3089{
3090 struct net_device *netdev = pci_get_drvdata(pdev);
3091 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003092 int size = 0;
3093 int num_rx_queues;
3094
3095#ifdef VMXNET3_RSS
3096 if (enable_mq)
3097 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3098 (int)num_online_cpus());
3099 else
3100#endif
3101 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07003102 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003103
Tejun Heo23f333a2010-12-12 16:45:14 +01003104 cancel_work_sync(&adapter->work);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003105
3106 unregister_netdev(netdev);
3107
3108 vmxnet3_free_intr_resources(adapter);
3109 vmxnet3_free_pci_resources(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003110#ifdef VMXNET3_RSS
3111 kfree(adapter->rss_conf);
3112#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003113 kfree(adapter->pm_conf);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003114
3115 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3116 size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
3117 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3118 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003119 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3120 adapter->shared, adapter->shared_pa);
3121 free_netdev(netdev);
3122}
3123
3124
3125#ifdef CONFIG_PM
3126
3127static int
3128vmxnet3_suspend(struct device *device)
3129{
3130 struct pci_dev *pdev = to_pci_dev(device);
3131 struct net_device *netdev = pci_get_drvdata(pdev);
3132 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3133 struct Vmxnet3_PMConf *pmConf;
3134 struct ethhdr *ehdr;
3135 struct arphdr *ahdr;
3136 u8 *arpreq;
3137 struct in_device *in_dev;
3138 struct in_ifaddr *ifa;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003139 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003140 int i = 0;
3141
3142 if (!netif_running(netdev))
3143 return 0;
3144
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003145 for (i = 0; i < adapter->num_rx_queues; i++)
3146 napi_disable(&adapter->rx_queue[i].napi);
3147
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003148 vmxnet3_disable_all_intrs(adapter);
3149 vmxnet3_free_irqs(adapter);
3150 vmxnet3_free_intr_resources(adapter);
3151
3152 netif_device_detach(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003153 netif_tx_stop_all_queues(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003154
3155 /* Create wake-up filters. */
3156 pmConf = adapter->pm_conf;
3157 memset(pmConf, 0, sizeof(*pmConf));
3158
3159 if (adapter->wol & WAKE_UCAST) {
3160 pmConf->filters[i].patternSize = ETH_ALEN;
3161 pmConf->filters[i].maskSize = 1;
3162 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
3163 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
3164
Harvey Harrison3843e512010-10-21 18:05:32 +00003165 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003166 i++;
3167 }
3168
3169 if (adapter->wol & WAKE_ARP) {
3170 in_dev = in_dev_get(netdev);
3171 if (!in_dev)
3172 goto skip_arp;
3173
3174 ifa = (struct in_ifaddr *)in_dev->ifa_list;
3175 if (!ifa)
3176 goto skip_arp;
3177
3178 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
3179 sizeof(struct arphdr) + /* ARP header */
3180 2 * ETH_ALEN + /* 2 Ethernet addresses*/
3181 2 * sizeof(u32); /*2 IPv4 addresses */
3182 pmConf->filters[i].maskSize =
3183 (pmConf->filters[i].patternSize - 1) / 8 + 1;
3184
3185 /* ETH_P_ARP in Ethernet header. */
3186 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
3187 ehdr->h_proto = htons(ETH_P_ARP);
3188
3189 /* ARPOP_REQUEST in ARP header. */
3190 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
3191 ahdr->ar_op = htons(ARPOP_REQUEST);
3192 arpreq = (u8 *)(ahdr + 1);
3193
3194 /* The Unicast IPv4 address in 'tip' field. */
3195 arpreq += 2 * ETH_ALEN + sizeof(u32);
3196 *(u32 *)arpreq = ifa->ifa_address;
3197
3198 /* The mask for the relevant bits. */
3199 pmConf->filters[i].mask[0] = 0x00;
3200 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
3201 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
3202 pmConf->filters[i].mask[3] = 0x00;
3203 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
3204 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
3205 in_dev_put(in_dev);
3206
Harvey Harrison3843e512010-10-21 18:05:32 +00003207 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003208 i++;
3209 }
3210
3211skip_arp:
3212 if (adapter->wol & WAKE_MAGIC)
Harvey Harrison3843e512010-10-21 18:05:32 +00003213 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003214
3215 pmConf->numFilters = i;
3216
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003217 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3218 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3219 *pmConf));
3220 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3221 pmConf));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003222
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003223 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003224 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3225 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003226 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003227
3228 pci_save_state(pdev);
3229 pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
3230 adapter->wol);
3231 pci_disable_device(pdev);
3232 pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
3233
3234 return 0;
3235}
3236
3237
3238static int
3239vmxnet3_resume(struct device *device)
3240{
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003241 int err, i = 0;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003242 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003243 struct pci_dev *pdev = to_pci_dev(device);
3244 struct net_device *netdev = pci_get_drvdata(pdev);
3245 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3246 struct Vmxnet3_PMConf *pmConf;
3247
3248 if (!netif_running(netdev))
3249 return 0;
3250
3251 /* Destroy wake-up filters. */
3252 pmConf = adapter->pm_conf;
3253 memset(pmConf, 0, sizeof(*pmConf));
3254
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003255 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3256 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3257 *pmConf));
Harvey Harrison0561cf32010-10-21 18:05:34 +00003258 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003259 pmConf));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003260
3261 netif_device_attach(netdev);
3262 pci_set_power_state(pdev, PCI_D0);
3263 pci_restore_state(pdev);
3264 err = pci_enable_device_mem(pdev);
3265 if (err != 0)
3266 return err;
3267
3268 pci_enable_wake(pdev, PCI_D0, 0);
3269
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003270 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003271 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3272 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003273 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003274 vmxnet3_alloc_intr_resources(adapter);
3275 vmxnet3_request_irqs(adapter);
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003276 for (i = 0; i < adapter->num_rx_queues; i++)
3277 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003278 vmxnet3_enable_all_intrs(adapter);
3279
3280 return 0;
3281}
3282
Alexey Dobriyan47145212009-12-14 18:00:08 -08003283static const struct dev_pm_ops vmxnet3_pm_ops = {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003284 .suspend = vmxnet3_suspend,
3285 .resume = vmxnet3_resume,
3286};
3287#endif
3288
3289static struct pci_driver vmxnet3_driver = {
3290 .name = vmxnet3_driver_name,
3291 .id_table = vmxnet3_pciid_table,
3292 .probe = vmxnet3_probe_device,
Bill Pemberton3a4751a2012-12-03 09:24:16 -05003293 .remove = vmxnet3_remove_device,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003294#ifdef CONFIG_PM
3295 .driver.pm = &vmxnet3_pm_ops,
3296#endif
3297};
3298
3299
3300static int __init
3301vmxnet3_init_module(void)
3302{
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003303 pr_info("%s - version %s\n", VMXNET3_DRIVER_DESC,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003304 VMXNET3_DRIVER_VERSION_REPORT);
3305 return pci_register_driver(&vmxnet3_driver);
3306}
3307
3308module_init(vmxnet3_init_module);
3309
3310
3311static void
3312vmxnet3_exit_module(void)
3313{
3314 pci_unregister_driver(&vmxnet3_driver);
3315}
3316
3317module_exit(vmxnet3_exit_module);
3318
3319MODULE_AUTHOR("VMware, Inc.");
3320MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
3321MODULE_LICENSE("GPL v2");
3322MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);