4 * Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
5 * Copyright (c) 2016 NXP. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
17 * * Neither the name of Freescale Semiconductor, Inc 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.
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.
38 #include <rte_ethdev.h>
39 #include <rte_malloc.h>
40 #include <rte_memcpy.h>
41 #include <rte_string_fns.h>
42 #include <rte_cycles.h>
43 #include <rte_kvargs.h>
45 #include <rte_ethdev.h>
46 #include <rte_fslmc.h>
48 #include <fslmc_logs.h>
49 #include <fslmc_vfio.h>
50 #include <dpaa2_hw_pvt.h>
52 #include "dpaa2_ethdev.h"
54 static struct rte_dpaa2_driver rte_dpaa2_pmd;
57 dpaa2_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
59 struct dpaa2_dev_priv *priv = dev->data->dev_private;
61 PMD_INIT_FUNC_TRACE();
63 dev_info->if_index = priv->hw_id;
65 dev_info->max_rx_queues = (uint16_t)priv->nb_rx_queues;
66 dev_info->max_tx_queues = (uint16_t)priv->nb_tx_queues;
68 dev_info->speed_capa = ETH_LINK_SPEED_1G |
74 dpaa2_alloc_rx_tx_queues(struct rte_eth_dev *dev)
76 struct dpaa2_dev_priv *priv = dev->data->dev_private;
79 struct dpaa2_queue *mc_q, *mcq;
82 struct dpaa2_queue *dpaa2_q;
84 PMD_INIT_FUNC_TRACE();
86 tot_queues = priv->nb_rx_queues + priv->nb_tx_queues;
87 mc_q = rte_malloc(NULL, sizeof(struct dpaa2_queue) * tot_queues,
90 PMD_INIT_LOG(ERR, "malloc failed for rx/tx queues\n");
94 for (i = 0; i < priv->nb_rx_queues; i++) {
96 priv->rx_vq[i] = mc_q++;
97 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
98 dpaa2_q->q_storage = rte_malloc("dq_storage",
99 sizeof(struct queue_storage_info_t),
100 RTE_CACHE_LINE_SIZE);
101 if (!dpaa2_q->q_storage)
104 memset(dpaa2_q->q_storage, 0,
105 sizeof(struct queue_storage_info_t));
106 dpaa2_q->q_storage->dq_storage[0] = rte_malloc(NULL,
107 DPAA2_DQRR_RING_SIZE * sizeof(struct qbman_result),
108 RTE_CACHE_LINE_SIZE);
111 for (i = 0; i < priv->nb_tx_queues; i++) {
113 mc_q->flow_id = DPNI_NEW_FLOW_ID;
114 priv->tx_vq[i] = mc_q++;
118 for (dist_idx = 0; dist_idx < priv->nb_rx_queues; dist_idx++) {
119 mcq = (struct dpaa2_queue *)priv->rx_vq[vq_id];
120 mcq->tc_index = DPAA2_DEF_TC;
121 mcq->flow_id = dist_idx;
128 mc_q = priv->rx_vq[0];
130 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
131 rte_free(dpaa2_q->q_storage->dq_storage[0]);
132 rte_free(dpaa2_q->q_storage);
133 priv->rx_vq[i--] = NULL;
140 dpaa2_eth_dev_configure(struct rte_eth_dev *dev)
142 struct rte_eth_dev_data *data = dev->data;
143 struct rte_eth_conf *eth_conf = &data->dev_conf;
145 PMD_INIT_FUNC_TRACE();
147 /* Check for correct configuration */
148 if (eth_conf->rxmode.mq_mode != ETH_MQ_RX_RSS &&
149 data->nb_rx_queues > 1) {
150 PMD_INIT_LOG(ERR, "Distribution is not enabled, "
151 "but Rx queues more than 1\n");
158 /* Function to setup RX flow information. It contains traffic class ID,
159 * flow ID, destination configuration etc.
162 dpaa2_dev_rx_queue_setup(struct rte_eth_dev *dev,
163 uint16_t rx_queue_id,
164 uint16_t nb_rx_desc __rte_unused,
165 unsigned int socket_id __rte_unused,
166 const struct rte_eth_rxconf *rx_conf __rte_unused,
167 struct rte_mempool *mb_pool)
169 struct dpaa2_dev_priv *priv = dev->data->dev_private;
170 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
171 struct dpaa2_queue *dpaa2_q;
172 struct dpni_queue cfg;
177 PMD_INIT_FUNC_TRACE();
179 PMD_INIT_LOG(DEBUG, "dev =%p, queue =%d, pool = %p, conf =%p",
180 dev, rx_queue_id, mb_pool, rx_conf);
182 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[rx_queue_id];
183 dpaa2_q->mb_pool = mb_pool; /**< mbuf pool to populate RX ring. */
185 /*Get the tc id and flow id from given VQ id*/
186 flow_id = rx_queue_id;
187 memset(&cfg, 0, sizeof(struct dpni_queue));
189 options = options | DPNI_QUEUE_OPT_USER_CTX;
190 cfg.user_context = (uint64_t)(dpaa2_q);
192 ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_RX,
193 dpaa2_q->tc_index, flow_id, options, &cfg);
195 PMD_INIT_LOG(ERR, "Error in setting the rx flow: = %d\n", ret);
199 dev->data->rx_queues[rx_queue_id] = dpaa2_q;
204 dpaa2_dev_tx_queue_setup(struct rte_eth_dev *dev,
205 uint16_t tx_queue_id,
206 uint16_t nb_tx_desc __rte_unused,
207 unsigned int socket_id __rte_unused,
208 const struct rte_eth_txconf *tx_conf __rte_unused)
210 struct dpaa2_dev_priv *priv = dev->data->dev_private;
211 struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)
212 priv->tx_vq[tx_queue_id];
213 struct fsl_mc_io *dpni = priv->hw;
214 struct dpni_queue tx_conf_cfg;
215 struct dpni_queue tx_flow_cfg;
216 uint8_t options = 0, flow_id;
220 PMD_INIT_FUNC_TRACE();
222 /* Return if queue already configured */
223 if (dpaa2_q->flow_id != DPNI_NEW_FLOW_ID)
226 memset(&tx_conf_cfg, 0, sizeof(struct dpni_queue));
227 memset(&tx_flow_cfg, 0, sizeof(struct dpni_queue));
230 flow_id = tx_queue_id;
232 ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_TX,
233 tc_id, flow_id, options, &tx_flow_cfg);
235 PMD_INIT_LOG(ERR, "Error in setting the tx flow: "
236 "tc_id=%d, flow =%d ErrorCode = %x\n",
237 tc_id, flow_id, -ret);
241 dpaa2_q->flow_id = flow_id;
243 if (tx_queue_id == 0) {
244 /*Set tx-conf and error configuration*/
245 ret = dpni_set_tx_confirmation_mode(dpni, CMD_PRI_LOW,
249 PMD_INIT_LOG(ERR, "Error in set tx conf mode settings"
250 " ErrorCode = %x", ret);
254 dpaa2_q->tc_index = tc_id;
256 dev->data->tx_queues[tx_queue_id] = dpaa2_q;
261 dpaa2_dev_rx_queue_release(void *q __rte_unused)
263 PMD_INIT_FUNC_TRACE();
267 dpaa2_dev_tx_queue_release(void *q __rte_unused)
269 PMD_INIT_FUNC_TRACE();
273 dpaa2_dev_start(struct rte_eth_dev *dev)
275 struct rte_eth_dev_data *data = dev->data;
276 struct dpaa2_dev_priv *priv = data->dev_private;
277 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
278 struct dpni_queue cfg;
280 struct dpni_queue_id qid;
281 struct dpaa2_queue *dpaa2_q;
284 PMD_INIT_FUNC_TRACE();
286 ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
288 PMD_INIT_LOG(ERR, "Failure %d in enabling dpni %d device\n",
293 ret = dpni_get_qdid(dpni, CMD_PRI_LOW, priv->token,
294 DPNI_QUEUE_TX, &qdid);
296 PMD_INIT_LOG(ERR, "Error to get qdid:ErrorCode = %d\n", ret);
301 for (i = 0; i < data->nb_rx_queues; i++) {
302 dpaa2_q = (struct dpaa2_queue *)data->rx_queues[i];
303 ret = dpni_get_queue(dpni, CMD_PRI_LOW, priv->token,
304 DPNI_QUEUE_RX, dpaa2_q->tc_index,
305 dpaa2_q->flow_id, &cfg, &qid);
307 PMD_INIT_LOG(ERR, "Error to get flow "
308 "information Error code = %d\n", ret);
311 dpaa2_q->fqid = qid.fqid;
318 * This routine disables all traffic on the adapter by issuing a
319 * global reset on the MAC.
322 dpaa2_dev_stop(struct rte_eth_dev *dev)
324 struct dpaa2_dev_priv *priv = dev->data->dev_private;
325 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
328 PMD_INIT_FUNC_TRACE();
330 ret = dpni_disable(dpni, CMD_PRI_LOW, priv->token);
332 PMD_INIT_LOG(ERR, "Failure (ret %d) in disabling dpni %d dev\n",
339 dpaa2_dev_close(struct rte_eth_dev *dev)
341 struct dpaa2_dev_priv *priv = dev->data->dev_private;
342 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
345 PMD_INIT_FUNC_TRACE();
347 /* Clean the device first */
348 ret = dpni_reset(dpni, CMD_PRI_LOW, priv->token);
350 PMD_INIT_LOG(ERR, "Failure cleaning dpni device with"
351 " error code %d\n", ret);
356 static struct eth_dev_ops dpaa2_ethdev_ops = {
357 .dev_configure = dpaa2_eth_dev_configure,
358 .dev_start = dpaa2_dev_start,
359 .dev_stop = dpaa2_dev_stop,
360 .dev_close = dpaa2_dev_close,
361 .dev_infos_get = dpaa2_dev_info_get,
362 .rx_queue_setup = dpaa2_dev_rx_queue_setup,
363 .rx_queue_release = dpaa2_dev_rx_queue_release,
364 .tx_queue_setup = dpaa2_dev_tx_queue_setup,
365 .tx_queue_release = dpaa2_dev_tx_queue_release,
369 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
371 struct rte_device *dev = eth_dev->device;
372 struct rte_dpaa2_device *dpaa2_dev;
373 struct fsl_mc_io *dpni_dev;
374 struct dpni_attr attr;
375 struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
378 PMD_INIT_FUNC_TRACE();
380 /* For secondary processes, the primary has done all the work */
381 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
384 dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
386 hw_id = dpaa2_dev->object_id;
388 dpni_dev = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
390 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
394 dpni_dev->regs = rte_mcp_ptr_list[0];
395 ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
397 PMD_INIT_LOG(ERR, "Failure in opening dpni@%d device with"
398 " error code %d\n", hw_id, ret);
402 /* Clean the device first */
403 ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
405 PMD_INIT_LOG(ERR, "Failure cleaning dpni@%d device with"
406 " error code %d\n", hw_id, ret);
410 ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
412 PMD_INIT_LOG(ERR, "Failure in getting dpni@%d attribute, "
413 " error code %d\n", hw_id, ret);
417 priv->num_tc = attr.num_tcs;
418 priv->nb_rx_queues = attr.num_queues;
419 priv->nb_tx_queues = attr.num_queues;
425 /* Allocate memory for hardware structure for queues */
426 ret = dpaa2_alloc_rx_tx_queues(eth_dev);
428 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
432 eth_dev->dev_ops = &dpaa2_ethdev_ops;
433 eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
439 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
441 struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
442 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
444 struct dpaa2_queue *dpaa2_q;
446 PMD_INIT_FUNC_TRACE();
448 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
452 PMD_INIT_LOG(WARNING, "Already closed or not started");
456 dpaa2_dev_close(eth_dev);
458 if (priv->rx_vq[0]) {
459 /* cleaning up queue storage */
460 for (i = 0; i < priv->nb_rx_queues; i++) {
461 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
462 if (dpaa2_q->q_storage)
463 rte_free(dpaa2_q->q_storage);
465 /*free the all queue memory */
466 rte_free(priv->rx_vq[0]);
467 priv->rx_vq[0] = NULL;
471 /*Close the device at underlying layer*/
472 ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
474 PMD_INIT_LOG(ERR, "Failure closing dpni device with"
475 " error code %d\n", ret);
478 /*Free the allocated memory for ethernet private data and dpni*/
482 eth_dev->dev_ops = NULL;
488 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
489 struct rte_dpaa2_device *dpaa2_dev)
491 struct rte_eth_dev *eth_dev;
492 char ethdev_name[RTE_ETH_NAME_MAX_LEN];
496 sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
498 eth_dev = rte_eth_dev_allocate(ethdev_name);
502 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
503 eth_dev->data->dev_private = rte_zmalloc(
504 "ethdev private structure",
505 sizeof(struct dpaa2_dev_priv),
506 RTE_CACHE_LINE_SIZE);
507 if (eth_dev->data->dev_private == NULL) {
508 PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
509 " private port data\n");
510 rte_eth_dev_release_port(eth_dev);
514 eth_dev->device = &dpaa2_dev->device;
515 dpaa2_dev->eth_dev = eth_dev;
516 eth_dev->data->rx_mbuf_alloc_failed = 0;
518 /* Invoke PMD device initialization function */
519 diag = dpaa2_dev_init(eth_dev);
523 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
524 rte_free(eth_dev->data->dev_private);
525 rte_eth_dev_release_port(eth_dev);
530 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
532 struct rte_eth_dev *eth_dev;
534 eth_dev = dpaa2_dev->eth_dev;
535 dpaa2_dev_uninit(eth_dev);
537 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
538 rte_free(eth_dev->data->dev_private);
539 rte_eth_dev_release_port(eth_dev);
544 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
545 .drv_type = DPAA2_MC_DPNI_DEVID,
546 .probe = rte_dpaa2_probe,
547 .remove = rte_dpaa2_remove,
550 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);