wmi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (c) 2010-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "htc.h"
  17. static const char *wmi_cmd_to_name(enum wmi_cmd_id wmi_cmd)
  18. {
  19. switch (wmi_cmd) {
  20. case WMI_ECHO_CMDID:
  21. return "WMI_ECHO_CMDID";
  22. case WMI_ACCESS_MEMORY_CMDID:
  23. return "WMI_ACCESS_MEMORY_CMDID";
  24. case WMI_GET_FW_VERSION:
  25. return "WMI_GET_FW_VERSION";
  26. case WMI_DISABLE_INTR_CMDID:
  27. return "WMI_DISABLE_INTR_CMDID";
  28. case WMI_ENABLE_INTR_CMDID:
  29. return "WMI_ENABLE_INTR_CMDID";
  30. case WMI_ATH_INIT_CMDID:
  31. return "WMI_ATH_INIT_CMDID";
  32. case WMI_ABORT_TXQ_CMDID:
  33. return "WMI_ABORT_TXQ_CMDID";
  34. case WMI_STOP_TX_DMA_CMDID:
  35. return "WMI_STOP_TX_DMA_CMDID";
  36. case WMI_ABORT_TX_DMA_CMDID:
  37. return "WMI_ABORT_TX_DMA_CMDID";
  38. case WMI_DRAIN_TXQ_CMDID:
  39. return "WMI_DRAIN_TXQ_CMDID";
  40. case WMI_DRAIN_TXQ_ALL_CMDID:
  41. return "WMI_DRAIN_TXQ_ALL_CMDID";
  42. case WMI_START_RECV_CMDID:
  43. return "WMI_START_RECV_CMDID";
  44. case WMI_STOP_RECV_CMDID:
  45. return "WMI_STOP_RECV_CMDID";
  46. case WMI_FLUSH_RECV_CMDID:
  47. return "WMI_FLUSH_RECV_CMDID";
  48. case WMI_SET_MODE_CMDID:
  49. return "WMI_SET_MODE_CMDID";
  50. case WMI_NODE_CREATE_CMDID:
  51. return "WMI_NODE_CREATE_CMDID";
  52. case WMI_NODE_REMOVE_CMDID:
  53. return "WMI_NODE_REMOVE_CMDID";
  54. case WMI_VAP_REMOVE_CMDID:
  55. return "WMI_VAP_REMOVE_CMDID";
  56. case WMI_VAP_CREATE_CMDID:
  57. return "WMI_VAP_CREATE_CMDID";
  58. case WMI_REG_READ_CMDID:
  59. return "WMI_REG_READ_CMDID";
  60. case WMI_REG_WRITE_CMDID:
  61. return "WMI_REG_WRITE_CMDID";
  62. case WMI_REG_RMW_CMDID:
  63. return "WMI_REG_RMW_CMDID";
  64. case WMI_RC_STATE_CHANGE_CMDID:
  65. return "WMI_RC_STATE_CHANGE_CMDID";
  66. case WMI_RC_RATE_UPDATE_CMDID:
  67. return "WMI_RC_RATE_UPDATE_CMDID";
  68. case WMI_TARGET_IC_UPDATE_CMDID:
  69. return "WMI_TARGET_IC_UPDATE_CMDID";
  70. case WMI_TX_AGGR_ENABLE_CMDID:
  71. return "WMI_TX_AGGR_ENABLE_CMDID";
  72. case WMI_TGT_DETACH_CMDID:
  73. return "WMI_TGT_DETACH_CMDID";
  74. case WMI_NODE_UPDATE_CMDID:
  75. return "WMI_NODE_UPDATE_CMDID";
  76. case WMI_INT_STATS_CMDID:
  77. return "WMI_INT_STATS_CMDID";
  78. case WMI_TX_STATS_CMDID:
  79. return "WMI_TX_STATS_CMDID";
  80. case WMI_RX_STATS_CMDID:
  81. return "WMI_RX_STATS_CMDID";
  82. case WMI_BITRATE_MASK_CMDID:
  83. return "WMI_BITRATE_MASK_CMDID";
  84. }
  85. return "Bogus";
  86. }
  87. struct wmi *ath9k_init_wmi(struct ath9k_htc_priv *priv)
  88. {
  89. struct wmi *wmi;
  90. wmi = kzalloc(sizeof(struct wmi), GFP_KERNEL);
  91. if (!wmi)
  92. return NULL;
  93. wmi->drv_priv = priv;
  94. wmi->stopped = false;
  95. skb_queue_head_init(&wmi->wmi_event_queue);
  96. spin_lock_init(&wmi->wmi_lock);
  97. spin_lock_init(&wmi->event_lock);
  98. mutex_init(&wmi->op_mutex);
  99. mutex_init(&wmi->multi_write_mutex);
  100. mutex_init(&wmi->multi_rmw_mutex);
  101. init_completion(&wmi->cmd_wait);
  102. INIT_LIST_HEAD(&wmi->pending_tx_events);
  103. tasklet_init(&wmi->wmi_event_tasklet, ath9k_wmi_event_tasklet,
  104. (unsigned long)wmi);
  105. return wmi;
  106. }
  107. void ath9k_deinit_wmi(struct ath9k_htc_priv *priv)
  108. {
  109. struct wmi *wmi = priv->wmi;
  110. mutex_lock(&wmi->op_mutex);
  111. wmi->stopped = true;
  112. mutex_unlock(&wmi->op_mutex);
  113. kfree(priv->wmi);
  114. }
  115. void ath9k_wmi_event_drain(struct ath9k_htc_priv *priv)
  116. {
  117. unsigned long flags;
  118. tasklet_kill(&priv->wmi->wmi_event_tasklet);
  119. spin_lock_irqsave(&priv->wmi->wmi_lock, flags);
  120. __skb_queue_purge(&priv->wmi->wmi_event_queue);
  121. spin_unlock_irqrestore(&priv->wmi->wmi_lock, flags);
  122. }
  123. void ath9k_wmi_event_tasklet(unsigned long data)
  124. {
  125. struct wmi *wmi = (struct wmi *)data;
  126. struct ath9k_htc_priv *priv = wmi->drv_priv;
  127. struct wmi_cmd_hdr *hdr;
  128. void *wmi_event;
  129. struct wmi_event_swba *swba;
  130. struct sk_buff *skb = NULL;
  131. unsigned long flags;
  132. u16 cmd_id;
  133. do {
  134. spin_lock_irqsave(&wmi->wmi_lock, flags);
  135. skb = __skb_dequeue(&wmi->wmi_event_queue);
  136. if (!skb) {
  137. spin_unlock_irqrestore(&wmi->wmi_lock, flags);
  138. return;
  139. }
  140. spin_unlock_irqrestore(&wmi->wmi_lock, flags);
  141. hdr = (struct wmi_cmd_hdr *) skb->data;
  142. cmd_id = be16_to_cpu(hdr->command_id);
  143. wmi_event = skb_pull(skb, sizeof(struct wmi_cmd_hdr));
  144. switch (cmd_id) {
  145. case WMI_SWBA_EVENTID:
  146. swba = (struct wmi_event_swba *) wmi_event;
  147. ath9k_htc_swba(priv, swba);
  148. break;
  149. case WMI_FATAL_EVENTID:
  150. ieee80211_queue_work(wmi->drv_priv->hw,
  151. &wmi->drv_priv->fatal_work);
  152. break;
  153. case WMI_TXSTATUS_EVENTID:
  154. spin_lock_bh(&priv->tx.tx_lock);
  155. if (priv->tx.flags & ATH9K_HTC_OP_TX_DRAIN) {
  156. spin_unlock_bh(&priv->tx.tx_lock);
  157. break;
  158. }
  159. spin_unlock_bh(&priv->tx.tx_lock);
  160. ath9k_htc_txstatus(priv, wmi_event);
  161. break;
  162. default:
  163. break;
  164. }
  165. kfree_skb(skb);
  166. } while (1);
  167. }
  168. void ath9k_fatal_work(struct work_struct *work)
  169. {
  170. struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
  171. fatal_work);
  172. struct ath_common *common = ath9k_hw_common(priv->ah);
  173. ath_dbg(common, FATAL, "FATAL Event received, resetting device\n");
  174. ath9k_htc_reset(priv);
  175. }
  176. static void ath9k_wmi_rsp_callback(struct wmi *wmi, struct sk_buff *skb)
  177. {
  178. skb_pull(skb, sizeof(struct wmi_cmd_hdr));
  179. if (wmi->cmd_rsp_buf != NULL && wmi->cmd_rsp_len != 0)
  180. memcpy(wmi->cmd_rsp_buf, skb->data, wmi->cmd_rsp_len);
  181. complete(&wmi->cmd_wait);
  182. }
  183. static void ath9k_wmi_ctrl_rx(void *priv, struct sk_buff *skb,
  184. enum htc_endpoint_id epid)
  185. {
  186. struct wmi *wmi = (struct wmi *) priv;
  187. struct wmi_cmd_hdr *hdr;
  188. u16 cmd_id;
  189. if (unlikely(wmi->stopped))
  190. goto free_skb;
  191. hdr = (struct wmi_cmd_hdr *) skb->data;
  192. cmd_id = be16_to_cpu(hdr->command_id);
  193. if (cmd_id & 0x1000) {
  194. spin_lock(&wmi->wmi_lock);
  195. __skb_queue_tail(&wmi->wmi_event_queue, skb);
  196. spin_unlock(&wmi->wmi_lock);
  197. tasklet_schedule(&wmi->wmi_event_tasklet);
  198. return;
  199. }
  200. /* Check if there has been a timeout. */
  201. spin_lock(&wmi->wmi_lock);
  202. if (be16_to_cpu(hdr->seq_no) != wmi->last_seq_id) {
  203. spin_unlock(&wmi->wmi_lock);
  204. goto free_skb;
  205. }
  206. spin_unlock(&wmi->wmi_lock);
  207. /* WMI command response */
  208. ath9k_wmi_rsp_callback(wmi, skb);
  209. free_skb:
  210. kfree_skb(skb);
  211. }
  212. static void ath9k_wmi_ctrl_tx(void *priv, struct sk_buff *skb,
  213. enum htc_endpoint_id epid, bool txok)
  214. {
  215. kfree_skb(skb);
  216. }
  217. int ath9k_wmi_connect(struct htc_target *htc, struct wmi *wmi,
  218. enum htc_endpoint_id *wmi_ctrl_epid)
  219. {
  220. struct htc_service_connreq connect;
  221. int ret;
  222. wmi->htc = htc;
  223. memset(&connect, 0, sizeof(connect));
  224. connect.ep_callbacks.priv = wmi;
  225. connect.ep_callbacks.tx = ath9k_wmi_ctrl_tx;
  226. connect.ep_callbacks.rx = ath9k_wmi_ctrl_rx;
  227. connect.service_id = WMI_CONTROL_SVC;
  228. ret = htc_connect_service(htc, &connect, &wmi->ctrl_epid);
  229. if (ret)
  230. return ret;
  231. *wmi_ctrl_epid = wmi->ctrl_epid;
  232. return 0;
  233. }
  234. static int ath9k_wmi_cmd_issue(struct wmi *wmi,
  235. struct sk_buff *skb,
  236. enum wmi_cmd_id cmd, u16 len)
  237. {
  238. struct wmi_cmd_hdr *hdr;
  239. unsigned long flags;
  240. hdr = (struct wmi_cmd_hdr *) skb_push(skb, sizeof(struct wmi_cmd_hdr));
  241. hdr->command_id = cpu_to_be16(cmd);
  242. hdr->seq_no = cpu_to_be16(++wmi->tx_seq_id);
  243. spin_lock_irqsave(&wmi->wmi_lock, flags);
  244. wmi->last_seq_id = wmi->tx_seq_id;
  245. spin_unlock_irqrestore(&wmi->wmi_lock, flags);
  246. return htc_send_epid(wmi->htc, skb, wmi->ctrl_epid);
  247. }
  248. int ath9k_wmi_cmd(struct wmi *wmi, enum wmi_cmd_id cmd_id,
  249. u8 *cmd_buf, u32 cmd_len,
  250. u8 *rsp_buf, u32 rsp_len,
  251. u32 timeout)
  252. {
  253. struct ath_hw *ah = wmi->drv_priv->ah;
  254. struct ath_common *common = ath9k_hw_common(ah);
  255. u16 headroom = sizeof(struct htc_frame_hdr) +
  256. sizeof(struct wmi_cmd_hdr);
  257. struct sk_buff *skb;
  258. u8 *data;
  259. unsigned long time_left;
  260. int ret = 0;
  261. if (ah->ah_flags & AH_UNPLUGGED)
  262. return 0;
  263. skb = alloc_skb(headroom + cmd_len, GFP_ATOMIC);
  264. if (!skb)
  265. return -ENOMEM;
  266. skb_reserve(skb, headroom);
  267. if (cmd_len != 0 && cmd_buf != NULL) {
  268. data = (u8 *) skb_put(skb, cmd_len);
  269. memcpy(data, cmd_buf, cmd_len);
  270. }
  271. mutex_lock(&wmi->op_mutex);
  272. /* check if wmi stopped flag is set */
  273. if (unlikely(wmi->stopped)) {
  274. ret = -EPROTO;
  275. goto out;
  276. }
  277. /* record the rsp buffer and length */
  278. wmi->cmd_rsp_buf = rsp_buf;
  279. wmi->cmd_rsp_len = rsp_len;
  280. ret = ath9k_wmi_cmd_issue(wmi, skb, cmd_id, cmd_len);
  281. if (ret)
  282. goto out;
  283. time_left = wait_for_completion_timeout(&wmi->cmd_wait, timeout);
  284. if (!time_left) {
  285. ath_dbg(common, WMI, "Timeout waiting for WMI command: %s\n",
  286. wmi_cmd_to_name(cmd_id));
  287. mutex_unlock(&wmi->op_mutex);
  288. return -ETIMEDOUT;
  289. }
  290. mutex_unlock(&wmi->op_mutex);
  291. return 0;
  292. out:
  293. ath_dbg(common, WMI, "WMI failure for: %s\n", wmi_cmd_to_name(cmd_id));
  294. mutex_unlock(&wmi->op_mutex);
  295. kfree_skb(skb);
  296. return ret;
  297. }