Merge ath-next from git://git.kernel.org/pub/scm/linux/kernel/git/kvalo/ath.git
[muen/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 #include <linux/of.h>
21 #include <linux/dmi.h>
22 #include <linux/ctype.h>
23 #include <asm/byteorder.h>
24
25 #include "core.h"
26 #include "mac.h"
27 #include "htc.h"
28 #include "hif.h"
29 #include "wmi.h"
30 #include "bmi.h"
31 #include "debug.h"
32 #include "htt.h"
33 #include "testmode.h"
34 #include "wmi-ops.h"
35
36 unsigned int ath10k_debug_mask;
37 static unsigned int ath10k_cryptmode_param;
38 static bool uart_print;
39 static bool skip_otp;
40 static bool rawmode;
41
42 module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
43 module_param_named(cryptmode, ath10k_cryptmode_param, uint, 0644);
44 module_param(uart_print, bool, 0644);
45 module_param(skip_otp, bool, 0644);
46 module_param(rawmode, bool, 0644);
47
48 MODULE_PARM_DESC(debug_mask, "Debugging mask");
49 MODULE_PARM_DESC(uart_print, "Uart target debugging");
50 MODULE_PARM_DESC(skip_otp, "Skip otp failure for calibration in testmode");
51 MODULE_PARM_DESC(cryptmode, "Crypto mode: 0-hardware, 1-software");
52 MODULE_PARM_DESC(rawmode, "Use raw 802.11 frame datapath");
53
54 static const struct ath10k_hw_params ath10k_hw_params_list[] = {
55         {
56                 .id = QCA988X_HW_2_0_VERSION,
57                 .dev_id = QCA988X_2_0_DEVICE_ID,
58                 .name = "qca988x hw2.0",
59                 .patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
60                 .uart_pin = 7,
61                 .cc_wraparound_type = ATH10K_HW_CC_WRAP_SHIFTED_ALL,
62                 .otp_exe_param = 0,
63                 .channel_counters_freq_hz = 88000,
64                 .max_probe_resp_desc_thres = 0,
65                 .cal_data_len = 2116,
66                 .fw = {
67                         .dir = QCA988X_HW_2_0_FW_DIR,
68                         .board = QCA988X_HW_2_0_BOARD_DATA_FILE,
69                         .board_size = QCA988X_BOARD_DATA_SZ,
70                         .board_ext_size = QCA988X_BOARD_EXT_DATA_SZ,
71                 },
72                 .hw_ops = &qca988x_ops,
73                 .decap_align_bytes = 4,
74                 .spectral_bin_discard = 0,
75         },
76         {
77                 .id = QCA9887_HW_1_0_VERSION,
78                 .dev_id = QCA9887_1_0_DEVICE_ID,
79                 .name = "qca9887 hw1.0",
80                 .patch_load_addr = QCA9887_HW_1_0_PATCH_LOAD_ADDR,
81                 .uart_pin = 7,
82                 .cc_wraparound_type = ATH10K_HW_CC_WRAP_SHIFTED_ALL,
83                 .otp_exe_param = 0,
84                 .channel_counters_freq_hz = 88000,
85                 .max_probe_resp_desc_thres = 0,
86                 .cal_data_len = 2116,
87                 .fw = {
88                         .dir = QCA9887_HW_1_0_FW_DIR,
89                         .board = QCA9887_HW_1_0_BOARD_DATA_FILE,
90                         .board_size = QCA9887_BOARD_DATA_SZ,
91                         .board_ext_size = QCA9887_BOARD_EXT_DATA_SZ,
92                 },
93                 .hw_ops = &qca988x_ops,
94                 .decap_align_bytes = 4,
95                 .spectral_bin_discard = 0,
96         },
97         {
98                 .id = QCA6174_HW_2_1_VERSION,
99                 .dev_id = QCA6164_2_1_DEVICE_ID,
100                 .name = "qca6164 hw2.1",
101                 .patch_load_addr = QCA6174_HW_2_1_PATCH_LOAD_ADDR,
102                 .uart_pin = 6,
103                 .otp_exe_param = 0,
104                 .channel_counters_freq_hz = 88000,
105                 .max_probe_resp_desc_thres = 0,
106                 .cal_data_len = 8124,
107                 .fw = {
108                         .dir = QCA6174_HW_2_1_FW_DIR,
109                         .board = QCA6174_HW_2_1_BOARD_DATA_FILE,
110                         .board_size = QCA6174_BOARD_DATA_SZ,
111                         .board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
112                 },
113                 .hw_ops = &qca988x_ops,
114                 .decap_align_bytes = 4,
115                 .spectral_bin_discard = 0,
116         },
117         {
118                 .id = QCA6174_HW_2_1_VERSION,
119                 .dev_id = QCA6174_2_1_DEVICE_ID,
120                 .name = "qca6174 hw2.1",
121                 .patch_load_addr = QCA6174_HW_2_1_PATCH_LOAD_ADDR,
122                 .uart_pin = 6,
123                 .otp_exe_param = 0,
124                 .channel_counters_freq_hz = 88000,
125                 .max_probe_resp_desc_thres = 0,
126                 .cal_data_len = 8124,
127                 .fw = {
128                         .dir = QCA6174_HW_2_1_FW_DIR,
129                         .board = QCA6174_HW_2_1_BOARD_DATA_FILE,
130                         .board_size = QCA6174_BOARD_DATA_SZ,
131                         .board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
132                 },
133                 .hw_ops = &qca988x_ops,
134                 .decap_align_bytes = 4,
135                 .spectral_bin_discard = 0,
136         },
137         {
138                 .id = QCA6174_HW_3_0_VERSION,
139                 .dev_id = QCA6174_2_1_DEVICE_ID,
140                 .name = "qca6174 hw3.0",
141                 .patch_load_addr = QCA6174_HW_3_0_PATCH_LOAD_ADDR,
142                 .uart_pin = 6,
143                 .otp_exe_param = 0,
144                 .channel_counters_freq_hz = 88000,
145                 .max_probe_resp_desc_thres = 0,
146                 .cal_data_len = 8124,
147                 .fw = {
148                         .dir = QCA6174_HW_3_0_FW_DIR,
149                         .board = QCA6174_HW_3_0_BOARD_DATA_FILE,
150                         .board_size = QCA6174_BOARD_DATA_SZ,
151                         .board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
152                 },
153                 .hw_ops = &qca988x_ops,
154                 .decap_align_bytes = 4,
155                 .spectral_bin_discard = 0,
156         },
157         {
158                 .id = QCA6174_HW_3_2_VERSION,
159                 .dev_id = QCA6174_2_1_DEVICE_ID,
160                 .name = "qca6174 hw3.2",
161                 .patch_load_addr = QCA6174_HW_3_0_PATCH_LOAD_ADDR,
162                 .uart_pin = 6,
163                 .otp_exe_param = 0,
164                 .channel_counters_freq_hz = 88000,
165                 .max_probe_resp_desc_thres = 0,
166                 .cal_data_len = 8124,
167                 .fw = {
168                         /* uses same binaries as hw3.0 */
169                         .dir = QCA6174_HW_3_0_FW_DIR,
170                         .board = QCA6174_HW_3_0_BOARD_DATA_FILE,
171                         .board_size = QCA6174_BOARD_DATA_SZ,
172                         .board_ext_size = QCA6174_BOARD_EXT_DATA_SZ,
173                 },
174                 .hw_ops = &qca6174_ops,
175                 .hw_clk = qca6174_clk,
176                 .target_cpu_freq = 176000000,
177                 .decap_align_bytes = 4,
178                 .spectral_bin_discard = 0,
179         },
180         {
181                 .id = QCA99X0_HW_2_0_DEV_VERSION,
182                 .dev_id = QCA99X0_2_0_DEVICE_ID,
183                 .name = "qca99x0 hw2.0",
184                 .patch_load_addr = QCA99X0_HW_2_0_PATCH_LOAD_ADDR,
185                 .uart_pin = 7,
186                 .otp_exe_param = 0x00000700,
187                 .continuous_frag_desc = true,
188                 .cck_rate_map_rev2 = true,
189                 .channel_counters_freq_hz = 150000,
190                 .max_probe_resp_desc_thres = 24,
191                 .tx_chain_mask = 0xf,
192                 .rx_chain_mask = 0xf,
193                 .max_spatial_stream = 4,
194                 .cal_data_len = 12064,
195                 .fw = {
196                         .dir = QCA99X0_HW_2_0_FW_DIR,
197                         .board = QCA99X0_HW_2_0_BOARD_DATA_FILE,
198                         .board_size = QCA99X0_BOARD_DATA_SZ,
199                         .board_ext_size = QCA99X0_BOARD_EXT_DATA_SZ,
200                 },
201                 .sw_decrypt_mcast_mgmt = true,
202                 .hw_ops = &qca99x0_ops,
203                 .decap_align_bytes = 1,
204                 .spectral_bin_discard = 4,
205         },
206         {
207                 .id = QCA9984_HW_1_0_DEV_VERSION,
208                 .dev_id = QCA9984_1_0_DEVICE_ID,
209                 .name = "qca9984/qca9994 hw1.0",
210                 .patch_load_addr = QCA9984_HW_1_0_PATCH_LOAD_ADDR,
211                 .uart_pin = 7,
212                 .cc_wraparound_type = ATH10K_HW_CC_WRAP_SHIFTED_EACH,
213                 .otp_exe_param = 0x00000700,
214                 .continuous_frag_desc = true,
215                 .cck_rate_map_rev2 = true,
216                 .channel_counters_freq_hz = 150000,
217                 .max_probe_resp_desc_thres = 24,
218                 .tx_chain_mask = 0xf,
219                 .rx_chain_mask = 0xf,
220                 .max_spatial_stream = 4,
221                 .cal_data_len = 12064,
222                 .fw = {
223                         .dir = QCA9984_HW_1_0_FW_DIR,
224                         .board = QCA9984_HW_1_0_BOARD_DATA_FILE,
225                         .board_size = QCA99X0_BOARD_DATA_SZ,
226                         .board_ext_size = QCA99X0_BOARD_EXT_DATA_SZ,
227                 },
228                 .sw_decrypt_mcast_mgmt = true,
229                 .hw_ops = &qca99x0_ops,
230                 .decap_align_bytes = 1,
231                 .spectral_bin_discard = 12,
232         },
233         {
234                 .id = QCA9888_HW_2_0_DEV_VERSION,
235                 .dev_id = QCA9888_2_0_DEVICE_ID,
236                 .name = "qca9888 hw2.0",
237                 .patch_load_addr = QCA9888_HW_2_0_PATCH_LOAD_ADDR,
238                 .uart_pin = 7,
239                 .cc_wraparound_type = ATH10K_HW_CC_WRAP_SHIFTED_EACH,
240                 .otp_exe_param = 0x00000700,
241                 .continuous_frag_desc = true,
242                 .channel_counters_freq_hz = 150000,
243                 .max_probe_resp_desc_thres = 24,
244                 .tx_chain_mask = 3,
245                 .rx_chain_mask = 3,
246                 .max_spatial_stream = 2,
247                 .cal_data_len = 12064,
248                 .fw = {
249                         .dir = QCA9888_HW_2_0_FW_DIR,
250                         .board = QCA9888_HW_2_0_BOARD_DATA_FILE,
251                         .board_size = QCA99X0_BOARD_DATA_SZ,
252                         .board_ext_size = QCA99X0_BOARD_EXT_DATA_SZ,
253                 },
254                 .sw_decrypt_mcast_mgmt = true,
255                 .hw_ops = &qca99x0_ops,
256                 .decap_align_bytes = 1,
257                 .spectral_bin_discard = 12,
258         },
259         {
260                 .id = QCA9377_HW_1_0_DEV_VERSION,
261                 .dev_id = QCA9377_1_0_DEVICE_ID,
262                 .name = "qca9377 hw1.0",
263                 .patch_load_addr = QCA9377_HW_1_0_PATCH_LOAD_ADDR,
264                 .uart_pin = 6,
265                 .otp_exe_param = 0,
266                 .channel_counters_freq_hz = 88000,
267                 .max_probe_resp_desc_thres = 0,
268                 .cal_data_len = 8124,
269                 .fw = {
270                         .dir = QCA9377_HW_1_0_FW_DIR,
271                         .board = QCA9377_HW_1_0_BOARD_DATA_FILE,
272                         .board_size = QCA9377_BOARD_DATA_SZ,
273                         .board_ext_size = QCA9377_BOARD_EXT_DATA_SZ,
274                 },
275                 .hw_ops = &qca988x_ops,
276                 .decap_align_bytes = 4,
277                 .spectral_bin_discard = 0,
278         },
279         {
280                 .id = QCA9377_HW_1_1_DEV_VERSION,
281                 .dev_id = QCA9377_1_0_DEVICE_ID,
282                 .name = "qca9377 hw1.1",
283                 .patch_load_addr = QCA9377_HW_1_0_PATCH_LOAD_ADDR,
284                 .uart_pin = 6,
285                 .otp_exe_param = 0,
286                 .channel_counters_freq_hz = 88000,
287                 .max_probe_resp_desc_thres = 0,
288                 .cal_data_len = 8124,
289                 .fw = {
290                         .dir = QCA9377_HW_1_0_FW_DIR,
291                         .board = QCA9377_HW_1_0_BOARD_DATA_FILE,
292                         .board_size = QCA9377_BOARD_DATA_SZ,
293                         .board_ext_size = QCA9377_BOARD_EXT_DATA_SZ,
294                 },
295                 .hw_ops = &qca6174_ops,
296                 .hw_clk = qca6174_clk,
297                 .target_cpu_freq = 176000000,
298                 .decap_align_bytes = 4,
299                 .spectral_bin_discard = 0,
300         },
301         {
302                 .id = QCA4019_HW_1_0_DEV_VERSION,
303                 .dev_id = 0,
304                 .name = "qca4019 hw1.0",
305                 .patch_load_addr = QCA4019_HW_1_0_PATCH_LOAD_ADDR,
306                 .uart_pin = 7,
307                 .cc_wraparound_type = ATH10K_HW_CC_WRAP_SHIFTED_EACH,
308                 .otp_exe_param = 0x0010000,
309                 .continuous_frag_desc = true,
310                 .cck_rate_map_rev2 = true,
311                 .channel_counters_freq_hz = 125000,
312                 .max_probe_resp_desc_thres = 24,
313                 .tx_chain_mask = 0x3,
314                 .rx_chain_mask = 0x3,
315                 .max_spatial_stream = 2,
316                 .cal_data_len = 12064,
317                 .fw = {
318                         .dir = QCA4019_HW_1_0_FW_DIR,
319                         .board = QCA4019_HW_1_0_BOARD_DATA_FILE,
320                         .board_size = QCA4019_BOARD_DATA_SZ,
321                         .board_ext_size = QCA4019_BOARD_EXT_DATA_SZ,
322                 },
323                 .sw_decrypt_mcast_mgmt = true,
324                 .hw_ops = &qca99x0_ops,
325                 .decap_align_bytes = 1,
326                 .spectral_bin_discard = 4,
327         },
328 };
329
330 static const char *const ath10k_core_fw_feature_str[] = {
331         [ATH10K_FW_FEATURE_EXT_WMI_MGMT_RX] = "wmi-mgmt-rx",
332         [ATH10K_FW_FEATURE_WMI_10X] = "wmi-10.x",
333         [ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX] = "has-wmi-mgmt-tx",
334         [ATH10K_FW_FEATURE_NO_P2P] = "no-p2p",
335         [ATH10K_FW_FEATURE_WMI_10_2] = "wmi-10.2",
336         [ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT] = "multi-vif-ps",
337         [ATH10K_FW_FEATURE_WOWLAN_SUPPORT] = "wowlan",
338         [ATH10K_FW_FEATURE_IGNORE_OTP_RESULT] = "ignore-otp",
339         [ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING] = "no-4addr-pad",
340         [ATH10K_FW_FEATURE_SUPPORTS_SKIP_CLOCK_INIT] = "skip-clock-init",
341         [ATH10K_FW_FEATURE_RAW_MODE_SUPPORT] = "raw-mode",
342         [ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA] = "adaptive-cca",
343         [ATH10K_FW_FEATURE_MFP_SUPPORT] = "mfp",
344         [ATH10K_FW_FEATURE_PEER_FLOW_CONTROL] = "peer-flow-ctrl",
345         [ATH10K_FW_FEATURE_BTCOEX_PARAM] = "btcoex-param",
346         [ATH10K_FW_FEATURE_SKIP_NULL_FUNC_WAR] = "skip-null-func-war",
347         [ATH10K_FW_FEATURE_ALLOWS_MESH_BCAST] = "allows-mesh-bcast",
348 };
349
350 static unsigned int ath10k_core_get_fw_feature_str(char *buf,
351                                                    size_t buf_len,
352                                                    enum ath10k_fw_features feat)
353 {
354         /* make sure that ath10k_core_fw_feature_str[] gets updated */
355         BUILD_BUG_ON(ARRAY_SIZE(ath10k_core_fw_feature_str) !=
356                      ATH10K_FW_FEATURE_COUNT);
357
358         if (feat >= ARRAY_SIZE(ath10k_core_fw_feature_str) ||
359             WARN_ON(!ath10k_core_fw_feature_str[feat])) {
360                 return scnprintf(buf, buf_len, "bit%d", feat);
361         }
362
363         return scnprintf(buf, buf_len, "%s", ath10k_core_fw_feature_str[feat]);
364 }
365
366 void ath10k_core_get_fw_features_str(struct ath10k *ar,
367                                      char *buf,
368                                      size_t buf_len)
369 {
370         size_t len = 0;
371         int i;
372
373         for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
374                 if (test_bit(i, ar->normal_mode_fw.fw_file.fw_features)) {
375                         if (len > 0)
376                                 len += scnprintf(buf + len, buf_len - len, ",");
377
378                         len += ath10k_core_get_fw_feature_str(buf + len,
379                                                               buf_len - len,
380                                                               i);
381                 }
382         }
383 }
384
385 static void ath10k_send_suspend_complete(struct ath10k *ar)
386 {
387         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot suspend complete\n");
388
389         complete(&ar->target_suspend);
390 }
391
392 static void ath10k_init_sdio(struct ath10k *ar)
393 {
394         u32 param = 0;
395
396         ath10k_bmi_write32(ar, hi_mbox_io_block_sz, 256);
397         ath10k_bmi_write32(ar, hi_mbox_isr_yield_limit, 99);
398         ath10k_bmi_read32(ar, hi_acs_flags, &param);
399
400         param |= (HI_ACS_FLAGS_SDIO_SWAP_MAILBOX_SET |
401                   HI_ACS_FLAGS_SDIO_REDUCE_TX_COMPL_SET |
402                   HI_ACS_FLAGS_ALT_DATA_CREDIT_SIZE);
403
404         ath10k_bmi_write32(ar, hi_acs_flags, param);
405 }
406
407 static int ath10k_init_configure_target(struct ath10k *ar)
408 {
409         u32 param_host;
410         int ret;
411
412         /* tell target which HTC version it is used*/
413         ret = ath10k_bmi_write32(ar, hi_app_host_interest,
414                                  HTC_PROTOCOL_VERSION);
415         if (ret) {
416                 ath10k_err(ar, "settings HTC version failed\n");
417                 return ret;
418         }
419
420         /* set the firmware mode to STA/IBSS/AP */
421         ret = ath10k_bmi_read32(ar, hi_option_flag, &param_host);
422         if (ret) {
423                 ath10k_err(ar, "setting firmware mode (1/2) failed\n");
424                 return ret;
425         }
426
427         /* TODO following parameters need to be re-visited. */
428         /* num_device */
429         param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
430         /* Firmware mode */
431         /* FIXME: Why FW_MODE_AP ??.*/
432         param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
433         /* mac_addr_method */
434         param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
435         /* firmware_bridge */
436         param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
437         /* fwsubmode */
438         param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
439
440         ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
441         if (ret) {
442                 ath10k_err(ar, "setting firmware mode (2/2) failed\n");
443                 return ret;
444         }
445
446         /* We do all byte-swapping on the host */
447         ret = ath10k_bmi_write32(ar, hi_be, 0);
448         if (ret) {
449                 ath10k_err(ar, "setting host CPU BE mode failed\n");
450                 return ret;
451         }
452
453         /* FW descriptor/Data swap flags */
454         ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
455
456         if (ret) {
457                 ath10k_err(ar, "setting FW data/desc swap flags failed\n");
458                 return ret;
459         }
460
461         /* Some devices have a special sanity check that verifies the PCI
462          * Device ID is written to this host interest var. It is known to be
463          * required to boot QCA6164.
464          */
465         ret = ath10k_bmi_write32(ar, hi_hci_uart_pwr_mgmt_params_ext,
466                                  ar->dev_id);
467         if (ret) {
468                 ath10k_err(ar, "failed to set pwr_mgmt_params: %d\n", ret);
469                 return ret;
470         }
471
472         return 0;
473 }
474
475 static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
476                                                    const char *dir,
477                                                    const char *file)
478 {
479         char filename[100];
480         const struct firmware *fw;
481         int ret;
482
483         if (file == NULL)
484                 return ERR_PTR(-ENOENT);
485
486         if (dir == NULL)
487                 dir = ".";
488
489         snprintf(filename, sizeof(filename), "%s/%s", dir, file);
490         ret = request_firmware_direct(&fw, filename, ar->dev);
491         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot fw request '%s': %d\n",
492                    filename, ret);
493
494         if (ret)
495                 return ERR_PTR(ret);
496
497         return fw;
498 }
499
500 static int ath10k_push_board_ext_data(struct ath10k *ar, const void *data,
501                                       size_t data_len)
502 {
503         u32 board_data_size = ar->hw_params.fw.board_size;
504         u32 board_ext_data_size = ar->hw_params.fw.board_ext_size;
505         u32 board_ext_data_addr;
506         int ret;
507
508         ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
509         if (ret) {
510                 ath10k_err(ar, "could not read board ext data addr (%d)\n",
511                            ret);
512                 return ret;
513         }
514
515         ath10k_dbg(ar, ATH10K_DBG_BOOT,
516                    "boot push board extended data addr 0x%x\n",
517                    board_ext_data_addr);
518
519         if (board_ext_data_addr == 0)
520                 return 0;
521
522         if (data_len != (board_data_size + board_ext_data_size)) {
523                 ath10k_err(ar, "invalid board (ext) data sizes %zu != %d+%d\n",
524                            data_len, board_data_size, board_ext_data_size);
525                 return -EINVAL;
526         }
527
528         ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
529                                       data + board_data_size,
530                                       board_ext_data_size);
531         if (ret) {
532                 ath10k_err(ar, "could not write board ext data (%d)\n", ret);
533                 return ret;
534         }
535
536         ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
537                                  (board_ext_data_size << 16) | 1);
538         if (ret) {
539                 ath10k_err(ar, "could not write board ext data bit (%d)\n",
540                            ret);
541                 return ret;
542         }
543
544         return 0;
545 }
546
547 static int ath10k_download_board_data(struct ath10k *ar, const void *data,
548                                       size_t data_len)
549 {
550         u32 board_data_size = ar->hw_params.fw.board_size;
551         u32 address;
552         int ret;
553
554         ret = ath10k_push_board_ext_data(ar, data, data_len);
555         if (ret) {
556                 ath10k_err(ar, "could not push board ext data (%d)\n", ret);
557                 goto exit;
558         }
559
560         ret = ath10k_bmi_read32(ar, hi_board_data, &address);
561         if (ret) {
562                 ath10k_err(ar, "could not read board data addr (%d)\n", ret);
563                 goto exit;
564         }
565
566         ret = ath10k_bmi_write_memory(ar, address, data,
567                                       min_t(u32, board_data_size,
568                                             data_len));
569         if (ret) {
570                 ath10k_err(ar, "could not write board data (%d)\n", ret);
571                 goto exit;
572         }
573
574         ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
575         if (ret) {
576                 ath10k_err(ar, "could not write board data bit (%d)\n", ret);
577                 goto exit;
578         }
579
580 exit:
581         return ret;
582 }
583
584 static int ath10k_download_cal_file(struct ath10k *ar,
585                                     const struct firmware *file)
586 {
587         int ret;
588
589         if (!file)
590                 return -ENOENT;
591
592         if (IS_ERR(file))
593                 return PTR_ERR(file);
594
595         ret = ath10k_download_board_data(ar, file->data, file->size);
596         if (ret) {
597                 ath10k_err(ar, "failed to download cal_file data: %d\n", ret);
598                 return ret;
599         }
600
601         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cal file downloaded\n");
602
603         return 0;
604 }
605
606 static int ath10k_download_cal_dt(struct ath10k *ar, const char *dt_name)
607 {
608         struct device_node *node;
609         int data_len;
610         void *data;
611         int ret;
612
613         node = ar->dev->of_node;
614         if (!node)
615                 /* Device Tree is optional, don't print any warnings if
616                  * there's no node for ath10k.
617                  */
618                 return -ENOENT;
619
620         if (!of_get_property(node, dt_name, &data_len)) {
621                 /* The calibration data node is optional */
622                 return -ENOENT;
623         }
624
625         if (data_len != ar->hw_params.cal_data_len) {
626                 ath10k_warn(ar, "invalid calibration data length in DT: %d\n",
627                             data_len);
628                 ret = -EMSGSIZE;
629                 goto out;
630         }
631
632         data = kmalloc(data_len, GFP_KERNEL);
633         if (!data) {
634                 ret = -ENOMEM;
635                 goto out;
636         }
637
638         ret = of_property_read_u8_array(node, dt_name, data, data_len);
639         if (ret) {
640                 ath10k_warn(ar, "failed to read calibration data from DT: %d\n",
641                             ret);
642                 goto out_free;
643         }
644
645         ret = ath10k_download_board_data(ar, data, data_len);
646         if (ret) {
647                 ath10k_warn(ar, "failed to download calibration data from Device Tree: %d\n",
648                             ret);
649                 goto out_free;
650         }
651
652         ret = 0;
653
654 out_free:
655         kfree(data);
656
657 out:
658         return ret;
659 }
660
661 static int ath10k_download_cal_eeprom(struct ath10k *ar)
662 {
663         size_t data_len;
664         void *data = NULL;
665         int ret;
666
667         ret = ath10k_hif_fetch_cal_eeprom(ar, &data, &data_len);
668         if (ret) {
669                 if (ret != -EOPNOTSUPP)
670                         ath10k_warn(ar, "failed to read calibration data from EEPROM: %d\n",
671                                     ret);
672                 goto out_free;
673         }
674
675         ret = ath10k_download_board_data(ar, data, data_len);
676         if (ret) {
677                 ath10k_warn(ar, "failed to download calibration data from EEPROM: %d\n",
678                             ret);
679                 goto out_free;
680         }
681
682         ret = 0;
683
684 out_free:
685         kfree(data);
686
687         return ret;
688 }
689
690 static int ath10k_core_get_board_id_from_otp(struct ath10k *ar)
691 {
692         u32 result, address;
693         u8 board_id, chip_id;
694         int ret;
695
696         address = ar->hw_params.patch_load_addr;
697
698         if (!ar->normal_mode_fw.fw_file.otp_data ||
699             !ar->normal_mode_fw.fw_file.otp_len) {
700                 ath10k_warn(ar,
701                             "failed to retrieve board id because of invalid otp\n");
702                 return -ENODATA;
703         }
704
705         ath10k_dbg(ar, ATH10K_DBG_BOOT,
706                    "boot upload otp to 0x%x len %zd for board id\n",
707                    address, ar->normal_mode_fw.fw_file.otp_len);
708
709         ret = ath10k_bmi_fast_download(ar, address,
710                                        ar->normal_mode_fw.fw_file.otp_data,
711                                        ar->normal_mode_fw.fw_file.otp_len);
712         if (ret) {
713                 ath10k_err(ar, "could not write otp for board id check: %d\n",
714                            ret);
715                 return ret;
716         }
717
718         ret = ath10k_bmi_execute(ar, address, BMI_PARAM_GET_EEPROM_BOARD_ID,
719                                  &result);
720         if (ret) {
721                 ath10k_err(ar, "could not execute otp for board id check: %d\n",
722                            ret);
723                 return ret;
724         }
725
726         board_id = MS(result, ATH10K_BMI_BOARD_ID_FROM_OTP);
727         chip_id = MS(result, ATH10K_BMI_CHIP_ID_FROM_OTP);
728
729         ath10k_dbg(ar, ATH10K_DBG_BOOT,
730                    "boot get otp board id result 0x%08x board_id %d chip_id %d\n",
731                    result, board_id, chip_id);
732
733         if ((result & ATH10K_BMI_BOARD_ID_STATUS_MASK) != 0 ||
734             (board_id == 0)) {
735                 ath10k_dbg(ar, ATH10K_DBG_BOOT,
736                            "board id does not exist in otp, ignore it\n");
737                 return -EOPNOTSUPP;
738         }
739
740         ar->id.bmi_ids_valid = true;
741         ar->id.bmi_board_id = board_id;
742         ar->id.bmi_chip_id = chip_id;
743
744         return 0;
745 }
746
747 static void ath10k_core_check_bdfext(const struct dmi_header *hdr, void *data)
748 {
749         struct ath10k *ar = data;
750         const char *bdf_ext;
751         const char *magic = ATH10K_SMBIOS_BDF_EXT_MAGIC;
752         u8 bdf_enabled;
753         int i;
754
755         if (hdr->type != ATH10K_SMBIOS_BDF_EXT_TYPE)
756                 return;
757
758         if (hdr->length != ATH10K_SMBIOS_BDF_EXT_LENGTH) {
759                 ath10k_dbg(ar, ATH10K_DBG_BOOT,
760                            "wrong smbios bdf ext type length (%d).\n",
761                            hdr->length);
762                 return;
763         }
764
765         bdf_enabled = *((u8 *)hdr + ATH10K_SMBIOS_BDF_EXT_OFFSET);
766         if (!bdf_enabled) {
767                 ath10k_dbg(ar, ATH10K_DBG_BOOT, "bdf variant name not found.\n");
768                 return;
769         }
770
771         /* Only one string exists (per spec) */
772         bdf_ext = (char *)hdr + hdr->length;
773
774         if (memcmp(bdf_ext, magic, strlen(magic)) != 0) {
775                 ath10k_dbg(ar, ATH10K_DBG_BOOT,
776                            "bdf variant magic does not match.\n");
777                 return;
778         }
779
780         for (i = 0; i < strlen(bdf_ext); i++) {
781                 if (!isascii(bdf_ext[i]) || !isprint(bdf_ext[i])) {
782                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
783                                    "bdf variant name contains non ascii chars.\n");
784                         return;
785                 }
786         }
787
788         /* Copy extension name without magic suffix */
789         if (strscpy(ar->id.bdf_ext, bdf_ext + strlen(magic),
790                     sizeof(ar->id.bdf_ext)) < 0) {
791                 ath10k_dbg(ar, ATH10K_DBG_BOOT,
792                            "bdf variant string is longer than the buffer can accommodate (variant: %s)\n",
793                             bdf_ext);
794                 return;
795         }
796
797         ath10k_dbg(ar, ATH10K_DBG_BOOT,
798                    "found and validated bdf variant smbios_type 0x%x bdf %s\n",
799                    ATH10K_SMBIOS_BDF_EXT_TYPE, bdf_ext);
800 }
801
802 static int ath10k_core_check_smbios(struct ath10k *ar)
803 {
804         ar->id.bdf_ext[0] = '\0';
805         dmi_walk(ath10k_core_check_bdfext, ar);
806
807         if (ar->id.bdf_ext[0] == '\0')
808                 return -ENODATA;
809
810         return 0;
811 }
812
813 static int ath10k_download_and_run_otp(struct ath10k *ar)
814 {
815         u32 result, address = ar->hw_params.patch_load_addr;
816         u32 bmi_otp_exe_param = ar->hw_params.otp_exe_param;
817         int ret;
818
819         ret = ath10k_download_board_data(ar,
820                                          ar->running_fw->board_data,
821                                          ar->running_fw->board_len);
822         if (ret) {
823                 ath10k_err(ar, "failed to download board data: %d\n", ret);
824                 return ret;
825         }
826
827         /* OTP is optional */
828
829         if (!ar->running_fw->fw_file.otp_data ||
830             !ar->running_fw->fw_file.otp_len) {
831                 ath10k_warn(ar, "Not running otp, calibration will be incorrect (otp-data %pK otp_len %zd)!\n",
832                             ar->running_fw->fw_file.otp_data,
833                             ar->running_fw->fw_file.otp_len);
834                 return 0;
835         }
836
837         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot upload otp to 0x%x len %zd\n",
838                    address, ar->running_fw->fw_file.otp_len);
839
840         ret = ath10k_bmi_fast_download(ar, address,
841                                        ar->running_fw->fw_file.otp_data,
842                                        ar->running_fw->fw_file.otp_len);
843         if (ret) {
844                 ath10k_err(ar, "could not write otp (%d)\n", ret);
845                 return ret;
846         }
847
848         ret = ath10k_bmi_execute(ar, address, bmi_otp_exe_param, &result);
849         if (ret) {
850                 ath10k_err(ar, "could not execute otp (%d)\n", ret);
851                 return ret;
852         }
853
854         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot otp execute result %d\n", result);
855
856         if (!(skip_otp || test_bit(ATH10K_FW_FEATURE_IGNORE_OTP_RESULT,
857                                    ar->running_fw->fw_file.fw_features)) &&
858             result != 0) {
859                 ath10k_err(ar, "otp calibration failed: %d", result);
860                 return -EINVAL;
861         }
862
863         return 0;
864 }
865
866 static int ath10k_download_fw(struct ath10k *ar)
867 {
868         u32 address, data_len;
869         const void *data;
870         int ret;
871
872         address = ar->hw_params.patch_load_addr;
873
874         data = ar->running_fw->fw_file.firmware_data;
875         data_len = ar->running_fw->fw_file.firmware_len;
876
877         ret = ath10k_swap_code_seg_configure(ar, &ar->running_fw->fw_file);
878         if (ret) {
879                 ath10k_err(ar, "failed to configure fw code swap: %d\n",
880                            ret);
881                 return ret;
882         }
883
884         ath10k_dbg(ar, ATH10K_DBG_BOOT,
885                    "boot uploading firmware image %pK len %d\n",
886                    data, data_len);
887
888         ret = ath10k_bmi_fast_download(ar, address, data, data_len);
889         if (ret) {
890                 ath10k_err(ar, "failed to download firmware: %d\n",
891                            ret);
892                 return ret;
893         }
894
895         return ret;
896 }
897
898 static void ath10k_core_free_board_files(struct ath10k *ar)
899 {
900         if (!IS_ERR(ar->normal_mode_fw.board))
901                 release_firmware(ar->normal_mode_fw.board);
902
903         ar->normal_mode_fw.board = NULL;
904         ar->normal_mode_fw.board_data = NULL;
905         ar->normal_mode_fw.board_len = 0;
906 }
907
908 static void ath10k_core_free_firmware_files(struct ath10k *ar)
909 {
910         if (!IS_ERR(ar->normal_mode_fw.fw_file.firmware))
911                 release_firmware(ar->normal_mode_fw.fw_file.firmware);
912
913         if (!IS_ERR(ar->cal_file))
914                 release_firmware(ar->cal_file);
915
916         if (!IS_ERR(ar->pre_cal_file))
917                 release_firmware(ar->pre_cal_file);
918
919         ath10k_swap_code_seg_release(ar, &ar->normal_mode_fw.fw_file);
920
921         ar->normal_mode_fw.fw_file.otp_data = NULL;
922         ar->normal_mode_fw.fw_file.otp_len = 0;
923
924         ar->normal_mode_fw.fw_file.firmware = NULL;
925         ar->normal_mode_fw.fw_file.firmware_data = NULL;
926         ar->normal_mode_fw.fw_file.firmware_len = 0;
927
928         ar->cal_file = NULL;
929         ar->pre_cal_file = NULL;
930 }
931
932 static int ath10k_fetch_cal_file(struct ath10k *ar)
933 {
934         char filename[100];
935
936         /* pre-cal-<bus>-<id>.bin */
937         scnprintf(filename, sizeof(filename), "pre-cal-%s-%s.bin",
938                   ath10k_bus_str(ar->hif.bus), dev_name(ar->dev));
939
940         ar->pre_cal_file = ath10k_fetch_fw_file(ar, ATH10K_FW_DIR, filename);
941         if (!IS_ERR(ar->pre_cal_file))
942                 goto success;
943
944         /* cal-<bus>-<id>.bin */
945         scnprintf(filename, sizeof(filename), "cal-%s-%s.bin",
946                   ath10k_bus_str(ar->hif.bus), dev_name(ar->dev));
947
948         ar->cal_file = ath10k_fetch_fw_file(ar, ATH10K_FW_DIR, filename);
949         if (IS_ERR(ar->cal_file))
950                 /* calibration file is optional, don't print any warnings */
951                 return PTR_ERR(ar->cal_file);
952 success:
953         ath10k_dbg(ar, ATH10K_DBG_BOOT, "found calibration file %s/%s\n",
954                    ATH10K_FW_DIR, filename);
955
956         return 0;
957 }
958
959 static int ath10k_core_fetch_board_data_api_1(struct ath10k *ar)
960 {
961         if (!ar->hw_params.fw.board) {
962                 ath10k_err(ar, "failed to find board file fw entry\n");
963                 return -EINVAL;
964         }
965
966         ar->normal_mode_fw.board = ath10k_fetch_fw_file(ar,
967                                                         ar->hw_params.fw.dir,
968                                                         ar->hw_params.fw.board);
969         if (IS_ERR(ar->normal_mode_fw.board))
970                 return PTR_ERR(ar->normal_mode_fw.board);
971
972         ar->normal_mode_fw.board_data = ar->normal_mode_fw.board->data;
973         ar->normal_mode_fw.board_len = ar->normal_mode_fw.board->size;
974
975         return 0;
976 }
977
978 static int ath10k_core_parse_bd_ie_board(struct ath10k *ar,
979                                          const void *buf, size_t buf_len,
980                                          const char *boardname)
981 {
982         const struct ath10k_fw_ie *hdr;
983         bool name_match_found;
984         int ret, board_ie_id;
985         size_t board_ie_len;
986         const void *board_ie_data;
987
988         name_match_found = false;
989
990         /* go through ATH10K_BD_IE_BOARD_ elements */
991         while (buf_len > sizeof(struct ath10k_fw_ie)) {
992                 hdr = buf;
993                 board_ie_id = le32_to_cpu(hdr->id);
994                 board_ie_len = le32_to_cpu(hdr->len);
995                 board_ie_data = hdr->data;
996
997                 buf_len -= sizeof(*hdr);
998                 buf += sizeof(*hdr);
999
1000                 if (buf_len < ALIGN(board_ie_len, 4)) {
1001                         ath10k_err(ar, "invalid ATH10K_BD_IE_BOARD length: %zu < %zu\n",
1002                                    buf_len, ALIGN(board_ie_len, 4));
1003                         ret = -EINVAL;
1004                         goto out;
1005                 }
1006
1007                 switch (board_ie_id) {
1008                 case ATH10K_BD_IE_BOARD_NAME:
1009                         ath10k_dbg_dump(ar, ATH10K_DBG_BOOT, "board name", "",
1010                                         board_ie_data, board_ie_len);
1011
1012                         if (board_ie_len != strlen(boardname))
1013                                 break;
1014
1015                         ret = memcmp(board_ie_data, boardname, strlen(boardname));
1016                         if (ret)
1017                                 break;
1018
1019                         name_match_found = true;
1020                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1021                                    "boot found match for name '%s'",
1022                                    boardname);
1023                         break;
1024                 case ATH10K_BD_IE_BOARD_DATA:
1025                         if (!name_match_found)
1026                                 /* no match found */
1027                                 break;
1028
1029                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1030                                    "boot found board data for '%s'",
1031                                    boardname);
1032
1033                         ar->normal_mode_fw.board_data = board_ie_data;
1034                         ar->normal_mode_fw.board_len = board_ie_len;
1035
1036                         ret = 0;
1037                         goto out;
1038                 default:
1039                         ath10k_warn(ar, "unknown ATH10K_BD_IE_BOARD found: %d\n",
1040                                     board_ie_id);
1041                         break;
1042                 }
1043
1044                 /* jump over the padding */
1045                 board_ie_len = ALIGN(board_ie_len, 4);
1046
1047                 buf_len -= board_ie_len;
1048                 buf += board_ie_len;
1049         }
1050
1051         /* no match found */
1052         ret = -ENOENT;
1053
1054 out:
1055         return ret;
1056 }
1057
1058 static int ath10k_core_fetch_board_data_api_n(struct ath10k *ar,
1059                                               const char *boardname,
1060                                               const char *filename)
1061 {
1062         size_t len, magic_len, ie_len;
1063         struct ath10k_fw_ie *hdr;
1064         const u8 *data;
1065         int ret, ie_id;
1066
1067         ar->normal_mode_fw.board = ath10k_fetch_fw_file(ar,
1068                                                         ar->hw_params.fw.dir,
1069                                                         filename);
1070         if (IS_ERR(ar->normal_mode_fw.board))
1071                 return PTR_ERR(ar->normal_mode_fw.board);
1072
1073         data = ar->normal_mode_fw.board->data;
1074         len = ar->normal_mode_fw.board->size;
1075
1076         /* magic has extra null byte padded */
1077         magic_len = strlen(ATH10K_BOARD_MAGIC) + 1;
1078         if (len < magic_len) {
1079                 ath10k_err(ar, "failed to find magic value in %s/%s, file too short: %zu\n",
1080                            ar->hw_params.fw.dir, filename, len);
1081                 ret = -EINVAL;
1082                 goto err;
1083         }
1084
1085         if (memcmp(data, ATH10K_BOARD_MAGIC, magic_len)) {
1086                 ath10k_err(ar, "found invalid board magic\n");
1087                 ret = -EINVAL;
1088                 goto err;
1089         }
1090
1091         /* magic is padded to 4 bytes */
1092         magic_len = ALIGN(magic_len, 4);
1093         if (len < magic_len) {
1094                 ath10k_err(ar, "failed: %s/%s too small to contain board data, len: %zu\n",
1095                            ar->hw_params.fw.dir, filename, len);
1096                 ret = -EINVAL;
1097                 goto err;
1098         }
1099
1100         data += magic_len;
1101         len -= magic_len;
1102
1103         while (len > sizeof(struct ath10k_fw_ie)) {
1104                 hdr = (struct ath10k_fw_ie *)data;
1105                 ie_id = le32_to_cpu(hdr->id);
1106                 ie_len = le32_to_cpu(hdr->len);
1107
1108                 len -= sizeof(*hdr);
1109                 data = hdr->data;
1110
1111                 if (len < ALIGN(ie_len, 4)) {
1112                         ath10k_err(ar, "invalid length for board ie_id %d ie_len %zu len %zu\n",
1113                                    ie_id, ie_len, len);
1114                         ret = -EINVAL;
1115                         goto err;
1116                 }
1117
1118                 switch (ie_id) {
1119                 case ATH10K_BD_IE_BOARD:
1120                         ret = ath10k_core_parse_bd_ie_board(ar, data, ie_len,
1121                                                             boardname);
1122                         if (ret == -ENOENT && ar->id.bdf_ext[0] != '\0') {
1123                                 /* try default bdf if variant was not found */
1124                                 char *s, *v = ",variant=";
1125                                 char boardname2[100];
1126
1127                                 strlcpy(boardname2, boardname,
1128                                         sizeof(boardname2));
1129
1130                                 s = strstr(boardname2, v);
1131                                 if (s)
1132                                         *s = '\0';  /* strip ",variant=%s" */
1133
1134                                 ret = ath10k_core_parse_bd_ie_board(ar, data,
1135                                                                     ie_len,
1136                                                                     boardname2);
1137                         }
1138
1139                         if (ret == -ENOENT)
1140                                 /* no match found, continue */
1141                                 break;
1142                         else if (ret)
1143                                 /* there was an error, bail out */
1144                                 goto err;
1145
1146                         /* board data found */
1147                         goto out;
1148                 }
1149
1150                 /* jump over the padding */
1151                 ie_len = ALIGN(ie_len, 4);
1152
1153                 len -= ie_len;
1154                 data += ie_len;
1155         }
1156
1157 out:
1158         if (!ar->normal_mode_fw.board_data || !ar->normal_mode_fw.board_len) {
1159                 ath10k_err(ar,
1160                            "failed to fetch board data for %s from %s/%s\n",
1161                            boardname, ar->hw_params.fw.dir, filename);
1162                 ret = -ENODATA;
1163                 goto err;
1164         }
1165
1166         return 0;
1167
1168 err:
1169         ath10k_core_free_board_files(ar);
1170         return ret;
1171 }
1172
1173 static int ath10k_core_create_board_name(struct ath10k *ar, char *name,
1174                                          size_t name_len)
1175 {
1176         /* strlen(',variant=') + strlen(ar->id.bdf_ext) */
1177         char variant[9 + ATH10K_SMBIOS_BDF_EXT_STR_LENGTH] = { 0 };
1178
1179         if (ar->id.bmi_ids_valid) {
1180                 scnprintf(name, name_len,
1181                           "bus=%s,bmi-chip-id=%d,bmi-board-id=%d",
1182                           ath10k_bus_str(ar->hif.bus),
1183                           ar->id.bmi_chip_id,
1184                           ar->id.bmi_board_id);
1185                 goto out;
1186         }
1187
1188         if (ar->id.bdf_ext[0] != '\0')
1189                 scnprintf(variant, sizeof(variant), ",variant=%s",
1190                           ar->id.bdf_ext);
1191
1192         scnprintf(name, name_len,
1193                   "bus=%s,vendor=%04x,device=%04x,subsystem-vendor=%04x,subsystem-device=%04x%s",
1194                   ath10k_bus_str(ar->hif.bus),
1195                   ar->id.vendor, ar->id.device,
1196                   ar->id.subsystem_vendor, ar->id.subsystem_device, variant);
1197 out:
1198         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot using board name '%s'\n", name);
1199
1200         return 0;
1201 }
1202
1203 static int ath10k_core_fetch_board_file(struct ath10k *ar)
1204 {
1205         char boardname[100];
1206         int ret;
1207
1208         ret = ath10k_core_create_board_name(ar, boardname, sizeof(boardname));
1209         if (ret) {
1210                 ath10k_err(ar, "failed to create board name: %d", ret);
1211                 return ret;
1212         }
1213
1214         ar->bd_api = 2;
1215         ret = ath10k_core_fetch_board_data_api_n(ar, boardname,
1216                                                  ATH10K_BOARD_API2_FILE);
1217         if (!ret)
1218                 goto success;
1219
1220         ar->bd_api = 1;
1221         ret = ath10k_core_fetch_board_data_api_1(ar);
1222         if (ret) {
1223                 ath10k_err(ar, "failed to fetch board-2.bin or board.bin from %s\n",
1224                            ar->hw_params.fw.dir);
1225                 return ret;
1226         }
1227
1228 success:
1229         ath10k_dbg(ar, ATH10K_DBG_BOOT, "using board api %d\n", ar->bd_api);
1230         return 0;
1231 }
1232
1233 int ath10k_core_fetch_firmware_api_n(struct ath10k *ar, const char *name,
1234                                      struct ath10k_fw_file *fw_file)
1235 {
1236         size_t magic_len, len, ie_len;
1237         int ie_id, i, index, bit, ret;
1238         struct ath10k_fw_ie *hdr;
1239         const u8 *data;
1240         __le32 *timestamp, *version;
1241
1242         /* first fetch the firmware file (firmware-*.bin) */
1243         fw_file->firmware = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir,
1244                                                  name);
1245         if (IS_ERR(fw_file->firmware))
1246                 return PTR_ERR(fw_file->firmware);
1247
1248         data = fw_file->firmware->data;
1249         len = fw_file->firmware->size;
1250
1251         /* magic also includes the null byte, check that as well */
1252         magic_len = strlen(ATH10K_FIRMWARE_MAGIC) + 1;
1253
1254         if (len < magic_len) {
1255                 ath10k_err(ar, "firmware file '%s/%s' too small to contain magic: %zu\n",
1256                            ar->hw_params.fw.dir, name, len);
1257                 ret = -EINVAL;
1258                 goto err;
1259         }
1260
1261         if (memcmp(data, ATH10K_FIRMWARE_MAGIC, magic_len) != 0) {
1262                 ath10k_err(ar, "invalid firmware magic\n");
1263                 ret = -EINVAL;
1264                 goto err;
1265         }
1266
1267         /* jump over the padding */
1268         magic_len = ALIGN(magic_len, 4);
1269
1270         len -= magic_len;
1271         data += magic_len;
1272
1273         /* loop elements */
1274         while (len > sizeof(struct ath10k_fw_ie)) {
1275                 hdr = (struct ath10k_fw_ie *)data;
1276
1277                 ie_id = le32_to_cpu(hdr->id);
1278                 ie_len = le32_to_cpu(hdr->len);
1279
1280                 len -= sizeof(*hdr);
1281                 data += sizeof(*hdr);
1282
1283                 if (len < ie_len) {
1284                         ath10k_err(ar, "invalid length for FW IE %d (%zu < %zu)\n",
1285                                    ie_id, len, ie_len);
1286                         ret = -EINVAL;
1287                         goto err;
1288                 }
1289
1290                 switch (ie_id) {
1291                 case ATH10K_FW_IE_FW_VERSION:
1292                         if (ie_len > sizeof(fw_file->fw_version) - 1)
1293                                 break;
1294
1295                         memcpy(fw_file->fw_version, data, ie_len);
1296                         fw_file->fw_version[ie_len] = '\0';
1297
1298                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1299                                    "found fw version %s\n",
1300                                     fw_file->fw_version);
1301                         break;
1302                 case ATH10K_FW_IE_TIMESTAMP:
1303                         if (ie_len != sizeof(u32))
1304                                 break;
1305
1306                         timestamp = (__le32 *)data;
1307
1308                         ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw timestamp %d\n",
1309                                    le32_to_cpup(timestamp));
1310                         break;
1311                 case ATH10K_FW_IE_FEATURES:
1312                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1313                                    "found firmware features ie (%zd B)\n",
1314                                    ie_len);
1315
1316                         for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
1317                                 index = i / 8;
1318                                 bit = i % 8;
1319
1320                                 if (index == ie_len)
1321                                         break;
1322
1323                                 if (data[index] & (1 << bit)) {
1324                                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1325                                                    "Enabling feature bit: %i\n",
1326                                                    i);
1327                                         __set_bit(i, fw_file->fw_features);
1328                                 }
1329                         }
1330
1331                         ath10k_dbg_dump(ar, ATH10K_DBG_BOOT, "features", "",
1332                                         fw_file->fw_features,
1333                                         sizeof(fw_file->fw_features));
1334                         break;
1335                 case ATH10K_FW_IE_FW_IMAGE:
1336                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1337                                    "found fw image ie (%zd B)\n",
1338                                    ie_len);
1339
1340                         fw_file->firmware_data = data;
1341                         fw_file->firmware_len = ie_len;
1342
1343                         break;
1344                 case ATH10K_FW_IE_OTP_IMAGE:
1345                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1346                                    "found otp image ie (%zd B)\n",
1347                                    ie_len);
1348
1349                         fw_file->otp_data = data;
1350                         fw_file->otp_len = ie_len;
1351
1352                         break;
1353                 case ATH10K_FW_IE_WMI_OP_VERSION:
1354                         if (ie_len != sizeof(u32))
1355                                 break;
1356
1357                         version = (__le32 *)data;
1358
1359                         fw_file->wmi_op_version = le32_to_cpup(version);
1360
1361                         ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw ie wmi op version %d\n",
1362                                    fw_file->wmi_op_version);
1363                         break;
1364                 case ATH10K_FW_IE_HTT_OP_VERSION:
1365                         if (ie_len != sizeof(u32))
1366                                 break;
1367
1368                         version = (__le32 *)data;
1369
1370                         fw_file->htt_op_version = le32_to_cpup(version);
1371
1372                         ath10k_dbg(ar, ATH10K_DBG_BOOT, "found fw ie htt op version %d\n",
1373                                    fw_file->htt_op_version);
1374                         break;
1375                 case ATH10K_FW_IE_FW_CODE_SWAP_IMAGE:
1376                         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1377                                    "found fw code swap image ie (%zd B)\n",
1378                                    ie_len);
1379                         fw_file->codeswap_data = data;
1380                         fw_file->codeswap_len = ie_len;
1381                         break;
1382                 default:
1383                         ath10k_warn(ar, "Unknown FW IE: %u\n",
1384                                     le32_to_cpu(hdr->id));
1385                         break;
1386                 }
1387
1388                 /* jump over the padding */
1389                 ie_len = ALIGN(ie_len, 4);
1390
1391                 len -= ie_len;
1392                 data += ie_len;
1393         }
1394
1395         if (!fw_file->firmware_data ||
1396             !fw_file->firmware_len) {
1397                 ath10k_warn(ar, "No ATH10K_FW_IE_FW_IMAGE found from '%s/%s', skipping\n",
1398                             ar->hw_params.fw.dir, name);
1399                 ret = -ENOMEDIUM;
1400                 goto err;
1401         }
1402
1403         return 0;
1404
1405 err:
1406         ath10k_core_free_firmware_files(ar);
1407         return ret;
1408 }
1409
1410 static void ath10k_core_get_fw_name(struct ath10k *ar, char *fw_name,
1411                                     size_t fw_name_len, int fw_api)
1412 {
1413         switch (ar->hif.bus) {
1414         case ATH10K_BUS_SDIO:
1415                 scnprintf(fw_name, fw_name_len, "%s-%s-%d.bin",
1416                           ATH10K_FW_FILE_BASE, ath10k_bus_str(ar->hif.bus),
1417                           fw_api);
1418                 break;
1419         case ATH10K_BUS_PCI:
1420         case ATH10K_BUS_AHB:
1421                 scnprintf(fw_name, fw_name_len, "%s-%d.bin",
1422                           ATH10K_FW_FILE_BASE, fw_api);
1423                 break;
1424         }
1425 }
1426
1427 static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
1428 {
1429         int ret, i;
1430         char fw_name[100];
1431
1432         /* calibration file is optional, don't check for any errors */
1433         ath10k_fetch_cal_file(ar);
1434
1435         for (i = ATH10K_FW_API_MAX; i >= ATH10K_FW_API_MIN; i--) {
1436                 ar->fw_api = i;
1437                 ath10k_dbg(ar, ATH10K_DBG_BOOT, "trying fw api %d\n",
1438                            ar->fw_api);
1439
1440                 ath10k_core_get_fw_name(ar, fw_name, sizeof(fw_name), ar->fw_api);
1441                 ret = ath10k_core_fetch_firmware_api_n(ar, fw_name,
1442                                                        &ar->normal_mode_fw.fw_file);
1443                 if (!ret)
1444                         goto success;
1445         }
1446
1447         /* we end up here if we couldn't fetch any firmware */
1448
1449         ath10k_err(ar, "Failed to find firmware-N.bin (N between %d and %d) from %s: %d",
1450                    ATH10K_FW_API_MIN, ATH10K_FW_API_MAX, ar->hw_params.fw.dir,
1451                    ret);
1452
1453         return ret;
1454
1455 success:
1456         ath10k_dbg(ar, ATH10K_DBG_BOOT, "using fw api %d\n", ar->fw_api);
1457
1458         return 0;
1459 }
1460
1461 static int ath10k_core_pre_cal_download(struct ath10k *ar)
1462 {
1463         int ret;
1464
1465         ret = ath10k_download_cal_file(ar, ar->pre_cal_file);
1466         if (ret == 0) {
1467                 ar->cal_mode = ATH10K_PRE_CAL_MODE_FILE;
1468                 goto success;
1469         }
1470
1471         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1472                    "boot did not find a pre calibration file, try DT next: %d\n",
1473                    ret);
1474
1475         ret = ath10k_download_cal_dt(ar, "qcom,ath10k-pre-calibration-data");
1476         if (ret) {
1477                 ath10k_dbg(ar, ATH10K_DBG_BOOT,
1478                            "unable to load pre cal data from DT: %d\n", ret);
1479                 return ret;
1480         }
1481         ar->cal_mode = ATH10K_PRE_CAL_MODE_DT;
1482
1483 success:
1484         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot using calibration mode %s\n",
1485                    ath10k_cal_mode_str(ar->cal_mode));
1486
1487         return 0;
1488 }
1489
1490 static int ath10k_core_pre_cal_config(struct ath10k *ar)
1491 {
1492         int ret;
1493
1494         ret = ath10k_core_pre_cal_download(ar);
1495         if (ret) {
1496                 ath10k_dbg(ar, ATH10K_DBG_BOOT,
1497                            "failed to load pre cal data: %d\n", ret);
1498                 return ret;
1499         }
1500
1501         ret = ath10k_core_get_board_id_from_otp(ar);
1502         if (ret) {
1503                 ath10k_err(ar, "failed to get board id: %d\n", ret);
1504                 return ret;
1505         }
1506
1507         ret = ath10k_download_and_run_otp(ar);
1508         if (ret) {
1509                 ath10k_err(ar, "failed to run otp: %d\n", ret);
1510                 return ret;
1511         }
1512
1513         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1514                    "pre cal configuration done successfully\n");
1515
1516         return 0;
1517 }
1518
1519 static int ath10k_download_cal_data(struct ath10k *ar)
1520 {
1521         int ret;
1522
1523         ret = ath10k_core_pre_cal_config(ar);
1524         if (ret == 0)
1525                 return 0;
1526
1527         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1528                    "pre cal download procedure failed, try cal file: %d\n",
1529                    ret);
1530
1531         ret = ath10k_download_cal_file(ar, ar->cal_file);
1532         if (ret == 0) {
1533                 ar->cal_mode = ATH10K_CAL_MODE_FILE;
1534                 goto done;
1535         }
1536
1537         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1538                    "boot did not find a calibration file, try DT next: %d\n",
1539                    ret);
1540
1541         ret = ath10k_download_cal_dt(ar, "qcom,ath10k-calibration-data");
1542         if (ret == 0) {
1543                 ar->cal_mode = ATH10K_CAL_MODE_DT;
1544                 goto done;
1545         }
1546
1547         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1548                    "boot did not find DT entry, try target EEPROM next: %d\n",
1549                    ret);
1550
1551         ret = ath10k_download_cal_eeprom(ar);
1552         if (ret == 0) {
1553                 ar->cal_mode = ATH10K_CAL_MODE_EEPROM;
1554                 goto done;
1555         }
1556
1557         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1558                    "boot did not find target EEPROM entry, try OTP next: %d\n",
1559                    ret);
1560
1561         ret = ath10k_download_and_run_otp(ar);
1562         if (ret) {
1563                 ath10k_err(ar, "failed to run otp: %d\n", ret);
1564                 return ret;
1565         }
1566
1567         ar->cal_mode = ATH10K_CAL_MODE_OTP;
1568
1569 done:
1570         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot using calibration mode %s\n",
1571                    ath10k_cal_mode_str(ar->cal_mode));
1572         return 0;
1573 }
1574
1575 static int ath10k_init_uart(struct ath10k *ar)
1576 {
1577         int ret;
1578
1579         /*
1580          * Explicitly setting UART prints to zero as target turns it on
1581          * based on scratch registers.
1582          */
1583         ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
1584         if (ret) {
1585                 ath10k_warn(ar, "could not disable UART prints (%d)\n", ret);
1586                 return ret;
1587         }
1588
1589         if (!uart_print)
1590                 return 0;
1591
1592         ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, ar->hw_params.uart_pin);
1593         if (ret) {
1594                 ath10k_warn(ar, "could not enable UART prints (%d)\n", ret);
1595                 return ret;
1596         }
1597
1598         ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
1599         if (ret) {
1600                 ath10k_warn(ar, "could not enable UART prints (%d)\n", ret);
1601                 return ret;
1602         }
1603
1604         /* Set the UART baud rate to 19200. */
1605         ret = ath10k_bmi_write32(ar, hi_desired_baud_rate, 19200);
1606         if (ret) {
1607                 ath10k_warn(ar, "could not set the baud rate (%d)\n", ret);
1608                 return ret;
1609         }
1610
1611         ath10k_info(ar, "UART prints enabled\n");
1612         return 0;
1613 }
1614
1615 static int ath10k_init_hw_params(struct ath10k *ar)
1616 {
1617         const struct ath10k_hw_params *uninitialized_var(hw_params);
1618         int i;
1619
1620         for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
1621                 hw_params = &ath10k_hw_params_list[i];
1622
1623                 if (hw_params->id == ar->target_version &&
1624                     hw_params->dev_id == ar->dev_id)
1625                         break;
1626         }
1627
1628         if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
1629                 ath10k_err(ar, "Unsupported hardware version: 0x%x\n",
1630                            ar->target_version);
1631                 return -EINVAL;
1632         }
1633
1634         ar->hw_params = *hw_params;
1635
1636         ath10k_dbg(ar, ATH10K_DBG_BOOT, "Hardware name %s version 0x%x\n",
1637                    ar->hw_params.name, ar->target_version);
1638
1639         return 0;
1640 }
1641
1642 static void ath10k_core_restart(struct work_struct *work)
1643 {
1644         struct ath10k *ar = container_of(work, struct ath10k, restart_work);
1645         int ret;
1646
1647         set_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
1648
1649         /* Place a barrier to make sure the compiler doesn't reorder
1650          * CRASH_FLUSH and calling other functions.
1651          */
1652         barrier();
1653
1654         ieee80211_stop_queues(ar->hw);
1655         ath10k_drain_tx(ar);
1656         complete(&ar->scan.started);
1657         complete(&ar->scan.completed);
1658         complete(&ar->scan.on_channel);
1659         complete(&ar->offchan_tx_completed);
1660         complete(&ar->install_key_done);
1661         complete(&ar->vdev_setup_done);
1662         complete(&ar->thermal.wmi_sync);
1663         complete(&ar->bss_survey_done);
1664         wake_up(&ar->htt.empty_tx_wq);
1665         wake_up(&ar->wmi.tx_credits_wq);
1666         wake_up(&ar->peer_mapping_wq);
1667
1668         /* TODO: We can have one instance of cancelling coverage_class_work by
1669          * moving it to ath10k_halt(), so that both stop() and restart() would
1670          * call that but it takes conf_mutex() and if we call cancel_work_sync()
1671          * with conf_mutex it will deadlock.
1672          */
1673         cancel_work_sync(&ar->set_coverage_class_work);
1674
1675         mutex_lock(&ar->conf_mutex);
1676
1677         switch (ar->state) {
1678         case ATH10K_STATE_ON:
1679                 ar->state = ATH10K_STATE_RESTARTING;
1680                 ath10k_halt(ar);
1681                 ath10k_scan_finish(ar);
1682                 ieee80211_restart_hw(ar->hw);
1683                 break;
1684         case ATH10K_STATE_OFF:
1685                 /* this can happen if driver is being unloaded
1686                  * or if the crash happens during FW probing
1687                  */
1688                 ath10k_warn(ar, "cannot restart a device that hasn't been started\n");
1689                 break;
1690         case ATH10K_STATE_RESTARTING:
1691                 /* hw restart might be requested from multiple places */
1692                 break;
1693         case ATH10K_STATE_RESTARTED:
1694                 ar->state = ATH10K_STATE_WEDGED;
1695                 /* fall through */
1696         case ATH10K_STATE_WEDGED:
1697                 ath10k_warn(ar, "device is wedged, will not restart\n");
1698                 break;
1699         case ATH10K_STATE_UTF:
1700                 ath10k_warn(ar, "firmware restart in UTF mode not supported\n");
1701                 break;
1702         }
1703
1704         mutex_unlock(&ar->conf_mutex);
1705
1706         ret = ath10k_debug_fw_devcoredump(ar);
1707         if (ret)
1708                 ath10k_warn(ar, "failed to send firmware crash dump via devcoredump: %d",
1709                             ret);
1710 }
1711
1712 static void ath10k_core_set_coverage_class_work(struct work_struct *work)
1713 {
1714         struct ath10k *ar = container_of(work, struct ath10k,
1715                                          set_coverage_class_work);
1716
1717         if (ar->hw_params.hw_ops->set_coverage_class)
1718                 ar->hw_params.hw_ops->set_coverage_class(ar, -1);
1719 }
1720
1721 static int ath10k_core_init_firmware_features(struct ath10k *ar)
1722 {
1723         struct ath10k_fw_file *fw_file = &ar->normal_mode_fw.fw_file;
1724
1725         if (test_bit(ATH10K_FW_FEATURE_WMI_10_2, fw_file->fw_features) &&
1726             !test_bit(ATH10K_FW_FEATURE_WMI_10X, fw_file->fw_features)) {
1727                 ath10k_err(ar, "feature bits corrupted: 10.2 feature requires 10.x feature to be set as well");
1728                 return -EINVAL;
1729         }
1730
1731         if (fw_file->wmi_op_version >= ATH10K_FW_WMI_OP_VERSION_MAX) {
1732                 ath10k_err(ar, "unsupported WMI OP version (max %d): %d\n",
1733                            ATH10K_FW_WMI_OP_VERSION_MAX, fw_file->wmi_op_version);
1734                 return -EINVAL;
1735         }
1736
1737         ar->wmi.rx_decap_mode = ATH10K_HW_TXRX_NATIVE_WIFI;
1738         switch (ath10k_cryptmode_param) {
1739         case ATH10K_CRYPT_MODE_HW:
1740                 clear_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags);
1741                 clear_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags);
1742                 break;
1743         case ATH10K_CRYPT_MODE_SW:
1744                 if (!test_bit(ATH10K_FW_FEATURE_RAW_MODE_SUPPORT,
1745                               fw_file->fw_features)) {
1746                         ath10k_err(ar, "cryptmode > 0 requires raw mode support from firmware");
1747                         return -EINVAL;
1748                 }
1749
1750                 set_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags);
1751                 set_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags);
1752                 break;
1753         default:
1754                 ath10k_info(ar, "invalid cryptmode: %d\n",
1755                             ath10k_cryptmode_param);
1756                 return -EINVAL;
1757         }
1758
1759         ar->htt.max_num_amsdu = ATH10K_HTT_MAX_NUM_AMSDU_DEFAULT;
1760         ar->htt.max_num_ampdu = ATH10K_HTT_MAX_NUM_AMPDU_DEFAULT;
1761
1762         if (rawmode) {
1763                 if (!test_bit(ATH10K_FW_FEATURE_RAW_MODE_SUPPORT,
1764                               fw_file->fw_features)) {
1765                         ath10k_err(ar, "rawmode = 1 requires support from firmware");
1766                         return -EINVAL;
1767                 }
1768                 set_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags);
1769         }
1770
1771         if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
1772                 ar->wmi.rx_decap_mode = ATH10K_HW_TXRX_RAW;
1773
1774                 /* Workaround:
1775                  *
1776                  * Firmware A-MSDU aggregation breaks with RAW Tx encap mode
1777                  * and causes enormous performance issues (malformed frames,
1778                  * etc).
1779                  *
1780                  * Disabling A-MSDU makes RAW mode stable with heavy traffic
1781                  * albeit a bit slower compared to regular operation.
1782                  */
1783                 ar->htt.max_num_amsdu = 1;
1784         }
1785
1786         /* Backwards compatibility for firmwares without
1787          * ATH10K_FW_IE_WMI_OP_VERSION.
1788          */
1789         if (fw_file->wmi_op_version == ATH10K_FW_WMI_OP_VERSION_UNSET) {
1790                 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, fw_file->fw_features)) {
1791                         if (test_bit(ATH10K_FW_FEATURE_WMI_10_2,
1792                                      fw_file->fw_features))
1793                                 fw_file->wmi_op_version = ATH10K_FW_WMI_OP_VERSION_10_2;
1794                         else
1795                                 fw_file->wmi_op_version = ATH10K_FW_WMI_OP_VERSION_10_1;
1796                 } else {
1797                         fw_file->wmi_op_version = ATH10K_FW_WMI_OP_VERSION_MAIN;
1798                 }
1799         }
1800
1801         switch (fw_file->wmi_op_version) {
1802         case ATH10K_FW_WMI_OP_VERSION_MAIN:
1803                 ar->max_num_peers = TARGET_NUM_PEERS;
1804                 ar->max_num_stations = TARGET_NUM_STATIONS;
1805                 ar->max_num_vdevs = TARGET_NUM_VDEVS;
1806                 ar->htt.max_num_pending_tx = TARGET_NUM_MSDU_DESC;
1807                 ar->fw_stats_req_mask = WMI_STAT_PDEV | WMI_STAT_VDEV |
1808                         WMI_STAT_PEER;
1809                 ar->max_spatial_stream = WMI_MAX_SPATIAL_STREAM;
1810                 break;
1811         case ATH10K_FW_WMI_OP_VERSION_10_1:
1812         case ATH10K_FW_WMI_OP_VERSION_10_2:
1813         case ATH10K_FW_WMI_OP_VERSION_10_2_4:
1814                 if (ath10k_peer_stats_enabled(ar)) {
1815                         ar->max_num_peers = TARGET_10X_TX_STATS_NUM_PEERS;
1816                         ar->max_num_stations = TARGET_10X_TX_STATS_NUM_STATIONS;
1817                 } else {
1818                         ar->max_num_peers = TARGET_10X_NUM_PEERS;
1819                         ar->max_num_stations = TARGET_10X_NUM_STATIONS;
1820                 }
1821                 ar->max_num_vdevs = TARGET_10X_NUM_VDEVS;
1822                 ar->htt.max_num_pending_tx = TARGET_10X_NUM_MSDU_DESC;
1823                 ar->fw_stats_req_mask = WMI_STAT_PEER;
1824                 ar->max_spatial_stream = WMI_MAX_SPATIAL_STREAM;
1825                 break;
1826         case ATH10K_FW_WMI_OP_VERSION_TLV:
1827                 ar->max_num_peers = TARGET_TLV_NUM_PEERS;
1828                 ar->max_num_stations = TARGET_TLV_NUM_STATIONS;
1829                 ar->max_num_vdevs = TARGET_TLV_NUM_VDEVS;
1830                 ar->max_num_tdls_vdevs = TARGET_TLV_NUM_TDLS_VDEVS;
1831                 ar->htt.max_num_pending_tx = TARGET_TLV_NUM_MSDU_DESC;
1832                 ar->wow.max_num_patterns = TARGET_TLV_NUM_WOW_PATTERNS;
1833                 ar->fw_stats_req_mask = WMI_STAT_PDEV | WMI_STAT_VDEV |
1834                         WMI_STAT_PEER;
1835                 ar->max_spatial_stream = WMI_MAX_SPATIAL_STREAM;
1836                 break;
1837         case ATH10K_FW_WMI_OP_VERSION_10_4:
1838                 ar->max_num_peers = TARGET_10_4_NUM_PEERS;
1839                 ar->max_num_stations = TARGET_10_4_NUM_STATIONS;
1840                 ar->num_active_peers = TARGET_10_4_ACTIVE_PEERS;
1841                 ar->max_num_vdevs = TARGET_10_4_NUM_VDEVS;
1842                 ar->num_tids = TARGET_10_4_TGT_NUM_TIDS;
1843                 ar->fw_stats_req_mask = WMI_10_4_STAT_PEER |
1844                                         WMI_10_4_STAT_PEER_EXTD;
1845                 ar->max_spatial_stream = ar->hw_params.max_spatial_stream;
1846
1847                 if (test_bit(ATH10K_FW_FEATURE_PEER_FLOW_CONTROL,
1848                              fw_file->fw_features))
1849                         ar->htt.max_num_pending_tx = TARGET_10_4_NUM_MSDU_DESC_PFC;
1850                 else
1851                         ar->htt.max_num_pending_tx = TARGET_10_4_NUM_MSDU_DESC;
1852                 break;
1853         case ATH10K_FW_WMI_OP_VERSION_UNSET:
1854         case ATH10K_FW_WMI_OP_VERSION_MAX:
1855                 WARN_ON(1);
1856                 return -EINVAL;
1857         }
1858
1859         /* Backwards compatibility for firmwares without
1860          * ATH10K_FW_IE_HTT_OP_VERSION.
1861          */
1862         if (fw_file->htt_op_version == ATH10K_FW_HTT_OP_VERSION_UNSET) {
1863                 switch (fw_file->wmi_op_version) {
1864                 case ATH10K_FW_WMI_OP_VERSION_MAIN:
1865                         fw_file->htt_op_version = ATH10K_FW_HTT_OP_VERSION_MAIN;
1866                         break;
1867                 case ATH10K_FW_WMI_OP_VERSION_10_1:
1868                 case ATH10K_FW_WMI_OP_VERSION_10_2:
1869                 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
1870                         fw_file->htt_op_version = ATH10K_FW_HTT_OP_VERSION_10_1;
1871                         break;
1872                 case ATH10K_FW_WMI_OP_VERSION_TLV:
1873                         fw_file->htt_op_version = ATH10K_FW_HTT_OP_VERSION_TLV;
1874                         break;
1875                 case ATH10K_FW_WMI_OP_VERSION_10_4:
1876                 case ATH10K_FW_WMI_OP_VERSION_UNSET:
1877                 case ATH10K_FW_WMI_OP_VERSION_MAX:
1878                         ath10k_err(ar, "htt op version not found from fw meta data");
1879                         return -EINVAL;
1880                 }
1881         }
1882
1883         return 0;
1884 }
1885
1886 static int ath10k_core_reset_rx_filter(struct ath10k *ar)
1887 {
1888         int ret;
1889         int vdev_id;
1890         int vdev_type;
1891         int vdev_subtype;
1892         const u8 *vdev_addr;
1893
1894         vdev_id = 0;
1895         vdev_type = WMI_VDEV_TYPE_STA;
1896         vdev_subtype = ath10k_wmi_get_vdev_subtype(ar, WMI_VDEV_SUBTYPE_NONE);
1897         vdev_addr = ar->mac_addr;
1898
1899         ret = ath10k_wmi_vdev_create(ar, vdev_id, vdev_type, vdev_subtype,
1900                                      vdev_addr);
1901         if (ret) {
1902                 ath10k_err(ar, "failed to create dummy vdev: %d\n", ret);
1903                 return ret;
1904         }
1905
1906         ret = ath10k_wmi_vdev_delete(ar, vdev_id);
1907         if (ret) {
1908                 ath10k_err(ar, "failed to delete dummy vdev: %d\n", ret);
1909                 return ret;
1910         }
1911
1912         /* WMI and HTT may use separate HIF pipes and are not guaranteed to be
1913          * serialized properly implicitly.
1914          *
1915          * Moreover (most) WMI commands have no explicit acknowledges. It is
1916          * possible to infer it implicitly by poking firmware with echo
1917          * command - getting a reply means all preceding comments have been
1918          * (mostly) processed.
1919          *
1920          * In case of vdev create/delete this is sufficient.
1921          *
1922          * Without this it's possible to end up with a race when HTT Rx ring is
1923          * started before vdev create/delete hack is complete allowing a short
1924          * window of opportunity to receive (and Tx ACK) a bunch of frames.
1925          */
1926         ret = ath10k_wmi_barrier(ar);
1927         if (ret) {
1928                 ath10k_err(ar, "failed to ping firmware: %d\n", ret);
1929                 return ret;
1930         }
1931
1932         return 0;
1933 }
1934
1935 int ath10k_core_start(struct ath10k *ar, enum ath10k_firmware_mode mode,
1936                       const struct ath10k_fw_components *fw)
1937 {
1938         int status;
1939         u32 val;
1940
1941         lockdep_assert_held(&ar->conf_mutex);
1942
1943         clear_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
1944
1945         ar->running_fw = fw;
1946
1947         ath10k_bmi_start(ar);
1948
1949         if (ath10k_init_configure_target(ar)) {
1950                 status = -EINVAL;
1951                 goto err;
1952         }
1953
1954         status = ath10k_download_cal_data(ar);
1955         if (status)
1956                 goto err;
1957
1958         /* Some of of qca988x solutions are having global reset issue
1959          * during target initialization. Bypassing PLL setting before
1960          * downloading firmware and letting the SoC run on REF_CLK is
1961          * fixing the problem. Corresponding firmware change is also needed
1962          * to set the clock source once the target is initialized.
1963          */
1964         if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_SKIP_CLOCK_INIT,
1965                      ar->running_fw->fw_file.fw_features)) {
1966                 status = ath10k_bmi_write32(ar, hi_skip_clock_init, 1);
1967                 if (status) {
1968                         ath10k_err(ar, "could not write to skip_clock_init: %d\n",
1969                                    status);
1970                         goto err;
1971                 }
1972         }
1973
1974         status = ath10k_download_fw(ar);
1975         if (status)
1976                 goto err;
1977
1978         status = ath10k_init_uart(ar);
1979         if (status)
1980                 goto err;
1981
1982         if (ar->hif.bus == ATH10K_BUS_SDIO)
1983                 ath10k_init_sdio(ar);
1984
1985         ar->htc.htc_ops.target_send_suspend_complete =
1986                 ath10k_send_suspend_complete;
1987
1988         status = ath10k_htc_init(ar);
1989         if (status) {
1990                 ath10k_err(ar, "could not init HTC (%d)\n", status);
1991                 goto err;
1992         }
1993
1994         status = ath10k_bmi_done(ar);
1995         if (status)
1996                 goto err;
1997
1998         status = ath10k_wmi_attach(ar);
1999         if (status) {
2000                 ath10k_err(ar, "WMI attach failed: %d\n", status);
2001                 goto err;
2002         }
2003
2004         status = ath10k_htt_init(ar);
2005         if (status) {
2006                 ath10k_err(ar, "failed to init htt: %d\n", status);
2007                 goto err_wmi_detach;
2008         }
2009
2010         status = ath10k_htt_tx_start(&ar->htt);
2011         if (status) {
2012                 ath10k_err(ar, "failed to alloc htt tx: %d\n", status);
2013                 goto err_wmi_detach;
2014         }
2015
2016         status = ath10k_htt_rx_alloc(&ar->htt);
2017         if (status) {
2018                 ath10k_err(ar, "failed to alloc htt rx: %d\n", status);
2019                 goto err_htt_tx_detach;
2020         }
2021
2022         status = ath10k_hif_start(ar);
2023         if (status) {
2024                 ath10k_err(ar, "could not start HIF: %d\n", status);
2025                 goto err_htt_rx_detach;
2026         }
2027
2028         status = ath10k_htc_wait_target(&ar->htc);
2029         if (status) {
2030                 ath10k_err(ar, "failed to connect to HTC: %d\n", status);
2031                 goto err_hif_stop;
2032         }
2033
2034         if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
2035                 status = ath10k_htt_connect(&ar->htt);
2036                 if (status) {
2037                         ath10k_err(ar, "failed to connect htt (%d)\n", status);
2038                         goto err_hif_stop;
2039                 }
2040         }
2041
2042         status = ath10k_wmi_connect(ar);
2043         if (status) {
2044                 ath10k_err(ar, "could not connect wmi: %d\n", status);
2045                 goto err_hif_stop;
2046         }
2047
2048         status = ath10k_htc_start(&ar->htc);
2049         if (status) {
2050                 ath10k_err(ar, "failed to start htc: %d\n", status);
2051                 goto err_hif_stop;
2052         }
2053
2054         if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
2055                 status = ath10k_wmi_wait_for_service_ready(ar);
2056                 if (status) {
2057                         ath10k_warn(ar, "wmi service ready event not received");
2058                         goto err_hif_stop;
2059                 }
2060         }
2061
2062         ath10k_dbg(ar, ATH10K_DBG_BOOT, "firmware %s booted\n",
2063                    ar->hw->wiphy->fw_version);
2064
2065         if (test_bit(WMI_SERVICE_EXT_RES_CFG_SUPPORT, ar->wmi.svc_map) &&
2066             mode == ATH10K_FIRMWARE_MODE_NORMAL) {
2067                 val = 0;
2068                 if (ath10k_peer_stats_enabled(ar))
2069                         val = WMI_10_4_PEER_STATS;
2070
2071                 if (test_bit(WMI_SERVICE_BSS_CHANNEL_INFO_64, ar->wmi.svc_map))
2072                         val |= WMI_10_4_BSS_CHANNEL_INFO_64;
2073
2074                 /* 10.4 firmware supports BT-Coex without reloading firmware
2075                  * via pdev param. To support Bluetooth coexistence pdev param,
2076                  * WMI_COEX_GPIO_SUPPORT of extended resource config should be
2077                  * enabled always.
2078                  */
2079                 if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map) &&
2080                     test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM,
2081                              ar->running_fw->fw_file.fw_features))
2082                         val |= WMI_10_4_COEX_GPIO_SUPPORT;
2083
2084                 status = ath10k_mac_ext_resource_config(ar, val);
2085                 if (status) {
2086                         ath10k_err(ar,
2087                                    "failed to send ext resource cfg command : %d\n",
2088                                    status);
2089                         goto err_hif_stop;
2090                 }
2091         }
2092
2093         status = ath10k_wmi_cmd_init(ar);
2094         if (status) {
2095                 ath10k_err(ar, "could not send WMI init command (%d)\n",
2096                            status);
2097                 goto err_hif_stop;
2098         }
2099
2100         status = ath10k_wmi_wait_for_unified_ready(ar);
2101         if (status) {
2102                 ath10k_err(ar, "wmi unified ready event not received\n");
2103                 goto err_hif_stop;
2104         }
2105
2106         /* Some firmware revisions do not properly set up hardware rx filter
2107          * registers.
2108          *
2109          * A known example from QCA9880 and 10.2.4 is that MAC_PCU_ADDR1_MASK
2110          * is filled with 0s instead of 1s allowing HW to respond with ACKs to
2111          * any frames that matches MAC_PCU_RX_FILTER which is also
2112          * misconfigured to accept anything.
2113          *
2114          * The ADDR1 is programmed using internal firmware structure field and
2115          * can't be (easily/sanely) reached from the driver explicitly. It is
2116          * possible to implicitly make it correct by creating a dummy vdev and
2117          * then deleting it.
2118          */
2119         if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
2120                 status = ath10k_core_reset_rx_filter(ar);
2121                 if (status) {
2122                         ath10k_err(ar,
2123                                    "failed to reset rx filter: %d\n", status);
2124                         goto err_hif_stop;
2125                 }
2126         }
2127
2128         /* If firmware indicates Full Rx Reorder support it must be used in a
2129          * slightly different manner. Let HTT code know.
2130          */
2131         ar->htt.rx_ring.in_ord_rx = !!(test_bit(WMI_SERVICE_RX_FULL_REORDER,
2132                                                 ar->wmi.svc_map));
2133
2134         status = ath10k_htt_rx_ring_refill(ar);
2135         if (status) {
2136                 ath10k_err(ar, "failed to refill htt rx ring: %d\n", status);
2137                 goto err_hif_stop;
2138         }
2139
2140         if (ar->max_num_vdevs >= 64)
2141                 ar->free_vdev_map = 0xFFFFFFFFFFFFFFFFLL;
2142         else
2143                 ar->free_vdev_map = (1LL << ar->max_num_vdevs) - 1;
2144
2145         INIT_LIST_HEAD(&ar->arvifs);
2146
2147         /* we don't care about HTT in UTF mode */
2148         if (mode == ATH10K_FIRMWARE_MODE_NORMAL) {
2149                 status = ath10k_htt_setup(&ar->htt);
2150                 if (status) {
2151                         ath10k_err(ar, "failed to setup htt: %d\n", status);
2152                         goto err_hif_stop;
2153                 }
2154         }
2155
2156         status = ath10k_debug_start(ar);
2157         if (status)
2158                 goto err_hif_stop;
2159
2160         return 0;
2161
2162 err_hif_stop:
2163         ath10k_hif_stop(ar);
2164 err_htt_rx_detach:
2165         ath10k_htt_rx_free(&ar->htt);
2166 err_htt_tx_detach:
2167         ath10k_htt_tx_free(&ar->htt);
2168 err_wmi_detach:
2169         ath10k_wmi_detach(ar);
2170 err:
2171         return status;
2172 }
2173 EXPORT_SYMBOL(ath10k_core_start);
2174
2175 int ath10k_wait_for_suspend(struct ath10k *ar, u32 suspend_opt)
2176 {
2177         int ret;
2178         unsigned long time_left;
2179
2180         reinit_completion(&ar->target_suspend);
2181
2182         ret = ath10k_wmi_pdev_suspend_target(ar, suspend_opt);
2183         if (ret) {
2184                 ath10k_warn(ar, "could not suspend target (%d)\n", ret);
2185                 return ret;
2186         }
2187
2188         time_left = wait_for_completion_timeout(&ar->target_suspend, 1 * HZ);
2189
2190         if (!time_left) {
2191                 ath10k_warn(ar, "suspend timed out - target pause event never came\n");
2192                 return -ETIMEDOUT;
2193         }
2194
2195         return 0;
2196 }
2197
2198 void ath10k_core_stop(struct ath10k *ar)
2199 {
2200         lockdep_assert_held(&ar->conf_mutex);
2201         ath10k_debug_stop(ar);
2202
2203         /* try to suspend target */
2204         if (ar->state != ATH10K_STATE_RESTARTING &&
2205             ar->state != ATH10K_STATE_UTF)
2206                 ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND_AND_DISABLE_INTR);
2207
2208         ath10k_hif_stop(ar);
2209         ath10k_htt_tx_stop(&ar->htt);
2210         ath10k_htt_rx_free(&ar->htt);
2211         ath10k_wmi_detach(ar);
2212 }
2213 EXPORT_SYMBOL(ath10k_core_stop);
2214
2215 /* mac80211 manages fw/hw initialization through start/stop hooks. However in
2216  * order to know what hw capabilities should be advertised to mac80211 it is
2217  * necessary to load the firmware (and tear it down immediately since start
2218  * hook will try to init it again) before registering
2219  */
2220 static int ath10k_core_probe_fw(struct ath10k *ar)
2221 {
2222         struct bmi_target_info target_info;
2223         int ret = 0;
2224
2225         ret = ath10k_hif_power_up(ar);
2226         if (ret) {
2227                 ath10k_err(ar, "could not start pci hif (%d)\n", ret);
2228                 return ret;
2229         }
2230
2231         memset(&target_info, 0, sizeof(target_info));
2232         if (ar->hif.bus == ATH10K_BUS_SDIO)
2233                 ret = ath10k_bmi_get_target_info_sdio(ar, &target_info);
2234         else
2235                 ret = ath10k_bmi_get_target_info(ar, &target_info);
2236         if (ret) {
2237                 ath10k_err(ar, "could not get target info (%d)\n", ret);
2238                 goto err_power_down;
2239         }
2240
2241         ar->target_version = target_info.version;
2242         ar->hw->wiphy->hw_version = target_info.version;
2243
2244         ret = ath10k_init_hw_params(ar);
2245         if (ret) {
2246                 ath10k_err(ar, "could not get hw params (%d)\n", ret);
2247                 goto err_power_down;
2248         }
2249
2250         ret = ath10k_core_fetch_firmware_files(ar);
2251         if (ret) {
2252                 ath10k_err(ar, "could not fetch firmware files (%d)\n", ret);
2253                 goto err_power_down;
2254         }
2255
2256         BUILD_BUG_ON(sizeof(ar->hw->wiphy->fw_version) !=
2257                      sizeof(ar->normal_mode_fw.fw_file.fw_version));
2258         memcpy(ar->hw->wiphy->fw_version, ar->normal_mode_fw.fw_file.fw_version,
2259                sizeof(ar->hw->wiphy->fw_version));
2260
2261         ath10k_debug_print_hwfw_info(ar);
2262
2263         ret = ath10k_core_pre_cal_download(ar);
2264         if (ret) {
2265                 /* pre calibration data download is not necessary
2266                  * for all the chipsets. Ignore failures and continue.
2267                  */
2268                 ath10k_dbg(ar, ATH10K_DBG_BOOT,
2269                            "could not load pre cal data: %d\n", ret);
2270         }
2271
2272         ret = ath10k_core_get_board_id_from_otp(ar);
2273         if (ret && ret != -EOPNOTSUPP) {
2274                 ath10k_err(ar, "failed to get board id from otp: %d\n",
2275                            ret);
2276                 goto err_free_firmware_files;
2277         }
2278
2279         ret = ath10k_core_check_smbios(ar);
2280         if (ret)
2281                 ath10k_dbg(ar, ATH10K_DBG_BOOT, "bdf variant name not set.\n");
2282
2283         ret = ath10k_core_fetch_board_file(ar);
2284         if (ret) {
2285                 ath10k_err(ar, "failed to fetch board file: %d\n", ret);
2286                 goto err_free_firmware_files;
2287         }
2288
2289         ath10k_debug_print_board_info(ar);
2290
2291         ret = ath10k_core_init_firmware_features(ar);
2292         if (ret) {
2293                 ath10k_err(ar, "fatal problem with firmware features: %d\n",
2294                            ret);
2295                 goto err_free_firmware_files;
2296         }
2297
2298         ret = ath10k_swap_code_seg_init(ar, &ar->normal_mode_fw.fw_file);
2299         if (ret) {
2300                 ath10k_err(ar, "failed to initialize code swap segment: %d\n",
2301                            ret);
2302                 goto err_free_firmware_files;
2303         }
2304
2305         mutex_lock(&ar->conf_mutex);
2306
2307         ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL,
2308                                 &ar->normal_mode_fw);
2309         if (ret) {
2310                 ath10k_err(ar, "could not init core (%d)\n", ret);
2311                 goto err_unlock;
2312         }
2313
2314         ath10k_debug_print_boot_info(ar);
2315         ath10k_core_stop(ar);
2316
2317         mutex_unlock(&ar->conf_mutex);
2318
2319         ath10k_hif_power_down(ar);
2320         return 0;
2321
2322 err_unlock:
2323         mutex_unlock(&ar->conf_mutex);
2324
2325 err_free_firmware_files:
2326         ath10k_core_free_firmware_files(ar);
2327
2328 err_power_down:
2329         ath10k_hif_power_down(ar);
2330
2331         return ret;
2332 }
2333
2334 static void ath10k_core_register_work(struct work_struct *work)
2335 {
2336         struct ath10k *ar = container_of(work, struct ath10k, register_work);
2337         int status;
2338
2339         /* peer stats are enabled by default */
2340         set_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags);
2341
2342         status = ath10k_core_probe_fw(ar);
2343         if (status) {
2344                 ath10k_err(ar, "could not probe fw (%d)\n", status);
2345                 goto err;
2346         }
2347
2348         status = ath10k_mac_register(ar);
2349         if (status) {
2350                 ath10k_err(ar, "could not register to mac80211 (%d)\n", status);
2351                 goto err_release_fw;
2352         }
2353
2354         status = ath10k_debug_register(ar);
2355         if (status) {
2356                 ath10k_err(ar, "unable to initialize debugfs\n");
2357                 goto err_unregister_mac;
2358         }
2359
2360         status = ath10k_spectral_create(ar);
2361         if (status) {
2362                 ath10k_err(ar, "failed to initialize spectral\n");
2363                 goto err_debug_destroy;
2364         }
2365
2366         status = ath10k_thermal_register(ar);
2367         if (status) {
2368                 ath10k_err(ar, "could not register thermal device: %d\n",
2369                            status);
2370                 goto err_spectral_destroy;
2371         }
2372
2373         set_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags);
2374         return;
2375
2376 err_spectral_destroy:
2377         ath10k_spectral_destroy(ar);
2378 err_debug_destroy:
2379         ath10k_debug_destroy(ar);
2380 err_unregister_mac:
2381         ath10k_mac_unregister(ar);
2382 err_release_fw:
2383         ath10k_core_free_firmware_files(ar);
2384 err:
2385         /* TODO: It's probably a good idea to release device from the driver
2386          * but calling device_release_driver() here will cause a deadlock.
2387          */
2388         return;
2389 }
2390
2391 int ath10k_core_register(struct ath10k *ar, u32 chip_id)
2392 {
2393         ar->chip_id = chip_id;
2394         queue_work(ar->workqueue, &ar->register_work);
2395
2396         return 0;
2397 }
2398 EXPORT_SYMBOL(ath10k_core_register);
2399
2400 void ath10k_core_unregister(struct ath10k *ar)
2401 {
2402         cancel_work_sync(&ar->register_work);
2403
2404         if (!test_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags))
2405                 return;
2406
2407         ath10k_thermal_unregister(ar);
2408         /* Stop spectral before unregistering from mac80211 to remove the
2409          * relayfs debugfs file cleanly. Otherwise the parent debugfs tree
2410          * would be already be free'd recursively, leading to a double free.
2411          */
2412         ath10k_spectral_destroy(ar);
2413
2414         /* We must unregister from mac80211 before we stop HTC and HIF.
2415          * Otherwise we will fail to submit commands to FW and mac80211 will be
2416          * unhappy about callback failures.
2417          */
2418         ath10k_mac_unregister(ar);
2419
2420         ath10k_testmode_destroy(ar);
2421
2422         ath10k_core_free_firmware_files(ar);
2423         ath10k_core_free_board_files(ar);
2424
2425         ath10k_debug_unregister(ar);
2426 }
2427 EXPORT_SYMBOL(ath10k_core_unregister);
2428
2429 struct ath10k *ath10k_core_create(size_t priv_size, struct device *dev,
2430                                   enum ath10k_bus bus,
2431                                   enum ath10k_hw_rev hw_rev,
2432                                   const struct ath10k_hif_ops *hif_ops)
2433 {
2434         struct ath10k *ar;
2435         int ret;
2436
2437         ar = ath10k_mac_create(priv_size);
2438         if (!ar)
2439                 return NULL;
2440
2441         ar->ath_common.priv = ar;
2442         ar->ath_common.hw = ar->hw;
2443         ar->dev = dev;
2444         ar->hw_rev = hw_rev;
2445         ar->hif.ops = hif_ops;
2446         ar->hif.bus = bus;
2447
2448         switch (hw_rev) {
2449         case ATH10K_HW_QCA988X:
2450         case ATH10K_HW_QCA9887:
2451                 ar->regs = &qca988x_regs;
2452                 ar->hw_values = &qca988x_values;
2453                 break;
2454         case ATH10K_HW_QCA6174:
2455         case ATH10K_HW_QCA9377:
2456                 ar->regs = &qca6174_regs;
2457                 ar->hw_values = &qca6174_values;
2458                 break;
2459         case ATH10K_HW_QCA99X0:
2460         case ATH10K_HW_QCA9984:
2461                 ar->regs = &qca99x0_regs;
2462                 ar->hw_values = &qca99x0_values;
2463                 break;
2464         case ATH10K_HW_QCA9888:
2465                 ar->regs = &qca99x0_regs;
2466                 ar->hw_values = &qca9888_values;
2467                 break;
2468         case ATH10K_HW_QCA4019:
2469                 ar->regs = &qca4019_regs;
2470                 ar->hw_values = &qca4019_values;
2471                 break;
2472         default:
2473                 ath10k_err(ar, "unsupported core hardware revision %d\n",
2474                            hw_rev);
2475                 ret = -ENOTSUPP;
2476                 goto err_free_mac;
2477         }
2478
2479         init_completion(&ar->scan.started);
2480         init_completion(&ar->scan.completed);
2481         init_completion(&ar->scan.on_channel);
2482         init_completion(&ar->target_suspend);
2483         init_completion(&ar->wow.wakeup_completed);
2484
2485         init_completion(&ar->install_key_done);
2486         init_completion(&ar->vdev_setup_done);
2487         init_completion(&ar->thermal.wmi_sync);
2488         init_completion(&ar->bss_survey_done);
2489
2490         INIT_DELAYED_WORK(&ar->scan.timeout, ath10k_scan_timeout_work);
2491
2492         ar->workqueue = create_singlethread_workqueue("ath10k_wq");
2493         if (!ar->workqueue)
2494                 goto err_free_mac;
2495
2496         ar->workqueue_aux = create_singlethread_workqueue("ath10k_aux_wq");
2497         if (!ar->workqueue_aux)
2498                 goto err_free_wq;
2499
2500         mutex_init(&ar->conf_mutex);
2501         spin_lock_init(&ar->data_lock);
2502         spin_lock_init(&ar->txqs_lock);
2503
2504         INIT_LIST_HEAD(&ar->txqs);
2505         INIT_LIST_HEAD(&ar->peers);
2506         init_waitqueue_head(&ar->peer_mapping_wq);
2507         init_waitqueue_head(&ar->htt.empty_tx_wq);
2508         init_waitqueue_head(&ar->wmi.tx_credits_wq);
2509
2510         init_completion(&ar->offchan_tx_completed);
2511         INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
2512         skb_queue_head_init(&ar->offchan_tx_queue);
2513
2514         INIT_WORK(&ar->wmi_mgmt_tx_work, ath10k_mgmt_over_wmi_tx_work);
2515         skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
2516
2517         INIT_WORK(&ar->register_work, ath10k_core_register_work);
2518         INIT_WORK(&ar->restart_work, ath10k_core_restart);
2519         INIT_WORK(&ar->set_coverage_class_work,
2520                   ath10k_core_set_coverage_class_work);
2521
2522         init_dummy_netdev(&ar->napi_dev);
2523
2524         ret = ath10k_debug_create(ar);
2525         if (ret)
2526                 goto err_free_aux_wq;
2527
2528         return ar;
2529
2530 err_free_aux_wq:
2531         destroy_workqueue(ar->workqueue_aux);
2532 err_free_wq:
2533         destroy_workqueue(ar->workqueue);
2534
2535 err_free_mac:
2536         ath10k_mac_destroy(ar);
2537
2538         return NULL;
2539 }
2540 EXPORT_SYMBOL(ath10k_core_create);
2541
2542 void ath10k_core_destroy(struct ath10k *ar)
2543 {
2544         flush_workqueue(ar->workqueue);
2545         destroy_workqueue(ar->workqueue);
2546
2547         flush_workqueue(ar->workqueue_aux);
2548         destroy_workqueue(ar->workqueue_aux);
2549
2550         ath10k_debug_destroy(ar);
2551         ath10k_htt_tx_destroy(&ar->htt);
2552         ath10k_wmi_free_host_mem(ar);
2553         ath10k_mac_destroy(ar);
2554 }
2555 EXPORT_SYMBOL(ath10k_core_destroy);
2556
2557 MODULE_AUTHOR("Qualcomm Atheros");
2558 MODULE_DESCRIPTION("Core module for Qualcomm Atheros 802.11ac wireless LAN cards.");
2559 MODULE_LICENSE("Dual BSD/GPL");