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"
44 lio_droq_compute_max_packet_bufs(struct lio_droq *droq)
49 count += droq->buffer_size;
50 } while (count < LIO_MAX_RX_PKTLEN);
54 lio_droq_reset_indices(struct lio_droq *droq)
59 droq->refill_count = 0;
60 rte_atomic64_set(&droq->pkts_pending, 0);
64 lio_droq_destroy_ring_buffers(struct lio_droq *droq)
68 for (i = 0; i < droq->max_count; i++) {
69 if (droq->recv_buf_list[i].buffer) {
70 rte_pktmbuf_free((struct rte_mbuf *)
71 droq->recv_buf_list[i].buffer);
72 droq->recv_buf_list[i].buffer = NULL;
76 lio_droq_reset_indices(droq);
80 lio_recv_buffer_alloc(struct lio_device *lio_dev, int q_no)
82 struct lio_droq *droq = lio_dev->droq[q_no];
83 struct rte_mempool *mpool = droq->mpool;
86 m = rte_pktmbuf_alloc(mpool);
88 lio_dev_err(lio_dev, "Cannot allocate\n");
92 rte_mbuf_refcnt_set(m, 1);
94 m->data_off = RTE_PKTMBUF_HEADROOM;
102 lio_droq_setup_ring_buffers(struct lio_device *lio_dev,
103 struct lio_droq *droq)
105 struct lio_droq_desc *desc_ring = droq->desc_ring;
109 for (i = 0; i < droq->max_count; i++) {
110 buf = lio_recv_buffer_alloc(lio_dev, droq->q_no);
112 lio_dev_err(lio_dev, "buffer alloc failed\n");
113 lio_droq_destroy_ring_buffers(droq);
117 droq->recv_buf_list[i].buffer = buf;
118 droq->info_list[i].length = 0;
120 /* map ring buffers into memory */
121 desc_ring[i].info_ptr = lio_map_ring_info(droq, i);
122 desc_ring[i].buffer_ptr =
123 lio_map_ring(droq->recv_buf_list[i].buffer);
126 lio_droq_reset_indices(droq);
128 lio_droq_compute_max_packet_bufs(droq);
134 lio_dma_zone_free(struct lio_device *lio_dev, const struct rte_memzone *mz)
136 const struct rte_memzone *mz_tmp;
140 lio_dev_err(lio_dev, "Memzone NULL\n");
144 mz_tmp = rte_memzone_lookup(mz->name);
145 if (mz_tmp == NULL) {
146 lio_dev_err(lio_dev, "Memzone %s Not Found\n", mz->name);
150 ret = rte_memzone_free(mz);
152 lio_dev_err(lio_dev, "Memzone free Failed ret %d\n", ret);
156 * Frees the space for descriptor ring for the droq.
158 * @param lio_dev - pointer to the lio device structure
159 * @param q_no - droq no.
162 lio_delete_droq(struct lio_device *lio_dev, uint32_t q_no)
164 struct lio_droq *droq = lio_dev->droq[q_no];
166 lio_dev_dbg(lio_dev, "OQ[%d]\n", q_no);
168 lio_droq_destroy_ring_buffers(droq);
169 rte_free(droq->recv_buf_list);
170 droq->recv_buf_list = NULL;
171 lio_dma_zone_free(lio_dev, droq->info_mz);
172 lio_dma_zone_free(lio_dev, droq->desc_ring_mz);
174 memset(droq, 0, LIO_DROQ_SIZE);
178 lio_alloc_info_buffer(struct lio_device *lio_dev,
179 struct lio_droq *droq, unsigned int socket_id)
181 droq->info_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
182 "info_list", droq->q_no,
188 if (droq->info_mz == NULL)
191 droq->info_list_dma = droq->info_mz->phys_addr;
192 droq->info_alloc_size = droq->info_mz->len;
193 droq->info_base_addr = (size_t)droq->info_mz->addr;
195 return droq->info_mz->addr;
199 * Allocates space for the descriptor ring for the droq and
200 * sets the base addr, num desc etc in Octeon registers.
202 * @param lio_dev - pointer to the lio device structure
203 * @param q_no - droq no.
204 * @param app_ctx - pointer to application context
205 * @return Success: 0 Failure: -1
208 lio_init_droq(struct lio_device *lio_dev, uint32_t q_no,
209 uint32_t num_descs, uint32_t desc_size,
210 struct rte_mempool *mpool, unsigned int socket_id)
212 uint32_t c_refill_threshold;
213 uint32_t desc_ring_size;
214 struct lio_droq *droq;
216 lio_dev_dbg(lio_dev, "OQ[%d]\n", q_no);
218 droq = lio_dev->droq[q_no];
219 droq->lio_dev = lio_dev;
223 c_refill_threshold = LIO_OQ_REFILL_THRESHOLD_CFG(lio_dev);
225 droq->max_count = num_descs;
226 droq->buffer_size = desc_size;
228 desc_ring_size = droq->max_count * LIO_DROQ_DESC_SIZE;
229 droq->desc_ring_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
235 if (droq->desc_ring_mz == NULL) {
237 "Output queue %d ring alloc failed\n", q_no);
241 droq->desc_ring_dma = droq->desc_ring_mz->phys_addr;
242 droq->desc_ring = (struct lio_droq_desc *)droq->desc_ring_mz->addr;
244 lio_dev_dbg(lio_dev, "droq[%d]: desc_ring: virt: 0x%p, dma: %lx\n",
245 q_no, droq->desc_ring, (unsigned long)droq->desc_ring_dma);
246 lio_dev_dbg(lio_dev, "droq[%d]: num_desc: %d\n", q_no,
249 droq->info_list = lio_alloc_info_buffer(lio_dev, droq, socket_id);
250 if (droq->info_list == NULL) {
251 lio_dev_err(lio_dev, "Cannot allocate memory for info list.\n");
255 droq->recv_buf_list = rte_zmalloc_socket("recv_buf_list",
257 LIO_DROQ_RECVBUF_SIZE),
260 if (droq->recv_buf_list == NULL) {
262 "Output queue recv buf list alloc failed\n");
266 if (lio_droq_setup_ring_buffers(lio_dev, droq))
269 droq->refill_threshold = c_refill_threshold;
271 rte_spinlock_init(&droq->lock);
273 lio_dev->fn_list.setup_oq_regs(lio_dev, q_no);
275 lio_dev->io_qmask.oq |= (1ULL << q_no);
280 lio_delete_droq(lio_dev, q_no);
286 lio_setup_droq(struct lio_device *lio_dev, int oq_no, int num_descs,
287 int desc_size, struct rte_mempool *mpool, unsigned int socket_id)
289 struct lio_droq *droq;
291 PMD_INIT_FUNC_TRACE();
293 if (lio_dev->droq[oq_no]) {
294 lio_dev_dbg(lio_dev, "Droq %d in use\n", oq_no);
298 /* Allocate the DS for the new droq. */
299 droq = rte_zmalloc_socket("ethdev RX queue", sizeof(*droq),
300 RTE_CACHE_LINE_SIZE, socket_id);
304 lio_dev->droq[oq_no] = droq;
306 /* Initialize the Droq */
307 if (lio_init_droq(lio_dev, oq_no, num_descs, desc_size, mpool,
309 lio_dev_err(lio_dev, "Droq[%u] Initialization Failed\n", oq_no);
310 rte_free(lio_dev->droq[oq_no]);
311 lio_dev->droq[oq_no] = NULL;
317 lio_dev_dbg(lio_dev, "Total number of OQ: %d\n", lio_dev->num_oqs);
319 /* Send credit for octeon output queues. credits are always
320 * sent after the output queue is enabled.
322 rte_write32(lio_dev->droq[oq_no]->max_count,
323 lio_dev->droq[oq_no]->pkts_credit_reg);
329 static inline uint32_t
330 lio_droq_get_bufcount(uint32_t buf_size, uint32_t total_len)
332 uint32_t buf_cnt = 0;
334 while (total_len > (buf_size * buf_cnt))
340 /* If we were not able to refill all buffers, try to move around
341 * the buffers that were not dispatched.
343 static inline uint32_t
344 lio_droq_refill_pullup_descs(struct lio_droq *droq,
345 struct lio_droq_desc *desc_ring)
347 uint32_t refill_index = droq->refill_idx;
348 uint32_t desc_refilled = 0;
350 while (refill_index != droq->read_idx) {
351 if (droq->recv_buf_list[refill_index].buffer) {
352 droq->recv_buf_list[droq->refill_idx].buffer =
353 droq->recv_buf_list[refill_index].buffer;
354 desc_ring[droq->refill_idx].buffer_ptr =
355 desc_ring[refill_index].buffer_ptr;
356 droq->recv_buf_list[refill_index].buffer = NULL;
357 desc_ring[refill_index].buffer_ptr = 0;
359 droq->refill_idx = lio_incr_index(
363 droq->refill_count--;
364 } while (droq->recv_buf_list[droq->refill_idx].buffer);
366 refill_index = lio_incr_index(refill_index, 1,
370 return desc_refilled;
375 * @param lio_dev - pointer to the lio device structure
376 * @param droq - droq in which descriptors require new buffers.
379 * Called during normal DROQ processing in interrupt mode or by the poll
380 * thread to refill the descriptors from which buffers were dispatched
381 * to upper layers. Attempts to allocate new buffers. If that fails, moves
382 * up buffers (that were not dispatched) to form a contiguous ring.
385 * No of descriptors refilled.
388 * This routine is called with droq->lock held.
391 lio_droq_refill(struct lio_device *lio_dev, struct lio_droq *droq)
393 struct lio_droq_desc *desc_ring;
394 uint32_t desc_refilled = 0;
397 desc_ring = droq->desc_ring;
399 while (droq->refill_count && (desc_refilled < droq->max_count)) {
400 /* If a valid buffer exists (happens if there is no dispatch),
401 * reuse the buffer, else allocate.
403 if (droq->recv_buf_list[droq->refill_idx].buffer == NULL) {
404 buf = lio_recv_buffer_alloc(lio_dev, droq->q_no);
405 /* If a buffer could not be allocated, no point in
411 droq->recv_buf_list[droq->refill_idx].buffer = buf;
414 desc_ring[droq->refill_idx].buffer_ptr =
415 lio_map_ring(droq->recv_buf_list[droq->refill_idx].buffer);
416 /* Reset any previous values in the length field. */
417 droq->info_list[droq->refill_idx].length = 0;
419 droq->refill_idx = lio_incr_index(droq->refill_idx, 1,
422 droq->refill_count--;
425 if (droq->refill_count)
426 desc_refilled += lio_droq_refill_pullup_descs(droq, desc_ring);
428 /* if droq->refill_count
429 * The refill count would not change in pass two. We only moved buffers
430 * to close the gap in the ring, but we would still have the same no. of
433 return desc_refilled;
437 lio_droq_fast_process_packet(struct lio_device *lio_dev,
438 struct lio_droq *droq,
439 struct rte_mbuf **rx_pkts)
441 struct rte_mbuf *nicbuf = NULL;
442 struct lio_droq_info *info;
443 uint32_t total_len = 0;
444 int data_total_len = 0;
445 uint32_t pkt_len = 0;
449 info = &droq->info_list[droq->read_idx];
450 lio_swap_8B_data((uint64_t *)info, 2);
455 /* Len of resp hdr in included in the received data len. */
456 info->length -= OCTEON_RH_SIZE;
459 total_len += (uint32_t)info->length;
461 if (lio_opcode_slow_path(rh)) {
464 buf_cnt = lio_droq_get_bufcount(droq->buffer_size,
465 (uint32_t)info->length);
466 droq->read_idx = lio_incr_index(droq->read_idx, buf_cnt,
468 droq->refill_count += buf_cnt;
470 if (info->length <= droq->buffer_size) {
471 if (rh->r_dh.has_hash)
472 pkt_len = (uint32_t)(info->length - 8);
474 pkt_len = (uint32_t)info->length;
476 nicbuf = droq->recv_buf_list[droq->read_idx].buffer;
477 droq->recv_buf_list[droq->read_idx].buffer = NULL;
478 droq->read_idx = lio_incr_index(
481 droq->refill_count++;
483 if (likely(nicbuf != NULL)) {
484 nicbuf->data_off = RTE_PKTMBUF_HEADROOM;
487 /* We don't have a way to pass flags yet */
488 nicbuf->ol_flags = 0;
489 if (rh->r_dh.has_hash) {
492 nicbuf->ol_flags |= PKT_RX_RSS_HASH;
493 hash_ptr = rte_pktmbuf_mtod(nicbuf,
495 lio_swap_8B_data(hash_ptr, 1);
496 nicbuf->hash.rss = (uint32_t)*hash_ptr;
497 nicbuf->data_off += 8;
500 nicbuf->pkt_len = pkt_len;
501 nicbuf->data_len = pkt_len;
502 nicbuf->port = lio_dev->port_id;
504 rx_pkts[data_pkts++] = nicbuf;
505 data_total_len += pkt_len;
508 /* Prefetch buffer pointers when on a cache line
511 if ((droq->read_idx & 3) == 0) {
513 &droq->recv_buf_list[droq->read_idx]);
515 &droq->info_list[droq->read_idx]);
518 struct rte_mbuf *first_buf = NULL;
519 struct rte_mbuf *last_buf = NULL;
521 while (pkt_len < info->length) {
524 cpy_len = ((pkt_len + droq->buffer_size) >
526 ? ((uint32_t)info->length -
531 droq->recv_buf_list[droq->read_idx].buffer;
532 droq->recv_buf_list[droq->read_idx].buffer =
535 if (likely(nicbuf != NULL)) {
536 /* Note the first seg */
540 nicbuf->data_off = RTE_PKTMBUF_HEADROOM;
543 nicbuf->port = lio_dev->port_id;
544 /* We don't have a way to pass
547 nicbuf->ol_flags = 0;
548 if ((!pkt_len) && (rh->r_dh.has_hash)) {
553 hash_ptr = rte_pktmbuf_mtod(
555 lio_swap_8B_data(hash_ptr, 1);
558 nicbuf->data_off += 8;
559 nicbuf->pkt_len = cpy_len - 8;
560 nicbuf->data_len = cpy_len - 8;
562 nicbuf->pkt_len = cpy_len;
563 nicbuf->data_len = cpy_len;
567 first_buf->nb_segs++;
570 last_buf->next = nicbuf;
574 PMD_RX_LOG(lio_dev, ERR, "no buf\n");
578 droq->read_idx = lio_incr_index(
581 droq->refill_count++;
583 /* Prefetch buffer pointers when on a
584 * cache line boundary
586 if ((droq->read_idx & 3) == 0) {
587 rte_prefetch0(&droq->recv_buf_list
591 &droq->info_list[droq->read_idx]);
594 rx_pkts[data_pkts++] = first_buf;
595 if (rh->r_dh.has_hash)
596 data_total_len += (pkt_len - 8);
598 data_total_len += pkt_len;
601 /* Inform upper layer about packet checksum verification */
602 struct rte_mbuf *m = rx_pkts[data_pkts - 1];
604 if (rh->r_dh.csum_verified & LIO_IP_CSUM_VERIFIED)
605 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
607 if (rh->r_dh.csum_verified & LIO_L4_CSUM_VERIFIED)
608 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
611 if (droq->refill_count >= droq->refill_threshold) {
612 int desc_refilled = lio_droq_refill(lio_dev, droq);
614 /* Flush the droq descriptor data to memory to be sure
615 * that when we update the credits the data in memory is
619 rte_write32(desc_refilled, droq->pkts_credit_reg);
620 /* make sure mmio write completes */
631 lio_droq_fast_process_packets(struct lio_device *lio_dev,
632 struct lio_droq *droq,
633 struct rte_mbuf **rx_pkts,
634 uint32_t pkts_to_process)
636 int ret, data_pkts = 0;
639 for (pkt = 0; pkt < pkts_to_process; pkt++) {
640 ret = lio_droq_fast_process_packet(lio_dev, droq,
641 &rx_pkts[data_pkts]);
643 lio_dev_err(lio_dev, "Port[%d] DROQ[%d] idx: %d len:0, pkt_cnt: %d\n",
644 lio_dev->port_id, droq->q_no,
645 droq->read_idx, pkts_to_process);
651 rte_atomic64_sub(&droq->pkts_pending, pkt);
656 static inline uint32_t
657 lio_droq_check_hw_for_pkts(struct lio_droq *droq)
662 pkt_count = rte_read32(droq->pkts_sent_reg);
664 last_count = pkt_count - droq->pkt_count;
665 droq->pkt_count = pkt_count;
668 rte_atomic64_add(&droq->pkts_pending, last_count);
674 lio_dev_recv_pkts(void *rx_queue,
675 struct rte_mbuf **rx_pkts,
678 struct lio_droq *droq = rx_queue;
679 struct lio_device *lio_dev = droq->lio_dev;
680 uint32_t pkts_processed = 0;
681 uint32_t pkt_count = 0;
683 lio_droq_check_hw_for_pkts(droq);
685 pkt_count = rte_atomic64_read(&droq->pkts_pending);
689 if (pkt_count > budget)
693 rte_spinlock_lock(&droq->lock);
694 pkts_processed = lio_droq_fast_process_packets(lio_dev,
698 if (droq->pkt_count) {
699 rte_write32(droq->pkt_count, droq->pkts_sent_reg);
703 /* Release the spin lock */
704 rte_spinlock_unlock(&droq->lock);
706 return pkts_processed;
710 lio_delete_droq_queue(struct lio_device *lio_dev,
713 lio_delete_droq(lio_dev, oq_no);
715 rte_free(lio_dev->droq[oq_no]);
716 lio_dev->droq[oq_no] = NULL;
720 * lio_init_instr_queue()
721 * @param lio_dev - pointer to the lio device structure.
722 * @param txpciq - queue to be initialized.
724 * Called at driver init time for each input queue. iq_conf has the
725 * configuration parameters for the queue.
727 * @return Success: 0 Failure: -1
730 lio_init_instr_queue(struct lio_device *lio_dev,
731 union octeon_txpciq txpciq,
732 uint32_t num_descs, unsigned int socket_id)
734 uint32_t iq_no = (uint32_t)txpciq.s.q_no;
735 struct lio_instr_queue *iq;
739 instr_type = LIO_IQ_INSTR_TYPE(lio_dev);
741 q_size = instr_type * num_descs;
742 iq = lio_dev->instr_queue[iq_no];
743 iq->iq_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
744 "instr_queue", iq_no, q_size,
747 if (iq->iq_mz == NULL) {
748 lio_dev_err(lio_dev, "Cannot allocate memory for instr queue %d\n",
753 iq->base_addr_dma = iq->iq_mz->phys_addr;
754 iq->base_addr = (uint8_t *)iq->iq_mz->addr;
756 iq->max_count = num_descs;
758 /* Initialize a list to holds requests that have been posted to Octeon
759 * but has yet to be fetched by octeon
761 iq->request_list = rte_zmalloc_socket("request_list",
762 sizeof(*iq->request_list) *
766 if (iq->request_list == NULL) {
767 lio_dev_err(lio_dev, "Alloc failed for IQ[%d] nr free list\n",
769 lio_dma_zone_free(lio_dev, iq->iq_mz);
773 lio_dev_dbg(lio_dev, "IQ[%d]: base: %p basedma: %lx count: %d\n",
774 iq_no, iq->base_addr, (unsigned long)iq->base_addr_dma,
777 iq->lio_dev = lio_dev;
778 iq->txpciq.txpciq64 = txpciq.txpciq64;
780 iq->host_write_index = 0;
781 iq->lio_read_index = 0;
784 rte_atomic64_set(&iq->instr_pending, 0);
786 /* Initialize the spinlock for this instruction queue */
787 rte_spinlock_init(&iq->lock);
788 rte_spinlock_init(&iq->post_lock);
790 rte_atomic64_clear(&iq->iq_flush_running);
792 lio_dev->io_qmask.iq |= (1ULL << iq_no);
794 /* Set the 32B/64B mode for each input queue */
795 lio_dev->io_qmask.iq64B |= ((instr_type == 64) << iq_no);
796 iq->iqcmd_64B = (instr_type == 64);
798 lio_dev->fn_list.setup_iq_regs(lio_dev, iq_no);
804 lio_setup_instr_queue0(struct lio_device *lio_dev)
806 union octeon_txpciq txpciq;
807 uint32_t num_descs = 0;
810 num_descs = LIO_NUM_DEF_TX_DESCS_CFG(lio_dev);
812 lio_dev->num_iqs = 0;
814 lio_dev->instr_queue[0] = rte_zmalloc(NULL,
815 sizeof(struct lio_instr_queue), 0);
816 if (lio_dev->instr_queue[0] == NULL)
819 lio_dev->instr_queue[0]->q_index = 0;
820 lio_dev->instr_queue[0]->app_ctx = (void *)(size_t)0;
822 txpciq.s.q_no = iq_no;
823 txpciq.s.pkind = lio_dev->pfvf_hsword.pkind;
824 txpciq.s.use_qpg = 0;
826 if (lio_init_instr_queue(lio_dev, txpciq, num_descs, SOCKET_ID_ANY)) {
827 rte_free(lio_dev->instr_queue[0]);
828 lio_dev->instr_queue[0] = NULL;
838 * lio_delete_instr_queue()
839 * @param lio_dev - pointer to the lio device structure.
840 * @param iq_no - queue to be deleted.
842 * Called at driver unload time for each input queue. Deletes all
843 * allocated resources for the input queue.
846 lio_delete_instr_queue(struct lio_device *lio_dev, uint32_t iq_no)
848 struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
850 rte_free(iq->request_list);
851 iq->request_list = NULL;
852 lio_dma_zone_free(lio_dev, iq->iq_mz);
856 lio_free_instr_queue0(struct lio_device *lio_dev)
858 lio_delete_instr_queue(lio_dev, 0);
859 rte_free(lio_dev->instr_queue[0]);
860 lio_dev->instr_queue[0] = NULL;
864 /* Return 0 on success, -1 on failure */
866 lio_setup_iq(struct lio_device *lio_dev, int q_index,
867 union octeon_txpciq txpciq, uint32_t num_descs, void *app_ctx,
868 unsigned int socket_id)
870 uint32_t iq_no = (uint32_t)txpciq.s.q_no;
872 if (lio_dev->instr_queue[iq_no]) {
873 lio_dev_dbg(lio_dev, "IQ is in use. Cannot create the IQ: %d again\n",
875 lio_dev->instr_queue[iq_no]->txpciq.txpciq64 = txpciq.txpciq64;
876 lio_dev->instr_queue[iq_no]->app_ctx = app_ctx;
880 lio_dev->instr_queue[iq_no] = rte_zmalloc_socket("ethdev TX queue",
881 sizeof(struct lio_instr_queue),
882 RTE_CACHE_LINE_SIZE, socket_id);
883 if (lio_dev->instr_queue[iq_no] == NULL)
886 lio_dev->instr_queue[iq_no]->q_index = q_index;
887 lio_dev->instr_queue[iq_no]->app_ctx = app_ctx;
889 if (lio_init_instr_queue(lio_dev, txpciq, num_descs, socket_id))
897 rte_free(lio_dev->instr_queue[iq_no]);
898 lio_dev->instr_queue[iq_no] = NULL;
904 lio_ring_doorbell(struct lio_device *lio_dev,
905 struct lio_instr_queue *iq)
907 if (rte_atomic64_read(&lio_dev->status) == LIO_DEV_RUNNING) {
908 rte_write32(iq->fill_cnt, iq->doorbell_reg);
909 /* make sure doorbell write goes through */
916 copy_cmd_into_iq(struct lio_instr_queue *iq, uint8_t *cmd)
918 uint8_t *iqptr, cmdsize;
920 cmdsize = ((iq->iqcmd_64B) ? 64 : 32);
921 iqptr = iq->base_addr + (cmdsize * iq->host_write_index);
923 rte_memcpy(iqptr, cmd, cmdsize);
926 static inline struct lio_iq_post_status
927 post_command2(struct lio_instr_queue *iq, uint8_t *cmd)
929 struct lio_iq_post_status st;
931 st.status = LIO_IQ_SEND_OK;
933 /* This ensures that the read index does not wrap around to the same
934 * position if queue gets full before Octeon could fetch any instr.
936 if (rte_atomic64_read(&iq->instr_pending) >=
937 (int32_t)(iq->max_count - 1)) {
938 st.status = LIO_IQ_SEND_FAILED;
943 if (rte_atomic64_read(&iq->instr_pending) >=
944 (int32_t)(iq->max_count - 2))
945 st.status = LIO_IQ_SEND_STOP;
947 copy_cmd_into_iq(iq, cmd);
949 /* "index" is returned, host_write_index is modified. */
950 st.index = iq->host_write_index;
951 iq->host_write_index = lio_incr_index(iq->host_write_index, 1,
955 /* Flush the command into memory. We need to be sure the data is in
956 * memory before indicating that the instruction is pending.
960 rte_atomic64_inc(&iq->instr_pending);
966 lio_add_to_request_list(struct lio_instr_queue *iq,
967 int idx, void *buf, int reqtype)
969 iq->request_list[idx].buf = buf;
970 iq->request_list[idx].reqtype = reqtype;
974 lio_send_command(struct lio_device *lio_dev, uint32_t iq_no, void *cmd,
975 void *buf, uint32_t datasize __rte_unused, uint32_t reqtype)
977 struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
978 struct lio_iq_post_status st;
980 rte_spinlock_lock(&iq->post_lock);
982 st = post_command2(iq, cmd);
984 if (st.status != LIO_IQ_SEND_FAILED) {
985 lio_add_to_request_list(iq, st.index, buf, reqtype);
986 lio_ring_doorbell(lio_dev, iq);
989 rte_spinlock_unlock(&iq->post_lock);
995 lio_prepare_soft_command(struct lio_device *lio_dev,
996 struct lio_soft_command *sc, uint8_t opcode,
997 uint8_t subcode, uint32_t irh_ossp, uint64_t ossp0,
1000 struct octeon_instr_pki_ih3 *pki_ih3;
1001 struct octeon_instr_ih3 *ih3;
1002 struct octeon_instr_irh *irh;
1003 struct octeon_instr_rdp *rdp;
1005 RTE_ASSERT(opcode <= 15);
1006 RTE_ASSERT(subcode <= 127);
1008 ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
1010 ih3->pkind = lio_dev->instr_queue[sc->iq_no]->txpciq.s.pkind;
1012 pki_ih3 = (struct octeon_instr_pki_ih3 *)&sc->cmd.cmd3.pki_ih3;
1017 pki_ih3->uqpg = lio_dev->instr_queue[sc->iq_no]->txpciq.s.use_qpg;
1020 pki_ih3->tag = LIO_CONTROL;
1021 pki_ih3->tagtype = OCTEON_ATOMIC_TAG;
1022 pki_ih3->qpg = lio_dev->instr_queue[sc->iq_no]->txpciq.s.qpg;
1027 ih3->dlengsz = sc->datasize;
1029 irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
1030 irh->opcode = opcode;
1031 irh->subcode = subcode;
1033 /* opcode/subcode specific parameters (ossp) */
1034 irh->ossp = irh_ossp;
1035 sc->cmd.cmd3.ossp[0] = ossp0;
1036 sc->cmd.cmd3.ossp[1] = ossp1;
1038 if (sc->rdatasize) {
1039 rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
1040 rdp->pcie_port = lio_dev->pcie_port;
1041 rdp->rlen = sc->rdatasize;
1044 ih3->fsz = OCTEON_SOFT_CMD_RESP_IH3;
1048 ih3->fsz = OCTEON_PCI_CMD_O3;
1053 lio_send_soft_command(struct lio_device *lio_dev,
1054 struct lio_soft_command *sc)
1056 struct octeon_instr_ih3 *ih3;
1057 struct octeon_instr_irh *irh;
1060 ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
1062 RTE_ASSERT(sc->dmadptr);
1063 sc->cmd.cmd3.dptr = sc->dmadptr;
1066 irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
1068 RTE_ASSERT(sc->dmarptr);
1069 RTE_ASSERT(sc->status_word != NULL);
1070 *sc->status_word = LIO_COMPLETION_WORD_INIT;
1071 sc->cmd.cmd3.rptr = sc->dmarptr;
1074 len = (uint32_t)ih3->dlengsz;
1077 sc->timeout = lio_uptime + sc->wait_time;
1079 return lio_send_command(lio_dev, sc->iq_no, &sc->cmd, sc, len,
1080 LIO_REQTYPE_SOFT_COMMAND);
1084 lio_setup_sc_buffer_pool(struct lio_device *lio_dev)
1086 char sc_pool_name[RTE_MEMPOOL_NAMESIZE];
1089 buf_size = LIO_SOFT_COMMAND_BUFFER_SIZE + RTE_PKTMBUF_HEADROOM;
1090 snprintf(sc_pool_name, sizeof(sc_pool_name),
1091 "lio_sc_pool_%u", lio_dev->port_id);
1092 lio_dev->sc_buf_pool = rte_pktmbuf_pool_create(sc_pool_name,
1093 LIO_MAX_SOFT_COMMAND_BUFFERS,
1094 0, 0, buf_size, SOCKET_ID_ANY);
1099 lio_free_sc_buffer_pool(struct lio_device *lio_dev)
1101 rte_mempool_free(lio_dev->sc_buf_pool);
1104 struct lio_soft_command *
1105 lio_alloc_soft_command(struct lio_device *lio_dev, uint32_t datasize,
1106 uint32_t rdatasize, uint32_t ctxsize)
1108 uint32_t offset = sizeof(struct lio_soft_command);
1109 struct lio_soft_command *sc;
1113 RTE_ASSERT((offset + datasize + rdatasize + ctxsize) <=
1114 LIO_SOFT_COMMAND_BUFFER_SIZE);
1116 m = rte_pktmbuf_alloc(lio_dev->sc_buf_pool);
1118 lio_dev_err(lio_dev, "Cannot allocate mbuf for sc\n");
1122 /* set rte_mbuf data size and there is only 1 segment */
1123 m->pkt_len = LIO_SOFT_COMMAND_BUFFER_SIZE;
1124 m->data_len = LIO_SOFT_COMMAND_BUFFER_SIZE;
1126 /* use rte_mbuf buffer for soft command */
1127 sc = rte_pktmbuf_mtod(m, struct lio_soft_command *);
1128 memset(sc, 0, LIO_SOFT_COMMAND_BUFFER_SIZE);
1129 sc->size = LIO_SOFT_COMMAND_BUFFER_SIZE;
1130 sc->dma_addr = rte_mbuf_data_dma_addr(m);
1133 dma_addr = sc->dma_addr;
1136 sc->ctxptr = (uint8_t *)sc + offset;
1137 sc->ctxsize = ctxsize;
1140 /* Start data at 128 byte boundary */
1141 offset = (offset + ctxsize + 127) & 0xffffff80;
1144 sc->virtdptr = (uint8_t *)sc + offset;
1145 sc->dmadptr = dma_addr + offset;
1146 sc->datasize = datasize;
1149 /* Start rdata at 128 byte boundary */
1150 offset = (offset + datasize + 127) & 0xffffff80;
1153 RTE_ASSERT(rdatasize >= 16);
1154 sc->virtrptr = (uint8_t *)sc + offset;
1155 sc->dmarptr = dma_addr + offset;
1156 sc->rdatasize = rdatasize;
1157 sc->status_word = (uint64_t *)((uint8_t *)(sc->virtrptr) +
1165 lio_free_soft_command(struct lio_soft_command *sc)
1167 rte_pktmbuf_free(sc->mbuf);
1171 lio_setup_response_list(struct lio_device *lio_dev)
1173 STAILQ_INIT(&lio_dev->response_list.head);
1174 rte_spinlock_init(&lio_dev->response_list.lock);
1175 rte_atomic64_set(&lio_dev->response_list.pending_req_count, 0);
1179 lio_process_ordered_list(struct lio_device *lio_dev)
1181 int resp_to_process = LIO_MAX_ORD_REQS_TO_PROCESS;
1182 struct lio_response_list *ordered_sc_list;
1183 struct lio_soft_command *sc;
1184 int request_complete = 0;
1188 ordered_sc_list = &lio_dev->response_list;
1191 rte_spinlock_lock(&ordered_sc_list->lock);
1193 if (STAILQ_EMPTY(&ordered_sc_list->head)) {
1194 /* ordered_sc_list is empty; there is
1195 * nothing to process
1197 rte_spinlock_unlock(&ordered_sc_list->lock);
1201 sc = LIO_STQUEUE_FIRST_ENTRY(&ordered_sc_list->head,
1202 struct lio_soft_command, node);
1204 status = LIO_REQUEST_PENDING;
1206 /* check if octeon has finished DMA'ing a response
1207 * to where rptr is pointing to
1209 status64 = *sc->status_word;
1211 if (status64 != LIO_COMPLETION_WORD_INIT) {
1212 /* This logic ensures that all 64b have been written.
1213 * 1. check byte 0 for non-FF
1214 * 2. if non-FF, then swap result from BE to host order
1215 * 3. check byte 7 (swapped to 0) for non-FF
1216 * 4. if non-FF, use the low 32-bit status code
1217 * 5. if either byte 0 or byte 7 is FF, don't use status
1219 if ((status64 & 0xff) != 0xff) {
1220 lio_swap_8B_data(&status64, 1);
1221 if (((status64 & 0xff) != 0xff)) {
1222 /* retrieve 16-bit firmware status */
1223 status = (uint32_t)(status64 &
1227 LIO_FIRMWARE_STATUS_CODE(
1231 status = LIO_REQUEST_DONE;
1235 } else if ((sc->timeout && lio_check_timeout(lio_uptime,
1237 lio_dev_err(lio_dev,
1238 "cmd failed, timeout (%ld, %ld)\n",
1239 (long)lio_uptime, (long)sc->timeout);
1240 status = LIO_REQUEST_TIMEOUT;
1243 if (status != LIO_REQUEST_PENDING) {
1244 /* we have received a response or we have timed out.
1245 * remove node from linked list
1247 STAILQ_REMOVE(&ordered_sc_list->head,
1248 &sc->node, lio_stailq_node, entries);
1250 &lio_dev->response_list.pending_req_count);
1251 rte_spinlock_unlock(&ordered_sc_list->lock);
1254 sc->callback(status, sc->callback_arg);
1258 /* no response yet */
1259 request_complete = 0;
1260 rte_spinlock_unlock(&ordered_sc_list->lock);
1263 /* If we hit the Max Ordered requests to process every loop,
1264 * we quit and let this function be invoked the next time
1265 * the poll thread runs to process the remaining requests.
1266 * This function can take up the entire CPU if there is
1267 * no upper limit to the requests processed.
1269 if (request_complete >= resp_to_process)
1271 } while (request_complete);