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
),
66 .reset_on_fatal_err
= 1,
68 .wimax_not_present
= 0,
70 .mode
= UMAC_MODE_BSS
,
72 /* UMAC configuration */
74 .frag_threshold
= IEEE80211_MAX_FRAG_THRESHOLD
,
75 .rts_threshold
= IEEE80211_MAX_RTS_THRESHOLD
,
80 .wireless_mode
= WIRELESS_MODE_11A
| WIRELESS_MODE_11G
,
81 .coexist_mode
= COEX_MODE_CM
,
84 .ibss_band
= UMAC_BAND_2GHZ
,
87 .mac_addr
= {0x00, 0x02, 0xb3, 0x01, 0x02, 0x03},
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])");
94 int iwm_mode_to_nl80211_iftype(int mode
)
98 return NL80211_IFTYPE_STATION
;
100 return NL80211_IFTYPE_ADHOC
;
102 return NL80211_IFTYPE_UNSPECIFIED
;
108 static void iwm_statistics_request(struct work_struct
*work
)
110 struct iwm_priv
*iwm
=
111 container_of(work
, struct iwm_priv
, stats_request
.work
);
113 iwm_send_umac_stats_req(iwm
, 0);
116 static void iwm_disconnect_work(struct work_struct
*work
)
118 struct iwm_priv
*iwm
=
119 container_of(work
, struct iwm_priv
, disconnect
.work
);
121 if (iwm
->umac_profile_active
)
122 iwm_invalidate_mlme_profile(iwm
);
124 clear_bit(IWM_STATUS_ASSOCIATED
, &iwm
->status
);
125 iwm
->umac_profile_active
= 0;
126 memset(iwm
->bssid
, 0, ETH_ALEN
);
131 wake_up_interruptible(&iwm
->mlme_queue
);
133 cfg80211_disconnected(iwm_to_ndev(iwm
), 0, NULL
, 0, GFP_KERNEL
);
136 static int __iwm_up(struct iwm_priv
*iwm
);
137 static int __iwm_down(struct iwm_priv
*iwm
);
139 static void iwm_reset_worker(struct work_struct
*work
)
141 struct iwm_priv
*iwm
;
142 struct iwm_umac_profile
*profile
= NULL
;
143 int uninitialized_var(ret
), retry
= 0;
145 iwm
= container_of(work
, struct iwm_priv
, reset_worker
);
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.
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");
160 if (iwm
->umac_profile_active
) {
161 profile
= kmalloc(sizeof(struct iwm_umac_profile
), GFP_KERNEL
);
163 memcpy(profile
, iwm
->umac_profile
, sizeof(*profile
));
165 IWM_ERR(iwm
, "Couldn't alloc memory for profile\n");
170 while (retry
++ < 3) {
175 schedule_timeout_uninterruptible(10 * HZ
);
179 IWM_WARN(iwm
, "iwm_up() failed: %d\n", ret
);
186 IWM_DBG_MLME(iwm
, DBG
, "Resend UMAC profile\n");
187 memcpy(iwm
->umac_profile
, profile
, sizeof(*profile
));
188 iwm_send_mlme_profile(iwm
);
191 clear_bit(IWM_STATUS_RESETTING
, &iwm
->status
);
194 mutex_unlock(&iwm
->mutex
);
197 static void iwm_watchdog(unsigned long data
)
199 struct iwm_priv
*iwm
= (struct iwm_priv
*)data
;
201 IWM_WARN(iwm
, "Watchdog expired: UMAC stalls!\n");
207 int iwm_priv_init(struct iwm_priv
*iwm
)
213 INIT_LIST_HEAD(&iwm
->pending_notif
);
214 init_waitqueue_head(&iwm
->notif_queue
);
215 init_waitqueue_head(&iwm
->nonwifi_queue
);
216 init_waitqueue_head(&iwm
->wifi_ntfy_queue
);
217 init_waitqueue_head(&iwm
->mlme_queue
);
218 memcpy(&iwm
->conf
, &def_iwm_conf
, sizeof(struct iwm_conf
));
219 spin_lock_init(&iwm
->tx_credit
.lock
);
220 INIT_LIST_HEAD(&iwm
->wifi_pending_cmd
);
221 INIT_LIST_HEAD(&iwm
->nonwifi_pending_cmd
);
222 iwm
->wifi_seq_num
= UMAC_WIFI_SEQ_NUM_BASE
;
223 iwm
->nonwifi_seq_num
= UMAC_NONWIFI_SEQ_NUM_BASE
;
224 spin_lock_init(&iwm
->cmd_lock
);
226 INIT_DELAYED_WORK(&iwm
->stats_request
, iwm_statistics_request
);
227 INIT_DELAYED_WORK(&iwm
->disconnect
, iwm_disconnect_work
);
228 INIT_WORK(&iwm
->reset_worker
, iwm_reset_worker
);
229 INIT_LIST_HEAD(&iwm
->bss_list
);
231 skb_queue_head_init(&iwm
->rx_list
);
232 INIT_LIST_HEAD(&iwm
->rx_tickets
);
233 for (i
= 0; i
< IWM_RX_ID_HASH
; i
++)
234 INIT_LIST_HEAD(&iwm
->rx_packets
[i
]);
236 INIT_WORK(&iwm
->rx_worker
, iwm_rx_worker
);
238 iwm
->rx_wq
= create_singlethread_workqueue(KBUILD_MODNAME
"_rx");
242 for (i
= 0; i
< IWM_TX_QUEUES
; i
++) {
243 INIT_WORK(&iwm
->txq
[i
].worker
, iwm_tx_worker
);
244 snprintf(name
, 32, KBUILD_MODNAME
"_tx_%d", i
);
246 iwm
->txq
[i
].wq
= create_singlethread_workqueue(name
);
250 skb_queue_head_init(&iwm
->txq
[i
].queue
);
253 for (i
= 0; i
< IWM_NUM_KEYS
; i
++)
254 memset(&iwm
->keys
[i
], 0, sizeof(struct iwm_key
));
256 iwm
->default_key
= -1;
258 init_timer(&iwm
->watchdog
);
259 iwm
->watchdog
.function
= iwm_watchdog
;
260 iwm
->watchdog
.data
= (unsigned long)iwm
;
261 mutex_init(&iwm
->mutex
);
263 iwm
->last_fw_err
= kzalloc(sizeof(struct iwm_fw_error_hdr
),
265 if (iwm
->last_fw_err
== NULL
)
271 void iwm_priv_deinit(struct iwm_priv
*iwm
)
275 for (i
= 0; i
< IWM_TX_QUEUES
; i
++)
276 destroy_workqueue(iwm
->txq
[i
].wq
);
278 destroy_workqueue(iwm
->rx_wq
);
279 kfree(iwm
->last_fw_err
);
283 * We reset all the structures, and we reset the UMAC.
284 * After calling this routine, you're expected to reload
287 void iwm_reset(struct iwm_priv
*iwm
)
289 struct iwm_notif
*notif
, *next
;
291 if (test_bit(IWM_STATUS_READY
, &iwm
->status
))
292 iwm_target_reset(iwm
);
294 if (test_bit(IWM_STATUS_RESETTING
, &iwm
->status
)) {
296 set_bit(IWM_STATUS_RESETTING
, &iwm
->status
);
301 list_for_each_entry_safe(notif
, next
, &iwm
->pending_notif
, pending
) {
302 list_del(¬if
->pending
);
309 flush_workqueue(iwm
->rx_wq
);
314 void iwm_resetting(struct iwm_priv
*iwm
)
316 set_bit(IWM_STATUS_RESETTING
, &iwm
->status
);
318 schedule_work(&iwm
->reset_worker
);
324 * We're faced with the following issue: Any host command can
325 * have an answer or not, and if there's an answer to expect,
326 * it can be treated synchronously or asynchronously.
327 * To work around the synchronous answer case, we implemented
328 * our notification mechanism.
329 * When a code path needs to wait for a command response
330 * synchronously, it calls notif_handle(), which waits for the
331 * right notification to show up, and then process it. Before
332 * starting to wait, it registered as a waiter for this specific
333 * answer (by toggling a bit in on of the handler_map), so that
334 * the rx code knows that it needs to send a notification to the
335 * waiting processes. It does so by calling iwm_notif_send(),
336 * which adds the notification to the pending notifications list,
337 * and then wakes the waiting processes up.
339 int iwm_notif_send(struct iwm_priv
*iwm
, struct iwm_wifi_cmd
*cmd
,
340 u8 cmd_id
, u8 source
, u8
*buf
, unsigned long buf_size
)
342 struct iwm_notif
*notif
;
344 notif
= kzalloc(sizeof(struct iwm_notif
), GFP_KERNEL
);
346 IWM_ERR(iwm
, "Couldn't alloc memory for notification\n");
350 INIT_LIST_HEAD(¬if
->pending
);
352 notif
->cmd_id
= cmd_id
;
354 notif
->buf
= kzalloc(buf_size
, GFP_KERNEL
);
356 IWM_ERR(iwm
, "Couldn't alloc notification buffer\n");
360 notif
->buf_size
= buf_size
;
361 memcpy(notif
->buf
, buf
, buf_size
);
362 list_add_tail(¬if
->pending
, &iwm
->pending_notif
);
364 wake_up_interruptible(&iwm
->notif_queue
);
369 static struct iwm_notif
*iwm_notif_find(struct iwm_priv
*iwm
, u32 cmd
,
372 struct iwm_notif
*notif
, *next
;
374 list_for_each_entry_safe(notif
, next
, &iwm
->pending_notif
, pending
) {
375 if ((notif
->cmd_id
== cmd
) && (notif
->src
== source
)) {
376 list_del(¬if
->pending
);
384 static struct iwm_notif
*iwm_notif_wait(struct iwm_priv
*iwm
, u32 cmd
,
385 u8 source
, long timeout
)
388 struct iwm_notif
*notif
;
389 unsigned long *map
= NULL
;
393 map
= &iwm
->lmac_handler_map
[0];
396 map
= &iwm
->umac_handler_map
[0];
399 map
= &iwm
->udma_handler_map
[0];
405 ret
= wait_event_interruptible_timeout(iwm
->notif_queue
,
406 ((notif
= iwm_notif_find(iwm
, cmd
, source
)) != NULL
),
416 int iwm_notif_handle(struct iwm_priv
*iwm
, u32 cmd
, u8 source
, long timeout
)
419 struct iwm_notif
*notif
;
421 notif
= iwm_notif_wait(iwm
, cmd
, source
, timeout
);
425 ret
= iwm_rx_handle_resp(iwm
, notif
->buf
, notif
->buf_size
, notif
->cmd
);
432 static int iwm_config_boot_params(struct iwm_priv
*iwm
)
434 struct iwm_udma_nonwifi_cmd target_cmd
;
437 /* check Wimax is off and config debug monitor */
438 if (iwm
->conf
.wimax_not_present
) {
440 u32 addr1
= 0x606BE258;
444 u32 addr2
= 0x606BE100;
447 u32 addr3
= 0x606BEC00;
450 target_cmd
.handle_by_hw
= 0;
453 target_cmd
.opcode
= UMAC_HDI_OUT_OPCODE_WRITE
;
454 target_cmd
.addr
= cpu_to_le32(addr1
);
455 target_cmd
.op1_sz
= cpu_to_le32(sizeof(u32
));
458 ret
= iwm_hal_send_target_cmd(iwm
, &target_cmd
, &data1
);
460 IWM_ERR(iwm
, "iwm_hal_send_target_cmd failed\n");
464 target_cmd
.opcode
= UMAC_HDI_OUT_OPCODE_READ_MODIFY_WRITE
;
465 target_cmd
.addr
= cpu_to_le32(addr2
);
466 target_cmd
.op1_sz
= cpu_to_le32(data2_set
);
467 target_cmd
.op2
= cpu_to_le32(data2_clr
);
469 ret
= iwm_hal_send_target_cmd(iwm
, &target_cmd
, &data1
);
471 IWM_ERR(iwm
, "iwm_hal_send_target_cmd failed\n");
475 target_cmd
.opcode
= UMAC_HDI_OUT_OPCODE_WRITE
;
476 target_cmd
.addr
= cpu_to_le32(addr3
);
477 target_cmd
.op1_sz
= cpu_to_le32(sizeof(u32
));
480 ret
= iwm_hal_send_target_cmd(iwm
, &target_cmd
, &data3
);
482 IWM_ERR(iwm
, "iwm_hal_send_target_cmd failed\n");
490 void iwm_init_default_profile(struct iwm_priv
*iwm
,
491 struct iwm_umac_profile
*profile
)
493 memset(profile
, 0, sizeof(struct iwm_umac_profile
));
495 profile
->sec
.auth_type
= UMAC_AUTH_TYPE_OPEN
;
496 profile
->sec
.flags
= UMAC_SEC_FLG_LEGACY_PROFILE
;
497 profile
->sec
.ucast_cipher
= UMAC_CIPHER_TYPE_NONE
;
498 profile
->sec
.mcast_cipher
= UMAC_CIPHER_TYPE_NONE
;
500 if (iwm
->conf
.enable_qos
)
501 profile
->flags
|= cpu_to_le16(UMAC_PROFILE_QOS_ALLOWED
);
503 profile
->wireless_mode
= iwm
->conf
.wireless_mode
;
504 profile
->mode
= cpu_to_le32(iwm
->conf
.mode
);
506 profile
->ibss
.atim
= 0;
507 profile
->ibss
.beacon_interval
= 100;
508 profile
->ibss
.join_only
= 0;
509 profile
->ibss
.band
= iwm
->conf
.ibss_band
;
510 profile
->ibss
.channel
= iwm
->conf
.ibss_channel
;
513 void iwm_link_on(struct iwm_priv
*iwm
)
515 netif_carrier_on(iwm_to_ndev(iwm
));
516 netif_tx_wake_all_queues(iwm_to_ndev(iwm
));
518 iwm_send_umac_stats_req(iwm
, 0);
521 void iwm_link_off(struct iwm_priv
*iwm
)
523 struct iw_statistics
*wstats
= &iwm
->wstats
;
526 netif_tx_stop_all_queues(iwm_to_ndev(iwm
));
527 netif_carrier_off(iwm_to_ndev(iwm
));
529 for (i
= 0; i
< IWM_TX_QUEUES
; i
++) {
530 skb_queue_purge(&iwm
->txq
[i
].queue
);
532 iwm
->txq
[i
].concat_count
= 0;
533 iwm
->txq
[i
].concat_ptr
= iwm
->txq
[i
].concat_buf
;
535 flush_workqueue(iwm
->txq
[i
].wq
);
540 cancel_delayed_work_sync(&iwm
->stats_request
);
541 memset(wstats
, 0, sizeof(struct iw_statistics
));
542 wstats
->qual
.updated
= IW_QUAL_ALL_INVALID
;
549 iwm
->resp_ie_len
= 0;
551 del_timer_sync(&iwm
->watchdog
);
554 static void iwm_bss_list_clean(struct iwm_priv
*iwm
)
556 struct iwm_bss_info
*bss
, *next
;
558 list_for_each_entry_safe(bss
, next
, &iwm
->bss_list
, node
) {
559 list_del(&bss
->node
);
565 static int iwm_channels_init(struct iwm_priv
*iwm
)
569 ret
= iwm_send_umac_channel_list(iwm
);
571 IWM_ERR(iwm
, "Send channel list failed\n");
575 ret
= iwm_notif_handle(iwm
, UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST
,
576 IWM_SRC_UMAC
, WAIT_NOTIF_TIMEOUT
);
578 IWM_ERR(iwm
, "Didn't get a channel list notification\n");
585 static int __iwm_up(struct iwm_priv
*iwm
)
588 struct iwm_notif
*notif_reboot
, *notif_ack
= NULL
;
590 ret
= iwm_bus_enable(iwm
);
592 IWM_ERR(iwm
, "Couldn't enable function\n");
596 iwm_rx_setup_handlers(iwm
);
598 /* Wait for initial BARKER_REBOOT from hardware */
599 notif_reboot
= iwm_notif_wait(iwm
, IWM_BARKER_REBOOT_NOTIFICATION
,
600 IWM_SRC_UDMA
, 2 * HZ
);
602 IWM_ERR(iwm
, "Wait for REBOOT_BARKER timeout\n");
606 /* We send the barker back */
607 ret
= iwm_bus_send_chunk(iwm
, notif_reboot
->buf
, 16);
609 IWM_ERR(iwm
, "REBOOT barker response failed\n");
614 kfree(notif_reboot
->buf
);
617 /* Wait for ACK_BARKER from hardware */
618 notif_ack
= iwm_notif_wait(iwm
, IWM_ACK_BARKER_NOTIFICATION
,
619 IWM_SRC_UDMA
, 2 * HZ
);
621 IWM_ERR(iwm
, "Wait for ACK_BARKER timeout\n");
625 kfree(notif_ack
->buf
);
628 /* We start to config static boot parameters */
629 ret
= iwm_config_boot_params(iwm
);
631 IWM_ERR(iwm
, "Config boot parameters failed\n");
635 ret
= iwm_read_mac(iwm
, iwm_to_ndev(iwm
)->dev_addr
);
637 IWM_ERR(iwm
, "MAC reading failed\n");
641 /* We can load the FWs */
642 ret
= iwm_load_fw(iwm
);
644 IWM_ERR(iwm
, "FW loading failed\n");
648 /* We configure the UMAC and enable the wifi module */
649 ret
= iwm_send_umac_config(iwm
,
650 cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_CORE_EN
) |
651 cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_LINK_EN
) |
652 cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_MLME_EN
));
654 IWM_ERR(iwm
, "UMAC config failed\n");
658 ret
= iwm_notif_handle(iwm
, UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS
,
659 IWM_SRC_UMAC
, WAIT_NOTIF_TIMEOUT
);
661 IWM_ERR(iwm
, "Didn't get a wifi core status notification\n");
665 if (iwm
->core_enabled
!= (UMAC_NTFY_WIFI_CORE_STATUS_LINK_EN
|
666 UMAC_NTFY_WIFI_CORE_STATUS_MLME_EN
)) {
667 IWM_DBG_BOOT(iwm
, DBG
, "Not all cores enabled:0x%x\n",
669 ret
= iwm_notif_handle(iwm
, UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS
,
670 IWM_SRC_UMAC
, WAIT_NOTIF_TIMEOUT
);
672 IWM_ERR(iwm
, "Didn't get a core status notification\n");
676 if (iwm
->core_enabled
!= (UMAC_NTFY_WIFI_CORE_STATUS_LINK_EN
|
677 UMAC_NTFY_WIFI_CORE_STATUS_MLME_EN
)) {
678 IWM_ERR(iwm
, "Not all cores enabled: 0x%x\n",
682 IWM_INFO(iwm
, "All cores enabled\n");
686 ret
= iwm_channels_init(iwm
);
688 IWM_ERR(iwm
, "Couldn't init channels\n");
692 /* Set the READY bit to indicate interface is brought up successfully */
693 set_bit(IWM_STATUS_READY
, &iwm
->status
);
698 iwm_eeprom_exit(iwm
);
701 ret
= iwm_bus_disable(iwm
);
703 IWM_ERR(iwm
, "Couldn't disable function\n");
708 int iwm_up(struct iwm_priv
*iwm
)
712 mutex_lock(&iwm
->mutex
);
714 mutex_unlock(&iwm
->mutex
);
719 static int __iwm_down(struct iwm_priv
*iwm
)
723 /* The interface is already down */
724 if (!test_bit(IWM_STATUS_READY
, &iwm
->status
))
727 if (iwm
->scan_request
) {
728 cfg80211_scan_done(iwm
->scan_request
, true);
729 iwm
->scan_request
= NULL
;
732 clear_bit(IWM_STATUS_READY
, &iwm
->status
);
734 iwm_eeprom_exit(iwm
);
735 iwm_bss_list_clean(iwm
);
736 iwm_init_default_profile(iwm
, iwm
->umac_profile
);
737 iwm
->umac_profile_active
= false;
738 iwm
->default_key
= -1;
739 iwm
->core_enabled
= 0;
741 ret
= iwm_bus_disable(iwm
);
743 IWM_ERR(iwm
, "Couldn't disable function\n");
750 int iwm_down(struct iwm_priv
*iwm
)
754 mutex_lock(&iwm
->mutex
);
755 ret
= __iwm_down(iwm
);
756 mutex_unlock(&iwm
->mutex
);