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