6d99137a97958de9ee51c6c355be376ab6626d19
[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                         /* FALLTHROUGH */
81                         /* ETH_8/64_POOLs */
82                         pools = conf->nb_queue_pools;
83                         /* For each pool, allocate MACVLAN CFA rule & VNIC */
84                         max_pools = RTE_MIN(bp->max_vnics,
85                                             RTE_MIN(bp->max_l2_ctx,
86                                             RTE_MIN(bp->max_rsscos_ctx,
87                                                     ETH_64_POOLS)));
88                         if (pools > max_pools)
89                                 pools = max_pools;
90                         break;
91                 case ETH_MQ_RX_RSS:
92                         pools = 1;
93                         break;
94                 default:
95                         PMD_DRV_LOG(ERR, "Unsupported mq_mod %d\n",
96                                 dev_conf->rxmode.mq_mode);
97                         rc = -EINVAL;
98                         goto err_out;
99                 }
100         }
101
102         nb_q_per_grp = bp->rx_cp_nr_rings / pools;
103         start_grp_id = 0;
104         end_grp_id = nb_q_per_grp;
105
106         for (i = 0; i < pools; i++) {
107                 vnic = bnxt_alloc_vnic(bp);
108                 if (!vnic) {
109                         PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
110                         rc = -ENOMEM;
111                         goto err_out;
112                 }
113                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
114                 STAILQ_INSERT_TAIL(&bp->ff_pool[i], vnic, next);
115                 bp->nr_vnics++;
116
117                 for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
118                         rxq = bp->eth_dev->data->rx_queues[ring_idx];
119                         rxq->vnic = vnic;
120                 }
121                 if (i == 0) {
122                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB) {
123                                 bp->eth_dev->data->promiscuous = 1;
124                                 vnic->flags |= BNXT_VNIC_INFO_PROMISC;
125                         }
126                         vnic->func_default = true;
127                 }
128                 vnic->ff_pool_idx = i;
129                 vnic->start_grp_id = start_grp_id;
130                 vnic->end_grp_id = end_grp_id;
131
132                 if (i) {
133                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB ||
134                             !(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS))
135                                 vnic->rss_dflt_cr = true;
136                         goto skip_filter_allocation;
137                 }
138                 filter = bnxt_alloc_filter(bp);
139                 if (!filter) {
140                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
141                         rc = -ENOMEM;
142                         goto err_out;
143                 }
144                 /*
145                  * TODO: Configure & associate CFA rule for
146                  * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
147                  */
148                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
149
150 skip_filter_allocation:
151                 start_grp_id = end_grp_id;
152                 end_grp_id += nb_q_per_grp;
153         }
154
155 out:
156         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
157                 struct rte_eth_rss_conf *rss = &dev_conf->rx_adv_conf.rss_conf;
158                 uint16_t hash_type = 0;
159
160                 if (bp->flags & BNXT_FLAG_UPDATE_HASH) {
161                         rss = &bp->rss_conf;
162                         bp->flags &= ~BNXT_FLAG_UPDATE_HASH;
163                 }
164
165                 if (rss->rss_hf & ETH_RSS_IPV4)
166                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
167                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
168                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
169                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
170                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
171                 if (rss->rss_hf & ETH_RSS_IPV6)
172                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
173                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP)
174                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
175                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV6_UDP)
176                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
177
178                 for (i = 0; i < bp->nr_vnics; i++) {
179                         STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
180                         vnic->hash_type = hash_type;
181
182                         /*
183                          * Use the supplied key if the key length is
184                          * acceptable and the rss_key is not NULL
185                          */
186                         if (rss->rss_key &&
187                             rss->rss_key_len <= HW_HASH_KEY_SIZE)
188                                 memcpy(vnic->rss_hash_key,
189                                        rss->rss_key, rss->rss_key_len);
190                         }
191                 }
192         }
193
194         return rc;
195
196 err_out:
197         /* Free allocated vnic/filters */
198
199         return rc;
200 }
201
202 void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
203 {
204         struct bnxt_sw_rx_bd *sw_ring;
205         struct bnxt_tpa_info *tpa_info;
206         uint16_t i;
207
208         rte_spinlock_lock(&rxq->lock);
209
210         if (rxq) {
211                 sw_ring = rxq->rx_ring->rx_buf_ring;
212                 if (sw_ring) {
213                         for (i = 0;
214                              i < rxq->rx_ring->rx_ring_struct->ring_size; i++) {
215                                 if (sw_ring[i].mbuf) {
216                                         rte_pktmbuf_free_seg(sw_ring[i].mbuf);
217                                         sw_ring[i].mbuf = NULL;
218                                 }
219                         }
220                 }
221                 /* Free up mbufs in Agg ring */
222                 sw_ring = rxq->rx_ring->ag_buf_ring;
223                 if (sw_ring) {
224                         for (i = 0;
225                              i < rxq->rx_ring->ag_ring_struct->ring_size; i++) {
226                                 if (sw_ring[i].mbuf) {
227                                         rte_pktmbuf_free_seg(sw_ring[i].mbuf);
228                                         sw_ring[i].mbuf = NULL;
229                                 }
230                         }
231                 }
232
233                 /* Free up mbufs in TPA */
234                 tpa_info = rxq->rx_ring->tpa_info;
235                 if (tpa_info) {
236                         for (i = 0; i < BNXT_TPA_MAX; i++) {
237                                 if (tpa_info[i].mbuf) {
238                                         rte_pktmbuf_free_seg(tpa_info[i].mbuf);
239                                         tpa_info[i].mbuf = NULL;
240                                 }
241                         }
242                 }
243         }
244
245         rte_spinlock_unlock(&rxq->lock);
246 }
247
248 void bnxt_free_rx_mbufs(struct bnxt *bp)
249 {
250         struct bnxt_rx_queue *rxq;
251         int i;
252
253         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
254                 rxq = bp->rx_queues[i];
255                 bnxt_rx_queue_release_mbufs(rxq);
256         }
257 }
258
259 void bnxt_rx_queue_release_op(void *rx_queue)
260 {
261         struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
262
263         if (rxq) {
264                 bnxt_rx_queue_release_mbufs(rxq);
265
266                 /* Free RX ring hardware descriptors */
267                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
268                 /* Free RX Agg ring hardware descriptors */
269                 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
270
271                 /* Free RX completion ring hardware descriptors */
272                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
273
274                 bnxt_free_rxq_stats(rxq);
275                 rte_memzone_free(rxq->mz);
276                 rxq->mz = NULL;
277
278                 rte_free(rxq);
279         }
280 }
281
282 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
283                                uint16_t queue_idx,
284                                uint16_t nb_desc,
285                                unsigned int socket_id,
286                                const struct rte_eth_rxconf *rx_conf,
287                                struct rte_mempool *mp)
288 {
289         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
290         uint64_t rx_offloads = eth_dev->data->dev_conf.rxmode.offloads;
291         struct bnxt_rx_queue *rxq;
292         int rc = 0;
293         uint8_t queue_state;
294
295         if (queue_idx >= bp->max_rx_rings) {
296                 PMD_DRV_LOG(ERR,
297                         "Cannot create Rx ring %d. Only %d rings available\n",
298                         queue_idx, bp->max_rx_rings);
299                 return -EINVAL;
300         }
301
302         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
303                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
304                 rc = -EINVAL;
305                 goto out;
306         }
307
308         if (eth_dev->data->rx_queues) {
309                 rxq = eth_dev->data->rx_queues[queue_idx];
310                 if (rxq)
311                         bnxt_rx_queue_release_op(rxq);
312         }
313         rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
314                                  RTE_CACHE_LINE_SIZE, socket_id);
315         if (!rxq) {
316                 PMD_DRV_LOG(ERR, "bnxt_rx_queue allocation failed!\n");
317                 rc = -ENOMEM;
318                 goto out;
319         }
320         rxq->bp = bp;
321         rxq->mb_pool = mp;
322         rxq->nb_rx_desc = nb_desc;
323         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
324
325         PMD_DRV_LOG(DEBUG, "RX Buf size is %d\n", rxq->rx_buf_use_size);
326         PMD_DRV_LOG(DEBUG, "RX Buf MTU %d\n", eth_dev->data->mtu);
327
328         rc = bnxt_init_rx_ring_struct(rxq, socket_id);
329         if (rc)
330                 goto out;
331
332         rxq->queue_id = queue_idx;
333         rxq->port_id = eth_dev->data->port_id;
334         if (rx_offloads & DEV_RX_OFFLOAD_KEEP_CRC)
335                 rxq->crc_len = ETHER_CRC_LEN;
336         else
337                 rxq->crc_len = 0;
338
339         eth_dev->data->rx_queues[queue_idx] = rxq;
340         /* Allocate RX ring hardware descriptors */
341         if (bnxt_alloc_rings(bp, queue_idx, NULL, rxq, rxq->cp_ring,
342                         "rxr")) {
343                 PMD_DRV_LOG(ERR,
344                         "ring_dma_zone_reserve for rx_ring failed!\n");
345                 bnxt_rx_queue_release_op(rxq);
346                 rc = -ENOMEM;
347                 goto out;
348         }
349         rte_atomic64_init(&rxq->rx_mbuf_alloc_fail);
350
351         rxq->rx_deferred_start = rx_conf->rx_deferred_start;
352         queue_state = rxq->rx_deferred_start ? RTE_ETH_QUEUE_STATE_STOPPED :
353                                                 RTE_ETH_QUEUE_STATE_STARTED;
354         eth_dev->data->rx_queue_state[queue_idx] = queue_state;
355         rte_spinlock_init(&rxq->lock);
356 out:
357         return rc;
358 }
359
360 int
361 bnxt_rx_queue_intr_enable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
362 {
363         struct bnxt_rx_queue *rxq;
364         struct bnxt_cp_ring_info *cpr;
365         int rc = 0;
366
367         if (eth_dev->data->rx_queues) {
368                 rxq = eth_dev->data->rx_queues[queue_id];
369                 if (!rxq) {
370                         rc = -EINVAL;
371                         return rc;
372                 }
373                 cpr = rxq->cp_ring;
374                 B_CP_DB_ARM(cpr);
375         }
376         return rc;
377 }
378
379 int
380 bnxt_rx_queue_intr_disable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
381 {
382         struct bnxt_rx_queue *rxq;
383         struct bnxt_cp_ring_info *cpr;
384         int rc = 0;
385
386         if (eth_dev->data->rx_queues) {
387                 rxq = eth_dev->data->rx_queues[queue_id];
388                 if (!rxq) {
389                         rc = -EINVAL;
390                         return rc;
391                 }
392                 cpr = rxq->cp_ring;
393                 B_CP_DB_DISARM(cpr);
394         }
395         return rc;
396 }
397
398 int bnxt_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
399 {
400         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
401         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
402         struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
403         struct bnxt_vnic_info *vnic = NULL;
404         int rc = 0;
405
406         if (rxq == NULL) {
407                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
408                 return -EINVAL;
409         }
410
411         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
412
413         bnxt_free_hwrm_rx_ring(bp, rx_queue_id);
414         bnxt_alloc_hwrm_rx_ring(bp, rx_queue_id);
415         PMD_DRV_LOG(INFO, "Rx queue started %d\n", rx_queue_id);
416
417         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
418                 vnic = rxq->vnic;
419
420                 if (vnic->fw_grp_ids[rx_queue_id] != INVALID_HW_RING_ID)
421                         return 0;
422
423                 PMD_DRV_LOG(DEBUG,
424                             "vnic = %p fw_grp_id = %d\n",
425                             vnic, bp->grp_info[rx_queue_id].fw_grp_id);
426
427                 vnic->fw_grp_ids[rx_queue_id] =
428                                         bp->grp_info[rx_queue_id].fw_grp_id;
429                 rc = bnxt_vnic_rss_configure(bp, vnic);
430         }
431
432         if (rc == 0)
433                 rxq->rx_deferred_start = false;
434
435         return rc;
436 }
437
438 int bnxt_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
439 {
440         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
441         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
442         struct bnxt_vnic_info *vnic = NULL;
443         struct bnxt_rx_queue *rxq = NULL;
444         int rc = 0;
445
446         /* Rx CQ 0 also works as Default CQ for async notifications */
447         if (!rx_queue_id) {
448                 PMD_DRV_LOG(ERR, "Cannot stop Rx queue id %d\n", rx_queue_id);
449                 return -EINVAL;
450         }
451
452         rxq = bp->rx_queues[rx_queue_id];
453
454         if (rxq == NULL) {
455                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
456                 return -EINVAL;
457         }
458
459         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
460         rxq->rx_deferred_start = true;
461         PMD_DRV_LOG(DEBUG, "Rx queue stopped\n");
462
463         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
464                 vnic = rxq->vnic;
465                 vnic->fw_grp_ids[rx_queue_id] = INVALID_HW_RING_ID;
466                 rc = bnxt_vnic_rss_configure(bp, vnic);
467         }
468
469         if (rc == 0)
470                 bnxt_rx_queue_release_mbufs(rxq);
471
472         return rc;
473 }