iwlwifi: send ADD_STA before RXON with assoc bit
[firewire-audio.git] / drivers / net / wireless / iwlwifi / iwl4965-base.c
blob0a279d15e058a55232eb932b8cc42b431d705ae2
1 /******************************************************************************
3 * Copyright(c) 2003 - 2008 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 * James P. Ketrenos <ipw2100-admin@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/version.h>
33 #include <linux/init.h>
34 #include <linux/pci.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/delay.h>
37 #include <linux/skbuff.h>
38 #include <linux/netdevice.h>
39 #include <linux/wireless.h>
40 #include <linux/firmware.h>
41 #include <linux/etherdevice.h>
42 #include <linux/if_arp.h>
44 #include <net/mac80211.h>
46 #include <asm/div64.h>
48 #include "iwl-eeprom.h"
49 #include "iwl-dev.h"
50 #include "iwl-core.h"
51 #include "iwl-io.h"
52 #include "iwl-helpers.h"
53 #include "iwl-sta.h"
54 #include "iwl-calib.h"
57 /******************************************************************************
59 * module boiler plate
61 ******************************************************************************/
64 * module name, copyright, version, etc.
65 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
68 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
70 #ifdef CONFIG_IWLWIFI_DEBUG
71 #define VD "d"
72 #else
73 #define VD
74 #endif
76 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
77 #define VS "s"
78 #else
79 #define VS
80 #endif
82 #define DRV_VERSION IWLWIFI_VERSION VD VS
85 MODULE_DESCRIPTION(DRV_DESCRIPTION);
86 MODULE_VERSION(DRV_VERSION);
87 MODULE_AUTHOR(DRV_COPYRIGHT);
88 MODULE_LICENSE("GPL");
90 /*************** STATION TABLE MANAGEMENT ****
91 * mac80211 should be examined to determine if sta_info is duplicating
92 * the functionality provided here
95 /**************************************************************/
99 static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
101 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
103 if (hw_decrypt)
104 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
105 else
106 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
111 * iwl4965_check_rxon_cmd - validate RXON structure is valid
113 * NOTE: This is really only useful during development and can eventually
114 * be #ifdef'd out once the driver is stable and folks aren't actively
115 * making changes
117 static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
119 int error = 0;
120 int counter = 1;
122 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
123 error |= le32_to_cpu(rxon->flags &
124 (RXON_FLG_TGJ_NARROW_BAND_MSK |
125 RXON_FLG_RADAR_DETECT_MSK));
126 if (error)
127 IWL_WARNING("check 24G fields %d | %d\n",
128 counter++, error);
129 } else {
130 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
131 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
132 if (error)
133 IWL_WARNING("check 52 fields %d | %d\n",
134 counter++, error);
135 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
136 if (error)
137 IWL_WARNING("check 52 CCK %d | %d\n",
138 counter++, error);
140 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
141 if (error)
142 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
144 /* make sure basic rates 6Mbps and 1Mbps are supported */
145 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
146 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
147 if (error)
148 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
150 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
151 if (error)
152 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
154 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
155 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
156 if (error)
157 IWL_WARNING("check CCK and short slot %d | %d\n",
158 counter++, error);
160 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
161 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
162 if (error)
163 IWL_WARNING("check CCK & auto detect %d | %d\n",
164 counter++, error);
166 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
167 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
168 if (error)
169 IWL_WARNING("check TGG and auto detect %d | %d\n",
170 counter++, error);
172 if (error)
173 IWL_WARNING("Tuning to channel %d\n",
174 le16_to_cpu(rxon->channel));
176 if (error) {
177 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
178 return -1;
180 return 0;
184 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
185 * @priv: staging_rxon is compared to active_rxon
187 * If the RXON structure is changing enough to require a new tune,
188 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
189 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
191 static int iwl4965_full_rxon_required(struct iwl_priv *priv)
194 /* These items are only settable from the full RXON command */
195 if (!(iwl_is_associated(priv)) ||
196 compare_ether_addr(priv->staging_rxon.bssid_addr,
197 priv->active_rxon.bssid_addr) ||
198 compare_ether_addr(priv->staging_rxon.node_addr,
199 priv->active_rxon.node_addr) ||
200 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
201 priv->active_rxon.wlap_bssid_addr) ||
202 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
203 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
204 (priv->staging_rxon.air_propagation !=
205 priv->active_rxon.air_propagation) ||
206 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
207 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
208 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
209 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
210 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
211 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
212 return 1;
214 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
215 * be updated with the RXON_ASSOC command -- however only some
216 * flag transitions are allowed using RXON_ASSOC */
218 /* Check if we are not switching bands */
219 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
220 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
221 return 1;
223 /* Check if we are switching association toggle */
224 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
225 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
226 return 1;
228 return 0;
232 * iwl4965_commit_rxon - commit staging_rxon to hardware
234 * The RXON command in staging_rxon is committed to the hardware and
235 * the active_rxon structure is updated with the new data. This
236 * function correctly transitions out of the RXON_ASSOC_MSK state if
237 * a HW tune is required based on the RXON structure changes.
239 static int iwl4965_commit_rxon(struct iwl_priv *priv)
241 /* cast away the const for active_rxon in this function */
242 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
243 DECLARE_MAC_BUF(mac);
244 int ret;
245 bool new_assoc =
246 !!(priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK);
248 if (!iwl_is_alive(priv))
249 return -EBUSY;
251 /* always get timestamp with Rx frame */
252 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
254 ret = iwl4965_check_rxon_cmd(&priv->staging_rxon);
255 if (ret) {
256 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
257 return -EINVAL;
260 /* If we don't need to send a full RXON, we can use
261 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
262 * and other flags for the current radio configuration. */
263 if (!iwl4965_full_rxon_required(priv)) {
264 ret = iwl_send_rxon_assoc(priv);
265 if (ret) {
266 IWL_ERROR("Error setting RXON_ASSOC (%d)\n", ret);
267 return ret;
270 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
271 return 0;
274 /* station table will be cleared */
275 priv->assoc_station_added = 0;
277 /* If we are currently associated and the new config requires
278 * an RXON_ASSOC and the new config wants the associated mask enabled,
279 * we must clear the associated from the active configuration
280 * before we apply the new config */
281 if (iwl_is_associated(priv) && new_assoc) {
282 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
283 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
285 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
286 sizeof(struct iwl_rxon_cmd),
287 &priv->active_rxon);
289 /* If the mask clearing failed then we set
290 * active_rxon back to what it was previously */
291 if (ret) {
292 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
293 IWL_ERROR("Error clearing ASSOC_MSK (%d)\n", ret);
294 return ret;
298 IWL_DEBUG_INFO("Sending RXON\n"
299 "* with%s RXON_FILTER_ASSOC_MSK\n"
300 "* channel = %d\n"
301 "* bssid = %s\n",
302 (new_assoc ? "" : "out"),
303 le16_to_cpu(priv->staging_rxon.channel),
304 print_mac(mac, priv->staging_rxon.bssid_addr));
306 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
308 /* Apply the new configuration
309 * RXON unassoc clears the station table in uCode, send it before
310 * we add the bcast station. If assoc bit is set, we will send RXON
311 * after having added the bcast and bssid station.
313 if (!new_assoc) {
314 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
315 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
316 if (ret) {
317 IWL_ERROR("Error setting new RXON (%d)\n", ret);
318 return ret;
320 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
323 iwlcore_clear_stations_table(priv);
325 if (!priv->error_recovering)
326 priv->start_calib = 0;
328 iwl_init_sensitivity(priv);
330 /* If we issue a new RXON command which required a tune then we must
331 * send a new TXPOWER command or we won't be able to Tx any frames */
332 ret = iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
333 if (ret) {
334 IWL_ERROR("Error sending TX power (%d)\n", ret);
335 return ret;
338 /* Add the broadcast address so we can send broadcast frames */
339 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
340 IWL_INVALID_STATION) {
341 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
342 return -EIO;
345 /* If we have set the ASSOC_MSK and we are in BSS mode then
346 * add the IWL_AP_ID to the station rate table */
347 if (new_assoc && (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
348 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
349 == IWL_INVALID_STATION) {
350 IWL_ERROR("Error adding AP address for transmit.\n");
351 return -EIO;
353 priv->assoc_station_added = 1;
354 if (priv->default_wep_key &&
355 iwl_send_static_wepkey_cmd(priv, 0))
356 IWL_ERROR("Could not send WEP static key.\n");
358 /* Apply the new configuration
359 * RXON assoc doesn't clear the station table in uCode,
361 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
362 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
363 if (ret) {
364 IWL_ERROR("Error setting new RXON (%d)\n", ret);
365 return ret;
367 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
370 return 0;
373 void iwl4965_update_chain_flags(struct iwl_priv *priv)
376 iwl_set_rxon_chain(priv);
377 iwl4965_commit_rxon(priv);
380 static int iwl4965_send_bt_config(struct iwl_priv *priv)
382 struct iwl4965_bt_cmd bt_cmd = {
383 .flags = 3,
384 .lead_time = 0xAA,
385 .max_kill = 1,
386 .kill_ack_mask = 0,
387 .kill_cts_mask = 0,
390 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
391 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
394 static void iwl_clear_free_frames(struct iwl_priv *priv)
396 struct list_head *element;
398 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
399 priv->frames_count);
401 while (!list_empty(&priv->free_frames)) {
402 element = priv->free_frames.next;
403 list_del(element);
404 kfree(list_entry(element, struct iwl_frame, list));
405 priv->frames_count--;
408 if (priv->frames_count) {
409 IWL_WARNING("%d frames still in use. Did we lose one?\n",
410 priv->frames_count);
411 priv->frames_count = 0;
415 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
417 struct iwl_frame *frame;
418 struct list_head *element;
419 if (list_empty(&priv->free_frames)) {
420 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
421 if (!frame) {
422 IWL_ERROR("Could not allocate frame!\n");
423 return NULL;
426 priv->frames_count++;
427 return frame;
430 element = priv->free_frames.next;
431 list_del(element);
432 return list_entry(element, struct iwl_frame, list);
435 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
437 memset(frame, 0, sizeof(*frame));
438 list_add(&frame->list, &priv->free_frames);
441 unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
442 struct ieee80211_hdr *hdr,
443 const u8 *dest, int left)
446 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
447 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
448 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
449 return 0;
451 if (priv->ibss_beacon->len > left)
452 return 0;
454 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
456 return priv->ibss_beacon->len;
459 static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
461 int i;
462 int rate_mask;
464 /* Set rate mask*/
465 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
466 rate_mask = priv->active_rate_basic & 0xF;
467 else
468 rate_mask = priv->active_rate_basic & 0xFF0;
470 /* Find lowest valid rate */
471 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
472 i = iwl_rates[i].next_ieee) {
473 if (rate_mask & (1 << i))
474 return iwl_rates[i].plcp;
477 /* No valid rate was found. Assign the lowest one */
478 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
479 return IWL_RATE_1M_PLCP;
480 else
481 return IWL_RATE_6M_PLCP;
484 static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
486 struct iwl_frame *frame;
487 unsigned int frame_size;
488 int rc;
489 u8 rate;
491 frame = iwl_get_free_frame(priv);
493 if (!frame) {
494 IWL_ERROR("Could not obtain free frame buffer for beacon "
495 "command.\n");
496 return -ENOMEM;
499 rate = iwl4965_rate_get_lowest_plcp(priv);
501 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
503 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
504 &frame->u.cmd[0]);
506 iwl_free_frame(priv, frame);
508 return rc;
511 /******************************************************************************
513 * Misc. internal state and helper functions
515 ******************************************************************************/
517 static void iwl4965_ht_conf(struct iwl_priv *priv,
518 struct ieee80211_bss_conf *bss_conf)
520 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
521 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
522 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
524 IWL_DEBUG_MAC80211("enter: \n");
526 iwl_conf->is_ht = bss_conf->assoc_ht;
528 if (!iwl_conf->is_ht)
529 return;
531 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
533 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
534 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
535 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
536 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
538 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
539 iwl_conf->max_amsdu_size =
540 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
542 iwl_conf->supported_chan_width =
543 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
544 iwl_conf->extension_chan_offset =
545 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
546 /* If no above or below channel supplied disable FAT channel */
547 if (iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_ABOVE &&
548 iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_BELOW) {
549 iwl_conf->extension_chan_offset = IEEE80211_HT_IE_CHA_SEC_NONE;
550 iwl_conf->supported_chan_width = 0;
553 iwl_conf->tx_mimo_ps_mode =
554 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
555 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
557 iwl_conf->control_channel = ht_bss_conf->primary_channel;
558 iwl_conf->tx_chan_width =
559 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
560 iwl_conf->ht_protection =
561 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
562 iwl_conf->non_GF_STA_present =
563 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
565 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
566 IWL_DEBUG_MAC80211("leave\n");
570 * QoS support
572 static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
573 struct iwl4965_qosparam_cmd *qos)
576 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
577 sizeof(struct iwl4965_qosparam_cmd), qos);
580 static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
582 unsigned long flags;
584 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
585 return;
587 if (!priv->qos_data.qos_enable)
588 return;
590 spin_lock_irqsave(&priv->lock, flags);
591 priv->qos_data.def_qos_parm.qos_flags = 0;
593 if (priv->qos_data.qos_cap.q_AP.queue_request &&
594 !priv->qos_data.qos_cap.q_AP.txop_request)
595 priv->qos_data.def_qos_parm.qos_flags |=
596 QOS_PARAM_FLG_TXOP_TYPE_MSK;
597 if (priv->qos_data.qos_active)
598 priv->qos_data.def_qos_parm.qos_flags |=
599 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
601 if (priv->current_ht_config.is_ht)
602 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
604 spin_unlock_irqrestore(&priv->lock, flags);
606 if (force || iwl_is_associated(priv)) {
607 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
608 priv->qos_data.qos_active,
609 priv->qos_data.def_qos_parm.qos_flags);
611 iwl4965_send_qos_params_command(priv,
612 &(priv->qos_data.def_qos_parm));
616 int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
618 /* Filter incoming packets to determine if they are targeted toward
619 * this network, discarding packets coming from ourselves */
620 switch (priv->iw_mode) {
621 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
622 /* packets from our adapter are dropped (echo) */
623 if (!compare_ether_addr(header->addr2, priv->mac_addr))
624 return 0;
625 /* {broad,multi}cast packets to our IBSS go through */
626 if (is_multicast_ether_addr(header->addr1))
627 return !compare_ether_addr(header->addr3, priv->bssid);
628 /* packets to our adapter go through */
629 return !compare_ether_addr(header->addr1, priv->mac_addr);
630 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
631 /* packets from our adapter are dropped (echo) */
632 if (!compare_ether_addr(header->addr3, priv->mac_addr))
633 return 0;
634 /* {broad,multi}cast packets to our BSS go through */
635 if (is_multicast_ether_addr(header->addr1))
636 return !compare_ether_addr(header->addr2, priv->bssid);
637 /* packets to our adapter go through */
638 return !compare_ether_addr(header->addr1, priv->mac_addr);
639 default:
640 break;
643 return 1;
646 static void iwl4965_sequence_reset(struct iwl_priv *priv)
648 /* Reset ieee stats */
650 /* We don't reset the net_device_stats (ieee->stats) on
651 * re-association */
653 priv->last_seq_num = -1;
654 priv->last_frag_num = -1;
655 priv->last_packet_time = 0;
657 iwl_scan_cancel(priv);
660 #define MAX_UCODE_BEACON_INTERVAL 4096
661 #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
663 static __le16 iwl4965_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 iwl4965_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.dw[1] = cpu_to_le32(priv->timestamp >> 32);
688 priv->rxon_timing.timestamp.dw[0] =
689 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
691 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
693 tsf = priv->timestamp;
695 beacon_int = priv->beacon_int;
696 spin_unlock_irqrestore(&priv->lock, flags);
698 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
699 if (beacon_int == 0) {
700 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
701 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
702 } else {
703 priv->rxon_timing.beacon_interval =
704 cpu_to_le16(beacon_int);
705 priv->rxon_timing.beacon_interval =
706 iwl4965_adjust_beacon_interval(
707 le16_to_cpu(priv->rxon_timing.beacon_interval));
710 priv->rxon_timing.atim_window = 0;
711 } else {
712 priv->rxon_timing.beacon_interval =
713 iwl4965_adjust_beacon_interval(conf->beacon_int);
714 /* TODO: we need to get atim_window from upper stack
715 * for now we set to 0 */
716 priv->rxon_timing.atim_window = 0;
719 interval_tm_unit =
720 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
721 result = do_div(tsf, interval_tm_unit);
722 priv->rxon_timing.beacon_init_val =
723 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
725 IWL_DEBUG_ASSOC
726 ("beacon interval %d beacon timer %d beacon tim %d\n",
727 le16_to_cpu(priv->rxon_timing.beacon_interval),
728 le32_to_cpu(priv->rxon_timing.beacon_init_val),
729 le16_to_cpu(priv->rxon_timing.atim_window));
732 static void iwl_set_flags_for_band(struct iwl_priv *priv,
733 enum ieee80211_band band)
735 if (band == IEEE80211_BAND_5GHZ) {
736 priv->staging_rxon.flags &=
737 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
738 | RXON_FLG_CCK_MSK);
739 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
740 } else {
741 /* Copied from iwl4965_post_associate() */
742 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
743 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
744 else
745 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
747 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
748 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
750 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
751 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
752 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
757 * initialize rxon structure with default values from eeprom
759 static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
761 const struct iwl_channel_info *ch_info;
763 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
765 switch (priv->iw_mode) {
766 case IEEE80211_IF_TYPE_AP:
767 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
768 break;
770 case IEEE80211_IF_TYPE_STA:
771 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
772 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
773 break;
775 case IEEE80211_IF_TYPE_IBSS:
776 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
777 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
778 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
779 RXON_FILTER_ACCEPT_GRP_MSK;
780 break;
782 case IEEE80211_IF_TYPE_MNTR:
783 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
784 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
785 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
786 break;
787 default:
788 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
789 break;
792 #if 0
793 /* TODO: Figure out when short_preamble would be set and cache from
794 * that */
795 if (!hw_to_local(priv->hw)->short_preamble)
796 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
797 else
798 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
799 #endif
801 ch_info = iwl_get_channel_info(priv, priv->band,
802 le16_to_cpu(priv->staging_rxon.channel));
804 if (!ch_info)
805 ch_info = &priv->channel_info[0];
808 * in some case A channels are all non IBSS
809 * in this case force B/G channel
811 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
812 !(is_channel_ibss(ch_info)))
813 ch_info = &priv->channel_info[0];
815 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
816 priv->band = ch_info->band;
818 iwl_set_flags_for_band(priv, priv->band);
820 priv->staging_rxon.ofdm_basic_rates =
821 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
822 priv->staging_rxon.cck_basic_rates =
823 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
825 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
826 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
827 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
828 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
829 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
830 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
831 iwl_set_rxon_chain(priv);
834 static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
836 priv->iw_mode = mode;
838 /* init channel/phymode to values given at driver init */
839 iwl_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
841 iwl4965_connection_init_rx_config(priv);
842 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
844 iwlcore_clear_stations_table(priv);
846 /* dont commit rxon if rf-kill is on*/
847 if (!iwl_is_ready_rf(priv))
848 return -EAGAIN;
850 cancel_delayed_work(&priv->scan_check);
851 if (iwl_scan_cancel_timeout(priv, 100)) {
852 IWL_WARNING("Aborted scan still in progress after 100ms\n");
853 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
854 return -EAGAIN;
857 iwl4965_commit_rxon(priv);
859 return 0;
862 static void iwl4965_set_rate(struct iwl_priv *priv)
864 const struct ieee80211_supported_band *hw = NULL;
865 struct ieee80211_rate *rate;
866 int i;
868 hw = iwl_get_hw_mode(priv, priv->band);
869 if (!hw) {
870 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
871 return;
874 priv->active_rate = 0;
875 priv->active_rate_basic = 0;
877 for (i = 0; i < hw->n_bitrates; i++) {
878 rate = &(hw->bitrates[i]);
879 if (rate->hw_value < IWL_RATE_COUNT)
880 priv->active_rate |= (1 << rate->hw_value);
883 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
884 priv->active_rate, priv->active_rate_basic);
887 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
888 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
889 * OFDM
891 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
892 priv->staging_rxon.cck_basic_rates =
893 ((priv->active_rate_basic &
894 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
895 else
896 priv->staging_rxon.cck_basic_rates =
897 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
899 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
900 priv->staging_rxon.ofdm_basic_rates =
901 ((priv->active_rate_basic &
902 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
903 IWL_FIRST_OFDM_RATE) & 0xFF;
904 else
905 priv->staging_rxon.ofdm_basic_rates =
906 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
909 #define IWL_PACKET_RETRY_TIME HZ
911 int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
913 u16 sc = le16_to_cpu(header->seq_ctrl);
914 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
915 u16 frag = sc & IEEE80211_SCTL_FRAG;
916 u16 *last_seq, *last_frag;
917 unsigned long *last_time;
919 switch (priv->iw_mode) {
920 case IEEE80211_IF_TYPE_IBSS:{
921 struct list_head *p;
922 struct iwl4965_ibss_seq *entry = NULL;
923 u8 *mac = header->addr2;
924 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
926 __list_for_each(p, &priv->ibss_mac_hash[index]) {
927 entry = list_entry(p, struct iwl4965_ibss_seq, list);
928 if (!compare_ether_addr(entry->mac, mac))
929 break;
931 if (p == &priv->ibss_mac_hash[index]) {
932 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
933 if (!entry) {
934 IWL_ERROR("Cannot malloc new mac entry\n");
935 return 0;
937 memcpy(entry->mac, mac, ETH_ALEN);
938 entry->seq_num = seq;
939 entry->frag_num = frag;
940 entry->packet_time = jiffies;
941 list_add(&entry->list, &priv->ibss_mac_hash[index]);
942 return 0;
944 last_seq = &entry->seq_num;
945 last_frag = &entry->frag_num;
946 last_time = &entry->packet_time;
947 break;
949 case IEEE80211_IF_TYPE_STA:
950 last_seq = &priv->last_seq_num;
951 last_frag = &priv->last_frag_num;
952 last_time = &priv->last_packet_time;
953 break;
954 default:
955 return 0;
957 if ((*last_seq == seq) &&
958 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
959 if (*last_frag == frag)
960 goto drop;
961 if (*last_frag + 1 != frag)
962 /* out-of-order fragment */
963 goto drop;
964 } else
965 *last_seq = seq;
967 *last_frag = frag;
968 *last_time = jiffies;
969 return 0;
971 drop:
972 return 1;
975 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
977 #include "iwl-spectrum.h"
979 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
980 #define BEACON_TIME_MASK_HIGH 0xFF000000
981 #define TIME_UNIT 1024
984 * extended beacon time format
985 * time in usec will be changed into a 32-bit value in 8:24 format
986 * the high 1 byte is the beacon counts
987 * the lower 3 bytes is the time in usec within one beacon interval
990 static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
992 u32 quot;
993 u32 rem;
994 u32 interval = beacon_interval * 1024;
996 if (!interval || !usec)
997 return 0;
999 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1000 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1002 return (quot << 24) + rem;
1005 /* base is usually what we get from ucode with each received frame,
1006 * the same as HW timer counter counting down
1009 static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
1011 u32 base_low = base & BEACON_TIME_MASK_LOW;
1012 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1013 u32 interval = beacon_interval * TIME_UNIT;
1014 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1015 (addon & BEACON_TIME_MASK_HIGH);
1017 if (base_low > addon_low)
1018 res += base_low - addon_low;
1019 else if (base_low < addon_low) {
1020 res += interval + base_low - addon_low;
1021 res += (1 << 24);
1022 } else
1023 res += (1 << 24);
1025 return cpu_to_le32(res);
1028 static int iwl4965_get_measurement(struct iwl_priv *priv,
1029 struct ieee80211_measurement_params *params,
1030 u8 type)
1032 struct iwl4965_spectrum_cmd spectrum;
1033 struct iwl_rx_packet *res;
1034 struct iwl_host_cmd cmd = {
1035 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1036 .data = (void *)&spectrum,
1037 .meta.flags = CMD_WANT_SKB,
1039 u32 add_time = le64_to_cpu(params->start_time);
1040 int rc;
1041 int spectrum_resp_status;
1042 int duration = le16_to_cpu(params->duration);
1044 if (iwl_is_associated(priv))
1045 add_time =
1046 iwl4965_usecs_to_beacons(
1047 le64_to_cpu(params->start_time) - priv->last_tsf,
1048 le16_to_cpu(priv->rxon_timing.beacon_interval));
1050 memset(&spectrum, 0, sizeof(spectrum));
1052 spectrum.channel_count = cpu_to_le16(1);
1053 spectrum.flags =
1054 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1055 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1056 cmd.len = sizeof(spectrum);
1057 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1059 if (iwl_is_associated(priv))
1060 spectrum.start_time =
1061 iwl4965_add_beacon_time(priv->last_beacon_time,
1062 add_time,
1063 le16_to_cpu(priv->rxon_timing.beacon_interval));
1064 else
1065 spectrum.start_time = 0;
1067 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1068 spectrum.channels[0].channel = params->channel;
1069 spectrum.channels[0].type = type;
1070 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1071 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1072 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1074 rc = iwl_send_cmd_sync(priv, &cmd);
1075 if (rc)
1076 return rc;
1078 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1079 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1080 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
1081 rc = -EIO;
1084 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1085 switch (spectrum_resp_status) {
1086 case 0: /* Command will be handled */
1087 if (res->u.spectrum.id != 0xff) {
1088 IWL_DEBUG_INFO
1089 ("Replaced existing measurement: %d\n",
1090 res->u.spectrum.id);
1091 priv->measurement_status &= ~MEASUREMENT_READY;
1093 priv->measurement_status |= MEASUREMENT_ACTIVE;
1094 rc = 0;
1095 break;
1097 case 1: /* Command will not be handled */
1098 rc = -EAGAIN;
1099 break;
1102 dev_kfree_skb_any(cmd.meta.u.skb);
1104 return rc;
1106 #endif
1108 /******************************************************************************
1110 * Generic RX handler implementations
1112 ******************************************************************************/
1113 static void iwl_rx_reply_alive(struct iwl_priv *priv,
1114 struct iwl_rx_mem_buffer *rxb)
1116 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1117 struct iwl_alive_resp *palive;
1118 struct delayed_work *pwork;
1120 palive = &pkt->u.alive_frame;
1122 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1123 "0x%01X 0x%01X\n",
1124 palive->is_valid, palive->ver_type,
1125 palive->ver_subtype);
1127 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1128 IWL_DEBUG_INFO("Initialization Alive received.\n");
1129 memcpy(&priv->card_alive_init,
1130 &pkt->u.alive_frame,
1131 sizeof(struct iwl_init_alive_resp));
1132 pwork = &priv->init_alive_start;
1133 } else {
1134 IWL_DEBUG_INFO("Runtime Alive received.\n");
1135 memcpy(&priv->card_alive, &pkt->u.alive_frame,
1136 sizeof(struct iwl_alive_resp));
1137 pwork = &priv->alive_start;
1140 /* We delay the ALIVE response by 5ms to
1141 * give the HW RF Kill time to activate... */
1142 if (palive->is_valid == UCODE_VALID_OK)
1143 queue_delayed_work(priv->workqueue, pwork,
1144 msecs_to_jiffies(5));
1145 else
1146 IWL_WARNING("uCode did not respond OK.\n");
1149 static void iwl4965_rx_reply_error(struct iwl_priv *priv,
1150 struct iwl_rx_mem_buffer *rxb)
1152 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1154 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1155 "seq 0x%04X ser 0x%08X\n",
1156 le32_to_cpu(pkt->u.err_resp.error_type),
1157 get_cmd_string(pkt->u.err_resp.cmd_id),
1158 pkt->u.err_resp.cmd_id,
1159 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1160 le32_to_cpu(pkt->u.err_resp.error_info));
1163 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1165 static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
1167 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1168 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
1169 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
1170 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
1171 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
1172 rxon->channel = csa->channel;
1173 priv->staging_rxon.channel = csa->channel;
1176 static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
1177 struct iwl_rx_mem_buffer *rxb)
1179 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
1180 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1181 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
1183 if (!report->state) {
1184 IWL_DEBUG(IWL_DL_11H,
1185 "Spectrum Measure Notification: Start\n");
1186 return;
1189 memcpy(&priv->measure_report, report, sizeof(*report));
1190 priv->measurement_status |= MEASUREMENT_READY;
1191 #endif
1194 static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
1195 struct iwl_rx_mem_buffer *rxb)
1197 #ifdef CONFIG_IWLWIFI_DEBUG
1198 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1199 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
1200 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
1201 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1202 #endif
1205 static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1206 struct iwl_rx_mem_buffer *rxb)
1208 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1209 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
1210 "notification for %s:\n",
1211 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
1212 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
1215 static void iwl4965_bg_beacon_update(struct work_struct *work)
1217 struct iwl_priv *priv =
1218 container_of(work, struct iwl_priv, beacon_update);
1219 struct sk_buff *beacon;
1221 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
1222 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
1224 if (!beacon) {
1225 IWL_ERROR("update beacon failed\n");
1226 return;
1229 mutex_lock(&priv->mutex);
1230 /* new beacon skb is allocated every time; dispose previous.*/
1231 if (priv->ibss_beacon)
1232 dev_kfree_skb(priv->ibss_beacon);
1234 priv->ibss_beacon = beacon;
1235 mutex_unlock(&priv->mutex);
1237 iwl4965_send_beacon_cmd(priv);
1241 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
1243 * This callback is provided in order to send a statistics request.
1245 * This timer function is continually reset to execute within
1246 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
1247 * was received. We need to ensure we receive the statistics in order
1248 * to update the temperature used for calibrating the TXPOWER.
1250 static void iwl4965_bg_statistics_periodic(unsigned long data)
1252 struct iwl_priv *priv = (struct iwl_priv *)data;
1254 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1255 return;
1257 iwl_send_statistics_request(priv, CMD_ASYNC);
1260 static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
1261 struct iwl_rx_mem_buffer *rxb)
1263 #ifdef CONFIG_IWLWIFI_DEBUG
1264 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1265 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
1266 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
1268 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
1269 "tsf %d %d rate %d\n",
1270 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
1271 beacon->beacon_notify_hdr.failure_frame,
1272 le32_to_cpu(beacon->ibss_mgr_status),
1273 le32_to_cpu(beacon->high_tsf),
1274 le32_to_cpu(beacon->low_tsf), rate);
1275 #endif
1277 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
1278 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1279 queue_work(priv->workqueue, &priv->beacon_update);
1282 /* Handle notification from uCode that card's power state is changing
1283 * due to software, hardware, or critical temperature RFKILL */
1284 static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
1285 struct iwl_rx_mem_buffer *rxb)
1287 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1288 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1289 unsigned long status = priv->status;
1291 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
1292 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1293 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1295 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
1296 RF_CARD_DISABLED)) {
1298 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1299 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1301 if (!iwl_grab_nic_access(priv)) {
1302 iwl_write_direct32(
1303 priv, HBUS_TARG_MBX_C,
1304 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1306 iwl_release_nic_access(priv);
1309 if (!(flags & RXON_CARD_DISABLED)) {
1310 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
1311 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1312 if (!iwl_grab_nic_access(priv)) {
1313 iwl_write_direct32(
1314 priv, HBUS_TARG_MBX_C,
1315 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1317 iwl_release_nic_access(priv);
1321 if (flags & RF_CARD_DISABLED) {
1322 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1323 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
1324 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1325 if (!iwl_grab_nic_access(priv))
1326 iwl_release_nic_access(priv);
1330 if (flags & HW_CARD_DISABLED)
1331 set_bit(STATUS_RF_KILL_HW, &priv->status);
1332 else
1333 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1336 if (flags & SW_CARD_DISABLED)
1337 set_bit(STATUS_RF_KILL_SW, &priv->status);
1338 else
1339 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1341 if (!(flags & RXON_CARD_DISABLED))
1342 iwl_scan_cancel(priv);
1344 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1345 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1346 (test_bit(STATUS_RF_KILL_SW, &status) !=
1347 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1348 queue_work(priv->workqueue, &priv->rf_kill);
1349 else
1350 wake_up_interruptible(&priv->wait_command_queue);
1353 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
1354 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
1355 static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
1356 struct iwl_rx_mem_buffer *rxb)
1358 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1359 priv->last_phy_res[0] = 1;
1360 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
1361 sizeof(struct iwl4965_rx_phy_res));
1365 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
1367 * Setup the RX handlers for each of the reply types sent from the uCode
1368 * to the host.
1370 * This function chains into the hardware specific files for them to setup
1371 * any hardware specific handlers as well.
1373 static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
1375 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
1376 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
1377 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
1378 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
1379 iwl4965_rx_spectrum_measure_notif;
1380 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
1381 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
1382 iwl4965_rx_pm_debug_statistics_notif;
1383 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
1386 * The same handler is used for both the REPLY to a discrete
1387 * statistics request from the host as well as for the periodic
1388 * statistics notifications (after received beacons) from the uCode.
1390 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
1391 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
1393 iwl_setup_rx_scan_handlers(priv);
1395 /* status change handler */
1396 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
1398 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
1399 iwl_rx_missed_beacon_notif;
1400 /* Rx handlers */
1401 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
1402 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
1403 /* Set up hardware specific Rx handlers */
1404 priv->cfg->ops->lib->rx_handler_setup(priv);
1408 * this should be called while priv->lock is locked
1410 static void __iwl_rx_replenish(struct iwl_priv *priv)
1412 iwl_rx_allocate(priv);
1413 iwl_rx_queue_restock(priv);
1418 * iwl_rx_handle - Main entry function for receiving responses from uCode
1420 * Uses the priv->rx_handlers callback function array to invoke
1421 * the appropriate handlers, including command responses,
1422 * frame-received notifications, and other notifications.
1424 void iwl_rx_handle(struct iwl_priv *priv)
1426 struct iwl_rx_mem_buffer *rxb;
1427 struct iwl_rx_packet *pkt;
1428 struct iwl_rx_queue *rxq = &priv->rxq;
1429 u32 r, i;
1430 int reclaim;
1431 unsigned long flags;
1432 u8 fill_rx = 0;
1433 u32 count = 8;
1435 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1436 * buffer that the driver may process (last buffer filled by ucode). */
1437 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
1438 i = rxq->read;
1440 /* Rx interrupt, but nothing sent from uCode */
1441 if (i == r)
1442 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
1444 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
1445 fill_rx = 1;
1447 while (i != r) {
1448 rxb = rxq->queue[i];
1450 /* If an RXB doesn't have a Rx queue slot associated with it,
1451 * then a bug has been introduced in the queue refilling
1452 * routines -- catch it here */
1453 BUG_ON(rxb == NULL);
1455 rxq->queue[i] = NULL;
1457 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
1458 priv->hw_params.rx_buf_size,
1459 PCI_DMA_FROMDEVICE);
1460 pkt = (struct iwl_rx_packet *)rxb->skb->data;
1462 /* Reclaim a command buffer only if this packet is a response
1463 * to a (driver-originated) command.
1464 * If the packet (e.g. Rx frame) originated from uCode,
1465 * there is no command buffer to reclaim.
1466 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1467 * but apparently a few don't get set; catch them here. */
1468 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1469 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
1470 (pkt->hdr.cmd != REPLY_RX) &&
1471 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
1472 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1473 (pkt->hdr.cmd != REPLY_TX);
1475 /* Based on type of command response or notification,
1476 * handle those that need handling via function in
1477 * rx_handlers table. See iwl4965_setup_rx_handlers() */
1478 if (priv->rx_handlers[pkt->hdr.cmd]) {
1479 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
1480 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1481 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1482 } else {
1483 /* No handling needed */
1484 IWL_DEBUG(IWL_DL_RX,
1485 "r %d i %d No handler needed for %s, 0x%02x\n",
1486 r, i, get_cmd_string(pkt->hdr.cmd),
1487 pkt->hdr.cmd);
1490 if (reclaim) {
1491 /* Invoke any callbacks, transfer the skb to caller, and
1492 * fire off the (possibly) blocking iwl_send_cmd()
1493 * as we reclaim the driver command queue */
1494 if (rxb && rxb->skb)
1495 iwl_tx_cmd_complete(priv, rxb);
1496 else
1497 IWL_WARNING("Claim null rxb?\n");
1500 /* For now we just don't re-use anything. We can tweak this
1501 * later to try and re-use notification packets and SKBs that
1502 * fail to Rx correctly */
1503 if (rxb->skb != NULL) {
1504 priv->alloc_rxb_skb--;
1505 dev_kfree_skb_any(rxb->skb);
1506 rxb->skb = NULL;
1509 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
1510 priv->hw_params.rx_buf_size,
1511 PCI_DMA_FROMDEVICE);
1512 spin_lock_irqsave(&rxq->lock, flags);
1513 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1514 spin_unlock_irqrestore(&rxq->lock, flags);
1515 i = (i + 1) & RX_QUEUE_MASK;
1516 /* If there are a lot of unused frames,
1517 * restock the Rx queue so ucode wont assert. */
1518 if (fill_rx) {
1519 count++;
1520 if (count >= 8) {
1521 priv->rxq.read = i;
1522 __iwl_rx_replenish(priv);
1523 count = 0;
1528 /* Backtrack one entry */
1529 priv->rxq.read = i;
1530 iwl_rx_queue_restock(priv);
1533 #define PERFECT_RSSI (-20) /* dBm */
1534 #define WORST_RSSI (-95) /* dBm */
1535 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1537 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
1538 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1539 * about formulas used below. */
1540 int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
1542 int sig_qual;
1543 int degradation = PERFECT_RSSI - rssi_dbm;
1545 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1546 * as indicator; formula is (signal dbm - noise dbm).
1547 * SNR at or above 40 is a great signal (100%).
1548 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1549 * Weakest usable signal is usually 10 - 15 dB SNR. */
1550 if (noise_dbm) {
1551 if (rssi_dbm - noise_dbm >= 40)
1552 return 100;
1553 else if (rssi_dbm < noise_dbm)
1554 return 0;
1555 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1557 /* Else use just the signal level.
1558 * This formula is a least squares fit of data points collected and
1559 * compared with a reference system that had a percentage (%) display
1560 * for signal quality. */
1561 } else
1562 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1563 (15 * RSSI_RANGE + 62 * degradation)) /
1564 (RSSI_RANGE * RSSI_RANGE);
1566 if (sig_qual > 100)
1567 sig_qual = 100;
1568 else if (sig_qual < 1)
1569 sig_qual = 0;
1571 return sig_qual;
1574 #ifdef CONFIG_IWLWIFI_DEBUG
1575 static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
1577 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
1578 DECLARE_MAC_BUF(mac);
1580 IWL_DEBUG_RADIO("RX CONFIG:\n");
1581 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
1582 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
1583 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
1584 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
1585 le32_to_cpu(rxon->filter_flags));
1586 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
1587 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
1588 rxon->ofdm_basic_rates);
1589 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
1590 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
1591 print_mac(mac, rxon->node_addr));
1592 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
1593 print_mac(mac, rxon->bssid_addr));
1594 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
1596 #endif
1598 static void iwl4965_enable_interrupts(struct iwl_priv *priv)
1600 IWL_DEBUG_ISR("Enabling interrupts\n");
1601 set_bit(STATUS_INT_ENABLED, &priv->status);
1602 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
1605 /* call this function to flush any scheduled tasklet */
1606 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1608 /* wait to make sure we flush pedding tasklet*/
1609 synchronize_irq(priv->pci_dev->irq);
1610 tasklet_kill(&priv->irq_tasklet);
1613 static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
1615 clear_bit(STATUS_INT_ENABLED, &priv->status);
1617 /* disable interrupts from uCode/NIC to host */
1618 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
1620 /* acknowledge/clear/reset any interrupts still pending
1621 * from uCode or flow handler (Rx/Tx DMA) */
1622 iwl_write32(priv, CSR_INT, 0xffffffff);
1623 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
1624 IWL_DEBUG_ISR("Disabled interrupts\n");
1629 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
1631 static void iwl4965_irq_handle_error(struct iwl_priv *priv)
1633 /* Set the FW error flag -- cleared on iwl4965_down */
1634 set_bit(STATUS_FW_ERROR, &priv->status);
1636 /* Cancel currently queued command. */
1637 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1639 #ifdef CONFIG_IWLWIFI_DEBUG
1640 if (priv->debug_level & IWL_DL_FW_ERRORS) {
1641 iwl_dump_nic_error_log(priv);
1642 iwl_dump_nic_event_log(priv);
1643 iwl4965_print_rx_config_cmd(priv);
1645 #endif
1647 wake_up_interruptible(&priv->wait_command_queue);
1649 /* Keep the restart process from trying to send host
1650 * commands by clearing the INIT status bit */
1651 clear_bit(STATUS_READY, &priv->status);
1653 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1654 IWL_DEBUG(IWL_DL_FW_ERRORS,
1655 "Restarting adapter due to uCode error.\n");
1657 if (iwl_is_associated(priv)) {
1658 memcpy(&priv->recovery_rxon, &priv->active_rxon,
1659 sizeof(priv->recovery_rxon));
1660 priv->error_recovering = 1;
1662 if (priv->cfg->mod_params->restart_fw)
1663 queue_work(priv->workqueue, &priv->restart);
1667 static void iwl4965_error_recovery(struct iwl_priv *priv)
1669 unsigned long flags;
1671 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
1672 sizeof(priv->staging_rxon));
1673 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1674 iwl4965_commit_rxon(priv);
1676 iwl_rxon_add_station(priv, priv->bssid, 1);
1678 spin_lock_irqsave(&priv->lock, flags);
1679 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
1680 priv->error_recovering = 0;
1681 spin_unlock_irqrestore(&priv->lock, flags);
1684 static void iwl4965_irq_tasklet(struct iwl_priv *priv)
1686 u32 inta, handled = 0;
1687 u32 inta_fh;
1688 unsigned long flags;
1689 #ifdef CONFIG_IWLWIFI_DEBUG
1690 u32 inta_mask;
1691 #endif
1693 spin_lock_irqsave(&priv->lock, flags);
1695 /* Ack/clear/reset pending uCode interrupts.
1696 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1697 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
1698 inta = iwl_read32(priv, CSR_INT);
1699 iwl_write32(priv, CSR_INT, inta);
1701 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1702 * Any new interrupts that happen after this, either while we're
1703 * in this tasklet, or later, will show up in next ISR/tasklet. */
1704 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1705 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
1707 #ifdef CONFIG_IWLWIFI_DEBUG
1708 if (priv->debug_level & IWL_DL_ISR) {
1709 /* just for debug */
1710 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1711 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1712 inta, inta_mask, inta_fh);
1714 #endif
1716 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1717 * atomic, make sure that inta covers all the interrupts that
1718 * we've discovered, even if FH interrupt came in just after
1719 * reading CSR_INT. */
1720 if (inta_fh & CSR49_FH_INT_RX_MASK)
1721 inta |= CSR_INT_BIT_FH_RX;
1722 if (inta_fh & CSR49_FH_INT_TX_MASK)
1723 inta |= CSR_INT_BIT_FH_TX;
1725 /* Now service all interrupt bits discovered above. */
1726 if (inta & CSR_INT_BIT_HW_ERR) {
1727 IWL_ERROR("Microcode HW error detected. Restarting.\n");
1729 /* Tell the device to stop sending interrupts */
1730 iwl4965_disable_interrupts(priv);
1732 iwl4965_irq_handle_error(priv);
1734 handled |= CSR_INT_BIT_HW_ERR;
1736 spin_unlock_irqrestore(&priv->lock, flags);
1738 return;
1741 #ifdef CONFIG_IWLWIFI_DEBUG
1742 if (priv->debug_level & (IWL_DL_ISR)) {
1743 /* NIC fires this, but we don't use it, redundant with WAKEUP */
1744 if (inta & CSR_INT_BIT_SCD)
1745 IWL_DEBUG_ISR("Scheduler finished to transmit "
1746 "the frame/frames.\n");
1748 /* Alive notification via Rx interrupt will do the real work */
1749 if (inta & CSR_INT_BIT_ALIVE)
1750 IWL_DEBUG_ISR("Alive interrupt\n");
1752 #endif
1753 /* Safely ignore these bits for debug checks below */
1754 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1756 /* HW RF KILL switch toggled */
1757 if (inta & CSR_INT_BIT_RF_KILL) {
1758 int hw_rf_kill = 0;
1759 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
1760 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1761 hw_rf_kill = 1;
1763 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
1764 hw_rf_kill ? "disable radio":"enable radio");
1766 /* Queue restart only if RF_KILL switch was set to "kill"
1767 * when we loaded driver, and is now set to "enable".
1768 * After we're Alive, RF_KILL gets handled by
1769 * iwl4965_rx_card_state_notif() */
1770 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
1771 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1772 queue_work(priv->workqueue, &priv->restart);
1775 handled |= CSR_INT_BIT_RF_KILL;
1778 /* Chip got too hot and stopped itself */
1779 if (inta & CSR_INT_BIT_CT_KILL) {
1780 IWL_ERROR("Microcode CT kill error detected.\n");
1781 handled |= CSR_INT_BIT_CT_KILL;
1784 /* Error detected by uCode */
1785 if (inta & CSR_INT_BIT_SW_ERR) {
1786 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
1787 inta);
1788 iwl4965_irq_handle_error(priv);
1789 handled |= CSR_INT_BIT_SW_ERR;
1792 /* uCode wakes up after power-down sleep */
1793 if (inta & CSR_INT_BIT_WAKEUP) {
1794 IWL_DEBUG_ISR("Wakeup interrupt\n");
1795 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
1796 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1797 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1798 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1799 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1800 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1801 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
1803 handled |= CSR_INT_BIT_WAKEUP;
1806 /* All uCode command responses, including Tx command responses,
1807 * Rx "responses" (frame-received notification), and other
1808 * notifications from uCode come through here*/
1809 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1810 iwl_rx_handle(priv);
1811 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1814 if (inta & CSR_INT_BIT_FH_TX) {
1815 IWL_DEBUG_ISR("Tx interrupt\n");
1816 handled |= CSR_INT_BIT_FH_TX;
1817 /* FH finished to write, send event */
1818 priv->ucode_write_complete = 1;
1819 wake_up_interruptible(&priv->wait_command_queue);
1822 if (inta & ~handled)
1823 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
1825 if (inta & ~CSR_INI_SET_MASK) {
1826 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
1827 inta & ~CSR_INI_SET_MASK);
1828 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
1831 /* Re-enable all interrupts */
1832 /* only Re-enable if diabled by irq */
1833 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1834 iwl4965_enable_interrupts(priv);
1836 #ifdef CONFIG_IWLWIFI_DEBUG
1837 if (priv->debug_level & (IWL_DL_ISR)) {
1838 inta = iwl_read32(priv, CSR_INT);
1839 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1840 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1841 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
1842 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1844 #endif
1845 spin_unlock_irqrestore(&priv->lock, flags);
1848 static irqreturn_t iwl4965_isr(int irq, void *data)
1850 struct iwl_priv *priv = data;
1851 u32 inta, inta_mask;
1852 u32 inta_fh;
1853 if (!priv)
1854 return IRQ_NONE;
1856 spin_lock(&priv->lock);
1858 /* Disable (but don't clear!) interrupts here to avoid
1859 * back-to-back ISRs and sporadic interrupts from our NIC.
1860 * If we have something to service, the tasklet will re-enable ints.
1861 * If we *don't* have something, we'll re-enable before leaving here. */
1862 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
1863 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
1865 /* Discover which interrupts are active/pending */
1866 inta = iwl_read32(priv, CSR_INT);
1867 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1869 /* Ignore interrupt if there's nothing in NIC to service.
1870 * This may be due to IRQ shared with another device,
1871 * or due to sporadic interrupts thrown from our NIC. */
1872 if (!inta && !inta_fh) {
1873 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
1874 goto none;
1877 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1878 /* Hardware disappeared. It might have already raised
1879 * an interrupt */
1880 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
1881 goto unplugged;
1884 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1885 inta, inta_mask, inta_fh);
1887 inta &= ~CSR_INT_BIT_SCD;
1889 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
1890 if (likely(inta || inta_fh))
1891 tasklet_schedule(&priv->irq_tasklet);
1893 unplugged:
1894 spin_unlock(&priv->lock);
1895 return IRQ_HANDLED;
1897 none:
1898 /* re-enable interrupts here since we don't have anything to service. */
1899 /* only Re-enable if diabled by irq */
1900 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1901 iwl4965_enable_interrupts(priv);
1902 spin_unlock(&priv->lock);
1903 return IRQ_NONE;
1906 /******************************************************************************
1908 * uCode download functions
1910 ******************************************************************************/
1912 static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
1914 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1915 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1916 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1917 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1918 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1919 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
1922 static void iwl4965_nic_start(struct iwl_priv *priv)
1924 /* Remove all resets to allow NIC to operate */
1925 iwl_write32(priv, CSR_RESET, 0);
1930 * iwl4965_read_ucode - Read uCode images from disk file.
1932 * Copy into buffers for card to fetch via bus-mastering
1934 static int iwl4965_read_ucode(struct iwl_priv *priv)
1936 struct iwl_ucode *ucode;
1937 int ret;
1938 const struct firmware *ucode_raw;
1939 const char *name = priv->cfg->fw_name;
1940 u8 *src;
1941 size_t len;
1942 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
1944 /* Ask kernel firmware_class module to get the boot firmware off disk.
1945 * request_firmware() is synchronous, file is in memory on return. */
1946 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
1947 if (ret < 0) {
1948 IWL_ERROR("%s firmware file req failed: Reason %d\n",
1949 name, ret);
1950 goto error;
1953 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
1954 name, ucode_raw->size);
1956 /* Make sure that we got at least our header! */
1957 if (ucode_raw->size < sizeof(*ucode)) {
1958 IWL_ERROR("File size way too small!\n");
1959 ret = -EINVAL;
1960 goto err_release;
1963 /* Data from ucode file: header followed by uCode images */
1964 ucode = (void *)ucode_raw->data;
1966 ver = le32_to_cpu(ucode->ver);
1967 inst_size = le32_to_cpu(ucode->inst_size);
1968 data_size = le32_to_cpu(ucode->data_size);
1969 init_size = le32_to_cpu(ucode->init_size);
1970 init_data_size = le32_to_cpu(ucode->init_data_size);
1971 boot_size = le32_to_cpu(ucode->boot_size);
1973 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
1974 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
1975 inst_size);
1976 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
1977 data_size);
1978 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
1979 init_size);
1980 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
1981 init_data_size);
1982 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
1983 boot_size);
1985 /* Verify size of file vs. image size info in file's header */
1986 if (ucode_raw->size < sizeof(*ucode) +
1987 inst_size + data_size + init_size +
1988 init_data_size + boot_size) {
1990 IWL_DEBUG_INFO("uCode file size %d too small\n",
1991 (int)ucode_raw->size);
1992 ret = -EINVAL;
1993 goto err_release;
1996 /* Verify that uCode images will fit in card's SRAM */
1997 if (inst_size > priv->hw_params.max_inst_size) {
1998 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
1999 inst_size);
2000 ret = -EINVAL;
2001 goto err_release;
2004 if (data_size > priv->hw_params.max_data_size) {
2005 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
2006 data_size);
2007 ret = -EINVAL;
2008 goto err_release;
2010 if (init_size > priv->hw_params.max_inst_size) {
2011 IWL_DEBUG_INFO
2012 ("uCode init instr len %d too large to fit in\n",
2013 init_size);
2014 ret = -EINVAL;
2015 goto err_release;
2017 if (init_data_size > priv->hw_params.max_data_size) {
2018 IWL_DEBUG_INFO
2019 ("uCode init data len %d too large to fit in\n",
2020 init_data_size);
2021 ret = -EINVAL;
2022 goto err_release;
2024 if (boot_size > priv->hw_params.max_bsm_size) {
2025 IWL_DEBUG_INFO
2026 ("uCode boot instr len %d too large to fit in\n",
2027 boot_size);
2028 ret = -EINVAL;
2029 goto err_release;
2032 /* Allocate ucode buffers for card's bus-master loading ... */
2034 /* Runtime instructions and 2 copies of data:
2035 * 1) unmodified from disk
2036 * 2) backup cache for save/restore during power-downs */
2037 priv->ucode_code.len = inst_size;
2038 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
2040 priv->ucode_data.len = data_size;
2041 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
2043 priv->ucode_data_backup.len = data_size;
2044 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2046 /* Initialization instructions and data */
2047 if (init_size && init_data_size) {
2048 priv->ucode_init.len = init_size;
2049 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
2051 priv->ucode_init_data.len = init_data_size;
2052 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2054 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2055 goto err_pci_alloc;
2058 /* Bootstrap (instructions only, no data) */
2059 if (boot_size) {
2060 priv->ucode_boot.len = boot_size;
2061 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
2063 if (!priv->ucode_boot.v_addr)
2064 goto err_pci_alloc;
2067 /* Copy images into buffers for card's bus-master reads ... */
2069 /* Runtime instructions (first block of data in file) */
2070 src = &ucode->data[0];
2071 len = priv->ucode_code.len;
2072 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
2073 memcpy(priv->ucode_code.v_addr, src, len);
2074 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2075 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2077 /* Runtime data (2nd block)
2078 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
2079 src = &ucode->data[inst_size];
2080 len = priv->ucode_data.len;
2081 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
2082 memcpy(priv->ucode_data.v_addr, src, len);
2083 memcpy(priv->ucode_data_backup.v_addr, src, len);
2085 /* Initialization instructions (3rd block) */
2086 if (init_size) {
2087 src = &ucode->data[inst_size + data_size];
2088 len = priv->ucode_init.len;
2089 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
2090 len);
2091 memcpy(priv->ucode_init.v_addr, src, len);
2094 /* Initialization data (4th block) */
2095 if (init_data_size) {
2096 src = &ucode->data[inst_size + data_size + init_size];
2097 len = priv->ucode_init_data.len;
2098 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
2099 len);
2100 memcpy(priv->ucode_init_data.v_addr, src, len);
2103 /* Bootstrap instructions (5th block) */
2104 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2105 len = priv->ucode_boot.len;
2106 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
2107 memcpy(priv->ucode_boot.v_addr, src, len);
2109 /* We have our copies now, allow OS release its copies */
2110 release_firmware(ucode_raw);
2111 return 0;
2113 err_pci_alloc:
2114 IWL_ERROR("failed to allocate pci memory\n");
2115 ret = -ENOMEM;
2116 iwl4965_dealloc_ucode_pci(priv);
2118 err_release:
2119 release_firmware(ucode_raw);
2121 error:
2122 return ret;
2126 * iwl_alive_start - called after REPLY_ALIVE notification received
2127 * from protocol/runtime uCode (initialization uCode's
2128 * Alive gets handled by iwl_init_alive_start()).
2130 static void iwl_alive_start(struct iwl_priv *priv)
2132 int ret = 0;
2134 IWL_DEBUG_INFO("Runtime Alive received.\n");
2136 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2137 /* We had an error bringing up the hardware, so take it
2138 * all the way back down so we can try again */
2139 IWL_DEBUG_INFO("Alive failed.\n");
2140 goto restart;
2143 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2144 * This is a paranoid check, because we would not have gotten the
2145 * "runtime" alive if code weren't properly loaded. */
2146 if (iwl_verify_ucode(priv)) {
2147 /* Runtime instruction load was bad;
2148 * take it all the way back down so we can try again */
2149 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
2150 goto restart;
2153 iwlcore_clear_stations_table(priv);
2154 ret = priv->cfg->ops->lib->alive_notify(priv);
2155 if (ret) {
2156 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
2157 ret);
2158 goto restart;
2161 /* After the ALIVE response, we can send host commands to 4965 uCode */
2162 set_bit(STATUS_ALIVE, &priv->status);
2164 if (iwl_is_rfkill(priv))
2165 return;
2167 ieee80211_wake_queues(priv->hw);
2169 priv->active_rate = priv->rates_mask;
2170 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2172 if (iwl_is_associated(priv)) {
2173 struct iwl_rxon_cmd *active_rxon =
2174 (struct iwl_rxon_cmd *)&priv->active_rxon;
2176 memcpy(&priv->staging_rxon, &priv->active_rxon,
2177 sizeof(priv->staging_rxon));
2178 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2179 } else {
2180 /* Initialize our rx_config data */
2181 iwl4965_connection_init_rx_config(priv);
2182 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2185 /* Configure Bluetooth device coexistence support */
2186 iwl4965_send_bt_config(priv);
2188 iwl_reset_run_time_calib(priv);
2190 /* Configure the adapter for unassociated operation */
2191 iwl4965_commit_rxon(priv);
2193 /* At this point, the NIC is initialized and operational */
2194 iwl_rf_kill_ct_config(priv);
2196 iwl_leds_register(priv);
2198 IWL_DEBUG_INFO("ALIVE processing complete.\n");
2199 set_bit(STATUS_READY, &priv->status);
2200 wake_up_interruptible(&priv->wait_command_queue);
2202 if (priv->error_recovering)
2203 iwl4965_error_recovery(priv);
2205 iwl_power_update_mode(priv, 1);
2206 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
2208 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
2209 iwl4965_set_mode(priv, priv->iw_mode);
2211 return;
2213 restart:
2214 queue_work(priv->workqueue, &priv->restart);
2217 static void iwl_cancel_deferred_work(struct iwl_priv *priv);
2219 static void __iwl4965_down(struct iwl_priv *priv)
2221 unsigned long flags;
2222 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2224 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
2226 if (!exit_pending)
2227 set_bit(STATUS_EXIT_PENDING, &priv->status);
2229 iwl_leds_unregister(priv);
2231 iwlcore_clear_stations_table(priv);
2233 /* Unblock any waiting calls */
2234 wake_up_interruptible_all(&priv->wait_command_queue);
2236 /* Wipe out the EXIT_PENDING status bit if we are not actually
2237 * exiting the module */
2238 if (!exit_pending)
2239 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2241 /* stop and reset the on-board processor */
2242 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
2244 /* tell the device to stop sending interrupts */
2245 spin_lock_irqsave(&priv->lock, flags);
2246 iwl4965_disable_interrupts(priv);
2247 spin_unlock_irqrestore(&priv->lock, flags);
2248 iwl_synchronize_irq(priv);
2250 if (priv->mac80211_registered)
2251 ieee80211_stop_queues(priv->hw);
2253 /* If we have not previously called iwl4965_init() then
2254 * clear all bits but the RF Kill and SUSPEND bits and return */
2255 if (!iwl_is_init(priv)) {
2256 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2257 STATUS_RF_KILL_HW |
2258 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2259 STATUS_RF_KILL_SW |
2260 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2261 STATUS_GEO_CONFIGURED |
2262 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2263 STATUS_IN_SUSPEND;
2264 goto exit;
2267 /* ...otherwise clear out all the status bits but the RF Kill and
2268 * SUSPEND bits and continue taking the NIC down. */
2269 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2270 STATUS_RF_KILL_HW |
2271 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2272 STATUS_RF_KILL_SW |
2273 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2274 STATUS_GEO_CONFIGURED |
2275 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2276 STATUS_IN_SUSPEND |
2277 test_bit(STATUS_FW_ERROR, &priv->status) <<
2278 STATUS_FW_ERROR;
2280 spin_lock_irqsave(&priv->lock, flags);
2281 iwl_clear_bit(priv, CSR_GP_CNTRL,
2282 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2283 spin_unlock_irqrestore(&priv->lock, flags);
2285 iwl_txq_ctx_stop(priv);
2286 iwl_rxq_stop(priv);
2288 spin_lock_irqsave(&priv->lock, flags);
2289 if (!iwl_grab_nic_access(priv)) {
2290 iwl_write_prph(priv, APMG_CLK_DIS_REG,
2291 APMG_CLK_VAL_DMA_CLK_RQT);
2292 iwl_release_nic_access(priv);
2294 spin_unlock_irqrestore(&priv->lock, flags);
2296 udelay(5);
2298 /* FIXME: apm_ops.suspend(priv) */
2299 priv->cfg->ops->lib->apm_ops.reset(priv);
2300 priv->cfg->ops->lib->free_shared_mem(priv);
2302 exit:
2303 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
2305 if (priv->ibss_beacon)
2306 dev_kfree_skb(priv->ibss_beacon);
2307 priv->ibss_beacon = NULL;
2309 /* clear out any free frames */
2310 iwl_clear_free_frames(priv);
2313 static void iwl4965_down(struct iwl_priv *priv)
2315 mutex_lock(&priv->mutex);
2316 __iwl4965_down(priv);
2317 mutex_unlock(&priv->mutex);
2319 iwl_cancel_deferred_work(priv);
2322 #define MAX_HW_RESTARTS 5
2324 static int __iwl4965_up(struct iwl_priv *priv)
2326 int i;
2327 int ret;
2329 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2330 IWL_WARNING("Exit pending; will not bring the NIC up\n");
2331 return -EIO;
2334 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
2335 IWL_ERROR("ucode not available for device bringup\n");
2336 return -EIO;
2339 /* If platform's RF_KILL switch is NOT set to KILL */
2340 if (iwl_read32(priv, CSR_GP_CNTRL) &
2341 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2342 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2343 else {
2344 set_bit(STATUS_RF_KILL_HW, &priv->status);
2345 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
2346 iwl_rfkill_set_hw_state(priv);
2347 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
2348 return -ENODEV;
2352 iwl_rfkill_set_hw_state(priv);
2353 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2355 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
2356 if (ret) {
2357 IWL_ERROR("Unable to allocate shared memory\n");
2358 return ret;
2361 ret = iwl_hw_nic_init(priv);
2362 if (ret) {
2363 IWL_ERROR("Unable to init nic\n");
2364 return ret;
2367 /* make sure rfkill handshake bits are cleared */
2368 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2369 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
2370 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2372 /* clear (again), then enable host interrupts */
2373 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2374 iwl4965_enable_interrupts(priv);
2376 /* really make sure rfkill handshake bits are cleared */
2377 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2378 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2380 /* Copy original ucode data image from disk into backup cache.
2381 * This will be used to initialize the on-board processor's
2382 * data SRAM for a clean start when the runtime program first loads. */
2383 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
2384 priv->ucode_data.len);
2386 /* We return success when we resume from suspend and rf_kill is on. */
2387 if (test_bit(STATUS_RF_KILL_HW, &priv->status) ||
2388 test_bit(STATUS_RF_KILL_SW, &priv->status))
2389 return 0;
2391 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2393 iwlcore_clear_stations_table(priv);
2395 /* load bootstrap state machine,
2396 * load bootstrap program into processor's memory,
2397 * prepare to load the "initialize" uCode */
2398 ret = priv->cfg->ops->lib->load_ucode(priv);
2400 if (ret) {
2401 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
2402 continue;
2405 /* Clear out the uCode error bit if it is set */
2406 clear_bit(STATUS_FW_ERROR, &priv->status);
2408 /* start card; "initialize" will load runtime ucode */
2409 iwl4965_nic_start(priv);
2411 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
2413 return 0;
2416 set_bit(STATUS_EXIT_PENDING, &priv->status);
2417 __iwl4965_down(priv);
2418 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2420 /* tried to restart and config the device for as long as our
2421 * patience could withstand */
2422 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
2423 return -EIO;
2427 /*****************************************************************************
2429 * Workqueue callbacks
2431 *****************************************************************************/
2433 static void iwl_bg_init_alive_start(struct work_struct *data)
2435 struct iwl_priv *priv =
2436 container_of(data, struct iwl_priv, init_alive_start.work);
2438 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2439 return;
2441 mutex_lock(&priv->mutex);
2442 priv->cfg->ops->lib->init_alive_start(priv);
2443 mutex_unlock(&priv->mutex);
2446 static void iwl_bg_alive_start(struct work_struct *data)
2448 struct iwl_priv *priv =
2449 container_of(data, struct iwl_priv, alive_start.work);
2451 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2452 return;
2454 mutex_lock(&priv->mutex);
2455 iwl_alive_start(priv);
2456 mutex_unlock(&priv->mutex);
2459 static void iwl4965_bg_rf_kill(struct work_struct *work)
2461 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
2463 wake_up_interruptible(&priv->wait_command_queue);
2465 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2466 return;
2468 mutex_lock(&priv->mutex);
2470 if (!iwl_is_rfkill(priv)) {
2471 IWL_DEBUG(IWL_DL_RF_KILL,
2472 "HW and/or SW RF Kill no longer active, restarting "
2473 "device\n");
2474 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
2475 queue_work(priv->workqueue, &priv->restart);
2476 } else {
2477 /* make sure mac80211 stop sending Tx frame */
2478 if (priv->mac80211_registered)
2479 ieee80211_stop_queues(priv->hw);
2481 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
2482 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2483 "disabled by SW switch\n");
2484 else
2485 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
2486 "Kill switch must be turned off for "
2487 "wireless networking to work.\n");
2489 iwl_rfkill_set_hw_state(priv);
2491 mutex_unlock(&priv->mutex);
2494 static void iwl4965_bg_set_monitor(struct work_struct *work)
2496 struct iwl_priv *priv = container_of(work,
2497 struct iwl_priv, set_monitor);
2498 int ret;
2500 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
2502 mutex_lock(&priv->mutex);
2504 ret = iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR);
2506 if (ret) {
2507 if (ret == -EAGAIN)
2508 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
2509 else
2510 IWL_ERROR("iwl4965_set_mode() failed ret = %d\n", ret);
2513 mutex_unlock(&priv->mutex);
2516 static void iwl_bg_run_time_calib_work(struct work_struct *work)
2518 struct iwl_priv *priv = container_of(work, struct iwl_priv,
2519 run_time_calib_work);
2521 mutex_lock(&priv->mutex);
2523 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
2524 test_bit(STATUS_SCANNING, &priv->status)) {
2525 mutex_unlock(&priv->mutex);
2526 return;
2529 if (priv->start_calib) {
2530 iwl_chain_noise_calibration(priv, &priv->statistics);
2532 iwl_sensitivity_calibration(priv, &priv->statistics);
2535 mutex_unlock(&priv->mutex);
2536 return;
2539 static void iwl4965_bg_up(struct work_struct *data)
2541 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
2543 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2544 return;
2546 mutex_lock(&priv->mutex);
2547 __iwl4965_up(priv);
2548 mutex_unlock(&priv->mutex);
2551 static void iwl4965_bg_restart(struct work_struct *data)
2553 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
2555 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2556 return;
2558 iwl4965_down(priv);
2559 queue_work(priv->workqueue, &priv->up);
2562 static void iwl4965_bg_rx_replenish(struct work_struct *data)
2564 struct iwl_priv *priv =
2565 container_of(data, struct iwl_priv, rx_replenish);
2567 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2568 return;
2570 mutex_lock(&priv->mutex);
2571 iwl_rx_replenish(priv);
2572 mutex_unlock(&priv->mutex);
2575 #define IWL_DELAY_NEXT_SCAN (HZ*2)
2577 static void iwl4965_post_associate(struct iwl_priv *priv)
2579 struct ieee80211_conf *conf = NULL;
2580 int ret = 0;
2581 DECLARE_MAC_BUF(mac);
2583 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2584 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
2585 return;
2588 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
2589 priv->assoc_id,
2590 print_mac(mac, priv->active_rxon.bssid_addr));
2593 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2594 return;
2597 if (!priv->vif || !priv->is_open)
2598 return;
2600 iwl_scan_cancel_timeout(priv, 200);
2602 conf = ieee80211_get_hw_conf(priv->hw);
2604 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2605 iwl4965_commit_rxon(priv);
2607 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
2608 iwl4965_setup_rxon_timing(priv);
2609 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
2610 sizeof(priv->rxon_timing), &priv->rxon_timing);
2611 if (ret)
2612 IWL_WARNING("REPLY_RXON_TIMING failed - "
2613 "Attempting to continue.\n");
2615 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2617 if (priv->current_ht_config.is_ht)
2618 iwl_set_rxon_ht(priv, &priv->current_ht_config);
2620 iwl_set_rxon_chain(priv);
2621 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2623 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
2624 priv->assoc_id, priv->beacon_int);
2626 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2627 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2628 else
2629 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2631 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2632 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2633 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2634 else
2635 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2637 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2638 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2642 iwl4965_commit_rxon(priv);
2644 switch (priv->iw_mode) {
2645 case IEEE80211_IF_TYPE_STA:
2646 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
2647 break;
2649 case IEEE80211_IF_TYPE_IBSS:
2651 /* assume default assoc id */
2652 priv->assoc_id = 1;
2654 iwl_rxon_add_station(priv, priv->bssid, 0);
2655 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
2656 iwl4965_send_beacon_cmd(priv);
2658 break;
2660 default:
2661 IWL_ERROR("%s Should not be called in %d mode\n",
2662 __FUNCTION__, priv->iw_mode);
2663 break;
2666 iwl4965_sequence_reset(priv);
2668 /* Enable Rx differential gain and sensitivity calibrations */
2669 iwl_chain_noise_reset(priv);
2670 priv->start_calib = 1;
2672 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2673 priv->assoc_station_added = 1;
2675 iwl4965_activate_qos(priv, 0);
2677 iwl_power_update_mode(priv, 0);
2678 /* we have just associated, don't start scan too early */
2679 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
2683 static void iwl4965_bg_post_associate(struct work_struct *data)
2685 struct iwl_priv *priv = container_of(data, struct iwl_priv,
2686 post_associate.work);
2688 mutex_lock(&priv->mutex);
2689 iwl4965_post_associate(priv);
2690 mutex_unlock(&priv->mutex);
2694 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
2696 static void iwl_bg_scan_completed(struct work_struct *work)
2698 struct iwl_priv *priv =
2699 container_of(work, struct iwl_priv, scan_completed);
2701 IWL_DEBUG_SCAN("SCAN complete scan\n");
2703 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2704 return;
2706 if (test_bit(STATUS_CONF_PENDING, &priv->status))
2707 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
2709 ieee80211_scan_completed(priv->hw);
2711 /* Since setting the TXPOWER may have been deferred while
2712 * performing the scan, fire one off */
2713 mutex_lock(&priv->mutex);
2714 iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
2715 mutex_unlock(&priv->mutex);
2718 /*****************************************************************************
2720 * mac80211 entry point functions
2722 *****************************************************************************/
2724 #define UCODE_READY_TIMEOUT (2 * HZ)
2726 static int iwl4965_mac_start(struct ieee80211_hw *hw)
2728 struct iwl_priv *priv = hw->priv;
2729 int ret;
2731 IWL_DEBUG_MAC80211("enter\n");
2733 if (pci_enable_device(priv->pci_dev)) {
2734 IWL_ERROR("Fail to pci_enable_device\n");
2735 return -ENODEV;
2737 pci_restore_state(priv->pci_dev);
2738 pci_enable_msi(priv->pci_dev);
2740 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
2741 DRV_NAME, priv);
2742 if (ret) {
2743 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
2744 goto out_disable_msi;
2747 /* we should be verifying the device is ready to be opened */
2748 mutex_lock(&priv->mutex);
2750 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
2751 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
2752 * ucode filename and max sizes are card-specific. */
2754 if (!priv->ucode_code.len) {
2755 ret = iwl4965_read_ucode(priv);
2756 if (ret) {
2757 IWL_ERROR("Could not read microcode: %d\n", ret);
2758 mutex_unlock(&priv->mutex);
2759 goto out_release_irq;
2763 ret = __iwl4965_up(priv);
2765 mutex_unlock(&priv->mutex);
2767 if (ret)
2768 goto out_release_irq;
2770 IWL_DEBUG_INFO("Start UP work done.\n");
2772 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
2773 return 0;
2775 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
2776 * mac80211 will not be run successfully. */
2777 if (priv->ucode_type == UCODE_RT) {
2778 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
2779 test_bit(STATUS_READY, &priv->status),
2780 UCODE_READY_TIMEOUT);
2781 if (!ret) {
2782 if (!test_bit(STATUS_READY, &priv->status)) {
2783 IWL_ERROR("START_ALIVE timeout after %dms.\n",
2784 jiffies_to_msecs(UCODE_READY_TIMEOUT));
2785 ret = -ETIMEDOUT;
2786 goto out_release_irq;
2790 priv->is_open = 1;
2792 IWL_DEBUG_MAC80211("leave\n");
2793 return 0;
2795 out_release_irq:
2796 free_irq(priv->pci_dev->irq, priv);
2797 out_disable_msi:
2798 pci_disable_msi(priv->pci_dev);
2799 pci_disable_device(priv->pci_dev);
2800 priv->is_open = 0;
2801 IWL_DEBUG_MAC80211("leave - failed\n");
2802 return ret;
2805 static void iwl4965_mac_stop(struct ieee80211_hw *hw)
2807 struct iwl_priv *priv = hw->priv;
2809 IWL_DEBUG_MAC80211("enter\n");
2811 if (!priv->is_open) {
2812 IWL_DEBUG_MAC80211("leave - skip\n");
2813 return;
2816 priv->is_open = 0;
2818 if (iwl_is_ready_rf(priv)) {
2819 /* stop mac, cancel any scan request and clear
2820 * RXON_FILTER_ASSOC_MSK BIT
2822 mutex_lock(&priv->mutex);
2823 iwl_scan_cancel_timeout(priv, 100);
2824 cancel_delayed_work(&priv->post_associate);
2825 mutex_unlock(&priv->mutex);
2828 iwl4965_down(priv);
2830 flush_workqueue(priv->workqueue);
2831 free_irq(priv->pci_dev->irq, priv);
2832 pci_disable_msi(priv->pci_dev);
2833 pci_save_state(priv->pci_dev);
2834 pci_disable_device(priv->pci_dev);
2836 IWL_DEBUG_MAC80211("leave\n");
2839 static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2841 struct iwl_priv *priv = hw->priv;
2843 IWL_DEBUG_MAC80211("enter\n");
2845 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
2846 IWL_DEBUG_MAC80211("leave - monitor\n");
2847 return -1;
2850 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
2851 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
2853 if (iwl_tx_skb(priv, skb))
2854 dev_kfree_skb_any(skb);
2856 IWL_DEBUG_MAC80211("leave\n");
2857 return 0;
2860 static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
2861 struct ieee80211_if_init_conf *conf)
2863 struct iwl_priv *priv = hw->priv;
2864 unsigned long flags;
2865 DECLARE_MAC_BUF(mac);
2867 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
2869 if (priv->vif) {
2870 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
2871 return -EOPNOTSUPP;
2874 spin_lock_irqsave(&priv->lock, flags);
2875 priv->vif = conf->vif;
2877 spin_unlock_irqrestore(&priv->lock, flags);
2879 mutex_lock(&priv->mutex);
2881 if (conf->mac_addr) {
2882 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
2883 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
2886 if (iwl4965_set_mode(priv, conf->type) == -EAGAIN)
2887 /* we are not ready, will run again when ready */
2888 set_bit(STATUS_MODE_PENDING, &priv->status);
2890 mutex_unlock(&priv->mutex);
2892 IWL_DEBUG_MAC80211("leave\n");
2893 return 0;
2897 * iwl4965_mac_config - mac80211 config callback
2899 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
2900 * be set inappropriately and the driver currently sets the hardware up to
2901 * use it whenever needed.
2903 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
2905 struct iwl_priv *priv = hw->priv;
2906 const struct iwl_channel_info *ch_info;
2907 unsigned long flags;
2908 int ret = 0;
2909 u16 channel;
2911 mutex_lock(&priv->mutex);
2912 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
2914 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
2916 if (conf->radio_enabled && iwl_radio_kill_sw_enable_radio(priv)) {
2917 IWL_DEBUG_MAC80211("leave - RF-KILL - waiting for uCode\n");
2918 goto out;
2921 if (!conf->radio_enabled)
2922 iwl_radio_kill_sw_disable_radio(priv);
2924 if (!iwl_is_ready(priv)) {
2925 IWL_DEBUG_MAC80211("leave - not ready\n");
2926 ret = -EIO;
2927 goto out;
2930 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
2931 test_bit(STATUS_SCANNING, &priv->status))) {
2932 IWL_DEBUG_MAC80211("leave - scanning\n");
2933 set_bit(STATUS_CONF_PENDING, &priv->status);
2934 mutex_unlock(&priv->mutex);
2935 return 0;
2938 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2939 ch_info = iwl_get_channel_info(priv, conf->channel->band, channel);
2940 if (!is_channel_valid(ch_info)) {
2941 IWL_DEBUG_MAC80211("leave - invalid channel\n");
2942 ret = -EINVAL;
2943 goto out;
2946 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS &&
2947 !is_channel_ibss(ch_info)) {
2948 IWL_ERROR("channel %d in band %d not IBSS channel\n",
2949 conf->channel->hw_value, conf->channel->band);
2950 ret = -EINVAL;
2951 goto out;
2954 spin_lock_irqsave(&priv->lock, flags);
2956 /* if we are switching from ht to 2.4 clear flags
2957 * from any ht related info since 2.4 does not
2958 * support ht */
2959 if ((le16_to_cpu(priv->staging_rxon.channel) != channel)
2960 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
2961 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
2962 #endif
2964 priv->staging_rxon.flags = 0;
2966 iwl_set_rxon_channel(priv, conf->channel->band, channel);
2968 iwl_set_flags_for_band(priv, conf->channel->band);
2970 /* The list of supported rates and rate mask can be different
2971 * for each band; since the band may have changed, reset
2972 * the rate mask to what mac80211 lists */
2973 iwl4965_set_rate(priv);
2975 spin_unlock_irqrestore(&priv->lock, flags);
2977 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
2978 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
2979 iwl4965_hw_channel_switch(priv, conf->channel);
2980 goto out;
2982 #endif
2984 if (!conf->radio_enabled) {
2985 IWL_DEBUG_MAC80211("leave - radio disabled\n");
2986 goto out;
2989 if (iwl_is_rfkill(priv)) {
2990 IWL_DEBUG_MAC80211("leave - RF kill\n");
2991 ret = -EIO;
2992 goto out;
2995 IWL_DEBUG_MAC80211("TX Power old=%d new=%d\n",
2996 priv->tx_power_user_lmt, conf->power_level);
2998 iwl_set_tx_power(priv, conf->power_level, false);
3000 iwl4965_set_rate(priv);
3002 if (memcmp(&priv->active_rxon,
3003 &priv->staging_rxon, sizeof(priv->staging_rxon)))
3004 iwl4965_commit_rxon(priv);
3005 else
3006 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
3008 IWL_DEBUG_MAC80211("leave\n");
3010 out:
3011 clear_bit(STATUS_CONF_PENDING, &priv->status);
3012 mutex_unlock(&priv->mutex);
3013 return ret;
3016 static void iwl4965_config_ap(struct iwl_priv *priv)
3018 int ret = 0;
3020 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3021 return;
3023 /* The following should be done only at AP bring up */
3024 if (!(iwl_is_associated(priv))) {
3026 /* RXON - unassoc (to set timing command) */
3027 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3028 iwl4965_commit_rxon(priv);
3030 /* RXON Timing */
3031 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3032 iwl4965_setup_rxon_timing(priv);
3033 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3034 sizeof(priv->rxon_timing), &priv->rxon_timing);
3035 if (ret)
3036 IWL_WARNING("REPLY_RXON_TIMING failed - "
3037 "Attempting to continue.\n");
3039 iwl_set_rxon_chain(priv);
3041 /* FIXME: what should be the assoc_id for AP? */
3042 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3043 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3044 priv->staging_rxon.flags |=
3045 RXON_FLG_SHORT_PREAMBLE_MSK;
3046 else
3047 priv->staging_rxon.flags &=
3048 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3050 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3051 if (priv->assoc_capability &
3052 WLAN_CAPABILITY_SHORT_SLOT_TIME)
3053 priv->staging_rxon.flags |=
3054 RXON_FLG_SHORT_SLOT_MSK;
3055 else
3056 priv->staging_rxon.flags &=
3057 ~RXON_FLG_SHORT_SLOT_MSK;
3059 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3060 priv->staging_rxon.flags &=
3061 ~RXON_FLG_SHORT_SLOT_MSK;
3063 /* restore RXON assoc */
3064 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3065 iwl4965_commit_rxon(priv);
3066 iwl4965_activate_qos(priv, 1);
3067 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
3069 iwl4965_send_beacon_cmd(priv);
3071 /* FIXME - we need to add code here to detect a totally new
3072 * configuration, reset the AP, unassoc, rxon timing, assoc,
3073 * clear sta table, add BCAST sta... */
3076 static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
3077 struct ieee80211_vif *vif,
3078 struct ieee80211_if_conf *conf)
3080 struct iwl_priv *priv = hw->priv;
3081 DECLARE_MAC_BUF(mac);
3082 unsigned long flags;
3083 int rc;
3085 if (conf == NULL)
3086 return -EIO;
3088 if (priv->vif != vif) {
3089 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
3090 return 0;
3093 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3094 (!conf->beacon || !conf->ssid_len)) {
3095 IWL_DEBUG_MAC80211
3096 ("Leaving in AP mode because HostAPD is not ready.\n");
3097 return 0;
3100 if (!iwl_is_alive(priv))
3101 return -EAGAIN;
3103 mutex_lock(&priv->mutex);
3105 if (conf->bssid)
3106 IWL_DEBUG_MAC80211("bssid: %s\n",
3107 print_mac(mac, conf->bssid));
3110 * very dubious code was here; the probe filtering flag is never set:
3112 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
3113 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
3116 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
3117 if (!conf->bssid) {
3118 conf->bssid = priv->mac_addr;
3119 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
3120 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
3121 print_mac(mac, conf->bssid));
3123 if (priv->ibss_beacon)
3124 dev_kfree_skb(priv->ibss_beacon);
3126 priv->ibss_beacon = conf->beacon;
3129 if (iwl_is_rfkill(priv))
3130 goto done;
3132 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
3133 !is_multicast_ether_addr(conf->bssid)) {
3134 /* If there is currently a HW scan going on in the background
3135 * then we need to cancel it else the RXON below will fail. */
3136 if (iwl_scan_cancel_timeout(priv, 100)) {
3137 IWL_WARNING("Aborted scan still in progress "
3138 "after 100ms\n");
3139 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
3140 mutex_unlock(&priv->mutex);
3141 return -EAGAIN;
3143 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
3145 /* TODO: Audit driver for usage of these members and see
3146 * if mac80211 deprecates them (priv->bssid looks like it
3147 * shouldn't be there, but I haven't scanned the IBSS code
3148 * to verify) - jpk */
3149 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
3151 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3152 iwl4965_config_ap(priv);
3153 else {
3154 rc = iwl4965_commit_rxon(priv);
3155 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
3156 iwl_rxon_add_station(
3157 priv, priv->active_rxon.bssid_addr, 1);
3160 } else {
3161 iwl_scan_cancel_timeout(priv, 100);
3162 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3163 iwl4965_commit_rxon(priv);
3166 done:
3167 spin_lock_irqsave(&priv->lock, flags);
3168 if (!conf->ssid_len)
3169 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
3170 else
3171 memcpy(priv->essid, conf->ssid, conf->ssid_len);
3173 priv->essid_len = conf->ssid_len;
3174 spin_unlock_irqrestore(&priv->lock, flags);
3176 IWL_DEBUG_MAC80211("leave\n");
3177 mutex_unlock(&priv->mutex);
3179 return 0;
3182 static void iwl4965_configure_filter(struct ieee80211_hw *hw,
3183 unsigned int changed_flags,
3184 unsigned int *total_flags,
3185 int mc_count, struct dev_addr_list *mc_list)
3188 * XXX: dummy
3189 * see also iwl4965_connection_init_rx_config
3191 struct iwl_priv *priv = hw->priv;
3192 int new_flags = 0;
3193 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3194 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3195 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
3196 IEEE80211_IF_TYPE_MNTR,
3197 changed_flags, *total_flags);
3198 /* queue work 'cuz mac80211 is holding a lock which
3199 * prevents us from issuing (synchronous) f/w cmds */
3200 queue_work(priv->workqueue, &priv->set_monitor);
3201 new_flags &= FIF_PROMISC_IN_BSS |
3202 FIF_OTHER_BSS |
3203 FIF_ALLMULTI;
3206 *total_flags = new_flags;
3209 static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
3210 struct ieee80211_if_init_conf *conf)
3212 struct iwl_priv *priv = hw->priv;
3214 IWL_DEBUG_MAC80211("enter\n");
3216 mutex_lock(&priv->mutex);
3218 if (iwl_is_ready_rf(priv)) {
3219 iwl_scan_cancel_timeout(priv, 100);
3220 cancel_delayed_work(&priv->post_associate);
3221 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3222 iwl4965_commit_rxon(priv);
3224 if (priv->vif == conf->vif) {
3225 priv->vif = NULL;
3226 memset(priv->bssid, 0, ETH_ALEN);
3227 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
3228 priv->essid_len = 0;
3230 mutex_unlock(&priv->mutex);
3232 IWL_DEBUG_MAC80211("leave\n");
3236 #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
3237 static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
3238 struct ieee80211_vif *vif,
3239 struct ieee80211_bss_conf *bss_conf,
3240 u32 changes)
3242 struct iwl_priv *priv = hw->priv;
3244 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
3246 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
3247 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
3248 bss_conf->use_short_preamble);
3249 if (bss_conf->use_short_preamble)
3250 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3251 else
3252 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3255 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
3256 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
3257 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
3258 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
3259 else
3260 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
3263 if (changes & BSS_CHANGED_HT) {
3264 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
3265 iwl4965_ht_conf(priv, bss_conf);
3266 iwl_set_rxon_chain(priv);
3269 if (changes & BSS_CHANGED_ASSOC) {
3270 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
3271 /* This should never happen as this function should
3272 * never be called from interrupt context. */
3273 if (WARN_ON_ONCE(in_interrupt()))
3274 return;
3275 if (bss_conf->assoc) {
3276 priv->assoc_id = bss_conf->aid;
3277 priv->beacon_int = bss_conf->beacon_int;
3278 priv->timestamp = bss_conf->timestamp;
3279 priv->assoc_capability = bss_conf->assoc_capability;
3280 priv->next_scan_jiffies = jiffies +
3281 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
3282 mutex_lock(&priv->mutex);
3283 iwl4965_post_associate(priv);
3284 mutex_unlock(&priv->mutex);
3285 } else {
3286 priv->assoc_id = 0;
3287 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
3289 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
3290 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
3291 iwl_send_rxon_assoc(priv);
3296 static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
3298 int rc = 0;
3299 unsigned long flags;
3300 struct iwl_priv *priv = hw->priv;
3302 IWL_DEBUG_MAC80211("enter\n");
3304 mutex_lock(&priv->mutex);
3305 spin_lock_irqsave(&priv->lock, flags);
3307 if (!iwl_is_ready_rf(priv)) {
3308 rc = -EIO;
3309 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
3310 goto out_unlock;
3313 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
3314 rc = -EIO;
3315 IWL_ERROR("ERROR: APs don't scan\n");
3316 goto out_unlock;
3319 /* we don't schedule scan within next_scan_jiffies period */
3320 if (priv->next_scan_jiffies &&
3321 time_after(priv->next_scan_jiffies, jiffies)) {
3322 rc = -EAGAIN;
3323 goto out_unlock;
3325 /* if we just finished scan ask for delay */
3326 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
3327 IWL_DELAY_NEXT_SCAN, jiffies)) {
3328 rc = -EAGAIN;
3329 goto out_unlock;
3331 if (len) {
3332 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
3333 iwl_escape_essid(ssid, len), (int)len);
3335 priv->one_direct_scan = 1;
3336 priv->direct_ssid_len = (u8)
3337 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
3338 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
3339 } else
3340 priv->one_direct_scan = 0;
3342 rc = iwl_scan_initiate(priv);
3344 IWL_DEBUG_MAC80211("leave\n");
3346 out_unlock:
3347 spin_unlock_irqrestore(&priv->lock, flags);
3348 mutex_unlock(&priv->mutex);
3350 return rc;
3353 static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
3354 struct ieee80211_key_conf *keyconf, const u8 *addr,
3355 u32 iv32, u16 *phase1key)
3357 struct iwl_priv *priv = hw->priv;
3358 u8 sta_id = IWL_INVALID_STATION;
3359 unsigned long flags;
3360 __le16 key_flags = 0;
3361 int i;
3362 DECLARE_MAC_BUF(mac);
3364 IWL_DEBUG_MAC80211("enter\n");
3366 sta_id = iwl_find_station(priv, addr);
3367 if (sta_id == IWL_INVALID_STATION) {
3368 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
3369 print_mac(mac, addr));
3370 return;
3373 iwl_scan_cancel_timeout(priv, 100);
3375 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3376 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3377 key_flags &= ~STA_KEY_FLG_INVALID;
3379 if (sta_id == priv->hw_params.bcast_sta_id)
3380 key_flags |= STA_KEY_MULTICAST_MSK;
3382 spin_lock_irqsave(&priv->sta_lock, flags);
3384 priv->stations[sta_id].sta.key.key_flags = key_flags;
3385 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3387 for (i = 0; i < 5; i++)
3388 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
3389 cpu_to_le16(phase1key[i]);
3391 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3392 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3394 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
3396 spin_unlock_irqrestore(&priv->sta_lock, flags);
3398 IWL_DEBUG_MAC80211("leave\n");
3401 static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3402 const u8 *local_addr, const u8 *addr,
3403 struct ieee80211_key_conf *key)
3405 struct iwl_priv *priv = hw->priv;
3406 DECLARE_MAC_BUF(mac);
3407 int ret = 0;
3408 u8 sta_id = IWL_INVALID_STATION;
3409 u8 is_default_wep_key = 0;
3411 IWL_DEBUG_MAC80211("enter\n");
3413 if (priv->hw_params.sw_crypto) {
3414 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
3415 return -EOPNOTSUPP;
3418 if (is_zero_ether_addr(addr))
3419 /* only support pairwise keys */
3420 return -EOPNOTSUPP;
3422 sta_id = iwl_find_station(priv, addr);
3423 if (sta_id == IWL_INVALID_STATION) {
3424 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
3425 print_mac(mac, addr));
3426 return -EINVAL;
3430 mutex_lock(&priv->mutex);
3431 iwl_scan_cancel_timeout(priv, 100);
3432 mutex_unlock(&priv->mutex);
3434 /* If we are getting WEP group key and we didn't receive any key mapping
3435 * so far, we are in legacy wep mode (group key only), otherwise we are
3436 * in 1X mode.
3437 * In legacy wep mode, we use another host command to the uCode */
3438 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
3439 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3440 if (cmd == SET_KEY)
3441 is_default_wep_key = !priv->key_mapping_key;
3442 else
3443 is_default_wep_key =
3444 (key->hw_key_idx == HW_KEY_DEFAULT);
3447 switch (cmd) {
3448 case SET_KEY:
3449 if (is_default_wep_key)
3450 ret = iwl_set_default_wep_key(priv, key);
3451 else
3452 ret = iwl_set_dynamic_key(priv, key, sta_id);
3454 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
3455 break;
3456 case DISABLE_KEY:
3457 if (is_default_wep_key)
3458 ret = iwl_remove_default_wep_key(priv, key);
3459 else
3460 ret = iwl_remove_dynamic_key(priv, key, sta_id);
3462 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
3463 break;
3464 default:
3465 ret = -EINVAL;
3468 IWL_DEBUG_MAC80211("leave\n");
3470 return ret;
3473 static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
3474 const struct ieee80211_tx_queue_params *params)
3476 struct iwl_priv *priv = hw->priv;
3477 unsigned long flags;
3478 int q;
3480 IWL_DEBUG_MAC80211("enter\n");
3482 if (!iwl_is_ready_rf(priv)) {
3483 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3484 return -EIO;
3487 if (queue >= AC_NUM) {
3488 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
3489 return 0;
3492 if (!priv->qos_data.qos_enable) {
3493 priv->qos_data.qos_active = 0;
3494 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
3495 return 0;
3497 q = AC_NUM - 1 - queue;
3499 spin_lock_irqsave(&priv->lock, flags);
3501 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
3502 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
3503 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
3504 priv->qos_data.def_qos_parm.ac[q].edca_txop =
3505 cpu_to_le16((params->txop * 32));
3507 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
3508 priv->qos_data.qos_active = 1;
3510 spin_unlock_irqrestore(&priv->lock, flags);
3512 mutex_lock(&priv->mutex);
3513 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3514 iwl4965_activate_qos(priv, 1);
3515 else if (priv->assoc_id && iwl_is_associated(priv))
3516 iwl4965_activate_qos(priv, 0);
3518 mutex_unlock(&priv->mutex);
3520 IWL_DEBUG_MAC80211("leave\n");
3521 return 0;
3524 static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
3525 struct ieee80211_tx_queue_stats *stats)
3527 struct iwl_priv *priv = hw->priv;
3528 int i, avail;
3529 struct iwl_tx_queue *txq;
3530 struct iwl_queue *q;
3531 unsigned long flags;
3533 IWL_DEBUG_MAC80211("enter\n");
3535 if (!iwl_is_ready_rf(priv)) {
3536 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3537 return -EIO;
3540 spin_lock_irqsave(&priv->lock, flags);
3542 for (i = 0; i < AC_NUM; i++) {
3543 txq = &priv->txq[i];
3544 q = &txq->q;
3545 avail = iwl_queue_space(q);
3547 stats[i].len = q->n_window - avail;
3548 stats[i].limit = q->n_window - q->high_mark;
3549 stats[i].count = q->n_window;
3552 spin_unlock_irqrestore(&priv->lock, flags);
3554 IWL_DEBUG_MAC80211("leave\n");
3556 return 0;
3559 static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
3560 struct ieee80211_low_level_stats *stats)
3562 struct iwl_priv *priv = hw->priv;
3564 priv = hw->priv;
3565 IWL_DEBUG_MAC80211("enter\n");
3566 IWL_DEBUG_MAC80211("leave\n");
3568 return 0;
3571 static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
3573 struct iwl_priv *priv;
3575 priv = hw->priv;
3576 IWL_DEBUG_MAC80211("enter\n");
3577 IWL_DEBUG_MAC80211("leave\n");
3579 return 0;
3582 static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
3584 struct iwl_priv *priv = hw->priv;
3585 unsigned long flags;
3587 mutex_lock(&priv->mutex);
3588 IWL_DEBUG_MAC80211("enter\n");
3590 priv->lq_mngr.lq_ready = 0;
3591 spin_lock_irqsave(&priv->lock, flags);
3592 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
3593 spin_unlock_irqrestore(&priv->lock, flags);
3595 iwl_reset_qos(priv);
3597 cancel_delayed_work(&priv->post_associate);
3599 spin_lock_irqsave(&priv->lock, flags);
3600 priv->assoc_id = 0;
3601 priv->assoc_capability = 0;
3602 priv->assoc_station_added = 0;
3604 /* new association get rid of ibss beacon skb */
3605 if (priv->ibss_beacon)
3606 dev_kfree_skb(priv->ibss_beacon);
3608 priv->ibss_beacon = NULL;
3610 priv->beacon_int = priv->hw->conf.beacon_int;
3611 priv->timestamp = 0;
3612 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
3613 priv->beacon_int = 0;
3615 spin_unlock_irqrestore(&priv->lock, flags);
3617 if (!iwl_is_ready_rf(priv)) {
3618 IWL_DEBUG_MAC80211("leave - not ready\n");
3619 mutex_unlock(&priv->mutex);
3620 return;
3623 /* we are restarting association process
3624 * clear RXON_FILTER_ASSOC_MSK bit
3626 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3627 iwl_scan_cancel_timeout(priv, 100);
3628 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3629 iwl4965_commit_rxon(priv);
3632 iwl_power_update_mode(priv, 0);
3634 /* Per mac80211.h: This is only used in IBSS mode... */
3635 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
3637 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
3638 mutex_unlock(&priv->mutex);
3639 return;
3642 iwl4965_set_rate(priv);
3644 mutex_unlock(&priv->mutex);
3646 IWL_DEBUG_MAC80211("leave\n");
3649 static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
3651 struct iwl_priv *priv = hw->priv;
3652 unsigned long flags;
3654 mutex_lock(&priv->mutex);
3655 IWL_DEBUG_MAC80211("enter\n");
3657 if (!iwl_is_ready_rf(priv)) {
3658 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3659 mutex_unlock(&priv->mutex);
3660 return -EIO;
3663 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
3664 IWL_DEBUG_MAC80211("leave - not IBSS\n");
3665 mutex_unlock(&priv->mutex);
3666 return -EIO;
3669 spin_lock_irqsave(&priv->lock, flags);
3671 if (priv->ibss_beacon)
3672 dev_kfree_skb(priv->ibss_beacon);
3674 priv->ibss_beacon = skb;
3676 priv->assoc_id = 0;
3678 IWL_DEBUG_MAC80211("leave\n");
3679 spin_unlock_irqrestore(&priv->lock, flags);
3681 iwl_reset_qos(priv);
3683 iwl4965_post_associate(priv);
3685 mutex_unlock(&priv->mutex);
3687 return 0;
3690 /*****************************************************************************
3692 * sysfs attributes
3694 *****************************************************************************/
3696 #ifdef CONFIG_IWLWIFI_DEBUG
3699 * The following adds a new attribute to the sysfs representation
3700 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3701 * used for controlling the debug level.
3703 * See the level definitions in iwl for details.
3706 static ssize_t show_debug_level(struct device *d,
3707 struct device_attribute *attr, char *buf)
3709 struct iwl_priv *priv = d->driver_data;
3711 return sprintf(buf, "0x%08X\n", priv->debug_level);
3713 static ssize_t store_debug_level(struct device *d,
3714 struct device_attribute *attr,
3715 const char *buf, size_t count)
3717 struct iwl_priv *priv = d->driver_data;
3718 char *p = (char *)buf;
3719 u32 val;
3721 val = simple_strtoul(p, &p, 0);
3722 if (p == buf)
3723 printk(KERN_INFO DRV_NAME
3724 ": %s is not in hex or decimal form.\n", buf);
3725 else
3726 priv->debug_level = val;
3728 return strnlen(buf, count);
3731 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3732 show_debug_level, store_debug_level);
3735 #endif /* CONFIG_IWLWIFI_DEBUG */
3738 static ssize_t show_version(struct device *d,
3739 struct device_attribute *attr, char *buf)
3741 struct iwl_priv *priv = d->driver_data;
3742 struct iwl_alive_resp *palive = &priv->card_alive;
3743 ssize_t pos = 0;
3744 u16 eeprom_ver;
3746 if (palive->is_valid)
3747 pos += sprintf(buf + pos,
3748 "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
3749 "fw type: 0x%01X 0x%01X\n",
3750 palive->ucode_major, palive->ucode_minor,
3751 palive->sw_rev[0], palive->sw_rev[1],
3752 palive->ver_type, palive->ver_subtype);
3753 else
3754 pos += sprintf(buf + pos, "fw not loaded\n");
3756 if (priv->eeprom) {
3757 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
3758 pos += sprintf(buf + pos, "EEPROM version: 0x%x\n",
3759 eeprom_ver);
3760 } else {
3761 pos += sprintf(buf + pos, "EEPROM not initialzed\n");
3764 return pos;
3767 static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
3769 static ssize_t show_temperature(struct device *d,
3770 struct device_attribute *attr, char *buf)
3772 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3774 if (!iwl_is_alive(priv))
3775 return -EAGAIN;
3777 return sprintf(buf, "%d\n", priv->temperature);
3780 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3782 static ssize_t show_rs_window(struct device *d,
3783 struct device_attribute *attr,
3784 char *buf)
3786 struct iwl_priv *priv = d->driver_data;
3787 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
3789 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
3791 static ssize_t show_tx_power(struct device *d,
3792 struct device_attribute *attr, char *buf)
3794 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3795 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
3798 static ssize_t store_tx_power(struct device *d,
3799 struct device_attribute *attr,
3800 const char *buf, size_t count)
3802 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3803 char *p = (char *)buf;
3804 u32 val;
3806 val = simple_strtoul(p, &p, 10);
3807 if (p == buf)
3808 printk(KERN_INFO DRV_NAME
3809 ": %s is not in decimal form.\n", buf);
3810 else
3811 iwl_set_tx_power(priv, val, false);
3813 return count;
3816 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3818 static ssize_t show_flags(struct device *d,
3819 struct device_attribute *attr, char *buf)
3821 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3823 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
3826 static ssize_t store_flags(struct device *d,
3827 struct device_attribute *attr,
3828 const char *buf, size_t count)
3830 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3831 u32 flags = simple_strtoul(buf, NULL, 0);
3833 mutex_lock(&priv->mutex);
3834 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
3835 /* Cancel any currently running scans... */
3836 if (iwl_scan_cancel_timeout(priv, 100))
3837 IWL_WARNING("Could not cancel scan.\n");
3838 else {
3839 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
3840 flags);
3841 priv->staging_rxon.flags = cpu_to_le32(flags);
3842 iwl4965_commit_rxon(priv);
3845 mutex_unlock(&priv->mutex);
3847 return count;
3850 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3852 static ssize_t show_filter_flags(struct device *d,
3853 struct device_attribute *attr, char *buf)
3855 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3857 return sprintf(buf, "0x%04X\n",
3858 le32_to_cpu(priv->active_rxon.filter_flags));
3861 static ssize_t store_filter_flags(struct device *d,
3862 struct device_attribute *attr,
3863 const char *buf, size_t count)
3865 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3866 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3868 mutex_lock(&priv->mutex);
3869 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
3870 /* Cancel any currently running scans... */
3871 if (iwl_scan_cancel_timeout(priv, 100))
3872 IWL_WARNING("Could not cancel scan.\n");
3873 else {
3874 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
3875 "0x%04X\n", filter_flags);
3876 priv->staging_rxon.filter_flags =
3877 cpu_to_le32(filter_flags);
3878 iwl4965_commit_rxon(priv);
3881 mutex_unlock(&priv->mutex);
3883 return count;
3886 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3887 store_filter_flags);
3889 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
3891 static ssize_t show_measurement(struct device *d,
3892 struct device_attribute *attr, char *buf)
3894 struct iwl_priv *priv = dev_get_drvdata(d);
3895 struct iwl4965_spectrum_notification measure_report;
3896 u32 size = sizeof(measure_report), len = 0, ofs = 0;
3897 u8 *data = (u8 *) & measure_report;
3898 unsigned long flags;
3900 spin_lock_irqsave(&priv->lock, flags);
3901 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3902 spin_unlock_irqrestore(&priv->lock, flags);
3903 return 0;
3905 memcpy(&measure_report, &priv->measure_report, size);
3906 priv->measurement_status = 0;
3907 spin_unlock_irqrestore(&priv->lock, flags);
3909 while (size && (PAGE_SIZE - len)) {
3910 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3911 PAGE_SIZE - len, 1);
3912 len = strlen(buf);
3913 if (PAGE_SIZE - len)
3914 buf[len++] = '\n';
3916 ofs += 16;
3917 size -= min(size, 16U);
3920 return len;
3923 static ssize_t store_measurement(struct device *d,
3924 struct device_attribute *attr,
3925 const char *buf, size_t count)
3927 struct iwl_priv *priv = dev_get_drvdata(d);
3928 struct ieee80211_measurement_params params = {
3929 .channel = le16_to_cpu(priv->active_rxon.channel),
3930 .start_time = cpu_to_le64(priv->last_tsf),
3931 .duration = cpu_to_le16(1),
3933 u8 type = IWL_MEASURE_BASIC;
3934 u8 buffer[32];
3935 u8 channel;
3937 if (count) {
3938 char *p = buffer;
3939 strncpy(buffer, buf, min(sizeof(buffer), count));
3940 channel = simple_strtoul(p, NULL, 0);
3941 if (channel)
3942 params.channel = channel;
3944 p = buffer;
3945 while (*p && *p != ' ')
3946 p++;
3947 if (*p)
3948 type = simple_strtoul(p + 1, NULL, 0);
3951 IWL_DEBUG_INFO("Invoking measurement of type %d on "
3952 "channel %d (for '%s')\n", type, params.channel, buf);
3953 iwl4965_get_measurement(priv, &params, type);
3955 return count;
3958 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3959 show_measurement, store_measurement);
3960 #endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
3962 static ssize_t store_retry_rate(struct device *d,
3963 struct device_attribute *attr,
3964 const char *buf, size_t count)
3966 struct iwl_priv *priv = dev_get_drvdata(d);
3968 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3969 if (priv->retry_rate <= 0)
3970 priv->retry_rate = 1;
3972 return count;
3975 static ssize_t show_retry_rate(struct device *d,
3976 struct device_attribute *attr, char *buf)
3978 struct iwl_priv *priv = dev_get_drvdata(d);
3979 return sprintf(buf, "%d", priv->retry_rate);
3982 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3983 store_retry_rate);
3985 static ssize_t store_power_level(struct device *d,
3986 struct device_attribute *attr,
3987 const char *buf, size_t count)
3989 struct iwl_priv *priv = dev_get_drvdata(d);
3990 int rc;
3991 int mode;
3993 mode = simple_strtoul(buf, NULL, 0);
3994 mutex_lock(&priv->mutex);
3996 if (!iwl_is_ready(priv)) {
3997 rc = -EAGAIN;
3998 goto out;
4001 rc = iwl_power_set_user_mode(priv, mode);
4002 if (rc) {
4003 IWL_DEBUG_MAC80211("failed setting power mode.\n");
4004 goto out;
4006 rc = count;
4008 out:
4009 mutex_unlock(&priv->mutex);
4010 return rc;
4013 #define MAX_WX_STRING 80
4015 /* Values are in microsecond */
4016 static const s32 timeout_duration[] = {
4017 350000,
4018 250000,
4019 75000,
4020 37000,
4021 25000,
4023 static const s32 period_duration[] = {
4024 400000,
4025 700000,
4026 1000000,
4027 1000000,
4028 1000000
4031 static ssize_t show_power_level(struct device *d,
4032 struct device_attribute *attr, char *buf)
4034 struct iwl_priv *priv = dev_get_drvdata(d);
4035 int level = priv->power_data.power_mode;
4036 char *p = buf;
4038 p += sprintf(p, "%d ", level);
4039 switch (level) {
4040 case IWL_POWER_MODE_CAM:
4041 case IWL_POWER_AC:
4042 p += sprintf(p, "(AC)");
4043 break;
4044 case IWL_POWER_BATTERY:
4045 p += sprintf(p, "(BATTERY)");
4046 break;
4047 default:
4048 p += sprintf(p,
4049 "(Timeout %dms, Period %dms)",
4050 timeout_duration[level - 1] / 1000,
4051 period_duration[level - 1] / 1000);
4054 if (!(priv->power_mode & IWL_POWER_ENABLED))
4055 p += sprintf(p, " OFF\n");
4056 else
4057 p += sprintf(p, " \n");
4059 p += sprintf(p, " \n");
4060 return (p - buf + 1);
4063 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
4064 store_power_level);
4066 static ssize_t show_channels(struct device *d,
4067 struct device_attribute *attr, char *buf)
4070 struct iwl_priv *priv = dev_get_drvdata(d);
4071 struct ieee80211_channel *channels = NULL;
4072 const struct ieee80211_supported_band *supp_band = NULL;
4073 int len = 0, i;
4074 int count = 0;
4076 if (!test_bit(STATUS_GEO_CONFIGURED, &priv->status))
4077 return -EAGAIN;
4079 supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_2GHZ);
4080 channels = supp_band->channels;
4081 count = supp_band->n_channels;
4083 len += sprintf(&buf[len],
4084 "Displaying %d channels in 2.4GHz band "
4085 "(802.11bg):\n", count);
4087 for (i = 0; i < count; i++)
4088 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
4089 ieee80211_frequency_to_channel(
4090 channels[i].center_freq),
4091 channels[i].max_power,
4092 channels[i].flags & IEEE80211_CHAN_RADAR ?
4093 " (IEEE 802.11h required)" : "",
4094 (!(channels[i].flags & IEEE80211_CHAN_NO_IBSS)
4095 || (channels[i].flags &
4096 IEEE80211_CHAN_RADAR)) ? "" :
4097 ", IBSS",
4098 channels[i].flags &
4099 IEEE80211_CHAN_PASSIVE_SCAN ?
4100 "passive only" : "active/passive");
4102 supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_5GHZ);
4103 channels = supp_band->channels;
4104 count = supp_band->n_channels;
4106 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
4107 "(802.11a):\n", count);
4109 for (i = 0; i < count; i++)
4110 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
4111 ieee80211_frequency_to_channel(
4112 channels[i].center_freq),
4113 channels[i].max_power,
4114 channels[i].flags & IEEE80211_CHAN_RADAR ?
4115 " (IEEE 802.11h required)" : "",
4116 ((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
4117 || (channels[i].flags &
4118 IEEE80211_CHAN_RADAR)) ? "" :
4119 ", IBSS",
4120 channels[i].flags &
4121 IEEE80211_CHAN_PASSIVE_SCAN ?
4122 "passive only" : "active/passive");
4124 return len;
4127 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4129 static ssize_t show_statistics(struct device *d,
4130 struct device_attribute *attr, char *buf)
4132 struct iwl_priv *priv = dev_get_drvdata(d);
4133 u32 size = sizeof(struct iwl4965_notif_statistics);
4134 u32 len = 0, ofs = 0;
4135 u8 *data = (u8 *) & priv->statistics;
4136 int rc = 0;
4138 if (!iwl_is_alive(priv))
4139 return -EAGAIN;
4141 mutex_lock(&priv->mutex);
4142 rc = iwl_send_statistics_request(priv, 0);
4143 mutex_unlock(&priv->mutex);
4145 if (rc) {
4146 len = sprintf(buf,
4147 "Error sending statistics request: 0x%08X\n", rc);
4148 return len;
4151 while (size && (PAGE_SIZE - len)) {
4152 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4153 PAGE_SIZE - len, 1);
4154 len = strlen(buf);
4155 if (PAGE_SIZE - len)
4156 buf[len++] = '\n';
4158 ofs += 16;
4159 size -= min(size, 16U);
4162 return len;
4165 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
4167 static ssize_t show_status(struct device *d,
4168 struct device_attribute *attr, char *buf)
4170 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4171 if (!iwl_is_alive(priv))
4172 return -EAGAIN;
4173 return sprintf(buf, "0x%08x\n", (int)priv->status);
4176 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
4178 /*****************************************************************************
4180 * driver setup and teardown
4182 *****************************************************************************/
4184 static void iwl_setup_deferred_work(struct iwl_priv *priv)
4186 priv->workqueue = create_workqueue(DRV_NAME);
4188 init_waitqueue_head(&priv->wait_command_queue);
4190 INIT_WORK(&priv->up, iwl4965_bg_up);
4191 INIT_WORK(&priv->restart, iwl4965_bg_restart);
4192 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
4193 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
4194 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
4195 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
4196 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
4197 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
4198 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
4199 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
4201 /* FIXME : remove when resolved PENDING */
4202 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
4203 iwl_setup_scan_deferred_work(priv);
4205 if (priv->cfg->ops->lib->setup_deferred_work)
4206 priv->cfg->ops->lib->setup_deferred_work(priv);
4208 init_timer(&priv->statistics_periodic);
4209 priv->statistics_periodic.data = (unsigned long)priv;
4210 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
4212 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
4213 iwl4965_irq_tasklet, (unsigned long)priv);
4216 static void iwl_cancel_deferred_work(struct iwl_priv *priv)
4218 if (priv->cfg->ops->lib->cancel_deferred_work)
4219 priv->cfg->ops->lib->cancel_deferred_work(priv);
4221 cancel_delayed_work_sync(&priv->init_alive_start);
4222 cancel_delayed_work(&priv->scan_check);
4223 cancel_delayed_work(&priv->alive_start);
4224 cancel_delayed_work(&priv->post_associate);
4225 cancel_work_sync(&priv->beacon_update);
4226 del_timer_sync(&priv->statistics_periodic);
4229 static struct attribute *iwl4965_sysfs_entries[] = {
4230 &dev_attr_channels.attr,
4231 &dev_attr_flags.attr,
4232 &dev_attr_filter_flags.attr,
4233 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
4234 &dev_attr_measurement.attr,
4235 #endif
4236 &dev_attr_power_level.attr,
4237 &dev_attr_retry_rate.attr,
4238 &dev_attr_rs_window.attr,
4239 &dev_attr_statistics.attr,
4240 &dev_attr_status.attr,
4241 &dev_attr_temperature.attr,
4242 &dev_attr_tx_power.attr,
4243 #ifdef CONFIG_IWLWIFI_DEBUG
4244 &dev_attr_debug_level.attr,
4245 #endif
4246 &dev_attr_version.attr,
4248 NULL
4251 static struct attribute_group iwl4965_attribute_group = {
4252 .name = NULL, /* put in device directory */
4253 .attrs = iwl4965_sysfs_entries,
4256 static struct ieee80211_ops iwl4965_hw_ops = {
4257 .tx = iwl4965_mac_tx,
4258 .start = iwl4965_mac_start,
4259 .stop = iwl4965_mac_stop,
4260 .add_interface = iwl4965_mac_add_interface,
4261 .remove_interface = iwl4965_mac_remove_interface,
4262 .config = iwl4965_mac_config,
4263 .config_interface = iwl4965_mac_config_interface,
4264 .configure_filter = iwl4965_configure_filter,
4265 .set_key = iwl4965_mac_set_key,
4266 .update_tkip_key = iwl4965_mac_update_tkip_key,
4267 .get_stats = iwl4965_mac_get_stats,
4268 .get_tx_stats = iwl4965_mac_get_tx_stats,
4269 .conf_tx = iwl4965_mac_conf_tx,
4270 .get_tsf = iwl4965_mac_get_tsf,
4271 .reset_tsf = iwl4965_mac_reset_tsf,
4272 .beacon_update = iwl4965_mac_beacon_update,
4273 .bss_info_changed = iwl4965_bss_info_changed,
4274 .ampdu_action = iwl4965_mac_ampdu_action,
4275 .hw_scan = iwl4965_mac_hw_scan
4278 static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4280 int err = 0;
4281 struct iwl_priv *priv;
4282 struct ieee80211_hw *hw;
4283 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
4284 unsigned long flags;
4285 DECLARE_MAC_BUF(mac);
4287 /************************
4288 * 1. Allocating HW data
4289 ************************/
4291 /* Disabling hardware scan means that mac80211 will perform scans
4292 * "the hard way", rather than using device's scan. */
4293 if (cfg->mod_params->disable_hw_scan) {
4294 if (cfg->mod_params->debug & IWL_DL_INFO)
4295 dev_printk(KERN_DEBUG, &(pdev->dev),
4296 "Disabling hw_scan\n");
4297 iwl4965_hw_ops.hw_scan = NULL;
4300 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
4301 if (!hw) {
4302 err = -ENOMEM;
4303 goto out;
4305 priv = hw->priv;
4306 /* At this point both hw and priv are allocated. */
4308 SET_IEEE80211_DEV(hw, &pdev->dev);
4310 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
4311 priv->cfg = cfg;
4312 priv->pci_dev = pdev;
4314 #ifdef CONFIG_IWLWIFI_DEBUG
4315 priv->debug_level = priv->cfg->mod_params->debug;
4316 atomic_set(&priv->restrict_refcnt, 0);
4317 #endif
4319 /**************************
4320 * 2. Initializing PCI bus
4321 **************************/
4322 if (pci_enable_device(pdev)) {
4323 err = -ENODEV;
4324 goto out_ieee80211_free_hw;
4327 pci_set_master(pdev);
4329 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
4330 if (!err)
4331 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
4332 if (err) {
4333 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
4334 if (!err)
4335 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
4336 /* both attempts failed: */
4337 if (err) {
4338 printk(KERN_WARNING "%s: No suitable DMA available.\n",
4339 DRV_NAME);
4340 goto out_pci_disable_device;
4344 err = pci_request_regions(pdev, DRV_NAME);
4345 if (err)
4346 goto out_pci_disable_device;
4348 pci_set_drvdata(pdev, priv);
4350 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4351 * PCI Tx retries from interfering with C3 CPU state */
4352 pci_write_config_byte(pdev, 0x41, 0x00);
4354 /***********************
4355 * 3. Read REV register
4356 ***********************/
4357 priv->hw_base = pci_iomap(pdev, 0, 0);
4358 if (!priv->hw_base) {
4359 err = -ENODEV;
4360 goto out_pci_release_regions;
4363 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
4364 (unsigned long long) pci_resource_len(pdev, 0));
4365 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
4367 iwl_hw_detect(priv);
4368 printk(KERN_INFO DRV_NAME
4369 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
4370 priv->cfg->name, priv->hw_rev);
4372 /* amp init */
4373 err = priv->cfg->ops->lib->apm_ops.init(priv);
4374 if (err < 0) {
4375 IWL_DEBUG_INFO("Failed to init APMG\n");
4376 goto out_iounmap;
4378 /*****************
4379 * 4. Read EEPROM
4380 *****************/
4381 /* Read the EEPROM */
4382 err = iwl_eeprom_init(priv);
4383 if (err) {
4384 IWL_ERROR("Unable to init EEPROM\n");
4385 goto out_iounmap;
4387 err = iwl_eeprom_check_version(priv);
4388 if (err)
4389 goto out_iounmap;
4391 /* extract MAC Address */
4392 iwl_eeprom_get_mac(priv, priv->mac_addr);
4393 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
4394 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4396 /************************
4397 * 5. Setup HW constants
4398 ************************/
4399 /* Device-specific setup */
4400 if (priv->cfg->ops->lib->set_hw_params(priv)) {
4401 IWL_ERROR("failed to set hw parameters\n");
4402 goto out_free_eeprom;
4405 /*******************
4406 * 6. Setup priv
4407 *******************/
4409 err = iwl_init_drv(priv);
4410 if (err)
4411 goto out_free_eeprom;
4412 /* At this point both hw and priv are initialized. */
4414 /**********************************
4415 * 7. Initialize module parameters
4416 **********************************/
4418 /* Disable radio (SW RF KILL) via parameter when loading driver */
4419 if (priv->cfg->mod_params->disable) {
4420 set_bit(STATUS_RF_KILL_SW, &priv->status);
4421 IWL_DEBUG_INFO("Radio disabled.\n");
4424 /********************
4425 * 8. Setup services
4426 ********************/
4427 spin_lock_irqsave(&priv->lock, flags);
4428 iwl4965_disable_interrupts(priv);
4429 spin_unlock_irqrestore(&priv->lock, flags);
4431 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4432 if (err) {
4433 IWL_ERROR("failed to create sysfs device attributes\n");
4434 goto out_uninit_drv;
4438 iwl_setup_deferred_work(priv);
4439 iwl4965_setup_rx_handlers(priv);
4441 /********************
4442 * 9. Conclude
4443 ********************/
4444 pci_save_state(pdev);
4445 pci_disable_device(pdev);
4447 /**********************************
4448 * 10. Setup and register mac80211
4449 **********************************/
4451 err = iwl_setup_mac(priv);
4452 if (err)
4453 goto out_remove_sysfs;
4455 err = iwl_dbgfs_register(priv, DRV_NAME);
4456 if (err)
4457 IWL_ERROR("failed to create debugfs files\n");
4459 err = iwl_rfkill_init(priv);
4460 if (err)
4461 IWL_ERROR("Unable to initialize RFKILL system. "
4462 "Ignoring error: %d\n", err);
4463 iwl_power_initialize(priv);
4464 return 0;
4466 out_remove_sysfs:
4467 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4468 out_uninit_drv:
4469 iwl_uninit_drv(priv);
4470 out_free_eeprom:
4471 iwl_eeprom_free(priv);
4472 out_iounmap:
4473 pci_iounmap(pdev, priv->hw_base);
4474 out_pci_release_regions:
4475 pci_release_regions(pdev);
4476 pci_set_drvdata(pdev, NULL);
4477 out_pci_disable_device:
4478 pci_disable_device(pdev);
4479 out_ieee80211_free_hw:
4480 ieee80211_free_hw(priv->hw);
4481 out:
4482 return err;
4485 static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
4487 struct iwl_priv *priv = pci_get_drvdata(pdev);
4488 struct list_head *p, *q;
4489 int i;
4490 unsigned long flags;
4492 if (!priv)
4493 return;
4495 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
4497 iwl_dbgfs_unregister(priv);
4498 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4500 if (priv->mac80211_registered) {
4501 ieee80211_unregister_hw(priv->hw);
4502 priv->mac80211_registered = 0;
4505 set_bit(STATUS_EXIT_PENDING, &priv->status);
4507 iwl4965_down(priv);
4509 /* make sure we flush any pending irq or
4510 * tasklet for the driver
4512 spin_lock_irqsave(&priv->lock, flags);
4513 iwl4965_disable_interrupts(priv);
4514 spin_unlock_irqrestore(&priv->lock, flags);
4516 iwl_synchronize_irq(priv);
4518 /* Free MAC hash list for ADHOC */
4519 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
4520 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
4521 list_del(p);
4522 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
4526 iwl_rfkill_unregister(priv);
4527 iwl4965_dealloc_ucode_pci(priv);
4529 if (priv->rxq.bd)
4530 iwl_rx_queue_free(priv, &priv->rxq);
4531 iwl_hw_txq_ctx_free(priv);
4533 iwlcore_clear_stations_table(priv);
4534 iwl_eeprom_free(priv);
4537 /*netif_stop_queue(dev); */
4538 flush_workqueue(priv->workqueue);
4540 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
4541 * priv->workqueue... so we can't take down the workqueue
4542 * until now... */
4543 destroy_workqueue(priv->workqueue);
4544 priv->workqueue = NULL;
4546 pci_iounmap(pdev, priv->hw_base);
4547 pci_release_regions(pdev);
4548 pci_disable_device(pdev);
4549 pci_set_drvdata(pdev, NULL);
4551 iwl_uninit_drv(priv);
4553 if (priv->ibss_beacon)
4554 dev_kfree_skb(priv->ibss_beacon);
4556 ieee80211_free_hw(priv->hw);
4559 #ifdef CONFIG_PM
4561 static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4563 struct iwl_priv *priv = pci_get_drvdata(pdev);
4565 if (priv->is_open) {
4566 set_bit(STATUS_IN_SUSPEND, &priv->status);
4567 iwl4965_mac_stop(priv->hw);
4568 priv->is_open = 1;
4571 pci_set_power_state(pdev, PCI_D3hot);
4573 return 0;
4576 static int iwl4965_pci_resume(struct pci_dev *pdev)
4578 struct iwl_priv *priv = pci_get_drvdata(pdev);
4580 pci_set_power_state(pdev, PCI_D0);
4582 if (priv->is_open)
4583 iwl4965_mac_start(priv->hw);
4585 clear_bit(STATUS_IN_SUSPEND, &priv->status);
4586 return 0;
4589 #endif /* CONFIG_PM */
4591 /*****************************************************************************
4593 * driver and module entry point
4595 *****************************************************************************/
4597 /* Hardware specific file defines the PCI IDs table for that hardware module */
4598 static struct pci_device_id iwl_hw_card_ids[] = {
4599 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
4600 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
4601 #ifdef CONFIG_IWL5000
4602 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
4603 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
4604 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
4605 #endif /* CONFIG_IWL5000 */
4608 MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
4610 static struct pci_driver iwl_driver = {
4611 .name = DRV_NAME,
4612 .id_table = iwl_hw_card_ids,
4613 .probe = iwl4965_pci_probe,
4614 .remove = __devexit_p(iwl4965_pci_remove),
4615 #ifdef CONFIG_PM
4616 .suspend = iwl4965_pci_suspend,
4617 .resume = iwl4965_pci_resume,
4618 #endif
4621 static int __init iwl4965_init(void)
4624 int ret;
4625 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4626 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
4628 ret = iwl4965_rate_control_register();
4629 if (ret) {
4630 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
4631 return ret;
4634 ret = pci_register_driver(&iwl_driver);
4635 if (ret) {
4636 IWL_ERROR("Unable to initialize PCI module\n");
4637 goto error_register;
4640 return ret;
4642 error_register:
4643 iwl4965_rate_control_unregister();
4644 return ret;
4647 static void __exit iwl4965_exit(void)
4649 pci_unregister_driver(&iwl_driver);
4650 iwl4965_rate_control_unregister();
4653 module_exit(iwl4965_exit);
4654 module_init(iwl4965_init);