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