blob: 91716709cac8c86b360964038cb8f97a0e60209b [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
Sam Leffler92ecbff2011-09-07 10:55:16 +030018#include <linux/of.h>
Kalle Valobdcd8172011-07-18 00:22:30 +030019#include <linux/mmc/sdio_func.h>
20#include "core.h"
21#include "cfg80211.h"
22#include "target.h"
23#include "debug.h"
24#include "hif-ops.h"
25
26unsigned int debug_mask;
Kalle Valo003353b2011-09-01 10:14:21 +030027static unsigned int testmode;
Kalle Valobdcd8172011-07-18 00:22:30 +030028
29module_param(debug_mask, uint, 0644);
Kalle Valo003353b2011-09-01 10:14:21 +030030module_param(testmode, uint, 0644);
Kalle Valobdcd8172011-07-18 00:22:30 +030031
32/*
33 * Include definitions here that can be used to tune the WLAN module
34 * behavior. Different customers can tune the behavior as per their needs,
35 * here.
36 */
37
38/*
39 * This configuration item enable/disable keepalive support.
40 * Keepalive support: In the absence of any data traffic to AP, null
41 * frames will be sent to the AP at periodic interval, to keep the association
42 * active. This configuration item defines the periodic interval.
43 * Use value of zero to disable keepalive support
44 * Default: 60 seconds
45 */
46#define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
47
48/*
49 * This configuration item sets the value of disconnect timeout
50 * Firmware delays sending the disconnec event to the host for this
51 * timeout after is gets disconnected from the current AP.
52 * If the firmware successly roams within the disconnect timeout
53 * it sends a new connect event
54 */
55#define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
56
57#define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
58
59enum addr_type {
60 DATASET_PATCH_ADDR,
61 APP_LOAD_ADDR,
62 APP_START_OVERRIDE_ADDR,
63};
64
65#define ATH6KL_DATA_OFFSET 64
66struct sk_buff *ath6kl_buf_alloc(int size)
67{
68 struct sk_buff *skb;
69 u16 reserved;
70
71 /* Add chacheline space at front and back of buffer */
72 reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
Vasanthakumar Thiagarajan1df94a82011-08-17 18:45:10 +053073 sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
Kalle Valobdcd8172011-07-18 00:22:30 +030074 skb = dev_alloc_skb(size + reserved);
75
76 if (skb)
77 skb_reserve(skb, reserved - L1_CACHE_BYTES);
78 return skb;
79}
80
81void ath6kl_init_profile_info(struct ath6kl *ar)
82{
83 ar->ssid_len = 0;
84 memset(ar->ssid, 0, sizeof(ar->ssid));
85
86 ar->dot11_auth_mode = OPEN_AUTH;
87 ar->auth_mode = NONE_AUTH;
88 ar->prwise_crypto = NONE_CRYPT;
89 ar->prwise_crypto_len = 0;
90 ar->grp_crypto = NONE_CRYPT;
Edward Lu38acde32011-08-30 21:58:06 +030091 ar->grp_crypto_len = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +030092 memset(ar->wep_key_list, 0, sizeof(ar->wep_key_list));
93 memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
94 memset(ar->bssid, 0, sizeof(ar->bssid));
95 ar->bss_ch = 0;
96 ar->nw_type = ar->next_mode = INFRA_NETWORK;
97}
98
99static u8 ath6kl_get_fw_iftype(struct ath6kl *ar)
100{
101 switch (ar->nw_type) {
102 case INFRA_NETWORK:
103 return HI_OPTION_FW_MODE_BSS_STA;
104 case ADHOC_NETWORK:
105 return HI_OPTION_FW_MODE_IBSS;
106 case AP_NETWORK:
107 return HI_OPTION_FW_MODE_AP;
108 default:
109 ath6kl_err("Unsupported interface type :%d\n", ar->nw_type);
110 return 0xff;
111 }
112}
113
Kalle Valobdcd8172011-07-18 00:22:30 +0300114static int ath6kl_set_host_app_area(struct ath6kl *ar)
115{
116 u32 address, data;
117 struct host_app_area host_app_area;
118
119 /* Fetch the address of the host_app_area_s
120 * instance in the host interest area */
121 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
Kevin Fang31024d92011-07-11 17:14:13 +0800122 address = TARG_VTOP(ar->target_type, address);
Kalle Valobdcd8172011-07-18 00:22:30 +0300123
Kalle Valoaddb44b2011-09-02 10:32:05 +0300124 if (ath6kl_diag_read32(ar, address, &data))
Kalle Valobdcd8172011-07-18 00:22:30 +0300125 return -EIO;
126
Kevin Fang31024d92011-07-11 17:14:13 +0800127 address = TARG_VTOP(ar->target_type, data);
Kalle Valobdcd8172011-07-18 00:22:30 +0300128 host_app_area.wmi_protocol_ver = WMI_PROTOCOL_VERSION;
Kalle Valoaddb44b2011-09-02 10:32:05 +0300129 if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
130 sizeof(struct host_app_area)))
Kalle Valobdcd8172011-07-18 00:22:30 +0300131 return -EIO;
132
133 return 0;
134}
135
136static inline void set_ac2_ep_map(struct ath6kl *ar,
137 u8 ac,
138 enum htc_endpoint_id ep)
139{
140 ar->ac2ep_map[ac] = ep;
141 ar->ep2ac_map[ep] = ac;
142}
143
144/* connect to a service */
145static int ath6kl_connectservice(struct ath6kl *ar,
146 struct htc_service_connect_req *con_req,
147 char *desc)
148{
149 int status;
150 struct htc_service_connect_resp response;
151
152 memset(&response, 0, sizeof(response));
153
Kalle Vaload226ec2011-08-10 09:49:12 +0300154 status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
Kalle Valobdcd8172011-07-18 00:22:30 +0300155 if (status) {
156 ath6kl_err("failed to connect to %s service status:%d\n",
157 desc, status);
158 return status;
159 }
160
161 switch (con_req->svc_id) {
162 case WMI_CONTROL_SVC:
163 if (test_bit(WMI_ENABLED, &ar->flag))
164 ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
165 ar->ctrl_ep = response.endpoint;
166 break;
167 case WMI_DATA_BE_SVC:
168 set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
169 break;
170 case WMI_DATA_BK_SVC:
171 set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
172 break;
173 case WMI_DATA_VI_SVC:
174 set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
175 break;
176 case WMI_DATA_VO_SVC:
177 set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
178 break;
179 default:
180 ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
181 return -EINVAL;
182 }
183
184 return 0;
185}
186
187static int ath6kl_init_service_ep(struct ath6kl *ar)
188{
189 struct htc_service_connect_req connect;
190
191 memset(&connect, 0, sizeof(connect));
192
193 /* these fields are the same for all service endpoints */
194 connect.ep_cb.rx = ath6kl_rx;
195 connect.ep_cb.rx_refill = ath6kl_rx_refill;
196 connect.ep_cb.tx_full = ath6kl_tx_queue_full;
197
198 /*
199 * Set the max queue depth so that our ath6kl_tx_queue_full handler
200 * gets called.
201 */
202 connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
203 connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
204 if (!connect.ep_cb.rx_refill_thresh)
205 connect.ep_cb.rx_refill_thresh++;
206
207 /* connect to control service */
208 connect.svc_id = WMI_CONTROL_SVC;
209 if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
210 return -EIO;
211
212 connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
213
214 /*
215 * Limit the HTC message size on the send path, although e can
216 * receive A-MSDU frames of 4K, we will only send ethernet-sized
217 * (802.3) frames on the send path.
218 */
219 connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
220
221 /*
222 * To reduce the amount of committed memory for larger A_MSDU
223 * frames, use the recv-alloc threshold mechanism for larger
224 * packets.
225 */
226 connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
227 connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
228
229 /*
230 * For the remaining data services set the connection flag to
231 * reduce dribbling, if configured to do so.
232 */
233 connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
234 connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
235 connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
236
237 connect.svc_id = WMI_DATA_BE_SVC;
238
239 if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
240 return -EIO;
241
242 /* connect to back-ground map this to WMI LOW_PRI */
243 connect.svc_id = WMI_DATA_BK_SVC;
244 if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
245 return -EIO;
246
247 /* connect to Video service, map this to to HI PRI */
248 connect.svc_id = WMI_DATA_VI_SVC;
249 if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
250 return -EIO;
251
252 /*
253 * Connect to VO service, this is currently not mapped to a WMI
254 * priority stream due to historical reasons. WMI originally
255 * defined 3 priorities over 3 mailboxes We can change this when
256 * WMI is reworked so that priorities are not dependent on
257 * mailboxes.
258 */
259 connect.svc_id = WMI_DATA_VO_SVC;
260 if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
261 return -EIO;
262
263 return 0;
264}
265
266static void ath6kl_init_control_info(struct ath6kl *ar)
267{
268 u8 ctr;
269
270 clear_bit(WMI_ENABLED, &ar->flag);
271 ath6kl_init_profile_info(ar);
272 ar->def_txkey_index = 0;
273 memset(ar->wep_key_list, 0, sizeof(ar->wep_key_list));
274 ar->ch_hint = 0;
275 ar->listen_intvl_t = A_DEFAULT_LISTEN_INTERVAL;
276 ar->listen_intvl_b = 0;
277 ar->tx_pwr = 0;
278 clear_bit(SKIP_SCAN, &ar->flag);
279 set_bit(WMM_ENABLED, &ar->flag);
280 ar->intra_bss = 1;
281 memset(&ar->sc_params, 0, sizeof(ar->sc_params));
282 ar->sc_params.short_scan_ratio = WMI_SHORTSCANRATIO_DEFAULT;
283 ar->sc_params.scan_ctrl_flags = DEFAULT_SCAN_CTRL_FLAGS;
Vivek Natarajane5090442011-08-31 15:02:19 +0530284 ar->lrssi_roam_threshold = DEF_LRSSI_ROAM_THRESHOLD;
Kalle Valobdcd8172011-07-18 00:22:30 +0300285
286 memset((u8 *)ar->sta_list, 0,
287 AP_MAX_NUM_STA * sizeof(struct ath6kl_sta));
288
289 spin_lock_init(&ar->mcastpsq_lock);
290
291 /* Init the PS queues */
292 for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
293 spin_lock_init(&ar->sta_list[ctr].psq_lock);
294 skb_queue_head_init(&ar->sta_list[ctr].psq);
295 }
296
297 skb_queue_head_init(&ar->mcastpsq);
298
299 memcpy(ar->ap_country_code, DEF_AP_COUNTRY_CODE, 3);
300}
301
302/*
303 * Set HTC/Mbox operational parameters, this can only be called when the
304 * target is in the BMI phase.
305 */
306static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
307 u8 htc_ctrl_buf)
308{
309 int status;
310 u32 blk_size;
311
312 blk_size = ar->mbox_info.block_size;
313
314 if (htc_ctrl_buf)
315 blk_size |= ((u32)htc_ctrl_buf) << 16;
316
317 /* set the host interest area for the block size */
318 status = ath6kl_bmi_write(ar,
319 ath6kl_get_hi_item_addr(ar,
320 HI_ITEM(hi_mbox_io_block_sz)),
321 (u8 *)&blk_size,
322 4);
323 if (status) {
324 ath6kl_err("bmi_write_memory for IO block size failed\n");
325 goto out;
326 }
327
328 ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
329 blk_size,
330 ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
331
332 if (mbox_isr_yield_val) {
333 /* set the host interest area for the mbox ISR yield limit */
334 status = ath6kl_bmi_write(ar,
335 ath6kl_get_hi_item_addr(ar,
336 HI_ITEM(hi_mbox_isr_yield_limit)),
337 (u8 *)&mbox_isr_yield_val,
338 4);
339 if (status) {
340 ath6kl_err("bmi_write_memory for yield limit failed\n");
341 goto out;
342 }
343 }
344
345out:
346 return status;
347}
348
349#define REG_DUMP_COUNT_AR6003 60
350#define REGISTER_DUMP_LEN_MAX 60
351
352static void ath6kl_dump_target_assert_info(struct ath6kl *ar)
353{
354 u32 address;
355 u32 regdump_loc = 0;
356 int status;
357 u32 regdump_val[REGISTER_DUMP_LEN_MAX];
358 u32 i;
359
360 if (ar->target_type != TARGET_TYPE_AR6003)
361 return;
362
363 /* the reg dump pointer is copied to the host interest area */
364 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_failure_state));
Kevin Fang31024d92011-07-11 17:14:13 +0800365 address = TARG_VTOP(ar->target_type, address);
Kalle Valobdcd8172011-07-18 00:22:30 +0300366
367 /* read RAM location through diagnostic window */
Kalle Valoaddb44b2011-09-02 10:32:05 +0300368 status = ath6kl_diag_read32(ar, address, &regdump_loc);
Kalle Valobdcd8172011-07-18 00:22:30 +0300369
370 if (status || !regdump_loc) {
371 ath6kl_err("failed to get ptr to register dump area\n");
372 return;
373 }
374
375 ath6kl_dbg(ATH6KL_DBG_TRC, "location of register dump data: 0x%X\n",
376 regdump_loc);
Kevin Fang31024d92011-07-11 17:14:13 +0800377 regdump_loc = TARG_VTOP(ar->target_type, regdump_loc);
Kalle Valobdcd8172011-07-18 00:22:30 +0300378
379 /* fetch register dump data */
Kalle Valoaddb44b2011-09-02 10:32:05 +0300380 status = ath6kl_diag_read(ar, regdump_loc, (u8 *)&regdump_val[0],
381 REG_DUMP_COUNT_AR6003 * (sizeof(u32)));
Kalle Valobdcd8172011-07-18 00:22:30 +0300382
383 if (status) {
384 ath6kl_err("failed to get register dump\n");
385 return;
386 }
387 ath6kl_dbg(ATH6KL_DBG_TRC, "Register Dump:\n");
388
389 for (i = 0; i < REG_DUMP_COUNT_AR6003; i++)
390 ath6kl_dbg(ATH6KL_DBG_TRC, " %d : 0x%8.8X\n",
391 i, regdump_val[i]);
392
393}
394
395void ath6kl_target_failure(struct ath6kl *ar)
396{
397 ath6kl_err("target asserted\n");
398
399 /* try dumping target assertion information (if any) */
400 ath6kl_dump_target_assert_info(ar);
401
402}
403
404static int ath6kl_target_config_wlan_params(struct ath6kl *ar)
405{
406 int status = 0;
Jouni Malinen4dea08e2011-08-30 21:57:57 +0300407 int ret;
Kalle Valobdcd8172011-07-18 00:22:30 +0300408
409 /*
410 * Configure the device for rx dot11 header rules. "0,0" are the
411 * default values. Required if checksum offload is needed. Set
412 * RxMetaVersion to 2.
413 */
414 if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi,
415 ar->rx_meta_ver, 0, 0)) {
416 ath6kl_err("unable to set the rx frame format\n");
417 status = -EIO;
418 }
419
420 if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
421 if ((ath6kl_wmi_pmparams_cmd(ar->wmi, 0, 1, 0, 0, 1,
422 IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
423 ath6kl_err("unable to set power save fail event policy\n");
424 status = -EIO;
425 }
426
427 if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
428 if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, 0,
429 WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
430 ath6kl_err("unable to set barker preamble policy\n");
431 status = -EIO;
432 }
433
434 if (ath6kl_wmi_set_keepalive_cmd(ar->wmi,
435 WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
436 ath6kl_err("unable to set keep alive interval\n");
437 status = -EIO;
438 }
439
440 if (ath6kl_wmi_disctimeout_cmd(ar->wmi,
441 WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
442 ath6kl_err("unable to set disconnect timeout\n");
443 status = -EIO;
444 }
445
446 if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
447 if (ath6kl_wmi_set_wmm_txop(ar->wmi, WMI_TXOP_DISABLED)) {
448 ath6kl_err("unable to set txop bursting\n");
449 status = -EIO;
450 }
451
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300452 if (ar->p2p) {
453 ret = ath6kl_wmi_info_req_cmd(ar->wmi,
454 P2P_FLAG_CAPABILITIES_REQ |
455 P2P_FLAG_MACADDR_REQ |
456 P2P_FLAG_HMODEL_REQ);
457 if (ret) {
458 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
459 "capabilities (%d) - assuming P2P not "
460 "supported\n", ret);
461 ar->p2p = 0;
462 }
463 }
464
465 if (ar->p2p) {
466 /* Enable Probe Request reporting for P2P */
467 ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, true);
468 if (ret) {
469 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
470 "Request reporting (%d)\n", ret);
471 }
Jouni Malinen4dea08e2011-08-30 21:57:57 +0300472 }
473
Kalle Valobdcd8172011-07-18 00:22:30 +0300474 return status;
475}
476
477int ath6kl_configure_target(struct ath6kl *ar)
478{
479 u32 param, ram_reserved_size;
480 u8 fw_iftype;
481
482 fw_iftype = ath6kl_get_fw_iftype(ar);
483 if (fw_iftype == 0xff)
484 return -EINVAL;
485
486 /* Tell target which HTC version it is used*/
487 param = HTC_PROTOCOL_VERSION;
488 if (ath6kl_bmi_write(ar,
489 ath6kl_get_hi_item_addr(ar,
490 HI_ITEM(hi_app_host_interest)),
491 (u8 *)&param, 4) != 0) {
492 ath6kl_err("bmi_write_memory for htc version failed\n");
493 return -EIO;
494 }
495
496 /* set the firmware mode to STA/IBSS/AP */
497 param = 0;
498
499 if (ath6kl_bmi_read(ar,
500 ath6kl_get_hi_item_addr(ar,
501 HI_ITEM(hi_option_flag)),
502 (u8 *)&param, 4) != 0) {
503 ath6kl_err("bmi_read_memory for setting fwmode failed\n");
504 return -EIO;
505 }
506
507 param |= (1 << HI_OPTION_NUM_DEV_SHIFT);
508 param |= (fw_iftype << HI_OPTION_FW_MODE_SHIFT);
Jouni Malinen6bbc7c32011-09-05 17:38:47 +0300509 if (ar->p2p && fw_iftype == HI_OPTION_FW_MODE_BSS_STA) {
510 param |= HI_OPTION_FW_SUBMODE_P2PDEV <<
511 HI_OPTION_FW_SUBMODE_SHIFT;
512 }
Kalle Valobdcd8172011-07-18 00:22:30 +0300513 param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
514 param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
515
516 if (ath6kl_bmi_write(ar,
517 ath6kl_get_hi_item_addr(ar,
518 HI_ITEM(hi_option_flag)),
519 (u8 *)&param,
520 4) != 0) {
521 ath6kl_err("bmi_write_memory for setting fwmode failed\n");
522 return -EIO;
523 }
524
525 ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
526
527 /*
528 * Hardcode the address use for the extended board data
529 * Ideally this should be pre-allocate by the OS at boot time
530 * But since it is a new feature and board data is loaded
531 * at init time, we have to workaround this from host.
532 * It is difficult to patch the firmware boot code,
533 * but possible in theory.
534 */
535
Kevin Fang31024d92011-07-11 17:14:13 +0800536 if (ar->target_type == TARGET_TYPE_AR6003 ||
537 ar->target_type == TARGET_TYPE_AR6004) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300538 if (ar->version.target_ver == AR6003_REV2_VERSION) {
539 param = AR6003_REV2_BOARD_EXT_DATA_ADDRESS;
540 ram_reserved_size = AR6003_REV2_RAM_RESERVE_SIZE;
Kevin Fang31024d92011-07-11 17:14:13 +0800541 } else if (ar->version.target_ver == AR6004_REV1_VERSION) {
542 param = AR6004_REV1_BOARD_EXT_DATA_ADDRESS;
543 ram_reserved_size = AR6004_REV1_RAM_RESERVE_SIZE;
Kalle Valobdcd8172011-07-18 00:22:30 +0300544 } else {
545 param = AR6003_REV3_BOARD_EXT_DATA_ADDRESS;
546 ram_reserved_size = AR6003_REV3_RAM_RESERVE_SIZE;
547 }
548
549 if (ath6kl_bmi_write(ar,
550 ath6kl_get_hi_item_addr(ar,
551 HI_ITEM(hi_board_ext_data)),
552 (u8 *)&param, 4) != 0) {
553 ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
554 return -EIO;
555 }
556 if (ath6kl_bmi_write(ar,
557 ath6kl_get_hi_item_addr(ar,
558 HI_ITEM(hi_end_ram_reserve_sz)),
559 (u8 *)&ram_reserved_size, 4) != 0) {
560 ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
561 return -EIO;
562 }
563 }
564
565 /* set the block size for the target */
566 if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
567 /* use default number of control buffers */
568 return -EIO;
569
570 return 0;
571}
572
573struct ath6kl *ath6kl_core_alloc(struct device *sdev)
574{
575 struct net_device *dev;
576 struct ath6kl *ar;
577 struct wireless_dev *wdev;
578
579 wdev = ath6kl_cfg80211_init(sdev);
580 if (!wdev) {
581 ath6kl_err("ath6kl_cfg80211_init failed\n");
582 return NULL;
583 }
584
585 ar = wdev_priv(wdev);
586 ar->dev = sdev;
587 ar->wdev = wdev;
588 wdev->iftype = NL80211_IFTYPE_STATION;
589
Vasanthakumar Thiagarajand999ba32011-08-26 13:06:31 +0530590 if (ath6kl_debug_init(ar)) {
591 ath6kl_err("Failed to initialize debugfs\n");
592 ath6kl_cfg80211_deinit(ar);
593 return NULL;
594 }
595
Kalle Valobdcd8172011-07-18 00:22:30 +0300596 dev = alloc_netdev(0, "wlan%d", ether_setup);
597 if (!dev) {
598 ath6kl_err("no memory for network device instance\n");
599 ath6kl_cfg80211_deinit(ar);
600 return NULL;
601 }
602
603 dev->ieee80211_ptr = wdev;
604 SET_NETDEV_DEV(dev, wiphy_dev(wdev->wiphy));
605 wdev->netdev = dev;
606 ar->sme_state = SME_DISCONNECTED;
607 ar->auto_auth_stage = AUTH_IDLE;
608
609 init_netdev(dev);
610
611 ar->net_dev = dev;
Raja Mani575b5f32011-07-19 19:27:33 +0530612 set_bit(WLAN_ENABLED, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +0300613
614 ar->wlan_pwr_state = WLAN_POWER_STATE_ON;
615
616 spin_lock_init(&ar->lock);
617
618 ath6kl_init_control_info(ar);
619 init_waitqueue_head(&ar->event_wq);
620 sema_init(&ar->sem, 1);
621 clear_bit(DESTROY_IN_PROGRESS, &ar->flag);
622
623 INIT_LIST_HEAD(&ar->amsdu_rx_buffer_queue);
624
625 setup_timer(&ar->disconnect_timer, disconnect_timer_handler,
626 (unsigned long) dev);
627
628 return ar;
629}
630
631int ath6kl_unavail_ev(struct ath6kl *ar)
632{
633 ath6kl_destroy(ar->net_dev, 1);
634
635 return 0;
636}
637
638/* firmware upload */
639static u32 ath6kl_get_load_address(u32 target_ver, enum addr_type type)
640{
641 WARN_ON(target_ver != AR6003_REV2_VERSION &&
Kevin Fang31024d92011-07-11 17:14:13 +0800642 target_ver != AR6003_REV3_VERSION &&
643 target_ver != AR6004_REV1_VERSION);
Kalle Valobdcd8172011-07-18 00:22:30 +0300644
645 switch (type) {
646 case DATASET_PATCH_ADDR:
647 return (target_ver == AR6003_REV2_VERSION) ?
648 AR6003_REV2_DATASET_PATCH_ADDRESS :
649 AR6003_REV3_DATASET_PATCH_ADDRESS;
650 case APP_LOAD_ADDR:
651 return (target_ver == AR6003_REV2_VERSION) ?
652 AR6003_REV2_APP_LOAD_ADDRESS :
653 0x1234;
654 case APP_START_OVERRIDE_ADDR:
655 return (target_ver == AR6003_REV2_VERSION) ?
656 AR6003_REV2_APP_START_OVERRIDE :
657 AR6003_REV3_APP_START_OVERRIDE;
658 default:
659 return 0;
660 }
661}
662
663static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
664 u8 **fw, size_t *fw_len)
665{
666 const struct firmware *fw_entry;
667 int ret;
668
669 ret = request_firmware(&fw_entry, filename, ar->dev);
670 if (ret)
671 return ret;
672
673 *fw_len = fw_entry->size;
674 *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
675
676 if (*fw == NULL)
677 ret = -ENOMEM;
678
679 release_firmware(fw_entry);
680
681 return ret;
682}
683
Sam Leffler92ecbff2011-09-07 10:55:16 +0300684#ifdef CONFIG_OF
685static const char *get_target_ver_dir(const struct ath6kl *ar)
686{
687 switch (ar->version.target_ver) {
688 case AR6003_REV1_VERSION:
689 return "ath6k/AR6003/hw1.0";
690 case AR6003_REV2_VERSION:
691 return "ath6k/AR6003/hw2.0";
692 case AR6003_REV3_VERSION:
693 return "ath6k/AR6003/hw2.1.1";
694 }
695 ath6kl_warn("%s: unsupported target version 0x%x.\n", __func__,
696 ar->version.target_ver);
697 return NULL;
698}
699
700/*
701 * Check the device tree for a board-id and use it to construct
702 * the pathname to the firmware file. Used (for now) to find a
703 * fallback to the "bdata.bin" file--typically a symlink to the
704 * appropriate board-specific file.
705 */
706static bool check_device_tree(struct ath6kl *ar)
707{
708 static const char *board_id_prop = "atheros,board-id";
709 struct device_node *node;
710 char board_filename[64];
711 const char *board_id;
712 int ret;
713
714 for_each_compatible_node(node, NULL, "atheros,ath6kl") {
715 board_id = of_get_property(node, board_id_prop, NULL);
716 if (board_id == NULL) {
717 ath6kl_warn("No \"%s\" property on %s node.\n",
718 board_id_prop, node->name);
719 continue;
720 }
721 snprintf(board_filename, sizeof(board_filename),
722 "%s/bdata.%s.bin", get_target_ver_dir(ar), board_id);
723
724 ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
725 &ar->fw_board_len);
726 if (ret) {
727 ath6kl_err("Failed to get DT board file %s: %d\n",
728 board_filename, ret);
729 continue;
730 }
731 return true;
732 }
733 return false;
734}
735#else
736static bool check_device_tree(struct ath6kl *ar)
737{
738 return false;
739}
740#endif /* CONFIG_OF */
741
Kalle Valobdcd8172011-07-18 00:22:30 +0300742static int ath6kl_fetch_board_file(struct ath6kl *ar)
743{
744 const char *filename;
745 int ret;
746
747 switch (ar->version.target_ver) {
748 case AR6003_REV2_VERSION:
749 filename = AR6003_REV2_BOARD_DATA_FILE;
750 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800751 case AR6004_REV1_VERSION:
752 filename = AR6004_REV1_BOARD_DATA_FILE;
753 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300754 default:
755 filename = AR6003_REV3_BOARD_DATA_FILE;
756 break;
757 }
758
759 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
760 &ar->fw_board_len);
761 if (ret == 0) {
762 /* managed to get proper board file */
763 return 0;
764 }
765
Sam Leffler92ecbff2011-09-07 10:55:16 +0300766 if (check_device_tree(ar)) {
767 /* got board file from device tree */
768 return 0;
769 }
770
Kalle Valobdcd8172011-07-18 00:22:30 +0300771 /* there was no proper board file, try to use default instead */
772 ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
773 filename, ret);
774
775 switch (ar->version.target_ver) {
776 case AR6003_REV2_VERSION:
777 filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
778 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800779 case AR6004_REV1_VERSION:
780 filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
781 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300782 default:
783 filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
784 break;
785 }
786
787 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
788 &ar->fw_board_len);
789 if (ret) {
790 ath6kl_err("Failed to get default board file %s: %d\n",
791 filename, ret);
792 return ret;
793 }
794
795 ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
796 ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
797
798 return 0;
799}
800
801
802static int ath6kl_upload_board_file(struct ath6kl *ar)
803{
804 u32 board_address, board_ext_address, param;
Kevin Fang31024d92011-07-11 17:14:13 +0800805 u32 board_data_size, board_ext_data_size;
Kalle Valobdcd8172011-07-18 00:22:30 +0300806 int ret;
807
808 if (ar->fw_board == NULL) {
809 ret = ath6kl_fetch_board_file(ar);
810 if (ret)
811 return ret;
812 }
813
Kevin Fang31024d92011-07-11 17:14:13 +0800814 /*
815 * Determine where in Target RAM to write Board Data.
816 * For AR6004, host determine Target RAM address for
817 * writing board data.
818 */
819 if (ar->target_type == TARGET_TYPE_AR6004) {
820 board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
821 ath6kl_bmi_write(ar,
822 ath6kl_get_hi_item_addr(ar,
823 HI_ITEM(hi_board_data)),
824 (u8 *) &board_address, 4);
825 } else {
826 ath6kl_bmi_read(ar,
827 ath6kl_get_hi_item_addr(ar,
828 HI_ITEM(hi_board_data)),
829 (u8 *) &board_address, 4);
830 }
831
Kalle Valobdcd8172011-07-18 00:22:30 +0300832 ath6kl_dbg(ATH6KL_DBG_TRC, "board data download addr: 0x%x\n",
833 board_address);
834
835 /* determine where in target ram to write extended board data */
836 ath6kl_bmi_read(ar,
837 ath6kl_get_hi_item_addr(ar,
838 HI_ITEM(hi_board_ext_data)),
839 (u8 *) &board_ext_address, 4);
840
841 ath6kl_dbg(ATH6KL_DBG_TRC, "board file download addr: 0x%x\n",
842 board_ext_address);
843
844 if (board_ext_address == 0) {
845 ath6kl_err("Failed to get board file target address.\n");
846 return -EINVAL;
847 }
848
Kevin Fang31024d92011-07-11 17:14:13 +0800849 switch (ar->target_type) {
850 case TARGET_TYPE_AR6003:
851 board_data_size = AR6003_BOARD_DATA_SZ;
852 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
853 break;
854 case TARGET_TYPE_AR6004:
855 board_data_size = AR6004_BOARD_DATA_SZ;
856 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
857 break;
858 default:
859 WARN_ON(1);
860 return -EINVAL;
861 break;
862 }
863
864 if (ar->fw_board_len == (board_data_size +
865 board_ext_data_size)) {
866
Kalle Valobdcd8172011-07-18 00:22:30 +0300867 /* write extended board data */
868 ret = ath6kl_bmi_write(ar, board_ext_address,
Kevin Fang31024d92011-07-11 17:14:13 +0800869 ar->fw_board + board_data_size,
870 board_ext_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +0300871 if (ret) {
872 ath6kl_err("Failed to write extended board data: %d\n",
873 ret);
874 return ret;
875 }
876
877 /* record that extended board data is initialized */
Kevin Fang31024d92011-07-11 17:14:13 +0800878 param = (board_ext_data_size << 16) | 1;
879
Kalle Valobdcd8172011-07-18 00:22:30 +0300880 ath6kl_bmi_write(ar,
881 ath6kl_get_hi_item_addr(ar,
882 HI_ITEM(hi_board_ext_data_config)),
883 (unsigned char *) &param, 4);
884 }
885
Kevin Fang31024d92011-07-11 17:14:13 +0800886 if (ar->fw_board_len < board_data_size) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300887 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
888 ret = -EINVAL;
889 return ret;
890 }
891
892 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
Kevin Fang31024d92011-07-11 17:14:13 +0800893 board_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +0300894
895 if (ret) {
896 ath6kl_err("Board file bmi write failed: %d\n", ret);
897 return ret;
898 }
899
900 /* record the fact that Board Data IS initialized */
901 param = 1;
902 ath6kl_bmi_write(ar,
903 ath6kl_get_hi_item_addr(ar,
904 HI_ITEM(hi_board_data_initialized)),
905 (u8 *)&param, 4);
906
907 return ret;
908}
909
910static int ath6kl_upload_otp(struct ath6kl *ar)
911{
912 const char *filename;
913 u32 address, param;
914 int ret;
915
916 switch (ar->version.target_ver) {
917 case AR6003_REV2_VERSION:
918 filename = AR6003_REV2_OTP_FILE;
919 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800920 case AR6004_REV1_VERSION:
921 ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
922 return 0;
923 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300924 default:
925 filename = AR6003_REV3_OTP_FILE;
926 break;
927 }
928
929 if (ar->fw_otp == NULL) {
930 ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
931 &ar->fw_otp_len);
932 if (ret) {
933 ath6kl_err("Failed to get OTP file %s: %d\n",
934 filename, ret);
935 return ret;
936 }
937 }
938
939 address = ath6kl_get_load_address(ar->version.target_ver,
940 APP_LOAD_ADDR);
941
942 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
943 ar->fw_otp_len);
944 if (ret) {
945 ath6kl_err("Failed to upload OTP file: %d\n", ret);
946 return ret;
947 }
948
949 /* execute the OTP code */
950 param = 0;
951 address = ath6kl_get_load_address(ar->version.target_ver,
952 APP_START_OVERRIDE_ADDR);
953 ath6kl_bmi_execute(ar, address, &param);
954
955 return ret;
956}
957
958static int ath6kl_upload_firmware(struct ath6kl *ar)
959{
960 const char *filename;
961 u32 address;
962 int ret;
963
Kalle Valo003353b2011-09-01 10:14:21 +0300964 if (testmode) {
965 switch (ar->version.target_ver) {
966 case AR6003_REV2_VERSION:
967 filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
968 break;
969 case AR6003_REV3_VERSION:
970 filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
971 break;
972 case AR6004_REV1_VERSION:
973 ath6kl_warn("testmode not supported with ar6004\n");
974 return -EOPNOTSUPP;
975 default:
976 ath6kl_warn("unknown target version: 0x%x\n",
977 ar->version.target_ver);
978 return -EINVAL;
979 }
980
981 set_bit(TESTMODE, &ar->flag);
982
983 goto get_fw;
984 }
985
Kalle Valobdcd8172011-07-18 00:22:30 +0300986 switch (ar->version.target_ver) {
987 case AR6003_REV2_VERSION:
988 filename = AR6003_REV2_FIRMWARE_FILE;
989 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800990 case AR6004_REV1_VERSION:
991 filename = AR6004_REV1_FIRMWARE_FILE;
992 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300993 default:
994 filename = AR6003_REV3_FIRMWARE_FILE;
995 break;
996 }
997
Kalle Valo003353b2011-09-01 10:14:21 +0300998get_fw:
999
Kalle Valobdcd8172011-07-18 00:22:30 +03001000 if (ar->fw == NULL) {
1001 ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
1002 if (ret) {
1003 ath6kl_err("Failed to get firmware file %s: %d\n",
1004 filename, ret);
1005 return ret;
1006 }
1007 }
1008
1009 address = ath6kl_get_load_address(ar->version.target_ver,
1010 APP_LOAD_ADDR);
1011
1012 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1013
1014 if (ret) {
1015 ath6kl_err("Failed to write firmware: %d\n", ret);
1016 return ret;
1017 }
1018
Kevin Fang31024d92011-07-11 17:14:13 +08001019 /*
1020 * Set starting address for firmware
1021 * Don't need to setup app_start override addr on AR6004
1022 */
1023 if (ar->target_type != TARGET_TYPE_AR6004) {
1024 address = ath6kl_get_load_address(ar->version.target_ver,
1025 APP_START_OVERRIDE_ADDR);
1026 ath6kl_bmi_set_app_start(ar, address);
1027 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001028 return ret;
1029}
1030
1031static int ath6kl_upload_patch(struct ath6kl *ar)
1032{
1033 const char *filename;
1034 u32 address, param;
1035 int ret;
1036
1037 switch (ar->version.target_ver) {
1038 case AR6003_REV2_VERSION:
1039 filename = AR6003_REV2_PATCH_FILE;
1040 break;
Kevin Fang31024d92011-07-11 17:14:13 +08001041 case AR6004_REV1_VERSION:
1042 /* FIXME: implement for AR6004 */
1043 return 0;
1044 break;
Kalle Valobdcd8172011-07-18 00:22:30 +03001045 default:
1046 filename = AR6003_REV3_PATCH_FILE;
1047 break;
1048 }
1049
1050 if (ar->fw_patch == NULL) {
1051 ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
1052 &ar->fw_patch_len);
1053 if (ret) {
1054 ath6kl_err("Failed to get patch file %s: %d\n",
1055 filename, ret);
1056 return ret;
1057 }
1058 }
1059
1060 address = ath6kl_get_load_address(ar->version.target_ver,
1061 DATASET_PATCH_ADDR);
1062
1063 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1064 if (ret) {
1065 ath6kl_err("Failed to write patch file: %d\n", ret);
1066 return ret;
1067 }
1068
1069 param = address;
1070 ath6kl_bmi_write(ar,
1071 ath6kl_get_hi_item_addr(ar,
1072 HI_ITEM(hi_dset_list_head)),
1073 (unsigned char *) &param, 4);
1074
1075 return 0;
1076}
1077
1078static int ath6kl_init_upload(struct ath6kl *ar)
1079{
1080 u32 param, options, sleep, address;
1081 int status = 0;
1082
Kevin Fang31024d92011-07-11 17:14:13 +08001083 if (ar->target_type != TARGET_TYPE_AR6003 &&
1084 ar->target_type != TARGET_TYPE_AR6004)
Kalle Valobdcd8172011-07-18 00:22:30 +03001085 return -EINVAL;
1086
1087 /* temporarily disable system sleep */
1088 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1089 status = ath6kl_bmi_reg_read(ar, address, &param);
1090 if (status)
1091 return status;
1092
1093 options = param;
1094
1095 param |= ATH6KL_OPTION_SLEEP_DISABLE;
1096 status = ath6kl_bmi_reg_write(ar, address, param);
1097 if (status)
1098 return status;
1099
1100 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1101 status = ath6kl_bmi_reg_read(ar, address, &param);
1102 if (status)
1103 return status;
1104
1105 sleep = param;
1106
1107 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1108 status = ath6kl_bmi_reg_write(ar, address, param);
1109 if (status)
1110 return status;
1111
1112 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1113 options, sleep);
1114
1115 /* program analog PLL register */
Kevin Fang31024d92011-07-11 17:14:13 +08001116 /* no need to control 40/44MHz clock on AR6004 */
1117 if (ar->target_type != TARGET_TYPE_AR6004) {
1118 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1119 0xF9104001);
Kalle Valobdcd8172011-07-18 00:22:30 +03001120
Kevin Fang31024d92011-07-11 17:14:13 +08001121 if (status)
1122 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +03001123
Kevin Fang31024d92011-07-11 17:14:13 +08001124 /* Run at 80/88MHz by default */
1125 param = SM(CPU_CLOCK_STANDARD, 1);
1126
1127 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1128 status = ath6kl_bmi_reg_write(ar, address, param);
1129 if (status)
1130 return status;
1131 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001132
1133 param = 0;
1134 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1135 param = SM(LPO_CAL_ENABLE, 1);
1136 status = ath6kl_bmi_reg_write(ar, address, param);
1137 if (status)
1138 return status;
1139
1140 /* WAR to avoid SDIO CRC err */
1141 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1142 ath6kl_err("temporary war to avoid sdio crc error\n");
1143
1144 param = 0x20;
1145
1146 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1147 status = ath6kl_bmi_reg_write(ar, address, param);
1148 if (status)
1149 return status;
1150
1151 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1152 status = ath6kl_bmi_reg_write(ar, address, param);
1153 if (status)
1154 return status;
1155
1156 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1157 status = ath6kl_bmi_reg_write(ar, address, param);
1158 if (status)
1159 return status;
1160
1161 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1162 status = ath6kl_bmi_reg_write(ar, address, param);
1163 if (status)
1164 return status;
1165 }
1166
1167 /* write EEPROM data to Target RAM */
1168 status = ath6kl_upload_board_file(ar);
1169 if (status)
1170 return status;
1171
1172 /* transfer One time Programmable data */
1173 status = ath6kl_upload_otp(ar);
1174 if (status)
1175 return status;
1176
1177 /* Download Target firmware */
1178 status = ath6kl_upload_firmware(ar);
1179 if (status)
1180 return status;
1181
1182 status = ath6kl_upload_patch(ar);
1183 if (status)
1184 return status;
1185
1186 /* Restore system sleep */
1187 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1188 status = ath6kl_bmi_reg_write(ar, address, sleep);
1189 if (status)
1190 return status;
1191
1192 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1193 param = options | 0x20;
1194 status = ath6kl_bmi_reg_write(ar, address, param);
1195 if (status)
1196 return status;
1197
1198 /* Configure GPIO AR6003 UART */
1199 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1200 status = ath6kl_bmi_write(ar,
1201 ath6kl_get_hi_item_addr(ar,
1202 HI_ITEM(hi_dbg_uart_txpin)),
1203 (u8 *)&param, 4);
1204
1205 return status;
1206}
1207
1208static int ath6kl_init(struct net_device *dev)
1209{
1210 struct ath6kl *ar = ath6kl_priv(dev);
1211 int status = 0;
1212 s32 timeleft;
1213
1214 if (!ar)
1215 return -EIO;
1216
1217 /* Do we need to finish the BMI phase */
1218 if (ath6kl_bmi_done(ar)) {
1219 status = -EIO;
1220 goto ath6kl_init_done;
1221 }
1222
1223 /* Indicate that WMI is enabled (although not ready yet) */
1224 set_bit(WMI_ENABLED, &ar->flag);
Vasanthakumar Thiagarajan28657852011-07-21 12:00:49 +05301225 ar->wmi = ath6kl_wmi_init(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001226 if (!ar->wmi) {
1227 ath6kl_err("failed to initialize wmi\n");
1228 status = -EIO;
1229 goto ath6kl_init_done;
1230 }
1231
1232 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1233
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301234 wlan_node_table_init(&ar->scan_table);
1235
Kalle Valobdcd8172011-07-18 00:22:30 +03001236 /*
1237 * The reason we have to wait for the target here is that the
1238 * driver layer has to init BMI in order to set the host block
1239 * size.
1240 */
Kalle Vaload226ec2011-08-10 09:49:12 +03001241 if (ath6kl_htc_wait_target(ar->htc_target)) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001242 status = -EIO;
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301243 goto err_node_cleanup;
Kalle Valobdcd8172011-07-18 00:22:30 +03001244 }
1245
1246 if (ath6kl_init_service_ep(ar)) {
1247 status = -EIO;
1248 goto err_cleanup_scatter;
1249 }
1250
1251 /* setup access class priority mappings */
1252 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1253 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1254 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1255 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1256
1257 /* give our connected endpoints some buffers */
1258 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1259 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1260
1261 /* allocate some buffers that handle larger AMSDU frames */
1262 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1263
1264 /* setup credit distribution */
1265 ath6k_setup_credit_dist(ar->htc_target, &ar->credit_state_info);
1266
1267 ath6kl_cookie_init(ar);
1268
1269 /* start HTC */
Kalle Vaload226ec2011-08-10 09:49:12 +03001270 status = ath6kl_htc_start(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001271
1272 if (status) {
1273 ath6kl_cookie_cleanup(ar);
1274 goto err_rxbuf_cleanup;
1275 }
1276
1277 /* Wait for Wmi event to be ready */
1278 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1279 test_bit(WMI_READY,
1280 &ar->flag),
1281 WMI_TIMEOUT);
1282
1283 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1284 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1285 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1286 status = -EIO;
1287 goto err_htc_stop;
1288 }
1289
1290 if (!timeleft || signal_pending(current)) {
1291 ath6kl_err("wmi is not ready or wait was interrupted\n");
1292 status = -EIO;
1293 goto err_htc_stop;
1294 }
1295
1296 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1297
1298 /* communicate the wmi protocol verision to the target */
1299 if ((ath6kl_set_host_app_area(ar)) != 0)
1300 ath6kl_err("unable to set the host app area\n");
1301
1302 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1303 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1304
1305 status = ath6kl_target_config_wlan_params(ar);
1306 if (!status)
1307 goto ath6kl_init_done;
1308
1309err_htc_stop:
Kalle Vaload226ec2011-08-10 09:49:12 +03001310 ath6kl_htc_stop(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001311err_rxbuf_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001312 ath6kl_htc_flush_rx_buf(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001313 ath6kl_cleanup_amsdu_rxbufs(ar);
1314err_cleanup_scatter:
1315 ath6kl_hif_cleanup_scatter(ar);
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301316err_node_cleanup:
1317 wlan_node_table_cleanup(&ar->scan_table);
Kalle Valobdcd8172011-07-18 00:22:30 +03001318 ath6kl_wmi_shutdown(ar->wmi);
1319 clear_bit(WMI_ENABLED, &ar->flag);
1320 ar->wmi = NULL;
1321
1322ath6kl_init_done:
1323 return status;
1324}
1325
1326int ath6kl_core_init(struct ath6kl *ar)
1327{
1328 int ret = 0;
1329 struct ath6kl_bmi_target_info targ_info;
1330
1331 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1332 if (!ar->ath6kl_wq)
1333 return -ENOMEM;
1334
1335 ret = ath6kl_bmi_init(ar);
1336 if (ret)
1337 goto err_wq;
1338
1339 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1340 if (ret)
1341 goto err_bmi_cleanup;
1342
1343 ar->version.target_ver = le32_to_cpu(targ_info.version);
1344 ar->target_type = le32_to_cpu(targ_info.type);
1345 ar->wdev->wiphy->hw_version = le32_to_cpu(targ_info.version);
1346
1347 ret = ath6kl_configure_target(ar);
1348 if (ret)
1349 goto err_bmi_cleanup;
1350
Kalle Vaload226ec2011-08-10 09:49:12 +03001351 ar->htc_target = ath6kl_htc_create(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001352
1353 if (!ar->htc_target) {
1354 ret = -ENOMEM;
1355 goto err_bmi_cleanup;
1356 }
1357
1358 ar->aggr_cntxt = aggr_init(ar->net_dev);
1359 if (!ar->aggr_cntxt) {
1360 ath6kl_err("failed to initialize aggr\n");
1361 ret = -ENOMEM;
1362 goto err_htc_cleanup;
1363 }
1364
1365 ret = ath6kl_init_upload(ar);
1366 if (ret)
1367 goto err_htc_cleanup;
1368
1369 ret = ath6kl_init(ar->net_dev);
1370 if (ret)
1371 goto err_htc_cleanup;
1372
1373 /* This runs the init function if registered */
1374 ret = register_netdev(ar->net_dev);
1375 if (ret) {
1376 ath6kl_err("register_netdev failed\n");
1377 ath6kl_destroy(ar->net_dev, 0);
1378 return ret;
1379 }
1380
1381 set_bit(NETDEV_REGISTERED, &ar->flag);
1382
1383 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1384 __func__, ar->net_dev->name, ar->net_dev, ar);
1385
1386 return ret;
1387
1388err_htc_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001389 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001390err_bmi_cleanup:
1391 ath6kl_bmi_cleanup(ar);
1392err_wq:
1393 destroy_workqueue(ar->ath6kl_wq);
1394 return ret;
1395}
1396
1397void ath6kl_stop_txrx(struct ath6kl *ar)
1398{
1399 struct net_device *ndev = ar->net_dev;
1400
1401 if (!ndev)
1402 return;
1403
1404 set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1405
1406 if (down_interruptible(&ar->sem)) {
1407 ath6kl_err("down_interruptible failed\n");
1408 return;
1409 }
1410
1411 if (ar->wlan_pwr_state != WLAN_POWER_STATE_CUT_PWR)
1412 ath6kl_stop_endpoint(ndev, false, true);
1413
Raja Mani575b5f32011-07-19 19:27:33 +05301414 clear_bit(WLAN_ENABLED, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001415}
1416
1417/*
1418 * We need to differentiate between the surprise and planned removal of the
1419 * device because of the following consideration:
1420 *
1421 * - In case of surprise removal, the hcd already frees up the pending
1422 * for the device and hence there is no need to unregister the function
1423 * driver inorder to get these requests. For planned removal, the function
1424 * driver has to explicitly unregister itself to have the hcd return all the
1425 * pending requests before the data structures for the devices are freed up.
1426 * Note that as per the current implementation, the function driver will
1427 * end up releasing all the devices since there is no API to selectively
1428 * release a particular device.
1429 *
1430 * - Certain commands issued to the target can be skipped for surprise
1431 * removal since they will anyway not go through.
1432 */
1433void ath6kl_destroy(struct net_device *dev, unsigned int unregister)
1434{
1435 struct ath6kl *ar;
1436
1437 if (!dev || !ath6kl_priv(dev)) {
1438 ath6kl_err("failed to get device structure\n");
1439 return;
1440 }
1441
1442 ar = ath6kl_priv(dev);
1443
1444 destroy_workqueue(ar->ath6kl_wq);
1445
1446 if (ar->htc_target)
Kalle Vaload226ec2011-08-10 09:49:12 +03001447 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001448
1449 aggr_module_destroy(ar->aggr_cntxt);
1450
1451 ath6kl_cookie_cleanup(ar);
1452
1453 ath6kl_cleanup_amsdu_rxbufs(ar);
1454
1455 ath6kl_bmi_cleanup(ar);
1456
Kalle Valobdf53962011-09-02 10:32:04 +03001457 ath6kl_debug_cleanup(ar);
1458
Kalle Valobdcd8172011-07-18 00:22:30 +03001459 if (unregister && test_bit(NETDEV_REGISTERED, &ar->flag)) {
1460 unregister_netdev(dev);
1461 clear_bit(NETDEV_REGISTERED, &ar->flag);
1462 }
1463
1464 free_netdev(dev);
1465
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301466 wlan_node_table_cleanup(&ar->scan_table);
1467
Raja Mani19703572011-08-04 19:26:30 +05301468 kfree(ar->fw_board);
1469 kfree(ar->fw_otp);
1470 kfree(ar->fw);
1471 kfree(ar->fw_patch);
1472
Kalle Valobdcd8172011-07-18 00:22:30 +03001473 ath6kl_cfg80211_deinit(ar);
1474}