blob: ab539758bec1c6f095c16d3e1ec007f18dfeffd9 [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 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020039static const struct pci_device_id 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;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -070047
Shreyas Bhatewaraf9f25022011-01-14 14:59:31 +000048static void
49vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac);
50
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -070051/*
52 * Enable/Disable the given intr
53 */
54static void
55vmxnet3_enable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
56{
57 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 0);
58}
59
60
61static void
62vmxnet3_disable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
63{
64 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 1);
65}
66
67
68/*
69 * Enable/Disable all intrs used by the device
70 */
71static void
72vmxnet3_enable_all_intrs(struct vmxnet3_adapter *adapter)
73{
74 int i;
75
76 for (i = 0; i < adapter->intr.num_intrs; i++)
77 vmxnet3_enable_intr(adapter, i);
Ronghua Zang6929fe82010-07-15 22:18:47 -070078 adapter->shared->devRead.intrConf.intrCtrl &=
79 cpu_to_le32(~VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -070080}
81
82
83static void
84vmxnet3_disable_all_intrs(struct vmxnet3_adapter *adapter)
85{
86 int i;
87
Ronghua Zang6929fe82010-07-15 22:18:47 -070088 adapter->shared->devRead.intrConf.intrCtrl |=
89 cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -070090 for (i = 0; i < adapter->intr.num_intrs; i++)
91 vmxnet3_disable_intr(adapter, i);
92}
93
94
95static void
96vmxnet3_ack_events(struct vmxnet3_adapter *adapter, u32 events)
97{
98 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_ECR, events);
99}
100
101
102static bool
103vmxnet3_tq_stopped(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
104{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000105 return tq->stopped;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700106}
107
108
109static void
110vmxnet3_tq_start(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
111{
112 tq->stopped = false;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000113 netif_start_subqueue(adapter->netdev, tq - adapter->tx_queue);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700114}
115
116
117static void
118vmxnet3_tq_wake(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
119{
120 tq->stopped = false;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000121 netif_wake_subqueue(adapter->netdev, (tq - adapter->tx_queue));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700122}
123
124
125static void
126vmxnet3_tq_stop(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
127{
128 tq->stopped = true;
129 tq->num_stop++;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000130 netif_stop_subqueue(adapter->netdev, (tq - adapter->tx_queue));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700131}
132
133
134/*
135 * Check the link state. This may start or stop the tx queue.
136 */
137static void
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +0000138vmxnet3_check_link(struct vmxnet3_adapter *adapter, bool affectTxQueue)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700139{
140 u32 ret;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000141 int i;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +0000142 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700143
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +0000144 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700145 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
146 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +0000147 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
148
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700149 adapter->link_speed = ret >> 16;
150 if (ret & 1) { /* Link is up. */
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000151 netdev_info(adapter->netdev, "NIC Link is Up %d Mbps\n",
152 adapter->link_speed);
Neil Horman6cdd20c2013-01-29 16:15:45 -0500153 netif_carrier_on(adapter->netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700154
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000155 if (affectTxQueue) {
156 for (i = 0; i < adapter->num_tx_queues; i++)
157 vmxnet3_tq_start(&adapter->tx_queue[i],
158 adapter);
159 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700160 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000161 netdev_info(adapter->netdev, "NIC Link is Down\n");
Neil Horman6cdd20c2013-01-29 16:15:45 -0500162 netif_carrier_off(adapter->netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700163
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000164 if (affectTxQueue) {
165 for (i = 0; i < adapter->num_tx_queues; i++)
166 vmxnet3_tq_stop(&adapter->tx_queue[i], adapter);
167 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700168 }
169}
170
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700171static void
172vmxnet3_process_events(struct vmxnet3_adapter *adapter)
173{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000174 int i;
Roland Dreiere328d412011-05-06 08:32:53 +0000175 unsigned long flags;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000176 u32 events = le32_to_cpu(adapter->shared->ecr);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700177 if (!events)
178 return;
179
180 vmxnet3_ack_events(adapter, events);
181
182 /* Check if link state has changed */
183 if (events & VMXNET3_ECR_LINK)
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +0000184 vmxnet3_check_link(adapter, true);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700185
186 /* Check if there is an error on xmit/recv queues */
187 if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
Roland Dreiere328d412011-05-06 08:32:53 +0000188 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700189 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
190 VMXNET3_CMD_GET_QUEUE_STATUS);
Roland Dreiere328d412011-05-06 08:32:53 +0000191 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700192
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000193 for (i = 0; i < adapter->num_tx_queues; i++)
194 if (adapter->tqd_start[i].status.stopped)
195 dev_err(&adapter->netdev->dev,
196 "%s: tq[%d] error 0x%x\n",
197 adapter->netdev->name, i, le32_to_cpu(
198 adapter->tqd_start[i].status.error));
199 for (i = 0; i < adapter->num_rx_queues; i++)
200 if (adapter->rqd_start[i].status.stopped)
201 dev_err(&adapter->netdev->dev,
202 "%s: rq[%d] error 0x%x\n",
203 adapter->netdev->name, i,
204 adapter->rqd_start[i].status.error);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700205
206 schedule_work(&adapter->work);
207 }
208}
209
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000210#ifdef __BIG_ENDIAN_BITFIELD
211/*
212 * The device expects the bitfields in shared structures to be written in
213 * little endian. When CPU is big endian, the following routines are used to
214 * correctly read and write into ABI.
215 * The general technique used here is : double word bitfields are defined in
216 * opposite order for big endian architecture. Then before reading them in
217 * driver the complete double word is translated using le32_to_cpu. Similarly
218 * After the driver writes into bitfields, cpu_to_le32 is used to translate the
219 * double words into required format.
220 * In order to avoid touching bits in shared structure more than once, temporary
221 * descriptors are used. These are passed as srcDesc to following functions.
222 */
223static void vmxnet3_RxDescToCPU(const struct Vmxnet3_RxDesc *srcDesc,
224 struct Vmxnet3_RxDesc *dstDesc)
225{
226 u32 *src = (u32 *)srcDesc + 2;
227 u32 *dst = (u32 *)dstDesc + 2;
228 dstDesc->addr = le64_to_cpu(srcDesc->addr);
229 *dst = le32_to_cpu(*src);
230 dstDesc->ext1 = le32_to_cpu(srcDesc->ext1);
231}
232
233static void vmxnet3_TxDescToLe(const struct Vmxnet3_TxDesc *srcDesc,
234 struct Vmxnet3_TxDesc *dstDesc)
235{
236 int i;
237 u32 *src = (u32 *)(srcDesc + 1);
238 u32 *dst = (u32 *)(dstDesc + 1);
239
240 /* Working backwards so that the gen bit is set at the end. */
241 for (i = 2; i > 0; i--) {
242 src--;
243 dst--;
244 *dst = cpu_to_le32(*src);
245 }
246}
247
248
249static void vmxnet3_RxCompToCPU(const struct Vmxnet3_RxCompDesc *srcDesc,
250 struct Vmxnet3_RxCompDesc *dstDesc)
251{
252 int i = 0;
253 u32 *src = (u32 *)srcDesc;
254 u32 *dst = (u32 *)dstDesc;
255 for (i = 0; i < sizeof(struct Vmxnet3_RxCompDesc) / sizeof(u32); i++) {
256 *dst = le32_to_cpu(*src);
257 src++;
258 dst++;
259 }
260}
261
262
263/* Used to read bitfield values from double words. */
264static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
265{
266 u32 temp = le32_to_cpu(*bitfield);
267 u32 mask = ((1 << size) - 1) << pos;
268 temp &= mask;
269 temp >>= pos;
270 return temp;
271}
272
273
274
275#endif /* __BIG_ENDIAN_BITFIELD */
276
277#ifdef __BIG_ENDIAN_BITFIELD
278
279# define VMXNET3_TXDESC_GET_GEN(txdesc) get_bitfield32(((const __le32 *) \
280 txdesc) + VMXNET3_TXD_GEN_DWORD_SHIFT, \
281 VMXNET3_TXD_GEN_SHIFT, VMXNET3_TXD_GEN_SIZE)
282# define VMXNET3_TXDESC_GET_EOP(txdesc) get_bitfield32(((const __le32 *) \
283 txdesc) + VMXNET3_TXD_EOP_DWORD_SHIFT, \
284 VMXNET3_TXD_EOP_SHIFT, VMXNET3_TXD_EOP_SIZE)
285# define VMXNET3_TCD_GET_GEN(tcd) get_bitfield32(((const __le32 *)tcd) + \
286 VMXNET3_TCD_GEN_DWORD_SHIFT, VMXNET3_TCD_GEN_SHIFT, \
287 VMXNET3_TCD_GEN_SIZE)
288# define VMXNET3_TCD_GET_TXIDX(tcd) get_bitfield32((const __le32 *)tcd, \
289 VMXNET3_TCD_TXIDX_SHIFT, VMXNET3_TCD_TXIDX_SIZE)
290# define vmxnet3_getRxComp(dstrcd, rcd, tmp) do { \
291 (dstrcd) = (tmp); \
292 vmxnet3_RxCompToCPU((rcd), (tmp)); \
293 } while (0)
294# define vmxnet3_getRxDesc(dstrxd, rxd, tmp) do { \
295 (dstrxd) = (tmp); \
296 vmxnet3_RxDescToCPU((rxd), (tmp)); \
297 } while (0)
298
299#else
300
301# define VMXNET3_TXDESC_GET_GEN(txdesc) ((txdesc)->gen)
302# define VMXNET3_TXDESC_GET_EOP(txdesc) ((txdesc)->eop)
303# define VMXNET3_TCD_GET_GEN(tcd) ((tcd)->gen)
304# define VMXNET3_TCD_GET_TXIDX(tcd) ((tcd)->txdIdx)
305# define vmxnet3_getRxComp(dstrcd, rcd, tmp) (dstrcd) = (rcd)
306# define vmxnet3_getRxDesc(dstrxd, rxd, tmp) (dstrxd) = (rxd)
307
308#endif /* __BIG_ENDIAN_BITFIELD */
309
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700310
311static void
312vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
313 struct pci_dev *pdev)
314{
315 if (tbi->map_type == VMXNET3_MAP_SINGLE)
Andy Kingb0eb57c2013-08-23 09:33:49 -0700316 dma_unmap_single(&pdev->dev, tbi->dma_addr, tbi->len,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700317 PCI_DMA_TODEVICE);
318 else if (tbi->map_type == VMXNET3_MAP_PAGE)
Andy Kingb0eb57c2013-08-23 09:33:49 -0700319 dma_unmap_page(&pdev->dev, tbi->dma_addr, tbi->len,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700320 PCI_DMA_TODEVICE);
321 else
322 BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
323
324 tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
325}
326
327
328static int
329vmxnet3_unmap_pkt(u32 eop_idx, struct vmxnet3_tx_queue *tq,
330 struct pci_dev *pdev, struct vmxnet3_adapter *adapter)
331{
332 struct sk_buff *skb;
333 int entries = 0;
334
335 /* no out of order completion */
336 BUG_ON(tq->buf_info[eop_idx].sop_idx != tq->tx_ring.next2comp);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000337 BUG_ON(VMXNET3_TXDESC_GET_EOP(&(tq->tx_ring.base[eop_idx].txd)) != 1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700338
339 skb = tq->buf_info[eop_idx].skb;
340 BUG_ON(skb == NULL);
341 tq->buf_info[eop_idx].skb = NULL;
342
343 VMXNET3_INC_RING_IDX_ONLY(eop_idx, tq->tx_ring.size);
344
345 while (tq->tx_ring.next2comp != eop_idx) {
346 vmxnet3_unmap_tx_buf(tq->buf_info + tq->tx_ring.next2comp,
347 pdev);
348
349 /* update next2comp w/o tx_lock. Since we are marking more,
350 * instead of less, tx ring entries avail, the worst case is
351 * that the tx routine incorrectly re-queues a pkt due to
352 * insufficient tx ring entries.
353 */
354 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
355 entries++;
356 }
357
358 dev_kfree_skb_any(skb);
359 return entries;
360}
361
362
363static int
364vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue *tq,
365 struct vmxnet3_adapter *adapter)
366{
367 int completed = 0;
368 union Vmxnet3_GenericDesc *gdesc;
369
370 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000371 while (VMXNET3_TCD_GET_GEN(&gdesc->tcd) == tq->comp_ring.gen) {
372 completed += vmxnet3_unmap_pkt(VMXNET3_TCD_GET_TXIDX(
373 &gdesc->tcd), tq, adapter->pdev,
374 adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700375
376 vmxnet3_comp_ring_adv_next2proc(&tq->comp_ring);
377 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
378 }
379
380 if (completed) {
381 spin_lock(&tq->tx_lock);
382 if (unlikely(vmxnet3_tq_stopped(tq, adapter) &&
383 vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) >
384 VMXNET3_WAKE_QUEUE_THRESHOLD(tq) &&
385 netif_carrier_ok(adapter->netdev))) {
386 vmxnet3_tq_wake(tq, adapter);
387 }
388 spin_unlock(&tq->tx_lock);
389 }
390 return completed;
391}
392
393
394static void
395vmxnet3_tq_cleanup(struct vmxnet3_tx_queue *tq,
396 struct vmxnet3_adapter *adapter)
397{
398 int i;
399
400 while (tq->tx_ring.next2comp != tq->tx_ring.next2fill) {
401 struct vmxnet3_tx_buf_info *tbi;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700402
403 tbi = tq->buf_info + tq->tx_ring.next2comp;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700404
405 vmxnet3_unmap_tx_buf(tbi, adapter->pdev);
406 if (tbi->skb) {
407 dev_kfree_skb_any(tbi->skb);
408 tbi->skb = NULL;
409 }
410 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
411 }
412
413 /* sanity check, verify all buffers are indeed unmapped and freed */
414 for (i = 0; i < tq->tx_ring.size; i++) {
415 BUG_ON(tq->buf_info[i].skb != NULL ||
416 tq->buf_info[i].map_type != VMXNET3_MAP_NONE);
417 }
418
419 tq->tx_ring.gen = VMXNET3_INIT_GEN;
420 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
421
422 tq->comp_ring.gen = VMXNET3_INIT_GEN;
423 tq->comp_ring.next2proc = 0;
424}
425
426
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000427static void
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700428vmxnet3_tq_destroy(struct vmxnet3_tx_queue *tq,
429 struct vmxnet3_adapter *adapter)
430{
431 if (tq->tx_ring.base) {
Andy Kingb0eb57c2013-08-23 09:33:49 -0700432 dma_free_coherent(&adapter->pdev->dev, tq->tx_ring.size *
433 sizeof(struct Vmxnet3_TxDesc),
434 tq->tx_ring.base, tq->tx_ring.basePA);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700435 tq->tx_ring.base = NULL;
436 }
437 if (tq->data_ring.base) {
Andy Kingb0eb57c2013-08-23 09:33:49 -0700438 dma_free_coherent(&adapter->pdev->dev, tq->data_ring.size *
439 sizeof(struct Vmxnet3_TxDataDesc),
440 tq->data_ring.base, tq->data_ring.basePA);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700441 tq->data_ring.base = NULL;
442 }
443 if (tq->comp_ring.base) {
Andy Kingb0eb57c2013-08-23 09:33:49 -0700444 dma_free_coherent(&adapter->pdev->dev, tq->comp_ring.size *
445 sizeof(struct Vmxnet3_TxCompDesc),
446 tq->comp_ring.base, tq->comp_ring.basePA);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700447 tq->comp_ring.base = NULL;
448 }
Andy Kingb0eb57c2013-08-23 09:33:49 -0700449 if (tq->buf_info) {
450 dma_free_coherent(&adapter->pdev->dev,
451 tq->tx_ring.size * sizeof(tq->buf_info[0]),
452 tq->buf_info, tq->buf_info_pa);
453 tq->buf_info = NULL;
454 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700455}
456
457
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000458/* Destroy all tx queues */
459void
460vmxnet3_tq_destroy_all(struct vmxnet3_adapter *adapter)
461{
462 int i;
463
464 for (i = 0; i < adapter->num_tx_queues; i++)
465 vmxnet3_tq_destroy(&adapter->tx_queue[i], adapter);
466}
467
468
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700469static void
470vmxnet3_tq_init(struct vmxnet3_tx_queue *tq,
471 struct vmxnet3_adapter *adapter)
472{
473 int i;
474
475 /* reset the tx ring contents to 0 and reset the tx ring states */
476 memset(tq->tx_ring.base, 0, tq->tx_ring.size *
477 sizeof(struct Vmxnet3_TxDesc));
478 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
479 tq->tx_ring.gen = VMXNET3_INIT_GEN;
480
481 memset(tq->data_ring.base, 0, tq->data_ring.size *
482 sizeof(struct Vmxnet3_TxDataDesc));
483
484 /* reset the tx comp ring contents to 0 and reset comp ring states */
485 memset(tq->comp_ring.base, 0, tq->comp_ring.size *
486 sizeof(struct Vmxnet3_TxCompDesc));
487 tq->comp_ring.next2proc = 0;
488 tq->comp_ring.gen = VMXNET3_INIT_GEN;
489
490 /* reset the bookkeeping data */
491 memset(tq->buf_info, 0, sizeof(tq->buf_info[0]) * tq->tx_ring.size);
492 for (i = 0; i < tq->tx_ring.size; i++)
493 tq->buf_info[i].map_type = VMXNET3_MAP_NONE;
494
495 /* stats are not reset */
496}
497
498
499static int
500vmxnet3_tq_create(struct vmxnet3_tx_queue *tq,
501 struct vmxnet3_adapter *adapter)
502{
Andy Kingb0eb57c2013-08-23 09:33:49 -0700503 size_t sz;
504
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700505 BUG_ON(tq->tx_ring.base || tq->data_ring.base ||
506 tq->comp_ring.base || tq->buf_info);
507
Andy Kingb0eb57c2013-08-23 09:33:49 -0700508 tq->tx_ring.base = dma_alloc_coherent(&adapter->pdev->dev,
509 tq->tx_ring.size * sizeof(struct Vmxnet3_TxDesc),
510 &tq->tx_ring.basePA, GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700511 if (!tq->tx_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000512 netdev_err(adapter->netdev, "failed to allocate tx ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700513 goto err;
514 }
515
Andy Kingb0eb57c2013-08-23 09:33:49 -0700516 tq->data_ring.base = dma_alloc_coherent(&adapter->pdev->dev,
517 tq->data_ring.size * sizeof(struct Vmxnet3_TxDataDesc),
518 &tq->data_ring.basePA, GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700519 if (!tq->data_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000520 netdev_err(adapter->netdev, "failed to allocate data ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700521 goto err;
522 }
523
Andy Kingb0eb57c2013-08-23 09:33:49 -0700524 tq->comp_ring.base = dma_alloc_coherent(&adapter->pdev->dev,
525 tq->comp_ring.size * sizeof(struct Vmxnet3_TxCompDesc),
526 &tq->comp_ring.basePA, GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700527 if (!tq->comp_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +0000528 netdev_err(adapter->netdev, "failed to allocate tx comp ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700529 goto err;
530 }
531
Andy Kingb0eb57c2013-08-23 09:33:49 -0700532 sz = tq->tx_ring.size * sizeof(tq->buf_info[0]);
533 tq->buf_info = dma_zalloc_coherent(&adapter->pdev->dev, sz,
534 &tq->buf_info_pa, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000535 if (!tq->buf_info)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700536 goto err;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700537
538 return 0;
539
540err:
541 vmxnet3_tq_destroy(tq, adapter);
542 return -ENOMEM;
543}
544
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000545static void
546vmxnet3_tq_cleanup_all(struct vmxnet3_adapter *adapter)
547{
548 int i;
549
550 for (i = 0; i < adapter->num_tx_queues; i++)
551 vmxnet3_tq_cleanup(&adapter->tx_queue[i], adapter);
552}
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700553
554/*
555 * starting from ring->next2fill, allocate rx buffers for the given ring
556 * of the rx queue and update the rx desc. stop after @num_to_alloc buffers
557 * are allocated or allocation fails
558 */
559
560static int
561vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
562 int num_to_alloc, struct vmxnet3_adapter *adapter)
563{
564 int num_allocated = 0;
565 struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
566 struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
567 u32 val;
568
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000569 while (num_allocated <= num_to_alloc) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700570 struct vmxnet3_rx_buf_info *rbi;
571 union Vmxnet3_GenericDesc *gd;
572
573 rbi = rbi_base + ring->next2fill;
574 gd = ring->base + ring->next2fill;
575
576 if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
577 if (rbi->skb == NULL) {
Stephen Hemminger0d735f12013-01-15 07:28:26 +0000578 rbi->skb = __netdev_alloc_skb_ip_align(adapter->netdev,
579 rbi->len,
580 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700581 if (unlikely(rbi->skb == NULL)) {
582 rq->stats.rx_buf_alloc_failure++;
583 break;
584 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700585
Andy Kingb0eb57c2013-08-23 09:33:49 -0700586 rbi->dma_addr = dma_map_single(
587 &adapter->pdev->dev,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700588 rbi->skb->data, rbi->len,
589 PCI_DMA_FROMDEVICE);
590 } else {
591 /* rx buffer skipped by the device */
592 }
593 val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
594 } else {
595 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
596 rbi->len != PAGE_SIZE);
597
598 if (rbi->page == NULL) {
599 rbi->page = alloc_page(GFP_ATOMIC);
600 if (unlikely(rbi->page == NULL)) {
601 rq->stats.rx_buf_alloc_failure++;
602 break;
603 }
Andy Kingb0eb57c2013-08-23 09:33:49 -0700604 rbi->dma_addr = dma_map_page(
605 &adapter->pdev->dev,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700606 rbi->page, 0, PAGE_SIZE,
607 PCI_DMA_FROMDEVICE);
608 } else {
609 /* rx buffers skipped by the device */
610 }
611 val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
612 }
613
614 BUG_ON(rbi->dma_addr == 0);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000615 gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000616 gd->dword[2] = cpu_to_le32((!ring->gen << VMXNET3_RXD_GEN_SHIFT)
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000617 | val | rbi->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700618
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000619 /* Fill the last buffer but dont mark it ready, or else the
620 * device will think that the queue is full */
621 if (num_allocated == num_to_alloc)
622 break;
623
624 gd->dword[2] |= cpu_to_le32(ring->gen << VMXNET3_RXD_GEN_SHIFT);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700625 num_allocated++;
626 vmxnet3_cmd_ring_adv_next2fill(ring);
627 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700628
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000629 netdev_dbg(adapter->netdev,
Stephen Hemminger69b9a712013-01-15 07:28:27 +0000630 "alloc_rx_buf: %d allocated, next2fill %u, next2comp %u\n",
631 num_allocated, ring->next2fill, ring->next2comp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700632
633 /* so that the device can distinguish a full ring and an empty ring */
634 BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
635
636 return num_allocated;
637}
638
639
640static void
641vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
642 struct vmxnet3_rx_buf_info *rbi)
643{
644 struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
645 skb_shinfo(skb)->nr_frags;
646
647 BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
648
Ian Campbell0e0634d2011-09-21 21:53:28 +0000649 __skb_frag_set_page(frag, rbi->page);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700650 frag->page_offset = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000651 skb_frag_size_set(frag, rcd->len);
652 skb->data_len += rcd->len;
Eric Dumazet5e6c3552011-10-13 11:38:17 +0000653 skb->truesize += PAGE_SIZE;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700654 skb_shinfo(skb)->nr_frags++;
655}
656
657
658static void
659vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
660 struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
661 struct vmxnet3_adapter *adapter)
662{
663 u32 dw2, len;
664 unsigned long buf_offset;
665 int i;
666 union Vmxnet3_GenericDesc *gdesc;
667 struct vmxnet3_tx_buf_info *tbi = NULL;
668
669 BUG_ON(ctx->copy_size > skb_headlen(skb));
670
671 /* use the previous gen bit for the SOP desc */
672 dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
673
674 ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
675 gdesc = ctx->sop_txd; /* both loops below can be skipped */
676
677 /* no need to map the buffer if headers are copied */
678 if (ctx->copy_size) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000679 ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700680 tq->tx_ring.next2fill *
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000681 sizeof(struct Vmxnet3_TxDataDesc));
682 ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700683 ctx->sop_txd->dword[3] = 0;
684
685 tbi = tq->buf_info + tq->tx_ring.next2fill;
686 tbi->map_type = VMXNET3_MAP_NONE;
687
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000688 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700689 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000690 tq->tx_ring.next2fill,
691 le64_to_cpu(ctx->sop_txd->txd.addr),
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700692 ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
693 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
694
695 /* use the right gen for non-SOP desc */
696 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
697 }
698
699 /* linear part can use multiple tx desc if it's big */
700 len = skb_headlen(skb) - ctx->copy_size;
701 buf_offset = ctx->copy_size;
702 while (len) {
703 u32 buf_size;
704
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000705 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
706 buf_size = len;
707 dw2 |= len;
708 } else {
709 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
710 /* spec says that for TxDesc.len, 0 == 2^14 */
711 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700712
713 tbi = tq->buf_info + tq->tx_ring.next2fill;
714 tbi->map_type = VMXNET3_MAP_SINGLE;
Andy Kingb0eb57c2013-08-23 09:33:49 -0700715 tbi->dma_addr = dma_map_single(&adapter->pdev->dev,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700716 skb->data + buf_offset, buf_size,
717 PCI_DMA_TODEVICE);
718
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000719 tbi->len = buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700720
721 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
722 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
723
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000724 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000725 gdesc->dword[2] = cpu_to_le32(dw2);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700726 gdesc->dword[3] = 0;
727
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000728 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700729 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000730 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
731 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700732 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
733 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
734
735 len -= buf_size;
736 buf_offset += buf_size;
737 }
738
739 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000740 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000741 u32 buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700742
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000743 buf_offset = 0;
744 len = skb_frag_size(frag);
745 while (len) {
746 tbi = tq->buf_info + tq->tx_ring.next2fill;
747 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
748 buf_size = len;
749 dw2 |= len;
750 } else {
751 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
752 /* spec says that for TxDesc.len, 0 == 2^14 */
753 }
754 tbi->map_type = VMXNET3_MAP_PAGE;
755 tbi->dma_addr = skb_frag_dma_map(&adapter->pdev->dev, frag,
756 buf_offset, buf_size,
757 DMA_TO_DEVICE);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700758
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000759 tbi->len = buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700760
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000761 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
762 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700763
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000764 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
765 gdesc->dword[2] = cpu_to_le32(dw2);
766 gdesc->dword[3] = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700767
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000768 netdev_dbg(adapter->netdev,
Hans Wennborg8b429462014-08-05 21:42:41 -0700769 "txd[%u]: 0x%llx %u %u\n",
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000770 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
771 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
772 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
773 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
774
775 len -= buf_size;
776 buf_offset += buf_size;
777 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700778 }
779
780 ctx->eop_txd = gdesc;
781
782 /* set the last buf_info for the pkt */
783 tbi->skb = skb;
784 tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
785}
786
787
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000788/* Init all tx queues */
789static void
790vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
791{
792 int i;
793
794 for (i = 0; i < adapter->num_tx_queues; i++)
795 vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
796}
797
798
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700799/*
800 * parse and copy relevant protocol headers:
801 * For a tso pkt, relevant headers are L2/3/4 including options
802 * For a pkt requesting csum offloading, they are L2/3 and may include L4
803 * if it's a TCP/UDP pkt
804 *
805 * Returns:
806 * -1: error happens during parsing
807 * 0: protocol headers parsed, but too big to be copied
808 * 1: protocol headers parsed and copied
809 *
810 * Other effects:
811 * 1. related *ctx fields are updated.
812 * 2. ctx->copy_size is # of bytes copied
813 * 3. the portion copied is guaranteed to be in the linear part
814 *
815 */
816static int
817vmxnet3_parse_and_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
818 struct vmxnet3_tx_ctx *ctx,
819 struct vmxnet3_adapter *adapter)
820{
821 struct Vmxnet3_TxDataDesc *tdd;
Shrikrishna Khare759c9352015-02-28 20:33:09 -0800822 u8 protocol = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700823
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000824 if (ctx->mss) { /* TSO */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700825 ctx->eth_ip_hdr_size = skb_transport_offset(skb);
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000826 ctx->l4_hdr_size = tcp_hdrlen(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700827 ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
828 } else {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700829 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000830 ctx->eth_ip_hdr_size = skb_checksum_start_offset(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700831
832 if (ctx->ipv4) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000833 const struct iphdr *iph = ip_hdr(skb);
834
Shrikrishna Khare759c9352015-02-28 20:33:09 -0800835 protocol = iph->protocol;
836 } else if (ctx->ipv6) {
837 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
838
839 protocol = ipv6h->nexthdr;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700840 }
Shrikrishna Khare759c9352015-02-28 20:33:09 -0800841
842 switch (protocol) {
843 case IPPROTO_TCP:
844 ctx->l4_hdr_size = tcp_hdrlen(skb);
845 break;
846 case IPPROTO_UDP:
847 ctx->l4_hdr_size = sizeof(struct udphdr);
848 break;
849 default:
850 ctx->l4_hdr_size = 0;
851 break;
852 }
853
Neil Hormanb2032622012-02-16 01:48:56 +0000854 ctx->copy_size = min(ctx->eth_ip_hdr_size +
855 ctx->l4_hdr_size, skb->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700856 } else {
857 ctx->eth_ip_hdr_size = 0;
858 ctx->l4_hdr_size = 0;
859 /* copy as much as allowed */
860 ctx->copy_size = min((unsigned int)VMXNET3_HDR_COPY_SIZE
861 , skb_headlen(skb));
862 }
863
Shreyas Bhatewarac41fcce2015-06-19 13:37:03 -0700864 if (skb->len <= VMXNET3_HDR_COPY_SIZE)
865 ctx->copy_size = skb->len;
866
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700867 /* make sure headers are accessible directly */
868 if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
869 goto err;
870 }
871
872 if (unlikely(ctx->copy_size > VMXNET3_HDR_COPY_SIZE)) {
873 tq->stats.oversized_hdr++;
874 ctx->copy_size = 0;
875 return 0;
876 }
877
878 tdd = tq->data_ring.base + tq->tx_ring.next2fill;
879
880 memcpy(tdd->data, skb->data, ctx->copy_size);
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000881 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700882 "copy %u bytes to dataRing[%u]\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700883 ctx->copy_size, tq->tx_ring.next2fill);
884 return 1;
885
886err:
887 return -1;
888}
889
890
891static void
892vmxnet3_prepare_tso(struct sk_buff *skb,
893 struct vmxnet3_tx_ctx *ctx)
894{
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000895 struct tcphdr *tcph = tcp_hdr(skb);
896
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700897 if (ctx->ipv4) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000898 struct iphdr *iph = ip_hdr(skb);
899
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700900 iph->check = 0;
901 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
902 IPPROTO_TCP, 0);
Shrikrishna Khare759c9352015-02-28 20:33:09 -0800903 } else if (ctx->ipv6) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000904 struct ipv6hdr *iph = ipv6_hdr(skb);
905
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700906 tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
907 IPPROTO_TCP, 0);
908 }
909}
910
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000911static int txd_estimate(const struct sk_buff *skb)
912{
913 int count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
914 int i;
915
916 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
917 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
918
919 count += VMXNET3_TXD_NEEDED(skb_frag_size(frag));
920 }
921 return count;
922}
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700923
924/*
925 * Transmits a pkt thru a given tq
926 * Returns:
927 * NETDEV_TX_OK: descriptors are setup successfully
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300928 * NETDEV_TX_OK: error occurred, the pkt is dropped
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700929 * NETDEV_TX_BUSY: tx ring is full, queue is stopped
930 *
931 * Side-effects:
932 * 1. tx ring may be changed
933 * 2. tq stats may be updated accordingly
934 * 3. shared->txNumDeferred may be updated
935 */
936
937static int
938vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
939 struct vmxnet3_adapter *adapter, struct net_device *netdev)
940{
941 int ret;
942 u32 count;
943 unsigned long flags;
944 struct vmxnet3_tx_ctx ctx;
945 union Vmxnet3_GenericDesc *gdesc;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000946#ifdef __BIG_ENDIAN_BITFIELD
947 /* Use temporary descriptor to avoid touching bits multiple times */
948 union Vmxnet3_GenericDesc tempTxDesc;
949#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700950
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000951 count = txd_estimate(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700952
Jesse Gross72e85c42011-06-23 13:04:39 +0000953 ctx.ipv4 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IP));
Shrikrishna Khare759c9352015-02-28 20:33:09 -0800954 ctx.ipv6 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IPV6));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700955
956 ctx.mss = skb_shinfo(skb)->gso_size;
957 if (ctx.mss) {
958 if (skb_header_cloned(skb)) {
959 if (unlikely(pskb_expand_head(skb, 0, 0,
960 GFP_ATOMIC) != 0)) {
961 tq->stats.drop_tso++;
962 goto drop_pkt;
963 }
964 tq->stats.copy_skb_header++;
965 }
966 vmxnet3_prepare_tso(skb, &ctx);
967 } else {
968 if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
969
970 /* non-tso pkts must not use more than
971 * VMXNET3_MAX_TXD_PER_PKT entries
972 */
973 if (skb_linearize(skb) != 0) {
974 tq->stats.drop_too_many_frags++;
975 goto drop_pkt;
976 }
977 tq->stats.linearized++;
978
979 /* recalculate the # of descriptors to use */
980 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
981 }
982 }
983
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000984 spin_lock_irqsave(&tq->tx_lock, flags);
985
986 if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
987 tq->stats.tx_ring_full++;
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000988 netdev_dbg(adapter->netdev,
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000989 "tx queue stopped on %s, next2comp %u"
990 " next2fill %u\n", adapter->netdev->name,
991 tq->tx_ring.next2comp, tq->tx_ring.next2fill);
992
993 vmxnet3_tq_stop(tq, adapter);
994 spin_unlock_irqrestore(&tq->tx_lock, flags);
995 return NETDEV_TX_BUSY;
996 }
997
998
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700999 ret = vmxnet3_parse_and_copy_hdr(skb, tq, &ctx, adapter);
1000 if (ret >= 0) {
1001 BUG_ON(ret <= 0 && ctx.copy_size != 0);
1002 /* hdrs parsed, check against other limits */
1003 if (ctx.mss) {
1004 if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
1005 VMXNET3_MAX_TX_BUF_SIZE)) {
1006 goto hdr_too_big;
1007 }
1008 } else {
1009 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1010 if (unlikely(ctx.eth_ip_hdr_size +
1011 skb->csum_offset >
1012 VMXNET3_MAX_CSUM_OFFSET)) {
1013 goto hdr_too_big;
1014 }
1015 }
1016 }
1017 } else {
1018 tq->stats.drop_hdr_inspect_err++;
Dan Carpenterf955e142010-12-20 03:03:15 +00001019 goto unlock_drop_pkt;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001020 }
1021
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001022 /* fill tx descs related to addr & len */
1023 vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter);
1024
1025 /* setup the EOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001026 ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001027
1028 /* setup the SOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001029#ifdef __BIG_ENDIAN_BITFIELD
1030 gdesc = &tempTxDesc;
1031 gdesc->dword[2] = ctx.sop_txd->dword[2];
1032 gdesc->dword[3] = ctx.sop_txd->dword[3];
1033#else
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001034 gdesc = ctx.sop_txd;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001035#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001036 if (ctx.mss) {
1037 gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
1038 gdesc->txd.om = VMXNET3_OM_TSO;
1039 gdesc->txd.msscof = ctx.mss;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001040 le32_add_cpu(&tq->shared->txNumDeferred, (skb->len -
1041 gdesc->txd.hlen + ctx.mss - 1) / ctx.mss);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001042 } else {
1043 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1044 gdesc->txd.hlen = ctx.eth_ip_hdr_size;
1045 gdesc->txd.om = VMXNET3_OM_CSUM;
1046 gdesc->txd.msscof = ctx.eth_ip_hdr_size +
1047 skb->csum_offset;
1048 } else {
1049 gdesc->txd.om = 0;
1050 gdesc->txd.msscof = 0;
1051 }
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001052 le32_add_cpu(&tq->shared->txNumDeferred, 1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001053 }
1054
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001055 if (skb_vlan_tag_present(skb)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001056 gdesc->txd.ti = 1;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001057 gdesc->txd.tci = skb_vlan_tag_get(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001058 }
1059
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001060 /* finally flips the GEN bit of the SOP desc. */
1061 gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
1062 VMXNET3_TXD_GEN);
1063#ifdef __BIG_ENDIAN_BITFIELD
1064 /* Finished updating in bitfields of Tx Desc, so write them in original
1065 * place.
1066 */
1067 vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
1068 (struct Vmxnet3_TxDesc *)ctx.sop_txd);
1069 gdesc = ctx.sop_txd;
1070#endif
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00001071 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -07001072 "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
Joe Perchesc2fd03a2012-06-04 12:44:18 +00001073 (u32)(ctx.sop_txd -
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001074 tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
1075 le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001076
1077 spin_unlock_irqrestore(&tq->tx_lock, flags);
1078
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001079 if (le32_to_cpu(tq->shared->txNumDeferred) >=
1080 le32_to_cpu(tq->shared->txThreshold)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001081 tq->shared->txNumDeferred = 0;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001082 VMXNET3_WRITE_BAR0_REG(adapter,
1083 VMXNET3_REG_TXPROD + tq->qid * 8,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001084 tq->tx_ring.next2fill);
1085 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001086
1087 return NETDEV_TX_OK;
1088
1089hdr_too_big:
1090 tq->stats.drop_oversized_hdr++;
Dan Carpenterf955e142010-12-20 03:03:15 +00001091unlock_drop_pkt:
1092 spin_unlock_irqrestore(&tq->tx_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001093drop_pkt:
1094 tq->stats.drop_total++;
Eric W. Biedermanb1b71812014-03-15 18:31:16 -07001095 dev_kfree_skb_any(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001096 return NETDEV_TX_OK;
1097}
1098
1099
1100static netdev_tx_t
1101vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1102{
1103 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001104
stephen hemminger96800ee2012-11-13 13:53:28 +00001105 BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
1106 return vmxnet3_tq_xmit(skb,
1107 &adapter->tx_queue[skb->queue_mapping],
1108 adapter, netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001109}
1110
1111
1112static void
1113vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
1114 struct sk_buff *skb,
1115 union Vmxnet3_GenericDesc *gdesc)
1116{
Michał Mirosława0d27302011-04-18 13:31:21 +00001117 if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001118 /* typical case: TCP/UDP over IP and both csums are correct */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001119 if ((le32_to_cpu(gdesc->dword[3]) & VMXNET3_RCD_CSUM_OK) ==
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001120 VMXNET3_RCD_CSUM_OK) {
1121 skb->ip_summed = CHECKSUM_UNNECESSARY;
1122 BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
1123 BUG_ON(!(gdesc->rcd.v4 || gdesc->rcd.v6));
1124 BUG_ON(gdesc->rcd.frg);
1125 } else {
1126 if (gdesc->rcd.csum) {
1127 skb->csum = htons(gdesc->rcd.csum);
1128 skb->ip_summed = CHECKSUM_PARTIAL;
1129 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001130 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001131 }
1132 }
1133 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001134 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001135 }
1136}
1137
1138
1139static void
1140vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
1141 struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
1142{
1143 rq->stats.drop_err++;
1144 if (!rcd->fcs)
1145 rq->stats.drop_fcs++;
1146
1147 rq->stats.drop_total++;
1148
1149 /*
1150 * We do not unmap and chain the rx buffer to the skb.
1151 * We basically pretend this buffer is not used and will be recycled
1152 * by vmxnet3_rq_alloc_rx_buf()
1153 */
1154
1155 /*
1156 * ctx->skb may be NULL if this is the first and the only one
1157 * desc for the pkt
1158 */
1159 if (ctx->skb)
1160 dev_kfree_skb_irq(ctx->skb);
1161
1162 ctx->skb = NULL;
1163}
1164
1165
1166static int
1167vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
1168 struct vmxnet3_adapter *adapter, int quota)
1169{
Joe Perches215faf92010-12-21 02:16:10 -08001170 static const u32 rxprod_reg[2] = {
1171 VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2
1172 };
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001173 u32 num_rxd = 0;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001174 bool skip_page_frags = false;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001175 struct Vmxnet3_RxCompDesc *rcd;
1176 struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001177#ifdef __BIG_ENDIAN_BITFIELD
1178 struct Vmxnet3_RxDesc rxCmdDesc;
1179 struct Vmxnet3_RxCompDesc rxComp;
1180#endif
1181 vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
1182 &rxComp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001183 while (rcd->gen == rq->comp_ring.gen) {
1184 struct vmxnet3_rx_buf_info *rbi;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001185 struct sk_buff *skb, *new_skb = NULL;
1186 struct page *new_page = NULL;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001187 int num_to_alloc;
1188 struct Vmxnet3_RxDesc *rxd;
1189 u32 idx, ring_idx;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001190 struct vmxnet3_cmd_ring *ring = NULL;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001191 if (num_rxd >= quota) {
1192 /* we may stop even before we see the EOP desc of
1193 * the current pkt
1194 */
1195 break;
1196 }
1197 num_rxd++;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001198 BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001199 idx = rcd->rxdIdx;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001200 ring_idx = rcd->rqID < adapter->num_rx_queues ? 0 : 1;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001201 ring = rq->rx_ring + ring_idx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001202 vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
1203 &rxCmdDesc);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001204 rbi = rq->buf_info[ring_idx] + idx;
1205
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001206 BUG_ON(rxd->addr != rbi->dma_addr ||
1207 rxd->len != rbi->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001208
1209 if (unlikely(rcd->eop && rcd->err)) {
1210 vmxnet3_rx_error(rq, rcd, ctx, adapter);
1211 goto rcd_done;
1212 }
1213
1214 if (rcd->sop) { /* first buf of the pkt */
1215 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
1216 rcd->rqID != rq->qid);
1217
1218 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
1219 BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
1220
1221 if (unlikely(rcd->len == 0)) {
1222 /* Pretend the rx buffer is skipped. */
1223 BUG_ON(!(rcd->sop && rcd->eop));
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00001224 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -07001225 "rxRing[%u][%u] 0 length\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001226 ring_idx, idx);
1227 goto rcd_done;
1228 }
1229
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001230 skip_page_frags = false;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001231 ctx->skb = rbi->skb;
Stephen Hemminger0d735f12013-01-15 07:28:26 +00001232 new_skb = netdev_alloc_skb_ip_align(adapter->netdev,
1233 rbi->len);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001234 if (new_skb == NULL) {
1235 /* Skb allocation failed, do not handover this
1236 * skb to stack. Reuse it. Drop the existing pkt
1237 */
1238 rq->stats.rx_buf_alloc_failure++;
1239 ctx->skb = NULL;
1240 rq->stats.drop_total++;
1241 skip_page_frags = true;
1242 goto rcd_done;
1243 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001244
Andy Kingb0eb57c2013-08-23 09:33:49 -07001245 dma_unmap_single(&adapter->pdev->dev, rbi->dma_addr,
1246 rbi->len,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001247 PCI_DMA_FROMDEVICE);
1248
Stephen Hemminger7db11f72013-01-15 07:28:35 +00001249#ifdef VMXNET3_RSS
1250 if (rcd->rssType != VMXNET3_RCD_RSS_TYPE_NONE &&
1251 (adapter->netdev->features & NETIF_F_RXHASH))
Michal Schmidt2c15a152013-12-20 13:16:57 +01001252 skb_set_hash(ctx->skb,
1253 le32_to_cpu(rcd->rssHash),
Tom Herbert0b680702013-12-17 23:32:08 -08001254 PKT_HASH_TYPE_L3);
Stephen Hemminger7db11f72013-01-15 07:28:35 +00001255#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001256 skb_put(ctx->skb, rcd->len);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001257
1258 /* Immediate refill */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001259 rbi->skb = new_skb;
Andy Kingb0eb57c2013-08-23 09:33:49 -07001260 rbi->dma_addr = dma_map_single(&adapter->pdev->dev,
stephen hemminger96800ee2012-11-13 13:53:28 +00001261 rbi->skb->data, rbi->len,
1262 PCI_DMA_FROMDEVICE);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001263 rxd->addr = cpu_to_le64(rbi->dma_addr);
1264 rxd->len = rbi->len;
1265
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001266 } else {
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001267 BUG_ON(ctx->skb == NULL && !skip_page_frags);
1268
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001269 /* non SOP buffer must be type 1 in most cases */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001270 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE);
1271 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001272
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001273 /* If an sop buffer was dropped, skip all
1274 * following non-sop fragments. They will be reused.
1275 */
1276 if (skip_page_frags)
1277 goto rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001278
Shreyas Bhatewarac41fcce2015-06-19 13:37:03 -07001279 if (rcd->len) {
1280 new_page = alloc_page(GFP_ATOMIC);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001281 /* Replacement page frag could not be allocated.
1282 * Reuse this page. Drop the pkt and free the
1283 * skb which contained this page as a frag. Skip
1284 * processing all the following non-sop frags.
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001285 */
Shreyas Bhatewarac41fcce2015-06-19 13:37:03 -07001286 if (unlikely(!new_page)) {
1287 rq->stats.rx_buf_alloc_failure++;
1288 dev_kfree_skb(ctx->skb);
1289 ctx->skb = NULL;
1290 skip_page_frags = true;
1291 goto rcd_done;
1292 }
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001293
Andy Kingb0eb57c2013-08-23 09:33:49 -07001294 dma_unmap_page(&adapter->pdev->dev,
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001295 rbi->dma_addr, rbi->len,
1296 PCI_DMA_FROMDEVICE);
1297
1298 vmxnet3_append_frag(ctx->skb, rcd, rbi);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001299
Shreyas Bhatewarac41fcce2015-06-19 13:37:03 -07001300 /* Immediate refill */
1301 rbi->page = new_page;
1302 rbi->dma_addr = dma_map_page(&adapter->pdev->dev
1303 , rbi->page,
1304 0, PAGE_SIZE,
1305 PCI_DMA_FROMDEVICE);
1306 rxd->addr = cpu_to_le64(rbi->dma_addr);
1307 rxd->len = rbi->len;
1308 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001309 }
1310
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001311
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001312 skb = ctx->skb;
1313 if (rcd->eop) {
1314 skb->len += skb->data_len;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001315
1316 vmxnet3_rx_csum(adapter, skb,
1317 (union Vmxnet3_GenericDesc *)rcd);
1318 skb->protocol = eth_type_trans(skb, adapter->netdev);
1319
Jesse Gross72e85c42011-06-23 13:04:39 +00001320 if (unlikely(rcd->ts))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001321 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rcd->tci);
Jesse Gross72e85c42011-06-23 13:04:39 +00001322
Jesse Gross213ade82011-06-24 14:24:35 +00001323 if (adapter->netdev->features & NETIF_F_LRO)
1324 netif_receive_skb(skb);
1325 else
1326 napi_gro_receive(&rq->napi, skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001327
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001328 ctx->skb = NULL;
1329 }
1330
1331rcd_done:
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001332 /* device may have skipped some rx descs */
1333 ring->next2comp = idx;
1334 num_to_alloc = vmxnet3_cmd_ring_desc_avail(ring);
1335 ring = rq->rx_ring + ring_idx;
1336 while (num_to_alloc) {
1337 vmxnet3_getRxDesc(rxd, &ring->base[ring->next2fill].rxd,
1338 &rxCmdDesc);
1339 BUG_ON(!rxd->addr);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001340
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001341 /* Recv desc is ready to be used by the device */
1342 rxd->gen = ring->gen;
1343 vmxnet3_cmd_ring_adv_next2fill(ring);
1344 num_to_alloc--;
1345 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001346
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001347 /* if needed, update the register */
1348 if (unlikely(rq->shared->updateRxProd)) {
1349 VMXNET3_WRITE_BAR0_REG(adapter,
stephen hemminger96800ee2012-11-13 13:53:28 +00001350 rxprod_reg[ring_idx] + rq->qid * 8,
1351 ring->next2fill);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001352 }
1353
1354 vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001355 vmxnet3_getRxComp(rcd,
stephen hemminger96800ee2012-11-13 13:53:28 +00001356 &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001357 }
1358
1359 return num_rxd;
1360}
1361
1362
1363static void
1364vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
1365 struct vmxnet3_adapter *adapter)
1366{
1367 u32 i, ring_idx;
1368 struct Vmxnet3_RxDesc *rxd;
1369
1370 for (ring_idx = 0; ring_idx < 2; ring_idx++) {
1371 for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001372#ifdef __BIG_ENDIAN_BITFIELD
1373 struct Vmxnet3_RxDesc rxDesc;
1374#endif
1375 vmxnet3_getRxDesc(rxd,
1376 &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001377
1378 if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
1379 rq->buf_info[ring_idx][i].skb) {
Andy Kingb0eb57c2013-08-23 09:33:49 -07001380 dma_unmap_single(&adapter->pdev->dev, rxd->addr,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001381 rxd->len, PCI_DMA_FROMDEVICE);
1382 dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
1383 rq->buf_info[ring_idx][i].skb = NULL;
1384 } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
1385 rq->buf_info[ring_idx][i].page) {
Andy Kingb0eb57c2013-08-23 09:33:49 -07001386 dma_unmap_page(&adapter->pdev->dev, rxd->addr,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001387 rxd->len, PCI_DMA_FROMDEVICE);
1388 put_page(rq->buf_info[ring_idx][i].page);
1389 rq->buf_info[ring_idx][i].page = NULL;
1390 }
1391 }
1392
1393 rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
1394 rq->rx_ring[ring_idx].next2fill =
1395 rq->rx_ring[ring_idx].next2comp = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001396 }
1397
1398 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1399 rq->comp_ring.next2proc = 0;
1400}
1401
1402
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001403static void
1404vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
1405{
1406 int i;
1407
1408 for (i = 0; i < adapter->num_rx_queues; i++)
1409 vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
1410}
1411
1412
stephen hemminger280b74f2013-02-22 08:26:29 +00001413static void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
1414 struct vmxnet3_adapter *adapter)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001415{
1416 int i;
1417 int j;
1418
1419 /* all rx buffers must have already been freed */
1420 for (i = 0; i < 2; i++) {
1421 if (rq->buf_info[i]) {
1422 for (j = 0; j < rq->rx_ring[i].size; j++)
1423 BUG_ON(rq->buf_info[i][j].page != NULL);
1424 }
1425 }
1426
1427
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001428 for (i = 0; i < 2; i++) {
1429 if (rq->rx_ring[i].base) {
Andy Kingb0eb57c2013-08-23 09:33:49 -07001430 dma_free_coherent(&adapter->pdev->dev,
1431 rq->rx_ring[i].size
1432 * sizeof(struct Vmxnet3_RxDesc),
1433 rq->rx_ring[i].base,
1434 rq->rx_ring[i].basePA);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001435 rq->rx_ring[i].base = NULL;
1436 }
1437 rq->buf_info[i] = NULL;
1438 }
1439
1440 if (rq->comp_ring.base) {
Andy Kingb0eb57c2013-08-23 09:33:49 -07001441 dma_free_coherent(&adapter->pdev->dev, rq->comp_ring.size
1442 * sizeof(struct Vmxnet3_RxCompDesc),
1443 rq->comp_ring.base, rq->comp_ring.basePA);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001444 rq->comp_ring.base = NULL;
1445 }
Andy Kingb0eb57c2013-08-23 09:33:49 -07001446
1447 if (rq->buf_info[0]) {
1448 size_t sz = sizeof(struct vmxnet3_rx_buf_info) *
1449 (rq->rx_ring[0].size + rq->rx_ring[1].size);
1450 dma_free_coherent(&adapter->pdev->dev, sz, rq->buf_info[0],
1451 rq->buf_info_pa);
1452 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001453}
1454
1455
1456static int
1457vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
1458 struct vmxnet3_adapter *adapter)
1459{
1460 int i;
1461
1462 /* initialize buf_info */
1463 for (i = 0; i < rq->rx_ring[0].size; i++) {
1464
1465 /* 1st buf for a pkt is skbuff */
1466 if (i % adapter->rx_buf_per_pkt == 0) {
1467 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
1468 rq->buf_info[0][i].len = adapter->skb_buf_size;
1469 } else { /* subsequent bufs for a pkt is frag */
1470 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
1471 rq->buf_info[0][i].len = PAGE_SIZE;
1472 }
1473 }
1474 for (i = 0; i < rq->rx_ring[1].size; i++) {
1475 rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
1476 rq->buf_info[1][i].len = PAGE_SIZE;
1477 }
1478
1479 /* reset internal state and allocate buffers for both rings */
1480 for (i = 0; i < 2; i++) {
1481 rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001482
1483 memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
1484 sizeof(struct Vmxnet3_RxDesc));
1485 rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
1486 }
1487 if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
1488 adapter) == 0) {
1489 /* at least has 1 rx buffer for the 1st ring */
1490 return -ENOMEM;
1491 }
1492 vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
1493
1494 /* reset the comp ring */
1495 rq->comp_ring.next2proc = 0;
1496 memset(rq->comp_ring.base, 0, rq->comp_ring.size *
1497 sizeof(struct Vmxnet3_RxCompDesc));
1498 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1499
1500 /* reset rxctx */
1501 rq->rx_ctx.skb = NULL;
1502
1503 /* stats are not reset */
1504 return 0;
1505}
1506
1507
1508static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001509vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
1510{
1511 int i, err = 0;
1512
1513 for (i = 0; i < adapter->num_rx_queues; i++) {
1514 err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
1515 if (unlikely(err)) {
1516 dev_err(&adapter->netdev->dev, "%s: failed to "
1517 "initialize rx queue%i\n",
1518 adapter->netdev->name, i);
1519 break;
1520 }
1521 }
1522 return err;
1523
1524}
1525
1526
1527static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001528vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
1529{
1530 int i;
1531 size_t sz;
1532 struct vmxnet3_rx_buf_info *bi;
1533
1534 for (i = 0; i < 2; i++) {
1535
1536 sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
Andy Kingb0eb57c2013-08-23 09:33:49 -07001537 rq->rx_ring[i].base = dma_alloc_coherent(
1538 &adapter->pdev->dev, sz,
1539 &rq->rx_ring[i].basePA,
1540 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001541 if (!rq->rx_ring[i].base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001542 netdev_err(adapter->netdev,
1543 "failed to allocate rx ring %d\n", i);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001544 goto err;
1545 }
1546 }
1547
1548 sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
Andy Kingb0eb57c2013-08-23 09:33:49 -07001549 rq->comp_ring.base = dma_alloc_coherent(&adapter->pdev->dev, sz,
1550 &rq->comp_ring.basePA,
1551 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001552 if (!rq->comp_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001553 netdev_err(adapter->netdev, "failed to allocate rx comp ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001554 goto err;
1555 }
1556
1557 sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
1558 rq->rx_ring[1].size);
Andy Kingb0eb57c2013-08-23 09:33:49 -07001559 bi = dma_zalloc_coherent(&adapter->pdev->dev, sz, &rq->buf_info_pa,
1560 GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001561 if (!bi)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001562 goto err;
Joe Perchese404dec2012-01-29 12:56:23 +00001563
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001564 rq->buf_info[0] = bi;
1565 rq->buf_info[1] = bi + rq->rx_ring[0].size;
1566
1567 return 0;
1568
1569err:
1570 vmxnet3_rq_destroy(rq, adapter);
1571 return -ENOMEM;
1572}
1573
1574
1575static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001576vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
1577{
1578 int i, err = 0;
1579
1580 for (i = 0; i < adapter->num_rx_queues; i++) {
1581 err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
1582 if (unlikely(err)) {
1583 dev_err(&adapter->netdev->dev,
1584 "%s: failed to create rx queue%i\n",
1585 adapter->netdev->name, i);
1586 goto err_out;
1587 }
1588 }
1589 return err;
1590err_out:
1591 vmxnet3_rq_destroy_all(adapter);
1592 return err;
1593
1594}
1595
1596/* Multiple queue aware polling function for tx and rx */
1597
1598static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001599vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
1600{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001601 int rcd_done = 0, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001602 if (unlikely(adapter->shared->ecr))
1603 vmxnet3_process_events(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001604 for (i = 0; i < adapter->num_tx_queues; i++)
1605 vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001606
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001607 for (i = 0; i < adapter->num_rx_queues; i++)
1608 rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
1609 adapter, budget);
1610 return rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001611}
1612
1613
1614static int
1615vmxnet3_poll(struct napi_struct *napi, int budget)
1616{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001617 struct vmxnet3_rx_queue *rx_queue = container_of(napi,
1618 struct vmxnet3_rx_queue, napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001619 int rxd_done;
1620
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001621 rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001622
1623 if (rxd_done < budget) {
1624 napi_complete(napi);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001625 vmxnet3_enable_all_intrs(rx_queue->adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001626 }
1627 return rxd_done;
1628}
1629
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001630/*
1631 * NAPI polling function for MSI-X mode with multiple Rx queues
1632 * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
1633 */
1634
1635static int
1636vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
1637{
1638 struct vmxnet3_rx_queue *rq = container_of(napi,
1639 struct vmxnet3_rx_queue, napi);
1640 struct vmxnet3_adapter *adapter = rq->adapter;
1641 int rxd_done;
1642
1643 /* When sharing interrupt with corresponding tx queue, process
1644 * tx completions in that queue as well
1645 */
1646 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
1647 struct vmxnet3_tx_queue *tq =
1648 &adapter->tx_queue[rq - adapter->rx_queue];
1649 vmxnet3_tq_tx_complete(tq, adapter);
1650 }
1651
1652 rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
1653
1654 if (rxd_done < budget) {
1655 napi_complete(napi);
1656 vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
1657 }
1658 return rxd_done;
1659}
1660
1661
1662#ifdef CONFIG_PCI_MSI
1663
1664/*
1665 * Handle completion interrupts on tx queues
1666 * Returns whether or not the intr is handled
1667 */
1668
1669static irqreturn_t
1670vmxnet3_msix_tx(int irq, void *data)
1671{
1672 struct vmxnet3_tx_queue *tq = data;
1673 struct vmxnet3_adapter *adapter = tq->adapter;
1674
1675 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1676 vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
1677
1678 /* Handle the case where only one irq is allocate for all tx queues */
1679 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1680 int i;
1681 for (i = 0; i < adapter->num_tx_queues; i++) {
1682 struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
1683 vmxnet3_tq_tx_complete(txq, adapter);
1684 }
1685 } else {
1686 vmxnet3_tq_tx_complete(tq, adapter);
1687 }
1688 vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
1689
1690 return IRQ_HANDLED;
1691}
1692
1693
1694/*
1695 * Handle completion interrupts on rx queues. Returns whether or not the
1696 * intr is handled
1697 */
1698
1699static irqreturn_t
1700vmxnet3_msix_rx(int irq, void *data)
1701{
1702 struct vmxnet3_rx_queue *rq = data;
1703 struct vmxnet3_adapter *adapter = rq->adapter;
1704
1705 /* disable intr if needed */
1706 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1707 vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
1708 napi_schedule(&rq->napi);
1709
1710 return IRQ_HANDLED;
1711}
1712
1713/*
1714 *----------------------------------------------------------------------------
1715 *
1716 * vmxnet3_msix_event --
1717 *
1718 * vmxnet3 msix event intr handler
1719 *
1720 * Result:
1721 * whether or not the intr is handled
1722 *
1723 *----------------------------------------------------------------------------
1724 */
1725
1726static irqreturn_t
1727vmxnet3_msix_event(int irq, void *data)
1728{
1729 struct net_device *dev = data;
1730 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1731
1732 /* disable intr if needed */
1733 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1734 vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
1735
1736 if (adapter->shared->ecr)
1737 vmxnet3_process_events(adapter);
1738
1739 vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
1740
1741 return IRQ_HANDLED;
1742}
1743
1744#endif /* CONFIG_PCI_MSI */
1745
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001746
1747/* Interrupt handler for vmxnet3 */
1748static irqreturn_t
1749vmxnet3_intr(int irq, void *dev_id)
1750{
1751 struct net_device *dev = dev_id;
1752 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1753
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001754 if (adapter->intr.type == VMXNET3_IT_INTX) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001755 u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
1756 if (unlikely(icr == 0))
1757 /* not ours */
1758 return IRQ_NONE;
1759 }
1760
1761
1762 /* disable intr if needed */
1763 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001764 vmxnet3_disable_all_intrs(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001765
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001766 napi_schedule(&adapter->rx_queue[0].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001767
1768 return IRQ_HANDLED;
1769}
1770
1771#ifdef CONFIG_NET_POLL_CONTROLLER
1772
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001773/* netpoll callback. */
1774static void
1775vmxnet3_netpoll(struct net_device *netdev)
1776{
1777 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001778
Neil Hormand25f06e2014-03-10 06:55:55 -04001779 switch (adapter->intr.type) {
Arnd Bergmann0a8d8c42014-03-13 10:44:34 +01001780#ifdef CONFIG_PCI_MSI
1781 case VMXNET3_IT_MSIX: {
1782 int i;
Neil Hormand25f06e2014-03-10 06:55:55 -04001783 for (i = 0; i < adapter->num_rx_queues; i++)
1784 vmxnet3_msix_rx(0, &adapter->rx_queue[i]);
1785 break;
Arnd Bergmann0a8d8c42014-03-13 10:44:34 +01001786 }
1787#endif
Neil Hormand25f06e2014-03-10 06:55:55 -04001788 case VMXNET3_IT_MSI:
1789 default:
1790 vmxnet3_intr(0, adapter->netdev);
1791 break;
1792 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001793
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001794}
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001795#endif /* CONFIG_NET_POLL_CONTROLLER */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001796
1797static int
1798vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
1799{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001800 struct vmxnet3_intr *intr = &adapter->intr;
1801 int err = 0, i;
1802 int vector = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001803
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001804#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001805 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001806 for (i = 0; i < adapter->num_tx_queues; i++) {
1807 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1808 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
1809 adapter->netdev->name, vector);
1810 err = request_irq(
1811 intr->msix_entries[vector].vector,
1812 vmxnet3_msix_tx, 0,
1813 adapter->tx_queue[i].name,
1814 &adapter->tx_queue[i]);
1815 } else {
1816 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
1817 adapter->netdev->name, vector);
1818 }
1819 if (err) {
1820 dev_err(&adapter->netdev->dev,
1821 "Failed to request irq for MSIX, %s, "
1822 "error %d\n",
1823 adapter->tx_queue[i].name, err);
1824 return err;
1825 }
1826
1827 /* Handle the case where only 1 MSIx was allocated for
1828 * all tx queues */
1829 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1830 for (; i < adapter->num_tx_queues; i++)
1831 adapter->tx_queue[i].comp_ring.intr_idx
1832 = vector;
1833 vector++;
1834 break;
1835 } else {
1836 adapter->tx_queue[i].comp_ring.intr_idx
1837 = vector++;
1838 }
1839 }
1840 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
1841 vector = 0;
1842
1843 for (i = 0; i < adapter->num_rx_queues; i++) {
1844 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
1845 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
1846 adapter->netdev->name, vector);
1847 else
1848 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
1849 adapter->netdev->name, vector);
1850 err = request_irq(intr->msix_entries[vector].vector,
1851 vmxnet3_msix_rx, 0,
1852 adapter->rx_queue[i].name,
1853 &(adapter->rx_queue[i]));
1854 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001855 netdev_err(adapter->netdev,
1856 "Failed to request irq for MSIX, "
1857 "%s, error %d\n",
1858 adapter->rx_queue[i].name, err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001859 return err;
1860 }
1861
1862 adapter->rx_queue[i].comp_ring.intr_idx = vector++;
1863 }
1864
1865 sprintf(intr->event_msi_vector_name, "%s-event-%d",
1866 adapter->netdev->name, vector);
1867 err = request_irq(intr->msix_entries[vector].vector,
1868 vmxnet3_msix_event, 0,
1869 intr->event_msi_vector_name, adapter->netdev);
1870 intr->event_intr_idx = vector;
1871
1872 } else if (intr->type == VMXNET3_IT_MSI) {
1873 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001874 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
1875 adapter->netdev->name, adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001876 } else {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001877#endif
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001878 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001879 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
1880 IRQF_SHARED, adapter->netdev->name,
1881 adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001882#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001883 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001884#endif
1885 intr->num_intrs = vector + 1;
1886 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001887 netdev_err(adapter->netdev,
1888 "Failed to request irq (intr type:%d), error %d\n",
1889 intr->type, err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001890 } else {
1891 /* Number of rx queues will not change after this */
1892 for (i = 0; i < adapter->num_rx_queues; i++) {
1893 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
1894 rq->qid = i;
1895 rq->qid2 = i + adapter->num_rx_queues;
1896 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001897
1898
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001899
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001900 /* init our intr settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001901 for (i = 0; i < intr->num_intrs; i++)
1902 intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
1903 if (adapter->intr.type != VMXNET3_IT_MSIX) {
1904 adapter->intr.event_intr_idx = 0;
1905 for (i = 0; i < adapter->num_tx_queues; i++)
1906 adapter->tx_queue[i].comp_ring.intr_idx = 0;
1907 adapter->rx_queue[0].comp_ring.intr_idx = 0;
1908 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001909
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001910 netdev_info(adapter->netdev,
1911 "intr type %u, mode %u, %u vectors allocated\n",
1912 intr->type, intr->mask_mode, intr->num_intrs);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001913 }
1914
1915 return err;
1916}
1917
1918
1919static void
1920vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
1921{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001922 struct vmxnet3_intr *intr = &adapter->intr;
1923 BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001924
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001925 switch (intr->type) {
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001926#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001927 case VMXNET3_IT_MSIX:
1928 {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001929 int i, vector = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001930
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001931 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1932 for (i = 0; i < adapter->num_tx_queues; i++) {
1933 free_irq(intr->msix_entries[vector++].vector,
1934 &(adapter->tx_queue[i]));
1935 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
1936 break;
1937 }
1938 }
1939
1940 for (i = 0; i < adapter->num_rx_queues; i++) {
1941 free_irq(intr->msix_entries[vector++].vector,
1942 &(adapter->rx_queue[i]));
1943 }
1944
1945 free_irq(intr->msix_entries[vector].vector,
1946 adapter->netdev);
1947 BUG_ON(vector >= intr->num_intrs);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001948 break;
1949 }
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001950#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001951 case VMXNET3_IT_MSI:
1952 free_irq(adapter->pdev->irq, adapter->netdev);
1953 break;
1954 case VMXNET3_IT_INTX:
1955 free_irq(adapter->pdev->irq, adapter->netdev);
1956 break;
1957 default:
Sasha Levinc068e772012-11-08 10:23:03 +00001958 BUG();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001959 }
1960}
1961
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001962
1963static void
1964vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
1965{
Jesse Gross72e85c42011-06-23 13:04:39 +00001966 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1967 u16 vid;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001968
Jesse Gross72e85c42011-06-23 13:04:39 +00001969 /* allow untagged pkts */
1970 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
1971
1972 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1973 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001974}
1975
1976
Jiri Pirko8e586132011-12-08 19:52:37 -05001977static int
Patrick McHardy80d5c362013-04-19 02:04:28 +00001978vmxnet3_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001979{
1980 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001981
Jesse Grossf6957f82011-08-07 23:15:47 +00001982 if (!(netdev->flags & IFF_PROMISC)) {
1983 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
1984 unsigned long flags;
1985
1986 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
1987 spin_lock_irqsave(&adapter->cmd_lock, flags);
1988 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1989 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1990 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1991 }
Jesse Gross72e85c42011-06-23 13:04:39 +00001992
1993 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001994
1995 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001996}
1997
1998
Jiri Pirko8e586132011-12-08 19:52:37 -05001999static int
Patrick McHardy80d5c362013-04-19 02:04:28 +00002000vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002001{
2002 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002003
Jesse Grossf6957f82011-08-07 23:15:47 +00002004 if (!(netdev->flags & IFF_PROMISC)) {
2005 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2006 unsigned long flags;
2007
2008 VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
2009 spin_lock_irqsave(&adapter->cmd_lock, flags);
2010 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2011 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
2012 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2013 }
Jesse Gross72e85c42011-06-23 13:04:39 +00002014
2015 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05002016
2017 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002018}
2019
2020
2021static u8 *
2022vmxnet3_copy_mc(struct net_device *netdev)
2023{
2024 u8 *buf = NULL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002025 u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002026
2027 /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
2028 if (sz <= 0xffff) {
2029 /* We may be called with BH disabled */
2030 buf = kmalloc(sz, GFP_ATOMIC);
2031 if (buf) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002032 struct netdev_hw_addr *ha;
Jiri Pirko567ec872010-02-23 23:17:07 +00002033 int i = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002034
Jiri Pirko22bedad32010-04-01 21:22:57 +00002035 netdev_for_each_mc_addr(ha, netdev)
2036 memcpy(buf + i++ * ETH_ALEN, ha->addr,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002037 ETH_ALEN);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002038 }
2039 }
2040 return buf;
2041}
2042
2043
2044static void
2045vmxnet3_set_mc(struct net_device *netdev)
2046{
2047 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002048 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002049 struct Vmxnet3_RxFilterConf *rxConf =
2050 &adapter->shared->devRead.rxFilterConf;
2051 u8 *new_table = NULL;
Andy Kingb0eb57c2013-08-23 09:33:49 -07002052 dma_addr_t new_table_pa = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002053 u32 new_mode = VMXNET3_RXM_UCAST;
2054
Jesse Gross72e85c42011-06-23 13:04:39 +00002055 if (netdev->flags & IFF_PROMISC) {
2056 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2057 memset(vfTable, 0, VMXNET3_VFT_SIZE * sizeof(*vfTable));
2058
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002059 new_mode |= VMXNET3_RXM_PROMISC;
Jesse Gross72e85c42011-06-23 13:04:39 +00002060 } else {
2061 vmxnet3_restore_vlan(adapter);
2062 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002063
2064 if (netdev->flags & IFF_BROADCAST)
2065 new_mode |= VMXNET3_RXM_BCAST;
2066
2067 if (netdev->flags & IFF_ALLMULTI)
2068 new_mode |= VMXNET3_RXM_ALL_MULTI;
2069 else
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002070 if (!netdev_mc_empty(netdev)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002071 new_table = vmxnet3_copy_mc(netdev);
2072 if (new_table) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002073 rxConf->mfTableLen = cpu_to_le16(
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002074 netdev_mc_count(netdev) * ETH_ALEN);
Andy Kingb0eb57c2013-08-23 09:33:49 -07002075 new_table_pa = dma_map_single(
2076 &adapter->pdev->dev,
2077 new_table,
2078 rxConf->mfTableLen,
2079 PCI_DMA_TODEVICE);
Andy King4ad9a642014-09-02 13:13:44 -07002080 }
2081
2082 if (new_table_pa) {
2083 new_mode |= VMXNET3_RXM_MCAST;
Andy Kingb0eb57c2013-08-23 09:33:49 -07002084 rxConf->mfTablePA = cpu_to_le64(new_table_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002085 } else {
Andy King4ad9a642014-09-02 13:13:44 -07002086 netdev_info(netdev,
2087 "failed to copy mcast list, setting ALL_MULTI\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002088 new_mode |= VMXNET3_RXM_ALL_MULTI;
2089 }
2090 }
2091
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002092 if (!(new_mode & VMXNET3_RXM_MCAST)) {
2093 rxConf->mfTableLen = 0;
2094 rxConf->mfTablePA = 0;
2095 }
2096
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002097 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002098 if (new_mode != rxConf->rxMode) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002099 rxConf->rxMode = cpu_to_le32(new_mode);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002100 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2101 VMXNET3_CMD_UPDATE_RX_MODE);
Jesse Gross72e85c42011-06-23 13:04:39 +00002102 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2103 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002104 }
2105
2106 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2107 VMXNET3_CMD_UPDATE_MAC_FILTERS);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002108 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002109
Andy King4ad9a642014-09-02 13:13:44 -07002110 if (new_table_pa)
Andy Kingb0eb57c2013-08-23 09:33:49 -07002111 dma_unmap_single(&adapter->pdev->dev, new_table_pa,
2112 rxConf->mfTableLen, PCI_DMA_TODEVICE);
Andy King4ad9a642014-09-02 13:13:44 -07002113 kfree(new_table);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002114}
2115
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002116void
2117vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
2118{
2119 int i;
2120
2121 for (i = 0; i < adapter->num_rx_queues; i++)
2122 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2123}
2124
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002125
2126/*
2127 * Set up driver_shared based on settings in adapter.
2128 */
2129
2130static void
2131vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2132{
2133 struct Vmxnet3_DriverShared *shared = adapter->shared;
2134 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2135 struct Vmxnet3_TxQueueConf *tqc;
2136 struct Vmxnet3_RxQueueConf *rqc;
2137 int i;
2138
2139 memset(shared, 0, sizeof(*shared));
2140
2141 /* driver settings */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002142 shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2143 devRead->misc.driverInfo.version = cpu_to_le32(
2144 VMXNET3_DRIVER_VERSION_NUM);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002145 devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2146 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2147 devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002148 *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2149 *((u32 *)&devRead->misc.driverInfo.gos));
2150 devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2151 devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002152
Andy Kingb0eb57c2013-08-23 09:33:49 -07002153 devRead->misc.ddPA = cpu_to_le64(adapter->adapter_pa);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002154 devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002155
2156 /* set up feature flags */
Michał Mirosława0d27302011-04-18 13:31:21 +00002157 if (adapter->netdev->features & NETIF_F_RXCSUM)
Harvey Harrison3843e512010-10-21 18:05:32 +00002158 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002159
Michał Mirosława0d27302011-04-18 13:31:21 +00002160 if (adapter->netdev->features & NETIF_F_LRO) {
Harvey Harrison3843e512010-10-21 18:05:32 +00002161 devRead->misc.uptFeatures |= UPT1_F_LRO;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002162 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002163 }
Patrick McHardyf6469682013-04-19 02:04:27 +00002164 if (adapter->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
Harvey Harrison3843e512010-10-21 18:05:32 +00002165 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002166
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002167 devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2168 devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2169 devRead->misc.queueDescLen = cpu_to_le32(
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002170 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2171 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002172
2173 /* tx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002174 devRead->misc.numTxQueues = adapter->num_tx_queues;
2175 for (i = 0; i < adapter->num_tx_queues; i++) {
2176 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2177 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2178 tqc = &adapter->tqd_start[i].conf;
2179 tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
2180 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2181 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
Andy Kingb0eb57c2013-08-23 09:33:49 -07002182 tqc->ddPA = cpu_to_le64(tq->buf_info_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002183 tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
2184 tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
2185 tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
2186 tqc->ddLen = cpu_to_le32(
2187 sizeof(struct vmxnet3_tx_buf_info) *
2188 tqc->txRingSize);
2189 tqc->intrIdx = tq->comp_ring.intr_idx;
2190 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002191
2192 /* rx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002193 devRead->misc.numRxQueues = adapter->num_rx_queues;
2194 for (i = 0; i < adapter->num_rx_queues; i++) {
2195 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2196 rqc = &adapter->rqd_start[i].conf;
2197 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2198 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2199 rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
Andy Kingb0eb57c2013-08-23 09:33:49 -07002200 rqc->ddPA = cpu_to_le64(rq->buf_info_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002201 rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
2202 rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
2203 rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
2204 rqc->ddLen = cpu_to_le32(
2205 sizeof(struct vmxnet3_rx_buf_info) *
2206 (rqc->rxRingSize[0] +
2207 rqc->rxRingSize[1]));
2208 rqc->intrIdx = rq->comp_ring.intr_idx;
2209 }
2210
2211#ifdef VMXNET3_RSS
2212 memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2213
2214 if (adapter->rss) {
2215 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
Stephen Hemminger66d35912013-01-15 07:28:34 +00002216
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002217 devRead->misc.uptFeatures |= UPT1_F_RSS;
2218 devRead->misc.numRxQueues = adapter->num_rx_queues;
2219 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2220 UPT1_RSS_HASH_TYPE_IPV4 |
2221 UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2222 UPT1_RSS_HASH_TYPE_IPV6;
2223 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2224 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2225 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
Eric Dumazet6bf79cd2014-11-16 06:23:18 -08002226 netdev_rss_key_fill(rssConf->hashKey, sizeof(rssConf->hashKey));
Stephen Hemminger66d35912013-01-15 07:28:34 +00002227
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002228 for (i = 0; i < rssConf->indTableSize; i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00002229 rssConf->indTable[i] = ethtool_rxfh_indir_default(
2230 i, adapter->num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002231
2232 devRead->rssConfDesc.confVer = 1;
Andy Kingb0eb57c2013-08-23 09:33:49 -07002233 devRead->rssConfDesc.confLen = cpu_to_le32(sizeof(*rssConf));
2234 devRead->rssConfDesc.confPA =
2235 cpu_to_le64(adapter->rss_conf_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002236 }
2237
2238#endif /* VMXNET3_RSS */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002239
2240 /* intr settings */
2241 devRead->intrConf.autoMask = adapter->intr.mask_mode ==
2242 VMXNET3_IMM_AUTO;
2243 devRead->intrConf.numIntrs = adapter->intr.num_intrs;
2244 for (i = 0; i < adapter->intr.num_intrs; i++)
2245 devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
2246
2247 devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
Ronghua Zang6929fe82010-07-15 22:18:47 -07002248 devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002249
2250 /* rx filter settings */
2251 devRead->rxFilterConf.rxMode = 0;
2252 vmxnet3_restore_vlan(adapter);
Shreyas Bhatewaraf9f25022011-01-14 14:59:31 +00002253 vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
2254
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002255 /* the rest are already zeroed */
2256}
2257
2258
2259int
2260vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
2261{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002262 int err, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002263 u32 ret;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002264 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002265
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00002266 netdev_dbg(adapter->netdev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002267 " ring sizes %u %u %u\n", adapter->netdev->name,
2268 adapter->skb_buf_size, adapter->rx_buf_per_pkt,
2269 adapter->tx_queue[0].tx_ring.size,
2270 adapter->rx_queue[0].rx_ring[0].size,
2271 adapter->rx_queue[0].rx_ring[1].size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002272
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002273 vmxnet3_tq_init_all(adapter);
2274 err = vmxnet3_rq_init_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002275 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002276 netdev_err(adapter->netdev,
2277 "Failed to init rx queue error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002278 goto rq_err;
2279 }
2280
2281 err = vmxnet3_request_irqs(adapter);
2282 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002283 netdev_err(adapter->netdev,
2284 "Failed to setup irq for error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002285 goto irq_err;
2286 }
2287
2288 vmxnet3_setup_driver_shared(adapter);
2289
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002290 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
2291 adapter->shared_pa));
2292 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
2293 adapter->shared_pa));
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002294 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002295 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2296 VMXNET3_CMD_ACTIVATE_DEV);
2297 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002298 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002299
2300 if (ret != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002301 netdev_err(adapter->netdev,
2302 "Failed to activate dev: error %u\n", ret);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002303 err = -EINVAL;
2304 goto activate_err;
2305 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002306
2307 for (i = 0; i < adapter->num_rx_queues; i++) {
2308 VMXNET3_WRITE_BAR0_REG(adapter,
2309 VMXNET3_REG_RXPROD + i * VMXNET3_REG_ALIGN,
2310 adapter->rx_queue[i].rx_ring[0].next2fill);
2311 VMXNET3_WRITE_BAR0_REG(adapter, (VMXNET3_REG_RXPROD2 +
2312 (i * VMXNET3_REG_ALIGN)),
2313 adapter->rx_queue[i].rx_ring[1].next2fill);
2314 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002315
2316 /* Apply the rx filter settins last. */
2317 vmxnet3_set_mc(adapter->netdev);
2318
2319 /*
2320 * Check link state when first activating device. It will start the
2321 * tx queue if the link is up.
2322 */
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00002323 vmxnet3_check_link(adapter, true);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002324 for (i = 0; i < adapter->num_rx_queues; i++)
2325 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002326 vmxnet3_enable_all_intrs(adapter);
2327 clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
2328 return 0;
2329
2330activate_err:
2331 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
2332 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
2333 vmxnet3_free_irqs(adapter);
2334irq_err:
2335rq_err:
2336 /* free up buffers we allocated */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002337 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002338 return err;
2339}
2340
2341
2342void
2343vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
2344{
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002345 unsigned long flags;
2346 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002347 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002348 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002349}
2350
2351
2352int
2353vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
2354{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002355 int i;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002356 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002357 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
2358 return 0;
2359
2360
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002361 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002362 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2363 VMXNET3_CMD_QUIESCE_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002364 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002365 vmxnet3_disable_all_intrs(adapter);
2366
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002367 for (i = 0; i < adapter->num_rx_queues; i++)
2368 napi_disable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002369 netif_tx_disable(adapter->netdev);
2370 adapter->link_speed = 0;
2371 netif_carrier_off(adapter->netdev);
2372
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002373 vmxnet3_tq_cleanup_all(adapter);
2374 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002375 vmxnet3_free_irqs(adapter);
2376 return 0;
2377}
2378
2379
2380static void
2381vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2382{
2383 u32 tmp;
2384
2385 tmp = *(u32 *)mac;
2386 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
2387
2388 tmp = (mac[5] << 8) | mac[4];
2389 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
2390}
2391
2392
2393static int
2394vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
2395{
2396 struct sockaddr *addr = p;
2397 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2398
2399 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2400 vmxnet3_write_mac_addr(adapter, addr->sa_data);
2401
2402 return 0;
2403}
2404
2405
2406/* ==================== initialization and cleanup routines ============ */
2407
2408static int
2409vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
2410{
2411 int err;
2412 unsigned long mmio_start, mmio_len;
2413 struct pci_dev *pdev = adapter->pdev;
2414
2415 err = pci_enable_device(pdev);
2416 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002417 dev_err(&pdev->dev, "Failed to enable adapter: error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002418 return err;
2419 }
2420
2421 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
2422 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002423 dev_err(&pdev->dev,
2424 "pci_set_consistent_dma_mask failed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002425 err = -EIO;
2426 goto err_set_mask;
2427 }
2428 *dma64 = true;
2429 } else {
2430 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002431 dev_err(&pdev->dev,
2432 "pci_set_dma_mask failed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002433 err = -EIO;
2434 goto err_set_mask;
2435 }
2436 *dma64 = false;
2437 }
2438
2439 err = pci_request_selected_regions(pdev, (1 << 2) - 1,
2440 vmxnet3_driver_name);
2441 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002442 dev_err(&pdev->dev,
2443 "Failed to request region for adapter: error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002444 goto err_set_mask;
2445 }
2446
2447 pci_set_master(pdev);
2448
2449 mmio_start = pci_resource_start(pdev, 0);
2450 mmio_len = pci_resource_len(pdev, 0);
2451 adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
2452 if (!adapter->hw_addr0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002453 dev_err(&pdev->dev, "Failed to map bar0\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002454 err = -EIO;
2455 goto err_ioremap;
2456 }
2457
2458 mmio_start = pci_resource_start(pdev, 1);
2459 mmio_len = pci_resource_len(pdev, 1);
2460 adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
2461 if (!adapter->hw_addr1) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002462 dev_err(&pdev->dev, "Failed to map bar1\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002463 err = -EIO;
2464 goto err_bar1;
2465 }
2466 return 0;
2467
2468err_bar1:
2469 iounmap(adapter->hw_addr0);
2470err_ioremap:
2471 pci_release_selected_regions(pdev, (1 << 2) - 1);
2472err_set_mask:
2473 pci_disable_device(pdev);
2474 return err;
2475}
2476
2477
2478static void
2479vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
2480{
2481 BUG_ON(!adapter->pdev);
2482
2483 iounmap(adapter->hw_addr0);
2484 iounmap(adapter->hw_addr1);
2485 pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
2486 pci_disable_device(adapter->pdev);
2487}
2488
2489
2490static void
2491vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
2492{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002493 size_t sz, i, ring0_size, ring1_size, comp_size;
2494 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[0];
2495
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002496
2497 if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
2498 VMXNET3_MAX_ETH_HDR_SIZE) {
2499 adapter->skb_buf_size = adapter->netdev->mtu +
2500 VMXNET3_MAX_ETH_HDR_SIZE;
2501 if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
2502 adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
2503
2504 adapter->rx_buf_per_pkt = 1;
2505 } else {
2506 adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
2507 sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
2508 VMXNET3_MAX_ETH_HDR_SIZE;
2509 adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
2510 }
2511
2512 /*
2513 * for simplicity, force the ring0 size to be a multiple of
2514 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
2515 */
2516 sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002517 ring0_size = adapter->rx_queue[0].rx_ring[0].size;
2518 ring0_size = (ring0_size + sz - 1) / sz * sz;
Shreyas Bhatewaraa53255d2011-01-14 14:59:25 +00002519 ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002520 sz * sz);
2521 ring1_size = adapter->rx_queue[0].rx_ring[1].size;
Shrikrishna Khare53831aa2015-01-06 09:20:15 -08002522 ring1_size = (ring1_size + sz - 1) / sz * sz;
2523 ring1_size = min_t(u32, ring1_size, VMXNET3_RX_RING2_MAX_SIZE /
2524 sz * sz);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002525 comp_size = ring0_size + ring1_size;
2526
2527 for (i = 0; i < adapter->num_rx_queues; i++) {
2528 rq = &adapter->rx_queue[i];
2529 rq->rx_ring[0].size = ring0_size;
2530 rq->rx_ring[1].size = ring1_size;
2531 rq->comp_ring.size = comp_size;
2532 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002533}
2534
2535
2536int
2537vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
2538 u32 rx_ring_size, u32 rx_ring2_size)
2539{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002540 int err = 0, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002541
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002542 for (i = 0; i < adapter->num_tx_queues; i++) {
2543 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2544 tq->tx_ring.size = tx_ring_size;
2545 tq->data_ring.size = tx_ring_size;
2546 tq->comp_ring.size = tx_ring_size;
2547 tq->shared = &adapter->tqd_start[i].ctrl;
2548 tq->stopped = true;
2549 tq->adapter = adapter;
2550 tq->qid = i;
2551 err = vmxnet3_tq_create(tq, adapter);
2552 /*
2553 * Too late to change num_tx_queues. We cannot do away with
2554 * lesser number of queues than what we asked for
2555 */
2556 if (err)
2557 goto queue_err;
2558 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002559
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002560 adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
2561 adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002562 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002563 for (i = 0; i < adapter->num_rx_queues; i++) {
2564 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2565 /* qid and qid2 for rx queues will be assigned later when num
2566 * of rx queues is finalized after allocating intrs */
2567 rq->shared = &adapter->rqd_start[i].ctrl;
2568 rq->adapter = adapter;
2569 err = vmxnet3_rq_create(rq, adapter);
2570 if (err) {
2571 if (i == 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002572 netdev_err(adapter->netdev,
2573 "Could not allocate any rx queues. "
2574 "Aborting.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002575 goto queue_err;
2576 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002577 netdev_info(adapter->netdev,
2578 "Number of rx queues changed "
2579 "to : %d.\n", i);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002580 adapter->num_rx_queues = i;
2581 err = 0;
2582 break;
2583 }
2584 }
2585 }
2586 return err;
2587queue_err:
2588 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002589 return err;
2590}
2591
2592static int
2593vmxnet3_open(struct net_device *netdev)
2594{
2595 struct vmxnet3_adapter *adapter;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002596 int err, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002597
2598 adapter = netdev_priv(netdev);
2599
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002600 for (i = 0; i < adapter->num_tx_queues; i++)
2601 spin_lock_init(&adapter->tx_queue[i].tx_lock);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002602
Neil Hormanf00e2b02014-06-13 10:03:21 -04002603 err = vmxnet3_create_queues(adapter, adapter->tx_ring_size,
2604 adapter->rx_ring_size,
Shrikrishna Khare53831aa2015-01-06 09:20:15 -08002605 adapter->rx_ring2_size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002606 if (err)
2607 goto queue_err;
2608
2609 err = vmxnet3_activate_dev(adapter);
2610 if (err)
2611 goto activate_err;
2612
2613 return 0;
2614
2615activate_err:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002616 vmxnet3_rq_destroy_all(adapter);
2617 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002618queue_err:
2619 return err;
2620}
2621
2622
2623static int
2624vmxnet3_close(struct net_device *netdev)
2625{
2626 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2627
2628 /*
2629 * Reset_work may be in the middle of resetting the device, wait for its
2630 * completion.
2631 */
2632 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2633 msleep(1);
2634
2635 vmxnet3_quiesce_dev(adapter);
2636
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002637 vmxnet3_rq_destroy_all(adapter);
2638 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002639
2640 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2641
2642
2643 return 0;
2644}
2645
2646
2647void
2648vmxnet3_force_close(struct vmxnet3_adapter *adapter)
2649{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002650 int i;
2651
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002652 /*
2653 * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
2654 * vmxnet3_close() will deadlock.
2655 */
2656 BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
2657
2658 /* we need to enable NAPI, otherwise dev_close will deadlock */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002659 for (i = 0; i < adapter->num_rx_queues; i++)
2660 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002661 dev_close(adapter->netdev);
2662}
2663
2664
2665static int
2666vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
2667{
2668 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2669 int err = 0;
2670
2671 if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
2672 return -EINVAL;
2673
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002674 netdev->mtu = new_mtu;
2675
2676 /*
2677 * Reset_work may be in the middle of resetting the device, wait for its
2678 * completion.
2679 */
2680 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2681 msleep(1);
2682
2683 if (netif_running(netdev)) {
2684 vmxnet3_quiesce_dev(adapter);
2685 vmxnet3_reset_dev(adapter);
2686
2687 /* we need to re-create the rx queue based on the new mtu */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002688 vmxnet3_rq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002689 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002690 err = vmxnet3_rq_create_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002691 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002692 netdev_err(netdev,
2693 "failed to re-create rx queues, "
2694 " error %d. Closing it.\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002695 goto out;
2696 }
2697
2698 err = vmxnet3_activate_dev(adapter);
2699 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002700 netdev_err(netdev,
2701 "failed to re-activate, error %d. "
2702 "Closing it\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002703 goto out;
2704 }
2705 }
2706
2707out:
2708 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2709 if (err)
2710 vmxnet3_force_close(adapter);
2711
2712 return err;
2713}
2714
2715
2716static void
2717vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
2718{
2719 struct net_device *netdev = adapter->netdev;
2720
Michał Mirosława0d27302011-04-18 13:31:21 +00002721 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00002722 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_CTAG_TX |
2723 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_TSO | NETIF_F_TSO6 |
Jesse Gross72e85c42011-06-23 13:04:39 +00002724 NETIF_F_LRO;
Michał Mirosława0d27302011-04-18 13:31:21 +00002725 if (dma64)
Shreyas Bhatewaraebbf9292011-07-20 17:21:51 +00002726 netdev->hw_features |= NETIF_F_HIGHDMA;
Jesse Gross72e85c42011-06-23 13:04:39 +00002727 netdev->vlan_features = netdev->hw_features &
Patrick McHardyf6469682013-04-19 02:04:27 +00002728 ~(NETIF_F_HW_VLAN_CTAG_TX |
2729 NETIF_F_HW_VLAN_CTAG_RX);
2730 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002731}
2732
2733
2734static void
2735vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2736{
2737 u32 tmp;
2738
2739 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
2740 *(u32 *)mac = tmp;
2741
2742 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
2743 mac[4] = tmp & 0xff;
2744 mac[5] = (tmp >> 8) & 0xff;
2745}
2746
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002747#ifdef CONFIG_PCI_MSI
2748
2749/*
2750 * Enable MSIx vectors.
2751 * Returns :
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002752 * VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002753 * were enabled.
2754 * number of vectors which were enabled otherwise (this number is greater
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002755 * than VMXNET3_LINUX_MIN_MSIX_VECT)
2756 */
2757
2758static int
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002759vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter, int nvec)
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002760{
Alexander Gordeevc0a1be32014-02-18 11:12:03 +01002761 int ret = pci_enable_msix_range(adapter->pdev,
2762 adapter->intr.msix_entries, nvec, nvec);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002763
Alexander Gordeevc0a1be32014-02-18 11:12:03 +01002764 if (ret == -ENOSPC && nvec > VMXNET3_LINUX_MIN_MSIX_VECT) {
2765 dev_err(&adapter->netdev->dev,
2766 "Failed to enable %d MSI-X, trying %d\n",
2767 nvec, VMXNET3_LINUX_MIN_MSIX_VECT);
2768
2769 ret = pci_enable_msix_range(adapter->pdev,
2770 adapter->intr.msix_entries,
2771 VMXNET3_LINUX_MIN_MSIX_VECT,
2772 VMXNET3_LINUX_MIN_MSIX_VECT);
2773 }
2774
2775 if (ret < 0) {
2776 dev_err(&adapter->netdev->dev,
2777 "Failed to enable MSI-X, error: %d\n", ret);
2778 }
2779
2780 return ret;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002781}
2782
2783
2784#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002785
2786static void
2787vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
2788{
2789 u32 cfg;
Roland Dreiere328d412011-05-06 08:32:53 +00002790 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002791
2792 /* intr settings */
Roland Dreiere328d412011-05-06 08:32:53 +00002793 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002794 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2795 VMXNET3_CMD_GET_CONF_INTR);
2796 cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Roland Dreiere328d412011-05-06 08:32:53 +00002797 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002798 adapter->intr.type = cfg & 0x3;
2799 adapter->intr.mask_mode = (cfg >> 2) & 0x3;
2800
2801 if (adapter->intr.type == VMXNET3_IT_AUTO) {
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002802 adapter->intr.type = VMXNET3_IT_MSIX;
2803 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002804
Randy Dunlap8f7e5242009-10-14 20:38:58 -07002805#ifdef CONFIG_PCI_MSI
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002806 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002807 int i, nvec;
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002808
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002809 nvec = adapter->share_intr == VMXNET3_INTR_TXSHARE ?
2810 1 : adapter->num_tx_queues;
2811 nvec += adapter->share_intr == VMXNET3_INTR_BUDDYSHARE ?
2812 0 : adapter->num_rx_queues;
2813 nvec += 1; /* for link event */
2814 nvec = nvec > VMXNET3_LINUX_MIN_MSIX_VECT ?
2815 nvec : VMXNET3_LINUX_MIN_MSIX_VECT;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002816
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002817 for (i = 0; i < nvec; i++)
2818 adapter->intr.msix_entries[i].entry = i;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002819
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002820 nvec = vmxnet3_acquire_msix_vectors(adapter, nvec);
2821 if (nvec < 0)
2822 goto msix_err;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002823
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002824 /* If we cannot allocate one MSIx vector per queue
2825 * then limit the number of rx queues to 1
2826 */
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002827 if (nvec == VMXNET3_LINUX_MIN_MSIX_VECT) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002828 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
Shreyas Bhatewara7e96fbf2011-01-14 15:00:03 +00002829 || adapter->num_rx_queues != 1) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002830 adapter->share_intr = VMXNET3_INTR_TXSHARE;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002831 netdev_err(adapter->netdev,
2832 "Number of rx queues : 1\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002833 adapter->num_rx_queues = 1;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002834 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002835 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002836
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002837 adapter->intr.num_intrs = nvec;
2838 return;
2839
2840msix_err:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002841 /* If we cannot allocate MSIx vectors use only one rx queue */
Stephen Hemminger4bad25f2013-01-15 07:28:28 +00002842 dev_info(&adapter->pdev->dev,
2843 "Failed to enable MSI-X, error %d. "
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002844 "Limiting #rx queues to 1, try MSI.\n", nvec);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002845
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002846 adapter->intr.type = VMXNET3_IT_MSI;
2847 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002848
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002849 if (adapter->intr.type == VMXNET3_IT_MSI) {
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002850 if (!pci_enable_msi(adapter->pdev)) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002851 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002852 adapter->intr.num_intrs = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002853 return;
2854 }
2855 }
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002856#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002857
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002858 adapter->num_rx_queues = 1;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002859 dev_info(&adapter->netdev->dev,
2860 "Using INTx interrupt, #Rx queues: 1.\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002861 adapter->intr.type = VMXNET3_IT_INTX;
2862
2863 /* INT-X related setting */
2864 adapter->intr.num_intrs = 1;
2865}
2866
2867
2868static void
2869vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
2870{
2871 if (adapter->intr.type == VMXNET3_IT_MSIX)
2872 pci_disable_msix(adapter->pdev);
2873 else if (adapter->intr.type == VMXNET3_IT_MSI)
2874 pci_disable_msi(adapter->pdev);
2875 else
2876 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
2877}
2878
2879
2880static void
2881vmxnet3_tx_timeout(struct net_device *netdev)
2882{
2883 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2884 adapter->tx_timeout_count++;
2885
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002886 netdev_err(adapter->netdev, "tx hang\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002887 schedule_work(&adapter->work);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002888 netif_wake_queue(adapter->netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002889}
2890
2891
2892static void
2893vmxnet3_reset_work(struct work_struct *data)
2894{
2895 struct vmxnet3_adapter *adapter;
2896
2897 adapter = container_of(data, struct vmxnet3_adapter, work);
2898
2899 /* if another thread is resetting the device, no need to proceed */
2900 if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2901 return;
2902
2903 /* if the device is closed, we must leave it alone */
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002904 rtnl_lock();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002905 if (netif_running(adapter->netdev)) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002906 netdev_notice(adapter->netdev, "resetting\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002907 vmxnet3_quiesce_dev(adapter);
2908 vmxnet3_reset_dev(adapter);
2909 vmxnet3_activate_dev(adapter);
2910 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002911 netdev_info(adapter->netdev, "already closed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002912 }
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00002913 rtnl_unlock();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002914
2915 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2916}
2917
2918
Bill Pemberton3a4751a2012-12-03 09:24:16 -05002919static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002920vmxnet3_probe_device(struct pci_dev *pdev,
2921 const struct pci_device_id *id)
2922{
2923 static const struct net_device_ops vmxnet3_netdev_ops = {
2924 .ndo_open = vmxnet3_open,
2925 .ndo_stop = vmxnet3_close,
2926 .ndo_start_xmit = vmxnet3_xmit_frame,
2927 .ndo_set_mac_address = vmxnet3_set_mac_addr,
2928 .ndo_change_mtu = vmxnet3_change_mtu,
Michał Mirosława0d27302011-04-18 13:31:21 +00002929 .ndo_set_features = vmxnet3_set_features,
stephen hemminger95305f62011-06-08 14:53:57 +00002930 .ndo_get_stats64 = vmxnet3_get_stats64,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002931 .ndo_tx_timeout = vmxnet3_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002932 .ndo_set_rx_mode = vmxnet3_set_mc,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002933 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
2934 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
2935#ifdef CONFIG_NET_POLL_CONTROLLER
2936 .ndo_poll_controller = vmxnet3_netpoll,
2937#endif
2938 };
2939 int err;
2940 bool dma64 = false; /* stupid gcc */
2941 u32 ver;
2942 struct net_device *netdev;
2943 struct vmxnet3_adapter *adapter;
2944 u8 mac[ETH_ALEN];
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002945 int size;
2946 int num_tx_queues;
2947 int num_rx_queues;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002948
Shreyas Bhatewarae154b632011-05-10 06:13:56 +00002949 if (!pci_msi_enabled())
2950 enable_mq = 0;
2951
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002952#ifdef VMXNET3_RSS
2953 if (enable_mq)
2954 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
2955 (int)num_online_cpus());
2956 else
2957#endif
2958 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002959 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002960
2961 if (enable_mq)
2962 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
2963 (int)num_online_cpus());
2964 else
2965 num_tx_queues = 1;
2966
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07002967 num_tx_queues = rounddown_pow_of_two(num_tx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002968 netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
2969 max(num_tx_queues, num_rx_queues));
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002970 dev_info(&pdev->dev,
2971 "# of Tx queues : %d, # of Rx queues : %d\n",
2972 num_tx_queues, num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002973
Joe Perches41de8d42012-01-29 13:47:52 +00002974 if (!netdev)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002975 return -ENOMEM;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002976
2977 pci_set_drvdata(pdev, netdev);
2978 adapter = netdev_priv(netdev);
2979 adapter->netdev = netdev;
2980 adapter->pdev = pdev;
2981
Neil Hormanf00e2b02014-06-13 10:03:21 -04002982 adapter->tx_ring_size = VMXNET3_DEF_TX_RING_SIZE;
2983 adapter->rx_ring_size = VMXNET3_DEF_RX_RING_SIZE;
Shrikrishna Khare53831aa2015-01-06 09:20:15 -08002984 adapter->rx_ring2_size = VMXNET3_DEF_RX_RING2_SIZE;
Neil Hormanf00e2b02014-06-13 10:03:21 -04002985
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002986 spin_lock_init(&adapter->cmd_lock);
Andy Kingb0eb57c2013-08-23 09:33:49 -07002987 adapter->adapter_pa = dma_map_single(&adapter->pdev->dev, adapter,
2988 sizeof(struct vmxnet3_adapter),
2989 PCI_DMA_TODEVICE);
2990 adapter->shared = dma_alloc_coherent(
2991 &adapter->pdev->dev,
2992 sizeof(struct Vmxnet3_DriverShared),
2993 &adapter->shared_pa, GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002994 if (!adapter->shared) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002995 dev_err(&pdev->dev, "Failed to allocate memory\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002996 err = -ENOMEM;
2997 goto err_alloc_shared;
2998 }
2999
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003000 adapter->num_rx_queues = num_rx_queues;
3001 adapter->num_tx_queues = num_tx_queues;
Bhavesh Davdae4fabf22013-03-06 12:04:53 +00003002 adapter->rx_buf_per_pkt = 1;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003003
3004 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3005 size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
Andy Kingb0eb57c2013-08-23 09:33:49 -07003006 adapter->tqd_start = dma_alloc_coherent(&adapter->pdev->dev, size,
3007 &adapter->queue_desc_pa,
3008 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003009
3010 if (!adapter->tqd_start) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003011 dev_err(&pdev->dev, "Failed to allocate memory\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003012 err = -ENOMEM;
3013 goto err_alloc_queue_desc;
3014 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003015 adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
stephen hemminger96800ee2012-11-13 13:53:28 +00003016 adapter->num_tx_queues);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003017
Andy Kingb0eb57c2013-08-23 09:33:49 -07003018 adapter->pm_conf = dma_alloc_coherent(&adapter->pdev->dev,
3019 sizeof(struct Vmxnet3_PMConf),
3020 &adapter->pm_conf_pa,
3021 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003022 if (adapter->pm_conf == NULL) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003023 err = -ENOMEM;
3024 goto err_alloc_pm;
3025 }
3026
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003027#ifdef VMXNET3_RSS
3028
Andy Kingb0eb57c2013-08-23 09:33:49 -07003029 adapter->rss_conf = dma_alloc_coherent(&adapter->pdev->dev,
3030 sizeof(struct UPT1_RSSConf),
3031 &adapter->rss_conf_pa,
3032 GFP_KERNEL);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003033 if (adapter->rss_conf == NULL) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003034 err = -ENOMEM;
3035 goto err_alloc_rss;
3036 }
3037#endif /* VMXNET3_RSS */
3038
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003039 err = vmxnet3_alloc_pci_resources(adapter, &dma64);
3040 if (err < 0)
3041 goto err_alloc_pci;
3042
3043 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
3044 if (ver & 1) {
3045 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
3046 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003047 dev_err(&pdev->dev,
3048 "Incompatible h/w version (0x%x) for adapter\n", ver);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003049 err = -EBUSY;
3050 goto err_ver;
3051 }
3052
3053 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
3054 if (ver & 1) {
3055 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
3056 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003057 dev_err(&pdev->dev,
3058 "Incompatible upt version (0x%x) for adapter\n", ver);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003059 err = -EBUSY;
3060 goto err_ver;
3061 }
3062
Shreyas Bhatewarae101e7d2011-07-20 16:01:11 +00003063 SET_NETDEV_DEV(netdev, &pdev->dev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003064 vmxnet3_declare_features(adapter, dma64);
3065
Stephen Hemminger4db37a72013-01-15 07:28:33 +00003066 if (adapter->num_tx_queues == adapter->num_rx_queues)
3067 adapter->share_intr = VMXNET3_INTR_BUDDYSHARE;
3068 else
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003069 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
3070
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003071 vmxnet3_alloc_intr_resources(adapter);
3072
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003073#ifdef VMXNET3_RSS
3074 if (adapter->num_rx_queues > 1 &&
3075 adapter->intr.type == VMXNET3_IT_MSIX) {
3076 adapter->rss = true;
Stephen Hemminger7db11f72013-01-15 07:28:35 +00003077 netdev->hw_features |= NETIF_F_RXHASH;
3078 netdev->features |= NETIF_F_RXHASH;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003079 dev_dbg(&pdev->dev, "RSS is enabled.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003080 } else {
3081 adapter->rss = false;
3082 }
3083#endif
3084
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003085 vmxnet3_read_mac_addr(adapter, mac);
3086 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3087
3088 netdev->netdev_ops = &vmxnet3_netdev_ops;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003089 vmxnet3_set_ethtool_ops(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003090 netdev->watchdog_timeo = 5 * HZ;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003091
3092 INIT_WORK(&adapter->work, vmxnet3_reset_work);
Steve Hodgsone3bc4ff2012-08-14 17:13:36 +01003093 set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003094
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003095 if (adapter->intr.type == VMXNET3_IT_MSIX) {
3096 int i;
3097 for (i = 0; i < adapter->num_rx_queues; i++) {
3098 netif_napi_add(adapter->netdev,
3099 &adapter->rx_queue[i].napi,
3100 vmxnet3_poll_rx_only, 64);
3101 }
3102 } else {
3103 netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
3104 vmxnet3_poll, 64);
3105 }
3106
3107 netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
3108 netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
3109
Neil Horman6cdd20c2013-01-29 16:15:45 -05003110 netif_carrier_off(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003111 err = register_netdev(netdev);
3112
3113 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003114 dev_err(&pdev->dev, "Failed to register adapter\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003115 goto err_register;
3116 }
3117
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00003118 vmxnet3_check_link(adapter, false);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003119 return 0;
3120
3121err_register:
3122 vmxnet3_free_intr_resources(adapter);
3123err_ver:
3124 vmxnet3_free_pci_resources(adapter);
3125err_alloc_pci:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003126#ifdef VMXNET3_RSS
Andy Kingb0eb57c2013-08-23 09:33:49 -07003127 dma_free_coherent(&adapter->pdev->dev, sizeof(struct UPT1_RSSConf),
3128 adapter->rss_conf, adapter->rss_conf_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003129err_alloc_rss:
3130#endif
Andy Kingb0eb57c2013-08-23 09:33:49 -07003131 dma_free_coherent(&adapter->pdev->dev, sizeof(struct Vmxnet3_PMConf),
3132 adapter->pm_conf, adapter->pm_conf_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003133err_alloc_pm:
Andy Kingb0eb57c2013-08-23 09:33:49 -07003134 dma_free_coherent(&adapter->pdev->dev, size, adapter->tqd_start,
3135 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003136err_alloc_queue_desc:
Andy Kingb0eb57c2013-08-23 09:33:49 -07003137 dma_free_coherent(&adapter->pdev->dev,
3138 sizeof(struct Vmxnet3_DriverShared),
3139 adapter->shared, adapter->shared_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003140err_alloc_shared:
Andy Kingb0eb57c2013-08-23 09:33:49 -07003141 dma_unmap_single(&adapter->pdev->dev, adapter->adapter_pa,
3142 sizeof(struct vmxnet3_adapter), PCI_DMA_TODEVICE);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003143 free_netdev(netdev);
3144 return err;
3145}
3146
3147
Bill Pemberton3a4751a2012-12-03 09:24:16 -05003148static void
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003149vmxnet3_remove_device(struct pci_dev *pdev)
3150{
3151 struct net_device *netdev = pci_get_drvdata(pdev);
3152 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003153 int size = 0;
3154 int num_rx_queues;
3155
3156#ifdef VMXNET3_RSS
3157 if (enable_mq)
3158 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3159 (int)num_online_cpus());
3160 else
3161#endif
3162 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07003163 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003164
Tejun Heo23f333a2010-12-12 16:45:14 +01003165 cancel_work_sync(&adapter->work);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003166
3167 unregister_netdev(netdev);
3168
3169 vmxnet3_free_intr_resources(adapter);
3170 vmxnet3_free_pci_resources(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003171#ifdef VMXNET3_RSS
Andy Kingb0eb57c2013-08-23 09:33:49 -07003172 dma_free_coherent(&adapter->pdev->dev, sizeof(struct UPT1_RSSConf),
3173 adapter->rss_conf, adapter->rss_conf_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003174#endif
Andy Kingb0eb57c2013-08-23 09:33:49 -07003175 dma_free_coherent(&adapter->pdev->dev, sizeof(struct Vmxnet3_PMConf),
3176 adapter->pm_conf, adapter->pm_conf_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003177
3178 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3179 size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
Andy Kingb0eb57c2013-08-23 09:33:49 -07003180 dma_free_coherent(&adapter->pdev->dev, size, adapter->tqd_start,
3181 adapter->queue_desc_pa);
3182 dma_free_coherent(&adapter->pdev->dev,
3183 sizeof(struct Vmxnet3_DriverShared),
3184 adapter->shared, adapter->shared_pa);
3185 dma_unmap_single(&adapter->pdev->dev, adapter->adapter_pa,
3186 sizeof(struct vmxnet3_adapter), PCI_DMA_TODEVICE);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003187 free_netdev(netdev);
3188}
3189
Shreyas Bhatewarae9ba47b2015-06-19 13:36:02 -07003190static void vmxnet3_shutdown_device(struct pci_dev *pdev)
3191{
3192 struct net_device *netdev = pci_get_drvdata(pdev);
3193 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3194 unsigned long flags;
3195
3196 /* Reset_work may be in the middle of resetting the device, wait for its
3197 * completion.
3198 */
3199 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
3200 msleep(1);
3201
3202 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED,
3203 &adapter->state)) {
3204 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
3205 return;
3206 }
3207 spin_lock_irqsave(&adapter->cmd_lock, flags);
3208 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3209 VMXNET3_CMD_QUIESCE_DEV);
3210 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3211 vmxnet3_disable_all_intrs(adapter);
3212
3213 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
3214}
3215
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003216
3217#ifdef CONFIG_PM
3218
3219static int
3220vmxnet3_suspend(struct device *device)
3221{
3222 struct pci_dev *pdev = to_pci_dev(device);
3223 struct net_device *netdev = pci_get_drvdata(pdev);
3224 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3225 struct Vmxnet3_PMConf *pmConf;
3226 struct ethhdr *ehdr;
3227 struct arphdr *ahdr;
3228 u8 *arpreq;
3229 struct in_device *in_dev;
3230 struct in_ifaddr *ifa;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003231 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003232 int i = 0;
3233
3234 if (!netif_running(netdev))
3235 return 0;
3236
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003237 for (i = 0; i < adapter->num_rx_queues; i++)
3238 napi_disable(&adapter->rx_queue[i].napi);
3239
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003240 vmxnet3_disable_all_intrs(adapter);
3241 vmxnet3_free_irqs(adapter);
3242 vmxnet3_free_intr_resources(adapter);
3243
3244 netif_device_detach(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003245 netif_tx_stop_all_queues(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003246
3247 /* Create wake-up filters. */
3248 pmConf = adapter->pm_conf;
3249 memset(pmConf, 0, sizeof(*pmConf));
3250
3251 if (adapter->wol & WAKE_UCAST) {
3252 pmConf->filters[i].patternSize = ETH_ALEN;
3253 pmConf->filters[i].maskSize = 1;
3254 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
3255 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
3256
Harvey Harrison3843e512010-10-21 18:05:32 +00003257 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003258 i++;
3259 }
3260
3261 if (adapter->wol & WAKE_ARP) {
3262 in_dev = in_dev_get(netdev);
3263 if (!in_dev)
3264 goto skip_arp;
3265
3266 ifa = (struct in_ifaddr *)in_dev->ifa_list;
3267 if (!ifa)
3268 goto skip_arp;
3269
3270 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
3271 sizeof(struct arphdr) + /* ARP header */
3272 2 * ETH_ALEN + /* 2 Ethernet addresses*/
3273 2 * sizeof(u32); /*2 IPv4 addresses */
3274 pmConf->filters[i].maskSize =
3275 (pmConf->filters[i].patternSize - 1) / 8 + 1;
3276
3277 /* ETH_P_ARP in Ethernet header. */
3278 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
3279 ehdr->h_proto = htons(ETH_P_ARP);
3280
3281 /* ARPOP_REQUEST in ARP header. */
3282 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
3283 ahdr->ar_op = htons(ARPOP_REQUEST);
3284 arpreq = (u8 *)(ahdr + 1);
3285
3286 /* The Unicast IPv4 address in 'tip' field. */
3287 arpreq += 2 * ETH_ALEN + sizeof(u32);
3288 *(u32 *)arpreq = ifa->ifa_address;
3289
3290 /* The mask for the relevant bits. */
3291 pmConf->filters[i].mask[0] = 0x00;
3292 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
3293 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
3294 pmConf->filters[i].mask[3] = 0x00;
3295 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
3296 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
3297 in_dev_put(in_dev);
3298
Harvey Harrison3843e512010-10-21 18:05:32 +00003299 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003300 i++;
3301 }
3302
3303skip_arp:
3304 if (adapter->wol & WAKE_MAGIC)
Harvey Harrison3843e512010-10-21 18:05:32 +00003305 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003306
3307 pmConf->numFilters = i;
3308
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003309 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3310 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3311 *pmConf));
Andy Kingb0eb57c2013-08-23 09:33:49 -07003312 adapter->shared->devRead.pmConfDesc.confPA =
3313 cpu_to_le64(adapter->pm_conf_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003314
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003315 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003316 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3317 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003318 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003319
3320 pci_save_state(pdev);
3321 pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
3322 adapter->wol);
3323 pci_disable_device(pdev);
3324 pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
3325
3326 return 0;
3327}
3328
3329
3330static int
3331vmxnet3_resume(struct device *device)
3332{
Shrikrishna Khare5ec82c12015-01-09 15:19:14 -08003333 int err;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003334 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003335 struct pci_dev *pdev = to_pci_dev(device);
3336 struct net_device *netdev = pci_get_drvdata(pdev);
3337 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003338
3339 if (!netif_running(netdev))
3340 return 0;
3341
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003342 pci_set_power_state(pdev, PCI_D0);
3343 pci_restore_state(pdev);
3344 err = pci_enable_device_mem(pdev);
3345 if (err != 0)
3346 return err;
3347
3348 pci_enable_wake(pdev, PCI_D0, 0);
3349
Shrikrishna Khare5ec82c12015-01-09 15:19:14 -08003350 vmxnet3_alloc_intr_resources(adapter);
3351
3352 /* During hibernate and suspend, device has to be reinitialized as the
3353 * device state need not be preserved.
3354 */
3355
3356 /* Need not check adapter state as other reset tasks cannot run during
3357 * device resume.
3358 */
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003359 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003360 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
Shrikrishna Khare5ec82c12015-01-09 15:19:14 -08003361 VMXNET3_CMD_QUIESCE_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003362 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shrikrishna Khare5ec82c12015-01-09 15:19:14 -08003363 vmxnet3_tq_cleanup_all(adapter);
3364 vmxnet3_rq_cleanup_all(adapter);
3365
3366 vmxnet3_reset_dev(adapter);
3367 err = vmxnet3_activate_dev(adapter);
3368 if (err != 0) {
3369 netdev_err(netdev,
3370 "failed to re-activate on resume, error: %d", err);
3371 vmxnet3_force_close(adapter);
3372 return err;
3373 }
3374 netif_device_attach(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003375
3376 return 0;
3377}
3378
Alexey Dobriyan47145212009-12-14 18:00:08 -08003379static const struct dev_pm_ops vmxnet3_pm_ops = {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003380 .suspend = vmxnet3_suspend,
3381 .resume = vmxnet3_resume,
Shrikrishna Khare5ec82c12015-01-09 15:19:14 -08003382 .freeze = vmxnet3_suspend,
3383 .restore = vmxnet3_resume,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003384};
3385#endif
3386
3387static struct pci_driver vmxnet3_driver = {
3388 .name = vmxnet3_driver_name,
3389 .id_table = vmxnet3_pciid_table,
3390 .probe = vmxnet3_probe_device,
Bill Pemberton3a4751a2012-12-03 09:24:16 -05003391 .remove = vmxnet3_remove_device,
Shreyas Bhatewarae9ba47b2015-06-19 13:36:02 -07003392 .shutdown = vmxnet3_shutdown_device,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003393#ifdef CONFIG_PM
3394 .driver.pm = &vmxnet3_pm_ops,
3395#endif
3396};
3397
3398
3399static int __init
3400vmxnet3_init_module(void)
3401{
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003402 pr_info("%s - version %s\n", VMXNET3_DRIVER_DESC,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003403 VMXNET3_DRIVER_VERSION_REPORT);
3404 return pci_register_driver(&vmxnet3_driver);
3405}
3406
3407module_init(vmxnet3_init_module);
3408
3409
3410static void
3411vmxnet3_exit_module(void)
3412{
3413 pci_unregister_driver(&vmxnet3_driver);
3414}
3415
3416module_exit(vmxnet3_exit_module);
3417
3418MODULE_AUTHOR("VMware, Inc.");
3419MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
3420MODULE_LICENSE("GPL v2");
3421MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);