Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2024 ZTE Corporation
3 : : */
4 : :
5 : : #include "zsda_qp.h"
6 : : #include "zsda_device.h"
7 : :
8 : : /* per-process array of device data */
9 : : struct zsda_device_info zsda_devs[RTE_PMD_ZSDA_MAX_PCI_DEVICES];
10 : : static int zsda_nb_pci_devices;
11 : :
12 : : /*
13 : : * The set of PCI devices this driver supports
14 : : */
15 : : static const struct rte_pci_id pci_id_zsda_map[] = {
16 : : {
17 : : RTE_PCI_DEVICE(0x1cf2, 0x8050),
18 : : },
19 : : {
20 : : RTE_PCI_DEVICE(0x1cf2, 0x8051),
21 : : },
22 : : {.device_id = 0},
23 : : };
24 : :
25 : : static struct zsda_pci_device *
26 : 0 : zsda_pci_dev_by_name_get(const char *name)
27 : : {
28 : : unsigned int i;
29 : :
30 [ # # ]: 0 : if (name == NULL) {
31 : 0 : ZSDA_LOG(ERR, "Failed! name is NULL.");
32 : 0 : return NULL;
33 : : }
34 [ # # ]: 0 : for (i = 0; i < RTE_PMD_ZSDA_MAX_PCI_DEVICES; i++) {
35 [ # # ]: 0 : if (zsda_devs[i].mz &&
36 : 0 : (strcmp(((struct zsda_pci_device *)zsda_devs[i].mz->addr)
37 [ # # ]: 0 : ->name,
38 : : name) == 0))
39 : 0 : return (struct zsda_pci_device *)zsda_devs[i].mz->addr;
40 : : }
41 : :
42 : : return NULL;
43 : : }
44 : :
45 : : static uint8_t
46 : : zsda_pci_dev_free_id_get(void)
47 : : {
48 : : uint32_t dev_id;
49 : :
50 [ # # ]: 0 : for (dev_id = 0; dev_id < RTE_PMD_ZSDA_MAX_PCI_DEVICES; dev_id++)
51 [ # # ]: 0 : if (zsda_devs[dev_id].mz == NULL)
52 : : break;
53 : :
54 : 0 : return dev_id & (ZSDA_MAX_DEV - 1);
55 : : }
56 : :
57 : : static struct zsda_pci_device *
58 : : zsda_pci_dev_get(const struct rte_pci_device *pci_dev)
59 : : {
60 : : char name[ZSDA_DEV_NAME_MAX_LEN];
61 : :
62 : 0 : rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
63 : :
64 : 0 : return zsda_pci_dev_by_name_get(name);
65 : : }
66 : :
67 : : static struct zsda_pci_device *
68 : 0 : zsda_pci_device_allocate(struct rte_pci_device *pci_dev)
69 : : {
70 : : struct zsda_pci_device *zsda_pci_dev;
71 : : uint8_t zsda_dev_id;
72 : : char name[ZSDA_DEV_NAME_MAX_LEN];
73 : 0 : unsigned int socket_id = rte_socket_id();
74 : :
75 : 0 : rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
76 : 0 : snprintf(name + strlen(name), (ZSDA_DEV_NAME_MAX_LEN - strlen(name)),
77 : : "_zsda");
78 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
79 : 0 : const struct rte_memzone *mz = rte_memzone_lookup(name);
80 : :
81 [ # # ]: 0 : if (mz == NULL) {
82 : 0 : ZSDA_LOG(ERR, "Secondary can't find %s mz", name);
83 : 0 : return NULL;
84 : : }
85 : 0 : zsda_pci_dev = mz->addr;
86 : 0 : zsda_devs[zsda_pci_dev->zsda_dev_id].mz = mz;
87 : 0 : zsda_devs[zsda_pci_dev->zsda_dev_id].pci_dev = pci_dev;
88 : 0 : zsda_nb_pci_devices++;
89 : 0 : return zsda_pci_dev;
90 : : }
91 : :
92 [ # # ]: 0 : if (zsda_pci_dev_by_name_get(name) != NULL) {
93 : 0 : ZSDA_LOG(ERR, "Failed! config");
94 : 0 : return NULL;
95 : : }
96 : :
97 : : zsda_dev_id = zsda_pci_dev_free_id_get();
98 : :
99 [ # # ]: 0 : if (zsda_dev_id == (RTE_PMD_ZSDA_MAX_PCI_DEVICES - 1))
100 : : return NULL;
101 : :
102 : 0 : zsda_devs[zsda_dev_id].mz =
103 : 0 : rte_memzone_reserve(name, sizeof(struct zsda_pci_device),
104 : 0 : (int)(socket_id & 0xfff), 0);
105 : :
106 [ # # ]: 0 : if (zsda_devs[zsda_dev_id].mz == NULL) {
107 : 0 : ZSDA_LOG(ERR, "Failed! malloc");
108 : 0 : return NULL;
109 : : }
110 : 0 : zsda_pci_dev = zsda_devs[zsda_dev_id].mz->addr;
111 : : memset(zsda_pci_dev, 0, sizeof(*zsda_pci_dev));
112 : 0 : memcpy(zsda_pci_dev->name, name, ZSDA_DEV_NAME_MAX_LEN);
113 : 0 : zsda_pci_dev->zsda_dev_id = zsda_dev_id;
114 : 0 : zsda_pci_dev->pci_dev = pci_dev;
115 : 0 : zsda_devs[zsda_dev_id].pci_dev = pci_dev;
116 : :
117 : 0 : zsda_nb_pci_devices++;
118 : :
119 : 0 : return zsda_pci_dev;
120 : : }
121 : :
122 : : static int
123 : 0 : zsda_pci_device_release(const struct rte_pci_device *pci_dev)
124 : : {
125 : : struct zsda_pci_device *zsda_pci_dev;
126 : : struct zsda_device_info *inst;
127 : : char name[ZSDA_DEV_NAME_MAX_LEN];
128 : :
129 [ # # ]: 0 : if (pci_dev == NULL)
130 : : return -EINVAL;
131 : :
132 : 0 : rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
133 : :
134 : 0 : snprintf(name + strlen(name),
135 : 0 : ZSDA_DEV_NAME_MAX_LEN - (strlen(name) - 1), "_zsda");
136 : 0 : zsda_pci_dev = zsda_pci_dev_by_name_get(name);
137 [ # # ]: 0 : if (zsda_pci_dev != NULL) {
138 : 0 : inst = &zsda_devs[zsda_pci_dev->zsda_dev_id];
139 : :
140 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
141 : 0 : rte_memzone_free(inst->mz);
142 : :
143 : : memset(inst, 0, sizeof(struct zsda_device_info));
144 : 0 : zsda_nb_pci_devices--;
145 : : }
146 : : return 0;
147 : : }
148 : :
149 : : static int
150 : : zsda_pci_dev_destroy(struct zsda_pci_device *zsda_pci_dev,
151 : : const struct rte_pci_device *pci_dev)
152 : : {
153 : :
154 : 0 : zsda_comp_dev_destroy(zsda_pci_dev);
155 : :
156 : 0 : return zsda_pci_device_release(pci_dev);
157 : : }
158 : :
159 : : static int
160 : 0 : zsda_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
161 : : struct rte_pci_device *pci_dev)
162 : : {
163 : : int ret = 0;
164 : : struct zsda_pci_device *zsda_pci_dev;
165 : :
166 : 0 : zsda_pci_dev = zsda_pci_device_allocate(pci_dev);
167 [ # # ]: 0 : if (zsda_pci_dev == NULL) {
168 : 0 : ZSDA_LOG(ERR, "Failed! zsda_pci_dev is NULL");
169 : 0 : return -ENODEV;
170 : : }
171 : :
172 : 0 : ret = zsda_queue_init(zsda_pci_dev);
173 [ # # ]: 0 : if (ret) {
174 : 0 : ZSDA_LOG(ERR, "Failed! queue init.");
175 : 0 : return ret;
176 : : }
177 : :
178 : 0 : ret = zsda_comp_dev_create(zsda_pci_dev);
179 [ # # ]: 0 : if (ret)
180 : 0 : ZSDA_LOG(ERR, "Failed! dev create.");
181 : :
182 : : return ret;
183 : : }
184 : :
185 : : static int
186 : 0 : zsda_pci_remove(struct rte_pci_device *pci_dev)
187 : : {
188 : : struct zsda_pci_device *zsda_pci_dev;
189 : :
190 [ # # ]: 0 : if (pci_dev == NULL)
191 : : return -EINVAL;
192 : :
193 : : zsda_pci_dev = zsda_pci_dev_get(pci_dev);
194 [ # # ]: 0 : if (zsda_pci_dev == NULL)
195 : : return 0;
196 : :
197 : 0 : return zsda_pci_dev_destroy(zsda_pci_dev, pci_dev);
198 : : }
199 : :
200 : : static struct rte_pci_driver rte_zsda_pmd = {
201 : : .id_table = pci_id_zsda_map,
202 : : .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
203 : : .probe = zsda_pci_probe,
204 : : .remove = zsda_pci_remove };
205 : :
206 : 252 : RTE_PMD_REGISTER_PCI(ZSDA_PCI_NAME, rte_zsda_pmd);
207 : : RTE_PMD_REGISTER_PCI_TABLE(ZSDA_PCI_NAME, pci_id_zsda_map);
208 : : RTE_PMD_REGISTER_KMOD_DEP(ZSDA_PCI_NAME,
209 : : "* igb_uio | uio_pci_generic | vfio-pci");
|