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/mutex.h>
48 #include <linux/list.h>
49 #include <linux/bitops.h>
50 #include <linux/delay.h>
51 #include "adf_accel_devices.h"
53 #include "adf_common_drv.h"
55 static LIST_HEAD(service_table);
56 static DEFINE_MUTEX(service_lock);
58 static void adf_service_add(struct service_hndl *service)
60 mutex_lock(&service_lock);
61 list_add(&service->list, &service_table);
62 mutex_unlock(&service_lock);
65 int adf_service_register(struct service_hndl *service)
67 memset(service->init_status, 0, sizeof(service->init_status));
68 memset(service->start_status, 0, sizeof(service->start_status));
69 adf_service_add(service);
73 static void adf_service_remove(struct service_hndl *service)
75 mutex_lock(&service_lock);
76 list_del(&service->list);
77 mutex_unlock(&service_lock);
80 int adf_service_unregister(struct service_hndl *service)
84 for (i = 0; i < ARRAY_SIZE(service->init_status); i++) {
85 if (service->init_status[i] || service->start_status[i]) {
86 pr_err("QAT: Could not remove active service\n");
90 adf_service_remove(service);
95 * adf_dev_init() - Init data structures and services for the given accel device
96 * @accel_dev: Pointer to acceleration device.
98 * Initialize the ring data structures and the admin comms and arbitration
101 * Return: 0 on success, error code otherwise.
103 int adf_dev_init(struct adf_accel_dev *accel_dev)
105 struct service_hndl *service;
106 struct list_head *list_itr;
107 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
111 dev_err(&GET_DEV(accel_dev),
112 "Failed to init device - hw_data not set\n");
116 if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status)) {
117 dev_err(&GET_DEV(accel_dev), "Device not configured\n");
121 if (adf_init_etr_data(accel_dev)) {
122 dev_err(&GET_DEV(accel_dev), "Failed initialize etr\n");
126 if (hw_data->init_admin_comms && hw_data->init_admin_comms(accel_dev)) {
127 dev_err(&GET_DEV(accel_dev), "Failed initialize admin comms\n");
131 if (hw_data->init_arb && hw_data->init_arb(accel_dev)) {
132 dev_err(&GET_DEV(accel_dev), "Failed initialize hw arbiter\n");
136 hw_data->enable_ints(accel_dev);
138 if (adf_ae_init(accel_dev)) {
139 dev_err(&GET_DEV(accel_dev),
140 "Failed to initialise Acceleration Engine\n");
143 set_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status);
145 if (adf_ae_fw_load(accel_dev)) {
146 dev_err(&GET_DEV(accel_dev),
147 "Failed to load acceleration FW\n");
150 set_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
152 if (hw_data->alloc_irq(accel_dev)) {
153 dev_err(&GET_DEV(accel_dev), "Failed to allocate interrupts\n");
156 set_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
159 * Subservice initialisation is divided into two stages: init and start.
160 * This is to facilitate any ordering dependencies between services
161 * prior to starting any of the accelerators.
163 list_for_each(list_itr, &service_table) {
164 service = list_entry(list_itr, struct service_hndl, list);
165 if (service->event_hld(accel_dev, ADF_EVENT_INIT)) {
166 dev_err(&GET_DEV(accel_dev),
167 "Failed to initialise service %s\n",
171 set_bit(accel_dev->accel_id, service->init_status);
174 hw_data->enable_error_correction(accel_dev);
175 ret = hw_data->enable_vf2pf_comms(accel_dev);
179 EXPORT_SYMBOL_GPL(adf_dev_init);
182 * adf_dev_start() - Start acceleration service for the given accel device
183 * @accel_dev: Pointer to acceleration device.
185 * Function notifies all the registered services that the acceleration device
186 * is ready to be used.
187 * To be used by QAT device specific drivers.
189 * Return: 0 on success, error code otherwise.
191 int adf_dev_start(struct adf_accel_dev *accel_dev)
193 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
194 struct service_hndl *service;
195 struct list_head *list_itr;
197 set_bit(ADF_STATUS_STARTING, &accel_dev->status);
199 if (adf_ae_start(accel_dev)) {
200 dev_err(&GET_DEV(accel_dev), "AE Start Failed\n");
203 set_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
205 if (hw_data->send_admin_init(accel_dev)) {
206 dev_err(&GET_DEV(accel_dev), "Failed to send init message\n");
210 list_for_each(list_itr, &service_table) {
211 service = list_entry(list_itr, struct service_hndl, list);
212 if (service->event_hld(accel_dev, ADF_EVENT_START)) {
213 dev_err(&GET_DEV(accel_dev),
214 "Failed to start service %s\n",
218 set_bit(accel_dev->accel_id, service->start_status);
221 clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
222 set_bit(ADF_STATUS_STARTED, &accel_dev->status);
224 if (!list_empty(&accel_dev->crypto_list) &&
225 (qat_algs_register() || qat_asym_algs_register())) {
226 dev_err(&GET_DEV(accel_dev),
227 "Failed to register crypto algs\n");
228 set_bit(ADF_STATUS_STARTING, &accel_dev->status);
229 clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
234 EXPORT_SYMBOL_GPL(adf_dev_start);
237 * adf_dev_stop() - Stop acceleration service for the given accel device
238 * @accel_dev: Pointer to acceleration device.
240 * Function notifies all the registered services that the acceleration device
242 * To be used by QAT device specific drivers.
246 void adf_dev_stop(struct adf_accel_dev *accel_dev)
248 struct service_hndl *service;
249 struct list_head *list_itr;
253 if (!adf_dev_started(accel_dev) &&
254 !test_bit(ADF_STATUS_STARTING, &accel_dev->status))
257 clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
258 clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
260 if (!list_empty(&accel_dev->crypto_list)) {
261 qat_algs_unregister();
262 qat_asym_algs_unregister();
265 list_for_each(list_itr, &service_table) {
266 service = list_entry(list_itr, struct service_hndl, list);
267 if (!test_bit(accel_dev->accel_id, service->start_status))
269 ret = service->event_hld(accel_dev, ADF_EVENT_STOP);
271 clear_bit(accel_dev->accel_id, service->start_status);
272 } else if (ret == -EAGAIN) {
274 clear_bit(accel_dev->accel_id, service->start_status);
281 if (test_bit(ADF_STATUS_AE_STARTED, &accel_dev->status)) {
282 if (adf_ae_stop(accel_dev))
283 dev_err(&GET_DEV(accel_dev), "failed to stop AE\n");
285 clear_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
288 EXPORT_SYMBOL_GPL(adf_dev_stop);
291 * adf_dev_shutdown() - shutdown acceleration services and data strucutures
292 * @accel_dev: Pointer to acceleration device
294 * Cleanup the ring data structures and the admin comms and arbitration
297 void adf_dev_shutdown(struct adf_accel_dev *accel_dev)
299 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
300 struct service_hndl *service;
301 struct list_head *list_itr;
304 dev_err(&GET_DEV(accel_dev),
305 "QAT: Failed to shutdown device - hw_data not set\n");
309 if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) {
310 adf_ae_fw_release(accel_dev);
311 clear_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
314 if (test_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status)) {
315 if (adf_ae_shutdown(accel_dev))
316 dev_err(&GET_DEV(accel_dev),
317 "Failed to shutdown Accel Engine\n");
319 clear_bit(ADF_STATUS_AE_INITIALISED,
323 list_for_each(list_itr, &service_table) {
324 service = list_entry(list_itr, struct service_hndl, list);
325 if (!test_bit(accel_dev->accel_id, service->init_status))
327 if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN))
328 dev_err(&GET_DEV(accel_dev),
329 "Failed to shutdown service %s\n",
332 clear_bit(accel_dev->accel_id, service->init_status);
335 hw_data->disable_iov(accel_dev);
337 if (test_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status)) {
338 hw_data->free_irq(accel_dev);
339 clear_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
342 /* Delete configuration only if not restarting */
343 if (!test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
344 adf_cfg_del_all(accel_dev);
346 if (hw_data->exit_arb)
347 hw_data->exit_arb(accel_dev);
349 if (hw_data->exit_admin_comms)
350 hw_data->exit_admin_comms(accel_dev);
352 adf_cleanup_etr_data(accel_dev);
353 adf_dev_restore(accel_dev);
355 EXPORT_SYMBOL_GPL(adf_dev_shutdown);
357 int adf_dev_restarting_notify(struct adf_accel_dev *accel_dev)
359 struct service_hndl *service;
360 struct list_head *list_itr;
362 list_for_each(list_itr, &service_table) {
363 service = list_entry(list_itr, struct service_hndl, list);
364 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING))
365 dev_err(&GET_DEV(accel_dev),
366 "Failed to restart service %s.\n",
372 int adf_dev_restarted_notify(struct adf_accel_dev *accel_dev)
374 struct service_hndl *service;
375 struct list_head *list_itr;
377 list_for_each(list_itr, &service_table) {
378 service = list_entry(list_itr, struct service_hndl, list);
379 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED))
380 dev_err(&GET_DEV(accel_dev),
381 "Failed to restart service %s.\n",