net/bnxt: update returned error on invalid max ring
[dpdk.git] / drivers / net / bnxt / bnxt_rxq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Broadcom
3  * All rights reserved.
4  */
5
6 #include <inttypes.h>
7
8 #include <rte_malloc.h>
9
10 #include "bnxt.h"
11 #include "bnxt_cpr.h"
12 #include "bnxt_filter.h"
13 #include "bnxt_hwrm.h"
14 #include "bnxt_ring.h"
15 #include "bnxt_rxq.h"
16 #include "bnxt_rxr.h"
17 #include "bnxt_vnic.h"
18 #include "hsi_struct_def_dpdk.h"
19
20 /*
21  * RX Queues
22  */
23
24 void bnxt_free_rxq_stats(struct bnxt_rx_queue *rxq)
25 {
26         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
27
28         if (cpr->hw_stats)
29                 cpr->hw_stats = NULL;
30 }
31
32 int bnxt_mq_rx_configure(struct bnxt *bp)
33 {
34         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
35         const struct rte_eth_vmdq_rx_conf *conf =
36                     &dev_conf->rx_adv_conf.vmdq_rx_conf;
37         unsigned int i, j, nb_q_per_grp = 1, ring_idx = 0;
38         int start_grp_id, end_grp_id = 1, rc = 0;
39         struct bnxt_vnic_info *vnic;
40         struct bnxt_filter_info *filter;
41         enum rte_eth_nb_pools pools = bp->rx_cp_nr_rings, max_pools = 0;
42         struct bnxt_rx_queue *rxq;
43
44         bp->nr_vnics = 0;
45
46         /* Single queue mode */
47         if (bp->rx_cp_nr_rings < 2) {
48                 vnic = bnxt_alloc_vnic(bp);
49                 if (!vnic) {
50                         PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
51                         rc = -ENOMEM;
52                         goto err_out;
53                 }
54                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
55                 STAILQ_INSERT_TAIL(&bp->ff_pool[0], vnic, next);
56                 bp->nr_vnics++;
57
58                 rxq = bp->eth_dev->data->rx_queues[0];
59                 rxq->vnic = vnic;
60
61                 vnic->func_default = true;
62                 vnic->ff_pool_idx = 0;
63                 vnic->start_grp_id = 0;
64                 vnic->end_grp_id = vnic->start_grp_id;
65                 filter = bnxt_alloc_filter(bp);
66                 if (!filter) {
67                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
68                         rc = -ENOMEM;
69                         goto err_out;
70                 }
71                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
72                 goto out;
73         }
74
75         /* Multi-queue mode */
76         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB_RSS) {
77                 /* VMDq ONLY, VMDq+RSS, VMDq+DCB, VMDq+DCB+RSS */
78
79                 switch (dev_conf->rxmode.mq_mode) {
80                 case ETH_MQ_RX_VMDQ_RSS:
81                 case ETH_MQ_RX_VMDQ_ONLY:
82                         /* ETH_8/64_POOLs */
83                         pools = conf->nb_queue_pools;
84                         /* For each pool, allocate MACVLAN CFA rule & VNIC */
85                         max_pools = RTE_MIN(bp->max_vnics,
86                                             RTE_MIN(bp->max_l2_ctx,
87                                             RTE_MIN(bp->max_rsscos_ctx,
88                                                     ETH_64_POOLS)));
89                         if (pools > max_pools)
90                                 pools = max_pools;
91                         break;
92                 case ETH_MQ_RX_RSS:
93                         pools = 1;
94                         break;
95                 default:
96                         PMD_DRV_LOG(ERR, "Unsupported mq_mod %d\n",
97                                 dev_conf->rxmode.mq_mode);
98                         rc = -EINVAL;
99                         goto err_out;
100                 }
101         }
102
103         nb_q_per_grp = bp->rx_cp_nr_rings / pools;
104         start_grp_id = 0;
105         end_grp_id = nb_q_per_grp;
106
107         for (i = 0; i < pools; i++) {
108                 vnic = bnxt_alloc_vnic(bp);
109                 if (!vnic) {
110                         PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
111                         rc = -ENOMEM;
112                         goto err_out;
113                 }
114                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
115                 STAILQ_INSERT_TAIL(&bp->ff_pool[i], vnic, next);
116                 bp->nr_vnics++;
117
118                 for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
119                         rxq = bp->eth_dev->data->rx_queues[ring_idx];
120                         rxq->vnic = vnic;
121                 }
122                 if (i == 0) {
123                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB) {
124                                 bp->eth_dev->data->promiscuous = 1;
125                                 vnic->flags |= BNXT_VNIC_INFO_PROMISC;
126                         }
127                         vnic->func_default = true;
128                 }
129                 vnic->ff_pool_idx = i;
130                 vnic->start_grp_id = start_grp_id;
131                 vnic->end_grp_id = end_grp_id;
132
133                 if (i) {
134                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB ||
135                             !(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS))
136                                 vnic->rss_dflt_cr = true;
137                         goto skip_filter_allocation;
138                 }
139                 filter = bnxt_alloc_filter(bp);
140                 if (!filter) {
141                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
142                         rc = -ENOMEM;
143                         goto err_out;
144                 }
145                 /*
146                  * TODO: Configure & associate CFA rule for
147                  * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
148                  */
149                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
150
151 skip_filter_allocation:
152                 start_grp_id = end_grp_id;
153                 end_grp_id += nb_q_per_grp;
154         }
155
156 out:
157         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
158                 struct rte_eth_rss_conf *rss = &dev_conf->rx_adv_conf.rss_conf;
159                 uint16_t hash_type = 0;
160
161                 if (bp->flags & BNXT_FLAG_UPDATE_HASH) {
162                         rss = &bp->rss_conf;
163                         bp->flags &= ~BNXT_FLAG_UPDATE_HASH;
164                 }
165
166                 if (rss->rss_hf & ETH_RSS_IPV4)
167                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
168                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
169                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
170                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
171                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
172                 if (rss->rss_hf & ETH_RSS_IPV6)
173                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
174                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP)
175                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
176                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV6_UDP)
177                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
178
179                 for (i = 0; i < bp->nr_vnics; i++) {
180                         STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
181                         vnic->hash_type = hash_type;
182
183                         /*
184                          * Use the supplied key if the key length is
185                          * acceptable and the rss_key is not NULL
186                          */
187                         if (rss->rss_key &&
188                             rss->rss_key_len <= HW_HASH_KEY_SIZE)
189                                 memcpy(vnic->rss_hash_key,
190                                        rss->rss_key, rss->rss_key_len);
191                         }
192                 }
193         }
194
195         return rc;
196
197 err_out:
198         /* Free allocated vnic/filters */
199
200         return rc;
201 }
202
203 static void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
204 {
205         struct bnxt_sw_rx_bd *sw_ring;
206         struct bnxt_tpa_info *tpa_info;
207         uint16_t i;
208
209         if (rxq) {
210                 sw_ring = rxq->rx_ring->rx_buf_ring;
211                 if (sw_ring) {
212                         for (i = 0; i < rxq->nb_rx_desc; i++) {
213                                 if (sw_ring[i].mbuf) {
214                                         rte_pktmbuf_free_seg(sw_ring[i].mbuf);
215                                         sw_ring[i].mbuf = NULL;
216                                 }
217                         }
218                 }
219                 /* Free up mbufs in Agg ring */
220                 sw_ring = rxq->rx_ring->ag_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
230                 /* Free up mbufs in TPA */
231                 tpa_info = rxq->rx_ring->tpa_info;
232                 if (tpa_info) {
233                         for (i = 0; i < BNXT_TPA_MAX; i++) {
234                                 if (tpa_info[i].mbuf) {
235                                         rte_pktmbuf_free_seg(tpa_info[i].mbuf);
236                                         tpa_info[i].mbuf = NULL;
237                                 }
238                         }
239                 }
240         }
241 }
242
243 void bnxt_free_rx_mbufs(struct bnxt *bp)
244 {
245         struct bnxt_rx_queue *rxq;
246         int i;
247
248         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
249                 rxq = bp->rx_queues[i];
250                 bnxt_rx_queue_release_mbufs(rxq);
251         }
252 }
253
254 void bnxt_rx_queue_release_op(void *rx_queue)
255 {
256         struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
257
258         if (rxq) {
259                 bnxt_rx_queue_release_mbufs(rxq);
260
261                 /* Free RX ring hardware descriptors */
262                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
263                 /* Free RX Agg ring hardware descriptors */
264                 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
265
266                 /* Free RX completion ring hardware descriptors */
267                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
268
269                 bnxt_free_rxq_stats(rxq);
270                 rte_memzone_free(rxq->mz);
271                 rxq->mz = NULL;
272
273                 rte_free(rxq);
274         }
275 }
276
277 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
278                                uint16_t queue_idx,
279                                uint16_t nb_desc,
280                                unsigned int socket_id,
281                                const struct rte_eth_rxconf *rx_conf,
282                                struct rte_mempool *mp)
283 {
284         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
285         uint64_t rx_offloads = eth_dev->data->dev_conf.rxmode.offloads;
286         struct bnxt_rx_queue *rxq;
287         int rc = 0;
288
289         if (queue_idx >= bp->max_rx_rings) {
290                 PMD_DRV_LOG(ERR,
291                         "Cannot create Rx ring %d. Only %d rings available\n",
292                         queue_idx, bp->max_rx_rings);
293                 return -EINVAL;
294         }
295
296         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
297                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
298                 rc = -EINVAL;
299                 goto out;
300         }
301
302         if (eth_dev->data->rx_queues) {
303                 rxq = eth_dev->data->rx_queues[queue_idx];
304                 if (rxq)
305                         bnxt_rx_queue_release_op(rxq);
306         }
307         rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
308                                  RTE_CACHE_LINE_SIZE, socket_id);
309         if (!rxq) {
310                 PMD_DRV_LOG(ERR, "bnxt_rx_queue allocation failed!\n");
311                 rc = -ENOMEM;
312                 goto out;
313         }
314         rxq->bp = bp;
315         rxq->mb_pool = mp;
316         rxq->nb_rx_desc = nb_desc;
317         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
318
319         PMD_DRV_LOG(DEBUG, "RX Buf size is %d\n", rxq->rx_buf_use_size);
320         PMD_DRV_LOG(DEBUG, "RX Buf MTU %d\n", eth_dev->data->mtu);
321
322         rc = bnxt_init_rx_ring_struct(rxq, socket_id);
323         if (rc)
324                 goto out;
325
326         rxq->queue_id = queue_idx;
327         rxq->port_id = eth_dev->data->port_id;
328         rxq->crc_len = rx_offloads & DEV_RX_OFFLOAD_CRC_STRIP ?
329                 0 : ETHER_CRC_LEN;
330
331         eth_dev->data->rx_queues[queue_idx] = rxq;
332         /* Allocate RX ring hardware descriptors */
333         if (bnxt_alloc_rings(bp, queue_idx, NULL, rxq, rxq->cp_ring,
334                         "rxr")) {
335                 PMD_DRV_LOG(ERR,
336                         "ring_dma_zone_reserve for rx_ring failed!\n");
337                 bnxt_rx_queue_release_op(rxq);
338                 rc = -ENOMEM;
339                 goto out;
340         }
341         rte_atomic64_init(&rxq->rx_mbuf_alloc_fail);
342
343 out:
344         return rc;
345 }
346
347 int
348 bnxt_rx_queue_intr_enable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
349 {
350         struct bnxt_rx_queue *rxq;
351         struct bnxt_cp_ring_info *cpr;
352         int rc = 0;
353
354         if (eth_dev->data->rx_queues) {
355                 rxq = eth_dev->data->rx_queues[queue_id];
356                 if (!rxq) {
357                         rc = -EINVAL;
358                         return rc;
359                 }
360                 cpr = rxq->cp_ring;
361                 B_CP_DB_ARM(cpr);
362         }
363         return rc;
364 }
365
366 int
367 bnxt_rx_queue_intr_disable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
368 {
369         struct bnxt_rx_queue *rxq;
370         struct bnxt_cp_ring_info *cpr;
371         int rc = 0;
372
373         if (eth_dev->data->rx_queues) {
374                 rxq = eth_dev->data->rx_queues[queue_id];
375                 if (!rxq) {
376                         rc = -EINVAL;
377                         return rc;
378                 }
379                 cpr = rxq->cp_ring;
380                 B_CP_DB_DISARM(cpr);
381         }
382         return rc;
383 }
384
385 int bnxt_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
386 {
387         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
388         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
389         struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
390         struct bnxt_vnic_info *vnic = NULL;
391
392         if (rxq == NULL) {
393                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
394                 return -EINVAL;
395         }
396
397         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
398         rxq->rx_deferred_start = false;
399         PMD_DRV_LOG(INFO, "Rx queue started %d\n", rx_queue_id);
400         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
401                 vnic = rxq->vnic;
402                 if (vnic->fw_grp_ids[rx_queue_id] != INVALID_HW_RING_ID)
403                         return 0;
404                 PMD_DRV_LOG(DEBUG, "vnic = %p fw_grp_id = %d\n",
405                         vnic, bp->grp_info[rx_queue_id + 1].fw_grp_id);
406                 vnic->fw_grp_ids[rx_queue_id] =
407                                         bp->grp_info[rx_queue_id + 1].fw_grp_id;
408                 return bnxt_vnic_rss_configure(bp, vnic);
409         }
410
411         return 0;
412 }
413
414 int bnxt_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
415 {
416         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
417         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
418         struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
419         struct bnxt_vnic_info *vnic = NULL;
420
421         if (rxq == NULL) {
422                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
423                 return -EINVAL;
424         }
425
426         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
427         rxq->rx_deferred_start = true;
428         PMD_DRV_LOG(DEBUG, "Rx queue stopped\n");
429
430         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
431                 vnic = rxq->vnic;
432                 vnic->fw_grp_ids[rx_queue_id] = INVALID_HW_RING_ID;
433                 return bnxt_vnic_rss_configure(bp, vnic);
434         }
435         return 0;
436 }