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 priv->active_rate = 0;
3008 priv->active_rate_basic = 0;
3010 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
3011 hw->mode == MODE_IEEE80211A ?
3012 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
3014 for (i = 0; i < hw->num_rates; i++) {
3015 rate = &(hw->rates[i]);
3016 if ((rate->val < IWL_RATE_COUNT) &&
3017 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
3018 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
3019 rate->val, iwl_rates[rate->val].plcp,
3020 (rate->flags & IEEE80211_RATE_BASIC) ?
3022 priv->active_rate |= (1 << rate->val);
3023 if (rate->flags & IEEE80211_RATE_BASIC)
3024 priv->active_rate_basic |= (1 << rate->val);
3026 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
3027 rate->val, iwl_rates[rate->val].plcp);
3030 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
3031 priv->active_rate, priv->active_rate_basic);
3034 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
3035 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
3038 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
3039 priv->staging_rxon.cck_basic_rates =
3040 ((priv->active_rate_basic &
3041 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
3043 priv->staging_rxon.cck_basic_rates =
3044 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
3046 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
3047 priv->staging_rxon.ofdm_basic_rates =
3048 ((priv->active_rate_basic &
3049 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
3050 IWL_FIRST_OFDM_RATE) & 0xFF;
3052 priv->staging_rxon.ofdm_basic_rates =
3053 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
3056 static void iwl_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
3058 unsigned long flags;
3060 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
3063 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
3064 disable_radio ? "OFF" : "ON");
3066 if (disable_radio) {
3067 iwl_scan_cancel(priv);
3068 /* FIXME: This is a workaround for AP */
3069 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3070 spin_lock_irqsave(&priv->lock, flags);
3071 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
3072 CSR_UCODE_SW_BIT_RFKILL);
3073 spin_unlock_irqrestore(&priv->lock, flags);
3074 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
3075 set_bit(STATUS_RF_KILL_SW, &priv->status);
3080 spin_lock_irqsave(&priv->lock, flags);
3081 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3083 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3084 spin_unlock_irqrestore(&priv->lock, flags);
3089 spin_lock_irqsave(&priv->lock, flags);
3090 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3091 if (!iwl_grab_restricted_access(priv))
3092 iwl_release_restricted_access(priv);
3093 spin_unlock_irqrestore(&priv->lock, flags);
3095 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3096 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3097 "disabled by HW switch\n");
3101 queue_work(priv->workqueue, &priv->restart);
3105 void iwl_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
3106 u32 decrypt_res, struct ieee80211_rx_status *stats)
3109 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3111 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3114 if (!(fc & IEEE80211_FCTL_PROTECTED))
3117 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3118 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3119 case RX_RES_STATUS_SEC_TYPE_TKIP:
3120 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3121 RX_RES_STATUS_BAD_ICV_MIC)
3122 stats->flag |= RX_FLAG_MMIC_ERROR;
3123 case RX_RES_STATUS_SEC_TYPE_WEP:
3124 case RX_RES_STATUS_SEC_TYPE_CCMP:
3125 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3126 RX_RES_STATUS_DECRYPT_OK) {
3127 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3128 stats->flag |= RX_FLAG_DECRYPTED;
3137 void iwl_handle_data_packet_monitor(struct iwl_priv *priv,
3138 struct iwl_rx_mem_buffer *rxb,