DPDK logo

Elixir Cross Referencer

  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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(C) 2019 Marvell International Ltd.
 */

#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>

#include <rte_bus.h>
#include <rte_bus_pci.h>
#include <rte_eal.h>
#include <rte_lcore.h>
#include <rte_mempool.h>
#include <rte_pci.h>

#include <rte_common.h>
#include <rte_rawdev.h>
#include <rte_rawdev_pmd.h>

#include "otx2_common.h"
#include "otx2_ep_enqdeq.h"

static void
sdp_dmazone_free(const struct rte_memzone *mz)
{
	const struct rte_memzone *mz_tmp;
	int ret = 0;

	if (mz == NULL) {
		otx2_err("Memzone %s : NULL", mz->name);
		return;
	}

	mz_tmp = rte_memzone_lookup(mz->name);
	if (mz_tmp == NULL) {
		otx2_err("Memzone %s Not Found", mz->name);
		return;
	}

	ret = rte_memzone_free(mz);
	if (ret)
		otx2_err("Memzone free failed : ret = %d", ret);

}

/* Free IQ resources */
int
sdp_delete_iqs(struct sdp_device *sdpvf, uint32_t iq_no)
{
	struct sdp_instr_queue *iq;

	iq = sdpvf->instr_queue[iq_no];
	if (iq == NULL) {
		otx2_err("Invalid IQ[%d]\n", iq_no);
		return -ENOMEM;
	}

	rte_free(iq->req_list);
	iq->req_list = NULL;

	if (iq->iq_mz) {
		sdp_dmazone_free(iq->iq_mz);
		iq->iq_mz = NULL;
	}

	rte_free(sdpvf->instr_queue[iq_no]);
	sdpvf->instr_queue[iq_no] = NULL;

	sdpvf->num_iqs--;

	otx2_info("IQ[%d] is deleted", iq_no);

	return 0;
}

/* IQ initialization */
static int
sdp_init_instr_queue(struct sdp_device *sdpvf, int iq_no)
{
	const struct sdp_config *conf;
	struct sdp_instr_queue *iq;
	uint32_t q_size;

	conf = sdpvf->conf;
	iq = sdpvf->instr_queue[iq_no];
	q_size = conf->iq.instr_type * conf->num_iqdef_descs;

	/* IQ memory creation for Instruction submission to OCTEON TX2 */
	iq->iq_mz = rte_memzone_reserve_aligned("iqmz",
					q_size,
					rte_socket_id(),
					RTE_MEMZONE_IOVA_CONTIG,
					RTE_CACHE_LINE_SIZE);
	if (iq->iq_mz == NULL) {
		otx2_err("IQ[%d] memzone alloc failed", iq_no);
		goto iq_init_fail;
	}

	iq->base_addr_dma = iq->iq_mz->iova;
	iq->base_addr = (uint8_t *)iq->iq_mz->addr;

	if (conf->num_iqdef_descs & (conf->num_iqdef_descs - 1)) {
		otx2_err("IQ[%d] descs not in power of 2", iq_no);
		goto iq_init_fail;
	}

	iq->nb_desc = conf->num_iqdef_descs;

	/* Create a IQ request list to hold requests that have been
	 * posted to OCTEON TX2. This list will be used for freeing the IQ
	 * data buffer(s) later once the OCTEON TX2 fetched the requests.
	 */
	iq->req_list = rte_zmalloc_socket("request_list",
			(iq->nb_desc * SDP_IQREQ_LIST_SIZE),
			RTE_CACHE_LINE_SIZE,
			rte_socket_id());
	if (iq->req_list == NULL) {
		otx2_err("IQ[%d] req_list alloc failed", iq_no);
		goto iq_init_fail;
	}

	otx2_info("IQ[%d]: base: %p basedma: %lx count: %d",
		     iq_no, iq->base_addr, (unsigned long)iq->base_addr_dma,
		     iq->nb_desc);

	iq->sdp_dev = sdpvf;
	iq->q_no = iq_no;
	iq->fill_cnt = 0;
	iq->host_write_index = 0;
	iq->otx_read_index = 0;
	iq->flush_index = 0;

	/* Initialize the spinlock for this instruction queue */
	rte_spinlock_init(&iq->lock);
	rte_spinlock_init(&iq->post_lock);

	rte_atomic64_clear(&iq->iq_flush_running);

	sdpvf->io_qmask.iq |= (1ull << iq_no);

	/* Set 32B/64B mode for each input queue */
	if (conf->iq.instr_type == 64)
		sdpvf->io_qmask.iq64B |= (1ull << iq_no);

	iq->iqcmd_64B = (conf->iq.instr_type == 64);

	/* Set up IQ registers */
	sdpvf->fn_list.setup_iq_regs(sdpvf, iq_no);

	return 0;

iq_init_fail:
	return -ENOMEM;

}

int
sdp_setup_iqs(struct sdp_device *sdpvf, uint32_t iq_no)
{
	struct sdp_instr_queue *iq;

	iq = (struct sdp_instr_queue *)rte_zmalloc("sdp_IQ", sizeof(*iq),
						RTE_CACHE_LINE_SIZE);
	if (iq == NULL)
		return -ENOMEM;

	sdpvf->instr_queue[iq_no] = iq;

	if (sdp_init_instr_queue(sdpvf, iq_no)) {
		otx2_err("IQ init is failed");
		goto delete_IQ;
	}
	otx2_info("IQ[%d] is created.", sdpvf->num_iqs);

	sdpvf->num_iqs++;


	return 0;

delete_IQ:
	sdp_delete_iqs(sdpvf, iq_no);
	return -ENOMEM;
}

static void
sdp_droq_reset_indices(struct sdp_droq *droq)
{
	droq->read_idx  = 0;
	droq->write_idx = 0;
	droq->refill_idx = 0;
	droq->refill_count = 0;
	rte_atomic64_set(&droq->pkts_pending, 0);
}

static void
sdp_droq_destroy_ring_buffers(struct sdp_device *sdpvf,
				struct sdp_droq *droq)
{
	uint32_t idx;

	for (idx = 0; idx < droq->nb_desc; idx++) {
		if (droq->recv_buf_list[idx].buffer) {
			rte_mempool_put(sdpvf->enqdeq_mpool,
				droq->recv_buf_list[idx].buffer);

			droq->recv_buf_list[idx].buffer = NULL;
		}
	}

	sdp_droq_reset_indices(droq);
}

/* Free OQs resources */
int
sdp_delete_oqs(struct sdp_device *sdpvf, uint32_t oq_no)
{
	struct sdp_droq *droq;

	droq = sdpvf->droq[oq_no];
	if (droq == NULL) {
		otx2_err("Invalid droq[%d]", oq_no);
		return -ENOMEM;
	}

	sdp_droq_destroy_ring_buffers(sdpvf, droq);
	rte_free(droq->recv_buf_list);
	droq->recv_buf_list = NULL;

	if (droq->info_mz) {
		sdp_dmazone_free(droq->info_mz);
		droq->info_mz = NULL;
	}

	if (droq->desc_ring_mz) {
		sdp_dmazone_free(droq->desc_ring_mz);
		droq->desc_ring_mz = NULL;
	}

	memset(droq, 0, SDP_DROQ_SIZE);

	rte_free(sdpvf->droq[oq_no]);
	sdpvf->droq[oq_no] = NULL;

	sdpvf->num_oqs--;

	otx2_info("OQ[%d] is deleted", oq_no);
	return 0;
}

static int
sdp_droq_setup_ring_buffers(struct sdp_device *sdpvf,
		struct sdp_droq *droq)
{
	struct sdp_droq_desc *desc_ring = droq->desc_ring;
	uint32_t idx;
	void *buf;

	for (idx = 0; idx < droq->nb_desc; idx++) {
		if (rte_mempool_get(sdpvf->enqdeq_mpool, &buf) ||
		    (buf == NULL)) {
			otx2_err("OQ buffer alloc failed");
			droq->stats.rx_alloc_failure++;
			/* sdp_droq_destroy_ring_buffers(droq);*/
			return -ENOMEM;
		}

		droq->recv_buf_list[idx].buffer = buf;
		droq->info_list[idx].length = 0;

		/* Map ring buffers into memory */
		desc_ring[idx].info_ptr = (uint64_t)(droq->info_list_dma +
			(idx * SDP_DROQ_INFO_SIZE));

		desc_ring[idx].buffer_ptr = rte_mem_virt2iova(buf);
	}

	sdp_droq_reset_indices(droq);

	return 0;
}

static void *
sdp_alloc_info_buffer(struct sdp_device *sdpvf __rte_unused,
	struct sdp_droq *droq)
{
	droq->info_mz = rte_memzone_reserve_aligned("OQ_info_list",
				(droq->nb_desc * SDP_DROQ_INFO_SIZE),
				rte_socket_id(),
				RTE_MEMZONE_IOVA_CONTIG,
				RTE_CACHE_LINE_SIZE);

	if (droq->info_mz == NULL)
		return NULL;

	droq->info_list_dma = droq->info_mz->iova;
	droq->info_alloc_size = droq->info_mz->len;
	droq->info_base_addr = (size_t)droq->info_mz->addr;

	return droq->info_mz->addr;
}

/* OQ initialization */
static int
sdp_init_droq(struct sdp_device *sdpvf, uint32_t q_no)
{
	const struct sdp_config *conf = sdpvf->conf;
	uint32_t c_refill_threshold;
	uint32_t desc_ring_size;
	struct sdp_droq *droq;

	otx2_info("OQ[%d] Init start", q_no);

	droq = sdpvf->droq[q_no];
	droq->sdp_dev = sdpvf;
	droq->q_no = q_no;

	c_refill_threshold = conf->oq.refill_threshold;
	droq->nb_desc      = conf->num_oqdef_descs;
	droq->buffer_size  = conf->oqdef_buf_size;

	/* OQ desc_ring set up */
	desc_ring_size = droq->nb_desc * SDP_DROQ_DESC_SIZE;
	droq->desc_ring_mz = rte_memzone_reserve_aligned("sdp_oqmz",
						desc_ring_size,
						rte_socket_id(),
						RTE_MEMZONE_IOVA_CONTIG,
						RTE_CACHE_LINE_SIZE);

	if (droq->desc_ring_mz == NULL) {
		otx2_err("OQ:%d desc_ring allocation failed", q_no);
		goto init_droq_fail;
	}

	droq->desc_ring_dma = droq->desc_ring_mz->iova;
	droq->desc_ring = (struct sdp_droq_desc *)droq->desc_ring_mz->addr;

	otx2_sdp_dbg("OQ[%d]: desc_ring: virt: 0x%p, dma: %lx",
		    q_no, droq->desc_ring, (unsigned long)droq->desc_ring_dma);
	otx2_sdp_dbg("OQ[%d]: num_desc: %d", q_no, droq->nb_desc);


	/* OQ info_list set up */
	droq->info_list = sdp_alloc_info_buffer(sdpvf, droq);
	if (droq->info_list == NULL) {
		otx2_err("memory allocation failed for OQ[%d] info_list", q_no);
		goto init_droq_fail;
	}

	/* OQ buf_list set up */
	droq->recv_buf_list = rte_zmalloc_socket("recv_buf_list",
				(droq->nb_desc * SDP_DROQ_RECVBUF_SIZE),
				 RTE_CACHE_LINE_SIZE, rte_socket_id());
	if (droq->recv_buf_list == NULL) {
		otx2_err("OQ recv_buf_list alloc failed");
		goto init_droq_fail;
	}

	if (sdp_droq_setup_ring_buffers(sdpvf, droq))
		goto init_droq_fail;

	droq->refill_threshold = c_refill_threshold;
	rte_spinlock_init(&droq->lock);


	/* Set up OQ registers */
	sdpvf->fn_list.setup_oq_regs(sdpvf, q_no);

	sdpvf->io_qmask.oq |= (1ull << q_no);

	return 0;

init_droq_fail:
	return -ENOMEM;
}

/* OQ configuration and setup */
int
sdp_setup_oqs(struct sdp_device *sdpvf, uint32_t oq_no)
{
	struct sdp_droq *droq;

	/* Allocate new droq. */
	droq = (struct sdp_droq *)rte_zmalloc("sdp_OQ",
				sizeof(*droq), RTE_CACHE_LINE_SIZE);
	if (droq == NULL) {
		otx2_err("Droq[%d] Creation Failed", oq_no);
		return -ENOMEM;
	}
	sdpvf->droq[oq_no] = droq;

	if (sdp_init_droq(sdpvf, oq_no)) {
		otx2_err("Droq[%d] Initialization failed", oq_no);
		goto delete_OQ;
	}
	otx2_info("OQ[%d] is created.", oq_no);

	sdpvf->num_oqs++;

	return 0;

delete_OQ:
	sdp_delete_oqs(sdpvf, oq_no);
	return -ENOMEM;
}

static inline void
sdp_iqreq_delete(struct sdp_device *sdpvf,
		struct sdp_instr_queue *iq, uint32_t idx)
{
	uint32_t reqtype;
	void *buf;

	buf     = iq->req_list[idx].buf;
	reqtype = iq->req_list[idx].reqtype;

	switch (reqtype) {
	case SDP_REQTYPE_NORESP:
		rte_mempool_put(sdpvf->enqdeq_mpool, buf);
		otx2_sdp_dbg("IQ buffer freed at idx[%d]", idx);
		break;

	case SDP_REQTYPE_NORESP_GATHER:
	case SDP_REQTYPE_NONE:
	default:
		otx2_info("This iqreq mode is not supported:%d", reqtype);

	}

	/* Reset the request list at this index */
	iq->req_list[idx].buf = NULL;
	iq->req_list[idx].reqtype = 0;
}

static inline void
sdp_iqreq_add(struct sdp_instr_queue *iq, void *buf,
		uint32_t reqtype)
{
	iq->req_list[iq->host_write_index].buf = buf;
	iq->req_list[iq->host_write_index].reqtype = reqtype;

	otx2_sdp_dbg("IQ buffer added at idx[%d]", iq->host_write_index);

}

static void
sdp_flush_iq(struct sdp_device *sdpvf,
		struct sdp_instr_queue *iq,
		uint32_t pending_thresh __rte_unused)
{
	uint32_t instr_processed = 0;

	rte_spinlock_lock(&iq->lock);

	iq->otx_read_index = sdpvf->fn_list.update_iq_read_idx(iq);
	while (iq->flush_index != iq->otx_read_index) {
		/* Free the IQ data buffer to the pool */
		sdp_iqreq_delete(sdpvf, iq, iq->flush_index);
		iq->flush_index =
			sdp_incr_index(iq->flush_index, 1, iq->nb_desc);

		instr_processed++;
	}

	iq->stats.instr_processed = instr_processed;
	rte_atomic64_sub(&iq->instr_pending, instr_processed);

	rte_spinlock_unlock(&iq->lock);
}

static inline void
sdp_ring_doorbell(struct sdp_device *sdpvf __rte_unused,
		struct sdp_instr_queue *iq)
{
	otx2_write64(iq->fill_cnt, iq->doorbell_reg);

	/* Make sure doorbell writes observed by HW */
	rte_cio_wmb();
	iq->fill_cnt = 0;

}

static inline int
post_iqcmd(struct sdp_instr_queue *iq, uint8_t *iqcmd)
{
	uint8_t *iqptr, cmdsize;

	/* This ensures that the read index does not wrap around to
	 * the same position if queue gets full before OCTEON TX2 could
	 * fetch any instr.
	 */
	if (rte_atomic64_read(&iq->instr_pending) >=
			      (int32_t)(iq->nb_desc - 1)) {
		otx2_err("IQ is full, pending:%ld",
			 (long)rte_atomic64_read(&iq->instr_pending));

		return SDP_IQ_SEND_FAILED;
	}

	/* Copy cmd into iq */
	cmdsize = ((iq->iqcmd_64B) ? 64 : 32);
	iqptr   = iq->base_addr + (cmdsize * iq->host_write_index);

	rte_memcpy(iqptr, iqcmd, cmdsize);

	otx2_sdp_dbg("IQ cmd posted @ index:%d", iq->host_write_index);

	/* Increment the host write index */
	iq->host_write_index =
		sdp_incr_index(iq->host_write_index, 1, iq->nb_desc);

	iq->fill_cnt++;

	/* Flush the command into memory. We need to be sure the data
	 * is in memory before indicating that the instruction is
	 * pending.
	 */
	rte_smp_wmb();
	rte_atomic64_inc(&iq->instr_pending);

	/* SDP_IQ_SEND_SUCCESS */
	return 0;
}


static int
sdp_send_data(struct sdp_device *sdpvf,
	      struct sdp_instr_queue *iq, void *cmd)
{
	uint32_t ret;

	/* Lock this IQ command queue before posting instruction */
	rte_spinlock_lock(&iq->post_lock);

	/* Submit IQ command */
	ret = post_iqcmd(iq, cmd);

	if (ret == SDP_IQ_SEND_SUCCESS) {
		sdp_ring_doorbell(sdpvf, iq);

		iq->stats.instr_posted++;
		otx2_sdp_dbg("Instr submit success posted: %ld\n",
			     (long)iq->stats.instr_posted);

	} else {
		iq->stats.instr_dropped++;
		otx2_err("Instr submit failed, dropped: %ld\n",
			 (long)iq->stats.instr_dropped);

	}

	rte_spinlock_unlock(&iq->post_lock);

	return ret;
}


/* Enqueue requests/packets to SDP IQ queue.
 * returns number of requests enqueued successfully
 */
int
sdp_rawdev_enqueue(struct rte_rawdev *rawdev,
		   struct rte_rawdev_buf **buffers __rte_unused,
		   unsigned int count, rte_rawdev_obj_t context)
{
	struct sdp_instr_64B *iqcmd;
	struct sdp_instr_queue *iq;
	struct sdp_soft_instr *si;
	struct sdp_device *sdpvf;

	struct sdp_instr_ih ihx;

	sdpvf = (struct sdp_device *)rawdev->dev_private;
	si = (struct sdp_soft_instr *)context;

	iq = sdpvf->instr_queue[si->q_no];

	if ((count > 1) || (count < 1)) {
		otx2_err("This mode not supported: req[%d]", count);
		goto enq_fail;
	}

	memset(&ihx, 0, sizeof(struct sdp_instr_ih));

	iqcmd = &si->command;
	memset(iqcmd, 0, sizeof(struct sdp_instr_64B));

	iqcmd->dptr = (uint64_t)si->dptr;

	/* Populate SDP IH */
	ihx.pkind  = sdpvf->pkind;
	ihx.fsz    = si->ih.fsz + 8; /* 8B for NIX IH */
	ihx.gather = si->ih.gather;

	/* Direct data instruction */
	ihx.tlen   = si->ih.tlen + ihx.fsz;

	switch (ihx.gather) {
	case 0: /* Direct data instr */
		ihx.tlen = si->ih.tlen + ihx.fsz;
		break;

	default: /* Gather */
		switch (si->ih.gsz) {
		case 0: /* Direct gather instr */
			otx2_err("Direct Gather instr : not supported");
			goto enq_fail;

		default: /* Indirect gather instr */
			otx2_err("Indirect Gather instr : not supported");
			goto enq_fail;
		}
	}

	rte_memcpy(&iqcmd->ih, &ihx, sizeof(uint64_t));
	iqcmd->rptr = (uint64_t)si->rptr;
	rte_memcpy(&iqcmd->irh, &si->irh, sizeof(uint64_t));

	/* Swap FSZ(front data) here, to avoid swapping on OCTEON TX2 side */
	sdp_swap_8B_data(&iqcmd->rptr, 1);
	sdp_swap_8B_data(&iqcmd->irh, 1);

	otx2_sdp_dbg("After swapping");
	otx2_sdp_dbg("Word0 [dptr]: 0x%016lx", (unsigned long)iqcmd->dptr);
	otx2_sdp_dbg("Word1 [ihtx]: 0x%016lx", (unsigned long)iqcmd->ih);
	otx2_sdp_dbg("Word2 [rptr]: 0x%016lx", (unsigned long)iqcmd->rptr);
	otx2_sdp_dbg("Word3 [irh]: 0x%016lx", (unsigned long)iqcmd->irh);
	otx2_sdp_dbg("Word4 [exhdr[0]]: 0x%016lx",
			(unsigned long)iqcmd->exhdr[0]);

	sdp_iqreq_add(iq, si->dptr, si->reqtype);

	if (sdp_send_data(sdpvf, iq, iqcmd)) {
		otx2_err("Data send failed :");
		sdp_iqreq_delete(sdpvf, iq, iq->host_write_index);
		goto enq_fail;
	}

	if (rte_atomic64_read(&iq->instr_pending) >= 1)
		sdp_flush_iq(sdpvf, iq, 1 /*(iq->nb_desc / 2)*/);

	/* Return no# of instructions posted successfully. */
	return count;

enq_fail:
	return SDP_IQ_SEND_FAILED;
}

static uint32_t
sdp_droq_refill(struct sdp_device *sdpvf, struct sdp_droq *droq)
{
	struct sdp_droq_desc *desc_ring;
	uint32_t desc_refilled = 0;
	void *buf = NULL;

	desc_ring = droq->desc_ring;

	while (droq->refill_count && (desc_refilled < droq->nb_desc)) {
		/* If a valid buffer exists (happens if there is no dispatch),
		 * reuse the buffer, else allocate.
		 */
		if (droq->recv_buf_list[droq->refill_idx].buffer != NULL)
			break;

		if (rte_mempool_get(sdpvf->enqdeq_mpool, &buf) ||
		    (buf == NULL)) {
			/* If a buffer could not be allocated, no point in
			 * continuing
			 */
			droq->stats.rx_alloc_failure++;
			break;
		}

		droq->recv_buf_list[droq->refill_idx].buffer = buf;
		desc_ring[droq->refill_idx].buffer_ptr = rte_mem_virt2iova(buf);

		/* Reset any previous values in the length field. */
		droq->info_list[droq->refill_idx].length = 0;

		droq->refill_idx = sdp_incr_index(droq->refill_idx, 1,
				droq->nb_desc);

		desc_refilled++;
		droq->refill_count--;

	}

	return desc_refilled;
}

static int
sdp_droq_read_packet(struct sdp_device *sdpvf __rte_unused,
		     struct sdp_droq *droq,
		     struct sdp_droq_pkt *droq_pkt)
{
	struct sdp_droq_info *info;
	uint32_t total_len = 0;
	uint32_t pkt_len = 0;

	info = &droq->info_list[droq->read_idx];
	sdp_swap_8B_data((uint64_t *)&info->length, 1);
	if (!info->length) {
		otx2_err("OQ info_list->length[%ld]", (long)info->length);
		goto oq_read_fail;
	}

	/* Deduce the actual data size */
	info->length -= SDP_RH_SIZE;
	total_len += (uint32_t)info->length;

	otx2_sdp_dbg("OQ: pkt_len[%ld], buffer_size %d",
			(long)info->length, droq->buffer_size);
	if (info->length > droq->buffer_size) {
		otx2_err("This mode is not supported: pkt_len > buffer_size");
		goto oq_read_fail;
	}

	if (info->length <= droq->buffer_size) {
		pkt_len = (uint32_t)info->length;
		droq_pkt->data = droq->recv_buf_list[droq->read_idx].buffer;
		droq_pkt->len  = pkt_len;

		droq->recv_buf_list[droq->read_idx].buffer = NULL;
		droq->read_idx = sdp_incr_index(droq->read_idx,	1,/* count */
						droq->nb_desc /* max rd idx */);
		droq->refill_count++;

	}

	info->length = 0;

	return SDP_OQ_RECV_SUCCESS;

oq_read_fail:
	return SDP_OQ_RECV_FAILED;
}

static inline uint32_t
sdp_check_droq_pkts(struct sdp_droq *droq, uint32_t burst_size)
{
	uint32_t min_pkts = 0;
	uint32_t new_pkts;
	uint32_t pkt_count;

	/* Latest available OQ packets */
	pkt_count = rte_read32(droq->pkts_sent_reg);

	/* Newly arrived packets */
	new_pkts = pkt_count - droq->last_pkt_count;
	otx2_sdp_dbg("Recvd [%d] new OQ pkts", new_pkts);

	min_pkts = (new_pkts > burst_size) ? burst_size : new_pkts;
	if (min_pkts) {
		rte_atomic64_add(&droq->pkts_pending, min_pkts);
		/* Back up the aggregated packet count so far */
		droq->last_pkt_count += min_pkts;
	}

	return min_pkts;
}

/* Check for response arrival from OCTEON TX2
 * returns number of requests completed
 */
int
sdp_rawdev_dequeue(struct rte_rawdev *rawdev,
		   struct rte_rawdev_buf **buffers, unsigned int count,
		   rte_rawdev_obj_t context __rte_unused)
{
	struct sdp_droq_pkt *oq_pkt;
	struct sdp_device *sdpvf;
	struct sdp_droq *droq;

	uint32_t q_no = 0, pkts;
	uint32_t new_pkts;
	uint32_t ret;

	sdpvf = (struct sdp_device *)rawdev->dev_private;

	droq = sdpvf->droq[q_no];
	if (!droq) {
		otx2_err("Invalid droq[%d]", q_no);
		goto droq_err;
	}

	/* Grab the lock */
	rte_spinlock_lock(&droq->lock);

	new_pkts = sdp_check_droq_pkts(droq, count);
	if (!new_pkts) {
		otx2_sdp_dbg("Zero new_pkts:%d", new_pkts);
		goto deq_fail; /* No pkts at this moment */
	}

	otx2_sdp_dbg("Received new_pkts = %d", new_pkts);

	for (pkts = 0; pkts < new_pkts; pkts++) {

		/* Push the received pkt to application */
		oq_pkt = (struct sdp_droq_pkt *)buffers[pkts];

		ret = sdp_droq_read_packet(sdpvf, droq, oq_pkt);
		if (ret) {
			otx2_err("DROQ read pakt failed.");
			goto deq_fail;
		}

		/* Stats */
		droq->stats.pkts_received++;
		droq->stats.bytes_received += oq_pkt->len;
	}

	/* Ack the h/w with no# of pkts read by Host */
	rte_write32(pkts, droq->pkts_sent_reg);
	rte_cio_wmb();

	droq->last_pkt_count -= pkts;

	otx2_sdp_dbg("DROQ pkts[%d] pushed to application", pkts);

	/* Refill DROQ buffers */
	if (droq->refill_count >= 2 /* droq->refill_threshold */) {
		int desc_refilled = sdp_droq_refill(sdpvf, droq);

		/* Flush the droq descriptor data to memory to be sure
		 * that when we update the credits the data in memory is
		 * accurate.
		 */
		rte_write32(desc_refilled, droq->pkts_credit_reg);

		/* Ensure mmio write completes */
		rte_wmb();
		otx2_sdp_dbg("Refilled count = %d", desc_refilled);
	}

	/* Release the spin lock */
	rte_spinlock_unlock(&droq->lock);

	return pkts;

deq_fail:
	rte_spinlock_unlock(&droq->lock);

droq_err:
	return SDP_OQ_RECV_FAILED;
}