net/avp: remove resources when port is closed
[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 "nfb_rx.h"
8 #include "nfb.h"
9
10 int
11 nfb_eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rxq_id)
12 {
13         struct ndp_rx_queue *rxq = dev->data->rx_queues[rxq_id];
14         int ret;
15
16         if (rxq->queue == NULL) {
17                 RTE_LOG(ERR, PMD, "RX NDP queue is NULL!\n");
18                 return -EINVAL;
19         }
20
21         ret = ndp_queue_start(rxq->queue);
22         if (ret != 0)
23                 goto err;
24         dev->data->rx_queue_state[rxq_id] = RTE_ETH_QUEUE_STATE_STARTED;
25         return 0;
26
27 err:
28         return -EINVAL;
29 }
30
31 int
32 nfb_eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rxq_id)
33 {
34         struct ndp_rx_queue *rxq = dev->data->rx_queues[rxq_id];
35         int ret;
36
37         if (rxq->queue == NULL) {
38                 RTE_LOG(ERR, PMD, "RX NDP queue is NULL!\n");
39                 return -EINVAL;
40         }
41
42         ret = ndp_queue_stop(rxq->queue);
43         if (ret != 0)
44                 return -EINVAL;
45
46         dev->data->rx_queue_state[rxq_id] = RTE_ETH_QUEUE_STATE_STOPPED;
47         return 0;
48 }
49
50 int
51 nfb_eth_rx_queue_setup(struct rte_eth_dev *dev,
52                 uint16_t rx_queue_id,
53                 uint16_t nb_rx_desc __rte_unused,
54                 unsigned int socket_id,
55                 const struct rte_eth_rxconf *rx_conf __rte_unused,
56                 struct rte_mempool *mb_pool)
57 {
58         struct pmd_internals *internals = dev->data->dev_private;
59
60         struct ndp_rx_queue *rxq;
61         int ret;
62
63         rxq = rte_zmalloc_socket("ndp rx queue",
64                         sizeof(struct ndp_rx_queue),
65                         RTE_CACHE_LINE_SIZE, socket_id);
66
67         if (rxq == NULL) {
68                 RTE_LOG(ERR, PMD, "rte_zmalloc_socket() failed for rx queue id "
69                                 "%" PRIu16 "!\n", rx_queue_id);
70                 return -ENOMEM;
71         }
72
73         ret = nfb_eth_rx_queue_init(internals->nfb,
74                 rx_queue_id,
75                 dev->data->port_id,
76                 mb_pool,
77                 rxq);
78
79         if (ret == 0)
80                 dev->data->rx_queues[rx_queue_id] = rxq;
81         else
82                 rte_free(rxq);
83
84         return ret;
85 }
86
87 int
88 nfb_eth_rx_queue_init(struct nfb_device *nfb,
89                 uint16_t rx_queue_id,
90                 uint16_t port_id,
91                 struct rte_mempool *mb_pool,
92                 struct ndp_rx_queue *rxq)
93 {
94         const struct rte_pktmbuf_pool_private *mbp_priv =
95                 rte_mempool_get_priv(mb_pool);
96
97         if (nfb == NULL)
98                 return -EINVAL;
99
100         rxq->queue = ndp_open_rx_queue(nfb, rx_queue_id);
101         if (rxq->queue == NULL)
102                 return -EINVAL;
103
104         rxq->nfb = nfb;
105         rxq->rx_queue_id = rx_queue_id;
106         rxq->in_port = port_id;
107         rxq->mb_pool = mb_pool;
108         rxq->buf_size = (uint16_t)(mbp_priv->mbuf_data_room_size -
109                 RTE_PKTMBUF_HEADROOM);
110
111         rxq->rx_pkts = 0;
112         rxq->rx_bytes = 0;
113         rxq->err_pkts = 0;
114
115         return 0;
116 }
117
118 void
119 nfb_eth_rx_queue_release(void *q)
120 {
121         struct ndp_rx_queue *rxq = (struct ndp_rx_queue *)q;
122         if (rxq->queue != NULL) {
123                 ndp_close_rx_queue(rxq->queue);
124                 rte_free(rxq);
125                 rxq->queue = NULL;
126         }
127 }