1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2008-2017 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
7 #include <rte_ethdev_driver.h>
9 #include <rte_prefetch.h>
11 #include "enic_compat.h"
12 #include "rq_enet_desc.h"
14 #include "enic_rxtx_common.h"
15 #include <rte_ether.h>
19 #define RTE_PMD_USE_PREFETCH
21 #ifdef RTE_PMD_USE_PREFETCH
22 /*Prefetch a cache line into all cache levels. */
23 #define rte_enic_prefetch(p) rte_prefetch0(p)
25 #define rte_enic_prefetch(p) do {} while (0)
28 #ifdef RTE_PMD_PACKET_PREFETCH
29 #define rte_packet_prefetch(p) rte_prefetch1(p)
31 #define rte_packet_prefetch(p) do {} while (0)
34 /* dummy receive function to replace actual function in
35 * order to do safe reconfiguration operations.
38 enic_dummy_recv_pkts(__rte_unused void *rx_queue,
39 __rte_unused struct rte_mbuf **rx_pkts,
40 __rte_unused uint16_t nb_pkts)
46 enic_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
49 struct vnic_rq *sop_rq = rx_queue;
50 struct vnic_rq *data_rq;
52 struct enic *enic = vnic_dev_priv(sop_rq->vdev);
54 uint16_t rq_idx, max_rx;
56 struct rte_mbuf *nmb, *rxmb;
59 volatile struct cq_desc *cqd_ptr;
63 struct rte_mbuf *first_seg = sop_rq->pkt_first_seg;
64 struct rte_mbuf *last_seg = sop_rq->pkt_last_seg;
66 cq = &enic->cq[enic_cq_rq(enic, sop_rq->index)];
67 cq_idx = cq->to_clean; /* index of cqd, rqd, mbuf_table */
68 cqd_ptr = (struct cq_desc *)(cq->ring.descs) + cq_idx;
69 color = cq->last_color;
71 data_rq = &enic->rq[sop_rq->data_queue_idx];
73 /* Receive until the end of the ring, at most. */
74 max_rx = RTE_MIN(nb_pkts, cq->ring.desc_count - cq_idx);
77 volatile struct rq_enet_desc *rqd_ptr;
84 /* Check for pkts available */
85 if ((cqd_ptr->type_color & CQ_DESC_COLOR_MASK_NOSHIFT) == color)
88 /* Get the cq descriptor and extract rq info from it */
90 rq_num = cqd.q_number & CQ_DESC_Q_NUM_MASK;
91 rq_idx = cqd.completed_index & CQ_DESC_COMP_NDX_MASK;
93 rq = &enic->rq[rq_num];
94 rqd_ptr = ((struct rq_enet_desc *)rq->ring.descs) + rq_idx;
96 /* allocate a new mbuf */
97 nmb = rte_mbuf_raw_alloc(rq->mp);
99 rte_atomic64_inc(&enic->soft_stats.rx_nombuf);
103 /* A packet error means descriptor and data are untrusted */
104 packet_error = enic_cq_rx_check_err(&cqd);
106 /* Get the mbuf to return and replace with one just allocated */
107 rxmb = rq->mbuf_ring[rq_idx];
108 rq->mbuf_ring[rq_idx] = nmb;
111 /* Prefetch next mbuf & desc while processing current one */
112 cqd_ptr = (struct cq_desc *)(cq->ring.descs) + cq_idx;
113 rte_enic_prefetch(cqd_ptr);
115 ciflags = enic_cq_rx_desc_ciflags(
116 (struct cq_enet_rq_desc *)&cqd);
118 /* Push descriptor for newly allocated mbuf */
119 nmb->data_off = RTE_PKTMBUF_HEADROOM;
121 * Only the address needs to be refilled. length_type of the
122 * descriptor it set during initialization
123 * (enic_alloc_rx_queue_mbufs) and does not change.
125 rqd_ptr->address = rte_cpu_to_le_64(nmb->buf_iova +
126 RTE_PKTMBUF_HEADROOM);
128 /* Fill in the rest of the mbuf */
129 seg_length = enic_cq_rx_desc_n_bytes(&cqd);
133 first_seg->pkt_len = seg_length;
135 first_seg->pkt_len = (uint16_t)(first_seg->pkt_len
137 first_seg->nb_segs++;
138 last_seg->next = rxmb;
141 rxmb->port = enic->port_id;
142 rxmb->data_len = seg_length;
146 if (!(enic_cq_rx_desc_eop(ciflags))) {
152 * When overlay offload is enabled, CQ.fcoe indicates the
153 * packet is tunnelled.
155 tnl = enic->overlay_offload &&
156 (ciflags & CQ_ENET_RQ_DESC_FLAGS_FCOE) != 0;
157 /* cq rx flags are only valid if eop bit is set */
158 first_seg->packet_type =
159 enic_cq_rx_flags_to_pkt_type(&cqd, tnl);
160 enic_cq_rx_to_pkt_flags(&cqd, first_seg);
162 /* Wipe the outer types set by enic_cq_rx_flags_to_pkt_type() */
164 first_seg->packet_type &= ~(RTE_PTYPE_L3_MASK |
167 if (unlikely(packet_error)) {
168 rte_pktmbuf_free(first_seg);
169 rte_atomic64_inc(&enic->soft_stats.rx_packet_errors);
174 /* prefetch mbuf data for caller */
175 rte_packet_prefetch(RTE_PTR_ADD(first_seg->buf_addr,
176 RTE_PKTMBUF_HEADROOM));
178 /* store the mbuf address into the next entry of the array */
179 rx_pkts[nb_rx++] = first_seg;
181 if (unlikely(cq_idx == cq->ring.desc_count)) {
183 cq->last_color ^= CQ_DESC_COLOR_MASK_NOSHIFT;
186 sop_rq->pkt_first_seg = first_seg;
187 sop_rq->pkt_last_seg = last_seg;
189 cq->to_clean = cq_idx;
191 if ((sop_rq->rx_nb_hold + data_rq->rx_nb_hold) >
192 sop_rq->rx_free_thresh) {
193 if (data_rq->in_use) {
194 data_rq->posted_index =
195 enic_ring_add(data_rq->ring.desc_count,
196 data_rq->posted_index,
197 data_rq->rx_nb_hold);
198 data_rq->rx_nb_hold = 0;
200 sop_rq->posted_index = enic_ring_add(sop_rq->ring.desc_count,
201 sop_rq->posted_index,
203 sop_rq->rx_nb_hold = 0;
207 iowrite32_relaxed(data_rq->posted_index,
208 &data_rq->ctrl->posted_index);
209 rte_compiler_barrier();
210 iowrite32_relaxed(sop_rq->posted_index,
211 &sop_rq->ctrl->posted_index);
219 enic_noscatter_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
222 struct rte_mbuf *mb, **rx, **rxmb;
223 uint16_t cq_idx, nb_rx, max_rx;
224 struct cq_enet_rq_desc *cqd;
225 struct rq_enet_desc *rqd;
226 unsigned int port_id;
235 enic = vnic_dev_priv(rq->vdev);
236 cq = &enic->cq[enic_cq_rq(enic, rq->index)];
237 cq_idx = cq->to_clean;
240 * Fill up the reserve of free mbufs. Below, we restock the receive
241 * ring with these mbufs to avoid allocation failures.
243 if (rq->num_free_mbufs == 0) {
244 if (rte_mempool_get_bulk(rq->mp, (void **)rq->free_mbufs,
247 rq->num_free_mbufs = ENIC_RX_BURST_MAX;
250 /* Receive until the end of the ring, at most. */
251 max_rx = RTE_MIN(nb_pkts, rq->num_free_mbufs);
252 max_rx = RTE_MIN(max_rx, cq->ring.desc_count - cq_idx);
254 cqd = (struct cq_enet_rq_desc *)(cq->ring.descs) + cq_idx;
255 color = cq->last_color;
256 rxmb = rq->mbuf_ring + cq_idx;
257 port_id = enic->port_id;
258 overlay = enic->overlay_offload;
263 if ((cqd->type_color & CQ_DESC_COLOR_MASK_NOSHIFT) == color)
265 if (unlikely(cqd->bytes_written_flags &
266 CQ_ENET_RQ_DESC_FLAGS_TRUNCATED)) {
267 rte_pktmbuf_free(*rxmb++);
268 rte_atomic64_inc(&enic->soft_stats.rx_packet_errors);
274 /* prefetch mbuf data for caller */
275 rte_packet_prefetch(RTE_PTR_ADD(mb->buf_addr,
276 RTE_PKTMBUF_HEADROOM));
277 mb->data_len = cqd->bytes_written_flags &
278 CQ_ENET_RQ_DESC_BYTES_WRITTEN_MASK;
279 mb->pkt_len = mb->data_len;
281 tnl = overlay && (cqd->completed_index_flags &
282 CQ_ENET_RQ_DESC_FLAGS_FCOE) != 0;
284 enic_cq_rx_flags_to_pkt_type((struct cq_desc *)cqd,
286 enic_cq_rx_to_pkt_flags((struct cq_desc *)cqd, mb);
287 /* Wipe the outer types set by enic_cq_rx_flags_to_pkt_type() */
289 mb->packet_type &= ~(RTE_PTYPE_L3_MASK |
295 /* Number of descriptors visited */
296 nb_rx = cqd - (struct cq_enet_rq_desc *)(cq->ring.descs) - cq_idx;
299 rqd = ((struct rq_enet_desc *)rq->ring.descs) + cq_idx;
300 rxmb = rq->mbuf_ring + cq_idx;
302 rq->rx_nb_hold += nb_rx;
303 if (unlikely(cq_idx == cq->ring.desc_count)) {
305 cq->last_color ^= CQ_DESC_COLOR_MASK_NOSHIFT;
307 cq->to_clean = cq_idx;
309 memcpy(rxmb, rq->free_mbufs + ENIC_RX_BURST_MAX - rq->num_free_mbufs,
310 sizeof(struct rte_mbuf *) * nb_rx);
311 rq->num_free_mbufs -= nb_rx;
315 mb->data_off = RTE_PKTMBUF_HEADROOM;
316 rqd->address = mb->buf_iova + RTE_PKTMBUF_HEADROOM;
319 if (rq->rx_nb_hold > rq->rx_free_thresh) {
320 rq->posted_index = enic_ring_add(rq->ring.desc_count,
325 iowrite32_relaxed(rq->posted_index,
326 &rq->ctrl->posted_index);
332 static inline void enic_free_wq_bufs(struct vnic_wq *wq, u16 completed_index)
334 struct rte_mbuf *buf;
335 struct rte_mbuf *m, *free[ENIC_MAX_WQ_DESCS];
336 unsigned int nb_to_free, nb_free = 0, i;
337 struct rte_mempool *pool;
338 unsigned int tail_idx;
339 unsigned int desc_count = wq->ring.desc_count;
341 nb_to_free = enic_ring_sub(desc_count, wq->tail_idx, completed_index)
343 tail_idx = wq->tail_idx;
344 pool = wq->bufs[tail_idx]->pool;
345 for (i = 0; i < nb_to_free; i++) {
346 buf = wq->bufs[tail_idx];
347 m = rte_pktmbuf_prefree_seg(buf);
348 if (unlikely(m == NULL)) {
349 tail_idx = enic_ring_incr(desc_count, tail_idx);
353 if (likely(m->pool == pool)) {
354 RTE_ASSERT(nb_free < ENIC_MAX_WQ_DESCS);
357 rte_mempool_put_bulk(pool, (void *)free, nb_free);
362 tail_idx = enic_ring_incr(desc_count, tail_idx);
366 rte_mempool_put_bulk(pool, (void **)free, nb_free);
368 wq->tail_idx = tail_idx;
369 wq->ring.desc_avail += nb_to_free;
372 unsigned int enic_cleanup_wq(__rte_unused struct enic *enic, struct vnic_wq *wq)
376 completed_index = *((uint32_t *)wq->cqmsg_rz->addr) & 0xffff;
378 if (wq->last_completed_index != completed_index) {
379 enic_free_wq_bufs(wq, completed_index);
380 wq->last_completed_index = completed_index;
385 uint16_t enic_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
388 struct vnic_wq *wq = (struct vnic_wq *)tx_queue;
394 for (i = 0; i != nb_pkts; i++) {
396 ol_flags = m->ol_flags;
397 if (!(ol_flags & PKT_TX_TCP_SEG)) {
398 if (unlikely(m->pkt_len > ENIC_TX_MAX_PKT_SIZE)) {
405 header_len = m->l2_len + m->l3_len + m->l4_len;
406 if (m->tso_segsz + header_len > ENIC_TX_MAX_PKT_SIZE) {
412 if (ol_flags & wq->tx_offload_notsup_mask) {
416 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
417 ret = rte_validate_tx_offload(m);
423 ret = rte_net_intel_cksum_prepare(m);
433 uint16_t enic_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
437 unsigned int pkt_len, data_len;
438 unsigned int nb_segs;
439 struct rte_mbuf *tx_pkt;
440 struct vnic_wq *wq = (struct vnic_wq *)tx_queue;
441 struct enic *enic = vnic_dev_priv(wq->vdev);
442 unsigned short vlan_id;
444 uint64_t ol_flags_mask;
445 unsigned int wq_desc_avail;
447 unsigned int desc_count;
448 struct wq_enet_desc *descs, *desc_p, desc_tmp;
450 uint8_t vlan_tag_insert;
453 uint8_t offload_mode;
456 rte_atomic64_t *tx_oversized;
458 enic_cleanup_wq(enic, wq);
459 wq_desc_avail = vnic_wq_desc_avail(wq);
460 head_idx = wq->head_idx;
461 desc_count = wq->ring.desc_count;
462 ol_flags_mask = PKT_TX_VLAN | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK;
463 tx_oversized = &enic->soft_stats.tx_oversized;
465 nb_pkts = RTE_MIN(nb_pkts, ENIC_TX_XMIT_MAX);
467 for (index = 0; index < nb_pkts; index++) {
469 pkt_len = tx_pkt->pkt_len;
470 data_len = tx_pkt->data_len;
471 ol_flags = tx_pkt->ol_flags;
472 nb_segs = tx_pkt->nb_segs;
473 tso = ol_flags & PKT_TX_TCP_SEG;
475 /* drop packet if it's too big to send */
476 if (unlikely(!tso && pkt_len > ENIC_TX_MAX_PKT_SIZE)) {
477 rte_pktmbuf_free(tx_pkt);
478 rte_atomic64_inc(tx_oversized);
482 if (nb_segs > wq_desc_avail) {
489 vlan_id = tx_pkt->vlan_tci;
490 vlan_tag_insert = !!(ol_flags & PKT_TX_VLAN);
491 bus_addr = (dma_addr_t)
492 (tx_pkt->buf_iova + tx_pkt->data_off);
494 descs = (struct wq_enet_desc *)wq->ring.descs;
495 desc_p = descs + head_idx;
497 eop = (data_len == pkt_len);
498 offload_mode = WQ_ENET_OFFLOAD_MODE_CSUM;
502 header_len = tx_pkt->l2_len + tx_pkt->l3_len +
505 /* Drop if non-TCP packet or TSO seg size is too big */
506 if (unlikely(header_len == 0 || ((tx_pkt->tso_segsz +
507 header_len) > ENIC_TX_MAX_PKT_SIZE))) {
508 rte_pktmbuf_free(tx_pkt);
509 rte_atomic64_inc(tx_oversized);
513 offload_mode = WQ_ENET_OFFLOAD_MODE_TSO;
514 mss = tx_pkt->tso_segsz;
515 /* For tunnel, need the size of outer+inner headers */
516 if (ol_flags & PKT_TX_TUNNEL_MASK) {
517 header_len += tx_pkt->outer_l2_len +
518 tx_pkt->outer_l3_len;
522 if ((ol_flags & ol_flags_mask) && (header_len == 0)) {
523 if (ol_flags & PKT_TX_IP_CKSUM)
524 mss |= ENIC_CALC_IP_CKSUM;
526 /* Nic uses just 1 bit for UDP and TCP */
527 switch (ol_flags & PKT_TX_L4_MASK) {
528 case PKT_TX_TCP_CKSUM:
529 case PKT_TX_UDP_CKSUM:
530 mss |= ENIC_CALC_TCP_UDP_CKSUM;
536 if (eop && wq->cq_pend >= ENIC_WQ_CQ_THRESH) {
540 wq_enet_desc_enc(&desc_tmp, bus_addr, data_len, mss, header_len,
541 offload_mode, eop, cq, 0, vlan_tag_insert,
545 wq->bufs[head_idx] = tx_pkt;
546 head_idx = enic_ring_incr(desc_count, head_idx);
550 for (tx_pkt = tx_pkt->next; tx_pkt; tx_pkt =
552 data_len = tx_pkt->data_len;
556 if (tx_pkt->next == NULL) {
558 if (wq->cq_pend >= ENIC_WQ_CQ_THRESH) {
563 desc_p = descs + head_idx;
564 bus_addr = (dma_addr_t)(tx_pkt->buf_iova
566 wq_enet_desc_enc((struct wq_enet_desc *)
567 &desc_tmp, bus_addr, data_len,
568 mss, 0, offload_mode, eop, cq,
569 0, vlan_tag_insert, vlan_id,
573 wq->bufs[head_idx] = tx_pkt;
574 head_idx = enic_ring_incr(desc_count, head_idx);
581 iowrite32_relaxed(head_idx, &wq->ctrl->posted_index);
583 wq->ring.desc_avail = wq_desc_avail;
584 wq->head_idx = head_idx;
589 static void enqueue_simple_pkts(struct rte_mbuf **pkts,
590 struct wq_enet_desc *desc,
600 desc->address = p->buf_iova + p->data_off;
601 desc->length = p->pkt_len;
603 desc->vlan_tag = p->vlan_tci;
604 desc->header_length_flags &=
605 ((1 << WQ_ENET_FLAGS_EOP_SHIFT) |
606 (1 << WQ_ENET_FLAGS_CQ_ENTRY_SHIFT));
607 if (p->ol_flags & PKT_TX_VLAN) {
608 desc->header_length_flags |=
609 1 << WQ_ENET_FLAGS_VLAN_TAG_INSERT_SHIFT;
612 * Checksum offload. We use WQ_ENET_OFFLOAD_MODE_CSUM, which
613 * is 0, so no need to set offload_mode.
616 if (p->ol_flags & PKT_TX_IP_CKSUM)
617 mss |= ENIC_CALC_IP_CKSUM << WQ_ENET_MSS_SHIFT;
618 if (p->ol_flags & PKT_TX_L4_MASK)
619 mss |= ENIC_CALC_TCP_UDP_CKSUM << WQ_ENET_MSS_SHIFT;
620 desc->mss_loopback = mss;
623 * The app should not send oversized
624 * packets. tx_pkt_prepare includes a check as
625 * well. But some apps ignore the device max size and
626 * tx_pkt_prepare. Oversized packets cause WQ errrors
627 * and the NIC ends up disabling the whole WQ. So
630 if (unlikely(p->pkt_len > ENIC_TX_MAX_PKT_SIZE)) {
631 desc->length = ENIC_TX_MAX_PKT_SIZE;
632 rte_atomic64_inc(&enic->soft_stats.tx_oversized);
638 uint16_t enic_simple_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
641 unsigned int head_idx, desc_count;
642 struct wq_enet_desc *desc;
647 wq = (struct vnic_wq *)tx_queue;
648 enic = vnic_dev_priv(wq->vdev);
649 enic_cleanup_wq(enic, wq);
650 /* Will enqueue this many packets in this call */
651 nb_pkts = RTE_MIN(nb_pkts, wq->ring.desc_avail);
655 head_idx = wq->head_idx;
656 desc_count = wq->ring.desc_count;
658 /* Descriptors until the end of the ring */
659 n = desc_count - head_idx;
660 n = RTE_MIN(nb_pkts, n);
662 /* Save mbuf pointers to free later */
663 memcpy(wq->bufs + head_idx, tx_pkts, sizeof(struct rte_mbuf *) * n);
665 /* Enqueue until the ring end */
667 desc = ((struct wq_enet_desc *)wq->ring.descs) + head_idx;
668 enqueue_simple_pkts(tx_pkts, desc, n, enic);
670 /* Wrap to the start of the ring */
673 memcpy(wq->bufs, tx_pkts, sizeof(struct rte_mbuf *) * rem);
674 desc = (struct wq_enet_desc *)wq->ring.descs;
675 enqueue_simple_pkts(tx_pkts, desc, rem, enic);
679 /* Update head_idx and desc_avail */
680 wq->ring.desc_avail -= nb_pkts;
682 if (head_idx >= desc_count)
683 head_idx -= desc_count;
684 wq->head_idx = head_idx;
685 iowrite32_relaxed(head_idx, &wq->ctrl->posted_index);