blob: ad9716c91a81525fa0c3aa536362d8674ed44cd0 [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
18#include <linux/mmc/sdio_func.h>
19#include "core.h"
20#include "cfg80211.h"
21#include "target.h"
22#include "debug.h"
23#include "hif-ops.h"
24
25unsigned int debug_mask;
26
27module_param(debug_mask, uint, 0644);
28
29/*
30 * Include definitions here that can be used to tune the WLAN module
31 * behavior. Different customers can tune the behavior as per their needs,
32 * here.
33 */
34
35/*
36 * This configuration item enable/disable keepalive support.
37 * Keepalive support: In the absence of any data traffic to AP, null
38 * frames will be sent to the AP at periodic interval, to keep the association
39 * active. This configuration item defines the periodic interval.
40 * Use value of zero to disable keepalive support
41 * Default: 60 seconds
42 */
43#define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
44
45/*
46 * This configuration item sets the value of disconnect timeout
47 * Firmware delays sending the disconnec event to the host for this
48 * timeout after is gets disconnected from the current AP.
49 * If the firmware successly roams within the disconnect timeout
50 * it sends a new connect event
51 */
52#define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
53
54#define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
55
56enum addr_type {
57 DATASET_PATCH_ADDR,
58 APP_LOAD_ADDR,
59 APP_START_OVERRIDE_ADDR,
60};
61
62#define ATH6KL_DATA_OFFSET 64
63struct sk_buff *ath6kl_buf_alloc(int size)
64{
65 struct sk_buff *skb;
66 u16 reserved;
67
68 /* Add chacheline space at front and back of buffer */
69 reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
Vasanthakumar Thiagarajan1df94a82011-08-17 18:45:10 +053070 sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
Kalle Valobdcd8172011-07-18 00:22:30 +030071 skb = dev_alloc_skb(size + reserved);
72
73 if (skb)
74 skb_reserve(skb, reserved - L1_CACHE_BYTES);
75 return skb;
76}
77
78void ath6kl_init_profile_info(struct ath6kl *ar)
79{
80 ar->ssid_len = 0;
81 memset(ar->ssid, 0, sizeof(ar->ssid));
82
83 ar->dot11_auth_mode = OPEN_AUTH;
84 ar->auth_mode = NONE_AUTH;
85 ar->prwise_crypto = NONE_CRYPT;
86 ar->prwise_crypto_len = 0;
87 ar->grp_crypto = NONE_CRYPT;
88 ar->grp_crpto_len = 0;
89 memset(ar->wep_key_list, 0, sizeof(ar->wep_key_list));
90 memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
91 memset(ar->bssid, 0, sizeof(ar->bssid));
92 ar->bss_ch = 0;
93 ar->nw_type = ar->next_mode = INFRA_NETWORK;
94}
95
96static u8 ath6kl_get_fw_iftype(struct ath6kl *ar)
97{
98 switch (ar->nw_type) {
99 case INFRA_NETWORK:
100 return HI_OPTION_FW_MODE_BSS_STA;
101 case ADHOC_NETWORK:
102 return HI_OPTION_FW_MODE_IBSS;
103 case AP_NETWORK:
104 return HI_OPTION_FW_MODE_AP;
105 default:
106 ath6kl_err("Unsupported interface type :%d\n", ar->nw_type);
107 return 0xff;
108 }
109}
110
111static inline u32 ath6kl_get_hi_item_addr(struct ath6kl *ar,
112 u32 item_offset)
113{
114 u32 addr = 0;
115
116 if (ar->target_type == TARGET_TYPE_AR6003)
Kevin Fang31024d92011-07-11 17:14:13 +0800117 addr = ATH6KL_AR6003_HI_START_ADDR + item_offset;
118 else if (ar->target_type == TARGET_TYPE_AR6004)
119 addr = ATH6KL_AR6004_HI_START_ADDR + item_offset;
Kalle Valobdcd8172011-07-18 00:22:30 +0300120
121 return addr;
122}
123
124static int ath6kl_set_host_app_area(struct ath6kl *ar)
125{
126 u32 address, data;
127 struct host_app_area host_app_area;
128
129 /* Fetch the address of the host_app_area_s
130 * instance in the host interest area */
131 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
Kevin Fang31024d92011-07-11 17:14:13 +0800132 address = TARG_VTOP(ar->target_type, address);
Kalle Valobdcd8172011-07-18 00:22:30 +0300133
134 if (ath6kl_read_reg_diag(ar, &address, &data))
135 return -EIO;
136
Kevin Fang31024d92011-07-11 17:14:13 +0800137 address = TARG_VTOP(ar->target_type, data);
Kalle Valobdcd8172011-07-18 00:22:30 +0300138 host_app_area.wmi_protocol_ver = WMI_PROTOCOL_VERSION;
139 if (ath6kl_access_datadiag(ar, address,
140 (u8 *)&host_app_area,
141 sizeof(struct host_app_area), false))
142 return -EIO;
143
144 return 0;
145}
146
147static inline void set_ac2_ep_map(struct ath6kl *ar,
148 u8 ac,
149 enum htc_endpoint_id ep)
150{
151 ar->ac2ep_map[ac] = ep;
152 ar->ep2ac_map[ep] = ac;
153}
154
155/* connect to a service */
156static int ath6kl_connectservice(struct ath6kl *ar,
157 struct htc_service_connect_req *con_req,
158 char *desc)
159{
160 int status;
161 struct htc_service_connect_resp response;
162
163 memset(&response, 0, sizeof(response));
164
Kalle Vaload226ec2011-08-10 09:49:12 +0300165 status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
Kalle Valobdcd8172011-07-18 00:22:30 +0300166 if (status) {
167 ath6kl_err("failed to connect to %s service status:%d\n",
168 desc, status);
169 return status;
170 }
171
172 switch (con_req->svc_id) {
173 case WMI_CONTROL_SVC:
174 if (test_bit(WMI_ENABLED, &ar->flag))
175 ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
176 ar->ctrl_ep = response.endpoint;
177 break;
178 case WMI_DATA_BE_SVC:
179 set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
180 break;
181 case WMI_DATA_BK_SVC:
182 set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
183 break;
184 case WMI_DATA_VI_SVC:
185 set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
186 break;
187 case WMI_DATA_VO_SVC:
188 set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
189 break;
190 default:
191 ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
192 return -EINVAL;
193 }
194
195 return 0;
196}
197
198static int ath6kl_init_service_ep(struct ath6kl *ar)
199{
200 struct htc_service_connect_req connect;
201
202 memset(&connect, 0, sizeof(connect));
203
204 /* these fields are the same for all service endpoints */
205 connect.ep_cb.rx = ath6kl_rx;
206 connect.ep_cb.rx_refill = ath6kl_rx_refill;
207 connect.ep_cb.tx_full = ath6kl_tx_queue_full;
208
209 /*
210 * Set the max queue depth so that our ath6kl_tx_queue_full handler
211 * gets called.
212 */
213 connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
214 connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
215 if (!connect.ep_cb.rx_refill_thresh)
216 connect.ep_cb.rx_refill_thresh++;
217
218 /* connect to control service */
219 connect.svc_id = WMI_CONTROL_SVC;
220 if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
221 return -EIO;
222
223 connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
224
225 /*
226 * Limit the HTC message size on the send path, although e can
227 * receive A-MSDU frames of 4K, we will only send ethernet-sized
228 * (802.3) frames on the send path.
229 */
230 connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
231
232 /*
233 * To reduce the amount of committed memory for larger A_MSDU
234 * frames, use the recv-alloc threshold mechanism for larger
235 * packets.
236 */
237 connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
238 connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
239
240 /*
241 * For the remaining data services set the connection flag to
242 * reduce dribbling, if configured to do so.
243 */
244 connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
245 connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
246 connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
247
248 connect.svc_id = WMI_DATA_BE_SVC;
249
250 if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
251 return -EIO;
252
253 /* connect to back-ground map this to WMI LOW_PRI */
254 connect.svc_id = WMI_DATA_BK_SVC;
255 if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
256 return -EIO;
257
258 /* connect to Video service, map this to to HI PRI */
259 connect.svc_id = WMI_DATA_VI_SVC;
260 if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
261 return -EIO;
262
263 /*
264 * Connect to VO service, this is currently not mapped to a WMI
265 * priority stream due to historical reasons. WMI originally
266 * defined 3 priorities over 3 mailboxes We can change this when
267 * WMI is reworked so that priorities are not dependent on
268 * mailboxes.
269 */
270 connect.svc_id = WMI_DATA_VO_SVC;
271 if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
272 return -EIO;
273
274 return 0;
275}
276
277static void ath6kl_init_control_info(struct ath6kl *ar)
278{
279 u8 ctr;
280
281 clear_bit(WMI_ENABLED, &ar->flag);
282 ath6kl_init_profile_info(ar);
283 ar->def_txkey_index = 0;
284 memset(ar->wep_key_list, 0, sizeof(ar->wep_key_list));
285 ar->ch_hint = 0;
286 ar->listen_intvl_t = A_DEFAULT_LISTEN_INTERVAL;
287 ar->listen_intvl_b = 0;
288 ar->tx_pwr = 0;
289 clear_bit(SKIP_SCAN, &ar->flag);
290 set_bit(WMM_ENABLED, &ar->flag);
291 ar->intra_bss = 1;
292 memset(&ar->sc_params, 0, sizeof(ar->sc_params));
293 ar->sc_params.short_scan_ratio = WMI_SHORTSCANRATIO_DEFAULT;
294 ar->sc_params.scan_ctrl_flags = DEFAULT_SCAN_CTRL_FLAGS;
295
296 memset((u8 *)ar->sta_list, 0,
297 AP_MAX_NUM_STA * sizeof(struct ath6kl_sta));
298
299 spin_lock_init(&ar->mcastpsq_lock);
300
301 /* Init the PS queues */
302 for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
303 spin_lock_init(&ar->sta_list[ctr].psq_lock);
304 skb_queue_head_init(&ar->sta_list[ctr].psq);
305 }
306
307 skb_queue_head_init(&ar->mcastpsq);
308
309 memcpy(ar->ap_country_code, DEF_AP_COUNTRY_CODE, 3);
310}
311
312/*
313 * Set HTC/Mbox operational parameters, this can only be called when the
314 * target is in the BMI phase.
315 */
316static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
317 u8 htc_ctrl_buf)
318{
319 int status;
320 u32 blk_size;
321
322 blk_size = ar->mbox_info.block_size;
323
324 if (htc_ctrl_buf)
325 blk_size |= ((u32)htc_ctrl_buf) << 16;
326
327 /* set the host interest area for the block size */
328 status = ath6kl_bmi_write(ar,
329 ath6kl_get_hi_item_addr(ar,
330 HI_ITEM(hi_mbox_io_block_sz)),
331 (u8 *)&blk_size,
332 4);
333 if (status) {
334 ath6kl_err("bmi_write_memory for IO block size failed\n");
335 goto out;
336 }
337
338 ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
339 blk_size,
340 ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
341
342 if (mbox_isr_yield_val) {
343 /* set the host interest area for the mbox ISR yield limit */
344 status = ath6kl_bmi_write(ar,
345 ath6kl_get_hi_item_addr(ar,
346 HI_ITEM(hi_mbox_isr_yield_limit)),
347 (u8 *)&mbox_isr_yield_val,
348 4);
349 if (status) {
350 ath6kl_err("bmi_write_memory for yield limit failed\n");
351 goto out;
352 }
353 }
354
355out:
356 return status;
357}
358
359#define REG_DUMP_COUNT_AR6003 60
360#define REGISTER_DUMP_LEN_MAX 60
361
362static void ath6kl_dump_target_assert_info(struct ath6kl *ar)
363{
364 u32 address;
365 u32 regdump_loc = 0;
366 int status;
367 u32 regdump_val[REGISTER_DUMP_LEN_MAX];
368 u32 i;
369
370 if (ar->target_type != TARGET_TYPE_AR6003)
371 return;
372
373 /* the reg dump pointer is copied to the host interest area */
374 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_failure_state));
Kevin Fang31024d92011-07-11 17:14:13 +0800375 address = TARG_VTOP(ar->target_type, address);
Kalle Valobdcd8172011-07-18 00:22:30 +0300376
377 /* read RAM location through diagnostic window */
378 status = ath6kl_read_reg_diag(ar, &address, &regdump_loc);
379
380 if (status || !regdump_loc) {
381 ath6kl_err("failed to get ptr to register dump area\n");
382 return;
383 }
384
385 ath6kl_dbg(ATH6KL_DBG_TRC, "location of register dump data: 0x%X\n",
386 regdump_loc);
Kevin Fang31024d92011-07-11 17:14:13 +0800387 regdump_loc = TARG_VTOP(ar->target_type, regdump_loc);
Kalle Valobdcd8172011-07-18 00:22:30 +0300388
389 /* fetch register dump data */
390 status = ath6kl_access_datadiag(ar,
391 regdump_loc,
392 (u8 *)&regdump_val[0],
393 REG_DUMP_COUNT_AR6003 * (sizeof(u32)),
394 true);
395
396 if (status) {
397 ath6kl_err("failed to get register dump\n");
398 return;
399 }
400 ath6kl_dbg(ATH6KL_DBG_TRC, "Register Dump:\n");
401
402 for (i = 0; i < REG_DUMP_COUNT_AR6003; i++)
403 ath6kl_dbg(ATH6KL_DBG_TRC, " %d : 0x%8.8X\n",
404 i, regdump_val[i]);
405
406}
407
408void ath6kl_target_failure(struct ath6kl *ar)
409{
410 ath6kl_err("target asserted\n");
411
412 /* try dumping target assertion information (if any) */
413 ath6kl_dump_target_assert_info(ar);
414
415}
416
417static int ath6kl_target_config_wlan_params(struct ath6kl *ar)
418{
419 int status = 0;
420
421 /*
422 * Configure the device for rx dot11 header rules. "0,0" are the
423 * default values. Required if checksum offload is needed. Set
424 * RxMetaVersion to 2.
425 */
426 if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi,
427 ar->rx_meta_ver, 0, 0)) {
428 ath6kl_err("unable to set the rx frame format\n");
429 status = -EIO;
430 }
431
432 if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
433 if ((ath6kl_wmi_pmparams_cmd(ar->wmi, 0, 1, 0, 0, 1,
434 IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
435 ath6kl_err("unable to set power save fail event policy\n");
436 status = -EIO;
437 }
438
439 if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
440 if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, 0,
441 WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
442 ath6kl_err("unable to set barker preamble policy\n");
443 status = -EIO;
444 }
445
446 if (ath6kl_wmi_set_keepalive_cmd(ar->wmi,
447 WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
448 ath6kl_err("unable to set keep alive interval\n");
449 status = -EIO;
450 }
451
452 if (ath6kl_wmi_disctimeout_cmd(ar->wmi,
453 WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
454 ath6kl_err("unable to set disconnect timeout\n");
455 status = -EIO;
456 }
457
458 if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
459 if (ath6kl_wmi_set_wmm_txop(ar->wmi, WMI_TXOP_DISABLED)) {
460 ath6kl_err("unable to set txop bursting\n");
461 status = -EIO;
462 }
463
464 return status;
465}
466
467int ath6kl_configure_target(struct ath6kl *ar)
468{
469 u32 param, ram_reserved_size;
470 u8 fw_iftype;
471
472 fw_iftype = ath6kl_get_fw_iftype(ar);
473 if (fw_iftype == 0xff)
474 return -EINVAL;
475
476 /* Tell target which HTC version it is used*/
477 param = HTC_PROTOCOL_VERSION;
478 if (ath6kl_bmi_write(ar,
479 ath6kl_get_hi_item_addr(ar,
480 HI_ITEM(hi_app_host_interest)),
481 (u8 *)&param, 4) != 0) {
482 ath6kl_err("bmi_write_memory for htc version failed\n");
483 return -EIO;
484 }
485
486 /* set the firmware mode to STA/IBSS/AP */
487 param = 0;
488
489 if (ath6kl_bmi_read(ar,
490 ath6kl_get_hi_item_addr(ar,
491 HI_ITEM(hi_option_flag)),
492 (u8 *)&param, 4) != 0) {
493 ath6kl_err("bmi_read_memory for setting fwmode failed\n");
494 return -EIO;
495 }
496
497 param |= (1 << HI_OPTION_NUM_DEV_SHIFT);
498 param |= (fw_iftype << HI_OPTION_FW_MODE_SHIFT);
499 param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
500 param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
501
502 if (ath6kl_bmi_write(ar,
503 ath6kl_get_hi_item_addr(ar,
504 HI_ITEM(hi_option_flag)),
505 (u8 *)&param,
506 4) != 0) {
507 ath6kl_err("bmi_write_memory for setting fwmode failed\n");
508 return -EIO;
509 }
510
511 ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
512
513 /*
514 * Hardcode the address use for the extended board data
515 * Ideally this should be pre-allocate by the OS at boot time
516 * But since it is a new feature and board data is loaded
517 * at init time, we have to workaround this from host.
518 * It is difficult to patch the firmware boot code,
519 * but possible in theory.
520 */
521
Kevin Fang31024d92011-07-11 17:14:13 +0800522 if (ar->target_type == TARGET_TYPE_AR6003 ||
523 ar->target_type == TARGET_TYPE_AR6004) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300524 if (ar->version.target_ver == AR6003_REV2_VERSION) {
525 param = AR6003_REV2_BOARD_EXT_DATA_ADDRESS;
526 ram_reserved_size = AR6003_REV2_RAM_RESERVE_SIZE;
Kevin Fang31024d92011-07-11 17:14:13 +0800527 } else if (ar->version.target_ver == AR6004_REV1_VERSION) {
528 param = AR6004_REV1_BOARD_EXT_DATA_ADDRESS;
529 ram_reserved_size = AR6004_REV1_RAM_RESERVE_SIZE;
Kalle Valobdcd8172011-07-18 00:22:30 +0300530 } else {
531 param = AR6003_REV3_BOARD_EXT_DATA_ADDRESS;
532 ram_reserved_size = AR6003_REV3_RAM_RESERVE_SIZE;
533 }
534
535 if (ath6kl_bmi_write(ar,
536 ath6kl_get_hi_item_addr(ar,
537 HI_ITEM(hi_board_ext_data)),
538 (u8 *)&param, 4) != 0) {
539 ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
540 return -EIO;
541 }
542 if (ath6kl_bmi_write(ar,
543 ath6kl_get_hi_item_addr(ar,
544 HI_ITEM(hi_end_ram_reserve_sz)),
545 (u8 *)&ram_reserved_size, 4) != 0) {
546 ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
547 return -EIO;
548 }
549 }
550
551 /* set the block size for the target */
552 if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
553 /* use default number of control buffers */
554 return -EIO;
555
556 return 0;
557}
558
559struct ath6kl *ath6kl_core_alloc(struct device *sdev)
560{
561 struct net_device *dev;
562 struct ath6kl *ar;
563 struct wireless_dev *wdev;
564
565 wdev = ath6kl_cfg80211_init(sdev);
566 if (!wdev) {
567 ath6kl_err("ath6kl_cfg80211_init failed\n");
568 return NULL;
569 }
570
571 ar = wdev_priv(wdev);
572 ar->dev = sdev;
573 ar->wdev = wdev;
574 wdev->iftype = NL80211_IFTYPE_STATION;
575
Vasanthakumar Thiagarajand999ba32011-08-26 13:06:31 +0530576 if (ath6kl_debug_init(ar)) {
577 ath6kl_err("Failed to initialize debugfs\n");
578 ath6kl_cfg80211_deinit(ar);
579 return NULL;
580 }
581
Kalle Valobdcd8172011-07-18 00:22:30 +0300582 dev = alloc_netdev(0, "wlan%d", ether_setup);
583 if (!dev) {
584 ath6kl_err("no memory for network device instance\n");
585 ath6kl_cfg80211_deinit(ar);
586 return NULL;
587 }
588
589 dev->ieee80211_ptr = wdev;
590 SET_NETDEV_DEV(dev, wiphy_dev(wdev->wiphy));
591 wdev->netdev = dev;
592 ar->sme_state = SME_DISCONNECTED;
593 ar->auto_auth_stage = AUTH_IDLE;
594
595 init_netdev(dev);
596
597 ar->net_dev = dev;
Raja Mani575b5f32011-07-19 19:27:33 +0530598 set_bit(WLAN_ENABLED, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +0300599
600 ar->wlan_pwr_state = WLAN_POWER_STATE_ON;
601
602 spin_lock_init(&ar->lock);
603
604 ath6kl_init_control_info(ar);
605 init_waitqueue_head(&ar->event_wq);
606 sema_init(&ar->sem, 1);
607 clear_bit(DESTROY_IN_PROGRESS, &ar->flag);
608
609 INIT_LIST_HEAD(&ar->amsdu_rx_buffer_queue);
610
611 setup_timer(&ar->disconnect_timer, disconnect_timer_handler,
612 (unsigned long) dev);
613
614 return ar;
615}
616
617int ath6kl_unavail_ev(struct ath6kl *ar)
618{
619 ath6kl_destroy(ar->net_dev, 1);
620
621 return 0;
622}
623
624/* firmware upload */
625static u32 ath6kl_get_load_address(u32 target_ver, enum addr_type type)
626{
627 WARN_ON(target_ver != AR6003_REV2_VERSION &&
Kevin Fang31024d92011-07-11 17:14:13 +0800628 target_ver != AR6003_REV3_VERSION &&
629 target_ver != AR6004_REV1_VERSION);
Kalle Valobdcd8172011-07-18 00:22:30 +0300630
631 switch (type) {
632 case DATASET_PATCH_ADDR:
633 return (target_ver == AR6003_REV2_VERSION) ?
634 AR6003_REV2_DATASET_PATCH_ADDRESS :
635 AR6003_REV3_DATASET_PATCH_ADDRESS;
636 case APP_LOAD_ADDR:
637 return (target_ver == AR6003_REV2_VERSION) ?
638 AR6003_REV2_APP_LOAD_ADDRESS :
639 0x1234;
640 case APP_START_OVERRIDE_ADDR:
641 return (target_ver == AR6003_REV2_VERSION) ?
642 AR6003_REV2_APP_START_OVERRIDE :
643 AR6003_REV3_APP_START_OVERRIDE;
644 default:
645 return 0;
646 }
647}
648
649static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
650 u8 **fw, size_t *fw_len)
651{
652 const struct firmware *fw_entry;
653 int ret;
654
655 ret = request_firmware(&fw_entry, filename, ar->dev);
656 if (ret)
657 return ret;
658
659 *fw_len = fw_entry->size;
660 *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
661
662 if (*fw == NULL)
663 ret = -ENOMEM;
664
665 release_firmware(fw_entry);
666
667 return ret;
668}
669
670static int ath6kl_fetch_board_file(struct ath6kl *ar)
671{
672 const char *filename;
673 int ret;
674
675 switch (ar->version.target_ver) {
676 case AR6003_REV2_VERSION:
677 filename = AR6003_REV2_BOARD_DATA_FILE;
678 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800679 case AR6004_REV1_VERSION:
680 filename = AR6004_REV1_BOARD_DATA_FILE;
681 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300682 default:
683 filename = AR6003_REV3_BOARD_DATA_FILE;
684 break;
685 }
686
687 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
688 &ar->fw_board_len);
689 if (ret == 0) {
690 /* managed to get proper board file */
691 return 0;
692 }
693
694 /* there was no proper board file, try to use default instead */
695 ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
696 filename, ret);
697
698 switch (ar->version.target_ver) {
699 case AR6003_REV2_VERSION:
700 filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
701 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800702 case AR6004_REV1_VERSION:
703 filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
704 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300705 default:
706 filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
707 break;
708 }
709
710 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
711 &ar->fw_board_len);
712 if (ret) {
713 ath6kl_err("Failed to get default board file %s: %d\n",
714 filename, ret);
715 return ret;
716 }
717
718 ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
719 ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
720
721 return 0;
722}
723
724
725static int ath6kl_upload_board_file(struct ath6kl *ar)
726{
727 u32 board_address, board_ext_address, param;
Kevin Fang31024d92011-07-11 17:14:13 +0800728 u32 board_data_size, board_ext_data_size;
Kalle Valobdcd8172011-07-18 00:22:30 +0300729 int ret;
730
731 if (ar->fw_board == NULL) {
732 ret = ath6kl_fetch_board_file(ar);
733 if (ret)
734 return ret;
735 }
736
Kevin Fang31024d92011-07-11 17:14:13 +0800737 /*
738 * Determine where in Target RAM to write Board Data.
739 * For AR6004, host determine Target RAM address for
740 * writing board data.
741 */
742 if (ar->target_type == TARGET_TYPE_AR6004) {
743 board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
744 ath6kl_bmi_write(ar,
745 ath6kl_get_hi_item_addr(ar,
746 HI_ITEM(hi_board_data)),
747 (u8 *) &board_address, 4);
748 } else {
749 ath6kl_bmi_read(ar,
750 ath6kl_get_hi_item_addr(ar,
751 HI_ITEM(hi_board_data)),
752 (u8 *) &board_address, 4);
753 }
754
Kalle Valobdcd8172011-07-18 00:22:30 +0300755 ath6kl_dbg(ATH6KL_DBG_TRC, "board data download addr: 0x%x\n",
756 board_address);
757
758 /* determine where in target ram to write extended board data */
759 ath6kl_bmi_read(ar,
760 ath6kl_get_hi_item_addr(ar,
761 HI_ITEM(hi_board_ext_data)),
762 (u8 *) &board_ext_address, 4);
763
764 ath6kl_dbg(ATH6KL_DBG_TRC, "board file download addr: 0x%x\n",
765 board_ext_address);
766
767 if (board_ext_address == 0) {
768 ath6kl_err("Failed to get board file target address.\n");
769 return -EINVAL;
770 }
771
Kevin Fang31024d92011-07-11 17:14:13 +0800772 switch (ar->target_type) {
773 case TARGET_TYPE_AR6003:
774 board_data_size = AR6003_BOARD_DATA_SZ;
775 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
776 break;
777 case TARGET_TYPE_AR6004:
778 board_data_size = AR6004_BOARD_DATA_SZ;
779 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
780 break;
781 default:
782 WARN_ON(1);
783 return -EINVAL;
784 break;
785 }
786
787 if (ar->fw_board_len == (board_data_size +
788 board_ext_data_size)) {
789
Kalle Valobdcd8172011-07-18 00:22:30 +0300790 /* write extended board data */
791 ret = ath6kl_bmi_write(ar, board_ext_address,
Kevin Fang31024d92011-07-11 17:14:13 +0800792 ar->fw_board + board_data_size,
793 board_ext_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +0300794 if (ret) {
795 ath6kl_err("Failed to write extended board data: %d\n",
796 ret);
797 return ret;
798 }
799
800 /* record that extended board data is initialized */
Kevin Fang31024d92011-07-11 17:14:13 +0800801 param = (board_ext_data_size << 16) | 1;
802
Kalle Valobdcd8172011-07-18 00:22:30 +0300803 ath6kl_bmi_write(ar,
804 ath6kl_get_hi_item_addr(ar,
805 HI_ITEM(hi_board_ext_data_config)),
806 (unsigned char *) &param, 4);
807 }
808
Kevin Fang31024d92011-07-11 17:14:13 +0800809 if (ar->fw_board_len < board_data_size) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300810 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
811 ret = -EINVAL;
812 return ret;
813 }
814
815 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
Kevin Fang31024d92011-07-11 17:14:13 +0800816 board_data_size);
Kalle Valobdcd8172011-07-18 00:22:30 +0300817
818 if (ret) {
819 ath6kl_err("Board file bmi write failed: %d\n", ret);
820 return ret;
821 }
822
823 /* record the fact that Board Data IS initialized */
824 param = 1;
825 ath6kl_bmi_write(ar,
826 ath6kl_get_hi_item_addr(ar,
827 HI_ITEM(hi_board_data_initialized)),
828 (u8 *)&param, 4);
829
830 return ret;
831}
832
833static int ath6kl_upload_otp(struct ath6kl *ar)
834{
835 const char *filename;
836 u32 address, param;
837 int ret;
838
839 switch (ar->version.target_ver) {
840 case AR6003_REV2_VERSION:
841 filename = AR6003_REV2_OTP_FILE;
842 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800843 case AR6004_REV1_VERSION:
844 ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
845 return 0;
846 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300847 default:
848 filename = AR6003_REV3_OTP_FILE;
849 break;
850 }
851
852 if (ar->fw_otp == NULL) {
853 ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
854 &ar->fw_otp_len);
855 if (ret) {
856 ath6kl_err("Failed to get OTP file %s: %d\n",
857 filename, ret);
858 return ret;
859 }
860 }
861
862 address = ath6kl_get_load_address(ar->version.target_ver,
863 APP_LOAD_ADDR);
864
865 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
866 ar->fw_otp_len);
867 if (ret) {
868 ath6kl_err("Failed to upload OTP file: %d\n", ret);
869 return ret;
870 }
871
872 /* execute the OTP code */
873 param = 0;
874 address = ath6kl_get_load_address(ar->version.target_ver,
875 APP_START_OVERRIDE_ADDR);
876 ath6kl_bmi_execute(ar, address, &param);
877
878 return ret;
879}
880
881static int ath6kl_upload_firmware(struct ath6kl *ar)
882{
883 const char *filename;
884 u32 address;
885 int ret;
886
887 switch (ar->version.target_ver) {
888 case AR6003_REV2_VERSION:
889 filename = AR6003_REV2_FIRMWARE_FILE;
890 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800891 case AR6004_REV1_VERSION:
892 filename = AR6004_REV1_FIRMWARE_FILE;
893 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300894 default:
895 filename = AR6003_REV3_FIRMWARE_FILE;
896 break;
897 }
898
899 if (ar->fw == NULL) {
900 ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
901 if (ret) {
902 ath6kl_err("Failed to get firmware file %s: %d\n",
903 filename, ret);
904 return ret;
905 }
906 }
907
908 address = ath6kl_get_load_address(ar->version.target_ver,
909 APP_LOAD_ADDR);
910
911 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
912
913 if (ret) {
914 ath6kl_err("Failed to write firmware: %d\n", ret);
915 return ret;
916 }
917
Kevin Fang31024d92011-07-11 17:14:13 +0800918 /*
919 * Set starting address for firmware
920 * Don't need to setup app_start override addr on AR6004
921 */
922 if (ar->target_type != TARGET_TYPE_AR6004) {
923 address = ath6kl_get_load_address(ar->version.target_ver,
924 APP_START_OVERRIDE_ADDR);
925 ath6kl_bmi_set_app_start(ar, address);
926 }
Kalle Valobdcd8172011-07-18 00:22:30 +0300927 return ret;
928}
929
930static int ath6kl_upload_patch(struct ath6kl *ar)
931{
932 const char *filename;
933 u32 address, param;
934 int ret;
935
936 switch (ar->version.target_ver) {
937 case AR6003_REV2_VERSION:
938 filename = AR6003_REV2_PATCH_FILE;
939 break;
Kevin Fang31024d92011-07-11 17:14:13 +0800940 case AR6004_REV1_VERSION:
941 /* FIXME: implement for AR6004 */
942 return 0;
943 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300944 default:
945 filename = AR6003_REV3_PATCH_FILE;
946 break;
947 }
948
949 if (ar->fw_patch == NULL) {
950 ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
951 &ar->fw_patch_len);
952 if (ret) {
953 ath6kl_err("Failed to get patch file %s: %d\n",
954 filename, ret);
955 return ret;
956 }
957 }
958
959 address = ath6kl_get_load_address(ar->version.target_ver,
960 DATASET_PATCH_ADDR);
961
962 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
963 if (ret) {
964 ath6kl_err("Failed to write patch file: %d\n", ret);
965 return ret;
966 }
967
968 param = address;
969 ath6kl_bmi_write(ar,
970 ath6kl_get_hi_item_addr(ar,
971 HI_ITEM(hi_dset_list_head)),
972 (unsigned char *) &param, 4);
973
974 return 0;
975}
976
977static int ath6kl_init_upload(struct ath6kl *ar)
978{
979 u32 param, options, sleep, address;
980 int status = 0;
981
Kevin Fang31024d92011-07-11 17:14:13 +0800982 if (ar->target_type != TARGET_TYPE_AR6003 &&
983 ar->target_type != TARGET_TYPE_AR6004)
Kalle Valobdcd8172011-07-18 00:22:30 +0300984 return -EINVAL;
985
986 /* temporarily disable system sleep */
987 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
988 status = ath6kl_bmi_reg_read(ar, address, &param);
989 if (status)
990 return status;
991
992 options = param;
993
994 param |= ATH6KL_OPTION_SLEEP_DISABLE;
995 status = ath6kl_bmi_reg_write(ar, address, param);
996 if (status)
997 return status;
998
999 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1000 status = ath6kl_bmi_reg_read(ar, address, &param);
1001 if (status)
1002 return status;
1003
1004 sleep = param;
1005
1006 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1007 status = ath6kl_bmi_reg_write(ar, address, param);
1008 if (status)
1009 return status;
1010
1011 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1012 options, sleep);
1013
1014 /* program analog PLL register */
Kevin Fang31024d92011-07-11 17:14:13 +08001015 /* no need to control 40/44MHz clock on AR6004 */
1016 if (ar->target_type != TARGET_TYPE_AR6004) {
1017 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1018 0xF9104001);
Kalle Valobdcd8172011-07-18 00:22:30 +03001019
Kevin Fang31024d92011-07-11 17:14:13 +08001020 if (status)
1021 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +03001022
Kevin Fang31024d92011-07-11 17:14:13 +08001023 /* Run at 80/88MHz by default */
1024 param = SM(CPU_CLOCK_STANDARD, 1);
1025
1026 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1027 status = ath6kl_bmi_reg_write(ar, address, param);
1028 if (status)
1029 return status;
1030 }
Kalle Valobdcd8172011-07-18 00:22:30 +03001031
1032 param = 0;
1033 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1034 param = SM(LPO_CAL_ENABLE, 1);
1035 status = ath6kl_bmi_reg_write(ar, address, param);
1036 if (status)
1037 return status;
1038
1039 /* WAR to avoid SDIO CRC err */
1040 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1041 ath6kl_err("temporary war to avoid sdio crc error\n");
1042
1043 param = 0x20;
1044
1045 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1046 status = ath6kl_bmi_reg_write(ar, address, param);
1047 if (status)
1048 return status;
1049
1050 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1051 status = ath6kl_bmi_reg_write(ar, address, param);
1052 if (status)
1053 return status;
1054
1055 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1056 status = ath6kl_bmi_reg_write(ar, address, param);
1057 if (status)
1058 return status;
1059
1060 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1061 status = ath6kl_bmi_reg_write(ar, address, param);
1062 if (status)
1063 return status;
1064 }
1065
1066 /* write EEPROM data to Target RAM */
1067 status = ath6kl_upload_board_file(ar);
1068 if (status)
1069 return status;
1070
1071 /* transfer One time Programmable data */
1072 status = ath6kl_upload_otp(ar);
1073 if (status)
1074 return status;
1075
1076 /* Download Target firmware */
1077 status = ath6kl_upload_firmware(ar);
1078 if (status)
1079 return status;
1080
1081 status = ath6kl_upload_patch(ar);
1082 if (status)
1083 return status;
1084
1085 /* Restore system sleep */
1086 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1087 status = ath6kl_bmi_reg_write(ar, address, sleep);
1088 if (status)
1089 return status;
1090
1091 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1092 param = options | 0x20;
1093 status = ath6kl_bmi_reg_write(ar, address, param);
1094 if (status)
1095 return status;
1096
1097 /* Configure GPIO AR6003 UART */
1098 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1099 status = ath6kl_bmi_write(ar,
1100 ath6kl_get_hi_item_addr(ar,
1101 HI_ITEM(hi_dbg_uart_txpin)),
1102 (u8 *)&param, 4);
1103
1104 return status;
1105}
1106
1107static int ath6kl_init(struct net_device *dev)
1108{
1109 struct ath6kl *ar = ath6kl_priv(dev);
1110 int status = 0;
1111 s32 timeleft;
1112
1113 if (!ar)
1114 return -EIO;
1115
1116 /* Do we need to finish the BMI phase */
1117 if (ath6kl_bmi_done(ar)) {
1118 status = -EIO;
1119 goto ath6kl_init_done;
1120 }
1121
1122 /* Indicate that WMI is enabled (although not ready yet) */
1123 set_bit(WMI_ENABLED, &ar->flag);
Vasanthakumar Thiagarajan28657852011-07-21 12:00:49 +05301124 ar->wmi = ath6kl_wmi_init(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001125 if (!ar->wmi) {
1126 ath6kl_err("failed to initialize wmi\n");
1127 status = -EIO;
1128 goto ath6kl_init_done;
1129 }
1130
1131 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1132
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301133 wlan_node_table_init(&ar->scan_table);
1134
Kalle Valobdcd8172011-07-18 00:22:30 +03001135 /*
1136 * The reason we have to wait for the target here is that the
1137 * driver layer has to init BMI in order to set the host block
1138 * size.
1139 */
Kalle Vaload226ec2011-08-10 09:49:12 +03001140 if (ath6kl_htc_wait_target(ar->htc_target)) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001141 status = -EIO;
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301142 goto err_node_cleanup;
Kalle Valobdcd8172011-07-18 00:22:30 +03001143 }
1144
1145 if (ath6kl_init_service_ep(ar)) {
1146 status = -EIO;
1147 goto err_cleanup_scatter;
1148 }
1149
1150 /* setup access class priority mappings */
1151 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1152 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1153 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1154 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1155
1156 /* give our connected endpoints some buffers */
1157 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1158 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1159
1160 /* allocate some buffers that handle larger AMSDU frames */
1161 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1162
1163 /* setup credit distribution */
1164 ath6k_setup_credit_dist(ar->htc_target, &ar->credit_state_info);
1165
1166 ath6kl_cookie_init(ar);
1167
1168 /* start HTC */
Kalle Vaload226ec2011-08-10 09:49:12 +03001169 status = ath6kl_htc_start(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001170
1171 if (status) {
1172 ath6kl_cookie_cleanup(ar);
1173 goto err_rxbuf_cleanup;
1174 }
1175
1176 /* Wait for Wmi event to be ready */
1177 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1178 test_bit(WMI_READY,
1179 &ar->flag),
1180 WMI_TIMEOUT);
1181
1182 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1183 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1184 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1185 status = -EIO;
1186 goto err_htc_stop;
1187 }
1188
1189 if (!timeleft || signal_pending(current)) {
1190 ath6kl_err("wmi is not ready or wait was interrupted\n");
1191 status = -EIO;
1192 goto err_htc_stop;
1193 }
1194
1195 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1196
1197 /* communicate the wmi protocol verision to the target */
1198 if ((ath6kl_set_host_app_area(ar)) != 0)
1199 ath6kl_err("unable to set the host app area\n");
1200
1201 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1202 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1203
1204 status = ath6kl_target_config_wlan_params(ar);
1205 if (!status)
1206 goto ath6kl_init_done;
1207
1208err_htc_stop:
Kalle Vaload226ec2011-08-10 09:49:12 +03001209 ath6kl_htc_stop(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001210err_rxbuf_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001211 ath6kl_htc_flush_rx_buf(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001212 ath6kl_cleanup_amsdu_rxbufs(ar);
1213err_cleanup_scatter:
1214 ath6kl_hif_cleanup_scatter(ar);
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301215err_node_cleanup:
1216 wlan_node_table_cleanup(&ar->scan_table);
Kalle Valobdcd8172011-07-18 00:22:30 +03001217 ath6kl_wmi_shutdown(ar->wmi);
1218 clear_bit(WMI_ENABLED, &ar->flag);
1219 ar->wmi = NULL;
1220
1221ath6kl_init_done:
1222 return status;
1223}
1224
1225int ath6kl_core_init(struct ath6kl *ar)
1226{
1227 int ret = 0;
1228 struct ath6kl_bmi_target_info targ_info;
1229
1230 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1231 if (!ar->ath6kl_wq)
1232 return -ENOMEM;
1233
1234 ret = ath6kl_bmi_init(ar);
1235 if (ret)
1236 goto err_wq;
1237
1238 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1239 if (ret)
1240 goto err_bmi_cleanup;
1241
1242 ar->version.target_ver = le32_to_cpu(targ_info.version);
1243 ar->target_type = le32_to_cpu(targ_info.type);
1244 ar->wdev->wiphy->hw_version = le32_to_cpu(targ_info.version);
1245
1246 ret = ath6kl_configure_target(ar);
1247 if (ret)
1248 goto err_bmi_cleanup;
1249
Kalle Vaload226ec2011-08-10 09:49:12 +03001250 ar->htc_target = ath6kl_htc_create(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001251
1252 if (!ar->htc_target) {
1253 ret = -ENOMEM;
1254 goto err_bmi_cleanup;
1255 }
1256
1257 ar->aggr_cntxt = aggr_init(ar->net_dev);
1258 if (!ar->aggr_cntxt) {
1259 ath6kl_err("failed to initialize aggr\n");
1260 ret = -ENOMEM;
1261 goto err_htc_cleanup;
1262 }
1263
1264 ret = ath6kl_init_upload(ar);
1265 if (ret)
1266 goto err_htc_cleanup;
1267
1268 ret = ath6kl_init(ar->net_dev);
1269 if (ret)
1270 goto err_htc_cleanup;
1271
1272 /* This runs the init function if registered */
1273 ret = register_netdev(ar->net_dev);
1274 if (ret) {
1275 ath6kl_err("register_netdev failed\n");
1276 ath6kl_destroy(ar->net_dev, 0);
1277 return ret;
1278 }
1279
1280 set_bit(NETDEV_REGISTERED, &ar->flag);
1281
1282 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1283 __func__, ar->net_dev->name, ar->net_dev, ar);
1284
1285 return ret;
1286
1287err_htc_cleanup:
Kalle Vaload226ec2011-08-10 09:49:12 +03001288 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001289err_bmi_cleanup:
1290 ath6kl_bmi_cleanup(ar);
1291err_wq:
1292 destroy_workqueue(ar->ath6kl_wq);
1293 return ret;
1294}
1295
1296void ath6kl_stop_txrx(struct ath6kl *ar)
1297{
1298 struct net_device *ndev = ar->net_dev;
1299
1300 if (!ndev)
1301 return;
1302
1303 set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1304
1305 if (down_interruptible(&ar->sem)) {
1306 ath6kl_err("down_interruptible failed\n");
1307 return;
1308 }
1309
1310 if (ar->wlan_pwr_state != WLAN_POWER_STATE_CUT_PWR)
1311 ath6kl_stop_endpoint(ndev, false, true);
1312
Raja Mani575b5f32011-07-19 19:27:33 +05301313 clear_bit(WLAN_ENABLED, &ar->flag);
Kalle Valobdcd8172011-07-18 00:22:30 +03001314}
1315
1316/*
1317 * We need to differentiate between the surprise and planned removal of the
1318 * device because of the following consideration:
1319 *
1320 * - In case of surprise removal, the hcd already frees up the pending
1321 * for the device and hence there is no need to unregister the function
1322 * driver inorder to get these requests. For planned removal, the function
1323 * driver has to explicitly unregister itself to have the hcd return all the
1324 * pending requests before the data structures for the devices are freed up.
1325 * Note that as per the current implementation, the function driver will
1326 * end up releasing all the devices since there is no API to selectively
1327 * release a particular device.
1328 *
1329 * - Certain commands issued to the target can be skipped for surprise
1330 * removal since they will anyway not go through.
1331 */
1332void ath6kl_destroy(struct net_device *dev, unsigned int unregister)
1333{
1334 struct ath6kl *ar;
1335
1336 if (!dev || !ath6kl_priv(dev)) {
1337 ath6kl_err("failed to get device structure\n");
1338 return;
1339 }
1340
1341 ar = ath6kl_priv(dev);
1342
1343 destroy_workqueue(ar->ath6kl_wq);
1344
1345 if (ar->htc_target)
Kalle Vaload226ec2011-08-10 09:49:12 +03001346 ath6kl_htc_cleanup(ar->htc_target);
Kalle Valobdcd8172011-07-18 00:22:30 +03001347
1348 aggr_module_destroy(ar->aggr_cntxt);
1349
1350 ath6kl_cookie_cleanup(ar);
1351
1352 ath6kl_cleanup_amsdu_rxbufs(ar);
1353
1354 ath6kl_bmi_cleanup(ar);
1355
1356 if (unregister && test_bit(NETDEV_REGISTERED, &ar->flag)) {
1357 unregister_netdev(dev);
1358 clear_bit(NETDEV_REGISTERED, &ar->flag);
1359 }
1360
1361 free_netdev(dev);
1362
Vasanthakumar Thiagarajan852bd9d2011-07-21 14:24:54 +05301363 wlan_node_table_cleanup(&ar->scan_table);
1364
Raja Mani19703572011-08-04 19:26:30 +05301365 kfree(ar->fw_board);
1366 kfree(ar->fw_otp);
1367 kfree(ar->fw);
1368 kfree(ar->fw_patch);
1369
Kalle Valobdcd8172011-07-18 00:22:30 +03001370 ath6kl_cfg80211_deinit(ar);
1371}