blob: 41f4e0d5858a598e93e54231000de9e4b86a2b4f [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
Kalle Valo50d41232011-09-07 10:55:17 +0300920static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
Kalle Valo772c31e2011-09-07 10:55:16 +0300921{
922 int ret;
923
Kalle Valo772c31e2011-09-07 10:55:16 +0300924 ret = ath6kl_fetch_otp_file(ar);
925 if (ret)
926 return ret;
927
928 ret = ath6kl_fetch_fw_file(ar);
929 if (ret)
930 return ret;
931
932 ret = ath6kl_fetch_patch_file(ar);
933 if (ret)
934 return ret;
935
936 return 0;
937}
Kalle Valobdcd8172011-07-18 00:22:30 +0300938
Kalle Valo50d41232011-09-07 10:55:17 +0300939static int ath6kl_fetch_fw_api2(struct ath6kl *ar)
940{
941 size_t magic_len, len, ie_len;
942 const struct firmware *fw;
943 struct ath6kl_fw_ie *hdr;
944 const char *filename;
945 const u8 *data;
946 int ret, ie_id;
947
948 switch (ar->version.target_ver) {
949 case AR6003_REV2_VERSION:
950 filename = AR6003_REV2_FIRMWARE_2_FILE;
951 break;
952 case AR6003_REV3_VERSION:
953 filename = AR6003_REV3_FIRMWARE_2_FILE;
954 break;
955 case AR6004_REV1_VERSION:
956 filename = AR6004_REV1_FIRMWARE_2_FILE;
957 break;
958 default:
959 return -EOPNOTSUPP;
960 }
961
962 ret = request_firmware(&fw, filename, ar->dev);
963 if (ret)
964 return ret;
965
966 data = fw->data;
967 len = fw->size;
968
969 /* magic also includes the null byte, check that as well */
970 magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
971
972 if (len < magic_len) {
973 ret = -EINVAL;
974 goto out;
975 }
976
977 if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
978 ret = -EINVAL;
979 goto out;
980 }
981
982 len -= magic_len;
983 data += magic_len;
984
985 /* loop elements */
986 while (len > sizeof(struct ath6kl_fw_ie)) {
987 /* hdr is unaligned! */
988 hdr = (struct ath6kl_fw_ie *) data;
989
990 ie_id = le32_to_cpup(&hdr->id);
991 ie_len = le32_to_cpup(&hdr->len);
992
993 len -= sizeof(*hdr);
994 data += sizeof(*hdr);
995
996 if (len < ie_len) {
997 ret = -EINVAL;
998 goto out;
999 }
1000
1001 switch (ie_id) {
1002 case ATH6KL_FW_IE_OTP_IMAGE:
1003 ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
1004
1005 if (ar->fw_otp == NULL) {
1006 ret = -ENOMEM;
1007 goto out;
1008 }
1009
1010 ar->fw_otp_len = ie_len;
1011 break;
1012 case ATH6KL_FW_IE_FW_IMAGE:
1013 ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
1014
1015 if (ar->fw == NULL) {
1016 ret = -ENOMEM;
1017 goto out;
1018 }
1019
1020 ar->fw_len = ie_len;
1021 break;
1022 case ATH6KL_FW_IE_PATCH_IMAGE:
1023 ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
1024
1025 if (ar->fw_patch == NULL) {
1026 ret = -ENOMEM;
1027 goto out;
1028 }
1029
1030 ar->fw_patch_len = ie_len;
1031 break;
1032 default:
1033 ath6kl_dbg(ATH6KL_DBG_TRC, "Unknown fw ie: %u\n",
1034 le32_to_cpup(&hdr->id));
1035 break;
1036 }
1037
1038 len -= ie_len;
1039 data += ie_len;
1040 };
1041
1042 ret = 0;
1043out:
1044 release_firmware(fw);
1045
1046 return ret;
1047}
1048
1049static int ath6kl_fetch_firmwares(struct ath6kl *ar)
1050{
1051 int ret;
1052
1053 ret = ath6kl_fetch_board_file(ar);
1054 if (ret)
1055 return ret;
1056
1057 ret = ath6kl_fetch_fw_api2(ar);
1058 if (ret == 0)
1059 /* fw api 2 found, use it */
1060 return 0;
1061
1062 ret = ath6kl_fetch_fw_api1(ar);
1063 if (ret)
1064 return ret;
1065
1066 return 0;
1067}
1068
Kalle Valobdcd8172011-07-18 00:22:30 +03001069static int ath6kl_upload_board_file(struct ath6kl *ar)
1070{
1071 u32 board_address, board_ext_address, param;
Kevin Fang31024d92011-07-11 17:14:13 +08001072 u32 board_data_size, board_ext_data_size;
Kalle Valobdcd8172011-07-18 00:22:30 +03001073 int ret;
1074
Kalle Valo772c31e2011-09-07 10:55:16 +03001075 if (WARN_ON(ar->fw_board == NULL))
1076 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001077
Kevin Fang31024d92011-07-11 17:14:13 +08001078 /*
1079 * Determine where in Target RAM to write Board Data.
1080 * For AR6004, host determine Target RAM address for
1081 * writing board data.
1082 */
1083 if (ar->target_type == TARGET_TYPE_AR6004) {
1084 board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
1085 ath6kl_bmi_write(ar,
1086 ath6kl_get_hi_item_addr(ar,
1087 HI_ITEM(hi_board_data)),
1088 (u8 *) &board_address, 4);
1089 } else {
1090 ath6kl_bmi_read(ar,
1091 ath6kl_get_hi_item_addr(ar,
1092 HI_ITEM(hi_board_data)),
1093 (u8 *) &board_address, 4);
1094 }
1095
Kalle Valobdcd8172011-07-18 00:22:30 +03001096 ath6kl_dbg(ATH6KL_DBG_TRC, "board data download addr: 0x%x\n",
1097 board_address);
1098
1099 /* determine where in target ram to write extended board data */
1100 ath6kl_bmi_read(ar,
1101 ath6kl_get_hi_item_addr(ar,
1102 HI_ITEM(hi_board_ext_data)),
1103 (u8 *) &board_ext_address, 4);
1104
1105 ath6kl_dbg(ATH6KL_DBG_TRC, "board file download addr: 0x%x\n",
1106 board_ext_address);
1107
1108 if (board_ext_address == 0) {
1109 ath6kl_err("Failed to get board file target address.\n");
1110 return -EINVAL;
1111 }
1112
Kevin Fang31024d92011-07-11 17:14:13 +08001113 switch (ar->target_type) {
1114 case TARGET_TYPE_AR6003:
1115 board_data_size = AR6003_BOARD_DATA_SZ;
1116 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1117 break;
1118 case TARGET_TYPE_AR6004:
1119 board_data_size = AR6004_BOARD_DATA_SZ;
1120 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
1121 break;
1122 default:
1123 WARN_ON(1);
1124 return -EINVAL;
1125 break;
1126 }
1127
1128 if (ar->fw_board_len == (board_data_size +
1129 board_ext_data_size)) {
1130
Kalle Valobdcd8172011-07-18 00:22:30 +03001131 /* write extended board data */
1132 ret = ath6kl_bmi_write(ar, board_ext_address,
Kevin Fang31024d92011-07-11 17:14:13 +08001133 ar->fw_board + board_data_size,
1134 board_ext_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001135 if (ret) {
1136 ath6kl_err("Failed to write extended board data: %d\n",
1137 ret);
1138 return ret;
1139 }
1140
1141 /* record that extended board data is initialized */
Kevin Fang31024d92011-07-11 17:14:13 +08001142 param = (board_ext_data_size << 16) | 1;
1143
Kalle Valobdcd8172011-07-18 00:22:30 +03001144 ath6kl_bmi_write(ar,
1145 ath6kl_get_hi_item_addr(ar,
1146 HI_ITEM(hi_board_ext_data_config)),
1147 (unsigned char *) &param, 4);
1148 }
1149
Kevin Fang31024d92011-07-11 17:14:13 +08001150 if (ar->fw_board_len < board_data_size) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001151 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1152 ret = -EINVAL;
1153 return ret;
1154 }
1155
1156 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
Kevin Fang31024d92011-07-11 17:14:13 +08001157 board_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +03001158
1159 if (ret) {
1160 ath6kl_err("Board file bmi write failed: %d\n", ret);
1161 return ret;
1162 }
1163
1164 /* record the fact that Board Data IS initialized */
1165 param = 1;
1166 ath6kl_bmi_write(ar,
1167 ath6kl_get_hi_item_addr(ar,
1168 HI_ITEM(hi_board_data_initialized)),
1169 (u8 *)&param, 4);
1170
1171 return ret;
1172}
1173
1174static int ath6kl_upload_otp(struct ath6kl *ar)
1175{
Kalle Valobdcd8172011-07-18 00:22:30 +03001176 u32 address, param;
1177 int ret;
1178
Kalle Valo772c31e2011-09-07 10:55:16 +03001179 if (WARN_ON(ar->fw_otp == NULL))
1180 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001181
1182 address = ath6kl_get_load_address(ar->version.target_ver,
1183 APP_LOAD_ADDR);
1184
1185 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1186 ar->fw_otp_len);
1187 if (ret) {
1188 ath6kl_err("Failed to upload OTP file: %d\n", ret);
1189 return ret;
1190 }
1191
1192 /* execute the OTP code */
1193 param = 0;
1194 address = ath6kl_get_load_address(ar->version.target_ver,
1195 APP_START_OVERRIDE_ADDR);
1196 ath6kl_bmi_execute(ar, address, &param);
1197
1198 return ret;
1199}
1200
1201static int ath6kl_upload_firmware(struct ath6kl *ar)
1202{
Kalle Valobdcd8172011-07-18 00:22:30 +03001203 u32 address;
1204 int ret;
1205
Kalle Valo772c31e2011-09-07 10:55:16 +03001206 if (WARN_ON(ar->fw == NULL))
1207 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001208
1209 address = ath6kl_get_load_address(ar->version.target_ver,
1210 APP_LOAD_ADDR);
1211
1212 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1213
1214 if (ret) {
1215 ath6kl_err("Failed to write firmware: %d\n", ret);
1216 return ret;
1217 }
1218
Kevin Fang31024d92011-07-11 17:14:13 +08001219 /*
1220 * Set starting address for firmware
1221 * Don't need to setup app_start override addr on AR6004
1222 */
1223 if (ar->target_type != TARGET_TYPE_AR6004) {
1224 address = ath6kl_get_load_address(ar->version.target_ver,
1225 APP_START_OVERRIDE_ADDR);
1226 ath6kl_bmi_set_app_start(ar, address);
1227 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001228 return ret;
1229}
1230
1231static int ath6kl_upload_patch(struct ath6kl *ar)
1232{
Kalle Valobdcd8172011-07-18 00:22:30 +03001233 u32 address, param;
1234 int ret;
1235
Kalle Valo772c31e2011-09-07 10:55:16 +03001236 if (WARN_ON(ar->fw_patch == NULL))
1237 return -ENOENT;
Kalle Valobdcd8172011-07-18 00:22:30 +03001238
1239 address = ath6kl_get_load_address(ar->version.target_ver,
1240 DATASET_PATCH_ADDR);
1241
1242 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1243 if (ret) {
1244 ath6kl_err("Failed to write patch file: %d\n", ret);
1245 return ret;
1246 }
1247
1248 param = address;
1249 ath6kl_bmi_write(ar,
1250 ath6kl_get_hi_item_addr(ar,
1251 HI_ITEM(hi_dset_list_head)),
1252 (unsigned char *) &param, 4);
1253
1254 return 0;
1255}
1256
1257static int ath6kl_init_upload(struct ath6kl *ar)
1258{
1259 u32 param, options, sleep, address;
1260 int status = 0;
1261
Kevin Fang31024d92011-07-11 17:14:13 +08001262 if (ar->target_type != TARGET_TYPE_AR6003 &&
1263 ar->target_type != TARGET_TYPE_AR6004)
Kalle Valobdcd8172011-07-18 00:22:30 +03001264 return -EINVAL;
1265
1266 /* temporarily disable system sleep */
1267 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1268 status = ath6kl_bmi_reg_read(ar, address, &param);
1269 if (status)
1270 return status;
1271
1272 options = param;
1273
1274 param |= ATH6KL_OPTION_SLEEP_DISABLE;
1275 status = ath6kl_bmi_reg_write(ar, address, param);
1276 if (status)
1277 return status;
1278
1279 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1280 status = ath6kl_bmi_reg_read(ar, address, &param);
1281 if (status)
1282 return status;
1283
1284 sleep = param;
1285
1286 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1287 status = ath6kl_bmi_reg_write(ar, address, param);
1288 if (status)
1289 return status;
1290
1291 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1292 options, sleep);
1293
1294 /* program analog PLL register */
Kevin Fang31024d92011-07-11 17:14:13 +08001295 /* no need to control 40/44MHz clock on AR6004 */
1296 if (ar->target_type != TARGET_TYPE_AR6004) {
1297 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1298 0xF9104001);
Kalle Valobdcd8172011-07-18 00:22:30 +03001299
Kevin Fang31024d92011-07-11 17:14:13 +08001300 if (status)
1301 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +03001302
Kevin Fang31024d92011-07-11 17:14:13 +08001303 /* Run at 80/88MHz by default */
1304 param = SM(CPU_CLOCK_STANDARD, 1);
1305
1306 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1307 status = ath6kl_bmi_reg_write(ar, address, param);
1308 if (status)
1309 return status;
1310 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001311
1312 param = 0;
1313 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1314 param = SM(LPO_CAL_ENABLE, 1);
1315 status = ath6kl_bmi_reg_write(ar, address, param);
1316 if (status)
1317 return status;
1318
1319 /* WAR to avoid SDIO CRC err */
1320 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1321 ath6kl_err("temporary war to avoid sdio crc error\n");
1322
1323 param = 0x20;
1324
1325 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1326 status = ath6kl_bmi_reg_write(ar, address, param);
1327 if (status)
1328 return status;
1329
1330 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1331 status = ath6kl_bmi_reg_write(ar, address, param);
1332 if (status)
1333 return status;
1334
1335 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1336 status = ath6kl_bmi_reg_write(ar, address, param);
1337 if (status)
1338 return status;
1339
1340 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1341 status = ath6kl_bmi_reg_write(ar, address, param);
1342 if (status)
1343 return status;
1344 }
1345
1346 /* write EEPROM data to Target RAM */
1347 status = ath6kl_upload_board_file(ar);
1348 if (status)
1349 return status;
1350
1351 /* transfer One time Programmable data */
1352 status = ath6kl_upload_otp(ar);
1353 if (status)
1354 return status;
1355
1356 /* Download Target firmware */
1357 status = ath6kl_upload_firmware(ar);
1358 if (status)
1359 return status;
1360
1361 status = ath6kl_upload_patch(ar);
1362 if (status)
1363 return status;
1364
1365 /* Restore system sleep */
1366 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1367 status = ath6kl_bmi_reg_write(ar, address, sleep);
1368 if (status)
1369 return status;
1370
1371 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1372 param = options | 0x20;
1373 status = ath6kl_bmi_reg_write(ar, address, param);
1374 if (status)
1375 return status;
1376
1377 /* Configure GPIO AR6003 UART */
1378 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1379 status = ath6kl_bmi_write(ar,
1380 ath6kl_get_hi_item_addr(ar,
1381 HI_ITEM(hi_dbg_uart_txpin)),
1382 (u8 *)&param, 4);
1383
1384 return status;
1385}
1386
1387static int ath6kl_init(struct net_device *dev)
1388{
1389 struct ath6kl *ar = ath6kl_priv(dev);
1390 int status = 0;
1391 s32 timeleft;
1392
1393 if (!ar)
1394 return -EIO;
1395
1396 /* Do we need to finish the BMI phase */
1397 if (ath6kl_bmi_done(ar)) {
1398 status = -EIO;
1399 goto ath6kl_init_done;
1400 }
1401
1402 /* Indicate that WMI is enabled (although not ready yet) */
1403 set_bit(WMI_ENABLED, &ar->flag);
Vasanthakumar Thiagarajan28657852011-07-21 12:00:49 +05301404 ar->wmi = ath6kl_wmi_init(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001405 if (!ar->wmi) {
1406 ath6kl_err("failed to initialize wmi\n");
1407 status = -EIO;
1408 goto ath6kl_init_done;
1409 }
1410
1411 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1412
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301413 wlan_node_table_init(&ar->scan_table);
1414
Kalle Valobdcd8172011-07-18 00:22:30 +03001415 /*
1416 * The reason we have to wait for the target here is that the
1417 * driver layer has to init BMI in order to set the host block
1418 * size.
1419 */
Kalle Vaload226ec2011-08-10 09:49:12 +03001420 if (ath6kl_htc_wait_target(ar->htc_target)) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001421 status = -EIO;
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301422 goto err_node_cleanup;
Kalle Valobdcd8172011-07-18 00:22:30 +03001423 }
1424
1425 if (ath6kl_init_service_ep(ar)) {
1426 status = -EIO;
1427 goto err_cleanup_scatter;
1428 }
1429
1430 /* setup access class priority mappings */
1431 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1432 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1433 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1434 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1435
1436 /* give our connected endpoints some buffers */
1437 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1438 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1439
1440 /* allocate some buffers that handle larger AMSDU frames */
1441 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1442
1443 /* setup credit distribution */
1444 ath6k_setup_credit_dist(ar->htc_target, &ar->credit_state_info);
1445
1446 ath6kl_cookie_init(ar);
1447
1448 /* start HTC */
Kalle Vaload226ec2011-08-10 09:49:12 +03001449 status = ath6kl_htc_start(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001450
1451 if (status) {
1452 ath6kl_cookie_cleanup(ar);
1453 goto err_rxbuf_cleanup;
1454 }
1455
1456 /* Wait for Wmi event to be ready */
1457 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1458 test_bit(WMI_READY,
1459 &ar->flag),
1460 WMI_TIMEOUT);
1461
1462 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1463 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1464 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1465 status = -EIO;
1466 goto err_htc_stop;
1467 }
1468
1469 if (!timeleft || signal_pending(current)) {
1470 ath6kl_err("wmi is not ready or wait was interrupted\n");
1471 status = -EIO;
1472 goto err_htc_stop;
1473 }
1474
1475 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1476
1477 /* communicate the wmi protocol verision to the target */
1478 if ((ath6kl_set_host_app_area(ar)) != 0)
1479 ath6kl_err("unable to set the host app area\n");
1480
1481 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1482 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1483
1484 status = ath6kl_target_config_wlan_params(ar);
1485 if (!status)
1486 goto ath6kl_init_done;
1487
1488err_htc_stop:
Kalle Vaload226ec2011-08-10 09:49:12 +03001489 ath6kl_htc_stop(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001490err_rxbuf_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001491 ath6kl_htc_flush_rx_buf(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001492 ath6kl_cleanup_amsdu_rxbufs(ar);
1493err_cleanup_scatter:
1494 ath6kl_hif_cleanup_scatter(ar);
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301495err_node_cleanup:
1496 wlan_node_table_cleanup(&ar->scan_table);
Kalle Valobdcd8172011-07-18 00:22:30 +03001497 ath6kl_wmi_shutdown(ar->wmi);
1498 clear_bit(WMI_ENABLED, &ar->flag);
1499 ar->wmi = NULL;
1500
1501ath6kl_init_done:
1502 return status;
1503}
1504
1505int ath6kl_core_init(struct ath6kl *ar)
1506{
1507 int ret = 0;
1508 struct ath6kl_bmi_target_info targ_info;
1509
1510 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1511 if (!ar->ath6kl_wq)
1512 return -ENOMEM;
1513
1514 ret = ath6kl_bmi_init(ar);
1515 if (ret)
1516 goto err_wq;
1517
1518 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1519 if (ret)
1520 goto err_bmi_cleanup;
1521
1522 ar->version.target_ver = le32_to_cpu(targ_info.version);
1523 ar->target_type = le32_to_cpu(targ_info.type);
1524 ar->wdev->wiphy->hw_version = le32_to_cpu(targ_info.version);
1525
1526 ret = ath6kl_configure_target(ar);
1527 if (ret)
1528 goto err_bmi_cleanup;
1529
Kalle Vaload226ec2011-08-10 09:49:12 +03001530 ar->htc_target = ath6kl_htc_create(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001531
1532 if (!ar->htc_target) {
1533 ret = -ENOMEM;
1534 goto err_bmi_cleanup;
1535 }
1536
1537 ar->aggr_cntxt = aggr_init(ar->net_dev);
1538 if (!ar->aggr_cntxt) {
1539 ath6kl_err("failed to initialize aggr\n");
1540 ret = -ENOMEM;
1541 goto err_htc_cleanup;
1542 }
1543
Kalle Valo772c31e2011-09-07 10:55:16 +03001544 ret = ath6kl_fetch_firmwares(ar);
1545 if (ret)
1546 goto err_htc_cleanup;
1547
Kalle Valobdcd8172011-07-18 00:22:30 +03001548 ret = ath6kl_init_upload(ar);
1549 if (ret)
1550 goto err_htc_cleanup;
1551
1552 ret = ath6kl_init(ar->net_dev);
1553 if (ret)
1554 goto err_htc_cleanup;
1555
1556 /* This runs the init function if registered */
1557 ret = register_netdev(ar->net_dev);
1558 if (ret) {
1559 ath6kl_err("register_netdev failed\n");
1560 ath6kl_destroy(ar->net_dev, 0);
1561 return ret;
1562 }
1563
1564 set_bit(NETDEV_REGISTERED, &ar->flag);
1565
1566 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1567 __func__, ar->net_dev->name, ar->net_dev, ar);
1568
1569 return ret;
1570
1571err_htc_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001572 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001573err_bmi_cleanup:
1574 ath6kl_bmi_cleanup(ar);
1575err_wq:
1576 destroy_workqueue(ar->ath6kl_wq);
1577 return ret;
1578}
1579
1580void ath6kl_stop_txrx(struct ath6kl *ar)
1581{
1582 struct net_device *ndev = ar->net_dev;
1583
1584 if (!ndev)
1585 return;
1586
1587 set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1588
1589 if (down_interruptible(&ar->sem)) {
1590 ath6kl_err("down_interruptible failed\n");
1591 return;
1592 }
1593
1594 if (ar->wlan_pwr_state != WLAN_POWER_STATE_CUT_PWR)
1595 ath6kl_stop_endpoint(ndev, false, true);
1596
Raja Mani575b5f32011-07-19 19:27:33 +05301597 clear_bit(WLAN_ENABLED, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001598}
1599
1600/*
1601 * We need to differentiate between the surprise and planned removal of the
1602 * device because of the following consideration:
1603 *
1604 * - In case of surprise removal, the hcd already frees up the pending
1605 * for the device and hence there is no need to unregister the function
1606 * driver inorder to get these requests. For planned removal, the function
1607 * driver has to explicitly unregister itself to have the hcd return all the
1608 * pending requests before the data structures for the devices are freed up.
1609 * Note that as per the current implementation, the function driver will
1610 * end up releasing all the devices since there is no API to selectively
1611 * release a particular device.
1612 *
1613 * - Certain commands issued to the target can be skipped for surprise
1614 * removal since they will anyway not go through.
1615 */
1616void ath6kl_destroy(struct net_device *dev, unsigned int unregister)
1617{
1618 struct ath6kl *ar;
1619
1620 if (!dev || !ath6kl_priv(dev)) {
1621 ath6kl_err("failed to get device structure\n");
1622 return;
1623 }
1624
1625 ar = ath6kl_priv(dev);
1626
1627 destroy_workqueue(ar->ath6kl_wq);
1628
1629 if (ar->htc_target)
Kalle Vaload226ec2011-08-10 09:49:12 +03001630 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001631
1632 aggr_module_destroy(ar->aggr_cntxt);
1633
1634 ath6kl_cookie_cleanup(ar);
1635
1636 ath6kl_cleanup_amsdu_rxbufs(ar);
1637
1638 ath6kl_bmi_cleanup(ar);
1639
Kalle Valobdf53962011-09-02 10:32:04 +03001640 ath6kl_debug_cleanup(ar);
1641
Kalle Valobdcd8172011-07-18 00:22:30 +03001642 if (unregister && test_bit(NETDEV_REGISTERED, &ar->flag)) {
1643 unregister_netdev(dev);
1644 clear_bit(NETDEV_REGISTERED, &ar->flag);
1645 }
1646
1647 free_netdev(dev);
1648
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301649 wlan_node_table_cleanup(&ar->scan_table);
1650
Raja Mani19703572011-08-04 19:26:30 +05301651 kfree(ar->fw_board);
1652 kfree(ar->fw_otp);
1653 kfree(ar->fw);
1654 kfree(ar->fw_patch);
1655
Kalle Valobdcd8172011-07-18 00:22:30 +03001656 ath6kl_cfg80211_deinit(ar);
1657}