iwl3945: use iwl rx handlers
[linux-2.6/libata-dev.git] / drivers / net / wireless / iwlwifi / iwl-agn.c
blob0928b890ee7c648bc9fb5691fbb1b18352d45b93
1 /******************************************************************************
3 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *****************************************************************************/
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/pci.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/delay.h>
36 #include <linux/skbuff.h>
37 #include <linux/netdevice.h>
38 #include <linux/wireless.h>
39 #include <linux/firmware.h>
40 #include <linux/etherdevice.h>
41 #include <linux/if_arp.h>
43 #include <net/mac80211.h>
45 #include <asm/div64.h>
47 #define DRV_NAME "iwlagn"
49 #include "iwl-eeprom.h"
50 #include "iwl-dev.h"
51 #include "iwl-core.h"
52 #include "iwl-io.h"
53 #include "iwl-helpers.h"
54 #include "iwl-sta.h"
55 #include "iwl-calib.h"
58 /******************************************************************************
60 * module boiler plate
62 ******************************************************************************/
65 * module name, copyright, version, etc.
67 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link AGN driver for Linux"
69 #ifdef CONFIG_IWLWIFI_DEBUG
70 #define VD "d"
71 #else
72 #define VD
73 #endif
75 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
76 #define VS "s"
77 #else
78 #define VS
79 #endif
81 #define DRV_VERSION IWLWIFI_VERSION VD VS
84 MODULE_DESCRIPTION(DRV_DESCRIPTION);
85 MODULE_VERSION(DRV_VERSION);
86 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
87 MODULE_LICENSE("GPL");
88 MODULE_ALIAS("iwl4965");
90 /*************** STATION TABLE MANAGEMENT ****
91 * mac80211 should be examined to determine if sta_info is duplicating
92 * the functionality provided here
95 /**************************************************************/
97 /**
98 * iwl_commit_rxon - commit staging_rxon to hardware
100 * The RXON command in staging_rxon is committed to the hardware and
101 * the active_rxon structure is updated with the new data. This
102 * function correctly transitions out of the RXON_ASSOC_MSK state if
103 * a HW tune is required based on the RXON structure changes.
105 static int iwl_commit_rxon(struct iwl_priv *priv)
107 /* cast away the const for active_rxon in this function */
108 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
109 int ret;
110 bool new_assoc =
111 !!(priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK);
113 if (!iwl_is_alive(priv))
114 return -EBUSY;
116 /* always get timestamp with Rx frame */
117 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
118 /* allow CTS-to-self if possible. this is relevant only for
119 * 5000, but will not damage 4965 */
120 priv->staging_rxon.flags |= RXON_FLG_SELF_CTS_EN;
122 ret = iwl_check_rxon_cmd(priv);
123 if (ret) {
124 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
125 return -EINVAL;
128 /* If we don't need to send a full RXON, we can use
129 * iwl_rxon_assoc_cmd which is used to reconfigure filter
130 * and other flags for the current radio configuration. */
131 if (!iwl_full_rxon_required(priv)) {
132 ret = iwl_send_rxon_assoc(priv);
133 if (ret) {
134 IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
135 return ret;
138 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
139 return 0;
142 /* station table will be cleared */
143 priv->assoc_station_added = 0;
145 /* If we are currently associated and the new config requires
146 * an RXON_ASSOC and the new config wants the associated mask enabled,
147 * we must clear the associated from the active configuration
148 * before we apply the new config */
149 if (iwl_is_associated(priv) && new_assoc) {
150 IWL_DEBUG_INFO(priv, "Toggling associated bit on current RXON\n");
151 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
153 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
154 sizeof(struct iwl_rxon_cmd),
155 &priv->active_rxon);
157 /* If the mask clearing failed then we set
158 * active_rxon back to what it was previously */
159 if (ret) {
160 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
161 IWL_ERR(priv, "Error clearing ASSOC_MSK (%d)\n", ret);
162 return ret;
166 IWL_DEBUG_INFO(priv, "Sending RXON\n"
167 "* with%s RXON_FILTER_ASSOC_MSK\n"
168 "* channel = %d\n"
169 "* bssid = %pM\n",
170 (new_assoc ? "" : "out"),
171 le16_to_cpu(priv->staging_rxon.channel),
172 priv->staging_rxon.bssid_addr);
174 iwl_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
176 /* Apply the new configuration
177 * RXON unassoc clears the station table in uCode, send it before
178 * we add the bcast station. If assoc bit is set, we will send RXON
179 * after having added the bcast and bssid station.
181 if (!new_assoc) {
182 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
183 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
184 if (ret) {
185 IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
186 return ret;
188 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
191 iwl_clear_stations_table(priv);
193 if (!priv->error_recovering)
194 priv->start_calib = 0;
196 /* Add the broadcast address so we can send broadcast frames */
197 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
198 IWL_INVALID_STATION) {
199 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
200 return -EIO;
203 /* If we have set the ASSOC_MSK and we are in BSS mode then
204 * add the IWL_AP_ID to the station rate table */
205 if (new_assoc) {
206 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
207 ret = iwl_rxon_add_station(priv,
208 priv->active_rxon.bssid_addr, 1);
209 if (ret == IWL_INVALID_STATION) {
210 IWL_ERR(priv,
211 "Error adding AP address for TX.\n");
212 return -EIO;
214 priv->assoc_station_added = 1;
215 if (priv->default_wep_key &&
216 iwl_send_static_wepkey_cmd(priv, 0))
217 IWL_ERR(priv,
218 "Could not send WEP static key.\n");
221 /* Apply the new configuration
222 * RXON assoc doesn't clear the station table in uCode,
224 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
225 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
226 if (ret) {
227 IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
228 return ret;
230 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
233 iwl_init_sensitivity(priv);
235 /* If we issue a new RXON command which required a tune then we must
236 * send a new TXPOWER command or we won't be able to Tx any frames */
237 ret = iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
238 if (ret) {
239 IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
240 return ret;
243 return 0;
246 void iwl_update_chain_flags(struct iwl_priv *priv)
249 iwl_set_rxon_chain(priv);
250 iwl_commit_rxon(priv);
253 static void iwl_clear_free_frames(struct iwl_priv *priv)
255 struct list_head *element;
257 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
258 priv->frames_count);
260 while (!list_empty(&priv->free_frames)) {
261 element = priv->free_frames.next;
262 list_del(element);
263 kfree(list_entry(element, struct iwl_frame, list));
264 priv->frames_count--;
267 if (priv->frames_count) {
268 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
269 priv->frames_count);
270 priv->frames_count = 0;
274 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
276 struct iwl_frame *frame;
277 struct list_head *element;
278 if (list_empty(&priv->free_frames)) {
279 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
280 if (!frame) {
281 IWL_ERR(priv, "Could not allocate frame!\n");
282 return NULL;
285 priv->frames_count++;
286 return frame;
289 element = priv->free_frames.next;
290 list_del(element);
291 return list_entry(element, struct iwl_frame, list);
294 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
296 memset(frame, 0, sizeof(*frame));
297 list_add(&frame->list, &priv->free_frames);
300 static unsigned int iwl_fill_beacon_frame(struct iwl_priv *priv,
301 struct ieee80211_hdr *hdr,
302 int left)
304 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
305 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
306 (priv->iw_mode != NL80211_IFTYPE_AP)))
307 return 0;
309 if (priv->ibss_beacon->len > left)
310 return 0;
312 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
314 return priv->ibss_beacon->len;
317 static unsigned int iwl_hw_get_beacon_cmd(struct iwl_priv *priv,
318 struct iwl_frame *frame, u8 rate)
320 struct iwl_tx_beacon_cmd *tx_beacon_cmd;
321 unsigned int frame_size;
323 tx_beacon_cmd = &frame->u.beacon;
324 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
326 tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
327 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
329 frame_size = iwl_fill_beacon_frame(priv, tx_beacon_cmd->frame,
330 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
332 BUG_ON(frame_size > MAX_MPDU_SIZE);
333 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
335 if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
336 tx_beacon_cmd->tx.rate_n_flags =
337 iwl_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
338 else
339 tx_beacon_cmd->tx.rate_n_flags =
340 iwl_hw_set_rate_n_flags(rate, 0);
342 tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
343 TX_CMD_FLG_TSF_MSK |
344 TX_CMD_FLG_STA_RATE_MSK;
346 return sizeof(*tx_beacon_cmd) + frame_size;
348 static int iwl_send_beacon_cmd(struct iwl_priv *priv)
350 struct iwl_frame *frame;
351 unsigned int frame_size;
352 int rc;
353 u8 rate;
355 frame = iwl_get_free_frame(priv);
357 if (!frame) {
358 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
359 "command.\n");
360 return -ENOMEM;
363 rate = iwl_rate_get_lowest_plcp(priv);
365 frame_size = iwl_hw_get_beacon_cmd(priv, frame, rate);
367 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
368 &frame->u.cmd[0]);
370 iwl_free_frame(priv, frame);
372 return rc;
375 static inline dma_addr_t iwl_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
377 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
379 dma_addr_t addr = get_unaligned_le32(&tb->lo);
380 if (sizeof(dma_addr_t) > sizeof(u32))
381 addr |=
382 ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
384 return addr;
387 static inline u16 iwl_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
389 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
391 return le16_to_cpu(tb->hi_n_len) >> 4;
394 static inline void iwl_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
395 dma_addr_t addr, u16 len)
397 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
398 u16 hi_n_len = len << 4;
400 put_unaligned_le32(addr, &tb->lo);
401 if (sizeof(dma_addr_t) > sizeof(u32))
402 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
404 tb->hi_n_len = cpu_to_le16(hi_n_len);
406 tfd->num_tbs = idx + 1;
409 static inline u8 iwl_tfd_get_num_tbs(struct iwl_tfd *tfd)
411 return tfd->num_tbs & 0x1f;
415 * iwl_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
416 * @priv - driver private data
417 * @txq - tx queue
419 * Does NOT advance any TFD circular buffer read/write indexes
420 * Does NOT free the TFD itself (which is within circular buffer)
422 void iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
424 struct iwl_tfd *tfd_tmp = (struct iwl_tfd *)txq->tfds;
425 struct iwl_tfd *tfd;
426 struct pci_dev *dev = priv->pci_dev;
427 int index = txq->q.read_ptr;
428 int i;
429 int num_tbs;
431 tfd = &tfd_tmp[index];
433 /* Sanity check on number of chunks */
434 num_tbs = iwl_tfd_get_num_tbs(tfd);
436 if (num_tbs >= IWL_NUM_OF_TBS) {
437 IWL_ERR(priv, "Too many chunks: %i\n", num_tbs);
438 /* @todo issue fatal error, it is quite serious situation */
439 return;
442 /* Unmap tx_cmd */
443 if (num_tbs)
444 pci_unmap_single(dev,
445 pci_unmap_addr(&txq->cmd[index]->meta, mapping),
446 pci_unmap_len(&txq->cmd[index]->meta, len),
447 PCI_DMA_TODEVICE);
449 /* Unmap chunks, if any. */
450 for (i = 1; i < num_tbs; i++) {
451 pci_unmap_single(dev, iwl_tfd_tb_get_addr(tfd, i),
452 iwl_tfd_tb_get_len(tfd, i), PCI_DMA_TODEVICE);
454 if (txq->txb) {
455 dev_kfree_skb(txq->txb[txq->q.read_ptr].skb[i - 1]);
456 txq->txb[txq->q.read_ptr].skb[i - 1] = NULL;
461 int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv,
462 struct iwl_tx_queue *txq,
463 dma_addr_t addr, u16 len,
464 u8 reset, u8 pad)
466 struct iwl_queue *q;
467 struct iwl_tfd *tfd, *tfd_tmp;
468 u32 num_tbs;
470 q = &txq->q;
471 tfd_tmp = (struct iwl_tfd *)txq->tfds;
472 tfd = &tfd_tmp[q->write_ptr];
474 if (reset)
475 memset(tfd, 0, sizeof(*tfd));
477 num_tbs = iwl_tfd_get_num_tbs(tfd);
479 /* Each TFD can point to a maximum 20 Tx buffers */
480 if (num_tbs >= IWL_NUM_OF_TBS) {
481 IWL_ERR(priv, "Error can not send more than %d chunks\n",
482 IWL_NUM_OF_TBS);
483 return -EINVAL;
486 BUG_ON(addr & ~DMA_BIT_MASK(36));
487 if (unlikely(addr & ~IWL_TX_DMA_MASK))
488 IWL_ERR(priv, "Unaligned address = %llx\n",
489 (unsigned long long)addr);
491 iwl_tfd_set_tb(tfd, num_tbs, addr, len);
493 return 0;
497 * Tell nic where to find circular buffer of Tx Frame Descriptors for
498 * given Tx queue, and enable the DMA channel used for that queue.
500 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
501 * channels supported in hardware.
503 int iwl_hw_tx_queue_init(struct iwl_priv *priv,
504 struct iwl_tx_queue *txq)
506 int ret;
507 unsigned long flags;
508 int txq_id = txq->q.id;
510 spin_lock_irqsave(&priv->lock, flags);
511 ret = iwl_grab_nic_access(priv);
512 if (ret) {
513 spin_unlock_irqrestore(&priv->lock, flags);
514 return ret;
517 /* Circular buffer (TFD queue in DRAM) physical base address */
518 iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
519 txq->q.dma_addr >> 8);
521 iwl_release_nic_access(priv);
522 spin_unlock_irqrestore(&priv->lock, flags);
524 return 0;
528 /******************************************************************************
530 * Misc. internal state and helper functions
532 ******************************************************************************/
534 static void iwl_ht_conf(struct iwl_priv *priv,
535 struct ieee80211_bss_conf *bss_conf)
537 struct ieee80211_sta_ht_cap *ht_conf;
538 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
539 struct ieee80211_sta *sta;
541 IWL_DEBUG_MAC80211(priv, "enter: \n");
543 if (!iwl_conf->is_ht)
544 return;
548 * It is totally wrong to base global information on something
549 * that is valid only when associated, alas, this driver works
550 * that way and I don't know how to fix it.
553 rcu_read_lock();
554 sta = ieee80211_find_sta(priv->hw, priv->bssid);
555 if (!sta) {
556 rcu_read_unlock();
557 return;
559 ht_conf = &sta->ht_cap;
561 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
562 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
563 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
564 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
566 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
567 iwl_conf->max_amsdu_size =
568 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
570 iwl_conf->supported_chan_width =
571 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40);
574 * XXX: The HT configuration needs to be moved into iwl_mac_config()
575 * to be done there correctly.
578 iwl_conf->extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
579 if (conf_is_ht40_minus(&priv->hw->conf))
580 iwl_conf->extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
581 else if (conf_is_ht40_plus(&priv->hw->conf))
582 iwl_conf->extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
584 /* If no above or below channel supplied disable FAT channel */
585 if (iwl_conf->extension_chan_offset != IEEE80211_HT_PARAM_CHA_SEC_ABOVE &&
586 iwl_conf->extension_chan_offset != IEEE80211_HT_PARAM_CHA_SEC_BELOW)
587 iwl_conf->supported_chan_width = 0;
589 iwl_conf->sm_ps = (u8)((ht_conf->cap & IEEE80211_HT_CAP_SM_PS) >> 2);
591 memcpy(&iwl_conf->mcs, &ht_conf->mcs, 16);
593 iwl_conf->tx_chan_width = iwl_conf->supported_chan_width != 0;
594 iwl_conf->ht_protection =
595 bss_conf->ht.operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
596 iwl_conf->non_GF_STA_present =
597 !!(bss_conf->ht.operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
599 rcu_read_unlock();
601 IWL_DEBUG_MAC80211(priv, "leave\n");
605 * QoS support
607 static void iwl_activate_qos(struct iwl_priv *priv, u8 force)
609 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
610 return;
612 priv->qos_data.def_qos_parm.qos_flags = 0;
614 if (priv->qos_data.qos_cap.q_AP.queue_request &&
615 !priv->qos_data.qos_cap.q_AP.txop_request)
616 priv->qos_data.def_qos_parm.qos_flags |=
617 QOS_PARAM_FLG_TXOP_TYPE_MSK;
618 if (priv->qos_data.qos_active)
619 priv->qos_data.def_qos_parm.qos_flags |=
620 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
622 if (priv->current_ht_config.is_ht)
623 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
625 if (force || iwl_is_associated(priv)) {
626 IWL_DEBUG_QOS(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
627 priv->qos_data.qos_active,
628 priv->qos_data.def_qos_parm.qos_flags);
630 iwl_send_cmd_pdu_async(priv, REPLY_QOS_PARAM,
631 sizeof(struct iwl_qosparam_cmd),
632 &priv->qos_data.def_qos_parm, NULL);
636 #define MAX_UCODE_BEACON_INTERVAL 4096
638 static u16 iwl_adjust_beacon_interval(u16 beacon_val)
640 u16 new_val = 0;
641 u16 beacon_factor = 0;
643 beacon_factor = (beacon_val + MAX_UCODE_BEACON_INTERVAL)
644 / MAX_UCODE_BEACON_INTERVAL;
645 new_val = beacon_val / beacon_factor;
647 if (!new_val)
648 new_val = MAX_UCODE_BEACON_INTERVAL;
650 return new_val;
653 static void iwl_setup_rxon_timing(struct iwl_priv *priv)
655 u64 tsf;
656 s32 interval_tm, rem;
657 unsigned long flags;
658 struct ieee80211_conf *conf = NULL;
659 u16 beacon_int = 0;
661 conf = ieee80211_get_hw_conf(priv->hw);
663 spin_lock_irqsave(&priv->lock, flags);
664 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
665 priv->rxon_timing.listen_interval = cpu_to_le16(conf->listen_interval);
667 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
668 beacon_int = iwl_adjust_beacon_interval(priv->beacon_int);
669 priv->rxon_timing.atim_window = 0;
670 } else {
671 beacon_int = iwl_adjust_beacon_interval(conf->beacon_int);
673 /* TODO: we need to get atim_window from upper stack
674 * for now we set to 0 */
675 priv->rxon_timing.atim_window = 0;
678 priv->rxon_timing.beacon_interval = cpu_to_le16(beacon_int);
680 tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
681 interval_tm = beacon_int * 1024;
682 rem = do_div(tsf, interval_tm);
683 priv->rxon_timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
685 spin_unlock_irqrestore(&priv->lock, flags);
686 IWL_DEBUG_ASSOC(priv, "beacon interval %d beacon timer %d beacon tim %d\n",
687 le16_to_cpu(priv->rxon_timing.beacon_interval),
688 le32_to_cpu(priv->rxon_timing.beacon_init_val),
689 le16_to_cpu(priv->rxon_timing.atim_window));
692 static int iwl_set_mode(struct iwl_priv *priv, int mode)
694 iwl_connection_init_rx_config(priv, mode);
695 iwl_set_rxon_chain(priv);
696 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
698 iwl_clear_stations_table(priv);
700 /* dont commit rxon if rf-kill is on*/
701 if (!iwl_is_ready_rf(priv))
702 return -EAGAIN;
704 cancel_delayed_work(&priv->scan_check);
705 if (iwl_scan_cancel_timeout(priv, 100)) {
706 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
707 IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
708 return -EAGAIN;
711 iwl_commit_rxon(priv);
713 return 0;
716 /******************************************************************************
718 * Generic RX handler implementations
720 ******************************************************************************/
721 static void iwl_rx_reply_alive(struct iwl_priv *priv,
722 struct iwl_rx_mem_buffer *rxb)
724 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
725 struct iwl_alive_resp *palive;
726 struct delayed_work *pwork;
728 palive = &pkt->u.alive_frame;
730 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
731 "0x%01X 0x%01X\n",
732 palive->is_valid, palive->ver_type,
733 palive->ver_subtype);
735 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
736 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
737 memcpy(&priv->card_alive_init,
738 &pkt->u.alive_frame,
739 sizeof(struct iwl_init_alive_resp));
740 pwork = &priv->init_alive_start;
741 } else {
742 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
743 memcpy(&priv->card_alive, &pkt->u.alive_frame,
744 sizeof(struct iwl_alive_resp));
745 pwork = &priv->alive_start;
748 /* We delay the ALIVE response by 5ms to
749 * give the HW RF Kill time to activate... */
750 if (palive->is_valid == UCODE_VALID_OK)
751 queue_delayed_work(priv->workqueue, pwork,
752 msecs_to_jiffies(5));
753 else
754 IWL_WARN(priv, "uCode did not respond OK.\n");
757 static void iwl_rx_reply_error(struct iwl_priv *priv,
758 struct iwl_rx_mem_buffer *rxb)
760 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
762 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
763 "seq 0x%04X ser 0x%08X\n",
764 le32_to_cpu(pkt->u.err_resp.error_type),
765 get_cmd_string(pkt->u.err_resp.cmd_id),
766 pkt->u.err_resp.cmd_id,
767 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
768 le32_to_cpu(pkt->u.err_resp.error_info));
771 static void iwl_bg_beacon_update(struct work_struct *work)
773 struct iwl_priv *priv =
774 container_of(work, struct iwl_priv, beacon_update);
775 struct sk_buff *beacon;
777 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
778 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
780 if (!beacon) {
781 IWL_ERR(priv, "update beacon failed\n");
782 return;
785 mutex_lock(&priv->mutex);
786 /* new beacon skb is allocated every time; dispose previous.*/
787 if (priv->ibss_beacon)
788 dev_kfree_skb(priv->ibss_beacon);
790 priv->ibss_beacon = beacon;
791 mutex_unlock(&priv->mutex);
793 iwl_send_beacon_cmd(priv);
797 * iwl_bg_statistics_periodic - Timer callback to queue statistics
799 * This callback is provided in order to send a statistics request.
801 * This timer function is continually reset to execute within
802 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
803 * was received. We need to ensure we receive the statistics in order
804 * to update the temperature used for calibrating the TXPOWER.
806 static void iwl_bg_statistics_periodic(unsigned long data)
808 struct iwl_priv *priv = (struct iwl_priv *)data;
810 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
811 return;
813 /* dont send host command if rf-kill is on */
814 if (!iwl_is_ready_rf(priv))
815 return;
817 iwl_send_statistics_request(priv, CMD_ASYNC);
820 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
821 struct iwl_rx_mem_buffer *rxb)
823 #ifdef CONFIG_IWLWIFI_DEBUG
824 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
825 struct iwl4965_beacon_notif *beacon =
826 (struct iwl4965_beacon_notif *)pkt->u.raw;
827 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
829 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
830 "tsf %d %d rate %d\n",
831 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
832 beacon->beacon_notify_hdr.failure_frame,
833 le32_to_cpu(beacon->ibss_mgr_status),
834 le32_to_cpu(beacon->high_tsf),
835 le32_to_cpu(beacon->low_tsf), rate);
836 #endif
838 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
839 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
840 queue_work(priv->workqueue, &priv->beacon_update);
843 /* Handle notification from uCode that card's power state is changing
844 * due to software, hardware, or critical temperature RFKILL */
845 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
846 struct iwl_rx_mem_buffer *rxb)
848 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
849 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
850 unsigned long status = priv->status;
852 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
853 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
854 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
856 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
857 RF_CARD_DISABLED)) {
859 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
860 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
862 if (!iwl_grab_nic_access(priv)) {
863 iwl_write_direct32(
864 priv, HBUS_TARG_MBX_C,
865 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
867 iwl_release_nic_access(priv);
870 if (!(flags & RXON_CARD_DISABLED)) {
871 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
872 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
873 if (!iwl_grab_nic_access(priv)) {
874 iwl_write_direct32(
875 priv, HBUS_TARG_MBX_C,
876 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
878 iwl_release_nic_access(priv);
882 if (flags & RF_CARD_DISABLED) {
883 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
884 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
885 iwl_read32(priv, CSR_UCODE_DRV_GP1);
886 if (!iwl_grab_nic_access(priv))
887 iwl_release_nic_access(priv);
891 if (flags & HW_CARD_DISABLED)
892 set_bit(STATUS_RF_KILL_HW, &priv->status);
893 else
894 clear_bit(STATUS_RF_KILL_HW, &priv->status);
897 if (flags & SW_CARD_DISABLED)
898 set_bit(STATUS_RF_KILL_SW, &priv->status);
899 else
900 clear_bit(STATUS_RF_KILL_SW, &priv->status);
902 if (!(flags & RXON_CARD_DISABLED))
903 iwl_scan_cancel(priv);
905 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
906 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
907 (test_bit(STATUS_RF_KILL_SW, &status) !=
908 test_bit(STATUS_RF_KILL_SW, &priv->status)))
909 queue_work(priv->workqueue, &priv->rf_kill);
910 else
911 wake_up_interruptible(&priv->wait_command_queue);
914 int iwl_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
916 int ret;
917 unsigned long flags;
919 spin_lock_irqsave(&priv->lock, flags);
920 ret = iwl_grab_nic_access(priv);
921 if (ret)
922 goto err;
924 if (src == IWL_PWR_SRC_VAUX) {
925 if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
926 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
927 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
928 ~APMG_PS_CTRL_MSK_PWR_SRC);
929 } else {
930 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
931 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
932 ~APMG_PS_CTRL_MSK_PWR_SRC);
935 iwl_release_nic_access(priv);
936 err:
937 spin_unlock_irqrestore(&priv->lock, flags);
938 return ret;
942 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
944 * Setup the RX handlers for each of the reply types sent from the uCode
945 * to the host.
947 * This function chains into the hardware specific files for them to setup
948 * any hardware specific handlers as well.
950 static void iwl_setup_rx_handlers(struct iwl_priv *priv)
952 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
953 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
954 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
955 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
956 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
957 iwl_rx_pm_debug_statistics_notif;
958 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
961 * The same handler is used for both the REPLY to a discrete
962 * statistics request from the host as well as for the periodic
963 * statistics notifications (after received beacons) from the uCode.
965 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_rx_statistics;
966 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
968 iwl_setup_spectrum_handlers(priv);
969 iwl_setup_rx_scan_handlers(priv);
971 /* status change handler */
972 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
974 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
975 iwl_rx_missed_beacon_notif;
976 /* Rx handlers */
977 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
978 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
979 /* block ack */
980 priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl_rx_reply_compressed_ba;
981 /* Set up hardware specific Rx handlers */
982 priv->cfg->ops->lib->rx_handler_setup(priv);
986 * iwl_rx_handle - Main entry function for receiving responses from uCode
988 * Uses the priv->rx_handlers callback function array to invoke
989 * the appropriate handlers, including command responses,
990 * frame-received notifications, and other notifications.
992 void iwl_rx_handle(struct iwl_priv *priv)
994 struct iwl_rx_mem_buffer *rxb;
995 struct iwl_rx_packet *pkt;
996 struct iwl_rx_queue *rxq = &priv->rxq;
997 u32 r, i;
998 int reclaim;
999 unsigned long flags;
1000 u8 fill_rx = 0;
1001 u32 count = 8;
1003 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1004 * buffer that the driver may process (last buffer filled by ucode). */
1005 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
1006 i = rxq->read;
1008 /* Rx interrupt, but nothing sent from uCode */
1009 if (i == r)
1010 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
1012 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
1013 fill_rx = 1;
1015 while (i != r) {
1016 rxb = rxq->queue[i];
1018 /* If an RXB doesn't have a Rx queue slot associated with it,
1019 * then a bug has been introduced in the queue refilling
1020 * routines -- catch it here */
1021 BUG_ON(rxb == NULL);
1023 rxq->queue[i] = NULL;
1025 dma_sync_single_range_for_cpu(
1026 &priv->pci_dev->dev, rxb->real_dma_addr,
1027 rxb->aligned_dma_addr - rxb->real_dma_addr,
1028 priv->hw_params.rx_buf_size,
1029 PCI_DMA_FROMDEVICE);
1030 pkt = (struct iwl_rx_packet *)rxb->skb->data;
1032 /* Reclaim a command buffer only if this packet is a response
1033 * to a (driver-originated) command.
1034 * If the packet (e.g. Rx frame) originated from uCode,
1035 * there is no command buffer to reclaim.
1036 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1037 * but apparently a few don't get set; catch them here. */
1038 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1039 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
1040 (pkt->hdr.cmd != REPLY_RX) &&
1041 (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
1042 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
1043 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1044 (pkt->hdr.cmd != REPLY_TX);
1046 /* Based on type of command response or notification,
1047 * handle those that need handling via function in
1048 * rx_handlers table. See iwl_setup_rx_handlers() */
1049 if (priv->rx_handlers[pkt->hdr.cmd]) {
1050 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r,
1051 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1052 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1053 } else {
1054 /* No handling needed */
1055 IWL_DEBUG_RX(priv,
1056 "r %d i %d No handler needed for %s, 0x%02x\n",
1057 r, i, get_cmd_string(pkt->hdr.cmd),
1058 pkt->hdr.cmd);
1061 if (reclaim) {
1062 /* Invoke any callbacks, transfer the skb to caller, and
1063 * fire off the (possibly) blocking iwl_send_cmd()
1064 * as we reclaim the driver command queue */
1065 if (rxb && rxb->skb)
1066 iwl_tx_cmd_complete(priv, rxb);
1067 else
1068 IWL_WARN(priv, "Claim null rxb?\n");
1071 /* For now we just don't re-use anything. We can tweak this
1072 * later to try and re-use notification packets and SKBs that
1073 * fail to Rx correctly */
1074 if (rxb->skb != NULL) {
1075 priv->alloc_rxb_skb--;
1076 dev_kfree_skb_any(rxb->skb);
1077 rxb->skb = NULL;
1080 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
1081 priv->hw_params.rx_buf_size + 256,
1082 PCI_DMA_FROMDEVICE);
1083 spin_lock_irqsave(&rxq->lock, flags);
1084 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1085 spin_unlock_irqrestore(&rxq->lock, flags);
1086 i = (i + 1) & RX_QUEUE_MASK;
1087 /* If there are a lot of unused frames,
1088 * restock the Rx queue so ucode wont assert. */
1089 if (fill_rx) {
1090 count++;
1091 if (count >= 8) {
1092 priv->rxq.read = i;
1093 iwl_rx_queue_restock(priv);
1094 count = 0;
1099 /* Backtrack one entry */
1100 priv->rxq.read = i;
1101 iwl_rx_queue_restock(priv);
1104 /* call this function to flush any scheduled tasklet */
1105 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1107 /* wait to make sure we flush pending tasklet*/
1108 synchronize_irq(priv->pci_dev->irq);
1109 tasklet_kill(&priv->irq_tasklet);
1112 static void iwl_error_recovery(struct iwl_priv *priv)
1114 unsigned long flags;
1116 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
1117 sizeof(priv->staging_rxon));
1118 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1119 iwl_commit_rxon(priv);
1121 iwl_rxon_add_station(priv, priv->bssid, 1);
1123 spin_lock_irqsave(&priv->lock, flags);
1124 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
1125 priv->error_recovering = 0;
1126 spin_unlock_irqrestore(&priv->lock, flags);
1129 static void iwl_irq_tasklet(struct iwl_priv *priv)
1131 u32 inta, handled = 0;
1132 u32 inta_fh;
1133 unsigned long flags;
1134 #ifdef CONFIG_IWLWIFI_DEBUG
1135 u32 inta_mask;
1136 #endif
1138 spin_lock_irqsave(&priv->lock, flags);
1140 /* Ack/clear/reset pending uCode interrupts.
1141 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1142 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
1143 inta = iwl_read32(priv, CSR_INT);
1144 iwl_write32(priv, CSR_INT, inta);
1146 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1147 * Any new interrupts that happen after this, either while we're
1148 * in this tasklet, or later, will show up in next ISR/tasklet. */
1149 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1150 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
1152 #ifdef CONFIG_IWLWIFI_DEBUG
1153 if (priv->debug_level & IWL_DL_ISR) {
1154 /* just for debug */
1155 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1156 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1157 inta, inta_mask, inta_fh);
1159 #endif
1161 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1162 * atomic, make sure that inta covers all the interrupts that
1163 * we've discovered, even if FH interrupt came in just after
1164 * reading CSR_INT. */
1165 if (inta_fh & CSR49_FH_INT_RX_MASK)
1166 inta |= CSR_INT_BIT_FH_RX;
1167 if (inta_fh & CSR49_FH_INT_TX_MASK)
1168 inta |= CSR_INT_BIT_FH_TX;
1170 /* Now service all interrupt bits discovered above. */
1171 if (inta & CSR_INT_BIT_HW_ERR) {
1172 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
1174 /* Tell the device to stop sending interrupts */
1175 iwl_disable_interrupts(priv);
1177 iwl_irq_handle_error(priv);
1179 handled |= CSR_INT_BIT_HW_ERR;
1181 spin_unlock_irqrestore(&priv->lock, flags);
1183 return;
1186 #ifdef CONFIG_IWLWIFI_DEBUG
1187 if (priv->debug_level & (IWL_DL_ISR)) {
1188 /* NIC fires this, but we don't use it, redundant with WAKEUP */
1189 if (inta & CSR_INT_BIT_SCD)
1190 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
1191 "the frame/frames.\n");
1193 /* Alive notification via Rx interrupt will do the real work */
1194 if (inta & CSR_INT_BIT_ALIVE)
1195 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
1197 #endif
1198 /* Safely ignore these bits for debug checks below */
1199 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1201 /* HW RF KILL switch toggled */
1202 if (inta & CSR_INT_BIT_RF_KILL) {
1203 int hw_rf_kill = 0;
1204 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
1205 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1206 hw_rf_kill = 1;
1208 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
1209 hw_rf_kill ? "disable radio" : "enable radio");
1211 /* driver only loads ucode once setting the interface up.
1212 * the driver allows loading the ucode even if the radio
1213 * is killed. Hence update the killswitch state here. The
1214 * rfkill handler will care about restarting if needed.
1216 if (!test_bit(STATUS_ALIVE, &priv->status)) {
1217 if (hw_rf_kill)
1218 set_bit(STATUS_RF_KILL_HW, &priv->status);
1219 else
1220 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1221 queue_work(priv->workqueue, &priv->rf_kill);
1224 handled |= CSR_INT_BIT_RF_KILL;
1227 /* Chip got too hot and stopped itself */
1228 if (inta & CSR_INT_BIT_CT_KILL) {
1229 IWL_ERR(priv, "Microcode CT kill error detected.\n");
1230 handled |= CSR_INT_BIT_CT_KILL;
1233 /* Error detected by uCode */
1234 if (inta & CSR_INT_BIT_SW_ERR) {
1235 IWL_ERR(priv, "Microcode SW error detected. "
1236 " Restarting 0x%X.\n", inta);
1237 iwl_irq_handle_error(priv);
1238 handled |= CSR_INT_BIT_SW_ERR;
1241 /* uCode wakes up after power-down sleep */
1242 if (inta & CSR_INT_BIT_WAKEUP) {
1243 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
1244 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
1245 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1246 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1247 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1248 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1249 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1250 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
1252 handled |= CSR_INT_BIT_WAKEUP;
1255 /* All uCode command responses, including Tx command responses,
1256 * Rx "responses" (frame-received notification), and other
1257 * notifications from uCode come through here*/
1258 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1259 iwl_rx_handle(priv);
1260 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1263 if (inta & CSR_INT_BIT_FH_TX) {
1264 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
1265 handled |= CSR_INT_BIT_FH_TX;
1266 /* FH finished to write, send event */
1267 priv->ucode_write_complete = 1;
1268 wake_up_interruptible(&priv->wait_command_queue);
1271 if (inta & ~handled)
1272 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
1274 if (inta & ~CSR_INI_SET_MASK) {
1275 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
1276 inta & ~CSR_INI_SET_MASK);
1277 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
1280 /* Re-enable all interrupts */
1281 /* only Re-enable if diabled by irq */
1282 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1283 iwl_enable_interrupts(priv);
1285 #ifdef CONFIG_IWLWIFI_DEBUG
1286 if (priv->debug_level & (IWL_DL_ISR)) {
1287 inta = iwl_read32(priv, CSR_INT);
1288 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1289 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1290 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
1291 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1293 #endif
1294 spin_unlock_irqrestore(&priv->lock, flags);
1297 static irqreturn_t iwl_isr(int irq, void *data)
1299 struct iwl_priv *priv = data;
1300 u32 inta, inta_mask;
1301 u32 inta_fh;
1302 if (!priv)
1303 return IRQ_NONE;
1305 spin_lock(&priv->lock);
1307 /* Disable (but don't clear!) interrupts here to avoid
1308 * back-to-back ISRs and sporadic interrupts from our NIC.
1309 * If we have something to service, the tasklet will re-enable ints.
1310 * If we *don't* have something, we'll re-enable before leaving here. */
1311 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
1312 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
1314 /* Discover which interrupts are active/pending */
1315 inta = iwl_read32(priv, CSR_INT);
1316 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1318 /* Ignore interrupt if there's nothing in NIC to service.
1319 * This may be due to IRQ shared with another device,
1320 * or due to sporadic interrupts thrown from our NIC. */
1321 if (!inta && !inta_fh) {
1322 IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0, inta_fh == 0\n");
1323 goto none;
1326 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1327 /* Hardware disappeared. It might have already raised
1328 * an interrupt */
1329 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
1330 goto unplugged;
1333 IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1334 inta, inta_mask, inta_fh);
1336 inta &= ~CSR_INT_BIT_SCD;
1338 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1339 if (likely(inta || inta_fh))
1340 tasklet_schedule(&priv->irq_tasklet);
1342 unplugged:
1343 spin_unlock(&priv->lock);
1344 return IRQ_HANDLED;
1346 none:
1347 /* re-enable interrupts here since we don't have anything to service. */
1348 /* only Re-enable if diabled by irq */
1349 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1350 iwl_enable_interrupts(priv);
1351 spin_unlock(&priv->lock);
1352 return IRQ_NONE;
1355 /******************************************************************************
1357 * uCode download functions
1359 ******************************************************************************/
1361 static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
1363 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1364 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1365 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1366 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1367 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1368 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
1371 static void iwl_nic_start(struct iwl_priv *priv)
1373 /* Remove all resets to allow NIC to operate */
1374 iwl_write32(priv, CSR_RESET, 0);
1379 * iwl_read_ucode - Read uCode images from disk file.
1381 * Copy into buffers for card to fetch via bus-mastering
1383 static int iwl_read_ucode(struct iwl_priv *priv)
1385 struct iwl_ucode *ucode;
1386 int ret = -EINVAL, index;
1387 const struct firmware *ucode_raw;
1388 const char *name_pre = priv->cfg->fw_name_pre;
1389 const unsigned int api_max = priv->cfg->ucode_api_max;
1390 const unsigned int api_min = priv->cfg->ucode_api_min;
1391 char buf[25];
1392 u8 *src;
1393 size_t len;
1394 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
1396 /* Ask kernel firmware_class module to get the boot firmware off disk.
1397 * request_firmware() is synchronous, file is in memory on return. */
1398 for (index = api_max; index >= api_min; index--) {
1399 sprintf(buf, "%s%d%s", name_pre, index, ".ucode");
1400 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
1401 if (ret < 0) {
1402 IWL_ERR(priv, "%s firmware file req failed: %d\n",
1403 buf, ret);
1404 if (ret == -ENOENT)
1405 continue;
1406 else
1407 goto error;
1408 } else {
1409 if (index < api_max)
1410 IWL_ERR(priv, "Loaded firmware %s, "
1411 "which is deprecated. "
1412 "Please use API v%u instead.\n",
1413 buf, api_max);
1415 IWL_DEBUG_INFO(priv, "Got firmware '%s' file (%zd bytes) from disk\n",
1416 buf, ucode_raw->size);
1417 break;
1421 if (ret < 0)
1422 goto error;
1424 /* Make sure that we got at least our header! */
1425 if (ucode_raw->size < sizeof(*ucode)) {
1426 IWL_ERR(priv, "File size way too small!\n");
1427 ret = -EINVAL;
1428 goto err_release;
1431 /* Data from ucode file: header followed by uCode images */
1432 ucode = (void *)ucode_raw->data;
1434 priv->ucode_ver = le32_to_cpu(ucode->ver);
1435 api_ver = IWL_UCODE_API(priv->ucode_ver);
1436 inst_size = le32_to_cpu(ucode->inst_size);
1437 data_size = le32_to_cpu(ucode->data_size);
1438 init_size = le32_to_cpu(ucode->init_size);
1439 init_data_size = le32_to_cpu(ucode->init_data_size);
1440 boot_size = le32_to_cpu(ucode->boot_size);
1442 /* api_ver should match the api version forming part of the
1443 * firmware filename ... but we don't check for that and only rely
1444 * on the API version read from firware header from here on forward */
1446 if (api_ver < api_min || api_ver > api_max) {
1447 IWL_ERR(priv, "Driver unable to support your firmware API. "
1448 "Driver supports v%u, firmware is v%u.\n",
1449 api_max, api_ver);
1450 priv->ucode_ver = 0;
1451 ret = -EINVAL;
1452 goto err_release;
1454 if (api_ver != api_max)
1455 IWL_ERR(priv, "Firmware has old API version. Expected v%u, "
1456 "got v%u. New firmware can be obtained "
1457 "from http://www.intellinuxwireless.org.\n",
1458 api_max, api_ver);
1460 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
1461 IWL_UCODE_MAJOR(priv->ucode_ver),
1462 IWL_UCODE_MINOR(priv->ucode_ver),
1463 IWL_UCODE_API(priv->ucode_ver),
1464 IWL_UCODE_SERIAL(priv->ucode_ver));
1466 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
1467 priv->ucode_ver);
1468 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
1469 inst_size);
1470 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
1471 data_size);
1472 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
1473 init_size);
1474 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
1475 init_data_size);
1476 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
1477 boot_size);
1479 /* Verify size of file vs. image size info in file's header */
1480 if (ucode_raw->size < sizeof(*ucode) +
1481 inst_size + data_size + init_size +
1482 init_data_size + boot_size) {
1484 IWL_DEBUG_INFO(priv, "uCode file size %d too small\n",
1485 (int)ucode_raw->size);
1486 ret = -EINVAL;
1487 goto err_release;
1490 /* Verify that uCode images will fit in card's SRAM */
1491 if (inst_size > priv->hw_params.max_inst_size) {
1492 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
1493 inst_size);
1494 ret = -EINVAL;
1495 goto err_release;
1498 if (data_size > priv->hw_params.max_data_size) {
1499 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
1500 data_size);
1501 ret = -EINVAL;
1502 goto err_release;
1504 if (init_size > priv->hw_params.max_inst_size) {
1505 IWL_INFO(priv, "uCode init instr len %d too large to fit in\n",
1506 init_size);
1507 ret = -EINVAL;
1508 goto err_release;
1510 if (init_data_size > priv->hw_params.max_data_size) {
1511 IWL_INFO(priv, "uCode init data len %d too large to fit in\n",
1512 init_data_size);
1513 ret = -EINVAL;
1514 goto err_release;
1516 if (boot_size > priv->hw_params.max_bsm_size) {
1517 IWL_INFO(priv, "uCode boot instr len %d too large to fit in\n",
1518 boot_size);
1519 ret = -EINVAL;
1520 goto err_release;
1523 /* Allocate ucode buffers for card's bus-master loading ... */
1525 /* Runtime instructions and 2 copies of data:
1526 * 1) unmodified from disk
1527 * 2) backup cache for save/restore during power-downs */
1528 priv->ucode_code.len = inst_size;
1529 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
1531 priv->ucode_data.len = data_size;
1532 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
1534 priv->ucode_data_backup.len = data_size;
1535 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1537 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
1538 !priv->ucode_data_backup.v_addr)
1539 goto err_pci_alloc;
1541 /* Initialization instructions and data */
1542 if (init_size && init_data_size) {
1543 priv->ucode_init.len = init_size;
1544 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
1546 priv->ucode_init_data.len = init_data_size;
1547 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1549 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
1550 goto err_pci_alloc;
1553 /* Bootstrap (instructions only, no data) */
1554 if (boot_size) {
1555 priv->ucode_boot.len = boot_size;
1556 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
1558 if (!priv->ucode_boot.v_addr)
1559 goto err_pci_alloc;
1562 /* Copy images into buffers for card's bus-master reads ... */
1564 /* Runtime instructions (first block of data in file) */
1565 src = &ucode->data[0];
1566 len = priv->ucode_code.len;
1567 IWL_DEBUG_INFO(priv, "Copying (but not loading) uCode instr len %Zd\n", len);
1568 memcpy(priv->ucode_code.v_addr, src, len);
1569 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
1570 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
1572 /* Runtime data (2nd block)
1573 * NOTE: Copy into backup buffer will be done in iwl_up() */
1574 src = &ucode->data[inst_size];
1575 len = priv->ucode_data.len;
1576 IWL_DEBUG_INFO(priv, "Copying (but not loading) uCode data len %Zd\n", len);
1577 memcpy(priv->ucode_data.v_addr, src, len);
1578 memcpy(priv->ucode_data_backup.v_addr, src, len);
1580 /* Initialization instructions (3rd block) */
1581 if (init_size) {
1582 src = &ucode->data[inst_size + data_size];
1583 len = priv->ucode_init.len;
1584 IWL_DEBUG_INFO(priv, "Copying (but not loading) init instr len %Zd\n",
1585 len);
1586 memcpy(priv->ucode_init.v_addr, src, len);
1589 /* Initialization data (4th block) */
1590 if (init_data_size) {
1591 src = &ucode->data[inst_size + data_size + init_size];
1592 len = priv->ucode_init_data.len;
1593 IWL_DEBUG_INFO(priv, "Copying (but not loading) init data len %Zd\n",
1594 len);
1595 memcpy(priv->ucode_init_data.v_addr, src, len);
1598 /* Bootstrap instructions (5th block) */
1599 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
1600 len = priv->ucode_boot.len;
1601 IWL_DEBUG_INFO(priv, "Copying (but not loading) boot instr len %Zd\n", len);
1602 memcpy(priv->ucode_boot.v_addr, src, len);
1604 /* We have our copies now, allow OS release its copies */
1605 release_firmware(ucode_raw);
1606 return 0;
1608 err_pci_alloc:
1609 IWL_ERR(priv, "failed to allocate pci memory\n");
1610 ret = -ENOMEM;
1611 iwl_dealloc_ucode_pci(priv);
1613 err_release:
1614 release_firmware(ucode_raw);
1616 error:
1617 return ret;
1620 /* temporary */
1621 static int iwl_mac_beacon_update(struct ieee80211_hw *hw,
1622 struct sk_buff *skb);
1625 * iwl_alive_start - called after REPLY_ALIVE notification received
1626 * from protocol/runtime uCode (initialization uCode's
1627 * Alive gets handled by iwl_init_alive_start()).
1629 static void iwl_alive_start(struct iwl_priv *priv)
1631 int ret = 0;
1633 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
1635 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
1636 /* We had an error bringing up the hardware, so take it
1637 * all the way back down so we can try again */
1638 IWL_DEBUG_INFO(priv, "Alive failed.\n");
1639 goto restart;
1642 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
1643 * This is a paranoid check, because we would not have gotten the
1644 * "runtime" alive if code weren't properly loaded. */
1645 if (iwl_verify_ucode(priv)) {
1646 /* Runtime instruction load was bad;
1647 * take it all the way back down so we can try again */
1648 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
1649 goto restart;
1652 iwl_clear_stations_table(priv);
1653 ret = priv->cfg->ops->lib->alive_notify(priv);
1654 if (ret) {
1655 IWL_WARN(priv,
1656 "Could not complete ALIVE transition [ntf]: %d\n", ret);
1657 goto restart;
1660 /* After the ALIVE response, we can send host commands to the uCode */
1661 set_bit(STATUS_ALIVE, &priv->status);
1663 if (iwl_is_rfkill(priv))
1664 return;
1666 ieee80211_wake_queues(priv->hw);
1668 priv->active_rate = priv->rates_mask;
1669 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1671 if (iwl_is_associated(priv)) {
1672 struct iwl_rxon_cmd *active_rxon =
1673 (struct iwl_rxon_cmd *)&priv->active_rxon;
1675 memcpy(&priv->staging_rxon, &priv->active_rxon,
1676 sizeof(priv->staging_rxon));
1677 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1678 } else {
1679 /* Initialize our rx_config data */
1680 iwl_connection_init_rx_config(priv, priv->iw_mode);
1681 iwl_set_rxon_chain(priv);
1682 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1685 /* Configure Bluetooth device coexistence support */
1686 iwl_send_bt_config(priv);
1688 iwl_reset_run_time_calib(priv);
1690 /* Configure the adapter for unassociated operation */
1691 iwl_commit_rxon(priv);
1693 /* At this point, the NIC is initialized and operational */
1694 iwl_rf_kill_ct_config(priv);
1696 iwl_leds_register(priv);
1698 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
1699 set_bit(STATUS_READY, &priv->status);
1700 wake_up_interruptible(&priv->wait_command_queue);
1702 if (priv->error_recovering)
1703 iwl_error_recovery(priv);
1705 iwl_power_update_mode(priv, 1);
1707 /* reassociate for ADHOC mode */
1708 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
1709 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
1710 priv->vif);
1711 if (beacon)
1712 iwl_mac_beacon_update(priv->hw, beacon);
1716 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
1717 iwl_set_mode(priv, priv->iw_mode);
1719 return;
1721 restart:
1722 queue_work(priv->workqueue, &priv->restart);
1725 static void iwl_cancel_deferred_work(struct iwl_priv *priv);
1727 static void __iwl_down(struct iwl_priv *priv)
1729 unsigned long flags;
1730 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
1732 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
1734 if (!exit_pending)
1735 set_bit(STATUS_EXIT_PENDING, &priv->status);
1737 iwl_leds_unregister(priv);
1739 iwl_clear_stations_table(priv);
1741 /* Unblock any waiting calls */
1742 wake_up_interruptible_all(&priv->wait_command_queue);
1744 /* Wipe out the EXIT_PENDING status bit if we are not actually
1745 * exiting the module */
1746 if (!exit_pending)
1747 clear_bit(STATUS_EXIT_PENDING, &priv->status);
1749 /* stop and reset the on-board processor */
1750 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
1752 /* tell the device to stop sending interrupts */
1753 spin_lock_irqsave(&priv->lock, flags);
1754 iwl_disable_interrupts(priv);
1755 spin_unlock_irqrestore(&priv->lock, flags);
1756 iwl_synchronize_irq(priv);
1758 if (priv->mac80211_registered)
1759 ieee80211_stop_queues(priv->hw);
1761 /* If we have not previously called iwl_init() then
1762 * clear all bits but the RF Kill and SUSPEND bits and return */
1763 if (!iwl_is_init(priv)) {
1764 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
1765 STATUS_RF_KILL_HW |
1766 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
1767 STATUS_RF_KILL_SW |
1768 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
1769 STATUS_GEO_CONFIGURED |
1770 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
1771 STATUS_IN_SUSPEND |
1772 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
1773 STATUS_EXIT_PENDING;
1774 goto exit;
1777 /* ...otherwise clear out all the status bits but the RF Kill and
1778 * SUSPEND bits and continue taking the NIC down. */
1779 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
1780 STATUS_RF_KILL_HW |
1781 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
1782 STATUS_RF_KILL_SW |
1783 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
1784 STATUS_GEO_CONFIGURED |
1785 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
1786 STATUS_IN_SUSPEND |
1787 test_bit(STATUS_FW_ERROR, &priv->status) <<
1788 STATUS_FW_ERROR |
1789 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
1790 STATUS_EXIT_PENDING;
1792 spin_lock_irqsave(&priv->lock, flags);
1793 iwl_clear_bit(priv, CSR_GP_CNTRL,
1794 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1795 spin_unlock_irqrestore(&priv->lock, flags);
1797 iwl_txq_ctx_stop(priv);
1798 iwl_rxq_stop(priv);
1800 spin_lock_irqsave(&priv->lock, flags);
1801 if (!iwl_grab_nic_access(priv)) {
1802 iwl_write_prph(priv, APMG_CLK_DIS_REG,
1803 APMG_CLK_VAL_DMA_CLK_RQT);
1804 iwl_release_nic_access(priv);
1806 spin_unlock_irqrestore(&priv->lock, flags);
1808 udelay(5);
1810 /* FIXME: apm_ops.suspend(priv) */
1811 if (exit_pending || test_bit(STATUS_IN_SUSPEND, &priv->status))
1812 priv->cfg->ops->lib->apm_ops.stop(priv);
1813 else
1814 priv->cfg->ops->lib->apm_ops.reset(priv);
1815 exit:
1816 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
1818 if (priv->ibss_beacon)
1819 dev_kfree_skb(priv->ibss_beacon);
1820 priv->ibss_beacon = NULL;
1822 /* clear out any free frames */
1823 iwl_clear_free_frames(priv);
1826 static void iwl_down(struct iwl_priv *priv)
1828 mutex_lock(&priv->mutex);
1829 __iwl_down(priv);
1830 mutex_unlock(&priv->mutex);
1832 iwl_cancel_deferred_work(priv);
1835 #define MAX_HW_RESTARTS 5
1837 static int __iwl_up(struct iwl_priv *priv)
1839 int i;
1840 int ret;
1842 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1843 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
1844 return -EIO;
1847 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
1848 IWL_ERR(priv, "ucode not available for device bringup\n");
1849 return -EIO;
1852 /* If platform's RF_KILL switch is NOT set to KILL */
1853 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
1854 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1855 else
1856 set_bit(STATUS_RF_KILL_HW, &priv->status);
1858 if (iwl_is_rfkill(priv)) {
1859 iwl_enable_interrupts(priv);
1860 IWL_WARN(priv, "Radio disabled by %s RF Kill switch\n",
1861 test_bit(STATUS_RF_KILL_HW, &priv->status) ? "HW" : "SW");
1862 return 0;
1865 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
1867 ret = iwl_hw_nic_init(priv);
1868 if (ret) {
1869 IWL_ERR(priv, "Unable to init nic\n");
1870 return ret;
1873 /* make sure rfkill handshake bits are cleared */
1874 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1875 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
1876 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1878 /* clear (again), then enable host interrupts */
1879 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
1880 iwl_enable_interrupts(priv);
1882 /* really make sure rfkill handshake bits are cleared */
1883 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1884 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1886 /* Copy original ucode data image from disk into backup cache.
1887 * This will be used to initialize the on-board processor's
1888 * data SRAM for a clean start when the runtime program first loads. */
1889 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
1890 priv->ucode_data.len);
1892 for (i = 0; i < MAX_HW_RESTARTS; i++) {
1894 iwl_clear_stations_table(priv);
1896 /* load bootstrap state machine,
1897 * load bootstrap program into processor's memory,
1898 * prepare to load the "initialize" uCode */
1899 ret = priv->cfg->ops->lib->load_ucode(priv);
1901 if (ret) {
1902 IWL_ERR(priv, "Unable to set up bootstrap uCode: %d\n",
1903 ret);
1904 continue;
1907 /* Clear out the uCode error bit if it is set */
1908 clear_bit(STATUS_FW_ERROR, &priv->status);
1910 /* start card; "initialize" will load runtime ucode */
1911 iwl_nic_start(priv);
1913 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
1915 return 0;
1918 set_bit(STATUS_EXIT_PENDING, &priv->status);
1919 __iwl_down(priv);
1920 clear_bit(STATUS_EXIT_PENDING, &priv->status);
1922 /* tried to restart and config the device for as long as our
1923 * patience could withstand */
1924 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
1925 return -EIO;
1929 /*****************************************************************************
1931 * Workqueue callbacks
1933 *****************************************************************************/
1935 static void iwl_bg_init_alive_start(struct work_struct *data)
1937 struct iwl_priv *priv =
1938 container_of(data, struct iwl_priv, init_alive_start.work);
1940 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1941 return;
1943 mutex_lock(&priv->mutex);
1944 priv->cfg->ops->lib->init_alive_start(priv);
1945 mutex_unlock(&priv->mutex);
1948 static void iwl_bg_alive_start(struct work_struct *data)
1950 struct iwl_priv *priv =
1951 container_of(data, struct iwl_priv, alive_start.work);
1953 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1954 return;
1956 mutex_lock(&priv->mutex);
1957 iwl_alive_start(priv);
1958 mutex_unlock(&priv->mutex);
1961 static void iwl_bg_run_time_calib_work(struct work_struct *work)
1963 struct iwl_priv *priv = container_of(work, struct iwl_priv,
1964 run_time_calib_work);
1966 mutex_lock(&priv->mutex);
1968 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1969 test_bit(STATUS_SCANNING, &priv->status)) {
1970 mutex_unlock(&priv->mutex);
1971 return;
1974 if (priv->start_calib) {
1975 iwl_chain_noise_calibration(priv, &priv->statistics);
1977 iwl_sensitivity_calibration(priv, &priv->statistics);
1980 mutex_unlock(&priv->mutex);
1981 return;
1984 static void iwl_bg_up(struct work_struct *data)
1986 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
1988 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1989 return;
1991 mutex_lock(&priv->mutex);
1992 __iwl_up(priv);
1993 mutex_unlock(&priv->mutex);
1994 iwl_rfkill_set_hw_state(priv);
1997 static void iwl_bg_restart(struct work_struct *data)
1999 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
2001 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2002 return;
2004 iwl_down(priv);
2005 queue_work(priv->workqueue, &priv->up);
2008 static void iwl_bg_rx_replenish(struct work_struct *data)
2010 struct iwl_priv *priv =
2011 container_of(data, struct iwl_priv, rx_replenish);
2013 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2014 return;
2016 mutex_lock(&priv->mutex);
2017 iwl_rx_replenish(priv);
2018 mutex_unlock(&priv->mutex);
2021 #define IWL_DELAY_NEXT_SCAN (HZ*2)
2023 static void iwl_post_associate(struct iwl_priv *priv)
2025 struct ieee80211_conf *conf = NULL;
2026 int ret = 0;
2027 unsigned long flags;
2029 if (priv->iw_mode == NL80211_IFTYPE_AP) {
2030 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
2031 return;
2034 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
2035 priv->assoc_id, priv->active_rxon.bssid_addr);
2038 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2039 return;
2042 if (!priv->vif || !priv->is_open)
2043 return;
2045 iwl_power_cancel_timeout(priv);
2046 iwl_scan_cancel_timeout(priv, 200);
2048 conf = ieee80211_get_hw_conf(priv->hw);
2050 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2051 iwl_commit_rxon(priv);
2053 iwl_setup_rxon_timing(priv);
2054 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
2055 sizeof(priv->rxon_timing), &priv->rxon_timing);
2056 if (ret)
2057 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
2058 "Attempting to continue.\n");
2060 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2062 iwl_set_rxon_ht(priv, &priv->current_ht_config);
2064 iwl_set_rxon_chain(priv);
2065 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2067 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
2068 priv->assoc_id, priv->beacon_int);
2070 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2071 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2072 else
2073 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2075 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2076 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2077 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2078 else
2079 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2081 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
2082 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2086 iwl_commit_rxon(priv);
2088 switch (priv->iw_mode) {
2089 case NL80211_IFTYPE_STATION:
2090 break;
2092 case NL80211_IFTYPE_ADHOC:
2094 /* assume default assoc id */
2095 priv->assoc_id = 1;
2097 iwl_rxon_add_station(priv, priv->bssid, 0);
2098 iwl_send_beacon_cmd(priv);
2100 break;
2102 default:
2103 IWL_ERR(priv, "%s Should not be called in %d mode\n",
2104 __func__, priv->iw_mode);
2105 break;
2108 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
2109 priv->assoc_station_added = 1;
2111 spin_lock_irqsave(&priv->lock, flags);
2112 iwl_activate_qos(priv, 0);
2113 spin_unlock_irqrestore(&priv->lock, flags);
2115 /* the chain noise calibration will enabled PM upon completion
2116 * If chain noise has already been run, then we need to enable
2117 * power management here */
2118 if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
2119 iwl_power_enable_management(priv);
2121 /* Enable Rx differential gain and sensitivity calibrations */
2122 iwl_chain_noise_reset(priv);
2123 priv->start_calib = 1;
2127 /*****************************************************************************
2129 * mac80211 entry point functions
2131 *****************************************************************************/
2133 #define UCODE_READY_TIMEOUT (4 * HZ)
2135 static int iwl_mac_start(struct ieee80211_hw *hw)
2137 struct iwl_priv *priv = hw->priv;
2138 int ret;
2140 IWL_DEBUG_MAC80211(priv, "enter\n");
2142 /* we should be verifying the device is ready to be opened */
2143 mutex_lock(&priv->mutex);
2145 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
2146 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
2147 * ucode filename and max sizes are card-specific. */
2149 if (!priv->ucode_code.len) {
2150 ret = iwl_read_ucode(priv);
2151 if (ret) {
2152 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
2153 mutex_unlock(&priv->mutex);
2154 return ret;
2158 ret = __iwl_up(priv);
2160 mutex_unlock(&priv->mutex);
2162 iwl_rfkill_set_hw_state(priv);
2164 if (ret)
2165 return ret;
2167 if (iwl_is_rfkill(priv))
2168 goto out;
2170 IWL_DEBUG_INFO(priv, "Start UP work done.\n");
2172 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
2173 return 0;
2175 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
2176 * mac80211 will not be run successfully. */
2177 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
2178 test_bit(STATUS_READY, &priv->status),
2179 UCODE_READY_TIMEOUT);
2180 if (!ret) {
2181 if (!test_bit(STATUS_READY, &priv->status)) {
2182 IWL_ERR(priv, "START_ALIVE timeout after %dms.\n",
2183 jiffies_to_msecs(UCODE_READY_TIMEOUT));
2184 return -ETIMEDOUT;
2188 out:
2189 priv->is_open = 1;
2190 IWL_DEBUG_MAC80211(priv, "leave\n");
2191 return 0;
2194 static void iwl_mac_stop(struct ieee80211_hw *hw)
2196 struct iwl_priv *priv = hw->priv;
2198 IWL_DEBUG_MAC80211(priv, "enter\n");
2200 if (!priv->is_open) {
2201 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
2202 return;
2205 priv->is_open = 0;
2207 if (iwl_is_ready_rf(priv)) {
2208 /* stop mac, cancel any scan request and clear
2209 * RXON_FILTER_ASSOC_MSK BIT
2211 mutex_lock(&priv->mutex);
2212 iwl_scan_cancel_timeout(priv, 100);
2213 mutex_unlock(&priv->mutex);
2216 iwl_down(priv);
2218 flush_workqueue(priv->workqueue);
2220 /* enable interrupts again in order to receive rfkill changes */
2221 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2222 iwl_enable_interrupts(priv);
2224 IWL_DEBUG_MAC80211(priv, "leave\n");
2227 static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2229 struct iwl_priv *priv = hw->priv;
2231 IWL_DEBUG_MACDUMP(priv, "enter\n");
2233 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
2234 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
2236 if (iwl_tx_skb(priv, skb))
2237 dev_kfree_skb_any(skb);
2239 IWL_DEBUG_MACDUMP(priv, "leave\n");
2240 return NETDEV_TX_OK;
2243 static int iwl_mac_add_interface(struct ieee80211_hw *hw,
2244 struct ieee80211_if_init_conf *conf)
2246 struct iwl_priv *priv = hw->priv;
2247 unsigned long flags;
2249 IWL_DEBUG_MAC80211(priv, "enter: type %d\n", conf->type);
2251 if (priv->vif) {
2252 IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
2253 return -EOPNOTSUPP;
2256 spin_lock_irqsave(&priv->lock, flags);
2257 priv->vif = conf->vif;
2258 priv->iw_mode = conf->type;
2260 spin_unlock_irqrestore(&priv->lock, flags);
2262 mutex_lock(&priv->mutex);
2264 if (conf->mac_addr) {
2265 IWL_DEBUG_MAC80211(priv, "Set %pM\n", conf->mac_addr);
2266 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
2269 if (iwl_set_mode(priv, conf->type) == -EAGAIN)
2270 /* we are not ready, will run again when ready */
2271 set_bit(STATUS_MODE_PENDING, &priv->status);
2273 mutex_unlock(&priv->mutex);
2275 IWL_DEBUG_MAC80211(priv, "leave\n");
2276 return 0;
2280 * iwl_mac_config - mac80211 config callback
2282 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
2283 * be set inappropriately and the driver currently sets the hardware up to
2284 * use it whenever needed.
2286 static int iwl_mac_config(struct ieee80211_hw *hw, u32 changed)
2288 struct iwl_priv *priv = hw->priv;
2289 const struct iwl_channel_info *ch_info;
2290 struct ieee80211_conf *conf = &hw->conf;
2291 unsigned long flags;
2292 int ret = 0;
2293 u16 channel;
2295 mutex_lock(&priv->mutex);
2296 IWL_DEBUG_MAC80211(priv, "enter to channel %d\n", conf->channel->hw_value);
2298 priv->current_ht_config.is_ht = conf_is_ht(conf);
2300 if (conf->radio_enabled && iwl_radio_kill_sw_enable_radio(priv)) {
2301 IWL_DEBUG_MAC80211(priv, "leave - RF-KILL - waiting for uCode\n");
2302 goto out;
2305 if (!conf->radio_enabled)
2306 iwl_radio_kill_sw_disable_radio(priv);
2308 if (!iwl_is_ready(priv)) {
2309 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2310 ret = -EIO;
2311 goto out;
2314 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
2315 test_bit(STATUS_SCANNING, &priv->status))) {
2316 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
2317 mutex_unlock(&priv->mutex);
2318 return 0;
2321 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2322 ch_info = iwl_get_channel_info(priv, conf->channel->band, channel);
2323 if (!is_channel_valid(ch_info)) {
2324 IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
2325 ret = -EINVAL;
2326 goto out;
2329 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
2330 !is_channel_ibss(ch_info)) {
2331 IWL_ERR(priv, "channel %d in band %d not IBSS channel\n",
2332 conf->channel->hw_value, conf->channel->band);
2333 ret = -EINVAL;
2334 goto out;
2337 spin_lock_irqsave(&priv->lock, flags);
2340 /* if we are switching from ht to 2.4 clear flags
2341 * from any ht related info since 2.4 does not
2342 * support ht */
2343 if ((le16_to_cpu(priv->staging_rxon.channel) != channel)
2344 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
2345 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
2346 #endif
2348 priv->staging_rxon.flags = 0;
2350 iwl_set_rxon_channel(priv, conf->channel);
2352 iwl_set_flags_for_band(priv, conf->channel->band);
2354 /* The list of supported rates and rate mask can be different
2355 * for each band; since the band may have changed, reset
2356 * the rate mask to what mac80211 lists */
2357 iwl_set_rate(priv);
2359 spin_unlock_irqrestore(&priv->lock, flags);
2361 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
2362 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
2363 iwl_hw_channel_switch(priv, conf->channel);
2364 goto out;
2366 #endif
2368 if (!conf->radio_enabled) {
2369 IWL_DEBUG_MAC80211(priv, "leave - radio disabled\n");
2370 goto out;
2373 if (iwl_is_rfkill(priv)) {
2374 IWL_DEBUG_MAC80211(priv, "leave - RF kill\n");
2375 ret = -EIO;
2376 goto out;
2379 if (conf->flags & IEEE80211_CONF_PS)
2380 ret = iwl_power_set_user_mode(priv, IWL_POWER_INDEX_3);
2381 else
2382 ret = iwl_power_set_user_mode(priv, IWL_POWER_MODE_CAM);
2383 if (ret)
2384 IWL_DEBUG_MAC80211(priv, "Error setting power level\n");
2386 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
2387 priv->tx_power_user_lmt, conf->power_level);
2389 iwl_set_tx_power(priv, conf->power_level, false);
2391 iwl_set_rate(priv);
2393 /* call to ensure that 4965 rx_chain is set properly in monitor mode */
2394 iwl_set_rxon_chain(priv);
2396 if (memcmp(&priv->active_rxon,
2397 &priv->staging_rxon, sizeof(priv->staging_rxon)))
2398 iwl_commit_rxon(priv);
2399 else
2400 IWL_DEBUG_INFO(priv, "No re-sending same RXON configuration.\n");
2402 IWL_DEBUG_MAC80211(priv, "leave\n");
2404 out:
2405 mutex_unlock(&priv->mutex);
2406 return ret;
2409 static void iwl_config_ap(struct iwl_priv *priv)
2411 int ret = 0;
2412 unsigned long flags;
2414 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2415 return;
2417 /* The following should be done only at AP bring up */
2418 if (!iwl_is_associated(priv)) {
2420 /* RXON - unassoc (to set timing command) */
2421 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2422 iwl_commit_rxon(priv);
2424 /* RXON Timing */
2425 iwl_setup_rxon_timing(priv);
2426 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
2427 sizeof(priv->rxon_timing), &priv->rxon_timing);
2428 if (ret)
2429 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
2430 "Attempting to continue.\n");
2432 iwl_set_rxon_chain(priv);
2434 /* FIXME: what should be the assoc_id for AP? */
2435 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2436 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2437 priv->staging_rxon.flags |=
2438 RXON_FLG_SHORT_PREAMBLE_MSK;
2439 else
2440 priv->staging_rxon.flags &=
2441 ~RXON_FLG_SHORT_PREAMBLE_MSK;
2443 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2444 if (priv->assoc_capability &
2445 WLAN_CAPABILITY_SHORT_SLOT_TIME)
2446 priv->staging_rxon.flags |=
2447 RXON_FLG_SHORT_SLOT_MSK;
2448 else
2449 priv->staging_rxon.flags &=
2450 ~RXON_FLG_SHORT_SLOT_MSK;
2452 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
2453 priv->staging_rxon.flags &=
2454 ~RXON_FLG_SHORT_SLOT_MSK;
2456 /* restore RXON assoc */
2457 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2458 iwl_commit_rxon(priv);
2459 spin_lock_irqsave(&priv->lock, flags);
2460 iwl_activate_qos(priv, 1);
2461 spin_unlock_irqrestore(&priv->lock, flags);
2462 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
2464 iwl_send_beacon_cmd(priv);
2466 /* FIXME - we need to add code here to detect a totally new
2467 * configuration, reset the AP, unassoc, rxon timing, assoc,
2468 * clear sta table, add BCAST sta... */
2472 static int iwl_mac_config_interface(struct ieee80211_hw *hw,
2473 struct ieee80211_vif *vif,
2474 struct ieee80211_if_conf *conf)
2476 struct iwl_priv *priv = hw->priv;
2477 int rc;
2479 if (conf == NULL)
2480 return -EIO;
2482 if (priv->vif != vif) {
2483 IWL_DEBUG_MAC80211(priv, "leave - priv->vif != vif\n");
2484 return 0;
2487 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
2488 conf->changed & IEEE80211_IFCC_BEACON) {
2489 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
2490 if (!beacon)
2491 return -ENOMEM;
2492 mutex_lock(&priv->mutex);
2493 rc = iwl_mac_beacon_update(hw, beacon);
2494 mutex_unlock(&priv->mutex);
2495 if (rc)
2496 return rc;
2499 if (!iwl_is_alive(priv))
2500 return -EAGAIN;
2502 mutex_lock(&priv->mutex);
2504 if (conf->bssid)
2505 IWL_DEBUG_MAC80211(priv, "bssid: %pM\n", conf->bssid);
2508 * very dubious code was here; the probe filtering flag is never set:
2510 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
2511 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
2514 if (priv->iw_mode == NL80211_IFTYPE_AP) {
2515 if (!conf->bssid) {
2516 conf->bssid = priv->mac_addr;
2517 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
2518 IWL_DEBUG_MAC80211(priv, "bssid was set to: %pM\n",
2519 conf->bssid);
2521 if (priv->ibss_beacon)
2522 dev_kfree_skb(priv->ibss_beacon);
2524 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
2527 if (iwl_is_rfkill(priv))
2528 goto done;
2530 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
2531 !is_multicast_ether_addr(conf->bssid)) {
2532 /* If there is currently a HW scan going on in the background
2533 * then we need to cancel it else the RXON below will fail. */
2534 if (iwl_scan_cancel_timeout(priv, 100)) {
2535 IWL_WARN(priv, "Aborted scan still in progress "
2536 "after 100ms\n");
2537 IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
2538 mutex_unlock(&priv->mutex);
2539 return -EAGAIN;
2541 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
2543 /* TODO: Audit driver for usage of these members and see
2544 * if mac80211 deprecates them (priv->bssid looks like it
2545 * shouldn't be there, but I haven't scanned the IBSS code
2546 * to verify) - jpk */
2547 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
2549 if (priv->iw_mode == NL80211_IFTYPE_AP)
2550 iwl_config_ap(priv);
2551 else {
2552 rc = iwl_commit_rxon(priv);
2553 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
2554 iwl_rxon_add_station(
2555 priv, priv->active_rxon.bssid_addr, 1);
2558 } else {
2559 iwl_scan_cancel_timeout(priv, 100);
2560 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2561 iwl_commit_rxon(priv);
2564 done:
2565 IWL_DEBUG_MAC80211(priv, "leave\n");
2566 mutex_unlock(&priv->mutex);
2568 return 0;
2571 static void iwl_mac_remove_interface(struct ieee80211_hw *hw,
2572 struct ieee80211_if_init_conf *conf)
2574 struct iwl_priv *priv = hw->priv;
2576 IWL_DEBUG_MAC80211(priv, "enter\n");
2578 mutex_lock(&priv->mutex);
2580 if (iwl_is_ready_rf(priv)) {
2581 iwl_scan_cancel_timeout(priv, 100);
2582 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2583 iwl_commit_rxon(priv);
2585 if (priv->vif == conf->vif) {
2586 priv->vif = NULL;
2587 memset(priv->bssid, 0, ETH_ALEN);
2589 mutex_unlock(&priv->mutex);
2591 IWL_DEBUG_MAC80211(priv, "leave\n");
2595 #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
2596 static void iwl_bss_info_changed(struct ieee80211_hw *hw,
2597 struct ieee80211_vif *vif,
2598 struct ieee80211_bss_conf *bss_conf,
2599 u32 changes)
2601 struct iwl_priv *priv = hw->priv;
2603 IWL_DEBUG_MAC80211(priv, "changes = 0x%X\n", changes);
2605 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
2606 IWL_DEBUG_MAC80211(priv, "ERP_PREAMBLE %d\n",
2607 bss_conf->use_short_preamble);
2608 if (bss_conf->use_short_preamble)
2609 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2610 else
2611 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2614 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
2615 IWL_DEBUG_MAC80211(priv, "ERP_CTS %d\n", bss_conf->use_cts_prot);
2616 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
2617 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
2618 else
2619 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
2622 if (changes & BSS_CHANGED_HT) {
2623 iwl_ht_conf(priv, bss_conf);
2624 iwl_set_rxon_chain(priv);
2627 if (changes & BSS_CHANGED_ASSOC) {
2628 IWL_DEBUG_MAC80211(priv, "ASSOC %d\n", bss_conf->assoc);
2629 /* This should never happen as this function should
2630 * never be called from interrupt context. */
2631 if (WARN_ON_ONCE(in_interrupt()))
2632 return;
2633 if (bss_conf->assoc) {
2634 priv->assoc_id = bss_conf->aid;
2635 priv->beacon_int = bss_conf->beacon_int;
2636 priv->power_data.dtim_period = bss_conf->dtim_period;
2637 priv->timestamp = bss_conf->timestamp;
2638 priv->assoc_capability = bss_conf->assoc_capability;
2640 /* we have just associated, don't start scan too early
2641 * leave time for EAPOL exchange to complete
2643 priv->next_scan_jiffies = jiffies +
2644 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
2645 mutex_lock(&priv->mutex);
2646 iwl_post_associate(priv);
2647 mutex_unlock(&priv->mutex);
2648 } else {
2649 priv->assoc_id = 0;
2650 IWL_DEBUG_MAC80211(priv, "DISASSOC %d\n", bss_conf->assoc);
2652 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
2653 IWL_DEBUG_MAC80211(priv, "Associated Changes %d\n", changes);
2654 iwl_send_rxon_assoc(priv);
2659 static void iwl_mac_update_tkip_key(struct ieee80211_hw *hw,
2660 struct ieee80211_key_conf *keyconf, const u8 *addr,
2661 u32 iv32, u16 *phase1key)
2664 struct iwl_priv *priv = hw->priv;
2665 IWL_DEBUG_MAC80211(priv, "enter\n");
2667 iwl_update_tkip_key(priv, keyconf, addr, iv32, phase1key);
2669 IWL_DEBUG_MAC80211(priv, "leave\n");
2672 static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2673 struct ieee80211_vif *vif,
2674 struct ieee80211_sta *sta,
2675 struct ieee80211_key_conf *key)
2677 struct iwl_priv *priv = hw->priv;
2678 const u8 *addr;
2679 int ret;
2680 u8 sta_id;
2681 bool is_default_wep_key = false;
2683 IWL_DEBUG_MAC80211(priv, "enter\n");
2685 if (priv->hw_params.sw_crypto) {
2686 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
2687 return -EOPNOTSUPP;
2689 addr = sta ? sta->addr : iwl_bcast_addr;
2690 sta_id = iwl_find_station(priv, addr);
2691 if (sta_id == IWL_INVALID_STATION) {
2692 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
2693 addr);
2694 return -EINVAL;
2698 mutex_lock(&priv->mutex);
2699 iwl_scan_cancel_timeout(priv, 100);
2700 mutex_unlock(&priv->mutex);
2702 /* If we are getting WEP group key and we didn't receive any key mapping
2703 * so far, we are in legacy wep mode (group key only), otherwise we are
2704 * in 1X mode.
2705 * In legacy wep mode, we use another host command to the uCode */
2706 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
2707 priv->iw_mode != NL80211_IFTYPE_AP) {
2708 if (cmd == SET_KEY)
2709 is_default_wep_key = !priv->key_mapping_key;
2710 else
2711 is_default_wep_key =
2712 (key->hw_key_idx == HW_KEY_DEFAULT);
2715 switch (cmd) {
2716 case SET_KEY:
2717 if (is_default_wep_key)
2718 ret = iwl_set_default_wep_key(priv, key);
2719 else
2720 ret = iwl_set_dynamic_key(priv, key, sta_id);
2722 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
2723 break;
2724 case DISABLE_KEY:
2725 if (is_default_wep_key)
2726 ret = iwl_remove_default_wep_key(priv, key);
2727 else
2728 ret = iwl_remove_dynamic_key(priv, key, sta_id);
2730 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
2731 break;
2732 default:
2733 ret = -EINVAL;
2736 IWL_DEBUG_MAC80211(priv, "leave\n");
2738 return ret;
2741 static int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
2742 const struct ieee80211_tx_queue_params *params)
2744 struct iwl_priv *priv = hw->priv;
2745 unsigned long flags;
2746 int q;
2748 IWL_DEBUG_MAC80211(priv, "enter\n");
2750 if (!iwl_is_ready_rf(priv)) {
2751 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
2752 return -EIO;
2755 if (queue >= AC_NUM) {
2756 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
2757 return 0;
2760 q = AC_NUM - 1 - queue;
2762 spin_lock_irqsave(&priv->lock, flags);
2764 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
2765 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
2766 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
2767 priv->qos_data.def_qos_parm.ac[q].edca_txop =
2768 cpu_to_le16((params->txop * 32));
2770 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
2771 priv->qos_data.qos_active = 1;
2773 if (priv->iw_mode == NL80211_IFTYPE_AP)
2774 iwl_activate_qos(priv, 1);
2775 else if (priv->assoc_id && iwl_is_associated(priv))
2776 iwl_activate_qos(priv, 0);
2778 spin_unlock_irqrestore(&priv->lock, flags);
2780 IWL_DEBUG_MAC80211(priv, "leave\n");
2781 return 0;
2784 static int iwl_mac_ampdu_action(struct ieee80211_hw *hw,
2785 enum ieee80211_ampdu_mlme_action action,
2786 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2788 struct iwl_priv *priv = hw->priv;
2790 IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
2791 sta->addr, tid);
2793 if (!(priv->cfg->sku & IWL_SKU_N))
2794 return -EACCES;
2796 switch (action) {
2797 case IEEE80211_AMPDU_RX_START:
2798 IWL_DEBUG_HT(priv, "start Rx\n");
2799 return iwl_sta_rx_agg_start(priv, sta->addr, tid, *ssn);
2800 case IEEE80211_AMPDU_RX_STOP:
2801 IWL_DEBUG_HT(priv, "stop Rx\n");
2802 return iwl_sta_rx_agg_stop(priv, sta->addr, tid);
2803 case IEEE80211_AMPDU_TX_START:
2804 IWL_DEBUG_HT(priv, "start Tx\n");
2805 return iwl_tx_agg_start(priv, sta->addr, tid, ssn);
2806 case IEEE80211_AMPDU_TX_STOP:
2807 IWL_DEBUG_HT(priv, "stop Tx\n");
2808 return iwl_tx_agg_stop(priv, sta->addr, tid);
2809 default:
2810 IWL_DEBUG_HT(priv, "unknown\n");
2811 return -EINVAL;
2812 break;
2814 return 0;
2817 static int iwl_mac_get_tx_stats(struct ieee80211_hw *hw,
2818 struct ieee80211_tx_queue_stats *stats)
2820 struct iwl_priv *priv = hw->priv;
2821 int i, avail;
2822 struct iwl_tx_queue *txq;
2823 struct iwl_queue *q;
2824 unsigned long flags;
2826 IWL_DEBUG_MAC80211(priv, "enter\n");
2828 if (!iwl_is_ready_rf(priv)) {
2829 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
2830 return -EIO;
2833 spin_lock_irqsave(&priv->lock, flags);
2835 for (i = 0; i < AC_NUM; i++) {
2836 txq = &priv->txq[i];
2837 q = &txq->q;
2838 avail = iwl_queue_space(q);
2840 stats[i].len = q->n_window - avail;
2841 stats[i].limit = q->n_window - q->high_mark;
2842 stats[i].count = q->n_window;
2845 spin_unlock_irqrestore(&priv->lock, flags);
2847 IWL_DEBUG_MAC80211(priv, "leave\n");
2849 return 0;
2852 static int iwl_mac_get_stats(struct ieee80211_hw *hw,
2853 struct ieee80211_low_level_stats *stats)
2855 struct iwl_priv *priv = hw->priv;
2857 priv = hw->priv;
2858 IWL_DEBUG_MAC80211(priv, "enter\n");
2859 IWL_DEBUG_MAC80211(priv, "leave\n");
2861 return 0;
2864 static void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
2866 struct iwl_priv *priv = hw->priv;
2867 unsigned long flags;
2869 mutex_lock(&priv->mutex);
2870 IWL_DEBUG_MAC80211(priv, "enter\n");
2872 spin_lock_irqsave(&priv->lock, flags);
2873 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
2874 spin_unlock_irqrestore(&priv->lock, flags);
2876 iwl_reset_qos(priv);
2878 spin_lock_irqsave(&priv->lock, flags);
2879 priv->assoc_id = 0;
2880 priv->assoc_capability = 0;
2881 priv->assoc_station_added = 0;
2883 /* new association get rid of ibss beacon skb */
2884 if (priv->ibss_beacon)
2885 dev_kfree_skb(priv->ibss_beacon);
2887 priv->ibss_beacon = NULL;
2889 priv->beacon_int = priv->hw->conf.beacon_int;
2890 priv->timestamp = 0;
2891 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
2892 priv->beacon_int = 0;
2894 spin_unlock_irqrestore(&priv->lock, flags);
2896 if (!iwl_is_ready_rf(priv)) {
2897 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2898 mutex_unlock(&priv->mutex);
2899 return;
2902 /* we are restarting association process
2903 * clear RXON_FILTER_ASSOC_MSK bit
2905 if (priv->iw_mode != NL80211_IFTYPE_AP) {
2906 iwl_scan_cancel_timeout(priv, 100);
2907 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2908 iwl_commit_rxon(priv);
2911 iwl_power_update_mode(priv, 0);
2913 /* Per mac80211.h: This is only used in IBSS mode... */
2914 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
2916 /* switch to CAM during association period.
2917 * the ucode will block any association/authentication
2918 * frome during assiciation period if it can not hear
2919 * the AP because of PM. the timer enable PM back is
2920 * association do not complete
2922 if (priv->hw->conf.channel->flags & (IEEE80211_CHAN_PASSIVE_SCAN |
2923 IEEE80211_CHAN_RADAR))
2924 iwl_power_disable_management(priv, 3000);
2926 IWL_DEBUG_MAC80211(priv, "leave - not in IBSS\n");
2927 mutex_unlock(&priv->mutex);
2928 return;
2931 iwl_set_rate(priv);
2933 mutex_unlock(&priv->mutex);
2935 IWL_DEBUG_MAC80211(priv, "leave\n");
2938 static int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
2940 struct iwl_priv *priv = hw->priv;
2941 unsigned long flags;
2942 __le64 timestamp;
2944 IWL_DEBUG_MAC80211(priv, "enter\n");
2946 if (!iwl_is_ready_rf(priv)) {
2947 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
2948 return -EIO;
2951 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
2952 IWL_DEBUG_MAC80211(priv, "leave - not IBSS\n");
2953 return -EIO;
2956 spin_lock_irqsave(&priv->lock, flags);
2958 if (priv->ibss_beacon)
2959 dev_kfree_skb(priv->ibss_beacon);
2961 priv->ibss_beacon = skb;
2963 priv->assoc_id = 0;
2964 timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
2965 priv->timestamp = le64_to_cpu(timestamp);
2967 IWL_DEBUG_MAC80211(priv, "leave\n");
2968 spin_unlock_irqrestore(&priv->lock, flags);
2970 iwl_reset_qos(priv);
2972 iwl_post_associate(priv);
2975 return 0;
2978 /*****************************************************************************
2980 * sysfs attributes
2982 *****************************************************************************/
2984 #ifdef CONFIG_IWLWIFI_DEBUG
2987 * The following adds a new attribute to the sysfs representation
2988 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
2989 * used for controlling the debug level.
2991 * See the level definitions in iwl for details.
2994 static ssize_t show_debug_level(struct device *d,
2995 struct device_attribute *attr, char *buf)
2997 struct iwl_priv *priv = d->driver_data;
2999 return sprintf(buf, "0x%08X\n", priv->debug_level);
3001 static ssize_t store_debug_level(struct device *d,
3002 struct device_attribute *attr,
3003 const char *buf, size_t count)
3005 struct iwl_priv *priv = d->driver_data;
3006 unsigned long val;
3007 int ret;
3009 ret = strict_strtoul(buf, 0, &val);
3010 if (ret)
3011 IWL_ERR(priv, "%s is not in hex or decimal form.\n", buf);
3012 else
3013 priv->debug_level = val;
3015 return strnlen(buf, count);
3018 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3019 show_debug_level, store_debug_level);
3022 #endif /* CONFIG_IWLWIFI_DEBUG */
3025 static ssize_t show_version(struct device *d,
3026 struct device_attribute *attr, char *buf)
3028 struct iwl_priv *priv = d->driver_data;
3029 struct iwl_alive_resp *palive = &priv->card_alive;
3030 ssize_t pos = 0;
3031 u16 eeprom_ver;
3033 if (palive->is_valid)
3034 pos += sprintf(buf + pos,
3035 "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
3036 "fw type: 0x%01X 0x%01X\n",
3037 palive->ucode_major, palive->ucode_minor,
3038 palive->sw_rev[0], palive->sw_rev[1],
3039 palive->ver_type, palive->ver_subtype);
3040 else
3041 pos += sprintf(buf + pos, "fw not loaded\n");
3043 if (priv->eeprom) {
3044 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
3045 pos += sprintf(buf + pos, "EEPROM version: 0x%x\n",
3046 eeprom_ver);
3047 } else {
3048 pos += sprintf(buf + pos, "EEPROM not initialzed\n");
3051 return pos;
3054 static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
3056 static ssize_t show_temperature(struct device *d,
3057 struct device_attribute *attr, char *buf)
3059 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3061 if (!iwl_is_alive(priv))
3062 return -EAGAIN;
3064 return sprintf(buf, "%d\n", priv->temperature);
3067 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3069 static ssize_t show_tx_power(struct device *d,
3070 struct device_attribute *attr, char *buf)
3072 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3074 if (!iwl_is_ready_rf(priv))
3075 return sprintf(buf, "off\n");
3076 else
3077 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
3080 static ssize_t store_tx_power(struct device *d,
3081 struct device_attribute *attr,
3082 const char *buf, size_t count)
3084 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3085 unsigned long val;
3086 int ret;
3088 ret = strict_strtoul(buf, 10, &val);
3089 if (ret)
3090 IWL_INFO(priv, "%s is not in decimal form.\n", buf);
3091 else
3092 iwl_set_tx_power(priv, val, false);
3094 return count;
3097 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3099 static ssize_t show_flags(struct device *d,
3100 struct device_attribute *attr, char *buf)
3102 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3104 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
3107 static ssize_t store_flags(struct device *d,
3108 struct device_attribute *attr,
3109 const char *buf, size_t count)
3111 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3112 unsigned long val;
3113 u32 flags;
3114 int ret = strict_strtoul(buf, 0, &val);
3115 if (ret)
3116 return ret;
3117 flags = (u32)val;
3119 mutex_lock(&priv->mutex);
3120 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
3121 /* Cancel any currently running scans... */
3122 if (iwl_scan_cancel_timeout(priv, 100))
3123 IWL_WARN(priv, "Could not cancel scan.\n");
3124 else {
3125 IWL_DEBUG_INFO(priv, "Commit rxon.flags = 0x%04X\n", flags);
3126 priv->staging_rxon.flags = cpu_to_le32(flags);
3127 iwl_commit_rxon(priv);
3130 mutex_unlock(&priv->mutex);
3132 return count;
3135 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3137 static ssize_t show_filter_flags(struct device *d,
3138 struct device_attribute *attr, char *buf)
3140 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3142 return sprintf(buf, "0x%04X\n",
3143 le32_to_cpu(priv->active_rxon.filter_flags));
3146 static ssize_t store_filter_flags(struct device *d,
3147 struct device_attribute *attr,
3148 const char *buf, size_t count)
3150 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3151 unsigned long val;
3152 u32 filter_flags;
3153 int ret = strict_strtoul(buf, 0, &val);
3154 if (ret)
3155 return ret;
3156 filter_flags = (u32)val;
3158 mutex_lock(&priv->mutex);
3159 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
3160 /* Cancel any currently running scans... */
3161 if (iwl_scan_cancel_timeout(priv, 100))
3162 IWL_WARN(priv, "Could not cancel scan.\n");
3163 else {
3164 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
3165 "0x%04X\n", filter_flags);
3166 priv->staging_rxon.filter_flags =
3167 cpu_to_le32(filter_flags);
3168 iwl_commit_rxon(priv);
3171 mutex_unlock(&priv->mutex);
3173 return count;
3176 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3177 store_filter_flags);
3179 static ssize_t store_power_level(struct device *d,
3180 struct device_attribute *attr,
3181 const char *buf, size_t count)
3183 struct iwl_priv *priv = dev_get_drvdata(d);
3184 int ret;
3185 unsigned long mode;
3188 mutex_lock(&priv->mutex);
3190 if (!iwl_is_ready(priv)) {
3191 ret = -EAGAIN;
3192 goto out;
3195 ret = strict_strtoul(buf, 10, &mode);
3196 if (ret)
3197 goto out;
3199 ret = iwl_power_set_user_mode(priv, mode);
3200 if (ret) {
3201 IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
3202 goto out;
3204 ret = count;
3206 out:
3207 mutex_unlock(&priv->mutex);
3208 return ret;
3211 static ssize_t show_power_level(struct device *d,
3212 struct device_attribute *attr, char *buf)
3214 struct iwl_priv *priv = dev_get_drvdata(d);
3215 int mode = priv->power_data.user_power_setting;
3216 int system = priv->power_data.system_power_setting;
3217 int level = priv->power_data.power_mode;
3218 char *p = buf;
3220 switch (system) {
3221 case IWL_POWER_SYS_AUTO:
3222 p += sprintf(p, "SYSTEM:auto");
3223 break;
3224 case IWL_POWER_SYS_AC:
3225 p += sprintf(p, "SYSTEM:ac");
3226 break;
3227 case IWL_POWER_SYS_BATTERY:
3228 p += sprintf(p, "SYSTEM:battery");
3229 break;
3232 p += sprintf(p, "\tMODE:%s", (mode < IWL_POWER_AUTO) ?
3233 "fixed" : "auto");
3234 p += sprintf(p, "\tINDEX:%d", level);
3235 p += sprintf(p, "\n");
3236 return p - buf + 1;
3239 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
3240 store_power_level);
3243 static ssize_t show_statistics(struct device *d,
3244 struct device_attribute *attr, char *buf)
3246 struct iwl_priv *priv = dev_get_drvdata(d);
3247 u32 size = sizeof(struct iwl_notif_statistics);
3248 u32 len = 0, ofs = 0;
3249 u8 *data = (u8 *)&priv->statistics;
3250 int rc = 0;
3252 if (!iwl_is_alive(priv))
3253 return -EAGAIN;
3255 mutex_lock(&priv->mutex);
3256 rc = iwl_send_statistics_request(priv, 0);
3257 mutex_unlock(&priv->mutex);
3259 if (rc) {
3260 len = sprintf(buf,
3261 "Error sending statistics request: 0x%08X\n", rc);
3262 return len;
3265 while (size && (PAGE_SIZE - len)) {
3266 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3267 PAGE_SIZE - len, 1);
3268 len = strlen(buf);
3269 if (PAGE_SIZE - len)
3270 buf[len++] = '\n';
3272 ofs += 16;
3273 size -= min(size, 16U);
3276 return len;
3279 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
3282 /*****************************************************************************
3284 * driver setup and teardown
3286 *****************************************************************************/
3288 static void iwl_setup_deferred_work(struct iwl_priv *priv)
3290 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
3292 init_waitqueue_head(&priv->wait_command_queue);
3294 INIT_WORK(&priv->up, iwl_bg_up);
3295 INIT_WORK(&priv->restart, iwl_bg_restart);
3296 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
3297 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
3298 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
3299 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
3300 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
3301 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
3303 iwl_setup_scan_deferred_work(priv);
3304 iwl_setup_power_deferred_work(priv);
3306 if (priv->cfg->ops->lib->setup_deferred_work)
3307 priv->cfg->ops->lib->setup_deferred_work(priv);
3309 init_timer(&priv->statistics_periodic);
3310 priv->statistics_periodic.data = (unsigned long)priv;
3311 priv->statistics_periodic.function = iwl_bg_statistics_periodic;
3313 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
3314 iwl_irq_tasklet, (unsigned long)priv);
3317 static void iwl_cancel_deferred_work(struct iwl_priv *priv)
3319 if (priv->cfg->ops->lib->cancel_deferred_work)
3320 priv->cfg->ops->lib->cancel_deferred_work(priv);
3322 cancel_delayed_work_sync(&priv->init_alive_start);
3323 cancel_delayed_work(&priv->scan_check);
3324 cancel_delayed_work_sync(&priv->set_power_save);
3325 cancel_delayed_work(&priv->alive_start);
3326 cancel_work_sync(&priv->beacon_update);
3327 del_timer_sync(&priv->statistics_periodic);
3330 static struct attribute *iwl_sysfs_entries[] = {
3331 &dev_attr_flags.attr,
3332 &dev_attr_filter_flags.attr,
3333 &dev_attr_power_level.attr,
3334 &dev_attr_statistics.attr,
3335 &dev_attr_temperature.attr,
3336 &dev_attr_tx_power.attr,
3337 #ifdef CONFIG_IWLWIFI_DEBUG
3338 &dev_attr_debug_level.attr,
3339 #endif
3340 &dev_attr_version.attr,
3342 NULL
3345 static struct attribute_group iwl_attribute_group = {
3346 .name = NULL, /* put in device directory */
3347 .attrs = iwl_sysfs_entries,
3350 static struct ieee80211_ops iwl_hw_ops = {
3351 .tx = iwl_mac_tx,
3352 .start = iwl_mac_start,
3353 .stop = iwl_mac_stop,
3354 .add_interface = iwl_mac_add_interface,
3355 .remove_interface = iwl_mac_remove_interface,
3356 .config = iwl_mac_config,
3357 .config_interface = iwl_mac_config_interface,
3358 .configure_filter = iwl_configure_filter,
3359 .set_key = iwl_mac_set_key,
3360 .update_tkip_key = iwl_mac_update_tkip_key,
3361 .get_stats = iwl_mac_get_stats,
3362 .get_tx_stats = iwl_mac_get_tx_stats,
3363 .conf_tx = iwl_mac_conf_tx,
3364 .reset_tsf = iwl_mac_reset_tsf,
3365 .bss_info_changed = iwl_bss_info_changed,
3366 .ampdu_action = iwl_mac_ampdu_action,
3367 .hw_scan = iwl_mac_hw_scan
3370 static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3372 int err = 0;
3373 struct iwl_priv *priv;
3374 struct ieee80211_hw *hw;
3375 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
3376 unsigned long flags;
3377 u16 pci_cmd;
3379 /************************
3380 * 1. Allocating HW data
3381 ************************/
3383 /* Disabling hardware scan means that mac80211 will perform scans
3384 * "the hard way", rather than using device's scan. */
3385 if (cfg->mod_params->disable_hw_scan) {
3386 if (cfg->mod_params->debug & IWL_DL_INFO)
3387 dev_printk(KERN_DEBUG, &(pdev->dev),
3388 "Disabling hw_scan\n");
3389 iwl_hw_ops.hw_scan = NULL;
3392 hw = iwl_alloc_all(cfg, &iwl_hw_ops);
3393 if (!hw) {
3394 err = -ENOMEM;
3395 goto out;
3397 priv = hw->priv;
3398 /* At this point both hw and priv are allocated. */
3400 SET_IEEE80211_DEV(hw, &pdev->dev);
3402 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
3403 priv->cfg = cfg;
3404 priv->pci_dev = pdev;
3406 #ifdef CONFIG_IWLWIFI_DEBUG
3407 priv->debug_level = priv->cfg->mod_params->debug;
3408 atomic_set(&priv->restrict_refcnt, 0);
3409 #endif
3411 /**************************
3412 * 2. Initializing PCI bus
3413 **************************/
3414 if (pci_enable_device(pdev)) {
3415 err = -ENODEV;
3416 goto out_ieee80211_free_hw;
3419 pci_set_master(pdev);
3421 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
3422 if (!err)
3423 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
3424 if (err) {
3425 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3426 if (!err)
3427 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
3428 /* both attempts failed: */
3429 if (err) {
3430 IWL_WARN(priv, "No suitable DMA available.\n");
3431 goto out_pci_disable_device;
3435 err = pci_request_regions(pdev, DRV_NAME);
3436 if (err)
3437 goto out_pci_disable_device;
3439 pci_set_drvdata(pdev, priv);
3442 /***********************
3443 * 3. Read REV register
3444 ***********************/
3445 priv->hw_base = pci_iomap(pdev, 0, 0);
3446 if (!priv->hw_base) {
3447 err = -ENODEV;
3448 goto out_pci_release_regions;
3451 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
3452 (unsigned long long) pci_resource_len(pdev, 0));
3453 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
3455 iwl_hw_detect(priv);
3456 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s REV=0x%X\n",
3457 priv->cfg->name, priv->hw_rev);
3459 /* We disable the RETRY_TIMEOUT register (0x41) to keep
3460 * PCI Tx retries from interfering with C3 CPU state */
3461 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
3463 /* amp init */
3464 err = priv->cfg->ops->lib->apm_ops.init(priv);
3465 if (err < 0) {
3466 IWL_DEBUG_INFO(priv, "Failed to init APMG\n");
3467 goto out_iounmap;
3469 /*****************
3470 * 4. Read EEPROM
3471 *****************/
3472 /* Read the EEPROM */
3473 err = iwl_eeprom_init(priv);
3474 if (err) {
3475 IWL_ERR(priv, "Unable to init EEPROM\n");
3476 goto out_iounmap;
3478 err = iwl_eeprom_check_version(priv);
3479 if (err)
3480 goto out_iounmap;
3482 /* extract MAC Address */
3483 iwl_eeprom_get_mac(priv, priv->mac_addr);
3484 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
3485 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
3487 /************************
3488 * 5. Setup HW constants
3489 ************************/
3490 if (iwl_set_hw_params(priv)) {
3491 IWL_ERR(priv, "failed to set hw parameters\n");
3492 goto out_free_eeprom;
3495 /*******************
3496 * 6. Setup priv
3497 *******************/
3499 err = iwl_init_drv(priv);
3500 if (err)
3501 goto out_free_eeprom;
3502 /* At this point both hw and priv are initialized. */
3504 /**********************************
3505 * 7. Initialize module parameters
3506 **********************************/
3508 /* Disable radio (SW RF KILL) via parameter when loading driver */
3509 if (priv->cfg->mod_params->disable) {
3510 set_bit(STATUS_RF_KILL_SW, &priv->status);
3511 IWL_DEBUG_INFO(priv, "Radio disabled.\n");
3514 /********************
3515 * 8. Setup services
3516 ********************/
3517 spin_lock_irqsave(&priv->lock, flags);
3518 iwl_disable_interrupts(priv);
3519 spin_unlock_irqrestore(&priv->lock, flags);
3521 pci_enable_msi(priv->pci_dev);
3523 err = request_irq(priv->pci_dev->irq, iwl_isr, IRQF_SHARED,
3524 DRV_NAME, priv);
3525 if (err) {
3526 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
3527 goto out_disable_msi;
3529 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
3530 if (err) {
3531 IWL_ERR(priv, "failed to create sysfs device attributes\n");
3532 goto out_free_irq;
3535 iwl_setup_deferred_work(priv);
3536 iwl_setup_rx_handlers(priv);
3538 /**********************************
3539 * 9. Setup and register mac80211
3540 **********************************/
3542 /* enable interrupts if needed: hw bug w/a */
3543 pci_read_config_word(priv->pci_dev, PCI_COMMAND, &pci_cmd);
3544 if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
3545 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
3546 pci_write_config_word(priv->pci_dev, PCI_COMMAND, pci_cmd);
3549 iwl_enable_interrupts(priv);
3551 err = iwl_setup_mac(priv);
3552 if (err)
3553 goto out_remove_sysfs;
3555 err = iwl_dbgfs_register(priv, DRV_NAME);
3556 if (err)
3557 IWL_ERR(priv, "failed to create debugfs files\n");
3559 /* If platform's RF_KILL switch is NOT set to KILL */
3560 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3561 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3562 else
3563 set_bit(STATUS_RF_KILL_HW, &priv->status);
3565 err = iwl_rfkill_init(priv);
3566 if (err)
3567 IWL_ERR(priv, "Unable to initialize RFKILL system. "
3568 "Ignoring error: %d\n", err);
3569 else
3570 iwl_rfkill_set_hw_state(priv);
3572 iwl_power_initialize(priv);
3573 return 0;
3575 out_remove_sysfs:
3576 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
3577 out_free_irq:
3578 free_irq(priv->pci_dev->irq, priv);
3579 out_disable_msi:
3580 pci_disable_msi(priv->pci_dev);
3581 iwl_uninit_drv(priv);
3582 out_free_eeprom:
3583 iwl_eeprom_free(priv);
3584 out_iounmap:
3585 pci_iounmap(pdev, priv->hw_base);
3586 out_pci_release_regions:
3587 pci_release_regions(pdev);
3588 pci_set_drvdata(pdev, NULL);
3589 out_pci_disable_device:
3590 pci_disable_device(pdev);
3591 out_ieee80211_free_hw:
3592 ieee80211_free_hw(priv->hw);
3593 out:
3594 return err;
3597 static void __devexit iwl_pci_remove(struct pci_dev *pdev)
3599 struct iwl_priv *priv = pci_get_drvdata(pdev);
3600 unsigned long flags;
3602 if (!priv)
3603 return;
3605 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
3607 iwl_dbgfs_unregister(priv);
3608 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
3610 /* ieee80211_unregister_hw call wil cause iwl_mac_stop to
3611 * to be called and iwl_down since we are removing the device
3612 * we need to set STATUS_EXIT_PENDING bit.
3614 set_bit(STATUS_EXIT_PENDING, &priv->status);
3615 if (priv->mac80211_registered) {
3616 ieee80211_unregister_hw(priv->hw);
3617 priv->mac80211_registered = 0;
3618 } else {
3619 iwl_down(priv);
3622 /* make sure we flush any pending irq or
3623 * tasklet for the driver
3625 spin_lock_irqsave(&priv->lock, flags);
3626 iwl_disable_interrupts(priv);
3627 spin_unlock_irqrestore(&priv->lock, flags);
3629 iwl_synchronize_irq(priv);
3631 iwl_rfkill_unregister(priv);
3632 iwl_dealloc_ucode_pci(priv);
3634 if (priv->rxq.bd)
3635 iwl_rx_queue_free(priv, &priv->rxq);
3636 iwl_hw_txq_ctx_free(priv);
3638 iwl_clear_stations_table(priv);
3639 iwl_eeprom_free(priv);
3642 /*netif_stop_queue(dev); */
3643 flush_workqueue(priv->workqueue);
3645 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
3646 * priv->workqueue... so we can't take down the workqueue
3647 * until now... */
3648 destroy_workqueue(priv->workqueue);
3649 priv->workqueue = NULL;
3651 free_irq(priv->pci_dev->irq, priv);
3652 pci_disable_msi(priv->pci_dev);
3653 pci_iounmap(pdev, priv->hw_base);
3654 pci_release_regions(pdev);
3655 pci_disable_device(pdev);
3656 pci_set_drvdata(pdev, NULL);
3658 iwl_uninit_drv(priv);
3660 if (priv->ibss_beacon)
3661 dev_kfree_skb(priv->ibss_beacon);
3663 ieee80211_free_hw(priv->hw);
3666 #ifdef CONFIG_PM
3668 static int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
3670 struct iwl_priv *priv = pci_get_drvdata(pdev);
3672 if (priv->is_open) {
3673 set_bit(STATUS_IN_SUSPEND, &priv->status);
3674 iwl_mac_stop(priv->hw);
3675 priv->is_open = 1;
3678 pci_save_state(pdev);
3679 pci_disable_device(pdev);
3680 pci_set_power_state(pdev, PCI_D3hot);
3682 return 0;
3685 static int iwl_pci_resume(struct pci_dev *pdev)
3687 struct iwl_priv *priv = pci_get_drvdata(pdev);
3688 int ret;
3690 pci_set_power_state(pdev, PCI_D0);
3691 ret = pci_enable_device(pdev);
3692 if (ret)
3693 return ret;
3694 pci_restore_state(pdev);
3695 iwl_enable_interrupts(priv);
3697 if (priv->is_open)
3698 iwl_mac_start(priv->hw);
3700 clear_bit(STATUS_IN_SUSPEND, &priv->status);
3701 return 0;
3704 #endif /* CONFIG_PM */
3706 /*****************************************************************************
3708 * driver and module entry point
3710 *****************************************************************************/
3712 /* Hardware specific file defines the PCI IDs table for that hardware module */
3713 static struct pci_device_id iwl_hw_card_ids[] = {
3714 #ifdef CONFIG_IWL4965
3715 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
3716 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
3717 #endif /* CONFIG_IWL4965 */
3718 #ifdef CONFIG_IWL5000
3719 {IWL_PCI_DEVICE(0x4232, 0x1205, iwl5100_bg_cfg)},
3720 {IWL_PCI_DEVICE(0x4232, 0x1305, iwl5100_bg_cfg)},
3721 {IWL_PCI_DEVICE(0x4232, 0x1206, iwl5100_abg_cfg)},
3722 {IWL_PCI_DEVICE(0x4232, 0x1306, iwl5100_abg_cfg)},
3723 {IWL_PCI_DEVICE(0x4232, 0x1326, iwl5100_abg_cfg)},
3724 {IWL_PCI_DEVICE(0x4237, 0x1216, iwl5100_abg_cfg)},
3725 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
3726 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
3727 {IWL_PCI_DEVICE(0x4236, PCI_ANY_ID, iwl5300_agn_cfg)},
3728 {IWL_PCI_DEVICE(0x4237, PCI_ANY_ID, iwl5100_agn_cfg)},
3729 /* 5350 WiFi/WiMax */
3730 {IWL_PCI_DEVICE(0x423A, 0x1001, iwl5350_agn_cfg)},
3731 {IWL_PCI_DEVICE(0x423A, 0x1021, iwl5350_agn_cfg)},
3732 {IWL_PCI_DEVICE(0x423B, 0x1011, iwl5350_agn_cfg)},
3733 /* 5150 Wifi/WiMax */
3734 {IWL_PCI_DEVICE(0x423C, PCI_ANY_ID, iwl5150_agn_cfg)},
3735 {IWL_PCI_DEVICE(0x423D, PCI_ANY_ID, iwl5150_agn_cfg)},
3736 /* 6000/6050 Series */
3737 {IWL_PCI_DEVICE(0x0082, 0x1102, iwl6000_2ag_cfg)},
3738 {IWL_PCI_DEVICE(0x0085, 0x1112, iwl6000_2ag_cfg)},
3739 {IWL_PCI_DEVICE(0x0082, 0x1122, iwl6000_2ag_cfg)},
3740 {IWL_PCI_DEVICE(0x422B, PCI_ANY_ID, iwl6000_3agn_cfg)},
3741 {IWL_PCI_DEVICE(0x4238, PCI_ANY_ID, iwl6000_3agn_cfg)},
3742 {IWL_PCI_DEVICE(0x0082, PCI_ANY_ID, iwl6000_2agn_cfg)},
3743 {IWL_PCI_DEVICE(0x0085, PCI_ANY_ID, iwl6000_3agn_cfg)},
3744 {IWL_PCI_DEVICE(0x0086, PCI_ANY_ID, iwl6050_3agn_cfg)},
3745 {IWL_PCI_DEVICE(0x0087, PCI_ANY_ID, iwl6050_2agn_cfg)},
3746 {IWL_PCI_DEVICE(0x0088, PCI_ANY_ID, iwl6050_3agn_cfg)},
3747 {IWL_PCI_DEVICE(0x0089, PCI_ANY_ID, iwl6050_2agn_cfg)},
3748 /* 100 Series WiFi */
3749 {IWL_PCI_DEVICE(0x0083, PCI_ANY_ID, iwl100_bgn_cfg)},
3750 {IWL_PCI_DEVICE(0x0084, PCI_ANY_ID, iwl100_bgn_cfg)},
3751 #endif /* CONFIG_IWL5000 */
3755 MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
3757 static struct pci_driver iwl_driver = {
3758 .name = DRV_NAME,
3759 .id_table = iwl_hw_card_ids,
3760 .probe = iwl_pci_probe,
3761 .remove = __devexit_p(iwl_pci_remove),
3762 #ifdef CONFIG_PM
3763 .suspend = iwl_pci_suspend,
3764 .resume = iwl_pci_resume,
3765 #endif
3768 static int __init iwl_init(void)
3771 int ret;
3772 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
3773 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
3775 ret = iwlagn_rate_control_register();
3776 if (ret) {
3777 printk(KERN_ERR DRV_NAME
3778 "Unable to register rate control algorithm: %d\n", ret);
3779 return ret;
3782 ret = pci_register_driver(&iwl_driver);
3783 if (ret) {
3784 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
3785 goto error_register;
3788 return ret;
3790 error_register:
3791 iwlagn_rate_control_unregister();
3792 return ret;
3795 static void __exit iwl_exit(void)
3797 pci_unregister_driver(&iwl_driver);
3798 iwlagn_rate_control_unregister();
3801 module_exit(iwl_exit);
3802 module_init(iwl_init);