0793820b14e309f74f7e4ec82ab45263363e6642
[deb_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                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
80                 STAILQ_INSERT_TAIL(&bp->ff_pool[0], vnic, next);
81                 bp->nr_vnics++;
82
83                 rxq = bp->eth_dev->data->rx_queues[0];
84                 rxq->vnic = vnic;
85
86                 vnic->func_default = true;
87                 vnic->ff_pool_idx = 0;
88                 vnic->start_grp_id = 0;
89                 vnic->end_grp_id = vnic->start_grp_id;
90                 filter = bnxt_alloc_filter(bp);
91                 if (!filter) {
92                         RTE_LOG(ERR, PMD, "L2 filter alloc failed\n");
93                         rc = -ENOMEM;
94                         goto err_out;
95                 }
96                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
97                 goto out;
98         }
99
100         /* Multi-queue mode */
101         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_FLAG) {
102                 /* VMDq ONLY, VMDq+RSS, VMDq+DCB, VMDq+DCB+RSS */
103                 enum rte_eth_nb_pools pools;
104
105                 switch (dev_conf->rxmode.mq_mode) {
106                 case ETH_MQ_RX_VMDQ_RSS:
107                 case ETH_MQ_RX_VMDQ_ONLY:
108                         {
109                                 const struct rte_eth_vmdq_rx_conf *conf =
110                                     &dev_conf->rx_adv_conf.vmdq_rx_conf;
111
112                                 /* ETH_8/64_POOLs */
113                                 pools = conf->nb_queue_pools;
114                                 break;
115                         }
116                 default:
117                         RTE_LOG(ERR, PMD, "Unsupported mq_mod %d\n",
118                                 dev_conf->rxmode.mq_mode);
119                         rc = -EINVAL;
120                         goto err_out;
121                 }
122                 /* For each pool, allocate MACVLAN CFA rule & VNIC */
123                 if (!pools) {
124                         pools = RTE_MIN(bp->max_vnics,
125                             RTE_MIN(bp->max_l2_ctx,
126                              RTE_MIN(bp->max_rsscos_ctx, ETH_64_POOLS)));
127                         RTE_LOG(ERR, PMD,
128                                 "VMDq pool not set, defaulted to 64\n");
129                         pools = ETH_64_POOLS;
130                 }
131                 nb_q_per_grp = bp->rx_cp_nr_rings / pools;
132                 start_grp_id = 0;
133                 end_grp_id = nb_q_per_grp;
134
135                 ring_idx = 0;
136                 for (i = 0; i < pools; i++) {
137                         vnic = bnxt_alloc_vnic(bp);
138                         if (!vnic) {
139                                 RTE_LOG(ERR, PMD,
140                                         "VNIC alloc failed\n");
141                                 rc = -ENOMEM;
142                                 goto err_out;
143                         }
144                         vnic->flags |= BNXT_VNIC_INFO_BCAST;
145                         STAILQ_INSERT_TAIL(&bp->ff_pool[i], vnic, next);
146                         bp->nr_vnics++;
147
148                         for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
149                                 rxq = bp->eth_dev->data->rx_queues[ring_idx];
150                                 rxq->vnic = vnic;
151                         }
152                         if (i == 0)
153                                 vnic->func_default = true;
154                         vnic->ff_pool_idx = i;
155                         vnic->start_grp_id = start_grp_id;
156                         vnic->end_grp_id = end_grp_id;
157
158                         filter = bnxt_alloc_filter(bp);
159                         if (!filter) {
160                                 RTE_LOG(ERR, PMD,
161                                         "L2 filter alloc failed\n");
162                                 rc = -ENOMEM;
163                                 goto err_out;
164                         }
165                         /*
166                          * TODO: Configure & associate CFA rule for
167                          * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
168                          */
169                         STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
170
171                         start_grp_id = end_grp_id + 1;
172                         end_grp_id += nb_q_per_grp;
173                 }
174                 goto out;
175         }
176
177         /* Non-VMDq mode - RSS, DCB, RSS+DCB */
178         /* Init default VNIC for RSS or DCB only */
179         vnic = bnxt_alloc_vnic(bp);
180         if (!vnic) {
181                 RTE_LOG(ERR, PMD, "VNIC alloc failed\n");
182                 rc = -ENOMEM;
183                 goto err_out;
184         }
185         vnic->flags |= BNXT_VNIC_INFO_BCAST;
186         /* Partition the rx queues for the single pool */
187         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
188                 rxq = bp->eth_dev->data->rx_queues[i];
189                 rxq->vnic = vnic;
190         }
191         STAILQ_INSERT_TAIL(&bp->ff_pool[0], vnic, next);
192         bp->nr_vnics++;
193
194         vnic->func_default = true;
195         vnic->ff_pool_idx = 0;
196         vnic->start_grp_id = 0;
197         vnic->end_grp_id = bp->rx_cp_nr_rings;
198         filter = bnxt_alloc_filter(bp);
199         if (!filter) {
200                 RTE_LOG(ERR, PMD, "L2 filter alloc failed\n");
201                 rc = -ENOMEM;
202                 goto err_out;
203         }
204         STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
205
206         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
207                 vnic->hash_type =
208                         HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4 |
209                         HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
210
211 out:
212         return rc;
213
214 err_out:
215         /* Free allocated vnic/filters */
216
217         return rc;
218 }
219
220 static void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
221 {
222         struct bnxt_sw_rx_bd *sw_ring;
223         struct bnxt_tpa_info *tpa_info;
224         uint16_t i;
225
226         if (rxq) {
227                 sw_ring = rxq->rx_ring->rx_buf_ring;
228                 if (sw_ring) {
229                         for (i = 0; i < rxq->nb_rx_desc; i++) {
230                                 if (sw_ring[i].mbuf) {
231                                         rte_pktmbuf_free_seg(sw_ring[i].mbuf);
232                                         sw_ring[i].mbuf = NULL;
233                                 }
234                         }
235                 }
236                 /* Free up mbufs in Agg ring */
237                 sw_ring = rxq->rx_ring->ag_buf_ring;
238                 if (sw_ring) {
239                         for (i = 0; i < rxq->nb_rx_desc; i++) {
240                                 if (sw_ring[i].mbuf) {
241                                         rte_pktmbuf_free_seg(sw_ring[i].mbuf);
242                                         sw_ring[i].mbuf = NULL;
243                                 }
244                         }
245                 }
246
247                 /* Free up mbufs in TPA */
248                 tpa_info = rxq->rx_ring->tpa_info;
249                 if (tpa_info) {
250                         for (i = 0; i < BNXT_TPA_MAX; i++) {
251                                 if (tpa_info[i].mbuf) {
252                                         rte_pktmbuf_free_seg(tpa_info[i].mbuf);
253                                         tpa_info[i].mbuf = NULL;
254                                 }
255                         }
256                 }
257         }
258 }
259
260 void bnxt_free_rx_mbufs(struct bnxt *bp)
261 {
262         struct bnxt_rx_queue *rxq;
263         int i;
264
265         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
266                 rxq = bp->rx_queues[i];
267                 bnxt_rx_queue_release_mbufs(rxq);
268         }
269 }
270
271 void bnxt_rx_queue_release_op(void *rx_queue)
272 {
273         struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
274
275         if (rxq) {
276                 bnxt_rx_queue_release_mbufs(rxq);
277
278                 /* Free RX ring hardware descriptors */
279                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
280                 /* Free RX Agg ring hardware descriptors */
281                 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
282
283                 /* Free RX completion ring hardware descriptors */
284                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
285
286                 bnxt_free_rxq_stats(rxq);
287
288                 rte_free(rxq);
289         }
290 }
291
292 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
293                                uint16_t queue_idx,
294                                uint16_t nb_desc,
295                                unsigned int socket_id,
296                                const struct rte_eth_rxconf *rx_conf,
297                                struct rte_mempool *mp)
298 {
299         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
300         struct bnxt_rx_queue *rxq;
301         int rc = 0;
302
303         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
304                 RTE_LOG(ERR, PMD, "nb_desc %d is invalid\n", nb_desc);
305                 rc = -EINVAL;
306                 goto out;
307         }
308
309         if (eth_dev->data->rx_queues) {
310                 rxq = eth_dev->data->rx_queues[queue_idx];
311                 if (rxq)
312                         bnxt_rx_queue_release_op(rxq);
313         }
314         rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
315                                  RTE_CACHE_LINE_SIZE, socket_id);
316         if (!rxq) {
317                 RTE_LOG(ERR, PMD, "bnxt_rx_queue allocation failed!\n");
318                 rc = -ENOMEM;
319                 goto out;
320         }
321         rxq->bp = bp;
322         rxq->mb_pool = mp;
323         rxq->nb_rx_desc = nb_desc;
324         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
325
326         RTE_LOG(DEBUG, PMD, "RX Buf size is %d\n", rxq->rx_buf_use_size);
327         RTE_LOG(DEBUG, PMD, "RX Buf MTU %d\n", eth_dev->data->mtu);
328
329         rc = bnxt_init_rx_ring_struct(rxq, socket_id);
330         if (rc)
331                 goto out;
332
333         rxq->queue_id = queue_idx;
334         rxq->port_id = eth_dev->data->port_id;
335         rxq->crc_len = (uint8_t)((eth_dev->data->dev_conf.rxmode.hw_strip_crc) ?
336                                 0 : ETHER_CRC_LEN);
337
338         eth_dev->data->rx_queues[queue_idx] = rxq;
339         /* Allocate RX ring hardware descriptors */
340         if (bnxt_alloc_rings(bp, queue_idx, NULL, rxq->rx_ring, rxq->cp_ring,
341                         "rxr")) {
342                 RTE_LOG(ERR, PMD,
343                         "ring_dma_zone_reserve for rx_ring failed!\n");
344                 bnxt_rx_queue_release_op(rxq);
345                 rc = -ENOMEM;
346                 goto out;
347         }
348
349 out:
350         return rc;
351 }