blob: 77444d1edd5abdd01237437e95f2ea55f1c93038 [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 Rothwellc6efe572011-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 Valo003353b2011-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 Valo003353b2011-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
Kalle Valo856f4b32011-11-14 19:30:29 +020036static const struct ath6kl_hw hw_list[] = {
37 {
38 .id = AR6003_REV2_VERSION,
39 .dataset_patch_addr = 0x57e884,
40 .app_load_addr = 0x543180,
41 .board_ext_data_addr = 0x57e500,
42 .reserved_ram_size = 6912,
43
44 /* hw2.0 needs override address hardcoded */
45 .app_start_override_addr = 0x944C00,
46 },
47 {
48 .id = AR6003_REV3_VERSION,
49 .dataset_patch_addr = 0x57ff74,
50 .app_load_addr = 0x1234,
51 .board_ext_data_addr = 0x542330,
52 .reserved_ram_size = 512,
53 },
54 {
55 .id = AR6004_REV1_VERSION,
56 .dataset_patch_addr = 0x57e884,
57 .app_load_addr = 0x1234,
58 .board_ext_data_addr = 0x437000,
59 .reserved_ram_size = 19456,
60 },
61 {
62 .id = AR6004_REV2_VERSION,
63 .dataset_patch_addr = 0x57e884,
64 .app_load_addr = 0x1234,
65 .board_ext_data_addr = 0x437000,
66 .reserved_ram_size = 11264,
67 },
68};
69
Kalle Valobdcd8172011-07-18 00:22:30 +030070/*
71 * Include definitions here that can be used to tune the WLAN module
72 * behavior. Different customers can tune the behavior as per their needs,
73 * here.
74 */
75
76/*
77 * This configuration item enable/disable keepalive support.
78 * Keepalive support: In the absence of any data traffic to AP, null
79 * frames will be sent to the AP at periodic interval, to keep the association
80 * active. This configuration item defines the periodic interval.
81 * Use value of zero to disable keepalive support
82 * Default: 60 seconds
83 */
84#define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
85
86/*
87 * This configuration item sets the value of disconnect timeout
88 * Firmware delays sending the disconnec event to the host for this
89 * timeout after is gets disconnected from the current AP.
90 * If the firmware successly roams within the disconnect timeout
91 * it sends a new connect event
92 */
93#define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
94
95#define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
96
Kalle Valobdcd8172011-07-18 00:22:30 +030097#define ATH6KL_DATA_OFFSET 64
98struct sk_buff *ath6kl_buf_alloc(int size)
99{
100 struct sk_buff *skb;
101 u16 reserved;
102
103 /* Add chacheline space at front and back of buffer */
104 reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
Vasanthakumar Thiagarajan1df94a82011-08-17 18:45:10 +0530105 sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
Kalle Valobdcd8172011-07-18 00:22:30 +0300106 skb = dev_alloc_skb(size + reserved);
107
108 if (skb)
109 skb_reserve(skb, reserved - L1_CACHE_BYTES);
110 return skb;
111}
112
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +0530113void ath6kl_init_profile_info(struct ath6kl_vif *vif)
Kalle Valobdcd8172011-07-18 00:22:30 +0300114{
Vasanthakumar Thiagarajan34503342011-10-25 19:34:02 +0530115 vif->ssid_len = 0;
116 memset(vif->ssid, 0, sizeof(vif->ssid));
117
118 vif->dot11_auth_mode = OPEN_AUTH;
119 vif->auth_mode = NONE_AUTH;
120 vif->prwise_crypto = NONE_CRYPT;
121 vif->prwise_crypto_len = 0;
122 vif->grp_crypto = NONE_CRYPT;
123 vif->grp_crypto_len = 0;
Vasanthakumar Thiagarajan6f2a73f2011-10-25 19:34:06 +0530124 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
Vasanthakumar Thiagarajan8c8b65e2011-10-25 19:34:04 +0530125 memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
126 memset(vif->bssid, 0, sizeof(vif->bssid));
Vasanthakumar Thiagarajanf74bac52011-10-25 19:34:05 +0530127 vif->bss_ch = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +0300128}
129
Kalle Valobdcd8172011-07-18 00:22:30 +0300130static int ath6kl_set_host_app_area(struct ath6kl *ar)
131{
132 u32 address, data;
133 struct host_app_area host_app_area;
134
135 /* Fetch the address of the host_app_area_s
136 * instance in the host interest area */
137 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
Kevin Fang31024d92011-07-11 17:14:13 +0800138 address = TARG_VTOP(ar->target_type, address);
Kalle Valobdcd8172011-07-18 00:22:30 +0300139
Kalle Valoaddb44b2011-09-02 10:32:05 +0300140 if (ath6kl_diag_read32(ar, address, &data))
Kalle Valobdcd8172011-07-18 00:22:30 +0300141 return -EIO;
142
Kevin Fang31024d92011-07-11 17:14:13 +0800143 address = TARG_VTOP(ar->target_type, data);
Kalle Valocbf49a62011-10-05 12:23:17 +0300144 host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
Kalle Valoaddb44b2011-09-02 10:32:05 +0300145 if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
146 sizeof(struct host_app_area)))
Kalle Valobdcd8172011-07-18 00:22:30 +0300147 return -EIO;
148
149 return 0;
150}
151
152static inline void set_ac2_ep_map(struct ath6kl *ar,
153 u8 ac,
154 enum htc_endpoint_id ep)
155{
156 ar->ac2ep_map[ac] = ep;
157 ar->ep2ac_map[ep] = ac;
158}
159
160/* connect to a service */
161static int ath6kl_connectservice(struct ath6kl *ar,
162 struct htc_service_connect_req *con_req,
163 char *desc)
164{
165 int status;
166 struct htc_service_connect_resp response;
167
168 memset(&response, 0, sizeof(response));
169
Kalle Vaload226ec2011-08-10 09:49:12 +0300170 status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
Kalle Valobdcd8172011-07-18 00:22:30 +0300171 if (status) {
172 ath6kl_err("failed to connect to %s service status:%d\n",
173 desc, status);
174 return status;
175 }
176
177 switch (con_req->svc_id) {
178 case WMI_CONTROL_SVC:
179 if (test_bit(WMI_ENABLED, &ar->flag))
180 ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
181 ar->ctrl_ep = response.endpoint;
182 break;
183 case WMI_DATA_BE_SVC:
184 set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
185 break;
186 case WMI_DATA_BK_SVC:
187 set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
188 break;
189 case WMI_DATA_VI_SVC:
190 set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
191 break;
192 case WMI_DATA_VO_SVC:
193 set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
194 break;
195 default:
196 ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
197 return -EINVAL;
198 }
199
200 return 0;
201}
202
203static int ath6kl_init_service_ep(struct ath6kl *ar)
204{
205 struct htc_service_connect_req connect;
206
207 memset(&connect, 0, sizeof(connect));
208
209 /* these fields are the same for all service endpoints */
210 connect.ep_cb.rx = ath6kl_rx;
211 connect.ep_cb.rx_refill = ath6kl_rx_refill;
212 connect.ep_cb.tx_full = ath6kl_tx_queue_full;
213
214 /*
215 * Set the max queue depth so that our ath6kl_tx_queue_full handler
216 * gets called.
217 */
218 connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
219 connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
220 if (!connect.ep_cb.rx_refill_thresh)
221 connect.ep_cb.rx_refill_thresh++;
222
223 /* connect to control service */
224 connect.svc_id = WMI_CONTROL_SVC;
225 if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
226 return -EIO;
227
228 connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
229
230 /*
231 * Limit the HTC message size on the send path, although e can
232 * receive A-MSDU frames of 4K, we will only send ethernet-sized
233 * (802.3) frames on the send path.
234 */
235 connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
236
237 /*
238 * To reduce the amount of committed memory for larger A_MSDU
239 * frames, use the recv-alloc threshold mechanism for larger
240 * packets.
241 */
242 connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
243 connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
244
245 /*
246 * For the remaining data services set the connection flag to
247 * reduce dribbling, if configured to do so.
248 */
249 connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
250 connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
251 connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
252
253 connect.svc_id = WMI_DATA_BE_SVC;
254
255 if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
256 return -EIO;
257
258 /* connect to back-ground map this to WMI LOW_PRI */
259 connect.svc_id = WMI_DATA_BK_SVC;
260 if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
261 return -EIO;
262
263 /* connect to Video service, map this to to HI PRI */
264 connect.svc_id = WMI_DATA_VI_SVC;
265 if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
266 return -EIO;
267
268 /*
269 * Connect to VO service, this is currently not mapped to a WMI
270 * priority stream due to historical reasons. WMI originally
271 * defined 3 priorities over 3 mailboxes We can change this when
272 * WMI is reworked so that priorities are not dependent on
273 * mailboxes.
274 */
275 connect.svc_id = WMI_DATA_VO_SVC;
276 if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
277 return -EIO;
278
279 return 0;
280}
281
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +0530282void ath6kl_init_control_info(struct ath6kl_vif *vif)
Kalle Valobdcd8172011-07-18 00:22:30 +0300283{
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +0530284 ath6kl_init_profile_info(vif);
Vasanthakumar Thiagarajan34503342011-10-25 19:34:02 +0530285 vif->def_txkey_index = 0;
Vasanthakumar Thiagarajan6f2a73f2011-10-25 19:34:06 +0530286 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
Vasanthakumar Thiagarajanf74bac52011-10-25 19:34:05 +0530287 vif->ch_hint = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +0300288}
289
290/*
291 * Set HTC/Mbox operational parameters, this can only be called when the
292 * target is in the BMI phase.
293 */
294static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
295 u8 htc_ctrl_buf)
296{
297 int status;
298 u32 blk_size;
299
300 blk_size = ar->mbox_info.block_size;
301
302 if (htc_ctrl_buf)
303 blk_size |= ((u32)htc_ctrl_buf) << 16;
304
305 /* set the host interest area for the block size */
306 status = ath6kl_bmi_write(ar,
307 ath6kl_get_hi_item_addr(ar,
308 HI_ITEM(hi_mbox_io_block_sz)),
309 (u8 *)&blk_size,
310 4);
311 if (status) {
312 ath6kl_err("bmi_write_memory for IO block size failed\n");
313 goto out;
314 }
315
316 ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
317 blk_size,
318 ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
319
320 if (mbox_isr_yield_val) {
321 /* set the host interest area for the mbox ISR yield limit */
322 status = ath6kl_bmi_write(ar,
323 ath6kl_get_hi_item_addr(ar,
324 HI_ITEM(hi_mbox_isr_yield_limit)),
325 (u8 *)&mbox_isr_yield_val,
326 4);
327 if (status) {
328 ath6kl_err("bmi_write_memory for yield limit failed\n");
329 goto out;
330 }
331 }
332
333out:
334 return status;
335}
336
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530337static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
Kalle Valobdcd8172011-07-18 00:22:30 +0300338{
339 int status = 0;
Jouni Malinen4dea08e2011-08-30 21:57:57 +0300340 int ret;
Kalle Valobdcd8172011-07-18 00:22:30 +0300341
342 /*
343 * Configure the device for rx dot11 header rules. "0,0" are the
344 * default values. Required if checksum offload is needed. Set
345 * RxMetaVersion to 2.
346 */
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530347 if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300348 ar->rx_meta_ver, 0, 0)) {
349 ath6kl_err("unable to set the rx frame format\n");
350 status = -EIO;
351 }
352
353 if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530354 if ((ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
Kalle Valobdcd8172011-07-18 00:22:30 +0300355 IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
356 ath6kl_err("unable to set power save fail event policy\n");
357 status = -EIO;
358 }
359
360 if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530361 if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
Kalle Valobdcd8172011-07-18 00:22:30 +0300362 WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
363 ath6kl_err("unable to set barker preamble policy\n");
364 status = -EIO;
365 }
366
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530367 if (ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300368 WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
369 ath6kl_err("unable to set keep alive interval\n");
370 status = -EIO;
371 }
372
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530373 if (ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300374 WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
375 ath6kl_err("unable to set disconnect timeout\n");
376 status = -EIO;
377 }
378
379 if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530380 if (ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED)) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300381 ath6kl_err("unable to set txop bursting\n");
382 status = -EIO;
383 }
384
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530385 /*
386 * FIXME: Make sure p2p configurations are not applied to
387 * non-p2p capable interfaces when multivif support is enabled.
388 */
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300389 if (ar->p2p) {
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530390 ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300391 P2P_FLAG_CAPABILITIES_REQ |
392 P2P_FLAG_MACADDR_REQ |
393 P2P_FLAG_HMODEL_REQ);
394 if (ret) {
395 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
396 "capabilities (%d) - assuming P2P not "
397 "supported\n", ret);
398 ar->p2p = 0;
399 }
400 }
401
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530402 /*
403 * FIXME: Make sure p2p configurations are not applied to
404 * non-p2p capable interfaces when multivif support is enabled.
405 */
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300406 if (ar->p2p) {
407 /* Enable Probe Request reporting for P2P */
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530408 ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300409 if (ret) {
410 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
411 "Request reporting (%d)\n", ret);
412 }
Jouni Malinen4dea08e2011-08-30 21:57:57 +0300413 }
414
Kalle Valobdcd8172011-07-18 00:22:30 +0300415 return status;
416}
417
418int ath6kl_configure_target(struct ath6kl *ar)
419{
420 u32 param, ram_reserved_size;
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530421 u8 fw_iftype, fw_mode = 0, fw_submode = 0;
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530422 int i;
Kalle Valobdcd8172011-07-18 00:22:30 +0300423
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530424 /*
425 * Note: Even though the firmware interface type is
426 * chosen as BSS_STA for all three interfaces, can
427 * be configured to IBSS/AP as long as the fw submode
428 * remains normal mode (0 - AP, STA and IBSS). But
429 * due to an target assert in firmware only one interface is
430 * configured for now.
431 */
Vasanthakumar Thiagarajandd3751f2011-10-25 19:33:59 +0530432 fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
Kalle Valobdcd8172011-07-18 00:22:30 +0300433
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530434 for (i = 0; i < MAX_NUM_VIF; i++)
435 fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
436
437 /*
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530438 * By default, submodes :
439 * vif[0] - AP/STA/IBSS
440 * vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
441 * vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530442 */
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530443
444 for (i = 0; i < ar->max_norm_iface; i++)
445 fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
446 (i * HI_OPTION_FW_SUBMODE_BITS);
447
448 for (i = ar->max_norm_iface; i < MAX_NUM_VIF; i++)
449 fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
450 (i * HI_OPTION_FW_SUBMODE_BITS);
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530451
452 /*
453 * FIXME: This needs to be removed once the multivif
454 * support is enabled.
455 */
456 if (ar->p2p)
457 fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530458
Kalle Valobdcd8172011-07-18 00:22:30 +0300459 param = HTC_PROTOCOL_VERSION;
460 if (ath6kl_bmi_write(ar,
461 ath6kl_get_hi_item_addr(ar,
462 HI_ITEM(hi_app_host_interest)),
463 (u8 *)&param, 4) != 0) {
464 ath6kl_err("bmi_write_memory for htc version failed\n");
465 return -EIO;
466 }
467
468 /* set the firmware mode to STA/IBSS/AP */
469 param = 0;
470
471 if (ath6kl_bmi_read(ar,
472 ath6kl_get_hi_item_addr(ar,
473 HI_ITEM(hi_option_flag)),
474 (u8 *)&param, 4) != 0) {
475 ath6kl_err("bmi_read_memory for setting fwmode failed\n");
476 return -EIO;
477 }
478
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530479 param |= (MAX_NUM_VIF << HI_OPTION_NUM_DEV_SHIFT);
480 param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
481 param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
482
Kalle Valobdcd8172011-07-18 00:22:30 +0300483 param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
484 param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
485
486 if (ath6kl_bmi_write(ar,
487 ath6kl_get_hi_item_addr(ar,
488 HI_ITEM(hi_option_flag)),
489 (u8 *)&param,
490 4) != 0) {
491 ath6kl_err("bmi_write_memory for setting fwmode failed\n");
492 return -EIO;
493 }
494
495 ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
496
497 /*
498 * Hardcode the address use for the extended board data
499 * Ideally this should be pre-allocate by the OS at boot time
500 * But since it is a new feature and board data is loaded
501 * at init time, we have to workaround this from host.
502 * It is difficult to patch the firmware boot code,
503 * but possible in theory.
504 */
505
Kalle Valo991b27e2011-09-07 10:55:17 +0300506 param = ar->hw.board_ext_data_addr;
507 ram_reserved_size = ar->hw.reserved_ram_size;
Kalle Valobdcd8172011-07-18 00:22:30 +0300508
Kalle Valo991b27e2011-09-07 10:55:17 +0300509 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
510 HI_ITEM(hi_board_ext_data)),
511 (u8 *)&param, 4) != 0) {
512 ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
513 return -EIO;
514 }
515
516 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
517 HI_ITEM(hi_end_ram_reserve_sz)),
518 (u8 *)&ram_reserved_size, 4) != 0) {
519 ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
520 return -EIO;
Kalle Valobdcd8172011-07-18 00:22:30 +0300521 }
522
523 /* set the block size for the target */
524 if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
525 /* use default number of control buffers */
526 return -EIO;
527
528 return 0;
529}
530
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +0530531void ath6kl_core_free(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +0300532{
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +0530533 wiphy_free(ar->wiphy);
Kalle Valobdcd8172011-07-18 00:22:30 +0300534}
535
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530536void ath6kl_core_cleanup(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +0300537{
Kalle Valob2e75692011-10-27 18:48:14 +0300538 ath6kl_hif_power_off(ar);
539
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530540 destroy_workqueue(ar->ath6kl_wq);
Kalle Valobdcd8172011-07-18 00:22:30 +0300541
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530542 if (ar->htc_target)
543 ath6kl_htc_cleanup(ar->htc_target);
544
545 ath6kl_cookie_cleanup(ar);
546
547 ath6kl_cleanup_amsdu_rxbufs(ar);
548
549 ath6kl_bmi_cleanup(ar);
550
551 ath6kl_debug_cleanup(ar);
552
553 kfree(ar->fw_board);
554 kfree(ar->fw_otp);
555 kfree(ar->fw);
556 kfree(ar->fw_patch);
557
558 ath6kl_deinit_ieee80211_hw(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +0300559}
560
561/* firmware upload */
Kalle Valobdcd8172011-07-18 00:22:30 +0300562static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
563 u8 **fw, size_t *fw_len)
564{
565 const struct firmware *fw_entry;
566 int ret;
567
568 ret = request_firmware(&fw_entry, filename, ar->dev);
569 if (ret)
570 return ret;
571
572 *fw_len = fw_entry->size;
573 *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
574
575 if (*fw == NULL)
576 ret = -ENOMEM;
577
578 release_firmware(fw_entry);
579
580 return ret;
581}
582
Sam Leffler92ecbff2011-09-07 10:55:16 +0300583#ifdef CONFIG_OF
584static const char *get_target_ver_dir(const struct ath6kl *ar)
585{
586 switch (ar->version.target_ver) {
587 case AR6003_REV1_VERSION:
588 return "ath6k/AR6003/hw1.0";
589 case AR6003_REV2_VERSION:
590 return "ath6k/AR6003/hw2.0";
591 case AR6003_REV3_VERSION:
592 return "ath6k/AR6003/hw2.1.1";
593 }
594 ath6kl_warn("%s: unsupported target version 0x%x.\n", __func__,
595 ar->version.target_ver);
596 return NULL;
597}
598
599/*
600 * Check the device tree for a board-id and use it to construct
601 * the pathname to the firmware file. Used (for now) to find a
602 * fallback to the "bdata.bin" file--typically a symlink to the
603 * appropriate board-specific file.
604 */
605static bool check_device_tree(struct ath6kl *ar)
606{
607 static const char *board_id_prop = "atheros,board-id";
608 struct device_node *node;
609 char board_filename[64];
610 const char *board_id;
611 int ret;
612
613 for_each_compatible_node(node, NULL, "atheros,ath6kl") {
614 board_id = of_get_property(node, board_id_prop, NULL);
615 if (board_id == NULL) {
616 ath6kl_warn("No \"%s\" property on %s node.\n",
617 board_id_prop, node->name);
618 continue;
619 }
620 snprintf(board_filename, sizeof(board_filename),
621 "%s/bdata.%s.bin", get_target_ver_dir(ar), board_id);
622
623 ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
624 &ar->fw_board_len);
625 if (ret) {
626 ath6kl_err("Failed to get DT board file %s: %d\n",
627 board_filename, ret);
628 continue;
629 }
630 return true;
631 }
632 return false;
633}
634#else
635static bool check_device_tree(struct ath6kl *ar)
636{
637 return false;
638}
639#endif /* CONFIG_OF */
640
Kalle Valobdcd8172011-07-18 00:22:30 +0300641static int ath6kl_fetch_board_file(struct ath6kl *ar)
642{
643 const char *filename;
644 int ret;
645
Kalle Valo772c31e2011-09-07 10:55:16 +0300646 if (ar->fw_board != NULL)
647 return 0;
648
Kalle Valobdcd8172011-07-18 00:22:30 +0300649 switch (ar->version.target_ver) {
650 case AR6003_REV2_VERSION:
651 filename = AR6003_REV2_BOARD_DATA_FILE;
652 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800653 case AR6004_REV1_VERSION:
654 filename = AR6004_REV1_BOARD_DATA_FILE;
655 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300656 default:
657 filename = AR6003_REV3_BOARD_DATA_FILE;
658 break;
659 }
660
661 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
662 &ar->fw_board_len);
663 if (ret == 0) {
664 /* managed to get proper board file */
665 return 0;
666 }
667
Sam Leffler92ecbff2011-09-07 10:55:16 +0300668 if (check_device_tree(ar)) {
669 /* got board file from device tree */
670 return 0;
671 }
672
Kalle Valobdcd8172011-07-18 00:22:30 +0300673 /* there was no proper board file, try to use default instead */
674 ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
675 filename, ret);
676
677 switch (ar->version.target_ver) {
678 case AR6003_REV2_VERSION:
679 filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
680 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800681 case AR6004_REV1_VERSION:
682 filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
683 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300684 default:
685 filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
686 break;
687 }
688
689 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
690 &ar->fw_board_len);
691 if (ret) {
692 ath6kl_err("Failed to get default board file %s: %d\n",
693 filename, ret);
694 return ret;
695 }
696
697 ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
698 ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
699
700 return 0;
701}
702
Kalle Valo772c31e2011-09-07 10:55:16 +0300703static int ath6kl_fetch_otp_file(struct ath6kl *ar)
704{
705 const char *filename;
706 int ret;
707
708 if (ar->fw_otp != NULL)
709 return 0;
710
711 switch (ar->version.target_ver) {
712 case AR6003_REV2_VERSION:
713 filename = AR6003_REV2_OTP_FILE;
714 break;
715 case AR6004_REV1_VERSION:
716 ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
717 return 0;
718 break;
719 default:
720 filename = AR6003_REV3_OTP_FILE;
721 break;
722 }
723
724 ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
725 &ar->fw_otp_len);
726 if (ret) {
727 ath6kl_err("Failed to get OTP file %s: %d\n",
728 filename, ret);
729 return ret;
730 }
731
732 return 0;
733}
734
735static int ath6kl_fetch_fw_file(struct ath6kl *ar)
736{
737 const char *filename;
738 int ret;
739
740 if (ar->fw != NULL)
741 return 0;
742
743 if (testmode) {
744 switch (ar->version.target_ver) {
745 case AR6003_REV2_VERSION:
746 filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
747 break;
748 case AR6003_REV3_VERSION:
749 filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
750 break;
751 case AR6004_REV1_VERSION:
752 ath6kl_warn("testmode not supported with ar6004\n");
753 return -EOPNOTSUPP;
754 default:
755 ath6kl_warn("unknown target version: 0x%x\n",
756 ar->version.target_ver);
757 return -EINVAL;
758 }
759
760 set_bit(TESTMODE, &ar->flag);
761
762 goto get_fw;
763 }
764
765 switch (ar->version.target_ver) {
766 case AR6003_REV2_VERSION:
767 filename = AR6003_REV2_FIRMWARE_FILE;
768 break;
769 case AR6004_REV1_VERSION:
770 filename = AR6004_REV1_FIRMWARE_FILE;
771 break;
772 default:
773 filename = AR6003_REV3_FIRMWARE_FILE;
774 break;
775 }
776
777get_fw:
778 ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
779 if (ret) {
780 ath6kl_err("Failed to get firmware file %s: %d\n",
781 filename, ret);
782 return ret;
783 }
784
785 return 0;
786}
787
788static int ath6kl_fetch_patch_file(struct ath6kl *ar)
789{
790 const char *filename;
791 int ret;
792
793 switch (ar->version.target_ver) {
794 case AR6003_REV2_VERSION:
795 filename = AR6003_REV2_PATCH_FILE;
796 break;
797 case AR6004_REV1_VERSION:
798 /* FIXME: implement for AR6004 */
799 return 0;
800 break;
801 default:
802 filename = AR6003_REV3_PATCH_FILE;
803 break;
804 }
805
806 if (ar->fw_patch == NULL) {
807 ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
808 &ar->fw_patch_len);
809 if (ret) {
810 ath6kl_err("Failed to get patch file %s: %d\n",
811 filename, ret);
812 return ret;
813 }
814 }
815
816 return 0;
817}
818
Kalle Valo50d41232011-09-07 10:55:17 +0300819static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
Kalle Valo772c31e2011-09-07 10:55:16 +0300820{
821 int ret;
822
Kalle Valo772c31e2011-09-07 10:55:16 +0300823 ret = ath6kl_fetch_otp_file(ar);
824 if (ret)
825 return ret;
826
827 ret = ath6kl_fetch_fw_file(ar);
828 if (ret)
829 return ret;
830
831 ret = ath6kl_fetch_patch_file(ar);
832 if (ret)
833 return ret;
834
835 return 0;
836}
Kalle Valobdcd8172011-07-18 00:22:30 +0300837
Kalle Valo50d41232011-09-07 10:55:17 +0300838static int ath6kl_fetch_fw_api2(struct ath6kl *ar)
839{
840 size_t magic_len, len, ie_len;
841 const struct firmware *fw;
842 struct ath6kl_fw_ie *hdr;
843 const char *filename;
844 const u8 *data;
Kalle Valo97e04962011-09-12 13:47:34 +0300845 int ret, ie_id, i, index, bit;
Kalle Valo8a137482011-09-07 10:55:17 +0300846 __le32 *val;
Kalle Valo50d41232011-09-07 10:55:17 +0300847
848 switch (ar->version.target_ver) {
849 case AR6003_REV2_VERSION:
850 filename = AR6003_REV2_FIRMWARE_2_FILE;
851 break;
852 case AR6003_REV3_VERSION:
853 filename = AR6003_REV3_FIRMWARE_2_FILE;
854 break;
855 case AR6004_REV1_VERSION:
856 filename = AR6004_REV1_FIRMWARE_2_FILE;
857 break;
Kalle Valo50e27402011-11-11 12:18:06 +0200858 case AR6004_REV2_VERSION:
859 filename = AR6004_REV2_FIRMWARE_2_FILE;
860 break;
Kalle Valo50d41232011-09-07 10:55:17 +0300861 default:
862 return -EOPNOTSUPP;
863 }
864
865 ret = request_firmware(&fw, filename, ar->dev);
866 if (ret)
867 return ret;
868
869 data = fw->data;
870 len = fw->size;
871
872 /* magic also includes the null byte, check that as well */
873 magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
874
875 if (len < magic_len) {
876 ret = -EINVAL;
877 goto out;
878 }
879
880 if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
881 ret = -EINVAL;
882 goto out;
883 }
884
885 len -= magic_len;
886 data += magic_len;
887
888 /* loop elements */
889 while (len > sizeof(struct ath6kl_fw_ie)) {
890 /* hdr is unaligned! */
891 hdr = (struct ath6kl_fw_ie *) data;
892
893 ie_id = le32_to_cpup(&hdr->id);
894 ie_len = le32_to_cpup(&hdr->len);
895
896 len -= sizeof(*hdr);
897 data += sizeof(*hdr);
898
899 if (len < ie_len) {
900 ret = -EINVAL;
901 goto out;
902 }
903
904 switch (ie_id) {
905 case ATH6KL_FW_IE_OTP_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300906 ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300907 ie_len);
908
Kalle Valo50d41232011-09-07 10:55:17 +0300909 ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
910
911 if (ar->fw_otp == NULL) {
912 ret = -ENOMEM;
913 goto out;
914 }
915
916 ar->fw_otp_len = ie_len;
917 break;
918 case ATH6KL_FW_IE_FW_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300919 ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300920 ie_len);
921
Kalle Valo50d41232011-09-07 10:55:17 +0300922 ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
923
924 if (ar->fw == NULL) {
925 ret = -ENOMEM;
926 goto out;
927 }
928
929 ar->fw_len = ie_len;
930 break;
931 case ATH6KL_FW_IE_PATCH_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300932 ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300933 ie_len);
934
Kalle Valo50d41232011-09-07 10:55:17 +0300935 ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
936
937 if (ar->fw_patch == NULL) {
938 ret = -ENOMEM;
939 goto out;
940 }
941
942 ar->fw_patch_len = ie_len;
943 break;
Kalle Valo8a137482011-09-07 10:55:17 +0300944 case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
945 val = (__le32 *) data;
946 ar->hw.reserved_ram_size = le32_to_cpup(val);
Kalle Valo6bc36432011-09-27 14:31:11 +0300947
948 ath6kl_dbg(ATH6KL_DBG_BOOT,
949 "found reserved ram size ie 0x%d\n",
950 ar->hw.reserved_ram_size);
Kalle Valo8a137482011-09-07 10:55:17 +0300951 break;
Kalle Valo97e04962011-09-12 13:47:34 +0300952 case ATH6KL_FW_IE_CAPABILITIES:
Kalle Valo6bc36432011-09-27 14:31:11 +0300953 ath6kl_dbg(ATH6KL_DBG_BOOT,
Kalle Valoef548622011-10-01 09:43:09 +0300954 "found firmware capabilities ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300955 ie_len);
956
Kalle Valo97e04962011-09-12 13:47:34 +0300957 for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
958 index = ALIGN(i, 8) / 8;
959 bit = i % 8;
960
961 if (data[index] & (1 << bit))
962 __set_bit(i, ar->fw_capabilities);
963 }
Kalle Valo6bc36432011-09-27 14:31:11 +0300964
965 ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
966 ar->fw_capabilities,
967 sizeof(ar->fw_capabilities));
Kalle Valo97e04962011-09-12 13:47:34 +0300968 break;
Kalle Valo1b4304d2011-09-27 11:05:26 +0300969 case ATH6KL_FW_IE_PATCH_ADDR:
970 if (ie_len != sizeof(*val))
971 break;
972
973 val = (__le32 *) data;
974 ar->hw.dataset_patch_addr = le32_to_cpup(val);
Kalle Valo6bc36432011-09-27 14:31:11 +0300975
976 ath6kl_dbg(ATH6KL_DBG_BOOT,
977 "found patch address ie 0x%d\n",
978 ar->hw.dataset_patch_addr);
Kalle Valo1b4304d2011-09-27 11:05:26 +0300979 break;
Kalle Valo50d41232011-09-07 10:55:17 +0300980 default:
Kalle Valo6bc36432011-09-27 14:31:11 +0300981 ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
Kalle Valo50d41232011-09-07 10:55:17 +0300982 le32_to_cpup(&hdr->id));
983 break;
984 }
985
986 len -= ie_len;
987 data += ie_len;
988 };
989
990 ret = 0;
991out:
992 release_firmware(fw);
993
994 return ret;
995}
996
997static int ath6kl_fetch_firmwares(struct ath6kl *ar)
998{
999 int ret;
1000
1001 ret = ath6kl_fetch_board_file(ar);
1002 if (ret)
1003 return ret;
1004
1005 ret = ath6kl_fetch_fw_api2(ar);
Kalle Valo6bc36432011-09-27 14:31:11 +03001006 if (ret == 0) {
1007 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 2\n");
Kalle Valo50d41232011-09-07 10:55:17 +03001008 return 0;
Kalle Valo6bc36432011-09-27 14:31:11 +03001009 }
Kalle Valo50d41232011-09-07 10:55:17 +03001010
1011 ret = ath6kl_fetch_fw_api1(ar);
1012 if (ret)
1013 return ret;
1014
Kalle Valo6bc36432011-09-27 14:31:11 +03001015 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 1\n");
1016
Kalle Valo50d41232011-09-07 10:55:17 +03001017 return 0;
1018}
1019
Kalle Valobdcd8172011-07-18 00:22:30 +03001020static int ath6kl_upload_board_file(struct ath6kl *ar)
1021{
1022 u32 board_address, board_ext_address, param;
Kevin Fang31024d92011-07-11 17:14:13 +08001023 u32 board_data_size, board_ext_data_size;
Kalle Valobdcd8172011-07-18 00:22:30 +03001024 int ret;
1025
Kalle Valo772c31e2011-09-07 10:55:16 +03001026 if (WARN_ON(ar->fw_board == NULL))
1027 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001028
Kevin Fang31024d92011-07-11 17:14:13 +08001029 /*
1030 * Determine where in Target RAM to write Board Data.
1031 * For AR6004, host determine Target RAM address for
1032 * writing board data.
1033 */
1034 if (ar->target_type == TARGET_TYPE_AR6004) {
Kalle Valo50e27402011-11-11 12:18:06 +02001035 if (ar->version.target_ver == AR6004_REV1_VERSION)
1036 board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
1037 else
1038 board_address = AR6004_REV2_BOARD_DATA_ADDRESS;
1039
Kevin Fang31024d92011-07-11 17:14:13 +08001040 ath6kl_bmi_write(ar,
1041 ath6kl_get_hi_item_addr(ar,
1042 HI_ITEM(hi_board_data)),
1043 (u8 *) &board_address, 4);
1044 } else {
1045 ath6kl_bmi_read(ar,
1046 ath6kl_get_hi_item_addr(ar,
1047 HI_ITEM(hi_board_data)),
1048 (u8 *) &board_address, 4);
1049 }
1050
Kalle Valobdcd8172011-07-18 00:22:30 +03001051 /* determine where in target ram to write extended board data */
1052 ath6kl_bmi_read(ar,
1053 ath6kl_get_hi_item_addr(ar,
1054 HI_ITEM(hi_board_ext_data)),
1055 (u8 *) &board_ext_address, 4);
1056
Kalle Valo50e27402011-11-11 12:18:06 +02001057 if (ar->target_type == TARGET_TYPE_AR6003 &&
1058 board_ext_address == 0) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001059 ath6kl_err("Failed to get board file target address.\n");
1060 return -EINVAL;
1061 }
1062
Kevin Fang31024d92011-07-11 17:14:13 +08001063 switch (ar->target_type) {
1064 case TARGET_TYPE_AR6003:
1065 board_data_size = AR6003_BOARD_DATA_SZ;
1066 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1067 break;
1068 case TARGET_TYPE_AR6004:
1069 board_data_size = AR6004_BOARD_DATA_SZ;
1070 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
1071 break;
1072 default:
1073 WARN_ON(1);
1074 return -EINVAL;
1075 break;
1076 }
1077
Kalle Valo50e27402011-11-11 12:18:06 +02001078 if (board_ext_address &&
1079 ar->fw_board_len == (board_data_size + board_ext_data_size)) {
Kevin Fang31024d92011-07-11 17:14:13 +08001080
Kalle Valobdcd8172011-07-18 00:22:30 +03001081 /* write extended board data */
Kalle Valo6bc36432011-09-27 14:31:11 +03001082 ath6kl_dbg(ATH6KL_DBG_BOOT,
1083 "writing extended board data to 0x%x (%d B)\n",
1084 board_ext_address, board_ext_data_size);
1085
Kalle Valobdcd8172011-07-18 00:22:30 +03001086 ret = ath6kl_bmi_write(ar, board_ext_address,
Kevin Fang31024d92011-07-11 17:14:13 +08001087 ar->fw_board + board_data_size,
1088 board_ext_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001089 if (ret) {
1090 ath6kl_err("Failed to write extended board data: %d\n",
1091 ret);
1092 return ret;
1093 }
1094
1095 /* record that extended board data is initialized */
Kevin Fang31024d92011-07-11 17:14:13 +08001096 param = (board_ext_data_size << 16) | 1;
1097
Kalle Valobdcd8172011-07-18 00:22:30 +03001098 ath6kl_bmi_write(ar,
1099 ath6kl_get_hi_item_addr(ar,
1100 HI_ITEM(hi_board_ext_data_config)),
1101 (unsigned char *) &param, 4);
1102 }
1103
Kevin Fang31024d92011-07-11 17:14:13 +08001104 if (ar->fw_board_len < board_data_size) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001105 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1106 ret = -EINVAL;
1107 return ret;
1108 }
1109
Kalle Valo6bc36432011-09-27 14:31:11 +03001110 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
1111 board_address, board_data_size);
1112
Kalle Valobdcd8172011-07-18 00:22:30 +03001113 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
Kevin Fang31024d92011-07-11 17:14:13 +08001114 board_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001115
1116 if (ret) {
1117 ath6kl_err("Board file bmi write failed: %d\n", ret);
1118 return ret;
1119 }
1120
1121 /* record the fact that Board Data IS initialized */
1122 param = 1;
1123 ath6kl_bmi_write(ar,
1124 ath6kl_get_hi_item_addr(ar,
1125 HI_ITEM(hi_board_data_initialized)),
1126 (u8 *)&param, 4);
1127
1128 return ret;
1129}
1130
1131static int ath6kl_upload_otp(struct ath6kl *ar)
1132{
Kalle Valobdcd8172011-07-18 00:22:30 +03001133 u32 address, param;
Kalle Valobef26a72011-10-12 09:58:28 +03001134 bool from_hw = false;
Kalle Valobdcd8172011-07-18 00:22:30 +03001135 int ret;
1136
Kalle Valo50e27402011-11-11 12:18:06 +02001137 if (ar->fw_otp == NULL)
1138 return 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03001139
Kalle Valoa01ac412011-09-07 10:55:17 +03001140 address = ar->hw.app_load_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001141
Kalle Valoef548622011-10-01 09:43:09 +03001142 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
Kalle Valo6bc36432011-09-27 14:31:11 +03001143 ar->fw_otp_len);
1144
Kalle Valobdcd8172011-07-18 00:22:30 +03001145 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1146 ar->fw_otp_len);
1147 if (ret) {
1148 ath6kl_err("Failed to upload OTP file: %d\n", ret);
1149 return ret;
1150 }
1151
Kalle Valo639d0b82011-09-12 12:48:09 +03001152 /* read firmware start address */
1153 ret = ath6kl_bmi_read(ar,
1154 ath6kl_get_hi_item_addr(ar,
1155 HI_ITEM(hi_app_start)),
1156 (u8 *) &address, sizeof(address));
1157
1158 if (ret) {
1159 ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1160 return ret;
1161 }
1162
Kalle Valobef26a72011-10-12 09:58:28 +03001163 if (ar->hw.app_start_override_addr == 0) {
1164 ar->hw.app_start_override_addr = address;
1165 from_hw = true;
1166 }
Kalle Valo639d0b82011-09-12 12:48:09 +03001167
Kalle Valobef26a72011-10-12 09:58:28 +03001168 ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1169 from_hw ? " (from hw)" : "",
Kalle Valo6bc36432011-09-27 14:31:11 +03001170 ar->hw.app_start_override_addr);
1171
Kalle Valobdcd8172011-07-18 00:22:30 +03001172 /* execute the OTP code */
Kalle Valobef26a72011-10-12 09:58:28 +03001173 ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1174 ar->hw.app_start_override_addr);
Kalle Valobdcd8172011-07-18 00:22:30 +03001175 param = 0;
Kalle Valobef26a72011-10-12 09:58:28 +03001176 ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
Kalle Valobdcd8172011-07-18 00:22:30 +03001177
1178 return ret;
1179}
1180
1181static int ath6kl_upload_firmware(struct ath6kl *ar)
1182{
Kalle Valobdcd8172011-07-18 00:22:30 +03001183 u32 address;
1184 int ret;
1185
Kalle Valo772c31e2011-09-07 10:55:16 +03001186 if (WARN_ON(ar->fw == NULL))
Kalle Valo50e27402011-11-11 12:18:06 +02001187 return 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03001188
Kalle Valoa01ac412011-09-07 10:55:17 +03001189 address = ar->hw.app_load_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001190
Kalle Valoef548622011-10-01 09:43:09 +03001191 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +03001192 address, ar->fw_len);
1193
Kalle Valobdcd8172011-07-18 00:22:30 +03001194 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1195
1196 if (ret) {
1197 ath6kl_err("Failed to write firmware: %d\n", ret);
1198 return ret;
1199 }
1200
Kevin Fang31024d92011-07-11 17:14:13 +08001201 /*
1202 * Set starting address for firmware
1203 * Don't need to setup app_start override addr on AR6004
1204 */
1205 if (ar->target_type != TARGET_TYPE_AR6004) {
Kalle Valoa01ac412011-09-07 10:55:17 +03001206 address = ar->hw.app_start_override_addr;
Kevin Fang31024d92011-07-11 17:14:13 +08001207 ath6kl_bmi_set_app_start(ar, address);
1208 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001209 return ret;
1210}
1211
1212static int ath6kl_upload_patch(struct ath6kl *ar)
1213{
Kalle Valobdcd8172011-07-18 00:22:30 +03001214 u32 address, param;
1215 int ret;
1216
Kalle Valo50e27402011-11-11 12:18:06 +02001217 if (ar->fw_patch == NULL)
1218 return 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03001219
Kalle Valoa01ac412011-09-07 10:55:17 +03001220 address = ar->hw.dataset_patch_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001221
Kalle Valoef548622011-10-01 09:43:09 +03001222 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +03001223 address, ar->fw_patch_len);
1224
Kalle Valobdcd8172011-07-18 00:22:30 +03001225 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1226 if (ret) {
1227 ath6kl_err("Failed to write patch file: %d\n", ret);
1228 return ret;
1229 }
1230
1231 param = address;
1232 ath6kl_bmi_write(ar,
1233 ath6kl_get_hi_item_addr(ar,
1234 HI_ITEM(hi_dset_list_head)),
1235 (unsigned char *) &param, 4);
1236
1237 return 0;
1238}
1239
1240static int ath6kl_init_upload(struct ath6kl *ar)
1241{
1242 u32 param, options, sleep, address;
1243 int status = 0;
1244
Kevin Fang31024d92011-07-11 17:14:13 +08001245 if (ar->target_type != TARGET_TYPE_AR6003 &&
1246 ar->target_type != TARGET_TYPE_AR6004)
Kalle Valobdcd8172011-07-18 00:22:30 +03001247 return -EINVAL;
1248
1249 /* temporarily disable system sleep */
1250 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1251 status = ath6kl_bmi_reg_read(ar, address, &param);
1252 if (status)
1253 return status;
1254
1255 options = param;
1256
1257 param |= ATH6KL_OPTION_SLEEP_DISABLE;
1258 status = ath6kl_bmi_reg_write(ar, address, param);
1259 if (status)
1260 return status;
1261
1262 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1263 status = ath6kl_bmi_reg_read(ar, address, &param);
1264 if (status)
1265 return status;
1266
1267 sleep = param;
1268
1269 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1270 status = ath6kl_bmi_reg_write(ar, address, param);
1271 if (status)
1272 return status;
1273
1274 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1275 options, sleep);
1276
1277 /* program analog PLL register */
Kevin Fang31024d92011-07-11 17:14:13 +08001278 /* no need to control 40/44MHz clock on AR6004 */
1279 if (ar->target_type != TARGET_TYPE_AR6004) {
1280 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1281 0xF9104001);
Kalle Valobdcd8172011-07-18 00:22:30 +03001282
Kevin Fang31024d92011-07-11 17:14:13 +08001283 if (status)
1284 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +03001285
Kevin Fang31024d92011-07-11 17:14:13 +08001286 /* Run at 80/88MHz by default */
1287 param = SM(CPU_CLOCK_STANDARD, 1);
1288
1289 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1290 status = ath6kl_bmi_reg_write(ar, address, param);
1291 if (status)
1292 return status;
1293 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001294
1295 param = 0;
1296 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1297 param = SM(LPO_CAL_ENABLE, 1);
1298 status = ath6kl_bmi_reg_write(ar, address, param);
1299 if (status)
1300 return status;
1301
1302 /* WAR to avoid SDIO CRC err */
1303 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1304 ath6kl_err("temporary war to avoid sdio crc error\n");
1305
1306 param = 0x20;
1307
1308 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1309 status = ath6kl_bmi_reg_write(ar, address, param);
1310 if (status)
1311 return status;
1312
1313 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1314 status = ath6kl_bmi_reg_write(ar, address, param);
1315 if (status)
1316 return status;
1317
1318 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1319 status = ath6kl_bmi_reg_write(ar, address, param);
1320 if (status)
1321 return status;
1322
1323 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1324 status = ath6kl_bmi_reg_write(ar, address, param);
1325 if (status)
1326 return status;
1327 }
1328
1329 /* write EEPROM data to Target RAM */
1330 status = ath6kl_upload_board_file(ar);
1331 if (status)
1332 return status;
1333
1334 /* transfer One time Programmable data */
1335 status = ath6kl_upload_otp(ar);
1336 if (status)
1337 return status;
1338
1339 /* Download Target firmware */
1340 status = ath6kl_upload_firmware(ar);
1341 if (status)
1342 return status;
1343
1344 status = ath6kl_upload_patch(ar);
1345 if (status)
1346 return status;
1347
1348 /* Restore system sleep */
1349 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1350 status = ath6kl_bmi_reg_write(ar, address, sleep);
1351 if (status)
1352 return status;
1353
1354 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1355 param = options | 0x20;
1356 status = ath6kl_bmi_reg_write(ar, address, param);
1357 if (status)
1358 return status;
1359
1360 /* Configure GPIO AR6003 UART */
1361 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1362 status = ath6kl_bmi_write(ar,
1363 ath6kl_get_hi_item_addr(ar,
1364 HI_ITEM(hi_dbg_uart_txpin)),
1365 (u8 *)&param, 4);
1366
1367 return status;
1368}
1369
Kalle Valoa01ac412011-09-07 10:55:17 +03001370static int ath6kl_init_hw_params(struct ath6kl *ar)
1371{
Kalle Valo856f4b32011-11-14 19:30:29 +02001372 const struct ath6kl_hw *hw;
1373 int i;
Kalle Valobef26a72011-10-12 09:58:28 +03001374
Kalle Valo856f4b32011-11-14 19:30:29 +02001375 for (i = 0; i < ARRAY_SIZE(hw_list); i++) {
1376 hw = &hw_list[i];
Kalle Valobef26a72011-10-12 09:58:28 +03001377
Kalle Valo856f4b32011-11-14 19:30:29 +02001378 if (hw->id == ar->version.target_ver)
1379 break;
1380 }
1381
1382 if (i == ARRAY_SIZE(hw_list)) {
Kalle Valoa01ac412011-09-07 10:55:17 +03001383 ath6kl_err("Unsupported hardware version: 0x%x\n",
1384 ar->version.target_ver);
1385 return -EINVAL;
1386 }
1387
Kalle Valo856f4b32011-11-14 19:30:29 +02001388 ar->hw = *hw;
1389
Kalle Valo6bc36432011-09-27 14:31:11 +03001390 ath6kl_dbg(ATH6KL_DBG_BOOT,
1391 "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
1392 ar->version.target_ver, ar->target_type,
1393 ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
1394 ath6kl_dbg(ATH6KL_DBG_BOOT,
1395 "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
1396 ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
1397 ar->hw.reserved_ram_size);
1398
Kalle Valoa01ac412011-09-07 10:55:17 +03001399 return 0;
1400}
1401
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001402int ath6kl_init_hw_start(struct ath6kl *ar)
Kalle Valo20459ee2011-10-27 18:48:37 +03001403{
1404 long timeleft;
1405 int ret, i;
1406
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001407 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
1408
Kalle Valo20459ee2011-10-27 18:48:37 +03001409 ret = ath6kl_hif_power_on(ar);
1410 if (ret)
1411 return ret;
1412
1413 ret = ath6kl_configure_target(ar);
1414 if (ret)
1415 goto err_power_off;
1416
1417 ret = ath6kl_init_upload(ar);
1418 if (ret)
1419 goto err_power_off;
1420
1421 /* Do we need to finish the BMI phase */
1422 /* FIXME: return error from ath6kl_bmi_done() */
1423 if (ath6kl_bmi_done(ar)) {
1424 ret = -EIO;
1425 goto err_power_off;
1426 }
1427
1428 /*
1429 * The reason we have to wait for the target here is that the
1430 * driver layer has to init BMI in order to set the host block
1431 * size.
1432 */
1433 if (ath6kl_htc_wait_target(ar->htc_target)) {
1434 ret = -EIO;
1435 goto err_power_off;
1436 }
1437
1438 if (ath6kl_init_service_ep(ar)) {
1439 ret = -EIO;
1440 goto err_cleanup_scatter;
1441 }
1442
1443 /* setup credit distribution */
1444 ath6kl_credit_setup(ar->htc_target, &ar->credit_state_info);
1445
1446 /* start HTC */
1447 ret = ath6kl_htc_start(ar->htc_target);
1448 if (ret) {
1449 /* FIXME: call this */
1450 ath6kl_cookie_cleanup(ar);
1451 goto err_cleanup_scatter;
1452 }
1453
1454 /* Wait for Wmi event to be ready */
1455 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1456 test_bit(WMI_READY,
1457 &ar->flag),
1458 WMI_TIMEOUT);
1459
1460 ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
1461
1462 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1463 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1464 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1465 ret = -EIO;
1466 goto err_htc_stop;
1467 }
1468
1469 if (!timeleft || signal_pending(current)) {
1470 ath6kl_err("wmi is not ready or wait was interrupted\n");
1471 ret = -EIO;
1472 goto err_htc_stop;
1473 }
1474
1475 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1476
1477 /* communicate the wmi protocol verision to the target */
1478 /* FIXME: return error */
1479 if ((ath6kl_set_host_app_area(ar)) != 0)
1480 ath6kl_err("unable to set the host app area\n");
1481
1482 for (i = 0; i < MAX_NUM_VIF; i++) {
1483 ret = ath6kl_target_config_wlan_params(ar, i);
1484 if (ret)
1485 goto err_htc_stop;
1486 }
1487
Kalle Valo76a9fbe2011-11-01 08:44:28 +02001488 ar->state = ATH6KL_STATE_ON;
1489
Kalle Valo20459ee2011-10-27 18:48:37 +03001490 return 0;
1491
1492err_htc_stop:
1493 ath6kl_htc_stop(ar->htc_target);
1494err_cleanup_scatter:
1495 ath6kl_hif_cleanup_scatter(ar);
1496err_power_off:
1497 ath6kl_hif_power_off(ar);
1498
1499 return ret;
1500}
1501
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001502int ath6kl_init_hw_stop(struct ath6kl *ar)
1503{
1504 int ret;
1505
1506 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
1507
1508 ath6kl_htc_stop(ar->htc_target);
1509
1510 ath6kl_hif_stop(ar);
1511
1512 ath6kl_bmi_reset(ar);
1513
1514 ret = ath6kl_hif_power_off(ar);
1515 if (ret)
1516 ath6kl_warn("failed to power off hif: %d\n", ret);
1517
Kalle Valo76a9fbe2011-11-01 08:44:28 +02001518 ar->state = ATH6KL_STATE_OFF;
1519
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001520 return 0;
1521}
1522
Kalle Valobdcd8172011-07-18 00:22:30 +03001523int ath6kl_core_init(struct ath6kl *ar)
1524{
Kalle Valobdcd8172011-07-18 00:22:30 +03001525 struct ath6kl_bmi_target_info targ_info;
Kalle Valo61448a92011-10-27 18:48:29 +03001526 struct net_device *ndev;
Kalle Valo20459ee2011-10-27 18:48:37 +03001527 int ret = 0, i;
Kalle Valobdcd8172011-07-18 00:22:30 +03001528
1529 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1530 if (!ar->ath6kl_wq)
1531 return -ENOMEM;
1532
1533 ret = ath6kl_bmi_init(ar);
1534 if (ret)
1535 goto err_wq;
1536
Kalle Valo20459ee2011-10-27 18:48:37 +03001537 /*
1538 * Turn on power to get hardware (target) version and leave power
1539 * on delibrately as we will boot the hardware anyway within few
1540 * seconds.
1541 */
Kalle Valob2e75692011-10-27 18:48:14 +03001542 ret = ath6kl_hif_power_on(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001543 if (ret)
1544 goto err_bmi_cleanup;
1545
Kalle Valob2e75692011-10-27 18:48:14 +03001546 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1547 if (ret)
1548 goto err_power_off;
1549
Kalle Valobdcd8172011-07-18 00:22:30 +03001550 ar->version.target_ver = le32_to_cpu(targ_info.version);
1551 ar->target_type = le32_to_cpu(targ_info.type);
Vasanthakumar Thiagarajanbe98e3a2011-10-25 19:33:57 +05301552 ar->wiphy->hw_version = le32_to_cpu(targ_info.version);
Kalle Valobdcd8172011-07-18 00:22:30 +03001553
Kalle Valoa01ac412011-09-07 10:55:17 +03001554 ret = ath6kl_init_hw_params(ar);
1555 if (ret)
Kalle Valob2e75692011-10-27 18:48:14 +03001556 goto err_power_off;
Kalle Valoa01ac412011-09-07 10:55:17 +03001557
Kalle Vaload226ec2011-08-10 09:49:12 +03001558 ar->htc_target = ath6kl_htc_create(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001559
1560 if (!ar->htc_target) {
1561 ret = -ENOMEM;
Kalle Valob2e75692011-10-27 18:48:14 +03001562 goto err_power_off;
Kalle Valobdcd8172011-07-18 00:22:30 +03001563 }
1564
Kalle Valo772c31e2011-09-07 10:55:16 +03001565 ret = ath6kl_fetch_firmwares(ar);
1566 if (ret)
1567 goto err_htc_cleanup;
1568
Kalle Valo61448a92011-10-27 18:48:29 +03001569 /* FIXME: we should free all firmwares in the error cases below */
1570
Kalle Valo61448a92011-10-27 18:48:29 +03001571 /* Indicate that WMI is enabled (although not ready yet) */
1572 set_bit(WMI_ENABLED, &ar->flag);
1573 ar->wmi = ath6kl_wmi_init(ar);
1574 if (!ar->wmi) {
1575 ath6kl_err("failed to initialize wmi\n");
1576 ret = -EIO;
1577 goto err_htc_cleanup;
1578 }
1579
1580 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1581
1582 ret = ath6kl_register_ieee80211_hw(ar);
1583 if (ret)
1584 goto err_node_cleanup;
1585
1586 ret = ath6kl_debug_init(ar);
1587 if (ret) {
1588 wiphy_unregister(ar->wiphy);
1589 goto err_node_cleanup;
1590 }
1591
1592 for (i = 0; i < MAX_NUM_VIF; i++)
1593 ar->avail_idx_map |= BIT(i);
1594
1595 rtnl_lock();
1596
1597 /* Add an initial station interface */
1598 ndev = ath6kl_interface_add(ar, "wlan%d", NL80211_IFTYPE_STATION, 0,
1599 INFRA_NETWORK);
1600
1601 rtnl_unlock();
1602
1603 if (!ndev) {
1604 ath6kl_err("Failed to instantiate a network device\n");
1605 ret = -ENOMEM;
1606 wiphy_unregister(ar->wiphy);
1607 goto err_debug_init;
1608 }
1609
1610
1611 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1612 __func__, ndev->name, ndev, ar);
1613
Kalle Valo61448a92011-10-27 18:48:29 +03001614 /* setup access class priority mappings */
1615 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1616 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1617 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1618 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1619
1620 /* give our connected endpoints some buffers */
1621 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1622 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1623
1624 /* allocate some buffers that handle larger AMSDU frames */
1625 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1626
Kalle Valo61448a92011-10-27 18:48:29 +03001627 ath6kl_cookie_init(ar);
1628
Kalle Valo61448a92011-10-27 18:48:29 +03001629 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1630 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1631
Kalle Valo8277de12011-11-03 12:18:31 +02001632 if (suspend_cutpower)
1633 ar->conf_flags |= ATH6KL_CONF_SUSPEND_CUTPOWER;
1634
Kalle Valo61448a92011-10-27 18:48:29 +03001635 ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM |
1636 WIPHY_FLAG_HAVE_AP_SME;
1637
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001638 set_bit(FIRST_BOOT, &ar->flag);
1639
1640 ret = ath6kl_init_hw_start(ar);
Kalle Valo20459ee2011-10-27 18:48:37 +03001641 if (ret) {
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001642 ath6kl_err("Failed to start hardware: %d\n", ret);
Kalle Valo20459ee2011-10-27 18:48:37 +03001643 goto err_rxbuf_cleanup;
Kalle Valo61448a92011-10-27 18:48:29 +03001644 }
1645
1646 /*
1647 * Set mac address which is received in ready event
1648 * FIXME: Move to ath6kl_interface_add()
1649 */
1650 memcpy(ndev->dev_addr, ar->mac_addr, ETH_ALEN);
Kalle Valobdcd8172011-07-18 00:22:30 +03001651
Kalle Valobdcd8172011-07-18 00:22:30 +03001652 return ret;
1653
Kalle Valo61448a92011-10-27 18:48:29 +03001654err_rxbuf_cleanup:
1655 ath6kl_htc_flush_rx_buf(ar->htc_target);
1656 ath6kl_cleanup_amsdu_rxbufs(ar);
Kalle Valo61448a92011-10-27 18:48:29 +03001657 rtnl_lock();
1658 ath6kl_deinit_if_data(netdev_priv(ndev));
1659 rtnl_unlock();
1660 wiphy_unregister(ar->wiphy);
1661err_debug_init:
1662 ath6kl_debug_cleanup(ar);
1663err_node_cleanup:
1664 ath6kl_wmi_shutdown(ar->wmi);
1665 clear_bit(WMI_ENABLED, &ar->flag);
1666 ar->wmi = NULL;
Kalle Valobdcd8172011-07-18 00:22:30 +03001667err_htc_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001668 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valob2e75692011-10-27 18:48:14 +03001669err_power_off:
1670 ath6kl_hif_power_off(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001671err_bmi_cleanup:
1672 ath6kl_bmi_cleanup(ar);
1673err_wq:
1674 destroy_workqueue(ar->ath6kl_wq);
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +05301675
Kalle Valobdcd8172011-07-18 00:22:30 +03001676 return ret;
1677}
1678
Vasanthakumar Thiagarajan55055972011-10-25 19:34:23 +05301679void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301680{
1681 static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1682 bool discon_issued;
1683
1684 netif_stop_queue(vif->ndev);
1685
1686 clear_bit(WLAN_ENABLED, &vif->flags);
1687
1688 if (wmi_ready) {
1689 discon_issued = test_bit(CONNECTED, &vif->flags) ||
1690 test_bit(CONNECT_PEND, &vif->flags);
1691 ath6kl_disconnect(vif);
1692 del_timer(&vif->disconnect_timer);
1693
1694 if (discon_issued)
1695 ath6kl_disconnect_event(vif, DISCONNECT_CMD,
1696 (vif->nw_type & AP_NETWORK) ?
1697 bcast_mac : vif->bssid,
1698 0, NULL, 0);
1699 }
1700
1701 if (vif->scan_req) {
1702 cfg80211_scan_done(vif->scan_req, true);
1703 vif->scan_req = NULL;
1704 }
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301705}
1706
Kalle Valobdcd8172011-07-18 00:22:30 +03001707void ath6kl_stop_txrx(struct ath6kl *ar)
1708{
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301709 struct ath6kl_vif *vif, *tmp_vif;
Kalle Valobdcd8172011-07-18 00:22:30 +03001710
1711 set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1712
1713 if (down_interruptible(&ar->sem)) {
1714 ath6kl_err("down_interruptible failed\n");
1715 return;
1716 }
1717
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301718 spin_lock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301719 list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1720 list_del(&vif->list);
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301721 spin_unlock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301722 ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
Vasanthakumar Thiagarajan27929722011-10-25 19:34:21 +05301723 rtnl_lock();
1724 ath6kl_deinit_if_data(vif);
1725 rtnl_unlock();
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301726 spin_lock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301727 }
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301728 spin_unlock_bh(&ar->list_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +03001729
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301730 clear_bit(WMI_READY, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001731
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301732 /*
1733 * After wmi_shudown all WMI events will be dropped. We
1734 * need to cleanup the buffers allocated in AP mode and
1735 * give disconnect notification to stack, which usually
1736 * happens in the disconnect_event. Simulate the disconnect
1737 * event by calling the function directly. Sometimes
1738 * disconnect_event will be received when the debug logs
1739 * are collected.
1740 */
1741 ath6kl_wmi_shutdown(ar->wmi);
Kalle Valobdcd8172011-07-18 00:22:30 +03001742
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301743 clear_bit(WMI_ENABLED, &ar->flag);
1744 if (ar->htc_target) {
1745 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
1746 ath6kl_htc_stop(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001747 }
1748
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301749 /*
1750 * Try to reset the device if we can. The driver may have been
1751 * configure NOT to reset the target during a debug session.
1752 */
1753 ath6kl_dbg(ATH6KL_DBG_TRC,
1754 "attempting to reset target on instance destroy\n");
1755 ath6kl_reset_device(ar, ar->target_type, true, true);
Kalle Valobdcd8172011-07-18 00:22:30 +03001756
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301757 clear_bit(WLAN_ENABLED, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001758}