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>
51 #include <dpaa2_hw_mempool.h>
53 #include "dpaa2_ethdev.h"
55 static struct rte_dpaa2_driver rte_dpaa2_pmd;
58 dpaa2_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
60 struct dpaa2_dev_priv *priv = dev->data->dev_private;
62 PMD_INIT_FUNC_TRACE();
64 dev_info->if_index = priv->hw_id;
66 dev_info->max_mac_addrs = priv->max_mac_filters;
67 dev_info->max_rx_pktlen = DPAA2_MAX_RX_PKT_LEN;
68 dev_info->min_rx_bufsize = DPAA2_MIN_RX_BUF_SIZE;
69 dev_info->max_rx_queues = (uint16_t)priv->nb_rx_queues;
70 dev_info->max_tx_queues = (uint16_t)priv->nb_tx_queues;
72 dev_info->speed_capa = ETH_LINK_SPEED_1G |
78 dpaa2_alloc_rx_tx_queues(struct rte_eth_dev *dev)
80 struct dpaa2_dev_priv *priv = dev->data->dev_private;
83 struct dpaa2_queue *mc_q, *mcq;
86 struct dpaa2_queue *dpaa2_q;
88 PMD_INIT_FUNC_TRACE();
90 tot_queues = priv->nb_rx_queues + priv->nb_tx_queues;
91 mc_q = rte_malloc(NULL, sizeof(struct dpaa2_queue) * tot_queues,
94 PMD_INIT_LOG(ERR, "malloc failed for rx/tx queues\n");
98 for (i = 0; i < priv->nb_rx_queues; i++) {
100 priv->rx_vq[i] = mc_q++;
101 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
102 dpaa2_q->q_storage = rte_malloc("dq_storage",
103 sizeof(struct queue_storage_info_t),
104 RTE_CACHE_LINE_SIZE);
105 if (!dpaa2_q->q_storage)
108 memset(dpaa2_q->q_storage, 0,
109 sizeof(struct queue_storage_info_t));
110 dpaa2_q->q_storage->dq_storage[0] = rte_malloc(NULL,
111 DPAA2_DQRR_RING_SIZE * sizeof(struct qbman_result),
112 RTE_CACHE_LINE_SIZE);
115 for (i = 0; i < priv->nb_tx_queues; i++) {
117 mc_q->flow_id = DPNI_NEW_FLOW_ID;
118 priv->tx_vq[i] = mc_q++;
122 for (dist_idx = 0; dist_idx < priv->num_dist_per_tc[DPAA2_DEF_TC];
124 mcq = (struct dpaa2_queue *)priv->rx_vq[vq_id];
125 mcq->tc_index = DPAA2_DEF_TC;
126 mcq->flow_id = dist_idx;
133 mc_q = priv->rx_vq[0];
135 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
136 rte_free(dpaa2_q->q_storage->dq_storage[0]);
137 rte_free(dpaa2_q->q_storage);
138 priv->rx_vq[i--] = NULL;
145 dpaa2_eth_dev_configure(struct rte_eth_dev *dev)
147 struct rte_eth_dev_data *data = dev->data;
148 struct rte_eth_conf *eth_conf = &data->dev_conf;
151 PMD_INIT_FUNC_TRACE();
153 /* Check for correct configuration */
154 if (eth_conf->rxmode.mq_mode != ETH_MQ_RX_RSS &&
155 data->nb_rx_queues > 1) {
156 PMD_INIT_LOG(ERR, "Distribution is not enabled, "
157 "but Rx queues more than 1\n");
161 if (eth_conf->rxmode.mq_mode == ETH_MQ_RX_RSS) {
162 /* Return in case number of Rx queues is 1 */
163 if (data->nb_rx_queues == 1)
165 ret = dpaa2_setup_flow_dist(dev,
166 eth_conf->rx_adv_conf.rss_conf.rss_hf);
168 PMD_INIT_LOG(ERR, "unable to set flow distribution."
169 "please check queue config\n");
176 /* Function to setup RX flow information. It contains traffic class ID,
177 * flow ID, destination configuration etc.
180 dpaa2_dev_rx_queue_setup(struct rte_eth_dev *dev,
181 uint16_t rx_queue_id,
182 uint16_t nb_rx_desc __rte_unused,
183 unsigned int socket_id __rte_unused,
184 const struct rte_eth_rxconf *rx_conf __rte_unused,
185 struct rte_mempool *mb_pool)
187 struct dpaa2_dev_priv *priv = dev->data->dev_private;
188 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
189 struct dpaa2_queue *dpaa2_q;
190 struct dpni_queue cfg;
196 PMD_INIT_FUNC_TRACE();
198 PMD_INIT_LOG(DEBUG, "dev =%p, queue =%d, pool = %p, conf =%p",
199 dev, rx_queue_id, mb_pool, rx_conf);
201 if (!priv->bp_list || priv->bp_list->mp != mb_pool) {
202 bpid = mempool_to_bpid(mb_pool);
203 ret = dpaa2_attach_bp_list(priv,
204 rte_dpaa2_bpid_info[bpid].bp_list);
208 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[rx_queue_id];
209 dpaa2_q->mb_pool = mb_pool; /**< mbuf pool to populate RX ring. */
211 /*Get the tc id and flow id from given VQ id*/
212 flow_id = rx_queue_id % priv->num_dist_per_tc[dpaa2_q->tc_index];
213 memset(&cfg, 0, sizeof(struct dpni_queue));
215 options = options | DPNI_QUEUE_OPT_USER_CTX;
216 cfg.user_context = (uint64_t)(dpaa2_q);
218 ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_RX,
219 dpaa2_q->tc_index, flow_id, options, &cfg);
221 PMD_INIT_LOG(ERR, "Error in setting the rx flow: = %d\n", ret);
225 dev->data->rx_queues[rx_queue_id] = dpaa2_q;
230 dpaa2_dev_tx_queue_setup(struct rte_eth_dev *dev,
231 uint16_t tx_queue_id,
232 uint16_t nb_tx_desc __rte_unused,
233 unsigned int socket_id __rte_unused,
234 const struct rte_eth_txconf *tx_conf __rte_unused)
236 struct dpaa2_dev_priv *priv = dev->data->dev_private;
237 struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)
238 priv->tx_vq[tx_queue_id];
239 struct fsl_mc_io *dpni = priv->hw;
240 struct dpni_queue tx_conf_cfg;
241 struct dpni_queue tx_flow_cfg;
242 uint8_t options = 0, flow_id;
246 PMD_INIT_FUNC_TRACE();
248 /* Return if queue already configured */
249 if (dpaa2_q->flow_id != DPNI_NEW_FLOW_ID)
252 memset(&tx_conf_cfg, 0, sizeof(struct dpni_queue));
253 memset(&tx_flow_cfg, 0, sizeof(struct dpni_queue));
256 flow_id = tx_queue_id;
258 ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_TX,
259 tc_id, flow_id, options, &tx_flow_cfg);
261 PMD_INIT_LOG(ERR, "Error in setting the tx flow: "
262 "tc_id=%d, flow =%d ErrorCode = %x\n",
263 tc_id, flow_id, -ret);
267 dpaa2_q->flow_id = flow_id;
269 if (tx_queue_id == 0) {
270 /*Set tx-conf and error configuration*/
271 ret = dpni_set_tx_confirmation_mode(dpni, CMD_PRI_LOW,
275 PMD_INIT_LOG(ERR, "Error in set tx conf mode settings"
276 " ErrorCode = %x", ret);
280 dpaa2_q->tc_index = tc_id;
282 dev->data->tx_queues[tx_queue_id] = dpaa2_q;
287 dpaa2_dev_rx_queue_release(void *q __rte_unused)
289 PMD_INIT_FUNC_TRACE();
293 dpaa2_dev_tx_queue_release(void *q __rte_unused)
295 PMD_INIT_FUNC_TRACE();
299 dpaa2_dev_start(struct rte_eth_dev *dev)
301 struct rte_eth_dev_data *data = dev->data;
302 struct dpaa2_dev_priv *priv = data->dev_private;
303 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
304 struct dpni_queue cfg;
306 struct dpni_queue_id qid;
307 struct dpaa2_queue *dpaa2_q;
310 PMD_INIT_FUNC_TRACE();
312 ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
314 PMD_INIT_LOG(ERR, "Failure %d in enabling dpni %d device\n",
319 ret = dpni_get_qdid(dpni, CMD_PRI_LOW, priv->token,
320 DPNI_QUEUE_TX, &qdid);
322 PMD_INIT_LOG(ERR, "Error to get qdid:ErrorCode = %d\n", ret);
327 for (i = 0; i < data->nb_rx_queues; i++) {
328 dpaa2_q = (struct dpaa2_queue *)data->rx_queues[i];
329 ret = dpni_get_queue(dpni, CMD_PRI_LOW, priv->token,
330 DPNI_QUEUE_RX, dpaa2_q->tc_index,
331 dpaa2_q->flow_id, &cfg, &qid);
333 PMD_INIT_LOG(ERR, "Error to get flow "
334 "information Error code = %d\n", ret);
337 dpaa2_q->fqid = qid.fqid;
344 * This routine disables all traffic on the adapter by issuing a
345 * global reset on the MAC.
348 dpaa2_dev_stop(struct rte_eth_dev *dev)
350 struct dpaa2_dev_priv *priv = dev->data->dev_private;
351 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
354 PMD_INIT_FUNC_TRACE();
356 ret = dpni_disable(dpni, CMD_PRI_LOW, priv->token);
358 PMD_INIT_LOG(ERR, "Failure (ret %d) in disabling dpni %d dev\n",
365 dpaa2_dev_close(struct rte_eth_dev *dev)
367 struct dpaa2_dev_priv *priv = dev->data->dev_private;
368 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
371 PMD_INIT_FUNC_TRACE();
373 /* Clean the device first */
374 ret = dpni_reset(dpni, CMD_PRI_LOW, priv->token);
376 PMD_INIT_LOG(ERR, "Failure cleaning dpni device with"
377 " error code %d\n", ret);
382 static struct eth_dev_ops dpaa2_ethdev_ops = {
383 .dev_configure = dpaa2_eth_dev_configure,
384 .dev_start = dpaa2_dev_start,
385 .dev_stop = dpaa2_dev_stop,
386 .dev_close = dpaa2_dev_close,
387 .dev_infos_get = dpaa2_dev_info_get,
388 .rx_queue_setup = dpaa2_dev_rx_queue_setup,
389 .rx_queue_release = dpaa2_dev_rx_queue_release,
390 .tx_queue_setup = dpaa2_dev_tx_queue_setup,
391 .tx_queue_release = dpaa2_dev_tx_queue_release,
395 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
397 struct rte_device *dev = eth_dev->device;
398 struct rte_dpaa2_device *dpaa2_dev;
399 struct fsl_mc_io *dpni_dev;
400 struct dpni_attr attr;
401 struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
402 struct dpni_buffer_layout layout;
406 PMD_INIT_FUNC_TRACE();
408 /* For secondary processes, the primary has done all the work */
409 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
412 dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
414 hw_id = dpaa2_dev->object_id;
416 dpni_dev = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
418 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
422 dpni_dev->regs = rte_mcp_ptr_list[0];
423 ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
425 PMD_INIT_LOG(ERR, "Failure in opening dpni@%d device with"
426 " error code %d\n", hw_id, ret);
430 /* Clean the device first */
431 ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
433 PMD_INIT_LOG(ERR, "Failure cleaning dpni@%d device with"
434 " error code %d\n", hw_id, ret);
438 ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
440 PMD_INIT_LOG(ERR, "Failure in getting dpni@%d attribute, "
441 " error code %d\n", hw_id, ret);
445 priv->num_tc = attr.num_tcs;
446 for (i = 0; i < attr.num_tcs; i++) {
447 priv->num_dist_per_tc[i] = attr.num_queues;
451 /* Distribution is per Tc only,
452 * so choosing RX queues from default TC only
454 priv->nb_rx_queues = priv->num_dist_per_tc[DPAA2_DEF_TC];
456 priv->nb_tx_queues = attr.num_queues;
460 priv->options = attr.options;
461 priv->max_mac_filters = attr.mac_filter_entries;
462 priv->max_vlan_filters = attr.vlan_filter_entries;
465 /* Allocate memory for hardware structure for queues */
466 ret = dpaa2_alloc_rx_tx_queues(eth_dev);
468 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
472 /* Allocate memory for storing MAC addresses */
473 eth_dev->data->mac_addrs = rte_zmalloc("dpni",
474 ETHER_ADDR_LEN * attr.mac_filter_entries, 0);
475 if (eth_dev->data->mac_addrs == NULL) {
476 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to "
477 "store MAC addresses",
478 ETHER_ADDR_LEN * attr.mac_filter_entries);
482 ret = dpni_get_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
484 (uint8_t *)(eth_dev->data->mac_addrs[0].addr_bytes));
486 PMD_INIT_LOG(ERR, "DPNI get mac address failed:"
487 " Error Code = %d\n", ret);
491 /* ... rx buffer layout ... */
492 tot_size = DPAA2_HW_BUF_RESERVE + RTE_PKTMBUF_HEADROOM;
493 tot_size = RTE_ALIGN_CEIL(tot_size,
494 DPAA2_PACKET_LAYOUT_ALIGN);
496 memset(&layout, 0, sizeof(struct dpni_buffer_layout));
497 layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
498 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
499 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
500 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
502 layout.pass_frame_status = 1;
503 layout.data_head_room = tot_size
504 - DPAA2_FD_PTA_SIZE - DPAA2_MBUF_HW_ANNOTATION;
505 layout.private_data_size = DPAA2_FD_PTA_SIZE;
506 layout.pass_parser_result = 1;
507 PMD_INIT_LOG(DEBUG, "Tot_size = %d, head room = %d, private = %d",
508 tot_size, layout.data_head_room, layout.private_data_size);
509 ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
510 DPNI_QUEUE_RX, &layout);
512 PMD_INIT_LOG(ERR, "Err(%d) in setting rx buffer layout", ret);
516 /* ... tx buffer layout ... */
517 memset(&layout, 0, sizeof(struct dpni_buffer_layout));
518 layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
519 layout.pass_frame_status = 1;
520 ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
521 DPNI_QUEUE_TX, &layout);
523 PMD_INIT_LOG(ERR, "Error (%d) in setting tx buffer"
528 /* ... tx-conf and error buffer layout ... */
529 memset(&layout, 0, sizeof(struct dpni_buffer_layout));
530 layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
531 layout.pass_frame_status = 1;
532 ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
533 DPNI_QUEUE_TX_CONFIRM, &layout);
535 PMD_INIT_LOG(ERR, "Error (%d) in setting tx-conf buffer"
540 eth_dev->dev_ops = &dpaa2_ethdev_ops;
541 eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
547 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
549 struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
550 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
552 struct dpaa2_queue *dpaa2_q;
554 PMD_INIT_FUNC_TRACE();
556 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
560 PMD_INIT_LOG(WARNING, "Already closed or not started");
564 dpaa2_dev_close(eth_dev);
566 if (priv->rx_vq[0]) {
567 /* cleaning up queue storage */
568 for (i = 0; i < priv->nb_rx_queues; i++) {
569 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
570 if (dpaa2_q->q_storage)
571 rte_free(dpaa2_q->q_storage);
573 /*free the all queue memory */
574 rte_free(priv->rx_vq[0]);
575 priv->rx_vq[0] = NULL;
578 /* Allocate memory for storing MAC addresses */
579 if (eth_dev->data->mac_addrs) {
580 rte_free(eth_dev->data->mac_addrs);
581 eth_dev->data->mac_addrs = NULL;
584 /*Close the device at underlying layer*/
585 ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
587 PMD_INIT_LOG(ERR, "Failure closing dpni device with"
588 " error code %d\n", ret);
591 /*Free the allocated memory for ethernet private data and dpni*/
595 eth_dev->dev_ops = NULL;
601 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
602 struct rte_dpaa2_device *dpaa2_dev)
604 struct rte_eth_dev *eth_dev;
605 char ethdev_name[RTE_ETH_NAME_MAX_LEN];
609 sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
611 eth_dev = rte_eth_dev_allocate(ethdev_name);
615 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
616 eth_dev->data->dev_private = rte_zmalloc(
617 "ethdev private structure",
618 sizeof(struct dpaa2_dev_priv),
619 RTE_CACHE_LINE_SIZE);
620 if (eth_dev->data->dev_private == NULL) {
621 PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
622 " private port data\n");
623 rte_eth_dev_release_port(eth_dev);
627 eth_dev->device = &dpaa2_dev->device;
628 dpaa2_dev->eth_dev = eth_dev;
629 eth_dev->data->rx_mbuf_alloc_failed = 0;
631 /* Invoke PMD device initialization function */
632 diag = dpaa2_dev_init(eth_dev);
636 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
637 rte_free(eth_dev->data->dev_private);
638 rte_eth_dev_release_port(eth_dev);
643 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
645 struct rte_eth_dev *eth_dev;
647 eth_dev = dpaa2_dev->eth_dev;
648 dpaa2_dev_uninit(eth_dev);
650 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
651 rte_free(eth_dev->data->dev_private);
652 rte_eth_dev_release_port(eth_dev);
657 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
658 .drv_type = DPAA2_MC_DPNI_DEVID,
659 .probe = rte_dpaa2_probe,
660 .remove = rte_dpaa2_remove,
663 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);