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