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 .init_calib_map
= BIT(PHY_CALIBRATE_DC_CMD
) |
57 BIT(PHY_CALIBRATE_LO_CMD
) |
58 BIT(PHY_CALIBRATE_TX_IQ_CMD
) |
59 BIT(PHY_CALIBRATE_RX_IQ_CMD
),
60 .periodic_calib_map
= BIT(PHY_CALIBRATE_DC_CMD
) |
61 BIT(PHY_CALIBRATE_LO_CMD
) |
62 BIT(PHY_CALIBRATE_TX_IQ_CMD
) |
63 BIT(PHY_CALIBRATE_RX_IQ_CMD
) |
64 BIT(SHILOH_PHY_CALIBRATE_BASE_BAND_CMD
),
65 .reset_on_fatal_err
= 1,
67 .wimax_not_present
= 0,
69 .mode
= UMAC_MODE_BSS
,
71 /* UMAC configuration */
73 .frag_threshold
= IEEE80211_MAX_FRAG_THRESHOLD
,
74 .rts_threshold
= IEEE80211_MAX_RTS_THRESHOLD
,
79 .wireless_mode
= WIRELESS_MODE_11A
| WIRELESS_MODE_11G
,
80 .coexist_mode
= COEX_MODE_CM
,
83 .ibss_band
= UMAC_BAND_2GHZ
,
86 .mac_addr
= {0x00, 0x02, 0xb3, 0x01, 0x02, 0x03},
89 static int modparam_reset
;
90 module_param_named(reset
, modparam_reset
, bool, 0644);
91 MODULE_PARM_DESC(reset
, "reset on firmware errors (default 0 [not reset])");
93 int iwm_mode_to_nl80211_iftype(int mode
)
97 return NL80211_IFTYPE_STATION
;
99 return NL80211_IFTYPE_ADHOC
;
101 return NL80211_IFTYPE_UNSPECIFIED
;
107 static void iwm_statistics_request(struct work_struct
*work
)
109 struct iwm_priv
*iwm
=
110 container_of(work
, struct iwm_priv
, stats_request
.work
);
112 iwm_send_umac_stats_req(iwm
, 0);
115 int __iwm_up(struct iwm_priv
*iwm
);
116 int __iwm_down(struct iwm_priv
*iwm
);
118 static void iwm_reset_worker(struct work_struct
*work
)
120 struct iwm_priv
*iwm
;
121 struct iwm_umac_profile
*profile
= NULL
;
122 int uninitialized_var(ret
), retry
= 0;
124 iwm
= container_of(work
, struct iwm_priv
, reset_worker
);
127 * XXX: The iwm->mutex is introduced purely for this reset work,
128 * because the other users for iwm_up and iwm_down are only netdev
129 * ndo_open and ndo_stop which are already protected by rtnl.
130 * Please remove iwm->mutex together if iwm_reset_worker() is not
131 * required in the future.
133 if (!mutex_trylock(&iwm
->mutex
)) {
134 IWM_WARN(iwm
, "We are in the middle of interface bringing "
135 "UP/DOWN. Skip driver resetting.\n");
139 if (iwm
->umac_profile_active
) {
140 profile
= kmalloc(sizeof(struct iwm_umac_profile
), GFP_KERNEL
);
142 memcpy(profile
, iwm
->umac_profile
, sizeof(*profile
));
144 IWM_ERR(iwm
, "Couldn't alloc memory for profile\n");
149 while (retry
++ < 3) {
154 schedule_timeout_uninterruptible(10 * HZ
);
158 IWM_WARN(iwm
, "iwm_up() failed: %d\n", ret
);
165 IWM_DBG_MLME(iwm
, DBG
, "Resend UMAC profile\n");
166 memcpy(iwm
->umac_profile
, profile
, sizeof(*profile
));
167 iwm_send_mlme_profile(iwm
);
172 mutex_unlock(&iwm
->mutex
);
175 static void iwm_watchdog(unsigned long data
)
177 struct iwm_priv
*iwm
= (struct iwm_priv
*)data
;
179 IWM_WARN(iwm
, "Watchdog expired: UMAC stalls!\n");
182 schedule_work(&iwm
->reset_worker
);
185 int iwm_priv_init(struct iwm_priv
*iwm
)
191 INIT_LIST_HEAD(&iwm
->pending_notif
);
192 init_waitqueue_head(&iwm
->notif_queue
);
193 init_waitqueue_head(&iwm
->nonwifi_queue
);
194 init_waitqueue_head(&iwm
->mlme_queue
);
195 memcpy(&iwm
->conf
, &def_iwm_conf
, sizeof(struct iwm_conf
));
196 spin_lock_init(&iwm
->tx_credit
.lock
);
197 INIT_LIST_HEAD(&iwm
->wifi_pending_cmd
);
198 INIT_LIST_HEAD(&iwm
->nonwifi_pending_cmd
);
199 iwm
->wifi_seq_num
= UMAC_WIFI_SEQ_NUM_BASE
;
200 iwm
->nonwifi_seq_num
= UMAC_NONWIFI_SEQ_NUM_BASE
;
201 spin_lock_init(&iwm
->cmd_lock
);
203 INIT_DELAYED_WORK(&iwm
->stats_request
, iwm_statistics_request
);
204 INIT_WORK(&iwm
->reset_worker
, iwm_reset_worker
);
205 INIT_LIST_HEAD(&iwm
->bss_list
);
207 skb_queue_head_init(&iwm
->rx_list
);
208 INIT_LIST_HEAD(&iwm
->rx_tickets
);
209 for (i
= 0; i
< IWM_RX_ID_HASH
; i
++)
210 INIT_LIST_HEAD(&iwm
->rx_packets
[i
]);
212 INIT_WORK(&iwm
->rx_worker
, iwm_rx_worker
);
214 iwm
->rx_wq
= create_singlethread_workqueue(KBUILD_MODNAME
"_rx");
218 for (i
= 0; i
< IWM_TX_QUEUES
; i
++) {
219 INIT_WORK(&iwm
->txq
[i
].worker
, iwm_tx_worker
);
220 snprintf(name
, 32, KBUILD_MODNAME
"_tx_%d", i
);
222 iwm
->txq
[i
].wq
= create_singlethread_workqueue(name
);
226 skb_queue_head_init(&iwm
->txq
[i
].queue
);
229 for (i
= 0; i
< IWM_NUM_KEYS
; i
++)
230 memset(&iwm
->keys
[i
], 0, sizeof(struct iwm_key
));
232 iwm
->default_key
= NULL
;
234 init_timer(&iwm
->watchdog
);
235 iwm
->watchdog
.function
= iwm_watchdog
;
236 iwm
->watchdog
.data
= (unsigned long)iwm
;
237 mutex_init(&iwm
->mutex
);
242 void iwm_priv_deinit(struct iwm_priv
*iwm
)
246 for (i
= 0; i
< IWM_TX_QUEUES
; i
++)
247 destroy_workqueue(iwm
->txq
[i
].wq
);
249 destroy_workqueue(iwm
->rx_wq
);
253 * We reset all the structures, and we reset the UMAC.
254 * After calling this routine, you're expected to reload
257 void iwm_reset(struct iwm_priv
*iwm
)
259 struct iwm_notif
*notif
, *next
;
261 if (test_bit(IWM_STATUS_READY
, &iwm
->status
))
262 iwm_target_reset(iwm
);
267 list_for_each_entry_safe(notif
, next
, &iwm
->pending_notif
, pending
) {
268 list_del(¬if
->pending
);
275 flush_workqueue(iwm
->rx_wq
);
283 * We're faced with the following issue: Any host command can
284 * have an answer or not, and if there's an answer to expect,
285 * it can be treated synchronously or asynchronously.
286 * To work around the synchronous answer case, we implemented
287 * our notification mechanism.
288 * When a code path needs to wait for a command response
289 * synchronously, it calls notif_handle(), which waits for the
290 * right notification to show up, and then process it. Before
291 * starting to wait, it registered as a waiter for this specific
292 * answer (by toggling a bit in on of the handler_map), so that
293 * the rx code knows that it needs to send a notification to the
294 * waiting processes. It does so by calling iwm_notif_send(),
295 * which adds the notification to the pending notifications list,
296 * and then wakes the waiting processes up.
298 int iwm_notif_send(struct iwm_priv
*iwm
, struct iwm_wifi_cmd
*cmd
,
299 u8 cmd_id
, u8 source
, u8
*buf
, unsigned long buf_size
)
301 struct iwm_notif
*notif
;
303 notif
= kzalloc(sizeof(struct iwm_notif
), GFP_KERNEL
);
305 IWM_ERR(iwm
, "Couldn't alloc memory for notification\n");
309 INIT_LIST_HEAD(¬if
->pending
);
311 notif
->cmd_id
= cmd_id
;
313 notif
->buf
= kzalloc(buf_size
, GFP_KERNEL
);
315 IWM_ERR(iwm
, "Couldn't alloc notification buffer\n");
319 notif
->buf_size
= buf_size
;
320 memcpy(notif
->buf
, buf
, buf_size
);
321 list_add_tail(¬if
->pending
, &iwm
->pending_notif
);
323 wake_up_interruptible(&iwm
->notif_queue
);
328 static struct iwm_notif
*iwm_notif_find(struct iwm_priv
*iwm
, u32 cmd
,
331 struct iwm_notif
*notif
, *next
;
333 list_for_each_entry_safe(notif
, next
, &iwm
->pending_notif
, pending
) {
334 if ((notif
->cmd_id
== cmd
) && (notif
->src
== source
)) {
335 list_del(¬if
->pending
);
343 static struct iwm_notif
*iwm_notif_wait(struct iwm_priv
*iwm
, u32 cmd
,
344 u8 source
, long timeout
)
347 struct iwm_notif
*notif
;
348 unsigned long *map
= NULL
;
352 map
= &iwm
->lmac_handler_map
[0];
355 map
= &iwm
->umac_handler_map
[0];
358 map
= &iwm
->udma_handler_map
[0];
364 ret
= wait_event_interruptible_timeout(iwm
->notif_queue
,
365 ((notif
= iwm_notif_find(iwm
, cmd
, source
)) != NULL
),
375 int iwm_notif_handle(struct iwm_priv
*iwm
, u32 cmd
, u8 source
, long timeout
)
378 struct iwm_notif
*notif
;
380 notif
= iwm_notif_wait(iwm
, cmd
, source
, timeout
);
384 ret
= iwm_rx_handle_resp(iwm
, notif
->buf
, notif
->buf_size
, notif
->cmd
);
391 static int iwm_config_boot_params(struct iwm_priv
*iwm
)
393 struct iwm_udma_nonwifi_cmd target_cmd
;
396 /* check Wimax is off and config debug monitor */
397 if (iwm
->conf
.wimax_not_present
) {
399 u32 addr1
= 0x606BE258;
403 u32 addr2
= 0x606BE100;
406 u32 addr3
= 0x606BEC00;
409 target_cmd
.handle_by_hw
= 0;
412 target_cmd
.opcode
= UMAC_HDI_OUT_OPCODE_WRITE
;
413 target_cmd
.addr
= cpu_to_le32(addr1
);
414 target_cmd
.op1_sz
= cpu_to_le32(sizeof(u32
));
417 ret
= iwm_hal_send_target_cmd(iwm
, &target_cmd
, &data1
);
419 IWM_ERR(iwm
, "iwm_hal_send_target_cmd failed\n");
423 target_cmd
.opcode
= UMAC_HDI_OUT_OPCODE_READ_MODIFY_WRITE
;
424 target_cmd
.addr
= cpu_to_le32(addr2
);
425 target_cmd
.op1_sz
= cpu_to_le32(data2_set
);
426 target_cmd
.op2
= cpu_to_le32(data2_clr
);
428 ret
= iwm_hal_send_target_cmd(iwm
, &target_cmd
, &data1
);
430 IWM_ERR(iwm
, "iwm_hal_send_target_cmd failed\n");
434 target_cmd
.opcode
= UMAC_HDI_OUT_OPCODE_WRITE
;
435 target_cmd
.addr
= cpu_to_le32(addr3
);
436 target_cmd
.op1_sz
= cpu_to_le32(sizeof(u32
));
439 ret
= iwm_hal_send_target_cmd(iwm
, &target_cmd
, &data3
);
441 IWM_ERR(iwm
, "iwm_hal_send_target_cmd failed\n");
449 void iwm_init_default_profile(struct iwm_priv
*iwm
,
450 struct iwm_umac_profile
*profile
)
452 memset(profile
, 0, sizeof(struct iwm_umac_profile
));
454 profile
->sec
.auth_type
= UMAC_AUTH_TYPE_OPEN
;
455 profile
->sec
.flags
= UMAC_SEC_FLG_LEGACY_PROFILE
;
456 profile
->sec
.ucast_cipher
= UMAC_CIPHER_TYPE_NONE
;
457 profile
->sec
.mcast_cipher
= UMAC_CIPHER_TYPE_NONE
;
459 if (iwm
->conf
.enable_qos
)
460 profile
->flags
|= cpu_to_le16(UMAC_PROFILE_QOS_ALLOWED
);
462 profile
->wireless_mode
= iwm
->conf
.wireless_mode
;
463 profile
->mode
= cpu_to_le32(iwm
->conf
.mode
);
465 profile
->ibss
.atim
= 0;
466 profile
->ibss
.beacon_interval
= 100;
467 profile
->ibss
.join_only
= 0;
468 profile
->ibss
.band
= iwm
->conf
.ibss_band
;
469 profile
->ibss
.channel
= iwm
->conf
.ibss_channel
;
472 void iwm_link_on(struct iwm_priv
*iwm
)
474 netif_carrier_on(iwm_to_ndev(iwm
));
475 netif_tx_wake_all_queues(iwm_to_ndev(iwm
));
477 iwm_send_umac_stats_req(iwm
, 0);
480 void iwm_link_off(struct iwm_priv
*iwm
)
482 struct iw_statistics
*wstats
= &iwm
->wstats
;
485 netif_tx_stop_all_queues(iwm_to_ndev(iwm
));
486 netif_carrier_off(iwm_to_ndev(iwm
));
488 for (i
= 0; i
< IWM_TX_QUEUES
; i
++) {
489 skb_queue_purge(&iwm
->txq
[i
].queue
);
491 iwm
->txq
[i
].concat_count
= 0;
492 iwm
->txq
[i
].concat_ptr
= iwm
->txq
[i
].concat_buf
;
494 flush_workqueue(iwm
->txq
[i
].wq
);
499 cancel_delayed_work_sync(&iwm
->stats_request
);
500 memset(wstats
, 0, sizeof(struct iw_statistics
));
501 wstats
->qual
.updated
= IW_QUAL_ALL_INVALID
;
503 del_timer_sync(&iwm
->watchdog
);
506 static void iwm_bss_list_clean(struct iwm_priv
*iwm
)
508 struct iwm_bss_info
*bss
, *next
;
510 list_for_each_entry_safe(bss
, next
, &iwm
->bss_list
, node
) {
511 list_del(&bss
->node
);
517 static int iwm_channels_init(struct iwm_priv
*iwm
)
521 #ifdef CONFIG_IWM_B0_HW_SUPPORT
522 if (iwm
->conf
.hw_b0
) {
523 IWM_INFO(iwm
, "Workaround EEPROM channels for B0 hardware\n");
528 ret
= iwm_send_umac_channel_list(iwm
);
530 IWM_ERR(iwm
, "Send channel list failed\n");
534 ret
= iwm_notif_handle(iwm
, UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST
,
535 IWM_SRC_UMAC
, WAIT_NOTIF_TIMEOUT
);
537 IWM_ERR(iwm
, "Didn't get a channel list notification\n");
544 int __iwm_up(struct iwm_priv
*iwm
)
547 struct iwm_notif
*notif_reboot
, *notif_ack
= NULL
;
549 ret
= iwm_bus_enable(iwm
);
551 IWM_ERR(iwm
, "Couldn't enable function\n");
555 iwm_rx_setup_handlers(iwm
);
557 /* Wait for initial BARKER_REBOOT from hardware */
558 notif_reboot
= iwm_notif_wait(iwm
, IWM_BARKER_REBOOT_NOTIFICATION
,
559 IWM_SRC_UDMA
, 2 * HZ
);
561 IWM_ERR(iwm
, "Wait for REBOOT_BARKER timeout\n");
565 /* We send the barker back */
566 ret
= iwm_bus_send_chunk(iwm
, notif_reboot
->buf
, 16);
568 IWM_ERR(iwm
, "REBOOT barker response failed\n");
573 kfree(notif_reboot
->buf
);
576 /* Wait for ACK_BARKER from hardware */
577 notif_ack
= iwm_notif_wait(iwm
, IWM_ACK_BARKER_NOTIFICATION
,
578 IWM_SRC_UDMA
, 2 * HZ
);
580 IWM_ERR(iwm
, "Wait for ACK_BARKER timeout\n");
584 kfree(notif_ack
->buf
);
587 /* We start to config static boot parameters */
588 ret
= iwm_config_boot_params(iwm
);
590 IWM_ERR(iwm
, "Config boot parameters failed\n");
594 ret
= iwm_read_mac(iwm
, iwm_to_ndev(iwm
)->dev_addr
);
596 IWM_ERR(iwm
, "MAC reading failed\n");
600 /* We can load the FWs */
601 ret
= iwm_load_fw(iwm
);
603 IWM_ERR(iwm
, "FW loading failed\n");
607 /* We configure the UMAC and enable the wifi module */
608 ret
= iwm_send_umac_config(iwm
,
609 cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_CORE_EN
) |
610 cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_LINK_EN
) |
611 cpu_to_le32(UMAC_RST_CTRL_FLG_WIFI_MLME_EN
));
613 IWM_ERR(iwm
, "UMAC config failed\n");
617 ret
= iwm_notif_handle(iwm
, UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS
,
618 IWM_SRC_UMAC
, WAIT_NOTIF_TIMEOUT
);
620 IWM_ERR(iwm
, "Didn't get a wifi core status notification\n");
624 if (iwm
->core_enabled
!= (UMAC_NTFY_WIFI_CORE_STATUS_LINK_EN
|
625 UMAC_NTFY_WIFI_CORE_STATUS_MLME_EN
)) {
626 IWM_DBG_BOOT(iwm
, DBG
, "Not all cores enabled:0x%x\n",
628 ret
= iwm_notif_handle(iwm
, UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS
,
629 IWM_SRC_UMAC
, WAIT_NOTIF_TIMEOUT
);
631 IWM_ERR(iwm
, "Didn't get a core status notification\n");
635 if (iwm
->core_enabled
!= (UMAC_NTFY_WIFI_CORE_STATUS_LINK_EN
|
636 UMAC_NTFY_WIFI_CORE_STATUS_MLME_EN
)) {
637 IWM_ERR(iwm
, "Not all cores enabled: 0x%x\n",
641 IWM_INFO(iwm
, "All cores enabled\n");
645 iwm
->umac_profile
= kmalloc(sizeof(struct iwm_umac_profile
),
647 if (!iwm
->umac_profile
) {
648 IWM_ERR(iwm
, "Couldn't alloc memory for profile\n");
652 iwm_init_default_profile(iwm
, iwm
->umac_profile
);
654 ret
= iwm_channels_init(iwm
);
656 IWM_ERR(iwm
, "Couldn't init channels\n");
660 /* Set the READY bit to indicate interface is brought up successfully */
661 set_bit(IWM_STATUS_READY
, &iwm
->status
);
666 kfree(iwm
->umac_profile
);
667 iwm
->umac_profile
= NULL
;
670 iwm_eeprom_exit(iwm
);
673 ret
= iwm_bus_disable(iwm
);
675 IWM_ERR(iwm
, "Couldn't disable function\n");
680 int iwm_up(struct iwm_priv
*iwm
)
684 mutex_lock(&iwm
->mutex
);
686 mutex_unlock(&iwm
->mutex
);
691 int __iwm_down(struct iwm_priv
*iwm
)
695 /* The interface is already down */
696 if (!test_bit(IWM_STATUS_READY
, &iwm
->status
))
699 if (iwm
->scan_request
) {
700 cfg80211_scan_done(iwm
->scan_request
, true);
701 iwm
->scan_request
= NULL
;
704 clear_bit(IWM_STATUS_READY
, &iwm
->status
);
706 iwm_eeprom_exit(iwm
);
707 kfree(iwm
->umac_profile
);
708 iwm
->umac_profile
= NULL
;
709 iwm_bss_list_clean(iwm
);
711 iwm
->default_key
= NULL
;
712 iwm
->core_enabled
= 0;
714 ret
= iwm_bus_disable(iwm
);
716 IWM_ERR(iwm
, "Couldn't disable function\n");
723 int iwm_down(struct iwm_priv
*iwm
)
727 mutex_lock(&iwm
->mutex
);
728 ret
= __iwm_down(iwm
);
729 mutex_unlock(&iwm
->mutex
);