mempool: introduce helpers for populate and required size
[dpdk.git] / drivers / net / nfb / nfb_rx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Cesnet
3  * Copyright(c) 2019 Netcope Technologies, a.s. <info@netcope.com>
4  * All rights reserved.
5  */
6
7 #include <rte_kvargs.h>
8
9 #include "nfb_rx.h"
10 #include "nfb.h"
11
12 static int
13 timestamp_check_handler(__rte_unused const char *key,
14         const char *value, __rte_unused void *opaque)
15 {
16         if (strcmp(value, "1"))
17                 return -1;
18
19         return 0;
20 }
21
22
23 static int
24 nfb_check_timestamp(struct rte_devargs *devargs)
25 {
26         struct rte_kvargs *kvlist;
27
28         if (devargs == NULL)
29                 return 0;
30
31         kvlist = rte_kvargs_parse(devargs->args, NULL);
32         if (kvlist == NULL)
33                 return 0;
34
35         if (!rte_kvargs_count(kvlist, TIMESTAMP_ARG)) {
36                 rte_kvargs_free(kvlist);
37                 return 0;
38         }
39         /* Timestamps are enabled when there is
40          * key-value pair: enable_timestamp=1
41          */
42         if (rte_kvargs_process(kvlist, TIMESTAMP_ARG,
43                 timestamp_check_handler, NULL) < 0) {
44                 rte_kvargs_free(kvlist);
45                 return 0;
46         }
47         rte_kvargs_free(kvlist);
48
49         return 1;
50 }
51
52 int
53 nfb_eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rxq_id)
54 {
55         struct ndp_rx_queue *rxq = dev->data->rx_queues[rxq_id];
56         int ret;
57
58         if (rxq->queue == NULL) {
59                 RTE_LOG(ERR, PMD, "RX NDP queue is NULL!\n");
60                 return -EINVAL;
61         }
62
63         ret = ndp_queue_start(rxq->queue);
64         if (ret != 0)
65                 goto err;
66         dev->data->rx_queue_state[rxq_id] = RTE_ETH_QUEUE_STATE_STARTED;
67         return 0;
68
69 err:
70         return -EINVAL;
71 }
72
73 int
74 nfb_eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rxq_id)
75 {
76         struct ndp_rx_queue *rxq = dev->data->rx_queues[rxq_id];
77         int ret;
78
79         if (rxq->queue == NULL) {
80                 RTE_LOG(ERR, PMD, "RX NDP queue is NULL!\n");
81                 return -EINVAL;
82         }
83
84         ret = ndp_queue_stop(rxq->queue);
85         if (ret != 0)
86                 return -EINVAL;
87
88         dev->data->rx_queue_state[rxq_id] = RTE_ETH_QUEUE_STATE_STOPPED;
89         return 0;
90 }
91
92 int
93 nfb_eth_rx_queue_setup(struct rte_eth_dev *dev,
94                 uint16_t rx_queue_id,
95                 uint16_t nb_rx_desc __rte_unused,
96                 unsigned int socket_id,
97                 const struct rte_eth_rxconf *rx_conf __rte_unused,
98                 struct rte_mempool *mb_pool)
99 {
100         struct pmd_internals *internals = dev->data->dev_private;
101
102         struct ndp_rx_queue *rxq;
103         int ret;
104
105         rxq = rte_zmalloc_socket("ndp rx queue",
106                         sizeof(struct ndp_rx_queue),
107                         RTE_CACHE_LINE_SIZE, socket_id);
108
109         if (rxq == NULL) {
110                 RTE_LOG(ERR, PMD, "rte_zmalloc_socket() failed for rx queue id "
111                                 "%" PRIu16 "!\n", rx_queue_id);
112                 return -ENOMEM;
113         }
114
115         rxq->flags = 0;
116
117         ret = nfb_eth_rx_queue_init(internals->nfb,
118                 rx_queue_id,
119                 dev->data->port_id,
120                 mb_pool,
121                 rxq);
122
123         if (ret == 0)
124                 dev->data->rx_queues[rx_queue_id] = rxq;
125         else
126                 rte_free(rxq);
127
128         if (nfb_check_timestamp(dev->device->devargs))
129                 rxq->flags |= NFB_TIMESTAMP_FLAG;
130
131         return ret;
132 }
133
134 int
135 nfb_eth_rx_queue_init(struct nfb_device *nfb,
136                 uint16_t rx_queue_id,
137                 uint16_t port_id,
138                 struct rte_mempool *mb_pool,
139                 struct ndp_rx_queue *rxq)
140 {
141         const struct rte_pktmbuf_pool_private *mbp_priv =
142                 rte_mempool_get_priv(mb_pool);
143
144         if (nfb == NULL)
145                 return -EINVAL;
146
147         rxq->queue = ndp_open_rx_queue(nfb, rx_queue_id);
148         if (rxq->queue == NULL)
149                 return -EINVAL;
150
151         rxq->nfb = nfb;
152         rxq->rx_queue_id = rx_queue_id;
153         rxq->in_port = port_id;
154         rxq->mb_pool = mb_pool;
155         rxq->buf_size = (uint16_t)(mbp_priv->mbuf_data_room_size -
156                 RTE_PKTMBUF_HEADROOM);
157
158         rxq->rx_pkts = 0;
159         rxq->rx_bytes = 0;
160         rxq->err_pkts = 0;
161
162         return 0;
163 }
164
165 void
166 nfb_eth_rx_queue_release(void *q)
167 {
168         struct ndp_rx_queue *rxq = (struct ndp_rx_queue *)q;
169         if (rxq->queue != NULL) {
170                 ndp_close_rx_queue(rxq->queue);
171                 rte_free(rxq);
172                 rxq->queue = NULL;
173         }
174 }