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