iwlwifi: add TGN flag to qos parameters
[linux-2.6/sactl.git] / drivers / net / wireless / iwlwifi / iwl4965-base.c
blob0a878cf49e07a190ba02d5f3ce3594b33fbca937
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
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *****************************************************************************/
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>
59 #define IWL 4965
61 #include "iwlwifi.h"
62 #include "iwl-4965.h"
63 #include "iwl-helpers.h"
65 #ifdef CONFIG_IWLWIFI_DEBUG
66 u32 iwl_debug_level;
67 #endif
69 /******************************************************************************
71 * module boiler plate
73 ******************************************************************************/
75 /* module parameters */
76 int iwl_param_disable_hw_scan;
77 int iwl_param_debug;
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
92 #define VD "d"
93 #else
94 #define VD
95 #endif
97 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
98 #define VS "s"
99 #else
100 #define VS
101 #endif
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);
125 return NULL;
128 static const struct ieee80211_hw_mode *iwl_get_hw_mode(
129 struct iwl_priv *priv, int mode)
131 int i;
133 for (i = 0; i < 3; i++)
134 if (priv->modes[i].mode == mode)
135 return &priv->modes[i];
137 return NULL;
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] == ' ')
144 return 1;
146 /* Otherwise, if the entire essid is 0, we assume it is hidden */
147 while (essid_len) {
148 essid_len--;
149 if (essid[essid_len] != '\0')
150 return 0;
153 return 1;
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;
160 char *d = escaped;
162 if (iwl_is_empty_essid(essid, essid_len)) {
163 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
164 return escaped;
167 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
168 while (essid_len--) {
169 if (*s == '\0') {
170 *d++ = '\\';
171 *d++ = '0';
172 s++;
173 } else
174 *d++ = *s++;
176 *d = '\0';
177 return escaped;
180 static void iwl_print_hex_dump(int level, void *p, u32 len)
182 #ifdef CONFIG_IWLWIFI_DEBUG
183 if (!(iwl_debug_level & level))
184 return;
186 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
187 p, len, 1);
188 #endif
191 /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
192 * DMA services
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,
202 * Tx queue resumed.
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->read_ptr - q->write_ptr;
213 if (q->read_ptr > q->write_ptr)
214 s -= q->n_bd;
216 if (s <= 0)
217 s += q->n_window;
218 /* keep some reserve to not confuse empty and full situations */
219 s -= 2;
220 if (s < 0)
221 s = 0;
222 return s;
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->write_ptr > q->read_ptr ?
240 (i >= q->read_ptr && i < q->write_ptr) :
241 !(i < q->read_ptr && i >= q->write_ptr);
244 static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
246 if (is_huge)
247 return q->n_window;
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)
255 q->n_bd = count;
256 q->n_window = slots_num;
257 q->id = id;
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;
268 if (q->low_mark < 4)
269 q->low_mark = 4;
271 q->high_mark = q->n_window / 8;
272 if (q->high_mark < 2)
273 q->high_mark = 2;
275 q->write_ptr = q->read_ptr = 0;
277 return 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);
288 if (!txq->txb) {
289 IWL_ERROR("kmalloc for auxiliary BD "
290 "structures failed\n");
291 goto error;
293 } else
294 txq->txb = NULL;
296 txq->bd = pci_alloc_consistent(dev,
297 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
298 &txq->q.dma_addr);
300 if (!txq->bd) {
301 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
302 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
303 goto error;
305 txq->q.id = id;
307 return 0;
309 error:
310 if (txq->txb) {
311 kfree(txq->txb);
312 txq->txb = NULL;
315 return -ENOMEM;
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;
322 int len;
323 int rc = 0;
325 /* allocate command space + one big command for scan since scan
326 * command is very huge the system will not have two scan at the
327 * same time */
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);
332 if (!txq->cmd)
333 return -ENOMEM;
335 rc = iwl_tx_queue_alloc(priv, txq, txq_id);
336 if (rc) {
337 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
339 return -ENOMEM;
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);
350 return 0;
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;
365 int len;
367 if (q->n_bd == 0)
368 return;
370 /* first, empty all BD's */
371 for (; q->write_ptr != q->read_ptr;
372 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, 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 */
382 if (txq->q.n_bd)
383 pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
384 txq->q.n_bd, txq->bd, txq->q.dma_addr);
386 if (txq->txb) {
387 kfree(txq->txb);
388 txq->txb = NULL;
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 /* temporary 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;
412 int i;
413 unsigned long flags;
415 spin_lock_irqsave(&priv->sta_lock, flags);
417 if (is_ap)
418 index = IWL_AP_ID;
419 else if (is_broadcast_ether_addr(addr))
420 index = priv->hw_setting.bcast_sta_id;
421 else
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,
425 addr)) {
426 index = i;
427 break;
430 if (unlikely(index == IWL_INVALID_STATION))
431 goto out;
433 if (priv->stations[index].used) {
434 priv->stations[index].used = 0;
435 priv->num_stations--;
438 BUG_ON(priv->num_stations < 0);
440 out:
441 spin_unlock_irqrestore(&priv->sta_lock, flags);
442 return 0;
444 #endif
446 static void iwl_clear_stations_table(struct iwl_priv *priv)
448 unsigned long flags;
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)
460 int i;
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);
467 if (is_ap)
468 index = IWL_AP_ID;
469 else if (is_broadcast_ether_addr(addr))
470 index = priv->hw_setting.bcast_sta_id;
471 else
472 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
473 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
474 addr)) {
475 index = i;
476 break;
479 if (!priv->stations[i].used &&
480 index == IWL_INVALID_STATION)
481 index = i;
485 /* These two 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);
489 return index;
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);
495 return index;
499 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
500 station = &priv->stations[index];
501 station->used = 1;
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);
519 return index;
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))
554 return 0;
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)
565 switch (cmd) {
566 IWL_CMD(REPLY_ALIVE);
567 IWL_CMD(REPLY_ERROR);
568 IWL_CMD(REPLY_RXON);
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);
575 IWL_CMD(REPLY_TX);
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);
612 default:
613 return "UNKNOWN";
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
627 * command queue.
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;
634 u32 *control_flags;
635 struct iwl_cmd *out_cmd;
636 u32 idx;
637 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
638 dma_addr_t phys_addr;
639 int ret;
640 unsigned long flags;
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");
650 return -ENOSPC;
653 spin_lock_irqsave(&priv->hcmd_lock, flags);
655 tfd = &txq->bd[q->write_ptr];
656 memset(tfd, 0, sizeof(*tfd));
658 control_flags = (u32 *) tfd;
660 idx = get_cmd_index(q, q->write_ptr, 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
668 * information */
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->write_ptr));
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->write_ptr, idx, IWL_CMD_QUEUE_NUM);
686 txq->need_update = 1;
687 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
688 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, 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)
697 int ret;
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))
708 return -EBUSY;
710 ret = iwl_enqueue_hcmd(priv, cmd);
711 if (ret < 0) {
712 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
713 get_cmd_string(cmd->id), ret);
714 return ret;
716 return 0;
719 int iwl_send_cmd_sync(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
721 int cmd_idx;
722 int ret;
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));
733 return -EBUSY;
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);
742 if (cmd_idx < 0) {
743 ret = cmd_idx;
744 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
745 get_cmd_string(cmd->id), ret);
746 goto out;
749 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
750 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
751 HOST_COMPLETE_TIMEOUT);
752 if (!ret) {
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);
759 ret = -ETIMEDOUT;
760 goto cancel;
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));
767 ret = -ECANCELED;
768 goto fail;
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));
773 ret = -EIO;
774 goto fail;
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));
779 ret = -EIO;
780 goto out;
783 ret = 0;
784 goto out;
786 cancel:
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;
797 fail:
798 if (cmd->meta.u.skb) {
799 dev_kfree_skb_any(cmd->meta.u.skb);
800 cmd->meta.u.skb = NULL;
802 out:
803 atomic_set(&entry, 0);
804 return ret;
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 = {
822 .id = id,
823 .len = len,
824 .data = data,
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 = {
833 .id = id,
834 .len = sizeof(val),
835 .data = &val,
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)
855 u8 sta_id;
857 sta_id = iwl_add_station(priv, addr, is_ap, 0);
858 iwl4965_add_station(priv, addr, is_ap);
860 return sta_id;
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",
877 channel, phymode);
878 return -EINVAL;
881 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
882 (priv->phymode == phymode))
883 return 0;
885 priv->staging_rxon.channel = cpu_to_le16(channel);
886 if (phymode == MODE_IEEE80211A)
887 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
888 else
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);
895 return 0;
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
903 * making changes
905 static int iwl_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
907 int error = 0;
908 int counter = 1;
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));
914 if (error)
915 IWL_WARNING("check 24G fields %d | %d\n",
916 counter++, error);
917 } else {
918 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
919 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
920 if (error)
921 IWL_WARNING("check 52 fields %d | %d\n",
922 counter++, error);
923 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
924 if (error)
925 IWL_WARNING("check 52 CCK %d | %d\n",
926 counter++, error);
928 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
929 if (error)
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));
935 if (error)
936 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
938 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
939 if (error)
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));
944 if (error)
945 IWL_WARNING("check CCK and short slot %d | %d\n",
946 counter++, error);
948 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
949 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
950 if (error)
951 IWL_WARNING("check CCK & auto detect %d | %d\n",
952 counter++, error);
954 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
955 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
956 if (error)
957 IWL_WARNING("check TGG and auto detect %d | %d\n",
958 counter++, error);
960 if (error)
961 IWL_WARNING("Tuning to channel %d\n",
962 le16_to_cpu(rxon->channel));
964 if (error) {
965 IWL_ERROR("Not a valid iwl_rxon_assoc_cmd field values\n");
966 return -1;
968 return 0;
972 * iwl_full_rxon_required - determine if RXON_ASSOC can be used in RXON commit
973 * @priv: staging_rxon is compared 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))
1000 return 1;
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))
1009 return 1;
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))
1014 return 1;
1016 return 0;
1019 static int iwl_send_rxon_assoc(struct iwl_priv *priv)
1021 int rc = 0;
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");
1043 return 0;
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);
1058 if (rc)
1059 return rc;
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");
1064 rc = -EIO;
1067 priv->alloc_rxb_skb--;
1068 dev_kfree_skb_any(cmd.meta.u.skb);
1070 return rc;
1074 * iwl_commit_rxon - commit staging_rxon to hardware
1076 * The RXON command in staging_rxon is committed 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);
1086 int rc = 0;
1088 if (!iwl_is_alive(priv))
1089 return -1;
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);
1095 if (rc) {
1096 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1097 return -EINVAL;
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);
1105 if (rc) {
1106 IWL_ERROR("Error setting RXON_ASSOC "
1107 "configuration (%d).\n", rc);
1108 return rc;
1111 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1113 return 0;
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 */
1142 if (rc) {
1143 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1144 IWL_ERROR("Error clearing ASSOC_MSK on current "
1145 "configuration (%d).\n", rc);
1146 return rc;
1150 IWL_DEBUG_INFO("Sending RXON\n"
1151 "* with%s RXON_FILTER_ASSOC_MSK\n"
1152 "* channel = %d\n"
1153 "* bssid = %s\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);
1162 if (rc) {
1163 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1164 return 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);
1182 if (rc) {
1183 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1184 return 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");
1191 return -EIO;
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");
1201 return -EIO;
1203 priv->assoc_station_added = 1;
1206 return 0;
1209 static int iwl_send_bt_config(struct iwl_priv *priv)
1211 struct iwl_bt_cmd bt_cmd = {
1212 .flags = 3,
1213 .lead_time = 0xAA,
1214 .max_kill = 1,
1215 .kill_ack_mask = 0,
1216 .kill_cts_mask = 0,
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)
1225 int rc = 0;
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);
1237 return 0;
1240 rc = iwl_send_cmd_sync(priv, &cmd);
1241 if (rc) {
1242 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1243 return rc;
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
1253 * completed. */
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);
1261 return rc;
1264 static int iwl_card_state_sync_callback(struct iwl_priv *priv,
1265 struct iwl_cmd *cmd,
1266 struct sk_buff *skb)
1268 return 1;
1272 * CARD_STATE_CMD
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,
1285 .len = sizeof(u32),
1286 .data = &flags,
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;
1301 if (!skb) {
1302 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1303 return 1;
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",
1309 res->hdr.flags);
1310 return 1;
1313 switch (res->u.add_sta.status) {
1314 case ADD_STA_SUCCESS_MSK:
1315 break;
1316 default:
1317 break;
1320 /* We didn't cache the SKB; let the caller free it */
1321 return 1;
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;
1328 int rc = 0;
1329 struct iwl_host_cmd cmd = {
1330 .id = REPLY_ADD_STA,
1331 .len = sizeof(struct iwl_addsta_cmd),
1332 .meta.flags = flags,
1333 .data = sta,
1336 if (flags & CMD_ASYNC)
1337 cmd.meta.u.callback = iwl_add_sta_sync_callback;
1338 else
1339 cmd.meta.flags |= CMD_WANT_SKB;
1341 rc = iwl_send_cmd(priv, &cmd);
1343 if (rc || (flags & CMD_ASYNC))
1344 return rc;
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",
1349 res->hdr.flags);
1350 rc = -EIO;
1353 if (rc == 0) {
1354 switch (res->u.add_sta.status) {
1355 case ADD_STA_SUCCESS_MSK:
1356 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1357 break;
1358 default:
1359 rc = -EIO;
1360 IWL_WARNING("REPLY_ADD_STA failed\n");
1361 break;
1365 priv->alloc_rxb_skb--;
1366 dev_kfree_skb_any(cmd.meta.u.skb);
1368 return rc;
1371 static int iwl_update_sta_key_info(struct iwl_priv *priv,
1372 struct ieee80211_key_conf *keyconf,
1373 u8 sta_id)
1375 unsigned long flags;
1376 __le16 key_flags = 0;
1378 switch (keyconf->alg) {
1379 case ALG_CCMP:
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;
1384 break;
1385 case ALG_TKIP:
1386 case ALG_WEP:
1387 return -EINVAL;
1388 default:
1389 return -EINVAL;
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,
1395 keyconf->keylen);
1397 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1398 keyconf->keylen);
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);
1407 return 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);
1424 return 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;
1436 list_del(element);
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);
1454 if (!frame) {
1455 IWL_ERROR("Could not allocate frame!\n");
1456 return NULL;
1459 priv->frames_count++;
1460 return frame;
1463 element = priv->free_frames.next;
1464 list_del(element);
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)))
1482 return 0;
1484 if (priv->ibss_beacon->len > left)
1485 return 0;
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)
1494 int i = 0;
1496 if (plcp & RATE_MCS_HT_MSK) {
1497 i = (plcp & 0xff);
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)
1505 i += 1;
1506 if ((i >= IWL_FIRST_OFDM_RATE) &&
1507 (i <= IWL_LAST_OFDM_RATE))
1508 return i;
1509 } else {
1510 for (i = 0; i < ARRAY_SIZE(iwl_rates); i++)
1511 if (iwl_rates[i].plcp == (plcp &0xFF))
1512 return i;
1514 return -1;
1517 static u8 iwl_rate_get_lowest_plcp(int rate_mask)
1519 u8 i;
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;
1534 int rc;
1535 u8 rate;
1537 frame = iwl_get_free_frame(priv);
1539 if (!frame) {
1540 IWL_ERROR("Could not obtain free frame buffer for beacon "
1541 "command.\n");
1542 return -ENOMEM;
1545 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
1546 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic &
1547 0xFF0);
1548 if (rate == IWL_INVALID_RATE)
1549 rate = IWL_RATE_6M_PLCP;
1550 } else {
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,
1559 &frame->u.cmd[0]);
1561 iwl_free_frame(priv, frame);
1563 return rc;
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);
1588 u32 r;
1589 int sz = sizeof(priv->eeprom);
1590 int rc;
1591 int i;
1592 u16 addr;
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);
1602 return -ENOENT;
1605 rc = iwl_eeprom_acquire_semaphore(priv);
1606 if (rc < 0) {
1607 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
1608 return -ENOENT;
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)
1620 break;
1621 udelay(IWL_EEPROM_ACCESS_DELAY);
1624 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1625 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1626 rc = -ETIMEDOUT;
1627 goto done;
1629 e[addr / 2] = le16_to_cpu(r >> 16);
1631 rc = 0;
1633 done:
1634 iwl_eeprom_release_semaphore(priv);
1635 return rc;
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)
1661 u32 to_us;
1662 u32 print_summary = 0;
1663 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1664 u32 hundred = 0;
1665 u32 dataframe = 0;
1666 u16 fc;
1667 u16 seq_ctl;
1668 u16 channel;
1669 u16 phy_flags;
1670 int rate_sym;
1671 u16 length;
1672 u16 status;
1673 u16 bcn_tmr;
1674 u32 tsf_low;
1675 u64 tsf;
1676 u8 rssi;
1677 u8 agc;
1678 u16 sig_avg;
1679 u16 noise_diff;
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);
1685 /* MAC header */
1686 fc = le16_to_cpu(header->frame_control);
1687 seq_ctl = le16_to_cpu(header->seq_ctrl);
1689 /* metadata */
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)) {
1713 dataframe = 1;
1714 if (!group100)
1715 print_summary = 1; /* print each frame */
1716 else if (priv->framecnt_to_us < 100) {
1717 priv->framecnt_to_us++;
1718 print_summary = 0;
1719 } else {
1720 priv->framecnt_to_us = 0;
1721 print_summary = 1;
1722 hundred = 1;
1724 } else {
1725 /* print summary for all other frames */
1726 print_summary = 1;
1729 if (print_summary) {
1730 char *title;
1731 u32 rate;
1733 if (hundred)
1734 title = "100Frames";
1735 else if (fc & IEEE80211_FCTL_RETRY)
1736 title = "Retry";
1737 else if (ieee80211_is_assoc_response(fc))
1738 title = "AscRsp";
1739 else if (ieee80211_is_reassoc_response(fc))
1740 title = "RasRsp";
1741 else if (ieee80211_is_probe_response(fc)) {
1742 title = "PrbRsp";
1743 print_dump = 1; /* dump frame contents */
1744 } else if (ieee80211_is_beacon(fc)) {
1745 title = "Beacon";
1746 print_dump = 1; /* dump frame contents */
1747 } else if (ieee80211_is_atim(fc))
1748 title = "ATIM";
1749 else if (ieee80211_is_auth(fc))
1750 title = "Auth";
1751 else if (ieee80211_is_deauth(fc))
1752 title = "DeAuth";
1753 else if (ieee80211_is_disassoc(fc))
1754 title = "DisAssoc";
1755 else
1756 title = "Frame";
1758 rate = iwl_rate_index_from_plcp(rate_sym);
1759 if (rate == -1)
1760 rate = 0;
1761 else
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. */
1767 if (dataframe)
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);
1772 else {
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);
1783 if (print_dump)
1784 iwl_print_hex_dump(IWL_DL_RX, data, length);
1786 #endif
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;
1806 int i;
1807 u8 *cnt = ie;
1808 u8 *rates = ie + 1;
1810 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1811 if (bit & supported_rate) {
1812 ret_rates |= bit;
1813 rates[*cnt] = iwl_rates[i].ieee |
1814 ((bit & basic_rate) ? 0x80 : 0x00);
1815 (*cnt)++;
1816 (*left)--;
1817 if ((*left <= 0) ||
1818 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
1819 break;
1823 return ret_rates;
1826 #ifdef CONFIG_IWLWIFI_HT
1827 void static iwl_set_ht_capab(struct ieee80211_hw *hw,
1828 struct ieee80211_ht_capability *ht_cap,
1829 u8 use_wide_chan);
1830 #endif
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)
1839 int len = 0;
1840 u8 *pos = NULL;
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 */
1845 left -= 24;
1846 if (left < 0)
1847 return 0;
1848 len += 24;
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 */
1857 /* ...next IE... */
1859 left -= 2;
1860 if (left < 0)
1861 return 0;
1862 len += 2;
1863 pos = &(frame->u.probe_req.variable[0]);
1864 *pos++ = WLAN_EID_SSID;
1865 *pos++ = 0;
1867 /* fill in our direct SSID IE... */
1868 if (is_direct) {
1869 /* ...next IE... */
1870 left -= 2 + priv->essid_len;
1871 if (left < 0)
1872 return 0;
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 */
1882 /* ...next IE... */
1883 left -= 2;
1884 if (left < 0)
1885 return 0;
1887 /* ... fill it in... */
1888 *pos++ = WLAN_EID_SUPP_RATES;
1889 *pos = 0;
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;
1904 len += 2 + *pos;
1905 pos += (*pos) + 1;
1906 if (active_rates == 0)
1907 goto fill_end;
1909 /* fill in supported extended rate */
1910 /* ...next IE... */
1911 left -= 2;
1912 if (left < 0)
1913 return 0;
1914 /* ... fill it in... */
1915 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1916 *pos = 0;
1917 iwl_supported_rate_to_ie(pos, active_rates,
1918 priv->active_rate_basic, &left);
1919 if (*pos > 0)
1920 len += 2 + *pos;
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)
1927 use_wide_chan = 0;
1928 pos += (*pos) + 1;
1929 *pos++ = WLAN_EID_HT_CAPABILITY;
1930 *pos++ = sizeof(struct ieee80211_ht_capability);
1931 iwl_set_ht_capab(NULL, (struct ieee80211_ht_capability *)pos,
1932 use_wide_chan);
1933 len += 2 + sizeof(struct ieee80211_ht_capability);
1935 #endif /*CONFIG_IWLWIFI_HT */
1937 fill_end:
1938 return (u16)len;
1942 * QoS support
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)
1955 u16 cw_min = 15;
1956 u16 cw_max = 1023;
1957 u8 aifs = 2;
1958 u8 is_legacy = 0;
1959 unsigned long flags;
1960 int i;
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)) {
1969 cw_min = 31;
1970 is_legacy = 1;
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)) {
1976 cw_min = 31;
1977 is_legacy = 1;
1980 if (priv->qos_data.qos_active)
1981 aifs = 3;
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) {
1990 i = 1;
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;
1997 i = 2;
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;
2003 if (is_legacy)
2004 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2005 cpu_to_le16(6016);
2006 else
2007 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2008 cpu_to_le16(3008);
2009 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2011 i = 3;
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;
2018 if (is_legacy)
2019 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2020 cpu_to_le16(3264);
2021 else
2022 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2023 cpu_to_le16(1504);
2024 } else {
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;
2044 if (priv == NULL)
2045 return;
2047 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2048 return;
2050 if (!priv->qos_data.qos_enable)
2051 return;
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;
2060 if (priv->qos_data.qos_active)
2061 priv->qos_data.def_qos_parm.qos_flags |=
2062 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2064 #ifdef CONFIG_IWLWIFI_HT
2065 if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
2066 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
2067 #endif /* CONFIG_IWLWIFI_HT */
2069 spin_unlock_irqrestore(&priv->lock, flags);
2071 if (force || iwl_is_associated(priv)) {
2072 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
2073 priv->qos_data.qos_active,
2074 priv->qos_data.def_qos_parm.qos_flags);
2076 iwl_send_qos_params_command(priv,
2077 &(priv->qos_data.def_qos_parm));
2081 #endif /* CONFIG_IWLWIFI_QOS */
2083 * Power management (not Tx power!) functions
2085 #define MSEC_TO_USEC 1024
2087 #define NOSLP __constant_cpu_to_le16(0), 0, 0
2088 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
2089 #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2090 #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2091 __constant_cpu_to_le32(X1), \
2092 __constant_cpu_to_le32(X2), \
2093 __constant_cpu_to_le32(X3), \
2094 __constant_cpu_to_le32(X4)}
2097 /* default power management (not Tx power) table values */
2098 /* for tim 0-10 */
2099 static struct iwl_power_vec_entry range_0[IWL_POWER_AC] = {
2100 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2101 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2102 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2103 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2104 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2105 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2108 /* for tim > 10 */
2109 static struct iwl_power_vec_entry range_1[IWL_POWER_AC] = {
2110 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2111 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2112 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2113 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2114 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2115 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2116 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2117 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2118 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2119 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2122 int iwl_power_init_handle(struct iwl_priv *priv)
2124 int rc = 0, i;
2125 struct iwl_power_mgr *pow_data;
2126 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
2127 u16 pci_pm;
2129 IWL_DEBUG_POWER("Initialize power \n");
2131 pow_data = &(priv->power_data);
2133 memset(pow_data, 0, sizeof(*pow_data));
2135 pow_data->active_index = IWL_POWER_RANGE_0;
2136 pow_data->dtim_val = 0xffff;
2138 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2139 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2141 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2142 if (rc != 0)
2143 return 0;
2144 else {
2145 struct iwl_powertable_cmd *cmd;
2147 IWL_DEBUG_POWER("adjust power command flags\n");
2149 for (i = 0; i < IWL_POWER_AC; i++) {
2150 cmd = &pow_data->pwr_range_0[i].cmd;
2152 if (pci_pm & 0x1)
2153 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2154 else
2155 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2158 return rc;
2161 static int iwl_update_power_cmd(struct iwl_priv *priv,
2162 struct iwl_powertable_cmd *cmd, u32 mode)
2164 int rc = 0, i;
2165 u8 skip;
2166 u32 max_sleep = 0;
2167 struct iwl_power_vec_entry *range;
2168 u8 period = 0;
2169 struct iwl_power_mgr *pow_data;
2171 if (mode > IWL_POWER_INDEX_5) {
2172 IWL_DEBUG_POWER("Error invalid power mode \n");
2173 return -1;
2175 pow_data = &(priv->power_data);
2177 if (pow_data->active_index == IWL_POWER_RANGE_0)
2178 range = &pow_data->pwr_range_0[0];
2179 else
2180 range = &pow_data->pwr_range_1[1];
2182 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
2184 #ifdef IWL_MAC80211_DISABLE
2185 if (priv->assoc_network != NULL) {
2186 unsigned long flags;
2188 period = priv->assoc_network->tim.tim_period;
2190 #endif /*IWL_MAC80211_DISABLE */
2191 skip = range[mode].no_dtim;
2193 if (period == 0) {
2194 period = 1;
2195 skip = 0;
2198 if (skip == 0) {
2199 max_sleep = period;
2200 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2201 } else {
2202 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2203 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2204 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2207 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2208 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2209 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2212 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2213 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2214 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2215 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2216 le32_to_cpu(cmd->sleep_interval[0]),
2217 le32_to_cpu(cmd->sleep_interval[1]),
2218 le32_to_cpu(cmd->sleep_interval[2]),
2219 le32_to_cpu(cmd->sleep_interval[3]),
2220 le32_to_cpu(cmd->sleep_interval[4]));
2222 return rc;
2225 static int iwl_send_power_mode(struct iwl_priv *priv, u32 mode)
2227 u32 final_mode = mode;
2228 int rc;
2229 struct iwl_powertable_cmd cmd;
2231 /* If on battery, set to 3,
2232 * if plugged into AC power, set to CAM ("continuously aware mode"),
2233 * else user level */
2234 switch (mode) {
2235 case IWL_POWER_BATTERY:
2236 final_mode = IWL_POWER_INDEX_3;
2237 break;
2238 case IWL_POWER_AC:
2239 final_mode = IWL_POWER_MODE_CAM;
2240 break;
2241 default:
2242 final_mode = mode;
2243 break;
2246 cmd.keep_alive_beacons = 0;
2248 iwl_update_power_cmd(priv, &cmd, final_mode);
2250 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2252 if (final_mode == IWL_POWER_MODE_CAM)
2253 clear_bit(STATUS_POWER_PMI, &priv->status);
2254 else
2255 set_bit(STATUS_POWER_PMI, &priv->status);
2257 return rc;
2260 int iwl_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
2262 /* Filter incoming packets to determine if they are targeted toward
2263 * this network, discarding packets coming from ourselves */
2264 switch (priv->iw_mode) {
2265 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2266 /* packets from our adapter are dropped (echo) */
2267 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2268 return 0;
2269 /* {broad,multi}cast packets to our IBSS go through */
2270 if (is_multicast_ether_addr(header->addr1))
2271 return !compare_ether_addr(header->addr3, priv->bssid);
2272 /* packets to our adapter go through */
2273 return !compare_ether_addr(header->addr1, priv->mac_addr);
2274 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2275 /* packets from our adapter are dropped (echo) */
2276 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2277 return 0;
2278 /* {broad,multi}cast packets to our BSS go through */
2279 if (is_multicast_ether_addr(header->addr1))
2280 return !compare_ether_addr(header->addr2, priv->bssid);
2281 /* packets to our adapter go through */
2282 return !compare_ether_addr(header->addr1, priv->mac_addr);
2285 return 1;
2288 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2290 const char *iwl_get_tx_fail_reason(u32 status)
2292 switch (status & TX_STATUS_MSK) {
2293 case TX_STATUS_SUCCESS:
2294 return "SUCCESS";
2295 TX_STATUS_ENTRY(SHORT_LIMIT);
2296 TX_STATUS_ENTRY(LONG_LIMIT);
2297 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2298 TX_STATUS_ENTRY(MGMNT_ABORT);
2299 TX_STATUS_ENTRY(NEXT_FRAG);
2300 TX_STATUS_ENTRY(LIFE_EXPIRE);
2301 TX_STATUS_ENTRY(DEST_PS);
2302 TX_STATUS_ENTRY(ABORTED);
2303 TX_STATUS_ENTRY(BT_RETRY);
2304 TX_STATUS_ENTRY(STA_INVALID);
2305 TX_STATUS_ENTRY(FRAG_DROPPED);
2306 TX_STATUS_ENTRY(TID_DISABLE);
2307 TX_STATUS_ENTRY(FRAME_FLUSHED);
2308 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2309 TX_STATUS_ENTRY(TX_LOCKED);
2310 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2313 return "UNKNOWN";
2317 * iwl_scan_cancel - Cancel any currently executing HW scan
2319 * NOTE: priv->mutex is not required before calling this function
2321 static int iwl_scan_cancel(struct iwl_priv *priv)
2323 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2324 clear_bit(STATUS_SCANNING, &priv->status);
2325 return 0;
2328 if (test_bit(STATUS_SCANNING, &priv->status)) {
2329 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2330 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2331 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2332 queue_work(priv->workqueue, &priv->abort_scan);
2334 } else
2335 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2337 return test_bit(STATUS_SCANNING, &priv->status);
2340 return 0;
2344 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
2345 * @ms: amount of time to wait (in milliseconds) for scan to abort
2347 * NOTE: priv->mutex must be held before calling this function
2349 static int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
2351 unsigned long now = jiffies;
2352 int ret;
2354 ret = iwl_scan_cancel(priv);
2355 if (ret && ms) {
2356 mutex_unlock(&priv->mutex);
2357 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2358 test_bit(STATUS_SCANNING, &priv->status))
2359 msleep(1);
2360 mutex_lock(&priv->mutex);
2362 return test_bit(STATUS_SCANNING, &priv->status);
2365 return ret;
2368 static void iwl_sequence_reset(struct iwl_priv *priv)
2370 /* Reset ieee stats */
2372 /* We don't reset the net_device_stats (ieee->stats) on
2373 * re-association */
2375 priv->last_seq_num = -1;
2376 priv->last_frag_num = -1;
2377 priv->last_packet_time = 0;
2379 iwl_scan_cancel(priv);
2382 #define MAX_UCODE_BEACON_INTERVAL 4096
2383 #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2385 static __le16 iwl_adjust_beacon_interval(u16 beacon_val)
2387 u16 new_val = 0;
2388 u16 beacon_factor = 0;
2390 beacon_factor =
2391 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2392 / MAX_UCODE_BEACON_INTERVAL;
2393 new_val = beacon_val / beacon_factor;
2395 return cpu_to_le16(new_val);
2398 static void iwl_setup_rxon_timing(struct iwl_priv *priv)
2400 u64 interval_tm_unit;
2401 u64 tsf, result;
2402 unsigned long flags;
2403 struct ieee80211_conf *conf = NULL;
2404 u16 beacon_int = 0;
2406 conf = ieee80211_get_hw_conf(priv->hw);
2408 spin_lock_irqsave(&priv->lock, flags);
2409 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2410 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2412 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2414 tsf = priv->timestamp1;
2415 tsf = ((tsf << 32) | priv->timestamp0);
2417 beacon_int = priv->beacon_int;
2418 spin_unlock_irqrestore(&priv->lock, flags);
2420 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2421 if (beacon_int == 0) {
2422 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2423 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2424 } else {
2425 priv->rxon_timing.beacon_interval =
2426 cpu_to_le16(beacon_int);
2427 priv->rxon_timing.beacon_interval =
2428 iwl_adjust_beacon_interval(
2429 le16_to_cpu(priv->rxon_timing.beacon_interval));
2432 priv->rxon_timing.atim_window = 0;
2433 } else {
2434 priv->rxon_timing.beacon_interval =
2435 iwl_adjust_beacon_interval(conf->beacon_int);
2436 /* TODO: we need to get atim_window from upper stack
2437 * for now we set to 0 */
2438 priv->rxon_timing.atim_window = 0;
2441 interval_tm_unit =
2442 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2443 result = do_div(tsf, interval_tm_unit);
2444 priv->rxon_timing.beacon_init_val =
2445 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2447 IWL_DEBUG_ASSOC
2448 ("beacon interval %d beacon timer %d beacon tim %d\n",
2449 le16_to_cpu(priv->rxon_timing.beacon_interval),
2450 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2451 le16_to_cpu(priv->rxon_timing.atim_window));
2454 static int iwl_scan_initiate(struct iwl_priv *priv)
2456 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2457 IWL_ERROR("APs don't scan.\n");
2458 return 0;
2461 if (!iwl_is_ready_rf(priv)) {
2462 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2463 return -EIO;
2466 if (test_bit(STATUS_SCANNING, &priv->status)) {
2467 IWL_DEBUG_SCAN("Scan already in progress.\n");
2468 return -EAGAIN;
2471 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2472 IWL_DEBUG_SCAN("Scan request while abort pending. "
2473 "Queuing.\n");
2474 return -EAGAIN;
2477 IWL_DEBUG_INFO("Starting scan...\n");
2478 priv->scan_bands = 2;
2479 set_bit(STATUS_SCANNING, &priv->status);
2480 priv->scan_start = jiffies;
2481 priv->scan_pass_start = priv->scan_start;
2483 queue_work(priv->workqueue, &priv->request_scan);
2485 return 0;
2488 static int iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
2490 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
2492 if (hw_decrypt)
2493 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2494 else
2495 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2497 return 0;
2500 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode)
2502 if (phymode == MODE_IEEE80211A) {
2503 priv->staging_rxon.flags &=
2504 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2505 | RXON_FLG_CCK_MSK);
2506 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2507 } else {
2508 /* Copied from iwl_bg_post_associate() */
2509 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2510 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2511 else
2512 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2514 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2515 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2517 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2518 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2519 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2524 * initialize rxon structure with default values from eeprom
2526 static void iwl_connection_init_rx_config(struct iwl_priv *priv)
2528 const struct iwl_channel_info *ch_info;
2530 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2532 switch (priv->iw_mode) {
2533 case IEEE80211_IF_TYPE_AP:
2534 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2535 break;
2537 case IEEE80211_IF_TYPE_STA:
2538 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2539 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2540 break;
2542 case IEEE80211_IF_TYPE_IBSS:
2543 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2544 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2545 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2546 RXON_FILTER_ACCEPT_GRP_MSK;
2547 break;
2549 case IEEE80211_IF_TYPE_MNTR:
2550 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2551 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2552 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2553 break;
2556 #if 0
2557 /* TODO: Figure out when short_preamble would be set and cache from
2558 * that */
2559 if (!hw_to_local(priv->hw)->short_preamble)
2560 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2561 else
2562 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2563 #endif
2565 ch_info = iwl_get_channel_info(priv, priv->phymode,
2566 le16_to_cpu(priv->staging_rxon.channel));
2568 if (!ch_info)
2569 ch_info = &priv->channel_info[0];
2572 * in some case A channels are all non IBSS
2573 * in this case force B/G channel
2575 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2576 !(is_channel_ibss(ch_info)))
2577 ch_info = &priv->channel_info[0];
2579 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2580 if (is_channel_a_band(ch_info))
2581 priv->phymode = MODE_IEEE80211A;
2582 else
2583 priv->phymode = MODE_IEEE80211G;
2585 iwl_set_flags_for_phymode(priv, priv->phymode);
2587 priv->staging_rxon.ofdm_basic_rates =
2588 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2589 priv->staging_rxon.cck_basic_rates =
2590 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2592 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2593 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2594 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2595 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2596 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2597 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2598 iwl4965_set_rxon_chain(priv);
2601 static int iwl_set_mode(struct iwl_priv *priv, int mode)
2603 if (!iwl_is_ready_rf(priv))
2604 return -EAGAIN;
2606 if (mode == IEEE80211_IF_TYPE_IBSS) {
2607 const struct iwl_channel_info *ch_info;
2609 ch_info = iwl_get_channel_info(priv,
2610 priv->phymode,
2611 le16_to_cpu(priv->staging_rxon.channel));
2613 if (!ch_info || !is_channel_ibss(ch_info)) {
2614 IWL_ERROR("channel %d not IBSS channel\n",
2615 le16_to_cpu(priv->staging_rxon.channel));
2616 return -EINVAL;
2620 cancel_delayed_work(&priv->scan_check);
2621 if (iwl_scan_cancel_timeout(priv, 100)) {
2622 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2623 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2624 return -EAGAIN;
2627 priv->iw_mode = mode;
2629 iwl_connection_init_rx_config(priv);
2630 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2632 iwl_clear_stations_table(priv);
2634 iwl_commit_rxon(priv);
2636 return 0;
2639 static void iwl_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
2640 struct ieee80211_tx_control *ctl,
2641 struct iwl_cmd *cmd,
2642 struct sk_buff *skb_frag,
2643 int last_frag)
2645 struct iwl_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2647 switch (keyinfo->alg) {
2648 case ALG_CCMP:
2649 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2650 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2651 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2652 break;
2654 case ALG_TKIP:
2655 #if 0
2656 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2658 if (last_frag)
2659 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2661 else
2662 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2663 #endif
2664 break;
2666 case ALG_WEP:
2667 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2668 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2670 if (keyinfo->keylen == 13)
2671 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2673 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2675 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2676 "with key %d\n", ctl->key_idx);
2677 break;
2679 default:
2680 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2681 break;
2686 * handle build REPLY_TX command notification.
2688 static void iwl_build_tx_cmd_basic(struct iwl_priv *priv,
2689 struct iwl_cmd *cmd,
2690 struct ieee80211_tx_control *ctrl,
2691 struct ieee80211_hdr *hdr,
2692 int is_unicast, u8 std_id)
2694 __le16 *qc;
2695 u16 fc = le16_to_cpu(hdr->frame_control);
2696 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2698 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2699 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2700 tx_flags |= TX_CMD_FLG_ACK_MSK;
2701 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2702 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2703 if (ieee80211_is_probe_response(fc) &&
2704 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2705 tx_flags |= TX_CMD_FLG_TSF_MSK;
2706 } else {
2707 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2708 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2711 cmd->cmd.tx.sta_id = std_id;
2712 if (ieee80211_get_morefrag(hdr))
2713 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2715 qc = ieee80211_get_qos_ctrl(hdr);
2716 if (qc) {
2717 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2718 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2719 } else
2720 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2722 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2723 tx_flags |= TX_CMD_FLG_RTS_MSK;
2724 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2725 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2726 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2727 tx_flags |= TX_CMD_FLG_CTS_MSK;
2730 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2731 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2733 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2734 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2735 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2736 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
2737 cmd->cmd.tx.timeout.pm_frame_timeout =
2738 cpu_to_le16(3);
2739 else
2740 cmd->cmd.tx.timeout.pm_frame_timeout =
2741 cpu_to_le16(2);
2742 } else
2743 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2745 cmd->cmd.tx.driver_txop = 0;
2746 cmd->cmd.tx.tx_flags = tx_flags;
2747 cmd->cmd.tx.next_frame_len = 0;
2750 static int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
2752 int sta_id;
2753 u16 fc = le16_to_cpu(hdr->frame_control);
2754 DECLARE_MAC_BUF(mac);
2756 /* If this frame is broadcast or not data then use the broadcast
2757 * station id */
2758 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2759 is_multicast_ether_addr(hdr->addr1))
2760 return priv->hw_setting.bcast_sta_id;
2762 switch (priv->iw_mode) {
2764 /* If this frame is part of a BSS network (we're a station), then
2765 * we use the AP's station id */
2766 case IEEE80211_IF_TYPE_STA:
2767 return IWL_AP_ID;
2769 /* If we are an AP, then find the station, or use BCAST */
2770 case IEEE80211_IF_TYPE_AP:
2771 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2772 if (sta_id != IWL_INVALID_STATION)
2773 return sta_id;
2774 return priv->hw_setting.bcast_sta_id;
2776 /* If this frame is part of a IBSS network, then we use the
2777 * target specific station id */
2778 case IEEE80211_IF_TYPE_IBSS:
2779 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2780 if (sta_id != IWL_INVALID_STATION)
2781 return sta_id;
2783 sta_id = iwl_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
2785 if (sta_id != IWL_INVALID_STATION)
2786 return sta_id;
2788 IWL_DEBUG_DROP("Station %s not in station map. "
2789 "Defaulting to broadcast...\n",
2790 print_mac(mac, hdr->addr1));
2791 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2792 return priv->hw_setting.bcast_sta_id;
2794 default:
2795 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
2796 return priv->hw_setting.bcast_sta_id;
2801 * start REPLY_TX command process
2803 static int iwl_tx_skb(struct iwl_priv *priv,
2804 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2806 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2807 struct iwl_tfd_frame *tfd;
2808 u32 *control_flags;
2809 int txq_id = ctl->queue;
2810 struct iwl_tx_queue *txq = NULL;
2811 struct iwl_queue *q = NULL;
2812 dma_addr_t phys_addr;
2813 dma_addr_t txcmd_phys;
2814 struct iwl_cmd *out_cmd = NULL;
2815 u16 len, idx, len_org;
2816 u8 id, hdr_len, unicast;
2817 u8 sta_id;
2818 u16 seq_number = 0;
2819 u16 fc;
2820 __le16 *qc;
2821 u8 wait_write_ptr = 0;
2822 unsigned long flags;
2823 int rc;
2825 spin_lock_irqsave(&priv->lock, flags);
2826 if (iwl_is_rfkill(priv)) {
2827 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2828 goto drop_unlock;
2831 if (!priv->interface_id) {
2832 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2833 goto drop_unlock;
2836 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2837 IWL_ERROR("ERROR: No TX rate available.\n");
2838 goto drop_unlock;
2841 unicast = !is_multicast_ether_addr(hdr->addr1);
2842 id = 0;
2844 fc = le16_to_cpu(hdr->frame_control);
2846 #ifdef CONFIG_IWLWIFI_DEBUG
2847 if (ieee80211_is_auth(fc))
2848 IWL_DEBUG_TX("Sending AUTH frame\n");
2849 else if (ieee80211_is_assoc_request(fc))
2850 IWL_DEBUG_TX("Sending ASSOC frame\n");
2851 else if (ieee80211_is_reassoc_request(fc))
2852 IWL_DEBUG_TX("Sending REASSOC frame\n");
2853 #endif
2855 if (!iwl_is_associated(priv) &&
2856 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
2857 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
2858 goto drop_unlock;
2861 spin_unlock_irqrestore(&priv->lock, flags);
2863 hdr_len = ieee80211_get_hdrlen(fc);
2864 sta_id = iwl_get_sta_id(priv, hdr);
2865 if (sta_id == IWL_INVALID_STATION) {
2866 DECLARE_MAC_BUF(mac);
2868 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2869 print_mac(mac, hdr->addr1));
2870 goto drop;
2873 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2875 qc = ieee80211_get_qos_ctrl(hdr);
2876 if (qc) {
2877 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2878 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2879 IEEE80211_SCTL_SEQ;
2880 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2881 (hdr->seq_ctrl &
2882 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2883 seq_number += 0x10;
2884 #ifdef CONFIG_IWLWIFI_HT
2885 #ifdef CONFIG_IWLWIFI_HT_AGG
2886 /* aggregation is on for this <sta,tid> */
2887 if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
2888 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
2889 #endif /* CONFIG_IWLWIFI_HT_AGG */
2890 #endif /* CONFIG_IWLWIFI_HT */
2892 txq = &priv->txq[txq_id];
2893 q = &txq->q;
2895 spin_lock_irqsave(&priv->lock, flags);
2897 tfd = &txq->bd[q->write_ptr];
2898 memset(tfd, 0, sizeof(*tfd));
2899 control_flags = (u32 *) tfd;
2900 idx = get_cmd_index(q, q->write_ptr, 0);
2902 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
2903 txq->txb[q->write_ptr].skb[0] = skb;
2904 memcpy(&(txq->txb[q->write_ptr].status.control),
2905 ctl, sizeof(struct ieee80211_tx_control));
2906 out_cmd = &txq->cmd[idx];
2907 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2908 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2909 out_cmd->hdr.cmd = REPLY_TX;
2910 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2911 INDEX_TO_SEQ(q->write_ptr)));
2912 /* copy frags header */
2913 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2915 /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
2916 len = priv->hw_setting.tx_cmd_len +
2917 sizeof(struct iwl_cmd_header) + hdr_len;
2919 len_org = len;
2920 len = (len + 3) & ~3;
2922 if (len_org != len)
2923 len_org = 1;
2924 else
2925 len_org = 0;
2927 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2928 offsetof(struct iwl_cmd, hdr);
2930 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
2932 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
2933 iwl_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
2935 /* 802.11 null functions have no payload... */
2936 len = skb->len - hdr_len;
2937 if (len) {
2938 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2939 len, PCI_DMA_TODEVICE);
2940 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
2943 if (len_org)
2944 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2946 len = (u16)skb->len;
2947 out_cmd->cmd.tx.len = cpu_to_le16(len);
2949 /* TODO need this for burst mode later on */
2950 iwl_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
2952 /* set is_hcca to 0; it probably will never be implemented */
2953 iwl_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
2955 iwl4965_tx_cmd(priv, out_cmd, sta_id, txcmd_phys,
2956 hdr, hdr_len, ctl, NULL);
2958 if (!ieee80211_get_morefrag(hdr)) {
2959 txq->need_update = 1;
2960 if (qc) {
2961 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2962 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2964 } else {
2965 wait_write_ptr = 1;
2966 txq->need_update = 0;
2969 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
2970 sizeof(out_cmd->cmd.tx));
2972 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
2973 ieee80211_get_hdrlen(fc));
2975 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2977 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
2978 rc = iwl_tx_queue_update_write_ptr(priv, txq);
2979 spin_unlock_irqrestore(&priv->lock, flags);
2981 if (rc)
2982 return rc;
2984 if ((iwl_queue_space(q) < q->high_mark)
2985 && priv->mac80211_registered) {
2986 if (wait_write_ptr) {
2987 spin_lock_irqsave(&priv->lock, flags);
2988 txq->need_update = 1;
2989 iwl_tx_queue_update_write_ptr(priv, txq);
2990 spin_unlock_irqrestore(&priv->lock, flags);
2993 ieee80211_stop_queue(priv->hw, ctl->queue);
2996 return 0;
2998 drop_unlock:
2999 spin_unlock_irqrestore(&priv->lock, flags);
3000 drop:
3001 return -1;
3004 static void iwl_set_rate(struct iwl_priv *priv)
3006 const struct ieee80211_hw_mode *hw = NULL;
3007 struct ieee80211_rate *rate;
3008 int i;
3010 hw = iwl_get_hw_mode(priv, priv->phymode);
3011 if (!hw) {
3012 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
3013 return;
3016 priv->active_rate = 0;
3017 priv->active_rate_basic = 0;
3019 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
3020 hw->mode == MODE_IEEE80211A ?
3021 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
3023 for (i = 0; i < hw->num_rates; i++) {
3024 rate = &(hw->rates[i]);
3025 if ((rate->val < IWL_RATE_COUNT) &&
3026 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
3027 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
3028 rate->val, iwl_rates[rate->val].plcp,
3029 (rate->flags & IEEE80211_RATE_BASIC) ?
3030 "*" : "");
3031 priv->active_rate |= (1 << rate->val);
3032 if (rate->flags & IEEE80211_RATE_BASIC)
3033 priv->active_rate_basic |= (1 << rate->val);
3034 } else
3035 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
3036 rate->val, iwl_rates[rate->val].plcp);
3039 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
3040 priv->active_rate, priv->active_rate_basic);
3043 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
3044 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
3045 * OFDM
3047 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
3048 priv->staging_rxon.cck_basic_rates =
3049 ((priv->active_rate_basic &
3050 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
3051 else
3052 priv->staging_rxon.cck_basic_rates =
3053 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
3055 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
3056 priv->staging_rxon.ofdm_basic_rates =
3057 ((priv->active_rate_basic &
3058 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
3059 IWL_FIRST_OFDM_RATE) & 0xFF;
3060 else
3061 priv->staging_rxon.ofdm_basic_rates =
3062 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
3065 static void iwl_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
3067 unsigned long flags;
3069 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
3070 return;
3072 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
3073 disable_radio ? "OFF" : "ON");
3075 if (disable_radio) {
3076 iwl_scan_cancel(priv);
3077 /* FIXME: This is a workaround for AP */
3078 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3079 spin_lock_irqsave(&priv->lock, flags);
3080 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
3081 CSR_UCODE_SW_BIT_RFKILL);
3082 spin_unlock_irqrestore(&priv->lock, flags);
3083 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
3084 set_bit(STATUS_RF_KILL_SW, &priv->status);
3086 return;
3089 spin_lock_irqsave(&priv->lock, flags);
3090 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3092 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3093 spin_unlock_irqrestore(&priv->lock, flags);
3095 /* wake up ucode */
3096 msleep(10);
3098 spin_lock_irqsave(&priv->lock, flags);
3099 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3100 if (!iwl_grab_restricted_access(priv))
3101 iwl_release_restricted_access(priv);
3102 spin_unlock_irqrestore(&priv->lock, flags);
3104 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3105 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3106 "disabled by HW switch\n");
3107 return;
3110 queue_work(priv->workqueue, &priv->restart);
3111 return;
3114 void iwl_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
3115 u32 decrypt_res, struct ieee80211_rx_status *stats)
3117 u16 fc =
3118 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3120 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3121 return;
3123 if (!(fc & IEEE80211_FCTL_PROTECTED))
3124 return;
3126 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3127 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3128 case RX_RES_STATUS_SEC_TYPE_TKIP:
3129 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3130 RX_RES_STATUS_BAD_ICV_MIC)
3131 stats->flag |= RX_FLAG_MMIC_ERROR;
3132 case RX_RES_STATUS_SEC_TYPE_WEP:
3133 case RX_RES_STATUS_SEC_TYPE_CCMP:
3134 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3135 RX_RES_STATUS_DECRYPT_OK) {
3136 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3137 stats->flag |= RX_FLAG_DECRYPTED;
3139 break;
3141 default:
3142 break;
3146 void iwl_handle_data_packet_monitor(struct iwl_priv *priv,
3147 struct iwl_rx_mem_buffer *rxb,
3148 void *data, short len,
3149 struct ieee80211_rx_status *stats,
3150 u16 phy_flags)
3152 struct iwl_rt_rx_hdr *iwl_rt;
3154 /* First cache any information we need before we overwrite
3155 * the information provided in the skb from the hardware */
3156 s8 signal = stats->ssi;
3157 s8 noise = 0;
3158 int rate = stats->rate;
3159 u64 tsf = stats->mactime;
3160 __le16 phy_flags_hw = cpu_to_le16(phy_flags);
3162 /* We received data from the HW, so stop the watchdog */
3163 if (len > IWL_RX_BUF_SIZE - sizeof(*iwl_rt)) {
3164 IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
3165 return;
3168 /* copy the frame data to write after where the radiotap header goes */
3169 iwl_rt = (void *)rxb->skb->data;
3170 memmove(iwl_rt->payload, data, len);
3172 iwl_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
3173 iwl_rt->rt_hdr.it_pad = 0; /* always good to zero */
3175 /* total header + data */
3176 iwl_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl_rt));
3178 /* Set the size of the skb to the size of the frame */
3179 skb_put(rxb->skb, sizeof(*iwl_rt) + len);
3181 /* Big bitfield of all the fields we provide in radiotap */
3182 iwl_rt->rt_hdr.it_present =
3183 cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
3184 (1 << IEEE80211_RADIOTAP_FLAGS) |
3185 (1 << IEEE80211_RADIOTAP_RATE) |
3186 (1 << IEEE80211_RADIOTAP_CHANNEL) |
3187 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
3188 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
3189 (1 << IEEE80211_RADIOTAP_ANTENNA));
3191 /* Zero the flags, we'll add to them as we go */
3192 iwl_rt->rt_flags = 0;
3194 iwl_rt->rt_tsf = cpu_to_le64(tsf);
3196 /* Convert to dBm */
3197 iwl_rt->rt_dbmsignal = signal;
3198 iwl_rt->rt_dbmnoise = noise;
3200 /* Convert the channel frequency and set the flags */
3201 iwl_rt->rt_channelMHz = cpu_to_le16(stats->freq);
3202 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
3203 iwl_rt->rt_chbitmask =
3204 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
3205 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
3206 iwl_rt->rt_chbitmask =
3207 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
3208 else /* 802.11g */
3209 iwl_rt->rt_chbitmask =
3210 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
3212 rate = iwl_rate_index_from_plcp(rate);
3213 if (rate == -1)
3214 iwl_rt->rt_rate = 0;
3215 else
3216 iwl_rt->rt_rate = iwl_rates[rate].ieee;
3218 /* antenna number */
3219 iwl_rt->rt_antenna =
3220 le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
3222 /* set the preamble flag if we have it */
3223 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
3224 iwl_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3226 IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
3228 stats->flag |= RX_FLAG_RADIOTAP;
3229 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3230 rxb->skb = NULL;
3234 #define IWL_PACKET_RETRY_TIME HZ
3236 int is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
3238 u16 sc = le16_to_cpu(header->seq_ctrl);
3239 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3240 u16 frag = sc & IEEE80211_SCTL_FRAG;
3241 u16 *last_seq, *last_frag;
3242 unsigned long *last_time;
3244 switch (priv->iw_mode) {
3245 case IEEE80211_IF_TYPE_IBSS:{
3246 struct list_head *p;
3247 struct iwl_ibss_seq *entry = NULL;
3248 u8 *mac = header->addr2;
3249 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3251 __list_for_each(p, &priv->ibss_mac_hash[index]) {
3252 entry =
3253 list_entry(p, struct iwl_ibss_seq, list);
3254 if (!compare_ether_addr(entry->mac, mac))
3255 break;
3257 if (p == &priv->ibss_mac_hash[index]) {
3258 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3259 if (!entry) {
3260 IWL_ERROR
3261 ("Cannot malloc new mac entry\n");
3262 return 0;
3264 memcpy(entry->mac, mac, ETH_ALEN);
3265 entry->seq_num = seq;
3266 entry->frag_num = frag;
3267 entry->packet_time = jiffies;
3268 list_add(&entry->list,
3269 &priv->ibss_mac_hash[index]);
3270 return 0;
3272 last_seq = &entry->seq_num;
3273 last_frag = &entry->frag_num;
3274 last_time = &entry->packet_time;
3275 break;
3277 case IEEE80211_IF_TYPE_STA:
3278 last_seq = &priv->last_seq_num;
3279 last_frag = &priv->last_frag_num;
3280 last_time = &priv->last_packet_time;
3281 break;
3282 default:
3283 return 0;
3285 if ((*last_seq == seq) &&
3286 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3287 if (*last_frag == frag)
3288 goto drop;
3289 if (*last_frag + 1 != frag)
3290 /* out-of-order fragment */
3291 goto drop;
3292 } else
3293 *last_seq = seq;
3295 *last_frag = frag;
3296 *last_time = jiffies;
3297 return 0;
3299 drop:
3300 return 1;
3303 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3305 #include "iwl-spectrum.h"
3307 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
3308 #define BEACON_TIME_MASK_HIGH 0xFF000000
3309 #define TIME_UNIT 1024
3312 * extended beacon time format
3313 * time in usec will be changed into a 32-bit value in 8:24 format
3314 * the high 1 byte is the beacon counts
3315 * the lower 3 bytes is the time in usec within one beacon interval
3318 static u32 iwl_usecs_to_beacons(u32 usec, u32 beacon_interval)
3320 u32 quot;
3321 u32 rem;
3322 u32 interval = beacon_interval * 1024;
3324 if (!interval || !usec)
3325 return 0;
3327 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3328 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3330 return (quot << 24) + rem;
3333 /* base is usually what we get from ucode with each received frame,
3334 * the same as HW timer counter counting down
3337 static __le32 iwl_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
3339 u32 base_low = base & BEACON_TIME_MASK_LOW;
3340 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3341 u32 interval = beacon_interval * TIME_UNIT;
3342 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3343 (addon & BEACON_TIME_MASK_HIGH);
3345 if (base_low > addon_low)
3346 res += base_low - addon_low;
3347 else if (base_low < addon_low) {
3348 res += interval + base_low - addon_low;
3349 res += (1 << 24);
3350 } else
3351 res += (1 << 24);
3353 return cpu_to_le32(res);
3356 static int iwl_get_measurement(struct iwl_priv *priv,
3357 struct ieee80211_measurement_params *params,
3358 u8 type)
3360 struct iwl_spectrum_cmd spectrum;
3361 struct iwl_rx_packet *res;
3362 struct iwl_host_cmd cmd = {
3363 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3364 .data = (void *)&spectrum,
3365 .meta.flags = CMD_WANT_SKB,
3367 u32 add_time = le64_to_cpu(params->start_time);
3368 int rc;
3369 int spectrum_resp_status;
3370 int duration = le16_to_cpu(params->duration);
3372 if (iwl_is_associated(priv))
3373 add_time =
3374 iwl_usecs_to_beacons(
3375 le64_to_cpu(params->start_time) - priv->last_tsf,
3376 le16_to_cpu(priv->rxon_timing.beacon_interval));
3378 memset(&spectrum, 0, sizeof(spectrum));
3380 spectrum.channel_count = cpu_to_le16(1);
3381 spectrum.flags =
3382 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3383 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3384 cmd.len = sizeof(spectrum);
3385 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3387 if (iwl_is_associated(priv))
3388 spectrum.start_time =
3389 iwl_add_beacon_time(priv->last_beacon_time,
3390 add_time,
3391 le16_to_cpu(priv->rxon_timing.beacon_interval));
3392 else
3393 spectrum.start_time = 0;
3395 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3396 spectrum.channels[0].channel = params->channel;
3397 spectrum.channels[0].type = type;
3398 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3399 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3400 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3402 rc = iwl_send_cmd_sync(priv, &cmd);
3403 if (rc)
3404 return rc;
3406 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
3407 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3408 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3409 rc = -EIO;
3412 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3413 switch (spectrum_resp_status) {
3414 case 0: /* Command will be handled */
3415 if (res->u.spectrum.id != 0xff) {
3416 IWL_DEBUG_INFO
3417 ("Replaced existing measurement: %d\n",
3418 res->u.spectrum.id);
3419 priv->measurement_status &= ~MEASUREMENT_READY;
3421 priv->measurement_status |= MEASUREMENT_ACTIVE;
3422 rc = 0;
3423 break;
3425 case 1: /* Command will not be handled */
3426 rc = -EAGAIN;
3427 break;
3430 dev_kfree_skb_any(cmd.meta.u.skb);
3432 return rc;
3434 #endif
3436 static void iwl_txstatus_to_ieee(struct iwl_priv *priv,
3437 struct iwl_tx_info *tx_sta)
3440 tx_sta->status.ack_signal = 0;
3441 tx_sta->status.excessive_retries = 0;
3442 tx_sta->status.queue_length = 0;
3443 tx_sta->status.queue_number = 0;
3445 if (in_interrupt())
3446 ieee80211_tx_status_irqsafe(priv->hw,
3447 tx_sta->skb[0], &(tx_sta->status));
3448 else
3449 ieee80211_tx_status(priv->hw,
3450 tx_sta->skb[0], &(tx_sta->status));
3452 tx_sta->skb[0] = NULL;
3456 * iwl_tx_queue_reclaim - Reclaim Tx queue entries no more used by NIC.
3458 * When FW advances 'R' index, all entries between old and
3459 * new 'R' index need to be reclaimed. As result, some free space
3460 * forms. If there is enough free space (> low mark), wake Tx queue.
3462 int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
3464 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3465 struct iwl_queue *q = &txq->q;
3466 int nfreed = 0;
3468 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3469 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3470 "is out of range [0-%d] %d %d.\n", txq_id,
3471 index, q->n_bd, q->write_ptr, q->read_ptr);
3472 return 0;
3475 for (index = iwl_queue_inc_wrap(index, q->n_bd);
3476 q->read_ptr != index;
3477 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3478 if (txq_id != IWL_CMD_QUEUE_NUM) {
3479 iwl_txstatus_to_ieee(priv,
3480 &(txq->txb[txq->q.read_ptr]));
3481 iwl_hw_txq_free_tfd(priv, txq);
3482 } else if (nfreed > 1) {
3483 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
3484 q->write_ptr, q->read_ptr);
3485 queue_work(priv->workqueue, &priv->restart);
3487 nfreed++;
3490 if (iwl_queue_space(q) > q->low_mark && (txq_id >= 0) &&
3491 (txq_id != IWL_CMD_QUEUE_NUM) &&
3492 priv->mac80211_registered)
3493 ieee80211_wake_queue(priv->hw, txq_id);
3496 return nfreed;
3499 static int iwl_is_tx_success(u32 status)
3501 status &= TX_STATUS_MSK;
3502 return (status == TX_STATUS_SUCCESS)
3503 || (status == TX_STATUS_DIRECT_DONE);
3506 /******************************************************************************
3508 * Generic RX handler implementations
3510 ******************************************************************************/
3511 #ifdef CONFIG_IWLWIFI_HT
3512 #ifdef CONFIG_IWLWIFI_HT_AGG
3514 static inline int iwl_get_ra_sta_id(struct iwl_priv *priv,
3515 struct ieee80211_hdr *hdr)
3517 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3518 return IWL_AP_ID;
3519 else {
3520 u8 *da = ieee80211_get_DA(hdr);
3521 return iwl_hw_find_station(priv, da);
3525 static struct ieee80211_hdr *iwl_tx_queue_get_hdr(
3526 struct iwl_priv *priv, int txq_id, int idx)
3528 if (priv->txq[txq_id].txb[idx].skb[0])
3529 return (struct ieee80211_hdr *)priv->txq[txq_id].
3530 txb[idx].skb[0]->data;
3531 return NULL;
3534 static inline u32 iwl_get_scd_ssn(struct iwl_tx_resp *tx_resp)
3536 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3537 tx_resp->frame_count);
3538 return le32_to_cpu(*scd_ssn) & MAX_SN;
3541 static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
3542 struct iwl_ht_agg *agg,
3543 struct iwl_tx_resp *tx_resp,
3544 u16 start_idx)
3546 u32 status;
3547 __le32 *frame_status = &tx_resp->status;
3548 struct ieee80211_tx_status *tx_status = NULL;
3549 struct ieee80211_hdr *hdr = NULL;
3550 int i, sh;
3551 int txq_id, idx;
3552 u16 seq;
3554 if (agg->wait_for_ba)
3555 IWL_DEBUG_TX_REPLY("got tx repsons w/o back\n");
3557 agg->frame_count = tx_resp->frame_count;
3558 agg->start_idx = start_idx;
3559 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3560 agg->bitmap0 = agg->bitmap1 = 0;
3562 if (agg->frame_count == 1) {
3563 struct iwl_tx_queue *txq ;
3564 status = le32_to_cpu(frame_status[0]);
3566 txq_id = agg->txq_id;
3567 txq = &priv->txq[txq_id];
3568 /* FIXME: code repetition */
3569 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d \n",
3570 agg->frame_count, agg->start_idx);
3572 tx_status = &(priv->txq[txq_id].txb[txq->q.read_ptr].status);
3573 tx_status->retry_count = tx_resp->failure_frame;
3574 tx_status->queue_number = status & 0xff;
3575 tx_status->queue_length = tx_resp->bt_kill_count;
3576 tx_status->queue_length |= tx_resp->failure_rts;
3578 tx_status->flags = iwl_is_tx_success(status)?
3579 IEEE80211_TX_STATUS_ACK : 0;
3580 tx_status->control.tx_rate =
3581 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags);
3582 /* FIXME: code repetition end */
3584 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3585 status & 0xff, tx_resp->failure_frame);
3586 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
3587 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags));
3589 agg->wait_for_ba = 0;
3590 } else {
3591 u64 bitmap = 0;
3592 int start = agg->start_idx;
3594 for (i = 0; i < agg->frame_count; i++) {
3595 u16 sc;
3596 status = le32_to_cpu(frame_status[i]);
3597 seq = status >> 16;
3598 idx = SEQ_TO_INDEX(seq);
3599 txq_id = SEQ_TO_QUEUE(seq);
3601 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3602 AGG_TX_STATE_ABORT_MSK))
3603 continue;
3605 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3606 agg->frame_count, txq_id, idx);
3608 hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
3610 sc = le16_to_cpu(hdr->seq_ctrl);
3611 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3612 IWL_ERROR("BUG_ON idx doesn't match seq control"
3613 " idx=%d, seq_idx=%d, seq=%d\n",
3614 idx, SEQ_TO_SN(sc),
3615 hdr->seq_ctrl);
3616 return -1;
3619 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3620 i, idx, SEQ_TO_SN(sc));
3622 sh = idx - start;
3623 if (sh > 64) {
3624 sh = (start - idx) + 0xff;
3625 bitmap = bitmap << sh;
3626 sh = 0;
3627 start = idx;
3628 } else if (sh < -64)
3629 sh = 0xff - (start - idx);
3630 else if (sh < 0) {
3631 sh = start - idx;
3632 start = idx;
3633 bitmap = bitmap << sh;
3634 sh = 0;
3636 bitmap |= (1 << sh);
3637 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3638 start, (u32)(bitmap & 0xFFFFFFFF));
3641 agg->bitmap0 = bitmap & 0xFFFFFFFF;
3642 agg->bitmap1 = bitmap >> 32;
3643 agg->start_idx = start;
3644 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3645 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%x\n",
3646 agg->frame_count, agg->start_idx,
3647 agg->bitmap0);
3649 if (bitmap)
3650 agg->wait_for_ba = 1;
3652 return 0;
3654 #endif
3655 #endif
3657 static void iwl_rx_reply_tx(struct iwl_priv *priv,
3658 struct iwl_rx_mem_buffer *rxb)
3660 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3661 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3662 int txq_id = SEQ_TO_QUEUE(sequence);
3663 int index = SEQ_TO_INDEX(sequence);
3664 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3665 struct ieee80211_tx_status *tx_status;
3666 struct iwl_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
3667 u32 status = le32_to_cpu(tx_resp->status);
3668 #ifdef CONFIG_IWLWIFI_HT
3669 #ifdef CONFIG_IWLWIFI_HT_AGG
3670 int tid, sta_id;
3671 #endif
3672 #endif
3674 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3675 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3676 "is out of range [0-%d] %d %d\n", txq_id,
3677 index, txq->q.n_bd, txq->q.write_ptr,
3678 txq->q.read_ptr);
3679 return;
3682 #ifdef CONFIG_IWLWIFI_HT
3683 #ifdef CONFIG_IWLWIFI_HT_AGG
3684 if (txq->sched_retry) {
3685 const u32 scd_ssn = iwl_get_scd_ssn(tx_resp);
3686 struct ieee80211_hdr *hdr =
3687 iwl_tx_queue_get_hdr(priv, txq_id, index);
3688 struct iwl_ht_agg *agg = NULL;
3689 __le16 *qc = ieee80211_get_qos_ctrl(hdr);
3691 if (qc == NULL) {
3692 IWL_ERROR("BUG_ON qc is null!!!!\n");
3693 return;
3696 tid = le16_to_cpu(*qc) & 0xf;
3698 sta_id = iwl_get_ra_sta_id(priv, hdr);
3699 if (unlikely(sta_id == IWL_INVALID_STATION)) {
3700 IWL_ERROR("Station not known for\n");
3701 return;
3704 agg = &priv->stations[sta_id].tid[tid].agg;
3706 iwl4965_tx_status_reply_tx(priv, agg, tx_resp, index);
3708 if ((tx_resp->frame_count == 1) &&
3709 !iwl_is_tx_success(status)) {
3710 /* TODO: send BAR */
3713 if ((txq->q.read_ptr != (scd_ssn & 0xff))) {
3714 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
3715 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3716 "%d index %d\n", scd_ssn , index);
3717 iwl_tx_queue_reclaim(priv, txq_id, index);
3719 } else {
3720 #endif /* CONFIG_IWLWIFI_HT_AGG */
3721 #endif /* CONFIG_IWLWIFI_HT */
3722 tx_status = &(txq->txb[txq->q.read_ptr].status);
3724 tx_status->retry_count = tx_resp->failure_frame;
3725 tx_status->queue_number = status;
3726 tx_status->queue_length = tx_resp->bt_kill_count;
3727 tx_status->queue_length |= tx_resp->failure_rts;
3729 tx_status->flags =
3730 iwl_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
3732 tx_status->control.tx_rate =
3733 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags);
3735 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
3736 "retries %d\n", txq_id, iwl_get_tx_fail_reason(status),
3737 status, le32_to_cpu(tx_resp->rate_n_flags),
3738 tx_resp->failure_frame);
3740 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3741 if (index != -1)
3742 iwl_tx_queue_reclaim(priv, txq_id, index);
3743 #ifdef CONFIG_IWLWIFI_HT
3744 #ifdef CONFIG_IWLWIFI_HT_AGG
3746 #endif /* CONFIG_IWLWIFI_HT_AGG */
3747 #endif /* CONFIG_IWLWIFI_HT */
3749 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3750 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3754 static void iwl_rx_reply_alive(struct iwl_priv *priv,
3755 struct iwl_rx_mem_buffer *rxb)
3757 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3758 struct iwl_alive_resp *palive;
3759 struct delayed_work *pwork;
3761 palive = &pkt->u.alive_frame;
3763 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3764 "0x%01X 0x%01X\n",
3765 palive->is_valid, palive->ver_type,
3766 palive->ver_subtype);
3768 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3769 IWL_DEBUG_INFO("Initialization Alive received.\n");
3770 memcpy(&priv->card_alive_init,
3771 &pkt->u.alive_frame,
3772 sizeof(struct iwl_init_alive_resp));
3773 pwork = &priv->init_alive_start;
3774 } else {
3775 IWL_DEBUG_INFO("Runtime Alive received.\n");
3776 memcpy(&priv->card_alive, &pkt->u.alive_frame,
3777 sizeof(struct iwl_alive_resp));
3778 pwork = &priv->alive_start;
3781 /* We delay the ALIVE response by 5ms to
3782 * give the HW RF Kill time to activate... */
3783 if (palive->is_valid == UCODE_VALID_OK)
3784 queue_delayed_work(priv->workqueue, pwork,
3785 msecs_to_jiffies(5));
3786 else
3787 IWL_WARNING("uCode did not respond OK.\n");
3790 static void iwl_rx_reply_add_sta(struct iwl_priv *priv,
3791 struct iwl_rx_mem_buffer *rxb)
3793 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3795 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3796 return;
3799 static void iwl_rx_reply_error(struct iwl_priv *priv,
3800 struct iwl_rx_mem_buffer *rxb)
3802 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3804 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3805 "seq 0x%04X ser 0x%08X\n",
3806 le32_to_cpu(pkt->u.err_resp.error_type),
3807 get_cmd_string(pkt->u.err_resp.cmd_id),
3808 pkt->u.err_resp.cmd_id,
3809 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3810 le32_to_cpu(pkt->u.err_resp.error_info));
3813 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3815 static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
3817 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3818 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
3819 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
3820 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3821 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3822 rxon->channel = csa->channel;
3823 priv->staging_rxon.channel = csa->channel;
3826 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
3827 struct iwl_rx_mem_buffer *rxb)
3829 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3830 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3831 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
3833 if (!report->state) {
3834 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3835 "Spectrum Measure Notification: Start\n");
3836 return;
3839 memcpy(&priv->measure_report, report, sizeof(*report));
3840 priv->measurement_status |= MEASUREMENT_READY;
3841 #endif
3844 static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
3845 struct iwl_rx_mem_buffer *rxb)
3847 #ifdef CONFIG_IWLWIFI_DEBUG
3848 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3849 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
3850 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3851 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3852 #endif
3855 static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
3856 struct iwl_rx_mem_buffer *rxb)
3858 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3859 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3860 "notification for %s:\n",
3861 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
3862 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
3865 static void iwl_bg_beacon_update(struct work_struct *work)
3867 struct iwl_priv *priv =
3868 container_of(work, struct iwl_priv, beacon_update);
3869 struct sk_buff *beacon;
3871 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3872 beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
3874 if (!beacon) {
3875 IWL_ERROR("update beacon failed\n");
3876 return;
3879 mutex_lock(&priv->mutex);
3880 /* new beacon skb is allocated every time; dispose previous.*/
3881 if (priv->ibss_beacon)
3882 dev_kfree_skb(priv->ibss_beacon);
3884 priv->ibss_beacon = beacon;
3885 mutex_unlock(&priv->mutex);
3887 iwl_send_beacon_cmd(priv);
3890 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
3891 struct iwl_rx_mem_buffer *rxb)
3893 #ifdef CONFIG_IWLWIFI_DEBUG
3894 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3895 struct iwl_beacon_notif *beacon = &(pkt->u.beacon_status);
3896 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
3898 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3899 "tsf %d %d rate %d\n",
3900 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3901 beacon->beacon_notify_hdr.failure_frame,
3902 le32_to_cpu(beacon->ibss_mgr_status),
3903 le32_to_cpu(beacon->high_tsf),
3904 le32_to_cpu(beacon->low_tsf), rate);
3905 #endif
3907 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3908 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3909 queue_work(priv->workqueue, &priv->beacon_update);
3912 /* Service response to REPLY_SCAN_CMD (0x80) */
3913 static void iwl_rx_reply_scan(struct iwl_priv *priv,
3914 struct iwl_rx_mem_buffer *rxb)
3916 #ifdef CONFIG_IWLWIFI_DEBUG
3917 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3918 struct iwl_scanreq_notification *notif =
3919 (struct iwl_scanreq_notification *)pkt->u.raw;
3921 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3922 #endif
3925 /* Service SCAN_START_NOTIFICATION (0x82) */
3926 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
3927 struct iwl_rx_mem_buffer *rxb)
3929 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3930 struct iwl_scanstart_notification *notif =
3931 (struct iwl_scanstart_notification *)pkt->u.raw;
3932 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3933 IWL_DEBUG_SCAN("Scan start: "
3934 "%d [802.11%s] "
3935 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3936 notif->channel,
3937 notif->band ? "bg" : "a",
3938 notif->tsf_high,
3939 notif->tsf_low, notif->status, notif->beacon_timer);
3942 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
3943 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
3944 struct iwl_rx_mem_buffer *rxb)
3946 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3947 struct iwl_scanresults_notification *notif =
3948 (struct iwl_scanresults_notification *)pkt->u.raw;
3950 IWL_DEBUG_SCAN("Scan ch.res: "
3951 "%d [802.11%s] "
3952 "(TSF: 0x%08X:%08X) - %d "
3953 "elapsed=%lu usec (%dms since last)\n",
3954 notif->channel,
3955 notif->band ? "bg" : "a",
3956 le32_to_cpu(notif->tsf_high),
3957 le32_to_cpu(notif->tsf_low),
3958 le32_to_cpu(notif->statistics[0]),
3959 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3960 jiffies_to_msecs(elapsed_jiffies
3961 (priv->last_scan_jiffies, jiffies)));
3963 priv->last_scan_jiffies = jiffies;
3966 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
3967 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
3968 struct iwl_rx_mem_buffer *rxb)
3970 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3971 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
3973 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3974 scan_notif->scanned_channels,
3975 scan_notif->tsf_low,
3976 scan_notif->tsf_high, scan_notif->status);
3978 /* The HW is no longer scanning */
3979 clear_bit(STATUS_SCAN_HW, &priv->status);
3981 /* The scan completion notification came in, so kill that timer... */
3982 cancel_delayed_work(&priv->scan_check);
3984 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3985 (priv->scan_bands == 2) ? "2.4" : "5.2",
3986 jiffies_to_msecs(elapsed_jiffies
3987 (priv->scan_pass_start, jiffies)));
3989 /* Remove this scanned band from the list
3990 * of pending bands to scan */
3991 priv->scan_bands--;
3993 /* If a request to abort was given, or the scan did not succeed
3994 * then we reset the scan state machine and terminate,
3995 * re-queuing another scan if one has been requested */
3996 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3997 IWL_DEBUG_INFO("Aborted scan completed.\n");
3998 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3999 } else {
4000 /* If there are more bands on this scan pass reschedule */
4001 if (priv->scan_bands > 0)
4002 goto reschedule;
4005 priv->last_scan_jiffies = jiffies;
4006 IWL_DEBUG_INFO("Setting scan to off\n");
4008 clear_bit(STATUS_SCANNING, &priv->status);
4010 IWL_DEBUG_INFO("Scan took %dms\n",
4011 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
4013 queue_work(priv->workqueue, &priv->scan_completed);
4015 return;
4017 reschedule:
4018 priv->scan_pass_start = jiffies;
4019 queue_work(priv->workqueue, &priv->request_scan);
4022 /* Handle notification from uCode that card's power state is changing
4023 * due to software, hardware, or critical temperature RFKILL */
4024 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
4025 struct iwl_rx_mem_buffer *rxb)
4027 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
4028 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
4029 unsigned long status = priv->status;
4031 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
4032 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
4033 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
4035 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
4036 RF_CARD_DISABLED)) {
4038 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
4039 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4041 if (!iwl_grab_restricted_access(priv)) {
4042 iwl_write_restricted(
4043 priv, HBUS_TARG_MBX_C,
4044 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4046 iwl_release_restricted_access(priv);
4049 if (!(flags & RXON_CARD_DISABLED)) {
4050 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
4051 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4052 if (!iwl_grab_restricted_access(priv)) {
4053 iwl_write_restricted(
4054 priv, HBUS_TARG_MBX_C,
4055 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4057 iwl_release_restricted_access(priv);
4061 if (flags & RF_CARD_DISABLED) {
4062 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
4063 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
4064 iwl_read32(priv, CSR_UCODE_DRV_GP1);
4065 if (!iwl_grab_restricted_access(priv))
4066 iwl_release_restricted_access(priv);
4070 if (flags & HW_CARD_DISABLED)
4071 set_bit(STATUS_RF_KILL_HW, &priv->status);
4072 else
4073 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4076 if (flags & SW_CARD_DISABLED)
4077 set_bit(STATUS_RF_KILL_SW, &priv->status);
4078 else
4079 clear_bit(STATUS_RF_KILL_SW, &priv->status);
4081 if (!(flags & RXON_CARD_DISABLED))
4082 iwl_scan_cancel(priv);
4084 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
4085 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
4086 (test_bit(STATUS_RF_KILL_SW, &status) !=
4087 test_bit(STATUS_RF_KILL_SW, &priv->status)))
4088 queue_work(priv->workqueue, &priv->rf_kill);
4089 else
4090 wake_up_interruptible(&priv->wait_command_queue);
4094 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
4096 * Setup the RX handlers for each of the reply types sent from the uCode
4097 * to the host.
4099 * This function chains into the hardware specific files for them to setup
4100 * any hardware specific handlers as well.
4102 static void iwl_setup_rx_handlers(struct iwl_priv *priv)
4104 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
4105 priv->rx_handlers[REPLY_ADD_STA] = iwl_rx_reply_add_sta;
4106 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
4107 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
4108 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
4109 iwl_rx_spectrum_measure_notif;
4110 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
4111 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
4112 iwl_rx_pm_debug_statistics_notif;
4113 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
4115 /* NOTE: iwl_rx_statistics is different based on whether
4116 * the build is for the 3945 or the 4965. See the
4117 * corresponding implementation in iwl-XXXX.c
4119 * The same handler is used for both the REPLY to a
4120 * discrete statistics request from the host as well as
4121 * for the periodic statistics notification from the uCode
4123 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_hw_rx_statistics;
4124 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_hw_rx_statistics;
4126 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
4127 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
4128 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
4129 iwl_rx_scan_results_notif;
4130 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
4131 iwl_rx_scan_complete_notif;
4132 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
4133 priv->rx_handlers[REPLY_TX] = iwl_rx_reply_tx;
4135 /* Setup hardware specific Rx handlers */
4136 iwl_hw_rx_handler_setup(priv);
4140 * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
4141 * @rxb: Rx buffer to reclaim
4143 * If an Rx buffer has an async callback associated with it the callback
4144 * will be executed. The attached skb (if present) will only be freed
4145 * if the callback returns 1
4147 static void iwl_tx_cmd_complete(struct iwl_priv *priv,
4148 struct iwl_rx_mem_buffer *rxb)
4150 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
4151 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
4152 int txq_id = SEQ_TO_QUEUE(sequence);
4153 int index = SEQ_TO_INDEX(sequence);
4154 int huge = sequence & SEQ_HUGE_FRAME;
4155 int cmd_index;
4156 struct iwl_cmd *cmd;
4158 /* If a Tx command is being handled and it isn't in the actual
4159 * command queue then there a command routing bug has been introduced
4160 * in the queue management code. */
4161 if (txq_id != IWL_CMD_QUEUE_NUM)
4162 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
4163 txq_id, pkt->hdr.cmd);
4164 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
4166 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
4167 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
4169 /* Input error checking is done when commands are added to queue. */
4170 if (cmd->meta.flags & CMD_WANT_SKB) {
4171 cmd->meta.source->u.skb = rxb->skb;
4172 rxb->skb = NULL;
4173 } else if (cmd->meta.u.callback &&
4174 !cmd->meta.u.callback(priv, cmd, rxb->skb))
4175 rxb->skb = NULL;
4177 iwl_tx_queue_reclaim(priv, txq_id, index);
4179 if (!(cmd->meta.flags & CMD_ASYNC)) {
4180 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4181 wake_up_interruptible(&priv->wait_command_queue);
4185 /************************** RX-FUNCTIONS ****************************/
4187 * Rx theory of operation
4189 * The host allocates 32 DMA target addresses and passes the host address
4190 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
4191 * 0 to 31
4193 * Rx Queue Indexes
4194 * The host/firmware share two index registers for managing the Rx buffers.
4196 * The READ index maps to the first position that the firmware may be writing
4197 * to -- the driver can read up to (but not including) this position and get
4198 * good data.
4199 * The READ index is managed by the firmware once the card is enabled.
4201 * The WRITE index maps to the last position the driver has read from -- the
4202 * position preceding WRITE is the last slot the firmware can place a packet.
4204 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
4205 * WRITE = READ.
4207 * During initialization the host sets up the READ queue position to the first
4208 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4210 * When the firmware places a packet in a buffer it will advance the READ index
4211 * and fire the RX interrupt. The driver can then query the READ index and
4212 * process as many packets as possible, moving the WRITE index forward as it
4213 * resets the Rx queue buffers with new memory.
4215 * The management in the driver is as follows:
4216 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
4217 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
4218 * to replenish the iwl->rxq->rx_free.
4219 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
4220 * iwl->rxq is replenished and the READ INDEX is updated (updating the
4221 * 'processed' and 'read' driver indexes as well)
4222 * + A received packet is processed and handed to the kernel network stack,
4223 * detached from the iwl->rxq. The driver 'processed' index is updated.
4224 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
4225 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
4226 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
4227 * were enough free buffers and RX_STALLED is set it is cleared.
4230 * Driver sequence:
4232 * iwl_rx_queue_alloc() Allocates rx_free
4233 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
4234 * iwl_rx_queue_restock
4235 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
4236 * queue, updates firmware pointers, and updates
4237 * the WRITE index. If insufficient rx_free buffers
4238 * are available, schedules iwl_rx_replenish
4240 * -- enable interrupts --
4241 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
4242 * READ INDEX, detaching the SKB from the pool.
4243 * Moves the packet buffer from queue to rx_used.
4244 * Calls iwl_rx_queue_restock to refill any empty
4245 * slots.
4246 * ...
4251 * iwl_rx_queue_space - Return number of free slots available in queue.
4253 static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
4255 int s = q->read - q->write;
4256 if (s <= 0)
4257 s += RX_QUEUE_SIZE;
4258 /* keep some buffer to not confuse full and empty queue */
4259 s -= 2;
4260 if (s < 0)
4261 s = 0;
4262 return s;
4266 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
4268 * NOTE: This function has 3945 and 4965 specific code sections
4269 * but is declared in base due to the majority of the
4270 * implementation being the same (only a numeric constant is
4271 * different)
4274 int iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
4276 u32 reg = 0;
4277 int rc = 0;
4278 unsigned long flags;
4280 spin_lock_irqsave(&q->lock, flags);
4282 if (q->need_update == 0)
4283 goto exit_unlock;
4285 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4286 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4288 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4289 iwl_set_bit(priv, CSR_GP_CNTRL,
4290 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4291 goto exit_unlock;
4294 rc = iwl_grab_restricted_access(priv);
4295 if (rc)
4296 goto exit_unlock;
4298 iwl_write_restricted(priv, FH_RSCSR_CHNL0_WPTR,
4299 q->write & ~0x7);
4300 iwl_release_restricted_access(priv);
4301 } else
4302 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
4305 q->need_update = 0;
4307 exit_unlock:
4308 spin_unlock_irqrestore(&q->lock, flags);
4309 return rc;
4313 * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer pointer.
4315 * NOTE: This function has 3945 and 4965 specific code paths in it.
4317 static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
4318 dma_addr_t dma_addr)
4320 return cpu_to_le32((u32)(dma_addr >> 8));
4325 * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
4327 * If there are slots in the RX queue that need to be restocked,
4328 * and we have free pre-allocated buffers, fill the ranks as much
4329 * as we can pulling from rx_free.
4331 * This moves the 'write' index forward to catch up with 'processed', and
4332 * also updates the memory address in the firmware to reference the new
4333 * target buffer.
4335 int iwl_rx_queue_restock(struct iwl_priv *priv)
4337 struct iwl_rx_queue *rxq = &priv->rxq;
4338 struct list_head *element;
4339 struct iwl_rx_mem_buffer *rxb;
4340 unsigned long flags;
4341 int write, rc;
4343 spin_lock_irqsave(&rxq->lock, flags);
4344 write = rxq->write & ~0x7;
4345 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
4346 element = rxq->rx_free.next;
4347 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4348 list_del(element);
4349 rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->dma_addr);
4350 rxq->queue[rxq->write] = rxb;
4351 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4352 rxq->free_count--;
4354 spin_unlock_irqrestore(&rxq->lock, flags);
4355 /* If the pre-allocated buffer pool is dropping low, schedule to
4356 * refill it */
4357 if (rxq->free_count <= RX_LOW_WATERMARK)
4358 queue_work(priv->workqueue, &priv->rx_replenish);
4361 /* If we've added more space for the firmware to place data, tell it */
4362 if ((write != (rxq->write & ~0x7))
4363 || (abs(rxq->write - rxq->read) > 7)) {
4364 spin_lock_irqsave(&rxq->lock, flags);
4365 rxq->need_update = 1;
4366 spin_unlock_irqrestore(&rxq->lock, flags);
4367 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
4368 if (rc)
4369 return rc;
4372 return 0;
4376 * iwl_rx_replenish - Move all used packet from rx_used to rx_free
4378 * When moving to rx_free an SKB is allocated for the slot.
4380 * Also restock the Rx queue via iwl_rx_queue_restock.
4381 * This is called as a scheduled work item (except for during initialization)
4383 void iwl_rx_replenish(void *data)
4385 struct iwl_priv *priv = data;
4386 struct iwl_rx_queue *rxq = &priv->rxq;
4387 struct list_head *element;
4388 struct iwl_rx_mem_buffer *rxb;
4389 unsigned long flags;
4390 spin_lock_irqsave(&rxq->lock, flags);
4391 while (!list_empty(&rxq->rx_used)) {
4392 element = rxq->rx_used.next;
4393 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4394 rxb->skb =
4395 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
4396 if (!rxb->skb) {
4397 if (net_ratelimit())
4398 printk(KERN_CRIT DRV_NAME
4399 ": Can not allocate SKB buffers\n");
4400 /* We don't reschedule replenish work here -- we will
4401 * call the restock method and if it still needs
4402 * more buffers it will schedule replenish */
4403 break;
4405 priv->alloc_rxb_skb++;
4406 list_del(element);
4407 rxb->dma_addr =
4408 pci_map_single(priv->pci_dev, rxb->skb->data,
4409 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4410 list_add_tail(&rxb->list, &rxq->rx_free);
4411 rxq->free_count++;
4413 spin_unlock_irqrestore(&rxq->lock, flags);
4415 spin_lock_irqsave(&priv->lock, flags);
4416 iwl_rx_queue_restock(priv);
4417 spin_unlock_irqrestore(&priv->lock, flags);
4420 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
4421 * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
4422 * This free routine walks the list of POOL entries and if SKB is set to
4423 * non NULL it is unmapped and freed
4425 void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4427 int i;
4428 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4429 if (rxq->pool[i].skb != NULL) {
4430 pci_unmap_single(priv->pci_dev,
4431 rxq->pool[i].dma_addr,
4432 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4433 dev_kfree_skb(rxq->pool[i].skb);
4437 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4438 rxq->dma_addr);
4439 rxq->bd = NULL;
4442 int iwl_rx_queue_alloc(struct iwl_priv *priv)
4444 struct iwl_rx_queue *rxq = &priv->rxq;
4445 struct pci_dev *dev = priv->pci_dev;
4446 int i;
4448 spin_lock_init(&rxq->lock);
4449 INIT_LIST_HEAD(&rxq->rx_free);
4450 INIT_LIST_HEAD(&rxq->rx_used);
4451 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4452 if (!rxq->bd)
4453 return -ENOMEM;
4454 /* Fill the rx_used queue with _all_ of the Rx buffers */
4455 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4456 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4457 /* Set us so that we have processed and used all buffers, but have
4458 * not restocked the Rx queue with fresh buffers */
4459 rxq->read = rxq->write = 0;
4460 rxq->free_count = 0;
4461 rxq->need_update = 0;
4462 return 0;
4465 void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4467 unsigned long flags;
4468 int i;
4469 spin_lock_irqsave(&rxq->lock, flags);
4470 INIT_LIST_HEAD(&rxq->rx_free);
4471 INIT_LIST_HEAD(&rxq->rx_used);
4472 /* Fill the rx_used queue with _all_ of the Rx buffers */
4473 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4474 /* In the reset function, these buffers may have been allocated
4475 * to an SKB, so we need to unmap and free potential storage */
4476 if (rxq->pool[i].skb != NULL) {
4477 pci_unmap_single(priv->pci_dev,
4478 rxq->pool[i].dma_addr,
4479 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4480 priv->alloc_rxb_skb--;
4481 dev_kfree_skb(rxq->pool[i].skb);
4482 rxq->pool[i].skb = NULL;
4484 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4487 /* Set us so that we have processed and used all buffers, but have
4488 * not restocked the Rx queue with fresh buffers */
4489 rxq->read = rxq->write = 0;
4490 rxq->free_count = 0;
4491 spin_unlock_irqrestore(&rxq->lock, flags);
4494 /* Convert linear signal-to-noise ratio into dB */
4495 static u8 ratio2dB[100] = {
4496 /* 0 1 2 3 4 5 6 7 8 9 */
4497 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4498 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4499 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4500 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4501 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4502 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4503 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4504 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4505 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4506 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4509 /* Calculates a relative dB value from a ratio of linear
4510 * (i.e. not dB) signal levels.
4511 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
4512 int iwl_calc_db_from_ratio(int sig_ratio)
4514 /* 1000:1 or higher just report as 60 dB */
4515 if (sig_ratio >= 1000)
4516 return 60;
4518 /* 100:1 or higher, divide by 10 and use table,
4519 * add 20 dB to make up for divide by 10 */
4520 if (sig_ratio >= 100)
4521 return (20 + (int)ratio2dB[sig_ratio/10]);
4523 /* We shouldn't see this */
4524 if (sig_ratio < 1)
4525 return 0;
4527 /* Use table for ratios 1:1 - 99:1 */
4528 return (int)ratio2dB[sig_ratio];
4531 #define PERFECT_RSSI (-20) /* dBm */
4532 #define WORST_RSSI (-95) /* dBm */
4533 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4535 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
4536 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4537 * about formulas used below. */
4538 int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
4540 int sig_qual;
4541 int degradation = PERFECT_RSSI - rssi_dbm;
4543 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4544 * as indicator; formula is (signal dbm - noise dbm).
4545 * SNR at or above 40 is a great signal (100%).
4546 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4547 * Weakest usable signal is usually 10 - 15 dB SNR. */
4548 if (noise_dbm) {
4549 if (rssi_dbm - noise_dbm >= 40)
4550 return 100;
4551 else if (rssi_dbm < noise_dbm)
4552 return 0;
4553 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4555 /* Else use just the signal level.
4556 * This formula is a least squares fit of data points collected and
4557 * compared with a reference system that had a percentage (%) display
4558 * for signal quality. */
4559 } else
4560 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4561 (15 * RSSI_RANGE + 62 * degradation)) /
4562 (RSSI_RANGE * RSSI_RANGE);
4564 if (sig_qual > 100)
4565 sig_qual = 100;
4566 else if (sig_qual < 1)
4567 sig_qual = 0;
4569 return sig_qual;
4573 * iwl_rx_handle - Main entry function for receiving responses from the uCode
4575 * Uses the priv->rx_handlers callback function array to invoke
4576 * the appropriate handlers, including command responses,
4577 * frame-received notifications, and other notifications.
4579 static void iwl_rx_handle(struct iwl_priv *priv)
4581 struct iwl_rx_mem_buffer *rxb;
4582 struct iwl_rx_packet *pkt;
4583 struct iwl_rx_queue *rxq = &priv->rxq;
4584 u32 r, i;
4585 int reclaim;
4586 unsigned long flags;
4588 r = iwl_hw_get_rx_read(priv);
4589 i = rxq->read;
4591 /* Rx interrupt, but nothing sent from uCode */
4592 if (i == r)
4593 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4595 while (i != r) {
4596 rxb = rxq->queue[i];
4598 /* If an RXB doesn't have a queue slot associated with it
4599 * then a bug has been introduced in the queue refilling
4600 * routines -- catch it here */
4601 BUG_ON(rxb == NULL);
4603 rxq->queue[i] = NULL;
4605 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4606 IWL_RX_BUF_SIZE,
4607 PCI_DMA_FROMDEVICE);
4608 pkt = (struct iwl_rx_packet *)rxb->skb->data;
4610 /* Reclaim a command buffer only if this packet is a response
4611 * to a (driver-originated) command.
4612 * If the packet (e.g. Rx frame) originated from uCode,
4613 * there is no command buffer to reclaim.
4614 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4615 * but apparently a few don't get set; catch them here. */
4616 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4617 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4618 (pkt->hdr.cmd != REPLY_4965_RX) &&
4619 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
4620 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4621 (pkt->hdr.cmd != REPLY_TX);
4623 /* Based on type of command response or notification,
4624 * handle those that need handling via function in
4625 * rx_handlers table. See iwl_setup_rx_handlers() */
4626 if (priv->rx_handlers[pkt->hdr.cmd]) {
4627 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4628 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4629 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4630 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4631 } else {
4632 /* No handling needed */
4633 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4634 "r %d i %d No handler needed for %s, 0x%02x\n",
4635 r, i, get_cmd_string(pkt->hdr.cmd),
4636 pkt->hdr.cmd);
4639 if (reclaim) {
4640 /* Invoke any callbacks, transfer the skb to caller,
4641 * and fire off the (possibly) blocking iwl_send_cmd()
4642 * as we reclaim the driver command queue */
4643 if (rxb && rxb->skb)
4644 iwl_tx_cmd_complete(priv, rxb);
4645 else
4646 IWL_WARNING("Claim null rxb?\n");
4649 /* For now we just don't re-use anything. We can tweak this
4650 * later to try and re-use notification packets and SKBs that
4651 * fail to Rx correctly */
4652 if (rxb->skb != NULL) {
4653 priv->alloc_rxb_skb--;
4654 dev_kfree_skb_any(rxb->skb);
4655 rxb->skb = NULL;
4658 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4659 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4660 spin_lock_irqsave(&rxq->lock, flags);
4661 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4662 spin_unlock_irqrestore(&rxq->lock, flags);
4663 i = (i + 1) & RX_QUEUE_MASK;
4666 /* Backtrack one entry */
4667 priv->rxq.read = i;
4668 iwl_rx_queue_restock(priv);
4671 int iwl_tx_queue_update_write_ptr(struct iwl_priv *priv,
4672 struct iwl_tx_queue *txq)
4674 u32 reg = 0;
4675 int rc = 0;
4676 int txq_id = txq->q.id;
4678 if (txq->need_update == 0)
4679 return rc;
4681 /* if we're trying to save power */
4682 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4683 /* wake up nic if it's powered down ...
4684 * uCode will wake up, and interrupt us again, so next
4685 * time we'll skip this part. */
4686 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4688 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4689 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
4690 iwl_set_bit(priv, CSR_GP_CNTRL,
4691 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4692 return rc;
4695 /* restore this queue's parameters in nic hardware. */
4696 rc = iwl_grab_restricted_access(priv);
4697 if (rc)
4698 return rc;
4699 iwl_write_restricted(priv, HBUS_TARG_WRPTR,
4700 txq->q.write_ptr | (txq_id << 8));
4701 iwl_release_restricted_access(priv);
4703 /* else not in power-save mode, uCode will never sleep when we're
4704 * trying to tx (during RFKILL, we're not trying to tx). */
4705 } else
4706 iwl_write32(priv, HBUS_TARG_WRPTR,
4707 txq->q.write_ptr | (txq_id << 8));
4709 txq->need_update = 0;
4711 return rc;
4714 #ifdef CONFIG_IWLWIFI_DEBUG
4715 static void iwl_print_rx_config_cmd(struct iwl_rxon_cmd *rxon)
4717 DECLARE_MAC_BUF(mac);
4719 IWL_DEBUG_RADIO("RX CONFIG:\n");
4720 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
4721 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4722 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4723 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4724 le32_to_cpu(rxon->filter_flags));
4725 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4726 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4727 rxon->ofdm_basic_rates);
4728 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
4729 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4730 print_mac(mac, rxon->node_addr));
4731 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4732 print_mac(mac, rxon->bssid_addr));
4733 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4735 #endif
4737 static void iwl_enable_interrupts(struct iwl_priv *priv)
4739 IWL_DEBUG_ISR("Enabling interrupts\n");
4740 set_bit(STATUS_INT_ENABLED, &priv->status);
4741 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
4744 static inline void iwl_disable_interrupts(struct iwl_priv *priv)
4746 clear_bit(STATUS_INT_ENABLED, &priv->status);
4748 /* disable interrupts from uCode/NIC to host */
4749 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
4751 /* acknowledge/clear/reset any interrupts still pending
4752 * from uCode or flow handler (Rx/Tx DMA) */
4753 iwl_write32(priv, CSR_INT, 0xffffffff);
4754 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
4755 IWL_DEBUG_ISR("Disabled interrupts\n");
4758 static const char *desc_lookup(int i)
4760 switch (i) {
4761 case 1:
4762 return "FAIL";
4763 case 2:
4764 return "BAD_PARAM";
4765 case 3:
4766 return "BAD_CHECKSUM";
4767 case 4:
4768 return "NMI_INTERRUPT";
4769 case 5:
4770 return "SYSASSERT";
4771 case 6:
4772 return "FATAL_ERROR";
4775 return "UNKNOWN";
4778 #define ERROR_START_OFFSET (1 * sizeof(u32))
4779 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
4781 static void iwl_dump_nic_error_log(struct iwl_priv *priv)
4783 u32 data2, line;
4784 u32 desc, time, count, base, data1;
4785 u32 blink1, blink2, ilink1, ilink2;
4786 int rc;
4788 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4790 if (!iwl_hw_valid_rtc_data_addr(base)) {
4791 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4792 return;
4795 rc = iwl_grab_restricted_access(priv);
4796 if (rc) {
4797 IWL_WARNING("Can not read from adapter at this time.\n");
4798 return;
4801 count = iwl_read_restricted_mem(priv, base);
4803 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4804 IWL_ERROR("Start IWL Error Log Dump:\n");
4805 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4806 priv->status, priv->config, count);
4809 desc = iwl_read_restricted_mem(priv, base + 1 * sizeof(u32));
4810 blink1 = iwl_read_restricted_mem(priv, base + 3 * sizeof(u32));
4811 blink2 = iwl_read_restricted_mem(priv, base + 4 * sizeof(u32));
4812 ilink1 = iwl_read_restricted_mem(priv, base + 5 * sizeof(u32));
4813 ilink2 = iwl_read_restricted_mem(priv, base + 6 * sizeof(u32));
4814 data1 = iwl_read_restricted_mem(priv, base + 7 * sizeof(u32));
4815 data2 = iwl_read_restricted_mem(priv, base + 8 * sizeof(u32));
4816 line = iwl_read_restricted_mem(priv, base + 9 * sizeof(u32));
4817 time = iwl_read_restricted_mem(priv, base + 11 * sizeof(u32));
4819 IWL_ERROR("Desc Time "
4820 "data1 data2 line\n");
4821 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4822 desc_lookup(desc), desc, time, data1, data2, line);
4823 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4824 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4825 ilink1, ilink2);
4827 iwl_release_restricted_access(priv);
4830 #define EVENT_START_OFFSET (4 * sizeof(u32))
4833 * iwl_print_event_log - Dump error event log to syslog
4835 * NOTE: Must be called with iwl_grab_restricted_access() already obtained!
4837 static void iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
4838 u32 num_events, u32 mode)
4840 u32 i;
4841 u32 base; /* SRAM byte address of event log header */
4842 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4843 u32 ptr; /* SRAM byte address of log data */
4844 u32 ev, time, data; /* event log data */
4846 if (num_events == 0)
4847 return;
4849 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4851 if (mode == 0)
4852 event_size = 2 * sizeof(u32);
4853 else
4854 event_size = 3 * sizeof(u32);
4856 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4858 /* "time" is actually "data" for mode 0 (no timestamp).
4859 * place event id # at far right for easier visual parsing. */
4860 for (i = 0; i < num_events; i++) {
4861 ev = iwl_read_restricted_mem(priv, ptr);
4862 ptr += sizeof(u32);
4863 time = iwl_read_restricted_mem(priv, ptr);
4864 ptr += sizeof(u32);
4865 if (mode == 0)
4866 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4867 else {
4868 data = iwl_read_restricted_mem(priv, ptr);
4869 ptr += sizeof(u32);
4870 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4875 static void iwl_dump_nic_event_log(struct iwl_priv *priv)
4877 int rc;
4878 u32 base; /* SRAM byte address of event log header */
4879 u32 capacity; /* event log capacity in # entries */
4880 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4881 u32 num_wraps; /* # times uCode wrapped to top of log */
4882 u32 next_entry; /* index of next entry to be written by uCode */
4883 u32 size; /* # entries that we'll print */
4885 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4886 if (!iwl_hw_valid_rtc_data_addr(base)) {
4887 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4888 return;
4891 rc = iwl_grab_restricted_access(priv);
4892 if (rc) {
4893 IWL_WARNING("Can not read from adapter at this time.\n");
4894 return;
4897 /* event log header */
4898 capacity = iwl_read_restricted_mem(priv, base);
4899 mode = iwl_read_restricted_mem(priv, base + (1 * sizeof(u32)));
4900 num_wraps = iwl_read_restricted_mem(priv, base + (2 * sizeof(u32)));
4901 next_entry = iwl_read_restricted_mem(priv, base + (3 * sizeof(u32)));
4903 size = num_wraps ? capacity : next_entry;
4905 /* bail out if nothing in log */
4906 if (size == 0) {
4907 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
4908 iwl_release_restricted_access(priv);
4909 return;
4912 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
4913 size, num_wraps);
4915 /* if uCode has wrapped back to top of log, start at the oldest entry,
4916 * i.e the next one that uCode would fill. */
4917 if (num_wraps)
4918 iwl_print_event_log(priv, next_entry,
4919 capacity - next_entry, mode);
4921 /* (then/else) start at top of log */
4922 iwl_print_event_log(priv, 0, next_entry, mode);
4924 iwl_release_restricted_access(priv);
4928 * iwl_irq_handle_error - called for HW or SW error interrupt from card
4930 static void iwl_irq_handle_error(struct iwl_priv *priv)
4932 /* Set the FW error flag -- cleared on iwl_down */
4933 set_bit(STATUS_FW_ERROR, &priv->status);
4935 /* Cancel currently queued command. */
4936 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4938 #ifdef CONFIG_IWLWIFI_DEBUG
4939 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
4940 iwl_dump_nic_error_log(priv);
4941 iwl_dump_nic_event_log(priv);
4942 iwl_print_rx_config_cmd(&priv->staging_rxon);
4944 #endif
4946 wake_up_interruptible(&priv->wait_command_queue);
4948 /* Keep the restart process from trying to send host
4949 * commands by clearing the INIT status bit */
4950 clear_bit(STATUS_READY, &priv->status);
4952 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4953 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4954 "Restarting adapter due to uCode error.\n");
4956 if (iwl_is_associated(priv)) {
4957 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4958 sizeof(priv->recovery_rxon));
4959 priv->error_recovering = 1;
4961 queue_work(priv->workqueue, &priv->restart);
4965 static void iwl_error_recovery(struct iwl_priv *priv)
4967 unsigned long flags;
4969 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4970 sizeof(priv->staging_rxon));
4971 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4972 iwl_commit_rxon(priv);
4974 iwl_rxon_add_station(priv, priv->bssid, 1);
4976 spin_lock_irqsave(&priv->lock, flags);
4977 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4978 priv->error_recovering = 0;
4979 spin_unlock_irqrestore(&priv->lock, flags);
4982 static void iwl_irq_tasklet(struct iwl_priv *priv)
4984 u32 inta, handled = 0;
4985 u32 inta_fh;
4986 unsigned long flags;
4987 #ifdef CONFIG_IWLWIFI_DEBUG
4988 u32 inta_mask;
4989 #endif
4991 spin_lock_irqsave(&priv->lock, flags);
4993 /* Ack/clear/reset pending uCode interrupts.
4994 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4995 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
4996 inta = iwl_read32(priv, CSR_INT);
4997 iwl_write32(priv, CSR_INT, inta);
4999 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
5000 * Any new interrupts that happen after this, either while we're
5001 * in this tasklet, or later, will show up in next ISR/tasklet. */
5002 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
5003 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
5005 #ifdef CONFIG_IWLWIFI_DEBUG
5006 if (iwl_debug_level & IWL_DL_ISR) {
5007 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
5008 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5009 inta, inta_mask, inta_fh);
5011 #endif
5013 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
5014 * atomic, make sure that inta covers all the interrupts that
5015 * we've discovered, even if FH interrupt came in just after
5016 * reading CSR_INT. */
5017 if (inta_fh & CSR_FH_INT_RX_MASK)
5018 inta |= CSR_INT_BIT_FH_RX;
5019 if (inta_fh & CSR_FH_INT_TX_MASK)
5020 inta |= CSR_INT_BIT_FH_TX;
5022 /* Now service all interrupt bits discovered above. */
5023 if (inta & CSR_INT_BIT_HW_ERR) {
5024 IWL_ERROR("Microcode HW error detected. Restarting.\n");
5026 /* Tell the device to stop sending interrupts */
5027 iwl_disable_interrupts(priv);
5029 iwl_irq_handle_error(priv);
5031 handled |= CSR_INT_BIT_HW_ERR;
5033 spin_unlock_irqrestore(&priv->lock, flags);
5035 return;
5038 #ifdef CONFIG_IWLWIFI_DEBUG
5039 if (iwl_debug_level & (IWL_DL_ISR)) {
5040 /* NIC fires this, but we don't use it, redundant with WAKEUP */
5041 if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
5042 IWL_DEBUG_ISR("Microcode started or stopped.\n");
5044 /* Alive notification via Rx interrupt will do the real work */
5045 if (inta & CSR_INT_BIT_ALIVE)
5046 IWL_DEBUG_ISR("Alive interrupt\n");
5048 #endif
5049 /* Safely ignore these bits for debug checks below */
5050 inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
5052 /* HW RF KILL switch toggled (4965 only) */
5053 if (inta & CSR_INT_BIT_RF_KILL) {
5054 int hw_rf_kill = 0;
5055 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
5056 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
5057 hw_rf_kill = 1;
5059 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
5060 "RF_KILL bit toggled to %s.\n",
5061 hw_rf_kill ? "disable radio":"enable radio");
5063 /* Queue restart only if RF_KILL switch was set to "kill"
5064 * when we loaded driver, and is now set to "enable".
5065 * After we're Alive, RF_KILL gets handled by
5066 * iwl_rx_card_state_notif() */
5067 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
5068 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5069 queue_work(priv->workqueue, &priv->restart);
5072 handled |= CSR_INT_BIT_RF_KILL;
5075 /* Chip got too hot and stopped itself (4965 only) */
5076 if (inta & CSR_INT_BIT_CT_KILL) {
5077 IWL_ERROR("Microcode CT kill error detected.\n");
5078 handled |= CSR_INT_BIT_CT_KILL;
5081 /* Error detected by uCode */
5082 if (inta & CSR_INT_BIT_SW_ERR) {
5083 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
5084 inta);
5085 iwl_irq_handle_error(priv);
5086 handled |= CSR_INT_BIT_SW_ERR;
5089 /* uCode wakes up after power-down sleep */
5090 if (inta & CSR_INT_BIT_WAKEUP) {
5091 IWL_DEBUG_ISR("Wakeup interrupt\n");
5092 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
5093 iwl_tx_queue_update_write_ptr(priv, &priv->txq[0]);
5094 iwl_tx_queue_update_write_ptr(priv, &priv->txq[1]);
5095 iwl_tx_queue_update_write_ptr(priv, &priv->txq[2]);
5096 iwl_tx_queue_update_write_ptr(priv, &priv->txq[3]);
5097 iwl_tx_queue_update_write_ptr(priv, &priv->txq[4]);
5098 iwl_tx_queue_update_write_ptr(priv, &priv->txq[5]);
5100 handled |= CSR_INT_BIT_WAKEUP;
5103 /* All uCode command responses, including Tx command responses,
5104 * Rx "responses" (frame-received notification), and other
5105 * notifications from uCode come through here*/
5106 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
5107 iwl_rx_handle(priv);
5108 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
5111 if (inta & CSR_INT_BIT_FH_TX) {
5112 IWL_DEBUG_ISR("Tx interrupt\n");
5113 handled |= CSR_INT_BIT_FH_TX;
5116 if (inta & ~handled)
5117 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
5119 if (inta & ~CSR_INI_SET_MASK) {
5120 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
5121 inta & ~CSR_INI_SET_MASK);
5122 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
5125 /* Re-enable all interrupts */
5126 iwl_enable_interrupts(priv);
5128 #ifdef CONFIG_IWLWIFI_DEBUG
5129 if (iwl_debug_level & (IWL_DL_ISR)) {
5130 inta = iwl_read32(priv, CSR_INT);
5131 inta_mask = iwl_read32(priv, CSR_INT_MASK);
5132 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
5133 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
5134 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
5136 #endif
5137 spin_unlock_irqrestore(&priv->lock, flags);
5140 static irqreturn_t iwl_isr(int irq, void *data)
5142 struct iwl_priv *priv = data;
5143 u32 inta, inta_mask;
5144 u32 inta_fh;
5145 if (!priv)
5146 return IRQ_NONE;
5148 spin_lock(&priv->lock);
5150 /* Disable (but don't clear!) interrupts here to avoid
5151 * back-to-back ISRs and sporadic interrupts from our NIC.
5152 * If we have something to service, the tasklet will re-enable ints.
5153 * If we *don't* have something, we'll re-enable before leaving here. */
5154 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
5155 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
5157 /* Discover which interrupts are active/pending */
5158 inta = iwl_read32(priv, CSR_INT);
5159 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
5161 /* Ignore interrupt if there's nothing in NIC to service.
5162 * This may be due to IRQ shared with another device,
5163 * or due to sporadic interrupts thrown from our NIC. */
5164 if (!inta && !inta_fh) {
5165 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
5166 goto none;
5169 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
5170 /* Hardware disappeared. It might have already raised
5171 * an interrupt */
5172 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
5173 goto unplugged;
5176 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5177 inta, inta_mask, inta_fh);
5179 /* iwl_irq_tasklet() will service interrupts and re-enable them */
5180 tasklet_schedule(&priv->irq_tasklet);
5182 unplugged:
5183 spin_unlock(&priv->lock);
5184 return IRQ_HANDLED;
5186 none:
5187 /* re-enable interrupts here since we don't have anything to service. */
5188 iwl_enable_interrupts(priv);
5189 spin_unlock(&priv->lock);
5190 return IRQ_NONE;
5193 /************************** EEPROM BANDS ****************************
5195 * The iwl_eeprom_band definitions below provide the mapping from the
5196 * EEPROM contents to the specific channel number supported for each
5197 * band.
5199 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
5200 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5201 * The specific geography and calibration information for that channel
5202 * is contained in the eeprom map itself.
5204 * During init, we copy the eeprom information and channel map
5205 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5207 * channel_map_24/52 provides the index in the channel_info array for a
5208 * given channel. We have to have two separate maps as there is channel
5209 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5210 * band_2
5212 * A value of 0xff stored in the channel_map indicates that the channel
5213 * is not supported by the hardware at all.
5215 * A value of 0xfe in the channel_map indicates that the channel is not
5216 * valid for Tx with the current hardware. This means that
5217 * while the system can tune and receive on a given channel, it may not
5218 * be able to associate or transmit any frames on that
5219 * channel. There is no corresponding channel information for that
5220 * entry.
5222 *********************************************************************/
5224 /* 2.4 GHz */
5225 static const u8 iwl_eeprom_band_1[14] = {
5226 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5229 /* 5.2 GHz bands */
5230 static const u8 iwl_eeprom_band_2[] = {
5231 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5234 static const u8 iwl_eeprom_band_3[] = { /* 5205-5320MHz */
5235 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5238 static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
5239 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5242 static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
5243 145, 149, 153, 157, 161, 165
5246 static u8 iwl_eeprom_band_6[] = { /* 2.4 FAT channel */
5247 1, 2, 3, 4, 5, 6, 7
5250 static u8 iwl_eeprom_band_7[] = { /* 5.2 FAT channel */
5251 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5254 static void iwl_init_band_reference(const struct iwl_priv *priv, int band,
5255 int *eeprom_ch_count,
5256 const struct iwl_eeprom_channel
5257 **eeprom_ch_info,
5258 const u8 **eeprom_ch_index)
5260 switch (band) {
5261 case 1: /* 2.4GHz band */
5262 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
5263 *eeprom_ch_info = priv->eeprom.band_1_channels;
5264 *eeprom_ch_index = iwl_eeprom_band_1;
5265 break;
5266 case 2: /* 5.2GHz band */
5267 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
5268 *eeprom_ch_info = priv->eeprom.band_2_channels;
5269 *eeprom_ch_index = iwl_eeprom_band_2;
5270 break;
5271 case 3: /* 5.2GHz band */
5272 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
5273 *eeprom_ch_info = priv->eeprom.band_3_channels;
5274 *eeprom_ch_index = iwl_eeprom_band_3;
5275 break;
5276 case 4: /* 5.2GHz band */
5277 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
5278 *eeprom_ch_info = priv->eeprom.band_4_channels;
5279 *eeprom_ch_index = iwl_eeprom_band_4;
5280 break;
5281 case 5: /* 5.2GHz band */
5282 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
5283 *eeprom_ch_info = priv->eeprom.band_5_channels;
5284 *eeprom_ch_index = iwl_eeprom_band_5;
5285 break;
5286 case 6:
5287 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_6);
5288 *eeprom_ch_info = priv->eeprom.band_24_channels;
5289 *eeprom_ch_index = iwl_eeprom_band_6;
5290 break;
5291 case 7:
5292 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_7);
5293 *eeprom_ch_info = priv->eeprom.band_52_channels;
5294 *eeprom_ch_index = iwl_eeprom_band_7;
5295 break;
5296 default:
5297 BUG();
5298 return;
5302 const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
5303 int phymode, u16 channel)
5305 int i;
5307 switch (phymode) {
5308 case MODE_IEEE80211A:
5309 for (i = 14; i < priv->channel_count; i++) {
5310 if (priv->channel_info[i].channel == channel)
5311 return &priv->channel_info[i];
5313 break;
5315 case MODE_IEEE80211B:
5316 case MODE_IEEE80211G:
5317 if (channel >= 1 && channel <= 14)
5318 return &priv->channel_info[channel - 1];
5319 break;
5323 return NULL;
5326 #define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5327 ? # x " " : "")
5329 static int iwl_init_channel_map(struct iwl_priv *priv)
5331 int eeprom_ch_count = 0;
5332 const u8 *eeprom_ch_index = NULL;
5333 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
5334 int band, ch;
5335 struct iwl_channel_info *ch_info;
5337 if (priv->channel_count) {
5338 IWL_DEBUG_INFO("Channel map already initialized.\n");
5339 return 0;
5342 if (priv->eeprom.version < 0x2f) {
5343 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5344 priv->eeprom.version);
5345 return -EINVAL;
5348 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5350 priv->channel_count =
5351 ARRAY_SIZE(iwl_eeprom_band_1) +
5352 ARRAY_SIZE(iwl_eeprom_band_2) +
5353 ARRAY_SIZE(iwl_eeprom_band_3) +
5354 ARRAY_SIZE(iwl_eeprom_band_4) +
5355 ARRAY_SIZE(iwl_eeprom_band_5);
5357 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5359 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
5360 priv->channel_count, GFP_KERNEL);
5361 if (!priv->channel_info) {
5362 IWL_ERROR("Could not allocate channel_info\n");
5363 priv->channel_count = 0;
5364 return -ENOMEM;
5367 ch_info = priv->channel_info;
5369 /* Loop through the 5 EEPROM bands adding them in order to the
5370 * channel map we maintain (that contains additional information than
5371 * what just in the EEPROM) */
5372 for (band = 1; band <= 5; band++) {
5374 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5375 &eeprom_ch_info, &eeprom_ch_index);
5377 /* Loop through each band adding each of the channels */
5378 for (ch = 0; ch < eeprom_ch_count; ch++) {
5379 ch_info->channel = eeprom_ch_index[ch];
5380 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5381 MODE_IEEE80211A;
5383 /* permanently store EEPROM's channel regulatory flags
5384 * and max power in channel info database. */
5385 ch_info->eeprom = eeprom_ch_info[ch];
5387 /* Copy the run-time flags so they are there even on
5388 * invalid channels */
5389 ch_info->flags = eeprom_ch_info[ch].flags;
5391 if (!(is_channel_valid(ch_info))) {
5392 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5393 "No traffic\n",
5394 ch_info->channel,
5395 ch_info->flags,
5396 is_channel_a_band(ch_info) ?
5397 "5.2" : "2.4");
5398 ch_info++;
5399 continue;
5402 /* Initialize regulatory-based run-time data */
5403 ch_info->max_power_avg = ch_info->curr_txpow =
5404 eeprom_ch_info[ch].max_power_avg;
5405 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5406 ch_info->min_power = 0;
5408 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5409 " %ddBm): Ad-Hoc %ssupported\n",
5410 ch_info->channel,
5411 is_channel_a_band(ch_info) ?
5412 "5.2" : "2.4",
5413 CHECK_AND_PRINT(IBSS),
5414 CHECK_AND_PRINT(ACTIVE),
5415 CHECK_AND_PRINT(RADAR),
5416 CHECK_AND_PRINT(WIDE),
5417 CHECK_AND_PRINT(NARROW),
5418 CHECK_AND_PRINT(DFS),
5419 eeprom_ch_info[ch].flags,
5420 eeprom_ch_info[ch].max_power_avg,
5421 ((eeprom_ch_info[ch].
5422 flags & EEPROM_CHANNEL_IBSS)
5423 && !(eeprom_ch_info[ch].
5424 flags & EEPROM_CHANNEL_RADAR))
5425 ? "" : "not ");
5427 /* Set the user_txpower_limit to the highest power
5428 * supported by any channel */
5429 if (eeprom_ch_info[ch].max_power_avg >
5430 priv->user_txpower_limit)
5431 priv->user_txpower_limit =
5432 eeprom_ch_info[ch].max_power_avg;
5434 ch_info++;
5438 for (band = 6; band <= 7; band++) {
5439 int phymode;
5440 u8 fat_extension_chan;
5442 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5443 &eeprom_ch_info, &eeprom_ch_index);
5445 phymode = (band == 6) ? MODE_IEEE80211B : MODE_IEEE80211A;
5446 /* Loop through each band adding each of the channels */
5447 for (ch = 0; ch < eeprom_ch_count; ch++) {
5449 if ((band == 6) &&
5450 ((eeprom_ch_index[ch] == 5) ||
5451 (eeprom_ch_index[ch] == 6) ||
5452 (eeprom_ch_index[ch] == 7)))
5453 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5454 else
5455 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5457 iwl4965_set_fat_chan_info(priv, phymode,
5458 eeprom_ch_index[ch],
5459 &(eeprom_ch_info[ch]),
5460 fat_extension_chan);
5462 iwl4965_set_fat_chan_info(priv, phymode,
5463 (eeprom_ch_index[ch] + 4),
5464 &(eeprom_ch_info[ch]),
5465 HT_IE_EXT_CHANNEL_BELOW);
5469 return 0;
5472 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5473 * sending probe req. This should be set long enough to hear probe responses
5474 * from more than one AP. */
5475 #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5476 #define IWL_ACTIVE_DWELL_TIME_52 (10)
5478 /* For faster active scanning, scan will move to the next channel if fewer than
5479 * PLCP_QUIET_THRESH packets are heard on this channel within
5480 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5481 * time if it's a quiet channel (nothing responded to our probe, and there's
5482 * no other traffic).
5483 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5484 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5485 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5487 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5488 * Must be set longer than active dwell time.
5489 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5490 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5491 #define IWL_PASSIVE_DWELL_TIME_52 (10)
5492 #define IWL_PASSIVE_DWELL_BASE (100)
5493 #define IWL_CHANNEL_TUNE_TIME 5
5495 static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv, int phymode)
5497 if (phymode == MODE_IEEE80211A)
5498 return IWL_ACTIVE_DWELL_TIME_52;
5499 else
5500 return IWL_ACTIVE_DWELL_TIME_24;
5503 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, int phymode)
5505 u16 active = iwl_get_active_dwell_time(priv, phymode);
5506 u16 passive = (phymode != MODE_IEEE80211A) ?
5507 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5508 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5510 if (iwl_is_associated(priv)) {
5511 /* If we're associated, we clamp the maximum passive
5512 * dwell time to be 98% of the beacon interval (minus
5513 * 2 * channel tune time) */
5514 passive = priv->beacon_int;
5515 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5516 passive = IWL_PASSIVE_DWELL_BASE;
5517 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5520 if (passive <= active)
5521 passive = active + 1;
5523 return passive;
5526 static int iwl_get_channels_for_scan(struct iwl_priv *priv, int phymode,
5527 u8 is_active, u8 direct_mask,
5528 struct iwl_scan_channel *scan_ch)
5530 const struct ieee80211_channel *channels = NULL;
5531 const struct ieee80211_hw_mode *hw_mode;
5532 const struct iwl_channel_info *ch_info;
5533 u16 passive_dwell = 0;
5534 u16 active_dwell = 0;
5535 int added, i;
5537 hw_mode = iwl_get_hw_mode(priv, phymode);
5538 if (!hw_mode)
5539 return 0;
5541 channels = hw_mode->channels;
5543 active_dwell = iwl_get_active_dwell_time(priv, phymode);
5544 passive_dwell = iwl_get_passive_dwell_time(priv, phymode);
5546 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5547 if (channels[i].chan ==
5548 le16_to_cpu(priv->active_rxon.channel)) {
5549 if (iwl_is_associated(priv)) {
5550 IWL_DEBUG_SCAN
5551 ("Skipping current channel %d\n",
5552 le16_to_cpu(priv->active_rxon.channel));
5553 continue;
5555 } else if (priv->only_active_channel)
5556 continue;
5558 scan_ch->channel = channels[i].chan;
5560 ch_info = iwl_get_channel_info(priv, phymode, scan_ch->channel);
5561 if (!is_channel_valid(ch_info)) {
5562 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5563 scan_ch->channel);
5564 continue;
5567 if (!is_active || is_channel_passive(ch_info) ||
5568 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5569 scan_ch->type = 0; /* passive */
5570 else
5571 scan_ch->type = 1; /* active */
5573 if (scan_ch->type & 1)
5574 scan_ch->type |= (direct_mask << 1);
5576 if (is_channel_narrow(ch_info))
5577 scan_ch->type |= (1 << 7);
5579 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5580 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5582 /* Set power levels to defaults */
5583 scan_ch->tpc.dsp_atten = 110;
5584 /* scan_pwr_info->tpc.dsp_atten; */
5586 /*scan_pwr_info->tpc.tx_gain; */
5587 if (phymode == MODE_IEEE80211A)
5588 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5589 else {
5590 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5591 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
5592 * power level
5593 scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
5597 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5598 scan_ch->channel,
5599 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5600 (scan_ch->type & 1) ?
5601 active_dwell : passive_dwell);
5603 scan_ch++;
5604 added++;
5607 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5608 return added;
5611 static void iwl_reset_channel_flag(struct iwl_priv *priv)
5613 int i, j;
5614 for (i = 0; i < 3; i++) {
5615 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5616 for (j = 0; j < hw_mode->num_channels; j++)
5617 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5621 static void iwl_init_hw_rates(struct iwl_priv *priv,
5622 struct ieee80211_rate *rates)
5624 int i;
5626 for (i = 0; i < IWL_RATE_COUNT; i++) {
5627 rates[i].rate = iwl_rates[i].ieee * 5;
5628 rates[i].val = i; /* Rate scaling will work on indexes */
5629 rates[i].val2 = i;
5630 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5631 /* Only OFDM have the bits-per-symbol set */
5632 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5633 rates[i].flags |= IEEE80211_RATE_OFDM;
5634 else {
5636 * If CCK 1M then set rate flag to CCK else CCK_2
5637 * which is CCK | PREAMBLE2
5639 rates[i].flags |= (iwl_rates[i].plcp == 10) ?
5640 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5643 /* Set up which ones are basic rates... */
5644 if (IWL_BASIC_RATES_MASK & (1 << i))
5645 rates[i].flags |= IEEE80211_RATE_BASIC;
5648 iwl4965_init_hw_rates(priv, rates);
5652 * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
5654 static int iwl_init_geos(struct iwl_priv *priv)
5656 struct iwl_channel_info *ch;
5657 struct ieee80211_hw_mode *modes;
5658 struct ieee80211_channel *channels;
5659 struct ieee80211_channel *geo_ch;
5660 struct ieee80211_rate *rates;
5661 int i = 0;
5662 enum {
5663 A = 0,
5664 B = 1,
5665 G = 2,
5666 A_11N = 3,
5667 G_11N = 4,
5669 int mode_count = 5;
5671 if (priv->modes) {
5672 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5673 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5674 return 0;
5677 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5678 GFP_KERNEL);
5679 if (!modes)
5680 return -ENOMEM;
5682 channels = kzalloc(sizeof(struct ieee80211_channel) *
5683 priv->channel_count, GFP_KERNEL);
5684 if (!channels) {
5685 kfree(modes);
5686 return -ENOMEM;
5689 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5690 GFP_KERNEL);
5691 if (!rates) {
5692 kfree(modes);
5693 kfree(channels);
5694 return -ENOMEM;
5697 /* 0 = 802.11a
5698 * 1 = 802.11b
5699 * 2 = 802.11g
5702 /* 5.2GHz channels start after the 2.4GHz channels */
5703 modes[A].mode = MODE_IEEE80211A;
5704 modes[A].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
5705 modes[A].rates = rates;
5706 modes[A].num_rates = 8; /* just OFDM */
5707 modes[A].rates = &rates[4];
5708 modes[A].num_channels = 0;
5710 modes[B].mode = MODE_IEEE80211B;
5711 modes[B].channels = channels;
5712 modes[B].rates = rates;
5713 modes[B].num_rates = 4; /* just CCK */
5714 modes[B].num_channels = 0;
5716 modes[G].mode = MODE_IEEE80211G;
5717 modes[G].channels = channels;
5718 modes[G].rates = rates;
5719 modes[G].num_rates = 12; /* OFDM & CCK */
5720 modes[G].num_channels = 0;
5722 modes[G_11N].mode = MODE_IEEE80211G;
5723 modes[G_11N].channels = channels;
5724 modes[G_11N].num_rates = 13; /* OFDM & CCK */
5725 modes[G_11N].rates = rates;
5726 modes[G_11N].num_channels = 0;
5728 modes[A_11N].mode = MODE_IEEE80211A;
5729 modes[A_11N].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
5730 modes[A_11N].rates = &rates[4];
5731 modes[A_11N].num_rates = 9; /* just OFDM */
5732 modes[A_11N].num_channels = 0;
5734 priv->ieee_channels = channels;
5735 priv->ieee_rates = rates;
5737 iwl_init_hw_rates(priv, rates);
5739 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5740 ch = &priv->channel_info[i];
5742 if (!is_channel_valid(ch)) {
5743 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5744 "skipping.\n",
5745 ch->channel, is_channel_a_band(ch) ?
5746 "5.2" : "2.4");
5747 continue;
5750 if (is_channel_a_band(ch)) {
5751 geo_ch = &modes[A].channels[modes[A].num_channels++];
5752 modes[A_11N].num_channels++;
5753 } else {
5754 geo_ch = &modes[B].channels[modes[B].num_channels++];
5755 modes[G].num_channels++;
5756 modes[G_11N].num_channels++;
5759 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5760 geo_ch->chan = ch->channel;
5761 geo_ch->power_level = ch->max_power_avg;
5762 geo_ch->antenna_max = 0xff;
5764 if (is_channel_valid(ch)) {
5765 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5766 if (ch->flags & EEPROM_CHANNEL_IBSS)
5767 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5769 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5770 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5772 if (ch->flags & EEPROM_CHANNEL_RADAR)
5773 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5775 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5776 priv->max_channel_txpower_limit =
5777 ch->max_power_avg;
5780 geo_ch->val = geo_ch->flag;
5783 if ((modes[A].num_channels == 0) && priv->is_abg) {
5784 printk(KERN_INFO DRV_NAME
5785 ": Incorrectly detected BG card as ABG. Please send "
5786 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5787 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5788 priv->is_abg = 0;
5791 printk(KERN_INFO DRV_NAME
5792 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5793 modes[G].num_channels, modes[A].num_channels);
5796 * NOTE: We register these in preference of order -- the
5797 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5798 * a phymode based on rates or AP capabilities but seems to
5799 * configure it purely on if the channel being configured
5800 * is supported by a mode -- and the first match is taken
5803 if (modes[G].num_channels)
5804 ieee80211_register_hwmode(priv->hw, &modes[G]);
5805 if (modes[B].num_channels)
5806 ieee80211_register_hwmode(priv->hw, &modes[B]);
5807 if (modes[A].num_channels)
5808 ieee80211_register_hwmode(priv->hw, &modes[A]);
5810 priv->modes = modes;
5811 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5813 return 0;
5816 /******************************************************************************
5818 * uCode download functions
5820 ******************************************************************************/
5822 static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
5824 if (priv->ucode_code.v_addr != NULL) {
5825 pci_free_consistent(priv->pci_dev,
5826 priv->ucode_code.len,
5827 priv->ucode_code.v_addr,
5828 priv->ucode_code.p_addr);
5829 priv->ucode_code.v_addr = NULL;
5831 if (priv->ucode_data.v_addr != NULL) {
5832 pci_free_consistent(priv->pci_dev,
5833 priv->ucode_data.len,
5834 priv->ucode_data.v_addr,
5835 priv->ucode_data.p_addr);
5836 priv->ucode_data.v_addr = NULL;
5838 if (priv->ucode_data_backup.v_addr != NULL) {
5839 pci_free_consistent(priv->pci_dev,
5840 priv->ucode_data_backup.len,
5841 priv->ucode_data_backup.v_addr,
5842 priv->ucode_data_backup.p_addr);
5843 priv->ucode_data_backup.v_addr = NULL;
5845 if (priv->ucode_init.v_addr != NULL) {
5846 pci_free_consistent(priv->pci_dev,
5847 priv->ucode_init.len,
5848 priv->ucode_init.v_addr,
5849 priv->ucode_init.p_addr);
5850 priv->ucode_init.v_addr = NULL;
5852 if (priv->ucode_init_data.v_addr != NULL) {
5853 pci_free_consistent(priv->pci_dev,
5854 priv->ucode_init_data.len,
5855 priv->ucode_init_data.v_addr,
5856 priv->ucode_init_data.p_addr);
5857 priv->ucode_init_data.v_addr = NULL;
5859 if (priv->ucode_boot.v_addr != NULL) {
5860 pci_free_consistent(priv->pci_dev,
5861 priv->ucode_boot.len,
5862 priv->ucode_boot.v_addr,
5863 priv->ucode_boot.p_addr);
5864 priv->ucode_boot.v_addr = NULL;
5869 * iwl_verify_inst_full - verify runtime uCode image in card vs. host,
5870 * looking at all data.
5872 static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 * image, u32 len)
5874 u32 val;
5875 u32 save_len = len;
5876 int rc = 0;
5877 u32 errcnt;
5879 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5881 rc = iwl_grab_restricted_access(priv);
5882 if (rc)
5883 return rc;
5885 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
5887 errcnt = 0;
5888 for (; len > 0; len -= sizeof(u32), image++) {
5889 /* read data comes through single port, auto-incr addr */
5890 /* NOTE: Use the debugless read so we don't flood kernel log
5891 * if IWL_DL_IO is set */
5892 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5893 if (val != le32_to_cpu(*image)) {
5894 IWL_ERROR("uCode INST section is invalid at "
5895 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5896 save_len - len, val, le32_to_cpu(*image));
5897 rc = -EIO;
5898 errcnt++;
5899 if (errcnt >= 20)
5900 break;
5904 iwl_release_restricted_access(priv);
5906 if (!errcnt)
5907 IWL_DEBUG_INFO
5908 ("ucode image in INSTRUCTION memory is good\n");
5910 return rc;
5915 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
5916 * using sample data 100 bytes apart. If these sample points are good,
5917 * it's a pretty good bet that everything between them is good, too.
5919 static int iwl_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
5921 u32 val;
5922 int rc = 0;
5923 u32 errcnt = 0;
5924 u32 i;
5926 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5928 rc = iwl_grab_restricted_access(priv);
5929 if (rc)
5930 return rc;
5932 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5933 /* read data comes through single port, auto-incr addr */
5934 /* NOTE: Use the debugless read so we don't flood kernel log
5935 * if IWL_DL_IO is set */
5936 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR,
5937 i + RTC_INST_LOWER_BOUND);
5938 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5939 if (val != le32_to_cpu(*image)) {
5940 #if 0 /* Enable this if you want to see details */
5941 IWL_ERROR("uCode INST section is invalid at "
5942 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5943 i, val, *image);
5944 #endif
5945 rc = -EIO;
5946 errcnt++;
5947 if (errcnt >= 3)
5948 break;
5952 iwl_release_restricted_access(priv);
5954 return rc;
5959 * iwl_verify_ucode - determine which instruction image is in SRAM,
5960 * and verify its contents
5962 static int iwl_verify_ucode(struct iwl_priv *priv)
5964 __le32 *image;
5965 u32 len;
5966 int rc = 0;
5968 /* Try bootstrap */
5969 image = (__le32 *)priv->ucode_boot.v_addr;
5970 len = priv->ucode_boot.len;
5971 rc = iwl_verify_inst_sparse(priv, image, len);
5972 if (rc == 0) {
5973 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5974 return 0;
5977 /* Try initialize */
5978 image = (__le32 *)priv->ucode_init.v_addr;
5979 len = priv->ucode_init.len;
5980 rc = iwl_verify_inst_sparse(priv, image, len);
5981 if (rc == 0) {
5982 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5983 return 0;
5986 /* Try runtime/protocol */
5987 image = (__le32 *)priv->ucode_code.v_addr;
5988 len = priv->ucode_code.len;
5989 rc = iwl_verify_inst_sparse(priv, image, len);
5990 if (rc == 0) {
5991 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5992 return 0;
5995 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5997 /* Show first several data entries in instruction SRAM.
5998 * Selection of bootstrap image is arbitrary. */
5999 image = (__le32 *)priv->ucode_boot.v_addr;
6000 len = priv->ucode_boot.len;
6001 rc = iwl_verify_inst_full(priv, image, len);
6003 return rc;
6007 /* check contents of special bootstrap uCode SRAM */
6008 static int iwl_verify_bsm(struct iwl_priv *priv)
6010 __le32 *image = priv->ucode_boot.v_addr;
6011 u32 len = priv->ucode_boot.len;
6012 u32 reg;
6013 u32 val;
6015 IWL_DEBUG_INFO("Begin verify bsm\n");
6017 /* verify BSM SRAM contents */
6018 val = iwl_read_restricted_reg(priv, BSM_WR_DWCOUNT_REG);
6019 for (reg = BSM_SRAM_LOWER_BOUND;
6020 reg < BSM_SRAM_LOWER_BOUND + len;
6021 reg += sizeof(u32), image ++) {
6022 val = iwl_read_restricted_reg(priv, reg);
6023 if (val != le32_to_cpu(*image)) {
6024 IWL_ERROR("BSM uCode verification failed at "
6025 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
6026 BSM_SRAM_LOWER_BOUND,
6027 reg - BSM_SRAM_LOWER_BOUND, len,
6028 val, le32_to_cpu(*image));
6029 return -EIO;
6033 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
6035 return 0;
6039 * iwl_load_bsm - Load bootstrap instructions
6041 * BSM operation:
6043 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
6044 * in special SRAM that does not power down during RFKILL. When powering back
6045 * up after power-saving sleeps (or during initial uCode load), the BSM loads
6046 * the bootstrap program into the on-board processor, and starts it.
6048 * The bootstrap program loads (via DMA) instructions and data for a new
6049 * program from host DRAM locations indicated by the host driver in the
6050 * BSM_DRAM_* registers. Once the new program is loaded, it starts
6051 * automatically.
6053 * When initializing the NIC, the host driver points the BSM to the
6054 * "initialize" uCode image. This uCode sets up some internal data, then
6055 * notifies host via "initialize alive" that it is complete.
6057 * The host then replaces the BSM_DRAM_* pointer values to point to the
6058 * normal runtime uCode instructions and a backup uCode data cache buffer
6059 * (filled initially with starting data values for the on-board processor),
6060 * then triggers the "initialize" uCode to load and launch the runtime uCode,
6061 * which begins normal operation.
6063 * When doing a power-save shutdown, runtime uCode saves data SRAM into
6064 * the backup data cache in DRAM before SRAM is powered down.
6066 * When powering back up, the BSM loads the bootstrap program. This reloads
6067 * the runtime uCode instructions and the backup data cache into SRAM,
6068 * and re-launches the runtime uCode from where it left off.
6070 static int iwl_load_bsm(struct iwl_priv *priv)
6072 __le32 *image = priv->ucode_boot.v_addr;
6073 u32 len = priv->ucode_boot.len;
6074 dma_addr_t pinst;
6075 dma_addr_t pdata;
6076 u32 inst_len;
6077 u32 data_len;
6078 int rc;
6079 int i;
6080 u32 done;
6081 u32 reg_offset;
6083 IWL_DEBUG_INFO("Begin load bsm\n");
6085 /* make sure bootstrap program is no larger than BSM's SRAM size */
6086 if (len > IWL_MAX_BSM_SIZE)
6087 return -EINVAL;
6089 /* Tell bootstrap uCode where to find the "Initialize" uCode
6090 * in host DRAM ... bits 31:0 for 3945, bits 35:4 for 4965.
6091 * NOTE: iwl_initialize_alive_start() will replace these values,
6092 * after the "initialize" uCode has run, to point to
6093 * runtime/protocol instructions and backup data cache. */
6094 pinst = priv->ucode_init.p_addr >> 4;
6095 pdata = priv->ucode_init_data.p_addr >> 4;
6096 inst_len = priv->ucode_init.len;
6097 data_len = priv->ucode_init_data.len;
6099 rc = iwl_grab_restricted_access(priv);
6100 if (rc)
6101 return rc;
6103 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
6104 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6105 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
6106 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
6108 /* Fill BSM memory with bootstrap instructions */
6109 for (reg_offset = BSM_SRAM_LOWER_BOUND;
6110 reg_offset < BSM_SRAM_LOWER_BOUND + len;
6111 reg_offset += sizeof(u32), image++)
6112 _iwl_write_restricted_reg(priv, reg_offset,
6113 le32_to_cpu(*image));
6115 rc = iwl_verify_bsm(priv);
6116 if (rc) {
6117 iwl_release_restricted_access(priv);
6118 return rc;
6121 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
6122 iwl_write_restricted_reg(priv, BSM_WR_MEM_SRC_REG, 0x0);
6123 iwl_write_restricted_reg(priv, BSM_WR_MEM_DST_REG,
6124 RTC_INST_LOWER_BOUND);
6125 iwl_write_restricted_reg(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
6127 /* Load bootstrap code into instruction SRAM now,
6128 * to prepare to load "initialize" uCode */
6129 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
6130 BSM_WR_CTRL_REG_BIT_START);
6132 /* Wait for load of bootstrap uCode to finish */
6133 for (i = 0; i < 100; i++) {
6134 done = iwl_read_restricted_reg(priv, BSM_WR_CTRL_REG);
6135 if (!(done & BSM_WR_CTRL_REG_BIT_START))
6136 break;
6137 udelay(10);
6139 if (i < 100)
6140 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
6141 else {
6142 IWL_ERROR("BSM write did not complete!\n");
6143 return -EIO;
6146 /* Enable future boot loads whenever power management unit triggers it
6147 * (e.g. when powering back up after power-save shutdown) */
6148 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
6149 BSM_WR_CTRL_REG_BIT_START_EN);
6151 iwl_release_restricted_access(priv);
6153 return 0;
6156 static void iwl_nic_start(struct iwl_priv *priv)
6158 /* Remove all resets to allow NIC to operate */
6159 iwl_write32(priv, CSR_RESET, 0);
6163 * iwl_read_ucode - Read uCode images from disk file.
6165 * Copy into buffers for card to fetch via bus-mastering
6167 static int iwl_read_ucode(struct iwl_priv *priv)
6169 struct iwl_ucode *ucode;
6170 int rc = 0;
6171 const struct firmware *ucode_raw;
6172 const char *name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode";
6173 u8 *src;
6174 size_t len;
6175 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
6177 /* Ask kernel firmware_class module to get the boot firmware off disk.
6178 * request_firmware() is synchronous, file is in memory on return. */
6179 rc = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
6180 if (rc < 0) {
6181 IWL_ERROR("%s firmware file req failed: Reason %d\n", name, rc);
6182 goto error;
6185 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
6186 name, ucode_raw->size);
6188 /* Make sure that we got at least our header! */
6189 if (ucode_raw->size < sizeof(*ucode)) {
6190 IWL_ERROR("File size way too small!\n");
6191 rc = -EINVAL;
6192 goto err_release;
6195 /* Data from ucode file: header followed by uCode images */
6196 ucode = (void *)ucode_raw->data;
6198 ver = le32_to_cpu(ucode->ver);
6199 inst_size = le32_to_cpu(ucode->inst_size);
6200 data_size = le32_to_cpu(ucode->data_size);
6201 init_size = le32_to_cpu(ucode->init_size);
6202 init_data_size = le32_to_cpu(ucode->init_data_size);
6203 boot_size = le32_to_cpu(ucode->boot_size);
6205 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
6206 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
6207 inst_size);
6208 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
6209 data_size);
6210 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
6211 init_size);
6212 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
6213 init_data_size);
6214 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
6215 boot_size);
6217 /* Verify size of file vs. image size info in file's header */
6218 if (ucode_raw->size < sizeof(*ucode) +
6219 inst_size + data_size + init_size +
6220 init_data_size + boot_size) {
6222 IWL_DEBUG_INFO("uCode file size %d too small\n",
6223 (int)ucode_raw->size);
6224 rc = -EINVAL;
6225 goto err_release;
6228 /* Verify that uCode images will fit in card's SRAM */
6229 if (inst_size > IWL_MAX_INST_SIZE) {
6230 IWL_DEBUG_INFO("uCode instr len %d too large to fit in card\n",
6231 (int)inst_size);
6232 rc = -EINVAL;
6233 goto err_release;
6236 if (data_size > IWL_MAX_DATA_SIZE) {
6237 IWL_DEBUG_INFO("uCode data len %d too large to fit in card\n",
6238 (int)data_size);
6239 rc = -EINVAL;
6240 goto err_release;
6242 if (init_size > IWL_MAX_INST_SIZE) {
6243 IWL_DEBUG_INFO
6244 ("uCode init instr len %d too large to fit in card\n",
6245 (int)init_size);
6246 rc = -EINVAL;
6247 goto err_release;
6249 if (init_data_size > IWL_MAX_DATA_SIZE) {
6250 IWL_DEBUG_INFO
6251 ("uCode init data len %d too large to fit in card\n",
6252 (int)init_data_size);
6253 rc = -EINVAL;
6254 goto err_release;
6256 if (boot_size > IWL_MAX_BSM_SIZE) {
6257 IWL_DEBUG_INFO
6258 ("uCode boot instr len %d too large to fit in bsm\n",
6259 (int)boot_size);
6260 rc = -EINVAL;
6261 goto err_release;
6264 /* Allocate ucode buffers for card's bus-master loading ... */
6266 /* Runtime instructions and 2 copies of data:
6267 * 1) unmodified from disk
6268 * 2) backup cache for save/restore during power-downs */
6269 priv->ucode_code.len = inst_size;
6270 priv->ucode_code.v_addr =
6271 pci_alloc_consistent(priv->pci_dev,
6272 priv->ucode_code.len,
6273 &(priv->ucode_code.p_addr));
6275 priv->ucode_data.len = data_size;
6276 priv->ucode_data.v_addr =
6277 pci_alloc_consistent(priv->pci_dev,
6278 priv->ucode_data.len,
6279 &(priv->ucode_data.p_addr));
6281 priv->ucode_data_backup.len = data_size;
6282 priv->ucode_data_backup.v_addr =
6283 pci_alloc_consistent(priv->pci_dev,
6284 priv->ucode_data_backup.len,
6285 &(priv->ucode_data_backup.p_addr));
6288 /* Initialization instructions and data */
6289 priv->ucode_init.len = init_size;
6290 priv->ucode_init.v_addr =
6291 pci_alloc_consistent(priv->pci_dev,
6292 priv->ucode_init.len,
6293 &(priv->ucode_init.p_addr));
6295 priv->ucode_init_data.len = init_data_size;
6296 priv->ucode_init_data.v_addr =
6297 pci_alloc_consistent(priv->pci_dev,
6298 priv->ucode_init_data.len,
6299 &(priv->ucode_init_data.p_addr));
6301 /* Bootstrap (instructions only, no data) */
6302 priv->ucode_boot.len = boot_size;
6303 priv->ucode_boot.v_addr =
6304 pci_alloc_consistent(priv->pci_dev,
6305 priv->ucode_boot.len,
6306 &(priv->ucode_boot.p_addr));
6308 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
6309 !priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr ||
6310 !priv->ucode_boot.v_addr || !priv->ucode_data_backup.v_addr)
6311 goto err_pci_alloc;
6313 /* Copy images into buffers for card's bus-master reads ... */
6315 /* Runtime instructions (first block of data in file) */
6316 src = &ucode->data[0];
6317 len = priv->ucode_code.len;
6318 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %d\n",
6319 (int)len);
6320 memcpy(priv->ucode_code.v_addr, src, len);
6321 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6322 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6324 /* Runtime data (2nd block)
6325 * NOTE: Copy into backup buffer will be done in iwl_up() */
6326 src = &ucode->data[inst_size];
6327 len = priv->ucode_data.len;
6328 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %d\n",
6329 (int)len);
6330 memcpy(priv->ucode_data.v_addr, src, len);
6331 memcpy(priv->ucode_data_backup.v_addr, src, len);
6333 /* Initialization instructions (3rd block) */
6334 if (init_size) {
6335 src = &ucode->data[inst_size + data_size];
6336 len = priv->ucode_init.len;
6337 IWL_DEBUG_INFO("Copying (but not loading) init instr len %d\n",
6338 (int)len);
6339 memcpy(priv->ucode_init.v_addr, src, len);
6342 /* Initialization data (4th block) */
6343 if (init_data_size) {
6344 src = &ucode->data[inst_size + data_size + init_size];
6345 len = priv->ucode_init_data.len;
6346 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
6347 (int)len);
6348 memcpy(priv->ucode_init_data.v_addr, src, len);
6351 /* Bootstrap instructions (5th block) */
6352 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6353 len = priv->ucode_boot.len;
6354 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
6355 (int)len);
6356 memcpy(priv->ucode_boot.v_addr, src, len);
6358 /* We have our copies now, allow OS release its copies */
6359 release_firmware(ucode_raw);
6360 return 0;
6362 err_pci_alloc:
6363 IWL_ERROR("failed to allocate pci memory\n");
6364 rc = -ENOMEM;
6365 iwl_dealloc_ucode_pci(priv);
6367 err_release:
6368 release_firmware(ucode_raw);
6370 error:
6371 return rc;
6376 * iwl_set_ucode_ptrs - Set uCode address location
6378 * Tell initialization uCode where to find runtime uCode.
6380 * BSM registers initially contain pointers to initialization uCode.
6381 * We need to replace them to load runtime uCode inst and data,
6382 * and to save runtime data when powering down.
6384 static int iwl_set_ucode_ptrs(struct iwl_priv *priv)
6386 dma_addr_t pinst;
6387 dma_addr_t pdata;
6388 int rc = 0;
6389 unsigned long flags;
6391 /* bits 35:4 for 4965 */
6392 pinst = priv->ucode_code.p_addr >> 4;
6393 pdata = priv->ucode_data_backup.p_addr >> 4;
6395 spin_lock_irqsave(&priv->lock, flags);
6396 rc = iwl_grab_restricted_access(priv);
6397 if (rc) {
6398 spin_unlock_irqrestore(&priv->lock, flags);
6399 return rc;
6402 /* Tell bootstrap uCode where to find image to load */
6403 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
6404 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6405 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
6406 priv->ucode_data.len);
6408 /* Inst bytecount must be last to set up, bit 31 signals uCode
6409 * that all new ptr/size info is in place */
6410 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG,
6411 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6413 iwl_release_restricted_access(priv);
6415 spin_unlock_irqrestore(&priv->lock, flags);
6417 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6419 return rc;
6423 * iwl_init_alive_start - Called after REPLY_ALIVE notification received
6425 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6427 * The 4965 "initialize" ALIVE reply contains calibration data for:
6428 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6429 * (3945 does not contain this data).
6431 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6433 static void iwl_init_alive_start(struct iwl_priv *priv)
6435 /* Check alive response for "valid" sign from uCode */
6436 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6437 /* We had an error bringing up the hardware, so take it
6438 * all the way back down so we can try again */
6439 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6440 goto restart;
6443 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6444 * This is a paranoid check, because we would not have gotten the
6445 * "initialize" alive if code weren't properly loaded. */
6446 if (iwl_verify_ucode(priv)) {
6447 /* Runtime instruction load was bad;
6448 * take it all the way back down so we can try again */
6449 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6450 goto restart;
6453 /* Calculate temperature */
6454 priv->temperature = iwl4965_get_temperature(priv);
6456 /* Send pointers to protocol/runtime uCode image ... init code will
6457 * load and launch runtime uCode, which will send us another "Alive"
6458 * notification. */
6459 IWL_DEBUG_INFO("Initialization Alive received.\n");
6460 if (iwl_set_ucode_ptrs(priv)) {
6461 /* Runtime instruction load won't happen;
6462 * take it all the way back down so we can try again */
6463 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6464 goto restart;
6466 return;
6468 restart:
6469 queue_work(priv->workqueue, &priv->restart);
6474 * iwl_alive_start - called after REPLY_ALIVE notification received
6475 * from protocol/runtime uCode (initialization uCode's
6476 * Alive gets handled by iwl_init_alive_start()).
6478 static void iwl_alive_start(struct iwl_priv *priv)
6480 int rc = 0;
6482 IWL_DEBUG_INFO("Runtime Alive received.\n");
6484 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6485 /* We had an error bringing up the hardware, so take it
6486 * all the way back down so we can try again */
6487 IWL_DEBUG_INFO("Alive failed.\n");
6488 goto restart;
6491 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6492 * This is a paranoid check, because we would not have gotten the
6493 * "runtime" alive if code weren't properly loaded. */
6494 if (iwl_verify_ucode(priv)) {
6495 /* Runtime instruction load was bad;
6496 * take it all the way back down so we can try again */
6497 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6498 goto restart;
6501 iwl_clear_stations_table(priv);
6503 rc = iwl4965_alive_notify(priv);
6504 if (rc) {
6505 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6506 rc);
6507 goto restart;
6510 /* After the ALIVE response, we can process host commands */
6511 set_bit(STATUS_ALIVE, &priv->status);
6513 /* Clear out the uCode error bit if it is set */
6514 clear_bit(STATUS_FW_ERROR, &priv->status);
6516 rc = iwl_init_channel_map(priv);
6517 if (rc) {
6518 IWL_ERROR("initializing regulatory failed: %d\n", rc);
6519 return;
6522 iwl_init_geos(priv);
6524 if (iwl_is_rfkill(priv))
6525 return;
6527 if (!priv->mac80211_registered) {
6528 /* Unlock so any user space entry points can call back into
6529 * the driver without a deadlock... */
6530 mutex_unlock(&priv->mutex);
6531 iwl_rate_control_register(priv->hw);
6532 rc = ieee80211_register_hw(priv->hw);
6533 priv->hw->conf.beacon_int = 100;
6534 mutex_lock(&priv->mutex);
6536 if (rc) {
6537 iwl_rate_control_unregister(priv->hw);
6538 IWL_ERROR("Failed to register network "
6539 "device (error %d)\n", rc);
6540 return;
6543 priv->mac80211_registered = 1;
6545 iwl_reset_channel_flag(priv);
6546 } else
6547 ieee80211_start_queues(priv->hw);
6549 priv->active_rate = priv->rates_mask;
6550 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6552 iwl_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
6554 if (iwl_is_associated(priv)) {
6555 struct iwl_rxon_cmd *active_rxon =
6556 (struct iwl_rxon_cmd *)(&priv->active_rxon);
6558 memcpy(&priv->staging_rxon, &priv->active_rxon,
6559 sizeof(priv->staging_rxon));
6560 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6561 } else {
6562 /* Initialize our rx_config data */
6563 iwl_connection_init_rx_config(priv);
6564 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6567 /* Configure BT coexistence */
6568 iwl_send_bt_config(priv);
6570 /* Configure the adapter for unassociated operation */
6571 iwl_commit_rxon(priv);
6573 /* At this point, the NIC is initialized and operational */
6574 priv->notif_missed_beacons = 0;
6575 set_bit(STATUS_READY, &priv->status);
6577 iwl4965_rf_kill_ct_config(priv);
6578 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6580 if (priv->error_recovering)
6581 iwl_error_recovery(priv);
6583 return;
6585 restart:
6586 queue_work(priv->workqueue, &priv->restart);
6589 static void iwl_cancel_deferred_work(struct iwl_priv *priv);
6591 static void __iwl_down(struct iwl_priv *priv)
6593 unsigned long flags;
6594 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6595 struct ieee80211_conf *conf = NULL;
6597 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6599 conf = ieee80211_get_hw_conf(priv->hw);
6601 if (!exit_pending)
6602 set_bit(STATUS_EXIT_PENDING, &priv->status);
6604 iwl_clear_stations_table(priv);
6606 /* Unblock any waiting calls */
6607 wake_up_interruptible_all(&priv->wait_command_queue);
6609 /* Wipe out the EXIT_PENDING status bit if we are not actually
6610 * exiting the module */
6611 if (!exit_pending)
6612 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6614 /* stop and reset the on-board processor */
6615 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
6617 /* tell the device to stop sending interrupts */
6618 iwl_disable_interrupts(priv);
6620 if (priv->mac80211_registered)
6621 ieee80211_stop_queues(priv->hw);
6623 /* If we have not previously called iwl_init() then
6624 * clear all bits but the RF Kill and SUSPEND bits and return */
6625 if (!iwl_is_init(priv)) {
6626 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6627 STATUS_RF_KILL_HW |
6628 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6629 STATUS_RF_KILL_SW |
6630 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6631 STATUS_IN_SUSPEND;
6632 goto exit;
6635 /* ...otherwise clear out all the status bits but the RF Kill and
6636 * SUSPEND bits and continue taking the NIC down. */
6637 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6638 STATUS_RF_KILL_HW |
6639 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6640 STATUS_RF_KILL_SW |
6641 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6642 STATUS_IN_SUSPEND |
6643 test_bit(STATUS_FW_ERROR, &priv->status) <<
6644 STATUS_FW_ERROR;
6646 spin_lock_irqsave(&priv->lock, flags);
6647 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
6648 spin_unlock_irqrestore(&priv->lock, flags);
6650 iwl_hw_txq_ctx_stop(priv);
6651 iwl_hw_rxq_stop(priv);
6653 spin_lock_irqsave(&priv->lock, flags);
6654 if (!iwl_grab_restricted_access(priv)) {
6655 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
6656 APMG_CLK_VAL_DMA_CLK_RQT);
6657 iwl_release_restricted_access(priv);
6659 spin_unlock_irqrestore(&priv->lock, flags);
6661 udelay(5);
6663 iwl_hw_nic_stop_master(priv);
6664 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6665 iwl_hw_nic_reset(priv);
6667 exit:
6668 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
6670 if (priv->ibss_beacon)
6671 dev_kfree_skb(priv->ibss_beacon);
6672 priv->ibss_beacon = NULL;
6674 /* clear out any free frames */
6675 iwl_clear_free_frames(priv);
6678 static void iwl_down(struct iwl_priv *priv)
6680 mutex_lock(&priv->mutex);
6681 __iwl_down(priv);
6682 mutex_unlock(&priv->mutex);
6684 iwl_cancel_deferred_work(priv);
6687 #define MAX_HW_RESTARTS 5
6689 static int __iwl_up(struct iwl_priv *priv)
6691 DECLARE_MAC_BUF(mac);
6692 int rc, i;
6693 u32 hw_rf_kill = 0;
6695 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6696 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6697 return -EIO;
6700 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6701 IWL_WARNING("Radio disabled by SW RF kill (module "
6702 "parameter)\n");
6703 return 0;
6706 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6707 IWL_ERROR("ucode not available for device bringup\n");
6708 return -EIO;
6711 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6713 rc = iwl_hw_nic_init(priv);
6714 if (rc) {
6715 IWL_ERROR("Unable to int nic\n");
6716 return rc;
6719 /* make sure rfkill handshake bits are cleared */
6720 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6721 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
6722 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6724 /* clear (again), then enable host interrupts */
6725 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6726 iwl_enable_interrupts(priv);
6728 /* really make sure rfkill handshake bits are cleared */
6729 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6730 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6732 /* Copy original ucode data image from disk into backup cache.
6733 * This will be used to initialize the on-board processor's
6734 * data SRAM for a clean start when the runtime program first loads. */
6735 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6736 priv->ucode_data.len);
6738 /* If platform's RF_KILL switch is set to KILL,
6739 * wait for BIT_INT_RF_KILL interrupt before loading uCode
6740 * and getting things started */
6741 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
6742 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
6743 hw_rf_kill = 1;
6745 if (test_bit(STATUS_RF_KILL_HW, &priv->status) || hw_rf_kill) {
6746 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6747 return 0;
6750 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6752 iwl_clear_stations_table(priv);
6754 /* load bootstrap state machine,
6755 * load bootstrap program into processor's memory,
6756 * prepare to load the "initialize" uCode */
6757 rc = iwl_load_bsm(priv);
6759 if (rc) {
6760 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6761 continue;
6764 /* start card; "initialize" will load runtime ucode */
6765 iwl_nic_start(priv);
6767 /* MAC Address location in EEPROM same for 3945/4965 */
6768 get_eeprom_mac(priv, priv->mac_addr);
6769 IWL_DEBUG_INFO("MAC address: %s\n",
6770 print_mac(mac, priv->mac_addr));
6772 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6774 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6776 return 0;
6779 set_bit(STATUS_EXIT_PENDING, &priv->status);
6780 __iwl_down(priv);
6782 /* tried to restart and config the device for as long as our
6783 * patience could withstand */
6784 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6785 return -EIO;
6789 /*****************************************************************************
6791 * Workqueue callbacks
6793 *****************************************************************************/
6795 static void iwl_bg_init_alive_start(struct work_struct *data)
6797 struct iwl_priv *priv =
6798 container_of(data, struct iwl_priv, init_alive_start.work);
6800 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6801 return;
6803 mutex_lock(&priv->mutex);
6804 iwl_init_alive_start(priv);
6805 mutex_unlock(&priv->mutex);
6808 static void iwl_bg_alive_start(struct work_struct *data)
6810 struct iwl_priv *priv =
6811 container_of(data, struct iwl_priv, alive_start.work);
6813 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6814 return;
6816 mutex_lock(&priv->mutex);
6817 iwl_alive_start(priv);
6818 mutex_unlock(&priv->mutex);
6821 static void iwl_bg_rf_kill(struct work_struct *work)
6823 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
6825 wake_up_interruptible(&priv->wait_command_queue);
6827 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6828 return;
6830 mutex_lock(&priv->mutex);
6832 if (!iwl_is_rfkill(priv)) {
6833 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6834 "HW and/or SW RF Kill no longer active, restarting "
6835 "device\n");
6836 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6837 queue_work(priv->workqueue, &priv->restart);
6838 } else {
6840 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6841 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6842 "disabled by SW switch\n");
6843 else
6844 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6845 "Kill switch must be turned off for "
6846 "wireless networking to work.\n");
6848 mutex_unlock(&priv->mutex);
6851 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6853 static void iwl_bg_scan_check(struct work_struct *data)
6855 struct iwl_priv *priv =
6856 container_of(data, struct iwl_priv, scan_check.work);
6858 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6859 return;
6861 mutex_lock(&priv->mutex);
6862 if (test_bit(STATUS_SCANNING, &priv->status) ||
6863 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6864 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6865 "Scan completion watchdog resetting adapter (%dms)\n",
6866 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
6868 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6869 iwl_send_scan_abort(priv);
6871 mutex_unlock(&priv->mutex);
6874 static void iwl_bg_request_scan(struct work_struct *data)
6876 struct iwl_priv *priv =
6877 container_of(data, struct iwl_priv, request_scan);
6878 struct iwl_host_cmd cmd = {
6879 .id = REPLY_SCAN_CMD,
6880 .len = sizeof(struct iwl_scan_cmd),
6881 .meta.flags = CMD_SIZE_HUGE,
6883 int rc = 0;
6884 struct iwl_scan_cmd *scan;
6885 struct ieee80211_conf *conf = NULL;
6886 u8 direct_mask;
6887 int phymode;
6889 conf = ieee80211_get_hw_conf(priv->hw);
6891 mutex_lock(&priv->mutex);
6893 if (!iwl_is_ready(priv)) {
6894 IWL_WARNING("request scan called when driver not ready.\n");
6895 goto done;
6898 /* Make sure the scan wasn't cancelled before this queued work
6899 * was given the chance to run... */
6900 if (!test_bit(STATUS_SCANNING, &priv->status))
6901 goto done;
6903 /* This should never be called or scheduled if there is currently
6904 * a scan active in the hardware. */
6905 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6906 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6907 "Ignoring second request.\n");
6908 rc = -EIO;
6909 goto done;
6912 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6913 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6914 goto done;
6917 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6918 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6919 goto done;
6922 if (iwl_is_rfkill(priv)) {
6923 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6924 goto done;
6927 if (!test_bit(STATUS_READY, &priv->status)) {
6928 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6929 goto done;
6932 if (!priv->scan_bands) {
6933 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6934 goto done;
6937 if (!priv->scan) {
6938 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
6939 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6940 if (!priv->scan) {
6941 rc = -ENOMEM;
6942 goto done;
6945 scan = priv->scan;
6946 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
6948 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6949 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6951 if (iwl_is_associated(priv)) {
6952 u16 interval = 0;
6953 u32 extra;
6954 u32 suspend_time = 100;
6955 u32 scan_suspend_time = 100;
6956 unsigned long flags;
6958 IWL_DEBUG_INFO("Scanning while associated...\n");
6960 spin_lock_irqsave(&priv->lock, flags);
6961 interval = priv->beacon_int;
6962 spin_unlock_irqrestore(&priv->lock, flags);
6964 scan->suspend_time = 0;
6965 scan->max_out_time = cpu_to_le32(200 * 1024);
6966 if (!interval)
6967 interval = suspend_time;
6969 extra = (suspend_time / interval) << 22;
6970 scan_suspend_time = (extra |
6971 ((suspend_time % interval) * 1024));
6972 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6973 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6974 scan_suspend_time, interval);
6977 /* We should add the ability for user to lock to PASSIVE ONLY */
6978 if (priv->one_direct_scan) {
6979 IWL_DEBUG_SCAN
6980 ("Kicking off one direct scan for '%s'\n",
6981 iwl_escape_essid(priv->direct_ssid,
6982 priv->direct_ssid_len));
6983 scan->direct_scan[0].id = WLAN_EID_SSID;
6984 scan->direct_scan[0].len = priv->direct_ssid_len;
6985 memcpy(scan->direct_scan[0].ssid,
6986 priv->direct_ssid, priv->direct_ssid_len);
6987 direct_mask = 1;
6988 } else if (!iwl_is_associated(priv) && priv->essid_len) {
6989 scan->direct_scan[0].id = WLAN_EID_SSID;
6990 scan->direct_scan[0].len = priv->essid_len;
6991 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6992 direct_mask = 1;
6993 } else
6994 direct_mask = 0;
6996 /* We don't build a direct scan probe request; the uCode will do
6997 * that based on the direct_mask added to each channel entry */
6998 scan->tx_cmd.len = cpu_to_le16(
6999 iwl_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
7000 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
7001 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
7002 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
7003 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
7005 /* flags + rate selection */
7007 scan->tx_cmd.tx_flags |= cpu_to_le32(0x200);
7009 switch (priv->scan_bands) {
7010 case 2:
7011 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
7012 scan->tx_cmd.rate_n_flags =
7013 iwl_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
7014 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
7016 scan->good_CRC_th = 0;
7017 phymode = MODE_IEEE80211G;
7018 break;
7020 case 1:
7021 scan->tx_cmd.rate_n_flags =
7022 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
7023 RATE_MCS_ANT_B_MSK);
7024 scan->good_CRC_th = IWL_GOOD_CRC_TH;
7025 phymode = MODE_IEEE80211A;
7026 break;
7028 default:
7029 IWL_WARNING("Invalid scan band count\n");
7030 goto done;
7033 /* select Rx chains */
7035 /* Force use of chains B and C (0x6) for scan Rx.
7036 * Avoid A (0x1) because of its off-channel reception on A-band.
7037 * MIMO is not used here, but value is required to make uCode happy. */
7038 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
7039 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
7040 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
7041 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
7043 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
7044 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
7046 if (direct_mask)
7047 IWL_DEBUG_SCAN
7048 ("Initiating direct scan for %s.\n",
7049 iwl_escape_essid(priv->essid, priv->essid_len));
7050 else
7051 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
7053 scan->channel_count =
7054 iwl_get_channels_for_scan(
7055 priv, phymode, 1, /* active */
7056 direct_mask,
7057 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
7059 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
7060 scan->channel_count * sizeof(struct iwl_scan_channel);
7061 cmd.data = scan;
7062 scan->len = cpu_to_le16(cmd.len);
7064 set_bit(STATUS_SCAN_HW, &priv->status);
7065 rc = iwl_send_cmd_sync(priv, &cmd);
7066 if (rc)
7067 goto done;
7069 queue_delayed_work(priv->workqueue, &priv->scan_check,
7070 IWL_SCAN_CHECK_WATCHDOG);
7072 mutex_unlock(&priv->mutex);
7073 return;
7075 done:
7076 /* inform mac80211 scan aborted */
7077 queue_work(priv->workqueue, &priv->scan_completed);
7078 mutex_unlock(&priv->mutex);
7081 static void iwl_bg_up(struct work_struct *data)
7083 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
7085 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7086 return;
7088 mutex_lock(&priv->mutex);
7089 __iwl_up(priv);
7090 mutex_unlock(&priv->mutex);
7093 static void iwl_bg_restart(struct work_struct *data)
7095 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
7097 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7098 return;
7100 iwl_down(priv);
7101 queue_work(priv->workqueue, &priv->up);
7104 static void iwl_bg_rx_replenish(struct work_struct *data)
7106 struct iwl_priv *priv =
7107 container_of(data, struct iwl_priv, rx_replenish);
7109 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7110 return;
7112 mutex_lock(&priv->mutex);
7113 iwl_rx_replenish(priv);
7114 mutex_unlock(&priv->mutex);
7117 static void iwl_bg_post_associate(struct work_struct *data)
7119 struct iwl_priv *priv = container_of(data, struct iwl_priv,
7120 post_associate.work);
7122 int rc = 0;
7123 struct ieee80211_conf *conf = NULL;
7124 DECLARE_MAC_BUF(mac);
7126 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7127 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
7128 return;
7131 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
7132 priv->assoc_id,
7133 print_mac(mac, priv->active_rxon.bssid_addr));
7136 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7137 return;
7139 mutex_lock(&priv->mutex);
7141 if (!priv->interface_id || !priv->is_open) {
7142 mutex_unlock(&priv->mutex);
7143 return;
7145 iwl_scan_cancel_timeout(priv, 200);
7147 conf = ieee80211_get_hw_conf(priv->hw);
7149 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7150 iwl_commit_rxon(priv);
7152 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
7153 iwl_setup_rxon_timing(priv);
7154 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7155 sizeof(priv->rxon_timing), &priv->rxon_timing);
7156 if (rc)
7157 IWL_WARNING("REPLY_RXON_TIMING failed - "
7158 "Attempting to continue.\n");
7160 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7162 #ifdef CONFIG_IWLWIFI_HT
7163 if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
7164 iwl4965_set_rxon_ht(priv, &priv->current_assoc_ht);
7165 else {
7166 priv->active_rate_ht[0] = 0;
7167 priv->active_rate_ht[1] = 0;
7168 priv->current_channel_width = IWL_CHANNEL_WIDTH_20MHZ;
7170 #endif /* CONFIG_IWLWIFI_HT*/
7171 iwl4965_set_rxon_chain(priv);
7172 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7174 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
7175 priv->assoc_id, priv->beacon_int);
7177 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7178 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7179 else
7180 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7182 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7183 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
7184 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
7185 else
7186 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7188 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7189 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7193 iwl_commit_rxon(priv);
7195 switch (priv->iw_mode) {
7196 case IEEE80211_IF_TYPE_STA:
7197 iwl_rate_scale_init(priv->hw, IWL_AP_ID);
7198 break;
7200 case IEEE80211_IF_TYPE_IBSS:
7202 /* clear out the station table */
7203 iwl_clear_stations_table(priv);
7205 iwl_rxon_add_station(priv, BROADCAST_ADDR, 0);
7206 iwl_rxon_add_station(priv, priv->bssid, 0);
7207 iwl_rate_scale_init(priv->hw, IWL_STA_ID);
7208 iwl_send_beacon_cmd(priv);
7210 break;
7212 default:
7213 IWL_ERROR("%s Should not be called in %d mode\n",
7214 __FUNCTION__, priv->iw_mode);
7215 break;
7218 iwl_sequence_reset(priv);
7220 #ifdef CONFIG_IWLWIFI_SENSITIVITY
7221 /* Enable Rx differential gain and sensitivity calibrations */
7222 iwl4965_chain_noise_reset(priv);
7223 priv->start_calib = 1;
7224 #endif /* CONFIG_IWLWIFI_SENSITIVITY */
7226 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7227 priv->assoc_station_added = 1;
7229 #ifdef CONFIG_IWLWIFI_QOS
7230 iwl_activate_qos(priv, 0);
7231 #endif /* CONFIG_IWLWIFI_QOS */
7232 mutex_unlock(&priv->mutex);
7235 static void iwl_bg_abort_scan(struct work_struct *work)
7237 struct iwl_priv *priv = container_of(work, struct iwl_priv,
7238 abort_scan);
7240 if (!iwl_is_ready(priv))
7241 return;
7243 mutex_lock(&priv->mutex);
7245 set_bit(STATUS_SCAN_ABORTING, &priv->status);
7246 iwl_send_scan_abort(priv);
7248 mutex_unlock(&priv->mutex);
7251 static void iwl_bg_scan_completed(struct work_struct *work)
7253 struct iwl_priv *priv =
7254 container_of(work, struct iwl_priv, scan_completed);
7256 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
7258 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7259 return;
7261 ieee80211_scan_completed(priv->hw);
7263 /* Since setting the TXPOWER may have been deferred while
7264 * performing the scan, fire one off */
7265 mutex_lock(&priv->mutex);
7266 iwl_hw_reg_send_txpower(priv);
7267 mutex_unlock(&priv->mutex);
7270 /*****************************************************************************
7272 * mac80211 entry point functions
7274 *****************************************************************************/
7276 static int iwl_mac_start(struct ieee80211_hw *hw)
7278 struct iwl_priv *priv = hw->priv;
7280 IWL_DEBUG_MAC80211("enter\n");
7282 /* we should be verifying the device is ready to be opened */
7283 mutex_lock(&priv->mutex);
7285 priv->is_open = 1;
7287 if (!iwl_is_rfkill(priv))
7288 ieee80211_start_queues(priv->hw);
7290 mutex_unlock(&priv->mutex);
7291 IWL_DEBUG_MAC80211("leave\n");
7292 return 0;
7295 static void iwl_mac_stop(struct ieee80211_hw *hw)
7297 struct iwl_priv *priv = hw->priv;
7299 IWL_DEBUG_MAC80211("enter\n");
7302 mutex_lock(&priv->mutex);
7303 /* stop mac, cancel any scan request and clear
7304 * RXON_FILTER_ASSOC_MSK BIT
7306 priv->is_open = 0;
7307 iwl_scan_cancel_timeout(priv, 100);
7308 cancel_delayed_work(&priv->post_associate);
7309 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7310 iwl_commit_rxon(priv);
7311 mutex_unlock(&priv->mutex);
7313 IWL_DEBUG_MAC80211("leave\n");
7316 static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
7317 struct ieee80211_tx_control *ctl)
7319 struct iwl_priv *priv = hw->priv;
7321 IWL_DEBUG_MAC80211("enter\n");
7323 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7324 IWL_DEBUG_MAC80211("leave - monitor\n");
7325 return -1;
7328 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
7329 ctl->tx_rate);
7331 if (iwl_tx_skb(priv, skb, ctl))
7332 dev_kfree_skb_any(skb);
7334 IWL_DEBUG_MAC80211("leave\n");
7335 return 0;
7338 static int iwl_mac_add_interface(struct ieee80211_hw *hw,
7339 struct ieee80211_if_init_conf *conf)
7341 struct iwl_priv *priv = hw->priv;
7342 unsigned long flags;
7343 DECLARE_MAC_BUF(mac);
7345 IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
7347 if (priv->interface_id) {
7348 IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
7349 return 0;
7352 spin_lock_irqsave(&priv->lock, flags);
7353 priv->interface_id = conf->if_id;
7355 spin_unlock_irqrestore(&priv->lock, flags);
7357 mutex_lock(&priv->mutex);
7359 if (conf->mac_addr) {
7360 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7361 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7363 iwl_set_mode(priv, conf->type);
7365 IWL_DEBUG_MAC80211("leave\n");
7366 mutex_unlock(&priv->mutex);
7368 return 0;
7372 * iwl_mac_config - mac80211 config callback
7374 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7375 * be set inappropriately and the driver currently sets the hardware up to
7376 * use it whenever needed.
7378 static int iwl_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
7380 struct iwl_priv *priv = hw->priv;
7381 const struct iwl_channel_info *ch_info;
7382 unsigned long flags;
7384 mutex_lock(&priv->mutex);
7385 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
7387 if (!iwl_is_ready(priv)) {
7388 IWL_DEBUG_MAC80211("leave - not ready\n");
7389 mutex_unlock(&priv->mutex);
7390 return -EIO;
7393 /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
7394 * what is exposed through include/ declarations */
7395 if (unlikely(!iwl_param_disable_hw_scan &&
7396 test_bit(STATUS_SCANNING, &priv->status))) {
7397 IWL_DEBUG_MAC80211("leave - scanning\n");
7398 mutex_unlock(&priv->mutex);
7399 return 0;
7402 spin_lock_irqsave(&priv->lock, flags);
7404 ch_info = iwl_get_channel_info(priv, conf->phymode, conf->channel);
7405 if (!is_channel_valid(ch_info)) {
7406 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
7407 conf->channel, conf->phymode);
7408 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7409 spin_unlock_irqrestore(&priv->lock, flags);
7410 mutex_unlock(&priv->mutex);
7411 return -EINVAL;
7414 #ifdef CONFIG_IWLWIFI_HT
7415 /* if we are switching fron ht to 2.4 clear flags
7416 * from any ht related info since 2.4 does not
7417 * support ht */
7418 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel)
7419 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
7420 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7421 #endif
7423 priv->staging_rxon.flags = 0;
7424 #endif /* CONFIG_IWLWIFI_HT */
7426 iwl_set_rxon_channel(priv, conf->phymode, conf->channel);
7428 iwl_set_flags_for_phymode(priv, conf->phymode);
7430 /* The list of supported rates and rate mask can be different
7431 * for each phymode; since the phymode may have changed, reset
7432 * the rate mask to what mac80211 lists */
7433 iwl_set_rate(priv);
7435 spin_unlock_irqrestore(&priv->lock, flags);
7437 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
7438 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
7439 iwl_hw_channel_switch(priv, conf->channel);
7440 mutex_unlock(&priv->mutex);
7441 return 0;
7443 #endif
7445 iwl_radio_kill_sw(priv, !conf->radio_enabled);
7447 if (!conf->radio_enabled) {
7448 IWL_DEBUG_MAC80211("leave - radio disabled\n");
7449 mutex_unlock(&priv->mutex);
7450 return 0;
7453 if (iwl_is_rfkill(priv)) {
7454 IWL_DEBUG_MAC80211("leave - RF kill\n");
7455 mutex_unlock(&priv->mutex);
7456 return -EIO;
7459 iwl_set_rate(priv);
7461 if (memcmp(&priv->active_rxon,
7462 &priv->staging_rxon, sizeof(priv->staging_rxon)))
7463 iwl_commit_rxon(priv);
7464 else
7465 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7467 IWL_DEBUG_MAC80211("leave\n");
7469 mutex_unlock(&priv->mutex);
7471 return 0;
7474 static void iwl_config_ap(struct iwl_priv *priv)
7476 int rc = 0;
7478 if (priv->status & STATUS_EXIT_PENDING)
7479 return;
7481 /* The following should be done only at AP bring up */
7482 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7484 /* RXON - unassoc (to set timing command) */
7485 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7486 iwl_commit_rxon(priv);
7488 /* RXON Timing */
7489 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
7490 iwl_setup_rxon_timing(priv);
7491 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7492 sizeof(priv->rxon_timing), &priv->rxon_timing);
7493 if (rc)
7494 IWL_WARNING("REPLY_RXON_TIMING failed - "
7495 "Attempting to continue.\n");
7497 iwl4965_set_rxon_chain(priv);
7499 /* FIXME: what should be the assoc_id for AP? */
7500 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7501 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7502 priv->staging_rxon.flags |=
7503 RXON_FLG_SHORT_PREAMBLE_MSK;
7504 else
7505 priv->staging_rxon.flags &=
7506 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7508 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7509 if (priv->assoc_capability &
7510 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7511 priv->staging_rxon.flags |=
7512 RXON_FLG_SHORT_SLOT_MSK;
7513 else
7514 priv->staging_rxon.flags &=
7515 ~RXON_FLG_SHORT_SLOT_MSK;
7517 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7518 priv->staging_rxon.flags &=
7519 ~RXON_FLG_SHORT_SLOT_MSK;
7521 /* restore RXON assoc */
7522 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7523 iwl_commit_rxon(priv);
7524 #ifdef CONFIG_IWLWIFI_QOS
7525 iwl_activate_qos(priv, 1);
7526 #endif
7527 iwl_rxon_add_station(priv, BROADCAST_ADDR, 0);
7529 iwl_send_beacon_cmd(priv);
7531 /* FIXME - we need to add code here to detect a totally new
7532 * configuration, reset the AP, unassoc, rxon timing, assoc,
7533 * clear sta table, add BCAST sta... */
7536 static int iwl_mac_config_interface(struct ieee80211_hw *hw, int if_id,
7537 struct ieee80211_if_conf *conf)
7539 struct iwl_priv *priv = hw->priv;
7540 DECLARE_MAC_BUF(mac);
7541 unsigned long flags;
7542 int rc;
7544 if (conf == NULL)
7545 return -EIO;
7547 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7548 (!conf->beacon || !conf->ssid_len)) {
7549 IWL_DEBUG_MAC80211
7550 ("Leaving in AP mode because HostAPD is not ready.\n");
7551 return 0;
7554 mutex_lock(&priv->mutex);
7556 IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
7557 if (conf->bssid)
7558 IWL_DEBUG_MAC80211("bssid: %s\n",
7559 print_mac(mac, conf->bssid));
7562 * very dubious code was here; the probe filtering flag is never set:
7564 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7565 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
7567 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
7568 IWL_DEBUG_MAC80211("leave - scanning\n");
7569 mutex_unlock(&priv->mutex);
7570 return 0;
7573 if (priv->interface_id != if_id) {
7574 IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
7575 mutex_unlock(&priv->mutex);
7576 return 0;
7579 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7580 if (!conf->bssid) {
7581 conf->bssid = priv->mac_addr;
7582 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
7583 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7584 print_mac(mac, conf->bssid));
7586 if (priv->ibss_beacon)
7587 dev_kfree_skb(priv->ibss_beacon);
7589 priv->ibss_beacon = conf->beacon;
7592 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7593 !is_multicast_ether_addr(conf->bssid)) {
7594 /* If there is currently a HW scan going on in the background
7595 * then we need to cancel it else the RXON below will fail. */
7596 if (iwl_scan_cancel_timeout(priv, 100)) {
7597 IWL_WARNING("Aborted scan still in progress "
7598 "after 100ms\n");
7599 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7600 mutex_unlock(&priv->mutex);
7601 return -EAGAIN;
7603 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7605 /* TODO: Audit driver for usage of these members and see
7606 * if mac80211 deprecates them (priv->bssid looks like it
7607 * shouldn't be there, but I haven't scanned the IBSS code
7608 * to verify) - jpk */
7609 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7611 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7612 iwl_config_ap(priv);
7613 else {
7614 rc = iwl_commit_rxon(priv);
7615 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
7616 iwl_rxon_add_station(
7617 priv, priv->active_rxon.bssid_addr, 1);
7620 } else {
7621 iwl_scan_cancel_timeout(priv, 100);
7622 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7623 iwl_commit_rxon(priv);
7626 spin_lock_irqsave(&priv->lock, flags);
7627 if (!conf->ssid_len)
7628 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7629 else
7630 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7632 priv->essid_len = conf->ssid_len;
7633 spin_unlock_irqrestore(&priv->lock, flags);
7635 IWL_DEBUG_MAC80211("leave\n");
7636 mutex_unlock(&priv->mutex);
7638 return 0;
7641 static void iwl_configure_filter(struct ieee80211_hw *hw,
7642 unsigned int changed_flags,
7643 unsigned int *total_flags,
7644 int mc_count, struct dev_addr_list *mc_list)
7647 * XXX: dummy
7648 * see also iwl_connection_init_rx_config
7650 *total_flags = 0;
7653 static void iwl_mac_remove_interface(struct ieee80211_hw *hw,
7654 struct ieee80211_if_init_conf *conf)
7656 struct iwl_priv *priv = hw->priv;
7658 IWL_DEBUG_MAC80211("enter\n");
7660 mutex_lock(&priv->mutex);
7662 iwl_scan_cancel_timeout(priv, 100);
7663 cancel_delayed_work(&priv->post_associate);
7664 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7665 iwl_commit_rxon(priv);
7667 if (priv->interface_id == conf->if_id) {
7668 priv->interface_id = 0;
7669 memset(priv->bssid, 0, ETH_ALEN);
7670 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7671 priv->essid_len = 0;
7673 mutex_unlock(&priv->mutex);
7675 IWL_DEBUG_MAC80211("leave\n");
7678 static void iwl_mac_erp_ie_changed(struct ieee80211_hw *hw,
7679 u8 changes, int cts_protection, int preamble)
7682 struct iwl_priv *priv = hw->priv;
7684 if (changes & IEEE80211_ERP_CHANGE_PREAMBLE) {
7685 if (preamble == WLAN_ERP_PREAMBLE_SHORT)
7686 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7687 else
7688 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7691 if (changes & IEEE80211_ERP_CHANGE_PROTECTION) {
7692 if (cts_protection)
7693 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7694 else
7695 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7698 if (iwl_is_associated(priv))
7699 iwl_send_rxon_assoc(priv);
7702 #define IWL_DELAY_NEXT_SCAN (HZ*2)
7703 static int iwl_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
7705 int rc = 0;
7706 unsigned long flags;
7707 struct iwl_priv *priv = hw->priv;
7709 IWL_DEBUG_MAC80211("enter\n");
7711 mutex_lock(&priv->mutex);
7712 spin_lock_irqsave(&priv->lock, flags);
7714 if (!iwl_is_ready_rf(priv)) {
7715 rc = -EIO;
7716 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7717 goto out_unlock;
7720 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7721 rc = -EIO;
7722 IWL_ERROR("ERROR: APs don't scan\n");
7723 goto out_unlock;
7726 /* if we just finished scan ask for delay */
7727 if (priv->last_scan_jiffies &&
7728 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
7729 jiffies)) {
7730 rc = -EAGAIN;
7731 goto out_unlock;
7733 if (len) {
7734 IWL_DEBUG_SCAN("direct scan for "
7735 "%s [%d]\n ",
7736 iwl_escape_essid(ssid, len), (int)len);
7738 priv->one_direct_scan = 1;
7739 priv->direct_ssid_len = (u8)
7740 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7741 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
7742 } else
7743 priv->one_direct_scan = 0;
7745 rc = iwl_scan_initiate(priv);
7747 IWL_DEBUG_MAC80211("leave\n");
7749 out_unlock:
7750 spin_unlock_irqrestore(&priv->lock, flags);
7751 mutex_unlock(&priv->mutex);
7753 return rc;
7756 static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
7757 const u8 *local_addr, const u8 *addr,
7758 struct ieee80211_key_conf *key)
7760 struct iwl_priv *priv = hw->priv;
7761 DECLARE_MAC_BUF(mac);
7762 int rc = 0;
7763 u8 sta_id;
7765 IWL_DEBUG_MAC80211("enter\n");
7767 if (!iwl_param_hwcrypto) {
7768 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7769 return -EOPNOTSUPP;
7772 if (is_zero_ether_addr(addr))
7773 /* only support pairwise keys */
7774 return -EOPNOTSUPP;
7776 sta_id = iwl_hw_find_station(priv, addr);
7777 if (sta_id == IWL_INVALID_STATION) {
7778 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7779 print_mac(mac, addr));
7780 return -EINVAL;
7783 mutex_lock(&priv->mutex);
7785 iwl_scan_cancel_timeout(priv, 100);
7787 switch (cmd) {
7788 case SET_KEY:
7789 rc = iwl_update_sta_key_info(priv, key, sta_id);
7790 if (!rc) {
7791 iwl_set_rxon_hwcrypto(priv, 1);
7792 iwl_commit_rxon(priv);
7793 key->hw_key_idx = sta_id;
7794 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7795 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7797 break;
7798 case DISABLE_KEY:
7799 rc = iwl_clear_sta_key_info(priv, sta_id);
7800 if (!rc) {
7801 iwl_set_rxon_hwcrypto(priv, 0);
7802 iwl_commit_rxon(priv);
7803 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7805 break;
7806 default:
7807 rc = -EINVAL;
7810 IWL_DEBUG_MAC80211("leave\n");
7811 mutex_unlock(&priv->mutex);
7813 return rc;
7816 static int iwl_mac_conf_tx(struct ieee80211_hw *hw, int queue,
7817 const struct ieee80211_tx_queue_params *params)
7819 struct iwl_priv *priv = hw->priv;
7820 #ifdef CONFIG_IWLWIFI_QOS
7821 unsigned long flags;
7822 int q;
7823 #endif /* CONFIG_IWL_QOS */
7825 IWL_DEBUG_MAC80211("enter\n");
7827 if (!iwl_is_ready_rf(priv)) {
7828 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7829 return -EIO;
7832 if (queue >= AC_NUM) {
7833 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7834 return 0;
7837 #ifdef CONFIG_IWLWIFI_QOS
7838 if (!priv->qos_data.qos_enable) {
7839 priv->qos_data.qos_active = 0;
7840 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7841 return 0;
7843 q = AC_NUM - 1 - queue;
7845 spin_lock_irqsave(&priv->lock, flags);
7847 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7848 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7849 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7850 priv->qos_data.def_qos_parm.ac[q].edca_txop =
7851 cpu_to_le16((params->burst_time * 100));
7853 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7854 priv->qos_data.qos_active = 1;
7856 spin_unlock_irqrestore(&priv->lock, flags);
7858 mutex_lock(&priv->mutex);
7859 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7860 iwl_activate_qos(priv, 1);
7861 else if (priv->assoc_id && iwl_is_associated(priv))
7862 iwl_activate_qos(priv, 0);
7864 mutex_unlock(&priv->mutex);
7866 #endif /*CONFIG_IWLWIFI_QOS */
7868 IWL_DEBUG_MAC80211("leave\n");
7869 return 0;
7872 static int iwl_mac_get_tx_stats(struct ieee80211_hw *hw,
7873 struct ieee80211_tx_queue_stats *stats)
7875 struct iwl_priv *priv = hw->priv;
7876 int i, avail;
7877 struct iwl_tx_queue *txq;
7878 struct iwl_queue *q;
7879 unsigned long flags;
7881 IWL_DEBUG_MAC80211("enter\n");
7883 if (!iwl_is_ready_rf(priv)) {
7884 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7885 return -EIO;
7888 spin_lock_irqsave(&priv->lock, flags);
7890 for (i = 0; i < AC_NUM; i++) {
7891 txq = &priv->txq[i];
7892 q = &txq->q;
7893 avail = iwl_queue_space(q);
7895 stats->data[i].len = q->n_window - avail;
7896 stats->data[i].limit = q->n_window - q->high_mark;
7897 stats->data[i].count = q->n_window;
7900 spin_unlock_irqrestore(&priv->lock, flags);
7902 IWL_DEBUG_MAC80211("leave\n");
7904 return 0;
7907 static int iwl_mac_get_stats(struct ieee80211_hw *hw,
7908 struct ieee80211_low_level_stats *stats)
7910 IWL_DEBUG_MAC80211("enter\n");
7911 IWL_DEBUG_MAC80211("leave\n");
7913 return 0;
7916 static u64 iwl_mac_get_tsf(struct ieee80211_hw *hw)
7918 IWL_DEBUG_MAC80211("enter\n");
7919 IWL_DEBUG_MAC80211("leave\n");
7921 return 0;
7924 static void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
7926 struct iwl_priv *priv = hw->priv;
7927 unsigned long flags;
7929 mutex_lock(&priv->mutex);
7930 IWL_DEBUG_MAC80211("enter\n");
7932 priv->lq_mngr.lq_ready = 0;
7933 #ifdef CONFIG_IWLWIFI_HT
7934 spin_lock_irqsave(&priv->lock, flags);
7935 memset(&priv->current_assoc_ht, 0, sizeof(struct sta_ht_info));
7936 spin_unlock_irqrestore(&priv->lock, flags);
7937 #ifdef CONFIG_IWLWIFI_HT_AGG
7938 /* if (priv->lq_mngr.agg_ctrl.granted_ba)
7939 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);*/
7941 memset(&(priv->lq_mngr.agg_ctrl), 0, sizeof(struct iwl_agg_control));
7942 priv->lq_mngr.agg_ctrl.tid_traffic_load_threshold = 10;
7943 priv->lq_mngr.agg_ctrl.ba_timeout = 5000;
7944 priv->lq_mngr.agg_ctrl.auto_agg = 1;
7946 if (priv->lq_mngr.agg_ctrl.auto_agg)
7947 priv->lq_mngr.agg_ctrl.requested_ba = TID_ALL_ENABLED;
7948 #endif /*CONFIG_IWLWIFI_HT_AGG */
7949 #endif /* CONFIG_IWLWIFI_HT */
7951 #ifdef CONFIG_IWLWIFI_QOS
7952 iwl_reset_qos(priv);
7953 #endif
7955 cancel_delayed_work(&priv->post_associate);
7957 spin_lock_irqsave(&priv->lock, flags);
7958 priv->assoc_id = 0;
7959 priv->assoc_capability = 0;
7960 priv->call_post_assoc_from_beacon = 0;
7961 priv->assoc_station_added = 0;
7963 /* new association get rid of ibss beacon skb */
7964 if (priv->ibss_beacon)
7965 dev_kfree_skb(priv->ibss_beacon);
7967 priv->ibss_beacon = NULL;
7969 priv->beacon_int = priv->hw->conf.beacon_int;
7970 priv->timestamp1 = 0;
7971 priv->timestamp0 = 0;
7972 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7973 priv->beacon_int = 0;
7975 spin_unlock_irqrestore(&priv->lock, flags);
7977 /* we are restarting association process
7978 * clear RXON_FILTER_ASSOC_MSK bit
7980 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
7981 iwl_scan_cancel_timeout(priv, 100);
7982 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7983 iwl_commit_rxon(priv);
7986 /* Per mac80211.h: This is only used in IBSS mode... */
7987 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7989 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7990 mutex_unlock(&priv->mutex);
7991 return;
7994 if (!iwl_is_ready_rf(priv)) {
7995 IWL_DEBUG_MAC80211("leave - not ready\n");
7996 mutex_unlock(&priv->mutex);
7997 return;
8000 priv->only_active_channel = 0;
8002 iwl_set_rate(priv);
8004 mutex_unlock(&priv->mutex);
8006 IWL_DEBUG_MAC80211("leave\n");
8010 static int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
8011 struct ieee80211_tx_control *control)
8013 struct iwl_priv *priv = hw->priv;
8014 unsigned long flags;
8016 mutex_lock(&priv->mutex);
8017 IWL_DEBUG_MAC80211("enter\n");
8019 if (!iwl_is_ready_rf(priv)) {
8020 IWL_DEBUG_MAC80211("leave - RF not ready\n");
8021 mutex_unlock(&priv->mutex);
8022 return -EIO;
8025 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
8026 IWL_DEBUG_MAC80211("leave - not IBSS\n");
8027 mutex_unlock(&priv->mutex);
8028 return -EIO;
8031 spin_lock_irqsave(&priv->lock, flags);
8033 if (priv->ibss_beacon)
8034 dev_kfree_skb(priv->ibss_beacon);
8036 priv->ibss_beacon = skb;
8038 priv->assoc_id = 0;
8040 IWL_DEBUG_MAC80211("leave\n");
8041 spin_unlock_irqrestore(&priv->lock, flags);
8043 #ifdef CONFIG_IWLWIFI_QOS
8044 iwl_reset_qos(priv);
8045 #endif
8047 queue_work(priv->workqueue, &priv->post_associate.work);
8049 mutex_unlock(&priv->mutex);
8051 return 0;
8054 #ifdef CONFIG_IWLWIFI_HT
8055 union ht_cap_info {
8056 struct {
8057 u16 advanced_coding_cap :1;
8058 u16 supported_chan_width_set :1;
8059 u16 mimo_power_save_mode :2;
8060 u16 green_field :1;
8061 u16 short_GI20 :1;
8062 u16 short_GI40 :1;
8063 u16 tx_stbc :1;
8064 u16 rx_stbc :1;
8065 u16 beam_forming :1;
8066 u16 delayed_ba :1;
8067 u16 maximal_amsdu_size :1;
8068 u16 cck_mode_at_40MHz :1;
8069 u16 psmp_support :1;
8070 u16 stbc_ctrl_frame_support :1;
8071 u16 sig_txop_protection_support :1;
8073 u16 val;
8074 } __attribute__ ((packed));
8076 union ht_param_info{
8077 struct {
8078 u8 max_rx_ampdu_factor :2;
8079 u8 mpdu_density :3;
8080 u8 reserved :3;
8082 u8 val;
8083 } __attribute__ ((packed));
8085 union ht_exra_param_info {
8086 struct {
8087 u8 ext_chan_offset :2;
8088 u8 tx_chan_width :1;
8089 u8 rifs_mode :1;
8090 u8 controlled_access_only :1;
8091 u8 service_interval_granularity :3;
8093 u8 val;
8094 } __attribute__ ((packed));
8096 union ht_operation_mode{
8097 struct {
8098 u16 op_mode :2;
8099 u16 non_GF :1;
8100 u16 reserved :13;
8102 u16 val;
8103 } __attribute__ ((packed));
8106 static int sta_ht_info_init(struct ieee80211_ht_capability *ht_cap,
8107 struct ieee80211_ht_additional_info *ht_extra,
8108 struct sta_ht_info *ht_info_ap,
8109 struct sta_ht_info *ht_info)
8111 union ht_cap_info cap;
8112 union ht_operation_mode op_mode;
8113 union ht_param_info param_info;
8114 union ht_exra_param_info extra_param_info;
8116 IWL_DEBUG_MAC80211("enter: \n");
8118 if (!ht_info) {
8119 IWL_DEBUG_MAC80211("leave: ht_info is NULL\n");
8120 return -1;
8123 if (ht_cap) {
8124 cap.val = (u16) le16_to_cpu(ht_cap->capabilities_info);
8125 param_info.val = ht_cap->mac_ht_params_info;
8126 ht_info->is_ht = 1;
8127 if (cap.short_GI20)
8128 ht_info->sgf |= 0x1;
8129 if (cap.short_GI40)
8130 ht_info->sgf |= 0x2;
8131 ht_info->is_green_field = cap.green_field;
8132 ht_info->max_amsdu_size = cap.maximal_amsdu_size;
8133 ht_info->supported_chan_width = cap.supported_chan_width_set;
8134 ht_info->tx_mimo_ps_mode = cap.mimo_power_save_mode;
8135 memcpy(ht_info->supp_rates, ht_cap->supported_mcs_set, 16);
8137 ht_info->ampdu_factor = param_info.max_rx_ampdu_factor;
8138 ht_info->mpdu_density = param_info.mpdu_density;
8140 IWL_DEBUG_MAC80211("SISO mask 0x%X MIMO mask 0x%X \n",
8141 ht_cap->supported_mcs_set[0],
8142 ht_cap->supported_mcs_set[1]);
8144 if (ht_info_ap) {
8145 ht_info->control_channel = ht_info_ap->control_channel;
8146 ht_info->extension_chan_offset =
8147 ht_info_ap->extension_chan_offset;
8148 ht_info->tx_chan_width = ht_info_ap->tx_chan_width;
8149 ht_info->operating_mode = ht_info_ap->operating_mode;
8152 if (ht_extra) {
8153 extra_param_info.val = ht_extra->ht_param;
8154 ht_info->control_channel = ht_extra->control_chan;
8155 ht_info->extension_chan_offset =
8156 extra_param_info.ext_chan_offset;
8157 ht_info->tx_chan_width = extra_param_info.tx_chan_width;
8158 op_mode.val = (u16)
8159 le16_to_cpu(ht_extra->operation_mode);
8160 ht_info->operating_mode = op_mode.op_mode;
8161 IWL_DEBUG_MAC80211("control channel %d\n",
8162 ht_extra->control_chan);
8164 } else
8165 ht_info->is_ht = 0;
8167 IWL_DEBUG_MAC80211("leave\n");
8168 return 0;
8171 static int iwl_mac_conf_ht(struct ieee80211_hw *hw,
8172 struct ieee80211_ht_capability *ht_cap,
8173 struct ieee80211_ht_additional_info *ht_extra)
8175 struct iwl_priv *priv = hw->priv;
8176 int rs;
8178 IWL_DEBUG_MAC80211("enter: \n");
8180 rs = sta_ht_info_init(ht_cap, ht_extra, NULL, &priv->current_assoc_ht);
8181 iwl4965_set_rxon_chain(priv);
8183 if (priv && priv->assoc_id &&
8184 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
8185 unsigned long flags;
8187 spin_lock_irqsave(&priv->lock, flags);
8188 if (priv->beacon_int)
8189 queue_work(priv->workqueue, &priv->post_associate.work);
8190 else
8191 priv->call_post_assoc_from_beacon = 1;
8192 spin_unlock_irqrestore(&priv->lock, flags);
8195 IWL_DEBUG_MAC80211("leave: control channel %d\n",
8196 ht_extra->control_chan);
8197 return rs;
8201 static void iwl_set_ht_capab(struct ieee80211_hw *hw,
8202 struct ieee80211_ht_capability *ht_cap,
8203 u8 use_wide_chan)
8205 union ht_cap_info cap;
8206 union ht_param_info param_info;
8208 memset(&cap, 0, sizeof(union ht_cap_info));
8209 memset(&param_info, 0, sizeof(union ht_param_info));
8211 cap.maximal_amsdu_size = HT_IE_MAX_AMSDU_SIZE_4K;
8212 cap.green_field = 1;
8213 cap.short_GI20 = 1;
8214 cap.short_GI40 = 1;
8215 cap.supported_chan_width_set = use_wide_chan;
8216 cap.mimo_power_save_mode = 0x3;
8218 param_info.max_rx_ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
8219 param_info.mpdu_density = CFG_HT_MPDU_DENSITY_DEF;
8220 ht_cap->capabilities_info = (__le16) cpu_to_le16(cap.val);
8221 ht_cap->mac_ht_params_info = (u8) param_info.val;
8223 ht_cap->supported_mcs_set[0] = 0xff;
8224 ht_cap->supported_mcs_set[1] = 0xff;
8225 ht_cap->supported_mcs_set[4] =
8226 (cap.supported_chan_width_set) ? 0x1: 0x0;
8229 static void iwl_mac_get_ht_capab(struct ieee80211_hw *hw,
8230 struct ieee80211_ht_capability *ht_cap)
8232 u8 use_wide_channel = 1;
8233 struct iwl_priv *priv = hw->priv;
8235 IWL_DEBUG_MAC80211("enter: \n");
8236 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
8237 use_wide_channel = 0;
8239 /* no fat tx allowed on 2.4GHZ */
8240 if (priv->phymode != MODE_IEEE80211A)
8241 use_wide_channel = 0;
8243 iwl_set_ht_capab(hw, ht_cap, use_wide_channel);
8244 IWL_DEBUG_MAC80211("leave: \n");
8246 #endif /*CONFIG_IWLWIFI_HT*/
8248 /*****************************************************************************
8250 * sysfs attributes
8252 *****************************************************************************/
8254 #ifdef CONFIG_IWLWIFI_DEBUG
8257 * The following adds a new attribute to the sysfs representation
8258 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
8259 * used for controlling the debug level.
8261 * See the level definitions in iwl for details.
8264 static ssize_t show_debug_level(struct device_driver *d, char *buf)
8266 return sprintf(buf, "0x%08X\n", iwl_debug_level);
8268 static ssize_t store_debug_level(struct device_driver *d,
8269 const char *buf, size_t count)
8271 char *p = (char *)buf;
8272 u32 val;
8274 val = simple_strtoul(p, &p, 0);
8275 if (p == buf)
8276 printk(KERN_INFO DRV_NAME
8277 ": %s is not in hex or decimal form.\n", buf);
8278 else
8279 iwl_debug_level = val;
8281 return strnlen(buf, count);
8284 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
8285 show_debug_level, store_debug_level);
8287 #endif /* CONFIG_IWLWIFI_DEBUG */
8289 static ssize_t show_rf_kill(struct device *d,
8290 struct device_attribute *attr, char *buf)
8293 * 0 - RF kill not enabled
8294 * 1 - SW based RF kill active (sysfs)
8295 * 2 - HW based RF kill active
8296 * 3 - Both HW and SW based RF kill active
8298 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8299 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
8300 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
8302 return sprintf(buf, "%i\n", val);
8305 static ssize_t store_rf_kill(struct device *d,
8306 struct device_attribute *attr,
8307 const char *buf, size_t count)
8309 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8311 mutex_lock(&priv->mutex);
8312 iwl_radio_kill_sw(priv, buf[0] == '1');
8313 mutex_unlock(&priv->mutex);
8315 return count;
8318 static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
8320 static ssize_t show_temperature(struct device *d,
8321 struct device_attribute *attr, char *buf)
8323 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8325 if (!iwl_is_alive(priv))
8326 return -EAGAIN;
8328 return sprintf(buf, "%d\n", iwl_hw_get_temperature(priv));
8331 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8333 static ssize_t show_rs_window(struct device *d,
8334 struct device_attribute *attr,
8335 char *buf)
8337 struct iwl_priv *priv = d->driver_data;
8338 return iwl_fill_rs_info(priv->hw, buf, IWL_AP_ID);
8340 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8342 static ssize_t show_tx_power(struct device *d,
8343 struct device_attribute *attr, char *buf)
8345 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8346 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8349 static ssize_t store_tx_power(struct device *d,
8350 struct device_attribute *attr,
8351 const char *buf, size_t count)
8353 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8354 char *p = (char *)buf;
8355 u32 val;
8357 val = simple_strtoul(p, &p, 10);
8358 if (p == buf)
8359 printk(KERN_INFO DRV_NAME
8360 ": %s is not in decimal form.\n", buf);
8361 else
8362 iwl_hw_reg_set_txpower(priv, val);
8364 return count;
8367 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8369 static ssize_t show_flags(struct device *d,
8370 struct device_attribute *attr, char *buf)
8372 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8374 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8377 static ssize_t store_flags(struct device *d,
8378 struct device_attribute *attr,
8379 const char *buf, size_t count)
8381 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8382 u32 flags = simple_strtoul(buf, NULL, 0);
8384 mutex_lock(&priv->mutex);
8385 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8386 /* Cancel any currently running scans... */
8387 if (iwl_scan_cancel_timeout(priv, 100))
8388 IWL_WARNING("Could not cancel scan.\n");
8389 else {
8390 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8391 flags);
8392 priv->staging_rxon.flags = cpu_to_le32(flags);
8393 iwl_commit_rxon(priv);
8396 mutex_unlock(&priv->mutex);
8398 return count;
8401 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8403 static ssize_t show_filter_flags(struct device *d,
8404 struct device_attribute *attr, char *buf)
8406 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8408 return sprintf(buf, "0x%04X\n",
8409 le32_to_cpu(priv->active_rxon.filter_flags));
8412 static ssize_t store_filter_flags(struct device *d,
8413 struct device_attribute *attr,
8414 const char *buf, size_t count)
8416 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8417 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8419 mutex_lock(&priv->mutex);
8420 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8421 /* Cancel any currently running scans... */
8422 if (iwl_scan_cancel_timeout(priv, 100))
8423 IWL_WARNING("Could not cancel scan.\n");
8424 else {
8425 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8426 "0x%04X\n", filter_flags);
8427 priv->staging_rxon.filter_flags =
8428 cpu_to_le32(filter_flags);
8429 iwl_commit_rxon(priv);
8432 mutex_unlock(&priv->mutex);
8434 return count;
8437 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8438 store_filter_flags);
8440 static ssize_t show_tune(struct device *d,
8441 struct device_attribute *attr, char *buf)
8443 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8445 return sprintf(buf, "0x%04X\n",
8446 (priv->phymode << 8) |
8447 le16_to_cpu(priv->active_rxon.channel));
8450 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode);
8452 static ssize_t store_tune(struct device *d,
8453 struct device_attribute *attr,
8454 const char *buf, size_t count)
8456 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8457 char *p = (char *)buf;
8458 u16 tune = simple_strtoul(p, &p, 0);
8459 u8 phymode = (tune >> 8) & 0xff;
8460 u16 channel = tune & 0xff;
8462 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
8464 mutex_lock(&priv->mutex);
8465 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
8466 (priv->phymode != phymode)) {
8467 const struct iwl_channel_info *ch_info;
8469 ch_info = iwl_get_channel_info(priv, phymode, channel);
8470 if (!ch_info) {
8471 IWL_WARNING("Requested invalid phymode/channel "
8472 "combination: %d %d\n", phymode, channel);
8473 mutex_unlock(&priv->mutex);
8474 return -EINVAL;
8477 /* Cancel any currently running scans... */
8478 if (iwl_scan_cancel_timeout(priv, 100))
8479 IWL_WARNING("Could not cancel scan.\n");
8480 else {
8481 IWL_DEBUG_INFO("Committing phymode and "
8482 "rxon.channel = %d %d\n",
8483 phymode, channel);
8485 iwl_set_rxon_channel(priv, phymode, channel);
8486 iwl_set_flags_for_phymode(priv, phymode);
8488 iwl_set_rate(priv);
8489 iwl_commit_rxon(priv);
8492 mutex_unlock(&priv->mutex);
8494 return count;
8497 static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
8499 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8501 static ssize_t show_measurement(struct device *d,
8502 struct device_attribute *attr, char *buf)
8504 struct iwl_priv *priv = dev_get_drvdata(d);
8505 struct iwl_spectrum_notification measure_report;
8506 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8507 u8 *data = (u8 *) & measure_report;
8508 unsigned long flags;
8510 spin_lock_irqsave(&priv->lock, flags);
8511 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8512 spin_unlock_irqrestore(&priv->lock, flags);
8513 return 0;
8515 memcpy(&measure_report, &priv->measure_report, size);
8516 priv->measurement_status = 0;
8517 spin_unlock_irqrestore(&priv->lock, flags);
8519 while (size && (PAGE_SIZE - len)) {
8520 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8521 PAGE_SIZE - len, 1);
8522 len = strlen(buf);
8523 if (PAGE_SIZE - len)
8524 buf[len++] = '\n';
8526 ofs += 16;
8527 size -= min(size, 16U);
8530 return len;
8533 static ssize_t store_measurement(struct device *d,
8534 struct device_attribute *attr,
8535 const char *buf, size_t count)
8537 struct iwl_priv *priv = dev_get_drvdata(d);
8538 struct ieee80211_measurement_params params = {
8539 .channel = le16_to_cpu(priv->active_rxon.channel),
8540 .start_time = cpu_to_le64(priv->last_tsf),
8541 .duration = cpu_to_le16(1),
8543 u8 type = IWL_MEASURE_BASIC;
8544 u8 buffer[32];
8545 u8 channel;
8547 if (count) {
8548 char *p = buffer;
8549 strncpy(buffer, buf, min(sizeof(buffer), count));
8550 channel = simple_strtoul(p, NULL, 0);
8551 if (channel)
8552 params.channel = channel;
8554 p = buffer;
8555 while (*p && *p != ' ')
8556 p++;
8557 if (*p)
8558 type = simple_strtoul(p + 1, NULL, 0);
8561 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8562 "channel %d (for '%s')\n", type, params.channel, buf);
8563 iwl_get_measurement(priv, &params, type);
8565 return count;
8568 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8569 show_measurement, store_measurement);
8570 #endif /* CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT */
8572 static ssize_t store_retry_rate(struct device *d,
8573 struct device_attribute *attr,
8574 const char *buf, size_t count)
8576 struct iwl_priv *priv = dev_get_drvdata(d);
8578 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8579 if (priv->retry_rate <= 0)
8580 priv->retry_rate = 1;
8582 return count;
8585 static ssize_t show_retry_rate(struct device *d,
8586 struct device_attribute *attr, char *buf)
8588 struct iwl_priv *priv = dev_get_drvdata(d);
8589 return sprintf(buf, "%d", priv->retry_rate);
8592 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8593 store_retry_rate);
8595 static ssize_t store_power_level(struct device *d,
8596 struct device_attribute *attr,
8597 const char *buf, size_t count)
8599 struct iwl_priv *priv = dev_get_drvdata(d);
8600 int rc;
8601 int mode;
8603 mode = simple_strtoul(buf, NULL, 0);
8604 mutex_lock(&priv->mutex);
8606 if (!iwl_is_ready(priv)) {
8607 rc = -EAGAIN;
8608 goto out;
8611 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8612 mode = IWL_POWER_AC;
8613 else
8614 mode |= IWL_POWER_ENABLED;
8616 if (mode != priv->power_mode) {
8617 rc = iwl_send_power_mode(priv, IWL_POWER_LEVEL(mode));
8618 if (rc) {
8619 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8620 goto out;
8622 priv->power_mode = mode;
8625 rc = count;
8627 out:
8628 mutex_unlock(&priv->mutex);
8629 return rc;
8632 #define MAX_WX_STRING 80
8634 /* Values are in microsecond */
8635 static const s32 timeout_duration[] = {
8636 350000,
8637 250000,
8638 75000,
8639 37000,
8640 25000,
8642 static const s32 period_duration[] = {
8643 400000,
8644 700000,
8645 1000000,
8646 1000000,
8647 1000000
8650 static ssize_t show_power_level(struct device *d,
8651 struct device_attribute *attr, char *buf)
8653 struct iwl_priv *priv = dev_get_drvdata(d);
8654 int level = IWL_POWER_LEVEL(priv->power_mode);
8655 char *p = buf;
8657 p += sprintf(p, "%d ", level);
8658 switch (level) {
8659 case IWL_POWER_MODE_CAM:
8660 case IWL_POWER_AC:
8661 p += sprintf(p, "(AC)");
8662 break;
8663 case IWL_POWER_BATTERY:
8664 p += sprintf(p, "(BATTERY)");
8665 break;
8666 default:
8667 p += sprintf(p,
8668 "(Timeout %dms, Period %dms)",
8669 timeout_duration[level - 1] / 1000,
8670 period_duration[level - 1] / 1000);
8673 if (!(priv->power_mode & IWL_POWER_ENABLED))
8674 p += sprintf(p, " OFF\n");
8675 else
8676 p += sprintf(p, " \n");
8678 return (p - buf + 1);
8682 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8683 store_power_level);
8685 static ssize_t show_channels(struct device *d,
8686 struct device_attribute *attr, char *buf)
8688 struct iwl_priv *priv = dev_get_drvdata(d);
8689 int len = 0, i;
8690 struct ieee80211_channel *channels = NULL;
8691 const struct ieee80211_hw_mode *hw_mode = NULL;
8692 int count = 0;
8694 if (!iwl_is_ready(priv))
8695 return -EAGAIN;
8697 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211G);
8698 if (!hw_mode)
8699 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211B);
8700 if (hw_mode) {
8701 channels = hw_mode->channels;
8702 count = hw_mode->num_channels;
8705 len +=
8706 sprintf(&buf[len],
8707 "Displaying %d channels in 2.4GHz band "
8708 "(802.11bg):\n", count);
8710 for (i = 0; i < count; i++)
8711 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8712 channels[i].chan,
8713 channels[i].power_level,
8714 channels[i].
8715 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8716 " (IEEE 802.11h required)" : "",
8717 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8718 || (channels[i].
8719 flag &
8720 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8721 ", IBSS",
8722 channels[i].
8723 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8724 "active/passive" : "passive only");
8726 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211A);
8727 if (hw_mode) {
8728 channels = hw_mode->channels;
8729 count = hw_mode->num_channels;
8730 } else {
8731 channels = NULL;
8732 count = 0;
8735 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8736 "(802.11a):\n", count);
8738 for (i = 0; i < count; i++)
8739 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8740 channels[i].chan,
8741 channels[i].power_level,
8742 channels[i].
8743 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8744 " (IEEE 802.11h required)" : "",
8745 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8746 || (channels[i].
8747 flag &
8748 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8749 ", IBSS",
8750 channels[i].
8751 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8752 "active/passive" : "passive only");
8754 return len;
8757 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8759 static ssize_t show_statistics(struct device *d,
8760 struct device_attribute *attr, char *buf)
8762 struct iwl_priv *priv = dev_get_drvdata(d);
8763 u32 size = sizeof(struct iwl_notif_statistics);
8764 u32 len = 0, ofs = 0;
8765 u8 *data = (u8 *) & priv->statistics;
8766 int rc = 0;
8768 if (!iwl_is_alive(priv))
8769 return -EAGAIN;
8771 mutex_lock(&priv->mutex);
8772 rc = iwl_send_statistics_request(priv);
8773 mutex_unlock(&priv->mutex);
8775 if (rc) {
8776 len = sprintf(buf,
8777 "Error sending statistics request: 0x%08X\n", rc);
8778 return len;
8781 while (size && (PAGE_SIZE - len)) {
8782 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8783 PAGE_SIZE - len, 1);
8784 len = strlen(buf);
8785 if (PAGE_SIZE - len)
8786 buf[len++] = '\n';
8788 ofs += 16;
8789 size -= min(size, 16U);
8792 return len;
8795 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8797 static ssize_t show_antenna(struct device *d,
8798 struct device_attribute *attr, char *buf)
8800 struct iwl_priv *priv = dev_get_drvdata(d);
8802 if (!iwl_is_alive(priv))
8803 return -EAGAIN;
8805 return sprintf(buf, "%d\n", priv->antenna);
8808 static ssize_t store_antenna(struct device *d,
8809 struct device_attribute *attr,
8810 const char *buf, size_t count)
8812 int ant;
8813 struct iwl_priv *priv = dev_get_drvdata(d);
8815 if (count == 0)
8816 return 0;
8818 if (sscanf(buf, "%1i", &ant) != 1) {
8819 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8820 return count;
8823 if ((ant >= 0) && (ant <= 2)) {
8824 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
8825 priv->antenna = (enum iwl_antenna)ant;
8826 } else
8827 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8830 return count;
8833 static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8835 static ssize_t show_status(struct device *d,
8836 struct device_attribute *attr, char *buf)
8838 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8839 if (!iwl_is_alive(priv))
8840 return -EAGAIN;
8841 return sprintf(buf, "0x%08x\n", (int)priv->status);
8844 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8846 static ssize_t dump_error_log(struct device *d,
8847 struct device_attribute *attr,
8848 const char *buf, size_t count)
8850 char *p = (char *)buf;
8852 if (p[0] == '1')
8853 iwl_dump_nic_error_log((struct iwl_priv *)d->driver_data);
8855 return strnlen(buf, count);
8858 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8860 static ssize_t dump_event_log(struct device *d,
8861 struct device_attribute *attr,
8862 const char *buf, size_t count)
8864 char *p = (char *)buf;
8866 if (p[0] == '1')
8867 iwl_dump_nic_event_log((struct iwl_priv *)d->driver_data);
8869 return strnlen(buf, count);
8872 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8874 /*****************************************************************************
8876 * driver setup and teardown
8878 *****************************************************************************/
8880 static void iwl_setup_deferred_work(struct iwl_priv *priv)
8882 priv->workqueue = create_workqueue(DRV_NAME);
8884 init_waitqueue_head(&priv->wait_command_queue);
8886 INIT_WORK(&priv->up, iwl_bg_up);
8887 INIT_WORK(&priv->restart, iwl_bg_restart);
8888 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
8889 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
8890 INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
8891 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
8892 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
8893 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
8894 INIT_DELAYED_WORK(&priv->post_associate, iwl_bg_post_associate);
8895 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
8896 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
8897 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
8899 iwl_hw_setup_deferred_work(priv);
8901 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
8902 iwl_irq_tasklet, (unsigned long)priv);
8905 static void iwl_cancel_deferred_work(struct iwl_priv *priv)
8907 iwl_hw_cancel_deferred_work(priv);
8909 cancel_delayed_work_sync(&priv->init_alive_start);
8910 cancel_delayed_work(&priv->scan_check);
8911 cancel_delayed_work(&priv->alive_start);
8912 cancel_delayed_work(&priv->post_associate);
8913 cancel_work_sync(&priv->beacon_update);
8916 static struct attribute *iwl_sysfs_entries[] = {
8917 &dev_attr_antenna.attr,
8918 &dev_attr_channels.attr,
8919 &dev_attr_dump_errors.attr,
8920 &dev_attr_dump_events.attr,
8921 &dev_attr_flags.attr,
8922 &dev_attr_filter_flags.attr,
8923 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8924 &dev_attr_measurement.attr,
8925 #endif
8926 &dev_attr_power_level.attr,
8927 &dev_attr_retry_rate.attr,
8928 &dev_attr_rf_kill.attr,
8929 &dev_attr_rs_window.attr,
8930 &dev_attr_statistics.attr,
8931 &dev_attr_status.attr,
8932 &dev_attr_temperature.attr,
8933 &dev_attr_tune.attr,
8934 &dev_attr_tx_power.attr,
8936 NULL
8939 static struct attribute_group iwl_attribute_group = {
8940 .name = NULL, /* put in device directory */
8941 .attrs = iwl_sysfs_entries,
8944 static struct ieee80211_ops iwl_hw_ops = {
8945 .tx = iwl_mac_tx,
8946 .start = iwl_mac_start,
8947 .stop = iwl_mac_stop,
8948 .add_interface = iwl_mac_add_interface,
8949 .remove_interface = iwl_mac_remove_interface,
8950 .config = iwl_mac_config,
8951 .config_interface = iwl_mac_config_interface,
8952 .configure_filter = iwl_configure_filter,
8953 .set_key = iwl_mac_set_key,
8954 .get_stats = iwl_mac_get_stats,
8955 .get_tx_stats = iwl_mac_get_tx_stats,
8956 .conf_tx = iwl_mac_conf_tx,
8957 .get_tsf = iwl_mac_get_tsf,
8958 .reset_tsf = iwl_mac_reset_tsf,
8959 .beacon_update = iwl_mac_beacon_update,
8960 .erp_ie_changed = iwl_mac_erp_ie_changed,
8961 #ifdef CONFIG_IWLWIFI_HT
8962 .conf_ht = iwl_mac_conf_ht,
8963 .get_ht_capab = iwl_mac_get_ht_capab,
8964 #ifdef CONFIG_IWLWIFI_HT_AGG
8965 .ht_tx_agg_start = iwl_mac_ht_tx_agg_start,
8966 .ht_tx_agg_stop = iwl_mac_ht_tx_agg_stop,
8967 .ht_rx_agg_start = iwl_mac_ht_rx_agg_start,
8968 .ht_rx_agg_stop = iwl_mac_ht_rx_agg_stop,
8969 #endif /* CONFIG_IWLWIFI_HT_AGG */
8970 #endif /* CONFIG_IWLWIFI_HT */
8971 .hw_scan = iwl_mac_hw_scan
8974 static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8976 int err = 0;
8977 struct iwl_priv *priv;
8978 struct ieee80211_hw *hw;
8979 int i;
8981 if (iwl_param_disable_hw_scan) {
8982 IWL_DEBUG_INFO("Disabling hw_scan\n");
8983 iwl_hw_ops.hw_scan = NULL;
8986 if ((iwl_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8987 (iwl_param_queues_num < IWL_MIN_NUM_QUEUES)) {
8988 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8989 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8990 err = -EINVAL;
8991 goto out;
8994 /* mac80211 allocates memory for this device instance, including
8995 * space for this driver's private structure */
8996 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl_hw_ops);
8997 if (hw == NULL) {
8998 IWL_ERROR("Can not allocate network device\n");
8999 err = -ENOMEM;
9000 goto out;
9002 SET_IEEE80211_DEV(hw, &pdev->dev);
9004 hw->rate_control_algorithm = "iwl-4965-rs";
9006 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
9007 priv = hw->priv;
9008 priv->hw = hw;
9010 priv->pci_dev = pdev;
9011 priv->antenna = (enum iwl_antenna)iwl_param_antenna;
9012 #ifdef CONFIG_IWLWIFI_DEBUG
9013 iwl_debug_level = iwl_param_debug;
9014 atomic_set(&priv->restrict_refcnt, 0);
9015 #endif
9016 priv->retry_rate = 1;
9018 priv->ibss_beacon = NULL;
9020 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
9021 * the range of signal quality values that we'll provide.
9022 * Negative values for level/noise indicate that we'll provide dBm.
9023 * For WE, at least, non-0 values here *enable* display of values
9024 * in app (iwconfig). */
9025 hw->max_rssi = -20; /* signal level, negative indicates dBm */
9026 hw->max_noise = -20; /* noise level, negative indicates dBm */
9027 hw->max_signal = 100; /* link quality indication (%) */
9029 /* Tell mac80211 our Tx characteristics */
9030 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
9032 hw->queues = 4;
9033 #ifdef CONFIG_IWLWIFI_HT
9034 #ifdef CONFIG_IWLWIFI_HT_AGG
9035 hw->queues = 16;
9036 #endif /* CONFIG_IWLWIFI_HT_AGG */
9037 #endif /* CONFIG_IWLWIFI_HT */
9039 spin_lock_init(&priv->lock);
9040 spin_lock_init(&priv->power_data.lock);
9041 spin_lock_init(&priv->sta_lock);
9042 spin_lock_init(&priv->hcmd_lock);
9043 spin_lock_init(&priv->lq_mngr.lock);
9045 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
9046 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
9048 INIT_LIST_HEAD(&priv->free_frames);
9050 mutex_init(&priv->mutex);
9051 if (pci_enable_device(pdev)) {
9052 err = -ENODEV;
9053 goto out_ieee80211_free_hw;
9056 pci_set_master(pdev);
9058 iwl_clear_stations_table(priv);
9060 priv->data_retry_limit = -1;
9061 priv->ieee_channels = NULL;
9062 priv->ieee_rates = NULL;
9063 priv->phymode = -1;
9065 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
9066 if (!err)
9067 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
9068 if (err) {
9069 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
9070 goto out_pci_disable_device;
9073 pci_set_drvdata(pdev, priv);
9074 err = pci_request_regions(pdev, DRV_NAME);
9075 if (err)
9076 goto out_pci_disable_device;
9077 /* We disable the RETRY_TIMEOUT register (0x41) to keep
9078 * PCI Tx retries from interfering with C3 CPU state */
9079 pci_write_config_byte(pdev, 0x41, 0x00);
9080 priv->hw_base = pci_iomap(pdev, 0, 0);
9081 if (!priv->hw_base) {
9082 err = -ENODEV;
9083 goto out_pci_release_regions;
9086 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
9087 (unsigned long long) pci_resource_len(pdev, 0));
9088 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
9090 /* Initialize module parameter values here */
9092 if (iwl_param_disable) {
9093 set_bit(STATUS_RF_KILL_SW, &priv->status);
9094 IWL_DEBUG_INFO("Radio disabled.\n");
9097 priv->iw_mode = IEEE80211_IF_TYPE_STA;
9099 priv->ps_mode = 0;
9100 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
9101 priv->is_ht_enabled = 1;
9102 priv->channel_width = IWL_CHANNEL_WIDTH_40MHZ;
9103 priv->valid_antenna = 0x7; /* assume all 3 connected */
9104 priv->ps_mode = IWL_MIMO_PS_NONE;
9105 priv->cck_power_index_compensation = iwl_read32(
9106 priv, CSR_HW_REV_WA_REG);
9108 iwl4965_set_rxon_chain(priv);
9110 printk(KERN_INFO DRV_NAME
9111 ": Detected Intel Wireless WiFi Link 4965AGN\n");
9113 /* Device-specific setup */
9114 if (iwl_hw_set_hw_setting(priv)) {
9115 IWL_ERROR("failed to set hw settings\n");
9116 mutex_unlock(&priv->mutex);
9117 goto out_iounmap;
9120 #ifdef CONFIG_IWLWIFI_QOS
9121 if (iwl_param_qos_enable)
9122 priv->qos_data.qos_enable = 1;
9124 iwl_reset_qos(priv);
9126 priv->qos_data.qos_active = 0;
9127 priv->qos_data.qos_cap.val = 0;
9128 #endif /* CONFIG_IWLWIFI_QOS */
9130 iwl_set_rxon_channel(priv, MODE_IEEE80211G, 6);
9131 iwl_setup_deferred_work(priv);
9132 iwl_setup_rx_handlers(priv);
9134 priv->rates_mask = IWL_RATES_MASK;
9135 /* If power management is turned on, default to AC mode */
9136 priv->power_mode = IWL_POWER_AC;
9137 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
9139 pci_enable_msi(pdev);
9141 err = request_irq(pdev->irq, iwl_isr, IRQF_SHARED, DRV_NAME, priv);
9142 if (err) {
9143 IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
9144 goto out_disable_msi;
9147 mutex_lock(&priv->mutex);
9149 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
9150 if (err) {
9151 IWL_ERROR("failed to create sysfs device attributes\n");
9152 mutex_unlock(&priv->mutex);
9153 goto out_release_irq;
9156 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
9157 * ucode filename and max sizes are card-specific. */
9158 err = iwl_read_ucode(priv);
9159 if (err) {
9160 IWL_ERROR("Could not read microcode: %d\n", err);
9161 mutex_unlock(&priv->mutex);
9162 goto out_pci_alloc;
9165 mutex_unlock(&priv->mutex);
9167 IWL_DEBUG_INFO("Queueing UP work.\n");
9169 queue_work(priv->workqueue, &priv->up);
9171 return 0;
9173 out_pci_alloc:
9174 iwl_dealloc_ucode_pci(priv);
9176 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
9178 out_release_irq:
9179 free_irq(pdev->irq, priv);
9181 out_disable_msi:
9182 pci_disable_msi(pdev);
9183 destroy_workqueue(priv->workqueue);
9184 priv->workqueue = NULL;
9185 iwl_unset_hw_setting(priv);
9187 out_iounmap:
9188 pci_iounmap(pdev, priv->hw_base);
9189 out_pci_release_regions:
9190 pci_release_regions(pdev);
9191 out_pci_disable_device:
9192 pci_disable_device(pdev);
9193 pci_set_drvdata(pdev, NULL);
9194 out_ieee80211_free_hw:
9195 ieee80211_free_hw(priv->hw);
9196 out:
9197 return err;
9200 static void iwl_pci_remove(struct pci_dev *pdev)
9202 struct iwl_priv *priv = pci_get_drvdata(pdev);
9203 struct list_head *p, *q;
9204 int i;
9206 if (!priv)
9207 return;
9209 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
9211 set_bit(STATUS_EXIT_PENDING, &priv->status);
9213 iwl_down(priv);
9215 /* Free MAC hash list for ADHOC */
9216 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
9217 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
9218 list_del(p);
9219 kfree(list_entry(p, struct iwl_ibss_seq, list));
9223 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
9225 iwl_dealloc_ucode_pci(priv);
9227 if (priv->rxq.bd)
9228 iwl_rx_queue_free(priv, &priv->rxq);
9229 iwl_hw_txq_ctx_free(priv);
9231 iwl_unset_hw_setting(priv);
9232 iwl_clear_stations_table(priv);
9234 if (priv->mac80211_registered) {
9235 ieee80211_unregister_hw(priv->hw);
9236 iwl_rate_control_unregister(priv->hw);
9239 /*netif_stop_queue(dev); */
9240 flush_workqueue(priv->workqueue);
9242 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
9243 * priv->workqueue... so we can't take down the workqueue
9244 * until now... */
9245 destroy_workqueue(priv->workqueue);
9246 priv->workqueue = NULL;
9248 free_irq(pdev->irq, priv);
9249 pci_disable_msi(pdev);
9250 pci_iounmap(pdev, priv->hw_base);
9251 pci_release_regions(pdev);
9252 pci_disable_device(pdev);
9253 pci_set_drvdata(pdev, NULL);
9255 kfree(priv->channel_info);
9257 kfree(priv->ieee_channels);
9258 kfree(priv->ieee_rates);
9260 if (priv->ibss_beacon)
9261 dev_kfree_skb(priv->ibss_beacon);
9263 ieee80211_free_hw(priv->hw);
9266 #ifdef CONFIG_PM
9268 static int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
9270 struct iwl_priv *priv = pci_get_drvdata(pdev);
9272 set_bit(STATUS_IN_SUSPEND, &priv->status);
9274 /* Take down the device; powers it off, etc. */
9275 iwl_down(priv);
9277 if (priv->mac80211_registered)
9278 ieee80211_stop_queues(priv->hw);
9280 pci_save_state(pdev);
9281 pci_disable_device(pdev);
9282 pci_set_power_state(pdev, PCI_D3hot);
9284 return 0;
9287 static void iwl_resume(struct iwl_priv *priv)
9289 unsigned long flags;
9291 /* The following it a temporary work around due to the
9292 * suspend / resume not fully initializing the NIC correctly.
9293 * Without all of the following, resume will not attempt to take
9294 * down the NIC (it shouldn't really need to) and will just try
9295 * and bring the NIC back up. However that fails during the
9296 * ucode verification process. This then causes iwl_down to be
9297 * called *after* iwl_hw_nic_init() has succeeded -- which
9298 * then lets the next init sequence succeed. So, we've
9299 * replicated all of that NIC init code here... */
9301 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
9303 iwl_hw_nic_init(priv);
9305 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9306 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
9307 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
9308 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
9309 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9310 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9312 /* tell the device to stop sending interrupts */
9313 iwl_disable_interrupts(priv);
9315 spin_lock_irqsave(&priv->lock, flags);
9316 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
9318 if (!iwl_grab_restricted_access(priv)) {
9319 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
9320 APMG_CLK_VAL_DMA_CLK_RQT);
9321 iwl_release_restricted_access(priv);
9323 spin_unlock_irqrestore(&priv->lock, flags);
9325 udelay(5);
9327 iwl_hw_nic_reset(priv);
9329 /* Bring the device back up */
9330 clear_bit(STATUS_IN_SUSPEND, &priv->status);
9331 queue_work(priv->workqueue, &priv->up);
9334 static int iwl_pci_resume(struct pci_dev *pdev)
9336 struct iwl_priv *priv = pci_get_drvdata(pdev);
9337 int err;
9339 printk(KERN_INFO "Coming out of suspend...\n");
9341 pci_set_power_state(pdev, PCI_D0);
9342 err = pci_enable_device(pdev);
9343 pci_restore_state(pdev);
9346 * Suspend/Resume resets the PCI configuration space, so we have to
9347 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
9348 * from interfering with C3 CPU state. pci_restore_state won't help
9349 * here since it only restores the first 64 bytes pci config header.
9351 pci_write_config_byte(pdev, 0x41, 0x00);
9353 iwl_resume(priv);
9355 return 0;
9358 #endif /* CONFIG_PM */
9360 /*****************************************************************************
9362 * driver and module entry point
9364 *****************************************************************************/
9366 static struct pci_driver iwl_driver = {
9367 .name = DRV_NAME,
9368 .id_table = iwl_hw_card_ids,
9369 .probe = iwl_pci_probe,
9370 .remove = __devexit_p(iwl_pci_remove),
9371 #ifdef CONFIG_PM
9372 .suspend = iwl_pci_suspend,
9373 .resume = iwl_pci_resume,
9374 #endif
9377 static int __init iwl_init(void)
9380 int ret;
9381 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
9382 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
9383 ret = pci_register_driver(&iwl_driver);
9384 if (ret) {
9385 IWL_ERROR("Unable to initialize PCI module\n");
9386 return ret;
9388 #ifdef CONFIG_IWLWIFI_DEBUG
9389 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
9390 if (ret) {
9391 IWL_ERROR("Unable to create driver sysfs file\n");
9392 pci_unregister_driver(&iwl_driver);
9393 return ret;
9395 #endif
9397 return ret;
9400 static void __exit iwl_exit(void)
9402 #ifdef CONFIG_IWLWIFI_DEBUG
9403 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
9404 #endif
9405 pci_unregister_driver(&iwl_driver);
9408 module_param_named(antenna, iwl_param_antenna, int, 0444);
9409 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
9410 module_param_named(disable, iwl_param_disable, int, 0444);
9411 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
9412 module_param_named(hwcrypto, iwl_param_hwcrypto, int, 0444);
9413 MODULE_PARM_DESC(hwcrypto,
9414 "using hardware crypto engine (default 0 [software])\n");
9415 module_param_named(debug, iwl_param_debug, int, 0444);
9416 MODULE_PARM_DESC(debug, "debug output mask");
9417 module_param_named(disable_hw_scan, iwl_param_disable_hw_scan, int, 0444);
9418 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
9420 module_param_named(queues_num, iwl_param_queues_num, int, 0444);
9421 MODULE_PARM_DESC(queues_num, "number of hw queues.");
9423 /* QoS */
9424 module_param_named(qos_enable, iwl_param_qos_enable, int, 0444);
9425 MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
9427 module_exit(iwl_exit);
9428 module_init(iwl_init);