iwl3945: use iwl rx handlers
[linux-2.6/libata-dev.git] / drivers / net / wireless / iwlwifi / iwl3945-base.c
blobabb9cd39bcda6f385e55d00df234bcf57f45cad4
1 /******************************************************************************
3 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
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 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/pci.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/delay.h>
36 #include <linux/skbuff.h>
37 #include <linux/netdevice.h>
38 #include <linux/wireless.h>
39 #include <linux/firmware.h>
40 #include <linux/etherdevice.h>
41 #include <linux/if_arp.h>
43 #include <net/ieee80211_radiotap.h>
44 #include <net/lib80211.h>
45 #include <net/mac80211.h>
47 #include <asm/div64.h>
49 #define DRV_NAME "iwl3945"
51 #include "iwl-fh.h"
52 #include "iwl-3945-fh.h"
53 #include "iwl-commands.h"
54 #include "iwl-sta.h"
55 #include "iwl-3945.h"
56 #include "iwl-helpers.h"
57 #include "iwl-core.h"
58 #include "iwl-dev.h"
61 * module name, copyright, version, etc.
64 #define DRV_DESCRIPTION \
65 "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
67 #ifdef CONFIG_IWLWIFI_DEBUG
68 #define VD "d"
69 #else
70 #define VD
71 #endif
73 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
74 #define VS "s"
75 #else
76 #define VS
77 #endif
79 #define IWL39_VERSION "1.2.26k" VD VS
80 #define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
81 #define DRV_AUTHOR "<ilw@linux.intel.com>"
82 #define DRV_VERSION IWL39_VERSION
85 MODULE_DESCRIPTION(DRV_DESCRIPTION);
86 MODULE_VERSION(DRV_VERSION);
87 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
88 MODULE_LICENSE("GPL");
90 /* module parameters */
91 struct iwl_mod_params iwl3945_mod_params = {
92 .num_of_queues = IWL39_MAX_NUM_QUEUES,
93 .sw_crypto = 1,
94 .restart_fw = 1,
95 /* the rest are 0 by default */
98 /*************** STATION TABLE MANAGEMENT ****
99 * mac80211 should be examined to determine if sta_info is duplicating
100 * the functionality provided here
103 /**************************************************************/
104 #if 0 /* temporary disable till we add real remove station */
106 * iwl3945_remove_station - Remove driver's knowledge of station.
108 * NOTE: This does not remove station from device's station table.
110 static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
112 int index = IWL_INVALID_STATION;
113 int i;
114 unsigned long flags;
116 spin_lock_irqsave(&priv->sta_lock, flags);
118 if (is_ap)
119 index = IWL_AP_ID;
120 else if (is_broadcast_ether_addr(addr))
121 index = priv->hw_params.bcast_sta_id;
122 else
123 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
124 if (priv->stations_39[i].used &&
125 !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
126 addr)) {
127 index = i;
128 break;
131 if (unlikely(index == IWL_INVALID_STATION))
132 goto out;
134 if (priv->stations_39[index].used) {
135 priv->stations_39[index].used = 0;
136 priv->num_stations--;
139 BUG_ON(priv->num_stations < 0);
141 out:
142 spin_unlock_irqrestore(&priv->sta_lock, flags);
143 return 0;
145 #endif
148 * iwl3945_clear_stations_table - Clear the driver's station table
150 * NOTE: This does not clear or otherwise alter the device's station table.
152 static void iwl3945_clear_stations_table(struct iwl_priv *priv)
154 unsigned long flags;
156 spin_lock_irqsave(&priv->sta_lock, flags);
158 priv->num_stations = 0;
159 memset(priv->stations_39, 0, sizeof(priv->stations_39));
161 spin_unlock_irqrestore(&priv->sta_lock, flags);
165 * iwl3945_add_station - Add station to station tables in driver and device
167 u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
169 int i;
170 int index = IWL_INVALID_STATION;
171 struct iwl3945_station_entry *station;
172 unsigned long flags_spin;
173 u8 rate;
175 spin_lock_irqsave(&priv->sta_lock, flags_spin);
176 if (is_ap)
177 index = IWL_AP_ID;
178 else if (is_broadcast_ether_addr(addr))
179 index = priv->hw_params.bcast_sta_id;
180 else
181 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
182 if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
183 addr)) {
184 index = i;
185 break;
188 if (!priv->stations_39[i].used &&
189 index == IWL_INVALID_STATION)
190 index = i;
193 /* These two conditions has the same outcome but keep them separate
194 since they have different meaning */
195 if (unlikely(index == IWL_INVALID_STATION)) {
196 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
197 return index;
200 if (priv->stations_39[index].used &&
201 !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
202 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
203 return index;
206 IWL_DEBUG_ASSOC(priv, "Add STA ID %d: %pM\n", index, addr);
207 station = &priv->stations_39[index];
208 station->used = 1;
209 priv->num_stations++;
211 /* Set up the REPLY_ADD_STA command to send to device */
212 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
213 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
214 station->sta.mode = 0;
215 station->sta.sta.sta_id = index;
216 station->sta.station_flags = 0;
218 if (priv->band == IEEE80211_BAND_5GHZ)
219 rate = IWL_RATE_6M_PLCP;
220 else
221 rate = IWL_RATE_1M_PLCP;
223 /* Turn on both antennas for the station... */
224 station->sta.rate_n_flags =
225 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
227 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
229 /* Add station to device's station table */
230 iwl_send_add_sta(priv,
231 (struct iwl_addsta_cmd *)&station->sta, flags);
232 return index;
236 static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
238 int rc = 0;
239 struct iwl_rx_packet *res = NULL;
240 struct iwl3945_rxon_assoc_cmd rxon_assoc;
241 struct iwl_host_cmd cmd = {
242 .id = REPLY_RXON_ASSOC,
243 .len = sizeof(rxon_assoc),
244 .meta.flags = CMD_WANT_SKB,
245 .data = &rxon_assoc,
247 const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
248 const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
250 if ((rxon1->flags == rxon2->flags) &&
251 (rxon1->filter_flags == rxon2->filter_flags) &&
252 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
253 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
254 IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC. Not resending.\n");
255 return 0;
258 rxon_assoc.flags = priv->staging_rxon.flags;
259 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
260 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
261 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
262 rxon_assoc.reserved = 0;
264 rc = iwl_send_cmd_sync(priv, &cmd);
265 if (rc)
266 return rc;
268 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
269 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
270 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
271 rc = -EIO;
274 priv->alloc_rxb_skb--;
275 dev_kfree_skb_any(cmd.meta.u.skb);
277 return rc;
281 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
282 * @priv: eeprom and antenna fields are used to determine antenna flags
284 * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
285 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
287 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
288 * IWL_ANTENNA_MAIN - Force MAIN antenna
289 * IWL_ANTENNA_AUX - Force AUX antenna
291 __le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
293 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
295 switch (iwl3945_mod_params.antenna) {
296 case IWL_ANTENNA_DIVERSITY:
297 return 0;
299 case IWL_ANTENNA_MAIN:
300 if (eeprom->antenna_switch_type)
301 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
302 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
304 case IWL_ANTENNA_AUX:
305 if (eeprom->antenna_switch_type)
306 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
307 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
310 /* bad antenna selector value */
311 IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
312 iwl3945_mod_params.antenna);
314 return 0; /* "diversity" is default if error */
318 * iwl3945_commit_rxon - commit staging_rxon to hardware
320 * The RXON command in staging_rxon is committed to the hardware and
321 * the active_rxon structure is updated with the new data. This
322 * function correctly transitions out of the RXON_ASSOC_MSK state if
323 * a HW tune is required based on the RXON structure changes.
325 static int iwl3945_commit_rxon(struct iwl_priv *priv)
327 /* cast away the const for active_rxon in this function */
328 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
329 struct iwl3945_rxon_cmd *staging_rxon = (void *)&priv->staging_rxon;
330 int rc = 0;
332 if (!iwl_is_alive(priv))
333 return -1;
335 /* always get timestamp with Rx frame */
336 staging_rxon->flags |= RXON_FLG_TSF2HOST_MSK;
338 /* select antenna */
339 staging_rxon->flags &=
340 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
341 staging_rxon->flags |= iwl3945_get_antenna_flags(priv);
343 rc = iwl_check_rxon_cmd(priv);
344 if (rc) {
345 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
346 return -EINVAL;
349 /* If we don't need to send a full RXON, we can use
350 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
351 * and other flags for the current radio configuration. */
352 if (!iwl_full_rxon_required(priv)) {
353 rc = iwl3945_send_rxon_assoc(priv);
354 if (rc) {
355 IWL_ERR(priv, "Error setting RXON_ASSOC "
356 "configuration (%d).\n", rc);
357 return rc;
360 memcpy(active_rxon, staging_rxon, sizeof(*active_rxon));
362 return 0;
365 /* If we are currently associated and the new config requires
366 * an RXON_ASSOC and the new config wants the associated mask enabled,
367 * we must clear the associated from the active configuration
368 * before we apply the new config */
369 if (iwl_is_associated(priv) &&
370 (staging_rxon->filter_flags & RXON_FILTER_ASSOC_MSK)) {
371 IWL_DEBUG_INFO(priv, "Toggling associated bit on current RXON\n");
372 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
375 * reserved4 and 5 could have been filled by the iwlcore code.
376 * Let's clear them before pushing to the 3945.
378 active_rxon->reserved4 = 0;
379 active_rxon->reserved5 = 0;
380 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
381 sizeof(struct iwl3945_rxon_cmd),
382 &priv->active_rxon);
384 /* If the mask clearing failed then we set
385 * active_rxon back to what it was previously */
386 if (rc) {
387 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
388 IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
389 "configuration (%d).\n", rc);
390 return rc;
394 IWL_DEBUG_INFO(priv, "Sending RXON\n"
395 "* with%s RXON_FILTER_ASSOC_MSK\n"
396 "* channel = %d\n"
397 "* bssid = %pM\n",
398 ((priv->staging_rxon.filter_flags &
399 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
400 le16_to_cpu(staging_rxon->channel),
401 staging_rxon->bssid_addr);
404 * reserved4 and 5 could have been filled by the iwlcore code.
405 * Let's clear them before pushing to the 3945.
407 staging_rxon->reserved4 = 0;
408 staging_rxon->reserved5 = 0;
410 /* Apply the new configuration */
411 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
412 sizeof(struct iwl3945_rxon_cmd),
413 staging_rxon);
414 if (rc) {
415 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
416 return rc;
419 memcpy(active_rxon, staging_rxon, sizeof(*active_rxon));
421 iwl3945_clear_stations_table(priv);
423 /* If we issue a new RXON command which required a tune then we must
424 * send a new TXPOWER command or we won't be able to Tx any frames */
425 rc = priv->cfg->ops->lib->send_tx_power(priv);
426 if (rc) {
427 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
428 return rc;
431 /* Add the broadcast address so we can send broadcast frames */
432 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
433 IWL_INVALID_STATION) {
434 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
435 return -EIO;
438 /* If we have set the ASSOC_MSK and we are in BSS mode then
439 * add the IWL_AP_ID to the station rate table */
440 if (iwl_is_associated(priv) &&
441 (priv->iw_mode == NL80211_IFTYPE_STATION))
442 if (iwl3945_add_station(priv, priv->active_rxon.bssid_addr,
443 1, 0)
444 == IWL_INVALID_STATION) {
445 IWL_ERR(priv, "Error adding AP address for transmit\n");
446 return -EIO;
449 /* Init the hardware's rate fallback order based on the band */
450 rc = iwl3945_init_hw_rate_table(priv);
451 if (rc) {
452 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
453 return -EIO;
456 return 0;
459 static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
460 struct ieee80211_key_conf *keyconf,
461 u8 sta_id)
463 unsigned long flags;
464 __le16 key_flags = 0;
466 switch (keyconf->alg) {
467 case ALG_CCMP:
468 key_flags |= STA_KEY_FLG_CCMP;
469 key_flags |= cpu_to_le16(
470 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
471 key_flags &= ~STA_KEY_FLG_INVALID;
472 break;
473 case ALG_TKIP:
474 case ALG_WEP:
475 default:
476 return -EINVAL;
478 spin_lock_irqsave(&priv->sta_lock, flags);
479 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
480 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
481 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
482 keyconf->keylen);
484 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
485 keyconf->keylen);
486 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
487 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
488 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
490 spin_unlock_irqrestore(&priv->sta_lock, flags);
492 IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
493 iwl_send_add_sta(priv,
494 (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
495 return 0;
498 static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
500 unsigned long flags;
502 spin_lock_irqsave(&priv->sta_lock, flags);
503 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
504 memset(&priv->stations_39[sta_id].sta.key, 0,
505 sizeof(struct iwl4965_keyinfo));
506 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
507 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
508 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
509 spin_unlock_irqrestore(&priv->sta_lock, flags);
511 IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
512 iwl_send_add_sta(priv,
513 (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
514 return 0;
517 static void iwl3945_clear_free_frames(struct iwl_priv *priv)
519 struct list_head *element;
521 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
522 priv->frames_count);
524 while (!list_empty(&priv->free_frames)) {
525 element = priv->free_frames.next;
526 list_del(element);
527 kfree(list_entry(element, struct iwl3945_frame, list));
528 priv->frames_count--;
531 if (priv->frames_count) {
532 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
533 priv->frames_count);
534 priv->frames_count = 0;
538 static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
540 struct iwl3945_frame *frame;
541 struct list_head *element;
542 if (list_empty(&priv->free_frames)) {
543 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
544 if (!frame) {
545 IWL_ERR(priv, "Could not allocate frame!\n");
546 return NULL;
549 priv->frames_count++;
550 return frame;
553 element = priv->free_frames.next;
554 list_del(element);
555 return list_entry(element, struct iwl3945_frame, list);
558 static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
560 memset(frame, 0, sizeof(*frame));
561 list_add(&frame->list, &priv->free_frames);
564 unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
565 struct ieee80211_hdr *hdr,
566 int left)
569 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
570 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
571 (priv->iw_mode != NL80211_IFTYPE_AP)))
572 return 0;
574 if (priv->ibss_beacon->len > left)
575 return 0;
577 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
579 return priv->ibss_beacon->len;
582 static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
584 struct iwl3945_frame *frame;
585 unsigned int frame_size;
586 int rc;
587 u8 rate;
589 frame = iwl3945_get_free_frame(priv);
591 if (!frame) {
592 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
593 "command.\n");
594 return -ENOMEM;
597 rate = iwl_rate_get_lowest_plcp(priv);
599 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
601 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
602 &frame->u.cmd[0]);
604 iwl3945_free_frame(priv, frame);
606 return rc;
609 static void iwl3945_unset_hw_params(struct iwl_priv *priv)
611 if (priv->shared_virt)
612 pci_free_consistent(priv->pci_dev,
613 sizeof(struct iwl3945_shared),
614 priv->shared_virt,
615 priv->shared_phys);
619 * QoS support
621 static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
622 struct iwl_qosparam_cmd *qos)
625 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
626 sizeof(struct iwl_qosparam_cmd), qos);
629 static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
631 unsigned long flags;
633 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
634 return;
636 spin_lock_irqsave(&priv->lock, flags);
637 priv->qos_data.def_qos_parm.qos_flags = 0;
639 if (priv->qos_data.qos_cap.q_AP.queue_request &&
640 !priv->qos_data.qos_cap.q_AP.txop_request)
641 priv->qos_data.def_qos_parm.qos_flags |=
642 QOS_PARAM_FLG_TXOP_TYPE_MSK;
644 if (priv->qos_data.qos_active)
645 priv->qos_data.def_qos_parm.qos_flags |=
646 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
648 spin_unlock_irqrestore(&priv->lock, flags);
650 if (force || iwl_is_associated(priv)) {
651 IWL_DEBUG_QOS(priv, "send QoS cmd with QoS active %d \n",
652 priv->qos_data.qos_active);
654 iwl3945_send_qos_params_command(priv,
655 &(priv->qos_data.def_qos_parm));
660 #define MAX_UCODE_BEACON_INTERVAL 1024
661 #define INTEL_CONN_LISTEN_INTERVAL cpu_to_le16(0xA)
663 static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
665 u16 new_val = 0;
666 u16 beacon_factor = 0;
668 beacon_factor =
669 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
670 / MAX_UCODE_BEACON_INTERVAL;
671 new_val = beacon_val / beacon_factor;
673 return cpu_to_le16(new_val);
676 static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
678 u64 interval_tm_unit;
679 u64 tsf, result;
680 unsigned long flags;
681 struct ieee80211_conf *conf = NULL;
682 u16 beacon_int = 0;
684 conf = ieee80211_get_hw_conf(priv->hw);
686 spin_lock_irqsave(&priv->lock, flags);
687 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
688 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
690 tsf = priv->timestamp;
692 beacon_int = priv->beacon_int;
693 spin_unlock_irqrestore(&priv->lock, flags);
695 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
696 if (beacon_int == 0) {
697 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
698 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
699 } else {
700 priv->rxon_timing.beacon_interval =
701 cpu_to_le16(beacon_int);
702 priv->rxon_timing.beacon_interval =
703 iwl3945_adjust_beacon_interval(
704 le16_to_cpu(priv->rxon_timing.beacon_interval));
707 priv->rxon_timing.atim_window = 0;
708 } else {
709 priv->rxon_timing.beacon_interval =
710 iwl3945_adjust_beacon_interval(conf->beacon_int);
711 /* TODO: we need to get atim_window from upper stack
712 * for now we set to 0 */
713 priv->rxon_timing.atim_window = 0;
716 interval_tm_unit =
717 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
718 result = do_div(tsf, interval_tm_unit);
719 priv->rxon_timing.beacon_init_val =
720 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
722 IWL_DEBUG_ASSOC(priv,
723 "beacon interval %d beacon timer %d beacon tim %d\n",
724 le16_to_cpu(priv->rxon_timing.beacon_interval),
725 le32_to_cpu(priv->rxon_timing.beacon_init_val),
726 le16_to_cpu(priv->rxon_timing.atim_window));
729 static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
731 if (mode == NL80211_IFTYPE_ADHOC) {
732 const struct iwl_channel_info *ch_info;
734 ch_info = iwl_get_channel_info(priv,
735 priv->band,
736 le16_to_cpu(priv->staging_rxon.channel));
738 if (!ch_info || !is_channel_ibss(ch_info)) {
739 IWL_ERR(priv, "channel %d not IBSS channel\n",
740 le16_to_cpu(priv->staging_rxon.channel));
741 return -EINVAL;
745 iwl_connection_init_rx_config(priv, mode);
747 iwl3945_clear_stations_table(priv);
749 /* don't commit rxon if rf-kill is on*/
750 if (!iwl_is_ready_rf(priv))
751 return -EAGAIN;
753 cancel_delayed_work(&priv->scan_check);
754 if (iwl_scan_cancel_timeout(priv, 100)) {
755 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
756 IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
757 return -EAGAIN;
760 iwl3945_commit_rxon(priv);
762 return 0;
765 static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
766 struct ieee80211_tx_info *info,
767 struct iwl_cmd *cmd,
768 struct sk_buff *skb_frag,
769 int last_frag)
771 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
772 struct iwl3945_hw_key *keyinfo =
773 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
775 switch (keyinfo->alg) {
776 case ALG_CCMP:
777 tx->sec_ctl = TX_CMD_SEC_CCM;
778 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
779 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
780 break;
782 case ALG_TKIP:
783 #if 0
784 tx->sec_ctl = TX_CMD_SEC_TKIP;
786 if (last_frag)
787 memcpy(tx->tkip_mic.byte, skb_frag->tail - 8,
789 else
790 memset(tx->tkip_mic.byte, 0, 8);
791 #endif
792 break;
794 case ALG_WEP:
795 tx->sec_ctl = TX_CMD_SEC_WEP |
796 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
798 if (keyinfo->keylen == 13)
799 tx->sec_ctl |= TX_CMD_SEC_KEY128;
801 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
803 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
804 "with key %d\n", info->control.hw_key->hw_key_idx);
805 break;
807 default:
808 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
809 break;
814 * handle build REPLY_TX command notification.
816 static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
817 struct iwl_cmd *cmd,
818 struct ieee80211_tx_info *info,
819 struct ieee80211_hdr *hdr, u8 std_id)
821 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
822 __le32 tx_flags = tx->tx_flags;
823 __le16 fc = hdr->frame_control;
824 u8 rc_flags = info->control.rates[0].flags;
826 tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
827 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
828 tx_flags |= TX_CMD_FLG_ACK_MSK;
829 if (ieee80211_is_mgmt(fc))
830 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
831 if (ieee80211_is_probe_resp(fc) &&
832 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
833 tx_flags |= TX_CMD_FLG_TSF_MSK;
834 } else {
835 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
836 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
839 tx->sta_id = std_id;
840 if (ieee80211_has_morefrags(fc))
841 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
843 if (ieee80211_is_data_qos(fc)) {
844 u8 *qc = ieee80211_get_qos_ctl(hdr);
845 tx->tid_tspec = qc[0] & 0xf;
846 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
847 } else {
848 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
851 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
852 tx_flags |= TX_CMD_FLG_RTS_MSK;
853 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
854 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
855 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
856 tx_flags |= TX_CMD_FLG_CTS_MSK;
859 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
860 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
862 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
863 if (ieee80211_is_mgmt(fc)) {
864 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
865 tx->timeout.pm_frame_timeout = cpu_to_le16(3);
866 else
867 tx->timeout.pm_frame_timeout = cpu_to_le16(2);
868 } else {
869 tx->timeout.pm_frame_timeout = 0;
870 #ifdef CONFIG_IWL3945_LEDS
871 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
872 #endif
875 tx->driver_txop = 0;
876 tx->tx_flags = tx_flags;
877 tx->next_frame_len = 0;
881 * iwl3945_get_sta_id - Find station's index within station table
883 static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
885 int sta_id;
886 u16 fc = le16_to_cpu(hdr->frame_control);
888 /* If this frame is broadcast or management, use broadcast station id */
889 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
890 is_multicast_ether_addr(hdr->addr1))
891 return priv->hw_params.bcast_sta_id;
893 switch (priv->iw_mode) {
895 /* If we are a client station in a BSS network, use the special
896 * AP station entry (that's the only station we communicate with) */
897 case NL80211_IFTYPE_STATION:
898 return IWL_AP_ID;
900 /* If we are an AP, then find the station, or use BCAST */
901 case NL80211_IFTYPE_AP:
902 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
903 if (sta_id != IWL_INVALID_STATION)
904 return sta_id;
905 return priv->hw_params.bcast_sta_id;
907 /* If this frame is going out to an IBSS network, find the station,
908 * or create a new station table entry */
909 case NL80211_IFTYPE_ADHOC: {
910 /* Create new station table entry */
911 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
912 if (sta_id != IWL_INVALID_STATION)
913 return sta_id;
915 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
917 if (sta_id != IWL_INVALID_STATION)
918 return sta_id;
920 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
921 "Defaulting to broadcast...\n",
922 hdr->addr1);
923 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
924 return priv->hw_params.bcast_sta_id;
926 /* If we are in monitor mode, use BCAST. This is required for
927 * packet injection. */
928 case NL80211_IFTYPE_MONITOR:
929 return priv->hw_params.bcast_sta_id;
931 default:
932 IWL_WARN(priv, "Unknown mode of operation: %d\n",
933 priv->iw_mode);
934 return priv->hw_params.bcast_sta_id;
939 * start REPLY_TX command process
941 static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
943 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
944 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
945 struct iwl3945_tx_cmd *tx;
946 struct iwl_tx_queue *txq = NULL;
947 struct iwl_queue *q = NULL;
948 struct iwl_cmd *out_cmd = NULL;
949 dma_addr_t phys_addr;
950 dma_addr_t txcmd_phys;
951 int txq_id = skb_get_queue_mapping(skb);
952 u16 len, idx, len_org, hdr_len;
953 u8 id;
954 u8 unicast;
955 u8 sta_id;
956 u8 tid = 0;
957 u16 seq_number = 0;
958 __le16 fc;
959 u8 wait_write_ptr = 0;
960 u8 *qc = NULL;
961 unsigned long flags;
962 int rc;
964 spin_lock_irqsave(&priv->lock, flags);
965 if (iwl_is_rfkill(priv)) {
966 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
967 goto drop_unlock;
970 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
971 IWL_ERR(priv, "ERROR: No TX rate available.\n");
972 goto drop_unlock;
975 unicast = !is_multicast_ether_addr(hdr->addr1);
976 id = 0;
978 fc = hdr->frame_control;
980 #ifdef CONFIG_IWLWIFI_DEBUG
981 if (ieee80211_is_auth(fc))
982 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
983 else if (ieee80211_is_assoc_req(fc))
984 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
985 else if (ieee80211_is_reassoc_req(fc))
986 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
987 #endif
989 /* drop all data frame if we are not associated */
990 if (ieee80211_is_data(fc) &&
991 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
992 (!iwl_is_associated(priv) ||
993 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
994 IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
995 goto drop_unlock;
998 spin_unlock_irqrestore(&priv->lock, flags);
1000 hdr_len = ieee80211_hdrlen(fc);
1002 /* Find (or create) index into station table for destination station */
1003 sta_id = iwl3945_get_sta_id(priv, hdr);
1004 if (sta_id == IWL_INVALID_STATION) {
1005 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
1006 hdr->addr1);
1007 goto drop;
1010 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
1012 if (ieee80211_is_data_qos(fc)) {
1013 qc = ieee80211_get_qos_ctl(hdr);
1014 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1015 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
1016 IEEE80211_SCTL_SEQ;
1017 hdr->seq_ctrl = cpu_to_le16(seq_number) |
1018 (hdr->seq_ctrl &
1019 cpu_to_le16(IEEE80211_SCTL_FRAG));
1020 seq_number += 0x10;
1023 /* Descriptor for chosen Tx queue */
1024 txq = &priv->txq[txq_id];
1025 q = &txq->q;
1027 spin_lock_irqsave(&priv->lock, flags);
1029 idx = get_cmd_index(q, q->write_ptr, 0);
1031 /* Set up driver data for this TFD */
1032 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
1033 txq->txb[q->write_ptr].skb[0] = skb;
1035 /* Init first empty entry in queue's array of Tx/cmd buffers */
1036 out_cmd = txq->cmd[idx];
1037 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
1038 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1039 memset(tx, 0, sizeof(*tx));
1042 * Set up the Tx-command (not MAC!) header.
1043 * Store the chosen Tx queue and TFD index within the sequence field;
1044 * after Tx, uCode's Tx response will return this value so driver can
1045 * locate the frame within the tx queue and do post-tx processing.
1047 out_cmd->hdr.cmd = REPLY_TX;
1048 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
1049 INDEX_TO_SEQ(q->write_ptr)));
1051 /* Copy MAC header from skb into command buffer */
1052 memcpy(tx->hdr, hdr, hdr_len);
1055 * Use the first empty entry in this queue's command buffer array
1056 * to contain the Tx command and MAC header concatenated together
1057 * (payload data will be in another buffer).
1058 * Size of this varies, due to varying MAC header length.
1059 * If end is not dword aligned, we'll have 2 extra bytes at the end
1060 * of the MAC header (device reads on dword boundaries).
1061 * We'll tell device about this padding later.
1063 len = sizeof(struct iwl3945_tx_cmd) +
1064 sizeof(struct iwl_cmd_header) + hdr_len;
1066 len_org = len;
1067 len = (len + 3) & ~3;
1069 if (len_org != len)
1070 len_org = 1;
1071 else
1072 len_org = 0;
1074 /* Physical address of this Tx command's header (not MAC header!),
1075 * within command buffer array. */
1076 txcmd_phys = pci_map_single(priv->pci_dev,
1077 out_cmd, sizeof(struct iwl_cmd),
1078 PCI_DMA_TODEVICE);
1079 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
1080 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
1081 /* Add buffer containing Tx command and MAC(!) header to TFD's
1082 * first entry */
1083 txcmd_phys += offsetof(struct iwl_cmd, hdr);
1085 /* Add buffer containing Tx command and MAC(!) header to TFD's
1086 * first entry */
1087 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
1088 txcmd_phys, len, 1, 0);
1090 if (info->control.hw_key)
1091 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
1093 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1094 * if any (802.11 null frames have no payload). */
1095 len = skb->len - hdr_len;
1096 if (len) {
1097 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
1098 len, PCI_DMA_TODEVICE);
1099 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
1100 phys_addr, len,
1101 0, U32_PAD(len));
1104 /* Total # bytes to be transmitted */
1105 len = (u16)skb->len;
1106 tx->len = cpu_to_le16(len);
1108 /* TODO need this for burst mode later on */
1109 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
1111 /* set is_hcca to 0; it probably will never be implemented */
1112 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
1114 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
1115 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
1117 if (!ieee80211_has_morefrags(hdr->frame_control)) {
1118 txq->need_update = 1;
1119 if (qc)
1120 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
1121 } else {
1122 wait_write_ptr = 1;
1123 txq->need_update = 0;
1126 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
1128 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
1129 ieee80211_hdrlen(fc));
1131 /* Tell device the write index *just past* this latest filled TFD */
1132 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
1133 rc = iwl_txq_update_write_ptr(priv, txq);
1134 spin_unlock_irqrestore(&priv->lock, flags);
1136 if (rc)
1137 return rc;
1139 if ((iwl_queue_space(q) < q->high_mark)
1140 && priv->mac80211_registered) {
1141 if (wait_write_ptr) {
1142 spin_lock_irqsave(&priv->lock, flags);
1143 txq->need_update = 1;
1144 iwl_txq_update_write_ptr(priv, txq);
1145 spin_unlock_irqrestore(&priv->lock, flags);
1148 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
1151 return 0;
1153 drop_unlock:
1154 spin_unlock_irqrestore(&priv->lock, flags);
1155 drop:
1156 return -1;
1159 static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
1161 unsigned long flags;
1163 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
1164 return;
1166 IWL_DEBUG_RF_KILL(priv, "Manual SW RF KILL set to: RADIO %s\n",
1167 disable_radio ? "OFF" : "ON");
1169 if (disable_radio) {
1170 iwl_scan_cancel(priv);
1171 /* FIXME: This is a workaround for AP */
1172 if (priv->iw_mode != NL80211_IFTYPE_AP) {
1173 spin_lock_irqsave(&priv->lock, flags);
1174 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1175 CSR_UCODE_SW_BIT_RFKILL);
1176 spin_unlock_irqrestore(&priv->lock, flags);
1177 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
1178 set_bit(STATUS_RF_KILL_SW, &priv->status);
1180 return;
1183 spin_lock_irqsave(&priv->lock, flags);
1184 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1186 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1187 spin_unlock_irqrestore(&priv->lock, flags);
1189 /* wake up ucode */
1190 msleep(10);
1192 spin_lock_irqsave(&priv->lock, flags);
1193 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1194 if (!iwl_grab_nic_access(priv))
1195 iwl_release_nic_access(priv);
1196 spin_unlock_irqrestore(&priv->lock, flags);
1198 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
1199 IWL_DEBUG_RF_KILL(priv, "Can not turn radio back on - "
1200 "disabled by HW switch\n");
1201 return;
1204 if (priv->is_open)
1205 queue_work(priv->workqueue, &priv->restart);
1206 return;
1209 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
1211 #include "iwl-spectrum.h"
1213 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
1214 #define BEACON_TIME_MASK_HIGH 0xFF000000
1215 #define TIME_UNIT 1024
1218 * extended beacon time format
1219 * time in usec will be changed into a 32-bit value in 8:24 format
1220 * the high 1 byte is the beacon counts
1221 * the lower 3 bytes is the time in usec within one beacon interval
1224 static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
1226 u32 quot;
1227 u32 rem;
1228 u32 interval = beacon_interval * 1024;
1230 if (!interval || !usec)
1231 return 0;
1233 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1234 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1236 return (quot << 24) + rem;
1239 /* base is usually what we get from ucode with each received frame,
1240 * the same as HW timer counter counting down
1243 static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
1245 u32 base_low = base & BEACON_TIME_MASK_LOW;
1246 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1247 u32 interval = beacon_interval * TIME_UNIT;
1248 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1249 (addon & BEACON_TIME_MASK_HIGH);
1251 if (base_low > addon_low)
1252 res += base_low - addon_low;
1253 else if (base_low < addon_low) {
1254 res += interval + base_low - addon_low;
1255 res += (1 << 24);
1256 } else
1257 res += (1 << 24);
1259 return cpu_to_le32(res);
1262 static int iwl3945_get_measurement(struct iwl_priv *priv,
1263 struct ieee80211_measurement_params *params,
1264 u8 type)
1266 struct iwl_spectrum_cmd spectrum;
1267 struct iwl_rx_packet *res;
1268 struct iwl_host_cmd cmd = {
1269 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1270 .data = (void *)&spectrum,
1271 .meta.flags = CMD_WANT_SKB,
1273 u32 add_time = le64_to_cpu(params->start_time);
1274 int rc;
1275 int spectrum_resp_status;
1276 int duration = le16_to_cpu(params->duration);
1278 if (iwl_is_associated(priv))
1279 add_time =
1280 iwl3945_usecs_to_beacons(
1281 le64_to_cpu(params->start_time) - priv->last_tsf,
1282 le16_to_cpu(priv->rxon_timing.beacon_interval));
1284 memset(&spectrum, 0, sizeof(spectrum));
1286 spectrum.channel_count = cpu_to_le16(1);
1287 spectrum.flags =
1288 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1289 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1290 cmd.len = sizeof(spectrum);
1291 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1293 if (iwl_is_associated(priv))
1294 spectrum.start_time =
1295 iwl3945_add_beacon_time(priv->last_beacon_time,
1296 add_time,
1297 le16_to_cpu(priv->rxon_timing.beacon_interval));
1298 else
1299 spectrum.start_time = 0;
1301 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1302 spectrum.channels[0].channel = params->channel;
1303 spectrum.channels[0].type = type;
1304 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1305 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1306 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1308 rc = iwl_send_cmd_sync(priv, &cmd);
1309 if (rc)
1310 return rc;
1312 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1313 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1314 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
1315 rc = -EIO;
1318 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1319 switch (spectrum_resp_status) {
1320 case 0: /* Command will be handled */
1321 if (res->u.spectrum.id != 0xff) {
1322 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
1323 res->u.spectrum.id);
1324 priv->measurement_status &= ~MEASUREMENT_READY;
1326 priv->measurement_status |= MEASUREMENT_ACTIVE;
1327 rc = 0;
1328 break;
1330 case 1: /* Command will not be handled */
1331 rc = -EAGAIN;
1332 break;
1335 dev_kfree_skb_any(cmd.meta.u.skb);
1337 return rc;
1339 #endif
1341 static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
1342 struct iwl_rx_mem_buffer *rxb)
1344 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1345 struct iwl_alive_resp *palive;
1346 struct delayed_work *pwork;
1348 palive = &pkt->u.alive_frame;
1350 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
1351 "0x%01X 0x%01X\n",
1352 palive->is_valid, palive->ver_type,
1353 palive->ver_subtype);
1355 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1356 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
1357 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
1358 sizeof(struct iwl_alive_resp));
1359 pwork = &priv->init_alive_start;
1360 } else {
1361 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
1362 memcpy(&priv->card_alive, &pkt->u.alive_frame,
1363 sizeof(struct iwl_alive_resp));
1364 pwork = &priv->alive_start;
1365 iwl3945_disable_events(priv);
1368 /* We delay the ALIVE response by 5ms to
1369 * give the HW RF Kill time to activate... */
1370 if (palive->is_valid == UCODE_VALID_OK)
1371 queue_delayed_work(priv->workqueue, pwork,
1372 msecs_to_jiffies(5));
1373 else
1374 IWL_WARN(priv, "uCode did not respond OK.\n");
1377 static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
1378 struct iwl_rx_mem_buffer *rxb)
1380 #ifdef CONFIG_IWLWIFI_DEBUG
1381 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1382 #endif
1384 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
1385 return;
1388 static void iwl3945_rx_reply_error(struct iwl_priv *priv,
1389 struct iwl_rx_mem_buffer *rxb)
1391 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1393 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
1394 "seq 0x%04X ser 0x%08X\n",
1395 le32_to_cpu(pkt->u.err_resp.error_type),
1396 get_cmd_string(pkt->u.err_resp.cmd_id),
1397 pkt->u.err_resp.cmd_id,
1398 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1399 le32_to_cpu(pkt->u.err_resp.error_info));
1402 static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
1403 struct iwl_rx_mem_buffer *rxb)
1405 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
1406 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1407 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
1409 if (!report->state) {
1410 IWL_DEBUG(priv, IWL_DL_11H | IWL_DL_INFO,
1411 "Spectrum Measure Notification: Start\n");
1412 return;
1415 memcpy(&priv->measure_report, report, sizeof(*report));
1416 priv->measurement_status |= MEASUREMENT_READY;
1417 #endif
1420 static void iwl3945_bg_beacon_update(struct work_struct *work)
1422 struct iwl_priv *priv =
1423 container_of(work, struct iwl_priv, beacon_update);
1424 struct sk_buff *beacon;
1426 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
1427 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
1429 if (!beacon) {
1430 IWL_ERR(priv, "update beacon failed\n");
1431 return;
1434 mutex_lock(&priv->mutex);
1435 /* new beacon skb is allocated every time; dispose previous.*/
1436 if (priv->ibss_beacon)
1437 dev_kfree_skb(priv->ibss_beacon);
1439 priv->ibss_beacon = beacon;
1440 mutex_unlock(&priv->mutex);
1442 iwl3945_send_beacon_cmd(priv);
1445 static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
1446 struct iwl_rx_mem_buffer *rxb)
1448 #ifdef CONFIG_IWLWIFI_DEBUG
1449 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1450 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
1451 u8 rate = beacon->beacon_notify_hdr.rate;
1453 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
1454 "tsf %d %d rate %d\n",
1455 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
1456 beacon->beacon_notify_hdr.failure_frame,
1457 le32_to_cpu(beacon->ibss_mgr_status),
1458 le32_to_cpu(beacon->high_tsf),
1459 le32_to_cpu(beacon->low_tsf), rate);
1460 #endif
1462 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
1463 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1464 queue_work(priv->workqueue, &priv->beacon_update);
1467 /* Handle notification from uCode that card's power state is changing
1468 * due to software, hardware, or critical temperature RFKILL */
1469 static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
1470 struct iwl_rx_mem_buffer *rxb)
1472 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1473 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1474 unsigned long status = priv->status;
1476 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
1477 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1478 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1480 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1481 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1483 if (flags & HW_CARD_DISABLED)
1484 set_bit(STATUS_RF_KILL_HW, &priv->status);
1485 else
1486 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1489 if (flags & SW_CARD_DISABLED)
1490 set_bit(STATUS_RF_KILL_SW, &priv->status);
1491 else
1492 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1494 iwl_scan_cancel(priv);
1496 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1497 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1498 (test_bit(STATUS_RF_KILL_SW, &status) !=
1499 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1500 queue_work(priv->workqueue, &priv->rf_kill);
1501 else
1502 wake_up_interruptible(&priv->wait_command_queue);
1506 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
1508 * Setup the RX handlers for each of the reply types sent from the uCode
1509 * to the host.
1511 * This function chains into the hardware specific files for them to setup
1512 * any hardware specific handlers as well.
1514 static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
1516 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
1517 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
1518 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
1519 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
1520 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
1521 iwl3945_rx_spectrum_measure_notif;
1522 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
1523 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
1524 iwl_rx_pm_debug_statistics_notif;
1525 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
1528 * The same handler is used for both the REPLY to a discrete
1529 * statistics request from the host as well as for the periodic
1530 * statistics notifications (after received beacons) from the uCode.
1532 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
1533 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
1535 iwl_setup_rx_scan_handlers(priv);
1536 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
1538 /* Set up hardware specific Rx handlers */
1539 iwl3945_hw_rx_handler_setup(priv);
1543 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
1544 * When FW advances 'R' index, all entries between old and new 'R' index
1545 * need to be reclaimed.
1547 static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
1548 int txq_id, int index)
1550 struct iwl_tx_queue *txq = &priv->txq[txq_id];
1551 struct iwl_queue *q = &txq->q;
1552 int nfreed = 0;
1554 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
1555 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
1556 "is out of range [0-%d] %d %d.\n", txq_id,
1557 index, q->n_bd, q->write_ptr, q->read_ptr);
1558 return;
1561 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
1562 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
1563 if (nfreed > 1) {
1564 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
1565 q->write_ptr, q->read_ptr);
1566 queue_work(priv->workqueue, &priv->restart);
1567 break;
1569 nfreed++;
1575 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
1576 * @rxb: Rx buffer to reclaim
1578 * If an Rx buffer has an async callback associated with it the callback
1579 * will be executed. The attached skb (if present) will only be freed
1580 * if the callback returns 1
1582 static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
1583 struct iwl_rx_mem_buffer *rxb)
1585 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1586 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1587 int txq_id = SEQ_TO_QUEUE(sequence);
1588 int index = SEQ_TO_INDEX(sequence);
1589 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
1590 int cmd_index;
1591 struct iwl_cmd *cmd;
1593 if (WARN(txq_id != IWL_CMD_QUEUE_NUM,
1594 "wrong command queue %d, sequence 0x%X readp=%d writep=%d\n",
1595 txq_id, sequence,
1596 priv->txq[IWL_CMD_QUEUE_NUM].q.read_ptr,
1597 priv->txq[IWL_CMD_QUEUE_NUM].q.write_ptr)) {
1598 iwl_print_hex_dump(priv, IWL_DL_INFO , rxb, 32);
1599 return;
1602 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
1603 cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
1605 /* Input error checking is done when commands are added to queue. */
1606 if (cmd->meta.flags & CMD_WANT_SKB) {
1607 cmd->meta.source->u.skb = rxb->skb;
1608 rxb->skb = NULL;
1609 } else if (cmd->meta.u.callback &&
1610 !cmd->meta.u.callback(priv, cmd, rxb->skb))
1611 rxb->skb = NULL;
1613 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
1615 if (!(cmd->meta.flags & CMD_ASYNC)) {
1616 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1617 wake_up_interruptible(&priv->wait_command_queue);
1621 /************************** RX-FUNCTIONS ****************************/
1623 * Rx theory of operation
1625 * The host allocates 32 DMA target addresses and passes the host address
1626 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
1627 * 0 to 31
1629 * Rx Queue Indexes
1630 * The host/firmware share two index registers for managing the Rx buffers.
1632 * The READ index maps to the first position that the firmware may be writing
1633 * to -- the driver can read up to (but not including) this position and get
1634 * good data.
1635 * The READ index is managed by the firmware once the card is enabled.
1637 * The WRITE index maps to the last position the driver has read from -- the
1638 * position preceding WRITE is the last slot the firmware can place a packet.
1640 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
1641 * WRITE = READ.
1643 * During initialization, the host sets up the READ queue position to the first
1644 * INDEX position, and WRITE to the last (READ - 1 wrapped)
1646 * When the firmware places a packet in a buffer, it will advance the READ index
1647 * and fire the RX interrupt. The driver can then query the READ index and
1648 * process as many packets as possible, moving the WRITE index forward as it
1649 * resets the Rx queue buffers with new memory.
1651 * The management in the driver is as follows:
1652 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
1653 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
1654 * to replenish the iwl->rxq->rx_free.
1655 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
1656 * iwl->rxq is replenished and the READ INDEX is updated (updating the
1657 * 'processed' and 'read' driver indexes as well)
1658 * + A received packet is processed and handed to the kernel network stack,
1659 * detached from the iwl->rxq. The driver 'processed' index is updated.
1660 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
1661 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
1662 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
1663 * were enough free buffers and RX_STALLED is set it is cleared.
1666 * Driver sequence:
1668 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
1669 * iwl3945_rx_queue_restock
1670 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
1671 * queue, updates firmware pointers, and updates
1672 * the WRITE index. If insufficient rx_free buffers
1673 * are available, schedules iwl3945_rx_replenish
1675 * -- enable interrupts --
1676 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
1677 * READ INDEX, detaching the SKB from the pool.
1678 * Moves the packet buffer from queue to rx_used.
1679 * Calls iwl3945_rx_queue_restock to refill any empty
1680 * slots.
1681 * ...
1686 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
1688 static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
1689 dma_addr_t dma_addr)
1691 return cpu_to_le32((u32)dma_addr);
1695 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
1697 * If there are slots in the RX queue that need to be restocked,
1698 * and we have free pre-allocated buffers, fill the ranks as much
1699 * as we can, pulling from rx_free.
1701 * This moves the 'write' index forward to catch up with 'processed', and
1702 * also updates the memory address in the firmware to reference the new
1703 * target buffer.
1705 static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
1707 struct iwl_rx_queue *rxq = &priv->rxq;
1708 struct list_head *element;
1709 struct iwl_rx_mem_buffer *rxb;
1710 unsigned long flags;
1711 int write, rc;
1713 spin_lock_irqsave(&rxq->lock, flags);
1714 write = rxq->write & ~0x7;
1715 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
1716 /* Get next free Rx buffer, remove from free list */
1717 element = rxq->rx_free.next;
1718 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1719 list_del(element);
1721 /* Point to Rx buffer via next RBD in circular buffer */
1722 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
1723 rxq->queue[rxq->write] = rxb;
1724 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1725 rxq->free_count--;
1727 spin_unlock_irqrestore(&rxq->lock, flags);
1728 /* If the pre-allocated buffer pool is dropping low, schedule to
1729 * refill it */
1730 if (rxq->free_count <= RX_LOW_WATERMARK)
1731 queue_work(priv->workqueue, &priv->rx_replenish);
1734 /* If we've added more space for the firmware to place data, tell it.
1735 * Increment device's write pointer in multiples of 8. */
1736 if ((write != (rxq->write & ~0x7))
1737 || (abs(rxq->write - rxq->read) > 7)) {
1738 spin_lock_irqsave(&rxq->lock, flags);
1739 rxq->need_update = 1;
1740 spin_unlock_irqrestore(&rxq->lock, flags);
1741 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
1742 if (rc)
1743 return rc;
1746 return 0;
1750 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
1752 * When moving to rx_free an SKB is allocated for the slot.
1754 * Also restock the Rx queue via iwl3945_rx_queue_restock.
1755 * This is called as a scheduled work item (except for during initialization)
1757 static void iwl3945_rx_allocate(struct iwl_priv *priv)
1759 struct iwl_rx_queue *rxq = &priv->rxq;
1760 struct list_head *element;
1761 struct iwl_rx_mem_buffer *rxb;
1762 unsigned long flags;
1763 spin_lock_irqsave(&rxq->lock, flags);
1764 while (!list_empty(&rxq->rx_used)) {
1765 element = rxq->rx_used.next;
1766 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1768 /* Alloc a new receive buffer */
1769 rxb->skb =
1770 alloc_skb(priv->hw_params.rx_buf_size,
1771 __GFP_NOWARN | GFP_ATOMIC);
1772 if (!rxb->skb) {
1773 if (net_ratelimit())
1774 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
1775 /* We don't reschedule replenish work here -- we will
1776 * call the restock method and if it still needs
1777 * more buffers it will schedule replenish */
1778 break;
1781 /* If radiotap head is required, reserve some headroom here.
1782 * The physical head count is a variable rx_stats->phy_count.
1783 * We reserve 4 bytes here. Plus these extra bytes, the
1784 * headroom of the physical head should be enough for the
1785 * radiotap head that iwl3945 supported. See iwl3945_rt.
1787 skb_reserve(rxb->skb, 4);
1789 priv->alloc_rxb_skb++;
1790 list_del(element);
1792 /* Get physical address of RB/SKB */
1793 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
1794 rxb->skb->data,
1795 priv->hw_params.rx_buf_size,
1796 PCI_DMA_FROMDEVICE);
1797 list_add_tail(&rxb->list, &rxq->rx_free);
1798 rxq->free_count++;
1800 spin_unlock_irqrestore(&rxq->lock, flags);
1804 * this should be called while priv->lock is locked
1806 static void __iwl3945_rx_replenish(void *data)
1808 struct iwl_priv *priv = data;
1810 iwl3945_rx_allocate(priv);
1811 iwl3945_rx_queue_restock(priv);
1815 void iwl3945_rx_replenish(void *data)
1817 struct iwl_priv *priv = data;
1818 unsigned long flags;
1820 iwl3945_rx_allocate(priv);
1822 spin_lock_irqsave(&priv->lock, flags);
1823 iwl3945_rx_queue_restock(priv);
1824 spin_unlock_irqrestore(&priv->lock, flags);
1827 /* Convert linear signal-to-noise ratio into dB */
1828 static u8 ratio2dB[100] = {
1829 /* 0 1 2 3 4 5 6 7 8 9 */
1830 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1831 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1832 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1833 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1834 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1835 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1836 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1837 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1838 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1839 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1842 /* Calculates a relative dB value from a ratio of linear
1843 * (i.e. not dB) signal levels.
1844 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
1845 int iwl3945_calc_db_from_ratio(int sig_ratio)
1847 /* 1000:1 or higher just report as 60 dB */
1848 if (sig_ratio >= 1000)
1849 return 60;
1851 /* 100:1 or higher, divide by 10 and use table,
1852 * add 20 dB to make up for divide by 10 */
1853 if (sig_ratio >= 100)
1854 return 20 + (int)ratio2dB[sig_ratio/10];
1856 /* We shouldn't see this */
1857 if (sig_ratio < 1)
1858 return 0;
1860 /* Use table for ratios 1:1 - 99:1 */
1861 return (int)ratio2dB[sig_ratio];
1864 #define PERFECT_RSSI (-20) /* dBm */
1865 #define WORST_RSSI (-95) /* dBm */
1866 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1868 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
1869 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1870 * about formulas used below. */
1871 int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
1873 int sig_qual;
1874 int degradation = PERFECT_RSSI - rssi_dbm;
1876 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1877 * as indicator; formula is (signal dbm - noise dbm).
1878 * SNR at or above 40 is a great signal (100%).
1879 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1880 * Weakest usable signal is usually 10 - 15 dB SNR. */
1881 if (noise_dbm) {
1882 if (rssi_dbm - noise_dbm >= 40)
1883 return 100;
1884 else if (rssi_dbm < noise_dbm)
1885 return 0;
1886 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1888 /* Else use just the signal level.
1889 * This formula is a least squares fit of data points collected and
1890 * compared with a reference system that had a percentage (%) display
1891 * for signal quality. */
1892 } else
1893 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1894 (15 * RSSI_RANGE + 62 * degradation)) /
1895 (RSSI_RANGE * RSSI_RANGE);
1897 if (sig_qual > 100)
1898 sig_qual = 100;
1899 else if (sig_qual < 1)
1900 sig_qual = 0;
1902 return sig_qual;
1906 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
1908 * Uses the priv->rx_handlers callback function array to invoke
1909 * the appropriate handlers, including command responses,
1910 * frame-received notifications, and other notifications.
1912 static void iwl3945_rx_handle(struct iwl_priv *priv)
1914 struct iwl_rx_mem_buffer *rxb;
1915 struct iwl_rx_packet *pkt;
1916 struct iwl_rx_queue *rxq = &priv->rxq;
1917 u32 r, i;
1918 int reclaim;
1919 unsigned long flags;
1920 u8 fill_rx = 0;
1921 u32 count = 8;
1923 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1924 * buffer that the driver may process (last buffer filled by ucode). */
1925 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
1926 i = rxq->read;
1928 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
1929 fill_rx = 1;
1930 /* Rx interrupt, but nothing sent from uCode */
1931 if (i == r)
1932 IWL_DEBUG(priv, IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
1934 while (i != r) {
1935 rxb = rxq->queue[i];
1937 /* If an RXB doesn't have a Rx queue slot associated with it,
1938 * then a bug has been introduced in the queue refilling
1939 * routines -- catch it here */
1940 BUG_ON(rxb == NULL);
1942 rxq->queue[i] = NULL;
1944 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
1945 priv->hw_params.rx_buf_size,
1946 PCI_DMA_FROMDEVICE);
1947 pkt = (struct iwl_rx_packet *)rxb->skb->data;
1949 /* Reclaim a command buffer only if this packet is a response
1950 * to a (driver-originated) command.
1951 * If the packet (e.g. Rx frame) originated from uCode,
1952 * there is no command buffer to reclaim.
1953 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1954 * but apparently a few don't get set; catch them here. */
1955 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1956 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1957 (pkt->hdr.cmd != REPLY_TX);
1959 /* Based on type of command response or notification,
1960 * handle those that need handling via function in
1961 * rx_handlers table. See iwl3945_setup_rx_handlers() */
1962 if (priv->rx_handlers[pkt->hdr.cmd]) {
1963 IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
1964 "r = %d, i = %d, %s, 0x%02x\n", r, i,
1965 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1966 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1967 } else {
1968 /* No handling needed */
1969 IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
1970 "r %d i %d No handler needed for %s, 0x%02x\n",
1971 r, i, get_cmd_string(pkt->hdr.cmd),
1972 pkt->hdr.cmd);
1975 if (reclaim) {
1976 /* Invoke any callbacks, transfer the skb to caller, and
1977 * fire off the (possibly) blocking iwl_send_cmd()
1978 * as we reclaim the driver command queue */
1979 if (rxb && rxb->skb)
1980 iwl3945_tx_cmd_complete(priv, rxb);
1981 else
1982 IWL_WARN(priv, "Claim null rxb?\n");
1985 /* For now we just don't re-use anything. We can tweak this
1986 * later to try and re-use notification packets and SKBs that
1987 * fail to Rx correctly */
1988 if (rxb->skb != NULL) {
1989 priv->alloc_rxb_skb--;
1990 dev_kfree_skb_any(rxb->skb);
1991 rxb->skb = NULL;
1994 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
1995 priv->hw_params.rx_buf_size,
1996 PCI_DMA_FROMDEVICE);
1997 spin_lock_irqsave(&rxq->lock, flags);
1998 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1999 spin_unlock_irqrestore(&rxq->lock, flags);
2000 i = (i + 1) & RX_QUEUE_MASK;
2001 /* If there are a lot of unused frames,
2002 * restock the Rx queue so ucode won't assert. */
2003 if (fill_rx) {
2004 count++;
2005 if (count >= 8) {
2006 priv->rxq.read = i;
2007 __iwl3945_rx_replenish(priv);
2008 count = 0;
2013 /* Backtrack one entry */
2014 priv->rxq.read = i;
2015 iwl3945_rx_queue_restock(priv);
2018 static void iwl3945_enable_interrupts(struct iwl_priv *priv)
2020 IWL_DEBUG_ISR(priv, "Enabling interrupts\n");
2021 set_bit(STATUS_INT_ENABLED, &priv->status);
2022 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
2026 /* call this function to flush any scheduled tasklet */
2027 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
2029 /* wait to make sure we flush pending tasklet*/
2030 synchronize_irq(priv->pci_dev->irq);
2031 tasklet_kill(&priv->irq_tasklet);
2035 static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
2037 clear_bit(STATUS_INT_ENABLED, &priv->status);
2039 /* disable interrupts from uCode/NIC to host */
2040 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
2042 /* acknowledge/clear/reset any interrupts still pending
2043 * from uCode or flow handler (Rx/Tx DMA) */
2044 iwl_write32(priv, CSR_INT, 0xffffffff);
2045 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
2046 IWL_DEBUG_ISR(priv, "Disabled interrupts\n");
2049 static const char *desc_lookup(int i)
2051 switch (i) {
2052 case 1:
2053 return "FAIL";
2054 case 2:
2055 return "BAD_PARAM";
2056 case 3:
2057 return "BAD_CHECKSUM";
2058 case 4:
2059 return "NMI_INTERRUPT";
2060 case 5:
2061 return "SYSASSERT";
2062 case 6:
2063 return "FATAL_ERROR";
2066 return "UNKNOWN";
2069 #define ERROR_START_OFFSET (1 * sizeof(u32))
2070 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
2072 static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
2074 u32 i;
2075 u32 desc, time, count, base, data1;
2076 u32 blink1, blink2, ilink1, ilink2;
2077 int rc;
2079 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
2081 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
2082 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
2083 return;
2086 rc = iwl_grab_nic_access(priv);
2087 if (rc) {
2088 IWL_WARN(priv, "Can not read from adapter at this time.\n");
2089 return;
2092 count = iwl_read_targ_mem(priv, base);
2094 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
2095 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
2096 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
2097 priv->status, count);
2100 IWL_ERR(priv, "Desc Time asrtPC blink2 "
2101 "ilink1 nmiPC Line\n");
2102 for (i = ERROR_START_OFFSET;
2103 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
2104 i += ERROR_ELEM_SIZE) {
2105 desc = iwl_read_targ_mem(priv, base + i);
2106 time =
2107 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
2108 blink1 =
2109 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
2110 blink2 =
2111 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
2112 ilink1 =
2113 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
2114 ilink2 =
2115 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
2116 data1 =
2117 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
2119 IWL_ERR(priv,
2120 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
2121 desc_lookup(desc), desc, time, blink1, blink2,
2122 ilink1, ilink2, data1);
2125 iwl_release_nic_access(priv);
2129 #define EVENT_START_OFFSET (6 * sizeof(u32))
2132 * iwl3945_print_event_log - Dump error event log to syslog
2134 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
2136 static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
2137 u32 num_events, u32 mode)
2139 u32 i;
2140 u32 base; /* SRAM byte address of event log header */
2141 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
2142 u32 ptr; /* SRAM byte address of log data */
2143 u32 ev, time, data; /* event log data */
2145 if (num_events == 0)
2146 return;
2148 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
2150 if (mode == 0)
2151 event_size = 2 * sizeof(u32);
2152 else
2153 event_size = 3 * sizeof(u32);
2155 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
2157 /* "time" is actually "data" for mode 0 (no timestamp).
2158 * place event id # at far right for easier visual parsing. */
2159 for (i = 0; i < num_events; i++) {
2160 ev = iwl_read_targ_mem(priv, ptr);
2161 ptr += sizeof(u32);
2162 time = iwl_read_targ_mem(priv, ptr);
2163 ptr += sizeof(u32);
2164 if (mode == 0) {
2165 /* data, ev */
2166 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
2167 } else {
2168 data = iwl_read_targ_mem(priv, ptr);
2169 ptr += sizeof(u32);
2170 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
2175 static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
2177 int rc;
2178 u32 base; /* SRAM byte address of event log header */
2179 u32 capacity; /* event log capacity in # entries */
2180 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
2181 u32 num_wraps; /* # times uCode wrapped to top of log */
2182 u32 next_entry; /* index of next entry to be written by uCode */
2183 u32 size; /* # entries that we'll print */
2185 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
2186 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
2187 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
2188 return;
2191 rc = iwl_grab_nic_access(priv);
2192 if (rc) {
2193 IWL_WARN(priv, "Can not read from adapter at this time.\n");
2194 return;
2197 /* event log header */
2198 capacity = iwl_read_targ_mem(priv, base);
2199 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
2200 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
2201 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
2203 size = num_wraps ? capacity : next_entry;
2205 /* bail out if nothing in log */
2206 if (size == 0) {
2207 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
2208 iwl_release_nic_access(priv);
2209 return;
2212 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
2213 size, num_wraps);
2215 /* if uCode has wrapped back to top of log, start at the oldest entry,
2216 * i.e the next one that uCode would fill. */
2217 if (num_wraps)
2218 iwl3945_print_event_log(priv, next_entry,
2219 capacity - next_entry, mode);
2221 /* (then/else) start at top of log */
2222 iwl3945_print_event_log(priv, 0, next_entry, mode);
2224 iwl_release_nic_access(priv);
2227 static void iwl3945_error_recovery(struct iwl_priv *priv)
2229 unsigned long flags;
2231 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
2232 sizeof(priv->staging_rxon));
2233 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2234 iwl3945_commit_rxon(priv);
2236 iwl3945_add_station(priv, priv->bssid, 1, 0);
2238 spin_lock_irqsave(&priv->lock, flags);
2239 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
2240 priv->error_recovering = 0;
2241 spin_unlock_irqrestore(&priv->lock, flags);
2244 static void iwl3945_irq_tasklet(struct iwl_priv *priv)
2246 u32 inta, handled = 0;
2247 u32 inta_fh;
2248 unsigned long flags;
2249 #ifdef CONFIG_IWLWIFI_DEBUG
2250 u32 inta_mask;
2251 #endif
2253 spin_lock_irqsave(&priv->lock, flags);
2255 /* Ack/clear/reset pending uCode interrupts.
2256 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
2257 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
2258 inta = iwl_read32(priv, CSR_INT);
2259 iwl_write32(priv, CSR_INT, inta);
2261 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
2262 * Any new interrupts that happen after this, either while we're
2263 * in this tasklet, or later, will show up in next ISR/tasklet. */
2264 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2265 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
2267 #ifdef CONFIG_IWLWIFI_DEBUG
2268 if (priv->debug_level & IWL_DL_ISR) {
2269 /* just for debug */
2270 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2271 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2272 inta, inta_mask, inta_fh);
2274 #endif
2276 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
2277 * atomic, make sure that inta covers all the interrupts that
2278 * we've discovered, even if FH interrupt came in just after
2279 * reading CSR_INT. */
2280 if (inta_fh & CSR39_FH_INT_RX_MASK)
2281 inta |= CSR_INT_BIT_FH_RX;
2282 if (inta_fh & CSR39_FH_INT_TX_MASK)
2283 inta |= CSR_INT_BIT_FH_TX;
2285 /* Now service all interrupt bits discovered above. */
2286 if (inta & CSR_INT_BIT_HW_ERR) {
2287 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
2289 /* Tell the device to stop sending interrupts */
2290 iwl3945_disable_interrupts(priv);
2292 iwl_irq_handle_error(priv);
2294 handled |= CSR_INT_BIT_HW_ERR;
2296 spin_unlock_irqrestore(&priv->lock, flags);
2298 return;
2301 #ifdef CONFIG_IWLWIFI_DEBUG
2302 if (priv->debug_level & (IWL_DL_ISR)) {
2303 /* NIC fires this, but we don't use it, redundant with WAKEUP */
2304 if (inta & CSR_INT_BIT_SCD)
2305 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
2306 "the frame/frames.\n");
2308 /* Alive notification via Rx interrupt will do the real work */
2309 if (inta & CSR_INT_BIT_ALIVE)
2310 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
2312 #endif
2313 /* Safely ignore these bits for debug checks below */
2314 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
2316 /* Error detected by uCode */
2317 if (inta & CSR_INT_BIT_SW_ERR) {
2318 IWL_ERR(priv, "Microcode SW error detected. "
2319 "Restarting 0x%X.\n", inta);
2320 iwl_irq_handle_error(priv);
2321 handled |= CSR_INT_BIT_SW_ERR;
2324 /* uCode wakes up after power-down sleep */
2325 if (inta & CSR_INT_BIT_WAKEUP) {
2326 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
2327 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
2328 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
2329 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
2330 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
2331 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
2332 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
2333 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
2335 handled |= CSR_INT_BIT_WAKEUP;
2338 /* All uCode command responses, including Tx command responses,
2339 * Rx "responses" (frame-received notification), and other
2340 * notifications from uCode come through here*/
2341 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
2342 iwl3945_rx_handle(priv);
2343 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
2346 if (inta & CSR_INT_BIT_FH_TX) {
2347 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
2349 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
2350 if (!iwl_grab_nic_access(priv)) {
2351 iwl_write_direct32(priv, FH39_TCSR_CREDIT
2352 (FH39_SRVC_CHNL), 0x0);
2353 iwl_release_nic_access(priv);
2355 handled |= CSR_INT_BIT_FH_TX;
2358 if (inta & ~handled)
2359 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
2361 if (inta & ~CSR_INI_SET_MASK) {
2362 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
2363 inta & ~CSR_INI_SET_MASK);
2364 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
2367 /* Re-enable all interrupts */
2368 /* only Re-enable if disabled by irq */
2369 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2370 iwl3945_enable_interrupts(priv);
2372 #ifdef CONFIG_IWLWIFI_DEBUG
2373 if (priv->debug_level & (IWL_DL_ISR)) {
2374 inta = iwl_read32(priv, CSR_INT);
2375 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2376 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2377 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
2378 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2380 #endif
2381 spin_unlock_irqrestore(&priv->lock, flags);
2384 static irqreturn_t iwl3945_isr(int irq, void *data)
2386 struct iwl_priv *priv = data;
2387 u32 inta, inta_mask;
2388 u32 inta_fh;
2389 if (!priv)
2390 return IRQ_NONE;
2392 spin_lock(&priv->lock);
2394 /* Disable (but don't clear!) interrupts here to avoid
2395 * back-to-back ISRs and sporadic interrupts from our NIC.
2396 * If we have something to service, the tasklet will re-enable ints.
2397 * If we *don't* have something, we'll re-enable before leaving here. */
2398 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
2399 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
2401 /* Discover which interrupts are active/pending */
2402 inta = iwl_read32(priv, CSR_INT);
2403 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2405 /* Ignore interrupt if there's nothing in NIC to service.
2406 * This may be due to IRQ shared with another device,
2407 * or due to sporadic interrupts thrown from our NIC. */
2408 if (!inta && !inta_fh) {
2409 IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0, inta_fh == 0\n");
2410 goto none;
2413 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
2414 /* Hardware disappeared */
2415 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
2416 goto unplugged;
2419 IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2420 inta, inta_mask, inta_fh);
2422 inta &= ~CSR_INT_BIT_SCD;
2424 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
2425 if (likely(inta || inta_fh))
2426 tasklet_schedule(&priv->irq_tasklet);
2427 unplugged:
2428 spin_unlock(&priv->lock);
2430 return IRQ_HANDLED;
2432 none:
2433 /* re-enable interrupts here since we don't have anything to service. */
2434 /* only Re-enable if disabled by irq */
2435 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2436 iwl3945_enable_interrupts(priv);
2437 spin_unlock(&priv->lock);
2438 return IRQ_NONE;
2441 static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
2442 enum ieee80211_band band,
2443 u8 is_active, u8 n_probes,
2444 struct iwl3945_scan_channel *scan_ch)
2446 const struct ieee80211_channel *channels = NULL;
2447 const struct ieee80211_supported_band *sband;
2448 const struct iwl_channel_info *ch_info;
2449 u16 passive_dwell = 0;
2450 u16 active_dwell = 0;
2451 int added, i;
2453 sband = iwl_get_hw_mode(priv, band);
2454 if (!sband)
2455 return 0;
2457 channels = sband->channels;
2459 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
2460 passive_dwell = iwl_get_passive_dwell_time(priv, band);
2462 if (passive_dwell <= active_dwell)
2463 passive_dwell = active_dwell + 1;
2465 for (i = 0, added = 0; i < sband->n_channels; i++) {
2466 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2467 continue;
2469 scan_ch->channel = channels[i].hw_value;
2471 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
2472 if (!is_channel_valid(ch_info)) {
2473 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
2474 scan_ch->channel);
2475 continue;
2478 scan_ch->active_dwell = cpu_to_le16(active_dwell);
2479 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
2480 /* If passive , set up for auto-switch
2481 * and use long active_dwell time.
2483 if (!is_active || is_channel_passive(ch_info) ||
2484 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
2485 scan_ch->type = 0; /* passive */
2486 if (IWL_UCODE_API(priv->ucode_ver) == 1)
2487 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
2488 } else {
2489 scan_ch->type = 1; /* active */
2492 /* Set direct probe bits. These may be used both for active
2493 * scan channels (probes gets sent right away),
2494 * or for passive channels (probes get se sent only after
2495 * hearing clear Rx packet).*/
2496 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
2497 if (n_probes)
2498 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
2499 } else {
2500 /* uCode v1 does not allow setting direct probe bits on
2501 * passive channel. */
2502 if ((scan_ch->type & 1) && n_probes)
2503 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
2506 /* Set txpower levels to defaults */
2507 scan_ch->tpc.dsp_atten = 110;
2508 /* scan_pwr_info->tpc.dsp_atten; */
2510 /*scan_pwr_info->tpc.tx_gain; */
2511 if (band == IEEE80211_BAND_5GHZ)
2512 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
2513 else {
2514 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
2515 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
2516 * power level:
2517 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
2521 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
2522 scan_ch->channel,
2523 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
2524 (scan_ch->type & 1) ?
2525 active_dwell : passive_dwell);
2527 scan_ch++;
2528 added++;
2531 IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
2532 return added;
2535 static void iwl3945_init_hw_rates(struct iwl_priv *priv,
2536 struct ieee80211_rate *rates)
2538 int i;
2540 for (i = 0; i < IWL_RATE_COUNT; i++) {
2541 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
2542 rates[i].hw_value = i; /* Rate scaling will work on indexes */
2543 rates[i].hw_value_short = i;
2544 rates[i].flags = 0;
2545 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
2547 * If CCK != 1M then set short preamble rate flag.
2549 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
2550 0 : IEEE80211_RATE_SHORT_PREAMBLE;
2555 /******************************************************************************
2557 * uCode download functions
2559 ******************************************************************************/
2561 static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
2563 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
2564 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
2565 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2566 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
2567 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2568 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
2572 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
2573 * looking at all data.
2575 static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
2577 u32 val;
2578 u32 save_len = len;
2579 int rc = 0;
2580 u32 errcnt;
2582 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
2584 rc = iwl_grab_nic_access(priv);
2585 if (rc)
2586 return rc;
2588 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
2589 IWL39_RTC_INST_LOWER_BOUND);
2591 errcnt = 0;
2592 for (; len > 0; len -= sizeof(u32), image++) {
2593 /* read data comes through single port, auto-incr addr */
2594 /* NOTE: Use the debugless read so we don't flood kernel log
2595 * if IWL_DL_IO is set */
2596 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
2597 if (val != le32_to_cpu(*image)) {
2598 IWL_ERR(priv, "uCode INST section is invalid at "
2599 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2600 save_len - len, val, le32_to_cpu(*image));
2601 rc = -EIO;
2602 errcnt++;
2603 if (errcnt >= 20)
2604 break;
2608 iwl_release_nic_access(priv);
2610 if (!errcnt)
2611 IWL_DEBUG_INFO(priv,
2612 "ucode image in INSTRUCTION memory is good\n");
2614 return rc;
2619 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
2620 * using sample data 100 bytes apart. If these sample points are good,
2621 * it's a pretty good bet that everything between them is good, too.
2623 static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
2625 u32 val;
2626 int rc = 0;
2627 u32 errcnt = 0;
2628 u32 i;
2630 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
2632 rc = iwl_grab_nic_access(priv);
2633 if (rc)
2634 return rc;
2636 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2637 /* read data comes through single port, auto-incr addr */
2638 /* NOTE: Use the debugless read so we don't flood kernel log
2639 * if IWL_DL_IO is set */
2640 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
2641 i + IWL39_RTC_INST_LOWER_BOUND);
2642 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
2643 if (val != le32_to_cpu(*image)) {
2644 #if 0 /* Enable this if you want to see details */
2645 IWL_ERR(priv, "uCode INST section is invalid at "
2646 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2647 i, val, *image);
2648 #endif
2649 rc = -EIO;
2650 errcnt++;
2651 if (errcnt >= 3)
2652 break;
2656 iwl_release_nic_access(priv);
2658 return rc;
2663 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
2664 * and verify its contents
2666 static int iwl3945_verify_ucode(struct iwl_priv *priv)
2668 __le32 *image;
2669 u32 len;
2670 int rc = 0;
2672 /* Try bootstrap */
2673 image = (__le32 *)priv->ucode_boot.v_addr;
2674 len = priv->ucode_boot.len;
2675 rc = iwl3945_verify_inst_sparse(priv, image, len);
2676 if (rc == 0) {
2677 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
2678 return 0;
2681 /* Try initialize */
2682 image = (__le32 *)priv->ucode_init.v_addr;
2683 len = priv->ucode_init.len;
2684 rc = iwl3945_verify_inst_sparse(priv, image, len);
2685 if (rc == 0) {
2686 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
2687 return 0;
2690 /* Try runtime/protocol */
2691 image = (__le32 *)priv->ucode_code.v_addr;
2692 len = priv->ucode_code.len;
2693 rc = iwl3945_verify_inst_sparse(priv, image, len);
2694 if (rc == 0) {
2695 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
2696 return 0;
2699 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
2701 /* Since nothing seems to match, show first several data entries in
2702 * instruction SRAM, so maybe visual inspection will give a clue.
2703 * Selection of bootstrap image (vs. other images) is arbitrary. */
2704 image = (__le32 *)priv->ucode_boot.v_addr;
2705 len = priv->ucode_boot.len;
2706 rc = iwl3945_verify_inst_full(priv, image, len);
2708 return rc;
2711 static void iwl3945_nic_start(struct iwl_priv *priv)
2713 /* Remove all resets to allow NIC to operate */
2714 iwl_write32(priv, CSR_RESET, 0);
2718 * iwl3945_read_ucode - Read uCode images from disk file.
2720 * Copy into buffers for card to fetch via bus-mastering
2722 static int iwl3945_read_ucode(struct iwl_priv *priv)
2724 struct iwl_ucode *ucode;
2725 int ret = -EINVAL, index;
2726 const struct firmware *ucode_raw;
2727 /* firmware file name contains uCode/driver compatibility version */
2728 const char *name_pre = priv->cfg->fw_name_pre;
2729 const unsigned int api_max = priv->cfg->ucode_api_max;
2730 const unsigned int api_min = priv->cfg->ucode_api_min;
2731 char buf[25];
2732 u8 *src;
2733 size_t len;
2734 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
2736 /* Ask kernel firmware_class module to get the boot firmware off disk.
2737 * request_firmware() is synchronous, file is in memory on return. */
2738 for (index = api_max; index >= api_min; index--) {
2739 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2740 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2741 if (ret < 0) {
2742 IWL_ERR(priv, "%s firmware file req failed: %d\n",
2743 buf, ret);
2744 if (ret == -ENOENT)
2745 continue;
2746 else
2747 goto error;
2748 } else {
2749 if (index < api_max)
2750 IWL_ERR(priv, "Loaded firmware %s, "
2751 "which is deprecated. "
2752 " Please use API v%u instead.\n",
2753 buf, api_max);
2754 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2755 "(%zd bytes) from disk\n",
2756 buf, ucode_raw->size);
2757 break;
2761 if (ret < 0)
2762 goto error;
2764 /* Make sure that we got at least our header! */
2765 if (ucode_raw->size < sizeof(*ucode)) {
2766 IWL_ERR(priv, "File size way too small!\n");
2767 ret = -EINVAL;
2768 goto err_release;
2771 /* Data from ucode file: header followed by uCode images */
2772 ucode = (void *)ucode_raw->data;
2774 priv->ucode_ver = le32_to_cpu(ucode->ver);
2775 api_ver = IWL_UCODE_API(priv->ucode_ver);
2776 inst_size = le32_to_cpu(ucode->inst_size);
2777 data_size = le32_to_cpu(ucode->data_size);
2778 init_size = le32_to_cpu(ucode->init_size);
2779 init_data_size = le32_to_cpu(ucode->init_data_size);
2780 boot_size = le32_to_cpu(ucode->boot_size);
2782 /* api_ver should match the api version forming part of the
2783 * firmware filename ... but we don't check for that and only rely
2784 * on the API version read from firware header from here on forward */
2786 if (api_ver < api_min || api_ver > api_max) {
2787 IWL_ERR(priv, "Driver unable to support your firmware API. "
2788 "Driver supports v%u, firmware is v%u.\n",
2789 api_max, api_ver);
2790 priv->ucode_ver = 0;
2791 ret = -EINVAL;
2792 goto err_release;
2794 if (api_ver != api_max)
2795 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
2796 "got %u. New firmware can be obtained "
2797 "from http://www.intellinuxwireless.org.\n",
2798 api_max, api_ver);
2800 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2801 IWL_UCODE_MAJOR(priv->ucode_ver),
2802 IWL_UCODE_MINOR(priv->ucode_ver),
2803 IWL_UCODE_API(priv->ucode_ver),
2804 IWL_UCODE_SERIAL(priv->ucode_ver));
2806 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
2807 priv->ucode_ver);
2808 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2809 inst_size);
2810 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2811 data_size);
2812 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2813 init_size);
2814 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2815 init_data_size);
2816 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2817 boot_size);
2820 /* Verify size of file vs. image size info in file's header */
2821 if (ucode_raw->size < sizeof(*ucode) +
2822 inst_size + data_size + init_size +
2823 init_data_size + boot_size) {
2825 IWL_DEBUG_INFO(priv, "uCode file size %zd too small\n",
2826 ucode_raw->size);
2827 ret = -EINVAL;
2828 goto err_release;
2831 /* Verify that uCode images will fit in card's SRAM */
2832 if (inst_size > IWL39_MAX_INST_SIZE) {
2833 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
2834 inst_size);
2835 ret = -EINVAL;
2836 goto err_release;
2839 if (data_size > IWL39_MAX_DATA_SIZE) {
2840 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
2841 data_size);
2842 ret = -EINVAL;
2843 goto err_release;
2845 if (init_size > IWL39_MAX_INST_SIZE) {
2846 IWL_DEBUG_INFO(priv,
2847 "uCode init instr len %d too large to fit in\n",
2848 init_size);
2849 ret = -EINVAL;
2850 goto err_release;
2852 if (init_data_size > IWL39_MAX_DATA_SIZE) {
2853 IWL_DEBUG_INFO(priv,
2854 "uCode init data len %d too large to fit in\n",
2855 init_data_size);
2856 ret = -EINVAL;
2857 goto err_release;
2859 if (boot_size > IWL39_MAX_BSM_SIZE) {
2860 IWL_DEBUG_INFO(priv,
2861 "uCode boot instr len %d too large to fit in\n",
2862 boot_size);
2863 ret = -EINVAL;
2864 goto err_release;
2867 /* Allocate ucode buffers for card's bus-master loading ... */
2869 /* Runtime instructions and 2 copies of data:
2870 * 1) unmodified from disk
2871 * 2) backup cache for save/restore during power-downs */
2872 priv->ucode_code.len = inst_size;
2873 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
2875 priv->ucode_data.len = data_size;
2876 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
2878 priv->ucode_data_backup.len = data_size;
2879 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2881 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
2882 !priv->ucode_data_backup.v_addr)
2883 goto err_pci_alloc;
2885 /* Initialization instructions and data */
2886 if (init_size && init_data_size) {
2887 priv->ucode_init.len = init_size;
2888 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
2890 priv->ucode_init_data.len = init_data_size;
2891 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2893 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2894 goto err_pci_alloc;
2897 /* Bootstrap (instructions only, no data) */
2898 if (boot_size) {
2899 priv->ucode_boot.len = boot_size;
2900 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
2902 if (!priv->ucode_boot.v_addr)
2903 goto err_pci_alloc;
2906 /* Copy images into buffers for card's bus-master reads ... */
2908 /* Runtime instructions (first block of data in file) */
2909 src = &ucode->data[0];
2910 len = priv->ucode_code.len;
2911 IWL_DEBUG_INFO(priv,
2912 "Copying (but not loading) uCode instr len %zd\n", len);
2913 memcpy(priv->ucode_code.v_addr, src, len);
2914 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2915 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2917 /* Runtime data (2nd block)
2918 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
2919 src = &ucode->data[inst_size];
2920 len = priv->ucode_data.len;
2921 IWL_DEBUG_INFO(priv,
2922 "Copying (but not loading) uCode data len %zd\n", len);
2923 memcpy(priv->ucode_data.v_addr, src, len);
2924 memcpy(priv->ucode_data_backup.v_addr, src, len);
2926 /* Initialization instructions (3rd block) */
2927 if (init_size) {
2928 src = &ucode->data[inst_size + data_size];
2929 len = priv->ucode_init.len;
2930 IWL_DEBUG_INFO(priv,
2931 "Copying (but not loading) init instr len %zd\n", len);
2932 memcpy(priv->ucode_init.v_addr, src, len);
2935 /* Initialization data (4th block) */
2936 if (init_data_size) {
2937 src = &ucode->data[inst_size + data_size + init_size];
2938 len = priv->ucode_init_data.len;
2939 IWL_DEBUG_INFO(priv,
2940 "Copying (but not loading) init data len %zd\n", len);
2941 memcpy(priv->ucode_init_data.v_addr, src, len);
2944 /* Bootstrap instructions (5th block) */
2945 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2946 len = priv->ucode_boot.len;
2947 IWL_DEBUG_INFO(priv,
2948 "Copying (but not loading) boot instr len %zd\n", len);
2949 memcpy(priv->ucode_boot.v_addr, src, len);
2951 /* We have our copies now, allow OS release its copies */
2952 release_firmware(ucode_raw);
2953 return 0;
2955 err_pci_alloc:
2956 IWL_ERR(priv, "failed to allocate pci memory\n");
2957 ret = -ENOMEM;
2958 iwl3945_dealloc_ucode_pci(priv);
2960 err_release:
2961 release_firmware(ucode_raw);
2963 error:
2964 return ret;
2969 * iwl3945_set_ucode_ptrs - Set uCode address location
2971 * Tell initialization uCode where to find runtime uCode.
2973 * BSM registers initially contain pointers to initialization uCode.
2974 * We need to replace them to load runtime uCode inst and data,
2975 * and to save runtime data when powering down.
2977 static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
2979 dma_addr_t pinst;
2980 dma_addr_t pdata;
2981 int rc = 0;
2982 unsigned long flags;
2984 /* bits 31:0 for 3945 */
2985 pinst = priv->ucode_code.p_addr;
2986 pdata = priv->ucode_data_backup.p_addr;
2988 spin_lock_irqsave(&priv->lock, flags);
2989 rc = iwl_grab_nic_access(priv);
2990 if (rc) {
2991 spin_unlock_irqrestore(&priv->lock, flags);
2992 return rc;
2995 /* Tell bootstrap uCode where to find image to load */
2996 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2997 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2998 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
2999 priv->ucode_data.len);
3001 /* Inst byte count must be last to set up, bit 31 signals uCode
3002 * that all new ptr/size info is in place */
3003 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
3004 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
3006 iwl_release_nic_access(priv);
3008 spin_unlock_irqrestore(&priv->lock, flags);
3010 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
3012 return rc;
3016 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
3018 * Called after REPLY_ALIVE notification received from "initialize" uCode.
3020 * Tell "initialize" uCode to go ahead and load the runtime uCode.
3022 static void iwl3945_init_alive_start(struct iwl_priv *priv)
3024 /* Check alive response for "valid" sign from uCode */
3025 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
3026 /* We had an error bringing up the hardware, so take it
3027 * all the way back down so we can try again */
3028 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
3029 goto restart;
3032 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
3033 * This is a paranoid check, because we would not have gotten the
3034 * "initialize" alive if code weren't properly loaded. */
3035 if (iwl3945_verify_ucode(priv)) {
3036 /* Runtime instruction load was bad;
3037 * take it all the way back down so we can try again */
3038 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
3039 goto restart;
3042 /* Send pointers to protocol/runtime uCode image ... init code will
3043 * load and launch runtime uCode, which will send us another "Alive"
3044 * notification. */
3045 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
3046 if (iwl3945_set_ucode_ptrs(priv)) {
3047 /* Runtime instruction load won't happen;
3048 * take it all the way back down so we can try again */
3049 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
3050 goto restart;
3052 return;
3054 restart:
3055 queue_work(priv->workqueue, &priv->restart);
3059 /* temporary */
3060 static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
3061 struct sk_buff *skb);
3064 * iwl3945_alive_start - called after REPLY_ALIVE notification received
3065 * from protocol/runtime uCode (initialization uCode's
3066 * Alive gets handled by iwl3945_init_alive_start()).
3068 static void iwl3945_alive_start(struct iwl_priv *priv)
3070 int rc = 0;
3071 int thermal_spin = 0;
3072 u32 rfkill;
3074 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
3076 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
3077 /* We had an error bringing up the hardware, so take it
3078 * all the way back down so we can try again */
3079 IWL_DEBUG_INFO(priv, "Alive failed.\n");
3080 goto restart;
3083 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
3084 * This is a paranoid check, because we would not have gotten the
3085 * "runtime" alive if code weren't properly loaded. */
3086 if (iwl3945_verify_ucode(priv)) {
3087 /* Runtime instruction load was bad;
3088 * take it all the way back down so we can try again */
3089 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
3090 goto restart;
3093 iwl3945_clear_stations_table(priv);
3095 rc = iwl_grab_nic_access(priv);
3096 if (rc) {
3097 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
3098 return;
3101 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
3102 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
3103 iwl_release_nic_access(priv);
3105 if (rfkill & 0x1) {
3106 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3107 /* if RFKILL is not on, then wait for thermal
3108 * sensor in adapter to kick in */
3109 while (iwl3945_hw_get_temperature(priv) == 0) {
3110 thermal_spin++;
3111 udelay(10);
3114 if (thermal_spin)
3115 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
3116 thermal_spin * 10);
3117 } else
3118 set_bit(STATUS_RF_KILL_HW, &priv->status);
3120 /* After the ALIVE response, we can send commands to 3945 uCode */
3121 set_bit(STATUS_ALIVE, &priv->status);
3123 /* Clear out the uCode error bit if it is set */
3124 clear_bit(STATUS_FW_ERROR, &priv->status);
3126 if (iwl_is_rfkill(priv))
3127 return;
3129 ieee80211_wake_queues(priv->hw);
3131 priv->active_rate = priv->rates_mask;
3132 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
3134 iwl_power_update_mode(priv, false);
3136 if (iwl_is_associated(priv)) {
3137 struct iwl3945_rxon_cmd *active_rxon =
3138 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
3140 memcpy(&priv->staging_rxon, &priv->active_rxon,
3141 sizeof(priv->staging_rxon));
3142 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3143 } else {
3144 /* Initialize our rx_config data */
3145 iwl_connection_init_rx_config(priv, priv->iw_mode);
3148 /* Configure Bluetooth device coexistence support */
3149 iwl_send_bt_config(priv);
3151 /* Configure the adapter for unassociated operation */
3152 iwl3945_commit_rxon(priv);
3154 iwl3945_reg_txpower_periodic(priv);
3156 iwl3945_led_register(priv);
3158 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
3159 set_bit(STATUS_READY, &priv->status);
3160 wake_up_interruptible(&priv->wait_command_queue);
3162 if (priv->error_recovering)
3163 iwl3945_error_recovery(priv);
3165 /* reassociate for ADHOC mode */
3166 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
3167 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
3168 priv->vif);
3169 if (beacon)
3170 iwl3945_mac_beacon_update(priv->hw, beacon);
3173 return;
3175 restart:
3176 queue_work(priv->workqueue, &priv->restart);
3179 static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
3181 static void __iwl3945_down(struct iwl_priv *priv)
3183 unsigned long flags;
3184 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
3185 struct ieee80211_conf *conf = NULL;
3187 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
3189 conf = ieee80211_get_hw_conf(priv->hw);
3191 if (!exit_pending)
3192 set_bit(STATUS_EXIT_PENDING, &priv->status);
3194 iwl3945_led_unregister(priv);
3195 iwl3945_clear_stations_table(priv);
3197 /* Unblock any waiting calls */
3198 wake_up_interruptible_all(&priv->wait_command_queue);
3200 /* Wipe out the EXIT_PENDING status bit if we are not actually
3201 * exiting the module */
3202 if (!exit_pending)
3203 clear_bit(STATUS_EXIT_PENDING, &priv->status);
3205 /* stop and reset the on-board processor */
3206 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
3208 /* tell the device to stop sending interrupts */
3209 spin_lock_irqsave(&priv->lock, flags);
3210 iwl3945_disable_interrupts(priv);
3211 spin_unlock_irqrestore(&priv->lock, flags);
3212 iwl_synchronize_irq(priv);
3214 if (priv->mac80211_registered)
3215 ieee80211_stop_queues(priv->hw);
3217 /* If we have not previously called iwl3945_init() then
3218 * clear all bits but the RF Kill and SUSPEND bits and return */
3219 if (!iwl_is_init(priv)) {
3220 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3221 STATUS_RF_KILL_HW |
3222 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3223 STATUS_RF_KILL_SW |
3224 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3225 STATUS_GEO_CONFIGURED |
3226 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3227 STATUS_IN_SUSPEND |
3228 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
3229 STATUS_EXIT_PENDING;
3230 goto exit;
3233 /* ...otherwise clear out all the status bits but the RF Kill and
3234 * SUSPEND bits and continue taking the NIC down. */
3235 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3236 STATUS_RF_KILL_HW |
3237 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3238 STATUS_RF_KILL_SW |
3239 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3240 STATUS_GEO_CONFIGURED |
3241 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3242 STATUS_IN_SUSPEND |
3243 test_bit(STATUS_FW_ERROR, &priv->status) <<
3244 STATUS_FW_ERROR |
3245 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
3246 STATUS_EXIT_PENDING;
3248 priv->cfg->ops->lib->apm_ops.reset(priv);
3249 spin_lock_irqsave(&priv->lock, flags);
3250 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3251 spin_unlock_irqrestore(&priv->lock, flags);
3253 iwl3945_hw_txq_ctx_stop(priv);
3254 iwl3945_hw_rxq_stop(priv);
3256 spin_lock_irqsave(&priv->lock, flags);
3257 if (!iwl_grab_nic_access(priv)) {
3258 iwl_write_prph(priv, APMG_CLK_DIS_REG,
3259 APMG_CLK_VAL_DMA_CLK_RQT);
3260 iwl_release_nic_access(priv);
3262 spin_unlock_irqrestore(&priv->lock, flags);
3264 udelay(5);
3266 if (exit_pending || test_bit(STATUS_IN_SUSPEND, &priv->status))
3267 priv->cfg->ops->lib->apm_ops.stop(priv);
3268 else
3269 priv->cfg->ops->lib->apm_ops.reset(priv);
3271 exit:
3272 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
3274 if (priv->ibss_beacon)
3275 dev_kfree_skb(priv->ibss_beacon);
3276 priv->ibss_beacon = NULL;
3278 /* clear out any free frames */
3279 iwl3945_clear_free_frames(priv);
3282 static void iwl3945_down(struct iwl_priv *priv)
3284 mutex_lock(&priv->mutex);
3285 __iwl3945_down(priv);
3286 mutex_unlock(&priv->mutex);
3288 iwl3945_cancel_deferred_work(priv);
3291 #define MAX_HW_RESTARTS 5
3293 static int __iwl3945_up(struct iwl_priv *priv)
3295 int rc, i;
3297 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3298 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
3299 return -EIO;
3302 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
3303 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
3304 "parameter)\n");
3305 return -ENODEV;
3308 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
3309 IWL_ERR(priv, "ucode not available for device bring up\n");
3310 return -EIO;
3313 /* If platform's RF_KILL switch is NOT set to KILL */
3314 if (iwl_read32(priv, CSR_GP_CNTRL) &
3315 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3316 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3317 else {
3318 set_bit(STATUS_RF_KILL_HW, &priv->status);
3319 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
3320 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
3321 return -ENODEV;
3325 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
3327 rc = iwl3945_hw_nic_init(priv);
3328 if (rc) {
3329 IWL_ERR(priv, "Unable to int nic\n");
3330 return rc;
3333 /* make sure rfkill handshake bits are cleared */
3334 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3335 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
3336 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3338 /* clear (again), then enable host interrupts */
3339 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
3340 iwl3945_enable_interrupts(priv);
3342 /* really make sure rfkill handshake bits are cleared */
3343 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3344 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3346 /* Copy original ucode data image from disk into backup cache.
3347 * This will be used to initialize the on-board processor's
3348 * data SRAM for a clean start when the runtime program first loads. */
3349 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
3350 priv->ucode_data.len);
3352 /* We return success when we resume from suspend and rf_kill is on. */
3353 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
3354 return 0;
3356 for (i = 0; i < MAX_HW_RESTARTS; i++) {
3358 iwl3945_clear_stations_table(priv);
3360 /* load bootstrap state machine,
3361 * load bootstrap program into processor's memory,
3362 * prepare to load the "initialize" uCode */
3363 priv->cfg->ops->lib->load_ucode(priv);
3365 if (rc) {
3366 IWL_ERR(priv,
3367 "Unable to set up bootstrap uCode: %d\n", rc);
3368 continue;
3371 /* start card; "initialize" will load runtime ucode */
3372 iwl3945_nic_start(priv);
3374 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
3376 return 0;
3379 set_bit(STATUS_EXIT_PENDING, &priv->status);
3380 __iwl3945_down(priv);
3381 clear_bit(STATUS_EXIT_PENDING, &priv->status);
3383 /* tried to restart and config the device for as long as our
3384 * patience could withstand */
3385 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
3386 return -EIO;
3390 /*****************************************************************************
3392 * Workqueue callbacks
3394 *****************************************************************************/
3396 static void iwl3945_bg_init_alive_start(struct work_struct *data)
3398 struct iwl_priv *priv =
3399 container_of(data, struct iwl_priv, init_alive_start.work);
3401 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3402 return;
3404 mutex_lock(&priv->mutex);
3405 iwl3945_init_alive_start(priv);
3406 mutex_unlock(&priv->mutex);
3409 static void iwl3945_bg_alive_start(struct work_struct *data)
3411 struct iwl_priv *priv =
3412 container_of(data, struct iwl_priv, alive_start.work);
3414 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3415 return;
3417 mutex_lock(&priv->mutex);
3418 iwl3945_alive_start(priv);
3419 mutex_unlock(&priv->mutex);
3422 static void iwl3945_rfkill_poll(struct work_struct *data)
3424 struct iwl_priv *priv =
3425 container_of(data, struct iwl_priv, rfkill_poll.work);
3426 unsigned long status = priv->status;
3428 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3429 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3430 else
3431 set_bit(STATUS_RF_KILL_HW, &priv->status);
3433 if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
3434 queue_work(priv->workqueue, &priv->rf_kill);
3436 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3437 round_jiffies_relative(2 * HZ));
3441 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
3442 static void iwl3945_bg_request_scan(struct work_struct *data)
3444 struct iwl_priv *priv =
3445 container_of(data, struct iwl_priv, request_scan);
3446 struct iwl_host_cmd cmd = {
3447 .id = REPLY_SCAN_CMD,
3448 .len = sizeof(struct iwl3945_scan_cmd),
3449 .meta.flags = CMD_SIZE_HUGE,
3451 int rc = 0;
3452 struct iwl3945_scan_cmd *scan;
3453 struct ieee80211_conf *conf = NULL;
3454 u8 n_probes = 2;
3455 enum ieee80211_band band;
3456 DECLARE_SSID_BUF(ssid);
3458 conf = ieee80211_get_hw_conf(priv->hw);
3460 mutex_lock(&priv->mutex);
3462 if (!iwl_is_ready(priv)) {
3463 IWL_WARN(priv, "request scan called when driver not ready.\n");
3464 goto done;
3467 /* Make sure the scan wasn't canceled before this queued work
3468 * was given the chance to run... */
3469 if (!test_bit(STATUS_SCANNING, &priv->status))
3470 goto done;
3472 /* This should never be called or scheduled if there is currently
3473 * a scan active in the hardware. */
3474 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
3475 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
3476 "Ignoring second request.\n");
3477 rc = -EIO;
3478 goto done;
3481 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3482 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
3483 goto done;
3486 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3487 IWL_DEBUG_HC(priv,
3488 "Scan request while abort pending. Queuing.\n");
3489 goto done;
3492 if (iwl_is_rfkill(priv)) {
3493 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
3494 goto done;
3497 if (!test_bit(STATUS_READY, &priv->status)) {
3498 IWL_DEBUG_HC(priv,
3499 "Scan request while uninitialized. Queuing.\n");
3500 goto done;
3503 if (!priv->scan_bands) {
3504 IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
3505 goto done;
3508 if (!priv->scan) {
3509 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
3510 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
3511 if (!priv->scan) {
3512 rc = -ENOMEM;
3513 goto done;
3516 scan = priv->scan;
3517 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
3519 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3520 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3522 if (iwl_is_associated(priv)) {
3523 u16 interval = 0;
3524 u32 extra;
3525 u32 suspend_time = 100;
3526 u32 scan_suspend_time = 100;
3527 unsigned long flags;
3529 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
3531 spin_lock_irqsave(&priv->lock, flags);
3532 interval = priv->beacon_int;
3533 spin_unlock_irqrestore(&priv->lock, flags);
3535 scan->suspend_time = 0;
3536 scan->max_out_time = cpu_to_le32(200 * 1024);
3537 if (!interval)
3538 interval = suspend_time;
3540 * suspend time format:
3541 * 0-19: beacon interval in usec (time before exec.)
3542 * 20-23: 0
3543 * 24-31: number of beacons (suspend between channels)
3546 extra = (suspend_time / interval) << 24;
3547 scan_suspend_time = 0xFF0FFFFF &
3548 (extra | ((suspend_time % interval) * 1024));
3550 scan->suspend_time = cpu_to_le32(scan_suspend_time);
3551 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
3552 scan_suspend_time, interval);
3555 /* We should add the ability for user to lock to PASSIVE ONLY */
3556 if (priv->one_direct_scan) {
3557 IWL_DEBUG_SCAN(priv, "Kicking off one direct scan for '%s'\n",
3558 print_ssid(ssid, priv->direct_ssid,
3559 priv->direct_ssid_len));
3560 scan->direct_scan[0].id = WLAN_EID_SSID;
3561 scan->direct_scan[0].len = priv->direct_ssid_len;
3562 memcpy(scan->direct_scan[0].ssid,
3563 priv->direct_ssid, priv->direct_ssid_len);
3564 n_probes++;
3565 } else
3566 IWL_DEBUG_SCAN(priv, "Kicking off one indirect scan.\n");
3568 /* We don't build a direct scan probe request; the uCode will do
3569 * that based on the direct_mask added to each channel entry */
3570 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
3571 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
3572 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3574 /* flags + rate selection */
3576 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
3577 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3578 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
3579 scan->good_CRC_th = 0;
3580 band = IEEE80211_BAND_2GHZ;
3581 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
3582 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
3583 scan->good_CRC_th = IWL_GOOD_CRC_TH;
3584 band = IEEE80211_BAND_5GHZ;
3585 } else {
3586 IWL_WARN(priv, "Invalid scan band count\n");
3587 goto done;
3590 scan->tx_cmd.len = cpu_to_le16(
3591 iwl_fill_probe_req(priv, band,
3592 (struct ieee80211_mgmt *)scan->data,
3593 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
3595 /* select Rx antennas */
3596 scan->flags |= iwl3945_get_antenna_flags(priv);
3598 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
3599 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3601 scan->channel_count =
3602 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
3603 n_probes,
3604 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
3606 if (scan->channel_count == 0) {
3607 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
3608 goto done;
3611 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
3612 scan->channel_count * sizeof(struct iwl3945_scan_channel);
3613 cmd.data = scan;
3614 scan->len = cpu_to_le16(cmd.len);
3616 set_bit(STATUS_SCAN_HW, &priv->status);
3617 rc = iwl_send_cmd_sync(priv, &cmd);
3618 if (rc)
3619 goto done;
3621 queue_delayed_work(priv->workqueue, &priv->scan_check,
3622 IWL_SCAN_CHECK_WATCHDOG);
3624 mutex_unlock(&priv->mutex);
3625 return;
3627 done:
3628 /* can not perform scan make sure we clear scanning
3629 * bits from status so next scan request can be performed.
3630 * if we dont clear scanning status bit here all next scan
3631 * will fail
3633 clear_bit(STATUS_SCAN_HW, &priv->status);
3634 clear_bit(STATUS_SCANNING, &priv->status);
3636 /* inform mac80211 scan aborted */
3637 queue_work(priv->workqueue, &priv->scan_completed);
3638 mutex_unlock(&priv->mutex);
3641 static void iwl3945_bg_up(struct work_struct *data)
3643 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
3645 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3646 return;
3648 mutex_lock(&priv->mutex);
3649 __iwl3945_up(priv);
3650 mutex_unlock(&priv->mutex);
3651 iwl_rfkill_set_hw_state(priv);
3654 static void iwl3945_bg_restart(struct work_struct *data)
3656 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
3658 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3659 return;
3661 iwl3945_down(priv);
3662 queue_work(priv->workqueue, &priv->up);
3665 static void iwl3945_bg_rx_replenish(struct work_struct *data)
3667 struct iwl_priv *priv =
3668 container_of(data, struct iwl_priv, rx_replenish);
3670 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3671 return;
3673 mutex_lock(&priv->mutex);
3674 iwl3945_rx_replenish(priv);
3675 mutex_unlock(&priv->mutex);
3678 #define IWL_DELAY_NEXT_SCAN (HZ*2)
3680 static void iwl3945_post_associate(struct iwl_priv *priv)
3682 int rc = 0;
3683 struct ieee80211_conf *conf = NULL;
3685 if (priv->iw_mode == NL80211_IFTYPE_AP) {
3686 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
3687 return;
3691 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
3692 priv->assoc_id, priv->active_rxon.bssid_addr);
3694 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3695 return;
3697 if (!priv->vif || !priv->is_open)
3698 return;
3700 iwl_scan_cancel_timeout(priv, 200);
3702 conf = ieee80211_get_hw_conf(priv->hw);
3704 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3705 iwl3945_commit_rxon(priv);
3707 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
3708 iwl3945_setup_rxon_timing(priv);
3709 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3710 sizeof(priv->rxon_timing), &priv->rxon_timing);
3711 if (rc)
3712 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
3713 "Attempting to continue.\n");
3715 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3717 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3719 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
3720 priv->assoc_id, priv->beacon_int);
3722 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3723 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3724 else
3725 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3727 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3728 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
3729 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
3730 else
3731 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3733 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
3734 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3738 iwl3945_commit_rxon(priv);
3740 switch (priv->iw_mode) {
3741 case NL80211_IFTYPE_STATION:
3742 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
3743 break;
3745 case NL80211_IFTYPE_ADHOC:
3747 priv->assoc_id = 1;
3748 iwl3945_add_station(priv, priv->bssid, 0, 0);
3749 iwl3945_sync_sta(priv, IWL_STA_ID,
3750 (priv->band == IEEE80211_BAND_5GHZ) ?
3751 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
3752 CMD_ASYNC);
3753 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
3754 iwl3945_send_beacon_cmd(priv);
3756 break;
3758 default:
3759 IWL_ERR(priv, "%s Should not be called in %d mode\n",
3760 __func__, priv->iw_mode);
3761 break;
3764 iwl3945_activate_qos(priv, 0);
3766 /* we have just associated, don't start scan too early */
3767 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
3770 static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
3772 /*****************************************************************************
3774 * mac80211 entry point functions
3776 *****************************************************************************/
3778 #define UCODE_READY_TIMEOUT (2 * HZ)
3780 static int iwl3945_mac_start(struct ieee80211_hw *hw)
3782 struct iwl_priv *priv = hw->priv;
3783 int ret;
3785 IWL_DEBUG_MAC80211(priv, "enter\n");
3787 /* we should be verifying the device is ready to be opened */
3788 mutex_lock(&priv->mutex);
3790 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
3791 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3792 * ucode filename and max sizes are card-specific. */
3794 if (!priv->ucode_code.len) {
3795 ret = iwl3945_read_ucode(priv);
3796 if (ret) {
3797 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
3798 mutex_unlock(&priv->mutex);
3799 goto out_release_irq;
3803 ret = __iwl3945_up(priv);
3805 mutex_unlock(&priv->mutex);
3807 iwl_rfkill_set_hw_state(priv);
3809 if (ret)
3810 goto out_release_irq;
3812 IWL_DEBUG_INFO(priv, "Start UP work.\n");
3814 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
3815 return 0;
3817 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3818 * mac80211 will not be run successfully. */
3819 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3820 test_bit(STATUS_READY, &priv->status),
3821 UCODE_READY_TIMEOUT);
3822 if (!ret) {
3823 if (!test_bit(STATUS_READY, &priv->status)) {
3824 IWL_ERR(priv,
3825 "Wait for START_ALIVE timeout after %dms.\n",
3826 jiffies_to_msecs(UCODE_READY_TIMEOUT));
3827 ret = -ETIMEDOUT;
3828 goto out_release_irq;
3832 /* ucode is running and will send rfkill notifications,
3833 * no need to poll the killswitch state anymore */
3834 cancel_delayed_work(&priv->rfkill_poll);
3836 priv->is_open = 1;
3837 IWL_DEBUG_MAC80211(priv, "leave\n");
3838 return 0;
3840 out_release_irq:
3841 priv->is_open = 0;
3842 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
3843 return ret;
3846 static void iwl3945_mac_stop(struct ieee80211_hw *hw)
3848 struct iwl_priv *priv = hw->priv;
3850 IWL_DEBUG_MAC80211(priv, "enter\n");
3852 if (!priv->is_open) {
3853 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
3854 return;
3857 priv->is_open = 0;
3859 if (iwl_is_ready_rf(priv)) {
3860 /* stop mac, cancel any scan request and clear
3861 * RXON_FILTER_ASSOC_MSK BIT
3863 mutex_lock(&priv->mutex);
3864 iwl_scan_cancel_timeout(priv, 100);
3865 mutex_unlock(&priv->mutex);
3868 iwl3945_down(priv);
3870 flush_workqueue(priv->workqueue);
3872 /* start polling the killswitch state again */
3873 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3874 round_jiffies_relative(2 * HZ));
3876 IWL_DEBUG_MAC80211(priv, "leave\n");
3879 static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3881 struct iwl_priv *priv = hw->priv;
3883 IWL_DEBUG_MAC80211(priv, "enter\n");
3885 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
3886 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
3888 if (iwl3945_tx_skb(priv, skb))
3889 dev_kfree_skb_any(skb);
3891 IWL_DEBUG_MAC80211(priv, "leave\n");
3892 return NETDEV_TX_OK;
3895 static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
3896 struct ieee80211_if_init_conf *conf)
3898 struct iwl_priv *priv = hw->priv;
3899 unsigned long flags;
3901 IWL_DEBUG_MAC80211(priv, "enter: type %d\n", conf->type);
3903 if (priv->vif) {
3904 IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
3905 return -EOPNOTSUPP;
3908 spin_lock_irqsave(&priv->lock, flags);
3909 priv->vif = conf->vif;
3910 priv->iw_mode = conf->type;
3912 spin_unlock_irqrestore(&priv->lock, flags);
3914 mutex_lock(&priv->mutex);
3916 if (conf->mac_addr) {
3917 IWL_DEBUG_MAC80211(priv, "Set: %pM\n", conf->mac_addr);
3918 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
3921 if (iwl_is_ready(priv))
3922 iwl3945_set_mode(priv, conf->type);
3924 mutex_unlock(&priv->mutex);
3926 IWL_DEBUG_MAC80211(priv, "leave\n");
3927 return 0;
3931 * iwl3945_mac_config - mac80211 config callback
3933 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
3934 * be set inappropriately and the driver currently sets the hardware up to
3935 * use it whenever needed.
3937 static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
3939 struct iwl_priv *priv = hw->priv;
3940 const struct iwl_channel_info *ch_info;
3941 struct ieee80211_conf *conf = &hw->conf;
3942 unsigned long flags;
3943 int ret = 0;
3945 mutex_lock(&priv->mutex);
3946 IWL_DEBUG_MAC80211(priv, "enter to channel %d\n",
3947 conf->channel->hw_value);
3949 if (!iwl_is_ready(priv)) {
3950 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
3951 ret = -EIO;
3952 goto out;
3955 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
3956 test_bit(STATUS_SCANNING, &priv->status))) {
3957 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
3958 set_bit(STATUS_CONF_PENDING, &priv->status);
3959 mutex_unlock(&priv->mutex);
3960 return 0;
3963 spin_lock_irqsave(&priv->lock, flags);
3965 ch_info = iwl_get_channel_info(priv, conf->channel->band,
3966 conf->channel->hw_value);
3967 if (!is_channel_valid(ch_info)) {
3968 IWL_DEBUG_SCAN(priv,
3969 "Channel %d [%d] is INVALID for this band.\n",
3970 conf->channel->hw_value, conf->channel->band);
3971 IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
3972 spin_unlock_irqrestore(&priv->lock, flags);
3973 ret = -EINVAL;
3974 goto out;
3977 iwl_set_rxon_channel(priv, conf->channel);
3979 iwl_set_flags_for_band(priv, conf->channel->band);
3981 /* The list of supported rates and rate mask can be different
3982 * for each phymode; since the phymode may have changed, reset
3983 * the rate mask to what mac80211 lists */
3984 iwl_set_rate(priv);
3986 spin_unlock_irqrestore(&priv->lock, flags);
3988 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
3989 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
3990 iwl3945_hw_channel_switch(priv, conf->channel);
3991 goto out;
3993 #endif
3995 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
3997 if (!conf->radio_enabled) {
3998 IWL_DEBUG_MAC80211(priv, "leave - radio disabled\n");
3999 goto out;
4002 if (iwl_is_rfkill(priv)) {
4003 IWL_DEBUG_MAC80211(priv, "leave - RF kill\n");
4004 ret = -EIO;
4005 goto out;
4008 iwl_set_rate(priv);
4010 if (memcmp(&priv->active_rxon,
4011 &priv->staging_rxon, sizeof(priv->staging_rxon)))
4012 iwl3945_commit_rxon(priv);
4013 else
4014 IWL_DEBUG_INFO(priv, "Not re-sending same RXON configuration\n");
4016 IWL_DEBUG_MAC80211(priv, "leave\n");
4018 out:
4019 clear_bit(STATUS_CONF_PENDING, &priv->status);
4020 mutex_unlock(&priv->mutex);
4021 return ret;
4024 static void iwl3945_config_ap(struct iwl_priv *priv)
4026 int rc = 0;
4028 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4029 return;
4031 /* The following should be done only at AP bring up */
4032 if (!(iwl_is_associated(priv))) {
4034 /* RXON - unassoc (to set timing command) */
4035 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4036 iwl3945_commit_rxon(priv);
4038 /* RXON Timing */
4039 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
4040 iwl3945_setup_rxon_timing(priv);
4041 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
4042 sizeof(priv->rxon_timing),
4043 &priv->rxon_timing);
4044 if (rc)
4045 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
4046 "Attempting to continue.\n");
4048 /* FIXME: what should be the assoc_id for AP? */
4049 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4050 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4051 priv->staging_rxon.flags |=
4052 RXON_FLG_SHORT_PREAMBLE_MSK;
4053 else
4054 priv->staging_rxon.flags &=
4055 ~RXON_FLG_SHORT_PREAMBLE_MSK;
4057 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4058 if (priv->assoc_capability &
4059 WLAN_CAPABILITY_SHORT_SLOT_TIME)
4060 priv->staging_rxon.flags |=
4061 RXON_FLG_SHORT_SLOT_MSK;
4062 else
4063 priv->staging_rxon.flags &=
4064 ~RXON_FLG_SHORT_SLOT_MSK;
4066 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
4067 priv->staging_rxon.flags &=
4068 ~RXON_FLG_SHORT_SLOT_MSK;
4070 /* restore RXON assoc */
4071 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
4072 iwl3945_commit_rxon(priv);
4073 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
4075 iwl3945_send_beacon_cmd(priv);
4077 /* FIXME - we need to add code here to detect a totally new
4078 * configuration, reset the AP, unassoc, rxon timing, assoc,
4079 * clear sta table, add BCAST sta... */
4082 static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
4083 struct ieee80211_vif *vif,
4084 struct ieee80211_if_conf *conf)
4086 struct iwl_priv *priv = hw->priv;
4087 int rc;
4089 if (conf == NULL)
4090 return -EIO;
4092 if (priv->vif != vif) {
4093 IWL_DEBUG_MAC80211(priv, "leave - priv->vif != vif\n");
4094 return 0;
4097 /* handle this temporarily here */
4098 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
4099 conf->changed & IEEE80211_IFCC_BEACON) {
4100 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
4101 if (!beacon)
4102 return -ENOMEM;
4103 mutex_lock(&priv->mutex);
4104 rc = iwl3945_mac_beacon_update(hw, beacon);
4105 mutex_unlock(&priv->mutex);
4106 if (rc)
4107 return rc;
4110 if (!iwl_is_alive(priv))
4111 return -EAGAIN;
4113 mutex_lock(&priv->mutex);
4115 if (conf->bssid)
4116 IWL_DEBUG_MAC80211(priv, "bssid: %pM\n", conf->bssid);
4119 * very dubious code was here; the probe filtering flag is never set:
4121 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
4122 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
4125 if (priv->iw_mode == NL80211_IFTYPE_AP) {
4126 if (!conf->bssid) {
4127 conf->bssid = priv->mac_addr;
4128 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
4129 IWL_DEBUG_MAC80211(priv, "bssid was set to: %pM\n",
4130 conf->bssid);
4132 if (priv->ibss_beacon)
4133 dev_kfree_skb(priv->ibss_beacon);
4135 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
4138 if (iwl_is_rfkill(priv))
4139 goto done;
4141 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
4142 !is_multicast_ether_addr(conf->bssid)) {
4143 /* If there is currently a HW scan going on in the background
4144 * then we need to cancel it else the RXON below will fail. */
4145 if (iwl_scan_cancel_timeout(priv, 100)) {
4146 IWL_WARN(priv, "Aborted scan still in progress "
4147 "after 100ms\n");
4148 IWL_DEBUG_MAC80211(priv, "leaving:scan abort failed\n");
4149 mutex_unlock(&priv->mutex);
4150 return -EAGAIN;
4152 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
4154 /* TODO: Audit driver for usage of these members and see
4155 * if mac80211 deprecates them (priv->bssid looks like it
4156 * shouldn't be there, but I haven't scanned the IBSS code
4157 * to verify) - jpk */
4158 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
4160 if (priv->iw_mode == NL80211_IFTYPE_AP)
4161 iwl3945_config_ap(priv);
4162 else {
4163 rc = iwl3945_commit_rxon(priv);
4164 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
4165 iwl3945_add_station(priv,
4166 priv->active_rxon.bssid_addr, 1, 0);
4169 } else {
4170 iwl_scan_cancel_timeout(priv, 100);
4171 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4172 iwl3945_commit_rxon(priv);
4175 done:
4176 IWL_DEBUG_MAC80211(priv, "leave\n");
4177 mutex_unlock(&priv->mutex);
4179 return 0;
4182 static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
4183 struct ieee80211_if_init_conf *conf)
4185 struct iwl_priv *priv = hw->priv;
4187 IWL_DEBUG_MAC80211(priv, "enter\n");
4189 mutex_lock(&priv->mutex);
4191 if (iwl_is_ready_rf(priv)) {
4192 iwl_scan_cancel_timeout(priv, 100);
4193 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4194 iwl3945_commit_rxon(priv);
4196 if (priv->vif == conf->vif) {
4197 priv->vif = NULL;
4198 memset(priv->bssid, 0, ETH_ALEN);
4200 mutex_unlock(&priv->mutex);
4202 IWL_DEBUG_MAC80211(priv, "leave\n");
4205 #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
4207 static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
4208 struct ieee80211_vif *vif,
4209 struct ieee80211_bss_conf *bss_conf,
4210 u32 changes)
4212 struct iwl_priv *priv = hw->priv;
4214 IWL_DEBUG_MAC80211(priv, "changes = 0x%X\n", changes);
4216 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
4217 IWL_DEBUG_MAC80211(priv, "ERP_PREAMBLE %d\n",
4218 bss_conf->use_short_preamble);
4219 if (bss_conf->use_short_preamble)
4220 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4221 else
4222 priv->staging_rxon.flags &=
4223 ~RXON_FLG_SHORT_PREAMBLE_MSK;
4226 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
4227 IWL_DEBUG_MAC80211(priv, "ERP_CTS %d\n",
4228 bss_conf->use_cts_prot);
4229 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
4230 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
4231 else
4232 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
4235 if (changes & BSS_CHANGED_ASSOC) {
4236 IWL_DEBUG_MAC80211(priv, "ASSOC %d\n", bss_conf->assoc);
4237 /* This should never happen as this function should
4238 * never be called from interrupt context. */
4239 if (WARN_ON_ONCE(in_interrupt()))
4240 return;
4241 if (bss_conf->assoc) {
4242 priv->assoc_id = bss_conf->aid;
4243 priv->beacon_int = bss_conf->beacon_int;
4244 priv->timestamp = bss_conf->timestamp;
4245 priv->assoc_capability = bss_conf->assoc_capability;
4246 priv->power_data.dtim_period = bss_conf->dtim_period;
4247 priv->next_scan_jiffies = jiffies +
4248 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
4249 mutex_lock(&priv->mutex);
4250 iwl3945_post_associate(priv);
4251 mutex_unlock(&priv->mutex);
4252 } else {
4253 priv->assoc_id = 0;
4254 IWL_DEBUG_MAC80211(priv,
4255 "DISASSOC %d\n", bss_conf->assoc);
4257 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
4258 IWL_DEBUG_MAC80211(priv,
4259 "Associated Changes %d\n", changes);
4260 iwl3945_send_rxon_assoc(priv);
4265 static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4266 struct ieee80211_vif *vif,
4267 struct ieee80211_sta *sta,
4268 struct ieee80211_key_conf *key)
4270 struct iwl_priv *priv = hw->priv;
4271 const u8 *addr;
4272 int ret;
4273 u8 sta_id;
4275 IWL_DEBUG_MAC80211(priv, "enter\n");
4277 if (iwl3945_mod_params.sw_crypto) {
4278 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
4279 return -EOPNOTSUPP;
4282 addr = sta ? sta->addr : iwl_bcast_addr;
4283 sta_id = iwl3945_hw_find_station(priv, addr);
4284 if (sta_id == IWL_INVALID_STATION) {
4285 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
4286 addr);
4287 return -EINVAL;
4290 mutex_lock(&priv->mutex);
4292 iwl_scan_cancel_timeout(priv, 100);
4294 switch (cmd) {
4295 case SET_KEY:
4296 ret = iwl3945_update_sta_key_info(priv, key, sta_id);
4297 if (!ret) {
4298 iwl_set_rxon_hwcrypto(priv, 1);
4299 iwl3945_commit_rxon(priv);
4300 key->hw_key_idx = sta_id;
4301 IWL_DEBUG_MAC80211(priv,
4302 "set_key success, using hwcrypto\n");
4303 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
4305 break;
4306 case DISABLE_KEY:
4307 ret = iwl3945_clear_sta_key_info(priv, sta_id);
4308 if (!ret) {
4309 iwl_set_rxon_hwcrypto(priv, 0);
4310 iwl3945_commit_rxon(priv);
4311 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
4313 break;
4314 default:
4315 ret = -EINVAL;
4318 IWL_DEBUG_MAC80211(priv, "leave\n");
4319 mutex_unlock(&priv->mutex);
4321 return ret;
4324 static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
4325 const struct ieee80211_tx_queue_params *params)
4327 struct iwl_priv *priv = hw->priv;
4328 unsigned long flags;
4329 int q;
4331 IWL_DEBUG_MAC80211(priv, "enter\n");
4333 if (!iwl_is_ready_rf(priv)) {
4334 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
4335 return -EIO;
4338 if (queue >= AC_NUM) {
4339 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
4340 return 0;
4343 q = AC_NUM - 1 - queue;
4345 spin_lock_irqsave(&priv->lock, flags);
4347 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
4348 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
4349 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
4350 priv->qos_data.def_qos_parm.ac[q].edca_txop =
4351 cpu_to_le16((params->txop * 32));
4353 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
4354 priv->qos_data.qos_active = 1;
4356 spin_unlock_irqrestore(&priv->lock, flags);
4358 mutex_lock(&priv->mutex);
4359 if (priv->iw_mode == NL80211_IFTYPE_AP)
4360 iwl3945_activate_qos(priv, 1);
4361 else if (priv->assoc_id && iwl_is_associated(priv))
4362 iwl3945_activate_qos(priv, 0);
4364 mutex_unlock(&priv->mutex);
4366 IWL_DEBUG_MAC80211(priv, "leave\n");
4367 return 0;
4370 static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
4371 struct ieee80211_tx_queue_stats *stats)
4373 struct iwl_priv *priv = hw->priv;
4374 int i, avail;
4375 struct iwl_tx_queue *txq;
4376 struct iwl_queue *q;
4377 unsigned long flags;
4379 IWL_DEBUG_MAC80211(priv, "enter\n");
4381 if (!iwl_is_ready_rf(priv)) {
4382 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
4383 return -EIO;
4386 spin_lock_irqsave(&priv->lock, flags);
4388 for (i = 0; i < AC_NUM; i++) {
4389 txq = &priv->txq[i];
4390 q = &txq->q;
4391 avail = iwl_queue_space(q);
4393 stats[i].len = q->n_window - avail;
4394 stats[i].limit = q->n_window - q->high_mark;
4395 stats[i].count = q->n_window;
4398 spin_unlock_irqrestore(&priv->lock, flags);
4400 IWL_DEBUG_MAC80211(priv, "leave\n");
4402 return 0;
4405 static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
4407 struct iwl_priv *priv = hw->priv;
4408 unsigned long flags;
4410 mutex_lock(&priv->mutex);
4411 IWL_DEBUG_MAC80211(priv, "enter\n");
4413 iwl_reset_qos(priv);
4415 spin_lock_irqsave(&priv->lock, flags);
4416 priv->assoc_id = 0;
4417 priv->assoc_capability = 0;
4419 /* new association get rid of ibss beacon skb */
4420 if (priv->ibss_beacon)
4421 dev_kfree_skb(priv->ibss_beacon);
4423 priv->ibss_beacon = NULL;
4425 priv->beacon_int = priv->hw->conf.beacon_int;
4426 priv->timestamp = 0;
4427 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
4428 priv->beacon_int = 0;
4430 spin_unlock_irqrestore(&priv->lock, flags);
4432 if (!iwl_is_ready_rf(priv)) {
4433 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
4434 mutex_unlock(&priv->mutex);
4435 return;
4438 /* we are restarting association process
4439 * clear RXON_FILTER_ASSOC_MSK bit
4441 if (priv->iw_mode != NL80211_IFTYPE_AP) {
4442 iwl_scan_cancel_timeout(priv, 100);
4443 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4444 iwl3945_commit_rxon(priv);
4447 /* Per mac80211.h: This is only used in IBSS mode... */
4448 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
4450 IWL_DEBUG_MAC80211(priv, "leave - not in IBSS\n");
4451 mutex_unlock(&priv->mutex);
4452 return;
4455 iwl_set_rate(priv);
4457 mutex_unlock(&priv->mutex);
4459 IWL_DEBUG_MAC80211(priv, "leave\n");
4463 static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
4465 struct iwl_priv *priv = hw->priv;
4466 unsigned long flags;
4468 IWL_DEBUG_MAC80211(priv, "enter\n");
4470 if (!iwl_is_ready_rf(priv)) {
4471 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
4472 return -EIO;
4475 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
4476 IWL_DEBUG_MAC80211(priv, "leave - not IBSS\n");
4477 return -EIO;
4480 spin_lock_irqsave(&priv->lock, flags);
4482 if (priv->ibss_beacon)
4483 dev_kfree_skb(priv->ibss_beacon);
4485 priv->ibss_beacon = skb;
4487 priv->assoc_id = 0;
4489 IWL_DEBUG_MAC80211(priv, "leave\n");
4490 spin_unlock_irqrestore(&priv->lock, flags);
4492 iwl_reset_qos(priv);
4494 iwl3945_post_associate(priv);
4497 return 0;
4500 /*****************************************************************************
4502 * sysfs attributes
4504 *****************************************************************************/
4506 #ifdef CONFIG_IWLWIFI_DEBUG
4509 * The following adds a new attribute to the sysfs representation
4510 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
4511 * used for controlling the debug level.
4513 * See the level definitions in iwl for details.
4515 static ssize_t show_debug_level(struct device *d,
4516 struct device_attribute *attr, char *buf)
4518 struct iwl_priv *priv = d->driver_data;
4520 return sprintf(buf, "0x%08X\n", priv->debug_level);
4522 static ssize_t store_debug_level(struct device *d,
4523 struct device_attribute *attr,
4524 const char *buf, size_t count)
4526 struct iwl_priv *priv = d->driver_data;
4527 unsigned long val;
4528 int ret;
4530 ret = strict_strtoul(buf, 0, &val);
4531 if (ret)
4532 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
4533 else
4534 priv->debug_level = val;
4536 return strnlen(buf, count);
4539 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
4540 show_debug_level, store_debug_level);
4542 #endif /* CONFIG_IWLWIFI_DEBUG */
4544 static ssize_t show_temperature(struct device *d,
4545 struct device_attribute *attr, char *buf)
4547 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4549 if (!iwl_is_alive(priv))
4550 return -EAGAIN;
4552 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
4555 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
4557 static ssize_t show_tx_power(struct device *d,
4558 struct device_attribute *attr, char *buf)
4560 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4561 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
4564 static ssize_t store_tx_power(struct device *d,
4565 struct device_attribute *attr,
4566 const char *buf, size_t count)
4568 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4569 char *p = (char *)buf;
4570 u32 val;
4572 val = simple_strtoul(p, &p, 10);
4573 if (p == buf)
4574 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
4575 else
4576 iwl3945_hw_reg_set_txpower(priv, val);
4578 return count;
4581 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
4583 static ssize_t show_flags(struct device *d,
4584 struct device_attribute *attr, char *buf)
4586 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4588 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
4591 static ssize_t store_flags(struct device *d,
4592 struct device_attribute *attr,
4593 const char *buf, size_t count)
4595 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4596 u32 flags = simple_strtoul(buf, NULL, 0);
4598 mutex_lock(&priv->mutex);
4599 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
4600 /* Cancel any currently running scans... */
4601 if (iwl_scan_cancel_timeout(priv, 100))
4602 IWL_WARN(priv, "Could not cancel scan.\n");
4603 else {
4604 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
4605 flags);
4606 priv->staging_rxon.flags = cpu_to_le32(flags);
4607 iwl3945_commit_rxon(priv);
4610 mutex_unlock(&priv->mutex);
4612 return count;
4615 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
4617 static ssize_t show_filter_flags(struct device *d,
4618 struct device_attribute *attr, char *buf)
4620 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4622 return sprintf(buf, "0x%04X\n",
4623 le32_to_cpu(priv->active_rxon.filter_flags));
4626 static ssize_t store_filter_flags(struct device *d,
4627 struct device_attribute *attr,
4628 const char *buf, size_t count)
4630 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4631 u32 filter_flags = simple_strtoul(buf, NULL, 0);
4633 mutex_lock(&priv->mutex);
4634 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
4635 /* Cancel any currently running scans... */
4636 if (iwl_scan_cancel_timeout(priv, 100))
4637 IWL_WARN(priv, "Could not cancel scan.\n");
4638 else {
4639 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
4640 "0x%04X\n", filter_flags);
4641 priv->staging_rxon.filter_flags =
4642 cpu_to_le32(filter_flags);
4643 iwl3945_commit_rxon(priv);
4646 mutex_unlock(&priv->mutex);
4648 return count;
4651 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
4652 store_filter_flags);
4654 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
4656 static ssize_t show_measurement(struct device *d,
4657 struct device_attribute *attr, char *buf)
4659 struct iwl_priv *priv = dev_get_drvdata(d);
4660 struct iwl_spectrum_notification measure_report;
4661 u32 size = sizeof(measure_report), len = 0, ofs = 0;
4662 u8 *data = (u8 *)&measure_report;
4663 unsigned long flags;
4665 spin_lock_irqsave(&priv->lock, flags);
4666 if (!(priv->measurement_status & MEASUREMENT_READY)) {
4667 spin_unlock_irqrestore(&priv->lock, flags);
4668 return 0;
4670 memcpy(&measure_report, &priv->measure_report, size);
4671 priv->measurement_status = 0;
4672 spin_unlock_irqrestore(&priv->lock, flags);
4674 while (size && (PAGE_SIZE - len)) {
4675 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4676 PAGE_SIZE - len, 1);
4677 len = strlen(buf);
4678 if (PAGE_SIZE - len)
4679 buf[len++] = '\n';
4681 ofs += 16;
4682 size -= min(size, 16U);
4685 return len;
4688 static ssize_t store_measurement(struct device *d,
4689 struct device_attribute *attr,
4690 const char *buf, size_t count)
4692 struct iwl_priv *priv = dev_get_drvdata(d);
4693 struct ieee80211_measurement_params params = {
4694 .channel = le16_to_cpu(priv->active_rxon.channel),
4695 .start_time = cpu_to_le64(priv->last_tsf),
4696 .duration = cpu_to_le16(1),
4698 u8 type = IWL_MEASURE_BASIC;
4699 u8 buffer[32];
4700 u8 channel;
4702 if (count) {
4703 char *p = buffer;
4704 strncpy(buffer, buf, min(sizeof(buffer), count));
4705 channel = simple_strtoul(p, NULL, 0);
4706 if (channel)
4707 params.channel = channel;
4709 p = buffer;
4710 while (*p && *p != ' ')
4711 p++;
4712 if (*p)
4713 type = simple_strtoul(p + 1, NULL, 0);
4716 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
4717 "channel %d (for '%s')\n", type, params.channel, buf);
4718 iwl3945_get_measurement(priv, &params, type);
4720 return count;
4723 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
4724 show_measurement, store_measurement);
4725 #endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
4727 static ssize_t store_retry_rate(struct device *d,
4728 struct device_attribute *attr,
4729 const char *buf, size_t count)
4731 struct iwl_priv *priv = dev_get_drvdata(d);
4733 priv->retry_rate = simple_strtoul(buf, NULL, 0);
4734 if (priv->retry_rate <= 0)
4735 priv->retry_rate = 1;
4737 return count;
4740 static ssize_t show_retry_rate(struct device *d,
4741 struct device_attribute *attr, char *buf)
4743 struct iwl_priv *priv = dev_get_drvdata(d);
4744 return sprintf(buf, "%d", priv->retry_rate);
4747 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
4748 store_retry_rate);
4751 static ssize_t store_power_level(struct device *d,
4752 struct device_attribute *attr,
4753 const char *buf, size_t count)
4755 struct iwl_priv *priv = dev_get_drvdata(d);
4756 int ret;
4757 unsigned long mode;
4760 mutex_lock(&priv->mutex);
4762 if (!iwl_is_ready(priv)) {
4763 ret = -EAGAIN;
4764 goto out;
4767 ret = strict_strtoul(buf, 10, &mode);
4768 if (ret)
4769 goto out;
4771 ret = iwl_power_set_user_mode(priv, mode);
4772 if (ret) {
4773 IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
4774 goto out;
4776 ret = count;
4778 out:
4779 mutex_unlock(&priv->mutex);
4780 return ret;
4783 static ssize_t show_power_level(struct device *d,
4784 struct device_attribute *attr, char *buf)
4786 struct iwl_priv *priv = dev_get_drvdata(d);
4787 int mode = priv->power_data.user_power_setting;
4788 int system = priv->power_data.system_power_setting;
4789 int level = priv->power_data.power_mode;
4790 char *p = buf;
4792 switch (system) {
4793 case IWL_POWER_SYS_AUTO:
4794 p += sprintf(p, "SYSTEM:auto");
4795 break;
4796 case IWL_POWER_SYS_AC:
4797 p += sprintf(p, "SYSTEM:ac");
4798 break;
4799 case IWL_POWER_SYS_BATTERY:
4800 p += sprintf(p, "SYSTEM:battery");
4801 break;
4804 p += sprintf(p, "\tMODE:%s", (mode < IWL_POWER_AUTO) ?
4805 "fixed" : "auto");
4806 p += sprintf(p, "\tINDEX:%d", level);
4807 p += sprintf(p, "\n");
4808 return p - buf + 1;
4811 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR,
4812 show_power_level, store_power_level);
4814 #define MAX_WX_STRING 80
4816 /* Values are in microsecond */
4817 static const s32 timeout_duration[] = {
4818 350000,
4819 250000,
4820 75000,
4821 37000,
4822 25000,
4824 static const s32 period_duration[] = {
4825 400000,
4826 700000,
4827 1000000,
4828 1000000,
4829 1000000
4832 static ssize_t show_channels(struct device *d,
4833 struct device_attribute *attr, char *buf)
4835 /* all this shit doesn't belong into sysfs anyway */
4836 return 0;
4839 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4841 static ssize_t show_statistics(struct device *d,
4842 struct device_attribute *attr, char *buf)
4844 struct iwl_priv *priv = dev_get_drvdata(d);
4845 u32 size = sizeof(struct iwl3945_notif_statistics);
4846 u32 len = 0, ofs = 0;
4847 u8 *data = (u8 *)&priv->statistics_39;
4848 int rc = 0;
4850 if (!iwl_is_alive(priv))
4851 return -EAGAIN;
4853 mutex_lock(&priv->mutex);
4854 rc = iwl_send_statistics_request(priv, 0);
4855 mutex_unlock(&priv->mutex);
4857 if (rc) {
4858 len = sprintf(buf,
4859 "Error sending statistics request: 0x%08X\n", rc);
4860 return len;
4863 while (size && (PAGE_SIZE - len)) {
4864 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4865 PAGE_SIZE - len, 1);
4866 len = strlen(buf);
4867 if (PAGE_SIZE - len)
4868 buf[len++] = '\n';
4870 ofs += 16;
4871 size -= min(size, 16U);
4874 return len;
4877 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
4879 static ssize_t show_antenna(struct device *d,
4880 struct device_attribute *attr, char *buf)
4882 struct iwl_priv *priv = dev_get_drvdata(d);
4884 if (!iwl_is_alive(priv))
4885 return -EAGAIN;
4887 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
4890 static ssize_t store_antenna(struct device *d,
4891 struct device_attribute *attr,
4892 const char *buf, size_t count)
4894 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
4895 int ant;
4897 if (count == 0)
4898 return 0;
4900 if (sscanf(buf, "%1i", &ant) != 1) {
4901 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
4902 return count;
4905 if ((ant >= 0) && (ant <= 2)) {
4906 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
4907 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
4908 } else
4909 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
4912 return count;
4915 static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
4917 static ssize_t show_status(struct device *d,
4918 struct device_attribute *attr, char *buf)
4920 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4921 if (!iwl_is_alive(priv))
4922 return -EAGAIN;
4923 return sprintf(buf, "0x%08x\n", (int)priv->status);
4926 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
4928 static ssize_t dump_error_log(struct device *d,
4929 struct device_attribute *attr,
4930 const char *buf, size_t count)
4932 char *p = (char *)buf;
4934 if (p[0] == '1')
4935 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
4937 return strnlen(buf, count);
4940 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
4942 static ssize_t dump_event_log(struct device *d,
4943 struct device_attribute *attr,
4944 const char *buf, size_t count)
4946 char *p = (char *)buf;
4948 if (p[0] == '1')
4949 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
4951 return strnlen(buf, count);
4954 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
4956 /*****************************************************************************
4958 * driver setup and tear down
4960 *****************************************************************************/
4962 static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
4964 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
4966 init_waitqueue_head(&priv->wait_command_queue);
4968 INIT_WORK(&priv->up, iwl3945_bg_up);
4969 INIT_WORK(&priv->restart, iwl3945_bg_restart);
4970 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
4971 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
4972 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
4973 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
4974 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
4975 INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
4976 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
4977 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
4978 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
4979 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
4981 iwl3945_hw_setup_deferred_work(priv);
4983 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
4984 iwl3945_irq_tasklet, (unsigned long)priv);
4987 static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
4989 iwl3945_hw_cancel_deferred_work(priv);
4991 cancel_delayed_work_sync(&priv->init_alive_start);
4992 cancel_delayed_work(&priv->scan_check);
4993 cancel_delayed_work(&priv->alive_start);
4994 cancel_work_sync(&priv->beacon_update);
4997 static struct attribute *iwl3945_sysfs_entries[] = {
4998 &dev_attr_antenna.attr,
4999 &dev_attr_channels.attr,
5000 &dev_attr_dump_errors.attr,
5001 &dev_attr_dump_events.attr,
5002 &dev_attr_flags.attr,
5003 &dev_attr_filter_flags.attr,
5004 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
5005 &dev_attr_measurement.attr,
5006 #endif
5007 &dev_attr_power_level.attr,
5008 &dev_attr_retry_rate.attr,
5009 &dev_attr_statistics.attr,
5010 &dev_attr_status.attr,
5011 &dev_attr_temperature.attr,
5012 &dev_attr_tx_power.attr,
5013 #ifdef CONFIG_IWLWIFI_DEBUG
5014 &dev_attr_debug_level.attr,
5015 #endif
5016 NULL
5019 static struct attribute_group iwl3945_attribute_group = {
5020 .name = NULL, /* put in device directory */
5021 .attrs = iwl3945_sysfs_entries,
5024 static struct ieee80211_ops iwl3945_hw_ops = {
5025 .tx = iwl3945_mac_tx,
5026 .start = iwl3945_mac_start,
5027 .stop = iwl3945_mac_stop,
5028 .add_interface = iwl3945_mac_add_interface,
5029 .remove_interface = iwl3945_mac_remove_interface,
5030 .config = iwl3945_mac_config,
5031 .config_interface = iwl3945_mac_config_interface,
5032 .configure_filter = iwl_configure_filter,
5033 .set_key = iwl3945_mac_set_key,
5034 .get_tx_stats = iwl3945_mac_get_tx_stats,
5035 .conf_tx = iwl3945_mac_conf_tx,
5036 .reset_tsf = iwl3945_mac_reset_tsf,
5037 .bss_info_changed = iwl3945_bss_info_changed,
5038 .hw_scan = iwl_mac_hw_scan
5041 static int iwl3945_init_drv(struct iwl_priv *priv)
5043 int ret;
5044 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
5046 priv->retry_rate = 1;
5047 priv->ibss_beacon = NULL;
5049 spin_lock_init(&priv->lock);
5050 spin_lock_init(&priv->power_data.lock);
5051 spin_lock_init(&priv->sta_lock);
5052 spin_lock_init(&priv->hcmd_lock);
5054 INIT_LIST_HEAD(&priv->free_frames);
5056 mutex_init(&priv->mutex);
5058 /* Clear the driver's (not device's) station table */
5059 iwl3945_clear_stations_table(priv);
5061 priv->data_retry_limit = -1;
5062 priv->ieee_channels = NULL;
5063 priv->ieee_rates = NULL;
5064 priv->band = IEEE80211_BAND_2GHZ;
5066 priv->iw_mode = NL80211_IFTYPE_STATION;
5068 iwl_reset_qos(priv);
5070 priv->qos_data.qos_active = 0;
5071 priv->qos_data.qos_cap.val = 0;
5073 priv->rates_mask = IWL_RATES_MASK;
5074 /* If power management is turned on, default to CAM mode */
5075 priv->power_mode = IWL_POWER_MODE_CAM;
5076 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
5078 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
5079 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
5080 eeprom->version);
5081 ret = -EINVAL;
5082 goto err;
5084 ret = iwl_init_channel_map(priv);
5085 if (ret) {
5086 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
5087 goto err;
5090 /* Set up txpower settings in driver for all channels */
5091 if (iwl3945_txpower_set_from_eeprom(priv)) {
5092 ret = -EIO;
5093 goto err_free_channel_map;
5096 ret = iwlcore_init_geos(priv);
5097 if (ret) {
5098 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
5099 goto err_free_channel_map;
5101 iwl3945_init_hw_rates(priv, priv->ieee_rates);
5103 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
5104 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5105 &priv->bands[IEEE80211_BAND_2GHZ];
5106 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
5107 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5108 &priv->bands[IEEE80211_BAND_5GHZ];
5110 return 0;
5112 err_free_channel_map:
5113 iwl_free_channel_map(priv);
5114 err:
5115 return ret;
5118 static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
5120 int err = 0;
5121 struct iwl_priv *priv;
5122 struct ieee80211_hw *hw;
5123 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
5124 struct iwl3945_eeprom *eeprom;
5125 unsigned long flags;
5127 /***********************
5128 * 1. Allocating HW data
5129 * ********************/
5131 /* mac80211 allocates memory for this device instance, including
5132 * space for this driver's private structure */
5133 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
5134 if (hw == NULL) {
5135 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
5136 err = -ENOMEM;
5137 goto out;
5139 priv = hw->priv;
5140 SET_IEEE80211_DEV(hw, &pdev->dev);
5142 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
5143 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
5144 IWL_ERR(priv,
5145 "invalid queues_num, should be between %d and %d\n",
5146 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
5147 err = -EINVAL;
5148 goto out;
5152 * Disabling hardware scan means that mac80211 will perform scans
5153 * "the hard way", rather than using device's scan.
5155 if (iwl3945_mod_params.disable_hw_scan) {
5156 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
5157 iwl3945_hw_ops.hw_scan = NULL;
5161 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
5162 priv->cfg = cfg;
5163 priv->pci_dev = pdev;
5165 #ifdef CONFIG_IWLWIFI_DEBUG
5166 priv->debug_level = iwl3945_mod_params.debug;
5167 atomic_set(&priv->restrict_refcnt, 0);
5168 #endif
5169 hw->rate_control_algorithm = "iwl-3945-rs";
5170 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
5172 /* Tell mac80211 our characteristics */
5173 hw->flags = IEEE80211_HW_SIGNAL_DBM |
5174 IEEE80211_HW_NOISE_DBM;
5176 hw->wiphy->interface_modes =
5177 BIT(NL80211_IFTYPE_STATION) |
5178 BIT(NL80211_IFTYPE_ADHOC);
5180 hw->wiphy->custom_regulatory = true;
5182 hw->wiphy->max_scan_ssids = 1;
5184 /* 4 EDCA QOS priorities */
5185 hw->queues = 4;
5187 /***************************
5188 * 2. Initializing PCI bus
5189 * *************************/
5190 if (pci_enable_device(pdev)) {
5191 err = -ENODEV;
5192 goto out_ieee80211_free_hw;
5195 pci_set_master(pdev);
5197 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5198 if (!err)
5199 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
5200 if (err) {
5201 IWL_WARN(priv, "No suitable DMA available.\n");
5202 goto out_pci_disable_device;
5205 pci_set_drvdata(pdev, priv);
5206 err = pci_request_regions(pdev, DRV_NAME);
5207 if (err)
5208 goto out_pci_disable_device;
5210 /***********************
5211 * 3. Read REV Register
5212 * ********************/
5213 priv->hw_base = pci_iomap(pdev, 0, 0);
5214 if (!priv->hw_base) {
5215 err = -ENODEV;
5216 goto out_pci_release_regions;
5219 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
5220 (unsigned long long) pci_resource_len(pdev, 0));
5221 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
5223 /* We disable the RETRY_TIMEOUT register (0x41) to keep
5224 * PCI Tx retries from interfering with C3 CPU state */
5225 pci_write_config_byte(pdev, 0x41, 0x00);
5227 /* amp init */
5228 err = priv->cfg->ops->lib->apm_ops.init(priv);
5229 if (err < 0) {
5230 IWL_DEBUG_INFO(priv, "Failed to init APMG\n");
5231 goto out_iounmap;
5234 /***********************
5235 * 4. Read EEPROM
5236 * ********************/
5238 /* Read the EEPROM */
5239 err = iwl_eeprom_init(priv);
5240 if (err) {
5241 IWL_ERR(priv, "Unable to init EEPROM\n");
5242 goto out_remove_sysfs;
5244 /* MAC Address location in EEPROM same for 3945/4965 */
5245 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
5246 memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
5247 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
5248 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
5250 /***********************
5251 * 5. Setup HW Constants
5252 * ********************/
5253 /* Device-specific setup */
5254 if (iwl3945_hw_set_hw_params(priv)) {
5255 IWL_ERR(priv, "failed to set hw settings\n");
5256 goto out_iounmap;
5259 /***********************
5260 * 6. Setup priv
5261 * ********************/
5263 err = iwl3945_init_drv(priv);
5264 if (err) {
5265 IWL_ERR(priv, "initializing driver failed\n");
5266 goto out_free_geos;
5269 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
5270 priv->cfg->name);
5272 /***********************************
5273 * 7. Initialize Module Parameters
5274 * **********************************/
5276 /* Initialize module parameter values here */
5277 /* Disable radio (SW RF KILL) via parameter when loading driver */
5278 if (iwl3945_mod_params.disable) {
5279 set_bit(STATUS_RF_KILL_SW, &priv->status);
5280 IWL_DEBUG_INFO(priv, "Radio disabled.\n");
5284 /***********************
5285 * 8. Setup Services
5286 * ********************/
5288 spin_lock_irqsave(&priv->lock, flags);
5289 iwl3945_disable_interrupts(priv);
5290 spin_unlock_irqrestore(&priv->lock, flags);
5292 pci_enable_msi(priv->pci_dev);
5294 err = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
5295 DRV_NAME, priv);
5296 if (err) {
5297 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
5298 goto out_disable_msi;
5301 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
5302 if (err) {
5303 IWL_ERR(priv, "failed to create sysfs device attributes\n");
5304 goto out_release_irq;
5307 iwl_set_rxon_channel(priv,
5308 &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
5309 iwl3945_setup_deferred_work(priv);
5310 iwl3945_setup_rx_handlers(priv);
5312 /*********************************
5313 * 9. Setup and Register mac80211
5314 * *******************************/
5316 err = ieee80211_register_hw(priv->hw);
5317 if (err) {
5318 IWL_ERR(priv, "Failed to register network device: %d\n", err);
5319 goto out_remove_sysfs;
5322 priv->hw->conf.beacon_int = 100;
5323 priv->mac80211_registered = 1;
5325 err = iwl_rfkill_init(priv);
5326 if (err)
5327 IWL_ERR(priv, "Unable to initialize RFKILL system. "
5328 "Ignoring error: %d\n", err);
5330 /* Start monitoring the killswitch */
5331 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
5332 2 * HZ);
5334 return 0;
5336 out_remove_sysfs:
5337 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
5338 out_free_geos:
5339 iwlcore_free_geos(priv);
5341 out_release_irq:
5342 free_irq(priv->pci_dev->irq, priv);
5343 destroy_workqueue(priv->workqueue);
5344 priv->workqueue = NULL;
5345 iwl3945_unset_hw_params(priv);
5346 out_disable_msi:
5347 pci_disable_msi(priv->pci_dev);
5348 out_iounmap:
5349 pci_iounmap(pdev, priv->hw_base);
5350 out_pci_release_regions:
5351 pci_release_regions(pdev);
5352 out_pci_disable_device:
5353 pci_disable_device(pdev);
5354 pci_set_drvdata(pdev, NULL);
5355 out_ieee80211_free_hw:
5356 ieee80211_free_hw(priv->hw);
5357 out:
5358 return err;
5361 static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
5363 struct iwl_priv *priv = pci_get_drvdata(pdev);
5364 unsigned long flags;
5366 if (!priv)
5367 return;
5369 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
5371 set_bit(STATUS_EXIT_PENDING, &priv->status);
5373 if (priv->mac80211_registered) {
5374 ieee80211_unregister_hw(priv->hw);
5375 priv->mac80211_registered = 0;
5376 } else {
5377 iwl3945_down(priv);
5380 /* make sure we flush any pending irq or
5381 * tasklet for the driver
5383 spin_lock_irqsave(&priv->lock, flags);
5384 iwl3945_disable_interrupts(priv);
5385 spin_unlock_irqrestore(&priv->lock, flags);
5387 iwl_synchronize_irq(priv);
5389 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
5391 iwl_rfkill_unregister(priv);
5392 cancel_delayed_work(&priv->rfkill_poll);
5394 iwl3945_dealloc_ucode_pci(priv);
5396 if (priv->rxq.bd)
5397 iwl_rx_queue_free(priv, &priv->rxq);
5398 iwl3945_hw_txq_ctx_free(priv);
5400 iwl3945_unset_hw_params(priv);
5401 iwl3945_clear_stations_table(priv);
5403 /*netif_stop_queue(dev); */
5404 flush_workqueue(priv->workqueue);
5406 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
5407 * priv->workqueue... so we can't take down the workqueue
5408 * until now... */
5409 destroy_workqueue(priv->workqueue);
5410 priv->workqueue = NULL;
5412 free_irq(pdev->irq, priv);
5413 pci_disable_msi(pdev);
5415 pci_iounmap(pdev, priv->hw_base);
5416 pci_release_regions(pdev);
5417 pci_disable_device(pdev);
5418 pci_set_drvdata(pdev, NULL);
5420 iwl_free_channel_map(priv);
5421 iwlcore_free_geos(priv);
5422 kfree(priv->scan);
5423 if (priv->ibss_beacon)
5424 dev_kfree_skb(priv->ibss_beacon);
5426 ieee80211_free_hw(priv->hw);
5429 #ifdef CONFIG_PM
5431 static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
5433 struct iwl_priv *priv = pci_get_drvdata(pdev);
5435 if (priv->is_open) {
5436 set_bit(STATUS_IN_SUSPEND, &priv->status);
5437 iwl3945_mac_stop(priv->hw);
5438 priv->is_open = 1;
5440 pci_save_state(pdev);
5441 pci_disable_device(pdev);
5442 pci_set_power_state(pdev, PCI_D3hot);
5444 return 0;
5447 static int iwl3945_pci_resume(struct pci_dev *pdev)
5449 struct iwl_priv *priv = pci_get_drvdata(pdev);
5450 int ret;
5452 pci_set_power_state(pdev, PCI_D0);
5453 ret = pci_enable_device(pdev);
5454 if (ret)
5455 return ret;
5456 pci_restore_state(pdev);
5458 if (priv->is_open)
5459 iwl3945_mac_start(priv->hw);
5461 clear_bit(STATUS_IN_SUSPEND, &priv->status);
5462 return 0;
5465 #endif /* CONFIG_PM */
5467 /*****************************************************************************
5469 * driver and module entry point
5471 *****************************************************************************/
5473 static struct pci_driver iwl3945_driver = {
5474 .name = DRV_NAME,
5475 .id_table = iwl3945_hw_card_ids,
5476 .probe = iwl3945_pci_probe,
5477 .remove = __devexit_p(iwl3945_pci_remove),
5478 #ifdef CONFIG_PM
5479 .suspend = iwl3945_pci_suspend,
5480 .resume = iwl3945_pci_resume,
5481 #endif
5484 static int __init iwl3945_init(void)
5487 int ret;
5488 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
5489 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
5491 ret = iwl3945_rate_control_register();
5492 if (ret) {
5493 printk(KERN_ERR DRV_NAME
5494 "Unable to register rate control algorithm: %d\n", ret);
5495 return ret;
5498 ret = pci_register_driver(&iwl3945_driver);
5499 if (ret) {
5500 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
5501 goto error_register;
5504 return ret;
5506 error_register:
5507 iwl3945_rate_control_unregister();
5508 return ret;
5511 static void __exit iwl3945_exit(void)
5513 pci_unregister_driver(&iwl3945_driver);
5514 iwl3945_rate_control_unregister();
5517 MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
5519 module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
5520 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
5521 module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
5522 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
5523 module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
5524 MODULE_PARM_DESC(swcrypto,
5525 "using software crypto (default 1 [software])\n");
5526 module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
5527 MODULE_PARM_DESC(debug, "debug output mask");
5528 module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
5529 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
5531 module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
5532 MODULE_PARM_DESC(queues_num, "number of hw queues.");
5534 module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, 0444);
5535 MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
5537 module_exit(iwl3945_exit);
5538 module_init(iwl3945_init);