net/bnxt: add 100G speed detection
[dpdk.git] / drivers / net / bnxt / bnxt_rxq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) Broadcom Limited.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Broadcom Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <inttypes.h>
35
36 #include <rte_malloc.h>
37
38 #include "bnxt.h"
39 #include "bnxt_cpr.h"
40 #include "bnxt_filter.h"
41 #include "bnxt_hwrm.h"
42 #include "bnxt_ring.h"
43 #include "bnxt_rxq.h"
44 #include "bnxt_rxr.h"
45 #include "bnxt_vnic.h"
46 #include "hsi_struct_def_dpdk.h"
47
48 /*
49  * RX Queues
50  */
51
52 void bnxt_free_rxq_stats(struct bnxt_rx_queue *rxq)
53 {
54         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
55
56         if (cpr->hw_stats)
57                 cpr->hw_stats = NULL;
58 }
59
60 int bnxt_mq_rx_configure(struct bnxt *bp)
61 {
62         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
63         const struct rte_eth_vmdq_rx_conf *conf =
64                     &dev_conf->rx_adv_conf.vmdq_rx_conf;
65         unsigned int i, j, nb_q_per_grp = 1, ring_idx = 0;
66         int start_grp_id, end_grp_id = 1, rc = 0;
67         struct bnxt_vnic_info *vnic;
68         struct bnxt_filter_info *filter;
69         enum rte_eth_nb_pools pools = bp->rx_cp_nr_rings, max_pools = 0;
70         struct bnxt_rx_queue *rxq;
71
72         bp->nr_vnics = 0;
73
74         /* Single queue mode */
75         if (bp->rx_cp_nr_rings < 2) {
76                 vnic = bnxt_alloc_vnic(bp);
77                 if (!vnic) {
78                         PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
79                         rc = -ENOMEM;
80                         goto err_out;
81                 }
82                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
83                 STAILQ_INSERT_TAIL(&bp->ff_pool[0], vnic, next);
84                 bp->nr_vnics++;
85
86                 rxq = bp->eth_dev->data->rx_queues[0];
87                 rxq->vnic = vnic;
88
89                 vnic->func_default = true;
90                 vnic->ff_pool_idx = 0;
91                 vnic->start_grp_id = 0;
92                 vnic->end_grp_id = vnic->start_grp_id;
93                 filter = bnxt_alloc_filter(bp);
94                 if (!filter) {
95                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
96                         rc = -ENOMEM;
97                         goto err_out;
98                 }
99                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
100                 goto out;
101         }
102
103         /* Multi-queue mode */
104         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB_RSS) {
105                 /* VMDq ONLY, VMDq+RSS, VMDq+DCB, VMDq+DCB+RSS */
106
107                 switch (dev_conf->rxmode.mq_mode) {
108                 case ETH_MQ_RX_VMDQ_RSS:
109                 case ETH_MQ_RX_VMDQ_ONLY:
110                         /* ETH_8/64_POOLs */
111                         pools = conf->nb_queue_pools;
112                         /* For each pool, allocate MACVLAN CFA rule & VNIC */
113                         max_pools = RTE_MIN(bp->max_vnics,
114                                             RTE_MIN(bp->max_l2_ctx,
115                                             RTE_MIN(bp->max_rsscos_ctx,
116                                                     ETH_64_POOLS)));
117                         if (pools > max_pools)
118                                 pools = max_pools;
119                         break;
120                 case ETH_MQ_RX_RSS:
121                         pools = bp->rx_cp_nr_rings;
122                         break;
123                 default:
124                         PMD_DRV_LOG(ERR, "Unsupported mq_mod %d\n",
125                                 dev_conf->rxmode.mq_mode);
126                         rc = -EINVAL;
127                         goto err_out;
128                 }
129         }
130
131         nb_q_per_grp = bp->rx_cp_nr_rings / pools;
132         start_grp_id = 0;
133         end_grp_id = nb_q_per_grp;
134
135         for (i = 0; i < pools; i++) {
136                 vnic = bnxt_alloc_vnic(bp);
137                 if (!vnic) {
138                         PMD_DRV_LOG(ERR, "VNIC alloc failed\n");
139                         rc = -ENOMEM;
140                         goto err_out;
141                 }
142                 vnic->flags |= BNXT_VNIC_INFO_BCAST;
143                 STAILQ_INSERT_TAIL(&bp->ff_pool[i], vnic, next);
144                 bp->nr_vnics++;
145
146                 for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
147                         rxq = bp->eth_dev->data->rx_queues[ring_idx];
148                         rxq->vnic = vnic;
149                 }
150                 if (i == 0) {
151                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB) {
152                                 bp->eth_dev->data->promiscuous = 1;
153                                 vnic->flags |= BNXT_VNIC_INFO_PROMISC;
154                         }
155                         vnic->func_default = true;
156                 }
157                 vnic->ff_pool_idx = i;
158                 vnic->start_grp_id = start_grp_id;
159                 vnic->end_grp_id = end_grp_id;
160
161                 if (i) {
162                         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_DCB ||
163                             !(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS))
164                                 vnic->rss_dflt_cr = true;
165                         goto skip_filter_allocation;
166                 }
167                 filter = bnxt_alloc_filter(bp);
168                 if (!filter) {
169                         PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
170                         rc = -ENOMEM;
171                         goto err_out;
172                 }
173                 /*
174                  * TODO: Configure & associate CFA rule for
175                  * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
176                  */
177                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
178
179 skip_filter_allocation:
180                 start_grp_id = end_grp_id;
181                 end_grp_id += nb_q_per_grp;
182         }
183
184 out:
185         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
186                 struct rte_eth_rss_conf *rss = &dev_conf->rx_adv_conf.rss_conf;
187                 uint16_t hash_type = 0;
188
189                 if (bp->flags & BNXT_FLAG_UPDATE_HASH) {
190                         rss = &bp->rss_conf;
191                         bp->flags &= ~BNXT_FLAG_UPDATE_HASH;
192                 }
193
194                 if (rss->rss_hf & ETH_RSS_IPV4)
195                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
196                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
197                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
198                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
199                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
200                 if (rss->rss_hf & ETH_RSS_IPV6)
201                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
202                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP)
203                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
204                 if (rss->rss_hf & ETH_RSS_NONFRAG_IPV6_UDP)
205                         hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
206
207                 for (i = 0; i < bp->nr_vnics; i++) {
208                         STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
209                         vnic->hash_type = hash_type;
210
211                         /*
212                          * Use the supplied key if the key length is
213                          * acceptable and the rss_key is not NULL
214                          */
215                         if (rss->rss_key &&
216                             rss->rss_key_len <= HW_HASH_KEY_SIZE)
217                                 memcpy(vnic->rss_hash_key,
218                                        rss->rss_key, rss->rss_key_len);
219                         }
220                 }
221         }
222
223         return rc;
224
225 err_out:
226         /* Free allocated vnic/filters */
227
228         return rc;
229 }
230
231 static void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
232 {
233         struct bnxt_sw_rx_bd *sw_ring;
234         struct bnxt_tpa_info *tpa_info;
235         uint16_t i;
236
237         if (rxq) {
238                 sw_ring = rxq->rx_ring->rx_buf_ring;
239                 if (sw_ring) {
240                         for (i = 0; i < rxq->nb_rx_desc; i++) {
241                                 if (sw_ring[i].mbuf) {
242                                         rte_pktmbuf_free_seg(sw_ring[i].mbuf);
243                                         sw_ring[i].mbuf = NULL;
244                                 }
245                         }
246                 }
247                 /* Free up mbufs in Agg ring */
248                 sw_ring = rxq->rx_ring->ag_buf_ring;
249                 if (sw_ring) {
250                         for (i = 0; i < rxq->nb_rx_desc; i++) {
251                                 if (sw_ring[i].mbuf) {
252                                         rte_pktmbuf_free_seg(sw_ring[i].mbuf);
253                                         sw_ring[i].mbuf = NULL;
254                                 }
255                         }
256                 }
257
258                 /* Free up mbufs in TPA */
259                 tpa_info = rxq->rx_ring->tpa_info;
260                 if (tpa_info) {
261                         for (i = 0; i < BNXT_TPA_MAX; i++) {
262                                 if (tpa_info[i].mbuf) {
263                                         rte_pktmbuf_free_seg(tpa_info[i].mbuf);
264                                         tpa_info[i].mbuf = NULL;
265                                 }
266                         }
267                 }
268         }
269 }
270
271 void bnxt_free_rx_mbufs(struct bnxt *bp)
272 {
273         struct bnxt_rx_queue *rxq;
274         int i;
275
276         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
277                 rxq = bp->rx_queues[i];
278                 bnxt_rx_queue_release_mbufs(rxq);
279         }
280 }
281
282 void bnxt_rx_queue_release_op(void *rx_queue)
283 {
284         struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
285
286         if (rxq) {
287                 bnxt_rx_queue_release_mbufs(rxq);
288
289                 /* Free RX ring hardware descriptors */
290                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
291                 /* Free RX Agg ring hardware descriptors */
292                 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
293
294                 /* Free RX completion ring hardware descriptors */
295                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
296
297                 bnxt_free_rxq_stats(rxq);
298
299                 rte_free(rxq);
300         }
301 }
302
303 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
304                                uint16_t queue_idx,
305                                uint16_t nb_desc,
306                                unsigned int socket_id,
307                                const struct rte_eth_rxconf *rx_conf,
308                                struct rte_mempool *mp)
309 {
310         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
311         struct bnxt_rx_queue *rxq;
312         int rc = 0;
313
314         if (queue_idx >= bp->max_rx_rings) {
315                 PMD_DRV_LOG(ERR,
316                         "Cannot create Rx ring %d. Only %d rings available\n",
317                         queue_idx, bp->max_rx_rings);
318                 return -ENOSPC;
319         }
320
321         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
322                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
323                 rc = -EINVAL;
324                 goto out;
325         }
326
327         if (eth_dev->data->rx_queues) {
328                 rxq = eth_dev->data->rx_queues[queue_idx];
329                 if (rxq)
330                         bnxt_rx_queue_release_op(rxq);
331         }
332         rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
333                                  RTE_CACHE_LINE_SIZE, socket_id);
334         if (!rxq) {
335                 PMD_DRV_LOG(ERR, "bnxt_rx_queue allocation failed!\n");
336                 rc = -ENOMEM;
337                 goto out;
338         }
339         rxq->bp = bp;
340         rxq->mb_pool = mp;
341         rxq->nb_rx_desc = nb_desc;
342         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
343
344         PMD_DRV_LOG(DEBUG, "RX Buf size is %d\n", rxq->rx_buf_use_size);
345         PMD_DRV_LOG(DEBUG, "RX Buf MTU %d\n", eth_dev->data->mtu);
346
347         rc = bnxt_init_rx_ring_struct(rxq, socket_id);
348         if (rc)
349                 goto out;
350
351         rxq->queue_id = queue_idx;
352         rxq->port_id = eth_dev->data->port_id;
353         rxq->crc_len = (uint8_t)((eth_dev->data->dev_conf.rxmode.hw_strip_crc) ?
354                                 0 : ETHER_CRC_LEN);
355
356         eth_dev->data->rx_queues[queue_idx] = rxq;
357         /* Allocate RX ring hardware descriptors */
358         if (bnxt_alloc_rings(bp, queue_idx, NULL, rxq->rx_ring, rxq->cp_ring,
359                         "rxr")) {
360                 PMD_DRV_LOG(ERR,
361                         "ring_dma_zone_reserve for rx_ring failed!\n");
362                 bnxt_rx_queue_release_op(rxq);
363                 rc = -ENOMEM;
364                 goto out;
365         }
366
367 out:
368         return rc;
369 }
370
371 int
372 bnxt_rx_queue_intr_enable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
373 {
374         struct bnxt_rx_queue *rxq;
375         struct bnxt_cp_ring_info *cpr;
376         int rc = 0;
377
378         if (eth_dev->data->rx_queues) {
379                 rxq = eth_dev->data->rx_queues[queue_id];
380                 if (!rxq) {
381                         rc = -EINVAL;
382                         return rc;
383                 }
384                 cpr = rxq->cp_ring;
385                 B_CP_DB_ARM(cpr);
386         }
387         return rc;
388 }
389
390 int
391 bnxt_rx_queue_intr_disable_op(struct rte_eth_dev *eth_dev, uint16_t queue_id)
392 {
393         struct bnxt_rx_queue *rxq;
394         struct bnxt_cp_ring_info *cpr;
395         int rc = 0;
396
397         if (eth_dev->data->rx_queues) {
398                 rxq = eth_dev->data->rx_queues[queue_id];
399                 if (!rxq) {
400                         rc = -EINVAL;
401                         return rc;
402                 }
403                 cpr = rxq->cp_ring;
404                 B_CP_DB_DISARM(cpr);
405         }
406         return rc;
407 }
408
409 int bnxt_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
410 {
411         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
412         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
413         struct bnxt_rx_queue *rxq = bp->rx_queues[rx_queue_id];
414         struct bnxt_vnic_info *vnic = NULL;
415
416         if (rxq == NULL) {
417                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
418                 return -EINVAL;
419         }
420
421         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
422         rxq->rx_deferred_start = false;
423         PMD_DRV_LOG(INFO, "Rx queue started %d\n", rx_queue_id);
424         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
425                 vnic = rxq->vnic;
426                 if (vnic->fw_grp_ids[rx_queue_id] != INVALID_HW_RING_ID)
427                         return 0;
428                 PMD_DRV_LOG(DEBUG, "vnic = %p fw_grp_id = %d\n",
429                         vnic, bp->grp_info[rx_queue_id + 1].fw_grp_id);
430                 vnic->fw_grp_ids[rx_queue_id] =
431                                         bp->grp_info[rx_queue_id + 1].fw_grp_id;
432                 return bnxt_vnic_rss_configure(bp, vnic);
433         }
434
435         return 0;
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_rx_queue *rxq = bp->rx_queues[rx_queue_id];
443         struct bnxt_vnic_info *vnic = NULL;
444
445         if (rxq == NULL) {
446                 PMD_DRV_LOG(ERR, "Invalid Rx queue %d\n", rx_queue_id);
447                 return -EINVAL;
448         }
449
450         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
451         rxq->rx_deferred_start = true;
452         PMD_DRV_LOG(DEBUG, "Rx queue stopped\n");
453
454         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
455                 vnic = rxq->vnic;
456                 vnic->fw_grp_ids[rx_queue_id] = INVALID_HW_RING_ID;
457                 return bnxt_vnic_rss_configure(bp, vnic);
458         }
459         return 0;
460 }