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 static unsigned int recovery_enable;
37 static unsigned int heart_beat_poll;
38 
39 module_param(debug_mask, uint, 0644);
40 module_param(suspend_mode, uint, 0644);
41 module_param(wow_mode, uint, 0644);
42 module_param(uart_debug, uint, 0644);
43 module_param(ath6kl_p2p, uint, 0644);
44 module_param(testmode, uint, 0644);
45 module_param(recovery_enable, uint, 0644);
46 module_param(heart_beat_poll, uint, 0644);
47 MODULE_PARM_DESC(recovery_enable, "Enable recovery from firmware error");
48 MODULE_PARM_DESC(heart_beat_poll, "Enable fw error detection periodic"   \
49 		 "polling. This also specifies the polling interval in"  \
50 		 "msecs. Set reocvery_enable for this to be effective");
51 
52 void ath6kl_core_tx_complete(struct ath6kl *ar, struct sk_buff *skb)
53 {
54 	ath6kl_htc_tx_complete(ar, skb);
55 }
56 EXPORT_SYMBOL(ath6kl_core_tx_complete);
57 
58 void ath6kl_core_rx_complete(struct ath6kl *ar, struct sk_buff *skb, u8 pipe)
59 {
60 	ath6kl_htc_rx_complete(ar, skb, pipe);
61 }
62 EXPORT_SYMBOL(ath6kl_core_rx_complete);
63 
64 int ath6kl_core_init(struct ath6kl *ar, enum ath6kl_htc_type htc_type)
65 {
66 	struct ath6kl_bmi_target_info targ_info;
67 	struct wireless_dev *wdev;
68 	int ret = 0, i;
69 
70 	switch (htc_type) {
71 	case ATH6KL_HTC_TYPE_MBOX:
72 		ath6kl_htc_mbox_attach(ar);
73 		break;
74 	case ATH6KL_HTC_TYPE_PIPE:
75 		ath6kl_htc_pipe_attach(ar);
76 		break;
77 	default:
78 		WARN_ON(1);
79 		return -ENOMEM;
80 	}
81 
82 	ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
83 	if (!ar->ath6kl_wq)
84 		return -ENOMEM;
85 
86 	ret = ath6kl_bmi_init(ar);
87 	if (ret)
88 		goto err_wq;
89 
90 	/*
91 	 * Turn on power to get hardware (target) version and leave power
92 	 * on delibrately as we will boot the hardware anyway within few
93 	 * seconds.
94 	 */
95 	ret = ath6kl_hif_power_on(ar);
96 	if (ret)
97 		goto err_bmi_cleanup;
98 
99 	ret = ath6kl_bmi_get_target_info(ar, &targ_info);
100 	if (ret)
101 		goto err_power_off;
102 
103 	ar->version.target_ver = le32_to_cpu(targ_info.version);
104 	ar->target_type = le32_to_cpu(targ_info.type);
105 	ar->wiphy->hw_version = le32_to_cpu(targ_info.version);
106 
107 	ret = ath6kl_init_hw_params(ar);
108 	if (ret)
109 		goto err_power_off;
110 
111 	ar->htc_target = ath6kl_htc_create(ar);
112 
113 	if (!ar->htc_target) {
114 		ret = -ENOMEM;
115 		goto err_power_off;
116 	}
117 
118 	ar->testmode = testmode;
119 
120 	ret = ath6kl_init_fetch_firmwares(ar);
121 	if (ret)
122 		goto err_htc_cleanup;
123 
124 	/* FIXME: we should free all firmwares in the error cases below */
125 
126 	/* Indicate that WMI is enabled (although not ready yet) */
127 	set_bit(WMI_ENABLED, &ar->flag);
128 	ar->wmi = ath6kl_wmi_init(ar);
129 	if (!ar->wmi) {
130 		ath6kl_err("failed to initialize wmi\n");
131 		ret = -EIO;
132 		goto err_htc_cleanup;
133 	}
134 
135 	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
136 
137 	/* setup access class priority mappings */
138 	ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest  */
139 	ar->ac_stream_pri_map[WMM_AC_BE] = 1;
140 	ar->ac_stream_pri_map[WMM_AC_VI] = 2;
141 	ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
142 
143 	/* allocate some buffers that handle larger AMSDU frames */
144 	ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
145 
146 	ath6kl_cookie_init(ar);
147 
148 	ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
149 			 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
150 
151 	if (suspend_mode &&
152 	    suspend_mode >= WLAN_POWER_STATE_CUT_PWR &&
153 	    suspend_mode <= WLAN_POWER_STATE_WOW)
154 		ar->suspend_mode = suspend_mode;
155 	else
156 		ar->suspend_mode = 0;
157 
158 	if (suspend_mode == WLAN_POWER_STATE_WOW &&
159 	    (wow_mode == WLAN_POWER_STATE_CUT_PWR ||
160 	     wow_mode == WLAN_POWER_STATE_DEEP_SLEEP))
161 		ar->wow_suspend_mode = wow_mode;
162 	else
163 		ar->wow_suspend_mode = 0;
164 
165 	if (uart_debug)
166 		ar->conf_flags |= ATH6KL_CONF_UART_DEBUG;
167 
168 	set_bit(FIRST_BOOT, &ar->flag);
169 
170 	ath6kl_debug_init(ar);
171 
172 	ret = ath6kl_init_hw_start(ar);
173 	if (ret) {
174 		ath6kl_err("Failed to start hardware: %d\n", ret);
175 		goto err_rxbuf_cleanup;
176 	}
177 
178 	/* give our connected endpoints some buffers */
179 	ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
180 	ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
181 
182 	ret = ath6kl_cfg80211_init(ar);
183 	if (ret)
184 		goto err_rxbuf_cleanup;
185 
186 	ret = ath6kl_debug_init_fs(ar);
187 	if (ret) {
188 		wiphy_unregister(ar->wiphy);
189 		goto err_rxbuf_cleanup;
190 	}
191 
192 	for (i = 0; i < ar->vif_max; i++)
193 		ar->avail_idx_map |= BIT(i);
194 
195 	rtnl_lock();
196 
197 	/* Add an initial station interface */
198 	wdev = ath6kl_interface_add(ar, "wlan%d", NL80211_IFTYPE_STATION, 0,
199 				    INFRA_NETWORK);
200 
201 	rtnl_unlock();
202 
203 	if (!wdev) {
204 		ath6kl_err("Failed to instantiate a network device\n");
205 		ret = -ENOMEM;
206 		wiphy_unregister(ar->wiphy);
207 		goto err_rxbuf_cleanup;
208 	}
209 
210 	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
211 		   __func__, wdev->netdev->name, wdev->netdev, ar);
212 
213 	ar->fw_recovery.enable = !!recovery_enable;
214 	if (!ar->fw_recovery.enable)
215 		return ret;
216 
217 	if (heart_beat_poll &&
218 	    test_bit(ATH6KL_FW_CAPABILITY_HEART_BEAT_POLL,
219 		     ar->fw_capabilities))
220 		ar->fw_recovery.hb_poll = heart_beat_poll;
221 
222 	ath6kl_recovery_init(ar);
223 
224 	return ret;
225 
226 err_rxbuf_cleanup:
227 	ath6kl_debug_cleanup(ar);
228 	ath6kl_htc_flush_rx_buf(ar->htc_target);
229 	ath6kl_cleanup_amsdu_rxbufs(ar);
230 	ath6kl_wmi_shutdown(ar->wmi);
231 	clear_bit(WMI_ENABLED, &ar->flag);
232 	ar->wmi = NULL;
233 err_htc_cleanup:
234 	ath6kl_htc_cleanup(ar->htc_target);
235 err_power_off:
236 	ath6kl_hif_power_off(ar);
237 err_bmi_cleanup:
238 	ath6kl_bmi_cleanup(ar);
239 err_wq:
240 	destroy_workqueue(ar->ath6kl_wq);
241 
242 	return ret;
243 }
244 EXPORT_SYMBOL(ath6kl_core_init);
245 
246 struct ath6kl *ath6kl_core_create(struct device *dev)
247 {
248 	struct ath6kl *ar;
249 	u8 ctr;
250 
251 	ar = ath6kl_cfg80211_create();
252 	if (!ar)
253 		return NULL;
254 
255 	ar->p2p = !!ath6kl_p2p;
256 	ar->dev = dev;
257 
258 	ar->vif_max = 1;
259 
260 	ar->max_norm_iface = 1;
261 
262 	spin_lock_init(&ar->lock);
263 	spin_lock_init(&ar->mcastpsq_lock);
264 	spin_lock_init(&ar->list_lock);
265 
266 	init_waitqueue_head(&ar->event_wq);
267 	sema_init(&ar->sem, 1);
268 
269 	INIT_LIST_HEAD(&ar->amsdu_rx_buffer_queue);
270 	INIT_LIST_HEAD(&ar->vif_list);
271 
272 	clear_bit(WMI_ENABLED, &ar->flag);
273 	clear_bit(SKIP_SCAN, &ar->flag);
274 	clear_bit(DESTROY_IN_PROGRESS, &ar->flag);
275 
276 	ar->tx_pwr = 0;
277 	ar->intra_bss = 1;
278 	ar->lrssi_roam_threshold = DEF_LRSSI_ROAM_THRESHOLD;
279 
280 	ar->state = ATH6KL_STATE_OFF;
281 
282 	memset((u8 *)ar->sta_list, 0,
283 	       AP_MAX_NUM_STA * sizeof(struct ath6kl_sta));
284 
285 	/* Init the PS queues */
286 	for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
287 		spin_lock_init(&ar->sta_list[ctr].psq_lock);
288 		skb_queue_head_init(&ar->sta_list[ctr].psq);
289 		skb_queue_head_init(&ar->sta_list[ctr].apsdq);
290 		ar->sta_list[ctr].mgmt_psq_len = 0;
291 		INIT_LIST_HEAD(&ar->sta_list[ctr].mgmt_psq);
292 		ar->sta_list[ctr].aggr_conn =
293 			kzalloc(sizeof(struct aggr_info_conn), GFP_KERNEL);
294 		if (!ar->sta_list[ctr].aggr_conn) {
295 			ath6kl_err("Failed to allocate memory for sta aggregation information\n");
296 			ath6kl_core_destroy(ar);
297 			return NULL;
298 		}
299 	}
300 
301 	skb_queue_head_init(&ar->mcastpsq);
302 
303 	memcpy(ar->ap_country_code, DEF_AP_COUNTRY_CODE, 3);
304 
305 	return ar;
306 }
307 EXPORT_SYMBOL(ath6kl_core_create);
308 
309 void ath6kl_core_cleanup(struct ath6kl *ar)
310 {
311 	ath6kl_hif_power_off(ar);
312 
313 	ath6kl_recovery_cleanup(ar);
314 
315 	destroy_workqueue(ar->ath6kl_wq);
316 
317 	if (ar->htc_target)
318 		ath6kl_htc_cleanup(ar->htc_target);
319 
320 	ath6kl_cookie_cleanup(ar);
321 
322 	ath6kl_cleanup_amsdu_rxbufs(ar);
323 
324 	ath6kl_bmi_cleanup(ar);
325 
326 	ath6kl_debug_cleanup(ar);
327 
328 	kfree(ar->fw_board);
329 	kfree(ar->fw_otp);
330 	vfree(ar->fw);
331 	kfree(ar->fw_patch);
332 	kfree(ar->fw_testscript);
333 
334 	ath6kl_cfg80211_cleanup(ar);
335 }
336 EXPORT_SYMBOL(ath6kl_core_cleanup);
337 
338 void ath6kl_core_destroy(struct ath6kl *ar)
339 {
340 	ath6kl_cfg80211_destroy(ar);
341 }
342 EXPORT_SYMBOL(ath6kl_core_destroy);
343 
344 MODULE_AUTHOR("Qualcomm Atheros");
345 MODULE_DESCRIPTION("Core module for AR600x SDIO and USB devices.");
346 MODULE_LICENSE("Dual BSD/GPL");
347