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/etherdevice.h>
52 #include <linux/if_arp.h>
54 #include <net/ieee80211_radiotap.h>
55 #include <net/mac80211.h>
57 #include <asm/div64.h>
63 #include "iwl-helpers.h"
65 #ifdef CONFIG_IWLWIFI_DEBUG
69 /******************************************************************************
73 ******************************************************************************/
75 /* module parameters */
76 int iwl_param_disable_hw_scan;
78 int iwl_param_disable; /* def: enable radio */
79 int iwl_param_antenna; /* def: 0 = both antennas (use diversity) */
80 int iwl_param_hwcrypto; /* def: using software encryption */
81 int iwl_param_qos_enable = 1;
82 int iwl_param_queues_num = IWL_MAX_NUM_QUEUES;
85 * module name, copyright, version, etc.
86 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
89 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
91 #ifdef CONFIG_IWLWIFI_DEBUG
97 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
103 #define IWLWIFI_VERSION "1.1.17k" VD VS
104 #define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
105 #define DRV_VERSION IWLWIFI_VERSION
107 /* Change firmware file name, using "-" and incrementing number,
108 * *only* when uCode interface or architecture changes so that it
109 * is not compatible with earlier drivers.
110 * This number will also appear in << 8 position of 1st dword of uCode file */
111 #define IWL4965_UCODE_API "-1"
113 MODULE_DESCRIPTION(DRV_DESCRIPTION);
114 MODULE_VERSION(DRV_VERSION);
115 MODULE_AUTHOR(DRV_COPYRIGHT);
116 MODULE_LICENSE("GPL");
118 __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
120 u16 fc = le16_to_cpu(hdr->frame_control);
121 int hdr_len = ieee80211_get_hdrlen(fc);
123 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
124 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
128 static const struct ieee80211_hw_mode *iwl_get_hw_mode(
129 struct iwl_priv *priv, int mode)
133 for (i = 0; i < 3; i++)
134 if (priv->modes[i].mode == mode)
135 return &priv->modes[i];
140 static int iwl_is_empty_essid(const char *essid, int essid_len)
142 /* Single white space is for Linksys APs */
143 if (essid_len == 1 && essid[0] == ' ')
146 /* Otherwise, if the entire essid is 0, we assume it is hidden */
149 if (essid[essid_len] != '\0')
156 static const char *iwl_escape_essid(const char *essid, u8 essid_len)
158 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
159 const char *s = essid;
162 if (iwl_is_empty_essid(essid, essid_len)) {
163 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
167 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
168 while (essid_len--) {
180 static void iwl_print_hex_dump(int level, void *p, u32 len)
182 #ifdef CONFIG_IWLWIFI_DEBUG
183 if (!(iwl_debug_level & level))
186 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
191 /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
194 * Theory of operation
196 * A queue is a circular buffers with 'Read' and 'Write' pointers.
197 * 2 empty entries always kept in the buffer to protect from overflow.
199 * For Tx queue, there are low mark and high mark limits. If, after queuing
200 * the packet for Tx, free space become < low mark, Tx queue stopped. When
201 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
204 * The IWL operates with six queues, one receive queue in the device's
205 * sram, one transmit queue for sending commands to the device firmware,
206 * and four transmit queues for data.
207 ***************************************************/
209 static int iwl_queue_space(const struct iwl_queue *q)
211 int s = q->last_used - q->first_empty;
213 if (q->last_used > q->first_empty)
218 /* keep some reserve to not confuse empty and full situations */
225 /* XXX: n_bd must be power-of-two size */
226 static inline int iwl_queue_inc_wrap(int index, int n_bd)
228 return ++index & (n_bd - 1);
231 /* XXX: n_bd must be power-of-two size */
232 static inline int iwl_queue_dec_wrap(int index, int n_bd)
234 return --index & (n_bd - 1);
237 static inline int x2_queue_used(const struct iwl_queue *q, int i)
239 return q->first_empty > q->last_used ?
240 (i >= q->last_used && i < q->first_empty) :
241 !(i < q->last_used && i >= q->first_empty);
244 static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
249 return index & (q->n_window - 1);
252 static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
253 int count, int slots_num, u32 id)
256 q->n_window = slots_num;
259 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
260 * and iwl_queue_dec_wrap are broken. */
261 BUG_ON(!is_power_of_2(count));
263 /* slots_num must be power-of-two size, otherwise
264 * get_cmd_index is broken. */
265 BUG_ON(!is_power_of_2(slots_num));
267 q->low_mark = q->n_window / 4;
271 q->high_mark = q->n_window / 8;
272 if (q->high_mark < 2)
275 q->first_empty = q->last_used = 0;
280 static int iwl_tx_queue_alloc(struct iwl_priv *priv,
281 struct iwl_tx_queue *txq, u32 id)
283 struct pci_dev *dev = priv->pci_dev;
285 if (id != IWL_CMD_QUEUE_NUM) {
286 txq->txb = kmalloc(sizeof(txq->txb[0]) *
287 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
289 IWL_ERROR("kmalloc for auxilary BD "
290 "structures failed\n");
296 txq->bd = pci_alloc_consistent(dev,
297 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
301 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
302 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
318 int iwl_tx_queue_init(struct iwl_priv *priv,
319 struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
321 struct pci_dev *dev = priv->pci_dev;
325 /* alocate command space + one big command for scan since scan
326 * command is very huge the system will not have two scan at the
328 len = sizeof(struct iwl_cmd) * slots_num;
329 if (txq_id == IWL_CMD_QUEUE_NUM)
330 len += IWL_MAX_SCAN_SIZE;
331 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
335 rc = iwl_tx_queue_alloc(priv, txq, txq_id);
337 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
341 txq->need_update = 0;
343 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
344 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
345 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
346 iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
348 iwl_hw_tx_queue_init(priv, txq);
354 * iwl_tx_queue_free - Deallocate DMA queue.
355 * @txq: Transmit queue to deallocate.
357 * Empty queue by removing and destroying all BD's.
358 * Free all buffers. txq itself is not freed.
361 void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
363 struct iwl_queue *q = &txq->q;
364 struct pci_dev *dev = priv->pci_dev;
370 /* first, empty all BD's */
371 for (; q->first_empty != q->last_used;
372 q->last_used = iwl_queue_inc_wrap(q->last_used, q->n_bd))
373 iwl_hw_txq_free_tfd(priv, txq);
375 len = sizeof(struct iwl_cmd) * q->n_window;
376 if (q->id == IWL_CMD_QUEUE_NUM)
377 len += IWL_MAX_SCAN_SIZE;
379 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
381 /* free buffers belonging to queue itself */
383 pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
384 txq->q.n_bd, txq->bd, txq->q.dma_addr);
391 /* 0 fill whole structure */
392 memset(txq, 0, sizeof(*txq));
395 const u8 BROADCAST_ADDR[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
397 /*************** STATION TABLE MANAGEMENT ****
399 * NOTE: This needs to be overhauled to better synchronize between
400 * how the iwl-4965.c is using iwl_hw_find_station vs. iwl-3945.c
402 * mac80211 should also be examined to determine if sta_info is duplicating
403 * the functionality provided here
406 /**************************************************************/
408 #if 0 /* temparary disable till we add real remove station */
409 static u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
411 int index = IWL_INVALID_STATION;
415 spin_lock_irqsave(&priv->sta_lock, flags);
419 else if (is_broadcast_ether_addr(addr))
420 index = priv->hw_setting.bcast_sta_id;
422 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
423 if (priv->stations[i].used &&
424 !compare_ether_addr(priv->stations[i].sta.sta.addr,
430 if (unlikely(index == IWL_INVALID_STATION))
433 if (priv->stations[index].used) {
434 priv->stations[index].used = 0;
435 priv->num_stations--;
438 BUG_ON(priv->num_stations < 0);
441 spin_unlock_irqrestore(&priv->sta_lock, flags);
446 static void iwl_clear_stations_table(struct iwl_priv *priv)
450 spin_lock_irqsave(&priv->sta_lock, flags);
452 priv->num_stations = 0;
453 memset(priv->stations, 0, sizeof(priv->stations));
455 spin_unlock_irqrestore(&priv->sta_lock, flags);
458 u8 iwl_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
461 int index = IWL_INVALID_STATION;
462 struct iwl_station_entry *station;
463 unsigned long flags_spin;
464 DECLARE_MAC_BUF(mac);
466 spin_lock_irqsave(&priv->sta_lock, flags_spin);
469 else if (is_broadcast_ether_addr(addr))
470 index = priv->hw_setting.bcast_sta_id;
472 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
473 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
479 if (!priv->stations[i].used &&
480 index == IWL_INVALID_STATION)
485 /* These twh conditions has the same outcome but keep them separate
486 since they have different meaning */
487 if (unlikely(index == IWL_INVALID_STATION)) {
488 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
492 if (priv->stations[index].used &&
493 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
494 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
499 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
500 station = &priv->stations[index];
502 priv->num_stations++;
504 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
505 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
506 station->sta.mode = 0;
507 station->sta.sta.sta_id = index;
508 station->sta.station_flags = 0;
510 #ifdef CONFIG_IWLWIFI_HT
511 /* BCAST station and IBSS stations do not work in HT mode */
512 if (index != priv->hw_setting.bcast_sta_id &&
513 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
514 iwl4965_set_ht_add_station(priv, index);
515 #endif /*CONFIG_IWLWIFI_HT*/
517 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
518 iwl_send_add_station(priv, &station->sta, flags);
523 /*************** DRIVER STATUS FUNCTIONS *****/
525 static inline int iwl_is_ready(struct iwl_priv *priv)
527 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
528 * set but EXIT_PENDING is not */
529 return test_bit(STATUS_READY, &priv->status) &&
530 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
531 !test_bit(STATUS_EXIT_PENDING, &priv->status);
534 static inline int iwl_is_alive(struct iwl_priv *priv)
536 return test_bit(STATUS_ALIVE, &priv->status);
539 static inline int iwl_is_init(struct iwl_priv *priv)
541 return test_bit(STATUS_INIT, &priv->status);
544 static inline int iwl_is_rfkill(struct iwl_priv *priv)
546 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
547 test_bit(STATUS_RF_KILL_SW, &priv->status);
550 static inline int iwl_is_ready_rf(struct iwl_priv *priv)
553 if (iwl_is_rfkill(priv))
556 return iwl_is_ready(priv);
559 /*************** HOST COMMAND QUEUE FUNCTIONS *****/
561 #define IWL_CMD(x) case x : return #x
563 static const char *get_cmd_string(u8 cmd)
566 IWL_CMD(REPLY_ALIVE);
567 IWL_CMD(REPLY_ERROR);
569 IWL_CMD(REPLY_RXON_ASSOC);
570 IWL_CMD(REPLY_QOS_PARAM);
571 IWL_CMD(REPLY_RXON_TIMING);
572 IWL_CMD(REPLY_ADD_STA);
573 IWL_CMD(REPLY_REMOVE_STA);
574 IWL_CMD(REPLY_REMOVE_ALL_STA);
576 IWL_CMD(REPLY_RATE_SCALE);
577 IWL_CMD(REPLY_LEDS_CMD);
578 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
579 IWL_CMD(RADAR_NOTIFICATION);
580 IWL_CMD(REPLY_QUIET_CMD);
581 IWL_CMD(REPLY_CHANNEL_SWITCH);
582 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
583 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
584 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
585 IWL_CMD(POWER_TABLE_CMD);
586 IWL_CMD(PM_SLEEP_NOTIFICATION);
587 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
588 IWL_CMD(REPLY_SCAN_CMD);
589 IWL_CMD(REPLY_SCAN_ABORT_CMD);
590 IWL_CMD(SCAN_START_NOTIFICATION);
591 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
592 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
593 IWL_CMD(BEACON_NOTIFICATION);
594 IWL_CMD(REPLY_TX_BEACON);
595 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
596 IWL_CMD(QUIET_NOTIFICATION);
597 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
598 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
599 IWL_CMD(REPLY_BT_CONFIG);
600 IWL_CMD(REPLY_STATISTICS_CMD);
601 IWL_CMD(STATISTICS_NOTIFICATION);
602 IWL_CMD(REPLY_CARD_STATE_CMD);
603 IWL_CMD(CARD_STATE_NOTIFICATION);
604 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
605 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
606 IWL_CMD(SENSITIVITY_CMD);
607 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
608 IWL_CMD(REPLY_RX_PHY_CMD);
609 IWL_CMD(REPLY_RX_MPDU_CMD);
610 IWL_CMD(REPLY_4965_RX);
611 IWL_CMD(REPLY_COMPRESSED_BA);
618 #define HOST_COMPLETE_TIMEOUT (HZ / 2)
621 * iwl_enqueue_hcmd - enqueue a uCode command
622 * @priv: device private data point
623 * @cmd: a point to the ucode command structure
625 * The function returns < 0 values to indicate the operation is
626 * failed. On success, it turns the index (> 0) of command in the
629 static int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
631 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
632 struct iwl_queue *q = &txq->q;
633 struct iwl_tfd_frame *tfd;
635 struct iwl_cmd *out_cmd;
637 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
638 dma_addr_t phys_addr;
642 /* If any of the command structures end up being larger than
643 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
644 * we will need to increase the size of the TFD entries */
645 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
646 !(cmd->meta.flags & CMD_SIZE_HUGE));
648 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
649 IWL_ERROR("No space for Tx\n");
653 spin_lock_irqsave(&priv->hcmd_lock, flags);
655 tfd = &txq->bd[q->first_empty];
656 memset(tfd, 0, sizeof(*tfd));
658 control_flags = (u32 *) tfd;
660 idx = get_cmd_index(q, q->first_empty, cmd->meta.flags & CMD_SIZE_HUGE);
661 out_cmd = &txq->cmd[idx];
663 out_cmd->hdr.cmd = cmd->id;
664 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
665 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
667 /* At this point, the out_cmd now has all of the incoming cmd
670 out_cmd->hdr.flags = 0;
671 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
672 INDEX_TO_SEQ(q->first_empty));
673 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
674 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
676 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
677 offsetof(struct iwl_cmd, hdr);
678 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
680 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
681 "%d bytes at %d[%d]:%d\n",
682 get_cmd_string(out_cmd->hdr.cmd),
683 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
684 fix_size, q->first_empty, idx, IWL_CMD_QUEUE_NUM);
686 txq->need_update = 1;
687 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
688 q->first_empty = iwl_queue_inc_wrap(q->first_empty, q->n_bd);
689 iwl_tx_queue_update_write_ptr(priv, txq);
691 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
692 return ret ? ret : idx;
695 int iwl_send_cmd_async(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
699 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
701 /* An asynchronous command can not expect an SKB to be set. */
702 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
704 /* An asynchronous command MUST have a callback. */
705 BUG_ON(!cmd->meta.u.callback);
707 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
710 ret = iwl_enqueue_hcmd(priv, cmd);
712 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
713 get_cmd_string(cmd->id), ret);
719 int iwl_send_cmd_sync(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
723 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
725 BUG_ON(cmd->meta.flags & CMD_ASYNC);
727 /* A synchronous command can not have a callback set. */
728 BUG_ON(cmd->meta.u.callback != NULL);
730 if (atomic_xchg(&entry, 1)) {
731 IWL_ERROR("Error sending %s: Already sending a host command\n",
732 get_cmd_string(cmd->id));
736 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
738 if (cmd->meta.flags & CMD_WANT_SKB)
739 cmd->meta.source = &cmd->meta;
741 cmd_idx = iwl_enqueue_hcmd(priv, cmd);
744 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
745 get_cmd_string(cmd->id), ret);
749 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
750 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
751 HOST_COMPLETE_TIMEOUT);
753 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
754 IWL_ERROR("Error sending %s: time out after %dms.\n",
755 get_cmd_string(cmd->id),
756 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
758 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
764 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
765 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
766 get_cmd_string(cmd->id));
770 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
771 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
772 get_cmd_string(cmd->id));
776 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
777 IWL_ERROR("Error: Response NULL in '%s'\n",
778 get_cmd_string(cmd->id));
787 if (cmd->meta.flags & CMD_WANT_SKB) {
788 struct iwl_cmd *qcmd;
790 /* Cancel the CMD_WANT_SKB flag for the cmd in the
791 * TX cmd queue. Otherwise in case the cmd comes
792 * in later, it will possibly set an invalid
793 * address (cmd->meta.source). */
794 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
795 qcmd->meta.flags &= ~CMD_WANT_SKB;
798 if (cmd->meta.u.skb) {
799 dev_kfree_skb_any(cmd->meta.u.skb);
800 cmd->meta.u.skb = NULL;
803 atomic_set(&entry, 0);
807 int iwl_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
809 /* A command can not be asynchronous AND expect an SKB to be set. */
810 BUG_ON((cmd->meta.flags & CMD_ASYNC) &&
811 (cmd->meta.flags & CMD_WANT_SKB));
813 if (cmd->meta.flags & CMD_ASYNC)
814 return iwl_send_cmd_async(priv, cmd);
816 return iwl_send_cmd_sync(priv, cmd);
819 int iwl_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
821 struct iwl_host_cmd cmd = {
827 return iwl_send_cmd_sync(priv, &cmd);
830 static int __must_check iwl_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
832 struct iwl_host_cmd cmd = {
838 return iwl_send_cmd_sync(priv, &cmd);
841 int iwl_send_statistics_request(struct iwl_priv *priv)
843 return iwl_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
847 * iwl_rxon_add_station - add station into station table.
849 * there is only one AP station with id= IWL_AP_ID
850 * NOTE: mutex must be held before calling the this fnction
852 static int iwl_rxon_add_station(struct iwl_priv *priv,
853 const u8 *addr, int is_ap)
857 sta_id = iwl_add_station(priv, addr, is_ap, 0);
858 iwl4965_add_station(priv, addr, is_ap);
864 * iwl_set_rxon_channel - Set the phymode and channel values in staging RXON
865 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
866 * @channel: Any channel valid for the requested phymode
868 * In addition to setting the staging RXON, priv->phymode is also set.
870 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
871 * in the staging RXON flag structure based on the phymode
873 static int iwl_set_rxon_channel(struct iwl_priv *priv, u8 phymode, u16 channel)
875 if (!iwl_get_channel_info(priv, phymode, channel)) {
876 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
881 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
882 (priv->phymode == phymode))
885 priv->staging_rxon.channel = cpu_to_le16(channel);
886 if (phymode == MODE_IEEE80211A)
887 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
889 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
891 priv->phymode = phymode;
893 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
899 * iwl_check_rxon_cmd - validate RXON structure is valid
901 * NOTE: This is really only useful during development and can eventually
902 * be #ifdef'd out once the driver is stable and folks aren't actively
905 static int iwl_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
910 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
911 error |= le32_to_cpu(rxon->flags &
912 (RXON_FLG_TGJ_NARROW_BAND_MSK |
913 RXON_FLG_RADAR_DETECT_MSK));
915 IWL_WARNING("check 24G fields %d | %d\n",
918 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
919 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
921 IWL_WARNING("check 52 fields %d | %d\n",
923 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
925 IWL_WARNING("check 52 CCK %d | %d\n",
928 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
930 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
932 /* make sure basic rates 6Mbps and 1Mbps are supported */
933 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
934 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
936 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
938 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
940 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
942 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
943 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
945 IWL_WARNING("check CCK and short slot %d | %d\n",
948 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
949 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
951 IWL_WARNING("check CCK & auto detect %d | %d\n",
954 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
955 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
957 IWL_WARNING("check TGG and auto detect %d | %d\n",
961 IWL_WARNING("Tuning to channel %d\n",
962 le16_to_cpu(rxon->channel));
965 IWL_ERROR("Not a valid iwl_rxon_assoc_cmd field values\n");
972 * iwl_full_rxon_required - determine if RXON_ASSOC can be used in RXON commit
973 * @priv: staging_rxon is comapred to active_rxon
975 * If the RXON structure is changing sufficient to require a new
976 * tune or to clear and reset the RXON_FILTER_ASSOC_MSK then return 1
977 * to indicate a new tune is required.
979 static int iwl_full_rxon_required(struct iwl_priv *priv)
982 /* These items are only settable from the full RXON command */
983 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
984 compare_ether_addr(priv->staging_rxon.bssid_addr,
985 priv->active_rxon.bssid_addr) ||
986 compare_ether_addr(priv->staging_rxon.node_addr,
987 priv->active_rxon.node_addr) ||
988 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
989 priv->active_rxon.wlap_bssid_addr) ||
990 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
991 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
992 (priv->staging_rxon.air_propagation !=
993 priv->active_rxon.air_propagation) ||
994 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
995 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
996 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
997 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
998 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
999 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1002 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1003 * be updated with the RXON_ASSOC command -- however only some
1004 * flag transitions are allowed using RXON_ASSOC */
1006 /* Check if we are not switching bands */
1007 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1008 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1011 /* Check if we are switching association toggle */
1012 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1013 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1019 static int iwl_send_rxon_assoc(struct iwl_priv *priv)
1022 struct iwl_rx_packet *res = NULL;
1023 struct iwl_rxon_assoc_cmd rxon_assoc;
1024 struct iwl_host_cmd cmd = {
1025 .id = REPLY_RXON_ASSOC,
1026 .len = sizeof(rxon_assoc),
1027 .meta.flags = CMD_WANT_SKB,
1028 .data = &rxon_assoc,
1030 const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
1031 const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
1033 if ((rxon1->flags == rxon2->flags) &&
1034 (rxon1->filter_flags == rxon2->filter_flags) &&
1035 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1036 (rxon1->ofdm_ht_single_stream_basic_rates ==
1037 rxon2->ofdm_ht_single_stream_basic_rates) &&
1038 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1039 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1040 (rxon1->rx_chain == rxon2->rx_chain) &&
1041 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1042 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1046 rxon_assoc.flags = priv->staging_rxon.flags;
1047 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1048 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1049 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1050 rxon_assoc.reserved = 0;
1051 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1052 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1053 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1054 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1055 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1057 rc = iwl_send_cmd_sync(priv, &cmd);
1061 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1062 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1063 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1067 priv->alloc_rxb_skb--;
1068 dev_kfree_skb_any(cmd.meta.u.skb);
1074 * iwl_commit_rxon - commit staging_rxon to hardware
1076 * The RXON command in staging_rxon is commited to the hardware and
1077 * the active_rxon structure is updated with the new data. This
1078 * function correctly transitions out of the RXON_ASSOC_MSK state if
1079 * a HW tune is required based on the RXON structure changes.
1081 static int iwl_commit_rxon(struct iwl_priv *priv)
1083 /* cast away the const for active_rxon in this function */
1084 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
1085 DECLARE_MAC_BUF(mac);
1088 if (!iwl_is_alive(priv))
1091 /* always get timestamp with Rx frame */
1092 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1094 rc = iwl_check_rxon_cmd(&priv->staging_rxon);
1096 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1100 /* If we don't need to send a full RXON, we can use
1101 * iwl_rxon_assoc_cmd which is used to reconfigure filter
1102 * and other flags for the current radio configuration. */
1103 if (!iwl_full_rxon_required(priv)) {
1104 rc = iwl_send_rxon_assoc(priv);
1106 IWL_ERROR("Error setting RXON_ASSOC "
1107 "configuration (%d).\n", rc);
1111 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1116 /* station table will be cleared */
1117 priv->assoc_station_added = 0;
1119 #ifdef CONFIG_IWLWIFI_SENSITIVITY
1120 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1121 if (!priv->error_recovering)
1122 priv->start_calib = 0;
1124 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
1125 #endif /* CONFIG_IWLWIFI_SENSITIVITY */
1127 /* If we are currently associated and the new config requires
1128 * an RXON_ASSOC and the new config wants the associated mask enabled,
1129 * we must clear the associated from the active configuration
1130 * before we apply the new config */
1131 if (iwl_is_associated(priv) &&
1132 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1133 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1134 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1136 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1137 sizeof(struct iwl_rxon_cmd),
1138 &priv->active_rxon);
1140 /* If the mask clearing failed then we set
1141 * active_rxon back to what it was previously */
1143 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1144 IWL_ERROR("Error clearing ASSOC_MSK on current "
1145 "configuration (%d).\n", rc);
1150 IWL_DEBUG_INFO("Sending RXON\n"
1151 "* with%s RXON_FILTER_ASSOC_MSK\n"
1154 ((priv->staging_rxon.filter_flags &
1155 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1156 le16_to_cpu(priv->staging_rxon.channel),
1157 print_mac(mac, priv->staging_rxon.bssid_addr));
1159 /* Apply the new configuration */
1160 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1161 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
1163 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1167 iwl_clear_stations_table(priv);
1169 #ifdef CONFIG_IWLWIFI_SENSITIVITY
1170 if (!priv->error_recovering)
1171 priv->start_calib = 0;
1173 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1174 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
1175 #endif /* CONFIG_IWLWIFI_SENSITIVITY */
1177 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1179 /* If we issue a new RXON command which required a tune then we must
1180 * send a new TXPOWER command or we won't be able to Tx any frames */
1181 rc = iwl_hw_reg_send_txpower(priv);
1183 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1187 /* Add the broadcast address so we can send broadcast frames */
1188 if (iwl_rxon_add_station(priv, BROADCAST_ADDR, 0) ==
1189 IWL_INVALID_STATION) {
1190 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1194 /* If we have set the ASSOC_MSK and we are in BSS mode then
1195 * add the IWL_AP_ID to the station rate table */
1196 if (iwl_is_associated(priv) &&
1197 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
1198 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
1199 == IWL_INVALID_STATION) {
1200 IWL_ERROR("Error adding AP address for transmit.\n");
1203 priv->assoc_station_added = 1;
1209 static int iwl_send_bt_config(struct iwl_priv *priv)
1211 struct iwl_bt_cmd bt_cmd = {
1219 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1220 sizeof(struct iwl_bt_cmd), &bt_cmd);
1223 static int iwl_send_scan_abort(struct iwl_priv *priv)
1226 struct iwl_rx_packet *res;
1227 struct iwl_host_cmd cmd = {
1228 .id = REPLY_SCAN_ABORT_CMD,
1229 .meta.flags = CMD_WANT_SKB,
1232 /* If there isn't a scan actively going on in the hardware
1233 * then we are in between scan bands and not actually
1234 * actively scanning, so don't send the abort command */
1235 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1236 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1240 rc = iwl_send_cmd_sync(priv, &cmd);
1242 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1246 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1247 if (res->u.status != CAN_ABORT_STATUS) {
1248 /* The scan abort will return 1 for success or
1249 * 2 for "failure". A failure condition can be
1250 * due to simply not being in an active scan which
1251 * can occur if we send the scan abort before we
1252 * the microcode has notified us that a scan is
1254 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1255 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1256 clear_bit(STATUS_SCAN_HW, &priv->status);
1259 dev_kfree_skb_any(cmd.meta.u.skb);
1264 static int iwl_card_state_sync_callback(struct iwl_priv *priv,
1265 struct iwl_cmd *cmd,
1266 struct sk_buff *skb)
1274 * Use: Sets the internal card state to enable, disable, or halt
1276 * When in the 'enable' state the card operates as normal.
1277 * When in the 'disable' state, the card enters into a low power mode.
1278 * When in the 'halt' state, the card is shut down and must be fully
1279 * restarted to come back on.
1281 static int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
1283 struct iwl_host_cmd cmd = {
1284 .id = REPLY_CARD_STATE_CMD,
1287 .meta.flags = meta_flag,
1290 if (meta_flag & CMD_ASYNC)
1291 cmd.meta.u.callback = iwl_card_state_sync_callback;
1293 return iwl_send_cmd(priv, &cmd);
1296 static int iwl_add_sta_sync_callback(struct iwl_priv *priv,
1297 struct iwl_cmd *cmd, struct sk_buff *skb)
1299 struct iwl_rx_packet *res = NULL;
1302 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1306 res = (struct iwl_rx_packet *)skb->data;
1307 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1308 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1313 switch (res->u.add_sta.status) {
1314 case ADD_STA_SUCCESS_MSK:
1320 /* We didn't cache the SKB; let the caller free it */
1324 int iwl_send_add_station(struct iwl_priv *priv,
1325 struct iwl_addsta_cmd *sta, u8 flags)
1327 struct iwl_rx_packet *res = NULL;
1329 struct iwl_host_cmd cmd = {
1330 .id = REPLY_ADD_STA,
1331 .len = sizeof(struct iwl_addsta_cmd),
1332 .meta.flags = flags,
1336 if (flags & CMD_ASYNC)
1337 cmd.meta.u.callback = iwl_add_sta_sync_callback;
1339 cmd.meta.flags |= CMD_WANT_SKB;
1341 rc = iwl_send_cmd(priv, &cmd);
1343 if (rc || (flags & CMD_ASYNC))
1346 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1347 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1348 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1354 switch (res->u.add_sta.status) {
1355 case ADD_STA_SUCCESS_MSK:
1356 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1360 IWL_WARNING("REPLY_ADD_STA failed\n");
1365 priv->alloc_rxb_skb--;
1366 dev_kfree_skb_any(cmd.meta.u.skb);
1371 static int iwl_update_sta_key_info(struct iwl_priv *priv,
1372 struct ieee80211_key_conf *keyconf,
1375 unsigned long flags;
1376 __le16 key_flags = 0;
1378 switch (keyconf->alg) {
1380 key_flags |= STA_KEY_FLG_CCMP;
1381 key_flags |= cpu_to_le16(
1382 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1383 key_flags &= ~STA_KEY_FLG_INVALID;
1391 spin_lock_irqsave(&priv->sta_lock, flags);
1392 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1393 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1394 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1397 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1399 priv->stations[sta_id].sta.key.key_flags = key_flags;
1400 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1401 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1403 spin_unlock_irqrestore(&priv->sta_lock, flags);
1405 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
1406 iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1410 static int iwl_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
1412 unsigned long flags;
1414 spin_lock_irqsave(&priv->sta_lock, flags);
1415 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
1416 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl_keyinfo));
1417 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1418 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1419 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1420 spin_unlock_irqrestore(&priv->sta_lock, flags);
1422 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
1423 iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1427 static void iwl_clear_free_frames(struct iwl_priv *priv)
1429 struct list_head *element;
1431 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1432 priv->frames_count);
1434 while (!list_empty(&priv->free_frames)) {
1435 element = priv->free_frames.next;
1437 kfree(list_entry(element, struct iwl_frame, list));
1438 priv->frames_count--;
1441 if (priv->frames_count) {
1442 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1443 priv->frames_count);
1444 priv->frames_count = 0;
1448 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
1450 struct iwl_frame *frame;
1451 struct list_head *element;
1452 if (list_empty(&priv->free_frames)) {
1453 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1455 IWL_ERROR("Could not allocate frame!\n");
1459 priv->frames_count++;
1463 element = priv->free_frames.next;
1465 return list_entry(element, struct iwl_frame, list);
1468 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
1470 memset(frame, 0, sizeof(*frame));
1471 list_add(&frame->list, &priv->free_frames);
1474 unsigned int iwl_fill_beacon_frame(struct iwl_priv *priv,
1475 struct ieee80211_hdr *hdr,
1476 const u8 *dest, int left)
1479 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
1480 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1481 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1484 if (priv->ibss_beacon->len > left)
1487 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1489 return priv->ibss_beacon->len;
1492 int iwl_rate_index_from_plcp(int plcp)
1496 if (plcp & RATE_MCS_HT_MSK) {
1499 if (i >= IWL_RATE_MIMO_6M_PLCP)
1500 i = i - IWL_RATE_MIMO_6M_PLCP;
1502 i += IWL_FIRST_OFDM_RATE;
1503 /* skip 9M not supported in ht*/
1504 if (i >= IWL_RATE_9M_INDEX)
1506 if ((i >= IWL_FIRST_OFDM_RATE) &&
1507 (i <= IWL_LAST_OFDM_RATE))
1510 for (i = 0; i < ARRAY_SIZE(iwl_rates); i++)
1511 if (iwl_rates[i].plcp == (plcp &0xFF))
1517 static u8 iwl_rate_get_lowest_plcp(int rate_mask)
1521 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
1522 i = iwl_rates[i].next_ieee) {
1523 if (rate_mask & (1 << i))
1524 return iwl_rates[i].plcp;
1527 return IWL_RATE_INVALID;
1530 static int iwl_send_beacon_cmd(struct iwl_priv *priv)
1532 struct iwl_frame *frame;
1533 unsigned int frame_size;
1537 frame = iwl_get_free_frame(priv);
1540 IWL_ERROR("Could not obtain free frame buffer for beacon "
1545 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
1546 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic &
1548 if (rate == IWL_INVALID_RATE)
1549 rate = IWL_RATE_6M_PLCP;
1551 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
1552 if (rate == IWL_INVALID_RATE)
1553 rate = IWL_RATE_1M_PLCP;
1556 frame_size = iwl_hw_get_beacon_cmd(priv, frame, rate);
1558 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
1561 iwl_free_frame(priv, frame);
1566 /******************************************************************************
1568 * EEPROM related functions
1570 ******************************************************************************/
1572 static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
1574 memcpy(mac, priv->eeprom.mac_address, 6);
1578 * iwl_eeprom_init - read EEPROM contents
1580 * Load the EEPROM from adapter into priv->eeprom
1582 * NOTE: This routine uses the non-debug IO access functions.
1584 int iwl_eeprom_init(struct iwl_priv *priv)
1586 u16 *e = (u16 *)&priv->eeprom;
1587 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
1589 int sz = sizeof(priv->eeprom);
1594 /* The EEPROM structure has several padding buffers within it
1595 * and when adding new EEPROM maps is subject to programmer errors
1596 * which may be very difficult to identify without explicitly
1597 * checking the resulting size of the eeprom map. */
1598 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1600 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1601 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1605 rc = iwl_eeprom_aqcuire_semaphore(priv);
1607 IWL_ERROR("Failed to aqcuire EEPROM semaphore.\n");
1611 /* eeprom is an array of 16bit values */
1612 for (addr = 0; addr < sz; addr += sizeof(u16)) {
1613 _iwl_write32(priv, CSR_EEPROM_REG, addr << 1);
1614 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1616 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1617 i += IWL_EEPROM_ACCESS_DELAY) {
1618 r = _iwl_read_restricted(priv, CSR_EEPROM_REG);
1619 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1621 udelay(IWL_EEPROM_ACCESS_DELAY);
1624 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1625 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1629 e[addr / 2] = le16_to_cpu(r >> 16);
1634 iwl_eeprom_release_semaphore(priv);
1638 /******************************************************************************
1640 * Misc. internal state and helper functions
1642 ******************************************************************************/
1643 #ifdef CONFIG_IWLWIFI_DEBUG
1646 * iwl_report_frame - dump frame to syslog during debug sessions
1648 * hack this function to show different aspects of received frames,
1649 * including selective frame dumps.
1650 * group100 parameter selects whether to show 1 out of 100 good frames.
1652 * TODO: ieee80211_hdr stuff is common to 3945 and 4965, so frame type
1653 * info output is okay, but some of this stuff (e.g. iwl_rx_frame_stats)
1654 * is 3945-specific and gives bad output for 4965. Need to split the
1655 * functionality, keep common stuff here.
1657 void iwl_report_frame(struct iwl_priv *priv,
1658 struct iwl_rx_packet *pkt,
1659 struct ieee80211_hdr *header, int group100)
1662 u32 print_summary = 0;
1663 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1680 struct iwl_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1681 struct iwl_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1682 struct iwl_rx_frame_end *rx_end = IWL_RX_END(pkt);
1683 u8 *data = IWL_RX_DATA(pkt);
1686 fc = le16_to_cpu(header->frame_control);
1687 seq_ctl = le16_to_cpu(header->seq_ctrl);
1690 channel = le16_to_cpu(rx_hdr->channel);
1691 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1692 rate_sym = rx_hdr->rate;
1693 length = le16_to_cpu(rx_hdr->len);
1695 /* end-of-frame status and timestamp */
1696 status = le32_to_cpu(rx_end->status);
1697 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1698 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1699 tsf = le64_to_cpu(rx_end->timestamp);
1701 /* signal statistics */
1702 rssi = rx_stats->rssi;
1703 agc = rx_stats->agc;
1704 sig_avg = le16_to_cpu(rx_stats->sig_avg);
1705 noise_diff = le16_to_cpu(rx_stats->noise_diff);
1707 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1709 /* if data frame is to us and all is good,
1710 * (optionally) print summary for only 1 out of every 100 */
1711 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1712 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1715 print_summary = 1; /* print each frame */
1716 else if (priv->framecnt_to_us < 100) {
1717 priv->framecnt_to_us++;
1720 priv->framecnt_to_us = 0;
1725 /* print summary for all other frames */
1729 if (print_summary) {
1734 title = "100Frames";
1735 else if (fc & IEEE80211_FCTL_RETRY)
1737 else if (ieee80211_is_assoc_response(fc))
1739 else if (ieee80211_is_reassoc_response(fc))
1741 else if (ieee80211_is_probe_response(fc)) {
1743 print_dump = 1; /* dump frame contents */
1744 } else if (ieee80211_is_beacon(fc)) {
1746 print_dump = 1; /* dump frame contents */
1747 } else if (ieee80211_is_atim(fc))
1749 else if (ieee80211_is_auth(fc))
1751 else if (ieee80211_is_deauth(fc))
1753 else if (ieee80211_is_disassoc(fc))
1758 rate = iwl_rate_index_from_plcp(rate_sym);
1762 rate = iwl_rates[rate].ieee / 2;
1764 /* print frame summary.
1765 * MAC addresses show just the last byte (for brevity),
1766 * but you can hack it to show more, if you'd like to. */
1768 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1769 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1770 title, fc, header->addr1[5],
1771 length, rssi, channel, rate);
1773 /* src/dst addresses assume managed mode */
1774 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1775 "src=0x%02x, rssi=%u, tim=%lu usec, "
1776 "phy=0x%02x, chnl=%d\n",
1777 title, fc, header->addr1[5],
1778 header->addr3[5], rssi,
1779 tsf_low - priv->scan_start_tsf,
1780 phy_flags, channel);
1784 iwl_print_hex_dump(IWL_DL_RX, data, length);
1788 static void iwl_unset_hw_setting(struct iwl_priv *priv)
1790 if (priv->hw_setting.shared_virt)
1791 pci_free_consistent(priv->pci_dev,
1792 sizeof(struct iwl_shared),
1793 priv->hw_setting.shared_virt,
1794 priv->hw_setting.shared_phys);
1798 * iwl_supported_rate_to_ie - fill in the supported rate in IE field
1800 * return : set the bit for each supported rate insert in ie
1802 static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
1803 u16 basic_rate, int *left)
1805 u16 ret_rates = 0, bit;
1810 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1811 if (bit & supported_rate) {
1813 rates[*cnt] = iwl_rates[i].ieee |
1814 ((bit & basic_rate) ? 0x80 : 0x00);
1818 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
1826 #ifdef CONFIG_IWLWIFI_HT
1827 void static iwl_set_ht_capab(struct ieee80211_hw *hw,
1828 struct ieee80211_ht_capability *ht_cap,
1833 * iwl_fill_probe_req - fill in all required fields and IE for probe request
1835 static u16 iwl_fill_probe_req(struct iwl_priv *priv,
1836 struct ieee80211_mgmt *frame,
1837 int left, int is_direct)
1841 u16 active_rates, ret_rates, cck_rates;
1843 /* Make sure there is enough space for the probe request,
1844 * two mandatory IEs and the data */
1850 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1851 memcpy(frame->da, BROADCAST_ADDR, ETH_ALEN);
1852 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
1853 memcpy(frame->bssid, BROADCAST_ADDR, ETH_ALEN);
1854 frame->seq_ctrl = 0;
1856 /* fill in our indirect SSID IE */
1863 pos = &(frame->u.probe_req.variable[0]);
1864 *pos++ = WLAN_EID_SSID;
1867 /* fill in our direct SSID IE... */
1870 left -= 2 + priv->essid_len;
1873 /* ... fill it in... */
1874 *pos++ = WLAN_EID_SSID;
1875 *pos++ = priv->essid_len;
1876 memcpy(pos, priv->essid, priv->essid_len);
1877 pos += priv->essid_len;
1878 len += 2 + priv->essid_len;
1881 /* fill in supported rate */
1887 /* ... fill it in... */
1888 *pos++ = WLAN_EID_SUPP_RATES;
1891 priv->active_rate = priv->rates_mask;
1892 active_rates = priv->active_rate;
1893 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1895 cck_rates = IWL_CCK_RATES_MASK & active_rates;
1896 ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
1897 priv->active_rate_basic, &left);
1898 active_rates &= ~ret_rates;
1900 ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
1901 priv->active_rate_basic, &left);
1902 active_rates &= ~ret_rates;
1906 if (active_rates == 0)
1909 /* fill in supported extended rate */
1914 /* ... fill it in... */
1915 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1917 iwl_supported_rate_to_ie(pos, active_rates,
1918 priv->active_rate_basic, &left);
1922 #ifdef CONFIG_IWLWIFI_HT
1923 if (is_direct && priv->is_ht_enabled) {
1924 u8 use_wide_chan = 1;
1926 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
1929 *pos++ = WLAN_EID_HT_CAPABILITY;
1930 *pos++ = sizeof(struct ieee80211_ht_capability);
1931 iwl_set_ht_capab(NULL, (struct ieee80211_ht_capability *)pos,
1933 len += 2 + sizeof(struct ieee80211_ht_capability);
1935 #endif /*CONFIG_IWLWIFI_HT */
1944 #ifdef CONFIG_IWLWIFI_QOS
1945 static int iwl_send_qos_params_command(struct iwl_priv *priv,
1946 struct iwl_qosparam_cmd *qos)
1949 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1950 sizeof(struct iwl_qosparam_cmd), qos);
1953 static void iwl_reset_qos(struct iwl_priv *priv)
1959 unsigned long flags;
1962 spin_lock_irqsave(&priv->lock, flags);
1963 priv->qos_data.qos_active = 0;
1965 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1966 if (priv->qos_data.qos_enable)
1967 priv->qos_data.qos_active = 1;
1968 if (!(priv->active_rate & 0xfff0)) {
1972 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1973 if (priv->qos_data.qos_enable)
1974 priv->qos_data.qos_active = 1;
1975 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1980 if (priv->qos_data.qos_active)
1983 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1984 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1985 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1986 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1987 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1989 if (priv->qos_data.qos_active) {
1991 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1992 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1993 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1994 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1995 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1998 priv->qos_data.def_qos_parm.ac[i].cw_min =
1999 cpu_to_le16((cw_min + 1) / 2 - 1);
2000 priv->qos_data.def_qos_parm.ac[i].cw_max =
2001 cpu_to_le16(cw_max);
2002 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2004 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2007 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2009 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2012 priv->qos_data.def_qos_parm.ac[i].cw_min =
2013 cpu_to_le16((cw_min + 1) / 4 - 1);
2014 priv->qos_data.def_qos_parm.ac[i].cw_max =
2015 cpu_to_le16((cw_max + 1) / 2 - 1);
2016 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2017 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2019 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2022 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2025 for (i = 1; i < 4; i++) {
2026 priv->qos_data.def_qos_parm.ac[i].cw_min =
2027 cpu_to_le16(cw_min);
2028 priv->qos_data.def_qos_parm.ac[i].cw_max =
2029 cpu_to_le16(cw_max);
2030 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
2031 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2032 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2035 IWL_DEBUG_QOS("set QoS to default \n");
2037 spin_unlock_irqrestore(&priv->lock, flags);
2040 static void iwl_activate_qos(struct iwl_priv *priv, u8 force)
2042 unsigned long flags;
2047 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2050 if (!priv->qos_data.qos_enable)
2053 spin_lock_irqsave(&priv->lock, flags);
2054 priv->qos_data.def_qos_parm.qos_flags = 0;
2056 if (priv->qos_data.qos_cap.q_AP.queue_request &&
2057 !priv->qos_data.qos_cap.q_AP.txop_request)
2058 priv->qos_data.def_qos_parm.qos_flags |=
2059 QOS_PARAM_FLG_TXOP_TYPE_MSK;
2061 if (priv->qos_data.qos_active)
2062 priv->qos_data.def_qos_parm.qos_flags |=
2063 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2065 spin_unlock_irqrestore(&priv->lock, flags);
2067 if (force || iwl_is_associated(priv)) {
2068 IWL_DEBUG_QOS("send QoS cmd with Qos active %d \n",
2069 priv->qos_data.qos_active);
2071 iwl_send_qos_params_command(priv,
2072 &(priv->qos_data.def_qos_parm));
2076 #endif /* CONFIG_IWLWIFI_QOS */
2078 * Power management (not Tx power!) functions
2080 #define MSEC_TO_USEC 1024
2082 #define NOSLP __constant_cpu_to_le16(0), 0, 0
2083 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
2084 #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2085 #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2086 __constant_cpu_to_le32(X1), \
2087 __constant_cpu_to_le32(X2), \
2088 __constant_cpu_to_le32(X3), \
2089 __constant_cpu_to_le32(X4)}
2092 /* default power management (not Tx power) table values */
2094 static struct iwl_power_vec_entry range_0[IWL_POWER_AC] = {
2095 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2096 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2097 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2098 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2099 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2100 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2104 static struct iwl_power_vec_entry range_1[IWL_POWER_AC] = {
2105 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2106 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2107 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2108 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2109 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2110 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2111 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2112 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2113 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2114 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2117 int iwl_power_init_handle(struct iwl_priv *priv)
2120 struct iwl_power_mgr *pow_data;
2121 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
2124 IWL_DEBUG_POWER("Initialize power \n");
2126 pow_data = &(priv->power_data);
2128 memset(pow_data, 0, sizeof(*pow_data));
2130 pow_data->active_index = IWL_POWER_RANGE_0;
2131 pow_data->dtim_val = 0xffff;
2133 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2134 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2136 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2140 struct iwl_powertable_cmd *cmd;
2142 IWL_DEBUG_POWER("adjust power command flags\n");
2144 for (i = 0; i < IWL_POWER_AC; i++) {
2145 cmd = &pow_data->pwr_range_0[i].cmd;
2148 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2150 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2156 static int iwl_update_power_cmd(struct iwl_priv *priv,
2157 struct iwl_powertable_cmd *cmd, u32 mode)
2162 struct iwl_power_vec_entry *range;
2164 struct iwl_power_mgr *pow_data;
2166 if (mode > IWL_POWER_INDEX_5) {
2167 IWL_DEBUG_POWER("Error invalid power mode \n");
2170 pow_data = &(priv->power_data);
2172 if (pow_data->active_index == IWL_POWER_RANGE_0)
2173 range = &pow_data->pwr_range_0[0];
2175 range = &pow_data->pwr_range_1[1];
2177 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
2179 #ifdef IWL_MAC80211_DISABLE
2180 if (priv->assoc_network != NULL) {
2181 unsigned long flags;
2183 period = priv->assoc_network->tim.tim_period;
2185 #endif /*IWL_MAC80211_DISABLE */
2186 skip = range[mode].no_dtim;
2195 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2197 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2198 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2199 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2202 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2203 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2204 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2207 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2208 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2209 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2210 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2211 le32_to_cpu(cmd->sleep_interval[0]),
2212 le32_to_cpu(cmd->sleep_interval[1]),
2213 le32_to_cpu(cmd->sleep_interval[2]),
2214 le32_to_cpu(cmd->sleep_interval[3]),
2215 le32_to_cpu(cmd->sleep_interval[4]));
2220 static int iwl_send_power_mode(struct iwl_priv *priv, u32 mode)
2222 u32 final_mode = mode;
2224 struct iwl_powertable_cmd cmd;
2226 /* If on battery, set to 3,
2227 * if plugged into AC power, set to CAM ("continuosly aware mode"),
2228 * else user level */
2230 case IWL_POWER_BATTERY:
2231 final_mode = IWL_POWER_INDEX_3;
2234 final_mode = IWL_POWER_MODE_CAM;
2241 cmd.keep_alive_beacons = 0;
2243 iwl_update_power_cmd(priv, &cmd, final_mode);
2245 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2247 if (final_mode == IWL_POWER_MODE_CAM)
2248 clear_bit(STATUS_POWER_PMI, &priv->status);
2250 set_bit(STATUS_POWER_PMI, &priv->status);
2255 int iwl_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
2257 /* Filter incoming packets to determine if they are targeted toward
2258 * this network, discarding packets coming from ourselves */
2259 switch (priv->iw_mode) {
2260 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2261 /* packets from our adapter are dropped (echo) */
2262 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2264 /* {broad,multi}cast packets to our IBSS go through */
2265 if (is_multicast_ether_addr(header->addr1))
2266 return !compare_ether_addr(header->addr3, priv->bssid);
2267 /* packets to our adapter go through */
2268 return !compare_ether_addr(header->addr1, priv->mac_addr);
2269 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2270 /* packets from our adapter are dropped (echo) */
2271 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2273 /* {broad,multi}cast packets to our BSS go through */
2274 if (is_multicast_ether_addr(header->addr1))
2275 return !compare_ether_addr(header->addr2, priv->bssid);
2276 /* packets to our adapter go through */
2277 return !compare_ether_addr(header->addr1, priv->mac_addr);
2283 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2285 const char *iwl_get_tx_fail_reason(u32 status)
2287 switch (status & TX_STATUS_MSK) {
2288 case TX_STATUS_SUCCESS:
2290 TX_STATUS_ENTRY(SHORT_LIMIT);
2291 TX_STATUS_ENTRY(LONG_LIMIT);
2292 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2293 TX_STATUS_ENTRY(MGMNT_ABORT);
2294 TX_STATUS_ENTRY(NEXT_FRAG);
2295 TX_STATUS_ENTRY(LIFE_EXPIRE);
2296 TX_STATUS_ENTRY(DEST_PS);
2297 TX_STATUS_ENTRY(ABORTED);
2298 TX_STATUS_ENTRY(BT_RETRY);
2299 TX_STATUS_ENTRY(STA_INVALID);
2300 TX_STATUS_ENTRY(FRAG_DROPPED);
2301 TX_STATUS_ENTRY(TID_DISABLE);
2302 TX_STATUS_ENTRY(FRAME_FLUSHED);
2303 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2304 TX_STATUS_ENTRY(TX_LOCKED);
2305 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2312 * iwl_scan_cancel - Cancel any currently executing HW scan
2314 * NOTE: priv->mutex is not required before calling this function
2316 static int iwl_scan_cancel(struct iwl_priv *priv)
2318 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2319 clear_bit(STATUS_SCANNING, &priv->status);
2323 if (test_bit(STATUS_SCANNING, &priv->status)) {
2324 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2325 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2326 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2327 queue_work(priv->workqueue, &priv->abort_scan);
2330 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2332 return test_bit(STATUS_SCANNING, &priv->status);
2339 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
2340 * @ms: amount of time to wait (in milliseconds) for scan to abort
2342 * NOTE: priv->mutex must be held before calling this function
2344 static int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
2346 unsigned long now = jiffies;
2349 ret = iwl_scan_cancel(priv);
2351 mutex_unlock(&priv->mutex);
2352 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2353 test_bit(STATUS_SCANNING, &priv->status))
2355 mutex_lock(&priv->mutex);
2357 return test_bit(STATUS_SCANNING, &priv->status);
2363 static void iwl_sequence_reset(struct iwl_priv *priv)
2365 /* Reset ieee stats */
2367 /* We don't reset the net_device_stats (ieee->stats) on
2370 priv->last_seq_num = -1;
2371 priv->last_frag_num = -1;
2372 priv->last_packet_time = 0;
2374 iwl_scan_cancel(priv);
2377 #define MAX_UCODE_BEACON_INTERVAL 4096
2378 #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2380 static __le16 iwl_adjust_beacon_interval(u16 beacon_val)
2383 u16 beacon_factor = 0;
2386 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2387 / MAX_UCODE_BEACON_INTERVAL;
2388 new_val = beacon_val / beacon_factor;
2390 return cpu_to_le16(new_val);
2393 static void iwl_setup_rxon_timing(struct iwl_priv *priv)
2395 u64 interval_tm_unit;
2397 unsigned long flags;
2398 struct ieee80211_conf *conf = NULL;
2401 conf = ieee80211_get_hw_conf(priv->hw);
2403 spin_lock_irqsave(&priv->lock, flags);
2404 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2405 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2407 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2409 tsf = priv->timestamp1;
2410 tsf = ((tsf << 32) | priv->timestamp0);
2412 beacon_int = priv->beacon_int;
2413 spin_unlock_irqrestore(&priv->lock, flags);
2415 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2416 if (beacon_int == 0) {
2417 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2418 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2420 priv->rxon_timing.beacon_interval =
2421 cpu_to_le16(beacon_int);
2422 priv->rxon_timing.beacon_interval =
2423 iwl_adjust_beacon_interval(
2424 le16_to_cpu(priv->rxon_timing.beacon_interval));
2427 priv->rxon_timing.atim_window = 0;
2429 priv->rxon_timing.beacon_interval =
2430 iwl_adjust_beacon_interval(conf->beacon_int);
2431 /* TODO: we need to get atim_window from upper stack
2432 * for now we set to 0 */
2433 priv->rxon_timing.atim_window = 0;
2437 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2438 result = do_div(tsf, interval_tm_unit);
2439 priv->rxon_timing.beacon_init_val =
2440 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2443 ("beacon interval %d beacon timer %d beacon tim %d\n",
2444 le16_to_cpu(priv->rxon_timing.beacon_interval),
2445 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2446 le16_to_cpu(priv->rxon_timing.atim_window));
2449 static int iwl_scan_initiate(struct iwl_priv *priv)
2451 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2452 IWL_ERROR("APs don't scan.\n");
2456 if (!iwl_is_ready_rf(priv)) {
2457 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2461 if (test_bit(STATUS_SCANNING, &priv->status)) {
2462 IWL_DEBUG_SCAN("Scan already in progress.\n");
2466 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2467 IWL_DEBUG_SCAN("Scan request while abort pending. "
2472 IWL_DEBUG_INFO("Starting scan...\n");
2473 priv->scan_bands = 2;
2474 set_bit(STATUS_SCANNING, &priv->status);
2475 priv->scan_start = jiffies;
2476 priv->scan_pass_start = priv->scan_start;
2478 queue_work(priv->workqueue, &priv->request_scan);
2483 static int iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
2485 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
2488 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2490 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2495 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode)
2497 if (phymode == MODE_IEEE80211A) {
2498 priv->staging_rxon.flags &=
2499 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2500 | RXON_FLG_CCK_MSK);
2501 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2503 /* Copied from iwl_bg_post_associate() */
2504 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2505 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2507 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2509 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2510 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2512 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2513 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2514 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2519 * initilize rxon structure with default values fromm eeprom
2521 static void iwl_connection_init_rx_config(struct iwl_priv *priv)
2523 const struct iwl_channel_info *ch_info;
2525 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2527 switch (priv->iw_mode) {
2528 case IEEE80211_IF_TYPE_AP:
2529 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2532 case IEEE80211_IF_TYPE_STA:
2533 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2534 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2537 case IEEE80211_IF_TYPE_IBSS:
2538 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2539 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2540 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2541 RXON_FILTER_ACCEPT_GRP_MSK;
2544 case IEEE80211_IF_TYPE_MNTR:
2545 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2546 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2547 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2552 /* TODO: Figure out when short_preamble would be set and cache from
2554 if (!hw_to_local(priv->hw)->short_preamble)
2555 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2557 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2560 ch_info = iwl_get_channel_info(priv, priv->phymode,
2561 le16_to_cpu(priv->staging_rxon.channel));
2564 ch_info = &priv->channel_info[0];
2567 * in some case A channels are all non IBSS
2568 * in this case force B/G channel
2570 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2571 !(is_channel_ibss(ch_info)))
2572 ch_info = &priv->channel_info[0];
2574 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2575 if (is_channel_a_band(ch_info))
2576 priv->phymode = MODE_IEEE80211A;
2578 priv->phymode = MODE_IEEE80211G;
2580 iwl_set_flags_for_phymode(priv, priv->phymode);
2582 priv->staging_rxon.ofdm_basic_rates =
2583 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2584 priv->staging_rxon.cck_basic_rates =
2585 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2587 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2588 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2589 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2590 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2591 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2592 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2593 iwl4965_set_rxon_chain(priv);
2596 static int iwl_set_mode(struct iwl_priv *priv, int mode)
2598 if (!iwl_is_ready_rf(priv))
2601 if (mode == IEEE80211_IF_TYPE_IBSS) {
2602 const struct iwl_channel_info *ch_info;
2604 ch_info = iwl_get_channel_info(priv,
2606 le16_to_cpu(priv->staging_rxon.channel));
2608 if (!ch_info || !is_channel_ibss(ch_info)) {
2609 IWL_ERROR("channel %d not IBSS channel\n",
2610 le16_to_cpu(priv->staging_rxon.channel));
2615 cancel_delayed_work(&priv->scan_check);
2616 if (iwl_scan_cancel_timeout(priv, 100)) {
2617 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2618 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2622 priv->iw_mode = mode;
2624 iwl_connection_init_rx_config(priv);
2625 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2627 iwl_clear_stations_table(priv);
2629 iwl_commit_rxon(priv);
2634 static void iwl_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
2635 struct ieee80211_tx_control *ctl,
2636 struct iwl_cmd *cmd,
2637 struct sk_buff *skb_frag,
2640 struct iwl_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2642 switch (keyinfo->alg) {
2644 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2645 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2646 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2651 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2654 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2657 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2662 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2663 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2665 if (keyinfo->keylen == 13)
2666 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2668 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2670 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2671 "with key %d\n", ctl->key_idx);
2675 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2681 * handle build REPLY_TX command notification.
2683 static void iwl_build_tx_cmd_basic(struct iwl_priv *priv,
2684 struct iwl_cmd *cmd,
2685 struct ieee80211_tx_control *ctrl,
2686 struct ieee80211_hdr *hdr,
2687 int is_unicast, u8 std_id)
2690 u16 fc = le16_to_cpu(hdr->frame_control);
2691 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2693 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2694 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2695 tx_flags |= TX_CMD_FLG_ACK_MSK;
2696 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2697 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2698 if (ieee80211_is_probe_response(fc) &&
2699 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2700 tx_flags |= TX_CMD_FLG_TSF_MSK;
2702 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2703 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2706 cmd->cmd.tx.sta_id = std_id;
2707 if (ieee80211_get_morefrag(hdr))
2708 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2710 qc = ieee80211_get_qos_ctrl(hdr);
2712 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2713 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2715 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2717 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2718 tx_flags |= TX_CMD_FLG_RTS_MSK;
2719 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2720 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2721 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2722 tx_flags |= TX_CMD_FLG_CTS_MSK;
2725 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2726 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2728 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2729 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2730 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2731 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
2732 cmd->cmd.tx.timeout.pm_frame_timeout =
2735 cmd->cmd.tx.timeout.pm_frame_timeout =
2738 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2740 cmd->cmd.tx.driver_txop = 0;
2741 cmd->cmd.tx.tx_flags = tx_flags;
2742 cmd->cmd.tx.next_frame_len = 0;
2745 static int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
2748 u16 fc = le16_to_cpu(hdr->frame_control);
2749 DECLARE_MAC_BUF(mac);
2751 /* If this frame is broadcast or not data then use the broadcast
2753 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2754 is_multicast_ether_addr(hdr->addr1))
2755 return priv->hw_setting.bcast_sta_id;
2757 switch (priv->iw_mode) {
2759 /* If this frame is part of a BSS network (we're a station), then
2760 * we use the AP's station id */
2761 case IEEE80211_IF_TYPE_STA:
2764 /* If we are an AP, then find the station, or use BCAST */
2765 case IEEE80211_IF_TYPE_AP:
2766 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2767 if (sta_id != IWL_INVALID_STATION)
2769 return priv->hw_setting.bcast_sta_id;
2771 /* If this frame is part of a IBSS network, then we use the
2772 * target specific station id */
2773 case IEEE80211_IF_TYPE_IBSS:
2774 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2775 if (sta_id != IWL_INVALID_STATION)
2778 sta_id = iwl_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
2780 if (sta_id != IWL_INVALID_STATION)
2783 IWL_DEBUG_DROP("Station %s not in station map. "
2784 "Defaulting to broadcast...\n",
2785 print_mac(mac, hdr->addr1));
2786 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2787 return priv->hw_setting.bcast_sta_id;
2790 IWL_WARNING("Unkown mode of operation: %d", priv->iw_mode);
2791 return priv->hw_setting.bcast_sta_id;
2796 * start REPLY_TX command process
2798 static int iwl_tx_skb(struct iwl_priv *priv,
2799 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2801 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2802 struct iwl_tfd_frame *tfd;
2804 int txq_id = ctl->queue;
2805 struct iwl_tx_queue *txq = NULL;
2806 struct iwl_queue *q = NULL;
2807 dma_addr_t phys_addr;
2808 dma_addr_t txcmd_phys;
2809 struct iwl_cmd *out_cmd = NULL;
2810 u16 len, idx, len_org;
2811 u8 id, hdr_len, unicast;
2816 u8 wait_write_ptr = 0;
2817 unsigned long flags;
2820 spin_lock_irqsave(&priv->lock, flags);
2821 if (iwl_is_rfkill(priv)) {
2822 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2826 if (!priv->interface_id) {
2827 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2831 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2832 IWL_ERROR("ERROR: No TX rate available.\n");
2836 unicast = !is_multicast_ether_addr(hdr->addr1);
2839 fc = le16_to_cpu(hdr->frame_control);
2841 #ifdef CONFIG_IWLWIFI_DEBUG
2842 if (ieee80211_is_auth(fc))
2843 IWL_DEBUG_TX("Sending AUTH frame\n");
2844 else if (ieee80211_is_assoc_request(fc))
2845 IWL_DEBUG_TX("Sending ASSOC frame\n");
2846 else if (ieee80211_is_reassoc_request(fc))
2847 IWL_DEBUG_TX("Sending REASSOC frame\n");
2850 if (!iwl_is_associated(priv) &&
2851 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
2852 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
2856 spin_unlock_irqrestore(&priv->lock, flags);
2858 hdr_len = ieee80211_get_hdrlen(fc);
2859 sta_id = iwl_get_sta_id(priv, hdr);
2860 if (sta_id == IWL_INVALID_STATION) {
2861 DECLARE_MAC_BUF(mac);
2863 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2864 print_mac(mac, hdr->addr1));
2868 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2870 qc = ieee80211_get_qos_ctrl(hdr);
2872 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2873 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2875 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2877 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2879 #ifdef CONFIG_IWLWIFI_HT
2880 #ifdef CONFIG_IWLWIFI_HT_AGG
2881 /* aggregation is on for this <sta,tid> */
2882 if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
2883 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
2884 #endif /* CONFIG_IWLWIFI_HT_AGG */
2885 #endif /* CONFIG_IWLWIFI_HT */
2887 txq = &priv->txq[txq_id];
2890 spin_lock_irqsave(&priv->lock, flags);
2892 tfd = &txq->bd[q->first_empty];
2893 memset(tfd, 0, sizeof(*tfd));
2894 control_flags = (u32 *) tfd;
2895 idx = get_cmd_index(q, q->first_empty, 0);
2897 memset(&(txq->txb[q->first_empty]), 0, sizeof(struct iwl_tx_info));
2898 txq->txb[q->first_empty].skb[0] = skb;
2899 memcpy(&(txq->txb[q->first_empty].status.control),
2900 ctl, sizeof(struct ieee80211_tx_control));
2901 out_cmd = &txq->cmd[idx];
2902 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2903 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2904 out_cmd->hdr.cmd = REPLY_TX;
2905 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2906 INDEX_TO_SEQ(q->first_empty)));
2907 /* copy frags header */
2908 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2910 /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
2911 len = priv->hw_setting.tx_cmd_len +
2912 sizeof(struct iwl_cmd_header) + hdr_len;
2915 len = (len + 3) & ~3;
2922 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2923 offsetof(struct iwl_cmd, hdr);
2925 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
2927 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
2928 iwl_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
2930 /* 802.11 null functions have no payload... */
2931 len = skb->len - hdr_len;
2933 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2934 len, PCI_DMA_TODEVICE);
2935 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
2939 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2941 len = (u16)skb->len;
2942 out_cmd->cmd.tx.len = cpu_to_le16(len);
2944 /* TODO need this for burst mode later on */
2945 iwl_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
2947 /* set is_hcca to 0; it probably will never be implemented */
2948 iwl_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
2950 iwl4965_tx_cmd(priv, out_cmd, sta_id, txcmd_phys,
2951 hdr, hdr_len, ctl, NULL);
2953 if (!ieee80211_get_morefrag(hdr)) {
2954 txq->need_update = 1;
2956 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2957 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2961 txq->need_update = 0;
2964 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
2965 sizeof(out_cmd->cmd.tx));
2967 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
2968 ieee80211_get_hdrlen(fc));
2970 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2972 q->first_empty = iwl_queue_inc_wrap(q->first_empty, q->n_bd);
2973 rc = iwl_tx_queue_update_write_ptr(priv, txq);
2974 spin_unlock_irqrestore(&priv->lock, flags);
2979 if ((iwl_queue_space(q) < q->high_mark)
2980 && priv->mac80211_registered) {
2981 if (wait_write_ptr) {
2982 spin_lock_irqsave(&priv->lock, flags);
2983 txq->need_update = 1;
2984 iwl_tx_queue_update_write_ptr(priv, txq);
2985 spin_unlock_irqrestore(&priv->lock, flags);
2988 ieee80211_stop_queue(priv->hw, ctl->queue);
2994 spin_unlock_irqrestore(&priv->lock, flags);
2999 static void iwl_set_rate(struct iwl_priv *priv)
3001 const struct ieee80211_hw_mode *hw = NULL;
3002 struct ieee80211_rate *rate;
3005 hw = iwl_get_hw_mode(priv, priv->phymode);
3007 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
3011 priv->active_rate = 0;
3012 priv->active_rate_basic = 0;
3014 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
3015 hw->mode == MODE_IEEE80211A ?
3016 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
3018 for (i = 0; i < hw->num_rates; i++) {
3019 rate = &(hw->rates[i]);
3020 if ((rate->val < IWL_RATE_COUNT) &&
3021 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
3022 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
3023 rate->val, iwl_rates[rate->val].plcp,
3024 (rate->flags & IEEE80211_RATE_BASIC) ?
3026 priv->active_rate |= (1 << rate->val);
3027 if (rate->flags & IEEE80211_RATE_BASIC)
3028 priv->active_rate_basic |= (1 << rate->val);
3030 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
3031 rate->val, iwl_rates[rate->val].plcp);
3034 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
3035 priv->active_rate, priv->active_rate_basic);
3038 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
3039 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
3042 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
3043 priv->staging_rxon.cck_basic_rates =
3044 ((priv->active_rate_basic &
3045 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
3047 priv->staging_rxon.cck_basic_rates =
3048 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
3050 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
3051 priv->staging_rxon.ofdm_basic_rates =
3052 ((priv->active_rate_basic &
3053 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
3054 IWL_FIRST_OFDM_RATE) & 0xFF;
3056 priv->staging_rxon.ofdm_basic_rates =
3057 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
3060 static void iwl_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
3062 unsigned long flags;
3064 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
3067 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
3068 disable_radio ? "OFF" : "ON");
3070 if (disable_radio) {
3071 iwl_scan_cancel(priv);
3072 /* FIXME: This is a workaround for AP */
3073 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3074 spin_lock_irqsave(&priv->lock, flags);
3075 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
3076 CSR_UCODE_SW_BIT_RFKILL);
3077 spin_unlock_irqrestore(&priv->lock, flags);
3078 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
3079 set_bit(STATUS_RF_KILL_SW, &priv->status);
3084 spin_lock_irqsave(&priv->lock, flags);
3085 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3087 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3088 spin_unlock_irqrestore(&priv->lock, flags);
3093 spin_lock_irqsave(&priv->lock, flags);
3094 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3095 if (!iwl_grab_restricted_access(priv))
3096 iwl_release_restricted_access(priv);
3097 spin_unlock_irqrestore(&priv->lock, flags);
3099 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3100 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3101 "disabled by HW switch\n");
3105 queue_work(priv->workqueue, &priv->restart);
3109 void iwl_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
3110 u32 decrypt_res, struct ieee80211_rx_status *stats)
3113 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3115 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3118 if (!(fc & IEEE80211_FCTL_PROTECTED))
3121 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3122 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3123 case RX_RES_STATUS_SEC_TYPE_TKIP:
3124 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3125 RX_RES_STATUS_BAD_ICV_MIC)
3126 stats->flag |= RX_FLAG_MMIC_ERROR;
3127 case RX_RES_STATUS_SEC_TYPE_WEP:
3128 case RX_RES_STATUS_SEC_TYPE_CCMP:
3129 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3130 RX_RES_STATUS_DECRYPT_OK) {
3131 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3132 stats->flag |= RX_FLAG_DECRYPTED;
3141 void iwl_handle_data_packet_monitor(struct iwl_priv *priv,
3142 struct iwl_rx_mem_buffer *rxb,
3143 void *data, short len,
3144 struct ieee80211_rx_status *stats,
3147 struct iwl_rt_rx_hdr *iwl_rt;
3149 /* First cache any information we need before we overwrite
3150 * the information provided in the skb from the hardware */
3151 s8 signal = stats->ssi;
3153 int rate = stats->rate;
3154 u64 tsf = stats->mactime;
3155 __le16 phy_flags_hw = cpu_to_le16(phy_flags);
3157 /* We received data from the HW, so stop the watchdog */
3158 if (len > IWL_RX_BUF_SIZE - sizeof(*iwl_rt)) {
3159 IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
3163 /* copy the frame data to write after where the radiotap header goes */
3164 iwl_rt = (void *)rxb->skb->data;
3165 memmove(iwl_rt->payload, data, len);
3167 iwl_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
3168 iwl_rt->rt_hdr.it_pad = 0; /* always good to zero */
3170 /* total header + data */
3171 iwl_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl_rt));
3173 /* Set the size of the skb to the size of the frame */
3174 skb_put(rxb->skb, sizeof(*iwl_rt) + len);
3176 /* Big bitfield of all the fields we provide in radiotap */
3177 iwl_rt->rt_hdr.it_present =
3178 cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
3179 (1 << IEEE80211_RADIOTAP_FLAGS) |
3180 (1 << IEEE80211_RADIOTAP_RATE) |
3181 (1 << IEEE80211_RADIOTAP_CHANNEL) |
3182 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
3183 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
3184 (1 << IEEE80211_RADIOTAP_ANTENNA));
3186 /* Zero the flags, we'll add to them as we go */
3187 iwl_rt->rt_flags = 0;
3189 iwl_rt->rt_tsf = cpu_to_le64(tsf);
3191 /* Convert to dBm */
3192 iwl_rt->rt_dbmsignal = signal;
3193 iwl_rt->rt_dbmnoise = noise;
3195 /* Convert the channel frequency and set the flags */
3196 iwl_rt->rt_channelMHz = cpu_to_le16(stats->freq);
3197 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
3198 iwl_rt->rt_chbitmask =
3199 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
3200 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
3201 iwl_rt->rt_chbitmask =
3202 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
3204 iwl_rt->rt_chbitmask =
3205 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
3207 rate = iwl_rate_index_from_plcp(rate);
3209 iwl_rt->rt_rate = 0;
3211 iwl_rt->rt_rate = iwl_rates[rate].ieee;
3213 /* antenna number */
3214 iwl_rt->rt_antenna =
3215 le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
3217 /* set the preamble flag if we have it */
3218 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
3219 iwl_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3221 IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
3223 stats->flag |= RX_FLAG_RADIOTAP;
3224 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3229 #define IWL_PACKET_RETRY_TIME HZ
3231 int is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
3233 u16 sc = le16_to_cpu(header->seq_ctrl);
3234 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3235 u16 frag = sc & IEEE80211_SCTL_FRAG;
3236 u16 *last_seq, *last_frag;
3237 unsigned long *last_time;
3239 switch (priv->iw_mode) {
3240 case IEEE80211_IF_TYPE_IBSS:{
3241 struct list_head *p;
3242 struct iwl_ibss_seq *entry = NULL;
3243 u8 *mac = header->addr2;
3244 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3246 __list_for_each(p, &priv->ibss_mac_hash[index]) {
3248 list_entry(p, struct iwl_ibss_seq, list);
3249 if (!compare_ether_addr(entry->mac, mac))
3252 if (p == &priv->ibss_mac_hash[index]) {
3253 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3256 ("Cannot malloc new mac entry\n");
3259 memcpy(entry->mac, mac, ETH_ALEN);
3260 entry->seq_num = seq;
3261 entry->frag_num = frag;
3262 entry->packet_time = jiffies;
3263 list_add(&entry->list,
3264 &priv->ibss_mac_hash[index]);
3267 last_seq = &entry->seq_num;
3268 last_frag = &entry->frag_num;
3269 last_time = &entry->packet_time;
3272 case IEEE80211_IF_TYPE_STA:
3273 last_seq = &priv->last_seq_num;
3274 last_frag = &priv->last_frag_num;
3275 last_time = &priv->last_packet_time;
3280 if ((*last_seq == seq) &&
3281 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3282 if (*last_frag == frag)
3284 if (*last_frag + 1 != frag)
3285 /* out-of-order fragment */
3291 *last_time = jiffies;
3298 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3300 #include "iwl-spectrum.h"
3302 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
3303 #define BEACON_TIME_MASK_HIGH 0xFF000000
3304 #define TIME_UNIT 1024
3307 * extended beacon time format
3308 * time in usec will be changed into a 32-bit value in 8:24 format
3309 * the high 1 byte is the beacon counts
3310 * the lower 3 bytes is the time in usec within one beacon interval
3313 static u32 iwl_usecs_to_beacons(u32 usec, u32 beacon_interval)
3317 u32 interval = beacon_interval * 1024;
3319 if (!interval || !usec)
3322 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3323 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3325 return (quot << 24) + rem;
3328 /* base is usually what we get from ucode with each received frame,
3329 * the same as HW timer counter counting down
3332 static __le32 iwl_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
3334 u32 base_low = base & BEACON_TIME_MASK_LOW;
3335 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3336 u32 interval = beacon_interval * TIME_UNIT;
3337 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3338 (addon & BEACON_TIME_MASK_HIGH);
3340 if (base_low > addon_low)
3341 res += base_low - addon_low;
3342 else if (base_low < addon_low) {
3343 res += interval + base_low - addon_low;
3348 return cpu_to_le32(res);
3351 static int iwl_get_measurement(struct iwl_priv *priv,
3352 struct ieee80211_measurement_params *params,
3355 struct iwl_spectrum_cmd spectrum;
3356 struct iwl_rx_packet *res;
3357 struct iwl_host_cmd cmd = {
3358 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3359 .data = (void *)&spectrum,
3360 .meta.flags = CMD_WANT_SKB,
3362 u32 add_time = le64_to_cpu(params->start_time);
3364 int spectrum_resp_status;
3365 int duration = le16_to_cpu(params->duration);
3367 if (iwl_is_associated(priv))
3369 iwl_usecs_to_beacons(
3370 le64_to_cpu(params->start_time) - priv->last_tsf,
3371 le16_to_cpu(priv->rxon_timing.beacon_interval));
3373 memset(&spectrum, 0, sizeof(spectrum));
3375 spectrum.channel_count = cpu_to_le16(1);
3377 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3378 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3379 cmd.len = sizeof(spectrum);
3380 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3382 if (iwl_is_associated(priv))
3383 spectrum.start_time =
3384 iwl_add_beacon_time(priv->last_beacon_time,
3386 le16_to_cpu(priv->rxon_timing.beacon_interval));
3388 spectrum.start_time = 0;
3390 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3391 spectrum.channels[0].channel = params->channel;
3392 spectrum.channels[0].type = type;
3393 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3394 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3395 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3397 rc = iwl_send_cmd_sync(priv, &cmd);
3401 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
3402 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3403 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3407 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3408 switch (spectrum_resp_status) {
3409 case 0: /* Command will be handled */
3410 if (res->u.spectrum.id != 0xff) {
3412 ("Replaced existing measurement: %d\n",
3413 res->u.spectrum.id);
3414 priv->measurement_status &= ~MEASUREMENT_READY;
3416 priv->measurement_status |= MEASUREMENT_ACTIVE;
3420 case 1: /* Command will not be handled */
3425 dev_kfree_skb_any(cmd.meta.u.skb);
3431 static void iwl_txstatus_to_ieee(struct iwl_priv *priv,
3432 struct iwl_tx_info *tx_sta)
3435 tx_sta->status.ack_signal = 0;
3436 tx_sta->status.excessive_retries = 0;
3437 tx_sta->status.queue_length = 0;
3438 tx_sta->status.queue_number = 0;
3441 ieee80211_tx_status_irqsafe(priv->hw,
3442 tx_sta->skb[0], &(tx_sta->status));
3444 ieee80211_tx_status(priv->hw,
3445 tx_sta->skb[0], &(tx_sta->status));
3447 tx_sta->skb[0] = NULL;
3451 * iwl_tx_queue_reclaim - Reclaim Tx queue entries no more used by NIC.
3453 * When FW advances 'R' index, all entries between old and
3454 * new 'R' index need to be reclaimed. As result, some free space
3455 * forms. If there is enough free space (> low mark), wake Tx queue.
3457 int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
3459 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3460 struct iwl_queue *q = &txq->q;
3463 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3464 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3465 "is out of range [0-%d] %d %d.\n", txq_id,
3466 index, q->n_bd, q->first_empty, q->last_used);
3470 for (index = iwl_queue_inc_wrap(index, q->n_bd);
3471 q->last_used != index;
3472 q->last_used = iwl_queue_inc_wrap(q->last_used, q->n_bd)) {
3473 if (txq_id != IWL_CMD_QUEUE_NUM) {
3474 iwl_txstatus_to_ieee(priv,
3475 &(txq->txb[txq->q.last_used]));
3476 iwl_hw_txq_free_tfd(priv, txq);
3477 } else if (nfreed > 1) {
3478 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
3479 q->first_empty, q->last_used);
3480 queue_work(priv->workqueue, &priv->restart);
3485 if (iwl_queue_space(q) > q->low_mark && (txq_id >= 0) &&
3486 (txq_id != IWL_CMD_QUEUE_NUM) &&
3487 priv->mac80211_registered)
3488 ieee80211_wake_queue(priv->hw, txq_id);
3494 static int iwl_is_tx_success(u32 status)
3496 status &= TX_STATUS_MSK;
3497 return (status == TX_STATUS_SUCCESS)
3498 || (status == TX_STATUS_DIRECT_DONE);
3501 /******************************************************************************
3503 * Generic RX handler implementations
3505 ******************************************************************************/
3506 #ifdef CONFIG_IWLWIFI_HT
3507 #ifdef CONFIG_IWLWIFI_HT_AGG
3509 static inline int iwl_get_ra_sta_id(struct iwl_priv *priv,
3510 struct ieee80211_hdr *hdr)
3512 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3515 u8 *da = ieee80211_get_DA(hdr);
3516 return iwl_hw_find_station(priv, da);
3520 static struct ieee80211_hdr *iwl_tx_queue_get_hdr(
3521 struct iwl_priv *priv, int txq_id, int idx)
3523 if (priv->txq[txq_id].txb[idx].skb[0])
3524 return (struct ieee80211_hdr *)priv->txq[txq_id].
3525 txb[idx].skb[0]->data;
3529 static inline u32 iwl_get_scd_ssn(struct iwl_tx_resp *tx_resp)
3531 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3532 tx_resp->frame_count);
3533 return le32_to_cpu(*scd_ssn) & MAX_SN;
3536 static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
3537 struct iwl_ht_agg *agg,
3538 struct iwl_tx_resp *tx_resp,
3542 __le32 *frame_status = &tx_resp->status;
3543 struct ieee80211_tx_status *tx_status = NULL;
3544 struct ieee80211_hdr *hdr = NULL;
3549 if (agg->wait_for_ba)
3550 IWL_DEBUG_TX_REPLY("got tx repsons w/o back\n");
3552 agg->frame_count = tx_resp->frame_count;
3553 agg->start_idx = start_idx;
3554 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3555 agg->bitmap0 = agg->bitmap1 = 0;
3557 if (agg->frame_count == 1) {
3558 struct iwl_tx_queue *txq ;
3559 status = le32_to_cpu(frame_status[0]);
3561 txq_id = agg->txq_id;
3562 txq = &priv->txq[txq_id];
3563 /* FIXME: code repetition */
3564 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d \n",
3565 agg->frame_count, agg->start_idx);
3567 tx_status = &(priv->txq[txq_id].txb[txq->q.last_used].status);
3568 tx_status->retry_count = tx_resp->failure_frame;
3569 tx_status->queue_number = status & 0xff;
3570 tx_status->queue_length = tx_resp->bt_kill_count;
3571 tx_status->queue_length |= tx_resp->failure_rts;
3573 tx_status->flags = iwl_is_tx_success(status)?
3574 IEEE80211_TX_STATUS_ACK : 0;
3575 tx_status->control.tx_rate =
3576 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags);
3577 /* FIXME: code repetition end */
3579 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3580 status & 0xff, tx_resp->failure_frame);
3581 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
3582 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags));
3584 agg->wait_for_ba = 0;
3587 int start = agg->start_idx;
3589 for (i = 0; i < agg->frame_count; i++) {
3591 status = le32_to_cpu(frame_status[i]);
3593 idx = SEQ_TO_INDEX(seq);
3594 txq_id = SEQ_TO_QUEUE(seq);
3596 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3597 AGG_TX_STATE_ABORT_MSK))
3600 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3601 agg->frame_count, txq_id, idx);
3603 hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
3605 sc = le16_to_cpu(hdr->seq_ctrl);
3606 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3607 IWL_ERROR("BUG_ON idx doesn't match seq control"
3608 " idx=%d, seq_idx=%d, seq=%d\n",
3614 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3615 i, idx, SEQ_TO_SN(sc));
3619 sh = (start - idx) + 0xff;
3620 bitmap = bitmap << sh;
3623 } else if (sh < -64)
3624 sh = 0xff - (start - idx);
3628 bitmap = bitmap << sh;
3631 bitmap |= (1 << sh);
3632 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3633 start, (u32)(bitmap & 0xFFFFFFFF));
3636 agg->bitmap0 = bitmap & 0xFFFFFFFF;
3637 agg->bitmap1 = bitmap >> 32;
3638 agg->start_idx = start;
3639 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3640 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%x\n",
3641 agg->frame_count, agg->start_idx,
3645 agg->wait_for_ba = 1;
3652 static void iwl_rx_reply_tx(struct iwl_priv *priv,
3653 struct iwl_rx_mem_buffer *rxb)
3655 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3656 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3657 int txq_id = SEQ_TO_QUEUE(sequence);
3658 int index = SEQ_TO_INDEX(sequence);
3659 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3660 struct ieee80211_tx_status *tx_status;
3661 struct iwl_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
3662 u32 status = le32_to_cpu(tx_resp->status);
3663 #ifdef CONFIG_IWLWIFI_HT
3664 #ifdef CONFIG_IWLWIFI_HT_AGG
3669 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3670 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3671 "is out of range [0-%d] %d %d\n", txq_id,
3672 index, txq->q.n_bd, txq->q.first_empty,
3677 #ifdef CONFIG_IWLWIFI_HT
3678 #ifdef CONFIG_IWLWIFI_HT_AGG
3679 if (txq->sched_retry) {
3680 const u32 scd_ssn = iwl_get_scd_ssn(tx_resp);
3681 struct ieee80211_hdr *hdr =
3682 iwl_tx_queue_get_hdr(priv, txq_id, index);
3683 struct iwl_ht_agg *agg = NULL;
3684 __le16 *qc = ieee80211_get_qos_ctrl(hdr);
3687 IWL_ERROR("BUG_ON qc is null!!!!\n");
3691 tid = le16_to_cpu(*qc) & 0xf;
3693 sta_id = iwl_get_ra_sta_id(priv, hdr);
3694 if (unlikely(sta_id == IWL_INVALID_STATION)) {
3695 IWL_ERROR("Station not known for\n");
3699 agg = &priv->stations[sta_id].tid[tid].agg;
3701 iwl4965_tx_status_reply_tx(priv, agg, tx_resp, index);
3703 if ((tx_resp->frame_count == 1) &&
3704 !iwl_is_tx_success(status)) {
3705 /* TODO: send BAR */
3708 if ((txq->q.last_used != (scd_ssn & 0xff))) {
3709 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
3710 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3711 "%d index %d\n", scd_ssn , index);
3712 iwl_tx_queue_reclaim(priv, txq_id, index);
3715 #endif /* CONFIG_IWLWIFI_HT_AGG */
3716 #endif /* CONFIG_IWLWIFI_HT */
3717 tx_status = &(txq->txb[txq->q.last_used].status);
3719 tx_status->retry_count = tx_resp->failure_frame;
3720 tx_status->queue_number = status;
3721 tx_status->queue_length = tx_resp->bt_kill_count;
3722 tx_status->queue_length |= tx_resp->failure_rts;
3725 iwl_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
3727 tx_status->control.tx_rate =
3728 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags);
3730 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
3731 "retries %d\n", txq_id, iwl_get_tx_fail_reason(status),
3732 status, le32_to_cpu(tx_resp->rate_n_flags),
3733 tx_resp->failure_frame);
3735 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3737 iwl_tx_queue_reclaim(priv, txq_id, index);
3738 #ifdef CONFIG_IWLWIFI_HT
3739 #ifdef CONFIG_IWLWIFI_HT_AGG
3741 #endif /* CONFIG_IWLWIFI_HT_AGG */
3742 #endif /* CONFIG_IWLWIFI_HT */
3744 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3745 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3749 static void iwl_rx_reply_alive(struct iwl_priv *priv,
3750 struct iwl_rx_mem_buffer *rxb)
3752 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3753 struct iwl_alive_resp *palive;
3754 struct delayed_work *pwork;
3756 palive = &pkt->u.alive_frame;
3758 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3760 palive->is_valid, palive->ver_type,
3761 palive->ver_subtype);
3763 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3764 IWL_DEBUG_INFO("Initialization Alive received.\n");
3765 memcpy(&priv->card_alive_init,
3766 &pkt->u.alive_frame,
3767 sizeof(struct iwl_init_alive_resp));
3768 pwork = &priv->init_alive_start;
3770 IWL_DEBUG_INFO("Runtime Alive received.\n");
3771 memcpy(&priv->card_alive, &pkt->u.alive_frame,
3772 sizeof(struct iwl_alive_resp));
3773 pwork = &priv->alive_start;
3776 /* We delay the ALIVE response by 5ms to
3777 * give the HW RF Kill time to activate... */
3778 if (palive->is_valid == UCODE_VALID_OK)
3779 queue_delayed_work(priv->workqueue, pwork,
3780 msecs_to_jiffies(5));
3782 IWL_WARNING("uCode did not respond OK.\n");
3785 static void iwl_rx_reply_add_sta(struct iwl_priv *priv,
3786 struct iwl_rx_mem_buffer *rxb)
3788 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3790 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3794 static void iwl_rx_reply_error(struct iwl_priv *priv,
3795 struct iwl_rx_mem_buffer *rxb)
3797 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3799 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3800 "seq 0x%04X ser 0x%08X\n",
3801 le32_to_cpu(pkt->u.err_resp.error_type),
3802 get_cmd_string(pkt->u.err_resp.cmd_id),
3803 pkt->u.err_resp.cmd_id,
3804 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3805 le32_to_cpu(pkt->u.err_resp.error_info));
3808 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3810 static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
3812 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3813 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
3814 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
3815 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3816 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3817 rxon->channel = csa->channel;
3818 priv->staging_rxon.channel = csa->channel;
3821 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
3822 struct iwl_rx_mem_buffer *rxb)
3824 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3825 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3826 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
3828 if (!report->state) {
3829 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3830 "Spectrum Measure Notification: Start\n");
3834 memcpy(&priv->measure_report, report, sizeof(*report));
3835 priv->measurement_status |= MEASUREMENT_READY;
3839 static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
3840 struct iwl_rx_mem_buffer *rxb)
3842 #ifdef CONFIG_IWLWIFI_DEBUG
3843 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3844 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
3845 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3846 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3850 static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
3851 struct iwl_rx_mem_buffer *rxb)
3853 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3854 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3855 "notification for %s:\n",
3856 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
3857 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
3860 static void iwl_bg_beacon_update(struct work_struct *work)
3862 struct iwl_priv *priv =
3863 container_of(work, struct iwl_priv, beacon_update);
3864 struct sk_buff *beacon;
3866 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3867 beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
3870 IWL_ERROR("update beacon failed\n");
3874 mutex_lock(&priv->mutex);
3875 /* new beacon skb is allocated every time; dispose previous.*/
3876 if (priv->ibss_beacon)
3877 dev_kfree_skb(priv->ibss_beacon);
3879 priv->ibss_beacon = beacon;
3880 mutex_unlock(&priv->mutex);
3882 iwl_send_beacon_cmd(priv);
3885 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
3886 struct iwl_rx_mem_buffer *rxb)
3888 #ifdef CONFIG_IWLWIFI_DEBUG
3889 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3890 struct iwl_beacon_notif *beacon = &(pkt->u.beacon_status);
3891 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
3893 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3894 "tsf %d %d rate %d\n",
3895 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3896 beacon->beacon_notify_hdr.failure_frame,
3897 le32_to_cpu(beacon->ibss_mgr_status),
3898 le32_to_cpu(beacon->high_tsf),
3899 le32_to_cpu(beacon->low_tsf), rate);
3902 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3903 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3904 queue_work(priv->workqueue, &priv->beacon_update);
3907 /* Service response to REPLY_SCAN_CMD (0x80) */
3908 static void iwl_rx_reply_scan(struct iwl_priv *priv,
3909 struct iwl_rx_mem_buffer *rxb)
3911 #ifdef CONFIG_IWLWIFI_DEBUG
3912 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3913 struct iwl_scanreq_notification *notif =
3914 (struct iwl_scanreq_notification *)pkt->u.raw;
3916 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3920 /* Service SCAN_START_NOTIFICATION (0x82) */
3921 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
3922 struct iwl_rx_mem_buffer *rxb)
3924 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3925 struct iwl_scanstart_notification *notif =
3926 (struct iwl_scanstart_notification *)pkt->u.raw;
3927 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3928 IWL_DEBUG_SCAN("Scan start: "
3930 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3932 notif->band ? "bg" : "a",
3934 notif->tsf_low, notif->status, notif->beacon_timer);
3937 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
3938 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
3939 struct iwl_rx_mem_buffer *rxb)
3941 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3942 struct iwl_scanresults_notification *notif =
3943 (struct iwl_scanresults_notification *)pkt->u.raw;
3945 IWL_DEBUG_SCAN("Scan ch.res: "
3947 "(TSF: 0x%08X:%08X) - %d "
3948 "elapsed=%lu usec (%dms since last)\n",
3950 notif->band ? "bg" : "a",
3951 le32_to_cpu(notif->tsf_high),
3952 le32_to_cpu(notif->tsf_low),
3953 le32_to_cpu(notif->statistics[0]),
3954 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3955 jiffies_to_msecs(elapsed_jiffies
3956 (priv->last_scan_jiffies, jiffies)));
3958 priv->last_scan_jiffies = jiffies;
3961 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
3962 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
3963 struct iwl_rx_mem_buffer *rxb)
3965 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3966 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
3968 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3969 scan_notif->scanned_channels,
3970 scan_notif->tsf_low,
3971 scan_notif->tsf_high, scan_notif->status);
3973 /* The HW is no longer scanning */
3974 clear_bit(STATUS_SCAN_HW, &priv->status);
3976 /* The scan completion notification came in, so kill that timer... */
3977 cancel_delayed_work(&priv->scan_check);
3979 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3980 (priv->scan_bands == 2) ? "2.4" : "5.2",
3981 jiffies_to_msecs(elapsed_jiffies
3982 (priv->scan_pass_start, jiffies)));
3984 /* Remove this scanned band from the list
3985 * of pending bands to scan */
3988 /* If a request to abort was given, or the scan did not succeed
3989 * then we reset the scan state machine and terminate,
3990 * re-queuing another scan if one has been requested */
3991 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3992 IWL_DEBUG_INFO("Aborted scan completed.\n");
3993 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3995 /* If there are more bands on this scan pass reschedule */
3996 if (priv->scan_bands > 0)
4000 priv->last_scan_jiffies = jiffies;
4001 IWL_DEBUG_INFO("Setting scan to off\n");
4003 clear_bit(STATUS_SCANNING, &priv->status);
4005 IWL_DEBUG_INFO("Scan took %dms\n",
4006 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
4008 queue_work(priv->workqueue, &priv->scan_completed);
4013 priv->scan_pass_start = jiffies;
4014 queue_work(priv->workqueue, &priv->request_scan);
4017 /* Handle notification from uCode that card's power state is changing
4018 * due to software, hardware, or critical temperature RFKILL */
4019 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
4020 struct iwl_rx_mem_buffer *rxb)
4022 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
4023 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
4024 unsigned long status = priv->status;
4026 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
4027 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
4028 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
4030 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
4031 RF_CARD_DISABLED)) {
4033 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
4034 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4036 if (!iwl_grab_restricted_access(priv)) {
4037 iwl_write_restricted(
4038 priv, HBUS_TARG_MBX_C,
4039 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4041 iwl_release_restricted_access(priv);
4044 if (!(flags & RXON_CARD_DISABLED)) {
4045 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
4046 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4047 if (!iwl_grab_restricted_access(priv)) {
4048 iwl_write_restricted(
4049 priv, HBUS_TARG_MBX_C,
4050 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4052 iwl_release_restricted_access(priv);
4056 if (flags & RF_CARD_DISABLED) {
4057 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
4058 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
4059 iwl_read32(priv, CSR_UCODE_DRV_GP1);
4060 if (!iwl_grab_restricted_access(priv))
4061 iwl_release_restricted_access(priv);
4065 if (flags & HW_CARD_DISABLED)
4066 set_bit(STATUS_RF_KILL_HW, &priv->status);
4068 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4071 if (flags & SW_CARD_DISABLED)
4072 set_bit(STATUS_RF_KILL_SW, &priv->status);
4074 clear_bit(STATUS_RF_KILL_SW, &priv->status);
4076 if (!(flags & RXON_CARD_DISABLED))
4077 iwl_scan_cancel(priv);
4079 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
4080 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
4081 (test_bit(STATUS_RF_KILL_SW, &status) !=
4082 test_bit(STATUS_RF_KILL_SW, &priv->status)))
4083 queue_work(priv->workqueue, &priv->rf_kill);
4085 wake_up_interruptible(&priv->wait_command_queue);
4089 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
4091 * Setup the RX handlers for each of the reply types sent from the uCode
4094 * This function chains into the hardware specific files for them to setup
4095 * any hardware specific handlers as well.
4097 static void iwl_setup_rx_handlers(struct iwl_priv *priv)
4099 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
4100 priv->rx_handlers[REPLY_ADD_STA] = iwl_rx_reply_add_sta;
4101 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
4102 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
4103 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
4104 iwl_rx_spectrum_measure_notif;
4105 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
4106 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
4107 iwl_rx_pm_debug_statistics_notif;
4108 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
4110 /* NOTE: iwl_rx_statistics is different based on whether
4111 * the build is for the 3945 or the 4965. See the
4112 * corresponding implementation in iwl-XXXX.c
4114 * The same handler is used for both the REPLY to a
4115 * discrete statistics request from the host as well as
4116 * for the periodic statistics notification from the uCode
4118 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_hw_rx_statistics;
4119 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_hw_rx_statistics;
4121 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
4122 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
4123 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
4124 iwl_rx_scan_results_notif;
4125 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
4126 iwl_rx_scan_complete_notif;
4127 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
4128 priv->rx_handlers[REPLY_TX] = iwl_rx_reply_tx;
4130 /* Setup hardware specific Rx handlers */
4131 iwl_hw_rx_handler_setup(priv);
4135 * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
4136 * @rxb: Rx buffer to reclaim
4138 * If an Rx buffer has an async callback associated with it the callback
4139 * will be executed. The attached skb (if present) will only be freed
4140 * if the callback returns 1
4142 static void iwl_tx_cmd_complete(struct iwl_priv *priv,
4143 struct iwl_rx_mem_buffer *rxb)
4145 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
4146 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
4147 int txq_id = SEQ_TO_QUEUE(sequence);
4148 int index = SEQ_TO_INDEX(sequence);
4149 int huge = sequence & SEQ_HUGE_FRAME;
4151 struct iwl_cmd *cmd;
4153 /* If a Tx command is being handled and it isn't in the actual
4154 * command queue then there a command routing bug has been introduced
4155 * in the queue management code. */
4156 if (txq_id != IWL_CMD_QUEUE_NUM)
4157 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
4158 txq_id, pkt->hdr.cmd);
4159 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
4161 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
4162 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
4164 /* Input error checking is done when commands are added to queue. */
4165 if (cmd->meta.flags & CMD_WANT_SKB) {
4166 cmd->meta.source->u.skb = rxb->skb;
4168 } else if (cmd->meta.u.callback &&
4169 !cmd->meta.u.callback(priv, cmd, rxb->skb))
4172 iwl_tx_queue_reclaim(priv, txq_id, index);
4174 if (!(cmd->meta.flags & CMD_ASYNC)) {
4175 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4176 wake_up_interruptible(&priv->wait_command_queue);
4180 /************************** RX-FUNCTIONS ****************************/
4182 * Rx theory of operation
4184 * The host allocates 32 DMA target addresses and passes the host address
4185 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
4189 * The host/firmware share two index registers for managing the Rx buffers.
4191 * The READ index maps to the first position that the firmware may be writing
4192 * to -- the driver can read up to (but not including) this position and get
4194 * The READ index is managed by the firmware once the card is enabled.
4196 * The WRITE index maps to the last position the driver has read from -- the
4197 * position preceding WRITE is the last slot the firmware can place a packet.
4199 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
4202 * During initialization the host sets up the READ queue position to the first
4203 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4205 * When the firmware places a packet in a buffer it will advance the READ index
4206 * and fire the RX interrupt. The driver can then query the READ index and
4207 * process as many packets as possible, moving the WRITE index forward as it
4208 * resets the Rx queue buffers with new memory.
4210 * The management in the driver is as follows:
4211 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
4212 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
4213 * to replensish the iwl->rxq->rx_free.
4214 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
4215 * iwl->rxq is replenished and the READ INDEX is updated (updating the
4216 * 'processed' and 'read' driver indexes as well)
4217 * + A received packet is processed and handed to the kernel network stack,
4218 * detached from the iwl->rxq. The driver 'processed' index is updated.
4219 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
4220 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
4221 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
4222 * were enough free buffers and RX_STALLED is set it is cleared.
4227 * iwl_rx_queue_alloc() Allocates rx_free
4228 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
4229 * iwl_rx_queue_restock
4230 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
4231 * queue, updates firmware pointers, and updates
4232 * the WRITE index. If insufficient rx_free buffers
4233 * are available, schedules iwl_rx_replenish
4235 * -- enable interrupts --
4236 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
4237 * READ INDEX, detaching the SKB from the pool.
4238 * Moves the packet buffer from queue to rx_used.
4239 * Calls iwl_rx_queue_restock to refill any empty
4246 * iwl_rx_queue_space - Return number of free slots available in queue.
4248 static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
4250 int s = q->read - q->write;
4253 /* keep some buffer to not confuse full and empty queue */
4261 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
4263 * NOTE: This function has 3945 and 4965 specific code sections
4264 * but is declared in base due to the majority of the
4265 * implementation being the same (only a numeric constant is
4269 int iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
4273 unsigned long flags;
4275 spin_lock_irqsave(&q->lock, flags);
4277 if (q->need_update == 0)
4280 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4281 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4283 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4284 iwl_set_bit(priv, CSR_GP_CNTRL,
4285 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4289 rc = iwl_grab_restricted_access(priv);
4293 iwl_write_restricted(priv, FH_RSCSR_CHNL0_WPTR,
4295 iwl_release_restricted_access(priv);
4297 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
4303 spin_unlock_irqrestore(&q->lock, flags);
4308 * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer pointer.
4310 * NOTE: This function has 3945 and 4965 specific code paths in it.
4312 static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
4313 dma_addr_t dma_addr)
4315 return cpu_to_le32((u32)(dma_addr >> 8));
4320 * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
4322 * If there are slots in the RX queue that need to be restocked,
4323 * and we have free pre-allocated buffers, fill the ranks as much
4324 * as we can pulling from rx_free.
4326 * This moves the 'write' index forward to catch up with 'processed', and
4327 * also updates the memory address in the firmware to reference the new
4330 int iwl_rx_queue_restock(struct iwl_priv *priv)
4332 struct iwl_rx_queue *rxq = &priv->rxq;
4333 struct list_head *element;
4334 struct iwl_rx_mem_buffer *rxb;
4335 unsigned long flags;
4338 spin_lock_irqsave(&rxq->lock, flags);
4339 write = rxq->write & ~0x7;
4340 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
4341 element = rxq->rx_free.next;
4342 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4344 rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->dma_addr);
4345 rxq->queue[rxq->write] = rxb;
4346 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4349 spin_unlock_irqrestore(&rxq->lock, flags);
4350 /* If the pre-allocated buffer pool is dropping low, schedule to
4352 if (rxq->free_count <= RX_LOW_WATERMARK)
4353 queue_work(priv->workqueue, &priv->rx_replenish);
4356 /* If we've added more space for the firmware to place data, tell it */
4357 if ((write != (rxq->write & ~0x7))
4358 || (abs(rxq->write - rxq->read) > 7)) {
4359 spin_lock_irqsave(&rxq->lock, flags);
4360 rxq->need_update = 1;
4361 spin_unlock_irqrestore(&rxq->lock, flags);
4362 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
4371 * iwl_rx_replensih - Move all used packet from rx_used to rx_free
4373 * When moving to rx_free an SKB is allocated for the slot.
4375 * Also restock the Rx queue via iwl_rx_queue_restock.
4376 * This is called as a scheduled work item (except for during intialization)
4378 void iwl_rx_replenish(void *data)
4380 struct iwl_priv *priv = data;
4381 struct iwl_rx_queue *rxq = &priv->rxq;
4382 struct list_head *element;
4383 struct iwl_rx_mem_buffer *rxb;
4384 unsigned long flags;
4385 spin_lock_irqsave(&rxq->lock, flags);
4386 while (!list_empty(&rxq->rx_used)) {
4387 element = rxq->rx_used.next;
4388 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4390 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
4392 if (net_ratelimit())
4393 printk(KERN_CRIT DRV_NAME
4394 ": Can not allocate SKB buffers\n");
4395 /* We don't reschedule replenish work here -- we will
4396 * call the restock method and if it still needs
4397 * more buffers it will schedule replenish */
4400 priv->alloc_rxb_skb++;
4403 pci_map_single(priv->pci_dev, rxb->skb->data,
4404 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4405 list_add_tail(&rxb->list, &rxq->rx_free);
4408 spin_unlock_irqrestore(&rxq->lock, flags);
4410 spin_lock_irqsave(&priv->lock, flags);
4411 iwl_rx_queue_restock(priv);
4412 spin_unlock_irqrestore(&priv->lock, flags);
4415 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
4416 * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
4417 * This free routine walks the list of POOL entries and if SKB is set to
4418 * non NULL it is unmapped and freed
4420 void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4423 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4424 if (rxq->pool[i].skb != NULL) {
4425 pci_unmap_single(priv->pci_dev,
4426 rxq->pool[i].dma_addr,
4427 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4428 dev_kfree_skb(rxq->pool[i].skb);
4432 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4437 int iwl_rx_queue_alloc(struct iwl_priv *priv)
4439 struct iwl_rx_queue *rxq = &priv->rxq;
4440 struct pci_dev *dev = priv->pci_dev;
4443 spin_lock_init(&rxq->lock);
4444 INIT_LIST_HEAD(&rxq->rx_free);
4445 INIT_LIST_HEAD(&rxq->rx_used);
4446 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4449 /* Fill the rx_used queue with _all_ of the Rx buffers */
4450 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4451 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4452 /* Set us so that we have processed and used all buffers, but have
4453 * not restocked the Rx queue with fresh buffers */
4454 rxq->read = rxq->write = 0;
4455 rxq->free_count = 0;
4456 rxq->need_update = 0;
4460 void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4462 unsigned long flags;
4464 spin_lock_irqsave(&rxq->lock, flags);
4465 INIT_LIST_HEAD(&rxq->rx_free);
4466 INIT_LIST_HEAD(&rxq->rx_used);
4467 /* Fill the rx_used queue with _all_ of the Rx buffers */
4468 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4469 /* In the reset function, these buffers may have been allocated
4470 * to an SKB, so we need to unmap and free potential storage */
4471 if (rxq->pool[i].skb != NULL) {
4472 pci_unmap_single(priv->pci_dev,
4473 rxq->pool[i].dma_addr,
4474 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4475 priv->alloc_rxb_skb--;
4476 dev_kfree_skb(rxq->pool[i].skb);
4477 rxq->pool[i].skb = NULL;
4479 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4482 /* Set us so that we have processed and used all buffers, but have
4483 * not restocked the Rx queue with fresh buffers */
4484 rxq->read = rxq->write = 0;
4485 rxq->free_count = 0;
4486 spin_unlock_irqrestore(&rxq->lock, flags);
4489 /* Convert linear signal-to-noise ratio into dB */
4490 static u8 ratio2dB[100] = {
4491 /* 0 1 2 3 4 5 6 7 8 9 */
4492 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4493 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4494 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4495 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4496 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4497 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4498 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4499 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4500 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4501 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4504 /* Calculates a relative dB value from a ratio of linear
4505 * (i.e. not dB) signal levels.
4506 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
4507 int iwl_calc_db_from_ratio(int sig_ratio)
4509 /* 1000:1 or higher just report as 60 dB */
4510 if (sig_ratio >= 1000)
4513 /* 100:1 or higher, divide by 10 and use table,
4514 * add 20 dB to make up for divide by 10 */
4515 if (sig_ratio >= 100)
4516 return (20 + (int)ratio2dB[sig_ratio/10]);
4518 /* We shouldn't see this */
4522 /* Use table for ratios 1:1 - 99:1 */
4523 return (int)ratio2dB[sig_ratio];
4526 #define PERFECT_RSSI (-20) /* dBm */
4527 #define WORST_RSSI (-95) /* dBm */
4528 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4530 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
4531 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4532 * about formulas used below. */
4533 int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
4536 int degradation = PERFECT_RSSI - rssi_dbm;
4538 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4539 * as indicator; formula is (signal dbm - noise dbm).
4540 * SNR at or above 40 is a great signal (100%).
4541 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4542 * Weakest usable signal is usually 10 - 15 dB SNR. */
4544 if (rssi_dbm - noise_dbm >= 40)
4546 else if (rssi_dbm < noise_dbm)
4548 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4550 /* Else use just the signal level.
4551 * This formula is a least squares fit of data points collected and
4552 * compared with a reference system that had a percentage (%) display
4553 * for signal quality. */
4555 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4556 (15 * RSSI_RANGE + 62 * degradation)) /
4557 (RSSI_RANGE * RSSI_RANGE);
4561 else if (sig_qual < 1)
4568 * iwl_rx_handle - Main entry function for receiving responses from the uCode
4570 * Uses the priv->rx_handlers callback function array to invoke
4571 * the appropriate handlers, including command responses,
4572 * frame-received notifications, and other notifications.
4574 static void iwl_rx_handle(struct iwl_priv *priv)
4576 struct iwl_rx_mem_buffer *rxb;
4577 struct iwl_rx_packet *pkt;
4578 struct iwl_rx_queue *rxq = &priv->rxq;
4581 unsigned long flags;
4583 r = iwl_hw_get_rx_read(priv);
4586 /* Rx interrupt, but nothing sent from uCode */
4588 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4591 rxb = rxq->queue[i];
4593 /* If an RXB doesn't have a queue slot associated with it
4594 * then a bug has been introduced in the queue refilling
4595 * routines -- catch it here */
4596 BUG_ON(rxb == NULL);
4598 rxq->queue[i] = NULL;
4600 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4602 PCI_DMA_FROMDEVICE);
4603 pkt = (struct iwl_rx_packet *)rxb->skb->data;
4605 /* Reclaim a command buffer only if this packet is a response
4606 * to a (driver-originated) command.
4607 * If the packet (e.g. Rx frame) originated from uCode,
4608 * there is no command buffer to reclaim.
4609 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4610 * but apparently a few don't get set; catch them here. */
4611 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4612 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4613 (pkt->hdr.cmd != REPLY_4965_RX) &&
4614 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
4615 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4616 (pkt->hdr.cmd != REPLY_TX);
4618 /* Based on type of command response or notification,
4619 * handle those that need handling via function in
4620 * rx_handlers table. See iwl_setup_rx_handlers() */
4621 if (priv->rx_handlers[pkt->hdr.cmd]) {
4622 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4623 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4624 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4625 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4627 /* No handling needed */
4628 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4629 "r %d i %d No handler needed for %s, 0x%02x\n",
4630 r, i, get_cmd_string(pkt->hdr.cmd),
4635 /* Invoke any callbacks, transfer the skb to caller,
4636 * and fire off the (possibly) blocking iwl_send_cmd()
4637 * as we reclaim the driver command queue */
4638 if (rxb && rxb->skb)
4639 iwl_tx_cmd_complete(priv, rxb);
4641 IWL_WARNING("Claim null rxb?\n");
4644 /* For now we just don't re-use anything. We can tweak this
4645 * later to try and re-use notification packets and SKBs that
4646 * fail to Rx correctly */
4647 if (rxb->skb != NULL) {
4648 priv->alloc_rxb_skb--;
4649 dev_kfree_skb_any(rxb->skb);
4653 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4654 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4655 spin_lock_irqsave(&rxq->lock, flags);
4656 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4657 spin_unlock_irqrestore(&rxq->lock, flags);
4658 i = (i + 1) & RX_QUEUE_MASK;
4661 /* Backtrack one entry */
4663 iwl_rx_queue_restock(priv);
4666 int iwl_tx_queue_update_write_ptr(struct iwl_priv *priv,
4667 struct iwl_tx_queue *txq)
4671 int txq_id = txq->q.id;
4673 if (txq->need_update == 0)
4676 /* if we're trying to save power */
4677 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4678 /* wake up nic if it's powered down ...
4679 * uCode will wake up, and interrupt us again, so next
4680 * time we'll skip this part. */
4681 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4683 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4684 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
4685 iwl_set_bit(priv, CSR_GP_CNTRL,
4686 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4690 /* restore this queue's parameters in nic hardware. */
4691 rc = iwl_grab_restricted_access(priv);
4694 iwl_write_restricted(priv, HBUS_TARG_WRPTR,
4695 txq->q.first_empty | (txq_id << 8));
4696 iwl_release_restricted_access(priv);
4698 /* else not in power-save mode, uCode will never sleep when we're
4699 * trying to tx (during RFKILL, we're not trying to tx). */
4701 iwl_write32(priv, HBUS_TARG_WRPTR,
4702 txq->q.first_empty | (txq_id << 8));
4704 txq->need_update = 0;
4709 #ifdef CONFIG_IWLWIFI_DEBUG
4710 static void iwl_print_rx_config_cmd(struct iwl_rxon_cmd *rxon)
4712 DECLARE_MAC_BUF(mac);
4714 IWL_DEBUG_RADIO("RX CONFIG:\n");
4715 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
4716 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4717 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4718 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4719 le32_to_cpu(rxon->filter_flags));
4720 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4721 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4722 rxon->ofdm_basic_rates);
4723 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
4724 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4725 print_mac(mac, rxon->node_addr));
4726 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4727 print_mac(mac, rxon->bssid_addr));
4728 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4732 static void iwl_enable_interrupts(struct iwl_priv *priv)
4734 IWL_DEBUG_ISR("Enabling interrupts\n");
4735 set_bit(STATUS_INT_ENABLED, &priv->status);
4736 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
4739 static inline void iwl_disable_interrupts(struct iwl_priv *priv)
4741 clear_bit(STATUS_INT_ENABLED, &priv->status);
4743 /* disable interrupts from uCode/NIC to host */
4744 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
4746 /* acknowledge/clear/reset any interrupts still pending
4747 * from uCode or flow handler (Rx/Tx DMA) */
4748 iwl_write32(priv, CSR_INT, 0xffffffff);
4749 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
4750 IWL_DEBUG_ISR("Disabled interrupts\n");
4753 static const char *desc_lookup(int i)
4761 return "BAD_CHECKSUM";
4763 return "NMI_INTERRUPT";
4767 return "FATAL_ERROR";
4773 #define ERROR_START_OFFSET (1 * sizeof(u32))
4774 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
4776 static void iwl_dump_nic_error_log(struct iwl_priv *priv)
4779 u32 desc, time, count, base, data1;
4780 u32 blink1, blink2, ilink1, ilink2;
4783 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4785 if (!iwl_hw_valid_rtc_data_addr(base)) {
4786 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4790 rc = iwl_grab_restricted_access(priv);
4792 IWL_WARNING("Can not read from adapter at this time.\n");
4796 count = iwl_read_restricted_mem(priv, base);
4798 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4799 IWL_ERROR("Start IWL Error Log Dump:\n");
4800 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4801 priv->status, priv->config, count);
4804 desc = iwl_read_restricted_mem(priv, base + 1 * sizeof(u32));
4805 blink1 = iwl_read_restricted_mem(priv, base + 3 * sizeof(u32));
4806 blink2 = iwl_read_restricted_mem(priv, base + 4 * sizeof(u32));
4807 ilink1 = iwl_read_restricted_mem(priv, base + 5 * sizeof(u32));
4808 ilink2 = iwl_read_restricted_mem(priv, base + 6 * sizeof(u32));
4809 data1 = iwl_read_restricted_mem(priv, base + 7 * sizeof(u32));
4810 data2 = iwl_read_restricted_mem(priv, base + 8 * sizeof(u32));
4811 line = iwl_read_restricted_mem(priv, base + 9 * sizeof(u32));
4812 time = iwl_read_restricted_mem(priv, base + 11 * sizeof(u32));
4814 IWL_ERROR("Desc Time "
4815 "data1 data2 line\n");
4816 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4817 desc_lookup(desc), desc, time, data1, data2, line);
4818 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4819 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4822 iwl_release_restricted_access(priv);
4825 #define EVENT_START_OFFSET (4 * sizeof(u32))
4828 * iwl_print_event_log - Dump error event log to syslog
4830 * NOTE: Must be called with iwl_grab_restricted_access() already obtained!
4832 static void iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
4833 u32 num_events, u32 mode)
4836 u32 base; /* SRAM byte address of event log header */
4837 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4838 u32 ptr; /* SRAM byte address of log data */
4839 u32 ev, time, data; /* event log data */
4841 if (num_events == 0)
4844 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4847 event_size = 2 * sizeof(u32);
4849 event_size = 3 * sizeof(u32);
4851 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4853 /* "time" is actually "data" for mode 0 (no timestamp).
4854 * place event id # at far right for easier visual parsing. */
4855 for (i = 0; i < num_events; i++) {
4856 ev = iwl_read_restricted_mem(priv, ptr);
4858 time = iwl_read_restricted_mem(priv, ptr);
4861 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4863 data = iwl_read_restricted_mem(priv, ptr);
4865 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4870 static void iwl_dump_nic_event_log(struct iwl_priv *priv)
4873 u32 base; /* SRAM byte address of event log header */
4874 u32 capacity; /* event log capacity in # entries */
4875 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4876 u32 num_wraps; /* # times uCode wrapped to top of log */
4877 u32 next_entry; /* index of next entry to be written by uCode */
4878 u32 size; /* # entries that we'll print */
4880 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4881 if (!iwl_hw_valid_rtc_data_addr(base)) {
4882 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4886 rc = iwl_grab_restricted_access(priv);
4888 IWL_WARNING("Can not read from adapter at this time.\n");
4892 /* event log header */
4893 capacity = iwl_read_restricted_mem(priv, base);
4894 mode = iwl_read_restricted_mem(priv, base + (1 * sizeof(u32)));
4895 num_wraps = iwl_read_restricted_mem(priv, base + (2 * sizeof(u32)));
4896 next_entry = iwl_read_restricted_mem(priv, base + (3 * sizeof(u32)));
4898 size = num_wraps ? capacity : next_entry;
4900 /* bail out if nothing in log */
4902 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
4903 iwl_release_restricted_access(priv);
4907 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
4910 /* if uCode has wrapped back to top of log, start at the oldest entry,
4911 * i.e the next one that uCode would fill. */
4913 iwl_print_event_log(priv, next_entry,
4914 capacity - next_entry, mode);
4916 /* (then/else) start at top of log */
4917 iwl_print_event_log(priv, 0, next_entry, mode);
4919 iwl_release_restricted_access(priv);
4923 * iwl_irq_handle_error - called for HW or SW error interrupt from card
4925 static void iwl_irq_handle_error(struct iwl_priv *priv)
4927 /* Set the FW error flag -- cleared on iwl_down */
4928 set_bit(STATUS_FW_ERROR, &priv->status);
4930 /* Cancel currently queued command. */
4931 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4933 #ifdef CONFIG_IWLWIFI_DEBUG
4934 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
4935 iwl_dump_nic_error_log(priv);
4936 iwl_dump_nic_event_log(priv);
4937 iwl_print_rx_config_cmd(&priv->staging_rxon);
4941 wake_up_interruptible(&priv->wait_command_queue);
4943 /* Keep the restart process from trying to send host
4944 * commands by clearing the INIT status bit */
4945 clear_bit(STATUS_READY, &priv->status);
4947 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4948 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4949 "Restarting adapter due to uCode error.\n");
4951 if (iwl_is_associated(priv)) {
4952 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4953 sizeof(priv->recovery_rxon));
4954 priv->error_recovering = 1;
4956 queue_work(priv->workqueue, &priv->restart);
4960 static void iwl_error_recovery(struct iwl_priv *priv)
4962 unsigned long flags;
4964 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4965 sizeof(priv->staging_rxon));
4966 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4967 iwl_commit_rxon(priv);
4969 iwl_rxon_add_station(priv, priv->bssid, 1);
4971 spin_lock_irqsave(&priv->lock, flags);
4972 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4973 priv->error_recovering = 0;
4974 spin_unlock_irqrestore(&priv->lock, flags);
4977 static void iwl_irq_tasklet(struct iwl_priv *priv)
4979 u32 inta, handled = 0;
4981 unsigned long flags;
4982 #ifdef CONFIG_IWLWIFI_DEBUG
4986 spin_lock_irqsave(&priv->lock, flags);
4988 /* Ack/clear/reset pending uCode interrupts.
4989 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4990 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
4991 inta = iwl_read32(priv, CSR_INT);
4992 iwl_write32(priv, CSR_INT, inta);
4994 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4995 * Any new interrupts that happen after this, either while we're
4996 * in this tasklet, or later, will show up in next ISR/tasklet. */
4997 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4998 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
5000 #ifdef CONFIG_IWLWIFI_DEBUG
5001 if (iwl_debug_level & IWL_DL_ISR) {
5002 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
5003 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5004 inta, inta_mask, inta_fh);
5008 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
5009 * atomic, make sure that inta covers all the interrupts that
5010 * we've discovered, even if FH interrupt came in just after
5011 * reading CSR_INT. */
5012 if (inta_fh & CSR_FH_INT_RX_MASK)
5013 inta |= CSR_INT_BIT_FH_RX;
5014 if (inta_fh & CSR_FH_INT_TX_MASK)
5015 inta |= CSR_INT_BIT_FH_TX;
5017 /* Now service all interrupt bits discovered above. */
5018 if (inta & CSR_INT_BIT_HW_ERR) {
5019 IWL_ERROR("Microcode HW error detected. Restarting.\n");
5021 /* Tell the device to stop sending interrupts */
5022 iwl_disable_interrupts(priv);
5024 iwl_irq_handle_error(priv);
5026 handled |= CSR_INT_BIT_HW_ERR;
5028 spin_unlock_irqrestore(&priv->lock, flags);
5033 #ifdef CONFIG_IWLWIFI_DEBUG
5034 if (iwl_debug_level & (IWL_DL_ISR)) {
5035 /* NIC fires this, but we don't use it, redundant with WAKEUP */
5036 if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
5037 IWL_DEBUG_ISR("Microcode started or stopped.\n");
5039 /* Alive notification via Rx interrupt will do the real work */
5040 if (inta & CSR_INT_BIT_ALIVE)
5041 IWL_DEBUG_ISR("Alive interrupt\n");
5044 /* Safely ignore these bits for debug checks below */
5045 inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
5047 /* HW RF KILL switch toggled (4965 only) */
5048 if (inta & CSR_INT_BIT_RF_KILL) {
5050 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
5051 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
5054 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
5055 "RF_KILL bit toggled to %s.\n",
5056 hw_rf_kill ? "disable radio":"enable radio");
5058 /* Queue restart only if RF_KILL switch was set to "kill"
5059 * when we loaded driver, and is now set to "enable".
5060 * After we're Alive, RF_KILL gets handled by
5061 * iwl_rx_card_state_notif() */
5062 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status))
5063 queue_work(priv->workqueue, &priv->restart);
5065 handled |= CSR_INT_BIT_RF_KILL;
5068 /* Chip got too hot and stopped itself (4965 only) */
5069 if (inta & CSR_INT_BIT_CT_KILL) {
5070 IWL_ERROR("Microcode CT kill error detected.\n");
5071 handled |= CSR_INT_BIT_CT_KILL;
5074 /* Error detected by uCode */
5075 if (inta & CSR_INT_BIT_SW_ERR) {
5076 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
5078 iwl_irq_handle_error(priv);
5079 handled |= CSR_INT_BIT_SW_ERR;
5082 /* uCode wakes up after power-down sleep */
5083 if (inta & CSR_INT_BIT_WAKEUP) {
5084 IWL_DEBUG_ISR("Wakeup interrupt\n");
5085 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
5086 iwl_tx_queue_update_write_ptr(priv, &priv->txq[0]);
5087 iwl_tx_queue_update_write_ptr(priv, &priv->txq[1]);
5088 iwl_tx_queue_update_write_ptr(priv, &priv->txq[2]);
5089 iwl_tx_queue_update_write_ptr(priv, &priv->txq[3]);
5090 iwl_tx_queue_update_write_ptr(priv, &priv->txq[4]);
5091 iwl_tx_queue_update_write_ptr(priv, &priv->txq[5]);
5093 handled |= CSR_INT_BIT_WAKEUP;
5096 /* All uCode command responses, including Tx command responses,
5097 * Rx "responses" (frame-received notification), and other
5098 * notifications from uCode come through here*/
5099 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
5100 iwl_rx_handle(priv);
5101 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
5104 if (inta & CSR_INT_BIT_FH_TX) {
5105 IWL_DEBUG_ISR("Tx interrupt\n");
5106 handled |= CSR_INT_BIT_FH_TX;
5109 if (inta & ~handled)
5110 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
5112 if (inta & ~CSR_INI_SET_MASK) {
5113 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
5114 inta & ~CSR_INI_SET_MASK);
5115 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
5118 /* Re-enable all interrupts */
5119 iwl_enable_interrupts(priv);
5121 #ifdef CONFIG_IWLWIFI_DEBUG
5122 if (iwl_debug_level & (IWL_DL_ISR)) {
5123 inta = iwl_read32(priv, CSR_INT);
5124 inta_mask = iwl_read32(priv, CSR_INT_MASK);
5125 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
5126 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
5127 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
5130 spin_unlock_irqrestore(&priv->lock, flags);
5133 static irqreturn_t iwl_isr(int irq, void *data)
5135 struct iwl_priv *priv = data;
5136 u32 inta, inta_mask;
5141 spin_lock(&priv->lock);
5143 /* Disable (but don't clear!) interrupts here to avoid
5144 * back-to-back ISRs and sporadic interrupts from our NIC.
5145 * If we have something to service, the tasklet will re-enable ints.
5146 * If we *don't* have something, we'll re-enable before leaving here. */
5147 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
5148 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
5150 /* Discover which interrupts are active/pending */
5151 inta = iwl_read32(priv, CSR_INT);
5152 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
5154 /* Ignore interrupt if there's nothing in NIC to service.
5155 * This may be due to IRQ shared with another device,
5156 * or due to sporadic interrupts thrown from our NIC. */
5157 if (!inta && !inta_fh) {
5158 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
5162 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
5163 /* Hardware disappeared. It might have already raised
5165 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
5169 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5170 inta, inta_mask, inta_fh);
5172 /* iwl_irq_tasklet() will service interrupts and re-enable them */
5173 tasklet_schedule(&priv->irq_tasklet);
5176 spin_unlock(&priv->lock);
5180 /* re-enable interrupts here since we don't have anything to service. */
5181 iwl_enable_interrupts(priv);
5182 spin_unlock(&priv->lock);
5186 /************************** EEPROM BANDS ****************************
5188 * The iwl_eeprom_band definitions below provide the mapping from the
5189 * EEPROM contents to the specific channel number supported for each
5192 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
5193 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5194 * The specific geography and calibration information for that channel
5195 * is contained in the eeprom map itself.
5197 * During init, we copy the eeprom information and channel map
5198 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5200 * channel_map_24/52 provides the index in the channel_info array for a
5201 * given channel. We have to have two separate maps as there is channel
5202 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5205 * A value of 0xff stored in the channel_map indicates that the channel
5206 * is not supported by the hardware at all.
5208 * A value of 0xfe in the channel_map indicates that the channel is not
5209 * valid for Tx with the current hardware. This means that
5210 * while the system can tune and receive on a given channel, it may not
5211 * be able to associate or transmit any frames on that
5212 * channel. There is no corresponding channel information for that
5215 *********************************************************************/
5218 static const u8 iwl_eeprom_band_1[14] = {
5219 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5223 static const u8 iwl_eeprom_band_2[] = {
5224 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5227 static const u8 iwl_eeprom_band_3[] = { /* 5205-5320MHz */
5228 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5231 static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
5232 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5235 static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
5236 145, 149, 153, 157, 161, 165
5239 static u8 iwl_eeprom_band_6[] = { /* 2.4 FAT channel */
5243 static u8 iwl_eeprom_band_7[] = { /* 5.2 FAT channel */
5244 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5247 static void iwl_init_band_reference(const struct iwl_priv *priv, int band,
5248 int *eeprom_ch_count,
5249 const struct iwl_eeprom_channel
5251 const u8 **eeprom_ch_index)
5254 case 1: /* 2.4GHz band */
5255 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
5256 *eeprom_ch_info = priv->eeprom.band_1_channels;
5257 *eeprom_ch_index = iwl_eeprom_band_1;
5259 case 2: /* 5.2GHz band */
5260 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
5261 *eeprom_ch_info = priv->eeprom.band_2_channels;
5262 *eeprom_ch_index = iwl_eeprom_band_2;
5264 case 3: /* 5.2GHz band */
5265 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
5266 *eeprom_ch_info = priv->eeprom.band_3_channels;
5267 *eeprom_ch_index = iwl_eeprom_band_3;
5269 case 4: /* 5.2GHz band */
5270 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
5271 *eeprom_ch_info = priv->eeprom.band_4_channels;
5272 *eeprom_ch_index = iwl_eeprom_band_4;
5274 case 5: /* 5.2GHz band */
5275 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
5276 *eeprom_ch_info = priv->eeprom.band_5_channels;
5277 *eeprom_ch_index = iwl_eeprom_band_5;
5280 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_6);
5281 *eeprom_ch_info = priv->eeprom.band_24_channels;
5282 *eeprom_ch_index = iwl_eeprom_band_6;
5285 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_7);
5286 *eeprom_ch_info = priv->eeprom.band_52_channels;
5287 *eeprom_ch_index = iwl_eeprom_band_7;
5295 const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
5296 int phymode, u16 channel)
5301 case MODE_IEEE80211A:
5302 for (i = 14; i < priv->channel_count; i++) {
5303 if (priv->channel_info[i].channel == channel)
5304 return &priv->channel_info[i];
5308 case MODE_IEEE80211B:
5309 case MODE_IEEE80211G:
5310 if (channel >= 1 && channel <= 14)
5311 return &priv->channel_info[channel - 1];
5319 #define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5322 static int iwl_init_channel_map(struct iwl_priv *priv)
5324 int eeprom_ch_count = 0;
5325 const u8 *eeprom_ch_index = NULL;
5326 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
5328 struct iwl_channel_info *ch_info;
5330 if (priv->channel_count) {
5331 IWL_DEBUG_INFO("Channel map already initialized.\n");
5335 if (priv->eeprom.version < 0x2f) {
5336 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5337 priv->eeprom.version);
5341 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5343 priv->channel_count =
5344 ARRAY_SIZE(iwl_eeprom_band_1) +
5345 ARRAY_SIZE(iwl_eeprom_band_2) +
5346 ARRAY_SIZE(iwl_eeprom_band_3) +
5347 ARRAY_SIZE(iwl_eeprom_band_4) +
5348 ARRAY_SIZE(iwl_eeprom_band_5);
5350 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5352 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
5353 priv->channel_count, GFP_KERNEL);
5354 if (!priv->channel_info) {
5355 IWL_ERROR("Could not allocate channel_info\n");
5356 priv->channel_count = 0;
5360 ch_info = priv->channel_info;
5362 /* Loop through the 5 EEPROM bands adding them in order to the
5363 * channel map we maintain (that contains additional information than
5364 * what just in the EEPROM) */
5365 for (band = 1; band <= 5; band++) {
5367 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5368 &eeprom_ch_info, &eeprom_ch_index);
5370 /* Loop through each band adding each of the channels */
5371 for (ch = 0; ch < eeprom_ch_count; ch++) {
5372 ch_info->channel = eeprom_ch_index[ch];
5373 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5376 /* permanently store EEPROM's channel regulatory flags
5377 * and max power in channel info database. */
5378 ch_info->eeprom = eeprom_ch_info[ch];
5380 /* Copy the run-time flags so they are there even on
5381 * invalid channels */
5382 ch_info->flags = eeprom_ch_info[ch].flags;
5384 if (!(is_channel_valid(ch_info))) {
5385 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5389 is_channel_a_band(ch_info) ?
5395 /* Initialize regulatory-based run-time data */
5396 ch_info->max_power_avg = ch_info->curr_txpow =
5397 eeprom_ch_info[ch].max_power_avg;
5398 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5399 ch_info->min_power = 0;
5401 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5402 " %ddBm): Ad-Hoc %ssupported\n",
5404 is_channel_a_band(ch_info) ?
5406 CHECK_AND_PRINT(IBSS),
5407 CHECK_AND_PRINT(ACTIVE),
5408 CHECK_AND_PRINT(RADAR),
5409 CHECK_AND_PRINT(WIDE),
5410 CHECK_AND_PRINT(NARROW),
5411 CHECK_AND_PRINT(DFS),
5412 eeprom_ch_info[ch].flags,
5413 eeprom_ch_info[ch].max_power_avg,
5414 ((eeprom_ch_info[ch].
5415 flags & EEPROM_CHANNEL_IBSS)
5416 && !(eeprom_ch_info[ch].
5417 flags & EEPROM_CHANNEL_RADAR))
5420 /* Set the user_txpower_limit to the highest power
5421 * supported by any channel */
5422 if (eeprom_ch_info[ch].max_power_avg >
5423 priv->user_txpower_limit)
5424 priv->user_txpower_limit =
5425 eeprom_ch_info[ch].max_power_avg;
5431 for (band = 6; band <= 7; band++) {
5433 u8 fat_extension_chan;
5435 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5436 &eeprom_ch_info, &eeprom_ch_index);
5438 phymode = (band == 6) ? MODE_IEEE80211B : MODE_IEEE80211A;
5439 /* Loop through each band adding each of the channels */
5440 for (ch = 0; ch < eeprom_ch_count; ch++) {
5443 ((eeprom_ch_index[ch] == 5) ||
5444 (eeprom_ch_index[ch] == 6) ||
5445 (eeprom_ch_index[ch] == 7)))
5446 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5448 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5450 iwl4965_set_fat_chan_info(priv, phymode,
5451 eeprom_ch_index[ch],
5452 &(eeprom_ch_info[ch]),
5453 fat_extension_chan);
5455 iwl4965_set_fat_chan_info(priv, phymode,
5456 (eeprom_ch_index[ch] + 4),
5457 &(eeprom_ch_info[ch]),
5458 HT_IE_EXT_CHANNEL_BELOW);
5465 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5466 * sending probe req. This should be set long enough to hear probe responses
5467 * from more than one AP. */
5468 #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5469 #define IWL_ACTIVE_DWELL_TIME_52 (10)
5471 /* For faster active scanning, scan will move to the next channel if fewer than
5472 * PLCP_QUIET_THRESH packets are heard on this channel within
5473 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5474 * time if it's a quiet channel (nothing responded to our probe, and there's
5475 * no other traffic).
5476 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5477 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5478 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5480 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5481 * Must be set longer than active dwell time.
5482 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5483 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5484 #define IWL_PASSIVE_DWELL_TIME_52 (10)
5485 #define IWL_PASSIVE_DWELL_BASE (100)
5486 #define IWL_CHANNEL_TUNE_TIME 5
5488 static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv, int phymode)
5490 if (phymode == MODE_IEEE80211A)
5491 return IWL_ACTIVE_DWELL_TIME_52;
5493 return IWL_ACTIVE_DWELL_TIME_24;
5496 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, int phymode)
5498 u16 active = iwl_get_active_dwell_time(priv, phymode);
5499 u16 passive = (phymode != MODE_IEEE80211A) ?
5500 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5501 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5503 if (iwl_is_associated(priv)) {
5504 /* If we're associated, we clamp the maximum passive
5505 * dwell time to be 98% of the beacon interval (minus
5506 * 2 * channel tune time) */
5507 passive = priv->beacon_int;
5508 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5509 passive = IWL_PASSIVE_DWELL_BASE;
5510 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5513 if (passive <= active)
5514 passive = active + 1;
5519 static int iwl_get_channels_for_scan(struct iwl_priv *priv, int phymode,
5520 u8 is_active, u8 direct_mask,
5521 struct iwl_scan_channel *scan_ch)
5523 const struct ieee80211_channel *channels = NULL;
5524 const struct ieee80211_hw_mode *hw_mode;
5525 const struct iwl_channel_info *ch_info;
5526 u16 passive_dwell = 0;
5527 u16 active_dwell = 0;
5530 hw_mode = iwl_get_hw_mode(priv, phymode);
5534 channels = hw_mode->channels;
5536 active_dwell = iwl_get_active_dwell_time(priv, phymode);
5537 passive_dwell = iwl_get_passive_dwell_time(priv, phymode);
5539 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5540 if (channels[i].chan ==
5541 le16_to_cpu(priv->active_rxon.channel)) {
5542 if (iwl_is_associated(priv)) {
5544 ("Skipping current channel %d\n",
5545 le16_to_cpu(priv->active_rxon.channel));
5548 } else if (priv->only_active_channel)
5551 scan_ch->channel = channels[i].chan;
5553 ch_info = iwl_get_channel_info(priv, phymode, scan_ch->channel);
5554 if (!is_channel_valid(ch_info)) {
5555 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5560 if (!is_active || is_channel_passive(ch_info) ||
5561 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5562 scan_ch->type = 0; /* passive */
5564 scan_ch->type = 1; /* active */
5566 if (scan_ch->type & 1)
5567 scan_ch->type |= (direct_mask << 1);
5569 if (is_channel_narrow(ch_info))
5570 scan_ch->type |= (1 << 7);
5572 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5573 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5575 /* Set power levels to defaults */
5576 scan_ch->tpc.dsp_atten = 110;
5577 /* scan_pwr_info->tpc.dsp_atten; */
5579 /*scan_pwr_info->tpc.tx_gain; */
5580 if (phymode == MODE_IEEE80211A)
5581 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5583 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5584 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
5586 scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
5590 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5592 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5593 (scan_ch->type & 1) ?
5594 active_dwell : passive_dwell);
5600 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5604 static void iwl_reset_channel_flag(struct iwl_priv *priv)
5607 for (i = 0; i < 3; i++) {
5608 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5609 for (j = 0; j < hw_mode->num_channels; j++)
5610 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5614 static void iwl_init_hw_rates(struct iwl_priv *priv,
5615 struct ieee80211_rate *rates)
5619 for (i = 0; i < IWL_RATE_COUNT; i++) {
5620 rates[i].rate = iwl_rates[i].ieee * 5;
5621 rates[i].val = i; /* Rate scaling will work on indexes */
5623 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5624 /* Only OFDM have the bits-per-symbol set */
5625 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5626 rates[i].flags |= IEEE80211_RATE_OFDM;
5629 * If CCK 1M then set rate flag to CCK else CCK_2
5630 * which is CCK | PREAMBLE2
5632 rates[i].flags |= (iwl_rates[i].plcp == 10) ?
5633 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5636 /* Set up which ones are basic rates... */
5637 if (IWL_BASIC_RATES_MASK & (1 << i))
5638 rates[i].flags |= IEEE80211_RATE_BASIC;
5641 iwl4965_init_hw_rates(priv, rates);
5645 * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
5647 static int iwl_init_geos(struct iwl_priv *priv)
5649 struct iwl_channel_info *ch;
5650 struct ieee80211_hw_mode *modes;
5651 struct ieee80211_channel *channels;
5652 struct ieee80211_channel *geo_ch;
5653 struct ieee80211_rate *rates;
5665 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5666 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5670 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5675 channels = kzalloc(sizeof(struct ieee80211_channel) *
5676 priv->channel_count, GFP_KERNEL);
5682 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5695 /* 5.2GHz channels start after the 2.4GHz channels */
5696 modes[A].mode = MODE_IEEE80211A;
5697 modes[A].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
5698 modes[A].rates = rates;
5699 modes[A].num_rates = 8; /* just OFDM */
5700 modes[A].rates = &rates[4];
5701 modes[A].num_channels = 0;
5703 modes[B].mode = MODE_IEEE80211B;
5704 modes[B].channels = channels;
5705 modes[B].rates = rates;
5706 modes[B].num_rates = 4; /* just CCK */
5707 modes[B].num_channels = 0;
5709 modes[G].mode = MODE_IEEE80211G;
5710 modes[G].channels = channels;
5711 modes[G].rates = rates;
5712 modes[G].num_rates = 12; /* OFDM & CCK */
5713 modes[G].num_channels = 0;
5715 modes[G_11N].mode = MODE_IEEE80211G;
5716 modes[G_11N].channels = channels;
5717 modes[G_11N].num_rates = 13; /* OFDM & CCK */
5718 modes[G_11N].rates = rates;
5719 modes[G_11N].num_channels = 0;
5721 modes[A_11N].mode = MODE_IEEE80211A;
5722 modes[A_11N].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
5723 modes[A_11N].rates = &rates[4];
5724 modes[A_11N].num_rates = 9; /* just OFDM */
5725 modes[A_11N].num_channels = 0;
5727 priv->ieee_channels = channels;
5728 priv->ieee_rates = rates;
5730 iwl_init_hw_rates(priv, rates);
5732 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5733 ch = &priv->channel_info[i];
5735 if (!is_channel_valid(ch)) {
5736 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5738 ch->channel, is_channel_a_band(ch) ?
5743 if (is_channel_a_band(ch)) {
5744 geo_ch = &modes[A].channels[modes[A].num_channels++];
5745 modes[A_11N].num_channels++;
5747 geo_ch = &modes[B].channels[modes[B].num_channels++];
5748 modes[G].num_channels++;
5749 modes[G_11N].num_channels++;
5752 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5753 geo_ch->chan = ch->channel;
5754 geo_ch->power_level = ch->max_power_avg;
5755 geo_ch->antenna_max = 0xff;
5757 if (is_channel_valid(ch)) {
5758 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5759 if (ch->flags & EEPROM_CHANNEL_IBSS)
5760 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5762 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5763 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5765 if (ch->flags & EEPROM_CHANNEL_RADAR)
5766 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5768 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5769 priv->max_channel_txpower_limit =
5773 geo_ch->val = geo_ch->flag;
5776 if ((modes[A].num_channels == 0) && priv->is_abg) {
5777 printk(KERN_INFO DRV_NAME
5778 ": Incorrectly detected BG card as ABG. Please send "
5779 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5780 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5784 printk(KERN_INFO DRV_NAME
5785 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5786 modes[G].num_channels, modes[A].num_channels);
5789 * NOTE: We register these in preference of order -- the
5790 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5791 * a phymode based on rates or AP capabilities but seems to
5792 * configure it purely on if the channel being configured
5793 * is supported by a mode -- and the first match is taken
5796 if (modes[G].num_channels)
5797 ieee80211_register_hwmode(priv->hw, &modes[G]);
5798 if (modes[B].num_channels)
5799 ieee80211_register_hwmode(priv->hw, &modes[B]);
5800 if (modes[A].num_channels)
5801 ieee80211_register_hwmode(priv->hw, &modes[A]);
5803 priv->modes = modes;
5804 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5809 /******************************************************************************
5811 * uCode download functions
5813 ******************************************************************************/
5815 static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
5817 if (priv->ucode_code.v_addr != NULL) {
5818 pci_free_consistent(priv->pci_dev,
5819 priv->ucode_code.len,
5820 priv->ucode_code.v_addr,
5821 priv->ucode_code.p_addr);
5822 priv->ucode_code.v_addr = NULL;
5824 if (priv->ucode_data.v_addr != NULL) {
5825 pci_free_consistent(priv->pci_dev,
5826 priv->ucode_data.len,
5827 priv->ucode_data.v_addr,
5828 priv->ucode_data.p_addr);
5829 priv->ucode_data.v_addr = NULL;
5831 if (priv->ucode_data_backup.v_addr != NULL) {
5832 pci_free_consistent(priv->pci_dev,
5833 priv->ucode_data_backup.len,
5834 priv->ucode_data_backup.v_addr,
5835 priv->ucode_data_backup.p_addr);
5836 priv->ucode_data_backup.v_addr = NULL;
5838 if (priv->ucode_init.v_addr != NULL) {
5839 pci_free_consistent(priv->pci_dev,
5840 priv->ucode_init.len,
5841 priv->ucode_init.v_addr,
5842 priv->ucode_init.p_addr);
5843 priv->ucode_init.v_addr = NULL;
5845 if (priv->ucode_init_data.v_addr != NULL) {
5846 pci_free_consistent(priv->pci_dev,
5847 priv->ucode_init_data.len,
5848 priv->ucode_init_data.v_addr,
5849 priv->ucode_init_data.p_addr);
5850 priv->ucode_init_data.v_addr = NULL;
5852 if (priv->ucode_boot.v_addr != NULL) {
5853 pci_free_consistent(priv->pci_dev,
5854 priv->ucode_boot.len,
5855 priv->ucode_boot.v_addr,
5856 priv->ucode_boot.p_addr);
5857 priv->ucode_boot.v_addr = NULL;
5862 * iwl_verify_inst_full - verify runtime uCode image in card vs. host,
5863 * looking at all data.
5865 static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 * image, u32 len)
5872 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5874 rc = iwl_grab_restricted_access(priv);
5878 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
5881 for (; len > 0; len -= sizeof(u32), image++) {
5882 /* read data comes through single port, auto-incr addr */
5883 /* NOTE: Use the debugless read so we don't flood kernel log
5884 * if IWL_DL_IO is set */
5885 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5886 if (val != le32_to_cpu(*image)) {
5887 IWL_ERROR("uCode INST section is invalid at "
5888 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5889 save_len - len, val, le32_to_cpu(*image));
5897 iwl_release_restricted_access(priv);
5901 ("ucode image in INSTRUCTION memory is good\n");
5908 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
5909 * using sample data 100 bytes apart. If these sample points are good,
5910 * it's a pretty good bet that everything between them is good, too.
5912 static int iwl_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
5919 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5921 rc = iwl_grab_restricted_access(priv);
5925 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5926 /* read data comes through single port, auto-incr addr */
5927 /* NOTE: Use the debugless read so we don't flood kernel log
5928 * if IWL_DL_IO is set */
5929 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR,
5930 i + RTC_INST_LOWER_BOUND);
5931 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5932 if (val != le32_to_cpu(*image)) {
5933 #if 0 /* Enable this if you want to see details */
5934 IWL_ERROR("uCode INST section is invalid at "
5935 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5945 iwl_release_restricted_access(priv);
5952 * iwl_verify_ucode - determine which instruction image is in SRAM,
5953 * and verify its contents
5955 static int iwl_verify_ucode(struct iwl_priv *priv)
5962 image = (__le32 *)priv->ucode_boot.v_addr;
5963 len = priv->ucode_boot.len;
5964 rc = iwl_verify_inst_sparse(priv, image, len);
5966 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5970 /* Try initialize */
5971 image = (__le32 *)priv->ucode_init.v_addr;
5972 len = priv->ucode_init.len;
5973 rc = iwl_verify_inst_sparse(priv, image, len);
5975 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5979 /* Try runtime/protocol */
5980 image = (__le32 *)priv->ucode_code.v_addr;
5981 len = priv->ucode_code.len;
5982 rc = iwl_verify_inst_sparse(priv, image, len);
5984 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5988 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5990 /* Show first several data entries in instruction SRAM.
5991 * Selection of bootstrap image is arbitrary. */
5992 image = (__le32 *)priv->ucode_boot.v_addr;
5993 len = priv->ucode_boot.len;
5994 rc = iwl_verify_inst_full(priv, image, len);
6000 /* check contents of special bootstrap uCode SRAM */
6001 static int iwl_verify_bsm(struct iwl_priv *priv)
6003 __le32 *image = priv->ucode_boot.v_addr;
6004 u32 len = priv->ucode_boot.len;
6008 IWL_DEBUG_INFO("Begin verify bsm\n");
6010 /* verify BSM SRAM contents */
6011 val = iwl_read_restricted_reg(priv, BSM_WR_DWCOUNT_REG);
6012 for (reg = BSM_SRAM_LOWER_BOUND;
6013 reg < BSM_SRAM_LOWER_BOUND + len;
6014 reg += sizeof(u32), image ++) {
6015 val = iwl_read_restricted_reg(priv, reg);
6016 if (val != le32_to_cpu(*image)) {
6017 IWL_ERROR("BSM uCode verification failed at "
6018 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
6019 BSM_SRAM_LOWER_BOUND,
6020 reg - BSM_SRAM_LOWER_BOUND, len,
6021 val, le32_to_cpu(*image));
6026 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
6032 * iwl_load_bsm - Load bootstrap instructions
6036 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
6037 * in special SRAM that does not power down during RFKILL. When powering back
6038 * up after power-saving sleeps (or during initial uCode load), the BSM loads
6039 * the bootstrap program into the on-board processor, and starts it.
6041 * The bootstrap program loads (via DMA) instructions and data for a new
6042 * program from host DRAM locations indicated by the host driver in the
6043 * BSM_DRAM_* registers. Once the new program is loaded, it starts
6046 * When initializing the NIC, the host driver points the BSM to the
6047 * "initialize" uCode image. This uCode sets up some internal data, then
6048 * notifies host via "initialize alive" that it is complete.
6050 * The host then replaces the BSM_DRAM_* pointer values to point to the
6051 * normal runtime uCode instructions and a backup uCode data cache buffer
6052 * (filled initially with starting data values for the on-board processor),
6053 * then triggers the "initialize" uCode to load and launch the runtime uCode,
6054 * which begins normal operation.
6056 * When doing a power-save shutdown, runtime uCode saves data SRAM into
6057 * the backup data cache in DRAM before SRAM is powered down.
6059 * When powering back up, the BSM loads the bootstrap program. This reloads
6060 * the runtime uCode instructions and the backup data cache into SRAM,
6061 * and re-launches the runtime uCode from where it left off.
6063 static int iwl_load_bsm(struct iwl_priv *priv)
6065 __le32 *image = priv->ucode_boot.v_addr;
6066 u32 len = priv->ucode_boot.len;
6076 IWL_DEBUG_INFO("Begin load bsm\n");
6078 /* make sure bootstrap program is no larger than BSM's SRAM size */
6079 if (len > IWL_MAX_BSM_SIZE)
6082 /* Tell bootstrap uCode where to find the "Initialize" uCode
6083 * in host DRAM ... bits 31:0 for 3945, bits 35:4 for 4965.
6084 * NOTE: iwl_initialize_alive_start() will replace these values,
6085 * after the "initialize" uCode has run, to point to
6086 * runtime/protocol instructions and backup data cache. */
6087 pinst = priv->ucode_init.p_addr >> 4;
6088 pdata = priv->ucode_init_data.p_addr >> 4;
6089 inst_len = priv->ucode_init.len;
6090 data_len = priv->ucode_init_data.len;
6092 rc = iwl_grab_restricted_access(priv);
6096 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
6097 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6098 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
6099 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
6101 /* Fill BSM memory with bootstrap instructions */
6102 for (reg_offset = BSM_SRAM_LOWER_BOUND;
6103 reg_offset < BSM_SRAM_LOWER_BOUND + len;
6104 reg_offset += sizeof(u32), image++)
6105 _iwl_write_restricted_reg(priv, reg_offset,
6106 le32_to_cpu(*image));
6108 rc = iwl_verify_bsm(priv);
6110 iwl_release_restricted_access(priv);
6114 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
6115 iwl_write_restricted_reg(priv, BSM_WR_MEM_SRC_REG, 0x0);
6116 iwl_write_restricted_reg(priv, BSM_WR_MEM_DST_REG,
6117 RTC_INST_LOWER_BOUND);
6118 iwl_write_restricted_reg(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
6120 /* Load bootstrap code into instruction SRAM now,
6121 * to prepare to load "initialize" uCode */
6122 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
6123 BSM_WR_CTRL_REG_BIT_START);
6125 /* Wait for load of bootstrap uCode to finish */
6126 for (i = 0; i < 100; i++) {
6127 done = iwl_read_restricted_reg(priv, BSM_WR_CTRL_REG);
6128 if (!(done & BSM_WR_CTRL_REG_BIT_START))
6133 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
6135 IWL_ERROR("BSM write did not complete!\n");
6139 /* Enable future boot loads whenever power management unit triggers it
6140 * (e.g. when powering back up after power-save shutdown) */
6141 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
6142 BSM_WR_CTRL_REG_BIT_START_EN);
6144 iwl_release_restricted_access(priv);
6149 static void iwl_nic_start(struct iwl_priv *priv)
6151 /* Remove all resets to allow NIC to operate */
6152 iwl_write32(priv, CSR_RESET, 0);
6156 * iwl_read_ucode - Read uCode images from disk file.
6158 * Copy into buffers for card to fetch via bus-mastering
6160 static int iwl_read_ucode(struct iwl_priv *priv)
6162 struct iwl_ucode *ucode;
6164 const struct firmware *ucode_raw;
6165 const char *name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode";
6168 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
6170 /* Ask kernel firmware_class module to get the boot firmware off disk.
6171 * request_firmware() is synchronous, file is in memory on return. */
6172 rc = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
6174 IWL_ERROR("%s firmware file req failed: Reason %d\n", name, rc);
6178 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
6179 name, ucode_raw->size);
6181 /* Make sure that we got at least our header! */
6182 if (ucode_raw->size < sizeof(*ucode)) {
6183 IWL_ERROR("File size way too small!\n");
6188 /* Data from ucode file: header followed by uCode images */
6189 ucode = (void *)ucode_raw->data;
6191 ver = le32_to_cpu(ucode->ver);
6192 inst_size = le32_to_cpu(ucode->inst_size);
6193 data_size = le32_to_cpu(ucode->data_size);
6194 init_size = le32_to_cpu(ucode->init_size);
6195 init_data_size = le32_to_cpu(ucode->init_data_size);
6196 boot_size = le32_to_cpu(ucode->boot_size);
6198 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
6199 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
6201 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
6203 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
6205 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
6207 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
6210 /* Verify size of file vs. image size info in file's header */
6211 if (ucode_raw->size < sizeof(*ucode) +
6212 inst_size + data_size + init_size +
6213 init_data_size + boot_size) {
6215 IWL_DEBUG_INFO("uCode file size %d too small\n",
6216 (int)ucode_raw->size);
6221 /* Verify that uCode images will fit in card's SRAM */
6222 if (inst_size > IWL_MAX_INST_SIZE) {
6223 IWL_DEBUG_INFO("uCode instr len %d too large to fit in card\n",
6229 if (data_size > IWL_MAX_DATA_SIZE) {
6230 IWL_DEBUG_INFO("uCode data len %d too large to fit in card\n",
6235 if (init_size > IWL_MAX_INST_SIZE) {
6237 ("uCode init instr len %d too large to fit in card\n",
6242 if (init_data_size > IWL_MAX_DATA_SIZE) {
6244 ("uCode init data len %d too large to fit in card\n",
6245 (int)init_data_size);
6249 if (boot_size > IWL_MAX_BSM_SIZE) {
6251 ("uCode boot instr len %d too large to fit in bsm\n",
6257 /* Allocate ucode buffers for card's bus-master loading ... */
6259 /* Runtime instructions and 2 copies of data:
6260 * 1) unmodified from disk
6261 * 2) backup cache for save/restore during power-downs */
6262 priv->ucode_code.len = inst_size;
6263 priv->ucode_code.v_addr =
6264 pci_alloc_consistent(priv->pci_dev,
6265 priv->ucode_code.len,
6266 &(priv->ucode_code.p_addr));
6268 priv->ucode_data.len = data_size;
6269 priv->ucode_data.v_addr =
6270 pci_alloc_consistent(priv->pci_dev,
6271 priv->ucode_data.len,
6272 &(priv->ucode_data.p_addr));
6274 priv->ucode_data_backup.len = data_size;
6275 priv->ucode_data_backup.v_addr =
6276 pci_alloc_consistent(priv->pci_dev,
6277 priv->ucode_data_backup.len,
6278 &(priv->ucode_data_backup.p_addr));
6281 /* Initialization instructions and data */
6282 priv->ucode_init.len = init_size;
6283 priv->ucode_init.v_addr =
6284 pci_alloc_consistent(priv->pci_dev,
6285 priv->ucode_init.len,
6286 &(priv->ucode_init.p_addr));
6288 priv->ucode_init_data.len = init_data_size;
6289 priv->ucode_init_data.v_addr =
6290 pci_alloc_consistent(priv->pci_dev,
6291 priv->ucode_init_data.len,
6292 &(priv->ucode_init_data.p_addr));
6294 /* Bootstrap (instructions only, no data) */
6295 priv->ucode_boot.len = boot_size;
6296 priv->ucode_boot.v_addr =
6297 pci_alloc_consistent(priv->pci_dev,
6298 priv->ucode_boot.len,
6299 &(priv->ucode_boot.p_addr));
6301 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
6302 !priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr ||
6303 !priv->ucode_boot.v_addr || !priv->ucode_data_backup.v_addr)
6306 /* Copy images into buffers for card's bus-master reads ... */
6308 /* Runtime instructions (first block of data in file) */
6309 src = &ucode->data[0];
6310 len = priv->ucode_code.len;
6311 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %d\n",
6313 memcpy(priv->ucode_code.v_addr, src, len);
6314 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6315 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6317 /* Runtime data (2nd block)
6318 * NOTE: Copy into backup buffer will be done in iwl_up() */
6319 src = &ucode->data[inst_size];
6320 len = priv->ucode_data.len;
6321 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %d\n",
6323 memcpy(priv->ucode_data.v_addr, src, len);
6324 memcpy(priv->ucode_data_backup.v_addr, src, len);
6326 /* Initialization instructions (3rd block) */
6328 src = &ucode->data[inst_size + data_size];
6329 len = priv->ucode_init.len;
6330 IWL_DEBUG_INFO("Copying (but not loading) init instr len %d\n",
6332 memcpy(priv->ucode_init.v_addr, src, len);
6335 /* Initialization data (4th block) */
6336 if (init_data_size) {
6337 src = &ucode->data[inst_size + data_size + init_size];
6338 len = priv->ucode_init_data.len;
6339 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
6341 memcpy(priv->ucode_init_data.v_addr, src, len);
6344 /* Bootstrap instructions (5th block) */
6345 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6346 len = priv->ucode_boot.len;
6347 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
6349 memcpy(priv->ucode_boot.v_addr, src, len);
6351 /* We have our copies now, allow OS release its copies */
6352 release_firmware(ucode_raw);
6356 IWL_ERROR("failed to allocate pci memory\n");
6358 iwl_dealloc_ucode_pci(priv);
6361 release_firmware(ucode_raw);
6369 * iwl_set_ucode_ptrs - Set uCode address location
6371 * Tell initialization uCode where to find runtime uCode.
6373 * BSM registers initially contain pointers to initialization uCode.
6374 * We need to replace them to load runtime uCode inst and data,
6375 * and to save runtime data when powering down.
6377 static int iwl_set_ucode_ptrs(struct iwl_priv *priv)
6382 unsigned long flags;
6384 /* bits 35:4 for 4965 */
6385 pinst = priv->ucode_code.p_addr >> 4;
6386 pdata = priv->ucode_data_backup.p_addr >> 4;
6388 spin_lock_irqsave(&priv->lock, flags);
6389 rc = iwl_grab_restricted_access(priv);
6391 spin_unlock_irqrestore(&priv->lock, flags);
6395 /* Tell bootstrap uCode where to find image to load */
6396 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
6397 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6398 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
6399 priv->ucode_data.len);
6401 /* Inst bytecount must be last to set up, bit 31 signals uCode
6402 * that all new ptr/size info is in place */
6403 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG,
6404 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6406 iwl_release_restricted_access(priv);
6408 spin_unlock_irqrestore(&priv->lock, flags);
6410 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6416 * iwl_init_alive_start - Called after REPLY_ALIVE notification receieved
6418 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6420 * The 4965 "initialize" ALIVE reply contains calibration data for:
6421 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6422 * (3945 does not contain this data).
6424 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6426 static void iwl_init_alive_start(struct iwl_priv *priv)
6428 /* Check alive response for "valid" sign from uCode */
6429 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6430 /* We had an error bringing up the hardware, so take it
6431 * all the way back down so we can try again */
6432 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6436 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6437 * This is a paranoid check, because we would not have gotten the
6438 * "initialize" alive if code weren't properly loaded. */
6439 if (iwl_verify_ucode(priv)) {
6440 /* Runtime instruction load was bad;
6441 * take it all the way back down so we can try again */
6442 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6446 /* Calculate temperature */
6447 priv->temperature = iwl4965_get_temperature(priv);
6449 /* Send pointers to protocol/runtime uCode image ... init code will
6450 * load and launch runtime uCode, which will send us another "Alive"
6452 IWL_DEBUG_INFO("Initialization Alive received.\n");
6453 if (iwl_set_ucode_ptrs(priv)) {
6454 /* Runtime instruction load won't happen;
6455 * take it all the way back down so we can try again */
6456 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6462 queue_work(priv->workqueue, &priv->restart);
6467 * iwl_alive_start - called after REPLY_ALIVE notification received
6468 * from protocol/runtime uCode (initialization uCode's
6469 * Alive gets handled by iwl_init_alive_start()).
6471 static void iwl_alive_start(struct iwl_priv *priv)
6475 IWL_DEBUG_INFO("Runtime Alive received.\n");
6477 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6478 /* We had an error bringing up the hardware, so take it
6479 * all the way back down so we can try again */
6480 IWL_DEBUG_INFO("Alive failed.\n");
6484 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6485 * This is a paranoid check, because we would not have gotten the
6486 * "runtime" alive if code weren't properly loaded. */
6487 if (iwl_verify_ucode(priv)) {
6488 /* Runtime instruction load was bad;
6489 * take it all the way back down so we can try again */
6490 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6494 iwl_clear_stations_table(priv);
6496 rc = iwl4965_alive_notify(priv);
6498 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6503 /* After the ALIVE response, we can process host commands */
6504 set_bit(STATUS_ALIVE, &priv->status);
6506 /* Clear out the uCode error bit if it is set */
6507 clear_bit(STATUS_FW_ERROR, &priv->status);
6509 rc = iwl_init_channel_map(priv);
6511 IWL_ERROR("initializing regulatory failed: %d\n", rc);
6515 iwl_init_geos(priv);
6517 if (iwl_is_rfkill(priv))
6520 if (!priv->mac80211_registered) {
6521 /* Unlock so any user space entry points can call back into
6522 * the driver without a deadlock... */
6523 mutex_unlock(&priv->mutex);
6524 iwl_rate_control_register(priv->hw);
6525 rc = ieee80211_register_hw(priv->hw);
6526 priv->hw->conf.beacon_int = 100;
6527 mutex_lock(&priv->mutex);
6530 IWL_ERROR("Failed to register network "
6531 "device (error %d)\n", rc);
6535 priv->mac80211_registered = 1;
6537 iwl_reset_channel_flag(priv);
6539 ieee80211_start_queues(priv->hw);
6541 priv->active_rate = priv->rates_mask;
6542 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6544 iwl_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
6546 if (iwl_is_associated(priv)) {
6547 struct iwl_rxon_cmd *active_rxon =
6548 (struct iwl_rxon_cmd *)(&priv->active_rxon);
6550 memcpy(&priv->staging_rxon, &priv->active_rxon,
6551 sizeof(priv->staging_rxon));
6552 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6554 /* Initialize our rx_config data */
6555 iwl_connection_init_rx_config(priv);
6556 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6559 /* Configure BT coexistence */
6560 iwl_send_bt_config(priv);
6562 /* Configure the adapter for unassociated operation */
6563 iwl_commit_rxon(priv);
6565 /* At this point, the NIC is initialized and operational */
6566 priv->notif_missed_beacons = 0;
6567 set_bit(STATUS_READY, &priv->status);
6569 iwl4965_rf_kill_ct_config(priv);
6570 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6572 if (priv->error_recovering)
6573 iwl_error_recovery(priv);
6578 queue_work(priv->workqueue, &priv->restart);
6581 static void iwl_cancel_deferred_work(struct iwl_priv *priv);
6583 static void __iwl_down(struct iwl_priv *priv)
6585 unsigned long flags;
6586 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6587 struct ieee80211_conf *conf = NULL;
6589 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6591 conf = ieee80211_get_hw_conf(priv->hw);
6594 set_bit(STATUS_EXIT_PENDING, &priv->status);
6596 iwl_clear_stations_table(priv);
6598 /* Unblock any waiting calls */
6599 wake_up_interruptible_all(&priv->wait_command_queue);
6601 iwl_cancel_deferred_work(priv);
6603 /* Wipe out the EXIT_PENDING status bit if we are not actually
6604 * exiting the module */
6606 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6608 /* stop and reset the on-board processor */
6609 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
6611 /* tell the device to stop sending interrupts */
6612 iwl_disable_interrupts(priv);
6614 if (priv->mac80211_registered)
6615 ieee80211_stop_queues(priv->hw);
6617 /* If we have not previously called iwl_init() then
6618 * clear all bits but the RF Kill and SUSPEND bits and return */
6619 if (!iwl_is_init(priv)) {
6620 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6622 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6624 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6629 /* ...otherwise clear out all the status bits but the RF Kill and
6630 * SUSPEND bits and continue taking the NIC down. */
6631 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6633 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6635 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6637 test_bit(STATUS_FW_ERROR, &priv->status) <<
6640 spin_lock_irqsave(&priv->lock, flags);
6641 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
6642 spin_unlock_irqrestore(&priv->lock, flags);
6644 iwl_hw_txq_ctx_stop(priv);
6645 iwl_hw_rxq_stop(priv);
6647 spin_lock_irqsave(&priv->lock, flags);
6648 if (!iwl_grab_restricted_access(priv)) {
6649 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
6650 APMG_CLK_VAL_DMA_CLK_RQT);
6651 iwl_release_restricted_access(priv);
6653 spin_unlock_irqrestore(&priv->lock, flags);
6657 iwl_hw_nic_stop_master(priv);
6658 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6659 iwl_hw_nic_reset(priv);
6662 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
6664 if (priv->ibss_beacon)
6665 dev_kfree_skb(priv->ibss_beacon);
6666 priv->ibss_beacon = NULL;
6668 /* clear out any free frames */
6669 iwl_clear_free_frames(priv);
6672 static void iwl_down(struct iwl_priv *priv)
6674 mutex_lock(&priv->mutex);
6676 mutex_unlock(&priv->mutex);
6679 #define MAX_HW_RESTARTS 5
6681 static int __iwl_up(struct iwl_priv *priv)
6683 DECLARE_MAC_BUF(mac);
6687 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6688 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6692 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6693 IWL_WARNING("Radio disabled by SW RF kill (module "
6698 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6700 rc = iwl_hw_nic_init(priv);
6702 IWL_ERROR("Unable to int nic\n");
6706 /* make sure rfkill handshake bits are cleared */
6707 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6708 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
6709 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6711 /* clear (again), then enable host interrupts */
6712 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6713 iwl_enable_interrupts(priv);
6715 /* really make sure rfkill handshake bits are cleared */
6716 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6717 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6719 /* Copy original ucode data image from disk into backup cache.
6720 * This will be used to initialize the on-board processor's
6721 * data SRAM for a clean start when the runtime program first loads. */
6722 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6723 priv->ucode_data.len);
6725 /* If platform's RF_KILL switch is set to KILL,
6726 * wait for BIT_INT_RF_KILL interrupt before loading uCode
6727 * and getting things started */
6728 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
6729 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
6732 if (test_bit(STATUS_RF_KILL_HW, &priv->status) || hw_rf_kill) {
6733 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6737 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6739 iwl_clear_stations_table(priv);
6741 /* load bootstrap state machine,
6742 * load bootstrap program into processor's memory,
6743 * prepare to load the "initialize" uCode */
6744 rc = iwl_load_bsm(priv);
6747 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6751 /* start card; "initialize" will load runtime ucode */
6752 iwl_nic_start(priv);
6754 /* MAC Address location in EEPROM same for 3945/4965 */
6755 get_eeprom_mac(priv, priv->mac_addr);
6756 IWL_DEBUG_INFO("MAC address: %s\n",
6757 print_mac(mac, priv->mac_addr));
6759 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6761 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6766 set_bit(STATUS_EXIT_PENDING, &priv->status);
6769 /* tried to restart and config the device for as long as our
6770 * patience could withstand */
6771 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6776 /*****************************************************************************
6778 * Workqueue callbacks
6780 *****************************************************************************/
6782 static void iwl_bg_init_alive_start(struct work_struct *data)
6784 struct iwl_priv *priv =
6785 container_of(data, struct iwl_priv, init_alive_start.work);
6787 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6790 mutex_lock(&priv->mutex);
6791 iwl_init_alive_start(priv);
6792 mutex_unlock(&priv->mutex);
6795 static void iwl_bg_alive_start(struct work_struct *data)
6797 struct iwl_priv *priv =
6798 container_of(data, struct iwl_priv, alive_start.work);
6800 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6803 mutex_lock(&priv->mutex);
6804 iwl_alive_start(priv);
6805 mutex_unlock(&priv->mutex);
6808 static void iwl_bg_rf_kill(struct work_struct *work)
6810 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
6812 wake_up_interruptible(&priv->wait_command_queue);
6814 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6817 mutex_lock(&priv->mutex);
6819 if (!iwl_is_rfkill(priv)) {
6820 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6821 "HW and/or SW RF Kill no longer active, restarting "
6823 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6824 queue_work(priv->workqueue, &priv->restart);
6827 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6828 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6829 "disabled by SW switch\n");
6831 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6832 "Kill switch must be turned off for "
6833 "wireless networking to work.\n");
6835 mutex_unlock(&priv->mutex);
6838 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6840 static void iwl_bg_scan_check(struct work_struct *data)
6842 struct iwl_priv *priv =
6843 container_of(data, struct iwl_priv, scan_check.work);
6845 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6848 mutex_lock(&priv->mutex);
6849 if (test_bit(STATUS_SCANNING, &priv->status) ||
6850 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6851 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6852 "Scan completion watchdog resetting adapter (%dms)\n",
6853 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
6855 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6856 iwl_send_scan_abort(priv);
6858 mutex_unlock(&priv->mutex);
6861 static void iwl_bg_request_scan(struct work_struct *data)
6863 struct iwl_priv *priv =
6864 container_of(data, struct iwl_priv, request_scan);
6865 struct iwl_host_cmd cmd = {
6866 .id = REPLY_SCAN_CMD,
6867 .len = sizeof(struct iwl_scan_cmd),
6868 .meta.flags = CMD_SIZE_HUGE,
6871 struct iwl_scan_cmd *scan;
6872 struct ieee80211_conf *conf = NULL;
6876 conf = ieee80211_get_hw_conf(priv->hw);
6878 mutex_lock(&priv->mutex);
6880 if (!iwl_is_ready(priv)) {
6881 IWL_WARNING("request scan called when driver not ready.\n");
6885 /* Make sure the scan wasn't cancelled before this queued work
6886 * was given the chance to run... */
6887 if (!test_bit(STATUS_SCANNING, &priv->status))
6890 /* This should never be called or scheduled if there is currently
6891 * a scan active in the hardware. */
6892 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6893 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6894 "Ignoring second request.\n");
6899 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6900 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6904 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6905 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6909 if (iwl_is_rfkill(priv)) {
6910 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6914 if (!test_bit(STATUS_READY, &priv->status)) {
6915 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6919 if (!priv->scan_bands) {
6920 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6925 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
6926 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6933 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
6935 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6936 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6938 if (iwl_is_associated(priv)) {
6941 u32 suspend_time = 100;
6942 u32 scan_suspend_time = 100;
6943 unsigned long flags;
6945 IWL_DEBUG_INFO("Scanning while associated...\n");
6947 spin_lock_irqsave(&priv->lock, flags);
6948 interval = priv->beacon_int;
6949 spin_unlock_irqrestore(&priv->lock, flags);
6951 scan->suspend_time = 0;
6952 scan->max_out_time = cpu_to_le32(200 * 1024);
6954 interval = suspend_time;
6956 extra = (suspend_time / interval) << 22;
6957 scan_suspend_time = (extra |
6958 ((suspend_time % interval) * 1024));
6959 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6960 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6961 scan_suspend_time, interval);
6964 /* We should add the ability for user to lock to PASSIVE ONLY */
6965 if (priv->one_direct_scan) {
6967 ("Kicking off one direct scan for '%s'\n",
6968 iwl_escape_essid(priv->direct_ssid,
6969 priv->direct_ssid_len));
6970 scan->direct_scan[0].id = WLAN_EID_SSID;
6971 scan->direct_scan[0].len = priv->direct_ssid_len;
6972 memcpy(scan->direct_scan[0].ssid,
6973 priv->direct_ssid, priv->direct_ssid_len);
6975 } else if (!iwl_is_associated(priv) && priv->essid_len) {
6976 scan->direct_scan[0].id = WLAN_EID_SSID;
6977 scan->direct_scan[0].len = priv->essid_len;
6978 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6983 /* We don't build a direct scan probe request; the uCode will do
6984 * that based on the direct_mask added to each channel entry */
6985 scan->tx_cmd.len = cpu_to_le16(
6986 iwl_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
6987 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
6988 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6989 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6990 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6992 /* flags + rate selection */
6994 scan->tx_cmd.tx_flags |= cpu_to_le32(0x200);
6996 switch (priv->scan_bands) {
6998 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6999 scan->tx_cmd.rate_n_flags =
7000 iwl_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
7001 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
7003 scan->good_CRC_th = 0;
7004 phymode = MODE_IEEE80211G;
7008 scan->tx_cmd.rate_n_flags =
7009 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
7010 RATE_MCS_ANT_B_MSK);
7011 scan->good_CRC_th = IWL_GOOD_CRC_TH;
7012 phymode = MODE_IEEE80211A;
7016 IWL_WARNING("Invalid scan band count\n");
7020 /* select Rx chains */
7022 /* Force use of chains B and C (0x6) for scan Rx.
7023 * Avoid A (0x1) because of its off-channel reception on A-band.
7024 * MIMO is not used here, but value is required to make uCode happy. */
7025 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
7026 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
7027 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
7028 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
7030 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
7031 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
7035 ("Initiating direct scan for %s.\n",
7036 iwl_escape_essid(priv->essid, priv->essid_len));
7038 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
7040 scan->channel_count =
7041 iwl_get_channels_for_scan(
7042 priv, phymode, 1, /* active */
7044 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
7046 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
7047 scan->channel_count * sizeof(struct iwl_scan_channel);
7049 scan->len = cpu_to_le16(cmd.len);
7051 set_bit(STATUS_SCAN_HW, &priv->status);
7052 rc = iwl_send_cmd_sync(priv, &cmd);
7056 queue_delayed_work(priv->workqueue, &priv->scan_check,
7057 IWL_SCAN_CHECK_WATCHDOG);
7059 mutex_unlock(&priv->mutex);
7063 /* inform mac80211 sacn aborted */
7064 queue_work(priv->workqueue, &priv->scan_completed);
7065 mutex_unlock(&priv->mutex);
7068 static void iwl_bg_up(struct work_struct *data)
7070 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
7072 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7075 mutex_lock(&priv->mutex);
7077 mutex_unlock(&priv->mutex);
7080 static void iwl_bg_restart(struct work_struct *data)
7082 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
7084 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7088 queue_work(priv->workqueue, &priv->up);
7091 static void iwl_bg_rx_replenish(struct work_struct *data)
7093 struct iwl_priv *priv =
7094 container_of(data, struct iwl_priv, rx_replenish);
7096 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7099 mutex_lock(&priv->mutex);
7100 iwl_rx_replenish(priv);
7101 mutex_unlock(&priv->mutex);
7104 static void iwl_bg_post_associate(struct work_struct *data)
7106 struct iwl_priv *priv = container_of(data, struct iwl_priv,
7107 post_associate.work);
7110 struct ieee80211_conf *conf = NULL;
7111 DECLARE_MAC_BUF(mac);
7113 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7114 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
7118 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
7120 print_mac(mac, priv->active_rxon.bssid_addr));
7123 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7126 mutex_lock(&priv->mutex);
7128 if (!priv->interface_id || !priv->is_open) {
7129 mutex_unlock(&priv->mutex);
7132 iwl_scan_cancel_timeout(priv, 200);
7134 conf = ieee80211_get_hw_conf(priv->hw);
7136 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7137 iwl_commit_rxon(priv);
7139 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
7140 iwl_setup_rxon_timing(priv);
7141 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7142 sizeof(priv->rxon_timing), &priv->rxon_timing);
7144 IWL_WARNING("REPLY_RXON_TIMING failed - "
7145 "Attempting to continue.\n");
7147 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7149 #ifdef CONFIG_IWLWIFI_HT
7150 if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
7151 iwl4965_set_rxon_ht(priv, &priv->current_assoc_ht);
7153 priv->active_rate_ht[0] = 0;
7154 priv->active_rate_ht[1] = 0;
7155 priv->current_channel_width = IWL_CHANNEL_WIDTH_20MHZ;
7157 #endif /* CONFIG_IWLWIFI_HT*/
7158 iwl4965_set_rxon_chain(priv);
7159 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7161 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
7162 priv->assoc_id, priv->beacon_int);
7164 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7165 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7167 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7169 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7170 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
7171 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
7173 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7175 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7176 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7180 iwl_commit_rxon(priv);
7182 switch (priv->iw_mode) {
7183 case IEEE80211_IF_TYPE_STA:
7184 iwl_rate_scale_init(priv->hw, IWL_AP_ID);
7187 case IEEE80211_IF_TYPE_IBSS:
7189 /* clear out the station table */
7190 iwl_clear_stations_table(priv);
7192 iwl_rxon_add_station(priv, BROADCAST_ADDR, 0);
7193 iwl_rxon_add_station(priv, priv->bssid, 0);
7194 iwl_rate_scale_init(priv->hw, IWL_STA_ID);
7195 iwl_send_beacon_cmd(priv);
7200 IWL_ERROR("%s Should not be called in %d mode\n",
7201 __FUNCTION__, priv->iw_mode);
7205 iwl_sequence_reset(priv);
7207 #ifdef CONFIG_IWLWIFI_SENSITIVITY
7208 /* Enable Rx differential gain and sensitivity calibrations */
7209 iwl4965_chain_noise_reset(priv);
7210 priv->start_calib = 1;
7211 #endif /* CONFIG_IWLWIFI_SENSITIVITY */
7213 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7214 priv->assoc_station_added = 1;
7216 #ifdef CONFIG_IWLWIFI_QOS
7217 iwl_activate_qos(priv, 0);
7218 #endif /* CONFIG_IWLWIFI_QOS */
7219 mutex_unlock(&priv->mutex);
7222 static void iwl_bg_abort_scan(struct work_struct *work)
7224 struct iwl_priv *priv = container_of(work, struct iwl_priv,
7227 if (!iwl_is_ready(priv))
7230 mutex_lock(&priv->mutex);
7232 set_bit(STATUS_SCAN_ABORTING, &priv->status);
7233 iwl_send_scan_abort(priv);
7235 mutex_unlock(&priv->mutex);
7238 static void iwl_bg_scan_completed(struct work_struct *work)
7240 struct iwl_priv *priv =
7241 container_of(work, struct iwl_priv, scan_completed);
7243 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
7245 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7248 ieee80211_scan_completed(priv->hw);
7250 /* Since setting the TXPOWER may have been deferred while
7251 * performing the scan, fire one off */
7252 mutex_lock(&priv->mutex);
7253 iwl_hw_reg_send_txpower(priv);
7254 mutex_unlock(&priv->mutex);
7257 /*****************************************************************************
7259 * mac80211 entry point functions
7261 *****************************************************************************/
7263 static int iwl_mac_start(struct ieee80211_hw *hw)
7265 struct iwl_priv *priv = hw->priv;
7267 IWL_DEBUG_MAC80211("enter\n");
7269 /* we should be verifying the device is ready to be opened */
7270 mutex_lock(&priv->mutex);
7274 if (!iwl_is_rfkill(priv))
7275 ieee80211_start_queues(priv->hw);
7277 mutex_unlock(&priv->mutex);
7278 IWL_DEBUG_MAC80211("leave\n");
7282 static void iwl_mac_stop(struct ieee80211_hw *hw)
7284 struct iwl_priv *priv = hw->priv;
7286 IWL_DEBUG_MAC80211("enter\n");
7289 mutex_lock(&priv->mutex);
7290 /* stop mac, cancel any scan request and clear
7291 * RXON_FILTER_ASSOC_MSK BIT
7294 iwl_scan_cancel_timeout(priv, 100);
7295 cancel_delayed_work(&priv->post_associate);
7296 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7297 iwl_commit_rxon(priv);
7298 mutex_unlock(&priv->mutex);
7300 IWL_DEBUG_MAC80211("leave\n");
7303 static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
7304 struct ieee80211_tx_control *ctl)
7306 struct iwl_priv *priv = hw->priv;
7308 IWL_DEBUG_MAC80211("enter\n");
7310 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7311 IWL_DEBUG_MAC80211("leave - monitor\n");
7315 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
7318 if (iwl_tx_skb(priv, skb, ctl))
7319 dev_kfree_skb_any(skb);
7321 IWL_DEBUG_MAC80211("leave\n");
7325 static int iwl_mac_add_interface(struct ieee80211_hw *hw,
7326 struct ieee80211_if_init_conf *conf)
7328 struct iwl_priv *priv = hw->priv;
7329 unsigned long flags;
7330 DECLARE_MAC_BUF(mac);
7332 IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
7334 if (priv->interface_id) {
7335 IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
7339 spin_lock_irqsave(&priv->lock, flags);
7340 priv->interface_id = conf->if_id;
7342 spin_unlock_irqrestore(&priv->lock, flags);
7344 mutex_lock(&priv->mutex);
7346 if (conf->mac_addr) {
7347 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7348 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7350 iwl_set_mode(priv, conf->type);
7352 IWL_DEBUG_MAC80211("leave\n");
7353 mutex_unlock(&priv->mutex);
7359 * iwl_mac_config - mac80211 config callback
7361 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7362 * be set inappropriately and the driver currently sets the hardware up to
7363 * use it whenever needed.
7365 static int iwl_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
7367 struct iwl_priv *priv = hw->priv;
7368 const struct iwl_channel_info *ch_info;
7369 unsigned long flags;
7371 mutex_lock(&priv->mutex);
7372 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
7374 if (!iwl_is_ready(priv)) {
7375 IWL_DEBUG_MAC80211("leave - not ready\n");
7376 mutex_unlock(&priv->mutex);
7380 /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
7381 * what is exposed through include/ declrations */
7382 if (unlikely(!iwl_param_disable_hw_scan &&
7383 test_bit(STATUS_SCANNING, &priv->status))) {
7384 IWL_DEBUG_MAC80211("leave - scanning\n");
7385 mutex_unlock(&priv->mutex);
7389 spin_lock_irqsave(&priv->lock, flags);
7391 ch_info = iwl_get_channel_info(priv, conf->phymode, conf->channel);
7392 if (!is_channel_valid(ch_info)) {
7393 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
7394 conf->channel, conf->phymode);
7395 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7396 spin_unlock_irqrestore(&priv->lock, flags);
7397 mutex_unlock(&priv->mutex);
7401 #ifdef CONFIG_IWLWIFI_HT
7402 /* if we are switching fron ht to 2.4 clear flags
7403 * from any ht related info since 2.4 does not
7405 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel)
7406 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
7407 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7410 priv->staging_rxon.flags = 0;
7411 #endif /* CONFIG_IWLWIFI_HT */
7413 iwl_set_rxon_channel(priv, conf->phymode, conf->channel);
7415 iwl_set_flags_for_phymode(priv, conf->phymode);
7417 /* The list of supported rates and rate mask can be different
7418 * for each phymode; since the phymode may have changed, reset
7419 * the rate mask to what mac80211 lists */
7422 spin_unlock_irqrestore(&priv->lock, flags);
7424 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
7425 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
7426 iwl_hw_channel_switch(priv, conf->channel);
7427 mutex_unlock(&priv->mutex);
7432 iwl_radio_kill_sw(priv, !conf->radio_enabled);
7434 if (!conf->radio_enabled) {
7435 IWL_DEBUG_MAC80211("leave - radio disabled\n");
7436 mutex_unlock(&priv->mutex);
7440 if (iwl_is_rfkill(priv)) {
7441 IWL_DEBUG_MAC80211("leave - RF kill\n");
7442 mutex_unlock(&priv->mutex);
7448 if (memcmp(&priv->active_rxon,
7449 &priv->staging_rxon, sizeof(priv->staging_rxon)))
7450 iwl_commit_rxon(priv);
7452 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7454 IWL_DEBUG_MAC80211("leave\n");
7456 mutex_unlock(&priv->mutex);
7461 static void iwl_config_ap(struct iwl_priv *priv)
7465 if (priv->status & STATUS_EXIT_PENDING)
7468 /* The following should be done only at AP bring up */
7469 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7471 /* RXON - unassoc (to set timing command) */
7472 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7473 iwl_commit_rxon(priv);
7476 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
7477 iwl_setup_rxon_timing(priv);
7478 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7479 sizeof(priv->rxon_timing), &priv->rxon_timing);
7481 IWL_WARNING("REPLY_RXON_TIMING failed - "
7482 "Attempting to continue.\n");
7484 iwl4965_set_rxon_chain(priv);
7486 /* FIXME: what should be the assoc_id for AP? */
7487 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7488 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7489 priv->staging_rxon.flags |=
7490 RXON_FLG_SHORT_PREAMBLE_MSK;
7492 priv->staging_rxon.flags &=
7493 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7495 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7496 if (priv->assoc_capability &
7497 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7498 priv->staging_rxon.flags |=
7499 RXON_FLG_SHORT_SLOT_MSK;
7501 priv->staging_rxon.flags &=
7502 ~RXON_FLG_SHORT_SLOT_MSK;
7504 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7505 priv->staging_rxon.flags &=
7506 ~RXON_FLG_SHORT_SLOT_MSK;
7508 /* restore RXON assoc */
7509 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7510 iwl_commit_rxon(priv);
7511 #ifdef CONFIG_IWLWIFI_QOS
7512 iwl_activate_qos(priv, 1);
7514 iwl_rxon_add_station(priv, BROADCAST_ADDR, 0);
7516 iwl_send_beacon_cmd(priv);
7518 /* FIXME - we need to add code here to detect a totally new
7519 * configuration, reset the AP, unassoc, rxon timing, assoc,
7520 * clear sta table, add BCAST sta... */
7523 static int iwl_mac_config_interface(struct ieee80211_hw *hw, int if_id,
7524 struct ieee80211_if_conf *conf)
7526 struct iwl_priv *priv = hw->priv;
7527 DECLARE_MAC_BUF(mac);
7528 unsigned long flags;
7534 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7535 (!conf->beacon || !conf->ssid_len)) {
7537 ("Leaving in AP mode because HostAPD is not ready.\n");
7541 mutex_lock(&priv->mutex);
7543 IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
7545 IWL_DEBUG_MAC80211("bssid: %s\n",
7546 print_mac(mac, conf->bssid));
7549 * very dubious code was here; the probe filtering flag is never set:
7551 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7552 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
7554 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
7555 IWL_DEBUG_MAC80211("leave - scanning\n");
7556 mutex_unlock(&priv->mutex);
7560 if (priv->interface_id != if_id) {
7561 IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
7562 mutex_unlock(&priv->mutex);
7566 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7568 conf->bssid = priv->mac_addr;
7569 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
7570 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7571 print_mac(mac, conf->bssid));
7573 if (priv->ibss_beacon)
7574 dev_kfree_skb(priv->ibss_beacon);
7576 priv->ibss_beacon = conf->beacon;
7579 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7580 !is_multicast_ether_addr(conf->bssid)) {
7581 /* If there is currently a HW scan going on in the background
7582 * then we need to cancel it else the RXON below will fail. */
7583 if (iwl_scan_cancel_timeout(priv, 100)) {
7584 IWL_WARNING("Aborted scan still in progress "
7586 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7587 mutex_unlock(&priv->mutex);
7590 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7592 /* TODO: Audit driver for usage of these members and see
7593 * if mac80211 deprecates them (priv->bssid looks like it
7594 * shouldn't be there, but I haven't scanned the IBSS code
7595 * to verify) - jpk */
7596 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7598 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7599 iwl_config_ap(priv);
7601 rc = iwl_commit_rxon(priv);
7602 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
7603 iwl_rxon_add_station(
7604 priv, priv->active_rxon.bssid_addr, 1);
7608 iwl_scan_cancel_timeout(priv, 100);
7609 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7610 iwl_commit_rxon(priv);
7613 spin_lock_irqsave(&priv->lock, flags);
7614 if (!conf->ssid_len)
7615 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7617 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7619 priv->essid_len = conf->ssid_len;
7620 spin_unlock_irqrestore(&priv->lock, flags);
7622 IWL_DEBUG_MAC80211("leave\n");
7623 mutex_unlock(&priv->mutex);
7628 static void iwl_configure_filter(struct ieee80211_hw *hw,
7629 unsigned int changed_flags,
7630 unsigned int *total_flags,
7631 int mc_count, struct dev_addr_list *mc_list)
7635 * see also iwl_connection_init_rx_config
7640 static void iwl_mac_remove_interface(struct ieee80211_hw *hw,
7641 struct ieee80211_if_init_conf *conf)
7643 struct iwl_priv *priv = hw->priv;
7645 IWL_DEBUG_MAC80211("enter\n");
7647 mutex_lock(&priv->mutex);
7649 iwl_scan_cancel_timeout(priv, 100);
7650 cancel_delayed_work(&priv->post_associate);
7651 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7652 iwl_commit_rxon(priv);
7654 if (priv->interface_id == conf->if_id) {
7655 priv->interface_id = 0;
7656 memset(priv->bssid, 0, ETH_ALEN);
7657 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7658 priv->essid_len = 0;
7660 mutex_unlock(&priv->mutex);
7662 IWL_DEBUG_MAC80211("leave\n");
7666 #define IWL_DELAY_NEXT_SCAN (HZ*2)
7667 static int iwl_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
7670 unsigned long flags;
7671 struct iwl_priv *priv = hw->priv;
7673 IWL_DEBUG_MAC80211("enter\n");
7675 mutex_lock(&priv->mutex);
7676 spin_lock_irqsave(&priv->lock, flags);
7678 if (!iwl_is_ready_rf(priv)) {
7680 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7684 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7686 IWL_ERROR("ERROR: APs don't scan\n");
7690 /* if we just finished scan ask for delay */
7691 if (priv->last_scan_jiffies &&
7692 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
7698 IWL_DEBUG_SCAN("direct scan for "
7700 iwl_escape_essid(ssid, len), (int)len);
7702 priv->one_direct_scan = 1;
7703 priv->direct_ssid_len = (u8)
7704 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7705 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
7707 priv->one_direct_scan = 0;
7709 rc = iwl_scan_initiate(priv);
7711 IWL_DEBUG_MAC80211("leave\n");
7714 spin_unlock_irqrestore(&priv->lock, flags);
7715 mutex_unlock(&priv->mutex);
7720 static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
7721 const u8 *local_addr, const u8 *addr,
7722 struct ieee80211_key_conf *key)
7724 struct iwl_priv *priv = hw->priv;
7725 DECLARE_MAC_BUF(mac);
7729 IWL_DEBUG_MAC80211("enter\n");
7731 if (!iwl_param_hwcrypto) {
7732 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7736 if (is_zero_ether_addr(addr))
7737 /* only support pairwise keys */
7740 sta_id = iwl_hw_find_station(priv, addr);
7741 if (sta_id == IWL_INVALID_STATION) {
7742 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7743 print_mac(mac, addr));
7747 mutex_lock(&priv->mutex);
7749 iwl_scan_cancel_timeout(priv, 100);
7753 rc = iwl_update_sta_key_info(priv, key, sta_id);
7755 iwl_set_rxon_hwcrypto(priv, 1);
7756 iwl_commit_rxon(priv);
7757 key->hw_key_idx = sta_id;
7758 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7759 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7763 rc = iwl_clear_sta_key_info(priv, sta_id);
7765 iwl_set_rxon_hwcrypto(priv, 0);
7766 iwl_commit_rxon(priv);
7767 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7774 IWL_DEBUG_MAC80211("leave\n");
7775 mutex_unlock(&priv->mutex);
7780 static int iwl_mac_conf_tx(struct ieee80211_hw *hw, int queue,
7781 const struct ieee80211_tx_queue_params *params)
7783 struct iwl_priv *priv = hw->priv;
7784 #ifdef CONFIG_IWLWIFI_QOS
7785 unsigned long flags;
7787 #endif /* CONFIG_IWL_QOS */
7789 IWL_DEBUG_MAC80211("enter\n");
7791 if (!iwl_is_ready_rf(priv)) {
7792 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7796 if (queue >= AC_NUM) {
7797 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7801 #ifdef CONFIG_IWLWIFI_QOS
7802 if (!priv->qos_data.qos_enable) {
7803 priv->qos_data.qos_active = 0;
7804 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7807 q = AC_NUM - 1 - queue;
7809 spin_lock_irqsave(&priv->lock, flags);
7811 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7812 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7813 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7814 priv->qos_data.def_qos_parm.ac[q].edca_txop =
7815 cpu_to_le16((params->burst_time * 100));
7817 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7818 priv->qos_data.qos_active = 1;
7820 spin_unlock_irqrestore(&priv->lock, flags);
7822 mutex_lock(&priv->mutex);
7823 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7824 iwl_activate_qos(priv, 1);
7825 else if (priv->assoc_id && iwl_is_associated(priv))
7826 iwl_activate_qos(priv, 0);
7828 mutex_unlock(&priv->mutex);
7830 #endif /*CONFIG_IWLWIFI_QOS */
7832 IWL_DEBUG_MAC80211("leave\n");
7836 static int iwl_mac_get_tx_stats(struct ieee80211_hw *hw,
7837 struct ieee80211_tx_queue_stats *stats)
7839 struct iwl_priv *priv = hw->priv;
7841 struct iwl_tx_queue *txq;
7842 struct iwl_queue *q;
7843 unsigned long flags;
7845 IWL_DEBUG_MAC80211("enter\n");
7847 if (!iwl_is_ready_rf(priv)) {
7848 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7852 spin_lock_irqsave(&priv->lock, flags);
7854 for (i = 0; i < AC_NUM; i++) {
7855 txq = &priv->txq[i];
7857 avail = iwl_queue_space(q);
7859 stats->data[i].len = q->n_window - avail;
7860 stats->data[i].limit = q->n_window - q->high_mark;
7861 stats->data[i].count = q->n_window;
7864 spin_unlock_irqrestore(&priv->lock, flags);
7866 IWL_DEBUG_MAC80211("leave\n");
7871 static int iwl_mac_get_stats(struct ieee80211_hw *hw,
7872 struct ieee80211_low_level_stats *stats)
7874 IWL_DEBUG_MAC80211("enter\n");
7875 IWL_DEBUG_MAC80211("leave\n");
7880 static u64 iwl_mac_get_tsf(struct ieee80211_hw *hw)
7882 IWL_DEBUG_MAC80211("enter\n");
7883 IWL_DEBUG_MAC80211("leave\n");
7888 static void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
7890 struct iwl_priv *priv = hw->priv;
7891 unsigned long flags;
7893 mutex_lock(&priv->mutex);
7894 IWL_DEBUG_MAC80211("enter\n");
7896 priv->lq_mngr.lq_ready = 0;
7897 #ifdef CONFIG_IWLWIFI_HT
7898 spin_lock_irqsave(&priv->lock, flags);
7899 memset(&priv->current_assoc_ht, 0, sizeof(struct sta_ht_info));
7900 spin_unlock_irqrestore(&priv->lock, flags);
7901 #ifdef CONFIG_IWLWIFI_HT_AGG
7902 /* if (priv->lq_mngr.agg_ctrl.granted_ba)
7903 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);*/
7905 memset(&(priv->lq_mngr.agg_ctrl), 0, sizeof(struct iwl_agg_control));
7906 priv->lq_mngr.agg_ctrl.tid_traffic_load_threshold = 10;
7907 priv->lq_mngr.agg_ctrl.ba_timeout = 5000;
7908 priv->lq_mngr.agg_ctrl.auto_agg = 1;
7910 if (priv->lq_mngr.agg_ctrl.auto_agg)
7911 priv->lq_mngr.agg_ctrl.requested_ba = TID_ALL_ENABLED;
7912 #endif /*CONFIG_IWLWIFI_HT_AGG */
7913 #endif /* CONFIG_IWLWIFI_HT */
7915 #ifdef CONFIG_IWLWIFI_QOS
7916 iwl_reset_qos(priv);
7919 cancel_delayed_work(&priv->post_associate);
7921 spin_lock_irqsave(&priv->lock, flags);
7923 priv->assoc_capability = 0;
7924 priv->call_post_assoc_from_beacon = 0;
7925 priv->assoc_station_added = 0;
7927 /* new association get rid of ibss beacon skb */
7928 if (priv->ibss_beacon)
7929 dev_kfree_skb(priv->ibss_beacon);
7931 priv->ibss_beacon = NULL;
7933 priv->beacon_int = priv->hw->conf.beacon_int;
7934 priv->timestamp1 = 0;
7935 priv->timestamp0 = 0;
7936 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7937 priv->beacon_int = 0;
7939 spin_unlock_irqrestore(&priv->lock, flags);
7941 /* we are restarting association process
7942 * clear RXON_FILTER_ASSOC_MSK bit
7944 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
7945 iwl_scan_cancel_timeout(priv, 100);
7946 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7947 iwl_commit_rxon(priv);
7950 /* Per mac80211.h: This is only used in IBSS mode... */
7951 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7953 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7954 mutex_unlock(&priv->mutex);
7958 if (!iwl_is_ready_rf(priv)) {
7959 IWL_DEBUG_MAC80211("leave - not ready\n");
7960 mutex_unlock(&priv->mutex);
7964 priv->only_active_channel = 0;
7968 mutex_unlock(&priv->mutex);
7970 IWL_DEBUG_MAC80211("leave\n");
7974 static int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
7975 struct ieee80211_tx_control *control)
7977 struct iwl_priv *priv = hw->priv;
7978 unsigned long flags;
7980 mutex_lock(&priv->mutex);
7981 IWL_DEBUG_MAC80211("enter\n");
7983 if (!iwl_is_ready_rf(priv)) {
7984 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7985 mutex_unlock(&priv->mutex);
7989 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7990 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7991 mutex_unlock(&priv->mutex);
7995 spin_lock_irqsave(&priv->lock, flags);
7997 if (priv->ibss_beacon)
7998 dev_kfree_skb(priv->ibss_beacon);
8000 priv->ibss_beacon = skb;
8004 IWL_DEBUG_MAC80211("leave\n");
8005 spin_unlock_irqrestore(&priv->lock, flags);
8007 #ifdef CONFIG_IWLWIFI_QOS
8008 iwl_reset_qos(priv);
8011 queue_work(priv->workqueue, &priv->post_associate.work);
8013 mutex_unlock(&priv->mutex);
8018 #ifdef CONFIG_IWLWIFI_HT
8021 u16 advanced_coding_cap :1;
8022 u16 supported_chan_width_set :1;
8023 u16 mimo_power_save_mode :2;
8029 u16 beam_forming :1;
8031 u16 maximal_amsdu_size :1;
8032 u16 cck_mode_at_40MHz :1;
8033 u16 psmp_support :1;
8034 u16 stbc_ctrl_frame_support :1;
8035 u16 sig_txop_protection_support :1;
8038 } __attribute__ ((packed));
8040 union ht_param_info{
8042 u8 max_rx_ampdu_factor :2;
8047 } __attribute__ ((packed));
8049 union ht_exra_param_info {
8051 u8 ext_chan_offset :2;
8052 u8 tx_chan_width :1;
8054 u8 controlled_access_only :1;
8055 u8 service_interval_granularity :3;
8058 } __attribute__ ((packed));
8060 union ht_operation_mode{
8067 } __attribute__ ((packed));
8070 static int sta_ht_info_init(struct ieee80211_ht_capability *ht_cap,
8071 struct ieee80211_ht_additional_info *ht_extra,
8072 struct sta_ht_info *ht_info_ap,
8073 struct sta_ht_info *ht_info)
8075 union ht_cap_info cap;
8076 union ht_operation_mode op_mode;
8077 union ht_param_info param_info;
8078 union ht_exra_param_info extra_param_info;
8080 IWL_DEBUG_MAC80211("enter: \n");
8083 IWL_DEBUG_MAC80211("leave: ht_info is NULL\n");
8088 cap.val = (u16) le16_to_cpu(ht_cap->capabilities_info);
8089 param_info.val = ht_cap->mac_ht_params_info;
8092 ht_info->sgf |= 0x1;
8094 ht_info->sgf |= 0x2;
8095 ht_info->is_green_field = cap.green_field;
8096 ht_info->max_amsdu_size = cap.maximal_amsdu_size;
8097 ht_info->supported_chan_width = cap.supported_chan_width_set;
8098 ht_info->tx_mimo_ps_mode = cap.mimo_power_save_mode;
8099 memcpy(ht_info->supp_rates, ht_cap->supported_mcs_set, 16);
8101 ht_info->ampdu_factor = param_info.max_rx_ampdu_factor;
8102 ht_info->mpdu_density = param_info.mpdu_density;
8104 IWL_DEBUG_MAC80211("SISO mask 0x%X MIMO mask 0x%X \n",
8105 ht_cap->supported_mcs_set[0],
8106 ht_cap->supported_mcs_set[1]);
8109 ht_info->control_channel = ht_info_ap->control_channel;
8110 ht_info->extension_chan_offset =
8111 ht_info_ap->extension_chan_offset;
8112 ht_info->tx_chan_width = ht_info_ap->tx_chan_width;
8113 ht_info->operating_mode = ht_info_ap->operating_mode;
8117 extra_param_info.val = ht_extra->ht_param;
8118 ht_info->control_channel = ht_extra->control_chan;
8119 ht_info->extension_chan_offset =
8120 extra_param_info.ext_chan_offset;
8121 ht_info->tx_chan_width = extra_param_info.tx_chan_width;
8123 le16_to_cpu(ht_extra->operation_mode);
8124 ht_info->operating_mode = op_mode.op_mode;
8125 IWL_DEBUG_MAC80211("control channel %d\n",
8126 ht_extra->control_chan);
8131 IWL_DEBUG_MAC80211("leave\n");
8135 static int iwl_mac_conf_ht(struct ieee80211_hw *hw,
8136 struct ieee80211_ht_capability *ht_cap,
8137 struct ieee80211_ht_additional_info *ht_extra)
8139 struct iwl_priv *priv = hw->priv;
8142 IWL_DEBUG_MAC80211("enter: \n");
8144 rs = sta_ht_info_init(ht_cap, ht_extra, NULL, &priv->current_assoc_ht);
8145 iwl4965_set_rxon_chain(priv);
8147 if (priv && priv->assoc_id &&
8148 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
8149 unsigned long flags;
8151 spin_lock_irqsave(&priv->lock, flags);
8152 if (priv->beacon_int)
8153 queue_work(priv->workqueue, &priv->post_associate.work);
8155 priv->call_post_assoc_from_beacon = 1;
8156 spin_unlock_irqrestore(&priv->lock, flags);
8159 IWL_DEBUG_MAC80211("leave: control channel %d\n",
8160 ht_extra->control_chan);
8165 static void iwl_set_ht_capab(struct ieee80211_hw *hw,
8166 struct ieee80211_ht_capability *ht_cap,
8169 union ht_cap_info cap;
8170 union ht_param_info param_info;
8172 memset(&cap, 0, sizeof(union ht_cap_info));
8173 memset(¶m_info, 0, sizeof(union ht_param_info));
8175 cap.maximal_amsdu_size = HT_IE_MAX_AMSDU_SIZE_4K;
8176 cap.green_field = 1;
8179 cap.supported_chan_width_set = use_wide_chan;
8180 cap.mimo_power_save_mode = 0x3;
8182 param_info.max_rx_ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
8183 param_info.mpdu_density = CFG_HT_MPDU_DENSITY_DEF;
8184 ht_cap->capabilities_info = (__le16) cpu_to_le16(cap.val);
8185 ht_cap->mac_ht_params_info = (u8) param_info.val;
8187 ht_cap->supported_mcs_set[0] = 0xff;
8188 ht_cap->supported_mcs_set[1] = 0xff;
8189 ht_cap->supported_mcs_set[4] =
8190 (cap.supported_chan_width_set) ? 0x1: 0x0;
8193 static void iwl_mac_get_ht_capab(struct ieee80211_hw *hw,
8194 struct ieee80211_ht_capability *ht_cap)
8196 u8 use_wide_channel = 1;
8197 struct iwl_priv *priv = hw->priv;
8199 IWL_DEBUG_MAC80211("enter: \n");
8200 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
8201 use_wide_channel = 0;
8203 /* no fat tx allowed on 2.4GHZ */
8204 if (priv->phymode != MODE_IEEE80211A)
8205 use_wide_channel = 0;
8207 iwl_set_ht_capab(hw, ht_cap, use_wide_channel);
8208 IWL_DEBUG_MAC80211("leave: \n");
8210 #endif /*CONFIG_IWLWIFI_HT*/
8212 /*****************************************************************************
8216 *****************************************************************************/
8218 #ifdef CONFIG_IWLWIFI_DEBUG
8221 * The following adds a new attribute to the sysfs representation
8222 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
8223 * used for controlling the debug level.
8225 * See the level definitions in iwl for details.
8228 static ssize_t show_debug_level(struct device_driver *d, char *buf)
8230 return sprintf(buf, "0x%08X\n", iwl_debug_level);
8232 static ssize_t store_debug_level(struct device_driver *d,
8233 const char *buf, size_t count)
8235 char *p = (char *)buf;
8238 val = simple_strtoul(p, &p, 0);
8240 printk(KERN_INFO DRV_NAME
8241 ": %s is not in hex or decimal form.\n", buf);
8243 iwl_debug_level = val;
8245 return strnlen(buf, count);
8248 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
8249 show_debug_level, store_debug_level);
8251 #endif /* CONFIG_IWLWIFI_DEBUG */
8253 static ssize_t show_rf_kill(struct device *d,
8254 struct device_attribute *attr, char *buf)
8257 * 0 - RF kill not enabled
8258 * 1 - SW based RF kill active (sysfs)
8259 * 2 - HW based RF kill active
8260 * 3 - Both HW and SW based RF kill active
8262 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8263 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
8264 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
8266 return sprintf(buf, "%i\n", val);
8269 static ssize_t store_rf_kill(struct device *d,
8270 struct device_attribute *attr,
8271 const char *buf, size_t count)
8273 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8275 mutex_lock(&priv->mutex);
8276 iwl_radio_kill_sw(priv, buf[0] == '1');
8277 mutex_unlock(&priv->mutex);
8282 static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
8284 static ssize_t show_temperature(struct device *d,
8285 struct device_attribute *attr, char *buf)
8287 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8289 if (!iwl_is_alive(priv))
8292 return sprintf(buf, "%d\n", iwl_hw_get_temperature(priv));
8295 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8297 static ssize_t show_rs_window(struct device *d,
8298 struct device_attribute *attr,
8301 struct iwl_priv *priv = d->driver_data;
8302 return iwl_fill_rs_info(priv->hw, buf, IWL_AP_ID);
8304 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8306 static ssize_t show_tx_power(struct device *d,
8307 struct device_attribute *attr, char *buf)
8309 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8310 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8313 static ssize_t store_tx_power(struct device *d,
8314 struct device_attribute *attr,
8315 const char *buf, size_t count)
8317 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8318 char *p = (char *)buf;
8321 val = simple_strtoul(p, &p, 10);
8323 printk(KERN_INFO DRV_NAME
8324 ": %s is not in decimal form.\n", buf);
8326 iwl_hw_reg_set_txpower(priv, val);
8331 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8333 static ssize_t show_flags(struct device *d,
8334 struct device_attribute *attr, char *buf)
8336 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8338 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8341 static ssize_t store_flags(struct device *d,
8342 struct device_attribute *attr,
8343 const char *buf, size_t count)
8345 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8346 u32 flags = simple_strtoul(buf, NULL, 0);
8348 mutex_lock(&priv->mutex);
8349 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8350 /* Cancel any currently running scans... */
8351 if (iwl_scan_cancel_timeout(priv, 100))
8352 IWL_WARNING("Could not cancel scan.\n");
8354 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8356 priv->staging_rxon.flags = cpu_to_le32(flags);
8357 iwl_commit_rxon(priv);
8360 mutex_unlock(&priv->mutex);
8365 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8367 static ssize_t show_filter_flags(struct device *d,
8368 struct device_attribute *attr, char *buf)
8370 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8372 return sprintf(buf, "0x%04X\n",
8373 le32_to_cpu(priv->active_rxon.filter_flags));
8376 static ssize_t store_filter_flags(struct device *d,
8377 struct device_attribute *attr,
8378 const char *buf, size_t count)
8380 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8381 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8383 mutex_lock(&priv->mutex);
8384 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8385 /* Cancel any currently running scans... */
8386 if (iwl_scan_cancel_timeout(priv, 100))
8387 IWL_WARNING("Could not cancel scan.\n");
8389 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8390 "0x%04X\n", filter_flags);
8391 priv->staging_rxon.filter_flags =
8392 cpu_to_le32(filter_flags);
8393 iwl_commit_rxon(priv);
8396 mutex_unlock(&priv->mutex);
8401 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8402 store_filter_flags);
8404 static ssize_t show_tune(struct device *d,
8405 struct device_attribute *attr, char *buf)
8407 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8409 return sprintf(buf, "0x%04X\n",
8410 (priv->phymode << 8) |
8411 le16_to_cpu(priv->active_rxon.channel));
8414 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode);
8416 static ssize_t store_tune(struct device *d,
8417 struct device_attribute *attr,
8418 const char *buf, size_t count)
8420 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8421 char *p = (char *)buf;
8422 u16 tune = simple_strtoul(p, &p, 0);
8423 u8 phymode = (tune >> 8) & 0xff;
8424 u16 channel = tune & 0xff;
8426 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
8428 mutex_lock(&priv->mutex);
8429 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
8430 (priv->phymode != phymode)) {
8431 const struct iwl_channel_info *ch_info;
8433 ch_info = iwl_get_channel_info(priv, phymode, channel);
8435 IWL_WARNING("Requested invalid phymode/channel "
8436 "combination: %d %d\n", phymode, channel);
8437 mutex_unlock(&priv->mutex);
8441 /* Cancel any currently running scans... */
8442 if (iwl_scan_cancel_timeout(priv, 100))
8443 IWL_WARNING("Could not cancel scan.\n");
8445 IWL_DEBUG_INFO("Committing phymode and "
8446 "rxon.channel = %d %d\n",
8449 iwl_set_rxon_channel(priv, phymode, channel);
8450 iwl_set_flags_for_phymode(priv, phymode);
8453 iwl_commit_rxon(priv);
8456 mutex_unlock(&priv->mutex);
8461 static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
8463 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8465 static ssize_t show_measurement(struct device *d,
8466 struct device_attribute *attr, char *buf)
8468 struct iwl_priv *priv = dev_get_drvdata(d);
8469 struct iwl_spectrum_notification measure_report;
8470 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8471 u8 *data = (u8 *) & measure_report;
8472 unsigned long flags;
8474 spin_lock_irqsave(&priv->lock, flags);
8475 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8476 spin_unlock_irqrestore(&priv->lock, flags);
8479 memcpy(&measure_report, &priv->measure_report, size);
8480 priv->measurement_status = 0;
8481 spin_unlock_irqrestore(&priv->lock, flags);
8483 while (size && (PAGE_SIZE - len)) {
8484 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8485 PAGE_SIZE - len, 1);
8487 if (PAGE_SIZE - len)
8491 size -= min(size, 16U);
8497 static ssize_t store_measurement(struct device *d,
8498 struct device_attribute *attr,
8499 const char *buf, size_t count)
8501 struct iwl_priv *priv = dev_get_drvdata(d);
8502 struct ieee80211_measurement_params params = {
8503 .channel = le16_to_cpu(priv->active_rxon.channel),
8504 .start_time = cpu_to_le64(priv->last_tsf),
8505 .duration = cpu_to_le16(1),
8507 u8 type = IWL_MEASURE_BASIC;
8513 strncpy(buffer, buf, min(sizeof(buffer), count));
8514 channel = simple_strtoul(p, NULL, 0);
8516 params.channel = channel;
8519 while (*p && *p != ' ')
8522 type = simple_strtoul(p + 1, NULL, 0);
8525 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8526 "channel %d (for '%s')\n", type, params.channel, buf);
8527 iwl_get_measurement(priv, ¶ms, type);
8532 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8533 show_measurement, store_measurement);
8534 #endif /* CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT */
8536 static ssize_t store_retry_rate(struct device *d,
8537 struct device_attribute *attr,
8538 const char *buf, size_t count)
8540 struct iwl_priv *priv = dev_get_drvdata(d);
8542 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8543 if (priv->retry_rate <= 0)
8544 priv->retry_rate = 1;
8549 static ssize_t show_retry_rate(struct device *d,
8550 struct device_attribute *attr, char *buf)
8552 struct iwl_priv *priv = dev_get_drvdata(d);
8553 return sprintf(buf, "%d", priv->retry_rate);
8556 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8559 static ssize_t store_power_level(struct device *d,
8560 struct device_attribute *attr,
8561 const char *buf, size_t count)
8563 struct iwl_priv *priv = dev_get_drvdata(d);
8567 mode = simple_strtoul(buf, NULL, 0);
8568 mutex_lock(&priv->mutex);
8570 if (!iwl_is_ready(priv)) {
8575 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8576 mode = IWL_POWER_AC;
8578 mode |= IWL_POWER_ENABLED;
8580 if (mode != priv->power_mode) {
8581 rc = iwl_send_power_mode(priv, IWL_POWER_LEVEL(mode));
8583 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8586 priv->power_mode = mode;
8592 mutex_unlock(&priv->mutex);
8596 #define MAX_WX_STRING 80
8598 /* Values are in microsecond */
8599 static const s32 timeout_duration[] = {
8606 static const s32 period_duration[] = {
8614 static ssize_t show_power_level(struct device *d,
8615 struct device_attribute *attr, char *buf)
8617 struct iwl_priv *priv = dev_get_drvdata(d);
8618 int level = IWL_POWER_LEVEL(priv->power_mode);
8621 p += sprintf(p, "%d ", level);
8623 case IWL_POWER_MODE_CAM:
8625 p += sprintf(p, "(AC)");
8627 case IWL_POWER_BATTERY:
8628 p += sprintf(p, "(BATTERY)");
8632 "(Timeout %dms, Period %dms)",
8633 timeout_duration[level - 1] / 1000,
8634 period_duration[level - 1] / 1000);
8637 if (!(priv->power_mode & IWL_POWER_ENABLED))
8638 p += sprintf(p, " OFF\n");
8640 p += sprintf(p, " \n");
8642 return (p - buf + 1);
8646 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8649 static ssize_t show_channels(struct device *d,
8650 struct device_attribute *attr, char *buf)
8652 struct iwl_priv *priv = dev_get_drvdata(d);
8654 struct ieee80211_channel *channels = NULL;
8655 const struct ieee80211_hw_mode *hw_mode = NULL;
8658 if (!iwl_is_ready(priv))
8661 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211G);
8663 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211B);
8665 channels = hw_mode->channels;
8666 count = hw_mode->num_channels;
8671 "Displaying %d channels in 2.4GHz band "
8672 "(802.11bg):\n", count);
8674 for (i = 0; i < count; i++)
8675 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8677 channels[i].power_level,
8679 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8680 " (IEEE 802.11h required)" : "",
8681 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8684 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8687 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8688 "active/passive" : "passive only");
8690 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211A);
8692 channels = hw_mode->channels;
8693 count = hw_mode->num_channels;
8699 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8700 "(802.11a):\n", count);
8702 for (i = 0; i < count; i++)
8703 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8705 channels[i].power_level,
8707 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8708 " (IEEE 802.11h required)" : "",
8709 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8712 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8715 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8716 "active/passive" : "passive only");
8721 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8723 static ssize_t show_statistics(struct device *d,
8724 struct device_attribute *attr, char *buf)
8726 struct iwl_priv *priv = dev_get_drvdata(d);
8727 u32 size = sizeof(struct iwl_notif_statistics);
8728 u32 len = 0, ofs = 0;
8729 u8 *data = (u8 *) & priv->statistics;
8732 if (!iwl_is_alive(priv))
8735 mutex_lock(&priv->mutex);
8736 rc = iwl_send_statistics_request(priv);
8737 mutex_unlock(&priv->mutex);
8741 "Error sending statistics request: 0x%08X\n", rc);
8745 while (size && (PAGE_SIZE - len)) {
8746 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8747 PAGE_SIZE - len, 1);
8749 if (PAGE_SIZE - len)
8753 size -= min(size, 16U);
8759 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8761 static ssize_t show_antenna(struct device *d,
8762 struct device_attribute *attr, char *buf)
8764 struct iwl_priv *priv = dev_get_drvdata(d);
8766 if (!iwl_is_alive(priv))
8769 return sprintf(buf, "%d\n", priv->antenna);
8772 static ssize_t store_antenna(struct device *d,
8773 struct device_attribute *attr,
8774 const char *buf, size_t count)
8777 struct iwl_priv *priv = dev_get_drvdata(d);
8782 if (sscanf(buf, "%1i", &ant) != 1) {
8783 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8787 if ((ant >= 0) && (ant <= 2)) {
8788 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
8789 priv->antenna = (enum iwl_antenna)ant;
8791 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8797 static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8799 static ssize_t show_status(struct device *d,
8800 struct device_attribute *attr, char *buf)
8802 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8803 if (!iwl_is_alive(priv))
8805 return sprintf(buf, "0x%08x\n", (int)priv->status);
8808 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8810 static ssize_t dump_error_log(struct device *d,
8811 struct device_attribute *attr,
8812 const char *buf, size_t count)
8814 char *p = (char *)buf;
8817 iwl_dump_nic_error_log((struct iwl_priv *)d->driver_data);
8819 return strnlen(buf, count);
8822 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8824 static ssize_t dump_event_log(struct device *d,
8825 struct device_attribute *attr,
8826 const char *buf, size_t count)
8828 char *p = (char *)buf;
8831 iwl_dump_nic_event_log((struct iwl_priv *)d->driver_data);
8833 return strnlen(buf, count);
8836 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8838 /*****************************************************************************
8840 * driver setup and teardown
8842 *****************************************************************************/
8844 static void iwl_setup_deferred_work(struct iwl_priv *priv)
8846 priv->workqueue = create_workqueue(DRV_NAME);
8848 init_waitqueue_head(&priv->wait_command_queue);
8850 INIT_WORK(&priv->up, iwl_bg_up);
8851 INIT_WORK(&priv->restart, iwl_bg_restart);
8852 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
8853 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
8854 INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
8855 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
8856 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
8857 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
8858 INIT_DELAYED_WORK(&priv->post_associate, iwl_bg_post_associate);
8859 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
8860 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
8861 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
8863 iwl_hw_setup_deferred_work(priv);
8865 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
8866 iwl_irq_tasklet, (unsigned long)priv);
8869 static void iwl_cancel_deferred_work(struct iwl_priv *priv)
8871 iwl_hw_cancel_deferred_work(priv);
8873 cancel_delayed_work_sync(&priv->init_alive_start);
8874 cancel_delayed_work(&priv->scan_check);
8875 cancel_delayed_work(&priv->alive_start);
8876 cancel_delayed_work(&priv->post_associate);
8877 cancel_work_sync(&priv->beacon_update);
8880 static struct attribute *iwl_sysfs_entries[] = {
8881 &dev_attr_antenna.attr,
8882 &dev_attr_channels.attr,
8883 &dev_attr_dump_errors.attr,
8884 &dev_attr_dump_events.attr,
8885 &dev_attr_flags.attr,
8886 &dev_attr_filter_flags.attr,
8887 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8888 &dev_attr_measurement.attr,
8890 &dev_attr_power_level.attr,
8891 &dev_attr_retry_rate.attr,
8892 &dev_attr_rf_kill.attr,
8893 &dev_attr_rs_window.attr,
8894 &dev_attr_statistics.attr,
8895 &dev_attr_status.attr,
8896 &dev_attr_temperature.attr,
8897 &dev_attr_tune.attr,
8898 &dev_attr_tx_power.attr,
8903 static struct attribute_group iwl_attribute_group = {
8904 .name = NULL, /* put in device directory */
8905 .attrs = iwl_sysfs_entries,
8908 static struct ieee80211_ops iwl_hw_ops = {
8910 .start = iwl_mac_start,
8911 .stop = iwl_mac_stop,
8912 .add_interface = iwl_mac_add_interface,
8913 .remove_interface = iwl_mac_remove_interface,
8914 .config = iwl_mac_config,
8915 .config_interface = iwl_mac_config_interface,
8916 .configure_filter = iwl_configure_filter,
8917 .set_key = iwl_mac_set_key,
8918 .get_stats = iwl_mac_get_stats,
8919 .get_tx_stats = iwl_mac_get_tx_stats,
8920 .conf_tx = iwl_mac_conf_tx,
8921 .get_tsf = iwl_mac_get_tsf,
8922 .reset_tsf = iwl_mac_reset_tsf,
8923 .beacon_update = iwl_mac_beacon_update,
8924 #ifdef CONFIG_IWLWIFI_HT
8925 .conf_ht = iwl_mac_conf_ht,
8926 .get_ht_capab = iwl_mac_get_ht_capab,
8927 #ifdef CONFIG_IWLWIFI_HT_AGG
8928 .ht_tx_agg_start = iwl_mac_ht_tx_agg_start,
8929 .ht_tx_agg_stop = iwl_mac_ht_tx_agg_stop,
8930 .ht_rx_agg_start = iwl_mac_ht_rx_agg_start,
8931 .ht_rx_agg_stop = iwl_mac_ht_rx_agg_stop,
8932 #endif /* CONFIG_IWLWIFI_HT_AGG */
8933 #endif /* CONFIG_IWLWIFI_HT */
8934 .hw_scan = iwl_mac_hw_scan
8937 static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8940 struct iwl_priv *priv;
8941 struct ieee80211_hw *hw;
8944 if (iwl_param_disable_hw_scan) {
8945 IWL_DEBUG_INFO("Disabling hw_scan\n");
8946 iwl_hw_ops.hw_scan = NULL;
8949 if ((iwl_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8950 (iwl_param_queues_num < IWL_MIN_NUM_QUEUES)) {
8951 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8952 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8957 /* mac80211 allocates memory for this device instance, including
8958 * space for this driver's private structure */
8959 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl_hw_ops);
8961 IWL_ERROR("Can not allocate network device\n");
8965 SET_IEEE80211_DEV(hw, &pdev->dev);
8967 hw->rate_control_algorithm = "iwl-4965-rs";
8969 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8973 priv->pci_dev = pdev;
8974 priv->antenna = (enum iwl_antenna)iwl_param_antenna;
8975 #ifdef CONFIG_IWLWIFI_DEBUG
8976 iwl_debug_level = iwl_param_debug;
8977 atomic_set(&priv->restrict_refcnt, 0);
8979 priv->retry_rate = 1;
8981 priv->ibss_beacon = NULL;
8983 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8984 * the range of signal quality values that we'll provide.
8985 * Negative values for level/noise indicate that we'll provide dBm.
8986 * For WE, at least, non-0 values here *enable* display of values
8987 * in app (iwconfig). */
8988 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8989 hw->max_noise = -20; /* noise level, negative indicates dBm */
8990 hw->max_signal = 100; /* link quality indication (%) */
8992 /* Tell mac80211 our Tx characteristics */
8993 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8996 #ifdef CONFIG_IWLWIFI_HT
8997 #ifdef CONFIG_IWLWIFI_HT_AGG
8999 #endif /* CONFIG_IWLWIFI_HT_AGG */
9000 #endif /* CONFIG_IWLWIFI_HT */
9002 spin_lock_init(&priv->lock);
9003 spin_lock_init(&priv->power_data.lock);
9004 spin_lock_init(&priv->sta_lock);
9005 spin_lock_init(&priv->hcmd_lock);
9006 spin_lock_init(&priv->lq_mngr.lock);
9008 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
9009 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
9011 INIT_LIST_HEAD(&priv->free_frames);
9013 mutex_init(&priv->mutex);
9014 if (pci_enable_device(pdev)) {
9016 goto out_ieee80211_free_hw;
9019 pci_set_master(pdev);
9021 iwl_clear_stations_table(priv);
9023 priv->data_retry_limit = -1;
9024 priv->ieee_channels = NULL;
9025 priv->ieee_rates = NULL;
9028 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
9030 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
9032 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
9033 goto out_pci_disable_device;
9036 pci_set_drvdata(pdev, priv);
9037 err = pci_request_regions(pdev, DRV_NAME);
9039 goto out_pci_disable_device;
9040 /* We disable the RETRY_TIMEOUT register (0x41) to keep
9041 * PCI Tx retries from interfering with C3 CPU state */
9042 pci_write_config_byte(pdev, 0x41, 0x00);
9043 priv->hw_base = pci_iomap(pdev, 0, 0);
9044 if (!priv->hw_base) {
9046 goto out_pci_release_regions;
9049 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
9050 (unsigned long long) pci_resource_len(pdev, 0));
9051 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
9053 /* Initialize module parameter values here */
9055 if (iwl_param_disable) {
9056 set_bit(STATUS_RF_KILL_SW, &priv->status);
9057 IWL_DEBUG_INFO("Radio disabled.\n");
9060 priv->iw_mode = IEEE80211_IF_TYPE_STA;
9063 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
9064 priv->is_ht_enabled = 1;
9065 priv->channel_width = IWL_CHANNEL_WIDTH_40MHZ;
9066 priv->valid_antenna = 0x7; /* assume all 3 connected */
9067 priv->ps_mode = IWL_MIMO_PS_NONE;
9068 priv->cck_power_index_compensation = iwl_read32(
9069 priv, CSR_HW_REV_WA_REG);
9071 iwl4965_set_rxon_chain(priv);
9073 printk(KERN_INFO DRV_NAME
9074 ": Detected Intel Wireless WiFi Link 4965AGN\n");
9076 /* Device-specific setup */
9077 if (iwl_hw_set_hw_setting(priv)) {
9078 IWL_ERROR("failed to set hw settings\n");
9079 mutex_unlock(&priv->mutex);
9083 #ifdef CONFIG_IWLWIFI_QOS
9084 if (iwl_param_qos_enable)
9085 priv->qos_data.qos_enable = 1;
9087 iwl_reset_qos(priv);
9089 priv->qos_data.qos_active = 0;
9090 priv->qos_data.qos_cap.val = 0;
9091 #endif /* CONFIG_IWLWIFI_QOS */
9093 iwl_set_rxon_channel(priv, MODE_IEEE80211G, 6);
9094 iwl_setup_deferred_work(priv);
9095 iwl_setup_rx_handlers(priv);
9097 priv->rates_mask = IWL_RATES_MASK;
9098 /* If power management is turned on, default to AC mode */
9099 priv->power_mode = IWL_POWER_AC;
9100 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
9102 pci_enable_msi(pdev);
9104 err = request_irq(pdev->irq, iwl_isr, IRQF_SHARED, DRV_NAME, priv);
9106 IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
9107 goto out_disable_msi;
9110 mutex_lock(&priv->mutex);
9112 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
9114 IWL_ERROR("failed to create sysfs device attributes\n");
9115 mutex_unlock(&priv->mutex);
9116 goto out_release_irq;
9119 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
9120 * ucode filename and max sizes are card-specific. */
9121 err = iwl_read_ucode(priv);
9123 IWL_ERROR("Could not read microcode: %d\n", err);
9124 mutex_unlock(&priv->mutex);
9128 mutex_unlock(&priv->mutex);
9130 IWL_DEBUG_INFO("Queing UP work.\n");
9132 queue_work(priv->workqueue, &priv->up);
9137 iwl_dealloc_ucode_pci(priv);
9139 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
9142 free_irq(pdev->irq, priv);
9145 pci_disable_msi(pdev);
9146 destroy_workqueue(priv->workqueue);
9147 priv->workqueue = NULL;
9148 iwl_unset_hw_setting(priv);
9151 pci_iounmap(pdev, priv->hw_base);
9152 out_pci_release_regions:
9153 pci_release_regions(pdev);
9154 out_pci_disable_device:
9155 pci_disable_device(pdev);
9156 pci_set_drvdata(pdev, NULL);
9157 out_ieee80211_free_hw:
9158 ieee80211_free_hw(priv->hw);
9163 static void iwl_pci_remove(struct pci_dev *pdev)
9165 struct iwl_priv *priv = pci_get_drvdata(pdev);
9166 struct list_head *p, *q;
9172 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
9174 mutex_lock(&priv->mutex);
9175 set_bit(STATUS_EXIT_PENDING, &priv->status);
9177 mutex_unlock(&priv->mutex);
9179 /* Free MAC hash list for ADHOC */
9180 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
9181 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
9183 kfree(list_entry(p, struct iwl_ibss_seq, list));
9187 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
9189 iwl_dealloc_ucode_pci(priv);
9192 iwl_rx_queue_free(priv, &priv->rxq);
9193 iwl_hw_txq_ctx_free(priv);
9195 iwl_unset_hw_setting(priv);
9196 iwl_clear_stations_table(priv);
9198 if (priv->mac80211_registered) {
9199 ieee80211_unregister_hw(priv->hw);
9200 iwl_rate_control_unregister(priv->hw);
9203 /*netif_stop_queue(dev); */
9204 flush_workqueue(priv->workqueue);
9206 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
9207 * priv->workqueue... so we can't take down the workqueue
9209 destroy_workqueue(priv->workqueue);
9210 priv->workqueue = NULL;
9212 free_irq(pdev->irq, priv);
9213 pci_disable_msi(pdev);
9214 pci_iounmap(pdev, priv->hw_base);
9215 pci_release_regions(pdev);
9216 pci_disable_device(pdev);
9217 pci_set_drvdata(pdev, NULL);
9219 kfree(priv->channel_info);
9221 kfree(priv->ieee_channels);
9222 kfree(priv->ieee_rates);
9224 if (priv->ibss_beacon)
9225 dev_kfree_skb(priv->ibss_beacon);
9227 ieee80211_free_hw(priv->hw);
9232 static int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
9234 struct iwl_priv *priv = pci_get_drvdata(pdev);
9236 mutex_lock(&priv->mutex);
9238 set_bit(STATUS_IN_SUSPEND, &priv->status);
9240 /* Take down the device; powers it off, etc. */
9243 if (priv->mac80211_registered)
9244 ieee80211_stop_queues(priv->hw);
9246 pci_save_state(pdev);
9247 pci_disable_device(pdev);
9248 pci_set_power_state(pdev, PCI_D3hot);
9250 mutex_unlock(&priv->mutex);
9255 static void iwl_resume(struct iwl_priv *priv)
9257 unsigned long flags;
9259 /* The following it a temporary work around due to the
9260 * suspend / resume not fully initializing the NIC correctly.
9261 * Without all of the following, resume will not attempt to take
9262 * down the NIC (it shouldn't really need to) and will just try
9263 * and bring the NIC back up. However that fails during the
9264 * ucode verification process. This then causes iwl_down to be
9265 * called *after* iwl_hw_nic_init() has succeeded -- which
9266 * then lets the next init sequence succeed. So, we've
9267 * replicated all of that NIC init code here... */
9269 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
9271 iwl_hw_nic_init(priv);
9273 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9274 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
9275 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
9276 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
9277 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9278 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9280 /* tell the device to stop sending interrupts */
9281 iwl_disable_interrupts(priv);
9283 spin_lock_irqsave(&priv->lock, flags);
9284 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
9286 if (!iwl_grab_restricted_access(priv)) {
9287 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
9288 APMG_CLK_VAL_DMA_CLK_RQT);
9289 iwl_release_restricted_access(priv);
9291 spin_unlock_irqrestore(&priv->lock, flags);
9295 iwl_hw_nic_reset(priv);
9297 /* Bring the device back up */
9298 clear_bit(STATUS_IN_SUSPEND, &priv->status);
9299 queue_work(priv->workqueue, &priv->up);
9302 static int iwl_pci_resume(struct pci_dev *pdev)
9304 struct iwl_priv *priv = pci_get_drvdata(pdev);
9307 printk(KERN_INFO "Coming out of suspend...\n");
9309 mutex_lock(&priv->mutex);
9311 pci_set_power_state(pdev, PCI_D0);
9312 err = pci_enable_device(pdev);
9313 pci_restore_state(pdev);
9316 * Suspend/Resume resets the PCI configuration space, so we have to
9317 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
9318 * from interfering with C3 CPU state. pci_restore_state won't help
9319 * here since it only restores the first 64 bytes pci config header.
9321 pci_write_config_byte(pdev, 0x41, 0x00);
9324 mutex_unlock(&priv->mutex);
9329 #endif /* CONFIG_PM */
9331 /*****************************************************************************
9333 * driver and module entry point
9335 *****************************************************************************/
9337 static struct pci_driver iwl_driver = {
9339 .id_table = iwl_hw_card_ids,
9340 .probe = iwl_pci_probe,
9341 .remove = __devexit_p(iwl_pci_remove),
9343 .suspend = iwl_pci_suspend,
9344 .resume = iwl_pci_resume,
9348 static int __init iwl_init(void)
9352 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
9353 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
9354 ret = pci_register_driver(&iwl_driver);
9356 IWL_ERROR("Unable to initialize PCI module\n");
9359 #ifdef CONFIG_IWLWIFI_DEBUG
9360 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
9362 IWL_ERROR("Unable to create driver sysfs file\n");
9363 pci_unregister_driver(&iwl_driver);
9371 static void __exit iwl_exit(void)
9373 #ifdef CONFIG_IWLWIFI_DEBUG
9374 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
9376 pci_unregister_driver(&iwl_driver);
9379 module_param_named(antenna, iwl_param_antenna, int, 0444);
9380 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
9381 module_param_named(disable, iwl_param_disable, int, 0444);
9382 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
9383 module_param_named(hwcrypto, iwl_param_hwcrypto, int, 0444);
9384 MODULE_PARM_DESC(hwcrypto,
9385 "using hardware crypto engine (default 0 [software])\n");
9386 module_param_named(debug, iwl_param_debug, int, 0444);
9387 MODULE_PARM_DESC(debug, "debug output mask");
9388 module_param_named(disable_hw_scan, iwl_param_disable_hw_scan, int, 0444);
9389 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
9391 module_param_named(queues_num, iwl_param_queues_num, int, 0444);
9392 MODULE_PARM_DESC(queues_num, "number of hw queues.");
9395 module_param_named(qos_enable, iwl_param_qos_enable, int, 0444);
9396 MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
9398 module_exit(iwl_exit);
9399 module_init(iwl_init);