net/octeontx: create ethdev ports
[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 int
109 octeontx_port_open(struct octeontx_nic *nic)
110 {
111         octeontx_mbox_bgx_port_conf_t bgx_port_conf;
112         int res;
113
114         res = 0;
115
116         PMD_INIT_FUNC_TRACE();
117
118         res = octeontx_bgx_port_open(nic->port_id, &bgx_port_conf);
119         if (res < 0) {
120                 octeontx_log_err("failed to open port %d", res);
121                 return res;
122         }
123
124         nic->node = bgx_port_conf.node;
125         nic->port_ena = bgx_port_conf.enable;
126         nic->base_ichan = bgx_port_conf.base_chan;
127         nic->base_ochan = bgx_port_conf.base_chan;
128         nic->num_ichans = bgx_port_conf.num_chans;
129         nic->num_ochans = bgx_port_conf.num_chans;
130         nic->mtu = bgx_port_conf.mtu;
131         nic->bpen = bgx_port_conf.bpen;
132         nic->fcs_strip = bgx_port_conf.fcs_strip;
133         nic->bcast_mode = bgx_port_conf.bcast_mode;
134         nic->mcast_mode = bgx_port_conf.mcast_mode;
135         nic->speed      = bgx_port_conf.mode;
136
137         memcpy(&nic->mac_addr[0], &bgx_port_conf.macaddr[0], ETHER_ADDR_LEN);
138
139         octeontx_log_dbg("port opened %d", nic->port_id);
140         return res;
141 }
142
143 static void
144 octeontx_port_close(struct octeontx_nic *nic)
145 {
146         PMD_INIT_FUNC_TRACE();
147
148         octeontx_bgx_port_close(nic->port_id);
149         octeontx_log_dbg("port closed %d", nic->port_id);
150 }
151
152 static inline void
153 devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
154                                 struct rte_event_dev_info *info)
155 {
156         memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
157         dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
158
159         dev_conf->nb_event_ports = info->max_event_ports;
160         dev_conf->nb_event_queues = info->max_event_queues;
161
162         dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
163         dev_conf->nb_event_port_dequeue_depth =
164                         info->max_event_port_dequeue_depth;
165         dev_conf->nb_event_port_enqueue_depth =
166                         info->max_event_port_enqueue_depth;
167         dev_conf->nb_event_port_enqueue_depth =
168                         info->max_event_port_enqueue_depth;
169         dev_conf->nb_events_limit =
170                         info->max_num_events;
171 }
172
173 /* Initialize and register driver with DPDK Application */
174 static const struct eth_dev_ops octeontx_dev_ops = {
175 };
176
177 /* Create Ethdev interface per BGX LMAC ports */
178 static int
179 octeontx_create(struct rte_vdev_device *dev, int port, uint8_t evdev,
180                         int socket_id)
181 {
182         int res;
183         char octtx_name[OCTEONTX_MAX_NAME_LEN];
184         struct octeontx_nic *nic = NULL;
185         struct rte_eth_dev *eth_dev = NULL;
186         struct rte_eth_dev_data *data = NULL;
187         const char *name = rte_vdev_device_name(dev);
188
189         PMD_INIT_FUNC_TRACE();
190
191         sprintf(octtx_name, "%s_%d", name, port);
192         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
193                 eth_dev = rte_eth_dev_attach_secondary(octtx_name);
194                 if (eth_dev == NULL)
195                         return -ENODEV;
196
197                 return 0;
198         }
199
200         data = rte_zmalloc_socket(octtx_name, sizeof(*data), 0, socket_id);
201         if (data == NULL) {
202                 octeontx_log_err("failed to allocate devdata");
203                 res = -ENOMEM;
204                 goto err;
205         }
206
207         nic = rte_zmalloc_socket(octtx_name, sizeof(*nic), 0, socket_id);
208         if (nic == NULL) {
209                 octeontx_log_err("failed to allocate nic structure");
210                 res = -ENOMEM;
211                 goto err;
212         }
213
214         nic->port_id = port;
215         nic->evdev = evdev;
216
217         res = octeontx_port_open(nic);
218         if (res < 0)
219                 goto err;
220
221         /* Rx side port configuration */
222         res = octeontx_pki_port_open(port);
223         if (res != 0) {
224                 octeontx_log_err("failed to open PKI port %d", port);
225                 res = -ENODEV;
226                 goto err;
227         }
228
229         /* Reserve an ethdev entry */
230         eth_dev = rte_eth_dev_allocate(octtx_name);
231         if (eth_dev == NULL) {
232                 octeontx_log_err("failed to allocate rte_eth_dev");
233                 res = -ENOMEM;
234                 goto err;
235         }
236
237         eth_dev->device = &dev->device;
238         eth_dev->intr_handle = NULL;
239         eth_dev->data->kdrv = RTE_KDRV_NONE;
240         eth_dev->data->numa_node = dev->device.numa_node;
241
242         rte_memcpy(data, (eth_dev)->data, sizeof(*data));
243         data->dev_private = nic;
244
245         data->port_id = eth_dev->data->port_id;
246         snprintf(data->name, sizeof(data->name), "%s", eth_dev->data->name);
247
248         nic->ev_queues = 1;
249         nic->ev_ports = 1;
250
251         data->dev_link.link_status = ETH_LINK_DOWN;
252         data->dev_started = 0;
253         data->promiscuous = 0;
254         data->all_multicast = 0;
255         data->scattered_rx = 0;
256
257         data->mac_addrs = rte_zmalloc_socket(octtx_name, ETHER_ADDR_LEN, 0,
258                                                         socket_id);
259         if (data->mac_addrs == NULL) {
260                 octeontx_log_err("failed to allocate memory for mac_addrs");
261                 res = -ENOMEM;
262                 goto err;
263         }
264
265         eth_dev->data = data;
266         eth_dev->dev_ops = &octeontx_dev_ops;
267
268         /* Finally save ethdev pointer to the NIC structure */
269         nic->dev = eth_dev;
270
271         if (nic->port_id != data->port_id) {
272                 octeontx_log_err("eth_dev->port_id (%d) is diff to orig (%d)",
273                                 data->port_id, nic->port_id);
274                 res = -EINVAL;
275                 goto err;
276         }
277
278         /* Update port_id mac to eth_dev */
279         memcpy(data->mac_addrs, nic->mac_addr, ETHER_ADDR_LEN);
280
281         PMD_INIT_LOG(DEBUG, "ethdev info: ");
282         PMD_INIT_LOG(DEBUG, "port %d, port_ena %d ochan %d num_ochan %d tx_q %d",
283                                 nic->port_id, nic->port_ena,
284                                 nic->base_ochan, nic->num_ochans,
285                                 nic->num_tx_queues);
286         PMD_INIT_LOG(DEBUG, "speed %d mtu %d", nic->speed, nic->mtu);
287
288         return data->port_id;
289
290 err:
291         if (port)
292                 octeontx_port_close(nic);
293
294         if (eth_dev != NULL) {
295                 rte_free(eth_dev->data->mac_addrs);
296                 rte_free(data);
297                 rte_free(nic);
298                 rte_eth_dev_release_port(eth_dev);
299         }
300
301         return res;
302 }
303
304 /* Un initialize octeontx device */
305 static int
306 octeontx_remove(struct rte_vdev_device *dev)
307 {
308         char octtx_name[OCTEONTX_MAX_NAME_LEN];
309         struct rte_eth_dev *eth_dev = NULL;
310         struct octeontx_nic *nic = NULL;
311         int i;
312
313         if (dev == NULL)
314                 return -EINVAL;
315
316         for (i = 0; i < OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT; i++) {
317                 sprintf(octtx_name, "eth_octeontx_%d", i);
318
319                 /* reserve an ethdev entry */
320                 eth_dev = rte_eth_dev_allocated(octtx_name);
321                 if (eth_dev == NULL)
322                         return -ENODEV;
323
324                 nic = octeontx_pmd_priv(eth_dev);
325                 rte_event_dev_stop(nic->evdev);
326                 PMD_INIT_LOG(INFO, "Closing octeontx device %s", octtx_name);
327
328                 rte_free(eth_dev->data->mac_addrs);
329                 rte_free(eth_dev->data->dev_private);
330                 rte_free(eth_dev->data);
331                 rte_eth_dev_release_port(eth_dev);
332                 rte_event_dev_close(nic->evdev);
333         }
334
335         /* Free FC resource */
336         octeontx_pko_fc_free();
337
338         return 0;
339 }
340
341 /* Initialize octeontx device */
342 static int
343 octeontx_probe(struct rte_vdev_device *dev)
344 {
345         const char *dev_name;
346         static int probe_once;
347         uint8_t socket_id, qlist;
348         int tx_vfcnt, port_id, evdev, qnum, pnum, res, i;
349         struct rte_event_dev_config dev_conf;
350         const char *eventdev_name = "event_octeontx";
351         struct rte_event_dev_info info;
352
353         struct octeontx_vdev_init_params init_params = {
354                 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT
355         };
356
357         dev_name = rte_vdev_device_name(dev);
358         res = octeontx_parse_vdev_init_params(&init_params, dev);
359         if (res < 0)
360                 return -EINVAL;
361
362         if (init_params.nr_port > OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT) {
363                 octeontx_log_err("nr_port (%d) > max (%d)", init_params.nr_port,
364                                 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT);
365                 return -ENOTSUP;
366         }
367
368         PMD_INIT_LOG(DEBUG, "initializing %s pmd", dev_name);
369
370         socket_id = rte_socket_id();
371
372         tx_vfcnt = octeontx_pko_vf_count();
373
374         if (tx_vfcnt < init_params.nr_port) {
375                 octeontx_log_err("not enough PKO (%d) for port number (%d)",
376                                 tx_vfcnt, init_params.nr_port);
377                 return -EINVAL;
378         }
379         evdev = rte_event_dev_get_dev_id(eventdev_name);
380         if (evdev < 0) {
381                 octeontx_log_err("eventdev %s not found", eventdev_name);
382                 return -ENODEV;
383         }
384
385         res = rte_event_dev_info_get(evdev, &info);
386         if (res < 0) {
387                 octeontx_log_err("failed to eventdev info %d", res);
388                 return -EINVAL;
389         }
390
391         PMD_INIT_LOG(DEBUG, "max_queue %d max_port %d",
392                         info.max_event_queues, info.max_event_ports);
393
394         if (octeontx_pko_init_fc(tx_vfcnt))
395                 return -ENOMEM;
396
397         devconf_set_default_sane_values(&dev_conf, &info);
398         res = rte_event_dev_configure(evdev, &dev_conf);
399         if (res < 0)
400                 goto parse_error;
401
402         rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_PORT_COUNT,
403                         (uint32_t *)&pnum);
404         rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_QUEUE_COUNT,
405                         (uint32_t *)&qnum);
406         if (pnum < qnum) {
407                 octeontx_log_err("too few event ports (%d) for event_q(%d)",
408                                 pnum, qnum);
409                 res = -EINVAL;
410                 goto parse_error;
411         }
412         if (pnum > qnum) {
413                 /*
414                  * We don't poll on event ports
415                  * that do not have any queues assigned.
416                  */
417                 pnum = qnum;
418                 PMD_INIT_LOG(INFO,
419                         "reducing number of active event ports to %d", pnum);
420         }
421         for (i = 0; i < qnum; i++) {
422                 res = rte_event_queue_setup(evdev, i, NULL);
423                 if (res < 0) {
424                         octeontx_log_err("failed to setup event_q(%d): res %d",
425                                         i, res);
426                         goto parse_error;
427                 }
428         }
429
430         for (i = 0; i < pnum; i++) {
431                 res = rte_event_port_setup(evdev, i, NULL);
432                 if (res < 0) {
433                         res = -ENODEV;
434                         octeontx_log_err("failed to setup ev port(%d) res=%d",
435                                                 i, res);
436                         goto parse_error;
437                 }
438                 /* Link one queue to one event port */
439                 qlist = i;
440                 res = rte_event_port_link(evdev, i, &qlist, NULL, 1);
441                 if (res < 0) {
442                         res = -ENODEV;
443                         octeontx_log_err("failed to link port (%d): res=%d",
444                                         i, res);
445                         goto parse_error;
446                 }
447         }
448
449         /* Create ethdev interface */
450         for (i = 0; i < init_params.nr_port; i++) {
451                 port_id = octeontx_create(dev, i, evdev, socket_id);
452                 if (port_id < 0) {
453                         octeontx_log_err("failed to create device %s",
454                                         dev_name);
455                         res = -ENODEV;
456                         goto parse_error;
457                 }
458
459                 PMD_INIT_LOG(INFO, "created ethdev %s for port %d", dev_name,
460                                         port_id);
461         }
462
463         if (probe_once) {
464                 octeontx_log_err("interface %s not supported", dev_name);
465                 octeontx_remove(dev);
466                 res = -ENOTSUP;
467                 goto parse_error;
468         }
469         probe_once = 1;
470
471         return 0;
472
473 parse_error:
474         octeontx_pko_fc_free();
475         return res;
476 }
477
478 static struct rte_vdev_driver octeontx_pmd_drv = {
479         .probe = octeontx_probe,
480         .remove = octeontx_remove,
481 };
482
483 RTE_PMD_REGISTER_VDEV(OCTEONTX_PMD, octeontx_pmd_drv);
484 RTE_PMD_REGISTER_ALIAS(OCTEONTX_PMD, eth_octeontx);
485 RTE_PMD_REGISTER_PARAM_STRING(OCTEONTX_PMD, "nr_port=<int> ");