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