net/bnxt: switch to the new offload API
[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
271                 rte_free(rxq);
272         }
273 }
274
275 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
276                                uint16_t queue_idx,
277                                uint16_t nb_desc,
278                                unsigned int socket_id,
279                                const struct rte_eth_rxconf *rx_conf,
280                                struct rte_mempool *mp)
281 {
282         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
283         uint64_t rx_offloads = eth_dev->data->dev_conf.rxmode.offloads;
284         struct bnxt_rx_queue *rxq;
285         int rc = 0;
286
287         if (queue_idx >= bp->max_rx_rings) {
288                 PMD_DRV_LOG(ERR,
289                         "Cannot create Rx ring %d. Only %d rings available\n",
290                         queue_idx, bp->max_rx_rings);
291                 return -ENOSPC;
292         }
293
294         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
295                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
296                 rc = -EINVAL;
297                 goto out;
298         }
299
300         if (eth_dev->data->rx_queues) {
301                 rxq = eth_dev->data->rx_queues[queue_idx];
302                 if (rxq)
303                         bnxt_rx_queue_release_op(rxq);
304         }
305         rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
306                                  RTE_CACHE_LINE_SIZE, socket_id);
307         if (!rxq) {
308                 PMD_DRV_LOG(ERR, "bnxt_rx_queue allocation failed!\n");
309                 rc = -ENOMEM;
310                 goto out;
311         }
312         rxq->bp = bp;
313         rxq->mb_pool = mp;
314         rxq->nb_rx_desc = nb_desc;
315         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
316
317         PMD_DRV_LOG(DEBUG, "RX Buf size is %d\n", rxq->rx_buf_use_size);
318         PMD_DRV_LOG(DEBUG, "RX Buf MTU %d\n", eth_dev->data->mtu);
319
320         rc = bnxt_init_rx_ring_struct(rxq, socket_id);
321         if (rc)
322                 goto out;
323
324         rxq->queue_id = queue_idx;
325         rxq->port_id = eth_dev->data->port_id;
326         rxq->crc_len = rx_offloads & DEV_RX_OFFLOAD_CRC_STRIP ?
327                 0 : ETHER_CRC_LEN;
328
329         eth_dev->data->rx_queues[queue_idx] = rxq;
330         /* Allocate RX ring hardware descriptors */
331         if (bnxt_alloc_rings(bp, queue_idx, NULL, rxq->rx_ring, rxq->cp_ring,
332                         "rxr")) {
333                 PMD_DRV_LOG(ERR,
334                         "ring_dma_zone_reserve for rx_ring failed!\n");
335                 bnxt_rx_queue_release_op(rxq);
336                 rc = -ENOMEM;
337                 goto out;
338         }
339
340 out:
341         return rc;
342 }
343
344 int
345 bnxt_rx_queue_intr_enable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
346 {
347         struct bnxt_rx_queue *rxq;
348         struct bnxt_cp_ring_info *cpr;
349         int rc = 0;
350
351         if (eth_dev->data->rx_queues) {
352                 rxq = eth_dev->data->rx_queues[queue_id];
353                 if (!rxq) {
354                         rc = -EINVAL;
355                         return rc;
356                 }
357                 cpr = rxq->cp_ring;
358                 B_CP_DB_ARM(cpr);
359         }
360         return rc;
361 }
362
363 int
364 bnxt_rx_queue_intr_disable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
365 {
366         struct bnxt_rx_queue *rxq;
367         struct bnxt_cp_ring_info *cpr;
368         int rc = 0;
369
370         if (eth_dev->data->rx_queues) {
371                 rxq = eth_dev->data->rx_queues[queue_id];
372                 if (!rxq) {
373                         rc = -EINVAL;
374                         return rc;
375                 }
376                 cpr = rxq->cp_ring;
377                 B_CP_DB_DISARM(cpr);
378         }
379         return rc;
380 }
381
382 int bnxt_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
383 {
384         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
385         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
386         struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
387         struct bnxt_vnic_info *vnic = NULL;
388
389         if (rxq == NULL) {
390                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
391                 return -EINVAL;
392         }
393
394         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
395         rxq->rx_deferred_start = false;
396         PMD_DRV_LOG(INFO, "Rx queue started %d\n", rx_queue_id);
397         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
398                 vnic = rxq->vnic;
399                 if (vnic->fw_grp_ids[rx_queue_id] != INVALID_HW_RING_ID)
400                         return 0;
401                 PMD_DRV_LOG(DEBUG, "vnic = %p fw_grp_id = %d\n",
402                         vnic, bp->grp_info[rx_queue_id + 1].fw_grp_id);
403                 vnic->fw_grp_ids[rx_queue_id] =
404                                         bp->grp_info[rx_queue_id + 1].fw_grp_id;
405                 return bnxt_vnic_rss_configure(bp, vnic);
406         }
407
408         return 0;
409 }
410
411 int bnxt_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
412 {
413         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
414         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
415         struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
416         struct bnxt_vnic_info *vnic = NULL;
417
418         if (rxq == NULL) {
419                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
420                 return -EINVAL;
421         }
422
423         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
424         rxq->rx_deferred_start = true;
425         PMD_DRV_LOG(DEBUG, "Rx queue stopped\n");
426
427         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
428                 vnic = rxq->vnic;
429                 vnic->fw_grp_ids[rx_queue_id] = INVALID_HW_RING_ID;
430                 return bnxt_vnic_rss_configure(bp, vnic);
431         }
432         return 0;
433 }