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 
adf_service_add(struct service_hndl * service)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 
adf_service_register(struct service_hndl * service)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 
adf_service_remove(struct service_hndl * service)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 
adf_service_unregister(struct service_hndl * service)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  */
adf_dev_init(struct adf_accel_dev * accel_dev)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 	int ret;
65 
66 	if (!hw_data) {
67 		dev_err(&GET_DEV(accel_dev),
68 			"Failed to init device - hw_data not set\n");
69 		return -EFAULT;
70 	}
71 
72 	if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status) &&
73 	    !accel_dev->is_vf) {
74 		dev_err(&GET_DEV(accel_dev), "Device not configured\n");
75 		return -EFAULT;
76 	}
77 
78 	if (adf_init_etr_data(accel_dev)) {
79 		dev_err(&GET_DEV(accel_dev), "Failed initialize etr\n");
80 		return -EFAULT;
81 	}
82 
83 	if (hw_data->init_device && hw_data->init_device(accel_dev)) {
84 		dev_err(&GET_DEV(accel_dev), "Failed to initialize device\n");
85 		return -EFAULT;
86 	}
87 
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");
90 		return -EFAULT;
91 	}
92 
93 	if (hw_data->init_arb && hw_data->init_arb(accel_dev)) {
94 		dev_err(&GET_DEV(accel_dev), "Failed initialize hw arbiter\n");
95 		return -EFAULT;
96 	}
97 
98 	if (adf_ae_init(accel_dev)) {
99 		dev_err(&GET_DEV(accel_dev),
100 			"Failed to initialise Acceleration Engine\n");
101 		return -EFAULT;
102 	}
103 	set_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status);
104 
105 	if (adf_ae_fw_load(accel_dev)) {
106 		dev_err(&GET_DEV(accel_dev),
107 			"Failed to load acceleration FW\n");
108 		return -EFAULT;
109 	}
110 	set_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
111 
112 	if (hw_data->alloc_irq(accel_dev)) {
113 		dev_err(&GET_DEV(accel_dev), "Failed to allocate interrupts\n");
114 		return -EFAULT;
115 	}
116 	set_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
117 
118 	hw_data->enable_ints(accel_dev);
119 	hw_data->enable_error_correction(accel_dev);
120 
121 	ret = hw_data->pfvf_ops.enable_comms(accel_dev);
122 	if (ret)
123 		return ret;
124 
125 	if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status) &&
126 	    accel_dev->is_vf) {
127 		if (qat_crypto_vf_dev_config(accel_dev))
128 			return -EFAULT;
129 	}
130 
131 	/*
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.
135 	 */
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",
141 				service->name);
142 			return -EFAULT;
143 		}
144 		set_bit(accel_dev->accel_id, service->init_status);
145 	}
146 
147 	return 0;
148 }
149 EXPORT_SYMBOL_GPL(adf_dev_init);
150 
151 /**
152  * adf_dev_start() - Start acceleration service for the given accel device
153  * @accel_dev:    Pointer to acceleration device.
154  *
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.
158  *
159  * Return: 0 on success, error code otherwise.
160  */
adf_dev_start(struct adf_accel_dev * accel_dev)161 int adf_dev_start(struct adf_accel_dev *accel_dev)
162 {
163 	struct adf_hw_device_data *hw_data = accel_dev->hw_device;
164 	struct service_hndl *service;
165 	struct list_head *list_itr;
166 
167 	set_bit(ADF_STATUS_STARTING, &accel_dev->status);
168 
169 	if (adf_ae_start(accel_dev)) {
170 		dev_err(&GET_DEV(accel_dev), "AE Start Failed\n");
171 		return -EFAULT;
172 	}
173 	set_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
174 
175 	if (hw_data->send_admin_init(accel_dev)) {
176 		dev_err(&GET_DEV(accel_dev), "Failed to send init message\n");
177 		return -EFAULT;
178 	}
179 
180 	/* Set ssm watch dog timer */
181 	if (hw_data->set_ssm_wdtimer)
182 		hw_data->set_ssm_wdtimer(accel_dev);
183 
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");
187 		return -EFAULT;
188 	}
189 
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",
195 				service->name);
196 			return -EFAULT;
197 		}
198 		set_bit(accel_dev->accel_id, service->start_status);
199 	}
200 
201 	clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
202 	set_bit(ADF_STATUS_STARTED, &accel_dev->status);
203 
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);
210 		return -EFAULT;
211 	}
212 
213 	if (!list_empty(&accel_dev->compression_list) && qat_comp_algs_register()) {
214 		dev_err(&GET_DEV(accel_dev),
215 			"Failed to register compression algs\n");
216 		set_bit(ADF_STATUS_STARTING, &accel_dev->status);
217 		clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
218 		return -EFAULT;
219 	}
220 	return 0;
221 }
222 EXPORT_SYMBOL_GPL(adf_dev_start);
223 
224 /**
225  * adf_dev_stop() - Stop acceleration service for the given accel device
226  * @accel_dev:    Pointer to acceleration device.
227  *
228  * Function notifies all the registered services that the acceleration device
229  * is shuting down.
230  * To be used by QAT device specific drivers.
231  *
232  * Return: void
233  */
adf_dev_stop(struct adf_accel_dev * accel_dev)234 void adf_dev_stop(struct adf_accel_dev *accel_dev)
235 {
236 	struct service_hndl *service;
237 	struct list_head *list_itr;
238 	bool wait = false;
239 	int ret;
240 
241 	if (!adf_dev_started(accel_dev) &&
242 	    !test_bit(ADF_STATUS_STARTING, &accel_dev->status))
243 		return;
244 
245 	clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
246 	clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
247 
248 	if (!list_empty(&accel_dev->crypto_list)) {
249 		qat_algs_unregister();
250 		qat_asym_algs_unregister();
251 	}
252 
253 	if (!list_empty(&accel_dev->compression_list))
254 		qat_comp_algs_unregister();
255 
256 	list_for_each(list_itr, &service_table) {
257 		service = list_entry(list_itr, struct service_hndl, list);
258 		if (!test_bit(accel_dev->accel_id, service->start_status))
259 			continue;
260 		ret = service->event_hld(accel_dev, ADF_EVENT_STOP);
261 		if (!ret) {
262 			clear_bit(accel_dev->accel_id, service->start_status);
263 		} else if (ret == -EAGAIN) {
264 			wait = true;
265 			clear_bit(accel_dev->accel_id, service->start_status);
266 		}
267 	}
268 
269 	if (wait)
270 		msleep(100);
271 
272 	if (test_bit(ADF_STATUS_AE_STARTED, &accel_dev->status)) {
273 		if (adf_ae_stop(accel_dev))
274 			dev_err(&GET_DEV(accel_dev), "failed to stop AE\n");
275 		else
276 			clear_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
277 	}
278 }
279 EXPORT_SYMBOL_GPL(adf_dev_stop);
280 
281 /**
282  * adf_dev_shutdown() - shutdown acceleration services and data strucutures
283  * @accel_dev: Pointer to acceleration device
284  *
285  * Cleanup the ring data structures and the admin comms and arbitration
286  * services.
287  */
adf_dev_shutdown(struct adf_accel_dev * accel_dev)288 void adf_dev_shutdown(struct adf_accel_dev *accel_dev)
289 {
290 	struct adf_hw_device_data *hw_data = accel_dev->hw_device;
291 	struct service_hndl *service;
292 	struct list_head *list_itr;
293 
294 	if (!hw_data) {
295 		dev_err(&GET_DEV(accel_dev),
296 			"QAT: Failed to shutdown device - hw_data not set\n");
297 		return;
298 	}
299 
300 	if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) {
301 		adf_ae_fw_release(accel_dev);
302 		clear_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
303 	}
304 
305 	if (test_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status)) {
306 		if (adf_ae_shutdown(accel_dev))
307 			dev_err(&GET_DEV(accel_dev),
308 				"Failed to shutdown Accel Engine\n");
309 		else
310 			clear_bit(ADF_STATUS_AE_INITIALISED,
311 				  &accel_dev->status);
312 	}
313 
314 	list_for_each(list_itr, &service_table) {
315 		service = list_entry(list_itr, struct service_hndl, list);
316 		if (!test_bit(accel_dev->accel_id, service->init_status))
317 			continue;
318 		if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN))
319 			dev_err(&GET_DEV(accel_dev),
320 				"Failed to shutdown service %s\n",
321 				service->name);
322 		else
323 			clear_bit(accel_dev->accel_id, service->init_status);
324 	}
325 
326 	hw_data->disable_iov(accel_dev);
327 
328 	if (test_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status)) {
329 		hw_data->free_irq(accel_dev);
330 		clear_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
331 	}
332 
333 	/* Delete configuration only if not restarting */
334 	if (!test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
335 		adf_cfg_del_all(accel_dev);
336 
337 	if (hw_data->exit_arb)
338 		hw_data->exit_arb(accel_dev);
339 
340 	if (hw_data->exit_admin_comms)
341 		hw_data->exit_admin_comms(accel_dev);
342 
343 	adf_cleanup_etr_data(accel_dev);
344 	adf_dev_restore(accel_dev);
345 }
346 EXPORT_SYMBOL_GPL(adf_dev_shutdown);
347 
adf_dev_restarting_notify(struct adf_accel_dev * accel_dev)348 int adf_dev_restarting_notify(struct adf_accel_dev *accel_dev)
349 {
350 	struct service_hndl *service;
351 	struct list_head *list_itr;
352 
353 	list_for_each(list_itr, &service_table) {
354 		service = list_entry(list_itr, struct service_hndl, list);
355 		if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING))
356 			dev_err(&GET_DEV(accel_dev),
357 				"Failed to restart service %s.\n",
358 				service->name);
359 	}
360 	return 0;
361 }
362 
adf_dev_restarted_notify(struct adf_accel_dev * accel_dev)363 int adf_dev_restarted_notify(struct adf_accel_dev *accel_dev)
364 {
365 	struct service_hndl *service;
366 	struct list_head *list_itr;
367 
368 	list_for_each(list_itr, &service_table) {
369 		service = list_entry(list_itr, struct service_hndl, list);
370 		if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED))
371 			dev_err(&GET_DEV(accel_dev),
372 				"Failed to restart service %s.\n",
373 				service->name);
374 	}
375 	return 0;
376 }
377 
adf_dev_shutdown_cache_cfg(struct adf_accel_dev * accel_dev)378 int adf_dev_shutdown_cache_cfg(struct adf_accel_dev *accel_dev)
379 {
380 	char services[ADF_CFG_MAX_VAL_LEN_IN_BYTES] = {0};
381 	int ret;
382 
383 	ret = adf_cfg_get_param_value(accel_dev, ADF_GENERAL_SEC,
384 				      ADF_SERVICES_ENABLED, services);
385 
386 	adf_dev_stop(accel_dev);
387 	adf_dev_shutdown(accel_dev);
388 
389 	if (!ret) {
390 		ret = adf_cfg_section_add(accel_dev, ADF_GENERAL_SEC);
391 		if (ret)
392 			return ret;
393 
394 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_GENERAL_SEC,
395 						  ADF_SERVICES_ENABLED,
396 						  services, ADF_STR);
397 		if (ret)
398 			return ret;
399 	}
400 
401 	return 0;
402 }
403