359ae94430b8958c16d60498f05612c1a5d93682
[deliverable/linux.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_cfg.h"
53 #include "adf_cfg_strings.h"
54 #include "qat_crypto.h"
55 #include "icp_qat_fw.h"
56
57 #define SEC ADF_KERNEL_SEC
58
59 static struct service_hndl qat_crypto;
60
61 void qat_crypto_put_instance(struct qat_crypto_instance *inst)
62 {
63 if (atomic_sub_return(1, &inst->refctr) == 0)
64 adf_dev_put(inst->accel_dev);
65 }
66
67 static int qat_crypto_free_instances(struct adf_accel_dev *accel_dev)
68 {
69 struct qat_crypto_instance *inst;
70 struct list_head *list_ptr, *tmp;
71 int i;
72
73 list_for_each_safe(list_ptr, tmp, &accel_dev->crypto_list) {
74 inst = list_entry(list_ptr, struct qat_crypto_instance, list);
75
76 for (i = 0; i < atomic_read(&inst->refctr); i++)
77 qat_crypto_put_instance(inst);
78
79 if (inst->sym_tx)
80 adf_remove_ring(inst->sym_tx);
81
82 if (inst->sym_rx)
83 adf_remove_ring(inst->sym_rx);
84
85 if (inst->pke_tx)
86 adf_remove_ring(inst->pke_tx);
87
88 if (inst->pke_rx)
89 adf_remove_ring(inst->pke_rx);
90
91 list_del(list_ptr);
92 kfree(inst);
93 }
94 return 0;
95 }
96
97 struct qat_crypto_instance *qat_crypto_get_instance_node(int node)
98 {
99 struct adf_accel_dev *accel_dev = NULL;
100 struct qat_crypto_instance *inst_best = NULL;
101 struct list_head *itr;
102 unsigned long best = ~0;
103
104 list_for_each(itr, adf_devmgr_get_head()) {
105 accel_dev = list_entry(itr, struct adf_accel_dev, list);
106 if ((node == dev_to_node(&GET_DEV(accel_dev)) ||
107 dev_to_node(&GET_DEV(accel_dev)) < 0) &&
108 adf_dev_started(accel_dev))
109 break;
110 accel_dev = NULL;
111 }
112 if (!accel_dev) {
113 pr_err("QAT: Could not find a device on node %d\n", node);
114 accel_dev = adf_devmgr_get_first();
115 }
116 if (!accel_dev || !adf_dev_started(accel_dev))
117 return NULL;
118
119 list_for_each(itr, &accel_dev->crypto_list) {
120 struct qat_crypto_instance *inst;
121 unsigned long cur;
122
123 inst = list_entry(itr, struct qat_crypto_instance, list);
124 cur = atomic_read(&inst->refctr);
125 if (best > cur) {
126 inst_best = inst;
127 best = cur;
128 }
129 }
130 if (inst_best) {
131 if (atomic_add_return(1, &inst_best->refctr) == 1) {
132 if (adf_dev_get(accel_dev)) {
133 atomic_dec(&inst_best->refctr);
134 dev_err(&GET_DEV(accel_dev),
135 "Could not increment dev refctr\n");
136 return NULL;
137 }
138 }
139 }
140 return inst_best;
141 }
142
143 static int qat_crypto_create_instances(struct adf_accel_dev *accel_dev)
144 {
145 int i;
146 unsigned long bank;
147 unsigned long num_inst, num_msg_sym, num_msg_asym;
148 int msg_size;
149 struct qat_crypto_instance *inst;
150 char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
151 char val[ADF_CFG_MAX_VAL_LEN_IN_BYTES];
152
153 INIT_LIST_HEAD(&accel_dev->crypto_list);
154 strlcpy(key, ADF_NUM_CY, sizeof(key));
155 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
156 return -EFAULT;
157
158 if (kstrtoul(val, 0, &num_inst))
159 return -EFAULT;
160
161 for (i = 0; i < num_inst; i++) {
162 inst = kzalloc_node(sizeof(*inst), GFP_KERNEL,
163 dev_to_node(&GET_DEV(accel_dev)));
164 if (!inst)
165 goto err;
166
167 list_add_tail(&inst->list, &accel_dev->crypto_list);
168 inst->id = i;
169 atomic_set(&inst->refctr, 0);
170 inst->accel_dev = accel_dev;
171 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_BANK_NUM, i);
172 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
173 goto err;
174
175 if (kstrtoul(val, 10, &bank))
176 goto err;
177 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_SIZE, i);
178 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
179 goto err;
180
181 if (kstrtoul(val, 10, &num_msg_sym))
182 goto err;
183
184 num_msg_sym = num_msg_sym >> 1;
185
186 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, i);
187 if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
188 goto err;
189
190 if (kstrtoul(val, 10, &num_msg_asym))
191 goto err;
192 num_msg_asym = num_msg_asym >> 1;
193
194 msg_size = ICP_QAT_FW_REQ_DEFAULT_SZ;
195 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_TX, i);
196 if (adf_create_ring(accel_dev, SEC, bank, num_msg_sym,
197 msg_size, key, NULL, 0, &inst->sym_tx))
198 goto err;
199
200 msg_size = msg_size >> 1;
201 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_TX, i);
202 if (adf_create_ring(accel_dev, SEC, bank, num_msg_asym,
203 msg_size, key, NULL, 0, &inst->pke_tx))
204 goto err;
205
206 msg_size = ICP_QAT_FW_RESP_DEFAULT_SZ;
207 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_RX, i);
208 if (adf_create_ring(accel_dev, SEC, bank, num_msg_sym,
209 msg_size, key, qat_alg_callback, 0,
210 &inst->sym_rx))
211 goto err;
212
213 snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_RX, i);
214 if (adf_create_ring(accel_dev, SEC, bank, num_msg_asym,
215 msg_size, key, qat_alg_asym_callback, 0,
216 &inst->pke_rx))
217 goto err;
218 }
219 return 0;
220 err:
221 qat_crypto_free_instances(accel_dev);
222 return -ENOMEM;
223 }
224
225 static int qat_crypto_init(struct adf_accel_dev *accel_dev)
226 {
227 if (qat_crypto_create_instances(accel_dev))
228 return -EFAULT;
229
230 return 0;
231 }
232
233 static int qat_crypto_shutdown(struct adf_accel_dev *accel_dev)
234 {
235 return qat_crypto_free_instances(accel_dev);
236 }
237
238 static int qat_crypto_event_handler(struct adf_accel_dev *accel_dev,
239 enum adf_event event)
240 {
241 int ret;
242
243 switch (event) {
244 case ADF_EVENT_INIT:
245 ret = qat_crypto_init(accel_dev);
246 break;
247 case ADF_EVENT_SHUTDOWN:
248 ret = qat_crypto_shutdown(accel_dev);
249 break;
250 case ADF_EVENT_RESTARTING:
251 case ADF_EVENT_RESTARTED:
252 case ADF_EVENT_START:
253 case ADF_EVENT_STOP:
254 default:
255 ret = 0;
256 }
257 return ret;
258 }
259
260 int qat_crypto_register(void)
261 {
262 memset(&qat_crypto, 0, sizeof(qat_crypto));
263 qat_crypto.event_hld = qat_crypto_event_handler;
264 qat_crypto.name = "qat_crypto";
265 return adf_service_register(&qat_crypto);
266 }
267
268 int qat_crypto_unregister(void)
269 {
270 return adf_service_unregister(&qat_crypto);
271 }
This page took 0.035978 seconds and 4 git commands to generate.