1 /* 2 * Copyright (c) 2014-2018 The Linux Foundation. All rights reserved. 3 * 4 * Permission to use, copy, modify, and/or distribute this software for 5 * any purpose with or without fee is hereby granted, provided that the 6 * above copyright notice and this permission notice appear in all 7 * copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 * PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <scheduler_api.h> 20 #include <scheduler_core.h> 21 #include <qdf_atomic.h> 22 23 QDF_STATUS scheduler_disable(void) 24 { 25 struct scheduler_ctx *sched_ctx; 26 27 sched_debug("Disabling Scheduler"); 28 29 sched_ctx = scheduler_get_context(); 30 QDF_BUG(sched_ctx); 31 if (!sched_ctx) 32 return QDF_STATUS_E_INVAL; 33 34 /* send shutdown signal to scheduler thread */ 35 qdf_atomic_set_bit(MC_SHUTDOWN_EVENT_MASK, &sched_ctx->sch_event_flag); 36 qdf_atomic_set_bit(MC_POST_EVENT_MASK, &sched_ctx->sch_event_flag); 37 qdf_wake_up_interruptible(&sched_ctx->sch_wait_queue); 38 39 /* wait for scheduler thread to shutdown */ 40 qdf_wait_single_event(&sched_ctx->sch_shutdown, 0); 41 sched_ctx->sch_thread = NULL; 42 43 /* flush any unprocessed scheduler messages */ 44 scheduler_queues_flush(sched_ctx); 45 46 return QDF_STATUS_SUCCESS; 47 } 48 49 static inline void scheduler_watchdog_notify(struct scheduler_ctx *sched) 50 { 51 char symbol[QDF_SYMBOL_LEN]; 52 53 if (sched->watchdog_callback) 54 qdf_sprint_symbol(symbol, sched->watchdog_callback); 55 56 sched_err("WLAN_BUG_RCA: Callback %s (type 0x%x) exceeded its allotted time of %ds", 57 sched->watchdog_callback ? symbol : "<null>", 58 sched->watchdog_msg_type, SCHEDULER_WATCHDOG_TIMEOUT / 1000); 59 } 60 61 #ifdef CONFIG_SLUB_DEBUG_ON 62 static void scheduler_watchdog_timeout(void *arg) 63 { 64 struct scheduler_ctx *sched = arg; 65 66 scheduler_watchdog_notify(sched); 67 if (sched->sch_thread) 68 qdf_print_thread_trace(sched->sch_thread); 69 70 /* avoid crashing during shutdown */ 71 if (qdf_atomic_test_bit(MC_SHUTDOWN_EVENT_MASK, &sched->sch_event_flag)) 72 return; 73 74 QDF_DEBUG_PANIC("Going down for Scheduler Watchdog Bite!"); 75 } 76 #else 77 static void scheduler_watchdog_timeout(void *arg) 78 { 79 scheduler_watchdog_notify((struct scheduler_ctx *)arg); 80 } 81 #endif 82 83 QDF_STATUS scheduler_enable(void) 84 { 85 struct scheduler_ctx *sched_ctx; 86 87 sched_debug("Enabling Scheduler"); 88 89 sched_ctx = scheduler_get_context(); 90 QDF_BUG(sched_ctx); 91 if (!sched_ctx) 92 return QDF_STATUS_E_INVAL; 93 94 qdf_atomic_clear_bit(MC_SHUTDOWN_EVENT_MASK, 95 &sched_ctx->sch_event_flag); 96 qdf_atomic_clear_bit(MC_POST_EVENT_MASK, 97 &sched_ctx->sch_event_flag); 98 99 /* create the scheduler thread */ 100 sched_ctx->sch_thread = qdf_create_thread(scheduler_thread, sched_ctx, 101 "scheduler_thread"); 102 if (IS_ERR(sched_ctx->sch_thread)) { 103 sched_err("Failed to create scheduler thread"); 104 return QDF_STATUS_E_RESOURCES; 105 } 106 107 sched_debug("Scheduler thread created"); 108 109 /* wait for the scheduler thread to startup */ 110 qdf_wake_up_process(sched_ctx->sch_thread); 111 qdf_wait_single_event(&sched_ctx->sch_start_event, 0); 112 113 sched_debug("Scheduler thread started"); 114 115 return QDF_STATUS_SUCCESS; 116 } 117 118 QDF_STATUS scheduler_init(void) 119 { 120 QDF_STATUS status; 121 struct scheduler_ctx *sched_ctx; 122 123 sched_debug("Initializing Scheduler"); 124 125 status = scheduler_create_ctx(); 126 if (QDF_IS_STATUS_ERROR(status)) { 127 sched_err("Failed to create context; status:%d", status); 128 return status; 129 } 130 131 sched_ctx = scheduler_get_context(); 132 QDF_BUG(sched_ctx); 133 if (!sched_ctx) { 134 status = QDF_STATUS_E_FAILURE; 135 goto ctx_destroy; 136 } 137 138 status = scheduler_queues_init(sched_ctx); 139 if (QDF_IS_STATUS_ERROR(status)) { 140 sched_err("Failed to init queues; status:%d", status); 141 goto ctx_destroy; 142 } 143 144 status = qdf_event_create(&sched_ctx->sch_start_event); 145 if (QDF_IS_STATUS_ERROR(status)) { 146 sched_err("Failed to create start event; status:%d", status); 147 goto queues_deinit; 148 } 149 150 status = qdf_event_create(&sched_ctx->sch_shutdown); 151 if (QDF_IS_STATUS_ERROR(status)) { 152 sched_err("Failed to create shutdown event; status:%d", status); 153 goto start_event_destroy; 154 } 155 156 status = qdf_event_create(&sched_ctx->resume_sch_event); 157 if (QDF_IS_STATUS_ERROR(status)) { 158 sched_err("Failed to create resume event; status:%d", status); 159 goto shutdown_event_destroy; 160 } 161 162 qdf_spinlock_create(&sched_ctx->sch_thread_lock); 163 qdf_init_waitqueue_head(&sched_ctx->sch_wait_queue); 164 sched_ctx->sch_event_flag = 0; 165 qdf_timer_init(NULL, 166 &sched_ctx->watchdog_timer, 167 &scheduler_watchdog_timeout, 168 sched_ctx, 169 QDF_TIMER_TYPE_SW); 170 171 qdf_register_mc_timer_callback(scheduler_mc_timer_callback); 172 173 return QDF_STATUS_SUCCESS; 174 175 shutdown_event_destroy: 176 qdf_event_destroy(&sched_ctx->sch_shutdown); 177 178 start_event_destroy: 179 qdf_event_destroy(&sched_ctx->sch_start_event); 180 181 queues_deinit: 182 scheduler_queues_deinit(sched_ctx); 183 184 ctx_destroy: 185 scheduler_destroy_ctx(); 186 187 return status; 188 } 189 190 QDF_STATUS scheduler_deinit(void) 191 { 192 QDF_STATUS status; 193 struct scheduler_ctx *sched_ctx; 194 195 sched_debug("Deinitializing Scheduler"); 196 197 sched_ctx = scheduler_get_context(); 198 QDF_BUG(sched_ctx); 199 if (!sched_ctx) 200 return QDF_STATUS_E_INVAL; 201 202 qdf_timer_free(&sched_ctx->watchdog_timer); 203 qdf_spinlock_destroy(&sched_ctx->sch_thread_lock); 204 qdf_event_destroy(&sched_ctx->resume_sch_event); 205 qdf_event_destroy(&sched_ctx->sch_shutdown); 206 qdf_event_destroy(&sched_ctx->sch_start_event); 207 208 status = scheduler_queues_deinit(sched_ctx); 209 if (QDF_IS_STATUS_ERROR(status)) 210 sched_err("Failed to deinit queues; status:%d", status); 211 212 status = scheduler_destroy_ctx(); 213 if (QDF_IS_STATUS_ERROR(status)) 214 sched_err("Failed to destroy context; status:%d", status); 215 216 return QDF_STATUS_SUCCESS; 217 } 218 219 QDF_STATUS scheduler_post_msg_by_priority(QDF_MODULE_ID qid, 220 struct scheduler_msg *msg, 221 bool is_high_priority) 222 { 223 uint8_t qidx; 224 struct scheduler_mq_type *target_mq; 225 struct scheduler_msg *queue_msg; 226 struct scheduler_ctx *sched_ctx; 227 228 QDF_BUG(msg); 229 if (!msg) 230 return QDF_STATUS_E_INVAL; 231 232 sched_ctx = scheduler_get_context(); 233 QDF_BUG(sched_ctx); 234 if (!sched_ctx) 235 return QDF_STATUS_E_INVAL; 236 237 if (!sched_ctx->sch_thread) { 238 sched_err("Cannot post message; scheduler thread is stopped"); 239 return QDF_STATUS_E_FAILURE; 240 } 241 242 if (msg->reserved != 0 && msg->reserved != SYS_MSG_COOKIE) { 243 QDF_DEBUG_PANIC("Scheduler messages must be initialized"); 244 return QDF_STATUS_E_FAILURE; 245 } 246 247 /* Target_If is a special message queue in phase 3 convergence beacause 248 * its used by both legacy WMA and as well as new UMAC components which 249 * directly populate callback handlers in message body. 250 * 1) WMA legacy messages should not have callback 251 * 2) New target_if message needs to have valid callback 252 * Clear callback handler for legacy WMA messages such that in case 253 * if someone is sending legacy WMA message from stack which has 254 * uninitialized callback then its handled properly. Also change 255 * legacy WMA message queue id to target_if queue such that its always 256 * handled in right order. 257 */ 258 if (QDF_MODULE_ID_WMA == qid) { 259 msg->callback = NULL; 260 /* change legacy WMA message id to new target_if mq id */ 261 qid = QDF_MODULE_ID_TARGET_IF; 262 } 263 264 qidx = sched_ctx->queue_ctx.scheduler_msg_qid_to_qidx[qid]; 265 if (qidx >= SCHEDULER_NUMBER_OF_MSG_QUEUE) { 266 sched_err("Scheduler is deinitialized ignore msg"); 267 return QDF_STATUS_E_FAILURE; 268 } 269 270 if (!sched_ctx->queue_ctx.scheduler_msg_process_fn[qidx]) { 271 QDF_DEBUG_PANIC("callback not registered for qid[%d]", qid); 272 return QDF_STATUS_E_FAILURE; 273 } 274 275 target_mq = &(sched_ctx->queue_ctx.sch_msg_q[qidx]); 276 277 queue_msg = scheduler_core_msg_dup(msg); 278 if (!queue_msg) 279 return QDF_STATUS_E_NOMEM; 280 281 if (is_high_priority) 282 scheduler_mq_put_front(target_mq, queue_msg); 283 else 284 scheduler_mq_put(target_mq, queue_msg); 285 286 qdf_atomic_set_bit(MC_POST_EVENT_MASK, &sched_ctx->sch_event_flag); 287 qdf_wake_up_interruptible(&sched_ctx->sch_wait_queue); 288 289 return QDF_STATUS_SUCCESS; 290 } 291 292 QDF_STATUS scheduler_register_module(QDF_MODULE_ID qid, 293 scheduler_msg_process_fn_t callback) 294 { 295 struct scheduler_mq_ctx *ctx; 296 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 297 298 sched_enter(); 299 300 QDF_BUG(sched_ctx); 301 if (!sched_ctx) 302 return QDF_STATUS_E_FAILURE; 303 304 if (sched_ctx->sch_last_qidx >= SCHEDULER_NUMBER_OF_MSG_QUEUE) { 305 sched_err("Already registered max %d no of message queues", 306 SCHEDULER_NUMBER_OF_MSG_QUEUE); 307 return QDF_STATUS_E_FAILURE; 308 } 309 310 ctx = &sched_ctx->queue_ctx; 311 ctx->scheduler_msg_qid_to_qidx[qid] = sched_ctx->sch_last_qidx; 312 ctx->sch_msg_q[sched_ctx->sch_last_qidx].qid = qid; 313 ctx->scheduler_msg_process_fn[sched_ctx->sch_last_qidx] = callback; 314 sched_ctx->sch_last_qidx++; 315 316 sched_exit(); 317 318 return QDF_STATUS_SUCCESS; 319 } 320 321 QDF_STATUS scheduler_deregister_module(QDF_MODULE_ID qid) 322 { 323 struct scheduler_mq_ctx *ctx; 324 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 325 uint8_t qidx; 326 327 sched_enter(); 328 329 QDF_BUG(sched_ctx); 330 if (!sched_ctx) 331 return QDF_STATUS_E_FAILURE; 332 333 ctx = &sched_ctx->queue_ctx; 334 qidx = ctx->scheduler_msg_qid_to_qidx[qid]; 335 ctx->scheduler_msg_process_fn[qidx] = NULL; 336 sched_ctx->sch_last_qidx--; 337 ctx->scheduler_msg_qid_to_qidx[qidx] = SCHEDULER_NUMBER_OF_MSG_QUEUE; 338 339 sched_exit(); 340 341 return QDF_STATUS_SUCCESS; 342 } 343 344 void scheduler_resume(void) 345 { 346 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 347 348 if (sched_ctx) 349 qdf_event_set(&sched_ctx->resume_sch_event); 350 } 351 352 void scheduler_register_hdd_suspend_callback(hdd_suspend_callback callback) 353 { 354 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 355 356 if (sched_ctx) 357 sched_ctx->hdd_callback = callback; 358 } 359 void scheduler_wake_up_controller_thread(void) 360 { 361 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 362 363 if (sched_ctx) 364 qdf_wake_up_interruptible(&sched_ctx->sch_wait_queue); 365 } 366 void scheduler_set_event_mask(uint32_t event_mask) 367 { 368 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 369 370 if (sched_ctx) 371 qdf_atomic_set_bit(event_mask, &sched_ctx->sch_event_flag); 372 } 373 374 void scheduler_clear_event_mask(uint32_t event_mask) 375 { 376 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 377 378 if (sched_ctx) 379 qdf_atomic_clear_bit(event_mask, &sched_ctx->sch_event_flag); 380 } 381 382 QDF_STATUS scheduler_target_if_mq_handler(struct scheduler_msg *msg) 383 { 384 QDF_STATUS status; 385 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 386 QDF_STATUS (*target_if_msg_handler)(struct scheduler_msg *); 387 388 QDF_BUG(msg); 389 if (!msg) 390 return QDF_STATUS_E_FAILURE; 391 392 QDF_BUG(sched_ctx); 393 if (!sched_ctx) 394 return QDF_STATUS_E_FAILURE; 395 396 target_if_msg_handler = msg->callback; 397 398 /* Target_If is a special message queue in phase 3 convergence beacause 399 * its used by both legacy WMA and as well as new UMAC components. New 400 * UMAC components directly pass their message handlers as callback in 401 * message body. 402 * 1) All Legacy WMA messages do not contain message callback so invoke 403 * registered legacy WMA handler. Scheduler message posting APIs 404 * makes sure legacy WMA messages do not have callbacks. 405 * 2) For new messages which have valid callbacks invoke their callbacks 406 * directly. 407 */ 408 if (!target_if_msg_handler) 409 status = sched_ctx->legacy_wma_handler(msg); 410 else 411 status = target_if_msg_handler(msg); 412 413 return status; 414 } 415 416 QDF_STATUS scheduler_os_if_mq_handler(struct scheduler_msg *msg) 417 { 418 QDF_STATUS (*os_if_msg_handler)(struct scheduler_msg *); 419 420 QDF_BUG(msg); 421 if (!msg) 422 return QDF_STATUS_E_FAILURE; 423 424 os_if_msg_handler = msg->callback; 425 426 QDF_BUG(os_if_msg_handler); 427 if (!os_if_msg_handler) 428 return QDF_STATUS_E_FAILURE; 429 430 os_if_msg_handler(msg); 431 432 return QDF_STATUS_SUCCESS; 433 } 434 435 QDF_STATUS scheduler_timer_q_mq_handler(struct scheduler_msg *msg) 436 { 437 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 438 qdf_mc_timer_callback_t timer_callback; 439 440 QDF_BUG(msg); 441 if (!msg) 442 return QDF_STATUS_E_FAILURE; 443 444 QDF_BUG(sched_ctx); 445 if (!sched_ctx) 446 return QDF_STATUS_E_FAILURE; 447 448 /* legacy sys message handler? */ 449 if (msg->reserved != SYS_MSG_COOKIE || msg->type != SYS_MSG_ID_MC_TIMER) 450 return sched_ctx->legacy_sys_handler(msg); 451 452 timer_callback = msg->callback; 453 QDF_BUG(timer_callback); 454 if (!timer_callback) 455 return QDF_STATUS_E_FAILURE; 456 457 timer_callback(msg->bodyptr); 458 459 return QDF_STATUS_SUCCESS; 460 } 461 462 QDF_STATUS scheduler_scan_mq_handler(struct scheduler_msg *msg) 463 { 464 QDF_STATUS (*scan_q_msg_handler)(struct scheduler_msg *); 465 466 QDF_BUG(msg); 467 if (!msg) 468 return QDF_STATUS_E_FAILURE; 469 470 scan_q_msg_handler = msg->callback; 471 472 QDF_BUG(scan_q_msg_handler); 473 if (!scan_q_msg_handler) 474 return QDF_STATUS_E_FAILURE; 475 476 scan_q_msg_handler(msg); 477 478 return QDF_STATUS_SUCCESS; 479 } 480 481 QDF_STATUS scheduler_register_wma_legacy_handler(scheduler_msg_process_fn_t 482 wma_callback) 483 { 484 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 485 486 QDF_BUG(sched_ctx); 487 if (!sched_ctx) 488 return QDF_STATUS_E_FAILURE; 489 490 sched_ctx->legacy_wma_handler = wma_callback; 491 492 return QDF_STATUS_SUCCESS; 493 } 494 495 QDF_STATUS scheduler_register_sys_legacy_handler(scheduler_msg_process_fn_t 496 sys_callback) 497 { 498 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 499 500 QDF_BUG(sched_ctx); 501 if (!sched_ctx) 502 return QDF_STATUS_E_FAILURE; 503 504 sched_ctx->legacy_sys_handler = sys_callback; 505 506 return QDF_STATUS_SUCCESS; 507 } 508 509 QDF_STATUS scheduler_deregister_wma_legacy_handler(void) 510 { 511 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 512 513 QDF_BUG(sched_ctx); 514 if (!sched_ctx) 515 return QDF_STATUS_E_FAILURE; 516 517 sched_ctx->legacy_wma_handler = NULL; 518 519 return QDF_STATUS_SUCCESS; 520 } 521 522 QDF_STATUS scheduler_deregister_sys_legacy_handler(void) 523 { 524 struct scheduler_ctx *sched_ctx = scheduler_get_context(); 525 526 QDF_BUG(sched_ctx); 527 if (!sched_ctx) 528 return QDF_STATUS_E_FAILURE; 529 530 sched_ctx->legacy_sys_handler = NULL; 531 532 return QDF_STATUS_SUCCESS; 533 } 534 535 static QDF_STATUS scheduler_msg_flush_noop(struct scheduler_msg *msg) 536 { 537 return QDF_STATUS_SUCCESS; 538 } 539 540 void scheduler_mc_timer_callback(unsigned long data) 541 { 542 qdf_mc_timer_t *timer = (qdf_mc_timer_t *)data; 543 struct scheduler_msg msg = {0}; 544 QDF_STATUS status; 545 546 qdf_mc_timer_callback_t callback = NULL; 547 void *user_data = NULL; 548 QDF_TIMER_TYPE type = QDF_TIMER_TYPE_SW; 549 550 QDF_BUG(timer); 551 if (!timer) 552 return; 553 554 qdf_spin_lock_irqsave(&timer->platform_info.spinlock); 555 556 switch (timer->state) { 557 case QDF_TIMER_STATE_STARTING: 558 /* we are in this state because someone just started the timer, 559 * MC timer got started and expired, but the time content have 560 * not been updated this is a rare race condition! 561 */ 562 timer->state = QDF_TIMER_STATE_STOPPED; 563 status = QDF_STATUS_E_ALREADY; 564 break; 565 566 case QDF_TIMER_STATE_STOPPED: 567 status = QDF_STATUS_E_ALREADY; 568 break; 569 570 case QDF_TIMER_STATE_UNUSED: 571 status = QDF_STATUS_E_EXISTS; 572 break; 573 574 case QDF_TIMER_STATE_RUNNING: 575 /* need to go to stop state here because the call-back function 576 * may restart timer (to emulate periodic timer) 577 */ 578 timer->state = QDF_TIMER_STATE_STOPPED; 579 /* copy the relevant timer information to local variables; 580 * once we exits from this critical section, the timer content 581 * may be modified by other tasks 582 */ 583 callback = timer->callback; 584 user_data = timer->user_data; 585 type = timer->type; 586 status = QDF_STATUS_SUCCESS; 587 break; 588 589 default: 590 QDF_ASSERT(0); 591 status = QDF_STATUS_E_FAULT; 592 break; 593 } 594 595 qdf_spin_unlock_irqrestore(&timer->platform_info.spinlock); 596 597 if (QDF_IS_STATUS_ERROR(status)) { 598 sched_debug("MC timer fired but is not running; skip callback"); 599 return; 600 } 601 602 qdf_try_allowing_sleep(type); 603 604 QDF_BUG(callback); 605 if (!callback) 606 return; 607 608 /* serialize to scheduler controller thread */ 609 msg.type = SYS_MSG_ID_MC_TIMER; 610 msg.reserved = SYS_MSG_COOKIE; 611 msg.callback = callback; 612 msg.bodyptr = user_data; 613 msg.bodyval = 0; 614 615 /* bodyptr points to user data, do not free it during msg flush */ 616 msg.flush_callback = scheduler_msg_flush_noop; 617 618 status = scheduler_post_msg(QDF_MODULE_ID_SYS, &msg); 619 if (QDF_IS_STATUS_ERROR(status)) 620 sched_err("Could not enqueue timer to timer queue"); 621 } 622