Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / net / wireless / iwmc3200wifi / main.c
1 /*
2  * Intel Wireless Multicomm 3200 WiFi driver
3  *
4  * Copyright (C) 2009 Intel Corporation. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *   * Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *   * Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in
14  *     the documentation and/or other materials provided with the
15  *     distribution.
16  *   * Neither the name of Intel Corporation nor the names of its
17  *     contributors may be used to endorse or promote products derived
18  *     from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *
33  * Intel Corporation <ilw@linux.intel.com>
34  * Samuel Ortiz <samuel.ortiz@intel.com>
35  * Zhu Yi <yi.zhu@intel.com>
36  *
37  */
38
39 #include <linux/kernel.h>
40 #include <linux/netdevice.h>
41 #include <linux/sched.h>
42 #include <linux/ieee80211.h>
43 #include <linux/wireless.h>
44
45 #include "iwm.h"
46 #include "debug.h"
47 #include "bus.h"
48 #include "umac.h"
49 #include "commands.h"
50 #include "hal.h"
51 #include "fw.h"
52 #include "rx.h"
53
54 static struct iwm_conf def_iwm_conf = {
55
56         .sdio_ior_timeout       = 5000,
57         .calib_map              = BIT(CALIB_CFG_DC_IDX) |
58                                   BIT(CALIB_CFG_LO_IDX) |
59                                   BIT(CALIB_CFG_TX_IQ_IDX)      |
60                                   BIT(CALIB_CFG_RX_IQ_IDX)      |
61                                   BIT(SHILOH_PHY_CALIBRATE_BASE_BAND_CMD),
62         .expected_calib_map     = BIT(PHY_CALIBRATE_DC_CMD)     |
63                                   BIT(PHY_CALIBRATE_LO_CMD)     |
64                                   BIT(PHY_CALIBRATE_TX_IQ_CMD)  |
65                                   BIT(PHY_CALIBRATE_RX_IQ_CMD)  |
66                                   BIT(SHILOH_PHY_CALIBRATE_BASE_BAND_CMD),
67         .ct_kill_entry          = 110,
68         .ct_kill_exit           = 110,
69         .reset_on_fatal_err     = 1,
70         .auto_connect           = 1,
71         .wimax_not_present      = 0,
72         .enable_qos             = 1,
73         .mode                   = UMAC_MODE_BSS,
74
75         /* UMAC configuration */
76         .power_index            = 0,
77         .frag_threshold         = IEEE80211_MAX_FRAG_THRESHOLD,
78         .rts_threshold          = IEEE80211_MAX_RTS_THRESHOLD,
79         .cts_to_self            = 0,
80
81         .assoc_timeout          = 2,
82         .roam_timeout           = 10,
83         .wireless_mode          = WIRELESS_MODE_11A | WIRELESS_MODE_11G,
84         .coexist_mode           = COEX_MODE_CM,
85
86         /* IBSS */
87         .ibss_band              = UMAC_BAND_2GHZ,
88         .ibss_channel           = 1,
89
90         .mac_addr               = {0x00, 0x02, 0xb3, 0x01, 0x02, 0x03},
91 };
92
93 static int modparam_reset;
94 module_param_named(reset, modparam_reset, bool, 0644);
95 MODULE_PARM_DESC(reset, "reset on firmware errors (default 0 [not reset])");
96
97 int iwm_mode_to_nl80211_iftype(int mode)
98 {
99         switch (mode) {
100         case UMAC_MODE_BSS:
101                 return NL80211_IFTYPE_STATION;
102         case UMAC_MODE_IBSS:
103                 return NL80211_IFTYPE_ADHOC;
104         default:
105                 return NL80211_IFTYPE_UNSPECIFIED;
106         }
107
108         return 0;
109 }
110
111 static void iwm_statistics_request(struct work_struct *work)
112 {
113         struct iwm_priv *iwm =
114                 container_of(work, struct iwm_priv, stats_request.work);
115
116         iwm_send_umac_stats_req(iwm, 0);
117 }
118
119 static void iwm_disconnect_work(struct work_struct *work)
120 {
121         struct iwm_priv *iwm =
122                 container_of(work, struct iwm_priv, disconnect.work);
123
124         if (iwm->umac_profile_active)
125                 iwm_invalidate_mlme_profile(iwm);
126
127         clear_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
128         iwm->umac_profile_active = 0;
129         memset(iwm->bssid, 0, ETH_ALEN);
130         iwm->channel = 0;
131
132         iwm_link_off(iwm);
133
134         wake_up_interruptible(&iwm->mlme_queue);
135
136         cfg80211_disconnected(iwm_to_ndev(iwm), 0, NULL, 0, GFP_KERNEL);
137 }
138
139 static void iwm_ct_kill_work(struct work_struct *work)
140 {
141         struct iwm_priv *iwm =
142                 container_of(work, struct iwm_priv, ct_kill_delay.work);
143         struct wiphy *wiphy = iwm_to_wiphy(iwm);
144
145         IWM_INFO(iwm, "CT kill delay timeout\n");
146
147         wiphy_rfkill_set_hw_state(wiphy, false);
148 }
149
150 static int __iwm_up(struct iwm_priv *iwm);
151 static int __iwm_down(struct iwm_priv *iwm);
152
153 static void iwm_reset_worker(struct work_struct *work)
154 {
155         struct iwm_priv *iwm;
156         struct iwm_umac_profile *profile = NULL;
157         int uninitialized_var(ret), retry = 0;
158
159         iwm = container_of(work, struct iwm_priv, reset_worker);
160
161         /*
162          * XXX: The iwm->mutex is introduced purely for this reset work,
163          * because the other users for iwm_up and iwm_down are only netdev
164          * ndo_open and ndo_stop which are already protected by rtnl.
165          * Please remove iwm->mutex together if iwm_reset_worker() is not
166          * required in the future.
167          */
168         if (!mutex_trylock(&iwm->mutex)) {
169                 IWM_WARN(iwm, "We are in the middle of interface bringing "
170                          "UP/DOWN. Skip driver resetting.\n");
171                 return;
172         }
173
174         if (iwm->umac_profile_active) {
175                 profile = kmalloc(sizeof(struct iwm_umac_profile), GFP_KERNEL);
176                 if (profile)
177                         memcpy(profile, iwm->umac_profile, sizeof(*profile));
178                 else
179                         IWM_ERR(iwm, "Couldn't alloc memory for profile\n");
180         }
181
182         __iwm_down(iwm);
183
184         while (retry++ < 3) {
185                 ret = __iwm_up(iwm);
186                 if (!ret)
187                         break;
188
189                 schedule_timeout_uninterruptible(10 * HZ);
190         }
191
192         if (ret) {
193                 IWM_WARN(iwm, "iwm_up() failed: %d\n", ret);
194
195                 kfree(profile);
196                 goto out;
197         }
198
199         if (profile) {
200                 IWM_DBG_MLME(iwm, DBG, "Resend UMAC profile\n");
201                 memcpy(iwm->umac_profile, profile, sizeof(*profile));
202                 iwm_send_mlme_profile(iwm);
203                 kfree(profile);
204         } else
205                 clear_bit(IWM_STATUS_RESETTING, &iwm->status);
206
207  out:
208         mutex_unlock(&iwm->mutex);
209 }
210
211 static void iwm_auth_retry_worker(struct work_struct *work)
212 {
213         struct iwm_priv *iwm;
214         int i, ret;
215
216         iwm = container_of(work, struct iwm_priv, auth_retry_worker);
217         if (iwm->umac_profile_active) {
218                 ret = iwm_invalidate_mlme_profile(iwm);
219                 if (ret < 0)
220                         return;
221         }
222
223         iwm->umac_profile->sec.auth_type = UMAC_AUTH_TYPE_LEGACY_PSK;
224
225         ret = iwm_send_mlme_profile(iwm);
226         if (ret < 0)
227                 return;
228
229         for (i = 0; i < IWM_NUM_KEYS; i++)
230                 if (iwm->keys[i].key_len)
231                         iwm_set_key(iwm, 0, &iwm->keys[i]);
232
233         iwm_set_tx_key(iwm, iwm->default_key);
234 }
235
236
237
238 static void iwm_watchdog(unsigned long data)
239 {
240         struct iwm_priv *iwm = (struct iwm_priv *)data;
241
242         IWM_WARN(iwm, "Watchdog expired: UMAC stalls!\n");
243
244         if (modparam_reset)
245                 iwm_resetting(iwm);
246 }
247
248 int iwm_priv_init(struct iwm_priv *iwm)
249 {
250         int i;
251         char name[32];
252
253         iwm->status = 0;
254         INIT_LIST_HEAD(&iwm->pending_notif);
255         init_waitqueue_head(&iwm->notif_queue);
256         init_waitqueue_head(&iwm->nonwifi_queue);
257         init_waitqueue_head(&iwm->wifi_ntfy_queue);
258         init_waitqueue_head(&iwm->mlme_queue);
259         memcpy(&iwm->conf, &def_iwm_conf, sizeof(struct iwm_conf));
260         spin_lock_init(&iwm->tx_credit.lock);
261         INIT_LIST_HEAD(&iwm->wifi_pending_cmd);
262         INIT_LIST_HEAD(&iwm->nonwifi_pending_cmd);
263         iwm->wifi_seq_num = UMAC_WIFI_SEQ_NUM_BASE;
264         iwm->nonwifi_seq_num = UMAC_NONWIFI_SEQ_NUM_BASE;
265         spin_lock_init(&iwm->cmd_lock);
266         iwm->scan_id = 1;
267         INIT_DELAYED_WORK(&iwm->stats_request, iwm_statistics_request);
268         INIT_DELAYED_WORK(&iwm->disconnect, iwm_disconnect_work);
269         INIT_DELAYED_WORK(&iwm->ct_kill_delay, iwm_ct_kill_work);
270         INIT_WORK(&iwm->reset_worker, iwm_reset_worker);
271         INIT_WORK(&iwm->auth_retry_worker, iwm_auth_retry_worker);
272         INIT_LIST_HEAD(&iwm->bss_list);
273
274         skb_queue_head_init(&iwm->rx_list);
275         INIT_LIST_HEAD(&iwm->rx_tickets);
276         for (i = 0; i < IWM_RX_ID_HASH; i++)
277                 INIT_LIST_HEAD(&iwm->rx_packets[i]);
278
279         INIT_WORK(&iwm->rx_worker, iwm_rx_worker);
280
281         iwm->rx_wq = create_singlethread_workqueue(KBUILD_MODNAME "_rx");
282         if (!iwm->rx_wq)
283                 return -EAGAIN;
284
285         for (i = 0; i < IWM_TX_QUEUES; i++) {
286                 INIT_WORK(&iwm->txq[i].worker, iwm_tx_worker);
287                 snprintf(name, 32, KBUILD_MODNAME "_tx_%d", i);
288                 iwm->txq[i].id = i;
289                 iwm->txq[i].wq = create_singlethread_workqueue(name);
290                 if (!iwm->txq[i].wq)
291                         return -EAGAIN;
292
293                 skb_queue_head_init(&iwm->txq[i].queue);
294         }
295
296         for (i = 0; i < IWM_NUM_KEYS; i++)
297                 memset(&iwm->keys[i], 0, sizeof(struct iwm_key));
298
299         iwm->default_key = -1;
300
301         init_timer(&iwm->watchdog);
302         iwm->watchdog.function = iwm_watchdog;
303         iwm->watchdog.data = (unsigned long)iwm;
304         mutex_init(&iwm->mutex);
305
306         iwm->last_fw_err = kzalloc(sizeof(struct iwm_fw_error_hdr),
307                                    GFP_KERNEL);
308         if (iwm->last_fw_err == NULL)
309                 return -ENOMEM;
310
311         return 0;
312 }
313
314 void iwm_priv_deinit(struct iwm_priv *iwm)
315 {
316         int i;
317
318         for (i = 0; i < IWM_TX_QUEUES; i++)
319                 destroy_workqueue(iwm->txq[i].wq);
320
321         destroy_workqueue(iwm->rx_wq);
322         kfree(iwm->last_fw_err);
323 }
324
325 /*
326  * We reset all the structures, and we reset the UMAC.
327  * After calling this routine, you're expected to reload
328  * the firmware.
329  */
330 void iwm_reset(struct iwm_priv *iwm)
331 {
332         struct iwm_notif *notif, *next;
333
334         if (test_bit(IWM_STATUS_READY, &iwm->status))
335                 iwm_target_reset(iwm);
336
337         if (test_bit(IWM_STATUS_RESETTING, &iwm->status)) {
338                 iwm->status = 0;
339                 set_bit(IWM_STATUS_RESETTING, &iwm->status);
340         } else
341                 iwm->status = 0;
342         iwm->scan_id = 1;
343
344         list_for_each_entry_safe(notif, next, &iwm->pending_notif, pending) {
345                 list_del(&notif->pending);
346                 kfree(notif->buf);
347                 kfree(notif);
348         }
349
350         iwm_cmd_flush(iwm);
351
352         flush_workqueue(iwm->rx_wq);
353
354         iwm_link_off(iwm);
355 }
356
357 void iwm_resetting(struct iwm_priv *iwm)
358 {
359         set_bit(IWM_STATUS_RESETTING, &iwm->status);
360
361         schedule_work(&iwm->reset_worker);
362 }
363
364 /*
365  * Notification code:
366  *
367  * We're faced with the following issue: Any host command can
368  * have an answer or not, and if there's an answer to expect,
369  * it can be treated synchronously or asynchronously.
370  * To work around the synchronous answer case, we implemented
371  * our notification mechanism.
372  * When a code path needs to wait for a command response
373  * synchronously, it calls notif_handle(), which waits for the
374  * right notification to show up, and then process it. Before
375  * starting to wait, it registered as a waiter for this specific
376  * answer (by toggling a bit in on of the handler_map), so that
377  * the rx code knows that it needs to send a notification to the
378  * waiting processes. It does so by calling iwm_notif_send(),
379  * which adds the notification to the pending notifications list,
380  * and then wakes the waiting processes up.
381  */
382 int iwm_notif_send(struct iwm_priv *iwm, struct iwm_wifi_cmd *cmd,
383                    u8 cmd_id, u8 source, u8 *buf, unsigned long buf_size)
384 {
385         struct iwm_notif *notif;
386
387         notif = kzalloc(sizeof(struct iwm_notif), GFP_KERNEL);
388         if (!notif) {
389                 IWM_ERR(iwm, "Couldn't alloc memory for notification\n");
390                 return -ENOMEM;
391         }
392
393         INIT_LIST_HEAD(&notif->pending);
394         notif->cmd = cmd;
395         notif->cmd_id = cmd_id;
396         notif->src = source;
397         notif->buf = kzalloc(buf_size, GFP_KERNEL);
398         if (!notif->buf) {
399                 IWM_ERR(iwm, "Couldn't alloc notification buffer\n");
400                 kfree(notif);
401                 return -ENOMEM;
402         }
403         notif->buf_size = buf_size;
404         memcpy(notif->buf, buf, buf_size);
405         list_add_tail(&notif->pending, &iwm->pending_notif);
406
407         wake_up_interruptible(&iwm->notif_queue);
408
409         return 0;
410 }
411
412 static struct iwm_notif *iwm_notif_find(struct iwm_priv *iwm, u32 cmd,
413                                         u8 source)
414 {
415         struct iwm_notif *notif, *next;
416
417         list_for_each_entry_safe(notif, next, &iwm->pending_notif, pending) {
418                 if ((notif->cmd_id == cmd) && (notif->src == source)) {
419                         list_del(&notif->pending);
420                         return notif;
421                 }
422         }
423
424         return NULL;
425 }
426
427 static struct iwm_notif *iwm_notif_wait(struct iwm_priv *iwm, u32 cmd,
428                                         u8 source, long timeout)
429 {
430         int ret;
431         struct iwm_notif *notif;
432         unsigned long *map = NULL;
433
434         switch (source) {
435         case IWM_SRC_LMAC:
436                 map = &iwm->lmac_handler_map[0];
437                 break;
438         case IWM_SRC_UMAC:
439                 map = &iwm->umac_handler_map[0];
440                 break;
441         case IWM_SRC_UDMA:
442                 map = &iwm->udma_handler_map[0];
443                 break;
444         }
445
446         set_bit(cmd, map);
447
448         ret = wait_event_interruptible_timeout(iwm->notif_queue,
449                          ((notif = iwm_notif_find(iwm, cmd, source)) != NULL),
450                                                timeout);
451         clear_bit(cmd, map);
452
453         if (!ret)
454                 return NULL;
455
456         return notif;
457 }
458
459 int iwm_notif_handle(struct iwm_priv *iwm, u32 cmd, u8 source, long timeout)
460 {
461         int ret;
462         struct iwm_notif *notif;
463
464         notif = iwm_notif_wait(iwm, cmd, source, timeout);
465         if (!notif)
466                 return -ETIME;
467
468         ret = iwm_rx_handle_resp(iwm, notif->buf, notif->buf_size, notif->cmd);
469         kfree(notif->buf);
470         kfree(notif);
471
472         return ret;
473 }
474
475 static int iwm_config_boot_params(struct iwm_priv *iwm)
476 {
477         struct iwm_udma_nonwifi_cmd target_cmd;
478         int ret;
479
480         /* check Wimax is off and config debug monitor */
481         if (iwm->conf.wimax_not_present) {
482                 u32 data1 = 0x1f;
483                 u32 addr1 = 0x606BE258;
484
485                 u32 data2_set = 0x0;
486                 u32 data2_clr = 0x1;
487                 u32 addr2 = 0x606BE100;
488
489                 u32 data3 = 0x1;
490                 u32 addr3 = 0x606BEC00;
491
492                 target_cmd.resp = 0;
493                 target_cmd.handle_by_hw = 0;
494                 target_cmd.eop = 1;
495
496                 target_cmd.opcode = UMAC_HDI_OUT_OPCODE_WRITE;
497                 target_cmd.addr = cpu_to_le32(addr1);
498                 target_cmd.op1_sz = cpu_to_le32(sizeof(u32));
499                 target_cmd.op2 = 0;
500
501                 ret = iwm_hal_send_target_cmd(iwm, &target_cmd, &data1);
502                 if (ret < 0) {
503                         IWM_ERR(iwm, "iwm_hal_send_target_cmd failed\n");
504                         return ret;
505                 }
506
507                 target_cmd.opcode = UMAC_HDI_OUT_OPCODE_READ_MODIFY_WRITE;
508                 target_cmd.addr = cpu_to_le32(addr2);
509                 target_cmd.op1_sz = cpu_to_le32(data2_set);
510                 target_cmd.op2 = cpu_to_le32(data2_clr);
511
512                 ret = iwm_hal_send_target_cmd(iwm, &target_cmd, &data1);
513                 if (ret < 0) {
514                         IWM_ERR(iwm, "iwm_hal_send_target_cmd failed\n");
515                         return ret;
516                 }
517
518                 target_cmd.opcode = UMAC_HDI_OUT_OPCODE_WRITE;
519                 target_cmd.addr = cpu_to_le32(addr3);
520                 target_cmd.op1_sz = cpu_to_le32(sizeof(u32));
521                 target_cmd.op2 = 0;
522
523                 ret = iwm_hal_send_target_cmd(iwm, &target_cmd, &data3);
524                 if (ret < 0) {
525                         IWM_ERR(iwm, "iwm_hal_send_target_cmd failed\n");
526                         return ret;
527                 }
528         }
529
530         return 0;
531 }
532
533 void iwm_init_default_profile(struct iwm_priv *iwm,
534                               struct iwm_umac_profile *profile)
535 {
536         memset(profile, 0, sizeof(struct iwm_umac_profile));
537
538         profile->sec.auth_type = UMAC_AUTH_TYPE_OPEN;
539         profile->sec.flags = UMAC_SEC_FLG_LEGACY_PROFILE;
540         profile->sec.ucast_cipher = UMAC_CIPHER_TYPE_NONE;
541         profile->sec.mcast_cipher = UMAC_CIPHER_TYPE_NONE;
542
543         if (iwm->conf.enable_qos)
544                 profile->flags |= cpu_to_le16(UMAC_PROFILE_QOS_ALLOWED);
545
546         profile->wireless_mode = iwm->conf.wireless_mode;
547         profile->mode = cpu_to_le32(iwm->conf.mode);
548
549         profile->ibss.atim = 0;
550         profile->ibss.beacon_interval = 100;
551         profile->ibss.join_only = 0;
552         profile->ibss.band = iwm->conf.ibss_band;
553         profile->ibss.channel = iwm->conf.ibss_channel;
554 }
555
556 void iwm_link_on(struct iwm_priv *iwm)
557 {
558         netif_carrier_on(iwm_to_ndev(iwm));
559         netif_tx_wake_all_queues(iwm_to_ndev(iwm));
560
561         iwm_send_umac_stats_req(iwm, 0);
562 }
563
564 void iwm_link_off(struct iwm_priv *iwm)
565 {
566         struct iw_statistics *wstats = &iwm->wstats;
567         int i;
568
569         netif_tx_stop_all_queues(iwm_to_ndev(iwm));
570         netif_carrier_off(iwm_to_ndev(iwm));
571
572         for (i = 0; i < IWM_TX_QUEUES; i++) {
573                 skb_queue_purge(&iwm->txq[i].queue);
574
575                 iwm->txq[i].concat_count = 0;
576                 iwm->txq[i].concat_ptr = iwm->txq[i].concat_buf;
577
578                 flush_workqueue(iwm->txq[i].wq);
579         }
580
581         iwm_rx_free(iwm);
582
583         cancel_delayed_work_sync(&iwm->stats_request);
584         memset(wstats, 0, sizeof(struct iw_statistics));
585         wstats->qual.updated = IW_QUAL_ALL_INVALID;
586
587         kfree(iwm->req_ie);
588         iwm->req_ie = NULL;
589         iwm->req_ie_len = 0;
590         kfree(iwm->resp_ie);
591         iwm->resp_ie = NULL;
592         iwm->resp_ie_len = 0;
593
594         del_timer_sync(&iwm->watchdog);
595 }
596
597 static void iwm_bss_list_clean(struct iwm_priv *iwm)
598 {
599         struct iwm_bss_info *bss, *next;
600
601         list_for_each_entry_safe(bss, next, &iwm->bss_list, node) {
602                 list_del(&bss->node);
603                 kfree(bss->bss);
604                 kfree(bss);
605         }
606 }
607
608 static int iwm_channels_init(struct iwm_priv *iwm)
609 {
610         int ret;
611
612         ret = iwm_send_umac_channel_list(iwm);
613         if (ret) {
614                 IWM_ERR(iwm, "Send channel list failed\n");
615                 return ret;
616         }
617
618         ret = iwm_notif_handle(iwm, UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST,
619                                IWM_SRC_UMAC, WAIT_NOTIF_TIMEOUT);
620         if (ret) {
621                 IWM_ERR(iwm, "Didn't get a channel list notification\n");
622                 return ret;
623         }
624
625         return 0;
626 }
627
628 static int __iwm_up(struct iwm_priv *iwm)
629 {
630         int ret;
631         struct iwm_notif *notif_reboot, *notif_ack = NULL;
632         struct wiphy *wiphy = iwm_to_wiphy(iwm);
633
634         ret = iwm_bus_enable(iwm);
635         if (ret) {
636                 IWM_ERR(iwm, "Couldn't enable function\n");
637                 return ret;
638         }
639
640         iwm_rx_setup_handlers(iwm);
641
642         /* Wait for initial BARKER_REBOOT from hardware */
643         notif_reboot = iwm_notif_wait(iwm, IWM_BARKER_REBOOT_NOTIFICATION,
644                                       IWM_SRC_UDMA, 2 * HZ);
645         if (!notif_reboot) {
646                 IWM_ERR(iwm, "Wait for REBOOT_BARKER timeout\n");
647                 goto err_disable;
648         }
649
650         /* We send the barker back */
651         ret = iwm_bus_send_chunk(iwm, notif_reboot->buf, 16);
652         if (ret) {
653                 IWM_ERR(iwm, "REBOOT barker response failed\n");
654                 kfree(notif_reboot);
655                 goto err_disable;
656         }
657
658         kfree(notif_reboot->buf);
659         kfree(notif_reboot);
660
661         /* Wait for ACK_BARKER from hardware */
662         notif_ack = iwm_notif_wait(iwm, IWM_ACK_BARKER_NOTIFICATION,
663                                    IWM_SRC_UDMA, 2 * HZ);
664         if (!notif_ack) {
665                 IWM_ERR(iwm, "Wait for ACK_BARKER timeout\n");
666                 goto err_disable;
667         }
668
669         kfree(notif_ack->buf);
670         kfree(notif_ack);
671
672         /* We start to config static boot parameters */
673         ret = iwm_config_boot_params(iwm);
674         if (ret) {
675                 IWM_ERR(iwm, "Config boot parameters failed\n");
676                 goto err_disable;
677         }
678
679         ret = iwm_read_mac(iwm, iwm_to_ndev(iwm)->dev_addr);
680         if (ret) {
681                 IWM_ERR(iwm, "MAC reading failed\n");
682                 goto err_disable;
683         }
684         memcpy(iwm_to_ndev(iwm)->perm_addr, iwm_to_ndev(iwm)->dev_addr,
685                 ETH_ALEN);
686
687         /* We can load the FWs */
688         ret = iwm_load_fw(iwm);
689         if (ret) {
690                 IWM_ERR(iwm, "FW loading failed\n");
691                 goto err_disable;
692         }
693
694         snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), "L%s_U%s",
695                  iwm->lmac_version, iwm->umac_version);
696
697         /* We configure the UMAC and enable the wifi module */
698         ret = iwm_send_umac_config(iwm,
699                         cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_CORE_EN) |
700                         cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_LINK_EN) |
701                         cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_MLME_EN));
702         if (ret) {
703                 IWM_ERR(iwm, "UMAC config failed\n");
704                 goto err_fw;
705         }
706
707         ret = iwm_notif_handle(iwm, UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS,
708                                IWM_SRC_UMAC, WAIT_NOTIF_TIMEOUT);
709         if (ret) {
710                 IWM_ERR(iwm, "Didn't get a wifi core status notification\n");
711                 goto err_fw;
712         }
713
714         if (iwm->core_enabled != (UMAC_NTFY_WIFI_CORE_STATUS_LINK_EN |
715                                   UMAC_NTFY_WIFI_CORE_STATUS_MLME_EN)) {
716                 IWM_DBG_BOOT(iwm, DBG, "Not all cores enabled:0x%x\n",
717                              iwm->core_enabled);
718                 ret = iwm_notif_handle(iwm, UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS,
719                                IWM_SRC_UMAC, WAIT_NOTIF_TIMEOUT);
720                 if (ret) {
721                         IWM_ERR(iwm, "Didn't get a core status notification\n");
722                         goto err_fw;
723                 }
724
725                 if (iwm->core_enabled != (UMAC_NTFY_WIFI_CORE_STATUS_LINK_EN |
726                                           UMAC_NTFY_WIFI_CORE_STATUS_MLME_EN)) {
727                         IWM_ERR(iwm, "Not all cores enabled: 0x%x\n",
728                                 iwm->core_enabled);
729                         goto err_fw;
730                 } else {
731                         IWM_INFO(iwm, "All cores enabled\n");
732                 }
733         }
734
735         ret = iwm_channels_init(iwm);
736         if (ret < 0) {
737                 IWM_ERR(iwm, "Couldn't init channels\n");
738                 goto err_fw;
739         }
740
741         /* Set the READY bit to indicate interface is brought up successfully */
742         set_bit(IWM_STATUS_READY, &iwm->status);
743
744         return 0;
745
746  err_fw:
747         iwm_eeprom_exit(iwm);
748
749  err_disable:
750         ret = iwm_bus_disable(iwm);
751         if (ret < 0)
752                 IWM_ERR(iwm, "Couldn't disable function\n");
753
754         return -EIO;
755 }
756
757 int iwm_up(struct iwm_priv *iwm)
758 {
759         int ret;
760
761         mutex_lock(&iwm->mutex);
762         ret = __iwm_up(iwm);
763         mutex_unlock(&iwm->mutex);
764
765         return ret;
766 }
767
768 static int __iwm_down(struct iwm_priv *iwm)
769 {
770         int ret;
771
772         /* The interface is already down */
773         if (!test_bit(IWM_STATUS_READY, &iwm->status))
774                 return 0;
775
776         if (iwm->scan_request) {
777                 cfg80211_scan_done(iwm->scan_request, true);
778                 iwm->scan_request = NULL;
779         }
780
781         clear_bit(IWM_STATUS_READY, &iwm->status);
782
783         iwm_eeprom_exit(iwm);
784         iwm_bss_list_clean(iwm);
785         iwm_init_default_profile(iwm, iwm->umac_profile);
786         iwm->umac_profile_active = false;
787         iwm->default_key = -1;
788         iwm->core_enabled = 0;
789
790         ret = iwm_bus_disable(iwm);
791         if (ret < 0) {
792                 IWM_ERR(iwm, "Couldn't disable function\n");
793                 return ret;
794         }
795
796         return 0;
797 }
798
799 int iwm_down(struct iwm_priv *iwm)
800 {
801         int ret;
802
803         mutex_lock(&iwm->mutex);
804         ret = __iwm_down(iwm);
805         mutex_unlock(&iwm->mutex);
806
807         return ret;
808 }