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