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