2 This file is provided under a dual BSD/GPLv2 license. When using or
3 redistributing this file, you may do so under either license.
6 Copyright(c) 2014 Intel Corporation.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of version 2 of the GNU General Public License as
9 published by the Free Software Foundation.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
20 Copyright(c) 2014 Intel Corporation.
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions
25 * Redistributions of source code must retain the above copyright
26 notice, this list of conditions and the following disclaimer.
27 * Redistributions in binary form must reproduce the above copyright
28 notice, this list of conditions and the following disclaimer in
29 the documentation and/or other materials provided with the
31 * Neither the name of Intel Corporation nor the names of its
32 contributors may be used to endorse or promote products derived
33 from this software without specific prior written permission.
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include "adf_accel_devices.h"
50 #include "adf_common_drv.h"
51 #include "adf_transport.h"
52 #include "adf_transport_access_macros.h"
54 #include "adf_cfg_strings.h"
55 #include "qat_crypto.h"
56 #include "icp_qat_fw.h"
58 #define SEC ADF_KERNEL_SEC
60 static struct service_hndl qat_crypto;
62 void qat_crypto_put_instance(struct qat_crypto_instance *inst)
64 atomic_dec(&inst->refctr);
65 adf_dev_put(inst->accel_dev);
68 static int qat_crypto_free_instances(struct adf_accel_dev *accel_dev)
70 struct qat_crypto_instance *inst, *tmp;
73 list_for_each_entry_safe(inst, tmp, &accel_dev->crypto_list, list) {
74 for (i = 0; i < atomic_read(&inst->refctr); i++)
75 qat_crypto_put_instance(inst);
78 adf_remove_ring(inst->sym_tx);
81 adf_remove_ring(inst->sym_rx);
84 adf_remove_ring(inst->pke_tx);
87 adf_remove_ring(inst->pke_rx);
89 list_del(&inst->list);
95 struct qat_crypto_instance *qat_crypto_get_instance_node(int node)
97 struct adf_accel_dev *accel_dev = NULL, *tmp_dev;
98 struct qat_crypto_instance *inst = NULL, *tmp_inst;
99 unsigned long best = ~0;
101 list_for_each_entry(tmp_dev, adf_devmgr_get_head(), list) {
104 if ((node == dev_to_node(&GET_DEV(tmp_dev)) ||
105 dev_to_node(&GET_DEV(tmp_dev)) < 0) &&
106 adf_dev_started(tmp_dev) &&
107 !list_empty(&tmp_dev->crypto_list)) {
108 ctr = atomic_read(&tmp_dev->ref_count);
117 pr_info("QAT: Could not find a device on node %d\n", node);
118 /* Get any started device */
119 list_for_each_entry(tmp_dev, adf_devmgr_get_head(), list) {
120 if (adf_dev_started(tmp_dev) &&
121 !list_empty(&tmp_dev->crypto_list)) {
132 list_for_each_entry(tmp_inst, &accel_dev->crypto_list, list) {
135 ctr = atomic_read(&tmp_inst->refctr);
142 if (adf_dev_get(accel_dev)) {
143 dev_err(&GET_DEV(accel_dev), "Could not increment dev refctr\n");
146 atomic_inc(&inst->refctr);
152 * qat_crypto_dev_config() - create dev config required to create crypto inst.
154 * @accel_dev: Pointer to acceleration device.
156 * Function creates device configuration required to create crypto instances
158 * Return: 0 on success, error code otherwise.
160 int qat_crypto_dev_config(struct adf_accel_dev *accel_dev)
162 int cpus = num_online_cpus();
163 int banks = GET_MAX_BANKS(accel_dev);
164 int instances = min(cpus, banks);
165 char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
169 if (adf_cfg_section_add(accel_dev, ADF_KERNEL_SEC))
171 if (adf_cfg_section_add(accel_dev, "Accelerator0"))
174 /* Temporarily set the number of crypto instances to zero to avoid
175 * registering the crypto algorithms.
176 * This will be removed when the algorithms will support the
177 * CRYPTO_TFM_REQ_MAY_BACKLOG flag
181 for (i = 0; i < instances; i++) {
183 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_BANK_NUM, i);
184 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
185 key, (void *)&val, ADF_DEC))
188 snprintf(key, sizeof(key), ADF_CY "%d" ADF_ETRMGR_CORE_AFFINITY,
190 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
191 key, (void *)&val, ADF_DEC))
194 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, i);
196 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
197 key, (void *)&val, ADF_DEC))
201 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_SIZE, i);
202 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
203 key, (void *)&val, ADF_DEC))
207 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_TX, i);
208 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
209 key, (void *)&val, ADF_DEC))
213 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_TX, i);
214 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
215 key, (void *)&val, ADF_DEC))
219 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_RX, i);
220 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
221 key, (void *)&val, ADF_DEC))
225 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_RX, i);
226 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
227 key, (void *)&val, ADF_DEC))
230 val = ADF_COALESCING_DEF_TIME;
231 snprintf(key, sizeof(key), ADF_ETRMGR_COALESCE_TIMER_FORMAT, i);
232 if (adf_cfg_add_key_value_param(accel_dev, "Accelerator0",
233 key, (void *)&val, ADF_DEC))
238 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
239 ADF_NUM_CY, (void *)&val, ADF_DEC))
242 set_bit(ADF_STATUS_CONFIGURED, &accel_dev->status);
245 dev_err(&GET_DEV(accel_dev), "Failed to start QAT accel dev\n");
248 EXPORT_SYMBOL_GPL(qat_crypto_dev_config);
250 static int qat_crypto_create_instances(struct adf_accel_dev *accel_dev)
254 unsigned long num_inst, num_msg_sym, num_msg_asym;
256 struct qat_crypto_instance *inst;
257 char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
258 char val[ADF_CFG_MAX_VAL_LEN_IN_BYTES];
260 INIT_LIST_HEAD(&accel_dev->crypto_list);
261 strlcpy(key, ADF_NUM_CY, sizeof(key));
262 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
265 if (kstrtoul(val, 0, &num_inst))
268 for (i = 0; i < num_inst; i++) {
269 inst = kzalloc_node(sizeof(*inst), GFP_KERNEL,
270 dev_to_node(&GET_DEV(accel_dev)));
274 list_add_tail(&inst->list, &accel_dev->crypto_list);
276 atomic_set(&inst->refctr, 0);
277 inst->accel_dev = accel_dev;
278 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_BANK_NUM, i);
279 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
282 if (kstrtoul(val, 10, &bank))
284 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_SIZE, i);
285 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
288 if (kstrtoul(val, 10, &num_msg_sym))
291 num_msg_sym = num_msg_sym >> 1;
293 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, i);
294 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
297 if (kstrtoul(val, 10, &num_msg_asym))
299 num_msg_asym = num_msg_asym >> 1;
301 msg_size = ICP_QAT_FW_REQ_DEFAULT_SZ;
302 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_TX, i);
303 if (adf_create_ring(accel_dev, SEC, bank, num_msg_sym,
304 msg_size, key, NULL, 0, &inst->sym_tx))
307 msg_size = msg_size >> 1;
308 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_TX, i);
309 if (adf_create_ring(accel_dev, SEC, bank, num_msg_asym,
310 msg_size, key, NULL, 0, &inst->pke_tx))
313 msg_size = ICP_QAT_FW_RESP_DEFAULT_SZ;
314 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_RX, i);
315 if (adf_create_ring(accel_dev, SEC, bank, num_msg_sym,
316 msg_size, key, qat_alg_callback, 0,
320 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_RX, i);
321 if (adf_create_ring(accel_dev, SEC, bank, num_msg_asym,
322 msg_size, key, qat_alg_asym_callback, 0,
328 qat_crypto_free_instances(accel_dev);
332 static int qat_crypto_init(struct adf_accel_dev *accel_dev)
334 if (qat_crypto_create_instances(accel_dev))
340 static int qat_crypto_shutdown(struct adf_accel_dev *accel_dev)
342 return qat_crypto_free_instances(accel_dev);
345 static int qat_crypto_event_handler(struct adf_accel_dev *accel_dev,
346 enum adf_event event)
352 ret = qat_crypto_init(accel_dev);
354 case ADF_EVENT_SHUTDOWN:
355 ret = qat_crypto_shutdown(accel_dev);
357 case ADF_EVENT_RESTARTING:
358 case ADF_EVENT_RESTARTED:
359 case ADF_EVENT_START:
367 int qat_crypto_register(void)
369 memset(&qat_crypto, 0, sizeof(qat_crypto));
370 qat_crypto.event_hld = qat_crypto_event_handler;
371 qat_crypto.name = "qat_crypto";
372 return adf_service_register(&qat_crypto);
375 int qat_crypto_unregister(void)
377 return adf_service_unregister(&qat_crypto);