summaryrefslogtreecommitdiff
path: root/drivers/crypto/qat/qat_device.c
blob: 0cab2e9281791753f5816c150bb1e9c0a54524f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Intel Corporation
 */

#include "qat_device.h"
#include "adf_transport_access_macros.h"
#include "qat_sym_pmd.h"
#include "qat_asym_pmd.h"
#include "qat_comp_pmd.h"

/* Hardware device information per generation */
__extension__
struct qat_gen_hw_data qat_gen_config[] =  {
	[QAT_GEN1] = {
		.dev_gen = QAT_GEN1,
		.qp_hw_data = qat_gen1_qps,
	},
	[QAT_GEN2] = {
		.dev_gen = QAT_GEN2,
		.qp_hw_data = qat_gen1_qps,
		/* gen2 has same ring layout as gen1 */
	},
};


static struct qat_pci_device qat_pci_devices[RTE_PMD_QAT_MAX_PCI_DEVICES];
static int qat_nb_pci_devices;

/*
 * The set of PCI devices this driver supports
 */

static const struct rte_pci_id pci_id_qat_map[] = {
		{
			RTE_PCI_DEVICE(0x8086, 0x0443),
		},
		{
			RTE_PCI_DEVICE(0x8086, 0x37c9),
		},
		{
			RTE_PCI_DEVICE(0x8086, 0x19e3),
		},
		{
			RTE_PCI_DEVICE(0x8086, 0x6f55),
		},
		{.device_id = 0},
};


static struct qat_pci_device *
qat_pci_get_dev(uint8_t dev_id)
{
	return &qat_pci_devices[dev_id];
}

static struct qat_pci_device *
qat_pci_get_named_dev(const char *name)
{
	struct qat_pci_device *dev;
	unsigned int i;

	if (name == NULL)
		return NULL;

	for (i = 0; i < RTE_PMD_QAT_MAX_PCI_DEVICES; i++) {
		dev = &qat_pci_devices[i];

		if ((dev->attached == QAT_ATTACHED) &&
				(strcmp(dev->name, name) == 0))
			return dev;
	}

	return NULL;
}

static uint8_t
qat_pci_find_free_device_index(void)
{
	uint8_t dev_id;

	for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES; dev_id++) {
		if (qat_pci_devices[dev_id].attached == QAT_DETACHED)
			break;
	}
	return dev_id;
}

struct qat_pci_device *
qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
{
	char name[QAT_DEV_NAME_MAX_LEN];

	rte_pci_device_name(&pci_dev->addr, name, sizeof(name));

	return qat_pci_get_named_dev(name);
}

struct qat_pci_device *
qat_pci_device_allocate(struct rte_pci_device *pci_dev)
{
	struct qat_pci_device *qat_dev;
	uint8_t qat_dev_id;
	char name[QAT_DEV_NAME_MAX_LEN];

	rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
	snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
	if (qat_pci_get_named_dev(name) != NULL) {
		PMD_DRV_LOG(ERR, "QAT device with name %s already allocated!",
				name);
		return NULL;
	}

	qat_dev_id = qat_pci_find_free_device_index();
	if (qat_dev_id == RTE_PMD_QAT_MAX_PCI_DEVICES) {
		PMD_DRV_LOG(ERR, "Reached maximum number of QAT devices");
		return NULL;
	}

	qat_dev = qat_pci_get_dev(qat_dev_id);
	memset(qat_dev, 0, sizeof(*qat_dev));
	snprintf(qat_dev->name, QAT_DEV_NAME_MAX_LEN, "%s", name);
	qat_dev->qat_dev_id = qat_dev_id;
	qat_dev->pci_dev = pci_dev;
	switch (qat_dev->pci_dev->id.device_id) {
	case 0x0443:
		qat_dev->qat_dev_gen = QAT_GEN1;
		break;
	case 0x37c9:
	case 0x19e3:
	case 0x6f55:
		qat_dev->qat_dev_gen = QAT_GEN2;
		break;
	default:
		PMD_DRV_LOG(ERR, "Invalid dev_id, can't determine generation");
		return NULL;
	}

	rte_spinlock_init(&qat_dev->arb_csr_lock);

	qat_dev->attached = QAT_ATTACHED;

	qat_nb_pci_devices++;

	PMD_DRV_LOG(DEBUG, "QAT device %d allocated, name %s, total QATs %d",
			qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);

	return qat_dev;
}

int
qat_pci_device_release(struct rte_pci_device *pci_dev)
{
	struct qat_pci_device *qat_dev;
	char name[QAT_DEV_NAME_MAX_LEN];

	if (pci_dev == NULL)
		return -EINVAL;

	rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
	snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
	qat_dev = qat_pci_get_named_dev(name);
	if (qat_dev != NULL) {

		/* Check that there are no service devs still on pci device */
		if (qat_dev->sym_dev != NULL)
			return -EBUSY;

		qat_dev->attached = QAT_DETACHED;
		qat_nb_pci_devices--;
	}
	PMD_DRV_LOG(DEBUG, "QAT device %s released, total QATs %d",
				name, qat_nb_pci_devices);
	return 0;
}

static int
qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
		struct rte_pci_device *pci_dev)
{
	qat_sym_dev_destroy(qat_pci_dev);
	qat_comp_dev_destroy(qat_pci_dev);
	qat_asym_dev_destroy(qat_pci_dev);
	return qat_pci_device_release(pci_dev);
}

static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
		struct rte_pci_device *pci_dev)
{
	int ret = 0;
	struct qat_pci_device *qat_pci_dev;

	PMD_DRV_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
			pci_dev->addr.bus,
			pci_dev->addr.devid,
			pci_dev->addr.function);

	qat_pci_dev = qat_pci_device_allocate(pci_dev);
	if (qat_pci_dev == NULL)
		return -ENODEV;

	ret = qat_sym_dev_create(qat_pci_dev);
	if (ret != 0)
		goto error_out;

	ret = qat_comp_dev_create(qat_pci_dev);
	if (ret != 0)
		goto error_out;

	ret = qat_asym_dev_create(qat_pci_dev);
	if (ret != 0)
		goto error_out;

	return 0;

error_out:
	qat_pci_dev_destroy(qat_pci_dev, pci_dev);
	return ret;

}

static int qat_pci_remove(struct rte_pci_device *pci_dev)
{
	struct qat_pci_device *qat_pci_dev;

	if (pci_dev == NULL)
		return -EINVAL;

	qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
	if (qat_pci_dev == NULL)
		return 0;

	return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
}

static struct rte_pci_driver rte_qat_pmd = {
	.id_table = pci_id_qat_map,
	.drv_flags = RTE_PCI_DRV_NEED_MAPPING,
	.probe = qat_pci_probe,
	.remove = qat_pci_remove
};
RTE_PMD_REGISTER_PCI(QAT_PCI_NAME, rte_qat_pmd);
RTE_PMD_REGISTER_PCI_TABLE(QAT_PCI_NAME, pci_id_qat_map);