Merge tag 'dma-mapping-5.14' of git://git.infradead.org/users/hch/dma-mapping
[linux-2.6-microblaze.git] / drivers / crypto / qat / qat_common / adf_init.c
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"
8 #include "adf_cfg.h"
9 #include "adf_common_drv.h"
10
11 static LIST_HEAD(service_table);
12 static DEFINE_MUTEX(service_lock);
13
14 static void adf_service_add(struct service_hndl *service)
15 {
16         mutex_lock(&service_lock);
17         list_add(&service->list, &service_table);
18         mutex_unlock(&service_lock);
19 }
20
21 int adf_service_register(struct service_hndl *service)
22 {
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);
26         return 0;
27 }
28
29 static void adf_service_remove(struct service_hndl *service)
30 {
31         mutex_lock(&service_lock);
32         list_del(&service->list);
33         mutex_unlock(&service_lock);
34 }
35
36 int adf_service_unregister(struct service_hndl *service)
37 {
38         int i;
39
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");
43                         return -EFAULT;
44                 }
45         }
46         adf_service_remove(service);
47         return 0;
48 }
49
50 /**
51  * adf_dev_init() - Init data structures and services for the given accel device
52  * @accel_dev: Pointer to acceleration device.
53  *
54  * Initialize the ring data structures and the admin comms and arbitration
55  * services.
56  *
57  * Return: 0 on success, error code otherwise.
58  */
59 int adf_dev_init(struct adf_accel_dev *accel_dev)
60 {
61         struct service_hndl *service;
62         struct list_head *list_itr;
63         struct adf_hw_device_data *hw_data = accel_dev->hw_device;
64
65         if (!hw_data) {
66                 dev_err(&GET_DEV(accel_dev),
67                         "Failed to init device - hw_data not set\n");
68                 return -EFAULT;
69         }
70
71         if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status)) {
72                 dev_err(&GET_DEV(accel_dev), "Device not configured\n");
73                 return -EFAULT;
74         }
75
76         if (adf_init_etr_data(accel_dev)) {
77                 dev_err(&GET_DEV(accel_dev), "Failed initialize etr\n");
78                 return -EFAULT;
79         }
80
81         if (hw_data->init_admin_comms && hw_data->init_admin_comms(accel_dev)) {
82                 dev_err(&GET_DEV(accel_dev), "Failed initialize admin comms\n");
83                 return -EFAULT;
84         }
85
86         if (hw_data->init_arb && hw_data->init_arb(accel_dev)) {
87                 dev_err(&GET_DEV(accel_dev), "Failed initialize hw arbiter\n");
88                 return -EFAULT;
89         }
90
91         hw_data->enable_ints(accel_dev);
92
93         if (adf_ae_init(accel_dev)) {
94                 dev_err(&GET_DEV(accel_dev),
95                         "Failed to initialise Acceleration Engine\n");
96                 return -EFAULT;
97         }
98         set_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status);
99
100         if (adf_ae_fw_load(accel_dev)) {
101                 dev_err(&GET_DEV(accel_dev),
102                         "Failed to load acceleration FW\n");
103                 return -EFAULT;
104         }
105         set_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
106
107         if (hw_data->alloc_irq(accel_dev)) {
108                 dev_err(&GET_DEV(accel_dev), "Failed to allocate interrupts\n");
109                 return -EFAULT;
110         }
111         set_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
112
113         /*
114          * Subservice initialisation is divided into two stages: init and start.
115          * This is to facilitate any ordering dependencies between services
116          * prior to starting any of the accelerators.
117          */
118         list_for_each(list_itr, &service_table) {
119                 service = list_entry(list_itr, struct service_hndl, list);
120                 if (service->event_hld(accel_dev, ADF_EVENT_INIT)) {
121                         dev_err(&GET_DEV(accel_dev),
122                                 "Failed to initialise service %s\n",
123                                 service->name);
124                         return -EFAULT;
125                 }
126                 set_bit(accel_dev->accel_id, service->init_status);
127         }
128
129         hw_data->enable_error_correction(accel_dev);
130         hw_data->enable_vf2pf_comms(accel_dev);
131
132         return 0;
133 }
134 EXPORT_SYMBOL_GPL(adf_dev_init);
135
136 /**
137  * adf_dev_start() - Start acceleration service for the given accel device
138  * @accel_dev:    Pointer to acceleration device.
139  *
140  * Function notifies all the registered services that the acceleration device
141  * is ready to be used.
142  * To be used by QAT device specific drivers.
143  *
144  * Return: 0 on success, error code otherwise.
145  */
146 int adf_dev_start(struct adf_accel_dev *accel_dev)
147 {
148         struct adf_hw_device_data *hw_data = accel_dev->hw_device;
149         struct service_hndl *service;
150         struct list_head *list_itr;
151
152         set_bit(ADF_STATUS_STARTING, &accel_dev->status);
153
154         if (adf_ae_start(accel_dev)) {
155                 dev_err(&GET_DEV(accel_dev), "AE Start Failed\n");
156                 return -EFAULT;
157         }
158         set_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
159
160         if (hw_data->send_admin_init(accel_dev)) {
161                 dev_err(&GET_DEV(accel_dev), "Failed to send init message\n");
162                 return -EFAULT;
163         }
164
165         /* Set ssm watch dog timer */
166         if (hw_data->set_ssm_wdtimer)
167                 hw_data->set_ssm_wdtimer(accel_dev);
168
169         list_for_each(list_itr, &service_table) {
170                 service = list_entry(list_itr, struct service_hndl, list);
171                 if (service->event_hld(accel_dev, ADF_EVENT_START)) {
172                         dev_err(&GET_DEV(accel_dev),
173                                 "Failed to start service %s\n",
174                                 service->name);
175                         return -EFAULT;
176                 }
177                 set_bit(accel_dev->accel_id, service->start_status);
178         }
179
180         clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
181         set_bit(ADF_STATUS_STARTED, &accel_dev->status);
182
183         if (!list_empty(&accel_dev->crypto_list) &&
184             (qat_algs_register() || qat_asym_algs_register())) {
185                 dev_err(&GET_DEV(accel_dev),
186                         "Failed to register crypto algs\n");
187                 set_bit(ADF_STATUS_STARTING, &accel_dev->status);
188                 clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
189                 return -EFAULT;
190         }
191         return 0;
192 }
193 EXPORT_SYMBOL_GPL(adf_dev_start);
194
195 /**
196  * adf_dev_stop() - Stop acceleration service for the given accel device
197  * @accel_dev:    Pointer to acceleration device.
198  *
199  * Function notifies all the registered services that the acceleration device
200  * is shuting down.
201  * To be used by QAT device specific drivers.
202  *
203  * Return: void
204  */
205 void adf_dev_stop(struct adf_accel_dev *accel_dev)
206 {
207         struct service_hndl *service;
208         struct list_head *list_itr;
209         bool wait = false;
210         int ret;
211
212         if (!adf_dev_started(accel_dev) &&
213             !test_bit(ADF_STATUS_STARTING, &accel_dev->status))
214                 return;
215
216         clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
217         clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
218
219         if (!list_empty(&accel_dev->crypto_list)) {
220                 qat_algs_unregister();
221                 qat_asym_algs_unregister();
222         }
223
224         list_for_each(list_itr, &service_table) {
225                 service = list_entry(list_itr, struct service_hndl, list);
226                 if (!test_bit(accel_dev->accel_id, service->start_status))
227                         continue;
228                 ret = service->event_hld(accel_dev, ADF_EVENT_STOP);
229                 if (!ret) {
230                         clear_bit(accel_dev->accel_id, service->start_status);
231                 } else if (ret == -EAGAIN) {
232                         wait = true;
233                         clear_bit(accel_dev->accel_id, service->start_status);
234                 }
235         }
236
237         if (wait)
238                 msleep(100);
239
240         if (test_bit(ADF_STATUS_AE_STARTED, &accel_dev->status)) {
241                 if (adf_ae_stop(accel_dev))
242                         dev_err(&GET_DEV(accel_dev), "failed to stop AE\n");
243                 else
244                         clear_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
245         }
246 }
247 EXPORT_SYMBOL_GPL(adf_dev_stop);
248
249 /**
250  * adf_dev_shutdown() - shutdown acceleration services and data strucutures
251  * @accel_dev: Pointer to acceleration device
252  *
253  * Cleanup the ring data structures and the admin comms and arbitration
254  * services.
255  */
256 void adf_dev_shutdown(struct adf_accel_dev *accel_dev)
257 {
258         struct adf_hw_device_data *hw_data = accel_dev->hw_device;
259         struct service_hndl *service;
260         struct list_head *list_itr;
261
262         if (!hw_data) {
263                 dev_err(&GET_DEV(accel_dev),
264                         "QAT: Failed to shutdown device - hw_data not set\n");
265                 return;
266         }
267
268         if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) {
269                 adf_ae_fw_release(accel_dev);
270                 clear_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
271         }
272
273         if (test_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status)) {
274                 if (adf_ae_shutdown(accel_dev))
275                         dev_err(&GET_DEV(accel_dev),
276                                 "Failed to shutdown Accel Engine\n");
277                 else
278                         clear_bit(ADF_STATUS_AE_INITIALISED,
279                                   &accel_dev->status);
280         }
281
282         list_for_each(list_itr, &service_table) {
283                 service = list_entry(list_itr, struct service_hndl, list);
284                 if (!test_bit(accel_dev->accel_id, service->init_status))
285                         continue;
286                 if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN))
287                         dev_err(&GET_DEV(accel_dev),
288                                 "Failed to shutdown service %s\n",
289                                 service->name);
290                 else
291                         clear_bit(accel_dev->accel_id, service->init_status);
292         }
293
294         hw_data->disable_iov(accel_dev);
295
296         if (test_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status)) {
297                 hw_data->free_irq(accel_dev);
298                 clear_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
299         }
300
301         /* Delete configuration only if not restarting */
302         if (!test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
303                 adf_cfg_del_all(accel_dev);
304
305         if (hw_data->exit_arb)
306                 hw_data->exit_arb(accel_dev);
307
308         if (hw_data->exit_admin_comms)
309                 hw_data->exit_admin_comms(accel_dev);
310
311         adf_cleanup_etr_data(accel_dev);
312         adf_dev_restore(accel_dev);
313 }
314 EXPORT_SYMBOL_GPL(adf_dev_shutdown);
315
316 int adf_dev_restarting_notify(struct adf_accel_dev *accel_dev)
317 {
318         struct service_hndl *service;
319         struct list_head *list_itr;
320
321         list_for_each(list_itr, &service_table) {
322                 service = list_entry(list_itr, struct service_hndl, list);
323                 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING))
324                         dev_err(&GET_DEV(accel_dev),
325                                 "Failed to restart service %s.\n",
326                                 service->name);
327         }
328         return 0;
329 }
330
331 int adf_dev_restarted_notify(struct adf_accel_dev *accel_dev)
332 {
333         struct service_hndl *service;
334         struct list_head *list_itr;
335
336         list_for_each(list_itr, &service_table) {
337                 service = list_entry(list_itr, struct service_hndl, list);
338                 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED))
339                         dev_err(&GET_DEV(accel_dev),
340                                 "Failed to restart service %s.\n",
341                                 service->name);
342         }
343         return 0;
344 }