crypto: qat - Intel(R) QAT driver framework
[deliverable/linux.git] / drivers / crypto / qat / qat_common / adf_init.c
CommitLineData
d8cba25d
TS
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/mutex.h>
48#include <linux/list.h>
49#include <linux/bitops.h>
50#include <linux/delay.h>
51#include "adf_accel_devices.h"
52#include "adf_cfg.h"
53#include "adf_common_drv.h"
54
55static LIST_HEAD(service_table);
56static DEFINE_MUTEX(service_lock);
57
58static void adf_service_add(struct service_hndl *service)
59{
60 mutex_lock(&service_lock);
61 list_add(&service->list, &service_table);
62 mutex_unlock(&service_lock);
63}
64
65/**
66 * adf_service_register() - Register acceleration service in the accel framework
67 * @service: Pointer to the service
68 *
69 * Function adds the acceleration service to the acceleration framework.
70 * To be used by QAT device specific drivers.
71 *
72 * Return: 0 on success, error code othewise.
73 */
74int adf_service_register(struct service_hndl *service)
75{
76 service->init_status = 0;
77 service->start_status = 0;
78 adf_service_add(service);
79 return 0;
80}
81EXPORT_SYMBOL_GPL(adf_service_register);
82
83static void adf_service_remove(struct service_hndl *service)
84{
85 mutex_lock(&service_lock);
86 list_del(&service->list);
87 mutex_unlock(&service_lock);
88}
89
90/**
91 * adf_service_unregister() - Unregister acceleration service from the framework
92 * @service: Pointer to the service
93 *
94 * Function remove the acceleration service from the acceleration framework.
95 * To be used by QAT device specific drivers.
96 *
97 * Return: 0 on success, error code othewise.
98 */
99int adf_service_unregister(struct service_hndl *service)
100{
101 if (service->init_status || service->start_status) {
102 pr_err("QAT: Could not remove active service\n");
103 return -EFAULT;
104 }
105 adf_service_remove(service);
106 return 0;
107}
108EXPORT_SYMBOL_GPL(adf_service_unregister);
109
110/**
111 * adf_dev_start() - Start acceleration service for the given accel device
112 * @accel_dev: Pointer to acceleration device.
113 *
114 * Function notifies all the registered services that the acceleration device
115 * is ready to be used.
116 * To be used by QAT device specific drivers.
117 *
118 * Return: 0 on success, error code othewise.
119 */
120int adf_dev_start(struct adf_accel_dev *accel_dev)
121{
122 struct service_hndl *service;
123 struct list_head *list_itr;
124 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
125
126 if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status)) {
127 pr_info("QAT: Device not configured\n");
128 return -EFAULT;
129 }
130 set_bit(ADF_STATUS_STARTING, &accel_dev->status);
131
132 if (adf_ae_init(accel_dev)) {
133 pr_err("QAT: Failed to initialise Acceleration Engine\n");
134 return -EFAULT;
135 }
136 set_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status);
137
138 if (adf_ae_fw_load(accel_dev)) {
139 pr_err("Failed to load acceleration FW\n");
140 adf_ae_fw_release(accel_dev);
141 return -EFAULT;
142 }
143 set_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
144
145 if (hw_data->alloc_irq(accel_dev)) {
146 pr_err("QAT: Failed to allocate interrupts\n");
147 return -EFAULT;
148 }
149 set_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
150
151 /*
152 * Subservice initialisation is divided into two stages: init and start.
153 * This is to facilitate any ordering dependencies between services
154 * prior to starting any of the accelerators.
155 */
156 list_for_each(list_itr, &service_table) {
157 service = list_entry(list_itr, struct service_hndl, list);
158 if (!service->admin)
159 continue;
160 if (service->event_hld(accel_dev, ADF_EVENT_INIT)) {
161 pr_err("QAT: Failed to initialise service %s\n",
162 service->name);
163 return -EFAULT;
164 }
165 set_bit(accel_dev->accel_id, &service->init_status);
166 }
167 list_for_each(list_itr, &service_table) {
168 service = list_entry(list_itr, struct service_hndl, list);
169 if (service->admin)
170 continue;
171 if (service->event_hld(accel_dev, ADF_EVENT_INIT)) {
172 pr_err("QAT: Failed to initialise service %s\n",
173 service->name);
174 return -EFAULT;
175 }
176 set_bit(accel_dev->accel_id, &service->init_status);
177 }
178
179 hw_data->enable_error_correction(accel_dev);
180
181 if (adf_ae_start(accel_dev)) {
182 pr_err("QAT: AE Start Failed\n");
183 return -EFAULT;
184 }
185 set_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
186
187 list_for_each(list_itr, &service_table) {
188 service = list_entry(list_itr, struct service_hndl, list);
189 if (!service->admin)
190 continue;
191 if (service->event_hld(accel_dev, ADF_EVENT_START)) {
192 pr_err("QAT: Failed to start service %s\n",
193 service->name);
194 return -EFAULT;
195 }
196 set_bit(accel_dev->accel_id, &service->start_status);
197 }
198 list_for_each(list_itr, &service_table) {
199 service = list_entry(list_itr, struct service_hndl, list);
200 if (service->admin)
201 continue;
202 if (service->event_hld(accel_dev, ADF_EVENT_START)) {
203 pr_err("QAT: Failed to start service %s\n",
204 service->name);
205 return -EFAULT;
206 }
207 set_bit(accel_dev->accel_id, &service->start_status);
208 }
209
210 clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
211 set_bit(ADF_STATUS_STARTED, &accel_dev->status);
212
213 if (qat_algs_register()) {
214 pr_err("QAT: Failed to register crypto algs\n");
215 set_bit(ADF_STATUS_STARTING, &accel_dev->status);
216 clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
217 return -EFAULT;
218 }
219 return 0;
220}
221EXPORT_SYMBOL_GPL(adf_dev_start);
222
223/**
224 * adf_dev_stop() - Stop acceleration service for the given accel device
225 * @accel_dev: Pointer to acceleration device.
226 *
227 * Function notifies all the registered services that the acceleration device
228 * is shuting down.
229 * To be used by QAT device specific drivers.
230 *
231 * Return: 0 on success, error code othewise.
232 */
233int adf_dev_stop(struct adf_accel_dev *accel_dev)
234{
235 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
236 struct service_hndl *service;
237 struct list_head *list_itr;
238 int ret, wait = 0;
239
240 if (!adf_dev_started(accel_dev) &&
241 !test_bit(ADF_STATUS_STARTING, &accel_dev->status)) {
242 return 0;
243 }
244 clear_bit(ADF_STATUS_CONFIGURED, &accel_dev->status);
245 clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
246 clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
247
248 if (qat_algs_unregister())
249 pr_err("QAT: Failed to unregister crypto algs\n");
250
251 list_for_each(list_itr, &service_table) {
252 service = list_entry(list_itr, struct service_hndl, list);
253 if (service->admin)
254 continue;
255 if (!test_bit(accel_dev->accel_id, &service->start_status))
256 continue;
257 ret = service->event_hld(accel_dev, ADF_EVENT_STOP);
258 if (!ret) {
259 clear_bit(accel_dev->accel_id, &service->start_status);
260 } else if (ret == -EAGAIN) {
261 wait = 1;
262 clear_bit(accel_dev->accel_id, &service->start_status);
263 }
264 }
265 list_for_each(list_itr, &service_table) {
266 service = list_entry(list_itr, struct service_hndl, list);
267 if (!service->admin)
268 continue;
269 if (!test_bit(accel_dev->accel_id, &service->start_status))
270 continue;
271 if (service->event_hld(accel_dev, ADF_EVENT_STOP))
272 pr_err("QAT: Failed to shutdown service %s\n",
273 service->name);
274 else
275 clear_bit(accel_dev->accel_id, &service->start_status);
276 }
277
278 if (wait)
279 msleep(100);
280
281 if (adf_dev_started(accel_dev)) {
282 if (adf_ae_stop(accel_dev))
283 pr_err("QAT: failed to stop AE\n");
284 else
285 clear_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
286 }
287
288 if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) {
289 if (adf_ae_fw_release(accel_dev))
290 pr_err("QAT: Failed to release the ucode\n");
291 else
292 clear_bit(ADF_STATUS_AE_UCODE_LOADED,
293 &accel_dev->status);
294 }
295
296 if (test_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status)) {
297 if (adf_ae_shutdown(accel_dev))
298 pr_err("QAT: Failed to shutdown Accel Engine\n");
299 else
300 clear_bit(ADF_STATUS_AE_INITIALISED,
301 &accel_dev->status);
302 }
303
304 list_for_each(list_itr, &service_table) {
305 service = list_entry(list_itr, struct service_hndl, list);
306 if (service->admin)
307 continue;
308 if (!test_bit(accel_dev->accel_id, &service->init_status))
309 continue;
310 if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN))
311 pr_err("QAT: Failed to shutdown service %s\n",
312 service->name);
313 else
314 clear_bit(accel_dev->accel_id, &service->init_status);
315 }
316 list_for_each(list_itr, &service_table) {
317 service = list_entry(list_itr, struct service_hndl, list);
318 if (!service->admin)
319 continue;
320 if (!test_bit(accel_dev->accel_id, &service->init_status))
321 continue;
322 if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN))
323 pr_err("QAT: Failed to shutdown service %s\n",
324 service->name);
325 else
326 clear_bit(accel_dev->accel_id, &service->init_status);
327 }
328
329 if (test_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status)) {
330 hw_data->free_irq(accel_dev);
331 clear_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
332 }
333
334 /* Delete configuration only if not restarting */
335 if (!test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
336 adf_cfg_del_all(accel_dev);
337
338 return 0;
339}
340EXPORT_SYMBOL_GPL(adf_dev_stop);
341
342int adf_dev_restarting_notify(struct adf_accel_dev *accel_dev)
343{
344 struct service_hndl *service;
345 struct list_head *list_itr;
346
347 list_for_each(list_itr, &service_table) {
348 service = list_entry(list_itr, struct service_hndl, list);
349 if (service->admin)
350 continue;
351 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING))
352 pr_err("QAT: Failed to restart service %s.\n",
353 service->name);
354 }
355 list_for_each(list_itr, &service_table) {
356 service = list_entry(list_itr, struct service_hndl, list);
357 if (!service->admin)
358 continue;
359 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING))
360 pr_err("QAT: Failed to restart service %s.\n",
361 service->name);
362 }
363 return 0;
364}
365
366int adf_dev_restarted_notify(struct adf_accel_dev *accel_dev)
367{
368 struct service_hndl *service;
369 struct list_head *list_itr;
370
371 list_for_each(list_itr, &service_table) {
372 service = list_entry(list_itr, struct service_hndl, list);
373 if (service->admin)
374 continue;
375 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED))
376 pr_err("QAT: Failed to restart service %s.\n",
377 service->name);
378 }
379 list_for_each(list_itr, &service_table) {
380 service = list_entry(list_itr, struct service_hndl, list);
381 if (!service->admin)
382 continue;
383 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED))
384 pr_err("QAT: Failed to restart service %s.\n",
385 service->name);
386 }
387 return 0;
388}
This page took 0.039971 seconds and 5 git commands to generate.