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