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