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 enetc_clean_tx_ring(tx_ring);
80 tx_ring->q_swbd[i].buffer_addr = tx_pkts[start];
81 txbd = ENETC_TXBD(*tx_ring, i);
82 tx_swbd = &tx_ring->q_swbd[i];
83 txbd->frm_len = tx_pkts[start]->pkt_len;
84 txbd->buf_len = txbd->frm_len;
85 txbd->flags = rte_cpu_to_le_16(ENETC_TXBD_FLAGS_F);
86 txbd->addr = (uint64_t)(uintptr_t)
87 rte_cpu_to_le_64((size_t)tx_swbd->buffer_addr->buf_iova +
88 tx_swbd->buffer_addr->data_off);
91 if (unlikely(i == tx_ring->bd_count))
95 tx_ring->next_to_use = i;
96 enetc_wr_reg(tx_ring->tcir, i);
101 enetc_refill_rx_ring(struct enetc_bdr *rx_ring, const int buff_cnt)
103 struct enetc_swbd *rx_swbd;
104 union enetc_rx_bd *rxbd;
107 i = rx_ring->next_to_use;
108 rx_swbd = &rx_ring->q_swbd[i];
109 rxbd = ENETC_RXBD(*rx_ring, i);
110 for (j = 0; j < buff_cnt; j++) {
111 rx_swbd->buffer_addr = (void *)(uintptr_t)
112 rte_cpu_to_le_64((uint64_t)(uintptr_t)
113 rte_pktmbuf_alloc(rx_ring->mb_pool));
114 rxbd->w.addr = (uint64_t)(uintptr_t)
115 rx_swbd->buffer_addr->buf_iova +
116 rx_swbd->buffer_addr->data_off;
117 /* clear 'R" as well */
122 if (unlikely(i == rx_ring->bd_count)) {
124 rxbd = ENETC_RXBD(*rx_ring, 0);
125 rx_swbd = &rx_ring->q_swbd[i];
130 rx_ring->next_to_alloc = i;
131 rx_ring->next_to_use = i;
132 enetc_wr_reg(rx_ring->rcir, i);
138 static inline void enetc_slow_parsing(struct rte_mbuf *m,
139 uint64_t parse_results)
141 m->ol_flags &= ~(PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD);
143 switch (parse_results) {
144 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV4:
145 m->packet_type = RTE_PTYPE_L2_ETHER |
147 m->ol_flags |= PKT_RX_IP_CKSUM_BAD;
149 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV6:
150 m->packet_type = RTE_PTYPE_L2_ETHER |
152 m->ol_flags |= PKT_RX_IP_CKSUM_BAD;
154 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV4_TCP:
155 m->packet_type = RTE_PTYPE_L2_ETHER |
158 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD |
161 case ENETC_PARSE_ERROR | ENETC_PKT_TYPE_IPV6_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_IPV4_UDP:
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_IPV6_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_IPV4_SCTP:
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_IPV6_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_IPV4_ICMP:
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_IPV6_ICMP:
204 m->packet_type = RTE_PTYPE_L2_ETHER |
207 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD |
210 /* More switch cases can be added */
212 m->packet_type = RTE_PTYPE_UNKNOWN;
213 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN |
214 PKT_RX_L4_CKSUM_UNKNOWN;
219 static inline void __attribute__((hot))
220 enetc_dev_rx_parse(struct rte_mbuf *m, uint16_t parse_results)
222 ENETC_PMD_DP_DEBUG("parse summary = 0x%x ", parse_results);
223 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD;
225 switch (parse_results) {
226 case ENETC_PKT_TYPE_ETHER:
227 m->packet_type = RTE_PTYPE_L2_ETHER;
229 case ENETC_PKT_TYPE_IPV4:
230 m->packet_type = RTE_PTYPE_L2_ETHER |
233 case ENETC_PKT_TYPE_IPV6:
234 m->packet_type = RTE_PTYPE_L2_ETHER |
237 case ENETC_PKT_TYPE_IPV4_TCP:
238 m->packet_type = RTE_PTYPE_L2_ETHER |
242 case ENETC_PKT_TYPE_IPV6_TCP:
243 m->packet_type = RTE_PTYPE_L2_ETHER |
247 case ENETC_PKT_TYPE_IPV4_UDP:
248 m->packet_type = RTE_PTYPE_L2_ETHER |
252 case ENETC_PKT_TYPE_IPV6_UDP:
253 m->packet_type = RTE_PTYPE_L2_ETHER |
257 case ENETC_PKT_TYPE_IPV4_SCTP:
258 m->packet_type = RTE_PTYPE_L2_ETHER |
262 case ENETC_PKT_TYPE_IPV6_SCTP:
263 m->packet_type = RTE_PTYPE_L2_ETHER |
267 case ENETC_PKT_TYPE_IPV4_ICMP:
268 m->packet_type = RTE_PTYPE_L2_ETHER |
272 case ENETC_PKT_TYPE_IPV6_ICMP:
273 m->packet_type = RTE_PTYPE_L2_ETHER |
277 /* More switch cases can be added */
279 enetc_slow_parsing(m, parse_results);
285 enetc_clean_rx_ring(struct enetc_bdr *rx_ring,
286 struct rte_mbuf **rx_pkts,
291 struct enetc_swbd *rx_swbd;
293 cleaned_cnt = enetc_bd_unused(rx_ring);
294 /* next descriptor to process */
295 i = rx_ring->next_to_clean;
296 rx_swbd = &rx_ring->q_swbd[i];
297 while (likely(rx_frm_cnt < work_limit)) {
298 union enetc_rx_bd *rxbd;
301 if (cleaned_cnt >= ENETC_RXBD_BUNDLE) {
302 int count = enetc_refill_rx_ring(rx_ring, cleaned_cnt);
304 cleaned_cnt -= count;
307 rxbd = ENETC_RXBD(*rx_ring, i);
308 bd_status = rte_le_to_cpu_32(rxbd->r.lstatus);
312 rx_swbd->buffer_addr->pkt_len = rxbd->r.buf_len -
314 rx_swbd->buffer_addr->data_len = rxbd->r.buf_len -
316 rx_swbd->buffer_addr->hash.rss = rxbd->r.rss_hash;
317 rx_swbd->buffer_addr->ol_flags = 0;
318 enetc_dev_rx_parse(rx_swbd->buffer_addr,
319 rxbd->r.parse_summary);
320 rx_pkts[rx_frm_cnt] = rx_swbd->buffer_addr;
324 if (unlikely(i == rx_ring->bd_count)) {
326 rx_swbd = &rx_ring->q_swbd[i];
329 rx_ring->next_to_clean = i;
337 enetc_recv_pkts(void *rxq, struct rte_mbuf **rx_pkts,
340 struct enetc_bdr *rx_ring = (struct enetc_bdr *)rxq;
342 return enetc_clean_rx_ring(rx_ring, rx_pkts, nb_pkts);