1 /******************************************************************************
3 * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *****************************************************************************/
31 * NOTE: This file (iwl-base.c) is used to build to multiple hardware targets
32 * by defining IWL to either 3945 or 4965. The Makefile used when building
33 * the base targets will create base-3945.o and base-4965.o
35 * The eventual goal is to move as many of the #if IWL / #endif blocks out of
36 * this file and into the hardware specific implementation files (iwl-XXXX.c)
37 * and leave only the common (non #ifdef sprinkled) code in this file
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/version.h>
43 #include <linux/init.h>
44 #include <linux/pci.h>
45 #include <linux/dma-mapping.h>
46 #include <linux/delay.h>
47 #include <linux/skbuff.h>
48 #include <linux/netdevice.h>
49 #include <linux/wireless.h>
50 #include <linux/firmware.h>
51 #include <linux/skbuff.h>
52 #include <linux/netdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/if_arp.h>
56 #include <net/ieee80211_radiotap.h>
57 #include <net/mac80211.h>
59 #include <asm/div64.h>
65 #include "iwl-helpers.h"
67 #ifdef CONFIG_IWLWIFI_DEBUG
71 /******************************************************************************
75 ******************************************************************************/
77 /* module parameters */
78 int iwl_param_disable_hw_scan;
80 int iwl_param_disable; /* def: enable radio */
81 int iwl_param_antenna; /* def: 0 = both antennas (use diversity) */
82 int iwl_param_hwcrypto; /* def: using software encryption */
83 int iwl_param_qos_enable = 1;
84 int iwl_param_queues_num = IWL_MAX_NUM_QUEUES;
87 * module name, copyright, version, etc.
88 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
91 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
93 #ifdef CONFIG_IWLWIFI_DEBUG
99 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
105 #define IWLWIFI_VERSION "0.1.15k" VD VS
106 #define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
107 #define DRV_VERSION IWLWIFI_VERSION
109 /* Change firmware file name, using "-" and incrementing number,
110 * *only* when uCode interface or architecture changes so that it
111 * is not compatible with earlier drivers.
112 * This number will also appear in << 8 position of 1st dword of uCode file */
113 #define IWL4965_UCODE_API "-1"
115 MODULE_DESCRIPTION(DRV_DESCRIPTION);
116 MODULE_VERSION(DRV_VERSION);
117 MODULE_AUTHOR(DRV_COPYRIGHT);
118 MODULE_LICENSE("GPL");
120 __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
122 u16 fc = le16_to_cpu(hdr->frame_control);
123 int hdr_len = ieee80211_get_hdrlen(fc);
125 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
126 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
130 static const struct ieee80211_hw_mode *iwl_get_hw_mode(
131 struct iwl_priv *priv, int mode)
135 for (i = 0; i < 3; i++)
136 if (priv->modes[i].mode == mode)
137 return &priv->modes[i];
142 static int iwl_is_empty_essid(const char *essid, int essid_len)
144 /* Single white space is for Linksys APs */
145 if (essid_len == 1 && essid[0] == ' ')
148 /* Otherwise, if the entire essid is 0, we assume it is hidden */
151 if (essid[essid_len] != '\0')
158 static const char *iwl_escape_essid(const char *essid, u8 essid_len)
160 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
161 const char *s = essid;
164 if (iwl_is_empty_essid(essid, essid_len)) {
165 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
169 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
170 while (essid_len--) {
182 static void iwl_print_hex_dump(int level, void *p, u32 len)
184 #ifdef CONFIG_IWLWIFI_DEBUG
185 if (!(iwl_debug_level & level))
188 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
193 /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
196 * Theory of operation
198 * A queue is a circular buffers with 'Read' and 'Write' pointers.
199 * 2 empty entries always kept in the buffer to protect from overflow.
201 * For Tx queue, there are low mark and high mark limits. If, after queuing
202 * the packet for Tx, free space become < low mark, Tx queue stopped. When
203 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
206 * The IWL operates with six queues, one receive queue in the device's
207 * sram, one transmit queue for sending commands to the device firmware,
208 * and four transmit queues for data.
209 ***************************************************/
211 static int iwl_queue_space(const struct iwl_queue *q)
213 int s = q->last_used - q->first_empty;
215 if (q->last_used > q->first_empty)
220 /* keep some reserve to not confuse empty and full situations */
227 /* XXX: n_bd must be power-of-two size */
228 static inline int iwl_queue_inc_wrap(int index, int n_bd)
230 return ++index & (n_bd - 1);
233 /* XXX: n_bd must be power-of-two size */
234 static inline int iwl_queue_dec_wrap(int index, int n_bd)
236 return --index & (n_bd - 1);
239 static inline int x2_queue_used(const struct iwl_queue *q, int i)
241 return q->first_empty > q->last_used ?
242 (i >= q->last_used && i < q->first_empty) :
243 !(i < q->last_used && i >= q->first_empty);
246 static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
251 return index & (q->n_window - 1);
254 static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
255 int count, int slots_num, u32 id)
258 q->n_window = slots_num;
261 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
262 * and iwl_queue_dec_wrap are broken. */
263 BUG_ON(!is_power_of_2(count));
265 /* slots_num must be power-of-two size, otherwise
266 * get_cmd_index is broken. */
267 BUG_ON(!is_power_of_2(slots_num));
269 q->low_mark = q->n_window / 4;
273 q->high_mark = q->n_window / 8;
274 if (q->high_mark < 2)
277 q->first_empty = q->last_used = 0;
282 static int iwl_tx_queue_alloc(struct iwl_priv *priv,
283 struct iwl_tx_queue *txq, u32 id)
285 struct pci_dev *dev = priv->pci_dev;
287 if (id != IWL_CMD_QUEUE_NUM) {
288 txq->txb = kmalloc(sizeof(txq->txb[0]) *
289 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
291 IWL_ERROR("kmalloc for auxilary BD "
292 "structures failed\n");
298 txq->bd = pci_alloc_consistent(dev,
299 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
303 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
304 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
320 int iwl_tx_queue_init(struct iwl_priv *priv,
321 struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
323 struct pci_dev *dev = priv->pci_dev;
327 /* alocate command space + one big command for scan since scan
328 * command is very huge the system will not have two scan at the
330 len = sizeof(struct iwl_cmd) * slots_num;
331 if (txq_id == IWL_CMD_QUEUE_NUM)
332 len += IWL_MAX_SCAN_SIZE;
333 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
337 rc = iwl_tx_queue_alloc(priv, txq, txq_id);
339 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
343 txq->need_update = 0;
345 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
346 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
347 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
348 iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
350 iwl_hw_tx_queue_init(priv, txq);
356 * iwl_tx_queue_free - Deallocate DMA queue.
357 * @txq: Transmit queue to deallocate.
359 * Empty queue by removing and destroying all BD's.
360 * Free all buffers. txq itself is not freed.
363 void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
365 struct iwl_queue *q = &txq->q;
366 struct pci_dev *dev = priv->pci_dev;
372 /* first, empty all BD's */
373 for (; q->first_empty != q->last_used;
374 q->last_used = iwl_queue_inc_wrap(q->last_used, q->n_bd))
375 iwl_hw_txq_free_tfd(priv, txq);
377 len = sizeof(struct iwl_cmd) * q->n_window;
378 if (q->id == IWL_CMD_QUEUE_NUM)
379 len += IWL_MAX_SCAN_SIZE;
381 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
383 /* free buffers belonging to queue itself */
385 pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
386 txq->q.n_bd, txq->bd, txq->q.dma_addr);
393 /* 0 fill whole structure */
394 memset(txq, 0, sizeof(*txq));
397 const u8 BROADCAST_ADDR[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
399 /*************** STATION TABLE MANAGEMENT ****
401 * NOTE: This needs to be overhauled to better synchronize between
402 * how the iwl-4965.c is using iwl_hw_find_station vs. iwl-3945.c
404 * mac80211 should also be examined to determine if sta_info is duplicating
405 * the functionality provided here
408 /**************************************************************/
410 #if 0 /* temparary disable till we add real remove station */
411 static u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
413 int index = IWL_INVALID_STATION;
417 spin_lock_irqsave(&priv->sta_lock, flags);
421 else if (is_broadcast_ether_addr(addr))
422 index = priv->hw_setting.bcast_sta_id;
424 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
425 if (priv->stations[i].used &&
426 !compare_ether_addr(priv->stations[i].sta.sta.addr,
432 if (unlikely(index == IWL_INVALID_STATION))
435 if (priv->stations[index].used) {
436 priv->stations[index].used = 0;
437 priv->num_stations--;
440 BUG_ON(priv->num_stations < 0);
443 spin_unlock_irqrestore(&priv->sta_lock, flags);
448 static void iwl_clear_stations_table(struct iwl_priv *priv)
452 spin_lock_irqsave(&priv->sta_lock, flags);
454 priv->num_stations = 0;
455 memset(priv->stations, 0, sizeof(priv->stations));
457 spin_unlock_irqrestore(&priv->sta_lock, flags);
460 u8 iwl_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
463 int index = IWL_INVALID_STATION;
464 struct iwl_station_entry *station;
465 unsigned long flags_spin;
466 DECLARE_MAC_BUF(mac);
468 spin_lock_irqsave(&priv->sta_lock, flags_spin);
471 else if (is_broadcast_ether_addr(addr))
472 index = priv->hw_setting.bcast_sta_id;
474 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
475 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
481 if (!priv->stations[i].used &&
482 index == IWL_INVALID_STATION)
487 /* These twh conditions has the same outcome but keep them separate
488 since they have different meaning */
489 if (unlikely(index == IWL_INVALID_STATION)) {
490 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
494 if (priv->stations[index].used &&
495 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
496 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
501 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
502 station = &priv->stations[index];
504 priv->num_stations++;
506 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
507 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
508 station->sta.mode = 0;
509 station->sta.sta.sta_id = index;
510 station->sta.station_flags = 0;
512 #ifdef CONFIG_IWLWIFI_HT
513 /* BCAST station and IBSS stations do not work in HT mode */
514 if (index != priv->hw_setting.bcast_sta_id &&
515 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
516 iwl4965_set_ht_add_station(priv, index);
517 #endif /*CONFIG_IWLWIFI_HT*/
519 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
520 iwl_send_add_station(priv, &station->sta, flags);
525 /*************** DRIVER STATUS FUNCTIONS *****/
527 static inline int iwl_is_ready(struct iwl_priv *priv)
529 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
530 * set but EXIT_PENDING is not */
531 return test_bit(STATUS_READY, &priv->status) &&
532 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
533 !test_bit(STATUS_EXIT_PENDING, &priv->status);
536 static inline int iwl_is_alive(struct iwl_priv *priv)
538 return test_bit(STATUS_ALIVE, &priv->status);
541 static inline int iwl_is_init(struct iwl_priv *priv)
543 return test_bit(STATUS_INIT, &priv->status);
546 static inline int iwl_is_rfkill(struct iwl_priv *priv)
548 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
549 test_bit(STATUS_RF_KILL_SW, &priv->status);
552 static inline int iwl_is_ready_rf(struct iwl_priv *priv)
555 if (iwl_is_rfkill(priv))
558 return iwl_is_ready(priv);
561 /*************** HOST COMMAND QUEUE FUNCTIONS *****/
563 #define IWL_CMD(x) case x : return #x
565 static const char *get_cmd_string(u8 cmd)
568 IWL_CMD(REPLY_ALIVE);
569 IWL_CMD(REPLY_ERROR);
571 IWL_CMD(REPLY_RXON_ASSOC);
572 IWL_CMD(REPLY_QOS_PARAM);
573 IWL_CMD(REPLY_RXON_TIMING);
574 IWL_CMD(REPLY_ADD_STA);
575 IWL_CMD(REPLY_REMOVE_STA);
576 IWL_CMD(REPLY_REMOVE_ALL_STA);
578 IWL_CMD(REPLY_RATE_SCALE);
579 IWL_CMD(REPLY_LEDS_CMD);
580 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
581 IWL_CMD(RADAR_NOTIFICATION);
582 IWL_CMD(REPLY_QUIET_CMD);
583 IWL_CMD(REPLY_CHANNEL_SWITCH);
584 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
585 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
586 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
587 IWL_CMD(POWER_TABLE_CMD);
588 IWL_CMD(PM_SLEEP_NOTIFICATION);
589 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
590 IWL_CMD(REPLY_SCAN_CMD);
591 IWL_CMD(REPLY_SCAN_ABORT_CMD);
592 IWL_CMD(SCAN_START_NOTIFICATION);
593 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
594 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
595 IWL_CMD(BEACON_NOTIFICATION);
596 IWL_CMD(REPLY_TX_BEACON);
597 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
598 IWL_CMD(QUIET_NOTIFICATION);
599 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
600 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
601 IWL_CMD(REPLY_BT_CONFIG);
602 IWL_CMD(REPLY_STATISTICS_CMD);
603 IWL_CMD(STATISTICS_NOTIFICATION);
604 IWL_CMD(REPLY_CARD_STATE_CMD);
605 IWL_CMD(CARD_STATE_NOTIFICATION);
606 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
607 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
608 IWL_CMD(SENSITIVITY_CMD);
609 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
610 IWL_CMD(REPLY_RX_PHY_CMD);
611 IWL_CMD(REPLY_RX_MPDU_CMD);
612 IWL_CMD(REPLY_4965_RX);
613 IWL_CMD(REPLY_COMPRESSED_BA);
620 #define HOST_COMPLETE_TIMEOUT (HZ / 2)
623 * iwl_enqueue_hcmd - enqueue a uCode command
624 * @priv: device private data point
625 * @cmd: a point to the ucode command structure
627 * The function returns < 0 values to indicate the operation is
628 * failed. On success, it turns the index (> 0) of command in the
631 static int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
633 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
634 struct iwl_queue *q = &txq->q;
635 struct iwl_tfd_frame *tfd;
637 struct iwl_cmd *out_cmd;
639 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
640 dma_addr_t phys_addr;
644 /* If any of the command structures end up being larger than
645 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
646 * we will need to increase the size of the TFD entries */
647 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
648 !(cmd->meta.flags & CMD_SIZE_HUGE));
650 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
651 IWL_ERROR("No space for Tx\n");
655 spin_lock_irqsave(&priv->hcmd_lock, flags);
657 tfd = &txq->bd[q->first_empty];
658 memset(tfd, 0, sizeof(*tfd));
660 control_flags = (u32 *) tfd;
662 idx = get_cmd_index(q, q->first_empty, cmd->meta.flags & CMD_SIZE_HUGE);
663 out_cmd = &txq->cmd[idx];
665 out_cmd->hdr.cmd = cmd->id;
666 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
667 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
669 /* At this point, the out_cmd now has all of the incoming cmd
672 out_cmd->hdr.flags = 0;
673 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
674 INDEX_TO_SEQ(q->first_empty));
675 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
676 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
678 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
679 offsetof(struct iwl_cmd, hdr);
680 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
682 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
683 "%d bytes at %d[%d]:%d\n",
684 get_cmd_string(out_cmd->hdr.cmd),
685 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
686 fix_size, q->first_empty, idx, IWL_CMD_QUEUE_NUM);
688 txq->need_update = 1;
689 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
690 q->first_empty = iwl_queue_inc_wrap(q->first_empty, q->n_bd);
691 iwl_tx_queue_update_write_ptr(priv, txq);
693 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
694 return ret ? ret : idx;
697 int iwl_send_cmd_async(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
701 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
703 /* An asynchronous command can not expect an SKB to be set. */
704 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
706 /* An asynchronous command MUST have a callback. */
707 BUG_ON(!cmd->meta.u.callback);
709 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
712 ret = iwl_enqueue_hcmd(priv, cmd);
714 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
715 get_cmd_string(cmd->id), ret);
721 int iwl_send_cmd_sync(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
725 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
727 BUG_ON(cmd->meta.flags & CMD_ASYNC);
729 /* A synchronous command can not have a callback set. */
730 BUG_ON(cmd->meta.u.callback != NULL);
732 if (atomic_xchg(&entry, 1)) {
733 IWL_ERROR("Error sending %s: Already sending a host command\n",
734 get_cmd_string(cmd->id));
738 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
740 if (cmd->meta.flags & CMD_WANT_SKB)
741 cmd->meta.source = &cmd->meta;
743 cmd_idx = iwl_enqueue_hcmd(priv, cmd);
746 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
747 get_cmd_string(cmd->id), ret);
751 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
752 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
753 HOST_COMPLETE_TIMEOUT);
755 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
756 IWL_ERROR("Error sending %s: time out after %dms.\n",
757 get_cmd_string(cmd->id),
758 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
760 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
766 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
767 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
768 get_cmd_string(cmd->id));
772 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
773 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
774 get_cmd_string(cmd->id));
778 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
779 IWL_ERROR("Error: Response NULL in '%s'\n",
780 get_cmd_string(cmd->id));
789 if (cmd->meta.flags & CMD_WANT_SKB) {
790 struct iwl_cmd *qcmd;
792 /* Cancel the CMD_WANT_SKB flag for the cmd in the
793 * TX cmd queue. Otherwise in case the cmd comes
794 * in later, it will possibly set an invalid
795 * address (cmd->meta.source). */
796 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
797 qcmd->meta.flags &= ~CMD_WANT_SKB;
800 if (cmd->meta.u.skb) {
801 dev_kfree_skb_any(cmd->meta.u.skb);
802 cmd->meta.u.skb = NULL;
805 atomic_set(&entry, 0);
809 int iwl_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
811 /* A command can not be asynchronous AND expect an SKB to be set. */
812 BUG_ON((cmd->meta.flags & CMD_ASYNC) &&
813 (cmd->meta.flags & CMD_WANT_SKB));
815 if (cmd->meta.flags & CMD_ASYNC)
816 return iwl_send_cmd_async(priv, cmd);
818 return iwl_send_cmd_sync(priv, cmd);
821 int iwl_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
823 struct iwl_host_cmd cmd = {
829 return iwl_send_cmd_sync(priv, &cmd);
832 static int __must_check iwl_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
834 struct iwl_host_cmd cmd = {
840 return iwl_send_cmd_sync(priv, &cmd);
843 int iwl_send_statistics_request(struct iwl_priv *priv)
845 return iwl_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
849 * iwl_rxon_add_station - add station into station table.
851 * there is only one AP station with id= IWL_AP_ID
852 * NOTE: mutex must be held before calling the this fnction
854 static int iwl_rxon_add_station(struct iwl_priv *priv,
855 const u8 *addr, int is_ap)
859 sta_id = iwl_add_station(priv, addr, is_ap, 0);
860 iwl4965_add_station(priv, addr, is_ap);
866 * iwl_set_rxon_channel - Set the phymode and channel values in staging RXON
867 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
868 * @channel: Any channel valid for the requested phymode
870 * In addition to setting the staging RXON, priv->phymode is also set.
872 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
873 * in the staging RXON flag structure based on the phymode
875 static int iwl_set_rxon_channel(struct iwl_priv *priv, u8 phymode, u16 channel)
877 if (!iwl_get_channel_info(priv, phymode, channel)) {
878 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
883 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
884 (priv->phymode == phymode))
887 priv->staging_rxon.channel = cpu_to_le16(channel);
888 if (phymode == MODE_IEEE80211A)
889 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
891 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
893 priv->phymode = phymode;
895 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
901 * iwl_check_rxon_cmd - validate RXON structure is valid
903 * NOTE: This is really only useful during development and can eventually
904 * be #ifdef'd out once the driver is stable and folks aren't actively
907 static int iwl_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
912 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
913 error |= le32_to_cpu(rxon->flags &
914 (RXON_FLG_TGJ_NARROW_BAND_MSK |
915 RXON_FLG_RADAR_DETECT_MSK));
917 IWL_WARNING("check 24G fields %d | %d\n",
920 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
921 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
923 IWL_WARNING("check 52 fields %d | %d\n",
925 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
927 IWL_WARNING("check 52 CCK %d | %d\n",
930 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
932 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
934 /* make sure basic rates 6Mbps and 1Mbps are supported */
935 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
936 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
938 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
940 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
942 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
944 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
945 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
947 IWL_WARNING("check CCK and short slot %d | %d\n",
950 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
951 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
953 IWL_WARNING("check CCK & auto detect %d | %d\n",
956 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
957 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
959 IWL_WARNING("check TGG and auto detect %d | %d\n",
963 IWL_WARNING("Tuning to channel %d\n",
964 le16_to_cpu(rxon->channel));
967 IWL_ERROR("Not a valid iwl_rxon_assoc_cmd field values\n");
974 * iwl_full_rxon_required - determine if RXON_ASSOC can be used in RXON commit
975 * @priv: staging_rxon is comapred to active_rxon
977 * If the RXON structure is changing sufficient to require a new
978 * tune or to clear and reset the RXON_FILTER_ASSOC_MSK then return 1
979 * to indicate a new tune is required.
981 static int iwl_full_rxon_required(struct iwl_priv *priv)
984 /* These items are only settable from the full RXON command */
985 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
986 compare_ether_addr(priv->staging_rxon.bssid_addr,
987 priv->active_rxon.bssid_addr) ||
988 compare_ether_addr(priv->staging_rxon.node_addr,
989 priv->active_rxon.node_addr) ||
990 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
991 priv->active_rxon.wlap_bssid_addr) ||
992 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
993 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
994 (priv->staging_rxon.air_propagation !=
995 priv->active_rxon.air_propagation) ||
996 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
997 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
998 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
999 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
1000 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
1001 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1004 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1005 * be updated with the RXON_ASSOC command -- however only some
1006 * flag transitions are allowed using RXON_ASSOC */
1008 /* Check if we are not switching bands */
1009 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1010 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1013 /* Check if we are switching association toggle */
1014 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1015 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1021 static int iwl_send_rxon_assoc(struct iwl_priv *priv)
1024 struct iwl_rx_packet *res = NULL;
1025 struct iwl_rxon_assoc_cmd rxon_assoc;
1026 struct iwl_host_cmd cmd = {
1027 .id = REPLY_RXON_ASSOC,
1028 .len = sizeof(rxon_assoc),
1029 .meta.flags = CMD_WANT_SKB,
1030 .data = &rxon_assoc,
1032 const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
1033 const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
1035 if ((rxon1->flags == rxon2->flags) &&
1036 (rxon1->filter_flags == rxon2->filter_flags) &&
1037 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1038 (rxon1->ofdm_ht_single_stream_basic_rates ==
1039 rxon2->ofdm_ht_single_stream_basic_rates) &&
1040 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1041 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1042 (rxon1->rx_chain == rxon2->rx_chain) &&
1043 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1044 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1048 rxon_assoc.flags = priv->staging_rxon.flags;
1049 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1050 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1051 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1052 rxon_assoc.reserved = 0;
1053 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1054 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1055 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1056 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1057 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1059 rc = iwl_send_cmd_sync(priv, &cmd);
1063 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1064 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1065 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1069 priv->alloc_rxb_skb--;
1070 dev_kfree_skb_any(cmd.meta.u.skb);
1076 * iwl_commit_rxon - commit staging_rxon to hardware
1078 * The RXON command in staging_rxon is commited to the hardware and
1079 * the active_rxon structure is updated with the new data. This
1080 * function correctly transitions out of the RXON_ASSOC_MSK state if
1081 * a HW tune is required based on the RXON structure changes.
1083 static int iwl_commit_rxon(struct iwl_priv *priv)
1085 /* cast away the const for active_rxon in this function */
1086 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
1087 DECLARE_MAC_BUF(mac);
1090 if (!iwl_is_alive(priv))
1093 /* always get timestamp with Rx frame */
1094 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1096 rc = iwl_check_rxon_cmd(&priv->staging_rxon);
1098 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1102 /* If we don't need to send a full RXON, we can use
1103 * iwl_rxon_assoc_cmd which is used to reconfigure filter
1104 * and other flags for the current radio configuration. */
1105 if (!iwl_full_rxon_required(priv)) {
1106 rc = iwl_send_rxon_assoc(priv);
1108 IWL_ERROR("Error setting RXON_ASSOC "
1109 "configuration (%d).\n", rc);
1113 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1118 /* station table will be cleared */
1119 priv->assoc_station_added = 0;
1121 #ifdef CONFIG_IWLWIFI_SENSITIVITY
1122 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1123 if (!priv->error_recovering)
1124 priv->start_calib = 0;
1126 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
1127 #endif /* CONFIG_IWLWIFI_SENSITIVITY */
1129 /* If we are currently associated and the new config requires
1130 * an RXON_ASSOC and the new config wants the associated mask enabled,
1131 * we must clear the associated from the active configuration
1132 * before we apply the new config */
1133 if (iwl_is_associated(priv) &&
1134 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1135 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1136 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1138 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1139 sizeof(struct iwl_rxon_cmd),
1140 &priv->active_rxon);
1142 /* If the mask clearing failed then we set
1143 * active_rxon back to what it was previously */
1145 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1146 IWL_ERROR("Error clearing ASSOC_MSK on current "
1147 "configuration (%d).\n", rc);
1152 IWL_DEBUG_INFO("Sending RXON\n"
1153 "* with%s RXON_FILTER_ASSOC_MSK\n"
1156 ((priv->staging_rxon.filter_flags &
1157 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1158 le16_to_cpu(priv->staging_rxon.channel),
1159 print_mac(mac, priv->staging_rxon.bssid_addr));
1161 /* Apply the new configuration */
1162 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1163 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
1165 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1169 iwl_clear_stations_table(priv);
1171 #ifdef CONFIG_IWLWIFI_SENSITIVITY
1172 if (!priv->error_recovering)
1173 priv->start_calib = 0;
1175 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1176 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
1177 #endif /* CONFIG_IWLWIFI_SENSITIVITY */
1179 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1181 /* If we issue a new RXON command which required a tune then we must
1182 * send a new TXPOWER command or we won't be able to Tx any frames */
1183 rc = iwl_hw_reg_send_txpower(priv);
1185 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1189 /* Add the broadcast address so we can send broadcast frames */
1190 if (iwl_rxon_add_station(priv, BROADCAST_ADDR, 0) ==
1191 IWL_INVALID_STATION) {
1192 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1196 /* If we have set the ASSOC_MSK and we are in BSS mode then
1197 * add the IWL_AP_ID to the station rate table */
1198 if (iwl_is_associated(priv) &&
1199 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
1200 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
1201 == IWL_INVALID_STATION) {
1202 IWL_ERROR("Error adding AP address for transmit.\n");
1205 priv->assoc_station_added = 1;
1211 static int iwl_send_bt_config(struct iwl_priv *priv)
1213 struct iwl_bt_cmd bt_cmd = {
1221 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1222 sizeof(struct iwl_bt_cmd), &bt_cmd);
1225 static int iwl_send_scan_abort(struct iwl_priv *priv)
1228 struct iwl_rx_packet *res;
1229 struct iwl_host_cmd cmd = {
1230 .id = REPLY_SCAN_ABORT_CMD,
1231 .meta.flags = CMD_WANT_SKB,
1234 /* If there isn't a scan actively going on in the hardware
1235 * then we are in between scan bands and not actually
1236 * actively scanning, so don't send the abort command */
1237 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1238 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1242 rc = iwl_send_cmd_sync(priv, &cmd);
1244 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1248 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1249 if (res->u.status != CAN_ABORT_STATUS) {
1250 /* The scan abort will return 1 for success or
1251 * 2 for "failure". A failure condition can be
1252 * due to simply not being in an active scan which
1253 * can occur if we send the scan abort before we
1254 * the microcode has notified us that a scan is
1256 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1257 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1258 clear_bit(STATUS_SCAN_HW, &priv->status);
1261 dev_kfree_skb_any(cmd.meta.u.skb);
1266 static int iwl_card_state_sync_callback(struct iwl_priv *priv,
1267 struct iwl_cmd *cmd,
1268 struct sk_buff *skb)
1276 * Use: Sets the internal card state to enable, disable, or halt
1278 * When in the 'enable' state the card operates as normal.
1279 * When in the 'disable' state, the card enters into a low power mode.
1280 * When in the 'halt' state, the card is shut down and must be fully
1281 * restarted to come back on.
1283 static int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
1285 struct iwl_host_cmd cmd = {
1286 .id = REPLY_CARD_STATE_CMD,
1289 .meta.flags = meta_flag,
1292 if (meta_flag & CMD_ASYNC)
1293 cmd.meta.u.callback = iwl_card_state_sync_callback;
1295 return iwl_send_cmd(priv, &cmd);
1298 static int iwl_add_sta_sync_callback(struct iwl_priv *priv,
1299 struct iwl_cmd *cmd, struct sk_buff *skb)
1301 struct iwl_rx_packet *res = NULL;
1304 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1308 res = (struct iwl_rx_packet *)skb->data;
1309 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1310 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1315 switch (res->u.add_sta.status) {
1316 case ADD_STA_SUCCESS_MSK:
1322 /* We didn't cache the SKB; let the caller free it */
1326 int iwl_send_add_station(struct iwl_priv *priv,
1327 struct iwl_addsta_cmd *sta, u8 flags)
1329 struct iwl_rx_packet *res = NULL;
1331 struct iwl_host_cmd cmd = {
1332 .id = REPLY_ADD_STA,
1333 .len = sizeof(struct iwl_addsta_cmd),
1334 .meta.flags = flags,
1338 if (flags & CMD_ASYNC)
1339 cmd.meta.u.callback = iwl_add_sta_sync_callback;
1341 cmd.meta.flags |= CMD_WANT_SKB;
1343 rc = iwl_send_cmd(priv, &cmd);
1345 if (rc || (flags & CMD_ASYNC))
1348 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1349 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1350 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1356 switch (res->u.add_sta.status) {
1357 case ADD_STA_SUCCESS_MSK:
1358 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1362 IWL_WARNING("REPLY_ADD_STA failed\n");
1367 priv->alloc_rxb_skb--;
1368 dev_kfree_skb_any(cmd.meta.u.skb);
1373 static int iwl_update_sta_key_info(struct iwl_priv *priv,
1374 struct ieee80211_key_conf *keyconf,
1377 unsigned long flags;
1378 __le16 key_flags = 0;
1380 switch (keyconf->alg) {
1382 key_flags |= STA_KEY_FLG_CCMP;
1383 key_flags |= cpu_to_le16(
1384 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1385 key_flags &= ~STA_KEY_FLG_INVALID;
1393 spin_lock_irqsave(&priv->sta_lock, flags);
1394 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1395 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1396 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1399 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1401 priv->stations[sta_id].sta.key.key_flags = key_flags;
1402 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1403 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1405 spin_unlock_irqrestore(&priv->sta_lock, flags);
1407 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
1408 iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1412 static int iwl_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
1414 unsigned long flags;
1416 spin_lock_irqsave(&priv->sta_lock, flags);
1417 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
1418 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl_keyinfo));
1419 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1420 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1421 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1422 spin_unlock_irqrestore(&priv->sta_lock, flags);
1424 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
1425 iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1429 static void iwl_clear_free_frames(struct iwl_priv *priv)
1431 struct list_head *element;
1433 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1434 priv->frames_count);
1436 while (!list_empty(&priv->free_frames)) {
1437 element = priv->free_frames.next;
1439 kfree(list_entry(element, struct iwl_frame, list));
1440 priv->frames_count--;
1443 if (priv->frames_count) {
1444 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1445 priv->frames_count);
1446 priv->frames_count = 0;
1450 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
1452 struct iwl_frame *frame;
1453 struct list_head *element;
1454 if (list_empty(&priv->free_frames)) {
1455 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1457 IWL_ERROR("Could not allocate frame!\n");
1461 priv->frames_count++;
1465 element = priv->free_frames.next;
1467 return list_entry(element, struct iwl_frame, list);
1470 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
1472 memset(frame, 0, sizeof(*frame));
1473 list_add(&frame->list, &priv->free_frames);
1476 unsigned int iwl_fill_beacon_frame(struct iwl_priv *priv,
1477 struct ieee80211_hdr *hdr,
1478 const u8 *dest, int left)
1481 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
1482 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1483 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1486 if (priv->ibss_beacon->len > left)
1489 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1491 return priv->ibss_beacon->len;
1494 int iwl_rate_index_from_plcp(int plcp)
1498 if (plcp & RATE_MCS_HT_MSK) {
1501 if (i >= IWL_RATE_MIMO_6M_PLCP)
1502 i = i - IWL_RATE_MIMO_6M_PLCP;
1504 i += IWL_FIRST_OFDM_RATE;
1505 /* skip 9M not supported in ht*/
1506 if (i >= IWL_RATE_9M_INDEX)
1508 if ((i >= IWL_FIRST_OFDM_RATE) &&
1509 (i <= IWL_LAST_OFDM_RATE))
1512 for (i = 0; i < ARRAY_SIZE(iwl_rates); i++)
1513 if (iwl_rates[i].plcp == (plcp &0xFF))
1519 static u8 iwl_rate_get_lowest_plcp(int rate_mask)
1523 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
1524 i = iwl_rates[i].next_ieee) {
1525 if (rate_mask & (1 << i))
1526 return iwl_rates[i].plcp;
1529 return IWL_RATE_INVALID;
1532 static int iwl_send_beacon_cmd(struct iwl_priv *priv)
1534 struct iwl_frame *frame;
1535 unsigned int frame_size;
1539 frame = iwl_get_free_frame(priv);
1542 IWL_ERROR("Could not obtain free frame buffer for beacon "
1547 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
1548 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic &
1550 if (rate == IWL_INVALID_RATE)
1551 rate = IWL_RATE_6M_PLCP;
1553 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
1554 if (rate == IWL_INVALID_RATE)
1555 rate = IWL_RATE_1M_PLCP;
1558 frame_size = iwl_hw_get_beacon_cmd(priv, frame, rate);
1560 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
1563 iwl_free_frame(priv, frame);
1568 /******************************************************************************
1570 * EEPROM related functions
1572 ******************************************************************************/
1574 static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
1576 memcpy(mac, priv->eeprom.mac_address, 6);
1580 * iwl_eeprom_init - read EEPROM contents
1582 * Load the EEPROM from adapter into priv->eeprom
1584 * NOTE: This routine uses the non-debug IO access functions.
1586 int iwl_eeprom_init(struct iwl_priv *priv)
1588 u16 *e = (u16 *)&priv->eeprom;
1589 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
1591 int sz = sizeof(priv->eeprom);
1596 /* The EEPROM structure has several padding buffers within it
1597 * and when adding new EEPROM maps is subject to programmer errors
1598 * which may be very difficult to identify without explicitly
1599 * checking the resulting size of the eeprom map. */
1600 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1602 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1603 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1607 rc = iwl_eeprom_aqcuire_semaphore(priv);
1609 IWL_ERROR("Failed to aqcuire EEPROM semaphore.\n");
1613 /* eeprom is an array of 16bit values */
1614 for (addr = 0; addr < sz; addr += sizeof(u16)) {
1615 _iwl_write32(priv, CSR_EEPROM_REG, addr << 1);
1616 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1618 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1619 i += IWL_EEPROM_ACCESS_DELAY) {
1620 r = _iwl_read_restricted(priv, CSR_EEPROM_REG);
1621 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1623 udelay(IWL_EEPROM_ACCESS_DELAY);
1626 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1627 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1631 e[addr / 2] = le16_to_cpu(r >> 16);
1636 iwl_eeprom_release_semaphore(priv);
1640 /******************************************************************************
1642 * Misc. internal state and helper functions
1644 ******************************************************************************/
1645 #ifdef CONFIG_IWLWIFI_DEBUG
1648 * iwl_report_frame - dump frame to syslog during debug sessions
1650 * hack this function to show different aspects of received frames,
1651 * including selective frame dumps.
1652 * group100 parameter selects whether to show 1 out of 100 good frames.
1654 * TODO: ieee80211_hdr stuff is common to 3945 and 4965, so frame type
1655 * info output is okay, but some of this stuff (e.g. iwl_rx_frame_stats)
1656 * is 3945-specific and gives bad output for 4965. Need to split the
1657 * functionality, keep common stuff here.
1659 void iwl_report_frame(struct iwl_priv *priv,
1660 struct iwl_rx_packet *pkt,
1661 struct ieee80211_hdr *header, int group100)
1664 u32 print_summary = 0;
1665 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1682 struct iwl_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1683 struct iwl_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1684 struct iwl_rx_frame_end *rx_end = IWL_RX_END(pkt);
1685 u8 *data = IWL_RX_DATA(pkt);
1688 fc = le16_to_cpu(header->frame_control);
1689 seq_ctl = le16_to_cpu(header->seq_ctrl);
1692 channel = le16_to_cpu(rx_hdr->channel);
1693 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1694 rate_sym = rx_hdr->rate;
1695 length = le16_to_cpu(rx_hdr->len);
1697 /* end-of-frame status and timestamp */
1698 status = le32_to_cpu(rx_end->status);
1699 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1700 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1701 tsf = le64_to_cpu(rx_end->timestamp);
1703 /* signal statistics */
1704 rssi = rx_stats->rssi;
1705 agc = rx_stats->agc;
1706 sig_avg = le16_to_cpu(rx_stats->sig_avg);
1707 noise_diff = le16_to_cpu(rx_stats->noise_diff);
1709 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1711 /* if data frame is to us and all is good,
1712 * (optionally) print summary for only 1 out of every 100 */
1713 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1714 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1717 print_summary = 1; /* print each frame */
1718 else if (priv->framecnt_to_us < 100) {
1719 priv->framecnt_to_us++;
1722 priv->framecnt_to_us = 0;
1727 /* print summary for all other frames */
1731 if (print_summary) {
1736 title = "100Frames";
1737 else if (fc & IEEE80211_FCTL_RETRY)
1739 else if (ieee80211_is_assoc_response(fc))
1741 else if (ieee80211_is_reassoc_response(fc))
1743 else if (ieee80211_is_probe_response(fc)) {
1745 print_dump = 1; /* dump frame contents */
1746 } else if (ieee80211_is_beacon(fc)) {
1748 print_dump = 1; /* dump frame contents */
1749 } else if (ieee80211_is_atim(fc))
1751 else if (ieee80211_is_auth(fc))
1753 else if (ieee80211_is_deauth(fc))
1755 else if (ieee80211_is_disassoc(fc))
1760 rate = iwl_rate_index_from_plcp(rate_sym);
1764 rate = iwl_rates[rate].ieee / 2;
1766 /* print frame summary.
1767 * MAC addresses show just the last byte (for brevity),
1768 * but you can hack it to show more, if you'd like to. */
1770 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1771 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1772 title, fc, header->addr1[5],
1773 length, rssi, channel, rate);
1775 /* src/dst addresses assume managed mode */
1776 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1777 "src=0x%02x, rssi=%u, tim=%lu usec, "
1778 "phy=0x%02x, chnl=%d\n",
1779 title, fc, header->addr1[5],
1780 header->addr3[5], rssi,
1781 tsf_low - priv->scan_start_tsf,
1782 phy_flags, channel);
1786 iwl_print_hex_dump(IWL_DL_RX, data, length);
1790 static void iwl_unset_hw_setting(struct iwl_priv *priv)
1792 if (priv->hw_setting.shared_virt)
1793 pci_free_consistent(priv->pci_dev,
1794 sizeof(struct iwl_shared),
1795 priv->hw_setting.shared_virt,
1796 priv->hw_setting.shared_phys);
1800 * iwl_supported_rate_to_ie - fill in the supported rate in IE field
1802 * return : set the bit for each supported rate insert in ie
1804 static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
1805 u16 basic_rate, int max_count)
1807 u16 ret_rates = 0, bit;
1813 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1814 if (bit & supported_rate) {
1816 rates[*ie] = iwl_rates[i].ieee |
1817 ((bit & basic_rate) ? 0x80 : 0x00);
1819 if (*ie >= max_count)
1827 #ifdef CONFIG_IWLWIFI_HT
1828 void static iwl_set_ht_capab(struct ieee80211_hw *hw,
1829 struct ieee80211_ht_capability *ht_cap,
1834 * iwl_fill_probe_req - fill in all required fields and IE for probe request
1836 static u16 iwl_fill_probe_req(struct iwl_priv *priv,
1837 struct ieee80211_mgmt *frame,
1838 int left, int is_direct)
1844 /* Make sure there is enough space for the probe request,
1845 * two mandatory IEs and the data */
1851 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1852 memcpy(frame->da, BROADCAST_ADDR, ETH_ALEN);
1853 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
1854 memcpy(frame->bssid, BROADCAST_ADDR, ETH_ALEN);
1855 frame->seq_ctrl = 0;
1857 /* fill in our indirect SSID IE */
1864 pos = &(frame->u.probe_req.variable[0]);
1865 *pos++ = WLAN_EID_SSID;
1868 /* fill in our direct SSID IE... */
1871 left -= 2 + priv->essid_len;
1874 /* ... fill it in... */
1875 *pos++ = WLAN_EID_SSID;
1876 *pos++ = priv->essid_len;
1877 memcpy(pos, priv->essid, priv->essid_len);
1878 pos += priv->essid_len;
1879 len += 2 + priv->essid_len;
1882 /* fill in supported rate */
1887 /* ... fill it in... */
1888 *pos++ = WLAN_EID_SUPP_RATES;
1890 ret_rates = priv->active_rate = priv->rates_mask;
1891 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1893 iwl_supported_rate_to_ie(pos, priv->active_rate,
1894 priv->active_rate_basic, left);
1897 ret_rates = ~ret_rates & priv->active_rate;
1902 /* fill in supported extended rate */
1907 /* ... fill it in... */
1908 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1910 iwl_supported_rate_to_ie(pos, ret_rates, priv->active_rate_basic, left);
1914 #ifdef CONFIG_IWLWIFI_HT
1915 if (is_direct && priv->is_ht_enabled) {
1916 u8 use_wide_chan = 1;
1918 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
1921 *pos++ = WLAN_EID_HT_CAPABILITY;
1922 *pos++ = sizeof(struct ieee80211_ht_capability);
1923 iwl_set_ht_capab(NULL, (struct ieee80211_ht_capability *)pos,
1925 len += 2 + sizeof(struct ieee80211_ht_capability);
1927 #endif /*CONFIG_IWLWIFI_HT */
1936 #ifdef CONFIG_IWLWIFI_QOS
1937 static int iwl_send_qos_params_command(struct iwl_priv *priv,
1938 struct iwl_qosparam_cmd *qos)
1941 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1942 sizeof(struct iwl_qosparam_cmd), qos);
1945 static void iwl_reset_qos(struct iwl_priv *priv)
1951 unsigned long flags;
1954 spin_lock_irqsave(&priv->lock, flags);
1955 priv->qos_data.qos_active = 0;
1957 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1958 if (priv->qos_data.qos_enable)
1959 priv->qos_data.qos_active = 1;
1960 if (!(priv->active_rate & 0xfff0)) {
1964 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1965 if (priv->qos_data.qos_enable)
1966 priv->qos_data.qos_active = 1;
1967 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1972 if (priv->qos_data.qos_active)
1975 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1976 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1977 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1978 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1979 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1981 if (priv->qos_data.qos_active) {
1983 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1984 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1985 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1986 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1987 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1990 priv->qos_data.def_qos_parm.ac[i].cw_min =
1991 cpu_to_le16((cw_min + 1) / 2 - 1);
1992 priv->qos_data.def_qos_parm.ac[i].cw_max =
1993 cpu_to_le16(cw_max);
1994 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1996 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1999 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2001 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2004 priv->qos_data.def_qos_parm.ac[i].cw_min =
2005 cpu_to_le16((cw_min + 1) / 4 - 1);
2006 priv->qos_data.def_qos_parm.ac[i].cw_max =
2007 cpu_to_le16((cw_max + 1) / 2 - 1);
2008 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2009 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2011 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2014 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2017 for (i = 1; i < 4; i++) {
2018 priv->qos_data.def_qos_parm.ac[i].cw_min =
2019 cpu_to_le16(cw_min);
2020 priv->qos_data.def_qos_parm.ac[i].cw_max =
2021 cpu_to_le16(cw_max);
2022 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
2023 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2024 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2027 IWL_DEBUG_QOS("set QoS to default \n");
2029 spin_unlock_irqrestore(&priv->lock, flags);
2032 static void iwl_activate_qos(struct iwl_priv *priv, u8 force)
2034 unsigned long flags;
2039 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2042 if (!priv->qos_data.qos_enable)
2045 spin_lock_irqsave(&priv->lock, flags);
2046 priv->qos_data.def_qos_parm.qos_flags = 0;
2048 if (priv->qos_data.qos_cap.q_AP.queue_request &&
2049 !priv->qos_data.qos_cap.q_AP.txop_request)
2050 priv->qos_data.def_qos_parm.qos_flags |=
2051 QOS_PARAM_FLG_TXOP_TYPE_MSK;
2053 if (priv->qos_data.qos_active)
2054 priv->qos_data.def_qos_parm.qos_flags |=
2055 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2057 spin_unlock_irqrestore(&priv->lock, flags);
2059 if (force || iwl_is_associated(priv)) {
2060 IWL_DEBUG_QOS("send QoS cmd with Qos active %d \n",
2061 priv->qos_data.qos_active);
2063 iwl_send_qos_params_command(priv,
2064 &(priv->qos_data.def_qos_parm));
2068 #endif /* CONFIG_IWLWIFI_QOS */
2070 * Power management (not Tx power!) functions
2072 #define MSEC_TO_USEC 1024
2074 #define NOSLP __constant_cpu_to_le16(0), 0, 0
2075 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
2076 #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2077 #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2078 __constant_cpu_to_le32(X1), \
2079 __constant_cpu_to_le32(X2), \
2080 __constant_cpu_to_le32(X3), \
2081 __constant_cpu_to_le32(X4)}
2084 /* default power management (not Tx power) table values */
2086 static struct iwl_power_vec_entry range_0[IWL_POWER_AC] = {
2087 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2088 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2089 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2090 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2091 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2092 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2096 static struct iwl_power_vec_entry range_1[IWL_POWER_AC] = {
2097 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2098 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2099 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2100 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2101 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2102 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2103 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2104 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2105 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2106 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2109 int iwl_power_init_handle(struct iwl_priv *priv)
2112 struct iwl_power_mgr *pow_data;
2113 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
2116 IWL_DEBUG_POWER("Initialize power \n");
2118 pow_data = &(priv->power_data);
2120 memset(pow_data, 0, sizeof(*pow_data));
2122 pow_data->active_index = IWL_POWER_RANGE_0;
2123 pow_data->dtim_val = 0xffff;
2125 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2126 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2128 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2132 struct iwl_powertable_cmd *cmd;
2134 IWL_DEBUG_POWER("adjust power command flags\n");
2136 for (i = 0; i < IWL_POWER_AC; i++) {
2137 cmd = &pow_data->pwr_range_0[i].cmd;
2140 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2142 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2148 static int iwl_update_power_cmd(struct iwl_priv *priv,
2149 struct iwl_powertable_cmd *cmd, u32 mode)
2154 struct iwl_power_vec_entry *range;
2156 struct iwl_power_mgr *pow_data;
2158 if (mode > IWL_POWER_INDEX_5) {
2159 IWL_DEBUG_POWER("Error invalid power mode \n");
2162 pow_data = &(priv->power_data);
2164 if (pow_data->active_index == IWL_POWER_RANGE_0)
2165 range = &pow_data->pwr_range_0[0];
2167 range = &pow_data->pwr_range_1[1];
2169 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
2171 #ifdef IWL_MAC80211_DISABLE
2172 if (priv->assoc_network != NULL) {
2173 unsigned long flags;
2175 period = priv->assoc_network->tim.tim_period;
2177 #endif /*IWL_MAC80211_DISABLE */
2178 skip = range[mode].no_dtim;
2187 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2189 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2190 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2191 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2194 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2195 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2196 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2199 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2200 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2201 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2202 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2203 le32_to_cpu(cmd->sleep_interval[0]),
2204 le32_to_cpu(cmd->sleep_interval[1]),
2205 le32_to_cpu(cmd->sleep_interval[2]),
2206 le32_to_cpu(cmd->sleep_interval[3]),
2207 le32_to_cpu(cmd->sleep_interval[4]));
2212 static int iwl_send_power_mode(struct iwl_priv *priv, u32 mode)
2214 u32 final_mode = mode;
2216 struct iwl_powertable_cmd cmd;
2218 /* If on battery, set to 3,
2219 * if plugged into AC power, set to CAM ("continuosly aware mode"),
2220 * else user level */
2222 case IWL_POWER_BATTERY:
2223 final_mode = IWL_POWER_INDEX_3;
2226 final_mode = IWL_POWER_MODE_CAM;
2233 cmd.keep_alive_beacons = 0;
2235 iwl_update_power_cmd(priv, &cmd, final_mode);
2237 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2239 if (final_mode == IWL_POWER_MODE_CAM)
2240 clear_bit(STATUS_POWER_PMI, &priv->status);
2242 set_bit(STATUS_POWER_PMI, &priv->status);
2247 int iwl_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
2249 /* Filter incoming packets to determine if they are targeted toward
2250 * this network, discarding packets coming from ourselves */
2251 switch (priv->iw_mode) {
2252 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2253 /* packets from our adapter are dropped (echo) */
2254 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2256 /* {broad,multi}cast packets to our IBSS go through */
2257 if (is_multicast_ether_addr(header->addr1))
2258 return !compare_ether_addr(header->addr3, priv->bssid);
2259 /* packets to our adapter go through */
2260 return !compare_ether_addr(header->addr1, priv->mac_addr);
2261 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2262 /* packets from our adapter are dropped (echo) */
2263 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2265 /* {broad,multi}cast packets to our BSS go through */
2266 if (is_multicast_ether_addr(header->addr1))
2267 return !compare_ether_addr(header->addr2, priv->bssid);
2268 /* packets to our adapter go through */
2269 return !compare_ether_addr(header->addr1, priv->mac_addr);
2275 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2277 const char *iwl_get_tx_fail_reason(u32 status)
2279 switch (status & TX_STATUS_MSK) {
2280 case TX_STATUS_SUCCESS:
2282 TX_STATUS_ENTRY(SHORT_LIMIT);
2283 TX_STATUS_ENTRY(LONG_LIMIT);
2284 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2285 TX_STATUS_ENTRY(MGMNT_ABORT);
2286 TX_STATUS_ENTRY(NEXT_FRAG);
2287 TX_STATUS_ENTRY(LIFE_EXPIRE);
2288 TX_STATUS_ENTRY(DEST_PS);
2289 TX_STATUS_ENTRY(ABORTED);
2290 TX_STATUS_ENTRY(BT_RETRY);
2291 TX_STATUS_ENTRY(STA_INVALID);
2292 TX_STATUS_ENTRY(FRAG_DROPPED);
2293 TX_STATUS_ENTRY(TID_DISABLE);
2294 TX_STATUS_ENTRY(FRAME_FLUSHED);
2295 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2296 TX_STATUS_ENTRY(TX_LOCKED);
2297 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2304 * iwl_scan_cancel - Cancel any currently executing HW scan
2306 * NOTE: priv->mutex is not required before calling this function
2308 static int iwl_scan_cancel(struct iwl_priv *priv)
2310 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2311 clear_bit(STATUS_SCANNING, &priv->status);
2315 if (test_bit(STATUS_SCANNING, &priv->status)) {
2316 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2317 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2318 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2319 queue_work(priv->workqueue, &priv->abort_scan);
2322 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2324 return test_bit(STATUS_SCANNING, &priv->status);
2331 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
2332 * @ms: amount of time to wait (in milliseconds) for scan to abort
2334 * NOTE: priv->mutex must be held before calling this function
2336 static int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
2338 unsigned long now = jiffies;
2341 ret = iwl_scan_cancel(priv);
2343 mutex_unlock(&priv->mutex);
2344 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2345 test_bit(STATUS_SCANNING, &priv->status))
2347 mutex_lock(&priv->mutex);
2349 return test_bit(STATUS_SCANNING, &priv->status);
2355 static void iwl_sequence_reset(struct iwl_priv *priv)
2357 /* Reset ieee stats */
2359 /* We don't reset the net_device_stats (ieee->stats) on
2362 priv->last_seq_num = -1;
2363 priv->last_frag_num = -1;
2364 priv->last_packet_time = 0;
2366 iwl_scan_cancel(priv);
2369 #define MAX_UCODE_BEACON_INTERVAL 4096
2370 #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2372 static __le16 iwl_adjust_beacon_interval(u16 beacon_val)
2375 u16 beacon_factor = 0;
2378 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2379 / MAX_UCODE_BEACON_INTERVAL;
2380 new_val = beacon_val / beacon_factor;
2382 return cpu_to_le16(new_val);
2385 static void iwl_setup_rxon_timing(struct iwl_priv *priv)
2387 u64 interval_tm_unit;
2389 unsigned long flags;
2390 struct ieee80211_conf *conf = NULL;
2393 conf = ieee80211_get_hw_conf(priv->hw);
2395 spin_lock_irqsave(&priv->lock, flags);
2396 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2397 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2399 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2401 tsf = priv->timestamp1;
2402 tsf = ((tsf << 32) | priv->timestamp0);
2404 beacon_int = priv->beacon_int;
2405 spin_unlock_irqrestore(&priv->lock, flags);
2407 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2408 if (beacon_int == 0) {
2409 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2410 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2412 priv->rxon_timing.beacon_interval =
2413 cpu_to_le16(beacon_int);
2414 priv->rxon_timing.beacon_interval =
2415 iwl_adjust_beacon_interval(
2416 le16_to_cpu(priv->rxon_timing.beacon_interval));
2419 priv->rxon_timing.atim_window = 0;
2421 priv->rxon_timing.beacon_interval =
2422 iwl_adjust_beacon_interval(conf->beacon_int);
2423 /* TODO: we need to get atim_window from upper stack
2424 * for now we set to 0 */
2425 priv->rxon_timing.atim_window = 0;
2429 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2430 result = do_div(tsf, interval_tm_unit);
2431 priv->rxon_timing.beacon_init_val =
2432 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2435 ("beacon interval %d beacon timer %d beacon tim %d\n",
2436 le16_to_cpu(priv->rxon_timing.beacon_interval),
2437 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2438 le16_to_cpu(priv->rxon_timing.atim_window));
2441 static int iwl_scan_initiate(struct iwl_priv *priv)
2443 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2444 IWL_ERROR("APs don't scan.\n");
2448 if (!iwl_is_ready_rf(priv)) {
2449 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2453 if (test_bit(STATUS_SCANNING, &priv->status)) {
2454 IWL_DEBUG_SCAN("Scan already in progress.\n");
2458 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2459 IWL_DEBUG_SCAN("Scan request while abort pending. "
2464 IWL_DEBUG_INFO("Starting scan...\n");
2465 priv->scan_bands = 2;
2466 set_bit(STATUS_SCANNING, &priv->status);
2467 priv->scan_start = jiffies;
2468 priv->scan_pass_start = priv->scan_start;
2470 queue_work(priv->workqueue, &priv->request_scan);
2475 static int iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
2477 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
2480 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2482 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2487 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode)
2489 if (phymode == MODE_IEEE80211A) {
2490 priv->staging_rxon.flags &=
2491 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2492 | RXON_FLG_CCK_MSK);
2493 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2495 /* Copied from iwl_bg_post_associate() */
2496 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2497 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2499 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2501 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2502 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2504 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2505 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2506 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2511 * initilize rxon structure with default values fromm eeprom
2513 static void iwl_connection_init_rx_config(struct iwl_priv *priv)
2515 const struct iwl_channel_info *ch_info;
2517 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2519 switch (priv->iw_mode) {
2520 case IEEE80211_IF_TYPE_AP:
2521 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2524 case IEEE80211_IF_TYPE_STA:
2525 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2526 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2529 case IEEE80211_IF_TYPE_IBSS:
2530 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2531 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2532 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2533 RXON_FILTER_ACCEPT_GRP_MSK;
2536 case IEEE80211_IF_TYPE_MNTR:
2537 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2538 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2539 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2544 /* TODO: Figure out when short_preamble would be set and cache from
2546 if (!hw_to_local(priv->hw)->short_preamble)
2547 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2549 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2552 ch_info = iwl_get_channel_info(priv, priv->phymode,
2553 le16_to_cpu(priv->staging_rxon.channel));
2556 ch_info = &priv->channel_info[0];
2559 * in some case A channels are all non IBSS
2560 * in this case force B/G channel
2562 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2563 !(is_channel_ibss(ch_info)))
2564 ch_info = &priv->channel_info[0];
2566 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2567 if (is_channel_a_band(ch_info))
2568 priv->phymode = MODE_IEEE80211A;
2570 priv->phymode = MODE_IEEE80211G;
2572 iwl_set_flags_for_phymode(priv, priv->phymode);
2574 priv->staging_rxon.ofdm_basic_rates =
2575 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2576 priv->staging_rxon.cck_basic_rates =
2577 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2579 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2580 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2581 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2582 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2583 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2584 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2585 iwl4965_set_rxon_chain(priv);
2588 static int iwl_set_mode(struct iwl_priv *priv, int mode)
2590 if (!iwl_is_ready_rf(priv))
2593 if (mode == IEEE80211_IF_TYPE_IBSS) {
2594 const struct iwl_channel_info *ch_info;
2596 ch_info = iwl_get_channel_info(priv,
2598 le16_to_cpu(priv->staging_rxon.channel));
2600 if (!ch_info || !is_channel_ibss(ch_info)) {
2601 IWL_ERROR("channel %d not IBSS channel\n",
2602 le16_to_cpu(priv->staging_rxon.channel));
2607 cancel_delayed_work(&priv->scan_check);
2608 if (iwl_scan_cancel_timeout(priv, 100)) {
2609 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2610 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2614 priv->iw_mode = mode;
2616 iwl_connection_init_rx_config(priv);
2617 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2619 iwl_clear_stations_table(priv);
2621 iwl_commit_rxon(priv);
2626 static void iwl_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
2627 struct ieee80211_tx_control *ctl,
2628 struct iwl_cmd *cmd,
2629 struct sk_buff *skb_frag,
2632 struct iwl_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2634 switch (keyinfo->alg) {
2636 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2637 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2638 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2643 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2646 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2649 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2654 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2655 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2657 if (keyinfo->keylen == 13)
2658 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2660 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2662 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2663 "with key %d\n", ctl->key_idx);
2667 IWL_DEBUG_TX("Tx packet in the clear (encrypt requested).\n");
2671 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2677 * handle build REPLY_TX command notification.
2679 static void iwl_build_tx_cmd_basic(struct iwl_priv *priv,
2680 struct iwl_cmd *cmd,
2681 struct ieee80211_tx_control *ctrl,
2682 struct ieee80211_hdr *hdr,
2683 int is_unicast, u8 std_id)
2686 u16 fc = le16_to_cpu(hdr->frame_control);
2687 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2689 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2690 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2691 tx_flags |= TX_CMD_FLG_ACK_MSK;
2692 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2693 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2694 if (ieee80211_is_probe_response(fc) &&
2695 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2696 tx_flags |= TX_CMD_FLG_TSF_MSK;
2698 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2699 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2702 cmd->cmd.tx.sta_id = std_id;
2703 if (ieee80211_get_morefrag(hdr))
2704 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2706 qc = ieee80211_get_qos_ctrl(hdr);
2708 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2709 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2711 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2713 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2714 tx_flags |= TX_CMD_FLG_RTS_MSK;
2715 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2716 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2717 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2718 tx_flags |= TX_CMD_FLG_CTS_MSK;
2721 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2722 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2724 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2725 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2726 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2727 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
2728 cmd->cmd.tx.timeout.pm_frame_timeout =
2731 cmd->cmd.tx.timeout.pm_frame_timeout =
2734 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2736 cmd->cmd.tx.driver_txop = 0;
2737 cmd->cmd.tx.tx_flags = tx_flags;
2738 cmd->cmd.tx.next_frame_len = 0;
2741 static int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
2744 u16 fc = le16_to_cpu(hdr->frame_control);
2745 DECLARE_MAC_BUF(mac);
2747 /* If this frame is broadcast or not data then use the broadcast
2749 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2750 is_multicast_ether_addr(hdr->addr1))
2751 return priv->hw_setting.bcast_sta_id;
2753 switch (priv->iw_mode) {
2755 /* If this frame is part of a BSS network (we're a station), then
2756 * we use the AP's station id */
2757 case IEEE80211_IF_TYPE_STA:
2760 /* If we are an AP, then find the station, or use BCAST */
2761 case IEEE80211_IF_TYPE_AP:
2762 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2763 if (sta_id != IWL_INVALID_STATION)
2765 return priv->hw_setting.bcast_sta_id;
2767 /* If this frame is part of a IBSS network, then we use the
2768 * target specific station id */
2769 case IEEE80211_IF_TYPE_IBSS:
2770 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2771 if (sta_id != IWL_INVALID_STATION)
2774 sta_id = iwl_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
2776 if (sta_id != IWL_INVALID_STATION)
2779 IWL_DEBUG_DROP("Station %s not in station map. "
2780 "Defaulting to broadcast...\n",
2781 print_mac(mac, hdr->addr1));
2782 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2783 return priv->hw_setting.bcast_sta_id;
2786 IWL_WARNING("Unkown mode of operation: %d", priv->iw_mode);
2787 return priv->hw_setting.bcast_sta_id;
2792 * start REPLY_TX command process
2794 static int iwl_tx_skb(struct iwl_priv *priv,
2795 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2797 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2798 struct iwl_tfd_frame *tfd;
2800 int txq_id = ctl->queue;
2801 struct iwl_tx_queue *txq = NULL;
2802 struct iwl_queue *q = NULL;
2803 dma_addr_t phys_addr;
2804 dma_addr_t txcmd_phys;
2805 struct iwl_cmd *out_cmd = NULL;
2806 u16 len, idx, len_org;
2807 u8 id, hdr_len, unicast;
2812 u8 wait_write_ptr = 0;
2813 unsigned long flags;
2816 spin_lock_irqsave(&priv->lock, flags);
2817 if (iwl_is_rfkill(priv)) {
2818 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2822 if (!priv->interface_id) {
2823 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2827 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2828 IWL_ERROR("ERROR: No TX rate available.\n");
2832 unicast = !is_multicast_ether_addr(hdr->addr1);
2835 fc = le16_to_cpu(hdr->frame_control);
2837 #ifdef CONFIG_IWLWIFI_DEBUG
2838 if (ieee80211_is_auth(fc))
2839 IWL_DEBUG_TX("Sending AUTH frame\n");
2840 else if (ieee80211_is_assoc_request(fc))
2841 IWL_DEBUG_TX("Sending ASSOC frame\n");
2842 else if (ieee80211_is_reassoc_request(fc))
2843 IWL_DEBUG_TX("Sending REASSOC frame\n");
2846 if (!iwl_is_associated(priv) &&
2847 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
2848 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
2852 spin_unlock_irqrestore(&priv->lock, flags);
2854 hdr_len = ieee80211_get_hdrlen(fc);
2855 sta_id = iwl_get_sta_id(priv, hdr);
2856 if (sta_id == IWL_INVALID_STATION) {
2857 DECLARE_MAC_BUF(mac);
2859 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2860 print_mac(mac, hdr->addr1));
2864 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2866 qc = ieee80211_get_qos_ctrl(hdr);
2868 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2869 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2871 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2873 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2875 #ifdef CONFIG_IWLWIFI_HT
2876 #ifdef CONFIG_IWLWIFI_HT_AGG
2877 /* aggregation is on for this <sta,tid> */
2878 if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
2879 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
2880 #endif /* CONFIG_IWLWIFI_HT_AGG */
2881 #endif /* CONFIG_IWLWIFI_HT */
2883 txq = &priv->txq[txq_id];
2886 spin_lock_irqsave(&priv->lock, flags);
2888 tfd = &txq->bd[q->first_empty];
2889 memset(tfd, 0, sizeof(*tfd));
2890 control_flags = (u32 *) tfd;
2891 idx = get_cmd_index(q, q->first_empty, 0);
2893 memset(&(txq->txb[q->first_empty]), 0, sizeof(struct iwl_tx_info));
2894 txq->txb[q->first_empty].skb[0] = skb;
2895 memcpy(&(txq->txb[q->first_empty].status.control),
2896 ctl, sizeof(struct ieee80211_tx_control));
2897 out_cmd = &txq->cmd[idx];
2898 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2899 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2900 out_cmd->hdr.cmd = REPLY_TX;
2901 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2902 INDEX_TO_SEQ(q->first_empty)));
2903 /* copy frags header */
2904 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2906 /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
2907 len = priv->hw_setting.tx_cmd_len +
2908 sizeof(struct iwl_cmd_header) + hdr_len;
2911 len = (len + 3) & ~3;
2918 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2919 offsetof(struct iwl_cmd, hdr);
2921 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
2923 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
2924 iwl_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
2926 /* 802.11 null functions have no payload... */
2927 len = skb->len - hdr_len;
2929 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2930 len, PCI_DMA_TODEVICE);
2931 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
2935 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2937 len = (u16)skb->len;
2938 out_cmd->cmd.tx.len = cpu_to_le16(len);
2940 /* TODO need this for burst mode later on */
2941 iwl_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
2943 /* set is_hcca to 0; it probably will never be implemented */
2944 iwl_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
2946 iwl4965_tx_cmd(priv, out_cmd, sta_id, txcmd_phys,
2947 hdr, hdr_len, ctl, NULL);
2949 if (!ieee80211_get_morefrag(hdr)) {
2950 txq->need_update = 1;
2952 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2953 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2957 txq->need_update = 0;
2960 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
2961 sizeof(out_cmd->cmd.tx));
2963 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
2964 ieee80211_get_hdrlen(fc));
2966 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2968 q->first_empty = iwl_queue_inc_wrap(q->first_empty, q->n_bd);
2969 rc = iwl_tx_queue_update_write_ptr(priv, txq);
2970 spin_unlock_irqrestore(&priv->lock, flags);
2975 if ((iwl_queue_space(q) < q->high_mark)
2976 && priv->mac80211_registered) {
2977 if (wait_write_ptr) {
2978 spin_lock_irqsave(&priv->lock, flags);
2979 txq->need_update = 1;
2980 iwl_tx_queue_update_write_ptr(priv, txq);
2981 spin_unlock_irqrestore(&priv->lock, flags);
2984 ieee80211_stop_queue(priv->hw, ctl->queue);
2990 spin_unlock_irqrestore(&priv->lock, flags);
2995 static void iwl_set_rate(struct iwl_priv *priv)
2997 const struct ieee80211_hw_mode *hw = NULL;
2998 struct ieee80211_rate *rate;
3001 hw = iwl_get_hw_mode(priv, priv->phymode);
3003 priv->active_rate = 0;
3004 priv->active_rate_basic = 0;
3006 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
3007 hw->mode == MODE_IEEE80211A ?
3008 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
3010 for (i = 0; i < hw->num_rates; i++) {
3011 rate = &(hw->rates[i]);
3012 if ((rate->val < IWL_RATE_COUNT) &&
3013 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
3014 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
3015 rate->val, iwl_rates[rate->val].plcp,
3016 (rate->flags & IEEE80211_RATE_BASIC) ?
3018 priv->active_rate |= (1 << rate->val);
3019 if (rate->flags & IEEE80211_RATE_BASIC)
3020 priv->active_rate_basic |= (1 << rate->val);
3022 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
3023 rate->val, iwl_rates[rate->val].plcp);
3026 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
3027 priv->active_rate, priv->active_rate_basic);
3030 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
3031 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
3034 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
3035 priv->staging_rxon.cck_basic_rates =
3036 ((priv->active_rate_basic &
3037 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
3039 priv->staging_rxon.cck_basic_rates =
3040 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
3042 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
3043 priv->staging_rxon.ofdm_basic_rates =
3044 ((priv->active_rate_basic &
3045 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
3046 IWL_FIRST_OFDM_RATE) & 0xFF;
3048 priv->staging_rxon.ofdm_basic_rates =
3049 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
3052 static void iwl_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
3054 unsigned long flags;
3056 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
3059 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
3060 disable_radio ? "OFF" : "ON");
3062 if (disable_radio) {
3063 iwl_scan_cancel(priv);
3064 /* FIXME: This is a workaround for AP */
3065 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3066 spin_lock_irqsave(&priv->lock, flags);
3067 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
3068 CSR_UCODE_SW_BIT_RFKILL);
3069 spin_unlock_irqrestore(&priv->lock, flags);
3070 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
3071 set_bit(STATUS_RF_KILL_SW, &priv->status);
3076 spin_lock_irqsave(&priv->lock, flags);
3077 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3079 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3080 spin_unlock_irqrestore(&priv->lock, flags);
3085 spin_lock_irqsave(&priv->lock, flags);
3086 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3087 if (!iwl_grab_restricted_access(priv))
3088 iwl_release_restricted_access(priv);
3089 spin_unlock_irqrestore(&priv->lock, flags);
3091 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3092 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3093 "disabled by HW switch\n");
3097 queue_work(priv->workqueue, &priv->restart);
3101 void iwl_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
3102 u32 decrypt_res, struct ieee80211_rx_status *stats)
3105 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3107 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3110 if (!(fc & IEEE80211_FCTL_PROTECTED))
3113 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3114 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3115 case RX_RES_STATUS_SEC_TYPE_TKIP:
3116 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3117 RX_RES_STATUS_BAD_ICV_MIC)
3118 stats->flag |= RX_FLAG_MMIC_ERROR;
3119 case RX_RES_STATUS_SEC_TYPE_WEP:
3120 case RX_RES_STATUS_SEC_TYPE_CCMP:
3121 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3122 RX_RES_STATUS_DECRYPT_OK) {
3123 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3124 stats->flag |= RX_FLAG_DECRYPTED;
3133 void iwl_handle_data_packet_monitor(struct iwl_priv *priv,
3134 struct iwl_rx_mem_buffer *rxb,
3135 void *data, short len,
3136 struct ieee80211_rx_status *stats,
3139 struct iwl_rt_rx_hdr *iwl_rt;
3141 /* First cache any information we need before we overwrite
3142 * the information provided in the skb from the hardware */
3143 s8 signal = stats->ssi;
3145 int rate = stats->rate;
3146 u64 tsf = stats->mactime;
3147 __le16 phy_flags_hw = cpu_to_le16(phy_flags);
3149 /* We received data from the HW, so stop the watchdog */
3150 if (len > IWL_RX_BUF_SIZE - sizeof(*iwl_rt)) {
3151 IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
3155 /* copy the frame data to write after where the radiotap header goes */
3156 iwl_rt = (void *)rxb->skb->data;
3157 memmove(iwl_rt->payload, data, len);
3159 iwl_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
3160 iwl_rt->rt_hdr.it_pad = 0; /* always good to zero */
3162 /* total header + data */
3163 iwl_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl_rt));
3165 /* Set the size of the skb to the size of the frame */
3166 skb_put(rxb->skb, sizeof(*iwl_rt) + len);
3168 /* Big bitfield of all the fields we provide in radiotap */
3169 iwl_rt->rt_hdr.it_present =
3170 cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
3171 (1 << IEEE80211_RADIOTAP_FLAGS) |
3172 (1 << IEEE80211_RADIOTAP_RATE) |
3173 (1 << IEEE80211_RADIOTAP_CHANNEL) |
3174 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
3175 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
3176 (1 << IEEE80211_RADIOTAP_ANTENNA));
3178 /* Zero the flags, we'll add to them as we go */
3179 iwl_rt->rt_flags = 0;
3181 iwl_rt->rt_tsf = cpu_to_le64(tsf);
3183 /* Convert to dBm */
3184 iwl_rt->rt_dbmsignal = signal;
3185 iwl_rt->rt_dbmnoise = noise;
3187 /* Convert the channel frequency and set the flags */
3188 iwl_rt->rt_channelMHz = cpu_to_le16(stats->freq);
3189 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
3190 iwl_rt->rt_chbitmask =
3191 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
3192 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
3193 iwl_rt->rt_chbitmask =
3194 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
3196 iwl_rt->rt_chbitmask =
3197 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
3199 rate = iwl_rate_index_from_plcp(rate);
3201 iwl_rt->rt_rate = 0;
3203 iwl_rt->rt_rate = iwl_rates[rate].ieee;
3205 /* antenna number */
3206 iwl_rt->rt_antenna =
3207 le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
3209 /* set the preamble flag if we have it */
3210 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
3211 iwl_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3213 IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
3215 stats->flag |= RX_FLAG_RADIOTAP;
3216 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3221 #define IWL_PACKET_RETRY_TIME HZ
3223 int is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
3225 u16 sc = le16_to_cpu(header->seq_ctrl);
3226 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3227 u16 frag = sc & IEEE80211_SCTL_FRAG;
3228 u16 *last_seq, *last_frag;
3229 unsigned long *last_time;
3231 switch (priv->iw_mode) {
3232 case IEEE80211_IF_TYPE_IBSS:{
3233 struct list_head *p;
3234 struct iwl_ibss_seq *entry = NULL;
3235 u8 *mac = header->addr2;
3236 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3238 __list_for_each(p, &priv->ibss_mac_hash[index]) {
3240 list_entry(p, struct iwl_ibss_seq, list);
3241 if (!compare_ether_addr(entry->mac, mac))
3244 if (p == &priv->ibss_mac_hash[index]) {
3245 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3248 ("Cannot malloc new mac entry\n");
3251 memcpy(entry->mac, mac, ETH_ALEN);
3252 entry->seq_num = seq;
3253 entry->frag_num = frag;
3254 entry->packet_time = jiffies;
3255 list_add(&entry->list,
3256 &priv->ibss_mac_hash[index]);
3259 last_seq = &entry->seq_num;
3260 last_frag = &entry->frag_num;
3261 last_time = &entry->packet_time;
3264 case IEEE80211_IF_TYPE_STA:
3265 last_seq = &priv->last_seq_num;
3266 last_frag = &priv->last_frag_num;
3267 last_time = &priv->last_packet_time;
3272 if ((*last_seq == seq) &&
3273 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3274 if (*last_frag == frag)
3276 if (*last_frag + 1 != frag)
3277 /* out-of-order fragment */
3283 *last_time = jiffies;
3290 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3292 #include "iwl-spectrum.h"
3294 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
3295 #define BEACON_TIME_MASK_HIGH 0xFF000000
3296 #define TIME_UNIT 1024
3299 * extended beacon time format
3300 * time in usec will be changed into a 32-bit value in 8:24 format
3301 * the high 1 byte is the beacon counts
3302 * the lower 3 bytes is the time in usec within one beacon interval
3305 static u32 iwl_usecs_to_beacons(u32 usec, u32 beacon_interval)
3309 u32 interval = beacon_interval * 1024;
3311 if (!interval || !usec)
3314 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3315 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3317 return (quot << 24) + rem;
3320 /* base is usually what we get from ucode with each received frame,
3321 * the same as HW timer counter counting down
3324 static __le32 iwl_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
3326 u32 base_low = base & BEACON_TIME_MASK_LOW;
3327 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3328 u32 interval = beacon_interval * TIME_UNIT;
3329 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3330 (addon & BEACON_TIME_MASK_HIGH);
3332 if (base_low > addon_low)
3333 res += base_low - addon_low;
3334 else if (base_low < addon_low) {
3335 res += interval + base_low - addon_low;
3340 return cpu_to_le32(res);
3343 static int iwl_get_measurement(struct iwl_priv *priv,
3344 struct ieee80211_measurement_params *params,
3347 struct iwl_spectrum_cmd spectrum;
3348 struct iwl_rx_packet *res;
3349 struct iwl_host_cmd cmd = {
3350 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3351 .data = (void *)&spectrum,
3352 .meta.flags = CMD_WANT_SKB,
3354 u32 add_time = le64_to_cpu(params->start_time);
3356 int spectrum_resp_status;
3357 int duration = le16_to_cpu(params->duration);
3359 if (iwl_is_associated(priv))
3361 iwl_usecs_to_beacons(
3362 le64_to_cpu(params->start_time) - priv->last_tsf,
3363 le16_to_cpu(priv->rxon_timing.beacon_interval));
3365 memset(&spectrum, 0, sizeof(spectrum));
3367 spectrum.channel_count = cpu_to_le16(1);
3369 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3370 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3371 cmd.len = sizeof(spectrum);
3372 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3374 if (iwl_is_associated(priv))
3375 spectrum.start_time =
3376 iwl_add_beacon_time(priv->last_beacon_time,
3378 le16_to_cpu(priv->rxon_timing.beacon_interval));
3380 spectrum.start_time = 0;
3382 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3383 spectrum.channels[0].channel = params->channel;
3384 spectrum.channels[0].type = type;
3385 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3386 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3387 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3389 rc = iwl_send_cmd_sync(priv, &cmd);
3393 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
3394 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3395 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3399 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3400 switch (spectrum_resp_status) {
3401 case 0: /* Command will be handled */
3402 if (res->u.spectrum.id != 0xff) {
3404 ("Replaced existing measurement: %d\n",
3405 res->u.spectrum.id);
3406 priv->measurement_status &= ~MEASUREMENT_READY;
3408 priv->measurement_status |= MEASUREMENT_ACTIVE;
3412 case 1: /* Command will not be handled */
3417 dev_kfree_skb_any(cmd.meta.u.skb);
3423 static void iwl_txstatus_to_ieee(struct iwl_priv *priv,
3424 struct iwl_tx_info *tx_sta)
3427 tx_sta->status.ack_signal = 0;
3428 tx_sta->status.excessive_retries = 0;
3429 tx_sta->status.queue_length = 0;
3430 tx_sta->status.queue_number = 0;
3433 ieee80211_tx_status_irqsafe(priv->hw,
3434 tx_sta->skb[0], &(tx_sta->status));
3436 ieee80211_tx_status(priv->hw,
3437 tx_sta->skb[0], &(tx_sta->status));
3439 tx_sta->skb[0] = NULL;
3443 * iwl_tx_queue_reclaim - Reclaim Tx queue entries no more used by NIC.
3445 * When FW advances 'R' index, all entries between old and
3446 * new 'R' index need to be reclaimed. As result, some free space
3447 * forms. If there is enough free space (> low mark), wake Tx queue.
3449 int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
3451 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3452 struct iwl_queue *q = &txq->q;
3455 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3456 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3457 "is out of range [0-%d] %d %d.\n", txq_id,
3458 index, q->n_bd, q->first_empty, q->last_used);
3462 for (index = iwl_queue_inc_wrap(index, q->n_bd);
3463 q->last_used != index;
3464 q->last_used = iwl_queue_inc_wrap(q->last_used, q->n_bd)) {
3465 if (txq_id != IWL_CMD_QUEUE_NUM) {
3466 iwl_txstatus_to_ieee(priv,
3467 &(txq->txb[txq->q.last_used]));
3468 iwl_hw_txq_free_tfd(priv, txq);
3469 } else if (nfreed > 1) {
3470 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
3471 q->first_empty, q->last_used);
3472 queue_work(priv->workqueue, &priv->restart);
3477 if (iwl_queue_space(q) > q->low_mark && (txq_id >= 0) &&
3478 (txq_id != IWL_CMD_QUEUE_NUM) &&
3479 priv->mac80211_registered)
3480 ieee80211_wake_queue(priv->hw, txq_id);
3486 static int iwl_is_tx_success(u32 status)
3488 status &= TX_STATUS_MSK;
3489 return (status == TX_STATUS_SUCCESS)
3490 || (status == TX_STATUS_DIRECT_DONE);
3493 /******************************************************************************
3495 * Generic RX handler implementations
3497 ******************************************************************************/
3498 #ifdef CONFIG_IWLWIFI_HT
3499 #ifdef CONFIG_IWLWIFI_HT_AGG
3501 static inline int iwl_get_ra_sta_id(struct iwl_priv *priv,
3502 struct ieee80211_hdr *hdr)
3504 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3507 u8 *da = ieee80211_get_DA(hdr);
3508 return iwl_hw_find_station(priv, da);
3512 static struct ieee80211_hdr *iwl_tx_queue_get_hdr(
3513 struct iwl_priv *priv, int txq_id, int idx)
3515 if (priv->txq[txq_id].txb[idx].skb[0])
3516 return (struct ieee80211_hdr *)priv->txq[txq_id].
3517 txb[idx].skb[0]->data;
3521 static inline u32 iwl_get_scd_ssn(struct iwl_tx_resp *tx_resp)
3523 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3524 tx_resp->frame_count);
3525 return le32_to_cpu(*scd_ssn) & MAX_SN;
3528 static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
3529 struct iwl_ht_agg *agg,
3530 struct iwl_tx_resp *tx_resp,
3534 __le32 *frame_status = &tx_resp->status;
3535 struct ieee80211_tx_status *tx_status = NULL;
3536 struct ieee80211_hdr *hdr = NULL;
3541 if (agg->wait_for_ba)
3542 IWL_DEBUG_TX_REPLY("got tx repsons w/o back\n");
3544 agg->frame_count = tx_resp->frame_count;
3545 agg->start_idx = start_idx;
3546 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3547 agg->bitmap0 = agg->bitmap1 = 0;
3549 if (agg->frame_count == 1) {
3550 struct iwl_tx_queue *txq ;
3551 status = le32_to_cpu(frame_status[0]);
3553 txq_id = agg->txq_id;
3554 txq = &priv->txq[txq_id];
3555 /* FIXME: code repetition */
3556 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d \n",
3557 agg->frame_count, agg->start_idx);
3559 tx_status = &(priv->txq[txq_id].txb[txq->q.last_used].status);
3560 tx_status->retry_count = tx_resp->failure_frame;
3561 tx_status->queue_number = status & 0xff;
3562 tx_status->queue_length = tx_resp->bt_kill_count;
3563 tx_status->queue_length |= tx_resp->failure_rts;
3565 tx_status->flags = iwl_is_tx_success(status)?
3566 IEEE80211_TX_STATUS_ACK : 0;
3567 tx_status->control.tx_rate =
3568 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags);
3569 /* FIXME: code repetition end */
3571 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3572 status & 0xff, tx_resp->failure_frame);
3573 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
3574 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags));
3576 agg->wait_for_ba = 0;
3579 int start = agg->start_idx;
3581 for (i = 0; i < agg->frame_count; i++) {
3583 status = le32_to_cpu(frame_status[i]);
3585 idx = SEQ_TO_INDEX(seq);
3586 txq_id = SEQ_TO_QUEUE(seq);
3588 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3589 AGG_TX_STATE_ABORT_MSK))
3592 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3593 agg->frame_count, txq_id, idx);
3595 hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
3597 sc = le16_to_cpu(hdr->seq_ctrl);
3598 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3599 IWL_ERROR("BUG_ON idx doesn't match seq control"
3600 " idx=%d, seq_idx=%d, seq=%d\n",
3606 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3607 i, idx, SEQ_TO_SN(sc));
3611 sh = (start - idx) + 0xff;
3612 bitmap = bitmap << sh;
3615 } else if (sh < -64)
3616 sh = 0xff - (start - idx);
3620 bitmap = bitmap << sh;
3623 bitmap |= (1 << sh);
3624 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3625 start, (u32)(bitmap & 0xFFFFFFFF));
3628 agg->bitmap0 = bitmap & 0xFFFFFFFF;
3629 agg->bitmap1 = bitmap >> 32;
3630 agg->start_idx = start;
3631 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3632 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%x\n",
3633 agg->frame_count, agg->start_idx,
3637 agg->wait_for_ba = 1;
3644 static void iwl_rx_reply_tx(struct iwl_priv *priv,
3645 struct iwl_rx_mem_buffer *rxb)
3647 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3648 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3649 int txq_id = SEQ_TO_QUEUE(sequence);
3650 int index = SEQ_TO_INDEX(sequence);
3651 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3652 struct ieee80211_tx_status *tx_status;
3653 struct iwl_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
3654 u32 status = le32_to_cpu(tx_resp->status);
3655 #ifdef CONFIG_IWLWIFI_HT
3656 #ifdef CONFIG_IWLWIFI_HT_AGG
3661 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3662 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3663 "is out of range [0-%d] %d %d\n", txq_id,
3664 index, txq->q.n_bd, txq->q.first_empty,
3669 #ifdef CONFIG_IWLWIFI_HT
3670 #ifdef CONFIG_IWLWIFI_HT_AGG
3671 if (txq->sched_retry) {
3672 const u32 scd_ssn = iwl_get_scd_ssn(tx_resp);
3673 struct ieee80211_hdr *hdr =
3674 iwl_tx_queue_get_hdr(priv, txq_id, index);
3675 struct iwl_ht_agg *agg = NULL;
3676 __le16 *qc = ieee80211_get_qos_ctrl(hdr);
3679 IWL_ERROR("BUG_ON qc is null!!!!\n");
3683 tid = le16_to_cpu(*qc) & 0xf;
3685 sta_id = iwl_get_ra_sta_id(priv, hdr);
3686 if (unlikely(sta_id == IWL_INVALID_STATION)) {
3687 IWL_ERROR("Station not known for\n");
3691 agg = &priv->stations[sta_id].tid[tid].agg;
3693 iwl4965_tx_status_reply_tx(priv, agg, tx_resp, index);
3695 if ((tx_resp->frame_count == 1) &&
3696 !iwl_is_tx_success(status)) {
3697 /* TODO: send BAR */
3700 if ((txq->q.last_used != (scd_ssn & 0xff))) {
3701 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
3702 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3703 "%d index %d\n", scd_ssn , index);
3704 iwl_tx_queue_reclaim(priv, txq_id, index);
3707 #endif /* CONFIG_IWLWIFI_HT_AGG */
3708 #endif /* CONFIG_IWLWIFI_HT */
3709 tx_status = &(txq->txb[txq->q.last_used].status);
3711 tx_status->retry_count = tx_resp->failure_frame;
3712 tx_status->queue_number = status;
3713 tx_status->queue_length = tx_resp->bt_kill_count;
3714 tx_status->queue_length |= tx_resp->failure_rts;
3717 iwl_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
3719 tx_status->control.tx_rate =
3720 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags);
3722 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
3723 "retries %d\n", txq_id, iwl_get_tx_fail_reason(status),
3724 status, le32_to_cpu(tx_resp->rate_n_flags),
3725 tx_resp->failure_frame);
3727 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3729 iwl_tx_queue_reclaim(priv, txq_id, index);
3730 #ifdef CONFIG_IWLWIFI_HT
3731 #ifdef CONFIG_IWLWIFI_HT_AGG
3733 #endif /* CONFIG_IWLWIFI_HT_AGG */
3734 #endif /* CONFIG_IWLWIFI_HT */
3736 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3737 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3741 static void iwl_rx_reply_alive(struct iwl_priv *priv,
3742 struct iwl_rx_mem_buffer *rxb)
3744 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3745 struct iwl_alive_resp *palive;
3746 struct delayed_work *pwork;
3748 palive = &pkt->u.alive_frame;
3750 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3752 palive->is_valid, palive->ver_type,
3753 palive->ver_subtype);
3755 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3756 IWL_DEBUG_INFO("Initialization Alive received.\n");
3757 memcpy(&priv->card_alive_init,
3758 &pkt->u.alive_frame,
3759 sizeof(struct iwl_init_alive_resp));
3760 pwork = &priv->init_alive_start;
3762 IWL_DEBUG_INFO("Runtime Alive received.\n");
3763 memcpy(&priv->card_alive, &pkt->u.alive_frame,
3764 sizeof(struct iwl_alive_resp));
3765 pwork = &priv->alive_start;
3768 /* We delay the ALIVE response by 5ms to
3769 * give the HW RF Kill time to activate... */
3770 if (palive->is_valid == UCODE_VALID_OK)
3771 queue_delayed_work(priv->workqueue, pwork,
3772 msecs_to_jiffies(5));
3774 IWL_WARNING("uCode did not respond OK.\n");
3777 static void iwl_rx_reply_add_sta(struct iwl_priv *priv,
3778 struct iwl_rx_mem_buffer *rxb)
3780 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3782 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3786 static void iwl_rx_reply_error(struct iwl_priv *priv,
3787 struct iwl_rx_mem_buffer *rxb)
3789 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3791 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3792 "seq 0x%04X ser 0x%08X\n",
3793 le32_to_cpu(pkt->u.err_resp.error_type),
3794 get_cmd_string(pkt->u.err_resp.cmd_id),
3795 pkt->u.err_resp.cmd_id,
3796 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3797 le32_to_cpu(pkt->u.err_resp.error_info));
3800 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3802 static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
3804 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3805 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
3806 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
3807 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3808 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3809 rxon->channel = csa->channel;
3810 priv->staging_rxon.channel = csa->channel;
3813 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
3814 struct iwl_rx_mem_buffer *rxb)
3816 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3817 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3818 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
3820 if (!report->state) {
3821 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3822 "Spectrum Measure Notification: Start\n");
3826 memcpy(&priv->measure_report, report, sizeof(*report));
3827 priv->measurement_status |= MEASUREMENT_READY;
3831 static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
3832 struct iwl_rx_mem_buffer *rxb)
3834 #ifdef CONFIG_IWLWIFI_DEBUG
3835 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3836 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
3837 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3838 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3842 static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
3843 struct iwl_rx_mem_buffer *rxb)
3845 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3846 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3847 "notification for %s:\n",
3848 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
3849 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
3852 static void iwl_bg_beacon_update(struct work_struct *work)
3854 struct iwl_priv *priv =
3855 container_of(work, struct iwl_priv, beacon_update);
3856 struct sk_buff *beacon;
3858 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3859 beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
3862 IWL_ERROR("update beacon failed\n");
3866 mutex_lock(&priv->mutex);
3867 /* new beacon skb is allocated every time; dispose previous.*/
3868 if (priv->ibss_beacon)
3869 dev_kfree_skb(priv->ibss_beacon);
3871 priv->ibss_beacon = beacon;
3872 mutex_unlock(&priv->mutex);
3874 iwl_send_beacon_cmd(priv);
3877 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
3878 struct iwl_rx_mem_buffer *rxb)
3880 #ifdef CONFIG_IWLWIFI_DEBUG
3881 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3882 struct iwl_beacon_notif *beacon = &(pkt->u.beacon_status);
3883 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
3885 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3886 "tsf %d %d rate %d\n",
3887 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3888 beacon->beacon_notify_hdr.failure_frame,
3889 le32_to_cpu(beacon->ibss_mgr_status),
3890 le32_to_cpu(beacon->high_tsf),
3891 le32_to_cpu(beacon->low_tsf), rate);
3894 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3895 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3896 queue_work(priv->workqueue, &priv->beacon_update);
3899 /* Service response to REPLY_SCAN_CMD (0x80) */
3900 static void iwl_rx_reply_scan(struct iwl_priv *priv,
3901 struct iwl_rx_mem_buffer *rxb)
3903 #ifdef CONFIG_IWLWIFI_DEBUG
3904 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3905 struct iwl_scanreq_notification *notif =
3906 (struct iwl_scanreq_notification *)pkt->u.raw;
3908 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3912 /* Service SCAN_START_NOTIFICATION (0x82) */
3913 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
3914 struct iwl_rx_mem_buffer *rxb)
3916 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3917 struct iwl_scanstart_notification *notif =
3918 (struct iwl_scanstart_notification *)pkt->u.raw;
3919 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3920 IWL_DEBUG_SCAN("Scan start: "
3922 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3924 notif->band ? "bg" : "a",
3926 notif->tsf_low, notif->status, notif->beacon_timer);
3929 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
3930 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
3931 struct iwl_rx_mem_buffer *rxb)
3933 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3934 struct iwl_scanresults_notification *notif =
3935 (struct iwl_scanresults_notification *)pkt->u.raw;
3937 IWL_DEBUG_SCAN("Scan ch.res: "
3939 "(TSF: 0x%08X:%08X) - %d "
3940 "elapsed=%lu usec (%dms since last)\n",
3942 notif->band ? "bg" : "a",
3943 le32_to_cpu(notif->tsf_high),
3944 le32_to_cpu(notif->tsf_low),
3945 le32_to_cpu(notif->statistics[0]),
3946 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3947 jiffies_to_msecs(elapsed_jiffies
3948 (priv->last_scan_jiffies, jiffies)));
3950 priv->last_scan_jiffies = jiffies;
3953 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
3954 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
3955 struct iwl_rx_mem_buffer *rxb)
3957 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3958 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
3960 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3961 scan_notif->scanned_channels,
3962 scan_notif->tsf_low,
3963 scan_notif->tsf_high, scan_notif->status);
3965 /* The HW is no longer scanning */
3966 clear_bit(STATUS_SCAN_HW, &priv->status);
3968 /* The scan completion notification came in, so kill that timer... */
3969 cancel_delayed_work(&priv->scan_check);
3971 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3972 (priv->scan_bands == 2) ? "2.4" : "5.2",
3973 jiffies_to_msecs(elapsed_jiffies
3974 (priv->scan_pass_start, jiffies)));
3976 /* Remove this scanned band from the list
3977 * of pending bands to scan */
3980 /* If a request to abort was given, or the scan did not succeed
3981 * then we reset the scan state machine and terminate,
3982 * re-queuing another scan if one has been requested */
3983 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3984 IWL_DEBUG_INFO("Aborted scan completed.\n");
3985 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3987 /* If there are more bands on this scan pass reschedule */
3988 if (priv->scan_bands > 0)
3992 priv->last_scan_jiffies = jiffies;
3993 IWL_DEBUG_INFO("Setting scan to off\n");
3995 clear_bit(STATUS_SCANNING, &priv->status);
3997 IWL_DEBUG_INFO("Scan took %dms\n",
3998 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
4000 queue_work(priv->workqueue, &priv->scan_completed);
4005 priv->scan_pass_start = jiffies;
4006 queue_work(priv->workqueue, &priv->request_scan);
4009 /* Handle notification from uCode that card's power state is changing
4010 * due to software, hardware, or critical temperature RFKILL */
4011 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
4012 struct iwl_rx_mem_buffer *rxb)
4014 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
4015 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
4016 unsigned long status = priv->status;
4018 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
4019 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
4020 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
4022 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
4023 RF_CARD_DISABLED)) {
4025 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
4026 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4028 if (!iwl_grab_restricted_access(priv)) {
4029 iwl_write_restricted(
4030 priv, HBUS_TARG_MBX_C,
4031 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4033 iwl_release_restricted_access(priv);
4036 if (!(flags & RXON_CARD_DISABLED)) {
4037 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
4038 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4039 if (!iwl_grab_restricted_access(priv)) {
4040 iwl_write_restricted(
4041 priv, HBUS_TARG_MBX_C,
4042 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4044 iwl_release_restricted_access(priv);
4048 if (flags & RF_CARD_DISABLED) {
4049 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
4050 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
4051 iwl_read32(priv, CSR_UCODE_DRV_GP1);
4052 if (!iwl_grab_restricted_access(priv))
4053 iwl_release_restricted_access(priv);
4057 if (flags & HW_CARD_DISABLED)
4058 set_bit(STATUS_RF_KILL_HW, &priv->status);
4060 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4063 if (flags & SW_CARD_DISABLED)
4064 set_bit(STATUS_RF_KILL_SW, &priv->status);
4066 clear_bit(STATUS_RF_KILL_SW, &priv->status);
4068 if (!(flags & RXON_CARD_DISABLED))
4069 iwl_scan_cancel(priv);
4071 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
4072 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
4073 (test_bit(STATUS_RF_KILL_SW, &status) !=
4074 test_bit(STATUS_RF_KILL_SW, &priv->status)))
4075 queue_work(priv->workqueue, &priv->rf_kill);
4077 wake_up_interruptible(&priv->wait_command_queue);
4081 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
4083 * Setup the RX handlers for each of the reply types sent from the uCode
4086 * This function chains into the hardware specific files for them to setup
4087 * any hardware specific handlers as well.
4089 static void iwl_setup_rx_handlers(struct iwl_priv *priv)
4091 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
4092 priv->rx_handlers[REPLY_ADD_STA] = iwl_rx_reply_add_sta;
4093 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
4094 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
4095 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
4096 iwl_rx_spectrum_measure_notif;
4097 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
4098 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
4099 iwl_rx_pm_debug_statistics_notif;
4100 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
4102 /* NOTE: iwl_rx_statistics is different based on whether
4103 * the build is for the 3945 or the 4965. See the
4104 * corresponding implementation in iwl-XXXX.c
4106 * The same handler is used for both the REPLY to a
4107 * discrete statistics request from the host as well as
4108 * for the periodic statistics notification from the uCode
4110 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_hw_rx_statistics;
4111 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_hw_rx_statistics;
4113 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
4114 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
4115 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
4116 iwl_rx_scan_results_notif;
4117 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
4118 iwl_rx_scan_complete_notif;
4119 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
4120 priv->rx_handlers[REPLY_TX] = iwl_rx_reply_tx;
4122 /* Setup hardware specific Rx handlers */
4123 iwl_hw_rx_handler_setup(priv);
4127 * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
4128 * @rxb: Rx buffer to reclaim
4130 * If an Rx buffer has an async callback associated with it the callback
4131 * will be executed. The attached skb (if present) will only be freed
4132 * if the callback returns 1
4134 static void iwl_tx_cmd_complete(struct iwl_priv *priv,
4135 struct iwl_rx_mem_buffer *rxb)
4137 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
4138 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
4139 int txq_id = SEQ_TO_QUEUE(sequence);
4140 int index = SEQ_TO_INDEX(sequence);
4141 int huge = sequence & SEQ_HUGE_FRAME;
4143 struct iwl_cmd *cmd;
4145 /* If a Tx command is being handled and it isn't in the actual
4146 * command queue then there a command routing bug has been introduced
4147 * in the queue management code. */
4148 if (txq_id != IWL_CMD_QUEUE_NUM)
4149 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
4150 txq_id, pkt->hdr.cmd);
4151 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
4153 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
4154 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
4156 /* Input error checking is done when commands are added to queue. */
4157 if (cmd->meta.flags & CMD_WANT_SKB) {
4158 cmd->meta.source->u.skb = rxb->skb;
4160 } else if (cmd->meta.u.callback &&
4161 !cmd->meta.u.callback(priv, cmd, rxb->skb))
4164 iwl_tx_queue_reclaim(priv, txq_id, index);
4166 if (!(cmd->meta.flags & CMD_ASYNC)) {
4167 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4168 wake_up_interruptible(&priv->wait_command_queue);
4172 /************************** RX-FUNCTIONS ****************************/
4174 * Rx theory of operation
4176 * The host allocates 32 DMA target addresses and passes the host address
4177 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
4181 * The host/firmware share two index registers for managing the Rx buffers.
4183 * The READ index maps to the first position that the firmware may be writing
4184 * to -- the driver can read up to (but not including) this position and get
4186 * The READ index is managed by the firmware once the card is enabled.
4188 * The WRITE index maps to the last position the driver has read from -- the
4189 * position preceding WRITE is the last slot the firmware can place a packet.
4191 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
4194 * During initialization the host sets up the READ queue position to the first
4195 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4197 * When the firmware places a packet in a buffer it will advance the READ index
4198 * and fire the RX interrupt. The driver can then query the READ index and
4199 * process as many packets as possible, moving the WRITE index forward as it
4200 * resets the Rx queue buffers with new memory.
4202 * The management in the driver is as follows:
4203 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
4204 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
4205 * to replensish the iwl->rxq->rx_free.
4206 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
4207 * iwl->rxq is replenished and the READ INDEX is updated (updating the
4208 * 'processed' and 'read' driver indexes as well)
4209 * + A received packet is processed and handed to the kernel network stack,
4210 * detached from the iwl->rxq. The driver 'processed' index is updated.
4211 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
4212 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
4213 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
4214 * were enough free buffers and RX_STALLED is set it is cleared.
4219 * iwl_rx_queue_alloc() Allocates rx_free
4220 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
4221 * iwl_rx_queue_restock
4222 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
4223 * queue, updates firmware pointers, and updates
4224 * the WRITE index. If insufficient rx_free buffers
4225 * are available, schedules iwl_rx_replenish
4227 * -- enable interrupts --
4228 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
4229 * READ INDEX, detaching the SKB from the pool.
4230 * Moves the packet buffer from queue to rx_used.
4231 * Calls iwl_rx_queue_restock to refill any empty
4238 * iwl_rx_queue_space - Return number of free slots available in queue.
4240 static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
4242 int s = q->read - q->write;
4245 /* keep some buffer to not confuse full and empty queue */
4253 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
4255 * NOTE: This function has 3945 and 4965 specific code sections
4256 * but is declared in base due to the majority of the
4257 * implementation being the same (only a numeric constant is
4261 int iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
4265 unsigned long flags;
4267 spin_lock_irqsave(&q->lock, flags);
4269 if (q->need_update == 0)
4272 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4273 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4275 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4276 iwl_set_bit(priv, CSR_GP_CNTRL,
4277 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4281 rc = iwl_grab_restricted_access(priv);
4285 iwl_write_restricted(priv, FH_RSCSR_CHNL0_WPTR,
4287 iwl_release_restricted_access(priv);
4289 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
4295 spin_unlock_irqrestore(&q->lock, flags);
4300 * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer pointer.
4302 * NOTE: This function has 3945 and 4965 specific code paths in it.
4304 static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
4305 dma_addr_t dma_addr)
4307 return cpu_to_le32((u32)(dma_addr >> 8));
4312 * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
4314 * If there are slots in the RX queue that need to be restocked,
4315 * and we have free pre-allocated buffers, fill the ranks as much
4316 * as we can pulling from rx_free.
4318 * This moves the 'write' index forward to catch up with 'processed', and
4319 * also updates the memory address in the firmware to reference the new
4322 int iwl_rx_queue_restock(struct iwl_priv *priv)
4324 struct iwl_rx_queue *rxq = &priv->rxq;
4325 struct list_head *element;
4326 struct iwl_rx_mem_buffer *rxb;
4327 unsigned long flags;
4330 spin_lock_irqsave(&rxq->lock, flags);
4331 write = rxq->write & ~0x7;
4332 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
4333 element = rxq->rx_free.next;
4334 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4336 rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->dma_addr);
4337 rxq->queue[rxq->write] = rxb;
4338 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4341 spin_unlock_irqrestore(&rxq->lock, flags);
4342 /* If the pre-allocated buffer pool is dropping low, schedule to
4344 if (rxq->free_count <= RX_LOW_WATERMARK)
4345 queue_work(priv->workqueue, &priv->rx_replenish);
4348 /* If we've added more space for the firmware to place data, tell it */
4349 if ((write != (rxq->write & ~0x7))
4350 || (abs(rxq->write - rxq->read) > 7)) {
4351 spin_lock_irqsave(&rxq->lock, flags);
4352 rxq->need_update = 1;
4353 spin_unlock_irqrestore(&rxq->lock, flags);
4354 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
4363 * iwl_rx_replensih - Move all used packet from rx_used to rx_free
4365 * When moving to rx_free an SKB is allocated for the slot.
4367 * Also restock the Rx queue via iwl_rx_queue_restock.
4368 * This is called as a scheduled work item (except for during intialization)
4370 void iwl_rx_replenish(void *data)
4372 struct iwl_priv *priv = data;
4373 struct iwl_rx_queue *rxq = &priv->rxq;
4374 struct list_head *element;
4375 struct iwl_rx_mem_buffer *rxb;
4376 unsigned long flags;
4377 spin_lock_irqsave(&rxq->lock, flags);
4378 while (!list_empty(&rxq->rx_used)) {
4379 element = rxq->rx_used.next;
4380 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4382 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
4384 if (net_ratelimit())
4385 printk(KERN_CRIT DRV_NAME
4386 ": Can not allocate SKB buffers\n");
4387 /* We don't reschedule replenish work here -- we will
4388 * call the restock method and if it still needs
4389 * more buffers it will schedule replenish */
4392 priv->alloc_rxb_skb++;
4395 pci_map_single(priv->pci_dev, rxb->skb->data,
4396 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4397 list_add_tail(&rxb->list, &rxq->rx_free);
4400 spin_unlock_irqrestore(&rxq->lock, flags);
4402 spin_lock_irqsave(&priv->lock, flags);
4403 iwl_rx_queue_restock(priv);
4404 spin_unlock_irqrestore(&priv->lock, flags);
4407 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
4408 * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
4409 * This free routine walks the list of POOL entries and if SKB is set to
4410 * non NULL it is unmapped and freed
4412 void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4415 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4416 if (rxq->pool[i].skb != NULL) {
4417 pci_unmap_single(priv->pci_dev,
4418 rxq->pool[i].dma_addr,
4419 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4420 dev_kfree_skb(rxq->pool[i].skb);
4424 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4429 int iwl_rx_queue_alloc(struct iwl_priv *priv)
4431 struct iwl_rx_queue *rxq = &priv->rxq;
4432 struct pci_dev *dev = priv->pci_dev;
4435 spin_lock_init(&rxq->lock);
4436 INIT_LIST_HEAD(&rxq->rx_free);
4437 INIT_LIST_HEAD(&rxq->rx_used);
4438 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4441 /* Fill the rx_used queue with _all_ of the Rx buffers */
4442 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4443 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4444 /* Set us so that we have processed and used all buffers, but have
4445 * not restocked the Rx queue with fresh buffers */
4446 rxq->read = rxq->write = 0;
4447 rxq->free_count = 0;
4448 rxq->need_update = 0;
4452 void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4454 unsigned long flags;
4456 spin_lock_irqsave(&rxq->lock, flags);
4457 INIT_LIST_HEAD(&rxq->rx_free);
4458 INIT_LIST_HEAD(&rxq->rx_used);
4459 /* Fill the rx_used queue with _all_ of the Rx buffers */
4460 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4461 /* In the reset function, these buffers may have been allocated
4462 * to an SKB, so we need to unmap and free potential storage */
4463 if (rxq->pool[i].skb != NULL) {
4464 pci_unmap_single(priv->pci_dev,
4465 rxq->pool[i].dma_addr,
4466 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4467 priv->alloc_rxb_skb--;
4468 dev_kfree_skb(rxq->pool[i].skb);
4469 rxq->pool[i].skb = NULL;
4471 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4474 /* Set us so that we have processed and used all buffers, but have
4475 * not restocked the Rx queue with fresh buffers */
4476 rxq->read = rxq->write = 0;
4477 rxq->free_count = 0;
4478 spin_unlock_irqrestore(&rxq->lock, flags);
4481 /* Convert linear signal-to-noise ratio into dB */
4482 static u8 ratio2dB[100] = {
4483 /* 0 1 2 3 4 5 6 7 8 9 */
4484 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4485 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4486 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4487 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4488 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4489 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4490 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4491 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4492 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4493 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4496 /* Calculates a relative dB value from a ratio of linear
4497 * (i.e. not dB) signal levels.
4498 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
4499 int iwl_calc_db_from_ratio(int sig_ratio)
4501 /* Anything above 1000:1 just report as 60 dB */
4502 if (sig_ratio > 1000)
4505 /* Above 100:1, divide by 10 and use table,
4506 * add 20 dB to make up for divide by 10 */
4507 if (sig_ratio > 100)
4508 return (20 + (int)ratio2dB[sig_ratio/10]);
4510 /* We shouldn't see this */
4514 /* Use table for ratios 1:1 - 99:1 */
4515 return (int)ratio2dB[sig_ratio];
4518 #define PERFECT_RSSI (-20) /* dBm */
4519 #define WORST_RSSI (-95) /* dBm */
4520 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4522 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
4523 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4524 * about formulas used below. */
4525 int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
4528 int degradation = PERFECT_RSSI - rssi_dbm;
4530 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4531 * as indicator; formula is (signal dbm - noise dbm).
4532 * SNR at or above 40 is a great signal (100%).
4533 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4534 * Weakest usable signal is usually 10 - 15 dB SNR. */
4536 if (rssi_dbm - noise_dbm >= 40)
4538 else if (rssi_dbm < noise_dbm)
4540 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4542 /* Else use just the signal level.
4543 * This formula is a least squares fit of data points collected and
4544 * compared with a reference system that had a percentage (%) display
4545 * for signal quality. */
4547 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4548 (15 * RSSI_RANGE + 62 * degradation)) /
4549 (RSSI_RANGE * RSSI_RANGE);
4553 else if (sig_qual < 1)
4560 * iwl_rx_handle - Main entry function for receiving responses from the uCode
4562 * Uses the priv->rx_handlers callback function array to invoke
4563 * the appropriate handlers, including command responses,
4564 * frame-received notifications, and other notifications.
4566 static void iwl_rx_handle(struct iwl_priv *priv)
4568 struct iwl_rx_mem_buffer *rxb;
4569 struct iwl_rx_packet *pkt;
4570 struct iwl_rx_queue *rxq = &priv->rxq;
4573 unsigned long flags;
4575 r = iwl_hw_get_rx_read(priv);
4578 /* Rx interrupt, but nothing sent from uCode */
4580 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4583 rxb = rxq->queue[i];
4585 /* If an RXB doesn't have a queue slot associated with it
4586 * then a bug has been introduced in the queue refilling
4587 * routines -- catch it here */
4588 BUG_ON(rxb == NULL);
4590 rxq->queue[i] = NULL;
4592 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4594 PCI_DMA_FROMDEVICE);
4595 pkt = (struct iwl_rx_packet *)rxb->skb->data;
4597 /* Reclaim a command buffer only if this packet is a response
4598 * to a (driver-originated) command.
4599 * If the packet (e.g. Rx frame) originated from uCode,
4600 * there is no command buffer to reclaim.
4601 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4602 * but apparently a few don't get set; catch them here. */
4603 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4604 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4605 (pkt->hdr.cmd != REPLY_4965_RX) &&
4606 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
4607 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4608 (pkt->hdr.cmd != REPLY_TX);
4610 /* Based on type of command response or notification,
4611 * handle those that need handling via function in
4612 * rx_handlers table. See iwl_setup_rx_handlers() */
4613 if (priv->rx_handlers[pkt->hdr.cmd]) {
4614 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4615 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4616 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4617 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4619 /* No handling needed */
4620 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4621 "r %d i %d No handler needed for %s, 0x%02x\n",
4622 r, i, get_cmd_string(pkt->hdr.cmd),
4627 /* Invoke any callbacks, transfer the skb to caller,
4628 * and fire off the (possibly) blocking iwl_send_cmd()
4629 * as we reclaim the driver command queue */
4630 if (rxb && rxb->skb)
4631 iwl_tx_cmd_complete(priv, rxb);
4633 IWL_WARNING("Claim null rxb?\n");
4636 /* For now we just don't re-use anything. We can tweak this
4637 * later to try and re-use notification packets and SKBs that
4638 * fail to Rx correctly */
4639 if (rxb->skb != NULL) {
4640 priv->alloc_rxb_skb--;
4641 dev_kfree_skb_any(rxb->skb);
4645 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4646 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4647 spin_lock_irqsave(&rxq->lock, flags);
4648 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4649 spin_unlock_irqrestore(&rxq->lock, flags);
4650 i = (i + 1) & RX_QUEUE_MASK;
4653 /* Backtrack one entry */
4655 iwl_rx_queue_restock(priv);
4658 int iwl_tx_queue_update_write_ptr(struct iwl_priv *priv,
4659 struct iwl_tx_queue *txq)
4663 int txq_id = txq->q.id;
4665 if (txq->need_update == 0)
4668 /* if we're trying to save power */
4669 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4670 /* wake up nic if it's powered down ...
4671 * uCode will wake up, and interrupt us again, so next
4672 * time we'll skip this part. */
4673 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4675 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4676 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
4677 iwl_set_bit(priv, CSR_GP_CNTRL,
4678 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4682 /* restore this queue's parameters in nic hardware. */
4683 rc = iwl_grab_restricted_access(priv);
4686 iwl_write_restricted(priv, HBUS_TARG_WRPTR,
4687 txq->q.first_empty | (txq_id << 8));
4688 iwl_release_restricted_access(priv);
4690 /* else not in power-save mode, uCode will never sleep when we're
4691 * trying to tx (during RFKILL, we're not trying to tx). */
4693 iwl_write32(priv, HBUS_TARG_WRPTR,
4694 txq->q.first_empty | (txq_id << 8));
4696 txq->need_update = 0;
4701 #ifdef CONFIG_IWLWIFI_DEBUG
4702 static void iwl_print_rx_config_cmd(struct iwl_rxon_cmd *rxon)
4704 DECLARE_MAC_BUF(mac);
4706 IWL_DEBUG_RADIO("RX CONFIG:\n");
4707 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
4708 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4709 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4710 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4711 le32_to_cpu(rxon->filter_flags));
4712 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4713 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4714 rxon->ofdm_basic_rates);
4715 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
4716 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4717 print_mac(mac, rxon->node_addr));
4718 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4719 print_mac(mac, rxon->bssid_addr));
4720 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4724 static void iwl_enable_interrupts(struct iwl_priv *priv)
4726 IWL_DEBUG_ISR("Enabling interrupts\n");
4727 set_bit(STATUS_INT_ENABLED, &priv->status);
4728 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
4731 static inline void iwl_disable_interrupts(struct iwl_priv *priv)
4733 clear_bit(STATUS_INT_ENABLED, &priv->status);
4735 /* disable interrupts from uCode/NIC to host */
4736 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
4738 /* acknowledge/clear/reset any interrupts still pending
4739 * from uCode or flow handler (Rx/Tx DMA) */
4740 iwl_write32(priv, CSR_INT, 0xffffffff);
4741 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
4742 IWL_DEBUG_ISR("Disabled interrupts\n");
4745 static const char *desc_lookup(int i)
4753 return "BAD_CHECKSUM";
4755 return "NMI_INTERRUPT";
4759 return "FATAL_ERROR";
4765 #define ERROR_START_OFFSET (1 * sizeof(u32))
4766 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
4768 static void iwl_dump_nic_error_log(struct iwl_priv *priv)
4771 u32 desc, time, count, base, data1;
4772 u32 blink1, blink2, ilink1, ilink2;
4775 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4777 if (!iwl_hw_valid_rtc_data_addr(base)) {
4778 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4782 rc = iwl_grab_restricted_access(priv);
4784 IWL_WARNING("Can not read from adapter at this time.\n");
4788 count = iwl_read_restricted_mem(priv, base);
4790 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4791 IWL_ERROR("Start IWL Error Log Dump:\n");
4792 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4793 priv->status, priv->config, count);
4796 desc = iwl_read_restricted_mem(priv, base + 1 * sizeof(u32));
4797 blink1 = iwl_read_restricted_mem(priv, base + 3 * sizeof(u32));
4798 blink2 = iwl_read_restricted_mem(priv, base + 4 * sizeof(u32));
4799 ilink1 = iwl_read_restricted_mem(priv, base + 5 * sizeof(u32));
4800 ilink2 = iwl_read_restricted_mem(priv, base + 6 * sizeof(u32));
4801 data1 = iwl_read_restricted_mem(priv, base + 7 * sizeof(u32));
4802 data2 = iwl_read_restricted_mem(priv, base + 8 * sizeof(u32));
4803 line = iwl_read_restricted_mem(priv, base + 9 * sizeof(u32));
4804 time = iwl_read_restricted_mem(priv, base + 11 * sizeof(u32));
4806 IWL_ERROR("Desc Time "
4807 "data1 data2 line\n");
4808 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4809 desc_lookup(desc), desc, time, data1, data2, line);
4810 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4811 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4814 iwl_release_restricted_access(priv);
4817 #define EVENT_START_OFFSET (4 * sizeof(u32))
4820 * iwl_print_event_log - Dump error event log to syslog
4822 * NOTE: Must be called with iwl_grab_restricted_access() already obtained!
4824 static void iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
4825 u32 num_events, u32 mode)
4828 u32 base; /* SRAM byte address of event log header */
4829 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4830 u32 ptr; /* SRAM byte address of log data */
4831 u32 ev, time, data; /* event log data */
4833 if (num_events == 0)
4836 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4839 event_size = 2 * sizeof(u32);
4841 event_size = 3 * sizeof(u32);
4843 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4845 /* "time" is actually "data" for mode 0 (no timestamp).
4846 * place event id # at far right for easier visual parsing. */
4847 for (i = 0; i < num_events; i++) {
4848 ev = iwl_read_restricted_mem(priv, ptr);
4850 time = iwl_read_restricted_mem(priv, ptr);
4853 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4855 data = iwl_read_restricted_mem(priv, ptr);
4857 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4862 static void iwl_dump_nic_event_log(struct iwl_priv *priv)
4865 u32 base; /* SRAM byte address of event log header */
4866 u32 capacity; /* event log capacity in # entries */
4867 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4868 u32 num_wraps; /* # times uCode wrapped to top of log */
4869 u32 next_entry; /* index of next entry to be written by uCode */
4870 u32 size; /* # entries that we'll print */
4872 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4873 if (!iwl_hw_valid_rtc_data_addr(base)) {
4874 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4878 rc = iwl_grab_restricted_access(priv);
4880 IWL_WARNING("Can not read from adapter at this time.\n");
4884 /* event log header */
4885 capacity = iwl_read_restricted_mem(priv, base);
4886 mode = iwl_read_restricted_mem(priv, base + (1 * sizeof(u32)));
4887 num_wraps = iwl_read_restricted_mem(priv, base + (2 * sizeof(u32)));
4888 next_entry = iwl_read_restricted_mem(priv, base + (3 * sizeof(u32)));
4890 size = num_wraps ? capacity : next_entry;
4892 /* bail out if nothing in log */
4894 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
4895 iwl_release_restricted_access(priv);
4899 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
4902 /* if uCode has wrapped back to top of log, start at the oldest entry,
4903 * i.e the next one that uCode would fill. */
4905 iwl_print_event_log(priv, next_entry,
4906 capacity - next_entry, mode);
4908 /* (then/else) start at top of log */
4909 iwl_print_event_log(priv, 0, next_entry, mode);
4911 iwl_release_restricted_access(priv);
4915 * iwl_irq_handle_error - called for HW or SW error interrupt from card
4917 static void iwl_irq_handle_error(struct iwl_priv *priv)
4919 /* Set the FW error flag -- cleared on iwl_down */
4920 set_bit(STATUS_FW_ERROR, &priv->status);
4922 /* Cancel currently queued command. */
4923 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4925 #ifdef CONFIG_IWLWIFI_DEBUG
4926 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
4927 iwl_dump_nic_error_log(priv);
4928 iwl_dump_nic_event_log(priv);
4929 iwl_print_rx_config_cmd(&priv->staging_rxon);
4933 wake_up_interruptible(&priv->wait_command_queue);
4935 /* Keep the restart process from trying to send host
4936 * commands by clearing the INIT status bit */
4937 clear_bit(STATUS_READY, &priv->status);
4939 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4940 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4941 "Restarting adapter due to uCode error.\n");
4943 if (iwl_is_associated(priv)) {
4944 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4945 sizeof(priv->recovery_rxon));
4946 priv->error_recovering = 1;
4948 queue_work(priv->workqueue, &priv->restart);
4952 static void iwl_error_recovery(struct iwl_priv *priv)
4954 unsigned long flags;
4956 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4957 sizeof(priv->staging_rxon));
4958 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4959 iwl_commit_rxon(priv);
4961 iwl_rxon_add_station(priv, priv->bssid, 1);
4963 spin_lock_irqsave(&priv->lock, flags);
4964 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4965 priv->error_recovering = 0;
4966 spin_unlock_irqrestore(&priv->lock, flags);
4969 static void iwl_irq_tasklet(struct iwl_priv *priv)
4971 u32 inta, handled = 0;
4973 unsigned long flags;
4974 #ifdef CONFIG_IWLWIFI_DEBUG
4978 spin_lock_irqsave(&priv->lock, flags);
4980 /* Ack/clear/reset pending uCode interrupts.
4981 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4982 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
4983 inta = iwl_read32(priv, CSR_INT);
4984 iwl_write32(priv, CSR_INT, inta);
4986 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4987 * Any new interrupts that happen after this, either while we're
4988 * in this tasklet, or later, will show up in next ISR/tasklet. */
4989 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4990 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
4992 #ifdef CONFIG_IWLWIFI_DEBUG
4993 if (iwl_debug_level & IWL_DL_ISR) {
4994 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4995 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4996 inta, inta_mask, inta_fh);
5000 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
5001 * atomic, make sure that inta covers all the interrupts that
5002 * we've discovered, even if FH interrupt came in just after
5003 * reading CSR_INT. */
5004 if (inta_fh & CSR_FH_INT_RX_MASK)
5005 inta |= CSR_INT_BIT_FH_RX;
5006 if (inta_fh & CSR_FH_INT_TX_MASK)
5007 inta |= CSR_INT_BIT_FH_TX;
5009 /* Now service all interrupt bits discovered above. */
5010 if (inta & CSR_INT_BIT_HW_ERR) {
5011 IWL_ERROR("Microcode HW error detected. Restarting.\n");
5013 /* Tell the device to stop sending interrupts */
5014 iwl_disable_interrupts(priv);
5016 iwl_irq_handle_error(priv);
5018 handled |= CSR_INT_BIT_HW_ERR;
5020 spin_unlock_irqrestore(&priv->lock, flags);
5025 #ifdef CONFIG_IWLWIFI_DEBUG
5026 if (iwl_debug_level & (IWL_DL_ISR)) {
5027 /* NIC fires this, but we don't use it, redundant with WAKEUP */
5028 if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
5029 IWL_DEBUG_ISR("Microcode started or stopped.\n");
5031 /* Alive notification via Rx interrupt will do the real work */
5032 if (inta & CSR_INT_BIT_ALIVE)
5033 IWL_DEBUG_ISR("Alive interrupt\n");
5036 /* Safely ignore these bits for debug checks below */
5037 inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
5039 /* HW RF KILL switch toggled (4965 only) */
5040 if (inta & CSR_INT_BIT_RF_KILL) {
5042 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
5043 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
5046 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
5047 "RF_KILL bit toggled to %s.\n",
5048 hw_rf_kill ? "disable radio":"enable radio");
5050 /* Queue restart only if RF_KILL switch was set to "kill"
5051 * when we loaded driver, and is now set to "enable".
5052 * After we're Alive, RF_KILL gets handled by
5053 * iwl_rx_card_state_notif() */
5054 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status))
5055 queue_work(priv->workqueue, &priv->restart);
5057 handled |= CSR_INT_BIT_RF_KILL;
5060 /* Chip got too hot and stopped itself (4965 only) */
5061 if (inta & CSR_INT_BIT_CT_KILL) {
5062 IWL_ERROR("Microcode CT kill error detected.\n");
5063 handled |= CSR_INT_BIT_CT_KILL;
5066 /* Error detected by uCode */
5067 if (inta & CSR_INT_BIT_SW_ERR) {
5068 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
5070 iwl_irq_handle_error(priv);
5071 handled |= CSR_INT_BIT_SW_ERR;
5074 /* uCode wakes up after power-down sleep */
5075 if (inta & CSR_INT_BIT_WAKEUP) {
5076 IWL_DEBUG_ISR("Wakeup interrupt\n");
5077 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
5078 iwl_tx_queue_update_write_ptr(priv, &priv->txq[0]);
5079 iwl_tx_queue_update_write_ptr(priv, &priv->txq[1]);
5080 iwl_tx_queue_update_write_ptr(priv, &priv->txq[2]);
5081 iwl_tx_queue_update_write_ptr(priv, &priv->txq[3]);
5082 iwl_tx_queue_update_write_ptr(priv, &priv->txq[4]);
5083 iwl_tx_queue_update_write_ptr(priv, &priv->txq[5]);
5085 handled |= CSR_INT_BIT_WAKEUP;
5088 /* All uCode command responses, including Tx command responses,
5089 * Rx "responses" (frame-received notification), and other
5090 * notifications from uCode come through here*/
5091 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
5092 iwl_rx_handle(priv);
5093 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
5096 if (inta & CSR_INT_BIT_FH_TX) {
5097 IWL_DEBUG_ISR("Tx interrupt\n");
5098 handled |= CSR_INT_BIT_FH_TX;
5101 if (inta & ~handled)
5102 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
5104 if (inta & ~CSR_INI_SET_MASK) {
5105 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
5106 inta & ~CSR_INI_SET_MASK);
5107 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
5110 /* Re-enable all interrupts */
5111 iwl_enable_interrupts(priv);
5113 #ifdef CONFIG_IWLWIFI_DEBUG
5114 if (iwl_debug_level & (IWL_DL_ISR)) {
5115 inta = iwl_read32(priv, CSR_INT);
5116 inta_mask = iwl_read32(priv, CSR_INT_MASK);
5117 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
5118 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
5119 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
5122 spin_unlock_irqrestore(&priv->lock, flags);
5125 static irqreturn_t iwl_isr(int irq, void *data)
5127 struct iwl_priv *priv = data;
5128 u32 inta, inta_mask;
5133 spin_lock(&priv->lock);
5135 /* Disable (but don't clear!) interrupts here to avoid
5136 * back-to-back ISRs and sporadic interrupts from our NIC.
5137 * If we have something to service, the tasklet will re-enable ints.
5138 * If we *don't* have something, we'll re-enable before leaving here. */
5139 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
5140 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
5142 /* Discover which interrupts are active/pending */
5143 inta = iwl_read32(priv, CSR_INT);
5144 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
5146 /* Ignore interrupt if there's nothing in NIC to service.
5147 * This may be due to IRQ shared with another device,
5148 * or due to sporadic interrupts thrown from our NIC. */
5149 if (!inta && !inta_fh) {
5150 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
5154 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
5155 /* Hardware disappeared */
5156 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
5160 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5161 inta, inta_mask, inta_fh);
5163 /* iwl_irq_tasklet() will service interrupts and re-enable them */
5164 tasklet_schedule(&priv->irq_tasklet);
5165 spin_unlock(&priv->lock);
5170 /* re-enable interrupts here since we don't have anything to service. */
5171 iwl_enable_interrupts(priv);
5172 spin_unlock(&priv->lock);
5176 /************************** EEPROM BANDS ****************************
5178 * The iwl_eeprom_band definitions below provide the mapping from the
5179 * EEPROM contents to the specific channel number supported for each
5182 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
5183 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5184 * The specific geography and calibration information for that channel
5185 * is contained in the eeprom map itself.
5187 * During init, we copy the eeprom information and channel map
5188 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5190 * channel_map_24/52 provides the index in the channel_info array for a
5191 * given channel. We have to have two separate maps as there is channel
5192 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5195 * A value of 0xff stored in the channel_map indicates that the channel
5196 * is not supported by the hardware at all.
5198 * A value of 0xfe in the channel_map indicates that the channel is not
5199 * valid for Tx with the current hardware. This means that
5200 * while the system can tune and receive on a given channel, it may not
5201 * be able to associate or transmit any frames on that
5202 * channel. There is no corresponding channel information for that
5205 *********************************************************************/
5208 static const u8 iwl_eeprom_band_1[14] = {
5209 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5213 static const u8 iwl_eeprom_band_2[] = {
5214 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5217 static const u8 iwl_eeprom_band_3[] = { /* 5205-5320MHz */
5218 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5221 static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
5222 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5225 static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
5226 145, 149, 153, 157, 161, 165
5229 static u8 iwl_eeprom_band_6[] = { /* 2.4 FAT channel */
5233 static u8 iwl_eeprom_band_7[] = { /* 5.2 FAT channel */
5234 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5237 static void iwl_init_band_reference(const struct iwl_priv *priv, int band,
5238 int *eeprom_ch_count,
5239 const struct iwl_eeprom_channel
5241 const u8 **eeprom_ch_index)
5244 case 1: /* 2.4GHz band */
5245 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
5246 *eeprom_ch_info = priv->eeprom.band_1_channels;
5247 *eeprom_ch_index = iwl_eeprom_band_1;
5249 case 2: /* 5.2GHz band */
5250 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
5251 *eeprom_ch_info = priv->eeprom.band_2_channels;
5252 *eeprom_ch_index = iwl_eeprom_band_2;
5254 case 3: /* 5.2GHz band */
5255 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
5256 *eeprom_ch_info = priv->eeprom.band_3_channels;
5257 *eeprom_ch_index = iwl_eeprom_band_3;
5259 case 4: /* 5.2GHz band */
5260 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
5261 *eeprom_ch_info = priv->eeprom.band_4_channels;
5262 *eeprom_ch_index = iwl_eeprom_band_4;
5264 case 5: /* 5.2GHz band */
5265 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
5266 *eeprom_ch_info = priv->eeprom.band_5_channels;
5267 *eeprom_ch_index = iwl_eeprom_band_5;
5270 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_6);
5271 *eeprom_ch_info = priv->eeprom.band_24_channels;
5272 *eeprom_ch_index = iwl_eeprom_band_6;
5275 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_7);
5276 *eeprom_ch_info = priv->eeprom.band_52_channels;
5277 *eeprom_ch_index = iwl_eeprom_band_7;
5285 const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
5286 int phymode, u16 channel)
5291 case MODE_IEEE80211A:
5292 for (i = 14; i < priv->channel_count; i++) {
5293 if (priv->channel_info[i].channel == channel)
5294 return &priv->channel_info[i];
5298 case MODE_IEEE80211B:
5299 case MODE_IEEE80211G:
5300 if (channel >= 1 && channel <= 14)
5301 return &priv->channel_info[channel - 1];
5309 #define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5312 static int iwl_init_channel_map(struct iwl_priv *priv)
5314 int eeprom_ch_count = 0;
5315 const u8 *eeprom_ch_index = NULL;
5316 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
5318 struct iwl_channel_info *ch_info;
5320 if (priv->channel_count) {
5321 IWL_DEBUG_INFO("Channel map already initialized.\n");
5325 if (priv->eeprom.version < 0x2f) {
5326 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5327 priv->eeprom.version);
5331 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5333 priv->channel_count =
5334 ARRAY_SIZE(iwl_eeprom_band_1) +
5335 ARRAY_SIZE(iwl_eeprom_band_2) +
5336 ARRAY_SIZE(iwl_eeprom_band_3) +
5337 ARRAY_SIZE(iwl_eeprom_band_4) +
5338 ARRAY_SIZE(iwl_eeprom_band_5);
5340 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5342 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
5343 priv->channel_count, GFP_KERNEL);
5344 if (!priv->channel_info) {
5345 IWL_ERROR("Could not allocate channel_info\n");
5346 priv->channel_count = 0;
5350 ch_info = priv->channel_info;
5352 /* Loop through the 5 EEPROM bands adding them in order to the
5353 * channel map we maintain (that contains additional information than
5354 * what just in the EEPROM) */
5355 for (band = 1; band <= 5; band++) {
5357 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5358 &eeprom_ch_info, &eeprom_ch_index);
5360 /* Loop through each band adding each of the channels */
5361 for (ch = 0; ch < eeprom_ch_count; ch++) {
5362 ch_info->channel = eeprom_ch_index[ch];
5363 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5366 /* permanently store EEPROM's channel regulatory flags
5367 * and max power in channel info database. */
5368 ch_info->eeprom = eeprom_ch_info[ch];
5370 /* Copy the run-time flags so they are there even on
5371 * invalid channels */
5372 ch_info->flags = eeprom_ch_info[ch].flags;
5374 if (!(is_channel_valid(ch_info))) {
5375 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5379 is_channel_a_band(ch_info) ?
5385 /* Initialize regulatory-based run-time data */
5386 ch_info->max_power_avg = ch_info->curr_txpow =
5387 eeprom_ch_info[ch].max_power_avg;
5388 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5389 ch_info->min_power = 0;
5391 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5392 " %ddBm): Ad-Hoc %ssupported\n",
5394 is_channel_a_band(ch_info) ?
5396 CHECK_AND_PRINT(IBSS),
5397 CHECK_AND_PRINT(ACTIVE),
5398 CHECK_AND_PRINT(RADAR),
5399 CHECK_AND_PRINT(WIDE),
5400 CHECK_AND_PRINT(NARROW),
5401 CHECK_AND_PRINT(DFS),
5402 eeprom_ch_info[ch].flags,
5403 eeprom_ch_info[ch].max_power_avg,
5404 ((eeprom_ch_info[ch].
5405 flags & EEPROM_CHANNEL_IBSS)
5406 && !(eeprom_ch_info[ch].
5407 flags & EEPROM_CHANNEL_RADAR))
5410 /* Set the user_txpower_limit to the highest power
5411 * supported by any channel */
5412 if (eeprom_ch_info[ch].max_power_avg >
5413 priv->user_txpower_limit)
5414 priv->user_txpower_limit =
5415 eeprom_ch_info[ch].max_power_avg;
5421 for (band = 6; band <= 7; band++) {
5423 u8 fat_extension_chan;
5425 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5426 &eeprom_ch_info, &eeprom_ch_index);
5428 phymode = (band == 6) ? MODE_IEEE80211B : MODE_IEEE80211A;
5429 /* Loop through each band adding each of the channels */
5430 for (ch = 0; ch < eeprom_ch_count; ch++) {
5433 ((eeprom_ch_index[ch] == 5) ||
5434 (eeprom_ch_index[ch] == 6) ||
5435 (eeprom_ch_index[ch] == 7)))
5436 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5438 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5440 iwl4965_set_fat_chan_info(priv, phymode,
5441 eeprom_ch_index[ch],
5442 &(eeprom_ch_info[ch]),
5443 fat_extension_chan);
5445 iwl4965_set_fat_chan_info(priv, phymode,
5446 (eeprom_ch_index[ch] + 4),
5447 &(eeprom_ch_info[ch]),
5448 HT_IE_EXT_CHANNEL_BELOW);
5455 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5456 * sending probe req. This should be set long enough to hear probe responses
5457 * from more than one AP. */
5458 #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5459 #define IWL_ACTIVE_DWELL_TIME_52 (10)
5461 /* For faster active scanning, scan will move to the next channel if fewer than
5462 * PLCP_QUIET_THRESH packets are heard on this channel within
5463 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5464 * time if it's a quiet channel (nothing responded to our probe, and there's
5465 * no other traffic).
5466 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5467 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5468 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5470 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5471 * Must be set longer than active dwell time.
5472 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5473 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5474 #define IWL_PASSIVE_DWELL_TIME_52 (10)
5475 #define IWL_PASSIVE_DWELL_BASE (100)
5476 #define IWL_CHANNEL_TUNE_TIME 5
5478 static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv, int phymode)
5480 if (phymode == MODE_IEEE80211A)
5481 return IWL_ACTIVE_DWELL_TIME_52;
5483 return IWL_ACTIVE_DWELL_TIME_24;
5486 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, int phymode)
5488 u16 active = iwl_get_active_dwell_time(priv, phymode);
5489 u16 passive = (phymode != MODE_IEEE80211A) ?
5490 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5491 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5493 if (iwl_is_associated(priv)) {
5494 /* If we're associated, we clamp the maximum passive
5495 * dwell time to be 98% of the beacon interval (minus
5496 * 2 * channel tune time) */
5497 passive = priv->beacon_int;
5498 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5499 passive = IWL_PASSIVE_DWELL_BASE;
5500 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5503 if (passive <= active)
5504 passive = active + 1;
5509 static int iwl_get_channels_for_scan(struct iwl_priv *priv, int phymode,
5510 u8 is_active, u8 direct_mask,
5511 struct iwl_scan_channel *scan_ch)
5513 const struct ieee80211_channel *channels = NULL;
5514 const struct ieee80211_hw_mode *hw_mode;
5515 const struct iwl_channel_info *ch_info;
5516 u16 passive_dwell = 0;
5517 u16 active_dwell = 0;
5520 hw_mode = iwl_get_hw_mode(priv, phymode);
5524 channels = hw_mode->channels;
5526 active_dwell = iwl_get_active_dwell_time(priv, phymode);
5527 passive_dwell = iwl_get_passive_dwell_time(priv, phymode);
5529 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5530 if (channels[i].chan ==
5531 le16_to_cpu(priv->active_rxon.channel)) {
5532 if (iwl_is_associated(priv)) {
5534 ("Skipping current channel %d\n",
5535 le16_to_cpu(priv->active_rxon.channel));
5538 } else if (priv->only_active_channel)
5541 scan_ch->channel = channels[i].chan;
5543 ch_info = iwl_get_channel_info(priv, phymode, scan_ch->channel);
5544 if (!is_channel_valid(ch_info)) {
5545 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5550 if (!is_active || is_channel_passive(ch_info) ||
5551 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5552 scan_ch->type = 0; /* passive */
5554 scan_ch->type = 1; /* active */
5556 if (scan_ch->type & 1)
5557 scan_ch->type |= (direct_mask << 1);
5559 if (is_channel_narrow(ch_info))
5560 scan_ch->type |= (1 << 7);
5562 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5563 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5565 /* Set power levels to defaults */
5566 scan_ch->tpc.dsp_atten = 110;
5567 /* scan_pwr_info->tpc.dsp_atten; */
5569 /*scan_pwr_info->tpc.tx_gain; */
5570 if (phymode == MODE_IEEE80211A)
5571 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5573 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5574 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
5576 scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
5580 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5582 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5583 (scan_ch->type & 1) ?
5584 active_dwell : passive_dwell);
5590 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5594 static void iwl_reset_channel_flag(struct iwl_priv *priv)
5597 for (i = 0; i < 3; i++) {
5598 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5599 for (j = 0; j < hw_mode->num_channels; j++)
5600 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5604 static void iwl_init_hw_rates(struct iwl_priv *priv,
5605 struct ieee80211_rate *rates)
5609 for (i = 0; i < IWL_RATE_COUNT; i++) {
5610 rates[i].rate = iwl_rates[i].ieee * 5;
5611 rates[i].val = i; /* Rate scaling will work on indexes */
5613 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5614 /* Only OFDM have the bits-per-symbol set */
5615 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5616 rates[i].flags |= IEEE80211_RATE_OFDM;
5619 * If CCK 1M then set rate flag to CCK else CCK_2
5620 * which is CCK | PREAMBLE2
5622 rates[i].flags |= (iwl_rates[i].plcp == 10) ?
5623 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5626 /* Set up which ones are basic rates... */
5627 if (IWL_BASIC_RATES_MASK & (1 << i))
5628 rates[i].flags |= IEEE80211_RATE_BASIC;
5631 iwl4965_init_hw_rates(priv, rates);
5635 * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
5637 static int iwl_init_geos(struct iwl_priv *priv)
5639 struct iwl_channel_info *ch;
5640 struct ieee80211_hw_mode *modes;
5641 struct ieee80211_channel *channels;
5642 struct ieee80211_channel *geo_ch;
5643 struct ieee80211_rate *rates;
5655 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5656 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5660 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5665 channels = kzalloc(sizeof(struct ieee80211_channel) *
5666 priv->channel_count, GFP_KERNEL);
5672 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5685 /* 5.2GHz channels start after the 2.4GHz channels */
5686 modes[A].mode = MODE_IEEE80211A;
5687 modes[A].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
5688 modes[A].rates = rates;
5689 modes[A].num_rates = 8; /* just OFDM */
5690 modes[A].rates = &rates[4];
5691 modes[A].num_channels = 0;
5693 modes[B].mode = MODE_IEEE80211B;
5694 modes[B].channels = channels;
5695 modes[B].rates = rates;
5696 modes[B].num_rates = 4; /* just CCK */
5697 modes[B].num_channels = 0;
5699 modes[G].mode = MODE_IEEE80211G;
5700 modes[G].channels = channels;
5701 modes[G].rates = rates;
5702 modes[G].num_rates = 12; /* OFDM & CCK */
5703 modes[G].num_channels = 0;
5705 modes[G_11N].mode = MODE_IEEE80211G;
5706 modes[G_11N].channels = channels;
5707 modes[G_11N].num_rates = 13; /* OFDM & CCK */
5708 modes[G_11N].rates = rates;
5709 modes[G_11N].num_channels = 0;
5711 modes[A_11N].mode = MODE_IEEE80211A;
5712 modes[A_11N].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
5713 modes[A_11N].rates = &rates[4];
5714 modes[A_11N].num_rates = 9; /* just OFDM */
5715 modes[A_11N].num_channels = 0;
5717 priv->ieee_channels = channels;
5718 priv->ieee_rates = rates;
5720 iwl_init_hw_rates(priv, rates);
5722 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5723 ch = &priv->channel_info[i];
5725 if (!is_channel_valid(ch)) {
5726 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5728 ch->channel, is_channel_a_band(ch) ?
5733 if (is_channel_a_band(ch)) {
5734 geo_ch = &modes[A].channels[modes[A].num_channels++];
5735 modes[A_11N].num_channels++;
5737 geo_ch = &modes[B].channels[modes[B].num_channels++];
5738 modes[G].num_channels++;
5739 modes[G_11N].num_channels++;
5742 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5743 geo_ch->chan = ch->channel;
5744 geo_ch->power_level = ch->max_power_avg;
5745 geo_ch->antenna_max = 0xff;
5747 if (is_channel_valid(ch)) {
5748 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5749 if (ch->flags & EEPROM_CHANNEL_IBSS)
5750 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5752 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5753 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5755 if (ch->flags & EEPROM_CHANNEL_RADAR)
5756 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5758 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5759 priv->max_channel_txpower_limit =
5763 geo_ch->val = geo_ch->flag;
5766 if ((modes[A].num_channels == 0) && priv->is_abg) {
5767 printk(KERN_INFO DRV_NAME
5768 ": Incorrectly detected BG card as ABG. Please send "
5769 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5770 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5774 printk(KERN_INFO DRV_NAME
5775 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5776 modes[G].num_channels, modes[A].num_channels);
5779 * NOTE: We register these in preference of order -- the
5780 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5781 * a phymode based on rates or AP capabilities but seems to
5782 * configure it purely on if the channel being configured
5783 * is supported by a mode -- and the first match is taken
5786 if (modes[G].num_channels)
5787 ieee80211_register_hwmode(priv->hw, &modes[G]);
5788 if (modes[B].num_channels)
5789 ieee80211_register_hwmode(priv->hw, &modes[B]);
5790 if (modes[A].num_channels)
5791 ieee80211_register_hwmode(priv->hw, &modes[A]);
5793 priv->modes = modes;
5794 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5799 /******************************************************************************
5801 * uCode download functions
5803 ******************************************************************************/
5805 static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
5807 if (priv->ucode_code.v_addr != NULL) {
5808 pci_free_consistent(priv->pci_dev,
5809 priv->ucode_code.len,
5810 priv->ucode_code.v_addr,
5811 priv->ucode_code.p_addr);
5812 priv->ucode_code.v_addr = NULL;
5814 if (priv->ucode_data.v_addr != NULL) {
5815 pci_free_consistent(priv->pci_dev,
5816 priv->ucode_data.len,
5817 priv->ucode_data.v_addr,
5818 priv->ucode_data.p_addr);
5819 priv->ucode_data.v_addr = NULL;
5821 if (priv->ucode_data_backup.v_addr != NULL) {
5822 pci_free_consistent(priv->pci_dev,
5823 priv->ucode_data_backup.len,
5824 priv->ucode_data_backup.v_addr,
5825 priv->ucode_data_backup.p_addr);
5826 priv->ucode_data_backup.v_addr = NULL;
5828 if (priv->ucode_init.v_addr != NULL) {
5829 pci_free_consistent(priv->pci_dev,
5830 priv->ucode_init.len,
5831 priv->ucode_init.v_addr,
5832 priv->ucode_init.p_addr);
5833 priv->ucode_init.v_addr = NULL;
5835 if (priv->ucode_init_data.v_addr != NULL) {
5836 pci_free_consistent(priv->pci_dev,
5837 priv->ucode_init_data.len,
5838 priv->ucode_init_data.v_addr,
5839 priv->ucode_init_data.p_addr);
5840 priv->ucode_init_data.v_addr = NULL;
5842 if (priv->ucode_boot.v_addr != NULL) {
5843 pci_free_consistent(priv->pci_dev,
5844 priv->ucode_boot.len,
5845 priv->ucode_boot.v_addr,
5846 priv->ucode_boot.p_addr);
5847 priv->ucode_boot.v_addr = NULL;
5852 * iwl_verify_inst_full - verify runtime uCode image in card vs. host,
5853 * looking at all data.
5855 static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 * image, u32 len)
5862 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5864 rc = iwl_grab_restricted_access(priv);
5868 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
5871 for (; len > 0; len -= sizeof(u32), image++) {
5872 /* read data comes through single port, auto-incr addr */
5873 /* NOTE: Use the debugless read so we don't flood kernel log
5874 * if IWL_DL_IO is set */
5875 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5876 if (val != le32_to_cpu(*image)) {
5877 IWL_ERROR("uCode INST section is invalid at "
5878 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5879 save_len - len, val, le32_to_cpu(*image));
5887 iwl_release_restricted_access(priv);
5891 ("ucode image in INSTRUCTION memory is good\n");
5898 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
5899 * using sample data 100 bytes apart. If these sample points are good,
5900 * it's a pretty good bet that everything between them is good, too.
5902 static int iwl_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
5909 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5911 rc = iwl_grab_restricted_access(priv);
5915 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5916 /* read data comes through single port, auto-incr addr */
5917 /* NOTE: Use the debugless read so we don't flood kernel log
5918 * if IWL_DL_IO is set */
5919 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR,
5920 i + RTC_INST_LOWER_BOUND);
5921 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5922 if (val != le32_to_cpu(*image)) {
5923 #if 0 /* Enable this if you want to see details */
5924 IWL_ERROR("uCode INST section is invalid at "
5925 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5935 iwl_release_restricted_access(priv);
5942 * iwl_verify_ucode - determine which instruction image is in SRAM,
5943 * and verify its contents
5945 static int iwl_verify_ucode(struct iwl_priv *priv)
5952 image = (__le32 *)priv->ucode_boot.v_addr;
5953 len = priv->ucode_boot.len;
5954 rc = iwl_verify_inst_sparse(priv, image, len);
5956 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5960 /* Try initialize */
5961 image = (__le32 *)priv->ucode_init.v_addr;
5962 len = priv->ucode_init.len;
5963 rc = iwl_verify_inst_sparse(priv, image, len);
5965 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5969 /* Try runtime/protocol */
5970 image = (__le32 *)priv->ucode_code.v_addr;
5971 len = priv->ucode_code.len;
5972 rc = iwl_verify_inst_sparse(priv, image, len);
5974 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5978 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5980 /* Show first several data entries in instruction SRAM.
5981 * Selection of bootstrap image is arbitrary. */
5982 image = (__le32 *)priv->ucode_boot.v_addr;
5983 len = priv->ucode_boot.len;
5984 rc = iwl_verify_inst_full(priv, image, len);
5990 /* check contents of special bootstrap uCode SRAM */
5991 static int iwl_verify_bsm(struct iwl_priv *priv)
5993 __le32 *image = priv->ucode_boot.v_addr;
5994 u32 len = priv->ucode_boot.len;
5998 IWL_DEBUG_INFO("Begin verify bsm\n");
6000 /* verify BSM SRAM contents */
6001 val = iwl_read_restricted_reg(priv, BSM_WR_DWCOUNT_REG);
6002 for (reg = BSM_SRAM_LOWER_BOUND;
6003 reg < BSM_SRAM_LOWER_BOUND + len;
6004 reg += sizeof(u32), image ++) {
6005 val = iwl_read_restricted_reg(priv, reg);
6006 if (val != le32_to_cpu(*image)) {
6007 IWL_ERROR("BSM uCode verification failed at "
6008 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
6009 BSM_SRAM_LOWER_BOUND,
6010 reg - BSM_SRAM_LOWER_BOUND, len,
6011 val, le32_to_cpu(*image));
6016 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
6022 * iwl_load_bsm - Load bootstrap instructions
6026 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
6027 * in special SRAM that does not power down during RFKILL. When powering back
6028 * up after power-saving sleeps (or during initial uCode load), the BSM loads
6029 * the bootstrap program into the on-board processor, and starts it.
6031 * The bootstrap program loads (via DMA) instructions and data for a new
6032 * program from host DRAM locations indicated by the host driver in the
6033 * BSM_DRAM_* registers. Once the new program is loaded, it starts
6036 * When initializing the NIC, the host driver points the BSM to the
6037 * "initialize" uCode image. This uCode sets up some internal data, then
6038 * notifies host via "initialize alive" that it is complete.
6040 * The host then replaces the BSM_DRAM_* pointer values to point to the
6041 * normal runtime uCode instructions and a backup uCode data cache buffer
6042 * (filled initially with starting data values for the on-board processor),
6043 * then triggers the "initialize" uCode to load and launch the runtime uCode,
6044 * which begins normal operation.
6046 * When doing a power-save shutdown, runtime uCode saves data SRAM into
6047 * the backup data cache in DRAM before SRAM is powered down.
6049 * When powering back up, the BSM loads the bootstrap program. This reloads
6050 * the runtime uCode instructions and the backup data cache into SRAM,
6051 * and re-launches the runtime uCode from where it left off.
6053 static int iwl_load_bsm(struct iwl_priv *priv)
6055 __le32 *image = priv->ucode_boot.v_addr;
6056 u32 len = priv->ucode_boot.len;
6066 IWL_DEBUG_INFO("Begin load bsm\n");
6068 /* make sure bootstrap program is no larger than BSM's SRAM size */
6069 if (len > IWL_MAX_BSM_SIZE)
6072 /* Tell bootstrap uCode where to find the "Initialize" uCode
6073 * in host DRAM ... bits 31:0 for 3945, bits 35:4 for 4965.
6074 * NOTE: iwl_initialize_alive_start() will replace these values,
6075 * after the "initialize" uCode has run, to point to
6076 * runtime/protocol instructions and backup data cache. */
6077 pinst = priv->ucode_init.p_addr >> 4;
6078 pdata = priv->ucode_init_data.p_addr >> 4;
6079 inst_len = priv->ucode_init.len;
6080 data_len = priv->ucode_init_data.len;
6082 rc = iwl_grab_restricted_access(priv);
6086 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
6087 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6088 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
6089 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
6091 /* Fill BSM memory with bootstrap instructions */
6092 for (reg_offset = BSM_SRAM_LOWER_BOUND;
6093 reg_offset < BSM_SRAM_LOWER_BOUND + len;
6094 reg_offset += sizeof(u32), image++)
6095 _iwl_write_restricted_reg(priv, reg_offset,
6096 le32_to_cpu(*image));
6098 rc = iwl_verify_bsm(priv);
6100 iwl_release_restricted_access(priv);
6104 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
6105 iwl_write_restricted_reg(priv, BSM_WR_MEM_SRC_REG, 0x0);
6106 iwl_write_restricted_reg(priv, BSM_WR_MEM_DST_REG,
6107 RTC_INST_LOWER_BOUND);
6108 iwl_write_restricted_reg(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
6110 /* Load bootstrap code into instruction SRAM now,
6111 * to prepare to load "initialize" uCode */
6112 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
6113 BSM_WR_CTRL_REG_BIT_START);
6115 /* Wait for load of bootstrap uCode to finish */
6116 for (i = 0; i < 100; i++) {
6117 done = iwl_read_restricted_reg(priv, BSM_WR_CTRL_REG);
6118 if (!(done & BSM_WR_CTRL_REG_BIT_START))
6123 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
6125 IWL_ERROR("BSM write did not complete!\n");
6129 /* Enable future boot loads whenever power management unit triggers it
6130 * (e.g. when powering back up after power-save shutdown) */
6131 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
6132 BSM_WR_CTRL_REG_BIT_START_EN);
6134 iwl_release_restricted_access(priv);
6139 static void iwl_nic_start(struct iwl_priv *priv)
6141 /* Remove all resets to allow NIC to operate */
6142 iwl_write32(priv, CSR_RESET, 0);
6146 * iwl_read_ucode - Read uCode images from disk file.
6148 * Copy into buffers for card to fetch via bus-mastering
6150 static int iwl_read_ucode(struct iwl_priv *priv)
6152 struct iwl_ucode *ucode;
6154 const struct firmware *ucode_raw;
6155 const char *name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode";
6158 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
6160 /* Ask kernel firmware_class module to get the boot firmware off disk.
6161 * request_firmware() is synchronous, file is in memory on return. */
6162 rc = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
6164 IWL_ERROR("%s firmware file req failed: Reason %d\n", name, rc);
6168 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
6169 name, ucode_raw->size);
6171 /* Make sure that we got at least our header! */
6172 if (ucode_raw->size < sizeof(*ucode)) {
6173 IWL_ERROR("File size way too small!\n");
6178 /* Data from ucode file: header followed by uCode images */
6179 ucode = (void *)ucode_raw->data;
6181 ver = le32_to_cpu(ucode->ver);
6182 inst_size = le32_to_cpu(ucode->inst_size);
6183 data_size = le32_to_cpu(ucode->data_size);
6184 init_size = le32_to_cpu(ucode->init_size);
6185 init_data_size = le32_to_cpu(ucode->init_data_size);
6186 boot_size = le32_to_cpu(ucode->boot_size);
6188 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
6189 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
6191 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
6193 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
6195 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
6197 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
6200 /* Verify size of file vs. image size info in file's header */
6201 if (ucode_raw->size < sizeof(*ucode) +
6202 inst_size + data_size + init_size +
6203 init_data_size + boot_size) {
6205 IWL_DEBUG_INFO("uCode file size %d too small\n",
6206 (int)ucode_raw->size);
6211 /* Verify that uCode images will fit in card's SRAM */
6212 if (inst_size > IWL_MAX_INST_SIZE) {
6213 IWL_DEBUG_INFO("uCode instr len %d too large to fit in card\n",
6219 if (data_size > IWL_MAX_DATA_SIZE) {
6220 IWL_DEBUG_INFO("uCode data len %d too large to fit in card\n",
6225 if (init_size > IWL_MAX_INST_SIZE) {
6227 ("uCode init instr len %d too large to fit in card\n",
6232 if (init_data_size > IWL_MAX_DATA_SIZE) {
6234 ("uCode init data len %d too large to fit in card\n",
6235 (int)init_data_size);
6239 if (boot_size > IWL_MAX_BSM_SIZE) {
6241 ("uCode boot instr len %d too large to fit in bsm\n",
6247 /* Allocate ucode buffers for card's bus-master loading ... */
6249 /* Runtime instructions and 2 copies of data:
6250 * 1) unmodified from disk
6251 * 2) backup cache for save/restore during power-downs */
6252 priv->ucode_code.len = inst_size;
6253 priv->ucode_code.v_addr =
6254 pci_alloc_consistent(priv->pci_dev,
6255 priv->ucode_code.len,
6256 &(priv->ucode_code.p_addr));
6258 priv->ucode_data.len = data_size;
6259 priv->ucode_data.v_addr =
6260 pci_alloc_consistent(priv->pci_dev,
6261 priv->ucode_data.len,
6262 &(priv->ucode_data.p_addr));
6264 priv->ucode_data_backup.len = data_size;
6265 priv->ucode_data_backup.v_addr =
6266 pci_alloc_consistent(priv->pci_dev,
6267 priv->ucode_data_backup.len,
6268 &(priv->ucode_data_backup.p_addr));
6271 /* Initialization instructions and data */
6272 priv->ucode_init.len = init_size;
6273 priv->ucode_init.v_addr =
6274 pci_alloc_consistent(priv->pci_dev,
6275 priv->ucode_init.len,
6276 &(priv->ucode_init.p_addr));
6278 priv->ucode_init_data.len = init_data_size;
6279 priv->ucode_init_data.v_addr =
6280 pci_alloc_consistent(priv->pci_dev,
6281 priv->ucode_init_data.len,
6282 &(priv->ucode_init_data.p_addr));
6284 /* Bootstrap (instructions only, no data) */
6285 priv->ucode_boot.len = boot_size;
6286 priv->ucode_boot.v_addr =
6287 pci_alloc_consistent(priv->pci_dev,
6288 priv->ucode_boot.len,
6289 &(priv->ucode_boot.p_addr));
6291 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
6292 !priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr ||
6293 !priv->ucode_boot.v_addr || !priv->ucode_data_backup.v_addr)
6296 /* Copy images into buffers for card's bus-master reads ... */
6298 /* Runtime instructions (first block of data in file) */
6299 src = &ucode->data[0];
6300 len = priv->ucode_code.len;
6301 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %d\n",
6303 memcpy(priv->ucode_code.v_addr, src, len);
6304 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6305 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6307 /* Runtime data (2nd block)
6308 * NOTE: Copy into backup buffer will be done in iwl_up() */
6309 src = &ucode->data[inst_size];
6310 len = priv->ucode_data.len;
6311 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %d\n",
6313 memcpy(priv->ucode_data.v_addr, src, len);
6314 memcpy(priv->ucode_data_backup.v_addr, src, len);
6316 /* Initialization instructions (3rd block) */
6318 src = &ucode->data[inst_size + data_size];
6319 len = priv->ucode_init.len;
6320 IWL_DEBUG_INFO("Copying (but not loading) init instr len %d\n",
6322 memcpy(priv->ucode_init.v_addr, src, len);
6325 /* Initialization data (4th block) */
6326 if (init_data_size) {
6327 src = &ucode->data[inst_size + data_size + init_size];
6328 len = priv->ucode_init_data.len;
6329 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
6331 memcpy(priv->ucode_init_data.v_addr, src, len);
6334 /* Bootstrap instructions (5th block) */
6335 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6336 len = priv->ucode_boot.len;
6337 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
6339 memcpy(priv->ucode_boot.v_addr, src, len);
6341 /* We have our copies now, allow OS release its copies */
6342 release_firmware(ucode_raw);
6346 IWL_ERROR("failed to allocate pci memory\n");
6348 iwl_dealloc_ucode_pci(priv);
6351 release_firmware(ucode_raw);
6359 * iwl_set_ucode_ptrs - Set uCode address location
6361 * Tell initialization uCode where to find runtime uCode.
6363 * BSM registers initially contain pointers to initialization uCode.
6364 * We need to replace them to load runtime uCode inst and data,
6365 * and to save runtime data when powering down.
6367 static int iwl_set_ucode_ptrs(struct iwl_priv *priv)
6372 unsigned long flags;
6374 /* bits 35:4 for 4965 */
6375 pinst = priv->ucode_code.p_addr >> 4;
6376 pdata = priv->ucode_data_backup.p_addr >> 4;
6378 spin_lock_irqsave(&priv->lock, flags);
6379 rc = iwl_grab_restricted_access(priv);
6381 spin_unlock_irqrestore(&priv->lock, flags);
6385 /* Tell bootstrap uCode where to find image to load */
6386 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
6387 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6388 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
6389 priv->ucode_data.len);
6391 /* Inst bytecount must be last to set up, bit 31 signals uCode
6392 * that all new ptr/size info is in place */
6393 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG,
6394 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6396 iwl_release_restricted_access(priv);
6398 spin_unlock_irqrestore(&priv->lock, flags);
6400 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6406 * iwl_init_alive_start - Called after REPLY_ALIVE notification receieved
6408 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6410 * The 4965 "initialize" ALIVE reply contains calibration data for:
6411 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6412 * (3945 does not contain this data).
6414 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6416 static void iwl_init_alive_start(struct iwl_priv *priv)
6418 /* Check alive response for "valid" sign from uCode */
6419 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6420 /* We had an error bringing up the hardware, so take it
6421 * all the way back down so we can try again */
6422 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6426 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6427 * This is a paranoid check, because we would not have gotten the
6428 * "initialize" alive if code weren't properly loaded. */
6429 if (iwl_verify_ucode(priv)) {
6430 /* Runtime instruction load was bad;
6431 * take it all the way back down so we can try again */
6432 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6436 /* Calculate temperature */
6437 priv->temperature = iwl4965_get_temperature(priv);
6439 /* Send pointers to protocol/runtime uCode image ... init code will
6440 * load and launch runtime uCode, which will send us another "Alive"
6442 IWL_DEBUG_INFO("Initialization Alive received.\n");
6443 if (iwl_set_ucode_ptrs(priv)) {
6444 /* Runtime instruction load won't happen;
6445 * take it all the way back down so we can try again */
6446 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6452 queue_work(priv->workqueue, &priv->restart);
6457 * iwl_alive_start - called after REPLY_ALIVE notification received
6458 * from protocol/runtime uCode (initialization uCode's
6459 * Alive gets handled by iwl_init_alive_start()).
6461 static void iwl_alive_start(struct iwl_priv *priv)
6465 IWL_DEBUG_INFO("Runtime Alive received.\n");
6467 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6468 /* We had an error bringing up the hardware, so take it
6469 * all the way back down so we can try again */
6470 IWL_DEBUG_INFO("Alive failed.\n");
6474 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6475 * This is a paranoid check, because we would not have gotten the
6476 * "runtime" alive if code weren't properly loaded. */
6477 if (iwl_verify_ucode(priv)) {
6478 /* Runtime instruction load was bad;
6479 * take it all the way back down so we can try again */
6480 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6484 iwl_clear_stations_table(priv);
6486 rc = iwl4965_alive_notify(priv);
6488 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6493 /* After the ALIVE response, we can process host commands */
6494 set_bit(STATUS_ALIVE, &priv->status);
6496 /* Clear out the uCode error bit if it is set */
6497 clear_bit(STATUS_FW_ERROR, &priv->status);
6499 rc = iwl_init_channel_map(priv);
6501 IWL_ERROR("initializing regulatory failed: %d\n", rc);
6505 iwl_init_geos(priv);
6507 if (iwl_is_rfkill(priv))
6510 if (!priv->mac80211_registered) {
6511 /* Unlock so any user space entry points can call back into
6512 * the driver without a deadlock... */
6513 mutex_unlock(&priv->mutex);
6514 iwl_rate_control_register(priv->hw);
6515 rc = ieee80211_register_hw(priv->hw);
6516 priv->hw->conf.beacon_int = 100;
6517 mutex_lock(&priv->mutex);
6520 IWL_ERROR("Failed to register network "
6521 "device (error %d)\n", rc);
6525 priv->mac80211_registered = 1;
6527 iwl_reset_channel_flag(priv);
6529 ieee80211_start_queues(priv->hw);
6531 priv->active_rate = priv->rates_mask;
6532 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6534 iwl_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
6536 if (iwl_is_associated(priv)) {
6537 struct iwl_rxon_cmd *active_rxon =
6538 (struct iwl_rxon_cmd *)(&priv->active_rxon);
6540 memcpy(&priv->staging_rxon, &priv->active_rxon,
6541 sizeof(priv->staging_rxon));
6542 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6544 /* Initialize our rx_config data */
6545 iwl_connection_init_rx_config(priv);
6546 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6549 /* Configure BT coexistence */
6550 iwl_send_bt_config(priv);
6552 /* Configure the adapter for unassociated operation */
6553 iwl_commit_rxon(priv);
6555 /* At this point, the NIC is initialized and operational */
6556 priv->notif_missed_beacons = 0;
6557 set_bit(STATUS_READY, &priv->status);
6559 iwl4965_rf_kill_ct_config(priv);
6560 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6562 if (priv->error_recovering)
6563 iwl_error_recovery(priv);
6568 queue_work(priv->workqueue, &priv->restart);
6571 static void iwl_cancel_deferred_work(struct iwl_priv *priv);
6573 static void __iwl_down(struct iwl_priv *priv)
6575 unsigned long flags;
6576 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6577 struct ieee80211_conf *conf = NULL;
6579 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6581 conf = ieee80211_get_hw_conf(priv->hw);
6584 set_bit(STATUS_EXIT_PENDING, &priv->status);
6586 iwl_clear_stations_table(priv);
6588 /* Unblock any waiting calls */
6589 wake_up_interruptible_all(&priv->wait_command_queue);
6591 iwl_cancel_deferred_work(priv);
6593 /* Wipe out the EXIT_PENDING status bit if we are not actually
6594 * exiting the module */
6596 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6598 /* stop and reset the on-board processor */
6599 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
6601 /* tell the device to stop sending interrupts */
6602 iwl_disable_interrupts(priv);
6604 if (priv->mac80211_registered)
6605 ieee80211_stop_queues(priv->hw);
6607 /* If we have not previously called iwl_init() then
6608 * clear all bits but the RF Kill and SUSPEND bits and return */
6609 if (!iwl_is_init(priv)) {
6610 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6612 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6614 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6619 /* ...otherwise clear out all the status bits but the RF Kill and
6620 * SUSPEND bits and continue taking the NIC down. */
6621 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6623 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6625 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6627 test_bit(STATUS_FW_ERROR, &priv->status) <<
6630 spin_lock_irqsave(&priv->lock, flags);
6631 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
6632 spin_unlock_irqrestore(&priv->lock, flags);
6634 iwl_hw_txq_ctx_stop(priv);
6635 iwl_hw_rxq_stop(priv);
6637 spin_lock_irqsave(&priv->lock, flags);
6638 if (!iwl_grab_restricted_access(priv)) {
6639 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
6640 APMG_CLK_VAL_DMA_CLK_RQT);
6641 iwl_release_restricted_access(priv);
6643 spin_unlock_irqrestore(&priv->lock, flags);
6647 iwl_hw_nic_stop_master(priv);
6648 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6649 iwl_hw_nic_reset(priv);
6652 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
6654 if (priv->ibss_beacon)
6655 dev_kfree_skb(priv->ibss_beacon);
6656 priv->ibss_beacon = NULL;
6658 /* clear out any free frames */
6659 iwl_clear_free_frames(priv);
6662 static void iwl_down(struct iwl_priv *priv)
6664 mutex_lock(&priv->mutex);
6666 mutex_unlock(&priv->mutex);
6669 #define MAX_HW_RESTARTS 5
6671 static int __iwl_up(struct iwl_priv *priv)
6673 DECLARE_MAC_BUF(mac);
6677 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6678 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6682 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6683 IWL_WARNING("Radio disabled by SW RF kill (module "
6688 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6690 rc = iwl_hw_nic_init(priv);
6692 IWL_ERROR("Unable to int nic\n");
6696 /* make sure rfkill handshake bits are cleared */
6697 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6698 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
6699 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6701 /* clear (again), then enable host interrupts */
6702 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6703 iwl_enable_interrupts(priv);
6705 /* really make sure rfkill handshake bits are cleared */
6706 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6707 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6709 /* Copy original ucode data image from disk into backup cache.
6710 * This will be used to initialize the on-board processor's
6711 * data SRAM for a clean start when the runtime program first loads. */
6712 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6713 priv->ucode_data.len);
6715 /* If platform's RF_KILL switch is set to KILL,
6716 * wait for BIT_INT_RF_KILL interrupt before loading uCode
6717 * and getting things started */
6718 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
6719 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
6722 if (test_bit(STATUS_RF_KILL_HW, &priv->status) || hw_rf_kill) {
6723 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6727 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6729 iwl_clear_stations_table(priv);
6731 /* load bootstrap state machine,
6732 * load bootstrap program into processor's memory,
6733 * prepare to load the "initialize" uCode */
6734 rc = iwl_load_bsm(priv);
6737 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6741 /* start card; "initialize" will load runtime ucode */
6742 iwl_nic_start(priv);
6744 /* MAC Address location in EEPROM same for 3945/4965 */
6745 get_eeprom_mac(priv, priv->mac_addr);
6746 IWL_DEBUG_INFO("MAC address: %s\n",
6747 print_mac(mac, priv->mac_addr));
6749 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6751 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6756 set_bit(STATUS_EXIT_PENDING, &priv->status);
6759 /* tried to restart and config the device for as long as our
6760 * patience could withstand */
6761 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6766 /*****************************************************************************
6768 * Workqueue callbacks
6770 *****************************************************************************/
6772 static void iwl_bg_init_alive_start(struct work_struct *data)
6774 struct iwl_priv *priv =
6775 container_of(data, struct iwl_priv, init_alive_start.work);
6777 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6780 mutex_lock(&priv->mutex);
6781 iwl_init_alive_start(priv);
6782 mutex_unlock(&priv->mutex);
6785 static void iwl_bg_alive_start(struct work_struct *data)
6787 struct iwl_priv *priv =
6788 container_of(data, struct iwl_priv, alive_start.work);
6790 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6793 mutex_lock(&priv->mutex);
6794 iwl_alive_start(priv);
6795 mutex_unlock(&priv->mutex);
6798 static void iwl_bg_rf_kill(struct work_struct *work)
6800 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
6802 wake_up_interruptible(&priv->wait_command_queue);
6804 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6807 mutex_lock(&priv->mutex);
6809 if (!iwl_is_rfkill(priv)) {
6810 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6811 "HW and/or SW RF Kill no longer active, restarting "
6813 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6814 queue_work(priv->workqueue, &priv->restart);
6817 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6818 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6819 "disabled by SW switch\n");
6821 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6822 "Kill switch must be turned off for "
6823 "wireless networking to work.\n");
6825 mutex_unlock(&priv->mutex);
6828 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6830 static void iwl_bg_scan_check(struct work_struct *data)
6832 struct iwl_priv *priv =
6833 container_of(data, struct iwl_priv, scan_check.work);
6835 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6838 mutex_lock(&priv->mutex);
6839 if (test_bit(STATUS_SCANNING, &priv->status) ||
6840 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6841 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6842 "Scan completion watchdog resetting adapter (%dms)\n",
6843 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
6844 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6845 queue_work(priv->workqueue, &priv->restart);
6847 mutex_unlock(&priv->mutex);
6850 static void iwl_bg_request_scan(struct work_struct *data)
6852 struct iwl_priv *priv =
6853 container_of(data, struct iwl_priv, request_scan);
6854 struct iwl_host_cmd cmd = {
6855 .id = REPLY_SCAN_CMD,
6856 .len = sizeof(struct iwl_scan_cmd),
6857 .meta.flags = CMD_SIZE_HUGE,
6860 struct iwl_scan_cmd *scan;
6861 struct ieee80211_conf *conf = NULL;
6865 conf = ieee80211_get_hw_conf(priv->hw);
6867 mutex_lock(&priv->mutex);
6869 if (!iwl_is_ready(priv)) {
6870 IWL_WARNING("request scan called when driver not ready.\n");
6874 /* Make sure the scan wasn't cancelled before this queued work
6875 * was given the chance to run... */
6876 if (!test_bit(STATUS_SCANNING, &priv->status))
6879 /* This should never be called or scheduled if there is currently
6880 * a scan active in the hardware. */
6881 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6882 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6883 "Ignoring second request.\n");
6888 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6889 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6893 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6894 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6898 if (iwl_is_rfkill(priv)) {
6899 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6903 if (!test_bit(STATUS_READY, &priv->status)) {
6904 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6908 if (!priv->scan_bands) {
6909 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6914 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
6915 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6922 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
6924 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6925 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6927 if (iwl_is_associated(priv)) {
6930 u32 suspend_time = 100;
6931 u32 scan_suspend_time = 100;
6932 unsigned long flags;
6934 IWL_DEBUG_INFO("Scanning while associated...\n");
6936 spin_lock_irqsave(&priv->lock, flags);
6937 interval = priv->beacon_int;
6938 spin_unlock_irqrestore(&priv->lock, flags);
6940 scan->suspend_time = 0;
6941 scan->max_out_time = cpu_to_le32(600 * 1024);
6943 interval = suspend_time;
6945 extra = (suspend_time / interval) << 22;
6946 scan_suspend_time = (extra |
6947 ((suspend_time % interval) * 1024));
6948 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6949 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6950 scan_suspend_time, interval);
6953 /* We should add the ability for user to lock to PASSIVE ONLY */
6954 if (priv->one_direct_scan) {
6956 ("Kicking off one direct scan for '%s'\n",
6957 iwl_escape_essid(priv->direct_ssid,
6958 priv->direct_ssid_len));
6959 scan->direct_scan[0].id = WLAN_EID_SSID;
6960 scan->direct_scan[0].len = priv->direct_ssid_len;
6961 memcpy(scan->direct_scan[0].ssid,
6962 priv->direct_ssid, priv->direct_ssid_len);
6964 } else if (!iwl_is_associated(priv)) {
6965 scan->direct_scan[0].id = WLAN_EID_SSID;
6966 scan->direct_scan[0].len = priv->essid_len;
6967 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6972 /* We don't build a direct scan probe request; the uCode will do
6973 * that based on the direct_mask added to each channel entry */
6974 scan->tx_cmd.len = cpu_to_le16(
6975 iwl_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
6976 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
6977 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6978 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6979 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6981 /* flags + rate selection */
6983 scan->tx_cmd.tx_flags |= cpu_to_le32(0x200);
6985 switch (priv->scan_bands) {
6987 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6988 scan->tx_cmd.rate_n_flags =
6989 iwl_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
6990 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6992 scan->good_CRC_th = 0;
6993 phymode = MODE_IEEE80211G;
6997 scan->tx_cmd.rate_n_flags =
6998 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
6999 RATE_MCS_ANT_B_MSK);
7000 scan->good_CRC_th = IWL_GOOD_CRC_TH;
7001 phymode = MODE_IEEE80211A;
7005 IWL_WARNING("Invalid scan band count\n");
7009 /* select Rx chains */
7011 /* Force use of chains B and C (0x6) for scan Rx.
7012 * Avoid A (0x1) because of its off-channel reception on A-band.
7013 * MIMO is not used here, but value is required to make uCode happy. */
7014 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
7015 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
7016 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
7017 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
7019 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
7020 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
7024 ("Initiating direct scan for %s.\n",
7025 iwl_escape_essid(priv->essid, priv->essid_len));
7027 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
7029 scan->channel_count =
7030 iwl_get_channels_for_scan(
7031 priv, phymode, 1, /* active */
7033 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
7035 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
7036 scan->channel_count * sizeof(struct iwl_scan_channel);
7038 scan->len = cpu_to_le16(cmd.len);
7040 set_bit(STATUS_SCAN_HW, &priv->status);
7041 rc = iwl_send_cmd_sync(priv, &cmd);
7045 queue_delayed_work(priv->workqueue, &priv->scan_check,
7046 IWL_SCAN_CHECK_WATCHDOG);
7048 mutex_unlock(&priv->mutex);
7052 /* inform mac80211 sacn aborted */
7053 queue_work(priv->workqueue, &priv->scan_completed);
7054 mutex_unlock(&priv->mutex);
7057 static void iwl_bg_up(struct work_struct *data)
7059 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
7061 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7064 mutex_lock(&priv->mutex);
7066 mutex_unlock(&priv->mutex);
7069 static void iwl_bg_restart(struct work_struct *data)
7071 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
7073 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7077 queue_work(priv->workqueue, &priv->up);
7080 static void iwl_bg_rx_replenish(struct work_struct *data)
7082 struct iwl_priv *priv =
7083 container_of(data, struct iwl_priv, rx_replenish);
7085 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7088 mutex_lock(&priv->mutex);
7089 iwl_rx_replenish(priv);
7090 mutex_unlock(&priv->mutex);
7093 static void iwl_bg_post_associate(struct work_struct *data)
7095 struct iwl_priv *priv = container_of(data, struct iwl_priv,
7096 post_associate.work);
7099 struct ieee80211_conf *conf = NULL;
7100 DECLARE_MAC_BUF(mac);
7102 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7103 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
7107 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
7109 print_mac(mac, priv->active_rxon.bssid_addr));
7112 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7115 mutex_lock(&priv->mutex);
7117 conf = ieee80211_get_hw_conf(priv->hw);
7119 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7120 iwl_commit_rxon(priv);
7122 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
7123 iwl_setup_rxon_timing(priv);
7124 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7125 sizeof(priv->rxon_timing), &priv->rxon_timing);
7127 IWL_WARNING("REPLY_RXON_TIMING failed - "
7128 "Attempting to continue.\n");
7130 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7132 #ifdef CONFIG_IWLWIFI_HT
7133 if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
7134 iwl4965_set_rxon_ht(priv, &priv->current_assoc_ht);
7136 priv->active_rate_ht[0] = 0;
7137 priv->active_rate_ht[1] = 0;
7138 priv->current_channel_width = IWL_CHANNEL_WIDTH_20MHZ;
7140 #endif /* CONFIG_IWLWIFI_HT*/
7141 iwl4965_set_rxon_chain(priv);
7142 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7144 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
7145 priv->assoc_id, priv->beacon_int);
7147 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7148 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7150 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7152 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7153 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
7154 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
7156 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7158 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7159 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7163 iwl_commit_rxon(priv);
7165 switch (priv->iw_mode) {
7166 case IEEE80211_IF_TYPE_STA:
7167 iwl_rate_scale_init(priv->hw, IWL_AP_ID);
7170 case IEEE80211_IF_TYPE_IBSS:
7172 /* clear out the station table */
7173 iwl_clear_stations_table(priv);
7175 iwl_rxon_add_station(priv, BROADCAST_ADDR, 0);
7176 iwl_rxon_add_station(priv, priv->bssid, 0);
7177 iwl_rate_scale_init(priv->hw, IWL_STA_ID);
7178 iwl_send_beacon_cmd(priv);
7183 IWL_ERROR("%s Should not be called in %d mode\n",
7184 __FUNCTION__, priv->iw_mode);
7188 iwl_sequence_reset(priv);
7190 #ifdef CONFIG_IWLWIFI_SENSITIVITY
7191 /* Enable Rx differential gain and sensitivity calibrations */
7192 iwl4965_chain_noise_reset(priv);
7193 priv->start_calib = 1;
7194 #endif /* CONFIG_IWLWIFI_SENSITIVITY */
7196 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7197 priv->assoc_station_added = 1;
7199 #ifdef CONFIG_IWLWIFI_QOS
7200 iwl_activate_qos(priv, 0);
7201 #endif /* CONFIG_IWLWIFI_QOS */
7202 mutex_unlock(&priv->mutex);
7205 static void iwl_bg_abort_scan(struct work_struct *work)
7207 struct iwl_priv *priv = container_of(work, struct iwl_priv,
7210 if (!iwl_is_ready(priv))
7213 mutex_lock(&priv->mutex);
7215 set_bit(STATUS_SCAN_ABORTING, &priv->status);
7216 iwl_send_scan_abort(priv);
7218 mutex_unlock(&priv->mutex);
7221 static void iwl_bg_scan_completed(struct work_struct *work)
7223 struct iwl_priv *priv =
7224 container_of(work, struct iwl_priv, scan_completed);
7226 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
7228 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7231 ieee80211_scan_completed(priv->hw);
7233 /* Since setting the TXPOWER may have been deferred while
7234 * performing the scan, fire one off */
7235 mutex_lock(&priv->mutex);
7236 iwl_hw_reg_send_txpower(priv);
7237 mutex_unlock(&priv->mutex);
7240 /*****************************************************************************
7242 * mac80211 entry point functions
7244 *****************************************************************************/
7246 static int iwl_mac_start(struct ieee80211_hw *hw)
7248 struct iwl_priv *priv = hw->priv;
7250 IWL_DEBUG_MAC80211("enter\n");
7252 /* we should be verifying the device is ready to be opened */
7253 mutex_lock(&priv->mutex);
7257 if (!iwl_is_rfkill(priv))
7258 ieee80211_start_queues(priv->hw);
7260 mutex_unlock(&priv->mutex);
7261 IWL_DEBUG_MAC80211("leave\n");
7265 static void iwl_mac_stop(struct ieee80211_hw *hw)
7267 struct iwl_priv *priv = hw->priv;
7269 IWL_DEBUG_MAC80211("enter\n");
7271 /*netif_stop_queue(dev); */
7272 flush_workqueue(priv->workqueue);
7273 IWL_DEBUG_MAC80211("leave\n");
7276 static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
7277 struct ieee80211_tx_control *ctl)
7279 struct iwl_priv *priv = hw->priv;
7281 IWL_DEBUG_MAC80211("enter\n");
7283 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7284 IWL_DEBUG_MAC80211("leave - monitor\n");
7288 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
7291 if (iwl_tx_skb(priv, skb, ctl))
7292 dev_kfree_skb_any(skb);
7294 IWL_DEBUG_MAC80211("leave\n");
7298 static int iwl_mac_add_interface(struct ieee80211_hw *hw,
7299 struct ieee80211_if_init_conf *conf)
7301 struct iwl_priv *priv = hw->priv;
7302 unsigned long flags;
7303 DECLARE_MAC_BUF(mac);
7305 IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
7307 IWL_DEBUG_MAC80211("enter: MAC %s\n",
7308 print_mac(mac, conf->mac_addr));
7310 if (priv->interface_id) {
7311 IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
7315 spin_lock_irqsave(&priv->lock, flags);
7316 priv->interface_id = conf->if_id;
7318 spin_unlock_irqrestore(&priv->lock, flags);
7320 mutex_lock(&priv->mutex);
7321 iwl_set_mode(priv, conf->type);
7323 IWL_DEBUG_MAC80211("leave\n");
7324 mutex_unlock(&priv->mutex);
7330 * iwl_mac_config - mac80211 config callback
7332 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7333 * be set inappropriately and the driver currently sets the hardware up to
7334 * use it whenever needed.
7336 static int iwl_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
7338 struct iwl_priv *priv = hw->priv;
7339 const struct iwl_channel_info *ch_info;
7340 unsigned long flags;
7342 mutex_lock(&priv->mutex);
7343 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
7345 if (!iwl_is_ready(priv)) {
7346 IWL_DEBUG_MAC80211("leave - not ready\n");
7347 mutex_unlock(&priv->mutex);
7351 /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
7352 * what is exposed through include/ declrations */
7353 if (unlikely(!iwl_param_disable_hw_scan &&
7354 test_bit(STATUS_SCANNING, &priv->status))) {
7355 IWL_DEBUG_MAC80211("leave - scanning\n");
7356 mutex_unlock(&priv->mutex);
7360 spin_lock_irqsave(&priv->lock, flags);
7362 ch_info = iwl_get_channel_info(priv, conf->phymode, conf->channel);
7363 if (!is_channel_valid(ch_info)) {
7364 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
7365 conf->channel, conf->phymode);
7366 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7367 spin_unlock_irqrestore(&priv->lock, flags);
7368 mutex_unlock(&priv->mutex);
7372 #ifdef CONFIG_IWLWIFI_HT
7373 /* if we are switching fron ht to 2.4 clear flags
7374 * from any ht related info since 2.4 does not
7376 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel)
7377 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
7378 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7381 priv->staging_rxon.flags = 0;
7382 #endif /* CONFIG_IWLWIFI_HT */
7384 iwl_set_rxon_channel(priv, conf->phymode, conf->channel);
7386 iwl_set_flags_for_phymode(priv, conf->phymode);
7388 /* The list of supported rates and rate mask can be different
7389 * for each phymode; since the phymode may have changed, reset
7390 * the rate mask to what mac80211 lists */
7393 spin_unlock_irqrestore(&priv->lock, flags);
7395 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
7396 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
7397 iwl_hw_channel_switch(priv, conf->channel);
7398 mutex_unlock(&priv->mutex);
7403 iwl_radio_kill_sw(priv, !conf->radio_enabled);
7405 if (!conf->radio_enabled) {
7406 IWL_DEBUG_MAC80211("leave - radio disabled\n");
7407 mutex_unlock(&priv->mutex);
7411 if (iwl_is_rfkill(priv)) {
7412 IWL_DEBUG_MAC80211("leave - RF kill\n");
7413 mutex_unlock(&priv->mutex);
7419 if (memcmp(&priv->active_rxon,
7420 &priv->staging_rxon, sizeof(priv->staging_rxon)))
7421 iwl_commit_rxon(priv);
7423 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7425 IWL_DEBUG_MAC80211("leave\n");
7427 mutex_unlock(&priv->mutex);
7432 static void iwl_config_ap(struct iwl_priv *priv)
7436 if (priv->status & STATUS_EXIT_PENDING)
7439 /* The following should be done only at AP bring up */
7440 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7442 /* RXON - unassoc (to set timing command) */
7443 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7444 iwl_commit_rxon(priv);
7447 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
7448 iwl_setup_rxon_timing(priv);
7449 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7450 sizeof(priv->rxon_timing), &priv->rxon_timing);
7452 IWL_WARNING("REPLY_RXON_TIMING failed - "
7453 "Attempting to continue.\n");
7455 iwl4965_set_rxon_chain(priv);
7457 /* FIXME: what should be the assoc_id for AP? */
7458 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7459 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7460 priv->staging_rxon.flags |=
7461 RXON_FLG_SHORT_PREAMBLE_MSK;
7463 priv->staging_rxon.flags &=
7464 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7466 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7467 if (priv->assoc_capability &
7468 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7469 priv->staging_rxon.flags |=
7470 RXON_FLG_SHORT_SLOT_MSK;
7472 priv->staging_rxon.flags &=
7473 ~RXON_FLG_SHORT_SLOT_MSK;
7475 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7476 priv->staging_rxon.flags &=
7477 ~RXON_FLG_SHORT_SLOT_MSK;
7479 /* restore RXON assoc */
7480 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7481 iwl_commit_rxon(priv);
7482 #ifdef CONFIG_IWLWIFI_QOS
7483 iwl_activate_qos(priv, 1);
7485 iwl_rxon_add_station(priv, BROADCAST_ADDR, 0);
7487 iwl_send_beacon_cmd(priv);
7489 /* FIXME - we need to add code here to detect a totally new
7490 * configuration, reset the AP, unassoc, rxon timing, assoc,
7491 * clear sta table, add BCAST sta... */
7494 static int iwl_mac_config_interface(struct ieee80211_hw *hw, int if_id,
7495 struct ieee80211_if_conf *conf)
7497 struct iwl_priv *priv = hw->priv;
7498 DECLARE_MAC_BUF(mac);
7499 unsigned long flags;
7505 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7506 (!conf->beacon || !conf->ssid_len)) {
7508 ("Leaving in AP mode because HostAPD is not ready.\n");
7512 mutex_lock(&priv->mutex);
7514 IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
7516 IWL_DEBUG_MAC80211("bssid: %s\n",
7517 print_mac(mac, conf->bssid));
7520 * very dubious code was here; the probe filtering flag is never set:
7522 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7523 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
7525 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
7526 IWL_DEBUG_MAC80211("leave - scanning\n");
7527 mutex_unlock(&priv->mutex);
7531 if (priv->interface_id != if_id) {
7532 IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
7533 mutex_unlock(&priv->mutex);
7537 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7539 conf->bssid = priv->mac_addr;
7540 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
7541 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7542 print_mac(mac, conf->bssid));
7544 if (priv->ibss_beacon)
7545 dev_kfree_skb(priv->ibss_beacon);
7547 priv->ibss_beacon = conf->beacon;
7550 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7551 !is_multicast_ether_addr(conf->bssid)) {
7552 /* If there is currently a HW scan going on in the background
7553 * then we need to cancel it else the RXON below will fail. */
7554 if (iwl_scan_cancel_timeout(priv, 100)) {
7555 IWL_WARNING("Aborted scan still in progress "
7557 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7558 mutex_unlock(&priv->mutex);
7561 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7563 /* TODO: Audit driver for usage of these members and see
7564 * if mac80211 deprecates them (priv->bssid looks like it
7565 * shouldn't be there, but I haven't scanned the IBSS code
7566 * to verify) - jpk */
7567 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7569 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7570 iwl_config_ap(priv);
7572 priv->staging_rxon.filter_flags |=
7573 RXON_FILTER_ASSOC_MSK;
7574 rc = iwl_commit_rxon(priv);
7575 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
7576 iwl_rxon_add_station(
7577 priv, priv->active_rxon.bssid_addr, 1);
7581 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7582 iwl_commit_rxon(priv);
7585 spin_lock_irqsave(&priv->lock, flags);
7586 if (!conf->ssid_len)
7587 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7589 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7591 priv->essid_len = conf->ssid_len;
7592 spin_unlock_irqrestore(&priv->lock, flags);
7594 IWL_DEBUG_MAC80211("leave\n");
7595 mutex_unlock(&priv->mutex);
7600 static void iwl_configure_filter(struct ieee80211_hw *hw,
7601 unsigned int changed_flags,
7602 unsigned int *total_flags,
7603 int mc_count, struct dev_addr_list *mc_list)
7607 * see also iwl_connection_init_rx_config
7612 static void iwl_mac_remove_interface(struct ieee80211_hw *hw,
7613 struct ieee80211_if_init_conf *conf)
7615 struct iwl_priv *priv = hw->priv;
7617 IWL_DEBUG_MAC80211("enter\n");
7619 mutex_lock(&priv->mutex);
7620 if (priv->interface_id == conf->if_id) {
7621 priv->interface_id = 0;
7622 memset(priv->bssid, 0, ETH_ALEN);
7623 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7624 priv->essid_len = 0;
7626 mutex_unlock(&priv->mutex);
7628 IWL_DEBUG_MAC80211("leave\n");
7632 #define IWL_DELAY_NEXT_SCAN (HZ*2)
7633 static int iwl_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
7636 unsigned long flags;
7637 struct iwl_priv *priv = hw->priv;
7639 IWL_DEBUG_MAC80211("enter\n");
7641 spin_lock_irqsave(&priv->lock, flags);
7643 if (!iwl_is_ready_rf(priv)) {
7645 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7649 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7651 IWL_ERROR("ERROR: APs don't scan\n");
7655 /* if we just finished scan ask for delay */
7656 if (priv->last_scan_jiffies &&
7657 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
7663 IWL_DEBUG_SCAN("direct scan for "
7665 iwl_escape_essid(ssid, len), (int)len);
7667 priv->one_direct_scan = 1;
7668 priv->direct_ssid_len = (u8)
7669 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7670 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
7673 rc = iwl_scan_initiate(priv);
7675 IWL_DEBUG_MAC80211("leave\n");
7678 spin_unlock_irqrestore(&priv->lock, flags);
7683 static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
7684 const u8 *local_addr, const u8 *addr,
7685 struct ieee80211_key_conf *key)
7687 struct iwl_priv *priv = hw->priv;
7688 DECLARE_MAC_BUF(mac);
7692 IWL_DEBUG_MAC80211("enter\n");
7694 if (!iwl_param_hwcrypto) {
7695 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7699 if (is_zero_ether_addr(addr))
7700 /* only support pairwise keys */
7703 sta_id = iwl_hw_find_station(priv, addr);
7704 if (sta_id == IWL_INVALID_STATION) {
7705 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7706 print_mac(mac, addr));
7710 mutex_lock(&priv->mutex);
7714 rc = iwl_update_sta_key_info(priv, key, sta_id);
7716 iwl_set_rxon_hwcrypto(priv, 1);
7717 iwl_commit_rxon(priv);
7718 key->hw_key_idx = sta_id;
7719 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7720 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7724 rc = iwl_clear_sta_key_info(priv, sta_id);
7726 iwl_set_rxon_hwcrypto(priv, 0);
7727 iwl_commit_rxon(priv);
7728 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7735 IWL_DEBUG_MAC80211("leave\n");
7736 mutex_unlock(&priv->mutex);
7741 static int iwl_mac_conf_tx(struct ieee80211_hw *hw, int queue,
7742 const struct ieee80211_tx_queue_params *params)
7744 struct iwl_priv *priv = hw->priv;
7745 #ifdef CONFIG_IWLWIFI_QOS
7746 unsigned long flags;
7748 #endif /* CONFIG_IWL_QOS */
7750 IWL_DEBUG_MAC80211("enter\n");
7752 if (!iwl_is_ready_rf(priv)) {
7753 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7757 if (queue >= AC_NUM) {
7758 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7762 #ifdef CONFIG_IWLWIFI_QOS
7763 if (!priv->qos_data.qos_enable) {
7764 priv->qos_data.qos_active = 0;
7765 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7768 q = AC_NUM - 1 - queue;
7770 spin_lock_irqsave(&priv->lock, flags);
7772 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7773 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7774 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7775 priv->qos_data.def_qos_parm.ac[q].edca_txop =
7776 cpu_to_le16((params->burst_time * 100));
7778 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7779 priv->qos_data.qos_active = 1;
7781 spin_unlock_irqrestore(&priv->lock, flags);
7783 mutex_lock(&priv->mutex);
7784 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7785 iwl_activate_qos(priv, 1);
7786 else if (priv->assoc_id && iwl_is_associated(priv))
7787 iwl_activate_qos(priv, 0);
7789 mutex_unlock(&priv->mutex);
7791 #endif /*CONFIG_IWLWIFI_QOS */
7793 IWL_DEBUG_MAC80211("leave\n");
7797 static int iwl_mac_get_tx_stats(struct ieee80211_hw *hw,
7798 struct ieee80211_tx_queue_stats *stats)
7800 struct iwl_priv *priv = hw->priv;
7802 struct iwl_tx_queue *txq;
7803 struct iwl_queue *q;
7804 unsigned long flags;
7806 IWL_DEBUG_MAC80211("enter\n");
7808 if (!iwl_is_ready_rf(priv)) {
7809 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7813 spin_lock_irqsave(&priv->lock, flags);
7815 for (i = 0; i < AC_NUM; i++) {
7816 txq = &priv->txq[i];
7818 avail = iwl_queue_space(q);
7820 stats->data[i].len = q->n_window - avail;
7821 stats->data[i].limit = q->n_window - q->high_mark;
7822 stats->data[i].count = q->n_window;
7825 spin_unlock_irqrestore(&priv->lock, flags);
7827 IWL_DEBUG_MAC80211("leave\n");
7832 static int iwl_mac_get_stats(struct ieee80211_hw *hw,
7833 struct ieee80211_low_level_stats *stats)
7835 IWL_DEBUG_MAC80211("enter\n");
7836 IWL_DEBUG_MAC80211("leave\n");
7841 static u64 iwl_mac_get_tsf(struct ieee80211_hw *hw)
7843 IWL_DEBUG_MAC80211("enter\n");
7844 IWL_DEBUG_MAC80211("leave\n");
7849 static void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
7851 struct iwl_priv *priv = hw->priv;
7852 unsigned long flags;
7854 mutex_lock(&priv->mutex);
7855 IWL_DEBUG_MAC80211("enter\n");
7857 priv->lq_mngr.lq_ready = 0;
7858 #ifdef CONFIG_IWLWIFI_HT
7859 spin_lock_irqsave(&priv->lock, flags);
7860 memset(&priv->current_assoc_ht, 0, sizeof(struct sta_ht_info));
7861 spin_unlock_irqrestore(&priv->lock, flags);
7862 #ifdef CONFIG_IWLWIFI_HT_AGG
7863 /* if (priv->lq_mngr.agg_ctrl.granted_ba)
7864 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);*/
7866 memset(&(priv->lq_mngr.agg_ctrl), 0, sizeof(struct iwl_agg_control));
7867 priv->lq_mngr.agg_ctrl.tid_traffic_load_threshold = 10;
7868 priv->lq_mngr.agg_ctrl.ba_timeout = 5000;
7869 priv->lq_mngr.agg_ctrl.auto_agg = 1;
7871 if (priv->lq_mngr.agg_ctrl.auto_agg)
7872 priv->lq_mngr.agg_ctrl.requested_ba = TID_ALL_ENABLED;
7873 #endif /*CONFIG_IWLWIFI_HT_AGG */
7874 #endif /* CONFIG_IWLWIFI_HT */
7876 #ifdef CONFIG_IWLWIFI_QOS
7877 iwl_reset_qos(priv);
7880 cancel_delayed_work(&priv->post_associate);
7882 spin_lock_irqsave(&priv->lock, flags);
7884 priv->assoc_capability = 0;
7885 priv->call_post_assoc_from_beacon = 0;
7886 priv->assoc_station_added = 0;
7888 /* new association get rid of ibss beacon skb */
7889 if (priv->ibss_beacon)
7890 dev_kfree_skb(priv->ibss_beacon);
7892 priv->ibss_beacon = NULL;
7894 priv->beacon_int = priv->hw->conf.beacon_int;
7895 priv->timestamp1 = 0;
7896 priv->timestamp0 = 0;
7897 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7898 priv->beacon_int = 0;
7900 spin_unlock_irqrestore(&priv->lock, flags);
7902 /* Per mac80211.h: This is only used in IBSS mode... */
7903 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7904 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7905 mutex_unlock(&priv->mutex);
7909 if (!iwl_is_ready_rf(priv)) {
7910 IWL_DEBUG_MAC80211("leave - not ready\n");
7911 mutex_unlock(&priv->mutex);
7915 priv->only_active_channel = 0;
7919 mutex_unlock(&priv->mutex);
7921 IWL_DEBUG_MAC80211("leave\n");
7925 static int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
7926 struct ieee80211_tx_control *control)
7928 struct iwl_priv *priv = hw->priv;
7929 unsigned long flags;
7931 mutex_lock(&priv->mutex);
7932 IWL_DEBUG_MAC80211("enter\n");
7934 if (!iwl_is_ready_rf(priv)) {
7935 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7936 mutex_unlock(&priv->mutex);
7940 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7941 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7942 mutex_unlock(&priv->mutex);
7946 spin_lock_irqsave(&priv->lock, flags);
7948 if (priv->ibss_beacon)
7949 dev_kfree_skb(priv->ibss_beacon);
7951 priv->ibss_beacon = skb;
7955 IWL_DEBUG_MAC80211("leave\n");
7956 spin_unlock_irqrestore(&priv->lock, flags);
7958 #ifdef CONFIG_IWLWIFI_QOS
7959 iwl_reset_qos(priv);
7962 queue_work(priv->workqueue, &priv->post_associate.work);
7964 mutex_unlock(&priv->mutex);
7969 #ifdef CONFIG_IWLWIFI_HT
7972 u16 advanced_coding_cap :1;
7973 u16 supported_chan_width_set :1;
7974 u16 mimo_power_save_mode :2;
7980 u16 beam_forming :1;
7982 u16 maximal_amsdu_size :1;
7983 u16 cck_mode_at_40MHz :1;
7984 u16 psmp_support :1;
7985 u16 stbc_ctrl_frame_support :1;
7986 u16 sig_txop_protection_support :1;
7989 } __attribute__ ((packed));
7991 union ht_param_info{
7993 u8 max_rx_ampdu_factor :2;
7998 } __attribute__ ((packed));
8000 union ht_exra_param_info {
8002 u8 ext_chan_offset :2;
8003 u8 tx_chan_width :1;
8005 u8 controlled_access_only :1;
8006 u8 service_interval_granularity :3;
8009 } __attribute__ ((packed));
8011 union ht_operation_mode{
8018 } __attribute__ ((packed));
8021 static int sta_ht_info_init(struct ieee80211_ht_capability *ht_cap,
8022 struct ieee80211_ht_additional_info *ht_extra,
8023 struct sta_ht_info *ht_info_ap,
8024 struct sta_ht_info *ht_info)
8026 union ht_cap_info cap;
8027 union ht_operation_mode op_mode;
8028 union ht_param_info param_info;
8029 union ht_exra_param_info extra_param_info;
8031 IWL_DEBUG_MAC80211("enter: \n");
8034 IWL_DEBUG_MAC80211("leave: ht_info is NULL\n");
8039 cap.val = (u16) le16_to_cpu(ht_cap->capabilities_info);
8040 param_info.val = ht_cap->mac_ht_params_info;
8043 ht_info->sgf |= 0x1;
8045 ht_info->sgf |= 0x2;
8046 ht_info->is_green_field = cap.green_field;
8047 ht_info->max_amsdu_size = cap.maximal_amsdu_size;
8048 ht_info->supported_chan_width = cap.supported_chan_width_set;
8049 ht_info->tx_mimo_ps_mode = cap.mimo_power_save_mode;
8050 memcpy(ht_info->supp_rates, ht_cap->supported_mcs_set, 16);
8052 ht_info->ampdu_factor = param_info.max_rx_ampdu_factor;
8053 ht_info->mpdu_density = param_info.mpdu_density;
8055 IWL_DEBUG_MAC80211("SISO mask 0x%X MIMO mask 0x%X \n",
8056 ht_cap->supported_mcs_set[0],
8057 ht_cap->supported_mcs_set[1]);
8060 ht_info->control_channel = ht_info_ap->control_channel;
8061 ht_info->extension_chan_offset =
8062 ht_info_ap->extension_chan_offset;
8063 ht_info->tx_chan_width = ht_info_ap->tx_chan_width;
8064 ht_info->operating_mode = ht_info_ap->operating_mode;
8068 extra_param_info.val = ht_extra->ht_param;
8069 ht_info->control_channel = ht_extra->control_chan;
8070 ht_info->extension_chan_offset =
8071 extra_param_info.ext_chan_offset;
8072 ht_info->tx_chan_width = extra_param_info.tx_chan_width;
8074 le16_to_cpu(ht_extra->operation_mode);
8075 ht_info->operating_mode = op_mode.op_mode;
8076 IWL_DEBUG_MAC80211("control channel %d\n",
8077 ht_extra->control_chan);
8082 IWL_DEBUG_MAC80211("leave\n");
8086 static int iwl_mac_conf_ht(struct ieee80211_hw *hw,
8087 struct ieee80211_ht_capability *ht_cap,
8088 struct ieee80211_ht_additional_info *ht_extra)
8090 struct iwl_priv *priv = hw->priv;
8093 IWL_DEBUG_MAC80211("enter: \n");
8095 rs = sta_ht_info_init(ht_cap, ht_extra, NULL, &priv->current_assoc_ht);
8096 iwl4965_set_rxon_chain(priv);
8098 if (priv && priv->assoc_id &&
8099 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
8100 unsigned long flags;
8102 spin_lock_irqsave(&priv->lock, flags);
8103 if (priv->beacon_int)
8104 queue_work(priv->workqueue, &priv->post_associate.work);
8106 priv->call_post_assoc_from_beacon = 1;
8107 spin_unlock_irqrestore(&priv->lock, flags);
8110 IWL_DEBUG_MAC80211("leave: control channel %d\n",
8111 ht_extra->control_chan);
8116 static void iwl_set_ht_capab(struct ieee80211_hw *hw,
8117 struct ieee80211_ht_capability *ht_cap,
8120 union ht_cap_info cap;
8121 union ht_param_info param_info;
8123 memset(&cap, 0, sizeof(union ht_cap_info));
8124 memset(¶m_info, 0, sizeof(union ht_param_info));
8126 cap.maximal_amsdu_size = HT_IE_MAX_AMSDU_SIZE_4K;
8127 cap.green_field = 1;
8130 cap.supported_chan_width_set = use_wide_chan;
8131 cap.mimo_power_save_mode = 0x3;
8133 param_info.max_rx_ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
8134 param_info.mpdu_density = CFG_HT_MPDU_DENSITY_DEF;
8135 ht_cap->capabilities_info = (__le16) cpu_to_le16(cap.val);
8136 ht_cap->mac_ht_params_info = (u8) param_info.val;
8138 ht_cap->supported_mcs_set[0] = 0xff;
8139 ht_cap->supported_mcs_set[1] = 0xff;
8140 ht_cap->supported_mcs_set[4] =
8141 (cap.supported_chan_width_set) ? 0x1: 0x0;
8144 static void iwl_mac_get_ht_capab(struct ieee80211_hw *hw,
8145 struct ieee80211_ht_capability *ht_cap)
8147 u8 use_wide_channel = 1;
8148 struct iwl_priv *priv = hw->priv;
8150 IWL_DEBUG_MAC80211("enter: \n");
8151 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
8152 use_wide_channel = 0;
8154 /* no fat tx allowed on 2.4GHZ */
8155 if (priv->phymode != MODE_IEEE80211A)
8156 use_wide_channel = 0;
8158 iwl_set_ht_capab(hw, ht_cap, use_wide_channel);
8159 IWL_DEBUG_MAC80211("leave: \n");
8161 #endif /*CONFIG_IWLWIFI_HT*/
8163 /*****************************************************************************
8167 *****************************************************************************/
8169 #ifdef CONFIG_IWLWIFI_DEBUG
8172 * The following adds a new attribute to the sysfs representation
8173 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
8174 * used for controlling the debug level.
8176 * See the level definitions in iwl for details.
8179 static ssize_t show_debug_level(struct device_driver *d, char *buf)
8181 return sprintf(buf, "0x%08X\n", iwl_debug_level);
8183 static ssize_t store_debug_level(struct device_driver *d,
8184 const char *buf, size_t count)
8186 char *p = (char *)buf;
8189 val = simple_strtoul(p, &p, 0);
8191 printk(KERN_INFO DRV_NAME
8192 ": %s is not in hex or decimal form.\n", buf);
8194 iwl_debug_level = val;
8196 return strnlen(buf, count);
8199 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
8200 show_debug_level, store_debug_level);
8202 #endif /* CONFIG_IWLWIFI_DEBUG */
8204 static ssize_t show_rf_kill(struct device *d,
8205 struct device_attribute *attr, char *buf)
8208 * 0 - RF kill not enabled
8209 * 1 - SW based RF kill active (sysfs)
8210 * 2 - HW based RF kill active
8211 * 3 - Both HW and SW based RF kill active
8213 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8214 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
8215 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
8217 return sprintf(buf, "%i\n", val);
8220 static ssize_t store_rf_kill(struct device *d,
8221 struct device_attribute *attr,
8222 const char *buf, size_t count)
8224 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8226 mutex_lock(&priv->mutex);
8227 iwl_radio_kill_sw(priv, buf[0] == '1');
8228 mutex_unlock(&priv->mutex);
8233 static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
8235 static ssize_t show_temperature(struct device *d,
8236 struct device_attribute *attr, char *buf)
8238 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8240 if (!iwl_is_alive(priv))
8243 return sprintf(buf, "%d\n", iwl_hw_get_temperature(priv));
8246 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8248 static ssize_t show_rs_window(struct device *d,
8249 struct device_attribute *attr,
8252 struct iwl_priv *priv = d->driver_data;
8253 return iwl_fill_rs_info(priv->hw, buf, IWL_AP_ID);
8255 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8257 static ssize_t show_tx_power(struct device *d,
8258 struct device_attribute *attr, char *buf)
8260 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8261 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8264 static ssize_t store_tx_power(struct device *d,
8265 struct device_attribute *attr,
8266 const char *buf, size_t count)
8268 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8269 char *p = (char *)buf;
8272 val = simple_strtoul(p, &p, 10);
8274 printk(KERN_INFO DRV_NAME
8275 ": %s is not in decimal form.\n", buf);
8277 iwl_hw_reg_set_txpower(priv, val);
8282 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8284 static ssize_t show_flags(struct device *d,
8285 struct device_attribute *attr, char *buf)
8287 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8289 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8292 static ssize_t store_flags(struct device *d,
8293 struct device_attribute *attr,
8294 const char *buf, size_t count)
8296 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8297 u32 flags = simple_strtoul(buf, NULL, 0);
8299 mutex_lock(&priv->mutex);
8300 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8301 /* Cancel any currently running scans... */
8302 if (iwl_scan_cancel_timeout(priv, 100))
8303 IWL_WARNING("Could not cancel scan.\n");
8305 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8307 priv->staging_rxon.flags = cpu_to_le32(flags);
8308 iwl_commit_rxon(priv);
8311 mutex_unlock(&priv->mutex);
8316 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8318 static ssize_t show_filter_flags(struct device *d,
8319 struct device_attribute *attr, char *buf)
8321 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8323 return sprintf(buf, "0x%04X\n",
8324 le32_to_cpu(priv->active_rxon.filter_flags));
8327 static ssize_t store_filter_flags(struct device *d,
8328 struct device_attribute *attr,
8329 const char *buf, size_t count)
8331 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8332 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8334 mutex_lock(&priv->mutex);
8335 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8336 /* Cancel any currently running scans... */
8337 if (iwl_scan_cancel_timeout(priv, 100))
8338 IWL_WARNING("Could not cancel scan.\n");
8340 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8341 "0x%04X\n", filter_flags);
8342 priv->staging_rxon.filter_flags =
8343 cpu_to_le32(filter_flags);
8344 iwl_commit_rxon(priv);
8347 mutex_unlock(&priv->mutex);
8352 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8353 store_filter_flags);
8355 static ssize_t show_tune(struct device *d,
8356 struct device_attribute *attr, char *buf)
8358 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8360 return sprintf(buf, "0x%04X\n",
8361 (priv->phymode << 8) |
8362 le16_to_cpu(priv->active_rxon.channel));
8365 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode);
8367 static ssize_t store_tune(struct device *d,
8368 struct device_attribute *attr,
8369 const char *buf, size_t count)
8371 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8372 char *p = (char *)buf;
8373 u16 tune = simple_strtoul(p, &p, 0);
8374 u8 phymode = (tune >> 8) & 0xff;
8375 u16 channel = tune & 0xff;
8377 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
8379 mutex_lock(&priv->mutex);
8380 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
8381 (priv->phymode != phymode)) {
8382 const struct iwl_channel_info *ch_info;
8384 ch_info = iwl_get_channel_info(priv, phymode, channel);
8386 IWL_WARNING("Requested invalid phymode/channel "
8387 "combination: %d %d\n", phymode, channel);
8388 mutex_unlock(&priv->mutex);
8392 /* Cancel any currently running scans... */
8393 if (iwl_scan_cancel_timeout(priv, 100))
8394 IWL_WARNING("Could not cancel scan.\n");
8396 IWL_DEBUG_INFO("Committing phymode and "
8397 "rxon.channel = %d %d\n",
8400 iwl_set_rxon_channel(priv, phymode, channel);
8401 iwl_set_flags_for_phymode(priv, phymode);
8404 iwl_commit_rxon(priv);
8407 mutex_unlock(&priv->mutex);
8412 static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
8414 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8416 static ssize_t show_measurement(struct device *d,
8417 struct device_attribute *attr, char *buf)
8419 struct iwl_priv *priv = dev_get_drvdata(d);
8420 struct iwl_spectrum_notification measure_report;
8421 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8422 u8 *data = (u8 *) & measure_report;
8423 unsigned long flags;
8425 spin_lock_irqsave(&priv->lock, flags);
8426 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8427 spin_unlock_irqrestore(&priv->lock, flags);
8430 memcpy(&measure_report, &priv->measure_report, size);
8431 priv->measurement_status = 0;
8432 spin_unlock_irqrestore(&priv->lock, flags);
8434 while (size && (PAGE_SIZE - len)) {
8435 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8436 PAGE_SIZE - len, 1);
8438 if (PAGE_SIZE - len)
8442 size -= min(size, 16U);
8448 static ssize_t store_measurement(struct device *d,
8449 struct device_attribute *attr,
8450 const char *buf, size_t count)
8452 struct iwl_priv *priv = dev_get_drvdata(d);
8453 struct ieee80211_measurement_params params = {
8454 .channel = le16_to_cpu(priv->active_rxon.channel),
8455 .start_time = cpu_to_le64(priv->last_tsf),
8456 .duration = cpu_to_le16(1),
8458 u8 type = IWL_MEASURE_BASIC;
8464 strncpy(buffer, buf, min(sizeof(buffer), count));
8465 channel = simple_strtoul(p, NULL, 0);
8467 params.channel = channel;
8470 while (*p && *p != ' ')
8473 type = simple_strtoul(p + 1, NULL, 0);
8476 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8477 "channel %d (for '%s')\n", type, params.channel, buf);
8478 iwl_get_measurement(priv, ¶ms, type);
8483 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8484 show_measurement, store_measurement);
8485 #endif /* CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT */
8487 static ssize_t store_retry_rate(struct device *d,
8488 struct device_attribute *attr,
8489 const char *buf, size_t count)
8491 struct iwl_priv *priv = dev_get_drvdata(d);
8493 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8494 if (priv->retry_rate <= 0)
8495 priv->retry_rate = 1;
8500 static ssize_t show_retry_rate(struct device *d,
8501 struct device_attribute *attr, char *buf)
8503 struct iwl_priv *priv = dev_get_drvdata(d);
8504 return sprintf(buf, "%d", priv->retry_rate);
8507 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8510 static ssize_t store_power_level(struct device *d,
8511 struct device_attribute *attr,
8512 const char *buf, size_t count)
8514 struct iwl_priv *priv = dev_get_drvdata(d);
8518 mode = simple_strtoul(buf, NULL, 0);
8519 mutex_lock(&priv->mutex);
8521 if (!iwl_is_ready(priv)) {
8526 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8527 mode = IWL_POWER_AC;
8529 mode |= IWL_POWER_ENABLED;
8531 if (mode != priv->power_mode) {
8532 rc = iwl_send_power_mode(priv, IWL_POWER_LEVEL(mode));
8534 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8537 priv->power_mode = mode;
8543 mutex_unlock(&priv->mutex);
8547 #define MAX_WX_STRING 80
8549 /* Values are in microsecond */
8550 static const s32 timeout_duration[] = {
8557 static const s32 period_duration[] = {
8565 static ssize_t show_power_level(struct device *d,
8566 struct device_attribute *attr, char *buf)
8568 struct iwl_priv *priv = dev_get_drvdata(d);
8569 int level = IWL_POWER_LEVEL(priv->power_mode);
8572 p += sprintf(p, "%d ", level);
8574 case IWL_POWER_MODE_CAM:
8576 p += sprintf(p, "(AC)");
8578 case IWL_POWER_BATTERY:
8579 p += sprintf(p, "(BATTERY)");
8583 "(Timeout %dms, Period %dms)",
8584 timeout_duration[level - 1] / 1000,
8585 period_duration[level - 1] / 1000);
8588 if (!(priv->power_mode & IWL_POWER_ENABLED))
8589 p += sprintf(p, " OFF\n");
8591 p += sprintf(p, " \n");
8593 return (p - buf + 1);
8597 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8600 static ssize_t show_channels(struct device *d,
8601 struct device_attribute *attr, char *buf)
8603 struct iwl_priv *priv = dev_get_drvdata(d);
8605 struct ieee80211_channel *channels = NULL;
8606 const struct ieee80211_hw_mode *hw_mode = NULL;
8609 if (!iwl_is_ready(priv))
8612 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211G);
8614 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211B);
8616 channels = hw_mode->channels;
8617 count = hw_mode->num_channels;
8622 "Displaying %d channels in 2.4GHz band "
8623 "(802.11bg):\n", count);
8625 for (i = 0; i < count; i++)
8626 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8628 channels[i].power_level,
8630 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8631 " (IEEE 802.11h required)" : "",
8632 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8635 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8638 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8639 "active/passive" : "passive only");
8641 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211A);
8643 channels = hw_mode->channels;
8644 count = hw_mode->num_channels;
8650 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8651 "(802.11a):\n", count);
8653 for (i = 0; i < count; i++)
8654 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8656 channels[i].power_level,
8658 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8659 " (IEEE 802.11h required)" : "",
8660 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8663 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8666 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8667 "active/passive" : "passive only");
8672 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8674 static ssize_t show_statistics(struct device *d,
8675 struct device_attribute *attr, char *buf)
8677 struct iwl_priv *priv = dev_get_drvdata(d);
8678 u32 size = sizeof(struct iwl_notif_statistics);
8679 u32 len = 0, ofs = 0;
8680 u8 *data = (u8 *) & priv->statistics;
8683 if (!iwl_is_alive(priv))
8686 mutex_lock(&priv->mutex);
8687 rc = iwl_send_statistics_request(priv);
8688 mutex_unlock(&priv->mutex);
8692 "Error sending statistics request: 0x%08X\n", rc);
8696 while (size && (PAGE_SIZE - len)) {
8697 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8698 PAGE_SIZE - len, 1);
8700 if (PAGE_SIZE - len)
8704 size -= min(size, 16U);
8710 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8712 static ssize_t show_antenna(struct device *d,
8713 struct device_attribute *attr, char *buf)
8715 struct iwl_priv *priv = dev_get_drvdata(d);
8717 if (!iwl_is_alive(priv))
8720 return sprintf(buf, "%d\n", priv->antenna);
8723 static ssize_t store_antenna(struct device *d,
8724 struct device_attribute *attr,
8725 const char *buf, size_t count)
8728 struct iwl_priv *priv = dev_get_drvdata(d);
8733 if (sscanf(buf, "%1i", &ant) != 1) {
8734 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8738 if ((ant >= 0) && (ant <= 2)) {
8739 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
8740 priv->antenna = (enum iwl_antenna)ant;
8742 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8748 static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8750 static ssize_t show_status(struct device *d,
8751 struct device_attribute *attr, char *buf)
8753 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8754 if (!iwl_is_alive(priv))
8756 return sprintf(buf, "0x%08x\n", (int)priv->status);
8759 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8761 static ssize_t dump_error_log(struct device *d,
8762 struct device_attribute *attr,
8763 const char *buf, size_t count)
8765 char *p = (char *)buf;
8768 iwl_dump_nic_error_log((struct iwl_priv *)d->driver_data);
8770 return strnlen(buf, count);
8773 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8775 static ssize_t dump_event_log(struct device *d,
8776 struct device_attribute *attr,
8777 const char *buf, size_t count)
8779 char *p = (char *)buf;
8782 iwl_dump_nic_event_log((struct iwl_priv *)d->driver_data);
8784 return strnlen(buf, count);
8787 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8789 /*****************************************************************************
8791 * driver setup and teardown
8793 *****************************************************************************/
8795 static void iwl_setup_deferred_work(struct iwl_priv *priv)
8797 priv->workqueue = create_workqueue(DRV_NAME);
8799 init_waitqueue_head(&priv->wait_command_queue);
8801 INIT_WORK(&priv->up, iwl_bg_up);
8802 INIT_WORK(&priv->restart, iwl_bg_restart);
8803 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
8804 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
8805 INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
8806 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
8807 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
8808 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
8809 INIT_DELAYED_WORK(&priv->post_associate, iwl_bg_post_associate);
8810 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
8811 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
8812 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
8814 iwl_hw_setup_deferred_work(priv);
8816 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
8817 iwl_irq_tasklet, (unsigned long)priv);
8820 static void iwl_cancel_deferred_work(struct iwl_priv *priv)
8822 iwl_hw_cancel_deferred_work(priv);
8824 cancel_delayed_work(&priv->scan_check);
8825 cancel_delayed_work(&priv->alive_start);
8826 cancel_delayed_work(&priv->post_associate);
8827 cancel_work_sync(&priv->beacon_update);
8830 static struct attribute *iwl_sysfs_entries[] = {
8831 &dev_attr_antenna.attr,
8832 &dev_attr_channels.attr,
8833 &dev_attr_dump_errors.attr,
8834 &dev_attr_dump_events.attr,
8835 &dev_attr_flags.attr,
8836 &dev_attr_filter_flags.attr,
8837 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8838 &dev_attr_measurement.attr,
8840 &dev_attr_power_level.attr,
8841 &dev_attr_retry_rate.attr,
8842 &dev_attr_rf_kill.attr,
8843 &dev_attr_rs_window.attr,
8844 &dev_attr_statistics.attr,
8845 &dev_attr_status.attr,
8846 &dev_attr_temperature.attr,
8847 &dev_attr_tune.attr,
8848 &dev_attr_tx_power.attr,
8853 static struct attribute_group iwl_attribute_group = {
8854 .name = NULL, /* put in device directory */
8855 .attrs = iwl_sysfs_entries,
8858 static struct ieee80211_ops iwl_hw_ops = {
8860 .start = iwl_mac_start,
8861 .stop = iwl_mac_stop,
8862 .add_interface = iwl_mac_add_interface,
8863 .remove_interface = iwl_mac_remove_interface,
8864 .config = iwl_mac_config,
8865 .config_interface = iwl_mac_config_interface,
8866 .configure_filter = iwl_configure_filter,
8867 .set_key = iwl_mac_set_key,
8868 .get_stats = iwl_mac_get_stats,
8869 .get_tx_stats = iwl_mac_get_tx_stats,
8870 .conf_tx = iwl_mac_conf_tx,
8871 .get_tsf = iwl_mac_get_tsf,
8872 .reset_tsf = iwl_mac_reset_tsf,
8873 .beacon_update = iwl_mac_beacon_update,
8874 #ifdef CONFIG_IWLWIFI_HT
8875 .conf_ht = iwl_mac_conf_ht,
8876 .get_ht_capab = iwl_mac_get_ht_capab,
8877 #ifdef CONFIG_IWLWIFI_HT_AGG
8878 .ht_tx_agg_start = iwl_mac_ht_tx_agg_start,
8879 .ht_tx_agg_stop = iwl_mac_ht_tx_agg_stop,
8880 .ht_rx_agg_start = iwl_mac_ht_rx_agg_start,
8881 .ht_rx_agg_stop = iwl_mac_ht_rx_agg_stop,
8882 #endif /* CONFIG_IWLWIFI_HT_AGG */
8883 #endif /* CONFIG_IWLWIFI_HT */
8884 .hw_scan = iwl_mac_hw_scan
8887 static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8890 struct iwl_priv *priv;
8891 struct ieee80211_hw *hw;
8894 if (iwl_param_disable_hw_scan) {
8895 IWL_DEBUG_INFO("Disabling hw_scan\n");
8896 iwl_hw_ops.hw_scan = NULL;
8899 if ((iwl_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8900 (iwl_param_queues_num < IWL_MIN_NUM_QUEUES)) {
8901 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8902 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8907 /* mac80211 allocates memory for this device instance, including
8908 * space for this driver's private structure */
8909 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl_hw_ops);
8911 IWL_ERROR("Can not allocate network device\n");
8915 SET_IEEE80211_DEV(hw, &pdev->dev);
8917 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8921 priv->pci_dev = pdev;
8922 priv->antenna = (enum iwl_antenna)iwl_param_antenna;
8923 #ifdef CONFIG_IWLWIFI_DEBUG
8924 iwl_debug_level = iwl_param_debug;
8925 atomic_set(&priv->restrict_refcnt, 0);
8927 priv->retry_rate = 1;
8929 priv->ibss_beacon = NULL;
8931 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8932 * the range of signal quality values that we'll provide.
8933 * Negative values for level/noise indicate that we'll provide dBm.
8934 * For WE, at least, non-0 values here *enable* display of values
8935 * in app (iwconfig). */
8936 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8937 hw->max_noise = -20; /* noise level, negative indicates dBm */
8938 hw->max_signal = 100; /* link quality indication (%) */
8940 /* Tell mac80211 our Tx characteristics */
8941 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8944 #ifdef CONFIG_IWLWIFI_HT
8945 #ifdef CONFIG_IWLWIFI_HT_AGG
8947 #endif /* CONFIG_IWLWIFI_HT_AGG */
8948 #endif /* CONFIG_IWLWIFI_HT */
8950 spin_lock_init(&priv->lock);
8951 spin_lock_init(&priv->power_data.lock);
8952 spin_lock_init(&priv->sta_lock);
8953 spin_lock_init(&priv->hcmd_lock);
8954 spin_lock_init(&priv->lq_mngr.lock);
8956 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8957 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8959 INIT_LIST_HEAD(&priv->free_frames);
8961 mutex_init(&priv->mutex);
8962 if (pci_enable_device(pdev)) {
8964 goto out_ieee80211_free_hw;
8967 pci_set_master(pdev);
8969 iwl_clear_stations_table(priv);
8971 priv->data_retry_limit = -1;
8972 priv->ieee_channels = NULL;
8973 priv->ieee_rates = NULL;
8976 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8978 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8980 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
8981 goto out_pci_disable_device;
8984 pci_set_drvdata(pdev, priv);
8985 err = pci_request_regions(pdev, DRV_NAME);
8987 goto out_pci_disable_device;
8988 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8989 * PCI Tx retries from interfering with C3 CPU state */
8990 pci_write_config_byte(pdev, 0x41, 0x00);
8991 priv->hw_base = pci_iomap(pdev, 0, 0);
8992 if (!priv->hw_base) {
8994 goto out_pci_release_regions;
8997 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8998 (unsigned long long) pci_resource_len(pdev, 0));
8999 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
9001 /* Initialize module parameter values here */
9003 if (iwl_param_disable) {
9004 set_bit(STATUS_RF_KILL_SW, &priv->status);
9005 IWL_DEBUG_INFO("Radio disabled.\n");
9008 priv->iw_mode = IEEE80211_IF_TYPE_STA;
9011 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
9012 priv->is_ht_enabled = 1;
9013 priv->channel_width = IWL_CHANNEL_WIDTH_40MHZ;
9014 priv->valid_antenna = 0x7; /* assume all 3 connected */
9015 priv->ps_mode = IWL_MIMO_PS_NONE;
9016 priv->cck_power_index_compensation = iwl_read32(
9017 priv, CSR_HW_REV_WA_REG);
9019 iwl4965_set_rxon_chain(priv);
9021 printk(KERN_INFO DRV_NAME
9022 ": Detected Intel Wireless WiFi Link 4965AGN\n");
9024 /* Device-specific setup */
9025 if (iwl_hw_set_hw_setting(priv)) {
9026 IWL_ERROR("failed to set hw settings\n");
9027 mutex_unlock(&priv->mutex);
9031 #ifdef CONFIG_IWLWIFI_QOS
9032 if (iwl_param_qos_enable)
9033 priv->qos_data.qos_enable = 1;
9035 iwl_reset_qos(priv);
9037 priv->qos_data.qos_active = 0;
9038 priv->qos_data.qos_cap.val = 0;
9039 #endif /* CONFIG_IWLWIFI_QOS */
9041 iwl_set_rxon_channel(priv, MODE_IEEE80211G, 6);
9042 iwl_setup_deferred_work(priv);
9043 iwl_setup_rx_handlers(priv);
9045 priv->rates_mask = IWL_RATES_MASK;
9046 /* If power management is turned on, default to AC mode */
9047 priv->power_mode = IWL_POWER_AC;
9048 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
9050 pci_enable_msi(pdev);
9052 err = request_irq(pdev->irq, iwl_isr, IRQF_SHARED, DRV_NAME, priv);
9054 IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
9055 goto out_disable_msi;
9058 mutex_lock(&priv->mutex);
9060 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
9062 IWL_ERROR("failed to create sysfs device attributes\n");
9063 mutex_unlock(&priv->mutex);
9064 goto out_release_irq;
9067 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
9068 * ucode filename and max sizes are card-specific. */
9069 err = iwl_read_ucode(priv);
9071 IWL_ERROR("Could not read microcode: %d\n", err);
9072 mutex_unlock(&priv->mutex);
9076 mutex_unlock(&priv->mutex);
9078 IWL_DEBUG_INFO("Queing UP work.\n");
9080 queue_work(priv->workqueue, &priv->up);
9085 iwl_dealloc_ucode_pci(priv);
9087 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
9090 free_irq(pdev->irq, priv);
9093 pci_disable_msi(pdev);
9094 destroy_workqueue(priv->workqueue);
9095 priv->workqueue = NULL;
9096 iwl_unset_hw_setting(priv);
9099 pci_iounmap(pdev, priv->hw_base);
9100 out_pci_release_regions:
9101 pci_release_regions(pdev);
9102 out_pci_disable_device:
9103 pci_disable_device(pdev);
9104 pci_set_drvdata(pdev, NULL);
9105 out_ieee80211_free_hw:
9106 ieee80211_free_hw(priv->hw);
9111 static void iwl_pci_remove(struct pci_dev *pdev)
9113 struct iwl_priv *priv = pci_get_drvdata(pdev);
9114 struct list_head *p, *q;
9120 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
9122 mutex_lock(&priv->mutex);
9123 set_bit(STATUS_EXIT_PENDING, &priv->status);
9125 mutex_unlock(&priv->mutex);
9127 /* Free MAC hash list for ADHOC */
9128 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
9129 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
9131 kfree(list_entry(p, struct iwl_ibss_seq, list));
9135 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
9137 iwl_dealloc_ucode_pci(priv);
9140 iwl_rx_queue_free(priv, &priv->rxq);
9141 iwl_hw_txq_ctx_free(priv);
9143 iwl_unset_hw_setting(priv);
9144 iwl_clear_stations_table(priv);
9146 if (priv->mac80211_registered) {
9147 ieee80211_unregister_hw(priv->hw);
9148 iwl_rate_control_unregister(priv->hw);
9151 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
9152 * priv->workqueue... so we can't take down the workqueue
9154 destroy_workqueue(priv->workqueue);
9155 priv->workqueue = NULL;
9157 free_irq(pdev->irq, priv);
9158 pci_disable_msi(pdev);
9159 pci_iounmap(pdev, priv->hw_base);
9160 pci_release_regions(pdev);
9161 pci_disable_device(pdev);
9162 pci_set_drvdata(pdev, NULL);
9164 kfree(priv->channel_info);
9166 kfree(priv->ieee_channels);
9167 kfree(priv->ieee_rates);
9169 if (priv->ibss_beacon)
9170 dev_kfree_skb(priv->ibss_beacon);
9172 ieee80211_free_hw(priv->hw);
9177 static int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
9179 struct iwl_priv *priv = pci_get_drvdata(pdev);
9181 mutex_lock(&priv->mutex);
9183 set_bit(STATUS_IN_SUSPEND, &priv->status);
9185 /* Take down the device; powers it off, etc. */
9188 if (priv->mac80211_registered)
9189 ieee80211_stop_queues(priv->hw);
9191 pci_save_state(pdev);
9192 pci_disable_device(pdev);
9193 pci_set_power_state(pdev, PCI_D3hot);
9195 mutex_unlock(&priv->mutex);
9200 static void iwl_resume(struct iwl_priv *priv)
9202 unsigned long flags;
9204 /* The following it a temporary work around due to the
9205 * suspend / resume not fully initializing the NIC correctly.
9206 * Without all of the following, resume will not attempt to take
9207 * down the NIC (it shouldn't really need to) and will just try
9208 * and bring the NIC back up. However that fails during the
9209 * ucode verification process. This then causes iwl_down to be
9210 * called *after* iwl_hw_nic_init() has succeeded -- which
9211 * then lets the next init sequence succeed. So, we've
9212 * replicated all of that NIC init code here... */
9214 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
9216 iwl_hw_nic_init(priv);
9218 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9219 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
9220 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
9221 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
9222 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9223 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9225 /* tell the device to stop sending interrupts */
9226 iwl_disable_interrupts(priv);
9228 spin_lock_irqsave(&priv->lock, flags);
9229 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
9231 if (!iwl_grab_restricted_access(priv)) {
9232 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
9233 APMG_CLK_VAL_DMA_CLK_RQT);
9234 iwl_release_restricted_access(priv);
9236 spin_unlock_irqrestore(&priv->lock, flags);
9240 iwl_hw_nic_reset(priv);
9242 /* Bring the device back up */
9243 clear_bit(STATUS_IN_SUSPEND, &priv->status);
9244 queue_work(priv->workqueue, &priv->up);
9247 static int iwl_pci_resume(struct pci_dev *pdev)
9249 struct iwl_priv *priv = pci_get_drvdata(pdev);
9252 printk(KERN_INFO "Coming out of suspend...\n");
9254 mutex_lock(&priv->mutex);
9256 pci_set_power_state(pdev, PCI_D0);
9257 err = pci_enable_device(pdev);
9258 pci_restore_state(pdev);
9261 * Suspend/Resume resets the PCI configuration space, so we have to
9262 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
9263 * from interfering with C3 CPU state. pci_restore_state won't help
9264 * here since it only restores the first 64 bytes pci config header.
9266 pci_write_config_byte(pdev, 0x41, 0x00);
9269 mutex_unlock(&priv->mutex);
9274 #endif /* CONFIG_PM */
9276 /*****************************************************************************
9278 * driver and module entry point
9280 *****************************************************************************/
9282 static struct pci_driver iwl_driver = {
9284 .id_table = iwl_hw_card_ids,
9285 .probe = iwl_pci_probe,
9286 .remove = __devexit_p(iwl_pci_remove),
9288 .suspend = iwl_pci_suspend,
9289 .resume = iwl_pci_resume,
9293 static int __init iwl_init(void)
9297 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
9298 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
9299 ret = pci_register_driver(&iwl_driver);
9301 IWL_ERROR("Unable to initialize PCI module\n");
9304 #ifdef CONFIG_IWLWIFI_DEBUG
9305 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
9307 IWL_ERROR("Unable to create driver sysfs file\n");
9308 pci_unregister_driver(&iwl_driver);
9316 static void __exit iwl_exit(void)
9318 #ifdef CONFIG_IWLWIFI_DEBUG
9319 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
9321 pci_unregister_driver(&iwl_driver);
9324 module_param_named(antenna, iwl_param_antenna, int, 0444);
9325 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
9326 module_param_named(disable, iwl_param_disable, int, 0444);
9327 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
9328 module_param_named(hwcrypto, iwl_param_hwcrypto, int, 0444);
9329 MODULE_PARM_DESC(hwcrypto,
9330 "using hardware crypto engine (default 0 [software])\n");
9331 module_param_named(debug, iwl_param_debug, int, 0444);
9332 MODULE_PARM_DESC(debug, "debug output mask");
9333 module_param_named(disable_hw_scan, iwl_param_disable_hw_scan, int, 0444);
9334 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
9336 module_param_named(queues_num, iwl_param_queues_num, int, 0444);
9337 MODULE_PARM_DESC(queues_num, "number of hw queues.");
9340 module_param_named(qos_enable, iwl_param_qos_enable, int, 0444);
9341 MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
9343 module_exit(iwl_exit);
9344 module_init(iwl_init);