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