notif-wait.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <linuxwifi@intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/sched.h>
  66. #include <linux/export.h>
  67. #include "iwl-drv.h"
  68. #include "notif-wait.h"
  69. void iwl_notification_wait_init(struct iwl_notif_wait_data *notif_wait)
  70. {
  71. spin_lock_init(&notif_wait->notif_wait_lock);
  72. INIT_LIST_HEAD(&notif_wait->notif_waits);
  73. init_waitqueue_head(&notif_wait->notif_waitq);
  74. }
  75. IWL_EXPORT_SYMBOL(iwl_notification_wait_init);
  76. bool iwl_notification_wait(struct iwl_notif_wait_data *notif_wait,
  77. struct iwl_rx_packet *pkt)
  78. {
  79. bool triggered = false;
  80. if (!list_empty(&notif_wait->notif_waits)) {
  81. struct iwl_notification_wait *w;
  82. spin_lock(&notif_wait->notif_wait_lock);
  83. list_for_each_entry(w, &notif_wait->notif_waits, list) {
  84. int i;
  85. bool found = false;
  86. /*
  87. * If it already finished (triggered) or has been
  88. * aborted then don't evaluate it again to avoid races,
  89. * Otherwise the function could be called again even
  90. * though it returned true before
  91. */
  92. if (w->triggered || w->aborted)
  93. continue;
  94. for (i = 0; i < w->n_cmds; i++) {
  95. u16 rec_id = WIDE_ID(pkt->hdr.group_id,
  96. pkt->hdr.cmd);
  97. if (w->cmds[i] == rec_id ||
  98. (!iwl_cmd_groupid(w->cmds[i]) &&
  99. DEF_ID(w->cmds[i]) == rec_id)) {
  100. found = true;
  101. break;
  102. }
  103. }
  104. if (!found)
  105. continue;
  106. if (!w->fn || w->fn(notif_wait, pkt, w->fn_data)) {
  107. w->triggered = true;
  108. triggered = true;
  109. }
  110. }
  111. spin_unlock(&notif_wait->notif_wait_lock);
  112. }
  113. return triggered;
  114. }
  115. IWL_EXPORT_SYMBOL(iwl_notification_wait);
  116. void iwl_abort_notification_waits(struct iwl_notif_wait_data *notif_wait)
  117. {
  118. struct iwl_notification_wait *wait_entry;
  119. spin_lock(&notif_wait->notif_wait_lock);
  120. list_for_each_entry(wait_entry, &notif_wait->notif_waits, list)
  121. wait_entry->aborted = true;
  122. spin_unlock(&notif_wait->notif_wait_lock);
  123. wake_up_all(&notif_wait->notif_waitq);
  124. }
  125. IWL_EXPORT_SYMBOL(iwl_abort_notification_waits);
  126. void
  127. iwl_init_notification_wait(struct iwl_notif_wait_data *notif_wait,
  128. struct iwl_notification_wait *wait_entry,
  129. const u16 *cmds, int n_cmds,
  130. bool (*fn)(struct iwl_notif_wait_data *notif_wait,
  131. struct iwl_rx_packet *pkt, void *data),
  132. void *fn_data)
  133. {
  134. if (WARN_ON(n_cmds > MAX_NOTIF_CMDS))
  135. n_cmds = MAX_NOTIF_CMDS;
  136. wait_entry->fn = fn;
  137. wait_entry->fn_data = fn_data;
  138. wait_entry->n_cmds = n_cmds;
  139. memcpy(wait_entry->cmds, cmds, n_cmds * sizeof(u16));
  140. wait_entry->triggered = false;
  141. wait_entry->aborted = false;
  142. spin_lock_bh(&notif_wait->notif_wait_lock);
  143. list_add(&wait_entry->list, &notif_wait->notif_waits);
  144. spin_unlock_bh(&notif_wait->notif_wait_lock);
  145. }
  146. IWL_EXPORT_SYMBOL(iwl_init_notification_wait);
  147. void iwl_remove_notification(struct iwl_notif_wait_data *notif_wait,
  148. struct iwl_notification_wait *wait_entry)
  149. {
  150. spin_lock_bh(&notif_wait->notif_wait_lock);
  151. list_del(&wait_entry->list);
  152. spin_unlock_bh(&notif_wait->notif_wait_lock);
  153. }
  154. IWL_EXPORT_SYMBOL(iwl_remove_notification);
  155. int iwl_wait_notification(struct iwl_notif_wait_data *notif_wait,
  156. struct iwl_notification_wait *wait_entry,
  157. unsigned long timeout)
  158. {
  159. int ret;
  160. ret = wait_event_timeout(notif_wait->notif_waitq,
  161. wait_entry->triggered || wait_entry->aborted,
  162. timeout);
  163. iwl_remove_notification(notif_wait, wait_entry);
  164. if (wait_entry->aborted)
  165. return -EIO;
  166. /* return value is always >= 0 */
  167. if (ret <= 0)
  168. return -ETIMEDOUT;
  169. return 0;
  170. }
  171. IWL_EXPORT_SYMBOL(iwl_wait_notification);