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