adf_init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. This file is provided under a dual BSD/GPLv2 license. When using or
  3. redistributing this file, you may do so under either license.
  4. GPL LICENSE SUMMARY
  5. Copyright(c) 2014 Intel Corporation.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Contact Information:
  14. qat-linux@intel.com
  15. BSD LICENSE
  16. Copyright(c) 2014 Intel Corporation.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. * Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. * Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in
  24. the documentation and/or other materials provided with the
  25. distribution.
  26. * Neither the name of Intel Corporation nor the names of its
  27. contributors may be used to endorse or promote products derived
  28. from this software without specific prior written permission.
  29. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. #include <linux/mutex.h>
  42. #include <linux/list.h>
  43. #include <linux/bitops.h>
  44. #include <linux/delay.h>
  45. #include "adf_accel_devices.h"
  46. #include "adf_cfg.h"
  47. #include "adf_common_drv.h"
  48. static LIST_HEAD(service_table);
  49. static DEFINE_MUTEX(service_lock);
  50. static void adf_service_add(struct service_hndl *service)
  51. {
  52. mutex_lock(&service_lock);
  53. list_add(&service->list, &service_table);
  54. mutex_unlock(&service_lock);
  55. }
  56. /**
  57. * adf_service_register() - Register acceleration service in the accel framework
  58. * @service: Pointer to the service
  59. *
  60. * Function adds the acceleration service to the acceleration framework.
  61. * To be used by QAT device specific drivers.
  62. *
  63. * Return: 0 on success, error code othewise.
  64. */
  65. int adf_service_register(struct service_hndl *service)
  66. {
  67. service->init_status = 0;
  68. service->start_status = 0;
  69. adf_service_add(service);
  70. return 0;
  71. }
  72. EXPORT_SYMBOL_GPL(adf_service_register);
  73. static void adf_service_remove(struct service_hndl *service)
  74. {
  75. mutex_lock(&service_lock);
  76. list_del(&service->list);
  77. mutex_unlock(&service_lock);
  78. }
  79. /**
  80. * adf_service_unregister() - Unregister acceleration service from the framework
  81. * @service: Pointer to the service
  82. *
  83. * Function remove the acceleration service from the acceleration framework.
  84. * To be used by QAT device specific drivers.
  85. *
  86. * Return: 0 on success, error code othewise.
  87. */
  88. int adf_service_unregister(struct service_hndl *service)
  89. {
  90. if (service->init_status || service->start_status) {
  91. pr_err("QAT: Could not remove active service\n");
  92. return -EFAULT;
  93. }
  94. adf_service_remove(service);
  95. return 0;
  96. }
  97. EXPORT_SYMBOL_GPL(adf_service_unregister);
  98. /**
  99. * adf_dev_start() - Start acceleration service for the given accel device
  100. * @accel_dev: Pointer to acceleration device.
  101. *
  102. * Function notifies all the registered services that the acceleration device
  103. * is ready to be used.
  104. * To be used by QAT device specific drivers.
  105. *
  106. * Return: 0 on success, error code othewise.
  107. */
  108. int adf_dev_start(struct adf_accel_dev *accel_dev)
  109. {
  110. struct service_hndl *service;
  111. struct list_head *list_itr;
  112. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  113. if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status)) {
  114. pr_info("QAT: Device not configured\n");
  115. return -EFAULT;
  116. }
  117. set_bit(ADF_STATUS_STARTING, &accel_dev->status);
  118. if (adf_ae_init(accel_dev)) {
  119. pr_err("QAT: Failed to initialise Acceleration Engine\n");
  120. return -EFAULT;
  121. }
  122. set_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status);
  123. if (adf_ae_fw_load(accel_dev)) {
  124. pr_err("QAT: Failed to load acceleration FW\n");
  125. adf_ae_fw_release(accel_dev);
  126. return -EFAULT;
  127. }
  128. set_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status);
  129. if (hw_data->alloc_irq(accel_dev)) {
  130. pr_err("QAT: Failed to allocate interrupts\n");
  131. return -EFAULT;
  132. }
  133. set_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
  134. /*
  135. * Subservice initialisation is divided into two stages: init and start.
  136. * This is to facilitate any ordering dependencies between services
  137. * prior to starting any of the accelerators.
  138. */
  139. list_for_each(list_itr, &service_table) {
  140. service = list_entry(list_itr, struct service_hndl, list);
  141. if (!service->admin)
  142. continue;
  143. if (service->event_hld(accel_dev, ADF_EVENT_INIT)) {
  144. pr_err("QAT: Failed to initialise service %s\n",
  145. service->name);
  146. return -EFAULT;
  147. }
  148. set_bit(accel_dev->accel_id, &service->init_status);
  149. }
  150. list_for_each(list_itr, &service_table) {
  151. service = list_entry(list_itr, struct service_hndl, list);
  152. if (service->admin)
  153. continue;
  154. if (service->event_hld(accel_dev, ADF_EVENT_INIT)) {
  155. pr_err("QAT: Failed to initialise service %s\n",
  156. service->name);
  157. return -EFAULT;
  158. }
  159. set_bit(accel_dev->accel_id, &service->init_status);
  160. }
  161. hw_data->enable_error_correction(accel_dev);
  162. if (adf_ae_start(accel_dev)) {
  163. pr_err("QAT: AE Start Failed\n");
  164. return -EFAULT;
  165. }
  166. set_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
  167. list_for_each(list_itr, &service_table) {
  168. service = list_entry(list_itr, struct service_hndl, list);
  169. if (!service->admin)
  170. continue;
  171. if (service->event_hld(accel_dev, ADF_EVENT_START)) {
  172. pr_err("QAT: Failed to start service %s\n",
  173. service->name);
  174. return -EFAULT;
  175. }
  176. set_bit(accel_dev->accel_id, &service->start_status);
  177. }
  178. list_for_each(list_itr, &service_table) {
  179. service = list_entry(list_itr, struct service_hndl, list);
  180. if (service->admin)
  181. continue;
  182. if (service->event_hld(accel_dev, ADF_EVENT_START)) {
  183. pr_err("QAT: Failed to start service %s\n",
  184. service->name);
  185. return -EFAULT;
  186. }
  187. set_bit(accel_dev->accel_id, &service->start_status);
  188. }
  189. clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
  190. set_bit(ADF_STATUS_STARTED, &accel_dev->status);
  191. if (qat_algs_register()) {
  192. pr_err("QAT: Failed to register crypto algs\n");
  193. set_bit(ADF_STATUS_STARTING, &accel_dev->status);
  194. clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
  195. return -EFAULT;
  196. }
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(adf_dev_start);
  200. /**
  201. * adf_dev_stop() - Stop acceleration service for the given accel device
  202. * @accel_dev: Pointer to acceleration device.
  203. *
  204. * Function notifies all the registered services that the acceleration device
  205. * is shuting down.
  206. * To be used by QAT device specific drivers.
  207. *
  208. * Return: 0 on success, error code othewise.
  209. */
  210. int adf_dev_stop(struct adf_accel_dev *accel_dev)
  211. {
  212. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  213. struct service_hndl *service;
  214. struct list_head *list_itr;
  215. int ret, wait = 0;
  216. if (!adf_dev_started(accel_dev) &&
  217. !test_bit(ADF_STATUS_STARTING, &accel_dev->status)) {
  218. return 0;
  219. }
  220. clear_bit(ADF_STATUS_CONFIGURED, &accel_dev->status);
  221. clear_bit(ADF_STATUS_STARTING, &accel_dev->status);
  222. clear_bit(ADF_STATUS_STARTED, &accel_dev->status);
  223. if (qat_algs_unregister())
  224. pr_err("QAT: Failed to unregister crypto algs\n");
  225. list_for_each(list_itr, &service_table) {
  226. service = list_entry(list_itr, struct service_hndl, list);
  227. if (service->admin)
  228. continue;
  229. if (!test_bit(accel_dev->accel_id, &service->start_status))
  230. continue;
  231. ret = service->event_hld(accel_dev, ADF_EVENT_STOP);
  232. if (!ret) {
  233. clear_bit(accel_dev->accel_id, &service->start_status);
  234. } else if (ret == -EAGAIN) {
  235. wait = 1;
  236. clear_bit(accel_dev->accel_id, &service->start_status);
  237. }
  238. }
  239. list_for_each(list_itr, &service_table) {
  240. service = list_entry(list_itr, struct service_hndl, list);
  241. if (!service->admin)
  242. continue;
  243. if (!test_bit(accel_dev->accel_id, &service->start_status))
  244. continue;
  245. if (service->event_hld(accel_dev, ADF_EVENT_STOP))
  246. pr_err("QAT: Failed to shutdown service %s\n",
  247. service->name);
  248. else
  249. clear_bit(accel_dev->accel_id, &service->start_status);
  250. }
  251. if (wait)
  252. msleep(100);
  253. if (adf_dev_started(accel_dev)) {
  254. if (adf_ae_stop(accel_dev))
  255. pr_err("QAT: failed to stop AE\n");
  256. else
  257. clear_bit(ADF_STATUS_AE_STARTED, &accel_dev->status);
  258. }
  259. if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) {
  260. if (adf_ae_fw_release(accel_dev))
  261. pr_err("QAT: Failed to release the ucode\n");
  262. else
  263. clear_bit(ADF_STATUS_AE_UCODE_LOADED,
  264. &accel_dev->status);
  265. }
  266. if (test_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status)) {
  267. if (adf_ae_shutdown(accel_dev))
  268. pr_err("QAT: Failed to shutdown Accel Engine\n");
  269. else
  270. clear_bit(ADF_STATUS_AE_INITIALISED,
  271. &accel_dev->status);
  272. }
  273. list_for_each(list_itr, &service_table) {
  274. service = list_entry(list_itr, struct service_hndl, list);
  275. if (service->admin)
  276. continue;
  277. if (!test_bit(accel_dev->accel_id, &service->init_status))
  278. continue;
  279. if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN))
  280. pr_err("QAT: Failed to shutdown service %s\n",
  281. service->name);
  282. else
  283. clear_bit(accel_dev->accel_id, &service->init_status);
  284. }
  285. list_for_each(list_itr, &service_table) {
  286. service = list_entry(list_itr, struct service_hndl, list);
  287. if (!service->admin)
  288. continue;
  289. if (!test_bit(accel_dev->accel_id, &service->init_status))
  290. continue;
  291. if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN))
  292. pr_err("QAT: Failed to shutdown service %s\n",
  293. service->name);
  294. else
  295. clear_bit(accel_dev->accel_id, &service->init_status);
  296. }
  297. if (test_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status)) {
  298. hw_data->free_irq(accel_dev);
  299. clear_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status);
  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. return 0;
  305. }
  306. EXPORT_SYMBOL_GPL(adf_dev_stop);
  307. int adf_dev_restarting_notify(struct adf_accel_dev *accel_dev)
  308. {
  309. struct service_hndl *service;
  310. struct list_head *list_itr;
  311. list_for_each(list_itr, &service_table) {
  312. service = list_entry(list_itr, struct service_hndl, list);
  313. if (service->admin)
  314. continue;
  315. if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING))
  316. pr_err("QAT: Failed to restart service %s.\n",
  317. service->name);
  318. }
  319. list_for_each(list_itr, &service_table) {
  320. service = list_entry(list_itr, struct service_hndl, list);
  321. if (!service->admin)
  322. continue;
  323. if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING))
  324. pr_err("QAT: Failed to restart service %s.\n",
  325. service->name);
  326. }
  327. return 0;
  328. }
  329. int adf_dev_restarted_notify(struct adf_accel_dev *accel_dev)
  330. {
  331. struct service_hndl *service;
  332. struct list_head *list_itr;
  333. list_for_each(list_itr, &service_table) {
  334. service = list_entry(list_itr, struct service_hndl, list);
  335. if (service->admin)
  336. continue;
  337. if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED))
  338. pr_err("QAT: Failed to restart service %s.\n",
  339. service->name);
  340. }
  341. list_for_each(list_itr, &service_table) {
  342. service = list_entry(list_itr, struct service_hndl, list);
  343. if (!service->admin)
  344. continue;
  345. if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED))
  346. pr_err("QAT: Failed to restart service %s.\n",
  347. service->name);
  348. }
  349. return 0;
  350. }