proc: use seq_puts()/seq_putc() where possible
[linux-2.6/next.git] / drivers / net / wireless / iwlwifi / iwl3945-base.c
blob371abbf60eac4949090aa19eef4a59797ffadd3a
1 /******************************************************************************
3 * Copyright(c) 2003 - 2010 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
15 * more details.
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/pci.h>
36 #include <linux/pci-aspm.h>
37 #include <linux/slab.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/delay.h>
40 #include <linux/sched.h>
41 #include <linux/skbuff.h>
42 #include <linux/netdevice.h>
43 #include <linux/wireless.h>
44 #include <linux/firmware.h>
45 #include <linux/etherdevice.h>
46 #include <linux/if_arp.h>
48 #include <net/ieee80211_radiotap.h>
49 #include <net/mac80211.h>
51 #include <asm/div64.h>
53 #define DRV_NAME "iwl3945"
55 #include "iwl-fh.h"
56 #include "iwl-3945-fh.h"
57 #include "iwl-commands.h"
58 #include "iwl-sta.h"
59 #include "iwl-3945.h"
60 #include "iwl-core.h"
61 #include "iwl-helpers.h"
62 #include "iwl-dev.h"
63 #include "iwl-spectrum.h"
64 #include "iwl-legacy.h"
67 * module name, copyright, version, etc.
70 #define DRV_DESCRIPTION \
71 "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
73 #ifdef CONFIG_IWLWIFI_DEBUG
74 #define VD "d"
75 #else
76 #define VD
77 #endif
80 * add "s" to indicate spectrum measurement included.
81 * we add it here to be consistent with previous releases in which
82 * this was configurable.
84 #define DRV_VERSION IWLWIFI_VERSION VD "s"
85 #define DRV_COPYRIGHT "Copyright(c) 2003-2010 Intel Corporation"
86 #define DRV_AUTHOR "<ilw@linux.intel.com>"
88 MODULE_DESCRIPTION(DRV_DESCRIPTION);
89 MODULE_VERSION(DRV_VERSION);
90 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
91 MODULE_LICENSE("GPL");
93 /* module parameters */
94 struct iwl_mod_params iwl3945_mod_params = {
95 .sw_crypto = 1,
96 .restart_fw = 1,
97 /* the rest are 0 by default */
101 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
102 * @priv: eeprom and antenna fields are used to determine antenna flags
104 * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
105 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
107 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
108 * IWL_ANTENNA_MAIN - Force MAIN antenna
109 * IWL_ANTENNA_AUX - Force AUX antenna
111 __le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
113 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
115 switch (iwl3945_mod_params.antenna) {
116 case IWL_ANTENNA_DIVERSITY:
117 return 0;
119 case IWL_ANTENNA_MAIN:
120 if (eeprom->antenna_switch_type)
121 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
122 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
124 case IWL_ANTENNA_AUX:
125 if (eeprom->antenna_switch_type)
126 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
127 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
130 /* bad antenna selector value */
131 IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
132 iwl3945_mod_params.antenna);
134 return 0; /* "diversity" is default if error */
137 static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
138 struct ieee80211_key_conf *keyconf,
139 u8 sta_id)
141 unsigned long flags;
142 __le16 key_flags = 0;
143 int ret;
145 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
146 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
148 if (sta_id == priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id)
149 key_flags |= STA_KEY_MULTICAST_MSK;
151 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
152 keyconf->hw_key_idx = keyconf->keyidx;
153 key_flags &= ~STA_KEY_FLG_INVALID;
155 spin_lock_irqsave(&priv->sta_lock, flags);
156 priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
157 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
158 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
159 keyconf->keylen);
161 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
162 keyconf->keylen);
164 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
165 == STA_KEY_FLG_NO_ENC)
166 priv->stations[sta_id].sta.key.key_offset =
167 iwl_get_free_ucode_key_index(priv);
168 /* else, we are overriding an existing key => no need to allocated room
169 * in uCode. */
171 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
172 "no space for a new key");
174 priv->stations[sta_id].sta.key.key_flags = key_flags;
175 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
176 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
178 IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
180 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
182 spin_unlock_irqrestore(&priv->sta_lock, flags);
184 return ret;
187 static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
188 struct ieee80211_key_conf *keyconf,
189 u8 sta_id)
191 return -EOPNOTSUPP;
194 static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
195 struct ieee80211_key_conf *keyconf,
196 u8 sta_id)
198 return -EOPNOTSUPP;
201 static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
203 unsigned long flags;
204 struct iwl_addsta_cmd sta_cmd;
206 spin_lock_irqsave(&priv->sta_lock, flags);
207 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
208 memset(&priv->stations[sta_id].sta.key, 0,
209 sizeof(struct iwl4965_keyinfo));
210 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
211 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
212 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
213 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
214 spin_unlock_irqrestore(&priv->sta_lock, flags);
216 IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
217 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
220 static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
221 struct ieee80211_key_conf *keyconf, u8 sta_id)
223 int ret = 0;
225 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
227 switch (keyconf->cipher) {
228 case WLAN_CIPHER_SUITE_CCMP:
229 ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
230 break;
231 case WLAN_CIPHER_SUITE_TKIP:
232 ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
233 break;
234 case WLAN_CIPHER_SUITE_WEP40:
235 case WLAN_CIPHER_SUITE_WEP104:
236 ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
237 break;
238 default:
239 IWL_ERR(priv, "Unknown alg: %s alg=%x\n", __func__,
240 keyconf->cipher);
241 ret = -EINVAL;
244 IWL_DEBUG_WEP(priv, "Set dynamic key: alg=%x len=%d idx=%d sta=%d ret=%d\n",
245 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
246 sta_id, ret);
248 return ret;
251 static int iwl3945_remove_static_key(struct iwl_priv *priv)
253 int ret = -EOPNOTSUPP;
255 return ret;
258 static int iwl3945_set_static_key(struct iwl_priv *priv,
259 struct ieee80211_key_conf *key)
261 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
262 key->cipher == WLAN_CIPHER_SUITE_WEP104)
263 return -EOPNOTSUPP;
265 IWL_ERR(priv, "Static key invalid: cipher %x\n", key->cipher);
266 return -EINVAL;
269 static void iwl3945_clear_free_frames(struct iwl_priv *priv)
271 struct list_head *element;
273 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
274 priv->frames_count);
276 while (!list_empty(&priv->free_frames)) {
277 element = priv->free_frames.next;
278 list_del(element);
279 kfree(list_entry(element, struct iwl3945_frame, list));
280 priv->frames_count--;
283 if (priv->frames_count) {
284 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
285 priv->frames_count);
286 priv->frames_count = 0;
290 static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
292 struct iwl3945_frame *frame;
293 struct list_head *element;
294 if (list_empty(&priv->free_frames)) {
295 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
296 if (!frame) {
297 IWL_ERR(priv, "Could not allocate frame!\n");
298 return NULL;
301 priv->frames_count++;
302 return frame;
305 element = priv->free_frames.next;
306 list_del(element);
307 return list_entry(element, struct iwl3945_frame, list);
310 static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
312 memset(frame, 0, sizeof(*frame));
313 list_add(&frame->list, &priv->free_frames);
316 unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
317 struct ieee80211_hdr *hdr,
318 int left)
321 if (!iwl_is_associated(priv, IWL_RXON_CTX_BSS) || !priv->beacon_skb)
322 return 0;
324 if (priv->beacon_skb->len > left)
325 return 0;
327 memcpy(hdr, priv->beacon_skb->data, priv->beacon_skb->len);
329 return priv->beacon_skb->len;
332 static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
334 struct iwl3945_frame *frame;
335 unsigned int frame_size;
336 int rc;
337 u8 rate;
339 frame = iwl3945_get_free_frame(priv);
341 if (!frame) {
342 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
343 "command.\n");
344 return -ENOMEM;
347 rate = iwl_rate_get_lowest_plcp(priv,
348 &priv->contexts[IWL_RXON_CTX_BSS]);
350 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
352 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
353 &frame->u.cmd[0]);
355 iwl3945_free_frame(priv, frame);
357 return rc;
360 static void iwl3945_unset_hw_params(struct iwl_priv *priv)
362 if (priv->_3945.shared_virt)
363 dma_free_coherent(&priv->pci_dev->dev,
364 sizeof(struct iwl3945_shared),
365 priv->_3945.shared_virt,
366 priv->_3945.shared_phys);
369 static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
370 struct ieee80211_tx_info *info,
371 struct iwl_device_cmd *cmd,
372 struct sk_buff *skb_frag,
373 int sta_id)
375 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
376 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
378 tx_cmd->sec_ctl = 0;
380 switch (keyinfo->cipher) {
381 case WLAN_CIPHER_SUITE_CCMP:
382 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
383 memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
384 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
385 break;
387 case WLAN_CIPHER_SUITE_TKIP:
388 break;
390 case WLAN_CIPHER_SUITE_WEP104:
391 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
392 /* fall through */
393 case WLAN_CIPHER_SUITE_WEP40:
394 tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
395 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
397 memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
399 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
400 "with key %d\n", info->control.hw_key->hw_key_idx);
401 break;
403 default:
404 IWL_ERR(priv, "Unknown encode cipher %x\n", keyinfo->cipher);
405 break;
410 * handle build REPLY_TX command notification.
412 static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
413 struct iwl_device_cmd *cmd,
414 struct ieee80211_tx_info *info,
415 struct ieee80211_hdr *hdr, u8 std_id)
417 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
418 __le32 tx_flags = tx_cmd->tx_flags;
419 __le16 fc = hdr->frame_control;
421 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
422 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
423 tx_flags |= TX_CMD_FLG_ACK_MSK;
424 if (ieee80211_is_mgmt(fc))
425 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
426 if (ieee80211_is_probe_resp(fc) &&
427 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
428 tx_flags |= TX_CMD_FLG_TSF_MSK;
429 } else {
430 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
431 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
434 tx_cmd->sta_id = std_id;
435 if (ieee80211_has_morefrags(fc))
436 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
438 if (ieee80211_is_data_qos(fc)) {
439 u8 *qc = ieee80211_get_qos_ctl(hdr);
440 tx_cmd->tid_tspec = qc[0] & 0xf;
441 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
442 } else {
443 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
446 priv->cfg->ops->utils->tx_cmd_protection(priv, info, fc, &tx_flags);
448 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
449 if (ieee80211_is_mgmt(fc)) {
450 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
451 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
452 else
453 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
454 } else {
455 tx_cmd->timeout.pm_frame_timeout = 0;
458 tx_cmd->driver_txop = 0;
459 tx_cmd->tx_flags = tx_flags;
460 tx_cmd->next_frame_len = 0;
464 * start REPLY_TX command process
466 static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
468 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
469 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
470 struct iwl3945_tx_cmd *tx_cmd;
471 struct iwl_tx_queue *txq = NULL;
472 struct iwl_queue *q = NULL;
473 struct iwl_device_cmd *out_cmd;
474 struct iwl_cmd_meta *out_meta;
475 dma_addr_t phys_addr;
476 dma_addr_t txcmd_phys;
477 int txq_id = skb_get_queue_mapping(skb);
478 u16 len, idx, hdr_len;
479 u8 id;
480 u8 unicast;
481 u8 sta_id;
482 u8 tid = 0;
483 __le16 fc;
484 u8 wait_write_ptr = 0;
485 unsigned long flags;
487 spin_lock_irqsave(&priv->lock, flags);
488 if (iwl_is_rfkill(priv)) {
489 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
490 goto drop_unlock;
493 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
494 IWL_ERR(priv, "ERROR: No TX rate available.\n");
495 goto drop_unlock;
498 unicast = !is_multicast_ether_addr(hdr->addr1);
499 id = 0;
501 fc = hdr->frame_control;
503 #ifdef CONFIG_IWLWIFI_DEBUG
504 if (ieee80211_is_auth(fc))
505 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
506 else if (ieee80211_is_assoc_req(fc))
507 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
508 else if (ieee80211_is_reassoc_req(fc))
509 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
510 #endif
512 spin_unlock_irqrestore(&priv->lock, flags);
514 hdr_len = ieee80211_hdrlen(fc);
516 /* Find index into station table for destination station */
517 sta_id = iwl_sta_id_or_broadcast(
518 priv, &priv->contexts[IWL_RXON_CTX_BSS],
519 info->control.sta);
520 if (sta_id == IWL_INVALID_STATION) {
521 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
522 hdr->addr1);
523 goto drop;
526 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
528 if (ieee80211_is_data_qos(fc)) {
529 u8 *qc = ieee80211_get_qos_ctl(hdr);
530 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
531 if (unlikely(tid >= MAX_TID_COUNT))
532 goto drop;
535 /* Descriptor for chosen Tx queue */
536 txq = &priv->txq[txq_id];
537 q = &txq->q;
539 if ((iwl_queue_space(q) < q->high_mark))
540 goto drop;
542 spin_lock_irqsave(&priv->lock, flags);
544 idx = get_cmd_index(q, q->write_ptr, 0);
546 /* Set up driver data for this TFD */
547 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
548 txq->txb[q->write_ptr].skb = skb;
549 txq->txb[q->write_ptr].ctx = &priv->contexts[IWL_RXON_CTX_BSS];
551 /* Init first empty entry in queue's array of Tx/cmd buffers */
552 out_cmd = txq->cmd[idx];
553 out_meta = &txq->meta[idx];
554 tx_cmd = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
555 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
556 memset(tx_cmd, 0, sizeof(*tx_cmd));
559 * Set up the Tx-command (not MAC!) header.
560 * Store the chosen Tx queue and TFD index within the sequence field;
561 * after Tx, uCode's Tx response will return this value so driver can
562 * locate the frame within the tx queue and do post-tx processing.
564 out_cmd->hdr.cmd = REPLY_TX;
565 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
566 INDEX_TO_SEQ(q->write_ptr)));
568 /* Copy MAC header from skb into command buffer */
569 memcpy(tx_cmd->hdr, hdr, hdr_len);
572 if (info->control.hw_key)
573 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
575 /* TODO need this for burst mode later on */
576 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
578 /* set is_hcca to 0; it probably will never be implemented */
579 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
581 /* Total # bytes to be transmitted */
582 len = (u16)skb->len;
583 tx_cmd->len = cpu_to_le16(len);
585 iwl_dbg_log_tx_data_frame(priv, len, hdr);
586 iwl_update_stats(priv, true, fc, len);
587 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
588 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
590 if (!ieee80211_has_morefrags(hdr->frame_control)) {
591 txq->need_update = 1;
592 } else {
593 wait_write_ptr = 1;
594 txq->need_update = 0;
597 IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
598 le16_to_cpu(out_cmd->hdr.sequence));
599 IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
600 iwl_print_hex_dump(priv, IWL_DL_TX, tx_cmd, sizeof(*tx_cmd));
601 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr,
602 ieee80211_hdrlen(fc));
605 * Use the first empty entry in this queue's command buffer array
606 * to contain the Tx command and MAC header concatenated together
607 * (payload data will be in another buffer).
608 * Size of this varies, due to varying MAC header length.
609 * If end is not dword aligned, we'll have 2 extra bytes at the end
610 * of the MAC header (device reads on dword boundaries).
611 * We'll tell device about this padding later.
613 len = sizeof(struct iwl3945_tx_cmd) +
614 sizeof(struct iwl_cmd_header) + hdr_len;
615 len = (len + 3) & ~3;
617 /* Physical address of this Tx command's header (not MAC header!),
618 * within command buffer array. */
619 txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
620 len, PCI_DMA_TODEVICE);
621 /* we do not map meta data ... so we can safely access address to
622 * provide to unmap command*/
623 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
624 dma_unmap_len_set(out_meta, len, len);
626 /* Add buffer containing Tx command and MAC(!) header to TFD's
627 * first entry */
628 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
629 txcmd_phys, len, 1, 0);
632 /* Set up TFD's 2nd entry to point directly to remainder of skb,
633 * if any (802.11 null frames have no payload). */
634 len = skb->len - hdr_len;
635 if (len) {
636 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
637 len, PCI_DMA_TODEVICE);
638 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
639 phys_addr, len,
640 0, U32_PAD(len));
644 /* Tell device the write index *just past* this latest filled TFD */
645 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
646 iwl_txq_update_write_ptr(priv, txq);
647 spin_unlock_irqrestore(&priv->lock, flags);
649 if ((iwl_queue_space(q) < q->high_mark)
650 && priv->mac80211_registered) {
651 if (wait_write_ptr) {
652 spin_lock_irqsave(&priv->lock, flags);
653 txq->need_update = 1;
654 iwl_txq_update_write_ptr(priv, txq);
655 spin_unlock_irqrestore(&priv->lock, flags);
658 iwl_stop_queue(priv, txq);
661 return 0;
663 drop_unlock:
664 spin_unlock_irqrestore(&priv->lock, flags);
665 drop:
666 return -1;
669 static int iwl3945_get_measurement(struct iwl_priv *priv,
670 struct ieee80211_measurement_params *params,
671 u8 type)
673 struct iwl_spectrum_cmd spectrum;
674 struct iwl_rx_packet *pkt;
675 struct iwl_host_cmd cmd = {
676 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
677 .data = (void *)&spectrum,
678 .flags = CMD_WANT_SKB,
680 u32 add_time = le64_to_cpu(params->start_time);
681 int rc;
682 int spectrum_resp_status;
683 int duration = le16_to_cpu(params->duration);
684 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
686 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
687 add_time = iwl_usecs_to_beacons(priv,
688 le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
689 le16_to_cpu(ctx->timing.beacon_interval));
691 memset(&spectrum, 0, sizeof(spectrum));
693 spectrum.channel_count = cpu_to_le16(1);
694 spectrum.flags =
695 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
696 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
697 cmd.len = sizeof(spectrum);
698 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
700 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
701 spectrum.start_time =
702 iwl_add_beacon_time(priv,
703 priv->_3945.last_beacon_time, add_time,
704 le16_to_cpu(ctx->timing.beacon_interval));
705 else
706 spectrum.start_time = 0;
708 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
709 spectrum.channels[0].channel = params->channel;
710 spectrum.channels[0].type = type;
711 if (ctx->active.flags & RXON_FLG_BAND_24G_MSK)
712 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
713 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
715 rc = iwl_send_cmd_sync(priv, &cmd);
716 if (rc)
717 return rc;
719 pkt = (struct iwl_rx_packet *)cmd.reply_page;
720 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
721 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
722 rc = -EIO;
725 spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
726 switch (spectrum_resp_status) {
727 case 0: /* Command will be handled */
728 if (pkt->u.spectrum.id != 0xff) {
729 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
730 pkt->u.spectrum.id);
731 priv->measurement_status &= ~MEASUREMENT_READY;
733 priv->measurement_status |= MEASUREMENT_ACTIVE;
734 rc = 0;
735 break;
737 case 1: /* Command will not be handled */
738 rc = -EAGAIN;
739 break;
742 iwl_free_pages(priv, cmd.reply_page);
744 return rc;
747 static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
748 struct iwl_rx_mem_buffer *rxb)
750 struct iwl_rx_packet *pkt = rxb_addr(rxb);
751 struct iwl_alive_resp *palive;
752 struct delayed_work *pwork;
754 palive = &pkt->u.alive_frame;
756 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
757 "0x%01X 0x%01X\n",
758 palive->is_valid, palive->ver_type,
759 palive->ver_subtype);
761 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
762 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
763 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
764 sizeof(struct iwl_alive_resp));
765 pwork = &priv->init_alive_start;
766 } else {
767 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
768 memcpy(&priv->card_alive, &pkt->u.alive_frame,
769 sizeof(struct iwl_alive_resp));
770 pwork = &priv->alive_start;
771 iwl3945_disable_events(priv);
774 /* We delay the ALIVE response by 5ms to
775 * give the HW RF Kill time to activate... */
776 if (palive->is_valid == UCODE_VALID_OK)
777 queue_delayed_work(priv->workqueue, pwork,
778 msecs_to_jiffies(5));
779 else
780 IWL_WARN(priv, "uCode did not respond OK.\n");
783 static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
784 struct iwl_rx_mem_buffer *rxb)
786 #ifdef CONFIG_IWLWIFI_DEBUG
787 struct iwl_rx_packet *pkt = rxb_addr(rxb);
788 #endif
790 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
793 static void iwl3945_bg_beacon_update(struct work_struct *work)
795 struct iwl_priv *priv =
796 container_of(work, struct iwl_priv, beacon_update);
797 struct sk_buff *beacon;
799 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
800 beacon = ieee80211_beacon_get(priv->hw,
801 priv->contexts[IWL_RXON_CTX_BSS].vif);
803 if (!beacon) {
804 IWL_ERR(priv, "update beacon failed\n");
805 return;
808 mutex_lock(&priv->mutex);
809 /* new beacon skb is allocated every time; dispose previous.*/
810 if (priv->beacon_skb)
811 dev_kfree_skb(priv->beacon_skb);
813 priv->beacon_skb = beacon;
814 mutex_unlock(&priv->mutex);
816 iwl3945_send_beacon_cmd(priv);
819 static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
820 struct iwl_rx_mem_buffer *rxb)
822 struct iwl_rx_packet *pkt = rxb_addr(rxb);
823 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
824 #ifdef CONFIG_IWLWIFI_DEBUG
825 u8 rate = beacon->beacon_notify_hdr.rate;
827 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
828 "tsf %d %d rate %d\n",
829 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
830 beacon->beacon_notify_hdr.failure_frame,
831 le32_to_cpu(beacon->ibss_mgr_status),
832 le32_to_cpu(beacon->high_tsf),
833 le32_to_cpu(beacon->low_tsf), rate);
834 #endif
836 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
838 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
839 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
840 queue_work(priv->workqueue, &priv->beacon_update);
843 /* Handle notification from uCode that card's power state is changing
844 * due to software, hardware, or critical temperature RFKILL */
845 static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
846 struct iwl_rx_mem_buffer *rxb)
848 struct iwl_rx_packet *pkt = rxb_addr(rxb);
849 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
850 unsigned long status = priv->status;
852 IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
853 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
854 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
856 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
857 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
859 if (flags & HW_CARD_DISABLED)
860 set_bit(STATUS_RF_KILL_HW, &priv->status);
861 else
862 clear_bit(STATUS_RF_KILL_HW, &priv->status);
865 iwl_scan_cancel(priv);
867 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
868 test_bit(STATUS_RF_KILL_HW, &priv->status)))
869 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
870 test_bit(STATUS_RF_KILL_HW, &priv->status));
871 else
872 wake_up_interruptible(&priv->wait_command_queue);
876 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
878 * Setup the RX handlers for each of the reply types sent from the uCode
879 * to the host.
881 * This function chains into the hardware specific files for them to setup
882 * any hardware specific handlers as well.
884 static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
886 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
887 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
888 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
889 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
890 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
891 iwl_rx_spectrum_measure_notif;
892 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
893 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
894 iwl_rx_pm_debug_statistics_notif;
895 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
898 * The same handler is used for both the REPLY to a discrete
899 * statistics request from the host as well as for the periodic
900 * statistics notifications (after received beacons) from the uCode.
902 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_reply_statistics;
903 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
905 iwl_setup_rx_scan_handlers(priv);
906 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
908 /* Set up hardware specific Rx handlers */
909 iwl3945_hw_rx_handler_setup(priv);
912 /************************** RX-FUNCTIONS ****************************/
914 * Rx theory of operation
916 * The host allocates 32 DMA target addresses and passes the host address
917 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
918 * 0 to 31
920 * Rx Queue Indexes
921 * The host/firmware share two index registers for managing the Rx buffers.
923 * The READ index maps to the first position that the firmware may be writing
924 * to -- the driver can read up to (but not including) this position and get
925 * good data.
926 * The READ index is managed by the firmware once the card is enabled.
928 * The WRITE index maps to the last position the driver has read from -- the
929 * position preceding WRITE is the last slot the firmware can place a packet.
931 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
932 * WRITE = READ.
934 * During initialization, the host sets up the READ queue position to the first
935 * INDEX position, and WRITE to the last (READ - 1 wrapped)
937 * When the firmware places a packet in a buffer, it will advance the READ index
938 * and fire the RX interrupt. The driver can then query the READ index and
939 * process as many packets as possible, moving the WRITE index forward as it
940 * resets the Rx queue buffers with new memory.
942 * The management in the driver is as follows:
943 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
944 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
945 * to replenish the iwl->rxq->rx_free.
946 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
947 * iwl->rxq is replenished and the READ INDEX is updated (updating the
948 * 'processed' and 'read' driver indexes as well)
949 * + A received packet is processed and handed to the kernel network stack,
950 * detached from the iwl->rxq. The driver 'processed' index is updated.
951 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
952 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
953 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
954 * were enough free buffers and RX_STALLED is set it is cleared.
957 * Driver sequence:
959 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
960 * iwl3945_rx_queue_restock
961 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
962 * queue, updates firmware pointers, and updates
963 * the WRITE index. If insufficient rx_free buffers
964 * are available, schedules iwl3945_rx_replenish
966 * -- enable interrupts --
967 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
968 * READ INDEX, detaching the SKB from the pool.
969 * Moves the packet buffer from queue to rx_used.
970 * Calls iwl3945_rx_queue_restock to refill any empty
971 * slots.
972 * ...
977 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
979 static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
980 dma_addr_t dma_addr)
982 return cpu_to_le32((u32)dma_addr);
986 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
988 * If there are slots in the RX queue that need to be restocked,
989 * and we have free pre-allocated buffers, fill the ranks as much
990 * as we can, pulling from rx_free.
992 * This moves the 'write' index forward to catch up with 'processed', and
993 * also updates the memory address in the firmware to reference the new
994 * target buffer.
996 static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
998 struct iwl_rx_queue *rxq = &priv->rxq;
999 struct list_head *element;
1000 struct iwl_rx_mem_buffer *rxb;
1001 unsigned long flags;
1002 int write;
1004 spin_lock_irqsave(&rxq->lock, flags);
1005 write = rxq->write & ~0x7;
1006 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
1007 /* Get next free Rx buffer, remove from free list */
1008 element = rxq->rx_free.next;
1009 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1010 list_del(element);
1012 /* Point to Rx buffer via next RBD in circular buffer */
1013 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
1014 rxq->queue[rxq->write] = rxb;
1015 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1016 rxq->free_count--;
1018 spin_unlock_irqrestore(&rxq->lock, flags);
1019 /* If the pre-allocated buffer pool is dropping low, schedule to
1020 * refill it */
1021 if (rxq->free_count <= RX_LOW_WATERMARK)
1022 queue_work(priv->workqueue, &priv->rx_replenish);
1025 /* If we've added more space for the firmware to place data, tell it.
1026 * Increment device's write pointer in multiples of 8. */
1027 if ((rxq->write_actual != (rxq->write & ~0x7))
1028 || (abs(rxq->write - rxq->read) > 7)) {
1029 spin_lock_irqsave(&rxq->lock, flags);
1030 rxq->need_update = 1;
1031 spin_unlock_irqrestore(&rxq->lock, flags);
1032 iwl_rx_queue_update_write_ptr(priv, rxq);
1037 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
1039 * When moving to rx_free an SKB is allocated for the slot.
1041 * Also restock the Rx queue via iwl3945_rx_queue_restock.
1042 * This is called as a scheduled work item (except for during initialization)
1044 static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
1046 struct iwl_rx_queue *rxq = &priv->rxq;
1047 struct list_head *element;
1048 struct iwl_rx_mem_buffer *rxb;
1049 struct page *page;
1050 unsigned long flags;
1051 gfp_t gfp_mask = priority;
1053 while (1) {
1054 spin_lock_irqsave(&rxq->lock, flags);
1056 if (list_empty(&rxq->rx_used)) {
1057 spin_unlock_irqrestore(&rxq->lock, flags);
1058 return;
1060 spin_unlock_irqrestore(&rxq->lock, flags);
1062 if (rxq->free_count > RX_LOW_WATERMARK)
1063 gfp_mask |= __GFP_NOWARN;
1065 if (priv->hw_params.rx_page_order > 0)
1066 gfp_mask |= __GFP_COMP;
1068 /* Alloc a new receive buffer */
1069 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
1070 if (!page) {
1071 if (net_ratelimit())
1072 IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
1073 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
1074 net_ratelimit())
1075 IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
1076 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
1077 rxq->free_count);
1078 /* We don't reschedule replenish work here -- we will
1079 * call the restock method and if it still needs
1080 * more buffers it will schedule replenish */
1081 break;
1084 spin_lock_irqsave(&rxq->lock, flags);
1085 if (list_empty(&rxq->rx_used)) {
1086 spin_unlock_irqrestore(&rxq->lock, flags);
1087 __free_pages(page, priv->hw_params.rx_page_order);
1088 return;
1090 element = rxq->rx_used.next;
1091 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1092 list_del(element);
1093 spin_unlock_irqrestore(&rxq->lock, flags);
1095 rxb->page = page;
1096 /* Get physical address of RB/SKB */
1097 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
1098 PAGE_SIZE << priv->hw_params.rx_page_order,
1099 PCI_DMA_FROMDEVICE);
1101 spin_lock_irqsave(&rxq->lock, flags);
1103 list_add_tail(&rxb->list, &rxq->rx_free);
1104 rxq->free_count++;
1105 priv->alloc_rxb_page++;
1107 spin_unlock_irqrestore(&rxq->lock, flags);
1111 void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1113 unsigned long flags;
1114 int i;
1115 spin_lock_irqsave(&rxq->lock, flags);
1116 INIT_LIST_HEAD(&rxq->rx_free);
1117 INIT_LIST_HEAD(&rxq->rx_used);
1118 /* Fill the rx_used queue with _all_ of the Rx buffers */
1119 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1120 /* In the reset function, these buffers may have been allocated
1121 * to an SKB, so we need to unmap and free potential storage */
1122 if (rxq->pool[i].page != NULL) {
1123 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1124 PAGE_SIZE << priv->hw_params.rx_page_order,
1125 PCI_DMA_FROMDEVICE);
1126 __iwl_free_pages(priv, rxq->pool[i].page);
1127 rxq->pool[i].page = NULL;
1129 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1132 /* Set us so that we have processed and used all buffers, but have
1133 * not restocked the Rx queue with fresh buffers */
1134 rxq->read = rxq->write = 0;
1135 rxq->write_actual = 0;
1136 rxq->free_count = 0;
1137 spin_unlock_irqrestore(&rxq->lock, flags);
1140 void iwl3945_rx_replenish(void *data)
1142 struct iwl_priv *priv = data;
1143 unsigned long flags;
1145 iwl3945_rx_allocate(priv, GFP_KERNEL);
1147 spin_lock_irqsave(&priv->lock, flags);
1148 iwl3945_rx_queue_restock(priv);
1149 spin_unlock_irqrestore(&priv->lock, flags);
1152 static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1154 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1156 iwl3945_rx_queue_restock(priv);
1160 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1161 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1162 * This free routine walks the list of POOL entries and if SKB is set to
1163 * non NULL it is unmapped and freed
1165 static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1167 int i;
1168 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
1169 if (rxq->pool[i].page != NULL) {
1170 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1171 PAGE_SIZE << priv->hw_params.rx_page_order,
1172 PCI_DMA_FROMDEVICE);
1173 __iwl_free_pages(priv, rxq->pool[i].page);
1174 rxq->pool[i].page = NULL;
1178 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
1179 rxq->bd_dma);
1180 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
1181 rxq->rb_stts, rxq->rb_stts_dma);
1182 rxq->bd = NULL;
1183 rxq->rb_stts = NULL;
1187 /* Convert linear signal-to-noise ratio into dB */
1188 static u8 ratio2dB[100] = {
1189 /* 0 1 2 3 4 5 6 7 8 9 */
1190 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1191 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1192 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1193 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1194 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1195 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1196 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1197 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1198 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1199 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1202 /* Calculates a relative dB value from a ratio of linear
1203 * (i.e. not dB) signal levels.
1204 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
1205 int iwl3945_calc_db_from_ratio(int sig_ratio)
1207 /* 1000:1 or higher just report as 60 dB */
1208 if (sig_ratio >= 1000)
1209 return 60;
1211 /* 100:1 or higher, divide by 10 and use table,
1212 * add 20 dB to make up for divide by 10 */
1213 if (sig_ratio >= 100)
1214 return 20 + (int)ratio2dB[sig_ratio/10];
1216 /* We shouldn't see this */
1217 if (sig_ratio < 1)
1218 return 0;
1220 /* Use table for ratios 1:1 - 99:1 */
1221 return (int)ratio2dB[sig_ratio];
1225 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
1227 * Uses the priv->rx_handlers callback function array to invoke
1228 * the appropriate handlers, including command responses,
1229 * frame-received notifications, and other notifications.
1231 static void iwl3945_rx_handle(struct iwl_priv *priv)
1233 struct iwl_rx_mem_buffer *rxb;
1234 struct iwl_rx_packet *pkt;
1235 struct iwl_rx_queue *rxq = &priv->rxq;
1236 u32 r, i;
1237 int reclaim;
1238 unsigned long flags;
1239 u8 fill_rx = 0;
1240 u32 count = 8;
1241 int total_empty = 0;
1243 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1244 * buffer that the driver may process (last buffer filled by ucode). */
1245 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
1246 i = rxq->read;
1248 /* calculate total frames need to be restock after handling RX */
1249 total_empty = r - rxq->write_actual;
1250 if (total_empty < 0)
1251 total_empty += RX_QUEUE_SIZE;
1253 if (total_empty > (RX_QUEUE_SIZE / 2))
1254 fill_rx = 1;
1255 /* Rx interrupt, but nothing sent from uCode */
1256 if (i == r)
1257 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
1259 while (i != r) {
1260 int len;
1262 rxb = rxq->queue[i];
1264 /* If an RXB doesn't have a Rx queue slot associated with it,
1265 * then a bug has been introduced in the queue refilling
1266 * routines -- catch it here */
1267 BUG_ON(rxb == NULL);
1269 rxq->queue[i] = NULL;
1271 pci_unmap_page(priv->pci_dev, rxb->page_dma,
1272 PAGE_SIZE << priv->hw_params.rx_page_order,
1273 PCI_DMA_FROMDEVICE);
1274 pkt = rxb_addr(rxb);
1276 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1277 len += sizeof(u32); /* account for status word */
1278 trace_iwlwifi_dev_rx(priv, pkt, len);
1280 /* Reclaim a command buffer only if this packet is a response
1281 * to a (driver-originated) command.
1282 * If the packet (e.g. Rx frame) originated from uCode,
1283 * there is no command buffer to reclaim.
1284 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1285 * but apparently a few don't get set; catch them here. */
1286 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1287 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1288 (pkt->hdr.cmd != REPLY_TX);
1290 /* Based on type of command response or notification,
1291 * handle those that need handling via function in
1292 * rx_handlers table. See iwl3945_setup_rx_handlers() */
1293 if (priv->rx_handlers[pkt->hdr.cmd]) {
1294 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
1295 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1296 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
1297 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1298 } else {
1299 /* No handling needed */
1300 IWL_DEBUG_RX(priv,
1301 "r %d i %d No handler needed for %s, 0x%02x\n",
1302 r, i, get_cmd_string(pkt->hdr.cmd),
1303 pkt->hdr.cmd);
1307 * XXX: After here, we should always check rxb->page
1308 * against NULL before touching it or its virtual
1309 * memory (pkt). Because some rx_handler might have
1310 * already taken or freed the pages.
1313 if (reclaim) {
1314 /* Invoke any callbacks, transfer the buffer to caller,
1315 * and fire off the (possibly) blocking iwl_send_cmd()
1316 * as we reclaim the driver command queue */
1317 if (rxb->page)
1318 iwl_tx_cmd_complete(priv, rxb);
1319 else
1320 IWL_WARN(priv, "Claim null rxb?\n");
1323 /* Reuse the page if possible. For notification packets and
1324 * SKBs that fail to Rx correctly, add them back into the
1325 * rx_free list for reuse later. */
1326 spin_lock_irqsave(&rxq->lock, flags);
1327 if (rxb->page != NULL) {
1328 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1329 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1330 PCI_DMA_FROMDEVICE);
1331 list_add_tail(&rxb->list, &rxq->rx_free);
1332 rxq->free_count++;
1333 } else
1334 list_add_tail(&rxb->list, &rxq->rx_used);
1336 spin_unlock_irqrestore(&rxq->lock, flags);
1338 i = (i + 1) & RX_QUEUE_MASK;
1339 /* If there are a lot of unused frames,
1340 * restock the Rx queue so ucode won't assert. */
1341 if (fill_rx) {
1342 count++;
1343 if (count >= 8) {
1344 rxq->read = i;
1345 iwl3945_rx_replenish_now(priv);
1346 count = 0;
1351 /* Backtrack one entry */
1352 rxq->read = i;
1353 if (fill_rx)
1354 iwl3945_rx_replenish_now(priv);
1355 else
1356 iwl3945_rx_queue_restock(priv);
1359 /* call this function to flush any scheduled tasklet */
1360 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1362 /* wait to make sure we flush pending tasklet*/
1363 synchronize_irq(priv->pci_dev->irq);
1364 tasklet_kill(&priv->irq_tasklet);
1367 static const char *desc_lookup(int i)
1369 switch (i) {
1370 case 1:
1371 return "FAIL";
1372 case 2:
1373 return "BAD_PARAM";
1374 case 3:
1375 return "BAD_CHECKSUM";
1376 case 4:
1377 return "NMI_INTERRUPT";
1378 case 5:
1379 return "SYSASSERT";
1380 case 6:
1381 return "FATAL_ERROR";
1384 return "UNKNOWN";
1387 #define ERROR_START_OFFSET (1 * sizeof(u32))
1388 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
1390 void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
1392 u32 i;
1393 u32 desc, time, count, base, data1;
1394 u32 blink1, blink2, ilink1, ilink2;
1396 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1398 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
1399 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
1400 return;
1404 count = iwl_read_targ_mem(priv, base);
1406 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
1407 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1408 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1409 priv->status, count);
1412 IWL_ERR(priv, "Desc Time asrtPC blink2 "
1413 "ilink1 nmiPC Line\n");
1414 for (i = ERROR_START_OFFSET;
1415 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1416 i += ERROR_ELEM_SIZE) {
1417 desc = iwl_read_targ_mem(priv, base + i);
1418 time =
1419 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
1420 blink1 =
1421 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
1422 blink2 =
1423 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
1424 ilink1 =
1425 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
1426 ilink2 =
1427 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
1428 data1 =
1429 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
1431 IWL_ERR(priv,
1432 "%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
1433 desc_lookup(desc), desc, time, blink1, blink2,
1434 ilink1, ilink2, data1);
1435 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, 0,
1436 0, blink1, blink2, ilink1, ilink2);
1440 #define EVENT_START_OFFSET (6 * sizeof(u32))
1443 * iwl3945_print_event_log - Dump error event log to syslog
1446 static int iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1447 u32 num_events, u32 mode,
1448 int pos, char **buf, size_t bufsz)
1450 u32 i;
1451 u32 base; /* SRAM byte address of event log header */
1452 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1453 u32 ptr; /* SRAM byte address of log data */
1454 u32 ev, time, data; /* event log data */
1455 unsigned long reg_flags;
1457 if (num_events == 0)
1458 return pos;
1460 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1462 if (mode == 0)
1463 event_size = 2 * sizeof(u32);
1464 else
1465 event_size = 3 * sizeof(u32);
1467 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1469 /* Make sure device is powered up for SRAM reads */
1470 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1471 iwl_grab_nic_access(priv);
1473 /* Set starting address; reads will auto-increment */
1474 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1475 rmb();
1477 /* "time" is actually "data" for mode 0 (no timestamp).
1478 * place event id # at far right for easier visual parsing. */
1479 for (i = 0; i < num_events; i++) {
1480 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1481 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1482 if (mode == 0) {
1483 /* data, ev */
1484 if (bufsz) {
1485 pos += scnprintf(*buf + pos, bufsz - pos,
1486 "0x%08x:%04u\n",
1487 time, ev);
1488 } else {
1489 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1490 trace_iwlwifi_dev_ucode_event(priv, 0,
1491 time, ev);
1493 } else {
1494 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1495 if (bufsz) {
1496 pos += scnprintf(*buf + pos, bufsz - pos,
1497 "%010u:0x%08x:%04u\n",
1498 time, data, ev);
1499 } else {
1500 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n",
1501 time, data, ev);
1502 trace_iwlwifi_dev_ucode_event(priv, time,
1503 data, ev);
1508 /* Allow device to power down */
1509 iwl_release_nic_access(priv);
1510 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
1511 return pos;
1515 * iwl3945_print_last_event_logs - Dump the newest # of event log to syslog
1517 static int iwl3945_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
1518 u32 num_wraps, u32 next_entry,
1519 u32 size, u32 mode,
1520 int pos, char **buf, size_t bufsz)
1523 * display the newest DEFAULT_LOG_ENTRIES entries
1524 * i.e the entries just before the next ont that uCode would fill.
1526 if (num_wraps) {
1527 if (next_entry < size) {
1528 pos = iwl3945_print_event_log(priv,
1529 capacity - (size - next_entry),
1530 size - next_entry, mode,
1531 pos, buf, bufsz);
1532 pos = iwl3945_print_event_log(priv, 0,
1533 next_entry, mode,
1534 pos, buf, bufsz);
1535 } else
1536 pos = iwl3945_print_event_log(priv, next_entry - size,
1537 size, mode,
1538 pos, buf, bufsz);
1539 } else {
1540 if (next_entry < size)
1541 pos = iwl3945_print_event_log(priv, 0,
1542 next_entry, mode,
1543 pos, buf, bufsz);
1544 else
1545 pos = iwl3945_print_event_log(priv, next_entry - size,
1546 size, mode,
1547 pos, buf, bufsz);
1549 return pos;
1552 #define DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES (20)
1554 int iwl3945_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1555 char **buf, bool display)
1557 u32 base; /* SRAM byte address of event log header */
1558 u32 capacity; /* event log capacity in # entries */
1559 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1560 u32 num_wraps; /* # times uCode wrapped to top of log */
1561 u32 next_entry; /* index of next entry to be written by uCode */
1562 u32 size; /* # entries that we'll print */
1563 int pos = 0;
1564 size_t bufsz = 0;
1566 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1567 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
1568 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
1569 return -EINVAL;
1572 /* event log header */
1573 capacity = iwl_read_targ_mem(priv, base);
1574 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1575 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1576 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
1578 if (capacity > priv->cfg->base_params->max_event_log_size) {
1579 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
1580 capacity, priv->cfg->base_params->max_event_log_size);
1581 capacity = priv->cfg->base_params->max_event_log_size;
1584 if (next_entry > priv->cfg->base_params->max_event_log_size) {
1585 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
1586 next_entry, priv->cfg->base_params->max_event_log_size);
1587 next_entry = priv->cfg->base_params->max_event_log_size;
1590 size = num_wraps ? capacity : next_entry;
1592 /* bail out if nothing in log */
1593 if (size == 0) {
1594 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
1595 return pos;
1598 #ifdef CONFIG_IWLWIFI_DEBUG
1599 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
1600 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1601 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1602 #else
1603 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1604 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1605 #endif
1607 IWL_ERR(priv, "Start IWL Event Log Dump: display last %d count\n",
1608 size);
1610 #ifdef CONFIG_IWLWIFI_DEBUG
1611 if (display) {
1612 if (full_log)
1613 bufsz = capacity * 48;
1614 else
1615 bufsz = size * 48;
1616 *buf = kmalloc(bufsz, GFP_KERNEL);
1617 if (!*buf)
1618 return -ENOMEM;
1620 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
1621 /* if uCode has wrapped back to top of log,
1622 * start at the oldest entry,
1623 * i.e the next one that uCode would fill.
1625 if (num_wraps)
1626 pos = iwl3945_print_event_log(priv, next_entry,
1627 capacity - next_entry, mode,
1628 pos, buf, bufsz);
1630 /* (then/else) start at top of log */
1631 pos = iwl3945_print_event_log(priv, 0, next_entry, mode,
1632 pos, buf, bufsz);
1633 } else
1634 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1635 next_entry, size, mode,
1636 pos, buf, bufsz);
1637 #else
1638 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1639 next_entry, size, mode,
1640 pos, buf, bufsz);
1641 #endif
1642 return pos;
1645 static void iwl3945_irq_tasklet(struct iwl_priv *priv)
1647 u32 inta, handled = 0;
1648 u32 inta_fh;
1649 unsigned long flags;
1650 #ifdef CONFIG_IWLWIFI_DEBUG
1651 u32 inta_mask;
1652 #endif
1654 spin_lock_irqsave(&priv->lock, flags);
1656 /* Ack/clear/reset pending uCode interrupts.
1657 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1658 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
1659 inta = iwl_read32(priv, CSR_INT);
1660 iwl_write32(priv, CSR_INT, inta);
1662 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1663 * Any new interrupts that happen after this, either while we're
1664 * in this tasklet, or later, will show up in next ISR/tasklet. */
1665 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1666 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
1668 #ifdef CONFIG_IWLWIFI_DEBUG
1669 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
1670 /* just for debug */
1671 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1672 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1673 inta, inta_mask, inta_fh);
1675 #endif
1677 spin_unlock_irqrestore(&priv->lock, flags);
1679 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1680 * atomic, make sure that inta covers all the interrupts that
1681 * we've discovered, even if FH interrupt came in just after
1682 * reading CSR_INT. */
1683 if (inta_fh & CSR39_FH_INT_RX_MASK)
1684 inta |= CSR_INT_BIT_FH_RX;
1685 if (inta_fh & CSR39_FH_INT_TX_MASK)
1686 inta |= CSR_INT_BIT_FH_TX;
1688 /* Now service all interrupt bits discovered above. */
1689 if (inta & CSR_INT_BIT_HW_ERR) {
1690 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
1692 /* Tell the device to stop sending interrupts */
1693 iwl_disable_interrupts(priv);
1695 priv->isr_stats.hw++;
1696 iwl_irq_handle_error(priv);
1698 handled |= CSR_INT_BIT_HW_ERR;
1700 return;
1703 #ifdef CONFIG_IWLWIFI_DEBUG
1704 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
1705 /* NIC fires this, but we don't use it, redundant with WAKEUP */
1706 if (inta & CSR_INT_BIT_SCD) {
1707 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
1708 "the frame/frames.\n");
1709 priv->isr_stats.sch++;
1712 /* Alive notification via Rx interrupt will do the real work */
1713 if (inta & CSR_INT_BIT_ALIVE) {
1714 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
1715 priv->isr_stats.alive++;
1718 #endif
1719 /* Safely ignore these bits for debug checks below */
1720 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1722 /* Error detected by uCode */
1723 if (inta & CSR_INT_BIT_SW_ERR) {
1724 IWL_ERR(priv, "Microcode SW error detected. "
1725 "Restarting 0x%X.\n", inta);
1726 priv->isr_stats.sw++;
1727 iwl_irq_handle_error(priv);
1728 handled |= CSR_INT_BIT_SW_ERR;
1731 /* uCode wakes up after power-down sleep */
1732 if (inta & CSR_INT_BIT_WAKEUP) {
1733 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
1734 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
1735 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1736 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1737 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1738 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1739 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1740 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
1742 priv->isr_stats.wakeup++;
1743 handled |= CSR_INT_BIT_WAKEUP;
1746 /* All uCode command responses, including Tx command responses,
1747 * Rx "responses" (frame-received notification), and other
1748 * notifications from uCode come through here*/
1749 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1750 iwl3945_rx_handle(priv);
1751 priv->isr_stats.rx++;
1752 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1755 if (inta & CSR_INT_BIT_FH_TX) {
1756 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
1757 priv->isr_stats.tx++;
1759 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
1760 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1761 (FH39_SRVC_CHNL), 0x0);
1762 handled |= CSR_INT_BIT_FH_TX;
1765 if (inta & ~handled) {
1766 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
1767 priv->isr_stats.unhandled++;
1770 if (inta & ~priv->inta_mask) {
1771 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
1772 inta & ~priv->inta_mask);
1773 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
1776 /* Re-enable all interrupts */
1777 /* only Re-enable if disabled by irq */
1778 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1779 iwl_enable_interrupts(priv);
1781 #ifdef CONFIG_IWLWIFI_DEBUG
1782 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
1783 inta = iwl_read32(priv, CSR_INT);
1784 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1785 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1786 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
1787 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1789 #endif
1792 static int iwl3945_get_single_channel_for_scan(struct iwl_priv *priv,
1793 struct ieee80211_vif *vif,
1794 enum ieee80211_band band,
1795 struct iwl3945_scan_channel *scan_ch)
1797 const struct ieee80211_supported_band *sband;
1798 u16 passive_dwell = 0;
1799 u16 active_dwell = 0;
1800 int added = 0;
1801 u8 channel = 0;
1803 sband = iwl_get_hw_mode(priv, band);
1804 if (!sband) {
1805 IWL_ERR(priv, "invalid band\n");
1806 return added;
1809 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
1810 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
1812 if (passive_dwell <= active_dwell)
1813 passive_dwell = active_dwell + 1;
1816 channel = iwl_get_single_channel_number(priv, band);
1818 if (channel) {
1819 scan_ch->channel = channel;
1820 scan_ch->type = 0; /* passive */
1821 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1822 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1823 /* Set txpower levels to defaults */
1824 scan_ch->tpc.dsp_atten = 110;
1825 if (band == IEEE80211_BAND_5GHZ)
1826 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1827 else
1828 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1829 added++;
1830 } else
1831 IWL_ERR(priv, "no valid channel found\n");
1832 return added;
1835 static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
1836 enum ieee80211_band band,
1837 u8 is_active, u8 n_probes,
1838 struct iwl3945_scan_channel *scan_ch,
1839 struct ieee80211_vif *vif)
1841 struct ieee80211_channel *chan;
1842 const struct ieee80211_supported_band *sband;
1843 const struct iwl_channel_info *ch_info;
1844 u16 passive_dwell = 0;
1845 u16 active_dwell = 0;
1846 int added, i;
1848 sband = iwl_get_hw_mode(priv, band);
1849 if (!sband)
1850 return 0;
1852 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
1853 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
1855 if (passive_dwell <= active_dwell)
1856 passive_dwell = active_dwell + 1;
1858 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1859 chan = priv->scan_request->channels[i];
1861 if (chan->band != band)
1862 continue;
1864 scan_ch->channel = chan->hw_value;
1866 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
1867 if (!is_channel_valid(ch_info)) {
1868 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
1869 scan_ch->channel);
1870 continue;
1873 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1874 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1875 /* If passive , set up for auto-switch
1876 * and use long active_dwell time.
1878 if (!is_active || is_channel_passive(ch_info) ||
1879 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
1880 scan_ch->type = 0; /* passive */
1881 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1882 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1883 } else {
1884 scan_ch->type = 1; /* active */
1887 /* Set direct probe bits. These may be used both for active
1888 * scan channels (probes gets sent right away),
1889 * or for passive channels (probes get se sent only after
1890 * hearing clear Rx packet).*/
1891 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1892 if (n_probes)
1893 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
1894 } else {
1895 /* uCode v1 does not allow setting direct probe bits on
1896 * passive channel. */
1897 if ((scan_ch->type & 1) && n_probes)
1898 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
1901 /* Set txpower levels to defaults */
1902 scan_ch->tpc.dsp_atten = 110;
1903 /* scan_pwr_info->tpc.dsp_atten; */
1905 /*scan_pwr_info->tpc.tx_gain; */
1906 if (band == IEEE80211_BAND_5GHZ)
1907 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1908 else {
1909 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1910 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
1911 * power level:
1912 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
1916 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
1917 scan_ch->channel,
1918 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1919 (scan_ch->type & 1) ?
1920 active_dwell : passive_dwell);
1922 scan_ch++;
1923 added++;
1926 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
1927 return added;
1930 static void iwl3945_init_hw_rates(struct iwl_priv *priv,
1931 struct ieee80211_rate *rates)
1933 int i;
1935 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
1936 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1937 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1938 rates[i].hw_value_short = i;
1939 rates[i].flags = 0;
1940 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
1942 * If CCK != 1M then set short preamble rate flag.
1944 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
1945 0 : IEEE80211_RATE_SHORT_PREAMBLE;
1950 /******************************************************************************
1952 * uCode download functions
1954 ******************************************************************************/
1956 static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
1958 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1959 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1960 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1961 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1962 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1963 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
1967 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
1968 * looking at all data.
1970 static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
1972 u32 val;
1973 u32 save_len = len;
1974 int rc = 0;
1975 u32 errcnt;
1977 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
1979 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
1980 IWL39_RTC_INST_LOWER_BOUND);
1982 errcnt = 0;
1983 for (; len > 0; len -= sizeof(u32), image++) {
1984 /* read data comes through single port, auto-incr addr */
1985 /* NOTE: Use the debugless read so we don't flood kernel log
1986 * if IWL_DL_IO is set */
1987 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1988 if (val != le32_to_cpu(*image)) {
1989 IWL_ERR(priv, "uCode INST section is invalid at "
1990 "offset 0x%x, is 0x%x, s/b 0x%x\n",
1991 save_len - len, val, le32_to_cpu(*image));
1992 rc = -EIO;
1993 errcnt++;
1994 if (errcnt >= 20)
1995 break;
2000 if (!errcnt)
2001 IWL_DEBUG_INFO(priv,
2002 "ucode image in INSTRUCTION memory is good\n");
2004 return rc;
2009 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
2010 * using sample data 100 bytes apart. If these sample points are good,
2011 * it's a pretty good bet that everything between them is good, too.
2013 static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
2015 u32 val;
2016 int rc = 0;
2017 u32 errcnt = 0;
2018 u32 i;
2020 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
2022 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2023 /* read data comes through single port, auto-incr addr */
2024 /* NOTE: Use the debugless read so we don't flood kernel log
2025 * if IWL_DL_IO is set */
2026 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
2027 i + IWL39_RTC_INST_LOWER_BOUND);
2028 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
2029 if (val != le32_to_cpu(*image)) {
2030 #if 0 /* Enable this if you want to see details */
2031 IWL_ERR(priv, "uCode INST section is invalid at "
2032 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2033 i, val, *image);
2034 #endif
2035 rc = -EIO;
2036 errcnt++;
2037 if (errcnt >= 3)
2038 break;
2042 return rc;
2047 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
2048 * and verify its contents
2050 static int iwl3945_verify_ucode(struct iwl_priv *priv)
2052 __le32 *image;
2053 u32 len;
2054 int rc = 0;
2056 /* Try bootstrap */
2057 image = (__le32 *)priv->ucode_boot.v_addr;
2058 len = priv->ucode_boot.len;
2059 rc = iwl3945_verify_inst_sparse(priv, image, len);
2060 if (rc == 0) {
2061 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
2062 return 0;
2065 /* Try initialize */
2066 image = (__le32 *)priv->ucode_init.v_addr;
2067 len = priv->ucode_init.len;
2068 rc = iwl3945_verify_inst_sparse(priv, image, len);
2069 if (rc == 0) {
2070 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
2071 return 0;
2074 /* Try runtime/protocol */
2075 image = (__le32 *)priv->ucode_code.v_addr;
2076 len = priv->ucode_code.len;
2077 rc = iwl3945_verify_inst_sparse(priv, image, len);
2078 if (rc == 0) {
2079 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
2080 return 0;
2083 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
2085 /* Since nothing seems to match, show first several data entries in
2086 * instruction SRAM, so maybe visual inspection will give a clue.
2087 * Selection of bootstrap image (vs. other images) is arbitrary. */
2088 image = (__le32 *)priv->ucode_boot.v_addr;
2089 len = priv->ucode_boot.len;
2090 rc = iwl3945_verify_inst_full(priv, image, len);
2092 return rc;
2095 static void iwl3945_nic_start(struct iwl_priv *priv)
2097 /* Remove all resets to allow NIC to operate */
2098 iwl_write32(priv, CSR_RESET, 0);
2101 #define IWL3945_UCODE_GET(item) \
2102 static u32 iwl3945_ucode_get_##item(const struct iwl_ucode_header *ucode)\
2104 return le32_to_cpu(ucode->u.v1.item); \
2107 static u32 iwl3945_ucode_get_header_size(u32 api_ver)
2109 return 24;
2112 static u8 *iwl3945_ucode_get_data(const struct iwl_ucode_header *ucode)
2114 return (u8 *) ucode->u.v1.data;
2117 IWL3945_UCODE_GET(inst_size);
2118 IWL3945_UCODE_GET(data_size);
2119 IWL3945_UCODE_GET(init_size);
2120 IWL3945_UCODE_GET(init_data_size);
2121 IWL3945_UCODE_GET(boot_size);
2124 * iwl3945_read_ucode - Read uCode images from disk file.
2126 * Copy into buffers for card to fetch via bus-mastering
2128 static int iwl3945_read_ucode(struct iwl_priv *priv)
2130 const struct iwl_ucode_header *ucode;
2131 int ret = -EINVAL, index;
2132 const struct firmware *ucode_raw;
2133 /* firmware file name contains uCode/driver compatibility version */
2134 const char *name_pre = priv->cfg->fw_name_pre;
2135 const unsigned int api_max = priv->cfg->ucode_api_max;
2136 const unsigned int api_min = priv->cfg->ucode_api_min;
2137 char buf[25];
2138 u8 *src;
2139 size_t len;
2140 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
2142 /* Ask kernel firmware_class module to get the boot firmware off disk.
2143 * request_firmware() is synchronous, file is in memory on return. */
2144 for (index = api_max; index >= api_min; index--) {
2145 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2146 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2147 if (ret < 0) {
2148 IWL_ERR(priv, "%s firmware file req failed: %d\n",
2149 buf, ret);
2150 if (ret == -ENOENT)
2151 continue;
2152 else
2153 goto error;
2154 } else {
2155 if (index < api_max)
2156 IWL_ERR(priv, "Loaded firmware %s, "
2157 "which is deprecated. "
2158 " Please use API v%u instead.\n",
2159 buf, api_max);
2160 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2161 "(%zd bytes) from disk\n",
2162 buf, ucode_raw->size);
2163 break;
2167 if (ret < 0)
2168 goto error;
2170 /* Make sure that we got at least our header! */
2171 if (ucode_raw->size < iwl3945_ucode_get_header_size(1)) {
2172 IWL_ERR(priv, "File size way too small!\n");
2173 ret = -EINVAL;
2174 goto err_release;
2177 /* Data from ucode file: header followed by uCode images */
2178 ucode = (struct iwl_ucode_header *)ucode_raw->data;
2180 priv->ucode_ver = le32_to_cpu(ucode->ver);
2181 api_ver = IWL_UCODE_API(priv->ucode_ver);
2182 inst_size = iwl3945_ucode_get_inst_size(ucode);
2183 data_size = iwl3945_ucode_get_data_size(ucode);
2184 init_size = iwl3945_ucode_get_init_size(ucode);
2185 init_data_size = iwl3945_ucode_get_init_data_size(ucode);
2186 boot_size = iwl3945_ucode_get_boot_size(ucode);
2187 src = iwl3945_ucode_get_data(ucode);
2189 /* api_ver should match the api version forming part of the
2190 * firmware filename ... but we don't check for that and only rely
2191 * on the API version read from firmware header from here on forward */
2193 if (api_ver < api_min || api_ver > api_max) {
2194 IWL_ERR(priv, "Driver unable to support your firmware API. "
2195 "Driver supports v%u, firmware is v%u.\n",
2196 api_max, api_ver);
2197 priv->ucode_ver = 0;
2198 ret = -EINVAL;
2199 goto err_release;
2201 if (api_ver != api_max)
2202 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
2203 "got %u. New firmware can be obtained "
2204 "from http://www.intellinuxwireless.org.\n",
2205 api_max, api_ver);
2207 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2208 IWL_UCODE_MAJOR(priv->ucode_ver),
2209 IWL_UCODE_MINOR(priv->ucode_ver),
2210 IWL_UCODE_API(priv->ucode_ver),
2211 IWL_UCODE_SERIAL(priv->ucode_ver));
2213 snprintf(priv->hw->wiphy->fw_version,
2214 sizeof(priv->hw->wiphy->fw_version),
2215 "%u.%u.%u.%u",
2216 IWL_UCODE_MAJOR(priv->ucode_ver),
2217 IWL_UCODE_MINOR(priv->ucode_ver),
2218 IWL_UCODE_API(priv->ucode_ver),
2219 IWL_UCODE_SERIAL(priv->ucode_ver));
2221 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
2222 priv->ucode_ver);
2223 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2224 inst_size);
2225 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2226 data_size);
2227 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2228 init_size);
2229 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2230 init_data_size);
2231 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2232 boot_size);
2235 /* Verify size of file vs. image size info in file's header */
2236 if (ucode_raw->size != iwl3945_ucode_get_header_size(api_ver) +
2237 inst_size + data_size + init_size +
2238 init_data_size + boot_size) {
2240 IWL_DEBUG_INFO(priv,
2241 "uCode file size %zd does not match expected size\n",
2242 ucode_raw->size);
2243 ret = -EINVAL;
2244 goto err_release;
2247 /* Verify that uCode images will fit in card's SRAM */
2248 if (inst_size > IWL39_MAX_INST_SIZE) {
2249 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
2250 inst_size);
2251 ret = -EINVAL;
2252 goto err_release;
2255 if (data_size > IWL39_MAX_DATA_SIZE) {
2256 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
2257 data_size);
2258 ret = -EINVAL;
2259 goto err_release;
2261 if (init_size > IWL39_MAX_INST_SIZE) {
2262 IWL_DEBUG_INFO(priv,
2263 "uCode init instr len %d too large to fit in\n",
2264 init_size);
2265 ret = -EINVAL;
2266 goto err_release;
2268 if (init_data_size > IWL39_MAX_DATA_SIZE) {
2269 IWL_DEBUG_INFO(priv,
2270 "uCode init data len %d too large to fit in\n",
2271 init_data_size);
2272 ret = -EINVAL;
2273 goto err_release;
2275 if (boot_size > IWL39_MAX_BSM_SIZE) {
2276 IWL_DEBUG_INFO(priv,
2277 "uCode boot instr len %d too large to fit in\n",
2278 boot_size);
2279 ret = -EINVAL;
2280 goto err_release;
2283 /* Allocate ucode buffers for card's bus-master loading ... */
2285 /* Runtime instructions and 2 copies of data:
2286 * 1) unmodified from disk
2287 * 2) backup cache for save/restore during power-downs */
2288 priv->ucode_code.len = inst_size;
2289 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
2291 priv->ucode_data.len = data_size;
2292 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
2294 priv->ucode_data_backup.len = data_size;
2295 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2297 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
2298 !priv->ucode_data_backup.v_addr)
2299 goto err_pci_alloc;
2301 /* Initialization instructions and data */
2302 if (init_size && init_data_size) {
2303 priv->ucode_init.len = init_size;
2304 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
2306 priv->ucode_init_data.len = init_data_size;
2307 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2309 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2310 goto err_pci_alloc;
2313 /* Bootstrap (instructions only, no data) */
2314 if (boot_size) {
2315 priv->ucode_boot.len = boot_size;
2316 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
2318 if (!priv->ucode_boot.v_addr)
2319 goto err_pci_alloc;
2322 /* Copy images into buffers for card's bus-master reads ... */
2324 /* Runtime instructions (first block of data in file) */
2325 len = inst_size;
2326 IWL_DEBUG_INFO(priv,
2327 "Copying (but not loading) uCode instr len %zd\n", len);
2328 memcpy(priv->ucode_code.v_addr, src, len);
2329 src += len;
2331 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2332 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2334 /* Runtime data (2nd block)
2335 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
2336 len = data_size;
2337 IWL_DEBUG_INFO(priv,
2338 "Copying (but not loading) uCode data len %zd\n", len);
2339 memcpy(priv->ucode_data.v_addr, src, len);
2340 memcpy(priv->ucode_data_backup.v_addr, src, len);
2341 src += len;
2343 /* Initialization instructions (3rd block) */
2344 if (init_size) {
2345 len = init_size;
2346 IWL_DEBUG_INFO(priv,
2347 "Copying (but not loading) init instr len %zd\n", len);
2348 memcpy(priv->ucode_init.v_addr, src, len);
2349 src += len;
2352 /* Initialization data (4th block) */
2353 if (init_data_size) {
2354 len = init_data_size;
2355 IWL_DEBUG_INFO(priv,
2356 "Copying (but not loading) init data len %zd\n", len);
2357 memcpy(priv->ucode_init_data.v_addr, src, len);
2358 src += len;
2361 /* Bootstrap instructions (5th block) */
2362 len = boot_size;
2363 IWL_DEBUG_INFO(priv,
2364 "Copying (but not loading) boot instr len %zd\n", len);
2365 memcpy(priv->ucode_boot.v_addr, src, len);
2367 /* We have our copies now, allow OS release its copies */
2368 release_firmware(ucode_raw);
2369 return 0;
2371 err_pci_alloc:
2372 IWL_ERR(priv, "failed to allocate pci memory\n");
2373 ret = -ENOMEM;
2374 iwl3945_dealloc_ucode_pci(priv);
2376 err_release:
2377 release_firmware(ucode_raw);
2379 error:
2380 return ret;
2385 * iwl3945_set_ucode_ptrs - Set uCode address location
2387 * Tell initialization uCode where to find runtime uCode.
2389 * BSM registers initially contain pointers to initialization uCode.
2390 * We need to replace them to load runtime uCode inst and data,
2391 * and to save runtime data when powering down.
2393 static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
2395 dma_addr_t pinst;
2396 dma_addr_t pdata;
2398 /* bits 31:0 for 3945 */
2399 pinst = priv->ucode_code.p_addr;
2400 pdata = priv->ucode_data_backup.p_addr;
2402 /* Tell bootstrap uCode where to find image to load */
2403 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2404 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2405 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
2406 priv->ucode_data.len);
2408 /* Inst byte count must be last to set up, bit 31 signals uCode
2409 * that all new ptr/size info is in place */
2410 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
2411 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2413 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
2415 return 0;
2419 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
2421 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2423 * Tell "initialize" uCode to go ahead and load the runtime uCode.
2425 static void iwl3945_init_alive_start(struct iwl_priv *priv)
2427 /* Check alive response for "valid" sign from uCode */
2428 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2429 /* We had an error bringing up the hardware, so take it
2430 * all the way back down so we can try again */
2431 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
2432 goto restart;
2435 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2436 * This is a paranoid check, because we would not have gotten the
2437 * "initialize" alive if code weren't properly loaded. */
2438 if (iwl3945_verify_ucode(priv)) {
2439 /* Runtime instruction load was bad;
2440 * take it all the way back down so we can try again */
2441 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
2442 goto restart;
2445 /* Send pointers to protocol/runtime uCode image ... init code will
2446 * load and launch runtime uCode, which will send us another "Alive"
2447 * notification. */
2448 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
2449 if (iwl3945_set_ucode_ptrs(priv)) {
2450 /* Runtime instruction load won't happen;
2451 * take it all the way back down so we can try again */
2452 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
2453 goto restart;
2455 return;
2457 restart:
2458 queue_work(priv->workqueue, &priv->restart);
2462 * iwl3945_alive_start - called after REPLY_ALIVE notification received
2463 * from protocol/runtime uCode (initialization uCode's
2464 * Alive gets handled by iwl3945_init_alive_start()).
2466 static void iwl3945_alive_start(struct iwl_priv *priv)
2468 int thermal_spin = 0;
2469 u32 rfkill;
2470 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
2472 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
2474 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2475 /* We had an error bringing up the hardware, so take it
2476 * all the way back down so we can try again */
2477 IWL_DEBUG_INFO(priv, "Alive failed.\n");
2478 goto restart;
2481 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2482 * This is a paranoid check, because we would not have gotten the
2483 * "runtime" alive if code weren't properly loaded. */
2484 if (iwl3945_verify_ucode(priv)) {
2485 /* Runtime instruction load was bad;
2486 * take it all the way back down so we can try again */
2487 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
2488 goto restart;
2491 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
2492 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
2494 if (rfkill & 0x1) {
2495 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2496 /* if RFKILL is not on, then wait for thermal
2497 * sensor in adapter to kick in */
2498 while (iwl3945_hw_get_temperature(priv) == 0) {
2499 thermal_spin++;
2500 udelay(10);
2503 if (thermal_spin)
2504 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
2505 thermal_spin * 10);
2506 } else
2507 set_bit(STATUS_RF_KILL_HW, &priv->status);
2509 /* After the ALIVE response, we can send commands to 3945 uCode */
2510 set_bit(STATUS_ALIVE, &priv->status);
2512 /* Enable watchdog to monitor the driver tx queues */
2513 iwl_setup_watchdog(priv);
2515 if (iwl_is_rfkill(priv))
2516 return;
2518 ieee80211_wake_queues(priv->hw);
2520 priv->active_rate = IWL_RATES_MASK;
2522 iwl_power_update_mode(priv, true);
2524 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
2525 struct iwl3945_rxon_cmd *active_rxon =
2526 (struct iwl3945_rxon_cmd *)(&ctx->active);
2528 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
2529 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2530 } else {
2531 /* Initialize our rx_config data */
2532 iwl_connection_init_rx_config(priv, ctx);
2535 /* Configure Bluetooth device coexistence support */
2536 priv->cfg->ops->hcmd->send_bt_config(priv);
2538 /* Configure the adapter for unassociated operation */
2539 iwl3945_commit_rxon(priv, ctx);
2541 iwl3945_reg_txpower_periodic(priv);
2543 iwl_leds_init(priv);
2545 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
2546 set_bit(STATUS_READY, &priv->status);
2547 wake_up_interruptible(&priv->wait_command_queue);
2549 return;
2551 restart:
2552 queue_work(priv->workqueue, &priv->restart);
2555 static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
2557 static void __iwl3945_down(struct iwl_priv *priv)
2559 unsigned long flags;
2560 int exit_pending;
2562 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
2564 iwl_scan_cancel_timeout(priv, 200);
2566 exit_pending = test_and_set_bit(STATUS_EXIT_PENDING, &priv->status);
2568 /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
2569 * to prevent rearm timer */
2570 del_timer_sync(&priv->watchdog);
2572 /* Station information will now be cleared in device */
2573 iwl_clear_ucode_stations(priv, NULL);
2574 iwl_dealloc_bcast_stations(priv);
2575 iwl_clear_driver_stations(priv);
2577 /* Unblock any waiting calls */
2578 wake_up_interruptible_all(&priv->wait_command_queue);
2580 /* Wipe out the EXIT_PENDING status bit if we are not actually
2581 * exiting the module */
2582 if (!exit_pending)
2583 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2585 /* stop and reset the on-board processor */
2586 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
2588 /* tell the device to stop sending interrupts */
2589 spin_lock_irqsave(&priv->lock, flags);
2590 iwl_disable_interrupts(priv);
2591 spin_unlock_irqrestore(&priv->lock, flags);
2592 iwl_synchronize_irq(priv);
2594 if (priv->mac80211_registered)
2595 ieee80211_stop_queues(priv->hw);
2597 /* If we have not previously called iwl3945_init() then
2598 * clear all bits but the RF Kill bits and return */
2599 if (!iwl_is_init(priv)) {
2600 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2601 STATUS_RF_KILL_HW |
2602 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2603 STATUS_GEO_CONFIGURED |
2604 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2605 STATUS_EXIT_PENDING;
2606 goto exit;
2609 /* ...otherwise clear out all the status bits but the RF Kill
2610 * bit and continue taking the NIC down. */
2611 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2612 STATUS_RF_KILL_HW |
2613 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2614 STATUS_GEO_CONFIGURED |
2615 test_bit(STATUS_FW_ERROR, &priv->status) <<
2616 STATUS_FW_ERROR |
2617 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2618 STATUS_EXIT_PENDING;
2620 iwl3945_hw_txq_ctx_stop(priv);
2621 iwl3945_hw_rxq_stop(priv);
2623 /* Power-down device's busmaster DMA clocks */
2624 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
2625 udelay(5);
2627 /* Stop the device, and put it in low power state */
2628 iwl_apm_stop(priv);
2630 exit:
2631 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
2633 if (priv->beacon_skb)
2634 dev_kfree_skb(priv->beacon_skb);
2635 priv->beacon_skb = NULL;
2637 /* clear out any free frames */
2638 iwl3945_clear_free_frames(priv);
2641 static void iwl3945_down(struct iwl_priv *priv)
2643 mutex_lock(&priv->mutex);
2644 __iwl3945_down(priv);
2645 mutex_unlock(&priv->mutex);
2647 iwl3945_cancel_deferred_work(priv);
2650 #define MAX_HW_RESTARTS 5
2652 static int iwl3945_alloc_bcast_station(struct iwl_priv *priv)
2654 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
2655 unsigned long flags;
2656 u8 sta_id;
2658 spin_lock_irqsave(&priv->sta_lock, flags);
2659 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
2660 if (sta_id == IWL_INVALID_STATION) {
2661 IWL_ERR(priv, "Unable to prepare broadcast station\n");
2662 spin_unlock_irqrestore(&priv->sta_lock, flags);
2664 return -EINVAL;
2667 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
2668 priv->stations[sta_id].used |= IWL_STA_BCAST;
2669 spin_unlock_irqrestore(&priv->sta_lock, flags);
2671 return 0;
2674 static int __iwl3945_up(struct iwl_priv *priv)
2676 int rc, i;
2678 rc = iwl3945_alloc_bcast_station(priv);
2679 if (rc)
2680 return rc;
2682 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2683 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
2684 return -EIO;
2687 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
2688 IWL_ERR(priv, "ucode not available for device bring up\n");
2689 return -EIO;
2692 /* If platform's RF_KILL switch is NOT set to KILL */
2693 if (iwl_read32(priv, CSR_GP_CNTRL) &
2694 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2695 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2696 else {
2697 set_bit(STATUS_RF_KILL_HW, &priv->status);
2698 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2699 return -ENODEV;
2702 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2704 rc = iwl3945_hw_nic_init(priv);
2705 if (rc) {
2706 IWL_ERR(priv, "Unable to int nic\n");
2707 return rc;
2710 /* make sure rfkill handshake bits are cleared */
2711 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2712 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
2713 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2715 /* clear (again), then enable host interrupts */
2716 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2717 iwl_enable_interrupts(priv);
2719 /* really make sure rfkill handshake bits are cleared */
2720 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2721 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2723 /* Copy original ucode data image from disk into backup cache.
2724 * This will be used to initialize the on-board processor's
2725 * data SRAM for a clean start when the runtime program first loads. */
2726 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
2727 priv->ucode_data.len);
2729 /* We return success when we resume from suspend and rf_kill is on. */
2730 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2731 return 0;
2733 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2735 /* load bootstrap state machine,
2736 * load bootstrap program into processor's memory,
2737 * prepare to load the "initialize" uCode */
2738 rc = priv->cfg->ops->lib->load_ucode(priv);
2740 if (rc) {
2741 IWL_ERR(priv,
2742 "Unable to set up bootstrap uCode: %d\n", rc);
2743 continue;
2746 /* start card; "initialize" will load runtime ucode */
2747 iwl3945_nic_start(priv);
2749 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
2751 return 0;
2754 set_bit(STATUS_EXIT_PENDING, &priv->status);
2755 __iwl3945_down(priv);
2756 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2758 /* tried to restart and config the device for as long as our
2759 * patience could withstand */
2760 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
2761 return -EIO;
2765 /*****************************************************************************
2767 * Workqueue callbacks
2769 *****************************************************************************/
2771 static void iwl3945_bg_init_alive_start(struct work_struct *data)
2773 struct iwl_priv *priv =
2774 container_of(data, struct iwl_priv, init_alive_start.work);
2776 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2777 return;
2779 mutex_lock(&priv->mutex);
2780 iwl3945_init_alive_start(priv);
2781 mutex_unlock(&priv->mutex);
2784 static void iwl3945_bg_alive_start(struct work_struct *data)
2786 struct iwl_priv *priv =
2787 container_of(data, struct iwl_priv, alive_start.work);
2789 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2790 return;
2792 mutex_lock(&priv->mutex);
2793 iwl3945_alive_start(priv);
2794 mutex_unlock(&priv->mutex);
2798 * 3945 cannot interrupt driver when hardware rf kill switch toggles;
2799 * driver must poll CSR_GP_CNTRL_REG register for change. This register
2800 * *is* readable even when device has been SW_RESET into low power mode
2801 * (e.g. during RF KILL).
2803 static void iwl3945_rfkill_poll(struct work_struct *data)
2805 struct iwl_priv *priv =
2806 container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
2807 bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
2808 bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
2809 & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
2811 if (new_rfkill != old_rfkill) {
2812 if (new_rfkill)
2813 set_bit(STATUS_RF_KILL_HW, &priv->status);
2814 else
2815 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2817 wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
2819 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
2820 new_rfkill ? "disable radio" : "enable radio");
2823 /* Keep this running, even if radio now enabled. This will be
2824 * cancelled in mac_start() if system decides to start again */
2825 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
2826 round_jiffies_relative(2 * HZ));
2830 int iwl3945_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
2832 struct iwl_host_cmd cmd = {
2833 .id = REPLY_SCAN_CMD,
2834 .len = sizeof(struct iwl3945_scan_cmd),
2835 .flags = CMD_SIZE_HUGE,
2837 struct iwl3945_scan_cmd *scan;
2838 u8 n_probes = 0;
2839 enum ieee80211_band band;
2840 bool is_active = false;
2841 int ret;
2843 lockdep_assert_held(&priv->mutex);
2845 if (!priv->scan_cmd) {
2846 priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
2847 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
2848 if (!priv->scan_cmd) {
2849 IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
2850 return -ENOMEM;
2853 scan = priv->scan_cmd;
2854 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
2856 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2857 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2859 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
2860 u16 interval = 0;
2861 u32 extra;
2862 u32 suspend_time = 100;
2863 u32 scan_suspend_time = 100;
2864 unsigned long flags;
2866 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
2868 spin_lock_irqsave(&priv->lock, flags);
2869 if (priv->is_internal_short_scan)
2870 interval = 0;
2871 else
2872 interval = vif->bss_conf.beacon_int;
2873 spin_unlock_irqrestore(&priv->lock, flags);
2875 scan->suspend_time = 0;
2876 scan->max_out_time = cpu_to_le32(200 * 1024);
2877 if (!interval)
2878 interval = suspend_time;
2880 * suspend time format:
2881 * 0-19: beacon interval in usec (time before exec.)
2882 * 20-23: 0
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->is_internal_short_scan) {
2896 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
2897 } else if (priv->scan_request->n_ssids) {
2898 int i, p = 0;
2899 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2900 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2901 /* always does wildcard anyway */
2902 if (!priv->scan_request->ssids[i].ssid_len)
2903 continue;
2904 scan->direct_scan[p].id = WLAN_EID_SSID;
2905 scan->direct_scan[p].len =
2906 priv->scan_request->ssids[i].ssid_len;
2907 memcpy(scan->direct_scan[p].ssid,
2908 priv->scan_request->ssids[i].ssid,
2909 priv->scan_request->ssids[i].ssid_len);
2910 n_probes++;
2911 p++;
2913 is_active = true;
2914 } else
2915 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
2917 /* We don't build a direct scan probe request; the uCode will do
2918 * that based on the direct_mask added to each channel entry */
2919 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
2920 scan->tx_cmd.sta_id = priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id;
2921 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2923 /* flags + rate selection */
2925 switch (priv->scan_band) {
2926 case IEEE80211_BAND_2GHZ:
2927 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2928 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
2929 band = IEEE80211_BAND_2GHZ;
2930 break;
2931 case IEEE80211_BAND_5GHZ:
2932 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
2933 band = IEEE80211_BAND_5GHZ;
2934 break;
2935 default:
2936 IWL_WARN(priv, "Invalid scan band\n");
2937 return -EIO;
2941 * If active scaning is requested but a certain channel
2942 * is marked passive, we can do active scanning if we
2943 * detect transmissions.
2945 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
2946 IWL_GOOD_CRC_TH_DISABLED;
2948 if (!priv->is_internal_short_scan) {
2949 scan->tx_cmd.len = cpu_to_le16(
2950 iwl_fill_probe_req(priv,
2951 (struct ieee80211_mgmt *)scan->data,
2952 vif->addr,
2953 priv->scan_request->ie,
2954 priv->scan_request->ie_len,
2955 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2956 } else {
2957 /* use bcast addr, will not be transmitted but must be valid */
2958 scan->tx_cmd.len = cpu_to_le16(
2959 iwl_fill_probe_req(priv,
2960 (struct ieee80211_mgmt *)scan->data,
2961 iwl_bcast_addr, NULL, 0,
2962 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2964 /* select Rx antennas */
2965 scan->flags |= iwl3945_get_antenna_flags(priv);
2967 if (priv->is_internal_short_scan) {
2968 scan->channel_count =
2969 iwl3945_get_single_channel_for_scan(priv, vif, band,
2970 (void *)&scan->data[le16_to_cpu(
2971 scan->tx_cmd.len)]);
2972 } else {
2973 scan->channel_count =
2974 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
2975 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)], vif);
2978 if (scan->channel_count == 0) {
2979 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
2980 return -EIO;
2983 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
2984 scan->channel_count * sizeof(struct iwl3945_scan_channel);
2985 cmd.data = scan;
2986 scan->len = cpu_to_le16(cmd.len);
2988 set_bit(STATUS_SCAN_HW, &priv->status);
2989 ret = iwl_send_cmd_sync(priv, &cmd);
2990 if (ret)
2991 clear_bit(STATUS_SCAN_HW, &priv->status);
2992 return ret;
2995 void iwl3945_post_scan(struct iwl_priv *priv)
2997 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
3000 * Since setting the RXON may have been deferred while
3001 * performing the scan, fire one off if needed
3003 if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
3004 iwl3945_commit_rxon(priv, ctx);
3007 static void iwl3945_bg_restart(struct work_struct *data)
3009 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
3011 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3012 return;
3014 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
3015 struct iwl_rxon_context *ctx;
3016 mutex_lock(&priv->mutex);
3017 for_each_context(priv, ctx)
3018 ctx->vif = NULL;
3019 priv->is_open = 0;
3020 mutex_unlock(&priv->mutex);
3021 iwl3945_down(priv);
3022 ieee80211_restart_hw(priv->hw);
3023 } else {
3024 iwl3945_down(priv);
3026 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3027 return;
3029 mutex_lock(&priv->mutex);
3030 __iwl3945_up(priv);
3031 mutex_unlock(&priv->mutex);
3035 static void iwl3945_bg_rx_replenish(struct work_struct *data)
3037 struct iwl_priv *priv =
3038 container_of(data, struct iwl_priv, rx_replenish);
3040 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3041 return;
3043 mutex_lock(&priv->mutex);
3044 iwl3945_rx_replenish(priv);
3045 mutex_unlock(&priv->mutex);
3048 void iwl3945_post_associate(struct iwl_priv *priv)
3050 int rc = 0;
3051 struct ieee80211_conf *conf = NULL;
3052 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
3054 if (!ctx->vif || !priv->is_open)
3055 return;
3057 if (ctx->vif->type == NL80211_IFTYPE_AP) {
3058 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
3059 return;
3062 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
3063 ctx->vif->bss_conf.aid, ctx->active.bssid_addr);
3065 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3066 return;
3068 iwl_scan_cancel_timeout(priv, 200);
3070 conf = ieee80211_get_hw_conf(priv->hw);
3072 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3073 iwl3945_commit_rxon(priv, ctx);
3075 rc = iwl_send_rxon_timing(priv, ctx);
3076 if (rc)
3077 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
3078 "Attempting to continue.\n");
3080 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
3082 ctx->staging.assoc_id = cpu_to_le16(ctx->vif->bss_conf.aid);
3084 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
3085 ctx->vif->bss_conf.aid, ctx->vif->bss_conf.beacon_int);
3087 if (ctx->vif->bss_conf.use_short_preamble)
3088 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3089 else
3090 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3092 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
3093 if (ctx->vif->bss_conf.use_short_slot)
3094 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
3095 else
3096 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3099 iwl3945_commit_rxon(priv, ctx);
3101 switch (ctx->vif->type) {
3102 case NL80211_IFTYPE_STATION:
3103 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
3104 break;
3105 case NL80211_IFTYPE_ADHOC:
3106 iwl3945_send_beacon_cmd(priv);
3107 break;
3108 default:
3109 IWL_ERR(priv, "%s Should not be called in %d mode\n",
3110 __func__, ctx->vif->type);
3111 break;
3115 /*****************************************************************************
3117 * mac80211 entry point functions
3119 *****************************************************************************/
3121 #define UCODE_READY_TIMEOUT (2 * HZ)
3123 static int iwl3945_mac_start(struct ieee80211_hw *hw)
3125 struct iwl_priv *priv = hw->priv;
3126 int ret;
3128 IWL_DEBUG_MAC80211(priv, "enter\n");
3130 /* we should be verifying the device is ready to be opened */
3131 mutex_lock(&priv->mutex);
3133 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3134 * ucode filename and max sizes are card-specific. */
3136 if (!priv->ucode_code.len) {
3137 ret = iwl3945_read_ucode(priv);
3138 if (ret) {
3139 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
3140 mutex_unlock(&priv->mutex);
3141 goto out_release_irq;
3145 ret = __iwl3945_up(priv);
3147 mutex_unlock(&priv->mutex);
3149 if (ret)
3150 goto out_release_irq;
3152 IWL_DEBUG_INFO(priv, "Start UP work.\n");
3154 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3155 * mac80211 will not be run successfully. */
3156 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3157 test_bit(STATUS_READY, &priv->status),
3158 UCODE_READY_TIMEOUT);
3159 if (!ret) {
3160 if (!test_bit(STATUS_READY, &priv->status)) {
3161 IWL_ERR(priv,
3162 "Wait for START_ALIVE timeout after %dms.\n",
3163 jiffies_to_msecs(UCODE_READY_TIMEOUT));
3164 ret = -ETIMEDOUT;
3165 goto out_release_irq;
3169 /* ucode is running and will send rfkill notifications,
3170 * no need to poll the killswitch state anymore */
3171 cancel_delayed_work(&priv->_3945.rfkill_poll);
3173 iwl_led_start(priv);
3175 priv->is_open = 1;
3176 IWL_DEBUG_MAC80211(priv, "leave\n");
3177 return 0;
3179 out_release_irq:
3180 priv->is_open = 0;
3181 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
3182 return ret;
3185 static void iwl3945_mac_stop(struct ieee80211_hw *hw)
3187 struct iwl_priv *priv = hw->priv;
3189 IWL_DEBUG_MAC80211(priv, "enter\n");
3191 if (!priv->is_open) {
3192 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
3193 return;
3196 priv->is_open = 0;
3198 iwl3945_down(priv);
3200 flush_workqueue(priv->workqueue);
3202 /* start polling the killswitch state again */
3203 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
3204 round_jiffies_relative(2 * HZ));
3206 IWL_DEBUG_MAC80211(priv, "leave\n");
3209 static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3211 struct iwl_priv *priv = hw->priv;
3213 IWL_DEBUG_MAC80211(priv, "enter\n");
3215 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
3216 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
3218 if (iwl3945_tx_skb(priv, skb))
3219 dev_kfree_skb_any(skb);
3221 IWL_DEBUG_MAC80211(priv, "leave\n");
3222 return NETDEV_TX_OK;
3225 void iwl3945_config_ap(struct iwl_priv *priv)
3227 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
3228 struct ieee80211_vif *vif = ctx->vif;
3229 int rc = 0;
3231 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3232 return;
3234 /* The following should be done only at AP bring up */
3235 if (!(iwl_is_associated(priv, IWL_RXON_CTX_BSS))) {
3237 /* RXON - unassoc (to set timing command) */
3238 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3239 iwl3945_commit_rxon(priv, ctx);
3241 /* RXON Timing */
3242 rc = iwl_send_rxon_timing(priv, ctx);
3243 if (rc)
3244 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
3245 "Attempting to continue.\n");
3247 ctx->staging.assoc_id = 0;
3249 if (vif->bss_conf.use_short_preamble)
3250 ctx->staging.flags |=
3251 RXON_FLG_SHORT_PREAMBLE_MSK;
3252 else
3253 ctx->staging.flags &=
3254 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3256 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
3257 if (vif->bss_conf.use_short_slot)
3258 ctx->staging.flags |=
3259 RXON_FLG_SHORT_SLOT_MSK;
3260 else
3261 ctx->staging.flags &=
3262 ~RXON_FLG_SHORT_SLOT_MSK;
3264 /* restore RXON assoc */
3265 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
3266 iwl3945_commit_rxon(priv, ctx);
3268 iwl3945_send_beacon_cmd(priv);
3270 /* FIXME - we need to add code here to detect a totally new
3271 * configuration, reset the AP, unassoc, rxon timing, assoc,
3272 * clear sta table, add BCAST sta... */
3275 static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3276 struct ieee80211_vif *vif,
3277 struct ieee80211_sta *sta,
3278 struct ieee80211_key_conf *key)
3280 struct iwl_priv *priv = hw->priv;
3281 int ret = 0;
3282 u8 sta_id = IWL_INVALID_STATION;
3283 u8 static_key;
3285 IWL_DEBUG_MAC80211(priv, "enter\n");
3287 if (iwl3945_mod_params.sw_crypto) {
3288 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
3289 return -EOPNOTSUPP;
3292 static_key = !iwl_is_associated(priv, IWL_RXON_CTX_BSS);
3294 if (!static_key) {
3295 sta_id = iwl_sta_id_or_broadcast(
3296 priv, &priv->contexts[IWL_RXON_CTX_BSS], sta);
3297 if (sta_id == IWL_INVALID_STATION)
3298 return -EINVAL;
3301 mutex_lock(&priv->mutex);
3302 iwl_scan_cancel_timeout(priv, 100);
3304 switch (cmd) {
3305 case SET_KEY:
3306 if (static_key)
3307 ret = iwl3945_set_static_key(priv, key);
3308 else
3309 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3310 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
3311 break;
3312 case DISABLE_KEY:
3313 if (static_key)
3314 ret = iwl3945_remove_static_key(priv);
3315 else
3316 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3317 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
3318 break;
3319 default:
3320 ret = -EINVAL;
3323 mutex_unlock(&priv->mutex);
3324 IWL_DEBUG_MAC80211(priv, "leave\n");
3326 return ret;
3329 static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
3330 struct ieee80211_vif *vif,
3331 struct ieee80211_sta *sta)
3333 struct iwl_priv *priv = hw->priv;
3334 struct iwl3945_sta_priv *sta_priv = (void *)sta->drv_priv;
3335 int ret;
3336 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
3337 u8 sta_id;
3339 IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
3340 sta->addr);
3341 mutex_lock(&priv->mutex);
3342 IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
3343 sta->addr);
3344 sta_priv->common.sta_id = IWL_INVALID_STATION;
3347 ret = iwl_add_station_common(priv, &priv->contexts[IWL_RXON_CTX_BSS],
3348 sta->addr, is_ap, sta, &sta_id);
3349 if (ret) {
3350 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
3351 sta->addr, ret);
3352 /* Should we return success if return code is EEXIST ? */
3353 mutex_unlock(&priv->mutex);
3354 return ret;
3357 sta_priv->common.sta_id = sta_id;
3359 /* Initialize rate scaling */
3360 IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
3361 sta->addr);
3362 iwl3945_rs_rate_init(priv, sta, sta_id);
3363 mutex_unlock(&priv->mutex);
3365 return 0;
3368 static void iwl3945_configure_filter(struct ieee80211_hw *hw,
3369 unsigned int changed_flags,
3370 unsigned int *total_flags,
3371 u64 multicast)
3373 struct iwl_priv *priv = hw->priv;
3374 __le32 filter_or = 0, filter_nand = 0;
3375 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
3377 #define CHK(test, flag) do { \
3378 if (*total_flags & (test)) \
3379 filter_or |= (flag); \
3380 else \
3381 filter_nand |= (flag); \
3382 } while (0)
3384 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
3385 changed_flags, *total_flags);
3387 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
3388 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
3389 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
3391 #undef CHK
3393 mutex_lock(&priv->mutex);
3395 ctx->staging.filter_flags &= ~filter_nand;
3396 ctx->staging.filter_flags |= filter_or;
3399 * Not committing directly because hardware can perform a scan,
3400 * but even if hw is ready, committing here breaks for some reason,
3401 * we'll eventually commit the filter flags change anyway.
3404 mutex_unlock(&priv->mutex);
3407 * Receiving all multicast frames is always enabled by the
3408 * default flags setup in iwl_connection_init_rx_config()
3409 * since we currently do not support programming multicast
3410 * filters into the device.
3412 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
3413 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
3417 /*****************************************************************************
3419 * sysfs attributes
3421 *****************************************************************************/
3423 #ifdef CONFIG_IWLWIFI_DEBUG
3426 * The following adds a new attribute to the sysfs representation
3427 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3428 * used for controlling the debug level.
3430 * See the level definitions in iwl for details.
3432 * The debug_level being managed using sysfs below is a per device debug
3433 * level that is used instead of the global debug level if it (the per
3434 * device debug level) is set.
3436 static ssize_t show_debug_level(struct device *d,
3437 struct device_attribute *attr, char *buf)
3439 struct iwl_priv *priv = dev_get_drvdata(d);
3440 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
3442 static ssize_t store_debug_level(struct device *d,
3443 struct device_attribute *attr,
3444 const char *buf, size_t count)
3446 struct iwl_priv *priv = dev_get_drvdata(d);
3447 unsigned long val;
3448 int ret;
3450 ret = strict_strtoul(buf, 0, &val);
3451 if (ret)
3452 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
3453 else {
3454 priv->debug_level = val;
3455 if (iwl_alloc_traffic_mem(priv))
3456 IWL_ERR(priv,
3457 "Not enough memory to generate traffic log\n");
3459 return strnlen(buf, count);
3462 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3463 show_debug_level, store_debug_level);
3465 #endif /* CONFIG_IWLWIFI_DEBUG */
3467 static ssize_t show_temperature(struct device *d,
3468 struct device_attribute *attr, char *buf)
3470 struct iwl_priv *priv = dev_get_drvdata(d);
3472 if (!iwl_is_alive(priv))
3473 return -EAGAIN;
3475 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
3478 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3480 static ssize_t show_tx_power(struct device *d,
3481 struct device_attribute *attr, char *buf)
3483 struct iwl_priv *priv = dev_get_drvdata(d);
3484 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
3487 static ssize_t store_tx_power(struct device *d,
3488 struct device_attribute *attr,
3489 const char *buf, size_t count)
3491 struct iwl_priv *priv = dev_get_drvdata(d);
3492 char *p = (char *)buf;
3493 u32 val;
3495 val = simple_strtoul(p, &p, 10);
3496 if (p == buf)
3497 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
3498 else
3499 iwl3945_hw_reg_set_txpower(priv, val);
3501 return count;
3504 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3506 static ssize_t show_flags(struct device *d,
3507 struct device_attribute *attr, char *buf)
3509 struct iwl_priv *priv = dev_get_drvdata(d);
3510 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
3512 return sprintf(buf, "0x%04X\n", ctx->active.flags);
3515 static ssize_t store_flags(struct device *d,
3516 struct device_attribute *attr,
3517 const char *buf, size_t count)
3519 struct iwl_priv *priv = dev_get_drvdata(d);
3520 u32 flags = simple_strtoul(buf, NULL, 0);
3521 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
3523 mutex_lock(&priv->mutex);
3524 if (le32_to_cpu(ctx->staging.flags) != flags) {
3525 /* Cancel any currently running scans... */
3526 if (iwl_scan_cancel_timeout(priv, 100))
3527 IWL_WARN(priv, "Could not cancel scan.\n");
3528 else {
3529 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
3530 flags);
3531 ctx->staging.flags = cpu_to_le32(flags);
3532 iwl3945_commit_rxon(priv, ctx);
3535 mutex_unlock(&priv->mutex);
3537 return count;
3540 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3542 static ssize_t show_filter_flags(struct device *d,
3543 struct device_attribute *attr, char *buf)
3545 struct iwl_priv *priv = dev_get_drvdata(d);
3546 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
3548 return sprintf(buf, "0x%04X\n",
3549 le32_to_cpu(ctx->active.filter_flags));
3552 static ssize_t store_filter_flags(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 iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
3558 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3560 mutex_lock(&priv->mutex);
3561 if (le32_to_cpu(ctx->staging.filter_flags) != filter_flags) {
3562 /* Cancel any currently running scans... */
3563 if (iwl_scan_cancel_timeout(priv, 100))
3564 IWL_WARN(priv, "Could not cancel scan.\n");
3565 else {
3566 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
3567 "0x%04X\n", filter_flags);
3568 ctx->staging.filter_flags =
3569 cpu_to_le32(filter_flags);
3570 iwl3945_commit_rxon(priv, ctx);
3573 mutex_unlock(&priv->mutex);
3575 return count;
3578 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3579 store_filter_flags);
3581 static ssize_t show_measurement(struct device *d,
3582 struct device_attribute *attr, char *buf)
3584 struct iwl_priv *priv = dev_get_drvdata(d);
3585 struct iwl_spectrum_notification measure_report;
3586 u32 size = sizeof(measure_report), len = 0, ofs = 0;
3587 u8 *data = (u8 *)&measure_report;
3588 unsigned long flags;
3590 spin_lock_irqsave(&priv->lock, flags);
3591 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3592 spin_unlock_irqrestore(&priv->lock, flags);
3593 return 0;
3595 memcpy(&measure_report, &priv->measure_report, size);
3596 priv->measurement_status = 0;
3597 spin_unlock_irqrestore(&priv->lock, flags);
3599 while (size && (PAGE_SIZE - len)) {
3600 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3601 PAGE_SIZE - len, 1);
3602 len = strlen(buf);
3603 if (PAGE_SIZE - len)
3604 buf[len++] = '\n';
3606 ofs += 16;
3607 size -= min(size, 16U);
3610 return len;
3613 static ssize_t store_measurement(struct device *d,
3614 struct device_attribute *attr,
3615 const char *buf, size_t count)
3617 struct iwl_priv *priv = dev_get_drvdata(d);
3618 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
3619 struct ieee80211_measurement_params params = {
3620 .channel = le16_to_cpu(ctx->active.channel),
3621 .start_time = cpu_to_le64(priv->_3945.last_tsf),
3622 .duration = cpu_to_le16(1),
3624 u8 type = IWL_MEASURE_BASIC;
3625 u8 buffer[32];
3626 u8 channel;
3628 if (count) {
3629 char *p = buffer;
3630 strncpy(buffer, buf, min(sizeof(buffer), count));
3631 channel = simple_strtoul(p, NULL, 0);
3632 if (channel)
3633 params.channel = channel;
3635 p = buffer;
3636 while (*p && *p != ' ')
3637 p++;
3638 if (*p)
3639 type = simple_strtoul(p + 1, NULL, 0);
3642 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
3643 "channel %d (for '%s')\n", type, params.channel, buf);
3644 iwl3945_get_measurement(priv, &params, type);
3646 return count;
3649 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3650 show_measurement, store_measurement);
3652 static ssize_t store_retry_rate(struct device *d,
3653 struct device_attribute *attr,
3654 const char *buf, size_t count)
3656 struct iwl_priv *priv = dev_get_drvdata(d);
3658 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3659 if (priv->retry_rate <= 0)
3660 priv->retry_rate = 1;
3662 return count;
3665 static ssize_t show_retry_rate(struct device *d,
3666 struct device_attribute *attr, char *buf)
3668 struct iwl_priv *priv = dev_get_drvdata(d);
3669 return sprintf(buf, "%d", priv->retry_rate);
3672 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3673 store_retry_rate);
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 */
3680 return 0;
3683 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3685 static ssize_t show_antenna(struct device *d,
3686 struct device_attribute *attr, char *buf)
3688 struct iwl_priv *priv = dev_get_drvdata(d);
3690 if (!iwl_is_alive(priv))
3691 return -EAGAIN;
3693 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
3696 static ssize_t store_antenna(struct device *d,
3697 struct device_attribute *attr,
3698 const char *buf, size_t count)
3700 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
3701 int ant;
3703 if (count == 0)
3704 return 0;
3706 if (sscanf(buf, "%1i", &ant) != 1) {
3707 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
3708 return count;
3711 if ((ant >= 0) && (ant <= 2)) {
3712 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
3713 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
3714 } else
3715 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
3718 return count;
3721 static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3723 static ssize_t show_status(struct device *d,
3724 struct device_attribute *attr, char *buf)
3726 struct iwl_priv *priv = dev_get_drvdata(d);
3727 if (!iwl_is_alive(priv))
3728 return -EAGAIN;
3729 return sprintf(buf, "0x%08x\n", (int)priv->status);
3732 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3734 static ssize_t dump_error_log(struct device *d,
3735 struct device_attribute *attr,
3736 const char *buf, size_t count)
3738 struct iwl_priv *priv = dev_get_drvdata(d);
3739 char *p = (char *)buf;
3741 if (p[0] == '1')
3742 iwl3945_dump_nic_error_log(priv);
3744 return strnlen(buf, count);
3747 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3749 /*****************************************************************************
3751 * driver setup and tear down
3753 *****************************************************************************/
3755 static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
3757 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
3759 init_waitqueue_head(&priv->wait_command_queue);
3761 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3762 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
3763 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
3764 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3765 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
3766 INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
3768 iwl_setup_scan_deferred_work(priv);
3770 iwl3945_hw_setup_deferred_work(priv);
3772 init_timer(&priv->watchdog);
3773 priv->watchdog.data = (unsigned long)priv;
3774 priv->watchdog.function = iwl_bg_watchdog;
3776 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
3777 iwl3945_irq_tasklet, (unsigned long)priv);
3780 static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
3782 iwl3945_hw_cancel_deferred_work(priv);
3784 cancel_delayed_work_sync(&priv->init_alive_start);
3785 cancel_delayed_work(&priv->alive_start);
3786 cancel_work_sync(&priv->beacon_update);
3788 iwl_cancel_scan_deferred_work(priv);
3791 static struct attribute *iwl3945_sysfs_entries[] = {
3792 &dev_attr_antenna.attr,
3793 &dev_attr_channels.attr,
3794 &dev_attr_dump_errors.attr,
3795 &dev_attr_flags.attr,
3796 &dev_attr_filter_flags.attr,
3797 &dev_attr_measurement.attr,
3798 &dev_attr_retry_rate.attr,
3799 &dev_attr_status.attr,
3800 &dev_attr_temperature.attr,
3801 &dev_attr_tx_power.attr,
3802 #ifdef CONFIG_IWLWIFI_DEBUG
3803 &dev_attr_debug_level.attr,
3804 #endif
3805 NULL
3808 static struct attribute_group iwl3945_attribute_group = {
3809 .name = NULL, /* put in device directory */
3810 .attrs = iwl3945_sysfs_entries,
3813 struct ieee80211_ops iwl3945_hw_ops = {
3814 .tx = iwl3945_mac_tx,
3815 .start = iwl3945_mac_start,
3816 .stop = iwl3945_mac_stop,
3817 .add_interface = iwl_mac_add_interface,
3818 .remove_interface = iwl_mac_remove_interface,
3819 .change_interface = iwl_mac_change_interface,
3820 .config = iwl_legacy_mac_config,
3821 .configure_filter = iwl3945_configure_filter,
3822 .set_key = iwl3945_mac_set_key,
3823 .conf_tx = iwl_mac_conf_tx,
3824 .reset_tsf = iwl_legacy_mac_reset_tsf,
3825 .bss_info_changed = iwl_legacy_mac_bss_info_changed,
3826 .hw_scan = iwl_mac_hw_scan,
3827 .sta_add = iwl3945_mac_sta_add,
3828 .sta_remove = iwl_mac_sta_remove,
3829 .tx_last_beacon = iwl_mac_tx_last_beacon,
3832 static int iwl3945_init_drv(struct iwl_priv *priv)
3834 int ret;
3835 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
3837 priv->retry_rate = 1;
3838 priv->beacon_skb = NULL;
3840 spin_lock_init(&priv->sta_lock);
3841 spin_lock_init(&priv->hcmd_lock);
3843 INIT_LIST_HEAD(&priv->free_frames);
3845 mutex_init(&priv->mutex);
3846 mutex_init(&priv->sync_cmd_mutex);
3848 priv->ieee_channels = NULL;
3849 priv->ieee_rates = NULL;
3850 priv->band = IEEE80211_BAND_2GHZ;
3852 priv->iw_mode = NL80211_IFTYPE_STATION;
3853 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
3855 /* initialize force reset */
3856 priv->force_reset[IWL_RF_RESET].reset_duration =
3857 IWL_DELAY_NEXT_FORCE_RF_RESET;
3858 priv->force_reset[IWL_FW_RESET].reset_duration =
3859 IWL_DELAY_NEXT_FORCE_FW_RELOAD;
3862 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
3863 priv->tx_power_next = IWL_DEFAULT_TX_POWER;
3865 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3866 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3867 eeprom->version);
3868 ret = -EINVAL;
3869 goto err;
3871 ret = iwl_init_channel_map(priv);
3872 if (ret) {
3873 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3874 goto err;
3877 /* Set up txpower settings in driver for all channels */
3878 if (iwl3945_txpower_set_from_eeprom(priv)) {
3879 ret = -EIO;
3880 goto err_free_channel_map;
3883 ret = iwlcore_init_geos(priv);
3884 if (ret) {
3885 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3886 goto err_free_channel_map;
3888 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3890 return 0;
3892 err_free_channel_map:
3893 iwl_free_channel_map(priv);
3894 err:
3895 return ret;
3898 #define IWL3945_MAX_PROBE_REQUEST 200
3900 static int iwl3945_setup_mac(struct iwl_priv *priv)
3902 int ret;
3903 struct ieee80211_hw *hw = priv->hw;
3905 hw->rate_control_algorithm = "iwl-3945-rs";
3906 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
3907 hw->vif_data_size = sizeof(struct iwl_vif_priv);
3909 /* Tell mac80211 our characteristics */
3910 hw->flags = IEEE80211_HW_SIGNAL_DBM |
3911 IEEE80211_HW_SPECTRUM_MGMT;
3913 if (!priv->cfg->base_params->broken_powersave)
3914 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
3915 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
3917 hw->wiphy->interface_modes =
3918 priv->contexts[IWL_RXON_CTX_BSS].interface_modes;
3920 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
3921 WIPHY_FLAG_DISABLE_BEACON_HINTS;
3923 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3924 /* we create the 802.11 header and a zero-length SSID element */
3925 hw->wiphy->max_scan_ie_len = IWL3945_MAX_PROBE_REQUEST - 24 - 2;
3927 /* Default value; 4 EDCA QOS priorities */
3928 hw->queues = 4;
3930 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3931 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3932 &priv->bands[IEEE80211_BAND_2GHZ];
3934 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3935 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3936 &priv->bands[IEEE80211_BAND_5GHZ];
3938 ret = ieee80211_register_hw(priv->hw);
3939 if (ret) {
3940 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3941 return ret;
3943 priv->mac80211_registered = 1;
3945 return 0;
3948 static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3950 int err = 0, i;
3951 struct iwl_priv *priv;
3952 struct ieee80211_hw *hw;
3953 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
3954 struct iwl3945_eeprom *eeprom;
3955 unsigned long flags;
3957 /***********************
3958 * 1. Allocating HW data
3959 * ********************/
3961 /* mac80211 allocates memory for this device instance, including
3962 * space for this driver's private structure */
3963 hw = iwl_alloc_all(cfg);
3964 if (hw == NULL) {
3965 pr_err("Can not allocate network device\n");
3966 err = -ENOMEM;
3967 goto out;
3969 priv = hw->priv;
3970 SET_IEEE80211_DEV(hw, &pdev->dev);
3972 priv->cmd_queue = IWL39_CMD_QUEUE_NUM;
3974 /* 3945 has only one valid context */
3975 priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
3977 for (i = 0; i < NUM_IWL_RXON_CTX; i++)
3978 priv->contexts[i].ctxid = i;
3980 priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
3981 priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
3982 priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
3983 priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
3984 priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
3985 priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
3986 priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
3987 BIT(NL80211_IFTYPE_STATION) |
3988 BIT(NL80211_IFTYPE_ADHOC);
3989 priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
3990 priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
3991 priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
3994 * Disabling hardware scan means that mac80211 will perform scans
3995 * "the hard way", rather than using device's scan.
3997 if (iwl3945_mod_params.disable_hw_scan) {
3998 dev_printk(KERN_DEBUG, &(pdev->dev),
3999 "sw scan support is deprecated\n");
4000 iwl3945_hw_ops.hw_scan = NULL;
4004 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
4005 priv->cfg = cfg;
4006 priv->pci_dev = pdev;
4007 priv->inta_mask = CSR_INI_SET_MASK;
4009 if (iwl_alloc_traffic_mem(priv))
4010 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
4012 /***************************
4013 * 2. Initializing PCI bus
4014 * *************************/
4015 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4016 PCIE_LINK_STATE_CLKPM);
4018 if (pci_enable_device(pdev)) {
4019 err = -ENODEV;
4020 goto out_ieee80211_free_hw;
4023 pci_set_master(pdev);
4025 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4026 if (!err)
4027 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
4028 if (err) {
4029 IWL_WARN(priv, "No suitable DMA available.\n");
4030 goto out_pci_disable_device;
4033 pci_set_drvdata(pdev, priv);
4034 err = pci_request_regions(pdev, DRV_NAME);
4035 if (err)
4036 goto out_pci_disable_device;
4038 /***********************
4039 * 3. Read REV Register
4040 * ********************/
4041 priv->hw_base = pci_iomap(pdev, 0, 0);
4042 if (!priv->hw_base) {
4043 err = -ENODEV;
4044 goto out_pci_release_regions;
4047 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
4048 (unsigned long long) pci_resource_len(pdev, 0));
4049 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
4051 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4052 * PCI Tx retries from interfering with C3 CPU state */
4053 pci_write_config_byte(pdev, 0x41, 0x00);
4055 /* these spin locks will be used in apm_ops.init and EEPROM access
4056 * we should init now
4058 spin_lock_init(&priv->reg_lock);
4059 spin_lock_init(&priv->lock);
4062 * stop and reset the on-board processor just in case it is in a
4063 * strange state ... like being left stranded by a primary kernel
4064 * and this is now the kdump kernel trying to start up
4066 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4068 /***********************
4069 * 4. Read EEPROM
4070 * ********************/
4072 /* Read the EEPROM */
4073 err = iwl_eeprom_init(priv);
4074 if (err) {
4075 IWL_ERR(priv, "Unable to init EEPROM\n");
4076 goto out_iounmap;
4078 /* MAC Address location in EEPROM same for 3945/4965 */
4079 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
4080 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", eeprom->mac_address);
4081 SET_IEEE80211_PERM_ADDR(priv->hw, eeprom->mac_address);
4083 /***********************
4084 * 5. Setup HW Constants
4085 * ********************/
4086 /* Device-specific setup */
4087 if (iwl3945_hw_set_hw_params(priv)) {
4088 IWL_ERR(priv, "failed to set hw settings\n");
4089 goto out_eeprom_free;
4092 /***********************
4093 * 6. Setup priv
4094 * ********************/
4096 err = iwl3945_init_drv(priv);
4097 if (err) {
4098 IWL_ERR(priv, "initializing driver failed\n");
4099 goto out_unset_hw_params;
4102 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4103 priv->cfg->name);
4105 /***********************
4106 * 7. Setup Services
4107 * ********************/
4109 spin_lock_irqsave(&priv->lock, flags);
4110 iwl_disable_interrupts(priv);
4111 spin_unlock_irqrestore(&priv->lock, flags);
4113 pci_enable_msi(priv->pci_dev);
4115 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr_ops.isr,
4116 IRQF_SHARED, DRV_NAME, priv);
4117 if (err) {
4118 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4119 goto out_disable_msi;
4122 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4123 if (err) {
4124 IWL_ERR(priv, "failed to create sysfs device attributes\n");
4125 goto out_release_irq;
4128 iwl_set_rxon_channel(priv,
4129 &priv->bands[IEEE80211_BAND_2GHZ].channels[5],
4130 &priv->contexts[IWL_RXON_CTX_BSS]);
4131 iwl3945_setup_deferred_work(priv);
4132 iwl3945_setup_rx_handlers(priv);
4133 iwl_power_initialize(priv);
4135 /*********************************
4136 * 8. Setup and Register mac80211
4137 * *******************************/
4139 iwl_enable_interrupts(priv);
4141 err = iwl3945_setup_mac(priv);
4142 if (err)
4143 goto out_remove_sysfs;
4145 err = iwl_dbgfs_register(priv, DRV_NAME);
4146 if (err)
4147 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4149 /* Start monitoring the killswitch */
4150 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
4151 2 * HZ);
4153 return 0;
4155 out_remove_sysfs:
4156 destroy_workqueue(priv->workqueue);
4157 priv->workqueue = NULL;
4158 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4159 out_release_irq:
4160 free_irq(priv->pci_dev->irq, priv);
4161 out_disable_msi:
4162 pci_disable_msi(priv->pci_dev);
4163 iwlcore_free_geos(priv);
4164 iwl_free_channel_map(priv);
4165 out_unset_hw_params:
4166 iwl3945_unset_hw_params(priv);
4167 out_eeprom_free:
4168 iwl_eeprom_free(priv);
4169 out_iounmap:
4170 pci_iounmap(pdev, priv->hw_base);
4171 out_pci_release_regions:
4172 pci_release_regions(pdev);
4173 out_pci_disable_device:
4174 pci_set_drvdata(pdev, NULL);
4175 pci_disable_device(pdev);
4176 out_ieee80211_free_hw:
4177 iwl_free_traffic_mem(priv);
4178 ieee80211_free_hw(priv->hw);
4179 out:
4180 return err;
4183 static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
4185 struct iwl_priv *priv = pci_get_drvdata(pdev);
4186 unsigned long flags;
4188 if (!priv)
4189 return;
4191 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
4193 iwl_dbgfs_unregister(priv);
4195 set_bit(STATUS_EXIT_PENDING, &priv->status);
4197 if (priv->mac80211_registered) {
4198 ieee80211_unregister_hw(priv->hw);
4199 priv->mac80211_registered = 0;
4200 } else {
4201 iwl3945_down(priv);
4205 * Make sure device is reset to low power before unloading driver.
4206 * This may be redundant with iwl_down(), but there are paths to
4207 * run iwl_down() without calling apm_ops.stop(), and there are
4208 * paths to avoid running iwl_down() at all before leaving driver.
4209 * This (inexpensive) call *makes sure* device is reset.
4211 iwl_apm_stop(priv);
4213 /* make sure we flush any pending irq or
4214 * tasklet for the driver
4216 spin_lock_irqsave(&priv->lock, flags);
4217 iwl_disable_interrupts(priv);
4218 spin_unlock_irqrestore(&priv->lock, flags);
4220 iwl_synchronize_irq(priv);
4222 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4224 cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
4226 iwl3945_dealloc_ucode_pci(priv);
4228 if (priv->rxq.bd)
4229 iwl3945_rx_queue_free(priv, &priv->rxq);
4230 iwl3945_hw_txq_ctx_free(priv);
4232 iwl3945_unset_hw_params(priv);
4234 /*netif_stop_queue(dev); */
4235 flush_workqueue(priv->workqueue);
4237 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
4238 * priv->workqueue... so we can't take down the workqueue
4239 * until now... */
4240 destroy_workqueue(priv->workqueue);
4241 priv->workqueue = NULL;
4242 iwl_free_traffic_mem(priv);
4244 free_irq(pdev->irq, priv);
4245 pci_disable_msi(pdev);
4247 pci_iounmap(pdev, priv->hw_base);
4248 pci_release_regions(pdev);
4249 pci_disable_device(pdev);
4250 pci_set_drvdata(pdev, NULL);
4252 iwl_free_channel_map(priv);
4253 iwlcore_free_geos(priv);
4254 kfree(priv->scan_cmd);
4255 if (priv->beacon_skb)
4256 dev_kfree_skb(priv->beacon_skb);
4258 ieee80211_free_hw(priv->hw);
4262 /*****************************************************************************
4264 * driver and module entry point
4266 *****************************************************************************/
4268 static struct pci_driver iwl3945_driver = {
4269 .name = DRV_NAME,
4270 .id_table = iwl3945_hw_card_ids,
4271 .probe = iwl3945_pci_probe,
4272 .remove = __devexit_p(iwl3945_pci_remove),
4273 .driver.pm = IWL_PM_OPS,
4276 static int __init iwl3945_init(void)
4279 int ret;
4280 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
4281 pr_info(DRV_COPYRIGHT "\n");
4283 ret = iwl3945_rate_control_register();
4284 if (ret) {
4285 pr_err("Unable to register rate control algorithm: %d\n", ret);
4286 return ret;
4289 ret = pci_register_driver(&iwl3945_driver);
4290 if (ret) {
4291 pr_err("Unable to initialize PCI module\n");
4292 goto error_register;
4295 return ret;
4297 error_register:
4298 iwl3945_rate_control_unregister();
4299 return ret;
4302 static void __exit iwl3945_exit(void)
4304 pci_unregister_driver(&iwl3945_driver);
4305 iwl3945_rate_control_unregister();
4308 MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
4310 module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
4311 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
4312 module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
4313 MODULE_PARM_DESC(swcrypto,
4314 "using software crypto (default 1 [software])\n");
4315 #ifdef CONFIG_IWLWIFI_DEBUG
4316 module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
4317 MODULE_PARM_DESC(debug, "debug output mask");
4318 #endif
4319 module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4320 int, S_IRUGO);
4321 MODULE_PARM_DESC(disable_hw_scan,
4322 "disable hardware scanning (default 0) (deprecated)");
4323 module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
4324 MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4326 module_exit(iwl3945_exit);
4327 module_init(iwl3945_init);