4 * Copyright(c) 2017 Cavium, Inc.. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Cavium, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <rte_ethdev.h>
35 #include <rte_cycles.h>
36 #include <rte_malloc.h>
39 #include "lio_struct.h"
40 #include "lio_ethdev.h"
46 lio_droq_compute_max_packet_bufs(struct lio_droq *droq)
51 count += droq->buffer_size;
52 } while (count < LIO_MAX_RX_PKTLEN);
56 lio_droq_reset_indices(struct lio_droq *droq)
61 droq->refill_count = 0;
62 rte_atomic64_set(&droq->pkts_pending, 0);
66 lio_droq_destroy_ring_buffers(struct lio_droq *droq)
70 for (i = 0; i < droq->max_count; i++) {
71 if (droq->recv_buf_list[i].buffer) {
72 rte_pktmbuf_free((struct rte_mbuf *)
73 droq->recv_buf_list[i].buffer);
74 droq->recv_buf_list[i].buffer = NULL;
78 lio_droq_reset_indices(droq);
82 lio_recv_buffer_alloc(struct lio_device *lio_dev, int q_no)
84 struct lio_droq *droq = lio_dev->droq[q_no];
85 struct rte_mempool *mpool = droq->mpool;
88 m = rte_pktmbuf_alloc(mpool);
90 lio_dev_err(lio_dev, "Cannot allocate\n");
94 rte_mbuf_refcnt_set(m, 1);
96 m->data_off = RTE_PKTMBUF_HEADROOM;
104 lio_droq_setup_ring_buffers(struct lio_device *lio_dev,
105 struct lio_droq *droq)
107 struct lio_droq_desc *desc_ring = droq->desc_ring;
111 for (i = 0; i < droq->max_count; i++) {
112 buf = lio_recv_buffer_alloc(lio_dev, droq->q_no);
114 lio_dev_err(lio_dev, "buffer alloc failed\n");
115 lio_droq_destroy_ring_buffers(droq);
119 droq->recv_buf_list[i].buffer = buf;
120 droq->info_list[i].length = 0;
122 /* map ring buffers into memory */
123 desc_ring[i].info_ptr = lio_map_ring_info(droq, i);
124 desc_ring[i].buffer_ptr =
125 lio_map_ring(droq->recv_buf_list[i].buffer);
128 lio_droq_reset_indices(droq);
130 lio_droq_compute_max_packet_bufs(droq);
136 lio_dma_zone_free(struct lio_device *lio_dev, const struct rte_memzone *mz)
138 const struct rte_memzone *mz_tmp;
142 lio_dev_err(lio_dev, "Memzone NULL\n");
146 mz_tmp = rte_memzone_lookup(mz->name);
147 if (mz_tmp == NULL) {
148 lio_dev_err(lio_dev, "Memzone %s Not Found\n", mz->name);
152 ret = rte_memzone_free(mz);
154 lio_dev_err(lio_dev, "Memzone free Failed ret %d\n", ret);
158 * Frees the space for descriptor ring for the droq.
160 * @param lio_dev - pointer to the lio device structure
161 * @param q_no - droq no.
164 lio_delete_droq(struct lio_device *lio_dev, uint32_t q_no)
166 struct lio_droq *droq = lio_dev->droq[q_no];
168 lio_dev_dbg(lio_dev, "OQ[%d]\n", q_no);
170 lio_droq_destroy_ring_buffers(droq);
171 rte_free(droq->recv_buf_list);
172 droq->recv_buf_list = NULL;
173 lio_dma_zone_free(lio_dev, droq->info_mz);
174 lio_dma_zone_free(lio_dev, droq->desc_ring_mz);
176 memset(droq, 0, LIO_DROQ_SIZE);
180 lio_alloc_info_buffer(struct lio_device *lio_dev,
181 struct lio_droq *droq, unsigned int socket_id)
183 droq->info_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
184 "info_list", droq->q_no,
190 if (droq->info_mz == NULL)
193 droq->info_list_dma = droq->info_mz->phys_addr;
194 droq->info_alloc_size = droq->info_mz->len;
195 droq->info_base_addr = (size_t)droq->info_mz->addr;
197 return droq->info_mz->addr;
201 * Allocates space for the descriptor ring for the droq and
202 * sets the base addr, num desc etc in Octeon registers.
204 * @param lio_dev - pointer to the lio device structure
205 * @param q_no - droq no.
206 * @param app_ctx - pointer to application context
207 * @return Success: 0 Failure: -1
210 lio_init_droq(struct lio_device *lio_dev, uint32_t q_no,
211 uint32_t num_descs, uint32_t desc_size,
212 struct rte_mempool *mpool, unsigned int socket_id)
214 uint32_t c_refill_threshold;
215 uint32_t desc_ring_size;
216 struct lio_droq *droq;
218 lio_dev_dbg(lio_dev, "OQ[%d]\n", q_no);
220 droq = lio_dev->droq[q_no];
221 droq->lio_dev = lio_dev;
225 c_refill_threshold = LIO_OQ_REFILL_THRESHOLD_CFG(lio_dev);
227 droq->max_count = num_descs;
228 droq->buffer_size = desc_size;
230 desc_ring_size = droq->max_count * LIO_DROQ_DESC_SIZE;
231 droq->desc_ring_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
237 if (droq->desc_ring_mz == NULL) {
239 "Output queue %d ring alloc failed\n", q_no);
243 droq->desc_ring_dma = droq->desc_ring_mz->phys_addr;
244 droq->desc_ring = (struct lio_droq_desc *)droq->desc_ring_mz->addr;
246 lio_dev_dbg(lio_dev, "droq[%d]: desc_ring: virt: 0x%p, dma: %lx\n",
247 q_no, droq->desc_ring, (unsigned long)droq->desc_ring_dma);
248 lio_dev_dbg(lio_dev, "droq[%d]: num_desc: %d\n", q_no,
251 droq->info_list = lio_alloc_info_buffer(lio_dev, droq, socket_id);
252 if (droq->info_list == NULL) {
253 lio_dev_err(lio_dev, "Cannot allocate memory for info list.\n");
257 droq->recv_buf_list = rte_zmalloc_socket("recv_buf_list",
259 LIO_DROQ_RECVBUF_SIZE),
262 if (droq->recv_buf_list == NULL) {
264 "Output queue recv buf list alloc failed\n");
268 if (lio_droq_setup_ring_buffers(lio_dev, droq))
271 droq->refill_threshold = c_refill_threshold;
273 rte_spinlock_init(&droq->lock);
275 lio_dev->fn_list.setup_oq_regs(lio_dev, q_no);
277 lio_dev->io_qmask.oq |= (1ULL << q_no);
282 lio_delete_droq(lio_dev, q_no);
288 lio_setup_droq(struct lio_device *lio_dev, int oq_no, int num_descs,
289 int desc_size, struct rte_mempool *mpool, unsigned int socket_id)
291 struct lio_droq *droq;
293 PMD_INIT_FUNC_TRACE();
295 if (lio_dev->droq[oq_no]) {
296 lio_dev_dbg(lio_dev, "Droq %d in use\n", oq_no);
300 /* Allocate the DS for the new droq. */
301 droq = rte_zmalloc_socket("ethdev RX queue", sizeof(*droq),
302 RTE_CACHE_LINE_SIZE, socket_id);
306 lio_dev->droq[oq_no] = droq;
308 /* Initialize the Droq */
309 if (lio_init_droq(lio_dev, oq_no, num_descs, desc_size, mpool,
311 lio_dev_err(lio_dev, "Droq[%u] Initialization Failed\n", oq_no);
312 rte_free(lio_dev->droq[oq_no]);
313 lio_dev->droq[oq_no] = NULL;
319 lio_dev_dbg(lio_dev, "Total number of OQ: %d\n", lio_dev->num_oqs);
321 /* Send credit for octeon output queues. credits are always
322 * sent after the output queue is enabled.
324 rte_write32(lio_dev->droq[oq_no]->max_count,
325 lio_dev->droq[oq_no]->pkts_credit_reg);
331 static inline uint32_t
332 lio_droq_get_bufcount(uint32_t buf_size, uint32_t total_len)
334 uint32_t buf_cnt = 0;
336 while (total_len > (buf_size * buf_cnt))
342 /* If we were not able to refill all buffers, try to move around
343 * the buffers that were not dispatched.
345 static inline uint32_t
346 lio_droq_refill_pullup_descs(struct lio_droq *droq,
347 struct lio_droq_desc *desc_ring)
349 uint32_t refill_index = droq->refill_idx;
350 uint32_t desc_refilled = 0;
352 while (refill_index != droq->read_idx) {
353 if (droq->recv_buf_list[refill_index].buffer) {
354 droq->recv_buf_list[droq->refill_idx].buffer =
355 droq->recv_buf_list[refill_index].buffer;
356 desc_ring[droq->refill_idx].buffer_ptr =
357 desc_ring[refill_index].buffer_ptr;
358 droq->recv_buf_list[refill_index].buffer = NULL;
359 desc_ring[refill_index].buffer_ptr = 0;
361 droq->refill_idx = lio_incr_index(
365 droq->refill_count--;
366 } while (droq->recv_buf_list[droq->refill_idx].buffer);
368 refill_index = lio_incr_index(refill_index, 1,
372 return desc_refilled;
377 * @param lio_dev - pointer to the lio device structure
378 * @param droq - droq in which descriptors require new buffers.
381 * Called during normal DROQ processing in interrupt mode or by the poll
382 * thread to refill the descriptors from which buffers were dispatched
383 * to upper layers. Attempts to allocate new buffers. If that fails, moves
384 * up buffers (that were not dispatched) to form a contiguous ring.
387 * No of descriptors refilled.
390 * This routine is called with droq->lock held.
393 lio_droq_refill(struct lio_device *lio_dev, struct lio_droq *droq)
395 struct lio_droq_desc *desc_ring;
396 uint32_t desc_refilled = 0;
399 desc_ring = droq->desc_ring;
401 while (droq->refill_count && (desc_refilled < droq->max_count)) {
402 /* If a valid buffer exists (happens if there is no dispatch),
403 * reuse the buffer, else allocate.
405 if (droq->recv_buf_list[droq->refill_idx].buffer == NULL) {
406 buf = lio_recv_buffer_alloc(lio_dev, droq->q_no);
407 /* If a buffer could not be allocated, no point in
413 droq->recv_buf_list[droq->refill_idx].buffer = buf;
416 desc_ring[droq->refill_idx].buffer_ptr =
417 lio_map_ring(droq->recv_buf_list[droq->refill_idx].buffer);
418 /* Reset any previous values in the length field. */
419 droq->info_list[droq->refill_idx].length = 0;
421 droq->refill_idx = lio_incr_index(droq->refill_idx, 1,
424 droq->refill_count--;
427 if (droq->refill_count)
428 desc_refilled += lio_droq_refill_pullup_descs(droq, desc_ring);
430 /* if droq->refill_count
431 * The refill count would not change in pass two. We only moved buffers
432 * to close the gap in the ring, but we would still have the same no. of
435 return desc_refilled;
439 lio_droq_fast_process_packet(struct lio_device *lio_dev,
440 struct lio_droq *droq,
441 struct rte_mbuf **rx_pkts)
443 struct rte_mbuf *nicbuf = NULL;
444 struct lio_droq_info *info;
445 uint32_t total_len = 0;
446 int data_total_len = 0;
447 uint32_t pkt_len = 0;
451 info = &droq->info_list[droq->read_idx];
452 lio_swap_8B_data((uint64_t *)info, 2);
457 /* Len of resp hdr in included in the received data len. */
458 info->length -= OCTEON_RH_SIZE;
461 total_len += (uint32_t)info->length;
463 if (lio_opcode_slow_path(rh)) {
466 buf_cnt = lio_droq_get_bufcount(droq->buffer_size,
467 (uint32_t)info->length);
468 droq->read_idx = lio_incr_index(droq->read_idx, buf_cnt,
470 droq->refill_count += buf_cnt;
472 if (info->length <= droq->buffer_size) {
473 if (rh->r_dh.has_hash)
474 pkt_len = (uint32_t)(info->length - 8);
476 pkt_len = (uint32_t)info->length;
478 nicbuf = droq->recv_buf_list[droq->read_idx].buffer;
479 droq->recv_buf_list[droq->read_idx].buffer = NULL;
480 droq->read_idx = lio_incr_index(
483 droq->refill_count++;
485 if (likely(nicbuf != NULL)) {
486 nicbuf->data_off = RTE_PKTMBUF_HEADROOM;
489 /* We don't have a way to pass flags yet */
490 nicbuf->ol_flags = 0;
491 if (rh->r_dh.has_hash) {
494 nicbuf->ol_flags |= PKT_RX_RSS_HASH;
495 hash_ptr = rte_pktmbuf_mtod(nicbuf,
497 lio_swap_8B_data(hash_ptr, 1);
498 nicbuf->hash.rss = (uint32_t)*hash_ptr;
499 nicbuf->data_off += 8;
502 nicbuf->pkt_len = pkt_len;
503 nicbuf->data_len = pkt_len;
504 nicbuf->port = lio_dev->port_id;
506 rx_pkts[data_pkts++] = nicbuf;
507 data_total_len += pkt_len;
510 /* Prefetch buffer pointers when on a cache line
513 if ((droq->read_idx & 3) == 0) {
515 &droq->recv_buf_list[droq->read_idx]);
517 &droq->info_list[droq->read_idx]);
520 struct rte_mbuf *first_buf = NULL;
521 struct rte_mbuf *last_buf = NULL;
523 while (pkt_len < info->length) {
526 cpy_len = ((pkt_len + droq->buffer_size) >
528 ? ((uint32_t)info->length -
533 droq->recv_buf_list[droq->read_idx].buffer;
534 droq->recv_buf_list[droq->read_idx].buffer =
537 if (likely(nicbuf != NULL)) {
538 /* Note the first seg */
542 nicbuf->data_off = RTE_PKTMBUF_HEADROOM;
545 nicbuf->port = lio_dev->port_id;
546 /* We don't have a way to pass
549 nicbuf->ol_flags = 0;
550 if ((!pkt_len) && (rh->r_dh.has_hash)) {
555 hash_ptr = rte_pktmbuf_mtod(
557 lio_swap_8B_data(hash_ptr, 1);
560 nicbuf->data_off += 8;
561 nicbuf->pkt_len = cpy_len - 8;
562 nicbuf->data_len = cpy_len - 8;
564 nicbuf->pkt_len = cpy_len;
565 nicbuf->data_len = cpy_len;
569 first_buf->nb_segs++;
572 last_buf->next = nicbuf;
576 PMD_RX_LOG(lio_dev, ERR, "no buf\n");
580 droq->read_idx = lio_incr_index(
583 droq->refill_count++;
585 /* Prefetch buffer pointers when on a
586 * cache line boundary
588 if ((droq->read_idx & 3) == 0) {
589 rte_prefetch0(&droq->recv_buf_list
593 &droq->info_list[droq->read_idx]);
596 rx_pkts[data_pkts++] = first_buf;
597 if (rh->r_dh.has_hash)
598 data_total_len += (pkt_len - 8);
600 data_total_len += pkt_len;
603 /* Inform upper layer about packet checksum verification */
604 struct rte_mbuf *m = rx_pkts[data_pkts - 1];
606 if (rh->r_dh.csum_verified & LIO_IP_CSUM_VERIFIED)
607 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
609 if (rh->r_dh.csum_verified & LIO_L4_CSUM_VERIFIED)
610 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
613 if (droq->refill_count >= droq->refill_threshold) {
614 int desc_refilled = lio_droq_refill(lio_dev, droq);
616 /* Flush the droq descriptor data to memory to be sure
617 * that when we update the credits the data in memory is
621 rte_write32(desc_refilled, droq->pkts_credit_reg);
622 /* make sure mmio write completes */
633 lio_droq_fast_process_packets(struct lio_device *lio_dev,
634 struct lio_droq *droq,
635 struct rte_mbuf **rx_pkts,
636 uint32_t pkts_to_process)
638 int ret, data_pkts = 0;
641 for (pkt = 0; pkt < pkts_to_process; pkt++) {
642 ret = lio_droq_fast_process_packet(lio_dev, droq,
643 &rx_pkts[data_pkts]);
645 lio_dev_err(lio_dev, "Port[%d] DROQ[%d] idx: %d len:0, pkt_cnt: %d\n",
646 lio_dev->port_id, droq->q_no,
647 droq->read_idx, pkts_to_process);
653 rte_atomic64_sub(&droq->pkts_pending, pkt);
658 static inline uint32_t
659 lio_droq_check_hw_for_pkts(struct lio_droq *droq)
664 pkt_count = rte_read32(droq->pkts_sent_reg);
666 last_count = pkt_count - droq->pkt_count;
667 droq->pkt_count = pkt_count;
670 rte_atomic64_add(&droq->pkts_pending, last_count);
676 lio_dev_recv_pkts(void *rx_queue,
677 struct rte_mbuf **rx_pkts,
680 struct lio_droq *droq = rx_queue;
681 struct lio_device *lio_dev = droq->lio_dev;
682 uint32_t pkts_processed = 0;
683 uint32_t pkt_count = 0;
685 lio_droq_check_hw_for_pkts(droq);
687 pkt_count = rte_atomic64_read(&droq->pkts_pending);
691 if (pkt_count > budget)
695 rte_spinlock_lock(&droq->lock);
696 pkts_processed = lio_droq_fast_process_packets(lio_dev,
700 if (droq->pkt_count) {
701 rte_write32(droq->pkt_count, droq->pkts_sent_reg);
705 /* Release the spin lock */
706 rte_spinlock_unlock(&droq->lock);
708 return pkts_processed;
712 lio_delete_droq_queue(struct lio_device *lio_dev,
715 lio_delete_droq(lio_dev, oq_no);
717 rte_free(lio_dev->droq[oq_no]);
718 lio_dev->droq[oq_no] = NULL;
722 * lio_init_instr_queue()
723 * @param lio_dev - pointer to the lio device structure.
724 * @param txpciq - queue to be initialized.
726 * Called at driver init time for each input queue. iq_conf has the
727 * configuration parameters for the queue.
729 * @return Success: 0 Failure: -1
732 lio_init_instr_queue(struct lio_device *lio_dev,
733 union octeon_txpciq txpciq,
734 uint32_t num_descs, unsigned int socket_id)
736 uint32_t iq_no = (uint32_t)txpciq.s.q_no;
737 struct lio_instr_queue *iq;
741 instr_type = LIO_IQ_INSTR_TYPE(lio_dev);
743 q_size = instr_type * num_descs;
744 iq = lio_dev->instr_queue[iq_no];
745 iq->iq_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
746 "instr_queue", iq_no, q_size,
749 if (iq->iq_mz == NULL) {
750 lio_dev_err(lio_dev, "Cannot allocate memory for instr queue %d\n",
755 iq->base_addr_dma = iq->iq_mz->phys_addr;
756 iq->base_addr = (uint8_t *)iq->iq_mz->addr;
758 iq->max_count = num_descs;
760 /* Initialize a list to holds requests that have been posted to Octeon
761 * but has yet to be fetched by octeon
763 iq->request_list = rte_zmalloc_socket("request_list",
764 sizeof(*iq->request_list) *
768 if (iq->request_list == NULL) {
769 lio_dev_err(lio_dev, "Alloc failed for IQ[%d] nr free list\n",
771 lio_dma_zone_free(lio_dev, iq->iq_mz);
775 lio_dev_dbg(lio_dev, "IQ[%d]: base: %p basedma: %lx count: %d\n",
776 iq_no, iq->base_addr, (unsigned long)iq->base_addr_dma,
779 iq->lio_dev = lio_dev;
780 iq->txpciq.txpciq64 = txpciq.txpciq64;
782 iq->host_write_index = 0;
783 iq->lio_read_index = 0;
786 rte_atomic64_set(&iq->instr_pending, 0);
788 /* Initialize the spinlock for this instruction queue */
789 rte_spinlock_init(&iq->lock);
790 rte_spinlock_init(&iq->post_lock);
792 rte_atomic64_clear(&iq->iq_flush_running);
794 lio_dev->io_qmask.iq |= (1ULL << iq_no);
796 /* Set the 32B/64B mode for each input queue */
797 lio_dev->io_qmask.iq64B |= ((instr_type == 64) << iq_no);
798 iq->iqcmd_64B = (instr_type == 64);
800 lio_dev->fn_list.setup_iq_regs(lio_dev, iq_no);
806 lio_setup_instr_queue0(struct lio_device *lio_dev)
808 union octeon_txpciq txpciq;
809 uint32_t num_descs = 0;
812 num_descs = LIO_NUM_DEF_TX_DESCS_CFG(lio_dev);
814 lio_dev->num_iqs = 0;
816 lio_dev->instr_queue[0] = rte_zmalloc(NULL,
817 sizeof(struct lio_instr_queue), 0);
818 if (lio_dev->instr_queue[0] == NULL)
821 lio_dev->instr_queue[0]->q_index = 0;
822 lio_dev->instr_queue[0]->app_ctx = (void *)(size_t)0;
824 txpciq.s.q_no = iq_no;
825 txpciq.s.pkind = lio_dev->pfvf_hsword.pkind;
826 txpciq.s.use_qpg = 0;
828 if (lio_init_instr_queue(lio_dev, txpciq, num_descs, SOCKET_ID_ANY)) {
829 rte_free(lio_dev->instr_queue[0]);
830 lio_dev->instr_queue[0] = NULL;
840 * lio_delete_instr_queue()
841 * @param lio_dev - pointer to the lio device structure.
842 * @param iq_no - queue to be deleted.
844 * Called at driver unload time for each input queue. Deletes all
845 * allocated resources for the input queue.
848 lio_delete_instr_queue(struct lio_device *lio_dev, uint32_t iq_no)
850 struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
852 rte_free(iq->request_list);
853 iq->request_list = NULL;
854 lio_dma_zone_free(lio_dev, iq->iq_mz);
858 lio_free_instr_queue0(struct lio_device *lio_dev)
860 lio_delete_instr_queue(lio_dev, 0);
861 rte_free(lio_dev->instr_queue[0]);
862 lio_dev->instr_queue[0] = NULL;
866 /* Return 0 on success, -1 on failure */
868 lio_setup_iq(struct lio_device *lio_dev, int q_index,
869 union octeon_txpciq txpciq, uint32_t num_descs, void *app_ctx,
870 unsigned int socket_id)
872 uint32_t iq_no = (uint32_t)txpciq.s.q_no;
874 if (lio_dev->instr_queue[iq_no]) {
875 lio_dev_dbg(lio_dev, "IQ is in use. Cannot create the IQ: %d again\n",
877 lio_dev->instr_queue[iq_no]->txpciq.txpciq64 = txpciq.txpciq64;
878 lio_dev->instr_queue[iq_no]->app_ctx = app_ctx;
882 lio_dev->instr_queue[iq_no] = rte_zmalloc_socket("ethdev TX queue",
883 sizeof(struct lio_instr_queue),
884 RTE_CACHE_LINE_SIZE, socket_id);
885 if (lio_dev->instr_queue[iq_no] == NULL)
888 lio_dev->instr_queue[iq_no]->q_index = q_index;
889 lio_dev->instr_queue[iq_no]->app_ctx = app_ctx;
891 if (lio_init_instr_queue(lio_dev, txpciq, num_descs, socket_id))
895 if (lio_dev->fn_list.enable_io_queues(lio_dev))
901 lio_delete_instr_queue(lio_dev, iq_no);
904 rte_free(lio_dev->instr_queue[iq_no]);
905 lio_dev->instr_queue[iq_no] = NULL;
911 lio_ring_doorbell(struct lio_device *lio_dev,
912 struct lio_instr_queue *iq)
914 if (rte_atomic64_read(&lio_dev->status) == LIO_DEV_RUNNING) {
915 rte_write32(iq->fill_cnt, iq->doorbell_reg);
916 /* make sure doorbell write goes through */
923 copy_cmd_into_iq(struct lio_instr_queue *iq, uint8_t *cmd)
925 uint8_t *iqptr, cmdsize;
927 cmdsize = ((iq->iqcmd_64B) ? 64 : 32);
928 iqptr = iq->base_addr + (cmdsize * iq->host_write_index);
930 rte_memcpy(iqptr, cmd, cmdsize);
933 static inline struct lio_iq_post_status
934 post_command2(struct lio_instr_queue *iq, uint8_t *cmd)
936 struct lio_iq_post_status st;
938 st.status = LIO_IQ_SEND_OK;
940 /* This ensures that the read index does not wrap around to the same
941 * position if queue gets full before Octeon could fetch any instr.
943 if (rte_atomic64_read(&iq->instr_pending) >=
944 (int32_t)(iq->max_count - 1)) {
945 st.status = LIO_IQ_SEND_FAILED;
950 if (rte_atomic64_read(&iq->instr_pending) >=
951 (int32_t)(iq->max_count - 2))
952 st.status = LIO_IQ_SEND_STOP;
954 copy_cmd_into_iq(iq, cmd);
956 /* "index" is returned, host_write_index is modified. */
957 st.index = iq->host_write_index;
958 iq->host_write_index = lio_incr_index(iq->host_write_index, 1,
962 /* Flush the command into memory. We need to be sure the data is in
963 * memory before indicating that the instruction is pending.
967 rte_atomic64_inc(&iq->instr_pending);
973 lio_add_to_request_list(struct lio_instr_queue *iq,
974 int idx, void *buf, int reqtype)
976 iq->request_list[idx].buf = buf;
977 iq->request_list[idx].reqtype = reqtype;
981 lio_send_command(struct lio_device *lio_dev, uint32_t iq_no, void *cmd,
982 void *buf, uint32_t datasize __rte_unused, uint32_t reqtype)
984 struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
985 struct lio_iq_post_status st;
987 rte_spinlock_lock(&iq->post_lock);
989 st = post_command2(iq, cmd);
991 if (st.status != LIO_IQ_SEND_FAILED) {
992 lio_add_to_request_list(iq, st.index, buf, reqtype);
993 lio_ring_doorbell(lio_dev, iq);
996 rte_spinlock_unlock(&iq->post_lock);
1002 lio_prepare_soft_command(struct lio_device *lio_dev,
1003 struct lio_soft_command *sc, uint8_t opcode,
1004 uint8_t subcode, uint32_t irh_ossp, uint64_t ossp0,
1007 struct octeon_instr_pki_ih3 *pki_ih3;
1008 struct octeon_instr_ih3 *ih3;
1009 struct octeon_instr_irh *irh;
1010 struct octeon_instr_rdp *rdp;
1012 RTE_ASSERT(opcode <= 15);
1013 RTE_ASSERT(subcode <= 127);
1015 ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
1017 ih3->pkind = lio_dev->instr_queue[sc->iq_no]->txpciq.s.pkind;
1019 pki_ih3 = (struct octeon_instr_pki_ih3 *)&sc->cmd.cmd3.pki_ih3;
1024 pki_ih3->uqpg = lio_dev->instr_queue[sc->iq_no]->txpciq.s.use_qpg;
1027 pki_ih3->tag = LIO_CONTROL;
1028 pki_ih3->tagtype = OCTEON_ATOMIC_TAG;
1029 pki_ih3->qpg = lio_dev->instr_queue[sc->iq_no]->txpciq.s.qpg;
1034 ih3->dlengsz = sc->datasize;
1036 irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
1037 irh->opcode = opcode;
1038 irh->subcode = subcode;
1040 /* opcode/subcode specific parameters (ossp) */
1041 irh->ossp = irh_ossp;
1042 sc->cmd.cmd3.ossp[0] = ossp0;
1043 sc->cmd.cmd3.ossp[1] = ossp1;
1045 if (sc->rdatasize) {
1046 rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
1047 rdp->pcie_port = lio_dev->pcie_port;
1048 rdp->rlen = sc->rdatasize;
1051 ih3->fsz = OCTEON_SOFT_CMD_RESP_IH3;
1055 ih3->fsz = OCTEON_PCI_CMD_O3;
1060 lio_send_soft_command(struct lio_device *lio_dev,
1061 struct lio_soft_command *sc)
1063 struct octeon_instr_ih3 *ih3;
1064 struct octeon_instr_irh *irh;
1067 ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
1069 RTE_ASSERT(sc->dmadptr);
1070 sc->cmd.cmd3.dptr = sc->dmadptr;
1073 irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
1075 RTE_ASSERT(sc->dmarptr);
1076 RTE_ASSERT(sc->status_word != NULL);
1077 *sc->status_word = LIO_COMPLETION_WORD_INIT;
1078 sc->cmd.cmd3.rptr = sc->dmarptr;
1081 len = (uint32_t)ih3->dlengsz;
1084 sc->timeout = lio_uptime + sc->wait_time;
1086 return lio_send_command(lio_dev, sc->iq_no, &sc->cmd, sc, len,
1087 LIO_REQTYPE_SOFT_COMMAND);
1091 lio_setup_sc_buffer_pool(struct lio_device *lio_dev)
1093 char sc_pool_name[RTE_MEMPOOL_NAMESIZE];
1096 buf_size = LIO_SOFT_COMMAND_BUFFER_SIZE + RTE_PKTMBUF_HEADROOM;
1097 snprintf(sc_pool_name, sizeof(sc_pool_name),
1098 "lio_sc_pool_%u", lio_dev->port_id);
1099 lio_dev->sc_buf_pool = rte_pktmbuf_pool_create(sc_pool_name,
1100 LIO_MAX_SOFT_COMMAND_BUFFERS,
1101 0, 0, buf_size, SOCKET_ID_ANY);
1106 lio_free_sc_buffer_pool(struct lio_device *lio_dev)
1108 rte_mempool_free(lio_dev->sc_buf_pool);
1111 struct lio_soft_command *
1112 lio_alloc_soft_command(struct lio_device *lio_dev, uint32_t datasize,
1113 uint32_t rdatasize, uint32_t ctxsize)
1115 uint32_t offset = sizeof(struct lio_soft_command);
1116 struct lio_soft_command *sc;
1120 RTE_ASSERT((offset + datasize + rdatasize + ctxsize) <=
1121 LIO_SOFT_COMMAND_BUFFER_SIZE);
1123 m = rte_pktmbuf_alloc(lio_dev->sc_buf_pool);
1125 lio_dev_err(lio_dev, "Cannot allocate mbuf for sc\n");
1129 /* set rte_mbuf data size and there is only 1 segment */
1130 m->pkt_len = LIO_SOFT_COMMAND_BUFFER_SIZE;
1131 m->data_len = LIO_SOFT_COMMAND_BUFFER_SIZE;
1133 /* use rte_mbuf buffer for soft command */
1134 sc = rte_pktmbuf_mtod(m, struct lio_soft_command *);
1135 memset(sc, 0, LIO_SOFT_COMMAND_BUFFER_SIZE);
1136 sc->size = LIO_SOFT_COMMAND_BUFFER_SIZE;
1137 sc->dma_addr = rte_mbuf_data_dma_addr(m);
1140 dma_addr = sc->dma_addr;
1143 sc->ctxptr = (uint8_t *)sc + offset;
1144 sc->ctxsize = ctxsize;
1147 /* Start data at 128 byte boundary */
1148 offset = (offset + ctxsize + 127) & 0xffffff80;
1151 sc->virtdptr = (uint8_t *)sc + offset;
1152 sc->dmadptr = dma_addr + offset;
1153 sc->datasize = datasize;
1156 /* Start rdata at 128 byte boundary */
1157 offset = (offset + datasize + 127) & 0xffffff80;
1160 RTE_ASSERT(rdatasize >= 16);
1161 sc->virtrptr = (uint8_t *)sc + offset;
1162 sc->dmarptr = dma_addr + offset;
1163 sc->rdatasize = rdatasize;
1164 sc->status_word = (uint64_t *)((uint8_t *)(sc->virtrptr) +
1172 lio_free_soft_command(struct lio_soft_command *sc)
1174 rte_pktmbuf_free(sc->mbuf);
1178 lio_setup_response_list(struct lio_device *lio_dev)
1180 STAILQ_INIT(&lio_dev->response_list.head);
1181 rte_spinlock_init(&lio_dev->response_list.lock);
1182 rte_atomic64_set(&lio_dev->response_list.pending_req_count, 0);
1186 lio_process_ordered_list(struct lio_device *lio_dev)
1188 int resp_to_process = LIO_MAX_ORD_REQS_TO_PROCESS;
1189 struct lio_response_list *ordered_sc_list;
1190 struct lio_soft_command *sc;
1191 int request_complete = 0;
1195 ordered_sc_list = &lio_dev->response_list;
1198 rte_spinlock_lock(&ordered_sc_list->lock);
1200 if (STAILQ_EMPTY(&ordered_sc_list->head)) {
1201 /* ordered_sc_list is empty; there is
1202 * nothing to process
1204 rte_spinlock_unlock(&ordered_sc_list->lock);
1208 sc = LIO_STQUEUE_FIRST_ENTRY(&ordered_sc_list->head,
1209 struct lio_soft_command, node);
1211 status = LIO_REQUEST_PENDING;
1213 /* check if octeon has finished DMA'ing a response
1214 * to where rptr is pointing to
1216 status64 = *sc->status_word;
1218 if (status64 != LIO_COMPLETION_WORD_INIT) {
1219 /* This logic ensures that all 64b have been written.
1220 * 1. check byte 0 for non-FF
1221 * 2. if non-FF, then swap result from BE to host order
1222 * 3. check byte 7 (swapped to 0) for non-FF
1223 * 4. if non-FF, use the low 32-bit status code
1224 * 5. if either byte 0 or byte 7 is FF, don't use status
1226 if ((status64 & 0xff) != 0xff) {
1227 lio_swap_8B_data(&status64, 1);
1228 if (((status64 & 0xff) != 0xff)) {
1229 /* retrieve 16-bit firmware status */
1230 status = (uint32_t)(status64 &
1234 LIO_FIRMWARE_STATUS_CODE(
1238 status = LIO_REQUEST_DONE;
1242 } else if ((sc->timeout && lio_check_timeout(lio_uptime,
1244 lio_dev_err(lio_dev,
1245 "cmd failed, timeout (%ld, %ld)\n",
1246 (long)lio_uptime, (long)sc->timeout);
1247 status = LIO_REQUEST_TIMEOUT;
1250 if (status != LIO_REQUEST_PENDING) {
1251 /* we have received a response or we have timed out.
1252 * remove node from linked list
1254 STAILQ_REMOVE(&ordered_sc_list->head,
1255 &sc->node, lio_stailq_node, entries);
1257 &lio_dev->response_list.pending_req_count);
1258 rte_spinlock_unlock(&ordered_sc_list->lock);
1261 sc->callback(status, sc->callback_arg);
1265 /* no response yet */
1266 request_complete = 0;
1267 rte_spinlock_unlock(&ordered_sc_list->lock);
1270 /* If we hit the Max Ordered requests to process every loop,
1271 * we quit and let this function be invoked the next time
1272 * the poll thread runs to process the remaining requests.
1273 * This function can take up the entire CPU if there is
1274 * no upper limit to the requests processed.
1276 if (request_complete >= resp_to_process)
1278 } while (request_complete);
1283 static inline struct lio_stailq_node *
1284 list_delete_first_node(struct lio_stailq_head *head)
1286 struct lio_stailq_node *node;
1288 if (STAILQ_EMPTY(head))
1291 node = STAILQ_FIRST(head);
1294 STAILQ_REMOVE(head, node, lio_stailq_node, entries);
1300 lio_delete_sglist(struct lio_instr_queue *txq)
1302 struct lio_device *lio_dev = txq->lio_dev;
1303 int iq_no = txq->q_index;
1304 struct lio_gather *g;
1306 if (lio_dev->glist_head == NULL)
1310 g = (struct lio_gather *)list_delete_first_node(
1311 &lio_dev->glist_head[iq_no]);
1315 (void *)((unsigned long)g->sg - g->adjust));
1322 * \brief Setup gather lists
1323 * @param lio per-network private data
1326 lio_setup_sglists(struct lio_device *lio_dev, int iq_no,
1327 int fw_mapped_iq, int num_descs, unsigned int socket_id)
1329 struct lio_gather *g;
1332 rte_spinlock_init(&lio_dev->glist_lock[iq_no]);
1334 STAILQ_INIT(&lio_dev->glist_head[iq_no]);
1336 for (i = 0; i < num_descs; i++) {
1337 g = rte_zmalloc_socket(NULL, sizeof(*g), RTE_CACHE_LINE_SIZE,
1340 lio_dev_err(lio_dev,
1341 "lio_gather memory allocation failed for qno %d\n",
1347 ((ROUNDUP4(LIO_MAX_SG) >> 2) * LIO_SG_ENTRY_SIZE);
1349 g->sg = rte_zmalloc_socket(NULL, g->sg_size + 8,
1350 RTE_CACHE_LINE_SIZE, socket_id);
1351 if (g->sg == NULL) {
1352 lio_dev_err(lio_dev,
1353 "sg list memory allocation failed for qno %d\n",
1359 /* The gather component should be aligned on 64-bit boundary */
1360 if (((unsigned long)g->sg) & 7) {
1361 g->adjust = 8 - (((unsigned long)g->sg) & 7);
1363 (struct lio_sg_entry *)((unsigned long)g->sg +
1367 STAILQ_INSERT_TAIL(&lio_dev->glist_head[iq_no], &g->list,
1371 if (i != num_descs) {
1372 lio_delete_sglist(lio_dev->instr_queue[fw_mapped_iq]);
1380 lio_delete_instruction_queue(struct lio_device *lio_dev, int iq_no)
1382 lio_delete_instr_queue(lio_dev, iq_no);
1383 rte_free(lio_dev->instr_queue[iq_no]);
1384 lio_dev->instr_queue[iq_no] = NULL;