• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 #include "otx2_reg.h"
13 #include "otx2_common.h"
14 #include "otx2_struct.h"
15 #include "otx2_txrx.h"
16 #include "otx2_ptp.h"
17 #include "cn10k.h"
18 
19 #define CQE_ADDR(CQ, idx) ((CQ)->cqe_base + ((CQ)->cqe_size * (idx)))
20 
otx2_nix_cq_op_status(struct otx2_nic * pfvf,struct otx2_cq_queue * cq)21 static int otx2_nix_cq_op_status(struct otx2_nic *pfvf,
22 				 struct otx2_cq_queue *cq)
23 {
24 	u64 incr = (u64)(cq->cq_idx) << 32;
25 	u64 status;
26 
27 	status = otx2_atomic64_fetch_add(incr, pfvf->cq_op_addr);
28 
29 	if (unlikely(status & BIT_ULL(CQ_OP_STAT_OP_ERR) ||
30 		     status & BIT_ULL(CQ_OP_STAT_CQ_ERR))) {
31 		dev_err(pfvf->dev, "CQ stopped due to error");
32 		return -EINVAL;
33 	}
34 
35 	cq->cq_tail = status & 0xFFFFF;
36 	cq->cq_head = (status >> 20) & 0xFFFFF;
37 	if (cq->cq_tail < cq->cq_head)
38 		cq->pend_cqe = (cq->cqe_cnt - cq->cq_head) +
39 				cq->cq_tail;
40 	else
41 		cq->pend_cqe = cq->cq_tail - cq->cq_head;
42 
43 	return 0;
44 }
45 
otx2_get_next_cqe(struct otx2_cq_queue * cq)46 static struct nix_cqe_hdr_s *otx2_get_next_cqe(struct otx2_cq_queue *cq)
47 {
48 	struct nix_cqe_hdr_s *cqe_hdr;
49 
50 	cqe_hdr = (struct nix_cqe_hdr_s *)CQE_ADDR(cq, cq->cq_head);
51 	if (cqe_hdr->cqe_type == NIX_XQE_TYPE_INVALID)
52 		return NULL;
53 
54 	cq->cq_head++;
55 	cq->cq_head &= (cq->cqe_cnt - 1);
56 
57 	return cqe_hdr;
58 }
59 
frag_num(unsigned int i)60 static unsigned int frag_num(unsigned int i)
61 {
62 #ifdef __BIG_ENDIAN
63 	return (i & ~3) + 3 - (i & 3);
64 #else
65 	return i;
66 #endif
67 }
68 
otx2_dma_map_skb_frag(struct otx2_nic * pfvf,struct sk_buff * skb,int seg,int * len)69 static dma_addr_t otx2_dma_map_skb_frag(struct otx2_nic *pfvf,
70 					struct sk_buff *skb, int seg, int *len)
71 {
72 	const skb_frag_t *frag;
73 	struct page *page;
74 	int offset;
75 
76 	/* First segment is always skb->data */
77 	if (!seg) {
78 		page = virt_to_page(skb->data);
79 		offset = offset_in_page(skb->data);
80 		*len = skb_headlen(skb);
81 	} else {
82 		frag = &skb_shinfo(skb)->frags[seg - 1];
83 		page = skb_frag_page(frag);
84 		offset = skb_frag_off(frag);
85 		*len = skb_frag_size(frag);
86 	}
87 	return otx2_dma_map_page(pfvf, page, offset, *len, DMA_TO_DEVICE);
88 }
89 
otx2_dma_unmap_skb_frags(struct otx2_nic * pfvf,struct sg_list * sg)90 static void otx2_dma_unmap_skb_frags(struct otx2_nic *pfvf, struct sg_list *sg)
91 {
92 	int seg;
93 
94 	for (seg = 0; seg < sg->num_segs; seg++) {
95 		otx2_dma_unmap_page(pfvf, sg->dma_addr[seg],
96 				    sg->size[seg], DMA_TO_DEVICE);
97 	}
98 	sg->num_segs = 0;
99 }
100 
otx2_snd_pkt_handler(struct otx2_nic * pfvf,struct otx2_cq_queue * cq,struct otx2_snd_queue * sq,struct nix_cqe_tx_s * cqe,int budget,int * tx_pkts,int * tx_bytes)101 static void otx2_snd_pkt_handler(struct otx2_nic *pfvf,
102 				 struct otx2_cq_queue *cq,
103 				 struct otx2_snd_queue *sq,
104 				 struct nix_cqe_tx_s *cqe,
105 				 int budget, int *tx_pkts, int *tx_bytes)
106 {
107 	struct nix_send_comp_s *snd_comp = &cqe->comp;
108 	struct skb_shared_hwtstamps ts;
109 	struct sk_buff *skb = NULL;
110 	u64 timestamp, tsns;
111 	struct sg_list *sg;
112 	int err;
113 
114 	if (unlikely(snd_comp->status) && netif_msg_tx_err(pfvf))
115 		net_err_ratelimited("%s: TX%d: Error in send CQ status:%x\n",
116 				    pfvf->netdev->name, cq->cint_idx,
117 				    snd_comp->status);
118 
119 	sg = &sq->sg[snd_comp->sqe_id];
120 	skb = (struct sk_buff *)sg->skb;
121 	if (unlikely(!skb))
122 		return;
123 
124 	if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) {
125 		timestamp = ((u64 *)sq->timestamps->base)[snd_comp->sqe_id];
126 		if (timestamp != 1) {
127 			err = otx2_ptp_tstamp2time(pfvf, timestamp, &tsns);
128 			if (!err) {
129 				memset(&ts, 0, sizeof(ts));
130 				ts.hwtstamp = ns_to_ktime(tsns);
131 				skb_tstamp_tx(skb, &ts);
132 			}
133 		}
134 	}
135 
136 	*tx_bytes += skb->len;
137 	(*tx_pkts)++;
138 	otx2_dma_unmap_skb_frags(pfvf, sg);
139 	napi_consume_skb(skb, budget);
140 	sg->skb = (u64)NULL;
141 }
142 
otx2_set_rxtstamp(struct otx2_nic * pfvf,struct sk_buff * skb,void * data)143 static void otx2_set_rxtstamp(struct otx2_nic *pfvf,
144 			      struct sk_buff *skb, void *data)
145 {
146 	u64 tsns;
147 	int err;
148 
149 	if (!(pfvf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED))
150 		return;
151 
152 	/* The first 8 bytes is the timestamp */
153 	err = otx2_ptp_tstamp2time(pfvf, be64_to_cpu(*(__be64 *)data), &tsns);
154 	if (err)
155 		return;
156 
157 	skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(tsns);
158 }
159 
otx2_skb_add_frag(struct otx2_nic * pfvf,struct sk_buff * skb,u64 iova,int len,struct nix_rx_parse_s * parse)160 static void otx2_skb_add_frag(struct otx2_nic *pfvf, struct sk_buff *skb,
161 			      u64 iova, int len, struct nix_rx_parse_s *parse)
162 {
163 	struct page *page;
164 	int off = 0;
165 	void *va;
166 
167 	va = phys_to_virt(otx2_iova_to_phys(pfvf->iommu_domain, iova));
168 
169 	if (likely(!skb_shinfo(skb)->nr_frags)) {
170 		/* Check if data starts at some nonzero offset
171 		 * from the start of the buffer.  For now the
172 		 * only possible offset is 8 bytes in the case
173 		 * where packet is prepended by a timestamp.
174 		 */
175 		if (parse->laptr) {
176 			otx2_set_rxtstamp(pfvf, skb, va);
177 			off = OTX2_HW_TIMESTAMP_LEN;
178 		}
179 	}
180 
181 	page = virt_to_page(va);
182 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
183 			va - page_address(page) + off, len - off, pfvf->rbsize);
184 
185 	otx2_dma_unmap_page(pfvf, iova - OTX2_HEAD_ROOM,
186 			    pfvf->rbsize, DMA_FROM_DEVICE);
187 }
188 
otx2_set_rxhash(struct otx2_nic * pfvf,struct nix_cqe_rx_s * cqe,struct sk_buff * skb)189 static void otx2_set_rxhash(struct otx2_nic *pfvf,
190 			    struct nix_cqe_rx_s *cqe, struct sk_buff *skb)
191 {
192 	enum pkt_hash_types hash_type = PKT_HASH_TYPE_NONE;
193 	struct otx2_rss_info *rss;
194 	u32 hash = 0;
195 
196 	if (!(pfvf->netdev->features & NETIF_F_RXHASH))
197 		return;
198 
199 	rss = &pfvf->hw.rss_info;
200 	if (rss->flowkey_cfg) {
201 		if (rss->flowkey_cfg &
202 		    ~(NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6))
203 			hash_type = PKT_HASH_TYPE_L4;
204 		else
205 			hash_type = PKT_HASH_TYPE_L3;
206 		hash = cqe->hdr.flow_tag;
207 	}
208 	skb_set_hash(skb, hash, hash_type);
209 }
210 
otx2_free_rcv_seg(struct otx2_nic * pfvf,struct nix_cqe_rx_s * cqe,int qidx)211 static void otx2_free_rcv_seg(struct otx2_nic *pfvf, struct nix_cqe_rx_s *cqe,
212 			      int qidx)
213 {
214 	struct nix_rx_sg_s *sg = &cqe->sg;
215 	void *end, *start;
216 	u64 *seg_addr;
217 	int seg;
218 
219 	start = (void *)sg;
220 	end = start + ((cqe->parse.desc_sizem1 + 1) * 16);
221 	while (start < end) {
222 		sg = (struct nix_rx_sg_s *)start;
223 		seg_addr = &sg->seg_addr;
224 		for (seg = 0; seg < sg->segs; seg++, seg_addr++)
225 			pfvf->hw_ops->aura_freeptr(pfvf, qidx,
226 						   *seg_addr & ~0x07ULL);
227 		start += sizeof(*sg);
228 	}
229 }
230 
otx2_check_rcv_errors(struct otx2_nic * pfvf,struct nix_cqe_rx_s * cqe,int qidx)231 static bool otx2_check_rcv_errors(struct otx2_nic *pfvf,
232 				  struct nix_cqe_rx_s *cqe, int qidx)
233 {
234 	struct otx2_drv_stats *stats = &pfvf->hw.drv_stats;
235 	struct nix_rx_parse_s *parse = &cqe->parse;
236 
237 	if (netif_msg_rx_err(pfvf))
238 		netdev_err(pfvf->netdev,
239 			   "RQ%d: Error pkt with errlev:0x%x errcode:0x%x\n",
240 			   qidx, parse->errlev, parse->errcode);
241 
242 	if (parse->errlev == NPC_ERRLVL_RE) {
243 		switch (parse->errcode) {
244 		case ERRCODE_FCS:
245 		case ERRCODE_FCS_RCV:
246 			atomic_inc(&stats->rx_fcs_errs);
247 			break;
248 		case ERRCODE_UNDERSIZE:
249 			atomic_inc(&stats->rx_undersize_errs);
250 			break;
251 		case ERRCODE_OVERSIZE:
252 			atomic_inc(&stats->rx_oversize_errs);
253 			break;
254 		case ERRCODE_OL2_LEN_MISMATCH:
255 			atomic_inc(&stats->rx_len_errs);
256 			break;
257 		default:
258 			atomic_inc(&stats->rx_other_errs);
259 			break;
260 		}
261 	} else if (parse->errlev == NPC_ERRLVL_NIX) {
262 		switch (parse->errcode) {
263 		case ERRCODE_OL3_LEN:
264 		case ERRCODE_OL4_LEN:
265 		case ERRCODE_IL3_LEN:
266 		case ERRCODE_IL4_LEN:
267 			atomic_inc(&stats->rx_len_errs);
268 			break;
269 		case ERRCODE_OL4_CSUM:
270 		case ERRCODE_IL4_CSUM:
271 			atomic_inc(&stats->rx_csum_errs);
272 			break;
273 		default:
274 			atomic_inc(&stats->rx_other_errs);
275 			break;
276 		}
277 	} else {
278 		atomic_inc(&stats->rx_other_errs);
279 		/* For now ignore all the NPC parser errors and
280 		 * pass the packets to stack.
281 		 */
282 		return false;
283 	}
284 
285 	/* If RXALL is enabled pass on packets to stack. */
286 	if (pfvf->netdev->features & NETIF_F_RXALL)
287 		return false;
288 
289 	/* Free buffer back to pool */
290 	if (cqe->sg.segs)
291 		otx2_free_rcv_seg(pfvf, cqe, qidx);
292 	return true;
293 }
294 
otx2_rcv_pkt_handler(struct otx2_nic * pfvf,struct napi_struct * napi,struct otx2_cq_queue * cq,struct nix_cqe_rx_s * cqe)295 static void otx2_rcv_pkt_handler(struct otx2_nic *pfvf,
296 				 struct napi_struct *napi,
297 				 struct otx2_cq_queue *cq,
298 				 struct nix_cqe_rx_s *cqe)
299 {
300 	struct nix_rx_parse_s *parse = &cqe->parse;
301 	struct nix_rx_sg_s *sg = &cqe->sg;
302 	struct sk_buff *skb = NULL;
303 	void *end, *start;
304 	u64 *seg_addr;
305 	u16 *seg_size;
306 	int seg;
307 
308 	if (unlikely(parse->errlev || parse->errcode)) {
309 		if (otx2_check_rcv_errors(pfvf, cqe, cq->cq_idx))
310 			return;
311 	}
312 
313 	skb = napi_get_frags(napi);
314 	if (unlikely(!skb))
315 		return;
316 
317 	start = (void *)sg;
318 	end = start + ((cqe->parse.desc_sizem1 + 1) * 16);
319 	while (start < end) {
320 		sg = (struct nix_rx_sg_s *)start;
321 		seg_addr = &sg->seg_addr;
322 		seg_size = (void *)sg;
323 		for (seg = 0; seg < sg->segs; seg++, seg_addr++) {
324 			otx2_skb_add_frag(pfvf, skb, *seg_addr, seg_size[seg],
325 					  parse);
326 			cq->pool_ptrs++;
327 		}
328 		start += sizeof(*sg);
329 	}
330 	otx2_set_rxhash(pfvf, cqe, skb);
331 
332 	skb_record_rx_queue(skb, cq->cq_idx);
333 	if (pfvf->netdev->features & NETIF_F_RXCSUM)
334 		skb->ip_summed = CHECKSUM_UNNECESSARY;
335 
336 	napi_gro_frags(napi);
337 }
338 
otx2_rx_napi_handler(struct otx2_nic * pfvf,struct napi_struct * napi,struct otx2_cq_queue * cq,int budget)339 static int otx2_rx_napi_handler(struct otx2_nic *pfvf,
340 				struct napi_struct *napi,
341 				struct otx2_cq_queue *cq, int budget)
342 {
343 	struct nix_cqe_rx_s *cqe;
344 	int processed_cqe = 0;
345 
346 	if (cq->pend_cqe >= budget)
347 		goto process_cqe;
348 
349 	if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe)
350 		return 0;
351 
352 process_cqe:
353 	while (likely(processed_cqe < budget) && cq->pend_cqe) {
354 		cqe = (struct nix_cqe_rx_s *)CQE_ADDR(cq, cq->cq_head);
355 		if (cqe->hdr.cqe_type == NIX_XQE_TYPE_INVALID ||
356 		    !cqe->sg.seg_addr) {
357 			if (!processed_cqe)
358 				return 0;
359 			break;
360 		}
361 		cq->cq_head++;
362 		cq->cq_head &= (cq->cqe_cnt - 1);
363 
364 		otx2_rcv_pkt_handler(pfvf, napi, cq, cqe);
365 
366 		cqe->hdr.cqe_type = NIX_XQE_TYPE_INVALID;
367 		cqe->sg.seg_addr = 0x00;
368 		processed_cqe++;
369 		cq->pend_cqe--;
370 	}
371 
372 	/* Free CQEs to HW */
373 	otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR,
374 		     ((u64)cq->cq_idx << 32) | processed_cqe);
375 
376 	if (unlikely(!cq->pool_ptrs))
377 		return 0;
378 	/* Refill pool with new buffers */
379 	pfvf->hw_ops->refill_pool_ptrs(pfvf, cq);
380 
381 	return processed_cqe;
382 }
383 
otx2_refill_pool_ptrs(void * dev,struct otx2_cq_queue * cq)384 void otx2_refill_pool_ptrs(void *dev, struct otx2_cq_queue *cq)
385 {
386 	struct otx2_nic *pfvf = dev;
387 	dma_addr_t bufptr;
388 
389 	while (cq->pool_ptrs) {
390 		if (otx2_alloc_buffer(pfvf, cq, &bufptr))
391 			break;
392 		otx2_aura_freeptr(pfvf, cq->cq_idx, bufptr + OTX2_HEAD_ROOM);
393 		cq->pool_ptrs--;
394 	}
395 }
396 
otx2_tx_napi_handler(struct otx2_nic * pfvf,struct otx2_cq_queue * cq,int budget)397 static int otx2_tx_napi_handler(struct otx2_nic *pfvf,
398 				struct otx2_cq_queue *cq, int budget)
399 {
400 	int tx_pkts = 0, tx_bytes = 0;
401 	struct nix_cqe_tx_s *cqe;
402 	int processed_cqe = 0;
403 
404 	if (cq->pend_cqe >= budget)
405 		goto process_cqe;
406 
407 	if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe)
408 		return 0;
409 
410 process_cqe:
411 	while (likely(processed_cqe < budget) && cq->pend_cqe) {
412 		cqe = (struct nix_cqe_tx_s *)otx2_get_next_cqe(cq);
413 		if (unlikely(!cqe)) {
414 			if (!processed_cqe)
415 				return 0;
416 			break;
417 		}
418 		otx2_snd_pkt_handler(pfvf, cq, &pfvf->qset.sq[cq->cint_idx],
419 				     cqe, budget, &tx_pkts, &tx_bytes);
420 
421 		cqe->hdr.cqe_type = NIX_XQE_TYPE_INVALID;
422 		processed_cqe++;
423 		cq->pend_cqe--;
424 	}
425 
426 	/* Free CQEs to HW */
427 	otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR,
428 		     ((u64)cq->cq_idx << 32) | processed_cqe);
429 
430 	if (likely(tx_pkts)) {
431 		struct netdev_queue *txq;
432 
433 		txq = netdev_get_tx_queue(pfvf->netdev, cq->cint_idx);
434 		netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
435 		/* Check if queue was stopped earlier due to ring full */
436 		smp_mb();
437 		if (netif_tx_queue_stopped(txq) &&
438 		    netif_carrier_ok(pfvf->netdev))
439 			netif_tx_wake_queue(txq);
440 	}
441 	return 0;
442 }
443 
otx2_napi_handler(struct napi_struct * napi,int budget)444 int otx2_napi_handler(struct napi_struct *napi, int budget)
445 {
446 	struct otx2_cq_poll *cq_poll;
447 	int workdone = 0, cq_idx, i;
448 	struct otx2_cq_queue *cq;
449 	struct otx2_qset *qset;
450 	struct otx2_nic *pfvf;
451 
452 	cq_poll = container_of(napi, struct otx2_cq_poll, napi);
453 	pfvf = (struct otx2_nic *)cq_poll->dev;
454 	qset = &pfvf->qset;
455 
456 	for (i = CQS_PER_CINT - 1; i >= 0; i--) {
457 		cq_idx = cq_poll->cq_ids[i];
458 		if (unlikely(cq_idx == CINT_INVALID_CQ))
459 			continue;
460 		cq = &qset->cq[cq_idx];
461 		if (cq->cq_type == CQ_RX) {
462 			/* If the RQ refill WQ task is running, skip napi
463 			 * scheduler for this queue.
464 			 */
465 			if (cq->refill_task_sched)
466 				continue;
467 			workdone += otx2_rx_napi_handler(pfvf, napi,
468 							 cq, budget);
469 		} else {
470 			workdone += otx2_tx_napi_handler(pfvf, cq, budget);
471 		}
472 	}
473 
474 	/* Clear the IRQ */
475 	otx2_write64(pfvf, NIX_LF_CINTX_INT(cq_poll->cint_idx), BIT_ULL(0));
476 
477 	if (workdone < budget && napi_complete_done(napi, workdone)) {
478 		/* If interface is going down, don't re-enable IRQ */
479 		if (pfvf->flags & OTX2_FLAG_INTF_DOWN)
480 			return workdone;
481 
482 		/* Re-enable interrupts */
483 		otx2_write64(pfvf, NIX_LF_CINTX_ENA_W1S(cq_poll->cint_idx),
484 			     BIT_ULL(0));
485 	}
486 	return workdone;
487 }
488 
otx2_sqe_flush(void * dev,struct otx2_snd_queue * sq,int size,int qidx)489 void otx2_sqe_flush(void *dev, struct otx2_snd_queue *sq,
490 		    int size, int qidx)
491 {
492 	u64 status;
493 
494 	/* Packet data stores should finish before SQE is flushed to HW */
495 	dma_wmb();
496 
497 	do {
498 		memcpy(sq->lmt_addr, sq->sqe_base, size);
499 		status = otx2_lmt_flush(sq->io_addr);
500 	} while (status == 0);
501 
502 	sq->head++;
503 	sq->head &= (sq->sqe_cnt - 1);
504 }
505 
506 #define MAX_SEGS_PER_SG	3
507 /* Add SQE scatter/gather subdescriptor structure */
otx2_sqe_add_sg(struct otx2_nic * pfvf,struct otx2_snd_queue * sq,struct sk_buff * skb,int num_segs,int * offset)508 static bool otx2_sqe_add_sg(struct otx2_nic *pfvf, struct otx2_snd_queue *sq,
509 			    struct sk_buff *skb, int num_segs, int *offset)
510 {
511 	struct nix_sqe_sg_s *sg = NULL;
512 	u64 dma_addr, *iova = NULL;
513 	u16 *sg_lens = NULL;
514 	int seg, len;
515 
516 	sq->sg[sq->head].num_segs = 0;
517 
518 	for (seg = 0; seg < num_segs; seg++) {
519 		if ((seg % MAX_SEGS_PER_SG) == 0) {
520 			sg = (struct nix_sqe_sg_s *)(sq->sqe_base + *offset);
521 			sg->ld_type = NIX_SEND_LDTYPE_LDD;
522 			sg->subdc = NIX_SUBDC_SG;
523 			sg->segs = 0;
524 			sg_lens = (void *)sg;
525 			iova = (void *)sg + sizeof(*sg);
526 			/* Next subdc always starts at a 16byte boundary.
527 			 * So if sg->segs is whether 2 or 3, offset += 16bytes.
528 			 */
529 			if ((num_segs - seg) >= (MAX_SEGS_PER_SG - 1))
530 				*offset += sizeof(*sg) + (3 * sizeof(u64));
531 			else
532 				*offset += sizeof(*sg) + sizeof(u64);
533 		}
534 		dma_addr = otx2_dma_map_skb_frag(pfvf, skb, seg, &len);
535 		if (dma_mapping_error(pfvf->dev, dma_addr))
536 			return false;
537 
538 		sg_lens[frag_num(seg % MAX_SEGS_PER_SG)] = len;
539 		sg->segs++;
540 		*iova++ = dma_addr;
541 
542 		/* Save DMA mapping info for later unmapping */
543 		sq->sg[sq->head].dma_addr[seg] = dma_addr;
544 		sq->sg[sq->head].size[seg] = len;
545 		sq->sg[sq->head].num_segs++;
546 	}
547 
548 	sq->sg[sq->head].skb = (u64)skb;
549 	return true;
550 }
551 
552 /* Add SQE extended header subdescriptor */
otx2_sqe_add_ext(struct otx2_nic * pfvf,struct otx2_snd_queue * sq,struct sk_buff * skb,int * offset)553 static void otx2_sqe_add_ext(struct otx2_nic *pfvf, struct otx2_snd_queue *sq,
554 			     struct sk_buff *skb, int *offset)
555 {
556 	struct nix_sqe_ext_s *ext;
557 
558 	ext = (struct nix_sqe_ext_s *)(sq->sqe_base + *offset);
559 	ext->subdc = NIX_SUBDC_EXT;
560 	if (skb_shinfo(skb)->gso_size) {
561 		ext->lso = 1;
562 		ext->lso_sb = skb_transport_offset(skb) + tcp_hdrlen(skb);
563 		ext->lso_mps = skb_shinfo(skb)->gso_size;
564 
565 		/* Only TSOv4 and TSOv6 GSO offloads are supported */
566 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
567 			ext->lso_format = pfvf->hw.lso_tsov4_idx;
568 
569 			/* HW adds payload size to 'ip_hdr->tot_len' while
570 			 * sending TSO segment, hence set payload length
571 			 * in IP header of the packet to just header length.
572 			 */
573 			ip_hdr(skb)->tot_len =
574 				htons(ext->lso_sb - skb_network_offset(skb));
575 		} else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
576 			ext->lso_format = pfvf->hw.lso_tsov6_idx;
577 			ipv6_hdr(skb)->payload_len = htons(tcp_hdrlen(skb));
578 		} else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
579 			__be16 l3_proto = vlan_get_protocol(skb);
580 			struct udphdr *udph = udp_hdr(skb);
581 			u16 iplen;
582 
583 			ext->lso_sb = skb_transport_offset(skb) +
584 					sizeof(struct udphdr);
585 
586 			/* HW adds payload size to length fields in IP and
587 			 * UDP headers while segmentation, hence adjust the
588 			 * lengths to just header sizes.
589 			 */
590 			iplen = htons(ext->lso_sb - skb_network_offset(skb));
591 			if (l3_proto == htons(ETH_P_IP)) {
592 				ip_hdr(skb)->tot_len = iplen;
593 				ext->lso_format = pfvf->hw.lso_udpv4_idx;
594 			} else {
595 				ipv6_hdr(skb)->payload_len = iplen;
596 				ext->lso_format = pfvf->hw.lso_udpv6_idx;
597 			}
598 
599 			udph->len = htons(sizeof(struct udphdr));
600 		}
601 	} else if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
602 		ext->tstmp = 1;
603 	}
604 
605 #define OTX2_VLAN_PTR_OFFSET     (ETH_HLEN - ETH_TLEN)
606 	if (skb_vlan_tag_present(skb)) {
607 		if (skb->vlan_proto == htons(ETH_P_8021Q)) {
608 			ext->vlan1_ins_ena = 1;
609 			ext->vlan1_ins_ptr = OTX2_VLAN_PTR_OFFSET;
610 			ext->vlan1_ins_tci = skb_vlan_tag_get(skb);
611 		} else if (skb->vlan_proto == htons(ETH_P_8021AD)) {
612 			ext->vlan0_ins_ena = 1;
613 			ext->vlan0_ins_ptr = OTX2_VLAN_PTR_OFFSET;
614 			ext->vlan0_ins_tci = skb_vlan_tag_get(skb);
615 		}
616 	}
617 
618 	*offset += sizeof(*ext);
619 }
620 
otx2_sqe_add_mem(struct otx2_snd_queue * sq,int * offset,int alg,u64 iova)621 static void otx2_sqe_add_mem(struct otx2_snd_queue *sq, int *offset,
622 			     int alg, u64 iova)
623 {
624 	struct nix_sqe_mem_s *mem;
625 
626 	mem = (struct nix_sqe_mem_s *)(sq->sqe_base + *offset);
627 	mem->subdc = NIX_SUBDC_MEM;
628 	mem->alg = alg;
629 	mem->wmem = 1; /* wait for the memory operation */
630 	mem->addr = iova;
631 
632 	*offset += sizeof(*mem);
633 }
634 
635 /* Add SQE header subdescriptor structure */
otx2_sqe_add_hdr(struct otx2_nic * pfvf,struct otx2_snd_queue * sq,struct nix_sqe_hdr_s * sqe_hdr,struct sk_buff * skb,u16 qidx)636 static void otx2_sqe_add_hdr(struct otx2_nic *pfvf, struct otx2_snd_queue *sq,
637 			     struct nix_sqe_hdr_s *sqe_hdr,
638 			     struct sk_buff *skb, u16 qidx)
639 {
640 	int proto = 0;
641 
642 	/* Check if SQE was framed before, if yes then no need to
643 	 * set these constants again and again.
644 	 */
645 	if (!sqe_hdr->total) {
646 		/* Don't free Tx buffers to Aura */
647 		sqe_hdr->df = 1;
648 		sqe_hdr->aura = sq->aura_id;
649 		/* Post a CQE Tx after pkt transmission */
650 		sqe_hdr->pnc = 1;
651 		sqe_hdr->sq = qidx;
652 	}
653 	sqe_hdr->total = skb->len;
654 	/* Set SQE identifier which will be used later for freeing SKB */
655 	sqe_hdr->sqe_id = sq->head;
656 
657 	/* Offload TCP/UDP checksum to HW */
658 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
659 		sqe_hdr->ol3ptr = skb_network_offset(skb);
660 		sqe_hdr->ol4ptr = skb_transport_offset(skb);
661 		/* get vlan protocol Ethertype */
662 		if (eth_type_vlan(skb->protocol))
663 			skb->protocol = vlan_get_protocol(skb);
664 
665 		if (skb->protocol == htons(ETH_P_IP)) {
666 			proto = ip_hdr(skb)->protocol;
667 			/* In case of TSO, HW needs this to be explicitly set.
668 			 * So set this always, instead of adding a check.
669 			 */
670 			sqe_hdr->ol3type = NIX_SENDL3TYPE_IP4_CKSUM;
671 		} else if (skb->protocol == htons(ETH_P_IPV6)) {
672 			proto = ipv6_hdr(skb)->nexthdr;
673 			sqe_hdr->ol3type = NIX_SENDL3TYPE_IP6;
674 		}
675 
676 		if (proto == IPPROTO_TCP)
677 			sqe_hdr->ol4type = NIX_SENDL4TYPE_TCP_CKSUM;
678 		else if (proto == IPPROTO_UDP)
679 			sqe_hdr->ol4type = NIX_SENDL4TYPE_UDP_CKSUM;
680 	}
681 }
682 
otx2_dma_map_tso_skb(struct otx2_nic * pfvf,struct otx2_snd_queue * sq,struct sk_buff * skb,int sqe,int hdr_len)683 static int otx2_dma_map_tso_skb(struct otx2_nic *pfvf,
684 				struct otx2_snd_queue *sq,
685 				struct sk_buff *skb, int sqe, int hdr_len)
686 {
687 	int num_segs = skb_shinfo(skb)->nr_frags + 1;
688 	struct sg_list *sg = &sq->sg[sqe];
689 	u64 dma_addr;
690 	int seg, len;
691 
692 	sg->num_segs = 0;
693 
694 	/* Get payload length at skb->data */
695 	len = skb_headlen(skb) - hdr_len;
696 
697 	for (seg = 0; seg < num_segs; seg++) {
698 		/* Skip skb->data, if there is no payload */
699 		if (!seg && !len)
700 			continue;
701 		dma_addr = otx2_dma_map_skb_frag(pfvf, skb, seg, &len);
702 		if (dma_mapping_error(pfvf->dev, dma_addr))
703 			goto unmap;
704 
705 		/* Save DMA mapping info for later unmapping */
706 		sg->dma_addr[sg->num_segs] = dma_addr;
707 		sg->size[sg->num_segs] = len;
708 		sg->num_segs++;
709 	}
710 	return 0;
711 unmap:
712 	otx2_dma_unmap_skb_frags(pfvf, sg);
713 	return -EINVAL;
714 }
715 
otx2_tso_frag_dma_addr(struct otx2_snd_queue * sq,struct sk_buff * skb,int seg,u64 seg_addr,int hdr_len,int sqe)716 static u64 otx2_tso_frag_dma_addr(struct otx2_snd_queue *sq,
717 				  struct sk_buff *skb, int seg,
718 				  u64 seg_addr, int hdr_len, int sqe)
719 {
720 	struct sg_list *sg = &sq->sg[sqe];
721 	const skb_frag_t *frag;
722 	int offset;
723 
724 	if (seg < 0)
725 		return sg->dma_addr[0] + (seg_addr - (u64)skb->data);
726 
727 	frag = &skb_shinfo(skb)->frags[seg];
728 	offset = seg_addr - (u64)skb_frag_address(frag);
729 	if (skb_headlen(skb) - hdr_len)
730 		seg++;
731 	return sg->dma_addr[seg] + offset;
732 }
733 
otx2_sqe_tso_add_sg(struct otx2_snd_queue * sq,struct sg_list * list,int * offset)734 static void otx2_sqe_tso_add_sg(struct otx2_snd_queue *sq,
735 				struct sg_list *list, int *offset)
736 {
737 	struct nix_sqe_sg_s *sg = NULL;
738 	u16 *sg_lens = NULL;
739 	u64 *iova = NULL;
740 	int seg;
741 
742 	/* Add SG descriptors with buffer addresses */
743 	for (seg = 0; seg < list->num_segs; seg++) {
744 		if ((seg % MAX_SEGS_PER_SG) == 0) {
745 			sg = (struct nix_sqe_sg_s *)(sq->sqe_base + *offset);
746 			sg->ld_type = NIX_SEND_LDTYPE_LDD;
747 			sg->subdc = NIX_SUBDC_SG;
748 			sg->segs = 0;
749 			sg_lens = (void *)sg;
750 			iova = (void *)sg + sizeof(*sg);
751 			/* Next subdc always starts at a 16byte boundary.
752 			 * So if sg->segs is whether 2 or 3, offset += 16bytes.
753 			 */
754 			if ((list->num_segs - seg) >= (MAX_SEGS_PER_SG - 1))
755 				*offset += sizeof(*sg) + (3 * sizeof(u64));
756 			else
757 				*offset += sizeof(*sg) + sizeof(u64);
758 		}
759 		sg_lens[frag_num(seg % MAX_SEGS_PER_SG)] = list->size[seg];
760 		*iova++ = list->dma_addr[seg];
761 		sg->segs++;
762 	}
763 }
764 
otx2_sq_append_tso(struct otx2_nic * pfvf,struct otx2_snd_queue * sq,struct sk_buff * skb,u16 qidx)765 static void otx2_sq_append_tso(struct otx2_nic *pfvf, struct otx2_snd_queue *sq,
766 			       struct sk_buff *skb, u16 qidx)
767 {
768 	struct netdev_queue *txq = netdev_get_tx_queue(pfvf->netdev, qidx);
769 	int hdr_len, tcp_data, seg_len, pkt_len, offset;
770 	struct nix_sqe_hdr_s *sqe_hdr;
771 	int first_sqe = sq->head;
772 	struct sg_list list;
773 	struct tso_t tso;
774 
775 	hdr_len = tso_start(skb, &tso);
776 
777 	/* Map SKB's fragments to DMA.
778 	 * It's done here to avoid mapping for every TSO segment's packet.
779 	 */
780 	if (otx2_dma_map_tso_skb(pfvf, sq, skb, first_sqe, hdr_len)) {
781 		dev_kfree_skb_any(skb);
782 		return;
783 	}
784 
785 	netdev_tx_sent_queue(txq, skb->len);
786 
787 	tcp_data = skb->len - hdr_len;
788 	while (tcp_data > 0) {
789 		char *hdr;
790 
791 		seg_len = min_t(int, skb_shinfo(skb)->gso_size, tcp_data);
792 		tcp_data -= seg_len;
793 
794 		/* Set SQE's SEND_HDR */
795 		memset(sq->sqe_base, 0, sq->sqe_size);
796 		sqe_hdr = (struct nix_sqe_hdr_s *)(sq->sqe_base);
797 		otx2_sqe_add_hdr(pfvf, sq, sqe_hdr, skb, qidx);
798 		offset = sizeof(*sqe_hdr);
799 
800 		/* Add TSO segment's pkt header */
801 		hdr = sq->tso_hdrs->base + (sq->head * TSO_HEADER_SIZE);
802 		tso_build_hdr(skb, hdr, &tso, seg_len, tcp_data == 0);
803 		list.dma_addr[0] =
804 			sq->tso_hdrs->iova + (sq->head * TSO_HEADER_SIZE);
805 		list.size[0] = hdr_len;
806 		list.num_segs = 1;
807 
808 		/* Add TSO segment's payload data fragments */
809 		pkt_len = hdr_len;
810 		while (seg_len > 0) {
811 			int size;
812 
813 			size = min_t(int, tso.size, seg_len);
814 
815 			list.size[list.num_segs] = size;
816 			list.dma_addr[list.num_segs] =
817 				otx2_tso_frag_dma_addr(sq, skb,
818 						       tso.next_frag_idx - 1,
819 						       (u64)tso.data, hdr_len,
820 						       first_sqe);
821 			list.num_segs++;
822 			pkt_len += size;
823 			seg_len -= size;
824 			tso_build_data(skb, &tso, size);
825 		}
826 		sqe_hdr->total = pkt_len;
827 		otx2_sqe_tso_add_sg(sq, &list, &offset);
828 
829 		/* DMA mappings and skb needs to be freed only after last
830 		 * TSO segment is transmitted out. So set 'PNC' only for
831 		 * last segment. Also point last segment's sqe_id to first
832 		 * segment's SQE index where skb address and DMA mappings
833 		 * are saved.
834 		 */
835 		if (!tcp_data) {
836 			sqe_hdr->pnc = 1;
837 			sqe_hdr->sqe_id = first_sqe;
838 			sq->sg[first_sqe].skb = (u64)skb;
839 		} else {
840 			sqe_hdr->pnc = 0;
841 		}
842 
843 		sqe_hdr->sizem1 = (offset / 16) - 1;
844 
845 		/* Flush SQE to HW */
846 		pfvf->hw_ops->sqe_flush(pfvf, sq, offset, qidx);
847 	}
848 }
849 
is_hw_tso_supported(struct otx2_nic * pfvf,struct sk_buff * skb)850 static bool is_hw_tso_supported(struct otx2_nic *pfvf,
851 				struct sk_buff *skb)
852 {
853 	int payload_len, last_seg_size;
854 
855 	if (test_bit(HW_TSO, &pfvf->hw.cap_flag))
856 		return true;
857 
858 	/* On 96xx A0, HW TSO not supported */
859 	if (!is_96xx_B0(pfvf->pdev))
860 		return false;
861 
862 	/* HW has an issue due to which when the payload of the last LSO
863 	 * segment is shorter than 16 bytes, some header fields may not
864 	 * be correctly modified, hence don't offload such TSO segments.
865 	 */
866 
867 	payload_len = skb->len - (skb_transport_offset(skb) + tcp_hdrlen(skb));
868 	last_seg_size = payload_len % skb_shinfo(skb)->gso_size;
869 	if (last_seg_size && last_seg_size < 16)
870 		return false;
871 
872 	return true;
873 }
874 
otx2_get_sqe_count(struct otx2_nic * pfvf,struct sk_buff * skb)875 static int otx2_get_sqe_count(struct otx2_nic *pfvf, struct sk_buff *skb)
876 {
877 	if (!skb_shinfo(skb)->gso_size)
878 		return 1;
879 
880 	/* HW TSO */
881 	if (is_hw_tso_supported(pfvf, skb))
882 		return 1;
883 
884 	/* SW TSO */
885 	return skb_shinfo(skb)->gso_segs;
886 }
887 
otx2_set_txtstamp(struct otx2_nic * pfvf,struct sk_buff * skb,struct otx2_snd_queue * sq,int * offset)888 static void otx2_set_txtstamp(struct otx2_nic *pfvf, struct sk_buff *skb,
889 			      struct otx2_snd_queue *sq, int *offset)
890 {
891 	u64 iova;
892 
893 	if (!skb_shinfo(skb)->gso_size &&
894 	    skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
895 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
896 		iova = sq->timestamps->iova + (sq->head * sizeof(u64));
897 		otx2_sqe_add_mem(sq, offset, NIX_SENDMEMALG_E_SETTSTMP, iova);
898 	} else {
899 		skb_tx_timestamp(skb);
900 	}
901 }
902 
otx2_sq_append_skb(struct net_device * netdev,struct otx2_snd_queue * sq,struct sk_buff * skb,u16 qidx)903 bool otx2_sq_append_skb(struct net_device *netdev, struct otx2_snd_queue *sq,
904 			struct sk_buff *skb, u16 qidx)
905 {
906 	struct netdev_queue *txq = netdev_get_tx_queue(netdev, qidx);
907 	struct otx2_nic *pfvf = netdev_priv(netdev);
908 	int offset, num_segs, free_sqe;
909 	struct nix_sqe_hdr_s *sqe_hdr;
910 
911 	/* Check if there is room for new SQE.
912 	 * 'Num of SQBs freed to SQ's pool - SQ's Aura count'
913 	 * will give free SQE count.
914 	 */
915 	free_sqe = (sq->num_sqbs - *sq->aura_fc_addr) * sq->sqe_per_sqb;
916 
917 	if (free_sqe < sq->sqe_thresh ||
918 	    free_sqe < otx2_get_sqe_count(pfvf, skb))
919 		return false;
920 
921 	num_segs = skb_shinfo(skb)->nr_frags + 1;
922 
923 	/* If SKB doesn't fit in a single SQE, linearize it.
924 	 * TODO: Consider adding JUMP descriptor instead.
925 	 */
926 	if (unlikely(num_segs > OTX2_MAX_FRAGS_IN_SQE)) {
927 		if (__skb_linearize(skb)) {
928 			dev_kfree_skb_any(skb);
929 			return true;
930 		}
931 		num_segs = skb_shinfo(skb)->nr_frags + 1;
932 	}
933 
934 	if (skb_shinfo(skb)->gso_size && !is_hw_tso_supported(pfvf, skb)) {
935 		/* Insert vlan tag before giving pkt to tso */
936 		if (skb_vlan_tag_present(skb))
937 			skb = __vlan_hwaccel_push_inside(skb);
938 		otx2_sq_append_tso(pfvf, sq, skb, qidx);
939 		return true;
940 	}
941 
942 	/* Set SQE's SEND_HDR.
943 	 * Do not clear the first 64bit as it contains constant info.
944 	 */
945 	memset(sq->sqe_base + 8, 0, sq->sqe_size - 8);
946 	sqe_hdr = (struct nix_sqe_hdr_s *)(sq->sqe_base);
947 	otx2_sqe_add_hdr(pfvf, sq, sqe_hdr, skb, qidx);
948 	offset = sizeof(*sqe_hdr);
949 
950 	/* Add extended header if needed */
951 	otx2_sqe_add_ext(pfvf, sq, skb, &offset);
952 
953 	/* Add SG subdesc with data frags */
954 	if (!otx2_sqe_add_sg(pfvf, sq, skb, num_segs, &offset)) {
955 		otx2_dma_unmap_skb_frags(pfvf, &sq->sg[sq->head]);
956 		return false;
957 	}
958 
959 	otx2_set_txtstamp(pfvf, skb, sq, &offset);
960 
961 	sqe_hdr->sizem1 = (offset / 16) - 1;
962 
963 	netdev_tx_sent_queue(txq, skb->len);
964 
965 	/* Flush SQE to HW */
966 	pfvf->hw_ops->sqe_flush(pfvf, sq, offset, qidx);
967 
968 	return true;
969 }
970 EXPORT_SYMBOL(otx2_sq_append_skb);
971 
otx2_cleanup_rx_cqes(struct otx2_nic * pfvf,struct otx2_cq_queue * cq)972 void otx2_cleanup_rx_cqes(struct otx2_nic *pfvf, struct otx2_cq_queue *cq)
973 {
974 	struct nix_cqe_rx_s *cqe;
975 	int processed_cqe = 0;
976 	u64 iova, pa;
977 
978 	if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe)
979 		return;
980 
981 	while (cq->pend_cqe) {
982 		cqe = (struct nix_cqe_rx_s *)otx2_get_next_cqe(cq);
983 		processed_cqe++;
984 		cq->pend_cqe--;
985 
986 		if (!cqe)
987 			continue;
988 		if (cqe->sg.segs > 1) {
989 			otx2_free_rcv_seg(pfvf, cqe, cq->cq_idx);
990 			continue;
991 		}
992 		iova = cqe->sg.seg_addr - OTX2_HEAD_ROOM;
993 		pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
994 		otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize, DMA_FROM_DEVICE);
995 		put_page(virt_to_page(phys_to_virt(pa)));
996 	}
997 
998 	/* Free CQEs to HW */
999 	otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR,
1000 		     ((u64)cq->cq_idx << 32) | processed_cqe);
1001 }
1002 
otx2_cleanup_tx_cqes(struct otx2_nic * pfvf,struct otx2_cq_queue * cq)1003 void otx2_cleanup_tx_cqes(struct otx2_nic *pfvf, struct otx2_cq_queue *cq)
1004 {
1005 	struct sk_buff *skb = NULL;
1006 	struct otx2_snd_queue *sq;
1007 	struct nix_cqe_tx_s *cqe;
1008 	int processed_cqe = 0;
1009 	struct sg_list *sg;
1010 
1011 	sq = &pfvf->qset.sq[cq->cint_idx];
1012 
1013 	if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe)
1014 		return;
1015 
1016 	while (cq->pend_cqe) {
1017 		cqe = (struct nix_cqe_tx_s *)otx2_get_next_cqe(cq);
1018 		processed_cqe++;
1019 		cq->pend_cqe--;
1020 
1021 		if (!cqe)
1022 			continue;
1023 		sg = &sq->sg[cqe->comp.sqe_id];
1024 		skb = (struct sk_buff *)sg->skb;
1025 		if (skb) {
1026 			otx2_dma_unmap_skb_frags(pfvf, sg);
1027 			dev_kfree_skb_any(skb);
1028 			sg->skb = (u64)NULL;
1029 		}
1030 	}
1031 
1032 	/* Free CQEs to HW */
1033 	otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR,
1034 		     ((u64)cq->cq_idx << 32) | processed_cqe);
1035 }
1036 
otx2_rxtx_enable(struct otx2_nic * pfvf,bool enable)1037 int otx2_rxtx_enable(struct otx2_nic *pfvf, bool enable)
1038 {
1039 	struct msg_req *msg;
1040 	int err;
1041 
1042 	mutex_lock(&pfvf->mbox.lock);
1043 	if (enable)
1044 		msg = otx2_mbox_alloc_msg_nix_lf_start_rx(&pfvf->mbox);
1045 	else
1046 		msg = otx2_mbox_alloc_msg_nix_lf_stop_rx(&pfvf->mbox);
1047 
1048 	if (!msg) {
1049 		mutex_unlock(&pfvf->mbox.lock);
1050 		return -ENOMEM;
1051 	}
1052 
1053 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1054 	mutex_unlock(&pfvf->mbox.lock);
1055 	return err;
1056 }
1057