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