blob: 4e42eb04a19818b387be742097d8b253af576578 [file] [log] [blame]
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001/*
2 * Linux driver for VMware's vmxnet3 ethernet NIC.
3 *
Shrikrishna Khare190af102016-06-16 10:51:53 -07004 * Copyright (C) 2008-2016, VMware, Inc. All Rights Reserved.
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07005 *
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 *
Shrikrishna Khare190af102016-06-16 10:51:53 -070023 * Maintained by: pv-drivers@vmware.com
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -070024 *
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) {
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -0700438 dma_free_coherent(&adapter->pdev->dev,
439 tq->data_ring.size * tq->txdata_desc_size,
Andy Kingb0eb57c2013-08-23 09:33:49 -0700440 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
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -0700481 memset(tq->data_ring.base, 0,
482 tq->data_ring.size * tq->txdata_desc_size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700483
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,
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -0700517 tq->data_ring.size * tq->txdata_desc_size,
Andy Kingb0eb57c2013-08-23 09:33:49 -0700518 &tq->data_ring.basePA, GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700519 if (!tq->data_ring.base) {
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -0700520 netdev_err(adapter->netdev, "failed to allocate tx 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);
Alexey Khoroshilov5738a092015-11-28 01:29:30 +0300590 if (dma_mapping_error(&adapter->pdev->dev,
591 rbi->dma_addr)) {
592 dev_kfree_skb_any(rbi->skb);
593 rq->stats.rx_buf_alloc_failure++;
594 break;
595 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700596 } else {
597 /* rx buffer skipped by the device */
598 }
599 val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
600 } else {
601 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
602 rbi->len != PAGE_SIZE);
603
604 if (rbi->page == NULL) {
605 rbi->page = alloc_page(GFP_ATOMIC);
606 if (unlikely(rbi->page == NULL)) {
607 rq->stats.rx_buf_alloc_failure++;
608 break;
609 }
Andy Kingb0eb57c2013-08-23 09:33:49 -0700610 rbi->dma_addr = dma_map_page(
611 &adapter->pdev->dev,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700612 rbi->page, 0, PAGE_SIZE,
613 PCI_DMA_FROMDEVICE);
Alexey Khoroshilov5738a092015-11-28 01:29:30 +0300614 if (dma_mapping_error(&adapter->pdev->dev,
615 rbi->dma_addr)) {
616 put_page(rbi->page);
617 rq->stats.rx_buf_alloc_failure++;
618 break;
619 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700620 } else {
621 /* rx buffers skipped by the device */
622 }
623 val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
624 }
625
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000626 gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000627 gd->dword[2] = cpu_to_le32((!ring->gen << VMXNET3_RXD_GEN_SHIFT)
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000628 | val | rbi->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700629
Shreyas Bhatewara5318d802011-07-05 14:34:05 +0000630 /* Fill the last buffer but dont mark it ready, or else the
631 * device will think that the queue is full */
632 if (num_allocated == num_to_alloc)
633 break;
634
635 gd->dword[2] |= cpu_to_le32(ring->gen << VMXNET3_RXD_GEN_SHIFT);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700636 num_allocated++;
637 vmxnet3_cmd_ring_adv_next2fill(ring);
638 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700639
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000640 netdev_dbg(adapter->netdev,
Stephen Hemminger69b9a712013-01-15 07:28:27 +0000641 "alloc_rx_buf: %d allocated, next2fill %u, next2comp %u\n",
642 num_allocated, ring->next2fill, ring->next2comp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700643
644 /* so that the device can distinguish a full ring and an empty ring */
645 BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
646
647 return num_allocated;
648}
649
650
651static void
652vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
653 struct vmxnet3_rx_buf_info *rbi)
654{
655 struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
656 skb_shinfo(skb)->nr_frags;
657
658 BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
659
Ian Campbell0e0634d2011-09-21 21:53:28 +0000660 __skb_frag_set_page(frag, rbi->page);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700661 frag->page_offset = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000662 skb_frag_size_set(frag, rcd->len);
663 skb->data_len += rcd->len;
Eric Dumazet5e6c3552011-10-13 11:38:17 +0000664 skb->truesize += PAGE_SIZE;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700665 skb_shinfo(skb)->nr_frags++;
666}
667
668
Alexey Khoroshilov5738a092015-11-28 01:29:30 +0300669static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700670vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
671 struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
672 struct vmxnet3_adapter *adapter)
673{
674 u32 dw2, len;
675 unsigned long buf_offset;
676 int i;
677 union Vmxnet3_GenericDesc *gdesc;
678 struct vmxnet3_tx_buf_info *tbi = NULL;
679
680 BUG_ON(ctx->copy_size > skb_headlen(skb));
681
682 /* use the previous gen bit for the SOP desc */
683 dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
684
685 ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
686 gdesc = ctx->sop_txd; /* both loops below can be skipped */
687
688 /* no need to map the buffer if headers are copied */
689 if (ctx->copy_size) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000690 ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700691 tq->tx_ring.next2fill *
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -0700692 tq->txdata_desc_size);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000693 ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700694 ctx->sop_txd->dword[3] = 0;
695
696 tbi = tq->buf_info + tq->tx_ring.next2fill;
697 tbi->map_type = VMXNET3_MAP_NONE;
698
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000699 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700700 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000701 tq->tx_ring.next2fill,
702 le64_to_cpu(ctx->sop_txd->txd.addr),
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700703 ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
704 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
705
706 /* use the right gen for non-SOP desc */
707 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
708 }
709
710 /* linear part can use multiple tx desc if it's big */
711 len = skb_headlen(skb) - ctx->copy_size;
712 buf_offset = ctx->copy_size;
713 while (len) {
714 u32 buf_size;
715
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000716 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
717 buf_size = len;
718 dw2 |= len;
719 } else {
720 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
721 /* spec says that for TxDesc.len, 0 == 2^14 */
722 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700723
724 tbi = tq->buf_info + tq->tx_ring.next2fill;
725 tbi->map_type = VMXNET3_MAP_SINGLE;
Andy Kingb0eb57c2013-08-23 09:33:49 -0700726 tbi->dma_addr = dma_map_single(&adapter->pdev->dev,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700727 skb->data + buf_offset, buf_size,
728 PCI_DMA_TODEVICE);
Alexey Khoroshilov5738a092015-11-28 01:29:30 +0300729 if (dma_mapping_error(&adapter->pdev->dev, tbi->dma_addr))
730 return -EFAULT;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700731
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000732 tbi->len = buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700733
734 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
735 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
736
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000737 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
Bhavesh Davda1f4b1612010-07-24 14:43:29 +0000738 gdesc->dword[2] = cpu_to_le32(dw2);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700739 gdesc->dword[3] = 0;
740
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000741 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700742 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000743 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
744 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700745 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
746 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
747
748 len -= buf_size;
749 buf_offset += buf_size;
750 }
751
752 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000753 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000754 u32 buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700755
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000756 buf_offset = 0;
757 len = skb_frag_size(frag);
758 while (len) {
759 tbi = tq->buf_info + tq->tx_ring.next2fill;
760 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
761 buf_size = len;
762 dw2 |= len;
763 } else {
764 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
765 /* spec says that for TxDesc.len, 0 == 2^14 */
766 }
767 tbi->map_type = VMXNET3_MAP_PAGE;
768 tbi->dma_addr = skb_frag_dma_map(&adapter->pdev->dev, frag,
769 buf_offset, buf_size,
770 DMA_TO_DEVICE);
Alexey Khoroshilov5738a092015-11-28 01:29:30 +0300771 if (dma_mapping_error(&adapter->pdev->dev, tbi->dma_addr))
772 return -EFAULT;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700773
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000774 tbi->len = buf_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700775
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000776 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
777 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700778
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000779 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
780 gdesc->dword[2] = cpu_to_le32(dw2);
781 gdesc->dword[3] = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700782
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000783 netdev_dbg(adapter->netdev,
Hans Wennborg8b429462014-08-05 21:42:41 -0700784 "txd[%u]: 0x%llx %u %u\n",
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000785 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
786 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
787 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
788 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
789
790 len -= buf_size;
791 buf_offset += buf_size;
792 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700793 }
794
795 ctx->eop_txd = gdesc;
796
797 /* set the last buf_info for the pkt */
798 tbi->skb = skb;
799 tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
Alexey Khoroshilov5738a092015-11-28 01:29:30 +0300800
801 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700802}
803
804
Shreyas Bhatewara09c50882010-11-19 10:55:24 +0000805/* Init all tx queues */
806static void
807vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
808{
809 int i;
810
811 for (i = 0; i < adapter->num_tx_queues; i++)
812 vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
813}
814
815
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700816/*
Neil Hormancec05562016-03-04 13:40:48 -0500817 * parse relevant protocol headers:
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700818 * For a tso pkt, relevant headers are L2/3/4 including options
819 * For a pkt requesting csum offloading, they are L2/3 and may include L4
820 * if it's a TCP/UDP pkt
821 *
822 * Returns:
823 * -1: error happens during parsing
824 * 0: protocol headers parsed, but too big to be copied
825 * 1: protocol headers parsed and copied
826 *
827 * Other effects:
828 * 1. related *ctx fields are updated.
829 * 2. ctx->copy_size is # of bytes copied
Neil Hormancec05562016-03-04 13:40:48 -0500830 * 3. the portion to be copied is guaranteed to be in the linear part
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700831 *
832 */
833static int
Neil Hormancec05562016-03-04 13:40:48 -0500834vmxnet3_parse_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
835 struct vmxnet3_tx_ctx *ctx,
836 struct vmxnet3_adapter *adapter)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700837{
Shrikrishna Khare759c9352015-02-28 20:33:09 -0800838 u8 protocol = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700839
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000840 if (ctx->mss) { /* TSO */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700841 ctx->eth_ip_hdr_size = skb_transport_offset(skb);
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000842 ctx->l4_hdr_size = tcp_hdrlen(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700843 ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
844 } else {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700845 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000846 ctx->eth_ip_hdr_size = skb_checksum_start_offset(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700847
848 if (ctx->ipv4) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000849 const struct iphdr *iph = ip_hdr(skb);
850
Shrikrishna Khare759c9352015-02-28 20:33:09 -0800851 protocol = iph->protocol;
852 } else if (ctx->ipv6) {
853 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
854
855 protocol = ipv6h->nexthdr;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700856 }
Shrikrishna Khare759c9352015-02-28 20:33:09 -0800857
858 switch (protocol) {
859 case IPPROTO_TCP:
860 ctx->l4_hdr_size = tcp_hdrlen(skb);
861 break;
862 case IPPROTO_UDP:
863 ctx->l4_hdr_size = sizeof(struct udphdr);
864 break;
865 default:
866 ctx->l4_hdr_size = 0;
867 break;
868 }
869
Neil Hormanb2032622012-02-16 01:48:56 +0000870 ctx->copy_size = min(ctx->eth_ip_hdr_size +
871 ctx->l4_hdr_size, skb->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700872 } else {
873 ctx->eth_ip_hdr_size = 0;
874 ctx->l4_hdr_size = 0;
875 /* copy as much as allowed */
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -0700876 ctx->copy_size = min_t(unsigned int,
877 tq->txdata_desc_size,
878 skb_headlen(skb));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700879 }
880
Shreyas Bhatewarac41fcce2015-06-19 13:37:03 -0700881 if (skb->len <= VMXNET3_HDR_COPY_SIZE)
882 ctx->copy_size = skb->len;
883
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700884 /* make sure headers are accessible directly */
885 if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
886 goto err;
887 }
888
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -0700889 if (unlikely(ctx->copy_size > tq->txdata_desc_size)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700890 tq->stats.oversized_hdr++;
891 ctx->copy_size = 0;
892 return 0;
893 }
894
Neil Hormancec05562016-03-04 13:40:48 -0500895 return 1;
896err:
897 return -1;
898}
899
900/*
901 * copy relevant protocol headers to the transmit ring:
902 * For a tso pkt, relevant headers are L2/3/4 including options
903 * For a pkt requesting csum offloading, they are L2/3 and may include L4
904 * if it's a TCP/UDP pkt
905 *
906 *
907 * Note that this requires that vmxnet3_parse_hdr be called first to set the
908 * appropriate bits in ctx first
909 */
910static void
911vmxnet3_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
912 struct vmxnet3_tx_ctx *ctx,
913 struct vmxnet3_adapter *adapter)
914{
915 struct Vmxnet3_TxDataDesc *tdd;
916
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700917 tdd = tq->data_ring.base + tq->tx_ring.next2fill;
918
919 memcpy(tdd->data, skb->data, ctx->copy_size);
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +0000920 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -0700921 "copy %u bytes to dataRing[%u]\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700922 ctx->copy_size, tq->tx_ring.next2fill);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700923}
924
925
926static void
927vmxnet3_prepare_tso(struct sk_buff *skb,
928 struct vmxnet3_tx_ctx *ctx)
929{
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000930 struct tcphdr *tcph = tcp_hdr(skb);
931
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700932 if (ctx->ipv4) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000933 struct iphdr *iph = ip_hdr(skb);
934
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700935 iph->check = 0;
936 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
937 IPPROTO_TCP, 0);
Shrikrishna Khare759c9352015-02-28 20:33:09 -0800938 } else if (ctx->ipv6) {
Eric Dumazet8bca5d12012-01-24 19:47:21 +0000939 struct ipv6hdr *iph = ipv6_hdr(skb);
940
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700941 tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
942 IPPROTO_TCP, 0);
943 }
944}
945
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000946static int txd_estimate(const struct sk_buff *skb)
947{
948 int count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
949 int i;
950
951 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
952 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
953
954 count += VMXNET3_TXD_NEEDED(skb_frag_size(frag));
955 }
956 return count;
957}
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700958
959/*
960 * Transmits a pkt thru a given tq
961 * Returns:
962 * NETDEV_TX_OK: descriptors are setup successfully
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300963 * NETDEV_TX_OK: error occurred, the pkt is dropped
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700964 * NETDEV_TX_BUSY: tx ring is full, queue is stopped
965 *
966 * Side-effects:
967 * 1. tx ring may be changed
968 * 2. tq stats may be updated accordingly
969 * 3. shared->txNumDeferred may be updated
970 */
971
972static int
973vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
974 struct vmxnet3_adapter *adapter, struct net_device *netdev)
975{
976 int ret;
977 u32 count;
978 unsigned long flags;
979 struct vmxnet3_tx_ctx ctx;
980 union Vmxnet3_GenericDesc *gdesc;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +0000981#ifdef __BIG_ENDIAN_BITFIELD
982 /* Use temporary descriptor to avoid touching bits multiple times */
983 union Vmxnet3_GenericDesc tempTxDesc;
984#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700985
Eric Dumazeta4d7e482012-10-29 07:30:49 +0000986 count = txd_estimate(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700987
Jesse Gross72e85c42011-06-23 13:04:39 +0000988 ctx.ipv4 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IP));
Shrikrishna Khare759c9352015-02-28 20:33:09 -0800989 ctx.ipv6 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IPV6));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -0700990
991 ctx.mss = skb_shinfo(skb)->gso_size;
992 if (ctx.mss) {
993 if (skb_header_cloned(skb)) {
994 if (unlikely(pskb_expand_head(skb, 0, 0,
995 GFP_ATOMIC) != 0)) {
996 tq->stats.drop_tso++;
997 goto drop_pkt;
998 }
999 tq->stats.copy_skb_header++;
1000 }
1001 vmxnet3_prepare_tso(skb, &ctx);
1002 } else {
1003 if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
1004
1005 /* non-tso pkts must not use more than
1006 * VMXNET3_MAX_TXD_PER_PKT entries
1007 */
1008 if (skb_linearize(skb) != 0) {
1009 tq->stats.drop_too_many_frags++;
1010 goto drop_pkt;
1011 }
1012 tq->stats.linearized++;
1013
1014 /* recalculate the # of descriptors to use */
1015 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
1016 }
1017 }
1018
Neil Hormancec05562016-03-04 13:40:48 -05001019 ret = vmxnet3_parse_hdr(skb, tq, &ctx, adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001020 if (ret >= 0) {
1021 BUG_ON(ret <= 0 && ctx.copy_size != 0);
1022 /* hdrs parsed, check against other limits */
1023 if (ctx.mss) {
1024 if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
1025 VMXNET3_MAX_TX_BUF_SIZE)) {
Arnd Bergmannefc21d92016-03-14 15:53:57 +01001026 tq->stats.drop_oversized_hdr++;
1027 goto drop_pkt;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001028 }
1029 } else {
1030 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1031 if (unlikely(ctx.eth_ip_hdr_size +
1032 skb->csum_offset >
1033 VMXNET3_MAX_CSUM_OFFSET)) {
Arnd Bergmannefc21d92016-03-14 15:53:57 +01001034 tq->stats.drop_oversized_hdr++;
1035 goto drop_pkt;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001036 }
1037 }
1038 }
1039 } else {
1040 tq->stats.drop_hdr_inspect_err++;
Neil Hormancec05562016-03-04 13:40:48 -05001041 goto drop_pkt;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001042 }
1043
Neil Hormancec05562016-03-04 13:40:48 -05001044 spin_lock_irqsave(&tq->tx_lock, flags);
1045
1046 if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
1047 tq->stats.tx_ring_full++;
1048 netdev_dbg(adapter->netdev,
1049 "tx queue stopped on %s, next2comp %u"
1050 " next2fill %u\n", adapter->netdev->name,
1051 tq->tx_ring.next2comp, tq->tx_ring.next2fill);
1052
1053 vmxnet3_tq_stop(tq, adapter);
1054 spin_unlock_irqrestore(&tq->tx_lock, flags);
1055 return NETDEV_TX_BUSY;
1056 }
1057
1058
1059 vmxnet3_copy_hdr(skb, tq, &ctx, adapter);
1060
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001061 /* fill tx descs related to addr & len */
Alexey Khoroshilov5738a092015-11-28 01:29:30 +03001062 if (vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter))
1063 goto unlock_drop_pkt;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001064
1065 /* setup the EOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001066 ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001067
1068 /* setup the SOP desc */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001069#ifdef __BIG_ENDIAN_BITFIELD
1070 gdesc = &tempTxDesc;
1071 gdesc->dword[2] = ctx.sop_txd->dword[2];
1072 gdesc->dword[3] = ctx.sop_txd->dword[3];
1073#else
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001074 gdesc = ctx.sop_txd;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001075#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001076 if (ctx.mss) {
1077 gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
1078 gdesc->txd.om = VMXNET3_OM_TSO;
1079 gdesc->txd.msscof = ctx.mss;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001080 le32_add_cpu(&tq->shared->txNumDeferred, (skb->len -
1081 gdesc->txd.hlen + ctx.mss - 1) / ctx.mss);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001082 } else {
1083 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1084 gdesc->txd.hlen = ctx.eth_ip_hdr_size;
1085 gdesc->txd.om = VMXNET3_OM_CSUM;
1086 gdesc->txd.msscof = ctx.eth_ip_hdr_size +
1087 skb->csum_offset;
1088 } else {
1089 gdesc->txd.om = 0;
1090 gdesc->txd.msscof = 0;
1091 }
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001092 le32_add_cpu(&tq->shared->txNumDeferred, 1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001093 }
1094
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001095 if (skb_vlan_tag_present(skb)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001096 gdesc->txd.ti = 1;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001097 gdesc->txd.tci = skb_vlan_tag_get(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001098 }
1099
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001100 /* finally flips the GEN bit of the SOP desc. */
1101 gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
1102 VMXNET3_TXD_GEN);
1103#ifdef __BIG_ENDIAN_BITFIELD
1104 /* Finished updating in bitfields of Tx Desc, so write them in original
1105 * place.
1106 */
1107 vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
1108 (struct Vmxnet3_TxDesc *)ctx.sop_txd);
1109 gdesc = ctx.sop_txd;
1110#endif
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00001111 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -07001112 "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
Joe Perchesc2fd03a2012-06-04 12:44:18 +00001113 (u32)(ctx.sop_txd -
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001114 tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
1115 le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001116
1117 spin_unlock_irqrestore(&tq->tx_lock, flags);
1118
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001119 if (le32_to_cpu(tq->shared->txNumDeferred) >=
1120 le32_to_cpu(tq->shared->txThreshold)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001121 tq->shared->txNumDeferred = 0;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001122 VMXNET3_WRITE_BAR0_REG(adapter,
1123 VMXNET3_REG_TXPROD + tq->qid * 8,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001124 tq->tx_ring.next2fill);
1125 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001126
1127 return NETDEV_TX_OK;
1128
Dan Carpenterf955e142010-12-20 03:03:15 +00001129unlock_drop_pkt:
1130 spin_unlock_irqrestore(&tq->tx_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001131drop_pkt:
1132 tq->stats.drop_total++;
Eric W. Biedermanb1b71812014-03-15 18:31:16 -07001133 dev_kfree_skb_any(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001134 return NETDEV_TX_OK;
1135}
1136
1137
1138static netdev_tx_t
1139vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1140{
1141 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001142
stephen hemminger96800ee2012-11-13 13:53:28 +00001143 BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
1144 return vmxnet3_tq_xmit(skb,
1145 &adapter->tx_queue[skb->queue_mapping],
1146 adapter, netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001147}
1148
1149
1150static void
1151vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
1152 struct sk_buff *skb,
1153 union Vmxnet3_GenericDesc *gdesc)
1154{
Michał Mirosława0d27302011-04-18 13:31:21 +00001155 if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
Shrikrishna Kharef0d43782016-04-20 18:12:29 -07001156 if (gdesc->rcd.v4 &&
1157 (le32_to_cpu(gdesc->dword[3]) &
1158 VMXNET3_RCD_CSUM_OK) == VMXNET3_RCD_CSUM_OK) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001159 skb->ip_summed = CHECKSUM_UNNECESSARY;
1160 BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
Shrikrishna Kharef0d43782016-04-20 18:12:29 -07001161 BUG_ON(gdesc->rcd.frg);
1162 } else if (gdesc->rcd.v6 && (le32_to_cpu(gdesc->dword[3]) &
1163 (1 << VMXNET3_RCD_TUC_SHIFT))) {
1164 skb->ip_summed = CHECKSUM_UNNECESSARY;
1165 BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001166 BUG_ON(gdesc->rcd.frg);
1167 } else {
1168 if (gdesc->rcd.csum) {
1169 skb->csum = htons(gdesc->rcd.csum);
1170 skb->ip_summed = CHECKSUM_PARTIAL;
1171 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001172 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001173 }
1174 }
1175 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001176 skb_checksum_none_assert(skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001177 }
1178}
1179
1180
1181static void
1182vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
1183 struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
1184{
1185 rq->stats.drop_err++;
1186 if (!rcd->fcs)
1187 rq->stats.drop_fcs++;
1188
1189 rq->stats.drop_total++;
1190
1191 /*
1192 * We do not unmap and chain the rx buffer to the skb.
1193 * We basically pretend this buffer is not used and will be recycled
1194 * by vmxnet3_rq_alloc_rx_buf()
1195 */
1196
1197 /*
1198 * ctx->skb may be NULL if this is the first and the only one
1199 * desc for the pkt
1200 */
1201 if (ctx->skb)
1202 dev_kfree_skb_irq(ctx->skb);
1203
1204 ctx->skb = NULL;
1205}
1206
1207
Shreyas Bhatewara45dac1d2015-06-19 13:38:29 -07001208static u32
1209vmxnet3_get_hdr_len(struct vmxnet3_adapter *adapter, struct sk_buff *skb,
1210 union Vmxnet3_GenericDesc *gdesc)
1211{
1212 u32 hlen, maplen;
1213 union {
1214 void *ptr;
1215 struct ethhdr *eth;
1216 struct iphdr *ipv4;
1217 struct ipv6hdr *ipv6;
1218 struct tcphdr *tcp;
1219 } hdr;
1220 BUG_ON(gdesc->rcd.tcp == 0);
1221
1222 maplen = skb_headlen(skb);
1223 if (unlikely(sizeof(struct iphdr) + sizeof(struct tcphdr) > maplen))
1224 return 0;
1225
1226 hdr.eth = eth_hdr(skb);
1227 if (gdesc->rcd.v4) {
1228 BUG_ON(hdr.eth->h_proto != htons(ETH_P_IP));
1229 hdr.ptr += sizeof(struct ethhdr);
1230 BUG_ON(hdr.ipv4->protocol != IPPROTO_TCP);
1231 hlen = hdr.ipv4->ihl << 2;
1232 hdr.ptr += hdr.ipv4->ihl << 2;
1233 } else if (gdesc->rcd.v6) {
1234 BUG_ON(hdr.eth->h_proto != htons(ETH_P_IPV6));
1235 hdr.ptr += sizeof(struct ethhdr);
1236 /* Use an estimated value, since we also need to handle
1237 * TSO case.
1238 */
1239 if (hdr.ipv6->nexthdr != IPPROTO_TCP)
1240 return sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1241 hlen = sizeof(struct ipv6hdr);
1242 hdr.ptr += sizeof(struct ipv6hdr);
1243 } else {
1244 /* Non-IP pkt, dont estimate header length */
1245 return 0;
1246 }
1247
1248 if (hlen + sizeof(struct tcphdr) > maplen)
1249 return 0;
1250
1251 return (hlen + (hdr.tcp->doff << 2));
1252}
1253
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001254static int
1255vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
1256 struct vmxnet3_adapter *adapter, int quota)
1257{
Joe Perches215faf92010-12-21 02:16:10 -08001258 static const u32 rxprod_reg[2] = {
1259 VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2
1260 };
Neil Horman07696362015-07-07 14:02:18 -04001261 u32 num_pkts = 0;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001262 bool skip_page_frags = false;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001263 struct Vmxnet3_RxCompDesc *rcd;
1264 struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
Shreyas Bhatewara45dac1d2015-06-19 13:38:29 -07001265 u16 segCnt = 0, mss = 0;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001266#ifdef __BIG_ENDIAN_BITFIELD
1267 struct Vmxnet3_RxDesc rxCmdDesc;
1268 struct Vmxnet3_RxCompDesc rxComp;
1269#endif
1270 vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
1271 &rxComp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001272 while (rcd->gen == rq->comp_ring.gen) {
1273 struct vmxnet3_rx_buf_info *rbi;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001274 struct sk_buff *skb, *new_skb = NULL;
1275 struct page *new_page = NULL;
Alexey Khoroshilov5738a092015-11-28 01:29:30 +03001276 dma_addr_t new_dma_addr;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001277 int num_to_alloc;
1278 struct Vmxnet3_RxDesc *rxd;
1279 u32 idx, ring_idx;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001280 struct vmxnet3_cmd_ring *ring = NULL;
Neil Horman07696362015-07-07 14:02:18 -04001281 if (num_pkts >= quota) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001282 /* we may stop even before we see the EOP desc of
1283 * the current pkt
1284 */
1285 break;
1286 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001287 BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001288 idx = rcd->rxdIdx;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001289 ring_idx = rcd->rqID < adapter->num_rx_queues ? 0 : 1;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001290 ring = rq->rx_ring + ring_idx;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001291 vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
1292 &rxCmdDesc);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001293 rbi = rq->buf_info[ring_idx] + idx;
1294
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001295 BUG_ON(rxd->addr != rbi->dma_addr ||
1296 rxd->len != rbi->len);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001297
1298 if (unlikely(rcd->eop && rcd->err)) {
1299 vmxnet3_rx_error(rq, rcd, ctx, adapter);
1300 goto rcd_done;
1301 }
1302
1303 if (rcd->sop) { /* first buf of the pkt */
1304 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
1305 rcd->rqID != rq->qid);
1306
1307 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
1308 BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
1309
1310 if (unlikely(rcd->len == 0)) {
1311 /* Pretend the rx buffer is skipped. */
1312 BUG_ON(!(rcd->sop && rcd->eop));
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00001313 netdev_dbg(adapter->netdev,
Randy Dunlapf69655822009-10-16 17:54:34 -07001314 "rxRing[%u][%u] 0 length\n",
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001315 ring_idx, idx);
1316 goto rcd_done;
1317 }
1318
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001319 skip_page_frags = false;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001320 ctx->skb = rbi->skb;
Stephen Hemminger0d735f12013-01-15 07:28:26 +00001321 new_skb = netdev_alloc_skb_ip_align(adapter->netdev,
1322 rbi->len);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001323 if (new_skb == NULL) {
1324 /* Skb allocation failed, do not handover this
1325 * skb to stack. Reuse it. Drop the existing pkt
1326 */
1327 rq->stats.rx_buf_alloc_failure++;
1328 ctx->skb = NULL;
1329 rq->stats.drop_total++;
1330 skip_page_frags = true;
1331 goto rcd_done;
1332 }
Alexey Khoroshilov5738a092015-11-28 01:29:30 +03001333 new_dma_addr = dma_map_single(&adapter->pdev->dev,
1334 new_skb->data, rbi->len,
1335 PCI_DMA_FROMDEVICE);
1336 if (dma_mapping_error(&adapter->pdev->dev,
1337 new_dma_addr)) {
1338 dev_kfree_skb(new_skb);
1339 /* Skb allocation failed, do not handover this
1340 * skb to stack. Reuse it. Drop the existing pkt
1341 */
1342 rq->stats.rx_buf_alloc_failure++;
1343 ctx->skb = NULL;
1344 rq->stats.drop_total++;
1345 skip_page_frags = true;
1346 goto rcd_done;
1347 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001348
Andy Kingb0eb57c2013-08-23 09:33:49 -07001349 dma_unmap_single(&adapter->pdev->dev, rbi->dma_addr,
1350 rbi->len,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001351 PCI_DMA_FROMDEVICE);
1352
Stephen Hemminger7db11f72013-01-15 07:28:35 +00001353#ifdef VMXNET3_RSS
1354 if (rcd->rssType != VMXNET3_RCD_RSS_TYPE_NONE &&
1355 (adapter->netdev->features & NETIF_F_RXHASH))
Michal Schmidt2c15a152013-12-20 13:16:57 +01001356 skb_set_hash(ctx->skb,
1357 le32_to_cpu(rcd->rssHash),
Tom Herbert0b680702013-12-17 23:32:08 -08001358 PKT_HASH_TYPE_L3);
Stephen Hemminger7db11f72013-01-15 07:28:35 +00001359#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001360 skb_put(ctx->skb, rcd->len);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001361
1362 /* Immediate refill */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001363 rbi->skb = new_skb;
Alexey Khoroshilov5738a092015-11-28 01:29:30 +03001364 rbi->dma_addr = new_dma_addr;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001365 rxd->addr = cpu_to_le64(rbi->dma_addr);
1366 rxd->len = rbi->len;
Shrikrishna Khare190af102016-06-16 10:51:53 -07001367 if (VMXNET3_VERSION_GE_2(adapter) &&
Shreyas Bhatewara45dac1d2015-06-19 13:38:29 -07001368 rcd->type == VMXNET3_CDTYPE_RXCOMP_LRO) {
1369 struct Vmxnet3_RxCompDescExt *rcdlro;
1370 rcdlro = (struct Vmxnet3_RxCompDescExt *)rcd;
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001371
Shreyas Bhatewara45dac1d2015-06-19 13:38:29 -07001372 segCnt = rcdlro->segCnt;
Shrikrishna Khare50219532016-06-08 07:40:53 -07001373 WARN_ON_ONCE(segCnt == 0);
Shreyas Bhatewara45dac1d2015-06-19 13:38:29 -07001374 mss = rcdlro->mss;
1375 if (unlikely(segCnt <= 1))
1376 segCnt = 0;
1377 } else {
1378 segCnt = 0;
1379 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001380 } else {
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001381 BUG_ON(ctx->skb == NULL && !skip_page_frags);
1382
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001383 /* non SOP buffer must be type 1 in most cases */
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001384 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE);
1385 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001386
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001387 /* If an sop buffer was dropped, skip all
1388 * following non-sop fragments. They will be reused.
1389 */
1390 if (skip_page_frags)
1391 goto rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001392
Shreyas Bhatewarac41fcce2015-06-19 13:37:03 -07001393 if (rcd->len) {
1394 new_page = alloc_page(GFP_ATOMIC);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001395 /* Replacement page frag could not be allocated.
1396 * Reuse this page. Drop the pkt and free the
1397 * skb which contained this page as a frag. Skip
1398 * processing all the following non-sop frags.
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001399 */
Shreyas Bhatewarac41fcce2015-06-19 13:37:03 -07001400 if (unlikely(!new_page)) {
1401 rq->stats.rx_buf_alloc_failure++;
1402 dev_kfree_skb(ctx->skb);
1403 ctx->skb = NULL;
1404 skip_page_frags = true;
1405 goto rcd_done;
1406 }
Shrikrishna Khare58caf632016-01-06 10:44:27 -08001407 new_dma_addr = dma_map_page(&adapter->pdev->dev,
1408 new_page,
1409 0, PAGE_SIZE,
1410 PCI_DMA_FROMDEVICE);
Alexey Khoroshilov5738a092015-11-28 01:29:30 +03001411 if (dma_mapping_error(&adapter->pdev->dev,
1412 new_dma_addr)) {
1413 put_page(new_page);
1414 rq->stats.rx_buf_alloc_failure++;
1415 dev_kfree_skb(ctx->skb);
1416 ctx->skb = NULL;
1417 skip_page_frags = true;
1418 goto rcd_done;
1419 }
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001420
Andy Kingb0eb57c2013-08-23 09:33:49 -07001421 dma_unmap_page(&adapter->pdev->dev,
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001422 rbi->dma_addr, rbi->len,
1423 PCI_DMA_FROMDEVICE);
1424
1425 vmxnet3_append_frag(ctx->skb, rcd, rbi);
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001426
Shreyas Bhatewarac41fcce2015-06-19 13:37:03 -07001427 /* Immediate refill */
1428 rbi->page = new_page;
Alexey Khoroshilov5738a092015-11-28 01:29:30 +03001429 rbi->dma_addr = new_dma_addr;
Shreyas Bhatewarac41fcce2015-06-19 13:37:03 -07001430 rxd->addr = cpu_to_le64(rbi->dma_addr);
1431 rxd->len = rbi->len;
1432 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001433 }
1434
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001435
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001436 skb = ctx->skb;
1437 if (rcd->eop) {
Shreyas Bhatewara45dac1d2015-06-19 13:38:29 -07001438 u32 mtu = adapter->netdev->mtu;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001439 skb->len += skb->data_len;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001440
1441 vmxnet3_rx_csum(adapter, skb,
1442 (union Vmxnet3_GenericDesc *)rcd);
1443 skb->protocol = eth_type_trans(skb, adapter->netdev);
Shreyas Bhatewara45dac1d2015-06-19 13:38:29 -07001444 if (!rcd->tcp || !adapter->lro)
1445 goto not_lro;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001446
Shreyas Bhatewara45dac1d2015-06-19 13:38:29 -07001447 if (segCnt != 0 && mss != 0) {
1448 skb_shinfo(skb)->gso_type = rcd->v4 ?
1449 SKB_GSO_TCPV4 : SKB_GSO_TCPV6;
1450 skb_shinfo(skb)->gso_size = mss;
1451 skb_shinfo(skb)->gso_segs = segCnt;
1452 } else if (segCnt != 0 || skb->len > mtu) {
1453 u32 hlen;
1454
1455 hlen = vmxnet3_get_hdr_len(adapter, skb,
1456 (union Vmxnet3_GenericDesc *)rcd);
1457 if (hlen == 0)
1458 goto not_lro;
1459
1460 skb_shinfo(skb)->gso_type =
1461 rcd->v4 ? SKB_GSO_TCPV4 : SKB_GSO_TCPV6;
1462 if (segCnt != 0) {
1463 skb_shinfo(skb)->gso_segs = segCnt;
1464 skb_shinfo(skb)->gso_size =
1465 DIV_ROUND_UP(skb->len -
1466 hlen, segCnt);
1467 } else {
1468 skb_shinfo(skb)->gso_size = mtu - hlen;
1469 }
1470 }
1471not_lro:
Jesse Gross72e85c42011-06-23 13:04:39 +00001472 if (unlikely(rcd->ts))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001473 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rcd->tci);
Jesse Gross72e85c42011-06-23 13:04:39 +00001474
Jesse Gross213ade82011-06-24 14:24:35 +00001475 if (adapter->netdev->features & NETIF_F_LRO)
1476 netif_receive_skb(skb);
1477 else
1478 napi_gro_receive(&rq->napi, skb);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001479
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001480 ctx->skb = NULL;
Neil Horman07696362015-07-07 14:02:18 -04001481 num_pkts++;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001482 }
1483
1484rcd_done:
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001485 /* device may have skipped some rx descs */
1486 ring->next2comp = idx;
1487 num_to_alloc = vmxnet3_cmd_ring_desc_avail(ring);
1488 ring = rq->rx_ring + ring_idx;
1489 while (num_to_alloc) {
1490 vmxnet3_getRxDesc(rxd, &ring->base[ring->next2fill].rxd,
1491 &rxCmdDesc);
1492 BUG_ON(!rxd->addr);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001493
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001494 /* Recv desc is ready to be used by the device */
1495 rxd->gen = ring->gen;
1496 vmxnet3_cmd_ring_adv_next2fill(ring);
1497 num_to_alloc--;
1498 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001499
Shreyas Bhatewara5318d802011-07-05 14:34:05 +00001500 /* if needed, update the register */
1501 if (unlikely(rq->shared->updateRxProd)) {
1502 VMXNET3_WRITE_BAR0_REG(adapter,
stephen hemminger96800ee2012-11-13 13:53:28 +00001503 rxprod_reg[ring_idx] + rq->qid * 8,
1504 ring->next2fill);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001505 }
1506
1507 vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001508 vmxnet3_getRxComp(rcd,
stephen hemminger96800ee2012-11-13 13:53:28 +00001509 &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001510 }
1511
Neil Horman07696362015-07-07 14:02:18 -04001512 return num_pkts;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001513}
1514
1515
1516static void
1517vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
1518 struct vmxnet3_adapter *adapter)
1519{
1520 u32 i, ring_idx;
1521 struct Vmxnet3_RxDesc *rxd;
1522
1523 for (ring_idx = 0; ring_idx < 2; ring_idx++) {
1524 for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00001525#ifdef __BIG_ENDIAN_BITFIELD
1526 struct Vmxnet3_RxDesc rxDesc;
1527#endif
1528 vmxnet3_getRxDesc(rxd,
1529 &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001530
1531 if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
1532 rq->buf_info[ring_idx][i].skb) {
Andy Kingb0eb57c2013-08-23 09:33:49 -07001533 dma_unmap_single(&adapter->pdev->dev, rxd->addr,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001534 rxd->len, PCI_DMA_FROMDEVICE);
1535 dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
1536 rq->buf_info[ring_idx][i].skb = NULL;
1537 } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
1538 rq->buf_info[ring_idx][i].page) {
Andy Kingb0eb57c2013-08-23 09:33:49 -07001539 dma_unmap_page(&adapter->pdev->dev, rxd->addr,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001540 rxd->len, PCI_DMA_FROMDEVICE);
1541 put_page(rq->buf_info[ring_idx][i].page);
1542 rq->buf_info[ring_idx][i].page = NULL;
1543 }
1544 }
1545
1546 rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
1547 rq->rx_ring[ring_idx].next2fill =
1548 rq->rx_ring[ring_idx].next2comp = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001549 }
1550
1551 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1552 rq->comp_ring.next2proc = 0;
1553}
1554
1555
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001556static void
1557vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
1558{
1559 int i;
1560
1561 for (i = 0; i < adapter->num_rx_queues; i++)
1562 vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
1563}
1564
1565
stephen hemminger280b74f2013-02-22 08:26:29 +00001566static void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
1567 struct vmxnet3_adapter *adapter)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001568{
1569 int i;
1570 int j;
1571
1572 /* all rx buffers must have already been freed */
1573 for (i = 0; i < 2; i++) {
1574 if (rq->buf_info[i]) {
1575 for (j = 0; j < rq->rx_ring[i].size; j++)
1576 BUG_ON(rq->buf_info[i][j].page != NULL);
1577 }
1578 }
1579
1580
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001581 for (i = 0; i < 2; i++) {
1582 if (rq->rx_ring[i].base) {
Andy Kingb0eb57c2013-08-23 09:33:49 -07001583 dma_free_coherent(&adapter->pdev->dev,
1584 rq->rx_ring[i].size
1585 * sizeof(struct Vmxnet3_RxDesc),
1586 rq->rx_ring[i].base,
1587 rq->rx_ring[i].basePA);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001588 rq->rx_ring[i].base = NULL;
1589 }
1590 rq->buf_info[i] = NULL;
1591 }
1592
1593 if (rq->comp_ring.base) {
Andy Kingb0eb57c2013-08-23 09:33:49 -07001594 dma_free_coherent(&adapter->pdev->dev, rq->comp_ring.size
1595 * sizeof(struct Vmxnet3_RxCompDesc),
1596 rq->comp_ring.base, rq->comp_ring.basePA);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001597 rq->comp_ring.base = NULL;
1598 }
Andy Kingb0eb57c2013-08-23 09:33:49 -07001599
1600 if (rq->buf_info[0]) {
1601 size_t sz = sizeof(struct vmxnet3_rx_buf_info) *
1602 (rq->rx_ring[0].size + rq->rx_ring[1].size);
1603 dma_free_coherent(&adapter->pdev->dev, sz, rq->buf_info[0],
1604 rq->buf_info_pa);
1605 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001606}
1607
1608
1609static int
1610vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
1611 struct vmxnet3_adapter *adapter)
1612{
1613 int i;
1614
1615 /* initialize buf_info */
1616 for (i = 0; i < rq->rx_ring[0].size; i++) {
1617
1618 /* 1st buf for a pkt is skbuff */
1619 if (i % adapter->rx_buf_per_pkt == 0) {
1620 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
1621 rq->buf_info[0][i].len = adapter->skb_buf_size;
1622 } else { /* subsequent bufs for a pkt is frag */
1623 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
1624 rq->buf_info[0][i].len = PAGE_SIZE;
1625 }
1626 }
1627 for (i = 0; i < rq->rx_ring[1].size; i++) {
1628 rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
1629 rq->buf_info[1][i].len = PAGE_SIZE;
1630 }
1631
1632 /* reset internal state and allocate buffers for both rings */
1633 for (i = 0; i < 2; i++) {
1634 rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001635
1636 memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
1637 sizeof(struct Vmxnet3_RxDesc));
1638 rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
1639 }
1640 if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
1641 adapter) == 0) {
1642 /* at least has 1 rx buffer for the 1st ring */
1643 return -ENOMEM;
1644 }
1645 vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
1646
1647 /* reset the comp ring */
1648 rq->comp_ring.next2proc = 0;
1649 memset(rq->comp_ring.base, 0, rq->comp_ring.size *
1650 sizeof(struct Vmxnet3_RxCompDesc));
1651 rq->comp_ring.gen = VMXNET3_INIT_GEN;
1652
1653 /* reset rxctx */
1654 rq->rx_ctx.skb = NULL;
1655
1656 /* stats are not reset */
1657 return 0;
1658}
1659
1660
1661static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001662vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
1663{
1664 int i, err = 0;
1665
1666 for (i = 0; i < adapter->num_rx_queues; i++) {
1667 err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
1668 if (unlikely(err)) {
1669 dev_err(&adapter->netdev->dev, "%s: failed to "
1670 "initialize rx queue%i\n",
1671 adapter->netdev->name, i);
1672 break;
1673 }
1674 }
1675 return err;
1676
1677}
1678
1679
1680static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001681vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
1682{
1683 int i;
1684 size_t sz;
1685 struct vmxnet3_rx_buf_info *bi;
1686
1687 for (i = 0; i < 2; i++) {
1688
1689 sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
Andy Kingb0eb57c2013-08-23 09:33:49 -07001690 rq->rx_ring[i].base = dma_alloc_coherent(
1691 &adapter->pdev->dev, sz,
1692 &rq->rx_ring[i].basePA,
1693 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001694 if (!rq->rx_ring[i].base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001695 netdev_err(adapter->netdev,
1696 "failed to allocate rx ring %d\n", i);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001697 goto err;
1698 }
1699 }
1700
1701 sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
Andy Kingb0eb57c2013-08-23 09:33:49 -07001702 rq->comp_ring.base = dma_alloc_coherent(&adapter->pdev->dev, sz,
1703 &rq->comp_ring.basePA,
1704 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001705 if (!rq->comp_ring.base) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00001706 netdev_err(adapter->netdev, "failed to allocate rx comp ring\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001707 goto err;
1708 }
1709
1710 sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
1711 rq->rx_ring[1].size);
Andy Kingb0eb57c2013-08-23 09:33:49 -07001712 bi = dma_zalloc_coherent(&adapter->pdev->dev, sz, &rq->buf_info_pa,
1713 GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001714 if (!bi)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001715 goto err;
Joe Perchese404dec2012-01-29 12:56:23 +00001716
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001717 rq->buf_info[0] = bi;
1718 rq->buf_info[1] = bi + rq->rx_ring[0].size;
1719
1720 return 0;
1721
1722err:
1723 vmxnet3_rq_destroy(rq, adapter);
1724 return -ENOMEM;
1725}
1726
1727
1728static int
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001729vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
1730{
1731 int i, err = 0;
1732
1733 for (i = 0; i < adapter->num_rx_queues; i++) {
1734 err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
1735 if (unlikely(err)) {
1736 dev_err(&adapter->netdev->dev,
1737 "%s: failed to create rx queue%i\n",
1738 adapter->netdev->name, i);
1739 goto err_out;
1740 }
1741 }
1742 return err;
1743err_out:
1744 vmxnet3_rq_destroy_all(adapter);
1745 return err;
1746
1747}
1748
1749/* Multiple queue aware polling function for tx and rx */
1750
1751static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001752vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
1753{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001754 int rcd_done = 0, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001755 if (unlikely(adapter->shared->ecr))
1756 vmxnet3_process_events(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001757 for (i = 0; i < adapter->num_tx_queues; i++)
1758 vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001759
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001760 for (i = 0; i < adapter->num_rx_queues; i++)
1761 rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
1762 adapter, budget);
1763 return rcd_done;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001764}
1765
1766
1767static int
1768vmxnet3_poll(struct napi_struct *napi, int budget)
1769{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001770 struct vmxnet3_rx_queue *rx_queue = container_of(napi,
1771 struct vmxnet3_rx_queue, napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001772 int rxd_done;
1773
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001774 rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001775
1776 if (rxd_done < budget) {
1777 napi_complete(napi);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001778 vmxnet3_enable_all_intrs(rx_queue->adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001779 }
1780 return rxd_done;
1781}
1782
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001783/*
1784 * NAPI polling function for MSI-X mode with multiple Rx queues
1785 * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
1786 */
1787
1788static int
1789vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
1790{
1791 struct vmxnet3_rx_queue *rq = container_of(napi,
1792 struct vmxnet3_rx_queue, napi);
1793 struct vmxnet3_adapter *adapter = rq->adapter;
1794 int rxd_done;
1795
1796 /* When sharing interrupt with corresponding tx queue, process
1797 * tx completions in that queue as well
1798 */
1799 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
1800 struct vmxnet3_tx_queue *tq =
1801 &adapter->tx_queue[rq - adapter->rx_queue];
1802 vmxnet3_tq_tx_complete(tq, adapter);
1803 }
1804
1805 rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
1806
1807 if (rxd_done < budget) {
1808 napi_complete(napi);
1809 vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
1810 }
1811 return rxd_done;
1812}
1813
1814
1815#ifdef CONFIG_PCI_MSI
1816
1817/*
1818 * Handle completion interrupts on tx queues
1819 * Returns whether or not the intr is handled
1820 */
1821
1822static irqreturn_t
1823vmxnet3_msix_tx(int irq, void *data)
1824{
1825 struct vmxnet3_tx_queue *tq = data;
1826 struct vmxnet3_adapter *adapter = tq->adapter;
1827
1828 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1829 vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
1830
1831 /* Handle the case where only one irq is allocate for all tx queues */
1832 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1833 int i;
1834 for (i = 0; i < adapter->num_tx_queues; i++) {
1835 struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
1836 vmxnet3_tq_tx_complete(txq, adapter);
1837 }
1838 } else {
1839 vmxnet3_tq_tx_complete(tq, adapter);
1840 }
1841 vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
1842
1843 return IRQ_HANDLED;
1844}
1845
1846
1847/*
1848 * Handle completion interrupts on rx queues. Returns whether or not the
1849 * intr is handled
1850 */
1851
1852static irqreturn_t
1853vmxnet3_msix_rx(int irq, void *data)
1854{
1855 struct vmxnet3_rx_queue *rq = data;
1856 struct vmxnet3_adapter *adapter = rq->adapter;
1857
1858 /* disable intr if needed */
1859 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1860 vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
1861 napi_schedule(&rq->napi);
1862
1863 return IRQ_HANDLED;
1864}
1865
1866/*
1867 *----------------------------------------------------------------------------
1868 *
1869 * vmxnet3_msix_event --
1870 *
1871 * vmxnet3 msix event intr handler
1872 *
1873 * Result:
1874 * whether or not the intr is handled
1875 *
1876 *----------------------------------------------------------------------------
1877 */
1878
1879static irqreturn_t
1880vmxnet3_msix_event(int irq, void *data)
1881{
1882 struct net_device *dev = data;
1883 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1884
1885 /* disable intr if needed */
1886 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
1887 vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
1888
1889 if (adapter->shared->ecr)
1890 vmxnet3_process_events(adapter);
1891
1892 vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
1893
1894 return IRQ_HANDLED;
1895}
1896
1897#endif /* CONFIG_PCI_MSI */
1898
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001899
1900/* Interrupt handler for vmxnet3 */
1901static irqreturn_t
1902vmxnet3_intr(int irq, void *dev_id)
1903{
1904 struct net_device *dev = dev_id;
1905 struct vmxnet3_adapter *adapter = netdev_priv(dev);
1906
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001907 if (adapter->intr.type == VMXNET3_IT_INTX) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001908 u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
1909 if (unlikely(icr == 0))
1910 /* not ours */
1911 return IRQ_NONE;
1912 }
1913
1914
1915 /* disable intr if needed */
1916 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001917 vmxnet3_disable_all_intrs(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001918
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001919 napi_schedule(&adapter->rx_queue[0].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001920
1921 return IRQ_HANDLED;
1922}
1923
1924#ifdef CONFIG_NET_POLL_CONTROLLER
1925
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001926/* netpoll callback. */
1927static void
1928vmxnet3_netpoll(struct net_device *netdev)
1929{
1930 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001931
Neil Hormand25f06e2014-03-10 06:55:55 -04001932 switch (adapter->intr.type) {
Arnd Bergmann0a8d8c42014-03-13 10:44:34 +01001933#ifdef CONFIG_PCI_MSI
1934 case VMXNET3_IT_MSIX: {
1935 int i;
Neil Hormand25f06e2014-03-10 06:55:55 -04001936 for (i = 0; i < adapter->num_rx_queues; i++)
1937 vmxnet3_msix_rx(0, &adapter->rx_queue[i]);
1938 break;
Arnd Bergmann0a8d8c42014-03-13 10:44:34 +01001939 }
1940#endif
Neil Hormand25f06e2014-03-10 06:55:55 -04001941 case VMXNET3_IT_MSI:
1942 default:
1943 vmxnet3_intr(0, adapter->netdev);
1944 break;
1945 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001946
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001947}
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001948#endif /* CONFIG_NET_POLL_CONTROLLER */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001949
1950static int
1951vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
1952{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001953 struct vmxnet3_intr *intr = &adapter->intr;
1954 int err = 0, i;
1955 int vector = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001956
Randy Dunlap8f7e5242009-10-14 20:38:58 -07001957#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07001958 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00001959 for (i = 0; i < adapter->num_tx_queues; i++) {
1960 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
1961 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
1962 adapter->netdev->name, vector);
1963 err = request_irq(
1964 intr->msix_entries[vector].vector,
1965 vmxnet3_msix_tx, 0,
1966 adapter->tx_queue[i].name,
1967 &adapter->tx_queue[i]);
1968 } else {
1969 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
1970 adapter->netdev->name, vector);
1971 }
1972 if (err) {
1973 dev_err(&adapter->netdev->dev,
1974 "Failed to request irq for MSIX, %s, "
1975 "error %d\n",
1976 adapter->tx_queue[i].name, err);
1977 return err;
1978 }
1979
1980 /* Handle the case where only 1 MSIx was allocated for
1981 * all tx queues */
1982 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
1983 for (; i < adapter->num_tx_queues; i++)
1984 adapter->tx_queue[i].comp_ring.intr_idx
1985 = vector;
1986 vector++;
1987 break;
1988 } else {
1989 adapter->tx_queue[i].comp_ring.intr_idx
1990 = vector++;
1991 }
1992 }
1993 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
1994 vector = 0;
1995
1996 for (i = 0; i < adapter->num_rx_queues; i++) {
1997 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
1998 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
1999 adapter->netdev->name, vector);
2000 else
2001 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
2002 adapter->netdev->name, vector);
2003 err = request_irq(intr->msix_entries[vector].vector,
2004 vmxnet3_msix_rx, 0,
2005 adapter->rx_queue[i].name,
2006 &(adapter->rx_queue[i]));
2007 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002008 netdev_err(adapter->netdev,
2009 "Failed to request irq for MSIX, "
2010 "%s, error %d\n",
2011 adapter->rx_queue[i].name, err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002012 return err;
2013 }
2014
2015 adapter->rx_queue[i].comp_ring.intr_idx = vector++;
2016 }
2017
2018 sprintf(intr->event_msi_vector_name, "%s-event-%d",
2019 adapter->netdev->name, vector);
2020 err = request_irq(intr->msix_entries[vector].vector,
2021 vmxnet3_msix_event, 0,
2022 intr->event_msi_vector_name, adapter->netdev);
2023 intr->event_intr_idx = vector;
2024
2025 } else if (intr->type == VMXNET3_IT_MSI) {
2026 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002027 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
2028 adapter->netdev->name, adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002029 } else {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002030#endif
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002031 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002032 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
2033 IRQF_SHARED, adapter->netdev->name,
2034 adapter->netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002035#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002036 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002037#endif
2038 intr->num_intrs = vector + 1;
2039 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002040 netdev_err(adapter->netdev,
2041 "Failed to request irq (intr type:%d), error %d\n",
2042 intr->type, err);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002043 } else {
2044 /* Number of rx queues will not change after this */
2045 for (i = 0; i < adapter->num_rx_queues; i++) {
2046 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2047 rq->qid = i;
2048 rq->qid2 = i + adapter->num_rx_queues;
2049 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002050
2051
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002052
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002053 /* init our intr settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002054 for (i = 0; i < intr->num_intrs; i++)
2055 intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
2056 if (adapter->intr.type != VMXNET3_IT_MSIX) {
2057 adapter->intr.event_intr_idx = 0;
2058 for (i = 0; i < adapter->num_tx_queues; i++)
2059 adapter->tx_queue[i].comp_ring.intr_idx = 0;
2060 adapter->rx_queue[0].comp_ring.intr_idx = 0;
2061 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002062
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002063 netdev_info(adapter->netdev,
2064 "intr type %u, mode %u, %u vectors allocated\n",
2065 intr->type, intr->mask_mode, intr->num_intrs);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002066 }
2067
2068 return err;
2069}
2070
2071
2072static void
2073vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
2074{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002075 struct vmxnet3_intr *intr = &adapter->intr;
2076 BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002077
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002078 switch (intr->type) {
Randy Dunlap8f7e5242009-10-14 20:38:58 -07002079#ifdef CONFIG_PCI_MSI
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002080 case VMXNET3_IT_MSIX:
2081 {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002082 int i, vector = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002083
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002084 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
2085 for (i = 0; i < adapter->num_tx_queues; i++) {
2086 free_irq(intr->msix_entries[vector++].vector,
2087 &(adapter->tx_queue[i]));
2088 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
2089 break;
2090 }
2091 }
2092
2093 for (i = 0; i < adapter->num_rx_queues; i++) {
2094 free_irq(intr->msix_entries[vector++].vector,
2095 &(adapter->rx_queue[i]));
2096 }
2097
2098 free_irq(intr->msix_entries[vector].vector,
2099 adapter->netdev);
2100 BUG_ON(vector >= intr->num_intrs);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002101 break;
2102 }
Randy Dunlap8f7e5242009-10-14 20:38:58 -07002103#endif
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002104 case VMXNET3_IT_MSI:
2105 free_irq(adapter->pdev->irq, adapter->netdev);
2106 break;
2107 case VMXNET3_IT_INTX:
2108 free_irq(adapter->pdev->irq, adapter->netdev);
2109 break;
2110 default:
Sasha Levinc068e772012-11-08 10:23:03 +00002111 BUG();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002112 }
2113}
2114
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002115
2116static void
2117vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
2118{
Jesse Gross72e85c42011-06-23 13:04:39 +00002119 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2120 u16 vid;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002121
Jesse Gross72e85c42011-06-23 13:04:39 +00002122 /* allow untagged pkts */
2123 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
2124
2125 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
2126 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002127}
2128
2129
Jiri Pirko8e586132011-12-08 19:52:37 -05002130static int
Patrick McHardy80d5c362013-04-19 02:04:28 +00002131vmxnet3_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002132{
2133 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002134
Jesse Grossf6957f82011-08-07 23:15:47 +00002135 if (!(netdev->flags & IFF_PROMISC)) {
2136 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2137 unsigned long flags;
2138
2139 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
2140 spin_lock_irqsave(&adapter->cmd_lock, flags);
2141 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2142 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
2143 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2144 }
Jesse Gross72e85c42011-06-23 13:04:39 +00002145
2146 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05002147
2148 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002149}
2150
2151
Jiri Pirko8e586132011-12-08 19:52:37 -05002152static int
Patrick McHardy80d5c362013-04-19 02:04:28 +00002153vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002154{
2155 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002156
Jesse Grossf6957f82011-08-07 23:15:47 +00002157 if (!(netdev->flags & IFF_PROMISC)) {
2158 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2159 unsigned long flags;
2160
2161 VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
2162 spin_lock_irqsave(&adapter->cmd_lock, flags);
2163 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2164 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
2165 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2166 }
Jesse Gross72e85c42011-06-23 13:04:39 +00002167
2168 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05002169
2170 return 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002171}
2172
2173
2174static u8 *
2175vmxnet3_copy_mc(struct net_device *netdev)
2176{
2177 u8 *buf = NULL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002178 u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002179
2180 /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
2181 if (sz <= 0xffff) {
2182 /* We may be called with BH disabled */
2183 buf = kmalloc(sz, GFP_ATOMIC);
2184 if (buf) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002185 struct netdev_hw_addr *ha;
Jiri Pirko567ec872010-02-23 23:17:07 +00002186 int i = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002187
Jiri Pirko22bedad32010-04-01 21:22:57 +00002188 netdev_for_each_mc_addr(ha, netdev)
2189 memcpy(buf + i++ * ETH_ALEN, ha->addr,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002190 ETH_ALEN);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002191 }
2192 }
2193 return buf;
2194}
2195
2196
2197static void
2198vmxnet3_set_mc(struct net_device *netdev)
2199{
2200 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002201 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002202 struct Vmxnet3_RxFilterConf *rxConf =
2203 &adapter->shared->devRead.rxFilterConf;
2204 u8 *new_table = NULL;
Andy Kingb0eb57c2013-08-23 09:33:49 -07002205 dma_addr_t new_table_pa = 0;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002206 u32 new_mode = VMXNET3_RXM_UCAST;
2207
Jesse Gross72e85c42011-06-23 13:04:39 +00002208 if (netdev->flags & IFF_PROMISC) {
2209 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2210 memset(vfTable, 0, VMXNET3_VFT_SIZE * sizeof(*vfTable));
2211
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002212 new_mode |= VMXNET3_RXM_PROMISC;
Jesse Gross72e85c42011-06-23 13:04:39 +00002213 } else {
2214 vmxnet3_restore_vlan(adapter);
2215 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002216
2217 if (netdev->flags & IFF_BROADCAST)
2218 new_mode |= VMXNET3_RXM_BCAST;
2219
2220 if (netdev->flags & IFF_ALLMULTI)
2221 new_mode |= VMXNET3_RXM_ALL_MULTI;
2222 else
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002223 if (!netdev_mc_empty(netdev)) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002224 new_table = vmxnet3_copy_mc(netdev);
2225 if (new_table) {
Shrikrishna Khared37d5ec2015-11-13 15:42:10 -08002226 size_t sz = netdev_mc_count(netdev) * ETH_ALEN;
2227
2228 rxConf->mfTableLen = cpu_to_le16(sz);
Andy Kingb0eb57c2013-08-23 09:33:49 -07002229 new_table_pa = dma_map_single(
2230 &adapter->pdev->dev,
2231 new_table,
Shrikrishna Khared37d5ec2015-11-13 15:42:10 -08002232 sz,
Andy Kingb0eb57c2013-08-23 09:33:49 -07002233 PCI_DMA_TODEVICE);
Andy King4ad9a642014-09-02 13:13:44 -07002234 }
2235
Alexey Khoroshilov5738a092015-11-28 01:29:30 +03002236 if (!dma_mapping_error(&adapter->pdev->dev,
2237 new_table_pa)) {
Andy King4ad9a642014-09-02 13:13:44 -07002238 new_mode |= VMXNET3_RXM_MCAST;
Andy Kingb0eb57c2013-08-23 09:33:49 -07002239 rxConf->mfTablePA = cpu_to_le64(new_table_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002240 } else {
Andy King4ad9a642014-09-02 13:13:44 -07002241 netdev_info(netdev,
2242 "failed to copy mcast list, setting ALL_MULTI\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002243 new_mode |= VMXNET3_RXM_ALL_MULTI;
2244 }
2245 }
2246
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002247 if (!(new_mode & VMXNET3_RXM_MCAST)) {
2248 rxConf->mfTableLen = 0;
2249 rxConf->mfTablePA = 0;
2250 }
2251
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002252 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002253 if (new_mode != rxConf->rxMode) {
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002254 rxConf->rxMode = cpu_to_le32(new_mode);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002255 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2256 VMXNET3_CMD_UPDATE_RX_MODE);
Jesse Gross72e85c42011-06-23 13:04:39 +00002257 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2258 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002259 }
2260
2261 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2262 VMXNET3_CMD_UPDATE_MAC_FILTERS);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002263 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002264
Andy King4ad9a642014-09-02 13:13:44 -07002265 if (new_table_pa)
Andy Kingb0eb57c2013-08-23 09:33:49 -07002266 dma_unmap_single(&adapter->pdev->dev, new_table_pa,
2267 rxConf->mfTableLen, PCI_DMA_TODEVICE);
Andy King4ad9a642014-09-02 13:13:44 -07002268 kfree(new_table);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002269}
2270
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002271void
2272vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
2273{
2274 int i;
2275
2276 for (i = 0; i < adapter->num_rx_queues; i++)
2277 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2278}
2279
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002280
2281/*
2282 * Set up driver_shared based on settings in adapter.
2283 */
2284
2285static void
2286vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2287{
2288 struct Vmxnet3_DriverShared *shared = adapter->shared;
2289 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2290 struct Vmxnet3_TxQueueConf *tqc;
2291 struct Vmxnet3_RxQueueConf *rqc;
2292 int i;
2293
2294 memset(shared, 0, sizeof(*shared));
2295
2296 /* driver settings */
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002297 shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2298 devRead->misc.driverInfo.version = cpu_to_le32(
2299 VMXNET3_DRIVER_VERSION_NUM);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002300 devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2301 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2302 devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002303 *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2304 *((u32 *)&devRead->misc.driverInfo.gos));
2305 devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2306 devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002307
Andy Kingb0eb57c2013-08-23 09:33:49 -07002308 devRead->misc.ddPA = cpu_to_le64(adapter->adapter_pa);
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002309 devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002310
2311 /* set up feature flags */
Michał Mirosława0d27302011-04-18 13:31:21 +00002312 if (adapter->netdev->features & NETIF_F_RXCSUM)
Harvey Harrison3843e512010-10-21 18:05:32 +00002313 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002314
Michał Mirosława0d27302011-04-18 13:31:21 +00002315 if (adapter->netdev->features & NETIF_F_LRO) {
Harvey Harrison3843e512010-10-21 18:05:32 +00002316 devRead->misc.uptFeatures |= UPT1_F_LRO;
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002317 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002318 }
Patrick McHardyf6469682013-04-19 02:04:27 +00002319 if (adapter->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
Harvey Harrison3843e512010-10-21 18:05:32 +00002320 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002321
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002322 devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2323 devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2324 devRead->misc.queueDescLen = cpu_to_le32(
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002325 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2326 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002327
2328 /* tx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002329 devRead->misc.numTxQueues = adapter->num_tx_queues;
2330 for (i = 0; i < adapter->num_tx_queues; i++) {
2331 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2332 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2333 tqc = &adapter->tqd_start[i].conf;
2334 tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
2335 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2336 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
Andy Kingb0eb57c2013-08-23 09:33:49 -07002337 tqc->ddPA = cpu_to_le64(tq->buf_info_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002338 tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
2339 tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -07002340 tqc->txDataRingDescSize = cpu_to_le32(tq->txdata_desc_size);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002341 tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
2342 tqc->ddLen = cpu_to_le32(
2343 sizeof(struct vmxnet3_tx_buf_info) *
2344 tqc->txRingSize);
2345 tqc->intrIdx = tq->comp_ring.intr_idx;
2346 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002347
2348 /* rx queue settings */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002349 devRead->misc.numRxQueues = adapter->num_rx_queues;
2350 for (i = 0; i < adapter->num_rx_queues; i++) {
2351 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2352 rqc = &adapter->rqd_start[i].conf;
2353 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2354 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2355 rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
Andy Kingb0eb57c2013-08-23 09:33:49 -07002356 rqc->ddPA = cpu_to_le64(rq->buf_info_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002357 rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
2358 rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
2359 rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
2360 rqc->ddLen = cpu_to_le32(
2361 sizeof(struct vmxnet3_rx_buf_info) *
2362 (rqc->rxRingSize[0] +
2363 rqc->rxRingSize[1]));
2364 rqc->intrIdx = rq->comp_ring.intr_idx;
2365 }
2366
2367#ifdef VMXNET3_RSS
2368 memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2369
2370 if (adapter->rss) {
2371 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
Stephen Hemminger66d35912013-01-15 07:28:34 +00002372
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002373 devRead->misc.uptFeatures |= UPT1_F_RSS;
2374 devRead->misc.numRxQueues = adapter->num_rx_queues;
2375 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2376 UPT1_RSS_HASH_TYPE_IPV4 |
2377 UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2378 UPT1_RSS_HASH_TYPE_IPV6;
2379 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2380 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2381 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
Eric Dumazet6bf79cd2014-11-16 06:23:18 -08002382 netdev_rss_key_fill(rssConf->hashKey, sizeof(rssConf->hashKey));
Stephen Hemminger66d35912013-01-15 07:28:34 +00002383
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002384 for (i = 0; i < rssConf->indTableSize; i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00002385 rssConf->indTable[i] = ethtool_rxfh_indir_default(
2386 i, adapter->num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002387
2388 devRead->rssConfDesc.confVer = 1;
Andy Kingb0eb57c2013-08-23 09:33:49 -07002389 devRead->rssConfDesc.confLen = cpu_to_le32(sizeof(*rssConf));
2390 devRead->rssConfDesc.confPA =
2391 cpu_to_le64(adapter->rss_conf_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002392 }
2393
2394#endif /* VMXNET3_RSS */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002395
2396 /* intr settings */
2397 devRead->intrConf.autoMask = adapter->intr.mask_mode ==
2398 VMXNET3_IMM_AUTO;
2399 devRead->intrConf.numIntrs = adapter->intr.num_intrs;
2400 for (i = 0; i < adapter->intr.num_intrs; i++)
2401 devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
2402
2403 devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
Ronghua Zang6929fe82010-07-15 22:18:47 -07002404 devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002405
2406 /* rx filter settings */
2407 devRead->rxFilterConf.rxMode = 0;
2408 vmxnet3_restore_vlan(adapter);
Shreyas Bhatewaraf9f25022011-01-14 14:59:31 +00002409 vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
2410
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002411 /* the rest are already zeroed */
2412}
2413
2414
2415int
2416vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
2417{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002418 int err, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002419 u32 ret;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002420 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002421
Stephen Hemmingerfdcd79b2013-01-15 07:28:29 +00002422 netdev_dbg(adapter->netdev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002423 " ring sizes %u %u %u\n", adapter->netdev->name,
2424 adapter->skb_buf_size, adapter->rx_buf_per_pkt,
2425 adapter->tx_queue[0].tx_ring.size,
2426 adapter->rx_queue[0].rx_ring[0].size,
2427 adapter->rx_queue[0].rx_ring[1].size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002428
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002429 vmxnet3_tq_init_all(adapter);
2430 err = vmxnet3_rq_init_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002431 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002432 netdev_err(adapter->netdev,
2433 "Failed to init rx queue error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002434 goto rq_err;
2435 }
2436
2437 err = vmxnet3_request_irqs(adapter);
2438 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002439 netdev_err(adapter->netdev,
2440 "Failed to setup irq for error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002441 goto irq_err;
2442 }
2443
2444 vmxnet3_setup_driver_shared(adapter);
2445
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00002446 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
2447 adapter->shared_pa));
2448 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
2449 adapter->shared_pa));
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002450 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002451 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2452 VMXNET3_CMD_ACTIVATE_DEV);
2453 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002454 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002455
2456 if (ret != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002457 netdev_err(adapter->netdev,
2458 "Failed to activate dev: error %u\n", ret);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002459 err = -EINVAL;
2460 goto activate_err;
2461 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002462
2463 for (i = 0; i < adapter->num_rx_queues; i++) {
2464 VMXNET3_WRITE_BAR0_REG(adapter,
2465 VMXNET3_REG_RXPROD + i * VMXNET3_REG_ALIGN,
2466 adapter->rx_queue[i].rx_ring[0].next2fill);
2467 VMXNET3_WRITE_BAR0_REG(adapter, (VMXNET3_REG_RXPROD2 +
2468 (i * VMXNET3_REG_ALIGN)),
2469 adapter->rx_queue[i].rx_ring[1].next2fill);
2470 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002471
2472 /* Apply the rx filter settins last. */
2473 vmxnet3_set_mc(adapter->netdev);
2474
2475 /*
2476 * Check link state when first activating device. It will start the
2477 * tx queue if the link is up.
2478 */
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00002479 vmxnet3_check_link(adapter, true);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002480 for (i = 0; i < adapter->num_rx_queues; i++)
2481 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002482 vmxnet3_enable_all_intrs(adapter);
2483 clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
2484 return 0;
2485
2486activate_err:
2487 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
2488 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
2489 vmxnet3_free_irqs(adapter);
2490irq_err:
2491rq_err:
2492 /* free up buffers we allocated */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002493 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002494 return err;
2495}
2496
2497
2498void
2499vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
2500{
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002501 unsigned long flags;
2502 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002503 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002504 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002505}
2506
2507
2508int
2509vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
2510{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002511 int i;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002512 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002513 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
2514 return 0;
2515
2516
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002517 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002518 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2519 VMXNET3_CMD_QUIESCE_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00002520 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002521 vmxnet3_disable_all_intrs(adapter);
2522
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002523 for (i = 0; i < adapter->num_rx_queues; i++)
2524 napi_disable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002525 netif_tx_disable(adapter->netdev);
2526 adapter->link_speed = 0;
2527 netif_carrier_off(adapter->netdev);
2528
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002529 vmxnet3_tq_cleanup_all(adapter);
2530 vmxnet3_rq_cleanup_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002531 vmxnet3_free_irqs(adapter);
2532 return 0;
2533}
2534
2535
2536static void
2537vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2538{
2539 u32 tmp;
2540
2541 tmp = *(u32 *)mac;
2542 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
2543
2544 tmp = (mac[5] << 8) | mac[4];
2545 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
2546}
2547
2548
2549static int
2550vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
2551{
2552 struct sockaddr *addr = p;
2553 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2554
2555 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2556 vmxnet3_write_mac_addr(adapter, addr->sa_data);
2557
2558 return 0;
2559}
2560
2561
2562/* ==================== initialization and cleanup routines ============ */
2563
2564static int
2565vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
2566{
2567 int err;
2568 unsigned long mmio_start, mmio_len;
2569 struct pci_dev *pdev = adapter->pdev;
2570
2571 err = pci_enable_device(pdev);
2572 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002573 dev_err(&pdev->dev, "Failed to enable adapter: error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002574 return err;
2575 }
2576
2577 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
2578 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002579 dev_err(&pdev->dev,
2580 "pci_set_consistent_dma_mask failed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002581 err = -EIO;
2582 goto err_set_mask;
2583 }
2584 *dma64 = true;
2585 } else {
2586 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002587 dev_err(&pdev->dev,
2588 "pci_set_dma_mask failed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002589 err = -EIO;
2590 goto err_set_mask;
2591 }
2592 *dma64 = false;
2593 }
2594
2595 err = pci_request_selected_regions(pdev, (1 << 2) - 1,
2596 vmxnet3_driver_name);
2597 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002598 dev_err(&pdev->dev,
2599 "Failed to request region for adapter: error %d\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002600 goto err_set_mask;
2601 }
2602
2603 pci_set_master(pdev);
2604
2605 mmio_start = pci_resource_start(pdev, 0);
2606 mmio_len = pci_resource_len(pdev, 0);
2607 adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
2608 if (!adapter->hw_addr0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002609 dev_err(&pdev->dev, "Failed to map bar0\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002610 err = -EIO;
2611 goto err_ioremap;
2612 }
2613
2614 mmio_start = pci_resource_start(pdev, 1);
2615 mmio_len = pci_resource_len(pdev, 1);
2616 adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
2617 if (!adapter->hw_addr1) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002618 dev_err(&pdev->dev, "Failed to map bar1\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002619 err = -EIO;
2620 goto err_bar1;
2621 }
2622 return 0;
2623
2624err_bar1:
2625 iounmap(adapter->hw_addr0);
2626err_ioremap:
2627 pci_release_selected_regions(pdev, (1 << 2) - 1);
2628err_set_mask:
2629 pci_disable_device(pdev);
2630 return err;
2631}
2632
2633
2634static void
2635vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
2636{
2637 BUG_ON(!adapter->pdev);
2638
2639 iounmap(adapter->hw_addr0);
2640 iounmap(adapter->hw_addr1);
2641 pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
2642 pci_disable_device(adapter->pdev);
2643}
2644
2645
2646static void
2647vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
2648{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002649 size_t sz, i, ring0_size, ring1_size, comp_size;
2650 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[0];
2651
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002652
2653 if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
2654 VMXNET3_MAX_ETH_HDR_SIZE) {
2655 adapter->skb_buf_size = adapter->netdev->mtu +
2656 VMXNET3_MAX_ETH_HDR_SIZE;
2657 if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
2658 adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
2659
2660 adapter->rx_buf_per_pkt = 1;
2661 } else {
2662 adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
2663 sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
2664 VMXNET3_MAX_ETH_HDR_SIZE;
2665 adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
2666 }
2667
2668 /*
2669 * for simplicity, force the ring0 size to be a multiple of
2670 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
2671 */
2672 sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002673 ring0_size = adapter->rx_queue[0].rx_ring[0].size;
2674 ring0_size = (ring0_size + sz - 1) / sz * sz;
Shreyas Bhatewaraa53255d2011-01-14 14:59:25 +00002675 ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002676 sz * sz);
2677 ring1_size = adapter->rx_queue[0].rx_ring[1].size;
Shrikrishna Khare53831aa2015-01-06 09:20:15 -08002678 ring1_size = (ring1_size + sz - 1) / sz * sz;
2679 ring1_size = min_t(u32, ring1_size, VMXNET3_RX_RING2_MAX_SIZE /
2680 sz * sz);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002681 comp_size = ring0_size + ring1_size;
2682
2683 for (i = 0; i < adapter->num_rx_queues; i++) {
2684 rq = &adapter->rx_queue[i];
2685 rq->rx_ring[0].size = ring0_size;
2686 rq->rx_ring[1].size = ring1_size;
2687 rq->comp_ring.size = comp_size;
2688 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002689}
2690
2691
2692int
2693vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -07002694 u32 rx_ring_size, u32 rx_ring2_size,
2695 u16 txdata_desc_size)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002696{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002697 int err = 0, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002698
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002699 for (i = 0; i < adapter->num_tx_queues; i++) {
2700 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2701 tq->tx_ring.size = tx_ring_size;
2702 tq->data_ring.size = tx_ring_size;
2703 tq->comp_ring.size = tx_ring_size;
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -07002704 tq->txdata_desc_size = txdata_desc_size;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002705 tq->shared = &adapter->tqd_start[i].ctrl;
2706 tq->stopped = true;
2707 tq->adapter = adapter;
2708 tq->qid = i;
2709 err = vmxnet3_tq_create(tq, adapter);
2710 /*
2711 * Too late to change num_tx_queues. We cannot do away with
2712 * lesser number of queues than what we asked for
2713 */
2714 if (err)
2715 goto queue_err;
2716 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002717
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002718 adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
2719 adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002720 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002721 for (i = 0; i < adapter->num_rx_queues; i++) {
2722 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2723 /* qid and qid2 for rx queues will be assigned later when num
2724 * of rx queues is finalized after allocating intrs */
2725 rq->shared = &adapter->rqd_start[i].ctrl;
2726 rq->adapter = adapter;
2727 err = vmxnet3_rq_create(rq, adapter);
2728 if (err) {
2729 if (i == 0) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002730 netdev_err(adapter->netdev,
2731 "Could not allocate any rx queues. "
2732 "Aborting.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002733 goto queue_err;
2734 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002735 netdev_info(adapter->netdev,
2736 "Number of rx queues changed "
2737 "to : %d.\n", i);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002738 adapter->num_rx_queues = i;
2739 err = 0;
2740 break;
2741 }
2742 }
2743 }
2744 return err;
2745queue_err:
2746 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002747 return err;
2748}
2749
2750static int
2751vmxnet3_open(struct net_device *netdev)
2752{
2753 struct vmxnet3_adapter *adapter;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002754 int err, i;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002755
2756 adapter = netdev_priv(netdev);
2757
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002758 for (i = 0; i < adapter->num_tx_queues; i++)
2759 spin_lock_init(&adapter->tx_queue[i].tx_lock);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002760
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -07002761 if (VMXNET3_VERSION_GE_3(adapter)) {
2762 unsigned long flags;
2763 u16 txdata_desc_size;
2764
2765 spin_lock_irqsave(&adapter->cmd_lock, flags);
2766 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2767 VMXNET3_CMD_GET_TXDATA_DESC_SIZE);
2768 txdata_desc_size = VMXNET3_READ_BAR1_REG(adapter,
2769 VMXNET3_REG_CMD);
2770 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2771
2772 if ((txdata_desc_size < VMXNET3_TXDATA_DESC_MIN_SIZE) ||
2773 (txdata_desc_size > VMXNET3_TXDATA_DESC_MAX_SIZE) ||
2774 (txdata_desc_size & VMXNET3_TXDATA_DESC_SIZE_MASK)) {
2775 adapter->txdata_desc_size =
2776 sizeof(struct Vmxnet3_TxDataDesc);
2777 } else {
2778 adapter->txdata_desc_size = txdata_desc_size;
2779 }
2780 } else {
2781 adapter->txdata_desc_size = sizeof(struct Vmxnet3_TxDataDesc);
2782 }
2783
2784 err = vmxnet3_create_queues(adapter,
2785 adapter->tx_ring_size,
Neil Hormanf00e2b02014-06-13 10:03:21 -04002786 adapter->rx_ring_size,
Shrikrishna Khare3c8b3ef2016-06-16 10:51:55 -07002787 adapter->rx_ring2_size,
2788 adapter->txdata_desc_size);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002789 if (err)
2790 goto queue_err;
2791
2792 err = vmxnet3_activate_dev(adapter);
2793 if (err)
2794 goto activate_err;
2795
2796 return 0;
2797
2798activate_err:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002799 vmxnet3_rq_destroy_all(adapter);
2800 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002801queue_err:
2802 return err;
2803}
2804
2805
2806static int
2807vmxnet3_close(struct net_device *netdev)
2808{
2809 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2810
2811 /*
2812 * Reset_work may be in the middle of resetting the device, wait for its
2813 * completion.
2814 */
2815 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2816 msleep(1);
2817
2818 vmxnet3_quiesce_dev(adapter);
2819
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002820 vmxnet3_rq_destroy_all(adapter);
2821 vmxnet3_tq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002822
2823 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2824
2825
2826 return 0;
2827}
2828
2829
2830void
2831vmxnet3_force_close(struct vmxnet3_adapter *adapter)
2832{
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002833 int i;
2834
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002835 /*
2836 * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
2837 * vmxnet3_close() will deadlock.
2838 */
2839 BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
2840
2841 /* we need to enable NAPI, otherwise dev_close will deadlock */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002842 for (i = 0; i < adapter->num_rx_queues; i++)
2843 napi_enable(&adapter->rx_queue[i].napi);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002844 dev_close(adapter->netdev);
2845}
2846
2847
2848static int
2849vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
2850{
2851 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2852 int err = 0;
2853
2854 if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
2855 return -EINVAL;
2856
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002857 netdev->mtu = new_mtu;
2858
2859 /*
2860 * Reset_work may be in the middle of resetting the device, wait for its
2861 * completion.
2862 */
2863 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
2864 msleep(1);
2865
2866 if (netif_running(netdev)) {
2867 vmxnet3_quiesce_dev(adapter);
2868 vmxnet3_reset_dev(adapter);
2869
2870 /* we need to re-create the rx queue based on the new mtu */
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002871 vmxnet3_rq_destroy_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002872 vmxnet3_adjust_rx_ring_size(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002873 err = vmxnet3_rq_create_all(adapter);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002874 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002875 netdev_err(netdev,
2876 "failed to re-create rx queues, "
2877 " error %d. Closing it.\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002878 goto out;
2879 }
2880
2881 err = vmxnet3_activate_dev(adapter);
2882 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00002883 netdev_err(netdev,
2884 "failed to re-activate, error %d. "
2885 "Closing it\n", err);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002886 goto out;
2887 }
2888 }
2889
2890out:
2891 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
2892 if (err)
2893 vmxnet3_force_close(adapter);
2894
2895 return err;
2896}
2897
2898
2899static void
2900vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
2901{
2902 struct net_device *netdev = adapter->netdev;
2903
Michał Mirosława0d27302011-04-18 13:31:21 +00002904 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00002905 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_CTAG_TX |
2906 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_TSO | NETIF_F_TSO6 |
Jesse Gross72e85c42011-06-23 13:04:39 +00002907 NETIF_F_LRO;
Michał Mirosława0d27302011-04-18 13:31:21 +00002908 if (dma64)
Shreyas Bhatewaraebbf9292011-07-20 17:21:51 +00002909 netdev->hw_features |= NETIF_F_HIGHDMA;
Jesse Gross72e85c42011-06-23 13:04:39 +00002910 netdev->vlan_features = netdev->hw_features &
Patrick McHardyf6469682013-04-19 02:04:27 +00002911 ~(NETIF_F_HW_VLAN_CTAG_TX |
2912 NETIF_F_HW_VLAN_CTAG_RX);
2913 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002914}
2915
2916
2917static void
2918vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
2919{
2920 u32 tmp;
2921
2922 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
2923 *(u32 *)mac = tmp;
2924
2925 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
2926 mac[4] = tmp & 0xff;
2927 mac[5] = (tmp >> 8) & 0xff;
2928}
2929
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002930#ifdef CONFIG_PCI_MSI
2931
2932/*
2933 * Enable MSIx vectors.
2934 * Returns :
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002935 * VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002936 * were enabled.
2937 * number of vectors which were enabled otherwise (this number is greater
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002938 * than VMXNET3_LINUX_MIN_MSIX_VECT)
2939 */
2940
2941static int
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002942vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter, int nvec)
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002943{
Alexander Gordeevc0a1be32014-02-18 11:12:03 +01002944 int ret = pci_enable_msix_range(adapter->pdev,
2945 adapter->intr.msix_entries, nvec, nvec);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002946
Alexander Gordeevc0a1be32014-02-18 11:12:03 +01002947 if (ret == -ENOSPC && nvec > VMXNET3_LINUX_MIN_MSIX_VECT) {
2948 dev_err(&adapter->netdev->dev,
2949 "Failed to enable %d MSI-X, trying %d\n",
2950 nvec, VMXNET3_LINUX_MIN_MSIX_VECT);
2951
2952 ret = pci_enable_msix_range(adapter->pdev,
2953 adapter->intr.msix_entries,
2954 VMXNET3_LINUX_MIN_MSIX_VECT,
2955 VMXNET3_LINUX_MIN_MSIX_VECT);
2956 }
2957
2958 if (ret < 0) {
2959 dev_err(&adapter->netdev->dev,
2960 "Failed to enable MSI-X, error: %d\n", ret);
2961 }
2962
2963 return ret;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002964}
2965
2966
2967#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002968
2969static void
2970vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
2971{
2972 u32 cfg;
Roland Dreiere328d412011-05-06 08:32:53 +00002973 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002974
2975 /* intr settings */
Roland Dreiere328d412011-05-06 08:32:53 +00002976 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002977 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2978 VMXNET3_CMD_GET_CONF_INTR);
2979 cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
Roland Dreiere328d412011-05-06 08:32:53 +00002980 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002981 adapter->intr.type = cfg & 0x3;
2982 adapter->intr.mask_mode = (cfg >> 2) & 0x3;
2983
2984 if (adapter->intr.type == VMXNET3_IT_AUTO) {
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002985 adapter->intr.type = VMXNET3_IT_MSIX;
2986 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07002987
Randy Dunlap8f7e5242009-10-14 20:38:58 -07002988#ifdef CONFIG_PCI_MSI
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002989 if (adapter->intr.type == VMXNET3_IT_MSIX) {
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002990 int i, nvec;
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00002991
Alexander Gordeevb60b8692014-02-18 11:12:02 +01002992 nvec = adapter->share_intr == VMXNET3_INTR_TXSHARE ?
2993 1 : adapter->num_tx_queues;
2994 nvec += adapter->share_intr == VMXNET3_INTR_BUDDYSHARE ?
2995 0 : adapter->num_rx_queues;
2996 nvec += 1; /* for link event */
2997 nvec = nvec > VMXNET3_LINUX_MIN_MSIX_VECT ?
2998 nvec : VMXNET3_LINUX_MIN_MSIX_VECT;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00002999
Alexander Gordeevb60b8692014-02-18 11:12:02 +01003000 for (i = 0; i < nvec; i++)
3001 adapter->intr.msix_entries[i].entry = i;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003002
Alexander Gordeevb60b8692014-02-18 11:12:02 +01003003 nvec = vmxnet3_acquire_msix_vectors(adapter, nvec);
3004 if (nvec < 0)
3005 goto msix_err;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003006
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003007 /* If we cannot allocate one MSIx vector per queue
3008 * then limit the number of rx queues to 1
3009 */
Alexander Gordeevb60b8692014-02-18 11:12:02 +01003010 if (nvec == VMXNET3_LINUX_MIN_MSIX_VECT) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003011 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
Shreyas Bhatewara7e96fbf2011-01-14 15:00:03 +00003012 || adapter->num_rx_queues != 1) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003013 adapter->share_intr = VMXNET3_INTR_TXSHARE;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003014 netdev_err(adapter->netdev,
3015 "Number of rx queues : 1\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003016 adapter->num_rx_queues = 1;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003017 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003018 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003019
Alexander Gordeevb60b8692014-02-18 11:12:02 +01003020 adapter->intr.num_intrs = nvec;
3021 return;
3022
3023msix_err:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003024 /* If we cannot allocate MSIx vectors use only one rx queue */
Stephen Hemminger4bad25f2013-01-15 07:28:28 +00003025 dev_info(&adapter->pdev->dev,
3026 "Failed to enable MSI-X, error %d. "
Alexander Gordeevb60b8692014-02-18 11:12:02 +01003027 "Limiting #rx queues to 1, try MSI.\n", nvec);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003028
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00003029 adapter->intr.type = VMXNET3_IT_MSI;
3030 }
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003031
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00003032 if (adapter->intr.type == VMXNET3_IT_MSI) {
Alexander Gordeevb60b8692014-02-18 11:12:02 +01003033 if (!pci_enable_msi(adapter->pdev)) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003034 adapter->num_rx_queues = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003035 adapter->intr.num_intrs = 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003036 return;
3037 }
3038 }
Shreyas Bhatewara0bdc0d72010-07-15 15:21:27 +00003039#endif /* CONFIG_PCI_MSI */
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003040
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003041 adapter->num_rx_queues = 1;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003042 dev_info(&adapter->netdev->dev,
3043 "Using INTx interrupt, #Rx queues: 1.\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003044 adapter->intr.type = VMXNET3_IT_INTX;
3045
3046 /* INT-X related setting */
3047 adapter->intr.num_intrs = 1;
3048}
3049
3050
3051static void
3052vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
3053{
3054 if (adapter->intr.type == VMXNET3_IT_MSIX)
3055 pci_disable_msix(adapter->pdev);
3056 else if (adapter->intr.type == VMXNET3_IT_MSI)
3057 pci_disable_msi(adapter->pdev);
3058 else
3059 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
3060}
3061
3062
3063static void
3064vmxnet3_tx_timeout(struct net_device *netdev)
3065{
3066 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3067 adapter->tx_timeout_count++;
3068
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003069 netdev_err(adapter->netdev, "tx hang\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003070 schedule_work(&adapter->work);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003071 netif_wake_queue(adapter->netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003072}
3073
3074
3075static void
3076vmxnet3_reset_work(struct work_struct *data)
3077{
3078 struct vmxnet3_adapter *adapter;
3079
3080 adapter = container_of(data, struct vmxnet3_adapter, work);
3081
3082 /* if another thread is resetting the device, no need to proceed */
3083 if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
3084 return;
3085
3086 /* if the device is closed, we must leave it alone */
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00003087 rtnl_lock();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003088 if (netif_running(adapter->netdev)) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003089 netdev_notice(adapter->netdev, "resetting\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003090 vmxnet3_quiesce_dev(adapter);
3091 vmxnet3_reset_dev(adapter);
3092 vmxnet3_activate_dev(adapter);
3093 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003094 netdev_info(adapter->netdev, "already closed\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003095 }
Shreyas Bhatewarad9a5f212010-07-19 07:02:13 +00003096 rtnl_unlock();
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003097
3098 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
3099}
3100
3101
Bill Pemberton3a4751a2012-12-03 09:24:16 -05003102static int
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003103vmxnet3_probe_device(struct pci_dev *pdev,
3104 const struct pci_device_id *id)
3105{
3106 static const struct net_device_ops vmxnet3_netdev_ops = {
3107 .ndo_open = vmxnet3_open,
3108 .ndo_stop = vmxnet3_close,
3109 .ndo_start_xmit = vmxnet3_xmit_frame,
3110 .ndo_set_mac_address = vmxnet3_set_mac_addr,
3111 .ndo_change_mtu = vmxnet3_change_mtu,
Michał Mirosława0d27302011-04-18 13:31:21 +00003112 .ndo_set_features = vmxnet3_set_features,
stephen hemminger95305f62011-06-08 14:53:57 +00003113 .ndo_get_stats64 = vmxnet3_get_stats64,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003114 .ndo_tx_timeout = vmxnet3_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003115 .ndo_set_rx_mode = vmxnet3_set_mc,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003116 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
3117 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
3118#ifdef CONFIG_NET_POLL_CONTROLLER
3119 .ndo_poll_controller = vmxnet3_netpoll,
3120#endif
3121 };
3122 int err;
3123 bool dma64 = false; /* stupid gcc */
3124 u32 ver;
3125 struct net_device *netdev;
3126 struct vmxnet3_adapter *adapter;
3127 u8 mac[ETH_ALEN];
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003128 int size;
3129 int num_tx_queues;
3130 int num_rx_queues;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003131
Shreyas Bhatewarae154b632011-05-10 06:13:56 +00003132 if (!pci_msi_enabled())
3133 enable_mq = 0;
3134
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003135#ifdef VMXNET3_RSS
3136 if (enable_mq)
3137 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3138 (int)num_online_cpus());
3139 else
3140#endif
3141 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07003142 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003143
3144 if (enable_mq)
3145 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
3146 (int)num_online_cpus());
3147 else
3148 num_tx_queues = 1;
3149
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07003150 num_tx_queues = rounddown_pow_of_two(num_tx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003151 netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
3152 max(num_tx_queues, num_rx_queues));
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003153 dev_info(&pdev->dev,
3154 "# of Tx queues : %d, # of Rx queues : %d\n",
3155 num_tx_queues, num_rx_queues);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003156
Joe Perches41de8d42012-01-29 13:47:52 +00003157 if (!netdev)
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003158 return -ENOMEM;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003159
3160 pci_set_drvdata(pdev, netdev);
3161 adapter = netdev_priv(netdev);
3162 adapter->netdev = netdev;
3163 adapter->pdev = pdev;
3164
Neil Hormanf00e2b02014-06-13 10:03:21 -04003165 adapter->tx_ring_size = VMXNET3_DEF_TX_RING_SIZE;
3166 adapter->rx_ring_size = VMXNET3_DEF_RX_RING_SIZE;
Shrikrishna Khare53831aa2015-01-06 09:20:15 -08003167 adapter->rx_ring2_size = VMXNET3_DEF_RX_RING2_SIZE;
Neil Hormanf00e2b02014-06-13 10:03:21 -04003168
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003169 spin_lock_init(&adapter->cmd_lock);
Andy Kingb0eb57c2013-08-23 09:33:49 -07003170 adapter->adapter_pa = dma_map_single(&adapter->pdev->dev, adapter,
3171 sizeof(struct vmxnet3_adapter),
3172 PCI_DMA_TODEVICE);
Alexey Khoroshilov5738a092015-11-28 01:29:30 +03003173 if (dma_mapping_error(&adapter->pdev->dev, adapter->adapter_pa)) {
3174 dev_err(&pdev->dev, "Failed to map dma\n");
3175 err = -EFAULT;
3176 goto err_dma_map;
3177 }
Andy Kingb0eb57c2013-08-23 09:33:49 -07003178 adapter->shared = dma_alloc_coherent(
3179 &adapter->pdev->dev,
3180 sizeof(struct Vmxnet3_DriverShared),
3181 &adapter->shared_pa, GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003182 if (!adapter->shared) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003183 dev_err(&pdev->dev, "Failed to allocate memory\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003184 err = -ENOMEM;
3185 goto err_alloc_shared;
3186 }
3187
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003188 adapter->num_rx_queues = num_rx_queues;
3189 adapter->num_tx_queues = num_tx_queues;
Bhavesh Davdae4fabf22013-03-06 12:04:53 +00003190 adapter->rx_buf_per_pkt = 1;
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003191
3192 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3193 size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
Andy Kingb0eb57c2013-08-23 09:33:49 -07003194 adapter->tqd_start = dma_alloc_coherent(&adapter->pdev->dev, size,
3195 &adapter->queue_desc_pa,
3196 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003197
3198 if (!adapter->tqd_start) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003199 dev_err(&pdev->dev, "Failed to allocate memory\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003200 err = -ENOMEM;
3201 goto err_alloc_queue_desc;
3202 }
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003203 adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
stephen hemminger96800ee2012-11-13 13:53:28 +00003204 adapter->num_tx_queues);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003205
Andy Kingb0eb57c2013-08-23 09:33:49 -07003206 adapter->pm_conf = dma_alloc_coherent(&adapter->pdev->dev,
3207 sizeof(struct Vmxnet3_PMConf),
3208 &adapter->pm_conf_pa,
3209 GFP_KERNEL);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003210 if (adapter->pm_conf == NULL) {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003211 err = -ENOMEM;
3212 goto err_alloc_pm;
3213 }
3214
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003215#ifdef VMXNET3_RSS
3216
Andy Kingb0eb57c2013-08-23 09:33:49 -07003217 adapter->rss_conf = dma_alloc_coherent(&adapter->pdev->dev,
3218 sizeof(struct UPT1_RSSConf),
3219 &adapter->rss_conf_pa,
3220 GFP_KERNEL);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003221 if (adapter->rss_conf == NULL) {
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003222 err = -ENOMEM;
3223 goto err_alloc_rss;
3224 }
3225#endif /* VMXNET3_RSS */
3226
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003227 err = vmxnet3_alloc_pci_resources(adapter, &dma64);
3228 if (err < 0)
3229 goto err_alloc_pci;
3230
3231 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
Shrikrishna Khare190af102016-06-16 10:51:53 -07003232 if (ver & (1 << VMXNET3_REV_2)) {
3233 VMXNET3_WRITE_BAR1_REG(adapter,
3234 VMXNET3_REG_VRRS,
3235 1 << VMXNET3_REV_2);
3236 adapter->version = VMXNET3_REV_2 + 1;
3237 } else if (ver & (1 << VMXNET3_REV_1)) {
3238 VMXNET3_WRITE_BAR1_REG(adapter,
3239 VMXNET3_REG_VRRS,
3240 1 << VMXNET3_REV_1);
3241 adapter->version = VMXNET3_REV_1 + 1;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003242 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003243 dev_err(&pdev->dev,
3244 "Incompatible h/w version (0x%x) for adapter\n", ver);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003245 err = -EBUSY;
3246 goto err_ver;
3247 }
Shreyas Bhatewara45dac1d2015-06-19 13:38:29 -07003248 dev_dbg(&pdev->dev, "Using device version %d\n", adapter->version);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003249
3250 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
3251 if (ver & 1) {
3252 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
3253 } else {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003254 dev_err(&pdev->dev,
3255 "Incompatible upt version (0x%x) for adapter\n", ver);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003256 err = -EBUSY;
3257 goto err_ver;
3258 }
3259
Shreyas Bhatewarae101e7d2011-07-20 16:01:11 +00003260 SET_NETDEV_DEV(netdev, &pdev->dev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003261 vmxnet3_declare_features(adapter, dma64);
3262
Stephen Hemminger4db37a72013-01-15 07:28:33 +00003263 if (adapter->num_tx_queues == adapter->num_rx_queues)
3264 adapter->share_intr = VMXNET3_INTR_BUDDYSHARE;
3265 else
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003266 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
3267
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003268 vmxnet3_alloc_intr_resources(adapter);
3269
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003270#ifdef VMXNET3_RSS
3271 if (adapter->num_rx_queues > 1 &&
3272 adapter->intr.type == VMXNET3_IT_MSIX) {
3273 adapter->rss = true;
Stephen Hemminger7db11f72013-01-15 07:28:35 +00003274 netdev->hw_features |= NETIF_F_RXHASH;
3275 netdev->features |= NETIF_F_RXHASH;
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003276 dev_dbg(&pdev->dev, "RSS is enabled.\n");
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003277 } else {
3278 adapter->rss = false;
3279 }
3280#endif
3281
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003282 vmxnet3_read_mac_addr(adapter, mac);
3283 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3284
3285 netdev->netdev_ops = &vmxnet3_netdev_ops;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003286 vmxnet3_set_ethtool_ops(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003287 netdev->watchdog_timeo = 5 * HZ;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003288
3289 INIT_WORK(&adapter->work, vmxnet3_reset_work);
Steve Hodgsone3bc4ff2012-08-14 17:13:36 +01003290 set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003291
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003292 if (adapter->intr.type == VMXNET3_IT_MSIX) {
3293 int i;
3294 for (i = 0; i < adapter->num_rx_queues; i++) {
3295 netif_napi_add(adapter->netdev,
3296 &adapter->rx_queue[i].napi,
3297 vmxnet3_poll_rx_only, 64);
3298 }
3299 } else {
3300 netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
3301 vmxnet3_poll, 64);
3302 }
3303
3304 netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
3305 netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
3306
Neil Horman6cdd20c2013-01-29 16:15:45 -05003307 netif_carrier_off(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003308 err = register_netdev(netdev);
3309
3310 if (err) {
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003311 dev_err(&pdev->dev, "Failed to register adapter\n");
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003312 goto err_register;
3313 }
3314
Shreyas Bhatewara4a1745fc2010-07-15 21:51:14 +00003315 vmxnet3_check_link(adapter, false);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003316 return 0;
3317
3318err_register:
3319 vmxnet3_free_intr_resources(adapter);
3320err_ver:
3321 vmxnet3_free_pci_resources(adapter);
3322err_alloc_pci:
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003323#ifdef VMXNET3_RSS
Andy Kingb0eb57c2013-08-23 09:33:49 -07003324 dma_free_coherent(&adapter->pdev->dev, sizeof(struct UPT1_RSSConf),
3325 adapter->rss_conf, adapter->rss_conf_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003326err_alloc_rss:
3327#endif
Andy Kingb0eb57c2013-08-23 09:33:49 -07003328 dma_free_coherent(&adapter->pdev->dev, sizeof(struct Vmxnet3_PMConf),
3329 adapter->pm_conf, adapter->pm_conf_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003330err_alloc_pm:
Andy Kingb0eb57c2013-08-23 09:33:49 -07003331 dma_free_coherent(&adapter->pdev->dev, size, adapter->tqd_start,
3332 adapter->queue_desc_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003333err_alloc_queue_desc:
Andy Kingb0eb57c2013-08-23 09:33:49 -07003334 dma_free_coherent(&adapter->pdev->dev,
3335 sizeof(struct Vmxnet3_DriverShared),
3336 adapter->shared, adapter->shared_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003337err_alloc_shared:
Andy Kingb0eb57c2013-08-23 09:33:49 -07003338 dma_unmap_single(&adapter->pdev->dev, adapter->adapter_pa,
3339 sizeof(struct vmxnet3_adapter), PCI_DMA_TODEVICE);
Alexey Khoroshilov5738a092015-11-28 01:29:30 +03003340err_dma_map:
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003341 free_netdev(netdev);
3342 return err;
3343}
3344
3345
Bill Pemberton3a4751a2012-12-03 09:24:16 -05003346static void
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003347vmxnet3_remove_device(struct pci_dev *pdev)
3348{
3349 struct net_device *netdev = pci_get_drvdata(pdev);
3350 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003351 int size = 0;
3352 int num_rx_queues;
3353
3354#ifdef VMXNET3_RSS
3355 if (enable_mq)
3356 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3357 (int)num_online_cpus());
3358 else
3359#endif
3360 num_rx_queues = 1;
Shreyas Bhatewaraeebb02b2011-07-07 00:25:52 -07003361 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003362
Tejun Heo23f333a2010-12-12 16:45:14 +01003363 cancel_work_sync(&adapter->work);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003364
3365 unregister_netdev(netdev);
3366
3367 vmxnet3_free_intr_resources(adapter);
3368 vmxnet3_free_pci_resources(adapter);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003369#ifdef VMXNET3_RSS
Andy Kingb0eb57c2013-08-23 09:33:49 -07003370 dma_free_coherent(&adapter->pdev->dev, sizeof(struct UPT1_RSSConf),
3371 adapter->rss_conf, adapter->rss_conf_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003372#endif
Andy Kingb0eb57c2013-08-23 09:33:49 -07003373 dma_free_coherent(&adapter->pdev->dev, sizeof(struct Vmxnet3_PMConf),
3374 adapter->pm_conf, adapter->pm_conf_pa);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003375
3376 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
3377 size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
Andy Kingb0eb57c2013-08-23 09:33:49 -07003378 dma_free_coherent(&adapter->pdev->dev, size, adapter->tqd_start,
3379 adapter->queue_desc_pa);
3380 dma_free_coherent(&adapter->pdev->dev,
3381 sizeof(struct Vmxnet3_DriverShared),
3382 adapter->shared, adapter->shared_pa);
3383 dma_unmap_single(&adapter->pdev->dev, adapter->adapter_pa,
3384 sizeof(struct vmxnet3_adapter), PCI_DMA_TODEVICE);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003385 free_netdev(netdev);
3386}
3387
Shreyas Bhatewarae9ba47b2015-06-19 13:36:02 -07003388static void vmxnet3_shutdown_device(struct pci_dev *pdev)
3389{
3390 struct net_device *netdev = pci_get_drvdata(pdev);
3391 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3392 unsigned long flags;
3393
3394 /* Reset_work may be in the middle of resetting the device, wait for its
3395 * completion.
3396 */
3397 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
3398 msleep(1);
3399
3400 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED,
3401 &adapter->state)) {
3402 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
3403 return;
3404 }
3405 spin_lock_irqsave(&adapter->cmd_lock, flags);
3406 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3407 VMXNET3_CMD_QUIESCE_DEV);
3408 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3409 vmxnet3_disable_all_intrs(adapter);
3410
3411 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
3412}
3413
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003414
3415#ifdef CONFIG_PM
3416
3417static int
3418vmxnet3_suspend(struct device *device)
3419{
3420 struct pci_dev *pdev = to_pci_dev(device);
3421 struct net_device *netdev = pci_get_drvdata(pdev);
3422 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3423 struct Vmxnet3_PMConf *pmConf;
3424 struct ethhdr *ehdr;
3425 struct arphdr *ahdr;
3426 u8 *arpreq;
3427 struct in_device *in_dev;
3428 struct in_ifaddr *ifa;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003429 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003430 int i = 0;
3431
3432 if (!netif_running(netdev))
3433 return 0;
3434
Shreyas Bhatewara51956cd2011-01-14 14:59:52 +00003435 for (i = 0; i < adapter->num_rx_queues; i++)
3436 napi_disable(&adapter->rx_queue[i].napi);
3437
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003438 vmxnet3_disable_all_intrs(adapter);
3439 vmxnet3_free_irqs(adapter);
3440 vmxnet3_free_intr_resources(adapter);
3441
3442 netif_device_detach(netdev);
Shreyas Bhatewara09c50882010-11-19 10:55:24 +00003443 netif_tx_stop_all_queues(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003444
3445 /* Create wake-up filters. */
3446 pmConf = adapter->pm_conf;
3447 memset(pmConf, 0, sizeof(*pmConf));
3448
3449 if (adapter->wol & WAKE_UCAST) {
3450 pmConf->filters[i].patternSize = ETH_ALEN;
3451 pmConf->filters[i].maskSize = 1;
3452 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
3453 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
3454
Harvey Harrison3843e512010-10-21 18:05:32 +00003455 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003456 i++;
3457 }
3458
3459 if (adapter->wol & WAKE_ARP) {
3460 in_dev = in_dev_get(netdev);
3461 if (!in_dev)
3462 goto skip_arp;
3463
3464 ifa = (struct in_ifaddr *)in_dev->ifa_list;
3465 if (!ifa)
3466 goto skip_arp;
3467
3468 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
3469 sizeof(struct arphdr) + /* ARP header */
3470 2 * ETH_ALEN + /* 2 Ethernet addresses*/
3471 2 * sizeof(u32); /*2 IPv4 addresses */
3472 pmConf->filters[i].maskSize =
3473 (pmConf->filters[i].patternSize - 1) / 8 + 1;
3474
3475 /* ETH_P_ARP in Ethernet header. */
3476 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
3477 ehdr->h_proto = htons(ETH_P_ARP);
3478
3479 /* ARPOP_REQUEST in ARP header. */
3480 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
3481 ahdr->ar_op = htons(ARPOP_REQUEST);
3482 arpreq = (u8 *)(ahdr + 1);
3483
3484 /* The Unicast IPv4 address in 'tip' field. */
3485 arpreq += 2 * ETH_ALEN + sizeof(u32);
3486 *(u32 *)arpreq = ifa->ifa_address;
3487
3488 /* The mask for the relevant bits. */
3489 pmConf->filters[i].mask[0] = 0x00;
3490 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
3491 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
3492 pmConf->filters[i].mask[3] = 0x00;
3493 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
3494 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
3495 in_dev_put(in_dev);
3496
Harvey Harrison3843e512010-10-21 18:05:32 +00003497 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003498 i++;
3499 }
3500
3501skip_arp:
3502 if (adapter->wol & WAKE_MAGIC)
Harvey Harrison3843e512010-10-21 18:05:32 +00003503 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003504
3505 pmConf->numFilters = i;
3506
Shreyas Bhatewara115924b2009-11-16 13:41:33 +00003507 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
3508 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
3509 *pmConf));
Andy Kingb0eb57c2013-08-23 09:33:49 -07003510 adapter->shared->devRead.pmConfDesc.confPA =
3511 cpu_to_le64(adapter->pm_conf_pa);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003512
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003513 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003514 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3515 VMXNET3_CMD_UPDATE_PMCFG);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003516 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003517
3518 pci_save_state(pdev);
3519 pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
3520 adapter->wol);
3521 pci_disable_device(pdev);
3522 pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
3523
3524 return 0;
3525}
3526
3527
3528static int
3529vmxnet3_resume(struct device *device)
3530{
Shrikrishna Khare5ec82c12015-01-09 15:19:14 -08003531 int err;
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003532 unsigned long flags;
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003533 struct pci_dev *pdev = to_pci_dev(device);
3534 struct net_device *netdev = pci_get_drvdata(pdev);
3535 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003536
3537 if (!netif_running(netdev))
3538 return 0;
3539
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003540 pci_set_power_state(pdev, PCI_D0);
3541 pci_restore_state(pdev);
3542 err = pci_enable_device_mem(pdev);
3543 if (err != 0)
3544 return err;
3545
3546 pci_enable_wake(pdev, PCI_D0, 0);
3547
Shrikrishna Khare5ec82c12015-01-09 15:19:14 -08003548 vmxnet3_alloc_intr_resources(adapter);
3549
3550 /* During hibernate and suspend, device has to be reinitialized as the
3551 * device state need not be preserved.
3552 */
3553
3554 /* Need not check adapter state as other reset tasks cannot run during
3555 * device resume.
3556 */
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003557 spin_lock_irqsave(&adapter->cmd_lock, flags);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003558 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
Shrikrishna Khare5ec82c12015-01-09 15:19:14 -08003559 VMXNET3_CMD_QUIESCE_DEV);
Shreyas Bhatewara83d0fef2011-01-14 14:59:57 +00003560 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
Shrikrishna Khare5ec82c12015-01-09 15:19:14 -08003561 vmxnet3_tq_cleanup_all(adapter);
3562 vmxnet3_rq_cleanup_all(adapter);
3563
3564 vmxnet3_reset_dev(adapter);
3565 err = vmxnet3_activate_dev(adapter);
3566 if (err != 0) {
3567 netdev_err(netdev,
3568 "failed to re-activate on resume, error: %d", err);
3569 vmxnet3_force_close(adapter);
3570 return err;
3571 }
3572 netif_device_attach(netdev);
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003573
3574 return 0;
3575}
3576
Alexey Dobriyan47145212009-12-14 18:00:08 -08003577static const struct dev_pm_ops vmxnet3_pm_ops = {
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003578 .suspend = vmxnet3_suspend,
3579 .resume = vmxnet3_resume,
Shrikrishna Khare5ec82c12015-01-09 15:19:14 -08003580 .freeze = vmxnet3_suspend,
3581 .restore = vmxnet3_resume,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003582};
3583#endif
3584
3585static struct pci_driver vmxnet3_driver = {
3586 .name = vmxnet3_driver_name,
3587 .id_table = vmxnet3_pciid_table,
3588 .probe = vmxnet3_probe_device,
Bill Pemberton3a4751a2012-12-03 09:24:16 -05003589 .remove = vmxnet3_remove_device,
Shreyas Bhatewarae9ba47b2015-06-19 13:36:02 -07003590 .shutdown = vmxnet3_shutdown_device,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003591#ifdef CONFIG_PM
3592 .driver.pm = &vmxnet3_pm_ops,
3593#endif
3594};
3595
3596
3597static int __init
3598vmxnet3_init_module(void)
3599{
Stephen Hemminger204a6e62013-01-15 07:28:30 +00003600 pr_info("%s - version %s\n", VMXNET3_DRIVER_DESC,
Shreyas Bhatewarad1a890fa2009-10-13 00:15:51 -07003601 VMXNET3_DRIVER_VERSION_REPORT);
3602 return pci_register_driver(&vmxnet3_driver);
3603}
3604
3605module_init(vmxnet3_init_module);
3606
3607
3608static void
3609vmxnet3_exit_module(void)
3610{
3611 pci_unregister_driver(&vmxnet3_driver);
3612}
3613
3614module_exit(vmxnet3_exit_module);
3615
3616MODULE_AUTHOR("VMware, Inc.");
3617MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
3618MODULE_LICENSE("GPL v2");
3619MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);