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) &&
74 dev_err(&GET_DEV(accel_dev), "Device not configured\n");
78 if (adf_init_etr_data(accel_dev)) {
79 dev_err(&GET_DEV(accel_dev), "Failed initialize etr\n");
83 if (hw_data->init_device && hw_data->init_device(accel_dev)) {
84 dev_err(&GET_DEV(accel_dev), "Failed to initialize device\n");
88 if (hw_data->init_admin_comms && hw_data->init_admin_comms(accel_dev)) {
89 dev_err(&GET_DEV(accel_dev), "Failed initialize admin comms\n");
93 if (hw_data->init_arb && hw_data->init_arb(accel_dev)) {
94 dev_err(&GET_DEV(accel_dev), "Failed initialize hw arbiter\n");
98 if (adf_ae_init(accel_dev)) {
99 dev_err(&GET_DEV(accel_dev),
100 "Failed to initialise Acceleration Engine\n");
103 set_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status);
105 if (adf_ae_fw_load(accel_dev)) {
106 dev_err(&GET_DEV(accel_dev),
107 "Failed to load acceleration FW\n");
110 set_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
112 if (hw_data->alloc_irq(accel_dev)) {
113 dev_err(&GET_DEV(accel_dev), "Failed to allocate interrupts\n");
116 set_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
118 hw_data->enable_ints(accel_dev);
119 hw_data->enable_error_correction(accel_dev);
121 ret = hw_data->pfvf_ops.enable_comms(accel_dev);
125 if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status) &&
127 if (qat_crypto_vf_dev_config(accel_dev))
132 * Subservice initialisation is divided into two stages: init and start.
133 * This is to facilitate any ordering dependencies between services
134 * prior to starting any of the accelerators.
136 list_for_each(list_itr, &service_table) {
137 service = list_entry(list_itr, struct service_hndl, list);
138 if (service->event_hld(accel_dev, ADF_EVENT_INIT)) {
139 dev_err(&GET_DEV(accel_dev),
140 "Failed to initialise service %s\n",
144 set_bit(accel_dev->accel_id, service->init_status);
149 EXPORT_SYMBOL_GPL(adf_dev_init);
152 * adf_dev_start() - Start acceleration service for the given accel device
153 * @accel_dev: Pointer to acceleration device.
155 * Function notifies all the registered services that the acceleration device
156 * is ready to be used.
157 * To be used by QAT device specific drivers.
159 * Return: 0 on success, error code otherwise.
161 int adf_dev_start(struct adf_accel_dev *accel_dev)
163 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
164 struct service_hndl *service;
165 struct list_head *list_itr;
167 set_bit(ADF_STATUS_STARTING, &accel_dev->status);
169 if (adf_ae_start(accel_dev)) {
170 dev_err(&GET_DEV(accel_dev), "AE Start Failed\n");
173 set_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
175 if (hw_data->send_admin_init(accel_dev)) {
176 dev_err(&GET_DEV(accel_dev), "Failed to send init message\n");
180 /* Set ssm watch dog timer */
181 if (hw_data->set_ssm_wdtimer)
182 hw_data->set_ssm_wdtimer(accel_dev);
184 /* Enable Power Management */
185 if (hw_data->enable_pm && hw_data->enable_pm(accel_dev)) {
186 dev_err(&GET_DEV(accel_dev), "Failed to configure Power Management\n");
190 list_for_each(list_itr, &service_table) {
191 service = list_entry(list_itr, struct service_hndl, list);
192 if (service->event_hld(accel_dev, ADF_EVENT_START)) {
193 dev_err(&GET_DEV(accel_dev),
194 "Failed to start service %s\n",
198 set_bit(accel_dev->accel_id, service->start_status);
201 clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
202 set_bit(ADF_STATUS_STARTED, &accel_dev->status);
204 if (!list_empty(&accel_dev->crypto_list) &&
205 (qat_algs_register() || qat_asym_algs_register())) {
206 dev_err(&GET_DEV(accel_dev),
207 "Failed to register crypto algs\n");
208 set_bit(ADF_STATUS_STARTING, &accel_dev->status);
209 clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
214 EXPORT_SYMBOL_GPL(adf_dev_start);
217 * adf_dev_stop() - Stop acceleration service for the given accel device
218 * @accel_dev: Pointer to acceleration device.
220 * Function notifies all the registered services that the acceleration device
222 * To be used by QAT device specific drivers.
226 void adf_dev_stop(struct adf_accel_dev *accel_dev)
228 struct service_hndl *service;
229 struct list_head *list_itr;
233 if (!adf_dev_started(accel_dev) &&
234 !test_bit(ADF_STATUS_STARTING, &accel_dev->status))
237 clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
238 clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
240 if (!list_empty(&accel_dev->crypto_list)) {
241 qat_algs_unregister();
242 qat_asym_algs_unregister();
245 list_for_each(list_itr, &service_table) {
246 service = list_entry(list_itr, struct service_hndl, list);
247 if (!test_bit(accel_dev->accel_id, service->start_status))
249 ret = service->event_hld(accel_dev, ADF_EVENT_STOP);
251 clear_bit(accel_dev->accel_id, service->start_status);
252 } else if (ret == -EAGAIN) {
254 clear_bit(accel_dev->accel_id, service->start_status);
261 if (test_bit(ADF_STATUS_AE_STARTED, &accel_dev->status)) {
262 if (adf_ae_stop(accel_dev))
263 dev_err(&GET_DEV(accel_dev), "failed to stop AE\n");
265 clear_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
268 EXPORT_SYMBOL_GPL(adf_dev_stop);
271 * adf_dev_shutdown() - shutdown acceleration services and data strucutures
272 * @accel_dev: Pointer to acceleration device
274 * Cleanup the ring data structures and the admin comms and arbitration
277 void adf_dev_shutdown(struct adf_accel_dev *accel_dev)
279 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
280 struct service_hndl *service;
281 struct list_head *list_itr;
284 dev_err(&GET_DEV(accel_dev),
285 "QAT: Failed to shutdown device - hw_data not set\n");
289 if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) {
290 adf_ae_fw_release(accel_dev);
291 clear_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
294 if (test_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status)) {
295 if (adf_ae_shutdown(accel_dev))
296 dev_err(&GET_DEV(accel_dev),
297 "Failed to shutdown Accel Engine\n");
299 clear_bit(ADF_STATUS_AE_INITIALISED,
303 list_for_each(list_itr, &service_table) {
304 service = list_entry(list_itr, struct service_hndl, list);
305 if (!test_bit(accel_dev->accel_id, service->init_status))
307 if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN))
308 dev_err(&GET_DEV(accel_dev),
309 "Failed to shutdown service %s\n",
312 clear_bit(accel_dev->accel_id, service->init_status);
315 hw_data->disable_iov(accel_dev);
317 if (test_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status)) {
318 hw_data->free_irq(accel_dev);
319 clear_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
322 /* Delete configuration only if not restarting */
323 if (!test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
324 adf_cfg_del_all(accel_dev);
326 if (hw_data->exit_arb)
327 hw_data->exit_arb(accel_dev);
329 if (hw_data->exit_admin_comms)
330 hw_data->exit_admin_comms(accel_dev);
332 adf_cleanup_etr_data(accel_dev);
333 adf_dev_restore(accel_dev);
335 EXPORT_SYMBOL_GPL(adf_dev_shutdown);
337 int adf_dev_restarting_notify(struct adf_accel_dev *accel_dev)
339 struct service_hndl *service;
340 struct list_head *list_itr;
342 list_for_each(list_itr, &service_table) {
343 service = list_entry(list_itr, struct service_hndl, list);
344 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING))
345 dev_err(&GET_DEV(accel_dev),
346 "Failed to restart service %s.\n",
352 int adf_dev_restarted_notify(struct adf_accel_dev *accel_dev)
354 struct service_hndl *service;
355 struct list_head *list_itr;
357 list_for_each(list_itr, &service_table) {
358 service = list_entry(list_itr, struct service_hndl, list);
359 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED))
360 dev_err(&GET_DEV(accel_dev),
361 "Failed to restart service %s.\n",
367 int adf_dev_shutdown_cache_cfg(struct adf_accel_dev *accel_dev)
369 char services[ADF_CFG_MAX_VAL_LEN_IN_BYTES] = {0};
372 ret = adf_cfg_get_param_value(accel_dev, ADF_GENERAL_SEC,
373 ADF_SERVICES_ENABLED, services);
375 adf_dev_stop(accel_dev);
376 adf_dev_shutdown(accel_dev);
379 ret = adf_cfg_section_add(accel_dev, ADF_GENERAL_SEC);
383 ret = adf_cfg_add_key_value_param(accel_dev, ADF_GENERAL_SEC,
384 ADF_SERVICES_ENABLED,