net/dpaa2: introduce NXP DPAA2 driver
[dpdk.git] / drivers / net / dpaa2 / dpaa2_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright (c) 2016 NXP. 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 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.
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 <time.h>
35 #include <net/if.h>
36
37 #include <rte_mbuf.h>
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>
44 #include <rte_dev.h>
45 #include <rte_ethdev.h>
46 #include <rte_fslmc.h>
47
48 #include <fslmc_vfio.h>
49 #include "dpaa2_ethdev.h"
50
51 static struct rte_dpaa2_driver rte_dpaa2_pmd;
52
53 static int
54 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
55 {
56         /* For secondary processes, the primary has done all the work */
57         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
58                 return 0;
59
60         eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
61
62         return 0;
63 }
64
65 static int
66 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev __rte_unused)
67 {
68         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
69                 return -EPERM;
70
71         return 0;
72 }
73
74 static int
75 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
76                 struct rte_dpaa2_device *dpaa2_dev)
77 {
78         struct rte_eth_dev *eth_dev;
79         char ethdev_name[RTE_ETH_NAME_MAX_LEN];
80
81         int diag;
82
83         sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
84
85         eth_dev = rte_eth_dev_allocate(ethdev_name);
86         if (eth_dev == NULL)
87                 return -ENOMEM;
88
89         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
90                 eth_dev->data->dev_private = rte_zmalloc(
91                                                 "ethdev private structure",
92                                                 sizeof(struct dpaa2_dev_priv),
93                                                 RTE_CACHE_LINE_SIZE);
94                 if (eth_dev->data->dev_private == NULL) {
95                         RTE_LOG(CRIT, PMD, "Cannot allocate memzone for"
96                                 " private port data\n");
97                         rte_eth_dev_release_port(eth_dev);
98                         return -ENOMEM;
99                 }
100         }
101         eth_dev->device = &dpaa2_dev->device;
102         dpaa2_dev->eth_dev = eth_dev;
103         eth_dev->data->rx_mbuf_alloc_failed = 0;
104
105         /* Invoke PMD device initialization function */
106         diag = dpaa2_dev_init(eth_dev);
107         if (diag == 0)
108                 return 0;
109
110         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
111                 rte_free(eth_dev->data->dev_private);
112         rte_eth_dev_release_port(eth_dev);
113         return diag;
114 }
115
116 static int
117 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
118 {
119         struct rte_eth_dev *eth_dev;
120
121         eth_dev = dpaa2_dev->eth_dev;
122         dpaa2_dev_uninit(eth_dev);
123
124         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
125                 rte_free(eth_dev->data->dev_private);
126         rte_eth_dev_release_port(eth_dev);
127
128         return 0;
129 }
130
131 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
132         .drv_type = DPAA2_MC_DPNI_DEVID,
133         .probe = rte_dpaa2_probe,
134         .remove = rte_dpaa2_remove,
135 };
136
137 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);