GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / crypto / qat / qat_common / qat_crypto.c
1 /*
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.
4
5   GPL LICENSE SUMMARY
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.
10
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.
15
16   Contact Information:
17   qat-linux@intel.com
18
19   BSD LICENSE
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
23   are met:
24
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
30       distribution.
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.
34
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.
46 */
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"
53 #include "adf_cfg.h"
54 #include "adf_cfg_strings.h"
55 #include "qat_crypto.h"
56 #include "icp_qat_fw.h"
57
58 #define SEC ADF_KERNEL_SEC
59
60 static struct service_hndl qat_crypto;
61
62 void qat_crypto_put_instance(struct qat_crypto_instance *inst)
63 {
64         atomic_dec(&inst->refctr);
65         adf_dev_put(inst->accel_dev);
66 }
67
68 static int qat_crypto_free_instances(struct adf_accel_dev *accel_dev)
69 {
70         struct qat_crypto_instance *inst, *tmp;
71         int i;
72
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);
76
77                 if (inst->sym_tx)
78                         adf_remove_ring(inst->sym_tx);
79
80                 if (inst->sym_rx)
81                         adf_remove_ring(inst->sym_rx);
82
83                 if (inst->pke_tx)
84                         adf_remove_ring(inst->pke_tx);
85
86                 if (inst->pke_rx)
87                         adf_remove_ring(inst->pke_rx);
88
89                 list_del(&inst->list);
90                 kfree(inst);
91         }
92         return 0;
93 }
94
95 struct qat_crypto_instance *qat_crypto_get_instance_node(int node)
96 {
97         struct adf_accel_dev *accel_dev = NULL, *tmp_dev;
98         struct qat_crypto_instance *inst = NULL, *tmp_inst;
99         unsigned long best = ~0;
100
101         list_for_each_entry(tmp_dev, adf_devmgr_get_head(), list) {
102                 unsigned long ctr;
103
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);
109                         if (best > ctr) {
110                                 accel_dev = tmp_dev;
111                                 best = ctr;
112                         }
113                 }
114         }
115
116         if (!accel_dev) {
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)) {
122                                 accel_dev = tmp_dev;
123                                 break;
124                         }
125                 }
126         }
127
128         if (!accel_dev)
129                 return NULL;
130
131         best = ~0;
132         list_for_each_entry(tmp_inst, &accel_dev->crypto_list, list) {
133                 unsigned long ctr;
134
135                 ctr = atomic_read(&tmp_inst->refctr);
136                 if (best > ctr) {
137                         inst = tmp_inst;
138                         best = ctr;
139                 }
140         }
141         if (inst) {
142                 if (adf_dev_get(accel_dev)) {
143                         dev_err(&GET_DEV(accel_dev), "Could not increment dev refctr\n");
144                         return NULL;
145                 }
146                 atomic_inc(&inst->refctr);
147         }
148         return inst;
149 }
150
151 /**
152  * qat_crypto_dev_config() - create dev config required to create crypto inst.
153  *
154  * @accel_dev: Pointer to acceleration device.
155  *
156  * Function creates device configuration required to create crypto instances
157  *
158  * Return: 0 on success, error code otherwise.
159  */
160 int qat_crypto_dev_config(struct adf_accel_dev *accel_dev)
161 {
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];
166         int i;
167         unsigned long val;
168
169         if (adf_cfg_section_add(accel_dev, ADF_KERNEL_SEC))
170                 goto err;
171         if (adf_cfg_section_add(accel_dev, "Accelerator0"))
172                 goto err;
173
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
178          */
179         instances = 0;
180
181         for (i = 0; i < instances; i++) {
182                 val = 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))
186                         goto err;
187
188                 snprintf(key, sizeof(key), ADF_CY "%d" ADF_ETRMGR_CORE_AFFINITY,
189                          i);
190                 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
191                                                 key, (void *)&val, ADF_DEC))
192                         goto err;
193
194                 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, i);
195                 val = 128;
196                 if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
197                                                 key, (void *)&val, ADF_DEC))
198                         goto err;
199
200                 val = 512;
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))
204                         goto err;
205
206                 val = 0;
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))
210                         goto err;
211
212                 val = 2;
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))
216                         goto err;
217
218                 val = 8;
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))
222                         goto err;
223
224                 val = 10;
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))
228                         goto err;
229
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))
234                         goto err;
235         }
236
237         val = i;
238         if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
239                                         ADF_NUM_CY, (void *)&val, ADF_DEC))
240                 goto err;
241
242         set_bit(ADF_STATUS_CONFIGURED, &accel_dev->status);
243         return 0;
244 err:
245         dev_err(&GET_DEV(accel_dev), "Failed to start QAT accel dev\n");
246         return -EINVAL;
247 }
248 EXPORT_SYMBOL_GPL(qat_crypto_dev_config);
249
250 static int qat_crypto_create_instances(struct adf_accel_dev *accel_dev)
251 {
252         int i;
253         unsigned long bank;
254         unsigned long num_inst, num_msg_sym, num_msg_asym;
255         int msg_size;
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];
259
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))
263                 return -EFAULT;
264
265         if (kstrtoul(val, 0, &num_inst))
266                 return -EFAULT;
267
268         for (i = 0; i < num_inst; i++) {
269                 inst = kzalloc_node(sizeof(*inst), GFP_KERNEL,
270                                     dev_to_node(&GET_DEV(accel_dev)));
271                 if (!inst)
272                         goto err;
273
274                 list_add_tail(&inst->list, &accel_dev->crypto_list);
275                 inst->id = i;
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))
280                         goto err;
281
282                 if (kstrtoul(val, 10, &bank))
283                         goto err;
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))
286                         goto err;
287
288                 if (kstrtoul(val, 10, &num_msg_sym))
289                         goto err;
290
291                 num_msg_sym = num_msg_sym >> 1;
292
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))
295                         goto err;
296
297                 if (kstrtoul(val, 10, &num_msg_asym))
298                         goto err;
299                 num_msg_asym = num_msg_asym >> 1;
300
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))
305                         goto err;
306
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))
311                         goto err;
312
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,
317                                     &inst->sym_rx))
318                         goto err;
319
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,
323                                     &inst->pke_rx))
324                         goto err;
325         }
326         return 0;
327 err:
328         qat_crypto_free_instances(accel_dev);
329         return -ENOMEM;
330 }
331
332 static int qat_crypto_init(struct adf_accel_dev *accel_dev)
333 {
334         if (qat_crypto_create_instances(accel_dev))
335                 return -EFAULT;
336
337         return 0;
338 }
339
340 static int qat_crypto_shutdown(struct adf_accel_dev *accel_dev)
341 {
342         return qat_crypto_free_instances(accel_dev);
343 }
344
345 static int qat_crypto_event_handler(struct adf_accel_dev *accel_dev,
346                                     enum adf_event event)
347 {
348         int ret;
349
350         switch (event) {
351         case ADF_EVENT_INIT:
352                 ret = qat_crypto_init(accel_dev);
353                 break;
354         case ADF_EVENT_SHUTDOWN:
355                 ret = qat_crypto_shutdown(accel_dev);
356                 break;
357         case ADF_EVENT_RESTARTING:
358         case ADF_EVENT_RESTARTED:
359         case ADF_EVENT_START:
360         case ADF_EVENT_STOP:
361         default:
362                 ret = 0;
363         }
364         return ret;
365 }
366
367 int qat_crypto_register(void)
368 {
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);
373 }
374
375 int qat_crypto_unregister(void)
376 {
377         return adf_service_unregister(&qat_crypto);
378 }