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 #include "adf_dbgfs.h" 11 #include "adf_heartbeat.h" 12 #include "adf_rl.h" 13 #include "adf_kpt.h" 14 #include "adf_sysfs_anti_rb.h" 15 #include "adf_sysfs_ras_counters.h" 16 #include "adf_telemetry.h" 17 18 static LIST_HEAD(service_table); 19 static DEFINE_MUTEX(service_lock); 20 21 static void adf_service_add(struct service_hndl *service) 22 { 23 mutex_lock(&service_lock); 24 list_add(&service->list, &service_table); 25 mutex_unlock(&service_lock); 26 } 27 28 int adf_service_register(struct service_hndl *service) 29 { 30 memset(service->init_status, 0, sizeof(service->init_status)); 31 memset(service->start_status, 0, sizeof(service->start_status)); 32 adf_service_add(service); 33 return 0; 34 } 35 36 static void adf_service_remove(struct service_hndl *service) 37 { 38 mutex_lock(&service_lock); 39 list_del(&service->list); 40 mutex_unlock(&service_lock); 41 } 42 43 int adf_service_unregister(struct service_hndl *service) 44 { 45 int i; 46 47 for (i = 0; i < ARRAY_SIZE(service->init_status); i++) { 48 if (service->init_status[i] || service->start_status[i]) { 49 pr_err("QAT: Could not remove active service\n"); 50 return -EFAULT; 51 } 52 } 53 adf_service_remove(service); 54 return 0; 55 } 56 57 /** 58 * adf_dev_init() - Init data structures and services for the given accel device 59 * @accel_dev: Pointer to acceleration device. 60 * 61 * Initialize the ring data structures and the admin comms and arbitration 62 * services. 63 * 64 * Return: 0 on success, error code otherwise. 65 */ 66 static int adf_dev_init(struct adf_accel_dev *accel_dev) 67 { 68 struct service_hndl *service; 69 struct adf_hw_device_data *hw_data = accel_dev->hw_device; 70 int ret; 71 72 if (!hw_data) { 73 dev_err(&GET_DEV(accel_dev), 74 "Failed to init device - hw_data not set\n"); 75 return -EFAULT; 76 } 77 78 adf_set_bme(accel_dev); 79 80 if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status) && 81 !accel_dev->is_vf) { 82 dev_err(&GET_DEV(accel_dev), "Device not configured\n"); 83 return -EFAULT; 84 } 85 86 if (adf_init_etr_data(accel_dev)) { 87 dev_err(&GET_DEV(accel_dev), "Failed initialize etr\n"); 88 return -EFAULT; 89 } 90 91 if (hw_data->init_device && hw_data->init_device(accel_dev)) { 92 dev_err(&GET_DEV(accel_dev), "Failed to initialize device\n"); 93 return -EFAULT; 94 } 95 96 if (hw_data->init_admin_comms && hw_data->init_admin_comms(accel_dev)) { 97 dev_err(&GET_DEV(accel_dev), "Failed initialize admin comms\n"); 98 return -EFAULT; 99 } 100 101 if (hw_data->init_arb && hw_data->init_arb(accel_dev)) { 102 dev_err(&GET_DEV(accel_dev), "Failed initialize hw arbiter\n"); 103 return -EFAULT; 104 } 105 106 if (hw_data->get_ring_to_svc_map) 107 hw_data->ring_to_svc_map = hw_data->get_ring_to_svc_map(accel_dev); 108 109 if (adf_ae_init(accel_dev)) { 110 dev_err(&GET_DEV(accel_dev), 111 "Failed to initialise Acceleration Engine\n"); 112 return -EFAULT; 113 } 114 set_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status); 115 116 if (adf_ae_fw_load(accel_dev)) { 117 dev_err(&GET_DEV(accel_dev), 118 "Failed to load acceleration FW\n"); 119 return -EFAULT; 120 } 121 set_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status); 122 123 if (hw_data->alloc_irq(accel_dev)) { 124 dev_err(&GET_DEV(accel_dev), "Failed to allocate interrupts\n"); 125 return -EFAULT; 126 } 127 set_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status); 128 129 if (hw_data->ras_ops.enable_ras_errors) 130 hw_data->ras_ops.enable_ras_errors(accel_dev); 131 132 hw_data->enable_ints(accel_dev); 133 hw_data->enable_error_correction(accel_dev); 134 135 ret = hw_data->pfvf_ops.enable_comms(accel_dev); 136 if (ret) 137 return ret; 138 139 if (!test_bit(ADF_STATUS_CONFIGURED, &accel_dev->status) && 140 accel_dev->is_vf) { 141 if (qat_crypto_vf_dev_config(accel_dev)) 142 return -EFAULT; 143 } 144 145 adf_heartbeat_init(accel_dev); 146 ret = adf_rl_init(accel_dev); 147 if (ret && ret != -EOPNOTSUPP) 148 return ret; 149 150 ret = adf_tl_init(accel_dev); 151 if (ret && ret != -EOPNOTSUPP) 152 return ret; 153 154 /* 155 * Subservice initialisation is divided into two stages: init and start. 156 * This is to facilitate any ordering dependencies between services 157 * prior to starting any of the accelerators. 158 */ 159 mutex_lock(&service_lock); 160 list_for_each_entry(service, &service_table, list) { 161 if (service->event_hld(accel_dev, ADF_EVENT_INIT)) { 162 dev_err(&GET_DEV(accel_dev), 163 "Failed to initialise service %s\n", 164 service->name); 165 mutex_unlock(&service_lock); 166 return -EFAULT; 167 } 168 set_bit(accel_dev->accel_id, service->init_status); 169 } 170 mutex_unlock(&service_lock); 171 172 return 0; 173 } 174 175 /** 176 * adf_dev_start() - Start acceleration service for the given accel device 177 * @accel_dev: Pointer to acceleration device. 178 * 179 * Function notifies all the registered services that the acceleration device 180 * is ready to be used. 181 * To be used by QAT device specific drivers. 182 * 183 * Return: 0 on success, error code otherwise. 184 */ 185 static int adf_dev_start(struct adf_accel_dev *accel_dev) 186 { 187 struct adf_hw_device_data *hw_data = accel_dev->hw_device; 188 struct service_hndl *service; 189 u32 caps; 190 int ret; 191 192 set_bit(ADF_STATUS_STARTING, &accel_dev->status); 193 194 if (adf_ae_start(accel_dev)) { 195 dev_err(&GET_DEV(accel_dev), "AE Start Failed\n"); 196 return -EFAULT; 197 } 198 set_bit(ADF_STATUS_AE_STARTED, &accel_dev->status); 199 200 if (hw_data->send_admin_init(accel_dev)) { 201 dev_err(&GET_DEV(accel_dev), "Failed to send init message\n"); 202 return -EFAULT; 203 } 204 205 if (hw_data->measure_clock) { 206 ret = hw_data->measure_clock(accel_dev); 207 if (ret) { 208 dev_err(&GET_DEV(accel_dev), "Failed measure device clock\n"); 209 return ret; 210 } 211 } 212 213 /* Set ssm watch dog timer */ 214 if (hw_data->set_ssm_wdtimer) 215 hw_data->set_ssm_wdtimer(accel_dev); 216 217 /* Enable Power Management */ 218 if (hw_data->enable_pm && hw_data->enable_pm(accel_dev)) { 219 dev_err(&GET_DEV(accel_dev), "Failed to configure Power Management\n"); 220 return -EFAULT; 221 } 222 223 /* Enable Key Protection Technology (KPT) */ 224 ret = adf_enable_kpt(accel_dev); 225 if (ret) { 226 dev_err(&GET_DEV(accel_dev), "Failed to enable KPT\n"); 227 return ret; 228 } 229 230 if (hw_data->start_timer) { 231 ret = hw_data->start_timer(accel_dev); 232 if (ret) { 233 dev_err(&GET_DEV(accel_dev), "Failed to start internal sync timer\n"); 234 return ret; 235 } 236 } 237 238 adf_heartbeat_start(accel_dev); 239 ret = adf_rl_start(accel_dev); 240 if (ret && ret != -EOPNOTSUPP) 241 return ret; 242 243 ret = adf_tl_start(accel_dev); 244 if (ret && ret != -EOPNOTSUPP) 245 return ret; 246 247 mutex_lock(&service_lock); 248 list_for_each_entry(service, &service_table, list) { 249 if (service->event_hld(accel_dev, ADF_EVENT_START)) { 250 dev_err(&GET_DEV(accel_dev), 251 "Failed to start service %s\n", 252 service->name); 253 mutex_unlock(&service_lock); 254 return -EFAULT; 255 } 256 set_bit(accel_dev->accel_id, service->start_status); 257 } 258 mutex_unlock(&service_lock); 259 260 clear_bit(ADF_STATUS_STARTING, &accel_dev->status); 261 set_bit(ADF_STATUS_STARTED, &accel_dev->status); 262 263 if (!list_empty(&accel_dev->crypto_list) && 264 (qat_algs_register() || qat_asym_algs_register())) { 265 dev_err(&GET_DEV(accel_dev), 266 "Failed to register crypto algs\n"); 267 set_bit(ADF_STATUS_STARTING, &accel_dev->status); 268 clear_bit(ADF_STATUS_STARTED, &accel_dev->status); 269 return -EFAULT; 270 } 271 set_bit(ADF_STATUS_CRYPTO_ALGS_REGISTERED, &accel_dev->status); 272 273 caps = hw_data->accel_capabilities_ext_mask; 274 if (!list_empty(&accel_dev->compression_list) && qat_comp_algs_register(caps)) { 275 dev_err(&GET_DEV(accel_dev), 276 "Failed to register compression algs\n"); 277 set_bit(ADF_STATUS_STARTING, &accel_dev->status); 278 clear_bit(ADF_STATUS_STARTED, &accel_dev->status); 279 return -EFAULT; 280 } 281 set_bit(ADF_STATUS_COMP_ALGS_REGISTERED, &accel_dev->status); 282 283 adf_dbgfs_add(accel_dev); 284 adf_sysfs_start_ras(accel_dev); 285 adf_sysfs_start_arb(accel_dev); 286 287 return 0; 288 } 289 290 /** 291 * adf_dev_stop() - Stop acceleration service for the given accel device 292 * @accel_dev: Pointer to acceleration device. 293 * 294 * Function notifies all the registered services that the acceleration device 295 * is shuting down. 296 * To be used by QAT device specific drivers. 297 * 298 * Return: void 299 */ 300 static void adf_dev_stop(struct adf_accel_dev *accel_dev) 301 { 302 struct adf_hw_device_data *hw_data = accel_dev->hw_device; 303 struct service_hndl *service; 304 bool wait = false; 305 int ret; 306 307 if (!adf_dev_started(accel_dev) && 308 !test_bit(ADF_STATUS_STARTING, &accel_dev->status)) 309 return; 310 311 adf_tl_stop(accel_dev); 312 adf_rl_stop(accel_dev); 313 adf_dbgfs_rm(accel_dev); 314 adf_sysfs_stop_ras(accel_dev); 315 adf_sysfs_stop_arb(accel_dev); 316 317 clear_bit(ADF_STATUS_STARTING, &accel_dev->status); 318 clear_bit(ADF_STATUS_STARTED, &accel_dev->status); 319 320 if (!list_empty(&accel_dev->crypto_list) && 321 test_bit(ADF_STATUS_CRYPTO_ALGS_REGISTERED, &accel_dev->status)) { 322 qat_algs_unregister(); 323 qat_asym_algs_unregister(); 324 } 325 clear_bit(ADF_STATUS_CRYPTO_ALGS_REGISTERED, &accel_dev->status); 326 327 if (!list_empty(&accel_dev->compression_list) && 328 test_bit(ADF_STATUS_COMP_ALGS_REGISTERED, &accel_dev->status)) 329 qat_comp_algs_unregister(hw_data->accel_capabilities_ext_mask); 330 clear_bit(ADF_STATUS_COMP_ALGS_REGISTERED, &accel_dev->status); 331 332 mutex_lock(&service_lock); 333 list_for_each_entry(service, &service_table, list) { 334 if (!test_bit(accel_dev->accel_id, service->start_status)) 335 continue; 336 ret = service->event_hld(accel_dev, ADF_EVENT_STOP); 337 if (!ret) { 338 clear_bit(accel_dev->accel_id, service->start_status); 339 } else if (ret == -EAGAIN) { 340 wait = true; 341 clear_bit(accel_dev->accel_id, service->start_status); 342 } 343 } 344 mutex_unlock(&service_lock); 345 346 if (hw_data->stop_timer) 347 hw_data->stop_timer(accel_dev); 348 349 hw_data->disable_iov(accel_dev); 350 351 if (wait) 352 msleep(100); 353 354 if (test_bit(ADF_STATUS_AE_STARTED, &accel_dev->status)) { 355 if (adf_ae_stop(accel_dev)) 356 dev_err(&GET_DEV(accel_dev), "failed to stop AE\n"); 357 else 358 clear_bit(ADF_STATUS_AE_STARTED, &accel_dev->status); 359 } 360 } 361 362 /** 363 * adf_dev_shutdown() - shutdown acceleration services and data strucutures 364 * @accel_dev: Pointer to acceleration device 365 * 366 * Cleanup the ring data structures and the admin comms and arbitration 367 * services. 368 */ 369 static void adf_dev_shutdown(struct adf_accel_dev *accel_dev) 370 { 371 struct adf_hw_device_data *hw_data = accel_dev->hw_device; 372 struct service_hndl *service; 373 374 if (!hw_data) { 375 dev_err(&GET_DEV(accel_dev), 376 "QAT: Failed to shutdown device - hw_data not set\n"); 377 return; 378 } 379 380 if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) { 381 adf_ae_fw_release(accel_dev); 382 clear_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status); 383 } 384 385 if (test_bit(ADF_STATUS_AE_INITIALISED, &accel_dev->status)) { 386 if (adf_ae_shutdown(accel_dev)) 387 dev_err(&GET_DEV(accel_dev), 388 "Failed to shutdown Accel Engine\n"); 389 else 390 clear_bit(ADF_STATUS_AE_INITIALISED, 391 &accel_dev->status); 392 } 393 394 mutex_lock(&service_lock); 395 list_for_each_entry(service, &service_table, list) { 396 if (!test_bit(accel_dev->accel_id, service->init_status)) 397 continue; 398 if (service->event_hld(accel_dev, ADF_EVENT_SHUTDOWN)) 399 dev_err(&GET_DEV(accel_dev), 400 "Failed to shutdown service %s\n", 401 service->name); 402 else 403 clear_bit(accel_dev->accel_id, service->init_status); 404 } 405 mutex_unlock(&service_lock); 406 407 adf_rl_exit(accel_dev); 408 409 if (hw_data->ras_ops.disable_ras_errors) 410 hw_data->ras_ops.disable_ras_errors(accel_dev); 411 412 adf_heartbeat_shutdown(accel_dev); 413 414 adf_tl_shutdown(accel_dev); 415 416 if (test_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status)) { 417 hw_data->free_irq(accel_dev); 418 clear_bit(ADF_STATUS_IRQ_ALLOCATED, &accel_dev->status); 419 } 420 421 /* If not restarting, delete all cfg sections except for GENERAL */ 422 if (!test_bit(ADF_STATUS_RESTARTING, &accel_dev->status)) 423 adf_cfg_del_all_except(accel_dev, ADF_GENERAL_SEC); 424 425 if (hw_data->exit_arb) 426 hw_data->exit_arb(accel_dev); 427 428 if (hw_data->exit_admin_comms) 429 hw_data->exit_admin_comms(accel_dev); 430 431 adf_cleanup_etr_data(accel_dev); 432 adf_misc_wq_flush(); 433 adf_dev_restore(accel_dev); 434 } 435 436 int adf_dev_restarting_notify(struct adf_accel_dev *accel_dev) 437 { 438 struct service_hndl *service; 439 440 mutex_lock(&service_lock); 441 list_for_each_entry(service, &service_table, list) { 442 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTING)) 443 dev_err(&GET_DEV(accel_dev), 444 "Failed to restart service %s.\n", 445 service->name); 446 } 447 mutex_unlock(&service_lock); 448 return 0; 449 } 450 451 int adf_dev_restarted_notify(struct adf_accel_dev *accel_dev) 452 { 453 struct service_hndl *service; 454 455 mutex_lock(&service_lock); 456 list_for_each_entry(service, &service_table, list) { 457 if (service->event_hld(accel_dev, ADF_EVENT_RESTARTED)) 458 dev_err(&GET_DEV(accel_dev), 459 "Failed to restart service %s.\n", 460 service->name); 461 } 462 mutex_unlock(&service_lock); 463 return 0; 464 } 465 466 void adf_error_notifier(struct adf_accel_dev *accel_dev) 467 { 468 struct service_hndl *service; 469 470 mutex_lock(&service_lock); 471 list_for_each_entry(service, &service_table, list) { 472 if (service->event_hld(accel_dev, ADF_EVENT_FATAL_ERROR)) 473 dev_err(&GET_DEV(accel_dev), 474 "Failed to send error event to %s.\n", 475 service->name); 476 } 477 mutex_unlock(&service_lock); 478 } 479 480 int adf_dev_down(struct adf_accel_dev *accel_dev) 481 { 482 int ret = 0; 483 484 if (!accel_dev) 485 return -EINVAL; 486 487 mutex_lock(&accel_dev->state_lock); 488 489 adf_dev_stop(accel_dev); 490 adf_dev_shutdown(accel_dev); 491 492 mutex_unlock(&accel_dev->state_lock); 493 return ret; 494 } 495 EXPORT_SYMBOL_GPL(adf_dev_down); 496 497 int adf_dev_up(struct adf_accel_dev *accel_dev, bool config) 498 { 499 int ret = 0; 500 501 if (!accel_dev) 502 return -EINVAL; 503 504 mutex_lock(&accel_dev->state_lock); 505 506 if (adf_dev_started(accel_dev)) { 507 dev_info(&GET_DEV(accel_dev), "Device qat_dev%d already up\n", 508 accel_dev->accel_id); 509 ret = -EALREADY; 510 goto out; 511 } 512 513 if (config && GET_HW_DATA(accel_dev)->dev_config) { 514 ret = GET_HW_DATA(accel_dev)->dev_config(accel_dev); 515 if (unlikely(ret)) 516 goto out; 517 } 518 519 ret = adf_dev_init(accel_dev); 520 if (unlikely(ret)) 521 goto out; 522 523 ret = adf_dev_start(accel_dev); 524 525 out: 526 mutex_unlock(&accel_dev->state_lock); 527 return ret; 528 } 529 EXPORT_SYMBOL_GPL(adf_dev_up); 530 531 int adf_dev_restart(struct adf_accel_dev *accel_dev) 532 { 533 int ret = 0; 534 535 if (!accel_dev) 536 return -EFAULT; 537 538 adf_dev_down(accel_dev); 539 540 ret = adf_dev_up(accel_dev, false); 541 /* if device is already up return success*/ 542 if (ret == -EALREADY) 543 return 0; 544 545 return ret; 546 } 547 EXPORT_SYMBOL_GPL(adf_dev_restart); 548