0d7d708a1bf2c81a3afc8d07c44e5f607619d965
[dpdk.git] / drivers / net / bnxt / bnxt_rxq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) Broadcom Limited.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Broadcom Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <inttypes.h>
35
36 #include <rte_malloc.h>
37
38 #include "bnxt.h"
39 #include "bnxt_cpr.h"
40 #include "bnxt_filter.h"
41 #include "bnxt_hwrm.h"
42 #include "bnxt_ring.h"
43 #include "bnxt_rxq.h"
44 #include "bnxt_rxr.h"
45 #include "bnxt_vnic.h"
46 #include "hsi_struct_def_dpdk.h"
47
48 /*
49  * RX Queues
50  */
51
52 void bnxt_free_rxq_stats(struct bnxt_rx_queue *rxq)
53 {
54         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
55
56         if (cpr->hw_stats)
57                 cpr->hw_stats = NULL;
58 }
59
60 int bnxt_mq_rx_configure(struct bnxt *bp)
61 {
62         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
63         unsigned int i, j, nb_q_per_grp, ring_idx;
64         int start_grp_id, end_grp_id, rc = 0;
65         struct bnxt_vnic_info *vnic;
66         struct bnxt_filter_info *filter;
67         struct bnxt_rx_queue *rxq;
68
69         bp->nr_vnics = 0;
70
71         /* Single queue mode */
72         if (bp->rx_cp_nr_rings < 2) {
73                 vnic = bnxt_alloc_vnic(bp);
74                 if (!vnic) {
75                         RTE_LOG(ERR, PMD, "VNIC alloc failed\n");
76                         rc = -ENOMEM;
77                         goto err_out;
78                 }
79                 STAILQ_INSERT_TAIL(&bp->ff_pool[0], vnic, next);
80                 bp->nr_vnics++;
81
82                 rxq = bp->eth_dev->data->rx_queues[0];
83                 rxq->vnic = vnic;
84
85                 vnic->func_default = true;
86                 vnic->ff_pool_idx = 0;
87                 vnic->start_grp_id = 0;
88                 vnic->end_grp_id = vnic->start_grp_id;
89                 filter = bnxt_alloc_filter(bp);
90                 if (!filter) {
91                         RTE_LOG(ERR, PMD, "L2 filter alloc failed\n");
92                         rc = -ENOMEM;
93                         goto err_out;
94                 }
95                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
96                 goto out;
97         }
98
99         /* Multi-queue mode */
100         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_FLAG) {
101                 /* VMDq ONLY, VMDq+RSS, VMDq+DCB, VMDq+DCB+RSS */
102                 enum rte_eth_nb_pools pools;
103
104                 switch (dev_conf->rxmode.mq_mode) {
105                 case ETH_MQ_RX_VMDQ_RSS:
106                 case ETH_MQ_RX_VMDQ_ONLY:
107                         {
108                                 const struct rte_eth_vmdq_rx_conf *conf =
109                                     &dev_conf->rx_adv_conf.vmdq_rx_conf;
110
111                                 /* ETH_8/64_POOLs */
112                                 pools = conf->nb_queue_pools;
113                                 break;
114                         }
115                 default:
116                         RTE_LOG(ERR, PMD, "Unsupported mq_mod %d\n",
117                                 dev_conf->rxmode.mq_mode);
118                         rc = -EINVAL;
119                         goto err_out;
120                 }
121                 /* For each pool, allocate MACVLAN CFA rule & VNIC */
122                 if (!pools) {
123                         RTE_LOG(ERR, PMD,
124                                 "VMDq pool not set, defaulted to 64\n");
125                         pools = ETH_64_POOLS;
126                 }
127                 nb_q_per_grp = bp->rx_cp_nr_rings / pools;
128                 start_grp_id = 0;
129                 end_grp_id = nb_q_per_grp;
130
131                 ring_idx = 0;
132                 for (i = 0; i < pools; i++) {
133                         vnic = bnxt_alloc_vnic(bp);
134                         if (!vnic) {
135                                 RTE_LOG(ERR, PMD,
136                                         "VNIC alloc failed\n");
137                                 rc = -ENOMEM;
138                                 goto err_out;
139                         }
140                         STAILQ_INSERT_TAIL(&bp->ff_pool[i], vnic, next);
141                         bp->nr_vnics++;
142
143                         for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
144                                 rxq = bp->eth_dev->data->rx_queues[ring_idx];
145                                 rxq->vnic = vnic;
146                         }
147                         if (i == 0)
148                                 vnic->func_default = true;
149                         vnic->ff_pool_idx = i;
150                         vnic->start_grp_id = start_grp_id;
151                         vnic->end_grp_id = end_grp_id;
152
153                         filter = bnxt_alloc_filter(bp);
154                         if (!filter) {
155                                 RTE_LOG(ERR, PMD,
156                                         "L2 filter alloc failed\n");
157                                 rc = -ENOMEM;
158                                 goto err_out;
159                         }
160                         /*
161                          * TODO: Configure & associate CFA rule for
162                          * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
163                          */
164                         STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
165
166                         start_grp_id = end_grp_id + 1;
167                         end_grp_id += nb_q_per_grp;
168                 }
169                 goto out;
170         }
171
172         /* Non-VMDq mode - RSS, DCB, RSS+DCB */
173         /* Init default VNIC for RSS or DCB only */
174         vnic = bnxt_alloc_vnic(bp);
175         if (!vnic) {
176                 RTE_LOG(ERR, PMD, "VNIC alloc failed\n");
177                 rc = -ENOMEM;
178                 goto err_out;
179         }
180         /* Partition the rx queues for the single pool */
181         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
182                 rxq = bp->eth_dev->data->rx_queues[i];
183                 rxq->vnic = vnic;
184         }
185         STAILQ_INSERT_TAIL(&bp->ff_pool[0], vnic, next);
186         bp->nr_vnics++;
187
188         vnic->func_default = true;
189         vnic->ff_pool_idx = 0;
190         vnic->start_grp_id = 0;
191         vnic->end_grp_id = bp->rx_cp_nr_rings;
192         filter = bnxt_alloc_filter(bp);
193         if (!filter) {
194                 RTE_LOG(ERR, PMD, "L2 filter alloc failed\n");
195                 rc = -ENOMEM;
196                 goto err_out;
197         }
198         STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
199
200         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
201                 vnic->hash_type =
202                         HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4 |
203                         HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
204
205 out:
206         return rc;
207
208 err_out:
209         /* Free allocated vnic/filters */
210
211         return rc;
212 }
213
214 static void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
215 {
216         struct bnxt_sw_rx_bd *sw_ring;
217         uint16_t i;
218
219         if (rxq) {
220                 sw_ring = rxq->rx_ring->rx_buf_ring;
221                 if (sw_ring) {
222                         for (i = 0; i < rxq->nb_rx_desc; i++) {
223                                 if (sw_ring[i].mbuf) {
224                                         rte_pktmbuf_free_seg(sw_ring[i].mbuf);
225                                         sw_ring[i].mbuf = NULL;
226                                 }
227                         }
228                 }
229                 /* Free up mbufs in Agg ring */
230                 sw_ring = rxq->rx_ring->ag_buf_ring;
231                 if (sw_ring) {
232                         for (i = 0; i < rxq->nb_rx_desc; i++) {
233                                 if (sw_ring[i].mbuf) {
234                                         rte_pktmbuf_free_seg(sw_ring[i].mbuf);
235                                         sw_ring[i].mbuf = NULL;
236                                 }
237                         }
238                 }
239         }
240 }
241
242 void bnxt_free_rx_mbufs(struct bnxt *bp)
243 {
244         struct bnxt_rx_queue *rxq;
245         int i;
246
247         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
248                 rxq = bp->rx_queues[i];
249                 bnxt_rx_queue_release_mbufs(rxq);
250         }
251 }
252
253 void bnxt_rx_queue_release_op(void *rx_queue)
254 {
255         struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
256
257         if (rxq) {
258                 bnxt_rx_queue_release_mbufs(rxq);
259
260                 /* Free RX ring hardware descriptors */
261                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
262                 /* Free RX Agg ring hardware descriptors */
263                 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
264
265                 /* Free RX completion ring hardware descriptors */
266                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
267
268                 bnxt_free_rxq_stats(rxq);
269
270                 rte_free(rxq);
271         }
272 }
273
274 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
275                                uint16_t queue_idx,
276                                uint16_t nb_desc,
277                                unsigned int socket_id,
278                                const struct rte_eth_rxconf *rx_conf,
279                                struct rte_mempool *mp)
280 {
281         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
282         struct bnxt_rx_queue *rxq;
283         int rc = 0;
284
285         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
286                 RTE_LOG(ERR, PMD, "nb_desc %d is invalid", nb_desc);
287                 rc = -EINVAL;
288                 goto out;
289         }
290
291         if (eth_dev->data->rx_queues) {
292                 rxq = eth_dev->data->rx_queues[queue_idx];
293                 if (rxq)
294                         bnxt_rx_queue_release_op(rxq);
295         }
296         rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
297                                  RTE_CACHE_LINE_SIZE, socket_id);
298         if (!rxq) {
299                 RTE_LOG(ERR, PMD, "bnxt_rx_queue allocation failed!");
300                 rc = -ENOMEM;
301                 goto out;
302         }
303         rxq->bp = bp;
304         rxq->mb_pool = mp;
305         rxq->nb_rx_desc = nb_desc;
306         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
307
308         RTE_LOG(DEBUG, PMD, "RX Buf size is %d\n", rxq->rx_buf_use_size);
309         RTE_LOG(DEBUG, PMD, "RX Buf MTU %d\n", eth_dev->data->mtu);
310
311         rc = bnxt_init_rx_ring_struct(rxq, socket_id);
312         if (rc)
313                 goto out;
314
315         rxq->queue_id = queue_idx;
316         rxq->port_id = eth_dev->data->port_id;
317         rxq->crc_len = (uint8_t)((eth_dev->data->dev_conf.rxmode.hw_strip_crc) ?
318                                 0 : ETHER_CRC_LEN);
319
320         eth_dev->data->rx_queues[queue_idx] = rxq;
321         /* Allocate RX ring hardware descriptors */
322         if (bnxt_alloc_rings(bp, queue_idx, NULL, rxq->rx_ring, rxq->cp_ring,
323                         "rxr")) {
324                 RTE_LOG(ERR, PMD, "ring_dma_zone_reserve for rx_ring failed!");
325                 bnxt_rx_queue_release_op(rxq);
326                 rc = -ENOMEM;
327                 goto out;
328         }
329
330 out:
331         return rc;
332 }