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