New upstream version 18.08
[deb_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         rxq->crc_len = rte_eth_dev_must_keep_crc(rx_offloads) ?
335                 ETHER_CRC_LEN : 0;
336
337         eth_dev->data->rx_queues[queue_idx] = rxq;
338         /* Allocate RX ring hardware descriptors */
339         if (bnxt_alloc_rings(bp, queue_idx, NULL, rxq, rxq->cp_ring,
340                         "rxr")) {
341                 PMD_DRV_LOG(ERR,
342                         "ring_dma_zone_reserve for rx_ring failed!\n");
343                 bnxt_rx_queue_release_op(rxq);
344                 rc = -ENOMEM;
345                 goto out;
346         }
347         rte_atomic64_init(&rxq->rx_mbuf_alloc_fail);
348
349         rxq->rx_deferred_start = rx_conf->rx_deferred_start;
350         queue_state = rxq->rx_deferred_start ? RTE_ETH_QUEUE_STATE_STOPPED :
351                                                 RTE_ETH_QUEUE_STATE_STARTED;
352         eth_dev->data->rx_queue_state[queue_idx] = queue_state;
353         rte_spinlock_init(&rxq->lock);
354 out:
355         return rc;
356 }
357
358 int
359 bnxt_rx_queue_intr_enable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
360 {
361         struct bnxt_rx_queue *rxq;
362         struct bnxt_cp_ring_info *cpr;
363         int rc = 0;
364
365         if (eth_dev->data->rx_queues) {
366                 rxq = eth_dev->data->rx_queues[queue_id];
367                 if (!rxq) {
368                         rc = -EINVAL;
369                         return rc;
370                 }
371                 cpr = rxq->cp_ring;
372                 B_CP_DB_ARM(cpr);
373         }
374         return rc;
375 }
376
377 int
378 bnxt_rx_queue_intr_disable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
379 {
380         struct bnxt_rx_queue *rxq;
381         struct bnxt_cp_ring_info *cpr;
382         int rc = 0;
383
384         if (eth_dev->data->rx_queues) {
385                 rxq = eth_dev->data->rx_queues[queue_id];
386                 if (!rxq) {
387                         rc = -EINVAL;
388                         return rc;
389                 }
390                 cpr = rxq->cp_ring;
391                 B_CP_DB_DISARM(cpr);
392         }
393         return rc;
394 }
395
396 int bnxt_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
397 {
398         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
399         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
400         struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
401         struct bnxt_vnic_info *vnic = NULL;
402         int rc = 0;
403
404         if (rxq == NULL) {
405                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
406                 return -EINVAL;
407         }
408
409         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
410
411         bnxt_free_hwrm_rx_ring(bp, rx_queue_id);
412         bnxt_alloc_hwrm_rx_ring(bp, rx_queue_id);
413         PMD_DRV_LOG(INFO, "Rx queue started %d\n", rx_queue_id);
414
415         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
416                 vnic = rxq->vnic;
417
418                 if (vnic->fw_grp_ids[rx_queue_id] != INVALID_HW_RING_ID)
419                         return 0;
420
421                 PMD_DRV_LOG(DEBUG,
422                             "vnic = %p fw_grp_id = %d\n",
423                             vnic, bp->grp_info[rx_queue_id].fw_grp_id);
424
425                 vnic->fw_grp_ids[rx_queue_id] =
426                                         bp->grp_info[rx_queue_id].fw_grp_id;
427                 rc = bnxt_vnic_rss_configure(bp, vnic);
428         }
429
430         if (rc == 0)
431                 rxq->rx_deferred_start = false;
432
433         return rc;
434 }
435
436 int bnxt_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
437 {
438         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
439         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
440         struct bnxt_vnic_info *vnic = NULL;
441         struct bnxt_rx_queue *rxq = NULL;
442         int rc = 0;
443
444         /* Rx CQ 0 also works as Default CQ for async notifications */
445         if (!rx_queue_id) {
446                 PMD_DRV_LOG(ERR, "Cannot stop Rx queue id %d\n", rx_queue_id);
447                 return -EINVAL;
448         }
449
450         rxq = bp->rx_queues[rx_queue_id];
451
452         if (rxq == NULL) {
453                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
454                 return -EINVAL;
455         }
456
457         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
458         rxq->rx_deferred_start = true;
459         PMD_DRV_LOG(DEBUG, "Rx queue stopped\n");
460
461         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
462                 vnic = rxq->vnic;
463                 vnic->fw_grp_ids[rx_queue_id] = INVALID_HW_RING_ID;
464                 rc = bnxt_vnic_rss_configure(bp, vnic);
465         }
466
467         if (rc == 0)
468                 bnxt_rx_queue_release_mbufs(rxq);
469
470         return rc;
471 }