dma/hisilicon: introduce driver skeleton
[dpdk.git] / drivers / dma / hisilicon / hisi_dmadev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 HiSilicon Limited
3  */
4
5 #include <inttypes.h>
6 #include <string.h>
7
8 #include <rte_bus_pci.h>
9 #include <rte_eal.h>
10 #include <rte_log.h>
11 #include <rte_pci.h>
12 #include <rte_dmadev_pmd.h>
13
14 #include "hisi_dmadev.h"
15
16 RTE_LOG_REGISTER_DEFAULT(hisi_dma_logtype, INFO);
17 #define HISI_DMA_LOG(level, fmt, args...) \
18                 rte_log(RTE_LOG_ ## level, hisi_dma_logtype, \
19                 "%s(): " fmt "\n", __func__, ##args)
20 #define HISI_DMA_LOG_RAW(hw, level, fmt, args...) \
21                 rte_log(RTE_LOG_ ## level, hisi_dma_logtype, \
22                 "%s %s(): " fmt "\n", (hw)->data->dev_name, \
23                 __func__, ##args)
24 #define HISI_DMA_DEBUG(hw, fmt, args...) \
25                 HISI_DMA_LOG_RAW(hw, DEBUG, fmt, ## args)
26 #define HISI_DMA_INFO(hw, fmt, args...) \
27                 HISI_DMA_LOG_RAW(hw, INFO, fmt, ## args)
28 #define HISI_DMA_WARN(hw, fmt, args...) \
29                 HISI_DMA_LOG_RAW(hw, WARNING, fmt, ## args)
30 #define HISI_DMA_ERR(hw, fmt, args...) \
31                 HISI_DMA_LOG_RAW(hw, ERR, fmt, ## args)
32
33 static uint8_t
34 hisi_dma_reg_layout(uint8_t revision)
35 {
36         if (revision == HISI_DMA_REVISION_HIP08B)
37                 return HISI_DMA_REG_LAYOUT_HIP08;
38         else
39                 return HISI_DMA_REG_LAYOUT_INVALID;
40 }
41
42 static void
43 hisi_dma_gen_pci_device_name(const struct rte_pci_device *pci_dev,
44                              char *name, size_t size)
45 {
46         memset(name, 0, size);
47         (void)snprintf(name, size, "%x:%x.%x",
48                  pci_dev->addr.bus, pci_dev->addr.devid,
49                  pci_dev->addr.function);
50 }
51
52 static int
53 hisi_dma_check_revision(struct rte_pci_device *pci_dev, const char *name,
54                         uint8_t *out_revision)
55 {
56         uint8_t revision;
57         int ret;
58
59         ret = rte_pci_read_config(pci_dev, &revision, 1,
60                                   HISI_DMA_PCI_REVISION_ID_REG);
61         if (ret != 1) {
62                 HISI_DMA_LOG(ERR, "%s read PCI revision failed!", name);
63                 return -EINVAL;
64         }
65         if (hisi_dma_reg_layout(revision) == HISI_DMA_REG_LAYOUT_INVALID) {
66                 HISI_DMA_LOG(ERR, "%s revision: 0x%x not supported!",
67                              name, revision);
68                 return -EINVAL;
69         }
70
71         *out_revision = revision;
72         return 0;
73 }
74
75 static int
76 hisi_dma_probe(struct rte_pci_driver *pci_drv __rte_unused,
77                struct rte_pci_device *pci_dev)
78 {
79         char name[RTE_DEV_NAME_MAX_LEN] = { 0 };
80         uint8_t revision;
81         int ret;
82
83         hisi_dma_gen_pci_device_name(pci_dev, name, sizeof(name));
84
85         if (pci_dev->mem_resource[2].addr == NULL) {
86                 HISI_DMA_LOG(ERR, "%s BAR2 is NULL!\n", name);
87                 return -ENODEV;
88         }
89
90         ret = hisi_dma_check_revision(pci_dev, name, &revision);
91         if (ret)
92                 return ret;
93         HISI_DMA_LOG(DEBUG, "%s read PCI revision: 0x%x", name, revision);
94
95         return ret;
96 }
97
98 static int
99 hisi_dma_remove(struct rte_pci_device *pci_dev)
100 {
101         RTE_SET_USED(pci_dev);
102         return 0;
103 }
104
105 static const struct rte_pci_id pci_id_hisi_dma_map[] = {
106         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HISI_DMA_DEVICE_ID) },
107         { .vendor_id = 0, }, /* sentinel */
108 };
109
110 static struct rte_pci_driver hisi_dma_pmd_drv = {
111         .id_table  = pci_id_hisi_dma_map,
112         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
113         .probe     = hisi_dma_probe,
114         .remove    = hisi_dma_remove,
115 };
116
117 RTE_PMD_REGISTER_PCI(dma_hisilicon, hisi_dma_pmd_drv);
118 RTE_PMD_REGISTER_PCI_TABLE(dma_hisilicon, pci_id_hisi_dma_map);
119 RTE_PMD_REGISTER_KMOD_DEP(dma_hisilicon, "vfio-pci");