blob: 7b5dd09fab67e282b9865b5979c736d6efac9110 [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <linux/module.h>
19#include <linux/firmware.h>
20
21#include "core.h"
22#include "mac.h"
23#include "htc.h"
24#include "hif.h"
25#include "wmi.h"
26#include "bmi.h"
27#include "debug.h"
28#include "htt.h"
29
30unsigned int ath10k_debug_mask;
31static bool uart_print;
32static unsigned int ath10k_p2p;
33module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
34module_param(uart_print, bool, 0644);
35module_param_named(p2p, ath10k_p2p, uint, 0644);
36MODULE_PARM_DESC(debug_mask, "Debugging mask");
37MODULE_PARM_DESC(uart_print, "Uart target debugging");
38MODULE_PARM_DESC(p2p, "Enable ath10k P2P support");
39
40static const struct ath10k_hw_params ath10k_hw_params_list[] = {
41 {
Kalle Valo5e3dd152013-06-12 20:52:10 +030042 .id = QCA988X_HW_2_0_VERSION,
43 .name = "qca988x hw2.0",
44 .patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
45 .fw = {
46 .dir = QCA988X_HW_2_0_FW_DIR,
47 .fw = QCA988X_HW_2_0_FW_FILE,
48 .otp = QCA988X_HW_2_0_OTP_FILE,
49 .board = QCA988X_HW_2_0_BOARD_DATA_FILE,
50 },
51 },
52};
53
54static void ath10k_send_suspend_complete(struct ath10k *ar)
55{
Kalle Valoeffea962013-09-08 17:55:44 +030056 ath10k_dbg(ATH10K_DBG_BOOT, "boot suspend complete\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +030057
58 ar->is_target_paused = true;
59 wake_up(&ar->event_queue);
60}
61
Kalle Valo5e3dd152013-06-12 20:52:10 +030062static int ath10k_init_connect_htc(struct ath10k *ar)
63{
64 int status;
65
66 status = ath10k_wmi_connect_htc_service(ar);
67 if (status)
68 goto conn_fail;
69
70 /* Start HTC */
Michal Kaziorcd003fa2013-07-05 16:15:13 +030071 status = ath10k_htc_start(&ar->htc);
Kalle Valo5e3dd152013-06-12 20:52:10 +030072 if (status)
73 goto conn_fail;
74
75 /* Wait for WMI event to be ready */
76 status = ath10k_wmi_wait_for_service_ready(ar);
77 if (status <= 0) {
78 ath10k_warn("wmi service ready event not received");
79 status = -ETIMEDOUT;
80 goto timeout;
81 }
82
Kalle Valoeffea962013-09-08 17:55:44 +030083 ath10k_dbg(ATH10K_DBG_BOOT, "boot wmi ready\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +030084 return 0;
85
86timeout:
Michal Kaziorcd003fa2013-07-05 16:15:13 +030087 ath10k_htc_stop(&ar->htc);
Kalle Valo5e3dd152013-06-12 20:52:10 +030088conn_fail:
89 return status;
90}
91
92static int ath10k_init_configure_target(struct ath10k *ar)
93{
94 u32 param_host;
95 int ret;
96
97 /* tell target which HTC version it is used*/
98 ret = ath10k_bmi_write32(ar, hi_app_host_interest,
99 HTC_PROTOCOL_VERSION);
100 if (ret) {
101 ath10k_err("settings HTC version failed\n");
102 return ret;
103 }
104
105 /* set the firmware mode to STA/IBSS/AP */
106 ret = ath10k_bmi_read32(ar, hi_option_flag, &param_host);
107 if (ret) {
108 ath10k_err("setting firmware mode (1/2) failed\n");
109 return ret;
110 }
111
112 /* TODO following parameters need to be re-visited. */
113 /* num_device */
114 param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
115 /* Firmware mode */
116 /* FIXME: Why FW_MODE_AP ??.*/
117 param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
118 /* mac_addr_method */
119 param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
120 /* firmware_bridge */
121 param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
122 /* fwsubmode */
123 param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
124
125 ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
126 if (ret) {
127 ath10k_err("setting firmware mode (2/2) failed\n");
128 return ret;
129 }
130
131 /* We do all byte-swapping on the host */
132 ret = ath10k_bmi_write32(ar, hi_be, 0);
133 if (ret) {
134 ath10k_err("setting host CPU BE mode failed\n");
135 return ret;
136 }
137
138 /* FW descriptor/Data swap flags */
139 ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
140
141 if (ret) {
142 ath10k_err("setting FW data/desc swap flags failed\n");
143 return ret;
144 }
145
146 return 0;
147}
148
149static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
150 const char *dir,
151 const char *file)
152{
153 char filename[100];
154 const struct firmware *fw;
155 int ret;
156
157 if (file == NULL)
158 return ERR_PTR(-ENOENT);
159
160 if (dir == NULL)
161 dir = ".";
162
163 snprintf(filename, sizeof(filename), "%s/%s", dir, file);
164 ret = request_firmware(&fw, filename, ar->dev);
165 if (ret)
166 return ERR_PTR(ret);
167
168 return fw;
169}
170
Kalle Valo958df3a2013-09-27 19:55:01 +0300171static int ath10k_push_board_ext_data(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300172{
173 u32 board_data_size = QCA988X_BOARD_DATA_SZ;
174 u32 board_ext_data_size = QCA988X_BOARD_EXT_DATA_SZ;
175 u32 board_ext_data_addr;
176 int ret;
177
178 ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
179 if (ret) {
180 ath10k_err("could not read board ext data addr (%d)\n", ret);
181 return ret;
182 }
183
Kalle Valob52b7682013-09-08 17:55:38 +0300184 ath10k_dbg(ATH10K_DBG_BOOT,
Kalle Valoeffea962013-09-08 17:55:44 +0300185 "boot push board extended data addr 0x%x\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300186 board_ext_data_addr);
187
188 if (board_ext_data_addr == 0)
189 return 0;
190
Kalle Valo958df3a2013-09-27 19:55:01 +0300191 if (ar->board_len != (board_data_size + board_ext_data_size)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300192 ath10k_err("invalid board (ext) data sizes %zu != %d+%d\n",
Kalle Valo958df3a2013-09-27 19:55:01 +0300193 ar->board_len, board_data_size, board_ext_data_size);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300194 return -EINVAL;
195 }
196
197 ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
Kalle Valo958df3a2013-09-27 19:55:01 +0300198 ar->board_data + board_data_size,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300199 board_ext_data_size);
200 if (ret) {
201 ath10k_err("could not write board ext data (%d)\n", ret);
202 return ret;
203 }
204
205 ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
206 (board_ext_data_size << 16) | 1);
207 if (ret) {
208 ath10k_err("could not write board ext data bit (%d)\n", ret);
209 return ret;
210 }
211
212 return 0;
213}
214
215static int ath10k_download_board_data(struct ath10k *ar)
216{
217 u32 board_data_size = QCA988X_BOARD_DATA_SZ;
218 u32 address;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300219 int ret;
220
Kalle Valo958df3a2013-09-27 19:55:01 +0300221 ret = ath10k_push_board_ext_data(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300222 if (ret) {
223 ath10k_err("could not push board ext data (%d)\n", ret);
224 goto exit;
225 }
226
227 ret = ath10k_bmi_read32(ar, hi_board_data, &address);
228 if (ret) {
229 ath10k_err("could not read board data addr (%d)\n", ret);
230 goto exit;
231 }
232
Kalle Valo958df3a2013-09-27 19:55:01 +0300233 ret = ath10k_bmi_write_memory(ar, address, ar->board_data,
234 min_t(u32, board_data_size,
235 ar->board_len));
Kalle Valo5e3dd152013-06-12 20:52:10 +0300236 if (ret) {
237 ath10k_err("could not write board data (%d)\n", ret);
238 goto exit;
239 }
240
241 ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
242 if (ret) {
243 ath10k_err("could not write board data bit (%d)\n", ret);
244 goto exit;
245 }
246
247exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300248 return ret;
249}
250
251static int ath10k_download_and_run_otp(struct ath10k *ar)
252{
Michal Kazior29385052013-07-16 09:38:58 +0200253 u32 address = ar->hw_params.patch_load_addr;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300254 u32 exec_param;
255 int ret;
256
257 /* OTP is optional */
258
Kalle Valo958df3a2013-09-27 19:55:01 +0300259 if (!ar->otp_data || !ar->otp_len)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300260 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300261
Kalle Valo958df3a2013-09-27 19:55:01 +0300262 ret = ath10k_bmi_fast_download(ar, address, ar->otp_data, ar->otp_len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300263 if (ret) {
264 ath10k_err("could not write otp (%d)\n", ret);
265 goto exit;
266 }
267
268 exec_param = 0;
269 ret = ath10k_bmi_execute(ar, address, &exec_param);
270 if (ret) {
271 ath10k_err("could not execute otp (%d)\n", ret);
272 goto exit;
273 }
274
275exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300276 return ret;
277}
278
279static int ath10k_download_fw(struct ath10k *ar)
280{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300281 u32 address;
282 int ret;
283
Kalle Valo5e3dd152013-06-12 20:52:10 +0300284 address = ar->hw_params.patch_load_addr;
285
Kalle Valo958df3a2013-09-27 19:55:01 +0300286 ret = ath10k_bmi_fast_download(ar, address, ar->firmware_data,
287 ar->firmware_len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300288 if (ret) {
289 ath10k_err("could not write fw (%d)\n", ret);
290 goto exit;
291 }
292
293exit:
Michal Kazior29385052013-07-16 09:38:58 +0200294 return ret;
295}
296
297static void ath10k_core_free_firmware_files(struct ath10k *ar)
298{
Kalle Valo36527912013-09-27 19:54:55 +0300299 if (ar->board && !IS_ERR(ar->board))
300 release_firmware(ar->board);
Michal Kazior29385052013-07-16 09:38:58 +0200301
302 if (ar->otp && !IS_ERR(ar->otp))
303 release_firmware(ar->otp);
304
305 if (ar->firmware && !IS_ERR(ar->firmware))
306 release_firmware(ar->firmware);
307
Kalle Valo36527912013-09-27 19:54:55 +0300308 ar->board = NULL;
Kalle Valo958df3a2013-09-27 19:55:01 +0300309 ar->board_data = NULL;
310 ar->board_len = 0;
311
Michal Kazior29385052013-07-16 09:38:58 +0200312 ar->otp = NULL;
Kalle Valo958df3a2013-09-27 19:55:01 +0300313 ar->otp_data = NULL;
314 ar->otp_len = 0;
315
Michal Kazior29385052013-07-16 09:38:58 +0200316 ar->firmware = NULL;
Kalle Valo958df3a2013-09-27 19:55:01 +0300317 ar->firmware_data = NULL;
318 ar->firmware_len = 0;
Michal Kazior29385052013-07-16 09:38:58 +0200319}
320
Kalle Valo1a222432013-09-27 19:55:07 +0300321static int ath10k_core_fetch_firmware_api_1(struct ath10k *ar)
Michal Kazior29385052013-07-16 09:38:58 +0200322{
323 int ret = 0;
324
325 if (ar->hw_params.fw.fw == NULL) {
326 ath10k_err("firmware file not defined\n");
327 return -EINVAL;
328 }
329
330 if (ar->hw_params.fw.board == NULL) {
331 ath10k_err("board data file not defined");
332 return -EINVAL;
333 }
334
Kalle Valo36527912013-09-27 19:54:55 +0300335 ar->board = ath10k_fetch_fw_file(ar,
336 ar->hw_params.fw.dir,
337 ar->hw_params.fw.board);
338 if (IS_ERR(ar->board)) {
339 ret = PTR_ERR(ar->board);
Michal Kazior29385052013-07-16 09:38:58 +0200340 ath10k_err("could not fetch board data (%d)\n", ret);
341 goto err;
342 }
343
Kalle Valo958df3a2013-09-27 19:55:01 +0300344 ar->board_data = ar->board->data;
345 ar->board_len = ar->board->size;
346
Michal Kazior29385052013-07-16 09:38:58 +0200347 ar->firmware = ath10k_fetch_fw_file(ar,
348 ar->hw_params.fw.dir,
349 ar->hw_params.fw.fw);
350 if (IS_ERR(ar->firmware)) {
351 ret = PTR_ERR(ar->firmware);
352 ath10k_err("could not fetch firmware (%d)\n", ret);
353 goto err;
354 }
355
Kalle Valo958df3a2013-09-27 19:55:01 +0300356 ar->firmware_data = ar->firmware->data;
357 ar->firmware_len = ar->firmware->size;
358
Michal Kazior29385052013-07-16 09:38:58 +0200359 /* OTP may be undefined. If so, don't fetch it at all */
360 if (ar->hw_params.fw.otp == NULL)
361 return 0;
362
363 ar->otp = ath10k_fetch_fw_file(ar,
364 ar->hw_params.fw.dir,
365 ar->hw_params.fw.otp);
366 if (IS_ERR(ar->otp)) {
367 ret = PTR_ERR(ar->otp);
368 ath10k_err("could not fetch otp (%d)\n", ret);
369 goto err;
370 }
371
Kalle Valo958df3a2013-09-27 19:55:01 +0300372 ar->otp_data = ar->otp->data;
373 ar->otp_len = ar->otp->size;
374
Michal Kazior29385052013-07-16 09:38:58 +0200375 return 0;
376
377err:
378 ath10k_core_free_firmware_files(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300379 return ret;
380}
381
Kalle Valo1a222432013-09-27 19:55:07 +0300382static int ath10k_core_fetch_firmware_api_n(struct ath10k *ar, const char *name)
383{
384 size_t magic_len, len, ie_len;
385 int ie_id, i, index, bit, ret;
386 struct ath10k_fw_ie *hdr;
387 const u8 *data;
388 __le32 *timestamp;
389
390 /* first fetch the firmware file (firmware-*.bin) */
391 ar->firmware = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir, name);
392 if (IS_ERR(ar->firmware)) {
393 ath10k_err("Could not fetch firmware file '%s': %ld\n",
394 name, PTR_ERR(ar->firmware));
395 return PTR_ERR(ar->firmware);
396 }
397
398 data = ar->firmware->data;
399 len = ar->firmware->size;
400
401 /* magic also includes the null byte, check that as well */
402 magic_len = strlen(ATH10K_FIRMWARE_MAGIC) + 1;
403
404 if (len < magic_len) {
405 ath10k_err("firmware image too small to contain magic: %d\n",
406 len);
407 return -EINVAL;
408 }
409
410 if (memcmp(data, ATH10K_FIRMWARE_MAGIC, magic_len) != 0) {
411 ath10k_err("Invalid firmware magic\n");
412 return -EINVAL;
413 }
414
415 /* jump over the padding */
416 magic_len = ALIGN(magic_len, 4);
417
418 len -= magic_len;
419 data += magic_len;
420
421 /* loop elements */
422 while (len > sizeof(struct ath10k_fw_ie)) {
423 hdr = (struct ath10k_fw_ie *)data;
424
425 ie_id = le32_to_cpu(hdr->id);
426 ie_len = le32_to_cpu(hdr->len);
427
428 len -= sizeof(*hdr);
429 data += sizeof(*hdr);
430
431 if (len < ie_len) {
432 ath10k_err("Invalid length for FW IE %d (%d < %d)\n",
433 ie_id, len, ie_len);
434 return -EINVAL;
435 }
436
437 switch (ie_id) {
438 case ATH10K_FW_IE_FW_VERSION:
439 if (ie_len > sizeof(ar->hw->wiphy->fw_version) - 1)
440 break;
441
442 memcpy(ar->hw->wiphy->fw_version, data, ie_len);
443 ar->hw->wiphy->fw_version[ie_len] = '\0';
444
445 ath10k_dbg(ATH10K_DBG_BOOT,
446 "found fw version %s\n",
447 ar->hw->wiphy->fw_version);
448 break;
449 case ATH10K_FW_IE_TIMESTAMP:
450 if (ie_len != sizeof(u32))
451 break;
452
453 timestamp = (__le32 *)data;
454
455 ath10k_dbg(ATH10K_DBG_BOOT, "found fw timestamp %d\n",
456 le32_to_cpup(timestamp));
457 break;
458 case ATH10K_FW_IE_FEATURES:
459 ath10k_dbg(ATH10K_DBG_BOOT,
460 "found firmware features ie (%zd B)\n",
461 ie_len);
462
463 for (i = 0; i < ATH10K_FW_FEATURE_COUNT; i++) {
464 index = i / 8;
465 bit = i % 8;
466
467 if (index == ie_len)
468 break;
469
470 if (data[index] & (1 << bit))
471 __set_bit(i, ar->fw_features);
472 }
473
474 ath10k_dbg_dump(ATH10K_DBG_BOOT, "features", "",
475 ar->fw_features,
476 sizeof(ar->fw_features));
477 break;
478 case ATH10K_FW_IE_FW_IMAGE:
479 ath10k_dbg(ATH10K_DBG_BOOT,
480 "found fw image ie (%zd B)\n",
481 ie_len);
482
483 ar->firmware_data = data;
484 ar->firmware_len = ie_len;
485
486 break;
487 case ATH10K_FW_IE_OTP_IMAGE:
488 ath10k_dbg(ATH10K_DBG_BOOT,
489 "found otp image ie (%zd B)\n",
490 ie_len);
491
492 ar->otp_data = data;
493 ar->otp_len = ie_len;
494
495 break;
496 default:
497 ath10k_warn("Unknown FW IE: %u\n",
498 le32_to_cpu(hdr->id));
499 break;
500 }
501
502 /* jump over the padding */
503 ie_len = ALIGN(ie_len, 4);
504
505 len -= ie_len;
506 data += ie_len;
507 };
508
509 if (!ar->firmware_data || !ar->firmware_len) {
510 ath10k_warn("No ATH10K_FW_IE_FW_IMAGE found from %s, skipping\n",
511 name);
512 ret = -ENOMEDIUM;
513 goto err;
514 }
515
516 /* now fetch the board file */
517 if (ar->hw_params.fw.board == NULL) {
518 ath10k_err("board data file not defined");
519 ret = -EINVAL;
520 goto err;
521 }
522
523 ar->board = ath10k_fetch_fw_file(ar,
524 ar->hw_params.fw.dir,
525 ar->hw_params.fw.board);
526 if (IS_ERR(ar->board)) {
527 ret = PTR_ERR(ar->board);
528 ath10k_err("could not fetch board data (%d)\n", ret);
529 goto err;
530 }
531
532 ar->board_data = ar->board->data;
533 ar->board_len = ar->board->size;
534
535 return 0;
536
537err:
538 ath10k_core_free_firmware_files(ar);
539 return ret;
540}
541
542static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
543{
544 int ret;
545
546 ret = ath10k_core_fetch_firmware_api_n(ar, ATH10K_FW_API2_FILE);
547 if (ret == 0) {
548 ar->fw_api = 2;
549 goto out;
550 }
551
552 ret = ath10k_core_fetch_firmware_api_1(ar);
553 if (ret)
554 return ret;
555
556 ar->fw_api = 1;
557
558out:
559 ath10k_dbg(ATH10K_DBG_BOOT, "using fw api %d\n", ar->fw_api);
560
561 return 0;
562}
563
Kalle Valo5e3dd152013-06-12 20:52:10 +0300564static int ath10k_init_download_firmware(struct ath10k *ar)
565{
566 int ret;
567
568 ret = ath10k_download_board_data(ar);
569 if (ret)
570 return ret;
571
572 ret = ath10k_download_and_run_otp(ar);
573 if (ret)
574 return ret;
575
576 ret = ath10k_download_fw(ar);
577 if (ret)
578 return ret;
579
580 return ret;
581}
582
583static int ath10k_init_uart(struct ath10k *ar)
584{
585 int ret;
586
587 /*
588 * Explicitly setting UART prints to zero as target turns it on
589 * based on scratch registers.
590 */
591 ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
592 if (ret) {
593 ath10k_warn("could not disable UART prints (%d)\n", ret);
594 return ret;
595 }
596
597 if (!uart_print) {
598 ath10k_info("UART prints disabled\n");
599 return 0;
600 }
601
602 ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, 7);
603 if (ret) {
604 ath10k_warn("could not enable UART prints (%d)\n", ret);
605 return ret;
606 }
607
608 ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
609 if (ret) {
610 ath10k_warn("could not enable UART prints (%d)\n", ret);
611 return ret;
612 }
613
Bartosz Markowski03fc1372013-09-03 14:24:02 +0200614 /* Set the UART baud rate to 19200. */
615 ret = ath10k_bmi_write32(ar, hi_desired_baud_rate, 19200);
616 if (ret) {
617 ath10k_warn("could not set the baud rate (%d)\n", ret);
618 return ret;
619 }
620
Kalle Valo5e3dd152013-06-12 20:52:10 +0300621 ath10k_info("UART prints enabled\n");
622 return 0;
623}
624
625static int ath10k_init_hw_params(struct ath10k *ar)
626{
627 const struct ath10k_hw_params *uninitialized_var(hw_params);
628 int i;
629
630 for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
631 hw_params = &ath10k_hw_params_list[i];
632
633 if (hw_params->id == ar->target_version)
634 break;
635 }
636
637 if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
638 ath10k_err("Unsupported hardware version: 0x%x\n",
639 ar->target_version);
640 return -EINVAL;
641 }
642
643 ar->hw_params = *hw_params;
644
645 ath10k_info("Hardware name %s version 0x%x\n",
646 ar->hw_params.name, ar->target_version);
647
648 return 0;
649}
650
Michal Kazioraffd3212013-07-16 09:54:35 +0200651static void ath10k_core_restart(struct work_struct *work)
652{
653 struct ath10k *ar = container_of(work, struct ath10k, restart_work);
654
655 mutex_lock(&ar->conf_mutex);
656
657 switch (ar->state) {
658 case ATH10K_STATE_ON:
659 ath10k_halt(ar);
660 ar->state = ATH10K_STATE_RESTARTING;
661 ieee80211_restart_hw(ar->hw);
662 break;
663 case ATH10K_STATE_OFF:
664 /* this can happen if driver is being unloaded */
665 ath10k_warn("cannot restart a device that hasn't been started\n");
666 break;
667 case ATH10K_STATE_RESTARTING:
668 case ATH10K_STATE_RESTARTED:
669 ar->state = ATH10K_STATE_WEDGED;
670 /* fall through */
671 case ATH10K_STATE_WEDGED:
672 ath10k_warn("device is wedged, will not restart\n");
673 break;
674 }
675
676 mutex_unlock(&ar->conf_mutex);
677}
678
Kalle Valo5e3dd152013-06-12 20:52:10 +0300679struct ath10k *ath10k_core_create(void *hif_priv, struct device *dev,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300680 const struct ath10k_hif_ops *hif_ops)
681{
682 struct ath10k *ar;
683
684 ar = ath10k_mac_create();
685 if (!ar)
686 return NULL;
687
688 ar->ath_common.priv = ar;
689 ar->ath_common.hw = ar->hw;
690
691 ar->p2p = !!ath10k_p2p;
692 ar->dev = dev;
693
694 ar->hif.priv = hif_priv;
695 ar->hif.ops = hif_ops;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300696
Kalle Valo5e3dd152013-06-12 20:52:10 +0300697 init_completion(&ar->scan.started);
698 init_completion(&ar->scan.completed);
699 init_completion(&ar->scan.on_channel);
700
701 init_completion(&ar->install_key_done);
702 init_completion(&ar->vdev_setup_done);
703
704 setup_timer(&ar->scan.timeout, ath10k_reset_scan, (unsigned long)ar);
705
706 ar->workqueue = create_singlethread_workqueue("ath10k_wq");
707 if (!ar->workqueue)
708 goto err_wq;
709
710 mutex_init(&ar->conf_mutex);
711 spin_lock_init(&ar->data_lock);
712
713 INIT_LIST_HEAD(&ar->peers);
714 init_waitqueue_head(&ar->peer_mapping_wq);
715
716 init_completion(&ar->offchan_tx_completed);
717 INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
718 skb_queue_head_init(&ar->offchan_tx_queue);
719
Bartosz Markowski5e00d312013-09-26 17:47:12 +0200720 INIT_WORK(&ar->wmi_mgmt_tx_work, ath10k_mgmt_over_wmi_tx_work);
721 skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
722
Kalle Valo5e3dd152013-06-12 20:52:10 +0300723 init_waitqueue_head(&ar->event_queue);
724
Michal Kazioraffd3212013-07-16 09:54:35 +0200725 INIT_WORK(&ar->restart_work, ath10k_core_restart);
726
Kalle Valo5e3dd152013-06-12 20:52:10 +0300727 return ar;
728
729err_wq:
730 ath10k_mac_destroy(ar);
731 return NULL;
732}
733EXPORT_SYMBOL(ath10k_core_create);
734
735void ath10k_core_destroy(struct ath10k *ar)
736{
737 flush_workqueue(ar->workqueue);
738 destroy_workqueue(ar->workqueue);
739
740 ath10k_mac_destroy(ar);
741}
742EXPORT_SYMBOL(ath10k_core_destroy);
743
Michal Kaziordd30a362013-07-16 09:38:51 +0200744int ath10k_core_start(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300745{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300746 int status;
747
Michal Kazior64d151d2013-07-16 09:38:53 +0200748 ath10k_bmi_start(ar);
749
Kalle Valo5e3dd152013-06-12 20:52:10 +0300750 if (ath10k_init_configure_target(ar)) {
751 status = -EINVAL;
752 goto err;
753 }
754
755 status = ath10k_init_download_firmware(ar);
756 if (status)
757 goto err;
758
759 status = ath10k_init_uart(ar);
760 if (status)
761 goto err;
762
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300763 ar->htc.htc_ops.target_send_suspend_complete =
764 ath10k_send_suspend_complete;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300765
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300766 status = ath10k_htc_init(ar);
767 if (status) {
768 ath10k_err("could not init HTC (%d)\n", status);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300769 goto err;
770 }
771
772 status = ath10k_bmi_done(ar);
773 if (status)
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300774 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300775
776 status = ath10k_wmi_attach(ar);
777 if (status) {
778 ath10k_err("WMI attach failed: %d\n", status);
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300779 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300780 }
781
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300782 status = ath10k_htc_wait_target(&ar->htc);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300783 if (status)
784 goto err_wmi_detach;
785
Michal Kazioredb82362013-07-05 16:15:14 +0300786 status = ath10k_htt_attach(ar);
787 if (status) {
788 ath10k_err("could not attach htt (%d)\n", status);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300789 goto err_wmi_detach;
790 }
791
792 status = ath10k_init_connect_htc(ar);
793 if (status)
794 goto err_htt_detach;
795
796 ath10k_info("firmware %s booted\n", ar->hw->wiphy->fw_version);
797
Kalle Valo5e3dd152013-06-12 20:52:10 +0300798 status = ath10k_wmi_cmd_init(ar);
799 if (status) {
800 ath10k_err("could not send WMI init command (%d)\n", status);
801 goto err_disconnect_htc;
802 }
803
804 status = ath10k_wmi_wait_for_unified_ready(ar);
805 if (status <= 0) {
806 ath10k_err("wmi unified ready event not received\n");
807 status = -ETIMEDOUT;
808 goto err_disconnect_htc;
809 }
810
Michal Kazioredb82362013-07-05 16:15:14 +0300811 status = ath10k_htt_attach_target(&ar->htt);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300812 if (status)
813 goto err_disconnect_htc;
814
Kalle Valodb66ea02013-09-03 11:44:03 +0300815 status = ath10k_debug_start(ar);
816 if (status)
817 goto err_disconnect_htc;
818
Michal Kazior1a1b8a82013-07-16 09:38:55 +0200819 ar->free_vdev_map = (1 << TARGET_NUM_VDEVS) - 1;
820
Michal Kaziordd30a362013-07-16 09:38:51 +0200821 return 0;
822
823err_disconnect_htc:
824 ath10k_htc_stop(&ar->htc);
825err_htt_detach:
826 ath10k_htt_detach(&ar->htt);
827err_wmi_detach:
828 ath10k_wmi_detach(ar);
829err:
830 return status;
831}
Michal Kazior818bdd12013-07-16 09:38:57 +0200832EXPORT_SYMBOL(ath10k_core_start);
Michal Kaziordd30a362013-07-16 09:38:51 +0200833
834void ath10k_core_stop(struct ath10k *ar)
835{
Kalle Valodb66ea02013-09-03 11:44:03 +0300836 ath10k_debug_stop(ar);
Michal Kaziordd30a362013-07-16 09:38:51 +0200837 ath10k_htc_stop(&ar->htc);
838 ath10k_htt_detach(&ar->htt);
839 ath10k_wmi_detach(ar);
840}
Michal Kazior818bdd12013-07-16 09:38:57 +0200841EXPORT_SYMBOL(ath10k_core_stop);
842
843/* mac80211 manages fw/hw initialization through start/stop hooks. However in
844 * order to know what hw capabilities should be advertised to mac80211 it is
845 * necessary to load the firmware (and tear it down immediately since start
846 * hook will try to init it again) before registering */
847static int ath10k_core_probe_fw(struct ath10k *ar)
848{
Michal Kazior29385052013-07-16 09:38:58 +0200849 struct bmi_target_info target_info;
850 int ret = 0;
Michal Kazior818bdd12013-07-16 09:38:57 +0200851
852 ret = ath10k_hif_power_up(ar);
853 if (ret) {
854 ath10k_err("could not start pci hif (%d)\n", ret);
855 return ret;
856 }
857
Michal Kazior29385052013-07-16 09:38:58 +0200858 memset(&target_info, 0, sizeof(target_info));
859 ret = ath10k_bmi_get_target_info(ar, &target_info);
860 if (ret) {
861 ath10k_err("could not get target info (%d)\n", ret);
862 ath10k_hif_power_down(ar);
863 return ret;
864 }
865
866 ar->target_version = target_info.version;
867 ar->hw->wiphy->hw_version = target_info.version;
868
869 ret = ath10k_init_hw_params(ar);
870 if (ret) {
871 ath10k_err("could not get hw params (%d)\n", ret);
872 ath10k_hif_power_down(ar);
873 return ret;
874 }
875
876 ret = ath10k_core_fetch_firmware_files(ar);
877 if (ret) {
878 ath10k_err("could not fetch firmware files (%d)\n", ret);
879 ath10k_hif_power_down(ar);
880 return ret;
881 }
882
Michal Kazior818bdd12013-07-16 09:38:57 +0200883 ret = ath10k_core_start(ar);
884 if (ret) {
885 ath10k_err("could not init core (%d)\n", ret);
Michal Kazior29385052013-07-16 09:38:58 +0200886 ath10k_core_free_firmware_files(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +0200887 ath10k_hif_power_down(ar);
888 return ret;
889 }
890
891 ath10k_core_stop(ar);
892 ath10k_hif_power_down(ar);
893 return 0;
894}
Michal Kaziordd30a362013-07-16 09:38:51 +0200895
Kalle Valoe01ae682013-09-01 11:22:14 +0300896static int ath10k_core_check_chip_id(struct ath10k *ar)
897{
898 u32 hw_revision = MS(ar->chip_id, SOC_CHIP_ID_REV);
899
Kalle Valoeffea962013-09-08 17:55:44 +0300900 ath10k_dbg(ATH10K_DBG_BOOT, "boot chip_id 0x%08x hw_revision 0x%x\n",
901 ar->chip_id, hw_revision);
902
Kalle Valoe01ae682013-09-01 11:22:14 +0300903 /* Check that we are not using hw1.0 (some of them have same pci id
904 * as hw2.0) before doing anything else as ath10k crashes horribly
905 * due to missing hw1.0 workarounds. */
906 switch (hw_revision) {
907 case QCA988X_HW_1_0_CHIP_ID_REV:
908 ath10k_err("ERROR: qca988x hw1.0 is not supported\n");
909 return -EOPNOTSUPP;
910
911 case QCA988X_HW_2_0_CHIP_ID_REV:
912 /* known hardware revision, continue normally */
913 return 0;
914
915 default:
916 ath10k_warn("Warning: hardware revision unknown (0x%x), expect problems\n",
917 ar->chip_id);
918 return 0;
919 }
920
921 return 0;
922}
923
924int ath10k_core_register(struct ath10k *ar, u32 chip_id)
Michal Kaziordd30a362013-07-16 09:38:51 +0200925{
926 int status;
927
Kalle Valoe01ae682013-09-01 11:22:14 +0300928 ar->chip_id = chip_id;
929
930 status = ath10k_core_check_chip_id(ar);
931 if (status) {
932 ath10k_err("Unsupported chip id 0x%08x\n", ar->chip_id);
933 return status;
934 }
935
Michal Kazior818bdd12013-07-16 09:38:57 +0200936 status = ath10k_core_probe_fw(ar);
937 if (status) {
938 ath10k_err("could not probe fw (%d)\n", status);
939 return status;
940 }
Michal Kaziordd30a362013-07-16 09:38:51 +0200941
Kalle Valo5e3dd152013-06-12 20:52:10 +0300942 status = ath10k_mac_register(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +0200943 if (status) {
944 ath10k_err("could not register to mac80211 (%d)\n", status);
Michal Kazior29385052013-07-16 09:38:58 +0200945 goto err_release_fw;
Michal Kazior818bdd12013-07-16 09:38:57 +0200946 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300947
948 status = ath10k_debug_create(ar);
949 if (status) {
950 ath10k_err("unable to initialize debugfs\n");
951 goto err_unregister_mac;
952 }
953
954 return 0;
955
956err_unregister_mac:
957 ath10k_mac_unregister(ar);
Michal Kazior29385052013-07-16 09:38:58 +0200958err_release_fw:
959 ath10k_core_free_firmware_files(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300960 return status;
961}
962EXPORT_SYMBOL(ath10k_core_register);
963
964void ath10k_core_unregister(struct ath10k *ar)
965{
966 /* We must unregister from mac80211 before we stop HTC and HIF.
967 * Otherwise we will fail to submit commands to FW and mac80211 will be
968 * unhappy about callback failures. */
969 ath10k_mac_unregister(ar);
Kalle Valodb66ea02013-09-03 11:44:03 +0300970
Michal Kazior29385052013-07-16 09:38:58 +0200971 ath10k_core_free_firmware_files(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300972}
973EXPORT_SYMBOL(ath10k_core_unregister);
974
Kalle Valo5e3dd152013-06-12 20:52:10 +0300975MODULE_AUTHOR("Qualcomm Atheros");
976MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
977MODULE_LICENSE("Dual BSD/GPL");