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