ath10k: decouple suspend code
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / core.c
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <linux/module.h>
19 #include <linux/firmware.h>
20
21 #include "core.h"
22 #include "mac.h"
23 #include "htc.h"
24 #include "hif.h"
25 #include "wmi.h"
26 #include "bmi.h"
27 #include "debug.h"
28 #include "htt.h"
29
30 unsigned int ath10k_debug_mask;
31 static bool uart_print;
32 static unsigned int ath10k_p2p;
33 module_param_named(debug_mask, ath10k_debug_mask, uint, 0644);
34 module_param(uart_print, bool, 0644);
35 module_param_named(p2p, ath10k_p2p, uint, 0644);
36 MODULE_PARM_DESC(debug_mask, "Debugging mask");
37 MODULE_PARM_DESC(uart_print, "Uart target debugging");
38 MODULE_PARM_DESC(p2p, "Enable ath10k P2P support");
39
40 static const struct ath10k_hw_params ath10k_hw_params_list[] = {
41         {
42                 .id = QCA988X_HW_1_0_VERSION,
43                 .name = "qca988x hw1.0",
44                 .patch_load_addr = QCA988X_HW_1_0_PATCH_LOAD_ADDR,
45                 .fw = {
46                         .dir = QCA988X_HW_1_0_FW_DIR,
47                         .fw = QCA988X_HW_1_0_FW_FILE,
48                         .otp = QCA988X_HW_1_0_OTP_FILE,
49                         .board = QCA988X_HW_1_0_BOARD_DATA_FILE,
50                 },
51         },
52         {
53                 .id = QCA988X_HW_2_0_VERSION,
54                 .name = "qca988x hw2.0",
55                 .patch_load_addr = QCA988X_HW_2_0_PATCH_LOAD_ADDR,
56                 .fw = {
57                         .dir = QCA988X_HW_2_0_FW_DIR,
58                         .fw = QCA988X_HW_2_0_FW_FILE,
59                         .otp = QCA988X_HW_2_0_OTP_FILE,
60                         .board = QCA988X_HW_2_0_BOARD_DATA_FILE,
61                 },
62         },
63 };
64
65 static void ath10k_send_suspend_complete(struct ath10k *ar)
66 {
67         ath10k_dbg(ATH10K_DBG_CORE, "%s\n", __func__);
68
69         ar->is_target_paused = true;
70         wake_up(&ar->event_queue);
71 }
72
73 static int ath10k_check_fw_version(struct ath10k *ar)
74 {
75         char version[32];
76
77         if (ar->fw_version_major >= SUPPORTED_FW_MAJOR &&
78             ar->fw_version_minor >= SUPPORTED_FW_MINOR &&
79             ar->fw_version_release >= SUPPORTED_FW_RELEASE &&
80             ar->fw_version_build >= SUPPORTED_FW_BUILD)
81                 return 0;
82
83         snprintf(version, sizeof(version), "%u.%u.%u.%u",
84                  SUPPORTED_FW_MAJOR, SUPPORTED_FW_MINOR,
85                  SUPPORTED_FW_RELEASE, SUPPORTED_FW_BUILD);
86
87         ath10k_warn("WARNING: Firmware version %s is not officially supported.\n",
88                     ar->hw->wiphy->fw_version);
89         ath10k_warn("Please upgrade to version %s (or newer)\n", version);
90
91         return 0;
92 }
93
94 static int ath10k_init_connect_htc(struct ath10k *ar)
95 {
96         int status;
97
98         status = ath10k_wmi_connect_htc_service(ar);
99         if (status)
100                 goto conn_fail;
101
102         /* Start HTC */
103         status = ath10k_htc_start(&ar->htc);
104         if (status)
105                 goto conn_fail;
106
107         /* Wait for WMI event to be ready */
108         status = ath10k_wmi_wait_for_service_ready(ar);
109         if (status <= 0) {
110                 ath10k_warn("wmi service ready event not received");
111                 status = -ETIMEDOUT;
112                 goto timeout;
113         }
114
115         ath10k_dbg(ATH10K_DBG_CORE, "core wmi ready\n");
116         return 0;
117
118 timeout:
119         ath10k_htc_stop(&ar->htc);
120 conn_fail:
121         return status;
122 }
123
124 static int ath10k_init_configure_target(struct ath10k *ar)
125 {
126         u32 param_host;
127         int ret;
128
129         /* tell target which HTC version it is used*/
130         ret = ath10k_bmi_write32(ar, hi_app_host_interest,
131                                  HTC_PROTOCOL_VERSION);
132         if (ret) {
133                 ath10k_err("settings HTC version failed\n");
134                 return ret;
135         }
136
137         /* set the firmware mode to STA/IBSS/AP */
138         ret = ath10k_bmi_read32(ar, hi_option_flag, &param_host);
139         if (ret) {
140                 ath10k_err("setting firmware mode (1/2) failed\n");
141                 return ret;
142         }
143
144         /* TODO following parameters need to be re-visited. */
145         /* num_device */
146         param_host |= (1 << HI_OPTION_NUM_DEV_SHIFT);
147         /* Firmware mode */
148         /* FIXME: Why FW_MODE_AP ??.*/
149         param_host |= (HI_OPTION_FW_MODE_AP << HI_OPTION_FW_MODE_SHIFT);
150         /* mac_addr_method */
151         param_host |= (1 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
152         /* firmware_bridge */
153         param_host |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
154         /* fwsubmode */
155         param_host |= (0 << HI_OPTION_FW_SUBMODE_SHIFT);
156
157         ret = ath10k_bmi_write32(ar, hi_option_flag, param_host);
158         if (ret) {
159                 ath10k_err("setting firmware mode (2/2) failed\n");
160                 return ret;
161         }
162
163         /* We do all byte-swapping on the host */
164         ret = ath10k_bmi_write32(ar, hi_be, 0);
165         if (ret) {
166                 ath10k_err("setting host CPU BE mode failed\n");
167                 return ret;
168         }
169
170         /* FW descriptor/Data swap flags */
171         ret = ath10k_bmi_write32(ar, hi_fw_swap, 0);
172
173         if (ret) {
174                 ath10k_err("setting FW data/desc swap flags failed\n");
175                 return ret;
176         }
177
178         return 0;
179 }
180
181 static const struct firmware *ath10k_fetch_fw_file(struct ath10k *ar,
182                                                    const char *dir,
183                                                    const char *file)
184 {
185         char filename[100];
186         const struct firmware *fw;
187         int ret;
188
189         if (file == NULL)
190                 return ERR_PTR(-ENOENT);
191
192         if (dir == NULL)
193                 dir = ".";
194
195         snprintf(filename, sizeof(filename), "%s/%s", dir, file);
196         ret = request_firmware(&fw, filename, ar->dev);
197         if (ret)
198                 return ERR_PTR(ret);
199
200         return fw;
201 }
202
203 static int ath10k_push_board_ext_data(struct ath10k *ar,
204                                       const struct firmware *fw)
205 {
206         u32 board_data_size = QCA988X_BOARD_DATA_SZ;
207         u32 board_ext_data_size = QCA988X_BOARD_EXT_DATA_SZ;
208         u32 board_ext_data_addr;
209         int ret;
210
211         ret = ath10k_bmi_read32(ar, hi_board_ext_data, &board_ext_data_addr);
212         if (ret) {
213                 ath10k_err("could not read board ext data addr (%d)\n", ret);
214                 return ret;
215         }
216
217         ath10k_dbg(ATH10K_DBG_CORE,
218                    "ath10k: Board extended Data download addr: 0x%x\n",
219                    board_ext_data_addr);
220
221         if (board_ext_data_addr == 0)
222                 return 0;
223
224         if (fw->size != (board_data_size + board_ext_data_size)) {
225                 ath10k_err("invalid board (ext) data sizes %zu != %d+%d\n",
226                            fw->size, board_data_size, board_ext_data_size);
227                 return -EINVAL;
228         }
229
230         ret = ath10k_bmi_write_memory(ar, board_ext_data_addr,
231                                       fw->data + board_data_size,
232                                       board_ext_data_size);
233         if (ret) {
234                 ath10k_err("could not write board ext data (%d)\n", ret);
235                 return ret;
236         }
237
238         ret = ath10k_bmi_write32(ar, hi_board_ext_data_config,
239                                  (board_ext_data_size << 16) | 1);
240         if (ret) {
241                 ath10k_err("could not write board ext data bit (%d)\n", ret);
242                 return ret;
243         }
244
245         return 0;
246 }
247
248 static int ath10k_download_board_data(struct ath10k *ar)
249 {
250         u32 board_data_size = QCA988X_BOARD_DATA_SZ;
251         u32 address;
252         const struct firmware *fw;
253         int ret;
254
255         fw = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir,
256                                   ar->hw_params.fw.board);
257         if (IS_ERR(fw)) {
258                 ath10k_err("could not fetch board data fw file (%ld)\n",
259                            PTR_ERR(fw));
260                 return PTR_ERR(fw);
261         }
262
263         ret = ath10k_push_board_ext_data(ar, fw);
264         if (ret) {
265                 ath10k_err("could not push board ext data (%d)\n", ret);
266                 goto exit;
267         }
268
269         ret = ath10k_bmi_read32(ar, hi_board_data, &address);
270         if (ret) {
271                 ath10k_err("could not read board data addr (%d)\n", ret);
272                 goto exit;
273         }
274
275         ret = ath10k_bmi_write_memory(ar, address, fw->data,
276                                       min_t(u32, board_data_size, fw->size));
277         if (ret) {
278                 ath10k_err("could not write board data (%d)\n", ret);
279                 goto exit;
280         }
281
282         ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
283         if (ret) {
284                 ath10k_err("could not write board data bit (%d)\n", ret);
285                 goto exit;
286         }
287
288 exit:
289         release_firmware(fw);
290         return ret;
291 }
292
293 static int ath10k_download_and_run_otp(struct ath10k *ar)
294 {
295         const struct firmware *fw;
296         u32 address;
297         u32 exec_param;
298         int ret;
299
300         /* OTP is optional */
301
302         if (ar->hw_params.fw.otp == NULL) {
303                 ath10k_info("otp file not defined\n");
304                 return 0;
305         }
306
307         address = ar->hw_params.patch_load_addr;
308
309         fw = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir,
310                                   ar->hw_params.fw.otp);
311         if (IS_ERR(fw)) {
312                 ath10k_warn("could not fetch otp (%ld)\n", PTR_ERR(fw));
313                 return 0;
314         }
315
316         ret = ath10k_bmi_fast_download(ar, address, fw->data, fw->size);
317         if (ret) {
318                 ath10k_err("could not write otp (%d)\n", ret);
319                 goto exit;
320         }
321
322         exec_param = 0;
323         ret = ath10k_bmi_execute(ar, address, &exec_param);
324         if (ret) {
325                 ath10k_err("could not execute otp (%d)\n", ret);
326                 goto exit;
327         }
328
329 exit:
330         release_firmware(fw);
331         return ret;
332 }
333
334 static int ath10k_download_fw(struct ath10k *ar)
335 {
336         const struct firmware *fw;
337         u32 address;
338         int ret;
339
340         if (ar->hw_params.fw.fw == NULL)
341                 return -EINVAL;
342
343         address = ar->hw_params.patch_load_addr;
344
345         fw = ath10k_fetch_fw_file(ar, ar->hw_params.fw.dir,
346                                   ar->hw_params.fw.fw);
347         if (IS_ERR(fw)) {
348                 ath10k_err("could not fetch fw (%ld)\n", PTR_ERR(fw));
349                 return PTR_ERR(fw);
350         }
351
352         ret = ath10k_bmi_fast_download(ar, address, fw->data, fw->size);
353         if (ret) {
354                 ath10k_err("could not write fw (%d)\n", ret);
355                 goto exit;
356         }
357
358 exit:
359         release_firmware(fw);
360         return ret;
361 }
362
363 static int ath10k_init_download_firmware(struct ath10k *ar)
364 {
365         int ret;
366
367         ret = ath10k_download_board_data(ar);
368         if (ret)
369                 return ret;
370
371         ret = ath10k_download_and_run_otp(ar);
372         if (ret)
373                 return ret;
374
375         ret = ath10k_download_fw(ar);
376         if (ret)
377                 return ret;
378
379         return ret;
380 }
381
382 static int ath10k_init_uart(struct ath10k *ar)
383 {
384         int ret;
385
386         /*
387          * Explicitly setting UART prints to zero as target turns it on
388          * based on scratch registers.
389          */
390         ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
391         if (ret) {
392                 ath10k_warn("could not disable UART prints (%d)\n", ret);
393                 return ret;
394         }
395
396         if (!uart_print) {
397                 ath10k_info("UART prints disabled\n");
398                 return 0;
399         }
400
401         ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, 7);
402         if (ret) {
403                 ath10k_warn("could not enable UART prints (%d)\n", ret);
404                 return ret;
405         }
406
407         ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
408         if (ret) {
409                 ath10k_warn("could not enable UART prints (%d)\n", ret);
410                 return ret;
411         }
412
413         ath10k_info("UART prints enabled\n");
414         return 0;
415 }
416
417 static int ath10k_init_hw_params(struct ath10k *ar)
418 {
419         const struct ath10k_hw_params *uninitialized_var(hw_params);
420         int i;
421
422         for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
423                 hw_params = &ath10k_hw_params_list[i];
424
425                 if (hw_params->id == ar->target_version)
426                         break;
427         }
428
429         if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
430                 ath10k_err("Unsupported hardware version: 0x%x\n",
431                            ar->target_version);
432                 return -EINVAL;
433         }
434
435         ar->hw_params = *hw_params;
436
437         ath10k_info("Hardware name %s version 0x%x\n",
438                     ar->hw_params.name, ar->target_version);
439
440         return 0;
441 }
442
443 struct ath10k *ath10k_core_create(void *hif_priv, struct device *dev,
444                                   const struct ath10k_hif_ops *hif_ops)
445 {
446         struct ath10k *ar;
447
448         ar = ath10k_mac_create();
449         if (!ar)
450                 return NULL;
451
452         ar->ath_common.priv = ar;
453         ar->ath_common.hw = ar->hw;
454
455         ar->p2p = !!ath10k_p2p;
456         ar->dev = dev;
457
458         ar->hif.priv = hif_priv;
459         ar->hif.ops = hif_ops;
460
461         ar->free_vdev_map = 0xFF; /* 8 vdevs */
462
463         init_completion(&ar->scan.started);
464         init_completion(&ar->scan.completed);
465         init_completion(&ar->scan.on_channel);
466
467         init_completion(&ar->install_key_done);
468         init_completion(&ar->vdev_setup_done);
469
470         setup_timer(&ar->scan.timeout, ath10k_reset_scan, (unsigned long)ar);
471
472         ar->workqueue = create_singlethread_workqueue("ath10k_wq");
473         if (!ar->workqueue)
474                 goto err_wq;
475
476         mutex_init(&ar->conf_mutex);
477         spin_lock_init(&ar->data_lock);
478
479         INIT_LIST_HEAD(&ar->peers);
480         init_waitqueue_head(&ar->peer_mapping_wq);
481
482         init_completion(&ar->offchan_tx_completed);
483         INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
484         skb_queue_head_init(&ar->offchan_tx_queue);
485
486         init_waitqueue_head(&ar->event_queue);
487
488         return ar;
489
490 err_wq:
491         ath10k_mac_destroy(ar);
492         return NULL;
493 }
494 EXPORT_SYMBOL(ath10k_core_create);
495
496 void ath10k_core_destroy(struct ath10k *ar)
497 {
498         flush_workqueue(ar->workqueue);
499         destroy_workqueue(ar->workqueue);
500
501         ath10k_mac_destroy(ar);
502 }
503 EXPORT_SYMBOL(ath10k_core_destroy);
504
505 int ath10k_core_start(struct ath10k *ar)
506 {
507         struct bmi_target_info target_info;
508         int status;
509
510         ath10k_bmi_start(ar);
511
512         memset(&target_info, 0, sizeof(target_info));
513         status = ath10k_bmi_get_target_info(ar, &target_info);
514         if (status)
515                 goto err;
516
517         ar->target_version = target_info.version;
518         ar->hw->wiphy->hw_version = target_info.version;
519
520         status = ath10k_init_hw_params(ar);
521         if (status)
522                 goto err;
523
524         if (ath10k_init_configure_target(ar)) {
525                 status = -EINVAL;
526                 goto err;
527         }
528
529         status = ath10k_init_download_firmware(ar);
530         if (status)
531                 goto err;
532
533         status = ath10k_init_uart(ar);
534         if (status)
535                 goto err;
536
537         ar->htc.htc_ops.target_send_suspend_complete =
538                 ath10k_send_suspend_complete;
539
540         status = ath10k_htc_init(ar);
541         if (status) {
542                 ath10k_err("could not init HTC (%d)\n", status);
543                 goto err;
544         }
545
546         status = ath10k_bmi_done(ar);
547         if (status)
548                 goto err;
549
550         status = ath10k_wmi_attach(ar);
551         if (status) {
552                 ath10k_err("WMI attach failed: %d\n", status);
553                 goto err;
554         }
555
556         status = ath10k_htc_wait_target(&ar->htc);
557         if (status)
558                 goto err_wmi_detach;
559
560         status = ath10k_htt_attach(ar);
561         if (status) {
562                 ath10k_err("could not attach htt (%d)\n", status);
563                 goto err_wmi_detach;
564         }
565
566         status = ath10k_init_connect_htc(ar);
567         if (status)
568                 goto err_htt_detach;
569
570         ath10k_info("firmware %s booted\n", ar->hw->wiphy->fw_version);
571
572         status = ath10k_check_fw_version(ar);
573         if (status)
574                 goto err_disconnect_htc;
575
576         status = ath10k_wmi_cmd_init(ar);
577         if (status) {
578                 ath10k_err("could not send WMI init command (%d)\n", status);
579                 goto err_disconnect_htc;
580         }
581
582         status = ath10k_wmi_wait_for_unified_ready(ar);
583         if (status <= 0) {
584                 ath10k_err("wmi unified ready event not received\n");
585                 status = -ETIMEDOUT;
586                 goto err_disconnect_htc;
587         }
588
589         status = ath10k_htt_attach_target(&ar->htt);
590         if (status)
591                 goto err_disconnect_htc;
592
593         return 0;
594
595 err_disconnect_htc:
596         ath10k_htc_stop(&ar->htc);
597 err_htt_detach:
598         ath10k_htt_detach(&ar->htt);
599 err_wmi_detach:
600         ath10k_wmi_detach(ar);
601 err:
602         return status;
603 }
604
605 void ath10k_core_stop(struct ath10k *ar)
606 {
607         ath10k_htc_stop(&ar->htc);
608         ath10k_htt_detach(&ar->htt);
609         ath10k_wmi_detach(ar);
610 }
611
612 int ath10k_core_register(struct ath10k *ar)
613 {
614         int status;
615
616         status = ath10k_core_start(ar);
617         if (status)
618                 goto err;
619
620         status = ath10k_mac_register(ar);
621         if (status)
622                 goto err_core_stop;
623
624         status = ath10k_debug_create(ar);
625         if (status) {
626                 ath10k_err("unable to initialize debugfs\n");
627                 goto err_unregister_mac;
628         }
629
630         return 0;
631
632 err_unregister_mac:
633         ath10k_mac_unregister(ar);
634 err_core_stop:
635         ath10k_core_stop(ar);
636 err:
637         return status;
638 }
639 EXPORT_SYMBOL(ath10k_core_register);
640
641 void ath10k_core_unregister(struct ath10k *ar)
642 {
643         /* We must unregister from mac80211 before we stop HTC and HIF.
644          * Otherwise we will fail to submit commands to FW and mac80211 will be
645          * unhappy about callback failures. */
646         ath10k_mac_unregister(ar);
647         ath10k_core_stop(ar);
648 }
649 EXPORT_SYMBOL(ath10k_core_unregister);
650
651 MODULE_AUTHOR("Qualcomm Atheros");
652 MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
653 MODULE_LICENSE("Dual BSD/GPL");