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