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