event/sw: add configure function
[dpdk.git] / drivers / event / sw / sw_evdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. 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 Intel Corporation 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
33 #include <string.h>
34
35 #include <rte_vdev.h>
36 #include <rte_memzone.h>
37 #include <rte_kvargs.h>
38 #include <rte_ring.h>
39
40 #include "sw_evdev.h"
41
42 #define EVENTDEV_NAME_SW_PMD event_sw
43 #define NUMA_NODE_ARG "numa_node"
44 #define SCHED_QUANTA_ARG "sched_quanta"
45 #define CREDIT_QUANTA_ARG "credit_quanta"
46
47 static int
48 sw_dev_configure(const struct rte_eventdev *dev)
49 {
50         struct sw_evdev *sw = sw_pmd_priv(dev);
51         const struct rte_eventdev_data *data = dev->data;
52         const struct rte_event_dev_config *conf = &data->dev_conf;
53
54         sw->qid_count = conf->nb_event_queues;
55         sw->port_count = conf->nb_event_ports;
56         sw->nb_events_limit = conf->nb_events_limit;
57
58         if (conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT)
59                 return -ENOTSUP;
60
61         return 0;
62 }
63
64 static void
65 sw_info_get(struct rte_eventdev *dev, struct rte_event_dev_info *info)
66 {
67         RTE_SET_USED(dev);
68
69         static const struct rte_event_dev_info evdev_sw_info = {
70                         .driver_name = SW_PMD_NAME,
71                         .max_event_queues = RTE_EVENT_MAX_QUEUES_PER_DEV,
72                         .max_event_queue_flows = SW_QID_NUM_FIDS,
73                         .max_event_queue_priority_levels = SW_Q_PRIORITY_MAX,
74                         .max_event_priority_levels = SW_IQS_MAX,
75                         .max_event_ports = SW_PORTS_MAX,
76                         .max_event_port_dequeue_depth = MAX_SW_CONS_Q_DEPTH,
77                         .max_event_port_enqueue_depth = MAX_SW_PROD_Q_DEPTH,
78                         .max_num_events = SW_INFLIGHT_EVENTS_TOTAL,
79                         .event_dev_cap = (RTE_EVENT_DEV_CAP_QUEUE_QOS |
80                                         RTE_EVENT_DEV_CAP_EVENT_QOS),
81         };
82
83         *info = evdev_sw_info;
84 }
85
86 static int
87 assign_numa_node(const char *key __rte_unused, const char *value, void *opaque)
88 {
89         int *socket_id = opaque;
90         *socket_id = atoi(value);
91         if (*socket_id >= RTE_MAX_NUMA_NODES)
92                 return -1;
93         return 0;
94 }
95
96 static int
97 set_sched_quanta(const char *key __rte_unused, const char *value, void *opaque)
98 {
99         int *quanta = opaque;
100         *quanta = atoi(value);
101         if (*quanta < 0 || *quanta >= 4096)
102                 return -1;
103         return 0;
104 }
105
106 static int
107 set_credit_quanta(const char *key __rte_unused, const char *value, void *opaque)
108 {
109         int *credit = opaque;
110         *credit = atoi(value);
111         if (*credit < 0 || *credit >= 128)
112                 return -1;
113         return 0;
114 }
115
116 static int
117 sw_probe(const char *name, const char *params)
118 {
119         static const struct rte_eventdev_ops evdev_sw_ops = {
120                         .dev_configure = sw_dev_configure,
121                         .dev_infos_get = sw_info_get,
122         };
123
124         static const char *const args[] = {
125                 NUMA_NODE_ARG,
126                 SCHED_QUANTA_ARG,
127                 CREDIT_QUANTA_ARG,
128                 NULL
129         };
130         struct rte_eventdev *dev;
131         struct sw_evdev *sw;
132         int socket_id = rte_socket_id();
133         int sched_quanta  = SW_DEFAULT_SCHED_QUANTA;
134         int credit_quanta = SW_DEFAULT_CREDIT_QUANTA;
135
136         if (params != NULL && params[0] != '\0') {
137                 struct rte_kvargs *kvlist = rte_kvargs_parse(params, args);
138
139                 if (!kvlist) {
140                         SW_LOG_INFO(
141                                 "Ignoring unsupported parameters when creating device '%s'\n",
142                                 name);
143                 } else {
144                         int ret = rte_kvargs_process(kvlist, NUMA_NODE_ARG,
145                                         assign_numa_node, &socket_id);
146                         if (ret != 0) {
147                                 SW_LOG_ERR(
148                                         "%s: Error parsing numa node parameter",
149                                         name);
150                                 rte_kvargs_free(kvlist);
151                                 return ret;
152                         }
153
154                         ret = rte_kvargs_process(kvlist, SCHED_QUANTA_ARG,
155                                         set_sched_quanta, &sched_quanta);
156                         if (ret != 0) {
157                                 SW_LOG_ERR(
158                                         "%s: Error parsing sched quanta parameter",
159                                         name);
160                                 rte_kvargs_free(kvlist);
161                                 return ret;
162                         }
163
164                         ret = rte_kvargs_process(kvlist, CREDIT_QUANTA_ARG,
165                                         set_credit_quanta, &credit_quanta);
166                         if (ret != 0) {
167                                 SW_LOG_ERR(
168                                         "%s: Error parsing credit quanta parameter",
169                                         name);
170                                 rte_kvargs_free(kvlist);
171                                 return ret;
172                         }
173
174                         rte_kvargs_free(kvlist);
175                 }
176         }
177
178         SW_LOG_INFO(
179                         "Creating eventdev sw device %s, numa_node=%d, sched_quanta=%d, credit_quanta=%d\n",
180                         name, socket_id, sched_quanta, credit_quanta);
181
182         dev = rte_event_pmd_vdev_init(name,
183                         sizeof(struct sw_evdev), socket_id);
184         if (dev == NULL) {
185                 SW_LOG_ERR("eventdev vdev init() failed");
186                 return -EFAULT;
187         }
188         dev->dev_ops = &evdev_sw_ops;
189
190         sw = dev->data->dev_private;
191         sw->data = dev->data;
192
193         /* copy values passed from vdev command line to instance */
194         sw->credit_update_quanta = credit_quanta;
195         sw->sched_quanta = sched_quanta;
196
197         return 0;
198 }
199
200 static int
201 sw_remove(const char *name)
202 {
203         if (name == NULL)
204                 return -EINVAL;
205
206         SW_LOG_INFO("Closing eventdev sw device %s\n", name);
207
208         return rte_event_pmd_vdev_uninit(name);
209 }
210
211 static struct rte_vdev_driver evdev_sw_pmd_drv = {
212         .probe = sw_probe,
213         .remove = sw_remove
214 };
215
216 RTE_PMD_REGISTER_VDEV(EVENTDEV_NAME_SW_PMD, evdev_sw_pmd_drv);
217 RTE_PMD_REGISTER_PARAM_STRING(event_sw, NUMA_NODE_ARG "=<int> "
218                 SCHED_QUANTA_ARG "=<int>" CREDIT_QUANTA_ARG "=<int>");