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->num_dist_per_tc[DPAA2_DEF_TC];
120 mcq = (struct dpaa2_queue *)priv->rx_vq[vq_id];
121 mcq->tc_index = DPAA2_DEF_TC;
122 mcq->flow_id = dist_idx;
129 mc_q = priv->rx_vq[0];
131 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
132 rte_free(dpaa2_q->q_storage->dq_storage[0]);
133 rte_free(dpaa2_q->q_storage);
134 priv->rx_vq[i--] = NULL;
141 dpaa2_eth_dev_configure(struct rte_eth_dev *dev)
143 struct rte_eth_dev_data *data = dev->data;
144 struct rte_eth_conf *eth_conf = &data->dev_conf;
147 PMD_INIT_FUNC_TRACE();
149 /* Check for correct configuration */
150 if (eth_conf->rxmode.mq_mode != ETH_MQ_RX_RSS &&
151 data->nb_rx_queues > 1) {
152 PMD_INIT_LOG(ERR, "Distribution is not enabled, "
153 "but Rx queues more than 1\n");
157 if (eth_conf->rxmode.mq_mode == ETH_MQ_RX_RSS) {
158 /* Return in case number of Rx queues is 1 */
159 if (data->nb_rx_queues == 1)
161 ret = dpaa2_setup_flow_dist(dev,
162 eth_conf->rx_adv_conf.rss_conf.rss_hf);
164 PMD_INIT_LOG(ERR, "unable to set flow distribution."
165 "please check queue config\n");
172 /* Function to setup RX flow information. It contains traffic class ID,
173 * flow ID, destination configuration etc.
176 dpaa2_dev_rx_queue_setup(struct rte_eth_dev *dev,
177 uint16_t rx_queue_id,
178 uint16_t nb_rx_desc __rte_unused,
179 unsigned int socket_id __rte_unused,
180 const struct rte_eth_rxconf *rx_conf __rte_unused,
181 struct rte_mempool *mb_pool)
183 struct dpaa2_dev_priv *priv = dev->data->dev_private;
184 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
185 struct dpaa2_queue *dpaa2_q;
186 struct dpni_queue cfg;
191 PMD_INIT_FUNC_TRACE();
193 PMD_INIT_LOG(DEBUG, "dev =%p, queue =%d, pool = %p, conf =%p",
194 dev, rx_queue_id, mb_pool, rx_conf);
196 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[rx_queue_id];
197 dpaa2_q->mb_pool = mb_pool; /**< mbuf pool to populate RX ring. */
199 /*Get the tc id and flow id from given VQ id*/
200 flow_id = rx_queue_id % priv->num_dist_per_tc[dpaa2_q->tc_index];
201 memset(&cfg, 0, sizeof(struct dpni_queue));
203 options = options | DPNI_QUEUE_OPT_USER_CTX;
204 cfg.user_context = (uint64_t)(dpaa2_q);
206 ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_RX,
207 dpaa2_q->tc_index, flow_id, options, &cfg);
209 PMD_INIT_LOG(ERR, "Error in setting the rx flow: = %d\n", ret);
213 dev->data->rx_queues[rx_queue_id] = dpaa2_q;
218 dpaa2_dev_tx_queue_setup(struct rte_eth_dev *dev,
219 uint16_t tx_queue_id,
220 uint16_t nb_tx_desc __rte_unused,
221 unsigned int socket_id __rte_unused,
222 const struct rte_eth_txconf *tx_conf __rte_unused)
224 struct dpaa2_dev_priv *priv = dev->data->dev_private;
225 struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)
226 priv->tx_vq[tx_queue_id];
227 struct fsl_mc_io *dpni = priv->hw;
228 struct dpni_queue tx_conf_cfg;
229 struct dpni_queue tx_flow_cfg;
230 uint8_t options = 0, flow_id;
234 PMD_INIT_FUNC_TRACE();
236 /* Return if queue already configured */
237 if (dpaa2_q->flow_id != DPNI_NEW_FLOW_ID)
240 memset(&tx_conf_cfg, 0, sizeof(struct dpni_queue));
241 memset(&tx_flow_cfg, 0, sizeof(struct dpni_queue));
244 flow_id = tx_queue_id;
246 ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_TX,
247 tc_id, flow_id, options, &tx_flow_cfg);
249 PMD_INIT_LOG(ERR, "Error in setting the tx flow: "
250 "tc_id=%d, flow =%d ErrorCode = %x\n",
251 tc_id, flow_id, -ret);
255 dpaa2_q->flow_id = flow_id;
257 if (tx_queue_id == 0) {
258 /*Set tx-conf and error configuration*/
259 ret = dpni_set_tx_confirmation_mode(dpni, CMD_PRI_LOW,
263 PMD_INIT_LOG(ERR, "Error in set tx conf mode settings"
264 " ErrorCode = %x", ret);
268 dpaa2_q->tc_index = tc_id;
270 dev->data->tx_queues[tx_queue_id] = dpaa2_q;
275 dpaa2_dev_rx_queue_release(void *q __rte_unused)
277 PMD_INIT_FUNC_TRACE();
281 dpaa2_dev_tx_queue_release(void *q __rte_unused)
283 PMD_INIT_FUNC_TRACE();
287 dpaa2_dev_start(struct rte_eth_dev *dev)
289 struct rte_eth_dev_data *data = dev->data;
290 struct dpaa2_dev_priv *priv = data->dev_private;
291 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
292 struct dpni_queue cfg;
294 struct dpni_queue_id qid;
295 struct dpaa2_queue *dpaa2_q;
298 PMD_INIT_FUNC_TRACE();
300 ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
302 PMD_INIT_LOG(ERR, "Failure %d in enabling dpni %d device\n",
307 ret = dpni_get_qdid(dpni, CMD_PRI_LOW, priv->token,
308 DPNI_QUEUE_TX, &qdid);
310 PMD_INIT_LOG(ERR, "Error to get qdid:ErrorCode = %d\n", ret);
315 for (i = 0; i < data->nb_rx_queues; i++) {
316 dpaa2_q = (struct dpaa2_queue *)data->rx_queues[i];
317 ret = dpni_get_queue(dpni, CMD_PRI_LOW, priv->token,
318 DPNI_QUEUE_RX, dpaa2_q->tc_index,
319 dpaa2_q->flow_id, &cfg, &qid);
321 PMD_INIT_LOG(ERR, "Error to get flow "
322 "information Error code = %d\n", ret);
325 dpaa2_q->fqid = qid.fqid;
332 * This routine disables all traffic on the adapter by issuing a
333 * global reset on the MAC.
336 dpaa2_dev_stop(struct rte_eth_dev *dev)
338 struct dpaa2_dev_priv *priv = dev->data->dev_private;
339 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
342 PMD_INIT_FUNC_TRACE();
344 ret = dpni_disable(dpni, CMD_PRI_LOW, priv->token);
346 PMD_INIT_LOG(ERR, "Failure (ret %d) in disabling dpni %d dev\n",
353 dpaa2_dev_close(struct rte_eth_dev *dev)
355 struct dpaa2_dev_priv *priv = dev->data->dev_private;
356 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
359 PMD_INIT_FUNC_TRACE();
361 /* Clean the device first */
362 ret = dpni_reset(dpni, CMD_PRI_LOW, priv->token);
364 PMD_INIT_LOG(ERR, "Failure cleaning dpni device with"
365 " error code %d\n", ret);
370 static struct eth_dev_ops dpaa2_ethdev_ops = {
371 .dev_configure = dpaa2_eth_dev_configure,
372 .dev_start = dpaa2_dev_start,
373 .dev_stop = dpaa2_dev_stop,
374 .dev_close = dpaa2_dev_close,
375 .dev_infos_get = dpaa2_dev_info_get,
376 .rx_queue_setup = dpaa2_dev_rx_queue_setup,
377 .rx_queue_release = dpaa2_dev_rx_queue_release,
378 .tx_queue_setup = dpaa2_dev_tx_queue_setup,
379 .tx_queue_release = dpaa2_dev_tx_queue_release,
383 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
385 struct rte_device *dev = eth_dev->device;
386 struct rte_dpaa2_device *dpaa2_dev;
387 struct fsl_mc_io *dpni_dev;
388 struct dpni_attr attr;
389 struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
392 PMD_INIT_FUNC_TRACE();
394 /* For secondary processes, the primary has done all the work */
395 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
398 dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
400 hw_id = dpaa2_dev->object_id;
402 dpni_dev = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
404 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
408 dpni_dev->regs = rte_mcp_ptr_list[0];
409 ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
411 PMD_INIT_LOG(ERR, "Failure in opening dpni@%d device with"
412 " error code %d\n", hw_id, ret);
416 /* Clean the device first */
417 ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
419 PMD_INIT_LOG(ERR, "Failure cleaning dpni@%d device with"
420 " error code %d\n", hw_id, ret);
424 ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
426 PMD_INIT_LOG(ERR, "Failure in getting dpni@%d attribute, "
427 " error code %d\n", hw_id, ret);
431 priv->num_tc = attr.num_tcs;
432 for (i = 0; i < attr.num_tcs; i++) {
433 priv->num_dist_per_tc[i] = attr.num_queues;
437 /* Distribution is per Tc only,
438 * so choosing RX queues from default TC only
440 priv->nb_rx_queues = priv->num_dist_per_tc[DPAA2_DEF_TC];
442 priv->nb_tx_queues = attr.num_queues;
448 /* Allocate memory for hardware structure for queues */
449 ret = dpaa2_alloc_rx_tx_queues(eth_dev);
451 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
455 eth_dev->dev_ops = &dpaa2_ethdev_ops;
456 eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
462 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
464 struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
465 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
467 struct dpaa2_queue *dpaa2_q;
469 PMD_INIT_FUNC_TRACE();
471 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
475 PMD_INIT_LOG(WARNING, "Already closed or not started");
479 dpaa2_dev_close(eth_dev);
481 if (priv->rx_vq[0]) {
482 /* cleaning up queue storage */
483 for (i = 0; i < priv->nb_rx_queues; i++) {
484 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
485 if (dpaa2_q->q_storage)
486 rte_free(dpaa2_q->q_storage);
488 /*free the all queue memory */
489 rte_free(priv->rx_vq[0]);
490 priv->rx_vq[0] = NULL;
494 /*Close the device at underlying layer*/
495 ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
497 PMD_INIT_LOG(ERR, "Failure closing dpni device with"
498 " error code %d\n", ret);
501 /*Free the allocated memory for ethernet private data and dpni*/
505 eth_dev->dev_ops = NULL;
511 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
512 struct rte_dpaa2_device *dpaa2_dev)
514 struct rte_eth_dev *eth_dev;
515 char ethdev_name[RTE_ETH_NAME_MAX_LEN];
519 sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
521 eth_dev = rte_eth_dev_allocate(ethdev_name);
525 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
526 eth_dev->data->dev_private = rte_zmalloc(
527 "ethdev private structure",
528 sizeof(struct dpaa2_dev_priv),
529 RTE_CACHE_LINE_SIZE);
530 if (eth_dev->data->dev_private == NULL) {
531 PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
532 " private port data\n");
533 rte_eth_dev_release_port(eth_dev);
537 eth_dev->device = &dpaa2_dev->device;
538 dpaa2_dev->eth_dev = eth_dev;
539 eth_dev->data->rx_mbuf_alloc_failed = 0;
541 /* Invoke PMD device initialization function */
542 diag = dpaa2_dev_init(eth_dev);
546 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
547 rte_free(eth_dev->data->dev_private);
548 rte_eth_dev_release_port(eth_dev);
553 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
555 struct rte_eth_dev *eth_dev;
557 eth_dev = dpaa2_dev->eth_dev;
558 dpaa2_dev_uninit(eth_dev);
560 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
561 rte_free(eth_dev->data->dev_private);
562 rte_eth_dev_release_port(eth_dev);
567 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
568 .drv_type = DPAA2_MC_DPNI_DEVID,
569 .probe = rte_dpaa2_probe,
570 .remove = rte_dpaa2_remove,
573 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);