1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2015-2021 Atomic Rules LLC
7 #include "ark_ethdev_tx.h"
8 #include "ark_global.h"
13 #define ARK_TX_META_SIZE 32
14 #define ARK_TX_META_OFFSET (RTE_PKTMBUF_HEADROOM - ARK_TX_META_SIZE)
15 #define ARK_TX_MAX_NOCHAIN (RTE_MBUF_DEFAULT_DATAROOM)
17 #ifndef RTE_LIBRTE_ARK_MIN_TX_PKTLEN
18 #define ARK_MIN_TX_PKTLEN 0
20 #define ARK_MIN_TX_PKTLEN RTE_LIBRTE_ARK_MIN_TX_PKTLEN
23 /* ************************************************************************* */
25 union ark_tx_meta *meta_q;
26 struct rte_mbuf **bufs;
28 /* handles for hw objects */
29 struct ark_mpu_t *mpu;
30 struct ark_ddm_t *ddm;
32 /* Stats HW tracks bytes and packets, need to count send errors */
35 tx_user_meta_hook_fn tx_user_meta_hook;
41 /* 3 indexes to the paired data rings. */
42 int32_t prod_index; /* where to put the next one */
43 int32_t free_index; /* mbuf has been freed */
45 /* The queue Id is used to identify the HW Q */
47 /* The queue Index within the dpdk device structures */
50 /* next cache line - fields written by device */
51 RTE_MARKER cacheline1 __rte_cache_min_aligned;
52 volatile int32_t cons_index; /* hw is done, can be freed */
53 } __rte_cache_aligned;
55 /* Forward declarations */
56 static int eth_ark_tx_jumbo(struct ark_tx_queue *queue,
57 struct rte_mbuf *mbuf,
58 uint32_t *user_meta, uint8_t meta_cnt);
59 static int eth_ark_tx_hw_queue_config(struct ark_tx_queue *queue);
60 static void free_completed_tx(struct ark_tx_queue *queue);
63 ark_tx_hw_queue_stop(struct ark_tx_queue *queue)
65 ark_mpu_stop(queue->mpu);
68 /* ************************************************************************* */
70 eth_ark_tx_desc_fill(struct ark_tx_queue *queue,
71 struct rte_mbuf *mbuf,
74 uint8_t meta_cnt /* 0 to 5 */
78 union ark_tx_meta *meta;
82 tx_idx = queue->prod_index & queue->queue_mask;
83 meta = &queue->meta_q[tx_idx];
84 meta->data_len = rte_pktmbuf_data_len(mbuf);
86 meta->meta_cnt = meta_cnt / 2;
87 meta->user1 = meta_cnt ? (*user_meta++) : 0;
90 queue->bufs[tx_idx] = mbuf;
92 /* 1 or 2 user meta data entries, user words 1,2 and 3,4 */
93 for (m = 1; m < meta_cnt; m += 2) {
94 tx_idx = queue->prod_index & queue->queue_mask;
95 meta = &queue->meta_q[tx_idx];
96 meta->usermeta0 = *user_meta++;
97 meta->usermeta1 = *user_meta++;
101 tx_idx = queue->prod_index & queue->queue_mask;
102 meta = &queue->meta_q[tx_idx];
103 meta->physaddr = rte_mbuf_data_iova(mbuf);
108 /* ************************************************************************* */
110 eth_ark_xmit_pkts(void *vtxq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
112 struct ark_tx_queue *queue;
113 struct rte_mbuf *mbuf;
114 uint32_t user_meta[5];
117 int32_t prod_index_limit;
119 uint8_t user_len = 0;
120 const uint32_t min_pkt_len = ARK_MIN_TX_PKTLEN;
121 tx_user_meta_hook_fn tx_user_meta_hook;
123 queue = (struct ark_tx_queue *)vtxq;
124 tx_user_meta_hook = queue->tx_user_meta_hook;
126 /* free any packets after the HW is done with them */
127 free_completed_tx(queue);
129 /* leave 4 elements mpu data */
130 prod_index_limit = queue->queue_size + queue->free_index - 4;
133 (nb < nb_pkts) && (prod_index_limit - queue->prod_index) > 0;
138 unlikely(rte_pktmbuf_pkt_len(mbuf) < min_pkt_len)) {
139 /* this packet even if it is small can be split,
140 * be sure to add to the end mbuf
142 uint16_t to_add = min_pkt_len -
143 rte_pktmbuf_pkt_len(mbuf);
145 rte_pktmbuf_append(mbuf, to_add);
148 /* This packet is in error,
149 * we cannot send it so just
150 * count it and delete it.
152 queue->tx_errors += 1;
153 rte_pktmbuf_free(mbuf);
156 memset(appended, 0, to_add);
159 if (tx_user_meta_hook)
160 tx_user_meta_hook(mbuf, user_meta, &user_len,
161 queue->ext_user_data);
162 if (unlikely(mbuf->nb_segs != 1)) {
163 stat = eth_ark_tx_jumbo(queue, mbuf,
164 user_meta, user_len);
165 if (unlikely(stat != 0))
166 break; /* Queue is full */
168 eth_ark_tx_desc_fill(queue, mbuf,
169 ARK_DDM_SOP | ARK_DDM_EOP,
170 user_meta, user_len);
174 if (ARK_DEBUG_CORE && nb != nb_pkts) {
175 ARK_PMD_LOG(DEBUG, "TX: Failure to send:"
180 " free: %" PRIU32 "\n",
185 ark_mpu_dump(queue->mpu,
190 /* let FPGA know producer index. */
192 ark_mpu_set_producer(queue->mpu, queue->prod_index);
197 /* ************************************************************************* */
199 eth_ark_tx_jumbo(struct ark_tx_queue *queue, struct rte_mbuf *mbuf,
200 uint32_t *user_meta, uint8_t meta_cnt)
202 struct rte_mbuf *next;
203 int32_t free_queue_space;
204 uint8_t flags = ARK_DDM_SOP;
206 free_queue_space = queue->queue_mask -
207 (queue->prod_index - queue->free_index);
208 /* We need up to 4 mbufs for first header and 2 for subsequent ones */
209 if (unlikely(free_queue_space < (2 + (2 * mbuf->nb_segs))))
212 while (mbuf != NULL) {
214 flags |= (next == NULL) ? ARK_DDM_EOP : 0;
216 eth_ark_tx_desc_fill(queue, mbuf, flags, user_meta, meta_cnt);
218 flags &= ~ARK_DDM_SOP; /* drop SOP flags */
219 meta_cnt = 0; /* Meta only on SOP */
226 /* ************************************************************************* */
228 eth_ark_tx_queue_setup(struct rte_eth_dev *dev,
231 unsigned int socket_id,
232 const struct rte_eth_txconf *tx_conf __rte_unused)
234 struct ark_adapter *ark = dev->data->dev_private;
235 struct ark_tx_queue *queue;
238 int qidx = queue_idx;
240 if (!rte_is_power_of_2(nb_desc)) {
242 "DPDK Arkville configuration queue size"
243 " must be power of two %u (%s)\n",
248 /* Each packet requires at least 2 mpu elements - double desc count */
249 nb_desc = 2 * nb_desc;
251 /* Allocate queue struct */
252 queue = rte_zmalloc_socket("Ark_txqueue",
253 sizeof(struct ark_tx_queue),
257 ARK_PMD_LOG(ERR, "Failed to allocate tx "
258 "queue memory in %s\n",
263 /* we use zmalloc no need to initialize fields */
264 queue->queue_size = nb_desc;
265 queue->queue_mask = nb_desc - 1;
266 queue->phys_qid = qidx;
267 queue->queue_index = queue_idx;
268 dev->data->tx_queues[queue_idx] = queue;
269 queue->tx_user_meta_hook = ark->user_ext.tx_user_meta_hook;
270 queue->ext_user_data = ark->user_data[dev->data->port_id];
273 rte_zmalloc_socket("Ark_txqueue meta",
274 nb_desc * sizeof(union ark_tx_meta),
278 rte_zmalloc_socket("Ark_txqueue bufs",
279 nb_desc * sizeof(struct rte_mbuf *),
283 if (queue->meta_q == 0 || queue->bufs == 0) {
284 ARK_PMD_LOG(ERR, "Failed to allocate "
285 "queue memory in %s\n", __func__);
286 rte_free(queue->meta_q);
287 rte_free(queue->bufs);
292 queue->ddm = RTE_PTR_ADD(ark->ddm.v, qidx * ARK_DDM_QOFFSET);
293 queue->mpu = RTE_PTR_ADD(ark->mputx.v, qidx * ARK_MPU_QOFFSET);
295 status = eth_ark_tx_hw_queue_config(queue);
297 if (unlikely(status != 0)) {
298 rte_free(queue->meta_q);
299 rte_free(queue->bufs);
301 return -1; /* ERROR CODE */
307 /* ************************************************************************* */
309 eth_ark_tx_hw_queue_config(struct ark_tx_queue *queue)
311 rte_iova_t queue_base, ring_base, cons_index_addr;
312 uint32_t write_interval_ns;
314 /* Verify HW -- MPU */
315 if (ark_mpu_verify(queue->mpu, sizeof(union ark_tx_meta)))
318 queue_base = rte_malloc_virt2iova(queue);
319 ring_base = rte_malloc_virt2iova(queue->meta_q);
321 queue_base + offsetof(struct ark_tx_queue, cons_index);
323 ark_mpu_stop(queue->mpu);
324 ark_mpu_reset(queue->mpu);
326 /* Stop and Reset and configure MPU */
327 ark_mpu_configure(queue->mpu, ring_base, queue->queue_size, 1);
330 * Adjust the write interval based on queue size --
331 * increase pcie traffic when low mbuf count
332 * Queue sizes less than 128 are not allowed
334 switch (queue->queue_size) {
336 write_interval_ns = 500;
339 write_interval_ns = 500;
342 write_interval_ns = 1000;
345 write_interval_ns = 2000;
349 /* Completion address in UDM */
350 ark_ddm_setup(queue->ddm, cons_index_addr, write_interval_ns);
355 /* ************************************************************************* */
357 eth_ark_tx_queue_release(void *vtx_queue)
359 struct ark_tx_queue *queue;
361 queue = (struct ark_tx_queue *)vtx_queue;
363 ark_tx_hw_queue_stop(queue);
365 queue->cons_index = queue->prod_index;
366 free_completed_tx(queue);
368 rte_free(queue->meta_q);
369 rte_free(queue->bufs);
373 /* ************************************************************************* */
375 eth_ark_tx_queue_stop(struct rte_eth_dev *dev, uint16_t queue_id)
377 struct ark_tx_queue *queue;
380 queue = dev->data->tx_queues[queue_id];
382 /* Wait for DDM to send out all packets. */
383 while (queue->cons_index != queue->prod_index) {
389 ark_mpu_stop(queue->mpu);
390 free_completed_tx(queue);
392 dev->data->tx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
398 eth_ark_tx_queue_start(struct rte_eth_dev *dev, uint16_t queue_id)
400 struct ark_tx_queue *queue;
402 queue = dev->data->tx_queues[queue_id];
403 if (dev->data->tx_queue_state[queue_id] == RTE_ETH_QUEUE_STATE_STARTED)
406 ark_mpu_start(queue->mpu);
407 dev->data->tx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
412 /* ************************************************************************* */
414 free_completed_tx(struct ark_tx_queue *queue)
416 struct rte_mbuf *mbuf;
417 union ark_tx_meta *meta;
420 top_index = queue->cons_index; /* read once */
421 while ((top_index - queue->free_index) > 0) {
422 meta = &queue->meta_q[queue->free_index & queue->queue_mask];
423 if (likely((meta->flags & ARK_DDM_SOP) != 0)) {
424 mbuf = queue->bufs[queue->free_index &
426 /* ref count of the mbuf is checked in this call. */
427 rte_pktmbuf_free(mbuf);
429 queue->free_index += (meta->meta_cnt + 2);
433 /* ************************************************************************* */
435 eth_tx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats)
437 struct ark_tx_queue *queue;
438 struct ark_ddm_t *ddm;
439 uint64_t bytes, pkts;
444 bytes = ark_ddm_queue_byte_count(ddm);
445 pkts = ark_ddm_queue_pkt_count(ddm);
447 stats->q_opackets[queue->queue_index] = pkts;
448 stats->q_obytes[queue->queue_index] = bytes;
449 stats->opackets += pkts;
450 stats->obytes += bytes;
451 stats->oerrors += queue->tx_errors;
455 eth_tx_queue_stats_reset(void *vqueue)
457 struct ark_tx_queue *queue;
458 struct ark_ddm_t *ddm;
463 ark_ddm_queue_reset_stats(ddm);
464 queue->tx_errors = 0;