Commit | Line | Data |
---|---|---|
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 | ||
55 | static LIST_HEAD(service_table); | |
56 | static DEFINE_MUTEX(service_lock); | |
57 | ||
58 | static 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 | */ | |
74 | int 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 | } | |
81 | EXPORT_SYMBOL_GPL(adf_service_register); | |
82 | ||
83 | static 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 | */ | |
99 | int 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 | } | |
108 | EXPORT_SYMBOL_GPL(adf_service_unregister); | |
109 | ||
110 | /** | |
22e4dda0 AB |
111 | * adf_dev_init() - Init data structures and services for the given accel device |
112 | * @accel_dev: Pointer to acceleration device. | |
d8cba25d | 113 | * |
22e4dda0 AB |
114 | * Initialize the ring data structures and the admin comms and arbitration |
115 | * services. | |
d8cba25d TS |
116 | * |
117 | * Return: 0 on success, error code othewise. | |
118 | */ | |
22e4dda0 | 119 | int adf_dev_init(struct adf_accel_dev *accel_dev) |
d8cba25d TS |
120 | { |
121 | struct service_hndl *service; | |
122 | struct list_head *list_itr; | |
123 | struct adf_hw_device_data *hw_data = accel_dev->hw_device; | |
124 | ||
22e4dda0 AB |
125 | if (!hw_data) { |
126 | dev_err(&GET_DEV(accel_dev), | |
66550304 | 127 | "Failed to init device - hw_data not set\n"); |
22e4dda0 AB |
128 | return -EFAULT; |
129 | } | |
130 | ||
d8cba25d | 131 | if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status)) { |
66550304 | 132 | dev_err(&GET_DEV(accel_dev), "Device not configured\n"); |
d8cba25d TS |
133 | return -EFAULT; |
134 | } | |
22e4dda0 AB |
135 | |
136 | if (adf_init_etr_data(accel_dev)) { | |
137 | dev_err(&GET_DEV(accel_dev), "Failed initialize etr\n"); | |
138 | return -EFAULT; | |
139 | } | |
140 | ||
141 | if (hw_data->init_admin_comms && hw_data->init_admin_comms(accel_dev)) { | |
142 | dev_err(&GET_DEV(accel_dev), "Failed initialize admin comms\n"); | |
143 | return -EFAULT; | |
144 | } | |
145 | ||
146 | if (hw_data->init_arb && hw_data->init_arb(accel_dev)) { | |
147 | dev_err(&GET_DEV(accel_dev), "Failed initialize hw arbiter\n"); | |
148 | return -EFAULT; | |
149 | } | |
150 | ||
151 | hw_data->enable_ints(accel_dev); | |
d8cba25d TS |
152 | |
153 | if (adf_ae_init(accel_dev)) { | |
66550304 AB |
154 | dev_err(&GET_DEV(accel_dev), |
155 | "Failed to initialise Acceleration Engine\n"); | |
d8cba25d TS |
156 | return -EFAULT; |
157 | } | |
158 | set_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status); | |
159 | ||
160 | if (adf_ae_fw_load(accel_dev)) { | |
66550304 AB |
161 | dev_err(&GET_DEV(accel_dev), |
162 | "Failed to load acceleration FW\n"); | |
d8cba25d TS |
163 | return -EFAULT; |
164 | } | |
165 | set_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status); | |
166 | ||
167 | if (hw_data->alloc_irq(accel_dev)) { | |
66550304 | 168 | dev_err(&GET_DEV(accel_dev), "Failed to allocate interrupts\n"); |
d8cba25d TS |
169 | return -EFAULT; |
170 | } | |
171 | set_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status); | |
172 | ||
173 | /* | |
174 | * Subservice initialisation is divided into two stages: init and start. | |
175 | * This is to facilitate any ordering dependencies between services | |
176 | * prior to starting any of the accelerators. | |
177 | */ | |
178 | list_for_each(list_itr, &service_table) { | |
179 | service = list_entry(list_itr, struct service_hndl, list); | |
180 | if (!service->admin) | |
181 | continue; | |
182 | if (service->event_hld(accel_dev, ADF_EVENT_INIT)) { | |
66550304 AB |
183 | dev_err(&GET_DEV(accel_dev), |
184 | "Failed to initialise service %s\n", | |
185 | service->name); | |
d8cba25d TS |
186 | return -EFAULT; |
187 | } | |
188 | set_bit(accel_dev->accel_id, &service->init_status); | |
189 | } | |
190 | list_for_each(list_itr, &service_table) { | |
191 | service = list_entry(list_itr, struct service_hndl, list); | |
192 | if (service->admin) | |
193 | continue; | |
194 | if (service->event_hld(accel_dev, ADF_EVENT_INIT)) { | |
66550304 AB |
195 | dev_err(&GET_DEV(accel_dev), |
196 | "Failed to initialise service %s\n", | |
197 | service->name); | |
d8cba25d TS |
198 | return -EFAULT; |
199 | } | |
200 | set_bit(accel_dev->accel_id, &service->init_status); | |
201 | } | |
202 | ||
203 | hw_data->enable_error_correction(accel_dev); | |
204 | ||
22e4dda0 AB |
205 | return 0; |
206 | } | |
207 | EXPORT_SYMBOL_GPL(adf_dev_init); | |
208 | ||
209 | /** | |
210 | * adf_dev_start() - Start acceleration service for the given accel device | |
211 | * @accel_dev: Pointer to acceleration device. | |
212 | * | |
213 | * Function notifies all the registered services that the acceleration device | |
214 | * is ready to be used. | |
215 | * To be used by QAT device specific drivers. | |
216 | * | |
217 | * Return: 0 on success, error code othewise. | |
218 | */ | |
219 | int adf_dev_start(struct adf_accel_dev *accel_dev) | |
220 | { | |
221 | struct service_hndl *service; | |
222 | struct list_head *list_itr; | |
223 | ||
224 | set_bit(ADF_STATUS_STARTING, &accel_dev->status); | |
225 | ||
d8cba25d | 226 | if (adf_ae_start(accel_dev)) { |
66550304 | 227 | dev_err(&GET_DEV(accel_dev), "AE Start Failed\n"); |
d8cba25d TS |
228 | return -EFAULT; |
229 | } | |
230 | set_bit(ADF_STATUS_AE_STARTED, &accel_dev->status); | |
231 | ||
232 | list_for_each(list_itr, &service_table) { | |
233 | service = list_entry(list_itr, struct service_hndl, list); | |
234 | if (!service->admin) | |
235 | continue; | |
236 | if (service->event_hld(accel_dev, ADF_EVENT_START)) { | |
66550304 AB |
237 | dev_err(&GET_DEV(accel_dev), |
238 | "Failed to start service %s\n", | |
239 | service->name); | |
d8cba25d TS |
240 | return -EFAULT; |
241 | } | |
242 | set_bit(accel_dev->accel_id, &service->start_status); | |
243 | } | |
244 | list_for_each(list_itr, &service_table) { | |
245 | service = list_entry(list_itr, struct service_hndl, list); | |
246 | if (service->admin) | |
247 | continue; | |
248 | if (service->event_hld(accel_dev, ADF_EVENT_START)) { | |
66550304 AB |
249 | dev_err(&GET_DEV(accel_dev), |
250 | "Failed to start service %s\n", | |
251 | service->name); | |
d8cba25d TS |
252 | return -EFAULT; |
253 | } | |
254 | set_bit(accel_dev->accel_id, &service->start_status); | |
255 | } | |
256 | ||
257 | clear_bit(ADF_STATUS_STARTING, &accel_dev->status); | |
258 | set_bit(ADF_STATUS_STARTED, &accel_dev->status); | |
259 | ||
a9905320 | 260 | if (qat_algs_register() || qat_asym_algs_register()) { |
66550304 AB |
261 | dev_err(&GET_DEV(accel_dev), |
262 | "Failed to register crypto algs\n"); | |
d8cba25d TS |
263 | set_bit(ADF_STATUS_STARTING, &accel_dev->status); |
264 | clear_bit(ADF_STATUS_STARTED, &accel_dev->status); | |
265 | return -EFAULT; | |
266 | } | |
267 | return 0; | |
268 | } | |
269 | EXPORT_SYMBOL_GPL(adf_dev_start); | |
270 | ||
271 | /** | |
272 | * adf_dev_stop() - Stop acceleration service for the given accel device | |
273 | * @accel_dev: Pointer to acceleration device. | |
274 | * | |
275 | * Function notifies all the registered services that the acceleration device | |
276 | * is shuting down. | |
277 | * To be used by QAT device specific drivers. | |
278 | * | |
279 | * Return: 0 on success, error code othewise. | |
280 | */ | |
281 | int adf_dev_stop(struct adf_accel_dev *accel_dev) | |
282 | { | |
d8cba25d TS |
283 | struct service_hndl *service; |
284 | struct list_head *list_itr; | |
53bc0251 AB |
285 | bool wait = false; |
286 | int ret; | |
d8cba25d TS |
287 | |
288 | if (!adf_dev_started(accel_dev) && | |
289 | !test_bit(ADF_STATUS_STARTING, &accel_dev->status)) { | |
290 | return 0; | |
291 | } | |
d8cba25d TS |
292 | clear_bit(ADF_STATUS_STARTING, &accel_dev->status); |
293 | clear_bit(ADF_STATUS_STARTED, &accel_dev->status); | |
294 | ||
295 | if (qat_algs_unregister()) | |
66550304 AB |
296 | dev_err(&GET_DEV(accel_dev), |
297 | "Failed to unregister crypto algs\n"); | |
d8cba25d | 298 | |
a9905320 TS |
299 | qat_asym_algs_unregister(); |
300 | ||
d8cba25d TS |
301 | list_for_each(list_itr, &service_table) { |
302 | service = list_entry(list_itr, struct service_hndl, list); | |
303 | if (service->admin) | |
304 | continue; | |
305 | if (!test_bit(accel_dev->accel_id, &service->start_status)) | |
306 | continue; | |
307 | ret = service->event_hld(accel_dev, ADF_EVENT_STOP); | |
308 | if (!ret) { | |
309 | clear_bit(accel_dev->accel_id, &service->start_status); | |
310 | } else if (ret == -EAGAIN) { | |
53bc0251 | 311 | wait = true; |
d8cba25d TS |
312 | clear_bit(accel_dev->accel_id, &service->start_status); |
313 | } | |
314 | } | |
315 | list_for_each(list_itr, &service_table) { | |
316 | service = list_entry(list_itr, struct service_hndl, list); | |
317 | if (!service->admin) | |
318 | continue; | |
319 | if (!test_bit(accel_dev->accel_id, &service->start_status)) | |
320 | continue; | |
321 | if (service->event_hld(accel_dev, ADF_EVENT_STOP)) | |
66550304 AB |
322 | dev_err(&GET_DEV(accel_dev), |
323 | "Failed to shutdown service %s\n", | |
324 | service->name); | |
d8cba25d TS |
325 | else |
326 | clear_bit(accel_dev->accel_id, &service->start_status); | |
327 | } | |
328 | ||
329 | if (wait) | |
330 | msleep(100); | |
331 | ||
fd98d692 | 332 | if (test_bit(ADF_STATUS_AE_STARTED, &accel_dev->status)) { |
d8cba25d | 333 | if (adf_ae_stop(accel_dev)) |
66550304 | 334 | dev_err(&GET_DEV(accel_dev), "failed to stop AE\n"); |
d8cba25d TS |
335 | else |
336 | clear_bit(ADF_STATUS_AE_STARTED, &accel_dev->status); | |
337 | } | |
338 | ||
22e4dda0 AB |
339 | return 0; |
340 | } | |
341 | EXPORT_SYMBOL_GPL(adf_dev_stop); | |
342 | ||
343 | /** | |
344 | * adf_dev_shutdown() - shutdown acceleration services and data strucutures | |
345 | * @accel_dev: Pointer to acceleration device | |
346 | * | |
347 | * Cleanup the ring data structures and the admin comms and arbitration | |
348 | * services. | |
349 | */ | |
350 | void adf_dev_shutdown(struct adf_accel_dev *accel_dev) | |
351 | { | |
352 | struct adf_hw_device_data *hw_data = accel_dev->hw_device; | |
353 | struct service_hndl *service; | |
354 | struct list_head *list_itr; | |
355 | ||
356 | if (!hw_data) { | |
357 | dev_err(&GET_DEV(accel_dev), | |
358 | "QAT: Failed to shutdown device - hw_data not set\n"); | |
359 | return; | |
360 | } | |
361 | ||
d8cba25d | 362 | if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) { |
b4e97050 TS |
363 | adf_ae_fw_release(accel_dev); |
364 | clear_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status); | |
d8cba25d TS |
365 | } |
366 | ||
367 | if (test_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status)) { | |
368 | if (adf_ae_shutdown(accel_dev)) | |
66550304 AB |
369 | dev_err(&GET_DEV(accel_dev), |
370 | "Failed to shutdown Accel Engine\n"); | |
d8cba25d TS |
371 | else |
372 | clear_bit(ADF_STATUS_AE_INITIALISED, | |
373 | &accel_dev->status); | |
374 | } | |
375 | ||
376 | list_for_each(list_itr, &service_table) { | |
377 | service = list_entry(list_itr, struct service_hndl, list); | |
378 | if (service->admin) | |
379 | continue; | |
380 | if (!test_bit(accel_dev->accel_id, &service->init_status)) | |
381 | continue; | |
382 | if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN)) | |
66550304 AB |
383 | dev_err(&GET_DEV(accel_dev), |
384 | "Failed to shutdown service %s\n", | |
385 | service->name); | |
d8cba25d TS |
386 | else |
387 | clear_bit(accel_dev->accel_id, &service->init_status); | |
388 | } | |
389 | list_for_each(list_itr, &service_table) { | |
390 | service = list_entry(list_itr, struct service_hndl, list); | |
391 | if (!service->admin) | |
392 | continue; | |
393 | if (!test_bit(accel_dev->accel_id, &service->init_status)) | |
394 | continue; | |
395 | if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN)) | |
66550304 AB |
396 | dev_err(&GET_DEV(accel_dev), |
397 | "Failed to shutdown service %s\n", | |
398 | service->name); | |
d8cba25d TS |
399 | else |
400 | clear_bit(accel_dev->accel_id, &service->init_status); | |
401 | } | |
402 | ||
403 | if (test_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status)) { | |
404 | hw_data->free_irq(accel_dev); | |
405 | clear_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status); | |
406 | } | |
407 | ||
408 | /* Delete configuration only if not restarting */ | |
409 | if (!test_bit(ADF_STATUS_RESTARTING, &accel_dev->status)) | |
410 | adf_cfg_del_all(accel_dev); | |
411 | ||
22e4dda0 AB |
412 | if (hw_data->exit_arb) |
413 | hw_data->exit_arb(accel_dev); | |
414 | ||
415 | if (hw_data->exit_admin_comms) | |
416 | hw_data->exit_admin_comms(accel_dev); | |
417 | ||
418 | adf_cleanup_etr_data(accel_dev); | |
d8cba25d | 419 | } |
22e4dda0 | 420 | EXPORT_SYMBOL_GPL(adf_dev_shutdown); |
d8cba25d TS |
421 | |
422 | int adf_dev_restarting_notify(struct adf_accel_dev *accel_dev) | |
423 | { | |
424 | struct service_hndl *service; | |
425 | struct list_head *list_itr; | |
426 | ||
427 | list_for_each(list_itr, &service_table) { | |
428 | service = list_entry(list_itr, struct service_hndl, list); | |
429 | if (service->admin) | |
430 | continue; | |
431 | if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING)) | |
66550304 AB |
432 | dev_err(&GET_DEV(accel_dev), |
433 | "Failed to restart service %s.\n", | |
434 | service->name); | |
d8cba25d TS |
435 | } |
436 | list_for_each(list_itr, &service_table) { | |
437 | service = list_entry(list_itr, struct service_hndl, list); | |
438 | if (!service->admin) | |
439 | continue; | |
440 | if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING)) | |
66550304 AB |
441 | dev_err(&GET_DEV(accel_dev), |
442 | "Failed to restart service %s.\n", | |
443 | service->name); | |
d8cba25d TS |
444 | } |
445 | return 0; | |
446 | } | |
447 | ||
448 | int adf_dev_restarted_notify(struct adf_accel_dev *accel_dev) | |
449 | { | |
450 | struct service_hndl *service; | |
451 | struct list_head *list_itr; | |
452 | ||
453 | list_for_each(list_itr, &service_table) { | |
454 | service = list_entry(list_itr, struct service_hndl, list); | |
455 | if (service->admin) | |
456 | continue; | |
457 | if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED)) | |
66550304 AB |
458 | dev_err(&GET_DEV(accel_dev), |
459 | "Failed to restart service %s.\n", | |
460 | service->name); | |
d8cba25d TS |
461 | } |
462 | list_for_each(list_itr, &service_table) { | |
463 | service = list_entry(list_itr, struct service_hndl, list); | |
464 | if (!service->admin) | |
465 | continue; | |
466 | if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED)) | |
66550304 AB |
467 | dev_err(&GET_DEV(accel_dev), |
468 | "Failed to restart service %s.\n", | |
469 | service->name); | |
d8cba25d TS |
470 | } |
471 | return 0; | |
472 | } |