dma/idxd: create dmadev instances on bus probe
[dpdk.git] / drivers / dma / idxd / idxd_common.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2021 Intel Corporation
3  */
4
5 #include <rte_malloc.h>
6 #include <rte_common.h>
7 #include <rte_log.h>
8
9 #include "idxd_internal.h"
10
11 #define IDXD_PMD_NAME_STR "dmadev_idxd"
12
13 int
14 idxd_dmadev_create(const char *name, struct rte_device *dev,
15                    const struct idxd_dmadev *base_idxd,
16                    const struct rte_dma_dev_ops *ops)
17 {
18         struct idxd_dmadev *idxd = NULL;
19         struct rte_dma_dev *dmadev = NULL;
20         int ret = 0;
21
22         if (!name) {
23                 IDXD_PMD_ERR("Invalid name of the device!");
24                 ret = -EINVAL;
25                 goto cleanup;
26         }
27
28         /* Allocate device structure */
29         dmadev = rte_dma_pmd_allocate(name, dev->numa_node, sizeof(struct idxd_dmadev));
30         if (dmadev == NULL) {
31                 IDXD_PMD_ERR("Unable to allocate dma device");
32                 ret = -ENOMEM;
33                 goto cleanup;
34         }
35         dmadev->dev_ops = ops;
36         dmadev->device = dev;
37
38         idxd = dmadev->data->dev_private;
39         *idxd = *base_idxd; /* copy over the main fields already passed in */
40         idxd->dmadev = dmadev;
41
42         /* allocate batch index ring and completion ring.
43          * The +1 is because we can never fully use
44          * the ring, otherwise read == write means both full and empty.
45          */
46         idxd->batch_comp_ring = rte_zmalloc_socket(NULL, (sizeof(idxd->batch_idx_ring[0]) +
47                         sizeof(idxd->batch_comp_ring[0]))       * (idxd->max_batches + 1),
48                         sizeof(idxd->batch_comp_ring[0]), dev->numa_node);
49         if (idxd->batch_comp_ring == NULL) {
50                 IDXD_PMD_ERR("Unable to reserve memory for batch data\n");
51                 ret = -ENOMEM;
52                 goto cleanup;
53         }
54         idxd->batch_idx_ring = (void *)&idxd->batch_comp_ring[idxd->max_batches+1];
55         idxd->batch_iova = rte_mem_virt2iova(idxd->batch_comp_ring);
56
57         dmadev->fp_obj->dev_private = idxd;
58
59         idxd->dmadev->state = RTE_DMA_DEV_READY;
60
61         return 0;
62
63 cleanup:
64         if (dmadev)
65                 rte_dma_pmd_release(name);
66
67         return ret;
68 }
69
70 int idxd_pmd_logtype;
71
72 RTE_LOG_REGISTER_DEFAULT(idxd_pmd_logtype, WARNING);