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