ath10k: store firmware files in memory
[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         const struct firmware *fw = ar->board_data;
251         u32 board_data_size = QCA988X_BOARD_DATA_SZ;
252         u32 address;
253         int ret;
254
255         ret = ath10k_push_board_ext_data(ar, fw);
256         if (ret) {
257                 ath10k_err("could not push board ext data (%d)\n", ret);
258                 goto exit;
259         }
260
261         ret = ath10k_bmi_read32(ar, hi_board_data, &address);
262         if (ret) {
263                 ath10k_err("could not read board data addr (%d)\n", ret);
264                 goto exit;
265         }
266
267         ret = ath10k_bmi_write_memory(ar, address, fw->data,
268                                       min_t(u32, board_data_size, fw->size));
269         if (ret) {
270                 ath10k_err("could not write board data (%d)\n", ret);
271                 goto exit;
272         }
273
274         ret = ath10k_bmi_write32(ar, hi_board_data_initialized, 1);
275         if (ret) {
276                 ath10k_err("could not write board data bit (%d)\n", ret);
277                 goto exit;
278         }
279
280 exit:
281         return ret;
282 }
283
284 static int ath10k_download_and_run_otp(struct ath10k *ar)
285 {
286         const struct firmware *fw = ar->otp;
287         u32 address = ar->hw_params.patch_load_addr;
288         u32 exec_param;
289         int ret;
290
291         /* OTP is optional */
292
293         if (!ar->otp)
294                 return 0;
295
296         ret = ath10k_bmi_fast_download(ar, address, fw->data, fw->size);
297         if (ret) {
298                 ath10k_err("could not write otp (%d)\n", ret);
299                 goto exit;
300         }
301
302         exec_param = 0;
303         ret = ath10k_bmi_execute(ar, address, &exec_param);
304         if (ret) {
305                 ath10k_err("could not execute otp (%d)\n", ret);
306                 goto exit;
307         }
308
309 exit:
310         return ret;
311 }
312
313 static int ath10k_download_fw(struct ath10k *ar)
314 {
315         const struct firmware *fw = ar->firmware;
316         u32 address;
317         int ret;
318
319         address = ar->hw_params.patch_load_addr;
320
321         ret = ath10k_bmi_fast_download(ar, address, fw->data, fw->size);
322         if (ret) {
323                 ath10k_err("could not write fw (%d)\n", ret);
324                 goto exit;
325         }
326
327 exit:
328         return ret;
329 }
330
331 static void ath10k_core_free_firmware_files(struct ath10k *ar)
332 {
333         if (ar->board_data && !IS_ERR(ar->board_data))
334                 release_firmware(ar->board_data);
335
336         if (ar->otp && !IS_ERR(ar->otp))
337                 release_firmware(ar->otp);
338
339         if (ar->firmware && !IS_ERR(ar->firmware))
340                 release_firmware(ar->firmware);
341
342         ar->board_data = NULL;
343         ar->otp = NULL;
344         ar->firmware = NULL;
345 }
346
347 static int ath10k_core_fetch_firmware_files(struct ath10k *ar)
348 {
349         int ret = 0;
350
351         if (ar->hw_params.fw.fw == NULL) {
352                 ath10k_err("firmware file not defined\n");
353                 return -EINVAL;
354         }
355
356         if (ar->hw_params.fw.board == NULL) {
357                 ath10k_err("board data file not defined");
358                 return -EINVAL;
359         }
360
361         ar->board_data = ath10k_fetch_fw_file(ar,
362                                               ar->hw_params.fw.dir,
363                                               ar->hw_params.fw.board);
364         if (IS_ERR(ar->board_data)) {
365                 ret = PTR_ERR(ar->board_data);
366                 ath10k_err("could not fetch board data (%d)\n", ret);
367                 goto err;
368         }
369
370         ar->firmware = ath10k_fetch_fw_file(ar,
371                                             ar->hw_params.fw.dir,
372                                             ar->hw_params.fw.fw);
373         if (IS_ERR(ar->firmware)) {
374                 ret = PTR_ERR(ar->firmware);
375                 ath10k_err("could not fetch firmware (%d)\n", ret);
376                 goto err;
377         }
378
379         /* OTP may be undefined. If so, don't fetch it at all */
380         if (ar->hw_params.fw.otp == NULL)
381                 return 0;
382
383         ar->otp = ath10k_fetch_fw_file(ar,
384                                        ar->hw_params.fw.dir,
385                                        ar->hw_params.fw.otp);
386         if (IS_ERR(ar->otp)) {
387                 ret = PTR_ERR(ar->otp);
388                 ath10k_err("could not fetch otp (%d)\n", ret);
389                 goto err;
390         }
391
392         return 0;
393
394 err:
395         ath10k_core_free_firmware_files(ar);
396         return ret;
397 }
398
399 static int ath10k_init_download_firmware(struct ath10k *ar)
400 {
401         int ret;
402
403         ret = ath10k_download_board_data(ar);
404         if (ret)
405                 return ret;
406
407         ret = ath10k_download_and_run_otp(ar);
408         if (ret)
409                 return ret;
410
411         ret = ath10k_download_fw(ar);
412         if (ret)
413                 return ret;
414
415         return ret;
416 }
417
418 static int ath10k_init_uart(struct ath10k *ar)
419 {
420         int ret;
421
422         /*
423          * Explicitly setting UART prints to zero as target turns it on
424          * based on scratch registers.
425          */
426         ret = ath10k_bmi_write32(ar, hi_serial_enable, 0);
427         if (ret) {
428                 ath10k_warn("could not disable UART prints (%d)\n", ret);
429                 return ret;
430         }
431
432         if (!uart_print) {
433                 ath10k_info("UART prints disabled\n");
434                 return 0;
435         }
436
437         ret = ath10k_bmi_write32(ar, hi_dbg_uart_txpin, 7);
438         if (ret) {
439                 ath10k_warn("could not enable UART prints (%d)\n", ret);
440                 return ret;
441         }
442
443         ret = ath10k_bmi_write32(ar, hi_serial_enable, 1);
444         if (ret) {
445                 ath10k_warn("could not enable UART prints (%d)\n", ret);
446                 return ret;
447         }
448
449         ath10k_info("UART prints enabled\n");
450         return 0;
451 }
452
453 static int ath10k_init_hw_params(struct ath10k *ar)
454 {
455         const struct ath10k_hw_params *uninitialized_var(hw_params);
456         int i;
457
458         for (i = 0; i < ARRAY_SIZE(ath10k_hw_params_list); i++) {
459                 hw_params = &ath10k_hw_params_list[i];
460
461                 if (hw_params->id == ar->target_version)
462                         break;
463         }
464
465         if (i == ARRAY_SIZE(ath10k_hw_params_list)) {
466                 ath10k_err("Unsupported hardware version: 0x%x\n",
467                            ar->target_version);
468                 return -EINVAL;
469         }
470
471         ar->hw_params = *hw_params;
472
473         ath10k_info("Hardware name %s version 0x%x\n",
474                     ar->hw_params.name, ar->target_version);
475
476         return 0;
477 }
478
479 struct ath10k *ath10k_core_create(void *hif_priv, struct device *dev,
480                                   const struct ath10k_hif_ops *hif_ops)
481 {
482         struct ath10k *ar;
483
484         ar = ath10k_mac_create();
485         if (!ar)
486                 return NULL;
487
488         ar->ath_common.priv = ar;
489         ar->ath_common.hw = ar->hw;
490
491         ar->p2p = !!ath10k_p2p;
492         ar->dev = dev;
493
494         ar->hif.priv = hif_priv;
495         ar->hif.ops = hif_ops;
496
497         init_completion(&ar->scan.started);
498         init_completion(&ar->scan.completed);
499         init_completion(&ar->scan.on_channel);
500
501         init_completion(&ar->install_key_done);
502         init_completion(&ar->vdev_setup_done);
503
504         setup_timer(&ar->scan.timeout, ath10k_reset_scan, (unsigned long)ar);
505
506         ar->workqueue = create_singlethread_workqueue("ath10k_wq");
507         if (!ar->workqueue)
508                 goto err_wq;
509
510         mutex_init(&ar->conf_mutex);
511         spin_lock_init(&ar->data_lock);
512
513         INIT_LIST_HEAD(&ar->peers);
514         init_waitqueue_head(&ar->peer_mapping_wq);
515
516         init_completion(&ar->offchan_tx_completed);
517         INIT_WORK(&ar->offchan_tx_work, ath10k_offchan_tx_work);
518         skb_queue_head_init(&ar->offchan_tx_queue);
519
520         init_waitqueue_head(&ar->event_queue);
521
522         return ar;
523
524 err_wq:
525         ath10k_mac_destroy(ar);
526         return NULL;
527 }
528 EXPORT_SYMBOL(ath10k_core_create);
529
530 void ath10k_core_destroy(struct ath10k *ar)
531 {
532         flush_workqueue(ar->workqueue);
533         destroy_workqueue(ar->workqueue);
534
535         ath10k_mac_destroy(ar);
536 }
537 EXPORT_SYMBOL(ath10k_core_destroy);
538
539 int ath10k_core_start(struct ath10k *ar)
540 {
541         int status;
542
543         ath10k_bmi_start(ar);
544
545         if (ath10k_init_configure_target(ar)) {
546                 status = -EINVAL;
547                 goto err;
548         }
549
550         status = ath10k_init_download_firmware(ar);
551         if (status)
552                 goto err;
553
554         status = ath10k_init_uart(ar);
555         if (status)
556                 goto err;
557
558         ar->htc.htc_ops.target_send_suspend_complete =
559                 ath10k_send_suspend_complete;
560
561         status = ath10k_htc_init(ar);
562         if (status) {
563                 ath10k_err("could not init HTC (%d)\n", status);
564                 goto err;
565         }
566
567         status = ath10k_bmi_done(ar);
568         if (status)
569                 goto err;
570
571         status = ath10k_wmi_attach(ar);
572         if (status) {
573                 ath10k_err("WMI attach failed: %d\n", status);
574                 goto err;
575         }
576
577         status = ath10k_htc_wait_target(&ar->htc);
578         if (status)
579                 goto err_wmi_detach;
580
581         status = ath10k_htt_attach(ar);
582         if (status) {
583                 ath10k_err("could not attach htt (%d)\n", status);
584                 goto err_wmi_detach;
585         }
586
587         status = ath10k_init_connect_htc(ar);
588         if (status)
589                 goto err_htt_detach;
590
591         ath10k_info("firmware %s booted\n", ar->hw->wiphy->fw_version);
592
593         status = ath10k_check_fw_version(ar);
594         if (status)
595                 goto err_disconnect_htc;
596
597         status = ath10k_wmi_cmd_init(ar);
598         if (status) {
599                 ath10k_err("could not send WMI init command (%d)\n", status);
600                 goto err_disconnect_htc;
601         }
602
603         status = ath10k_wmi_wait_for_unified_ready(ar);
604         if (status <= 0) {
605                 ath10k_err("wmi unified ready event not received\n");
606                 status = -ETIMEDOUT;
607                 goto err_disconnect_htc;
608         }
609
610         status = ath10k_htt_attach_target(&ar->htt);
611         if (status)
612                 goto err_disconnect_htc;
613
614         ar->free_vdev_map = (1 << TARGET_NUM_VDEVS) - 1;
615
616         return 0;
617
618 err_disconnect_htc:
619         ath10k_htc_stop(&ar->htc);
620 err_htt_detach:
621         ath10k_htt_detach(&ar->htt);
622 err_wmi_detach:
623         ath10k_wmi_detach(ar);
624 err:
625         return status;
626 }
627 EXPORT_SYMBOL(ath10k_core_start);
628
629 void ath10k_core_stop(struct ath10k *ar)
630 {
631         ath10k_htc_stop(&ar->htc);
632         ath10k_htt_detach(&ar->htt);
633         ath10k_wmi_detach(ar);
634 }
635 EXPORT_SYMBOL(ath10k_core_stop);
636
637 /* mac80211 manages fw/hw initialization through start/stop hooks. However in
638  * order to know what hw capabilities should be advertised to mac80211 it is
639  * necessary to load the firmware (and tear it down immediately since start
640  * hook will try to init it again) before registering */
641 static int ath10k_core_probe_fw(struct ath10k *ar)
642 {
643         struct bmi_target_info target_info;
644         int ret = 0;
645
646         ret = ath10k_hif_power_up(ar);
647         if (ret) {
648                 ath10k_err("could not start pci hif (%d)\n", ret);
649                 return ret;
650         }
651
652         memset(&target_info, 0, sizeof(target_info));
653         ret = ath10k_bmi_get_target_info(ar, &target_info);
654         if (ret) {
655                 ath10k_err("could not get target info (%d)\n", ret);
656                 ath10k_hif_power_down(ar);
657                 return ret;
658         }
659
660         ar->target_version = target_info.version;
661         ar->hw->wiphy->hw_version = target_info.version;
662
663         ret = ath10k_init_hw_params(ar);
664         if (ret) {
665                 ath10k_err("could not get hw params (%d)\n", ret);
666                 ath10k_hif_power_down(ar);
667                 return ret;
668         }
669
670         ret = ath10k_core_fetch_firmware_files(ar);
671         if (ret) {
672                 ath10k_err("could not fetch firmware files (%d)\n", ret);
673                 ath10k_hif_power_down(ar);
674                 return ret;
675         }
676
677         ret = ath10k_core_start(ar);
678         if (ret) {
679                 ath10k_err("could not init core (%d)\n", ret);
680                 ath10k_core_free_firmware_files(ar);
681                 ath10k_hif_power_down(ar);
682                 return ret;
683         }
684
685         ath10k_core_stop(ar);
686         ath10k_hif_power_down(ar);
687         return 0;
688 }
689
690 int ath10k_core_register(struct ath10k *ar)
691 {
692         int status;
693
694         status = ath10k_core_probe_fw(ar);
695         if (status) {
696                 ath10k_err("could not probe fw (%d)\n", status);
697                 return status;
698         }
699
700         status = ath10k_mac_register(ar);
701         if (status) {
702                 ath10k_err("could not register to mac80211 (%d)\n", status);
703                 goto err_release_fw;
704         }
705
706         status = ath10k_debug_create(ar);
707         if (status) {
708                 ath10k_err("unable to initialize debugfs\n");
709                 goto err_unregister_mac;
710         }
711
712         return 0;
713
714 err_unregister_mac:
715         ath10k_mac_unregister(ar);
716 err_release_fw:
717         ath10k_core_free_firmware_files(ar);
718         return status;
719 }
720 EXPORT_SYMBOL(ath10k_core_register);
721
722 void ath10k_core_unregister(struct ath10k *ar)
723 {
724         /* We must unregister from mac80211 before we stop HTC and HIF.
725          * Otherwise we will fail to submit commands to FW and mac80211 will be
726          * unhappy about callback failures. */
727         ath10k_mac_unregister(ar);
728         ath10k_core_free_firmware_files(ar);
729 }
730 EXPORT_SYMBOL(ath10k_core_unregister);
731
732 MODULE_AUTHOR("Qualcomm Atheros");
733 MODULE_DESCRIPTION("Core module for QCA988X PCIe devices.");
734 MODULE_LICENSE("Dual BSD/GPL");