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