2 * Intel Wireless Multicomm 3200 WiFi driver
4 * Copyright (C) 2009 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 * Intel Corporation <ilw@linux.intel.com>
34 * Samuel Ortiz <samuel.ortiz@intel.com>
35 * Zhu Yi <yi.zhu@intel.com>
39 #include <linux/kernel.h>
40 #include <linux/netdevice.h>
41 #include <linux/ieee80211.h>
42 #include <linux/wireless.h>
53 static struct iwm_conf def_iwm_conf = {
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),
68 .reset_on_fatal_err = 1,
70 .wimax_not_present = 0,
72 .mode = UMAC_MODE_BSS,
74 /* UMAC configuration */
76 .frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD,
77 .rts_threshold = IEEE80211_MAX_RTS_THRESHOLD,
82 .wireless_mode = WIRELESS_MODE_11A | WIRELESS_MODE_11G,
83 .coexist_mode = COEX_MODE_CM,
86 .ibss_band = UMAC_BAND_2GHZ,
89 .mac_addr = {0x00, 0x02, 0xb3, 0x01, 0x02, 0x03},
92 static int modparam_reset;
93 module_param_named(reset, modparam_reset, bool, 0644);
94 MODULE_PARM_DESC(reset, "reset on firmware errors (default 0 [not reset])");
96 int iwm_mode_to_nl80211_iftype(int mode)
100 return NL80211_IFTYPE_STATION;
102 return NL80211_IFTYPE_ADHOC;
104 return NL80211_IFTYPE_UNSPECIFIED;
110 static void iwm_statistics_request(struct work_struct *work)
112 struct iwm_priv *iwm =
113 container_of(work, struct iwm_priv, stats_request.work);
115 iwm_send_umac_stats_req(iwm, 0);
118 static void iwm_disconnect_work(struct work_struct *work)
120 struct iwm_priv *iwm =
121 container_of(work, struct iwm_priv, disconnect.work);
123 if (iwm->umac_profile_active)
124 iwm_invalidate_mlme_profile(iwm);
126 clear_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
127 iwm->umac_profile_active = 0;
128 memset(iwm->bssid, 0, ETH_ALEN);
133 wake_up_interruptible(&iwm->mlme_queue);
135 cfg80211_disconnected(iwm_to_ndev(iwm), 0, NULL, 0, GFP_KERNEL);
138 static void iwm_ct_kill_work(struct work_struct *work)
140 struct iwm_priv *iwm =
141 container_of(work, struct iwm_priv, ct_kill_delay.work);
142 struct wiphy *wiphy = iwm_to_wiphy(iwm);
144 IWM_INFO(iwm, "CT kill delay timeout\n");
146 wiphy_rfkill_set_hw_state(wiphy, false);
149 static int __iwm_up(struct iwm_priv *iwm);
150 static int __iwm_down(struct iwm_priv *iwm);
152 static void iwm_reset_worker(struct work_struct *work)
154 struct iwm_priv *iwm;
155 struct iwm_umac_profile *profile = NULL;
156 int uninitialized_var(ret), retry = 0;
158 iwm = container_of(work, struct iwm_priv, reset_worker);
161 * XXX: The iwm->mutex is introduced purely for this reset work,
162 * because the other users for iwm_up and iwm_down are only netdev
163 * ndo_open and ndo_stop which are already protected by rtnl.
164 * Please remove iwm->mutex together if iwm_reset_worker() is not
165 * required in the future.
167 if (!mutex_trylock(&iwm->mutex)) {
168 IWM_WARN(iwm, "We are in the middle of interface bringing "
169 "UP/DOWN. Skip driver resetting.\n");
173 if (iwm->umac_profile_active) {
174 profile = kmalloc(sizeof(struct iwm_umac_profile), GFP_KERNEL);
176 memcpy(profile, iwm->umac_profile, sizeof(*profile));
178 IWM_ERR(iwm, "Couldn't alloc memory for profile\n");
183 while (retry++ < 3) {
188 schedule_timeout_uninterruptible(10 * HZ);
192 IWM_WARN(iwm, "iwm_up() failed: %d\n", ret);
199 IWM_DBG_MLME(iwm, DBG, "Resend UMAC profile\n");
200 memcpy(iwm->umac_profile, profile, sizeof(*profile));
201 iwm_send_mlme_profile(iwm);
204 clear_bit(IWM_STATUS_RESETTING, &iwm->status);
207 mutex_unlock(&iwm->mutex);
210 static void iwm_auth_retry_worker(struct work_struct *work)
212 struct iwm_priv *iwm;
215 iwm = container_of(work, struct iwm_priv, auth_retry_worker);
216 if (iwm->umac_profile_active) {
217 ret = iwm_invalidate_mlme_profile(iwm);
222 iwm->umac_profile->sec.auth_type = UMAC_AUTH_TYPE_LEGACY_PSK;
224 ret = iwm_send_mlme_profile(iwm);
228 for (i = 0; i < IWM_NUM_KEYS; i++)
229 if (iwm->keys[i].key_len)
230 iwm_set_key(iwm, 0, &iwm->keys[i]);
232 iwm_set_tx_key(iwm, iwm->default_key);
237 static void iwm_watchdog(unsigned long data)
239 struct iwm_priv *iwm = (struct iwm_priv *)data;
241 IWM_WARN(iwm, "Watchdog expired: UMAC stalls!\n");
247 int iwm_priv_init(struct iwm_priv *iwm)
253 INIT_LIST_HEAD(&iwm->pending_notif);
254 init_waitqueue_head(&iwm->notif_queue);
255 init_waitqueue_head(&iwm->nonwifi_queue);
256 init_waitqueue_head(&iwm->wifi_ntfy_queue);
257 init_waitqueue_head(&iwm->mlme_queue);
258 memcpy(&iwm->conf, &def_iwm_conf, sizeof(struct iwm_conf));
259 spin_lock_init(&iwm->tx_credit.lock);
260 INIT_LIST_HEAD(&iwm->wifi_pending_cmd);
261 INIT_LIST_HEAD(&iwm->nonwifi_pending_cmd);
262 iwm->wifi_seq_num = UMAC_WIFI_SEQ_NUM_BASE;
263 iwm->nonwifi_seq_num = UMAC_NONWIFI_SEQ_NUM_BASE;
264 spin_lock_init(&iwm->cmd_lock);
266 INIT_DELAYED_WORK(&iwm->stats_request, iwm_statistics_request);
267 INIT_DELAYED_WORK(&iwm->disconnect, iwm_disconnect_work);
268 INIT_DELAYED_WORK(&iwm->ct_kill_delay, iwm_ct_kill_work);
269 INIT_WORK(&iwm->reset_worker, iwm_reset_worker);
270 INIT_WORK(&iwm->auth_retry_worker, iwm_auth_retry_worker);
271 INIT_LIST_HEAD(&iwm->bss_list);
273 skb_queue_head_init(&iwm->rx_list);
274 INIT_LIST_HEAD(&iwm->rx_tickets);
275 for (i = 0; i < IWM_RX_ID_HASH; i++)
276 INIT_LIST_HEAD(&iwm->rx_packets[i]);
278 INIT_WORK(&iwm->rx_worker, iwm_rx_worker);
280 iwm->rx_wq = create_singlethread_workqueue(KBUILD_MODNAME "_rx");
284 for (i = 0; i < IWM_TX_QUEUES; i++) {
285 INIT_WORK(&iwm->txq[i].worker, iwm_tx_worker);
286 snprintf(name, 32, KBUILD_MODNAME "_tx_%d", i);
288 iwm->txq[i].wq = create_singlethread_workqueue(name);
292 skb_queue_head_init(&iwm->txq[i].queue);
295 for (i = 0; i < IWM_NUM_KEYS; i++)
296 memset(&iwm->keys[i], 0, sizeof(struct iwm_key));
298 iwm->default_key = -1;
300 init_timer(&iwm->watchdog);
301 iwm->watchdog.function = iwm_watchdog;
302 iwm->watchdog.data = (unsigned long)iwm;
303 mutex_init(&iwm->mutex);
305 iwm->last_fw_err = kzalloc(sizeof(struct iwm_fw_error_hdr),
307 if (iwm->last_fw_err == NULL)
313 void iwm_priv_deinit(struct iwm_priv *iwm)
317 for (i = 0; i < IWM_TX_QUEUES; i++)
318 destroy_workqueue(iwm->txq[i].wq);
320 destroy_workqueue(iwm->rx_wq);
321 kfree(iwm->last_fw_err);
325 * We reset all the structures, and we reset the UMAC.
326 * After calling this routine, you're expected to reload
329 void iwm_reset(struct iwm_priv *iwm)
331 struct iwm_notif *notif, *next;
333 if (test_bit(IWM_STATUS_READY, &iwm->status))
334 iwm_target_reset(iwm);
336 if (test_bit(IWM_STATUS_RESETTING, &iwm->status)) {
338 set_bit(IWM_STATUS_RESETTING, &iwm->status);
343 list_for_each_entry_safe(notif, next, &iwm->pending_notif, pending) {
344 list_del(¬if->pending);
351 flush_workqueue(iwm->rx_wq);
356 void iwm_resetting(struct iwm_priv *iwm)
358 set_bit(IWM_STATUS_RESETTING, &iwm->status);
360 schedule_work(&iwm->reset_worker);
366 * We're faced with the following issue: Any host command can
367 * have an answer or not, and if there's an answer to expect,
368 * it can be treated synchronously or asynchronously.
369 * To work around the synchronous answer case, we implemented
370 * our notification mechanism.
371 * When a code path needs to wait for a command response
372 * synchronously, it calls notif_handle(), which waits for the
373 * right notification to show up, and then process it. Before
374 * starting to wait, it registered as a waiter for this specific
375 * answer (by toggling a bit in on of the handler_map), so that
376 * the rx code knows that it needs to send a notification to the
377 * waiting processes. It does so by calling iwm_notif_send(),
378 * which adds the notification to the pending notifications list,
379 * and then wakes the waiting processes up.
381 int iwm_notif_send(struct iwm_priv *iwm, struct iwm_wifi_cmd *cmd,
382 u8 cmd_id, u8 source, u8 *buf, unsigned long buf_size)
384 struct iwm_notif *notif;
386 notif = kzalloc(sizeof(struct iwm_notif), GFP_KERNEL);
388 IWM_ERR(iwm, "Couldn't alloc memory for notification\n");
392 INIT_LIST_HEAD(¬if->pending);
394 notif->cmd_id = cmd_id;
396 notif->buf = kzalloc(buf_size, GFP_KERNEL);
398 IWM_ERR(iwm, "Couldn't alloc notification buffer\n");
402 notif->buf_size = buf_size;
403 memcpy(notif->buf, buf, buf_size);
404 list_add_tail(¬if->pending, &iwm->pending_notif);
406 wake_up_interruptible(&iwm->notif_queue);
411 static struct iwm_notif *iwm_notif_find(struct iwm_priv *iwm, u32 cmd,
414 struct iwm_notif *notif, *next;
416 list_for_each_entry_safe(notif, next, &iwm->pending_notif, pending) {
417 if ((notif->cmd_id == cmd) && (notif->src == source)) {
418 list_del(¬if->pending);
426 static struct iwm_notif *iwm_notif_wait(struct iwm_priv *iwm, u32 cmd,
427 u8 source, long timeout)
430 struct iwm_notif *notif;
431 unsigned long *map = NULL;
435 map = &iwm->lmac_handler_map[0];
438 map = &iwm->umac_handler_map[0];
441 map = &iwm->udma_handler_map[0];
447 ret = wait_event_interruptible_timeout(iwm->notif_queue,
448 ((notif = iwm_notif_find(iwm, cmd, source)) != NULL),
458 int iwm_notif_handle(struct iwm_priv *iwm, u32 cmd, u8 source, long timeout)
461 struct iwm_notif *notif;
463 notif = iwm_notif_wait(iwm, cmd, source, timeout);
467 ret = iwm_rx_handle_resp(iwm, notif->buf, notif->buf_size, notif->cmd);
474 static int iwm_config_boot_params(struct iwm_priv *iwm)
476 struct iwm_udma_nonwifi_cmd target_cmd;
479 /* check Wimax is off and config debug monitor */
480 if (iwm->conf.wimax_not_present) {
482 u32 addr1 = 0x606BE258;
486 u32 addr2 = 0x606BE100;
489 u32 addr3 = 0x606BEC00;
492 target_cmd.handle_by_hw = 0;
495 target_cmd.opcode = UMAC_HDI_OUT_OPCODE_WRITE;
496 target_cmd.addr = cpu_to_le32(addr1);
497 target_cmd.op1_sz = cpu_to_le32(sizeof(u32));
500 ret = iwm_hal_send_target_cmd(iwm, &target_cmd, &data1);
502 IWM_ERR(iwm, "iwm_hal_send_target_cmd failed\n");
506 target_cmd.opcode = UMAC_HDI_OUT_OPCODE_READ_MODIFY_WRITE;
507 target_cmd.addr = cpu_to_le32(addr2);
508 target_cmd.op1_sz = cpu_to_le32(data2_set);
509 target_cmd.op2 = cpu_to_le32(data2_clr);
511 ret = iwm_hal_send_target_cmd(iwm, &target_cmd, &data1);
513 IWM_ERR(iwm, "iwm_hal_send_target_cmd failed\n");
517 target_cmd.opcode = UMAC_HDI_OUT_OPCODE_WRITE;
518 target_cmd.addr = cpu_to_le32(addr3);
519 target_cmd.op1_sz = cpu_to_le32(sizeof(u32));
522 ret = iwm_hal_send_target_cmd(iwm, &target_cmd, &data3);
524 IWM_ERR(iwm, "iwm_hal_send_target_cmd failed\n");
532 void iwm_init_default_profile(struct iwm_priv *iwm,
533 struct iwm_umac_profile *profile)
535 memset(profile, 0, sizeof(struct iwm_umac_profile));
537 profile->sec.auth_type = UMAC_AUTH_TYPE_OPEN;
538 profile->sec.flags = UMAC_SEC_FLG_LEGACY_PROFILE;
539 profile->sec.ucast_cipher = UMAC_CIPHER_TYPE_NONE;
540 profile->sec.mcast_cipher = UMAC_CIPHER_TYPE_NONE;
542 if (iwm->conf.enable_qos)
543 profile->flags |= cpu_to_le16(UMAC_PROFILE_QOS_ALLOWED);
545 profile->wireless_mode = iwm->conf.wireless_mode;
546 profile->mode = cpu_to_le32(iwm->conf.mode);
548 profile->ibss.atim = 0;
549 profile->ibss.beacon_interval = 100;
550 profile->ibss.join_only = 0;
551 profile->ibss.band = iwm->conf.ibss_band;
552 profile->ibss.channel = iwm->conf.ibss_channel;
555 void iwm_link_on(struct iwm_priv *iwm)
557 netif_carrier_on(iwm_to_ndev(iwm));
558 netif_tx_wake_all_queues(iwm_to_ndev(iwm));
560 iwm_send_umac_stats_req(iwm, 0);
563 void iwm_link_off(struct iwm_priv *iwm)
565 struct iw_statistics *wstats = &iwm->wstats;
568 netif_tx_stop_all_queues(iwm_to_ndev(iwm));
569 netif_carrier_off(iwm_to_ndev(iwm));
571 for (i = 0; i < IWM_TX_QUEUES; i++) {
572 skb_queue_purge(&iwm->txq[i].queue);
574 iwm->txq[i].concat_count = 0;
575 iwm->txq[i].concat_ptr = iwm->txq[i].concat_buf;
577 flush_workqueue(iwm->txq[i].wq);
582 cancel_delayed_work_sync(&iwm->stats_request);
583 memset(wstats, 0, sizeof(struct iw_statistics));
584 wstats->qual.updated = IW_QUAL_ALL_INVALID;
591 iwm->resp_ie_len = 0;
593 del_timer_sync(&iwm->watchdog);
596 static void iwm_bss_list_clean(struct iwm_priv *iwm)
598 struct iwm_bss_info *bss, *next;
600 list_for_each_entry_safe(bss, next, &iwm->bss_list, node) {
601 list_del(&bss->node);
607 static int iwm_channels_init(struct iwm_priv *iwm)
611 ret = iwm_send_umac_channel_list(iwm);
613 IWM_ERR(iwm, "Send channel list failed\n");
617 ret = iwm_notif_handle(iwm, UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST,
618 IWM_SRC_UMAC, WAIT_NOTIF_TIMEOUT);
620 IWM_ERR(iwm, "Didn't get a channel list notification\n");
627 static int __iwm_up(struct iwm_priv *iwm)
630 struct iwm_notif *notif_reboot, *notif_ack = NULL;
632 ret = iwm_bus_enable(iwm);
634 IWM_ERR(iwm, "Couldn't enable function\n");
638 iwm_rx_setup_handlers(iwm);
640 /* Wait for initial BARKER_REBOOT from hardware */
641 notif_reboot = iwm_notif_wait(iwm, IWM_BARKER_REBOOT_NOTIFICATION,
642 IWM_SRC_UDMA, 2 * HZ);
644 IWM_ERR(iwm, "Wait for REBOOT_BARKER timeout\n");
648 /* We send the barker back */
649 ret = iwm_bus_send_chunk(iwm, notif_reboot->buf, 16);
651 IWM_ERR(iwm, "REBOOT barker response failed\n");
656 kfree(notif_reboot->buf);
659 /* Wait for ACK_BARKER from hardware */
660 notif_ack = iwm_notif_wait(iwm, IWM_ACK_BARKER_NOTIFICATION,
661 IWM_SRC_UDMA, 2 * HZ);
663 IWM_ERR(iwm, "Wait for ACK_BARKER timeout\n");
667 kfree(notif_ack->buf);
670 /* We start to config static boot parameters */
671 ret = iwm_config_boot_params(iwm);
673 IWM_ERR(iwm, "Config boot parameters failed\n");
677 ret = iwm_read_mac(iwm, iwm_to_ndev(iwm)->dev_addr);
679 IWM_ERR(iwm, "MAC reading failed\n");
682 memcpy(iwm_to_ndev(iwm)->perm_addr, iwm_to_ndev(iwm)->dev_addr,
685 /* We can load the FWs */
686 ret = iwm_load_fw(iwm);
688 IWM_ERR(iwm, "FW loading failed\n");
692 /* We configure the UMAC and enable the wifi module */
693 ret = iwm_send_umac_config(iwm,
694 cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_CORE_EN) |
695 cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_LINK_EN) |
696 cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_MLME_EN));
698 IWM_ERR(iwm, "UMAC config failed\n");
702 ret = iwm_notif_handle(iwm, UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS,
703 IWM_SRC_UMAC, WAIT_NOTIF_TIMEOUT);
705 IWM_ERR(iwm, "Didn't get a wifi core status notification\n");
709 if (iwm->core_enabled != (UMAC_NTFY_WIFI_CORE_STATUS_LINK_EN |
710 UMAC_NTFY_WIFI_CORE_STATUS_MLME_EN)) {
711 IWM_DBG_BOOT(iwm, DBG, "Not all cores enabled:0x%x\n",
713 ret = iwm_notif_handle(iwm, UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS,
714 IWM_SRC_UMAC, WAIT_NOTIF_TIMEOUT);
716 IWM_ERR(iwm, "Didn't get a core status notification\n");
720 if (iwm->core_enabled != (UMAC_NTFY_WIFI_CORE_STATUS_LINK_EN |
721 UMAC_NTFY_WIFI_CORE_STATUS_MLME_EN)) {
722 IWM_ERR(iwm, "Not all cores enabled: 0x%x\n",
726 IWM_INFO(iwm, "All cores enabled\n");
730 ret = iwm_channels_init(iwm);
732 IWM_ERR(iwm, "Couldn't init channels\n");
736 /* Set the READY bit to indicate interface is brought up successfully */
737 set_bit(IWM_STATUS_READY, &iwm->status);
742 iwm_eeprom_exit(iwm);
745 ret = iwm_bus_disable(iwm);
747 IWM_ERR(iwm, "Couldn't disable function\n");
752 int iwm_up(struct iwm_priv *iwm)
756 mutex_lock(&iwm->mutex);
758 mutex_unlock(&iwm->mutex);
763 static int __iwm_down(struct iwm_priv *iwm)
767 /* The interface is already down */
768 if (!test_bit(IWM_STATUS_READY, &iwm->status))
771 if (iwm->scan_request) {
772 cfg80211_scan_done(iwm->scan_request, true);
773 iwm->scan_request = NULL;
776 clear_bit(IWM_STATUS_READY, &iwm->status);
778 iwm_eeprom_exit(iwm);
779 iwm_bss_list_clean(iwm);
780 iwm_init_default_profile(iwm, iwm->umac_profile);
781 iwm->umac_profile_active = false;
782 iwm->default_key = -1;
783 iwm->core_enabled = 0;
785 ret = iwm_bus_disable(iwm);
787 IWM_ERR(iwm, "Couldn't disable function\n");
794 int iwm_down(struct iwm_priv *iwm)
798 mutex_lock(&iwm->mutex);
799 ret = __iwm_down(iwm);
800 mutex_unlock(&iwm->mutex);