Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1 | /* |
Sujith Manoharan | 5b68138 | 2011-05-17 13:36:18 +0530 | [diff] [blame] | 2 | * Copyright (c) 2010-2011 Atheros Communications Inc. |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 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 | |
| 17 | #include "htc.h" |
| 18 | |
| 19 | static const char *wmi_cmd_to_name(enum wmi_cmd_id wmi_cmd) |
| 20 | { |
| 21 | switch (wmi_cmd) { |
| 22 | case WMI_ECHO_CMDID: |
| 23 | return "WMI_ECHO_CMDID"; |
| 24 | case WMI_ACCESS_MEMORY_CMDID: |
| 25 | return "WMI_ACCESS_MEMORY_CMDID"; |
Sujith Manoharan | 29bbfb2 | 2011-04-13 11:22:51 +0530 | [diff] [blame] | 26 | case WMI_GET_FW_VERSION: |
| 27 | return "WMI_GET_FW_VERSION"; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 28 | case WMI_DISABLE_INTR_CMDID: |
| 29 | return "WMI_DISABLE_INTR_CMDID"; |
| 30 | case WMI_ENABLE_INTR_CMDID: |
| 31 | return "WMI_ENABLE_INTR_CMDID"; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 32 | case WMI_ATH_INIT_CMDID: |
| 33 | return "WMI_ATH_INIT_CMDID"; |
| 34 | case WMI_ABORT_TXQ_CMDID: |
| 35 | return "WMI_ABORT_TXQ_CMDID"; |
| 36 | case WMI_STOP_TX_DMA_CMDID: |
| 37 | return "WMI_STOP_TX_DMA_CMDID"; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 38 | case WMI_ABORT_TX_DMA_CMDID: |
| 39 | return "WMI_ABORT_TX_DMA_CMDID"; |
| 40 | case WMI_DRAIN_TXQ_CMDID: |
| 41 | return "WMI_DRAIN_TXQ_CMDID"; |
| 42 | case WMI_DRAIN_TXQ_ALL_CMDID: |
| 43 | return "WMI_DRAIN_TXQ_ALL_CMDID"; |
| 44 | case WMI_START_RECV_CMDID: |
| 45 | return "WMI_START_RECV_CMDID"; |
| 46 | case WMI_STOP_RECV_CMDID: |
| 47 | return "WMI_STOP_RECV_CMDID"; |
| 48 | case WMI_FLUSH_RECV_CMDID: |
| 49 | return "WMI_FLUSH_RECV_CMDID"; |
| 50 | case WMI_SET_MODE_CMDID: |
| 51 | return "WMI_SET_MODE_CMDID"; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 52 | case WMI_NODE_CREATE_CMDID: |
| 53 | return "WMI_NODE_CREATE_CMDID"; |
| 54 | case WMI_NODE_REMOVE_CMDID: |
| 55 | return "WMI_NODE_REMOVE_CMDID"; |
| 56 | case WMI_VAP_REMOVE_CMDID: |
| 57 | return "WMI_VAP_REMOVE_CMDID"; |
| 58 | case WMI_VAP_CREATE_CMDID: |
| 59 | return "WMI_VAP_CREATE_CMDID"; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 60 | case WMI_REG_READ_CMDID: |
| 61 | return "WMI_REG_READ_CMDID"; |
| 62 | case WMI_REG_WRITE_CMDID: |
| 63 | return "WMI_REG_WRITE_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"; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 68 | case WMI_TARGET_IC_UPDATE_CMDID: |
| 69 | return "WMI_TARGET_IC_UPDATE_CMDID"; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 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"; |
Sujith Manoharan | fbc29d6 | 2011-04-13 11:26:58 +0530 | [diff] [blame] | 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"; |
Sujith Manoharan | a55bb94 | 2011-04-27 17:13:23 +0530 | [diff] [blame] | 82 | case WMI_BITRATE_MASK_CMDID: |
| 83 | return "WMI_BITRATE_MASK_CMDID"; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return "Bogus"; |
| 87 | } |
| 88 | |
| 89 | struct wmi *ath9k_init_wmi(struct ath9k_htc_priv *priv) |
| 90 | { |
| 91 | struct wmi *wmi; |
| 92 | |
| 93 | wmi = kzalloc(sizeof(struct wmi), GFP_KERNEL); |
| 94 | if (!wmi) |
| 95 | return NULL; |
| 96 | |
| 97 | wmi->drv_priv = priv; |
| 98 | wmi->stopped = false; |
Sujith Manoharan | f4c8899 | 2011-04-13 11:23:52 +0530 | [diff] [blame] | 99 | skb_queue_head_init(&wmi->wmi_event_queue); |
Sujith Manoharan | 859c3ca | 2011-04-13 11:26:39 +0530 | [diff] [blame] | 100 | spin_lock_init(&wmi->wmi_lock); |
| 101 | spin_lock_init(&wmi->event_lock); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 102 | mutex_init(&wmi->op_mutex); |
Sujith | 4a22fe1 | 2010-04-16 11:53:53 +0530 | [diff] [blame] | 103 | mutex_init(&wmi->multi_write_mutex); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 104 | init_completion(&wmi->cmd_wait); |
Sujith Manoharan | 859c3ca | 2011-04-13 11:26:39 +0530 | [diff] [blame] | 105 | INIT_LIST_HEAD(&wmi->pending_tx_events); |
Sujith Manoharan | f4c8899 | 2011-04-13 11:23:52 +0530 | [diff] [blame] | 106 | tasklet_init(&wmi->wmi_event_tasklet, ath9k_wmi_event_tasklet, |
| 107 | (unsigned long)wmi); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 108 | |
| 109 | return wmi; |
| 110 | } |
| 111 | |
| 112 | void ath9k_deinit_wmi(struct ath9k_htc_priv *priv) |
| 113 | { |
| 114 | struct wmi *wmi = priv->wmi; |
| 115 | |
| 116 | mutex_lock(&wmi->op_mutex); |
| 117 | wmi->stopped = true; |
| 118 | mutex_unlock(&wmi->op_mutex); |
| 119 | |
| 120 | kfree(priv->wmi); |
| 121 | } |
| 122 | |
Sujith Manoharan | f4c8899 | 2011-04-13 11:23:52 +0530 | [diff] [blame] | 123 | void ath9k_wmi_event_drain(struct ath9k_htc_priv *priv) |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 124 | { |
Sujith Manoharan | f4c8899 | 2011-04-13 11:23:52 +0530 | [diff] [blame] | 125 | unsigned long flags; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 126 | |
Sujith Manoharan | f4c8899 | 2011-04-13 11:23:52 +0530 | [diff] [blame] | 127 | tasklet_kill(&priv->wmi->wmi_event_tasklet); |
| 128 | spin_lock_irqsave(&priv->wmi->wmi_lock, flags); |
| 129 | __skb_queue_purge(&priv->wmi->wmi_event_queue); |
| 130 | spin_unlock_irqrestore(&priv->wmi->wmi_lock, flags); |
| 131 | } |
| 132 | |
| 133 | void ath9k_wmi_event_tasklet(unsigned long data) |
| 134 | { |
| 135 | struct wmi *wmi = (struct wmi *)data; |
| 136 | struct ath9k_htc_priv *priv = wmi->drv_priv; |
| 137 | struct wmi_cmd_hdr *hdr; |
| 138 | void *wmi_event; |
| 139 | struct wmi_event_swba *swba; |
| 140 | struct sk_buff *skb = NULL; |
| 141 | unsigned long flags; |
| 142 | u16 cmd_id; |
Sujith Manoharan | f4c8899 | 2011-04-13 11:23:52 +0530 | [diff] [blame] | 143 | |
| 144 | do { |
| 145 | spin_lock_irqsave(&wmi->wmi_lock, flags); |
| 146 | skb = __skb_dequeue(&wmi->wmi_event_queue); |
| 147 | if (!skb) { |
| 148 | spin_unlock_irqrestore(&wmi->wmi_lock, flags); |
| 149 | return; |
| 150 | } |
| 151 | spin_unlock_irqrestore(&wmi->wmi_lock, flags); |
| 152 | |
| 153 | hdr = (struct wmi_cmd_hdr *) skb->data; |
| 154 | cmd_id = be16_to_cpu(hdr->command_id); |
| 155 | wmi_event = skb_pull(skb, sizeof(struct wmi_cmd_hdr)); |
| 156 | |
| 157 | switch (cmd_id) { |
| 158 | case WMI_SWBA_EVENTID: |
| 159 | swba = (struct wmi_event_swba *) wmi_event; |
| 160 | ath9k_htc_swba(priv, swba); |
| 161 | break; |
| 162 | case WMI_FATAL_EVENTID: |
| 163 | ieee80211_queue_work(wmi->drv_priv->hw, |
| 164 | &wmi->drv_priv->fatal_work); |
| 165 | break; |
Sujith Manoharan | 27876a2 | 2011-04-13 11:26:18 +0530 | [diff] [blame] | 166 | case WMI_TXSTATUS_EVENTID: |
| 167 | spin_lock_bh(&priv->tx.tx_lock); |
| 168 | if (priv->tx.flags & ATH9K_HTC_OP_TX_DRAIN) { |
| 169 | spin_unlock_bh(&priv->tx.tx_lock); |
| 170 | break; |
| 171 | } |
| 172 | spin_unlock_bh(&priv->tx.tx_lock); |
| 173 | |
| 174 | ath9k_htc_txstatus(priv, wmi_event); |
| 175 | break; |
Sujith Manoharan | f4c8899 | 2011-04-13 11:23:52 +0530 | [diff] [blame] | 176 | default: |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | kfree_skb(skb); |
| 181 | } while (1); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 182 | } |
| 183 | |
Sujith Manoharan | 7390867 | 2010-12-28 14:28:27 +0530 | [diff] [blame] | 184 | void ath9k_fatal_work(struct work_struct *work) |
| 185 | { |
| 186 | struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv, |
| 187 | fatal_work); |
| 188 | struct ath_common *common = ath9k_hw_common(priv->ah); |
| 189 | |
Joe Perches | d2182b6 | 2011-12-15 14:55:53 -0800 | [diff] [blame] | 190 | ath_dbg(common, FATAL, "FATAL Event received, resetting device\n"); |
Sujith Manoharan | 7390867 | 2010-12-28 14:28:27 +0530 | [diff] [blame] | 191 | ath9k_htc_reset(priv); |
| 192 | } |
| 193 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 194 | static void ath9k_wmi_rsp_callback(struct wmi *wmi, struct sk_buff *skb) |
| 195 | { |
| 196 | skb_pull(skb, sizeof(struct wmi_cmd_hdr)); |
| 197 | |
| 198 | if (wmi->cmd_rsp_buf != NULL && wmi->cmd_rsp_len != 0) |
| 199 | memcpy(wmi->cmd_rsp_buf, skb->data, wmi->cmd_rsp_len); |
| 200 | |
| 201 | complete(&wmi->cmd_wait); |
| 202 | } |
| 203 | |
| 204 | static void ath9k_wmi_ctrl_rx(void *priv, struct sk_buff *skb, |
| 205 | enum htc_endpoint_id epid) |
| 206 | { |
| 207 | struct wmi *wmi = (struct wmi *) priv; |
| 208 | struct wmi_cmd_hdr *hdr; |
| 209 | u16 cmd_id; |
| 210 | |
| 211 | if (unlikely(wmi->stopped)) |
| 212 | goto free_skb; |
| 213 | |
| 214 | hdr = (struct wmi_cmd_hdr *) skb->data; |
| 215 | cmd_id = be16_to_cpu(hdr->command_id); |
| 216 | |
| 217 | if (cmd_id & 0x1000) { |
Sujith Manoharan | f4c8899 | 2011-04-13 11:23:52 +0530 | [diff] [blame] | 218 | spin_lock(&wmi->wmi_lock); |
| 219 | __skb_queue_tail(&wmi->wmi_event_queue, skb); |
| 220 | spin_unlock(&wmi->wmi_lock); |
| 221 | tasklet_schedule(&wmi->wmi_event_tasklet); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
Sujith | 6ce34ec | 2010-04-16 11:54:01 +0530 | [diff] [blame] | 225 | /* Check if there has been a timeout. */ |
| 226 | spin_lock(&wmi->wmi_lock); |
| 227 | if (cmd_id != wmi->last_cmd_id) { |
| 228 | spin_unlock(&wmi->wmi_lock); |
| 229 | goto free_skb; |
| 230 | } |
| 231 | spin_unlock(&wmi->wmi_lock); |
| 232 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 233 | /* WMI command response */ |
| 234 | ath9k_wmi_rsp_callback(wmi, skb); |
| 235 | |
| 236 | free_skb: |
Ming Lei | e6c6d33 | 2010-04-13 00:29:05 +0800 | [diff] [blame] | 237 | kfree_skb(skb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void ath9k_wmi_ctrl_tx(void *priv, struct sk_buff *skb, |
| 241 | enum htc_endpoint_id epid, bool txok) |
| 242 | { |
Ming Lei | 0fa35a5 | 2010-04-13 00:29:15 +0800 | [diff] [blame] | 243 | kfree_skb(skb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | int ath9k_wmi_connect(struct htc_target *htc, struct wmi *wmi, |
| 247 | enum htc_endpoint_id *wmi_ctrl_epid) |
| 248 | { |
| 249 | struct htc_service_connreq connect; |
| 250 | int ret; |
| 251 | |
| 252 | wmi->htc = htc; |
| 253 | |
| 254 | memset(&connect, 0, sizeof(connect)); |
| 255 | |
| 256 | connect.ep_callbacks.priv = wmi; |
| 257 | connect.ep_callbacks.tx = ath9k_wmi_ctrl_tx; |
| 258 | connect.ep_callbacks.rx = ath9k_wmi_ctrl_rx; |
| 259 | connect.service_id = WMI_CONTROL_SVC; |
| 260 | |
| 261 | ret = htc_connect_service(htc, &connect, &wmi->ctrl_epid); |
| 262 | if (ret) |
| 263 | return ret; |
| 264 | |
| 265 | *wmi_ctrl_epid = wmi->ctrl_epid; |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int ath9k_wmi_cmd_issue(struct wmi *wmi, |
| 271 | struct sk_buff *skb, |
| 272 | enum wmi_cmd_id cmd, u16 len) |
| 273 | { |
| 274 | struct wmi_cmd_hdr *hdr; |
| 275 | |
| 276 | hdr = (struct wmi_cmd_hdr *) skb_push(skb, sizeof(struct wmi_cmd_hdr)); |
| 277 | hdr->command_id = cpu_to_be16(cmd); |
| 278 | hdr->seq_no = cpu_to_be16(++wmi->tx_seq_id); |
| 279 | |
Sujith Manoharan | d67ee53 | 2011-04-13 11:25:35 +0530 | [diff] [blame] | 280 | return htc_send_epid(wmi->htc, skb, wmi->ctrl_epid); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | int ath9k_wmi_cmd(struct wmi *wmi, enum wmi_cmd_id cmd_id, |
| 284 | u8 *cmd_buf, u32 cmd_len, |
| 285 | u8 *rsp_buf, u32 rsp_len, |
| 286 | u32 timeout) |
| 287 | { |
| 288 | struct ath_hw *ah = wmi->drv_priv->ah; |
| 289 | struct ath_common *common = ath9k_hw_common(ah); |
| 290 | u16 headroom = sizeof(struct htc_frame_hdr) + |
| 291 | sizeof(struct wmi_cmd_hdr); |
| 292 | struct sk_buff *skb; |
| 293 | u8 *data; |
| 294 | int time_left, ret = 0; |
Sujith | 6ce34ec | 2010-04-16 11:54:01 +0530 | [diff] [blame] | 295 | unsigned long flags; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 296 | |
Sujith Manoharan | 97dcec5 | 2010-12-20 08:02:42 +0530 | [diff] [blame] | 297 | if (ah->ah_flags & AH_UNPLUGGED) |
Sujith | a3be14b | 2010-04-23 10:28:10 +0530 | [diff] [blame] | 298 | return 0; |
| 299 | |
Ming Lei | 0fa35a5 | 2010-04-13 00:29:15 +0800 | [diff] [blame] | 300 | skb = alloc_skb(headroom + cmd_len, GFP_ATOMIC); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 301 | if (!skb) |
| 302 | return -ENOMEM; |
| 303 | |
| 304 | skb_reserve(skb, headroom); |
| 305 | |
| 306 | if (cmd_len != 0 && cmd_buf != NULL) { |
| 307 | data = (u8 *) skb_put(skb, cmd_len); |
| 308 | memcpy(data, cmd_buf, cmd_len); |
| 309 | } |
| 310 | |
| 311 | mutex_lock(&wmi->op_mutex); |
| 312 | |
| 313 | /* check if wmi stopped flag is set */ |
| 314 | if (unlikely(wmi->stopped)) { |
| 315 | ret = -EPROTO; |
| 316 | goto out; |
| 317 | } |
| 318 | |
| 319 | /* record the rsp buffer and length */ |
| 320 | wmi->cmd_rsp_buf = rsp_buf; |
| 321 | wmi->cmd_rsp_len = rsp_len; |
| 322 | |
Sujith | 6ce34ec | 2010-04-16 11:54:01 +0530 | [diff] [blame] | 323 | spin_lock_irqsave(&wmi->wmi_lock, flags); |
| 324 | wmi->last_cmd_id = cmd_id; |
| 325 | spin_unlock_irqrestore(&wmi->wmi_lock, flags); |
| 326 | |
Sujith | 0d36d71 | 2010-04-23 10:28:19 +0530 | [diff] [blame] | 327 | ret = ath9k_wmi_cmd_issue(wmi, skb, cmd_id, cmd_len); |
| 328 | if (ret) |
| 329 | goto out; |
| 330 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 331 | time_left = wait_for_completion_timeout(&wmi->cmd_wait, timeout); |
| 332 | if (!time_left) { |
Joe Perches | d2182b6 | 2011-12-15 14:55:53 -0800 | [diff] [blame] | 333 | ath_dbg(common, WMI, "Timeout waiting for WMI command: %s\n", |
Joe Perches | 226afe6 | 2010-12-02 19:12:37 -0800 | [diff] [blame] | 334 | wmi_cmd_to_name(cmd_id)); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 335 | mutex_unlock(&wmi->op_mutex); |
| 336 | return -ETIMEDOUT; |
| 337 | } |
| 338 | |
| 339 | mutex_unlock(&wmi->op_mutex); |
| 340 | |
| 341 | return 0; |
| 342 | |
| 343 | out: |
Joe Perches | d2182b6 | 2011-12-15 14:55:53 -0800 | [diff] [blame] | 344 | ath_dbg(common, WMI, "WMI failure for: %s\n", wmi_cmd_to_name(cmd_id)); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 345 | mutex_unlock(&wmi->op_mutex); |
Ming Lei | 0fa35a5 | 2010-04-13 00:29:15 +0800 | [diff] [blame] | 346 | kfree_skb(skb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 347 | |
| 348 | return ret; |
| 349 | } |