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