Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2026 Advanced Micro Devices, Inc. All rights reserved.
3 : : */
4 : :
5 : : #include <errno.h>
6 : : #include <inttypes.h>
7 : : #include <stdio.h>
8 : : #include <string.h>
9 : :
10 : : #include <bus_pci_driver.h>
11 : : #include <rte_dmadev_pmd.h>
12 : : #include <rte_malloc.h>
13 : :
14 : : #include "ae4dma_internal.h"
15 : :
16 : : /*
17 : : * One dmadev per AE4DMA hardware channel; each dmadev has exactly one virtual channel.
18 : : * The HW's per-queue register block must be densely packed right after the engine-common
19 : : * config register at BAR0+0; the build-time check below catches an accidental layout change.
20 : : */
21 : : static_assert(sizeof(struct ae4dma_hwq_regs) == 32,
22 : : "ae4dma_hwq_regs stride changed; per-queue offset math will break");
23 : :
24 [ - + ]: 305 : RTE_LOG_REGISTER_DEFAULT(ae4dma_pmd_logtype, INFO);
25 : :
26 : : #define AE4DMA_PMD_NAME dmadev_ae4dma
27 : :
28 : : static const struct rte_memzone *
29 : : ae4dma_queue_dma_zone_reserve(const char *queue_name, uint32_t queue_size, int socket_id)
30 : : {
31 : 0 : return rte_memzone_reserve_aligned(queue_name, queue_size, socket_id,
32 : : RTE_MEMZONE_IOVA_CONTIG, queue_size);
33 : : }
34 : :
35 : : static int
36 : 0 : ae4dma_dev_configure(struct rte_dma_dev *dev __rte_unused, const struct rte_dma_conf *dev_conf,
37 : : uint32_t conf_sz)
38 : : {
39 [ # # ]: 0 : if (conf_sz < sizeof(struct rte_dma_conf))
40 : : return -EINVAL;
41 : :
42 [ # # ]: 0 : if (dev_conf->nb_vchans != 1)
43 : 0 : return -EINVAL;
44 : :
45 : : return 0;
46 : : }
47 : :
48 : : /* Setup a virtual channel for AE4DMA, only 1 vchan is supported per dmadev. */
49 : : static int
50 : 0 : ae4dma_vchan_setup(struct rte_dma_dev *dev, uint16_t vchan __rte_unused,
51 : : const struct rte_dma_vchan_conf *qconf, uint32_t qconf_sz)
52 : : {
53 : 0 : struct ae4dma_dmadev *ae4dma = dev->fp_obj->dev_private;
54 : : struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
55 : 0 : uint16_t max_desc = qconf->nb_desc;
56 : :
57 [ # # ]: 0 : if (qconf_sz < sizeof(struct rte_dma_vchan_conf))
58 : : return -EINVAL;
59 : :
60 : : /*
61 : : * The framework already clamps nb_desc to the advertised [min_desc,
62 : : * max_desc] range. The HW ring slot is computed by masking a
63 : : * free-running 16-bit index, so the depth must be a power of two:
64 : : * round up here when the application asks for a non-power-of-two size.
65 : : */
66 [ # # ]: 0 : if (!rte_is_power_of_2(max_desc))
67 : 0 : max_desc = rte_align32pow2(max_desc);
68 : :
69 : 0 : cmd_q->qcfg = *qconf;
70 : 0 : cmd_q->qcfg.nb_desc = max_desc;
71 : :
72 : : /*
73 : : * Reset ring indices and stats when (re)configuring the vchan.
74 : : * hw_base is (re)sampled from the HW consumer index in dev_start.
75 : : */
76 : 0 : cmd_q->next_write = 0;
77 : 0 : cmd_q->next_read = 0;
78 : 0 : cmd_q->last_write = 0;
79 : 0 : cmd_q->hw_base = 0;
80 : 0 : memset(&cmd_q->stats, 0, sizeof(cmd_q->stats));
81 : 0 : return 0;
82 : : }
83 : :
84 : : static int
85 : 0 : ae4dma_dev_start(struct rte_dma_dev *dev)
86 : : {
87 : 0 : struct ae4dma_dmadev *ae4dma = dev->fp_obj->dev_private;
88 : : struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
89 : 0 : uint16_t nb = cmd_q->qcfg.nb_desc;
90 : : uint16_t hw_pos;
91 : :
92 [ # # ]: 0 : if (nb == 0)
93 : : return -EBUSY;
94 : :
95 : : /*
96 : : * Program ring depth and (re)enable the HW queue. On HW that resets
97 : : * the queue indices on enable, this must happen before we sample
98 : : * read_idx below.
99 : : */
100 : 0 : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->max_idx, nb);
101 : 0 : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->control_reg.control_raw, AE4DMA_CMD_QUEUE_ENABLE);
102 : :
103 : : /*
104 : : * The dmadev ring index is a free-running 16-bit counter that restarts
105 : : * from 0 on every (re)start. read_idx is HW-owned (read-only), so we
106 : : * cannot force it to 0; instead anchor index 0 to wherever the HW
107 : : * consumer currently sits (hw_base) and derive the ring slot as
108 : : * (hw_base + idx) & (nb - 1). write_idx is set equal to read_idx so the
109 : : * queue starts empty without touching the read-only read_idx register.
110 : : */
111 : 0 : hw_pos = (uint16_t)(AE4DMA_READ_REG(&cmd_q->hwq_regs->read_idx) & (nb - 1));
112 : 0 : cmd_q->hw_base = hw_pos;
113 : 0 : cmd_q->next_write = 0;
114 : 0 : cmd_q->next_read = 0;
115 : 0 : cmd_q->last_write = 0;
116 : 0 : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->write_idx, hw_pos);
117 : 0 : return 0;
118 : : }
119 : :
120 : : static int
121 : 0 : ae4dma_dev_stop(struct rte_dma_dev *dev)
122 : : {
123 : 0 : struct ae4dma_dmadev *ae4dma = dev->fp_obj->dev_private;
124 : : struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
125 : :
126 [ # # ]: 0 : if (cmd_q->hwq_regs != NULL)
127 : : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->control_reg.control_raw,
128 : : AE4DMA_CMD_QUEUE_DISABLE);
129 : 0 : return 0;
130 : : }
131 : :
132 : : static int
133 : 0 : ae4dma_dev_info_get(const struct rte_dma_dev *dev __rte_unused,
134 : : struct rte_dma_info *info, uint32_t size __rte_unused)
135 : : {
136 : 0 : info->dev_capa = RTE_DMA_CAPA_MEM_TO_MEM | RTE_DMA_CAPA_OPS_COPY;
137 : 0 : info->max_vchans = 1;
138 : 0 : info->min_desc = 2;
139 : 0 : info->max_desc = AE4DMA_DESCRIPTORS_PER_CMDQ;
140 : 0 : info->nb_vchans = 1;
141 : 0 : return 0;
142 : : }
143 : :
144 : : static int
145 : 0 : ae4dma_dev_close(struct rte_dma_dev *dev)
146 : : {
147 : 0 : struct ae4dma_dmadev *ae4dma = dev->fp_obj->dev_private;
148 : : struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
149 : :
150 [ # # ]: 0 : if (cmd_q->hwq_regs != NULL)
151 : : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->control_reg.control_raw,
152 : : AE4DMA_CMD_QUEUE_DISABLE);
153 : :
154 : 0 : rte_memzone_free(cmd_q->mz);
155 : 0 : cmd_q->mz = NULL;
156 : 0 : cmd_q->qbase_desc = NULL;
157 : 0 : cmd_q->qbase_addr = NULL;
158 : 0 : cmd_q->qbase_phys_addr = 0;
159 : 0 : return 0;
160 : : }
161 : :
162 : : /* Ring the doorbell: publish all enqueued descriptors to the hardware. */
163 : : static inline void
164 : : __submit(struct ae4dma_dmadev *ae4dma)
165 : : {
166 : : struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
167 : 0 : uint16_t nb = cmd_q->qcfg.nb_desc;
168 : :
169 [ # # ]: 0 : if (nb == 0)
170 : : return;
171 : :
172 : : /* The HW producer index is the ring slot of the next free descriptor. */
173 : 0 : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->write_idx,
174 : : (cmd_q->hw_base + cmd_q->next_write) & (nb - 1));
175 : 0 : cmd_q->stats.submitted += (uint16_t)(cmd_q->next_write - cmd_q->last_write);
176 : 0 : cmd_q->last_write = cmd_q->next_write;
177 : : }
178 : :
179 : : static int
180 [ # # ]: 0 : ae4dma_submit(void *dev_private, uint16_t vchan __rte_unused)
181 : : {
182 : : struct ae4dma_dmadev *ae4dma = dev_private;
183 : :
184 : : __submit(ae4dma);
185 : 0 : return 0;
186 : : }
187 : :
188 : : /* Write a copy descriptor; returns the free-running ring_idx assigned to it. */
189 : : static inline int
190 : 0 : __write_desc_copy(void *dev_private, rte_iova_t src, rte_iova_t dst,
191 : : uint32_t len, uint64_t flags)
192 : : {
193 : : struct ae4dma_dmadev *ae4dma = dev_private;
194 : : struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
195 : 0 : uint16_t ring_idx = cmd_q->next_write;
196 : 0 : uint16_t nb = cmd_q->qcfg.nb_desc;
197 : : struct ae4dma_desc *dma_desc;
198 : : uint16_t in_flight;
199 : :
200 [ # # ]: 0 : if (nb == 0)
201 : : return -EINVAL;
202 : :
203 : : /* Reserve one slot to distinguish full from empty on the ring. */
204 : 0 : in_flight = (uint16_t)(cmd_q->next_write - cmd_q->next_read);
205 [ # # ]: 0 : if (in_flight >= nb - 1)
206 : : return -ENOSPC;
207 : :
208 [ # # ]: 0 : dma_desc = &cmd_q->qbase_desc[(cmd_q->hw_base + ring_idx) & (nb - 1)];
209 : : memset(dma_desc, 0, sizeof(*dma_desc));
210 : 0 : dma_desc->length = len;
211 : 0 : dma_desc->src_hi = upper_32_bits(src);
212 : 0 : dma_desc->src_lo = lower_32_bits(src);
213 : 0 : dma_desc->dst_hi = upper_32_bits(dst);
214 : 0 : dma_desc->dst_lo = lower_32_bits(dst);
215 : :
216 : 0 : cmd_q->next_write++;
217 [ # # ]: 0 : if (flags & RTE_DMA_OP_FLAG_SUBMIT)
218 : : __submit(ae4dma);
219 : :
220 : : /* dmadev ring_idx is a free-running 16-bit value, not a ring slot. */
221 : : return ring_idx;
222 : : }
223 : :
224 : : /* Enqueue a copy operation onto the ae4dma device. */
225 : : static int
226 : 0 : ae4dma_enqueue_copy(void *dev_private, uint16_t vchan __rte_unused, rte_iova_t src, rte_iova_t dst,
227 : : uint32_t length, uint64_t flags)
228 : : {
229 : 0 : return __write_desc_copy(dev_private, src, dst, length, flags);
230 : : }
231 : :
232 : : static int
233 : 0 : ae4dma_dev_dump(const struct rte_dma_dev *dev, FILE *f)
234 : : {
235 : 0 : struct ae4dma_dmadev *ae4dma = dev->fp_obj->dev_private;
236 : : struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
237 : 0 : void *mmio = ae4dma->io_regs;
238 : :
239 : 0 : fprintf(f, "cmd_q->id = %" PRIx64 "\n", cmd_q->id);
240 : 0 : fprintf(f, "cmd_q->qidx = %" PRIx64 "\n", cmd_q->qidx);
241 : 0 : fprintf(f, "cmd_q->qsize = %" PRIx64 "\n", cmd_q->qsize);
242 : : fprintf(f, "mmio_base_addr = %p\n", mmio);
243 : : fprintf(f, "queues per ae4dma engine = %d\n",
244 : : AE4DMA_READ_REG_OFFSET(mmio, AE4DMA_COMMON_CONFIG_OFFSET));
245 : : fprintf(f, "== Private Data ==\n");
246 : 0 : fprintf(f, " Config: { ring_size: %u }\n", cmd_q->qcfg.nb_desc);
247 : 0 : fprintf(f, " Ring virt: %p\tphys: %#" PRIx64 "\n", (void *)cmd_q->qbase_desc,
248 : 0 : (uint64_t)cmd_q->qbase_phys_addr);
249 : 0 : fprintf(f, " Next write: %u\n", cmd_q->next_write);
250 : 0 : fprintf(f, " Next read: %u\n", cmd_q->next_read);
251 : 0 : fprintf(f, " current queue depth: %u\n",
252 : 0 : (uint16_t)(cmd_q->next_write - cmd_q->next_read));
253 : : fprintf(f, " }\n");
254 : 0 : fprintf(f, " Key Stats { submitted: %" PRIu64 ", comp: %" PRIu64 ", failed: %" PRIu64 " }\n",
255 : : cmd_q->stats.submitted, cmd_q->stats.completed, cmd_q->stats.errors);
256 : 0 : return 0;
257 : : }
258 : :
259 : : /* Translate an AE4DMA descriptor error code to the dmadev status code. */
260 : : static inline enum rte_dma_status_code
261 : : __translate_status_ae4dma_to_dma(enum ae4dma_dma_err status)
262 : : {
263 : : switch (status) {
264 : : case AE4DMA_DMA_ERR_NO_ERR:
265 : : return RTE_DMA_STATUS_SUCCESSFUL;
266 : : case AE4DMA_DMA_ERR_INV_LEN:
267 : : return RTE_DMA_STATUS_INVALID_LENGTH;
268 : : case AE4DMA_DMA_ERR_INV_SRC:
269 : : return RTE_DMA_STATUS_INVALID_SRC_ADDR;
270 : : case AE4DMA_DMA_ERR_INV_DST:
271 : : return RTE_DMA_STATUS_INVALID_DST_ADDR;
272 : : default:
273 : : return RTE_DMA_STATUS_ERROR_UNKNOWN;
274 : : }
275 : : }
276 : :
277 : : /* True if the descriptor at ring slot has been flagged as failed by HW. */
278 : : static inline bool
279 : : ae4dma_desc_failed(volatile struct ae4dma_desc *hw_desc)
280 : : {
281 : : /*
282 : : * read_idx advancing is the definitive completion signal.
283 : : * The per-descriptor status byte is informational and may not yet be written
284 : : * when we observe it, so a slot is only a failure if the HW flagged DESC_ERROR
285 : : * or set a non-zero err_code.
286 : : */
287 : 0 : return hw_desc->dw1.status == AE4DMA_DMA_DESC_ERROR ||
288 [ # # # # ]: 0 : hw_desc->dw1.err_code != AE4DMA_DMA_ERR_NO_ERR;
289 : : }
290 : :
291 : : /*
292 : : * Number of descriptors the HW has completed since our last reap,
293 : : * without consuming them (non-blocking, no state change).
294 : : *
295 : : * The AE4DMA engine signals completion by advancing the per-queue read_idx register;
296 : : * it does not (reliably) write a status word back into the descriptor.
297 : : * We therefore use the HW read_idx register as the source of truth for how many ops are done.
298 : : *
299 : : * next_read/next_write are free-running 16-bit indices;
300 : : * the HW ring slot is ((hw_base + idx) & mask)
301 : : * and the number of in-flight ops is (next_write - next_read),
302 : : * both relying on nb_desc being a power of two.
303 : : */
304 : : static inline uint16_t
305 : : ae4dma_pending(struct ae4dma_cmd_queue *cmd_q)
306 : : {
307 : : uint16_t nb = cmd_q->qcfg.nb_desc;
308 : : uint16_t hw_read_pos, tail_pos;
309 : : uint16_t newly_done, in_flight;
310 : : uint16_t mask;
311 : :
312 : 0 : if (nb == 0)
313 : : return 0;
314 : : mask = nb - 1;
315 : :
316 : 0 : in_flight = (uint16_t)(cmd_q->next_write - cmd_q->next_read);
317 [ # # # # ]: 0 : if (in_flight == 0)
318 : : return 0;
319 : :
320 : : /* Descriptors HW has consumed since our last visit: [tail, read). */
321 : 0 : hw_read_pos = (uint16_t)(AE4DMA_READ_REG(&cmd_q->hwq_regs->read_idx) & mask);
322 : 0 : tail_pos = (uint16_t)((cmd_q->hw_base + cmd_q->next_read) & mask);
323 : 0 : newly_done = (uint16_t)((hw_read_pos - tail_pos) & mask);
324 : :
325 : 0 : return RTE_MIN(newly_done, in_flight);
326 : : }
327 : :
328 : : /*
329 : : * Report ops that completed successfully, stopping at the first failure.
330 : : *
331 : : * Per the dmadev contract, this reaps only the run of successful ops at the head of
332 : : * the completed region and stops as soon as it meets a failed one.
333 : : * The failed op is left in place (not consumed) and *has_error is set
334 : : * so the application switches to rte_dma_completed_status() to retrieve its status.
335 : : * last_idx is the ring_idx of the last successfully completed op.
336 : : */
337 : : static uint16_t
338 : 0 : ae4dma_completed(void *dev_private, uint16_t vchan __rte_unused, const uint16_t max_ops,
339 : : uint16_t *last_idx, bool *has_error)
340 : : {
341 : : struct ae4dma_dmadev *ae4dma = dev_private;
342 : : struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
343 : 0 : uint16_t mask = cmd_q->qcfg.nb_desc - 1;
344 : : uint16_t ready, count;
345 : :
346 [ # # ]: 0 : *has_error = false;
347 : :
348 : : ready = ae4dma_pending(cmd_q);
349 : : if (ready > max_ops)
350 : : ready = max_ops;
351 : :
352 [ # # ]: 0 : for (count = 0; count < ready; count++) {
353 : 0 : uint16_t slot = (cmd_q->hw_base + cmd_q->next_read + count) & mask;
354 : :
355 [ # # # # ]: 0 : if (ae4dma_desc_failed(&cmd_q->qbase_desc[slot])) {
356 : 0 : *has_error = true;
357 : 0 : break;
358 : : }
359 : : }
360 : :
361 : 0 : cmd_q->next_read += count;
362 : 0 : cmd_q->stats.completed += count;
363 : 0 : *last_idx = (uint16_t)(cmd_q->next_read - 1);
364 : :
365 : 0 : return count;
366 : : }
367 : :
368 : : /*
369 : : * Report per-op status for all completed ops, including failures.
370 : : *
371 : : * Unlike ae4dma_completed(), this consumes failed ops too so
372 : : * the application can drain past an error.
373 : : * last_idx is the ring_idx of the last op reported.
374 : : */
375 : : static uint16_t
376 : 0 : ae4dma_completed_status(void *dev_private, uint16_t vchan __rte_unused, uint16_t max_ops,
377 : : uint16_t *last_idx, enum rte_dma_status_code *status)
378 : : {
379 : : struct ae4dma_dmadev *ae4dma = dev_private;
380 : : struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
381 [ # # ]: 0 : uint16_t mask = cmd_q->qcfg.nb_desc - 1;
382 : : uint16_t ready, fails = 0;
383 : : uint16_t i;
384 : :
385 : : ready = ae4dma_pending(cmd_q);
386 : : if (ready > max_ops)
387 : : ready = max_ops;
388 : :
389 [ # # ]: 0 : for (i = 0; i < ready; i++) {
390 : 0 : uint16_t slot = (cmd_q->hw_base + cmd_q->next_read + i) & mask;
391 [ # # ]: 0 : volatile struct ae4dma_desc *hw_desc = &cmd_q->qbase_desc[slot];
392 : :
393 [ # # ]: 0 : if (ae4dma_desc_failed(hw_desc)) {
394 : 0 : status[i] = __translate_status_ae4dma_to_dma(
395 [ # # ]: 0 : (enum ae4dma_dma_err)hw_desc->dw1.err_code);
396 : 0 : AE4DMA_PMD_WARN("Desc failed: status=%u err=%u", hw_desc->dw1.status,
397 : : hw_desc->dw1.err_code);
398 : 0 : fails++;
399 : : } else {
400 : 0 : status[i] = RTE_DMA_STATUS_SUCCESSFUL;
401 : : }
402 : : }
403 : :
404 : 0 : cmd_q->next_read += ready;
405 : 0 : cmd_q->stats.completed += ready;
406 : 0 : cmd_q->stats.errors += fails;
407 : 0 : *last_idx = (uint16_t)(cmd_q->next_read - 1);
408 : :
409 : 0 : return ready;
410 : : }
411 : :
412 : : /* Get the remaining capacity of the ring. */
413 : : static uint16_t
414 : 0 : ae4dma_burst_capacity(const void *dev_private, uint16_t vchan __rte_unused)
415 : : {
416 : : const struct ae4dma_dmadev *ae4dma = dev_private;
417 : : const struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
418 : 0 : uint16_t nb = cmd_q->qcfg.nb_desc;
419 : : uint16_t in_flight;
420 : :
421 [ # # ]: 0 : if (nb == 0)
422 : : return 0;
423 : :
424 : : /* One slot reserved to distinguish full from empty. */
425 : 0 : in_flight = (uint16_t)(cmd_q->next_write - cmd_q->next_read);
426 [ # # ]: 0 : if (in_flight >= nb - 1)
427 : : return 0;
428 : 0 : return (uint16_t)(nb - 1 - in_flight);
429 : : }
430 : :
431 : : static int
432 : 0 : ae4dma_stats_get(const struct rte_dma_dev *dev, uint16_t vchan __rte_unused,
433 : : struct rte_dma_stats *rte_stats, uint32_t size __rte_unused)
434 : : {
435 : 0 : const struct ae4dma_dmadev *ae4dma = dev->fp_obj->dev_private;
436 : : const struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
437 : :
438 : 0 : *rte_stats = cmd_q->stats;
439 : 0 : return 0;
440 : : }
441 : :
442 : : static int
443 : 0 : ae4dma_stats_reset(struct rte_dma_dev *dev, uint16_t vchan __rte_unused)
444 : : {
445 : 0 : struct ae4dma_dmadev *ae4dma = dev->fp_obj->dev_private;
446 : : struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
447 : :
448 : 0 : memset(&cmd_q->stats, 0, sizeof(cmd_q->stats));
449 : 0 : return 0;
450 : : }
451 : :
452 : : /*
453 : : * Report channel state to the dmadev framework.
454 : : *
455 : : * RTE_DMA_VCHAN_HALTED_ERROR - HW queue is disabled (never started, or
456 : : * stopped via dev_stop()).
457 : : * RTE_DMA_VCHAN_IDLE - HW has caught up: read_idx == write_idx,
458 : : * no descriptors in flight.
459 : : * RTE_DMA_VCHAN_ACTIVE - HW still has descriptors to process.
460 : : */
461 : : static int
462 : 0 : ae4dma_vchan_status(const struct rte_dma_dev *dev, uint16_t vchan __rte_unused,
463 : : enum rte_dma_vchan_status *status)
464 : : {
465 : 0 : const struct ae4dma_dmadev *ae4dma = dev->fp_obj->dev_private;
466 : : const struct ae4dma_cmd_queue *cmd_q = &ae4dma->cmd_q;
467 : : uint32_t ctrl, hw_read, hw_write;
468 : :
469 [ # # ]: 0 : if (cmd_q->hwq_regs == NULL) {
470 : 0 : *status = RTE_DMA_VCHAN_HALTED_ERROR;
471 : 0 : return 0;
472 : : }
473 : :
474 : : ctrl = AE4DMA_READ_REG(&cmd_q->hwq_regs->control_reg.control_raw);
475 [ # # ]: 0 : if ((ctrl & AE4DMA_CMD_QUEUE_ENABLE) == 0) {
476 : 0 : *status = RTE_DMA_VCHAN_HALTED_ERROR;
477 : 0 : return 0;
478 : : }
479 : :
480 : 0 : hw_read = AE4DMA_READ_REG(&cmd_q->hwq_regs->read_idx);
481 : 0 : hw_write = AE4DMA_READ_REG(&cmd_q->hwq_regs->write_idx);
482 : :
483 : 0 : *status = (hw_read == hw_write) ? RTE_DMA_VCHAN_IDLE : RTE_DMA_VCHAN_ACTIVE;
484 : 0 : return 0;
485 : : }
486 : :
487 : : static int
488 : 0 : ae4dma_add_queue(struct ae4dma_dmadev *dev, struct rte_pci_device *pci, uint8_t qn,
489 : : const char *pci_name)
490 : : {
491 : : const struct rte_memzone *q_mz;
492 : : struct ae4dma_cmd_queue *cmd_q;
493 : : uint32_t dma_addr_lo, dma_addr_hi;
494 : :
495 : 0 : dev->io_regs = pci->mem_resource[AE4DMA_PCIE_BAR].addr;
496 : :
497 : : cmd_q = &dev->cmd_q;
498 : 0 : cmd_q->id = qn;
499 : 0 : cmd_q->qidx = 0;
500 : 0 : cmd_q->qsize = AE4DMA_QUEUE_SIZE(AE4DMA_QUEUE_DESC_SIZE);
501 : 0 : cmd_q->hwq_regs = (volatile struct ae4dma_hwq_regs *)dev->io_regs + (qn + 1);
502 : :
503 : : /*
504 : : * Memzone name must be globally unique. Embed PCI BDF so multiple
505 : : * PCI functions probed concurrently don't collide.
506 : : */
507 : 0 : snprintf(cmd_q->memz_name, sizeof(cmd_q->memz_name), "ae4dma_%s_q%u", pci_name,
508 : : (unsigned int)qn);
509 : :
510 : 0 : q_mz = ae4dma_queue_dma_zone_reserve(cmd_q->memz_name, cmd_q->qsize, rte_socket_id());
511 [ # # ]: 0 : if (q_mz == NULL) {
512 : 0 : AE4DMA_PMD_ERR("memzone reserve failed for %s", cmd_q->memz_name);
513 : 0 : return -ENOMEM;
514 : : }
515 : :
516 : 0 : cmd_q->mz = q_mz;
517 : 0 : cmd_q->qbase_addr = q_mz->addr;
518 : 0 : cmd_q->qbase_desc = q_mz->addr;
519 : 0 : cmd_q->qbase_phys_addr = q_mz->iova;
520 : :
521 : 0 : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->max_idx, AE4DMA_DESCRIPTORS_PER_CMDQ);
522 : 0 : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->control_reg.control_raw, AE4DMA_CMD_QUEUE_ENABLE);
523 : 0 : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->intr_status_reg.intr_status_raw,
524 : : AE4DMA_DISABLE_INTR);
525 : 0 : cmd_q->next_write = AE4DMA_READ_REG(&cmd_q->hwq_regs->write_idx);
526 : 0 : cmd_q->next_read = AE4DMA_READ_REG(&cmd_q->hwq_regs->read_idx);
527 : :
528 : 0 : dma_addr_lo = lower_32_bits(cmd_q->qbase_phys_addr);
529 : 0 : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->qbase_lo, dma_addr_lo);
530 : 0 : dma_addr_hi = upper_32_bits(cmd_q->qbase_phys_addr);
531 : 0 : AE4DMA_WRITE_REG(&cmd_q->hwq_regs->qbase_hi, dma_addr_hi);
532 : :
533 : 0 : return 0;
534 : : }
535 : :
536 : : static void
537 : : ae4dma_channel_dev_name(char *out, size_t outlen, const char *pci_name, unsigned int ch)
538 : : {
539 : : snprintf(out, outlen, "%s-ch%u", pci_name, ch);
540 : : }
541 : :
542 : : static int
543 : 0 : ae4dma_dmadev_create(const char *name, struct rte_pci_device *dev, uint8_t qn)
544 : : {
545 : : static const struct rte_dma_dev_ops ae4dma_dmadev_ops = {
546 : : .dev_close = ae4dma_dev_close,
547 : : .dev_configure = ae4dma_dev_configure,
548 : : .dev_dump = ae4dma_dev_dump,
549 : : .dev_info_get = ae4dma_dev_info_get,
550 : : .dev_start = ae4dma_dev_start,
551 : : .dev_stop = ae4dma_dev_stop,
552 : : .stats_get = ae4dma_stats_get,
553 : : .stats_reset = ae4dma_stats_reset,
554 : : .vchan_status = ae4dma_vchan_status,
555 : : .vchan_setup = ae4dma_vchan_setup,
556 : : };
557 : :
558 : 0 : char hwq_dev_name[RTE_DEV_NAME_MAX_LEN] = {0};
559 : : struct ae4dma_dmadev *ae4dma;
560 : : struct rte_dma_dev *dmadev;
561 : :
562 : 0 : ae4dma_channel_dev_name(hwq_dev_name, sizeof(hwq_dev_name), name, qn);
563 : :
564 : 0 : dmadev = rte_dma_pmd_allocate(hwq_dev_name, dev->device.numa_node,
565 : : sizeof(struct ae4dma_dmadev));
566 [ # # ]: 0 : if (dmadev == NULL) {
567 : 0 : AE4DMA_PMD_ERR("Unable to allocate dma device");
568 : 0 : return -ENOMEM;
569 : : }
570 : 0 : dmadev->device = &dev->device;
571 : 0 : dmadev->fp_obj->dev_private = dmadev->data->dev_private;
572 : 0 : dmadev->dev_ops = &ae4dma_dmadev_ops;
573 : :
574 : 0 : dmadev->fp_obj->burst_capacity = ae4dma_burst_capacity;
575 : 0 : dmadev->fp_obj->completed = ae4dma_completed;
576 : 0 : dmadev->fp_obj->completed_status = ae4dma_completed_status;
577 : 0 : dmadev->fp_obj->copy = ae4dma_enqueue_copy;
578 : 0 : dmadev->fp_obj->submit = ae4dma_submit;
579 : : /* fill capability not advertised: leave fp_obj->fill as zero-initialised. */
580 : :
581 : : ae4dma = dmadev->data->dev_private;
582 : :
583 [ # # ]: 0 : if (ae4dma_add_queue(ae4dma, dev, qn, name) != 0)
584 : 0 : goto init_error;
585 : : return 0;
586 : :
587 : : init_error:
588 : 0 : AE4DMA_PMD_ERR("Failed to create dmadev %s", hwq_dev_name);
589 : 0 : rte_dma_pmd_release(hwq_dev_name);
590 : 0 : return -ENOMEM;
591 : : }
592 : :
593 : : static int
594 : 0 : ae4dma_dmadev_probe(struct rte_pci_driver *drv __rte_unused, struct rte_pci_device *dev)
595 : : {
596 : : char chname[RTE_DEV_NAME_MAX_LEN];
597 : : uint32_t q_per_eng;
598 : : void *mmio_base;
599 : : char name[32];
600 : : int ret = 0;
601 : : uint8_t i;
602 : :
603 : 0 : rte_pci_device_name(&dev->addr, name, sizeof(name));
604 : 0 : AE4DMA_PMD_INFO("Init %s on NUMA node %d", name, dev->device.numa_node);
605 : :
606 : 0 : mmio_base = dev->mem_resource[AE4DMA_PCIE_BAR].addr;
607 [ # # ]: 0 : if (mmio_base == NULL) {
608 : 0 : AE4DMA_PMD_ERR("%s: BAR%d not mapped", name, AE4DMA_PCIE_BAR);
609 : 0 : return -ENODEV;
610 : : }
611 : :
612 : : /* Program the per-engine HW queue count once. */
613 : : AE4DMA_WRITE_REG_OFFSET(mmio_base, AE4DMA_COMMON_CONFIG_OFFSET, AE4DMA_MAX_HW_QUEUES);
614 : : q_per_eng = AE4DMA_READ_REG_OFFSET(mmio_base, AE4DMA_COMMON_CONFIG_OFFSET);
615 : 0 : AE4DMA_PMD_INFO("%s: AE4DMA queues per engine = %u", name, q_per_eng);
616 : :
617 [ # # ]: 0 : for (i = 0; i < AE4DMA_MAX_HW_QUEUES; i++) {
618 : 0 : ret = ae4dma_dmadev_create(name, dev, i);
619 [ # # ]: 0 : if (ret != 0) {
620 : 0 : AE4DMA_PMD_ERR("%s create dmadev %u failed!", name, i);
621 [ # # ]: 0 : while (i > 0) {
622 : 0 : i--;
623 : 0 : ae4dma_channel_dev_name(chname, sizeof(chname), name, i);
624 : 0 : rte_dma_pmd_release(chname);
625 : : }
626 : : break;
627 : : }
628 : : }
629 : : return ret;
630 : : }
631 : :
632 : : static int
633 : 0 : ae4dma_dmadev_remove(struct rte_pci_device *dev)
634 : : {
635 : : char chname[RTE_DEV_NAME_MAX_LEN];
636 : : unsigned int i;
637 : : char name[32];
638 : :
639 : 0 : rte_pci_device_name(&dev->addr, name, sizeof(name));
640 : :
641 : 0 : AE4DMA_PMD_INFO("Closing %s on NUMA node %d", name, dev->device.numa_node);
642 : :
643 [ # # ]: 0 : for (i = 0; i < AE4DMA_MAX_HW_QUEUES; i++) {
644 : : ae4dma_channel_dev_name(chname, sizeof(chname), name, i);
645 : 0 : rte_dma_pmd_release(chname);
646 : : }
647 : 0 : return 0;
648 : : }
649 : :
650 : : static const struct rte_pci_id pci_id_ae4dma_map[] = {
651 : : { RTE_PCI_DEVICE(AMD_VENDOR_ID, AE4DMA_DEVICE_ID) },
652 : : { .vendor_id = 0, /* sentinel */ },
653 : : };
654 : :
655 : : static struct rte_pci_driver ae4dma_pmd_drv = {
656 : : .id_table = pci_id_ae4dma_map,
657 : : .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
658 : : .probe = ae4dma_dmadev_probe,
659 : : .remove = ae4dma_dmadev_remove,
660 : : };
661 : :
662 : 305 : RTE_PMD_REGISTER_PCI(AE4DMA_PMD_NAME, ae4dma_pmd_drv);
663 : : RTE_PMD_REGISTER_PCI_TABLE(AE4DMA_PMD_NAME, pci_id_ae4dma_map);
664 : : RTE_PMD_REGISTER_KMOD_DEP(AE4DMA_PMD_NAME, "* igb_uio | uio_pci_generic | vfio-pci");
|