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