xref: /linux/drivers/net/ethernet/pensando/ionic/ionic_txrx.c (revision a126eca844353360ebafa9088d22865cb8e022e3)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
3 
4 #include <linux/ip.h>
5 #include <linux/ipv6.h>
6 #include <linux/if_vlan.h>
7 #include <net/ip6_checksum.h>
8 #include <net/netdev_queues.h>
9 
10 #include "ionic.h"
11 #include "ionic_lif.h"
12 #include "ionic_txrx.h"
13 
14 static dma_addr_t ionic_tx_map_single(struct ionic_queue *q,
15 				      void *data, size_t len);
16 
17 static dma_addr_t ionic_tx_map_frag(struct ionic_queue *q,
18 				    const skb_frag_t *frag,
19 				    size_t offset, size_t len);
20 
21 static void ionic_tx_desc_unmap_bufs(struct ionic_queue *q,
22 				     struct ionic_tx_desc_info *desc_info);
23 
24 static void ionic_tx_clean(struct ionic_queue *q,
25 			   struct ionic_tx_desc_info *desc_info,
26 			   struct ionic_txq_comp *comp);
27 
28 static inline void ionic_txq_post(struct ionic_queue *q, bool ring_dbell)
29 {
30 	ionic_q_post(q, ring_dbell);
31 }
32 
33 static inline void ionic_rxq_post(struct ionic_queue *q, bool ring_dbell)
34 {
35 	ionic_q_post(q, ring_dbell);
36 }
37 
38 bool ionic_txq_poke_doorbell(struct ionic_queue *q)
39 {
40 	struct netdev_queue *netdev_txq;
41 	unsigned long now, then, dif;
42 	struct net_device *netdev;
43 
44 	netdev = q->lif->netdev;
45 	netdev_txq = netdev_get_tx_queue(netdev, q->index);
46 
47 	HARD_TX_LOCK(netdev, netdev_txq, smp_processor_id());
48 
49 	if (q->tail_idx == q->head_idx) {
50 		HARD_TX_UNLOCK(netdev, netdev_txq);
51 		return false;
52 	}
53 
54 	now = READ_ONCE(jiffies);
55 	then = q->dbell_jiffies;
56 	dif = now - then;
57 
58 	if (dif > q->dbell_deadline) {
59 		ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
60 				 q->dbval | q->head_idx);
61 
62 		q->dbell_jiffies = now;
63 	}
64 
65 	HARD_TX_UNLOCK(netdev, netdev_txq);
66 
67 	return true;
68 }
69 
70 bool ionic_rxq_poke_doorbell(struct ionic_queue *q)
71 {
72 	unsigned long now, then, dif;
73 
74 	/* no lock, called from rx napi or txrx napi, nothing else can fill */
75 
76 	if (q->tail_idx == q->head_idx)
77 		return false;
78 
79 	now = READ_ONCE(jiffies);
80 	then = q->dbell_jiffies;
81 	dif = now - then;
82 
83 	if (dif > q->dbell_deadline) {
84 		ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
85 				 q->dbval | q->head_idx);
86 
87 		q->dbell_jiffies = now;
88 
89 		dif = 2 * q->dbell_deadline;
90 		if (dif > IONIC_RX_MAX_DOORBELL_DEADLINE)
91 			dif = IONIC_RX_MAX_DOORBELL_DEADLINE;
92 
93 		q->dbell_deadline = dif;
94 	}
95 
96 	return true;
97 }
98 
99 static inline struct ionic_txq_sg_elem *ionic_tx_sg_elems(struct ionic_queue *q)
100 {
101 	if (likely(q->sg_desc_size == sizeof(struct ionic_txq_sg_desc_v1)))
102 		return q->txq_sgl_v1[q->head_idx].elems;
103 	else
104 		return q->txq_sgl[q->head_idx].elems;
105 }
106 
107 static inline struct netdev_queue *q_to_ndq(struct net_device *netdev,
108 					    struct ionic_queue *q)
109 {
110 	return netdev_get_tx_queue(netdev, q->index);
111 }
112 
113 static void *ionic_rx_buf_va(struct ionic_buf_info *buf_info)
114 {
115 	return page_address(buf_info->page) + buf_info->page_offset;
116 }
117 
118 static dma_addr_t ionic_rx_buf_pa(struct ionic_buf_info *buf_info)
119 {
120 	return buf_info->dma_addr + buf_info->page_offset;
121 }
122 
123 static unsigned int ionic_rx_buf_size(struct ionic_buf_info *buf_info)
124 {
125 	return min_t(u32, IONIC_MAX_BUF_LEN, IONIC_PAGE_SIZE - buf_info->page_offset);
126 }
127 
128 static int ionic_rx_page_alloc(struct ionic_queue *q,
129 			       struct ionic_buf_info *buf_info)
130 {
131 	struct device *dev = q->dev;
132 	dma_addr_t dma_addr;
133 	struct page *page;
134 
135 	page = alloc_pages(IONIC_PAGE_GFP_MASK, 0);
136 	if (unlikely(!page)) {
137 		net_err_ratelimited("%s: %s page alloc failed\n",
138 				    dev_name(dev), q->name);
139 		q_to_rx_stats(q)->alloc_err++;
140 		return -ENOMEM;
141 	}
142 
143 	dma_addr = dma_map_page(dev, page, 0,
144 				IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
145 	if (unlikely(dma_mapping_error(dev, dma_addr))) {
146 		__free_pages(page, 0);
147 		net_err_ratelimited("%s: %s dma map failed\n",
148 				    dev_name(dev), q->name);
149 		q_to_rx_stats(q)->dma_map_err++;
150 		return -EIO;
151 	}
152 
153 	buf_info->dma_addr = dma_addr;
154 	buf_info->page = page;
155 	buf_info->page_offset = 0;
156 
157 	return 0;
158 }
159 
160 static void ionic_rx_page_free(struct ionic_queue *q,
161 			       struct ionic_buf_info *buf_info)
162 {
163 	struct device *dev = q->dev;
164 
165 	if (unlikely(!buf_info)) {
166 		net_err_ratelimited("%s: %s invalid buf_info in free\n",
167 				    dev_name(dev), q->name);
168 		return;
169 	}
170 
171 	if (!buf_info->page)
172 		return;
173 
174 	dma_unmap_page(dev, buf_info->dma_addr, IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
175 	__free_pages(buf_info->page, 0);
176 	buf_info->page = NULL;
177 }
178 
179 static bool ionic_rx_buf_recycle(struct ionic_queue *q,
180 				 struct ionic_buf_info *buf_info, u32 len)
181 {
182 	u32 size;
183 
184 	/* don't re-use pages allocated in low-mem condition */
185 	if (page_is_pfmemalloc(buf_info->page))
186 		return false;
187 
188 	/* don't re-use buffers from non-local numa nodes */
189 	if (page_to_nid(buf_info->page) != numa_mem_id())
190 		return false;
191 
192 	size = ALIGN(len, q->xdp_rxq_info ? IONIC_PAGE_SIZE : IONIC_PAGE_SPLIT_SZ);
193 	buf_info->page_offset += size;
194 	if (buf_info->page_offset >= IONIC_PAGE_SIZE)
195 		return false;
196 
197 	get_page(buf_info->page);
198 
199 	return true;
200 }
201 
202 static void ionic_rx_add_skb_frag(struct ionic_queue *q,
203 				  struct sk_buff *skb,
204 				  struct ionic_buf_info *buf_info,
205 				  u32 off, u32 len,
206 				  bool synced)
207 {
208 	if (!synced)
209 		dma_sync_single_range_for_cpu(q->dev, ionic_rx_buf_pa(buf_info),
210 					      off, len, DMA_FROM_DEVICE);
211 
212 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
213 			buf_info->page, buf_info->page_offset + off,
214 			len,
215 			IONIC_PAGE_SIZE);
216 
217 	if (!ionic_rx_buf_recycle(q, buf_info, len)) {
218 		dma_unmap_page(q->dev, buf_info->dma_addr,
219 			       IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
220 		buf_info->page = NULL;
221 	}
222 }
223 
224 static struct sk_buff *ionic_rx_build_skb(struct ionic_queue *q,
225 					  struct ionic_rx_desc_info *desc_info,
226 					  unsigned int headroom,
227 					  unsigned int len,
228 					  unsigned int num_sg_elems,
229 					  bool synced)
230 {
231 	struct ionic_buf_info *buf_info;
232 	struct sk_buff *skb;
233 	unsigned int i;
234 	u16 frag_len;
235 
236 	buf_info = &desc_info->bufs[0];
237 	prefetchw(buf_info->page);
238 
239 	skb = napi_get_frags(&q_to_qcq(q)->napi);
240 	if (unlikely(!skb)) {
241 		net_warn_ratelimited("%s: SKB alloc failed on %s!\n",
242 				     dev_name(q->dev), q->name);
243 		q_to_rx_stats(q)->alloc_err++;
244 		return NULL;
245 	}
246 
247 	if (headroom)
248 		frag_len = min_t(u16, len,
249 				 IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN);
250 	else
251 		frag_len = min_t(u16, len, ionic_rx_buf_size(buf_info));
252 
253 	if (unlikely(!buf_info->page))
254 		goto err_bad_buf_page;
255 	ionic_rx_add_skb_frag(q, skb, buf_info, headroom, frag_len, synced);
256 	len -= frag_len;
257 	buf_info++;
258 
259 	for (i = 0; i < num_sg_elems; i++, buf_info++) {
260 		if (unlikely(!buf_info->page))
261 			goto err_bad_buf_page;
262 		frag_len = min_t(u16, len, ionic_rx_buf_size(buf_info));
263 		ionic_rx_add_skb_frag(q, skb, buf_info, 0, frag_len, synced);
264 		len -= frag_len;
265 	}
266 
267 	return skb;
268 
269 err_bad_buf_page:
270 	dev_kfree_skb(skb);
271 	return NULL;
272 }
273 
274 static struct sk_buff *ionic_rx_copybreak(struct net_device *netdev,
275 					  struct ionic_queue *q,
276 					  struct ionic_rx_desc_info *desc_info,
277 					  unsigned int headroom,
278 					  unsigned int len,
279 					  bool synced)
280 {
281 	struct ionic_buf_info *buf_info;
282 	struct device *dev = q->dev;
283 	struct sk_buff *skb;
284 
285 	buf_info = &desc_info->bufs[0];
286 
287 	skb = napi_alloc_skb(&q_to_qcq(q)->napi, len);
288 	if (unlikely(!skb)) {
289 		net_warn_ratelimited("%s: SKB alloc failed on %s!\n",
290 				     dev_name(dev), q->name);
291 		q_to_rx_stats(q)->alloc_err++;
292 		return NULL;
293 	}
294 
295 	if (unlikely(!buf_info->page)) {
296 		dev_kfree_skb(skb);
297 		return NULL;
298 	}
299 
300 	if (!synced)
301 		dma_sync_single_range_for_cpu(dev, ionic_rx_buf_pa(buf_info),
302 					      headroom, len, DMA_FROM_DEVICE);
303 	skb_copy_to_linear_data(skb, ionic_rx_buf_va(buf_info) + headroom, len);
304 	dma_sync_single_range_for_device(dev, ionic_rx_buf_pa(buf_info),
305 					 headroom, len, DMA_FROM_DEVICE);
306 
307 	skb_put(skb, len);
308 	skb->protocol = eth_type_trans(skb, netdev);
309 
310 	return skb;
311 }
312 
313 static void ionic_xdp_tx_desc_clean(struct ionic_queue *q,
314 				    struct ionic_tx_desc_info *desc_info)
315 {
316 	unsigned int nbufs = desc_info->nbufs;
317 	struct ionic_buf_info *buf_info;
318 	struct device *dev = q->dev;
319 	int i;
320 
321 	if (!nbufs)
322 		return;
323 
324 	buf_info = desc_info->bufs;
325 	dma_unmap_single(dev, buf_info->dma_addr,
326 			 buf_info->len, DMA_TO_DEVICE);
327 	if (desc_info->act == XDP_TX)
328 		__free_pages(buf_info->page, 0);
329 	buf_info->page = NULL;
330 
331 	buf_info++;
332 	for (i = 1; i < nbufs + 1 && buf_info->page; i++, buf_info++) {
333 		dma_unmap_page(dev, buf_info->dma_addr,
334 			       buf_info->len, DMA_TO_DEVICE);
335 		if (desc_info->act == XDP_TX)
336 			__free_pages(buf_info->page, 0);
337 		buf_info->page = NULL;
338 	}
339 
340 	if (desc_info->act == XDP_REDIRECT)
341 		xdp_return_frame(desc_info->xdpf);
342 
343 	desc_info->nbufs = 0;
344 	desc_info->xdpf = NULL;
345 	desc_info->act = 0;
346 }
347 
348 static int ionic_xdp_post_frame(struct ionic_queue *q, struct xdp_frame *frame,
349 				enum xdp_action act, struct page *page, int off,
350 				bool ring_doorbell)
351 {
352 	struct ionic_tx_desc_info *desc_info;
353 	struct ionic_buf_info *buf_info;
354 	struct ionic_tx_stats *stats;
355 	struct ionic_txq_desc *desc;
356 	size_t len = frame->len;
357 	dma_addr_t dma_addr;
358 	u64 cmd;
359 
360 	desc_info = &q->tx_info[q->head_idx];
361 	desc = &q->txq[q->head_idx];
362 	buf_info = desc_info->bufs;
363 	stats = q_to_tx_stats(q);
364 
365 	dma_addr = ionic_tx_map_single(q, frame->data, len);
366 	if (!dma_addr)
367 		return -EIO;
368 	buf_info->dma_addr = dma_addr;
369 	buf_info->len = len;
370 	buf_info->page = page;
371 	buf_info->page_offset = off;
372 
373 	desc_info->nbufs = 1;
374 	desc_info->xdpf = frame;
375 	desc_info->act = act;
376 
377 	if (xdp_frame_has_frags(frame)) {
378 		struct ionic_txq_sg_elem *elem;
379 		struct skb_shared_info *sinfo;
380 		struct ionic_buf_info *bi;
381 		skb_frag_t *frag;
382 		int i;
383 
384 		bi = &buf_info[1];
385 		sinfo = xdp_get_shared_info_from_frame(frame);
386 		frag = sinfo->frags;
387 		elem = ionic_tx_sg_elems(q);
388 		for (i = 0; i < sinfo->nr_frags; i++, frag++, bi++) {
389 			dma_addr = ionic_tx_map_frag(q, frag, 0, skb_frag_size(frag));
390 			if (!dma_addr) {
391 				ionic_tx_desc_unmap_bufs(q, desc_info);
392 				return -EIO;
393 			}
394 			bi->dma_addr = dma_addr;
395 			bi->len = skb_frag_size(frag);
396 			bi->page = skb_frag_page(frag);
397 
398 			elem->addr = cpu_to_le64(bi->dma_addr);
399 			elem->len = cpu_to_le16(bi->len);
400 			elem++;
401 
402 			desc_info->nbufs++;
403 		}
404 	}
405 
406 	cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_NONE,
407 				  0, (desc_info->nbufs - 1), buf_info->dma_addr);
408 	desc->cmd = cpu_to_le64(cmd);
409 	desc->len = cpu_to_le16(len);
410 	desc->csum_start = 0;
411 	desc->csum_offset = 0;
412 
413 	stats->xdp_frames++;
414 	stats->pkts++;
415 	stats->bytes += len;
416 
417 	ionic_txq_post(q, ring_doorbell);
418 
419 	return 0;
420 }
421 
422 int ionic_xdp_xmit(struct net_device *netdev, int n,
423 		   struct xdp_frame **xdp_frames, u32 flags)
424 {
425 	struct ionic_lif *lif = netdev_priv(netdev);
426 	struct ionic_queue *txq;
427 	struct netdev_queue *nq;
428 	int nxmit;
429 	int space;
430 	int cpu;
431 	int qi;
432 
433 	if (unlikely(!test_bit(IONIC_LIF_F_UP, lif->state)))
434 		return -ENETDOWN;
435 
436 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
437 		return -EINVAL;
438 
439 	/* AdminQ is assumed on cpu 0, while we attempt to affinitize the
440 	 * TxRx queue pairs 0..n-1 on cpus 1..n.  We try to keep with that
441 	 * affinitization here, but of course irqbalance and friends might
442 	 * have juggled things anyway, so we have to check for the 0 case.
443 	 */
444 	cpu = smp_processor_id();
445 	qi = cpu ? (cpu - 1) % lif->nxqs : cpu;
446 
447 	txq = &lif->txqcqs[qi]->q;
448 	nq = netdev_get_tx_queue(netdev, txq->index);
449 	__netif_tx_lock(nq, cpu);
450 	txq_trans_cond_update(nq);
451 
452 	if (netif_tx_queue_stopped(nq) ||
453 	    !netif_txq_maybe_stop(q_to_ndq(netdev, txq),
454 				  ionic_q_space_avail(txq),
455 				  1, 1)) {
456 		__netif_tx_unlock(nq);
457 		return -EIO;
458 	}
459 
460 	space = min_t(int, n, ionic_q_space_avail(txq));
461 	for (nxmit = 0; nxmit < space ; nxmit++) {
462 		if (ionic_xdp_post_frame(txq, xdp_frames[nxmit],
463 					 XDP_REDIRECT,
464 					 virt_to_page(xdp_frames[nxmit]->data),
465 					 0, false)) {
466 			nxmit--;
467 			break;
468 		}
469 	}
470 
471 	if (flags & XDP_XMIT_FLUSH)
472 		ionic_dbell_ring(lif->kern_dbpage, txq->hw_type,
473 				 txq->dbval | txq->head_idx);
474 
475 	netif_txq_maybe_stop(q_to_ndq(netdev, txq),
476 			     ionic_q_space_avail(txq),
477 			     4, 4);
478 	__netif_tx_unlock(nq);
479 
480 	return nxmit;
481 }
482 
483 static bool ionic_run_xdp(struct ionic_rx_stats *stats,
484 			  struct net_device *netdev,
485 			  struct bpf_prog *xdp_prog,
486 			  struct ionic_queue *rxq,
487 			  struct ionic_buf_info *buf_info,
488 			  int len)
489 {
490 	u32 xdp_action = XDP_ABORTED;
491 	struct xdp_buff xdp_buf;
492 	struct ionic_queue *txq;
493 	struct netdev_queue *nq;
494 	struct xdp_frame *xdpf;
495 	int remain_len;
496 	int frag_len;
497 	int err = 0;
498 
499 	xdp_init_buff(&xdp_buf, IONIC_PAGE_SIZE, rxq->xdp_rxq_info);
500 	frag_len = min_t(u16, len, IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN);
501 	xdp_prepare_buff(&xdp_buf, ionic_rx_buf_va(buf_info),
502 			 XDP_PACKET_HEADROOM, frag_len, false);
503 
504 	dma_sync_single_range_for_cpu(rxq->dev, ionic_rx_buf_pa(buf_info),
505 				      XDP_PACKET_HEADROOM, len,
506 				      DMA_FROM_DEVICE);
507 
508 	prefetchw(&xdp_buf.data_hard_start);
509 
510 	/*  We limit MTU size to one buffer if !xdp_has_frags, so
511 	 *  if the recv len is bigger than one buffer
512 	 *     then we know we have frag info to gather
513 	 */
514 	remain_len = len - frag_len;
515 	if (remain_len) {
516 		struct skb_shared_info *sinfo;
517 		struct ionic_buf_info *bi;
518 		skb_frag_t *frag;
519 
520 		bi = buf_info;
521 		sinfo = xdp_get_shared_info_from_buff(&xdp_buf);
522 		sinfo->nr_frags = 0;
523 		sinfo->xdp_frags_size = 0;
524 		xdp_buff_set_frags_flag(&xdp_buf);
525 
526 		do {
527 			if (unlikely(sinfo->nr_frags >= MAX_SKB_FRAGS)) {
528 				err = -ENOSPC;
529 				goto out_xdp_abort;
530 			}
531 
532 			frag = &sinfo->frags[sinfo->nr_frags];
533 			sinfo->nr_frags++;
534 			bi++;
535 			frag_len = min_t(u16, remain_len, ionic_rx_buf_size(bi));
536 			dma_sync_single_range_for_cpu(rxq->dev, ionic_rx_buf_pa(bi),
537 						      0, frag_len, DMA_FROM_DEVICE);
538 			skb_frag_fill_page_desc(frag, bi->page, 0, frag_len);
539 			sinfo->xdp_frags_size += frag_len;
540 			remain_len -= frag_len;
541 
542 			if (page_is_pfmemalloc(bi->page))
543 				xdp_buff_set_frag_pfmemalloc(&xdp_buf);
544 		} while (remain_len > 0);
545 	}
546 
547 	xdp_action = bpf_prog_run_xdp(xdp_prog, &xdp_buf);
548 
549 	switch (xdp_action) {
550 	case XDP_PASS:
551 		stats->xdp_pass++;
552 		return false;  /* false = we didn't consume the packet */
553 
554 	case XDP_DROP:
555 		ionic_rx_page_free(rxq, buf_info);
556 		stats->xdp_drop++;
557 		break;
558 
559 	case XDP_TX:
560 		xdpf = xdp_convert_buff_to_frame(&xdp_buf);
561 		if (!xdpf)
562 			goto out_xdp_abort;
563 
564 		txq = rxq->partner;
565 		nq = netdev_get_tx_queue(netdev, txq->index);
566 		__netif_tx_lock(nq, smp_processor_id());
567 		txq_trans_cond_update(nq);
568 
569 		if (netif_tx_queue_stopped(nq) ||
570 		    !netif_txq_maybe_stop(q_to_ndq(netdev, txq),
571 					  ionic_q_space_avail(txq),
572 					  1, 1)) {
573 			__netif_tx_unlock(nq);
574 			goto out_xdp_abort;
575 		}
576 
577 		dma_unmap_page(rxq->dev, buf_info->dma_addr,
578 			       IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
579 
580 		err = ionic_xdp_post_frame(txq, xdpf, XDP_TX,
581 					   buf_info->page,
582 					   buf_info->page_offset,
583 					   true);
584 		__netif_tx_unlock(nq);
585 		if (err) {
586 			netdev_dbg(netdev, "tx ionic_xdp_post_frame err %d\n", err);
587 			goto out_xdp_abort;
588 		}
589 		buf_info->page = NULL;
590 		stats->xdp_tx++;
591 
592 		/* the Tx completion will free the buffers */
593 		break;
594 
595 	case XDP_REDIRECT:
596 		/* unmap the pages before handing them to a different device */
597 		dma_unmap_page(rxq->dev, buf_info->dma_addr,
598 			       IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
599 
600 		err = xdp_do_redirect(netdev, &xdp_buf, xdp_prog);
601 		if (err) {
602 			netdev_dbg(netdev, "xdp_do_redirect err %d\n", err);
603 			goto out_xdp_abort;
604 		}
605 		buf_info->page = NULL;
606 		rxq->xdp_flush = true;
607 		stats->xdp_redirect++;
608 		break;
609 
610 	case XDP_ABORTED:
611 	default:
612 		goto out_xdp_abort;
613 	}
614 
615 	return true;
616 
617 out_xdp_abort:
618 	trace_xdp_exception(netdev, xdp_prog, xdp_action);
619 	ionic_rx_page_free(rxq, buf_info);
620 	stats->xdp_aborted++;
621 
622 	return true;
623 }
624 
625 static void ionic_rx_clean(struct ionic_queue *q,
626 			   struct ionic_rx_desc_info *desc_info,
627 			   struct ionic_rxq_comp *comp)
628 {
629 	struct net_device *netdev = q->lif->netdev;
630 	struct ionic_qcq *qcq = q_to_qcq(q);
631 	struct ionic_rx_stats *stats;
632 	struct bpf_prog *xdp_prog;
633 	unsigned int headroom;
634 	struct sk_buff *skb;
635 	bool synced = false;
636 	bool use_copybreak;
637 	u16 len;
638 
639 	stats = q_to_rx_stats(q);
640 
641 	if (comp->status) {
642 		stats->dropped++;
643 		return;
644 	}
645 
646 	len = le16_to_cpu(comp->len);
647 	stats->pkts++;
648 	stats->bytes += len;
649 
650 	xdp_prog = READ_ONCE(q->lif->xdp_prog);
651 	if (xdp_prog) {
652 		if (ionic_run_xdp(stats, netdev, xdp_prog, q, desc_info->bufs, len))
653 			return;
654 		synced = true;
655 	}
656 
657 	headroom = q->xdp_rxq_info ? XDP_PACKET_HEADROOM : 0;
658 	use_copybreak = len <= q->lif->rx_copybreak;
659 	if (use_copybreak)
660 		skb = ionic_rx_copybreak(netdev, q, desc_info,
661 					 headroom, len, synced);
662 	else
663 		skb = ionic_rx_build_skb(q, desc_info, headroom, len,
664 					 comp->num_sg_elems, synced);
665 
666 	if (unlikely(!skb)) {
667 		stats->dropped++;
668 		return;
669 	}
670 
671 	skb_record_rx_queue(skb, q->index);
672 
673 	if (likely(netdev->features & NETIF_F_RXHASH)) {
674 		switch (comp->pkt_type_color & IONIC_RXQ_COMP_PKT_TYPE_MASK) {
675 		case IONIC_PKT_TYPE_IPV4:
676 		case IONIC_PKT_TYPE_IPV6:
677 			skb_set_hash(skb, le32_to_cpu(comp->rss_hash),
678 				     PKT_HASH_TYPE_L3);
679 			break;
680 		case IONIC_PKT_TYPE_IPV4_TCP:
681 		case IONIC_PKT_TYPE_IPV6_TCP:
682 		case IONIC_PKT_TYPE_IPV4_UDP:
683 		case IONIC_PKT_TYPE_IPV6_UDP:
684 			skb_set_hash(skb, le32_to_cpu(comp->rss_hash),
685 				     PKT_HASH_TYPE_L4);
686 			break;
687 		}
688 	}
689 
690 	if (likely(netdev->features & NETIF_F_RXCSUM) &&
691 	    (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_CALC)) {
692 		skb->ip_summed = CHECKSUM_COMPLETE;
693 		skb->csum = (__force __wsum)le16_to_cpu(comp->csum);
694 		stats->csum_complete++;
695 	} else {
696 		stats->csum_none++;
697 	}
698 
699 	if (unlikely((comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_TCP_BAD) ||
700 		     (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_UDP_BAD) ||
701 		     (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_IP_BAD)))
702 		stats->csum_error++;
703 
704 	if (likely(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
705 	    (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_VLAN)) {
706 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
707 				       le16_to_cpu(comp->vlan_tci));
708 		stats->vlan_stripped++;
709 	}
710 
711 	if (unlikely(q->features & IONIC_RXQ_F_HWSTAMP)) {
712 		__le64 *cq_desc_hwstamp;
713 		u64 hwstamp;
714 
715 		cq_desc_hwstamp =
716 			(void *)comp +
717 			qcq->cq.desc_size -
718 			sizeof(struct ionic_rxq_comp) -
719 			IONIC_HWSTAMP_CQ_NEGOFFSET;
720 
721 		hwstamp = le64_to_cpu(*cq_desc_hwstamp);
722 
723 		if (hwstamp != IONIC_HWSTAMP_INVALID) {
724 			skb_hwtstamps(skb)->hwtstamp = ionic_lif_phc_ktime(q->lif, hwstamp);
725 			stats->hwstamp_valid++;
726 		} else {
727 			stats->hwstamp_invalid++;
728 		}
729 	}
730 
731 	if (use_copybreak)
732 		napi_gro_receive(&qcq->napi, skb);
733 	else
734 		napi_gro_frags(&qcq->napi);
735 }
736 
737 bool ionic_rx_service(struct ionic_cq *cq)
738 {
739 	struct ionic_rx_desc_info *desc_info;
740 	struct ionic_queue *q = cq->bound_q;
741 	struct ionic_rxq_comp *comp;
742 
743 	comp = &((struct ionic_rxq_comp *)cq->base)[cq->tail_idx];
744 
745 	if (!color_match(comp->pkt_type_color, cq->done_color))
746 		return false;
747 
748 	/* check for empty queue */
749 	if (q->tail_idx == q->head_idx)
750 		return false;
751 
752 	if (q->tail_idx != le16_to_cpu(comp->comp_index))
753 		return false;
754 
755 	desc_info = &q->rx_info[q->tail_idx];
756 	q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
757 
758 	/* clean the related q entry, only one per qc completion */
759 	ionic_rx_clean(q, desc_info, comp);
760 
761 	return true;
762 }
763 
764 static inline void ionic_write_cmb_desc(struct ionic_queue *q,
765 					void *desc)
766 {
767 	/* Since Rx and Tx descriptors are the same size, we can
768 	 * save an instruction or two and skip the qtype check.
769 	 */
770 	if (unlikely(q_to_qcq(q)->flags & IONIC_QCQ_F_CMB_RINGS))
771 		memcpy_toio(&q->cmb_txq[q->head_idx], desc, sizeof(q->cmb_txq[0]));
772 }
773 
774 void ionic_rx_fill(struct ionic_queue *q)
775 {
776 	struct net_device *netdev = q->lif->netdev;
777 	struct ionic_rx_desc_info *desc_info;
778 	struct ionic_rxq_sg_elem *sg_elem;
779 	struct ionic_buf_info *buf_info;
780 	unsigned int fill_threshold;
781 	struct ionic_rxq_desc *desc;
782 	unsigned int remain_len;
783 	unsigned int frag_len;
784 	unsigned int nfrags;
785 	unsigned int n_fill;
786 	unsigned int len;
787 	unsigned int i;
788 	unsigned int j;
789 
790 	n_fill = ionic_q_space_avail(q);
791 
792 	fill_threshold = min_t(unsigned int, IONIC_RX_FILL_THRESHOLD,
793 			       q->num_descs / IONIC_RX_FILL_DIV);
794 	if (n_fill < fill_threshold)
795 		return;
796 
797 	len = netdev->mtu + VLAN_ETH_HLEN;
798 
799 	for (i = n_fill; i; i--) {
800 		unsigned int headroom;
801 		unsigned int buf_len;
802 
803 		nfrags = 0;
804 		remain_len = len;
805 		desc = &q->rxq[q->head_idx];
806 		desc_info = &q->rx_info[q->head_idx];
807 		buf_info = &desc_info->bufs[0];
808 
809 		if (!buf_info->page) { /* alloc a new buffer? */
810 			if (unlikely(ionic_rx_page_alloc(q, buf_info))) {
811 				desc->addr = 0;
812 				desc->len = 0;
813 				return;
814 			}
815 		}
816 
817 		/* fill main descriptor - buf[0]
818 		 * XDP uses space in the first buffer, so account for
819 		 * head room, tail room, and ip header in the first frag size.
820 		 */
821 		headroom = q->xdp_rxq_info ? XDP_PACKET_HEADROOM : 0;
822 		if (q->xdp_rxq_info)
823 			buf_len = IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN;
824 		else
825 			buf_len = ionic_rx_buf_size(buf_info);
826 		frag_len = min_t(u16, len, buf_len);
827 
828 		desc->addr = cpu_to_le64(ionic_rx_buf_pa(buf_info) + headroom);
829 		desc->len = cpu_to_le16(frag_len);
830 		remain_len -= frag_len;
831 		buf_info++;
832 		nfrags++;
833 
834 		/* fill sg descriptors - buf[1..n] */
835 		sg_elem = q->rxq_sgl[q->head_idx].elems;
836 		for (j = 0; remain_len > 0 && j < q->max_sg_elems; j++, sg_elem++) {
837 			if (!buf_info->page) { /* alloc a new sg buffer? */
838 				if (unlikely(ionic_rx_page_alloc(q, buf_info))) {
839 					sg_elem->addr = 0;
840 					sg_elem->len = 0;
841 					return;
842 				}
843 			}
844 
845 			sg_elem->addr = cpu_to_le64(ionic_rx_buf_pa(buf_info));
846 			frag_len = min_t(u16, remain_len, ionic_rx_buf_size(buf_info));
847 			sg_elem->len = cpu_to_le16(frag_len);
848 			remain_len -= frag_len;
849 			buf_info++;
850 			nfrags++;
851 		}
852 
853 		/* clear end sg element as a sentinel */
854 		if (j < q->max_sg_elems)
855 			memset(sg_elem, 0, sizeof(*sg_elem));
856 
857 		desc->opcode = (nfrags > 1) ? IONIC_RXQ_DESC_OPCODE_SG :
858 					      IONIC_RXQ_DESC_OPCODE_SIMPLE;
859 		desc_info->nbufs = nfrags;
860 
861 		ionic_write_cmb_desc(q, desc);
862 
863 		ionic_rxq_post(q, false);
864 	}
865 
866 	ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
867 			 q->dbval | q->head_idx);
868 
869 	q->dbell_deadline = IONIC_RX_MIN_DOORBELL_DEADLINE;
870 	q->dbell_jiffies = jiffies;
871 
872 	mod_timer(&q_to_qcq(q)->napi_qcq->napi_deadline,
873 		  jiffies + IONIC_NAPI_DEADLINE);
874 }
875 
876 void ionic_rx_empty(struct ionic_queue *q)
877 {
878 	struct ionic_rx_desc_info *desc_info;
879 	struct ionic_buf_info *buf_info;
880 	unsigned int i, j;
881 
882 	for (i = 0; i < q->num_descs; i++) {
883 		desc_info = &q->rx_info[i];
884 		for (j = 0; j < ARRAY_SIZE(desc_info->bufs); j++) {
885 			buf_info = &desc_info->bufs[j];
886 			if (buf_info->page)
887 				ionic_rx_page_free(q, buf_info);
888 		}
889 
890 		desc_info->nbufs = 0;
891 	}
892 
893 	q->head_idx = 0;
894 	q->tail_idx = 0;
895 }
896 
897 static void ionic_dim_update(struct ionic_qcq *qcq, int napi_mode)
898 {
899 	struct dim_sample dim_sample;
900 	struct ionic_lif *lif;
901 	unsigned int qi;
902 	u64 pkts, bytes;
903 
904 	if (!qcq->intr.dim_coal_hw)
905 		return;
906 
907 	lif = qcq->q.lif;
908 	qi = qcq->cq.bound_q->index;
909 
910 	switch (napi_mode) {
911 	case IONIC_LIF_F_TX_DIM_INTR:
912 		pkts = lif->txqstats[qi].pkts;
913 		bytes = lif->txqstats[qi].bytes;
914 		break;
915 	case IONIC_LIF_F_RX_DIM_INTR:
916 		pkts = lif->rxqstats[qi].pkts;
917 		bytes = lif->rxqstats[qi].bytes;
918 		break;
919 	default:
920 		pkts = lif->txqstats[qi].pkts + lif->rxqstats[qi].pkts;
921 		bytes = lif->txqstats[qi].bytes + lif->rxqstats[qi].bytes;
922 		break;
923 	}
924 
925 	dim_update_sample(qcq->cq.bound_intr->rearm_count,
926 			  pkts, bytes, &dim_sample);
927 
928 	net_dim(&qcq->dim, dim_sample);
929 }
930 
931 int ionic_tx_napi(struct napi_struct *napi, int budget)
932 {
933 	struct ionic_qcq *qcq = napi_to_qcq(napi);
934 	struct ionic_cq *cq = napi_to_cq(napi);
935 	u32 work_done = 0;
936 	u32 flags = 0;
937 
938 	work_done = ionic_tx_cq_service(cq, budget);
939 
940 	if (unlikely(!budget))
941 		return budget;
942 
943 	if (work_done < budget && napi_complete_done(napi, work_done)) {
944 		ionic_dim_update(qcq, IONIC_LIF_F_TX_DIM_INTR);
945 		flags |= IONIC_INTR_CRED_UNMASK;
946 		cq->bound_intr->rearm_count++;
947 	}
948 
949 	if (work_done || flags) {
950 		flags |= IONIC_INTR_CRED_RESET_COALESCE;
951 		ionic_intr_credits(cq->idev->intr_ctrl,
952 				   cq->bound_intr->index,
953 				   work_done, flags);
954 	}
955 
956 	if (!work_done && ionic_txq_poke_doorbell(&qcq->q))
957 		mod_timer(&qcq->napi_deadline, jiffies + IONIC_NAPI_DEADLINE);
958 
959 	return work_done;
960 }
961 
962 static void ionic_xdp_do_flush(struct ionic_cq *cq)
963 {
964 	if (cq->bound_q->xdp_flush) {
965 		xdp_do_flush();
966 		cq->bound_q->xdp_flush = false;
967 	}
968 }
969 
970 int ionic_rx_napi(struct napi_struct *napi, int budget)
971 {
972 	struct ionic_qcq *qcq = napi_to_qcq(napi);
973 	struct ionic_cq *cq = napi_to_cq(napi);
974 	u32 work_done = 0;
975 	u32 flags = 0;
976 
977 	if (unlikely(!budget))
978 		return budget;
979 
980 	work_done = ionic_cq_service(cq, budget,
981 				     ionic_rx_service, NULL, NULL);
982 
983 	ionic_rx_fill(cq->bound_q);
984 
985 	ionic_xdp_do_flush(cq);
986 	if (work_done < budget && napi_complete_done(napi, work_done)) {
987 		ionic_dim_update(qcq, IONIC_LIF_F_RX_DIM_INTR);
988 		flags |= IONIC_INTR_CRED_UNMASK;
989 		cq->bound_intr->rearm_count++;
990 	}
991 
992 	if (work_done || flags) {
993 		flags |= IONIC_INTR_CRED_RESET_COALESCE;
994 		ionic_intr_credits(cq->idev->intr_ctrl,
995 				   cq->bound_intr->index,
996 				   work_done, flags);
997 	}
998 
999 	if (!work_done && ionic_rxq_poke_doorbell(&qcq->q))
1000 		mod_timer(&qcq->napi_deadline, jiffies + IONIC_NAPI_DEADLINE);
1001 
1002 	return work_done;
1003 }
1004 
1005 int ionic_txrx_napi(struct napi_struct *napi, int budget)
1006 {
1007 	struct ionic_qcq *rxqcq = napi_to_qcq(napi);
1008 	struct ionic_cq *rxcq = napi_to_cq(napi);
1009 	unsigned int qi = rxcq->bound_q->index;
1010 	struct ionic_qcq *txqcq;
1011 	struct ionic_lif *lif;
1012 	struct ionic_cq *txcq;
1013 	bool resched = false;
1014 	u32 rx_work_done = 0;
1015 	u32 tx_work_done = 0;
1016 	u32 flags = 0;
1017 
1018 	lif = rxcq->bound_q->lif;
1019 	txqcq = lif->txqcqs[qi];
1020 	txcq = &lif->txqcqs[qi]->cq;
1021 
1022 	tx_work_done = ionic_tx_cq_service(txcq, IONIC_TX_BUDGET_DEFAULT);
1023 
1024 	if (unlikely(!budget))
1025 		return budget;
1026 
1027 	rx_work_done = ionic_cq_service(rxcq, budget,
1028 					ionic_rx_service, NULL, NULL);
1029 
1030 	ionic_rx_fill(rxcq->bound_q);
1031 
1032 	ionic_xdp_do_flush(rxcq);
1033 	if (rx_work_done < budget && napi_complete_done(napi, rx_work_done)) {
1034 		ionic_dim_update(rxqcq, 0);
1035 		flags |= IONIC_INTR_CRED_UNMASK;
1036 		rxcq->bound_intr->rearm_count++;
1037 	}
1038 
1039 	if (rx_work_done || flags) {
1040 		flags |= IONIC_INTR_CRED_RESET_COALESCE;
1041 		ionic_intr_credits(rxcq->idev->intr_ctrl, rxcq->bound_intr->index,
1042 				   tx_work_done + rx_work_done, flags);
1043 	}
1044 
1045 	if (!rx_work_done && ionic_rxq_poke_doorbell(&rxqcq->q))
1046 		resched = true;
1047 	if (!tx_work_done && ionic_txq_poke_doorbell(&txqcq->q))
1048 		resched = true;
1049 	if (resched)
1050 		mod_timer(&rxqcq->napi_deadline, jiffies + IONIC_NAPI_DEADLINE);
1051 
1052 	return rx_work_done;
1053 }
1054 
1055 static dma_addr_t ionic_tx_map_single(struct ionic_queue *q,
1056 				      void *data, size_t len)
1057 {
1058 	struct device *dev = q->dev;
1059 	dma_addr_t dma_addr;
1060 
1061 	dma_addr = dma_map_single(dev, data, len, DMA_TO_DEVICE);
1062 	if (dma_mapping_error(dev, dma_addr)) {
1063 		net_warn_ratelimited("%s: DMA single map failed on %s!\n",
1064 				     dev_name(dev), q->name);
1065 		q_to_tx_stats(q)->dma_map_err++;
1066 		return 0;
1067 	}
1068 	return dma_addr;
1069 }
1070 
1071 static dma_addr_t ionic_tx_map_frag(struct ionic_queue *q,
1072 				    const skb_frag_t *frag,
1073 				    size_t offset, size_t len)
1074 {
1075 	struct device *dev = q->dev;
1076 	dma_addr_t dma_addr;
1077 
1078 	dma_addr = skb_frag_dma_map(dev, frag, offset, len, DMA_TO_DEVICE);
1079 	if (dma_mapping_error(dev, dma_addr)) {
1080 		net_warn_ratelimited("%s: DMA frag map failed on %s!\n",
1081 				     dev_name(dev), q->name);
1082 		q_to_tx_stats(q)->dma_map_err++;
1083 		return 0;
1084 	}
1085 	return dma_addr;
1086 }
1087 
1088 static int ionic_tx_map_skb(struct ionic_queue *q, struct sk_buff *skb,
1089 			    struct ionic_tx_desc_info *desc_info)
1090 {
1091 	struct ionic_buf_info *buf_info = desc_info->bufs;
1092 	struct device *dev = q->dev;
1093 	dma_addr_t dma_addr;
1094 	unsigned int nfrags;
1095 	skb_frag_t *frag;
1096 	int frag_idx;
1097 
1098 	dma_addr = ionic_tx_map_single(q, skb->data, skb_headlen(skb));
1099 	if (!dma_addr)
1100 		return -EIO;
1101 	buf_info->dma_addr = dma_addr;
1102 	buf_info->len = skb_headlen(skb);
1103 	buf_info++;
1104 
1105 	frag = skb_shinfo(skb)->frags;
1106 	nfrags = skb_shinfo(skb)->nr_frags;
1107 	for (frag_idx = 0; frag_idx < nfrags; frag_idx++, frag++) {
1108 		dma_addr = ionic_tx_map_frag(q, frag, 0, skb_frag_size(frag));
1109 		if (!dma_addr)
1110 			goto dma_fail;
1111 		buf_info->dma_addr = dma_addr;
1112 		buf_info->len = skb_frag_size(frag);
1113 		buf_info++;
1114 	}
1115 
1116 	desc_info->nbufs = 1 + nfrags;
1117 
1118 	return 0;
1119 
1120 dma_fail:
1121 	/* unwind the frag mappings and the head mapping */
1122 	while (frag_idx > 0) {
1123 		frag_idx--;
1124 		buf_info--;
1125 		dma_unmap_page(dev, buf_info->dma_addr,
1126 			       buf_info->len, DMA_TO_DEVICE);
1127 	}
1128 	dma_unmap_single(dev, desc_info->bufs[0].dma_addr,
1129 			 desc_info->bufs[0].len, DMA_TO_DEVICE);
1130 	return -EIO;
1131 }
1132 
1133 static void ionic_tx_desc_unmap_bufs(struct ionic_queue *q,
1134 				     struct ionic_tx_desc_info *desc_info)
1135 {
1136 	struct ionic_buf_info *buf_info = desc_info->bufs;
1137 	struct device *dev = q->dev;
1138 	unsigned int i;
1139 
1140 	if (!desc_info->nbufs)
1141 		return;
1142 
1143 	dma_unmap_single(dev, buf_info->dma_addr,
1144 			 buf_info->len, DMA_TO_DEVICE);
1145 	buf_info++;
1146 	for (i = 1; i < desc_info->nbufs; i++, buf_info++)
1147 		dma_unmap_page(dev, buf_info->dma_addr,
1148 			       buf_info->len, DMA_TO_DEVICE);
1149 
1150 	desc_info->nbufs = 0;
1151 }
1152 
1153 static void ionic_tx_clean(struct ionic_queue *q,
1154 			   struct ionic_tx_desc_info *desc_info,
1155 			   struct ionic_txq_comp *comp)
1156 {
1157 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1158 	struct ionic_qcq *qcq = q_to_qcq(q);
1159 	struct sk_buff *skb;
1160 
1161 	if (desc_info->xdpf) {
1162 		ionic_xdp_tx_desc_clean(q->partner, desc_info);
1163 		stats->clean++;
1164 
1165 		if (unlikely(__netif_subqueue_stopped(q->lif->netdev, q->index)))
1166 			netif_wake_subqueue(q->lif->netdev, q->index);
1167 
1168 		return;
1169 	}
1170 
1171 	ionic_tx_desc_unmap_bufs(q, desc_info);
1172 
1173 	skb = desc_info->skb;
1174 	if (!skb)
1175 		return;
1176 
1177 	if (unlikely(ionic_txq_hwstamp_enabled(q))) {
1178 		if (comp) {
1179 			struct skb_shared_hwtstamps hwts = {};
1180 			__le64 *cq_desc_hwstamp;
1181 			u64 hwstamp;
1182 
1183 			cq_desc_hwstamp =
1184 				(void *)comp +
1185 				qcq->cq.desc_size -
1186 				sizeof(struct ionic_txq_comp) -
1187 				IONIC_HWSTAMP_CQ_NEGOFFSET;
1188 
1189 			hwstamp = le64_to_cpu(*cq_desc_hwstamp);
1190 
1191 			if (hwstamp != IONIC_HWSTAMP_INVALID) {
1192 				hwts.hwtstamp = ionic_lif_phc_ktime(q->lif, hwstamp);
1193 
1194 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1195 				skb_tstamp_tx(skb, &hwts);
1196 
1197 				stats->hwstamp_valid++;
1198 			} else {
1199 				stats->hwstamp_invalid++;
1200 			}
1201 		}
1202 	}
1203 
1204 	desc_info->bytes = skb->len;
1205 	stats->clean++;
1206 
1207 	napi_consume_skb(skb, 1);
1208 }
1209 
1210 static bool ionic_tx_service(struct ionic_cq *cq,
1211 			     unsigned int *total_pkts, unsigned int *total_bytes)
1212 {
1213 	struct ionic_tx_desc_info *desc_info;
1214 	struct ionic_queue *q = cq->bound_q;
1215 	struct ionic_txq_comp *comp;
1216 	unsigned int bytes = 0;
1217 	unsigned int pkts = 0;
1218 	u16 index;
1219 
1220 	comp = &((struct ionic_txq_comp *)cq->base)[cq->tail_idx];
1221 
1222 	if (!color_match(comp->color, cq->done_color))
1223 		return false;
1224 
1225 	/* clean the related q entries, there could be
1226 	 * several q entries completed for each cq completion
1227 	 */
1228 	do {
1229 		desc_info = &q->tx_info[q->tail_idx];
1230 		desc_info->bytes = 0;
1231 		index = q->tail_idx;
1232 		q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
1233 		ionic_tx_clean(q, desc_info, comp);
1234 		if (desc_info->skb) {
1235 			pkts++;
1236 			bytes += desc_info->bytes;
1237 			desc_info->skb = NULL;
1238 		}
1239 	} while (index != le16_to_cpu(comp->comp_index));
1240 
1241 	(*total_pkts) += pkts;
1242 	(*total_bytes) += bytes;
1243 
1244 	return true;
1245 }
1246 
1247 unsigned int ionic_tx_cq_service(struct ionic_cq *cq, unsigned int work_to_do)
1248 {
1249 	unsigned int work_done = 0;
1250 	unsigned int bytes = 0;
1251 	unsigned int pkts = 0;
1252 
1253 	if (work_to_do == 0)
1254 		return 0;
1255 
1256 	while (ionic_tx_service(cq, &pkts, &bytes)) {
1257 		if (cq->tail_idx == cq->num_descs - 1)
1258 			cq->done_color = !cq->done_color;
1259 		cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
1260 
1261 		if (++work_done >= work_to_do)
1262 			break;
1263 	}
1264 
1265 	if (work_done) {
1266 		struct ionic_queue *q = cq->bound_q;
1267 
1268 		if (likely(!ionic_txq_hwstamp_enabled(q)))
1269 			netif_txq_completed_wake(q_to_ndq(q->lif->netdev, q),
1270 						 pkts, bytes,
1271 						 ionic_q_space_avail(q),
1272 						 IONIC_TSO_DESCS_NEEDED);
1273 	}
1274 
1275 	return work_done;
1276 }
1277 
1278 void ionic_tx_flush(struct ionic_cq *cq)
1279 {
1280 	u32 work_done;
1281 
1282 	work_done = ionic_tx_cq_service(cq, cq->num_descs);
1283 	if (work_done)
1284 		ionic_intr_credits(cq->idev->intr_ctrl, cq->bound_intr->index,
1285 				   work_done, IONIC_INTR_CRED_RESET_COALESCE);
1286 }
1287 
1288 void ionic_tx_empty(struct ionic_queue *q)
1289 {
1290 	struct ionic_tx_desc_info *desc_info;
1291 	int bytes = 0;
1292 	int pkts = 0;
1293 
1294 	/* walk the not completed tx entries, if any */
1295 	while (q->head_idx != q->tail_idx) {
1296 		desc_info = &q->tx_info[q->tail_idx];
1297 		desc_info->bytes = 0;
1298 		q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
1299 		ionic_tx_clean(q, desc_info, NULL);
1300 		if (desc_info->skb) {
1301 			pkts++;
1302 			bytes += desc_info->bytes;
1303 			desc_info->skb = NULL;
1304 		}
1305 	}
1306 
1307 	if (likely(!ionic_txq_hwstamp_enabled(q))) {
1308 		struct netdev_queue *ndq = q_to_ndq(q->lif->netdev, q);
1309 
1310 		netdev_tx_completed_queue(ndq, pkts, bytes);
1311 		netdev_tx_reset_queue(ndq);
1312 	}
1313 }
1314 
1315 static int ionic_tx_tcp_inner_pseudo_csum(struct sk_buff *skb)
1316 {
1317 	int err;
1318 
1319 	err = skb_cow_head(skb, 0);
1320 	if (err)
1321 		return err;
1322 
1323 	if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
1324 		inner_ip_hdr(skb)->check = 0;
1325 		inner_tcp_hdr(skb)->check =
1326 			~csum_tcpudp_magic(inner_ip_hdr(skb)->saddr,
1327 					   inner_ip_hdr(skb)->daddr,
1328 					   0, IPPROTO_TCP, 0);
1329 	} else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
1330 		inner_tcp_hdr(skb)->check =
1331 			~csum_ipv6_magic(&inner_ipv6_hdr(skb)->saddr,
1332 					 &inner_ipv6_hdr(skb)->daddr,
1333 					 0, IPPROTO_TCP, 0);
1334 	}
1335 
1336 	return 0;
1337 }
1338 
1339 static int ionic_tx_tcp_pseudo_csum(struct sk_buff *skb)
1340 {
1341 	int err;
1342 
1343 	err = skb_cow_head(skb, 0);
1344 	if (err)
1345 		return err;
1346 
1347 	if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
1348 		ip_hdr(skb)->check = 0;
1349 		tcp_hdr(skb)->check =
1350 			~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1351 					   ip_hdr(skb)->daddr,
1352 					   0, IPPROTO_TCP, 0);
1353 	} else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
1354 		tcp_v6_gso_csum_prep(skb);
1355 	}
1356 
1357 	return 0;
1358 }
1359 
1360 static void ionic_tx_tso_post(struct net_device *netdev, struct ionic_queue *q,
1361 			      struct ionic_tx_desc_info *desc_info,
1362 			      struct sk_buff *skb,
1363 			      dma_addr_t addr, u8 nsge, u16 len,
1364 			      unsigned int hdrlen, unsigned int mss,
1365 			      bool outer_csum,
1366 			      u16 vlan_tci, bool has_vlan,
1367 			      bool start, bool done)
1368 {
1369 	struct ionic_txq_desc *desc = &q->txq[q->head_idx];
1370 	u8 flags = 0;
1371 	u64 cmd;
1372 
1373 	flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1374 	flags |= outer_csum ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1375 	flags |= start ? IONIC_TXQ_DESC_FLAG_TSO_SOT : 0;
1376 	flags |= done ? IONIC_TXQ_DESC_FLAG_TSO_EOT : 0;
1377 
1378 	cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_TSO, flags, nsge, addr);
1379 	desc->cmd = cpu_to_le64(cmd);
1380 	desc->len = cpu_to_le16(len);
1381 	desc->vlan_tci = cpu_to_le16(vlan_tci);
1382 	desc->hdr_len = cpu_to_le16(hdrlen);
1383 	desc->mss = cpu_to_le16(mss);
1384 
1385 	ionic_write_cmb_desc(q, desc);
1386 
1387 	if (start) {
1388 		skb_tx_timestamp(skb);
1389 		if (likely(!ionic_txq_hwstamp_enabled(q)))
1390 			netdev_tx_sent_queue(q_to_ndq(netdev, q), skb->len);
1391 		ionic_txq_post(q, false);
1392 	} else {
1393 		ionic_txq_post(q, done);
1394 	}
1395 }
1396 
1397 static int ionic_tx_tso(struct net_device *netdev, struct ionic_queue *q,
1398 			struct sk_buff *skb)
1399 {
1400 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1401 	struct ionic_tx_desc_info *desc_info;
1402 	struct ionic_buf_info *buf_info;
1403 	struct ionic_txq_sg_elem *elem;
1404 	struct ionic_txq_desc *desc;
1405 	unsigned int chunk_len;
1406 	unsigned int frag_rem;
1407 	unsigned int tso_rem;
1408 	unsigned int seg_rem;
1409 	dma_addr_t desc_addr;
1410 	dma_addr_t frag_addr;
1411 	unsigned int hdrlen;
1412 	unsigned int len;
1413 	unsigned int mss;
1414 	bool start, done;
1415 	bool outer_csum;
1416 	bool has_vlan;
1417 	u16 desc_len;
1418 	u8 desc_nsge;
1419 	u16 vlan_tci;
1420 	bool encap;
1421 	int err;
1422 
1423 	desc_info = &q->tx_info[q->head_idx];
1424 
1425 	if (unlikely(ionic_tx_map_skb(q, skb, desc_info)))
1426 		return -EIO;
1427 
1428 	len = skb->len;
1429 	mss = skb_shinfo(skb)->gso_size;
1430 	outer_csum = (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
1431 						   SKB_GSO_GRE_CSUM |
1432 						   SKB_GSO_IPXIP4 |
1433 						   SKB_GSO_IPXIP6 |
1434 						   SKB_GSO_UDP_TUNNEL |
1435 						   SKB_GSO_UDP_TUNNEL_CSUM));
1436 	has_vlan = !!skb_vlan_tag_present(skb);
1437 	vlan_tci = skb_vlan_tag_get(skb);
1438 	encap = skb->encapsulation;
1439 
1440 	/* Preload inner-most TCP csum field with IP pseudo hdr
1441 	 * calculated with IP length set to zero.  HW will later
1442 	 * add in length to each TCP segment resulting from the TSO.
1443 	 */
1444 
1445 	if (encap)
1446 		err = ionic_tx_tcp_inner_pseudo_csum(skb);
1447 	else
1448 		err = ionic_tx_tcp_pseudo_csum(skb);
1449 	if (err) {
1450 		/* clean up mapping from ionic_tx_map_skb */
1451 		ionic_tx_desc_unmap_bufs(q, desc_info);
1452 		return err;
1453 	}
1454 
1455 	if (encap)
1456 		hdrlen = skb_inner_tcp_all_headers(skb);
1457 	else
1458 		hdrlen = skb_tcp_all_headers(skb);
1459 
1460 	desc_info->skb = skb;
1461 	buf_info = desc_info->bufs;
1462 	tso_rem = len;
1463 	seg_rem = min(tso_rem, hdrlen + mss);
1464 
1465 	frag_addr = 0;
1466 	frag_rem = 0;
1467 
1468 	start = true;
1469 
1470 	while (tso_rem > 0) {
1471 		desc = NULL;
1472 		elem = NULL;
1473 		desc_addr = 0;
1474 		desc_len = 0;
1475 		desc_nsge = 0;
1476 		/* use fragments until we have enough to post a single descriptor */
1477 		while (seg_rem > 0) {
1478 			/* if the fragment is exhausted then move to the next one */
1479 			if (frag_rem == 0) {
1480 				/* grab the next fragment */
1481 				frag_addr = buf_info->dma_addr;
1482 				frag_rem = buf_info->len;
1483 				buf_info++;
1484 			}
1485 			chunk_len = min(frag_rem, seg_rem);
1486 			if (!desc) {
1487 				/* fill main descriptor */
1488 				desc = &q->txq[q->head_idx];
1489 				elem = ionic_tx_sg_elems(q);
1490 				desc_addr = frag_addr;
1491 				desc_len = chunk_len;
1492 			} else {
1493 				/* fill sg descriptor */
1494 				elem->addr = cpu_to_le64(frag_addr);
1495 				elem->len = cpu_to_le16(chunk_len);
1496 				elem++;
1497 				desc_nsge++;
1498 			}
1499 			frag_addr += chunk_len;
1500 			frag_rem -= chunk_len;
1501 			tso_rem -= chunk_len;
1502 			seg_rem -= chunk_len;
1503 		}
1504 		seg_rem = min(tso_rem, mss);
1505 		done = (tso_rem == 0);
1506 		/* post descriptor */
1507 		ionic_tx_tso_post(netdev, q, desc_info, skb,
1508 				  desc_addr, desc_nsge, desc_len,
1509 				  hdrlen, mss, outer_csum, vlan_tci, has_vlan,
1510 				  start, done);
1511 		start = false;
1512 		/* Buffer information is stored with the first tso descriptor */
1513 		desc_info = &q->tx_info[q->head_idx];
1514 		desc_info->nbufs = 0;
1515 	}
1516 
1517 	stats->pkts += DIV_ROUND_UP(len - hdrlen, mss);
1518 	stats->bytes += len;
1519 	stats->tso++;
1520 	stats->tso_bytes = len;
1521 
1522 	return 0;
1523 }
1524 
1525 static void ionic_tx_calc_csum(struct ionic_queue *q, struct sk_buff *skb,
1526 			       struct ionic_tx_desc_info *desc_info)
1527 {
1528 	struct ionic_txq_desc *desc = &q->txq[q->head_idx];
1529 	struct ionic_buf_info *buf_info = desc_info->bufs;
1530 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1531 	bool has_vlan;
1532 	u8 flags = 0;
1533 	bool encap;
1534 	u64 cmd;
1535 
1536 	has_vlan = !!skb_vlan_tag_present(skb);
1537 	encap = skb->encapsulation;
1538 
1539 	flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1540 	flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1541 
1542 	cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_PARTIAL,
1543 				  flags, skb_shinfo(skb)->nr_frags,
1544 				  buf_info->dma_addr);
1545 	desc->cmd = cpu_to_le64(cmd);
1546 	desc->len = cpu_to_le16(buf_info->len);
1547 	if (has_vlan) {
1548 		desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb));
1549 		stats->vlan_inserted++;
1550 	} else {
1551 		desc->vlan_tci = 0;
1552 	}
1553 	desc->csum_start = cpu_to_le16(skb_checksum_start_offset(skb));
1554 	desc->csum_offset = cpu_to_le16(skb->csum_offset);
1555 
1556 	ionic_write_cmb_desc(q, desc);
1557 
1558 	if (skb_csum_is_sctp(skb))
1559 		stats->crc32_csum++;
1560 	else
1561 		stats->csum++;
1562 }
1563 
1564 static void ionic_tx_calc_no_csum(struct ionic_queue *q, struct sk_buff *skb,
1565 				  struct ionic_tx_desc_info *desc_info)
1566 {
1567 	struct ionic_txq_desc *desc = &q->txq[q->head_idx];
1568 	struct ionic_buf_info *buf_info = desc_info->bufs;
1569 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1570 	bool has_vlan;
1571 	u8 flags = 0;
1572 	bool encap;
1573 	u64 cmd;
1574 
1575 	has_vlan = !!skb_vlan_tag_present(skb);
1576 	encap = skb->encapsulation;
1577 
1578 	flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1579 	flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1580 
1581 	cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_NONE,
1582 				  flags, skb_shinfo(skb)->nr_frags,
1583 				  buf_info->dma_addr);
1584 	desc->cmd = cpu_to_le64(cmd);
1585 	desc->len = cpu_to_le16(buf_info->len);
1586 	if (has_vlan) {
1587 		desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb));
1588 		stats->vlan_inserted++;
1589 	} else {
1590 		desc->vlan_tci = 0;
1591 	}
1592 	desc->csum_start = 0;
1593 	desc->csum_offset = 0;
1594 
1595 	ionic_write_cmb_desc(q, desc);
1596 
1597 	stats->csum_none++;
1598 }
1599 
1600 static void ionic_tx_skb_frags(struct ionic_queue *q, struct sk_buff *skb,
1601 			       struct ionic_tx_desc_info *desc_info)
1602 {
1603 	struct ionic_buf_info *buf_info = &desc_info->bufs[1];
1604 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1605 	struct ionic_txq_sg_elem *elem;
1606 	unsigned int i;
1607 
1608 	elem = ionic_tx_sg_elems(q);
1609 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, buf_info++, elem++) {
1610 		elem->addr = cpu_to_le64(buf_info->dma_addr);
1611 		elem->len = cpu_to_le16(buf_info->len);
1612 	}
1613 
1614 	stats->frags += skb_shinfo(skb)->nr_frags;
1615 }
1616 
1617 static int ionic_tx(struct net_device *netdev, struct ionic_queue *q,
1618 		    struct sk_buff *skb)
1619 {
1620 	struct ionic_tx_desc_info *desc_info = &q->tx_info[q->head_idx];
1621 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1622 	bool ring_dbell = true;
1623 
1624 	if (unlikely(ionic_tx_map_skb(q, skb, desc_info)))
1625 		return -EIO;
1626 
1627 	desc_info->skb = skb;
1628 
1629 	/* set up the initial descriptor */
1630 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1631 		ionic_tx_calc_csum(q, skb, desc_info);
1632 	else
1633 		ionic_tx_calc_no_csum(q, skb, desc_info);
1634 
1635 	/* add frags */
1636 	ionic_tx_skb_frags(q, skb, desc_info);
1637 
1638 	skb_tx_timestamp(skb);
1639 	stats->pkts++;
1640 	stats->bytes += skb->len;
1641 
1642 	if (likely(!ionic_txq_hwstamp_enabled(q))) {
1643 		struct netdev_queue *ndq = q_to_ndq(netdev, q);
1644 
1645 		if (unlikely(!ionic_q_has_space(q, MAX_SKB_FRAGS + 1)))
1646 			netif_tx_stop_queue(ndq);
1647 		ring_dbell = __netdev_tx_sent_queue(ndq, skb->len,
1648 						    netdev_xmit_more());
1649 	}
1650 	ionic_txq_post(q, ring_dbell);
1651 
1652 	return 0;
1653 }
1654 
1655 static int ionic_tx_descs_needed(struct ionic_queue *q, struct sk_buff *skb)
1656 {
1657 	int nr_frags = skb_shinfo(skb)->nr_frags;
1658 	bool too_many_frags = false;
1659 	skb_frag_t *frag;
1660 	int desc_bufs;
1661 	int chunk_len;
1662 	int frag_rem;
1663 	int tso_rem;
1664 	int seg_rem;
1665 	bool encap;
1666 	int hdrlen;
1667 	int ndescs;
1668 	int err;
1669 
1670 	/* Each desc is mss long max, so a descriptor for each gso_seg */
1671 	if (skb_is_gso(skb)) {
1672 		ndescs = skb_shinfo(skb)->gso_segs;
1673 		if (!nr_frags)
1674 			return ndescs;
1675 	} else {
1676 		ndescs = 1;
1677 		if (!nr_frags)
1678 			return ndescs;
1679 
1680 		if (unlikely(nr_frags > q->max_sg_elems)) {
1681 			too_many_frags = true;
1682 			goto linearize;
1683 		}
1684 
1685 		return ndescs;
1686 	}
1687 
1688 	/* We need to scan the skb to be sure that none of the MTU sized
1689 	 * packets in the TSO will require more sgs per descriptor than we
1690 	 * can support.  We loop through the frags, add up the lengths for
1691 	 * a packet, and count the number of sgs used per packet.
1692 	 */
1693 	tso_rem = skb->len;
1694 	frag = skb_shinfo(skb)->frags;
1695 	encap = skb->encapsulation;
1696 
1697 	/* start with just hdr in first part of first descriptor */
1698 	if (encap)
1699 		hdrlen = skb_inner_tcp_all_headers(skb);
1700 	else
1701 		hdrlen = skb_tcp_all_headers(skb);
1702 	seg_rem = min_t(int, tso_rem, hdrlen + skb_shinfo(skb)->gso_size);
1703 	frag_rem = hdrlen;
1704 
1705 	while (tso_rem > 0) {
1706 		desc_bufs = 0;
1707 		while (seg_rem > 0) {
1708 			desc_bufs++;
1709 
1710 			/* We add the +1 because we can take buffers for one
1711 			 * more than we have SGs: one for the initial desc data
1712 			 * in addition to the SG segments that might follow.
1713 			 */
1714 			if (desc_bufs > q->max_sg_elems + 1) {
1715 				too_many_frags = true;
1716 				goto linearize;
1717 			}
1718 
1719 			if (frag_rem == 0) {
1720 				frag_rem = skb_frag_size(frag);
1721 				frag++;
1722 			}
1723 			chunk_len = min(frag_rem, seg_rem);
1724 			frag_rem -= chunk_len;
1725 			tso_rem -= chunk_len;
1726 			seg_rem -= chunk_len;
1727 		}
1728 
1729 		seg_rem = min_t(int, tso_rem, skb_shinfo(skb)->gso_size);
1730 	}
1731 
1732 linearize:
1733 	if (too_many_frags) {
1734 		err = skb_linearize(skb);
1735 		if (err)
1736 			return err;
1737 		q_to_tx_stats(q)->linearize++;
1738 	}
1739 
1740 	return ndescs;
1741 }
1742 
1743 static netdev_tx_t ionic_start_hwstamp_xmit(struct sk_buff *skb,
1744 					    struct net_device *netdev)
1745 {
1746 	struct ionic_lif *lif = netdev_priv(netdev);
1747 	struct ionic_queue *q;
1748 	int err, ndescs;
1749 
1750 	/* Does not stop/start txq, because we post to a separate tx queue
1751 	 * for timestamping, and if a packet can't be posted immediately to
1752 	 * the timestamping queue, it is dropped.
1753 	 */
1754 
1755 	q = &lif->hwstamp_txq->q;
1756 	ndescs = ionic_tx_descs_needed(q, skb);
1757 	if (unlikely(ndescs < 0))
1758 		goto err_out_drop;
1759 
1760 	if (unlikely(!ionic_q_has_space(q, ndescs)))
1761 		goto err_out_drop;
1762 
1763 	skb_shinfo(skb)->tx_flags |= SKBTX_HW_TSTAMP;
1764 	if (skb_is_gso(skb))
1765 		err = ionic_tx_tso(netdev, q, skb);
1766 	else
1767 		err = ionic_tx(netdev, q, skb);
1768 
1769 	if (err)
1770 		goto err_out_drop;
1771 
1772 	return NETDEV_TX_OK;
1773 
1774 err_out_drop:
1775 	q->drop++;
1776 	dev_kfree_skb(skb);
1777 	return NETDEV_TX_OK;
1778 }
1779 
1780 netdev_tx_t ionic_start_xmit(struct sk_buff *skb, struct net_device *netdev)
1781 {
1782 	u16 queue_index = skb_get_queue_mapping(skb);
1783 	struct ionic_lif *lif = netdev_priv(netdev);
1784 	struct ionic_queue *q;
1785 	int ndescs;
1786 	int err;
1787 
1788 	if (unlikely(!test_bit(IONIC_LIF_F_UP, lif->state))) {
1789 		dev_kfree_skb(skb);
1790 		return NETDEV_TX_OK;
1791 	}
1792 
1793 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
1794 		if (lif->hwstamp_txq && lif->phc->ts_config_tx_mode)
1795 			return ionic_start_hwstamp_xmit(skb, netdev);
1796 
1797 	if (unlikely(queue_index >= lif->nxqs))
1798 		queue_index = 0;
1799 	q = &lif->txqcqs[queue_index]->q;
1800 
1801 	ndescs = ionic_tx_descs_needed(q, skb);
1802 	if (ndescs < 0)
1803 		goto err_out_drop;
1804 
1805 	if (!netif_txq_maybe_stop(q_to_ndq(netdev, q),
1806 				  ionic_q_space_avail(q),
1807 				  ndescs, ndescs))
1808 		return NETDEV_TX_BUSY;
1809 
1810 	if (skb_is_gso(skb))
1811 		err = ionic_tx_tso(netdev, q, skb);
1812 	else
1813 		err = ionic_tx(netdev, q, skb);
1814 
1815 	if (err)
1816 		goto err_out_drop;
1817 
1818 	return NETDEV_TX_OK;
1819 
1820 err_out_drop:
1821 	q->drop++;
1822 	dev_kfree_skb(skb);
1823 	return NETDEV_TX_OK;
1824 }
1825