1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2014 - 2020 Intel Corporation */
3 #include <linux/mutex.h>
4 #include <linux/list.h>
5 #include <linux/bitops.h>
6 #include <linux/delay.h>
7 #include "adf_accel_devices.h"
9 #include "adf_common_drv.h"
11 static LIST_HEAD(service_table);
12 static DEFINE_MUTEX(service_lock);
14 static void adf_service_add(struct service_hndl *service)
16 mutex_lock(&service_lock);
17 list_add(&service->list, &service_table);
18 mutex_unlock(&service_lock);
21 int adf_service_register(struct service_hndl *service)
23 memset(service->init_status, 0, sizeof(service->init_status));
24 memset(service->start_status, 0, sizeof(service->start_status));
25 adf_service_add(service);
29 static void adf_service_remove(struct service_hndl *service)
31 mutex_lock(&service_lock);
32 list_del(&service->list);
33 mutex_unlock(&service_lock);
36 int adf_service_unregister(struct service_hndl *service)
40 for (i = 0; i < ARRAY_SIZE(service->init_status); i++) {
41 if (service->init_status[i] || service->start_status[i]) {
42 pr_err("QAT: Could not remove active service\n");
46 adf_service_remove(service);
51 * adf_dev_init() - Init data structures and services for the given accel device
52 * @accel_dev: Pointer to acceleration device.
54 * Initialize the ring data structures and the admin comms and arbitration
57 * Return: 0 on success, error code otherwise.
59 int adf_dev_init(struct adf_accel_dev *accel_dev)
61 struct service_hndl *service;
62 struct list_head *list_itr;
63 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
67 dev_err(&GET_DEV(accel_dev),
68 "Failed to init device - hw_data not set\n");
72 if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status)) {
73 dev_err(&GET_DEV(accel_dev), "Device not configured\n");
77 if (adf_init_etr_data(accel_dev)) {
78 dev_err(&GET_DEV(accel_dev), "Failed initialize etr\n");
82 if (hw_data->init_admin_comms && hw_data->init_admin_comms(accel_dev)) {
83 dev_err(&GET_DEV(accel_dev), "Failed initialize admin comms\n");
87 if (hw_data->init_arb && hw_data->init_arb(accel_dev)) {
88 dev_err(&GET_DEV(accel_dev), "Failed initialize hw arbiter\n");
92 if (adf_ae_init(accel_dev)) {
93 dev_err(&GET_DEV(accel_dev),
94 "Failed to initialise Acceleration Engine\n");
97 set_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status);
99 if (adf_ae_fw_load(accel_dev)) {
100 dev_err(&GET_DEV(accel_dev),
101 "Failed to load acceleration FW\n");
104 set_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
106 if (hw_data->alloc_irq(accel_dev)) {
107 dev_err(&GET_DEV(accel_dev), "Failed to allocate interrupts\n");
110 set_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
112 hw_data->enable_ints(accel_dev);
113 hw_data->enable_error_correction(accel_dev);
115 ret = hw_data->enable_pfvf_comms(accel_dev);
120 * Subservice initialisation is divided into two stages: init and start.
121 * This is to facilitate any ordering dependencies between services
122 * prior to starting any of the accelerators.
124 list_for_each(list_itr, &service_table) {
125 service = list_entry(list_itr, struct service_hndl, list);
126 if (service->event_hld(accel_dev, ADF_EVENT_INIT)) {
127 dev_err(&GET_DEV(accel_dev),
128 "Failed to initialise service %s\n",
132 set_bit(accel_dev->accel_id, service->init_status);
137 EXPORT_SYMBOL_GPL(adf_dev_init);
140 * adf_dev_start() - Start acceleration service for the given accel device
141 * @accel_dev: Pointer to acceleration device.
143 * Function notifies all the registered services that the acceleration device
144 * is ready to be used.
145 * To be used by QAT device specific drivers.
147 * Return: 0 on success, error code otherwise.
149 int adf_dev_start(struct adf_accel_dev *accel_dev)
151 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
152 struct service_hndl *service;
153 struct list_head *list_itr;
155 set_bit(ADF_STATUS_STARTING, &accel_dev->status);
157 if (adf_ae_start(accel_dev)) {
158 dev_err(&GET_DEV(accel_dev), "AE Start Failed\n");
161 set_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
163 if (hw_data->send_admin_init(accel_dev)) {
164 dev_err(&GET_DEV(accel_dev), "Failed to send init message\n");
168 /* Set ssm watch dog timer */
169 if (hw_data->set_ssm_wdtimer)
170 hw_data->set_ssm_wdtimer(accel_dev);
172 list_for_each(list_itr, &service_table) {
173 service = list_entry(list_itr, struct service_hndl, list);
174 if (service->event_hld(accel_dev, ADF_EVENT_START)) {
175 dev_err(&GET_DEV(accel_dev),
176 "Failed to start service %s\n",
180 set_bit(accel_dev->accel_id, service->start_status);
183 clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
184 set_bit(ADF_STATUS_STARTED, &accel_dev->status);
186 if (!list_empty(&accel_dev->crypto_list) &&
187 (qat_algs_register() || qat_asym_algs_register())) {
188 dev_err(&GET_DEV(accel_dev),
189 "Failed to register crypto algs\n");
190 set_bit(ADF_STATUS_STARTING, &accel_dev->status);
191 clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
196 EXPORT_SYMBOL_GPL(adf_dev_start);
199 * adf_dev_stop() - Stop acceleration service for the given accel device
200 * @accel_dev: Pointer to acceleration device.
202 * Function notifies all the registered services that the acceleration device
204 * To be used by QAT device specific drivers.
208 void adf_dev_stop(struct adf_accel_dev *accel_dev)
210 struct service_hndl *service;
211 struct list_head *list_itr;
215 if (!adf_dev_started(accel_dev) &&
216 !test_bit(ADF_STATUS_STARTING, &accel_dev->status))
219 clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
220 clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
222 if (!list_empty(&accel_dev->crypto_list)) {
223 qat_algs_unregister();
224 qat_asym_algs_unregister();
227 list_for_each(list_itr, &service_table) {
228 service = list_entry(list_itr, struct service_hndl, list);
229 if (!test_bit(accel_dev->accel_id, service->start_status))
231 ret = service->event_hld(accel_dev, ADF_EVENT_STOP);
233 clear_bit(accel_dev->accel_id, service->start_status);
234 } else if (ret == -EAGAIN) {
236 clear_bit(accel_dev->accel_id, service->start_status);
243 if (test_bit(ADF_STATUS_AE_STARTED, &accel_dev->status)) {
244 if (adf_ae_stop(accel_dev))
245 dev_err(&GET_DEV(accel_dev), "failed to stop AE\n");
247 clear_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
250 EXPORT_SYMBOL_GPL(adf_dev_stop);
253 * adf_dev_shutdown() - shutdown acceleration services and data strucutures
254 * @accel_dev: Pointer to acceleration device
256 * Cleanup the ring data structures and the admin comms and arbitration
259 void adf_dev_shutdown(struct adf_accel_dev *accel_dev)
261 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
262 struct service_hndl *service;
263 struct list_head *list_itr;
266 dev_err(&GET_DEV(accel_dev),
267 "QAT: Failed to shutdown device - hw_data not set\n");
271 if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) {
272 adf_ae_fw_release(accel_dev);
273 clear_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
276 if (test_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status)) {
277 if (adf_ae_shutdown(accel_dev))
278 dev_err(&GET_DEV(accel_dev),
279 "Failed to shutdown Accel Engine\n");
281 clear_bit(ADF_STATUS_AE_INITIALISED,
285 list_for_each(list_itr, &service_table) {
286 service = list_entry(list_itr, struct service_hndl, list);
287 if (!test_bit(accel_dev->accel_id, service->init_status))
289 if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN))
290 dev_err(&GET_DEV(accel_dev),
291 "Failed to shutdown service %s\n",
294 clear_bit(accel_dev->accel_id, service->init_status);
297 hw_data->disable_iov(accel_dev);
299 if (test_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status)) {
300 hw_data->free_irq(accel_dev);
301 clear_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
304 /* Delete configuration only if not restarting */
305 if (!test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
306 adf_cfg_del_all(accel_dev);
308 if (hw_data->exit_arb)
309 hw_data->exit_arb(accel_dev);
311 if (hw_data->exit_admin_comms)
312 hw_data->exit_admin_comms(accel_dev);
314 adf_cleanup_etr_data(accel_dev);
315 adf_dev_restore(accel_dev);
317 EXPORT_SYMBOL_GPL(adf_dev_shutdown);
319 int adf_dev_restarting_notify(struct adf_accel_dev *accel_dev)
321 struct service_hndl *service;
322 struct list_head *list_itr;
324 list_for_each(list_itr, &service_table) {
325 service = list_entry(list_itr, struct service_hndl, list);
326 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING))
327 dev_err(&GET_DEV(accel_dev),
328 "Failed to restart service %s.\n",
334 int adf_dev_restarted_notify(struct adf_accel_dev *accel_dev)
336 struct service_hndl *service;
337 struct list_head *list_itr;
339 list_for_each(list_itr, &service_table) {
340 service = list_entry(list_itr, struct service_hndl, list);
341 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED))
342 dev_err(&GET_DEV(accel_dev),
343 "Failed to restart service %s.\n",