1 /* 2 * Copyright (c) 2004-2011 Atheros Communications Inc. 3 * Copyright (c) 2011-2012 Qualcomm Atheros, Inc. 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include "core.h" 19 20 #include <linux/module.h> 21 #include <linux/moduleparam.h> 22 #include <linux/export.h> 23 #include <linux/vmalloc.h> 24 25 #include "debug.h" 26 #include "hif-ops.h" 27 #include "htc-ops.h" 28 #include "cfg80211.h" 29 30 unsigned int debug_mask; 31 static unsigned int suspend_mode; 32 static unsigned int wow_mode; 33 static unsigned int uart_debug; 34 static unsigned int ath6kl_p2p; 35 static unsigned int testmode; 36 37 module_param(debug_mask, uint, 0644); 38 module_param(suspend_mode, uint, 0644); 39 module_param(wow_mode, uint, 0644); 40 module_param(uart_debug, uint, 0644); 41 module_param(ath6kl_p2p, uint, 0644); 42 module_param(testmode, uint, 0644); 43 44 void ath6kl_core_tx_complete(struct ath6kl *ar, struct sk_buff *skb) 45 { 46 ath6kl_htc_tx_complete(ar, skb); 47 } 48 EXPORT_SYMBOL(ath6kl_core_tx_complete); 49 50 void ath6kl_core_rx_complete(struct ath6kl *ar, struct sk_buff *skb, u8 pipe) 51 { 52 ath6kl_htc_rx_complete(ar, skb, pipe); 53 } 54 EXPORT_SYMBOL(ath6kl_core_rx_complete); 55 56 int ath6kl_core_init(struct ath6kl *ar, enum ath6kl_htc_type htc_type) 57 { 58 struct ath6kl_bmi_target_info targ_info; 59 struct wireless_dev *wdev; 60 int ret = 0, i; 61 62 switch (htc_type) { 63 case ATH6KL_HTC_TYPE_MBOX: 64 ath6kl_htc_mbox_attach(ar); 65 break; 66 case ATH6KL_HTC_TYPE_PIPE: 67 ath6kl_htc_pipe_attach(ar); 68 break; 69 default: 70 WARN_ON(1); 71 return -ENOMEM; 72 } 73 74 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl"); 75 if (!ar->ath6kl_wq) 76 return -ENOMEM; 77 78 ret = ath6kl_bmi_init(ar); 79 if (ret) 80 goto err_wq; 81 82 /* 83 * Turn on power to get hardware (target) version and leave power 84 * on delibrately as we will boot the hardware anyway within few 85 * seconds. 86 */ 87 ret = ath6kl_hif_power_on(ar); 88 if (ret) 89 goto err_bmi_cleanup; 90 91 ret = ath6kl_bmi_get_target_info(ar, &targ_info); 92 if (ret) 93 goto err_power_off; 94 95 ar->version.target_ver = le32_to_cpu(targ_info.version); 96 ar->target_type = le32_to_cpu(targ_info.type); 97 ar->wiphy->hw_version = le32_to_cpu(targ_info.version); 98 99 ret = ath6kl_init_hw_params(ar); 100 if (ret) 101 goto err_power_off; 102 103 ar->htc_target = ath6kl_htc_create(ar); 104 105 if (!ar->htc_target) { 106 ret = -ENOMEM; 107 goto err_power_off; 108 } 109 110 ar->testmode = testmode; 111 112 ret = ath6kl_init_fetch_firmwares(ar); 113 if (ret) 114 goto err_htc_cleanup; 115 116 /* FIXME: we should free all firmwares in the error cases below */ 117 118 /* Indicate that WMI is enabled (although not ready yet) */ 119 set_bit(WMI_ENABLED, &ar->flag); 120 ar->wmi = ath6kl_wmi_init(ar); 121 if (!ar->wmi) { 122 ath6kl_err("failed to initialize wmi\n"); 123 ret = -EIO; 124 goto err_htc_cleanup; 125 } 126 127 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi); 128 129 /* setup access class priority mappings */ 130 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */ 131 ar->ac_stream_pri_map[WMM_AC_BE] = 1; 132 ar->ac_stream_pri_map[WMM_AC_VI] = 2; 133 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */ 134 135 /* allocate some buffers that handle larger AMSDU frames */ 136 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS); 137 138 ath6kl_cookie_init(ar); 139 140 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER | 141 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST; 142 143 if (suspend_mode && 144 suspend_mode >= WLAN_POWER_STATE_CUT_PWR && 145 suspend_mode <= WLAN_POWER_STATE_WOW) 146 ar->suspend_mode = suspend_mode; 147 else 148 ar->suspend_mode = 0; 149 150 if (suspend_mode == WLAN_POWER_STATE_WOW && 151 (wow_mode == WLAN_POWER_STATE_CUT_PWR || 152 wow_mode == WLAN_POWER_STATE_DEEP_SLEEP)) 153 ar->wow_suspend_mode = wow_mode; 154 else 155 ar->wow_suspend_mode = 0; 156 157 if (uart_debug) 158 ar->conf_flags |= ATH6KL_CONF_UART_DEBUG; 159 160 set_bit(FIRST_BOOT, &ar->flag); 161 162 ath6kl_debug_init(ar); 163 164 ret = ath6kl_init_hw_start(ar); 165 if (ret) { 166 ath6kl_err("Failed to start hardware: %d\n", ret); 167 goto err_rxbuf_cleanup; 168 } 169 170 /* give our connected endpoints some buffers */ 171 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep); 172 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]); 173 174 ret = ath6kl_cfg80211_init(ar); 175 if (ret) 176 goto err_rxbuf_cleanup; 177 178 ret = ath6kl_debug_init_fs(ar); 179 if (ret) { 180 wiphy_unregister(ar->wiphy); 181 goto err_rxbuf_cleanup; 182 } 183 184 for (i = 0; i < ar->vif_max; i++) 185 ar->avail_idx_map |= BIT(i); 186 187 rtnl_lock(); 188 189 /* Add an initial station interface */ 190 wdev = ath6kl_interface_add(ar, "wlan%d", NL80211_IFTYPE_STATION, 0, 191 INFRA_NETWORK); 192 193 rtnl_unlock(); 194 195 if (!wdev) { 196 ath6kl_err("Failed to instantiate a network device\n"); 197 ret = -ENOMEM; 198 wiphy_unregister(ar->wiphy); 199 goto err_rxbuf_cleanup; 200 } 201 202 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n", 203 __func__, wdev->netdev->name, wdev->netdev, ar); 204 205 return ret; 206 207 err_rxbuf_cleanup: 208 ath6kl_debug_cleanup(ar); 209 ath6kl_htc_flush_rx_buf(ar->htc_target); 210 ath6kl_cleanup_amsdu_rxbufs(ar); 211 ath6kl_wmi_shutdown(ar->wmi); 212 clear_bit(WMI_ENABLED, &ar->flag); 213 ar->wmi = NULL; 214 err_htc_cleanup: 215 ath6kl_htc_cleanup(ar->htc_target); 216 err_power_off: 217 ath6kl_hif_power_off(ar); 218 err_bmi_cleanup: 219 ath6kl_bmi_cleanup(ar); 220 err_wq: 221 destroy_workqueue(ar->ath6kl_wq); 222 223 return ret; 224 } 225 EXPORT_SYMBOL(ath6kl_core_init); 226 227 struct ath6kl *ath6kl_core_create(struct device *dev) 228 { 229 struct ath6kl *ar; 230 u8 ctr; 231 232 ar = ath6kl_cfg80211_create(); 233 if (!ar) 234 return NULL; 235 236 ar->p2p = !!ath6kl_p2p; 237 ar->dev = dev; 238 239 ar->vif_max = 1; 240 241 ar->max_norm_iface = 1; 242 243 spin_lock_init(&ar->lock); 244 spin_lock_init(&ar->mcastpsq_lock); 245 spin_lock_init(&ar->list_lock); 246 247 init_waitqueue_head(&ar->event_wq); 248 sema_init(&ar->sem, 1); 249 250 INIT_LIST_HEAD(&ar->amsdu_rx_buffer_queue); 251 INIT_LIST_HEAD(&ar->vif_list); 252 253 clear_bit(WMI_ENABLED, &ar->flag); 254 clear_bit(SKIP_SCAN, &ar->flag); 255 clear_bit(DESTROY_IN_PROGRESS, &ar->flag); 256 257 ar->tx_pwr = 0; 258 ar->intra_bss = 1; 259 ar->lrssi_roam_threshold = DEF_LRSSI_ROAM_THRESHOLD; 260 261 ar->state = ATH6KL_STATE_OFF; 262 263 memset((u8 *)ar->sta_list, 0, 264 AP_MAX_NUM_STA * sizeof(struct ath6kl_sta)); 265 266 /* Init the PS queues */ 267 for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) { 268 spin_lock_init(&ar->sta_list[ctr].psq_lock); 269 skb_queue_head_init(&ar->sta_list[ctr].psq); 270 skb_queue_head_init(&ar->sta_list[ctr].apsdq); 271 ar->sta_list[ctr].mgmt_psq_len = 0; 272 INIT_LIST_HEAD(&ar->sta_list[ctr].mgmt_psq); 273 ar->sta_list[ctr].aggr_conn = 274 kzalloc(sizeof(struct aggr_info_conn), GFP_KERNEL); 275 if (!ar->sta_list[ctr].aggr_conn) { 276 ath6kl_err("Failed to allocate memory for sta aggregation information\n"); 277 ath6kl_core_destroy(ar); 278 return NULL; 279 } 280 } 281 282 skb_queue_head_init(&ar->mcastpsq); 283 284 memcpy(ar->ap_country_code, DEF_AP_COUNTRY_CODE, 3); 285 286 return ar; 287 } 288 EXPORT_SYMBOL(ath6kl_core_create); 289 290 void ath6kl_core_cleanup(struct ath6kl *ar) 291 { 292 ath6kl_hif_power_off(ar); 293 294 destroy_workqueue(ar->ath6kl_wq); 295 296 if (ar->htc_target) 297 ath6kl_htc_cleanup(ar->htc_target); 298 299 ath6kl_cookie_cleanup(ar); 300 301 ath6kl_cleanup_amsdu_rxbufs(ar); 302 303 ath6kl_bmi_cleanup(ar); 304 305 ath6kl_debug_cleanup(ar); 306 307 kfree(ar->fw_board); 308 kfree(ar->fw_otp); 309 vfree(ar->fw); 310 kfree(ar->fw_patch); 311 kfree(ar->fw_testscript); 312 313 ath6kl_cfg80211_cleanup(ar); 314 } 315 EXPORT_SYMBOL(ath6kl_core_cleanup); 316 317 void ath6kl_core_destroy(struct ath6kl *ar) 318 { 319 ath6kl_cfg80211_destroy(ar); 320 } 321 EXPORT_SYMBOL(ath6kl_core_destroy); 322 323 MODULE_AUTHOR("Qualcomm Atheros"); 324 MODULE_DESCRIPTION("Core module for AR600x SDIO and USB devices."); 325 MODULE_LICENSE("Dual BSD/GPL"); 326