common/qat: fix failure to create PMD
[dpdk.git] / drivers / common / qat / qat_device.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <rte_string_fns.h>
6
7 #include "qat_device.h"
8 #include "adf_transport_access_macros.h"
9 #include "qat_sym_pmd.h"
10
11 /* Hardware device information per generation */
12 __extension__
13 struct qat_gen_hw_data qat_gen_config[] =  {
14         [QAT_GEN1] = {
15                 .dev_gen = QAT_GEN1,
16                 .qp_hw_data = qat_gen1_qps,
17         },
18         [QAT_GEN2] = {
19                 .dev_gen = QAT_GEN2,
20                 .qp_hw_data = qat_gen1_qps,
21                 /* gen2 has same ring layout as gen1 */
22         },
23         [QAT_GEN3] = {
24                 .dev_gen = QAT_GEN3,
25                 .qp_hw_data = qat_gen3_qps,
26         },
27 };
28
29
30 static struct qat_pci_device qat_pci_devices[RTE_PMD_QAT_MAX_PCI_DEVICES];
31 static int qat_nb_pci_devices;
32
33 /*
34  * The set of PCI devices this driver supports
35  */
36
37 static const struct rte_pci_id pci_id_qat_map[] = {
38                 {
39                         RTE_PCI_DEVICE(0x8086, 0x0443),
40                 },
41                 {
42                         RTE_PCI_DEVICE(0x8086, 0x37c9),
43                 },
44                 {
45                         RTE_PCI_DEVICE(0x8086, 0x19e3),
46                 },
47                 {
48                         RTE_PCI_DEVICE(0x8086, 0x6f55),
49                 },
50                 {
51                         RTE_PCI_DEVICE(0x8086, 0x18a1),
52                 },
53                 {.device_id = 0},
54 };
55
56 static struct qat_pci_device *
57 qat_pci_get_dev(uint8_t dev_id)
58 {
59         return &qat_pci_devices[dev_id];
60 }
61
62 static struct qat_pci_device *
63 qat_pci_get_named_dev(const char *name)
64 {
65         struct qat_pci_device *dev;
66         unsigned int i;
67
68         if (name == NULL)
69                 return NULL;
70
71         for (i = 0; i < RTE_PMD_QAT_MAX_PCI_DEVICES; i++) {
72                 dev = &qat_pci_devices[i];
73
74                 if ((dev->attached == QAT_ATTACHED) &&
75                                 (strcmp(dev->name, name) == 0))
76                         return dev;
77         }
78
79         return NULL;
80 }
81
82 static uint8_t
83 qat_pci_find_free_device_index(void)
84 {
85         uint8_t dev_id;
86
87         for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES; dev_id++) {
88                 if (qat_pci_devices[dev_id].attached == QAT_DETACHED)
89                         break;
90         }
91         return dev_id;
92 }
93
94 struct qat_pci_device *
95 qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
96 {
97         char name[QAT_DEV_NAME_MAX_LEN];
98
99         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
100
101         return qat_pci_get_named_dev(name);
102 }
103
104 struct qat_pci_device *
105 qat_pci_device_allocate(struct rte_pci_device *pci_dev)
106 {
107         struct qat_pci_device *qat_dev;
108         uint8_t qat_dev_id;
109         char name[QAT_DEV_NAME_MAX_LEN];
110
111         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
112         snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
113         if (qat_pci_get_named_dev(name) != NULL) {
114                 QAT_LOG(ERR, "QAT device with name %s already allocated!",
115                                 name);
116                 return NULL;
117         }
118
119         qat_dev_id = qat_pci_find_free_device_index();
120         if (qat_dev_id == RTE_PMD_QAT_MAX_PCI_DEVICES) {
121                 QAT_LOG(ERR, "Reached maximum number of QAT devices");
122                 return NULL;
123         }
124
125         qat_dev = qat_pci_get_dev(qat_dev_id);
126         memset(qat_dev, 0, sizeof(*qat_dev));
127         strlcpy(qat_dev->name, name, QAT_DEV_NAME_MAX_LEN);
128         qat_dev->qat_dev_id = qat_dev_id;
129         qat_dev->pci_dev = pci_dev;
130         switch (qat_dev->pci_dev->id.device_id) {
131         case 0x0443:
132                 qat_dev->qat_dev_gen = QAT_GEN1;
133                 break;
134         case 0x37c9:
135         case 0x19e3:
136         case 0x6f55:
137                 qat_dev->qat_dev_gen = QAT_GEN2;
138                 break;
139         case 0x18a1:
140                 qat_dev->qat_dev_gen = QAT_GEN3;
141                 break;
142         default:
143                 QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
144                 return NULL;
145         }
146
147         rte_spinlock_init(&qat_dev->arb_csr_lock);
148
149         qat_dev->attached = QAT_ATTACHED;
150
151         qat_nb_pci_devices++;
152
153         QAT_LOG(DEBUG, "QAT device %d allocated, name %s, total QATs %d",
154                         qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
155
156         return qat_dev;
157 }
158
159 int
160 qat_pci_device_release(struct rte_pci_device *pci_dev)
161 {
162         struct qat_pci_device *qat_dev;
163         char name[QAT_DEV_NAME_MAX_LEN];
164
165         if (pci_dev == NULL)
166                 return -EINVAL;
167
168         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
169         snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
170         qat_dev = qat_pci_get_named_dev(name);
171         if (qat_dev != NULL) {
172
173                 /* Check that there are no service devs still on pci device */
174                 if (qat_dev->sym_dev != NULL)
175                         return -EBUSY;
176
177                 qat_dev->attached = QAT_DETACHED;
178                 qat_nb_pci_devices--;
179         }
180         QAT_LOG(DEBUG, "QAT device %s released, total QATs %d",
181                                 name, qat_nb_pci_devices);
182         return 0;
183 }
184
185 static int
186 qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
187                 struct rte_pci_device *pci_dev)
188 {
189         qat_sym_dev_destroy(qat_pci_dev);
190         qat_comp_dev_destroy(qat_pci_dev);
191         qat_asym_dev_destroy(qat_pci_dev);
192         return qat_pci_device_release(pci_dev);
193 }
194
195 static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
196                 struct rte_pci_device *pci_dev)
197 {
198         int ret = 0;
199         int num_pmds_created = 0;
200         struct qat_pci_device *qat_pci_dev;
201
202         QAT_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
203                         pci_dev->addr.bus,
204                         pci_dev->addr.devid,
205                         pci_dev->addr.function);
206
207         qat_pci_dev = qat_pci_device_allocate(pci_dev);
208         if (qat_pci_dev == NULL)
209                 return -ENODEV;
210
211         ret = qat_sym_dev_create(qat_pci_dev);
212         if (ret == 0)
213                 num_pmds_created++;
214         else
215                 QAT_LOG(WARNING,
216                                 "Failed to create QAT SYM PMD on device %s",
217                                 qat_pci_dev->name);
218
219         ret = qat_comp_dev_create(qat_pci_dev);
220         if (ret == 0)
221                 num_pmds_created++;
222         else
223                 QAT_LOG(WARNING,
224                                 "Failed to create QAT COMP PMD on device %s",
225                                 qat_pci_dev->name);
226
227         ret = qat_asym_dev_create(qat_pci_dev);
228         if (ret == 0)
229                 num_pmds_created++;
230         else
231                 QAT_LOG(WARNING,
232                                 "Failed to create QAT ASYM PMD on device %s",
233                                 qat_pci_dev->name);
234
235         if (num_pmds_created == 0)
236                 qat_pci_dev_destroy(qat_pci_dev, pci_dev);
237
238         return 0;
239 }
240
241 static int qat_pci_remove(struct rte_pci_device *pci_dev)
242 {
243         struct qat_pci_device *qat_pci_dev;
244
245         if (pci_dev == NULL)
246                 return -EINVAL;
247
248         qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
249         if (qat_pci_dev == NULL)
250                 return 0;
251
252         return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
253 }
254
255 static struct rte_pci_driver rte_qat_pmd = {
256         .id_table = pci_id_qat_map,
257         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
258         .probe = qat_pci_probe,
259         .remove = qat_pci_remove
260 };
261
262 __attribute__((weak)) int
263 qat_sym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
264 {
265         return 0;
266 }
267
268 __attribute__((weak)) int
269 qat_asym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
270 {
271         return 0;
272 }
273
274 __attribute__((weak)) int
275 qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
276 {
277         return 0;
278 }
279
280 __attribute__((weak)) int
281 qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
282 {
283         return 0;
284 }
285
286 __attribute__((weak)) int
287 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
288 {
289         return 0;
290 }
291
292 __attribute__((weak)) int
293 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
294 {
295         return 0;
296 }
297
298 RTE_PMD_REGISTER_PCI(QAT_PCI_NAME, rte_qat_pmd);
299 RTE_PMD_REGISTER_PCI_TABLE(QAT_PCI_NAME, pci_id_qat_map);