blob: bed468d3208bfc10bc56f2bf4f3415f22352ab55 [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,
Kalle Valo0d4d72b2011-11-14 19:30:39 +020060 .board_addr = 0x433900,
Kalle Valo856f4b32011-11-14 19:30:29 +020061 },
62 {
63 .id = AR6004_REV2_VERSION,
64 .dataset_patch_addr = 0x57e884,
65 .app_load_addr = 0x1234,
66 .board_ext_data_addr = 0x437000,
67 .reserved_ram_size = 11264,
Kalle Valo0d4d72b2011-11-14 19:30:39 +020068 .board_addr = 0x43d400,
Kalle Valo856f4b32011-11-14 19:30:29 +020069 },
70};
71
Kalle Valobdcd8172011-07-18 00:22:30 +030072/*
73 * Include definitions here that can be used to tune the WLAN module
74 * behavior. Different customers can tune the behavior as per their needs,
75 * here.
76 */
77
78/*
79 * This configuration item enable/disable keepalive support.
80 * Keepalive support: In the absence of any data traffic to AP, null
81 * frames will be sent to the AP at periodic interval, to keep the association
82 * active. This configuration item defines the periodic interval.
83 * Use value of zero to disable keepalive support
84 * Default: 60 seconds
85 */
86#define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
87
88/*
89 * This configuration item sets the value of disconnect timeout
90 * Firmware delays sending the disconnec event to the host for this
91 * timeout after is gets disconnected from the current AP.
92 * If the firmware successly roams within the disconnect timeout
93 * it sends a new connect event
94 */
95#define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
96
97#define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
98
Kalle Valobdcd8172011-07-18 00:22:30 +030099#define ATH6KL_DATA_OFFSET 64
100struct sk_buff *ath6kl_buf_alloc(int size)
101{
102 struct sk_buff *skb;
103 u16 reserved;
104
105 /* Add chacheline space at front and back of buffer */
106 reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
Vasanthakumar Thiagarajan1df94a82011-08-17 18:45:10 +0530107 sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
Kalle Valobdcd8172011-07-18 00:22:30 +0300108 skb = dev_alloc_skb(size + reserved);
109
110 if (skb)
111 skb_reserve(skb, reserved - L1_CACHE_BYTES);
112 return skb;
113}
114
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +0530115void ath6kl_init_profile_info(struct ath6kl_vif *vif)
Kalle Valobdcd8172011-07-18 00:22:30 +0300116{
Vasanthakumar Thiagarajan34503342011-10-25 19:34:02 +0530117 vif->ssid_len = 0;
118 memset(vif->ssid, 0, sizeof(vif->ssid));
119
120 vif->dot11_auth_mode = OPEN_AUTH;
121 vif->auth_mode = NONE_AUTH;
122 vif->prwise_crypto = NONE_CRYPT;
123 vif->prwise_crypto_len = 0;
124 vif->grp_crypto = NONE_CRYPT;
125 vif->grp_crypto_len = 0;
Vasanthakumar Thiagarajan6f2a73f2011-10-25 19:34:06 +0530126 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
Vasanthakumar Thiagarajan8c8b65e2011-10-25 19:34:04 +0530127 memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
128 memset(vif->bssid, 0, sizeof(vif->bssid));
Vasanthakumar Thiagarajanf74bac52011-10-25 19:34:05 +0530129 vif->bss_ch = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +0300130}
131
Kalle Valobdcd8172011-07-18 00:22:30 +0300132static int ath6kl_set_host_app_area(struct ath6kl *ar)
133{
134 u32 address, data;
135 struct host_app_area host_app_area;
136
137 /* Fetch the address of the host_app_area_s
138 * instance in the host interest area */
139 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
Kevin Fang31024d92011-07-11 17:14:13 +0800140 address = TARG_VTOP(ar->target_type, address);
Kalle Valobdcd8172011-07-18 00:22:30 +0300141
Kalle Valoaddb44b2011-09-02 10:32:05 +0300142 if (ath6kl_diag_read32(ar, address, &data))
Kalle Valobdcd8172011-07-18 00:22:30 +0300143 return -EIO;
144
Kevin Fang31024d92011-07-11 17:14:13 +0800145 address = TARG_VTOP(ar->target_type, data);
Kalle Valocbf49a62011-10-05 12:23:17 +0300146 host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
Kalle Valoaddb44b2011-09-02 10:32:05 +0300147 if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
148 sizeof(struct host_app_area)))
Kalle Valobdcd8172011-07-18 00:22:30 +0300149 return -EIO;
150
151 return 0;
152}
153
154static inline void set_ac2_ep_map(struct ath6kl *ar,
155 u8 ac,
156 enum htc_endpoint_id ep)
157{
158 ar->ac2ep_map[ac] = ep;
159 ar->ep2ac_map[ep] = ac;
160}
161
162/* connect to a service */
163static int ath6kl_connectservice(struct ath6kl *ar,
164 struct htc_service_connect_req *con_req,
165 char *desc)
166{
167 int status;
168 struct htc_service_connect_resp response;
169
170 memset(&response, 0, sizeof(response));
171
Kalle Vaload226ec2011-08-10 09:49:12 +0300172 status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
Kalle Valobdcd8172011-07-18 00:22:30 +0300173 if (status) {
174 ath6kl_err("failed to connect to %s service status:%d\n",
175 desc, status);
176 return status;
177 }
178
179 switch (con_req->svc_id) {
180 case WMI_CONTROL_SVC:
181 if (test_bit(WMI_ENABLED, &ar->flag))
182 ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
183 ar->ctrl_ep = response.endpoint;
184 break;
185 case WMI_DATA_BE_SVC:
186 set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
187 break;
188 case WMI_DATA_BK_SVC:
189 set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
190 break;
191 case WMI_DATA_VI_SVC:
192 set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
193 break;
194 case WMI_DATA_VO_SVC:
195 set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
196 break;
197 default:
198 ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
199 return -EINVAL;
200 }
201
202 return 0;
203}
204
205static int ath6kl_init_service_ep(struct ath6kl *ar)
206{
207 struct htc_service_connect_req connect;
208
209 memset(&connect, 0, sizeof(connect));
210
211 /* these fields are the same for all service endpoints */
212 connect.ep_cb.rx = ath6kl_rx;
213 connect.ep_cb.rx_refill = ath6kl_rx_refill;
214 connect.ep_cb.tx_full = ath6kl_tx_queue_full;
215
216 /*
217 * Set the max queue depth so that our ath6kl_tx_queue_full handler
218 * gets called.
219 */
220 connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
221 connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
222 if (!connect.ep_cb.rx_refill_thresh)
223 connect.ep_cb.rx_refill_thresh++;
224
225 /* connect to control service */
226 connect.svc_id = WMI_CONTROL_SVC;
227 if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
228 return -EIO;
229
230 connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
231
232 /*
233 * Limit the HTC message size on the send path, although e can
234 * receive A-MSDU frames of 4K, we will only send ethernet-sized
235 * (802.3) frames on the send path.
236 */
237 connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
238
239 /*
240 * To reduce the amount of committed memory for larger A_MSDU
241 * frames, use the recv-alloc threshold mechanism for larger
242 * packets.
243 */
244 connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
245 connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
246
247 /*
248 * For the remaining data services set the connection flag to
249 * reduce dribbling, if configured to do so.
250 */
251 connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
252 connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
253 connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
254
255 connect.svc_id = WMI_DATA_BE_SVC;
256
257 if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
258 return -EIO;
259
260 /* connect to back-ground map this to WMI LOW_PRI */
261 connect.svc_id = WMI_DATA_BK_SVC;
262 if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
263 return -EIO;
264
265 /* connect to Video service, map this to to HI PRI */
266 connect.svc_id = WMI_DATA_VI_SVC;
267 if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
268 return -EIO;
269
270 /*
271 * Connect to VO service, this is currently not mapped to a WMI
272 * priority stream due to historical reasons. WMI originally
273 * defined 3 priorities over 3 mailboxes We can change this when
274 * WMI is reworked so that priorities are not dependent on
275 * mailboxes.
276 */
277 connect.svc_id = WMI_DATA_VO_SVC;
278 if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
279 return -EIO;
280
281 return 0;
282}
283
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +0530284void ath6kl_init_control_info(struct ath6kl_vif *vif)
Kalle Valobdcd8172011-07-18 00:22:30 +0300285{
Vasanthakumar Thiagarajane29f25f2011-10-25 19:34:15 +0530286 ath6kl_init_profile_info(vif);
Vasanthakumar Thiagarajan34503342011-10-25 19:34:02 +0530287 vif->def_txkey_index = 0;
Vasanthakumar Thiagarajan6f2a73f2011-10-25 19:34:06 +0530288 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
Vasanthakumar Thiagarajanf74bac52011-10-25 19:34:05 +0530289 vif->ch_hint = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +0300290}
291
292/*
293 * Set HTC/Mbox operational parameters, this can only be called when the
294 * target is in the BMI phase.
295 */
296static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
297 u8 htc_ctrl_buf)
298{
299 int status;
300 u32 blk_size;
301
302 blk_size = ar->mbox_info.block_size;
303
304 if (htc_ctrl_buf)
305 blk_size |= ((u32)htc_ctrl_buf) << 16;
306
307 /* set the host interest area for the block size */
308 status = ath6kl_bmi_write(ar,
309 ath6kl_get_hi_item_addr(ar,
310 HI_ITEM(hi_mbox_io_block_sz)),
311 (u8 *)&blk_size,
312 4);
313 if (status) {
314 ath6kl_err("bmi_write_memory for IO block size failed\n");
315 goto out;
316 }
317
318 ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
319 blk_size,
320 ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
321
322 if (mbox_isr_yield_val) {
323 /* set the host interest area for the mbox ISR yield limit */
324 status = ath6kl_bmi_write(ar,
325 ath6kl_get_hi_item_addr(ar,
326 HI_ITEM(hi_mbox_isr_yield_limit)),
327 (u8 *)&mbox_isr_yield_val,
328 4);
329 if (status) {
330 ath6kl_err("bmi_write_memory for yield limit failed\n");
331 goto out;
332 }
333 }
334
335out:
336 return status;
337}
338
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530339static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
Kalle Valobdcd8172011-07-18 00:22:30 +0300340{
341 int status = 0;
Jouni Malinen4dea08e2011-08-30 21:57:57 +0300342 int ret;
Kalle Valobdcd8172011-07-18 00:22:30 +0300343
344 /*
345 * Configure the device for rx dot11 header rules. "0,0" are the
346 * default values. Required if checksum offload is needed. Set
347 * RxMetaVersion to 2.
348 */
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530349 if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300350 ar->rx_meta_ver, 0, 0)) {
351 ath6kl_err("unable to set the rx frame format\n");
352 status = -EIO;
353 }
354
355 if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530356 if ((ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
Kalle Valobdcd8172011-07-18 00:22:30 +0300357 IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
358 ath6kl_err("unable to set power save fail event policy\n");
359 status = -EIO;
360 }
361
362 if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530363 if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
Kalle Valobdcd8172011-07-18 00:22:30 +0300364 WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
365 ath6kl_err("unable to set barker preamble policy\n");
366 status = -EIO;
367 }
368
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530369 if (ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300370 WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
371 ath6kl_err("unable to set keep alive interval\n");
372 status = -EIO;
373 }
374
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530375 if (ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
Kalle Valobdcd8172011-07-18 00:22:30 +0300376 WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
377 ath6kl_err("unable to set disconnect timeout\n");
378 status = -EIO;
379 }
380
381 if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530382 if (ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED)) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300383 ath6kl_err("unable to set txop bursting\n");
384 status = -EIO;
385 }
386
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530387 /*
388 * FIXME: Make sure p2p configurations are not applied to
389 * non-p2p capable interfaces when multivif support is enabled.
390 */
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300391 if (ar->p2p) {
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530392 ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300393 P2P_FLAG_CAPABILITIES_REQ |
394 P2P_FLAG_MACADDR_REQ |
395 P2P_FLAG_HMODEL_REQ);
396 if (ret) {
397 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
398 "capabilities (%d) - assuming P2P not "
399 "supported\n", ret);
400 ar->p2p = 0;
401 }
402 }
403
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530404 /*
405 * FIXME: Make sure p2p configurations are not applied to
406 * non-p2p capable interfaces when multivif support is enabled.
407 */
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300408 if (ar->p2p) {
409 /* Enable Probe Request reporting for P2P */
Vasanthakumar Thiagarajan0ce59442011-10-25 19:34:25 +0530410 ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300411 if (ret) {
412 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
413 "Request reporting (%d)\n", ret);
414 }
Jouni Malinen4dea08e2011-08-30 21:57:57 +0300415 }
416
Kalle Valobdcd8172011-07-18 00:22:30 +0300417 return status;
418}
419
420int ath6kl_configure_target(struct ath6kl *ar)
421{
422 u32 param, ram_reserved_size;
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530423 u8 fw_iftype, fw_mode = 0, fw_submode = 0;
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530424 int i;
Kalle Valobdcd8172011-07-18 00:22:30 +0300425
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530426 /*
427 * Note: Even though the firmware interface type is
428 * chosen as BSS_STA for all three interfaces, can
429 * be configured to IBSS/AP as long as the fw submode
430 * remains normal mode (0 - AP, STA and IBSS). But
431 * due to an target assert in firmware only one interface is
432 * configured for now.
433 */
Vasanthakumar Thiagarajandd3751f2011-10-25 19:33:59 +0530434 fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
Kalle Valobdcd8172011-07-18 00:22:30 +0300435
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530436 for (i = 0; i < MAX_NUM_VIF; i++)
437 fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
438
439 /*
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530440 * By default, submodes :
441 * vif[0] - AP/STA/IBSS
442 * vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
443 * vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530444 */
Vasanthakumar Thiagarajan3226f68a2011-10-25 19:34:24 +0530445
446 for (i = 0; i < ar->max_norm_iface; i++)
447 fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
448 (i * HI_OPTION_FW_SUBMODE_BITS);
449
450 for (i = ar->max_norm_iface; i < MAX_NUM_VIF; i++)
451 fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
452 (i * HI_OPTION_FW_SUBMODE_BITS);
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530453
454 /*
455 * FIXME: This needs to be removed once the multivif
456 * support is enabled.
457 */
458 if (ar->p2p)
459 fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530460
Kalle Valobdcd8172011-07-18 00:22:30 +0300461 param = HTC_PROTOCOL_VERSION;
462 if (ath6kl_bmi_write(ar,
463 ath6kl_get_hi_item_addr(ar,
464 HI_ITEM(hi_app_host_interest)),
465 (u8 *)&param, 4) != 0) {
466 ath6kl_err("bmi_write_memory for htc version failed\n");
467 return -EIO;
468 }
469
470 /* set the firmware mode to STA/IBSS/AP */
471 param = 0;
472
473 if (ath6kl_bmi_read(ar,
474 ath6kl_get_hi_item_addr(ar,
475 HI_ITEM(hi_option_flag)),
476 (u8 *)&param, 4) != 0) {
477 ath6kl_err("bmi_read_memory for setting fwmode failed\n");
478 return -EIO;
479 }
480
Vasanthakumar Thiagarajan7b858322011-10-25 19:34:22 +0530481 param |= (MAX_NUM_VIF << HI_OPTION_NUM_DEV_SHIFT);
482 param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
483 param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
484
Kalle Valobdcd8172011-07-18 00:22:30 +0300485 param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
486 param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
487
488 if (ath6kl_bmi_write(ar,
489 ath6kl_get_hi_item_addr(ar,
490 HI_ITEM(hi_option_flag)),
491 (u8 *)&param,
492 4) != 0) {
493 ath6kl_err("bmi_write_memory for setting fwmode failed\n");
494 return -EIO;
495 }
496
497 ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
498
499 /*
500 * Hardcode the address use for the extended board data
501 * Ideally this should be pre-allocate by the OS at boot time
502 * But since it is a new feature and board data is loaded
503 * at init time, we have to workaround this from host.
504 * It is difficult to patch the firmware boot code,
505 * but possible in theory.
506 */
507
Kalle Valo991b27e2011-09-07 10:55:17 +0300508 param = ar->hw.board_ext_data_addr;
509 ram_reserved_size = ar->hw.reserved_ram_size;
Kalle Valobdcd8172011-07-18 00:22:30 +0300510
Kalle Valo991b27e2011-09-07 10:55:17 +0300511 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
512 HI_ITEM(hi_board_ext_data)),
513 (u8 *)&param, 4) != 0) {
514 ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
515 return -EIO;
516 }
517
518 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
519 HI_ITEM(hi_end_ram_reserve_sz)),
520 (u8 *)&ram_reserved_size, 4) != 0) {
521 ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
522 return -EIO;
Kalle Valobdcd8172011-07-18 00:22:30 +0300523 }
524
525 /* set the block size for the target */
526 if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
527 /* use default number of control buffers */
528 return -EIO;
529
530 return 0;
531}
532
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +0530533void ath6kl_core_free(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +0300534{
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +0530535 wiphy_free(ar->wiphy);
Kalle Valobdcd8172011-07-18 00:22:30 +0300536}
537
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530538void ath6kl_core_cleanup(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +0300539{
Kalle Valob2e75692011-10-27 18:48:14 +0300540 ath6kl_hif_power_off(ar);
541
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530542 destroy_workqueue(ar->ath6kl_wq);
Kalle Valobdcd8172011-07-18 00:22:30 +0300543
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +0530544 if (ar->htc_target)
545 ath6kl_htc_cleanup(ar->htc_target);
546
547 ath6kl_cookie_cleanup(ar);
548
549 ath6kl_cleanup_amsdu_rxbufs(ar);
550
551 ath6kl_bmi_cleanup(ar);
552
553 ath6kl_debug_cleanup(ar);
554
555 kfree(ar->fw_board);
556 kfree(ar->fw_otp);
557 kfree(ar->fw);
558 kfree(ar->fw_patch);
559
560 ath6kl_deinit_ieee80211_hw(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +0300561}
562
563/* firmware upload */
Kalle Valobdcd8172011-07-18 00:22:30 +0300564static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
565 u8 **fw, size_t *fw_len)
566{
567 const struct firmware *fw_entry;
568 int ret;
569
570 ret = request_firmware(&fw_entry, filename, ar->dev);
571 if (ret)
572 return ret;
573
574 *fw_len = fw_entry->size;
575 *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
576
577 if (*fw == NULL)
578 ret = -ENOMEM;
579
580 release_firmware(fw_entry);
581
582 return ret;
583}
584
Sam Leffler92ecbff2011-09-07 10:55:16 +0300585#ifdef CONFIG_OF
586static const char *get_target_ver_dir(const struct ath6kl *ar)
587{
588 switch (ar->version.target_ver) {
589 case AR6003_REV1_VERSION:
590 return "ath6k/AR6003/hw1.0";
591 case AR6003_REV2_VERSION:
592 return "ath6k/AR6003/hw2.0";
593 case AR6003_REV3_VERSION:
594 return "ath6k/AR6003/hw2.1.1";
595 }
596 ath6kl_warn("%s: unsupported target version 0x%x.\n", __func__,
597 ar->version.target_ver);
598 return NULL;
599}
600
601/*
602 * Check the device tree for a board-id and use it to construct
603 * the pathname to the firmware file. Used (for now) to find a
604 * fallback to the "bdata.bin" file--typically a symlink to the
605 * appropriate board-specific file.
606 */
607static bool check_device_tree(struct ath6kl *ar)
608{
609 static const char *board_id_prop = "atheros,board-id";
610 struct device_node *node;
611 char board_filename[64];
612 const char *board_id;
613 int ret;
614
615 for_each_compatible_node(node, NULL, "atheros,ath6kl") {
616 board_id = of_get_property(node, board_id_prop, NULL);
617 if (board_id == NULL) {
618 ath6kl_warn("No \"%s\" property on %s node.\n",
619 board_id_prop, node->name);
620 continue;
621 }
622 snprintf(board_filename, sizeof(board_filename),
623 "%s/bdata.%s.bin", get_target_ver_dir(ar), board_id);
624
625 ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
626 &ar->fw_board_len);
627 if (ret) {
628 ath6kl_err("Failed to get DT board file %s: %d\n",
629 board_filename, ret);
630 continue;
631 }
632 return true;
633 }
634 return false;
635}
636#else
637static bool check_device_tree(struct ath6kl *ar)
638{
639 return false;
640}
641#endif /* CONFIG_OF */
642
Kalle Valobdcd8172011-07-18 00:22:30 +0300643static int ath6kl_fetch_board_file(struct ath6kl *ar)
644{
645 const char *filename;
646 int ret;
647
Kalle Valo772c31e2011-09-07 10:55:16 +0300648 if (ar->fw_board != NULL)
649 return 0;
650
Kalle Valobdcd8172011-07-18 00:22:30 +0300651 switch (ar->version.target_ver) {
652 case AR6003_REV2_VERSION:
653 filename = AR6003_REV2_BOARD_DATA_FILE;
654 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800655 case AR6004_REV1_VERSION:
656 filename = AR6004_REV1_BOARD_DATA_FILE;
657 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300658 default:
659 filename = AR6003_REV3_BOARD_DATA_FILE;
660 break;
661 }
662
663 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
664 &ar->fw_board_len);
665 if (ret == 0) {
666 /* managed to get proper board file */
667 return 0;
668 }
669
Sam Leffler92ecbff2011-09-07 10:55:16 +0300670 if (check_device_tree(ar)) {
671 /* got board file from device tree */
672 return 0;
673 }
674
Kalle Valobdcd8172011-07-18 00:22:30 +0300675 /* there was no proper board file, try to use default instead */
676 ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
677 filename, ret);
678
679 switch (ar->version.target_ver) {
680 case AR6003_REV2_VERSION:
681 filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
682 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800683 case AR6004_REV1_VERSION:
684 filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
685 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300686 default:
687 filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
688 break;
689 }
690
691 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
692 &ar->fw_board_len);
693 if (ret) {
694 ath6kl_err("Failed to get default board file %s: %d\n",
695 filename, ret);
696 return ret;
697 }
698
699 ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
700 ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
701
702 return 0;
703}
704
Kalle Valo772c31e2011-09-07 10:55:16 +0300705static int ath6kl_fetch_otp_file(struct ath6kl *ar)
706{
707 const char *filename;
708 int ret;
709
710 if (ar->fw_otp != NULL)
711 return 0;
712
713 switch (ar->version.target_ver) {
714 case AR6003_REV2_VERSION:
715 filename = AR6003_REV2_OTP_FILE;
716 break;
717 case AR6004_REV1_VERSION:
718 ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
719 return 0;
720 break;
721 default:
722 filename = AR6003_REV3_OTP_FILE;
723 break;
724 }
725
726 ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
727 &ar->fw_otp_len);
728 if (ret) {
729 ath6kl_err("Failed to get OTP file %s: %d\n",
730 filename, ret);
731 return ret;
732 }
733
734 return 0;
735}
736
737static int ath6kl_fetch_fw_file(struct ath6kl *ar)
738{
739 const char *filename;
740 int ret;
741
742 if (ar->fw != NULL)
743 return 0;
744
745 if (testmode) {
746 switch (ar->version.target_ver) {
747 case AR6003_REV2_VERSION:
748 filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
749 break;
750 case AR6003_REV3_VERSION:
751 filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
752 break;
753 case AR6004_REV1_VERSION:
754 ath6kl_warn("testmode not supported with ar6004\n");
755 return -EOPNOTSUPP;
756 default:
757 ath6kl_warn("unknown target version: 0x%x\n",
758 ar->version.target_ver);
759 return -EINVAL;
760 }
761
762 set_bit(TESTMODE, &ar->flag);
763
764 goto get_fw;
765 }
766
767 switch (ar->version.target_ver) {
768 case AR6003_REV2_VERSION:
769 filename = AR6003_REV2_FIRMWARE_FILE;
770 break;
771 case AR6004_REV1_VERSION:
772 filename = AR6004_REV1_FIRMWARE_FILE;
773 break;
774 default:
775 filename = AR6003_REV3_FIRMWARE_FILE;
776 break;
777 }
778
779get_fw:
780 ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
781 if (ret) {
782 ath6kl_err("Failed to get firmware file %s: %d\n",
783 filename, ret);
784 return ret;
785 }
786
787 return 0;
788}
789
790static int ath6kl_fetch_patch_file(struct ath6kl *ar)
791{
792 const char *filename;
793 int ret;
794
795 switch (ar->version.target_ver) {
796 case AR6003_REV2_VERSION:
797 filename = AR6003_REV2_PATCH_FILE;
798 break;
799 case AR6004_REV1_VERSION:
800 /* FIXME: implement for AR6004 */
801 return 0;
802 break;
803 default:
804 filename = AR6003_REV3_PATCH_FILE;
805 break;
806 }
807
808 if (ar->fw_patch == NULL) {
809 ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
810 &ar->fw_patch_len);
811 if (ret) {
812 ath6kl_err("Failed to get patch file %s: %d\n",
813 filename, ret);
814 return ret;
815 }
816 }
817
818 return 0;
819}
820
Kalle Valo50d41232011-09-07 10:55:17 +0300821static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
Kalle Valo772c31e2011-09-07 10:55:16 +0300822{
823 int ret;
824
Kalle Valo772c31e2011-09-07 10:55:16 +0300825 ret = ath6kl_fetch_otp_file(ar);
826 if (ret)
827 return ret;
828
829 ret = ath6kl_fetch_fw_file(ar);
830 if (ret)
831 return ret;
832
833 ret = ath6kl_fetch_patch_file(ar);
834 if (ret)
835 return ret;
836
837 return 0;
838}
Kalle Valobdcd8172011-07-18 00:22:30 +0300839
Kalle Valo50d41232011-09-07 10:55:17 +0300840static int ath6kl_fetch_fw_api2(struct ath6kl *ar)
841{
842 size_t magic_len, len, ie_len;
843 const struct firmware *fw;
844 struct ath6kl_fw_ie *hdr;
845 const char *filename;
846 const u8 *data;
Kalle Valo97e04962011-09-12 13:47:34 +0300847 int ret, ie_id, i, index, bit;
Kalle Valo8a137482011-09-07 10:55:17 +0300848 __le32 *val;
Kalle Valo50d41232011-09-07 10:55:17 +0300849
850 switch (ar->version.target_ver) {
851 case AR6003_REV2_VERSION:
852 filename = AR6003_REV2_FIRMWARE_2_FILE;
853 break;
854 case AR6003_REV3_VERSION:
855 filename = AR6003_REV3_FIRMWARE_2_FILE;
856 break;
857 case AR6004_REV1_VERSION:
858 filename = AR6004_REV1_FIRMWARE_2_FILE;
859 break;
Kalle Valo50e27402011-11-11 12:18:06 +0200860 case AR6004_REV2_VERSION:
861 filename = AR6004_REV2_FIRMWARE_2_FILE;
862 break;
Kalle Valo50d41232011-09-07 10:55:17 +0300863 default:
864 return -EOPNOTSUPP;
865 }
866
867 ret = request_firmware(&fw, filename, ar->dev);
868 if (ret)
869 return ret;
870
871 data = fw->data;
872 len = fw->size;
873
874 /* magic also includes the null byte, check that as well */
875 magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
876
877 if (len < magic_len) {
878 ret = -EINVAL;
879 goto out;
880 }
881
882 if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
883 ret = -EINVAL;
884 goto out;
885 }
886
887 len -= magic_len;
888 data += magic_len;
889
890 /* loop elements */
891 while (len > sizeof(struct ath6kl_fw_ie)) {
892 /* hdr is unaligned! */
893 hdr = (struct ath6kl_fw_ie *) data;
894
895 ie_id = le32_to_cpup(&hdr->id);
896 ie_len = le32_to_cpup(&hdr->len);
897
898 len -= sizeof(*hdr);
899 data += sizeof(*hdr);
900
901 if (len < ie_len) {
902 ret = -EINVAL;
903 goto out;
904 }
905
906 switch (ie_id) {
907 case ATH6KL_FW_IE_OTP_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300908 ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300909 ie_len);
910
Kalle Valo50d41232011-09-07 10:55:17 +0300911 ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
912
913 if (ar->fw_otp == NULL) {
914 ret = -ENOMEM;
915 goto out;
916 }
917
918 ar->fw_otp_len = ie_len;
919 break;
920 case ATH6KL_FW_IE_FW_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300921 ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300922 ie_len);
923
Kalle Valo50d41232011-09-07 10:55:17 +0300924 ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
925
926 if (ar->fw == NULL) {
927 ret = -ENOMEM;
928 goto out;
929 }
930
931 ar->fw_len = ie_len;
932 break;
933 case ATH6KL_FW_IE_PATCH_IMAGE:
Kalle Valoef548622011-10-01 09:43:09 +0300934 ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300935 ie_len);
936
Kalle Valo50d41232011-09-07 10:55:17 +0300937 ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
938
939 if (ar->fw_patch == NULL) {
940 ret = -ENOMEM;
941 goto out;
942 }
943
944 ar->fw_patch_len = ie_len;
945 break;
Kalle Valo8a137482011-09-07 10:55:17 +0300946 case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
947 val = (__le32 *) data;
948 ar->hw.reserved_ram_size = le32_to_cpup(val);
Kalle Valo6bc36432011-09-27 14:31:11 +0300949
950 ath6kl_dbg(ATH6KL_DBG_BOOT,
951 "found reserved ram size ie 0x%d\n",
952 ar->hw.reserved_ram_size);
Kalle Valo8a137482011-09-07 10:55:17 +0300953 break;
Kalle Valo97e04962011-09-12 13:47:34 +0300954 case ATH6KL_FW_IE_CAPABILITIES:
Kalle Valo6bc36432011-09-27 14:31:11 +0300955 ath6kl_dbg(ATH6KL_DBG_BOOT,
Kalle Valoef548622011-10-01 09:43:09 +0300956 "found firmware capabilities ie (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300957 ie_len);
958
Kalle Valo97e04962011-09-12 13:47:34 +0300959 for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
960 index = ALIGN(i, 8) / 8;
961 bit = i % 8;
962
963 if (data[index] & (1 << bit))
964 __set_bit(i, ar->fw_capabilities);
965 }
Kalle Valo6bc36432011-09-27 14:31:11 +0300966
967 ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
968 ar->fw_capabilities,
969 sizeof(ar->fw_capabilities));
Kalle Valo97e04962011-09-12 13:47:34 +0300970 break;
Kalle Valo1b4304d2011-09-27 11:05:26 +0300971 case ATH6KL_FW_IE_PATCH_ADDR:
972 if (ie_len != sizeof(*val))
973 break;
974
975 val = (__le32 *) data;
976 ar->hw.dataset_patch_addr = le32_to_cpup(val);
Kalle Valo6bc36432011-09-27 14:31:11 +0300977
978 ath6kl_dbg(ATH6KL_DBG_BOOT,
979 "found patch address ie 0x%d\n",
980 ar->hw.dataset_patch_addr);
Kalle Valo1b4304d2011-09-27 11:05:26 +0300981 break;
Kalle Valo50d41232011-09-07 10:55:17 +0300982 default:
Kalle Valo6bc36432011-09-27 14:31:11 +0300983 ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
Kalle Valo50d41232011-09-07 10:55:17 +0300984 le32_to_cpup(&hdr->id));
985 break;
986 }
987
988 len -= ie_len;
989 data += ie_len;
990 };
991
992 ret = 0;
993out:
994 release_firmware(fw);
995
996 return ret;
997}
998
999static int ath6kl_fetch_firmwares(struct ath6kl *ar)
1000{
1001 int ret;
1002
1003 ret = ath6kl_fetch_board_file(ar);
1004 if (ret)
1005 return ret;
1006
1007 ret = ath6kl_fetch_fw_api2(ar);
Kalle Valo6bc36432011-09-27 14:31:11 +03001008 if (ret == 0) {
1009 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 2\n");
Kalle Valo50d41232011-09-07 10:55:17 +03001010 return 0;
Kalle Valo6bc36432011-09-27 14:31:11 +03001011 }
Kalle Valo50d41232011-09-07 10:55:17 +03001012
1013 ret = ath6kl_fetch_fw_api1(ar);
1014 if (ret)
1015 return ret;
1016
Kalle Valo6bc36432011-09-27 14:31:11 +03001017 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 1\n");
1018
Kalle Valo50d41232011-09-07 10:55:17 +03001019 return 0;
1020}
1021
Kalle Valobdcd8172011-07-18 00:22:30 +03001022static int ath6kl_upload_board_file(struct ath6kl *ar)
1023{
1024 u32 board_address, board_ext_address, param;
Kevin Fang31024d92011-07-11 17:14:13 +08001025 u32 board_data_size, board_ext_data_size;
Kalle Valobdcd8172011-07-18 00:22:30 +03001026 int ret;
1027
Kalle Valo772c31e2011-09-07 10:55:16 +03001028 if (WARN_ON(ar->fw_board == NULL))
1029 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001030
Kevin Fang31024d92011-07-11 17:14:13 +08001031 /*
1032 * Determine where in Target RAM to write Board Data.
1033 * For AR6004, host determine Target RAM address for
1034 * writing board data.
1035 */
Kalle Valo0d4d72b2011-11-14 19:30:39 +02001036 if (ar->hw.board_addr != 0) {
1037 board_address = ar->hw.board_addr;
Kevin Fang31024d92011-07-11 17:14:13 +08001038 ath6kl_bmi_write(ar,
1039 ath6kl_get_hi_item_addr(ar,
1040 HI_ITEM(hi_board_data)),
1041 (u8 *) &board_address, 4);
1042 } else {
1043 ath6kl_bmi_read(ar,
1044 ath6kl_get_hi_item_addr(ar,
1045 HI_ITEM(hi_board_data)),
1046 (u8 *) &board_address, 4);
1047 }
1048
Kalle Valobdcd8172011-07-18 00:22:30 +03001049 /* determine where in target ram to write extended board data */
1050 ath6kl_bmi_read(ar,
1051 ath6kl_get_hi_item_addr(ar,
1052 HI_ITEM(hi_board_ext_data)),
1053 (u8 *) &board_ext_address, 4);
1054
Kalle Valo50e27402011-11-11 12:18:06 +02001055 if (ar->target_type == TARGET_TYPE_AR6003 &&
1056 board_ext_address == 0) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001057 ath6kl_err("Failed to get board file target address.\n");
1058 return -EINVAL;
1059 }
1060
Kevin Fang31024d92011-07-11 17:14:13 +08001061 switch (ar->target_type) {
1062 case TARGET_TYPE_AR6003:
1063 board_data_size = AR6003_BOARD_DATA_SZ;
1064 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1065 break;
1066 case TARGET_TYPE_AR6004:
1067 board_data_size = AR6004_BOARD_DATA_SZ;
1068 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
1069 break;
1070 default:
1071 WARN_ON(1);
1072 return -EINVAL;
1073 break;
1074 }
1075
Kalle Valo50e27402011-11-11 12:18:06 +02001076 if (board_ext_address &&
1077 ar->fw_board_len == (board_data_size + board_ext_data_size)) {
Kevin Fang31024d92011-07-11 17:14:13 +08001078
Kalle Valobdcd8172011-07-18 00:22:30 +03001079 /* write extended board data */
Kalle Valo6bc36432011-09-27 14:31:11 +03001080 ath6kl_dbg(ATH6KL_DBG_BOOT,
1081 "writing extended board data to 0x%x (%d B)\n",
1082 board_ext_address, board_ext_data_size);
1083
Kalle Valobdcd8172011-07-18 00:22:30 +03001084 ret = ath6kl_bmi_write(ar, board_ext_address,
Kevin Fang31024d92011-07-11 17:14:13 +08001085 ar->fw_board + board_data_size,
1086 board_ext_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001087 if (ret) {
1088 ath6kl_err("Failed to write extended board data: %d\n",
1089 ret);
1090 return ret;
1091 }
1092
1093 /* record that extended board data is initialized */
Kevin Fang31024d92011-07-11 17:14:13 +08001094 param = (board_ext_data_size << 16) | 1;
1095
Kalle Valobdcd8172011-07-18 00:22:30 +03001096 ath6kl_bmi_write(ar,
1097 ath6kl_get_hi_item_addr(ar,
1098 HI_ITEM(hi_board_ext_data_config)),
1099 (unsigned char *) &param, 4);
1100 }
1101
Kevin Fang31024d92011-07-11 17:14:13 +08001102 if (ar->fw_board_len < board_data_size) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001103 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1104 ret = -EINVAL;
1105 return ret;
1106 }
1107
Kalle Valo6bc36432011-09-27 14:31:11 +03001108 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
1109 board_address, board_data_size);
1110
Kalle Valobdcd8172011-07-18 00:22:30 +03001111 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
Kevin Fang31024d92011-07-11 17:14:13 +08001112 board_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001113
1114 if (ret) {
1115 ath6kl_err("Board file bmi write failed: %d\n", ret);
1116 return ret;
1117 }
1118
1119 /* record the fact that Board Data IS initialized */
1120 param = 1;
1121 ath6kl_bmi_write(ar,
1122 ath6kl_get_hi_item_addr(ar,
1123 HI_ITEM(hi_board_data_initialized)),
1124 (u8 *)&param, 4);
1125
1126 return ret;
1127}
1128
1129static int ath6kl_upload_otp(struct ath6kl *ar)
1130{
Kalle Valobdcd8172011-07-18 00:22:30 +03001131 u32 address, param;
Kalle Valobef26a72011-10-12 09:58:28 +03001132 bool from_hw = false;
Kalle Valobdcd8172011-07-18 00:22:30 +03001133 int ret;
1134
Kalle Valo50e27402011-11-11 12:18:06 +02001135 if (ar->fw_otp == NULL)
1136 return 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03001137
Kalle Valoa01ac412011-09-07 10:55:17 +03001138 address = ar->hw.app_load_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001139
Kalle Valoef548622011-10-01 09:43:09 +03001140 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
Kalle Valo6bc36432011-09-27 14:31:11 +03001141 ar->fw_otp_len);
1142
Kalle Valobdcd8172011-07-18 00:22:30 +03001143 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1144 ar->fw_otp_len);
1145 if (ret) {
1146 ath6kl_err("Failed to upload OTP file: %d\n", ret);
1147 return ret;
1148 }
1149
Kalle Valo639d0b82011-09-12 12:48:09 +03001150 /* read firmware start address */
1151 ret = ath6kl_bmi_read(ar,
1152 ath6kl_get_hi_item_addr(ar,
1153 HI_ITEM(hi_app_start)),
1154 (u8 *) &address, sizeof(address));
1155
1156 if (ret) {
1157 ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1158 return ret;
1159 }
1160
Kalle Valobef26a72011-10-12 09:58:28 +03001161 if (ar->hw.app_start_override_addr == 0) {
1162 ar->hw.app_start_override_addr = address;
1163 from_hw = true;
1164 }
Kalle Valo639d0b82011-09-12 12:48:09 +03001165
Kalle Valobef26a72011-10-12 09:58:28 +03001166 ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1167 from_hw ? " (from hw)" : "",
Kalle Valo6bc36432011-09-27 14:31:11 +03001168 ar->hw.app_start_override_addr);
1169
Kalle Valobdcd8172011-07-18 00:22:30 +03001170 /* execute the OTP code */
Kalle Valobef26a72011-10-12 09:58:28 +03001171 ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1172 ar->hw.app_start_override_addr);
Kalle Valobdcd8172011-07-18 00:22:30 +03001173 param = 0;
Kalle Valobef26a72011-10-12 09:58:28 +03001174 ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
Kalle Valobdcd8172011-07-18 00:22:30 +03001175
1176 return ret;
1177}
1178
1179static int ath6kl_upload_firmware(struct ath6kl *ar)
1180{
Kalle Valobdcd8172011-07-18 00:22:30 +03001181 u32 address;
1182 int ret;
1183
Kalle Valo772c31e2011-09-07 10:55:16 +03001184 if (WARN_ON(ar->fw == NULL))
Kalle Valo50e27402011-11-11 12:18:06 +02001185 return 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03001186
Kalle Valoa01ac412011-09-07 10:55:17 +03001187 address = ar->hw.app_load_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001188
Kalle Valoef548622011-10-01 09:43:09 +03001189 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +03001190 address, ar->fw_len);
1191
Kalle Valobdcd8172011-07-18 00:22:30 +03001192 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1193
1194 if (ret) {
1195 ath6kl_err("Failed to write firmware: %d\n", ret);
1196 return ret;
1197 }
1198
Kevin Fang31024d92011-07-11 17:14:13 +08001199 /*
1200 * Set starting address for firmware
1201 * Don't need to setup app_start override addr on AR6004
1202 */
1203 if (ar->target_type != TARGET_TYPE_AR6004) {
Kalle Valoa01ac412011-09-07 10:55:17 +03001204 address = ar->hw.app_start_override_addr;
Kevin Fang31024d92011-07-11 17:14:13 +08001205 ath6kl_bmi_set_app_start(ar, address);
1206 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001207 return ret;
1208}
1209
1210static int ath6kl_upload_patch(struct ath6kl *ar)
1211{
Kalle Valobdcd8172011-07-18 00:22:30 +03001212 u32 address, param;
1213 int ret;
1214
Kalle Valo50e27402011-11-11 12:18:06 +02001215 if (ar->fw_patch == NULL)
1216 return 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03001217
Kalle Valoa01ac412011-09-07 10:55:17 +03001218 address = ar->hw.dataset_patch_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001219
Kalle Valoef548622011-10-01 09:43:09 +03001220 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +03001221 address, ar->fw_patch_len);
1222
Kalle Valobdcd8172011-07-18 00:22:30 +03001223 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1224 if (ret) {
1225 ath6kl_err("Failed to write patch file: %d\n", ret);
1226 return ret;
1227 }
1228
1229 param = address;
1230 ath6kl_bmi_write(ar,
1231 ath6kl_get_hi_item_addr(ar,
1232 HI_ITEM(hi_dset_list_head)),
1233 (unsigned char *) &param, 4);
1234
1235 return 0;
1236}
1237
1238static int ath6kl_init_upload(struct ath6kl *ar)
1239{
1240 u32 param, options, sleep, address;
1241 int status = 0;
1242
Kevin Fang31024d92011-07-11 17:14:13 +08001243 if (ar->target_type != TARGET_TYPE_AR6003 &&
1244 ar->target_type != TARGET_TYPE_AR6004)
Kalle Valobdcd8172011-07-18 00:22:30 +03001245 return -EINVAL;
1246
1247 /* temporarily disable system sleep */
1248 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1249 status = ath6kl_bmi_reg_read(ar, address, &param);
1250 if (status)
1251 return status;
1252
1253 options = param;
1254
1255 param |= ATH6KL_OPTION_SLEEP_DISABLE;
1256 status = ath6kl_bmi_reg_write(ar, address, param);
1257 if (status)
1258 return status;
1259
1260 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1261 status = ath6kl_bmi_reg_read(ar, address, &param);
1262 if (status)
1263 return status;
1264
1265 sleep = param;
1266
1267 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1268 status = ath6kl_bmi_reg_write(ar, address, param);
1269 if (status)
1270 return status;
1271
1272 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1273 options, sleep);
1274
1275 /* program analog PLL register */
Kevin Fang31024d92011-07-11 17:14:13 +08001276 /* no need to control 40/44MHz clock on AR6004 */
1277 if (ar->target_type != TARGET_TYPE_AR6004) {
1278 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1279 0xF9104001);
Kalle Valobdcd8172011-07-18 00:22:30 +03001280
Kevin Fang31024d92011-07-11 17:14:13 +08001281 if (status)
1282 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +03001283
Kevin Fang31024d92011-07-11 17:14:13 +08001284 /* Run at 80/88MHz by default */
1285 param = SM(CPU_CLOCK_STANDARD, 1);
1286
1287 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1288 status = ath6kl_bmi_reg_write(ar, address, param);
1289 if (status)
1290 return status;
1291 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001292
1293 param = 0;
1294 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1295 param = SM(LPO_CAL_ENABLE, 1);
1296 status = ath6kl_bmi_reg_write(ar, address, param);
1297 if (status)
1298 return status;
1299
1300 /* WAR to avoid SDIO CRC err */
1301 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1302 ath6kl_err("temporary war to avoid sdio crc error\n");
1303
1304 param = 0x20;
1305
1306 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1307 status = ath6kl_bmi_reg_write(ar, address, param);
1308 if (status)
1309 return status;
1310
1311 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1312 status = ath6kl_bmi_reg_write(ar, address, param);
1313 if (status)
1314 return status;
1315
1316 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1317 status = ath6kl_bmi_reg_write(ar, address, param);
1318 if (status)
1319 return status;
1320
1321 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1322 status = ath6kl_bmi_reg_write(ar, address, param);
1323 if (status)
1324 return status;
1325 }
1326
1327 /* write EEPROM data to Target RAM */
1328 status = ath6kl_upload_board_file(ar);
1329 if (status)
1330 return status;
1331
1332 /* transfer One time Programmable data */
1333 status = ath6kl_upload_otp(ar);
1334 if (status)
1335 return status;
1336
1337 /* Download Target firmware */
1338 status = ath6kl_upload_firmware(ar);
1339 if (status)
1340 return status;
1341
1342 status = ath6kl_upload_patch(ar);
1343 if (status)
1344 return status;
1345
1346 /* Restore system sleep */
1347 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1348 status = ath6kl_bmi_reg_write(ar, address, sleep);
1349 if (status)
1350 return status;
1351
1352 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1353 param = options | 0x20;
1354 status = ath6kl_bmi_reg_write(ar, address, param);
1355 if (status)
1356 return status;
1357
1358 /* Configure GPIO AR6003 UART */
1359 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1360 status = ath6kl_bmi_write(ar,
1361 ath6kl_get_hi_item_addr(ar,
1362 HI_ITEM(hi_dbg_uart_txpin)),
1363 (u8 *)&param, 4);
1364
1365 return status;
1366}
1367
Kalle Valoa01ac412011-09-07 10:55:17 +03001368static int ath6kl_init_hw_params(struct ath6kl *ar)
1369{
Kalle Valo856f4b32011-11-14 19:30:29 +02001370 const struct ath6kl_hw *hw;
1371 int i;
Kalle Valobef26a72011-10-12 09:58:28 +03001372
Kalle Valo856f4b32011-11-14 19:30:29 +02001373 for (i = 0; i < ARRAY_SIZE(hw_list); i++) {
1374 hw = &hw_list[i];
Kalle Valobef26a72011-10-12 09:58:28 +03001375
Kalle Valo856f4b32011-11-14 19:30:29 +02001376 if (hw->id == ar->version.target_ver)
1377 break;
1378 }
1379
1380 if (i == ARRAY_SIZE(hw_list)) {
Kalle Valoa01ac412011-09-07 10:55:17 +03001381 ath6kl_err("Unsupported hardware version: 0x%x\n",
1382 ar->version.target_ver);
1383 return -EINVAL;
1384 }
1385
Kalle Valo856f4b32011-11-14 19:30:29 +02001386 ar->hw = *hw;
1387
Kalle Valo6bc36432011-09-27 14:31:11 +03001388 ath6kl_dbg(ATH6KL_DBG_BOOT,
1389 "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
1390 ar->version.target_ver, ar->target_type,
1391 ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
1392 ath6kl_dbg(ATH6KL_DBG_BOOT,
1393 "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
1394 ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
1395 ar->hw.reserved_ram_size);
1396
Kalle Valoa01ac412011-09-07 10:55:17 +03001397 return 0;
1398}
1399
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001400int ath6kl_init_hw_start(struct ath6kl *ar)
Kalle Valo20459ee2011-10-27 18:48:37 +03001401{
1402 long timeleft;
1403 int ret, i;
1404
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001405 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
1406
Kalle Valo20459ee2011-10-27 18:48:37 +03001407 ret = ath6kl_hif_power_on(ar);
1408 if (ret)
1409 return ret;
1410
1411 ret = ath6kl_configure_target(ar);
1412 if (ret)
1413 goto err_power_off;
1414
1415 ret = ath6kl_init_upload(ar);
1416 if (ret)
1417 goto err_power_off;
1418
1419 /* Do we need to finish the BMI phase */
1420 /* FIXME: return error from ath6kl_bmi_done() */
1421 if (ath6kl_bmi_done(ar)) {
1422 ret = -EIO;
1423 goto err_power_off;
1424 }
1425
1426 /*
1427 * The reason we have to wait for the target here is that the
1428 * driver layer has to init BMI in order to set the host block
1429 * size.
1430 */
1431 if (ath6kl_htc_wait_target(ar->htc_target)) {
1432 ret = -EIO;
1433 goto err_power_off;
1434 }
1435
1436 if (ath6kl_init_service_ep(ar)) {
1437 ret = -EIO;
1438 goto err_cleanup_scatter;
1439 }
1440
1441 /* setup credit distribution */
1442 ath6kl_credit_setup(ar->htc_target, &ar->credit_state_info);
1443
1444 /* start HTC */
1445 ret = ath6kl_htc_start(ar->htc_target);
1446 if (ret) {
1447 /* FIXME: call this */
1448 ath6kl_cookie_cleanup(ar);
1449 goto err_cleanup_scatter;
1450 }
1451
1452 /* Wait for Wmi event to be ready */
1453 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1454 test_bit(WMI_READY,
1455 &ar->flag),
1456 WMI_TIMEOUT);
1457
1458 ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
1459
1460 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1461 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1462 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1463 ret = -EIO;
1464 goto err_htc_stop;
1465 }
1466
1467 if (!timeleft || signal_pending(current)) {
1468 ath6kl_err("wmi is not ready or wait was interrupted\n");
1469 ret = -EIO;
1470 goto err_htc_stop;
1471 }
1472
1473 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1474
1475 /* communicate the wmi protocol verision to the target */
1476 /* FIXME: return error */
1477 if ((ath6kl_set_host_app_area(ar)) != 0)
1478 ath6kl_err("unable to set the host app area\n");
1479
1480 for (i = 0; i < MAX_NUM_VIF; i++) {
1481 ret = ath6kl_target_config_wlan_params(ar, i);
1482 if (ret)
1483 goto err_htc_stop;
1484 }
1485
Kalle Valo76a9fbe2011-11-01 08:44:28 +02001486 ar->state = ATH6KL_STATE_ON;
1487
Kalle Valo20459ee2011-10-27 18:48:37 +03001488 return 0;
1489
1490err_htc_stop:
1491 ath6kl_htc_stop(ar->htc_target);
1492err_cleanup_scatter:
1493 ath6kl_hif_cleanup_scatter(ar);
1494err_power_off:
1495 ath6kl_hif_power_off(ar);
1496
1497 return ret;
1498}
1499
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001500int ath6kl_init_hw_stop(struct ath6kl *ar)
1501{
1502 int ret;
1503
1504 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
1505
1506 ath6kl_htc_stop(ar->htc_target);
1507
1508 ath6kl_hif_stop(ar);
1509
1510 ath6kl_bmi_reset(ar);
1511
1512 ret = ath6kl_hif_power_off(ar);
1513 if (ret)
1514 ath6kl_warn("failed to power off hif: %d\n", ret);
1515
Kalle Valo76a9fbe2011-11-01 08:44:28 +02001516 ar->state = ATH6KL_STATE_OFF;
1517
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001518 return 0;
1519}
1520
Kalle Valobdcd8172011-07-18 00:22:30 +03001521int ath6kl_core_init(struct ath6kl *ar)
1522{
Kalle Valobdcd8172011-07-18 00:22:30 +03001523 struct ath6kl_bmi_target_info targ_info;
Kalle Valo61448a92011-10-27 18:48:29 +03001524 struct net_device *ndev;
Kalle Valo20459ee2011-10-27 18:48:37 +03001525 int ret = 0, i;
Kalle Valobdcd8172011-07-18 00:22:30 +03001526
1527 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1528 if (!ar->ath6kl_wq)
1529 return -ENOMEM;
1530
1531 ret = ath6kl_bmi_init(ar);
1532 if (ret)
1533 goto err_wq;
1534
Kalle Valo20459ee2011-10-27 18:48:37 +03001535 /*
1536 * Turn on power to get hardware (target) version and leave power
1537 * on delibrately as we will boot the hardware anyway within few
1538 * seconds.
1539 */
Kalle Valob2e75692011-10-27 18:48:14 +03001540 ret = ath6kl_hif_power_on(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001541 if (ret)
1542 goto err_bmi_cleanup;
1543
Kalle Valob2e75692011-10-27 18:48:14 +03001544 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1545 if (ret)
1546 goto err_power_off;
1547
Kalle Valobdcd8172011-07-18 00:22:30 +03001548 ar->version.target_ver = le32_to_cpu(targ_info.version);
1549 ar->target_type = le32_to_cpu(targ_info.type);
Vasanthakumar Thiagarajanbe98e3a2011-10-25 19:33:57 +05301550 ar->wiphy->hw_version = le32_to_cpu(targ_info.version);
Kalle Valobdcd8172011-07-18 00:22:30 +03001551
Kalle Valoa01ac412011-09-07 10:55:17 +03001552 ret = ath6kl_init_hw_params(ar);
1553 if (ret)
Kalle Valob2e75692011-10-27 18:48:14 +03001554 goto err_power_off;
Kalle Valoa01ac412011-09-07 10:55:17 +03001555
Kalle Vaload226ec2011-08-10 09:49:12 +03001556 ar->htc_target = ath6kl_htc_create(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001557
1558 if (!ar->htc_target) {
1559 ret = -ENOMEM;
Kalle Valob2e75692011-10-27 18:48:14 +03001560 goto err_power_off;
Kalle Valobdcd8172011-07-18 00:22:30 +03001561 }
1562
Kalle Valo772c31e2011-09-07 10:55:16 +03001563 ret = ath6kl_fetch_firmwares(ar);
1564 if (ret)
1565 goto err_htc_cleanup;
1566
Kalle Valo61448a92011-10-27 18:48:29 +03001567 /* FIXME: we should free all firmwares in the error cases below */
1568
Kalle Valo61448a92011-10-27 18:48:29 +03001569 /* Indicate that WMI is enabled (although not ready yet) */
1570 set_bit(WMI_ENABLED, &ar->flag);
1571 ar->wmi = ath6kl_wmi_init(ar);
1572 if (!ar->wmi) {
1573 ath6kl_err("failed to initialize wmi\n");
1574 ret = -EIO;
1575 goto err_htc_cleanup;
1576 }
1577
1578 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1579
1580 ret = ath6kl_register_ieee80211_hw(ar);
1581 if (ret)
1582 goto err_node_cleanup;
1583
1584 ret = ath6kl_debug_init(ar);
1585 if (ret) {
1586 wiphy_unregister(ar->wiphy);
1587 goto err_node_cleanup;
1588 }
1589
1590 for (i = 0; i < MAX_NUM_VIF; i++)
1591 ar->avail_idx_map |= BIT(i);
1592
1593 rtnl_lock();
1594
1595 /* Add an initial station interface */
1596 ndev = ath6kl_interface_add(ar, "wlan%d", NL80211_IFTYPE_STATION, 0,
1597 INFRA_NETWORK);
1598
1599 rtnl_unlock();
1600
1601 if (!ndev) {
1602 ath6kl_err("Failed to instantiate a network device\n");
1603 ret = -ENOMEM;
1604 wiphy_unregister(ar->wiphy);
1605 goto err_debug_init;
1606 }
1607
1608
1609 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1610 __func__, ndev->name, ndev, ar);
1611
Kalle Valo61448a92011-10-27 18:48:29 +03001612 /* setup access class priority mappings */
1613 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1614 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1615 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1616 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1617
1618 /* give our connected endpoints some buffers */
1619 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1620 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1621
1622 /* allocate some buffers that handle larger AMSDU frames */
1623 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1624
Kalle Valo61448a92011-10-27 18:48:29 +03001625 ath6kl_cookie_init(ar);
1626
Kalle Valo61448a92011-10-27 18:48:29 +03001627 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1628 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1629
Kalle Valo8277de12011-11-03 12:18:31 +02001630 if (suspend_cutpower)
1631 ar->conf_flags |= ATH6KL_CONF_SUSPEND_CUTPOWER;
1632
Kalle Valo61448a92011-10-27 18:48:29 +03001633 ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM |
1634 WIPHY_FLAG_HAVE_AP_SME;
1635
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001636 set_bit(FIRST_BOOT, &ar->flag);
1637
1638 ret = ath6kl_init_hw_start(ar);
Kalle Valo20459ee2011-10-27 18:48:37 +03001639 if (ret) {
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001640 ath6kl_err("Failed to start hardware: %d\n", ret);
Kalle Valo20459ee2011-10-27 18:48:37 +03001641 goto err_rxbuf_cleanup;
Kalle Valo61448a92011-10-27 18:48:29 +03001642 }
1643
1644 /*
1645 * Set mac address which is received in ready event
1646 * FIXME: Move to ath6kl_interface_add()
1647 */
1648 memcpy(ndev->dev_addr, ar->mac_addr, ETH_ALEN);
Kalle Valobdcd8172011-07-18 00:22:30 +03001649
Kalle Valobdcd8172011-07-18 00:22:30 +03001650 return ret;
1651
Kalle Valo61448a92011-10-27 18:48:29 +03001652err_rxbuf_cleanup:
1653 ath6kl_htc_flush_rx_buf(ar->htc_target);
1654 ath6kl_cleanup_amsdu_rxbufs(ar);
Kalle Valo61448a92011-10-27 18:48:29 +03001655 rtnl_lock();
1656 ath6kl_deinit_if_data(netdev_priv(ndev));
1657 rtnl_unlock();
1658 wiphy_unregister(ar->wiphy);
1659err_debug_init:
1660 ath6kl_debug_cleanup(ar);
1661err_node_cleanup:
1662 ath6kl_wmi_shutdown(ar->wmi);
1663 clear_bit(WMI_ENABLED, &ar->flag);
1664 ar->wmi = NULL;
Kalle Valobdcd8172011-07-18 00:22:30 +03001665err_htc_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001666 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valob2e75692011-10-27 18:48:14 +03001667err_power_off:
1668 ath6kl_hif_power_off(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001669err_bmi_cleanup:
1670 ath6kl_bmi_cleanup(ar);
1671err_wq:
1672 destroy_workqueue(ar->ath6kl_wq);
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +05301673
Kalle Valobdcd8172011-07-18 00:22:30 +03001674 return ret;
1675}
1676
Vasanthakumar Thiagarajan55055972011-10-25 19:34:23 +05301677void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301678{
1679 static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1680 bool discon_issued;
1681
1682 netif_stop_queue(vif->ndev);
1683
1684 clear_bit(WLAN_ENABLED, &vif->flags);
1685
1686 if (wmi_ready) {
1687 discon_issued = test_bit(CONNECTED, &vif->flags) ||
1688 test_bit(CONNECT_PEND, &vif->flags);
1689 ath6kl_disconnect(vif);
1690 del_timer(&vif->disconnect_timer);
1691
1692 if (discon_issued)
1693 ath6kl_disconnect_event(vif, DISCONNECT_CMD,
1694 (vif->nw_type & AP_NETWORK) ?
1695 bcast_mac : vif->bssid,
1696 0, NULL, 0);
1697 }
1698
1699 if (vif->scan_req) {
1700 cfg80211_scan_done(vif->scan_req, true);
1701 vif->scan_req = NULL;
1702 }
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301703}
1704
Kalle Valobdcd8172011-07-18 00:22:30 +03001705void ath6kl_stop_txrx(struct ath6kl *ar)
1706{
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301707 struct ath6kl_vif *vif, *tmp_vif;
Kalle Valobdcd8172011-07-18 00:22:30 +03001708
1709 set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1710
1711 if (down_interruptible(&ar->sem)) {
1712 ath6kl_err("down_interruptible failed\n");
1713 return;
1714 }
1715
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301716 spin_lock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301717 list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1718 list_del(&vif->list);
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301719 spin_unlock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301720 ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
Vasanthakumar Thiagarajan27929722011-10-25 19:34:21 +05301721 rtnl_lock();
1722 ath6kl_deinit_if_data(vif);
1723 rtnl_unlock();
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301724 spin_lock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301725 }
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301726 spin_unlock_bh(&ar->list_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +03001727
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301728 clear_bit(WMI_READY, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001729
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301730 /*
1731 * After wmi_shudown all WMI events will be dropped. We
1732 * need to cleanup the buffers allocated in AP mode and
1733 * give disconnect notification to stack, which usually
1734 * happens in the disconnect_event. Simulate the disconnect
1735 * event by calling the function directly. Sometimes
1736 * disconnect_event will be received when the debug logs
1737 * are collected.
1738 */
1739 ath6kl_wmi_shutdown(ar->wmi);
Kalle Valobdcd8172011-07-18 00:22:30 +03001740
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301741 clear_bit(WMI_ENABLED, &ar->flag);
1742 if (ar->htc_target) {
1743 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
1744 ath6kl_htc_stop(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001745 }
1746
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301747 /*
1748 * Try to reset the device if we can. The driver may have been
1749 * configure NOT to reset the target during a debug session.
1750 */
1751 ath6kl_dbg(ATH6KL_DBG_TRC,
1752 "attempting to reset target on instance destroy\n");
1753 ath6kl_reset_device(ar, ar->target_type, true, true);
Kalle Valobdcd8172011-07-18 00:22:30 +03001754
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301755 clear_bit(WLAN_ENABLED, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001756}