Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 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 | |
| 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"; |
| 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_RX_LINK_CMDID: |
| 31 | return "WMI_RX_LINK_CMDID"; |
| 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"; |
| 38 | case WMI_STOP_DMA_RECV_CMDID: |
| 39 | return "WMI_STOP_DMA_RECV_CMDID"; |
| 40 | case WMI_ABORT_TX_DMA_CMDID: |
| 41 | return "WMI_ABORT_TX_DMA_CMDID"; |
| 42 | case WMI_DRAIN_TXQ_CMDID: |
| 43 | return "WMI_DRAIN_TXQ_CMDID"; |
| 44 | case WMI_DRAIN_TXQ_ALL_CMDID: |
| 45 | return "WMI_DRAIN_TXQ_ALL_CMDID"; |
| 46 | case WMI_START_RECV_CMDID: |
| 47 | return "WMI_START_RECV_CMDID"; |
| 48 | case WMI_STOP_RECV_CMDID: |
| 49 | return "WMI_STOP_RECV_CMDID"; |
| 50 | case WMI_FLUSH_RECV_CMDID: |
| 51 | return "WMI_FLUSH_RECV_CMDID"; |
| 52 | case WMI_SET_MODE_CMDID: |
| 53 | return "WMI_SET_MODE_CMDID"; |
| 54 | case WMI_RESET_CMDID: |
| 55 | return "WMI_RESET_CMDID"; |
| 56 | case WMI_NODE_CREATE_CMDID: |
| 57 | return "WMI_NODE_CREATE_CMDID"; |
| 58 | case WMI_NODE_REMOVE_CMDID: |
| 59 | return "WMI_NODE_REMOVE_CMDID"; |
| 60 | case WMI_VAP_REMOVE_CMDID: |
| 61 | return "WMI_VAP_REMOVE_CMDID"; |
| 62 | case WMI_VAP_CREATE_CMDID: |
| 63 | return "WMI_VAP_CREATE_CMDID"; |
| 64 | case WMI_BEACON_UPDATE_CMDID: |
| 65 | return "WMI_BEACON_UPDATE_CMDID"; |
| 66 | case WMI_REG_READ_CMDID: |
| 67 | return "WMI_REG_READ_CMDID"; |
| 68 | case WMI_REG_WRITE_CMDID: |
| 69 | return "WMI_REG_WRITE_CMDID"; |
| 70 | case WMI_RC_STATE_CHANGE_CMDID: |
| 71 | return "WMI_RC_STATE_CHANGE_CMDID"; |
| 72 | case WMI_RC_RATE_UPDATE_CMDID: |
| 73 | return "WMI_RC_RATE_UPDATE_CMDID"; |
| 74 | case WMI_DEBUG_INFO_CMDID: |
| 75 | return "WMI_DEBUG_INFO_CMDID"; |
| 76 | case WMI_HOST_ATTACH: |
| 77 | return "WMI_HOST_ATTACH"; |
| 78 | case WMI_TARGET_IC_UPDATE_CMDID: |
| 79 | return "WMI_TARGET_IC_UPDATE_CMDID"; |
| 80 | case WMI_TGT_STATS_CMDID: |
| 81 | return "WMI_TGT_STATS_CMDID"; |
| 82 | case WMI_TX_AGGR_ENABLE_CMDID: |
| 83 | return "WMI_TX_AGGR_ENABLE_CMDID"; |
| 84 | case WMI_TGT_DETACH_CMDID: |
| 85 | return "WMI_TGT_DETACH_CMDID"; |
| 86 | case WMI_TGT_TXQ_ENABLE_CMDID: |
| 87 | return "WMI_TGT_TXQ_ENABLE_CMDID"; |
| 88 | } |
| 89 | |
| 90 | return "Bogus"; |
| 91 | } |
| 92 | |
| 93 | struct wmi *ath9k_init_wmi(struct ath9k_htc_priv *priv) |
| 94 | { |
| 95 | struct wmi *wmi; |
| 96 | |
| 97 | wmi = kzalloc(sizeof(struct wmi), GFP_KERNEL); |
| 98 | if (!wmi) |
| 99 | return NULL; |
| 100 | |
| 101 | wmi->drv_priv = priv; |
| 102 | wmi->stopped = false; |
| 103 | mutex_init(&wmi->op_mutex); |
| 104 | init_completion(&wmi->cmd_wait); |
| 105 | |
| 106 | return wmi; |
| 107 | } |
| 108 | |
| 109 | void ath9k_deinit_wmi(struct ath9k_htc_priv *priv) |
| 110 | { |
| 111 | struct wmi *wmi = priv->wmi; |
| 112 | |
| 113 | mutex_lock(&wmi->op_mutex); |
| 114 | wmi->stopped = true; |
| 115 | mutex_unlock(&wmi->op_mutex); |
| 116 | |
| 117 | kfree(priv->wmi); |
| 118 | } |
| 119 | |
| 120 | void ath9k_wmi_tasklet(unsigned long data) |
| 121 | { |
| 122 | struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data; |
| 123 | struct ath_common *common = ath9k_hw_common(priv->ah); |
| 124 | struct wmi_cmd_hdr *hdr; |
| 125 | struct wmi_swba *swba_hdr; |
| 126 | enum wmi_event_id event; |
| 127 | struct sk_buff *skb; |
| 128 | void *wmi_event; |
| 129 | unsigned long flags; |
| 130 | #ifdef CONFIG_ATH9K_HTC_DEBUGFS |
| 131 | u32 txrate; |
| 132 | #endif |
| 133 | |
| 134 | spin_lock_irqsave(&priv->wmi->wmi_lock, flags); |
| 135 | skb = priv->wmi->wmi_skb; |
| 136 | spin_unlock_irqrestore(&priv->wmi->wmi_lock, flags); |
| 137 | |
| 138 | hdr = (struct wmi_cmd_hdr *) skb->data; |
| 139 | event = be16_to_cpu(hdr->command_id); |
| 140 | wmi_event = skb_pull(skb, sizeof(struct wmi_cmd_hdr)); |
| 141 | |
| 142 | ath_print(common, ATH_DBG_WMI, |
| 143 | "WMI Event: 0x%x\n", event); |
| 144 | |
| 145 | switch (event) { |
| 146 | case WMI_TGT_RDY_EVENTID: |
| 147 | break; |
| 148 | case WMI_SWBA_EVENTID: |
| 149 | swba_hdr = (struct wmi_swba *) wmi_event; |
| 150 | ath9k_htc_swba(priv, swba_hdr->beacon_pending); |
| 151 | break; |
| 152 | case WMI_FATAL_EVENTID: |
| 153 | break; |
| 154 | case WMI_TXTO_EVENTID: |
| 155 | break; |
| 156 | case WMI_BMISS_EVENTID: |
| 157 | break; |
| 158 | case WMI_WLAN_TXCOMP_EVENTID: |
| 159 | break; |
| 160 | case WMI_DELBA_EVENTID: |
| 161 | break; |
| 162 | case WMI_TXRATE_EVENTID: |
| 163 | #ifdef CONFIG_ATH9K_HTC_DEBUGFS |
| 164 | txrate = ((struct wmi_event_txrate *)wmi_event)->txrate; |
| 165 | priv->debug.txrate = be32_to_cpu(txrate); |
| 166 | #endif |
| 167 | break; |
| 168 | default: |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | dev_kfree_skb_any(skb); |
| 173 | } |
| 174 | |
| 175 | static void ath9k_wmi_rsp_callback(struct wmi *wmi, struct sk_buff *skb) |
| 176 | { |
| 177 | skb_pull(skb, sizeof(struct wmi_cmd_hdr)); |
| 178 | |
| 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 | |
| 182 | complete(&wmi->cmd_wait); |
| 183 | } |
| 184 | |
| 185 | static void ath9k_wmi_ctrl_rx(void *priv, struct sk_buff *skb, |
| 186 | enum htc_endpoint_id epid) |
| 187 | { |
| 188 | struct wmi *wmi = (struct wmi *) priv; |
| 189 | struct wmi_cmd_hdr *hdr; |
| 190 | u16 cmd_id; |
| 191 | |
| 192 | if (unlikely(wmi->stopped)) |
| 193 | goto free_skb; |
| 194 | |
| 195 | hdr = (struct wmi_cmd_hdr *) skb->data; |
| 196 | cmd_id = be16_to_cpu(hdr->command_id); |
| 197 | |
| 198 | if (cmd_id & 0x1000) { |
| 199 | spin_lock(&wmi->wmi_lock); |
| 200 | wmi->wmi_skb = skb; |
| 201 | spin_unlock(&wmi->wmi_lock); |
| 202 | tasklet_schedule(&wmi->drv_priv->wmi_tasklet); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | /* WMI command response */ |
| 207 | ath9k_wmi_rsp_callback(wmi, skb); |
| 208 | |
| 209 | free_skb: |
| 210 | dev_kfree_skb_any(skb); |
| 211 | } |
| 212 | |
| 213 | static void ath9k_wmi_ctrl_tx(void *priv, struct sk_buff *skb, |
| 214 | enum htc_endpoint_id epid, bool txok) |
| 215 | { |
| 216 | dev_kfree_skb_any(skb); |
| 217 | } |
| 218 | |
| 219 | int ath9k_wmi_connect(struct htc_target *htc, struct wmi *wmi, |
| 220 | enum htc_endpoint_id *wmi_ctrl_epid) |
| 221 | { |
| 222 | struct htc_service_connreq connect; |
| 223 | int ret; |
| 224 | |
| 225 | wmi->htc = htc; |
| 226 | |
| 227 | memset(&connect, 0, sizeof(connect)); |
| 228 | |
| 229 | connect.ep_callbacks.priv = wmi; |
| 230 | connect.ep_callbacks.tx = ath9k_wmi_ctrl_tx; |
| 231 | connect.ep_callbacks.rx = ath9k_wmi_ctrl_rx; |
| 232 | connect.service_id = WMI_CONTROL_SVC; |
| 233 | |
| 234 | ret = htc_connect_service(htc, &connect, &wmi->ctrl_epid); |
| 235 | if (ret) |
| 236 | return ret; |
| 237 | |
| 238 | *wmi_ctrl_epid = wmi->ctrl_epid; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int ath9k_wmi_cmd_issue(struct wmi *wmi, |
| 244 | struct sk_buff *skb, |
| 245 | enum wmi_cmd_id cmd, u16 len) |
| 246 | { |
| 247 | struct wmi_cmd_hdr *hdr; |
| 248 | |
| 249 | hdr = (struct wmi_cmd_hdr *) skb_push(skb, sizeof(struct wmi_cmd_hdr)); |
| 250 | hdr->command_id = cpu_to_be16(cmd); |
| 251 | hdr->seq_no = cpu_to_be16(++wmi->tx_seq_id); |
| 252 | |
| 253 | return htc_send(wmi->htc, skb, wmi->ctrl_epid, NULL); |
| 254 | } |
| 255 | |
| 256 | int ath9k_wmi_cmd(struct wmi *wmi, enum wmi_cmd_id cmd_id, |
| 257 | u8 *cmd_buf, u32 cmd_len, |
| 258 | u8 *rsp_buf, u32 rsp_len, |
| 259 | u32 timeout) |
| 260 | { |
| 261 | struct ath_hw *ah = wmi->drv_priv->ah; |
| 262 | struct ath_common *common = ath9k_hw_common(ah); |
| 263 | u16 headroom = sizeof(struct htc_frame_hdr) + |
| 264 | sizeof(struct wmi_cmd_hdr); |
| 265 | struct sk_buff *skb; |
| 266 | u8 *data; |
| 267 | int time_left, ret = 0; |
| 268 | |
| 269 | if (!wmi) |
| 270 | return -EINVAL; |
| 271 | |
| 272 | skb = dev_alloc_skb(headroom + cmd_len); |
| 273 | if (!skb) |
| 274 | return -ENOMEM; |
| 275 | |
| 276 | skb_reserve(skb, headroom); |
| 277 | |
| 278 | if (cmd_len != 0 && cmd_buf != NULL) { |
| 279 | data = (u8 *) skb_put(skb, cmd_len); |
| 280 | memcpy(data, cmd_buf, cmd_len); |
| 281 | } |
| 282 | |
| 283 | mutex_lock(&wmi->op_mutex); |
| 284 | |
| 285 | /* check if wmi stopped flag is set */ |
| 286 | if (unlikely(wmi->stopped)) { |
| 287 | ret = -EPROTO; |
| 288 | goto out; |
| 289 | } |
| 290 | |
| 291 | /* record the rsp buffer and length */ |
| 292 | wmi->cmd_rsp_buf = rsp_buf; |
| 293 | wmi->cmd_rsp_len = rsp_len; |
| 294 | |
| 295 | ret = ath9k_wmi_cmd_issue(wmi, skb, cmd_id, cmd_len); |
| 296 | if (ret) |
| 297 | goto out; |
| 298 | |
| 299 | time_left = wait_for_completion_timeout(&wmi->cmd_wait, timeout); |
| 300 | if (!time_left) { |
| 301 | ath_print(common, ATH_DBG_WMI, |
| 302 | "Timeout waiting for WMI command: %s\n", |
| 303 | wmi_cmd_to_name(cmd_id)); |
| 304 | mutex_unlock(&wmi->op_mutex); |
| 305 | return -ETIMEDOUT; |
| 306 | } |
| 307 | |
| 308 | mutex_unlock(&wmi->op_mutex); |
| 309 | |
| 310 | return 0; |
| 311 | |
| 312 | out: |
| 313 | ath_print(common, ATH_DBG_WMI, |
| 314 | "WMI failure for: %s\n", wmi_cmd_to_name(cmd_id)); |
| 315 | mutex_unlock(&wmi->op_mutex); |
| 316 | dev_kfree_skb_any(skb); |
| 317 | |
| 318 | return ret; |
| 319 | } |