blob: 1f66a7e3c25f7ce2d2bbd9c026dd840c00a9d7df [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
Shreyas Bhatewara09c50882010-11-19 10:55:24 +000046static int enable_mq = 1;
47static int irq_share_mode;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -070048
Shreyas Bhatewaraf9f25022011-01-14 14:59:31 +000049static void
50vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac);
51
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -070052/*
53 * Enable/Disable the given intr
54 */
55static void
56vmxnet3_enable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
57{
58 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 0);
59}
60
61
62static void
63vmxnet3_disable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
64{
65 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 1);
66}
67
68
69/*
70 * Enable/Disable all intrs used by the device
71 */
72static void
73vmxnet3_enable_all_intrs(struct vmxnet3_adapter *adapter)
74{
75 int i;
76
77 for (i = 0; i < adapter->intr.num_intrs; i++)
78 vmxnet3_enable_intr(adapter, i);
Ronghua Zang6929fe82010-07-15 22:18:47 -070079 adapter->shared->devRead.intrConf.intrCtrl &=
80 cpu_to_le32(~VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -070081}
82
83
84static void
85vmxnet3_disable_all_intrs(struct vmxnet3_adapter *adapter)
86{
87 int i;
88
Ronghua Zang6929fe82010-07-15 22:18:47 -070089 adapter->shared->devRead.intrConf.intrCtrl |=
90 cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -070091 for (i = 0; i < adapter->intr.num_intrs; i++)
92 vmxnet3_disable_intr(adapter, i);
93}
94
95
96static void
97vmxnet3_ack_events(struct vmxnet3_adapter *adapter, u32 events)
98{
99 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_ECR, events);
100}
101
102
103static bool
104vmxnet3_tq_stopped(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
105{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000106 return tq->stopped;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700107}
108
109
110static void
111vmxnet3_tq_start(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
112{
113 tq->stopped = false;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000114 netif_start_subqueue(adapter->netdev, tq - adapter->tx_queue);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700115}
116
117
118static void
119vmxnet3_tq_wake(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
120{
121 tq->stopped = false;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000122 netif_wake_subqueue(adapter->netdev, (tq - adapter->tx_queue));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700123}
124
125
126static void
127vmxnet3_tq_stop(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
128{
129 tq->stopped = true;
130 tq->num_stop++;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000131 netif_stop_subqueue(adapter->netdev, (tq - adapter->tx_queue));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700132}
133
134
135/*
136 * Check the link state. This may start or stop the tx queue.
137 */
138static void
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +0000139vmxnet3_check_link(struct vmxnet3_adapter *adapter, bool affectTxQueue)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700140{
141 u32 ret;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000142 int i;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +0000143 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700144
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +0000145 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700146 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
147 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +0000148 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
149
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700150 adapter->link_speed = ret >> 16;
151 if (ret & 1) { /* Link is up. */
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000152 netdev_info(adapter->netdev, "NIC Link is Up %d Mbps\n",
153 adapter->link_speed);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700154 if (!netif_carrier_ok(adapter->netdev))
155 netif_carrier_on(adapter->netdev);
156
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000157 if (affectTxQueue) {
158 for (i = 0; i < adapter->num_tx_queues; i++)
159 vmxnet3_tq_start(&adapter->tx_queue[i],
160 adapter);
161 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700162 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000163 netdev_info(adapter->netdev, "NIC Link is Down\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700164 if (netif_carrier_ok(adapter->netdev))
165 netif_carrier_off(adapter->netdev);
166
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000167 if (affectTxQueue) {
168 for (i = 0; i < adapter->num_tx_queues; i++)
169 vmxnet3_tq_stop(&adapter->tx_queue[i], adapter);
170 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700171 }
172}
173
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700174static void
175vmxnet3_process_events(struct vmxnet3_adapter *adapter)
176{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000177 int i;
Roland Dreiere328d412011-05-06 08:32:53 +0000178 unsigned long flags;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000179 u32 events = le32_to_cpu(adapter->shared->ecr);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700180 if (!events)
181 return;
182
183 vmxnet3_ack_events(adapter, events);
184
185 /* Check if link state has changed */
186 if (events & VMXNET3_ECR_LINK)
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +0000187 vmxnet3_check_link(adapter, true);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700188
189 /* Check if there is an error on xmit/recv queues */
190 if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
Roland Dreiere328d412011-05-06 08:32:53 +0000191 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700192 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
193 VMXNET3_CMD_GET_QUEUE_STATUS);
Roland Dreiere328d412011-05-06 08:32:53 +0000194 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700195
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000196 for (i = 0; i < adapter->num_tx_queues; i++)
197 if (adapter->tqd_start[i].status.stopped)
198 dev_err(&adapter->netdev->dev,
199 "%s: tq[%d] error 0x%x\n",
200 adapter->netdev->name, i, le32_to_cpu(
201 adapter->tqd_start[i].status.error));
202 for (i = 0; i < adapter->num_rx_queues; i++)
203 if (adapter->rqd_start[i].status.stopped)
204 dev_err(&adapter->netdev->dev,
205 "%s: rq[%d] error 0x%x\n",
206 adapter->netdev->name, i,
207 adapter->rqd_start[i].status.error);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700208
209 schedule_work(&adapter->work);
210 }
211}
212
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000213#ifdef __BIG_ENDIAN_BITFIELD
214/*
215 * The device expects the bitfields in shared structures to be written in
216 * little endian. When CPU is big endian, the following routines are used to
217 * correctly read and write into ABI.
218 * The general technique used here is : double word bitfields are defined in
219 * opposite order for big endian architecture. Then before reading them in
220 * driver the complete double word is translated using le32_to_cpu. Similarly
221 * After the driver writes into bitfields, cpu_to_le32 is used to translate the
222 * double words into required format.
223 * In order to avoid touching bits in shared structure more than once, temporary
224 * descriptors are used. These are passed as srcDesc to following functions.
225 */
226static void vmxnet3_RxDescToCPU(const struct Vmxnet3_RxDesc *srcDesc,
227 struct Vmxnet3_RxDesc *dstDesc)
228{
229 u32 *src = (u32 *)srcDesc + 2;
230 u32 *dst = (u32 *)dstDesc + 2;
231 dstDesc->addr = le64_to_cpu(srcDesc->addr);
232 *dst = le32_to_cpu(*src);
233 dstDesc->ext1 = le32_to_cpu(srcDesc->ext1);
234}
235
236static void vmxnet3_TxDescToLe(const struct Vmxnet3_TxDesc *srcDesc,
237 struct Vmxnet3_TxDesc *dstDesc)
238{
239 int i;
240 u32 *src = (u32 *)(srcDesc + 1);
241 u32 *dst = (u32 *)(dstDesc + 1);
242
243 /* Working backwards so that the gen bit is set at the end. */
244 for (i = 2; i > 0; i--) {
245 src--;
246 dst--;
247 *dst = cpu_to_le32(*src);
248 }
249}
250
251
252static void vmxnet3_RxCompToCPU(const struct Vmxnet3_RxCompDesc *srcDesc,
253 struct Vmxnet3_RxCompDesc *dstDesc)
254{
255 int i = 0;
256 u32 *src = (u32 *)srcDesc;
257 u32 *dst = (u32 *)dstDesc;
258 for (i = 0; i < sizeof(struct Vmxnet3_RxCompDesc) / sizeof(u32); i++) {
259 *dst = le32_to_cpu(*src);
260 src++;
261 dst++;
262 }
263}
264
265
266/* Used to read bitfield values from double words. */
267static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
268{
269 u32 temp = le32_to_cpu(*bitfield);
270 u32 mask = ((1 << size) - 1) << pos;
271 temp &= mask;
272 temp >>= pos;
273 return temp;
274}
275
276
277
278#endif /* __BIG_ENDIAN_BITFIELD */
279
280#ifdef __BIG_ENDIAN_BITFIELD
281
282# define VMXNET3_TXDESC_GET_GEN(txdesc) get_bitfield32(((const __le32 *) \
283 txdesc) + VMXNET3_TXD_GEN_DWORD_SHIFT, \
284 VMXNET3_TXD_GEN_SHIFT, VMXNET3_TXD_GEN_SIZE)
285# define VMXNET3_TXDESC_GET_EOP(txdesc) get_bitfield32(((const __le32 *) \
286 txdesc) + VMXNET3_TXD_EOP_DWORD_SHIFT, \
287 VMXNET3_TXD_EOP_SHIFT, VMXNET3_TXD_EOP_SIZE)
288# define VMXNET3_TCD_GET_GEN(tcd) get_bitfield32(((const __le32 *)tcd) + \
289 VMXNET3_TCD_GEN_DWORD_SHIFT, VMXNET3_TCD_GEN_SHIFT, \
290 VMXNET3_TCD_GEN_SIZE)
291# define VMXNET3_TCD_GET_TXIDX(tcd) get_bitfield32((const __le32 *)tcd, \
292 VMXNET3_TCD_TXIDX_SHIFT, VMXNET3_TCD_TXIDX_SIZE)
293# define vmxnet3_getRxComp(dstrcd, rcd, tmp) do { \
294 (dstrcd) = (tmp); \
295 vmxnet3_RxCompToCPU((rcd), (tmp)); \
296 } while (0)
297# define vmxnet3_getRxDesc(dstrxd, rxd, tmp) do { \
298 (dstrxd) = (tmp); \
299 vmxnet3_RxDescToCPU((rxd), (tmp)); \
300 } while (0)
301
302#else
303
304# define VMXNET3_TXDESC_GET_GEN(txdesc) ((txdesc)->gen)
305# define VMXNET3_TXDESC_GET_EOP(txdesc) ((txdesc)->eop)
306# define VMXNET3_TCD_GET_GEN(tcd) ((tcd)->gen)
307# define VMXNET3_TCD_GET_TXIDX(tcd) ((tcd)->txdIdx)
308# define vmxnet3_getRxComp(dstrcd, rcd, tmp) (dstrcd) = (rcd)
309# define vmxnet3_getRxDesc(dstrxd, rxd, tmp) (dstrxd) = (rxd)
310
311#endif /* __BIG_ENDIAN_BITFIELD */
312
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700313
314static void
315vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
316 struct pci_dev *pdev)
317{
318 if (tbi->map_type == VMXNET3_MAP_SINGLE)
319 pci_unmap_single(pdev, tbi->dma_addr, tbi->len,
320 PCI_DMA_TODEVICE);
321 else if (tbi->map_type == VMXNET3_MAP_PAGE)
322 pci_unmap_page(pdev, tbi->dma_addr, tbi->len,
323 PCI_DMA_TODEVICE);
324 else
325 BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
326
327 tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
328}
329
330
331static int
332vmxnet3_unmap_pkt(u32 eop_idx, struct vmxnet3_tx_queue *tq,
333 struct pci_dev *pdev, struct vmxnet3_adapter *adapter)
334{
335 struct sk_buff *skb;
336 int entries = 0;
337
338 /* no out of order completion */
339 BUG_ON(tq->buf_info[eop_idx].sop_idx != tq->tx_ring.next2comp);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000340 BUG_ON(VMXNET3_TXDESC_GET_EOP(&(tq->tx_ring.base[eop_idx].txd)) != 1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700341
342 skb = tq->buf_info[eop_idx].skb;
343 BUG_ON(skb == NULL);
344 tq->buf_info[eop_idx].skb = NULL;
345
346 VMXNET3_INC_RING_IDX_ONLY(eop_idx, tq->tx_ring.size);
347
348 while (tq->tx_ring.next2comp != eop_idx) {
349 vmxnet3_unmap_tx_buf(tq->buf_info + tq->tx_ring.next2comp,
350 pdev);
351
352 /* update next2comp w/o tx_lock. Since we are marking more,
353 * instead of less, tx ring entries avail, the worst case is
354 * that the tx routine incorrectly re-queues a pkt due to
355 * insufficient tx ring entries.
356 */
357 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
358 entries++;
359 }
360
361 dev_kfree_skb_any(skb);
362 return entries;
363}
364
365
366static int
367vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue *tq,
368 struct vmxnet3_adapter *adapter)
369{
370 int completed = 0;
371 union Vmxnet3_GenericDesc *gdesc;
372
373 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000374 while (VMXNET3_TCD_GET_GEN(&gdesc->tcd) == tq->comp_ring.gen) {
375 completed += vmxnet3_unmap_pkt(VMXNET3_TCD_GET_TXIDX(
376 &gdesc->tcd), tq, adapter->pdev,
377 adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700378
379 vmxnet3_comp_ring_adv_next2proc(&tq->comp_ring);
380 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
381 }
382
383 if (completed) {
384 spin_lock(&tq->tx_lock);
385 if (unlikely(vmxnet3_tq_stopped(tq, adapter) &&
386 vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) >
387 VMXNET3_WAKE_QUEUE_THRESHOLD(tq) &&
388 netif_carrier_ok(adapter->netdev))) {
389 vmxnet3_tq_wake(tq, adapter);
390 }
391 spin_unlock(&tq->tx_lock);
392 }
393 return completed;
394}
395
396
397static void
398vmxnet3_tq_cleanup(struct vmxnet3_tx_queue *tq,
399 struct vmxnet3_adapter *adapter)
400{
401 int i;
402
403 while (tq->tx_ring.next2comp != tq->tx_ring.next2fill) {
404 struct vmxnet3_tx_buf_info *tbi;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700405
406 tbi = tq->buf_info + tq->tx_ring.next2comp;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700407
408 vmxnet3_unmap_tx_buf(tbi, adapter->pdev);
409 if (tbi->skb) {
410 dev_kfree_skb_any(tbi->skb);
411 tbi->skb = NULL;
412 }
413 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
414 }
415
416 /* sanity check, verify all buffers are indeed unmapped and freed */
417 for (i = 0; i < tq->tx_ring.size; i++) {
418 BUG_ON(tq->buf_info[i].skb != NULL ||
419 tq->buf_info[i].map_type != VMXNET3_MAP_NONE);
420 }
421
422 tq->tx_ring.gen = VMXNET3_INIT_GEN;
423 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
424
425 tq->comp_ring.gen = VMXNET3_INIT_GEN;
426 tq->comp_ring.next2proc = 0;
427}
428
429
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000430static void
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700431vmxnet3_tq_destroy(struct vmxnet3_tx_queue *tq,
432 struct vmxnet3_adapter *adapter)
433{
434 if (tq->tx_ring.base) {
435 pci_free_consistent(adapter->pdev, tq->tx_ring.size *
436 sizeof(struct Vmxnet3_TxDesc),
437 tq->tx_ring.base, tq->tx_ring.basePA);
438 tq->tx_ring.base = NULL;
439 }
440 if (tq->data_ring.base) {
441 pci_free_consistent(adapter->pdev, tq->data_ring.size *
442 sizeof(struct Vmxnet3_TxDataDesc),
443 tq->data_ring.base, tq->data_ring.basePA);
444 tq->data_ring.base = NULL;
445 }
446 if (tq->comp_ring.base) {
447 pci_free_consistent(adapter->pdev, tq->comp_ring.size *
448 sizeof(struct Vmxnet3_TxCompDesc),
449 tq->comp_ring.base, tq->comp_ring.basePA);
450 tq->comp_ring.base = NULL;
451 }
452 kfree(tq->buf_info);
453 tq->buf_info = NULL;
454}
455
456
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000457/* Destroy all tx queues */
458void
459vmxnet3_tq_destroy_all(struct vmxnet3_adapter *adapter)
460{
461 int i;
462
463 for (i = 0; i < adapter->num_tx_queues; i++)
464 vmxnet3_tq_destroy(&adapter->tx_queue[i], adapter);
465}
466
467
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700468static void
469vmxnet3_tq_init(struct vmxnet3_tx_queue *tq,
470 struct vmxnet3_adapter *adapter)
471{
472 int i;
473
474 /* reset the tx ring contents to 0 and reset the tx ring states */
475 memset(tq->tx_ring.base, 0, tq->tx_ring.size *
476 sizeof(struct Vmxnet3_TxDesc));
477 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
478 tq->tx_ring.gen = VMXNET3_INIT_GEN;
479
480 memset(tq->data_ring.base, 0, tq->data_ring.size *
481 sizeof(struct Vmxnet3_TxDataDesc));
482
483 /* reset the tx comp ring contents to 0 and reset comp ring states */
484 memset(tq->comp_ring.base, 0, tq->comp_ring.size *
485 sizeof(struct Vmxnet3_TxCompDesc));
486 tq->comp_ring.next2proc = 0;
487 tq->comp_ring.gen = VMXNET3_INIT_GEN;
488
489 /* reset the bookkeeping data */
490 memset(tq->buf_info, 0, sizeof(tq->buf_info[0]) * tq->tx_ring.size);
491 for (i = 0; i < tq->tx_ring.size; i++)
492 tq->buf_info[i].map_type = VMXNET3_MAP_NONE;
493
494 /* stats are not reset */
495}
496
497
498static int
499vmxnet3_tq_create(struct vmxnet3_tx_queue *tq,
500 struct vmxnet3_adapter *adapter)
501{
502 BUG_ON(tq->tx_ring.base || tq->data_ring.base ||
503 tq->comp_ring.base || tq->buf_info);
504
505 tq->tx_ring.base = pci_alloc_consistent(adapter->pdev, tq->tx_ring.size
506 * sizeof(struct Vmxnet3_TxDesc),
507 &tq->tx_ring.basePA);
508 if (!tq->tx_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000509 netdev_err(adapter->netdev, "failed to allocate tx ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700510 goto err;
511 }
512
513 tq->data_ring.base = pci_alloc_consistent(adapter->pdev,
514 tq->data_ring.size *
515 sizeof(struct Vmxnet3_TxDataDesc),
516 &tq->data_ring.basePA);
517 if (!tq->data_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000518 netdev_err(adapter->netdev, "failed to allocate data ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700519 goto err;
520 }
521
522 tq->comp_ring.base = pci_alloc_consistent(adapter->pdev,
523 tq->comp_ring.size *
524 sizeof(struct Vmxnet3_TxCompDesc),
525 &tq->comp_ring.basePA);
526 if (!tq->comp_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000527 netdev_err(adapter->netdev, "failed to allocate tx comp ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700528 goto err;
529 }
530
531 tq->buf_info = kcalloc(tq->tx_ring.size, sizeof(tq->buf_info[0]),
532 GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000533 if (!tq->buf_info)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700534 goto err;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700535
536 return 0;
537
538err:
539 vmxnet3_tq_destroy(tq, adapter);
540 return -ENOMEM;
541}
542
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000543static void
544vmxnet3_tq_cleanup_all(struct vmxnet3_adapter *adapter)
545{
546 int i;
547
548 for (i = 0; i < adapter->num_tx_queues; i++)
549 vmxnet3_tq_cleanup(&adapter->tx_queue[i], adapter);
550}
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700551
552/*
553 * starting from ring->next2fill, allocate rx buffers for the given ring
554 * of the rx queue and update the rx desc. stop after @num_to_alloc buffers
555 * are allocated or allocation fails
556 */
557
558static int
559vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
560 int num_to_alloc, struct vmxnet3_adapter *adapter)
561{
562 int num_allocated = 0;
563 struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
564 struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
565 u32 val;
566
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000567 while (num_allocated <= num_to_alloc) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700568 struct vmxnet3_rx_buf_info *rbi;
569 union Vmxnet3_GenericDesc *gd;
570
571 rbi = rbi_base + ring->next2fill;
572 gd = ring->base + ring->next2fill;
573
574 if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
575 if (rbi->skb == NULL) {
Stephen Hemminger0d735f12013-01-15 07:28:26 +0000576 rbi->skb = __netdev_alloc_skb_ip_align(adapter->netdev,
577 rbi->len,
578 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700579 if (unlikely(rbi->skb == NULL)) {
580 rq->stats.rx_buf_alloc_failure++;
581 break;
582 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700583
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700584 rbi->dma_addr = pci_map_single(adapter->pdev,
585 rbi->skb->data, rbi->len,
586 PCI_DMA_FROMDEVICE);
587 } else {
588 /* rx buffer skipped by the device */
589 }
590 val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
591 } else {
592 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
593 rbi->len != PAGE_SIZE);
594
595 if (rbi->page == NULL) {
596 rbi->page = alloc_page(GFP_ATOMIC);
597 if (unlikely(rbi->page == NULL)) {
598 rq->stats.rx_buf_alloc_failure++;
599 break;
600 }
601 rbi->dma_addr = pci_map_page(adapter->pdev,
602 rbi->page, 0, PAGE_SIZE,
603 PCI_DMA_FROMDEVICE);
604 } else {
605 /* rx buffers skipped by the device */
606 }
607 val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
608 }
609
610 BUG_ON(rbi->dma_addr == 0);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000611 gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000612 gd->dword[2] = cpu_to_le32((!ring->gen << VMXNET3_RXD_GEN_SHIFT)
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000613 | val | rbi->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700614
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000615 /* Fill the last buffer but dont mark it ready, or else the
616 * device will think that the queue is full */
617 if (num_allocated == num_to_alloc)
618 break;
619
620 gd->dword[2] |= cpu_to_le32(ring->gen << VMXNET3_RXD_GEN_SHIFT);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700621 num_allocated++;
622 vmxnet3_cmd_ring_adv_next2fill(ring);
623 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700624
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000625 netdev_dbg(adapter->netdev,
Stephen Hemminger69b9a712013-01-15 07:28:27 +0000626 "alloc_rx_buf: %d allocated, next2fill %u, next2comp %u\n",
627 num_allocated, ring->next2fill, ring->next2comp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700628
629 /* so that the device can distinguish a full ring and an empty ring */
630 BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
631
632 return num_allocated;
633}
634
635
636static void
637vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
638 struct vmxnet3_rx_buf_info *rbi)
639{
640 struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
641 skb_shinfo(skb)->nr_frags;
642
643 BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
644
Ian Campbell0e0634d2011-09-21 21:53:28 +0000645 __skb_frag_set_page(frag, rbi->page);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700646 frag->page_offset = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000647 skb_frag_size_set(frag, rcd->len);
648 skb->data_len += rcd->len;
Eric Dumazet5e6c3552011-10-13 11:38:17 +0000649 skb->truesize += PAGE_SIZE;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700650 skb_shinfo(skb)->nr_frags++;
651}
652
653
654static void
655vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
656 struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
657 struct vmxnet3_adapter *adapter)
658{
659 u32 dw2, len;
660 unsigned long buf_offset;
661 int i;
662 union Vmxnet3_GenericDesc *gdesc;
663 struct vmxnet3_tx_buf_info *tbi = NULL;
664
665 BUG_ON(ctx->copy_size > skb_headlen(skb));
666
667 /* use the previous gen bit for the SOP desc */
668 dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
669
670 ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
671 gdesc = ctx->sop_txd; /* both loops below can be skipped */
672
673 /* no need to map the buffer if headers are copied */
674 if (ctx->copy_size) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000675 ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700676 tq->tx_ring.next2fill *
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000677 sizeof(struct Vmxnet3_TxDataDesc));
678 ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700679 ctx->sop_txd->dword[3] = 0;
680
681 tbi = tq->buf_info + tq->tx_ring.next2fill;
682 tbi->map_type = VMXNET3_MAP_NONE;
683
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000684 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700685 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000686 tq->tx_ring.next2fill,
687 le64_to_cpu(ctx->sop_txd->txd.addr),
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700688 ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
689 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
690
691 /* use the right gen for non-SOP desc */
692 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
693 }
694
695 /* linear part can use multiple tx desc if it's big */
696 len = skb_headlen(skb) - ctx->copy_size;
697 buf_offset = ctx->copy_size;
698 while (len) {
699 u32 buf_size;
700
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000701 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
702 buf_size = len;
703 dw2 |= len;
704 } else {
705 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
706 /* spec says that for TxDesc.len, 0 == 2^14 */
707 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700708
709 tbi = tq->buf_info + tq->tx_ring.next2fill;
710 tbi->map_type = VMXNET3_MAP_SINGLE;
711 tbi->dma_addr = pci_map_single(adapter->pdev,
712 skb->data + buf_offset, buf_size,
713 PCI_DMA_TODEVICE);
714
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000715 tbi->len = buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700716
717 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
718 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
719
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000720 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000721 gdesc->dword[2] = cpu_to_le32(dw2);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700722 gdesc->dword[3] = 0;
723
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000724 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700725 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000726 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
727 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700728 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
729 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
730
731 len -= buf_size;
732 buf_offset += buf_size;
733 }
734
735 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000736 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000737 u32 buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700738
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000739 buf_offset = 0;
740 len = skb_frag_size(frag);
741 while (len) {
742 tbi = tq->buf_info + tq->tx_ring.next2fill;
743 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
744 buf_size = len;
745 dw2 |= len;
746 } else {
747 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
748 /* spec says that for TxDesc.len, 0 == 2^14 */
749 }
750 tbi->map_type = VMXNET3_MAP_PAGE;
751 tbi->dma_addr = skb_frag_dma_map(&adapter->pdev->dev, frag,
752 buf_offset, buf_size,
753 DMA_TO_DEVICE);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700754
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000755 tbi->len = buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700756
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000757 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
758 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700759
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000760 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
761 gdesc->dword[2] = cpu_to_le32(dw2);
762 gdesc->dword[3] = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700763
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000764 netdev_dbg(adapter->netdev,
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000765 "txd[%u]: 0x%llu %u %u\n",
766 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
767 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
768 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
769 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
770
771 len -= buf_size;
772 buf_offset += buf_size;
773 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700774 }
775
776 ctx->eop_txd = gdesc;
777
778 /* set the last buf_info for the pkt */
779 tbi->skb = skb;
780 tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
781}
782
783
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000784/* Init all tx queues */
785static void
786vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
787{
788 int i;
789
790 for (i = 0; i < adapter->num_tx_queues; i++)
791 vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
792}
793
794
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700795/*
796 * parse and copy relevant protocol headers:
797 * For a tso pkt, relevant headers are L2/3/4 including options
798 * For a pkt requesting csum offloading, they are L2/3 and may include L4
799 * if it's a TCP/UDP pkt
800 *
801 * Returns:
802 * -1: error happens during parsing
803 * 0: protocol headers parsed, but too big to be copied
804 * 1: protocol headers parsed and copied
805 *
806 * Other effects:
807 * 1. related *ctx fields are updated.
808 * 2. ctx->copy_size is # of bytes copied
809 * 3. the portion copied is guaranteed to be in the linear part
810 *
811 */
812static int
813vmxnet3_parse_and_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
814 struct vmxnet3_tx_ctx *ctx,
815 struct vmxnet3_adapter *adapter)
816{
817 struct Vmxnet3_TxDataDesc *tdd;
818
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000819 if (ctx->mss) { /* TSO */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700820 ctx->eth_ip_hdr_size = skb_transport_offset(skb);
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000821 ctx->l4_hdr_size = tcp_hdrlen(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700822 ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
823 } else {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700824 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000825 ctx->eth_ip_hdr_size = skb_checksum_start_offset(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700826
827 if (ctx->ipv4) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000828 const struct iphdr *iph = ip_hdr(skb);
829
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000830 if (iph->protocol == IPPROTO_TCP)
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000831 ctx->l4_hdr_size = tcp_hdrlen(skb);
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000832 else if (iph->protocol == IPPROTO_UDP)
David S. Millerf6a1ad42012-03-05 21:16:26 -0500833 ctx->l4_hdr_size = sizeof(struct udphdr);
Shreyas Bhatewara39d4a962011-01-14 14:59:41 +0000834 else
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700835 ctx->l4_hdr_size = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700836 } else {
837 /* for simplicity, don't copy L4 headers */
838 ctx->l4_hdr_size = 0;
839 }
Neil Hormanb2032622012-02-16 01:48:56 +0000840 ctx->copy_size = min(ctx->eth_ip_hdr_size +
841 ctx->l4_hdr_size, skb->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700842 } else {
843 ctx->eth_ip_hdr_size = 0;
844 ctx->l4_hdr_size = 0;
845 /* copy as much as allowed */
846 ctx->copy_size = min((unsigned int)VMXNET3_HDR_COPY_SIZE
847 , skb_headlen(skb));
848 }
849
850 /* make sure headers are accessible directly */
851 if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
852 goto err;
853 }
854
855 if (unlikely(ctx->copy_size > VMXNET3_HDR_COPY_SIZE)) {
856 tq->stats.oversized_hdr++;
857 ctx->copy_size = 0;
858 return 0;
859 }
860
861 tdd = tq->data_ring.base + tq->tx_ring.next2fill;
862
863 memcpy(tdd->data, skb->data, ctx->copy_size);
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000864 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700865 "copy %u bytes to dataRing[%u]\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700866 ctx->copy_size, tq->tx_ring.next2fill);
867 return 1;
868
869err:
870 return -1;
871}
872
873
874static void
875vmxnet3_prepare_tso(struct sk_buff *skb,
876 struct vmxnet3_tx_ctx *ctx)
877{
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000878 struct tcphdr *tcph = tcp_hdr(skb);
879
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700880 if (ctx->ipv4) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000881 struct iphdr *iph = ip_hdr(skb);
882
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700883 iph->check = 0;
884 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
885 IPPROTO_TCP, 0);
886 } else {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000887 struct ipv6hdr *iph = ipv6_hdr(skb);
888
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700889 tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
890 IPPROTO_TCP, 0);
891 }
892}
893
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000894static int txd_estimate(const struct sk_buff *skb)
895{
896 int count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
897 int i;
898
899 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
900 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
901
902 count += VMXNET3_TXD_NEEDED(skb_frag_size(frag));
903 }
904 return count;
905}
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700906
907/*
908 * Transmits a pkt thru a given tq
909 * Returns:
910 * NETDEV_TX_OK: descriptors are setup successfully
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300911 * NETDEV_TX_OK: error occurred, the pkt is dropped
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700912 * NETDEV_TX_BUSY: tx ring is full, queue is stopped
913 *
914 * Side-effects:
915 * 1. tx ring may be changed
916 * 2. tq stats may be updated accordingly
917 * 3. shared->txNumDeferred may be updated
918 */
919
920static int
921vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
922 struct vmxnet3_adapter *adapter, struct net_device *netdev)
923{
924 int ret;
925 u32 count;
926 unsigned long flags;
927 struct vmxnet3_tx_ctx ctx;
928 union Vmxnet3_GenericDesc *gdesc;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000929#ifdef __BIG_ENDIAN_BITFIELD
930 /* Use temporary descriptor to avoid touching bits multiple times */
931 union Vmxnet3_GenericDesc tempTxDesc;
932#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700933
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000934 count = txd_estimate(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700935
Jesse Gross72e85c42011-06-23 13:04:39 +0000936 ctx.ipv4 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IP));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700937
938 ctx.mss = skb_shinfo(skb)->gso_size;
939 if (ctx.mss) {
940 if (skb_header_cloned(skb)) {
941 if (unlikely(pskb_expand_head(skb, 0, 0,
942 GFP_ATOMIC) != 0)) {
943 tq->stats.drop_tso++;
944 goto drop_pkt;
945 }
946 tq->stats.copy_skb_header++;
947 }
948 vmxnet3_prepare_tso(skb, &ctx);
949 } else {
950 if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
951
952 /* non-tso pkts must not use more than
953 * VMXNET3_MAX_TXD_PER_PKT entries
954 */
955 if (skb_linearize(skb) != 0) {
956 tq->stats.drop_too_many_frags++;
957 goto drop_pkt;
958 }
959 tq->stats.linearized++;
960
961 /* recalculate the # of descriptors to use */
962 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
963 }
964 }
965
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000966 spin_lock_irqsave(&tq->tx_lock, flags);
967
968 if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
969 tq->stats.tx_ring_full++;
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000970 netdev_dbg(adapter->netdev,
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000971 "tx queue stopped on %s, next2comp %u"
972 " next2fill %u\n", adapter->netdev->name,
973 tq->tx_ring.next2comp, tq->tx_ring.next2fill);
974
975 vmxnet3_tq_stop(tq, adapter);
976 spin_unlock_irqrestore(&tq->tx_lock, flags);
977 return NETDEV_TX_BUSY;
978 }
979
980
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700981 ret = vmxnet3_parse_and_copy_hdr(skb, tq, &ctx, adapter);
982 if (ret >= 0) {
983 BUG_ON(ret <= 0 && ctx.copy_size != 0);
984 /* hdrs parsed, check against other limits */
985 if (ctx.mss) {
986 if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
987 VMXNET3_MAX_TX_BUF_SIZE)) {
988 goto hdr_too_big;
989 }
990 } else {
991 if (skb->ip_summed == CHECKSUM_PARTIAL) {
992 if (unlikely(ctx.eth_ip_hdr_size +
993 skb->csum_offset >
994 VMXNET3_MAX_CSUM_OFFSET)) {
995 goto hdr_too_big;
996 }
997 }
998 }
999 } else {
1000 tq->stats.drop_hdr_inspect_err++;
Dan Carpenterf955e142010-12-20 03:03:15 +00001001 goto unlock_drop_pkt;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001002 }
1003
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001004 /* fill tx descs related to addr & len */
1005 vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter);
1006
1007 /* setup the EOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001008 ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001009
1010 /* setup the SOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001011#ifdef __BIG_ENDIAN_BITFIELD
1012 gdesc = &tempTxDesc;
1013 gdesc->dword[2] = ctx.sop_txd->dword[2];
1014 gdesc->dword[3] = ctx.sop_txd->dword[3];
1015#else
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001016 gdesc = ctx.sop_txd;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001017#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001018 if (ctx.mss) {
1019 gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
1020 gdesc->txd.om = VMXNET3_OM_TSO;
1021 gdesc->txd.msscof = ctx.mss;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001022 le32_add_cpu(&tq->shared->txNumDeferred, (skb->len -
1023 gdesc->txd.hlen + ctx.mss - 1) / ctx.mss);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001024 } else {
1025 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1026 gdesc->txd.hlen = ctx.eth_ip_hdr_size;
1027 gdesc->txd.om = VMXNET3_OM_CSUM;
1028 gdesc->txd.msscof = ctx.eth_ip_hdr_size +
1029 skb->csum_offset;
1030 } else {
1031 gdesc->txd.om = 0;
1032 gdesc->txd.msscof = 0;
1033 }
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001034 le32_add_cpu(&tq->shared->txNumDeferred, 1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001035 }
1036
1037 if (vlan_tx_tag_present(skb)) {
1038 gdesc->txd.ti = 1;
1039 gdesc->txd.tci = vlan_tx_tag_get(skb);
1040 }
1041
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001042 /* finally flips the GEN bit of the SOP desc. */
1043 gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
1044 VMXNET3_TXD_GEN);
1045#ifdef __BIG_ENDIAN_BITFIELD
1046 /* Finished updating in bitfields of Tx Desc, so write them in original
1047 * place.
1048 */
1049 vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
1050 (struct Vmxnet3_TxDesc *)ctx.sop_txd);
1051 gdesc = ctx.sop_txd;
1052#endif
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00001053 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -07001054 "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
Joe Perchesc2fd03a2012-06-04 12:44:18 +00001055 (u32)(ctx.sop_txd -
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001056 tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
1057 le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001058
1059 spin_unlock_irqrestore(&tq->tx_lock, flags);
1060
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001061 if (le32_to_cpu(tq->shared->txNumDeferred) >=
1062 le32_to_cpu(tq->shared->txThreshold)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001063 tq->shared->txNumDeferred = 0;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001064 VMXNET3_WRITE_BAR0_REG(adapter,
1065 VMXNET3_REG_TXPROD + tq->qid * 8,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001066 tq->tx_ring.next2fill);
1067 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001068
1069 return NETDEV_TX_OK;
1070
1071hdr_too_big:
1072 tq->stats.drop_oversized_hdr++;
Dan Carpenterf955e142010-12-20 03:03:15 +00001073unlock_drop_pkt:
1074 spin_unlock_irqrestore(&tq->tx_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001075drop_pkt:
1076 tq->stats.drop_total++;
1077 dev_kfree_skb(skb);
1078 return NETDEV_TX_OK;
1079}
1080
1081
1082static netdev_tx_t
1083vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1084{
1085 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001086
stephen hemminger96800ee2012-11-13 13:53:28 +00001087 BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
1088 return vmxnet3_tq_xmit(skb,
1089 &adapter->tx_queue[skb->queue_mapping],
1090 adapter, netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001091}
1092
1093
1094static void
1095vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
1096 struct sk_buff *skb,
1097 union Vmxnet3_GenericDesc *gdesc)
1098{
Michał Mirosława0d27302011-04-18 13:31:21 +00001099 if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001100 /* typical case: TCP/UDP over IP and both csums are correct */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001101 if ((le32_to_cpu(gdesc->dword[3]) & VMXNET3_RCD_CSUM_OK) ==
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001102 VMXNET3_RCD_CSUM_OK) {
1103 skb->ip_summed = CHECKSUM_UNNECESSARY;
1104 BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
1105 BUG_ON(!(gdesc->rcd.v4 || gdesc->rcd.v6));
1106 BUG_ON(gdesc->rcd.frg);
1107 } else {
1108 if (gdesc->rcd.csum) {
1109 skb->csum = htons(gdesc->rcd.csum);
1110 skb->ip_summed = CHECKSUM_PARTIAL;
1111 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001112 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001113 }
1114 }
1115 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001116 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001117 }
1118}
1119
1120
1121static void
1122vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
1123 struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
1124{
1125 rq->stats.drop_err++;
1126 if (!rcd->fcs)
1127 rq->stats.drop_fcs++;
1128
1129 rq->stats.drop_total++;
1130
1131 /*
1132 * We do not unmap and chain the rx buffer to the skb.
1133 * We basically pretend this buffer is not used and will be recycled
1134 * by vmxnet3_rq_alloc_rx_buf()
1135 */
1136
1137 /*
1138 * ctx->skb may be NULL if this is the first and the only one
1139 * desc for the pkt
1140 */
1141 if (ctx->skb)
1142 dev_kfree_skb_irq(ctx->skb);
1143
1144 ctx->skb = NULL;
1145}
1146
1147
1148static int
1149vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
1150 struct vmxnet3_adapter *adapter, int quota)
1151{
Joe Perches215faf92010-12-21 02:16:10 -08001152 static const u32 rxprod_reg[2] = {
1153 VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2
1154 };
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001155 u32 num_rxd = 0;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001156 bool skip_page_frags = false;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001157 struct Vmxnet3_RxCompDesc *rcd;
1158 struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001159#ifdef __BIG_ENDIAN_BITFIELD
1160 struct Vmxnet3_RxDesc rxCmdDesc;
1161 struct Vmxnet3_RxCompDesc rxComp;
1162#endif
1163 vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
1164 &rxComp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001165 while (rcd->gen == rq->comp_ring.gen) {
1166 struct vmxnet3_rx_buf_info *rbi;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001167 struct sk_buff *skb, *new_skb = NULL;
1168 struct page *new_page = NULL;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001169 int num_to_alloc;
1170 struct Vmxnet3_RxDesc *rxd;
1171 u32 idx, ring_idx;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001172 struct vmxnet3_cmd_ring *ring = NULL;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001173 if (num_rxd >= quota) {
1174 /* we may stop even before we see the EOP desc of
1175 * the current pkt
1176 */
1177 break;
1178 }
1179 num_rxd++;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001180 BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001181 idx = rcd->rxdIdx;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001182 ring_idx = rcd->rqID < adapter->num_rx_queues ? 0 : 1;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001183 ring = rq->rx_ring + ring_idx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001184 vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
1185 &rxCmdDesc);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001186 rbi = rq->buf_info[ring_idx] + idx;
1187
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001188 BUG_ON(rxd->addr != rbi->dma_addr ||
1189 rxd->len != rbi->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001190
1191 if (unlikely(rcd->eop && rcd->err)) {
1192 vmxnet3_rx_error(rq, rcd, ctx, adapter);
1193 goto rcd_done;
1194 }
1195
1196 if (rcd->sop) { /* first buf of the pkt */
1197 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
1198 rcd->rqID != rq->qid);
1199
1200 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
1201 BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
1202
1203 if (unlikely(rcd->len == 0)) {
1204 /* Pretend the rx buffer is skipped. */
1205 BUG_ON(!(rcd->sop && rcd->eop));
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00001206 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -07001207 "rxRing[%u][%u] 0 length\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001208 ring_idx, idx);
1209 goto rcd_done;
1210 }
1211
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001212 skip_page_frags = false;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001213 ctx->skb = rbi->skb;
Stephen Hemminger0d735f12013-01-15 07:28:26 +00001214 new_skb = netdev_alloc_skb_ip_align(adapter->netdev,
1215 rbi->len);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001216 if (new_skb == NULL) {
1217 /* Skb allocation failed, do not handover this
1218 * skb to stack. Reuse it. Drop the existing pkt
1219 */
1220 rq->stats.rx_buf_alloc_failure++;
1221 ctx->skb = NULL;
1222 rq->stats.drop_total++;
1223 skip_page_frags = true;
1224 goto rcd_done;
1225 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001226
1227 pci_unmap_single(adapter->pdev, rbi->dma_addr, rbi->len,
1228 PCI_DMA_FROMDEVICE);
1229
1230 skb_put(ctx->skb, rcd->len);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001231
1232 /* Immediate refill */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001233 rbi->skb = new_skb;
1234 rbi->dma_addr = pci_map_single(adapter->pdev,
stephen hemminger96800ee2012-11-13 13:53:28 +00001235 rbi->skb->data, rbi->len,
1236 PCI_DMA_FROMDEVICE);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001237 rxd->addr = cpu_to_le64(rbi->dma_addr);
1238 rxd->len = rbi->len;
1239
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001240 } else {
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001241 BUG_ON(ctx->skb == NULL && !skip_page_frags);
1242
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001243 /* non SOP buffer must be type 1 in most cases */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001244 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE);
1245 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001246
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001247 /* If an sop buffer was dropped, skip all
1248 * following non-sop fragments. They will be reused.
1249 */
1250 if (skip_page_frags)
1251 goto rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001252
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001253 new_page = alloc_page(GFP_ATOMIC);
1254 if (unlikely(new_page == NULL)) {
1255 /* Replacement page frag could not be allocated.
1256 * Reuse this page. Drop the pkt and free the
1257 * skb which contained this page as a frag. Skip
1258 * processing all the following non-sop frags.
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001259 */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001260 rq->stats.rx_buf_alloc_failure++;
1261 dev_kfree_skb(ctx->skb);
1262 ctx->skb = NULL;
1263 skip_page_frags = true;
1264 goto rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001265 }
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001266
1267 if (rcd->len) {
1268 pci_unmap_page(adapter->pdev,
1269 rbi->dma_addr, rbi->len,
1270 PCI_DMA_FROMDEVICE);
1271
1272 vmxnet3_append_frag(ctx->skb, rcd, rbi);
1273 }
1274
1275 /* Immediate refill */
1276 rbi->page = new_page;
1277 rbi->dma_addr = pci_map_page(adapter->pdev, rbi->page,
1278 0, PAGE_SIZE,
1279 PCI_DMA_FROMDEVICE);
1280 rxd->addr = cpu_to_le64(rbi->dma_addr);
1281 rxd->len = rbi->len;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001282 }
1283
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001284
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001285 skb = ctx->skb;
1286 if (rcd->eop) {
1287 skb->len += skb->data_len;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001288
1289 vmxnet3_rx_csum(adapter, skb,
1290 (union Vmxnet3_GenericDesc *)rcd);
1291 skb->protocol = eth_type_trans(skb, adapter->netdev);
1292
Jesse Gross72e85c42011-06-23 13:04:39 +00001293 if (unlikely(rcd->ts))
1294 __vlan_hwaccel_put_tag(skb, rcd->tci);
1295
Jesse Gross213ade82011-06-24 14:24:35 +00001296 if (adapter->netdev->features & NETIF_F_LRO)
1297 netif_receive_skb(skb);
1298 else
1299 napi_gro_receive(&rq->napi, skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001300
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001301 ctx->skb = NULL;
1302 }
1303
1304rcd_done:
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001305 /* device may have skipped some rx descs */
1306 ring->next2comp = idx;
1307 num_to_alloc = vmxnet3_cmd_ring_desc_avail(ring);
1308 ring = rq->rx_ring + ring_idx;
1309 while (num_to_alloc) {
1310 vmxnet3_getRxDesc(rxd, &ring->base[ring->next2fill].rxd,
1311 &rxCmdDesc);
1312 BUG_ON(!rxd->addr);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001313
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001314 /* Recv desc is ready to be used by the device */
1315 rxd->gen = ring->gen;
1316 vmxnet3_cmd_ring_adv_next2fill(ring);
1317 num_to_alloc--;
1318 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001319
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001320 /* if needed, update the register */
1321 if (unlikely(rq->shared->updateRxProd)) {
1322 VMXNET3_WRITE_BAR0_REG(adapter,
stephen hemminger96800ee2012-11-13 13:53:28 +00001323 rxprod_reg[ring_idx] + rq->qid * 8,
1324 ring->next2fill);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001325 }
1326
1327 vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001328 vmxnet3_getRxComp(rcd,
stephen hemminger96800ee2012-11-13 13:53:28 +00001329 &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001330 }
1331
1332 return num_rxd;
1333}
1334
1335
1336static void
1337vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
1338 struct vmxnet3_adapter *adapter)
1339{
1340 u32 i, ring_idx;
1341 struct Vmxnet3_RxDesc *rxd;
1342
1343 for (ring_idx = 0; ring_idx < 2; ring_idx++) {
1344 for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001345#ifdef __BIG_ENDIAN_BITFIELD
1346 struct Vmxnet3_RxDesc rxDesc;
1347#endif
1348 vmxnet3_getRxDesc(rxd,
1349 &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001350
1351 if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
1352 rq->buf_info[ring_idx][i].skb) {
1353 pci_unmap_single(adapter->pdev, rxd->addr,
1354 rxd->len, PCI_DMA_FROMDEVICE);
1355 dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
1356 rq->buf_info[ring_idx][i].skb = NULL;
1357 } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
1358 rq->buf_info[ring_idx][i].page) {
1359 pci_unmap_page(adapter->pdev, rxd->addr,
1360 rxd->len, PCI_DMA_FROMDEVICE);
1361 put_page(rq->buf_info[ring_idx][i].page);
1362 rq->buf_info[ring_idx][i].page = NULL;
1363 }
1364 }
1365
1366 rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
1367 rq->rx_ring[ring_idx].next2fill =
1368 rq->rx_ring[ring_idx].next2comp = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001369 }
1370
1371 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1372 rq->comp_ring.next2proc = 0;
1373}
1374
1375
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001376static void
1377vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
1378{
1379 int i;
1380
1381 for (i = 0; i < adapter->num_rx_queues; i++)
1382 vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
1383}
1384
1385
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001386void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
1387 struct vmxnet3_adapter *adapter)
1388{
1389 int i;
1390 int j;
1391
1392 /* all rx buffers must have already been freed */
1393 for (i = 0; i < 2; i++) {
1394 if (rq->buf_info[i]) {
1395 for (j = 0; j < rq->rx_ring[i].size; j++)
1396 BUG_ON(rq->buf_info[i][j].page != NULL);
1397 }
1398 }
1399
1400
1401 kfree(rq->buf_info[0]);
1402
1403 for (i = 0; i < 2; i++) {
1404 if (rq->rx_ring[i].base) {
1405 pci_free_consistent(adapter->pdev, rq->rx_ring[i].size
1406 * sizeof(struct Vmxnet3_RxDesc),
1407 rq->rx_ring[i].base,
1408 rq->rx_ring[i].basePA);
1409 rq->rx_ring[i].base = NULL;
1410 }
1411 rq->buf_info[i] = NULL;
1412 }
1413
1414 if (rq->comp_ring.base) {
1415 pci_free_consistent(adapter->pdev, rq->comp_ring.size *
1416 sizeof(struct Vmxnet3_RxCompDesc),
1417 rq->comp_ring.base, rq->comp_ring.basePA);
1418 rq->comp_ring.base = NULL;
1419 }
1420}
1421
1422
1423static int
1424vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
1425 struct vmxnet3_adapter *adapter)
1426{
1427 int i;
1428
1429 /* initialize buf_info */
1430 for (i = 0; i < rq->rx_ring[0].size; i++) {
1431
1432 /* 1st buf for a pkt is skbuff */
1433 if (i % adapter->rx_buf_per_pkt == 0) {
1434 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
1435 rq->buf_info[0][i].len = adapter->skb_buf_size;
1436 } else { /* subsequent bufs for a pkt is frag */
1437 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
1438 rq->buf_info[0][i].len = PAGE_SIZE;
1439 }
1440 }
1441 for (i = 0; i < rq->rx_ring[1].size; i++) {
1442 rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
1443 rq->buf_info[1][i].len = PAGE_SIZE;
1444 }
1445
1446 /* reset internal state and allocate buffers for both rings */
1447 for (i = 0; i < 2; i++) {
1448 rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001449
1450 memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
1451 sizeof(struct Vmxnet3_RxDesc));
1452 rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
1453 }
1454 if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
1455 adapter) == 0) {
1456 /* at least has 1 rx buffer for the 1st ring */
1457 return -ENOMEM;
1458 }
1459 vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
1460
1461 /* reset the comp ring */
1462 rq->comp_ring.next2proc = 0;
1463 memset(rq->comp_ring.base, 0, rq->comp_ring.size *
1464 sizeof(struct Vmxnet3_RxCompDesc));
1465 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1466
1467 /* reset rxctx */
1468 rq->rx_ctx.skb = NULL;
1469
1470 /* stats are not reset */
1471 return 0;
1472}
1473
1474
1475static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001476vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
1477{
1478 int i, err = 0;
1479
1480 for (i = 0; i < adapter->num_rx_queues; i++) {
1481 err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
1482 if (unlikely(err)) {
1483 dev_err(&adapter->netdev->dev, "%s: failed to "
1484 "initialize rx queue%i\n",
1485 adapter->netdev->name, i);
1486 break;
1487 }
1488 }
1489 return err;
1490
1491}
1492
1493
1494static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001495vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
1496{
1497 int i;
1498 size_t sz;
1499 struct vmxnet3_rx_buf_info *bi;
1500
1501 for (i = 0; i < 2; i++) {
1502
1503 sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
1504 rq->rx_ring[i].base = pci_alloc_consistent(adapter->pdev, sz,
1505 &rq->rx_ring[i].basePA);
1506 if (!rq->rx_ring[i].base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001507 netdev_err(adapter->netdev,
1508 "failed to allocate rx ring %d\n", i);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001509 goto err;
1510 }
1511 }
1512
1513 sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
1514 rq->comp_ring.base = pci_alloc_consistent(adapter->pdev, sz,
1515 &rq->comp_ring.basePA);
1516 if (!rq->comp_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001517 netdev_err(adapter->netdev, "failed to allocate rx comp ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001518 goto err;
1519 }
1520
1521 sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
1522 rq->rx_ring[1].size);
Julia Lawall476c6092010-05-13 10:05:40 +00001523 bi = kzalloc(sz, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001524 if (!bi)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001525 goto err;
Joe Perchese404dec2012-01-29 12:56:23 +00001526
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001527 rq->buf_info[0] = bi;
1528 rq->buf_info[1] = bi + rq->rx_ring[0].size;
1529
1530 return 0;
1531
1532err:
1533 vmxnet3_rq_destroy(rq, adapter);
1534 return -ENOMEM;
1535}
1536
1537
1538static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001539vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
1540{
1541 int i, err = 0;
1542
1543 for (i = 0; i < adapter->num_rx_queues; i++) {
1544 err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
1545 if (unlikely(err)) {
1546 dev_err(&adapter->netdev->dev,
1547 "%s: failed to create rx queue%i\n",
1548 adapter->netdev->name, i);
1549 goto err_out;
1550 }
1551 }
1552 return err;
1553err_out:
1554 vmxnet3_rq_destroy_all(adapter);
1555 return err;
1556
1557}
1558
1559/* Multiple queue aware polling function for tx and rx */
1560
1561static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001562vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
1563{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001564 int rcd_done = 0, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001565 if (unlikely(adapter->shared->ecr))
1566 vmxnet3_process_events(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001567 for (i = 0; i < adapter->num_tx_queues; i++)
1568 vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001569
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001570 for (i = 0; i < adapter->num_rx_queues; i++)
1571 rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
1572 adapter, budget);
1573 return rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001574}
1575
1576
1577static int
1578vmxnet3_poll(struct napi_struct *napi, int budget)
1579{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001580 struct vmxnet3_rx_queue *rx_queue = container_of(napi,
1581 struct vmxnet3_rx_queue, napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001582 int rxd_done;
1583
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001584 rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001585
1586 if (rxd_done < budget) {
1587 napi_complete(napi);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001588 vmxnet3_enable_all_intrs(rx_queue->adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001589 }
1590 return rxd_done;
1591}
1592
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001593/*
1594 * NAPI polling function for MSI-X mode with multiple Rx queues
1595 * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
1596 */
1597
1598static int
1599vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
1600{
1601 struct vmxnet3_rx_queue *rq = container_of(napi,
1602 struct vmxnet3_rx_queue, napi);
1603 struct vmxnet3_adapter *adapter = rq->adapter;
1604 int rxd_done;
1605
1606 /* When sharing interrupt with corresponding tx queue, process
1607 * tx completions in that queue as well
1608 */
1609 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
1610 struct vmxnet3_tx_queue *tq =
1611 &adapter->tx_queue[rq - adapter->rx_queue];
1612 vmxnet3_tq_tx_complete(tq, adapter);
1613 }
1614
1615 rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
1616
1617 if (rxd_done < budget) {
1618 napi_complete(napi);
1619 vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
1620 }
1621 return rxd_done;
1622}
1623
1624
1625#ifdef CONFIG_PCI_MSI
1626
1627/*
1628 * Handle completion interrupts on tx queues
1629 * Returns whether or not the intr is handled
1630 */
1631
1632static irqreturn_t
1633vmxnet3_msix_tx(int irq, void *data)
1634{
1635 struct vmxnet3_tx_queue *tq = data;
1636 struct vmxnet3_adapter *adapter = tq->adapter;
1637
1638 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1639 vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
1640
1641 /* Handle the case where only one irq is allocate for all tx queues */
1642 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1643 int i;
1644 for (i = 0; i < adapter->num_tx_queues; i++) {
1645 struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
1646 vmxnet3_tq_tx_complete(txq, adapter);
1647 }
1648 } else {
1649 vmxnet3_tq_tx_complete(tq, adapter);
1650 }
1651 vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
1652
1653 return IRQ_HANDLED;
1654}
1655
1656
1657/*
1658 * Handle completion interrupts on rx queues. Returns whether or not the
1659 * intr is handled
1660 */
1661
1662static irqreturn_t
1663vmxnet3_msix_rx(int irq, void *data)
1664{
1665 struct vmxnet3_rx_queue *rq = data;
1666 struct vmxnet3_adapter *adapter = rq->adapter;
1667
1668 /* disable intr if needed */
1669 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1670 vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
1671 napi_schedule(&rq->napi);
1672
1673 return IRQ_HANDLED;
1674}
1675
1676/*
1677 *----------------------------------------------------------------------------
1678 *
1679 * vmxnet3_msix_event --
1680 *
1681 * vmxnet3 msix event intr handler
1682 *
1683 * Result:
1684 * whether or not the intr is handled
1685 *
1686 *----------------------------------------------------------------------------
1687 */
1688
1689static irqreturn_t
1690vmxnet3_msix_event(int irq, void *data)
1691{
1692 struct net_device *dev = data;
1693 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1694
1695 /* disable intr if needed */
1696 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1697 vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
1698
1699 if (adapter->shared->ecr)
1700 vmxnet3_process_events(adapter);
1701
1702 vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
1703
1704 return IRQ_HANDLED;
1705}
1706
1707#endif /* CONFIG_PCI_MSI */
1708
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001709
1710/* Interrupt handler for vmxnet3 */
1711static irqreturn_t
1712vmxnet3_intr(int irq, void *dev_id)
1713{
1714 struct net_device *dev = dev_id;
1715 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1716
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001717 if (adapter->intr.type == VMXNET3_IT_INTX) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001718 u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
1719 if (unlikely(icr == 0))
1720 /* not ours */
1721 return IRQ_NONE;
1722 }
1723
1724
1725 /* disable intr if needed */
1726 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001727 vmxnet3_disable_all_intrs(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001728
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001729 napi_schedule(&adapter->rx_queue[0].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001730
1731 return IRQ_HANDLED;
1732}
1733
1734#ifdef CONFIG_NET_POLL_CONTROLLER
1735
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001736/* netpoll callback. */
1737static void
1738vmxnet3_netpoll(struct net_device *netdev)
1739{
1740 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001741
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001742 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1743 vmxnet3_disable_all_intrs(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001744
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001745 vmxnet3_do_poll(adapter, adapter->rx_queue[0].rx_ring[0].size);
1746 vmxnet3_enable_all_intrs(adapter);
1747
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001748}
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001749#endif /* CONFIG_NET_POLL_CONTROLLER */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001750
1751static int
1752vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
1753{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001754 struct vmxnet3_intr *intr = &adapter->intr;
1755 int err = 0, i;
1756 int vector = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001757
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001758#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001759 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001760 for (i = 0; i < adapter->num_tx_queues; i++) {
1761 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1762 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
1763 adapter->netdev->name, vector);
1764 err = request_irq(
1765 intr->msix_entries[vector].vector,
1766 vmxnet3_msix_tx, 0,
1767 adapter->tx_queue[i].name,
1768 &adapter->tx_queue[i]);
1769 } else {
1770 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
1771 adapter->netdev->name, vector);
1772 }
1773 if (err) {
1774 dev_err(&adapter->netdev->dev,
1775 "Failed to request irq for MSIX, %s, "
1776 "error %d\n",
1777 adapter->tx_queue[i].name, err);
1778 return err;
1779 }
1780
1781 /* Handle the case where only 1 MSIx was allocated for
1782 * all tx queues */
1783 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1784 for (; i < adapter->num_tx_queues; i++)
1785 adapter->tx_queue[i].comp_ring.intr_idx
1786 = vector;
1787 vector++;
1788 break;
1789 } else {
1790 adapter->tx_queue[i].comp_ring.intr_idx
1791 = vector++;
1792 }
1793 }
1794 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
1795 vector = 0;
1796
1797 for (i = 0; i < adapter->num_rx_queues; i++) {
1798 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
1799 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
1800 adapter->netdev->name, vector);
1801 else
1802 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
1803 adapter->netdev->name, vector);
1804 err = request_irq(intr->msix_entries[vector].vector,
1805 vmxnet3_msix_rx, 0,
1806 adapter->rx_queue[i].name,
1807 &(adapter->rx_queue[i]));
1808 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001809 netdev_err(adapter->netdev,
1810 "Failed to request irq for MSIX, "
1811 "%s, error %d\n",
1812 adapter->rx_queue[i].name, err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001813 return err;
1814 }
1815
1816 adapter->rx_queue[i].comp_ring.intr_idx = vector++;
1817 }
1818
1819 sprintf(intr->event_msi_vector_name, "%s-event-%d",
1820 adapter->netdev->name, vector);
1821 err = request_irq(intr->msix_entries[vector].vector,
1822 vmxnet3_msix_event, 0,
1823 intr->event_msi_vector_name, adapter->netdev);
1824 intr->event_intr_idx = vector;
1825
1826 } else if (intr->type == VMXNET3_IT_MSI) {
1827 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001828 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
1829 adapter->netdev->name, adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001830 } else {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001831#endif
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001832 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001833 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
1834 IRQF_SHARED, adapter->netdev->name,
1835 adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001836#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001837 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001838#endif
1839 intr->num_intrs = vector + 1;
1840 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001841 netdev_err(adapter->netdev,
1842 "Failed to request irq (intr type:%d), error %d\n",
1843 intr->type, err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001844 } else {
1845 /* Number of rx queues will not change after this */
1846 for (i = 0; i < adapter->num_rx_queues; i++) {
1847 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
1848 rq->qid = i;
1849 rq->qid2 = i + adapter->num_rx_queues;
1850 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001851
1852
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001853
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001854 /* init our intr settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001855 for (i = 0; i < intr->num_intrs; i++)
1856 intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
1857 if (adapter->intr.type != VMXNET3_IT_MSIX) {
1858 adapter->intr.event_intr_idx = 0;
1859 for (i = 0; i < adapter->num_tx_queues; i++)
1860 adapter->tx_queue[i].comp_ring.intr_idx = 0;
1861 adapter->rx_queue[0].comp_ring.intr_idx = 0;
1862 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001863
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001864 netdev_info(adapter->netdev,
1865 "intr type %u, mode %u, %u vectors allocated\n",
1866 intr->type, intr->mask_mode, intr->num_intrs);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001867 }
1868
1869 return err;
1870}
1871
1872
1873static void
1874vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
1875{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001876 struct vmxnet3_intr *intr = &adapter->intr;
1877 BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001878
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001879 switch (intr->type) {
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001880#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001881 case VMXNET3_IT_MSIX:
1882 {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001883 int i, vector = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001884
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001885 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1886 for (i = 0; i < adapter->num_tx_queues; i++) {
1887 free_irq(intr->msix_entries[vector++].vector,
1888 &(adapter->tx_queue[i]));
1889 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
1890 break;
1891 }
1892 }
1893
1894 for (i = 0; i < adapter->num_rx_queues; i++) {
1895 free_irq(intr->msix_entries[vector++].vector,
1896 &(adapter->rx_queue[i]));
1897 }
1898
1899 free_irq(intr->msix_entries[vector].vector,
1900 adapter->netdev);
1901 BUG_ON(vector >= intr->num_intrs);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001902 break;
1903 }
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001904#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001905 case VMXNET3_IT_MSI:
1906 free_irq(adapter->pdev->irq, adapter->netdev);
1907 break;
1908 case VMXNET3_IT_INTX:
1909 free_irq(adapter->pdev->irq, adapter->netdev);
1910 break;
1911 default:
Sasha Levinc068e772012-11-08 10:23:03 +00001912 BUG();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001913 }
1914}
1915
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001916
1917static void
1918vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
1919{
Jesse Gross72e85c42011-06-23 13:04:39 +00001920 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1921 u16 vid;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001922
Jesse Gross72e85c42011-06-23 13:04:39 +00001923 /* allow untagged pkts */
1924 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1925
1926 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1927 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001928}
1929
1930
Jiri Pirko8e586132011-12-08 19:52:37 -05001931static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001932vmxnet3_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1933{
1934 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001935
Jesse Grossf6957f82011-08-07 23:15:47 +00001936 if (!(netdev->flags & IFF_PROMISC)) {
1937 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1938 unsigned long flags;
1939
1940 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1941 spin_lock_irqsave(&adapter->cmd_lock, flags);
1942 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1943 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1944 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1945 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001946
1947 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001948
1949 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001950}
1951
1952
Jiri Pirko8e586132011-12-08 19:52:37 -05001953static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001954vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1955{
1956 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001957
Jesse Grossf6957f82011-08-07 23:15:47 +00001958 if (!(netdev->flags & IFF_PROMISC)) {
1959 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1960 unsigned long flags;
1961
1962 VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
1963 spin_lock_irqsave(&adapter->cmd_lock, flags);
1964 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1965 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1966 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1967 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001968
1969 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001970
1971 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001972}
1973
1974
1975static u8 *
1976vmxnet3_copy_mc(struct net_device *netdev)
1977{
1978 u8 *buf = NULL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001979 u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001980
1981 /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
1982 if (sz <= 0xffff) {
1983 /* We may be called with BH disabled */
1984 buf = kmalloc(sz, GFP_ATOMIC);
1985 if (buf) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001986 struct netdev_hw_addr *ha;
Jiri Pirko567ec872010-02-23 23:17:07 +00001987 int i = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001988
Jiri Pirko22bedad32010-04-01 21:22:57 +00001989 netdev_for_each_mc_addr(ha, netdev)
1990 memcpy(buf + i++ * ETH_ALEN, ha->addr,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001991 ETH_ALEN);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001992 }
1993 }
1994 return buf;
1995}
1996
1997
1998static void
1999vmxnet3_set_mc(struct net_device *netdev)
2000{
2001 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002002 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002003 struct Vmxnet3_RxFilterConf *rxConf =
2004 &adapter->shared->devRead.rxFilterConf;
2005 u8 *new_table = NULL;
2006 u32 new_mode = VMXNET3_RXM_UCAST;
2007
Jesse Gross72e85c42011-06-23 13:04:39 +00002008 if (netdev->flags & IFF_PROMISC) {
2009 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2010 memset(vfTable, 0, VMXNET3_VFT_SIZE * sizeof(*vfTable));
2011
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002012 new_mode |= VMXNET3_RXM_PROMISC;
Jesse Gross72e85c42011-06-23 13:04:39 +00002013 } else {
2014 vmxnet3_restore_vlan(adapter);
2015 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002016
2017 if (netdev->flags & IFF_BROADCAST)
2018 new_mode |= VMXNET3_RXM_BCAST;
2019
2020 if (netdev->flags & IFF_ALLMULTI)
2021 new_mode |= VMXNET3_RXM_ALL_MULTI;
2022 else
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002023 if (!netdev_mc_empty(netdev)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002024 new_table = vmxnet3_copy_mc(netdev);
2025 if (new_table) {
2026 new_mode |= VMXNET3_RXM_MCAST;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002027 rxConf->mfTableLen = cpu_to_le16(
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002028 netdev_mc_count(netdev) * ETH_ALEN);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002029 rxConf->mfTablePA = cpu_to_le64(virt_to_phys(
2030 new_table));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002031 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002032 netdev_info(netdev, "failed to copy mcast list"
2033 ", setting ALL_MULTI\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002034 new_mode |= VMXNET3_RXM_ALL_MULTI;
2035 }
2036 }
2037
2038
2039 if (!(new_mode & VMXNET3_RXM_MCAST)) {
2040 rxConf->mfTableLen = 0;
2041 rxConf->mfTablePA = 0;
2042 }
2043
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002044 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002045 if (new_mode != rxConf->rxMode) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002046 rxConf->rxMode = cpu_to_le32(new_mode);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002047 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2048 VMXNET3_CMD_UPDATE_RX_MODE);
Jesse Gross72e85c42011-06-23 13:04:39 +00002049 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2050 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002051 }
2052
2053 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2054 VMXNET3_CMD_UPDATE_MAC_FILTERS);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002055 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002056
2057 kfree(new_table);
2058}
2059
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002060void
2061vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
2062{
2063 int i;
2064
2065 for (i = 0; i < adapter->num_rx_queues; i++)
2066 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2067}
2068
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002069
2070/*
2071 * Set up driver_shared based on settings in adapter.
2072 */
2073
2074static void
2075vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2076{
2077 struct Vmxnet3_DriverShared *shared = adapter->shared;
2078 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2079 struct Vmxnet3_TxQueueConf *tqc;
2080 struct Vmxnet3_RxQueueConf *rqc;
2081 int i;
2082
2083 memset(shared, 0, sizeof(*shared));
2084
2085 /* driver settings */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002086 shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2087 devRead->misc.driverInfo.version = cpu_to_le32(
2088 VMXNET3_DRIVER_VERSION_NUM);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002089 devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2090 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2091 devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002092 *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2093 *((u32 *)&devRead->misc.driverInfo.gos));
2094 devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2095 devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002096
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002097 devRead->misc.ddPA = cpu_to_le64(virt_to_phys(adapter));
2098 devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002099
2100 /* set up feature flags */
Michał Mirosława0d27302011-04-18 13:31:21 +00002101 if (adapter->netdev->features & NETIF_F_RXCSUM)
Harvey Harrison3843e512010-10-21 18:05:32 +00002102 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002103
Michał Mirosława0d27302011-04-18 13:31:21 +00002104 if (adapter->netdev->features & NETIF_F_LRO) {
Harvey Harrison3843e512010-10-21 18:05:32 +00002105 devRead->misc.uptFeatures |= UPT1_F_LRO;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002106 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002107 }
Shreyas Bhatewara54da3d02011-01-14 14:59:36 +00002108 if (adapter->netdev->features & NETIF_F_HW_VLAN_RX)
Harvey Harrison3843e512010-10-21 18:05:32 +00002109 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002110
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002111 devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2112 devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2113 devRead->misc.queueDescLen = cpu_to_le32(
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002114 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2115 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002116
2117 /* tx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002118 devRead->misc.numTxQueues = adapter->num_tx_queues;
2119 for (i = 0; i < adapter->num_tx_queues; i++) {
2120 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2121 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2122 tqc = &adapter->tqd_start[i].conf;
2123 tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
2124 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2125 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
2126 tqc->ddPA = cpu_to_le64(virt_to_phys(tq->buf_info));
2127 tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
2128 tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
2129 tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
2130 tqc->ddLen = cpu_to_le32(
2131 sizeof(struct vmxnet3_tx_buf_info) *
2132 tqc->txRingSize);
2133 tqc->intrIdx = tq->comp_ring.intr_idx;
2134 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002135
2136 /* rx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002137 devRead->misc.numRxQueues = adapter->num_rx_queues;
2138 for (i = 0; i < adapter->num_rx_queues; i++) {
2139 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2140 rqc = &adapter->rqd_start[i].conf;
2141 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2142 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2143 rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
2144 rqc->ddPA = cpu_to_le64(virt_to_phys(
2145 rq->buf_info));
2146 rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
2147 rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
2148 rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
2149 rqc->ddLen = cpu_to_le32(
2150 sizeof(struct vmxnet3_rx_buf_info) *
2151 (rqc->rxRingSize[0] +
2152 rqc->rxRingSize[1]));
2153 rqc->intrIdx = rq->comp_ring.intr_idx;
2154 }
2155
2156#ifdef VMXNET3_RSS
2157 memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2158
2159 if (adapter->rss) {
2160 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
2161 devRead->misc.uptFeatures |= UPT1_F_RSS;
2162 devRead->misc.numRxQueues = adapter->num_rx_queues;
2163 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2164 UPT1_RSS_HASH_TYPE_IPV4 |
2165 UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2166 UPT1_RSS_HASH_TYPE_IPV6;
2167 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2168 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2169 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
2170 get_random_bytes(&rssConf->hashKey[0], rssConf->hashKeySize);
2171 for (i = 0; i < rssConf->indTableSize; i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00002172 rssConf->indTable[i] = ethtool_rxfh_indir_default(
2173 i, adapter->num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002174
2175 devRead->rssConfDesc.confVer = 1;
2176 devRead->rssConfDesc.confLen = sizeof(*rssConf);
2177 devRead->rssConfDesc.confPA = virt_to_phys(rssConf);
2178 }
2179
2180#endif /* VMXNET3_RSS */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002181
2182 /* intr settings */
2183 devRead->intrConf.autoMask = adapter->intr.mask_mode ==
2184 VMXNET3_IMM_AUTO;
2185 devRead->intrConf.numIntrs = adapter->intr.num_intrs;
2186 for (i = 0; i < adapter->intr.num_intrs; i++)
2187 devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
2188
2189 devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
Ronghua Zang6929fe82010-07-15 22:18:47 -07002190 devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002191
2192 /* rx filter settings */
2193 devRead->rxFilterConf.rxMode = 0;
2194 vmxnet3_restore_vlan(adapter);
Shreyas Bhatewaraf9f25022011-01-14 14:59:31 +00002195 vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
2196
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002197 /* the rest are already zeroed */
2198}
2199
2200
2201int
2202vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
2203{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002204 int err, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002205 u32 ret;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002206 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002207
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00002208 netdev_dbg(adapter->netdev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002209 " ring sizes %u %u %u\n", adapter->netdev->name,
2210 adapter->skb_buf_size, adapter->rx_buf_per_pkt,
2211 adapter->tx_queue[0].tx_ring.size,
2212 adapter->rx_queue[0].rx_ring[0].size,
2213 adapter->rx_queue[0].rx_ring[1].size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002214
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002215 vmxnet3_tq_init_all(adapter);
2216 err = vmxnet3_rq_init_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002217 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002218 netdev_err(adapter->netdev,
2219 "Failed to init rx queue error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002220 goto rq_err;
2221 }
2222
2223 err = vmxnet3_request_irqs(adapter);
2224 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002225 netdev_err(adapter->netdev,
2226 "Failed to setup irq for error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002227 goto irq_err;
2228 }
2229
2230 vmxnet3_setup_driver_shared(adapter);
2231
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002232 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
2233 adapter->shared_pa));
2234 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
2235 adapter->shared_pa));
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002236 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002237 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2238 VMXNET3_CMD_ACTIVATE_DEV);
2239 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002240 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002241
2242 if (ret != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002243 netdev_err(adapter->netdev,
2244 "Failed to activate dev: error %u\n", ret);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002245 err = -EINVAL;
2246 goto activate_err;
2247 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002248
2249 for (i = 0; i < adapter->num_rx_queues; i++) {
2250 VMXNET3_WRITE_BAR0_REG(adapter,
2251 VMXNET3_REG_RXPROD + i * VMXNET3_REG_ALIGN,
2252 adapter->rx_queue[i].rx_ring[0].next2fill);
2253 VMXNET3_WRITE_BAR0_REG(adapter, (VMXNET3_REG_RXPROD2 +
2254 (i * VMXNET3_REG_ALIGN)),
2255 adapter->rx_queue[i].rx_ring[1].next2fill);
2256 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002257
2258 /* Apply the rx filter settins last. */
2259 vmxnet3_set_mc(adapter->netdev);
2260
2261 /*
2262 * Check link state when first activating device. It will start the
2263 * tx queue if the link is up.
2264 */
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00002265 vmxnet3_check_link(adapter, true);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002266 for (i = 0; i < adapter->num_rx_queues; i++)
2267 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002268 vmxnet3_enable_all_intrs(adapter);
2269 clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
2270 return 0;
2271
2272activate_err:
2273 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
2274 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
2275 vmxnet3_free_irqs(adapter);
2276irq_err:
2277rq_err:
2278 /* free up buffers we allocated */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002279 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002280 return err;
2281}
2282
2283
2284void
2285vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
2286{
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002287 unsigned long flags;
2288 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002289 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002290 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002291}
2292
2293
2294int
2295vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
2296{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002297 int i;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002298 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002299 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
2300 return 0;
2301
2302
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002303 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002304 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2305 VMXNET3_CMD_QUIESCE_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002306 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002307 vmxnet3_disable_all_intrs(adapter);
2308
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002309 for (i = 0; i < adapter->num_rx_queues; i++)
2310 napi_disable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002311 netif_tx_disable(adapter->netdev);
2312 adapter->link_speed = 0;
2313 netif_carrier_off(adapter->netdev);
2314
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002315 vmxnet3_tq_cleanup_all(adapter);
2316 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002317 vmxnet3_free_irqs(adapter);
2318 return 0;
2319}
2320
2321
2322static void
2323vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2324{
2325 u32 tmp;
2326
2327 tmp = *(u32 *)mac;
2328 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
2329
2330 tmp = (mac[5] << 8) | mac[4];
2331 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
2332}
2333
2334
2335static int
2336vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
2337{
2338 struct sockaddr *addr = p;
2339 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2340
2341 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2342 vmxnet3_write_mac_addr(adapter, addr->sa_data);
2343
2344 return 0;
2345}
2346
2347
2348/* ==================== initialization and cleanup routines ============ */
2349
2350static int
2351vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
2352{
2353 int err;
2354 unsigned long mmio_start, mmio_len;
2355 struct pci_dev *pdev = adapter->pdev;
2356
2357 err = pci_enable_device(pdev);
2358 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002359 dev_err(&pdev->dev, "Failed to enable adapter: error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002360 return err;
2361 }
2362
2363 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
2364 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002365 dev_err(&pdev->dev,
2366 "pci_set_consistent_dma_mask failed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002367 err = -EIO;
2368 goto err_set_mask;
2369 }
2370 *dma64 = true;
2371 } else {
2372 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002373 dev_err(&pdev->dev,
2374 "pci_set_dma_mask failed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002375 err = -EIO;
2376 goto err_set_mask;
2377 }
2378 *dma64 = false;
2379 }
2380
2381 err = pci_request_selected_regions(pdev, (1 << 2) - 1,
2382 vmxnet3_driver_name);
2383 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002384 dev_err(&pdev->dev,
2385 "Failed to request region for adapter: error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002386 goto err_set_mask;
2387 }
2388
2389 pci_set_master(pdev);
2390
2391 mmio_start = pci_resource_start(pdev, 0);
2392 mmio_len = pci_resource_len(pdev, 0);
2393 adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
2394 if (!adapter->hw_addr0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002395 dev_err(&pdev->dev, "Failed to map bar0\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002396 err = -EIO;
2397 goto err_ioremap;
2398 }
2399
2400 mmio_start = pci_resource_start(pdev, 1);
2401 mmio_len = pci_resource_len(pdev, 1);
2402 adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
2403 if (!adapter->hw_addr1) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002404 dev_err(&pdev->dev, "Failed to map bar1\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002405 err = -EIO;
2406 goto err_bar1;
2407 }
2408 return 0;
2409
2410err_bar1:
2411 iounmap(adapter->hw_addr0);
2412err_ioremap:
2413 pci_release_selected_regions(pdev, (1 << 2) - 1);
2414err_set_mask:
2415 pci_disable_device(pdev);
2416 return err;
2417}
2418
2419
2420static void
2421vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
2422{
2423 BUG_ON(!adapter->pdev);
2424
2425 iounmap(adapter->hw_addr0);
2426 iounmap(adapter->hw_addr1);
2427 pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
2428 pci_disable_device(adapter->pdev);
2429}
2430
2431
2432static void
2433vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
2434{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002435 size_t sz, i, ring0_size, ring1_size, comp_size;
2436 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[0];
2437
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002438
2439 if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
2440 VMXNET3_MAX_ETH_HDR_SIZE) {
2441 adapter->skb_buf_size = adapter->netdev->mtu +
2442 VMXNET3_MAX_ETH_HDR_SIZE;
2443 if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
2444 adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
2445
2446 adapter->rx_buf_per_pkt = 1;
2447 } else {
2448 adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
2449 sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
2450 VMXNET3_MAX_ETH_HDR_SIZE;
2451 adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
2452 }
2453
2454 /*
2455 * for simplicity, force the ring0 size to be a multiple of
2456 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
2457 */
2458 sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002459 ring0_size = adapter->rx_queue[0].rx_ring[0].size;
2460 ring0_size = (ring0_size + sz - 1) / sz * sz;
Shreyas Bhatewaraa53255d2011-01-14 14:59:25 +00002461 ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002462 sz * sz);
2463 ring1_size = adapter->rx_queue[0].rx_ring[1].size;
2464 comp_size = ring0_size + ring1_size;
2465
2466 for (i = 0; i < adapter->num_rx_queues; i++) {
2467 rq = &adapter->rx_queue[i];
2468 rq->rx_ring[0].size = ring0_size;
2469 rq->rx_ring[1].size = ring1_size;
2470 rq->comp_ring.size = comp_size;
2471 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002472}
2473
2474
2475int
2476vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
2477 u32 rx_ring_size, u32 rx_ring2_size)
2478{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002479 int err = 0, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002480
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002481 for (i = 0; i < adapter->num_tx_queues; i++) {
2482 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2483 tq->tx_ring.size = tx_ring_size;
2484 tq->data_ring.size = tx_ring_size;
2485 tq->comp_ring.size = tx_ring_size;
2486 tq->shared = &adapter->tqd_start[i].ctrl;
2487 tq->stopped = true;
2488 tq->adapter = adapter;
2489 tq->qid = i;
2490 err = vmxnet3_tq_create(tq, adapter);
2491 /*
2492 * Too late to change num_tx_queues. We cannot do away with
2493 * lesser number of queues than what we asked for
2494 */
2495 if (err)
2496 goto queue_err;
2497 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002498
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002499 adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
2500 adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002501 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002502 for (i = 0; i < adapter->num_rx_queues; i++) {
2503 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2504 /* qid and qid2 for rx queues will be assigned later when num
2505 * of rx queues is finalized after allocating intrs */
2506 rq->shared = &adapter->rqd_start[i].ctrl;
2507 rq->adapter = adapter;
2508 err = vmxnet3_rq_create(rq, adapter);
2509 if (err) {
2510 if (i == 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002511 netdev_err(adapter->netdev,
2512 "Could not allocate any rx queues. "
2513 "Aborting.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002514 goto queue_err;
2515 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002516 netdev_info(adapter->netdev,
2517 "Number of rx queues changed "
2518 "to : %d.\n", i);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002519 adapter->num_rx_queues = i;
2520 err = 0;
2521 break;
2522 }
2523 }
2524 }
2525 return err;
2526queue_err:
2527 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002528 return err;
2529}
2530
2531static int
2532vmxnet3_open(struct net_device *netdev)
2533{
2534 struct vmxnet3_adapter *adapter;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002535 int err, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002536
2537 adapter = netdev_priv(netdev);
2538
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002539 for (i = 0; i < adapter->num_tx_queues; i++)
2540 spin_lock_init(&adapter->tx_queue[i].tx_lock);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002541
2542 err = vmxnet3_create_queues(adapter, VMXNET3_DEF_TX_RING_SIZE,
2543 VMXNET3_DEF_RX_RING_SIZE,
2544 VMXNET3_DEF_RX_RING_SIZE);
2545 if (err)
2546 goto queue_err;
2547
2548 err = vmxnet3_activate_dev(adapter);
2549 if (err)
2550 goto activate_err;
2551
2552 return 0;
2553
2554activate_err:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002555 vmxnet3_rq_destroy_all(adapter);
2556 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002557queue_err:
2558 return err;
2559}
2560
2561
2562static int
2563vmxnet3_close(struct net_device *netdev)
2564{
2565 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2566
2567 /*
2568 * Reset_work may be in the middle of resetting the device, wait for its
2569 * completion.
2570 */
2571 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2572 msleep(1);
2573
2574 vmxnet3_quiesce_dev(adapter);
2575
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002576 vmxnet3_rq_destroy_all(adapter);
2577 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002578
2579 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2580
2581
2582 return 0;
2583}
2584
2585
2586void
2587vmxnet3_force_close(struct vmxnet3_adapter *adapter)
2588{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002589 int i;
2590
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002591 /*
2592 * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
2593 * vmxnet3_close() will deadlock.
2594 */
2595 BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
2596
2597 /* we need to enable NAPI, otherwise dev_close will deadlock */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002598 for (i = 0; i < adapter->num_rx_queues; i++)
2599 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002600 dev_close(adapter->netdev);
2601}
2602
2603
2604static int
2605vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
2606{
2607 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2608 int err = 0;
2609
2610 if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
2611 return -EINVAL;
2612
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002613 netdev->mtu = new_mtu;
2614
2615 /*
2616 * Reset_work may be in the middle of resetting the device, wait for its
2617 * completion.
2618 */
2619 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2620 msleep(1);
2621
2622 if (netif_running(netdev)) {
2623 vmxnet3_quiesce_dev(adapter);
2624 vmxnet3_reset_dev(adapter);
2625
2626 /* we need to re-create the rx queue based on the new mtu */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002627 vmxnet3_rq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002628 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002629 err = vmxnet3_rq_create_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002630 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002631 netdev_err(netdev,
2632 "failed to re-create rx queues, "
2633 " error %d. Closing it.\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002634 goto out;
2635 }
2636
2637 err = vmxnet3_activate_dev(adapter);
2638 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002639 netdev_err(netdev,
2640 "failed to re-activate, error %d. "
2641 "Closing it\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002642 goto out;
2643 }
2644 }
2645
2646out:
2647 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2648 if (err)
2649 vmxnet3_force_close(adapter);
2650
2651 return err;
2652}
2653
2654
2655static void
2656vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
2657{
2658 struct net_device *netdev = adapter->netdev;
2659
Michał Mirosława0d27302011-04-18 13:31:21 +00002660 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
2661 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX |
Jesse Gross72e85c42011-06-23 13:04:39 +00002662 NETIF_F_HW_VLAN_RX | NETIF_F_TSO | NETIF_F_TSO6 |
2663 NETIF_F_LRO;
Michał Mirosława0d27302011-04-18 13:31:21 +00002664 if (dma64)
Shreyas Bhatewaraebbf9292011-07-20 17:21:51 +00002665 netdev->hw_features |= NETIF_F_HIGHDMA;
Jesse Gross72e85c42011-06-23 13:04:39 +00002666 netdev->vlan_features = netdev->hw_features &
2667 ~(NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
2668 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002669}
2670
2671
2672static void
2673vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2674{
2675 u32 tmp;
2676
2677 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
2678 *(u32 *)mac = tmp;
2679
2680 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
2681 mac[4] = tmp & 0xff;
2682 mac[5] = (tmp >> 8) & 0xff;
2683}
2684
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002685#ifdef CONFIG_PCI_MSI
2686
2687/*
2688 * Enable MSIx vectors.
2689 * Returns :
2690 * 0 on successful enabling of required vectors,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002691 * VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002692 * could be enabled.
2693 * number of vectors which can be enabled otherwise (this number is smaller
2694 * than VMXNET3_LINUX_MIN_MSIX_VECT)
2695 */
2696
2697static int
2698vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter,
2699 int vectors)
2700{
2701 int err = 0, vector_threshold;
2702 vector_threshold = VMXNET3_LINUX_MIN_MSIX_VECT;
2703
2704 while (vectors >= vector_threshold) {
2705 err = pci_enable_msix(adapter->pdev, adapter->intr.msix_entries,
2706 vectors);
2707 if (!err) {
2708 adapter->intr.num_intrs = vectors;
2709 return 0;
2710 } else if (err < 0) {
Stephen Hemminger4bad25f2013-01-15 07:28:28 +00002711 dev_err(&adapter->netdev->dev,
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002712 "Failed to enable MSI-X, error: %d\n", err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002713 vectors = 0;
2714 } else if (err < vector_threshold) {
2715 break;
2716 } else {
2717 /* If fails to enable required number of MSI-x vectors
Shreyas Bhatewara7e96fbf2011-01-14 15:00:03 +00002718 * try enabling minimum number of vectors required.
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002719 */
Stephen Hemminger4bad25f2013-01-15 07:28:28 +00002720 dev_err(&adapter->netdev->dev,
2721 "Failed to enable %d MSI-X, trying %d instead\n",
Shreyas Bhatewara4c1dc802012-02-28 22:08:39 +00002722 vectors, vector_threshold);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002723 vectors = vector_threshold;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002724 }
2725 }
2726
Stephen Hemminger4bad25f2013-01-15 07:28:28 +00002727 dev_info(&adapter->pdev->dev,
2728 "Number of MSI-X interrupts which can be allocated "
2729 "is lower than min threshold required.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002730 return err;
2731}
2732
2733
2734#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002735
2736static void
2737vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
2738{
2739 u32 cfg;
Roland Dreiere328d412011-05-06 08:32:53 +00002740 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002741
2742 /* intr settings */
Roland Dreiere328d412011-05-06 08:32:53 +00002743 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002744 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2745 VMXNET3_CMD_GET_CONF_INTR);
2746 cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Roland Dreiere328d412011-05-06 08:32:53 +00002747 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002748 adapter->intr.type = cfg & 0x3;
2749 adapter->intr.mask_mode = (cfg >> 2) & 0x3;
2750
2751 if (adapter->intr.type == VMXNET3_IT_AUTO) {
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002752 adapter->intr.type = VMXNET3_IT_MSIX;
2753 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002754
Randy Dunlap8f7e5242009-10-14 20:38:58 -07002755#ifdef CONFIG_PCI_MSI
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002756 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002757 int vector, err = 0;
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002758
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002759 adapter->intr.num_intrs = (adapter->share_intr ==
2760 VMXNET3_INTR_TXSHARE) ? 1 :
2761 adapter->num_tx_queues;
2762 adapter->intr.num_intrs += (adapter->share_intr ==
2763 VMXNET3_INTR_BUDDYSHARE) ? 0 :
2764 adapter->num_rx_queues;
2765 adapter->intr.num_intrs += 1; /* for link event */
2766
2767 adapter->intr.num_intrs = (adapter->intr.num_intrs >
2768 VMXNET3_LINUX_MIN_MSIX_VECT
2769 ? adapter->intr.num_intrs :
2770 VMXNET3_LINUX_MIN_MSIX_VECT);
2771
2772 for (vector = 0; vector < adapter->intr.num_intrs; vector++)
2773 adapter->intr.msix_entries[vector].entry = vector;
2774
2775 err = vmxnet3_acquire_msix_vectors(adapter,
2776 adapter->intr.num_intrs);
2777 /* If we cannot allocate one MSIx vector per queue
2778 * then limit the number of rx queues to 1
2779 */
2780 if (err == VMXNET3_LINUX_MIN_MSIX_VECT) {
2781 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
Shreyas Bhatewara7e96fbf2011-01-14 15:00:03 +00002782 || adapter->num_rx_queues != 1) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002783 adapter->share_intr = VMXNET3_INTR_TXSHARE;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002784 netdev_err(adapter->netdev,
2785 "Number of rx queues : 1\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002786 adapter->num_rx_queues = 1;
2787 adapter->intr.num_intrs =
2788 VMXNET3_LINUX_MIN_MSIX_VECT;
2789 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002790 return;
2791 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002792 if (!err)
2793 return;
2794
2795 /* If we cannot allocate MSIx vectors use only one rx queue */
Stephen Hemminger4bad25f2013-01-15 07:28:28 +00002796 dev_info(&adapter->pdev->dev,
2797 "Failed to enable MSI-X, error %d. "
2798 "Limiting #rx queues to 1, try MSI.\n", err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002799
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002800 adapter->intr.type = VMXNET3_IT_MSI;
2801 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002802
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002803 if (adapter->intr.type == VMXNET3_IT_MSI) {
2804 int err;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002805 err = pci_enable_msi(adapter->pdev);
2806 if (!err) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002807 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002808 adapter->intr.num_intrs = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002809 return;
2810 }
2811 }
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002812#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002813
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002814 adapter->num_rx_queues = 1;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002815 dev_info(&adapter->netdev->dev,
2816 "Using INTx interrupt, #Rx queues: 1.\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002817 adapter->intr.type = VMXNET3_IT_INTX;
2818
2819 /* INT-X related setting */
2820 adapter->intr.num_intrs = 1;
2821}
2822
2823
2824static void
2825vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
2826{
2827 if (adapter->intr.type == VMXNET3_IT_MSIX)
2828 pci_disable_msix(adapter->pdev);
2829 else if (adapter->intr.type == VMXNET3_IT_MSI)
2830 pci_disable_msi(adapter->pdev);
2831 else
2832 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
2833}
2834
2835
2836static void
2837vmxnet3_tx_timeout(struct net_device *netdev)
2838{
2839 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2840 adapter->tx_timeout_count++;
2841
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002842 netdev_err(adapter->netdev, "tx hang\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002843 schedule_work(&adapter->work);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002844 netif_wake_queue(adapter->netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002845}
2846
2847
2848static void
2849vmxnet3_reset_work(struct work_struct *data)
2850{
2851 struct vmxnet3_adapter *adapter;
2852
2853 adapter = container_of(data, struct vmxnet3_adapter, work);
2854
2855 /* if another thread is resetting the device, no need to proceed */
2856 if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2857 return;
2858
2859 /* if the device is closed, we must leave it alone */
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002860 rtnl_lock();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002861 if (netif_running(adapter->netdev)) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002862 netdev_notice(adapter->netdev, "resetting\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002863 vmxnet3_quiesce_dev(adapter);
2864 vmxnet3_reset_dev(adapter);
2865 vmxnet3_activate_dev(adapter);
2866 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002867 netdev_info(adapter->netdev, "already closed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002868 }
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002869 rtnl_unlock();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002870
2871 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2872}
2873
2874
Bill Pemberton3a4751a2012-12-03 09:24:16 -05002875static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002876vmxnet3_probe_device(struct pci_dev *pdev,
2877 const struct pci_device_id *id)
2878{
2879 static const struct net_device_ops vmxnet3_netdev_ops = {
2880 .ndo_open = vmxnet3_open,
2881 .ndo_stop = vmxnet3_close,
2882 .ndo_start_xmit = vmxnet3_xmit_frame,
2883 .ndo_set_mac_address = vmxnet3_set_mac_addr,
2884 .ndo_change_mtu = vmxnet3_change_mtu,
Michał Mirosława0d27302011-04-18 13:31:21 +00002885 .ndo_set_features = vmxnet3_set_features,
stephen hemminger95305f62011-06-08 14:53:57 +00002886 .ndo_get_stats64 = vmxnet3_get_stats64,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002887 .ndo_tx_timeout = vmxnet3_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002888 .ndo_set_rx_mode = vmxnet3_set_mc,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002889 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
2890 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
2891#ifdef CONFIG_NET_POLL_CONTROLLER
2892 .ndo_poll_controller = vmxnet3_netpoll,
2893#endif
2894 };
2895 int err;
2896 bool dma64 = false; /* stupid gcc */
2897 u32 ver;
2898 struct net_device *netdev;
2899 struct vmxnet3_adapter *adapter;
2900 u8 mac[ETH_ALEN];
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002901 int size;
2902 int num_tx_queues;
2903 int num_rx_queues;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002904
Shreyas Bhatewarae154b632011-05-10 06:13:56 +00002905 if (!pci_msi_enabled())
2906 enable_mq = 0;
2907
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002908#ifdef VMXNET3_RSS
2909 if (enable_mq)
2910 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
2911 (int)num_online_cpus());
2912 else
2913#endif
2914 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002915 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002916
2917 if (enable_mq)
2918 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
2919 (int)num_online_cpus());
2920 else
2921 num_tx_queues = 1;
2922
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002923 num_tx_queues = rounddown_pow_of_two(num_tx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002924 netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
2925 max(num_tx_queues, num_rx_queues));
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002926 dev_info(&pdev->dev,
2927 "# of Tx queues : %d, # of Rx queues : %d\n",
2928 num_tx_queues, num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002929
Joe Perches41de8d42012-01-29 13:47:52 +00002930 if (!netdev)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002931 return -ENOMEM;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002932
2933 pci_set_drvdata(pdev, netdev);
2934 adapter = netdev_priv(netdev);
2935 adapter->netdev = netdev;
2936 adapter->pdev = pdev;
2937
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002938 spin_lock_init(&adapter->cmd_lock);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002939 adapter->shared = pci_alloc_consistent(adapter->pdev,
stephen hemminger96800ee2012-11-13 13:53:28 +00002940 sizeof(struct Vmxnet3_DriverShared),
2941 &adapter->shared_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002942 if (!adapter->shared) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002943 dev_err(&pdev->dev, "Failed to allocate memory\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002944 err = -ENOMEM;
2945 goto err_alloc_shared;
2946 }
2947
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002948 adapter->num_rx_queues = num_rx_queues;
2949 adapter->num_tx_queues = num_tx_queues;
2950
2951 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
2952 size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
2953 adapter->tqd_start = pci_alloc_consistent(adapter->pdev, size,
stephen hemminger96800ee2012-11-13 13:53:28 +00002954 &adapter->queue_desc_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002955
2956 if (!adapter->tqd_start) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002957 dev_err(&pdev->dev, "Failed to allocate memory\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002958 err = -ENOMEM;
2959 goto err_alloc_queue_desc;
2960 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002961 adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
stephen hemminger96800ee2012-11-13 13:53:28 +00002962 adapter->num_tx_queues);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002963
2964 adapter->pm_conf = kmalloc(sizeof(struct Vmxnet3_PMConf), GFP_KERNEL);
2965 if (adapter->pm_conf == NULL) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002966 err = -ENOMEM;
2967 goto err_alloc_pm;
2968 }
2969
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002970#ifdef VMXNET3_RSS
2971
2972 adapter->rss_conf = kmalloc(sizeof(struct UPT1_RSSConf), GFP_KERNEL);
2973 if (adapter->rss_conf == NULL) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002974 err = -ENOMEM;
2975 goto err_alloc_rss;
2976 }
2977#endif /* VMXNET3_RSS */
2978
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002979 err = vmxnet3_alloc_pci_resources(adapter, &dma64);
2980 if (err < 0)
2981 goto err_alloc_pci;
2982
2983 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
2984 if (ver & 1) {
2985 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
2986 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002987 dev_err(&pdev->dev,
2988 "Incompatible h/w version (0x%x) for adapter\n", ver);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002989 err = -EBUSY;
2990 goto err_ver;
2991 }
2992
2993 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
2994 if (ver & 1) {
2995 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
2996 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002997 dev_err(&pdev->dev,
2998 "Incompatible upt version (0x%x) for adapter\n", ver);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002999 err = -EBUSY;
3000 goto err_ver;
3001 }
3002
Shreyas Bhatewarae101e7d2011-07-20 16:01:11 +00003003 SET_NETDEV_DEV(netdev, &pdev->dev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003004 vmxnet3_declare_features(adapter, dma64);
3005
stephen hemminger96800ee2012-11-13 13:53:28 +00003006 adapter->share_intr = irq_share_mode;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003007 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE &&
3008 adapter->num_tx_queues != adapter->num_rx_queues)
3009 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
3010
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003011 vmxnet3_alloc_intr_resources(adapter);
3012
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003013#ifdef VMXNET3_RSS
3014 if (adapter->num_rx_queues > 1 &&
3015 adapter->intr.type == VMXNET3_IT_MSIX) {
3016 adapter->rss = true;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003017 dev_dbg(&pdev->dev, "RSS is enabled.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003018 } else {
3019 adapter->rss = false;
3020 }
3021#endif
3022
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003023 vmxnet3_read_mac_addr(adapter, mac);
3024 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3025
3026 netdev->netdev_ops = &vmxnet3_netdev_ops;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003027 vmxnet3_set_ethtool_ops(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003028 netdev->watchdog_timeo = 5 * HZ;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003029
3030 INIT_WORK(&adapter->work, vmxnet3_reset_work);
Steve Hodgsone3bc4ff2012-08-14 17:13:36 +01003031 set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003032
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003033 if (adapter->intr.type == VMXNET3_IT_MSIX) {
3034 int i;
3035 for (i = 0; i < adapter->num_rx_queues; i++) {
3036 netif_napi_add(adapter->netdev,
3037 &adapter->rx_queue[i].napi,
3038 vmxnet3_poll_rx_only, 64);
3039 }
3040 } else {
3041 netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
3042 vmxnet3_poll, 64);
3043 }
3044
3045 netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
3046 netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
3047
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003048 err = register_netdev(netdev);
3049
3050 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003051 dev_err(&pdev->dev, "Failed to register adapter\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003052 goto err_register;
3053 }
3054
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00003055 vmxnet3_check_link(adapter, false);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003056 return 0;
3057
3058err_register:
3059 vmxnet3_free_intr_resources(adapter);
3060err_ver:
3061 vmxnet3_free_pci_resources(adapter);
3062err_alloc_pci:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003063#ifdef VMXNET3_RSS
3064 kfree(adapter->rss_conf);
3065err_alloc_rss:
3066#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003067 kfree(adapter->pm_conf);
3068err_alloc_pm:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003069 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3070 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003071err_alloc_queue_desc:
3072 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3073 adapter->shared, adapter->shared_pa);
3074err_alloc_shared:
3075 pci_set_drvdata(pdev, NULL);
3076 free_netdev(netdev);
3077 return err;
3078}
3079
3080
Bill Pemberton3a4751a2012-12-03 09:24:16 -05003081static void
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003082vmxnet3_remove_device(struct pci_dev *pdev)
3083{
3084 struct net_device *netdev = pci_get_drvdata(pdev);
3085 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003086 int size = 0;
3087 int num_rx_queues;
3088
3089#ifdef VMXNET3_RSS
3090 if (enable_mq)
3091 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3092 (int)num_online_cpus());
3093 else
3094#endif
3095 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07003096 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003097
Tejun Heo23f333a2010-12-12 16:45:14 +01003098 cancel_work_sync(&adapter->work);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003099
3100 unregister_netdev(netdev);
3101
3102 vmxnet3_free_intr_resources(adapter);
3103 vmxnet3_free_pci_resources(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003104#ifdef VMXNET3_RSS
3105 kfree(adapter->rss_conf);
3106#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003107 kfree(adapter->pm_conf);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003108
3109 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3110 size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
3111 pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
3112 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003113 pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
3114 adapter->shared, adapter->shared_pa);
3115 free_netdev(netdev);
3116}
3117
3118
3119#ifdef CONFIG_PM
3120
3121static int
3122vmxnet3_suspend(struct device *device)
3123{
3124 struct pci_dev *pdev = to_pci_dev(device);
3125 struct net_device *netdev = pci_get_drvdata(pdev);
3126 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3127 struct Vmxnet3_PMConf *pmConf;
3128 struct ethhdr *ehdr;
3129 struct arphdr *ahdr;
3130 u8 *arpreq;
3131 struct in_device *in_dev;
3132 struct in_ifaddr *ifa;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003133 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003134 int i = 0;
3135
3136 if (!netif_running(netdev))
3137 return 0;
3138
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003139 for (i = 0; i < adapter->num_rx_queues; i++)
3140 napi_disable(&adapter->rx_queue[i].napi);
3141
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003142 vmxnet3_disable_all_intrs(adapter);
3143 vmxnet3_free_irqs(adapter);
3144 vmxnet3_free_intr_resources(adapter);
3145
3146 netif_device_detach(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003147 netif_tx_stop_all_queues(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003148
3149 /* Create wake-up filters. */
3150 pmConf = adapter->pm_conf;
3151 memset(pmConf, 0, sizeof(*pmConf));
3152
3153 if (adapter->wol & WAKE_UCAST) {
3154 pmConf->filters[i].patternSize = ETH_ALEN;
3155 pmConf->filters[i].maskSize = 1;
3156 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
3157 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
3158
Harvey Harrison3843e512010-10-21 18:05:32 +00003159 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003160 i++;
3161 }
3162
3163 if (adapter->wol & WAKE_ARP) {
3164 in_dev = in_dev_get(netdev);
3165 if (!in_dev)
3166 goto skip_arp;
3167
3168 ifa = (struct in_ifaddr *)in_dev->ifa_list;
3169 if (!ifa)
3170 goto skip_arp;
3171
3172 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
3173 sizeof(struct arphdr) + /* ARP header */
3174 2 * ETH_ALEN + /* 2 Ethernet addresses*/
3175 2 * sizeof(u32); /*2 IPv4 addresses */
3176 pmConf->filters[i].maskSize =
3177 (pmConf->filters[i].patternSize - 1) / 8 + 1;
3178
3179 /* ETH_P_ARP in Ethernet header. */
3180 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
3181 ehdr->h_proto = htons(ETH_P_ARP);
3182
3183 /* ARPOP_REQUEST in ARP header. */
3184 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
3185 ahdr->ar_op = htons(ARPOP_REQUEST);
3186 arpreq = (u8 *)(ahdr + 1);
3187
3188 /* The Unicast IPv4 address in 'tip' field. */
3189 arpreq += 2 * ETH_ALEN + sizeof(u32);
3190 *(u32 *)arpreq = ifa->ifa_address;
3191
3192 /* The mask for the relevant bits. */
3193 pmConf->filters[i].mask[0] = 0x00;
3194 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
3195 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
3196 pmConf->filters[i].mask[3] = 0x00;
3197 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
3198 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
3199 in_dev_put(in_dev);
3200
Harvey Harrison3843e512010-10-21 18:05:32 +00003201 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003202 i++;
3203 }
3204
3205skip_arp:
3206 if (adapter->wol & WAKE_MAGIC)
Harvey Harrison3843e512010-10-21 18:05:32 +00003207 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003208
3209 pmConf->numFilters = i;
3210
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003211 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3212 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3213 *pmConf));
3214 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
3215 pmConf));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003216
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003217 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003218 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3219 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003220 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003221
3222 pci_save_state(pdev);
3223 pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
3224 adapter->wol);
3225 pci_disable_device(pdev);
3226 pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
3227
3228 return 0;
3229}
3230
3231
3232static int
3233vmxnet3_resume(struct device *device)
3234{
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003235 int err, i = 0;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003236 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003237 struct pci_dev *pdev = to_pci_dev(device);
3238 struct net_device *netdev = pci_get_drvdata(pdev);
3239 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3240 struct Vmxnet3_PMConf *pmConf;
3241
3242 if (!netif_running(netdev))
3243 return 0;
3244
3245 /* Destroy wake-up filters. */
3246 pmConf = adapter->pm_conf;
3247 memset(pmConf, 0, sizeof(*pmConf));
3248
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003249 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3250 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3251 *pmConf));
Harvey Harrison0561cf32010-10-21 18:05:34 +00003252 adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003253 pmConf));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003254
3255 netif_device_attach(netdev);
3256 pci_set_power_state(pdev, PCI_D0);
3257 pci_restore_state(pdev);
3258 err = pci_enable_device_mem(pdev);
3259 if (err != 0)
3260 return err;
3261
3262 pci_enable_wake(pdev, PCI_D0, 0);
3263
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003264 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003265 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3266 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003267 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003268 vmxnet3_alloc_intr_resources(adapter);
3269 vmxnet3_request_irqs(adapter);
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003270 for (i = 0; i < adapter->num_rx_queues; i++)
3271 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003272 vmxnet3_enable_all_intrs(adapter);
3273
3274 return 0;
3275}
3276
Alexey Dobriyan47145212009-12-14 18:00:08 -08003277static const struct dev_pm_ops vmxnet3_pm_ops = {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003278 .suspend = vmxnet3_suspend,
3279 .resume = vmxnet3_resume,
3280};
3281#endif
3282
3283static struct pci_driver vmxnet3_driver = {
3284 .name = vmxnet3_driver_name,
3285 .id_table = vmxnet3_pciid_table,
3286 .probe = vmxnet3_probe_device,
Bill Pemberton3a4751a2012-12-03 09:24:16 -05003287 .remove = vmxnet3_remove_device,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003288#ifdef CONFIG_PM
3289 .driver.pm = &vmxnet3_pm_ops,
3290#endif
3291};
3292
3293
3294static int __init
3295vmxnet3_init_module(void)
3296{
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003297 pr_info("%s - version %s\n", VMXNET3_DRIVER_DESC,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003298 VMXNET3_DRIVER_VERSION_REPORT);
3299 return pci_register_driver(&vmxnet3_driver);
3300}
3301
3302module_init(vmxnet3_init_module);
3303
3304
3305static void
3306vmxnet3_exit_module(void)
3307{
3308 pci_unregister_driver(&vmxnet3_driver);
3309}
3310
3311module_exit(vmxnet3_exit_module);
3312
3313MODULE_AUTHOR("VMware, Inc.");
3314MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
3315MODULE_LICENSE("GPL v2");
3316MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);