scheduler_api.c 19 KB

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