• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 #include "ionic.h"
10 #include "ionic_lif.h"
11 #include "ionic_txrx.h"
12 
ionic_txq_post(struct ionic_queue * q,bool ring_dbell,ionic_desc_cb cb_func,void * cb_arg)13 static inline void ionic_txq_post(struct ionic_queue *q, bool ring_dbell,
14 				  ionic_desc_cb cb_func, void *cb_arg)
15 {
16 	ionic_q_post(q, ring_dbell, cb_func, cb_arg);
17 }
18 
ionic_rxq_post(struct ionic_queue * q,bool ring_dbell,ionic_desc_cb cb_func,void * cb_arg)19 static inline void ionic_rxq_post(struct ionic_queue *q, bool ring_dbell,
20 				  ionic_desc_cb cb_func, void *cb_arg)
21 {
22 	ionic_q_post(q, ring_dbell, cb_func, cb_arg);
23 }
24 
ionic_txq_poke_doorbell(struct ionic_queue * q)25 bool ionic_txq_poke_doorbell(struct ionic_queue *q)
26 {
27 	unsigned long now, then, dif;
28 	struct netdev_queue *netdev_txq;
29 	struct net_device *netdev;
30 
31 	netdev = q->lif->netdev;
32 	netdev_txq = netdev_get_tx_queue(netdev, q->index);
33 
34 	HARD_TX_LOCK(netdev, netdev_txq, smp_processor_id());
35 
36 	if (q->tail_idx == q->head_idx) {
37 		HARD_TX_UNLOCK(netdev, netdev_txq);
38 		return false;
39 	}
40 
41 	now = READ_ONCE(jiffies);
42 	then = q->dbell_jiffies;
43 	dif = now - then;
44 
45 	if (dif > q->dbell_deadline) {
46 		ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
47 				 q->dbval | q->head_idx);
48 
49 		q->dbell_jiffies = now;
50 	}
51 
52 	HARD_TX_UNLOCK(netdev, netdev_txq);
53 
54 	return true;
55 }
56 
ionic_rxq_poke_doorbell(struct ionic_queue * q)57 bool ionic_rxq_poke_doorbell(struct ionic_queue *q)
58 {
59 	unsigned long now, then, dif;
60 
61 	/* no lock, called from rx napi or txrx napi, nothing else can fill */
62 
63 	if (q->tail_idx == q->head_idx)
64 		return false;
65 
66 	now = READ_ONCE(jiffies);
67 	then = q->dbell_jiffies;
68 	dif = now - then;
69 
70 	if (dif > q->dbell_deadline) {
71 		ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
72 				 q->dbval | q->head_idx);
73 
74 		q->dbell_jiffies = now;
75 
76 		dif = 2 * q->dbell_deadline;
77 		if (dif > IONIC_RX_MAX_DOORBELL_DEADLINE)
78 			dif = IONIC_RX_MAX_DOORBELL_DEADLINE;
79 
80 		q->dbell_deadline = dif;
81 	}
82 
83 	return true;
84 }
85 
q_to_ndq(struct ionic_queue * q)86 static inline struct netdev_queue *q_to_ndq(struct ionic_queue *q)
87 {
88 	return netdev_get_tx_queue(q->lif->netdev, q->index);
89 }
90 
ionic_rx_page_alloc(struct ionic_queue * q,struct ionic_buf_info * buf_info)91 static int ionic_rx_page_alloc(struct ionic_queue *q,
92 			       struct ionic_buf_info *buf_info)
93 {
94 	struct net_device *netdev = q->lif->netdev;
95 	struct ionic_rx_stats *stats;
96 	struct device *dev;
97 	struct page *page;
98 
99 	dev = q->dev;
100 	stats = q_to_rx_stats(q);
101 
102 	if (unlikely(!buf_info)) {
103 		net_err_ratelimited("%s: %s invalid buf_info in alloc\n",
104 				    netdev->name, q->name);
105 		return -EINVAL;
106 	}
107 
108 	page = alloc_pages(IONIC_PAGE_GFP_MASK, 0);
109 	if (unlikely(!page)) {
110 		net_err_ratelimited("%s: %s page alloc failed\n",
111 				    netdev->name, q->name);
112 		stats->alloc_err++;
113 		return -ENOMEM;
114 	}
115 
116 	buf_info->dma_addr = dma_map_page(dev, page, 0,
117 					  IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
118 	if (unlikely(dma_mapping_error(dev, buf_info->dma_addr))) {
119 		__free_pages(page, 0);
120 		net_err_ratelimited("%s: %s dma map failed\n",
121 				    netdev->name, q->name);
122 		stats->dma_map_err++;
123 		return -EIO;
124 	}
125 
126 	buf_info->page = page;
127 	buf_info->page_offset = 0;
128 
129 	return 0;
130 }
131 
ionic_rx_page_free(struct ionic_queue * q,struct ionic_buf_info * buf_info)132 static void ionic_rx_page_free(struct ionic_queue *q,
133 			       struct ionic_buf_info *buf_info)
134 {
135 	struct net_device *netdev = q->lif->netdev;
136 	struct device *dev = q->dev;
137 
138 	if (unlikely(!buf_info)) {
139 		net_err_ratelimited("%s: %s invalid buf_info in free\n",
140 				    netdev->name, q->name);
141 		return;
142 	}
143 
144 	if (!buf_info->page)
145 		return;
146 
147 	dma_unmap_page(dev, buf_info->dma_addr, IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
148 	__free_pages(buf_info->page, 0);
149 	buf_info->page = NULL;
150 }
151 
ionic_rx_buf_recycle(struct ionic_queue * q,struct ionic_buf_info * buf_info,u32 used)152 static bool ionic_rx_buf_recycle(struct ionic_queue *q,
153 				 struct ionic_buf_info *buf_info, u32 used)
154 {
155 	u32 size;
156 
157 	/* don't re-use pages allocated in low-mem condition */
158 	if (page_is_pfmemalloc(buf_info->page))
159 		return false;
160 
161 	/* don't re-use buffers from non-local numa nodes */
162 	if (page_to_nid(buf_info->page) != numa_mem_id())
163 		return false;
164 
165 	size = ALIGN(used, IONIC_PAGE_SPLIT_SZ);
166 	buf_info->page_offset += size;
167 	if (buf_info->page_offset >= IONIC_PAGE_SIZE)
168 		return false;
169 
170 	get_page(buf_info->page);
171 
172 	return true;
173 }
174 
ionic_rx_frags(struct ionic_queue * q,struct ionic_desc_info * desc_info,struct ionic_rxq_comp * comp)175 static struct sk_buff *ionic_rx_frags(struct ionic_queue *q,
176 				      struct ionic_desc_info *desc_info,
177 				      struct ionic_rxq_comp *comp)
178 {
179 	struct net_device *netdev = q->lif->netdev;
180 	struct ionic_buf_info *buf_info;
181 	struct ionic_rx_stats *stats;
182 	struct device *dev = q->dev;
183 	struct sk_buff *skb;
184 	unsigned int i;
185 	u16 frag_len;
186 	u16 len;
187 
188 	stats = q_to_rx_stats(q);
189 
190 	buf_info = &desc_info->bufs[0];
191 	len = le16_to_cpu(comp->len);
192 
193 	prefetchw(buf_info->page);
194 
195 	skb = napi_get_frags(&q_to_qcq(q)->napi);
196 	if (unlikely(!skb)) {
197 		net_warn_ratelimited("%s: SKB alloc failed on %s!\n",
198 				     netdev->name, q->name);
199 		stats->alloc_err++;
200 		return NULL;
201 	}
202 
203 	i = comp->num_sg_elems + 1;
204 	do {
205 		if (unlikely(!buf_info->page)) {
206 			dev_kfree_skb(skb);
207 			return NULL;
208 		}
209 
210 		frag_len = min_t(u16, len, min_t(u32, IONIC_MAX_BUF_LEN,
211 						 IONIC_PAGE_SIZE - buf_info->page_offset));
212 		len -= frag_len;
213 
214 		dma_sync_single_for_cpu(dev,
215 					buf_info->dma_addr + buf_info->page_offset,
216 					frag_len, DMA_FROM_DEVICE);
217 
218 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
219 				buf_info->page, buf_info->page_offset, frag_len,
220 				IONIC_PAGE_SIZE);
221 
222 		if (!ionic_rx_buf_recycle(q, buf_info, frag_len)) {
223 			dma_unmap_page(dev, buf_info->dma_addr,
224 				       IONIC_PAGE_SIZE, DMA_FROM_DEVICE);
225 			buf_info->page = NULL;
226 		}
227 
228 		buf_info++;
229 
230 		i--;
231 	} while (i > 0);
232 
233 	return skb;
234 }
235 
ionic_rx_copybreak(struct ionic_queue * q,struct ionic_desc_info * desc_info,struct ionic_rxq_comp * comp)236 static struct sk_buff *ionic_rx_copybreak(struct ionic_queue *q,
237 					  struct ionic_desc_info *desc_info,
238 					  struct ionic_rxq_comp *comp)
239 {
240 	struct net_device *netdev = q->lif->netdev;
241 	struct ionic_buf_info *buf_info;
242 	struct ionic_rx_stats *stats;
243 	struct device *dev = q->dev;
244 	struct sk_buff *skb;
245 	u16 len;
246 
247 	stats = q_to_rx_stats(q);
248 
249 	buf_info = &desc_info->bufs[0];
250 	len = le16_to_cpu(comp->len);
251 
252 	skb = napi_alloc_skb(&q_to_qcq(q)->napi, len);
253 	if (unlikely(!skb)) {
254 		net_warn_ratelimited("%s: SKB alloc failed on %s!\n",
255 				     netdev->name, q->name);
256 		stats->alloc_err++;
257 		return NULL;
258 	}
259 
260 	if (unlikely(!buf_info->page)) {
261 		dev_kfree_skb(skb);
262 		return NULL;
263 	}
264 
265 	dma_sync_single_for_cpu(dev, buf_info->dma_addr + buf_info->page_offset,
266 				len, DMA_FROM_DEVICE);
267 	skb_copy_to_linear_data(skb, page_address(buf_info->page) + buf_info->page_offset, len);
268 	dma_sync_single_for_device(dev, buf_info->dma_addr + buf_info->page_offset,
269 				   len, DMA_FROM_DEVICE);
270 
271 	skb_put(skb, len);
272 	skb->protocol = eth_type_trans(skb, q->lif->netdev);
273 
274 	return skb;
275 }
276 
ionic_rx_clean(struct ionic_queue * q,struct ionic_desc_info * desc_info,struct ionic_cq_info * cq_info,void * cb_arg)277 static void ionic_rx_clean(struct ionic_queue *q,
278 			   struct ionic_desc_info *desc_info,
279 			   struct ionic_cq_info *cq_info,
280 			   void *cb_arg)
281 {
282 	struct net_device *netdev = q->lif->netdev;
283 	struct ionic_qcq *qcq = q_to_qcq(q);
284 	struct ionic_rx_stats *stats;
285 	struct ionic_rxq_comp *comp;
286 	struct sk_buff *skb;
287 
288 	comp = cq_info->cq_desc + qcq->cq.desc_size - sizeof(*comp);
289 
290 	stats = q_to_rx_stats(q);
291 
292 	if (comp->status) {
293 		stats->dropped++;
294 		return;
295 	}
296 
297 	stats->pkts++;
298 	stats->bytes += le16_to_cpu(comp->len);
299 
300 	if (le16_to_cpu(comp->len) <= q->lif->rx_copybreak)
301 		skb = ionic_rx_copybreak(q, desc_info, comp);
302 	else
303 		skb = ionic_rx_frags(q, desc_info, comp);
304 
305 	if (unlikely(!skb)) {
306 		stats->dropped++;
307 		return;
308 	}
309 
310 	skb_record_rx_queue(skb, q->index);
311 
312 	if (likely(netdev->features & NETIF_F_RXHASH)) {
313 		switch (comp->pkt_type_color & IONIC_RXQ_COMP_PKT_TYPE_MASK) {
314 		case IONIC_PKT_TYPE_IPV4:
315 		case IONIC_PKT_TYPE_IPV6:
316 			skb_set_hash(skb, le32_to_cpu(comp->rss_hash),
317 				     PKT_HASH_TYPE_L3);
318 			break;
319 		case IONIC_PKT_TYPE_IPV4_TCP:
320 		case IONIC_PKT_TYPE_IPV6_TCP:
321 		case IONIC_PKT_TYPE_IPV4_UDP:
322 		case IONIC_PKT_TYPE_IPV6_UDP:
323 			skb_set_hash(skb, le32_to_cpu(comp->rss_hash),
324 				     PKT_HASH_TYPE_L4);
325 			break;
326 		}
327 	}
328 
329 	if (likely(netdev->features & NETIF_F_RXCSUM) &&
330 	    (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_CALC)) {
331 		skb->ip_summed = CHECKSUM_COMPLETE;
332 		skb->csum = (__force __wsum)le16_to_cpu(comp->csum);
333 		stats->csum_complete++;
334 	} else {
335 		stats->csum_none++;
336 	}
337 
338 	if (unlikely((comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_TCP_BAD) ||
339 		     (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_UDP_BAD) ||
340 		     (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_IP_BAD)))
341 		stats->csum_error++;
342 
343 	if (likely(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
344 	    (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_VLAN)) {
345 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
346 				       le16_to_cpu(comp->vlan_tci));
347 		stats->vlan_stripped++;
348 	}
349 
350 	if (unlikely(q->features & IONIC_RXQ_F_HWSTAMP)) {
351 		__le64 *cq_desc_hwstamp;
352 		u64 hwstamp;
353 
354 		cq_desc_hwstamp =
355 			cq_info->cq_desc +
356 			qcq->cq.desc_size -
357 			sizeof(struct ionic_rxq_comp) -
358 			IONIC_HWSTAMP_CQ_NEGOFFSET;
359 
360 		hwstamp = le64_to_cpu(*cq_desc_hwstamp);
361 
362 		if (hwstamp != IONIC_HWSTAMP_INVALID) {
363 			skb_hwtstamps(skb)->hwtstamp = ionic_lif_phc_ktime(q->lif, hwstamp);
364 			stats->hwstamp_valid++;
365 		} else {
366 			stats->hwstamp_invalid++;
367 		}
368 	}
369 
370 	if (le16_to_cpu(comp->len) <= q->lif->rx_copybreak)
371 		napi_gro_receive(&qcq->napi, skb);
372 	else
373 		napi_gro_frags(&qcq->napi);
374 }
375 
ionic_rx_service(struct ionic_cq * cq,struct ionic_cq_info * cq_info)376 bool ionic_rx_service(struct ionic_cq *cq, struct ionic_cq_info *cq_info)
377 {
378 	struct ionic_queue *q = cq->bound_q;
379 	struct ionic_desc_info *desc_info;
380 	struct ionic_rxq_comp *comp;
381 
382 	comp = cq_info->cq_desc + cq->desc_size - sizeof(*comp);
383 
384 	if (!color_match(comp->pkt_type_color, cq->done_color))
385 		return false;
386 
387 	/* check for empty queue */
388 	if (q->tail_idx == q->head_idx)
389 		return false;
390 
391 	if (q->tail_idx != le16_to_cpu(comp->comp_index))
392 		return false;
393 
394 	desc_info = &q->info[q->tail_idx];
395 	q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
396 
397 	/* clean the related q entry, only one per qc completion */
398 	ionic_rx_clean(q, desc_info, cq_info, desc_info->cb_arg);
399 
400 	desc_info->cb = NULL;
401 	desc_info->cb_arg = NULL;
402 
403 	return true;
404 }
405 
ionic_rx_fill(struct ionic_queue * q)406 void ionic_rx_fill(struct ionic_queue *q)
407 {
408 	struct net_device *netdev = q->lif->netdev;
409 	struct ionic_desc_info *desc_info;
410 	struct ionic_rxq_sg_desc *sg_desc;
411 	struct ionic_rxq_sg_elem *sg_elem;
412 	struct ionic_buf_info *buf_info;
413 	unsigned int fill_threshold;
414 	struct ionic_rxq_desc *desc;
415 	unsigned int remain_len;
416 	unsigned int frag_len;
417 	unsigned int nfrags;
418 	unsigned int n_fill;
419 	unsigned int i, j;
420 	unsigned int len;
421 
422 	n_fill = ionic_q_space_avail(q);
423 
424 	fill_threshold = min_t(unsigned int, IONIC_RX_FILL_THRESHOLD,
425 			       q->num_descs / IONIC_RX_FILL_DIV);
426 	if (n_fill < fill_threshold)
427 		return;
428 
429 	len = netdev->mtu + ETH_HLEN + VLAN_HLEN;
430 
431 	for (i = n_fill; i; i--) {
432 		nfrags = 0;
433 		remain_len = len;
434 		desc_info = &q->info[q->head_idx];
435 		desc = desc_info->desc;
436 		buf_info = &desc_info->bufs[0];
437 
438 		if (!buf_info->page) { /* alloc a new buffer? */
439 			if (unlikely(ionic_rx_page_alloc(q, buf_info))) {
440 				desc->addr = 0;
441 				desc->len = 0;
442 				return;
443 			}
444 		}
445 
446 		/* fill main descriptor - buf[0] */
447 		desc->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
448 		frag_len = min_t(u16, len, min_t(u32, IONIC_MAX_BUF_LEN,
449 						 IONIC_PAGE_SIZE - buf_info->page_offset));
450 		desc->len = cpu_to_le16(frag_len);
451 		remain_len -= frag_len;
452 		buf_info++;
453 		nfrags++;
454 
455 		/* fill sg descriptors - buf[1..n] */
456 		sg_desc = desc_info->sg_desc;
457 		for (j = 0; remain_len > 0 && j < q->max_sg_elems; j++) {
458 			sg_elem = &sg_desc->elems[j];
459 			if (!buf_info->page) { /* alloc a new sg buffer? */
460 				if (unlikely(ionic_rx_page_alloc(q, buf_info))) {
461 					sg_elem->addr = 0;
462 					sg_elem->len = 0;
463 					return;
464 				}
465 			}
466 
467 			sg_elem->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset);
468 			frag_len = min_t(u16, remain_len, min_t(u32, IONIC_MAX_BUF_LEN,
469 								IONIC_PAGE_SIZE -
470 								buf_info->page_offset));
471 			sg_elem->len = cpu_to_le16(frag_len);
472 			remain_len -= frag_len;
473 			buf_info++;
474 			nfrags++;
475 		}
476 
477 		/* clear end sg element as a sentinel */
478 		if (j < q->max_sg_elems) {
479 			sg_elem = &sg_desc->elems[j];
480 			memset(sg_elem, 0, sizeof(*sg_elem));
481 		}
482 
483 		desc->opcode = (nfrags > 1) ? IONIC_RXQ_DESC_OPCODE_SG :
484 					      IONIC_RXQ_DESC_OPCODE_SIMPLE;
485 		desc_info->nbufs = nfrags;
486 
487 		ionic_rxq_post(q, false, ionic_rx_clean, NULL);
488 	}
489 
490 	ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
491 			 q->dbval | q->head_idx);
492 
493 	q->dbell_deadline = IONIC_RX_MIN_DOORBELL_DEADLINE;
494 	q->dbell_jiffies = jiffies;
495 
496 	mod_timer(&q_to_qcq(q)->napi_qcq->napi_deadline,
497 		  jiffies + IONIC_NAPI_DEADLINE);
498 }
499 
ionic_rx_empty(struct ionic_queue * q)500 void ionic_rx_empty(struct ionic_queue *q)
501 {
502 	struct ionic_desc_info *desc_info;
503 	struct ionic_buf_info *buf_info;
504 	unsigned int i, j;
505 
506 	for (i = 0; i < q->num_descs; i++) {
507 		desc_info = &q->info[i];
508 		for (j = 0; j < IONIC_RX_MAX_SG_ELEMS + 1; j++) {
509 			buf_info = &desc_info->bufs[j];
510 			if (buf_info->page)
511 				ionic_rx_page_free(q, buf_info);
512 		}
513 
514 		desc_info->nbufs = 0;
515 		desc_info->cb = NULL;
516 		desc_info->cb_arg = NULL;
517 	}
518 
519 	q->head_idx = 0;
520 	q->tail_idx = 0;
521 }
522 
ionic_dim_update(struct ionic_qcq * qcq,int napi_mode)523 static void ionic_dim_update(struct ionic_qcq *qcq, int napi_mode)
524 {
525 	struct dim_sample dim_sample;
526 	struct ionic_lif *lif;
527 	unsigned int qi;
528 	u64 pkts, bytes;
529 
530 	if (!qcq->intr.dim_coal_hw)
531 		return;
532 
533 	lif = qcq->q.lif;
534 	qi = qcq->cq.bound_q->index;
535 
536 	switch (napi_mode) {
537 	case IONIC_LIF_F_TX_DIM_INTR:
538 		pkts = lif->txqstats[qi].pkts;
539 		bytes = lif->txqstats[qi].bytes;
540 		break;
541 	case IONIC_LIF_F_RX_DIM_INTR:
542 		pkts = lif->rxqstats[qi].pkts;
543 		bytes = lif->rxqstats[qi].bytes;
544 		break;
545 	default:
546 		pkts = lif->txqstats[qi].pkts + lif->rxqstats[qi].pkts;
547 		bytes = lif->txqstats[qi].bytes + lif->rxqstats[qi].bytes;
548 		break;
549 	}
550 
551 	dim_update_sample(qcq->cq.bound_intr->rearm_count,
552 			  pkts, bytes, &dim_sample);
553 
554 	net_dim(&qcq->dim, dim_sample);
555 }
556 
ionic_tx_napi(struct napi_struct * napi,int budget)557 int ionic_tx_napi(struct napi_struct *napi, int budget)
558 {
559 	struct ionic_qcq *qcq = napi_to_qcq(napi);
560 	struct ionic_cq *cq = napi_to_cq(napi);
561 	struct ionic_dev *idev;
562 	struct ionic_lif *lif;
563 	u32 work_done = 0;
564 	u32 flags = 0;
565 
566 	lif = cq->bound_q->lif;
567 	idev = &lif->ionic->idev;
568 
569 	work_done = ionic_cq_service(cq, budget,
570 				     ionic_tx_service, NULL, NULL);
571 
572 	if (work_done < budget && napi_complete_done(napi, work_done)) {
573 		ionic_dim_update(qcq, IONIC_LIF_F_TX_DIM_INTR);
574 		flags |= IONIC_INTR_CRED_UNMASK;
575 		cq->bound_intr->rearm_count++;
576 	}
577 
578 	if (work_done || flags) {
579 		flags |= IONIC_INTR_CRED_RESET_COALESCE;
580 		ionic_intr_credits(idev->intr_ctrl,
581 				   cq->bound_intr->index,
582 				   work_done, flags);
583 	}
584 
585 	if (!work_done && ionic_txq_poke_doorbell(&qcq->q))
586 		mod_timer(&qcq->napi_deadline, jiffies + IONIC_NAPI_DEADLINE);
587 
588 	return work_done;
589 }
590 
ionic_rx_napi(struct napi_struct * napi,int budget)591 int ionic_rx_napi(struct napi_struct *napi, int budget)
592 {
593 	struct ionic_qcq *qcq = napi_to_qcq(napi);
594 	struct ionic_cq *cq = napi_to_cq(napi);
595 	struct ionic_dev *idev;
596 	struct ionic_lif *lif;
597 	u32 work_done = 0;
598 	u32 flags = 0;
599 
600 	lif = cq->bound_q->lif;
601 	idev = &lif->ionic->idev;
602 
603 	work_done = ionic_cq_service(cq, budget,
604 				     ionic_rx_service, NULL, NULL);
605 
606 	ionic_rx_fill(cq->bound_q);
607 
608 	if (work_done < budget && napi_complete_done(napi, work_done)) {
609 		ionic_dim_update(qcq, IONIC_LIF_F_RX_DIM_INTR);
610 		flags |= IONIC_INTR_CRED_UNMASK;
611 		cq->bound_intr->rearm_count++;
612 	}
613 
614 	if (work_done || flags) {
615 		flags |= IONIC_INTR_CRED_RESET_COALESCE;
616 		ionic_intr_credits(idev->intr_ctrl,
617 				   cq->bound_intr->index,
618 				   work_done, flags);
619 	}
620 
621 	if (!work_done && ionic_rxq_poke_doorbell(&qcq->q))
622 		mod_timer(&qcq->napi_deadline, jiffies + IONIC_NAPI_DEADLINE);
623 
624 	return work_done;
625 }
626 
ionic_txrx_napi(struct napi_struct * napi,int budget)627 int ionic_txrx_napi(struct napi_struct *napi, int budget)
628 {
629 	struct ionic_qcq *rxqcq = napi_to_qcq(napi);
630 	struct ionic_cq *rxcq = napi_to_cq(napi);
631 	unsigned int qi = rxcq->bound_q->index;
632 	struct ionic_qcq *txqcq;
633 	struct ionic_dev *idev;
634 	struct ionic_lif *lif;
635 	struct ionic_cq *txcq;
636 	bool resched = false;
637 	u32 rx_work_done = 0;
638 	u32 tx_work_done = 0;
639 	u32 flags = 0;
640 
641 	lif = rxcq->bound_q->lif;
642 	idev = &lif->ionic->idev;
643 	txqcq = lif->txqcqs[qi];
644 	txcq = &lif->txqcqs[qi]->cq;
645 
646 	tx_work_done = ionic_cq_service(txcq, IONIC_TX_BUDGET_DEFAULT,
647 					ionic_tx_service, NULL, NULL);
648 
649 	rx_work_done = ionic_cq_service(rxcq, budget,
650 					ionic_rx_service, NULL, NULL);
651 
652 	ionic_rx_fill(rxcq->bound_q);
653 
654 	if (rx_work_done < budget && napi_complete_done(napi, rx_work_done)) {
655 		ionic_dim_update(rxqcq, 0);
656 		flags |= IONIC_INTR_CRED_UNMASK;
657 		rxcq->bound_intr->rearm_count++;
658 	}
659 
660 	if (rx_work_done || flags) {
661 		flags |= IONIC_INTR_CRED_RESET_COALESCE;
662 		ionic_intr_credits(idev->intr_ctrl, rxcq->bound_intr->index,
663 				   tx_work_done + rx_work_done, flags);
664 	}
665 
666 	if (!rx_work_done && ionic_rxq_poke_doorbell(&rxqcq->q))
667 		resched = true;
668 	if (!tx_work_done && ionic_txq_poke_doorbell(&txqcq->q))
669 		resched = true;
670 	if (resched)
671 		mod_timer(&rxqcq->napi_deadline, jiffies + IONIC_NAPI_DEADLINE);
672 
673 	return rx_work_done;
674 }
675 
ionic_tx_map_single(struct ionic_queue * q,void * data,size_t len)676 static dma_addr_t ionic_tx_map_single(struct ionic_queue *q,
677 				      void *data, size_t len)
678 {
679 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
680 	struct device *dev = q->dev;
681 	dma_addr_t dma_addr;
682 
683 	dma_addr = dma_map_single(dev, data, len, DMA_TO_DEVICE);
684 	if (dma_mapping_error(dev, dma_addr)) {
685 		net_warn_ratelimited("%s: DMA single map failed on %s!\n",
686 				     q->lif->netdev->name, q->name);
687 		stats->dma_map_err++;
688 		return 0;
689 	}
690 	return dma_addr;
691 }
692 
ionic_tx_map_frag(struct ionic_queue * q,const skb_frag_t * frag,size_t offset,size_t len)693 static dma_addr_t ionic_tx_map_frag(struct ionic_queue *q,
694 				    const skb_frag_t *frag,
695 				    size_t offset, size_t len)
696 {
697 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
698 	struct device *dev = q->dev;
699 	dma_addr_t dma_addr;
700 
701 	dma_addr = skb_frag_dma_map(dev, frag, offset, len, DMA_TO_DEVICE);
702 	if (dma_mapping_error(dev, dma_addr)) {
703 		net_warn_ratelimited("%s: DMA frag map failed on %s!\n",
704 				     q->lif->netdev->name, q->name);
705 		stats->dma_map_err++;
706 	}
707 	return dma_addr;
708 }
709 
ionic_tx_map_skb(struct ionic_queue * q,struct sk_buff * skb,struct ionic_desc_info * desc_info)710 static int ionic_tx_map_skb(struct ionic_queue *q, struct sk_buff *skb,
711 			    struct ionic_desc_info *desc_info)
712 {
713 	struct ionic_buf_info *buf_info = desc_info->bufs;
714 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
715 	struct device *dev = q->dev;
716 	dma_addr_t dma_addr;
717 	unsigned int nfrags;
718 	skb_frag_t *frag;
719 	int frag_idx;
720 
721 	dma_addr = ionic_tx_map_single(q, skb->data, skb_headlen(skb));
722 	if (dma_mapping_error(dev, dma_addr)) {
723 		stats->dma_map_err++;
724 		return -EIO;
725 	}
726 	buf_info->dma_addr = dma_addr;
727 	buf_info->len = skb_headlen(skb);
728 	buf_info++;
729 
730 	frag = skb_shinfo(skb)->frags;
731 	nfrags = skb_shinfo(skb)->nr_frags;
732 	for (frag_idx = 0; frag_idx < nfrags; frag_idx++, frag++) {
733 		dma_addr = ionic_tx_map_frag(q, frag, 0, skb_frag_size(frag));
734 		if (dma_mapping_error(dev, dma_addr)) {
735 			stats->dma_map_err++;
736 			goto dma_fail;
737 		}
738 		buf_info->dma_addr = dma_addr;
739 		buf_info->len = skb_frag_size(frag);
740 		buf_info++;
741 	}
742 
743 	desc_info->nbufs = 1 + nfrags;
744 
745 	return 0;
746 
747 dma_fail:
748 	/* unwind the frag mappings and the head mapping */
749 	while (frag_idx > 0) {
750 		frag_idx--;
751 		buf_info--;
752 		dma_unmap_page(dev, buf_info->dma_addr,
753 			       buf_info->len, DMA_TO_DEVICE);
754 	}
755 	dma_unmap_single(dev, buf_info->dma_addr, buf_info->len, DMA_TO_DEVICE);
756 	return -EIO;
757 }
758 
ionic_tx_desc_unmap_bufs(struct ionic_queue * q,struct ionic_desc_info * desc_info)759 static void ionic_tx_desc_unmap_bufs(struct ionic_queue *q,
760 				     struct ionic_desc_info *desc_info)
761 {
762 	struct ionic_buf_info *buf_info = desc_info->bufs;
763 	struct device *dev = q->dev;
764 	unsigned int i;
765 
766 	if (!desc_info->nbufs)
767 		return;
768 
769 	dma_unmap_single(dev, (dma_addr_t)buf_info->dma_addr,
770 			 buf_info->len, DMA_TO_DEVICE);
771 	buf_info++;
772 	for (i = 1; i < desc_info->nbufs; i++, buf_info++)
773 		dma_unmap_page(dev, (dma_addr_t)buf_info->dma_addr,
774 			       buf_info->len, DMA_TO_DEVICE);
775 
776 	desc_info->nbufs = 0;
777 }
778 
ionic_tx_clean(struct ionic_queue * q,struct ionic_desc_info * desc_info,struct ionic_cq_info * cq_info,void * cb_arg)779 static void ionic_tx_clean(struct ionic_queue *q,
780 			   struct ionic_desc_info *desc_info,
781 			   struct ionic_cq_info *cq_info,
782 			   void *cb_arg)
783 {
784 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
785 	struct ionic_qcq *qcq = q_to_qcq(q);
786 	struct sk_buff *skb = cb_arg;
787 	u16 qi;
788 
789 	ionic_tx_desc_unmap_bufs(q, desc_info);
790 
791 	if (!skb)
792 		return;
793 
794 	qi = skb_get_queue_mapping(skb);
795 
796 	if (unlikely(q->features & IONIC_TXQ_F_HWSTAMP)) {
797 		if (cq_info) {
798 			struct skb_shared_hwtstamps hwts = {};
799 			__le64 *cq_desc_hwstamp;
800 			u64 hwstamp;
801 
802 			cq_desc_hwstamp =
803 				cq_info->cq_desc +
804 				qcq->cq.desc_size -
805 				sizeof(struct ionic_txq_comp) -
806 				IONIC_HWSTAMP_CQ_NEGOFFSET;
807 
808 			hwstamp = le64_to_cpu(*cq_desc_hwstamp);
809 
810 			if (hwstamp != IONIC_HWSTAMP_INVALID) {
811 				hwts.hwtstamp = ionic_lif_phc_ktime(q->lif, hwstamp);
812 
813 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
814 				skb_tstamp_tx(skb, &hwts);
815 
816 				stats->hwstamp_valid++;
817 			} else {
818 				stats->hwstamp_invalid++;
819 			}
820 		}
821 
822 	} else if (unlikely(__netif_subqueue_stopped(q->lif->netdev, qi))) {
823 		netif_wake_subqueue(q->lif->netdev, qi);
824 	}
825 
826 	desc_info->bytes = skb->len;
827 	stats->clean++;
828 
829 	dev_consume_skb_any(skb);
830 }
831 
ionic_tx_service(struct ionic_cq * cq,struct ionic_cq_info * cq_info)832 bool ionic_tx_service(struct ionic_cq *cq, struct ionic_cq_info *cq_info)
833 {
834 	struct ionic_queue *q = cq->bound_q;
835 	struct ionic_desc_info *desc_info;
836 	struct ionic_txq_comp *comp;
837 	int bytes = 0;
838 	int pkts = 0;
839 	u16 index;
840 
841 	comp = cq_info->cq_desc + cq->desc_size - sizeof(*comp);
842 
843 	if (!color_match(comp->color, cq->done_color))
844 		return false;
845 
846 	/* clean the related q entries, there could be
847 	 * several q entries completed for each cq completion
848 	 */
849 	do {
850 		desc_info = &q->info[q->tail_idx];
851 		desc_info->bytes = 0;
852 		index = q->tail_idx;
853 		q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
854 		ionic_tx_clean(q, desc_info, cq_info, desc_info->cb_arg);
855 		if (desc_info->cb_arg) {
856 			pkts++;
857 			bytes += desc_info->bytes;
858 		}
859 		desc_info->cb = NULL;
860 		desc_info->cb_arg = NULL;
861 	} while (index != le16_to_cpu(comp->comp_index));
862 
863 	if (pkts && bytes && !unlikely(q->features & IONIC_TXQ_F_HWSTAMP))
864 		netdev_tx_completed_queue(q_to_ndq(q), pkts, bytes);
865 
866 	return true;
867 }
868 
ionic_tx_flush(struct ionic_cq * cq)869 void ionic_tx_flush(struct ionic_cq *cq)
870 {
871 	struct ionic_dev *idev = &cq->lif->ionic->idev;
872 	u32 work_done;
873 
874 	work_done = ionic_cq_service(cq, cq->num_descs,
875 				     ionic_tx_service, NULL, NULL);
876 	if (work_done)
877 		ionic_intr_credits(idev->intr_ctrl, cq->bound_intr->index,
878 				   work_done, IONIC_INTR_CRED_RESET_COALESCE);
879 }
880 
ionic_tx_empty(struct ionic_queue * q)881 void ionic_tx_empty(struct ionic_queue *q)
882 {
883 	struct ionic_desc_info *desc_info;
884 	int bytes = 0;
885 	int pkts = 0;
886 
887 	/* walk the not completed tx entries, if any */
888 	while (q->head_idx != q->tail_idx) {
889 		desc_info = &q->info[q->tail_idx];
890 		desc_info->bytes = 0;
891 		q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
892 		ionic_tx_clean(q, desc_info, NULL, desc_info->cb_arg);
893 		if (desc_info->cb_arg) {
894 			pkts++;
895 			bytes += desc_info->bytes;
896 		}
897 		desc_info->cb = NULL;
898 		desc_info->cb_arg = NULL;
899 	}
900 
901 	if (pkts && bytes && !unlikely(q->features & IONIC_TXQ_F_HWSTAMP))
902 		netdev_tx_completed_queue(q_to_ndq(q), pkts, bytes);
903 }
904 
ionic_tx_tcp_inner_pseudo_csum(struct sk_buff * skb)905 static int ionic_tx_tcp_inner_pseudo_csum(struct sk_buff *skb)
906 {
907 	int err;
908 
909 	err = skb_cow_head(skb, 0);
910 	if (err)
911 		return err;
912 
913 	if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
914 		inner_ip_hdr(skb)->check = 0;
915 		inner_tcp_hdr(skb)->check =
916 			~csum_tcpudp_magic(inner_ip_hdr(skb)->saddr,
917 					   inner_ip_hdr(skb)->daddr,
918 					   0, IPPROTO_TCP, 0);
919 	} else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
920 		inner_tcp_hdr(skb)->check =
921 			~csum_ipv6_magic(&inner_ipv6_hdr(skb)->saddr,
922 					 &inner_ipv6_hdr(skb)->daddr,
923 					 0, IPPROTO_TCP, 0);
924 	}
925 
926 	return 0;
927 }
928 
ionic_tx_tcp_pseudo_csum(struct sk_buff * skb)929 static int ionic_tx_tcp_pseudo_csum(struct sk_buff *skb)
930 {
931 	int err;
932 
933 	err = skb_cow_head(skb, 0);
934 	if (err)
935 		return err;
936 
937 	if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
938 		ip_hdr(skb)->check = 0;
939 		tcp_hdr(skb)->check =
940 			~csum_tcpudp_magic(ip_hdr(skb)->saddr,
941 					   ip_hdr(skb)->daddr,
942 					   0, IPPROTO_TCP, 0);
943 	} else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
944 		tcp_v6_gso_csum_prep(skb);
945 	}
946 
947 	return 0;
948 }
949 
ionic_tx_tso_post(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)950 static void ionic_tx_tso_post(struct ionic_queue *q, struct ionic_txq_desc *desc,
951 			      struct sk_buff *skb,
952 			      dma_addr_t addr, u8 nsge, u16 len,
953 			      unsigned int hdrlen, unsigned int mss,
954 			      bool outer_csum,
955 			      u16 vlan_tci, bool has_vlan,
956 			      bool start, bool done)
957 {
958 	u8 flags = 0;
959 	u64 cmd;
960 
961 	flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
962 	flags |= outer_csum ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
963 	flags |= start ? IONIC_TXQ_DESC_FLAG_TSO_SOT : 0;
964 	flags |= done ? IONIC_TXQ_DESC_FLAG_TSO_EOT : 0;
965 
966 	cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_TSO, flags, nsge, addr);
967 	desc->cmd = cpu_to_le64(cmd);
968 	desc->len = cpu_to_le16(len);
969 	desc->vlan_tci = cpu_to_le16(vlan_tci);
970 	desc->hdr_len = cpu_to_le16(hdrlen);
971 	desc->mss = cpu_to_le16(mss);
972 
973 	if (start) {
974 		skb_tx_timestamp(skb);
975 		if (!unlikely(q->features & IONIC_TXQ_F_HWSTAMP))
976 			netdev_tx_sent_queue(q_to_ndq(q), skb->len);
977 		ionic_txq_post(q, false, ionic_tx_clean, skb);
978 	} else {
979 		ionic_txq_post(q, done, NULL, NULL);
980 	}
981 }
982 
ionic_tx_tso(struct ionic_queue * q,struct sk_buff * skb)983 static int ionic_tx_tso(struct ionic_queue *q, struct sk_buff *skb)
984 {
985 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
986 	struct ionic_desc_info *desc_info;
987 	struct ionic_buf_info *buf_info;
988 	struct ionic_txq_sg_elem *elem;
989 	struct ionic_txq_desc *desc;
990 	unsigned int chunk_len;
991 	unsigned int frag_rem;
992 	unsigned int tso_rem;
993 	unsigned int seg_rem;
994 	dma_addr_t desc_addr;
995 	dma_addr_t frag_addr;
996 	unsigned int hdrlen;
997 	unsigned int len;
998 	unsigned int mss;
999 	bool start, done;
1000 	bool outer_csum;
1001 	bool has_vlan;
1002 	u16 desc_len;
1003 	u8 desc_nsge;
1004 	u16 vlan_tci;
1005 	bool encap;
1006 	int err;
1007 
1008 	desc_info = &q->info[q->head_idx];
1009 	buf_info = desc_info->bufs;
1010 
1011 	if (unlikely(ionic_tx_map_skb(q, skb, desc_info)))
1012 		return -EIO;
1013 
1014 	len = skb->len;
1015 	mss = skb_shinfo(skb)->gso_size;
1016 	outer_csum = (skb_shinfo(skb)->gso_type & SKB_GSO_GRE_CSUM) ||
1017 		     (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM);
1018 	has_vlan = !!skb_vlan_tag_present(skb);
1019 	vlan_tci = skb_vlan_tag_get(skb);
1020 	encap = skb->encapsulation;
1021 
1022 	/* Preload inner-most TCP csum field with IP pseudo hdr
1023 	 * calculated with IP length set to zero.  HW will later
1024 	 * add in length to each TCP segment resulting from the TSO.
1025 	 */
1026 
1027 	if (encap)
1028 		err = ionic_tx_tcp_inner_pseudo_csum(skb);
1029 	else
1030 		err = ionic_tx_tcp_pseudo_csum(skb);
1031 	if (err) {
1032 		/* clean up mapping from ionic_tx_map_skb */
1033 		ionic_tx_desc_unmap_bufs(q, desc_info);
1034 		return err;
1035 	}
1036 
1037 	if (encap)
1038 		hdrlen = skb_inner_tcp_all_headers(skb);
1039 	else
1040 		hdrlen = skb_tcp_all_headers(skb);
1041 
1042 	tso_rem = len;
1043 	seg_rem = min(tso_rem, hdrlen + mss);
1044 
1045 	frag_addr = 0;
1046 	frag_rem = 0;
1047 
1048 	start = true;
1049 
1050 	while (tso_rem > 0) {
1051 		desc = NULL;
1052 		elem = NULL;
1053 		desc_addr = 0;
1054 		desc_len = 0;
1055 		desc_nsge = 0;
1056 		/* use fragments until we have enough to post a single descriptor */
1057 		while (seg_rem > 0) {
1058 			/* if the fragment is exhausted then move to the next one */
1059 			if (frag_rem == 0) {
1060 				/* grab the next fragment */
1061 				frag_addr = buf_info->dma_addr;
1062 				frag_rem = buf_info->len;
1063 				buf_info++;
1064 			}
1065 			chunk_len = min(frag_rem, seg_rem);
1066 			if (!desc) {
1067 				/* fill main descriptor */
1068 				desc = desc_info->txq_desc;
1069 				elem = desc_info->txq_sg_desc->elems;
1070 				desc_addr = frag_addr;
1071 				desc_len = chunk_len;
1072 			} else {
1073 				/* fill sg descriptor */
1074 				elem->addr = cpu_to_le64(frag_addr);
1075 				elem->len = cpu_to_le16(chunk_len);
1076 				elem++;
1077 				desc_nsge++;
1078 			}
1079 			frag_addr += chunk_len;
1080 			frag_rem -= chunk_len;
1081 			tso_rem -= chunk_len;
1082 			seg_rem -= chunk_len;
1083 		}
1084 		seg_rem = min(tso_rem, mss);
1085 		done = (tso_rem == 0);
1086 		/* post descriptor */
1087 		ionic_tx_tso_post(q, desc, skb,
1088 				  desc_addr, desc_nsge, desc_len,
1089 				  hdrlen, mss, outer_csum, vlan_tci, has_vlan,
1090 				  start, done);
1091 		start = false;
1092 		/* Buffer information is stored with the first tso descriptor */
1093 		desc_info = &q->info[q->head_idx];
1094 		desc_info->nbufs = 0;
1095 	}
1096 
1097 	stats->pkts += DIV_ROUND_UP(len - hdrlen, mss);
1098 	stats->bytes += len;
1099 	stats->tso++;
1100 	stats->tso_bytes = len;
1101 
1102 	return 0;
1103 }
1104 
ionic_tx_calc_csum(struct ionic_queue * q,struct sk_buff * skb,struct ionic_desc_info * desc_info)1105 static void ionic_tx_calc_csum(struct ionic_queue *q, struct sk_buff *skb,
1106 			       struct ionic_desc_info *desc_info)
1107 {
1108 	struct ionic_txq_desc *desc = desc_info->txq_desc;
1109 	struct ionic_buf_info *buf_info = desc_info->bufs;
1110 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1111 	bool has_vlan;
1112 	u8 flags = 0;
1113 	bool encap;
1114 	u64 cmd;
1115 
1116 	has_vlan = !!skb_vlan_tag_present(skb);
1117 	encap = skb->encapsulation;
1118 
1119 	flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1120 	flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1121 
1122 	cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_PARTIAL,
1123 				  flags, skb_shinfo(skb)->nr_frags,
1124 				  buf_info->dma_addr);
1125 	desc->cmd = cpu_to_le64(cmd);
1126 	desc->len = cpu_to_le16(buf_info->len);
1127 	if (has_vlan) {
1128 		desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb));
1129 		stats->vlan_inserted++;
1130 	} else {
1131 		desc->vlan_tci = 0;
1132 	}
1133 	desc->csum_start = cpu_to_le16(skb_checksum_start_offset(skb));
1134 	desc->csum_offset = cpu_to_le16(skb->csum_offset);
1135 
1136 	if (skb_csum_is_sctp(skb))
1137 		stats->crc32_csum++;
1138 	else
1139 		stats->csum++;
1140 }
1141 
ionic_tx_calc_no_csum(struct ionic_queue * q,struct sk_buff * skb,struct ionic_desc_info * desc_info)1142 static void ionic_tx_calc_no_csum(struct ionic_queue *q, struct sk_buff *skb,
1143 				  struct ionic_desc_info *desc_info)
1144 {
1145 	struct ionic_txq_desc *desc = desc_info->txq_desc;
1146 	struct ionic_buf_info *buf_info = desc_info->bufs;
1147 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1148 	bool has_vlan;
1149 	u8 flags = 0;
1150 	bool encap;
1151 	u64 cmd;
1152 
1153 	has_vlan = !!skb_vlan_tag_present(skb);
1154 	encap = skb->encapsulation;
1155 
1156 	flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
1157 	flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
1158 
1159 	cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_NONE,
1160 				  flags, skb_shinfo(skb)->nr_frags,
1161 				  buf_info->dma_addr);
1162 	desc->cmd = cpu_to_le64(cmd);
1163 	desc->len = cpu_to_le16(buf_info->len);
1164 	if (has_vlan) {
1165 		desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb));
1166 		stats->vlan_inserted++;
1167 	} else {
1168 		desc->vlan_tci = 0;
1169 	}
1170 	desc->csum_start = 0;
1171 	desc->csum_offset = 0;
1172 
1173 	stats->csum_none++;
1174 }
1175 
ionic_tx_skb_frags(struct ionic_queue * q,struct sk_buff * skb,struct ionic_desc_info * desc_info)1176 static void ionic_tx_skb_frags(struct ionic_queue *q, struct sk_buff *skb,
1177 			       struct ionic_desc_info *desc_info)
1178 {
1179 	struct ionic_txq_sg_desc *sg_desc = desc_info->txq_sg_desc;
1180 	struct ionic_buf_info *buf_info = &desc_info->bufs[1];
1181 	struct ionic_txq_sg_elem *elem = sg_desc->elems;
1182 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1183 	unsigned int i;
1184 
1185 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, buf_info++, elem++) {
1186 		elem->addr = cpu_to_le64(buf_info->dma_addr);
1187 		elem->len = cpu_to_le16(buf_info->len);
1188 	}
1189 
1190 	stats->frags += skb_shinfo(skb)->nr_frags;
1191 }
1192 
ionic_tx(struct ionic_queue * q,struct sk_buff * skb)1193 static int ionic_tx(struct ionic_queue *q, struct sk_buff *skb)
1194 {
1195 	struct ionic_desc_info *desc_info = &q->info[q->head_idx];
1196 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1197 
1198 	if (unlikely(ionic_tx_map_skb(q, skb, desc_info)))
1199 		return -EIO;
1200 
1201 	/* set up the initial descriptor */
1202 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1203 		ionic_tx_calc_csum(q, skb, desc_info);
1204 	else
1205 		ionic_tx_calc_no_csum(q, skb, desc_info);
1206 
1207 	/* add frags */
1208 	ionic_tx_skb_frags(q, skb, desc_info);
1209 
1210 	skb_tx_timestamp(skb);
1211 	stats->pkts++;
1212 	stats->bytes += skb->len;
1213 
1214 	if (!unlikely(q->features & IONIC_TXQ_F_HWSTAMP))
1215 		netdev_tx_sent_queue(q_to_ndq(q), skb->len);
1216 	ionic_txq_post(q, !netdev_xmit_more(), ionic_tx_clean, skb);
1217 
1218 	return 0;
1219 }
1220 
ionic_tx_descs_needed(struct ionic_queue * q,struct sk_buff * skb)1221 static int ionic_tx_descs_needed(struct ionic_queue *q, struct sk_buff *skb)
1222 {
1223 	struct ionic_tx_stats *stats = q_to_tx_stats(q);
1224 	int ndescs;
1225 	int err;
1226 
1227 	/* Each desc is mss long max, so a descriptor for each gso_seg */
1228 	if (skb_is_gso(skb))
1229 		ndescs = skb_shinfo(skb)->gso_segs;
1230 	else
1231 		ndescs = 1;
1232 
1233 	/* If non-TSO, just need 1 desc and nr_frags sg elems */
1234 	if (skb_shinfo(skb)->nr_frags <= q->max_sg_elems)
1235 		return ndescs;
1236 
1237 	/* Too many frags, so linearize */
1238 	err = skb_linearize(skb);
1239 	if (err)
1240 		return err;
1241 
1242 	stats->linearize++;
1243 
1244 	return ndescs;
1245 }
1246 
ionic_maybe_stop_tx(struct ionic_queue * q,int ndescs)1247 static int ionic_maybe_stop_tx(struct ionic_queue *q, int ndescs)
1248 {
1249 	int stopped = 0;
1250 
1251 	if (unlikely(!ionic_q_has_space(q, ndescs))) {
1252 		netif_stop_subqueue(q->lif->netdev, q->index);
1253 		stopped = 1;
1254 
1255 		/* Might race with ionic_tx_clean, check again */
1256 		smp_rmb();
1257 		if (ionic_q_has_space(q, ndescs)) {
1258 			netif_wake_subqueue(q->lif->netdev, q->index);
1259 			stopped = 0;
1260 		}
1261 	}
1262 
1263 	return stopped;
1264 }
1265 
ionic_start_hwstamp_xmit(struct sk_buff * skb,struct net_device * netdev)1266 static netdev_tx_t ionic_start_hwstamp_xmit(struct sk_buff *skb,
1267 					    struct net_device *netdev)
1268 {
1269 	struct ionic_lif *lif = netdev_priv(netdev);
1270 	struct ionic_queue *q = &lif->hwstamp_txq->q;
1271 	int err, ndescs;
1272 
1273 	/* Does not stop/start txq, because we post to a separate tx queue
1274 	 * for timestamping, and if a packet can't be posted immediately to
1275 	 * the timestamping queue, it is dropped.
1276 	 */
1277 
1278 	ndescs = ionic_tx_descs_needed(q, skb);
1279 	if (unlikely(ndescs < 0))
1280 		goto err_out_drop;
1281 
1282 	if (unlikely(!ionic_q_has_space(q, ndescs)))
1283 		goto err_out_drop;
1284 
1285 	skb_shinfo(skb)->tx_flags |= SKBTX_HW_TSTAMP;
1286 	if (skb_is_gso(skb))
1287 		err = ionic_tx_tso(q, skb);
1288 	else
1289 		err = ionic_tx(q, skb);
1290 
1291 	if (err)
1292 		goto err_out_drop;
1293 
1294 	return NETDEV_TX_OK;
1295 
1296 err_out_drop:
1297 	q->drop++;
1298 	dev_kfree_skb(skb);
1299 	return NETDEV_TX_OK;
1300 }
1301 
ionic_start_xmit(struct sk_buff * skb,struct net_device * netdev)1302 netdev_tx_t ionic_start_xmit(struct sk_buff *skb, struct net_device *netdev)
1303 {
1304 	u16 queue_index = skb_get_queue_mapping(skb);
1305 	struct ionic_lif *lif = netdev_priv(netdev);
1306 	struct ionic_queue *q;
1307 	int ndescs;
1308 	int err;
1309 
1310 	if (unlikely(!test_bit(IONIC_LIF_F_UP, lif->state))) {
1311 		dev_kfree_skb(skb);
1312 		return NETDEV_TX_OK;
1313 	}
1314 
1315 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
1316 		if (lif->hwstamp_txq && lif->phc->ts_config_tx_mode)
1317 			return ionic_start_hwstamp_xmit(skb, netdev);
1318 
1319 	if (unlikely(queue_index >= lif->nxqs))
1320 		queue_index = 0;
1321 	q = &lif->txqcqs[queue_index]->q;
1322 
1323 	ndescs = ionic_tx_descs_needed(q, skb);
1324 	if (ndescs < 0)
1325 		goto err_out_drop;
1326 
1327 	if (unlikely(ionic_maybe_stop_tx(q, ndescs)))
1328 		return NETDEV_TX_BUSY;
1329 
1330 	if (skb_is_gso(skb))
1331 		err = ionic_tx_tso(q, skb);
1332 	else
1333 		err = ionic_tx(q, skb);
1334 
1335 	if (err)
1336 		goto err_out_drop;
1337 
1338 	/* Stop the queue if there aren't descriptors for the next packet.
1339 	 * Since our SG lists per descriptor take care of most of the possible
1340 	 * fragmentation, we don't need to have many descriptors available.
1341 	 */
1342 	ionic_maybe_stop_tx(q, 4);
1343 
1344 	return NETDEV_TX_OK;
1345 
1346 err_out_drop:
1347 	q->drop++;
1348 	dev_kfree_skb(skb);
1349 	return NETDEV_TX_OK;
1350 }
1351