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 	return 0;
213 }
214 EXPORT_SYMBOL_GPL(adf_dev_start);
215 
216 /**
217  * adf_dev_stop() - Stop acceleration service for the given accel device
218  * @accel_dev:    Pointer to acceleration device.
219  *
220  * Function notifies all the registered services that the acceleration device
221  * is shuting down.
222  * To be used by QAT device specific drivers.
223  *
224  * Return: void
225  */
adf_dev_stop(struct adf_accel_dev * accel_dev)226 void adf_dev_stop(struct adf_accel_dev *accel_dev)
227 {
228 	struct service_hndl *service;
229 	struct list_head *list_itr;
230 	bool wait = false;
231 	int ret;
232 
233 	if (!adf_dev_started(accel_dev) &&
234 	    !test_bit(ADF_STATUS_STARTING, &accel_dev->status))
235 		return;
236 
237 	clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
238 	clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
239 
240 	if (!list_empty(&accel_dev->crypto_list)) {
241 		qat_algs_unregister();
242 		qat_asym_algs_unregister();
243 	}
244 
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))
248 			continue;
249 		ret = service->event_hld(accel_dev, ADF_EVENT_STOP);
250 		if (!ret) {
251 			clear_bit(accel_dev->accel_id, service->start_status);
252 		} else if (ret == -EAGAIN) {
253 			wait = true;
254 			clear_bit(accel_dev->accel_id, service->start_status);
255 		}
256 	}
257 
258 	if (wait)
259 		msleep(100);
260 
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");
264 		else
265 			clear_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
266 	}
267 }
268 EXPORT_SYMBOL_GPL(adf_dev_stop);
269 
270 /**
271  * adf_dev_shutdown() - shutdown acceleration services and data strucutures
272  * @accel_dev: Pointer to acceleration device
273  *
274  * Cleanup the ring data structures and the admin comms and arbitration
275  * services.
276  */
adf_dev_shutdown(struct adf_accel_dev * accel_dev)277 void adf_dev_shutdown(struct adf_accel_dev *accel_dev)
278 {
279 	struct adf_hw_device_data *hw_data = accel_dev->hw_device;
280 	struct service_hndl *service;
281 	struct list_head *list_itr;
282 
283 	if (!hw_data) {
284 		dev_err(&GET_DEV(accel_dev),
285 			"QAT: Failed to shutdown device - hw_data not set\n");
286 		return;
287 	}
288 
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);
292 	}
293 
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");
298 		else
299 			clear_bit(ADF_STATUS_AE_INITIALISED,
300 				  &accel_dev->status);
301 	}
302 
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))
306 			continue;
307 		if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN))
308 			dev_err(&GET_DEV(accel_dev),
309 				"Failed to shutdown service %s\n",
310 				service->name);
311 		else
312 			clear_bit(accel_dev->accel_id, service->init_status);
313 	}
314 
315 	hw_data->disable_iov(accel_dev);
316 
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);
320 	}
321 
322 	/* Delete configuration only if not restarting */
323 	if (!test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
324 		adf_cfg_del_all(accel_dev);
325 
326 	if (hw_data->exit_arb)
327 		hw_data->exit_arb(accel_dev);
328 
329 	if (hw_data->exit_admin_comms)
330 		hw_data->exit_admin_comms(accel_dev);
331 
332 	adf_cleanup_etr_data(accel_dev);
333 	adf_dev_restore(accel_dev);
334 }
335 EXPORT_SYMBOL_GPL(adf_dev_shutdown);
336 
adf_dev_restarting_notify(struct adf_accel_dev * accel_dev)337 int adf_dev_restarting_notify(struct adf_accel_dev *accel_dev)
338 {
339 	struct service_hndl *service;
340 	struct list_head *list_itr;
341 
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",
347 				service->name);
348 	}
349 	return 0;
350 }
351 
adf_dev_restarted_notify(struct adf_accel_dev * accel_dev)352 int adf_dev_restarted_notify(struct adf_accel_dev *accel_dev)
353 {
354 	struct service_hndl *service;
355 	struct list_head *list_itr;
356 
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",
362 				service->name);
363 	}
364 	return 0;
365 }
366 
adf_dev_shutdown_cache_cfg(struct adf_accel_dev * accel_dev)367 int adf_dev_shutdown_cache_cfg(struct adf_accel_dev *accel_dev)
368 {
369 	char services[ADF_CFG_MAX_VAL_LEN_IN_BYTES] = {0};
370 	int ret;
371 
372 	ret = adf_cfg_get_param_value(accel_dev, ADF_GENERAL_SEC,
373 				      ADF_SERVICES_ENABLED, services);
374 
375 	adf_dev_stop(accel_dev);
376 	adf_dev_shutdown(accel_dev);
377 
378 	if (!ret) {
379 		ret = adf_cfg_section_add(accel_dev, ADF_GENERAL_SEC);
380 		if (ret)
381 			return ret;
382 
383 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_GENERAL_SEC,
384 						  ADF_SERVICES_ENABLED,
385 						  services, ADF_STR);
386 		if (ret)
387 			return ret;
388 	}
389 
390 	return 0;
391 }
392