summaryrefslogtreecommitdiffstats
path: root/src/spdk/dpdk/drivers/event/opdl/opdl_evdev_init.c
blob: 15aae47524fa02dfc75955732686edab1d5a5cbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2017 Intel Corporation
 */

#include <inttypes.h>
#include <string.h>

#include <rte_bus_vdev.h>
#include <rte_errno.h>
#include <rte_cycles.h>
#include <rte_memzone.h>

#include "opdl_evdev.h"
#include "opdl_ring.h"
#include "opdl_log.h"


static __rte_always_inline uint32_t
enqueue_check(struct opdl_port *p,
		const struct rte_event ev[],
		uint16_t num,
		uint16_t num_events)
{
	uint16_t i;

	if (p->opdl->do_validation) {

		for (i = 0; i < num; i++) {
			if (ev[i].queue_id != p->next_external_qid) {
				PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
					     "ERROR - port:[%u] - event wants"
					     " to enq to q_id[%u],"
					     " but should be [%u]",
					     opdl_pmd_dev_id(p->opdl),
					     p->id,
					     ev[i].queue_id,
					     p->next_external_qid);
				rte_errno = EINVAL;
				return 0;
			}
		}

		/* Stats */
		if (p->p_type == OPDL_PURE_RX_PORT ||
				p->p_type == OPDL_ASYNC_PORT) {
			/* Stats */
			if (num_events) {
				p->port_stat[claim_pkts_requested] += num;
				p->port_stat[claim_pkts_granted] += num_events;
				p->port_stat[claim_non_empty]++;
				p->start_cycles = rte_rdtsc();
			} else {
				p->port_stat[claim_empty]++;
				p->start_cycles = 0;
			}
		} else {
			if (p->start_cycles) {
				uint64_t end_cycles = rte_rdtsc();
				p->port_stat[total_cycles] +=
					end_cycles - p->start_cycles;
			}
		}
	} else {
		if (num > 0 &&
				ev[0].queue_id != p->next_external_qid) {
			rte_errno = EINVAL;
			return 0;
		}
	}

	return num;
}

static __rte_always_inline void
update_on_dequeue(struct opdl_port *p,
		struct rte_event ev[],
		uint16_t num,
		uint16_t num_events)
{
	if (p->opdl->do_validation) {
		int16_t i;
		for (i = 0; i < num; i++)
			ev[i].queue_id =
				p->opdl->queue[p->queue_id].external_qid;

		/* Stats */
		if (num_events) {
			p->port_stat[claim_pkts_requested] += num;
			p->port_stat[claim_pkts_granted] += num_events;
			p->port_stat[claim_non_empty]++;
			p->start_cycles = rte_rdtsc();
		} else {
			p->port_stat[claim_empty]++;
			p->start_cycles = 0;
		}
	} else {
		if (num > 0)
			ev[0].queue_id =
				p->opdl->queue[p->queue_id].external_qid;
	}
}


/*
 * Error RX enqueue:
 *
 *
 */

static uint16_t
opdl_rx_error_enqueue(struct opdl_port *p,
		const struct rte_event ev[],
		uint16_t num)
{
	RTE_SET_USED(p);
	RTE_SET_USED(ev);
	RTE_SET_USED(num);

	rte_errno = ENOSPC;

	return 0;
}

/*
 * RX enqueue:
 *
 * This function handles enqueue for a single input stage_inst with
 *	threadsafe disabled or enabled. eg 1 thread using a stage_inst or
 *	multiple threads sharing a stage_inst
 */

static uint16_t
opdl_rx_enqueue(struct opdl_port *p,
		const struct rte_event ev[],
		uint16_t num)
{
	uint16_t enqueued = 0;

	enqueued = opdl_ring_input(opdl_stage_get_opdl_ring(p->enq_stage_inst),
				   ev,
				   num,
				   false);
	if (!enqueue_check(p, ev, num, enqueued))
		return 0;


	if (enqueued < num)
		rte_errno = ENOSPC;

	return enqueued;
}

/*
 * Error TX handler
 *
 */

static uint16_t
opdl_tx_error_dequeue(struct opdl_port *p,
		struct rte_event ev[],
		uint16_t num)
{
	RTE_SET_USED(p);
	RTE_SET_USED(ev);
	RTE_SET_USED(num);

	rte_errno = ENOSPC;

	return 0;
}

/*
 * TX single threaded claim
 *
 * This function handles dequeue for a single worker stage_inst with
 *	threadsafe disabled. eg 1 thread using an stage_inst
 */

static uint16_t
opdl_tx_dequeue_single_thread(struct opdl_port *p,
			struct rte_event ev[],
			uint16_t num)
{
	uint16_t returned;

	struct opdl_ring  *ring;

	ring = opdl_stage_get_opdl_ring(p->deq_stage_inst);

	returned = opdl_ring_copy_to_burst(ring,
					   p->deq_stage_inst,
					   ev,
					   num,
					   false);

	update_on_dequeue(p, ev, num, returned);

	return returned;
}

/*
 * TX multi threaded claim
 *
 * This function handles dequeue for multiple worker stage_inst with
 *	threadsafe disabled. eg multiple stage_inst each with its own instance
 */

static uint16_t
opdl_tx_dequeue_multi_inst(struct opdl_port *p,
			struct rte_event ev[],
			uint16_t num)
{
	uint32_t num_events = 0;

	num_events = opdl_stage_claim(p->deq_stage_inst,
				    (void *)ev,
				    num,
				    NULL,
				    false,
				    false);

	update_on_dequeue(p, ev, num, num_events);

	return opdl_stage_disclaim(p->deq_stage_inst, num_events, false);
}


/*
 * Worker thread claim
 *
 */

static uint16_t
opdl_claim(struct opdl_port *p, struct rte_event ev[], uint16_t num)
{
	uint32_t num_events = 0;

	if (unlikely(num > MAX_OPDL_CONS_Q_DEPTH)) {
		PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
			     "Attempt to dequeue num of events larger than port (%d) max",
			     opdl_pmd_dev_id(p->opdl),
			     p->id);
		rte_errno = EINVAL;
		return 0;
	}


	num_events = opdl_stage_claim(p->deq_stage_inst,
			(void *)ev,
			num,
			NULL,
			false,
			p->atomic_claim);


	update_on_dequeue(p, ev, num, num_events);

	return num_events;
}

/*
 * Worker thread disclaim
 */

static uint16_t
opdl_disclaim(struct opdl_port *p, const struct rte_event ev[], uint16_t num)
{
	uint16_t enqueued = 0;

	uint32_t i = 0;

	for (i = 0; i < num; i++)
		opdl_ring_cas_slot(p->enq_stage_inst, &ev[i],
				i, p->atomic_claim);

	enqueued = opdl_stage_disclaim(p->enq_stage_inst,
				       num,
				       false);

	return enqueue_check(p, ev, num, enqueued);
}

static __rte_always_inline struct opdl_stage *
stage_for_port(struct opdl_queue *q, unsigned int i)
{
	if (q->q_pos == OPDL_Q_POS_START || q->q_pos == OPDL_Q_POS_MIDDLE)
		return q->ports[i]->enq_stage_inst;
	else
		return q->ports[i]->deq_stage_inst;
}

static int opdl_add_deps(struct opdl_evdev *device,
			 int q_id,
			 int deps_q_id)
{
	unsigned int i, j;
	int status;
	struct opdl_ring  *ring;
	struct opdl_queue *queue = &device->queue[q_id];
	struct opdl_queue *queue_deps = &device->queue[deps_q_id];
	struct opdl_stage *dep_stages[OPDL_PORTS_MAX];

	/* sanity check that all stages are for same opdl ring */
	for (i = 0; i < queue->nb_ports; i++) {
		struct opdl_ring *r =
			opdl_stage_get_opdl_ring(stage_for_port(queue, i));
		for (j = 0; j < queue_deps->nb_ports; j++) {
			struct opdl_ring *rj =
				opdl_stage_get_opdl_ring(
						stage_for_port(queue_deps, j));
			if (r != rj) {
				PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
					     "Stages and dependents"
					     " are not for same opdl ring",
					     opdl_pmd_dev_id(device));
				uint32_t k;
				for (k = 0; k < device->nb_opdls; k++) {
					opdl_ring_dump(device->opdl[k],
							stdout);
				}
				return -EINVAL;
			}
		}
	}

	/* Gather all stages instance in deps */
	for (i = 0; i < queue_deps->nb_ports; i++)
		dep_stages[i] = stage_for_port(queue_deps, i);


	/* Add all deps for each port->stage_inst in this queue */
	for (i = 0; i < queue->nb_ports; i++) {

		ring = opdl_stage_get_opdl_ring(stage_for_port(queue, i));

		status = opdl_stage_deps_add(ring,
				stage_for_port(queue, i),
				queue->ports[i]->num_instance,
				queue->ports[i]->instance_id,
				dep_stages,
				queue_deps->nb_ports);
		if (status < 0)
			return -EINVAL;
	}

	return 0;
}

int
opdl_add_event_handlers(struct rte_eventdev *dev)
{
	int err = 0;

	struct opdl_evdev *device = opdl_pmd_priv(dev);
	unsigned int i;

	for (i = 0; i < device->max_port_nb; i++) {

		struct opdl_port *port = &device->ports[i];

		if (port->configured) {
			if (port->p_type == OPDL_PURE_RX_PORT) {
				port->enq = opdl_rx_enqueue;
				port->deq = opdl_tx_error_dequeue;

			} else if (port->p_type == OPDL_PURE_TX_PORT) {

				port->enq = opdl_rx_error_enqueue;

				if (port->num_instance == 1)
					port->deq =
						opdl_tx_dequeue_single_thread;
				else
					port->deq = opdl_tx_dequeue_multi_inst;

			} else if (port->p_type == OPDL_REGULAR_PORT) {

				port->enq = opdl_disclaim;
				port->deq = opdl_claim;

			} else if (port->p_type == OPDL_ASYNC_PORT) {

				port->enq = opdl_rx_enqueue;

				/* Always single instance */
				port->deq = opdl_tx_dequeue_single_thread;
			} else {
				PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
					     "port:[%u] has invalid port type - ",
					     opdl_pmd_dev_id(port->opdl),
					     port->id);
				err = -EINVAL;
				break;
			}
			port->initialized = 1;
		}
	}

	if (!err)
		fprintf(stdout, "Success - enqueue/dequeue handler(s) added\n");
	return err;
}

int
build_all_dependencies(struct rte_eventdev *dev)
{

	int err = 0;
	unsigned int i;
	struct opdl_evdev *device = opdl_pmd_priv(dev);

	uint8_t start_qid = 0;

	for (i = 0; i < RTE_EVENT_MAX_QUEUES_PER_DEV; i++) {
		struct opdl_queue *queue = &device->queue[i];
		if (!queue->initialized)
			break;

		if (queue->q_pos == OPDL_Q_POS_START) {
			start_qid = i;
			continue;
		}

		if (queue->q_pos == OPDL_Q_POS_MIDDLE) {
			err = opdl_add_deps(device, i, i-1);
			if (err < 0) {
				PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
					     "dependency addition for queue:[%u] - FAILED",
					     dev->data->dev_id,
					     queue->external_qid);
				break;
			}
		}

		if (queue->q_pos == OPDL_Q_POS_END) {
			/* Add this dependency */
			err = opdl_add_deps(device, i, i-1);
			if (err < 0) {
				PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
					     "dependency addition for queue:[%u] - FAILED",
					     dev->data->dev_id,
					     queue->external_qid);
				break;
			}
			/* Add dependency for rx on tx */
			err = opdl_add_deps(device, start_qid, i);
			if (err < 0) {
				PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
					     "dependency addition for queue:[%u] - FAILED",
					     dev->data->dev_id,
					     queue->external_qid);
				break;
			}
		}
	}

	if (!err)
		fprintf(stdout, "Success - dependencies built\n");

	return err;
}
int
check_queues_linked(struct rte_eventdev *dev)
{

	int err = 0;
	unsigned int i;
	struct opdl_evdev *device = opdl_pmd_priv(dev);
	uint32_t nb_iq = 0;

	for (i = 0; i < RTE_EVENT_MAX_QUEUES_PER_DEV; i++) {
		struct opdl_queue *queue = &device->queue[i];

		if (!queue->initialized)
			break;

		if (queue->external_qid == OPDL_INVALID_QID)
			nb_iq++;

		if (queue->nb_ports == 0) {
			PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
				     "queue:[%u] has no associated ports",
				     dev->data->dev_id,
				     i);
			err = -EINVAL;
			break;
		}
	}
	if (!err) {
		if ((i - nb_iq) != device->max_queue_nb) {
			PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
				     "%u queues counted but should be %u",
				     dev->data->dev_id,
				     i - nb_iq,
				     device->max_queue_nb);
			err = -1;
		}

	}
	return err;
}

void
destroy_queues_and_rings(struct rte_eventdev *dev)
{
	struct opdl_evdev *device = opdl_pmd_priv(dev);
	uint32_t i;

	for (i = 0; i < device->nb_opdls; i++) {
		if (device->opdl[i])
			opdl_ring_free(device->opdl[i]);
	}

	memset(&device->queue,
			0,
			sizeof(struct opdl_queue)
			* RTE_EVENT_MAX_QUEUES_PER_DEV);
}

#define OPDL_ID(d)(d->nb_opdls - 1)

static __rte_always_inline void
initialise_queue(struct opdl_evdev *device,
		enum queue_pos pos,
		int32_t i)
{
	struct opdl_queue *queue = &device->queue[device->nb_queues];

	if (i == -1) {
		queue->q_type = OPDL_Q_TYPE_ORDERED;
		queue->external_qid = OPDL_INVALID_QID;
	} else {
		queue->q_type = device->q_md[i].type;
		queue->external_qid = device->q_md[i].ext_id;
		/* Add ex->in for queues setup */
		device->q_map_ex_to_in[queue->external_qid] = device->nb_queues;
	}
	queue->opdl_id = OPDL_ID(device);
	queue->q_pos = pos;
	queue->nb_ports = 0;
	queue->configured = 1;

	device->nb_queues++;
}


static __rte_always_inline int
create_opdl(struct opdl_evdev *device)
{
	int err = 0;

	char name[RTE_MEMZONE_NAMESIZE];

	snprintf(name, RTE_MEMZONE_NAMESIZE,
			"%s_%u", device->service_name, device->nb_opdls);

	device->opdl[device->nb_opdls] =
		opdl_ring_create(name,
				device->nb_events_limit,
				sizeof(struct rte_event),
				device->max_port_nb * 2,
				device->socket);

	if (!device->opdl[device->nb_opdls]) {
		PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
			     "opdl ring %u creation - FAILED",
			     opdl_pmd_dev_id(device),
			     device->nb_opdls);
		err = -EINVAL;
	} else {
		device->nb_opdls++;
	}
	return err;
}

static __rte_always_inline int
create_link_opdl(struct opdl_evdev *device, uint32_t index)
{

	int err = 0;

	if (device->q_md[index + 1].type !=
			OPDL_Q_TYPE_SINGLE_LINK) {

		/* async queue with regular
		 * queue following it
		 */

		/* create a new opdl ring */
		err = create_opdl(device);
		if (!err) {
			/* create an initial
			 * dummy queue for new opdl
			 */
			initialise_queue(device,
					OPDL_Q_POS_START,
					-1);
		} else {
			err = -EINVAL;
		}
	} else {
		PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
			     "queue %u, two consecutive"
			     " SINGLE_LINK queues, not allowed",
			     opdl_pmd_dev_id(device),
			     index);
		err = -EINVAL;
	}

	return err;
}

int
create_queues_and_rings(struct rte_eventdev *dev)
{
	int err = 0;

	struct opdl_evdev *device = opdl_pmd_priv(dev);

	device->nb_queues = 0;

	if (device->nb_ports != device->max_port_nb) {
		PMD_DRV_LOG(ERR, "Number ports setup:%u NOT EQUAL to max port"
				" number:%u for this device",
				device->nb_ports,
				device->max_port_nb);
		err = -1;
	}

	if (!err) {
		/* We will have at least one opdl so create it now */
		err = create_opdl(device);
	}

	if (!err) {

		/* Create 1st "dummy" queue */
		initialise_queue(device,
				 OPDL_Q_POS_START,
				 -1);

		uint32_t i;
		for (i = 0; i < device->nb_q_md; i++) {

			/* Check */
			if (!device->q_md[i].setup) {

				PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
					     "queue meta data slot %u"
					     " not setup - FAILING",
					     dev->data->dev_id,
					     i);
				err = -EINVAL;
				break;
			} else if (device->q_md[i].type !=
					OPDL_Q_TYPE_SINGLE_LINK) {

				if (!device->q_md[i + 1].setup) {
					/* Create a simple ORDERED/ATOMIC
					 * queue at the end
					 */
					initialise_queue(device,
							OPDL_Q_POS_END,
							i);

				} else {
					/* Create a simple ORDERED/ATOMIC
					 * queue in the middle
					 */
					initialise_queue(device,
							OPDL_Q_POS_MIDDLE,
							i);
				}
			} else if (device->q_md[i].type ==
					OPDL_Q_TYPE_SINGLE_LINK) {

				/* create last queue for this opdl */
				initialise_queue(device,
						OPDL_Q_POS_END,
						i);

				err = create_link_opdl(device, i);

				if (err)
					break;


			}
		}
	}
	if (err)
		destroy_queues_and_rings(dev);

	return err;
}


int
initialise_all_other_ports(struct rte_eventdev *dev)
{
	int err = 0;
	struct opdl_stage *stage_inst = NULL;

	struct opdl_evdev *device = opdl_pmd_priv(dev);

	uint32_t i;
	for (i = 0; i < device->nb_ports; i++) {
		struct opdl_port *port = &device->ports[i];
		struct opdl_queue *queue = &device->queue[port->queue_id];

		if (port->queue_id == 0) {
			continue;
		} else if (queue->q_type != OPDL_Q_TYPE_SINGLE_LINK) {

			if (queue->q_pos == OPDL_Q_POS_MIDDLE) {

				/* Regular port with claim/disclaim */
				stage_inst = opdl_stage_add(
					device->opdl[queue->opdl_id],
						false,
						false);
				port->deq_stage_inst = stage_inst;
				port->enq_stage_inst = stage_inst;

				if (queue->q_type == OPDL_Q_TYPE_ATOMIC)
					port->atomic_claim = true;
				else
					port->atomic_claim = false;

				port->p_type =  OPDL_REGULAR_PORT;

				/* Add the port to the queue array of ports */
				queue->ports[queue->nb_ports] = port;
				port->instance_id = queue->nb_ports;
				queue->nb_ports++;
				opdl_stage_set_queue_id(stage_inst,
						port->queue_id);

			} else if (queue->q_pos == OPDL_Q_POS_END) {

				/* tx port  */
				stage_inst = opdl_stage_add(
					device->opdl[queue->opdl_id],
						false,
						false);
				port->deq_stage_inst = stage_inst;
				port->enq_stage_inst = NULL;
				port->p_type = OPDL_PURE_TX_PORT;

				/* Add the port to the queue array of ports */
				queue->ports[queue->nb_ports] = port;
				port->instance_id = queue->nb_ports;
				queue->nb_ports++;
			} else {

				PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
					     "port %u:, linked incorrectly"
					     " to a q_pos START/INVALID %u",
					     opdl_pmd_dev_id(port->opdl),
					     port->id,
					     queue->q_pos);
				err = -EINVAL;
				break;
			}

		} else if (queue->q_type == OPDL_Q_TYPE_SINGLE_LINK) {

			port->p_type = OPDL_ASYNC_PORT;

			/* -- tx -- */
			stage_inst = opdl_stage_add(
				device->opdl[queue->opdl_id],
					false,
					false); /* First stage */
			port->deq_stage_inst = stage_inst;

			/* Add the port to the queue array of ports */
			queue->ports[queue->nb_ports] = port;
			port->instance_id = queue->nb_ports;
			queue->nb_ports++;

			if (queue->nb_ports > 1) {
				PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
					     "queue %u:, setup as SINGLE_LINK"
					     " but has more than one port linked",
					     opdl_pmd_dev_id(port->opdl),
					     queue->external_qid);
				err = -EINVAL;
				break;
			}

			/* -- single instance rx for next opdl -- */
			uint8_t next_qid =
				device->q_map_ex_to_in[queue->external_qid] + 1;
			if (next_qid < RTE_EVENT_MAX_QUEUES_PER_DEV &&
					device->queue[next_qid].configured) {

				/* Remap the queue */
				queue = &device->queue[next_qid];

				stage_inst = opdl_stage_add(
					device->opdl[queue->opdl_id],
						false,
						true);
				port->enq_stage_inst = stage_inst;

				/* Add the port to the queue array of ports */
				queue->ports[queue->nb_ports] = port;
				port->instance_id = queue->nb_ports;
				queue->nb_ports++;
				if (queue->nb_ports > 1) {
					PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
						"dummy queue %u: for "
						"port %u, "
						"SINGLE_LINK but has more "
						"than one port linked",
						opdl_pmd_dev_id(port->opdl),
						next_qid,
						port->id);
					err = -EINVAL;
					break;
				}
				/* Set this queue to initialized as it is never
				 * referenced by any ports
				 */
				queue->initialized = 1;
			}
		}
	}

	/* Now that all ports are initialised we need to
	 * setup the last bit of stage md
	 */
	if (!err) {
		for (i = 0; i < device->nb_ports; i++) {
			struct opdl_port *port = &device->ports[i];
			struct opdl_queue *queue =
				&device->queue[port->queue_id];

			if (port->configured &&
					(port->queue_id != OPDL_INVALID_QID)) {
				if (queue->nb_ports == 0) {
					PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
						"queue:[%u] has no ports"
						" linked to it",
						opdl_pmd_dev_id(port->opdl),
						port->id);
					err = -EINVAL;
					break;
				}

				port->num_instance = queue->nb_ports;
				port->initialized = 1;
				queue->initialized = 1;
			} else {
				PMD_DRV_LOG(ERR, "DEV_ID:[%02d] : "
					     "Port:[%u] not configured  invalid"
					     " queue configuration",
					     opdl_pmd_dev_id(port->opdl),
					     port->id);
				err = -EINVAL;
				break;
			}
		}
	}
	return err;
}

int
initialise_queue_zero_ports(struct rte_eventdev *dev)
{
	int err = 0;
	uint8_t mt_rx = 0;
	struct opdl_stage *stage_inst = NULL;
	struct opdl_queue *queue = NULL;

	struct opdl_evdev *device = opdl_pmd_priv(dev);

	/* Assign queue zero and figure out how many Q0 ports we have */
	uint32_t i;
	for (i = 0; i < device->nb_ports; i++) {
		struct opdl_port *port = &device->ports[i];
		if (port->queue_id == OPDL_INVALID_QID) {
			port->queue_id = 0;
			port->external_qid = OPDL_INVALID_QID;
			port->p_type = OPDL_PURE_RX_PORT;
			mt_rx++;
		}
	}

	/* Create the stage */
	stage_inst = opdl_stage_add(device->opdl[0],
			(mt_rx > 1 ? true : false),
			true);
	if (stage_inst) {

		/* Assign the new created input stage to all relevant ports */
		for (i = 0; i < device->nb_ports; i++) {
			struct opdl_port *port = &device->ports[i];
			if (port->queue_id == 0) {
				queue = &device->queue[port->queue_id];
				port->enq_stage_inst = stage_inst;
				port->deq_stage_inst = NULL;
				port->configured = 1;
				port->initialized = 1;

				queue->ports[queue->nb_ports] = port;
				port->instance_id = queue->nb_ports;
				queue->nb_ports++;
			}
		}
	} else {
		err = -1;
	}
	return err;
}

int
assign_internal_queue_ids(struct rte_eventdev *dev)
{
	int err = 0;
	struct opdl_evdev *device = opdl_pmd_priv(dev);
	uint32_t i;

	for (i = 0; i < device->nb_ports; i++) {
		struct opdl_port *port = &device->ports[i];
		if (port->external_qid != OPDL_INVALID_QID) {
			port->queue_id =
				device->q_map_ex_to_in[port->external_qid];

			/* Now do the external_qid of the next queue */
			struct opdl_queue *queue =
				&device->queue[port->queue_id];
			if (queue->q_pos == OPDL_Q_POS_END)
				port->next_external_qid =
				device->queue[port->queue_id + 2].external_qid;
			else
				port->next_external_qid =
				device->queue[port->queue_id + 1].external_qid;
		}
	}
	return err;
}