xref: /dpdk/drivers/net/ark/ark_ethdev_rx.c (revision 8d54b1ec4a8be40975ae6978535bcc1431caad02)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2015-2018 Atomic Rules LLC
3  */
4 
5 #include <unistd.h>
6 
7 #include "ark_ethdev_rx.h"
8 #include "ark_global.h"
9 #include "ark_logs.h"
10 #include "ark_mpu.h"
11 #include "ark_udm.h"
12 
13 #define ARK_RX_META_SIZE 32
14 #define ARK_RX_META_OFFSET (RTE_PKTMBUF_HEADROOM - ARK_RX_META_SIZE)
15 #define ARK_RX_MPU_CHUNK (64U)
16 
17 /* Forward declarations */
18 struct ark_rx_queue;
19 struct ark_rx_meta;
20 
21 static void dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi);
22 static void ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue);
23 static uint32_t eth_ark_rx_jumbo(struct ark_rx_queue *queue,
24 				 struct ark_rx_meta *meta,
25 				 struct rte_mbuf *mbuf0,
26 				 uint32_t cons_index);
27 static inline int eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue);
28 
29 /* ************************************************************************* */
30 struct ark_rx_queue {
31 	/* array of mbufs to populate */
32 	struct rte_mbuf **reserve_q;
33 	/* array of physical addresses of the mbuf data pointer */
34 	/* This point is a virtual address */
35 	rte_iova_t *paddress_q;
36 	struct rte_mempool *mb_pool;
37 
38 	struct ark_udm_t *udm;
39 	struct ark_mpu_t *mpu;
40 
41 	rx_user_meta_hook_fn rx_user_meta_hook;
42 	void *ext_user_data;
43 
44 	uint32_t dataroom;
45 	uint32_t headroom;
46 
47 	uint32_t queue_size;
48 	uint32_t queue_mask;
49 
50 	uint32_t seed_index;		/* step 1 set with empty mbuf */
51 	uint32_t cons_index;		/* step 3 consumed by driver */
52 
53 	/* The queue Id is used to identify the HW Q */
54 	uint16_t phys_qid;
55 
56 	/* The queue Index is used within the dpdk device structures */
57 	uint16_t queue_index;
58 
59 	uint32_t unused;
60 
61 	/* next cache line - fields written by device */
62 	RTE_MARKER cacheline1 __rte_cache_min_aligned;
63 
64 	volatile uint32_t prod_index;	/* step 2 filled by FPGA */
65 } __rte_cache_aligned;
66 
67 /* ************************************************************************* */
68 static int
69 eth_ark_rx_hw_setup(struct rte_eth_dev *dev,
70 		    struct ark_rx_queue *queue,
71 		    uint16_t rx_queue_id __rte_unused, uint16_t rx_queue_idx)
72 {
73 	rte_iova_t queue_base;
74 	rte_iova_t phys_addr_q_base;
75 	rte_iova_t phys_addr_prod_index;
76 
77 	queue_base = rte_malloc_virt2iova(queue);
78 	phys_addr_prod_index = queue_base +
79 		offsetof(struct ark_rx_queue, prod_index);
80 
81 	phys_addr_q_base = rte_malloc_virt2iova(queue->paddress_q);
82 
83 	/* Verify HW */
84 	if (ark_mpu_verify(queue->mpu, sizeof(rte_iova_t))) {
85 		ARK_PMD_LOG(ERR, "Illegal configuration rx queue\n");
86 		return -1;
87 	}
88 
89 	/* Stop and Reset and configure MPU */
90 	ark_mpu_configure(queue->mpu, phys_addr_q_base, queue->queue_size, 0);
91 
92 	ark_udm_write_addr(queue->udm, phys_addr_prod_index);
93 
94 	/* The seed is the producer index for the HW */
95 	ark_mpu_set_producer(queue->mpu, queue->seed_index);
96 	dev->data->rx_queue_state[rx_queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED;
97 
98 	return 0;
99 }
100 
101 static inline void
102 eth_ark_rx_update_cons_index(struct ark_rx_queue *queue, uint32_t cons_index)
103 {
104 	queue->cons_index = cons_index;
105 	if ((cons_index + queue->queue_size - queue->seed_index) >= ARK_RX_MPU_CHUNK) {
106 		eth_ark_rx_seed_mbufs(queue);
107 		ark_mpu_set_producer(queue->mpu, queue->seed_index);
108 	}
109 }
110 
111 /* ************************************************************************* */
112 int
113 eth_ark_dev_rx_queue_setup(struct rte_eth_dev *dev,
114 			   uint16_t queue_idx,
115 			   uint16_t nb_desc,
116 			   unsigned int socket_id,
117 			   const struct rte_eth_rxconf *rx_conf,
118 			   struct rte_mempool *mb_pool)
119 {
120 	static int warning1;		/* = 0 */
121 	struct ark_adapter *ark = dev->data->dev_private;
122 
123 	struct ark_rx_queue *queue;
124 	uint32_t i;
125 	int status;
126 
127 	int qidx = queue_idx;
128 
129 	/* We may already be setup, free memory prior to re-allocation */
130 	if (dev->data->rx_queues[queue_idx] != NULL) {
131 		eth_ark_dev_rx_queue_release(dev->data->rx_queues[queue_idx]);
132 		dev->data->rx_queues[queue_idx] = NULL;
133 	}
134 
135 	if (rx_conf != NULL && warning1 == 0) {
136 		warning1 = 1;
137 		ARK_PMD_LOG(NOTICE,
138 			    "Arkville ignores rte_eth_rxconf argument.\n");
139 	}
140 
141 	if (RTE_PKTMBUF_HEADROOM < ARK_RX_META_SIZE) {
142 		ARK_PMD_LOG(ERR,
143 			    "Error: DPDK Arkville requires head room > %d bytes (%s)\n",
144 			    ARK_RX_META_SIZE, __func__);
145 		return -1;		/* ERROR CODE */
146 	}
147 
148 	if (!rte_is_power_of_2(nb_desc)) {
149 		ARK_PMD_LOG(ERR,
150 			    "DPDK Arkville configuration queue size must be power of two %u (%s)\n",
151 			    nb_desc, __func__);
152 		return -1;		/* ERROR CODE */
153 	}
154 
155 	/* Allocate queue struct */
156 	queue = rte_zmalloc_socket("Ark_rxqueue",
157 				   sizeof(struct ark_rx_queue),
158 				   64,
159 				   socket_id);
160 	if (queue == 0) {
161 		ARK_PMD_LOG(ERR, "Failed to allocate memory in %s\n", __func__);
162 		return -ENOMEM;
163 	}
164 
165 	/* NOTE zmalloc is used, no need to 0 indexes, etc. */
166 	queue->mb_pool = mb_pool;
167 	queue->dataroom = rte_pktmbuf_data_room_size(mb_pool) -
168 		RTE_PKTMBUF_HEADROOM;
169 	queue->headroom = RTE_PKTMBUF_HEADROOM;
170 	queue->phys_qid = qidx;
171 	queue->queue_index = queue_idx;
172 	queue->queue_size = nb_desc;
173 	queue->queue_mask = nb_desc - 1;
174 	queue->rx_user_meta_hook = ark->user_ext.rx_user_meta_hook;
175 	queue->ext_user_data = ark->user_data[dev->data->port_id];
176 
177 	queue->reserve_q =
178 		rte_zmalloc_socket("Ark_rx_queue mbuf",
179 				   nb_desc * sizeof(struct rte_mbuf *),
180 				   512,
181 				   socket_id);
182 	queue->paddress_q =
183 		rte_zmalloc_socket("Ark_rx_queue paddr",
184 				   nb_desc * sizeof(rte_iova_t),
185 				   512,
186 				   socket_id);
187 
188 	if (queue->reserve_q == 0 || queue->paddress_q == 0) {
189 		ARK_PMD_LOG(ERR,
190 			    "Failed to allocate queue memory in %s\n",
191 			    __func__);
192 		rte_free(queue->reserve_q);
193 		rte_free(queue->paddress_q);
194 		rte_free(queue);
195 		return -ENOMEM;
196 	}
197 
198 	dev->data->rx_queues[queue_idx] = queue;
199 	queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET);
200 	queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET);
201 
202 	/* Configure UDM per queue */
203 	ark_udm_configure(queue->udm,
204 			  RTE_PKTMBUF_HEADROOM,
205 			  queue->dataroom);
206 	ark_udm_queue_stats_reset(queue->udm);
207 
208 	/* populate mbuf reserve */
209 	status = eth_ark_rx_seed_mbufs(queue);
210 
211 	if (queue->seed_index != nb_desc) {
212 		ARK_PMD_LOG(ERR, "Failed to allocate %u mbufs for RX queue %d\n",
213 			    nb_desc, qidx);
214 		status = -1;
215 	}
216 	/* MPU Setup */
217 	if (status == 0)
218 		status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx);
219 
220 	if (unlikely(status != 0)) {
221 		struct rte_mbuf **mbuf;
222 
223 		ARK_PMD_LOG(ERR, "Failed to initialize RX queue %d %s\n",
224 			    qidx,
225 			    __func__);
226 		/* Free the mbufs allocated */
227 		for (i = 0, mbuf = queue->reserve_q;
228 		     i < queue->seed_index; ++i, mbuf++) {
229 			rte_pktmbuf_free(*mbuf);
230 		}
231 		rte_free(queue->reserve_q);
232 		rte_free(queue->paddress_q);
233 		rte_free(queue);
234 		return -1;		/* ERROR CODE */
235 	}
236 
237 	return 0;
238 }
239 
240 /* ************************************************************************* */
241 uint16_t
242 eth_ark_recv_pkts(void *rx_queue,
243 		  struct rte_mbuf **rx_pkts,
244 		  uint16_t nb_pkts)
245 {
246 	struct ark_rx_queue *queue;
247 	register uint32_t cons_index, prod_index;
248 	uint16_t nb;
249 	uint16_t i;
250 	struct rte_mbuf *mbuf;
251 	struct rte_mbuf **pmbuf;
252 	struct ark_rx_meta *meta;
253 	rx_user_meta_hook_fn rx_user_meta_hook;
254 
255 	queue = (struct ark_rx_queue *)rx_queue;
256 	if (unlikely(queue == 0))
257 		return 0;
258 	if (unlikely(nb_pkts == 0))
259 		return 0;
260 	prod_index = queue->prod_index;
261 	cons_index = queue->cons_index;
262 	if (prod_index == cons_index)
263 		return 0;
264 	nb = 0;
265 
266 	while (prod_index != cons_index) {
267 		mbuf = queue->reserve_q[cons_index & queue->queue_mask];
268 		/* prefetch mbuf */
269 		rte_mbuf_prefetch_part1(mbuf);
270 		rte_mbuf_prefetch_part2(mbuf);
271 
272 		/* META DATA embedded in headroom */
273 		meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
274 
275 		mbuf->pkt_len = meta->pkt_len;
276 		mbuf->data_len = meta->pkt_len;
277 
278 		if (ARK_DEBUG_CORE) {	/* debug sanity checks */
279 
280 			if ((meta->pkt_len > (1024 * 16)) ||
281 			    (meta->pkt_len == 0)) {
282 				ARK_PMD_LOG(DEBUG, "RX: Bad Meta Q: %u"
283 					   " cons: %" PRIU32
284 					   " prod: %" PRIU32
285 					   " seed_index %" PRIU32
286 					   "\n",
287 					   queue->phys_qid,
288 					   cons_index,
289 					   queue->prod_index,
290 					   queue->seed_index);
291 
292 
293 				ARK_PMD_LOG(DEBUG, "       :  UDM"
294 					   " prod: %" PRIU32
295 					   " len: %u\n",
296 					   queue->udm->rt_cfg.prod_idx,
297 					   meta->pkt_len);
298 				ark_mpu_dump(queue->mpu,
299 					     "    ",
300 					     queue->phys_qid);
301 				dump_mbuf_data(mbuf, 0, 256);
302 				/* its FUBAR so fix it */
303 				mbuf->pkt_len = 63;
304 				meta->pkt_len = 63;
305 			}
306 		}
307 
308 		if (unlikely(meta->pkt_len > queue->dataroom))
309 			cons_index = eth_ark_rx_jumbo
310 				(queue, meta, mbuf, cons_index + 1);
311 		else
312 			cons_index += 1;
313 
314 		rx_pkts[nb] = mbuf;
315 		nb++;
316 		if (nb >= nb_pkts)
317 			break;
318 	}
319 
320 	rx_user_meta_hook = queue->rx_user_meta_hook;
321 	for (pmbuf = rx_pkts, i = 0; rx_user_meta_hook && i < nb; i++) {
322 		mbuf = *pmbuf++;
323 		meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
324 		rx_user_meta_hook(mbuf, meta->user_meta, queue->ext_user_data);
325 	}
326 
327 	eth_ark_rx_update_cons_index(queue, cons_index);
328 
329 	return nb;
330 }
331 
332 /* ************************************************************************* */
333 static uint32_t
334 eth_ark_rx_jumbo(struct ark_rx_queue *queue,
335 		 struct ark_rx_meta *meta,
336 		 struct rte_mbuf *mbuf0,
337 		 uint32_t cons_index)
338 {
339 	struct rte_mbuf *mbuf_prev;
340 	struct rte_mbuf *mbuf;
341 
342 	uint16_t remaining;
343 	uint16_t data_len;
344 	uint16_t segments;
345 
346 	/* first buf populated by called */
347 	mbuf_prev = mbuf0;
348 	segments = 1;
349 	data_len = RTE_MIN(meta->pkt_len, queue->dataroom);
350 	remaining = meta->pkt_len - data_len;
351 	mbuf0->data_len = data_len;
352 
353 	/* HW guarantees that the data does not exceed prod_index! */
354 	while (remaining != 0) {
355 		data_len = RTE_MIN(remaining,
356 				   queue->dataroom);
357 
358 		remaining -= data_len;
359 		segments += 1;
360 
361 		mbuf = queue->reserve_q[cons_index & queue->queue_mask];
362 		mbuf_prev->next = mbuf;
363 		mbuf_prev = mbuf;
364 		mbuf->data_len = data_len;
365 
366 		cons_index += 1;
367 	}
368 
369 	mbuf0->nb_segs = segments;
370 	return cons_index;
371 }
372 
373 /* Drain the internal queue allowing hw to clear out. */
374 static void
375 eth_ark_rx_queue_drain(struct ark_rx_queue *queue)
376 {
377 	register uint32_t cons_index;
378 	struct rte_mbuf *mbuf;
379 
380 	cons_index = queue->cons_index;
381 
382 	/* NOT performance optimized, since this is a one-shot call */
383 	while ((cons_index ^ queue->prod_index) & queue->queue_mask) {
384 		mbuf = queue->reserve_q[cons_index & queue->queue_mask];
385 		rte_pktmbuf_free(mbuf);
386 		cons_index++;
387 		eth_ark_rx_update_cons_index(queue, cons_index);
388 	}
389 }
390 
391 uint32_t
392 eth_ark_dev_rx_queue_count(void *rx_queue)
393 {
394 	struct ark_rx_queue *queue;
395 
396 	queue = rx_queue;
397 	return (queue->prod_index - queue->cons_index);	/* mod arith */
398 }
399 
400 /* ************************************************************************* */
401 int
402 eth_ark_rx_start_queue(struct rte_eth_dev *dev, uint16_t queue_id)
403 {
404 	struct ark_rx_queue *queue;
405 
406 	queue = dev->data->rx_queues[queue_id];
407 	if (queue == 0)
408 		return -1;
409 
410 	dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
411 
412 	ark_mpu_set_producer(queue->mpu, queue->seed_index);
413 	ark_mpu_start(queue->mpu);
414 
415 	ark_udm_queue_enable(queue->udm, 1);
416 
417 	return 0;
418 }
419 
420 /* ************************************************************************* */
421 
422 /* Queue can be restarted.   data remains
423  */
424 int
425 eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id)
426 {
427 	struct ark_rx_queue *queue;
428 
429 	queue = dev->data->rx_queues[queue_id];
430 	if (queue == 0)
431 		return -1;
432 
433 	ark_udm_queue_enable(queue->udm, 0);
434 
435 	dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
436 
437 	return 0;
438 }
439 
440 /* ************************************************************************* */
441 static inline int
442 eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue)
443 {
444 	uint32_t limit = (queue->cons_index & ~(ARK_RX_MPU_CHUNK - 1)) +
445 		queue->queue_size;
446 	uint32_t seed_index = queue->seed_index;
447 
448 	uint32_t count = 0;
449 	uint32_t seed_m = queue->seed_index & queue->queue_mask;
450 
451 	uint32_t nb = limit - seed_index;
452 
453 	/* Handle wrap around -- remainder is filled on the next call */
454 	if (unlikely(seed_m + nb > queue->queue_size))
455 		nb = queue->queue_size - seed_m;
456 
457 	struct rte_mbuf **mbufs = &queue->reserve_q[seed_m];
458 	int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb);
459 
460 	if (unlikely(status != 0)) {
461 		ARK_PMD_LOG(NOTICE,
462 			    "Could not allocate %u mbufs from pool"
463 			    " for RX queue %u;"
464 			    " %u free buffers remaining in queue\n",
465 			    nb, queue->queue_index,
466 			    queue->seed_index - queue->cons_index);
467 		return -1;
468 	}
469 
470 	if (ARK_DEBUG_CORE) {		/* DEBUG */
471 		while (count != nb) {
472 			struct rte_mbuf *mbuf_init =
473 				queue->reserve_q[seed_m + count];
474 
475 			memset(mbuf_init->buf_addr, -1, 512);
476 			*((uint32_t *)mbuf_init->buf_addr) =
477 				seed_index + count;
478 			*(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) =
479 				queue->phys_qid;
480 			count++;
481 		}
482 		count = 0;
483 	} /* DEBUG */
484 	queue->seed_index += nb;
485 
486 	/* Duff's device https://en.wikipedia.org/wiki/Duff's_device */
487 	switch (nb % 4) {
488 	case 0:
489 		while (count != nb) {
490 			queue->paddress_q[seed_m++] =
491 				(*mbufs++)->buf_iova;
492 			count++;
493 		/* FALLTHROUGH */
494 	case 3:
495 		queue->paddress_q[seed_m++] =
496 			(*mbufs++)->buf_iova;
497 		count++;
498 		/* FALLTHROUGH */
499 	case 2:
500 		queue->paddress_q[seed_m++] =
501 			(*mbufs++)->buf_iova;
502 		count++;
503 		/* FALLTHROUGH */
504 	case 1:
505 		queue->paddress_q[seed_m++] =
506 			(*mbufs++)->buf_iova;
507 		count++;
508 		/* FALLTHROUGH */
509 
510 		} /* while (count != nb) */
511 	} /* switch */
512 
513 	return 0;
514 }
515 
516 void
517 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id,
518 		      const char *msg)
519 {
520 	struct ark_rx_queue *queue;
521 
522 	queue = dev->data->rx_queues[queue_id];
523 
524 	ark_ethdev_rx_dump(msg, queue);
525 }
526 
527 /* ************************************************************************* */
528 /* Call on device closed no user API, queue is stopped */
529 void
530 eth_ark_dev_rx_queue_release(void *vqueue)
531 {
532 	struct ark_rx_queue *queue;
533 	uint32_t i;
534 
535 	queue = (struct ark_rx_queue *)vqueue;
536 	if (queue == 0)
537 		return;
538 
539 	ark_udm_queue_enable(queue->udm, 0);
540 	/* Stop the MPU since pointer are going away */
541 	ark_mpu_stop(queue->mpu);
542 
543 	/* Need to clear out mbufs here, dropping packets along the way */
544 	eth_ark_rx_queue_drain(queue);
545 
546 	for (i = 0; i < queue->queue_size; ++i)
547 		rte_pktmbuf_free(queue->reserve_q[i]);
548 
549 	rte_free(queue->reserve_q);
550 	rte_free(queue->paddress_q);
551 	rte_free(queue);
552 }
553 
554 void
555 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
556 {
557 	struct ark_rx_queue *queue;
558 	struct ark_udm_t *udm;
559 
560 	queue = vqueue;
561 	if (queue == 0)
562 		return;
563 	udm = queue->udm;
564 
565 	uint64_t ibytes = ark_udm_bytes(udm);
566 	uint64_t ipackets = ark_udm_packets(udm);
567 	uint64_t idropped = ark_udm_dropped(queue->udm);
568 
569 	stats->q_ipackets[queue->queue_index] = ipackets;
570 	stats->q_ibytes[queue->queue_index] = ibytes;
571 	stats->q_errors[queue->queue_index] = idropped;
572 	stats->ipackets += ipackets;
573 	stats->ibytes += ibytes;
574 	stats->imissed += idropped;
575 }
576 
577 void
578 eth_rx_queue_stats_reset(void *vqueue)
579 {
580 	struct ark_rx_queue *queue;
581 
582 	queue = vqueue;
583 	if (queue == 0)
584 		return;
585 
586 	ark_udm_queue_stats_reset(queue->udm);
587 }
588 
589 static void
590 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue)
591 {
592 	if (queue == NULL)
593 		return;
594 	ARK_PMD_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name);
595 	ARK_PMD_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n",
596 			"queue_size", queue->queue_size,
597 			"seed_index", queue->seed_index,
598 			"prod_index", queue->prod_index,
599 			"cons_index", queue->cons_index);
600 
601 	ark_mpu_dump(queue->mpu, name, queue->phys_qid);
602 	ark_mpu_dump_setup(queue->mpu, queue->phys_qid);
603 	ark_udm_dump_setup(queue->udm, queue->phys_qid);
604 }
605 
606 /* Only used in debug.
607  * This function is a raw memory dump of a portion of an mbuf's memory
608  * region.  The usual function, rte_pktmbuf_dump() only shows data
609  * with respect to the data_off field.  This function show data
610  * anywhere in the mbuf's buffer.  This is useful for examining
611  * data in the headroom or tailroom portion of an mbuf.
612  */
613 static void
614 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi)
615 {
616 	uint16_t i, j;
617 
618 	ARK_PMD_LOG(DEBUG, " MBUF: %p len %d, off: %d\n",
619 		    mbuf, mbuf->pkt_len, mbuf->data_off);
620 	for (i = lo; i < hi; i += 16) {
621 		uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i);
622 
623 		ARK_PMD_LOG(DEBUG, "  %6d:  ", i);
624 		for (j = 0; j < 16; j++)
625 			ARK_PMD_LOG(DEBUG, " %02x", dp[j]);
626 
627 		ARK_PMD_LOG(DEBUG, "\n");
628 	}
629 }
630