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