1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Ethernet driver
3  *
4  * Copyright (C) 2020 Marvell.
5  *
6  */
7 
8 #include <linux/etherdevice.h>
9 #include <net/ip.h>
10 #include <net/tso.h>
11 #include <linux/bpf.h>
12 #include <linux/bpf_trace.h>
13 #include <net/ip6_checksum.h>
14 
15 #include "otx2_reg.h"
16 #include "otx2_common.h"
17 #include "otx2_struct.h"
18 #include "otx2_txrx.h"
19 #include "otx2_ptp.h"
20 #include "cn10k.h"
21 
22 #define CQE_ADDR(CQ, idx) ((CQ)->cqe_base + ((CQ)->cqe_size * (idx)))
23 #define PTP_PORT	        0x13F
24 /* PTPv2 header Original Timestamp starts at byte offset 34 and
25  * contains 6 byte seconds field and 4 byte nano seconds field.
26  */
27 #define PTP_SYNC_SEC_OFFSET	34
28 
29 static bool otx2_xdp_rcv_pkt_handler(struct otx2_nic *pfvf,
30 				     struct bpf_prog *prog,
31 				     struct nix_cqe_rx_s *cqe,
32 				     struct otx2_cq_queue *cq);
33 
34 static int otx2_nix_cq_op_status(struct otx2_nic *pfvf,
35 				 struct otx2_cq_queue *cq)
36 {
37 	u64 incr = (u64)(cq->cq_idx) << 32;
38 	u64 status;
39 
40 	status = otx2_atomic64_fetch_add(incr, pfvf->cq_op_addr);
41 
42 	if (unlikely(status & BIT_ULL(CQ_OP_STAT_OP_ERR) ||
43 		     status & BIT_ULL(CQ_OP_STAT_CQ_ERR))) {
44 		dev_err(pfvf->dev, "CQ stopped due to error");
45 		return -EINVAL;
46 	}
47 
48 	cq->cq_tail = status & 0xFFFFF;
49 	cq->cq_head = (status >> 20) & 0xFFFFF;
50 	if (cq->cq_tail < cq->cq_head)
51 		cq->pend_cqe = (cq->cqe_cnt - cq->cq_head) +
52 				cq->cq_tail;
53 	else
54 		cq->pend_cqe = cq->cq_tail - cq->cq_head;
55 
56 	return 0;
57 }
58 
59 static struct nix_cqe_hdr_s *otx2_get_next_cqe(struct otx2_cq_queue *cq)
60 {
61 	struct nix_cqe_hdr_s *cqe_hdr;
62 
63 	cqe_hdr = (struct nix_cqe_hdr_s *)CQE_ADDR(cq, cq->cq_head);
64 	if (cqe_hdr->cqe_type == NIX_XQE_TYPE_INVALID)
65 		return NULL;
66 
67 	cq->cq_head++;
68 	cq->cq_head &= (cq->cqe_cnt - 1);
69 
70 	return cqe_hdr;
71 }
72 
73 static unsigned int frag_num(unsigned int i)
74 {
75 #ifdef __BIG_ENDIAN
76 	return (i & ~3) + 3 - (i & 3);
77 #else
78 	return i;
79 #endif
80 }
81 
82 static dma_addr_t otx2_dma_map_skb_frag(struct otx2_nic *pfvf,
83 					struct sk_buff *skb, int seg, int *len)
84 {
85 	const skb_frag_t *frag;
86 	struct page *page;
87 	int offset;
88 
89 	/* First segment is always skb->data */
90 	if (!seg) {
91 		page = virt_to_page(skb->data);
92 		offset = offset_in_page(skb->data);
93 		*len = skb_headlen(skb);
94 	} else {
95 		frag = &skb_shinfo(skb)->frags[seg - 1];
96 		page = skb_frag_page(frag);
97 		offset = skb_frag_off(frag);
98 		*len = skb_frag_size(frag);
99 	}
100 	return otx2_dma_map_page(pfvf, page, offset, *len, DMA_TO_DEVICE);
101 }
102 
103 static void otx2_dma_unmap_skb_frags(struct otx2_nic *pfvf, struct sg_list *sg)
104 {
105 	int seg;
106 
107 	for (seg = 0; seg < sg->num_segs; seg++) {
108 		otx2_dma_unmap_page(pfvf, sg->dma_addr[seg],
109 				    sg->size[seg], DMA_TO_DEVICE);
110 	}
111 	sg->num_segs = 0;
112 }
113 
114 static void otx2_xdp_snd_pkt_handler(struct otx2_nic *pfvf,
115 				     struct otx2_snd_queue *sq,
116 				 struct nix_cqe_tx_s *cqe)
117 {
118 	struct nix_send_comp_s *snd_comp = &cqe->comp;
119 	struct sg_list *sg;
120 	struct page *page;
121 	u64 pa;
122 
123 	sg = &sq->sg[snd_comp->sqe_id];
124 
125 	pa = otx2_iova_to_phys(pfvf->iommu_domain, sg->dma_addr[0]);
126 	otx2_dma_unmap_page(pfvf, sg->dma_addr[0],
127 			    sg->size[0], DMA_TO_DEVICE);
128 	page = virt_to_page(phys_to_virt(pa));
129 	put_page(page);
130 }
131 
132 static void otx2_snd_pkt_handler(struct otx2_nic *pfvf,
133 				 struct otx2_cq_queue *cq,
134 				 struct otx2_snd_queue *sq,
135 				 struct nix_cqe_tx_s *cqe,
136 				 int budget, int *tx_pkts, int *tx_bytes)
137 {
138 	struct nix_send_comp_s *snd_comp = &cqe->comp;
139 	struct skb_shared_hwtstamps ts;
140 	struct sk_buff *skb = NULL;
141 	u64 timestamp, tsns;
142 	struct sg_list *sg;
143 	int err;
144 
145 	if (unlikely(snd_comp->status) && netif_msg_tx_err(pfvf))
146 		net_err_ratelimited("%s: TX%d: Error in send CQ status:%x\n",
147 				    pfvf->netdev->name, cq->cint_idx,
148 				    snd_comp->status);
149 
150 	sg = &sq->sg[snd_comp->sqe_id];
151 	skb = (struct sk_buff *)sg->skb;
152 	if (unlikely(!skb))
153 		return;
154 
155 	if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) {
156 		timestamp = ((u64 *)sq->timestamps->base)[snd_comp->sqe_id];
157 		if (timestamp != 1) {
158 			timestamp = pfvf->ptp->convert_tx_ptp_tstmp(timestamp);
159 			err = otx2_ptp_tstamp2time(pfvf, timestamp, &tsns);
160 			if (!err) {
161 				memset(&ts, 0, sizeof(ts));
162 				ts.hwtstamp = ns_to_ktime(tsns);
163 				skb_tstamp_tx(skb, &ts);
164 			}
165 		}
166 	}
167 
168 	*tx_bytes += skb->len;
169 	(*tx_pkts)++;
170 	otx2_dma_unmap_skb_frags(pfvf, sg);
171 	napi_consume_skb(skb, budget);
172 	sg->skb = (u64)NULL;
173 }
174 
175 static void otx2_set_rxtstamp(struct otx2_nic *pfvf,
176 			      struct sk_buff *skb, void *data)
177 {
178 	u64 timestamp, tsns;
179 	int err;
180 
181 	if (!(pfvf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED))
182 		return;
183 
184 	timestamp = pfvf->ptp->convert_rx_ptp_tstmp(*(u64 *)data);
185 	/* The first 8 bytes is the timestamp */
186 	err = otx2_ptp_tstamp2time(pfvf, timestamp, &tsns);
187 	if (err)
188 		return;
189 
190 	skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(tsns);
191 }
192 
193 static bool otx2_skb_add_frag(struct otx2_nic *pfvf, struct sk_buff *skb,
194 			      u64 iova, int len, struct nix_rx_parse_s *parse,
195 			      int qidx)
196 {
197 	struct page *page;
198 	int off = 0;
199 	void *va;
200 
201 	va = phys_to_virt(otx2_iova_to_phys(pfvf->iommu_domain, iova));
202 
203 	if (likely(!skb_shinfo(skb)->nr_frags)) {
204 		/* Check if data starts at some nonzero offset
205 		 * from the start of the buffer.  For now the
206 		 * only possible offset is 8 bytes in the case
207 		 * where packet is prepended by a timestamp.
208 		 */
209 		if (parse->laptr) {
210 			otx2_set_rxtstamp(pfvf, skb, va);
211 			off = OTX2_HW_TIMESTAMP_LEN;
212 		}
213 	}
214 
215 	page = virt_to_page(va);
216 	if (likely(skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)) {
217 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
218 				va - page_address(page) + off,
219 				len - off, pfvf->rbsize);
220 
221 		otx2_dma_unmap_page(pfvf, iova - OTX2_HEAD_ROOM,
222 				    pfvf->rbsize, DMA_FROM_DEVICE);
223 		return true;
224 	}
225 
226 	/* If more than MAX_SKB_FRAGS fragments are received then
227 	 * give back those buffer pointers to hardware for reuse.
228 	 */
229 	pfvf->hw_ops->aura_freeptr(pfvf, qidx, iova & ~0x07ULL);
230 
231 	return false;
232 }
233 
234 static void otx2_set_rxhash(struct otx2_nic *pfvf,
235 			    struct nix_cqe_rx_s *cqe, struct sk_buff *skb)
236 {
237 	enum pkt_hash_types hash_type = PKT_HASH_TYPE_NONE;
238 	struct otx2_rss_info *rss;
239 	u32 hash = 0;
240 
241 	if (!(pfvf->netdev->features & NETIF_F_RXHASH))
242 		return;
243 
244 	rss = &pfvf->hw.rss_info;
245 	if (rss->flowkey_cfg) {
246 		if (rss->flowkey_cfg &
247 		    ~(NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6))
248 			hash_type = PKT_HASH_TYPE_L4;
249 		else
250 			hash_type = PKT_HASH_TYPE_L3;
251 		hash = cqe->hdr.flow_tag;
252 	}
253 	skb_set_hash(skb, hash, hash_type);
254 }
255 
256 static void otx2_free_rcv_seg(struct otx2_nic *pfvf, struct nix_cqe_rx_s *cqe,
257 			      int qidx)
258 {
259 	struct nix_rx_sg_s *sg = &cqe->sg;
260 	void *end, *start;
261 	u64 *seg_addr;
262 	int seg;
263 
264 	start = (void *)sg;
265 	end = start + ((cqe->parse.desc_sizem1 + 1) * 16);
266 	while (start < end) {
267 		sg = (struct nix_rx_sg_s *)start;
268 		seg_addr = &sg->seg_addr;
269 		for (seg = 0; seg < sg->segs; seg++, seg_addr++)
270 			pfvf->hw_ops->aura_freeptr(pfvf, qidx,
271 						   *seg_addr & ~0x07ULL);
272 		start += sizeof(*sg);
273 	}
274 }
275 
276 static bool otx2_check_rcv_errors(struct otx2_nic *pfvf,
277 				  struct nix_cqe_rx_s *cqe, int qidx)
278 {
279 	struct otx2_drv_stats *stats = &pfvf->hw.drv_stats;
280 	struct nix_rx_parse_s *parse = &cqe->parse;
281 
282 	if (netif_msg_rx_err(pfvf))
283 		netdev_err(pfvf->netdev,
284 			   "RQ%d: Error pkt with errlev:0x%x errcode:0x%x\n",
285 			   qidx, parse->errlev, parse->errcode);
286 
287 	if (parse->errlev == NPC_ERRLVL_RE) {
288 		switch (parse->errcode) {
289 		case ERRCODE_FCS:
290 		case ERRCODE_FCS_RCV:
291 			atomic_inc(&stats->rx_fcs_errs);
292 			break;
293 		case ERRCODE_UNDERSIZE:
294 			atomic_inc(&stats->rx_undersize_errs);
295 			break;
296 		case ERRCODE_OVERSIZE:
297 			atomic_inc(&stats->rx_oversize_errs);
298 			break;
299 		case ERRCODE_OL2_LEN_MISMATCH:
300 			atomic_inc(&stats->rx_len_errs);
301 			break;
302 		default:
303 			atomic_inc(&stats->rx_other_errs);
304 			break;
305 		}
306 	} else if (parse->errlev == NPC_ERRLVL_NIX) {
307 		switch (parse->errcode) {
308 		case ERRCODE_OL3_LEN:
309 		case ERRCODE_OL4_LEN:
310 		case ERRCODE_IL3_LEN:
311 		case ERRCODE_IL4_LEN:
312 			atomic_inc(&stats->rx_len_errs);
313 			break;
314 		case ERRCODE_OL4_CSUM:
315 		case ERRCODE_IL4_CSUM:
316 			atomic_inc(&stats->rx_csum_errs);
317 			break;
318 		default:
319 			atomic_inc(&stats->rx_other_errs);
320 			break;
321 		}
322 	} else {
323 		atomic_inc(&stats->rx_other_errs);
324 		/* For now ignore all the NPC parser errors and
325 		 * pass the packets to stack.
326 		 */
327 		return false;
328 	}
329 
330 	/* If RXALL is enabled pass on packets to stack. */
331 	if (pfvf->netdev->features & NETIF_F_RXALL)
332 		return false;
333 
334 	/* Free buffer back to pool */
335 	if (cqe->sg.segs)
336 		otx2_free_rcv_seg(pfvf, cqe, qidx);
337 	return true;
338 }
339 
340 static void otx2_rcv_pkt_handler(struct otx2_nic *pfvf,
341 				 struct napi_struct *napi,
342 				 struct otx2_cq_queue *cq,
343 				 struct nix_cqe_rx_s *cqe)
344 {
345 	struct nix_rx_parse_s *parse = &cqe->parse;
346 	struct nix_rx_sg_s *sg = &cqe->sg;
347 	struct sk_buff *skb = NULL;
348 	void *end, *start;
349 	u64 *seg_addr;
350 	u16 *seg_size;
351 	int seg;
352 
353 	if (unlikely(parse->errlev || parse->errcode)) {
354 		if (otx2_check_rcv_errors(pfvf, cqe, cq->cq_idx))
355 			return;
356 	}
357 
358 	if (pfvf->xdp_prog)
359 		if (otx2_xdp_rcv_pkt_handler(pfvf, pfvf->xdp_prog, cqe, cq))
360 			return;
361 
362 	skb = napi_get_frags(napi);
363 	if (unlikely(!skb))
364 		return;
365 
366 	start = (void *)sg;
367 	end = start + ((cqe->parse.desc_sizem1 + 1) * 16);
368 	while (start < end) {
369 		sg = (struct nix_rx_sg_s *)start;
370 		seg_addr = &sg->seg_addr;
371 		seg_size = (void *)sg;
372 		for (seg = 0; seg < sg->segs; seg++, seg_addr++) {
373 			if (otx2_skb_add_frag(pfvf, skb, *seg_addr,
374 					      seg_size[seg], parse, cq->cq_idx))
375 				cq->pool_ptrs++;
376 		}
377 		start += sizeof(*sg);
378 	}
379 	otx2_set_rxhash(pfvf, cqe, skb);
380 
381 	skb_record_rx_queue(skb, cq->cq_idx);
382 	if (pfvf->netdev->features & NETIF_F_RXCSUM)
383 		skb->ip_summed = CHECKSUM_UNNECESSARY;
384 
385 	napi_gro_frags(napi);
386 }
387 
388 static int otx2_rx_napi_handler(struct otx2_nic *pfvf,
389 				struct napi_struct *napi,
390 				struct otx2_cq_queue *cq, int budget)
391 {
392 	struct nix_cqe_rx_s *cqe;
393 	int processed_cqe = 0;
394 
395 	if (cq->pend_cqe >= budget)
396 		goto process_cqe;
397 
398 	if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe)
399 		return 0;
400 
401 process_cqe:
402 	while (likely(processed_cqe < budget) && cq->pend_cqe) {
403 		cqe = (struct nix_cqe_rx_s *)CQE_ADDR(cq, cq->cq_head);
404 		if (cqe->hdr.cqe_type == NIX_XQE_TYPE_INVALID ||
405 		    !cqe->sg.seg_addr) {
406 			if (!processed_cqe)
407 				return 0;
408 			break;
409 		}
410 		cq->cq_head++;
411 		cq->cq_head &= (cq->cqe_cnt - 1);
412 
413 		otx2_rcv_pkt_handler(pfvf, napi, cq, cqe);
414 
415 		cqe->hdr.cqe_type = NIX_XQE_TYPE_INVALID;
416 		cqe->sg.seg_addr = 0x00;
417 		processed_cqe++;
418 		cq->pend_cqe--;
419 	}
420 
421 	/* Free CQEs to HW */
422 	otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR,
423 		     ((u64)cq->cq_idx << 32) | processed_cqe);
424 
425 	return processed_cqe;
426 }
427 
428 void otx2_refill_pool_ptrs(void *dev, struct otx2_cq_queue *cq)
429 {
430 	struct otx2_nic *pfvf = dev;
431 	dma_addr_t bufptr;
432 
433 	while (cq->pool_ptrs) {
434 		if (otx2_alloc_buffer(pfvf, cq, &bufptr))
435 			break;
436 		otx2_aura_freeptr(pfvf, cq->cq_idx, bufptr + OTX2_HEAD_ROOM);
437 		cq->pool_ptrs--;
438 	}
439 }
440 
441 static int otx2_tx_napi_handler(struct otx2_nic *pfvf,
442 				struct otx2_cq_queue *cq, int budget)
443 {
444 	int tx_pkts = 0, tx_bytes = 0, qidx;
445 	struct otx2_snd_queue *sq;
446 	struct nix_cqe_tx_s *cqe;
447 	int processed_cqe = 0;
448 
449 	if (cq->pend_cqe >= budget)
450 		goto process_cqe;
451 
452 	if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe)
453 		return 0;
454 
455 process_cqe:
456 	qidx = cq->cq_idx - pfvf->hw.rx_queues;
457 	sq = &pfvf->qset.sq[qidx];
458 
459 	while (likely(processed_cqe < budget) && cq->pend_cqe) {
460 		cqe = (struct nix_cqe_tx_s *)otx2_get_next_cqe(cq);
461 		if (unlikely(!cqe)) {
462 			if (!processed_cqe)
463 				return 0;
464 			break;
465 		}
466 
467 		if (cq->cq_type == CQ_XDP) {
468 			otx2_xdp_snd_pkt_handler(pfvf, sq, cqe);
469 		} else {
470 			otx2_snd_pkt_handler(pfvf, cq, sq, cqe, budget,
471 					     &tx_pkts, &tx_bytes);
472 		}
473 
474 		cqe->hdr.cqe_type = NIX_XQE_TYPE_INVALID;
475 		processed_cqe++;
476 		cq->pend_cqe--;
477 
478 		sq->cons_head++;
479 		sq->cons_head &= (sq->sqe_cnt - 1);
480 	}
481 
482 	/* Free CQEs to HW */
483 	otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR,
484 		     ((u64)cq->cq_idx << 32) | processed_cqe);
485 
486 	if (likely(tx_pkts)) {
487 		struct netdev_queue *txq;
488 
489 		txq = netdev_get_tx_queue(pfvf->netdev, cq->cint_idx);
490 		netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
491 		/* Check if queue was stopped earlier due to ring full */
492 		smp_mb();
493 		if (netif_tx_queue_stopped(txq) &&
494 		    netif_carrier_ok(pfvf->netdev))
495 			netif_tx_wake_queue(txq);
496 	}
497 	return 0;
498 }
499 
500 static void otx2_adjust_adaptive_coalese(struct otx2_nic *pfvf, struct otx2_cq_poll *cq_poll)
501 {
502 	struct dim_sample dim_sample;
503 	u64 rx_frames, rx_bytes;
504 
505 	rx_frames = OTX2_GET_RX_STATS(RX_BCAST) + OTX2_GET_RX_STATS(RX_MCAST) +
506 		OTX2_GET_RX_STATS(RX_UCAST);
507 	rx_bytes = OTX2_GET_RX_STATS(RX_OCTS);
508 	dim_update_sample(pfvf->napi_events, rx_frames, rx_bytes, &dim_sample);
509 	net_dim(&cq_poll->dim, dim_sample);
510 }
511 
512 int otx2_napi_handler(struct napi_struct *napi, int budget)
513 {
514 	struct otx2_cq_queue *rx_cq = NULL;
515 	struct otx2_cq_poll *cq_poll;
516 	int workdone = 0, cq_idx, i;
517 	struct otx2_cq_queue *cq;
518 	struct otx2_qset *qset;
519 	struct otx2_nic *pfvf;
520 
521 	cq_poll = container_of(napi, struct otx2_cq_poll, napi);
522 	pfvf = (struct otx2_nic *)cq_poll->dev;
523 	qset = &pfvf->qset;
524 
525 	for (i = 0; i < CQS_PER_CINT; i++) {
526 		cq_idx = cq_poll->cq_ids[i];
527 		if (unlikely(cq_idx == CINT_INVALID_CQ))
528 			continue;
529 		cq = &qset->cq[cq_idx];
530 		if (cq->cq_type == CQ_RX) {
531 			rx_cq = cq;
532 			workdone += otx2_rx_napi_handler(pfvf, napi,
533 							 cq, budget);
534 		} else {
535 			workdone += otx2_tx_napi_handler(pfvf, cq, budget);
536 		}
537 	}
538 
539 	if (rx_cq && rx_cq->pool_ptrs)
540 		pfvf->hw_ops->refill_pool_ptrs(pfvf, rx_cq);
541 	/* Clear the IRQ */
542 	otx2_write64(pfvf, NIX_LF_CINTX_INT(cq_poll->cint_idx), BIT_ULL(0));
543 
544 	if (workdone < budget && napi_complete_done(napi, workdone)) {
545 		/* If interface is going down, don't re-enable IRQ */
546 		if (pfvf->flags & OTX2_FLAG_INTF_DOWN)
547 			return workdone;
548 
549 		/* Check for adaptive interrupt coalesce */
550 		if (workdone != 0 &&
551 		    ((pfvf->flags & OTX2_FLAG_ADPTV_INT_COAL_ENABLED) ==
552 		     OTX2_FLAG_ADPTV_INT_COAL_ENABLED)) {
553 			/* Adjust irq coalese using net_dim */
554 			otx2_adjust_adaptive_coalese(pfvf, cq_poll);
555 			/* Update irq coalescing */
556 			for (i = 0; i < pfvf->hw.cint_cnt; i++)
557 				otx2_config_irq_coalescing(pfvf, i);
558 		}
559 
560 		/* Re-enable interrupts */
561 		otx2_write64(pfvf, NIX_LF_CINTX_ENA_W1S(cq_poll->cint_idx),
562 			     BIT_ULL(0));
563 	}
564 	return workdone;
565 }
566 
567 void otx2_sqe_flush(void *dev, struct otx2_snd_queue *sq,
568 		    int size, int qidx)
569 {
570 	u64 status;
571 
572 	/* Packet data stores should finish before SQE is flushed to HW */
573 	dma_wmb();
574 
575 	do {
576 		memcpy(sq->lmt_addr, sq->sqe_base, size);
577 		status = otx2_lmt_flush(sq->io_addr);
578 	} while (status == 0);
579 
580 	sq->head++;
581 	sq->head &= (sq->sqe_cnt - 1);
582 }
583 
584 #define MAX_SEGS_PER_SG	3
585 /* Add SQE scatter/gather subdescriptor structure */
586 static bool otx2_sqe_add_sg(struct otx2_nic *pfvf, struct otx2_snd_queue *sq,
587 			    struct sk_buff *skb, int num_segs, int *offset)
588 {
589 	struct nix_sqe_sg_s *sg = NULL;
590 	u64 dma_addr, *iova = NULL;
591 	u16 *sg_lens = NULL;
592 	int seg, len;
593 
594 	sq->sg[sq->head].num_segs = 0;
595 
596 	for (seg = 0; seg < num_segs; seg++) {
597 		if ((seg % MAX_SEGS_PER_SG) == 0) {
598 			sg = (struct nix_sqe_sg_s *)(sq->sqe_base + *offset);
599 			sg->ld_type = NIX_SEND_LDTYPE_LDD;
600 			sg->subdc = NIX_SUBDC_SG;
601 			sg->segs = 0;
602 			sg_lens = (void *)sg;
603 			iova = (void *)sg + sizeof(*sg);
604 			/* Next subdc always starts at a 16byte boundary.
605 			 * So if sg->segs is whether 2 or 3, offset += 16bytes.
606 			 */
607 			if ((num_segs - seg) >= (MAX_SEGS_PER_SG - 1))
608 				*offset += sizeof(*sg) + (3 * sizeof(u64));
609 			else
610 				*offset += sizeof(*sg) + sizeof(u64);
611 		}
612 		dma_addr = otx2_dma_map_skb_frag(pfvf, skb, seg, &len);
613 		if (dma_mapping_error(pfvf->dev, dma_addr))
614 			return false;
615 
616 		sg_lens[frag_num(seg % MAX_SEGS_PER_SG)] = len;
617 		sg->segs++;
618 		*iova++ = dma_addr;
619 
620 		/* Save DMA mapping info for later unmapping */
621 		sq->sg[sq->head].dma_addr[seg] = dma_addr;
622 		sq->sg[sq->head].size[seg] = len;
623 		sq->sg[sq->head].num_segs++;
624 	}
625 
626 	sq->sg[sq->head].skb = (u64)skb;
627 	return true;
628 }
629 
630 /* Add SQE extended header subdescriptor */
631 static void otx2_sqe_add_ext(struct otx2_nic *pfvf, struct otx2_snd_queue *sq,
632 			     struct sk_buff *skb, int *offset)
633 {
634 	struct nix_sqe_ext_s *ext;
635 
636 	ext = (struct nix_sqe_ext_s *)(sq->sqe_base + *offset);
637 	ext->subdc = NIX_SUBDC_EXT;
638 	if (skb_shinfo(skb)->gso_size) {
639 		ext->lso = 1;
640 		ext->lso_sb = skb_tcp_all_headers(skb);
641 		ext->lso_mps = skb_shinfo(skb)->gso_size;
642 
643 		/* Only TSOv4 and TSOv6 GSO offloads are supported */
644 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
645 			ext->lso_format = pfvf->hw.lso_tsov4_idx;
646 
647 			/* HW adds payload size to 'ip_hdr->tot_len' while
648 			 * sending TSO segment, hence set payload length
649 			 * in IP header of the packet to just header length.
650 			 */
651 			ip_hdr(skb)->tot_len =
652 				htons(ext->lso_sb - skb_network_offset(skb));
653 		} else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
654 			ext->lso_format = pfvf->hw.lso_tsov6_idx;
655 			ipv6_hdr(skb)->payload_len = htons(tcp_hdrlen(skb));
656 		} else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
657 			__be16 l3_proto = vlan_get_protocol(skb);
658 			struct udphdr *udph = udp_hdr(skb);
659 			u16 iplen;
660 
661 			ext->lso_sb = skb_transport_offset(skb) +
662 					sizeof(struct udphdr);
663 
664 			/* HW adds payload size to length fields in IP and
665 			 * UDP headers while segmentation, hence adjust the
666 			 * lengths to just header sizes.
667 			 */
668 			iplen = htons(ext->lso_sb - skb_network_offset(skb));
669 			if (l3_proto == htons(ETH_P_IP)) {
670 				ip_hdr(skb)->tot_len = iplen;
671 				ext->lso_format = pfvf->hw.lso_udpv4_idx;
672 			} else {
673 				ipv6_hdr(skb)->payload_len = iplen;
674 				ext->lso_format = pfvf->hw.lso_udpv6_idx;
675 			}
676 
677 			udph->len = htons(sizeof(struct udphdr));
678 		}
679 	} else if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
680 		ext->tstmp = 1;
681 	}
682 
683 #define OTX2_VLAN_PTR_OFFSET     (ETH_HLEN - ETH_TLEN)
684 	if (skb_vlan_tag_present(skb)) {
685 		if (skb->vlan_proto == htons(ETH_P_8021Q)) {
686 			ext->vlan1_ins_ena = 1;
687 			ext->vlan1_ins_ptr = OTX2_VLAN_PTR_OFFSET;
688 			ext->vlan1_ins_tci = skb_vlan_tag_get(skb);
689 		} else if (skb->vlan_proto == htons(ETH_P_8021AD)) {
690 			ext->vlan0_ins_ena = 1;
691 			ext->vlan0_ins_ptr = OTX2_VLAN_PTR_OFFSET;
692 			ext->vlan0_ins_tci = skb_vlan_tag_get(skb);
693 		}
694 	}
695 
696 	*offset += sizeof(*ext);
697 }
698 
699 static void otx2_sqe_add_mem(struct otx2_snd_queue *sq, int *offset,
700 			     int alg, u64 iova, int ptp_offset,
701 			     u64 base_ns, bool udp_csum_crt)
702 {
703 	struct nix_sqe_mem_s *mem;
704 
705 	mem = (struct nix_sqe_mem_s *)(sq->sqe_base + *offset);
706 	mem->subdc = NIX_SUBDC_MEM;
707 	mem->alg = alg;
708 	mem->wmem = 1; /* wait for the memory operation */
709 	mem->addr = iova;
710 
711 	if (ptp_offset) {
712 		mem->start_offset = ptp_offset;
713 		mem->udp_csum_crt = !!udp_csum_crt;
714 		mem->base_ns = base_ns;
715 		mem->step_type = 1;
716 	}
717 
718 	*offset += sizeof(*mem);
719 }
720 
721 /* Add SQE header subdescriptor structure */
722 static void otx2_sqe_add_hdr(struct otx2_nic *pfvf, struct otx2_snd_queue *sq,
723 			     struct nix_sqe_hdr_s *sqe_hdr,
724 			     struct sk_buff *skb, u16 qidx)
725 {
726 	int proto = 0;
727 
728 	/* Check if SQE was framed before, if yes then no need to
729 	 * set these constants again and again.
730 	 */
731 	if (!sqe_hdr->total) {
732 		/* Don't free Tx buffers to Aura */
733 		sqe_hdr->df = 1;
734 		sqe_hdr->aura = sq->aura_id;
735 		/* Post a CQE Tx after pkt transmission */
736 		sqe_hdr->pnc = 1;
737 		sqe_hdr->sq = qidx;
738 	}
739 	sqe_hdr->total = skb->len;
740 	/* Set SQE identifier which will be used later for freeing SKB */
741 	sqe_hdr->sqe_id = sq->head;
742 
743 	/* Offload TCP/UDP checksum to HW */
744 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
745 		sqe_hdr->ol3ptr = skb_network_offset(skb);
746 		sqe_hdr->ol4ptr = skb_transport_offset(skb);
747 		/* get vlan protocol Ethertype */
748 		if (eth_type_vlan(skb->protocol))
749 			skb->protocol = vlan_get_protocol(skb);
750 
751 		if (skb->protocol == htons(ETH_P_IP)) {
752 			proto = ip_hdr(skb)->protocol;
753 			/* In case of TSO, HW needs this to be explicitly set.
754 			 * So set this always, instead of adding a check.
755 			 */
756 			sqe_hdr->ol3type = NIX_SENDL3TYPE_IP4_CKSUM;
757 		} else if (skb->protocol == htons(ETH_P_IPV6)) {
758 			proto = ipv6_hdr(skb)->nexthdr;
759 			sqe_hdr->ol3type = NIX_SENDL3TYPE_IP6;
760 		}
761 
762 		if (proto == IPPROTO_TCP)
763 			sqe_hdr->ol4type = NIX_SENDL4TYPE_TCP_CKSUM;
764 		else if (proto == IPPROTO_UDP)
765 			sqe_hdr->ol4type = NIX_SENDL4TYPE_UDP_CKSUM;
766 	}
767 }
768 
769 static int otx2_dma_map_tso_skb(struct otx2_nic *pfvf,
770 				struct otx2_snd_queue *sq,
771 				struct sk_buff *skb, int sqe, int hdr_len)
772 {
773 	int num_segs = skb_shinfo(skb)->nr_frags + 1;
774 	struct sg_list *sg = &sq->sg[sqe];
775 	u64 dma_addr;
776 	int seg, len;
777 
778 	sg->num_segs = 0;
779 
780 	/* Get payload length at skb->data */
781 	len = skb_headlen(skb) - hdr_len;
782 
783 	for (seg = 0; seg < num_segs; seg++) {
784 		/* Skip skb->data, if there is no payload */
785 		if (!seg && !len)
786 			continue;
787 		dma_addr = otx2_dma_map_skb_frag(pfvf, skb, seg, &len);
788 		if (dma_mapping_error(pfvf->dev, dma_addr))
789 			goto unmap;
790 
791 		/* Save DMA mapping info for later unmapping */
792 		sg->dma_addr[sg->num_segs] = dma_addr;
793 		sg->size[sg->num_segs] = len;
794 		sg->num_segs++;
795 	}
796 	return 0;
797 unmap:
798 	otx2_dma_unmap_skb_frags(pfvf, sg);
799 	return -EINVAL;
800 }
801 
802 static u64 otx2_tso_frag_dma_addr(struct otx2_snd_queue *sq,
803 				  struct sk_buff *skb, int seg,
804 				  u64 seg_addr, int hdr_len, int sqe)
805 {
806 	struct sg_list *sg = &sq->sg[sqe];
807 	const skb_frag_t *frag;
808 	int offset;
809 
810 	if (seg < 0)
811 		return sg->dma_addr[0] + (seg_addr - (u64)skb->data);
812 
813 	frag = &skb_shinfo(skb)->frags[seg];
814 	offset = seg_addr - (u64)skb_frag_address(frag);
815 	if (skb_headlen(skb) - hdr_len)
816 		seg++;
817 	return sg->dma_addr[seg] + offset;
818 }
819 
820 static void otx2_sqe_tso_add_sg(struct otx2_snd_queue *sq,
821 				struct sg_list *list, int *offset)
822 {
823 	struct nix_sqe_sg_s *sg = NULL;
824 	u16 *sg_lens = NULL;
825 	u64 *iova = NULL;
826 	int seg;
827 
828 	/* Add SG descriptors with buffer addresses */
829 	for (seg = 0; seg < list->num_segs; seg++) {
830 		if ((seg % MAX_SEGS_PER_SG) == 0) {
831 			sg = (struct nix_sqe_sg_s *)(sq->sqe_base + *offset);
832 			sg->ld_type = NIX_SEND_LDTYPE_LDD;
833 			sg->subdc = NIX_SUBDC_SG;
834 			sg->segs = 0;
835 			sg_lens = (void *)sg;
836 			iova = (void *)sg + sizeof(*sg);
837 			/* Next subdc always starts at a 16byte boundary.
838 			 * So if sg->segs is whether 2 or 3, offset += 16bytes.
839 			 */
840 			if ((list->num_segs - seg) >= (MAX_SEGS_PER_SG - 1))
841 				*offset += sizeof(*sg) + (3 * sizeof(u64));
842 			else
843 				*offset += sizeof(*sg) + sizeof(u64);
844 		}
845 		sg_lens[frag_num(seg % MAX_SEGS_PER_SG)] = list->size[seg];
846 		*iova++ = list->dma_addr[seg];
847 		sg->segs++;
848 	}
849 }
850 
851 static void otx2_sq_append_tso(struct otx2_nic *pfvf, struct otx2_snd_queue *sq,
852 			       struct sk_buff *skb, u16 qidx)
853 {
854 	struct netdev_queue *txq = netdev_get_tx_queue(pfvf->netdev, qidx);
855 	int hdr_len, tcp_data, seg_len, pkt_len, offset;
856 	struct nix_sqe_hdr_s *sqe_hdr;
857 	int first_sqe = sq->head;
858 	struct sg_list list;
859 	struct tso_t tso;
860 
861 	hdr_len = tso_start(skb, &tso);
862 
863 	/* Map SKB's fragments to DMA.
864 	 * It's done here to avoid mapping for every TSO segment's packet.
865 	 */
866 	if (otx2_dma_map_tso_skb(pfvf, sq, skb, first_sqe, hdr_len)) {
867 		dev_kfree_skb_any(skb);
868 		return;
869 	}
870 
871 	netdev_tx_sent_queue(txq, skb->len);
872 
873 	tcp_data = skb->len - hdr_len;
874 	while (tcp_data > 0) {
875 		char *hdr;
876 
877 		seg_len = min_t(int, skb_shinfo(skb)->gso_size, tcp_data);
878 		tcp_data -= seg_len;
879 
880 		/* Set SQE's SEND_HDR */
881 		memset(sq->sqe_base, 0, sq->sqe_size);
882 		sqe_hdr = (struct nix_sqe_hdr_s *)(sq->sqe_base);
883 		otx2_sqe_add_hdr(pfvf, sq, sqe_hdr, skb, qidx);
884 		offset = sizeof(*sqe_hdr);
885 
886 		/* Add TSO segment's pkt header */
887 		hdr = sq->tso_hdrs->base + (sq->head * TSO_HEADER_SIZE);
888 		tso_build_hdr(skb, hdr, &tso, seg_len, tcp_data == 0);
889 		list.dma_addr[0] =
890 			sq->tso_hdrs->iova + (sq->head * TSO_HEADER_SIZE);
891 		list.size[0] = hdr_len;
892 		list.num_segs = 1;
893 
894 		/* Add TSO segment's payload data fragments */
895 		pkt_len = hdr_len;
896 		while (seg_len > 0) {
897 			int size;
898 
899 			size = min_t(int, tso.size, seg_len);
900 
901 			list.size[list.num_segs] = size;
902 			list.dma_addr[list.num_segs] =
903 				otx2_tso_frag_dma_addr(sq, skb,
904 						       tso.next_frag_idx - 1,
905 						       (u64)tso.data, hdr_len,
906 						       first_sqe);
907 			list.num_segs++;
908 			pkt_len += size;
909 			seg_len -= size;
910 			tso_build_data(skb, &tso, size);
911 		}
912 		sqe_hdr->total = pkt_len;
913 		otx2_sqe_tso_add_sg(sq, &list, &offset);
914 
915 		/* DMA mappings and skb needs to be freed only after last
916 		 * TSO segment is transmitted out. So set 'PNC' only for
917 		 * last segment. Also point last segment's sqe_id to first
918 		 * segment's SQE index where skb address and DMA mappings
919 		 * are saved.
920 		 */
921 		if (!tcp_data) {
922 			sqe_hdr->pnc = 1;
923 			sqe_hdr->sqe_id = first_sqe;
924 			sq->sg[first_sqe].skb = (u64)skb;
925 		} else {
926 			sqe_hdr->pnc = 0;
927 		}
928 
929 		sqe_hdr->sizem1 = (offset / 16) - 1;
930 
931 		/* Flush SQE to HW */
932 		pfvf->hw_ops->sqe_flush(pfvf, sq, offset, qidx);
933 	}
934 }
935 
936 static bool is_hw_tso_supported(struct otx2_nic *pfvf,
937 				struct sk_buff *skb)
938 {
939 	int payload_len, last_seg_size;
940 
941 	if (test_bit(HW_TSO, &pfvf->hw.cap_flag))
942 		return true;
943 
944 	/* On 96xx A0, HW TSO not supported */
945 	if (!is_96xx_B0(pfvf->pdev))
946 		return false;
947 
948 	/* HW has an issue due to which when the payload of the last LSO
949 	 * segment is shorter than 16 bytes, some header fields may not
950 	 * be correctly modified, hence don't offload such TSO segments.
951 	 */
952 
953 	payload_len = skb->len - skb_tcp_all_headers(skb);
954 	last_seg_size = payload_len % skb_shinfo(skb)->gso_size;
955 	if (last_seg_size && last_seg_size < 16)
956 		return false;
957 
958 	return true;
959 }
960 
961 static int otx2_get_sqe_count(struct otx2_nic *pfvf, struct sk_buff *skb)
962 {
963 	if (!skb_shinfo(skb)->gso_size)
964 		return 1;
965 
966 	/* HW TSO */
967 	if (is_hw_tso_supported(pfvf, skb))
968 		return 1;
969 
970 	/* SW TSO */
971 	return skb_shinfo(skb)->gso_segs;
972 }
973 
974 static bool otx2_validate_network_transport(struct sk_buff *skb)
975 {
976 	if ((ip_hdr(skb)->protocol == IPPROTO_UDP) ||
977 	    (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)) {
978 		struct udphdr *udph = udp_hdr(skb);
979 
980 		if (udph->source == htons(PTP_PORT) &&
981 		    udph->dest == htons(PTP_PORT))
982 			return true;
983 	}
984 
985 	return false;
986 }
987 
988 static bool otx2_ptp_is_sync(struct sk_buff *skb, int *offset, bool *udp_csum_crt)
989 {
990 	struct ethhdr *eth = (struct ethhdr *)(skb->data);
991 	u16 nix_offload_hlen = 0, inner_vhlen = 0;
992 	bool udp_hdr_present = false, is_sync;
993 	u8 *data = skb->data, *msgtype;
994 	__be16 proto = eth->h_proto;
995 	int network_depth = 0;
996 
997 	/* NIX is programmed to offload outer  VLAN header
998 	 * in case of single vlan protocol field holds Network header ETH_IP/V6
999 	 * in case of stacked vlan protocol field holds Inner vlan (8100)
1000 	 */
1001 	if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX &&
1002 	    skb->dev->features & NETIF_F_HW_VLAN_STAG_TX) {
1003 		if (skb->vlan_proto == htons(ETH_P_8021AD)) {
1004 			/* Get vlan protocol */
1005 			proto = __vlan_get_protocol(skb, eth->h_proto, NULL);
1006 			/* SKB APIs like skb_transport_offset does not include
1007 			 * offloaded vlan header length. Need to explicitly add
1008 			 * the length
1009 			 */
1010 			nix_offload_hlen = VLAN_HLEN;
1011 			inner_vhlen = VLAN_HLEN;
1012 		} else if (skb->vlan_proto == htons(ETH_P_8021Q)) {
1013 			nix_offload_hlen = VLAN_HLEN;
1014 		}
1015 	} else if (eth_type_vlan(eth->h_proto)) {
1016 		proto = __vlan_get_protocol(skb, eth->h_proto, &network_depth);
1017 	}
1018 
1019 	switch (ntohs(proto)) {
1020 	case ETH_P_1588:
1021 		if (network_depth)
1022 			*offset = network_depth;
1023 		else
1024 			*offset = ETH_HLEN + nix_offload_hlen +
1025 				  inner_vhlen;
1026 		break;
1027 	case ETH_P_IP:
1028 	case ETH_P_IPV6:
1029 		if (!otx2_validate_network_transport(skb))
1030 			return false;
1031 
1032 		*offset = nix_offload_hlen + skb_transport_offset(skb) +
1033 			  sizeof(struct udphdr);
1034 		udp_hdr_present = true;
1035 
1036 	}
1037 
1038 	msgtype = data + *offset;
1039 	/* Check PTP messageId is SYNC or not */
1040 	is_sync = !(*msgtype & 0xf);
1041 	if (is_sync)
1042 		*udp_csum_crt = udp_hdr_present;
1043 	else
1044 		*offset = 0;
1045 
1046 	return is_sync;
1047 }
1048 
1049 static void otx2_set_txtstamp(struct otx2_nic *pfvf, struct sk_buff *skb,
1050 			      struct otx2_snd_queue *sq, int *offset)
1051 {
1052 	struct ethhdr	*eth = (struct ethhdr *)(skb->data);
1053 	struct ptpv2_tstamp *origin_tstamp;
1054 	bool udp_csum_crt = false;
1055 	unsigned int udphoff;
1056 	struct timespec64 ts;
1057 	int ptp_offset = 0;
1058 	__wsum skb_csum;
1059 	u64 iova;
1060 
1061 	if (unlikely(!skb_shinfo(skb)->gso_size &&
1062 		     (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))) {
1063 		if (unlikely(pfvf->flags & OTX2_FLAG_PTP_ONESTEP_SYNC &&
1064 			     otx2_ptp_is_sync(skb, &ptp_offset, &udp_csum_crt))) {
1065 			origin_tstamp = (struct ptpv2_tstamp *)
1066 					((u8 *)skb->data + ptp_offset +
1067 					 PTP_SYNC_SEC_OFFSET);
1068 			ts = ns_to_timespec64(pfvf->ptp->tstamp);
1069 			origin_tstamp->seconds_msb = htons((ts.tv_sec >> 32) & 0xffff);
1070 			origin_tstamp->seconds_lsb = htonl(ts.tv_sec & 0xffffffff);
1071 			origin_tstamp->nanoseconds = htonl(ts.tv_nsec);
1072 			/* Point to correction field in PTP packet */
1073 			ptp_offset += 8;
1074 
1075 			/* When user disables hw checksum, stack calculates the csum,
1076 			 * but it does not cover ptp timestamp which is added later.
1077 			 * Recalculate the checksum manually considering the timestamp.
1078 			 */
1079 			if (udp_csum_crt) {
1080 				struct udphdr *uh = udp_hdr(skb);
1081 
1082 				if (skb->ip_summed != CHECKSUM_PARTIAL && uh->check != 0) {
1083 					udphoff = skb_transport_offset(skb);
1084 					uh->check = 0;
1085 					skb_csum = skb_checksum(skb, udphoff, skb->len - udphoff,
1086 								0);
1087 					if (ntohs(eth->h_proto) == ETH_P_IPV6)
1088 						uh->check = csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1089 									    &ipv6_hdr(skb)->daddr,
1090 									    skb->len - udphoff,
1091 									    ipv6_hdr(skb)->nexthdr,
1092 									    skb_csum);
1093 					else
1094 						uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
1095 									      ip_hdr(skb)->daddr,
1096 									      skb->len - udphoff,
1097 									      IPPROTO_UDP,
1098 									      skb_csum);
1099 				}
1100 			}
1101 		} else {
1102 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1103 		}
1104 		iova = sq->timestamps->iova + (sq->head * sizeof(u64));
1105 		otx2_sqe_add_mem(sq, offset, NIX_SENDMEMALG_E_SETTSTMP, iova,
1106 				 ptp_offset, pfvf->ptp->base_ns, udp_csum_crt);
1107 	} else {
1108 		skb_tx_timestamp(skb);
1109 	}
1110 }
1111 
1112 bool otx2_sq_append_skb(struct net_device *netdev, struct otx2_snd_queue *sq,
1113 			struct sk_buff *skb, u16 qidx)
1114 {
1115 	struct netdev_queue *txq = netdev_get_tx_queue(netdev, qidx);
1116 	struct otx2_nic *pfvf = netdev_priv(netdev);
1117 	int offset, num_segs, free_desc;
1118 	struct nix_sqe_hdr_s *sqe_hdr;
1119 
1120 	/* Check if there is enough room between producer
1121 	 * and consumer index.
1122 	 */
1123 	free_desc = (sq->cons_head - sq->head - 1 + sq->sqe_cnt) & (sq->sqe_cnt - 1);
1124 	if (free_desc < sq->sqe_thresh)
1125 		return false;
1126 
1127 	if (free_desc < otx2_get_sqe_count(pfvf, skb))
1128 		return false;
1129 
1130 	num_segs = skb_shinfo(skb)->nr_frags + 1;
1131 
1132 	/* If SKB doesn't fit in a single SQE, linearize it.
1133 	 * TODO: Consider adding JUMP descriptor instead.
1134 	 */
1135 	if (unlikely(num_segs > OTX2_MAX_FRAGS_IN_SQE)) {
1136 		if (__skb_linearize(skb)) {
1137 			dev_kfree_skb_any(skb);
1138 			return true;
1139 		}
1140 		num_segs = skb_shinfo(skb)->nr_frags + 1;
1141 	}
1142 
1143 	if (skb_shinfo(skb)->gso_size && !is_hw_tso_supported(pfvf, skb)) {
1144 		/* Insert vlan tag before giving pkt to tso */
1145 		if (skb_vlan_tag_present(skb))
1146 			skb = __vlan_hwaccel_push_inside(skb);
1147 		otx2_sq_append_tso(pfvf, sq, skb, qidx);
1148 		return true;
1149 	}
1150 
1151 	/* Set SQE's SEND_HDR.
1152 	 * Do not clear the first 64bit as it contains constant info.
1153 	 */
1154 	memset(sq->sqe_base + 8, 0, sq->sqe_size - 8);
1155 	sqe_hdr = (struct nix_sqe_hdr_s *)(sq->sqe_base);
1156 	otx2_sqe_add_hdr(pfvf, sq, sqe_hdr, skb, qidx);
1157 	offset = sizeof(*sqe_hdr);
1158 
1159 	/* Add extended header if needed */
1160 	otx2_sqe_add_ext(pfvf, sq, skb, &offset);
1161 
1162 	/* Add SG subdesc with data frags */
1163 	if (!otx2_sqe_add_sg(pfvf, sq, skb, num_segs, &offset)) {
1164 		otx2_dma_unmap_skb_frags(pfvf, &sq->sg[sq->head]);
1165 		return false;
1166 	}
1167 
1168 	otx2_set_txtstamp(pfvf, skb, sq, &offset);
1169 
1170 	sqe_hdr->sizem1 = (offset / 16) - 1;
1171 
1172 	netdev_tx_sent_queue(txq, skb->len);
1173 
1174 	/* Flush SQE to HW */
1175 	pfvf->hw_ops->sqe_flush(pfvf, sq, offset, qidx);
1176 
1177 	return true;
1178 }
1179 EXPORT_SYMBOL(otx2_sq_append_skb);
1180 
1181 void otx2_cleanup_rx_cqes(struct otx2_nic *pfvf, struct otx2_cq_queue *cq)
1182 {
1183 	struct nix_cqe_rx_s *cqe;
1184 	int processed_cqe = 0;
1185 	u64 iova, pa;
1186 
1187 	if (pfvf->xdp_prog)
1188 		xdp_rxq_info_unreg(&cq->xdp_rxq);
1189 
1190 	if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe)
1191 		return;
1192 
1193 	while (cq->pend_cqe) {
1194 		cqe = (struct nix_cqe_rx_s *)otx2_get_next_cqe(cq);
1195 		processed_cqe++;
1196 		cq->pend_cqe--;
1197 
1198 		if (!cqe)
1199 			continue;
1200 		if (cqe->sg.segs > 1) {
1201 			otx2_free_rcv_seg(pfvf, cqe, cq->cq_idx);
1202 			continue;
1203 		}
1204 		iova = cqe->sg.seg_addr - OTX2_HEAD_ROOM;
1205 		pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
1206 		otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize, DMA_FROM_DEVICE);
1207 		put_page(virt_to_page(phys_to_virt(pa)));
1208 	}
1209 
1210 	/* Free CQEs to HW */
1211 	otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR,
1212 		     ((u64)cq->cq_idx << 32) | processed_cqe);
1213 }
1214 
1215 void otx2_cleanup_tx_cqes(struct otx2_nic *pfvf, struct otx2_cq_queue *cq)
1216 {
1217 	struct sk_buff *skb = NULL;
1218 	struct otx2_snd_queue *sq;
1219 	struct nix_cqe_tx_s *cqe;
1220 	int processed_cqe = 0;
1221 	struct sg_list *sg;
1222 
1223 	sq = &pfvf->qset.sq[cq->cint_idx];
1224 
1225 	if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe)
1226 		return;
1227 
1228 	while (cq->pend_cqe) {
1229 		cqe = (struct nix_cqe_tx_s *)otx2_get_next_cqe(cq);
1230 		processed_cqe++;
1231 		cq->pend_cqe--;
1232 
1233 		if (!cqe)
1234 			continue;
1235 		sg = &sq->sg[cqe->comp.sqe_id];
1236 		skb = (struct sk_buff *)sg->skb;
1237 		if (skb) {
1238 			otx2_dma_unmap_skb_frags(pfvf, sg);
1239 			dev_kfree_skb_any(skb);
1240 			sg->skb = (u64)NULL;
1241 		}
1242 	}
1243 
1244 	/* Free CQEs to HW */
1245 	otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR,
1246 		     ((u64)cq->cq_idx << 32) | processed_cqe);
1247 }
1248 
1249 int otx2_rxtx_enable(struct otx2_nic *pfvf, bool enable)
1250 {
1251 	struct msg_req *msg;
1252 	int err;
1253 
1254 	mutex_lock(&pfvf->mbox.lock);
1255 	if (enable)
1256 		msg = otx2_mbox_alloc_msg_nix_lf_start_rx(&pfvf->mbox);
1257 	else
1258 		msg = otx2_mbox_alloc_msg_nix_lf_stop_rx(&pfvf->mbox);
1259 
1260 	if (!msg) {
1261 		mutex_unlock(&pfvf->mbox.lock);
1262 		return -ENOMEM;
1263 	}
1264 
1265 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1266 	mutex_unlock(&pfvf->mbox.lock);
1267 	return err;
1268 }
1269 
1270 static void otx2_xdp_sqe_add_sg(struct otx2_snd_queue *sq, u64 dma_addr,
1271 				int len, int *offset)
1272 {
1273 	struct nix_sqe_sg_s *sg = NULL;
1274 	u64 *iova = NULL;
1275 
1276 	sg = (struct nix_sqe_sg_s *)(sq->sqe_base + *offset);
1277 	sg->ld_type = NIX_SEND_LDTYPE_LDD;
1278 	sg->subdc = NIX_SUBDC_SG;
1279 	sg->segs = 1;
1280 	sg->seg1_size = len;
1281 	iova = (void *)sg + sizeof(*sg);
1282 	*iova = dma_addr;
1283 	*offset += sizeof(*sg) + sizeof(u64);
1284 
1285 	sq->sg[sq->head].dma_addr[0] = dma_addr;
1286 	sq->sg[sq->head].size[0] = len;
1287 	sq->sg[sq->head].num_segs = 1;
1288 }
1289 
1290 bool otx2_xdp_sq_append_pkt(struct otx2_nic *pfvf, u64 iova, int len, u16 qidx)
1291 {
1292 	struct nix_sqe_hdr_s *sqe_hdr;
1293 	struct otx2_snd_queue *sq;
1294 	int offset, free_sqe;
1295 
1296 	sq = &pfvf->qset.sq[qidx];
1297 	free_sqe = (sq->num_sqbs - *sq->aura_fc_addr) * sq->sqe_per_sqb;
1298 	if (free_sqe < sq->sqe_thresh)
1299 		return false;
1300 
1301 	memset(sq->sqe_base + 8, 0, sq->sqe_size - 8);
1302 
1303 	sqe_hdr = (struct nix_sqe_hdr_s *)(sq->sqe_base);
1304 
1305 	if (!sqe_hdr->total) {
1306 		sqe_hdr->aura = sq->aura_id;
1307 		sqe_hdr->df = 1;
1308 		sqe_hdr->sq = qidx;
1309 		sqe_hdr->pnc = 1;
1310 	}
1311 	sqe_hdr->total = len;
1312 	sqe_hdr->sqe_id = sq->head;
1313 
1314 	offset = sizeof(*sqe_hdr);
1315 
1316 	otx2_xdp_sqe_add_sg(sq, iova, len, &offset);
1317 	sqe_hdr->sizem1 = (offset / 16) - 1;
1318 	pfvf->hw_ops->sqe_flush(pfvf, sq, offset, qidx);
1319 
1320 	return true;
1321 }
1322 
1323 static bool otx2_xdp_rcv_pkt_handler(struct otx2_nic *pfvf,
1324 				     struct bpf_prog *prog,
1325 				     struct nix_cqe_rx_s *cqe,
1326 				     struct otx2_cq_queue *cq)
1327 {
1328 	unsigned char *hard_start, *data;
1329 	int qidx = cq->cq_idx;
1330 	struct xdp_buff xdp;
1331 	struct page *page;
1332 	u64 iova, pa;
1333 	u32 act;
1334 	int err;
1335 
1336 	iova = cqe->sg.seg_addr - OTX2_HEAD_ROOM;
1337 	pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
1338 	page = virt_to_page(phys_to_virt(pa));
1339 
1340 	xdp_init_buff(&xdp, pfvf->rbsize, &cq->xdp_rxq);
1341 
1342 	data = (unsigned char *)phys_to_virt(pa);
1343 	hard_start = page_address(page);
1344 	xdp_prepare_buff(&xdp, hard_start, data - hard_start,
1345 			 cqe->sg.seg_size, false);
1346 
1347 	act = bpf_prog_run_xdp(prog, &xdp);
1348 
1349 	switch (act) {
1350 	case XDP_PASS:
1351 		break;
1352 	case XDP_TX:
1353 		qidx += pfvf->hw.tx_queues;
1354 		cq->pool_ptrs++;
1355 		return otx2_xdp_sq_append_pkt(pfvf, iova,
1356 					      cqe->sg.seg_size, qidx);
1357 	case XDP_REDIRECT:
1358 		cq->pool_ptrs++;
1359 		err = xdp_do_redirect(pfvf->netdev, &xdp, prog);
1360 
1361 		otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize,
1362 				    DMA_FROM_DEVICE);
1363 		if (!err)
1364 			return true;
1365 		put_page(page);
1366 		break;
1367 	default:
1368 		bpf_warn_invalid_xdp_action(pfvf->netdev, prog, act);
1369 		break;
1370 	case XDP_ABORTED:
1371 		trace_xdp_exception(pfvf->netdev, prog, act);
1372 		break;
1373 	case XDP_DROP:
1374 		otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize,
1375 				    DMA_FROM_DEVICE);
1376 		put_page(page);
1377 		cq->pool_ptrs++;
1378 		return true;
1379 	}
1380 	return false;
1381 }
1382