1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2014 - 2020 Intel Corporation */
3 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include "adf_accel_devices.h"
6 #include "adf_common_drv.h"
7 #include "adf_transport.h"
8 #include "adf_transport_access_macros.h"
10 #include "adf_cfg_strings.h"
11 #include "qat_crypto.h"
12 #include "icp_qat_fw.h"
14 #define SEC ADF_KERNEL_SEC
16 static struct service_hndl qat_crypto;
18 void qat_crypto_put_instance(struct qat_crypto_instance *inst)
20 atomic_dec(&inst->refctr);
21 adf_dev_put(inst->accel_dev);
24 static int qat_crypto_free_instances(struct adf_accel_dev *accel_dev)
26 struct qat_crypto_instance *inst, *tmp;
29 list_for_each_entry_safe(inst, tmp, &accel_dev->crypto_list, list) {
30 for (i = 0; i < atomic_read(&inst->refctr); i++)
31 qat_crypto_put_instance(inst);
34 adf_remove_ring(inst->sym_tx);
37 adf_remove_ring(inst->sym_rx);
40 adf_remove_ring(inst->pke_tx);
43 adf_remove_ring(inst->pke_rx);
45 list_del(&inst->list);
51 struct qat_crypto_instance *qat_crypto_get_instance_node(int node)
53 struct adf_accel_dev *accel_dev = NULL, *tmp_dev;
54 struct qat_crypto_instance *inst = NULL, *tmp_inst;
55 unsigned long best = ~0;
57 list_for_each_entry(tmp_dev, adf_devmgr_get_head(), list) {
60 if ((node == dev_to_node(&GET_DEV(tmp_dev)) ||
61 dev_to_node(&GET_DEV(tmp_dev)) < 0) &&
62 adf_dev_started(tmp_dev) &&
63 !list_empty(&tmp_dev->crypto_list)) {
64 ctr = atomic_read(&tmp_dev->ref_count);
73 pr_info("QAT: Could not find a device on node %d\n", node);
74 /* Get any started device */
75 list_for_each_entry(tmp_dev, adf_devmgr_get_head(), list) {
76 if (adf_dev_started(tmp_dev) &&
77 !list_empty(&tmp_dev->crypto_list)) {
88 list_for_each_entry(tmp_inst, &accel_dev->crypto_list, list) {
91 ctr = atomic_read(&tmp_inst->refctr);
98 if (adf_dev_get(accel_dev)) {
99 dev_err(&GET_DEV(accel_dev), "Could not increment dev refctr\n");
102 atomic_inc(&inst->refctr);
108 * qat_crypto_dev_config() - create dev config required to create crypto inst.
110 * @accel_dev: Pointer to acceleration device.
112 * Function creates device configuration required to create crypto instances
114 * Return: 0 on success, error code otherwise.
116 int qat_crypto_dev_config(struct adf_accel_dev *accel_dev)
118 int cpus = num_online_cpus();
119 int banks = GET_MAX_BANKS(accel_dev);
120 int instances = min(cpus, banks);
121 char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
125 if (adf_cfg_section_add(accel_dev, ADF_KERNEL_SEC))
127 if (adf_cfg_section_add(accel_dev, "Accelerator0"))
130 /* Temporarily set the number of crypto instances to zero to avoid
131 * registering the crypto algorithms.
132 * This will be removed when the algorithms will support the
133 * CRYPTO_TFM_REQ_MAY_BACKLOG flag
137 for (i = 0; i < instances; i++) {
139 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_BANK_NUM, i);
140 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
141 key, (void *)&val, ADF_DEC))
144 snprintf(key, sizeof(key), ADF_CY "%d" ADF_ETRMGR_CORE_AFFINITY,
146 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
147 key, (void *)&val, ADF_DEC))
150 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, i);
152 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
153 key, (void *)&val, ADF_DEC))
157 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_SIZE, i);
158 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
159 key, (void *)&val, ADF_DEC))
163 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_TX, i);
164 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
165 key, (void *)&val, ADF_DEC))
169 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_TX, i);
170 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
171 key, (void *)&val, ADF_DEC))
175 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_RX, i);
176 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
177 key, (void *)&val, ADF_DEC))
181 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_RX, i);
182 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
183 key, (void *)&val, ADF_DEC))
186 val = ADF_COALESCING_DEF_TIME;
187 snprintf(key, sizeof(key), ADF_ETRMGR_COALESCE_TIMER_FORMAT, i);
188 if (adf_cfg_add_key_value_param(accel_dev, "Accelerator0",
189 key, (void *)&val, ADF_DEC))
194 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
195 ADF_NUM_CY, (void *)&val, ADF_DEC))
198 set_bit(ADF_STATUS_CONFIGURED, &accel_dev->status);
201 dev_err(&GET_DEV(accel_dev), "Failed to start QAT accel dev\n");
204 EXPORT_SYMBOL_GPL(qat_crypto_dev_config);
206 static int qat_crypto_create_instances(struct adf_accel_dev *accel_dev)
210 unsigned long num_inst, num_msg_sym, num_msg_asym;
212 struct qat_crypto_instance *inst;
213 char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
214 char val[ADF_CFG_MAX_VAL_LEN_IN_BYTES];
216 INIT_LIST_HEAD(&accel_dev->crypto_list);
217 if (adf_cfg_get_param_value(accel_dev, SEC, ADF_NUM_CY, val))
220 if (kstrtoul(val, 0, &num_inst))
223 for (i = 0; i < num_inst; i++) {
224 inst = kzalloc_node(sizeof(*inst), GFP_KERNEL,
225 dev_to_node(&GET_DEV(accel_dev)));
229 list_add_tail(&inst->list, &accel_dev->crypto_list);
231 atomic_set(&inst->refctr, 0);
232 inst->accel_dev = accel_dev;
233 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_BANK_NUM, i);
234 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
237 if (kstrtoul(val, 10, &bank))
239 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_SIZE, i);
240 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
243 if (kstrtoul(val, 10, &num_msg_sym))
246 num_msg_sym = num_msg_sym >> 1;
248 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, i);
249 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
252 if (kstrtoul(val, 10, &num_msg_asym))
254 num_msg_asym = num_msg_asym >> 1;
256 msg_size = ICP_QAT_FW_REQ_DEFAULT_SZ;
257 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_TX, i);
258 if (adf_create_ring(accel_dev, SEC, bank, num_msg_sym,
259 msg_size, key, NULL, 0, &inst->sym_tx))
262 msg_size = msg_size >> 1;
263 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_TX, i);
264 if (adf_create_ring(accel_dev, SEC, bank, num_msg_asym,
265 msg_size, key, NULL, 0, &inst->pke_tx))
268 msg_size = ICP_QAT_FW_RESP_DEFAULT_SZ;
269 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_RX, i);
270 if (adf_create_ring(accel_dev, SEC, bank, num_msg_sym,
271 msg_size, key, qat_alg_callback, 0,
275 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_RX, i);
276 if (adf_create_ring(accel_dev, SEC, bank, num_msg_asym,
277 msg_size, key, qat_alg_asym_callback, 0,
283 qat_crypto_free_instances(accel_dev);
287 static int qat_crypto_init(struct adf_accel_dev *accel_dev)
289 if (qat_crypto_create_instances(accel_dev))
295 static int qat_crypto_shutdown(struct adf_accel_dev *accel_dev)
297 return qat_crypto_free_instances(accel_dev);
300 static int qat_crypto_event_handler(struct adf_accel_dev *accel_dev,
301 enum adf_event event)
307 ret = qat_crypto_init(accel_dev);
309 case ADF_EVENT_SHUTDOWN:
310 ret = qat_crypto_shutdown(accel_dev);
312 case ADF_EVENT_RESTARTING:
313 case ADF_EVENT_RESTARTED:
314 case ADF_EVENT_START:
322 int qat_crypto_register(void)
324 memset(&qat_crypto, 0, sizeof(qat_crypto));
325 qat_crypto.event_hld = qat_crypto_event_handler;
326 qat_crypto.name = "qat_crypto";
327 return adf_service_register(&qat_crypto);
330 int qat_crypto_unregister(void)
332 return adf_service_unregister(&qat_crypto);