1 /******************************************************************************
3 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *****************************************************************************/
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/pci.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/delay.h>
36 #include <linux/skbuff.h>
37 #include <linux/netdevice.h>
38 #include <linux/wireless.h>
39 #include <linux/firmware.h>
40 #include <linux/etherdevice.h>
41 #include <linux/if_arp.h>
43 #include <net/ieee80211_radiotap.h>
44 #include <net/lib80211.h>
45 #include <net/mac80211.h>
47 #include <asm/div64.h>
49 #define DRV_NAME "iwl3945"
52 #include "iwl-3945-fh.h"
53 #include "iwl-commands.h"
56 #include "iwl-helpers.h"
61 * module name, copyright, version, etc.
64 #define DRV_DESCRIPTION \
65 "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
67 #ifdef CONFIG_IWLWIFI_DEBUG
73 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
79 #define IWL39_VERSION "1.2.26k" VD VS
80 #define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
81 #define DRV_AUTHOR "<ilw@linux.intel.com>"
82 #define DRV_VERSION IWL39_VERSION
85 MODULE_DESCRIPTION(DRV_DESCRIPTION);
86 MODULE_VERSION(DRV_VERSION);
87 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
88 MODULE_LICENSE("GPL");
90 /* module parameters */
91 struct iwl_mod_params iwl3945_mod_params = {
92 .num_of_queues = IWL39_MAX_NUM_QUEUES,
95 /* the rest are 0 by default */
99 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
100 * @priv: eeprom and antenna fields are used to determine antenna flags
102 * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
103 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
105 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
106 * IWL_ANTENNA_MAIN - Force MAIN antenna
107 * IWL_ANTENNA_AUX - Force AUX antenna
109 __le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
111 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
113 switch (iwl3945_mod_params.antenna) {
114 case IWL_ANTENNA_DIVERSITY:
117 case IWL_ANTENNA_MAIN:
118 if (eeprom->antenna_switch_type)
119 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
120 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
122 case IWL_ANTENNA_AUX:
123 if (eeprom->antenna_switch_type)
124 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
125 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
128 /* bad antenna selector value */
129 IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
130 iwl3945_mod_params.antenna);
132 return 0; /* "diversity" is default if error */
135 static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
136 struct ieee80211_key_conf *keyconf,
140 __le16 key_flags = 0;
143 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
144 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
146 if (sta_id == priv->hw_params.bcast_sta_id)
147 key_flags |= STA_KEY_MULTICAST_MSK;
149 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
150 keyconf->hw_key_idx = keyconf->keyidx;
151 key_flags &= ~STA_KEY_FLG_INVALID;
153 spin_lock_irqsave(&priv->sta_lock, flags);
154 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
155 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
156 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
159 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
162 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
163 == STA_KEY_FLG_NO_ENC)
164 priv->stations[sta_id].sta.key.key_offset =
165 iwl_get_free_ucode_key_index(priv);
166 /* else, we are overriding an existing key => no need to allocated room
169 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
170 "no space for a new key");
172 priv->stations[sta_id].sta.key.key_flags = key_flags;
173 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
174 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
176 IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
178 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
180 spin_unlock_irqrestore(&priv->sta_lock, flags);
185 static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
186 struct ieee80211_key_conf *keyconf,
192 static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
193 struct ieee80211_key_conf *keyconf,
199 static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
203 spin_lock_irqsave(&priv->sta_lock, flags);
204 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
205 memset(&priv->stations[sta_id].sta.key, 0,
206 sizeof(struct iwl4965_keyinfo));
207 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
208 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
209 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
210 spin_unlock_irqrestore(&priv->sta_lock, flags);
212 IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
213 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, 0);
217 static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
218 struct ieee80211_key_conf *keyconf, u8 sta_id)
222 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
224 switch (keyconf->alg) {
226 ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
229 ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
232 ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
235 IWL_ERR(priv, "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
239 IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
240 keyconf->alg, keyconf->keylen, keyconf->keyidx,
246 static int iwl3945_remove_static_key(struct iwl_priv *priv)
248 int ret = -EOPNOTSUPP;
253 static int iwl3945_set_static_key(struct iwl_priv *priv,
254 struct ieee80211_key_conf *key)
256 if (key->alg == ALG_WEP)
259 IWL_ERR(priv, "Static key invalid: alg %d\n", key->alg);
263 static void iwl3945_clear_free_frames(struct iwl_priv *priv)
265 struct list_head *element;
267 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
270 while (!list_empty(&priv->free_frames)) {
271 element = priv->free_frames.next;
273 kfree(list_entry(element, struct iwl3945_frame, list));
274 priv->frames_count--;
277 if (priv->frames_count) {
278 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
280 priv->frames_count = 0;
284 static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
286 struct iwl3945_frame *frame;
287 struct list_head *element;
288 if (list_empty(&priv->free_frames)) {
289 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
291 IWL_ERR(priv, "Could not allocate frame!\n");
295 priv->frames_count++;
299 element = priv->free_frames.next;
301 return list_entry(element, struct iwl3945_frame, list);
304 static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
306 memset(frame, 0, sizeof(*frame));
307 list_add(&frame->list, &priv->free_frames);
310 unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
311 struct ieee80211_hdr *hdr,
315 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
316 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
317 (priv->iw_mode != NL80211_IFTYPE_AP)))
320 if (priv->ibss_beacon->len > left)
323 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
325 return priv->ibss_beacon->len;
328 static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
330 struct iwl3945_frame *frame;
331 unsigned int frame_size;
335 frame = iwl3945_get_free_frame(priv);
338 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
343 rate = iwl_rate_get_lowest_plcp(priv);
345 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
347 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
350 iwl3945_free_frame(priv, frame);
355 static void iwl3945_unset_hw_params(struct iwl_priv *priv)
357 if (priv->shared_virt)
358 pci_free_consistent(priv->pci_dev,
359 sizeof(struct iwl3945_shared),
364 #define MAX_UCODE_BEACON_INTERVAL 1024
365 #define INTEL_CONN_LISTEN_INTERVAL cpu_to_le16(0xA)
367 static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
370 u16 beacon_factor = 0;
373 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
374 / MAX_UCODE_BEACON_INTERVAL;
375 new_val = beacon_val / beacon_factor;
377 return cpu_to_le16(new_val);
380 static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
382 u64 interval_tm_unit;
385 struct ieee80211_conf *conf = NULL;
388 conf = ieee80211_get_hw_conf(priv->hw);
390 spin_lock_irqsave(&priv->lock, flags);
391 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
392 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
394 tsf = priv->timestamp;
396 beacon_int = priv->beacon_int;
397 spin_unlock_irqrestore(&priv->lock, flags);
399 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
400 if (beacon_int == 0) {
401 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
402 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
404 priv->rxon_timing.beacon_interval =
405 cpu_to_le16(beacon_int);
406 priv->rxon_timing.beacon_interval =
407 iwl3945_adjust_beacon_interval(
408 le16_to_cpu(priv->rxon_timing.beacon_interval));
411 priv->rxon_timing.atim_window = 0;
413 priv->rxon_timing.beacon_interval =
414 iwl3945_adjust_beacon_interval(
415 priv->vif->bss_conf.beacon_int);
416 /* TODO: we need to get atim_window from upper stack
417 * for now we set to 0 */
418 priv->rxon_timing.atim_window = 0;
422 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
423 result = do_div(tsf, interval_tm_unit);
424 priv->rxon_timing.beacon_init_val =
425 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
427 IWL_DEBUG_ASSOC(priv,
428 "beacon interval %d beacon timer %d beacon tim %d\n",
429 le16_to_cpu(priv->rxon_timing.beacon_interval),
430 le32_to_cpu(priv->rxon_timing.beacon_init_val),
431 le16_to_cpu(priv->rxon_timing.atim_window));
434 static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
435 struct ieee80211_tx_info *info,
437 struct sk_buff *skb_frag,
440 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
441 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
443 switch (keyinfo->alg) {
445 tx->sec_ctl = TX_CMD_SEC_CCM;
446 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
447 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
454 tx->sec_ctl = TX_CMD_SEC_WEP |
455 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
457 if (keyinfo->keylen == 13)
458 tx->sec_ctl |= TX_CMD_SEC_KEY128;
460 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
462 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
463 "with key %d\n", info->control.hw_key->hw_key_idx);
467 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
473 * handle build REPLY_TX command notification.
475 static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
477 struct ieee80211_tx_info *info,
478 struct ieee80211_hdr *hdr, u8 std_id)
480 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
481 __le32 tx_flags = tx->tx_flags;
482 __le16 fc = hdr->frame_control;
483 u8 rc_flags = info->control.rates[0].flags;
485 tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
486 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
487 tx_flags |= TX_CMD_FLG_ACK_MSK;
488 if (ieee80211_is_mgmt(fc))
489 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
490 if (ieee80211_is_probe_resp(fc) &&
491 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
492 tx_flags |= TX_CMD_FLG_TSF_MSK;
494 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
495 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
499 if (ieee80211_has_morefrags(fc))
500 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
502 if (ieee80211_is_data_qos(fc)) {
503 u8 *qc = ieee80211_get_qos_ctl(hdr);
504 tx->tid_tspec = qc[0] & 0xf;
505 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
507 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
510 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
511 tx_flags |= TX_CMD_FLG_RTS_MSK;
512 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
513 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
514 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
515 tx_flags |= TX_CMD_FLG_CTS_MSK;
518 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
519 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
521 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
522 if (ieee80211_is_mgmt(fc)) {
523 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
524 tx->timeout.pm_frame_timeout = cpu_to_le16(3);
526 tx->timeout.pm_frame_timeout = cpu_to_le16(2);
528 tx->timeout.pm_frame_timeout = 0;
529 #ifdef CONFIG_IWLWIFI_LEDS
530 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
535 tx->tx_flags = tx_flags;
536 tx->next_frame_len = 0;
540 * start REPLY_TX command process
542 static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
544 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
545 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
546 struct iwl3945_tx_cmd *tx;
547 struct iwl_tx_queue *txq = NULL;
548 struct iwl_queue *q = NULL;
549 struct iwl_cmd *out_cmd = NULL;
550 dma_addr_t phys_addr;
551 dma_addr_t txcmd_phys;
552 int txq_id = skb_get_queue_mapping(skb);
553 u16 len, idx, len_org, hdr_len; /* TODO: len_org is not used */
560 u8 wait_write_ptr = 0;
565 spin_lock_irqsave(&priv->lock, flags);
566 if (iwl_is_rfkill(priv)) {
567 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
571 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
572 IWL_ERR(priv, "ERROR: No TX rate available.\n");
576 unicast = !is_multicast_ether_addr(hdr->addr1);
579 fc = hdr->frame_control;
581 #ifdef CONFIG_IWLWIFI_DEBUG
582 if (ieee80211_is_auth(fc))
583 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
584 else if (ieee80211_is_assoc_req(fc))
585 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
586 else if (ieee80211_is_reassoc_req(fc))
587 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
590 /* drop all data frame if we are not associated */
591 if (ieee80211_is_data(fc) &&
592 (!iwl_is_monitor_mode(priv)) && /* packet injection */
593 (!iwl_is_associated(priv) ||
594 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
595 IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
599 spin_unlock_irqrestore(&priv->lock, flags);
601 hdr_len = ieee80211_hdrlen(fc);
603 /* Find (or create) index into station table for destination station */
604 sta_id = iwl_get_sta_id(priv, hdr);
605 if (sta_id == IWL_INVALID_STATION) {
606 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
611 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
613 if (ieee80211_is_data_qos(fc)) {
614 qc = ieee80211_get_qos_ctl(hdr);
615 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
616 seq_number = priv->stations[sta_id].tid[tid].seq_number &
618 hdr->seq_ctrl = cpu_to_le16(seq_number) |
620 cpu_to_le16(IEEE80211_SCTL_FRAG));
624 /* Descriptor for chosen Tx queue */
625 txq = &priv->txq[txq_id];
628 spin_lock_irqsave(&priv->lock, flags);
630 idx = get_cmd_index(q, q->write_ptr, 0);
632 /* Set up driver data for this TFD */
633 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
634 txq->txb[q->write_ptr].skb[0] = skb;
636 /* Init first empty entry in queue's array of Tx/cmd buffers */
637 out_cmd = txq->cmd[idx];
638 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
639 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
640 memset(tx, 0, sizeof(*tx));
643 * Set up the Tx-command (not MAC!) header.
644 * Store the chosen Tx queue and TFD index within the sequence field;
645 * after Tx, uCode's Tx response will return this value so driver can
646 * locate the frame within the tx queue and do post-tx processing.
648 out_cmd->hdr.cmd = REPLY_TX;
649 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
650 INDEX_TO_SEQ(q->write_ptr)));
652 /* Copy MAC header from skb into command buffer */
653 memcpy(tx->hdr, hdr, hdr_len);
656 if (info->control.hw_key)
657 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
659 /* TODO need this for burst mode later on */
660 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
662 /* set is_hcca to 0; it probably will never be implemented */
663 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
665 /* Total # bytes to be transmitted */
667 tx->len = cpu_to_le16(len);
670 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
671 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
673 if (!ieee80211_has_morefrags(hdr->frame_control)) {
674 txq->need_update = 1;
676 priv->stations[sta_id].tid[tid].seq_number = seq_number;
679 txq->need_update = 0;
682 IWL_DEBUG_TX(priv, "sequence nr = 0X%x \n",
683 le16_to_cpu(out_cmd->hdr.sequence));
684 IWL_DEBUG_TX(priv, "tx_flags = 0X%x \n", le32_to_cpu(tx->tx_flags));
685 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
686 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
687 ieee80211_hdrlen(fc));
690 * Use the first empty entry in this queue's command buffer array
691 * to contain the Tx command and MAC header concatenated together
692 * (payload data will be in another buffer).
693 * Size of this varies, due to varying MAC header length.
694 * If end is not dword aligned, we'll have 2 extra bytes at the end
695 * of the MAC header (device reads on dword boundaries).
696 * We'll tell device about this padding later.
698 len = sizeof(struct iwl3945_tx_cmd) +
699 sizeof(struct iwl_cmd_header) + hdr_len;
702 len = (len + 3) & ~3;
709 /* Physical address of this Tx command's header (not MAC header!),
710 * within command buffer array. */
711 txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
712 len, PCI_DMA_TODEVICE);
713 /* we do not map meta data ... so we can safely access address to
714 * provide to unmap command*/
715 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
716 pci_unmap_len_set(&out_cmd->meta, len, len);
718 /* Add buffer containing Tx command and MAC(!) header to TFD's
720 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
721 txcmd_phys, len, 1, 0);
724 /* Set up TFD's 2nd entry to point directly to remainder of skb,
725 * if any (802.11 null frames have no payload). */
726 len = skb->len - hdr_len;
728 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
729 len, PCI_DMA_TODEVICE);
730 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
736 /* Tell device the write index *just past* this latest filled TFD */
737 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
738 rc = iwl_txq_update_write_ptr(priv, txq);
739 spin_unlock_irqrestore(&priv->lock, flags);
744 if ((iwl_queue_space(q) < q->high_mark)
745 && priv->mac80211_registered) {
746 if (wait_write_ptr) {
747 spin_lock_irqsave(&priv->lock, flags);
748 txq->need_update = 1;
749 iwl_txq_update_write_ptr(priv, txq);
750 spin_unlock_irqrestore(&priv->lock, flags);
753 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
759 spin_unlock_irqrestore(&priv->lock, flags);
764 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
766 #include "iwl-spectrum.h"
768 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
769 #define BEACON_TIME_MASK_HIGH 0xFF000000
770 #define TIME_UNIT 1024
773 * extended beacon time format
774 * time in usec will be changed into a 32-bit value in 8:24 format
775 * the high 1 byte is the beacon counts
776 * the lower 3 bytes is the time in usec within one beacon interval
779 static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
783 u32 interval = beacon_interval * 1024;
785 if (!interval || !usec)
788 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
789 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
791 return (quot << 24) + rem;
794 /* base is usually what we get from ucode with each received frame,
795 * the same as HW timer counter counting down
798 static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
800 u32 base_low = base & BEACON_TIME_MASK_LOW;
801 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
802 u32 interval = beacon_interval * TIME_UNIT;
803 u32 res = (base & BEACON_TIME_MASK_HIGH) +
804 (addon & BEACON_TIME_MASK_HIGH);
806 if (base_low > addon_low)
807 res += base_low - addon_low;
808 else if (base_low < addon_low) {
809 res += interval + base_low - addon_low;
814 return cpu_to_le32(res);
817 static int iwl3945_get_measurement(struct iwl_priv *priv,
818 struct ieee80211_measurement_params *params,
821 struct iwl_spectrum_cmd spectrum;
822 struct iwl_rx_packet *res;
823 struct iwl_host_cmd cmd = {
824 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
825 .data = (void *)&spectrum,
826 .meta.flags = CMD_WANT_SKB,
828 u32 add_time = le64_to_cpu(params->start_time);
830 int spectrum_resp_status;
831 int duration = le16_to_cpu(params->duration);
833 if (iwl_is_associated(priv))
835 iwl3945_usecs_to_beacons(
836 le64_to_cpu(params->start_time) - priv->last_tsf,
837 le16_to_cpu(priv->rxon_timing.beacon_interval));
839 memset(&spectrum, 0, sizeof(spectrum));
841 spectrum.channel_count = cpu_to_le16(1);
843 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
844 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
845 cmd.len = sizeof(spectrum);
846 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
848 if (iwl_is_associated(priv))
849 spectrum.start_time =
850 iwl3945_add_beacon_time(priv->last_beacon_time,
852 le16_to_cpu(priv->rxon_timing.beacon_interval));
854 spectrum.start_time = 0;
856 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
857 spectrum.channels[0].channel = params->channel;
858 spectrum.channels[0].type = type;
859 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
860 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
861 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
863 rc = iwl_send_cmd_sync(priv, &cmd);
867 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
868 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
869 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
873 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
874 switch (spectrum_resp_status) {
875 case 0: /* Command will be handled */
876 if (res->u.spectrum.id != 0xff) {
877 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
879 priv->measurement_status &= ~MEASUREMENT_READY;
881 priv->measurement_status |= MEASUREMENT_ACTIVE;
885 case 1: /* Command will not be handled */
890 dev_kfree_skb_any(cmd.meta.u.skb);
896 static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
897 struct iwl_rx_mem_buffer *rxb)
899 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
900 struct iwl_alive_resp *palive;
901 struct delayed_work *pwork;
903 palive = &pkt->u.alive_frame;
905 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
907 palive->is_valid, palive->ver_type,
908 palive->ver_subtype);
910 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
911 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
912 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
913 sizeof(struct iwl_alive_resp));
914 pwork = &priv->init_alive_start;
916 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
917 memcpy(&priv->card_alive, &pkt->u.alive_frame,
918 sizeof(struct iwl_alive_resp));
919 pwork = &priv->alive_start;
920 iwl3945_disable_events(priv);
923 /* We delay the ALIVE response by 5ms to
924 * give the HW RF Kill time to activate... */
925 if (palive->is_valid == UCODE_VALID_OK)
926 queue_delayed_work(priv->workqueue, pwork,
927 msecs_to_jiffies(5));
929 IWL_WARN(priv, "uCode did not respond OK.\n");
932 static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
933 struct iwl_rx_mem_buffer *rxb)
935 #ifdef CONFIG_IWLWIFI_DEBUG
936 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
939 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
943 static void iwl3945_bg_beacon_update(struct work_struct *work)
945 struct iwl_priv *priv =
946 container_of(work, struct iwl_priv, beacon_update);
947 struct sk_buff *beacon;
949 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
950 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
953 IWL_ERR(priv, "update beacon failed\n");
957 mutex_lock(&priv->mutex);
958 /* new beacon skb is allocated every time; dispose previous.*/
959 if (priv->ibss_beacon)
960 dev_kfree_skb(priv->ibss_beacon);
962 priv->ibss_beacon = beacon;
963 mutex_unlock(&priv->mutex);
965 iwl3945_send_beacon_cmd(priv);
968 static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
969 struct iwl_rx_mem_buffer *rxb)
971 #ifdef CONFIG_IWLWIFI_DEBUG
972 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
973 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
974 u8 rate = beacon->beacon_notify_hdr.rate;
976 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
977 "tsf %d %d rate %d\n",
978 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
979 beacon->beacon_notify_hdr.failure_frame,
980 le32_to_cpu(beacon->ibss_mgr_status),
981 le32_to_cpu(beacon->high_tsf),
982 le32_to_cpu(beacon->low_tsf), rate);
985 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
986 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
987 queue_work(priv->workqueue, &priv->beacon_update);
990 /* Handle notification from uCode that card's power state is changing
991 * due to software, hardware, or critical temperature RFKILL */
992 static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
993 struct iwl_rx_mem_buffer *rxb)
995 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
996 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
997 unsigned long status = priv->status;
999 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
1000 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1001 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1003 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1004 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1006 if (flags & HW_CARD_DISABLED)
1007 set_bit(STATUS_RF_KILL_HW, &priv->status);
1009 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1012 iwl_scan_cancel(priv);
1014 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1015 test_bit(STATUS_RF_KILL_HW, &priv->status)))
1016 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
1017 test_bit(STATUS_RF_KILL_HW, &priv->status));
1019 wake_up_interruptible(&priv->wait_command_queue);
1023 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
1025 * Setup the RX handlers for each of the reply types sent from the uCode
1028 * This function chains into the hardware specific files for them to setup
1029 * any hardware specific handlers as well.
1031 static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
1033 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
1034 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
1035 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
1036 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
1037 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
1038 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
1039 iwl_rx_pm_debug_statistics_notif;
1040 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
1043 * The same handler is used for both the REPLY to a discrete
1044 * statistics request from the host as well as for the periodic
1045 * statistics notifications (after received beacons) from the uCode.
1047 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
1048 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
1050 iwl_setup_spectrum_handlers(priv);
1051 iwl_setup_rx_scan_handlers(priv);
1052 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
1054 /* Set up hardware specific Rx handlers */
1055 iwl3945_hw_rx_handler_setup(priv);
1058 /************************** RX-FUNCTIONS ****************************/
1060 * Rx theory of operation
1062 * The host allocates 32 DMA target addresses and passes the host address
1063 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
1067 * The host/firmware share two index registers for managing the Rx buffers.
1069 * The READ index maps to the first position that the firmware may be writing
1070 * to -- the driver can read up to (but not including) this position and get
1072 * The READ index is managed by the firmware once the card is enabled.
1074 * The WRITE index maps to the last position the driver has read from -- the
1075 * position preceding WRITE is the last slot the firmware can place a packet.
1077 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
1080 * During initialization, the host sets up the READ queue position to the first
1081 * INDEX position, and WRITE to the last (READ - 1 wrapped)
1083 * When the firmware places a packet in a buffer, it will advance the READ index
1084 * and fire the RX interrupt. The driver can then query the READ index and
1085 * process as many packets as possible, moving the WRITE index forward as it
1086 * resets the Rx queue buffers with new memory.
1088 * The management in the driver is as follows:
1089 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
1090 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
1091 * to replenish the iwl->rxq->rx_free.
1092 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
1093 * iwl->rxq is replenished and the READ INDEX is updated (updating the
1094 * 'processed' and 'read' driver indexes as well)
1095 * + A received packet is processed and handed to the kernel network stack,
1096 * detached from the iwl->rxq. The driver 'processed' index is updated.
1097 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
1098 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
1099 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
1100 * were enough free buffers and RX_STALLED is set it is cleared.
1105 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
1106 * iwl3945_rx_queue_restock
1107 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
1108 * queue, updates firmware pointers, and updates
1109 * the WRITE index. If insufficient rx_free buffers
1110 * are available, schedules iwl3945_rx_replenish
1112 * -- enable interrupts --
1113 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
1114 * READ INDEX, detaching the SKB from the pool.
1115 * Moves the packet buffer from queue to rx_used.
1116 * Calls iwl3945_rx_queue_restock to refill any empty
1123 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
1125 static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
1126 dma_addr_t dma_addr)
1128 return cpu_to_le32((u32)dma_addr);
1132 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
1134 * If there are slots in the RX queue that need to be restocked,
1135 * and we have free pre-allocated buffers, fill the ranks as much
1136 * as we can, pulling from rx_free.
1138 * This moves the 'write' index forward to catch up with 'processed', and
1139 * also updates the memory address in the firmware to reference the new
1142 static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
1144 struct iwl_rx_queue *rxq = &priv->rxq;
1145 struct list_head *element;
1146 struct iwl_rx_mem_buffer *rxb;
1147 unsigned long flags;
1150 spin_lock_irqsave(&rxq->lock, flags);
1151 write = rxq->write & ~0x7;
1152 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
1153 /* Get next free Rx buffer, remove from free list */
1154 element = rxq->rx_free.next;
1155 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1158 /* Point to Rx buffer via next RBD in circular buffer */
1159 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
1160 rxq->queue[rxq->write] = rxb;
1161 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1164 spin_unlock_irqrestore(&rxq->lock, flags);
1165 /* If the pre-allocated buffer pool is dropping low, schedule to
1167 if (rxq->free_count <= RX_LOW_WATERMARK)
1168 queue_work(priv->workqueue, &priv->rx_replenish);
1171 /* If we've added more space for the firmware to place data, tell it.
1172 * Increment device's write pointer in multiples of 8. */
1173 if ((rxq->write_actual != (rxq->write & ~0x7))
1174 || (abs(rxq->write - rxq->read) > 7)) {
1175 spin_lock_irqsave(&rxq->lock, flags);
1176 rxq->need_update = 1;
1177 spin_unlock_irqrestore(&rxq->lock, flags);
1178 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
1187 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
1189 * When moving to rx_free an SKB is allocated for the slot.
1191 * Also restock the Rx queue via iwl3945_rx_queue_restock.
1192 * This is called as a scheduled work item (except for during initialization)
1194 static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
1196 struct iwl_rx_queue *rxq = &priv->rxq;
1197 struct list_head *element;
1198 struct iwl_rx_mem_buffer *rxb;
1199 unsigned long flags;
1202 spin_lock_irqsave(&rxq->lock, flags);
1204 if (list_empty(&rxq->rx_used)) {
1205 spin_unlock_irqrestore(&rxq->lock, flags);
1209 element = rxq->rx_used.next;
1210 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1212 spin_unlock_irqrestore(&rxq->lock, flags);
1214 /* Alloc a new receive buffer */
1216 alloc_skb(priv->hw_params.rx_buf_size,
1219 if (net_ratelimit())
1220 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
1221 /* We don't reschedule replenish work here -- we will
1222 * call the restock method and if it still needs
1223 * more buffers it will schedule replenish */
1227 /* If radiotap head is required, reserve some headroom here.
1228 * The physical head count is a variable rx_stats->phy_count.
1229 * We reserve 4 bytes here. Plus these extra bytes, the
1230 * headroom of the physical head should be enough for the
1231 * radiotap head that iwl3945 supported. See iwl3945_rt.
1233 skb_reserve(rxb->skb, 4);
1235 /* Get physical address of RB/SKB */
1236 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
1238 priv->hw_params.rx_buf_size,
1239 PCI_DMA_FROMDEVICE);
1241 spin_lock_irqsave(&rxq->lock, flags);
1242 list_add_tail(&rxb->list, &rxq->rx_free);
1243 priv->alloc_rxb_skb++;
1245 spin_unlock_irqrestore(&rxq->lock, flags);
1249 void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1251 unsigned long flags;
1253 spin_lock_irqsave(&rxq->lock, flags);
1254 INIT_LIST_HEAD(&rxq->rx_free);
1255 INIT_LIST_HEAD(&rxq->rx_used);
1256 /* Fill the rx_used queue with _all_ of the Rx buffers */
1257 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1258 /* In the reset function, these buffers may have been allocated
1259 * to an SKB, so we need to unmap and free potential storage */
1260 if (rxq->pool[i].skb != NULL) {
1261 pci_unmap_single(priv->pci_dev,
1262 rxq->pool[i].real_dma_addr,
1263 priv->hw_params.rx_buf_size,
1264 PCI_DMA_FROMDEVICE);
1265 priv->alloc_rxb_skb--;
1266 dev_kfree_skb(rxq->pool[i].skb);
1267 rxq->pool[i].skb = NULL;
1269 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1272 /* Set us so that we have processed and used all buffers, but have
1273 * not restocked the Rx queue with fresh buffers */
1274 rxq->read = rxq->write = 0;
1275 rxq->free_count = 0;
1276 rxq->write_actual = 0;
1277 spin_unlock_irqrestore(&rxq->lock, flags);
1280 void iwl3945_rx_replenish(void *data)
1282 struct iwl_priv *priv = data;
1283 unsigned long flags;
1285 iwl3945_rx_allocate(priv, GFP_KERNEL);
1287 spin_lock_irqsave(&priv->lock, flags);
1288 iwl3945_rx_queue_restock(priv);
1289 spin_unlock_irqrestore(&priv->lock, flags);
1292 static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1294 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1296 iwl3945_rx_queue_restock(priv);
1300 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1301 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1302 * This free routine walks the list of POOL entries and if SKB is set to
1303 * non NULL it is unmapped and freed
1305 static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1308 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
1309 if (rxq->pool[i].skb != NULL) {
1310 pci_unmap_single(priv->pci_dev,
1311 rxq->pool[i].real_dma_addr,
1312 priv->hw_params.rx_buf_size,
1313 PCI_DMA_FROMDEVICE);
1314 dev_kfree_skb(rxq->pool[i].skb);
1318 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
1320 pci_free_consistent(priv->pci_dev, sizeof(struct iwl_rb_status),
1321 rxq->rb_stts, rxq->rb_stts_dma);
1323 rxq->rb_stts = NULL;
1327 /* Convert linear signal-to-noise ratio into dB */
1328 static u8 ratio2dB[100] = {
1329 /* 0 1 2 3 4 5 6 7 8 9 */
1330 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1331 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1332 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1333 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1334 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1335 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1336 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1337 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1338 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1339 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1342 /* Calculates a relative dB value from a ratio of linear
1343 * (i.e. not dB) signal levels.
1344 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
1345 int iwl3945_calc_db_from_ratio(int sig_ratio)
1347 /* 1000:1 or higher just report as 60 dB */
1348 if (sig_ratio >= 1000)
1351 /* 100:1 or higher, divide by 10 and use table,
1352 * add 20 dB to make up for divide by 10 */
1353 if (sig_ratio >= 100)
1354 return 20 + (int)ratio2dB[sig_ratio/10];
1356 /* We shouldn't see this */
1360 /* Use table for ratios 1:1 - 99:1 */
1361 return (int)ratio2dB[sig_ratio];
1364 #define PERFECT_RSSI (-20) /* dBm */
1365 #define WORST_RSSI (-95) /* dBm */
1366 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1368 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
1369 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1370 * about formulas used below. */
1371 int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
1374 int degradation = PERFECT_RSSI - rssi_dbm;
1376 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1377 * as indicator; formula is (signal dbm - noise dbm).
1378 * SNR at or above 40 is a great signal (100%).
1379 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1380 * Weakest usable signal is usually 10 - 15 dB SNR. */
1382 if (rssi_dbm - noise_dbm >= 40)
1384 else if (rssi_dbm < noise_dbm)
1386 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1388 /* Else use just the signal level.
1389 * This formula is a least squares fit of data points collected and
1390 * compared with a reference system that had a percentage (%) display
1391 * for signal quality. */
1393 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1394 (15 * RSSI_RANGE + 62 * degradation)) /
1395 (RSSI_RANGE * RSSI_RANGE);
1399 else if (sig_qual < 1)
1406 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
1408 * Uses the priv->rx_handlers callback function array to invoke
1409 * the appropriate handlers, including command responses,
1410 * frame-received notifications, and other notifications.
1412 static void iwl3945_rx_handle(struct iwl_priv *priv)
1414 struct iwl_rx_mem_buffer *rxb;
1415 struct iwl_rx_packet *pkt;
1416 struct iwl_rx_queue *rxq = &priv->rxq;
1419 unsigned long flags;
1422 int total_empty = 0;
1424 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1425 * buffer that the driver may process (last buffer filled by ucode). */
1426 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
1429 /* calculate total frames need to be restock after handling RX */
1430 total_empty = r - priv->rxq.write_actual;
1431 if (total_empty < 0)
1432 total_empty += RX_QUEUE_SIZE;
1434 if (total_empty > (RX_QUEUE_SIZE / 2))
1436 /* Rx interrupt, but nothing sent from uCode */
1438 IWL_DEBUG(priv, IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
1441 rxb = rxq->queue[i];
1443 /* If an RXB doesn't have a Rx queue slot associated with it,
1444 * then a bug has been introduced in the queue refilling
1445 * routines -- catch it here */
1446 BUG_ON(rxb == NULL);
1448 rxq->queue[i] = NULL;
1450 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
1451 priv->hw_params.rx_buf_size,
1452 PCI_DMA_FROMDEVICE);
1453 pkt = (struct iwl_rx_packet *)rxb->skb->data;
1455 /* Reclaim a command buffer only if this packet is a response
1456 * to a (driver-originated) command.
1457 * If the packet (e.g. Rx frame) originated from uCode,
1458 * there is no command buffer to reclaim.
1459 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1460 * but apparently a few don't get set; catch them here. */
1461 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1462 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1463 (pkt->hdr.cmd != REPLY_TX);
1465 /* Based on type of command response or notification,
1466 * handle those that need handling via function in
1467 * rx_handlers table. See iwl3945_setup_rx_handlers() */
1468 if (priv->rx_handlers[pkt->hdr.cmd]) {
1469 IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
1470 "r = %d, i = %d, %s, 0x%02x\n", r, i,
1471 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1472 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1473 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
1475 /* No handling needed */
1476 IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
1477 "r %d i %d No handler needed for %s, 0x%02x\n",
1478 r, i, get_cmd_string(pkt->hdr.cmd),
1483 /* Invoke any callbacks, transfer the skb to caller, and
1484 * fire off the (possibly) blocking iwl_send_cmd()
1485 * as we reclaim the driver command queue */
1486 if (rxb && rxb->skb)
1487 iwl_tx_cmd_complete(priv, rxb);
1489 IWL_WARN(priv, "Claim null rxb?\n");
1492 /* For now we just don't re-use anything. We can tweak this
1493 * later to try and re-use notification packets and SKBs that
1494 * fail to Rx correctly */
1495 if (rxb->skb != NULL) {
1496 priv->alloc_rxb_skb--;
1497 dev_kfree_skb_any(rxb->skb);
1501 spin_lock_irqsave(&rxq->lock, flags);
1502 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1503 spin_unlock_irqrestore(&rxq->lock, flags);
1504 i = (i + 1) & RX_QUEUE_MASK;
1505 /* If there are a lot of unused frames,
1506 * restock the Rx queue so ucode won't assert. */
1511 iwl3945_rx_replenish_now(priv);
1517 /* Backtrack one entry */
1520 iwl3945_rx_replenish_now(priv);
1522 iwl3945_rx_queue_restock(priv);
1525 /* call this function to flush any scheduled tasklet */
1526 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1528 /* wait to make sure we flush pending tasklet*/
1529 synchronize_irq(priv->pci_dev->irq);
1530 tasklet_kill(&priv->irq_tasklet);
1533 static const char *desc_lookup(int i)
1541 return "BAD_CHECKSUM";
1543 return "NMI_INTERRUPT";
1547 return "FATAL_ERROR";
1553 #define ERROR_START_OFFSET (1 * sizeof(u32))
1554 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
1556 static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
1559 u32 desc, time, count, base, data1;
1560 u32 blink1, blink2, ilink1, ilink2;
1562 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1564 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
1565 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
1570 count = iwl_read_targ_mem(priv, base);
1572 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
1573 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1574 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1575 priv->status, count);
1578 IWL_ERR(priv, "Desc Time asrtPC blink2 "
1579 "ilink1 nmiPC Line\n");
1580 for (i = ERROR_START_OFFSET;
1581 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1582 i += ERROR_ELEM_SIZE) {
1583 desc = iwl_read_targ_mem(priv, base + i);
1585 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
1587 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
1589 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
1591 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
1593 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
1595 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
1598 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
1599 desc_lookup(desc), desc, time, blink1, blink2,
1600 ilink1, ilink2, data1);
1605 #define EVENT_START_OFFSET (6 * sizeof(u32))
1608 * iwl3945_print_event_log - Dump error event log to syslog
1611 static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1612 u32 num_events, u32 mode)
1615 u32 base; /* SRAM byte address of event log header */
1616 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1617 u32 ptr; /* SRAM byte address of log data */
1618 u32 ev, time, data; /* event log data */
1620 if (num_events == 0)
1623 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1626 event_size = 2 * sizeof(u32);
1628 event_size = 3 * sizeof(u32);
1630 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1632 /* "time" is actually "data" for mode 0 (no timestamp).
1633 * place event id # at far right for easier visual parsing. */
1634 for (i = 0; i < num_events; i++) {
1635 ev = iwl_read_targ_mem(priv, ptr);
1637 time = iwl_read_targ_mem(priv, ptr);
1641 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1643 data = iwl_read_targ_mem(priv, ptr);
1645 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
1650 static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
1652 u32 base; /* SRAM byte address of event log header */
1653 u32 capacity; /* event log capacity in # entries */
1654 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1655 u32 num_wraps; /* # times uCode wrapped to top of log */
1656 u32 next_entry; /* index of next entry to be written by uCode */
1657 u32 size; /* # entries that we'll print */
1659 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1660 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
1661 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
1665 /* event log header */
1666 capacity = iwl_read_targ_mem(priv, base);
1667 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1668 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1669 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
1671 size = num_wraps ? capacity : next_entry;
1673 /* bail out if nothing in log */
1675 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
1679 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
1682 /* if uCode has wrapped back to top of log, start at the oldest entry,
1683 * i.e the next one that uCode would fill. */
1685 iwl3945_print_event_log(priv, next_entry,
1686 capacity - next_entry, mode);
1688 /* (then/else) start at top of log */
1689 iwl3945_print_event_log(priv, 0, next_entry, mode);
1693 static void iwl3945_irq_tasklet(struct iwl_priv *priv)
1695 u32 inta, handled = 0;
1697 unsigned long flags;
1698 #ifdef CONFIG_IWLWIFI_DEBUG
1702 spin_lock_irqsave(&priv->lock, flags);
1704 /* Ack/clear/reset pending uCode interrupts.
1705 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1706 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
1707 inta = iwl_read32(priv, CSR_INT);
1708 iwl_write32(priv, CSR_INT, inta);
1710 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1711 * Any new interrupts that happen after this, either while we're
1712 * in this tasklet, or later, will show up in next ISR/tasklet. */
1713 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1714 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
1716 #ifdef CONFIG_IWLWIFI_DEBUG
1717 if (priv->debug_level & IWL_DL_ISR) {
1718 /* just for debug */
1719 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1720 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1721 inta, inta_mask, inta_fh);
1725 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1726 * atomic, make sure that inta covers all the interrupts that
1727 * we've discovered, even if FH interrupt came in just after
1728 * reading CSR_INT. */
1729 if (inta_fh & CSR39_FH_INT_RX_MASK)
1730 inta |= CSR_INT_BIT_FH_RX;
1731 if (inta_fh & CSR39_FH_INT_TX_MASK)
1732 inta |= CSR_INT_BIT_FH_TX;
1734 /* Now service all interrupt bits discovered above. */
1735 if (inta & CSR_INT_BIT_HW_ERR) {
1736 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
1738 /* Tell the device to stop sending interrupts */
1739 iwl_disable_interrupts(priv);
1741 priv->isr_stats.hw++;
1742 iwl_irq_handle_error(priv);
1744 handled |= CSR_INT_BIT_HW_ERR;
1746 spin_unlock_irqrestore(&priv->lock, flags);
1751 #ifdef CONFIG_IWLWIFI_DEBUG
1752 if (priv->debug_level & (IWL_DL_ISR)) {
1753 /* NIC fires this, but we don't use it, redundant with WAKEUP */
1754 if (inta & CSR_INT_BIT_SCD) {
1755 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
1756 "the frame/frames.\n");
1757 priv->isr_stats.sch++;
1760 /* Alive notification via Rx interrupt will do the real work */
1761 if (inta & CSR_INT_BIT_ALIVE) {
1762 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
1763 priv->isr_stats.alive++;
1767 /* Safely ignore these bits for debug checks below */
1768 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1770 /* Error detected by uCode */
1771 if (inta & CSR_INT_BIT_SW_ERR) {
1772 IWL_ERR(priv, "Microcode SW error detected. "
1773 "Restarting 0x%X.\n", inta);
1774 priv->isr_stats.sw++;
1775 priv->isr_stats.sw_err = inta;
1776 iwl_irq_handle_error(priv);
1777 handled |= CSR_INT_BIT_SW_ERR;
1780 /* uCode wakes up after power-down sleep */
1781 if (inta & CSR_INT_BIT_WAKEUP) {
1782 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
1783 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
1784 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1785 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1786 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1787 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1788 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1789 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
1791 priv->isr_stats.wakeup++;
1792 handled |= CSR_INT_BIT_WAKEUP;
1795 /* All uCode command responses, including Tx command responses,
1796 * Rx "responses" (frame-received notification), and other
1797 * notifications from uCode come through here*/
1798 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1799 iwl3945_rx_handle(priv);
1800 priv->isr_stats.rx++;
1801 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1804 if (inta & CSR_INT_BIT_FH_TX) {
1805 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
1806 priv->isr_stats.tx++;
1808 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
1809 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1810 (FH39_SRVC_CHNL), 0x0);
1811 handled |= CSR_INT_BIT_FH_TX;
1814 if (inta & ~handled) {
1815 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
1816 priv->isr_stats.unhandled++;
1819 if (inta & ~priv->inta_mask) {
1820 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
1821 inta & ~priv->inta_mask);
1822 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
1825 /* Re-enable all interrupts */
1826 /* only Re-enable if disabled by irq */
1827 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1828 iwl_enable_interrupts(priv);
1830 #ifdef CONFIG_IWLWIFI_DEBUG
1831 if (priv->debug_level & (IWL_DL_ISR)) {
1832 inta = iwl_read32(priv, CSR_INT);
1833 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1834 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1835 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
1836 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1839 spin_unlock_irqrestore(&priv->lock, flags);
1842 static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
1843 enum ieee80211_band band,
1844 u8 is_active, u8 n_probes,
1845 struct iwl3945_scan_channel *scan_ch)
1847 const struct ieee80211_channel *channels = NULL;
1848 const struct ieee80211_supported_band *sband;
1849 const struct iwl_channel_info *ch_info;
1850 u16 passive_dwell = 0;
1851 u16 active_dwell = 0;
1854 sband = iwl_get_hw_mode(priv, band);
1858 channels = sband->channels;
1860 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
1861 passive_dwell = iwl_get_passive_dwell_time(priv, band);
1863 if (passive_dwell <= active_dwell)
1864 passive_dwell = active_dwell + 1;
1866 for (i = 0, added = 0; i < sband->n_channels; i++) {
1867 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
1870 scan_ch->channel = channels[i].hw_value;
1872 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
1873 if (!is_channel_valid(ch_info)) {
1874 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
1879 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1880 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1881 /* If passive , set up for auto-switch
1882 * and use long active_dwell time.
1884 if (!is_active || is_channel_passive(ch_info) ||
1885 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
1886 scan_ch->type = 0; /* passive */
1887 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1888 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1890 scan_ch->type = 1; /* active */
1893 /* Set direct probe bits. These may be used both for active
1894 * scan channels (probes gets sent right away),
1895 * or for passive channels (probes get se sent only after
1896 * hearing clear Rx packet).*/
1897 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1899 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
1901 /* uCode v1 does not allow setting direct probe bits on
1902 * passive channel. */
1903 if ((scan_ch->type & 1) && n_probes)
1904 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
1907 /* Set txpower levels to defaults */
1908 scan_ch->tpc.dsp_atten = 110;
1909 /* scan_pwr_info->tpc.dsp_atten; */
1911 /*scan_pwr_info->tpc.tx_gain; */
1912 if (band == IEEE80211_BAND_5GHZ)
1913 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1915 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1916 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
1918 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
1922 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
1924 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1925 (scan_ch->type & 1) ?
1926 active_dwell : passive_dwell);
1932 IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
1936 static void iwl3945_init_hw_rates(struct iwl_priv *priv,
1937 struct ieee80211_rate *rates)
1941 for (i = 0; i < IWL_RATE_COUNT; i++) {
1942 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1943 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1944 rates[i].hw_value_short = i;
1946 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
1948 * If CCK != 1M then set short preamble rate flag.
1950 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
1951 0 : IEEE80211_RATE_SHORT_PREAMBLE;
1956 /******************************************************************************
1958 * uCode download functions
1960 ******************************************************************************/
1962 static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
1964 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1965 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1966 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1967 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1968 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1969 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
1973 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
1974 * looking at all data.
1976 static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
1983 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
1985 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
1986 IWL39_RTC_INST_LOWER_BOUND);
1989 for (; len > 0; len -= sizeof(u32), image++) {
1990 /* read data comes through single port, auto-incr addr */
1991 /* NOTE: Use the debugless read so we don't flood kernel log
1992 * if IWL_DL_IO is set */
1993 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1994 if (val != le32_to_cpu(*image)) {
1995 IWL_ERR(priv, "uCode INST section is invalid at "
1996 "offset 0x%x, is 0x%x, s/b 0x%x\n",
1997 save_len - len, val, le32_to_cpu(*image));
2007 IWL_DEBUG_INFO(priv,
2008 "ucode image in INSTRUCTION memory is good\n");
2015 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
2016 * using sample data 100 bytes apart. If these sample points are good,
2017 * it's a pretty good bet that everything between them is good, too.
2019 static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
2026 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
2028 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2029 /* read data comes through single port, auto-incr addr */
2030 /* NOTE: Use the debugless read so we don't flood kernel log
2031 * if IWL_DL_IO is set */
2032 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
2033 i + IWL39_RTC_INST_LOWER_BOUND);
2034 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
2035 if (val != le32_to_cpu(*image)) {
2036 #if 0 /* Enable this if you want to see details */
2037 IWL_ERR(priv, "uCode INST section is invalid at "
2038 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2053 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
2054 * and verify its contents
2056 static int iwl3945_verify_ucode(struct iwl_priv *priv)
2063 image = (__le32 *)priv->ucode_boot.v_addr;
2064 len = priv->ucode_boot.len;
2065 rc = iwl3945_verify_inst_sparse(priv, image, len);
2067 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
2071 /* Try initialize */
2072 image = (__le32 *)priv->ucode_init.v_addr;
2073 len = priv->ucode_init.len;
2074 rc = iwl3945_verify_inst_sparse(priv, image, len);
2076 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
2080 /* Try runtime/protocol */
2081 image = (__le32 *)priv->ucode_code.v_addr;
2082 len = priv->ucode_code.len;
2083 rc = iwl3945_verify_inst_sparse(priv, image, len);
2085 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
2089 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
2091 /* Since nothing seems to match, show first several data entries in
2092 * instruction SRAM, so maybe visual inspection will give a clue.
2093 * Selection of bootstrap image (vs. other images) is arbitrary. */
2094 image = (__le32 *)priv->ucode_boot.v_addr;
2095 len = priv->ucode_boot.len;
2096 rc = iwl3945_verify_inst_full(priv, image, len);
2101 static void iwl3945_nic_start(struct iwl_priv *priv)
2103 /* Remove all resets to allow NIC to operate */
2104 iwl_write32(priv, CSR_RESET, 0);
2108 * iwl3945_read_ucode - Read uCode images from disk file.
2110 * Copy into buffers for card to fetch via bus-mastering
2112 static int iwl3945_read_ucode(struct iwl_priv *priv)
2114 struct iwl_ucode *ucode;
2115 int ret = -EINVAL, index;
2116 const struct firmware *ucode_raw;
2117 /* firmware file name contains uCode/driver compatibility version */
2118 const char *name_pre = priv->cfg->fw_name_pre;
2119 const unsigned int api_max = priv->cfg->ucode_api_max;
2120 const unsigned int api_min = priv->cfg->ucode_api_min;
2124 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
2126 /* Ask kernel firmware_class module to get the boot firmware off disk.
2127 * request_firmware() is synchronous, file is in memory on return. */
2128 for (index = api_max; index >= api_min; index--) {
2129 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2130 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2132 IWL_ERR(priv, "%s firmware file req failed: %d\n",
2139 if (index < api_max)
2140 IWL_ERR(priv, "Loaded firmware %s, "
2141 "which is deprecated. "
2142 " Please use API v%u instead.\n",
2144 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2145 "(%zd bytes) from disk\n",
2146 buf, ucode_raw->size);
2154 /* Make sure that we got at least our header! */
2155 if (ucode_raw->size < sizeof(*ucode)) {
2156 IWL_ERR(priv, "File size way too small!\n");
2161 /* Data from ucode file: header followed by uCode images */
2162 ucode = (void *)ucode_raw->data;
2164 priv->ucode_ver = le32_to_cpu(ucode->ver);
2165 api_ver = IWL_UCODE_API(priv->ucode_ver);
2166 inst_size = le32_to_cpu(ucode->inst_size);
2167 data_size = le32_to_cpu(ucode->data_size);
2168 init_size = le32_to_cpu(ucode->init_size);
2169 init_data_size = le32_to_cpu(ucode->init_data_size);
2170 boot_size = le32_to_cpu(ucode->boot_size);
2172 /* api_ver should match the api version forming part of the
2173 * firmware filename ... but we don't check for that and only rely
2174 * on the API version read from firmware header from here on forward */
2176 if (api_ver < api_min || api_ver > api_max) {
2177 IWL_ERR(priv, "Driver unable to support your firmware API. "
2178 "Driver supports v%u, firmware is v%u.\n",
2180 priv->ucode_ver = 0;
2184 if (api_ver != api_max)
2185 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
2186 "got %u. New firmware can be obtained "
2187 "from http://www.intellinuxwireless.org.\n",
2190 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2191 IWL_UCODE_MAJOR(priv->ucode_ver),
2192 IWL_UCODE_MINOR(priv->ucode_ver),
2193 IWL_UCODE_API(priv->ucode_ver),
2194 IWL_UCODE_SERIAL(priv->ucode_ver));
2196 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
2198 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2200 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2202 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2204 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2206 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2210 /* Verify size of file vs. image size info in file's header */
2211 if (ucode_raw->size < sizeof(*ucode) +
2212 inst_size + data_size + init_size +
2213 init_data_size + boot_size) {
2215 IWL_DEBUG_INFO(priv, "uCode file size %zd too small\n",
2221 /* Verify that uCode images will fit in card's SRAM */
2222 if (inst_size > IWL39_MAX_INST_SIZE) {
2223 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
2229 if (data_size > IWL39_MAX_DATA_SIZE) {
2230 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
2235 if (init_size > IWL39_MAX_INST_SIZE) {
2236 IWL_DEBUG_INFO(priv,
2237 "uCode init instr len %d too large to fit in\n",
2242 if (init_data_size > IWL39_MAX_DATA_SIZE) {
2243 IWL_DEBUG_INFO(priv,
2244 "uCode init data len %d too large to fit in\n",
2249 if (boot_size > IWL39_MAX_BSM_SIZE) {
2250 IWL_DEBUG_INFO(priv,
2251 "uCode boot instr len %d too large to fit in\n",
2257 /* Allocate ucode buffers for card's bus-master loading ... */
2259 /* Runtime instructions and 2 copies of data:
2260 * 1) unmodified from disk
2261 * 2) backup cache for save/restore during power-downs */
2262 priv->ucode_code.len = inst_size;
2263 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
2265 priv->ucode_data.len = data_size;
2266 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
2268 priv->ucode_data_backup.len = data_size;
2269 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2271 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
2272 !priv->ucode_data_backup.v_addr)
2275 /* Initialization instructions and data */
2276 if (init_size && init_data_size) {
2277 priv->ucode_init.len = init_size;
2278 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
2280 priv->ucode_init_data.len = init_data_size;
2281 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2283 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2287 /* Bootstrap (instructions only, no data) */
2289 priv->ucode_boot.len = boot_size;
2290 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
2292 if (!priv->ucode_boot.v_addr)
2296 /* Copy images into buffers for card's bus-master reads ... */
2298 /* Runtime instructions (first block of data in file) */
2299 src = &ucode->data[0];
2300 len = priv->ucode_code.len;
2301 IWL_DEBUG_INFO(priv,
2302 "Copying (but not loading) uCode instr len %zd\n", len);
2303 memcpy(priv->ucode_code.v_addr, src, len);
2304 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2305 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2307 /* Runtime data (2nd block)
2308 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
2309 src = &ucode->data[inst_size];
2310 len = priv->ucode_data.len;
2311 IWL_DEBUG_INFO(priv,
2312 "Copying (but not loading) uCode data len %zd\n", len);
2313 memcpy(priv->ucode_data.v_addr, src, len);
2314 memcpy(priv->ucode_data_backup.v_addr, src, len);
2316 /* Initialization instructions (3rd block) */
2318 src = &ucode->data[inst_size + data_size];
2319 len = priv->ucode_init.len;
2320 IWL_DEBUG_INFO(priv,
2321 "Copying (but not loading) init instr len %zd\n", len);
2322 memcpy(priv->ucode_init.v_addr, src, len);
2325 /* Initialization data (4th block) */
2326 if (init_data_size) {
2327 src = &ucode->data[inst_size + data_size + init_size];
2328 len = priv->ucode_init_data.len;
2329 IWL_DEBUG_INFO(priv,
2330 "Copying (but not loading) init data len %zd\n", len);
2331 memcpy(priv->ucode_init_data.v_addr, src, len);
2334 /* Bootstrap instructions (5th block) */
2335 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2336 len = priv->ucode_boot.len;
2337 IWL_DEBUG_INFO(priv,
2338 "Copying (but not loading) boot instr len %zd\n", len);
2339 memcpy(priv->ucode_boot.v_addr, src, len);
2341 /* We have our copies now, allow OS release its copies */
2342 release_firmware(ucode_raw);
2346 IWL_ERR(priv, "failed to allocate pci memory\n");
2348 iwl3945_dealloc_ucode_pci(priv);
2351 release_firmware(ucode_raw);
2359 * iwl3945_set_ucode_ptrs - Set uCode address location
2361 * Tell initialization uCode where to find runtime uCode.
2363 * BSM registers initially contain pointers to initialization uCode.
2364 * We need to replace them to load runtime uCode inst and data,
2365 * and to save runtime data when powering down.
2367 static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
2372 /* bits 31:0 for 3945 */
2373 pinst = priv->ucode_code.p_addr;
2374 pdata = priv->ucode_data_backup.p_addr;
2376 /* Tell bootstrap uCode where to find image to load */
2377 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2378 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2379 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
2380 priv->ucode_data.len);
2382 /* Inst byte count must be last to set up, bit 31 signals uCode
2383 * that all new ptr/size info is in place */
2384 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
2385 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2387 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
2393 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
2395 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2397 * Tell "initialize" uCode to go ahead and load the runtime uCode.
2399 static void iwl3945_init_alive_start(struct iwl_priv *priv)
2401 /* Check alive response for "valid" sign from uCode */
2402 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2403 /* We had an error bringing up the hardware, so take it
2404 * all the way back down so we can try again */
2405 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
2409 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2410 * This is a paranoid check, because we would not have gotten the
2411 * "initialize" alive if code weren't properly loaded. */
2412 if (iwl3945_verify_ucode(priv)) {
2413 /* Runtime instruction load was bad;
2414 * take it all the way back down so we can try again */
2415 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
2419 /* Send pointers to protocol/runtime uCode image ... init code will
2420 * load and launch runtime uCode, which will send us another "Alive"
2422 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
2423 if (iwl3945_set_ucode_ptrs(priv)) {
2424 /* Runtime instruction load won't happen;
2425 * take it all the way back down so we can try again */
2426 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
2432 queue_work(priv->workqueue, &priv->restart);
2436 * iwl3945_alive_start - called after REPLY_ALIVE notification received
2437 * from protocol/runtime uCode (initialization uCode's
2438 * Alive gets handled by iwl3945_init_alive_start()).
2440 static void iwl3945_alive_start(struct iwl_priv *priv)
2442 int thermal_spin = 0;
2445 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
2447 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2448 /* We had an error bringing up the hardware, so take it
2449 * all the way back down so we can try again */
2450 IWL_DEBUG_INFO(priv, "Alive failed.\n");
2454 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2455 * This is a paranoid check, because we would not have gotten the
2456 * "runtime" alive if code weren't properly loaded. */
2457 if (iwl3945_verify_ucode(priv)) {
2458 /* Runtime instruction load was bad;
2459 * take it all the way back down so we can try again */
2460 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
2464 iwl_clear_stations_table(priv);
2466 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
2467 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
2470 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2471 /* if RFKILL is not on, then wait for thermal
2472 * sensor in adapter to kick in */
2473 while (iwl3945_hw_get_temperature(priv) == 0) {
2479 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
2482 set_bit(STATUS_RF_KILL_HW, &priv->status);
2484 /* After the ALIVE response, we can send commands to 3945 uCode */
2485 set_bit(STATUS_ALIVE, &priv->status);
2487 if (iwl_is_rfkill(priv))
2490 ieee80211_wake_queues(priv->hw);
2492 priv->active_rate = priv->rates_mask;
2493 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2495 iwl_power_update_mode(priv, false);
2497 if (iwl_is_associated(priv)) {
2498 struct iwl3945_rxon_cmd *active_rxon =
2499 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
2501 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2502 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2504 /* Initialize our rx_config data */
2505 iwl_connection_init_rx_config(priv, priv->iw_mode);
2508 /* Configure Bluetooth device coexistence support */
2509 iwl_send_bt_config(priv);
2511 /* Configure the adapter for unassociated operation */
2512 iwlcore_commit_rxon(priv);
2514 iwl3945_reg_txpower_periodic(priv);
2516 iwl3945_led_register(priv);
2518 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
2519 set_bit(STATUS_READY, &priv->status);
2520 wake_up_interruptible(&priv->wait_command_queue);
2522 /* reassociate for ADHOC mode */
2523 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
2524 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
2527 iwl_mac_beacon_update(priv->hw, beacon);
2530 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
2531 iwl_set_mode(priv, priv->iw_mode);
2536 queue_work(priv->workqueue, &priv->restart);
2539 static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
2541 static void __iwl3945_down(struct iwl_priv *priv)
2543 unsigned long flags;
2544 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2545 struct ieee80211_conf *conf = NULL;
2547 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
2549 conf = ieee80211_get_hw_conf(priv->hw);
2552 set_bit(STATUS_EXIT_PENDING, &priv->status);
2554 iwl3945_led_unregister(priv);
2555 iwl_clear_stations_table(priv);
2557 /* Unblock any waiting calls */
2558 wake_up_interruptible_all(&priv->wait_command_queue);
2560 /* Wipe out the EXIT_PENDING status bit if we are not actually
2561 * exiting the module */
2563 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2565 /* stop and reset the on-board processor */
2566 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
2568 /* tell the device to stop sending interrupts */
2569 spin_lock_irqsave(&priv->lock, flags);
2570 iwl_disable_interrupts(priv);
2571 spin_unlock_irqrestore(&priv->lock, flags);
2572 iwl_synchronize_irq(priv);
2574 if (priv->mac80211_registered)
2575 ieee80211_stop_queues(priv->hw);
2577 /* If we have not previously called iwl3945_init() then
2578 * clear all bits but the RF Kill bits and return */
2579 if (!iwl_is_init(priv)) {
2580 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2582 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2583 STATUS_GEO_CONFIGURED |
2584 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2585 STATUS_EXIT_PENDING;
2589 /* ...otherwise clear out all the status bits but the RF Kill
2590 * bit and continue taking the NIC down. */
2591 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2593 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2594 STATUS_GEO_CONFIGURED |
2595 test_bit(STATUS_FW_ERROR, &priv->status) <<
2597 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2598 STATUS_EXIT_PENDING;
2600 priv->cfg->ops->lib->apm_ops.reset(priv);
2601 spin_lock_irqsave(&priv->lock, flags);
2602 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2603 spin_unlock_irqrestore(&priv->lock, flags);
2605 iwl3945_hw_txq_ctx_stop(priv);
2606 iwl3945_hw_rxq_stop(priv);
2608 iwl_write_prph(priv, APMG_CLK_DIS_REG,
2609 APMG_CLK_VAL_DMA_CLK_RQT);
2614 priv->cfg->ops->lib->apm_ops.stop(priv);
2616 priv->cfg->ops->lib->apm_ops.reset(priv);
2619 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
2621 if (priv->ibss_beacon)
2622 dev_kfree_skb(priv->ibss_beacon);
2623 priv->ibss_beacon = NULL;
2625 /* clear out any free frames */
2626 iwl3945_clear_free_frames(priv);
2629 static void iwl3945_down(struct iwl_priv *priv)
2631 mutex_lock(&priv->mutex);
2632 __iwl3945_down(priv);
2633 mutex_unlock(&priv->mutex);
2635 iwl3945_cancel_deferred_work(priv);
2638 #define MAX_HW_RESTARTS 5
2640 static int __iwl3945_up(struct iwl_priv *priv)
2644 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2645 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
2649 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
2650 IWL_ERR(priv, "ucode not available for device bring up\n");
2654 /* If platform's RF_KILL switch is NOT set to KILL */
2655 if (iwl_read32(priv, CSR_GP_CNTRL) &
2656 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2657 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2659 set_bit(STATUS_RF_KILL_HW, &priv->status);
2660 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2664 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2666 rc = iwl3945_hw_nic_init(priv);
2668 IWL_ERR(priv, "Unable to int nic\n");
2672 /* make sure rfkill handshake bits are cleared */
2673 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2674 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
2675 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2677 /* clear (again), then enable host interrupts */
2678 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2679 iwl_enable_interrupts(priv);
2681 /* really make sure rfkill handshake bits are cleared */
2682 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2683 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2685 /* Copy original ucode data image from disk into backup cache.
2686 * This will be used to initialize the on-board processor's
2687 * data SRAM for a clean start when the runtime program first loads. */
2688 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
2689 priv->ucode_data.len);
2691 /* We return success when we resume from suspend and rf_kill is on. */
2692 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2695 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2697 iwl_clear_stations_table(priv);
2699 /* load bootstrap state machine,
2700 * load bootstrap program into processor's memory,
2701 * prepare to load the "initialize" uCode */
2702 priv->cfg->ops->lib->load_ucode(priv);
2706 "Unable to set up bootstrap uCode: %d\n", rc);
2710 /* start card; "initialize" will load runtime ucode */
2711 iwl3945_nic_start(priv);
2713 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
2718 set_bit(STATUS_EXIT_PENDING, &priv->status);
2719 __iwl3945_down(priv);
2720 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2722 /* tried to restart and config the device for as long as our
2723 * patience could withstand */
2724 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
2729 /*****************************************************************************
2731 * Workqueue callbacks
2733 *****************************************************************************/
2735 static void iwl3945_bg_init_alive_start(struct work_struct *data)
2737 struct iwl_priv *priv =
2738 container_of(data, struct iwl_priv, init_alive_start.work);
2740 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2743 mutex_lock(&priv->mutex);
2744 iwl3945_init_alive_start(priv);
2745 mutex_unlock(&priv->mutex);
2748 static void iwl3945_bg_alive_start(struct work_struct *data)
2750 struct iwl_priv *priv =
2751 container_of(data, struct iwl_priv, alive_start.work);
2753 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2756 mutex_lock(&priv->mutex);
2757 iwl3945_alive_start(priv);
2758 mutex_unlock(&priv->mutex);
2761 static void iwl3945_rfkill_poll(struct work_struct *data)
2763 struct iwl_priv *priv =
2764 container_of(data, struct iwl_priv, rfkill_poll.work);
2766 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2767 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2769 set_bit(STATUS_RF_KILL_HW, &priv->status);
2771 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
2772 test_bit(STATUS_RF_KILL_HW, &priv->status));
2774 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
2775 round_jiffies_relative(2 * HZ));
2779 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
2780 static void iwl3945_bg_request_scan(struct work_struct *data)
2782 struct iwl_priv *priv =
2783 container_of(data, struct iwl_priv, request_scan);
2784 struct iwl_host_cmd cmd = {
2785 .id = REPLY_SCAN_CMD,
2786 .len = sizeof(struct iwl3945_scan_cmd),
2787 .meta.flags = CMD_SIZE_HUGE,
2790 struct iwl3945_scan_cmd *scan;
2791 struct ieee80211_conf *conf = NULL;
2793 enum ieee80211_band band;
2794 bool is_active = false;
2796 conf = ieee80211_get_hw_conf(priv->hw);
2798 mutex_lock(&priv->mutex);
2800 cancel_delayed_work(&priv->scan_check);
2802 if (!iwl_is_ready(priv)) {
2803 IWL_WARN(priv, "request scan called when driver not ready.\n");
2807 /* Make sure the scan wasn't canceled before this queued work
2808 * was given the chance to run... */
2809 if (!test_bit(STATUS_SCANNING, &priv->status))
2812 /* This should never be called or scheduled if there is currently
2813 * a scan active in the hardware. */
2814 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
2815 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
2816 "Ignoring second request.\n");
2821 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2822 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
2826 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2828 "Scan request while abort pending. Queuing.\n");
2832 if (iwl_is_rfkill(priv)) {
2833 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
2837 if (!test_bit(STATUS_READY, &priv->status)) {
2839 "Scan request while uninitialized. Queuing.\n");
2843 if (!priv->scan_bands) {
2844 IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
2849 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
2850 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
2857 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
2859 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2860 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2862 if (iwl_is_associated(priv)) {
2865 u32 suspend_time = 100;
2866 u32 scan_suspend_time = 100;
2867 unsigned long flags;
2869 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
2871 spin_lock_irqsave(&priv->lock, flags);
2872 interval = priv->beacon_int;
2873 spin_unlock_irqrestore(&priv->lock, flags);
2875 scan->suspend_time = 0;
2876 scan->max_out_time = cpu_to_le32(200 * 1024);
2878 interval = suspend_time;
2880 * suspend time format:
2881 * 0-19: beacon interval in usec (time before exec.)
2883 * 24-31: number of beacons (suspend between channels)
2886 extra = (suspend_time / interval) << 24;
2887 scan_suspend_time = 0xFF0FFFFF &
2888 (extra | ((suspend_time % interval) * 1024));
2890 scan->suspend_time = cpu_to_le32(scan_suspend_time);
2891 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
2892 scan_suspend_time, interval);
2895 if (priv->scan_request->n_ssids) {
2897 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2898 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2899 /* always does wildcard anyway */
2900 if (!priv->scan_request->ssids[i].ssid_len)
2902 scan->direct_scan[p].id = WLAN_EID_SSID;
2903 scan->direct_scan[p].len =
2904 priv->scan_request->ssids[i].ssid_len;
2905 memcpy(scan->direct_scan[p].ssid,
2906 priv->scan_request->ssids[i].ssid,
2907 priv->scan_request->ssids[i].ssid_len);
2913 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
2915 /* We don't build a direct scan probe request; the uCode will do
2916 * that based on the direct_mask added to each channel entry */
2917 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
2918 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
2919 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2921 /* flags + rate selection */
2923 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
2924 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2925 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
2926 scan->good_CRC_th = 0;
2927 band = IEEE80211_BAND_2GHZ;
2928 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
2929 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
2931 * If active scaning is requested but a certain channel
2932 * is marked passive, we can do active scanning if we
2933 * detect transmissions.
2935 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH : 0;
2936 band = IEEE80211_BAND_5GHZ;
2938 IWL_WARN(priv, "Invalid scan band count\n");
2942 scan->tx_cmd.len = cpu_to_le16(
2943 iwl_fill_probe_req(priv,
2944 (struct ieee80211_mgmt *)scan->data,
2945 priv->scan_request->ie,
2946 priv->scan_request->ie_len,
2947 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2949 /* select Rx antennas */
2950 scan->flags |= iwl3945_get_antenna_flags(priv);
2952 if (iwl_is_monitor_mode(priv))
2953 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
2955 scan->channel_count =
2956 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
2957 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
2959 if (scan->channel_count == 0) {
2960 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
2964 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
2965 scan->channel_count * sizeof(struct iwl3945_scan_channel);
2967 scan->len = cpu_to_le16(cmd.len);
2969 set_bit(STATUS_SCAN_HW, &priv->status);
2970 rc = iwl_send_cmd_sync(priv, &cmd);
2974 queue_delayed_work(priv->workqueue, &priv->scan_check,
2975 IWL_SCAN_CHECK_WATCHDOG);
2977 mutex_unlock(&priv->mutex);
2981 /* can not perform scan make sure we clear scanning
2982 * bits from status so next scan request can be performed.
2983 * if we dont clear scanning status bit here all next scan
2986 clear_bit(STATUS_SCAN_HW, &priv->status);
2987 clear_bit(STATUS_SCANNING, &priv->status);
2989 /* inform mac80211 scan aborted */
2990 queue_work(priv->workqueue, &priv->scan_completed);
2991 mutex_unlock(&priv->mutex);
2994 static void iwl3945_bg_up(struct work_struct *data)
2996 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
2998 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3001 mutex_lock(&priv->mutex);
3003 mutex_unlock(&priv->mutex);
3006 static void iwl3945_bg_restart(struct work_struct *data)
3008 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
3010 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3013 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
3014 mutex_lock(&priv->mutex);
3017 mutex_unlock(&priv->mutex);
3019 ieee80211_restart_hw(priv->hw);
3022 queue_work(priv->workqueue, &priv->up);
3026 static void iwl3945_bg_rx_replenish(struct work_struct *data)
3028 struct iwl_priv *priv =
3029 container_of(data, struct iwl_priv, rx_replenish);
3031 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3034 mutex_lock(&priv->mutex);
3035 iwl3945_rx_replenish(priv);
3036 mutex_unlock(&priv->mutex);
3039 #define IWL_DELAY_NEXT_SCAN (HZ*2)
3041 void iwl3945_post_associate(struct iwl_priv *priv)
3044 struct ieee80211_conf *conf = NULL;
3046 if (priv->iw_mode == NL80211_IFTYPE_AP) {
3047 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
3052 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
3053 priv->assoc_id, priv->active_rxon.bssid_addr);
3055 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3058 if (!priv->vif || !priv->is_open)
3061 iwl_scan_cancel_timeout(priv, 200);
3063 conf = ieee80211_get_hw_conf(priv->hw);
3065 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3066 iwlcore_commit_rxon(priv);
3068 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
3069 iwl3945_setup_rxon_timing(priv);
3070 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3071 sizeof(priv->rxon_timing), &priv->rxon_timing);
3073 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
3074 "Attempting to continue.\n");
3076 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3078 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3080 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
3081 priv->assoc_id, priv->beacon_int);
3083 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3084 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3086 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3088 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3089 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
3090 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
3092 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3094 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
3095 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3099 iwlcore_commit_rxon(priv);
3101 switch (priv->iw_mode) {
3102 case NL80211_IFTYPE_STATION:
3103 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
3106 case NL80211_IFTYPE_ADHOC:
3109 iwl_add_station(priv, priv->bssid, 0, CMD_SYNC, NULL);
3110 iwl3945_sync_sta(priv, IWL_STA_ID,
3111 (priv->band == IEEE80211_BAND_5GHZ) ?
3112 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
3114 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
3115 iwl3945_send_beacon_cmd(priv);
3120 IWL_ERR(priv, "%s Should not be called in %d mode\n",
3121 __func__, priv->iw_mode);
3125 iwl_activate_qos(priv, 0);
3127 /* we have just associated, don't start scan too early */
3128 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
3131 /*****************************************************************************
3133 * mac80211 entry point functions
3135 *****************************************************************************/
3137 #define UCODE_READY_TIMEOUT (2 * HZ)
3139 static int iwl3945_mac_start(struct ieee80211_hw *hw)
3141 struct iwl_priv *priv = hw->priv;
3144 IWL_DEBUG_MAC80211(priv, "enter\n");
3146 /* we should be verifying the device is ready to be opened */
3147 mutex_lock(&priv->mutex);
3149 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3150 * ucode filename and max sizes are card-specific. */
3152 if (!priv->ucode_code.len) {
3153 ret = iwl3945_read_ucode(priv);
3155 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
3156 mutex_unlock(&priv->mutex);
3157 goto out_release_irq;
3161 ret = __iwl3945_up(priv);
3163 mutex_unlock(&priv->mutex);
3166 goto out_release_irq;
3168 IWL_DEBUG_INFO(priv, "Start UP work.\n");
3170 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3171 * mac80211 will not be run successfully. */
3172 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3173 test_bit(STATUS_READY, &priv->status),
3174 UCODE_READY_TIMEOUT);
3176 if (!test_bit(STATUS_READY, &priv->status)) {
3178 "Wait for START_ALIVE timeout after %dms.\n",
3179 jiffies_to_msecs(UCODE_READY_TIMEOUT));
3181 goto out_release_irq;
3185 /* ucode is running and will send rfkill notifications,
3186 * no need to poll the killswitch state anymore */
3187 cancel_delayed_work(&priv->rfkill_poll);
3190 IWL_DEBUG_MAC80211(priv, "leave\n");
3195 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
3199 static void iwl3945_mac_stop(struct ieee80211_hw *hw)
3201 struct iwl_priv *priv = hw->priv;
3203 IWL_DEBUG_MAC80211(priv, "enter\n");
3205 if (!priv->is_open) {
3206 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
3212 if (iwl_is_ready_rf(priv)) {
3213 /* stop mac, cancel any scan request and clear
3214 * RXON_FILTER_ASSOC_MSK BIT
3216 mutex_lock(&priv->mutex);
3217 iwl_scan_cancel_timeout(priv, 100);
3218 mutex_unlock(&priv->mutex);
3223 flush_workqueue(priv->workqueue);
3225 /* start polling the killswitch state again */
3226 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3227 round_jiffies_relative(2 * HZ));
3229 IWL_DEBUG_MAC80211(priv, "leave\n");
3232 static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3234 struct iwl_priv *priv = hw->priv;
3236 IWL_DEBUG_MAC80211(priv, "enter\n");
3238 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
3239 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
3241 if (iwl3945_tx_skb(priv, skb))
3242 dev_kfree_skb_any(skb);
3244 IWL_DEBUG_MAC80211(priv, "leave\n");
3245 return NETDEV_TX_OK;
3248 void iwl3945_config_ap(struct iwl_priv *priv)
3252 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3255 /* The following should be done only at AP bring up */
3256 if (!(iwl_is_associated(priv))) {
3258 /* RXON - unassoc (to set timing command) */
3259 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3260 iwlcore_commit_rxon(priv);
3263 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
3264 iwl3945_setup_rxon_timing(priv);
3265 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3266 sizeof(priv->rxon_timing),
3267 &priv->rxon_timing);
3269 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
3270 "Attempting to continue.\n");
3272 /* FIXME: what should be the assoc_id for AP? */
3273 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3274 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3275 priv->staging_rxon.flags |=
3276 RXON_FLG_SHORT_PREAMBLE_MSK;
3278 priv->staging_rxon.flags &=
3279 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3281 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3282 if (priv->assoc_capability &
3283 WLAN_CAPABILITY_SHORT_SLOT_TIME)
3284 priv->staging_rxon.flags |=
3285 RXON_FLG_SHORT_SLOT_MSK;
3287 priv->staging_rxon.flags &=
3288 ~RXON_FLG_SHORT_SLOT_MSK;
3290 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
3291 priv->staging_rxon.flags &=
3292 ~RXON_FLG_SHORT_SLOT_MSK;
3294 /* restore RXON assoc */
3295 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3296 iwlcore_commit_rxon(priv);
3297 iwl_add_station(priv, iwl_bcast_addr, 0, CMD_SYNC, NULL);
3299 iwl3945_send_beacon_cmd(priv);
3301 /* FIXME - we need to add code here to detect a totally new
3302 * configuration, reset the AP, unassoc, rxon timing, assoc,
3303 * clear sta table, add BCAST sta... */
3306 static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3307 struct ieee80211_vif *vif,
3308 struct ieee80211_sta *sta,
3309 struct ieee80211_key_conf *key)
3311 struct iwl_priv *priv = hw->priv;
3314 u8 sta_id = IWL_INVALID_STATION;
3317 IWL_DEBUG_MAC80211(priv, "enter\n");
3319 if (iwl3945_mod_params.sw_crypto) {
3320 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
3324 addr = sta ? sta->addr : iwl_bcast_addr;
3325 static_key = !iwl_is_associated(priv);
3328 sta_id = iwl_find_station(priv, addr);
3329 if (sta_id == IWL_INVALID_STATION) {
3330 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
3336 mutex_lock(&priv->mutex);
3337 iwl_scan_cancel_timeout(priv, 100);
3338 mutex_unlock(&priv->mutex);
3343 ret = iwl3945_set_static_key(priv, key);
3345 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3346 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
3350 ret = iwl3945_remove_static_key(priv);
3352 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3353 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
3359 IWL_DEBUG_MAC80211(priv, "leave\n");
3364 /*****************************************************************************
3368 *****************************************************************************/
3370 #ifdef CONFIG_IWLWIFI_DEBUG
3373 * The following adds a new attribute to the sysfs representation
3374 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3375 * used for controlling the debug level.
3377 * See the level definitions in iwl for details.
3379 static ssize_t show_debug_level(struct device *d,
3380 struct device_attribute *attr, char *buf)
3382 struct iwl_priv *priv = dev_get_drvdata(d);
3384 return sprintf(buf, "0x%08X\n", priv->debug_level);
3386 static ssize_t store_debug_level(struct device *d,
3387 struct device_attribute *attr,
3388 const char *buf, size_t count)
3390 struct iwl_priv *priv = dev_get_drvdata(d);
3394 ret = strict_strtoul(buf, 0, &val);
3396 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
3398 priv->debug_level = val;
3400 return strnlen(buf, count);
3403 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3404 show_debug_level, store_debug_level);
3406 #endif /* CONFIG_IWLWIFI_DEBUG */
3408 static ssize_t show_temperature(struct device *d,
3409 struct device_attribute *attr, char *buf)
3411 struct iwl_priv *priv = dev_get_drvdata(d);
3413 if (!iwl_is_alive(priv))
3416 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
3419 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3421 static ssize_t show_tx_power(struct device *d,
3422 struct device_attribute *attr, char *buf)
3424 struct iwl_priv *priv = dev_get_drvdata(d);
3425 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
3428 static ssize_t store_tx_power(struct device *d,
3429 struct device_attribute *attr,
3430 const char *buf, size_t count)
3432 struct iwl_priv *priv = dev_get_drvdata(d);
3433 char *p = (char *)buf;
3436 val = simple_strtoul(p, &p, 10);
3438 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
3440 iwl3945_hw_reg_set_txpower(priv, val);
3445 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3447 static ssize_t show_flags(struct device *d,
3448 struct device_attribute *attr, char *buf)
3450 struct iwl_priv *priv = dev_get_drvdata(d);
3452 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
3455 static ssize_t store_flags(struct device *d,
3456 struct device_attribute *attr,
3457 const char *buf, size_t count)
3459 struct iwl_priv *priv = dev_get_drvdata(d);
3460 u32 flags = simple_strtoul(buf, NULL, 0);
3462 mutex_lock(&priv->mutex);
3463 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
3464 /* Cancel any currently running scans... */
3465 if (iwl_scan_cancel_timeout(priv, 100))
3466 IWL_WARN(priv, "Could not cancel scan.\n");
3468 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
3470 priv->staging_rxon.flags = cpu_to_le32(flags);
3471 iwlcore_commit_rxon(priv);
3474 mutex_unlock(&priv->mutex);
3479 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3481 static ssize_t show_filter_flags(struct device *d,
3482 struct device_attribute *attr, char *buf)
3484 struct iwl_priv *priv = dev_get_drvdata(d);
3486 return sprintf(buf, "0x%04X\n",
3487 le32_to_cpu(priv->active_rxon.filter_flags));
3490 static ssize_t store_filter_flags(struct device *d,
3491 struct device_attribute *attr,
3492 const char *buf, size_t count)
3494 struct iwl_priv *priv = dev_get_drvdata(d);
3495 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3497 mutex_lock(&priv->mutex);
3498 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
3499 /* Cancel any currently running scans... */
3500 if (iwl_scan_cancel_timeout(priv, 100))
3501 IWL_WARN(priv, "Could not cancel scan.\n");
3503 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
3504 "0x%04X\n", filter_flags);
3505 priv->staging_rxon.filter_flags =
3506 cpu_to_le32(filter_flags);
3507 iwlcore_commit_rxon(priv);
3510 mutex_unlock(&priv->mutex);
3515 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3516 store_filter_flags);
3518 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
3520 static ssize_t show_measurement(struct device *d,
3521 struct device_attribute *attr, char *buf)
3523 struct iwl_priv *priv = dev_get_drvdata(d);
3524 struct iwl_spectrum_notification measure_report;
3525 u32 size = sizeof(measure_report), len = 0, ofs = 0;
3526 u8 *data = (u8 *)&measure_report;
3527 unsigned long flags;
3529 spin_lock_irqsave(&priv->lock, flags);
3530 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3531 spin_unlock_irqrestore(&priv->lock, flags);
3534 memcpy(&measure_report, &priv->measure_report, size);
3535 priv->measurement_status = 0;
3536 spin_unlock_irqrestore(&priv->lock, flags);
3538 while (size && (PAGE_SIZE - len)) {
3539 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3540 PAGE_SIZE - len, 1);
3542 if (PAGE_SIZE - len)
3546 size -= min(size, 16U);
3552 static ssize_t store_measurement(struct device *d,
3553 struct device_attribute *attr,
3554 const char *buf, size_t count)
3556 struct iwl_priv *priv = dev_get_drvdata(d);
3557 struct ieee80211_measurement_params params = {
3558 .channel = le16_to_cpu(priv->active_rxon.channel),
3559 .start_time = cpu_to_le64(priv->last_tsf),
3560 .duration = cpu_to_le16(1),
3562 u8 type = IWL_MEASURE_BASIC;
3568 strncpy(buffer, buf, min(sizeof(buffer), count));
3569 channel = simple_strtoul(p, NULL, 0);
3571 params.channel = channel;
3574 while (*p && *p != ' ')
3577 type = simple_strtoul(p + 1, NULL, 0);
3580 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
3581 "channel %d (for '%s')\n", type, params.channel, buf);
3582 iwl3945_get_measurement(priv, ¶ms, type);
3587 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3588 show_measurement, store_measurement);
3589 #endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
3591 static ssize_t store_retry_rate(struct device *d,
3592 struct device_attribute *attr,
3593 const char *buf, size_t count)
3595 struct iwl_priv *priv = dev_get_drvdata(d);
3597 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3598 if (priv->retry_rate <= 0)
3599 priv->retry_rate = 1;
3604 static ssize_t show_retry_rate(struct device *d,
3605 struct device_attribute *attr, char *buf)
3607 struct iwl_priv *priv = dev_get_drvdata(d);
3608 return sprintf(buf, "%d", priv->retry_rate);
3611 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3615 static ssize_t store_power_level(struct device *d,
3616 struct device_attribute *attr,
3617 const char *buf, size_t count)
3619 struct iwl_priv *priv = dev_get_drvdata(d);
3624 mutex_lock(&priv->mutex);
3626 ret = strict_strtoul(buf, 10, &mode);
3630 ret = iwl_power_set_user_mode(priv, mode);
3632 IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
3638 mutex_unlock(&priv->mutex);
3642 static ssize_t show_power_level(struct device *d,
3643 struct device_attribute *attr, char *buf)
3645 struct iwl_priv *priv = dev_get_drvdata(d);
3646 int mode = priv->power_data.user_power_setting;
3647 int level = priv->power_data.power_mode;
3650 p += sprintf(p, "INDEX:%d\t", level);
3651 p += sprintf(p, "USER:%d\n", mode);
3655 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR,
3656 show_power_level, store_power_level);
3658 #define MAX_WX_STRING 80
3660 /* Values are in microsecond */
3661 static const s32 timeout_duration[] = {
3668 static const s32 period_duration[] = {
3676 static ssize_t show_channels(struct device *d,
3677 struct device_attribute *attr, char *buf)
3679 /* all this shit doesn't belong into sysfs anyway */
3683 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3685 static ssize_t show_statistics(struct device *d,
3686 struct device_attribute *attr, char *buf)
3688 struct iwl_priv *priv = dev_get_drvdata(d);
3689 u32 size = sizeof(struct iwl3945_notif_statistics);
3690 u32 len = 0, ofs = 0;
3691 u8 *data = (u8 *)&priv->statistics_39;
3694 if (!iwl_is_alive(priv))
3697 mutex_lock(&priv->mutex);
3698 rc = iwl_send_statistics_request(priv, 0);
3699 mutex_unlock(&priv->mutex);
3703 "Error sending statistics request: 0x%08X\n", rc);
3707 while (size && (PAGE_SIZE - len)) {
3708 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3709 PAGE_SIZE - len, 1);
3711 if (PAGE_SIZE - len)
3715 size -= min(size, 16U);
3721 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
3723 static ssize_t show_antenna(struct device *d,
3724 struct device_attribute *attr, char *buf)
3726 struct iwl_priv *priv = dev_get_drvdata(d);
3728 if (!iwl_is_alive(priv))
3731 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
3734 static ssize_t store_antenna(struct device *d,
3735 struct device_attribute *attr,
3736 const char *buf, size_t count)
3738 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
3744 if (sscanf(buf, "%1i", &ant) != 1) {
3745 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
3749 if ((ant >= 0) && (ant <= 2)) {
3750 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
3751 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
3753 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
3759 static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3761 static ssize_t show_status(struct device *d,
3762 struct device_attribute *attr, char *buf)
3764 struct iwl_priv *priv = dev_get_drvdata(d);
3765 if (!iwl_is_alive(priv))
3767 return sprintf(buf, "0x%08x\n", (int)priv->status);
3770 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3772 static ssize_t dump_error_log(struct device *d,
3773 struct device_attribute *attr,
3774 const char *buf, size_t count)
3776 struct iwl_priv *priv = dev_get_drvdata(d);
3777 char *p = (char *)buf;
3780 iwl3945_dump_nic_error_log(priv);
3782 return strnlen(buf, count);
3785 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3787 static ssize_t dump_event_log(struct device *d,
3788 struct device_attribute *attr,
3789 const char *buf, size_t count)
3791 struct iwl_priv *priv = dev_get_drvdata(d);
3792 char *p = (char *)buf;
3795 iwl3945_dump_nic_event_log(priv);
3797 return strnlen(buf, count);
3800 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
3802 /*****************************************************************************
3804 * driver setup and tear down
3806 *****************************************************************************/
3808 static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
3810 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
3812 init_waitqueue_head(&priv->wait_command_queue);
3814 INIT_WORK(&priv->up, iwl3945_bg_up);
3815 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3816 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
3817 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
3818 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3819 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
3820 INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
3821 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
3822 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
3823 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
3824 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
3826 iwl3945_hw_setup_deferred_work(priv);
3828 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
3829 iwl3945_irq_tasklet, (unsigned long)priv);
3832 static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
3834 iwl3945_hw_cancel_deferred_work(priv);
3836 cancel_delayed_work_sync(&priv->init_alive_start);
3837 cancel_delayed_work(&priv->scan_check);
3838 cancel_delayed_work(&priv->alive_start);
3839 cancel_work_sync(&priv->beacon_update);
3842 static struct attribute *iwl3945_sysfs_entries[] = {
3843 &dev_attr_antenna.attr,
3844 &dev_attr_channels.attr,
3845 &dev_attr_dump_errors.attr,
3846 &dev_attr_dump_events.attr,
3847 &dev_attr_flags.attr,
3848 &dev_attr_filter_flags.attr,
3849 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
3850 &dev_attr_measurement.attr,
3852 &dev_attr_power_level.attr,
3853 &dev_attr_retry_rate.attr,
3854 &dev_attr_statistics.attr,
3855 &dev_attr_status.attr,
3856 &dev_attr_temperature.attr,
3857 &dev_attr_tx_power.attr,
3858 #ifdef CONFIG_IWLWIFI_DEBUG
3859 &dev_attr_debug_level.attr,
3864 static struct attribute_group iwl3945_attribute_group = {
3865 .name = NULL, /* put in device directory */
3866 .attrs = iwl3945_sysfs_entries,
3869 static struct ieee80211_ops iwl3945_hw_ops = {
3870 .tx = iwl3945_mac_tx,
3871 .start = iwl3945_mac_start,
3872 .stop = iwl3945_mac_stop,
3873 .add_interface = iwl_mac_add_interface,
3874 .remove_interface = iwl_mac_remove_interface,
3875 .config = iwl_mac_config,
3876 .configure_filter = iwl_configure_filter,
3877 .set_key = iwl3945_mac_set_key,
3878 .get_tx_stats = iwl_mac_get_tx_stats,
3879 .conf_tx = iwl_mac_conf_tx,
3880 .reset_tsf = iwl_mac_reset_tsf,
3881 .bss_info_changed = iwl_bss_info_changed,
3882 .hw_scan = iwl_mac_hw_scan
3885 static int iwl3945_init_drv(struct iwl_priv *priv)
3888 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
3890 priv->retry_rate = 1;
3891 priv->ibss_beacon = NULL;
3893 spin_lock_init(&priv->lock);
3894 spin_lock_init(&priv->sta_lock);
3895 spin_lock_init(&priv->hcmd_lock);
3897 INIT_LIST_HEAD(&priv->free_frames);
3899 mutex_init(&priv->mutex);
3901 /* Clear the driver's (not device's) station table */
3902 iwl_clear_stations_table(priv);
3904 priv->data_retry_limit = -1;
3905 priv->ieee_channels = NULL;
3906 priv->ieee_rates = NULL;
3907 priv->band = IEEE80211_BAND_2GHZ;
3909 priv->iw_mode = NL80211_IFTYPE_STATION;
3911 iwl_reset_qos(priv);
3913 priv->qos_data.qos_active = 0;
3914 priv->qos_data.qos_cap.val = 0;
3916 priv->rates_mask = IWL_RATES_MASK;
3917 /* If power management is turned on, default to CAM mode */
3918 priv->power_mode = IWL_POWER_MODE_CAM;
3919 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
3921 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3922 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3927 ret = iwl_init_channel_map(priv);
3929 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3933 /* Set up txpower settings in driver for all channels */
3934 if (iwl3945_txpower_set_from_eeprom(priv)) {
3936 goto err_free_channel_map;
3939 ret = iwlcore_init_geos(priv);
3941 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3942 goto err_free_channel_map;
3944 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3948 err_free_channel_map:
3949 iwl_free_channel_map(priv);
3954 static int iwl3945_setup_mac(struct iwl_priv *priv)
3957 struct ieee80211_hw *hw = priv->hw;
3959 hw->rate_control_algorithm = "iwl-3945-rs";
3960 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
3962 /* Tell mac80211 our characteristics */
3963 hw->flags = IEEE80211_HW_SIGNAL_DBM |
3964 IEEE80211_HW_NOISE_DBM |
3965 IEEE80211_HW_SPECTRUM_MGMT;
3967 hw->wiphy->interface_modes =
3968 BIT(NL80211_IFTYPE_STATION) |
3969 BIT(NL80211_IFTYPE_ADHOC);
3971 hw->wiphy->custom_regulatory = true;
3973 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3974 /* we create the 802.11 header and a zero-length SSID element */
3975 hw->wiphy->max_scan_ie_len = IWL_MAX_PROBE_REQUEST - 24 - 2;
3977 /* Default value; 4 EDCA QOS priorities */
3980 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3981 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3982 &priv->bands[IEEE80211_BAND_2GHZ];
3984 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3985 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3986 &priv->bands[IEEE80211_BAND_5GHZ];
3988 ret = ieee80211_register_hw(priv->hw);
3990 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3993 priv->mac80211_registered = 1;
3998 static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4001 struct iwl_priv *priv;
4002 struct ieee80211_hw *hw;
4003 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
4004 struct iwl3945_eeprom *eeprom;
4005 unsigned long flags;
4007 /***********************
4008 * 1. Allocating HW data
4009 * ********************/
4011 /* mac80211 allocates memory for this device instance, including
4012 * space for this driver's private structure */
4013 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
4015 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
4020 SET_IEEE80211_DEV(hw, &pdev->dev);
4022 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
4023 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
4025 "invalid queues_num, should be between %d and %d\n",
4026 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
4028 goto out_ieee80211_free_hw;
4032 * Disabling hardware scan means that mac80211 will perform scans
4033 * "the hard way", rather than using device's scan.
4035 if (iwl3945_mod_params.disable_hw_scan) {
4036 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
4037 iwl3945_hw_ops.hw_scan = NULL;
4041 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
4043 priv->pci_dev = pdev;
4044 priv->inta_mask = CSR_INI_SET_MASK;
4046 #ifdef CONFIG_IWLWIFI_DEBUG
4047 priv->debug_level = iwl3945_mod_params.debug;
4048 atomic_set(&priv->restrict_refcnt, 0);
4051 /***************************
4052 * 2. Initializing PCI bus
4053 * *************************/
4054 if (pci_enable_device(pdev)) {
4056 goto out_ieee80211_free_hw;
4059 pci_set_master(pdev);
4061 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4063 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
4065 IWL_WARN(priv, "No suitable DMA available.\n");
4066 goto out_pci_disable_device;
4069 pci_set_drvdata(pdev, priv);
4070 err = pci_request_regions(pdev, DRV_NAME);
4072 goto out_pci_disable_device;
4074 /***********************
4075 * 3. Read REV Register
4076 * ********************/
4077 priv->hw_base = pci_iomap(pdev, 0, 0);
4078 if (!priv->hw_base) {
4080 goto out_pci_release_regions;
4083 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
4084 (unsigned long long) pci_resource_len(pdev, 0));
4085 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
4087 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4088 * PCI Tx retries from interfering with C3 CPU state */
4089 pci_write_config_byte(pdev, 0x41, 0x00);
4091 /* this spin lock will be used in apm_ops.init and EEPROM access
4092 * we should init now
4094 spin_lock_init(&priv->reg_lock);
4097 err = priv->cfg->ops->lib->apm_ops.init(priv);
4099 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
4103 /***********************
4105 * ********************/
4107 /* Read the EEPROM */
4108 err = iwl_eeprom_init(priv);
4110 IWL_ERR(priv, "Unable to init EEPROM\n");
4113 /* MAC Address location in EEPROM same for 3945/4965 */
4114 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
4115 memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
4116 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
4117 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4119 /***********************
4120 * 5. Setup HW Constants
4121 * ********************/
4122 /* Device-specific setup */
4123 if (iwl3945_hw_set_hw_params(priv)) {
4124 IWL_ERR(priv, "failed to set hw settings\n");
4125 goto out_eeprom_free;
4128 /***********************
4130 * ********************/
4132 err = iwl3945_init_drv(priv);
4134 IWL_ERR(priv, "initializing driver failed\n");
4135 goto out_unset_hw_params;
4138 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4141 /***********************
4143 * ********************/
4145 spin_lock_irqsave(&priv->lock, flags);
4146 iwl_disable_interrupts(priv);
4147 spin_unlock_irqrestore(&priv->lock, flags);
4149 pci_enable_msi(priv->pci_dev);
4151 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr,
4152 IRQF_SHARED, DRV_NAME, priv);
4154 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4155 goto out_disable_msi;
4158 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4160 IWL_ERR(priv, "failed to create sysfs device attributes\n");
4161 goto out_release_irq;
4164 iwl_set_rxon_channel(priv,
4165 &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
4166 iwl3945_setup_deferred_work(priv);
4167 iwl3945_setup_rx_handlers(priv);
4169 /*********************************
4170 * 8. Setup and Register mac80211
4171 * *******************************/
4173 iwl_enable_interrupts(priv);
4175 err = iwl3945_setup_mac(priv);
4177 goto out_remove_sysfs;
4179 err = iwl_dbgfs_register(priv, DRV_NAME);
4181 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4183 /* Start monitoring the killswitch */
4184 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
4190 destroy_workqueue(priv->workqueue);
4191 priv->workqueue = NULL;
4192 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4194 free_irq(priv->pci_dev->irq, priv);
4196 pci_disable_msi(priv->pci_dev);
4197 iwlcore_free_geos(priv);
4198 iwl_free_channel_map(priv);
4199 out_unset_hw_params:
4200 iwl3945_unset_hw_params(priv);
4202 iwl_eeprom_free(priv);
4204 pci_iounmap(pdev, priv->hw_base);
4205 out_pci_release_regions:
4206 pci_release_regions(pdev);
4207 out_pci_disable_device:
4208 pci_set_drvdata(pdev, NULL);
4209 pci_disable_device(pdev);
4210 out_ieee80211_free_hw:
4211 ieee80211_free_hw(priv->hw);
4216 static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
4218 struct iwl_priv *priv = pci_get_drvdata(pdev);
4219 unsigned long flags;
4224 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
4226 iwl_dbgfs_unregister(priv);
4228 set_bit(STATUS_EXIT_PENDING, &priv->status);
4230 if (priv->mac80211_registered) {
4231 ieee80211_unregister_hw(priv->hw);
4232 priv->mac80211_registered = 0;
4237 /* make sure we flush any pending irq or
4238 * tasklet for the driver
4240 spin_lock_irqsave(&priv->lock, flags);
4241 iwl_disable_interrupts(priv);
4242 spin_unlock_irqrestore(&priv->lock, flags);
4244 iwl_synchronize_irq(priv);
4246 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4248 cancel_delayed_work_sync(&priv->rfkill_poll);
4250 iwl3945_dealloc_ucode_pci(priv);
4253 iwl3945_rx_queue_free(priv, &priv->rxq);
4254 iwl3945_hw_txq_ctx_free(priv);
4256 iwl3945_unset_hw_params(priv);
4257 iwl_clear_stations_table(priv);
4259 /*netif_stop_queue(dev); */
4260 flush_workqueue(priv->workqueue);
4262 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
4263 * priv->workqueue... so we can't take down the workqueue
4265 destroy_workqueue(priv->workqueue);
4266 priv->workqueue = NULL;
4268 free_irq(pdev->irq, priv);
4269 pci_disable_msi(pdev);
4271 pci_iounmap(pdev, priv->hw_base);
4272 pci_release_regions(pdev);
4273 pci_disable_device(pdev);
4274 pci_set_drvdata(pdev, NULL);
4276 iwl_free_channel_map(priv);
4277 iwlcore_free_geos(priv);
4279 if (priv->ibss_beacon)
4280 dev_kfree_skb(priv->ibss_beacon);
4282 ieee80211_free_hw(priv->hw);
4286 /*****************************************************************************
4288 * driver and module entry point
4290 *****************************************************************************/
4292 static struct pci_driver iwl3945_driver = {
4294 .id_table = iwl3945_hw_card_ids,
4295 .probe = iwl3945_pci_probe,
4296 .remove = __devexit_p(iwl3945_pci_remove),
4298 .suspend = iwl_pci_suspend,
4299 .resume = iwl_pci_resume,
4303 static int __init iwl3945_init(void)
4307 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4308 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
4310 ret = iwl3945_rate_control_register();
4312 printk(KERN_ERR DRV_NAME
4313 "Unable to register rate control algorithm: %d\n", ret);
4317 ret = pci_register_driver(&iwl3945_driver);
4319 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
4320 goto error_register;
4326 iwl3945_rate_control_unregister();
4330 static void __exit iwl3945_exit(void)
4332 pci_unregister_driver(&iwl3945_driver);
4333 iwl3945_rate_control_unregister();
4336 MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
4338 module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
4339 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
4340 module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
4341 MODULE_PARM_DESC(swcrypto,
4342 "using software crypto (default 1 [software])\n");
4343 module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
4344 MODULE_PARM_DESC(debug, "debug output mask");
4345 module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
4346 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
4348 module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
4349 MODULE_PARM_DESC(queues_num, "number of hw queues.");
4351 module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, 0444);
4352 MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4354 module_exit(iwl3945_exit);
4355 module_init(iwl3945_init);