net/i40e: fix Rx packet statistics
[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 "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_MAX_NOCHAIN (RTE_MBUF_DEFAULT_DATAROOM)
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 queue_size;
45         uint32_t queue_mask;
46
47         uint32_t seed_index;            /* step 1 set with empty mbuf */
48         uint32_t cons_index;            /* step 3 consumed by driver */
49
50         /* The queue Id is used to identify the HW Q */
51         uint16_t phys_qid;
52
53         /* The queue Index is used within the dpdk device structures */
54         uint16_t queue_index;
55
56         uint32_t unused;
57
58         /* next cache line - fields written by device */
59         RTE_MARKER cacheline1 __rte_cache_min_aligned;
60
61         volatile uint32_t prod_index;   /* step 2 filled by FPGA */
62 } __rte_cache_aligned;
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         queue->rx_user_meta_hook = ark->user_ext.rx_user_meta_hook;
172         queue->ext_user_data = ark->user_data[dev->data->port_id];
173
174         queue->reserve_q =
175                 rte_zmalloc_socket("Ark_rx_queue mbuf",
176                                    nb_desc * sizeof(struct rte_mbuf *),
177                                    64,
178                                    socket_id);
179         queue->paddress_q =
180                 rte_zmalloc_socket("Ark_rx_queue paddr",
181                                    nb_desc * sizeof(rte_iova_t),
182                                    64,
183                                    socket_id);
184
185         if (queue->reserve_q == 0 || queue->paddress_q == 0) {
186                 ARK_PMD_LOG(ERR,
187                             "Failed to allocate queue memory in %s\n",
188                             __func__);
189                 rte_free(queue->reserve_q);
190                 rte_free(queue->paddress_q);
191                 rte_free(queue);
192                 return -ENOMEM;
193         }
194
195         dev->data->rx_queues[queue_idx] = queue;
196         queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET);
197         queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET);
198
199         /* populate mbuf reserve */
200         status = eth_ark_rx_seed_mbufs(queue);
201
202         if (queue->seed_index != nb_desc) {
203                 ARK_PMD_LOG(ERR, "Failed to allocate %u mbufs for RX queue %d\n",
204                             nb_desc, qidx);
205                 status = -1;
206         }
207         /* MPU Setup */
208         if (status == 0)
209                 status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx);
210
211         if (unlikely(status != 0)) {
212                 struct rte_mbuf **mbuf;
213
214                 ARK_PMD_LOG(ERR, "Failed to initialize RX queue %d %s\n",
215                             qidx,
216                             __func__);
217                 /* Free the mbufs allocated */
218                 for (i = 0, mbuf = queue->reserve_q;
219                      i < queue->seed_index; ++i, mbuf++) {
220                         rte_pktmbuf_free(*mbuf);
221                 }
222                 rte_free(queue->reserve_q);
223                 rte_free(queue->paddress_q);
224                 rte_free(queue);
225                 return -1;              /* ERROR CODE */
226         }
227
228         return 0;
229 }
230
231 /* ************************************************************************* */
232 uint16_t
233 eth_ark_recv_pkts_noop(void *rx_queue __rte_unused,
234                        struct rte_mbuf **rx_pkts __rte_unused,
235                        uint16_t nb_pkts __rte_unused)
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                         if ((meta->pkt_len > (1024 * 16)) ||
280                             (meta->pkt_len == 0)) {
281                                 ARK_PMD_LOG(DEBUG, "RX: Bad Meta Q: %u"
282                                            " cons: %" PRIU32
283                                            " prod: %" PRIU32
284                                            " seed_index %" PRIU32
285                                            "\n",
286                                            queue->phys_qid,
287                                            cons_index,
288                                            queue->prod_index,
289                                            queue->seed_index);
290
291
292                                 ARK_PMD_LOG(DEBUG, "       :  UDM"
293                                            " prod: %" PRIU32
294                                            " len: %u\n",
295                                            queue->udm->rt_cfg.prod_idx,
296                                            meta->pkt_len);
297                                 ark_mpu_dump(queue->mpu,
298                                              "    ",
299                                              queue->phys_qid);
300                                 dump_mbuf_data(mbuf, 0, 256);
301                                 /* its FUBAR so fix it */
302                                 mbuf->pkt_len = 63;
303                                 meta->pkt_len = 63;
304                         }
305                 }
306
307                 if (unlikely(meta->pkt_len > ARK_RX_MAX_NOCHAIN))
308                         cons_index = eth_ark_rx_jumbo
309                                 (queue, meta, mbuf, cons_index + 1);
310                 else
311                         cons_index += 1;
312
313                 rx_pkts[nb] = mbuf;
314                 nb++;
315                 if (nb >= nb_pkts)
316                         break;
317         }
318
319         rx_user_meta_hook = queue->rx_user_meta_hook;
320         for (pmbuf = rx_pkts, i = 0; rx_user_meta_hook && i < nb; i++) {
321                 mbuf = *pmbuf++;
322                 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET);
323                 rx_user_meta_hook(mbuf, meta->user_meta, queue->ext_user_data);
324         }
325
326         eth_ark_rx_update_cons_index(queue, cons_index);
327
328         return nb;
329 }
330
331 /* ************************************************************************* */
332 static uint32_t
333 eth_ark_rx_jumbo(struct ark_rx_queue *queue,
334                  struct ark_rx_meta *meta,
335                  struct rte_mbuf *mbuf0,
336                  uint32_t cons_index)
337 {
338         struct rte_mbuf *mbuf_prev;
339         struct rte_mbuf *mbuf;
340
341         uint16_t remaining;
342         uint16_t data_len;
343         uint16_t segments;
344
345         /* first buf populated by called */
346         mbuf_prev = mbuf0;
347         segments = 1;
348         data_len = RTE_MIN(meta->pkt_len, RTE_MBUF_DEFAULT_DATAROOM);
349         remaining = meta->pkt_len - data_len;
350         mbuf0->data_len = data_len;
351
352         /* HW guarantees that the data does not exceed prod_index! */
353         while (remaining != 0) {
354                 data_len = RTE_MIN(remaining,
355                                    RTE_MBUF_DEFAULT_DATAROOM);
356
357                 remaining -= data_len;
358                 segments += 1;
359
360                 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
361                 mbuf_prev->next = mbuf;
362                 mbuf_prev = mbuf;
363                 mbuf->data_len = data_len;
364
365                 cons_index += 1;
366         }
367
368         mbuf0->nb_segs = segments;
369         return cons_index;
370 }
371
372 /* Drain the internal queue allowing hw to clear out. */
373 static void
374 eth_ark_rx_queue_drain(struct ark_rx_queue *queue)
375 {
376         register uint32_t cons_index;
377         struct rte_mbuf *mbuf;
378
379         cons_index = queue->cons_index;
380
381         /* NOT performance optimized, since this is a one-shot call */
382         while ((cons_index ^ queue->prod_index) & queue->queue_mask) {
383                 mbuf = queue->reserve_q[cons_index & queue->queue_mask];
384                 rte_pktmbuf_free(mbuf);
385                 cons_index++;
386                 eth_ark_rx_update_cons_index(queue, cons_index);
387         }
388 }
389
390 uint32_t
391 eth_ark_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t queue_id)
392 {
393         struct ark_rx_queue *queue;
394
395         queue = dev->data->rx_queues[queue_id];
396         return (queue->prod_index - queue->cons_index); /* mod arith */
397 }
398
399 /* ************************************************************************* */
400 int
401 eth_ark_rx_start_queue(struct rte_eth_dev *dev, uint16_t queue_id)
402 {
403         struct ark_rx_queue *queue;
404
405         queue = dev->data->rx_queues[queue_id];
406         if (queue == 0)
407                 return -1;
408
409         dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
410
411         ark_mpu_set_producer(queue->mpu, queue->seed_index);
412         ark_mpu_start(queue->mpu);
413
414         ark_udm_queue_enable(queue->udm, 1);
415
416         return 0;
417 }
418
419 /* ************************************************************************* */
420
421 /* Queue can be restarted.   data remains
422  */
423 int
424 eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id)
425 {
426         struct ark_rx_queue *queue;
427
428         queue = dev->data->rx_queues[queue_id];
429         if (queue == 0)
430                 return -1;
431
432         ark_udm_queue_enable(queue->udm, 0);
433
434         dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
435
436         return 0;
437 }
438
439 /* ************************************************************************* */
440 static inline int
441 eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue)
442 {
443         uint32_t limit = queue->cons_index + queue->queue_size;
444         uint32_t seed_index = queue->seed_index;
445
446         uint32_t count = 0;
447         uint32_t seed_m = queue->seed_index & queue->queue_mask;
448
449         uint32_t nb = limit - seed_index;
450
451         /* Handle wrap around -- remainder is filled on the next call */
452         if (unlikely(seed_m + nb > queue->queue_size))
453                 nb = queue->queue_size - seed_m;
454
455         struct rte_mbuf **mbufs = &queue->reserve_q[seed_m];
456         int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb);
457
458         if (unlikely(status != 0)) {
459                 ARK_PMD_LOG(NOTICE,
460                             "Could not allocate %u mbufs from pool"
461                             " for RX queue %u;"
462                             " %u free buffers remaining in queue\n",
463                             nb, queue->queue_index,
464                             queue->seed_index - queue->cons_index);
465                 return -1;
466         }
467
468         if (ARK_DEBUG_CORE) {           /* DEBUG */
469                 while (count != nb) {
470                         struct rte_mbuf *mbuf_init =
471                                 queue->reserve_q[seed_m + count];
472
473                         memset(mbuf_init->buf_addr, -1, 512);
474                         *((uint32_t *)mbuf_init->buf_addr) =
475                                 seed_index + count;
476                         *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) =
477                                 queue->phys_qid;
478                         count++;
479                 }
480                 count = 0;
481         } /* DEBUG */
482         queue->seed_index += nb;
483
484         /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */
485         switch (nb % 4) {
486         case 0:
487                 while (count != nb) {
488                         queue->paddress_q[seed_m++] =
489                                 (*mbufs++)->buf_iova;
490                         count++;
491                 /* FALLTHROUGH */
492         case 3:
493                 queue->paddress_q[seed_m++] =
494                         (*mbufs++)->buf_iova;
495                 count++;
496                 /* FALLTHROUGH */
497         case 2:
498                 queue->paddress_q[seed_m++] =
499                         (*mbufs++)->buf_iova;
500                 count++;
501                 /* FALLTHROUGH */
502         case 1:
503                 queue->paddress_q[seed_m++] =
504                         (*mbufs++)->buf_iova;
505                 count++;
506                 /* FALLTHROUGH */
507
508                 } /* while (count != nb) */
509         } /* switch */
510
511         return 0;
512 }
513
514 void
515 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id,
516                       const char *msg)
517 {
518         struct ark_rx_queue *queue;
519
520         queue = dev->data->rx_queues[queue_id];
521
522         ark_ethdev_rx_dump(msg, queue);
523 }
524
525 /* ************************************************************************* */
526 /* Call on device closed no user API, queue is stopped */
527 void
528 eth_ark_dev_rx_queue_release(void *vqueue)
529 {
530         struct ark_rx_queue *queue;
531         uint32_t i;
532
533         queue = (struct ark_rx_queue *)vqueue;
534         if (queue == 0)
535                 return;
536
537         ark_udm_queue_enable(queue->udm, 0);
538         /* Stop the MPU since pointer are going away */
539         ark_mpu_stop(queue->mpu);
540
541         /* Need to clear out mbufs here, dropping packets along the way */
542         eth_ark_rx_queue_drain(queue);
543
544         for (i = 0; i < queue->queue_size; ++i)
545                 rte_pktmbuf_free(queue->reserve_q[i]);
546
547         rte_free(queue->reserve_q);
548         rte_free(queue->paddress_q);
549         rte_free(queue);
550 }
551
552 void
553 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
554 {
555         struct ark_rx_queue *queue;
556         struct ark_udm_t *udm;
557
558         queue = vqueue;
559         if (queue == 0)
560                 return;
561         udm = queue->udm;
562
563         uint64_t ibytes = ark_udm_bytes(udm);
564         uint64_t ipackets = ark_udm_packets(udm);
565         uint64_t idropped = ark_udm_dropped(queue->udm);
566
567         stats->q_ipackets[queue->queue_index] = ipackets;
568         stats->q_ibytes[queue->queue_index] = ibytes;
569         stats->q_errors[queue->queue_index] = idropped;
570         stats->ipackets += ipackets;
571         stats->ibytes += ibytes;
572         stats->imissed += idropped;
573 }
574
575 void
576 eth_rx_queue_stats_reset(void *vqueue)
577 {
578         struct ark_rx_queue *queue;
579
580         queue = vqueue;
581         if (queue == 0)
582                 return;
583
584         ark_mpu_reset_stats(queue->mpu);
585         ark_udm_queue_stats_reset(queue->udm);
586 }
587
588 void
589 eth_ark_udm_force_close(struct rte_eth_dev *dev)
590 {
591         struct ark_adapter *ark = dev->data->dev_private;
592         struct ark_rx_queue *queue;
593         uint32_t index;
594         uint16_t i;
595
596         if (!ark_udm_is_flushed(ark->udm.v)) {
597                 /* restart the MPUs */
598                 ARK_PMD_LOG(NOTICE, "UDM not flushed -- forcing flush\n");
599                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
600                         queue = (struct ark_rx_queue *)dev->data->rx_queues[i];
601                         if (queue == 0)
602                                 continue;
603
604                         ark_mpu_start(queue->mpu);
605                         /* Add some buffers */
606                         index = 100000 + queue->seed_index;
607                         ark_mpu_set_producer(queue->mpu, index);
608                 }
609                 /* Wait to allow data to pass */
610                 usleep(100);
611
612                 ARK_PMD_LOG(DEBUG, "UDM forced flush attempt, stopped = %d\n",
613                                 ark_udm_is_flushed(ark->udm.v));
614         }
615         ark_udm_reset(ark->udm.v);
616 }
617
618 static void
619 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue)
620 {
621         if (queue == NULL)
622                 return;
623         ARK_PMD_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name);
624         ARK_PMD_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n",
625                         "queue_size", queue->queue_size,
626                         "seed_index", queue->seed_index,
627                         "prod_index", queue->prod_index,
628                         "cons_index", queue->cons_index);
629
630         ark_mpu_dump(queue->mpu, name, queue->phys_qid);
631         ark_mpu_dump_setup(queue->mpu, queue->phys_qid);
632         ark_udm_dump(queue->udm, name);
633         ark_udm_dump_setup(queue->udm, queue->phys_qid);
634 }
635
636 /* Only used in debug.
637  * This function is a raw memory dump of a portion of an mbuf's memory
638  * region.  The usual function, rte_pktmbuf_dump() only shows data
639  * with respect to the data_off field.  This function show data
640  * anywhere in the mbuf's buffer.  This is useful for examining
641  * data in the headroom or tailroom portion of an mbuf.
642  */
643 static void
644 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi)
645 {
646         uint16_t i, j;
647
648         ARK_PMD_LOG(DEBUG, " MBUF: %p len %d, off: %d\n",
649                     mbuf, mbuf->pkt_len, mbuf->data_off);
650         for (i = lo; i < hi; i += 16) {
651                 uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i);
652
653                 ARK_PMD_LOG(DEBUG, "  %6d:  ", i);
654                 for (j = 0; j < 16; j++)
655                         ARK_PMD_LOG(DEBUG, " %02x", dp[j]);
656
657                 ARK_PMD_LOG(DEBUG, "\n");
658         }
659 }