blob: 4055947ffd67ebdd28aa1e2dfdd3d741ac41d670 [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
Kalle Valo772c31e2011-09-07 10:55:16 +0300747 if (ar->fw_board != NULL)
748 return 0;
749
Kalle Valobdcd8172011-07-18 00:22:30 +0300750 switch (ar->version.target_ver) {
751 case AR6003_REV2_VERSION:
752 filename = AR6003_REV2_BOARD_DATA_FILE;
753 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800754 case AR6004_REV1_VERSION:
755 filename = AR6004_REV1_BOARD_DATA_FILE;
756 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300757 default:
758 filename = AR6003_REV3_BOARD_DATA_FILE;
759 break;
760 }
761
762 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
763 &ar->fw_board_len);
764 if (ret == 0) {
765 /* managed to get proper board file */
766 return 0;
767 }
768
Sam Leffler92ecbff2011-09-07 10:55:16 +0300769 if (check_device_tree(ar)) {
770 /* got board file from device tree */
771 return 0;
772 }
773
Kalle Valobdcd8172011-07-18 00:22:30 +0300774 /* there was no proper board file, try to use default instead */
775 ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
776 filename, ret);
777
778 switch (ar->version.target_ver) {
779 case AR6003_REV2_VERSION:
780 filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
781 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800782 case AR6004_REV1_VERSION:
783 filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
784 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300785 default:
786 filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
787 break;
788 }
789
790 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
791 &ar->fw_board_len);
792 if (ret) {
793 ath6kl_err("Failed to get default board file %s: %d\n",
794 filename, ret);
795 return ret;
796 }
797
798 ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
799 ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
800
801 return 0;
802}
803
Kalle Valo772c31e2011-09-07 10:55:16 +0300804static int ath6kl_fetch_otp_file(struct ath6kl *ar)
805{
806 const char *filename;
807 int ret;
808
809 if (ar->fw_otp != NULL)
810 return 0;
811
812 switch (ar->version.target_ver) {
813 case AR6003_REV2_VERSION:
814 filename = AR6003_REV2_OTP_FILE;
815 break;
816 case AR6004_REV1_VERSION:
817 ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
818 return 0;
819 break;
820 default:
821 filename = AR6003_REV3_OTP_FILE;
822 break;
823 }
824
825 ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
826 &ar->fw_otp_len);
827 if (ret) {
828 ath6kl_err("Failed to get OTP file %s: %d\n",
829 filename, ret);
830 return ret;
831 }
832
833 return 0;
834}
835
836static int ath6kl_fetch_fw_file(struct ath6kl *ar)
837{
838 const char *filename;
839 int ret;
840
841 if (ar->fw != NULL)
842 return 0;
843
844 if (testmode) {
845 switch (ar->version.target_ver) {
846 case AR6003_REV2_VERSION:
847 filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
848 break;
849 case AR6003_REV3_VERSION:
850 filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
851 break;
852 case AR6004_REV1_VERSION:
853 ath6kl_warn("testmode not supported with ar6004\n");
854 return -EOPNOTSUPP;
855 default:
856 ath6kl_warn("unknown target version: 0x%x\n",
857 ar->version.target_ver);
858 return -EINVAL;
859 }
860
861 set_bit(TESTMODE, &ar->flag);
862
863 goto get_fw;
864 }
865
866 switch (ar->version.target_ver) {
867 case AR6003_REV2_VERSION:
868 filename = AR6003_REV2_FIRMWARE_FILE;
869 break;
870 case AR6004_REV1_VERSION:
871 filename = AR6004_REV1_FIRMWARE_FILE;
872 break;
873 default:
874 filename = AR6003_REV3_FIRMWARE_FILE;
875 break;
876 }
877
878get_fw:
879 ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
880 if (ret) {
881 ath6kl_err("Failed to get firmware file %s: %d\n",
882 filename, ret);
883 return ret;
884 }
885
886 return 0;
887}
888
889static int ath6kl_fetch_patch_file(struct ath6kl *ar)
890{
891 const char *filename;
892 int ret;
893
894 switch (ar->version.target_ver) {
895 case AR6003_REV2_VERSION:
896 filename = AR6003_REV2_PATCH_FILE;
897 break;
898 case AR6004_REV1_VERSION:
899 /* FIXME: implement for AR6004 */
900 return 0;
901 break;
902 default:
903 filename = AR6003_REV3_PATCH_FILE;
904 break;
905 }
906
907 if (ar->fw_patch == NULL) {
908 ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
909 &ar->fw_patch_len);
910 if (ret) {
911 ath6kl_err("Failed to get patch file %s: %d\n",
912 filename, ret);
913 return ret;
914 }
915 }
916
917 return 0;
918}
919
920static int ath6kl_fetch_firmwares(struct ath6kl *ar)
921{
922 int ret;
923
924 ret = ath6kl_fetch_board_file(ar);
925 if (ret)
926 return ret;
927
928 ret = ath6kl_fetch_otp_file(ar);
929 if (ret)
930 return ret;
931
932 ret = ath6kl_fetch_fw_file(ar);
933 if (ret)
934 return ret;
935
936 ret = ath6kl_fetch_patch_file(ar);
937 if (ret)
938 return ret;
939
940 return 0;
941}
Kalle Valobdcd8172011-07-18 00:22:30 +0300942
943static int ath6kl_upload_board_file(struct ath6kl *ar)
944{
945 u32 board_address, board_ext_address, param;
Kevin Fang31024d92011-07-11 17:14:13 +0800946 u32 board_data_size, board_ext_data_size;
Kalle Valobdcd8172011-07-18 00:22:30 +0300947 int ret;
948
Kalle Valo772c31e2011-09-07 10:55:16 +0300949 if (WARN_ON(ar->fw_board == NULL))
950 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +0300951
Kevin Fang31024d92011-07-11 17:14:13 +0800952 /*
953 * Determine where in Target RAM to write Board Data.
954 * For AR6004, host determine Target RAM address for
955 * writing board data.
956 */
957 if (ar->target_type == TARGET_TYPE_AR6004) {
958 board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
959 ath6kl_bmi_write(ar,
960 ath6kl_get_hi_item_addr(ar,
961 HI_ITEM(hi_board_data)),
962 (u8 *) &board_address, 4);
963 } else {
964 ath6kl_bmi_read(ar,
965 ath6kl_get_hi_item_addr(ar,
966 HI_ITEM(hi_board_data)),
967 (u8 *) &board_address, 4);
968 }
969
Kalle Valobdcd8172011-07-18 00:22:30 +0300970 ath6kl_dbg(ATH6KL_DBG_TRC, "board data download addr: 0x%x\n",
971 board_address);
972
973 /* determine where in target ram to write extended board data */
974 ath6kl_bmi_read(ar,
975 ath6kl_get_hi_item_addr(ar,
976 HI_ITEM(hi_board_ext_data)),
977 (u8 *) &board_ext_address, 4);
978
979 ath6kl_dbg(ATH6KL_DBG_TRC, "board file download addr: 0x%x\n",
980 board_ext_address);
981
982 if (board_ext_address == 0) {
983 ath6kl_err("Failed to get board file target address.\n");
984 return -EINVAL;
985 }
986
Kevin Fang31024d92011-07-11 17:14:13 +0800987 switch (ar->target_type) {
988 case TARGET_TYPE_AR6003:
989 board_data_size = AR6003_BOARD_DATA_SZ;
990 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
991 break;
992 case TARGET_TYPE_AR6004:
993 board_data_size = AR6004_BOARD_DATA_SZ;
994 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
995 break;
996 default:
997 WARN_ON(1);
998 return -EINVAL;
999 break;
1000 }
1001
1002 if (ar->fw_board_len == (board_data_size +
1003 board_ext_data_size)) {
1004
Kalle Valobdcd8172011-07-18 00:22:30 +03001005 /* write extended board data */
1006 ret = ath6kl_bmi_write(ar, board_ext_address,
Kevin Fang31024d92011-07-11 17:14:13 +08001007 ar->fw_board + board_data_size,
1008 board_ext_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001009 if (ret) {
1010 ath6kl_err("Failed to write extended board data: %d\n",
1011 ret);
1012 return ret;
1013 }
1014
1015 /* record that extended board data is initialized */
Kevin Fang31024d92011-07-11 17:14:13 +08001016 param = (board_ext_data_size << 16) | 1;
1017
Kalle Valobdcd8172011-07-18 00:22:30 +03001018 ath6kl_bmi_write(ar,
1019 ath6kl_get_hi_item_addr(ar,
1020 HI_ITEM(hi_board_ext_data_config)),
1021 (unsigned char *) &param, 4);
1022 }
1023
Kevin Fang31024d92011-07-11 17:14:13 +08001024 if (ar->fw_board_len < board_data_size) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001025 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1026 ret = -EINVAL;
1027 return ret;
1028 }
1029
1030 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
Kevin Fang31024d92011-07-11 17:14:13 +08001031 board_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001032
1033 if (ret) {
1034 ath6kl_err("Board file bmi write failed: %d\n", ret);
1035 return ret;
1036 }
1037
1038 /* record the fact that Board Data IS initialized */
1039 param = 1;
1040 ath6kl_bmi_write(ar,
1041 ath6kl_get_hi_item_addr(ar,
1042 HI_ITEM(hi_board_data_initialized)),
1043 (u8 *)&param, 4);
1044
1045 return ret;
1046}
1047
1048static int ath6kl_upload_otp(struct ath6kl *ar)
1049{
Kalle Valobdcd8172011-07-18 00:22:30 +03001050 u32 address, param;
1051 int ret;
1052
Kalle Valo772c31e2011-09-07 10:55:16 +03001053 if (WARN_ON(ar->fw_otp == NULL))
1054 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001055
1056 address = ath6kl_get_load_address(ar->version.target_ver,
1057 APP_LOAD_ADDR);
1058
1059 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1060 ar->fw_otp_len);
1061 if (ret) {
1062 ath6kl_err("Failed to upload OTP file: %d\n", ret);
1063 return ret;
1064 }
1065
1066 /* execute the OTP code */
1067 param = 0;
1068 address = ath6kl_get_load_address(ar->version.target_ver,
1069 APP_START_OVERRIDE_ADDR);
1070 ath6kl_bmi_execute(ar, address, &param);
1071
1072 return ret;
1073}
1074
1075static int ath6kl_upload_firmware(struct ath6kl *ar)
1076{
Kalle Valobdcd8172011-07-18 00:22:30 +03001077 u32 address;
1078 int ret;
1079
Kalle Valo772c31e2011-09-07 10:55:16 +03001080 if (WARN_ON(ar->fw == NULL))
1081 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001082
1083 address = ath6kl_get_load_address(ar->version.target_ver,
1084 APP_LOAD_ADDR);
1085
1086 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1087
1088 if (ret) {
1089 ath6kl_err("Failed to write firmware: %d\n", ret);
1090 return ret;
1091 }
1092
Kevin Fang31024d92011-07-11 17:14:13 +08001093 /*
1094 * Set starting address for firmware
1095 * Don't need to setup app_start override addr on AR6004
1096 */
1097 if (ar->target_type != TARGET_TYPE_AR6004) {
1098 address = ath6kl_get_load_address(ar->version.target_ver,
1099 APP_START_OVERRIDE_ADDR);
1100 ath6kl_bmi_set_app_start(ar, address);
1101 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001102 return ret;
1103}
1104
1105static int ath6kl_upload_patch(struct ath6kl *ar)
1106{
Kalle Valobdcd8172011-07-18 00:22:30 +03001107 u32 address, param;
1108 int ret;
1109
Kalle Valo772c31e2011-09-07 10:55:16 +03001110 if (WARN_ON(ar->fw_patch == NULL))
1111 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001112
1113 address = ath6kl_get_load_address(ar->version.target_ver,
1114 DATASET_PATCH_ADDR);
1115
1116 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1117 if (ret) {
1118 ath6kl_err("Failed to write patch file: %d\n", ret);
1119 return ret;
1120 }
1121
1122 param = address;
1123 ath6kl_bmi_write(ar,
1124 ath6kl_get_hi_item_addr(ar,
1125 HI_ITEM(hi_dset_list_head)),
1126 (unsigned char *) &param, 4);
1127
1128 return 0;
1129}
1130
1131static int ath6kl_init_upload(struct ath6kl *ar)
1132{
1133 u32 param, options, sleep, address;
1134 int status = 0;
1135
Kevin Fang31024d92011-07-11 17:14:13 +08001136 if (ar->target_type != TARGET_TYPE_AR6003 &&
1137 ar->target_type != TARGET_TYPE_AR6004)
Kalle Valobdcd8172011-07-18 00:22:30 +03001138 return -EINVAL;
1139
1140 /* temporarily disable system sleep */
1141 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1142 status = ath6kl_bmi_reg_read(ar, address, &param);
1143 if (status)
1144 return status;
1145
1146 options = param;
1147
1148 param |= ATH6KL_OPTION_SLEEP_DISABLE;
1149 status = ath6kl_bmi_reg_write(ar, address, param);
1150 if (status)
1151 return status;
1152
1153 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1154 status = ath6kl_bmi_reg_read(ar, address, &param);
1155 if (status)
1156 return status;
1157
1158 sleep = param;
1159
1160 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1161 status = ath6kl_bmi_reg_write(ar, address, param);
1162 if (status)
1163 return status;
1164
1165 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1166 options, sleep);
1167
1168 /* program analog PLL register */
Kevin Fang31024d92011-07-11 17:14:13 +08001169 /* no need to control 40/44MHz clock on AR6004 */
1170 if (ar->target_type != TARGET_TYPE_AR6004) {
1171 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1172 0xF9104001);
Kalle Valobdcd8172011-07-18 00:22:30 +03001173
Kevin Fang31024d92011-07-11 17:14:13 +08001174 if (status)
1175 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +03001176
Kevin Fang31024d92011-07-11 17:14:13 +08001177 /* Run at 80/88MHz by default */
1178 param = SM(CPU_CLOCK_STANDARD, 1);
1179
1180 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1181 status = ath6kl_bmi_reg_write(ar, address, param);
1182 if (status)
1183 return status;
1184 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001185
1186 param = 0;
1187 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1188 param = SM(LPO_CAL_ENABLE, 1);
1189 status = ath6kl_bmi_reg_write(ar, address, param);
1190 if (status)
1191 return status;
1192
1193 /* WAR to avoid SDIO CRC err */
1194 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1195 ath6kl_err("temporary war to avoid sdio crc error\n");
1196
1197 param = 0x20;
1198
1199 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1200 status = ath6kl_bmi_reg_write(ar, address, param);
1201 if (status)
1202 return status;
1203
1204 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1205 status = ath6kl_bmi_reg_write(ar, address, param);
1206 if (status)
1207 return status;
1208
1209 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1210 status = ath6kl_bmi_reg_write(ar, address, param);
1211 if (status)
1212 return status;
1213
1214 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1215 status = ath6kl_bmi_reg_write(ar, address, param);
1216 if (status)
1217 return status;
1218 }
1219
1220 /* write EEPROM data to Target RAM */
1221 status = ath6kl_upload_board_file(ar);
1222 if (status)
1223 return status;
1224
1225 /* transfer One time Programmable data */
1226 status = ath6kl_upload_otp(ar);
1227 if (status)
1228 return status;
1229
1230 /* Download Target firmware */
1231 status = ath6kl_upload_firmware(ar);
1232 if (status)
1233 return status;
1234
1235 status = ath6kl_upload_patch(ar);
1236 if (status)
1237 return status;
1238
1239 /* Restore system sleep */
1240 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1241 status = ath6kl_bmi_reg_write(ar, address, sleep);
1242 if (status)
1243 return status;
1244
1245 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1246 param = options | 0x20;
1247 status = ath6kl_bmi_reg_write(ar, address, param);
1248 if (status)
1249 return status;
1250
1251 /* Configure GPIO AR6003 UART */
1252 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1253 status = ath6kl_bmi_write(ar,
1254 ath6kl_get_hi_item_addr(ar,
1255 HI_ITEM(hi_dbg_uart_txpin)),
1256 (u8 *)&param, 4);
1257
1258 return status;
1259}
1260
1261static int ath6kl_init(struct net_device *dev)
1262{
1263 struct ath6kl *ar = ath6kl_priv(dev);
1264 int status = 0;
1265 s32 timeleft;
1266
1267 if (!ar)
1268 return -EIO;
1269
1270 /* Do we need to finish the BMI phase */
1271 if (ath6kl_bmi_done(ar)) {
1272 status = -EIO;
1273 goto ath6kl_init_done;
1274 }
1275
1276 /* Indicate that WMI is enabled (although not ready yet) */
1277 set_bit(WMI_ENABLED, &ar->flag);
Vasanthakumar Thiagarajan28657852011-07-21 12:00:49 +05301278 ar->wmi = ath6kl_wmi_init(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001279 if (!ar->wmi) {
1280 ath6kl_err("failed to initialize wmi\n");
1281 status = -EIO;
1282 goto ath6kl_init_done;
1283 }
1284
1285 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1286
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301287 wlan_node_table_init(&ar->scan_table);
1288
Kalle Valobdcd8172011-07-18 00:22:30 +03001289 /*
1290 * The reason we have to wait for the target here is that the
1291 * driver layer has to init BMI in order to set the host block
1292 * size.
1293 */
Kalle Vaload226ec2011-08-10 09:49:12 +03001294 if (ath6kl_htc_wait_target(ar->htc_target)) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001295 status = -EIO;
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301296 goto err_node_cleanup;
Kalle Valobdcd8172011-07-18 00:22:30 +03001297 }
1298
1299 if (ath6kl_init_service_ep(ar)) {
1300 status = -EIO;
1301 goto err_cleanup_scatter;
1302 }
1303
1304 /* setup access class priority mappings */
1305 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1306 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1307 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1308 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1309
1310 /* give our connected endpoints some buffers */
1311 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1312 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1313
1314 /* allocate some buffers that handle larger AMSDU frames */
1315 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1316
1317 /* setup credit distribution */
1318 ath6k_setup_credit_dist(ar->htc_target, &ar->credit_state_info);
1319
1320 ath6kl_cookie_init(ar);
1321
1322 /* start HTC */
Kalle Vaload226ec2011-08-10 09:49:12 +03001323 status = ath6kl_htc_start(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001324
1325 if (status) {
1326 ath6kl_cookie_cleanup(ar);
1327 goto err_rxbuf_cleanup;
1328 }
1329
1330 /* Wait for Wmi event to be ready */
1331 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1332 test_bit(WMI_READY,
1333 &ar->flag),
1334 WMI_TIMEOUT);
1335
1336 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1337 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1338 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1339 status = -EIO;
1340 goto err_htc_stop;
1341 }
1342
1343 if (!timeleft || signal_pending(current)) {
1344 ath6kl_err("wmi is not ready or wait was interrupted\n");
1345 status = -EIO;
1346 goto err_htc_stop;
1347 }
1348
1349 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1350
1351 /* communicate the wmi protocol verision to the target */
1352 if ((ath6kl_set_host_app_area(ar)) != 0)
1353 ath6kl_err("unable to set the host app area\n");
1354
1355 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1356 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1357
1358 status = ath6kl_target_config_wlan_params(ar);
1359 if (!status)
1360 goto ath6kl_init_done;
1361
1362err_htc_stop:
Kalle Vaload226ec2011-08-10 09:49:12 +03001363 ath6kl_htc_stop(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001364err_rxbuf_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001365 ath6kl_htc_flush_rx_buf(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001366 ath6kl_cleanup_amsdu_rxbufs(ar);
1367err_cleanup_scatter:
1368 ath6kl_hif_cleanup_scatter(ar);
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301369err_node_cleanup:
1370 wlan_node_table_cleanup(&ar->scan_table);
Kalle Valobdcd8172011-07-18 00:22:30 +03001371 ath6kl_wmi_shutdown(ar->wmi);
1372 clear_bit(WMI_ENABLED, &ar->flag);
1373 ar->wmi = NULL;
1374
1375ath6kl_init_done:
1376 return status;
1377}
1378
1379int ath6kl_core_init(struct ath6kl *ar)
1380{
1381 int ret = 0;
1382 struct ath6kl_bmi_target_info targ_info;
1383
1384 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1385 if (!ar->ath6kl_wq)
1386 return -ENOMEM;
1387
1388 ret = ath6kl_bmi_init(ar);
1389 if (ret)
1390 goto err_wq;
1391
1392 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1393 if (ret)
1394 goto err_bmi_cleanup;
1395
1396 ar->version.target_ver = le32_to_cpu(targ_info.version);
1397 ar->target_type = le32_to_cpu(targ_info.type);
1398 ar->wdev->wiphy->hw_version = le32_to_cpu(targ_info.version);
1399
1400 ret = ath6kl_configure_target(ar);
1401 if (ret)
1402 goto err_bmi_cleanup;
1403
Kalle Vaload226ec2011-08-10 09:49:12 +03001404 ar->htc_target = ath6kl_htc_create(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001405
1406 if (!ar->htc_target) {
1407 ret = -ENOMEM;
1408 goto err_bmi_cleanup;
1409 }
1410
1411 ar->aggr_cntxt = aggr_init(ar->net_dev);
1412 if (!ar->aggr_cntxt) {
1413 ath6kl_err("failed to initialize aggr\n");
1414 ret = -ENOMEM;
1415 goto err_htc_cleanup;
1416 }
1417
Kalle Valo772c31e2011-09-07 10:55:16 +03001418 ret = ath6kl_fetch_firmwares(ar);
1419 if (ret)
1420 goto err_htc_cleanup;
1421
Kalle Valobdcd8172011-07-18 00:22:30 +03001422 ret = ath6kl_init_upload(ar);
1423 if (ret)
1424 goto err_htc_cleanup;
1425
1426 ret = ath6kl_init(ar->net_dev);
1427 if (ret)
1428 goto err_htc_cleanup;
1429
1430 /* This runs the init function if registered */
1431 ret = register_netdev(ar->net_dev);
1432 if (ret) {
1433 ath6kl_err("register_netdev failed\n");
1434 ath6kl_destroy(ar->net_dev, 0);
1435 return ret;
1436 }
1437
1438 set_bit(NETDEV_REGISTERED, &ar->flag);
1439
1440 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1441 __func__, ar->net_dev->name, ar->net_dev, ar);
1442
1443 return ret;
1444
1445err_htc_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001446 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001447err_bmi_cleanup:
1448 ath6kl_bmi_cleanup(ar);
1449err_wq:
1450 destroy_workqueue(ar->ath6kl_wq);
1451 return ret;
1452}
1453
1454void ath6kl_stop_txrx(struct ath6kl *ar)
1455{
1456 struct net_device *ndev = ar->net_dev;
1457
1458 if (!ndev)
1459 return;
1460
1461 set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1462
1463 if (down_interruptible(&ar->sem)) {
1464 ath6kl_err("down_interruptible failed\n");
1465 return;
1466 }
1467
1468 if (ar->wlan_pwr_state != WLAN_POWER_STATE_CUT_PWR)
1469 ath6kl_stop_endpoint(ndev, false, true);
1470
Raja Mani575b5f32011-07-19 19:27:33 +05301471 clear_bit(WLAN_ENABLED, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001472}
1473
1474/*
1475 * We need to differentiate between the surprise and planned removal of the
1476 * device because of the following consideration:
1477 *
1478 * - In case of surprise removal, the hcd already frees up the pending
1479 * for the device and hence there is no need to unregister the function
1480 * driver inorder to get these requests. For planned removal, the function
1481 * driver has to explicitly unregister itself to have the hcd return all the
1482 * pending requests before the data structures for the devices are freed up.
1483 * Note that as per the current implementation, the function driver will
1484 * end up releasing all the devices since there is no API to selectively
1485 * release a particular device.
1486 *
1487 * - Certain commands issued to the target can be skipped for surprise
1488 * removal since they will anyway not go through.
1489 */
1490void ath6kl_destroy(struct net_device *dev, unsigned int unregister)
1491{
1492 struct ath6kl *ar;
1493
1494 if (!dev || !ath6kl_priv(dev)) {
1495 ath6kl_err("failed to get device structure\n");
1496 return;
1497 }
1498
1499 ar = ath6kl_priv(dev);
1500
1501 destroy_workqueue(ar->ath6kl_wq);
1502
1503 if (ar->htc_target)
Kalle Vaload226ec2011-08-10 09:49:12 +03001504 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001505
1506 aggr_module_destroy(ar->aggr_cntxt);
1507
1508 ath6kl_cookie_cleanup(ar);
1509
1510 ath6kl_cleanup_amsdu_rxbufs(ar);
1511
1512 ath6kl_bmi_cleanup(ar);
1513
Kalle Valobdf53962011-09-02 10:32:04 +03001514 ath6kl_debug_cleanup(ar);
1515
Kalle Valobdcd8172011-07-18 00:22:30 +03001516 if (unregister && test_bit(NETDEV_REGISTERED, &ar->flag)) {
1517 unregister_netdev(dev);
1518 clear_bit(NETDEV_REGISTERED, &ar->flag);
1519 }
1520
1521 free_netdev(dev);
1522
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301523 wlan_node_table_cleanup(&ar->scan_table);
1524
Raja Mani19703572011-08-04 19:26:30 +05301525 kfree(ar->fw_board);
1526 kfree(ar->fw_otp);
1527 kfree(ar->fw);
1528 kfree(ar->fw_patch);
1529
Kalle Valobdcd8172011-07-18 00:22:30 +03001530 ath6kl_cfg80211_deinit(ar);
1531}