net/dpaa: add NXP DPAA PMD driver skeleton
[dpdk.git] / drivers / net / dpaa / dpaa_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright 2017 NXP.
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 /* System headers */
34 #include <stdio.h>
35 #include <inttypes.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <sched.h>
39 #include <signal.h>
40 #include <pthread.h>
41 #include <sys/types.h>
42 #include <sys/syscall.h>
43
44 #include <rte_config.h>
45 #include <rte_byteorder.h>
46 #include <rte_common.h>
47 #include <rte_interrupts.h>
48 #include <rte_log.h>
49 #include <rte_debug.h>
50 #include <rte_pci.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_tailq.h>
56 #include <rte_eal.h>
57 #include <rte_alarm.h>
58 #include <rte_ether.h>
59 #include <rte_ethdev.h>
60 #include <rte_malloc.h>
61 #include <rte_ring.h>
62
63 #include <rte_dpaa_bus.h>
64 #include <rte_dpaa_logs.h>
65
66 #include <dpaa_ethdev.h>
67
68 /* Keep track of whether QMAN and BMAN have been globally initialized */
69 static int is_global_init;
70
71 static int
72 dpaa_eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
73 {
74         PMD_INIT_FUNC_TRACE();
75
76         return 0;
77 }
78
79 static int dpaa_eth_dev_start(struct rte_eth_dev *dev)
80 {
81         PMD_INIT_FUNC_TRACE();
82
83         /* Change tx callback to the real one */
84         dev->tx_pkt_burst = NULL;
85
86         return 0;
87 }
88
89 static void dpaa_eth_dev_stop(struct rte_eth_dev *dev)
90 {
91         dev->tx_pkt_burst = NULL;
92 }
93
94 static void dpaa_eth_dev_close(struct rte_eth_dev *dev __rte_unused)
95 {
96         PMD_INIT_FUNC_TRACE();
97 }
98
99 static struct eth_dev_ops dpaa_devops = {
100         .dev_configure            = dpaa_eth_dev_configure,
101         .dev_start                = dpaa_eth_dev_start,
102         .dev_stop                 = dpaa_eth_dev_stop,
103         .dev_close                = dpaa_eth_dev_close,
104 };
105
106 /* Initialise a network interface */
107 static int
108 dpaa_dev_init(struct rte_eth_dev *eth_dev)
109 {
110         int dev_id;
111         struct rte_dpaa_device *dpaa_device;
112         struct dpaa_if *dpaa_intf;
113
114         PMD_INIT_FUNC_TRACE();
115
116         /* For secondary processes, the primary has done all the work */
117         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
118                 return 0;
119
120         dpaa_device = DEV_TO_DPAA_DEVICE(eth_dev->device);
121         dev_id = dpaa_device->id.dev_id;
122         dpaa_intf = eth_dev->data->dev_private;
123
124         dpaa_intf->name = dpaa_device->name;
125
126         dpaa_intf->ifid = dev_id;
127
128         eth_dev->dev_ops = &dpaa_devops;
129
130         return 0;
131 }
132
133 static int
134 dpaa_dev_uninit(struct rte_eth_dev *dev)
135 {
136         struct dpaa_if *dpaa_intf = dev->data->dev_private;
137
138         PMD_INIT_FUNC_TRACE();
139
140         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
141                 return -EPERM;
142
143         if (!dpaa_intf) {
144                 DPAA_PMD_WARN("Already closed or not started");
145                 return -1;
146         }
147
148         dpaa_eth_dev_close(dev);
149
150         dev->dev_ops = NULL;
151         dev->rx_pkt_burst = NULL;
152         dev->tx_pkt_burst = NULL;
153
154         return 0;
155 }
156
157 static int
158 rte_dpaa_probe(struct rte_dpaa_driver *dpaa_drv,
159                struct rte_dpaa_device *dpaa_dev)
160 {
161         int diag;
162         int ret;
163         struct rte_eth_dev *eth_dev;
164
165         PMD_INIT_FUNC_TRACE();
166
167         /* In case of secondary process, the device is already configured
168          * and no further action is required, except portal initialization
169          * and verifying secondary attachment to port name.
170          */
171         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
172                 eth_dev = rte_eth_dev_attach_secondary(dpaa_dev->name);
173                 if (!eth_dev)
174                         return -ENOMEM;
175                 return 0;
176         }
177
178         if (!is_global_init) {
179                 /* One time load of Qman/Bman drivers */
180                 ret = qman_global_init();
181                 if (ret) {
182                         DPAA_PMD_ERR("QMAN initialization failed: %d",
183                                      ret);
184                         return ret;
185                 }
186                 ret = bman_global_init();
187                 if (ret) {
188                         DPAA_PMD_ERR("BMAN initialization failed: %d",
189                                      ret);
190                         return ret;
191                 }
192
193                 is_global_init = 1;
194         }
195
196         ret = rte_dpaa_portal_init((void *)1);
197         if (ret) {
198                 DPAA_PMD_ERR("Unable to initialize portal");
199                 return ret;
200         }
201
202         eth_dev = rte_eth_dev_allocate(dpaa_dev->name);
203         if (eth_dev == NULL)
204                 return -ENOMEM;
205
206         eth_dev->data->dev_private = rte_zmalloc(
207                                         "ethdev private structure",
208                                         sizeof(struct dpaa_if),
209                                         RTE_CACHE_LINE_SIZE);
210         if (!eth_dev->data->dev_private) {
211                 DPAA_PMD_ERR("Cannot allocate memzone for port data");
212                 rte_eth_dev_release_port(eth_dev);
213                 return -ENOMEM;
214         }
215
216         eth_dev->device = &dpaa_dev->device;
217         eth_dev->device->driver = &dpaa_drv->driver;
218         dpaa_dev->eth_dev = eth_dev;
219
220         /* Invoke PMD device initialization function */
221         diag = dpaa_dev_init(eth_dev);
222         if (diag == 0)
223                 return 0;
224
225         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
226                 rte_free(eth_dev->data->dev_private);
227
228         rte_eth_dev_release_port(eth_dev);
229         return diag;
230 }
231
232 static int
233 rte_dpaa_remove(struct rte_dpaa_device *dpaa_dev)
234 {
235         struct rte_eth_dev *eth_dev;
236
237         PMD_INIT_FUNC_TRACE();
238
239         eth_dev = dpaa_dev->eth_dev;
240         dpaa_dev_uninit(eth_dev);
241
242         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
243                 rte_free(eth_dev->data->dev_private);
244
245         rte_eth_dev_release_port(eth_dev);
246
247         return 0;
248 }
249
250 static struct rte_dpaa_driver rte_dpaa_pmd = {
251         .drv_type = FSL_DPAA_ETH,
252         .probe = rte_dpaa_probe,
253         .remove = rte_dpaa_remove,
254 };
255
256 RTE_PMD_REGISTER_DPAA(net_dpaa, rte_dpaa_pmd);