1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018-2020 NXP
9 #include "rte_ethdev.h"
10 #include "rte_malloc.h"
11 #include "rte_memzone.h"
13 #include "base/enetc_hw.h"
15 #include "enetc_logs.h"
17 #define ENETC_RXBD_BUNDLE 8 /* Number of BDs to update at once */
20 enetc_clean_tx_ring(struct enetc_bdr *tx_ring)
23 struct enetc_swbd *tx_swbd;
26 /* we don't need barriers here, we just want a relatively current value
29 hwci = (int)(rte_read32_relaxed(tx_ring->tcisr) &
30 ENETC_TBCISR_IDX_MASK);
32 i = tx_ring->next_to_clean;
33 tx_swbd = &tx_ring->q_swbd[i];
35 /* we're only reading the CI index once here, which means HW may update
36 * it while we're doing clean-up. We could read the register in a loop
37 * but for now I assume it's OK to leave a few Tx frames for next call.
38 * The issue with reading the register in a loop is that we're stalling
39 * here trying to catch up with HW which keeps sending traffic as long
40 * as it has traffic to send, so in effect we could be waiting here for
41 * the Tx ring to be drained by HW, instead of us doing Rx in that
45 rte_pktmbuf_free(tx_swbd->buffer_addr);
46 tx_swbd->buffer_addr = NULL;
49 if (unlikely(i == tx_ring->bd_count)) {
51 tx_swbd = &tx_ring->q_swbd[0];
57 tx_ring->next_to_clean = i;
62 enetc_xmit_pkts(void *tx_queue,
63 struct rte_mbuf **tx_pkts,
66 struct enetc_swbd *tx_swbd;
67 int i, start, bds_to_use;
68 struct enetc_tx_bd *txbd;
69 struct enetc_bdr *tx_ring = (struct enetc_bdr *)tx_queue;
71 i = tx_ring->next_to_use;
73 bds_to_use = enetc_bd_unused(tx_ring);
74 if (bds_to_use < nb_pkts)
79 tx_ring->q_swbd[i].buffer_addr = tx_pkts[start];
80 txbd = ENETC_TXBD(*tx_ring, i);
81 tx_swbd = &tx_ring->q_swbd[i];
82 txbd->frm_len = tx_pkts[start]->pkt_len;
83 txbd->buf_len = txbd->frm_len;
84 txbd->flags = rte_cpu_to_le_16(ENETC_TXBD_FLAGS_F);
85 txbd->addr = (uint64_t)(uintptr_t)
86 rte_cpu_to_le_64((size_t)tx_swbd->buffer_addr->buf_iova +
87 tx_swbd->buffer_addr->data_off);
90 if (unlikely(i == tx_ring->bd_count))
94 /* we're only cleaning up the Tx ring here, on the assumption that
95 * software is slower than hardware and hardware completed sending
96 * older frames out by now.
97 * We're also cleaning up the ring before kicking off Tx for the new
98 * batch to minimize chances of contention on the Tx ring
100 enetc_clean_tx_ring(tx_ring);
102 tx_ring->next_to_use = i;
103 enetc_wr_reg(tx_ring->tcir, i);
108 enetc_refill_rx_ring(struct enetc_bdr *rx_ring, const int buff_cnt)
110 struct enetc_swbd *rx_swbd;
111 union enetc_rx_bd *rxbd;
114 i = rx_ring->next_to_use;
115 rx_swbd = &rx_ring->q_swbd[i];
116 rxbd = ENETC_RXBD(*rx_ring, i);
117 for (j = 0; j < buff_cnt; j++) {
118 rx_swbd->buffer_addr = (void *)(uintptr_t)
119 rte_cpu_to_le_64((uint64_t)(uintptr_t)
120 rte_pktmbuf_alloc(rx_ring->mb_pool));
121 rxbd->w.addr = (uint64_t)(uintptr_t)
122 rx_swbd->buffer_addr->buf_iova +
123 rx_swbd->buffer_addr->data_off;
124 /* clear 'R" as well */
129 if (unlikely(i == rx_ring->bd_count)) {
131 rxbd = ENETC_RXBD(*rx_ring, 0);
132 rx_swbd = &rx_ring->q_swbd[i];
137 rx_ring->next_to_alloc = i;
138 rx_ring->next_to_use = i;
139 enetc_wr_reg(rx_ring->rcir, i);
145 static inline void enetc_slow_parsing(struct rte_mbuf *m,
146 uint64_t parse_results)
148 m->ol_flags &= ~(PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD);
150 switch (parse_results) {
151 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV4:
152 m->packet_type = RTE_PTYPE_L2_ETHER |
154 m->ol_flags |= PKT_RX_IP_CKSUM_BAD;
156 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV6:
157 m->packet_type = RTE_PTYPE_L2_ETHER |
159 m->ol_flags |= PKT_RX_IP_CKSUM_BAD;
161 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV4_TCP:
162 m->packet_type = RTE_PTYPE_L2_ETHER |
165 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD |
168 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV6_TCP:
169 m->packet_type = RTE_PTYPE_L2_ETHER |
172 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD |
175 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV4_UDP:
176 m->packet_type = RTE_PTYPE_L2_ETHER |
179 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD |
182 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV6_UDP:
183 m->packet_type = RTE_PTYPE_L2_ETHER |
186 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD |
189 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV4_SCTP:
190 m->packet_type = RTE_PTYPE_L2_ETHER |
193 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD |
196 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV6_SCTP:
197 m->packet_type = RTE_PTYPE_L2_ETHER |
200 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD |
203 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV4_ICMP:
204 m->packet_type = RTE_PTYPE_L2_ETHER |
207 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD |
210 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV6_ICMP:
211 m->packet_type = RTE_PTYPE_L2_ETHER |
214 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD |
217 /* More switch cases can be added */
219 m->packet_type = RTE_PTYPE_UNKNOWN;
220 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN |
221 PKT_RX_L4_CKSUM_UNKNOWN;
226 static inline void __attribute__((hot))
227 enetc_dev_rx_parse(struct rte_mbuf *m, uint16_t parse_results)
229 ENETC_PMD_DP_DEBUG("parse summary = 0x%x ", parse_results);
230 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD;
232 switch (parse_results) {
233 case ENETC_PKT_TYPE_ETHER:
234 m->packet_type = RTE_PTYPE_L2_ETHER;
236 case ENETC_PKT_TYPE_IPV4:
237 m->packet_type = RTE_PTYPE_L2_ETHER |
240 case ENETC_PKT_TYPE_IPV6:
241 m->packet_type = RTE_PTYPE_L2_ETHER |
244 case ENETC_PKT_TYPE_IPV4_TCP:
245 m->packet_type = RTE_PTYPE_L2_ETHER |
249 case ENETC_PKT_TYPE_IPV6_TCP:
250 m->packet_type = RTE_PTYPE_L2_ETHER |
254 case ENETC_PKT_TYPE_IPV4_UDP:
255 m->packet_type = RTE_PTYPE_L2_ETHER |
259 case ENETC_PKT_TYPE_IPV6_UDP:
260 m->packet_type = RTE_PTYPE_L2_ETHER |
264 case ENETC_PKT_TYPE_IPV4_SCTP:
265 m->packet_type = RTE_PTYPE_L2_ETHER |
269 case ENETC_PKT_TYPE_IPV6_SCTP:
270 m->packet_type = RTE_PTYPE_L2_ETHER |
274 case ENETC_PKT_TYPE_IPV4_ICMP:
275 m->packet_type = RTE_PTYPE_L2_ETHER |
279 case ENETC_PKT_TYPE_IPV6_ICMP:
280 m->packet_type = RTE_PTYPE_L2_ETHER |
284 /* More switch cases can be added */
286 enetc_slow_parsing(m, parse_results);
292 enetc_clean_rx_ring(struct enetc_bdr *rx_ring,
293 struct rte_mbuf **rx_pkts,
298 struct enetc_swbd *rx_swbd;
300 cleaned_cnt = enetc_bd_unused(rx_ring);
301 /* next descriptor to process */
302 i = rx_ring->next_to_clean;
303 rx_swbd = &rx_ring->q_swbd[i];
304 while (likely(rx_frm_cnt < work_limit)) {
305 union enetc_rx_bd *rxbd;
308 if (cleaned_cnt >= ENETC_RXBD_BUNDLE) {
309 int count = enetc_refill_rx_ring(rx_ring, cleaned_cnt);
311 cleaned_cnt -= count;
314 rxbd = ENETC_RXBD(*rx_ring, i);
315 bd_status = rte_le_to_cpu_32(rxbd->r.lstatus);
319 rx_swbd->buffer_addr->pkt_len = rxbd->r.buf_len -
321 rx_swbd->buffer_addr->data_len = rxbd->r.buf_len -
323 rx_swbd->buffer_addr->hash.rss = rxbd->r.rss_hash;
324 rx_swbd->buffer_addr->ol_flags = 0;
325 enetc_dev_rx_parse(rx_swbd->buffer_addr,
326 rxbd->r.parse_summary);
327 rx_pkts[rx_frm_cnt] = rx_swbd->buffer_addr;
331 if (unlikely(i == rx_ring->bd_count)) {
333 rx_swbd = &rx_ring->q_swbd[i];
336 rx_ring->next_to_clean = i;
344 enetc_recv_pkts(void *rxq, struct rte_mbuf **rx_pkts,
347 struct enetc_bdr *rx_ring = (struct enetc_bdr *)rxq;
349 return enetc_clean_rx_ring(rx_ring, rx_pkts, nb_pkts);