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