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