19c0e3f6476def739e819521530a86f971f08833
[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_vnic.h"
45 #include "hsi_struct_def_dpdk.h"
46
47 /*
48  * RX Queues
49  */
50
51 void bnxt_free_rxq_stats(struct bnxt_rx_queue *rxq)
52 {
53         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
54
55         /* 'Unreserve' rte_memzone */
56
57         if (cpr->hw_stats)
58                 cpr->hw_stats = NULL;
59 }
60
61 int bnxt_mq_rx_configure(struct bnxt *bp)
62 {
63         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
64         unsigned int i, j, nb_q_per_grp, ring_idx;
65         int start_grp_id, end_grp_id, rc = 0;
66         struct bnxt_vnic_info *vnic;
67         struct bnxt_filter_info *filter;
68         struct bnxt_rx_queue *rxq;
69
70         bp->nr_vnics = 0;
71
72         /* Single queue mode */
73         if (bp->rx_cp_nr_rings < 2) {
74                 vnic = bnxt_alloc_vnic(bp);
75                 if (!vnic) {
76                         RTE_LOG(ERR, PMD, "VNIC alloc failed\n");
77                         rc = -ENOMEM;
78                         goto err_out;
79                 }
80                 STAILQ_INSERT_TAIL(&bp->ff_pool[0], vnic, next);
81                 bp->nr_vnics++;
82
83                 rxq = bp->eth_dev->data->rx_queues[0];
84                 rxq->vnic = vnic;
85
86                 vnic->func_default = true;
87                 vnic->ff_pool_idx = 0;
88                 vnic->start_grp_id = 1;
89                 vnic->end_grp_id = vnic->start_grp_id +
90                                    bp->rx_cp_nr_rings - 1;
91                 filter = bnxt_alloc_filter(bp);
92                 if (!filter) {
93                         RTE_LOG(ERR, PMD, "L2 filter alloc failed\n");
94                         rc = -ENOMEM;
95                         goto err_out;
96                 }
97                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
98                 goto out;
99         }
100
101         /* Multi-queue mode */
102         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_VMDQ_FLAG) {
103                 /* VMDq ONLY, VMDq+RSS, VMDq+DCB, VMDq+DCB+RSS */
104                 enum rte_eth_nb_pools pools;
105
106                 switch (dev_conf->rxmode.mq_mode) {
107                 case ETH_MQ_RX_VMDQ_RSS:
108                 case ETH_MQ_RX_VMDQ_ONLY:
109                         {
110                                 const struct rte_eth_vmdq_rx_conf *conf =
111                                     &dev_conf->rx_adv_conf.vmdq_rx_conf;
112
113                                 /* ETH_8/64_POOLs */
114                                 pools = conf->nb_queue_pools;
115                                 break;
116                         }
117                 default:
118                         RTE_LOG(ERR, PMD, "Unsupported mq_mod %d\n",
119                                 dev_conf->rxmode.mq_mode);
120                         rc = -EINVAL;
121                         goto err_out;
122                 }
123                 /* For each pool, allocate MACVLAN CFA rule & VNIC */
124                 if (!pools) {
125                         RTE_LOG(ERR, PMD,
126                                 "VMDq pool not set, defaulted to 64\n");
127                         pools = ETH_64_POOLS;
128                 }
129                 nb_q_per_grp = bp->rx_cp_nr_rings / pools;
130                 start_grp_id = 1;
131                 end_grp_id = start_grp_id + nb_q_per_grp - 1;
132
133                 ring_idx = 0;
134                 for (i = 0; i < pools; i++) {
135                         vnic = bnxt_alloc_vnic(bp);
136                         if (!vnic) {
137                                 RTE_LOG(ERR, PMD,
138                                         "VNIC alloc failed\n");
139                                 rc = -ENOMEM;
140                                 goto err_out;
141                         }
142                         STAILQ_INSERT_TAIL(&bp->ff_pool[i], vnic, next);
143                         bp->nr_vnics++;
144
145                         for (j = 0; j < nb_q_per_grp; j++, ring_idx++) {
146                                 rxq = bp->eth_dev->data->rx_queues[ring_idx];
147                                 rxq->vnic = vnic;
148                         }
149                         if (i == 0)
150                                 vnic->func_default = true;
151                         vnic->ff_pool_idx = i;
152                         vnic->start_grp_id = start_grp_id;
153                         vnic->end_grp_id = end_grp_id;
154
155                         filter = bnxt_alloc_filter(bp);
156                         if (!filter) {
157                                 RTE_LOG(ERR, PMD,
158                                         "L2 filter alloc failed\n");
159                                 rc = -ENOMEM;
160                                 goto err_out;
161                         }
162                         /*
163                          * TODO: Configure & associate CFA rule for
164                          * each VNIC for each VMDq with MACVLAN, MACVLAN+TC
165                          */
166                         STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
167
168                         start_grp_id = end_grp_id + 1;
169                         end_grp_id += nb_q_per_grp;
170                 }
171                 goto out;
172         }
173
174         /* Non-VMDq mode - RSS, DCB, RSS+DCB */
175         /* Init default VNIC for RSS or DCB only */
176         vnic = bnxt_alloc_vnic(bp);
177         if (!vnic) {
178                 RTE_LOG(ERR, PMD, "VNIC alloc failed\n");
179                 rc = -ENOMEM;
180                 goto err_out;
181         }
182         /* Partition the rx queues for the single pool */
183         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
184                 rxq = bp->eth_dev->data->rx_queues[i];
185                 rxq->vnic = vnic;
186         }
187         STAILQ_INSERT_TAIL(&bp->ff_pool[0], vnic, next);
188         bp->nr_vnics++;
189
190         vnic->func_default = true;
191         vnic->ff_pool_idx = 0;
192         vnic->start_grp_id = 1;
193         vnic->end_grp_id = vnic->start_grp_id +
194                            bp->rx_cp_nr_rings - 1;
195         filter = bnxt_alloc_filter(bp);
196         if (!filter) {
197                 RTE_LOG(ERR, PMD, "L2 filter alloc failed\n");
198                 rc = -ENOMEM;
199                 goto err_out;
200         }
201         STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
202
203         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
204                 vnic->hash_type =
205                         HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4 |
206                         HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
207
208 out:
209         return rc;
210
211 err_out:
212         /* Free allocated vnic/filters */
213
214         return rc;
215 }
216
217 static void bnxt_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq __rte_unused)
218 {
219         /* TODO: Requires interaction with TX ring */
220 }
221
222 void bnxt_free_rx_mbufs(struct bnxt *bp)
223 {
224         struct bnxt_rx_queue *rxq;
225         int i;
226
227         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
228                 rxq = bp->rx_queues[i];
229                 bnxt_rx_queue_release_mbufs(rxq);
230         }
231 }
232
233 void bnxt_rx_queue_release_op(void *rx_queue)
234 {
235         struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
236
237         if (rxq) {
238                 bnxt_rx_queue_release_mbufs(rxq);
239
240                 /* TODO: Free ring and stats here */
241
242                 rte_free(rxq);
243         }
244 }
245
246 int bnxt_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
247                                uint16_t queue_idx,
248                                uint16_t nb_desc,
249                                unsigned int socket_id,
250                                const struct rte_eth_rxconf *rx_conf,
251                                struct rte_mempool *mp)
252 {
253         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
254         struct bnxt_rx_queue *rxq;
255
256         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
257                 RTE_LOG(ERR, PMD, "nb_desc %d is invalid", nb_desc);
258                 return -EINVAL;
259         }
260
261         if (eth_dev->data->rx_queues) {
262                 rxq = eth_dev->data->rx_queues[queue_idx];
263                 if (rxq)
264                         bnxt_rx_queue_release_op(rxq);
265         }
266         rxq = rte_zmalloc_socket("bnxt_rx_queue", sizeof(struct bnxt_rx_queue),
267                                  RTE_CACHE_LINE_SIZE, socket_id);
268         if (!rxq) {
269                 RTE_LOG(ERR, PMD, "bnxt_rx_queue allocation failed!");
270                 return -ENOMEM;
271         }
272         rxq->bp = bp;
273         rxq->mb_pool = mp;
274         rxq->nb_rx_desc = nb_desc;
275         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
276
277         /* TODO: Initialize ring structure */
278
279         rxq->queue_id = queue_idx;
280         rxq->port_id = eth_dev->data->port_id;
281         rxq->crc_len = (uint8_t)((eth_dev->data->dev_conf.rxmode.hw_strip_crc) ?
282                                 0 : ETHER_CRC_LEN);
283
284         eth_dev->data->rx_queues[queue_idx] = rxq;
285         /* TODO: Allocate RX ring hardware descriptors */
286
287         return 0;
288 }