]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/ath/ath6kl/init.c
ath9k: Support RSN Mesh
[~andy/linux] / drivers / net / wireless / ath / ath6kl / init.c
1
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/moduleparam.h>
19 #include <linux/errno.h>
20 #include <linux/of.h>
21 #include <linux/mmc/sdio_func.h>
22 #include "core.h"
23 #include "cfg80211.h"
24 #include "target.h"
25 #include "debug.h"
26 #include "hif-ops.h"
27
28 unsigned int debug_mask;
29 static unsigned int testmode;
30 static bool suspend_cutpower;
31
32 module_param(debug_mask, uint, 0644);
33 module_param(testmode, uint, 0644);
34 module_param(suspend_cutpower, bool, 0444);
35
36 /*
37  * Include definitions here that can be used to tune the WLAN module
38  * behavior. Different customers can tune the behavior as per their needs,
39  * here.
40  */
41
42 /*
43  * This configuration item enable/disable keepalive support.
44  * Keepalive support: In the absence of any data traffic to AP, null
45  * frames will be sent to the AP at periodic interval, to keep the association
46  * active. This configuration item defines the periodic interval.
47  * Use value of zero to disable keepalive support
48  * Default: 60 seconds
49  */
50 #define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
51
52 /*
53  * This configuration item sets the value of disconnect timeout
54  * Firmware delays sending the disconnec event to the host for this
55  * timeout after is gets disconnected from the current AP.
56  * If the firmware successly roams within the disconnect timeout
57  * it sends a new connect event
58  */
59 #define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
60
61 #define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
62
63 #define ATH6KL_DATA_OFFSET    64
64 struct sk_buff *ath6kl_buf_alloc(int size)
65 {
66         struct sk_buff *skb;
67         u16 reserved;
68
69         /* Add chacheline space at front and back of buffer */
70         reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
71                    sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
72         skb = dev_alloc_skb(size + reserved);
73
74         if (skb)
75                 skb_reserve(skb, reserved - L1_CACHE_BYTES);
76         return skb;
77 }
78
79 void ath6kl_init_profile_info(struct ath6kl_vif *vif)
80 {
81         vif->ssid_len = 0;
82         memset(vif->ssid, 0, sizeof(vif->ssid));
83
84         vif->dot11_auth_mode = OPEN_AUTH;
85         vif->auth_mode = NONE_AUTH;
86         vif->prwise_crypto = NONE_CRYPT;
87         vif->prwise_crypto_len = 0;
88         vif->grp_crypto = NONE_CRYPT;
89         vif->grp_crypto_len = 0;
90         memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
91         memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
92         memset(vif->bssid, 0, sizeof(vif->bssid));
93         vif->bss_ch = 0;
94 }
95
96 static int ath6kl_set_host_app_area(struct ath6kl *ar)
97 {
98         u32 address, data;
99         struct host_app_area host_app_area;
100
101         /* Fetch the address of the host_app_area_s
102          * instance in the host interest area */
103         address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
104         address = TARG_VTOP(ar->target_type, address);
105
106         if (ath6kl_diag_read32(ar, address, &data))
107                 return -EIO;
108
109         address = TARG_VTOP(ar->target_type, data);
110         host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
111         if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
112                               sizeof(struct host_app_area)))
113                 return -EIO;
114
115         return 0;
116 }
117
118 static inline void set_ac2_ep_map(struct ath6kl *ar,
119                                   u8 ac,
120                                   enum htc_endpoint_id ep)
121 {
122         ar->ac2ep_map[ac] = ep;
123         ar->ep2ac_map[ep] = ac;
124 }
125
126 /* connect to a service */
127 static int ath6kl_connectservice(struct ath6kl *ar,
128                                  struct htc_service_connect_req  *con_req,
129                                  char *desc)
130 {
131         int status;
132         struct htc_service_connect_resp response;
133
134         memset(&response, 0, sizeof(response));
135
136         status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
137         if (status) {
138                 ath6kl_err("failed to connect to %s service status:%d\n",
139                            desc, status);
140                 return status;
141         }
142
143         switch (con_req->svc_id) {
144         case WMI_CONTROL_SVC:
145                 if (test_bit(WMI_ENABLED, &ar->flag))
146                         ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
147                 ar->ctrl_ep = response.endpoint;
148                 break;
149         case WMI_DATA_BE_SVC:
150                 set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
151                 break;
152         case WMI_DATA_BK_SVC:
153                 set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
154                 break;
155         case WMI_DATA_VI_SVC:
156                 set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
157                 break;
158         case WMI_DATA_VO_SVC:
159                 set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
160                 break;
161         default:
162                 ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
163                 return -EINVAL;
164         }
165
166         return 0;
167 }
168
169 static int ath6kl_init_service_ep(struct ath6kl *ar)
170 {
171         struct htc_service_connect_req connect;
172
173         memset(&connect, 0, sizeof(connect));
174
175         /* these fields are the same for all service endpoints */
176         connect.ep_cb.rx = ath6kl_rx;
177         connect.ep_cb.rx_refill = ath6kl_rx_refill;
178         connect.ep_cb.tx_full = ath6kl_tx_queue_full;
179
180         /*
181          * Set the max queue depth so that our ath6kl_tx_queue_full handler
182          * gets called.
183         */
184         connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
185         connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
186         if (!connect.ep_cb.rx_refill_thresh)
187                 connect.ep_cb.rx_refill_thresh++;
188
189         /* connect to control service */
190         connect.svc_id = WMI_CONTROL_SVC;
191         if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
192                 return -EIO;
193
194         connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
195
196         /*
197          * Limit the HTC message size on the send path, although e can
198          * receive A-MSDU frames of 4K, we will only send ethernet-sized
199          * (802.3) frames on the send path.
200          */
201         connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
202
203         /*
204          * To reduce the amount of committed memory for larger A_MSDU
205          * frames, use the recv-alloc threshold mechanism for larger
206          * packets.
207          */
208         connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
209         connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
210
211         /*
212          * For the remaining data services set the connection flag to
213          * reduce dribbling, if configured to do so.
214          */
215         connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
216         connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
217         connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
218
219         connect.svc_id = WMI_DATA_BE_SVC;
220
221         if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
222                 return -EIO;
223
224         /* connect to back-ground map this to WMI LOW_PRI */
225         connect.svc_id = WMI_DATA_BK_SVC;
226         if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
227                 return -EIO;
228
229         /* connect to Video service, map this to to HI PRI */
230         connect.svc_id = WMI_DATA_VI_SVC;
231         if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
232                 return -EIO;
233
234         /*
235          * Connect to VO service, this is currently not mapped to a WMI
236          * priority stream due to historical reasons. WMI originally
237          * defined 3 priorities over 3 mailboxes We can change this when
238          * WMI is reworked so that priorities are not dependent on
239          * mailboxes.
240          */
241         connect.svc_id = WMI_DATA_VO_SVC;
242         if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
243                 return -EIO;
244
245         return 0;
246 }
247
248 void ath6kl_init_control_info(struct ath6kl_vif *vif)
249 {
250         ath6kl_init_profile_info(vif);
251         vif->def_txkey_index = 0;
252         memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
253         vif->ch_hint = 0;
254 }
255
256 /*
257  * Set HTC/Mbox operational parameters, this can only be called when the
258  * target is in the BMI phase.
259  */
260 static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
261                                  u8 htc_ctrl_buf)
262 {
263         int status;
264         u32 blk_size;
265
266         blk_size = ar->mbox_info.block_size;
267
268         if (htc_ctrl_buf)
269                 blk_size |=  ((u32)htc_ctrl_buf) << 16;
270
271         /* set the host interest area for the block size */
272         status = ath6kl_bmi_write(ar,
273                         ath6kl_get_hi_item_addr(ar,
274                         HI_ITEM(hi_mbox_io_block_sz)),
275                         (u8 *)&blk_size,
276                         4);
277         if (status) {
278                 ath6kl_err("bmi_write_memory for IO block size failed\n");
279                 goto out;
280         }
281
282         ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
283                    blk_size,
284                    ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
285
286         if (mbox_isr_yield_val) {
287                 /* set the host interest area for the mbox ISR yield limit */
288                 status = ath6kl_bmi_write(ar,
289                                 ath6kl_get_hi_item_addr(ar,
290                                 HI_ITEM(hi_mbox_isr_yield_limit)),
291                                 (u8 *)&mbox_isr_yield_val,
292                                 4);
293                 if (status) {
294                         ath6kl_err("bmi_write_memory for yield limit failed\n");
295                         goto out;
296                 }
297         }
298
299 out:
300         return status;
301 }
302
303 static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
304 {
305         int status = 0;
306         int ret;
307
308         /*
309          * Configure the device for rx dot11 header rules. "0,0" are the
310          * default values. Required if checksum offload is needed. Set
311          * RxMetaVersion to 2.
312          */
313         if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
314                                                ar->rx_meta_ver, 0, 0)) {
315                 ath6kl_err("unable to set the rx frame format\n");
316                 status = -EIO;
317         }
318
319         if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
320                 if ((ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
321                      IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
322                         ath6kl_err("unable to set power save fail event policy\n");
323                         status = -EIO;
324                 }
325
326         if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
327                 if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
328                      WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
329                         ath6kl_err("unable to set barker preamble policy\n");
330                         status = -EIO;
331                 }
332
333         if (ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
334                         WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
335                 ath6kl_err("unable to set keep alive interval\n");
336                 status = -EIO;
337         }
338
339         if (ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
340                         WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
341                 ath6kl_err("unable to set disconnect timeout\n");
342                 status = -EIO;
343         }
344
345         if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
346                 if (ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED)) {
347                         ath6kl_err("unable to set txop bursting\n");
348                         status = -EIO;
349                 }
350
351         /*
352          * FIXME: Make sure p2p configurations are not applied to
353          * non-p2p capable interfaces when multivif support is enabled.
354          */
355         if (ar->p2p) {
356                 ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
357                                               P2P_FLAG_CAPABILITIES_REQ |
358                                               P2P_FLAG_MACADDR_REQ |
359                                               P2P_FLAG_HMODEL_REQ);
360                 if (ret) {
361                         ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
362                                    "capabilities (%d) - assuming P2P not "
363                                    "supported\n", ret);
364                         ar->p2p = 0;
365                 }
366         }
367
368         /*
369          * FIXME: Make sure p2p configurations are not applied to
370          * non-p2p capable interfaces when multivif support is enabled.
371          */
372         if (ar->p2p) {
373                 /* Enable Probe Request reporting for P2P */
374                 ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
375                 if (ret) {
376                         ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
377                                    "Request reporting (%d)\n", ret);
378                 }
379         }
380
381         return status;
382 }
383
384 int ath6kl_configure_target(struct ath6kl *ar)
385 {
386         u32 param, ram_reserved_size;
387         u8 fw_iftype, fw_mode = 0, fw_submode = 0;
388         int i;
389
390         /*
391          * Note: Even though the firmware interface type is
392          * chosen as BSS_STA for all three interfaces, can
393          * be configured to IBSS/AP as long as the fw submode
394          * remains normal mode (0 - AP, STA and IBSS). But
395          * due to an target assert in firmware only one interface is
396          * configured for now.
397          */
398         fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
399
400         for (i = 0; i < MAX_NUM_VIF; i++)
401                 fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
402
403         /*
404          * By default, submodes :
405          *              vif[0] - AP/STA/IBSS
406          *              vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
407          *              vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
408          */
409
410         for (i = 0; i < ar->max_norm_iface; i++)
411                 fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
412                               (i * HI_OPTION_FW_SUBMODE_BITS);
413
414         for (i = ar->max_norm_iface; i < MAX_NUM_VIF; i++)
415                 fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
416                               (i * HI_OPTION_FW_SUBMODE_BITS);
417
418         /*
419          * FIXME: This needs to be removed once the multivif
420          * support is enabled.
421          */
422         if (ar->p2p)
423                 fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
424
425         param = HTC_PROTOCOL_VERSION;
426         if (ath6kl_bmi_write(ar,
427                              ath6kl_get_hi_item_addr(ar,
428                              HI_ITEM(hi_app_host_interest)),
429                              (u8 *)&param, 4) != 0) {
430                 ath6kl_err("bmi_write_memory for htc version failed\n");
431                 return -EIO;
432         }
433
434         /* set the firmware mode to STA/IBSS/AP */
435         param = 0;
436
437         if (ath6kl_bmi_read(ar,
438                             ath6kl_get_hi_item_addr(ar,
439                             HI_ITEM(hi_option_flag)),
440                             (u8 *)&param, 4) != 0) {
441                 ath6kl_err("bmi_read_memory for setting fwmode failed\n");
442                 return -EIO;
443         }
444
445         param |= (MAX_NUM_VIF << HI_OPTION_NUM_DEV_SHIFT);
446         param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
447         param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
448
449         param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
450         param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
451
452         if (ath6kl_bmi_write(ar,
453                              ath6kl_get_hi_item_addr(ar,
454                              HI_ITEM(hi_option_flag)),
455                              (u8 *)&param,
456                              4) != 0) {
457                 ath6kl_err("bmi_write_memory for setting fwmode failed\n");
458                 return -EIO;
459         }
460
461         ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
462
463         /*
464          * Hardcode the address use for the extended board data
465          * Ideally this should be pre-allocate by the OS at boot time
466          * But since it is a new feature and board data is loaded
467          * at init time, we have to workaround this from host.
468          * It is difficult to patch the firmware boot code,
469          * but possible in theory.
470          */
471
472         param = ar->hw.board_ext_data_addr;
473         ram_reserved_size = ar->hw.reserved_ram_size;
474
475         if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
476                                         HI_ITEM(hi_board_ext_data)),
477                              (u8 *)&param, 4) != 0) {
478                 ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
479                 return -EIO;
480         }
481
482         if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
483                                         HI_ITEM(hi_end_ram_reserve_sz)),
484                              (u8 *)&ram_reserved_size, 4) != 0) {
485                 ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
486                 return -EIO;
487         }
488
489         /* set the block size for the target */
490         if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
491                 /* use default number of control buffers */
492                 return -EIO;
493
494         return 0;
495 }
496
497 void ath6kl_core_free(struct ath6kl *ar)
498 {
499         wiphy_free(ar->wiphy);
500 }
501
502 void ath6kl_core_cleanup(struct ath6kl *ar)
503 {
504         ath6kl_hif_power_off(ar);
505
506         destroy_workqueue(ar->ath6kl_wq);
507
508         if (ar->htc_target)
509                 ath6kl_htc_cleanup(ar->htc_target);
510
511         ath6kl_cookie_cleanup(ar);
512
513         ath6kl_cleanup_amsdu_rxbufs(ar);
514
515         ath6kl_bmi_cleanup(ar);
516
517         ath6kl_debug_cleanup(ar);
518
519         kfree(ar->fw_board);
520         kfree(ar->fw_otp);
521         kfree(ar->fw);
522         kfree(ar->fw_patch);
523
524         ath6kl_deinit_ieee80211_hw(ar);
525 }
526
527 /* firmware upload */
528 static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
529                          u8 **fw, size_t *fw_len)
530 {
531         const struct firmware *fw_entry;
532         int ret;
533
534         ret = request_firmware(&fw_entry, filename, ar->dev);
535         if (ret)
536                 return ret;
537
538         *fw_len = fw_entry->size;
539         *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
540
541         if (*fw == NULL)
542                 ret = -ENOMEM;
543
544         release_firmware(fw_entry);
545
546         return ret;
547 }
548
549 #ifdef CONFIG_OF
550 static const char *get_target_ver_dir(const struct ath6kl *ar)
551 {
552         switch (ar->version.target_ver) {
553         case AR6003_REV1_VERSION:
554                 return "ath6k/AR6003/hw1.0";
555         case AR6003_REV2_VERSION:
556                 return "ath6k/AR6003/hw2.0";
557         case AR6003_REV3_VERSION:
558                 return "ath6k/AR6003/hw2.1.1";
559         }
560         ath6kl_warn("%s: unsupported target version 0x%x.\n", __func__,
561                     ar->version.target_ver);
562         return NULL;
563 }
564
565 /*
566  * Check the device tree for a board-id and use it to construct
567  * the pathname to the firmware file.  Used (for now) to find a
568  * fallback to the "bdata.bin" file--typically a symlink to the
569  * appropriate board-specific file.
570  */
571 static bool check_device_tree(struct ath6kl *ar)
572 {
573         static const char *board_id_prop = "atheros,board-id";
574         struct device_node *node;
575         char board_filename[64];
576         const char *board_id;
577         int ret;
578
579         for_each_compatible_node(node, NULL, "atheros,ath6kl") {
580                 board_id = of_get_property(node, board_id_prop, NULL);
581                 if (board_id == NULL) {
582                         ath6kl_warn("No \"%s\" property on %s node.\n",
583                                     board_id_prop, node->name);
584                         continue;
585                 }
586                 snprintf(board_filename, sizeof(board_filename),
587                          "%s/bdata.%s.bin", get_target_ver_dir(ar), board_id);
588
589                 ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
590                                     &ar->fw_board_len);
591                 if (ret) {
592                         ath6kl_err("Failed to get DT board file %s: %d\n",
593                                    board_filename, ret);
594                         continue;
595                 }
596                 return true;
597         }
598         return false;
599 }
600 #else
601 static bool check_device_tree(struct ath6kl *ar)
602 {
603         return false;
604 }
605 #endif /* CONFIG_OF */
606
607 static int ath6kl_fetch_board_file(struct ath6kl *ar)
608 {
609         const char *filename;
610         int ret;
611
612         if (ar->fw_board != NULL)
613                 return 0;
614
615         switch (ar->version.target_ver) {
616         case AR6003_REV2_VERSION:
617                 filename = AR6003_REV2_BOARD_DATA_FILE;
618                 break;
619         case AR6004_REV1_VERSION:
620                 filename = AR6004_REV1_BOARD_DATA_FILE;
621                 break;
622         default:
623                 filename = AR6003_REV3_BOARD_DATA_FILE;
624                 break;
625         }
626
627         ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
628                             &ar->fw_board_len);
629         if (ret == 0) {
630                 /* managed to get proper board file */
631                 return 0;
632         }
633
634         if (check_device_tree(ar)) {
635                 /* got board file from device tree */
636                 return 0;
637         }
638
639         /* there was no proper board file, try to use default instead */
640         ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
641                     filename, ret);
642
643         switch (ar->version.target_ver) {
644         case AR6003_REV2_VERSION:
645                 filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
646                 break;
647         case AR6004_REV1_VERSION:
648                 filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
649                 break;
650         default:
651                 filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
652                 break;
653         }
654
655         ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
656                             &ar->fw_board_len);
657         if (ret) {
658                 ath6kl_err("Failed to get default board file %s: %d\n",
659                            filename, ret);
660                 return ret;
661         }
662
663         ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
664         ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
665
666         return 0;
667 }
668
669 static int ath6kl_fetch_otp_file(struct ath6kl *ar)
670 {
671         const char *filename;
672         int ret;
673
674         if (ar->fw_otp != NULL)
675                 return 0;
676
677         switch (ar->version.target_ver) {
678         case AR6003_REV2_VERSION:
679                 filename = AR6003_REV2_OTP_FILE;
680                 break;
681         case AR6004_REV1_VERSION:
682                 ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
683                 return 0;
684                 break;
685         default:
686                 filename = AR6003_REV3_OTP_FILE;
687                 break;
688         }
689
690         ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
691                             &ar->fw_otp_len);
692         if (ret) {
693                 ath6kl_err("Failed to get OTP file %s: %d\n",
694                            filename, ret);
695                 return ret;
696         }
697
698         return 0;
699 }
700
701 static int ath6kl_fetch_fw_file(struct ath6kl *ar)
702 {
703         const char *filename;
704         int ret;
705
706         if (ar->fw != NULL)
707                 return 0;
708
709         if (testmode) {
710                 switch (ar->version.target_ver) {
711                 case AR6003_REV2_VERSION:
712                         filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
713                         break;
714                 case AR6003_REV3_VERSION:
715                         filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
716                         break;
717                 case AR6004_REV1_VERSION:
718                         ath6kl_warn("testmode not supported with ar6004\n");
719                         return -EOPNOTSUPP;
720                 default:
721                         ath6kl_warn("unknown target version: 0x%x\n",
722                                        ar->version.target_ver);
723                         return -EINVAL;
724                 }
725
726                 set_bit(TESTMODE, &ar->flag);
727
728                 goto get_fw;
729         }
730
731         switch (ar->version.target_ver) {
732         case AR6003_REV2_VERSION:
733                 filename = AR6003_REV2_FIRMWARE_FILE;
734                 break;
735         case AR6004_REV1_VERSION:
736                 filename = AR6004_REV1_FIRMWARE_FILE;
737                 break;
738         default:
739                 filename = AR6003_REV3_FIRMWARE_FILE;
740                 break;
741         }
742
743 get_fw:
744         ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
745         if (ret) {
746                 ath6kl_err("Failed to get firmware file %s: %d\n",
747                            filename, ret);
748                 return ret;
749         }
750
751         return 0;
752 }
753
754 static int ath6kl_fetch_patch_file(struct ath6kl *ar)
755 {
756         const char *filename;
757         int ret;
758
759         switch (ar->version.target_ver) {
760         case AR6003_REV2_VERSION:
761                 filename = AR6003_REV2_PATCH_FILE;
762                 break;
763         case AR6004_REV1_VERSION:
764                 /* FIXME: implement for AR6004 */
765                 return 0;
766                 break;
767         default:
768                 filename = AR6003_REV3_PATCH_FILE;
769                 break;
770         }
771
772         if (ar->fw_patch == NULL) {
773                 ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
774                                     &ar->fw_patch_len);
775                 if (ret) {
776                         ath6kl_err("Failed to get patch file %s: %d\n",
777                                    filename, ret);
778                         return ret;
779                 }
780         }
781
782         return 0;
783 }
784
785 static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
786 {
787         int ret;
788
789         ret = ath6kl_fetch_otp_file(ar);
790         if (ret)
791                 return ret;
792
793         ret = ath6kl_fetch_fw_file(ar);
794         if (ret)
795                 return ret;
796
797         ret = ath6kl_fetch_patch_file(ar);
798         if (ret)
799                 return ret;
800
801         return 0;
802 }
803
804 static int ath6kl_fetch_fw_api2(struct ath6kl *ar)
805 {
806         size_t magic_len, len, ie_len;
807         const struct firmware *fw;
808         struct ath6kl_fw_ie *hdr;
809         const char *filename;
810         const u8 *data;
811         int ret, ie_id, i, index, bit;
812         __le32 *val;
813
814         switch (ar->version.target_ver) {
815         case AR6003_REV2_VERSION:
816                 filename = AR6003_REV2_FIRMWARE_2_FILE;
817                 break;
818         case AR6003_REV3_VERSION:
819                 filename = AR6003_REV3_FIRMWARE_2_FILE;
820                 break;
821         case AR6004_REV1_VERSION:
822                 filename = AR6004_REV1_FIRMWARE_2_FILE;
823                 break;
824         default:
825                 return -EOPNOTSUPP;
826         }
827
828         ret = request_firmware(&fw, filename, ar->dev);
829         if (ret)
830                 return ret;
831
832         data = fw->data;
833         len = fw->size;
834
835         /* magic also includes the null byte, check that as well */
836         magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
837
838         if (len < magic_len) {
839                 ret = -EINVAL;
840                 goto out;
841         }
842
843         if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
844                 ret = -EINVAL;
845                 goto out;
846         }
847
848         len -= magic_len;
849         data += magic_len;
850
851         /* loop elements */
852         while (len > sizeof(struct ath6kl_fw_ie)) {
853                 /* hdr is unaligned! */
854                 hdr = (struct ath6kl_fw_ie *) data;
855
856                 ie_id = le32_to_cpup(&hdr->id);
857                 ie_len = le32_to_cpup(&hdr->len);
858
859                 len -= sizeof(*hdr);
860                 data += sizeof(*hdr);
861
862                 if (len < ie_len) {
863                         ret = -EINVAL;
864                         goto out;
865                 }
866
867                 switch (ie_id) {
868                 case ATH6KL_FW_IE_OTP_IMAGE:
869                         ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
870                                 ie_len);
871
872                         ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
873
874                         if (ar->fw_otp == NULL) {
875                                 ret = -ENOMEM;
876                                 goto out;
877                         }
878
879                         ar->fw_otp_len = ie_len;
880                         break;
881                 case ATH6KL_FW_IE_FW_IMAGE:
882                         ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
883                                 ie_len);
884
885                         ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
886
887                         if (ar->fw == NULL) {
888                                 ret = -ENOMEM;
889                                 goto out;
890                         }
891
892                         ar->fw_len = ie_len;
893                         break;
894                 case ATH6KL_FW_IE_PATCH_IMAGE:
895                         ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
896                                 ie_len);
897
898                         ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
899
900                         if (ar->fw_patch == NULL) {
901                                 ret = -ENOMEM;
902                                 goto out;
903                         }
904
905                         ar->fw_patch_len = ie_len;
906                         break;
907                 case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
908                         val = (__le32 *) data;
909                         ar->hw.reserved_ram_size = le32_to_cpup(val);
910
911                         ath6kl_dbg(ATH6KL_DBG_BOOT,
912                                    "found reserved ram size ie 0x%d\n",
913                                    ar->hw.reserved_ram_size);
914                         break;
915                 case ATH6KL_FW_IE_CAPABILITIES:
916                         ath6kl_dbg(ATH6KL_DBG_BOOT,
917                                    "found firmware capabilities ie (%zd B)\n",
918                                    ie_len);
919
920                         for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
921                                 index = ALIGN(i, 8) / 8;
922                                 bit = i % 8;
923
924                                 if (data[index] & (1 << bit))
925                                         __set_bit(i, ar->fw_capabilities);
926                         }
927
928                         ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
929                                         ar->fw_capabilities,
930                                         sizeof(ar->fw_capabilities));
931                         break;
932                 case ATH6KL_FW_IE_PATCH_ADDR:
933                         if (ie_len != sizeof(*val))
934                                 break;
935
936                         val = (__le32 *) data;
937                         ar->hw.dataset_patch_addr = le32_to_cpup(val);
938
939                         ath6kl_dbg(ATH6KL_DBG_BOOT,
940                                    "found patch address ie 0x%d\n",
941                                    ar->hw.dataset_patch_addr);
942                         break;
943                 default:
944                         ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
945                                    le32_to_cpup(&hdr->id));
946                         break;
947                 }
948
949                 len -= ie_len;
950                 data += ie_len;
951         };
952
953         ret = 0;
954 out:
955         release_firmware(fw);
956
957         return ret;
958 }
959
960 static int ath6kl_fetch_firmwares(struct ath6kl *ar)
961 {
962         int ret;
963
964         ret = ath6kl_fetch_board_file(ar);
965         if (ret)
966                 return ret;
967
968         ret = ath6kl_fetch_fw_api2(ar);
969         if (ret == 0) {
970                 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 2\n");
971                 return 0;
972         }
973
974         ret = ath6kl_fetch_fw_api1(ar);
975         if (ret)
976                 return ret;
977
978         ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 1\n");
979
980         return 0;
981 }
982
983 static int ath6kl_upload_board_file(struct ath6kl *ar)
984 {
985         u32 board_address, board_ext_address, param;
986         u32 board_data_size, board_ext_data_size;
987         int ret;
988
989         if (WARN_ON(ar->fw_board == NULL))
990                 return -ENOENT;
991
992         /*
993          * Determine where in Target RAM to write Board Data.
994          * For AR6004, host determine Target RAM address for
995          * writing board data.
996          */
997         if (ar->target_type == TARGET_TYPE_AR6004) {
998                 board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
999                 ath6kl_bmi_write(ar,
1000                                 ath6kl_get_hi_item_addr(ar,
1001                                 HI_ITEM(hi_board_data)),
1002                                 (u8 *) &board_address, 4);
1003         } else {
1004                 ath6kl_bmi_read(ar,
1005                                 ath6kl_get_hi_item_addr(ar,
1006                                 HI_ITEM(hi_board_data)),
1007                                 (u8 *) &board_address, 4);
1008         }
1009
1010         /* determine where in target ram to write extended board data */
1011         ath6kl_bmi_read(ar,
1012                         ath6kl_get_hi_item_addr(ar,
1013                         HI_ITEM(hi_board_ext_data)),
1014                         (u8 *) &board_ext_address, 4);
1015
1016         if (board_ext_address == 0) {
1017                 ath6kl_err("Failed to get board file target address.\n");
1018                 return -EINVAL;
1019         }
1020
1021         switch (ar->target_type) {
1022         case TARGET_TYPE_AR6003:
1023                 board_data_size = AR6003_BOARD_DATA_SZ;
1024                 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1025                 break;
1026         case TARGET_TYPE_AR6004:
1027                 board_data_size = AR6004_BOARD_DATA_SZ;
1028                 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
1029                 break;
1030         default:
1031                 WARN_ON(1);
1032                 return -EINVAL;
1033                 break;
1034         }
1035
1036         if (ar->fw_board_len == (board_data_size +
1037                                  board_ext_data_size)) {
1038
1039                 /* write extended board data */
1040                 ath6kl_dbg(ATH6KL_DBG_BOOT,
1041                            "writing extended board data to 0x%x (%d B)\n",
1042                            board_ext_address, board_ext_data_size);
1043
1044                 ret = ath6kl_bmi_write(ar, board_ext_address,
1045                                        ar->fw_board + board_data_size,
1046                                        board_ext_data_size);
1047                 if (ret) {
1048                         ath6kl_err("Failed to write extended board data: %d\n",
1049                                    ret);
1050                         return ret;
1051                 }
1052
1053                 /* record that extended board data is initialized */
1054                 param = (board_ext_data_size << 16) | 1;
1055
1056                 ath6kl_bmi_write(ar,
1057                                  ath6kl_get_hi_item_addr(ar,
1058                                  HI_ITEM(hi_board_ext_data_config)),
1059                                  (unsigned char *) &param, 4);
1060         }
1061
1062         if (ar->fw_board_len < board_data_size) {
1063                 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1064                 ret = -EINVAL;
1065                 return ret;
1066         }
1067
1068         ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
1069                    board_address, board_data_size);
1070
1071         ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
1072                                board_data_size);
1073
1074         if (ret) {
1075                 ath6kl_err("Board file bmi write failed: %d\n", ret);
1076                 return ret;
1077         }
1078
1079         /* record the fact that Board Data IS initialized */
1080         param = 1;
1081         ath6kl_bmi_write(ar,
1082                          ath6kl_get_hi_item_addr(ar,
1083                          HI_ITEM(hi_board_data_initialized)),
1084                          (u8 *)&param, 4);
1085
1086         return ret;
1087 }
1088
1089 static int ath6kl_upload_otp(struct ath6kl *ar)
1090 {
1091         u32 address, param;
1092         bool from_hw = false;
1093         int ret;
1094
1095         if (WARN_ON(ar->fw_otp == NULL))
1096                 return -ENOENT;
1097
1098         address = ar->hw.app_load_addr;
1099
1100         ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
1101                    ar->fw_otp_len);
1102
1103         ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1104                                        ar->fw_otp_len);
1105         if (ret) {
1106                 ath6kl_err("Failed to upload OTP file: %d\n", ret);
1107                 return ret;
1108         }
1109
1110         /* read firmware start address */
1111         ret = ath6kl_bmi_read(ar,
1112                               ath6kl_get_hi_item_addr(ar,
1113                                                       HI_ITEM(hi_app_start)),
1114                               (u8 *) &address, sizeof(address));
1115
1116         if (ret) {
1117                 ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1118                 return ret;
1119         }
1120
1121         if (ar->hw.app_start_override_addr == 0) {
1122                 ar->hw.app_start_override_addr = address;
1123                 from_hw = true;
1124         }
1125
1126         ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1127                    from_hw ? " (from hw)" : "",
1128                    ar->hw.app_start_override_addr);
1129
1130         /* execute the OTP code */
1131         ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1132                    ar->hw.app_start_override_addr);
1133         param = 0;
1134         ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
1135
1136         return ret;
1137 }
1138
1139 static int ath6kl_upload_firmware(struct ath6kl *ar)
1140 {
1141         u32 address;
1142         int ret;
1143
1144         if (WARN_ON(ar->fw == NULL))
1145                 return -ENOENT;
1146
1147         address = ar->hw.app_load_addr;
1148
1149         ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
1150                    address, ar->fw_len);
1151
1152         ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1153
1154         if (ret) {
1155                 ath6kl_err("Failed to write firmware: %d\n", ret);
1156                 return ret;
1157         }
1158
1159         /*
1160          * Set starting address for firmware
1161          * Don't need to setup app_start override addr on AR6004
1162          */
1163         if (ar->target_type != TARGET_TYPE_AR6004) {
1164                 address = ar->hw.app_start_override_addr;
1165                 ath6kl_bmi_set_app_start(ar, address);
1166         }
1167         return ret;
1168 }
1169
1170 static int ath6kl_upload_patch(struct ath6kl *ar)
1171 {
1172         u32 address, param;
1173         int ret;
1174
1175         if (WARN_ON(ar->fw_patch == NULL))
1176                 return -ENOENT;
1177
1178         address = ar->hw.dataset_patch_addr;
1179
1180         ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
1181                    address, ar->fw_patch_len);
1182
1183         ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1184         if (ret) {
1185                 ath6kl_err("Failed to write patch file: %d\n", ret);
1186                 return ret;
1187         }
1188
1189         param = address;
1190         ath6kl_bmi_write(ar,
1191                          ath6kl_get_hi_item_addr(ar,
1192                          HI_ITEM(hi_dset_list_head)),
1193                          (unsigned char *) &param, 4);
1194
1195         return 0;
1196 }
1197
1198 static int ath6kl_init_upload(struct ath6kl *ar)
1199 {
1200         u32 param, options, sleep, address;
1201         int status = 0;
1202
1203         if (ar->target_type != TARGET_TYPE_AR6003 &&
1204                 ar->target_type != TARGET_TYPE_AR6004)
1205                 return -EINVAL;
1206
1207         /* temporarily disable system sleep */
1208         address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1209         status = ath6kl_bmi_reg_read(ar, address, &param);
1210         if (status)
1211                 return status;
1212
1213         options = param;
1214
1215         param |= ATH6KL_OPTION_SLEEP_DISABLE;
1216         status = ath6kl_bmi_reg_write(ar, address, param);
1217         if (status)
1218                 return status;
1219
1220         address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1221         status = ath6kl_bmi_reg_read(ar, address, &param);
1222         if (status)
1223                 return status;
1224
1225         sleep = param;
1226
1227         param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1228         status = ath6kl_bmi_reg_write(ar, address, param);
1229         if (status)
1230                 return status;
1231
1232         ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1233                    options, sleep);
1234
1235         /* program analog PLL register */
1236         /* no need to control 40/44MHz clock on AR6004 */
1237         if (ar->target_type != TARGET_TYPE_AR6004) {
1238                 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1239                                               0xF9104001);
1240
1241                 if (status)
1242                         return status;
1243
1244                 /* Run at 80/88MHz by default */
1245                 param = SM(CPU_CLOCK_STANDARD, 1);
1246
1247                 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1248                 status = ath6kl_bmi_reg_write(ar, address, param);
1249                 if (status)
1250                         return status;
1251         }
1252
1253         param = 0;
1254         address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1255         param = SM(LPO_CAL_ENABLE, 1);
1256         status = ath6kl_bmi_reg_write(ar, address, param);
1257         if (status)
1258                 return status;
1259
1260         /* WAR to avoid SDIO CRC err */
1261         if (ar->version.target_ver == AR6003_REV2_VERSION) {
1262                 ath6kl_err("temporary war to avoid sdio crc error\n");
1263
1264                 param = 0x20;
1265
1266                 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1267                 status = ath6kl_bmi_reg_write(ar, address, param);
1268                 if (status)
1269                         return status;
1270
1271                 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1272                 status = ath6kl_bmi_reg_write(ar, address, param);
1273                 if (status)
1274                         return status;
1275
1276                 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1277                 status = ath6kl_bmi_reg_write(ar, address, param);
1278                 if (status)
1279                         return status;
1280
1281                 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1282                 status = ath6kl_bmi_reg_write(ar, address, param);
1283                 if (status)
1284                         return status;
1285         }
1286
1287         /* write EEPROM data to Target RAM */
1288         status = ath6kl_upload_board_file(ar);
1289         if (status)
1290                 return status;
1291
1292         /* transfer One time Programmable data */
1293         status = ath6kl_upload_otp(ar);
1294         if (status)
1295                 return status;
1296
1297         /* Download Target firmware */
1298         status = ath6kl_upload_firmware(ar);
1299         if (status)
1300                 return status;
1301
1302         status = ath6kl_upload_patch(ar);
1303         if (status)
1304                 return status;
1305
1306         /* Restore system sleep */
1307         address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1308         status = ath6kl_bmi_reg_write(ar, address, sleep);
1309         if (status)
1310                 return status;
1311
1312         address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1313         param = options | 0x20;
1314         status = ath6kl_bmi_reg_write(ar, address, param);
1315         if (status)
1316                 return status;
1317
1318         /* Configure GPIO AR6003 UART */
1319         param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1320         status = ath6kl_bmi_write(ar,
1321                                   ath6kl_get_hi_item_addr(ar,
1322                                   HI_ITEM(hi_dbg_uart_txpin)),
1323                                   (u8 *)&param, 4);
1324
1325         return status;
1326 }
1327
1328 static int ath6kl_init_hw_params(struct ath6kl *ar)
1329 {
1330         switch (ar->version.target_ver) {
1331         case AR6003_REV2_VERSION:
1332                 ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
1333                 ar->hw.app_load_addr = AR6003_REV2_APP_LOAD_ADDRESS;
1334                 ar->hw.board_ext_data_addr = AR6003_REV2_BOARD_EXT_DATA_ADDRESS;
1335                 ar->hw.reserved_ram_size = AR6003_REV2_RAM_RESERVE_SIZE;
1336
1337                 /* hw2.0 needs override address hardcoded */
1338                 ar->hw.app_start_override_addr = 0x944C00;
1339
1340                 break;
1341         case AR6003_REV3_VERSION:
1342                 ar->hw.dataset_patch_addr = AR6003_REV3_DATASET_PATCH_ADDRESS;
1343                 ar->hw.app_load_addr = 0x1234;
1344                 ar->hw.board_ext_data_addr = AR6003_REV3_BOARD_EXT_DATA_ADDRESS;
1345                 ar->hw.reserved_ram_size = AR6003_REV3_RAM_RESERVE_SIZE;
1346                 break;
1347         case AR6004_REV1_VERSION:
1348                 ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
1349                 ar->hw.app_load_addr = AR6003_REV3_APP_LOAD_ADDRESS;
1350                 ar->hw.board_ext_data_addr = AR6004_REV1_BOARD_EXT_DATA_ADDRESS;
1351                 ar->hw.reserved_ram_size = AR6004_REV1_RAM_RESERVE_SIZE;
1352                 break;
1353         default:
1354                 ath6kl_err("Unsupported hardware version: 0x%x\n",
1355                            ar->version.target_ver);
1356                 return -EINVAL;
1357         }
1358
1359         ath6kl_dbg(ATH6KL_DBG_BOOT,
1360                    "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
1361                    ar->version.target_ver, ar->target_type,
1362                    ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
1363         ath6kl_dbg(ATH6KL_DBG_BOOT,
1364                    "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
1365                    ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
1366                    ar->hw.reserved_ram_size);
1367
1368         return 0;
1369 }
1370
1371 int ath6kl_init_hw_start(struct ath6kl *ar)
1372 {
1373         long timeleft;
1374         int ret, i;
1375
1376         ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
1377
1378         ret = ath6kl_hif_power_on(ar);
1379         if (ret)
1380                 return ret;
1381
1382         ret = ath6kl_configure_target(ar);
1383         if (ret)
1384                 goto err_power_off;
1385
1386         ret = ath6kl_init_upload(ar);
1387         if (ret)
1388                 goto err_power_off;
1389
1390         /* Do we need to finish the BMI phase */
1391         /* FIXME: return error from ath6kl_bmi_done() */
1392         if (ath6kl_bmi_done(ar)) {
1393                 ret = -EIO;
1394                 goto err_power_off;
1395         }
1396
1397         /*
1398          * The reason we have to wait for the target here is that the
1399          * driver layer has to init BMI in order to set the host block
1400          * size.
1401          */
1402         if (ath6kl_htc_wait_target(ar->htc_target)) {
1403                 ret = -EIO;
1404                 goto err_power_off;
1405         }
1406
1407         if (ath6kl_init_service_ep(ar)) {
1408                 ret = -EIO;
1409                 goto err_cleanup_scatter;
1410         }
1411
1412         /* setup credit distribution */
1413         ath6kl_credit_setup(ar->htc_target, &ar->credit_state_info);
1414
1415         /* start HTC */
1416         ret = ath6kl_htc_start(ar->htc_target);
1417         if (ret) {
1418                 /* FIXME: call this */
1419                 ath6kl_cookie_cleanup(ar);
1420                 goto err_cleanup_scatter;
1421         }
1422
1423         /* Wait for Wmi event to be ready */
1424         timeleft = wait_event_interruptible_timeout(ar->event_wq,
1425                                                     test_bit(WMI_READY,
1426                                                              &ar->flag),
1427                                                     WMI_TIMEOUT);
1428
1429         ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
1430
1431         if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1432                 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1433                            ATH6KL_ABI_VERSION, ar->version.abi_ver);
1434                 ret = -EIO;
1435                 goto err_htc_stop;
1436         }
1437
1438         if (!timeleft || signal_pending(current)) {
1439                 ath6kl_err("wmi is not ready or wait was interrupted\n");
1440                 ret = -EIO;
1441                 goto err_htc_stop;
1442         }
1443
1444         ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1445
1446         /* communicate the wmi protocol verision to the target */
1447         /* FIXME: return error */
1448         if ((ath6kl_set_host_app_area(ar)) != 0)
1449                 ath6kl_err("unable to set the host app area\n");
1450
1451         for (i = 0; i < MAX_NUM_VIF; i++) {
1452                 ret = ath6kl_target_config_wlan_params(ar, i);
1453                 if (ret)
1454                         goto err_htc_stop;
1455         }
1456
1457         ar->state = ATH6KL_STATE_ON;
1458
1459         return 0;
1460
1461 err_htc_stop:
1462         ath6kl_htc_stop(ar->htc_target);
1463 err_cleanup_scatter:
1464         ath6kl_hif_cleanup_scatter(ar);
1465 err_power_off:
1466         ath6kl_hif_power_off(ar);
1467
1468         return ret;
1469 }
1470
1471 int ath6kl_init_hw_stop(struct ath6kl *ar)
1472 {
1473         int ret;
1474
1475         ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
1476
1477         ath6kl_htc_stop(ar->htc_target);
1478
1479         ath6kl_hif_stop(ar);
1480
1481         ath6kl_bmi_reset(ar);
1482
1483         ret = ath6kl_hif_power_off(ar);
1484         if (ret)
1485                 ath6kl_warn("failed to power off hif: %d\n", ret);
1486
1487         ar->state = ATH6KL_STATE_OFF;
1488
1489         return 0;
1490 }
1491
1492 int ath6kl_core_init(struct ath6kl *ar)
1493 {
1494         struct ath6kl_bmi_target_info targ_info;
1495         struct net_device *ndev;
1496         int ret = 0, i;
1497
1498         ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1499         if (!ar->ath6kl_wq)
1500                 return -ENOMEM;
1501
1502         ret = ath6kl_bmi_init(ar);
1503         if (ret)
1504                 goto err_wq;
1505
1506         /*
1507          * Turn on power to get hardware (target) version and leave power
1508          * on delibrately as we will boot the hardware anyway within few
1509          * seconds.
1510          */
1511         ret = ath6kl_hif_power_on(ar);
1512         if (ret)
1513                 goto err_bmi_cleanup;
1514
1515         ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1516         if (ret)
1517                 goto err_power_off;
1518
1519         ar->version.target_ver = le32_to_cpu(targ_info.version);
1520         ar->target_type = le32_to_cpu(targ_info.type);
1521         ar->wiphy->hw_version = le32_to_cpu(targ_info.version);
1522
1523         ret = ath6kl_init_hw_params(ar);
1524         if (ret)
1525                 goto err_power_off;
1526
1527         ar->htc_target = ath6kl_htc_create(ar);
1528
1529         if (!ar->htc_target) {
1530                 ret = -ENOMEM;
1531                 goto err_power_off;
1532         }
1533
1534         ret = ath6kl_fetch_firmwares(ar);
1535         if (ret)
1536                 goto err_htc_cleanup;
1537
1538         /* FIXME: we should free all firmwares in the error cases below */
1539
1540         /* Indicate that WMI is enabled (although not ready yet) */
1541         set_bit(WMI_ENABLED, &ar->flag);
1542         ar->wmi = ath6kl_wmi_init(ar);
1543         if (!ar->wmi) {
1544                 ath6kl_err("failed to initialize wmi\n");
1545                 ret = -EIO;
1546                 goto err_htc_cleanup;
1547         }
1548
1549         ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1550
1551         ret = ath6kl_register_ieee80211_hw(ar);
1552         if (ret)
1553                 goto err_node_cleanup;
1554
1555         ret = ath6kl_debug_init(ar);
1556         if (ret) {
1557                 wiphy_unregister(ar->wiphy);
1558                 goto err_node_cleanup;
1559         }
1560
1561         for (i = 0; i < MAX_NUM_VIF; i++)
1562                 ar->avail_idx_map |= BIT(i);
1563
1564         rtnl_lock();
1565
1566         /* Add an initial station interface */
1567         ndev = ath6kl_interface_add(ar, "wlan%d", NL80211_IFTYPE_STATION, 0,
1568                                     INFRA_NETWORK);
1569
1570         rtnl_unlock();
1571
1572         if (!ndev) {
1573                 ath6kl_err("Failed to instantiate a network device\n");
1574                 ret = -ENOMEM;
1575                 wiphy_unregister(ar->wiphy);
1576                 goto err_debug_init;
1577         }
1578
1579
1580         ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1581                         __func__, ndev->name, ndev, ar);
1582
1583         /* setup access class priority mappings */
1584         ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest  */
1585         ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1586         ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1587         ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1588
1589         /* give our connected endpoints some buffers */
1590         ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1591         ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1592
1593         /* allocate some buffers that handle larger AMSDU frames */
1594         ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1595
1596         ath6kl_cookie_init(ar);
1597
1598         ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1599                          ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1600
1601         if (suspend_cutpower)
1602                 ar->conf_flags |= ATH6KL_CONF_SUSPEND_CUTPOWER;
1603
1604         ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM |
1605                             WIPHY_FLAG_HAVE_AP_SME |
1606                             WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
1607
1608         set_bit(FIRST_BOOT, &ar->flag);
1609
1610         ret = ath6kl_init_hw_start(ar);
1611         if (ret) {
1612                 ath6kl_err("Failed to start hardware: %d\n", ret);
1613                 goto err_rxbuf_cleanup;
1614         }
1615
1616         /*
1617          * Set mac address which is received in ready event
1618          * FIXME: Move to ath6kl_interface_add()
1619          */
1620         memcpy(ndev->dev_addr, ar->mac_addr, ETH_ALEN);
1621
1622         return ret;
1623
1624 err_rxbuf_cleanup:
1625         ath6kl_htc_flush_rx_buf(ar->htc_target);
1626         ath6kl_cleanup_amsdu_rxbufs(ar);
1627         rtnl_lock();
1628         ath6kl_deinit_if_data(netdev_priv(ndev));
1629         rtnl_unlock();
1630         wiphy_unregister(ar->wiphy);
1631 err_debug_init:
1632         ath6kl_debug_cleanup(ar);
1633 err_node_cleanup:
1634         ath6kl_wmi_shutdown(ar->wmi);
1635         clear_bit(WMI_ENABLED, &ar->flag);
1636         ar->wmi = NULL;
1637 err_htc_cleanup:
1638         ath6kl_htc_cleanup(ar->htc_target);
1639 err_power_off:
1640         ath6kl_hif_power_off(ar);
1641 err_bmi_cleanup:
1642         ath6kl_bmi_cleanup(ar);
1643 err_wq:
1644         destroy_workqueue(ar->ath6kl_wq);
1645
1646         return ret;
1647 }
1648
1649 void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
1650 {
1651         static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1652         bool discon_issued;
1653
1654         netif_stop_queue(vif->ndev);
1655
1656         clear_bit(WLAN_ENABLED, &vif->flags);
1657
1658         if (wmi_ready) {
1659                 discon_issued = test_bit(CONNECTED, &vif->flags) ||
1660                                 test_bit(CONNECT_PEND, &vif->flags);
1661                 ath6kl_disconnect(vif);
1662                 del_timer(&vif->disconnect_timer);
1663
1664                 if (discon_issued)
1665                         ath6kl_disconnect_event(vif, DISCONNECT_CMD,
1666                                                 (vif->nw_type & AP_NETWORK) ?
1667                                                 bcast_mac : vif->bssid,
1668                                                 0, NULL, 0);
1669         }
1670
1671         if (vif->scan_req) {
1672                 cfg80211_scan_done(vif->scan_req, true);
1673                 vif->scan_req = NULL;
1674         }
1675 }
1676
1677 void ath6kl_stop_txrx(struct ath6kl *ar)
1678 {
1679         struct ath6kl_vif *vif, *tmp_vif;
1680
1681         set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1682
1683         if (down_interruptible(&ar->sem)) {
1684                 ath6kl_err("down_interruptible failed\n");
1685                 return;
1686         }
1687
1688         spin_lock_bh(&ar->list_lock);
1689         list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1690                 list_del(&vif->list);
1691                 spin_unlock_bh(&ar->list_lock);
1692                 ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
1693                 rtnl_lock();
1694                 ath6kl_deinit_if_data(vif);
1695                 rtnl_unlock();
1696                 spin_lock_bh(&ar->list_lock);
1697         }
1698         spin_unlock_bh(&ar->list_lock);
1699
1700         clear_bit(WMI_READY, &ar->flag);
1701
1702         /*
1703          * After wmi_shudown all WMI events will be dropped. We
1704          * need to cleanup the buffers allocated in AP mode and
1705          * give disconnect notification to stack, which usually
1706          * happens in the disconnect_event. Simulate the disconnect
1707          * event by calling the function directly. Sometimes
1708          * disconnect_event will be received when the debug logs
1709          * are collected.
1710          */
1711         ath6kl_wmi_shutdown(ar->wmi);
1712
1713         clear_bit(WMI_ENABLED, &ar->flag);
1714         if (ar->htc_target) {
1715                 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
1716                 ath6kl_htc_stop(ar->htc_target);
1717         }
1718
1719         /*
1720          * Try to reset the device if we can. The driver may have been
1721          * configure NOT to reset the target during a debug session.
1722          */
1723         ath6kl_dbg(ATH6KL_DBG_TRC,
1724                         "attempting to reset target on instance destroy\n");
1725         ath6kl_reset_device(ar, ar->target_type, true, true);
1726
1727         clear_bit(WLAN_ENABLED, &ar->flag);
1728 }