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_mac_addrs = priv->max_mac_filters;
66 dev_info->max_rx_queues = (uint16_t)priv->nb_rx_queues;
67 dev_info->max_tx_queues = (uint16_t)priv->nb_tx_queues;
69 dev_info->speed_capa = ETH_LINK_SPEED_1G |
75 dpaa2_alloc_rx_tx_queues(struct rte_eth_dev *dev)
77 struct dpaa2_dev_priv *priv = dev->data->dev_private;
80 struct dpaa2_queue *mc_q, *mcq;
83 struct dpaa2_queue *dpaa2_q;
85 PMD_INIT_FUNC_TRACE();
87 tot_queues = priv->nb_rx_queues + priv->nb_tx_queues;
88 mc_q = rte_malloc(NULL, sizeof(struct dpaa2_queue) * tot_queues,
91 PMD_INIT_LOG(ERR, "malloc failed for rx/tx queues\n");
95 for (i = 0; i < priv->nb_rx_queues; i++) {
97 priv->rx_vq[i] = mc_q++;
98 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
99 dpaa2_q->q_storage = rte_malloc("dq_storage",
100 sizeof(struct queue_storage_info_t),
101 RTE_CACHE_LINE_SIZE);
102 if (!dpaa2_q->q_storage)
105 memset(dpaa2_q->q_storage, 0,
106 sizeof(struct queue_storage_info_t));
107 dpaa2_q->q_storage->dq_storage[0] = rte_malloc(NULL,
108 DPAA2_DQRR_RING_SIZE * sizeof(struct qbman_result),
109 RTE_CACHE_LINE_SIZE);
112 for (i = 0; i < priv->nb_tx_queues; i++) {
114 mc_q->flow_id = DPNI_NEW_FLOW_ID;
115 priv->tx_vq[i] = mc_q++;
119 for (dist_idx = 0; dist_idx < priv->num_dist_per_tc[DPAA2_DEF_TC];
121 mcq = (struct dpaa2_queue *)priv->rx_vq[vq_id];
122 mcq->tc_index = DPAA2_DEF_TC;
123 mcq->flow_id = dist_idx;
130 mc_q = priv->rx_vq[0];
132 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
133 rte_free(dpaa2_q->q_storage->dq_storage[0]);
134 rte_free(dpaa2_q->q_storage);
135 priv->rx_vq[i--] = NULL;
142 dpaa2_eth_dev_configure(struct rte_eth_dev *dev)
144 struct rte_eth_dev_data *data = dev->data;
145 struct rte_eth_conf *eth_conf = &data->dev_conf;
148 PMD_INIT_FUNC_TRACE();
150 /* Check for correct configuration */
151 if (eth_conf->rxmode.mq_mode != ETH_MQ_RX_RSS &&
152 data->nb_rx_queues > 1) {
153 PMD_INIT_LOG(ERR, "Distribution is not enabled, "
154 "but Rx queues more than 1\n");
158 if (eth_conf->rxmode.mq_mode == ETH_MQ_RX_RSS) {
159 /* Return in case number of Rx queues is 1 */
160 if (data->nb_rx_queues == 1)
162 ret = dpaa2_setup_flow_dist(dev,
163 eth_conf->rx_adv_conf.rss_conf.rss_hf);
165 PMD_INIT_LOG(ERR, "unable to set flow distribution."
166 "please check queue config\n");
173 /* Function to setup RX flow information. It contains traffic class ID,
174 * flow ID, destination configuration etc.
177 dpaa2_dev_rx_queue_setup(struct rte_eth_dev *dev,
178 uint16_t rx_queue_id,
179 uint16_t nb_rx_desc __rte_unused,
180 unsigned int socket_id __rte_unused,
181 const struct rte_eth_rxconf *rx_conf __rte_unused,
182 struct rte_mempool *mb_pool)
184 struct dpaa2_dev_priv *priv = dev->data->dev_private;
185 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
186 struct dpaa2_queue *dpaa2_q;
187 struct dpni_queue cfg;
192 PMD_INIT_FUNC_TRACE();
194 PMD_INIT_LOG(DEBUG, "dev =%p, queue =%d, pool = %p, conf =%p",
195 dev, rx_queue_id, mb_pool, rx_conf);
197 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[rx_queue_id];
198 dpaa2_q->mb_pool = mb_pool; /**< mbuf pool to populate RX ring. */
200 /*Get the tc id and flow id from given VQ id*/
201 flow_id = rx_queue_id % priv->num_dist_per_tc[dpaa2_q->tc_index];
202 memset(&cfg, 0, sizeof(struct dpni_queue));
204 options = options | DPNI_QUEUE_OPT_USER_CTX;
205 cfg.user_context = (uint64_t)(dpaa2_q);
207 ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_RX,
208 dpaa2_q->tc_index, flow_id, options, &cfg);
210 PMD_INIT_LOG(ERR, "Error in setting the rx flow: = %d\n", ret);
214 dev->data->rx_queues[rx_queue_id] = dpaa2_q;
219 dpaa2_dev_tx_queue_setup(struct rte_eth_dev *dev,
220 uint16_t tx_queue_id,
221 uint16_t nb_tx_desc __rte_unused,
222 unsigned int socket_id __rte_unused,
223 const struct rte_eth_txconf *tx_conf __rte_unused)
225 struct dpaa2_dev_priv *priv = dev->data->dev_private;
226 struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)
227 priv->tx_vq[tx_queue_id];
228 struct fsl_mc_io *dpni = priv->hw;
229 struct dpni_queue tx_conf_cfg;
230 struct dpni_queue tx_flow_cfg;
231 uint8_t options = 0, flow_id;
235 PMD_INIT_FUNC_TRACE();
237 /* Return if queue already configured */
238 if (dpaa2_q->flow_id != DPNI_NEW_FLOW_ID)
241 memset(&tx_conf_cfg, 0, sizeof(struct dpni_queue));
242 memset(&tx_flow_cfg, 0, sizeof(struct dpni_queue));
245 flow_id = tx_queue_id;
247 ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_TX,
248 tc_id, flow_id, options, &tx_flow_cfg);
250 PMD_INIT_LOG(ERR, "Error in setting the tx flow: "
251 "tc_id=%d, flow =%d ErrorCode = %x\n",
252 tc_id, flow_id, -ret);
256 dpaa2_q->flow_id = flow_id;
258 if (tx_queue_id == 0) {
259 /*Set tx-conf and error configuration*/
260 ret = dpni_set_tx_confirmation_mode(dpni, CMD_PRI_LOW,
264 PMD_INIT_LOG(ERR, "Error in set tx conf mode settings"
265 " ErrorCode = %x", ret);
269 dpaa2_q->tc_index = tc_id;
271 dev->data->tx_queues[tx_queue_id] = dpaa2_q;
276 dpaa2_dev_rx_queue_release(void *q __rte_unused)
278 PMD_INIT_FUNC_TRACE();
282 dpaa2_dev_tx_queue_release(void *q __rte_unused)
284 PMD_INIT_FUNC_TRACE();
288 dpaa2_dev_start(struct rte_eth_dev *dev)
290 struct rte_eth_dev_data *data = dev->data;
291 struct dpaa2_dev_priv *priv = data->dev_private;
292 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
293 struct dpni_queue cfg;
295 struct dpni_queue_id qid;
296 struct dpaa2_queue *dpaa2_q;
299 PMD_INIT_FUNC_TRACE();
301 ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
303 PMD_INIT_LOG(ERR, "Failure %d in enabling dpni %d device\n",
308 ret = dpni_get_qdid(dpni, CMD_PRI_LOW, priv->token,
309 DPNI_QUEUE_TX, &qdid);
311 PMD_INIT_LOG(ERR, "Error to get qdid:ErrorCode = %d\n", ret);
316 for (i = 0; i < data->nb_rx_queues; i++) {
317 dpaa2_q = (struct dpaa2_queue *)data->rx_queues[i];
318 ret = dpni_get_queue(dpni, CMD_PRI_LOW, priv->token,
319 DPNI_QUEUE_RX, dpaa2_q->tc_index,
320 dpaa2_q->flow_id, &cfg, &qid);
322 PMD_INIT_LOG(ERR, "Error to get flow "
323 "information Error code = %d\n", ret);
326 dpaa2_q->fqid = qid.fqid;
333 * This routine disables all traffic on the adapter by issuing a
334 * global reset on the MAC.
337 dpaa2_dev_stop(struct rte_eth_dev *dev)
339 struct dpaa2_dev_priv *priv = dev->data->dev_private;
340 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
343 PMD_INIT_FUNC_TRACE();
345 ret = dpni_disable(dpni, CMD_PRI_LOW, priv->token);
347 PMD_INIT_LOG(ERR, "Failure (ret %d) in disabling dpni %d dev\n",
354 dpaa2_dev_close(struct rte_eth_dev *dev)
356 struct dpaa2_dev_priv *priv = dev->data->dev_private;
357 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
360 PMD_INIT_FUNC_TRACE();
362 /* Clean the device first */
363 ret = dpni_reset(dpni, CMD_PRI_LOW, priv->token);
365 PMD_INIT_LOG(ERR, "Failure cleaning dpni device with"
366 " error code %d\n", ret);
371 static struct eth_dev_ops dpaa2_ethdev_ops = {
372 .dev_configure = dpaa2_eth_dev_configure,
373 .dev_start = dpaa2_dev_start,
374 .dev_stop = dpaa2_dev_stop,
375 .dev_close = dpaa2_dev_close,
376 .dev_infos_get = dpaa2_dev_info_get,
377 .rx_queue_setup = dpaa2_dev_rx_queue_setup,
378 .rx_queue_release = dpaa2_dev_rx_queue_release,
379 .tx_queue_setup = dpaa2_dev_tx_queue_setup,
380 .tx_queue_release = dpaa2_dev_tx_queue_release,
384 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
386 struct rte_device *dev = eth_dev->device;
387 struct rte_dpaa2_device *dpaa2_dev;
388 struct fsl_mc_io *dpni_dev;
389 struct dpni_attr attr;
390 struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
393 PMD_INIT_FUNC_TRACE();
395 /* For secondary processes, the primary has done all the work */
396 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
399 dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
401 hw_id = dpaa2_dev->object_id;
403 dpni_dev = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
405 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
409 dpni_dev->regs = rte_mcp_ptr_list[0];
410 ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
412 PMD_INIT_LOG(ERR, "Failure in opening dpni@%d device with"
413 " error code %d\n", hw_id, ret);
417 /* Clean the device first */
418 ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
420 PMD_INIT_LOG(ERR, "Failure cleaning dpni@%d device with"
421 " error code %d\n", hw_id, ret);
425 ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
427 PMD_INIT_LOG(ERR, "Failure in getting dpni@%d attribute, "
428 " error code %d\n", hw_id, ret);
432 priv->num_tc = attr.num_tcs;
433 for (i = 0; i < attr.num_tcs; i++) {
434 priv->num_dist_per_tc[i] = attr.num_queues;
438 /* Distribution is per Tc only,
439 * so choosing RX queues from default TC only
441 priv->nb_rx_queues = priv->num_dist_per_tc[DPAA2_DEF_TC];
443 priv->nb_tx_queues = attr.num_queues;
447 priv->options = attr.options;
448 priv->max_mac_filters = attr.mac_filter_entries;
449 priv->max_vlan_filters = attr.vlan_filter_entries;
452 /* Allocate memory for hardware structure for queues */
453 ret = dpaa2_alloc_rx_tx_queues(eth_dev);
455 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
459 /* Allocate memory for storing MAC addresses */
460 eth_dev->data->mac_addrs = rte_zmalloc("dpni",
461 ETHER_ADDR_LEN * attr.mac_filter_entries, 0);
462 if (eth_dev->data->mac_addrs == NULL) {
463 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to "
464 "store MAC addresses",
465 ETHER_ADDR_LEN * attr.mac_filter_entries);
469 ret = dpni_get_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
471 (uint8_t *)(eth_dev->data->mac_addrs[0].addr_bytes));
473 PMD_INIT_LOG(ERR, "DPNI get mac address failed:"
474 " Error Code = %d\n", ret);
478 eth_dev->dev_ops = &dpaa2_ethdev_ops;
479 eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
485 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
487 struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
488 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
490 struct dpaa2_queue *dpaa2_q;
492 PMD_INIT_FUNC_TRACE();
494 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
498 PMD_INIT_LOG(WARNING, "Already closed or not started");
502 dpaa2_dev_close(eth_dev);
504 if (priv->rx_vq[0]) {
505 /* cleaning up queue storage */
506 for (i = 0; i < priv->nb_rx_queues; i++) {
507 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
508 if (dpaa2_q->q_storage)
509 rte_free(dpaa2_q->q_storage);
511 /*free the all queue memory */
512 rte_free(priv->rx_vq[0]);
513 priv->rx_vq[0] = NULL;
516 /* Allocate memory for storing MAC addresses */
517 if (eth_dev->data->mac_addrs) {
518 rte_free(eth_dev->data->mac_addrs);
519 eth_dev->data->mac_addrs = NULL;
522 /*Close the device at underlying layer*/
523 ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
525 PMD_INIT_LOG(ERR, "Failure closing dpni device with"
526 " error code %d\n", ret);
529 /*Free the allocated memory for ethernet private data and dpni*/
533 eth_dev->dev_ops = NULL;
539 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
540 struct rte_dpaa2_device *dpaa2_dev)
542 struct rte_eth_dev *eth_dev;
543 char ethdev_name[RTE_ETH_NAME_MAX_LEN];
547 sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
549 eth_dev = rte_eth_dev_allocate(ethdev_name);
553 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
554 eth_dev->data->dev_private = rte_zmalloc(
555 "ethdev private structure",
556 sizeof(struct dpaa2_dev_priv),
557 RTE_CACHE_LINE_SIZE);
558 if (eth_dev->data->dev_private == NULL) {
559 PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
560 " private port data\n");
561 rte_eth_dev_release_port(eth_dev);
565 eth_dev->device = &dpaa2_dev->device;
566 dpaa2_dev->eth_dev = eth_dev;
567 eth_dev->data->rx_mbuf_alloc_failed = 0;
569 /* Invoke PMD device initialization function */
570 diag = dpaa2_dev_init(eth_dev);
574 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
575 rte_free(eth_dev->data->dev_private);
576 rte_eth_dev_release_port(eth_dev);
581 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
583 struct rte_eth_dev *eth_dev;
585 eth_dev = dpaa2_dev->eth_dev;
586 dpaa2_dev_uninit(eth_dev);
588 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
589 rte_free(eth_dev->data->dev_private);
590 rte_eth_dev_release_port(eth_dev);
595 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
596 .drv_type = DPAA2_MC_DPNI_DEVID,
597 .probe = rte_dpaa2_probe,
598 .remove = rte_dpaa2_remove,
601 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);