net/bnxt: support CoS classification
[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 = &bp->vnic_info[0];
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                 bp->nr_vnics++;
54
55                 rxq = bp->eth_dev->data->rx_queues[0];
56                 rxq->vnic = vnic;
57
58                 vnic->func_default = true;
59                 vnic->start_grp_id = 0;
60                 vnic->end_grp_id = vnic->start_grp_id;
61                 filter = bnxt_alloc_filter(bp);
62                 if (!filter) {
63                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
64                         rc = -ENOMEM;
65                         goto err_out;
66                 }
67                 filter->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST;
68                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
69                 goto out;
70         }
71
72         /* Multi-queue mode */
73         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB_RSS) {
74                 /* VMDq ONLY, VMDq+RSS, VMDq+DCB, VMDq+DCB+RSS */
75
76                 switch (dev_conf->rxmode.mq_mode) {
77                 case ETH_MQ_RX_VMDQ_RSS:
78                 case ETH_MQ_RX_VMDQ_ONLY:
79                 case ETH_MQ_RX_VMDQ_DCB_RSS:
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                         PMD_DRV_LOG(DEBUG,
89                                     "pools = %u max_pools = %u\n",
90                                     pools, max_pools);
91                         if (pools > max_pools)
92                                 pools = max_pools;
93                         break;
94                 case ETH_MQ_RX_RSS:
95                         pools = bp->rx_cosq_cnt ? bp->rx_cosq_cnt : 1;
96                         break;
97                 default:
98                         PMD_DRV_LOG(ERR, "Unsupported mq_mod %d\n",
99                                 dev_conf->rxmode.mq_mode);
100                         rc = -EINVAL;
101                         goto err_out;
102                 }
103         }
104         nb_q_per_grp = bp->rx_cp_nr_rings / pools;
105         bp->rx_num_qs_per_vnic = nb_q_per_grp;
106         PMD_DRV_LOG(DEBUG, "pools = %u nb_q_per_grp = %u\n",
107                     pools, nb_q_per_grp);
108         start_grp_id = 0;
109         end_grp_id = nb_q_per_grp;
110
111         for (i = 0; i < pools; i++) {
112                 vnic = &bp->vnic_info[i];
113                 if (!vnic) {
114                         PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
115                         rc = -ENOMEM;
116                         goto err_out;
117                 }
118                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
119                 bp->nr_vnics++;
120
121                 for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
122                         rxq = bp->eth_dev->data->rx_queues[ring_idx];
123                         rxq->vnic = vnic;
124                         PMD_DRV_LOG(DEBUG,
125                                     "rxq[%d] = %p vnic[%d] = %p\n",
126                                     ring_idx, rxq, i, vnic);
127                 }
128                 if (i == 0) {
129                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB) {
130                                 bp->eth_dev->data->promiscuous = 1;
131                                 vnic->flags |= BNXT_VNIC_INFO_PROMISC;
132                         }
133                         vnic->func_default = true;
134                 }
135                 vnic->start_grp_id = start_grp_id;
136                 vnic->end_grp_id = end_grp_id;
137
138                 if (i) {
139                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB ||
140                             !(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS))
141                                 vnic->rss_dflt_cr = true;
142                         goto skip_filter_allocation;
143                 }
144                 filter = bnxt_alloc_filter(bp);
145                 if (!filter) {
146                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
147                         rc = -ENOMEM;
148                         goto err_out;
149                 }
150                 filter->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST;
151                 /*
152                  * TODO: Configure & associate CFA rule for
153                  * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
154                  */
155                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
156
157 skip_filter_allocation:
158                 start_grp_id = end_grp_id;
159                 end_grp_id += nb_q_per_grp;
160         }
161
162 out:
163         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
164                 struct rte_eth_rss_conf *rss = &dev_conf->rx_adv_conf.rss_conf;
165
166                 if (bp->flags & BNXT_FLAG_UPDATE_HASH) {
167                         rss = &bp->rss_conf;
168                         bp->flags &= ~BNXT_FLAG_UPDATE_HASH;
169                 }
170
171                 for (i = 0; i < bp->nr_vnics; i++) {
172                         vnic = &bp->vnic_info[i];
173                         vnic->hash_type =
174                                 bnxt_rte_to_hwrm_hash_types(rss->rss_hf);
175
176                         /*
177                          * Use the supplied key if the key length is
178                          * acceptable and the rss_key is not NULL
179                          */
180                         if (rss->rss_key &&
181                             rss->rss_key_len <= HW_HASH_KEY_SIZE)
182                                 memcpy(vnic->rss_hash_key,
183                                        rss->rss_key, rss->rss_key_len);
184                 }
185         }
186
187         return rc;
188
189 err_out:
190         /* Free allocated vnic/filters */
191
192         return rc;
193 }
194
195 void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
196 {
197         struct bnxt_sw_rx_bd *sw_ring;
198         struct bnxt_tpa_info *tpa_info;
199         uint16_t i;
200
201         if (!rxq)
202                 return;
203
204         rte_spinlock_lock(&rxq->lock);
205
206         sw_ring = rxq->rx_ring->rx_buf_ring;
207         if (sw_ring) {
208                 for (i = 0;
209                      i < rxq->rx_ring->rx_ring_struct->ring_size; i++) {
210                         if (sw_ring[i].mbuf) {
211                                 rte_pktmbuf_free_seg(sw_ring[i].mbuf);
212                                 sw_ring[i].mbuf = NULL;
213                         }
214                 }
215         }
216         /* Free up mbufs in Agg ring */
217         sw_ring = rxq->rx_ring->ag_buf_ring;
218         if (sw_ring) {
219                 for (i = 0;
220                      i < rxq->rx_ring->ag_ring_struct->ring_size; i++) {
221                         if (sw_ring[i].mbuf) {
222                                 rte_pktmbuf_free_seg(sw_ring[i].mbuf);
223                                 sw_ring[i].mbuf = NULL;
224                         }
225                 }
226         }
227
228         /* Free up mbufs in TPA */
229         tpa_info = rxq->rx_ring->tpa_info;
230         if (tpa_info) {
231                 int max_aggs = BNXT_TPA_MAX_AGGS(rxq->bp);
232
233                 for (i = 0; i < max_aggs; 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         rte_spinlock_unlock(&rxq->lock);
242 }
243
244 void bnxt_free_rx_mbufs(struct bnxt *bp)
245 {
246         struct bnxt_rx_queue *rxq;
247         int i;
248
249         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
250                 rxq = bp->rx_queues[i];
251                 bnxt_rx_queue_release_mbufs(rxq);
252         }
253 }
254
255 void bnxt_rx_queue_release_op(void *rx_queue)
256 {
257         struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
258
259         if (rxq) {
260                 if (is_bnxt_in_error(rxq->bp))
261                         return;
262
263                 bnxt_rx_queue_release_mbufs(rxq);
264
265                 /* Free RX ring hardware descriptors */
266                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
267                 /* Free RX Agg ring hardware descriptors */
268                 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
269
270                 /* Free RX completion ring hardware descriptors */
271                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
272
273                 bnxt_free_rxq_stats(rxq);
274                 rte_memzone_free(rxq->mz);
275                 rxq->mz = NULL;
276
277                 rte_free(rxq);
278         }
279 }
280
281 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
282                                uint16_t queue_idx,
283                                uint16_t nb_desc,
284                                unsigned int socket_id,
285                                const struct rte_eth_rxconf *rx_conf,
286                                struct rte_mempool *mp)
287 {
288         struct bnxt *bp = eth_dev->data->dev_private;
289         uint64_t rx_offloads = eth_dev->data->dev_conf.rxmode.offloads;
290         struct bnxt_rx_queue *rxq;
291         int rc = 0;
292         uint8_t queue_state;
293
294         rc = is_bnxt_in_error(bp);
295         if (rc)
296                 return rc;
297
298         if (queue_idx >= bp->max_rx_rings) {
299                 PMD_DRV_LOG(ERR,
300                         "Cannot create Rx ring %d. Only %d rings available\n",
301                         queue_idx, bp->max_rx_rings);
302                 return -EINVAL;
303         }
304
305         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
306                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
307                 rc = -EINVAL;
308                 goto out;
309         }
310
311         if (eth_dev->data->rx_queues) {
312                 rxq = eth_dev->data->rx_queues[queue_idx];
313                 if (rxq)
314                         bnxt_rx_queue_release_op(rxq);
315         }
316         rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
317                                  RTE_CACHE_LINE_SIZE, socket_id);
318         if (!rxq) {
319                 PMD_DRV_LOG(ERR, "bnxt_rx_queue allocation failed!\n");
320                 rc = -ENOMEM;
321                 goto out;
322         }
323         rxq->bp = bp;
324         rxq->mb_pool = mp;
325         rxq->nb_rx_desc = nb_desc;
326         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
327
328         PMD_DRV_LOG(DEBUG, "RX Buf MTU %d\n", eth_dev->data->mtu);
329
330         rc = bnxt_init_rx_ring_struct(rxq, socket_id);
331         if (rc)
332                 goto out;
333
334         PMD_DRV_LOG(DEBUG, "RX Buf size is %d\n", rxq->rx_buf_size);
335         rxq->queue_id = queue_idx;
336         rxq->port_id = eth_dev->data->port_id;
337         if (rx_offloads & DEV_RX_OFFLOAD_KEEP_CRC)
338                 rxq->crc_len = RTE_ETHER_CRC_LEN;
339         else
340                 rxq->crc_len = 0;
341
342         eth_dev->data->rx_queues[queue_idx] = rxq;
343         /* Allocate RX ring hardware descriptors */
344         if (bnxt_alloc_rings(bp, queue_idx, NULL, rxq, rxq->cp_ring,
345                         rxq->nq_ring, "rxr")) {
346                 PMD_DRV_LOG(ERR,
347                         "ring_dma_zone_reserve for rx_ring failed!\n");
348                 bnxt_rx_queue_release_op(rxq);
349                 rc = -ENOMEM;
350                 goto out;
351         }
352         rte_atomic64_init(&rxq->rx_mbuf_alloc_fail);
353
354         /* rxq 0 must not be stopped when used as async CPR */
355         if (!BNXT_NUM_ASYNC_CPR(bp) && queue_idx == 0)
356                 rxq->rx_deferred_start = false;
357         else
358                 rxq->rx_deferred_start = rx_conf->rx_deferred_start;
359
360         if (rxq->rx_deferred_start) {
361                 queue_state = RTE_ETH_QUEUE_STATE_STOPPED;
362                 rxq->rx_started = false;
363         } else {
364                 queue_state = RTE_ETH_QUEUE_STATE_STARTED;
365                 rxq->rx_started = true;
366         }
367         eth_dev->data->rx_queue_state[queue_idx] = queue_state;
368         rte_spinlock_init(&rxq->lock);
369
370 out:
371         return rc;
372 }
373
374 int
375 bnxt_rx_queue_intr_enable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
376 {
377         struct bnxt *bp = eth_dev->data->dev_private;
378         struct bnxt_rx_queue *rxq;
379         struct bnxt_cp_ring_info *cpr;
380         int rc = 0;
381
382         rc = is_bnxt_in_error(bp);
383         if (rc)
384                 return rc;
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_REARM(cpr, cpr->cp_raw_cons);
394         }
395         return rc;
396 }
397
398 int
399 bnxt_rx_queue_intr_disable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
400 {
401         struct bnxt *bp = eth_dev->data->dev_private;
402         struct bnxt_rx_queue *rxq;
403         struct bnxt_cp_ring_info *cpr;
404         int rc = 0;
405
406         rc = is_bnxt_in_error(bp);
407         if (rc)
408                 return rc;
409
410         if (eth_dev->data->rx_queues) {
411                 rxq = eth_dev->data->rx_queues[queue_id];
412                 if (!rxq) {
413                         rc = -EINVAL;
414                         return rc;
415                 }
416                 cpr = rxq->cp_ring;
417                 B_CP_DB_DISARM(cpr);
418         }
419         return rc;
420 }
421
422 int bnxt_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
423 {
424         struct bnxt *bp = dev->data->dev_private;
425         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
426         struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
427         struct bnxt_vnic_info *vnic = NULL;
428         int rc = 0;
429
430         rc = is_bnxt_in_error(bp);
431         if (rc)
432                 return rc;
433
434         if (rxq == NULL) {
435                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
436                 return -EINVAL;
437         }
438
439         /* Set the queue state to started here.
440          * We check the status of the queue while posting buffer.
441          * If queue is it started, we do not post buffers for Rx.
442          */
443         rxq->rx_started = true;
444         bnxt_free_hwrm_rx_ring(bp, rx_queue_id);
445         rc = bnxt_alloc_hwrm_rx_ring(bp, rx_queue_id);
446         if (rc)
447                 return rc;
448
449         PMD_DRV_LOG(INFO, "Rx queue started %d\n", rx_queue_id);
450
451         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
452                 vnic = rxq->vnic;
453
454                 if (BNXT_HAS_RING_GRPS(bp)) {
455                         if (vnic->fw_grp_ids[rx_queue_id] != INVALID_HW_RING_ID)
456                                 return 0;
457
458                         vnic->fw_grp_ids[rx_queue_id] =
459                                         bp->grp_info[rx_queue_id].fw_grp_id;
460                         PMD_DRV_LOG(DEBUG,
461                                     "vnic = %p fw_grp_id = %d\n",
462                                     vnic, bp->grp_info[rx_queue_id].fw_grp_id);
463                 }
464
465                 PMD_DRV_LOG(DEBUG, "Rx Queue Count %d\n", vnic->rx_queue_cnt);
466                 if (vnic->rx_queue_cnt > 1)
467                         rc = bnxt_vnic_rss_configure(bp, vnic);
468         }
469
470         if (rc == 0)
471                 dev->data->rx_queue_state[rx_queue_id] =
472                                 RTE_ETH_QUEUE_STATE_STARTED;
473         else
474                 rxq->rx_started = false;
475
476         PMD_DRV_LOG(INFO,
477                     "queue %d, rx_deferred_start %d, state %d!\n",
478                     rx_queue_id, rxq->rx_deferred_start,
479                     bp->eth_dev->data->rx_queue_state[rx_queue_id]);
480
481         return rc;
482 }
483
484 int bnxt_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
485 {
486         struct bnxt *bp = dev->data->dev_private;
487         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
488         struct bnxt_vnic_info *vnic = NULL;
489         struct bnxt_rx_queue *rxq = NULL;
490         int rc = 0;
491
492         rc = is_bnxt_in_error(bp);
493         if (rc)
494                 return rc;
495
496         /* For the stingray platform and other platforms needing tighter
497          * control of resource utilization, Rx CQ 0 also works as
498          * Default CQ for async notifications
499          */
500         if (!BNXT_NUM_ASYNC_CPR(bp) && !rx_queue_id) {
501                 PMD_DRV_LOG(ERR, "Cannot stop Rx queue id %d\n", rx_queue_id);
502                 return -EINVAL;
503         }
504
505         rxq = bp->rx_queues[rx_queue_id];
506
507         if (rxq == NULL) {
508                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
509                 return -EINVAL;
510         }
511
512         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
513         rxq->rx_started = false;
514         PMD_DRV_LOG(DEBUG, "Rx queue stopped\n");
515
516         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
517                 vnic = rxq->vnic;
518                 if (BNXT_HAS_RING_GRPS(bp))
519                         vnic->fw_grp_ids[rx_queue_id] = INVALID_HW_RING_ID;
520
521                 PMD_DRV_LOG(DEBUG, "Rx Queue Count %d\n", vnic->rx_queue_cnt);
522                 if (vnic->rx_queue_cnt > 1)
523                         rc = bnxt_vnic_rss_configure(bp, vnic);
524         }
525
526         if (rc == 0)
527                 bnxt_rx_queue_release_mbufs(rxq);
528
529         return rc;
530 }