blob: 83b4f165eebf6d0dc195f3df6d5a3f19fcd87adb [file] [log] [blame]
Kalle Valobdcd8172011-07-18 00:22:30 +03001
2/*
3 * Copyright (c) 2011 Atheros Communications 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
Stephen Rothwellc6efe5782011-09-28 18:32:34 +100018#include <linux/moduleparam.h>
Sangwook Leef7830202011-10-26 16:28:38 +010019#include <linux/errno.h>
Sam Leffler92ecbff2011-09-07 10:55:16 +030020#include <linux/of.h>
Kalle Valobdcd8172011-07-18 00:22:30 +030021#include <linux/mmc/sdio_func.h>
22#include "core.h"
23#include "cfg80211.h"
24#include "target.h"
25#include "debug.h"
26#include "hif-ops.h"
27
28unsigned int debug_mask;
Kalle Valo003353b0d2011-09-01 10:14:21 +030029static unsigned int testmode;
Kalle Valobdcd8172011-07-18 00:22:30 +030030
31module_param(debug_mask, uint, 0644);
Kalle Valo003353b0d2011-09-01 10:14:21 +030032module_param(testmode, uint, 0644);
Kalle Valobdcd8172011-07-18 00:22:30 +030033
34/*
35 * Include definitions here that can be used to tune the WLAN module
36 * behavior. Different customers can tune the behavior as per their needs,
37 * here.
38 */
39
40/*
41 * This configuration item enable/disable keepalive support.
42 * Keepalive support: In the absence of any data traffic to AP, null
43 * frames will be sent to the AP at periodic interval, to keep the association
44 * active. This configuration item defines the periodic interval.
45 * Use value of zero to disable keepalive support
46 * Default: 60 seconds
47 */
48#define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
49
50/*
51 * This configuration item sets the value of disconnect timeout
52 * Firmware delays sending the disconnec event to the host for this
53 * timeout after is gets disconnected from the current AP.
54 * If the firmware successly roams within the disconnect timeout
55 * it sends a new connect event
56 */
57#define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
58
59#define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
60
Kalle Valobdcd8172011-07-18 00:22:30 +030061#define ATH6KL_DATA_OFFSET 64
62struct sk_buff *ath6kl_buf_alloc(int size)
63{
64 struct sk_buff *skb;
65 u16 reserved;
66
67 /* Add chacheline space at front and back of buffer */
68 reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
Vasanthakumar Thiagarajan1df94a82011-08-17 18:45:10 +053069 sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
Kalle Valobdcd8172011-07-18 00:22:30 +030070 skb = dev_alloc_skb(size + reserved);
71
72 if (skb)
73 skb_reserve(skb, reserved - L1_CACHE_BYTES);
74 return skb;
75}
76
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +053077void ath6kl_init_profile_info(struct ath6kl_vif *vif)
Kalle Valobdcd8172011-07-18 00:22:30 +030078{
Vasanthakumar Thiagarajan34503342011-10-25 19:34:02 +053079 vif->ssid_len = 0;
80 memset(vif->ssid, 0, sizeof(vif->ssid));
81
82 vif->dot11_auth_mode = OPEN_AUTH;
83 vif->auth_mode = NONE_AUTH;
84 vif->prwise_crypto = NONE_CRYPT;
85 vif->prwise_crypto_len = 0;
86 vif->grp_crypto = NONE_CRYPT;
87 vif->grp_crypto_len = 0;
Vasanthakumar Thiagarajan6f2a73f2011-10-25 19:34:06 +053088 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
Vasanthakumar Thiagarajan8c8b65e2011-10-25 19:34:04 +053089 memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
90 memset(vif->bssid, 0, sizeof(vif->bssid));
Vasanthakumar Thiagarajanf74bac52011-10-25 19:34:05 +053091 vif->bss_ch = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +030092}
93
Kalle Valobdcd8172011-07-18 00:22:30 +030094static int ath6kl_set_host_app_area(struct ath6kl *ar)
95{
96 u32 address, data;
97 struct host_app_area host_app_area;
98
99 /* Fetch the address of the host_app_area_s
100 * instance in the host interest area */
101 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
Kevin Fang31024d92011-07-11 17:14:13 +0800102 address = TARG_VTOP(ar->target_type, address);
Kalle Valobdcd8172011-07-18 00:22:30 +0300103
Kalle Valoaddb44b2011-09-02 10:32:05 +0300104 if (ath6kl_diag_read32(ar, address, &data))
Kalle Valobdcd8172011-07-18 00:22:30 +0300105 return -EIO;
106
Kevin Fang31024d92011-07-11 17:14:13 +0800107 address = TARG_VTOP(ar->target_type, data);
Kalle Valocbf49a62011-10-05 12:23:17 +0300108 host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
Kalle Valoaddb44b2011-09-02 10:32:05 +0300109 if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
110 sizeof(struct host_app_area)))
Kalle Valobdcd8172011-07-18 00:22:30 +0300111 return -EIO;
112
113 return 0;
114}
115
116static inline void set_ac2_ep_map(struct ath6kl *ar,
117 u8 ac,
118 enum htc_endpoint_id ep)
119{
120 ar->ac2ep_map[ac] = ep;
121 ar->ep2ac_map[ep] = ac;
122}
123
124/* connect to a service */
125static int ath6kl_connectservice(struct ath6kl *ar,
126 struct htc_service_connect_req *con_req,
127 char *desc)
128{
129 int status;
130 struct htc_service_connect_resp response;
131
132 memset(&response, 0, sizeof(response));
133
Kalle Vaload226ec2011-08-10 09:49:12 +0300134 status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
Kalle Valobdcd8172011-07-18 00:22:30 +0300135 if (status) {
136 ath6kl_err("failed to connect to %s service status:%d\n",
137 desc, status);
138 return status;
139 }
140
141 switch (con_req->svc_id) {
142 case WMI_CONTROL_SVC:
143 if (test_bit(WMI_ENABLED, &ar->flag))
144 ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
145 ar->ctrl_ep = response.endpoint;
146 break;
147 case WMI_DATA_BE_SVC:
148 set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
149 break;
150 case WMI_DATA_BK_SVC:
151 set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
152 break;
153 case WMI_DATA_VI_SVC:
154 set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
155 break;
156 case WMI_DATA_VO_SVC:
157 set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
158 break;
159 default:
160 ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
161 return -EINVAL;
162 }
163
164 return 0;
165}
166
167static int ath6kl_init_service_ep(struct ath6kl *ar)
168{
169 struct htc_service_connect_req connect;
170
171 memset(&connect, 0, sizeof(connect));
172
173 /* these fields are the same for all service endpoints */
174 connect.ep_cb.rx = ath6kl_rx;
175 connect.ep_cb.rx_refill = ath6kl_rx_refill;
176 connect.ep_cb.tx_full = ath6kl_tx_queue_full;
177
178 /*
179 * Set the max queue depth so that our ath6kl_tx_queue_full handler
180 * gets called.
181 */
182 connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
183 connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
184 if (!connect.ep_cb.rx_refill_thresh)
185 connect.ep_cb.rx_refill_thresh++;
186
187 /* connect to control service */
188 connect.svc_id = WMI_CONTROL_SVC;
189 if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
190 return -EIO;
191
192 connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
193
194 /*
195 * Limit the HTC message size on the send path, although e can
196 * receive A-MSDU frames of 4K, we will only send ethernet-sized
197 * (802.3) frames on the send path.
198 */
199 connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
200
201 /*
202 * To reduce the amount of committed memory for larger A_MSDU
203 * frames, use the recv-alloc threshold mechanism for larger
204 * packets.
205 */
206 connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
207 connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
208
209 /*
210 * For the remaining data services set the connection flag to
211 * reduce dribbling, if configured to do so.
212 */
213 connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
214 connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
215 connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
216
217 connect.svc_id = WMI_DATA_BE_SVC;
218
219 if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
220 return -EIO;
221
222 /* connect to back-ground map this to WMI LOW_PRI */
223 connect.svc_id = WMI_DATA_BK_SVC;
224 if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
225 return -EIO;
226
227 /* connect to Video service, map this to to HI PRI */
228 connect.svc_id = WMI_DATA_VI_SVC;
229 if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
230 return -EIO;
231
232 /*
233 * Connect to VO service, this is currently not mapped to a WMI
234 * priority stream due to historical reasons. WMI originally
235 * defined 3 priorities over 3 mailboxes We can change this when
236 * WMI is reworked so that priorities are not dependent on
237 * mailboxes.
238 */
239 connect.svc_id = WMI_DATA_VO_SVC;
240 if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
241 return -EIO;
242
243 return 0;
244}
245
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +0530246void ath6kl_init_control_info(struct ath6kl_vif *vif)
Kalle Valobdcd8172011-07-18 00:22:30 +0300247{
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +0530248 ath6kl_init_profile_info(vif);
Vasanthakumar Thiagarajan34503342011-10-25 19:34:02 +0530249 vif->def_txkey_index = 0;
Vasanthakumar Thiagarajan6f2a73f2011-10-25 19:34:06 +0530250 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
Vasanthakumar Thiagarajanf74bac52011-10-25 19:34:05 +0530251 vif->ch_hint = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +0300252}
253
254/*
255 * Set HTC/Mbox operational parameters, this can only be called when the
256 * target is in the BMI phase.
257 */
258static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
259 u8 htc_ctrl_buf)
260{
261 int status;
262 u32 blk_size;
263
264 blk_size = ar->mbox_info.block_size;
265
266 if (htc_ctrl_buf)
267 blk_size |= ((u32)htc_ctrl_buf) << 16;
268
269 /* set the host interest area for the block size */
270 status = ath6kl_bmi_write(ar,
271 ath6kl_get_hi_item_addr(ar,
272 HI_ITEM(hi_mbox_io_block_sz)),
273 (u8 *)&blk_size,
274 4);
275 if (status) {
276 ath6kl_err("bmi_write_memory for IO block size failed\n");
277 goto out;
278 }
279
280 ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
281 blk_size,
282 ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
283
284 if (mbox_isr_yield_val) {
285 /* set the host interest area for the mbox ISR yield limit */
286 status = ath6kl_bmi_write(ar,
287 ath6kl_get_hi_item_addr(ar,
288 HI_ITEM(hi_mbox_isr_yield_limit)),
289 (u8 *)&mbox_isr_yield_val,
290 4);
291 if (status) {
292 ath6kl_err("bmi_write_memory for yield limit failed\n");
293 goto out;
294 }
295 }
296
297out:
298 return status;
299}
300
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530301static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
Kalle Valobdcd8172011-07-18 00:22:30 +0300302{
303 int status = 0;
Jouni Malinen4dea08e2011-08-30 21:57:57 +0300304 int ret;
Kalle Valobdcd8172011-07-18 00:22:30 +0300305
306 /*
307 * Configure the device for rx dot11 header rules. "0,0" are the
308 * default values. Required if checksum offload is needed. Set
309 * RxMetaVersion to 2.
310 */
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530311 if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300312 ar->rx_meta_ver, 0, 0)) {
313 ath6kl_err("unable to set the rx frame format\n");
314 status = -EIO;
315 }
316
317 if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530318 if ((ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
Kalle Valobdcd8172011-07-18 00:22:30 +0300319 IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
320 ath6kl_err("unable to set power save fail event policy\n");
321 status = -EIO;
322 }
323
324 if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530325 if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
Kalle Valobdcd8172011-07-18 00:22:30 +0300326 WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
327 ath6kl_err("unable to set barker preamble policy\n");
328 status = -EIO;
329 }
330
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530331 if (ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300332 WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
333 ath6kl_err("unable to set keep alive interval\n");
334 status = -EIO;
335 }
336
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530337 if (ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300338 WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
339 ath6kl_err("unable to set disconnect timeout\n");
340 status = -EIO;
341 }
342
343 if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530344 if (ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED)) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300345 ath6kl_err("unable to set txop bursting\n");
346 status = -EIO;
347 }
348
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530349 /*
350 * FIXME: Make sure p2p configurations are not applied to
351 * non-p2p capable interfaces when multivif support is enabled.
352 */
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300353 if (ar->p2p) {
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530354 ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300355 P2P_FLAG_CAPABILITIES_REQ |
356 P2P_FLAG_MACADDR_REQ |
357 P2P_FLAG_HMODEL_REQ);
358 if (ret) {
359 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
360 "capabilities (%d) - assuming P2P not "
361 "supported\n", ret);
362 ar->p2p = 0;
363 }
364 }
365
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530366 /*
367 * FIXME: Make sure p2p configurations are not applied to
368 * non-p2p capable interfaces when multivif support is enabled.
369 */
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300370 if (ar->p2p) {
371 /* Enable Probe Request reporting for P2P */
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530372 ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300373 if (ret) {
374 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
375 "Request reporting (%d)\n", ret);
376 }
Jouni Malinen4dea08e2011-08-30 21:57:57 +0300377 }
378
Kalle Valobdcd8172011-07-18 00:22:30 +0300379 return status;
380}
381
382int ath6kl_configure_target(struct ath6kl *ar)
383{
384 u32 param, ram_reserved_size;
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530385 u8 fw_iftype, fw_mode = 0, fw_submode = 0;
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530386 int i;
Kalle Valobdcd8172011-07-18 00:22:30 +0300387
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530388 /*
389 * Note: Even though the firmware interface type is
390 * chosen as BSS_STA for all three interfaces, can
391 * be configured to IBSS/AP as long as the fw submode
392 * remains normal mode (0 - AP, STA and IBSS). But
393 * due to an target assert in firmware only one interface is
394 * configured for now.
395 */
Vasanthakumar Thiagarajandd3751f2011-10-25 19:33:59 +0530396 fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
Kalle Valobdcd8172011-07-18 00:22:30 +0300397
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530398 for (i = 0; i < MAX_NUM_VIF; i++)
399 fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
400
401 /*
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530402 * By default, submodes :
403 * vif[0] - AP/STA/IBSS
404 * vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
405 * vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530406 */
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530407
408 for (i = 0; i < ar->max_norm_iface; i++)
409 fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
410 (i * HI_OPTION_FW_SUBMODE_BITS);
411
412 for (i = ar->max_norm_iface; i < MAX_NUM_VIF; i++)
413 fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
414 (i * HI_OPTION_FW_SUBMODE_BITS);
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530415
416 /*
417 * FIXME: This needs to be removed once the multivif
418 * support is enabled.
419 */
420 if (ar->p2p)
421 fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530422
Kalle Valobdcd8172011-07-18 00:22:30 +0300423 param = HTC_PROTOCOL_VERSION;
424 if (ath6kl_bmi_write(ar,
425 ath6kl_get_hi_item_addr(ar,
426 HI_ITEM(hi_app_host_interest)),
427 (u8 *)&param, 4) != 0) {
428 ath6kl_err("bmi_write_memory for htc version failed\n");
429 return -EIO;
430 }
431
432 /* set the firmware mode to STA/IBSS/AP */
433 param = 0;
434
435 if (ath6kl_bmi_read(ar,
436 ath6kl_get_hi_item_addr(ar,
437 HI_ITEM(hi_option_flag)),
438 (u8 *)&param, 4) != 0) {
439 ath6kl_err("bmi_read_memory for setting fwmode failed\n");
440 return -EIO;
441 }
442
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530443 param |= (MAX_NUM_VIF << HI_OPTION_NUM_DEV_SHIFT);
444 param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
445 param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
446
Kalle Valobdcd8172011-07-18 00:22:30 +0300447 param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
448 param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
449
450 if (ath6kl_bmi_write(ar,
451 ath6kl_get_hi_item_addr(ar,
452 HI_ITEM(hi_option_flag)),
453 (u8 *)&param,
454 4) != 0) {
455 ath6kl_err("bmi_write_memory for setting fwmode failed\n");
456 return -EIO;
457 }
458
459 ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
460
461 /*
462 * Hardcode the address use for the extended board data
463 * Ideally this should be pre-allocate by the OS at boot time
464 * But since it is a new feature and board data is loaded
465 * at init time, we have to workaround this from host.
466 * It is difficult to patch the firmware boot code,
467 * but possible in theory.
468 */
469
Kalle Valo991b27e2011-09-07 10:55:17 +0300470 param = ar->hw.board_ext_data_addr;
471 ram_reserved_size = ar->hw.reserved_ram_size;
Kalle Valobdcd8172011-07-18 00:22:30 +0300472
Kalle Valo991b27e2011-09-07 10:55:17 +0300473 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
474 HI_ITEM(hi_board_ext_data)),
475 (u8 *)&param, 4) != 0) {
476 ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
477 return -EIO;
478 }
479
480 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
481 HI_ITEM(hi_end_ram_reserve_sz)),
482 (u8 *)&ram_reserved_size, 4) != 0) {
483 ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
484 return -EIO;
Kalle Valobdcd8172011-07-18 00:22:30 +0300485 }
486
487 /* set the block size for the target */
488 if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
489 /* use default number of control buffers */
490 return -EIO;
491
492 return 0;
493}
494
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +0530495void ath6kl_core_free(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +0300496{
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +0530497 wiphy_free(ar->wiphy);
Kalle Valobdcd8172011-07-18 00:22:30 +0300498}
499
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530500void ath6kl_core_cleanup(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +0300501{
Kalle Valob2e75692011-10-27 18:48:14 +0300502 ath6kl_hif_power_off(ar);
503
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530504 destroy_workqueue(ar->ath6kl_wq);
Kalle Valobdcd8172011-07-18 00:22:30 +0300505
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530506 if (ar->htc_target)
507 ath6kl_htc_cleanup(ar->htc_target);
508
509 ath6kl_cookie_cleanup(ar);
510
511 ath6kl_cleanup_amsdu_rxbufs(ar);
512
513 ath6kl_bmi_cleanup(ar);
514
515 ath6kl_debug_cleanup(ar);
516
517 kfree(ar->fw_board);
518 kfree(ar->fw_otp);
519 kfree(ar->fw);
520 kfree(ar->fw_patch);
521
522 ath6kl_deinit_ieee80211_hw(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +0300523}
524
525/* firmware upload */
Kalle Valobdcd8172011-07-18 00:22:30 +0300526static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
527 u8 **fw, size_t *fw_len)
528{
529 const struct firmware *fw_entry;
530 int ret;
531
532 ret = request_firmware(&fw_entry, filename, ar->dev);
533 if (ret)
534 return ret;
535
536 *fw_len = fw_entry->size;
537 *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
538
539 if (*fw == NULL)
540 ret = -ENOMEM;
541
542 release_firmware(fw_entry);
543
544 return ret;
545}
546
Sam Leffler92ecbff2011-09-07 10:55:16 +0300547#ifdef CONFIG_OF
548static const char *get_target_ver_dir(const struct ath6kl *ar)
549{
550 switch (ar->version.target_ver) {
551 case AR6003_REV1_VERSION:
552 return "ath6k/AR6003/hw1.0";
553 case AR6003_REV2_VERSION:
554 return "ath6k/AR6003/hw2.0";
555 case AR6003_REV3_VERSION:
556 return "ath6k/AR6003/hw2.1.1";
557 }
558 ath6kl_warn("%s: unsupported target version 0x%x.\n", __func__,
559 ar->version.target_ver);
560 return NULL;
561}
562
563/*
564 * Check the device tree for a board-id and use it to construct
565 * the pathname to the firmware file. Used (for now) to find a
566 * fallback to the "bdata.bin" file--typically a symlink to the
567 * appropriate board-specific file.
568 */
569static bool check_device_tree(struct ath6kl *ar)
570{
571 static const char *board_id_prop = "atheros,board-id";
572 struct device_node *node;
573 char board_filename[64];
574 const char *board_id;
575 int ret;
576
577 for_each_compatible_node(node, NULL, "atheros,ath6kl") {
578 board_id = of_get_property(node, board_id_prop, NULL);
579 if (board_id == NULL) {
580 ath6kl_warn("No \"%s\" property on %s node.\n",
581 board_id_prop, node->name);
582 continue;
583 }
584 snprintf(board_filename, sizeof(board_filename),
585 "%s/bdata.%s.bin", get_target_ver_dir(ar), board_id);
586
587 ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
588 &ar->fw_board_len);
589 if (ret) {
590 ath6kl_err("Failed to get DT board file %s: %d\n",
591 board_filename, ret);
592 continue;
593 }
594 return true;
595 }
596 return false;
597}
598#else
599static bool check_device_tree(struct ath6kl *ar)
600{
601 return false;
602}
603#endif /* CONFIG_OF */
604
Kalle Valobdcd8172011-07-18 00:22:30 +0300605static int ath6kl_fetch_board_file(struct ath6kl *ar)
606{
607 const char *filename;
608 int ret;
609
Kalle Valo772c31e2011-09-07 10:55:16 +0300610 if (ar->fw_board != NULL)
611 return 0;
612
Kalle Valobdcd8172011-07-18 00:22:30 +0300613 switch (ar->version.target_ver) {
614 case AR6003_REV2_VERSION:
615 filename = AR6003_REV2_BOARD_DATA_FILE;
616 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800617 case AR6004_REV1_VERSION:
618 filename = AR6004_REV1_BOARD_DATA_FILE;
619 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300620 default:
621 filename = AR6003_REV3_BOARD_DATA_FILE;
622 break;
623 }
624
625 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
626 &ar->fw_board_len);
627 if (ret == 0) {
628 /* managed to get proper board file */
629 return 0;
630 }
631
Sam Leffler92ecbff2011-09-07 10:55:16 +0300632 if (check_device_tree(ar)) {
633 /* got board file from device tree */
634 return 0;
635 }
636
Kalle Valobdcd8172011-07-18 00:22:30 +0300637 /* there was no proper board file, try to use default instead */
638 ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
639 filename, ret);
640
641 switch (ar->version.target_ver) {
642 case AR6003_REV2_VERSION:
643 filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
644 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800645 case AR6004_REV1_VERSION:
646 filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
647 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300648 default:
649 filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
650 break;
651 }
652
653 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
654 &ar->fw_board_len);
655 if (ret) {
656 ath6kl_err("Failed to get default board file %s: %d\n",
657 filename, ret);
658 return ret;
659 }
660
661 ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
662 ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
663
664 return 0;
665}
666
Kalle Valo772c31e2011-09-07 10:55:16 +0300667static int ath6kl_fetch_otp_file(struct ath6kl *ar)
668{
669 const char *filename;
670 int ret;
671
672 if (ar->fw_otp != NULL)
673 return 0;
674
675 switch (ar->version.target_ver) {
676 case AR6003_REV2_VERSION:
677 filename = AR6003_REV2_OTP_FILE;
678 break;
679 case AR6004_REV1_VERSION:
680 ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
681 return 0;
682 break;
683 default:
684 filename = AR6003_REV3_OTP_FILE;
685 break;
686 }
687
688 ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
689 &ar->fw_otp_len);
690 if (ret) {
691 ath6kl_err("Failed to get OTP file %s: %d\n",
692 filename, ret);
693 return ret;
694 }
695
696 return 0;
697}
698
699static int ath6kl_fetch_fw_file(struct ath6kl *ar)
700{
701 const char *filename;
702 int ret;
703
704 if (ar->fw != NULL)
705 return 0;
706
707 if (testmode) {
708 switch (ar->version.target_ver) {
709 case AR6003_REV2_VERSION:
710 filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
711 break;
712 case AR6003_REV3_VERSION:
713 filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
714 break;
715 case AR6004_REV1_VERSION:
716 ath6kl_warn("testmode not supported with ar6004\n");
717 return -EOPNOTSUPP;
718 default:
719 ath6kl_warn("unknown target version: 0x%x\n",
720 ar->version.target_ver);
721 return -EINVAL;
722 }
723
724 set_bit(TESTMODE, &ar->flag);
725
726 goto get_fw;
727 }
728
729 switch (ar->version.target_ver) {
730 case AR6003_REV2_VERSION:
731 filename = AR6003_REV2_FIRMWARE_FILE;
732 break;
733 case AR6004_REV1_VERSION:
734 filename = AR6004_REV1_FIRMWARE_FILE;
735 break;
736 default:
737 filename = AR6003_REV3_FIRMWARE_FILE;
738 break;
739 }
740
741get_fw:
742 ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
743 if (ret) {
744 ath6kl_err("Failed to get firmware file %s: %d\n",
745 filename, ret);
746 return ret;
747 }
748
749 return 0;
750}
751
752static int ath6kl_fetch_patch_file(struct ath6kl *ar)
753{
754 const char *filename;
755 int ret;
756
757 switch (ar->version.target_ver) {
758 case AR6003_REV2_VERSION:
759 filename = AR6003_REV2_PATCH_FILE;
760 break;
761 case AR6004_REV1_VERSION:
762 /* FIXME: implement for AR6004 */
763 return 0;
764 break;
765 default:
766 filename = AR6003_REV3_PATCH_FILE;
767 break;
768 }
769
770 if (ar->fw_patch == NULL) {
771 ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
772 &ar->fw_patch_len);
773 if (ret) {
774 ath6kl_err("Failed to get patch file %s: %d\n",
775 filename, ret);
776 return ret;
777 }
778 }
779
780 return 0;
781}
782
Kalle Valo50d41232011-09-07 10:55:17 +0300783static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
Kalle Valo772c31e2011-09-07 10:55:16 +0300784{
785 int ret;
786
Kalle Valo772c31e2011-09-07 10:55:16 +0300787 ret = ath6kl_fetch_otp_file(ar);
788 if (ret)
789 return ret;
790
791 ret = ath6kl_fetch_fw_file(ar);
792 if (ret)
793 return ret;
794
795 ret = ath6kl_fetch_patch_file(ar);
796 if (ret)
797 return ret;
798
799 return 0;
800}
Kalle Valobdcd8172011-07-18 00:22:30 +0300801
Kalle Valo50d41232011-09-07 10:55:17 +0300802static int ath6kl_fetch_fw_api2(struct ath6kl *ar)
803{
804 size_t magic_len, len, ie_len;
805 const struct firmware *fw;
806 struct ath6kl_fw_ie *hdr;
807 const char *filename;
808 const u8 *data;
Kalle Valo97e04962011-09-12 13:47:34 +0300809 int ret, ie_id, i, index, bit;
Kalle Valo8a137482011-09-07 10:55:17 +0300810 __le32 *val;
Kalle Valo50d41232011-09-07 10:55:17 +0300811
812 switch (ar->version.target_ver) {
813 case AR6003_REV2_VERSION:
814 filename = AR6003_REV2_FIRMWARE_2_FILE;
815 break;
816 case AR6003_REV3_VERSION:
817 filename = AR6003_REV3_FIRMWARE_2_FILE;
818 break;
819 case AR6004_REV1_VERSION:
820 filename = AR6004_REV1_FIRMWARE_2_FILE;
821 break;
822 default:
823 return -EOPNOTSUPP;
824 }
825
826 ret = request_firmware(&fw, filename, ar->dev);
827 if (ret)
828 return ret;
829
830 data = fw->data;
831 len = fw->size;
832
833 /* magic also includes the null byte, check that as well */
834 magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
835
836 if (len < magic_len) {
837 ret = -EINVAL;
838 goto out;
839 }
840
841 if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
842 ret = -EINVAL;
843 goto out;
844 }
845
846 len -= magic_len;
847 data += magic_len;
848
849 /* loop elements */
850 while (len > sizeof(struct ath6kl_fw_ie)) {
851 /* hdr is unaligned! */
852 hdr = (struct ath6kl_fw_ie *) data;
853
854 ie_id = le32_to_cpup(&hdr->id);
855 ie_len = le32_to_cpup(&hdr->len);
856
857 len -= sizeof(*hdr);
858 data += sizeof(*hdr);
859
860 if (len < ie_len) {
861 ret = -EINVAL;
862 goto out;
863 }
864
865 switch (ie_id) {
866 case ATH6KL_FW_IE_OTP_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300867 ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300868 ie_len);
869
Kalle Valo50d41232011-09-07 10:55:17 +0300870 ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
871
872 if (ar->fw_otp == NULL) {
873 ret = -ENOMEM;
874 goto out;
875 }
876
877 ar->fw_otp_len = ie_len;
878 break;
879 case ATH6KL_FW_IE_FW_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300880 ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300881 ie_len);
882
Kalle Valo50d41232011-09-07 10:55:17 +0300883 ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
884
885 if (ar->fw == NULL) {
886 ret = -ENOMEM;
887 goto out;
888 }
889
890 ar->fw_len = ie_len;
891 break;
892 case ATH6KL_FW_IE_PATCH_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300893 ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300894 ie_len);
895
Kalle Valo50d41232011-09-07 10:55:17 +0300896 ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
897
898 if (ar->fw_patch == NULL) {
899 ret = -ENOMEM;
900 goto out;
901 }
902
903 ar->fw_patch_len = ie_len;
904 break;
Kalle Valo8a137482011-09-07 10:55:17 +0300905 case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
906 val = (__le32 *) data;
907 ar->hw.reserved_ram_size = le32_to_cpup(val);
Kalle Valo6bc36432011-09-27 14:31:11 +0300908
909 ath6kl_dbg(ATH6KL_DBG_BOOT,
910 "found reserved ram size ie 0x%d\n",
911 ar->hw.reserved_ram_size);
Kalle Valo8a137482011-09-07 10:55:17 +0300912 break;
Kalle Valo97e04962011-09-12 13:47:34 +0300913 case ATH6KL_FW_IE_CAPABILITIES:
Kalle Valo6bc36432011-09-27 14:31:11 +0300914 ath6kl_dbg(ATH6KL_DBG_BOOT,
Kalle Valoef548622011-10-01 09:43:09 +0300915 "found firmware capabilities ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300916 ie_len);
917
Kalle Valo97e04962011-09-12 13:47:34 +0300918 for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
919 index = ALIGN(i, 8) / 8;
920 bit = i % 8;
921
922 if (data[index] & (1 << bit))
923 __set_bit(i, ar->fw_capabilities);
924 }
Kalle Valo6bc36432011-09-27 14:31:11 +0300925
926 ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
927 ar->fw_capabilities,
928 sizeof(ar->fw_capabilities));
Kalle Valo97e04962011-09-12 13:47:34 +0300929 break;
Kalle Valo1b4304d2011-09-27 11:05:26 +0300930 case ATH6KL_FW_IE_PATCH_ADDR:
931 if (ie_len != sizeof(*val))
932 break;
933
934 val = (__le32 *) data;
935 ar->hw.dataset_patch_addr = le32_to_cpup(val);
Kalle Valo6bc36432011-09-27 14:31:11 +0300936
937 ath6kl_dbg(ATH6KL_DBG_BOOT,
938 "found patch address ie 0x%d\n",
939 ar->hw.dataset_patch_addr);
Kalle Valo1b4304d2011-09-27 11:05:26 +0300940 break;
Kalle Valo50d41232011-09-07 10:55:17 +0300941 default:
Kalle Valo6bc36432011-09-27 14:31:11 +0300942 ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
Kalle Valo50d41232011-09-07 10:55:17 +0300943 le32_to_cpup(&hdr->id));
944 break;
945 }
946
947 len -= ie_len;
948 data += ie_len;
949 };
950
951 ret = 0;
952out:
953 release_firmware(fw);
954
955 return ret;
956}
957
958static int ath6kl_fetch_firmwares(struct ath6kl *ar)
959{
960 int ret;
961
962 ret = ath6kl_fetch_board_file(ar);
963 if (ret)
964 return ret;
965
966 ret = ath6kl_fetch_fw_api2(ar);
Kalle Valo6bc36432011-09-27 14:31:11 +0300967 if (ret == 0) {
968 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 2\n");
Kalle Valo50d41232011-09-07 10:55:17 +0300969 return 0;
Kalle Valo6bc36432011-09-27 14:31:11 +0300970 }
Kalle Valo50d41232011-09-07 10:55:17 +0300971
972 ret = ath6kl_fetch_fw_api1(ar);
973 if (ret)
974 return ret;
975
Kalle Valo6bc36432011-09-27 14:31:11 +0300976 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 1\n");
977
Kalle Valo50d41232011-09-07 10:55:17 +0300978 return 0;
979}
980
Kalle Valobdcd8172011-07-18 00:22:30 +0300981static int ath6kl_upload_board_file(struct ath6kl *ar)
982{
983 u32 board_address, board_ext_address, param;
Kevin Fang31024d92011-07-11 17:14:13 +0800984 u32 board_data_size, board_ext_data_size;
Kalle Valobdcd8172011-07-18 00:22:30 +0300985 int ret;
986
Kalle Valo772c31e2011-09-07 10:55:16 +0300987 if (WARN_ON(ar->fw_board == NULL))
988 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +0300989
Kevin Fang31024d92011-07-11 17:14:13 +0800990 /*
991 * Determine where in Target RAM to write Board Data.
992 * For AR6004, host determine Target RAM address for
993 * writing board data.
994 */
995 if (ar->target_type == TARGET_TYPE_AR6004) {
996 board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
997 ath6kl_bmi_write(ar,
998 ath6kl_get_hi_item_addr(ar,
999 HI_ITEM(hi_board_data)),
1000 (u8 *) &board_address, 4);
1001 } else {
1002 ath6kl_bmi_read(ar,
1003 ath6kl_get_hi_item_addr(ar,
1004 HI_ITEM(hi_board_data)),
1005 (u8 *) &board_address, 4);
1006 }
1007
Kalle Valobdcd8172011-07-18 00:22:30 +03001008 /* determine where in target ram to write extended board data */
1009 ath6kl_bmi_read(ar,
1010 ath6kl_get_hi_item_addr(ar,
1011 HI_ITEM(hi_board_ext_data)),
1012 (u8 *) &board_ext_address, 4);
1013
Kalle Valobdcd8172011-07-18 00:22:30 +03001014 if (board_ext_address == 0) {
1015 ath6kl_err("Failed to get board file target address.\n");
1016 return -EINVAL;
1017 }
1018
Kevin Fang31024d92011-07-11 17:14:13 +08001019 switch (ar->target_type) {
1020 case TARGET_TYPE_AR6003:
1021 board_data_size = AR6003_BOARD_DATA_SZ;
1022 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1023 break;
1024 case TARGET_TYPE_AR6004:
1025 board_data_size = AR6004_BOARD_DATA_SZ;
1026 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
1027 break;
1028 default:
1029 WARN_ON(1);
1030 return -EINVAL;
1031 break;
1032 }
1033
1034 if (ar->fw_board_len == (board_data_size +
1035 board_ext_data_size)) {
1036
Kalle Valobdcd8172011-07-18 00:22:30 +03001037 /* write extended board data */
Kalle Valo6bc36432011-09-27 14:31:11 +03001038 ath6kl_dbg(ATH6KL_DBG_BOOT,
1039 "writing extended board data to 0x%x (%d B)\n",
1040 board_ext_address, board_ext_data_size);
1041
Kalle Valobdcd8172011-07-18 00:22:30 +03001042 ret = ath6kl_bmi_write(ar, board_ext_address,
Kevin Fang31024d92011-07-11 17:14:13 +08001043 ar->fw_board + board_data_size,
1044 board_ext_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001045 if (ret) {
1046 ath6kl_err("Failed to write extended board data: %d\n",
1047 ret);
1048 return ret;
1049 }
1050
1051 /* record that extended board data is initialized */
Kevin Fang31024d92011-07-11 17:14:13 +08001052 param = (board_ext_data_size << 16) | 1;
1053
Kalle Valobdcd8172011-07-18 00:22:30 +03001054 ath6kl_bmi_write(ar,
1055 ath6kl_get_hi_item_addr(ar,
1056 HI_ITEM(hi_board_ext_data_config)),
1057 (unsigned char *) &param, 4);
1058 }
1059
Kevin Fang31024d92011-07-11 17:14:13 +08001060 if (ar->fw_board_len < board_data_size) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001061 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1062 ret = -EINVAL;
1063 return ret;
1064 }
1065
Kalle Valo6bc36432011-09-27 14:31:11 +03001066 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
1067 board_address, board_data_size);
1068
Kalle Valobdcd8172011-07-18 00:22:30 +03001069 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
Kevin Fang31024d92011-07-11 17:14:13 +08001070 board_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001071
1072 if (ret) {
1073 ath6kl_err("Board file bmi write failed: %d\n", ret);
1074 return ret;
1075 }
1076
1077 /* record the fact that Board Data IS initialized */
1078 param = 1;
1079 ath6kl_bmi_write(ar,
1080 ath6kl_get_hi_item_addr(ar,
1081 HI_ITEM(hi_board_data_initialized)),
1082 (u8 *)&param, 4);
1083
1084 return ret;
1085}
1086
1087static int ath6kl_upload_otp(struct ath6kl *ar)
1088{
Kalle Valobdcd8172011-07-18 00:22:30 +03001089 u32 address, param;
Kalle Valobef26a72011-10-12 09:58:28 +03001090 bool from_hw = false;
Kalle Valobdcd8172011-07-18 00:22:30 +03001091 int ret;
1092
Kalle Valo772c31e2011-09-07 10:55:16 +03001093 if (WARN_ON(ar->fw_otp == NULL))
1094 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001095
Kalle Valoa01ac412011-09-07 10:55:17 +03001096 address = ar->hw.app_load_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001097
Kalle Valoef548622011-10-01 09:43:09 +03001098 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
Kalle Valo6bc36432011-09-27 14:31:11 +03001099 ar->fw_otp_len);
1100
Kalle Valobdcd8172011-07-18 00:22:30 +03001101 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1102 ar->fw_otp_len);
1103 if (ret) {
1104 ath6kl_err("Failed to upload OTP file: %d\n", ret);
1105 return ret;
1106 }
1107
Kalle Valo639d0b82011-09-12 12:48:09 +03001108 /* read firmware start address */
1109 ret = ath6kl_bmi_read(ar,
1110 ath6kl_get_hi_item_addr(ar,
1111 HI_ITEM(hi_app_start)),
1112 (u8 *) &address, sizeof(address));
1113
1114 if (ret) {
1115 ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1116 return ret;
1117 }
1118
Kalle Valobef26a72011-10-12 09:58:28 +03001119 if (ar->hw.app_start_override_addr == 0) {
1120 ar->hw.app_start_override_addr = address;
1121 from_hw = true;
1122 }
Kalle Valo639d0b82011-09-12 12:48:09 +03001123
Kalle Valobef26a72011-10-12 09:58:28 +03001124 ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1125 from_hw ? " (from hw)" : "",
Kalle Valo6bc36432011-09-27 14:31:11 +03001126 ar->hw.app_start_override_addr);
1127
Kalle Valobdcd8172011-07-18 00:22:30 +03001128 /* execute the OTP code */
Kalle Valobef26a72011-10-12 09:58:28 +03001129 ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1130 ar->hw.app_start_override_addr);
Kalle Valobdcd8172011-07-18 00:22:30 +03001131 param = 0;
Kalle Valobef26a72011-10-12 09:58:28 +03001132 ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
Kalle Valobdcd8172011-07-18 00:22:30 +03001133
1134 return ret;
1135}
1136
1137static int ath6kl_upload_firmware(struct ath6kl *ar)
1138{
Kalle Valobdcd8172011-07-18 00:22:30 +03001139 u32 address;
1140 int ret;
1141
Kalle Valo772c31e2011-09-07 10:55:16 +03001142 if (WARN_ON(ar->fw == NULL))
1143 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001144
Kalle Valoa01ac412011-09-07 10:55:17 +03001145 address = ar->hw.app_load_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001146
Kalle Valoef548622011-10-01 09:43:09 +03001147 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +03001148 address, ar->fw_len);
1149
Kalle Valobdcd8172011-07-18 00:22:30 +03001150 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1151
1152 if (ret) {
1153 ath6kl_err("Failed to write firmware: %d\n", ret);
1154 return ret;
1155 }
1156
Kevin Fang31024d92011-07-11 17:14:13 +08001157 /*
1158 * Set starting address for firmware
1159 * Don't need to setup app_start override addr on AR6004
1160 */
1161 if (ar->target_type != TARGET_TYPE_AR6004) {
Kalle Valoa01ac412011-09-07 10:55:17 +03001162 address = ar->hw.app_start_override_addr;
Kevin Fang31024d92011-07-11 17:14:13 +08001163 ath6kl_bmi_set_app_start(ar, address);
1164 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001165 return ret;
1166}
1167
1168static int ath6kl_upload_patch(struct ath6kl *ar)
1169{
Kalle Valobdcd8172011-07-18 00:22:30 +03001170 u32 address, param;
1171 int ret;
1172
Kalle Valo772c31e2011-09-07 10:55:16 +03001173 if (WARN_ON(ar->fw_patch == NULL))
1174 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001175
Kalle Valoa01ac412011-09-07 10:55:17 +03001176 address = ar->hw.dataset_patch_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001177
Kalle Valoef548622011-10-01 09:43:09 +03001178 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +03001179 address, ar->fw_patch_len);
1180
Kalle Valobdcd8172011-07-18 00:22:30 +03001181 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1182 if (ret) {
1183 ath6kl_err("Failed to write patch file: %d\n", ret);
1184 return ret;
1185 }
1186
1187 param = address;
1188 ath6kl_bmi_write(ar,
1189 ath6kl_get_hi_item_addr(ar,
1190 HI_ITEM(hi_dset_list_head)),
1191 (unsigned char *) &param, 4);
1192
1193 return 0;
1194}
1195
1196static int ath6kl_init_upload(struct ath6kl *ar)
1197{
1198 u32 param, options, sleep, address;
1199 int status = 0;
1200
Kevin Fang31024d92011-07-11 17:14:13 +08001201 if (ar->target_type != TARGET_TYPE_AR6003 &&
1202 ar->target_type != TARGET_TYPE_AR6004)
Kalle Valobdcd8172011-07-18 00:22:30 +03001203 return -EINVAL;
1204
1205 /* temporarily disable system sleep */
1206 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1207 status = ath6kl_bmi_reg_read(ar, address, &param);
1208 if (status)
1209 return status;
1210
1211 options = param;
1212
1213 param |= ATH6KL_OPTION_SLEEP_DISABLE;
1214 status = ath6kl_bmi_reg_write(ar, address, param);
1215 if (status)
1216 return status;
1217
1218 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1219 status = ath6kl_bmi_reg_read(ar, address, &param);
1220 if (status)
1221 return status;
1222
1223 sleep = param;
1224
1225 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1226 status = ath6kl_bmi_reg_write(ar, address, param);
1227 if (status)
1228 return status;
1229
1230 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1231 options, sleep);
1232
1233 /* program analog PLL register */
Kevin Fang31024d92011-07-11 17:14:13 +08001234 /* no need to control 40/44MHz clock on AR6004 */
1235 if (ar->target_type != TARGET_TYPE_AR6004) {
1236 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1237 0xF9104001);
Kalle Valobdcd8172011-07-18 00:22:30 +03001238
Kevin Fang31024d92011-07-11 17:14:13 +08001239 if (status)
1240 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +03001241
Kevin Fang31024d92011-07-11 17:14:13 +08001242 /* Run at 80/88MHz by default */
1243 param = SM(CPU_CLOCK_STANDARD, 1);
1244
1245 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1246 status = ath6kl_bmi_reg_write(ar, address, param);
1247 if (status)
1248 return status;
1249 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001250
1251 param = 0;
1252 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1253 param = SM(LPO_CAL_ENABLE, 1);
1254 status = ath6kl_bmi_reg_write(ar, address, param);
1255 if (status)
1256 return status;
1257
1258 /* WAR to avoid SDIO CRC err */
1259 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1260 ath6kl_err("temporary war to avoid sdio crc error\n");
1261
1262 param = 0x20;
1263
1264 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1265 status = ath6kl_bmi_reg_write(ar, address, param);
1266 if (status)
1267 return status;
1268
1269 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1270 status = ath6kl_bmi_reg_write(ar, address, param);
1271 if (status)
1272 return status;
1273
1274 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1275 status = ath6kl_bmi_reg_write(ar, address, param);
1276 if (status)
1277 return status;
1278
1279 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1280 status = ath6kl_bmi_reg_write(ar, address, param);
1281 if (status)
1282 return status;
1283 }
1284
1285 /* write EEPROM data to Target RAM */
1286 status = ath6kl_upload_board_file(ar);
1287 if (status)
1288 return status;
1289
1290 /* transfer One time Programmable data */
1291 status = ath6kl_upload_otp(ar);
1292 if (status)
1293 return status;
1294
1295 /* Download Target firmware */
1296 status = ath6kl_upload_firmware(ar);
1297 if (status)
1298 return status;
1299
1300 status = ath6kl_upload_patch(ar);
1301 if (status)
1302 return status;
1303
1304 /* Restore system sleep */
1305 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1306 status = ath6kl_bmi_reg_write(ar, address, sleep);
1307 if (status)
1308 return status;
1309
1310 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1311 param = options | 0x20;
1312 status = ath6kl_bmi_reg_write(ar, address, param);
1313 if (status)
1314 return status;
1315
1316 /* Configure GPIO AR6003 UART */
1317 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1318 status = ath6kl_bmi_write(ar,
1319 ath6kl_get_hi_item_addr(ar,
1320 HI_ITEM(hi_dbg_uart_txpin)),
1321 (u8 *)&param, 4);
1322
1323 return status;
1324}
1325
Kalle Valoa01ac412011-09-07 10:55:17 +03001326static int ath6kl_init_hw_params(struct ath6kl *ar)
1327{
1328 switch (ar->version.target_ver) {
1329 case AR6003_REV2_VERSION:
1330 ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
1331 ar->hw.app_load_addr = AR6003_REV2_APP_LOAD_ADDRESS;
Kalle Valo991b27e2011-09-07 10:55:17 +03001332 ar->hw.board_ext_data_addr = AR6003_REV2_BOARD_EXT_DATA_ADDRESS;
1333 ar->hw.reserved_ram_size = AR6003_REV2_RAM_RESERVE_SIZE;
Kalle Valobef26a72011-10-12 09:58:28 +03001334
1335 /* hw2.0 needs override address hardcoded */
1336 ar->hw.app_start_override_addr = 0x944C00;
1337
Kalle Valoa01ac412011-09-07 10:55:17 +03001338 break;
1339 case AR6003_REV3_VERSION:
1340 ar->hw.dataset_patch_addr = AR6003_REV3_DATASET_PATCH_ADDRESS;
1341 ar->hw.app_load_addr = 0x1234;
Kalle Valo991b27e2011-09-07 10:55:17 +03001342 ar->hw.board_ext_data_addr = AR6003_REV3_BOARD_EXT_DATA_ADDRESS;
1343 ar->hw.reserved_ram_size = AR6003_REV3_RAM_RESERVE_SIZE;
Kalle Valoa01ac412011-09-07 10:55:17 +03001344 break;
1345 case AR6004_REV1_VERSION:
1346 ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
1347 ar->hw.app_load_addr = AR6003_REV3_APP_LOAD_ADDRESS;
Kalle Valo991b27e2011-09-07 10:55:17 +03001348 ar->hw.board_ext_data_addr = AR6004_REV1_BOARD_EXT_DATA_ADDRESS;
1349 ar->hw.reserved_ram_size = AR6004_REV1_RAM_RESERVE_SIZE;
Kalle Valoa01ac412011-09-07 10:55:17 +03001350 break;
1351 default:
1352 ath6kl_err("Unsupported hardware version: 0x%x\n",
1353 ar->version.target_ver);
1354 return -EINVAL;
1355 }
1356
Kalle Valo6bc36432011-09-27 14:31:11 +03001357 ath6kl_dbg(ATH6KL_DBG_BOOT,
1358 "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
1359 ar->version.target_ver, ar->target_type,
1360 ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
1361 ath6kl_dbg(ATH6KL_DBG_BOOT,
1362 "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
1363 ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
1364 ar->hw.reserved_ram_size);
1365
Kalle Valoa01ac412011-09-07 10:55:17 +03001366 return 0;
1367}
1368
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001369int ath6kl_init_hw_start(struct ath6kl *ar)
Kalle Valo20459ee2011-10-27 18:48:37 +03001370{
1371 long timeleft;
1372 int ret, i;
1373
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001374 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
1375
Kalle Valo20459ee2011-10-27 18:48:37 +03001376 ret = ath6kl_hif_power_on(ar);
1377 if (ret)
1378 return ret;
1379
1380 ret = ath6kl_configure_target(ar);
1381 if (ret)
1382 goto err_power_off;
1383
1384 ret = ath6kl_init_upload(ar);
1385 if (ret)
1386 goto err_power_off;
1387
1388 /* Do we need to finish the BMI phase */
1389 /* FIXME: return error from ath6kl_bmi_done() */
1390 if (ath6kl_bmi_done(ar)) {
1391 ret = -EIO;
1392 goto err_power_off;
1393 }
1394
1395 /*
1396 * The reason we have to wait for the target here is that the
1397 * driver layer has to init BMI in order to set the host block
1398 * size.
1399 */
1400 if (ath6kl_htc_wait_target(ar->htc_target)) {
1401 ret = -EIO;
1402 goto err_power_off;
1403 }
1404
1405 if (ath6kl_init_service_ep(ar)) {
1406 ret = -EIO;
1407 goto err_cleanup_scatter;
1408 }
1409
1410 /* setup credit distribution */
1411 ath6kl_credit_setup(ar->htc_target, &ar->credit_state_info);
1412
1413 /* start HTC */
1414 ret = ath6kl_htc_start(ar->htc_target);
1415 if (ret) {
1416 /* FIXME: call this */
1417 ath6kl_cookie_cleanup(ar);
1418 goto err_cleanup_scatter;
1419 }
1420
1421 /* Wait for Wmi event to be ready */
1422 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1423 test_bit(WMI_READY,
1424 &ar->flag),
1425 WMI_TIMEOUT);
1426
1427 ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
1428
1429 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1430 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1431 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1432 ret = -EIO;
1433 goto err_htc_stop;
1434 }
1435
1436 if (!timeleft || signal_pending(current)) {
1437 ath6kl_err("wmi is not ready or wait was interrupted\n");
1438 ret = -EIO;
1439 goto err_htc_stop;
1440 }
1441
1442 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1443
1444 /* communicate the wmi protocol verision to the target */
1445 /* FIXME: return error */
1446 if ((ath6kl_set_host_app_area(ar)) != 0)
1447 ath6kl_err("unable to set the host app area\n");
1448
1449 for (i = 0; i < MAX_NUM_VIF; i++) {
1450 ret = ath6kl_target_config_wlan_params(ar, i);
1451 if (ret)
1452 goto err_htc_stop;
1453 }
1454
Kalle Valo76a9fbe2011-11-01 08:44:28 +02001455 ar->state = ATH6KL_STATE_ON;
1456
Kalle Valo20459ee2011-10-27 18:48:37 +03001457 return 0;
1458
1459err_htc_stop:
1460 ath6kl_htc_stop(ar->htc_target);
1461err_cleanup_scatter:
1462 ath6kl_hif_cleanup_scatter(ar);
1463err_power_off:
1464 ath6kl_hif_power_off(ar);
1465
1466 return ret;
1467}
1468
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001469int ath6kl_init_hw_stop(struct ath6kl *ar)
1470{
1471 int ret;
1472
1473 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
1474
1475 ath6kl_htc_stop(ar->htc_target);
1476
1477 ath6kl_hif_stop(ar);
1478
1479 ath6kl_bmi_reset(ar);
1480
1481 ret = ath6kl_hif_power_off(ar);
1482 if (ret)
1483 ath6kl_warn("failed to power off hif: %d\n", ret);
1484
Kalle Valo76a9fbe2011-11-01 08:44:28 +02001485 ar->state = ATH6KL_STATE_OFF;
1486
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001487 return 0;
1488}
1489
Kalle Valobdcd8172011-07-18 00:22:30 +03001490int ath6kl_core_init(struct ath6kl *ar)
1491{
Kalle Valobdcd8172011-07-18 00:22:30 +03001492 struct ath6kl_bmi_target_info targ_info;
Kalle Valo61448a92011-10-27 18:48:29 +03001493 struct net_device *ndev;
Kalle Valo20459ee2011-10-27 18:48:37 +03001494 int ret = 0, i;
Kalle Valobdcd8172011-07-18 00:22:30 +03001495
1496 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1497 if (!ar->ath6kl_wq)
1498 return -ENOMEM;
1499
1500 ret = ath6kl_bmi_init(ar);
1501 if (ret)
1502 goto err_wq;
1503
Kalle Valo20459ee2011-10-27 18:48:37 +03001504 /*
1505 * Turn on power to get hardware (target) version and leave power
1506 * on delibrately as we will boot the hardware anyway within few
1507 * seconds.
1508 */
Kalle Valob2e75692011-10-27 18:48:14 +03001509 ret = ath6kl_hif_power_on(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001510 if (ret)
1511 goto err_bmi_cleanup;
1512
Kalle Valob2e75692011-10-27 18:48:14 +03001513 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1514 if (ret)
1515 goto err_power_off;
1516
Kalle Valobdcd8172011-07-18 00:22:30 +03001517 ar->version.target_ver = le32_to_cpu(targ_info.version);
1518 ar->target_type = le32_to_cpu(targ_info.type);
Vasanthakumar Thiagarajanbe98e3a2011-10-25 19:33:57 +05301519 ar->wiphy->hw_version = le32_to_cpu(targ_info.version);
Kalle Valobdcd8172011-07-18 00:22:30 +03001520
Kalle Valoa01ac412011-09-07 10:55:17 +03001521 ret = ath6kl_init_hw_params(ar);
1522 if (ret)
Kalle Valob2e75692011-10-27 18:48:14 +03001523 goto err_power_off;
Kalle Valoa01ac412011-09-07 10:55:17 +03001524
Kalle Vaload226ec2011-08-10 09:49:12 +03001525 ar->htc_target = ath6kl_htc_create(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001526
1527 if (!ar->htc_target) {
1528 ret = -ENOMEM;
Kalle Valob2e75692011-10-27 18:48:14 +03001529 goto err_power_off;
Kalle Valobdcd8172011-07-18 00:22:30 +03001530 }
1531
Kalle Valo772c31e2011-09-07 10:55:16 +03001532 ret = ath6kl_fetch_firmwares(ar);
1533 if (ret)
1534 goto err_htc_cleanup;
1535
Kalle Valo61448a92011-10-27 18:48:29 +03001536 /* FIXME: we should free all firmwares in the error cases below */
1537
Kalle Valo61448a92011-10-27 18:48:29 +03001538 /* Indicate that WMI is enabled (although not ready yet) */
1539 set_bit(WMI_ENABLED, &ar->flag);
1540 ar->wmi = ath6kl_wmi_init(ar);
1541 if (!ar->wmi) {
1542 ath6kl_err("failed to initialize wmi\n");
1543 ret = -EIO;
1544 goto err_htc_cleanup;
1545 }
1546
1547 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1548
1549 ret = ath6kl_register_ieee80211_hw(ar);
1550 if (ret)
1551 goto err_node_cleanup;
1552
1553 ret = ath6kl_debug_init(ar);
1554 if (ret) {
1555 wiphy_unregister(ar->wiphy);
1556 goto err_node_cleanup;
1557 }
1558
1559 for (i = 0; i < MAX_NUM_VIF; i++)
1560 ar->avail_idx_map |= BIT(i);
1561
1562 rtnl_lock();
1563
1564 /* Add an initial station interface */
1565 ndev = ath6kl_interface_add(ar, "wlan%d", NL80211_IFTYPE_STATION, 0,
1566 INFRA_NETWORK);
1567
1568 rtnl_unlock();
1569
1570 if (!ndev) {
1571 ath6kl_err("Failed to instantiate a network device\n");
1572 ret = -ENOMEM;
1573 wiphy_unregister(ar->wiphy);
1574 goto err_debug_init;
1575 }
1576
1577
1578 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1579 __func__, ndev->name, ndev, ar);
1580
Kalle Valo61448a92011-10-27 18:48:29 +03001581 /* setup access class priority mappings */
1582 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1583 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1584 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1585 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1586
1587 /* give our connected endpoints some buffers */
1588 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1589 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1590
1591 /* allocate some buffers that handle larger AMSDU frames */
1592 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1593
Kalle Valo61448a92011-10-27 18:48:29 +03001594 ath6kl_cookie_init(ar);
1595
Kalle Valo61448a92011-10-27 18:48:29 +03001596 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1597 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1598
1599 ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM |
1600 WIPHY_FLAG_HAVE_AP_SME;
1601
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001602 set_bit(FIRST_BOOT, &ar->flag);
1603
1604 ret = ath6kl_init_hw_start(ar);
Kalle Valo20459ee2011-10-27 18:48:37 +03001605 if (ret) {
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001606 ath6kl_err("Failed to start hardware: %d\n", ret);
Kalle Valo20459ee2011-10-27 18:48:37 +03001607 goto err_rxbuf_cleanup;
Kalle Valo61448a92011-10-27 18:48:29 +03001608 }
1609
1610 /*
1611 * Set mac address which is received in ready event
1612 * FIXME: Move to ath6kl_interface_add()
1613 */
1614 memcpy(ndev->dev_addr, ar->mac_addr, ETH_ALEN);
Kalle Valobdcd8172011-07-18 00:22:30 +03001615
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001616 ret = ath6kl_init_hw_stop(ar);
1617 if (ret) {
1618 ath6kl_err("Failed to stop hardware: %d\n", ret);
1619 goto err_htc_cleanup;
1620 }
1621
Kalle Valobdcd8172011-07-18 00:22:30 +03001622 return ret;
1623
Kalle Valo61448a92011-10-27 18:48:29 +03001624err_rxbuf_cleanup:
1625 ath6kl_htc_flush_rx_buf(ar->htc_target);
1626 ath6kl_cleanup_amsdu_rxbufs(ar);
Kalle Valo61448a92011-10-27 18:48:29 +03001627 rtnl_lock();
1628 ath6kl_deinit_if_data(netdev_priv(ndev));
1629 rtnl_unlock();
1630 wiphy_unregister(ar->wiphy);
1631err_debug_init:
1632 ath6kl_debug_cleanup(ar);
1633err_node_cleanup:
1634 ath6kl_wmi_shutdown(ar->wmi);
1635 clear_bit(WMI_ENABLED, &ar->flag);
1636 ar->wmi = NULL;
Kalle Valobdcd8172011-07-18 00:22:30 +03001637err_htc_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001638 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valob2e75692011-10-27 18:48:14 +03001639err_power_off:
1640 ath6kl_hif_power_off(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001641err_bmi_cleanup:
1642 ath6kl_bmi_cleanup(ar);
1643err_wq:
1644 destroy_workqueue(ar->ath6kl_wq);
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +05301645
Kalle Valobdcd8172011-07-18 00:22:30 +03001646 return ret;
1647}
1648
Vasanthakumar Thiagarajan55055972011-10-25 19:34:23 +05301649void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301650{
1651 static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1652 bool discon_issued;
1653
1654 netif_stop_queue(vif->ndev);
1655
1656 clear_bit(WLAN_ENABLED, &vif->flags);
1657
1658 if (wmi_ready) {
1659 discon_issued = test_bit(CONNECTED, &vif->flags) ||
1660 test_bit(CONNECT_PEND, &vif->flags);
1661 ath6kl_disconnect(vif);
1662 del_timer(&vif->disconnect_timer);
1663
1664 if (discon_issued)
1665 ath6kl_disconnect_event(vif, DISCONNECT_CMD,
1666 (vif->nw_type & AP_NETWORK) ?
1667 bcast_mac : vif->bssid,
1668 0, NULL, 0);
1669 }
1670
1671 if (vif->scan_req) {
1672 cfg80211_scan_done(vif->scan_req, true);
1673 vif->scan_req = NULL;
1674 }
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301675}
1676
Kalle Valobdcd8172011-07-18 00:22:30 +03001677void ath6kl_stop_txrx(struct ath6kl *ar)
1678{
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301679 struct ath6kl_vif *vif, *tmp_vif;
Kalle Valobdcd8172011-07-18 00:22:30 +03001680
1681 set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1682
1683 if (down_interruptible(&ar->sem)) {
1684 ath6kl_err("down_interruptible failed\n");
1685 return;
1686 }
1687
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301688 spin_lock(&ar->list_lock);
1689 list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1690 list_del(&vif->list);
1691 spin_unlock(&ar->list_lock);
1692 ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
Vasanthakumar Thiagarajan27929722011-10-25 19:34:21 +05301693 rtnl_lock();
1694 ath6kl_deinit_if_data(vif);
1695 rtnl_unlock();
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301696 spin_lock(&ar->list_lock);
1697 }
1698 spin_unlock(&ar->list_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +03001699
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301700 clear_bit(WMI_READY, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001701
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301702 /*
1703 * After wmi_shudown all WMI events will be dropped. We
1704 * need to cleanup the buffers allocated in AP mode and
1705 * give disconnect notification to stack, which usually
1706 * happens in the disconnect_event. Simulate the disconnect
1707 * event by calling the function directly. Sometimes
1708 * disconnect_event will be received when the debug logs
1709 * are collected.
1710 */
1711 ath6kl_wmi_shutdown(ar->wmi);
Kalle Valobdcd8172011-07-18 00:22:30 +03001712
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301713 clear_bit(WMI_ENABLED, &ar->flag);
1714 if (ar->htc_target) {
1715 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
1716 ath6kl_htc_stop(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001717 }
1718
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301719 /*
1720 * Try to reset the device if we can. The driver may have been
1721 * configure NOT to reset the target during a debug session.
1722 */
1723 ath6kl_dbg(ATH6KL_DBG_TRC,
1724 "attempting to reset target on instance destroy\n");
1725 ath6kl_reset_device(ar, ar->target_type, true, true);
Kalle Valobdcd8172011-07-18 00:22:30 +03001726
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301727 clear_bit(WLAN_ENABLED, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001728}