1 /*
2 * Linux driver for VMware's vmxnet3 ethernet NIC.
3 *
4 * Copyright (C) 2008-2024, VMware, Inc. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License and no later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
14 * details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * The full GNU General Public License is included in this distribution in
21 * the file called "COPYING".
22 *
23 * Maintained by: pv-drivers@vmware.com
24 *
25 */
26
27 #include <linux/module.h>
28 #include <net/ip6_checksum.h>
29
30 #include "vmxnet3_int.h"
31 #include "vmxnet3_xdp.h"
32
33 char vmxnet3_driver_name[] = "vmxnet3";
34 #define VMXNET3_DRIVER_DESC "VMware vmxnet3 virtual NIC driver"
35
36 /*
37 * PCI Device ID Table
38 * Last entry must be all 0s
39 */
40 static const struct pci_device_id vmxnet3_pciid_table[] = {
41 {PCI_VDEVICE(VMWARE, PCI_DEVICE_ID_VMWARE_VMXNET3)},
42 {0}
43 };
44
45 MODULE_DEVICE_TABLE(pci, vmxnet3_pciid_table);
46
47 static int enable_mq = 1;
48
49 static void
50 vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, const u8 *mac);
51
52 /*
53 * Enable/Disable the given intr
54 */
55 static void
vmxnet3_enable_intr(struct vmxnet3_adapter * adapter,unsigned intr_idx)56 vmxnet3_enable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
57 {
58 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 0);
59 }
60
61
62 static void
vmxnet3_disable_intr(struct vmxnet3_adapter * adapter,unsigned intr_idx)63 vmxnet3_disable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
64 {
65 VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 1);
66 }
67
68
69 /*
70 * Enable/Disable all intrs used by the device
71 */
72 static void
vmxnet3_enable_all_intrs(struct vmxnet3_adapter * adapter)73 vmxnet3_enable_all_intrs(struct vmxnet3_adapter *adapter)
74 {
75 int i;
76
77 for (i = 0; i < adapter->intr.num_intrs; i++)
78 vmxnet3_enable_intr(adapter, i);
79 if (!VMXNET3_VERSION_GE_6(adapter) ||
80 !adapter->queuesExtEnabled) {
81 adapter->shared->devRead.intrConf.intrCtrl &=
82 cpu_to_le32(~VMXNET3_IC_DISABLE_ALL);
83 } else {
84 adapter->shared->devReadExt.intrConfExt.intrCtrl &=
85 cpu_to_le32(~VMXNET3_IC_DISABLE_ALL);
86 }
87 }
88
89
90 static void
vmxnet3_disable_all_intrs(struct vmxnet3_adapter * adapter)91 vmxnet3_disable_all_intrs(struct vmxnet3_adapter *adapter)
92 {
93 int i;
94
95 if (!VMXNET3_VERSION_GE_6(adapter) ||
96 !adapter->queuesExtEnabled) {
97 adapter->shared->devRead.intrConf.intrCtrl |=
98 cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
99 } else {
100 adapter->shared->devReadExt.intrConfExt.intrCtrl |=
101 cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
102 }
103 for (i = 0; i < adapter->intr.num_intrs; i++)
104 vmxnet3_disable_intr(adapter, i);
105 }
106
107
108 static void
vmxnet3_ack_events(struct vmxnet3_adapter * adapter,u32 events)109 vmxnet3_ack_events(struct vmxnet3_adapter *adapter, u32 events)
110 {
111 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_ECR, events);
112 }
113
114
115 static bool
vmxnet3_tq_stopped(struct vmxnet3_tx_queue * tq,struct vmxnet3_adapter * adapter)116 vmxnet3_tq_stopped(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
117 {
118 return tq->stopped;
119 }
120
121
122 static void
vmxnet3_tq_start(struct vmxnet3_tx_queue * tq,struct vmxnet3_adapter * adapter)123 vmxnet3_tq_start(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
124 {
125 tq->stopped = false;
126 netif_start_subqueue(adapter->netdev, tq - adapter->tx_queue);
127 }
128
129
130 static void
vmxnet3_tq_wake(struct vmxnet3_tx_queue * tq,struct vmxnet3_adapter * adapter)131 vmxnet3_tq_wake(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
132 {
133 tq->stopped = false;
134 netif_wake_subqueue(adapter->netdev, (tq - adapter->tx_queue));
135 }
136
137
138 static void
vmxnet3_tq_stop(struct vmxnet3_tx_queue * tq,struct vmxnet3_adapter * adapter)139 vmxnet3_tq_stop(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
140 {
141 tq->stopped = true;
142 tq->num_stop++;
143 netif_stop_subqueue(adapter->netdev, (tq - adapter->tx_queue));
144 }
145
146 static u64
vmxnet3_get_cycles(int pmc)147 vmxnet3_get_cycles(int pmc)
148 {
149 #ifdef CONFIG_X86
150 return native_read_pmc(pmc);
151 #else
152 return 0;
153 #endif
154 }
155
156 static bool
vmxnet3_apply_timestamp(struct vmxnet3_tx_queue * tq,u16 rate)157 vmxnet3_apply_timestamp(struct vmxnet3_tx_queue *tq, u16 rate)
158 {
159 #ifdef CONFIG_X86
160 if (rate > 0) {
161 if (tq->tsPktCount == 1) {
162 if (rate != 1)
163 tq->tsPktCount = rate;
164 return true;
165 }
166 tq->tsPktCount--;
167 }
168 #endif
169 return false;
170 }
171
172 /* Check if capability is supported by UPT device or
173 * UPT is even requested
174 */
175 bool
vmxnet3_check_ptcapability(u32 cap_supported,u32 cap)176 vmxnet3_check_ptcapability(u32 cap_supported, u32 cap)
177 {
178 if (cap_supported & (1UL << VMXNET3_DCR_ERROR) ||
179 cap_supported & (1UL << cap)) {
180 return true;
181 }
182
183 return false;
184 }
185
186
187 /*
188 * Check the link state. This may start or stop the tx queue.
189 */
190 static void
vmxnet3_check_link(struct vmxnet3_adapter * adapter,bool affectTxQueue)191 vmxnet3_check_link(struct vmxnet3_adapter *adapter, bool affectTxQueue)
192 {
193 u32 ret;
194 int i;
195 unsigned long flags;
196
197 spin_lock_irqsave(&adapter->cmd_lock, flags);
198 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
199 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
200 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
201
202 adapter->link_speed = ret >> 16;
203 if (ret & 1) { /* Link is up. */
204 netdev_info(adapter->netdev, "NIC Link is Up %d Mbps\n",
205 adapter->link_speed);
206 netif_carrier_on(adapter->netdev);
207
208 if (affectTxQueue) {
209 for (i = 0; i < adapter->num_tx_queues; i++)
210 vmxnet3_tq_start(&adapter->tx_queue[i],
211 adapter);
212 }
213 } else {
214 netdev_info(adapter->netdev, "NIC Link is Down\n");
215 netif_carrier_off(adapter->netdev);
216
217 if (affectTxQueue) {
218 for (i = 0; i < adapter->num_tx_queues; i++)
219 vmxnet3_tq_stop(&adapter->tx_queue[i], adapter);
220 }
221 }
222 }
223
224 static void
vmxnet3_process_events(struct vmxnet3_adapter * adapter)225 vmxnet3_process_events(struct vmxnet3_adapter *adapter)
226 {
227 int i;
228 unsigned long flags;
229 u32 events = le32_to_cpu(adapter->shared->ecr);
230 if (!events)
231 return;
232
233 vmxnet3_ack_events(adapter, events);
234
235 /* Check if link state has changed */
236 if (events & VMXNET3_ECR_LINK)
237 vmxnet3_check_link(adapter, true);
238
239 /* Check if there is an error on xmit/recv queues */
240 if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
241 spin_lock_irqsave(&adapter->cmd_lock, flags);
242 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
243 VMXNET3_CMD_GET_QUEUE_STATUS);
244 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
245
246 for (i = 0; i < adapter->num_tx_queues; i++)
247 if (adapter->tqd_start[i].status.stopped)
248 dev_err(&adapter->netdev->dev,
249 "%s: tq[%d] error 0x%x\n",
250 adapter->netdev->name, i, le32_to_cpu(
251 adapter->tqd_start[i].status.error));
252 for (i = 0; i < adapter->num_rx_queues; i++)
253 if (adapter->rqd_start[i].status.stopped)
254 dev_err(&adapter->netdev->dev,
255 "%s: rq[%d] error 0x%x\n",
256 adapter->netdev->name, i,
257 adapter->rqd_start[i].status.error);
258
259 schedule_work(&adapter->work);
260 }
261 }
262
263 #ifdef __BIG_ENDIAN_BITFIELD
264 /*
265 * The device expects the bitfields in shared structures to be written in
266 * little endian. When CPU is big endian, the following routines are used to
267 * correctly read and write into ABI.
268 * The general technique used here is : double word bitfields are defined in
269 * opposite order for big endian architecture. Then before reading them in
270 * driver the complete double word is translated using le32_to_cpu. Similarly
271 * After the driver writes into bitfields, cpu_to_le32 is used to translate the
272 * double words into required format.
273 * In order to avoid touching bits in shared structure more than once, temporary
274 * descriptors are used. These are passed as srcDesc to following functions.
275 */
vmxnet3_RxDescToCPU(const struct Vmxnet3_RxDesc * srcDesc,struct Vmxnet3_RxDesc * dstDesc)276 static void vmxnet3_RxDescToCPU(const struct Vmxnet3_RxDesc *srcDesc,
277 struct Vmxnet3_RxDesc *dstDesc)
278 {
279 u32 *src = (u32 *)srcDesc + 2;
280 u32 *dst = (u32 *)dstDesc + 2;
281 dstDesc->addr = le64_to_cpu(srcDesc->addr);
282 *dst = le32_to_cpu(*src);
283 dstDesc->ext1 = le32_to_cpu(srcDesc->ext1);
284 }
285
vmxnet3_TxDescToLe(const struct Vmxnet3_TxDesc * srcDesc,struct Vmxnet3_TxDesc * dstDesc)286 static void vmxnet3_TxDescToLe(const struct Vmxnet3_TxDesc *srcDesc,
287 struct Vmxnet3_TxDesc *dstDesc)
288 {
289 int i;
290 u32 *src = (u32 *)(srcDesc + 1);
291 u32 *dst = (u32 *)(dstDesc + 1);
292
293 /* Working backwards so that the gen bit is set at the end. */
294 for (i = 2; i > 0; i--) {
295 src--;
296 dst--;
297 *dst = cpu_to_le32(*src);
298 }
299 }
300
301
vmxnet3_RxCompToCPU(const struct Vmxnet3_RxCompDesc * srcDesc,struct Vmxnet3_RxCompDesc * dstDesc)302 static void vmxnet3_RxCompToCPU(const struct Vmxnet3_RxCompDesc *srcDesc,
303 struct Vmxnet3_RxCompDesc *dstDesc)
304 {
305 int i = 0;
306 u32 *src = (u32 *)srcDesc;
307 u32 *dst = (u32 *)dstDesc;
308 for (i = 0; i < sizeof(struct Vmxnet3_RxCompDesc) / sizeof(u32); i++) {
309 *dst = le32_to_cpu(*src);
310 src++;
311 dst++;
312 }
313 }
314
315
316 /* Used to read bitfield values from double words. */
get_bitfield32(const __le32 * bitfield,u32 pos,u32 size)317 static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
318 {
319 u32 temp = le32_to_cpu(*bitfield);
320 u32 mask = ((1 << size) - 1) << pos;
321 temp &= mask;
322 temp >>= pos;
323 return temp;
324 }
325
326
327
328 #endif /* __BIG_ENDIAN_BITFIELD */
329
330 #ifdef __BIG_ENDIAN_BITFIELD
331
332 # define VMXNET3_TXDESC_GET_GEN(txdesc) get_bitfield32(((const __le32 *) \
333 txdesc) + VMXNET3_TXD_GEN_DWORD_SHIFT, \
334 VMXNET3_TXD_GEN_SHIFT, VMXNET3_TXD_GEN_SIZE)
335 # define VMXNET3_TXDESC_GET_EOP(txdesc) get_bitfield32(((const __le32 *) \
336 txdesc) + VMXNET3_TXD_EOP_DWORD_SHIFT, \
337 VMXNET3_TXD_EOP_SHIFT, VMXNET3_TXD_EOP_SIZE)
338 # define VMXNET3_TCD_GET_GEN(tcd) get_bitfield32(((const __le32 *)tcd) + \
339 VMXNET3_TCD_GEN_DWORD_SHIFT, VMXNET3_TCD_GEN_SHIFT, \
340 VMXNET3_TCD_GEN_SIZE)
341 # define VMXNET3_TCD_GET_TXIDX(tcd) get_bitfield32((const __le32 *)tcd, \
342 VMXNET3_TCD_TXIDX_SHIFT, VMXNET3_TCD_TXIDX_SIZE)
343 # define vmxnet3_getRxComp(dstrcd, rcd, tmp) do { \
344 (dstrcd) = (tmp); \
345 vmxnet3_RxCompToCPU((rcd), (tmp)); \
346 } while (0)
347 # define vmxnet3_getRxDesc(dstrxd, rxd, tmp) do { \
348 (dstrxd) = (tmp); \
349 vmxnet3_RxDescToCPU((rxd), (tmp)); \
350 } while (0)
351
352 #else
353
354 # define VMXNET3_TXDESC_GET_GEN(txdesc) ((txdesc)->gen)
355 # define VMXNET3_TXDESC_GET_EOP(txdesc) ((txdesc)->eop)
356 # define VMXNET3_TCD_GET_GEN(tcd) ((tcd)->gen)
357 # define VMXNET3_TCD_GET_TXIDX(tcd) ((tcd)->txdIdx)
358 # define vmxnet3_getRxComp(dstrcd, rcd, tmp) (dstrcd) = (rcd)
359 # define vmxnet3_getRxDesc(dstrxd, rxd, tmp) (dstrxd) = (rxd)
360
361 #endif /* __BIG_ENDIAN_BITFIELD */
362
363
364 static void
vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info * tbi,struct pci_dev * pdev)365 vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
366 struct pci_dev *pdev)
367 {
368 u32 map_type = tbi->map_type;
369
370 if (map_type & VMXNET3_MAP_SINGLE)
371 dma_unmap_single(&pdev->dev, tbi->dma_addr, tbi->len,
372 DMA_TO_DEVICE);
373 else if (map_type & VMXNET3_MAP_PAGE)
374 dma_unmap_page(&pdev->dev, tbi->dma_addr, tbi->len,
375 DMA_TO_DEVICE);
376 else
377 BUG_ON(map_type & ~VMXNET3_MAP_XDP);
378
379 tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
380 }
381
382
383 static int
vmxnet3_unmap_pkt(u32 eop_idx,struct vmxnet3_tx_queue * tq,struct pci_dev * pdev,struct vmxnet3_adapter * adapter,struct xdp_frame_bulk * bq)384 vmxnet3_unmap_pkt(u32 eop_idx, struct vmxnet3_tx_queue *tq,
385 struct pci_dev *pdev, struct vmxnet3_adapter *adapter,
386 struct xdp_frame_bulk *bq)
387 {
388 struct vmxnet3_tx_buf_info *tbi;
389 int entries = 0;
390 u32 map_type;
391
392 /* no out of order completion */
393 BUG_ON(tq->buf_info[eop_idx].sop_idx != tq->tx_ring.next2comp);
394 BUG_ON(VMXNET3_TXDESC_GET_EOP(&(tq->tx_ring.base[eop_idx].txd)) != 1);
395
396 tbi = &tq->buf_info[eop_idx];
397 BUG_ON(!tbi->skb);
398 map_type = tbi->map_type;
399 VMXNET3_INC_RING_IDX_ONLY(eop_idx, tq->tx_ring.size);
400
401 while (tq->tx_ring.next2comp != eop_idx) {
402 vmxnet3_unmap_tx_buf(tq->buf_info + tq->tx_ring.next2comp,
403 pdev);
404
405 /* update next2comp w/o tx_lock. Since we are marking more,
406 * instead of less, tx ring entries avail, the worst case is
407 * that the tx routine incorrectly re-queues a pkt due to
408 * insufficient tx ring entries.
409 */
410 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
411 entries++;
412 }
413
414 if (map_type & VMXNET3_MAP_XDP)
415 xdp_return_frame_bulk(tbi->xdpf, bq);
416 else
417 dev_kfree_skb_any(tbi->skb);
418
419 /* xdpf and skb are in an anonymous union. */
420 tbi->skb = NULL;
421
422 return entries;
423 }
424
425
426 static int
vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue * tq,struct vmxnet3_adapter * adapter)427 vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue *tq,
428 struct vmxnet3_adapter *adapter)
429 {
430 union Vmxnet3_GenericDesc *gdesc;
431 struct xdp_frame_bulk bq;
432 int completed = 0;
433
434 xdp_frame_bulk_init(&bq);
435 rcu_read_lock();
436
437 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
438 while (VMXNET3_TCD_GET_GEN(&gdesc->tcd) == tq->comp_ring.gen) {
439 /* Prevent any &gdesc->tcd field from being (speculatively)
440 * read before (&gdesc->tcd)->gen is read.
441 */
442 dma_rmb();
443
444 completed += vmxnet3_unmap_pkt(VMXNET3_TCD_GET_TXIDX(
445 &gdesc->tcd), tq, adapter->pdev,
446 adapter, &bq);
447
448 vmxnet3_comp_ring_adv_next2proc(&tq->comp_ring);
449 gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
450 }
451 xdp_flush_frame_bulk(&bq);
452 rcu_read_unlock();
453
454 if (completed) {
455 spin_lock(&tq->tx_lock);
456 if (unlikely(vmxnet3_tq_stopped(tq, adapter) &&
457 vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) >
458 VMXNET3_WAKE_QUEUE_THRESHOLD(tq) &&
459 netif_carrier_ok(adapter->netdev))) {
460 vmxnet3_tq_wake(tq, adapter);
461 }
462 spin_unlock(&tq->tx_lock);
463 }
464 return completed;
465 }
466
467
468 static void
vmxnet3_tq_cleanup(struct vmxnet3_tx_queue * tq,struct vmxnet3_adapter * adapter)469 vmxnet3_tq_cleanup(struct vmxnet3_tx_queue *tq,
470 struct vmxnet3_adapter *adapter)
471 {
472 struct xdp_frame_bulk bq;
473 u32 map_type;
474 int i;
475
476 xdp_frame_bulk_init(&bq);
477 rcu_read_lock();
478
479 while (tq->tx_ring.next2comp != tq->tx_ring.next2fill) {
480 struct vmxnet3_tx_buf_info *tbi;
481
482 tbi = tq->buf_info + tq->tx_ring.next2comp;
483 map_type = tbi->map_type;
484
485 vmxnet3_unmap_tx_buf(tbi, adapter->pdev);
486 if (tbi->skb) {
487 if (map_type & VMXNET3_MAP_XDP)
488 xdp_return_frame_bulk(tbi->xdpf, &bq);
489 else
490 dev_kfree_skb_any(tbi->skb);
491 tbi->skb = NULL;
492 }
493 vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
494 }
495
496 xdp_flush_frame_bulk(&bq);
497 rcu_read_unlock();
498
499 /* sanity check, verify all buffers are indeed unmapped */
500 for (i = 0; i < tq->tx_ring.size; i++)
501 BUG_ON(tq->buf_info[i].map_type != VMXNET3_MAP_NONE);
502
503 tq->tx_ring.gen = VMXNET3_INIT_GEN;
504 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
505
506 tq->comp_ring.gen = VMXNET3_INIT_GEN;
507 tq->comp_ring.next2proc = 0;
508 }
509
510
511 static void
vmxnet3_tq_destroy(struct vmxnet3_tx_queue * tq,struct vmxnet3_adapter * adapter)512 vmxnet3_tq_destroy(struct vmxnet3_tx_queue *tq,
513 struct vmxnet3_adapter *adapter)
514 {
515 if (tq->tx_ring.base) {
516 dma_free_coherent(&adapter->pdev->dev, tq->tx_ring.size *
517 sizeof(struct Vmxnet3_TxDesc),
518 tq->tx_ring.base, tq->tx_ring.basePA);
519 tq->tx_ring.base = NULL;
520 }
521 if (tq->data_ring.base) {
522 dma_free_coherent(&adapter->pdev->dev,
523 tq->data_ring.size * tq->txdata_desc_size,
524 tq->data_ring.base, tq->data_ring.basePA);
525 tq->data_ring.base = NULL;
526 }
527 if (tq->ts_ring.base) {
528 dma_free_coherent(&adapter->pdev->dev,
529 tq->tx_ring.size * tq->tx_ts_desc_size,
530 tq->ts_ring.base, tq->ts_ring.basePA);
531 tq->ts_ring.base = NULL;
532 }
533 if (tq->comp_ring.base) {
534 dma_free_coherent(&adapter->pdev->dev, tq->comp_ring.size *
535 sizeof(struct Vmxnet3_TxCompDesc),
536 tq->comp_ring.base, tq->comp_ring.basePA);
537 tq->comp_ring.base = NULL;
538 }
539 kfree(tq->buf_info);
540 tq->buf_info = NULL;
541 }
542
543
544 /* Destroy all tx queues */
545 void
vmxnet3_tq_destroy_all(struct vmxnet3_adapter * adapter)546 vmxnet3_tq_destroy_all(struct vmxnet3_adapter *adapter)
547 {
548 int i;
549
550 for (i = 0; i < adapter->num_tx_queues; i++)
551 vmxnet3_tq_destroy(&adapter->tx_queue[i], adapter);
552 }
553
554
555 static void
vmxnet3_tq_init(struct vmxnet3_tx_queue * tq,struct vmxnet3_adapter * adapter)556 vmxnet3_tq_init(struct vmxnet3_tx_queue *tq,
557 struct vmxnet3_adapter *adapter)
558 {
559 int i;
560
561 /* reset the tx ring contents to 0 and reset the tx ring states */
562 memset(tq->tx_ring.base, 0, tq->tx_ring.size *
563 sizeof(struct Vmxnet3_TxDesc));
564 tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
565 tq->tx_ring.gen = VMXNET3_INIT_GEN;
566
567 memset(tq->data_ring.base, 0,
568 tq->data_ring.size * tq->txdata_desc_size);
569
570 if (tq->ts_ring.base)
571 memset(tq->ts_ring.base, 0,
572 tq->tx_ring.size * tq->tx_ts_desc_size);
573
574 /* reset the tx comp ring contents to 0 and reset comp ring states */
575 memset(tq->comp_ring.base, 0, tq->comp_ring.size *
576 sizeof(struct Vmxnet3_TxCompDesc));
577 tq->comp_ring.next2proc = 0;
578 tq->comp_ring.gen = VMXNET3_INIT_GEN;
579
580 /* reset the bookkeeping data */
581 memset(tq->buf_info, 0, sizeof(tq->buf_info[0]) * tq->tx_ring.size);
582 for (i = 0; i < tq->tx_ring.size; i++)
583 tq->buf_info[i].map_type = VMXNET3_MAP_NONE;
584
585 /* stats are not reset */
586 }
587
588
589 static int
vmxnet3_tq_create(struct vmxnet3_tx_queue * tq,struct vmxnet3_adapter * adapter)590 vmxnet3_tq_create(struct vmxnet3_tx_queue *tq,
591 struct vmxnet3_adapter *adapter)
592 {
593 BUG_ON(tq->tx_ring.base || tq->data_ring.base ||
594 tq->comp_ring.base || tq->buf_info);
595
596 tq->tx_ring.base = dma_alloc_coherent(&adapter->pdev->dev,
597 tq->tx_ring.size * sizeof(struct Vmxnet3_TxDesc),
598 &tq->tx_ring.basePA, GFP_KERNEL);
599 if (!tq->tx_ring.base) {
600 netdev_err(adapter->netdev, "failed to allocate tx ring\n");
601 goto err;
602 }
603
604 tq->data_ring.base = dma_alloc_coherent(&adapter->pdev->dev,
605 tq->data_ring.size * tq->txdata_desc_size,
606 &tq->data_ring.basePA, GFP_KERNEL);
607 if (!tq->data_ring.base) {
608 netdev_err(adapter->netdev, "failed to allocate tx data ring\n");
609 goto err;
610 }
611
612 if (tq->tx_ts_desc_size != 0) {
613 tq->ts_ring.base = dma_alloc_coherent(&adapter->pdev->dev,
614 tq->tx_ring.size * tq->tx_ts_desc_size,
615 &tq->ts_ring.basePA, GFP_KERNEL);
616 if (!tq->ts_ring.base) {
617 netdev_err(adapter->netdev, "failed to allocate tx ts ring\n");
618 tq->tx_ts_desc_size = 0;
619 }
620 } else {
621 tq->ts_ring.base = NULL;
622 }
623
624 tq->comp_ring.base = dma_alloc_coherent(&adapter->pdev->dev,
625 tq->comp_ring.size * sizeof(struct Vmxnet3_TxCompDesc),
626 &tq->comp_ring.basePA, GFP_KERNEL);
627 if (!tq->comp_ring.base) {
628 netdev_err(adapter->netdev, "failed to allocate tx comp ring\n");
629 goto err;
630 }
631
632 tq->buf_info = kcalloc_node(tq->tx_ring.size, sizeof(tq->buf_info[0]),
633 GFP_KERNEL,
634 dev_to_node(&adapter->pdev->dev));
635 if (!tq->buf_info)
636 goto err;
637
638 return 0;
639
640 err:
641 vmxnet3_tq_destroy(tq, adapter);
642 return -ENOMEM;
643 }
644
645 static void
vmxnet3_tq_cleanup_all(struct vmxnet3_adapter * adapter)646 vmxnet3_tq_cleanup_all(struct vmxnet3_adapter *adapter)
647 {
648 int i;
649
650 for (i = 0; i < adapter->num_tx_queues; i++)
651 vmxnet3_tq_cleanup(&adapter->tx_queue[i], adapter);
652 }
653
654 /*
655 * starting from ring->next2fill, allocate rx buffers for the given ring
656 * of the rx queue and update the rx desc. stop after @num_to_alloc buffers
657 * are allocated or allocation fails
658 */
659
660 static int
vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue * rq,u32 ring_idx,int num_to_alloc,struct vmxnet3_adapter * adapter)661 vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
662 int num_to_alloc, struct vmxnet3_adapter *adapter)
663 {
664 int num_allocated = 0;
665 struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
666 struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
667 u32 val;
668
669 while (num_allocated <= num_to_alloc) {
670 struct vmxnet3_rx_buf_info *rbi;
671 union Vmxnet3_GenericDesc *gd;
672
673 rbi = rbi_base + ring->next2fill;
674 gd = ring->base + ring->next2fill;
675 rbi->comp_state = VMXNET3_RXD_COMP_PENDING;
676
677 if (rbi->buf_type == VMXNET3_RX_BUF_XDP) {
678 void *data = vmxnet3_pp_get_buff(rq->page_pool,
679 &rbi->dma_addr,
680 GFP_KERNEL);
681 if (!data) {
682 rq->stats.rx_buf_alloc_failure++;
683 break;
684 }
685 rbi->page = virt_to_page(data);
686 val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
687 } else if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
688 if (rbi->skb == NULL) {
689 rbi->skb = __netdev_alloc_skb_ip_align(adapter->netdev,
690 rbi->len,
691 GFP_KERNEL);
692 if (unlikely(rbi->skb == NULL)) {
693 rq->stats.rx_buf_alloc_failure++;
694 break;
695 }
696
697 rbi->dma_addr = dma_map_single(
698 &adapter->pdev->dev,
699 rbi->skb->data, rbi->len,
700 DMA_FROM_DEVICE);
701 if (dma_mapping_error(&adapter->pdev->dev,
702 rbi->dma_addr)) {
703 dev_kfree_skb_any(rbi->skb);
704 rbi->skb = NULL;
705 rq->stats.rx_buf_alloc_failure++;
706 break;
707 }
708 } else {
709 /* rx buffer skipped by the device */
710 }
711 val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
712 } else {
713 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
714 rbi->len != PAGE_SIZE);
715
716 if (rbi->page == NULL) {
717 rbi->page = alloc_page(GFP_ATOMIC);
718 if (unlikely(rbi->page == NULL)) {
719 rq->stats.rx_buf_alloc_failure++;
720 break;
721 }
722 rbi->dma_addr = dma_map_page(
723 &adapter->pdev->dev,
724 rbi->page, 0, PAGE_SIZE,
725 DMA_FROM_DEVICE);
726 if (dma_mapping_error(&adapter->pdev->dev,
727 rbi->dma_addr)) {
728 put_page(rbi->page);
729 rbi->page = NULL;
730 rq->stats.rx_buf_alloc_failure++;
731 break;
732 }
733 } else {
734 /* rx buffers skipped by the device */
735 }
736 val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
737 }
738
739 gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
740 gd->dword[2] = cpu_to_le32((!ring->gen << VMXNET3_RXD_GEN_SHIFT)
741 | val | rbi->len);
742
743 /* Fill the last buffer but dont mark it ready, or else the
744 * device will think that the queue is full */
745 if (num_allocated == num_to_alloc) {
746 rbi->comp_state = VMXNET3_RXD_COMP_DONE;
747 break;
748 }
749
750 gd->dword[2] |= cpu_to_le32(ring->gen << VMXNET3_RXD_GEN_SHIFT);
751 num_allocated++;
752 vmxnet3_cmd_ring_adv_next2fill(ring);
753 }
754
755 netdev_dbg(adapter->netdev,
756 "alloc_rx_buf: %d allocated, next2fill %u, next2comp %u\n",
757 num_allocated, ring->next2fill, ring->next2comp);
758
759 /* so that the device can distinguish a full ring and an empty ring */
760 BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
761
762 return num_allocated;
763 }
764
765
766 static void
vmxnet3_append_frag(struct sk_buff * skb,struct Vmxnet3_RxCompDesc * rcd,struct vmxnet3_rx_buf_info * rbi)767 vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
768 struct vmxnet3_rx_buf_info *rbi)
769 {
770 skb_frag_t *frag = skb_shinfo(skb)->frags + skb_shinfo(skb)->nr_frags;
771
772 BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
773
774 skb_frag_fill_page_desc(frag, rbi->page, 0, rcd->len);
775 skb->data_len += rcd->len;
776 skb->truesize += PAGE_SIZE;
777 skb_shinfo(skb)->nr_frags++;
778 }
779
780
781 static int
vmxnet3_map_pkt(struct sk_buff * skb,struct vmxnet3_tx_ctx * ctx,struct vmxnet3_tx_queue * tq,struct pci_dev * pdev,struct vmxnet3_adapter * adapter)782 vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
783 struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
784 struct vmxnet3_adapter *adapter)
785 {
786 u32 dw2, len;
787 unsigned long buf_offset;
788 int i;
789 union Vmxnet3_GenericDesc *gdesc;
790 struct vmxnet3_tx_buf_info *tbi = NULL;
791
792 BUG_ON(ctx->copy_size > skb_headlen(skb));
793
794 /* use the previous gen bit for the SOP desc */
795 dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
796
797 ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
798 gdesc = ctx->sop_txd; /* both loops below can be skipped */
799
800 /* no need to map the buffer if headers are copied */
801 if (ctx->copy_size) {
802 ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
803 tq->tx_ring.next2fill *
804 tq->txdata_desc_size);
805 ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
806 ctx->sop_txd->dword[3] = 0;
807
808 tbi = tq->buf_info + tq->tx_ring.next2fill;
809 tbi->map_type = VMXNET3_MAP_NONE;
810
811 netdev_dbg(adapter->netdev,
812 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
813 tq->tx_ring.next2fill,
814 le64_to_cpu(ctx->sop_txd->txd.addr),
815 ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
816 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
817
818 /* use the right gen for non-SOP desc */
819 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
820 }
821
822 /* linear part can use multiple tx desc if it's big */
823 len = skb_headlen(skb) - ctx->copy_size;
824 buf_offset = ctx->copy_size;
825 while (len) {
826 u32 buf_size;
827
828 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
829 buf_size = len;
830 dw2 |= len;
831 } else {
832 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
833 /* spec says that for TxDesc.len, 0 == 2^14 */
834 }
835
836 tbi = tq->buf_info + tq->tx_ring.next2fill;
837 tbi->map_type = VMXNET3_MAP_SINGLE;
838 tbi->dma_addr = dma_map_single(&adapter->pdev->dev,
839 skb->data + buf_offset, buf_size,
840 DMA_TO_DEVICE);
841 if (dma_mapping_error(&adapter->pdev->dev, tbi->dma_addr))
842 return -EFAULT;
843
844 tbi->len = buf_size;
845
846 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
847 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
848
849 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
850 gdesc->dword[2] = cpu_to_le32(dw2);
851 gdesc->dword[3] = 0;
852
853 netdev_dbg(adapter->netdev,
854 "txd[%u]: 0x%Lx 0x%x 0x%x\n",
855 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
856 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
857 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
858 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
859
860 len -= buf_size;
861 buf_offset += buf_size;
862 }
863
864 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
865 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
866 u32 buf_size;
867
868 buf_offset = 0;
869 len = skb_frag_size(frag);
870 while (len) {
871 tbi = tq->buf_info + tq->tx_ring.next2fill;
872 if (len < VMXNET3_MAX_TX_BUF_SIZE) {
873 buf_size = len;
874 dw2 |= len;
875 } else {
876 buf_size = VMXNET3_MAX_TX_BUF_SIZE;
877 /* spec says that for TxDesc.len, 0 == 2^14 */
878 }
879 tbi->map_type = VMXNET3_MAP_PAGE;
880 tbi->dma_addr = skb_frag_dma_map(&adapter->pdev->dev, frag,
881 buf_offset, buf_size,
882 DMA_TO_DEVICE);
883 if (dma_mapping_error(&adapter->pdev->dev, tbi->dma_addr))
884 return -EFAULT;
885
886 tbi->len = buf_size;
887
888 gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
889 BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
890
891 gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
892 gdesc->dword[2] = cpu_to_le32(dw2);
893 gdesc->dword[3] = 0;
894
895 netdev_dbg(adapter->netdev,
896 "txd[%u]: 0x%llx %u %u\n",
897 tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
898 le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
899 vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
900 dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
901
902 len -= buf_size;
903 buf_offset += buf_size;
904 }
905 }
906
907 ctx->eop_txd = gdesc;
908
909 /* set the last buf_info for the pkt */
910 tbi->skb = skb;
911 tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
912 if (tq->tx_ts_desc_size != 0) {
913 ctx->ts_txd = (struct Vmxnet3_TxTSDesc *)((u8 *)tq->ts_ring.base +
914 tbi->sop_idx * tq->tx_ts_desc_size);
915 ctx->ts_txd->ts.tsi = 0;
916 }
917
918 return 0;
919 }
920
921
922 /* Init all tx queues */
923 static void
vmxnet3_tq_init_all(struct vmxnet3_adapter * adapter)924 vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
925 {
926 int i;
927
928 for (i = 0; i < adapter->num_tx_queues; i++)
929 vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
930 }
931
932
933 /*
934 * parse relevant protocol headers:
935 * For a tso pkt, relevant headers are L2/3/4 including options
936 * For a pkt requesting csum offloading, they are L2/3 and may include L4
937 * if it's a TCP/UDP pkt
938 *
939 * Returns:
940 * -1: error happens during parsing
941 * 0: protocol headers parsed, but too big to be copied
942 * 1: protocol headers parsed and copied
943 *
944 * Other effects:
945 * 1. related *ctx fields are updated.
946 * 2. ctx->copy_size is # of bytes copied
947 * 3. the portion to be copied is guaranteed to be in the linear part
948 *
949 */
950 static int
vmxnet3_parse_hdr(struct sk_buff * skb,struct vmxnet3_tx_queue * tq,struct vmxnet3_tx_ctx * ctx,struct vmxnet3_adapter * adapter)951 vmxnet3_parse_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
952 struct vmxnet3_tx_ctx *ctx,
953 struct vmxnet3_adapter *adapter)
954 {
955 u8 protocol = 0;
956
957 if (ctx->mss) { /* TSO */
958 if (VMXNET3_VERSION_GE_4(adapter) && skb->encapsulation) {
959 ctx->l4_offset = skb_inner_transport_offset(skb);
960 ctx->l4_hdr_size = inner_tcp_hdrlen(skb);
961 ctx->copy_size = ctx->l4_offset + ctx->l4_hdr_size;
962 } else {
963 ctx->l4_offset = skb_transport_offset(skb);
964 ctx->l4_hdr_size = tcp_hdrlen(skb);
965 ctx->copy_size = ctx->l4_offset + ctx->l4_hdr_size;
966 }
967 } else {
968 if (skb->ip_summed == CHECKSUM_PARTIAL) {
969 /* For encap packets, skb_checksum_start_offset refers
970 * to inner L4 offset. Thus, below works for encap as
971 * well as non-encap case
972 */
973 ctx->l4_offset = skb_checksum_start_offset(skb);
974
975 if (VMXNET3_VERSION_GE_4(adapter) &&
976 skb->encapsulation) {
977 struct iphdr *iph = inner_ip_hdr(skb);
978
979 if (iph->version == 4) {
980 protocol = iph->protocol;
981 } else {
982 const struct ipv6hdr *ipv6h;
983
984 ipv6h = inner_ipv6_hdr(skb);
985 protocol = ipv6h->nexthdr;
986 }
987 } else {
988 if (ctx->ipv4) {
989 const struct iphdr *iph = ip_hdr(skb);
990
991 protocol = iph->protocol;
992 } else if (ctx->ipv6) {
993 const struct ipv6hdr *ipv6h;
994
995 ipv6h = ipv6_hdr(skb);
996 protocol = ipv6h->nexthdr;
997 }
998 }
999
1000 switch (protocol) {
1001 case IPPROTO_TCP:
1002 ctx->l4_hdr_size = skb->encapsulation ? inner_tcp_hdrlen(skb) :
1003 tcp_hdrlen(skb);
1004 break;
1005 case IPPROTO_UDP:
1006 ctx->l4_hdr_size = sizeof(struct udphdr);
1007 break;
1008 default:
1009 ctx->l4_hdr_size = 0;
1010 break;
1011 }
1012
1013 ctx->copy_size = min(ctx->l4_offset +
1014 ctx->l4_hdr_size, skb->len);
1015 } else {
1016 ctx->l4_offset = 0;
1017 ctx->l4_hdr_size = 0;
1018 /* copy as much as allowed */
1019 ctx->copy_size = min_t(unsigned int,
1020 tq->txdata_desc_size,
1021 skb_headlen(skb));
1022 }
1023
1024 if (skb->len <= tq->txdata_desc_size)
1025 ctx->copy_size = skb->len;
1026
1027 /* make sure headers are accessible directly */
1028 if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
1029 goto err;
1030 }
1031
1032 if (unlikely(ctx->copy_size > tq->txdata_desc_size)) {
1033 tq->stats.oversized_hdr++;
1034 ctx->copy_size = 0;
1035 return 0;
1036 }
1037
1038 return 1;
1039 err:
1040 return -1;
1041 }
1042
1043 /*
1044 * copy relevant protocol headers to the transmit ring:
1045 * For a tso pkt, relevant headers are L2/3/4 including options
1046 * For a pkt requesting csum offloading, they are L2/3 and may include L4
1047 * if it's a TCP/UDP pkt
1048 *
1049 *
1050 * Note that this requires that vmxnet3_parse_hdr be called first to set the
1051 * appropriate bits in ctx first
1052 */
1053 static void
vmxnet3_copy_hdr(struct sk_buff * skb,struct vmxnet3_tx_queue * tq,struct vmxnet3_tx_ctx * ctx,struct vmxnet3_adapter * adapter)1054 vmxnet3_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
1055 struct vmxnet3_tx_ctx *ctx,
1056 struct vmxnet3_adapter *adapter)
1057 {
1058 struct Vmxnet3_TxDataDesc *tdd;
1059
1060 tdd = (struct Vmxnet3_TxDataDesc *)((u8 *)tq->data_ring.base +
1061 tq->tx_ring.next2fill *
1062 tq->txdata_desc_size);
1063
1064 memcpy(tdd->data, skb->data, ctx->copy_size);
1065 netdev_dbg(adapter->netdev,
1066 "copy %u bytes to dataRing[%u]\n",
1067 ctx->copy_size, tq->tx_ring.next2fill);
1068 }
1069
1070
1071 static void
vmxnet3_prepare_inner_tso(struct sk_buff * skb,struct vmxnet3_tx_ctx * ctx)1072 vmxnet3_prepare_inner_tso(struct sk_buff *skb,
1073 struct vmxnet3_tx_ctx *ctx)
1074 {
1075 struct tcphdr *tcph = inner_tcp_hdr(skb);
1076 struct iphdr *iph = inner_ip_hdr(skb);
1077
1078 if (iph->version == 4) {
1079 iph->check = 0;
1080 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
1081 IPPROTO_TCP, 0);
1082 } else {
1083 struct ipv6hdr *iph = inner_ipv6_hdr(skb);
1084
1085 tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
1086 IPPROTO_TCP, 0);
1087 }
1088 }
1089
1090 static void
vmxnet3_prepare_tso(struct sk_buff * skb,struct vmxnet3_tx_ctx * ctx)1091 vmxnet3_prepare_tso(struct sk_buff *skb,
1092 struct vmxnet3_tx_ctx *ctx)
1093 {
1094 struct tcphdr *tcph = tcp_hdr(skb);
1095
1096 if (ctx->ipv4) {
1097 struct iphdr *iph = ip_hdr(skb);
1098
1099 iph->check = 0;
1100 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
1101 IPPROTO_TCP, 0);
1102 } else if (ctx->ipv6) {
1103 tcp_v6_gso_csum_prep(skb);
1104 }
1105 }
1106
txd_estimate(const struct sk_buff * skb)1107 static int txd_estimate(const struct sk_buff *skb)
1108 {
1109 int count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
1110 int i;
1111
1112 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1113 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1114
1115 count += VMXNET3_TXD_NEEDED(skb_frag_size(frag));
1116 }
1117 return count;
1118 }
1119
1120 /*
1121 * Transmits a pkt thru a given tq
1122 * Returns:
1123 * NETDEV_TX_OK: descriptors are setup successfully
1124 * NETDEV_TX_OK: error occurred, the pkt is dropped
1125 * NETDEV_TX_BUSY: tx ring is full, queue is stopped
1126 *
1127 * Side-effects:
1128 * 1. tx ring may be changed
1129 * 2. tq stats may be updated accordingly
1130 * 3. shared->txNumDeferred may be updated
1131 */
1132
1133 static int
vmxnet3_tq_xmit(struct sk_buff * skb,struct vmxnet3_tx_queue * tq,struct vmxnet3_adapter * adapter,struct net_device * netdev)1134 vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
1135 struct vmxnet3_adapter *adapter, struct net_device *netdev)
1136 {
1137 int ret;
1138 u32 count;
1139 int num_pkts;
1140 int tx_num_deferred;
1141 unsigned long flags;
1142 struct vmxnet3_tx_ctx ctx;
1143 union Vmxnet3_GenericDesc *gdesc;
1144 #ifdef __BIG_ENDIAN_BITFIELD
1145 /* Use temporary descriptor to avoid touching bits multiple times */
1146 union Vmxnet3_GenericDesc tempTxDesc;
1147 #endif
1148
1149 count = txd_estimate(skb);
1150
1151 ctx.ipv4 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IP));
1152 ctx.ipv6 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IPV6));
1153
1154 ctx.mss = skb_shinfo(skb)->gso_size;
1155 if (ctx.mss) {
1156 if (skb_header_cloned(skb)) {
1157 if (unlikely(pskb_expand_head(skb, 0, 0,
1158 GFP_ATOMIC) != 0)) {
1159 tq->stats.drop_tso++;
1160 goto drop_pkt;
1161 }
1162 tq->stats.copy_skb_header++;
1163 }
1164 if (unlikely(count > VMXNET3_MAX_TSO_TXD_PER_PKT)) {
1165 /* tso pkts must not use more than
1166 * VMXNET3_MAX_TSO_TXD_PER_PKT entries
1167 */
1168 if (skb_linearize(skb) != 0) {
1169 tq->stats.drop_too_many_frags++;
1170 goto drop_pkt;
1171 }
1172 tq->stats.linearized++;
1173
1174 /* recalculate the # of descriptors to use */
1175 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
1176 if (unlikely(count > VMXNET3_MAX_TSO_TXD_PER_PKT)) {
1177 tq->stats.drop_too_many_frags++;
1178 goto drop_pkt;
1179 }
1180 }
1181 if (skb->encapsulation) {
1182 vmxnet3_prepare_inner_tso(skb, &ctx);
1183 } else {
1184 vmxnet3_prepare_tso(skb, &ctx);
1185 }
1186 } else {
1187 if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
1188
1189 /* non-tso pkts must not use more than
1190 * VMXNET3_MAX_TXD_PER_PKT entries
1191 */
1192 if (skb_linearize(skb) != 0) {
1193 tq->stats.drop_too_many_frags++;
1194 goto drop_pkt;
1195 }
1196 tq->stats.linearized++;
1197
1198 /* recalculate the # of descriptors to use */
1199 count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
1200 }
1201 }
1202
1203 ret = vmxnet3_parse_hdr(skb, tq, &ctx, adapter);
1204 if (ret >= 0) {
1205 BUG_ON(ret <= 0 && ctx.copy_size != 0);
1206 /* hdrs parsed, check against other limits */
1207 if (ctx.mss) {
1208 if (unlikely(ctx.l4_offset + ctx.l4_hdr_size >
1209 VMXNET3_MAX_TX_BUF_SIZE)) {
1210 tq->stats.drop_oversized_hdr++;
1211 goto drop_pkt;
1212 }
1213 } else {
1214 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1215 if (unlikely(ctx.l4_offset +
1216 skb->csum_offset >
1217 VMXNET3_MAX_CSUM_OFFSET)) {
1218 tq->stats.drop_oversized_hdr++;
1219 goto drop_pkt;
1220 }
1221 }
1222 }
1223 } else {
1224 tq->stats.drop_hdr_inspect_err++;
1225 goto drop_pkt;
1226 }
1227
1228 spin_lock_irqsave(&tq->tx_lock, flags);
1229
1230 if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
1231 tq->stats.tx_ring_full++;
1232 netdev_dbg(adapter->netdev,
1233 "tx queue stopped on %s, next2comp %u"
1234 " next2fill %u\n", adapter->netdev->name,
1235 tq->tx_ring.next2comp, tq->tx_ring.next2fill);
1236
1237 vmxnet3_tq_stop(tq, adapter);
1238 spin_unlock_irqrestore(&tq->tx_lock, flags);
1239 return NETDEV_TX_BUSY;
1240 }
1241
1242
1243 vmxnet3_copy_hdr(skb, tq, &ctx, adapter);
1244
1245 /* fill tx descs related to addr & len */
1246 if (vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter))
1247 goto unlock_drop_pkt;
1248
1249 /* setup the EOP desc */
1250 ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
1251
1252 /* setup the SOP desc */
1253 #ifdef __BIG_ENDIAN_BITFIELD
1254 gdesc = &tempTxDesc;
1255 gdesc->dword[2] = ctx.sop_txd->dword[2];
1256 gdesc->dword[3] = ctx.sop_txd->dword[3];
1257 #else
1258 gdesc = ctx.sop_txd;
1259 #endif
1260 tx_num_deferred = le32_to_cpu(tq->shared->txNumDeferred);
1261 if (ctx.mss) {
1262 if (VMXNET3_VERSION_GE_4(adapter) && skb->encapsulation) {
1263 gdesc->txd.hlen = ctx.l4_offset + ctx.l4_hdr_size;
1264 if (VMXNET3_VERSION_GE_7(adapter)) {
1265 gdesc->txd.om = VMXNET3_OM_TSO;
1266 gdesc->txd.ext1 = 1;
1267 } else {
1268 gdesc->txd.om = VMXNET3_OM_ENCAP;
1269 }
1270 gdesc->txd.msscof = ctx.mss;
1271
1272 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)
1273 gdesc->txd.oco = 1;
1274 } else {
1275 gdesc->txd.hlen = ctx.l4_offset + ctx.l4_hdr_size;
1276 gdesc->txd.om = VMXNET3_OM_TSO;
1277 gdesc->txd.msscof = ctx.mss;
1278 }
1279 num_pkts = (skb->len - gdesc->txd.hlen + ctx.mss - 1) / ctx.mss;
1280 } else {
1281 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1282 if (VMXNET3_VERSION_GE_4(adapter) &&
1283 skb->encapsulation) {
1284 gdesc->txd.hlen = ctx.l4_offset +
1285 ctx.l4_hdr_size;
1286 if (VMXNET3_VERSION_GE_7(adapter)) {
1287 gdesc->txd.om = VMXNET3_OM_CSUM;
1288 gdesc->txd.msscof = ctx.l4_offset +
1289 skb->csum_offset;
1290 gdesc->txd.ext1 = 1;
1291 } else {
1292 gdesc->txd.om = VMXNET3_OM_ENCAP;
1293 gdesc->txd.msscof = 0; /* Reserved */
1294 }
1295 } else {
1296 gdesc->txd.hlen = ctx.l4_offset;
1297 gdesc->txd.om = VMXNET3_OM_CSUM;
1298 gdesc->txd.msscof = ctx.l4_offset +
1299 skb->csum_offset;
1300 }
1301 } else {
1302 gdesc->txd.om = 0;
1303 gdesc->txd.msscof = 0;
1304 }
1305 num_pkts = 1;
1306 }
1307 le32_add_cpu(&tq->shared->txNumDeferred, num_pkts);
1308 tx_num_deferred += num_pkts;
1309
1310 if (skb_vlan_tag_present(skb)) {
1311 gdesc->txd.ti = 1;
1312 gdesc->txd.tci = skb_vlan_tag_get(skb);
1313 }
1314
1315 if (tq->tx_ts_desc_size != 0 &&
1316 adapter->latencyConf->sampleRate != 0) {
1317 if (vmxnet3_apply_timestamp(tq, adapter->latencyConf->sampleRate)) {
1318 ctx.ts_txd->ts.tsData = vmxnet3_get_cycles(VMXNET3_PMC_PSEUDO_TSC);
1319 ctx.ts_txd->ts.tsi = 1;
1320 }
1321 }
1322
1323 /* Ensure that the write to (&gdesc->txd)->gen will be observed after
1324 * all other writes to &gdesc->txd.
1325 */
1326 dma_wmb();
1327
1328 /* finally flips the GEN bit of the SOP desc. */
1329 gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
1330 VMXNET3_TXD_GEN);
1331 #ifdef __BIG_ENDIAN_BITFIELD
1332 /* Finished updating in bitfields of Tx Desc, so write them in original
1333 * place.
1334 */
1335 vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
1336 (struct Vmxnet3_TxDesc *)ctx.sop_txd);
1337 gdesc = ctx.sop_txd;
1338 #endif
1339 netdev_dbg(adapter->netdev,
1340 "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
1341 (u32)(ctx.sop_txd -
1342 tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
1343 le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
1344
1345 spin_unlock_irqrestore(&tq->tx_lock, flags);
1346
1347 if (tx_num_deferred >= le32_to_cpu(tq->shared->txThreshold)) {
1348 tq->shared->txNumDeferred = 0;
1349 VMXNET3_WRITE_BAR0_REG(adapter,
1350 adapter->tx_prod_offset + tq->qid * 8,
1351 tq->tx_ring.next2fill);
1352 }
1353
1354 return NETDEV_TX_OK;
1355
1356 unlock_drop_pkt:
1357 spin_unlock_irqrestore(&tq->tx_lock, flags);
1358 drop_pkt:
1359 tq->stats.drop_total++;
1360 dev_kfree_skb_any(skb);
1361 return NETDEV_TX_OK;
1362 }
1363
1364 static int
vmxnet3_create_pp(struct vmxnet3_adapter * adapter,struct vmxnet3_rx_queue * rq,int size)1365 vmxnet3_create_pp(struct vmxnet3_adapter *adapter,
1366 struct vmxnet3_rx_queue *rq, int size)
1367 {
1368 bool xdp_prog = vmxnet3_xdp_enabled(adapter);
1369 const struct page_pool_params pp_params = {
1370 .order = 0,
1371 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
1372 .pool_size = size,
1373 .nid = NUMA_NO_NODE,
1374 .dev = &adapter->pdev->dev,
1375 .offset = VMXNET3_XDP_RX_OFFSET,
1376 .max_len = VMXNET3_XDP_MAX_FRSIZE,
1377 .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
1378 };
1379 struct page_pool *pp;
1380 int err;
1381
1382 pp = page_pool_create(&pp_params);
1383 if (IS_ERR(pp))
1384 return PTR_ERR(pp);
1385
1386 err = xdp_rxq_info_reg(&rq->xdp_rxq, adapter->netdev, rq->qid,
1387 rq->napi.napi_id);
1388 if (err < 0)
1389 goto err_free_pp;
1390
1391 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq, MEM_TYPE_PAGE_POOL, pp);
1392 if (err)
1393 goto err_unregister_rxq;
1394
1395 rq->page_pool = pp;
1396
1397 return 0;
1398
1399 err_unregister_rxq:
1400 xdp_rxq_info_unreg(&rq->xdp_rxq);
1401 err_free_pp:
1402 page_pool_destroy(pp);
1403
1404 return err;
1405 }
1406
1407 void *
vmxnet3_pp_get_buff(struct page_pool * pp,dma_addr_t * dma_addr,gfp_t gfp_mask)1408 vmxnet3_pp_get_buff(struct page_pool *pp, dma_addr_t *dma_addr,
1409 gfp_t gfp_mask)
1410 {
1411 struct page *page;
1412
1413 page = page_pool_alloc_pages(pp, gfp_mask | __GFP_NOWARN);
1414 if (unlikely(!page))
1415 return NULL;
1416
1417 *dma_addr = page_pool_get_dma_addr(page) + pp->p.offset;
1418
1419 return page_address(page);
1420 }
1421
1422 static netdev_tx_t
vmxnet3_xmit_frame(struct sk_buff * skb,struct net_device * netdev)1423 vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1424 {
1425 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1426
1427 BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
1428 return vmxnet3_tq_xmit(skb,
1429 &adapter->tx_queue[skb->queue_mapping],
1430 adapter, netdev);
1431 }
1432
1433
1434 static void
vmxnet3_rx_csum(struct vmxnet3_adapter * adapter,struct sk_buff * skb,union Vmxnet3_GenericDesc * gdesc)1435 vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
1436 struct sk_buff *skb,
1437 union Vmxnet3_GenericDesc *gdesc)
1438 {
1439 if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
1440 if (gdesc->rcd.v4 &&
1441 (le32_to_cpu(gdesc->dword[3]) &
1442 VMXNET3_RCD_CSUM_OK) == VMXNET3_RCD_CSUM_OK) {
1443 skb->ip_summed = CHECKSUM_UNNECESSARY;
1444 if ((le32_to_cpu(gdesc->dword[0]) &
1445 (1UL << VMXNET3_RCD_HDR_INNER_SHIFT))) {
1446 skb->csum_level = 1;
1447 }
1448 WARN_ON_ONCE(!(gdesc->rcd.tcp || gdesc->rcd.udp) &&
1449 !(le32_to_cpu(gdesc->dword[0]) &
1450 (1UL << VMXNET3_RCD_HDR_INNER_SHIFT)));
1451 WARN_ON_ONCE(gdesc->rcd.frg &&
1452 !(le32_to_cpu(gdesc->dword[0]) &
1453 (1UL << VMXNET3_RCD_HDR_INNER_SHIFT)));
1454 } else if (gdesc->rcd.v6 && (le32_to_cpu(gdesc->dword[3]) &
1455 (1 << VMXNET3_RCD_TUC_SHIFT))) {
1456 skb->ip_summed = CHECKSUM_UNNECESSARY;
1457 if ((le32_to_cpu(gdesc->dword[0]) &
1458 (1UL << VMXNET3_RCD_HDR_INNER_SHIFT))) {
1459 skb->csum_level = 1;
1460 }
1461 WARN_ON_ONCE(!(gdesc->rcd.tcp || gdesc->rcd.udp) &&
1462 !(le32_to_cpu(gdesc->dword[0]) &
1463 (1UL << VMXNET3_RCD_HDR_INNER_SHIFT)));
1464 WARN_ON_ONCE(gdesc->rcd.frg &&
1465 !(le32_to_cpu(gdesc->dword[0]) &
1466 (1UL << VMXNET3_RCD_HDR_INNER_SHIFT)));
1467 } else {
1468 if (gdesc->rcd.csum) {
1469 skb->csum = htons(gdesc->rcd.csum);
1470 skb->ip_summed = CHECKSUM_PARTIAL;
1471 } else {
1472 skb_checksum_none_assert(skb);
1473 }
1474 }
1475 } else {
1476 skb_checksum_none_assert(skb);
1477 }
1478 }
1479
1480
1481 static void
vmxnet3_rx_error(struct vmxnet3_rx_queue * rq,struct Vmxnet3_RxCompDesc * rcd,struct vmxnet3_rx_ctx * ctx,struct vmxnet3_adapter * adapter)1482 vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
1483 struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
1484 {
1485 rq->stats.drop_err++;
1486 if (!rcd->fcs)
1487 rq->stats.drop_fcs++;
1488
1489 rq->stats.drop_total++;
1490
1491 /*
1492 * We do not unmap and chain the rx buffer to the skb.
1493 * We basically pretend this buffer is not used and will be recycled
1494 * by vmxnet3_rq_alloc_rx_buf()
1495 */
1496
1497 /*
1498 * ctx->skb may be NULL if this is the first and the only one
1499 * desc for the pkt
1500 */
1501 if (ctx->skb)
1502 dev_kfree_skb_irq(ctx->skb);
1503
1504 ctx->skb = NULL;
1505 }
1506
1507
1508 static u32
vmxnet3_get_hdr_len(struct vmxnet3_adapter * adapter,struct sk_buff * skb,union Vmxnet3_GenericDesc * gdesc)1509 vmxnet3_get_hdr_len(struct vmxnet3_adapter *adapter, struct sk_buff *skb,
1510 union Vmxnet3_GenericDesc *gdesc)
1511 {
1512 u32 hlen, maplen;
1513 union {
1514 void *ptr;
1515 struct ethhdr *eth;
1516 struct vlan_ethhdr *veth;
1517 struct iphdr *ipv4;
1518 struct ipv6hdr *ipv6;
1519 struct tcphdr *tcp;
1520 } hdr;
1521 BUG_ON(gdesc->rcd.tcp == 0);
1522
1523 maplen = skb_headlen(skb);
1524 if (unlikely(sizeof(struct iphdr) + sizeof(struct tcphdr) > maplen))
1525 return 0;
1526
1527 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
1528 skb->protocol == cpu_to_be16(ETH_P_8021AD))
1529 hlen = sizeof(struct vlan_ethhdr);
1530 else
1531 hlen = sizeof(struct ethhdr);
1532
1533 hdr.eth = eth_hdr(skb);
1534 if (gdesc->rcd.v4) {
1535 BUG_ON(hdr.eth->h_proto != htons(ETH_P_IP) &&
1536 hdr.veth->h_vlan_encapsulated_proto != htons(ETH_P_IP));
1537 hdr.ptr += hlen;
1538 BUG_ON(hdr.ipv4->protocol != IPPROTO_TCP);
1539 hlen = hdr.ipv4->ihl << 2;
1540 hdr.ptr += hdr.ipv4->ihl << 2;
1541 } else if (gdesc->rcd.v6) {
1542 BUG_ON(hdr.eth->h_proto != htons(ETH_P_IPV6) &&
1543 hdr.veth->h_vlan_encapsulated_proto != htons(ETH_P_IPV6));
1544 hdr.ptr += hlen;
1545 /* Use an estimated value, since we also need to handle
1546 * TSO case.
1547 */
1548 if (hdr.ipv6->nexthdr != IPPROTO_TCP)
1549 return sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1550 hlen = sizeof(struct ipv6hdr);
1551 hdr.ptr += sizeof(struct ipv6hdr);
1552 } else {
1553 /* Non-IP pkt, dont estimate header length */
1554 return 0;
1555 }
1556
1557 if (hlen + sizeof(struct tcphdr) > maplen)
1558 return 0;
1559
1560 return (hlen + (hdr.tcp->doff << 2));
1561 }
1562
1563 static void
vmxnet3_lro_tunnel(struct sk_buff * skb,__be16 ip_proto)1564 vmxnet3_lro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1565 {
1566 struct udphdr *uh = NULL;
1567
1568 if (ip_proto == htons(ETH_P_IP)) {
1569 struct iphdr *iph = (struct iphdr *)skb->data;
1570
1571 if (iph->protocol == IPPROTO_UDP)
1572 uh = (struct udphdr *)(iph + 1);
1573 } else {
1574 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1575
1576 if (iph->nexthdr == IPPROTO_UDP)
1577 uh = (struct udphdr *)(iph + 1);
1578 }
1579 if (uh) {
1580 if (uh->check)
1581 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1582 else
1583 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1584 }
1585 }
1586
1587 static int
vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue * rq,struct vmxnet3_adapter * adapter,int quota)1588 vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
1589 struct vmxnet3_adapter *adapter, int quota)
1590 {
1591 u32 rxprod_reg[2] = {
1592 adapter->rx_prod_offset, adapter->rx_prod2_offset
1593 };
1594 u32 num_pkts = 0;
1595 bool skip_page_frags = false;
1596 bool encap_lro = false;
1597 struct Vmxnet3_RxCompDesc *rcd;
1598 struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
1599 u16 segCnt = 0, mss = 0;
1600 int comp_offset, fill_offset;
1601 #ifdef __BIG_ENDIAN_BITFIELD
1602 struct Vmxnet3_RxDesc rxCmdDesc;
1603 struct Vmxnet3_RxCompDesc rxComp;
1604 #endif
1605 bool need_flush = false;
1606
1607 vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
1608 &rxComp);
1609 while (rcd->gen == rq->comp_ring.gen) {
1610 struct vmxnet3_rx_buf_info *rbi;
1611 struct sk_buff *skb, *new_skb = NULL;
1612 struct page *new_page = NULL;
1613 dma_addr_t new_dma_addr;
1614 int num_to_alloc;
1615 struct Vmxnet3_RxDesc *rxd;
1616 u32 idx, ring_idx;
1617 struct vmxnet3_cmd_ring *ring = NULL;
1618 if (num_pkts >= quota) {
1619 /* we may stop even before we see the EOP desc of
1620 * the current pkt
1621 */
1622 break;
1623 }
1624
1625 /* Prevent any rcd field from being (speculatively) read before
1626 * rcd->gen is read.
1627 */
1628 dma_rmb();
1629
1630 BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2 &&
1631 rcd->rqID != rq->dataRingQid);
1632 idx = rcd->rxdIdx;
1633 ring_idx = VMXNET3_GET_RING_IDX(adapter, rcd->rqID);
1634 ring = rq->rx_ring + ring_idx;
1635 vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
1636 &rxCmdDesc);
1637 rbi = rq->buf_info[ring_idx] + idx;
1638
1639 BUG_ON(rxd->addr != rbi->dma_addr ||
1640 rxd->len != rbi->len);
1641
1642 if (unlikely(rcd->eop && rcd->err)) {
1643 vmxnet3_rx_error(rq, rcd, ctx, adapter);
1644 goto rcd_done;
1645 }
1646
1647 if (rcd->sop && rcd->eop && vmxnet3_xdp_enabled(adapter)) {
1648 struct sk_buff *skb_xdp_pass;
1649 int act;
1650
1651 if (VMXNET3_RX_DATA_RING(adapter, rcd->rqID)) {
1652 ctx->skb = NULL;
1653 goto skip_xdp; /* Handle it later. */
1654 }
1655
1656 if (rbi->buf_type != VMXNET3_RX_BUF_XDP)
1657 goto rcd_done;
1658
1659 act = vmxnet3_process_xdp(adapter, rq, rcd, rbi, rxd,
1660 &skb_xdp_pass);
1661 if (act == XDP_PASS) {
1662 ctx->skb = skb_xdp_pass;
1663 goto sop_done;
1664 }
1665 ctx->skb = NULL;
1666 need_flush |= act == XDP_REDIRECT;
1667
1668 goto rcd_done;
1669 }
1670 skip_xdp:
1671
1672 if (rcd->sop) { /* first buf of the pkt */
1673 bool rxDataRingUsed;
1674 u16 len;
1675
1676 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
1677 (rcd->rqID != rq->qid &&
1678 rcd->rqID != rq->dataRingQid));
1679
1680 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB &&
1681 rbi->buf_type != VMXNET3_RX_BUF_XDP);
1682 BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
1683
1684 if (unlikely(rcd->len == 0)) {
1685 /* Pretend the rx buffer is skipped. */
1686 BUG_ON(!(rcd->sop && rcd->eop));
1687 netdev_dbg(adapter->netdev,
1688 "rxRing[%u][%u] 0 length\n",
1689 ring_idx, idx);
1690 goto rcd_done;
1691 }
1692
1693 skip_page_frags = false;
1694 ctx->skb = rbi->skb;
1695
1696 if (rq->rx_ts_desc_size != 0 && rcd->ext2) {
1697 struct Vmxnet3_RxTSDesc *ts_rxd;
1698
1699 ts_rxd = (struct Vmxnet3_RxTSDesc *)((u8 *)rq->ts_ring.base +
1700 idx * rq->rx_ts_desc_size);
1701 ts_rxd->ts.tsData = vmxnet3_get_cycles(VMXNET3_PMC_PSEUDO_TSC);
1702 ts_rxd->ts.tsi = 1;
1703 }
1704
1705 rxDataRingUsed =
1706 VMXNET3_RX_DATA_RING(adapter, rcd->rqID);
1707 len = rxDataRingUsed ? rcd->len : rbi->len;
1708
1709 if (rxDataRingUsed && vmxnet3_xdp_enabled(adapter)) {
1710 struct sk_buff *skb_xdp_pass;
1711 size_t sz;
1712 int act;
1713
1714 sz = rcd->rxdIdx * rq->data_ring.desc_size;
1715 act = vmxnet3_process_xdp_small(adapter, rq,
1716 &rq->data_ring.base[sz],
1717 rcd->len,
1718 &skb_xdp_pass);
1719 if (act == XDP_PASS) {
1720 ctx->skb = skb_xdp_pass;
1721 goto sop_done;
1722 }
1723 need_flush |= act == XDP_REDIRECT;
1724
1725 goto rcd_done;
1726 }
1727 new_skb = netdev_alloc_skb_ip_align(adapter->netdev,
1728 len);
1729 if (new_skb == NULL) {
1730 /* Skb allocation failed, do not handover this
1731 * skb to stack. Reuse it. Drop the existing pkt
1732 */
1733 rq->stats.rx_buf_alloc_failure++;
1734 ctx->skb = NULL;
1735 rq->stats.drop_total++;
1736 skip_page_frags = true;
1737 goto rcd_done;
1738 }
1739
1740 if (rxDataRingUsed && adapter->rxdataring_enabled) {
1741 size_t sz;
1742
1743 BUG_ON(rcd->len > rq->data_ring.desc_size);
1744
1745 ctx->skb = new_skb;
1746 sz = rcd->rxdIdx * rq->data_ring.desc_size;
1747 memcpy(new_skb->data,
1748 &rq->data_ring.base[sz], rcd->len);
1749 } else {
1750 ctx->skb = rbi->skb;
1751
1752 new_dma_addr =
1753 dma_map_single(&adapter->pdev->dev,
1754 new_skb->data, rbi->len,
1755 DMA_FROM_DEVICE);
1756 if (dma_mapping_error(&adapter->pdev->dev,
1757 new_dma_addr)) {
1758 dev_kfree_skb(new_skb);
1759 /* Skb allocation failed, do not
1760 * handover this skb to stack. Reuse
1761 * it. Drop the existing pkt.
1762 */
1763 rq->stats.rx_buf_alloc_failure++;
1764 ctx->skb = NULL;
1765 rq->stats.drop_total++;
1766 skip_page_frags = true;
1767 goto rcd_done;
1768 }
1769
1770 dma_unmap_single(&adapter->pdev->dev,
1771 rbi->dma_addr,
1772 rbi->len,
1773 DMA_FROM_DEVICE);
1774
1775 /* Immediate refill */
1776 rbi->skb = new_skb;
1777 rbi->dma_addr = new_dma_addr;
1778 rxd->addr = cpu_to_le64(rbi->dma_addr);
1779 rxd->len = rbi->len;
1780 }
1781
1782 skb_record_rx_queue(ctx->skb, rq->qid);
1783 skb_put(ctx->skb, rcd->len);
1784
1785 if (VMXNET3_VERSION_GE_2(adapter) &&
1786 rcd->type == VMXNET3_CDTYPE_RXCOMP_LRO) {
1787 struct Vmxnet3_RxCompDescExt *rcdlro;
1788 union Vmxnet3_GenericDesc *gdesc;
1789
1790 rcdlro = (struct Vmxnet3_RxCompDescExt *)rcd;
1791 gdesc = (union Vmxnet3_GenericDesc *)rcd;
1792
1793 segCnt = rcdlro->segCnt;
1794 WARN_ON_ONCE(segCnt == 0);
1795 mss = rcdlro->mss;
1796 if (unlikely(segCnt <= 1))
1797 segCnt = 0;
1798 encap_lro = (le32_to_cpu(gdesc->dword[0]) &
1799 (1UL << VMXNET3_RCD_HDR_INNER_SHIFT));
1800 } else {
1801 segCnt = 0;
1802 }
1803 } else {
1804 BUG_ON(ctx->skb == NULL && !skip_page_frags);
1805
1806 /* non SOP buffer must be type 1 in most cases */
1807 BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE);
1808 BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
1809
1810 /* If an sop buffer was dropped, skip all
1811 * following non-sop fragments. They will be reused.
1812 */
1813 if (skip_page_frags)
1814 goto rcd_done;
1815
1816 if (rcd->len) {
1817 new_page = alloc_page(GFP_ATOMIC);
1818 /* Replacement page frag could not be allocated.
1819 * Reuse this page. Drop the pkt and free the
1820 * skb which contained this page as a frag. Skip
1821 * processing all the following non-sop frags.
1822 */
1823 if (unlikely(!new_page)) {
1824 rq->stats.rx_buf_alloc_failure++;
1825 dev_kfree_skb(ctx->skb);
1826 ctx->skb = NULL;
1827 skip_page_frags = true;
1828 goto rcd_done;
1829 }
1830 new_dma_addr = dma_map_page(&adapter->pdev->dev,
1831 new_page,
1832 0, PAGE_SIZE,
1833 DMA_FROM_DEVICE);
1834 if (dma_mapping_error(&adapter->pdev->dev,
1835 new_dma_addr)) {
1836 put_page(new_page);
1837 rq->stats.rx_buf_alloc_failure++;
1838 dev_kfree_skb(ctx->skb);
1839 ctx->skb = NULL;
1840 skip_page_frags = true;
1841 goto rcd_done;
1842 }
1843
1844 dma_unmap_page(&adapter->pdev->dev,
1845 rbi->dma_addr, rbi->len,
1846 DMA_FROM_DEVICE);
1847
1848 vmxnet3_append_frag(ctx->skb, rcd, rbi);
1849
1850 /* Immediate refill */
1851 rbi->page = new_page;
1852 rbi->dma_addr = new_dma_addr;
1853 rxd->addr = cpu_to_le64(rbi->dma_addr);
1854 rxd->len = rbi->len;
1855 }
1856 }
1857
1858
1859 sop_done:
1860 skb = ctx->skb;
1861 if (rcd->eop) {
1862 u32 mtu = adapter->netdev->mtu;
1863 skb->len += skb->data_len;
1864
1865 #ifdef VMXNET3_RSS
1866 if (rcd->rssType != VMXNET3_RCD_RSS_TYPE_NONE &&
1867 (adapter->netdev->features & NETIF_F_RXHASH)) {
1868 enum pkt_hash_types hash_type;
1869
1870 switch (rcd->rssType) {
1871 case VMXNET3_RCD_RSS_TYPE_IPV4:
1872 case VMXNET3_RCD_RSS_TYPE_IPV6:
1873 hash_type = PKT_HASH_TYPE_L3;
1874 break;
1875 case VMXNET3_RCD_RSS_TYPE_TCPIPV4:
1876 case VMXNET3_RCD_RSS_TYPE_TCPIPV6:
1877 case VMXNET3_RCD_RSS_TYPE_UDPIPV4:
1878 case VMXNET3_RCD_RSS_TYPE_UDPIPV6:
1879 hash_type = PKT_HASH_TYPE_L4;
1880 break;
1881 default:
1882 hash_type = PKT_HASH_TYPE_L3;
1883 break;
1884 }
1885 skb_set_hash(skb,
1886 le32_to_cpu(rcd->rssHash),
1887 hash_type);
1888 }
1889 #endif
1890 vmxnet3_rx_csum(adapter, skb,
1891 (union Vmxnet3_GenericDesc *)rcd);
1892 skb->protocol = eth_type_trans(skb, adapter->netdev);
1893 if ((!rcd->tcp && !encap_lro) ||
1894 !(adapter->netdev->features & NETIF_F_LRO))
1895 goto not_lro;
1896
1897 if (segCnt != 0 && mss != 0) {
1898 skb_shinfo(skb)->gso_type = rcd->v4 ?
1899 SKB_GSO_TCPV4 : SKB_GSO_TCPV6;
1900 if (encap_lro)
1901 vmxnet3_lro_tunnel(skb, skb->protocol);
1902 skb_shinfo(skb)->gso_size = mss;
1903 skb_shinfo(skb)->gso_segs = segCnt;
1904 } else if ((segCnt != 0 || skb->len > mtu) && !encap_lro) {
1905 u32 hlen;
1906
1907 hlen = vmxnet3_get_hdr_len(adapter, skb,
1908 (union Vmxnet3_GenericDesc *)rcd);
1909 if (hlen == 0)
1910 goto not_lro;
1911
1912 skb_shinfo(skb)->gso_type =
1913 rcd->v4 ? SKB_GSO_TCPV4 : SKB_GSO_TCPV6;
1914 if (segCnt != 0) {
1915 skb_shinfo(skb)->gso_segs = segCnt;
1916 skb_shinfo(skb)->gso_size =
1917 DIV_ROUND_UP(skb->len -
1918 hlen, segCnt);
1919 } else {
1920 skb_shinfo(skb)->gso_size = mtu - hlen;
1921 }
1922 }
1923 not_lro:
1924 if (unlikely(rcd->ts))
1925 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), rcd->tci);
1926
1927 /* Use GRO callback if UPT is enabled */
1928 if ((adapter->netdev->features & NETIF_F_LRO) &&
1929 !rq->shared->updateRxProd)
1930 netif_receive_skb(skb);
1931 else
1932 napi_gro_receive(&rq->napi, skb);
1933
1934 ctx->skb = NULL;
1935 encap_lro = false;
1936 num_pkts++;
1937 }
1938
1939 rcd_done:
1940 /* device may have skipped some rx descs */
1941 ring = rq->rx_ring + ring_idx;
1942 rbi->comp_state = VMXNET3_RXD_COMP_DONE;
1943
1944 comp_offset = vmxnet3_cmd_ring_desc_avail(ring);
1945 fill_offset = (idx > ring->next2fill ? 0 : ring->size) +
1946 idx - ring->next2fill - 1;
1947 if (!ring->isOutOfOrder || fill_offset >= comp_offset)
1948 ring->next2comp = idx;
1949 num_to_alloc = vmxnet3_cmd_ring_desc_avail(ring);
1950
1951 /* Ensure that the writes to rxd->gen bits will be observed
1952 * after all other writes to rxd objects.
1953 */
1954 dma_wmb();
1955
1956 while (num_to_alloc) {
1957 rbi = rq->buf_info[ring_idx] + ring->next2fill;
1958 if (!(adapter->dev_caps[0] & (1UL << VMXNET3_CAP_OOORX_COMP)))
1959 goto refill_buf;
1960 if (ring_idx == 0) {
1961 /* ring0 Type1 buffers can get skipped; re-fill them */
1962 if (rbi->buf_type != VMXNET3_RX_BUF_SKB)
1963 goto refill_buf;
1964 }
1965 if (rbi->comp_state == VMXNET3_RXD_COMP_DONE) {
1966 refill_buf:
1967 vmxnet3_getRxDesc(rxd, &ring->base[ring->next2fill].rxd,
1968 &rxCmdDesc);
1969 WARN_ON(!rxd->addr);
1970
1971 /* Recv desc is ready to be used by the device */
1972 rxd->gen = ring->gen;
1973 vmxnet3_cmd_ring_adv_next2fill(ring);
1974 rbi->comp_state = VMXNET3_RXD_COMP_PENDING;
1975 num_to_alloc--;
1976 } else {
1977 /* rx completion hasn't occurred */
1978 ring->isOutOfOrder = 1;
1979 break;
1980 }
1981 }
1982
1983 if (num_to_alloc == 0) {
1984 ring->isOutOfOrder = 0;
1985 }
1986
1987 /* if needed, update the register */
1988 if (unlikely(rq->shared->updateRxProd) && (ring->next2fill & 0xf) == 0) {
1989 VMXNET3_WRITE_BAR0_REG(adapter,
1990 rxprod_reg[ring_idx] + rq->qid * 8,
1991 ring->next2fill);
1992 }
1993
1994 vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
1995 vmxnet3_getRxComp(rcd,
1996 &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
1997 }
1998 if (need_flush)
1999 xdp_do_flush();
2000
2001 return num_pkts;
2002 }
2003
2004
2005 static void
vmxnet3_rq_cleanup(struct vmxnet3_rx_queue * rq,struct vmxnet3_adapter * adapter)2006 vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
2007 struct vmxnet3_adapter *adapter)
2008 {
2009 u32 i, ring_idx;
2010 struct Vmxnet3_RxDesc *rxd;
2011
2012 /* ring has already been cleaned up */
2013 if (!rq->rx_ring[0].base)
2014 return;
2015
2016 for (ring_idx = 0; ring_idx < 2; ring_idx++) {
2017 for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
2018 struct vmxnet3_rx_buf_info *rbi;
2019 #ifdef __BIG_ENDIAN_BITFIELD
2020 struct Vmxnet3_RxDesc rxDesc;
2021 #endif
2022
2023 rbi = &rq->buf_info[ring_idx][i];
2024 vmxnet3_getRxDesc(rxd,
2025 &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
2026
2027 if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
2028 rbi->page && rbi->buf_type == VMXNET3_RX_BUF_XDP) {
2029 page_pool_recycle_direct(rq->page_pool,
2030 rbi->page);
2031 rbi->page = NULL;
2032 } else if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
2033 rbi->skb) {
2034 dma_unmap_single(&adapter->pdev->dev, rxd->addr,
2035 rxd->len, DMA_FROM_DEVICE);
2036 dev_kfree_skb(rbi->skb);
2037 rbi->skb = NULL;
2038 } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
2039 rbi->page) {
2040 dma_unmap_page(&adapter->pdev->dev, rxd->addr,
2041 rxd->len, DMA_FROM_DEVICE);
2042 put_page(rbi->page);
2043 rbi->page = NULL;
2044 }
2045 }
2046
2047 rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
2048 rq->rx_ring[ring_idx].next2fill =
2049 rq->rx_ring[ring_idx].next2comp = 0;
2050 }
2051
2052 rq->comp_ring.gen = VMXNET3_INIT_GEN;
2053 rq->comp_ring.next2proc = 0;
2054
2055 if (xdp_rxq_info_is_reg(&rq->xdp_rxq))
2056 xdp_rxq_info_unreg(&rq->xdp_rxq);
2057 page_pool_destroy(rq->page_pool);
2058 rq->page_pool = NULL;
2059 }
2060
2061
2062 static void
vmxnet3_rq_cleanup_all(struct vmxnet3_adapter * adapter)2063 vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
2064 {
2065 int i;
2066
2067 for (i = 0; i < adapter->num_rx_queues; i++)
2068 vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
2069 rcu_assign_pointer(adapter->xdp_bpf_prog, NULL);
2070 }
2071
2072
vmxnet3_rq_destroy(struct vmxnet3_rx_queue * rq,struct vmxnet3_adapter * adapter)2073 static void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
2074 struct vmxnet3_adapter *adapter)
2075 {
2076 int i;
2077 int j;
2078
2079 /* all rx buffers must have already been freed */
2080 for (i = 0; i < 2; i++) {
2081 if (rq->buf_info[i]) {
2082 for (j = 0; j < rq->rx_ring[i].size; j++)
2083 BUG_ON(rq->buf_info[i][j].page != NULL);
2084 }
2085 }
2086
2087
2088 for (i = 0; i < 2; i++) {
2089 if (rq->rx_ring[i].base) {
2090 dma_free_coherent(&adapter->pdev->dev,
2091 rq->rx_ring[i].size
2092 * sizeof(struct Vmxnet3_RxDesc),
2093 rq->rx_ring[i].base,
2094 rq->rx_ring[i].basePA);
2095 rq->rx_ring[i].base = NULL;
2096 }
2097 }
2098
2099 if (rq->data_ring.base) {
2100 dma_free_coherent(&adapter->pdev->dev,
2101 rq->rx_ring[0].size * rq->data_ring.desc_size,
2102 rq->data_ring.base, rq->data_ring.basePA);
2103 rq->data_ring.base = NULL;
2104 }
2105
2106 if (rq->ts_ring.base) {
2107 dma_free_coherent(&adapter->pdev->dev,
2108 rq->rx_ring[0].size * rq->rx_ts_desc_size,
2109 rq->ts_ring.base, rq->ts_ring.basePA);
2110 rq->ts_ring.base = NULL;
2111 }
2112
2113 if (rq->comp_ring.base) {
2114 dma_free_coherent(&adapter->pdev->dev, rq->comp_ring.size
2115 * sizeof(struct Vmxnet3_RxCompDesc),
2116 rq->comp_ring.base, rq->comp_ring.basePA);
2117 rq->comp_ring.base = NULL;
2118 }
2119
2120 kfree(rq->buf_info[0]);
2121 rq->buf_info[0] = NULL;
2122 rq->buf_info[1] = NULL;
2123 }
2124
2125 static void
vmxnet3_rq_destroy_all_rxdataring(struct vmxnet3_adapter * adapter)2126 vmxnet3_rq_destroy_all_rxdataring(struct vmxnet3_adapter *adapter)
2127 {
2128 int i;
2129
2130 for (i = 0; i < adapter->num_rx_queues; i++) {
2131 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2132
2133 if (rq->data_ring.base) {
2134 dma_free_coherent(&adapter->pdev->dev,
2135 (rq->rx_ring[0].size *
2136 rq->data_ring.desc_size),
2137 rq->data_ring.base,
2138 rq->data_ring.basePA);
2139 rq->data_ring.base = NULL;
2140 }
2141 rq->data_ring.desc_size = 0;
2142 }
2143 }
2144
2145 static int
vmxnet3_rq_init(struct vmxnet3_rx_queue * rq,struct vmxnet3_adapter * adapter)2146 vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
2147 struct vmxnet3_adapter *adapter)
2148 {
2149 int i, err;
2150
2151 /* initialize buf_info */
2152 for (i = 0; i < rq->rx_ring[0].size; i++) {
2153
2154 /* 1st buf for a pkt is skbuff or xdp page */
2155 if (i % adapter->rx_buf_per_pkt == 0) {
2156 rq->buf_info[0][i].buf_type = vmxnet3_xdp_enabled(adapter) ?
2157 VMXNET3_RX_BUF_XDP :
2158 VMXNET3_RX_BUF_SKB;
2159 rq->buf_info[0][i].len = adapter->skb_buf_size;
2160 } else { /* subsequent bufs for a pkt is frag */
2161 rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
2162 rq->buf_info[0][i].len = PAGE_SIZE;
2163 }
2164 }
2165 for (i = 0; i < rq->rx_ring[1].size; i++) {
2166 rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
2167 rq->buf_info[1][i].len = PAGE_SIZE;
2168 }
2169
2170 /* reset internal state and allocate buffers for both rings */
2171 for (i = 0; i < 2; i++) {
2172 rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
2173
2174 memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
2175 sizeof(struct Vmxnet3_RxDesc));
2176 rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
2177 rq->rx_ring[i].isOutOfOrder = 0;
2178 }
2179
2180 err = vmxnet3_create_pp(adapter, rq,
2181 rq->rx_ring[0].size + rq->rx_ring[1].size);
2182 if (err)
2183 return err;
2184
2185 if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
2186 adapter) == 0) {
2187 xdp_rxq_info_unreg(&rq->xdp_rxq);
2188 page_pool_destroy(rq->page_pool);
2189 rq->page_pool = NULL;
2190
2191 /* at least has 1 rx buffer for the 1st ring */
2192 return -ENOMEM;
2193 }
2194 vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
2195
2196 if (rq->ts_ring.base)
2197 memset(rq->ts_ring.base, 0,
2198 rq->rx_ring[0].size * rq->rx_ts_desc_size);
2199
2200 /* reset the comp ring */
2201 rq->comp_ring.next2proc = 0;
2202 memset(rq->comp_ring.base, 0, rq->comp_ring.size *
2203 sizeof(struct Vmxnet3_RxCompDesc));
2204 rq->comp_ring.gen = VMXNET3_INIT_GEN;
2205
2206 /* reset rxctx */
2207 rq->rx_ctx.skb = NULL;
2208
2209 /* stats are not reset */
2210 return 0;
2211 }
2212
2213
2214 static int
vmxnet3_rq_init_all(struct vmxnet3_adapter * adapter)2215 vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
2216 {
2217 int i, err = 0;
2218
2219 for (i = 0; i < adapter->num_rx_queues; i++) {
2220 err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
2221 if (unlikely(err)) {
2222 dev_err(&adapter->netdev->dev, "%s: failed to "
2223 "initialize rx queue%i\n",
2224 adapter->netdev->name, i);
2225 break;
2226 }
2227 }
2228 return err;
2229
2230 }
2231
2232
2233 static int
vmxnet3_rq_create(struct vmxnet3_rx_queue * rq,struct vmxnet3_adapter * adapter)2234 vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
2235 {
2236 int i;
2237 size_t sz;
2238 struct vmxnet3_rx_buf_info *bi;
2239
2240 for (i = 0; i < 2; i++) {
2241
2242 sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
2243 rq->rx_ring[i].base = dma_alloc_coherent(
2244 &adapter->pdev->dev, sz,
2245 &rq->rx_ring[i].basePA,
2246 GFP_KERNEL);
2247 if (!rq->rx_ring[i].base) {
2248 netdev_err(adapter->netdev,
2249 "failed to allocate rx ring %d\n", i);
2250 goto err;
2251 }
2252 }
2253
2254 if ((adapter->rxdataring_enabled) && (rq->data_ring.desc_size != 0)) {
2255 sz = rq->rx_ring[0].size * rq->data_ring.desc_size;
2256 rq->data_ring.base =
2257 dma_alloc_coherent(&adapter->pdev->dev, sz,
2258 &rq->data_ring.basePA,
2259 GFP_KERNEL);
2260 if (!rq->data_ring.base) {
2261 netdev_err(adapter->netdev,
2262 "rx data ring will be disabled\n");
2263 adapter->rxdataring_enabled = false;
2264 }
2265 } else {
2266 rq->data_ring.base = NULL;
2267 rq->data_ring.desc_size = 0;
2268 }
2269
2270 if (rq->rx_ts_desc_size != 0) {
2271 sz = rq->rx_ring[0].size * rq->rx_ts_desc_size;
2272 rq->ts_ring.base =
2273 dma_alloc_coherent(&adapter->pdev->dev, sz,
2274 &rq->ts_ring.basePA,
2275 GFP_KERNEL);
2276 if (!rq->ts_ring.base) {
2277 netdev_err(adapter->netdev,
2278 "rx ts ring will be disabled\n");
2279 rq->rx_ts_desc_size = 0;
2280 }
2281 } else {
2282 rq->ts_ring.base = NULL;
2283 }
2284
2285 sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
2286 rq->comp_ring.base = dma_alloc_coherent(&adapter->pdev->dev, sz,
2287 &rq->comp_ring.basePA,
2288 GFP_KERNEL);
2289 if (!rq->comp_ring.base) {
2290 netdev_err(adapter->netdev, "failed to allocate rx comp ring\n");
2291 goto err;
2292 }
2293
2294 bi = kcalloc_node(rq->rx_ring[0].size + rq->rx_ring[1].size,
2295 sizeof(rq->buf_info[0][0]), GFP_KERNEL,
2296 dev_to_node(&adapter->pdev->dev));
2297 if (!bi)
2298 goto err;
2299
2300 rq->buf_info[0] = bi;
2301 rq->buf_info[1] = bi + rq->rx_ring[0].size;
2302
2303 return 0;
2304
2305 err:
2306 vmxnet3_rq_destroy(rq, adapter);
2307 return -ENOMEM;
2308 }
2309
2310
2311 int
vmxnet3_rq_create_all(struct vmxnet3_adapter * adapter)2312 vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
2313 {
2314 int i, err = 0;
2315
2316 adapter->rxdataring_enabled = VMXNET3_VERSION_GE_3(adapter);
2317
2318 for (i = 0; i < adapter->num_rx_queues; i++) {
2319 err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
2320 if (unlikely(err)) {
2321 dev_err(&adapter->netdev->dev,
2322 "%s: failed to create rx queue%i\n",
2323 adapter->netdev->name, i);
2324 goto err_out;
2325 }
2326 }
2327
2328 if (!adapter->rxdataring_enabled)
2329 vmxnet3_rq_destroy_all_rxdataring(adapter);
2330
2331 return err;
2332 err_out:
2333 vmxnet3_rq_destroy_all(adapter);
2334 return err;
2335
2336 }
2337
2338 /* Multiple queue aware polling function for tx and rx */
2339
2340 static int
vmxnet3_do_poll(struct vmxnet3_adapter * adapter,int budget)2341 vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
2342 {
2343 int rcd_done = 0, i;
2344 if (unlikely(adapter->shared->ecr))
2345 vmxnet3_process_events(adapter);
2346 for (i = 0; i < adapter->num_tx_queues; i++)
2347 vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
2348
2349 for (i = 0; i < adapter->num_rx_queues; i++)
2350 rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
2351 adapter, budget);
2352 return rcd_done;
2353 }
2354
2355
2356 static int
vmxnet3_poll(struct napi_struct * napi,int budget)2357 vmxnet3_poll(struct napi_struct *napi, int budget)
2358 {
2359 struct vmxnet3_rx_queue *rx_queue = container_of(napi,
2360 struct vmxnet3_rx_queue, napi);
2361 int rxd_done;
2362
2363 rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
2364
2365 if (rxd_done < budget) {
2366 napi_complete_done(napi, rxd_done);
2367 vmxnet3_enable_all_intrs(rx_queue->adapter);
2368 }
2369 return rxd_done;
2370 }
2371
2372 /*
2373 * NAPI polling function for MSI-X mode with multiple Rx queues
2374 * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
2375 */
2376
2377 static int
vmxnet3_poll_rx_only(struct napi_struct * napi,int budget)2378 vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
2379 {
2380 struct vmxnet3_rx_queue *rq = container_of(napi,
2381 struct vmxnet3_rx_queue, napi);
2382 struct vmxnet3_adapter *adapter = rq->adapter;
2383 int rxd_done;
2384
2385 /* When sharing interrupt with corresponding tx queue, process
2386 * tx completions in that queue as well
2387 */
2388 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
2389 struct vmxnet3_tx_queue *tq =
2390 &adapter->tx_queue[rq - adapter->rx_queue];
2391 vmxnet3_tq_tx_complete(tq, adapter);
2392 }
2393
2394 rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
2395
2396 if (rxd_done < budget) {
2397 napi_complete_done(napi, rxd_done);
2398 vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
2399 }
2400 return rxd_done;
2401 }
2402
2403
2404 #ifdef CONFIG_PCI_MSI
2405
2406 /*
2407 * Handle completion interrupts on tx queues
2408 * Returns whether or not the intr is handled
2409 */
2410
2411 static irqreturn_t
vmxnet3_msix_tx(int irq,void * data)2412 vmxnet3_msix_tx(int irq, void *data)
2413 {
2414 struct vmxnet3_tx_queue *tq = data;
2415 struct vmxnet3_adapter *adapter = tq->adapter;
2416
2417 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
2418 vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
2419
2420 /* Handle the case where only one irq is allocate for all tx queues */
2421 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
2422 int i;
2423 for (i = 0; i < adapter->num_tx_queues; i++) {
2424 struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
2425 vmxnet3_tq_tx_complete(txq, adapter);
2426 }
2427 } else {
2428 vmxnet3_tq_tx_complete(tq, adapter);
2429 }
2430 vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
2431
2432 return IRQ_HANDLED;
2433 }
2434
2435
2436 /*
2437 * Handle completion interrupts on rx queues. Returns whether or not the
2438 * intr is handled
2439 */
2440
2441 static irqreturn_t
vmxnet3_msix_rx(int irq,void * data)2442 vmxnet3_msix_rx(int irq, void *data)
2443 {
2444 struct vmxnet3_rx_queue *rq = data;
2445 struct vmxnet3_adapter *adapter = rq->adapter;
2446
2447 /* disable intr if needed */
2448 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
2449 vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
2450 napi_schedule(&rq->napi);
2451
2452 return IRQ_HANDLED;
2453 }
2454
2455 /*
2456 *----------------------------------------------------------------------------
2457 *
2458 * vmxnet3_msix_event --
2459 *
2460 * vmxnet3 msix event intr handler
2461 *
2462 * Result:
2463 * whether or not the intr is handled
2464 *
2465 *----------------------------------------------------------------------------
2466 */
2467
2468 static irqreturn_t
vmxnet3_msix_event(int irq,void * data)2469 vmxnet3_msix_event(int irq, void *data)
2470 {
2471 struct net_device *dev = data;
2472 struct vmxnet3_adapter *adapter = netdev_priv(dev);
2473
2474 /* disable intr if needed */
2475 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
2476 vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
2477
2478 if (adapter->shared->ecr)
2479 vmxnet3_process_events(adapter);
2480
2481 vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
2482
2483 return IRQ_HANDLED;
2484 }
2485
2486 #endif /* CONFIG_PCI_MSI */
2487
2488
2489 /* Interrupt handler for vmxnet3 */
2490 static irqreturn_t
vmxnet3_intr(int irq,void * dev_id)2491 vmxnet3_intr(int irq, void *dev_id)
2492 {
2493 struct net_device *dev = dev_id;
2494 struct vmxnet3_adapter *adapter = netdev_priv(dev);
2495
2496 if (adapter->intr.type == VMXNET3_IT_INTX) {
2497 u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
2498 if (unlikely(icr == 0))
2499 /* not ours */
2500 return IRQ_NONE;
2501 }
2502
2503
2504 /* disable intr if needed */
2505 if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
2506 vmxnet3_disable_all_intrs(adapter);
2507
2508 napi_schedule(&adapter->rx_queue[0].napi);
2509
2510 return IRQ_HANDLED;
2511 }
2512
2513 #ifdef CONFIG_NET_POLL_CONTROLLER
2514
2515 /* netpoll callback. */
2516 static void
vmxnet3_netpoll(struct net_device * netdev)2517 vmxnet3_netpoll(struct net_device *netdev)
2518 {
2519 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2520
2521 switch (adapter->intr.type) {
2522 #ifdef CONFIG_PCI_MSI
2523 case VMXNET3_IT_MSIX: {
2524 int i;
2525 for (i = 0; i < adapter->num_rx_queues; i++)
2526 vmxnet3_msix_rx(0, &adapter->rx_queue[i]);
2527 break;
2528 }
2529 #endif
2530 case VMXNET3_IT_MSI:
2531 default:
2532 vmxnet3_intr(0, adapter->netdev);
2533 break;
2534 }
2535
2536 }
2537 #endif /* CONFIG_NET_POLL_CONTROLLER */
2538
2539 static int
vmxnet3_request_irqs(struct vmxnet3_adapter * adapter)2540 vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
2541 {
2542 struct vmxnet3_intr *intr = &adapter->intr;
2543 int err = 0, i;
2544 int vector = 0;
2545
2546 #ifdef CONFIG_PCI_MSI
2547 if (adapter->intr.type == VMXNET3_IT_MSIX) {
2548 for (i = 0; i < adapter->num_tx_queues; i++) {
2549 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
2550 sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
2551 adapter->netdev->name, vector);
2552 err = request_irq(
2553 intr->msix_entries[vector].vector,
2554 vmxnet3_msix_tx, 0,
2555 adapter->tx_queue[i].name,
2556 &adapter->tx_queue[i]);
2557 } else {
2558 sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
2559 adapter->netdev->name, vector);
2560 }
2561 if (err) {
2562 dev_err(&adapter->netdev->dev,
2563 "Failed to request irq for MSIX, %s, "
2564 "error %d\n",
2565 adapter->tx_queue[i].name, err);
2566 return err;
2567 }
2568
2569 /* Handle the case where only 1 MSIx was allocated for
2570 * all tx queues */
2571 if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
2572 for (; i < adapter->num_tx_queues; i++)
2573 adapter->tx_queue[i].comp_ring.intr_idx
2574 = vector;
2575 vector++;
2576 break;
2577 } else {
2578 adapter->tx_queue[i].comp_ring.intr_idx
2579 = vector++;
2580 }
2581 }
2582 if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
2583 vector = 0;
2584
2585 for (i = 0; i < adapter->num_rx_queues; i++) {
2586 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
2587 sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
2588 adapter->netdev->name, vector);
2589 else
2590 sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
2591 adapter->netdev->name, vector);
2592 err = request_irq(intr->msix_entries[vector].vector,
2593 vmxnet3_msix_rx, 0,
2594 adapter->rx_queue[i].name,
2595 &(adapter->rx_queue[i]));
2596 if (err) {
2597 netdev_err(adapter->netdev,
2598 "Failed to request irq for MSIX, "
2599 "%s, error %d\n",
2600 adapter->rx_queue[i].name, err);
2601 return err;
2602 }
2603
2604 adapter->rx_queue[i].comp_ring.intr_idx = vector++;
2605 }
2606
2607 sprintf(intr->event_msi_vector_name, "%s-event-%d",
2608 adapter->netdev->name, vector);
2609 err = request_irq(intr->msix_entries[vector].vector,
2610 vmxnet3_msix_event, 0,
2611 intr->event_msi_vector_name, adapter->netdev);
2612 intr->event_intr_idx = vector;
2613
2614 } else if (intr->type == VMXNET3_IT_MSI) {
2615 adapter->num_rx_queues = 1;
2616 err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
2617 adapter->netdev->name, adapter->netdev);
2618 } else {
2619 #endif
2620 adapter->num_rx_queues = 1;
2621 err = request_irq(adapter->pdev->irq, vmxnet3_intr,
2622 IRQF_SHARED, adapter->netdev->name,
2623 adapter->netdev);
2624 #ifdef CONFIG_PCI_MSI
2625 }
2626 #endif
2627 intr->num_intrs = vector + 1;
2628 if (err) {
2629 netdev_err(adapter->netdev,
2630 "Failed to request irq (intr type:%d), error %d\n",
2631 intr->type, err);
2632 } else {
2633 /* Number of rx queues will not change after this */
2634 for (i = 0; i < adapter->num_rx_queues; i++) {
2635 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2636 rq->qid = i;
2637 rq->qid2 = i + adapter->num_rx_queues;
2638 rq->dataRingQid = i + 2 * adapter->num_rx_queues;
2639 }
2640
2641 /* init our intr settings */
2642 for (i = 0; i < intr->num_intrs; i++)
2643 intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
2644 if (adapter->intr.type != VMXNET3_IT_MSIX) {
2645 adapter->intr.event_intr_idx = 0;
2646 for (i = 0; i < adapter->num_tx_queues; i++)
2647 adapter->tx_queue[i].comp_ring.intr_idx = 0;
2648 adapter->rx_queue[0].comp_ring.intr_idx = 0;
2649 }
2650
2651 netdev_info(adapter->netdev,
2652 "intr type %u, mode %u, %u vectors allocated\n",
2653 intr->type, intr->mask_mode, intr->num_intrs);
2654 }
2655
2656 return err;
2657 }
2658
2659
2660 static void
vmxnet3_free_irqs(struct vmxnet3_adapter * adapter)2661 vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
2662 {
2663 struct vmxnet3_intr *intr = &adapter->intr;
2664 BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
2665
2666 switch (intr->type) {
2667 #ifdef CONFIG_PCI_MSI
2668 case VMXNET3_IT_MSIX:
2669 {
2670 int i, vector = 0;
2671
2672 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
2673 for (i = 0; i < adapter->num_tx_queues; i++) {
2674 free_irq(intr->msix_entries[vector++].vector,
2675 &(adapter->tx_queue[i]));
2676 if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
2677 break;
2678 }
2679 }
2680
2681 for (i = 0; i < adapter->num_rx_queues; i++) {
2682 free_irq(intr->msix_entries[vector++].vector,
2683 &(adapter->rx_queue[i]));
2684 }
2685
2686 free_irq(intr->msix_entries[vector].vector,
2687 adapter->netdev);
2688 BUG_ON(vector >= intr->num_intrs);
2689 break;
2690 }
2691 #endif
2692 case VMXNET3_IT_MSI:
2693 free_irq(adapter->pdev->irq, adapter->netdev);
2694 break;
2695 case VMXNET3_IT_INTX:
2696 free_irq(adapter->pdev->irq, adapter->netdev);
2697 break;
2698 default:
2699 BUG();
2700 }
2701 }
2702
2703
2704 static void
vmxnet3_restore_vlan(struct vmxnet3_adapter * adapter)2705 vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
2706 {
2707 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2708 u16 vid;
2709
2710 /* allow untagged pkts */
2711 VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
2712
2713 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
2714 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
2715 }
2716
2717
2718 static int
vmxnet3_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)2719 vmxnet3_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
2720 {
2721 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2722
2723 if (!(netdev->flags & IFF_PROMISC)) {
2724 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2725 unsigned long flags;
2726
2727 VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
2728 spin_lock_irqsave(&adapter->cmd_lock, flags);
2729 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2730 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
2731 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2732 }
2733
2734 set_bit(vid, adapter->active_vlans);
2735
2736 return 0;
2737 }
2738
2739
2740 static int
vmxnet3_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)2741 vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
2742 {
2743 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2744
2745 if (!(netdev->flags & IFF_PROMISC)) {
2746 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2747 unsigned long flags;
2748
2749 VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
2750 spin_lock_irqsave(&adapter->cmd_lock, flags);
2751 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2752 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
2753 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2754 }
2755
2756 clear_bit(vid, adapter->active_vlans);
2757
2758 return 0;
2759 }
2760
2761
2762 static u8 *
vmxnet3_copy_mc(struct net_device * netdev)2763 vmxnet3_copy_mc(struct net_device *netdev)
2764 {
2765 u8 *buf = NULL;
2766 u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
2767
2768 /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
2769 if (sz <= 0xffff) {
2770 /* We may be called with BH disabled */
2771 buf = kmalloc(sz, GFP_ATOMIC);
2772 if (buf) {
2773 struct netdev_hw_addr *ha;
2774 int i = 0;
2775
2776 netdev_for_each_mc_addr(ha, netdev)
2777 memcpy(buf + i++ * ETH_ALEN, ha->addr,
2778 ETH_ALEN);
2779 }
2780 }
2781 return buf;
2782 }
2783
2784
2785 static void
vmxnet3_set_mc(struct net_device * netdev)2786 vmxnet3_set_mc(struct net_device *netdev)
2787 {
2788 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
2789 unsigned long flags;
2790 struct Vmxnet3_RxFilterConf *rxConf =
2791 &adapter->shared->devRead.rxFilterConf;
2792 u8 *new_table = NULL;
2793 dma_addr_t new_table_pa = 0;
2794 bool new_table_pa_valid = false;
2795 u32 new_mode = VMXNET3_RXM_UCAST;
2796
2797 if (netdev->flags & IFF_PROMISC) {
2798 u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
2799 memset(vfTable, 0, VMXNET3_VFT_SIZE * sizeof(*vfTable));
2800
2801 new_mode |= VMXNET3_RXM_PROMISC;
2802 } else {
2803 vmxnet3_restore_vlan(adapter);
2804 }
2805
2806 if (netdev->flags & IFF_BROADCAST)
2807 new_mode |= VMXNET3_RXM_BCAST;
2808
2809 if (netdev->flags & IFF_ALLMULTI)
2810 new_mode |= VMXNET3_RXM_ALL_MULTI;
2811 else
2812 if (!netdev_mc_empty(netdev)) {
2813 new_table = vmxnet3_copy_mc(netdev);
2814 if (new_table) {
2815 size_t sz = netdev_mc_count(netdev) * ETH_ALEN;
2816
2817 rxConf->mfTableLen = cpu_to_le16(sz);
2818 new_table_pa = dma_map_single(
2819 &adapter->pdev->dev,
2820 new_table,
2821 sz,
2822 DMA_TO_DEVICE);
2823 if (!dma_mapping_error(&adapter->pdev->dev,
2824 new_table_pa)) {
2825 new_mode |= VMXNET3_RXM_MCAST;
2826 new_table_pa_valid = true;
2827 rxConf->mfTablePA = cpu_to_le64(
2828 new_table_pa);
2829 }
2830 }
2831 if (!new_table_pa_valid) {
2832 netdev_info(netdev,
2833 "failed to copy mcast list, setting ALL_MULTI\n");
2834 new_mode |= VMXNET3_RXM_ALL_MULTI;
2835 }
2836 }
2837
2838 if (!(new_mode & VMXNET3_RXM_MCAST)) {
2839 rxConf->mfTableLen = 0;
2840 rxConf->mfTablePA = 0;
2841 }
2842
2843 spin_lock_irqsave(&adapter->cmd_lock, flags);
2844 if (new_mode != rxConf->rxMode) {
2845 rxConf->rxMode = cpu_to_le32(new_mode);
2846 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2847 VMXNET3_CMD_UPDATE_RX_MODE);
2848 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2849 VMXNET3_CMD_UPDATE_VLAN_FILTERS);
2850 }
2851
2852 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
2853 VMXNET3_CMD_UPDATE_MAC_FILTERS);
2854 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
2855
2856 if (new_table_pa_valid)
2857 dma_unmap_single(&adapter->pdev->dev, new_table_pa,
2858 rxConf->mfTableLen, DMA_TO_DEVICE);
2859 kfree(new_table);
2860 }
2861
2862 void
vmxnet3_rq_destroy_all(struct vmxnet3_adapter * adapter)2863 vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
2864 {
2865 int i;
2866
2867 for (i = 0; i < adapter->num_rx_queues; i++)
2868 vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
2869 }
2870
2871
2872 /*
2873 * Set up driver_shared based on settings in adapter.
2874 */
2875
2876 static void
vmxnet3_setup_driver_shared(struct vmxnet3_adapter * adapter)2877 vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
2878 {
2879 struct Vmxnet3_DriverShared *shared = adapter->shared;
2880 struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
2881 struct Vmxnet3_DSDevReadExt *devReadExt = &shared->devReadExt;
2882 struct Vmxnet3_TxQueueConf *tqc;
2883 struct Vmxnet3_RxQueueConf *rqc;
2884 struct Vmxnet3_TxQueueTSConf *tqtsc;
2885 struct Vmxnet3_RxQueueTSConf *rqtsc;
2886 int i;
2887
2888 memset(shared, 0, sizeof(*shared));
2889
2890 /* driver settings */
2891 shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
2892 devRead->misc.driverInfo.version = cpu_to_le32(
2893 VMXNET3_DRIVER_VERSION_NUM);
2894 devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
2895 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
2896 devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
2897 *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
2898 *((u32 *)&devRead->misc.driverInfo.gos));
2899 devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
2900 devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
2901
2902 devRead->misc.ddPA = cpu_to_le64(adapter->adapter_pa);
2903 devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
2904
2905 /* set up feature flags */
2906 if (adapter->netdev->features & NETIF_F_RXCSUM)
2907 devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
2908
2909 if (adapter->netdev->features & NETIF_F_LRO) {
2910 devRead->misc.uptFeatures |= UPT1_F_LRO;
2911 devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
2912 }
2913 if (adapter->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2914 devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
2915
2916 if (adapter->netdev->features & (NETIF_F_GSO_UDP_TUNNEL |
2917 NETIF_F_GSO_UDP_TUNNEL_CSUM))
2918 devRead->misc.uptFeatures |= UPT1_F_RXINNEROFLD;
2919
2920 devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
2921 devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
2922 devRead->misc.queueDescLen = cpu_to_le32(
2923 adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
2924 adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
2925
2926 /* tx queue settings */
2927 devRead->misc.numTxQueues = adapter->num_tx_queues;
2928 for (i = 0; i < adapter->num_tx_queues; i++) {
2929 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
2930 BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
2931 tqc = &adapter->tqd_start[i].conf;
2932 tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
2933 tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
2934 tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
2935 tqc->ddPA = cpu_to_le64(~0ULL);
2936 tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
2937 tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
2938 tqc->txDataRingDescSize = cpu_to_le32(tq->txdata_desc_size);
2939 tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
2940 tqc->ddLen = cpu_to_le32(0);
2941 tqc->intrIdx = tq->comp_ring.intr_idx;
2942 if (VMXNET3_VERSION_GE_9(adapter)) {
2943 tqtsc = &adapter->tqd_start[i].tsConf;
2944 tqtsc->txTSRingBasePA = cpu_to_le64(tq->ts_ring.basePA);
2945 tqtsc->txTSRingDescSize = cpu_to_le16(tq->tx_ts_desc_size);
2946 }
2947 }
2948
2949 /* rx queue settings */
2950 devRead->misc.numRxQueues = adapter->num_rx_queues;
2951 for (i = 0; i < adapter->num_rx_queues; i++) {
2952 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
2953 rqc = &adapter->rqd_start[i].conf;
2954 rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
2955 rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
2956 rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
2957 rqc->ddPA = cpu_to_le64(~0ULL);
2958 rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
2959 rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
2960 rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
2961 rqc->ddLen = cpu_to_le32(0);
2962 rqc->intrIdx = rq->comp_ring.intr_idx;
2963 if (VMXNET3_VERSION_GE_3(adapter)) {
2964 rqc->rxDataRingBasePA =
2965 cpu_to_le64(rq->data_ring.basePA);
2966 rqc->rxDataRingDescSize =
2967 cpu_to_le16(rq->data_ring.desc_size);
2968 }
2969 if (VMXNET3_VERSION_GE_9(adapter)) {
2970 rqtsc = &adapter->rqd_start[i].tsConf;
2971 rqtsc->rxTSRingBasePA = cpu_to_le64(rq->ts_ring.basePA);
2972 rqtsc->rxTSRingDescSize = cpu_to_le16(rq->rx_ts_desc_size);
2973 }
2974 }
2975
2976 #ifdef VMXNET3_RSS
2977 memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
2978
2979 if (adapter->rss) {
2980 struct UPT1_RSSConf *rssConf = adapter->rss_conf;
2981
2982 devRead->misc.uptFeatures |= UPT1_F_RSS;
2983 devRead->misc.numRxQueues = adapter->num_rx_queues;
2984 rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
2985 UPT1_RSS_HASH_TYPE_IPV4 |
2986 UPT1_RSS_HASH_TYPE_TCP_IPV6 |
2987 UPT1_RSS_HASH_TYPE_IPV6;
2988 rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
2989 rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
2990 rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
2991 netdev_rss_key_fill(rssConf->hashKey, sizeof(rssConf->hashKey));
2992
2993 for (i = 0; i < rssConf->indTableSize; i++)
2994 rssConf->indTable[i] = ethtool_rxfh_indir_default(
2995 i, adapter->num_rx_queues);
2996
2997 devRead->rssConfDesc.confVer = 1;
2998 devRead->rssConfDesc.confLen = cpu_to_le32(sizeof(*rssConf));
2999 devRead->rssConfDesc.confPA =
3000 cpu_to_le64(adapter->rss_conf_pa);
3001 }
3002
3003 #endif /* VMXNET3_RSS */
3004
3005 /* intr settings */
3006 if (!VMXNET3_VERSION_GE_6(adapter) ||
3007 !adapter->queuesExtEnabled) {
3008 devRead->intrConf.autoMask = adapter->intr.mask_mode ==
3009 VMXNET3_IMM_AUTO;
3010 devRead->intrConf.numIntrs = adapter->intr.num_intrs;
3011 for (i = 0; i < adapter->intr.num_intrs; i++)
3012 devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
3013
3014 devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
3015 devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
3016 } else {
3017 devReadExt->intrConfExt.autoMask = adapter->intr.mask_mode ==
3018 VMXNET3_IMM_AUTO;
3019 devReadExt->intrConfExt.numIntrs = adapter->intr.num_intrs;
3020 for (i = 0; i < adapter->intr.num_intrs; i++)
3021 devReadExt->intrConfExt.modLevels[i] = adapter->intr.mod_levels[i];
3022
3023 devReadExt->intrConfExt.eventIntrIdx = adapter->intr.event_intr_idx;
3024 devReadExt->intrConfExt.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
3025 }
3026
3027 /* rx filter settings */
3028 devRead->rxFilterConf.rxMode = 0;
3029 vmxnet3_restore_vlan(adapter);
3030 vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
3031
3032 /* the rest are already zeroed */
3033 }
3034
3035 static void
vmxnet3_init_bufsize(struct vmxnet3_adapter * adapter)3036 vmxnet3_init_bufsize(struct vmxnet3_adapter *adapter)
3037 {
3038 struct Vmxnet3_DriverShared *shared = adapter->shared;
3039 union Vmxnet3_CmdInfo *cmdInfo = &shared->cu.cmdInfo;
3040 unsigned long flags;
3041
3042 if (!VMXNET3_VERSION_GE_7(adapter))
3043 return;
3044
3045 cmdInfo->ringBufSize = adapter->ringBufSize;
3046 spin_lock_irqsave(&adapter->cmd_lock, flags);
3047 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3048 VMXNET3_CMD_SET_RING_BUFFER_SIZE);
3049 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3050 }
3051
3052 static void
vmxnet3_init_coalesce(struct vmxnet3_adapter * adapter)3053 vmxnet3_init_coalesce(struct vmxnet3_adapter *adapter)
3054 {
3055 struct Vmxnet3_DriverShared *shared = adapter->shared;
3056 union Vmxnet3_CmdInfo *cmdInfo = &shared->cu.cmdInfo;
3057 unsigned long flags;
3058
3059 if (!VMXNET3_VERSION_GE_3(adapter))
3060 return;
3061
3062 spin_lock_irqsave(&adapter->cmd_lock, flags);
3063 cmdInfo->varConf.confVer = 1;
3064 cmdInfo->varConf.confLen =
3065 cpu_to_le32(sizeof(*adapter->coal_conf));
3066 cmdInfo->varConf.confPA = cpu_to_le64(adapter->coal_conf_pa);
3067
3068 if (adapter->default_coal_mode) {
3069 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3070 VMXNET3_CMD_GET_COALESCE);
3071 } else {
3072 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3073 VMXNET3_CMD_SET_COALESCE);
3074 }
3075
3076 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3077 }
3078
3079 static void
vmxnet3_init_rssfields(struct vmxnet3_adapter * adapter)3080 vmxnet3_init_rssfields(struct vmxnet3_adapter *adapter)
3081 {
3082 struct Vmxnet3_DriverShared *shared = adapter->shared;
3083 union Vmxnet3_CmdInfo *cmdInfo = &shared->cu.cmdInfo;
3084 unsigned long flags;
3085
3086 if (!VMXNET3_VERSION_GE_4(adapter))
3087 return;
3088
3089 spin_lock_irqsave(&adapter->cmd_lock, flags);
3090
3091 if (adapter->default_rss_fields) {
3092 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3093 VMXNET3_CMD_GET_RSS_FIELDS);
3094 adapter->rss_fields =
3095 VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
3096 } else {
3097 if (VMXNET3_VERSION_GE_7(adapter)) {
3098 if ((adapter->rss_fields & VMXNET3_RSS_FIELDS_UDPIP4 ||
3099 adapter->rss_fields & VMXNET3_RSS_FIELDS_UDPIP6) &&
3100 vmxnet3_check_ptcapability(adapter->ptcap_supported[0],
3101 VMXNET3_CAP_UDP_RSS)) {
3102 adapter->dev_caps[0] |= 1UL << VMXNET3_CAP_UDP_RSS;
3103 } else {
3104 adapter->dev_caps[0] &= ~(1UL << VMXNET3_CAP_UDP_RSS);
3105 }
3106
3107 if ((adapter->rss_fields & VMXNET3_RSS_FIELDS_ESPIP4) &&
3108 vmxnet3_check_ptcapability(adapter->ptcap_supported[0],
3109 VMXNET3_CAP_ESP_RSS_IPV4)) {
3110 adapter->dev_caps[0] |= 1UL << VMXNET3_CAP_ESP_RSS_IPV4;
3111 } else {
3112 adapter->dev_caps[0] &= ~(1UL << VMXNET3_CAP_ESP_RSS_IPV4);
3113 }
3114
3115 if ((adapter->rss_fields & VMXNET3_RSS_FIELDS_ESPIP6) &&
3116 vmxnet3_check_ptcapability(adapter->ptcap_supported[0],
3117 VMXNET3_CAP_ESP_RSS_IPV6)) {
3118 adapter->dev_caps[0] |= 1UL << VMXNET3_CAP_ESP_RSS_IPV6;
3119 } else {
3120 adapter->dev_caps[0] &= ~(1UL << VMXNET3_CAP_ESP_RSS_IPV6);
3121 }
3122
3123 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DCR, adapter->dev_caps[0]);
3124 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_DCR0_REG);
3125 adapter->dev_caps[0] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
3126 }
3127 cmdInfo->setRssFields = adapter->rss_fields;
3128 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3129 VMXNET3_CMD_SET_RSS_FIELDS);
3130 /* Not all requested RSS may get applied, so get and
3131 * cache what was actually applied.
3132 */
3133 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3134 VMXNET3_CMD_GET_RSS_FIELDS);
3135 adapter->rss_fields =
3136 VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
3137 }
3138
3139 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3140 }
3141
3142 int
vmxnet3_activate_dev(struct vmxnet3_adapter * adapter)3143 vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
3144 {
3145 int err, i;
3146 u32 ret;
3147 unsigned long flags;
3148
3149 netdev_dbg(adapter->netdev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
3150 " ring sizes %u %u %u\n", adapter->netdev->name,
3151 adapter->skb_buf_size, adapter->rx_buf_per_pkt,
3152 adapter->tx_queue[0].tx_ring.size,
3153 adapter->rx_queue[0].rx_ring[0].size,
3154 adapter->rx_queue[0].rx_ring[1].size);
3155
3156 vmxnet3_tq_init_all(adapter);
3157 err = vmxnet3_rq_init_all(adapter);
3158 if (err) {
3159 netdev_err(adapter->netdev,
3160 "Failed to init rx queue error %d\n", err);
3161 goto rq_err;
3162 }
3163
3164 err = vmxnet3_request_irqs(adapter);
3165 if (err) {
3166 netdev_err(adapter->netdev,
3167 "Failed to setup irq for error %d\n", err);
3168 goto irq_err;
3169 }
3170
3171 vmxnet3_setup_driver_shared(adapter);
3172
3173 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
3174 adapter->shared_pa));
3175 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
3176 adapter->shared_pa));
3177 spin_lock_irqsave(&adapter->cmd_lock, flags);
3178 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3179 VMXNET3_CMD_ACTIVATE_DEV);
3180 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
3181 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3182
3183 if (ret != 0) {
3184 netdev_err(adapter->netdev,
3185 "Failed to activate dev: error %u\n", ret);
3186 err = -EINVAL;
3187 goto activate_err;
3188 }
3189
3190 vmxnet3_init_bufsize(adapter);
3191 vmxnet3_init_coalesce(adapter);
3192 vmxnet3_init_rssfields(adapter);
3193
3194 for (i = 0; i < adapter->num_rx_queues; i++) {
3195 VMXNET3_WRITE_BAR0_REG(adapter,
3196 adapter->rx_prod_offset + i * VMXNET3_REG_ALIGN,
3197 adapter->rx_queue[i].rx_ring[0].next2fill);
3198 VMXNET3_WRITE_BAR0_REG(adapter, (adapter->rx_prod2_offset +
3199 (i * VMXNET3_REG_ALIGN)),
3200 adapter->rx_queue[i].rx_ring[1].next2fill);
3201 }
3202
3203 /* Apply the rx filter settins last. */
3204 vmxnet3_set_mc(adapter->netdev);
3205
3206 /*
3207 * Check link state when first activating device. It will start the
3208 * tx queue if the link is up.
3209 */
3210 vmxnet3_check_link(adapter, true);
3211 netif_tx_wake_all_queues(adapter->netdev);
3212 for (i = 0; i < adapter->num_rx_queues; i++)
3213 napi_enable(&adapter->rx_queue[i].napi);
3214 vmxnet3_enable_all_intrs(adapter);
3215 clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
3216 return 0;
3217
3218 activate_err:
3219 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
3220 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
3221 vmxnet3_free_irqs(adapter);
3222 irq_err:
3223 rq_err:
3224 /* free up buffers we allocated */
3225 vmxnet3_rq_cleanup_all(adapter);
3226 return err;
3227 }
3228
3229
3230 void
vmxnet3_reset_dev(struct vmxnet3_adapter * adapter)3231 vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
3232 {
3233 unsigned long flags;
3234 spin_lock_irqsave(&adapter->cmd_lock, flags);
3235 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
3236 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3237 }
3238
3239
3240 int
vmxnet3_quiesce_dev(struct vmxnet3_adapter * adapter)3241 vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
3242 {
3243 int i;
3244 unsigned long flags;
3245 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
3246 return 0;
3247
3248
3249 spin_lock_irqsave(&adapter->cmd_lock, flags);
3250 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3251 VMXNET3_CMD_QUIESCE_DEV);
3252 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3253 vmxnet3_disable_all_intrs(adapter);
3254
3255 for (i = 0; i < adapter->num_rx_queues; i++)
3256 napi_disable(&adapter->rx_queue[i].napi);
3257 netif_tx_disable(adapter->netdev);
3258 adapter->link_speed = 0;
3259 netif_carrier_off(adapter->netdev);
3260
3261 vmxnet3_tq_cleanup_all(adapter);
3262 vmxnet3_rq_cleanup_all(adapter);
3263 vmxnet3_free_irqs(adapter);
3264 return 0;
3265 }
3266
3267
3268 static void
vmxnet3_write_mac_addr(struct vmxnet3_adapter * adapter,const u8 * mac)3269 vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, const u8 *mac)
3270 {
3271 u32 tmp;
3272
3273 tmp = *(u32 *)mac;
3274 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
3275
3276 tmp = (mac[5] << 8) | mac[4];
3277 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
3278 }
3279
3280
3281 static int
vmxnet3_set_mac_addr(struct net_device * netdev,void * p)3282 vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
3283 {
3284 struct sockaddr *addr = p;
3285 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3286
3287 dev_addr_set(netdev, addr->sa_data);
3288 vmxnet3_write_mac_addr(adapter, addr->sa_data);
3289
3290 return 0;
3291 }
3292
3293
3294 /* ==================== initialization and cleanup routines ============ */
3295
3296 static int
vmxnet3_alloc_pci_resources(struct vmxnet3_adapter * adapter)3297 vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter)
3298 {
3299 int err;
3300 unsigned long mmio_start, mmio_len;
3301 struct pci_dev *pdev = adapter->pdev;
3302
3303 err = pci_enable_device(pdev);
3304 if (err) {
3305 dev_err(&pdev->dev, "Failed to enable adapter: error %d\n", err);
3306 return err;
3307 }
3308
3309 err = pci_request_selected_regions(pdev, (1 << 2) - 1,
3310 vmxnet3_driver_name);
3311 if (err) {
3312 dev_err(&pdev->dev,
3313 "Failed to request region for adapter: error %d\n", err);
3314 goto err_enable_device;
3315 }
3316
3317 pci_set_master(pdev);
3318
3319 mmio_start = pci_resource_start(pdev, 0);
3320 mmio_len = pci_resource_len(pdev, 0);
3321 adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
3322 if (!adapter->hw_addr0) {
3323 dev_err(&pdev->dev, "Failed to map bar0\n");
3324 err = -EIO;
3325 goto err_ioremap;
3326 }
3327
3328 mmio_start = pci_resource_start(pdev, 1);
3329 mmio_len = pci_resource_len(pdev, 1);
3330 adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
3331 if (!adapter->hw_addr1) {
3332 dev_err(&pdev->dev, "Failed to map bar1\n");
3333 err = -EIO;
3334 goto err_bar1;
3335 }
3336 return 0;
3337
3338 err_bar1:
3339 iounmap(adapter->hw_addr0);
3340 err_ioremap:
3341 pci_release_selected_regions(pdev, (1 << 2) - 1);
3342 err_enable_device:
3343 pci_disable_device(pdev);
3344 return err;
3345 }
3346
3347
3348 static void
vmxnet3_free_pci_resources(struct vmxnet3_adapter * adapter)3349 vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
3350 {
3351 BUG_ON(!adapter->pdev);
3352
3353 iounmap(adapter->hw_addr0);
3354 iounmap(adapter->hw_addr1);
3355 pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
3356 pci_disable_device(adapter->pdev);
3357 }
3358
3359
3360 void
vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter * adapter)3361 vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
3362 {
3363 size_t sz, i, ring0_size, ring1_size, comp_size;
3364 /* With version7 ring1 will have only T0 buffers */
3365 if (!VMXNET3_VERSION_GE_7(adapter)) {
3366 if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
3367 VMXNET3_MAX_ETH_HDR_SIZE) {
3368 adapter->skb_buf_size = adapter->netdev->mtu +
3369 VMXNET3_MAX_ETH_HDR_SIZE;
3370 if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
3371 adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
3372
3373 adapter->rx_buf_per_pkt = 1;
3374 } else {
3375 adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
3376 sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
3377 VMXNET3_MAX_ETH_HDR_SIZE;
3378 adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
3379 }
3380 } else {
3381 adapter->skb_buf_size = min((int)adapter->netdev->mtu + VMXNET3_MAX_ETH_HDR_SIZE,
3382 VMXNET3_MAX_SKB_BUF_SIZE);
3383 adapter->rx_buf_per_pkt = 1;
3384 adapter->ringBufSize.ring1BufSizeType0 = cpu_to_le16(adapter->skb_buf_size);
3385 adapter->ringBufSize.ring1BufSizeType1 = 0;
3386 adapter->ringBufSize.ring2BufSizeType1 = cpu_to_le16(PAGE_SIZE);
3387 }
3388
3389 /*
3390 * for simplicity, force the ring0 size to be a multiple of
3391 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
3392 */
3393 sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
3394 ring0_size = adapter->rx_queue[0].rx_ring[0].size;
3395 ring0_size = (ring0_size + sz - 1) / sz * sz;
3396 ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
3397 sz * sz);
3398 ring1_size = adapter->rx_queue[0].rx_ring[1].size;
3399 ring1_size = (ring1_size + sz - 1) / sz * sz;
3400 ring1_size = min_t(u32, ring1_size, VMXNET3_RX_RING2_MAX_SIZE /
3401 sz * sz);
3402 /* For v7 and later, keep ring size power of 2 for UPT */
3403 if (VMXNET3_VERSION_GE_7(adapter)) {
3404 ring0_size = rounddown_pow_of_two(ring0_size);
3405 ring1_size = rounddown_pow_of_two(ring1_size);
3406 }
3407 comp_size = ring0_size + ring1_size;
3408
3409 for (i = 0; i < adapter->num_rx_queues; i++) {
3410 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
3411
3412 rq->rx_ring[0].size = ring0_size;
3413 rq->rx_ring[1].size = ring1_size;
3414 rq->comp_ring.size = comp_size;
3415 }
3416 }
3417
3418
3419 int
vmxnet3_create_queues(struct vmxnet3_adapter * adapter,u32 tx_ring_size,u32 rx_ring_size,u32 rx_ring2_size,u16 txdata_desc_size,u16 rxdata_desc_size)3420 vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
3421 u32 rx_ring_size, u32 rx_ring2_size,
3422 u16 txdata_desc_size, u16 rxdata_desc_size)
3423 {
3424 int err = 0, i;
3425
3426 for (i = 0; i < adapter->num_tx_queues; i++) {
3427 struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
3428 tq->tx_ring.size = tx_ring_size;
3429 tq->data_ring.size = tx_ring_size;
3430 tq->comp_ring.size = tx_ring_size;
3431 tq->txdata_desc_size = txdata_desc_size;
3432 tq->shared = &adapter->tqd_start[i].ctrl;
3433 tq->stopped = true;
3434 tq->adapter = adapter;
3435 tq->qid = i;
3436 tq->tx_ts_desc_size = adapter->tx_ts_desc_size;
3437 tq->tsPktCount = 1;
3438 err = vmxnet3_tq_create(tq, adapter);
3439 /*
3440 * Too late to change num_tx_queues. We cannot do away with
3441 * lesser number of queues than what we asked for
3442 */
3443 if (err)
3444 goto queue_err;
3445 }
3446
3447 adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
3448 adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
3449 vmxnet3_adjust_rx_ring_size(adapter);
3450
3451 adapter->rxdataring_enabled = VMXNET3_VERSION_GE_3(adapter);
3452 for (i = 0; i < adapter->num_rx_queues; i++) {
3453 struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
3454 /* qid and qid2 for rx queues will be assigned later when num
3455 * of rx queues is finalized after allocating intrs */
3456 rq->shared = &adapter->rqd_start[i].ctrl;
3457 rq->adapter = adapter;
3458 rq->data_ring.desc_size = rxdata_desc_size;
3459 rq->rx_ts_desc_size = adapter->rx_ts_desc_size;
3460 err = vmxnet3_rq_create(rq, adapter);
3461 if (err) {
3462 if (i == 0) {
3463 netdev_err(adapter->netdev,
3464 "Could not allocate any rx queues. "
3465 "Aborting.\n");
3466 goto queue_err;
3467 } else {
3468 netdev_info(adapter->netdev,
3469 "Number of rx queues changed "
3470 "to : %d.\n", i);
3471 adapter->num_rx_queues = i;
3472 err = 0;
3473 break;
3474 }
3475 }
3476 }
3477
3478 if (!adapter->rxdataring_enabled)
3479 vmxnet3_rq_destroy_all_rxdataring(adapter);
3480
3481 return err;
3482 queue_err:
3483 vmxnet3_tq_destroy_all(adapter);
3484 return err;
3485 }
3486
3487 static int
vmxnet3_open(struct net_device * netdev)3488 vmxnet3_open(struct net_device *netdev)
3489 {
3490 struct vmxnet3_adapter *adapter;
3491 int err, i;
3492
3493 adapter = netdev_priv(netdev);
3494
3495 for (i = 0; i < adapter->num_tx_queues; i++)
3496 spin_lock_init(&adapter->tx_queue[i].tx_lock);
3497
3498 if (VMXNET3_VERSION_GE_3(adapter)) {
3499 unsigned long flags;
3500 u16 txdata_desc_size;
3501 u32 ret;
3502
3503 spin_lock_irqsave(&adapter->cmd_lock, flags);
3504 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3505 VMXNET3_CMD_GET_TXDATA_DESC_SIZE);
3506 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
3507 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3508
3509 txdata_desc_size = ret & 0xffff;
3510 if ((txdata_desc_size < VMXNET3_TXDATA_DESC_MIN_SIZE) ||
3511 (txdata_desc_size > VMXNET3_TXDATA_DESC_MAX_SIZE) ||
3512 (txdata_desc_size & VMXNET3_TXDATA_DESC_SIZE_MASK)) {
3513 adapter->txdata_desc_size =
3514 sizeof(struct Vmxnet3_TxDataDesc);
3515 } else {
3516 adapter->txdata_desc_size = txdata_desc_size;
3517 }
3518 if (VMXNET3_VERSION_GE_9(adapter))
3519 adapter->rxdata_desc_size = (ret >> 16) & 0xffff;
3520 } else {
3521 adapter->txdata_desc_size = sizeof(struct Vmxnet3_TxDataDesc);
3522 }
3523
3524 if (VMXNET3_VERSION_GE_9(adapter)) {
3525 unsigned long flags;
3526 u16 tx_ts_desc_size = 0;
3527 u16 rx_ts_desc_size = 0;
3528 u32 ret;
3529
3530 spin_lock_irqsave(&adapter->cmd_lock, flags);
3531 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3532 VMXNET3_CMD_GET_TSRING_DESC_SIZE);
3533 ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
3534 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3535 if (ret > 0) {
3536 tx_ts_desc_size = (ret & 0xff);
3537 rx_ts_desc_size = ((ret >> 16) & 0xff);
3538 }
3539 if (tx_ts_desc_size > VMXNET3_TXTS_DESC_MAX_SIZE ||
3540 tx_ts_desc_size & VMXNET3_TXTS_DESC_SIZE_MASK)
3541 tx_ts_desc_size = 0;
3542 if (rx_ts_desc_size > VMXNET3_RXTS_DESC_MAX_SIZE ||
3543 rx_ts_desc_size & VMXNET3_RXTS_DESC_SIZE_MASK)
3544 rx_ts_desc_size = 0;
3545 adapter->tx_ts_desc_size = tx_ts_desc_size;
3546 adapter->rx_ts_desc_size = rx_ts_desc_size;
3547 } else {
3548 adapter->tx_ts_desc_size = 0;
3549 adapter->rx_ts_desc_size = 0;
3550 }
3551
3552 err = vmxnet3_create_queues(adapter,
3553 adapter->tx_ring_size,
3554 adapter->rx_ring_size,
3555 adapter->rx_ring2_size,
3556 adapter->txdata_desc_size,
3557 adapter->rxdata_desc_size);
3558 if (err)
3559 goto queue_err;
3560
3561 err = vmxnet3_activate_dev(adapter);
3562 if (err)
3563 goto activate_err;
3564
3565 return 0;
3566
3567 activate_err:
3568 vmxnet3_rq_destroy_all(adapter);
3569 vmxnet3_tq_destroy_all(adapter);
3570 queue_err:
3571 return err;
3572 }
3573
3574
3575 static int
vmxnet3_close(struct net_device * netdev)3576 vmxnet3_close(struct net_device *netdev)
3577 {
3578 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3579
3580 /*
3581 * Reset_work may be in the middle of resetting the device, wait for its
3582 * completion.
3583 */
3584 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
3585 usleep_range(1000, 2000);
3586
3587 vmxnet3_quiesce_dev(adapter);
3588
3589 vmxnet3_rq_destroy_all(adapter);
3590 vmxnet3_tq_destroy_all(adapter);
3591
3592 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
3593
3594
3595 return 0;
3596 }
3597
3598
3599 void
vmxnet3_force_close(struct vmxnet3_adapter * adapter)3600 vmxnet3_force_close(struct vmxnet3_adapter *adapter)
3601 {
3602 int i;
3603
3604 /*
3605 * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
3606 * vmxnet3_close() will deadlock.
3607 */
3608 BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
3609
3610 /* we need to enable NAPI, otherwise dev_close will deadlock */
3611 for (i = 0; i < adapter->num_rx_queues; i++)
3612 napi_enable(&adapter->rx_queue[i].napi);
3613 /*
3614 * Need to clear the quiesce bit to ensure that vmxnet3_close
3615 * can quiesce the device properly
3616 */
3617 clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
3618 dev_close(adapter->netdev);
3619 }
3620
3621
3622 static int
vmxnet3_change_mtu(struct net_device * netdev,int new_mtu)3623 vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
3624 {
3625 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3626 int err = 0;
3627
3628 /*
3629 * Reset_work may be in the middle of resetting the device, wait for its
3630 * completion.
3631 */
3632 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
3633 usleep_range(1000, 2000);
3634
3635 if (netif_running(netdev)) {
3636 vmxnet3_quiesce_dev(adapter);
3637 vmxnet3_reset_dev(adapter);
3638
3639 /* we need to re-create the rx queue based on the new mtu */
3640 vmxnet3_rq_destroy_all(adapter);
3641 WRITE_ONCE(netdev->mtu, new_mtu);
3642 vmxnet3_adjust_rx_ring_size(adapter);
3643 err = vmxnet3_rq_create_all(adapter);
3644 if (err) {
3645 netdev_err(netdev,
3646 "failed to re-create rx queues, "
3647 " error %d. Closing it.\n", err);
3648 goto out;
3649 }
3650
3651 err = vmxnet3_activate_dev(adapter);
3652 if (err) {
3653 netdev_err(netdev,
3654 "failed to re-activate, error %d. "
3655 "Closing it\n", err);
3656 goto out;
3657 }
3658 } else {
3659 WRITE_ONCE(netdev->mtu, new_mtu);
3660 }
3661
3662 out:
3663 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
3664 if (err)
3665 vmxnet3_force_close(adapter);
3666
3667 return err;
3668 }
3669
3670
3671 static void
vmxnet3_declare_features(struct vmxnet3_adapter * adapter)3672 vmxnet3_declare_features(struct vmxnet3_adapter *adapter)
3673 {
3674 struct net_device *netdev = adapter->netdev;
3675 unsigned long flags;
3676
3677 if (VMXNET3_VERSION_GE_9(adapter)) {
3678 spin_lock_irqsave(&adapter->cmd_lock, flags);
3679 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3680 VMXNET3_CMD_GET_DISABLED_OFFLOADS);
3681 adapter->disabledOffloads = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
3682 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3683 }
3684
3685 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
3686 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_CTAG_TX |
3687 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_TSO | NETIF_F_TSO6 |
3688 NETIF_F_LRO | NETIF_F_HIGHDMA;
3689
3690 if (VMXNET3_VERSION_GE_4(adapter)) {
3691 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3692 NETIF_F_GSO_UDP_TUNNEL_CSUM;
3693
3694 netdev->hw_enc_features = NETIF_F_SG | NETIF_F_RXCSUM |
3695 NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_CTAG_TX |
3696 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_TSO | NETIF_F_TSO6 |
3697 NETIF_F_LRO | NETIF_F_GSO_UDP_TUNNEL |
3698 NETIF_F_GSO_UDP_TUNNEL_CSUM;
3699 }
3700
3701 if (adapter->disabledOffloads & VMXNET3_OFFLOAD_TSO) {
3702 netdev->hw_features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
3703 netdev->hw_enc_features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
3704 }
3705
3706 if (adapter->disabledOffloads & VMXNET3_OFFLOAD_LRO) {
3707 netdev->hw_features &= ~(NETIF_F_LRO);
3708 netdev->hw_enc_features &= ~(NETIF_F_LRO);
3709 }
3710
3711 if (VMXNET3_VERSION_GE_7(adapter)) {
3712 unsigned long flags;
3713
3714 if (vmxnet3_check_ptcapability(adapter->ptcap_supported[0],
3715 VMXNET3_CAP_GENEVE_CHECKSUM_OFFLOAD)) {
3716 adapter->dev_caps[0] |= 1UL << VMXNET3_CAP_GENEVE_CHECKSUM_OFFLOAD;
3717 }
3718 if (vmxnet3_check_ptcapability(adapter->ptcap_supported[0],
3719 VMXNET3_CAP_VXLAN_CHECKSUM_OFFLOAD)) {
3720 adapter->dev_caps[0] |= 1UL << VMXNET3_CAP_VXLAN_CHECKSUM_OFFLOAD;
3721 }
3722 if (vmxnet3_check_ptcapability(adapter->ptcap_supported[0],
3723 VMXNET3_CAP_GENEVE_TSO)) {
3724 adapter->dev_caps[0] |= 1UL << VMXNET3_CAP_GENEVE_TSO;
3725 }
3726 if (vmxnet3_check_ptcapability(adapter->ptcap_supported[0],
3727 VMXNET3_CAP_VXLAN_TSO)) {
3728 adapter->dev_caps[0] |= 1UL << VMXNET3_CAP_VXLAN_TSO;
3729 }
3730 if (vmxnet3_check_ptcapability(adapter->ptcap_supported[0],
3731 VMXNET3_CAP_GENEVE_OUTER_CHECKSUM_OFFLOAD)) {
3732 adapter->dev_caps[0] |= 1UL << VMXNET3_CAP_GENEVE_OUTER_CHECKSUM_OFFLOAD;
3733 }
3734 if (vmxnet3_check_ptcapability(adapter->ptcap_supported[0],
3735 VMXNET3_CAP_VXLAN_OUTER_CHECKSUM_OFFLOAD)) {
3736 adapter->dev_caps[0] |= 1UL << VMXNET3_CAP_VXLAN_OUTER_CHECKSUM_OFFLOAD;
3737 }
3738
3739 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DCR, adapter->dev_caps[0]);
3740 spin_lock_irqsave(&adapter->cmd_lock, flags);
3741 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_DCR0_REG);
3742 adapter->dev_caps[0] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
3743 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3744
3745 if (!(adapter->dev_caps[0] & (1UL << VMXNET3_CAP_GENEVE_CHECKSUM_OFFLOAD)) &&
3746 !(adapter->dev_caps[0] & (1UL << VMXNET3_CAP_VXLAN_CHECKSUM_OFFLOAD)) &&
3747 !(adapter->dev_caps[0] & (1UL << VMXNET3_CAP_GENEVE_TSO)) &&
3748 !(adapter->dev_caps[0] & (1UL << VMXNET3_CAP_VXLAN_TSO))) {
3749 netdev->hw_enc_features &= ~NETIF_F_GSO_UDP_TUNNEL;
3750 netdev->hw_features &= ~NETIF_F_GSO_UDP_TUNNEL;
3751 }
3752 if (!(adapter->dev_caps[0] & (1UL << VMXNET3_CAP_GENEVE_OUTER_CHECKSUM_OFFLOAD)) &&
3753 !(adapter->dev_caps[0] & (1UL << VMXNET3_CAP_VXLAN_OUTER_CHECKSUM_OFFLOAD))) {
3754 netdev->hw_enc_features &= ~NETIF_F_GSO_UDP_TUNNEL_CSUM;
3755 netdev->hw_features &= ~NETIF_F_GSO_UDP_TUNNEL_CSUM;
3756 }
3757 }
3758
3759 netdev->vlan_features = netdev->hw_features &
3760 ~(NETIF_F_HW_VLAN_CTAG_TX |
3761 NETIF_F_HW_VLAN_CTAG_RX);
3762 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
3763 }
3764
3765
3766 static void
vmxnet3_read_mac_addr(struct vmxnet3_adapter * adapter,u8 * mac)3767 vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
3768 {
3769 u32 tmp;
3770
3771 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
3772 *(u32 *)mac = tmp;
3773
3774 tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
3775 mac[4] = tmp & 0xff;
3776 mac[5] = (tmp >> 8) & 0xff;
3777 }
3778
3779 #ifdef CONFIG_PCI_MSI
3780
3781 /*
3782 * Enable MSIx vectors.
3783 * Returns :
3784 * VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
3785 * were enabled.
3786 * number of vectors which were enabled otherwise (this number is greater
3787 * than VMXNET3_LINUX_MIN_MSIX_VECT)
3788 */
3789
3790 static int
vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter * adapter,int nvec)3791 vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter, int nvec)
3792 {
3793 int ret = pci_enable_msix_range(adapter->pdev,
3794 adapter->intr.msix_entries, nvec, nvec);
3795
3796 if (ret == -ENOSPC && nvec > VMXNET3_LINUX_MIN_MSIX_VECT) {
3797 dev_err(&adapter->netdev->dev,
3798 "Failed to enable %d MSI-X, trying %d\n",
3799 nvec, VMXNET3_LINUX_MIN_MSIX_VECT);
3800
3801 ret = pci_enable_msix_range(adapter->pdev,
3802 adapter->intr.msix_entries,
3803 VMXNET3_LINUX_MIN_MSIX_VECT,
3804 VMXNET3_LINUX_MIN_MSIX_VECT);
3805 }
3806
3807 if (ret < 0) {
3808 dev_err(&adapter->netdev->dev,
3809 "Failed to enable MSI-X, error: %d\n", ret);
3810 }
3811
3812 return ret;
3813 }
3814
3815
3816 #endif /* CONFIG_PCI_MSI */
3817
3818 static void
vmxnet3_alloc_intr_resources(struct vmxnet3_adapter * adapter)3819 vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
3820 {
3821 u32 cfg;
3822 unsigned long flags;
3823
3824 /* intr settings */
3825 spin_lock_irqsave(&adapter->cmd_lock, flags);
3826 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
3827 VMXNET3_CMD_GET_CONF_INTR);
3828 cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
3829 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
3830 adapter->intr.type = cfg & 0x3;
3831 adapter->intr.mask_mode = (cfg >> 2) & 0x3;
3832
3833 if (adapter->intr.type == VMXNET3_IT_AUTO) {
3834 adapter->intr.type = VMXNET3_IT_MSIX;
3835 }
3836
3837 #ifdef CONFIG_PCI_MSI
3838 if (adapter->intr.type == VMXNET3_IT_MSIX) {
3839 int i, nvec, nvec_allocated;
3840
3841 nvec = adapter->share_intr == VMXNET3_INTR_TXSHARE ?
3842 1 : adapter->num_tx_queues;
3843 nvec += adapter->share_intr == VMXNET3_INTR_BUDDYSHARE ?
3844 0 : adapter->num_rx_queues;
3845 nvec += 1; /* for link event */
3846 nvec = nvec > VMXNET3_LINUX_MIN_MSIX_VECT ?
3847 nvec : VMXNET3_LINUX_MIN_MSIX_VECT;
3848
3849 for (i = 0; i < nvec; i++)
3850 adapter->intr.msix_entries[i].entry = i;
3851
3852 nvec_allocated = vmxnet3_acquire_msix_vectors(adapter, nvec);
3853 if (nvec_allocated < 0)
3854 goto msix_err;
3855
3856 /* If we cannot allocate one MSIx vector per queue
3857 * then limit the number of rx queues to 1
3858 */
3859 if (nvec_allocated == VMXNET3_LINUX_MIN_MSIX_VECT &&
3860 nvec != VMXNET3_LINUX_MIN_MSIX_VECT) {
3861 if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
3862 || adapter->num_rx_queues != 1) {
3863 adapter->share_intr = VMXNET3_INTR_TXSHARE;
3864 netdev_err(adapter->netdev,
3865 "Number of rx queues : 1\n");
3866 adapter->num_rx_queues = 1;
3867 }
3868 }
3869
3870 adapter->intr.num_intrs = nvec_allocated;
3871 return;
3872
3873 msix_err:
3874 /* If we cannot allocate MSIx vectors use only one rx queue */
3875 dev_info(&adapter->pdev->dev,
3876 "Failed to enable MSI-X, error %d. "
3877 "Limiting #rx queues to 1, try MSI.\n", nvec_allocated);
3878
3879 adapter->intr.type = VMXNET3_IT_MSI;
3880 }
3881
3882 if (adapter->intr.type == VMXNET3_IT_MSI) {
3883 if (!pci_enable_msi(adapter->pdev)) {
3884 adapter->num_rx_queues = 1;
3885 adapter->intr.num_intrs = 1;
3886 return;
3887 }
3888 }
3889 #endif /* CONFIG_PCI_MSI */
3890
3891 adapter->num_rx_queues = 1;
3892 dev_info(&adapter->netdev->dev,
3893 "Using INTx interrupt, #Rx queues: 1.\n");
3894 adapter->intr.type = VMXNET3_IT_INTX;
3895
3896 /* INT-X related setting */
3897 adapter->intr.num_intrs = 1;
3898 }
3899
3900
3901 static void
vmxnet3_free_intr_resources(struct vmxnet3_adapter * adapter)3902 vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
3903 {
3904 if (adapter->intr.type == VMXNET3_IT_MSIX)
3905 pci_disable_msix(adapter->pdev);
3906 else if (adapter->intr.type == VMXNET3_IT_MSI)
3907 pci_disable_msi(adapter->pdev);
3908 else
3909 BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
3910 }
3911
3912
3913 static void
vmxnet3_tx_timeout(struct net_device * netdev,unsigned int txqueue)3914 vmxnet3_tx_timeout(struct net_device *netdev, unsigned int txqueue)
3915 {
3916 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
3917 adapter->tx_timeout_count++;
3918
3919 netdev_err(adapter->netdev, "tx hang\n");
3920 schedule_work(&adapter->work);
3921 }
3922
3923
3924 static void
vmxnet3_reset_work(struct work_struct * data)3925 vmxnet3_reset_work(struct work_struct *data)
3926 {
3927 struct vmxnet3_adapter *adapter;
3928
3929 adapter = container_of(data, struct vmxnet3_adapter, work);
3930
3931 /* if another thread is resetting the device, no need to proceed */
3932 if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
3933 return;
3934
3935 /* if the device is closed, we must leave it alone */
3936 rtnl_lock();
3937 if (netif_running(adapter->netdev)) {
3938 netdev_notice(adapter->netdev, "resetting\n");
3939 vmxnet3_quiesce_dev(adapter);
3940 vmxnet3_reset_dev(adapter);
3941 vmxnet3_activate_dev(adapter);
3942 } else {
3943 netdev_info(adapter->netdev, "already closed\n");
3944 }
3945 rtnl_unlock();
3946
3947 netif_wake_queue(adapter->netdev);
3948 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
3949 }
3950
3951
3952 static int
vmxnet3_probe_device(struct pci_dev * pdev,const struct pci_device_id * id)3953 vmxnet3_probe_device(struct pci_dev *pdev,
3954 const struct pci_device_id *id)
3955 {
3956 static const struct net_device_ops vmxnet3_netdev_ops = {
3957 .ndo_open = vmxnet3_open,
3958 .ndo_stop = vmxnet3_close,
3959 .ndo_start_xmit = vmxnet3_xmit_frame,
3960 .ndo_set_mac_address = vmxnet3_set_mac_addr,
3961 .ndo_change_mtu = vmxnet3_change_mtu,
3962 .ndo_fix_features = vmxnet3_fix_features,
3963 .ndo_set_features = vmxnet3_set_features,
3964 .ndo_features_check = vmxnet3_features_check,
3965 .ndo_get_stats64 = vmxnet3_get_stats64,
3966 .ndo_tx_timeout = vmxnet3_tx_timeout,
3967 .ndo_set_rx_mode = vmxnet3_set_mc,
3968 .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
3969 .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
3970 #ifdef CONFIG_NET_POLL_CONTROLLER
3971 .ndo_poll_controller = vmxnet3_netpoll,
3972 #endif
3973 .ndo_bpf = vmxnet3_xdp,
3974 .ndo_xdp_xmit = vmxnet3_xdp_xmit,
3975 };
3976 int err;
3977 u32 ver;
3978 struct net_device *netdev;
3979 struct vmxnet3_adapter *adapter;
3980 u8 mac[ETH_ALEN];
3981 int size, i;
3982 int num_tx_queues;
3983 int num_rx_queues;
3984 int queues;
3985 unsigned long flags;
3986
3987 if (!pci_msi_enabled())
3988 enable_mq = 0;
3989
3990 #ifdef VMXNET3_RSS
3991 if (enable_mq)
3992 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
3993 (int)num_online_cpus());
3994 else
3995 #endif
3996 num_rx_queues = 1;
3997
3998 if (enable_mq)
3999 num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
4000 (int)num_online_cpus());
4001 else
4002 num_tx_queues = 1;
4003
4004 netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
4005 max(num_tx_queues, num_rx_queues));
4006 if (!netdev)
4007 return -ENOMEM;
4008
4009 pci_set_drvdata(pdev, netdev);
4010 adapter = netdev_priv(netdev);
4011 adapter->netdev = netdev;
4012 adapter->pdev = pdev;
4013
4014 adapter->tx_ring_size = VMXNET3_DEF_TX_RING_SIZE;
4015 adapter->rx_ring_size = VMXNET3_DEF_RX_RING_SIZE;
4016 adapter->rx_ring2_size = VMXNET3_DEF_RX_RING2_SIZE;
4017
4018 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
4019 if (err) {
4020 dev_err(&pdev->dev, "dma_set_mask failed\n");
4021 goto err_set_mask;
4022 }
4023
4024 spin_lock_init(&adapter->cmd_lock);
4025 adapter->adapter_pa = dma_map_single(&adapter->pdev->dev, adapter,
4026 sizeof(struct vmxnet3_adapter),
4027 DMA_TO_DEVICE);
4028 if (dma_mapping_error(&adapter->pdev->dev, adapter->adapter_pa)) {
4029 dev_err(&pdev->dev, "Failed to map dma\n");
4030 err = -EFAULT;
4031 goto err_set_mask;
4032 }
4033 adapter->shared = dma_alloc_coherent(
4034 &adapter->pdev->dev,
4035 sizeof(struct Vmxnet3_DriverShared),
4036 &adapter->shared_pa, GFP_KERNEL);
4037 if (!adapter->shared) {
4038 dev_err(&pdev->dev, "Failed to allocate memory\n");
4039 err = -ENOMEM;
4040 goto err_alloc_shared;
4041 }
4042
4043 err = vmxnet3_alloc_pci_resources(adapter);
4044 if (err < 0)
4045 goto err_alloc_pci;
4046
4047 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
4048 for (i = VMXNET3_REV_9; i >= VMXNET3_REV_1; i--) {
4049 if (ver & (1 << i)) {
4050 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1 << i);
4051 adapter->version = i + 1;
4052 break;
4053 }
4054 }
4055 if (i < VMXNET3_REV_1) {
4056 dev_err(&pdev->dev,
4057 "Incompatible h/w version (0x%x) for adapter\n", ver);
4058 err = -EBUSY;
4059 goto err_ver;
4060 }
4061 dev_dbg(&pdev->dev, "Using device version %d\n", adapter->version);
4062
4063 ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
4064 if (ver & 1) {
4065 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
4066 } else {
4067 dev_err(&pdev->dev,
4068 "Incompatible upt version (0x%x) for adapter\n", ver);
4069 err = -EBUSY;
4070 goto err_ver;
4071 }
4072
4073 if (VMXNET3_VERSION_GE_7(adapter)) {
4074 adapter->devcap_supported[0] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_DCR);
4075 adapter->ptcap_supported[0] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_PTCR);
4076 if (adapter->devcap_supported[0] & (1UL << VMXNET3_CAP_LARGE_BAR)) {
4077 adapter->dev_caps[0] = adapter->devcap_supported[0] &
4078 (1UL << VMXNET3_CAP_LARGE_BAR);
4079 }
4080 if (!(adapter->ptcap_supported[0] & (1UL << VMXNET3_DCR_ERROR)) &&
4081 adapter->ptcap_supported[0] & (1UL << VMXNET3_CAP_OOORX_COMP) &&
4082 adapter->devcap_supported[0] & (1UL << VMXNET3_CAP_OOORX_COMP)) {
4083 adapter->dev_caps[0] |= adapter->devcap_supported[0] &
4084 (1UL << VMXNET3_CAP_OOORX_COMP);
4085 }
4086 if (adapter->dev_caps[0])
4087 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DCR, adapter->dev_caps[0]);
4088
4089 spin_lock_irqsave(&adapter->cmd_lock, flags);
4090 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_DCR0_REG);
4091 adapter->dev_caps[0] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
4092 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
4093 }
4094
4095 if (VMXNET3_VERSION_GE_7(adapter) &&
4096 adapter->dev_caps[0] & (1UL << VMXNET3_CAP_LARGE_BAR)) {
4097 adapter->tx_prod_offset = VMXNET3_REG_LB_TXPROD;
4098 adapter->rx_prod_offset = VMXNET3_REG_LB_RXPROD;
4099 adapter->rx_prod2_offset = VMXNET3_REG_LB_RXPROD2;
4100 } else {
4101 adapter->tx_prod_offset = VMXNET3_REG_TXPROD;
4102 adapter->rx_prod_offset = VMXNET3_REG_RXPROD;
4103 adapter->rx_prod2_offset = VMXNET3_REG_RXPROD2;
4104 }
4105
4106 if (VMXNET3_VERSION_GE_6(adapter)) {
4107 spin_lock_irqsave(&adapter->cmd_lock, flags);
4108 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
4109 VMXNET3_CMD_GET_MAX_QUEUES_CONF);
4110 queues = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
4111 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
4112 if (queues > 0) {
4113 adapter->num_rx_queues = min(num_rx_queues, ((queues >> 8) & 0xff));
4114 adapter->num_tx_queues = min(num_tx_queues, (queues & 0xff));
4115 } else {
4116 adapter->num_rx_queues = min(num_rx_queues,
4117 VMXNET3_DEVICE_DEFAULT_RX_QUEUES);
4118 adapter->num_tx_queues = min(num_tx_queues,
4119 VMXNET3_DEVICE_DEFAULT_TX_QUEUES);
4120 }
4121 if (adapter->num_rx_queues > VMXNET3_MAX_RX_QUEUES ||
4122 adapter->num_tx_queues > VMXNET3_MAX_TX_QUEUES) {
4123 adapter->queuesExtEnabled = true;
4124 } else {
4125 adapter->queuesExtEnabled = false;
4126 }
4127 } else {
4128 adapter->queuesExtEnabled = false;
4129 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
4130 num_tx_queues = rounddown_pow_of_two(num_tx_queues);
4131 adapter->num_rx_queues = min(num_rx_queues,
4132 VMXNET3_DEVICE_DEFAULT_RX_QUEUES);
4133 adapter->num_tx_queues = min(num_tx_queues,
4134 VMXNET3_DEVICE_DEFAULT_TX_QUEUES);
4135 }
4136 dev_info(&pdev->dev,
4137 "# of Tx queues : %d, # of Rx queues : %d\n",
4138 adapter->num_tx_queues, adapter->num_rx_queues);
4139
4140 adapter->rx_buf_per_pkt = 1;
4141
4142 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
4143 size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
4144 adapter->tqd_start = dma_alloc_coherent(&adapter->pdev->dev, size,
4145 &adapter->queue_desc_pa,
4146 GFP_KERNEL);
4147
4148 if (!adapter->tqd_start) {
4149 dev_err(&pdev->dev, "Failed to allocate memory\n");
4150 err = -ENOMEM;
4151 goto err_ver;
4152 }
4153 adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
4154 adapter->num_tx_queues);
4155 if (VMXNET3_VERSION_GE_9(adapter))
4156 adapter->latencyConf = &adapter->tqd_start->tsConf.latencyConf;
4157
4158 adapter->pm_conf = dma_alloc_coherent(&adapter->pdev->dev,
4159 sizeof(struct Vmxnet3_PMConf),
4160 &adapter->pm_conf_pa,
4161 GFP_KERNEL);
4162 if (adapter->pm_conf == NULL) {
4163 err = -ENOMEM;
4164 goto err_alloc_pm;
4165 }
4166
4167 #ifdef VMXNET3_RSS
4168
4169 adapter->rss_conf = dma_alloc_coherent(&adapter->pdev->dev,
4170 sizeof(struct UPT1_RSSConf),
4171 &adapter->rss_conf_pa,
4172 GFP_KERNEL);
4173 if (adapter->rss_conf == NULL) {
4174 err = -ENOMEM;
4175 goto err_alloc_rss;
4176 }
4177 #endif /* VMXNET3_RSS */
4178
4179 if (VMXNET3_VERSION_GE_3(adapter)) {
4180 adapter->coal_conf =
4181 dma_alloc_coherent(&adapter->pdev->dev,
4182 sizeof(struct Vmxnet3_CoalesceScheme)
4183 ,
4184 &adapter->coal_conf_pa,
4185 GFP_KERNEL);
4186 if (!adapter->coal_conf) {
4187 err = -ENOMEM;
4188 goto err_coal_conf;
4189 }
4190 adapter->coal_conf->coalMode = VMXNET3_COALESCE_DISABLED;
4191 adapter->default_coal_mode = true;
4192 }
4193
4194 if (VMXNET3_VERSION_GE_4(adapter)) {
4195 adapter->default_rss_fields = true;
4196 adapter->rss_fields = VMXNET3_RSS_FIELDS_DEFAULT;
4197 }
4198
4199 SET_NETDEV_DEV(netdev, &pdev->dev);
4200 vmxnet3_declare_features(adapter);
4201 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
4202 NETDEV_XDP_ACT_NDO_XMIT;
4203
4204 adapter->rxdata_desc_size = VMXNET3_VERSION_GE_3(adapter) ?
4205 VMXNET3_DEF_RXDATA_DESC_SIZE : 0;
4206
4207 if (adapter->num_tx_queues == adapter->num_rx_queues)
4208 adapter->share_intr = VMXNET3_INTR_BUDDYSHARE;
4209 else
4210 adapter->share_intr = VMXNET3_INTR_DONTSHARE;
4211
4212 vmxnet3_alloc_intr_resources(adapter);
4213
4214 #ifdef VMXNET3_RSS
4215 if (adapter->num_rx_queues > 1 &&
4216 adapter->intr.type == VMXNET3_IT_MSIX) {
4217 adapter->rss = true;
4218 netdev->hw_features |= NETIF_F_RXHASH;
4219 netdev->features |= NETIF_F_RXHASH;
4220 dev_dbg(&pdev->dev, "RSS is enabled.\n");
4221 } else {
4222 adapter->rss = false;
4223 }
4224 #endif
4225
4226 vmxnet3_read_mac_addr(adapter, mac);
4227 dev_addr_set(netdev, mac);
4228
4229 netdev->netdev_ops = &vmxnet3_netdev_ops;
4230 vmxnet3_set_ethtool_ops(netdev);
4231 netdev->watchdog_timeo = 5 * HZ;
4232
4233 /* MTU range: 60 - 9190 */
4234 netdev->min_mtu = VMXNET3_MIN_MTU;
4235 if (VMXNET3_VERSION_GE_6(adapter))
4236 netdev->max_mtu = VMXNET3_V6_MAX_MTU;
4237 else
4238 netdev->max_mtu = VMXNET3_MAX_MTU;
4239
4240 INIT_WORK(&adapter->work, vmxnet3_reset_work);
4241 set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
4242
4243 if (adapter->intr.type == VMXNET3_IT_MSIX) {
4244 int i;
4245 for (i = 0; i < adapter->num_rx_queues; i++) {
4246 netif_napi_add(adapter->netdev,
4247 &adapter->rx_queue[i].napi,
4248 vmxnet3_poll_rx_only);
4249 }
4250 } else {
4251 netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
4252 vmxnet3_poll);
4253 }
4254
4255 netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
4256 netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
4257
4258 netif_carrier_off(netdev);
4259 err = register_netdev(netdev);
4260
4261 if (err) {
4262 dev_err(&pdev->dev, "Failed to register adapter\n");
4263 goto err_register;
4264 }
4265
4266 vmxnet3_check_link(adapter, false);
4267 return 0;
4268
4269 err_register:
4270 if (VMXNET3_VERSION_GE_3(adapter)) {
4271 dma_free_coherent(&adapter->pdev->dev,
4272 sizeof(struct Vmxnet3_CoalesceScheme),
4273 adapter->coal_conf, adapter->coal_conf_pa);
4274 }
4275 vmxnet3_free_intr_resources(adapter);
4276 err_coal_conf:
4277 #ifdef VMXNET3_RSS
4278 dma_free_coherent(&adapter->pdev->dev, sizeof(struct UPT1_RSSConf),
4279 adapter->rss_conf, adapter->rss_conf_pa);
4280 err_alloc_rss:
4281 #endif
4282 dma_free_coherent(&adapter->pdev->dev, sizeof(struct Vmxnet3_PMConf),
4283 adapter->pm_conf, adapter->pm_conf_pa);
4284 err_alloc_pm:
4285 dma_free_coherent(&adapter->pdev->dev, size, adapter->tqd_start,
4286 adapter->queue_desc_pa);
4287 err_ver:
4288 vmxnet3_free_pci_resources(adapter);
4289 err_alloc_pci:
4290 dma_free_coherent(&adapter->pdev->dev,
4291 sizeof(struct Vmxnet3_DriverShared),
4292 adapter->shared, adapter->shared_pa);
4293 err_alloc_shared:
4294 dma_unmap_single(&adapter->pdev->dev, adapter->adapter_pa,
4295 sizeof(struct vmxnet3_adapter), DMA_TO_DEVICE);
4296 err_set_mask:
4297 free_netdev(netdev);
4298 return err;
4299 }
4300
4301
4302 static void
vmxnet3_remove_device(struct pci_dev * pdev)4303 vmxnet3_remove_device(struct pci_dev *pdev)
4304 {
4305 struct net_device *netdev = pci_get_drvdata(pdev);
4306 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
4307 int size = 0;
4308 int num_rx_queues, rx_queues;
4309 unsigned long flags;
4310
4311 #ifdef VMXNET3_RSS
4312 if (enable_mq)
4313 num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
4314 (int)num_online_cpus());
4315 else
4316 #endif
4317 num_rx_queues = 1;
4318 if (!VMXNET3_VERSION_GE_6(adapter)) {
4319 num_rx_queues = rounddown_pow_of_two(num_rx_queues);
4320 }
4321 if (VMXNET3_VERSION_GE_6(adapter)) {
4322 spin_lock_irqsave(&adapter->cmd_lock, flags);
4323 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
4324 VMXNET3_CMD_GET_MAX_QUEUES_CONF);
4325 rx_queues = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
4326 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
4327 if (rx_queues > 0)
4328 rx_queues = (rx_queues >> 8) & 0xff;
4329 else
4330 rx_queues = min(num_rx_queues, VMXNET3_DEVICE_DEFAULT_RX_QUEUES);
4331 num_rx_queues = min(num_rx_queues, rx_queues);
4332 } else {
4333 num_rx_queues = min(num_rx_queues,
4334 VMXNET3_DEVICE_DEFAULT_RX_QUEUES);
4335 }
4336
4337 cancel_work_sync(&adapter->work);
4338
4339 unregister_netdev(netdev);
4340
4341 vmxnet3_free_intr_resources(adapter);
4342 vmxnet3_free_pci_resources(adapter);
4343 if (VMXNET3_VERSION_GE_3(adapter)) {
4344 dma_free_coherent(&adapter->pdev->dev,
4345 sizeof(struct Vmxnet3_CoalesceScheme),
4346 adapter->coal_conf, adapter->coal_conf_pa);
4347 }
4348 #ifdef VMXNET3_RSS
4349 dma_free_coherent(&adapter->pdev->dev, sizeof(struct UPT1_RSSConf),
4350 adapter->rss_conf, adapter->rss_conf_pa);
4351 #endif
4352 dma_free_coherent(&adapter->pdev->dev, sizeof(struct Vmxnet3_PMConf),
4353 adapter->pm_conf, adapter->pm_conf_pa);
4354
4355 size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
4356 size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
4357 dma_free_coherent(&adapter->pdev->dev, size, adapter->tqd_start,
4358 adapter->queue_desc_pa);
4359 dma_free_coherent(&adapter->pdev->dev,
4360 sizeof(struct Vmxnet3_DriverShared),
4361 adapter->shared, adapter->shared_pa);
4362 dma_unmap_single(&adapter->pdev->dev, adapter->adapter_pa,
4363 sizeof(struct vmxnet3_adapter), DMA_TO_DEVICE);
4364 free_netdev(netdev);
4365 }
4366
vmxnet3_shutdown_device(struct pci_dev * pdev)4367 static void vmxnet3_shutdown_device(struct pci_dev *pdev)
4368 {
4369 struct net_device *netdev = pci_get_drvdata(pdev);
4370 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
4371 unsigned long flags;
4372
4373 /* Reset_work may be in the middle of resetting the device, wait for its
4374 * completion.
4375 */
4376 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
4377 usleep_range(1000, 2000);
4378
4379 if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED,
4380 &adapter->state)) {
4381 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
4382 return;
4383 }
4384 spin_lock_irqsave(&adapter->cmd_lock, flags);
4385 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
4386 VMXNET3_CMD_QUIESCE_DEV);
4387 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
4388 vmxnet3_disable_all_intrs(adapter);
4389
4390 clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
4391 }
4392
4393
4394 #ifdef CONFIG_PM
4395
4396 static int
vmxnet3_suspend(struct device * device)4397 vmxnet3_suspend(struct device *device)
4398 {
4399 struct pci_dev *pdev = to_pci_dev(device);
4400 struct net_device *netdev = pci_get_drvdata(pdev);
4401 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
4402 struct Vmxnet3_PMConf *pmConf;
4403 struct ethhdr *ehdr;
4404 struct arphdr *ahdr;
4405 u8 *arpreq;
4406 struct in_device *in_dev;
4407 struct in_ifaddr *ifa;
4408 unsigned long flags;
4409 int i = 0;
4410
4411 if (!netif_running(netdev))
4412 return 0;
4413
4414 for (i = 0; i < adapter->num_rx_queues; i++)
4415 napi_disable(&adapter->rx_queue[i].napi);
4416
4417 vmxnet3_disable_all_intrs(adapter);
4418 vmxnet3_free_irqs(adapter);
4419 vmxnet3_free_intr_resources(adapter);
4420
4421 netif_device_detach(netdev);
4422
4423 /* Create wake-up filters. */
4424 pmConf = adapter->pm_conf;
4425 memset(pmConf, 0, sizeof(*pmConf));
4426
4427 if (adapter->wol & WAKE_UCAST) {
4428 pmConf->filters[i].patternSize = ETH_ALEN;
4429 pmConf->filters[i].maskSize = 1;
4430 memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
4431 pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
4432
4433 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
4434 i++;
4435 }
4436
4437 if (adapter->wol & WAKE_ARP) {
4438 rcu_read_lock();
4439
4440 in_dev = __in_dev_get_rcu(netdev);
4441 if (!in_dev) {
4442 rcu_read_unlock();
4443 goto skip_arp;
4444 }
4445
4446 ifa = rcu_dereference(in_dev->ifa_list);
4447 if (!ifa) {
4448 rcu_read_unlock();
4449 goto skip_arp;
4450 }
4451
4452 pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
4453 sizeof(struct arphdr) + /* ARP header */
4454 2 * ETH_ALEN + /* 2 Ethernet addresses*/
4455 2 * sizeof(u32); /*2 IPv4 addresses */
4456 pmConf->filters[i].maskSize =
4457 (pmConf->filters[i].patternSize - 1) / 8 + 1;
4458
4459 /* ETH_P_ARP in Ethernet header. */
4460 ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
4461 ehdr->h_proto = htons(ETH_P_ARP);
4462
4463 /* ARPOP_REQUEST in ARP header. */
4464 ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
4465 ahdr->ar_op = htons(ARPOP_REQUEST);
4466 arpreq = (u8 *)(ahdr + 1);
4467
4468 /* The Unicast IPv4 address in 'tip' field. */
4469 arpreq += 2 * ETH_ALEN + sizeof(u32);
4470 *(__be32 *)arpreq = ifa->ifa_address;
4471
4472 rcu_read_unlock();
4473
4474 /* The mask for the relevant bits. */
4475 pmConf->filters[i].mask[0] = 0x00;
4476 pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
4477 pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
4478 pmConf->filters[i].mask[3] = 0x00;
4479 pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
4480 pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
4481
4482 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
4483 i++;
4484 }
4485
4486 skip_arp:
4487 if (adapter->wol & WAKE_MAGIC)
4488 pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
4489
4490 pmConf->numFilters = i;
4491
4492 adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
4493 adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
4494 *pmConf));
4495 adapter->shared->devRead.pmConfDesc.confPA =
4496 cpu_to_le64(adapter->pm_conf_pa);
4497
4498 spin_lock_irqsave(&adapter->cmd_lock, flags);
4499 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
4500 VMXNET3_CMD_UPDATE_PMCFG);
4501 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
4502
4503 pci_save_state(pdev);
4504 pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
4505 adapter->wol);
4506 pci_disable_device(pdev);
4507 pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
4508
4509 return 0;
4510 }
4511
4512
4513 static int
vmxnet3_resume(struct device * device)4514 vmxnet3_resume(struct device *device)
4515 {
4516 int err;
4517 unsigned long flags;
4518 struct pci_dev *pdev = to_pci_dev(device);
4519 struct net_device *netdev = pci_get_drvdata(pdev);
4520 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
4521
4522 if (!netif_running(netdev))
4523 return 0;
4524
4525 pci_set_power_state(pdev, PCI_D0);
4526 pci_restore_state(pdev);
4527 err = pci_enable_device_mem(pdev);
4528 if (err != 0)
4529 return err;
4530
4531 pci_enable_wake(pdev, PCI_D0, 0);
4532
4533 vmxnet3_alloc_intr_resources(adapter);
4534
4535 /* During hibernate and suspend, device has to be reinitialized as the
4536 * device state need not be preserved.
4537 */
4538
4539 /* Need not check adapter state as other reset tasks cannot run during
4540 * device resume.
4541 */
4542 spin_lock_irqsave(&adapter->cmd_lock, flags);
4543 VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
4544 VMXNET3_CMD_QUIESCE_DEV);
4545 spin_unlock_irqrestore(&adapter->cmd_lock, flags);
4546 vmxnet3_tq_cleanup_all(adapter);
4547 vmxnet3_rq_cleanup_all(adapter);
4548
4549 vmxnet3_reset_dev(adapter);
4550 err = vmxnet3_activate_dev(adapter);
4551 if (err != 0) {
4552 netdev_err(netdev,
4553 "failed to re-activate on resume, error: %d", err);
4554 vmxnet3_force_close(adapter);
4555 return err;
4556 }
4557 netif_device_attach(netdev);
4558
4559 return 0;
4560 }
4561
4562 static const struct dev_pm_ops vmxnet3_pm_ops = {
4563 .suspend = vmxnet3_suspend,
4564 .resume = vmxnet3_resume,
4565 .freeze = vmxnet3_suspend,
4566 .restore = vmxnet3_resume,
4567 };
4568 #endif
4569
4570 static struct pci_driver vmxnet3_driver = {
4571 .name = vmxnet3_driver_name,
4572 .id_table = vmxnet3_pciid_table,
4573 .probe = vmxnet3_probe_device,
4574 .remove = vmxnet3_remove_device,
4575 .shutdown = vmxnet3_shutdown_device,
4576 #ifdef CONFIG_PM
4577 .driver.pm = &vmxnet3_pm_ops,
4578 #endif
4579 };
4580
4581
4582 static int __init
vmxnet3_init_module(void)4583 vmxnet3_init_module(void)
4584 {
4585 pr_info("%s - version %s\n", VMXNET3_DRIVER_DESC,
4586 VMXNET3_DRIVER_VERSION_REPORT);
4587 return pci_register_driver(&vmxnet3_driver);
4588 }
4589
4590 module_init(vmxnet3_init_module);
4591
4592
4593 static void
vmxnet3_exit_module(void)4594 vmxnet3_exit_module(void)
4595 {
4596 pci_unregister_driver(&vmxnet3_driver);
4597 }
4598
4599 module_exit(vmxnet3_exit_module);
4600
4601 MODULE_AUTHOR("VMware, Inc.");
4602 MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
4603 MODULE_LICENSE("GPL v2");
4604 MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);
4605