866fb56b183984cba2501a2fbd69ff0d187a2a09
[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         if (rxq && rxq->cp_ring && rxq->cp_ring->hw_stats)
27                 rxq->cp_ring->hw_stats = NULL;
28 }
29
30 int bnxt_mq_rx_configure(struct bnxt *bp)
31 {
32         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
33         const struct rte_eth_vmdq_rx_conf *conf =
34                     &dev_conf->rx_adv_conf.vmdq_rx_conf;
35         unsigned int i, j, nb_q_per_grp = 1, ring_idx = 0;
36         int start_grp_id, end_grp_id = 1, rc = 0;
37         struct bnxt_vnic_info *vnic;
38         struct bnxt_filter_info *filter;
39         enum rte_eth_nb_pools pools = bp->rx_cp_nr_rings, max_pools = 0;
40         struct bnxt_rx_queue *rxq;
41
42         bp->nr_vnics = 0;
43
44         /* Single queue mode */
45         if (bp->rx_cp_nr_rings < 2) {
46                 vnic = bnxt_alloc_vnic(bp);
47                 if (!vnic) {
48                         PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
49                         rc = -ENOMEM;
50                         goto err_out;
51                 }
52                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
53                 STAILQ_INSERT_TAIL(&bp->ff_pool[0], vnic, next);
54                 bp->nr_vnics++;
55
56                 rxq = bp->eth_dev->data->rx_queues[0];
57                 rxq->vnic = vnic;
58
59                 vnic->func_default = true;
60                 vnic->ff_pool_idx = 0;
61                 vnic->start_grp_id = 0;
62                 vnic->end_grp_id = vnic->start_grp_id;
63                 filter = bnxt_alloc_filter(bp);
64                 if (!filter) {
65                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
66                         rc = -ENOMEM;
67                         goto err_out;
68                 }
69                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
70                 goto out;
71         }
72
73         /* Multi-queue mode */
74         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB_RSS) {
75                 /* VMDq ONLY, VMDq+RSS, VMDq+DCB, VMDq+DCB+RSS */
76
77                 switch (dev_conf->rxmode.mq_mode) {
78                 case ETH_MQ_RX_VMDQ_RSS:
79                 case ETH_MQ_RX_VMDQ_ONLY:
80                         /* ETH_8/64_POOLs */
81                         pools = conf->nb_queue_pools;
82                         /* For each pool, allocate MACVLAN CFA rule & VNIC */
83                         max_pools = RTE_MIN(bp->max_vnics,
84                                             RTE_MIN(bp->max_l2_ctx,
85                                             RTE_MIN(bp->max_rsscos_ctx,
86                                                     ETH_64_POOLS)));
87                         if (pools > max_pools)
88                                 pools = max_pools;
89                         break;
90                 case ETH_MQ_RX_RSS:
91                         pools = 1;
92                         break;
93                 default:
94                         PMD_DRV_LOG(ERR, "Unsupported mq_mod %d\n",
95                                 dev_conf->rxmode.mq_mode);
96                         rc = -EINVAL;
97                         goto err_out;
98                 }
99         }
100
101         nb_q_per_grp = bp->rx_cp_nr_rings / pools;
102         start_grp_id = 0;
103         end_grp_id = nb_q_per_grp;
104
105         for (i = 0; i < pools; i++) {
106                 vnic = bnxt_alloc_vnic(bp);
107                 if (!vnic) {
108                         PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
109                         rc = -ENOMEM;
110                         goto err_out;
111                 }
112                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
113                 STAILQ_INSERT_TAIL(&bp->ff_pool[i], vnic, next);
114                 bp->nr_vnics++;
115
116                 for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
117                         rxq = bp->eth_dev->data->rx_queues[ring_idx];
118                         rxq->vnic = vnic;
119                 }
120                 if (i == 0) {
121                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB) {
122                                 bp->eth_dev->data->promiscuous = 1;
123                                 vnic->flags |= BNXT_VNIC_INFO_PROMISC;
124                         }
125                         vnic->func_default = true;
126                 }
127                 vnic->ff_pool_idx = i;
128                 vnic->start_grp_id = start_grp_id;
129                 vnic->end_grp_id = end_grp_id;
130
131                 if (i) {
132                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB ||
133                             !(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS))
134                                 vnic->rss_dflt_cr = true;
135                         goto skip_filter_allocation;
136                 }
137                 filter = bnxt_alloc_filter(bp);
138                 if (!filter) {
139                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
140                         rc = -ENOMEM;
141                         goto err_out;
142                 }
143                 /*
144                  * TODO: Configure & associate CFA rule for
145                  * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
146                  */
147                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
148
149 skip_filter_allocation:
150                 start_grp_id = end_grp_id;
151                 end_grp_id += nb_q_per_grp;
152         }
153
154 out:
155         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
156                 struct rte_eth_rss_conf *rss = &dev_conf->rx_adv_conf.rss_conf;
157                 uint16_t hash_type = 0;
158
159                 if (bp->flags & BNXT_FLAG_UPDATE_HASH) {
160                         rss = &bp->rss_conf;
161                         bp->flags &= ~BNXT_FLAG_UPDATE_HASH;
162                 }
163
164                 if (rss->rss_hf & ETH_RSS_IPV4)
165                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
166                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
167                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
168                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
169                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
170                 if (rss->rss_hf & ETH_RSS_IPV6)
171                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
172                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP)
173                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
174                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV6_UDP)
175                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
176
177                 for (i = 0; i < bp->nr_vnics; i++) {
178                         STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
179                         vnic->hash_type = hash_type;
180
181                         /*
182                          * Use the supplied key if the key length is
183                          * acceptable and the rss_key is not NULL
184                          */
185                         if (rss->rss_key &&
186                             rss->rss_key_len <= HW_HASH_KEY_SIZE)
187                                 memcpy(vnic->rss_hash_key,
188                                        rss->rss_key, rss->rss_key_len);
189                         }
190                 }
191         }
192
193         return rc;
194
195 err_out:
196         /* Free allocated vnic/filters */
197
198         return rc;
199 }
200
201 static void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
202 {
203         struct bnxt_sw_rx_bd *sw_ring;
204         struct bnxt_tpa_info *tpa_info;
205         uint16_t i;
206
207         if (rxq) {
208                 sw_ring = rxq->rx_ring->rx_buf_ring;
209                 if (sw_ring) {
210                         for (i = 0;
211                              i < rxq->rx_ring->rx_ring_struct->ring_size; i++) {
212                                 if (sw_ring[i].mbuf) {
213                                         rte_pktmbuf_free_seg(sw_ring[i].mbuf);
214                                         sw_ring[i].mbuf = NULL;
215                                 }
216                         }
217                 }
218                 /* Free up mbufs in Agg ring */
219                 sw_ring = rxq->rx_ring->ag_buf_ring;
220                 if (sw_ring) {
221                         for (i = 0;
222                              i < rxq->rx_ring->ag_ring_struct->ring_size; 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 }