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