8e55b851a27ba5d9d9417465a9fe19a35793de28
[dpdk.git] / drivers / net / ark / ark_ethdev_rx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2015-2018 Atomic Rules LLC
3  */
4
5 #include <unistd.h>
6
7 #include "rte_pmd_ark.h"
8 #include "ark_ethdev_rx.h"
9 #include "ark_global.h"
10 #include "ark_logs.h"
11 #include "ark_mpu.h"
12 #include "ark_udm.h"
13
14 #define ARK_RX_META_SIZE 32
15 #define ARK_RX_META_OFFSET (RTE_PKTMBUF_HEADROOM - ARK_RX_META_SIZE)
16 #define ARK_RX_MAX_NOCHAIN (RTE_MBUF_DEFAULT_DATAROOM)
17
18 /* Forward declarations */
19 struct ark_rx_queue;
20 struct ark_rx_meta;
21
22 static void dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi);
23 static void ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue);
24 static uint32_t eth_ark_rx_jumbo(struct ark_rx_queue *queue,
25                                  struct ark_rx_meta *meta,
26                                  struct rte_mbuf *mbuf0,
27                                  uint32_t cons_index);
28 static inline int eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue);
29
30 /* ************************************************************************* */
31 struct ark_rx_queue {
32         /* array of mbufs to populate */
33         struct rte_mbuf **reserve_q;
34         /* array of physical addresses of the mbuf data pointer */
35         /* This point is a virtual address */
36         rte_iova_t *paddress_q;
37         struct rte_mempool *mb_pool;
38
39         struct ark_udm_t *udm;
40         struct ark_mpu_t *mpu;
41
42         uint32_t queue_size;
43         uint32_t queue_mask;
44
45         uint32_t seed_index;            /* step 1 set with empty mbuf */
46         uint32_t cons_index;            /* step 3 consumed by driver */
47
48         /* The queue Id is used to identify the HW Q */
49         uint16_t phys_qid;
50
51         /* The queue Index is used within the dpdk device structures */
52         uint16_t queue_index;
53
54         uint32_t unused;
55
56         /* separate cache line */
57         /* second cache line - fields only used in slow path */
58         RTE_MARKER cacheline1 __rte_cache_min_aligned;
59
60         volatile uint32_t prod_index;   /* step 2 filled by FPGA */
61 } __rte_cache_aligned;
62
63
64 /* ************************************************************************* */
65 static int
66 eth_ark_rx_hw_setup(struct rte_eth_dev *dev,
67                     struct ark_rx_queue *queue,
68                     uint16_t rx_queue_id __rte_unused, uint16_t rx_queue_idx)
69 {
70         rte_iova_t queue_base;
71         rte_iova_t phys_addr_q_base;
72         rte_iova_t phys_addr_prod_index;
73
74         queue_base = rte_malloc_virt2iova(queue);
75         phys_addr_prod_index = queue_base +
76                 offsetof(struct ark_rx_queue, prod_index);
77
78         phys_addr_q_base = rte_malloc_virt2iova(queue->paddress_q);
79
80         /* Verify HW */
81         if (ark_mpu_verify(queue->mpu, sizeof(rte_iova_t))) {
82                 ARK_PMD_LOG(ERR, "Illegal configuration rx queue\n");
83                 return -1;
84         }
85
86         /* Stop and Reset and configure MPU */
87         ark_mpu_configure(queue->mpu, phys_addr_q_base, queue->queue_size, 0);
88
89         ark_udm_write_addr(queue->udm, phys_addr_prod_index);
90
91         /* advance the valid pointer, but don't start until the queue starts */
92         ark_mpu_reset_stats(queue->mpu);
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) >= 64U) {
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->phys_qid = qidx;
168         queue->queue_index = queue_idx;
169         queue->queue_size = nb_desc;
170         queue->queue_mask = nb_desc - 1;
171
172         queue->reserve_q =
173                 rte_zmalloc_socket("Ark_rx_queue mbuf",
174                                    nb_desc * sizeof(struct rte_mbuf *),
175                                    64,
176                                    socket_id);
177         queue->paddress_q =
178                 rte_zmalloc_socket("Ark_rx_queue paddr",
179                                    nb_desc * sizeof(rte_iova_t),
180                                    64,
181                                    socket_id);
182
183         if (queue->reserve_q == 0 || queue->paddress_q == 0) {
184                 ARK_PMD_LOG(ERR,
185                             "Failed to allocate queue memory in %s\n",
186                             __func__);
187                 rte_free(queue->reserve_q);
188                 rte_free(queue->paddress_q);
189                 rte_free(queue);
190                 return -ENOMEM;
191         }
192
193         dev->data->rx_queues[queue_idx] = queue;
194         queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET);
195         queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET);
196
197         /* populate mbuf reserve */
198         status = eth_ark_rx_seed_mbufs(queue);
199
200         if (queue->seed_index != nb_desc) {
201                 ARK_PMD_LOG(ERR, "Failed to allocate %u mbufs for RX queue %d\n",
202                             nb_desc, qidx);
203                 status = -1;
204         }
205         /* MPU Setup */
206         if (status == 0)
207                 status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx);
208
209         if (unlikely(status != 0)) {
210                 struct rte_mbuf **mbuf;
211
212                 ARK_PMD_LOG(ERR, "Failed to initialize RX queue %d %s\n",
213                             qidx,
214                             __func__);
215                 /* Free the mbufs allocated */
216                 for (i = 0, mbuf = queue->reserve_q;
217                      i < queue->seed_index; ++i, mbuf++) {
218                         rte_pktmbuf_free(*mbuf);
219                 }
220                 rte_free(queue->reserve_q);
221                 rte_free(queue->paddress_q);
222                 rte_free(queue);
223                 return -1;              /* ERROR CODE */
224         }
225
226         return 0;
227 }
228
229 /* ************************************************************************* */
230 uint16_t
231 eth_ark_recv_pkts_noop(void *rx_queue __rte_unused,
232                        struct rte_mbuf **rx_pkts __rte_unused,
233                        uint16_t nb_pkts __rte_unused)
234 {
235         return 0;
236 }
237
238 /* ************************************************************************* */
239 uint16_t
240 eth_ark_recv_pkts(void *rx_queue,
241                   struct rte_mbuf **rx_pkts,
242                   uint16_t nb_pkts)
243 {
244         struct ark_rx_queue *queue;
245         register uint32_t cons_index, prod_index;
246         uint16_t nb;
247         struct rte_mbuf *mbuf;
248         struct ark_rx_meta *meta;
249
250         queue = (struct ark_rx_queue *)rx_queue;
251         if (unlikely(queue == 0))
252                 return 0;
253         if (unlikely(nb_pkts == 0))
254                 return 0;
255         prod_index = queue->prod_index;
256         cons_index = queue->cons_index;
257         nb = 0;
258
259         while (prod_index != cons_index) {
260                 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
261                 /* prefetch mbuf */
262                 rte_mbuf_prefetch_part1(mbuf);
263                 rte_mbuf_prefetch_part2(mbuf);
264
265                 /* META DATA embedded in headroom */
266                 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
267
268                 mbuf->port = meta->port;
269                 mbuf->pkt_len = meta->pkt_len;
270                 mbuf->data_len = meta->pkt_len;
271                 /* set timestamp if enabled at least on one device */
272                 if (ark_timestamp_rx_dynflag > 0) {
273                         *RTE_MBUF_DYNFIELD(mbuf, ark_timestamp_dynfield_offset,
274                                 rte_mbuf_timestamp_t *) = meta->timestamp;
275                         mbuf->ol_flags |= ark_timestamp_rx_dynflag;
276                 }
277                 rte_pmd_ark_mbuf_rx_userdata_set(mbuf, meta->user_data);
278
279                 if (ARK_DEBUG_CORE) {   /* debug sanity checks */
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 > ARK_RX_MAX_NOCHAIN))
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         eth_ark_rx_update_cons_index(queue, cons_index);
321
322         return nb;
323 }
324
325 /* ************************************************************************* */
326 static uint32_t
327 eth_ark_rx_jumbo(struct ark_rx_queue *queue,
328                  struct ark_rx_meta *meta,
329                  struct rte_mbuf *mbuf0,
330                  uint32_t cons_index)
331 {
332         struct rte_mbuf *mbuf_prev;
333         struct rte_mbuf *mbuf;
334
335         uint16_t remaining;
336         uint16_t data_len;
337         uint16_t segments;
338
339         /* first buf populated by called */
340         mbuf_prev = mbuf0;
341         segments = 1;
342         data_len = RTE_MIN(meta->pkt_len, RTE_MBUF_DEFAULT_DATAROOM);
343         remaining = meta->pkt_len - data_len;
344         mbuf0->data_len = data_len;
345
346         /* HW guarantees that the data does not exceed prod_index! */
347         while (remaining != 0) {
348                 data_len = RTE_MIN(remaining,
349                                    RTE_MBUF_DEFAULT_DATAROOM +
350                                    RTE_PKTMBUF_HEADROOM);
351
352                 remaining -= data_len;
353                 segments += 1;
354
355                 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
356                 mbuf_prev->next = mbuf;
357                 mbuf_prev = mbuf;
358                 mbuf->data_len = data_len;
359                 mbuf->data_off = 0;
360
361                 cons_index += 1;
362         }
363
364         mbuf0->nb_segs = segments;
365         return cons_index;
366 }
367
368 /* Drain the internal queue allowing hw to clear out. */
369 static void
370 eth_ark_rx_queue_drain(struct ark_rx_queue *queue)
371 {
372         register uint32_t cons_index;
373         struct rte_mbuf *mbuf;
374
375         cons_index = queue->cons_index;
376
377         /* NOT performance optimized, since this is a one-shot call */
378         while ((cons_index ^ queue->prod_index) & queue->queue_mask) {
379                 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
380                 rte_pktmbuf_free(mbuf);
381                 cons_index++;
382                 eth_ark_rx_update_cons_index(queue, cons_index);
383         }
384 }
385
386 uint32_t
387 eth_ark_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t queue_id)
388 {
389         struct ark_rx_queue *queue;
390
391         queue = dev->data->rx_queues[queue_id];
392         return (queue->prod_index - queue->cons_index); /* mod arith */
393 }
394
395 /* ************************************************************************* */
396 int
397 eth_ark_rx_start_queue(struct rte_eth_dev *dev, uint16_t queue_id)
398 {
399         struct ark_rx_queue *queue;
400
401         queue = dev->data->rx_queues[queue_id];
402         if (queue == 0)
403                 return -1;
404
405         dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
406
407         ark_mpu_set_producer(queue->mpu, queue->seed_index);
408         ark_mpu_start(queue->mpu);
409
410         ark_udm_queue_enable(queue->udm, 1);
411
412         return 0;
413 }
414
415 /* ************************************************************************* */
416
417 /* Queue can be restarted.   data remains
418  */
419 int
420 eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id)
421 {
422         struct ark_rx_queue *queue;
423
424         queue = dev->data->rx_queues[queue_id];
425         if (queue == 0)
426                 return -1;
427
428         ark_udm_queue_enable(queue->udm, 0);
429
430         dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
431
432         return 0;
433 }
434
435 /* ************************************************************************* */
436 static inline int
437 eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue)
438 {
439         uint32_t limit = queue->cons_index + queue->queue_size;
440         uint32_t seed_index = queue->seed_index;
441
442         uint32_t count = 0;
443         uint32_t seed_m = queue->seed_index & queue->queue_mask;
444
445         uint32_t nb = limit - seed_index;
446
447         /* Handle wrap around -- remainder is filled on the next call */
448         if (unlikely(seed_m + nb > queue->queue_size))
449                 nb = queue->queue_size - seed_m;
450
451         struct rte_mbuf **mbufs = &queue->reserve_q[seed_m];
452         int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb);
453
454         if (unlikely(status != 0)) {
455                 ARK_PMD_LOG(NOTICE,
456                             "Could not allocate %u mbufs from pool"
457                             " for RX queue %u;"
458                             " %u free buffers remaining in queue\n",
459                             nb, queue->queue_index,
460                             queue->seed_index - queue->cons_index);
461                 return -1;
462         }
463
464         if (ARK_DEBUG_CORE) {           /* DEBUG */
465                 while (count != nb) {
466                         struct rte_mbuf *mbuf_init =
467                                 queue->reserve_q[seed_m + count];
468
469                         memset(mbuf_init->buf_addr, -1, 512);
470                         *((uint32_t *)mbuf_init->buf_addr) =
471                                 seed_index + count;
472                         *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) =
473                                 queue->phys_qid;
474                         count++;
475                 }
476                 count = 0;
477         } /* DEBUG */
478         queue->seed_index += nb;
479
480         /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */
481         switch (nb % 4) {
482         case 0:
483                 while (count != nb) {
484                         queue->paddress_q[seed_m++] =
485                                 (*mbufs++)->buf_iova;
486                         count++;
487                 /* FALLTHROUGH */
488         case 3:
489                 queue->paddress_q[seed_m++] =
490                         (*mbufs++)->buf_iova;
491                 count++;
492                 /* FALLTHROUGH */
493         case 2:
494                 queue->paddress_q[seed_m++] =
495                         (*mbufs++)->buf_iova;
496                 count++;
497                 /* FALLTHROUGH */
498         case 1:
499                 queue->paddress_q[seed_m++] =
500                         (*mbufs++)->buf_iova;
501                 count++;
502                 /* FALLTHROUGH */
503
504                 } /* while (count != nb) */
505         } /* switch */
506
507         return 0;
508 }
509
510 void
511 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id,
512                       const char *msg)
513 {
514         struct ark_rx_queue *queue;
515
516         queue = dev->data->rx_queues[queue_id];
517
518         ark_ethdev_rx_dump(msg, queue);
519 }
520
521 /* ************************************************************************* */
522 /* Call on device closed no user API, queue is stopped */
523 void
524 eth_ark_dev_rx_queue_release(void *vqueue)
525 {
526         struct ark_rx_queue *queue;
527         uint32_t i;
528
529         queue = (struct ark_rx_queue *)vqueue;
530         if (queue == 0)
531                 return;
532
533         ark_udm_queue_enable(queue->udm, 0);
534         /* Stop the MPU since pointer are going away */
535         ark_mpu_stop(queue->mpu);
536
537         /* Need to clear out mbufs here, dropping packets along the way */
538         eth_ark_rx_queue_drain(queue);
539
540         for (i = 0; i < queue->queue_size; ++i)
541                 rte_pktmbuf_free(queue->reserve_q[i]);
542
543         rte_free(queue->reserve_q);
544         rte_free(queue->paddress_q);
545         rte_free(queue);
546 }
547
548 void
549 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
550 {
551         struct ark_rx_queue *queue;
552         struct ark_udm_t *udm;
553
554         queue = vqueue;
555         if (queue == 0)
556                 return;
557         udm = queue->udm;
558
559         uint64_t ibytes = ark_udm_bytes(udm);
560         uint64_t ipackets = ark_udm_packets(udm);
561         uint64_t idropped = ark_udm_dropped(queue->udm);
562
563         stats->q_ipackets[queue->queue_index] = ipackets;
564         stats->q_ibytes[queue->queue_index] = ibytes;
565         stats->q_errors[queue->queue_index] = idropped;
566         stats->ipackets += ipackets;
567         stats->ibytes += ibytes;
568         stats->imissed += idropped;
569 }
570
571 void
572 eth_rx_queue_stats_reset(void *vqueue)
573 {
574         struct ark_rx_queue *queue;
575
576         queue = vqueue;
577         if (queue == 0)
578                 return;
579
580         ark_mpu_reset_stats(queue->mpu);
581         ark_udm_queue_stats_reset(queue->udm);
582 }
583
584 void
585 eth_ark_udm_force_close(struct rte_eth_dev *dev)
586 {
587         struct ark_adapter *ark = dev->data->dev_private;
588         struct ark_rx_queue *queue;
589         uint32_t index;
590         uint16_t i;
591
592         if (!ark_udm_is_flushed(ark->udm.v)) {
593                 /* restart the MPUs */
594                 ARK_PMD_LOG(NOTICE, "UDM not flushed -- forcing flush\n");
595                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
596                         queue = (struct ark_rx_queue *)dev->data->rx_queues[i];
597                         if (queue == 0)
598                                 continue;
599
600                         ark_mpu_start(queue->mpu);
601                         /* Add some buffers */
602                         index = 100000 + queue->seed_index;
603                         ark_mpu_set_producer(queue->mpu, index);
604                 }
605                 /* Wait to allow data to pass */
606                 usleep(100);
607
608                 ARK_PMD_LOG(DEBUG, "UDM forced flush attempt, stopped = %d\n",
609                                 ark_udm_is_flushed(ark->udm.v));
610         }
611         ark_udm_reset(ark->udm.v);
612 }
613
614 static void
615 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue)
616 {
617         if (queue == NULL)
618                 return;
619         ARK_PMD_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name);
620         ARK_PMD_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n",
621                         "queue_size", queue->queue_size,
622                         "seed_index", queue->seed_index,
623                         "prod_index", queue->prod_index,
624                         "cons_index", queue->cons_index);
625
626         ark_mpu_dump(queue->mpu, name, queue->phys_qid);
627         ark_mpu_dump_setup(queue->mpu, queue->phys_qid);
628         ark_udm_dump(queue->udm, name);
629         ark_udm_dump_setup(queue->udm, queue->phys_qid);
630 }
631
632 /* Only used in debug.
633  * This function is a raw memory dump of a portion of an mbuf's memory
634  * region.  The usual function, rte_pktmbuf_dump() only shows data
635  * with respect to the data_off field.  This function show data
636  * anywhere in the mbuf's buffer.  This is useful for examining
637  * data in the headroom or tailroom portion of an mbuf.
638  */
639 static void
640 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi)
641 {
642         uint16_t i, j;
643
644         ARK_PMD_LOG(DEBUG, " MBUF: %p len %d, off: %d\n",
645                     mbuf, mbuf->pkt_len, mbuf->data_off);
646         for (i = lo; i < hi; i += 16) {
647                 uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i);
648
649                 ARK_PMD_LOG(DEBUG, "  %6d:  ", i);
650                 for (j = 0; j < 16; j++)
651                         ARK_PMD_LOG(DEBUG, " %02x", dp[j]);
652
653                 ARK_PMD_LOG(DEBUG, "\n");
654         }
655 }