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