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