iwl-hcmd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *****************************************************************************/
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/sched.h>
  31. #include <net/mac80211.h>
  32. #include "iwl-dev.h" /* FIXME: remove */
  33. #include "iwl-debug.h"
  34. #include "iwl-eeprom.h"
  35. #include "iwl-core.h"
  36. const char *get_cmd_string(u8 cmd)
  37. {
  38. switch (cmd) {
  39. IWL_CMD(REPLY_ALIVE);
  40. IWL_CMD(REPLY_ERROR);
  41. IWL_CMD(REPLY_RXON);
  42. IWL_CMD(REPLY_RXON_ASSOC);
  43. IWL_CMD(REPLY_QOS_PARAM);
  44. IWL_CMD(REPLY_RXON_TIMING);
  45. IWL_CMD(REPLY_ADD_STA);
  46. IWL_CMD(REPLY_REMOVE_STA);
  47. IWL_CMD(REPLY_REMOVE_ALL_STA);
  48. IWL_CMD(REPLY_TXFIFO_FLUSH);
  49. IWL_CMD(REPLY_WEPKEY);
  50. IWL_CMD(REPLY_3945_RX);
  51. IWL_CMD(REPLY_TX);
  52. IWL_CMD(REPLY_RATE_SCALE);
  53. IWL_CMD(REPLY_LEDS_CMD);
  54. IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
  55. IWL_CMD(COEX_PRIORITY_TABLE_CMD);
  56. IWL_CMD(COEX_MEDIUM_NOTIFICATION);
  57. IWL_CMD(COEX_EVENT_CMD);
  58. IWL_CMD(REPLY_QUIET_CMD);
  59. IWL_CMD(REPLY_CHANNEL_SWITCH);
  60. IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
  61. IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
  62. IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
  63. IWL_CMD(POWER_TABLE_CMD);
  64. IWL_CMD(PM_SLEEP_NOTIFICATION);
  65. IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
  66. IWL_CMD(REPLY_SCAN_CMD);
  67. IWL_CMD(REPLY_SCAN_ABORT_CMD);
  68. IWL_CMD(SCAN_START_NOTIFICATION);
  69. IWL_CMD(SCAN_RESULTS_NOTIFICATION);
  70. IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
  71. IWL_CMD(BEACON_NOTIFICATION);
  72. IWL_CMD(REPLY_TX_BEACON);
  73. IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
  74. IWL_CMD(QUIET_NOTIFICATION);
  75. IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
  76. IWL_CMD(MEASURE_ABORT_NOTIFICATION);
  77. IWL_CMD(REPLY_BT_CONFIG);
  78. IWL_CMD(REPLY_STATISTICS_CMD);
  79. IWL_CMD(STATISTICS_NOTIFICATION);
  80. IWL_CMD(REPLY_CARD_STATE_CMD);
  81. IWL_CMD(CARD_STATE_NOTIFICATION);
  82. IWL_CMD(MISSED_BEACONS_NOTIFICATION);
  83. IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
  84. IWL_CMD(SENSITIVITY_CMD);
  85. IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
  86. IWL_CMD(REPLY_RX_PHY_CMD);
  87. IWL_CMD(REPLY_RX_MPDU_CMD);
  88. IWL_CMD(REPLY_RX);
  89. IWL_CMD(REPLY_COMPRESSED_BA);
  90. IWL_CMD(CALIBRATION_CFG_CMD);
  91. IWL_CMD(CALIBRATION_RES_NOTIFICATION);
  92. IWL_CMD(CALIBRATION_COMPLETE_NOTIFICATION);
  93. IWL_CMD(REPLY_TX_POWER_DBM_CMD);
  94. IWL_CMD(TEMPERATURE_NOTIFICATION);
  95. IWL_CMD(TX_ANT_CONFIGURATION_CMD);
  96. IWL_CMD(REPLY_BT_COEX_PROFILE_NOTIF);
  97. IWL_CMD(REPLY_BT_COEX_PRIO_TABLE);
  98. IWL_CMD(REPLY_BT_COEX_PROT_ENV);
  99. IWL_CMD(REPLY_WIPAN_PARAMS);
  100. IWL_CMD(REPLY_WIPAN_RXON);
  101. IWL_CMD(REPLY_WIPAN_RXON_TIMING);
  102. IWL_CMD(REPLY_WIPAN_RXON_ASSOC);
  103. IWL_CMD(REPLY_WIPAN_QOS_PARAM);
  104. IWL_CMD(REPLY_WIPAN_WEPKEY);
  105. IWL_CMD(REPLY_WIPAN_P2P_CHANNEL_SWITCH);
  106. IWL_CMD(REPLY_WIPAN_NOA_NOTIFICATION);
  107. default:
  108. return "UNKNOWN";
  109. }
  110. }
  111. EXPORT_SYMBOL(get_cmd_string);
  112. #define HOST_COMPLETE_TIMEOUT (HZ / 2)
  113. static void iwl_generic_cmd_callback(struct iwl_priv *priv,
  114. struct iwl_device_cmd *cmd,
  115. struct iwl_rx_packet *pkt)
  116. {
  117. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  118. IWL_ERR(priv, "Bad return from %s (0x%08X)\n",
  119. get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  120. return;
  121. }
  122. #ifdef CONFIG_IWLWIFI_DEBUG
  123. switch (cmd->hdr.cmd) {
  124. case REPLY_TX_LINK_QUALITY_CMD:
  125. case SENSITIVITY_CMD:
  126. IWL_DEBUG_HC_DUMP(priv, "back from %s (0x%08X)\n",
  127. get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  128. break;
  129. default:
  130. IWL_DEBUG_HC(priv, "back from %s (0x%08X)\n",
  131. get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  132. }
  133. #endif
  134. }
  135. static int iwl_send_cmd_async(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  136. {
  137. int ret;
  138. BUG_ON(!(cmd->flags & CMD_ASYNC));
  139. /* An asynchronous command can not expect an SKB to be set. */
  140. BUG_ON(cmd->flags & CMD_WANT_SKB);
  141. /* Assign a generic callback if one is not provided */
  142. if (!cmd->callback)
  143. cmd->callback = iwl_generic_cmd_callback;
  144. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  145. return -EBUSY;
  146. ret = iwl_enqueue_hcmd(priv, cmd);
  147. if (ret < 0) {
  148. IWL_ERR(priv, "Error sending %s: enqueue_hcmd failed: %d\n",
  149. get_cmd_string(cmd->id), ret);
  150. return ret;
  151. }
  152. return 0;
  153. }
  154. int iwl_send_cmd_sync(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  155. {
  156. int cmd_idx;
  157. int ret;
  158. BUG_ON(cmd->flags & CMD_ASYNC);
  159. /* A synchronous command can not have a callback set. */
  160. BUG_ON(cmd->callback);
  161. IWL_DEBUG_INFO(priv, "Attempting to send sync command %s\n",
  162. get_cmd_string(cmd->id));
  163. mutex_lock(&priv->sync_cmd_mutex);
  164. set_bit(STATUS_HCMD_ACTIVE, &priv->status);
  165. IWL_DEBUG_INFO(priv, "Setting HCMD_ACTIVE for command %s\n",
  166. get_cmd_string(cmd->id));
  167. cmd_idx = iwl_enqueue_hcmd(priv, cmd);
  168. if (cmd_idx < 0) {
  169. ret = cmd_idx;
  170. IWL_ERR(priv, "Error sending %s: enqueue_hcmd failed: %d\n",
  171. get_cmd_string(cmd->id), ret);
  172. goto out;
  173. }
  174. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  175. !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
  176. HOST_COMPLETE_TIMEOUT);
  177. if (!ret) {
  178. if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
  179. IWL_ERR(priv,
  180. "Error sending %s: time out after %dms.\n",
  181. get_cmd_string(cmd->id),
  182. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  183. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  184. IWL_DEBUG_INFO(priv, "Clearing HCMD_ACTIVE for command %s\n",
  185. get_cmd_string(cmd->id));
  186. ret = -ETIMEDOUT;
  187. goto cancel;
  188. }
  189. }
  190. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  191. IWL_ERR(priv, "Command %s aborted: RF KILL Switch\n",
  192. get_cmd_string(cmd->id));
  193. ret = -ECANCELED;
  194. goto fail;
  195. }
  196. if (test_bit(STATUS_FW_ERROR, &priv->status)) {
  197. IWL_ERR(priv, "Command %s failed: FW Error\n",
  198. get_cmd_string(cmd->id));
  199. ret = -EIO;
  200. goto fail;
  201. }
  202. if ((cmd->flags & CMD_WANT_SKB) && !cmd->reply_page) {
  203. IWL_ERR(priv, "Error: Response NULL in '%s'\n",
  204. get_cmd_string(cmd->id));
  205. ret = -EIO;
  206. goto cancel;
  207. }
  208. ret = 0;
  209. goto out;
  210. cancel:
  211. if (cmd->flags & CMD_WANT_SKB) {
  212. /*
  213. * Cancel the CMD_WANT_SKB flag for the cmd in the
  214. * TX cmd queue. Otherwise in case the cmd comes
  215. * in later, it will possibly set an invalid
  216. * address (cmd->meta.source).
  217. */
  218. priv->txq[priv->cmd_queue].meta[cmd_idx].flags &=
  219. ~CMD_WANT_SKB;
  220. }
  221. fail:
  222. if (cmd->reply_page) {
  223. iwl_free_pages(priv, cmd->reply_page);
  224. cmd->reply_page = 0;
  225. }
  226. out:
  227. mutex_unlock(&priv->sync_cmd_mutex);
  228. return ret;
  229. }
  230. EXPORT_SYMBOL(iwl_send_cmd_sync);
  231. int iwl_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  232. {
  233. if (cmd->flags & CMD_ASYNC)
  234. return iwl_send_cmd_async(priv, cmd);
  235. return iwl_send_cmd_sync(priv, cmd);
  236. }
  237. EXPORT_SYMBOL(iwl_send_cmd);
  238. int iwl_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
  239. {
  240. struct iwl_host_cmd cmd = {
  241. .id = id,
  242. .len = len,
  243. .data = data,
  244. };
  245. return iwl_send_cmd_sync(priv, &cmd);
  246. }
  247. EXPORT_SYMBOL(iwl_send_cmd_pdu);
  248. int iwl_send_cmd_pdu_async(struct iwl_priv *priv,
  249. u8 id, u16 len, const void *data,
  250. void (*callback)(struct iwl_priv *priv,
  251. struct iwl_device_cmd *cmd,
  252. struct iwl_rx_packet *pkt))
  253. {
  254. struct iwl_host_cmd cmd = {
  255. .id = id,
  256. .len = len,
  257. .data = data,
  258. };
  259. cmd.flags |= CMD_ASYNC;
  260. cmd.callback = callback;
  261. return iwl_send_cmd_async(priv, &cmd);
  262. }
  263. EXPORT_SYMBOL(iwl_send_cmd_pdu_async);