blob: 0b0ae5ed280f77b2fb88c8b11fb8bb5ced574325 [file] [log] [blame]
Kalle Valobdcd8172011-07-18 00:22:30 +03001
2/*
3 * Copyright (c) 2011 Atheros Communications Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
Stephen Rothwellc6efe5782011-09-28 18:32:34 +100018#include <linux/moduleparam.h>
Sangwook Leef7830202011-10-26 16:28:38 +010019#include <linux/errno.h>
Sam Leffler92ecbff2011-09-07 10:55:16 +030020#include <linux/of.h>
Kalle Valobdcd8172011-07-18 00:22:30 +030021#include <linux/mmc/sdio_func.h>
22#include "core.h"
23#include "cfg80211.h"
24#include "target.h"
25#include "debug.h"
26#include "hif-ops.h"
27
28unsigned int debug_mask;
Kalle Valo003353b0d2011-09-01 10:14:21 +030029static unsigned int testmode;
Kalle Valo8277de12011-11-03 12:18:31 +020030static bool suspend_cutpower;
Kalle Valobdcd8172011-07-18 00:22:30 +030031
32module_param(debug_mask, uint, 0644);
Kalle Valo003353b0d2011-09-01 10:14:21 +030033module_param(testmode, uint, 0644);
Kalle Valo8277de12011-11-03 12:18:31 +020034module_param(suspend_cutpower, bool, 0444);
Kalle Valobdcd8172011-07-18 00:22:30 +030035
Kalle Valo856f4b312011-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 Valo856f4b312011-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 Valo856f4b312011-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,
Kalle Valo03ef0252011-11-14 19:30:47 +0200979 "found patch address ie 0x%x\n",
Kalle Valo6bc36432011-09-27 14:31:11 +0300980 ar->hw.dataset_patch_addr);
Kalle Valo1b4304d2011-09-27 11:05:26 +0300981 break;
Kalle Valo03ef0252011-11-14 19:30:47 +0200982 case ATH6KL_FW_IE_BOARD_ADDR:
983 if (ie_len != sizeof(*val))
984 break;
985
986 val = (__le32 *) data;
987 ar->hw.board_addr = le32_to_cpup(val);
988
989 ath6kl_dbg(ATH6KL_DBG_BOOT,
990 "found board address ie 0x%x\n",
991 ar->hw.board_addr);
992 break;
Kalle Valo50d41232011-09-07 10:55:17 +0300993 default:
Kalle Valo6bc36432011-09-27 14:31:11 +0300994 ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
Kalle Valo50d41232011-09-07 10:55:17 +0300995 le32_to_cpup(&hdr->id));
996 break;
997 }
998
999 len -= ie_len;
1000 data += ie_len;
1001 };
1002
1003 ret = 0;
1004out:
1005 release_firmware(fw);
1006
1007 return ret;
1008}
1009
1010static int ath6kl_fetch_firmwares(struct ath6kl *ar)
1011{
1012 int ret;
1013
1014 ret = ath6kl_fetch_board_file(ar);
1015 if (ret)
1016 return ret;
1017
1018 ret = ath6kl_fetch_fw_api2(ar);
Kalle Valo6bc36432011-09-27 14:31:11 +03001019 if (ret == 0) {
1020 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 2\n");
Kalle Valo50d41232011-09-07 10:55:17 +03001021 return 0;
Kalle Valo6bc36432011-09-27 14:31:11 +03001022 }
Kalle Valo50d41232011-09-07 10:55:17 +03001023
1024 ret = ath6kl_fetch_fw_api1(ar);
1025 if (ret)
1026 return ret;
1027
Kalle Valo6bc36432011-09-27 14:31:11 +03001028 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 1\n");
1029
Kalle Valo50d41232011-09-07 10:55:17 +03001030 return 0;
1031}
1032
Kalle Valobdcd8172011-07-18 00:22:30 +03001033static int ath6kl_upload_board_file(struct ath6kl *ar)
1034{
1035 u32 board_address, board_ext_address, param;
Kevin Fang31024d92011-07-11 17:14:13 +08001036 u32 board_data_size, board_ext_data_size;
Kalle Valobdcd8172011-07-18 00:22:30 +03001037 int ret;
1038
Kalle Valo772c31e2011-09-07 10:55:16 +03001039 if (WARN_ON(ar->fw_board == NULL))
1040 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001041
Kevin Fang31024d92011-07-11 17:14:13 +08001042 /*
1043 * Determine where in Target RAM to write Board Data.
1044 * For AR6004, host determine Target RAM address for
1045 * writing board data.
1046 */
Kalle Valo0d4d72b2011-11-14 19:30:39 +02001047 if (ar->hw.board_addr != 0) {
1048 board_address = ar->hw.board_addr;
Kevin Fang31024d92011-07-11 17:14:13 +08001049 ath6kl_bmi_write(ar,
1050 ath6kl_get_hi_item_addr(ar,
1051 HI_ITEM(hi_board_data)),
1052 (u8 *) &board_address, 4);
1053 } else {
1054 ath6kl_bmi_read(ar,
1055 ath6kl_get_hi_item_addr(ar,
1056 HI_ITEM(hi_board_data)),
1057 (u8 *) &board_address, 4);
1058 }
1059
Kalle Valobdcd8172011-07-18 00:22:30 +03001060 /* determine where in target ram to write extended board data */
1061 ath6kl_bmi_read(ar,
1062 ath6kl_get_hi_item_addr(ar,
1063 HI_ITEM(hi_board_ext_data)),
1064 (u8 *) &board_ext_address, 4);
1065
Kalle Valo50e27402011-11-11 12:18:06 +02001066 if (ar->target_type == TARGET_TYPE_AR6003 &&
1067 board_ext_address == 0) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001068 ath6kl_err("Failed to get board file target address.\n");
1069 return -EINVAL;
1070 }
1071
Kevin Fang31024d92011-07-11 17:14:13 +08001072 switch (ar->target_type) {
1073 case TARGET_TYPE_AR6003:
1074 board_data_size = AR6003_BOARD_DATA_SZ;
1075 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1076 break;
1077 case TARGET_TYPE_AR6004:
1078 board_data_size = AR6004_BOARD_DATA_SZ;
1079 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
1080 break;
1081 default:
1082 WARN_ON(1);
1083 return -EINVAL;
1084 break;
1085 }
1086
Kalle Valo50e27402011-11-11 12:18:06 +02001087 if (board_ext_address &&
1088 ar->fw_board_len == (board_data_size + board_ext_data_size)) {
Kevin Fang31024d92011-07-11 17:14:13 +08001089
Kalle Valobdcd8172011-07-18 00:22:30 +03001090 /* write extended board data */
Kalle Valo6bc36432011-09-27 14:31:11 +03001091 ath6kl_dbg(ATH6KL_DBG_BOOT,
1092 "writing extended board data to 0x%x (%d B)\n",
1093 board_ext_address, board_ext_data_size);
1094
Kalle Valobdcd8172011-07-18 00:22:30 +03001095 ret = ath6kl_bmi_write(ar, board_ext_address,
Kevin Fang31024d92011-07-11 17:14:13 +08001096 ar->fw_board + board_data_size,
1097 board_ext_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001098 if (ret) {
1099 ath6kl_err("Failed to write extended board data: %d\n",
1100 ret);
1101 return ret;
1102 }
1103
1104 /* record that extended board data is initialized */
Kevin Fang31024d92011-07-11 17:14:13 +08001105 param = (board_ext_data_size << 16) | 1;
1106
Kalle Valobdcd8172011-07-18 00:22:30 +03001107 ath6kl_bmi_write(ar,
1108 ath6kl_get_hi_item_addr(ar,
1109 HI_ITEM(hi_board_ext_data_config)),
1110 (unsigned char *) &param, 4);
1111 }
1112
Kevin Fang31024d92011-07-11 17:14:13 +08001113 if (ar->fw_board_len < board_data_size) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001114 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1115 ret = -EINVAL;
1116 return ret;
1117 }
1118
Kalle Valo6bc36432011-09-27 14:31:11 +03001119 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
1120 board_address, board_data_size);
1121
Kalle Valobdcd8172011-07-18 00:22:30 +03001122 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
Kevin Fang31024d92011-07-11 17:14:13 +08001123 board_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001124
1125 if (ret) {
1126 ath6kl_err("Board file bmi write failed: %d\n", ret);
1127 return ret;
1128 }
1129
1130 /* record the fact that Board Data IS initialized */
1131 param = 1;
1132 ath6kl_bmi_write(ar,
1133 ath6kl_get_hi_item_addr(ar,
1134 HI_ITEM(hi_board_data_initialized)),
1135 (u8 *)&param, 4);
1136
1137 return ret;
1138}
1139
1140static int ath6kl_upload_otp(struct ath6kl *ar)
1141{
Kalle Valobdcd8172011-07-18 00:22:30 +03001142 u32 address, param;
Kalle Valobef26a72011-10-12 09:58:28 +03001143 bool from_hw = false;
Kalle Valobdcd8172011-07-18 00:22:30 +03001144 int ret;
1145
Kalle Valo50e27402011-11-11 12:18:06 +02001146 if (ar->fw_otp == NULL)
1147 return 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03001148
Kalle Valoa01ac412011-09-07 10:55:17 +03001149 address = ar->hw.app_load_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001150
Kalle Valoef548622011-10-01 09:43:09 +03001151 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
Kalle Valo6bc36432011-09-27 14:31:11 +03001152 ar->fw_otp_len);
1153
Kalle Valobdcd8172011-07-18 00:22:30 +03001154 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1155 ar->fw_otp_len);
1156 if (ret) {
1157 ath6kl_err("Failed to upload OTP file: %d\n", ret);
1158 return ret;
1159 }
1160
Kalle Valo639d0b82011-09-12 12:48:09 +03001161 /* read firmware start address */
1162 ret = ath6kl_bmi_read(ar,
1163 ath6kl_get_hi_item_addr(ar,
1164 HI_ITEM(hi_app_start)),
1165 (u8 *) &address, sizeof(address));
1166
1167 if (ret) {
1168 ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1169 return ret;
1170 }
1171
Kalle Valobef26a72011-10-12 09:58:28 +03001172 if (ar->hw.app_start_override_addr == 0) {
1173 ar->hw.app_start_override_addr = address;
1174 from_hw = true;
1175 }
Kalle Valo639d0b82011-09-12 12:48:09 +03001176
Kalle Valobef26a72011-10-12 09:58:28 +03001177 ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1178 from_hw ? " (from hw)" : "",
Kalle Valo6bc36432011-09-27 14:31:11 +03001179 ar->hw.app_start_override_addr);
1180
Kalle Valobdcd8172011-07-18 00:22:30 +03001181 /* execute the OTP code */
Kalle Valobef26a72011-10-12 09:58:28 +03001182 ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1183 ar->hw.app_start_override_addr);
Kalle Valobdcd8172011-07-18 00:22:30 +03001184 param = 0;
Kalle Valobef26a72011-10-12 09:58:28 +03001185 ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
Kalle Valobdcd8172011-07-18 00:22:30 +03001186
1187 return ret;
1188}
1189
1190static int ath6kl_upload_firmware(struct ath6kl *ar)
1191{
Kalle Valobdcd8172011-07-18 00:22:30 +03001192 u32 address;
1193 int ret;
1194
Kalle Valo772c31e2011-09-07 10:55:16 +03001195 if (WARN_ON(ar->fw == NULL))
Kalle Valo50e27402011-11-11 12:18:06 +02001196 return 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03001197
Kalle Valoa01ac412011-09-07 10:55:17 +03001198 address = ar->hw.app_load_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001199
Kalle Valoef548622011-10-01 09:43:09 +03001200 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +03001201 address, ar->fw_len);
1202
Kalle Valobdcd8172011-07-18 00:22:30 +03001203 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1204
1205 if (ret) {
1206 ath6kl_err("Failed to write firmware: %d\n", ret);
1207 return ret;
1208 }
1209
Kevin Fang31024d92011-07-11 17:14:13 +08001210 /*
1211 * Set starting address for firmware
1212 * Don't need to setup app_start override addr on AR6004
1213 */
1214 if (ar->target_type != TARGET_TYPE_AR6004) {
Kalle Valoa01ac412011-09-07 10:55:17 +03001215 address = ar->hw.app_start_override_addr;
Kevin Fang31024d92011-07-11 17:14:13 +08001216 ath6kl_bmi_set_app_start(ar, address);
1217 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001218 return ret;
1219}
1220
1221static int ath6kl_upload_patch(struct ath6kl *ar)
1222{
Kalle Valobdcd8172011-07-18 00:22:30 +03001223 u32 address, param;
1224 int ret;
1225
Kalle Valo50e27402011-11-11 12:18:06 +02001226 if (ar->fw_patch == NULL)
1227 return 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03001228
Kalle Valoa01ac412011-09-07 10:55:17 +03001229 address = ar->hw.dataset_patch_addr;
Kalle Valobdcd8172011-07-18 00:22:30 +03001230
Kalle Valoef548622011-10-01 09:43:09 +03001231 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
Kalle Valo6bc36432011-09-27 14:31:11 +03001232 address, ar->fw_patch_len);
1233
Kalle Valobdcd8172011-07-18 00:22:30 +03001234 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1235 if (ret) {
1236 ath6kl_err("Failed to write patch file: %d\n", ret);
1237 return ret;
1238 }
1239
1240 param = address;
1241 ath6kl_bmi_write(ar,
1242 ath6kl_get_hi_item_addr(ar,
1243 HI_ITEM(hi_dset_list_head)),
1244 (unsigned char *) &param, 4);
1245
1246 return 0;
1247}
1248
1249static int ath6kl_init_upload(struct ath6kl *ar)
1250{
1251 u32 param, options, sleep, address;
1252 int status = 0;
1253
Kevin Fang31024d92011-07-11 17:14:13 +08001254 if (ar->target_type != TARGET_TYPE_AR6003 &&
1255 ar->target_type != TARGET_TYPE_AR6004)
Kalle Valobdcd8172011-07-18 00:22:30 +03001256 return -EINVAL;
1257
1258 /* temporarily disable system sleep */
1259 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1260 status = ath6kl_bmi_reg_read(ar, address, &param);
1261 if (status)
1262 return status;
1263
1264 options = param;
1265
1266 param |= ATH6KL_OPTION_SLEEP_DISABLE;
1267 status = ath6kl_bmi_reg_write(ar, address, param);
1268 if (status)
1269 return status;
1270
1271 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1272 status = ath6kl_bmi_reg_read(ar, address, &param);
1273 if (status)
1274 return status;
1275
1276 sleep = param;
1277
1278 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1279 status = ath6kl_bmi_reg_write(ar, address, param);
1280 if (status)
1281 return status;
1282
1283 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1284 options, sleep);
1285
1286 /* program analog PLL register */
Kevin Fang31024d92011-07-11 17:14:13 +08001287 /* no need to control 40/44MHz clock on AR6004 */
1288 if (ar->target_type != TARGET_TYPE_AR6004) {
1289 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1290 0xF9104001);
Kalle Valobdcd8172011-07-18 00:22:30 +03001291
Kevin Fang31024d92011-07-11 17:14:13 +08001292 if (status)
1293 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +03001294
Kevin Fang31024d92011-07-11 17:14:13 +08001295 /* Run at 80/88MHz by default */
1296 param = SM(CPU_CLOCK_STANDARD, 1);
1297
1298 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1299 status = ath6kl_bmi_reg_write(ar, address, param);
1300 if (status)
1301 return status;
1302 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001303
1304 param = 0;
1305 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1306 param = SM(LPO_CAL_ENABLE, 1);
1307 status = ath6kl_bmi_reg_write(ar, address, param);
1308 if (status)
1309 return status;
1310
1311 /* WAR to avoid SDIO CRC err */
1312 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1313 ath6kl_err("temporary war to avoid sdio crc error\n");
1314
1315 param = 0x20;
1316
1317 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1318 status = ath6kl_bmi_reg_write(ar, address, param);
1319 if (status)
1320 return status;
1321
1322 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1323 status = ath6kl_bmi_reg_write(ar, address, param);
1324 if (status)
1325 return status;
1326
1327 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1328 status = ath6kl_bmi_reg_write(ar, address, param);
1329 if (status)
1330 return status;
1331
1332 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1333 status = ath6kl_bmi_reg_write(ar, address, param);
1334 if (status)
1335 return status;
1336 }
1337
1338 /* write EEPROM data to Target RAM */
1339 status = ath6kl_upload_board_file(ar);
1340 if (status)
1341 return status;
1342
1343 /* transfer One time Programmable data */
1344 status = ath6kl_upload_otp(ar);
1345 if (status)
1346 return status;
1347
1348 /* Download Target firmware */
1349 status = ath6kl_upload_firmware(ar);
1350 if (status)
1351 return status;
1352
1353 status = ath6kl_upload_patch(ar);
1354 if (status)
1355 return status;
1356
1357 /* Restore system sleep */
1358 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1359 status = ath6kl_bmi_reg_write(ar, address, sleep);
1360 if (status)
1361 return status;
1362
1363 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1364 param = options | 0x20;
1365 status = ath6kl_bmi_reg_write(ar, address, param);
1366 if (status)
1367 return status;
1368
1369 /* Configure GPIO AR6003 UART */
1370 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1371 status = ath6kl_bmi_write(ar,
1372 ath6kl_get_hi_item_addr(ar,
1373 HI_ITEM(hi_dbg_uart_txpin)),
1374 (u8 *)&param, 4);
1375
1376 return status;
1377}
1378
Kalle Valoa01ac412011-09-07 10:55:17 +03001379static int ath6kl_init_hw_params(struct ath6kl *ar)
1380{
Kalle Valo856f4b312011-11-14 19:30:29 +02001381 const struct ath6kl_hw *hw;
1382 int i;
Kalle Valobef26a72011-10-12 09:58:28 +03001383
Kalle Valo856f4b312011-11-14 19:30:29 +02001384 for (i = 0; i < ARRAY_SIZE(hw_list); i++) {
1385 hw = &hw_list[i];
Kalle Valobef26a72011-10-12 09:58:28 +03001386
Kalle Valo856f4b312011-11-14 19:30:29 +02001387 if (hw->id == ar->version.target_ver)
1388 break;
1389 }
1390
1391 if (i == ARRAY_SIZE(hw_list)) {
Kalle Valoa01ac412011-09-07 10:55:17 +03001392 ath6kl_err("Unsupported hardware version: 0x%x\n",
1393 ar->version.target_ver);
1394 return -EINVAL;
1395 }
1396
Kalle Valo856f4b312011-11-14 19:30:29 +02001397 ar->hw = *hw;
1398
Kalle Valo6bc36432011-09-27 14:31:11 +03001399 ath6kl_dbg(ATH6KL_DBG_BOOT,
1400 "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
1401 ar->version.target_ver, ar->target_type,
1402 ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
1403 ath6kl_dbg(ATH6KL_DBG_BOOT,
1404 "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
1405 ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
1406 ar->hw.reserved_ram_size);
1407
Kalle Valoa01ac412011-09-07 10:55:17 +03001408 return 0;
1409}
1410
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001411int ath6kl_init_hw_start(struct ath6kl *ar)
Kalle Valo20459ee2011-10-27 18:48:37 +03001412{
1413 long timeleft;
1414 int ret, i;
1415
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001416 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
1417
Kalle Valo20459ee2011-10-27 18:48:37 +03001418 ret = ath6kl_hif_power_on(ar);
1419 if (ret)
1420 return ret;
1421
1422 ret = ath6kl_configure_target(ar);
1423 if (ret)
1424 goto err_power_off;
1425
1426 ret = ath6kl_init_upload(ar);
1427 if (ret)
1428 goto err_power_off;
1429
1430 /* Do we need to finish the BMI phase */
1431 /* FIXME: return error from ath6kl_bmi_done() */
1432 if (ath6kl_bmi_done(ar)) {
1433 ret = -EIO;
1434 goto err_power_off;
1435 }
1436
1437 /*
1438 * The reason we have to wait for the target here is that the
1439 * driver layer has to init BMI in order to set the host block
1440 * size.
1441 */
1442 if (ath6kl_htc_wait_target(ar->htc_target)) {
1443 ret = -EIO;
1444 goto err_power_off;
1445 }
1446
1447 if (ath6kl_init_service_ep(ar)) {
1448 ret = -EIO;
1449 goto err_cleanup_scatter;
1450 }
1451
1452 /* setup credit distribution */
1453 ath6kl_credit_setup(ar->htc_target, &ar->credit_state_info);
1454
1455 /* start HTC */
1456 ret = ath6kl_htc_start(ar->htc_target);
1457 if (ret) {
1458 /* FIXME: call this */
1459 ath6kl_cookie_cleanup(ar);
1460 goto err_cleanup_scatter;
1461 }
1462
1463 /* Wait for Wmi event to be ready */
1464 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1465 test_bit(WMI_READY,
1466 &ar->flag),
1467 WMI_TIMEOUT);
1468
1469 ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
1470
1471 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1472 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1473 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1474 ret = -EIO;
1475 goto err_htc_stop;
1476 }
1477
1478 if (!timeleft || signal_pending(current)) {
1479 ath6kl_err("wmi is not ready or wait was interrupted\n");
1480 ret = -EIO;
1481 goto err_htc_stop;
1482 }
1483
1484 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1485
1486 /* communicate the wmi protocol verision to the target */
1487 /* FIXME: return error */
1488 if ((ath6kl_set_host_app_area(ar)) != 0)
1489 ath6kl_err("unable to set the host app area\n");
1490
1491 for (i = 0; i < MAX_NUM_VIF; i++) {
1492 ret = ath6kl_target_config_wlan_params(ar, i);
1493 if (ret)
1494 goto err_htc_stop;
1495 }
1496
Kalle Valo76a9fbe2011-11-01 08:44:28 +02001497 ar->state = ATH6KL_STATE_ON;
1498
Kalle Valo20459ee2011-10-27 18:48:37 +03001499 return 0;
1500
1501err_htc_stop:
1502 ath6kl_htc_stop(ar->htc_target);
1503err_cleanup_scatter:
1504 ath6kl_hif_cleanup_scatter(ar);
1505err_power_off:
1506 ath6kl_hif_power_off(ar);
1507
1508 return ret;
1509}
1510
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001511int ath6kl_init_hw_stop(struct ath6kl *ar)
1512{
1513 int ret;
1514
1515 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
1516
1517 ath6kl_htc_stop(ar->htc_target);
1518
1519 ath6kl_hif_stop(ar);
1520
1521 ath6kl_bmi_reset(ar);
1522
1523 ret = ath6kl_hif_power_off(ar);
1524 if (ret)
1525 ath6kl_warn("failed to power off hif: %d\n", ret);
1526
Kalle Valo76a9fbe2011-11-01 08:44:28 +02001527 ar->state = ATH6KL_STATE_OFF;
1528
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001529 return 0;
1530}
1531
Kalle Valobdcd8172011-07-18 00:22:30 +03001532int ath6kl_core_init(struct ath6kl *ar)
1533{
Kalle Valobdcd8172011-07-18 00:22:30 +03001534 struct ath6kl_bmi_target_info targ_info;
Kalle Valo61448a92011-10-27 18:48:29 +03001535 struct net_device *ndev;
Kalle Valo20459ee2011-10-27 18:48:37 +03001536 int ret = 0, i;
Kalle Valobdcd8172011-07-18 00:22:30 +03001537
1538 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1539 if (!ar->ath6kl_wq)
1540 return -ENOMEM;
1541
1542 ret = ath6kl_bmi_init(ar);
1543 if (ret)
1544 goto err_wq;
1545
Kalle Valo20459ee2011-10-27 18:48:37 +03001546 /*
1547 * Turn on power to get hardware (target) version and leave power
1548 * on delibrately as we will boot the hardware anyway within few
1549 * seconds.
1550 */
Kalle Valob2e75692011-10-27 18:48:14 +03001551 ret = ath6kl_hif_power_on(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001552 if (ret)
1553 goto err_bmi_cleanup;
1554
Kalle Valob2e75692011-10-27 18:48:14 +03001555 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1556 if (ret)
1557 goto err_power_off;
1558
Kalle Valobdcd8172011-07-18 00:22:30 +03001559 ar->version.target_ver = le32_to_cpu(targ_info.version);
1560 ar->target_type = le32_to_cpu(targ_info.type);
Vasanthakumar Thiagarajanbe98e3a2011-10-25 19:33:57 +05301561 ar->wiphy->hw_version = le32_to_cpu(targ_info.version);
Kalle Valobdcd8172011-07-18 00:22:30 +03001562
Kalle Valoa01ac412011-09-07 10:55:17 +03001563 ret = ath6kl_init_hw_params(ar);
1564 if (ret)
Kalle Valob2e75692011-10-27 18:48:14 +03001565 goto err_power_off;
Kalle Valoa01ac412011-09-07 10:55:17 +03001566
Kalle Vaload226ec2011-08-10 09:49:12 +03001567 ar->htc_target = ath6kl_htc_create(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001568
1569 if (!ar->htc_target) {
1570 ret = -ENOMEM;
Kalle Valob2e75692011-10-27 18:48:14 +03001571 goto err_power_off;
Kalle Valobdcd8172011-07-18 00:22:30 +03001572 }
1573
Kalle Valo772c31e2011-09-07 10:55:16 +03001574 ret = ath6kl_fetch_firmwares(ar);
1575 if (ret)
1576 goto err_htc_cleanup;
1577
Kalle Valo61448a92011-10-27 18:48:29 +03001578 /* FIXME: we should free all firmwares in the error cases below */
1579
Kalle Valo61448a92011-10-27 18:48:29 +03001580 /* Indicate that WMI is enabled (although not ready yet) */
1581 set_bit(WMI_ENABLED, &ar->flag);
1582 ar->wmi = ath6kl_wmi_init(ar);
1583 if (!ar->wmi) {
1584 ath6kl_err("failed to initialize wmi\n");
1585 ret = -EIO;
1586 goto err_htc_cleanup;
1587 }
1588
1589 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1590
1591 ret = ath6kl_register_ieee80211_hw(ar);
1592 if (ret)
1593 goto err_node_cleanup;
1594
1595 ret = ath6kl_debug_init(ar);
1596 if (ret) {
1597 wiphy_unregister(ar->wiphy);
1598 goto err_node_cleanup;
1599 }
1600
1601 for (i = 0; i < MAX_NUM_VIF; i++)
1602 ar->avail_idx_map |= BIT(i);
1603
1604 rtnl_lock();
1605
1606 /* Add an initial station interface */
1607 ndev = ath6kl_interface_add(ar, "wlan%d", NL80211_IFTYPE_STATION, 0,
1608 INFRA_NETWORK);
1609
1610 rtnl_unlock();
1611
1612 if (!ndev) {
1613 ath6kl_err("Failed to instantiate a network device\n");
1614 ret = -ENOMEM;
1615 wiphy_unregister(ar->wiphy);
1616 goto err_debug_init;
1617 }
1618
1619
1620 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1621 __func__, ndev->name, ndev, ar);
1622
Kalle Valo61448a92011-10-27 18:48:29 +03001623 /* setup access class priority mappings */
1624 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1625 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1626 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1627 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1628
1629 /* give our connected endpoints some buffers */
1630 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1631 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1632
1633 /* allocate some buffers that handle larger AMSDU frames */
1634 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1635
Kalle Valo61448a92011-10-27 18:48:29 +03001636 ath6kl_cookie_init(ar);
1637
Kalle Valo61448a92011-10-27 18:48:29 +03001638 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1639 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1640
Kalle Valo8277de12011-11-03 12:18:31 +02001641 if (suspend_cutpower)
1642 ar->conf_flags |= ATH6KL_CONF_SUSPEND_CUTPOWER;
1643
Kalle Valo61448a92011-10-27 18:48:29 +03001644 ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM |
1645 WIPHY_FLAG_HAVE_AP_SME;
1646
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001647 set_bit(FIRST_BOOT, &ar->flag);
1648
1649 ret = ath6kl_init_hw_start(ar);
Kalle Valo20459ee2011-10-27 18:48:37 +03001650 if (ret) {
Kalle Valo5fe4dff2011-10-30 21:16:15 +02001651 ath6kl_err("Failed to start hardware: %d\n", ret);
Kalle Valo20459ee2011-10-27 18:48:37 +03001652 goto err_rxbuf_cleanup;
Kalle Valo61448a92011-10-27 18:48:29 +03001653 }
1654
1655 /*
1656 * Set mac address which is received in ready event
1657 * FIXME: Move to ath6kl_interface_add()
1658 */
1659 memcpy(ndev->dev_addr, ar->mac_addr, ETH_ALEN);
Kalle Valobdcd8172011-07-18 00:22:30 +03001660
Kalle Valobdcd8172011-07-18 00:22:30 +03001661 return ret;
1662
Kalle Valo61448a92011-10-27 18:48:29 +03001663err_rxbuf_cleanup:
1664 ath6kl_htc_flush_rx_buf(ar->htc_target);
1665 ath6kl_cleanup_amsdu_rxbufs(ar);
Kalle Valo61448a92011-10-27 18:48:29 +03001666 rtnl_lock();
1667 ath6kl_deinit_if_data(netdev_priv(ndev));
1668 rtnl_unlock();
1669 wiphy_unregister(ar->wiphy);
1670err_debug_init:
1671 ath6kl_debug_cleanup(ar);
1672err_node_cleanup:
1673 ath6kl_wmi_shutdown(ar->wmi);
1674 clear_bit(WMI_ENABLED, &ar->flag);
1675 ar->wmi = NULL;
Kalle Valobdcd8172011-07-18 00:22:30 +03001676err_htc_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001677 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valob2e75692011-10-27 18:48:14 +03001678err_power_off:
1679 ath6kl_hif_power_off(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001680err_bmi_cleanup:
1681 ath6kl_bmi_cleanup(ar);
1682err_wq:
1683 destroy_workqueue(ar->ath6kl_wq);
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +05301684
Kalle Valobdcd8172011-07-18 00:22:30 +03001685 return ret;
1686}
1687
Vasanthakumar Thiagarajan55055972011-10-25 19:34:23 +05301688void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301689{
1690 static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1691 bool discon_issued;
1692
1693 netif_stop_queue(vif->ndev);
1694
1695 clear_bit(WLAN_ENABLED, &vif->flags);
1696
1697 if (wmi_ready) {
1698 discon_issued = test_bit(CONNECTED, &vif->flags) ||
1699 test_bit(CONNECT_PEND, &vif->flags);
1700 ath6kl_disconnect(vif);
1701 del_timer(&vif->disconnect_timer);
1702
1703 if (discon_issued)
1704 ath6kl_disconnect_event(vif, DISCONNECT_CMD,
1705 (vif->nw_type & AP_NETWORK) ?
1706 bcast_mac : vif->bssid,
1707 0, NULL, 0);
1708 }
1709
1710 if (vif->scan_req) {
1711 cfg80211_scan_done(vif->scan_req, true);
1712 vif->scan_req = NULL;
1713 }
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301714}
1715
Kalle Valobdcd8172011-07-18 00:22:30 +03001716void ath6kl_stop_txrx(struct ath6kl *ar)
1717{
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301718 struct ath6kl_vif *vif, *tmp_vif;
Kalle Valobdcd8172011-07-18 00:22:30 +03001719
1720 set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1721
1722 if (down_interruptible(&ar->sem)) {
1723 ath6kl_err("down_interruptible failed\n");
1724 return;
1725 }
1726
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301727 spin_lock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301728 list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1729 list_del(&vif->list);
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301730 spin_unlock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301731 ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
Vasanthakumar Thiagarajan27929722011-10-25 19:34:21 +05301732 rtnl_lock();
1733 ath6kl_deinit_if_data(vif);
1734 rtnl_unlock();
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301735 spin_lock_bh(&ar->list_lock);
Vasanthakumar Thiagarajan990bd912011-10-25 19:34:20 +05301736 }
Vasanthakumar Thiagarajan11f6e402011-11-01 16:38:50 +05301737 spin_unlock_bh(&ar->list_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +03001738
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301739 clear_bit(WMI_READY, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001740
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301741 /*
1742 * After wmi_shudown all WMI events will be dropped. We
1743 * need to cleanup the buffers allocated in AP mode and
1744 * give disconnect notification to stack, which usually
1745 * happens in the disconnect_event. Simulate the disconnect
1746 * event by calling the function directly. Sometimes
1747 * disconnect_event will be received when the debug logs
1748 * are collected.
1749 */
1750 ath6kl_wmi_shutdown(ar->wmi);
Kalle Valobdcd8172011-07-18 00:22:30 +03001751
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301752 clear_bit(WMI_ENABLED, &ar->flag);
1753 if (ar->htc_target) {
1754 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
1755 ath6kl_htc_stop(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001756 }
1757
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301758 /*
1759 * Try to reset the device if we can. The driver may have been
1760 * configure NOT to reset the target during a debug session.
1761 */
1762 ath6kl_dbg(ATH6KL_DBG_TRC,
1763 "attempting to reset target on instance destroy\n");
1764 ath6kl_reset_device(ar, ar->target_type, true, true);
Kalle Valobdcd8172011-07-18 00:22:30 +03001765
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301766 clear_bit(WLAN_ENABLED, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001767}