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