5acb4048b008af886d8ade971fef201dbc781ea2
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / core.c
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, 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/module.h>
19 #include <linux/firmware.h>
20
21 #include "core.h"
22 #include "mac.h"
23 #include "htc.h"
24 #include "hif.h"
25 #include "wmi.h"
26 #include "bmi.h"
27 #include "debug.h"
28 #include "htt.h"
29
30 unsigned int ath10k_debug_mask;
31 static bool uart_print;
32 static unsigned int ath10k_p2p;
33 module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
34 module_param(uart_print, bool, 0644);
35 module_param_named(p2p, ath10k_p2p, uint, 0644);
36 MODULE_PARM_DESC(debug_mask, "Debugging mask");
37 MODULE_PARM_DESC(uart_print, "Uart target debugging");
38 MODULE_PARM_DESC(p2p, "Enable ath10k P2P support");
39
40 static const struct ath10k_hw_params ath10k_hw_params_list[] = {
41         {
42                 .id = QCA988X_HW_2_0_VERSION,
43                 .name = "qca988x hw2.0",
44                 .patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
45                 .fw = {
46                         .dir = QCA988X_HW_2_0_FW_DIR,
47                         .fw = QCA988X_HW_2_0_FW_FILE,
48                         .otp = QCA988X_HW_2_0_OTP_FILE,
49                         .board = QCA988X_HW_2_0_BOARD_DATA_FILE,
50                 },
51         },
52 };
53
54 static void ath10k_send_suspend_complete(struct ath10k *ar)
55 {
56         ath10k_dbg(ATH10K_DBG_BOOT, "boot suspend complete\n");
57
58         ar->is_target_paused = true;
59         wake_up(&ar->event_queue);
60 }
61
62 static int ath10k_init_connect_htc(struct ath10k *ar)
63 {
64         int status;
65
66         status = ath10k_wmi_connect_htc_service(ar);
67         if (status)
68                 goto conn_fail;
69
70         /* Start HTC */
71         status = ath10k_htc_start(&ar->htc);
72         if (status)
73                 goto conn_fail;
74
75         /* Wait for WMI event to be ready */
76         status = ath10k_wmi_wait_for_service_ready(ar);
77         if (status <= 0) {
78                 ath10k_warn("wmi service ready event not received");
79                 status = -ETIMEDOUT;
80                 goto timeout;
81         }
82
83         ath10k_dbg(ATH10K_DBG_BOOT, "boot wmi ready\n");
84         return 0;
85
86 timeout:
87         ath10k_htc_stop(&ar->htc);
88 conn_fail:
89         return status;
90 }
91
92 static int ath10k_init_configure_target(struct ath10k *ar)
93 {
94         u32 param_host;
95         int ret;
96
97         /* tell target which HTC version it is used*/
98         ret = ath10k_bmi_write32(ar, hi_app_host_interest,
99                                  HTC_PROTOCOL_VERSION);
100         if (ret) {
101                 ath10k_err("settings HTC version failed\n");
102                 return ret;
103         }
104
105         /* set the firmware mode to STA/IBSS/AP */
106         ret = ath10k_bmi_read32(ar, hi_option_flag, &param_host);
107         if (ret) {
108                 ath10k_err("setting firmware mode (1/2) failed\n");
109                 return ret;
110         }
111
112         /* TODO following parameters need to be re-visited. */
113         /* num_device */
114         param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
115         /* Firmware mode */
116         /* FIXME: Why FW_MODE_AP ??.*/
117         param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
118         /* mac_addr_method */
119         param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
120         /* firmware_bridge */
121         param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
122         /* fwsubmode */
123         param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
124
125         ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
126         if (ret) {
127                 ath10k_err("setting firmware mode (2/2) failed\n");
128                 return ret;
129         }
130
131         /* We do all byte-swapping on the host */
132         ret = ath10k_bmi_write32(ar, hi_be, 0);
133         if (ret) {
134                 ath10k_err("setting host CPU BE mode failed\n");
135                 return ret;
136         }
137
138         /* FW descriptor/Data swap flags */
139         ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
140
141         if (ret) {
142                 ath10k_err("setting FW data/desc swap flags failed\n");
143                 return ret;
144         }
145
146         return 0;
147 }
148
149 static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
150                                                    const char *dir,
151                                                    const char *file)
152 {
153         char filename[100];
154         const struct firmware *fw;
155         int ret;
156
157         if (file == NULL)
158                 return ERR_PTR(-ENOENT);
159
160         if (dir == NULL)
161                 dir = ".";
162
163         snprintf(filename, sizeof(filename), "%s/%s", dir, file);
164         ret = request_firmware(&fw, filename, ar->dev);
165         if (ret)
166                 return ERR_PTR(ret);
167
168         return fw;
169 }
170
171 static int ath10k_push_board_ext_data(struct ath10k *ar)
172 {
173         u32 board_data_size = QCA988X_BOARD_DATA_SZ;
174         u32 board_ext_data_size = QCA988X_BOARD_EXT_DATA_SZ;
175         u32 board_ext_data_addr;
176         int ret;
177
178         ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
179         if (ret) {
180                 ath10k_err("could not read board ext data addr (%d)\n", ret);
181                 return ret;
182         }
183
184         ath10k_dbg(ATH10K_DBG_BOOT,
185                    "boot push board extended data addr 0x%x\n",
186                    board_ext_data_addr);
187
188         if (board_ext_data_addr == 0)
189                 return 0;
190
191         if (ar->board_len != (board_data_size + board_ext_data_size)) {
192                 ath10k_err("invalid board (ext) data sizes %zu != %d+%d\n",
193                            ar->board_len, board_data_size, board_ext_data_size);
194                 return -EINVAL;
195         }
196
197         ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
198                                       ar->board_data + board_data_size,
199                                       board_ext_data_size);
200         if (ret) {
201                 ath10k_err("could not write board ext data (%d)\n", ret);
202                 return ret;
203         }
204
205         ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
206                                  (board_ext_data_size << 16) | 1);
207         if (ret) {
208                 ath10k_err("could not write board ext data bit (%d)\n", ret);
209                 return ret;
210         }
211
212         return 0;
213 }
214
215 static int ath10k_download_board_data(struct ath10k *ar)
216 {
217         u32 board_data_size = QCA988X_BOARD_DATA_SZ;
218         u32 address;
219         int ret;
220
221         ret = ath10k_push_board_ext_data(ar);
222         if (ret) {
223                 ath10k_err("could not push board ext data (%d)\n", ret);
224                 goto exit;
225         }
226
227         ret = ath10k_bmi_read32(ar, hi_board_data, &address);
228         if (ret) {
229                 ath10k_err("could not read board data addr (%d)\n", ret);
230                 goto exit;
231         }
232
233         ret = ath10k_bmi_write_memory(ar, address, ar->board_data,
234                                       min_t(u32, board_data_size,
235                                             ar->board_len));
236         if (ret) {
237                 ath10k_err("could not write board data (%d)\n", ret);
238                 goto exit;
239         }
240
241         ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
242         if (ret) {
243                 ath10k_err("could not write board data bit (%d)\n", ret);
244                 goto exit;
245         }
246
247 exit:
248         return ret;
249 }
250
251 static int ath10k_download_and_run_otp(struct ath10k *ar)
252 {
253         u32 address = ar->hw_params.patch_load_addr;
254         u32 exec_param;
255         int ret;
256
257         /* OTP is optional */
258
259         if (!ar->otp_data || !ar->otp_len)
260                 return 0;
261
262         ret = ath10k_bmi_fast_download(ar, address, ar->otp_data, ar->otp_len);
263         if (ret) {
264                 ath10k_err("could not write otp (%d)\n", ret);
265                 goto exit;
266         }
267
268         exec_param = 0;
269         ret = ath10k_bmi_execute(ar, address, &exec_param);
270         if (ret) {
271                 ath10k_err("could not execute otp (%d)\n", ret);
272                 goto exit;
273         }
274
275 exit:
276         return ret;
277 }
278
279 static int ath10k_download_fw(struct ath10k *ar)
280 {
281         u32 address;
282         int ret;
283
284         address = ar->hw_params.patch_load_addr;
285
286         ret = ath10k_bmi_fast_download(ar, address, ar->firmware_data,
287                                        ar->firmware_len);
288         if (ret) {
289                 ath10k_err("could not write fw (%d)\n", ret);
290                 goto exit;
291         }
292
293 exit:
294         return ret;
295 }
296
297 static void ath10k_core_free_firmware_files(struct ath10k *ar)
298 {
299         if (ar->board && !IS_ERR(ar->board))
300                 release_firmware(ar->board);
301
302         if (ar->otp && !IS_ERR(ar->otp))
303                 release_firmware(ar->otp);
304
305         if (ar->firmware && !IS_ERR(ar->firmware))
306                 release_firmware(ar->firmware);
307
308         ar->board = NULL;
309         ar->board_data = NULL;
310         ar->board_len = 0;
311
312         ar->otp = NULL;
313         ar->otp_data = NULL;
314         ar->otp_len = 0;
315
316         ar->firmware = NULL;
317         ar->firmware_data = NULL;
318         ar->firmware_len = 0;
319 }
320
321 static int ath10k_core_fetch_firmware_api_1(struct ath10k *ar)
322 {
323         int ret = 0;
324
325         if (ar->hw_params.fw.fw == NULL) {
326                 ath10k_err("firmware file not defined\n");
327                 return -EINVAL;
328         }
329
330         if (ar->hw_params.fw.board == NULL) {
331                 ath10k_err("board data file not defined");
332                 return -EINVAL;
333         }
334
335         ar->board = ath10k_fetch_fw_file(ar,
336                                          ar->hw_params.fw.dir,
337                                          ar->hw_params.fw.board);
338         if (IS_ERR(ar->board)) {
339                 ret = PTR_ERR(ar->board);
340                 ath10k_err("could not fetch board data (%d)\n", ret);
341                 goto err;
342         }
343
344         ar->board_data = ar->board->data;
345         ar->board_len = ar->board->size;
346
347         ar->firmware = ath10k_fetch_fw_file(ar,
348                                             ar->hw_params.fw.dir,
349                                             ar->hw_params.fw.fw);
350         if (IS_ERR(ar->firmware)) {
351                 ret = PTR_ERR(ar->firmware);
352                 ath10k_err("could not fetch firmware (%d)\n", ret);
353                 goto err;
354         }
355
356         ar->firmware_data = ar->firmware->data;
357         ar->firmware_len = ar->firmware->size;
358
359         /* OTP may be undefined. If so, don't fetch it at all */
360         if (ar->hw_params.fw.otp == NULL)
361                 return 0;
362
363         ar->otp = ath10k_fetch_fw_file(ar,
364                                        ar->hw_params.fw.dir,
365                                        ar->hw_params.fw.otp);
366         if (IS_ERR(ar->otp)) {
367                 ret = PTR_ERR(ar->otp);
368                 ath10k_err("could not fetch otp (%d)\n", ret);
369                 goto err;
370         }
371
372         ar->otp_data = ar->otp->data;
373         ar->otp_len = ar->otp->size;
374
375         return 0;
376
377 err:
378         ath10k_core_free_firmware_files(ar);
379         return ret;
380 }
381
382 static int ath10k_core_fetch_firmware_api_n(struct ath10k *ar, const char *name)
383 {
384         size_t magic_len, len, ie_len;
385         int ie_id, i, index, bit, ret;
386         struct ath10k_fw_ie *hdr;
387         const u8 *data;
388         __le32 *timestamp;
389
390         /* first fetch the firmware file (firmware-*.bin) */
391         ar->firmware = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir, name);
392         if (IS_ERR(ar->firmware)) {
393                 ath10k_err("Could not fetch firmware file '%s': %ld\n",
394                            name, PTR_ERR(ar->firmware));
395                 return PTR_ERR(ar->firmware);
396         }
397
398         data = ar->firmware->data;
399         len = ar->firmware->size;
400
401         /* magic also includes the null byte, check that as well */
402         magic_len = strlen(ATH10K_FIRMWARE_MAGIC) + 1;
403
404         if (len < magic_len) {
405                 ath10k_err("firmware image too small to contain magic: %zu\n",
406                            len);
407                 ret = -EINVAL;
408                 goto err;
409         }
410
411         if (memcmp(data, ATH10K_FIRMWARE_MAGIC, magic_len) != 0) {
412                 ath10k_err("Invalid firmware magic\n");
413                 ret = -EINVAL;
414                 goto err;
415         }
416
417         /* jump over the padding */
418         magic_len = ALIGN(magic_len, 4);
419
420         len -= magic_len;
421         data += magic_len;
422
423         /* loop elements */
424         while (len > sizeof(struct ath10k_fw_ie)) {
425                 hdr = (struct ath10k_fw_ie *)data;
426
427                 ie_id = le32_to_cpu(hdr->id);
428                 ie_len = le32_to_cpu(hdr->len);
429
430                 len -= sizeof(*hdr);
431                 data += sizeof(*hdr);
432
433                 if (len < ie_len) {
434                         ath10k_err("Invalid length for FW IE %d (%zu < %zu)\n",
435                                    ie_id, len, ie_len);
436                         ret = -EINVAL;
437                         goto err;
438                 }
439
440                 switch (ie_id) {
441                 case ATH10K_FW_IE_FW_VERSION:
442                         if (ie_len > sizeof(ar->hw->wiphy->fw_version) - 1)
443                                 break;
444
445                         memcpy(ar->hw->wiphy->fw_version, data, ie_len);
446                         ar->hw->wiphy->fw_version[ie_len] = '\0';
447
448                         ath10k_dbg(ATH10K_DBG_BOOT,
449                                    "found fw version %s\n",
450                                     ar->hw->wiphy->fw_version);
451                         break;
452                 case ATH10K_FW_IE_TIMESTAMP:
453                         if (ie_len != sizeof(u32))
454                                 break;
455
456                         timestamp = (__le32 *)data;
457
458                         ath10k_dbg(ATH10K_DBG_BOOT, "found fw timestamp %d\n",
459                                    le32_to_cpup(timestamp));
460                         break;
461                 case ATH10K_FW_IE_FEATURES:
462                         ath10k_dbg(ATH10K_DBG_BOOT,
463                                    "found firmware features ie (%zd B)\n",
464                                    ie_len);
465
466                         for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
467                                 index = i / 8;
468                                 bit = i % 8;
469
470                                 if (index == ie_len)
471                                         break;
472
473                                 if (data[index] & (1 << bit))
474                                         __set_bit(i, ar->fw_features);
475                         }
476
477                         ath10k_dbg_dump(ATH10K_DBG_BOOT, "features", "",
478                                         ar->fw_features,
479                                         sizeof(ar->fw_features));
480                         break;
481                 case ATH10K_FW_IE_FW_IMAGE:
482                         ath10k_dbg(ATH10K_DBG_BOOT,
483                                    "found fw image ie (%zd B)\n",
484                                    ie_len);
485
486                         ar->firmware_data = data;
487                         ar->firmware_len = ie_len;
488
489                         break;
490                 case ATH10K_FW_IE_OTP_IMAGE:
491                         ath10k_dbg(ATH10K_DBG_BOOT,
492                                    "found otp image ie (%zd B)\n",
493                                    ie_len);
494
495                         ar->otp_data = data;
496                         ar->otp_len = ie_len;
497
498                         break;
499                 default:
500                         ath10k_warn("Unknown FW IE: %u\n",
501                                     le32_to_cpu(hdr->id));
502                         break;
503                 }
504
505                 /* jump over the padding */
506                 ie_len = ALIGN(ie_len, 4);
507
508                 len -= ie_len;
509                 data += ie_len;
510         };
511
512         if (!ar->firmware_data || !ar->firmware_len) {
513                 ath10k_warn("No ATH10K_FW_IE_FW_IMAGE found from %s, skipping\n",
514                             name);
515                 ret = -ENOMEDIUM;
516                 goto err;
517         }
518
519         /* now fetch the board file */
520         if (ar->hw_params.fw.board == NULL) {
521                 ath10k_err("board data file not defined");
522                 ret = -EINVAL;
523                 goto err;
524         }
525
526         ar->board = ath10k_fetch_fw_file(ar,
527                                          ar->hw_params.fw.dir,
528                                          ar->hw_params.fw.board);
529         if (IS_ERR(ar->board)) {
530                 ret = PTR_ERR(ar->board);
531                 ath10k_err("could not fetch board data (%d)\n", ret);
532                 goto err;
533         }
534
535         ar->board_data = ar->board->data;
536         ar->board_len = ar->board->size;
537
538         return 0;
539
540 err:
541         ath10k_core_free_firmware_files(ar);
542         return ret;
543 }
544
545 static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
546 {
547         int ret;
548
549         ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API2_FILE);
550         if (ret == 0) {
551                 ar->fw_api = 2;
552                 goto out;
553         }
554
555         ret = ath10k_core_fetch_firmware_api_1(ar);
556         if (ret)
557                 return ret;
558
559         ar->fw_api = 1;
560
561 out:
562         ath10k_dbg(ATH10K_DBG_BOOT, "using fw api %d\n", ar->fw_api);
563
564         return 0;
565 }
566
567 static int ath10k_init_download_firmware(struct ath10k *ar)
568 {
569         int ret;
570
571         ret = ath10k_download_board_data(ar);
572         if (ret)
573                 return ret;
574
575         ret = ath10k_download_and_run_otp(ar);
576         if (ret)
577                 return ret;
578
579         ret = ath10k_download_fw(ar);
580         if (ret)
581                 return ret;
582
583         return ret;
584 }
585
586 static int ath10k_init_uart(struct ath10k *ar)
587 {
588         int ret;
589
590         /*
591          * Explicitly setting UART prints to zero as target turns it on
592          * based on scratch registers.
593          */
594         ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
595         if (ret) {
596                 ath10k_warn("could not disable UART prints (%d)\n", ret);
597                 return ret;
598         }
599
600         if (!uart_print) {
601                 ath10k_info("UART prints disabled\n");
602                 return 0;
603         }
604
605         ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, 7);
606         if (ret) {
607                 ath10k_warn("could not enable UART prints (%d)\n", ret);
608                 return ret;
609         }
610
611         ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
612         if (ret) {
613                 ath10k_warn("could not enable UART prints (%d)\n", ret);
614                 return ret;
615         }
616
617         /* Set the UART baud rate to 19200. */
618         ret = ath10k_bmi_write32(ar, hi_desired_baud_rate, 19200);
619         if (ret) {
620                 ath10k_warn("could not set the baud rate (%d)\n", ret);
621                 return ret;
622         }
623
624         ath10k_info("UART prints enabled\n");
625         return 0;
626 }
627
628 static int ath10k_init_hw_params(struct ath10k *ar)
629 {
630         const struct ath10k_hw_params *uninitialized_var(hw_params);
631         int i;
632
633         for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
634                 hw_params = &ath10k_hw_params_list[i];
635
636                 if (hw_params->id == ar->target_version)
637                         break;
638         }
639
640         if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
641                 ath10k_err("Unsupported hardware version: 0x%x\n",
642                            ar->target_version);
643                 return -EINVAL;
644         }
645
646         ar->hw_params = *hw_params;
647
648         ath10k_info("Hardware name %s version 0x%x\n",
649                     ar->hw_params.name, ar->target_version);
650
651         return 0;
652 }
653
654 static void ath10k_core_restart(struct work_struct *work)
655 {
656         struct ath10k *ar = container_of(work, struct ath10k, restart_work);
657
658         mutex_lock(&ar->conf_mutex);
659
660         switch (ar->state) {
661         case ATH10K_STATE_ON:
662                 ath10k_halt(ar);
663                 ar->state = ATH10K_STATE_RESTARTING;
664                 ieee80211_restart_hw(ar->hw);
665                 break;
666         case ATH10K_STATE_OFF:
667                 /* this can happen if driver is being unloaded */
668                 ath10k_warn("cannot restart a device that hasn't been started\n");
669                 break;
670         case ATH10K_STATE_RESTARTING:
671         case ATH10K_STATE_RESTARTED:
672                 ar->state = ATH10K_STATE_WEDGED;
673                 /* fall through */
674         case ATH10K_STATE_WEDGED:
675                 ath10k_warn("device is wedged, will not restart\n");
676                 break;
677         }
678
679         mutex_unlock(&ar->conf_mutex);
680 }
681
682 struct ath10k *ath10k_core_create(void *hif_priv, struct device *dev,
683                                   const struct ath10k_hif_ops *hif_ops)
684 {
685         struct ath10k *ar;
686
687         ar = ath10k_mac_create();
688         if (!ar)
689                 return NULL;
690
691         ar->ath_common.priv = ar;
692         ar->ath_common.hw = ar->hw;
693
694         ar->p2p = !!ath10k_p2p;
695         ar->dev = dev;
696
697         ar->hif.priv = hif_priv;
698         ar->hif.ops = hif_ops;
699
700         init_completion(&ar->scan.started);
701         init_completion(&ar->scan.completed);
702         init_completion(&ar->scan.on_channel);
703
704         init_completion(&ar->install_key_done);
705         init_completion(&ar->vdev_setup_done);
706
707         setup_timer(&ar->scan.timeout, ath10k_reset_scan, (unsigned long)ar);
708
709         ar->workqueue = create_singlethread_workqueue("ath10k_wq");
710         if (!ar->workqueue)
711                 goto err_wq;
712
713         mutex_init(&ar->conf_mutex);
714         spin_lock_init(&ar->data_lock);
715
716         INIT_LIST_HEAD(&ar->peers);
717         init_waitqueue_head(&ar->peer_mapping_wq);
718
719         init_completion(&ar->offchan_tx_completed);
720         INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
721         skb_queue_head_init(&ar->offchan_tx_queue);
722
723         INIT_WORK(&ar->wmi_mgmt_tx_work, ath10k_mgmt_over_wmi_tx_work);
724         skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
725
726         init_waitqueue_head(&ar->event_queue);
727
728         INIT_WORK(&ar->restart_work, ath10k_core_restart);
729
730         return ar;
731
732 err_wq:
733         ath10k_mac_destroy(ar);
734         return NULL;
735 }
736 EXPORT_SYMBOL(ath10k_core_create);
737
738 void ath10k_core_destroy(struct ath10k *ar)
739 {
740         flush_workqueue(ar->workqueue);
741         destroy_workqueue(ar->workqueue);
742
743         ath10k_mac_destroy(ar);
744 }
745 EXPORT_SYMBOL(ath10k_core_destroy);
746
747 int ath10k_core_start(struct ath10k *ar)
748 {
749         int status;
750
751         ath10k_bmi_start(ar);
752
753         if (ath10k_init_configure_target(ar)) {
754                 status = -EINVAL;
755                 goto err;
756         }
757
758         status = ath10k_init_download_firmware(ar);
759         if (status)
760                 goto err;
761
762         status = ath10k_init_uart(ar);
763         if (status)
764                 goto err;
765
766         ar->htc.htc_ops.target_send_suspend_complete =
767                 ath10k_send_suspend_complete;
768
769         status = ath10k_htc_init(ar);
770         if (status) {
771                 ath10k_err("could not init HTC (%d)\n", status);
772                 goto err;
773         }
774
775         status = ath10k_bmi_done(ar);
776         if (status)
777                 goto err;
778
779         status = ath10k_wmi_attach(ar);
780         if (status) {
781                 ath10k_err("WMI attach failed: %d\n", status);
782                 goto err;
783         }
784
785         status = ath10k_htc_wait_target(&ar->htc);
786         if (status)
787                 goto err_wmi_detach;
788
789         status = ath10k_htt_attach(ar);
790         if (status) {
791                 ath10k_err("could not attach htt (%d)\n", status);
792                 goto err_wmi_detach;
793         }
794
795         status = ath10k_init_connect_htc(ar);
796         if (status)
797                 goto err_htt_detach;
798
799         ath10k_info("firmware %s booted\n", ar->hw->wiphy->fw_version);
800
801         status = ath10k_wmi_cmd_init(ar);
802         if (status) {
803                 ath10k_err("could not send WMI init command (%d)\n", status);
804                 goto err_disconnect_htc;
805         }
806
807         status = ath10k_wmi_wait_for_unified_ready(ar);
808         if (status <= 0) {
809                 ath10k_err("wmi unified ready event not received\n");
810                 status = -ETIMEDOUT;
811                 goto err_disconnect_htc;
812         }
813
814         status = ath10k_htt_attach_target(&ar->htt);
815         if (status)
816                 goto err_disconnect_htc;
817
818         status = ath10k_debug_start(ar);
819         if (status)
820                 goto err_disconnect_htc;
821
822         ar->free_vdev_map = (1 << TARGET_NUM_VDEVS) - 1;
823
824         return 0;
825
826 err_disconnect_htc:
827         ath10k_htc_stop(&ar->htc);
828 err_htt_detach:
829         ath10k_htt_detach(&ar->htt);
830 err_wmi_detach:
831         ath10k_wmi_detach(ar);
832 err:
833         return status;
834 }
835 EXPORT_SYMBOL(ath10k_core_start);
836
837 void ath10k_core_stop(struct ath10k *ar)
838 {
839         ath10k_debug_stop(ar);
840         ath10k_htc_stop(&ar->htc);
841         ath10k_htt_detach(&ar->htt);
842         ath10k_wmi_detach(ar);
843 }
844 EXPORT_SYMBOL(ath10k_core_stop);
845
846 /* mac80211 manages fw/hw initialization through start/stop hooks. However in
847  * order to know what hw capabilities should be advertised to mac80211 it is
848  * necessary to load the firmware (and tear it down immediately since start
849  * hook will try to init it again) before registering */
850 static int ath10k_core_probe_fw(struct ath10k *ar)
851 {
852         struct bmi_target_info target_info;
853         int ret = 0;
854
855         ret = ath10k_hif_power_up(ar);
856         if (ret) {
857                 ath10k_err("could not start pci hif (%d)\n", ret);
858                 return ret;
859         }
860
861         memset(&target_info, 0, sizeof(target_info));
862         ret = ath10k_bmi_get_target_info(ar, &target_info);
863         if (ret) {
864                 ath10k_err("could not get target info (%d)\n", ret);
865                 ath10k_hif_power_down(ar);
866                 return ret;
867         }
868
869         ar->target_version = target_info.version;
870         ar->hw->wiphy->hw_version = target_info.version;
871
872         ret = ath10k_init_hw_params(ar);
873         if (ret) {
874                 ath10k_err("could not get hw params (%d)\n", ret);
875                 ath10k_hif_power_down(ar);
876                 return ret;
877         }
878
879         ret = ath10k_core_fetch_firmware_files(ar);
880         if (ret) {
881                 ath10k_err("could not fetch firmware files (%d)\n", ret);
882                 ath10k_hif_power_down(ar);
883                 return ret;
884         }
885
886         ret = ath10k_core_start(ar);
887         if (ret) {
888                 ath10k_err("could not init core (%d)\n", ret);
889                 ath10k_core_free_firmware_files(ar);
890                 ath10k_hif_power_down(ar);
891                 return ret;
892         }
893
894         ath10k_core_stop(ar);
895         ath10k_hif_power_down(ar);
896         return 0;
897 }
898
899 static int ath10k_core_check_chip_id(struct ath10k *ar)
900 {
901         u32 hw_revision = MS(ar->chip_id, SOC_CHIP_ID_REV);
902
903         ath10k_dbg(ATH10K_DBG_BOOT, "boot chip_id 0x%08x hw_revision 0x%x\n",
904                    ar->chip_id, hw_revision);
905
906         /* Check that we are not using hw1.0 (some of them have same pci id
907          * as hw2.0) before doing anything else as ath10k crashes horribly
908          * due to missing hw1.0 workarounds. */
909         switch (hw_revision) {
910         case QCA988X_HW_1_0_CHIP_ID_REV:
911                 ath10k_err("ERROR: qca988x hw1.0 is not supported\n");
912                 return -EOPNOTSUPP;
913
914         case QCA988X_HW_2_0_CHIP_ID_REV:
915                 /* known hardware revision, continue normally */
916                 return 0;
917
918         default:
919                 ath10k_warn("Warning: hardware revision unknown (0x%x), expect problems\n",
920                             ar->chip_id);
921                 return 0;
922         }
923
924         return 0;
925 }
926
927 int ath10k_core_register(struct ath10k *ar, u32 chip_id)
928 {
929         int status;
930
931         ar->chip_id = chip_id;
932
933         status = ath10k_core_check_chip_id(ar);
934         if (status) {
935                 ath10k_err("Unsupported chip id 0x%08x\n", ar->chip_id);
936                 return status;
937         }
938
939         status = ath10k_core_probe_fw(ar);
940         if (status) {
941                 ath10k_err("could not probe fw (%d)\n", status);
942                 return status;
943         }
944
945         status = ath10k_mac_register(ar);
946         if (status) {
947                 ath10k_err("could not register to mac80211 (%d)\n", status);
948                 goto err_release_fw;
949         }
950
951         status = ath10k_debug_create(ar);
952         if (status) {
953                 ath10k_err("unable to initialize debugfs\n");
954                 goto err_unregister_mac;
955         }
956
957         return 0;
958
959 err_unregister_mac:
960         ath10k_mac_unregister(ar);
961 err_release_fw:
962         ath10k_core_free_firmware_files(ar);
963         return status;
964 }
965 EXPORT_SYMBOL(ath10k_core_register);
966
967 void ath10k_core_unregister(struct ath10k *ar)
968 {
969         /* We must unregister from mac80211 before we stop HTC and HIF.
970          * Otherwise we will fail to submit commands to FW and mac80211 will be
971          * unhappy about callback failures. */
972         ath10k_mac_unregister(ar);
973
974         ath10k_core_free_firmware_files(ar);
975 }
976 EXPORT_SYMBOL(ath10k_core_unregister);
977
978 MODULE_AUTHOR("Qualcomm Atheros");
979 MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
980 MODULE_LICENSE("Dual BSD/GPL");