blob: 5acb4a4b93bf33b4870b9f2bd3055142ee3734b2 [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 Valo8277de12011-11-03 12:18:31 +020030static bool suspend_cutpower;
Kalle Valobdcd8172011-07-18 00:22:30 +030031
32module_param(debug_mask, uint, 0644);
Kalle Valo003353b0d2011-09-01 10:14:21 +030033module_param(testmode, uint, 0644);
Kalle Valo8277de12011-11-03 12:18:31 +020034module_param(suspend_cutpower, bool, 0444);
Kalle Valobdcd8172011-07-18 00:22:30 +030035
36/*
37 * Include definitions here that can be used to tune the WLAN module
38 * behavior. Different customers can tune the behavior as per their needs,
39 * here.
40 */
41
42/*
43 * This configuration item enable/disable keepalive support.
44 * Keepalive support: In the absence of any data traffic to AP, null
45 * frames will be sent to the AP at periodic interval, to keep the association
46 * active. This configuration item defines the periodic interval.
47 * Use value of zero to disable keepalive support
48 * Default: 60 seconds
49 */
50#define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
51
52/*
53 * This configuration item sets the value of disconnect timeout
54 * Firmware delays sending the disconnec event to the host for this
55 * timeout after is gets disconnected from the current AP.
56 * If the firmware successly roams within the disconnect timeout
57 * it sends a new connect event
58 */
59#define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
60
61#define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
62
Kalle Valobdcd8172011-07-18 00:22:30 +030063#define ATH6KL_DATA_OFFSET 64
64struct sk_buff *ath6kl_buf_alloc(int size)
65{
66 struct sk_buff *skb;
67 u16 reserved;
68
69 /* Add chacheline space at front and back of buffer */
70 reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
Vasanthakumar Thiagarajan1df94a82011-08-17 18:45:10 +053071 sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
Kalle Valobdcd8172011-07-18 00:22:30 +030072 skb = dev_alloc_skb(size + reserved);
73
74 if (skb)
75 skb_reserve(skb, reserved - L1_CACHE_BYTES);
76 return skb;
77}
78
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +053079void ath6kl_init_profile_info(struct ath6kl_vif *vif)
Kalle Valobdcd8172011-07-18 00:22:30 +030080{
Vasanthakumar Thiagarajan34503342011-10-25 19:34:02 +053081 vif->ssid_len = 0;
82 memset(vif->ssid, 0, sizeof(vif->ssid));
83
84 vif->dot11_auth_mode = OPEN_AUTH;
85 vif->auth_mode = NONE_AUTH;
86 vif->prwise_crypto = NONE_CRYPT;
87 vif->prwise_crypto_len = 0;
88 vif->grp_crypto = NONE_CRYPT;
89 vif->grp_crypto_len = 0;
Vasanthakumar Thiagarajan6f2a73f2011-10-25 19:34:06 +053090 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
Vasanthakumar Thiagarajan8c8b65e2011-10-25 19:34:04 +053091 memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
92 memset(vif->bssid, 0, sizeof(vif->bssid));
Vasanthakumar Thiagarajanf74bac52011-10-25 19:34:05 +053093 vif->bss_ch = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +030094}
95
Kalle Valobdcd8172011-07-18 00:22:30 +030096static int ath6kl_set_host_app_area(struct ath6kl *ar)
97{
98 u32 address, data;
99 struct host_app_area host_app_area;
100
101 /* Fetch the address of the host_app_area_s
102 * instance in the host interest area */
103 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
Kevin Fang31024d92011-07-11 17:14:13 +0800104 address = TARG_VTOP(ar->target_type, address);
Kalle Valobdcd8172011-07-18 00:22:30 +0300105
Kalle Valoaddb44b2011-09-02 10:32:05 +0300106 if (ath6kl_diag_read32(ar, address, &data))
Kalle Valobdcd8172011-07-18 00:22:30 +0300107 return -EIO;
108
Kevin Fang31024d92011-07-11 17:14:13 +0800109 address = TARG_VTOP(ar->target_type, data);
Kalle Valocbf49a62011-10-05 12:23:17 +0300110 host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
Kalle Valoaddb44b2011-09-02 10:32:05 +0300111 if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
112 sizeof(struct host_app_area)))
Kalle Valobdcd8172011-07-18 00:22:30 +0300113 return -EIO;
114
115 return 0;
116}
117
118static inline void set_ac2_ep_map(struct ath6kl *ar,
119 u8 ac,
120 enum htc_endpoint_id ep)
121{
122 ar->ac2ep_map[ac] = ep;
123 ar->ep2ac_map[ep] = ac;
124}
125
126/* connect to a service */
127static int ath6kl_connectservice(struct ath6kl *ar,
128 struct htc_service_connect_req *con_req,
129 char *desc)
130{
131 int status;
132 struct htc_service_connect_resp response;
133
134 memset(&response, 0, sizeof(response));
135
Kalle Vaload226ec2011-08-10 09:49:12 +0300136 status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
Kalle Valobdcd8172011-07-18 00:22:30 +0300137 if (status) {
138 ath6kl_err("failed to connect to %s service status:%d\n",
139 desc, status);
140 return status;
141 }
142
143 switch (con_req->svc_id) {
144 case WMI_CONTROL_SVC:
145 if (test_bit(WMI_ENABLED, &ar->flag))
146 ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
147 ar->ctrl_ep = response.endpoint;
148 break;
149 case WMI_DATA_BE_SVC:
150 set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
151 break;
152 case WMI_DATA_BK_SVC:
153 set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
154 break;
155 case WMI_DATA_VI_SVC:
156 set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
157 break;
158 case WMI_DATA_VO_SVC:
159 set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
160 break;
161 default:
162 ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
163 return -EINVAL;
164 }
165
166 return 0;
167}
168
169static int ath6kl_init_service_ep(struct ath6kl *ar)
170{
171 struct htc_service_connect_req connect;
172
173 memset(&connect, 0, sizeof(connect));
174
175 /* these fields are the same for all service endpoints */
176 connect.ep_cb.rx = ath6kl_rx;
177 connect.ep_cb.rx_refill = ath6kl_rx_refill;
178 connect.ep_cb.tx_full = ath6kl_tx_queue_full;
179
180 /*
181 * Set the max queue depth so that our ath6kl_tx_queue_full handler
182 * gets called.
183 */
184 connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
185 connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
186 if (!connect.ep_cb.rx_refill_thresh)
187 connect.ep_cb.rx_refill_thresh++;
188
189 /* connect to control service */
190 connect.svc_id = WMI_CONTROL_SVC;
191 if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
192 return -EIO;
193
194 connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
195
196 /*
197 * Limit the HTC message size on the send path, although e can
198 * receive A-MSDU frames of 4K, we will only send ethernet-sized
199 * (802.3) frames on the send path.
200 */
201 connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
202
203 /*
204 * To reduce the amount of committed memory for larger A_MSDU
205 * frames, use the recv-alloc threshold mechanism for larger
206 * packets.
207 */
208 connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
209 connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
210
211 /*
212 * For the remaining data services set the connection flag to
213 * reduce dribbling, if configured to do so.
214 */
215 connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
216 connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
217 connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
218
219 connect.svc_id = WMI_DATA_BE_SVC;
220
221 if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
222 return -EIO;
223
224 /* connect to back-ground map this to WMI LOW_PRI */
225 connect.svc_id = WMI_DATA_BK_SVC;
226 if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
227 return -EIO;
228
229 /* connect to Video service, map this to to HI PRI */
230 connect.svc_id = WMI_DATA_VI_SVC;
231 if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
232 return -EIO;
233
234 /*
235 * Connect to VO service, this is currently not mapped to a WMI
236 * priority stream due to historical reasons. WMI originally
237 * defined 3 priorities over 3 mailboxes We can change this when
238 * WMI is reworked so that priorities are not dependent on
239 * mailboxes.
240 */
241 connect.svc_id = WMI_DATA_VO_SVC;
242 if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
243 return -EIO;
244
245 return 0;
246}
247
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +0530248void ath6kl_init_control_info(struct ath6kl_vif *vif)
Kalle Valobdcd8172011-07-18 00:22:30 +0300249{
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +0530250 ath6kl_init_profile_info(vif);
Vasanthakumar Thiagarajan34503342011-10-25 19:34:02 +0530251 vif->def_txkey_index = 0;
Vasanthakumar Thiagarajan6f2a73f2011-10-25 19:34:06 +0530252 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
Vasanthakumar Thiagarajanf74bac52011-10-25 19:34:05 +0530253 vif->ch_hint = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +0300254}
255
256/*
257 * Set HTC/Mbox operational parameters, this can only be called when the
258 * target is in the BMI phase.
259 */
260static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
261 u8 htc_ctrl_buf)
262{
263 int status;
264 u32 blk_size;
265
266 blk_size = ar->mbox_info.block_size;
267
268 if (htc_ctrl_buf)
269 blk_size |= ((u32)htc_ctrl_buf) << 16;
270
271 /* set the host interest area for the block size */
272 status = ath6kl_bmi_write(ar,
273 ath6kl_get_hi_item_addr(ar,
274 HI_ITEM(hi_mbox_io_block_sz)),
275 (u8 *)&blk_size,
276 4);
277 if (status) {
278 ath6kl_err("bmi_write_memory for IO block size failed\n");
279 goto out;
280 }
281
282 ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
283 blk_size,
284 ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
285
286 if (mbox_isr_yield_val) {
287 /* set the host interest area for the mbox ISR yield limit */
288 status = ath6kl_bmi_write(ar,
289 ath6kl_get_hi_item_addr(ar,
290 HI_ITEM(hi_mbox_isr_yield_limit)),
291 (u8 *)&mbox_isr_yield_val,
292 4);
293 if (status) {
294 ath6kl_err("bmi_write_memory for yield limit failed\n");
295 goto out;
296 }
297 }
298
299out:
300 return status;
301}
302
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530303static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
Kalle Valobdcd8172011-07-18 00:22:30 +0300304{
305 int status = 0;
Jouni Malinen4dea08e2011-08-30 21:57:57 +0300306 int ret;
Kalle Valobdcd8172011-07-18 00:22:30 +0300307
308 /*
309 * Configure the device for rx dot11 header rules. "0,0" are the
310 * default values. Required if checksum offload is needed. Set
311 * RxMetaVersion to 2.
312 */
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530313 if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300314 ar->rx_meta_ver, 0, 0)) {
315 ath6kl_err("unable to set the rx frame format\n");
316 status = -EIO;
317 }
318
319 if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530320 if ((ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
Kalle Valobdcd8172011-07-18 00:22:30 +0300321 IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
322 ath6kl_err("unable to set power save fail event policy\n");
323 status = -EIO;
324 }
325
326 if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530327 if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
Kalle Valobdcd8172011-07-18 00:22:30 +0300328 WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
329 ath6kl_err("unable to set barker preamble policy\n");
330 status = -EIO;
331 }
332
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530333 if (ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300334 WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
335 ath6kl_err("unable to set keep alive interval\n");
336 status = -EIO;
337 }
338
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530339 if (ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300340 WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
341 ath6kl_err("unable to set disconnect timeout\n");
342 status = -EIO;
343 }
344
345 if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530346 if (ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED)) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300347 ath6kl_err("unable to set txop bursting\n");
348 status = -EIO;
349 }
350
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530351 /*
352 * FIXME: Make sure p2p configurations are not applied to
353 * non-p2p capable interfaces when multivif support is enabled.
354 */
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300355 if (ar->p2p) {
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530356 ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300357 P2P_FLAG_CAPABILITIES_REQ |
358 P2P_FLAG_MACADDR_REQ |
359 P2P_FLAG_HMODEL_REQ);
360 if (ret) {
361 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
362 "capabilities (%d) - assuming P2P not "
363 "supported\n", ret);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000364 ar->p2p = false;
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300365 }
366 }
367
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530368 /*
369 * FIXME: Make sure p2p configurations are not applied to
370 * non-p2p capable interfaces when multivif support is enabled.
371 */
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300372 if (ar->p2p) {
373 /* Enable Probe Request reporting for P2P */
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530374 ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300375 if (ret) {
376 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
377 "Request reporting (%d)\n", ret);
378 }
Jouni Malinen4dea08e2011-08-30 21:57:57 +0300379 }
380
Kalle Valobdcd8172011-07-18 00:22:30 +0300381 return status;
382}
383
384int ath6kl_configure_target(struct ath6kl *ar)
385{
386 u32 param, ram_reserved_size;
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530387 u8 fw_iftype, fw_mode = 0, fw_submode = 0;
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530388 int i;
Kalle Valobdcd8172011-07-18 00:22:30 +0300389
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530390 /*
391 * Note: Even though the firmware interface type is
392 * chosen as BSS_STA for all three interfaces, can
393 * be configured to IBSS/AP as long as the fw submode
394 * remains normal mode (0 - AP, STA and IBSS). But
395 * due to an target assert in firmware only one interface is
396 * configured for now.
397 */
Vasanthakumar Thiagarajandd3751f2011-10-25 19:33:59 +0530398 fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
Kalle Valobdcd8172011-07-18 00:22:30 +0300399
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530400 for (i = 0; i < MAX_NUM_VIF; i++)
401 fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
402
403 /*
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530404 * By default, submodes :
405 * vif[0] - AP/STA/IBSS
406 * vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
407 * vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530408 */
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530409
410 for (i = 0; i < ar->max_norm_iface; i++)
411 fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
412 (i * HI_OPTION_FW_SUBMODE_BITS);
413
414 for (i = ar->max_norm_iface; i < MAX_NUM_VIF; i++)
415 fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
416 (i * HI_OPTION_FW_SUBMODE_BITS);
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530417
418 /*
419 * FIXME: This needs to be removed once the multivif
420 * support is enabled.
421 */
422 if (ar->p2p)
423 fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530424
Kalle Valobdcd8172011-07-18 00:22:30 +0300425 param = HTC_PROTOCOL_VERSION;
426 if (ath6kl_bmi_write(ar,
427 ath6kl_get_hi_item_addr(ar,
428 HI_ITEM(hi_app_host_interest)),
429 (u8 *)&param, 4) != 0) {
430 ath6kl_err("bmi_write_memory for htc version failed\n");
431 return -EIO;
432 }
433
434 /* set the firmware mode to STA/IBSS/AP */
435 param = 0;
436
437 if (ath6kl_bmi_read(ar,
438 ath6kl_get_hi_item_addr(ar,
439 HI_ITEM(hi_option_flag)),
440 (u8 *)&param, 4) != 0) {
441 ath6kl_err("bmi_read_memory for setting fwmode failed\n");
442 return -EIO;
443 }
444
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530445 param |= (MAX_NUM_VIF << HI_OPTION_NUM_DEV_SHIFT);
446 param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
447 param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
448
Kalle Valobdcd8172011-07-18 00:22:30 +0300449 param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
450 param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
451
452 if (ath6kl_bmi_write(ar,
453 ath6kl_get_hi_item_addr(ar,
454 HI_ITEM(hi_option_flag)),
455 (u8 *)&param,
456 4) != 0) {
457 ath6kl_err("bmi_write_memory for setting fwmode failed\n");
458 return -EIO;
459 }
460
461 ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
462
463 /*
464 * Hardcode the address use for the extended board data
465 * Ideally this should be pre-allocate by the OS at boot time
466 * But since it is a new feature and board data is loaded
467 * at init time, we have to workaround this from host.
468 * It is difficult to patch the firmware boot code,
469 * but possible in theory.
470 */
471
Kalle Valo991b27e2011-09-07 10:55:17 +0300472 param = ar->hw.board_ext_data_addr;
473 ram_reserved_size = ar->hw.reserved_ram_size;
Kalle Valobdcd8172011-07-18 00:22:30 +0300474
Kalle Valo991b27e2011-09-07 10:55:17 +0300475 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
476 HI_ITEM(hi_board_ext_data)),
477 (u8 *)&param, 4) != 0) {
478 ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
479 return -EIO;
480 }
481
482 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
483 HI_ITEM(hi_end_ram_reserve_sz)),
484 (u8 *)&ram_reserved_size, 4) != 0) {
485 ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
486 return -EIO;
Kalle Valobdcd8172011-07-18 00:22:30 +0300487 }
488
489 /* set the block size for the target */
490 if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
491 /* use default number of control buffers */
492 return -EIO;
493
494 return 0;
495}
496
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +0530497void ath6kl_core_free(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +0300498{
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +0530499 wiphy_free(ar->wiphy);
Kalle Valobdcd8172011-07-18 00:22:30 +0300500}
501
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530502void ath6kl_core_cleanup(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +0300503{
Kalle Valob2e75692011-10-27 18:48:14 +0300504 ath6kl_hif_power_off(ar);
505
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530506 destroy_workqueue(ar->ath6kl_wq);
Kalle Valobdcd8172011-07-18 00:22:30 +0300507
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530508 if (ar->htc_target)
509 ath6kl_htc_cleanup(ar->htc_target);
510
511 ath6kl_cookie_cleanup(ar);
512
513 ath6kl_cleanup_amsdu_rxbufs(ar);
514
515 ath6kl_bmi_cleanup(ar);
516
517 ath6kl_debug_cleanup(ar);
518
519 kfree(ar->fw_board);
520 kfree(ar->fw_otp);
521 kfree(ar->fw);
522 kfree(ar->fw_patch);
523
524 ath6kl_deinit_ieee80211_hw(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +0300525}
526
527/* firmware upload */
Kalle Valobdcd8172011-07-18 00:22:30 +0300528static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
529 u8 **fw, size_t *fw_len)
530{
531 const struct firmware *fw_entry;
532 int ret;
533
534 ret = request_firmware(&fw_entry, filename, ar->dev);
535 if (ret)
536 return ret;
537
538 *fw_len = fw_entry->size;
539 *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
540
541 if (*fw == NULL)
542 ret = -ENOMEM;
543
544 release_firmware(fw_entry);
545
546 return ret;
547}
548
Sam Leffler92ecbff2011-09-07 10:55:16 +0300549#ifdef CONFIG_OF
550static const char *get_target_ver_dir(const struct ath6kl *ar)
551{
552 switch (ar->version.target_ver) {
553 case AR6003_REV1_VERSION:
554 return "ath6k/AR6003/hw1.0";
555 case AR6003_REV2_VERSION:
556 return "ath6k/AR6003/hw2.0";
557 case AR6003_REV3_VERSION:
558 return "ath6k/AR6003/hw2.1.1";
559 }
560 ath6kl_warn("%s: unsupported target version 0x%x.\n", __func__,
561 ar->version.target_ver);
562 return NULL;
563}
564
565/*
566 * Check the device tree for a board-id and use it to construct
567 * the pathname to the firmware file. Used (for now) to find a
568 * fallback to the "bdata.bin" file--typically a symlink to the
569 * appropriate board-specific file.
570 */
571static bool check_device_tree(struct ath6kl *ar)
572{
573 static const char *board_id_prop = "atheros,board-id";
574 struct device_node *node;
575 char board_filename[64];
576 const char *board_id;
577 int ret;
578
579 for_each_compatible_node(node, NULL, "atheros,ath6kl") {
580 board_id = of_get_property(node, board_id_prop, NULL);
581 if (board_id == NULL) {
582 ath6kl_warn("No \"%s\" property on %s node.\n",
583 board_id_prop, node->name);
584 continue;
585 }
586 snprintf(board_filename, sizeof(board_filename),
587 "%s/bdata.%s.bin", get_target_ver_dir(ar), board_id);
588
589 ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
590 &ar->fw_board_len);
591 if (ret) {
592 ath6kl_err("Failed to get DT board file %s: %d\n",
593 board_filename, ret);
594 continue;
595 }
596 return true;
597 }
598 return false;
599}
600#else
601static bool check_device_tree(struct ath6kl *ar)
602{
603 return false;
604}
605#endif /* CONFIG_OF */
606
Kalle Valobdcd8172011-07-18 00:22:30 +0300607static int ath6kl_fetch_board_file(struct ath6kl *ar)
608{
609 const char *filename;
610 int ret;
611
Kalle Valo772c31e2011-09-07 10:55:16 +0300612 if (ar->fw_board != NULL)
613 return 0;
614
Kalle Valobdcd8172011-07-18 00:22:30 +0300615 switch (ar->version.target_ver) {
616 case AR6003_REV2_VERSION:
617 filename = AR6003_REV2_BOARD_DATA_FILE;
618 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800619 case AR6004_REV1_VERSION:
620 filename = AR6004_REV1_BOARD_DATA_FILE;
621 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300622 default:
623 filename = AR6003_REV3_BOARD_DATA_FILE;
624 break;
625 }
626
627 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
628 &ar->fw_board_len);
629 if (ret == 0) {
630 /* managed to get proper board file */
631 return 0;
632 }
633
Sam Leffler92ecbff2011-09-07 10:55:16 +0300634 if (check_device_tree(ar)) {
635 /* got board file from device tree */
636 return 0;
637 }
638
Kalle Valobdcd8172011-07-18 00:22:30 +0300639 /* there was no proper board file, try to use default instead */
640 ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
641 filename, ret);
642
643 switch (ar->version.target_ver) {
644 case AR6003_REV2_VERSION:
645 filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
646 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800647 case AR6004_REV1_VERSION:
648 filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
649 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300650 default:
651 filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
652 break;
653 }
654
655 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
656 &ar->fw_board_len);
657 if (ret) {
658 ath6kl_err("Failed to get default board file %s: %d\n",
659 filename, ret);
660 return ret;
661 }
662
663 ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
664 ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
665
666 return 0;
667}
668
Kalle Valo772c31e2011-09-07 10:55:16 +0300669static int ath6kl_fetch_otp_file(struct ath6kl *ar)
670{
671 const char *filename;
672 int ret;
673
674 if (ar->fw_otp != NULL)
675 return 0;
676
677 switch (ar->version.target_ver) {
678 case AR6003_REV2_VERSION:
679 filename = AR6003_REV2_OTP_FILE;
680 break;
681 case AR6004_REV1_VERSION:
682 ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
683 return 0;
684 break;
685 default:
686 filename = AR6003_REV3_OTP_FILE;
687 break;
688 }
689
690 ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
691 &ar->fw_otp_len);
692 if (ret) {
693 ath6kl_err("Failed to get OTP file %s: %d\n",
694 filename, ret);
695 return ret;
696 }
697
698 return 0;
699}
700
701static int ath6kl_fetch_fw_file(struct ath6kl *ar)
702{
703 const char *filename;
704 int ret;
705
706 if (ar->fw != NULL)
707 return 0;
708
709 if (testmode) {
710 switch (ar->version.target_ver) {
711 case AR6003_REV2_VERSION:
712 filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
713 break;
714 case AR6003_REV3_VERSION:
715 filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
716 break;
717 case AR6004_REV1_VERSION:
718 ath6kl_warn("testmode not supported with ar6004\n");
719 return -EOPNOTSUPP;
720 default:
721 ath6kl_warn("unknown target version: 0x%x\n",
722 ar->version.target_ver);
723 return -EINVAL;
724 }
725
726 set_bit(TESTMODE, &ar->flag);
727
728 goto get_fw;
729 }
730
731 switch (ar->version.target_ver) {
732 case AR6003_REV2_VERSION:
733 filename = AR6003_REV2_FIRMWARE_FILE;
734 break;
735 case AR6004_REV1_VERSION:
736 filename = AR6004_REV1_FIRMWARE_FILE;
737 break;
738 default:
739 filename = AR6003_REV3_FIRMWARE_FILE;
740 break;
741 }
742
743get_fw:
744 ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
745 if (ret) {
746 ath6kl_err("Failed to get firmware file %s: %d\n",
747 filename, ret);
748 return ret;
749 }
750
751 return 0;
752}
753
754static int ath6kl_fetch_patch_file(struct ath6kl *ar)
755{
756 const char *filename;
757 int ret;
758
759 switch (ar->version.target_ver) {
760 case AR6003_REV2_VERSION:
761 filename = AR6003_REV2_PATCH_FILE;
762 break;
763 case AR6004_REV1_VERSION:
764 /* FIXME: implement for AR6004 */
765 return 0;
766 break;
767 default:
768 filename = AR6003_REV3_PATCH_FILE;
769 break;
770 }
771
772 if (ar->fw_patch == NULL) {
773 ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
774 &ar->fw_patch_len);
775 if (ret) {
776 ath6kl_err("Failed to get patch file %s: %d\n",
777 filename, ret);
778 return ret;
779 }
780 }
781
782 return 0;
783}
784
Kalle Valo50d41232011-09-07 10:55:17 +0300785static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
Kalle Valo772c31e2011-09-07 10:55:16 +0300786{
787 int ret;
788
Kalle Valo772c31e2011-09-07 10:55:16 +0300789 ret = ath6kl_fetch_otp_file(ar);
790 if (ret)
791 return ret;
792
793 ret = ath6kl_fetch_fw_file(ar);
794 if (ret)
795 return ret;
796
797 ret = ath6kl_fetch_patch_file(ar);
798 if (ret)
799 return ret;
800
801 return 0;
802}
Kalle Valobdcd8172011-07-18 00:22:30 +0300803
Kalle Valo50d41232011-09-07 10:55:17 +0300804static int ath6kl_fetch_fw_api2(struct ath6kl *ar)
805{
806 size_t magic_len, len, ie_len;
807 const struct firmware *fw;
808 struct ath6kl_fw_ie *hdr;
809 const char *filename;
810 const u8 *data;
Kalle Valo97e04962011-09-12 13:47:34 +0300811 int ret, ie_id, i, index, bit;
Kalle Valo8a137482011-09-07 10:55:17 +0300812 __le32 *val;
Kalle Valo50d41232011-09-07 10:55:17 +0300813
814 switch (ar->version.target_ver) {
815 case AR6003_REV2_VERSION:
816 filename = AR6003_REV2_FIRMWARE_2_FILE;
817 break;
818 case AR6003_REV3_VERSION:
819 filename = AR6003_REV3_FIRMWARE_2_FILE;
820 break;
821 case AR6004_REV1_VERSION:
822 filename = AR6004_REV1_FIRMWARE_2_FILE;
823 break;
824 default:
825 return -EOPNOTSUPP;
826 }
827
828 ret = request_firmware(&fw, filename, ar->dev);
829 if (ret)
830 return ret;
831
832 data = fw->data;
833 len = fw->size;
834
835 /* magic also includes the null byte, check that as well */
836 magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
837
838 if (len < magic_len) {
839 ret = -EINVAL;
840 goto out;
841 }
842
843 if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
844 ret = -EINVAL;
845 goto out;
846 }
847
848 len -= magic_len;
849 data += magic_len;
850
851 /* loop elements */
852 while (len > sizeof(struct ath6kl_fw_ie)) {
853 /* hdr is unaligned! */
854 hdr = (struct ath6kl_fw_ie *) data;
855
856 ie_id = le32_to_cpup(&hdr->id);
857 ie_len = le32_to_cpup(&hdr->len);
858
859 len -= sizeof(*hdr);
860 data += sizeof(*hdr);
861
862 if (len < ie_len) {
863 ret = -EINVAL;
864 goto out;
865 }
866
867 switch (ie_id) {
868 case ATH6KL_FW_IE_OTP_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300869 ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300870 ie_len);
871
Kalle Valo50d41232011-09-07 10:55:17 +0300872 ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
873
874 if (ar->fw_otp == NULL) {
875 ret = -ENOMEM;
876 goto out;
877 }
878
879 ar->fw_otp_len = ie_len;
880 break;
881 case ATH6KL_FW_IE_FW_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300882 ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300883 ie_len);
884
Kalle Valo50d41232011-09-07 10:55:17 +0300885 ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
886
887 if (ar->fw == NULL) {
888 ret = -ENOMEM;
889 goto out;
890 }
891
892 ar->fw_len = ie_len;
893 break;
894 case ATH6KL_FW_IE_PATCH_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300895 ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300896 ie_len);
897
Kalle Valo50d41232011-09-07 10:55:17 +0300898 ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
899
900 if (ar->fw_patch == NULL) {
901 ret = -ENOMEM;
902 goto out;
903 }
904
905 ar->fw_patch_len = ie_len;
906 break;
Kalle Valo8a137482011-09-07 10:55:17 +0300907 case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
908 val = (__le32 *) data;
909 ar->hw.reserved_ram_size = le32_to_cpup(val);
Kalle Valo6bc36432011-09-27 14:31:11 +0300910
911 ath6kl_dbg(ATH6KL_DBG_BOOT,
912 "found reserved ram size ie 0x%d\n",
913 ar->hw.reserved_ram_size);
Kalle Valo8a137482011-09-07 10:55:17 +0300914 break;
Kalle Valo97e04962011-09-12 13:47:34 +0300915 case ATH6KL_FW_IE_CAPABILITIES:
Kalle Valo6bc36432011-09-27 14:31:11 +0300916 ath6kl_dbg(ATH6KL_DBG_BOOT,
Kalle Valoef548622011-10-01 09:43:09 +0300917 "found firmware capabilities ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300918 ie_len);
919
Kalle Valo97e04962011-09-12 13:47:34 +0300920 for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
921 index = ALIGN(i, 8) / 8;
922 bit = i % 8;
923
924 if (data[index] & (1 << bit))
925 __set_bit(i, ar->fw_capabilities);
926 }
Kalle Valo6bc36432011-09-27 14:31:11 +0300927
928 ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
929 ar->fw_capabilities,
930 sizeof(ar->fw_capabilities));
Kalle Valo97e04962011-09-12 13:47:34 +0300931 break;
Kalle Valo1b4304d2011-09-27 11:05:26 +0300932 case ATH6KL_FW_IE_PATCH_ADDR:
933 if (ie_len != sizeof(*val))
934 break;
935
936 val = (__le32 *) data;
937 ar->hw.dataset_patch_addr = le32_to_cpup(val);
Kalle Valo6bc36432011-09-27 14:31:11 +0300938
939 ath6kl_dbg(ATH6KL_DBG_BOOT,
940 "found patch address ie 0x%d\n",
941 ar->hw.dataset_patch_addr);
Kalle Valo1b4304d2011-09-27 11:05:26 +0300942 break;
Kalle Valo50d41232011-09-07 10:55:17 +0300943 default:
Kalle Valo6bc36432011-09-27 14:31:11 +0300944 ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
Kalle Valo50d41232011-09-07 10:55:17 +0300945 le32_to_cpup(&hdr->id));
946 break;
947 }
948
949 len -= ie_len;
950 data += ie_len;
951 };
952
953 ret = 0;
954out:
955 release_firmware(fw);
956
957 return ret;
958}
959
960static int ath6kl_fetch_firmwares(struct ath6kl *ar)
961{
962 int ret;
963
964 ret = ath6kl_fetch_board_file(ar);
965 if (ret)
966 return ret;
967
968 ret = ath6kl_fetch_fw_api2(ar);
Kalle Valo6bc36432011-09-27 14:31:11 +0300969 if (ret == 0) {
970 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 2\n");
Kalle Valo50d41232011-09-07 10:55:17 +0300971 return 0;
Kalle Valo6bc36432011-09-27 14:31:11 +0300972 }
Kalle Valo50d41232011-09-07 10:55:17 +0300973
974 ret = ath6kl_fetch_fw_api1(ar);
975 if (ret)
976 return ret;
977
Kalle Valo6bc36432011-09-27 14:31:11 +0300978 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 1\n");
979
Kalle Valo50d41232011-09-07 10:55:17 +0300980 return 0;
981}
982
Kalle Valobdcd8172011-07-18 00:22:30 +0300983static int ath6kl_upload_board_file(struct ath6kl *ar)
984{
985 u32 board_address, board_ext_address, param;
Kevin Fang31024d92011-07-11 17:14:13 +0800986 u32 board_data_size, board_ext_data_size;
Kalle Valobdcd8172011-07-18 00:22:30 +0300987 int ret;
988
Kalle Valo772c31e2011-09-07 10:55:16 +0300989 if (WARN_ON(ar->fw_board == NULL))
990 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +0300991
Kevin Fang31024d92011-07-11 17:14:13 +0800992 /*
993 * Determine where in Target RAM to write Board Data.
994 * For AR6004, host determine Target RAM address for
995 * writing board data.
996 */
997 if (ar->target_type == TARGET_TYPE_AR6004) {
998 board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
999 ath6kl_bmi_write(ar,
1000 ath6kl_get_hi_item_addr(ar,
1001 HI_ITEM(hi_board_data)),
1002 (u8 *) &board_address, 4);
1003 } else {
1004 ath6kl_bmi_read(ar,
1005 ath6kl_get_hi_item_addr(ar,
1006 HI_ITEM(hi_board_data)),
1007 (u8 *) &board_address, 4);
1008 }
1009
Kalle Valobdcd8172011-07-18 00:22:30 +03001010 /* determine where in target ram to write extended board data */
1011 ath6kl_bmi_read(ar,
1012 ath6kl_get_hi_item_addr(ar,
1013 HI_ITEM(hi_board_ext_data)),
1014 (u8 *) &board_ext_address, 4);
1015
Kalle Valobdcd8172011-07-18 00:22:30 +03001016 if (board_ext_address == 0) {
1017 ath6kl_err("Failed to get board file target address.\n");
1018 return -EINVAL;
1019 }
1020
Kevin Fang31024d92011-07-11 17:14:13 +08001021 switch (ar->target_type) {
1022 case TARGET_TYPE_AR6003:
1023 board_data_size = AR6003_BOARD_DATA_SZ;
1024 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1025 break;
1026 case TARGET_TYPE_AR6004:
1027 board_data_size = AR6004_BOARD_DATA_SZ;
1028 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
1029 break;
1030 default:
1031 WARN_ON(1);
1032 return -EINVAL;
1033 break;
1034 }
1035
1036 if (ar->fw_board_len == (board_data_size +
1037 board_ext_data_size)) {
1038
Kalle Valobdcd8172011-07-18 00:22:30 +03001039 /* write extended board data */
Kalle Valo6bc36432011-09-27 14:31:11 +03001040 ath6kl_dbg(ATH6KL_DBG_BOOT,
1041 "writing extended board data to 0x%x (%d B)\n",
1042 board_ext_address, board_ext_data_size);
1043
Kalle Valobdcd8172011-07-18 00:22:30 +03001044 ret = ath6kl_bmi_write(ar, board_ext_address,
Kevin Fang31024d92011-07-11 17:14:13 +08001045 ar->fw_board + board_data_size,
1046 board_ext_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001047 if (ret) {
1048 ath6kl_err("Failed to write extended board data: %d\n",
1049 ret);
1050 return ret;
1051 }
1052
1053 /* record that extended board data is initialized */
Kevin Fang31024d92011-07-11 17:14:13 +08001054 param = (board_ext_data_size << 16) | 1;
1055
Kalle Valobdcd8172011-07-18 00:22:30 +03001056 ath6kl_bmi_write(ar,
1057 ath6kl_get_hi_item_addr(ar,
1058 HI_ITEM(hi_board_ext_data_config)),
1059 (unsigned char *) &param, 4);
1060 }
1061
Kevin Fang31024d92011-07-11 17:14:13 +08001062 if (ar->fw_board_len < board_data_size) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001063 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1064 ret = -EINVAL;
1065 return ret;
1066 }
1067
Kalle Valo6bc36432011-09-27 14:31:11 +03001068 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
1069 board_address, board_data_size);
1070
Kalle Valobdcd8172011-07-18 00:22:30 +03001071 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
Kevin Fang31024d92011-07-11 17:14:13 +08001072 board_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001073
1074 if (ret) {
1075 ath6kl_err("Board file bmi write failed: %d\n", ret);
1076 return ret;
1077 }
1078
1079 /* record the fact that Board Data IS initialized */
1080 param = 1;
1081 ath6kl_bmi_write(ar,
1082 ath6kl_get_hi_item_addr(ar,
1083 HI_ITEM(hi_board_data_initialized)),
1084 (u8 *)&param, 4);
1085
1086 return ret;
1087}
1088
1089static int ath6kl_upload_otp(struct ath6kl *ar)
1090{
Kalle Valobdcd8172011-07-18 00:22:30 +03001091 u32 address, param;
Kalle Valobef26a72011-10-12 09:58:28 +03001092 bool from_hw = false;
Kalle Valobdcd8172011-07-18 00:22:30 +03001093 int ret;
1094
Kalle Valo772c31e2011-09-07 10:55:16 +03001095 if (WARN_ON(ar->fw_otp == NULL))
1096 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001097
Kalle Valoa01ac412011-09-07 10:55:17 +03001098 address = ar->hw.app_load_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001099
Kalle Valoef548622011-10-01 09:43:09 +03001100 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
Kalle Valo6bc36432011-09-27 14:31:11 +03001101 ar->fw_otp_len);
1102
Kalle Valobdcd8172011-07-18 00:22:30 +03001103 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1104 ar->fw_otp_len);
1105 if (ret) {
1106 ath6kl_err("Failed to upload OTP file: %d\n", ret);
1107 return ret;
1108 }
1109
Kalle Valo639d0b82011-09-12 12:48:09 +03001110 /* read firmware start address */
1111 ret = ath6kl_bmi_read(ar,
1112 ath6kl_get_hi_item_addr(ar,
1113 HI_ITEM(hi_app_start)),
1114 (u8 *) &address, sizeof(address));
1115
1116 if (ret) {
1117 ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1118 return ret;
1119 }
1120
Kalle Valobef26a72011-10-12 09:58:28 +03001121 if (ar->hw.app_start_override_addr == 0) {
1122 ar->hw.app_start_override_addr = address;
1123 from_hw = true;
1124 }
Kalle Valo639d0b82011-09-12 12:48:09 +03001125
Kalle Valobef26a72011-10-12 09:58:28 +03001126 ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1127 from_hw ? " (from hw)" : "",
Kalle Valo6bc36432011-09-27 14:31:11 +03001128 ar->hw.app_start_override_addr);
1129
Kalle Valobdcd8172011-07-18 00:22:30 +03001130 /* execute the OTP code */
Kalle Valobef26a72011-10-12 09:58:28 +03001131 ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1132 ar->hw.app_start_override_addr);
Kalle Valobdcd8172011-07-18 00:22:30 +03001133 param = 0;
Kalle Valobef26a72011-10-12 09:58:28 +03001134 ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
Kalle Valobdcd8172011-07-18 00:22:30 +03001135
1136 return ret;
1137}
1138
1139static int ath6kl_upload_firmware(struct ath6kl *ar)
1140{
Kalle Valobdcd8172011-07-18 00:22:30 +03001141 u32 address;
1142 int ret;
1143
Kalle Valo772c31e2011-09-07 10:55:16 +03001144 if (WARN_ON(ar->fw == NULL))
1145 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001146
Kalle Valoa01ac412011-09-07 10:55:17 +03001147 address = ar->hw.app_load_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001148
Kalle Valoef548622011-10-01 09:43:09 +03001149 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +03001150 address, ar->fw_len);
1151
Kalle Valobdcd8172011-07-18 00:22:30 +03001152 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1153
1154 if (ret) {
1155 ath6kl_err("Failed to write firmware: %d\n", ret);
1156 return ret;
1157 }
1158
Kevin Fang31024d92011-07-11 17:14:13 +08001159 /*
1160 * Set starting address for firmware
1161 * Don't need to setup app_start override addr on AR6004
1162 */
1163 if (ar->target_type != TARGET_TYPE_AR6004) {
Kalle Valoa01ac412011-09-07 10:55:17 +03001164 address = ar->hw.app_start_override_addr;
Kevin Fang31024d92011-07-11 17:14:13 +08001165 ath6kl_bmi_set_app_start(ar, address);
1166 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001167 return ret;
1168}
1169
1170static int ath6kl_upload_patch(struct ath6kl *ar)
1171{
Kalle Valobdcd8172011-07-18 00:22:30 +03001172 u32 address, param;
1173 int ret;
1174
Kalle Valo772c31e2011-09-07 10:55:16 +03001175 if (WARN_ON(ar->fw_patch == NULL))
1176 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001177
Kalle Valoa01ac412011-09-07 10:55:17 +03001178 address = ar->hw.dataset_patch_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001179
Kalle Valoef548622011-10-01 09:43:09 +03001180 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +03001181 address, ar->fw_patch_len);
1182
Kalle Valobdcd8172011-07-18 00:22:30 +03001183 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1184 if (ret) {
1185 ath6kl_err("Failed to write patch file: %d\n", ret);
1186 return ret;
1187 }
1188
1189 param = address;
1190 ath6kl_bmi_write(ar,
1191 ath6kl_get_hi_item_addr(ar,
1192 HI_ITEM(hi_dset_list_head)),
1193 (unsigned char *) &param, 4);
1194
1195 return 0;
1196}
1197
1198static int ath6kl_init_upload(struct ath6kl *ar)
1199{
1200 u32 param, options, sleep, address;
1201 int status = 0;
1202
Kevin Fang31024d92011-07-11 17:14:13 +08001203 if (ar->target_type != TARGET_TYPE_AR6003 &&
1204 ar->target_type != TARGET_TYPE_AR6004)
Kalle Valobdcd8172011-07-18 00:22:30 +03001205 return -EINVAL;
1206
1207 /* temporarily disable system sleep */
1208 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1209 status = ath6kl_bmi_reg_read(ar, address, &param);
1210 if (status)
1211 return status;
1212
1213 options = param;
1214
1215 param |= ATH6KL_OPTION_SLEEP_DISABLE;
1216 status = ath6kl_bmi_reg_write(ar, address, param);
1217 if (status)
1218 return status;
1219
1220 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1221 status = ath6kl_bmi_reg_read(ar, address, &param);
1222 if (status)
1223 return status;
1224
1225 sleep = param;
1226
1227 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1228 status = ath6kl_bmi_reg_write(ar, address, param);
1229 if (status)
1230 return status;
1231
1232 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1233 options, sleep);
1234
1235 /* program analog PLL register */
Kevin Fang31024d92011-07-11 17:14:13 +08001236 /* no need to control 40/44MHz clock on AR6004 */
1237 if (ar->target_type != TARGET_TYPE_AR6004) {
1238 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1239 0xF9104001);
Kalle Valobdcd8172011-07-18 00:22:30 +03001240
Kevin Fang31024d92011-07-11 17:14:13 +08001241 if (status)
1242 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +03001243
Kevin Fang31024d92011-07-11 17:14:13 +08001244 /* Run at 80/88MHz by default */
1245 param = SM(CPU_CLOCK_STANDARD, 1);
1246
1247 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1248 status = ath6kl_bmi_reg_write(ar, address, param);
1249 if (status)
1250 return status;
1251 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001252
1253 param = 0;
1254 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1255 param = SM(LPO_CAL_ENABLE, 1);
1256 status = ath6kl_bmi_reg_write(ar, address, param);
1257 if (status)
1258 return status;
1259
1260 /* WAR to avoid SDIO CRC err */
1261 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1262 ath6kl_err("temporary war to avoid sdio crc error\n");
1263
1264 param = 0x20;
1265
1266 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1267 status = ath6kl_bmi_reg_write(ar, address, param);
1268 if (status)
1269 return status;
1270
1271 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1272 status = ath6kl_bmi_reg_write(ar, address, param);
1273 if (status)
1274 return status;
1275
1276 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1277 status = ath6kl_bmi_reg_write(ar, address, param);
1278 if (status)
1279 return status;
1280
1281 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1282 status = ath6kl_bmi_reg_write(ar, address, param);
1283 if (status)
1284 return status;
1285 }
1286
1287 /* write EEPROM data to Target RAM */
1288 status = ath6kl_upload_board_file(ar);
1289 if (status)
1290 return status;
1291
1292 /* transfer One time Programmable data */
1293 status = ath6kl_upload_otp(ar);
1294 if (status)
1295 return status;
1296
1297 /* Download Target firmware */
1298 status = ath6kl_upload_firmware(ar);
1299 if (status)
1300 return status;
1301
1302 status = ath6kl_upload_patch(ar);
1303 if (status)
1304 return status;
1305
1306 /* Restore system sleep */
1307 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1308 status = ath6kl_bmi_reg_write(ar, address, sleep);
1309 if (status)
1310 return status;
1311
1312 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1313 param = options | 0x20;
1314 status = ath6kl_bmi_reg_write(ar, address, param);
1315 if (status)
1316 return status;
1317
1318 /* Configure GPIO AR6003 UART */
1319 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1320 status = ath6kl_bmi_write(ar,
1321 ath6kl_get_hi_item_addr(ar,
1322 HI_ITEM(hi_dbg_uart_txpin)),
1323 (u8 *)&param, 4);
1324
1325 return status;
1326}
1327
Kalle Valoa01ac412011-09-07 10:55:17 +03001328static int ath6kl_init_hw_params(struct ath6kl *ar)
1329{
1330 switch (ar->version.target_ver) {
1331 case AR6003_REV2_VERSION:
1332 ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
1333 ar->hw.app_load_addr = AR6003_REV2_APP_LOAD_ADDRESS;
Kalle Valo991b27e2011-09-07 10:55:17 +03001334 ar->hw.board_ext_data_addr = AR6003_REV2_BOARD_EXT_DATA_ADDRESS;
1335 ar->hw.reserved_ram_size = AR6003_REV2_RAM_RESERVE_SIZE;
Kalle Valobef26a72011-10-12 09:58:28 +03001336
1337 /* hw2.0 needs override address hardcoded */
1338 ar->hw.app_start_override_addr = 0x944C00;
1339
Kalle Valoa01ac412011-09-07 10:55:17 +03001340 break;
1341 case AR6003_REV3_VERSION:
1342 ar->hw.dataset_patch_addr = AR6003_REV3_DATASET_PATCH_ADDRESS;
1343 ar->hw.app_load_addr = 0x1234;
Kalle Valo991b27e2011-09-07 10:55:17 +03001344 ar->hw.board_ext_data_addr = AR6003_REV3_BOARD_EXT_DATA_ADDRESS;
1345 ar->hw.reserved_ram_size = AR6003_REV3_RAM_RESERVE_SIZE;
Kalle Valoa01ac412011-09-07 10:55:17 +03001346 break;
1347 case AR6004_REV1_VERSION:
1348 ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
1349 ar->hw.app_load_addr = AR6003_REV3_APP_LOAD_ADDRESS;
Kalle Valo991b27e2011-09-07 10:55:17 +03001350 ar->hw.board_ext_data_addr = AR6004_REV1_BOARD_EXT_DATA_ADDRESS;
1351 ar->hw.reserved_ram_size = AR6004_REV1_RAM_RESERVE_SIZE;
Kalle Valoa01ac412011-09-07 10:55:17 +03001352 break;
1353 default:
1354 ath6kl_err("Unsupported hardware version: 0x%x\n",
1355 ar->version.target_ver);
1356 return -EINVAL;
1357 }
1358
Kalle Valo6bc36432011-09-27 14:31:11 +03001359 ath6kl_dbg(ATH6KL_DBG_BOOT,
1360 "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
1361 ar->version.target_ver, ar->target_type,
1362 ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
1363 ath6kl_dbg(ATH6KL_DBG_BOOT,
1364 "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
1365 ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
1366 ar->hw.reserved_ram_size);
1367
Kalle Valoa01ac412011-09-07 10:55:17 +03001368 return 0;
1369}
1370
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001371int ath6kl_init_hw_start(struct ath6kl *ar)
Kalle Valo20459ee2011-10-27 18:48:37 +03001372{
1373 long timeleft;
1374 int ret, i;
1375
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001376 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
1377
Kalle Valo20459ee2011-10-27 18:48:37 +03001378 ret = ath6kl_hif_power_on(ar);
1379 if (ret)
1380 return ret;
1381
1382 ret = ath6kl_configure_target(ar);
1383 if (ret)
1384 goto err_power_off;
1385
1386 ret = ath6kl_init_upload(ar);
1387 if (ret)
1388 goto err_power_off;
1389
1390 /* Do we need to finish the BMI phase */
1391 /* FIXME: return error from ath6kl_bmi_done() */
1392 if (ath6kl_bmi_done(ar)) {
1393 ret = -EIO;
1394 goto err_power_off;
1395 }
1396
1397 /*
1398 * The reason we have to wait for the target here is that the
1399 * driver layer has to init BMI in order to set the host block
1400 * size.
1401 */
1402 if (ath6kl_htc_wait_target(ar->htc_target)) {
1403 ret = -EIO;
1404 goto err_power_off;
1405 }
1406
1407 if (ath6kl_init_service_ep(ar)) {
1408 ret = -EIO;
1409 goto err_cleanup_scatter;
1410 }
1411
1412 /* setup credit distribution */
1413 ath6kl_credit_setup(ar->htc_target, &ar->credit_state_info);
1414
1415 /* start HTC */
1416 ret = ath6kl_htc_start(ar->htc_target);
1417 if (ret) {
1418 /* FIXME: call this */
1419 ath6kl_cookie_cleanup(ar);
1420 goto err_cleanup_scatter;
1421 }
1422
1423 /* Wait for Wmi event to be ready */
1424 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1425 test_bit(WMI_READY,
1426 &ar->flag),
1427 WMI_TIMEOUT);
1428
1429 ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
1430
1431 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1432 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1433 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1434 ret = -EIO;
1435 goto err_htc_stop;
1436 }
1437
1438 if (!timeleft || signal_pending(current)) {
1439 ath6kl_err("wmi is not ready or wait was interrupted\n");
1440 ret = -EIO;
1441 goto err_htc_stop;
1442 }
1443
1444 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1445
1446 /* communicate the wmi protocol verision to the target */
1447 /* FIXME: return error */
1448 if ((ath6kl_set_host_app_area(ar)) != 0)
1449 ath6kl_err("unable to set the host app area\n");
1450
1451 for (i = 0; i < MAX_NUM_VIF; i++) {
1452 ret = ath6kl_target_config_wlan_params(ar, i);
1453 if (ret)
1454 goto err_htc_stop;
1455 }
1456
Kalle Valo76a9fbe2011-11-01 08:44:28 +02001457 ar->state = ATH6KL_STATE_ON;
1458
Kalle Valo20459ee2011-10-27 18:48:37 +03001459 return 0;
1460
1461err_htc_stop:
1462 ath6kl_htc_stop(ar->htc_target);
1463err_cleanup_scatter:
1464 ath6kl_hif_cleanup_scatter(ar);
1465err_power_off:
1466 ath6kl_hif_power_off(ar);
1467
1468 return ret;
1469}
1470
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001471int ath6kl_init_hw_stop(struct ath6kl *ar)
1472{
1473 int ret;
1474
1475 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
1476
1477 ath6kl_htc_stop(ar->htc_target);
1478
1479 ath6kl_hif_stop(ar);
1480
1481 ath6kl_bmi_reset(ar);
1482
1483 ret = ath6kl_hif_power_off(ar);
1484 if (ret)
1485 ath6kl_warn("failed to power off hif: %d\n", ret);
1486
Kalle Valo76a9fbe2011-11-01 08:44:28 +02001487 ar->state = ATH6KL_STATE_OFF;
1488
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001489 return 0;
1490}
1491
Kalle Valobdcd8172011-07-18 00:22:30 +03001492int ath6kl_core_init(struct ath6kl *ar)
1493{
Kalle Valobdcd8172011-07-18 00:22:30 +03001494 struct ath6kl_bmi_target_info targ_info;
Kalle Valo61448a92011-10-27 18:48:29 +03001495 struct net_device *ndev;
Kalle Valo20459ee2011-10-27 18:48:37 +03001496 int ret = 0, i;
Kalle Valobdcd8172011-07-18 00:22:30 +03001497
1498 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1499 if (!ar->ath6kl_wq)
1500 return -ENOMEM;
1501
1502 ret = ath6kl_bmi_init(ar);
1503 if (ret)
1504 goto err_wq;
1505
Kalle Valo20459ee2011-10-27 18:48:37 +03001506 /*
1507 * Turn on power to get hardware (target) version and leave power
1508 * on delibrately as we will boot the hardware anyway within few
1509 * seconds.
1510 */
Kalle Valob2e75692011-10-27 18:48:14 +03001511 ret = ath6kl_hif_power_on(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001512 if (ret)
1513 goto err_bmi_cleanup;
1514
Kalle Valob2e75692011-10-27 18:48:14 +03001515 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1516 if (ret)
1517 goto err_power_off;
1518
Kalle Valobdcd8172011-07-18 00:22:30 +03001519 ar->version.target_ver = le32_to_cpu(targ_info.version);
1520 ar->target_type = le32_to_cpu(targ_info.type);
Vasanthakumar Thiagarajanbe98e3a2011-10-25 19:33:57 +05301521 ar->wiphy->hw_version = le32_to_cpu(targ_info.version);
Kalle Valobdcd8172011-07-18 00:22:30 +03001522
Kalle Valoa01ac412011-09-07 10:55:17 +03001523 ret = ath6kl_init_hw_params(ar);
1524 if (ret)
Kalle Valob2e75692011-10-27 18:48:14 +03001525 goto err_power_off;
Kalle Valoa01ac412011-09-07 10:55:17 +03001526
Kalle Vaload226ec2011-08-10 09:49:12 +03001527 ar->htc_target = ath6kl_htc_create(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001528
1529 if (!ar->htc_target) {
1530 ret = -ENOMEM;
Kalle Valob2e75692011-10-27 18:48:14 +03001531 goto err_power_off;
Kalle Valobdcd8172011-07-18 00:22:30 +03001532 }
1533
Kalle Valo772c31e2011-09-07 10:55:16 +03001534 ret = ath6kl_fetch_firmwares(ar);
1535 if (ret)
1536 goto err_htc_cleanup;
1537
Kalle Valo61448a92011-10-27 18:48:29 +03001538 /* FIXME: we should free all firmwares in the error cases below */
1539
Kalle Valo61448a92011-10-27 18:48:29 +03001540 /* Indicate that WMI is enabled (although not ready yet) */
1541 set_bit(WMI_ENABLED, &ar->flag);
1542 ar->wmi = ath6kl_wmi_init(ar);
1543 if (!ar->wmi) {
1544 ath6kl_err("failed to initialize wmi\n");
1545 ret = -EIO;
1546 goto err_htc_cleanup;
1547 }
1548
1549 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1550
1551 ret = ath6kl_register_ieee80211_hw(ar);
1552 if (ret)
1553 goto err_node_cleanup;
1554
1555 ret = ath6kl_debug_init(ar);
1556 if (ret) {
1557 wiphy_unregister(ar->wiphy);
1558 goto err_node_cleanup;
1559 }
1560
1561 for (i = 0; i < MAX_NUM_VIF; i++)
1562 ar->avail_idx_map |= BIT(i);
1563
1564 rtnl_lock();
1565
1566 /* Add an initial station interface */
1567 ndev = ath6kl_interface_add(ar, "wlan%d", NL80211_IFTYPE_STATION, 0,
1568 INFRA_NETWORK);
1569
1570 rtnl_unlock();
1571
1572 if (!ndev) {
1573 ath6kl_err("Failed to instantiate a network device\n");
1574 ret = -ENOMEM;
1575 wiphy_unregister(ar->wiphy);
1576 goto err_debug_init;
1577 }
1578
1579
1580 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1581 __func__, ndev->name, ndev, ar);
1582
Kalle Valo61448a92011-10-27 18:48:29 +03001583 /* setup access class priority mappings */
1584 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1585 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1586 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1587 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1588
1589 /* give our connected endpoints some buffers */
1590 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1591 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1592
1593 /* allocate some buffers that handle larger AMSDU frames */
1594 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1595
Kalle Valo61448a92011-10-27 18:48:29 +03001596 ath6kl_cookie_init(ar);
1597
Kalle Valo61448a92011-10-27 18:48:29 +03001598 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1599 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1600
Kalle Valo8277de12011-11-03 12:18:31 +02001601 if (suspend_cutpower)
1602 ar->conf_flags |= ATH6KL_CONF_SUSPEND_CUTPOWER;
1603
Kalle Valo61448a92011-10-27 18:48:29 +03001604 ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM |
Johannes Berg7c4ef712011-11-18 15:33:48 +01001605 WIPHY_FLAG_HAVE_AP_SME |
1606 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
Kalle Valo61448a92011-10-27 18:48:29 +03001607
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001608 set_bit(FIRST_BOOT, &ar->flag);
1609
1610 ret = ath6kl_init_hw_start(ar);
Kalle Valo20459ee2011-10-27 18:48:37 +03001611 if (ret) {
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001612 ath6kl_err("Failed to start hardware: %d\n", ret);
Kalle Valo20459ee2011-10-27 18:48:37 +03001613 goto err_rxbuf_cleanup;
Kalle Valo61448a92011-10-27 18:48:29 +03001614 }
1615
1616 /*
1617 * Set mac address which is received in ready event
1618 * FIXME: Move to ath6kl_interface_add()
1619 */
1620 memcpy(ndev->dev_addr, ar->mac_addr, ETH_ALEN);
Kalle Valobdcd8172011-07-18 00:22:30 +03001621
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 Thiagarajan11f6e402011-11-01 16:38:50 +05301688 spin_lock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301689 list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1690 list_del(&vif->list);
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301691 spin_unlock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301692 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 Thiagarajan11f6e402011-11-01 16:38:50 +05301696 spin_lock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301697 }
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301698 spin_unlock_bh(&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}