New upstream version 17.11-rc3
[deb_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 <inttypes.h>
34 #include <string.h>
35
36 #include <rte_bus_vdev.h>
37 #include <rte_kvargs.h>
38 #include <rte_ring.h>
39 #include <rte_errno.h>
40 #include <rte_event_ring.h>
41 #include <rte_service_component.h>
42
43 #include "sw_evdev.h"
44 #include "iq_ring.h"
45
46 #define EVENTDEV_NAME_SW_PMD event_sw
47 #define NUMA_NODE_ARG "numa_node"
48 #define SCHED_QUANTA_ARG "sched_quanta"
49 #define CREDIT_QUANTA_ARG "credit_quanta"
50
51 static void
52 sw_info_get(struct rte_eventdev *dev, struct rte_event_dev_info *info);
53
54 static int
55 sw_port_link(struct rte_eventdev *dev, void *port, const uint8_t queues[],
56                 const uint8_t priorities[], uint16_t num)
57 {
58         struct sw_port *p = port;
59         struct sw_evdev *sw = sw_pmd_priv(dev);
60         int i;
61
62         RTE_SET_USED(priorities);
63         for (i = 0; i < num; i++) {
64                 struct sw_qid *q = &sw->qids[queues[i]];
65
66                 /* check for qid map overflow */
67                 if (q->cq_num_mapped_cqs >= RTE_DIM(q->cq_map)) {
68                         rte_errno = -EDQUOT;
69                         break;
70                 }
71
72                 if (p->is_directed && p->num_qids_mapped > 0) {
73                         rte_errno = -EDQUOT;
74                         break;
75                 }
76
77                 if (q->type == SW_SCHED_TYPE_DIRECT) {
78                         /* check directed qids only map to one port */
79                         if (p->num_qids_mapped > 0) {
80                                 rte_errno = -EDQUOT;
81                                 break;
82                         }
83                         /* check port only takes a directed flow */
84                         if (num > 1) {
85                                 rte_errno = -EDQUOT;
86                                 break;
87                         }
88
89                         p->is_directed = 1;
90                         p->num_qids_mapped = 1;
91                 } else if (q->type == RTE_SCHED_TYPE_ORDERED) {
92                         p->num_ordered_qids++;
93                         p->num_qids_mapped++;
94                 } else if (q->type == RTE_SCHED_TYPE_ATOMIC ||
95                                 q->type == RTE_SCHED_TYPE_PARALLEL) {
96                         p->num_qids_mapped++;
97                 }
98
99                 q->cq_map[q->cq_num_mapped_cqs] = p->id;
100                 rte_smp_wmb();
101                 q->cq_num_mapped_cqs++;
102         }
103         return i;
104 }
105
106 static int
107 sw_port_unlink(struct rte_eventdev *dev, void *port, uint8_t queues[],
108                 uint16_t nb_unlinks)
109 {
110         struct sw_port *p = port;
111         struct sw_evdev *sw = sw_pmd_priv(dev);
112         unsigned int i, j;
113
114         int unlinked = 0;
115         for (i = 0; i < nb_unlinks; i++) {
116                 struct sw_qid *q = &sw->qids[queues[i]];
117                 for (j = 0; j < q->cq_num_mapped_cqs; j++) {
118                         if (q->cq_map[j] == p->id) {
119                                 q->cq_map[j] =
120                                         q->cq_map[q->cq_num_mapped_cqs - 1];
121                                 rte_smp_wmb();
122                                 q->cq_num_mapped_cqs--;
123                                 unlinked++;
124
125                                 p->num_qids_mapped--;
126
127                                 if (q->type == RTE_SCHED_TYPE_ORDERED)
128                                         p->num_ordered_qids--;
129
130                                 continue;
131                         }
132                 }
133         }
134         return unlinked;
135 }
136
137 static int
138 sw_port_setup(struct rte_eventdev *dev, uint8_t port_id,
139                 const struct rte_event_port_conf *conf)
140 {
141         struct sw_evdev *sw = sw_pmd_priv(dev);
142         struct sw_port *p = &sw->ports[port_id];
143         char buf[RTE_RING_NAMESIZE];
144         unsigned int i;
145
146         struct rte_event_dev_info info;
147         sw_info_get(dev, &info);
148
149         /* detect re-configuring and return credits to instance if needed */
150         if (p->initialized) {
151                 /* taking credits from pool is done one quanta at a time, and
152                  * credits may be spend (counted in p->inflights) or still
153                  * available in the port (p->inflight_credits). We must return
154                  * the sum to no leak credits
155                  */
156                 int possible_inflights = p->inflight_credits + p->inflights;
157                 rte_atomic32_sub(&sw->inflights, possible_inflights);
158         }
159
160         *p = (struct sw_port){0}; /* zero entire structure */
161         p->id = port_id;
162         p->sw = sw;
163
164         /* check to see if rings exists - port_setup() can be called multiple
165          * times legally (assuming device is stopped). If ring exists, free it
166          * to so it gets re-created with the correct size
167          */
168         snprintf(buf, sizeof(buf), "sw%d_p%u_%s", dev->data->dev_id,
169                         port_id, "rx_worker_ring");
170         struct rte_event_ring *existing_ring = rte_event_ring_lookup(buf);
171         if (existing_ring)
172                 rte_event_ring_free(existing_ring);
173
174         p->rx_worker_ring = rte_event_ring_create(buf, MAX_SW_PROD_Q_DEPTH,
175                         dev->data->socket_id,
176                         RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
177         if (p->rx_worker_ring == NULL) {
178                 SW_LOG_ERR("Error creating RX worker ring for port %d\n",
179                                 port_id);
180                 return -1;
181         }
182
183         p->inflight_max = conf->new_event_threshold;
184
185         /* check if ring exists, same as rx_worker above */
186         snprintf(buf, sizeof(buf), "sw%d_p%u, %s", dev->data->dev_id,
187                         port_id, "cq_worker_ring");
188         existing_ring = rte_event_ring_lookup(buf);
189         if (existing_ring)
190                 rte_event_ring_free(existing_ring);
191
192         p->cq_worker_ring = rte_event_ring_create(buf, conf->dequeue_depth,
193                         dev->data->socket_id,
194                         RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
195         if (p->cq_worker_ring == NULL) {
196                 rte_event_ring_free(p->rx_worker_ring);
197                 SW_LOG_ERR("Error creating CQ worker ring for port %d\n",
198                                 port_id);
199                 return -1;
200         }
201         sw->cq_ring_space[port_id] = conf->dequeue_depth;
202
203         /* set hist list contents to empty */
204         for (i = 0; i < SW_PORT_HIST_LIST; i++) {
205                 p->hist_list[i].fid = -1;
206                 p->hist_list[i].qid = -1;
207         }
208         dev->data->ports[port_id] = p;
209
210         rte_smp_wmb();
211         p->initialized = 1;
212         return 0;
213 }
214
215 static void
216 sw_port_release(void *port)
217 {
218         struct sw_port *p = (void *)port;
219         if (p == NULL)
220                 return;
221
222         rte_event_ring_free(p->rx_worker_ring);
223         rte_event_ring_free(p->cq_worker_ring);
224         memset(p, 0, sizeof(*p));
225 }
226
227 static int32_t
228 qid_init(struct sw_evdev *sw, unsigned int idx, int type,
229                 const struct rte_event_queue_conf *queue_conf)
230 {
231         unsigned int i;
232         int dev_id = sw->data->dev_id;
233         int socket_id = sw->data->socket_id;
234         char buf[IQ_RING_NAMESIZE];
235         struct sw_qid *qid = &sw->qids[idx];
236
237         for (i = 0; i < SW_IQS_MAX; i++) {
238                 snprintf(buf, sizeof(buf), "q_%u_iq_%d", idx, i);
239                 qid->iq[i] = iq_ring_create(buf, socket_id);
240                 if (!qid->iq[i]) {
241                         SW_LOG_DBG("ring create failed");
242                         goto cleanup;
243                 }
244         }
245
246         /* Initialize the FID structures to no pinning (-1), and zero packets */
247         const struct sw_fid_t fid = {.cq = -1, .pcount = 0};
248         for (i = 0; i < RTE_DIM(qid->fids); i++)
249                 qid->fids[i] = fid;
250
251         qid->id = idx;
252         qid->type = type;
253         qid->priority = queue_conf->priority;
254
255         if (qid->type == RTE_SCHED_TYPE_ORDERED) {
256                 char ring_name[RTE_RING_NAMESIZE];
257                 uint32_t window_size;
258
259                 /* rte_ring and window_size_mask require require window_size to
260                  * be a power-of-2.
261                  */
262                 window_size = rte_align32pow2(
263                                 queue_conf->nb_atomic_order_sequences);
264
265                 qid->window_size = window_size - 1;
266
267                 if (!window_size) {
268                         SW_LOG_DBG(
269                                 "invalid reorder_window_size for ordered queue\n"
270                                 );
271                         goto cleanup;
272                 }
273
274                 snprintf(buf, sizeof(buf), "sw%d_iq_%d_rob", dev_id, i);
275                 qid->reorder_buffer = rte_zmalloc_socket(buf,
276                                 window_size * sizeof(qid->reorder_buffer[0]),
277                                 0, socket_id);
278                 if (!qid->reorder_buffer) {
279                         SW_LOG_DBG("reorder_buffer malloc failed\n");
280                         goto cleanup;
281                 }
282
283                 memset(&qid->reorder_buffer[0],
284                        0,
285                        window_size * sizeof(qid->reorder_buffer[0]));
286
287                 snprintf(ring_name, sizeof(ring_name), "sw%d_q%d_freelist",
288                                 dev_id, idx);
289
290                 /* lookup the ring, and if it already exists, free it */
291                 struct rte_ring *cleanup = rte_ring_lookup(ring_name);
292                 if (cleanup)
293                         rte_ring_free(cleanup);
294
295                 qid->reorder_buffer_freelist = rte_ring_create(ring_name,
296                                 window_size,
297                                 socket_id,
298                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
299                 if (!qid->reorder_buffer_freelist) {
300                         SW_LOG_DBG("freelist ring create failed");
301                         goto cleanup;
302                 }
303
304                 /* Populate the freelist with reorder buffer entries. Enqueue
305                  * 'window_size - 1' entries because the rte_ring holds only
306                  * that many.
307                  */
308                 for (i = 0; i < window_size - 1; i++) {
309                         if (rte_ring_sp_enqueue(qid->reorder_buffer_freelist,
310                                                 &qid->reorder_buffer[i]) < 0)
311                                 goto cleanup;
312                 }
313
314                 qid->reorder_buffer_index = 0;
315                 qid->cq_next_tx = 0;
316         }
317
318         qid->initialized = 1;
319
320         return 0;
321
322 cleanup:
323         for (i = 0; i < SW_IQS_MAX; i++) {
324                 if (qid->iq[i])
325                         iq_ring_destroy(qid->iq[i]);
326         }
327
328         if (qid->reorder_buffer) {
329                 rte_free(qid->reorder_buffer);
330                 qid->reorder_buffer = NULL;
331         }
332
333         if (qid->reorder_buffer_freelist) {
334                 rte_ring_free(qid->reorder_buffer_freelist);
335                 qid->reorder_buffer_freelist = NULL;
336         }
337
338         return -EINVAL;
339 }
340
341 static int
342 sw_queue_setup(struct rte_eventdev *dev, uint8_t queue_id,
343                 const struct rte_event_queue_conf *conf)
344 {
345         int type;
346
347         type = conf->schedule_type;
348
349         if (RTE_EVENT_QUEUE_CFG_SINGLE_LINK & conf->event_queue_cfg) {
350                 type = SW_SCHED_TYPE_DIRECT;
351         } else if (RTE_EVENT_QUEUE_CFG_ALL_TYPES
352                         & conf->event_queue_cfg) {
353                 SW_LOG_ERR("QUEUE_CFG_ALL_TYPES not supported\n");
354                 return -ENOTSUP;
355         }
356
357         struct sw_evdev *sw = sw_pmd_priv(dev);
358         return qid_init(sw, queue_id, type, conf);
359 }
360
361 static void
362 sw_queue_release(struct rte_eventdev *dev, uint8_t id)
363 {
364         struct sw_evdev *sw = sw_pmd_priv(dev);
365         struct sw_qid *qid = &sw->qids[id];
366         uint32_t i;
367
368         for (i = 0; i < SW_IQS_MAX; i++)
369                 iq_ring_destroy(qid->iq[i]);
370
371         if (qid->type == RTE_SCHED_TYPE_ORDERED) {
372                 rte_free(qid->reorder_buffer);
373                 rte_ring_free(qid->reorder_buffer_freelist);
374         }
375         memset(qid, 0, sizeof(*qid));
376 }
377
378 static void
379 sw_queue_def_conf(struct rte_eventdev *dev, uint8_t queue_id,
380                                  struct rte_event_queue_conf *conf)
381 {
382         RTE_SET_USED(dev);
383         RTE_SET_USED(queue_id);
384
385         static const struct rte_event_queue_conf default_conf = {
386                 .nb_atomic_flows = 4096,
387                 .nb_atomic_order_sequences = 1,
388                 .schedule_type = RTE_SCHED_TYPE_ATOMIC,
389                 .priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
390         };
391
392         *conf = default_conf;
393 }
394
395 static void
396 sw_port_def_conf(struct rte_eventdev *dev, uint8_t port_id,
397                  struct rte_event_port_conf *port_conf)
398 {
399         RTE_SET_USED(dev);
400         RTE_SET_USED(port_id);
401
402         port_conf->new_event_threshold = 1024;
403         port_conf->dequeue_depth = 16;
404         port_conf->enqueue_depth = 16;
405 }
406
407 static int
408 sw_dev_configure(const struct rte_eventdev *dev)
409 {
410         struct sw_evdev *sw = sw_pmd_priv(dev);
411         const struct rte_eventdev_data *data = dev->data;
412         const struct rte_event_dev_config *conf = &data->dev_conf;
413
414         sw->qid_count = conf->nb_event_queues;
415         sw->port_count = conf->nb_event_ports;
416         sw->nb_events_limit = conf->nb_events_limit;
417         rte_atomic32_set(&sw->inflights, 0);
418
419         if (conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT)
420                 return -ENOTSUP;
421
422         return 0;
423 }
424
425 struct rte_eth_dev;
426
427 static int
428 sw_eth_rx_adapter_caps_get(const struct rte_eventdev *dev,
429                         const struct rte_eth_dev *eth_dev,
430                         uint32_t *caps)
431 {
432         RTE_SET_USED(dev);
433         RTE_SET_USED(eth_dev);
434         *caps = RTE_EVENT_ETH_RX_ADAPTER_SW_CAP;
435         return 0;
436 }
437
438 static void
439 sw_info_get(struct rte_eventdev *dev, struct rte_event_dev_info *info)
440 {
441         RTE_SET_USED(dev);
442
443         static const struct rte_event_dev_info evdev_sw_info = {
444                         .driver_name = SW_PMD_NAME,
445                         .max_event_queues = RTE_EVENT_MAX_QUEUES_PER_DEV,
446                         .max_event_queue_flows = SW_QID_NUM_FIDS,
447                         .max_event_queue_priority_levels = SW_Q_PRIORITY_MAX,
448                         .max_event_priority_levels = SW_IQS_MAX,
449                         .max_event_ports = SW_PORTS_MAX,
450                         .max_event_port_dequeue_depth = MAX_SW_CONS_Q_DEPTH,
451                         .max_event_port_enqueue_depth = MAX_SW_PROD_Q_DEPTH,
452                         .max_num_events = SW_INFLIGHT_EVENTS_TOTAL,
453                         .event_dev_cap = (RTE_EVENT_DEV_CAP_QUEUE_QOS |
454                                         RTE_EVENT_DEV_CAP_BURST_MODE |
455                                         RTE_EVENT_DEV_CAP_EVENT_QOS),
456         };
457
458         *info = evdev_sw_info;
459 }
460
461 static void
462 sw_dump(struct rte_eventdev *dev, FILE *f)
463 {
464         const struct sw_evdev *sw = sw_pmd_priv(dev);
465
466         static const char * const q_type_strings[] = {
467                         "Ordered", "Atomic", "Parallel", "Directed"
468         };
469         uint32_t i;
470         fprintf(f, "EventDev %s: ports %d, qids %d\n", "todo-fix-name",
471                         sw->port_count, sw->qid_count);
472
473         fprintf(f, "\trx   %"PRIu64"\n\tdrop %"PRIu64"\n\ttx   %"PRIu64"\n",
474                 sw->stats.rx_pkts, sw->stats.rx_dropped, sw->stats.tx_pkts);
475         fprintf(f, "\tsched calls: %"PRIu64"\n", sw->sched_called);
476         fprintf(f, "\tsched cq/qid call: %"PRIu64"\n", sw->sched_cq_qid_called);
477         fprintf(f, "\tsched no IQ enq: %"PRIu64"\n", sw->sched_no_iq_enqueues);
478         fprintf(f, "\tsched no CQ enq: %"PRIu64"\n", sw->sched_no_cq_enqueues);
479         uint32_t inflights = rte_atomic32_read(&sw->inflights);
480         uint32_t credits = sw->nb_events_limit - inflights;
481         fprintf(f, "\tinflight %d, credits: %d\n", inflights, credits);
482
483 #define COL_RED "\x1b[31m"
484 #define COL_RESET "\x1b[0m"
485
486         for (i = 0; i < sw->port_count; i++) {
487                 int max, j;
488                 const struct sw_port *p = &sw->ports[i];
489                 if (!p->initialized) {
490                         fprintf(f, "  %sPort %d not initialized.%s\n",
491                                 COL_RED, i, COL_RESET);
492                         continue;
493                 }
494                 fprintf(f, "  Port %d %s\n", i,
495                         p->is_directed ? " (SingleCons)" : "");
496                 fprintf(f, "\trx   %"PRIu64"\tdrop %"PRIu64"\ttx   %"PRIu64
497                         "\t%sinflight %d%s\n", sw->ports[i].stats.rx_pkts,
498                         sw->ports[i].stats.rx_dropped,
499                         sw->ports[i].stats.tx_pkts,
500                         (p->inflights == p->inflight_max) ?
501                                 COL_RED : COL_RESET,
502                         sw->ports[i].inflights, COL_RESET);
503
504                 fprintf(f, "\tMax New: %u"
505                         "\tAvg cycles PP: %"PRIu64"\tCredits: %u\n",
506                         sw->ports[i].inflight_max,
507                         sw->ports[i].avg_pkt_ticks,
508                         sw->ports[i].inflight_credits);
509                 fprintf(f, "\tReceive burst distribution:\n");
510                 float zp_percent = p->zero_polls * 100.0 / p->total_polls;
511                 fprintf(f, zp_percent < 10 ? "\t\t0:%.02f%% " : "\t\t0:%.0f%% ",
512                                 zp_percent);
513                 for (max = (int)RTE_DIM(p->poll_buckets); max-- > 0;)
514                         if (p->poll_buckets[max] != 0)
515                                 break;
516                 for (j = 0; j <= max; j++) {
517                         if (p->poll_buckets[j] != 0) {
518                                 float poll_pc = p->poll_buckets[j] * 100.0 /
519                                         p->total_polls;
520                                 fprintf(f, "%u-%u:%.02f%% ",
521                                         ((j << SW_DEQ_STAT_BUCKET_SHIFT) + 1),
522                                         ((j+1) << SW_DEQ_STAT_BUCKET_SHIFT),
523                                         poll_pc);
524                         }
525                 }
526                 fprintf(f, "\n");
527
528                 if (p->rx_worker_ring) {
529                         uint64_t used = rte_event_ring_count(p->rx_worker_ring);
530                         uint64_t space = rte_event_ring_free_count(
531                                         p->rx_worker_ring);
532                         const char *col = (space == 0) ? COL_RED : COL_RESET;
533                         fprintf(f, "\t%srx ring used: %4"PRIu64"\tfree: %4"
534                                         PRIu64 COL_RESET"\n", col, used, space);
535                 } else
536                         fprintf(f, "\trx ring not initialized.\n");
537
538                 if (p->cq_worker_ring) {
539                         uint64_t used = rte_event_ring_count(p->cq_worker_ring);
540                         uint64_t space = rte_event_ring_free_count(
541                                         p->cq_worker_ring);
542                         const char *col = (space == 0) ? COL_RED : COL_RESET;
543                         fprintf(f, "\t%scq ring used: %4"PRIu64"\tfree: %4"
544                                         PRIu64 COL_RESET"\n", col, used, space);
545                 } else
546                         fprintf(f, "\tcq ring not initialized.\n");
547         }
548
549         for (i = 0; i < sw->qid_count; i++) {
550                 const struct sw_qid *qid = &sw->qids[i];
551                 if (!qid->initialized) {
552                         fprintf(f, "  %sQueue %d not initialized.%s\n",
553                                 COL_RED, i, COL_RESET);
554                         continue;
555                 }
556                 int affinities_per_port[SW_PORTS_MAX] = {0};
557                 uint32_t inflights = 0;
558
559                 fprintf(f, "  Queue %d (%s)\n", i, q_type_strings[qid->type]);
560                 fprintf(f, "\trx   %"PRIu64"\tdrop %"PRIu64"\ttx   %"PRIu64"\n",
561                         qid->stats.rx_pkts, qid->stats.rx_dropped,
562                         qid->stats.tx_pkts);
563                 if (qid->type == RTE_SCHED_TYPE_ORDERED) {
564                         struct rte_ring *rob_buf_free =
565                                 qid->reorder_buffer_freelist;
566                         if (rob_buf_free)
567                                 fprintf(f, "\tReorder entries in use: %u\n",
568                                         rte_ring_free_count(rob_buf_free));
569                         else
570                                 fprintf(f,
571                                         "\tReorder buffer not initialized\n");
572                 }
573
574                 uint32_t flow;
575                 for (flow = 0; flow < RTE_DIM(qid->fids); flow++)
576                         if (qid->fids[flow].cq != -1) {
577                                 affinities_per_port[qid->fids[flow].cq]++;
578                                 inflights += qid->fids[flow].pcount;
579                         }
580
581                 uint32_t port;
582                 fprintf(f, "\tPer Port Stats:\n");
583                 for (port = 0; port < sw->port_count; port++) {
584                         fprintf(f, "\t  Port %d: Pkts: %"PRIu64, port,
585                                         qid->to_port[port]);
586                         fprintf(f, "\tFlows: %d\n", affinities_per_port[port]);
587                 }
588
589                 uint32_t iq;
590                 uint32_t iq_printed = 0;
591                 for (iq = 0; iq < SW_IQS_MAX; iq++) {
592                         if (!qid->iq[iq]) {
593                                 fprintf(f, "\tiq %d is not initialized.\n", iq);
594                                 iq_printed = 1;
595                                 continue;
596                         }
597                         uint32_t used = iq_ring_count(qid->iq[iq]);
598                         uint32_t free = iq_ring_free_count(qid->iq[iq]);
599                         const char *col = (free == 0) ? COL_RED : COL_RESET;
600                         if (used > 0) {
601                                 fprintf(f, "\t%siq %d: Used %d\tFree %d"
602                                         COL_RESET"\n", col, iq, used, free);
603                                 iq_printed = 1;
604                         }
605                 }
606                 if (iq_printed == 0)
607                         fprintf(f, "\t-- iqs empty --\n");
608         }
609 }
610
611 static int
612 sw_start(struct rte_eventdev *dev)
613 {
614         unsigned int i, j;
615         struct sw_evdev *sw = sw_pmd_priv(dev);
616
617         rte_service_component_runstate_set(sw->service_id, 1);
618
619         /* check a service core is mapped to this service */
620         if (!rte_service_runstate_get(sw->service_id)) {
621                 SW_LOG_ERR("Warning: No Service core enabled on service %s\n",
622                                 sw->service_name);
623                 return -ENOENT;
624         }
625
626         /* check all ports are set up */
627         for (i = 0; i < sw->port_count; i++)
628                 if (sw->ports[i].rx_worker_ring == NULL) {
629                         SW_LOG_ERR("Port %d not configured\n", i);
630                         return -ESTALE;
631                 }
632
633         /* check all queues are configured and mapped to ports*/
634         for (i = 0; i < sw->qid_count; i++)
635                 if (sw->qids[i].iq[0] == NULL ||
636                                 sw->qids[i].cq_num_mapped_cqs == 0) {
637                         SW_LOG_ERR("Queue %d not configured\n", i);
638                         return -ENOLINK;
639                 }
640
641         /* build up our prioritized array of qids */
642         /* We don't use qsort here, as if all/multiple entries have the same
643          * priority, the result is non-deterministic. From "man 3 qsort":
644          * "If two members compare as equal, their order in the sorted
645          * array is undefined."
646          */
647         uint32_t qidx = 0;
648         for (j = 0; j <= RTE_EVENT_DEV_PRIORITY_LOWEST; j++) {
649                 for (i = 0; i < sw->qid_count; i++) {
650                         if (sw->qids[i].priority == j) {
651                                 sw->qids_prioritized[qidx] = &sw->qids[i];
652                                 qidx++;
653                         }
654                 }
655         }
656
657         if (sw_xstats_init(sw) < 0)
658                 return -EINVAL;
659
660         rte_smp_wmb();
661         sw->started = 1;
662
663         return 0;
664 }
665
666 static void
667 sw_stop(struct rte_eventdev *dev)
668 {
669         struct sw_evdev *sw = sw_pmd_priv(dev);
670         sw_xstats_uninit(sw);
671         sw->started = 0;
672         rte_smp_wmb();
673 }
674
675 static int
676 sw_close(struct rte_eventdev *dev)
677 {
678         struct sw_evdev *sw = sw_pmd_priv(dev);
679         uint32_t i;
680
681         for (i = 0; i < sw->qid_count; i++)
682                 sw_queue_release(dev, i);
683         sw->qid_count = 0;
684
685         for (i = 0; i < sw->port_count; i++)
686                 sw_port_release(&sw->ports[i]);
687         sw->port_count = 0;
688
689         memset(&sw->stats, 0, sizeof(sw->stats));
690         sw->sched_called = 0;
691         sw->sched_no_iq_enqueues = 0;
692         sw->sched_no_cq_enqueues = 0;
693         sw->sched_cq_qid_called = 0;
694
695         return 0;
696 }
697
698 static int
699 assign_numa_node(const char *key __rte_unused, const char *value, void *opaque)
700 {
701         int *socket_id = opaque;
702         *socket_id = atoi(value);
703         if (*socket_id >= RTE_MAX_NUMA_NODES)
704                 return -1;
705         return 0;
706 }
707
708 static int
709 set_sched_quanta(const char *key __rte_unused, const char *value, void *opaque)
710 {
711         int *quanta = opaque;
712         *quanta = atoi(value);
713         if (*quanta < 0 || *quanta >= 4096)
714                 return -1;
715         return 0;
716 }
717
718 static int
719 set_credit_quanta(const char *key __rte_unused, const char *value, void *opaque)
720 {
721         int *credit = opaque;
722         *credit = atoi(value);
723         if (*credit < 0 || *credit >= 128)
724                 return -1;
725         return 0;
726 }
727
728
729 static int32_t sw_sched_service_func(void *args)
730 {
731         struct rte_eventdev *dev = args;
732         sw_event_schedule(dev);
733         return 0;
734 }
735
736 static int
737 sw_probe(struct rte_vdev_device *vdev)
738 {
739         static const struct rte_eventdev_ops evdev_sw_ops = {
740                         .dev_configure = sw_dev_configure,
741                         .dev_infos_get = sw_info_get,
742                         .dev_close = sw_close,
743                         .dev_start = sw_start,
744                         .dev_stop = sw_stop,
745                         .dump = sw_dump,
746
747                         .queue_def_conf = sw_queue_def_conf,
748                         .queue_setup = sw_queue_setup,
749                         .queue_release = sw_queue_release,
750                         .port_def_conf = sw_port_def_conf,
751                         .port_setup = sw_port_setup,
752                         .port_release = sw_port_release,
753                         .port_link = sw_port_link,
754                         .port_unlink = sw_port_unlink,
755
756                         .eth_rx_adapter_caps_get = sw_eth_rx_adapter_caps_get,
757
758                         .xstats_get = sw_xstats_get,
759                         .xstats_get_names = sw_xstats_get_names,
760                         .xstats_get_by_name = sw_xstats_get_by_name,
761                         .xstats_reset = sw_xstats_reset,
762         };
763
764         static const char *const args[] = {
765                 NUMA_NODE_ARG,
766                 SCHED_QUANTA_ARG,
767                 CREDIT_QUANTA_ARG,
768                 NULL
769         };
770         const char *name;
771         const char *params;
772         struct rte_eventdev *dev;
773         struct sw_evdev *sw;
774         int socket_id = rte_socket_id();
775         int sched_quanta  = SW_DEFAULT_SCHED_QUANTA;
776         int credit_quanta = SW_DEFAULT_CREDIT_QUANTA;
777
778         name = rte_vdev_device_name(vdev);
779         params = rte_vdev_device_args(vdev);
780         if (params != NULL && params[0] != '\0') {
781                 struct rte_kvargs *kvlist = rte_kvargs_parse(params, args);
782
783                 if (!kvlist) {
784                         SW_LOG_INFO(
785                                 "Ignoring unsupported parameters when creating device '%s'\n",
786                                 name);
787                 } else {
788                         int ret = rte_kvargs_process(kvlist, NUMA_NODE_ARG,
789                                         assign_numa_node, &socket_id);
790                         if (ret != 0) {
791                                 SW_LOG_ERR(
792                                         "%s: Error parsing numa node parameter",
793                                         name);
794                                 rte_kvargs_free(kvlist);
795                                 return ret;
796                         }
797
798                         ret = rte_kvargs_process(kvlist, SCHED_QUANTA_ARG,
799                                         set_sched_quanta, &sched_quanta);
800                         if (ret != 0) {
801                                 SW_LOG_ERR(
802                                         "%s: Error parsing sched quanta parameter",
803                                         name);
804                                 rte_kvargs_free(kvlist);
805                                 return ret;
806                         }
807
808                         ret = rte_kvargs_process(kvlist, CREDIT_QUANTA_ARG,
809                                         set_credit_quanta, &credit_quanta);
810                         if (ret != 0) {
811                                 SW_LOG_ERR(
812                                         "%s: Error parsing credit quanta parameter",
813                                         name);
814                                 rte_kvargs_free(kvlist);
815                                 return ret;
816                         }
817
818                         rte_kvargs_free(kvlist);
819                 }
820         }
821
822         SW_LOG_INFO(
823                         "Creating eventdev sw device %s, numa_node=%d, sched_quanta=%d, credit_quanta=%d\n",
824                         name, socket_id, sched_quanta, credit_quanta);
825
826         dev = rte_event_pmd_vdev_init(name,
827                         sizeof(struct sw_evdev), socket_id);
828         if (dev == NULL) {
829                 SW_LOG_ERR("eventdev vdev init() failed");
830                 return -EFAULT;
831         }
832         dev->dev_ops = &evdev_sw_ops;
833         dev->enqueue = sw_event_enqueue;
834         dev->enqueue_burst = sw_event_enqueue_burst;
835         dev->enqueue_new_burst = sw_event_enqueue_burst;
836         dev->enqueue_forward_burst = sw_event_enqueue_burst;
837         dev->dequeue = sw_event_dequeue;
838         dev->dequeue_burst = sw_event_dequeue_burst;
839
840         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
841                 return 0;
842
843         sw = dev->data->dev_private;
844         sw->data = dev->data;
845
846         /* copy values passed from vdev command line to instance */
847         sw->credit_update_quanta = credit_quanta;
848         sw->sched_quanta = sched_quanta;
849
850         /* register service with EAL */
851         struct rte_service_spec service;
852         memset(&service, 0, sizeof(struct rte_service_spec));
853         snprintf(service.name, sizeof(service.name), "%s_service", name);
854         snprintf(sw->service_name, sizeof(sw->service_name), "%s_service",
855                         name);
856         service.socket_id = socket_id;
857         service.callback = sw_sched_service_func;
858         service.callback_userdata = (void *)dev;
859
860         int32_t ret = rte_service_component_register(&service, &sw->service_id);
861         if (ret) {
862                 SW_LOG_ERR("service register() failed");
863                 return -ENOEXEC;
864         }
865
866         dev->data->service_inited = 1;
867         dev->data->service_id = sw->service_id;
868
869         return 0;
870 }
871
872 static int
873 sw_remove(struct rte_vdev_device *vdev)
874 {
875         const char *name;
876
877         name = rte_vdev_device_name(vdev);
878         if (name == NULL)
879                 return -EINVAL;
880
881         SW_LOG_INFO("Closing eventdev sw device %s\n", name);
882
883         return rte_event_pmd_vdev_uninit(name);
884 }
885
886 static struct rte_vdev_driver evdev_sw_pmd_drv = {
887         .probe = sw_probe,
888         .remove = sw_remove
889 };
890
891 RTE_PMD_REGISTER_VDEV(EVENTDEV_NAME_SW_PMD, evdev_sw_pmd_drv);
892 RTE_PMD_REGISTER_PARAM_STRING(event_sw, NUMA_NODE_ARG "=<int> "
893                 SCHED_QUANTA_ARG "=<int>" CREDIT_QUANTA_ARG "=<int>");