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