net/octeontx: add net device probe and remove
[dpdk.git] / drivers / net / octeontx / octeontx_ethdev.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium Inc. 2017. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium networks nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include <rte_alarm.h>
40 #include <rte_branch_prediction.h>
41 #include <rte_debug.h>
42 #include <rte_devargs.h>
43 #include <rte_dev.h>
44 #include <rte_kvargs.h>
45 #include <rte_malloc.h>
46 #include <rte_prefetch.h>
47 #include <rte_vdev.h>
48
49 #include "octeontx_ethdev.h"
50 #include "octeontx_logs.h"
51
52 struct octeontx_vdev_init_params {
53         uint8_t nr_port;
54 };
55
56 /* Parse integer from integer argument */
57 static int
58 parse_integer_arg(const char *key __rte_unused,
59                 const char *value, void *extra_args)
60 {
61         int *i = (int *)extra_args;
62
63         *i = atoi(value);
64         if (*i < 0) {
65                 octeontx_log_err("argument has to be positive.");
66                 return -1;
67         }
68
69         return 0;
70 }
71
72 static int
73 octeontx_parse_vdev_init_params(struct octeontx_vdev_init_params *params,
74                                 struct rte_vdev_device *dev)
75 {
76         struct rte_kvargs *kvlist = NULL;
77         int ret = 0;
78
79         static const char * const octeontx_vdev_valid_params[] = {
80                 OCTEONTX_VDEV_NR_PORT_ARG,
81                 NULL
82         };
83
84         const char *input_args = rte_vdev_device_args(dev);
85         if (params == NULL)
86                 return -EINVAL;
87
88
89         if (input_args) {
90                 kvlist = rte_kvargs_parse(input_args,
91                                 octeontx_vdev_valid_params);
92                 if (kvlist == NULL)
93                         return -1;
94
95                 ret = rte_kvargs_process(kvlist,
96                                         OCTEONTX_VDEV_NR_PORT_ARG,
97                                         &parse_integer_arg,
98                                         &params->nr_port);
99                 if (ret < 0)
100                         goto free_kvlist;
101         }
102
103 free_kvlist:
104         rte_kvargs_free(kvlist);
105         return ret;
106 }
107
108 static inline void
109 devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
110                                 struct rte_event_dev_info *info)
111 {
112         memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
113         dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
114
115         dev_conf->nb_event_ports = info->max_event_ports;
116         dev_conf->nb_event_queues = info->max_event_queues;
117
118         dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
119         dev_conf->nb_event_port_dequeue_depth =
120                         info->max_event_port_dequeue_depth;
121         dev_conf->nb_event_port_enqueue_depth =
122                         info->max_event_port_enqueue_depth;
123         dev_conf->nb_event_port_enqueue_depth =
124                         info->max_event_port_enqueue_depth;
125         dev_conf->nb_events_limit =
126                         info->max_num_events;
127 }
128
129 /* Create Ethdev interface per BGX LMAC ports */
130 static int
131 octeontx_create(struct rte_vdev_device *dev, int port, uint8_t evdev,
132                         int socket_id)
133 {
134         RTE_SET_USED(dev);
135         RTE_SET_USED(port);
136         RTE_SET_USED(evdev);
137         RTE_SET_USED(socket_id);
138
139         return -ENODEV;
140 }
141
142 /* Un initialize octeontx device */
143 static int
144 octeontx_remove(struct rte_vdev_device *dev)
145 {
146         char octtx_name[OCTEONTX_MAX_NAME_LEN];
147         struct rte_eth_dev *eth_dev = NULL;
148         struct octeontx_nic *nic = NULL;
149         int i;
150
151         if (dev == NULL)
152                 return -EINVAL;
153
154         for (i = 0; i < OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT; i++) {
155                 sprintf(octtx_name, "eth_octeontx_%d", i);
156
157                 /* reserve an ethdev entry */
158                 eth_dev = rte_eth_dev_allocated(octtx_name);
159                 if (eth_dev == NULL)
160                         return -ENODEV;
161
162                 nic = octeontx_pmd_priv(eth_dev);
163                 rte_event_dev_stop(nic->evdev);
164                 PMD_INIT_LOG(INFO, "Closing octeontx device %s", octtx_name);
165
166                 rte_free(eth_dev->data->mac_addrs);
167                 rte_free(eth_dev->data->dev_private);
168                 rte_free(eth_dev->data);
169                 rte_eth_dev_release_port(eth_dev);
170                 rte_event_dev_close(nic->evdev);
171         }
172
173         /* Free FC resource */
174         octeontx_pko_fc_free();
175
176         return 0;
177 }
178
179 /* Initialize octeontx device */
180 static int
181 octeontx_probe(struct rte_vdev_device *dev)
182 {
183         const char *dev_name;
184         static int probe_once;
185         uint8_t socket_id, qlist;
186         int tx_vfcnt, port_id, evdev, qnum, pnum, res, i;
187         struct rte_event_dev_config dev_conf;
188         const char *eventdev_name = "event_octeontx";
189         struct rte_event_dev_info info;
190
191         struct octeontx_vdev_init_params init_params = {
192                 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT
193         };
194
195         dev_name = rte_vdev_device_name(dev);
196         res = octeontx_parse_vdev_init_params(&init_params, dev);
197         if (res < 0)
198                 return -EINVAL;
199
200         if (init_params.nr_port > OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT) {
201                 octeontx_log_err("nr_port (%d) > max (%d)", init_params.nr_port,
202                                 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT);
203                 return -ENOTSUP;
204         }
205
206         PMD_INIT_LOG(DEBUG, "initializing %s pmd", dev_name);
207
208         socket_id = rte_socket_id();
209
210         tx_vfcnt = octeontx_pko_vf_count();
211
212         if (tx_vfcnt < init_params.nr_port) {
213                 octeontx_log_err("not enough PKO (%d) for port number (%d)",
214                                 tx_vfcnt, init_params.nr_port);
215                 return -EINVAL;
216         }
217         evdev = rte_event_dev_get_dev_id(eventdev_name);
218         if (evdev < 0) {
219                 octeontx_log_err("eventdev %s not found", eventdev_name);
220                 return -ENODEV;
221         }
222
223         res = rte_event_dev_info_get(evdev, &info);
224         if (res < 0) {
225                 octeontx_log_err("failed to eventdev info %d", res);
226                 return -EINVAL;
227         }
228
229         PMD_INIT_LOG(DEBUG, "max_queue %d max_port %d",
230                         info.max_event_queues, info.max_event_ports);
231
232         if (octeontx_pko_init_fc(tx_vfcnt))
233                 return -ENOMEM;
234
235         devconf_set_default_sane_values(&dev_conf, &info);
236         res = rte_event_dev_configure(evdev, &dev_conf);
237         if (res < 0)
238                 goto parse_error;
239
240         rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_PORT_COUNT,
241                         (uint32_t *)&pnum);
242         rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_QUEUE_COUNT,
243                         (uint32_t *)&qnum);
244         if (pnum < qnum) {
245                 octeontx_log_err("too few event ports (%d) for event_q(%d)",
246                                 pnum, qnum);
247                 res = -EINVAL;
248                 goto parse_error;
249         }
250         if (pnum > qnum) {
251                 /*
252                  * We don't poll on event ports
253                  * that do not have any queues assigned.
254                  */
255                 pnum = qnum;
256                 PMD_INIT_LOG(INFO,
257                         "reducing number of active event ports to %d", pnum);
258         }
259         for (i = 0; i < qnum; i++) {
260                 res = rte_event_queue_setup(evdev, i, NULL);
261                 if (res < 0) {
262                         octeontx_log_err("failed to setup event_q(%d): res %d",
263                                         i, res);
264                         goto parse_error;
265                 }
266         }
267
268         for (i = 0; i < pnum; i++) {
269                 res = rte_event_port_setup(evdev, i, NULL);
270                 if (res < 0) {
271                         res = -ENODEV;
272                         octeontx_log_err("failed to setup ev port(%d) res=%d",
273                                                 i, res);
274                         goto parse_error;
275                 }
276                 /* Link one queue to one event port */
277                 qlist = i;
278                 res = rte_event_port_link(evdev, i, &qlist, NULL, 1);
279                 if (res < 0) {
280                         res = -ENODEV;
281                         octeontx_log_err("failed to link port (%d): res=%d",
282                                         i, res);
283                         goto parse_error;
284                 }
285         }
286
287         /* Create ethdev interface */
288         for (i = 0; i < init_params.nr_port; i++) {
289                 port_id = octeontx_create(dev, i, evdev, socket_id);
290                 if (port_id < 0) {
291                         octeontx_log_err("failed to create device %s",
292                                         dev_name);
293                         res = -ENODEV;
294                         goto parse_error;
295                 }
296
297                 PMD_INIT_LOG(INFO, "created ethdev %s for port %d", dev_name,
298                                         port_id);
299         }
300
301         if (probe_once) {
302                 octeontx_log_err("interface %s not supported", dev_name);
303                 octeontx_remove(dev);
304                 res = -ENOTSUP;
305                 goto parse_error;
306         }
307         probe_once = 1;
308
309         return 0;
310
311 parse_error:
312         octeontx_pko_fc_free();
313         return res;
314 }
315
316 static struct rte_vdev_driver octeontx_pmd_drv = {
317         .probe = octeontx_probe,
318         .remove = octeontx_remove,
319 };
320
321 RTE_PMD_REGISTER_VDEV(OCTEONTX_PMD, octeontx_pmd_drv);
322 RTE_PMD_REGISTER_ALIAS(OCTEONTX_PMD, eth_octeontx);
323 RTE_PMD_REGISTER_PARAM_STRING(OCTEONTX_PMD, "nr_port=<int> ");