]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/net/wireless/ath/ath10k/core.c
ath10k: fix whitespace usage
[linux-beck.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 #include <linux/of.h>
21
22 #include "core.h"
23 #include "mac.h"
24 #include "htc.h"
25 #include "hif.h"
26 #include "wmi.h"
27 #include "bmi.h"
28 #include "debug.h"
29 #include "htt.h"
30 #include "testmode.h"
31 #include "wmi-ops.h"
32
33 unsigned int ath10k_debug_mask;
34 static unsigned int ath10k_cryptmode_param;
35 static bool uart_print;
36 static bool skip_otp;
37 static bool rawmode;
38
39 module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
40 module_param_named(cryptmode, ath10k_cryptmode_param, uint, 0644);
41 module_param(uart_print, bool, 0644);
42 module_param(skip_otp, bool, 0644);
43 module_param(rawmode, bool, 0644);
44
45 MODULE_PARM_DESC(debug_mask, "Debugging mask");
46 MODULE_PARM_DESC(uart_print, "Uart target debugging");
47 MODULE_PARM_DESC(skip_otp, "Skip otp failure for calibration in testmode");
48 MODULE_PARM_DESC(cryptmode, "Crypto mode: 0-hardware, 1-software");
49 MODULE_PARM_DESC(rawmode, "Use raw 802.11 frame datapath");
50
51 static const struct ath10k_hw_params ath10k_hw_params_list[] = {
52         {
53                 .id = QCA988X_HW_2_0_VERSION,
54                 .name = "qca988x hw2.0",
55                 .patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
56                 .uart_pin = 7,
57                 .has_shifted_cc_wraparound = true,
58                 .otp_exe_param = 0,
59                 .channel_counters_freq_hz = 88000,
60                 .max_probe_resp_desc_thres = 0,
61                 .fw = {
62                         .dir = QCA988X_HW_2_0_FW_DIR,
63                         .fw = QCA988X_HW_2_0_FW_FILE,
64                         .otp = QCA988X_HW_2_0_OTP_FILE,
65                         .board = QCA988X_HW_2_0_BOARD_DATA_FILE,
66                         .board_size = QCA988X_BOARD_DATA_SZ,
67                         .board_ext_size = QCA988X_BOARD_EXT_DATA_SZ,
68                 },
69         },
70         {
71                 .id = QCA6174_HW_2_1_VERSION,
72                 .name = "qca6174 hw2.1",
73                 .patch_load_addr = QCA6174_HW_2_1_PATCH_LOAD_ADDR,
74                 .uart_pin = 6,
75                 .otp_exe_param = 0,
76                 .channel_counters_freq_hz = 88000,
77                 .max_probe_resp_desc_thres = 0,
78                 .fw = {
79                         .dir = QCA6174_HW_2_1_FW_DIR,
80                         .fw = QCA6174_HW_2_1_FW_FILE,
81                         .otp = QCA6174_HW_2_1_OTP_FILE,
82                         .board = QCA6174_HW_2_1_BOARD_DATA_FILE,
83                         .board_size = QCA6174_BOARD_DATA_SZ,
84                         .board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
85                 },
86         },
87         {
88                 .id = QCA6174_HW_3_0_VERSION,
89                 .name = "qca6174 hw3.0",
90                 .patch_load_addr = QCA6174_HW_3_0_PATCH_LOAD_ADDR,
91                 .uart_pin = 6,
92                 .otp_exe_param = 0,
93                 .channel_counters_freq_hz = 88000,
94                 .max_probe_resp_desc_thres = 0,
95                 .fw = {
96                         .dir = QCA6174_HW_3_0_FW_DIR,
97                         .fw = QCA6174_HW_3_0_FW_FILE,
98                         .otp = QCA6174_HW_3_0_OTP_FILE,
99                         .board = QCA6174_HW_3_0_BOARD_DATA_FILE,
100                         .board_size = QCA6174_BOARD_DATA_SZ,
101                         .board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
102                 },
103         },
104         {
105                 .id = QCA6174_HW_3_2_VERSION,
106                 .name = "qca6174 hw3.2",
107                 .patch_load_addr = QCA6174_HW_3_0_PATCH_LOAD_ADDR,
108                 .uart_pin = 6,
109                 .otp_exe_param = 0,
110                 .channel_counters_freq_hz = 88000,
111                 .max_probe_resp_desc_thres = 0,
112                 .fw = {
113                         /* uses same binaries as hw3.0 */
114                         .dir = QCA6174_HW_3_0_FW_DIR,
115                         .fw = QCA6174_HW_3_0_FW_FILE,
116                         .otp = QCA6174_HW_3_0_OTP_FILE,
117                         .board = QCA6174_HW_3_0_BOARD_DATA_FILE,
118                         .board_size = QCA6174_BOARD_DATA_SZ,
119                         .board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
120                 },
121         },
122         {
123                 .id = QCA99X0_HW_2_0_DEV_VERSION,
124                 .name = "qca99x0 hw2.0",
125                 .patch_load_addr = QCA99X0_HW_2_0_PATCH_LOAD_ADDR,
126                 .uart_pin = 7,
127                 .otp_exe_param = 0x00000700,
128                 .continuous_frag_desc = true,
129                 .channel_counters_freq_hz = 150000,
130                 .max_probe_resp_desc_thres = 24,
131                 .fw = {
132                         .dir = QCA99X0_HW_2_0_FW_DIR,
133                         .fw = QCA99X0_HW_2_0_FW_FILE,
134                         .otp = QCA99X0_HW_2_0_OTP_FILE,
135                         .board = QCA99X0_HW_2_0_BOARD_DATA_FILE,
136                         .board_size = QCA99X0_BOARD_DATA_SZ,
137                         .board_ext_size = QCA99X0_BOARD_EXT_DATA_SZ,
138                 },
139         },
140 };
141
142 static const char *const ath10k_core_fw_feature_str[] = {
143         [ATH10K_FW_FEATURE_EXT_WMI_MGMT_RX] = "wmi-mgmt-rx",
144         [ATH10K_FW_FEATURE_WMI_10X] = "wmi-10.x",
145         [ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX] = "has-wmi-mgmt-tx",
146         [ATH10K_FW_FEATURE_NO_P2P] = "no-p2p",
147         [ATH10K_FW_FEATURE_WMI_10_2] = "wmi-10.2",
148         [ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT] = "multi-vif-ps",
149         [ATH10K_FW_FEATURE_WOWLAN_SUPPORT] = "wowlan",
150         [ATH10K_FW_FEATURE_IGNORE_OTP_RESULT] = "ignore-otp",
151         [ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING] = "no-4addr-pad",
152         [ATH10K_FW_FEATURE_SUPPORTS_SKIP_CLOCK_INIT] = "skip-clock-init",
153         [ATH10K_FW_FEATURE_RAW_MODE_SUPPORT] = "raw-mode",
154 };
155
156 static unsigned int ath10k_core_get_fw_feature_str(char *buf,
157                                                    size_t buf_len,
158                                                    enum ath10k_fw_features feat)
159 {
160         /* make sure that ath10k_core_fw_feature_str[] gets updated */
161         BUILD_BUG_ON(ARRAY_SIZE(ath10k_core_fw_feature_str) !=
162                      ATH10K_FW_FEATURE_COUNT);
163
164         if (feat >= ARRAY_SIZE(ath10k_core_fw_feature_str) ||
165             WARN_ON(!ath10k_core_fw_feature_str[feat])) {
166                 return scnprintf(buf, buf_len, "bit%d", feat);
167         }
168
169         return scnprintf(buf, buf_len, "%s", ath10k_core_fw_feature_str[feat]);
170 }
171
172 void ath10k_core_get_fw_features_str(struct ath10k *ar,
173                                      char *buf,
174                                      size_t buf_len)
175 {
176         unsigned int len = 0;
177         int i;
178
179         for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
180                 if (test_bit(i, ar->fw_features)) {
181                         if (len > 0)
182                                 len += scnprintf(buf + len, buf_len - len, ",");
183
184                         len += ath10k_core_get_fw_feature_str(buf + len,
185                                                               buf_len - len,
186                                                               i);
187                 }
188         }
189 }
190
191 static void ath10k_send_suspend_complete(struct ath10k *ar)
192 {
193         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot suspend complete\n");
194
195         complete(&ar->target_suspend);
196 }
197
198 static int ath10k_init_configure_target(struct ath10k *ar)
199 {
200         u32 param_host;
201         int ret;
202
203         /* tell target which HTC version it is used*/
204         ret = ath10k_bmi_write32(ar, hi_app_host_interest,
205                                  HTC_PROTOCOL_VERSION);
206         if (ret) {
207                 ath10k_err(ar, "settings HTC version failed\n");
208                 return ret;
209         }
210
211         /* set the firmware mode to STA/IBSS/AP */
212         ret = ath10k_bmi_read32(ar, hi_option_flag, &param_host);
213         if (ret) {
214                 ath10k_err(ar, "setting firmware mode (1/2) failed\n");
215                 return ret;
216         }
217
218         /* TODO following parameters need to be re-visited. */
219         /* num_device */
220         param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
221         /* Firmware mode */
222         /* FIXME: Why FW_MODE_AP ??.*/
223         param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
224         /* mac_addr_method */
225         param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
226         /* firmware_bridge */
227         param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
228         /* fwsubmode */
229         param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
230
231         ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
232         if (ret) {
233                 ath10k_err(ar, "setting firmware mode (2/2) failed\n");
234                 return ret;
235         }
236
237         /* We do all byte-swapping on the host */
238         ret = ath10k_bmi_write32(ar, hi_be, 0);
239         if (ret) {
240                 ath10k_err(ar, "setting host CPU BE mode failed\n");
241                 return ret;
242         }
243
244         /* FW descriptor/Data swap flags */
245         ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
246
247         if (ret) {
248                 ath10k_err(ar, "setting FW data/desc swap flags failed\n");
249                 return ret;
250         }
251
252         /* Some devices have a special sanity check that verifies the PCI
253          * Device ID is written to this host interest var. It is known to be
254          * required to boot QCA6164.
255          */
256         ret = ath10k_bmi_write32(ar, hi_hci_uart_pwr_mgmt_params_ext,
257                                  ar->dev_id);
258         if (ret) {
259                 ath10k_err(ar, "failed to set pwr_mgmt_params: %d\n", ret);
260                 return ret;
261         }
262
263         return 0;
264 }
265
266 static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
267                                                    const char *dir,
268                                                    const char *file)
269 {
270         char filename[100];
271         const struct firmware *fw;
272         int ret;
273
274         if (file == NULL)
275                 return ERR_PTR(-ENOENT);
276
277         if (dir == NULL)
278                 dir = ".";
279
280         snprintf(filename, sizeof(filename), "%s/%s", dir, file);
281         ret = request_firmware(&fw, filename, ar->dev);
282         if (ret)
283                 return ERR_PTR(ret);
284
285         return fw;
286 }
287
288 static int ath10k_push_board_ext_data(struct ath10k *ar, const void *data,
289                                       size_t data_len)
290 {
291         u32 board_data_size = ar->hw_params.fw.board_size;
292         u32 board_ext_data_size = ar->hw_params.fw.board_ext_size;
293         u32 board_ext_data_addr;
294         int ret;
295
296         ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
297         if (ret) {
298                 ath10k_err(ar, "could not read board ext data addr (%d)\n",
299                            ret);
300                 return ret;
301         }
302
303         ath10k_dbg(ar, ATH10K_DBG_BOOT,
304                    "boot push board extended data addr 0x%x\n",
305                    board_ext_data_addr);
306
307         if (board_ext_data_addr == 0)
308                 return 0;
309
310         if (data_len != (board_data_size + board_ext_data_size)) {
311                 ath10k_err(ar, "invalid board (ext) data sizes %zu != %d+%d\n",
312                            data_len, board_data_size, board_ext_data_size);
313                 return -EINVAL;
314         }
315
316         ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
317                                       data + board_data_size,
318                                       board_ext_data_size);
319         if (ret) {
320                 ath10k_err(ar, "could not write board ext data (%d)\n", ret);
321                 return ret;
322         }
323
324         ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
325                                  (board_ext_data_size << 16) | 1);
326         if (ret) {
327                 ath10k_err(ar, "could not write board ext data bit (%d)\n",
328                            ret);
329                 return ret;
330         }
331
332         return 0;
333 }
334
335 static int ath10k_download_board_data(struct ath10k *ar, const void *data,
336                                       size_t data_len)
337 {
338         u32 board_data_size = ar->hw_params.fw.board_size;
339         u32 address;
340         int ret;
341
342         ret = ath10k_push_board_ext_data(ar, data, data_len);
343         if (ret) {
344                 ath10k_err(ar, "could not push board ext data (%d)\n", ret);
345                 goto exit;
346         }
347
348         ret = ath10k_bmi_read32(ar, hi_board_data, &address);
349         if (ret) {
350                 ath10k_err(ar, "could not read board data addr (%d)\n", ret);
351                 goto exit;
352         }
353
354         ret = ath10k_bmi_write_memory(ar, address, data,
355                                       min_t(u32, board_data_size,
356                                             data_len));
357         if (ret) {
358                 ath10k_err(ar, "could not write board data (%d)\n", ret);
359                 goto exit;
360         }
361
362         ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
363         if (ret) {
364                 ath10k_err(ar, "could not write board data bit (%d)\n", ret);
365                 goto exit;
366         }
367
368 exit:
369         return ret;
370 }
371
372 static int ath10k_download_cal_file(struct ath10k *ar)
373 {
374         int ret;
375
376         if (!ar->cal_file)
377                 return -ENOENT;
378
379         if (IS_ERR(ar->cal_file))
380                 return PTR_ERR(ar->cal_file);
381
382         ret = ath10k_download_board_data(ar, ar->cal_file->data,
383                                          ar->cal_file->size);
384         if (ret) {
385                 ath10k_err(ar, "failed to download cal_file data: %d\n", ret);
386                 return ret;
387         }
388
389         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cal file downloaded\n");
390
391         return 0;
392 }
393
394 static int ath10k_download_cal_dt(struct ath10k *ar)
395 {
396         struct device_node *node;
397         int data_len;
398         void *data;
399         int ret;
400
401         node = ar->dev->of_node;
402         if (!node)
403                 /* Device Tree is optional, don't print any warnings if
404                  * there's no node for ath10k.
405                  */
406                 return -ENOENT;
407
408         if (!of_get_property(node, "qcom,ath10k-calibration-data",
409                              &data_len)) {
410                 /* The calibration data node is optional */
411                 return -ENOENT;
412         }
413
414         if (data_len != QCA988X_CAL_DATA_LEN) {
415                 ath10k_warn(ar, "invalid calibration data length in DT: %d\n",
416                             data_len);
417                 ret = -EMSGSIZE;
418                 goto out;
419         }
420
421         data = kmalloc(data_len, GFP_KERNEL);
422         if (!data) {
423                 ret = -ENOMEM;
424                 goto out;
425         }
426
427         ret = of_property_read_u8_array(node, "qcom,ath10k-calibration-data",
428                                         data, data_len);
429         if (ret) {
430                 ath10k_warn(ar, "failed to read calibration data from DT: %d\n",
431                             ret);
432                 goto out_free;
433         }
434
435         ret = ath10k_download_board_data(ar, data, data_len);
436         if (ret) {
437                 ath10k_warn(ar, "failed to download calibration data from Device Tree: %d\n",
438                             ret);
439                 goto out_free;
440         }
441
442         ret = 0;
443
444 out_free:
445         kfree(data);
446
447 out:
448         return ret;
449 }
450
451 static int ath10k_download_and_run_otp(struct ath10k *ar)
452 {
453         u32 result, address = ar->hw_params.patch_load_addr;
454         u32 bmi_otp_exe_param = ar->hw_params.otp_exe_param;
455         int ret;
456
457         ret = ath10k_download_board_data(ar, ar->board_data, ar->board_len);
458         if (ret) {
459                 ath10k_err(ar, "failed to download board data: %d\n", ret);
460                 return ret;
461         }
462
463         /* OTP is optional */
464
465         if (!ar->otp_data || !ar->otp_len) {
466                 ath10k_warn(ar, "Not running otp, calibration will be incorrect (otp-data %p otp_len %zd)!\n",
467                             ar->otp_data, ar->otp_len);
468                 return 0;
469         }
470
471         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot upload otp to 0x%x len %zd\n",
472                    address, ar->otp_len);
473
474         ret = ath10k_bmi_fast_download(ar, address, ar->otp_data, ar->otp_len);
475         if (ret) {
476                 ath10k_err(ar, "could not write otp (%d)\n", ret);
477                 return ret;
478         }
479
480         ret = ath10k_bmi_execute(ar, address, bmi_otp_exe_param, &result);
481         if (ret) {
482                 ath10k_err(ar, "could not execute otp (%d)\n", ret);
483                 return ret;
484         }
485
486         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot otp execute result %d\n", result);
487
488         if (!(skip_otp || test_bit(ATH10K_FW_FEATURE_IGNORE_OTP_RESULT,
489                                    ar->fw_features)) &&
490             result != 0) {
491                 ath10k_err(ar, "otp calibration failed: %d", result);
492                 return -EINVAL;
493         }
494
495         return 0;
496 }
497
498 static int ath10k_download_fw(struct ath10k *ar, enum ath10k_firmware_mode mode)
499 {
500         u32 address, data_len;
501         const char *mode_name;
502         const void *data;
503         int ret;
504
505         address = ar->hw_params.patch_load_addr;
506
507         switch (mode) {
508         case ATH10K_FIRMWARE_MODE_NORMAL:
509                 data = ar->firmware_data;
510                 data_len = ar->firmware_len;
511                 mode_name = "normal";
512                 ret = ath10k_swap_code_seg_configure(ar,
513                                                      ATH10K_SWAP_CODE_SEG_BIN_TYPE_FW);
514                 if (ret) {
515                         ath10k_err(ar, "failed to configure fw code swap: %d\n",
516                                    ret);
517                         return ret;
518                 }
519                 break;
520         case ATH10K_FIRMWARE_MODE_UTF:
521                 data = ar->testmode.utf->data;
522                 data_len = ar->testmode.utf->size;
523                 mode_name = "utf";
524                 break;
525         default:
526                 ath10k_err(ar, "unknown firmware mode: %d\n", mode);
527                 return -EINVAL;
528         }
529
530         ath10k_dbg(ar, ATH10K_DBG_BOOT,
531                    "boot uploading firmware image %p len %d mode %s\n",
532                    data, data_len, mode_name);
533
534         ret = ath10k_bmi_fast_download(ar, address, data, data_len);
535         if (ret) {
536                 ath10k_err(ar, "failed to download %s firmware: %d\n",
537                            mode_name, ret);
538                 return ret;
539         }
540
541         return ret;
542 }
543
544 static void ath10k_core_free_firmware_files(struct ath10k *ar)
545 {
546         if (!IS_ERR(ar->board))
547                 release_firmware(ar->board);
548
549         if (!IS_ERR(ar->otp))
550                 release_firmware(ar->otp);
551
552         if (!IS_ERR(ar->firmware))
553                 release_firmware(ar->firmware);
554
555         if (!IS_ERR(ar->cal_file))
556                 release_firmware(ar->cal_file);
557
558         ath10k_swap_code_seg_release(ar);
559
560         ar->board = NULL;
561         ar->board_data = NULL;
562         ar->board_len = 0;
563
564         ar->otp = NULL;
565         ar->otp_data = NULL;
566         ar->otp_len = 0;
567
568         ar->firmware = NULL;
569         ar->firmware_data = NULL;
570         ar->firmware_len = 0;
571
572         ar->cal_file = NULL;
573 }
574
575 static int ath10k_fetch_cal_file(struct ath10k *ar)
576 {
577         char filename[100];
578
579         /* cal-<bus>-<id>.bin */
580         scnprintf(filename, sizeof(filename), "cal-%s-%s.bin",
581                   ath10k_bus_str(ar->hif.bus), dev_name(ar->dev));
582
583         ar->cal_file = ath10k_fetch_fw_file(ar, ATH10K_FW_DIR, filename);
584         if (IS_ERR(ar->cal_file))
585                 /* calibration file is optional, don't print any warnings */
586                 return PTR_ERR(ar->cal_file);
587
588         ath10k_dbg(ar, ATH10K_DBG_BOOT, "found calibration file %s/%s\n",
589                    ATH10K_FW_DIR, filename);
590
591         return 0;
592 }
593
594 static int ath10k_core_fetch_spec_board_file(struct ath10k *ar)
595 {
596         char filename[100];
597
598         scnprintf(filename, sizeof(filename), "board-%s-%s.bin",
599                   ath10k_bus_str(ar->hif.bus), ar->spec_board_id);
600
601         ar->board = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir, filename);
602         if (IS_ERR(ar->board))
603                 return PTR_ERR(ar->board);
604
605         ar->board_data = ar->board->data;
606         ar->board_len = ar->board->size;
607         ar->spec_board_loaded = true;
608
609         return 0;
610 }
611
612 static int ath10k_core_fetch_generic_board_file(struct ath10k *ar)
613 {
614         if (!ar->hw_params.fw.board) {
615                 ath10k_err(ar, "failed to find board file fw entry\n");
616                 return -EINVAL;
617         }
618
619         ar->board = ath10k_fetch_fw_file(ar,
620                                          ar->hw_params.fw.dir,
621                                          ar->hw_params.fw.board);
622         if (IS_ERR(ar->board))
623                 return PTR_ERR(ar->board);
624
625         ar->board_data = ar->board->data;
626         ar->board_len = ar->board->size;
627         ar->spec_board_loaded = false;
628
629         return 0;
630 }
631
632 static int ath10k_core_fetch_board_file(struct ath10k *ar)
633 {
634         int ret;
635
636         if (strlen(ar->spec_board_id) > 0) {
637                 ret = ath10k_core_fetch_spec_board_file(ar);
638                 if (ret) {
639                         ath10k_info(ar, "failed to load spec board file, falling back to generic: %d\n",
640                                     ret);
641                         goto generic;
642                 }
643
644                 ath10k_dbg(ar, ATH10K_DBG_BOOT, "found specific board file for %s\n",
645                            ar->spec_board_id);
646                 return 0;
647         }
648
649 generic:
650         ret = ath10k_core_fetch_generic_board_file(ar);
651         if (ret) {
652                 ath10k_err(ar, "failed to fetch generic board data: %d\n", ret);
653                 return ret;
654         }
655
656         return 0;
657 }
658
659 static int ath10k_core_fetch_firmware_api_1(struct ath10k *ar)
660 {
661         int ret = 0;
662
663         if (ar->hw_params.fw.fw == NULL) {
664                 ath10k_err(ar, "firmware file not defined\n");
665                 return -EINVAL;
666         }
667
668         ar->firmware = ath10k_fetch_fw_file(ar,
669                                             ar->hw_params.fw.dir,
670                                             ar->hw_params.fw.fw);
671         if (IS_ERR(ar->firmware)) {
672                 ret = PTR_ERR(ar->firmware);
673                 ath10k_err(ar, "could not fetch firmware (%d)\n", ret);
674                 goto err;
675         }
676
677         ar->firmware_data = ar->firmware->data;
678         ar->firmware_len = ar->firmware->size;
679
680         /* OTP may be undefined. If so, don't fetch it at all */
681         if (ar->hw_params.fw.otp == NULL)
682                 return 0;
683
684         ar->otp = ath10k_fetch_fw_file(ar,
685                                        ar->hw_params.fw.dir,
686                                        ar->hw_params.fw.otp);
687         if (IS_ERR(ar->otp)) {
688                 ret = PTR_ERR(ar->otp);
689                 ath10k_err(ar, "could not fetch otp (%d)\n", ret);
690                 goto err;
691         }
692
693         ar->otp_data = ar->otp->data;
694         ar->otp_len = ar->otp->size;
695
696         return 0;
697
698 err:
699         ath10k_core_free_firmware_files(ar);
700         return ret;
701 }
702
703 static int ath10k_core_fetch_firmware_api_n(struct ath10k *ar, const char *name)
704 {
705         size_t magic_len, len, ie_len;
706         int ie_id, i, index, bit, ret;
707         struct ath10k_fw_ie *hdr;
708         const u8 *data;
709         __le32 *timestamp, *version;
710
711         /* first fetch the firmware file (firmware-*.bin) */
712         ar->firmware = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir, name);
713         if (IS_ERR(ar->firmware)) {
714                 ath10k_err(ar, "could not fetch firmware file '%s/%s': %ld\n",
715                            ar->hw_params.fw.dir, name, PTR_ERR(ar->firmware));
716                 return PTR_ERR(ar->firmware);
717         }
718
719         data = ar->firmware->data;
720         len = ar->firmware->size;
721
722         /* magic also includes the null byte, check that as well */
723         magic_len = strlen(ATH10K_FIRMWARE_MAGIC) + 1;
724
725         if (len < magic_len) {
726                 ath10k_err(ar, "firmware file '%s/%s' too small to contain magic: %zu\n",
727                            ar->hw_params.fw.dir, name, len);
728                 ret = -EINVAL;
729                 goto err;
730         }
731
732         if (memcmp(data, ATH10K_FIRMWARE_MAGIC, magic_len) != 0) {
733                 ath10k_err(ar, "invalid firmware magic\n");
734                 ret = -EINVAL;
735                 goto err;
736         }
737
738         /* jump over the padding */
739         magic_len = ALIGN(magic_len, 4);
740
741         len -= magic_len;
742         data += magic_len;
743
744         /* loop elements */
745         while (len > sizeof(struct ath10k_fw_ie)) {
746                 hdr = (struct ath10k_fw_ie *)data;
747
748                 ie_id = le32_to_cpu(hdr->id);
749                 ie_len = le32_to_cpu(hdr->len);
750
751                 len -= sizeof(*hdr);
752                 data += sizeof(*hdr);
753
754                 if (len < ie_len) {
755                         ath10k_err(ar, "invalid length for FW IE %d (%zu < %zu)\n",
756                                    ie_id, len, ie_len);
757                         ret = -EINVAL;
758                         goto err;
759                 }
760
761                 switch (ie_id) {
762                 case ATH10K_FW_IE_FW_VERSION:
763                         if (ie_len > sizeof(ar->hw->wiphy->fw_version) - 1)
764                                 break;
765
766                         memcpy(ar->hw->wiphy->fw_version, data, ie_len);
767                         ar->hw->wiphy->fw_version[ie_len] = '\0';
768
769                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
770                                    "found fw version %s\n",
771                                     ar->hw->wiphy->fw_version);
772                         break;
773                 case ATH10K_FW_IE_TIMESTAMP:
774                         if (ie_len != sizeof(u32))
775                                 break;
776
777                         timestamp = (__le32 *)data;
778
779                         ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw timestamp %d\n",
780                                    le32_to_cpup(timestamp));
781                         break;
782                 case ATH10K_FW_IE_FEATURES:
783                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
784                                    "found firmware features ie (%zd B)\n",
785                                    ie_len);
786
787                         for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
788                                 index = i / 8;
789                                 bit = i % 8;
790
791                                 if (index == ie_len)
792                                         break;
793
794                                 if (data[index] & (1 << bit)) {
795                                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
796                                                    "Enabling feature bit: %i\n",
797                                                    i);
798                                         __set_bit(i, ar->fw_features);
799                                 }
800                         }
801
802                         ath10k_dbg_dump(ar, ATH10K_DBG_BOOT, "features", "",
803                                         ar->fw_features,
804                                         sizeof(ar->fw_features));
805                         break;
806                 case ATH10K_FW_IE_FW_IMAGE:
807                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
808                                    "found fw image ie (%zd B)\n",
809                                    ie_len);
810
811                         ar->firmware_data = data;
812                         ar->firmware_len = ie_len;
813
814                         break;
815                 case ATH10K_FW_IE_OTP_IMAGE:
816                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
817                                    "found otp image ie (%zd B)\n",
818                                    ie_len);
819
820                         ar->otp_data = data;
821                         ar->otp_len = ie_len;
822
823                         break;
824                 case ATH10K_FW_IE_WMI_OP_VERSION:
825                         if (ie_len != sizeof(u32))
826                                 break;
827
828                         version = (__le32 *)data;
829
830                         ar->wmi.op_version = le32_to_cpup(version);
831
832                         ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw ie wmi op version %d\n",
833                                    ar->wmi.op_version);
834                         break;
835                 case ATH10K_FW_IE_HTT_OP_VERSION:
836                         if (ie_len != sizeof(u32))
837                                 break;
838
839                         version = (__le32 *)data;
840
841                         ar->htt.op_version = le32_to_cpup(version);
842
843                         ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw ie htt op version %d\n",
844                                    ar->htt.op_version);
845                         break;
846                 case ATH10K_FW_IE_FW_CODE_SWAP_IMAGE:
847                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
848                                    "found fw code swap image ie (%zd B)\n",
849                                    ie_len);
850                         ar->swap.firmware_codeswap_data = data;
851                         ar->swap.firmware_codeswap_len = ie_len;
852                         break;
853                 default:
854                         ath10k_warn(ar, "Unknown FW IE: %u\n",
855                                     le32_to_cpu(hdr->id));
856                         break;
857                 }
858
859                 /* jump over the padding */
860                 ie_len = ALIGN(ie_len, 4);
861
862                 len -= ie_len;
863                 data += ie_len;
864         }
865
866         if (!ar->firmware_data || !ar->firmware_len) {
867                 ath10k_warn(ar, "No ATH10K_FW_IE_FW_IMAGE found from '%s/%s', skipping\n",
868                             ar->hw_params.fw.dir, name);
869                 ret = -ENOMEDIUM;
870                 goto err;
871         }
872
873         return 0;
874
875 err:
876         ath10k_core_free_firmware_files(ar);
877         return ret;
878 }
879
880 static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
881 {
882         int ret;
883
884         /* calibration file is optional, don't check for any errors */
885         ath10k_fetch_cal_file(ar);
886
887         ret = ath10k_core_fetch_board_file(ar);
888         if (ret) {
889                 ath10k_err(ar, "failed to fetch board file: %d\n", ret);
890                 return ret;
891         }
892
893         ar->fw_api = 5;
894         ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
895
896         ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API5_FILE);
897         if (ret == 0)
898                 goto success;
899
900         ar->fw_api = 4;
901         ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
902
903         ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API4_FILE);
904         if (ret == 0)
905                 goto success;
906
907         ar->fw_api = 3;
908         ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
909
910         ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API3_FILE);
911         if (ret == 0)
912                 goto success;
913
914         ar->fw_api = 2;
915         ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
916
917         ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API2_FILE);
918         if (ret == 0)
919                 goto success;
920
921         ar->fw_api = 1;
922         ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n", ar->fw_api);
923
924         ret = ath10k_core_fetch_firmware_api_1(ar);
925         if (ret)
926                 return ret;
927
928 success:
929         ath10k_dbg(ar, ATH10K_DBG_BOOT, "using fw api %d\n", ar->fw_api);
930
931         return 0;
932 }
933
934 static int ath10k_download_cal_data(struct ath10k *ar)
935 {
936         int ret;
937
938         ret = ath10k_download_cal_file(ar);
939         if (ret == 0) {
940                 ar->cal_mode = ATH10K_CAL_MODE_FILE;
941                 goto done;
942         }
943
944         ath10k_dbg(ar, ATH10K_DBG_BOOT,
945                    "boot did not find a calibration file, try DT next: %d\n",
946                    ret);
947
948         ret = ath10k_download_cal_dt(ar);
949         if (ret == 0) {
950                 ar->cal_mode = ATH10K_CAL_MODE_DT;
951                 goto done;
952         }
953
954         ath10k_dbg(ar, ATH10K_DBG_BOOT,
955                    "boot did not find DT entry, try OTP next: %d\n",
956                    ret);
957
958         ret = ath10k_download_and_run_otp(ar);
959         if (ret) {
960                 ath10k_err(ar, "failed to run otp: %d\n", ret);
961                 return ret;
962         }
963
964         ar->cal_mode = ATH10K_CAL_MODE_OTP;
965
966 done:
967         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot using calibration mode %s\n",
968                    ath10k_cal_mode_str(ar->cal_mode));
969         return 0;
970 }
971
972 static int ath10k_init_uart(struct ath10k *ar)
973 {
974         int ret;
975
976         /*
977          * Explicitly setting UART prints to zero as target turns it on
978          * based on scratch registers.
979          */
980         ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
981         if (ret) {
982                 ath10k_warn(ar, "could not disable UART prints (%d)\n", ret);
983                 return ret;
984         }
985
986         if (!uart_print)
987                 return 0;
988
989         ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, ar->hw_params.uart_pin);
990         if (ret) {
991                 ath10k_warn(ar, "could not enable UART prints (%d)\n", ret);
992                 return ret;
993         }
994
995         ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
996         if (ret) {
997                 ath10k_warn(ar, "could not enable UART prints (%d)\n", ret);
998                 return ret;
999         }
1000
1001         /* Set the UART baud rate to 19200. */
1002         ret = ath10k_bmi_write32(ar, hi_desired_baud_rate, 19200);
1003         if (ret) {
1004                 ath10k_warn(ar, "could not set the baud rate (%d)\n", ret);
1005                 return ret;
1006         }
1007
1008         ath10k_info(ar, "UART prints enabled\n");
1009         return 0;
1010 }
1011
1012 static int ath10k_init_hw_params(struct ath10k *ar)
1013 {
1014         const struct ath10k_hw_params *uninitialized_var(hw_params);
1015         int i;
1016
1017         for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
1018                 hw_params = &ath10k_hw_params_list[i];
1019
1020                 if (hw_params->id == ar->target_version)
1021                         break;
1022         }
1023
1024         if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
1025                 ath10k_err(ar, "Unsupported hardware version: 0x%x\n",
1026                            ar->target_version);
1027                 return -EINVAL;
1028         }
1029
1030         ar->hw_params = *hw_params;
1031
1032         ath10k_dbg(ar, ATH10K_DBG_BOOT, "Hardware name %s version 0x%x\n",
1033                    ar->hw_params.name, ar->target_version);
1034
1035         return 0;
1036 }
1037
1038 static void ath10k_core_restart(struct work_struct *work)
1039 {
1040         struct ath10k *ar = container_of(work, struct ath10k, restart_work);
1041
1042         set_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
1043
1044         /* Place a barrier to make sure the compiler doesn't reorder
1045          * CRASH_FLUSH and calling other functions.
1046          */
1047         barrier();
1048
1049         ieee80211_stop_queues(ar->hw);
1050         ath10k_drain_tx(ar);
1051         complete_all(&ar->scan.started);
1052         complete_all(&ar->scan.completed);
1053         complete_all(&ar->scan.on_channel);
1054         complete_all(&ar->offchan_tx_completed);
1055         complete_all(&ar->install_key_done);
1056         complete_all(&ar->vdev_setup_done);
1057         complete_all(&ar->thermal.wmi_sync);
1058         wake_up(&ar->htt.empty_tx_wq);
1059         wake_up(&ar->wmi.tx_credits_wq);
1060         wake_up(&ar->peer_mapping_wq);
1061
1062         mutex_lock(&ar->conf_mutex);
1063
1064         switch (ar->state) {
1065         case ATH10K_STATE_ON:
1066                 ar->state = ATH10K_STATE_RESTARTING;
1067                 ath10k_hif_stop(ar);
1068                 ath10k_scan_finish(ar);
1069                 ieee80211_restart_hw(ar->hw);
1070                 break;
1071         case ATH10K_STATE_OFF:
1072                 /* this can happen if driver is being unloaded
1073                  * or if the crash happens during FW probing */
1074                 ath10k_warn(ar, "cannot restart a device that hasn't been started\n");
1075                 break;
1076         case ATH10K_STATE_RESTARTING:
1077                 /* hw restart might be requested from multiple places */
1078                 break;
1079         case ATH10K_STATE_RESTARTED:
1080                 ar->state = ATH10K_STATE_WEDGED;
1081                 /* fall through */
1082         case ATH10K_STATE_WEDGED:
1083                 ath10k_warn(ar, "device is wedged, will not restart\n");
1084                 break;
1085         case ATH10K_STATE_UTF:
1086                 ath10k_warn(ar, "firmware restart in UTF mode not supported\n");
1087                 break;
1088         }
1089
1090         mutex_unlock(&ar->conf_mutex);
1091 }
1092
1093 static int ath10k_core_init_firmware_features(struct ath10k *ar)
1094 {
1095         if (test_bit(ATH10K_FW_FEATURE_WMI_10_2, ar->fw_features) &&
1096             !test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
1097                 ath10k_err(ar, "feature bits corrupted: 10.2 feature requires 10.x feature to be set as well");
1098                 return -EINVAL;
1099         }
1100
1101         if (ar->wmi.op_version >= ATH10K_FW_WMI_OP_VERSION_MAX) {
1102                 ath10k_err(ar, "unsupported WMI OP version (max %d): %d\n",
1103                            ATH10K_FW_WMI_OP_VERSION_MAX, ar->wmi.op_version);
1104                 return -EINVAL;
1105         }
1106
1107         ar->wmi.rx_decap_mode = ATH10K_HW_TXRX_NATIVE_WIFI;
1108         switch (ath10k_cryptmode_param) {
1109         case ATH10K_CRYPT_MODE_HW:
1110                 clear_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags);
1111                 clear_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags);
1112                 break;
1113         case ATH10K_CRYPT_MODE_SW:
1114                 if (!test_bit(ATH10K_FW_FEATURE_RAW_MODE_SUPPORT,
1115                               ar->fw_features)) {
1116                         ath10k_err(ar, "cryptmode > 0 requires raw mode support from firmware");
1117                         return -EINVAL;
1118                 }
1119
1120                 set_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags);
1121                 set_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags);
1122                 break;
1123         default:
1124                 ath10k_info(ar, "invalid cryptmode: %d\n",
1125                             ath10k_cryptmode_param);
1126                 return -EINVAL;
1127         }
1128
1129         ar->htt.max_num_amsdu = ATH10K_HTT_MAX_NUM_AMSDU_DEFAULT;
1130         ar->htt.max_num_ampdu = ATH10K_HTT_MAX_NUM_AMPDU_DEFAULT;
1131
1132         if (rawmode) {
1133                 if (!test_bit(ATH10K_FW_FEATURE_RAW_MODE_SUPPORT,
1134                               ar->fw_features)) {
1135                         ath10k_err(ar, "rawmode = 1 requires support from firmware");
1136                         return -EINVAL;
1137                 }
1138                 set_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags);
1139         }
1140
1141         if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
1142                 ar->wmi.rx_decap_mode = ATH10K_HW_TXRX_RAW;
1143
1144                 /* Workaround:
1145                  *
1146                  * Firmware A-MSDU aggregation breaks with RAW Tx encap mode
1147                  * and causes enormous performance issues (malformed frames,
1148                  * etc).
1149                  *
1150                  * Disabling A-MSDU makes RAW mode stable with heavy traffic
1151                  * albeit a bit slower compared to regular operation.
1152                  */
1153                 ar->htt.max_num_amsdu = 1;
1154         }
1155
1156         /* Backwards compatibility for firmwares without
1157          * ATH10K_FW_IE_WMI_OP_VERSION.
1158          */
1159         if (ar->wmi.op_version == ATH10K_FW_WMI_OP_VERSION_UNSET) {
1160                 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
1161                         if (test_bit(ATH10K_FW_FEATURE_WMI_10_2,
1162                                      ar->fw_features))
1163                                 ar->wmi.op_version = ATH10K_FW_WMI_OP_VERSION_10_2;
1164                         else
1165                                 ar->wmi.op_version = ATH10K_FW_WMI_OP_VERSION_10_1;
1166                 } else {
1167                         ar->wmi.op_version = ATH10K_FW_WMI_OP_VERSION_MAIN;
1168                 }
1169         }
1170
1171         switch (ar->wmi.op_version) {
1172         case ATH10K_FW_WMI_OP_VERSION_MAIN:
1173                 ar->max_num_peers = TARGET_NUM_PEERS;
1174                 ar->max_num_stations = TARGET_NUM_STATIONS;
1175                 ar->max_num_vdevs = TARGET_NUM_VDEVS;
1176                 ar->htt.max_num_pending_tx = TARGET_NUM_MSDU_DESC;
1177                 ar->fw_stats_req_mask = WMI_STAT_PDEV | WMI_STAT_VDEV |
1178                         WMI_STAT_PEER;
1179                 ar->max_spatial_stream = WMI_MAX_SPATIAL_STREAM;
1180                 break;
1181         case ATH10K_FW_WMI_OP_VERSION_10_1:
1182         case ATH10K_FW_WMI_OP_VERSION_10_2:
1183         case ATH10K_FW_WMI_OP_VERSION_10_2_4:
1184                 ar->max_num_peers = TARGET_10X_NUM_PEERS;
1185                 ar->max_num_stations = TARGET_10X_NUM_STATIONS;
1186                 ar->max_num_vdevs = TARGET_10X_NUM_VDEVS;
1187                 ar->htt.max_num_pending_tx = TARGET_10X_NUM_MSDU_DESC;
1188                 ar->fw_stats_req_mask = WMI_STAT_PEER;
1189                 ar->max_spatial_stream = WMI_MAX_SPATIAL_STREAM;
1190                 break;
1191         case ATH10K_FW_WMI_OP_VERSION_TLV:
1192                 ar->max_num_peers = TARGET_TLV_NUM_PEERS;
1193                 ar->max_num_stations = TARGET_TLV_NUM_STATIONS;
1194                 ar->max_num_vdevs = TARGET_TLV_NUM_VDEVS;
1195                 ar->max_num_tdls_vdevs = TARGET_TLV_NUM_TDLS_VDEVS;
1196                 ar->htt.max_num_pending_tx = TARGET_TLV_NUM_MSDU_DESC;
1197                 ar->wow.max_num_patterns = TARGET_TLV_NUM_WOW_PATTERNS;
1198                 ar->fw_stats_req_mask = WMI_STAT_PDEV | WMI_STAT_VDEV |
1199                         WMI_STAT_PEER;
1200                 ar->max_spatial_stream = WMI_MAX_SPATIAL_STREAM;
1201                 break;
1202         case ATH10K_FW_WMI_OP_VERSION_10_4:
1203                 ar->max_num_peers = TARGET_10_4_NUM_PEERS;
1204                 ar->max_num_stations = TARGET_10_4_NUM_STATIONS;
1205                 ar->num_active_peers = TARGET_10_4_ACTIVE_PEERS;
1206                 ar->max_num_vdevs = TARGET_10_4_NUM_VDEVS;
1207                 ar->num_tids = TARGET_10_4_TGT_NUM_TIDS;
1208                 ar->htt.max_num_pending_tx = TARGET_10_4_NUM_MSDU_DESC;
1209                 ar->fw_stats_req_mask = WMI_STAT_PEER;
1210                 ar->max_spatial_stream = WMI_10_4_MAX_SPATIAL_STREAM;
1211                 break;
1212         case ATH10K_FW_WMI_OP_VERSION_UNSET:
1213         case ATH10K_FW_WMI_OP_VERSION_MAX:
1214                 WARN_ON(1);
1215                 return -EINVAL;
1216         }
1217
1218         /* Backwards compatibility for firmwares without
1219          * ATH10K_FW_IE_HTT_OP_VERSION.
1220          */
1221         if (ar->htt.op_version == ATH10K_FW_HTT_OP_VERSION_UNSET) {
1222                 switch (ar->wmi.op_version) {
1223                 case ATH10K_FW_WMI_OP_VERSION_MAIN:
1224                         ar->htt.op_version = ATH10K_FW_HTT_OP_VERSION_MAIN;
1225                         break;
1226                 case ATH10K_FW_WMI_OP_VERSION_10_1:
1227                 case ATH10K_FW_WMI_OP_VERSION_10_2:
1228                 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
1229                         ar->htt.op_version = ATH10K_FW_HTT_OP_VERSION_10_1;
1230                         break;
1231                 case ATH10K_FW_WMI_OP_VERSION_TLV:
1232                         ar->htt.op_version = ATH10K_FW_HTT_OP_VERSION_TLV;
1233                         break;
1234                 case ATH10K_FW_WMI_OP_VERSION_10_4:
1235                 case ATH10K_FW_WMI_OP_VERSION_UNSET:
1236                 case ATH10K_FW_WMI_OP_VERSION_MAX:
1237                         WARN_ON(1);
1238                         return -EINVAL;
1239                 }
1240         }
1241
1242         return 0;
1243 }
1244
1245 int ath10k_core_start(struct ath10k *ar, enum ath10k_firmware_mode mode)
1246 {
1247         int status;
1248
1249         lockdep_assert_held(&ar->conf_mutex);
1250
1251         clear_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
1252
1253         ath10k_bmi_start(ar);
1254
1255         if (ath10k_init_configure_target(ar)) {
1256                 status = -EINVAL;
1257                 goto err;
1258         }
1259
1260         status = ath10k_download_cal_data(ar);
1261         if (status)
1262                 goto err;
1263
1264         /* Some of of qca988x solutions are having global reset issue
1265          * during target initialization. Bypassing PLL setting before
1266          * downloading firmware and letting the SoC run on REF_CLK is
1267          * fixing the problem. Corresponding firmware change is also needed
1268          * to set the clock source once the target is initialized.
1269          */
1270         if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_SKIP_CLOCK_INIT,
1271                      ar->fw_features)) {
1272                 status = ath10k_bmi_write32(ar, hi_skip_clock_init, 1);
1273                 if (status) {
1274                         ath10k_err(ar, "could not write to skip_clock_init: %d\n",
1275                                    status);
1276                         goto err;
1277                 }
1278         }
1279
1280         status = ath10k_download_fw(ar, mode);
1281         if (status)
1282                 goto err;
1283
1284         status = ath10k_init_uart(ar);
1285         if (status)
1286                 goto err;
1287
1288         ar->htc.htc_ops.target_send_suspend_complete =
1289                 ath10k_send_suspend_complete;
1290
1291         status = ath10k_htc_init(ar);
1292         if (status) {
1293                 ath10k_err(ar, "could not init HTC (%d)\n", status);
1294                 goto err;
1295         }
1296
1297         status = ath10k_bmi_done(ar);
1298         if (status)
1299                 goto err;
1300
1301         status = ath10k_wmi_attach(ar);
1302         if (status) {
1303                 ath10k_err(ar, "WMI attach failed: %d\n", status);
1304                 goto err;
1305         }
1306
1307         status = ath10k_htt_init(ar);
1308         if (status) {
1309                 ath10k_err(ar, "failed to init htt: %d\n", status);
1310                 goto err_wmi_detach;
1311         }
1312
1313         status = ath10k_htt_tx_alloc(&ar->htt);
1314         if (status) {
1315                 ath10k_err(ar, "failed to alloc htt tx: %d\n", status);
1316                 goto err_wmi_detach;
1317         }
1318
1319         status = ath10k_htt_rx_alloc(&ar->htt);
1320         if (status) {
1321                 ath10k_err(ar, "failed to alloc htt rx: %d\n", status);
1322                 goto err_htt_tx_detach;
1323         }
1324
1325         status = ath10k_hif_start(ar);
1326         if (status) {
1327                 ath10k_err(ar, "could not start HIF: %d\n", status);
1328                 goto err_htt_rx_detach;
1329         }
1330
1331         status = ath10k_htc_wait_target(&ar->htc);
1332         if (status) {
1333                 ath10k_err(ar, "failed to connect to HTC: %d\n", status);
1334                 goto err_hif_stop;
1335         }
1336
1337         if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
1338                 status = ath10k_htt_connect(&ar->htt);
1339                 if (status) {
1340                         ath10k_err(ar, "failed to connect htt (%d)\n", status);
1341                         goto err_hif_stop;
1342                 }
1343         }
1344
1345         status = ath10k_wmi_connect(ar);
1346         if (status) {
1347                 ath10k_err(ar, "could not connect wmi: %d\n", status);
1348                 goto err_hif_stop;
1349         }
1350
1351         status = ath10k_htc_start(&ar->htc);
1352         if (status) {
1353                 ath10k_err(ar, "failed to start htc: %d\n", status);
1354                 goto err_hif_stop;
1355         }
1356
1357         if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
1358                 status = ath10k_wmi_wait_for_service_ready(ar);
1359                 if (status) {
1360                         ath10k_warn(ar, "wmi service ready event not received");
1361                         goto err_hif_stop;
1362                 }
1363         }
1364
1365         ath10k_dbg(ar, ATH10K_DBG_BOOT, "firmware %s booted\n",
1366                    ar->hw->wiphy->fw_version);
1367
1368         status = ath10k_wmi_cmd_init(ar);
1369         if (status) {
1370                 ath10k_err(ar, "could not send WMI init command (%d)\n",
1371                            status);
1372                 goto err_hif_stop;
1373         }
1374
1375         status = ath10k_wmi_wait_for_unified_ready(ar);
1376         if (status) {
1377                 ath10k_err(ar, "wmi unified ready event not received\n");
1378                 goto err_hif_stop;
1379         }
1380
1381         /* If firmware indicates Full Rx Reorder support it must be used in a
1382          * slightly different manner. Let HTT code know.
1383          */
1384         ar->htt.rx_ring.in_ord_rx = !!(test_bit(WMI_SERVICE_RX_FULL_REORDER,
1385                                                 ar->wmi.svc_map));
1386
1387         status = ath10k_htt_rx_ring_refill(ar);
1388         if (status) {
1389                 ath10k_err(ar, "failed to refill htt rx ring: %d\n", status);
1390                 goto err_hif_stop;
1391         }
1392
1393         /* we don't care about HTT in UTF mode */
1394         if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
1395                 status = ath10k_htt_setup(&ar->htt);
1396                 if (status) {
1397                         ath10k_err(ar, "failed to setup htt: %d\n", status);
1398                         goto err_hif_stop;
1399                 }
1400         }
1401
1402         status = ath10k_debug_start(ar);
1403         if (status)
1404                 goto err_hif_stop;
1405
1406         ar->free_vdev_map = (1LL << ar->max_num_vdevs) - 1;
1407
1408         INIT_LIST_HEAD(&ar->arvifs);
1409
1410         return 0;
1411
1412 err_hif_stop:
1413         ath10k_hif_stop(ar);
1414 err_htt_rx_detach:
1415         ath10k_htt_rx_free(&ar->htt);
1416 err_htt_tx_detach:
1417         ath10k_htt_tx_free(&ar->htt);
1418 err_wmi_detach:
1419         ath10k_wmi_detach(ar);
1420 err:
1421         return status;
1422 }
1423 EXPORT_SYMBOL(ath10k_core_start);
1424
1425 int ath10k_wait_for_suspend(struct ath10k *ar, u32 suspend_opt)
1426 {
1427         int ret;
1428         unsigned long time_left;
1429
1430         reinit_completion(&ar->target_suspend);
1431
1432         ret = ath10k_wmi_pdev_suspend_target(ar, suspend_opt);
1433         if (ret) {
1434                 ath10k_warn(ar, "could not suspend target (%d)\n", ret);
1435                 return ret;
1436         }
1437
1438         time_left = wait_for_completion_timeout(&ar->target_suspend, 1 * HZ);
1439
1440         if (!time_left) {
1441                 ath10k_warn(ar, "suspend timed out - target pause event never came\n");
1442                 return -ETIMEDOUT;
1443         }
1444
1445         return 0;
1446 }
1447
1448 void ath10k_core_stop(struct ath10k *ar)
1449 {
1450         lockdep_assert_held(&ar->conf_mutex);
1451         ath10k_debug_stop(ar);
1452
1453         /* try to suspend target */
1454         if (ar->state != ATH10K_STATE_RESTARTING &&
1455             ar->state != ATH10K_STATE_UTF)
1456                 ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND_AND_DISABLE_INTR);
1457
1458         ath10k_hif_stop(ar);
1459         ath10k_htt_tx_free(&ar->htt);
1460         ath10k_htt_rx_free(&ar->htt);
1461         ath10k_wmi_detach(ar);
1462 }
1463 EXPORT_SYMBOL(ath10k_core_stop);
1464
1465 /* mac80211 manages fw/hw initialization through start/stop hooks. However in
1466  * order to know what hw capabilities should be advertised to mac80211 it is
1467  * necessary to load the firmware (and tear it down immediately since start
1468  * hook will try to init it again) before registering */
1469 static int ath10k_core_probe_fw(struct ath10k *ar)
1470 {
1471         struct bmi_target_info target_info;
1472         int ret = 0;
1473
1474         ret = ath10k_hif_power_up(ar);
1475         if (ret) {
1476                 ath10k_err(ar, "could not start pci hif (%d)\n", ret);
1477                 return ret;
1478         }
1479
1480         memset(&target_info, 0, sizeof(target_info));
1481         ret = ath10k_bmi_get_target_info(ar, &target_info);
1482         if (ret) {
1483                 ath10k_err(ar, "could not get target info (%d)\n", ret);
1484                 goto err_power_down;
1485         }
1486
1487         ar->target_version = target_info.version;
1488         ar->hw->wiphy->hw_version = target_info.version;
1489
1490         ret = ath10k_init_hw_params(ar);
1491         if (ret) {
1492                 ath10k_err(ar, "could not get hw params (%d)\n", ret);
1493                 goto err_power_down;
1494         }
1495
1496         ret = ath10k_core_fetch_firmware_files(ar);
1497         if (ret) {
1498                 ath10k_err(ar, "could not fetch firmware files (%d)\n", ret);
1499                 goto err_power_down;
1500         }
1501
1502         ret = ath10k_core_init_firmware_features(ar);
1503         if (ret) {
1504                 ath10k_err(ar, "fatal problem with firmware features: %d\n",
1505                            ret);
1506                 goto err_free_firmware_files;
1507         }
1508
1509         ret = ath10k_swap_code_seg_init(ar);
1510         if (ret) {
1511                 ath10k_err(ar, "failed to initialize code swap segment: %d\n",
1512                            ret);
1513                 goto err_free_firmware_files;
1514         }
1515
1516         mutex_lock(&ar->conf_mutex);
1517
1518         ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
1519         if (ret) {
1520                 ath10k_err(ar, "could not init core (%d)\n", ret);
1521                 goto err_unlock;
1522         }
1523
1524         ath10k_print_driver_info(ar);
1525         ath10k_core_stop(ar);
1526
1527         mutex_unlock(&ar->conf_mutex);
1528
1529         ath10k_hif_power_down(ar);
1530         return 0;
1531
1532 err_unlock:
1533         mutex_unlock(&ar->conf_mutex);
1534
1535 err_free_firmware_files:
1536         ath10k_core_free_firmware_files(ar);
1537
1538 err_power_down:
1539         ath10k_hif_power_down(ar);
1540
1541         return ret;
1542 }
1543
1544 static void ath10k_core_register_work(struct work_struct *work)
1545 {
1546         struct ath10k *ar = container_of(work, struct ath10k, register_work);
1547         int status;
1548
1549         status = ath10k_core_probe_fw(ar);
1550         if (status) {
1551                 ath10k_err(ar, "could not probe fw (%d)\n", status);
1552                 goto err;
1553         }
1554
1555         status = ath10k_mac_register(ar);
1556         if (status) {
1557                 ath10k_err(ar, "could not register to mac80211 (%d)\n", status);
1558                 goto err_release_fw;
1559         }
1560
1561         status = ath10k_debug_register(ar);
1562         if (status) {
1563                 ath10k_err(ar, "unable to initialize debugfs\n");
1564                 goto err_unregister_mac;
1565         }
1566
1567         status = ath10k_spectral_create(ar);
1568         if (status) {
1569                 ath10k_err(ar, "failed to initialize spectral\n");
1570                 goto err_debug_destroy;
1571         }
1572
1573         status = ath10k_thermal_register(ar);
1574         if (status) {
1575                 ath10k_err(ar, "could not register thermal device: %d\n",
1576                            status);
1577                 goto err_spectral_destroy;
1578         }
1579
1580         set_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags);
1581         return;
1582
1583 err_spectral_destroy:
1584         ath10k_spectral_destroy(ar);
1585 err_debug_destroy:
1586         ath10k_debug_destroy(ar);
1587 err_unregister_mac:
1588         ath10k_mac_unregister(ar);
1589 err_release_fw:
1590         ath10k_core_free_firmware_files(ar);
1591 err:
1592         /* TODO: It's probably a good idea to release device from the driver
1593          * but calling device_release_driver() here will cause a deadlock.
1594          */
1595         return;
1596 }
1597
1598 int ath10k_core_register(struct ath10k *ar, u32 chip_id)
1599 {
1600         ar->chip_id = chip_id;
1601         queue_work(ar->workqueue, &ar->register_work);
1602
1603         return 0;
1604 }
1605 EXPORT_SYMBOL(ath10k_core_register);
1606
1607 void ath10k_core_unregister(struct ath10k *ar)
1608 {
1609         cancel_work_sync(&ar->register_work);
1610
1611         if (!test_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags))
1612                 return;
1613
1614         ath10k_thermal_unregister(ar);
1615         /* Stop spectral before unregistering from mac80211 to remove the
1616          * relayfs debugfs file cleanly. Otherwise the parent debugfs tree
1617          * would be already be free'd recursively, leading to a double free.
1618          */
1619         ath10k_spectral_destroy(ar);
1620
1621         /* We must unregister from mac80211 before we stop HTC and HIF.
1622          * Otherwise we will fail to submit commands to FW and mac80211 will be
1623          * unhappy about callback failures. */
1624         ath10k_mac_unregister(ar);
1625
1626         ath10k_testmode_destroy(ar);
1627
1628         ath10k_core_free_firmware_files(ar);
1629
1630         ath10k_debug_unregister(ar);
1631 }
1632 EXPORT_SYMBOL(ath10k_core_unregister);
1633
1634 struct ath10k *ath10k_core_create(size_t priv_size, struct device *dev,
1635                                   enum ath10k_bus bus,
1636                                   enum ath10k_hw_rev hw_rev,
1637                                   const struct ath10k_hif_ops *hif_ops)
1638 {
1639         struct ath10k *ar;
1640         int ret;
1641
1642         ar = ath10k_mac_create(priv_size);
1643         if (!ar)
1644                 return NULL;
1645
1646         ar->ath_common.priv = ar;
1647         ar->ath_common.hw = ar->hw;
1648         ar->dev = dev;
1649         ar->hw_rev = hw_rev;
1650         ar->hif.ops = hif_ops;
1651         ar->hif.bus = bus;
1652
1653         switch (hw_rev) {
1654         case ATH10K_HW_QCA988X:
1655                 ar->regs = &qca988x_regs;
1656                 ar->hw_values = &qca988x_values;
1657                 break;
1658         case ATH10K_HW_QCA6174:
1659                 ar->regs = &qca6174_regs;
1660                 ar->hw_values = &qca6174_values;
1661                 break;
1662         case ATH10K_HW_QCA99X0:
1663                 ar->regs = &qca99x0_regs;
1664                 ar->hw_values = &qca99x0_values;
1665                 break;
1666         default:
1667                 ath10k_err(ar, "unsupported core hardware revision %d\n",
1668                            hw_rev);
1669                 ret = -ENOTSUPP;
1670                 goto err_free_mac;
1671         }
1672
1673         init_completion(&ar->scan.started);
1674         init_completion(&ar->scan.completed);
1675         init_completion(&ar->scan.on_channel);
1676         init_completion(&ar->target_suspend);
1677         init_completion(&ar->wow.wakeup_completed);
1678
1679         init_completion(&ar->install_key_done);
1680         init_completion(&ar->vdev_setup_done);
1681         init_completion(&ar->thermal.wmi_sync);
1682
1683         INIT_DELAYED_WORK(&ar->scan.timeout, ath10k_scan_timeout_work);
1684
1685         ar->workqueue = create_singlethread_workqueue("ath10k_wq");
1686         if (!ar->workqueue)
1687                 goto err_free_mac;
1688
1689         ar->workqueue_aux = create_singlethread_workqueue("ath10k_aux_wq");
1690         if (!ar->workqueue_aux)
1691                 goto err_free_wq;
1692
1693         mutex_init(&ar->conf_mutex);
1694         spin_lock_init(&ar->data_lock);
1695
1696         INIT_LIST_HEAD(&ar->peers);
1697         init_waitqueue_head(&ar->peer_mapping_wq);
1698         init_waitqueue_head(&ar->htt.empty_tx_wq);
1699         init_waitqueue_head(&ar->wmi.tx_credits_wq);
1700
1701         init_completion(&ar->offchan_tx_completed);
1702         INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
1703         skb_queue_head_init(&ar->offchan_tx_queue);
1704
1705         INIT_WORK(&ar->wmi_mgmt_tx_work, ath10k_mgmt_over_wmi_tx_work);
1706         skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
1707
1708         INIT_WORK(&ar->register_work, ath10k_core_register_work);
1709         INIT_WORK(&ar->restart_work, ath10k_core_restart);
1710
1711         ret = ath10k_debug_create(ar);
1712         if (ret)
1713                 goto err_free_aux_wq;
1714
1715         return ar;
1716
1717 err_free_aux_wq:
1718         destroy_workqueue(ar->workqueue_aux);
1719 err_free_wq:
1720         destroy_workqueue(ar->workqueue);
1721
1722 err_free_mac:
1723         ath10k_mac_destroy(ar);
1724
1725         return NULL;
1726 }
1727 EXPORT_SYMBOL(ath10k_core_create);
1728
1729 void ath10k_core_destroy(struct ath10k *ar)
1730 {
1731         flush_workqueue(ar->workqueue);
1732         destroy_workqueue(ar->workqueue);
1733
1734         flush_workqueue(ar->workqueue_aux);
1735         destroy_workqueue(ar->workqueue_aux);
1736
1737         ath10k_debug_destroy(ar);
1738         ath10k_wmi_free_host_mem(ar);
1739         ath10k_mac_destroy(ar);
1740 }
1741 EXPORT_SYMBOL(ath10k_core_destroy);
1742
1743 MODULE_AUTHOR("Qualcomm Atheros");
1744 MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
1745 MODULE_LICENSE("Dual BSD/GPL");