[PATCH] iwl3945: fix direct scan problem
[linux-2.6/mini2440.git] / drivers / net / wireless / iwlwifi / iwl3945-base.c
blob4f22a7174caff31747511ced655ac0ce12a375cc
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 3945
61 #include "iwlwifi.h"
62 #include "iwl-3945.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 \
90 "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
92 #ifdef CONFIG_IWLWIFI_DEBUG
93 #define VD "d"
94 #else
95 #define VD
96 #endif
98 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
99 #define VS "s"
100 #else
101 #define VS
102 #endif
104 #define IWLWIFI_VERSION "1.1.17k" VD VS
105 #define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
106 #define DRV_VERSION IWLWIFI_VERSION
108 /* Change firmware file name, using "-" and incrementing number,
109 * *only* when uCode interface or architecture changes so that it
110 * is not compatible with earlier drivers.
111 * This number will also appear in << 8 position of 1st dword of uCode file */
112 #define IWL3945_UCODE_API "-1"
114 MODULE_DESCRIPTION(DRV_DESCRIPTION);
115 MODULE_VERSION(DRV_VERSION);
116 MODULE_AUTHOR(DRV_COPYRIGHT);
117 MODULE_LICENSE("GPL");
119 __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
121 u16 fc = le16_to_cpu(hdr->frame_control);
122 int hdr_len = ieee80211_get_hdrlen(fc);
124 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
125 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
126 return NULL;
129 static const struct ieee80211_hw_mode *iwl_get_hw_mode(
130 struct iwl_priv *priv, int mode)
132 int i;
134 for (i = 0; i < 3; i++)
135 if (priv->modes[i].mode == mode)
136 return &priv->modes[i];
138 return NULL;
141 static int iwl_is_empty_essid(const char *essid, int essid_len)
143 /* Single white space is for Linksys APs */
144 if (essid_len == 1 && essid[0] == ' ')
145 return 1;
147 /* Otherwise, if the entire essid is 0, we assume it is hidden */
148 while (essid_len) {
149 essid_len--;
150 if (essid[essid_len] != '\0')
151 return 0;
154 return 1;
157 static const char *iwl_escape_essid(const char *essid, u8 essid_len)
159 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
160 const char *s = essid;
161 char *d = escaped;
163 if (iwl_is_empty_essid(essid, essid_len)) {
164 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
165 return escaped;
168 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
169 while (essid_len--) {
170 if (*s == '\0') {
171 *d++ = '\\';
172 *d++ = '0';
173 s++;
174 } else
175 *d++ = *s++;
177 *d = '\0';
178 return escaped;
181 static void iwl_print_hex_dump(int level, void *p, u32 len)
183 #ifdef CONFIG_IWLWIFI_DEBUG
184 if (!(iwl_debug_level & level))
185 return;
187 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
188 p, len, 1);
189 #endif
192 /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
193 * DMA services
195 * Theory of operation
197 * A queue is a circular buffers with 'Read' and 'Write' pointers.
198 * 2 empty entries always kept in the buffer to protect from overflow.
200 * For Tx queue, there are low mark and high mark limits. If, after queuing
201 * the packet for Tx, free space become < low mark, Tx queue stopped. When
202 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
203 * Tx queue resumed.
205 * The IWL operates with six queues, one receive queue in the device's
206 * sram, one transmit queue for sending commands to the device firmware,
207 * and four transmit queues for data.
208 ***************************************************/
210 static int iwl_queue_space(const struct iwl_queue *q)
212 int s = q->last_used - q->first_empty;
214 if (q->last_used > q->first_empty)
215 s -= q->n_bd;
217 if (s <= 0)
218 s += q->n_window;
219 /* keep some reserve to not confuse empty and full situations */
220 s -= 2;
221 if (s < 0)
222 s = 0;
223 return s;
226 /* XXX: n_bd must be power-of-two size */
227 static inline int iwl_queue_inc_wrap(int index, int n_bd)
229 return ++index & (n_bd - 1);
232 /* XXX: n_bd must be power-of-two size */
233 static inline int iwl_queue_dec_wrap(int index, int n_bd)
235 return --index & (n_bd - 1);
238 static inline int x2_queue_used(const struct iwl_queue *q, int i)
240 return q->first_empty > q->last_used ?
241 (i >= q->last_used && i < q->first_empty) :
242 !(i < q->last_used && i >= q->first_empty);
245 static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
247 if (is_huge)
248 return q->n_window;
250 return index & (q->n_window - 1);
253 static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
254 int count, int slots_num, u32 id)
256 q->n_bd = count;
257 q->n_window = slots_num;
258 q->id = id;
260 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
261 * and iwl_queue_dec_wrap are broken. */
262 BUG_ON(!is_power_of_2(count));
264 /* slots_num must be power-of-two size, otherwise
265 * get_cmd_index is broken. */
266 BUG_ON(!is_power_of_2(slots_num));
268 q->low_mark = q->n_window / 4;
269 if (q->low_mark < 4)
270 q->low_mark = 4;
272 q->high_mark = q->n_window / 8;
273 if (q->high_mark < 2)
274 q->high_mark = 2;
276 q->first_empty = q->last_used = 0;
278 return 0;
281 static int iwl_tx_queue_alloc(struct iwl_priv *priv,
282 struct iwl_tx_queue *txq, u32 id)
284 struct pci_dev *dev = priv->pci_dev;
286 if (id != IWL_CMD_QUEUE_NUM) {
287 txq->txb = kmalloc(sizeof(txq->txb[0]) *
288 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
289 if (!txq->txb) {
290 IWL_ERROR("kmalloc for auxilary BD "
291 "structures failed\n");
292 goto error;
294 } else
295 txq->txb = NULL;
297 txq->bd = pci_alloc_consistent(dev,
298 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
299 &txq->q.dma_addr);
301 if (!txq->bd) {
302 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
303 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
304 goto error;
306 txq->q.id = id;
308 return 0;
310 error:
311 if (txq->txb) {
312 kfree(txq->txb);
313 txq->txb = NULL;
316 return -ENOMEM;
319 int iwl_tx_queue_init(struct iwl_priv *priv,
320 struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
322 struct pci_dev *dev = priv->pci_dev;
323 int len;
324 int rc = 0;
326 /* alocate command space + one big command for scan since scan
327 * command is very huge the system will not have two scan at the
328 * same time */
329 len = sizeof(struct iwl_cmd) * slots_num;
330 if (txq_id == IWL_CMD_QUEUE_NUM)
331 len += IWL_MAX_SCAN_SIZE;
332 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
333 if (!txq->cmd)
334 return -ENOMEM;
336 rc = iwl_tx_queue_alloc(priv, txq, txq_id);
337 if (rc) {
338 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
340 return -ENOMEM;
342 txq->need_update = 0;
344 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
345 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
346 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
347 iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
349 iwl_hw_tx_queue_init(priv, txq);
351 return 0;
355 * iwl_tx_queue_free - Deallocate DMA queue.
356 * @txq: Transmit queue to deallocate.
358 * Empty queue by removing and destroying all BD's.
359 * Free all buffers. txq itself is not freed.
362 void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
364 struct iwl_queue *q = &txq->q;
365 struct pci_dev *dev = priv->pci_dev;
366 int len;
368 if (q->n_bd == 0)
369 return;
371 /* first, empty all BD's */
372 for (; q->first_empty != q->last_used;
373 q->last_used = iwl_queue_inc_wrap(q->last_used, q->n_bd))
374 iwl_hw_txq_free_tfd(priv, txq);
376 len = sizeof(struct iwl_cmd) * q->n_window;
377 if (q->id == IWL_CMD_QUEUE_NUM)
378 len += IWL_MAX_SCAN_SIZE;
380 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
382 /* free buffers belonging to queue itself */
383 if (txq->q.n_bd)
384 pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
385 txq->q.n_bd, txq->bd, txq->q.dma_addr);
387 if (txq->txb) {
388 kfree(txq->txb);
389 txq->txb = NULL;
392 /* 0 fill whole structure */
393 memset(txq, 0, sizeof(*txq));
396 const u8 BROADCAST_ADDR[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
398 /*************** STATION TABLE MANAGEMENT ****
400 * NOTE: This needs to be overhauled to better synchronize between
401 * how the iwl-4965.c is using iwl_hw_find_station vs. iwl-3945.c
403 * mac80211 should also be examined to determine if sta_info is duplicating
404 * the functionality provided here
407 /**************************************************************/
408 #if 0 /* temparary disable till we add real remove station */
409 static u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
411 int index = IWL_INVALID_STATION;
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
445 static void iwl_clear_stations_table(struct iwl_priv *priv)
447 unsigned long flags;
449 spin_lock_irqsave(&priv->sta_lock, flags);
451 priv->num_stations = 0;
452 memset(priv->stations, 0, sizeof(priv->stations));
454 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);
465 u8 rate;
467 spin_lock_irqsave(&priv->sta_lock, flags_spin);
468 if (is_ap)
469 index = IWL_AP_ID;
470 else if (is_broadcast_ether_addr(addr))
471 index = priv->hw_setting.bcast_sta_id;
472 else
473 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
474 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
475 addr)) {
476 index = i;
477 break;
480 if (!priv->stations[i].used &&
481 index == IWL_INVALID_STATION)
482 index = i;
485 /* These twh conditions has the same outcome but keep them separate
486 since they have different meaning */
487 if (unlikely(index == IWL_INVALID_STATION)) {
488 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
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;
498 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
499 station = &priv->stations[index];
500 station->used = 1;
501 priv->num_stations++;
503 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
504 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
505 station->sta.mode = 0;
506 station->sta.sta.sta_id = index;
507 station->sta.station_flags = 0;
509 rate = (priv->phymode == MODE_IEEE80211A) ? IWL_RATE_6M_PLCP :
510 IWL_RATE_1M_PLCP | priv->hw_setting.cck_flag;
512 /* Turn on both antennas for the station... */
513 station->sta.rate_n_flags =
514 iwl_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
515 station->current_rate.rate_n_flags =
516 le16_to_cpu(station->sta.rate_n_flags);
518 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
519 iwl_send_add_station(priv, &station->sta, flags);
520 return index;
524 /*************** DRIVER STATUS FUNCTIONS *****/
526 static inline int iwl_is_ready(struct iwl_priv *priv)
528 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
529 * set but EXIT_PENDING is not */
530 return test_bit(STATUS_READY, &priv->status) &&
531 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
532 !test_bit(STATUS_EXIT_PENDING, &priv->status);
535 static inline int iwl_is_alive(struct iwl_priv *priv)
537 return test_bit(STATUS_ALIVE, &priv->status);
540 static inline int iwl_is_init(struct iwl_priv *priv)
542 return test_bit(STATUS_INIT, &priv->status);
545 static inline int iwl_is_rfkill(struct iwl_priv *priv)
547 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
548 test_bit(STATUS_RF_KILL_SW, &priv->status);
551 static inline int iwl_is_ready_rf(struct iwl_priv *priv)
554 if (iwl_is_rfkill(priv))
555 return 0;
557 return iwl_is_ready(priv);
560 /*************** HOST COMMAND QUEUE FUNCTIONS *****/
562 #define IWL_CMD(x) case x : return #x
564 static const char *get_cmd_string(u8 cmd)
566 switch (cmd) {
567 IWL_CMD(REPLY_ALIVE);
568 IWL_CMD(REPLY_ERROR);
569 IWL_CMD(REPLY_RXON);
570 IWL_CMD(REPLY_RXON_ASSOC);
571 IWL_CMD(REPLY_QOS_PARAM);
572 IWL_CMD(REPLY_RXON_TIMING);
573 IWL_CMD(REPLY_ADD_STA);
574 IWL_CMD(REPLY_REMOVE_STA);
575 IWL_CMD(REPLY_REMOVE_ALL_STA);
576 IWL_CMD(REPLY_3945_RX);
577 IWL_CMD(REPLY_TX);
578 IWL_CMD(REPLY_RATE_SCALE);
579 IWL_CMD(REPLY_LEDS_CMD);
580 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
581 IWL_CMD(RADAR_NOTIFICATION);
582 IWL_CMD(REPLY_QUIET_CMD);
583 IWL_CMD(REPLY_CHANNEL_SWITCH);
584 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
585 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
586 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
587 IWL_CMD(POWER_TABLE_CMD);
588 IWL_CMD(PM_SLEEP_NOTIFICATION);
589 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
590 IWL_CMD(REPLY_SCAN_CMD);
591 IWL_CMD(REPLY_SCAN_ABORT_CMD);
592 IWL_CMD(SCAN_START_NOTIFICATION);
593 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
594 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
595 IWL_CMD(BEACON_NOTIFICATION);
596 IWL_CMD(REPLY_TX_BEACON);
597 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
598 IWL_CMD(QUIET_NOTIFICATION);
599 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
600 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
601 IWL_CMD(REPLY_BT_CONFIG);
602 IWL_CMD(REPLY_STATISTICS_CMD);
603 IWL_CMD(STATISTICS_NOTIFICATION);
604 IWL_CMD(REPLY_CARD_STATE_CMD);
605 IWL_CMD(CARD_STATE_NOTIFICATION);
606 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
607 default:
608 return "UNKNOWN";
613 #define HOST_COMPLETE_TIMEOUT (HZ / 2)
616 * iwl_enqueue_hcmd - enqueue a uCode command
617 * @priv: device private data point
618 * @cmd: a point to the ucode command structure
620 * The function returns < 0 values to indicate the operation is
621 * failed. On success, it turns the index (> 0) of command in the
622 * command queue.
624 static int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
626 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
627 struct iwl_queue *q = &txq->q;
628 struct iwl_tfd_frame *tfd;
629 u32 *control_flags;
630 struct iwl_cmd *out_cmd;
631 u32 idx;
632 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
633 dma_addr_t phys_addr;
634 int pad;
635 u16 count;
636 int ret;
637 unsigned long flags;
639 /* If any of the command structures end up being larger than
640 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
641 * we will need to increase the size of the TFD entries */
642 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
643 !(cmd->meta.flags & CMD_SIZE_HUGE));
645 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
646 IWL_ERROR("No space for Tx\n");
647 return -ENOSPC;
650 spin_lock_irqsave(&priv->hcmd_lock, flags);
652 tfd = &txq->bd[q->first_empty];
653 memset(tfd, 0, sizeof(*tfd));
655 control_flags = (u32 *) tfd;
657 idx = get_cmd_index(q, q->first_empty, cmd->meta.flags & CMD_SIZE_HUGE);
658 out_cmd = &txq->cmd[idx];
660 out_cmd->hdr.cmd = cmd->id;
661 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
662 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
664 /* At this point, the out_cmd now has all of the incoming cmd
665 * information */
667 out_cmd->hdr.flags = 0;
668 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
669 INDEX_TO_SEQ(q->first_empty));
670 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
671 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
673 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
674 offsetof(struct iwl_cmd, hdr);
675 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
677 pad = U32_PAD(cmd->len);
678 count = TFD_CTL_COUNT_GET(*control_flags);
679 *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
681 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
682 "%d bytes at %d[%d]:%d\n",
683 get_cmd_string(out_cmd->hdr.cmd),
684 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
685 fix_size, q->first_empty, idx, IWL_CMD_QUEUE_NUM);
687 txq->need_update = 1;
688 q->first_empty = iwl_queue_inc_wrap(q->first_empty, q->n_bd);
689 ret = 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_set_rxon_channel - Set the phymode and channel values in staging RXON
848 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
849 * @channel: Any channel valid for the requested phymode
851 * In addition to setting the staging RXON, priv->phymode is also set.
853 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
854 * in the staging RXON flag structure based on the phymode
856 static int iwl_set_rxon_channel(struct iwl_priv *priv, u8 phymode, u16 channel)
858 if (!iwl_get_channel_info(priv, phymode, channel)) {
859 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
860 channel, phymode);
861 return -EINVAL;
864 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
865 (priv->phymode == phymode))
866 return 0;
868 priv->staging_rxon.channel = cpu_to_le16(channel);
869 if (phymode == MODE_IEEE80211A)
870 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
871 else
872 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
874 priv->phymode = phymode;
876 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
878 return 0;
882 * iwl_check_rxon_cmd - validate RXON structure is valid
884 * NOTE: This is really only useful during development and can eventually
885 * be #ifdef'd out once the driver is stable and folks aren't actively
886 * making changes
888 static int iwl_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
890 int error = 0;
891 int counter = 1;
893 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
894 error |= le32_to_cpu(rxon->flags &
895 (RXON_FLG_TGJ_NARROW_BAND_MSK |
896 RXON_FLG_RADAR_DETECT_MSK));
897 if (error)
898 IWL_WARNING("check 24G fields %d | %d\n",
899 counter++, error);
900 } else {
901 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
902 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
903 if (error)
904 IWL_WARNING("check 52 fields %d | %d\n",
905 counter++, error);
906 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
907 if (error)
908 IWL_WARNING("check 52 CCK %d | %d\n",
909 counter++, error);
911 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
912 if (error)
913 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
915 /* make sure basic rates 6Mbps and 1Mbps are supported */
916 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
917 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
918 if (error)
919 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
921 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
922 if (error)
923 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
925 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
926 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
927 if (error)
928 IWL_WARNING("check CCK and short slot %d | %d\n",
929 counter++, error);
931 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
932 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
933 if (error)
934 IWL_WARNING("check CCK & auto detect %d | %d\n",
935 counter++, error);
937 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
938 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
939 if (error)
940 IWL_WARNING("check TGG and auto detect %d | %d\n",
941 counter++, error);
943 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
944 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
945 RXON_FLG_ANT_A_MSK)) == 0);
946 if (error)
947 IWL_WARNING("check antenna %d %d\n", counter++, error);
949 if (error)
950 IWL_WARNING("Tuning to channel %d\n",
951 le16_to_cpu(rxon->channel));
953 if (error) {
954 IWL_ERROR("Not a valid iwl_rxon_assoc_cmd field values\n");
955 return -1;
957 return 0;
961 * iwl_full_rxon_required - determine if RXON_ASSOC can be used in RXON commit
962 * @priv: staging_rxon is comapred to active_rxon
964 * If the RXON structure is changing sufficient to require a new
965 * tune or to clear and reset the RXON_FILTER_ASSOC_MSK then return 1
966 * to indicate a new tune is required.
968 static int iwl_full_rxon_required(struct iwl_priv *priv)
971 /* These items are only settable from the full RXON command */
972 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
973 compare_ether_addr(priv->staging_rxon.bssid_addr,
974 priv->active_rxon.bssid_addr) ||
975 compare_ether_addr(priv->staging_rxon.node_addr,
976 priv->active_rxon.node_addr) ||
977 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
978 priv->active_rxon.wlap_bssid_addr) ||
979 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
980 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
981 (priv->staging_rxon.air_propagation !=
982 priv->active_rxon.air_propagation) ||
983 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
984 return 1;
986 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
987 * be updated with the RXON_ASSOC command -- however only some
988 * flag transitions are allowed using RXON_ASSOC */
990 /* Check if we are not switching bands */
991 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
992 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
993 return 1;
995 /* Check if we are switching association toggle */
996 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
997 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
998 return 1;
1000 return 0;
1003 static int iwl_send_rxon_assoc(struct iwl_priv *priv)
1005 int rc = 0;
1006 struct iwl_rx_packet *res = NULL;
1007 struct iwl_rxon_assoc_cmd rxon_assoc;
1008 struct iwl_host_cmd cmd = {
1009 .id = REPLY_RXON_ASSOC,
1010 .len = sizeof(rxon_assoc),
1011 .meta.flags = CMD_WANT_SKB,
1012 .data = &rxon_assoc,
1014 const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
1015 const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
1017 if ((rxon1->flags == rxon2->flags) &&
1018 (rxon1->filter_flags == rxon2->filter_flags) &&
1019 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1020 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1021 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1022 return 0;
1025 rxon_assoc.flags = priv->staging_rxon.flags;
1026 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1027 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1028 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1029 rxon_assoc.reserved = 0;
1031 rc = iwl_send_cmd_sync(priv, &cmd);
1032 if (rc)
1033 return rc;
1035 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1036 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1037 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1038 rc = -EIO;
1041 priv->alloc_rxb_skb--;
1042 dev_kfree_skb_any(cmd.meta.u.skb);
1044 return rc;
1048 * iwl_commit_rxon - commit staging_rxon to hardware
1050 * The RXON command in staging_rxon is commited to the hardware and
1051 * the active_rxon structure is updated with the new data. This
1052 * function correctly transitions out of the RXON_ASSOC_MSK state if
1053 * a HW tune is required based on the RXON structure changes.
1055 static int iwl_commit_rxon(struct iwl_priv *priv)
1057 /* cast away the const for active_rxon in this function */
1058 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
1059 int rc = 0;
1060 DECLARE_MAC_BUF(mac);
1062 if (!iwl_is_alive(priv))
1063 return -1;
1065 /* always get timestamp with Rx frame */
1066 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1068 /* select antenna */
1069 priv->staging_rxon.flags &=
1070 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
1071 priv->staging_rxon.flags |= iwl3945_get_antenna_flags(priv);
1073 rc = iwl_check_rxon_cmd(&priv->staging_rxon);
1074 if (rc) {
1075 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1076 return -EINVAL;
1079 /* If we don't need to send a full RXON, we can use
1080 * iwl_rxon_assoc_cmd which is used to reconfigure filter
1081 * and other flags for the current radio configuration. */
1082 if (!iwl_full_rxon_required(priv)) {
1083 rc = iwl_send_rxon_assoc(priv);
1084 if (rc) {
1085 IWL_ERROR("Error setting RXON_ASSOC "
1086 "configuration (%d).\n", rc);
1087 return rc;
1090 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1092 return 0;
1095 /* If we are currently associated and the new config requires
1096 * an RXON_ASSOC and the new config wants the associated mask enabled,
1097 * we must clear the associated from the active configuration
1098 * before we apply the new config */
1099 if (iwl_is_associated(priv) &&
1100 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1101 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1102 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1104 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1105 sizeof(struct iwl_rxon_cmd),
1106 &priv->active_rxon);
1108 /* If the mask clearing failed then we set
1109 * active_rxon back to what it was previously */
1110 if (rc) {
1111 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1112 IWL_ERROR("Error clearing ASSOC_MSK on current "
1113 "configuration (%d).\n", rc);
1114 return rc;
1118 IWL_DEBUG_INFO("Sending RXON\n"
1119 "* with%s RXON_FILTER_ASSOC_MSK\n"
1120 "* channel = %d\n"
1121 "* bssid = %s\n",
1122 ((priv->staging_rxon.filter_flags &
1123 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1124 le16_to_cpu(priv->staging_rxon.channel),
1125 print_mac(mac, priv->staging_rxon.bssid_addr));
1127 /* Apply the new configuration */
1128 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1129 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
1130 if (rc) {
1131 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1132 return rc;
1135 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1137 iwl_clear_stations_table(priv);
1139 /* If we issue a new RXON command which required a tune then we must
1140 * send a new TXPOWER command or we won't be able to Tx any frames */
1141 rc = iwl_hw_reg_send_txpower(priv);
1142 if (rc) {
1143 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1144 return rc;
1147 /* Add the broadcast address so we can send broadcast frames */
1148 if (iwl_add_station(priv, BROADCAST_ADDR, 0, 0) ==
1149 IWL_INVALID_STATION) {
1150 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1151 return -EIO;
1154 /* If we have set the ASSOC_MSK and we are in BSS mode then
1155 * add the IWL_AP_ID to the station rate table */
1156 if (iwl_is_associated(priv) &&
1157 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
1158 if (iwl_add_station(priv, priv->active_rxon.bssid_addr, 1, 0)
1159 == IWL_INVALID_STATION) {
1160 IWL_ERROR("Error adding AP address for transmit.\n");
1161 return -EIO;
1164 /* Init the hardware's rate fallback order based on the
1165 * phymode */
1166 rc = iwl3945_init_hw_rate_table(priv);
1167 if (rc) {
1168 IWL_ERROR("Error setting HW rate table: %02X\n", rc);
1169 return -EIO;
1172 return 0;
1175 static int iwl_send_bt_config(struct iwl_priv *priv)
1177 struct iwl_bt_cmd bt_cmd = {
1178 .flags = 3,
1179 .lead_time = 0xAA,
1180 .max_kill = 1,
1181 .kill_ack_mask = 0,
1182 .kill_cts_mask = 0,
1185 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1186 sizeof(struct iwl_bt_cmd), &bt_cmd);
1189 static int iwl_send_scan_abort(struct iwl_priv *priv)
1191 int rc = 0;
1192 struct iwl_rx_packet *res;
1193 struct iwl_host_cmd cmd = {
1194 .id = REPLY_SCAN_ABORT_CMD,
1195 .meta.flags = CMD_WANT_SKB,
1198 /* If there isn't a scan actively going on in the hardware
1199 * then we are in between scan bands and not actually
1200 * actively scanning, so don't send the abort command */
1201 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1202 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1203 return 0;
1206 rc = iwl_send_cmd_sync(priv, &cmd);
1207 if (rc) {
1208 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1209 return rc;
1212 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1213 if (res->u.status != CAN_ABORT_STATUS) {
1214 /* The scan abort will return 1 for success or
1215 * 2 for "failure". A failure condition can be
1216 * due to simply not being in an active scan which
1217 * can occur if we send the scan abort before we
1218 * the microcode has notified us that a scan is
1219 * completed. */
1220 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1221 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1222 clear_bit(STATUS_SCAN_HW, &priv->status);
1225 dev_kfree_skb_any(cmd.meta.u.skb);
1227 return rc;
1230 static int iwl_card_state_sync_callback(struct iwl_priv *priv,
1231 struct iwl_cmd *cmd,
1232 struct sk_buff *skb)
1234 return 1;
1238 * CARD_STATE_CMD
1240 * Use: Sets the internal card state to enable, disable, or halt
1242 * When in the 'enable' state the card operates as normal.
1243 * When in the 'disable' state, the card enters into a low power mode.
1244 * When in the 'halt' state, the card is shut down and must be fully
1245 * restarted to come back on.
1247 static int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
1249 struct iwl_host_cmd cmd = {
1250 .id = REPLY_CARD_STATE_CMD,
1251 .len = sizeof(u32),
1252 .data = &flags,
1253 .meta.flags = meta_flag,
1256 if (meta_flag & CMD_ASYNC)
1257 cmd.meta.u.callback = iwl_card_state_sync_callback;
1259 return iwl_send_cmd(priv, &cmd);
1262 static int iwl_add_sta_sync_callback(struct iwl_priv *priv,
1263 struct iwl_cmd *cmd, struct sk_buff *skb)
1265 struct iwl_rx_packet *res = NULL;
1267 if (!skb) {
1268 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1269 return 1;
1272 res = (struct iwl_rx_packet *)skb->data;
1273 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1274 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1275 res->hdr.flags);
1276 return 1;
1279 switch (res->u.add_sta.status) {
1280 case ADD_STA_SUCCESS_MSK:
1281 break;
1282 default:
1283 break;
1286 /* We didn't cache the SKB; let the caller free it */
1287 return 1;
1290 int iwl_send_add_station(struct iwl_priv *priv,
1291 struct iwl_addsta_cmd *sta, u8 flags)
1293 struct iwl_rx_packet *res = NULL;
1294 int rc = 0;
1295 struct iwl_host_cmd cmd = {
1296 .id = REPLY_ADD_STA,
1297 .len = sizeof(struct iwl_addsta_cmd),
1298 .meta.flags = flags,
1299 .data = sta,
1302 if (flags & CMD_ASYNC)
1303 cmd.meta.u.callback = iwl_add_sta_sync_callback;
1304 else
1305 cmd.meta.flags |= CMD_WANT_SKB;
1307 rc = iwl_send_cmd(priv, &cmd);
1309 if (rc || (flags & CMD_ASYNC))
1310 return rc;
1312 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1313 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1314 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1315 res->hdr.flags);
1316 rc = -EIO;
1319 if (rc == 0) {
1320 switch (res->u.add_sta.status) {
1321 case ADD_STA_SUCCESS_MSK:
1322 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1323 break;
1324 default:
1325 rc = -EIO;
1326 IWL_WARNING("REPLY_ADD_STA failed\n");
1327 break;
1331 priv->alloc_rxb_skb--;
1332 dev_kfree_skb_any(cmd.meta.u.skb);
1334 return rc;
1337 static int iwl_update_sta_key_info(struct iwl_priv *priv,
1338 struct ieee80211_key_conf *keyconf,
1339 u8 sta_id)
1341 unsigned long flags;
1342 __le16 key_flags = 0;
1344 switch (keyconf->alg) {
1345 case ALG_CCMP:
1346 key_flags |= STA_KEY_FLG_CCMP;
1347 key_flags |= cpu_to_le16(
1348 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1349 key_flags &= ~STA_KEY_FLG_INVALID;
1350 break;
1351 case ALG_TKIP:
1352 case ALG_WEP:
1353 return -EINVAL;
1354 default:
1355 return -EINVAL;
1357 spin_lock_irqsave(&priv->sta_lock, flags);
1358 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1359 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1360 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1361 keyconf->keylen);
1363 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1364 keyconf->keylen);
1365 priv->stations[sta_id].sta.key.key_flags = key_flags;
1366 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1367 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1369 spin_unlock_irqrestore(&priv->sta_lock, flags);
1371 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
1372 iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1373 return 0;
1376 static int iwl_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
1378 unsigned long flags;
1380 spin_lock_irqsave(&priv->sta_lock, flags);
1381 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
1382 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl_keyinfo));
1383 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1384 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1385 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1386 spin_unlock_irqrestore(&priv->sta_lock, flags);
1388 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
1389 iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1390 return 0;
1393 static void iwl_clear_free_frames(struct iwl_priv *priv)
1395 struct list_head *element;
1397 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1398 priv->frames_count);
1400 while (!list_empty(&priv->free_frames)) {
1401 element = priv->free_frames.next;
1402 list_del(element);
1403 kfree(list_entry(element, struct iwl_frame, list));
1404 priv->frames_count--;
1407 if (priv->frames_count) {
1408 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1409 priv->frames_count);
1410 priv->frames_count = 0;
1414 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
1416 struct iwl_frame *frame;
1417 struct list_head *element;
1418 if (list_empty(&priv->free_frames)) {
1419 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1420 if (!frame) {
1421 IWL_ERROR("Could not allocate frame!\n");
1422 return NULL;
1425 priv->frames_count++;
1426 return frame;
1429 element = priv->free_frames.next;
1430 list_del(element);
1431 return list_entry(element, struct iwl_frame, list);
1434 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
1436 memset(frame, 0, sizeof(*frame));
1437 list_add(&frame->list, &priv->free_frames);
1440 unsigned int iwl_fill_beacon_frame(struct iwl_priv *priv,
1441 struct ieee80211_hdr *hdr,
1442 const u8 *dest, int left)
1445 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
1446 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1447 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1448 return 0;
1450 if (priv->ibss_beacon->len > left)
1451 return 0;
1453 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1455 return priv->ibss_beacon->len;
1458 static int iwl_rate_index_from_plcp(int plcp)
1460 int i = 0;
1462 for (i = 0; i < IWL_RATE_COUNT; i++)
1463 if (iwl_rates[i].plcp == plcp)
1464 return i;
1465 return -1;
1468 static u8 iwl_rate_get_lowest_plcp(int rate_mask)
1470 u8 i;
1472 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
1473 i = iwl_rates[i].next_ieee) {
1474 if (rate_mask & (1 << i))
1475 return iwl_rates[i].plcp;
1478 return IWL_RATE_INVALID;
1481 static int iwl_send_beacon_cmd(struct iwl_priv *priv)
1483 struct iwl_frame *frame;
1484 unsigned int frame_size;
1485 int rc;
1486 u8 rate;
1488 frame = iwl_get_free_frame(priv);
1490 if (!frame) {
1491 IWL_ERROR("Could not obtain free frame buffer for beacon "
1492 "command.\n");
1493 return -ENOMEM;
1496 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
1497 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic &
1498 0xFF0);
1499 if (rate == IWL_INVALID_RATE)
1500 rate = IWL_RATE_6M_PLCP;
1501 } else {
1502 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
1503 if (rate == IWL_INVALID_RATE)
1504 rate = IWL_RATE_1M_PLCP;
1507 frame_size = iwl_hw_get_beacon_cmd(priv, frame, rate);
1509 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
1510 &frame->u.cmd[0]);
1512 iwl_free_frame(priv, frame);
1514 return rc;
1517 /******************************************************************************
1519 * EEPROM related functions
1521 ******************************************************************************/
1523 static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
1525 memcpy(mac, priv->eeprom.mac_address, 6);
1529 * iwl_eeprom_init - read EEPROM contents
1531 * Load the EEPROM from adapter into priv->eeprom
1533 * NOTE: This routine uses the non-debug IO access functions.
1535 int iwl_eeprom_init(struct iwl_priv *priv)
1537 u16 *e = (u16 *)&priv->eeprom;
1538 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
1539 u32 r;
1540 int sz = sizeof(priv->eeprom);
1541 int rc;
1542 int i;
1543 u16 addr;
1545 /* The EEPROM structure has several padding buffers within it
1546 * and when adding new EEPROM maps is subject to programmer errors
1547 * which may be very difficult to identify without explicitly
1548 * checking the resulting size of the eeprom map. */
1549 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1551 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1552 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1553 return -ENOENT;
1556 rc = iwl_eeprom_aqcuire_semaphore(priv);
1557 if (rc < 0) {
1558 IWL_ERROR("Failed to aqcuire EEPROM semaphore.\n");
1559 return -ENOENT;
1562 /* eeprom is an array of 16bit values */
1563 for (addr = 0; addr < sz; addr += sizeof(u16)) {
1564 _iwl_write32(priv, CSR_EEPROM_REG, addr << 1);
1565 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1567 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1568 i += IWL_EEPROM_ACCESS_DELAY) {
1569 r = _iwl_read_restricted(priv, CSR_EEPROM_REG);
1570 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1571 break;
1572 udelay(IWL_EEPROM_ACCESS_DELAY);
1575 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1576 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1577 return -ETIMEDOUT;
1579 e[addr / 2] = le16_to_cpu(r >> 16);
1582 return 0;
1585 /******************************************************************************
1587 * Misc. internal state and helper functions
1589 ******************************************************************************/
1590 #ifdef CONFIG_IWLWIFI_DEBUG
1593 * iwl_report_frame - dump frame to syslog during debug sessions
1595 * hack this function to show different aspects of received frames,
1596 * including selective frame dumps.
1597 * group100 parameter selects whether to show 1 out of 100 good frames.
1599 * TODO: ieee80211_hdr stuff is common to 3945 and 4965, so frame type
1600 * info output is okay, but some of this stuff (e.g. iwl_rx_frame_stats)
1601 * is 3945-specific and gives bad output for 4965. Need to split the
1602 * functionality, keep common stuff here.
1604 void iwl_report_frame(struct iwl_priv *priv,
1605 struct iwl_rx_packet *pkt,
1606 struct ieee80211_hdr *header, int group100)
1608 u32 to_us;
1609 u32 print_summary = 0;
1610 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1611 u32 hundred = 0;
1612 u32 dataframe = 0;
1613 u16 fc;
1614 u16 seq_ctl;
1615 u16 channel;
1616 u16 phy_flags;
1617 int rate_sym;
1618 u16 length;
1619 u16 status;
1620 u16 bcn_tmr;
1621 u32 tsf_low;
1622 u64 tsf;
1623 u8 rssi;
1624 u8 agc;
1625 u16 sig_avg;
1626 u16 noise_diff;
1627 struct iwl_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1628 struct iwl_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1629 struct iwl_rx_frame_end *rx_end = IWL_RX_END(pkt);
1630 u8 *data = IWL_RX_DATA(pkt);
1632 /* MAC header */
1633 fc = le16_to_cpu(header->frame_control);
1634 seq_ctl = le16_to_cpu(header->seq_ctrl);
1636 /* metadata */
1637 channel = le16_to_cpu(rx_hdr->channel);
1638 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1639 rate_sym = rx_hdr->rate;
1640 length = le16_to_cpu(rx_hdr->len);
1642 /* end-of-frame status and timestamp */
1643 status = le32_to_cpu(rx_end->status);
1644 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1645 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1646 tsf = le64_to_cpu(rx_end->timestamp);
1648 /* signal statistics */
1649 rssi = rx_stats->rssi;
1650 agc = rx_stats->agc;
1651 sig_avg = le16_to_cpu(rx_stats->sig_avg);
1652 noise_diff = le16_to_cpu(rx_stats->noise_diff);
1654 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1656 /* if data frame is to us and all is good,
1657 * (optionally) print summary for only 1 out of every 100 */
1658 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1659 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1660 dataframe = 1;
1661 if (!group100)
1662 print_summary = 1; /* print each frame */
1663 else if (priv->framecnt_to_us < 100) {
1664 priv->framecnt_to_us++;
1665 print_summary = 0;
1666 } else {
1667 priv->framecnt_to_us = 0;
1668 print_summary = 1;
1669 hundred = 1;
1671 } else {
1672 /* print summary for all other frames */
1673 print_summary = 1;
1676 if (print_summary) {
1677 char *title;
1678 u32 rate;
1680 if (hundred)
1681 title = "100Frames";
1682 else if (fc & IEEE80211_FCTL_RETRY)
1683 title = "Retry";
1684 else if (ieee80211_is_assoc_response(fc))
1685 title = "AscRsp";
1686 else if (ieee80211_is_reassoc_response(fc))
1687 title = "RasRsp";
1688 else if (ieee80211_is_probe_response(fc)) {
1689 title = "PrbRsp";
1690 print_dump = 1; /* dump frame contents */
1691 } else if (ieee80211_is_beacon(fc)) {
1692 title = "Beacon";
1693 print_dump = 1; /* dump frame contents */
1694 } else if (ieee80211_is_atim(fc))
1695 title = "ATIM";
1696 else if (ieee80211_is_auth(fc))
1697 title = "Auth";
1698 else if (ieee80211_is_deauth(fc))
1699 title = "DeAuth";
1700 else if (ieee80211_is_disassoc(fc))
1701 title = "DisAssoc";
1702 else
1703 title = "Frame";
1705 rate = iwl_rate_index_from_plcp(rate_sym);
1706 if (rate == -1)
1707 rate = 0;
1708 else
1709 rate = iwl_rates[rate].ieee / 2;
1711 /* print frame summary.
1712 * MAC addresses show just the last byte (for brevity),
1713 * but you can hack it to show more, if you'd like to. */
1714 if (dataframe)
1715 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1716 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1717 title, fc, header->addr1[5],
1718 length, rssi, channel, rate);
1719 else {
1720 /* src/dst addresses assume managed mode */
1721 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1722 "src=0x%02x, rssi=%u, tim=%lu usec, "
1723 "phy=0x%02x, chnl=%d\n",
1724 title, fc, header->addr1[5],
1725 header->addr3[5], rssi,
1726 tsf_low - priv->scan_start_tsf,
1727 phy_flags, channel);
1730 if (print_dump)
1731 iwl_print_hex_dump(IWL_DL_RX, data, length);
1733 #endif
1735 static void iwl_unset_hw_setting(struct iwl_priv *priv)
1737 if (priv->hw_setting.shared_virt)
1738 pci_free_consistent(priv->pci_dev,
1739 sizeof(struct iwl_shared),
1740 priv->hw_setting.shared_virt,
1741 priv->hw_setting.shared_phys);
1745 * iwl_supported_rate_to_ie - fill in the supported rate in IE field
1747 * return : set the bit for each supported rate insert in ie
1749 static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
1750 u16 basic_rate, int *left)
1752 u16 ret_rates = 0, bit;
1753 int i;
1754 u8 *cnt = ie;
1755 u8 *rates = ie + 1;
1757 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1758 if (bit & supported_rate) {
1759 ret_rates |= bit;
1760 rates[*cnt] = iwl_rates[i].ieee |
1761 ((bit & basic_rate) ? 0x80 : 0x00);
1762 (*cnt)++;
1763 (*left)--;
1764 if ((*left <= 0) ||
1765 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
1766 break;
1770 return ret_rates;
1774 * iwl_fill_probe_req - fill in all required fields and IE for probe request
1776 static u16 iwl_fill_probe_req(struct iwl_priv *priv,
1777 struct ieee80211_mgmt *frame,
1778 int left, int is_direct)
1780 int len = 0;
1781 u8 *pos = NULL;
1782 u16 active_rates, ret_rates, cck_rates;
1784 /* Make sure there is enough space for the probe request,
1785 * two mandatory IEs and the data */
1786 left -= 24;
1787 if (left < 0)
1788 return 0;
1789 len += 24;
1791 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1792 memcpy(frame->da, BROADCAST_ADDR, ETH_ALEN);
1793 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
1794 memcpy(frame->bssid, BROADCAST_ADDR, ETH_ALEN);
1795 frame->seq_ctrl = 0;
1797 /* fill in our indirect SSID IE */
1798 /* ...next IE... */
1800 left -= 2;
1801 if (left < 0)
1802 return 0;
1803 len += 2;
1804 pos = &(frame->u.probe_req.variable[0]);
1805 *pos++ = WLAN_EID_SSID;
1806 *pos++ = 0;
1808 /* fill in our direct SSID IE... */
1809 if (is_direct) {
1810 /* ...next IE... */
1811 left -= 2 + priv->essid_len;
1812 if (left < 0)
1813 return 0;
1814 /* ... fill it in... */
1815 *pos++ = WLAN_EID_SSID;
1816 *pos++ = priv->essid_len;
1817 memcpy(pos, priv->essid, priv->essid_len);
1818 pos += priv->essid_len;
1819 len += 2 + priv->essid_len;
1822 /* fill in supported rate */
1823 /* ...next IE... */
1824 left -= 2;
1825 if (left < 0)
1826 return 0;
1828 /* ... fill it in... */
1829 *pos++ = WLAN_EID_SUPP_RATES;
1830 *pos = 0;
1832 priv->active_rate = priv->rates_mask;
1833 active_rates = priv->active_rate;
1834 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1836 cck_rates = IWL_CCK_RATES_MASK & active_rates;
1837 ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
1838 priv->active_rate_basic, &left);
1839 active_rates &= ~ret_rates;
1841 ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
1842 priv->active_rate_basic, &left);
1843 active_rates &= ~ret_rates;
1845 len += 2 + *pos;
1846 pos += (*pos) + 1;
1847 if (active_rates == 0)
1848 goto fill_end;
1850 /* fill in supported extended rate */
1851 /* ...next IE... */
1852 left -= 2;
1853 if (left < 0)
1854 return 0;
1855 /* ... fill it in... */
1856 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1857 *pos = 0;
1858 iwl_supported_rate_to_ie(pos, active_rates,
1859 priv->active_rate_basic, &left);
1860 if (*pos > 0)
1861 len += 2 + *pos;
1863 fill_end:
1864 return (u16)len;
1868 * QoS support
1870 #ifdef CONFIG_IWLWIFI_QOS
1871 static int iwl_send_qos_params_command(struct iwl_priv *priv,
1872 struct iwl_qosparam_cmd *qos)
1875 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1876 sizeof(struct iwl_qosparam_cmd), qos);
1879 static void iwl_reset_qos(struct iwl_priv *priv)
1881 u16 cw_min = 15;
1882 u16 cw_max = 1023;
1883 u8 aifs = 2;
1884 u8 is_legacy = 0;
1885 unsigned long flags;
1886 int i;
1888 spin_lock_irqsave(&priv->lock, flags);
1889 priv->qos_data.qos_active = 0;
1891 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1892 if (priv->qos_data.qos_enable)
1893 priv->qos_data.qos_active = 1;
1894 if (!(priv->active_rate & 0xfff0)) {
1895 cw_min = 31;
1896 is_legacy = 1;
1898 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1899 if (priv->qos_data.qos_enable)
1900 priv->qos_data.qos_active = 1;
1901 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1902 cw_min = 31;
1903 is_legacy = 1;
1906 if (priv->qos_data.qos_active)
1907 aifs = 3;
1909 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1910 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1911 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1912 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1913 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1915 if (priv->qos_data.qos_active) {
1916 i = 1;
1917 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1918 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1919 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1920 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1921 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1923 i = 2;
1924 priv->qos_data.def_qos_parm.ac[i].cw_min =
1925 cpu_to_le16((cw_min + 1) / 2 - 1);
1926 priv->qos_data.def_qos_parm.ac[i].cw_max =
1927 cpu_to_le16(cw_max);
1928 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1929 if (is_legacy)
1930 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1931 cpu_to_le16(6016);
1932 else
1933 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1934 cpu_to_le16(3008);
1935 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1937 i = 3;
1938 priv->qos_data.def_qos_parm.ac[i].cw_min =
1939 cpu_to_le16((cw_min + 1) / 4 - 1);
1940 priv->qos_data.def_qos_parm.ac[i].cw_max =
1941 cpu_to_le16((cw_max + 1) / 2 - 1);
1942 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1943 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1944 if (is_legacy)
1945 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1946 cpu_to_le16(3264);
1947 else
1948 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1949 cpu_to_le16(1504);
1950 } else {
1951 for (i = 1; i < 4; i++) {
1952 priv->qos_data.def_qos_parm.ac[i].cw_min =
1953 cpu_to_le16(cw_min);
1954 priv->qos_data.def_qos_parm.ac[i].cw_max =
1955 cpu_to_le16(cw_max);
1956 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1957 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1958 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1961 IWL_DEBUG_QOS("set QoS to default \n");
1963 spin_unlock_irqrestore(&priv->lock, flags);
1966 static void iwl_activate_qos(struct iwl_priv *priv, u8 force)
1968 unsigned long flags;
1970 if (priv == NULL)
1971 return;
1973 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1974 return;
1976 if (!priv->qos_data.qos_enable)
1977 return;
1979 spin_lock_irqsave(&priv->lock, flags);
1980 priv->qos_data.def_qos_parm.qos_flags = 0;
1982 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1983 !priv->qos_data.qos_cap.q_AP.txop_request)
1984 priv->qos_data.def_qos_parm.qos_flags |=
1985 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1987 if (priv->qos_data.qos_active)
1988 priv->qos_data.def_qos_parm.qos_flags |=
1989 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1991 spin_unlock_irqrestore(&priv->lock, flags);
1993 if (force || iwl_is_associated(priv)) {
1994 IWL_DEBUG_QOS("send QoS cmd with Qos active %d \n",
1995 priv->qos_data.qos_active);
1997 iwl_send_qos_params_command(priv,
1998 &(priv->qos_data.def_qos_parm));
2002 #endif /* CONFIG_IWLWIFI_QOS */
2004 * Power management (not Tx power!) functions
2006 #define MSEC_TO_USEC 1024
2008 #define NOSLP __constant_cpu_to_le32(0)
2009 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
2010 #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2011 #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2012 __constant_cpu_to_le32(X1), \
2013 __constant_cpu_to_le32(X2), \
2014 __constant_cpu_to_le32(X3), \
2015 __constant_cpu_to_le32(X4)}
2018 /* default power management (not Tx power) table values */
2019 /* for tim 0-10 */
2020 static struct iwl_power_vec_entry range_0[IWL_POWER_AC] = {
2021 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2022 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2023 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2024 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2025 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2026 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2029 /* for tim > 10 */
2030 static struct iwl_power_vec_entry range_1[IWL_POWER_AC] = {
2031 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2032 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2033 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2034 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2035 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2036 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2037 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2038 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2039 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2040 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2043 int iwl_power_init_handle(struct iwl_priv *priv)
2045 int rc = 0, i;
2046 struct iwl_power_mgr *pow_data;
2047 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
2048 u16 pci_pm;
2050 IWL_DEBUG_POWER("Initialize power \n");
2052 pow_data = &(priv->power_data);
2054 memset(pow_data, 0, sizeof(*pow_data));
2056 pow_data->active_index = IWL_POWER_RANGE_0;
2057 pow_data->dtim_val = 0xffff;
2059 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2060 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2062 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2063 if (rc != 0)
2064 return 0;
2065 else {
2066 struct iwl_powertable_cmd *cmd;
2068 IWL_DEBUG_POWER("adjust power command flags\n");
2070 for (i = 0; i < IWL_POWER_AC; i++) {
2071 cmd = &pow_data->pwr_range_0[i].cmd;
2073 if (pci_pm & 0x1)
2074 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2075 else
2076 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2079 return rc;
2082 static int iwl_update_power_cmd(struct iwl_priv *priv,
2083 struct iwl_powertable_cmd *cmd, u32 mode)
2085 int rc = 0, i;
2086 u8 skip;
2087 u32 max_sleep = 0;
2088 struct iwl_power_vec_entry *range;
2089 u8 period = 0;
2090 struct iwl_power_mgr *pow_data;
2092 if (mode > IWL_POWER_INDEX_5) {
2093 IWL_DEBUG_POWER("Error invalid power mode \n");
2094 return -1;
2096 pow_data = &(priv->power_data);
2098 if (pow_data->active_index == IWL_POWER_RANGE_0)
2099 range = &pow_data->pwr_range_0[0];
2100 else
2101 range = &pow_data->pwr_range_1[1];
2103 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
2105 #ifdef IWL_MAC80211_DISABLE
2106 if (priv->assoc_network != NULL) {
2107 unsigned long flags;
2109 period = priv->assoc_network->tim.tim_period;
2111 #endif /*IWL_MAC80211_DISABLE */
2112 skip = range[mode].no_dtim;
2114 if (period == 0) {
2115 period = 1;
2116 skip = 0;
2119 if (skip == 0) {
2120 max_sleep = period;
2121 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2122 } else {
2123 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2124 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2125 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2128 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2129 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2130 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2133 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2134 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2135 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2136 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2137 le32_to_cpu(cmd->sleep_interval[0]),
2138 le32_to_cpu(cmd->sleep_interval[1]),
2139 le32_to_cpu(cmd->sleep_interval[2]),
2140 le32_to_cpu(cmd->sleep_interval[3]),
2141 le32_to_cpu(cmd->sleep_interval[4]));
2143 return rc;
2146 static int iwl_send_power_mode(struct iwl_priv *priv, u32 mode)
2148 u32 final_mode = mode;
2149 int rc;
2150 struct iwl_powertable_cmd cmd;
2152 /* If on battery, set to 3,
2153 * if plugged into AC power, set to CAM ("continuosly aware mode"),
2154 * else user level */
2155 switch (mode) {
2156 case IWL_POWER_BATTERY:
2157 final_mode = IWL_POWER_INDEX_3;
2158 break;
2159 case IWL_POWER_AC:
2160 final_mode = IWL_POWER_MODE_CAM;
2161 break;
2162 default:
2163 final_mode = mode;
2164 break;
2167 iwl_update_power_cmd(priv, &cmd, final_mode);
2169 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2171 if (final_mode == IWL_POWER_MODE_CAM)
2172 clear_bit(STATUS_POWER_PMI, &priv->status);
2173 else
2174 set_bit(STATUS_POWER_PMI, &priv->status);
2176 return rc;
2179 int iwl_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
2181 /* Filter incoming packets to determine if they are targeted toward
2182 * this network, discarding packets coming from ourselves */
2183 switch (priv->iw_mode) {
2184 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2185 /* packets from our adapter are dropped (echo) */
2186 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2187 return 0;
2188 /* {broad,multi}cast packets to our IBSS go through */
2189 if (is_multicast_ether_addr(header->addr1))
2190 return !compare_ether_addr(header->addr3, priv->bssid);
2191 /* packets to our adapter go through */
2192 return !compare_ether_addr(header->addr1, priv->mac_addr);
2193 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2194 /* packets from our adapter are dropped (echo) */
2195 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2196 return 0;
2197 /* {broad,multi}cast packets to our BSS go through */
2198 if (is_multicast_ether_addr(header->addr1))
2199 return !compare_ether_addr(header->addr2, priv->bssid);
2200 /* packets to our adapter go through */
2201 return !compare_ether_addr(header->addr1, priv->mac_addr);
2204 return 1;
2207 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2209 const char *iwl_get_tx_fail_reason(u32 status)
2211 switch (status & TX_STATUS_MSK) {
2212 case TX_STATUS_SUCCESS:
2213 return "SUCCESS";
2214 TX_STATUS_ENTRY(SHORT_LIMIT);
2215 TX_STATUS_ENTRY(LONG_LIMIT);
2216 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2217 TX_STATUS_ENTRY(MGMNT_ABORT);
2218 TX_STATUS_ENTRY(NEXT_FRAG);
2219 TX_STATUS_ENTRY(LIFE_EXPIRE);
2220 TX_STATUS_ENTRY(DEST_PS);
2221 TX_STATUS_ENTRY(ABORTED);
2222 TX_STATUS_ENTRY(BT_RETRY);
2223 TX_STATUS_ENTRY(STA_INVALID);
2224 TX_STATUS_ENTRY(FRAG_DROPPED);
2225 TX_STATUS_ENTRY(TID_DISABLE);
2226 TX_STATUS_ENTRY(FRAME_FLUSHED);
2227 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2228 TX_STATUS_ENTRY(TX_LOCKED);
2229 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2232 return "UNKNOWN";
2236 * iwl_scan_cancel - Cancel any currently executing HW scan
2238 * NOTE: priv->mutex is not required before calling this function
2240 static int iwl_scan_cancel(struct iwl_priv *priv)
2242 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2243 clear_bit(STATUS_SCANNING, &priv->status);
2244 return 0;
2247 if (test_bit(STATUS_SCANNING, &priv->status)) {
2248 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2249 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2250 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2251 queue_work(priv->workqueue, &priv->abort_scan);
2253 } else
2254 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2256 return test_bit(STATUS_SCANNING, &priv->status);
2259 return 0;
2263 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
2264 * @ms: amount of time to wait (in milliseconds) for scan to abort
2266 * NOTE: priv->mutex must be held before calling this function
2268 static int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
2270 unsigned long now = jiffies;
2271 int ret;
2273 ret = iwl_scan_cancel(priv);
2274 if (ret && ms) {
2275 mutex_unlock(&priv->mutex);
2276 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2277 test_bit(STATUS_SCANNING, &priv->status))
2278 msleep(1);
2279 mutex_lock(&priv->mutex);
2281 return test_bit(STATUS_SCANNING, &priv->status);
2284 return ret;
2287 static void iwl_sequence_reset(struct iwl_priv *priv)
2289 /* Reset ieee stats */
2291 /* We don't reset the net_device_stats (ieee->stats) on
2292 * re-association */
2294 priv->last_seq_num = -1;
2295 priv->last_frag_num = -1;
2296 priv->last_packet_time = 0;
2298 iwl_scan_cancel(priv);
2301 #define MAX_UCODE_BEACON_INTERVAL 1024
2302 #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2304 static __le16 iwl_adjust_beacon_interval(u16 beacon_val)
2306 u16 new_val = 0;
2307 u16 beacon_factor = 0;
2309 beacon_factor =
2310 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2311 / MAX_UCODE_BEACON_INTERVAL;
2312 new_val = beacon_val / beacon_factor;
2314 return cpu_to_le16(new_val);
2317 static void iwl_setup_rxon_timing(struct iwl_priv *priv)
2319 u64 interval_tm_unit;
2320 u64 tsf, result;
2321 unsigned long flags;
2322 struct ieee80211_conf *conf = NULL;
2323 u16 beacon_int = 0;
2325 conf = ieee80211_get_hw_conf(priv->hw);
2327 spin_lock_irqsave(&priv->lock, flags);
2328 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2329 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2331 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2333 tsf = priv->timestamp1;
2334 tsf = ((tsf << 32) | priv->timestamp0);
2336 beacon_int = priv->beacon_int;
2337 spin_unlock_irqrestore(&priv->lock, flags);
2339 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2340 if (beacon_int == 0) {
2341 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2342 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2343 } else {
2344 priv->rxon_timing.beacon_interval =
2345 cpu_to_le16(beacon_int);
2346 priv->rxon_timing.beacon_interval =
2347 iwl_adjust_beacon_interval(
2348 le16_to_cpu(priv->rxon_timing.beacon_interval));
2351 priv->rxon_timing.atim_window = 0;
2352 } else {
2353 priv->rxon_timing.beacon_interval =
2354 iwl_adjust_beacon_interval(conf->beacon_int);
2355 /* TODO: we need to get atim_window from upper stack
2356 * for now we set to 0 */
2357 priv->rxon_timing.atim_window = 0;
2360 interval_tm_unit =
2361 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2362 result = do_div(tsf, interval_tm_unit);
2363 priv->rxon_timing.beacon_init_val =
2364 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2366 IWL_DEBUG_ASSOC
2367 ("beacon interval %d beacon timer %d beacon tim %d\n",
2368 le16_to_cpu(priv->rxon_timing.beacon_interval),
2369 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2370 le16_to_cpu(priv->rxon_timing.atim_window));
2373 static int iwl_scan_initiate(struct iwl_priv *priv)
2375 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2376 IWL_ERROR("APs don't scan.\n");
2377 return 0;
2380 if (!iwl_is_ready_rf(priv)) {
2381 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2382 return -EIO;
2385 if (test_bit(STATUS_SCANNING, &priv->status)) {
2386 IWL_DEBUG_SCAN("Scan already in progress.\n");
2387 return -EAGAIN;
2390 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2391 IWL_DEBUG_SCAN("Scan request while abort pending. "
2392 "Queuing.\n");
2393 return -EAGAIN;
2396 IWL_DEBUG_INFO("Starting scan...\n");
2397 priv->scan_bands = 2;
2398 set_bit(STATUS_SCANNING, &priv->status);
2399 priv->scan_start = jiffies;
2400 priv->scan_pass_start = priv->scan_start;
2402 queue_work(priv->workqueue, &priv->request_scan);
2404 return 0;
2407 static int iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
2409 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
2411 if (hw_decrypt)
2412 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2413 else
2414 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2416 return 0;
2419 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode)
2421 if (phymode == MODE_IEEE80211A) {
2422 priv->staging_rxon.flags &=
2423 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2424 | RXON_FLG_CCK_MSK);
2425 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2426 } else {
2427 /* Copied from iwl_bg_post_associate() */
2428 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2429 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2430 else
2431 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2433 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2434 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2436 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2437 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2438 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2443 * initilize rxon structure with default values fromm eeprom
2445 static void iwl_connection_init_rx_config(struct iwl_priv *priv)
2447 const struct iwl_channel_info *ch_info;
2449 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2451 switch (priv->iw_mode) {
2452 case IEEE80211_IF_TYPE_AP:
2453 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2454 break;
2456 case IEEE80211_IF_TYPE_STA:
2457 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2458 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2459 break;
2461 case IEEE80211_IF_TYPE_IBSS:
2462 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2463 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2464 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2465 RXON_FILTER_ACCEPT_GRP_MSK;
2466 break;
2468 case IEEE80211_IF_TYPE_MNTR:
2469 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2470 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2471 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2472 break;
2475 #if 0
2476 /* TODO: Figure out when short_preamble would be set and cache from
2477 * that */
2478 if (!hw_to_local(priv->hw)->short_preamble)
2479 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2480 else
2481 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2482 #endif
2484 ch_info = iwl_get_channel_info(priv, priv->phymode,
2485 le16_to_cpu(priv->staging_rxon.channel));
2487 if (!ch_info)
2488 ch_info = &priv->channel_info[0];
2491 * in some case A channels are all non IBSS
2492 * in this case force B/G channel
2494 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2495 !(is_channel_ibss(ch_info)))
2496 ch_info = &priv->channel_info[0];
2498 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2499 if (is_channel_a_band(ch_info))
2500 priv->phymode = MODE_IEEE80211A;
2501 else
2502 priv->phymode = MODE_IEEE80211G;
2504 iwl_set_flags_for_phymode(priv, priv->phymode);
2506 priv->staging_rxon.ofdm_basic_rates =
2507 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2508 priv->staging_rxon.cck_basic_rates =
2509 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2512 static int iwl_set_mode(struct iwl_priv *priv, int mode)
2514 if (!iwl_is_ready_rf(priv))
2515 return -EAGAIN;
2517 if (mode == IEEE80211_IF_TYPE_IBSS) {
2518 const struct iwl_channel_info *ch_info;
2520 ch_info = iwl_get_channel_info(priv,
2521 priv->phymode,
2522 le16_to_cpu(priv->staging_rxon.channel));
2524 if (!ch_info || !is_channel_ibss(ch_info)) {
2525 IWL_ERROR("channel %d not IBSS channel\n",
2526 le16_to_cpu(priv->staging_rxon.channel));
2527 return -EINVAL;
2531 cancel_delayed_work(&priv->scan_check);
2532 if (iwl_scan_cancel_timeout(priv, 100)) {
2533 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2534 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2535 return -EAGAIN;
2538 priv->iw_mode = mode;
2540 iwl_connection_init_rx_config(priv);
2541 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2543 iwl_clear_stations_table(priv);
2545 iwl_commit_rxon(priv);
2547 return 0;
2550 static void iwl_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
2551 struct ieee80211_tx_control *ctl,
2552 struct iwl_cmd *cmd,
2553 struct sk_buff *skb_frag,
2554 int last_frag)
2556 struct iwl_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2558 switch (keyinfo->alg) {
2559 case ALG_CCMP:
2560 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2561 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2562 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2563 break;
2565 case ALG_TKIP:
2566 #if 0
2567 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2569 if (last_frag)
2570 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2572 else
2573 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2574 #endif
2575 break;
2577 case ALG_WEP:
2578 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2579 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2581 if (keyinfo->keylen == 13)
2582 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2584 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2586 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2587 "with key %d\n", ctl->key_idx);
2588 break;
2590 default:
2591 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2592 break;
2597 * handle build REPLY_TX command notification.
2599 static void iwl_build_tx_cmd_basic(struct iwl_priv *priv,
2600 struct iwl_cmd *cmd,
2601 struct ieee80211_tx_control *ctrl,
2602 struct ieee80211_hdr *hdr,
2603 int is_unicast, u8 std_id)
2605 __le16 *qc;
2606 u16 fc = le16_to_cpu(hdr->frame_control);
2607 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2609 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2610 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2611 tx_flags |= TX_CMD_FLG_ACK_MSK;
2612 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2613 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2614 if (ieee80211_is_probe_response(fc) &&
2615 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2616 tx_flags |= TX_CMD_FLG_TSF_MSK;
2617 } else {
2618 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2619 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2622 cmd->cmd.tx.sta_id = std_id;
2623 if (ieee80211_get_morefrag(hdr))
2624 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2626 qc = ieee80211_get_qos_ctrl(hdr);
2627 if (qc) {
2628 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2629 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2630 } else
2631 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2633 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2634 tx_flags |= TX_CMD_FLG_RTS_MSK;
2635 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2636 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2637 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2638 tx_flags |= TX_CMD_FLG_CTS_MSK;
2641 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2642 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2644 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2645 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2646 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2647 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
2648 cmd->cmd.tx.timeout.pm_frame_timeout =
2649 cpu_to_le16(3);
2650 else
2651 cmd->cmd.tx.timeout.pm_frame_timeout =
2652 cpu_to_le16(2);
2653 } else
2654 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2656 cmd->cmd.tx.driver_txop = 0;
2657 cmd->cmd.tx.tx_flags = tx_flags;
2658 cmd->cmd.tx.next_frame_len = 0;
2661 static int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
2663 int sta_id;
2664 u16 fc = le16_to_cpu(hdr->frame_control);
2666 /* If this frame is broadcast or not data then use the broadcast
2667 * station id */
2668 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2669 is_multicast_ether_addr(hdr->addr1))
2670 return priv->hw_setting.bcast_sta_id;
2672 switch (priv->iw_mode) {
2674 /* If this frame is part of a BSS network (we're a station), then
2675 * we use the AP's station id */
2676 case IEEE80211_IF_TYPE_STA:
2677 return IWL_AP_ID;
2679 /* If we are an AP, then find the station, or use BCAST */
2680 case IEEE80211_IF_TYPE_AP:
2681 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2682 if (sta_id != IWL_INVALID_STATION)
2683 return sta_id;
2684 return priv->hw_setting.bcast_sta_id;
2686 /* If this frame is part of a IBSS network, then we use the
2687 * target specific station id */
2688 case IEEE80211_IF_TYPE_IBSS: {
2689 DECLARE_MAC_BUF(mac);
2691 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2692 if (sta_id != IWL_INVALID_STATION)
2693 return sta_id;
2695 sta_id = iwl_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
2697 if (sta_id != IWL_INVALID_STATION)
2698 return sta_id;
2700 IWL_DEBUG_DROP("Station %s not in station map. "
2701 "Defaulting to broadcast...\n",
2702 print_mac(mac, hdr->addr1));
2703 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2704 return priv->hw_setting.bcast_sta_id;
2706 default:
2707 IWL_WARNING("Unkown mode of operation: %d", priv->iw_mode);
2708 return priv->hw_setting.bcast_sta_id;
2713 * start REPLY_TX command process
2715 static int iwl_tx_skb(struct iwl_priv *priv,
2716 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2718 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2719 struct iwl_tfd_frame *tfd;
2720 u32 *control_flags;
2721 int txq_id = ctl->queue;
2722 struct iwl_tx_queue *txq = NULL;
2723 struct iwl_queue *q = NULL;
2724 dma_addr_t phys_addr;
2725 dma_addr_t txcmd_phys;
2726 struct iwl_cmd *out_cmd = NULL;
2727 u16 len, idx, len_org;
2728 u8 id, hdr_len, unicast;
2729 u8 sta_id;
2730 u16 seq_number = 0;
2731 u16 fc;
2732 __le16 *qc;
2733 u8 wait_write_ptr = 0;
2734 unsigned long flags;
2735 int rc;
2737 spin_lock_irqsave(&priv->lock, flags);
2738 if (iwl_is_rfkill(priv)) {
2739 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2740 goto drop_unlock;
2743 if (!priv->interface_id) {
2744 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2745 goto drop_unlock;
2748 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2749 IWL_ERROR("ERROR: No TX rate available.\n");
2750 goto drop_unlock;
2753 unicast = !is_multicast_ether_addr(hdr->addr1);
2754 id = 0;
2756 fc = le16_to_cpu(hdr->frame_control);
2758 #ifdef CONFIG_IWLWIFI_DEBUG
2759 if (ieee80211_is_auth(fc))
2760 IWL_DEBUG_TX("Sending AUTH frame\n");
2761 else if (ieee80211_is_assoc_request(fc))
2762 IWL_DEBUG_TX("Sending ASSOC frame\n");
2763 else if (ieee80211_is_reassoc_request(fc))
2764 IWL_DEBUG_TX("Sending REASSOC frame\n");
2765 #endif
2767 if (!iwl_is_associated(priv) &&
2768 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
2769 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
2770 goto drop_unlock;
2773 spin_unlock_irqrestore(&priv->lock, flags);
2775 hdr_len = ieee80211_get_hdrlen(fc);
2776 sta_id = iwl_get_sta_id(priv, hdr);
2777 if (sta_id == IWL_INVALID_STATION) {
2778 DECLARE_MAC_BUF(mac);
2780 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2781 print_mac(mac, hdr->addr1));
2782 goto drop;
2785 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2787 qc = ieee80211_get_qos_ctrl(hdr);
2788 if (qc) {
2789 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2790 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2791 IEEE80211_SCTL_SEQ;
2792 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2793 (hdr->seq_ctrl &
2794 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2795 seq_number += 0x10;
2797 txq = &priv->txq[txq_id];
2798 q = &txq->q;
2800 spin_lock_irqsave(&priv->lock, flags);
2802 tfd = &txq->bd[q->first_empty];
2803 memset(tfd, 0, sizeof(*tfd));
2804 control_flags = (u32 *) tfd;
2805 idx = get_cmd_index(q, q->first_empty, 0);
2807 memset(&(txq->txb[q->first_empty]), 0, sizeof(struct iwl_tx_info));
2808 txq->txb[q->first_empty].skb[0] = skb;
2809 memcpy(&(txq->txb[q->first_empty].status.control),
2810 ctl, sizeof(struct ieee80211_tx_control));
2811 out_cmd = &txq->cmd[idx];
2812 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2813 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2814 out_cmd->hdr.cmd = REPLY_TX;
2815 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2816 INDEX_TO_SEQ(q->first_empty)));
2817 /* copy frags header */
2818 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2820 /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
2821 len = priv->hw_setting.tx_cmd_len +
2822 sizeof(struct iwl_cmd_header) + hdr_len;
2824 len_org = len;
2825 len = (len + 3) & ~3;
2827 if (len_org != len)
2828 len_org = 1;
2829 else
2830 len_org = 0;
2832 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2833 offsetof(struct iwl_cmd, hdr);
2835 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
2837 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
2838 iwl_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
2840 /* 802.11 null functions have no payload... */
2841 len = skb->len - hdr_len;
2842 if (len) {
2843 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2844 len, PCI_DMA_TODEVICE);
2845 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
2848 /* If there is no payload, then only one TFD is used */
2849 if (!len)
2850 *control_flags = TFD_CTL_COUNT_SET(1);
2851 else
2852 *control_flags = TFD_CTL_COUNT_SET(2) |
2853 TFD_CTL_PAD_SET(U32_PAD(len));
2855 len = (u16)skb->len;
2856 out_cmd->cmd.tx.len = cpu_to_le16(len);
2858 /* TODO need this for burst mode later on */
2859 iwl_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
2861 /* set is_hcca to 0; it probably will never be implemented */
2862 iwl_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
2864 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2865 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2867 if (!ieee80211_get_morefrag(hdr)) {
2868 txq->need_update = 1;
2869 if (qc) {
2870 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2871 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2873 } else {
2874 wait_write_ptr = 1;
2875 txq->need_update = 0;
2878 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
2879 sizeof(out_cmd->cmd.tx));
2881 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
2882 ieee80211_get_hdrlen(fc));
2884 q->first_empty = iwl_queue_inc_wrap(q->first_empty, q->n_bd);
2885 rc = iwl_tx_queue_update_write_ptr(priv, txq);
2886 spin_unlock_irqrestore(&priv->lock, flags);
2888 if (rc)
2889 return rc;
2891 if ((iwl_queue_space(q) < q->high_mark)
2892 && priv->mac80211_registered) {
2893 if (wait_write_ptr) {
2894 spin_lock_irqsave(&priv->lock, flags);
2895 txq->need_update = 1;
2896 iwl_tx_queue_update_write_ptr(priv, txq);
2897 spin_unlock_irqrestore(&priv->lock, flags);
2900 ieee80211_stop_queue(priv->hw, ctl->queue);
2903 return 0;
2905 drop_unlock:
2906 spin_unlock_irqrestore(&priv->lock, flags);
2907 drop:
2908 return -1;
2911 static void iwl_set_rate(struct iwl_priv *priv)
2913 const struct ieee80211_hw_mode *hw = NULL;
2914 struct ieee80211_rate *rate;
2915 int i;
2917 hw = iwl_get_hw_mode(priv, priv->phymode);
2919 priv->active_rate = 0;
2920 priv->active_rate_basic = 0;
2922 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
2923 hw->mode == MODE_IEEE80211A ?
2924 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
2926 for (i = 0; i < hw->num_rates; i++) {
2927 rate = &(hw->rates[i]);
2928 if ((rate->val < IWL_RATE_COUNT) &&
2929 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
2930 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
2931 rate->val, iwl_rates[rate->val].plcp,
2932 (rate->flags & IEEE80211_RATE_BASIC) ?
2933 "*" : "");
2934 priv->active_rate |= (1 << rate->val);
2935 if (rate->flags & IEEE80211_RATE_BASIC)
2936 priv->active_rate_basic |= (1 << rate->val);
2937 } else
2938 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
2939 rate->val, iwl_rates[rate->val].plcp);
2942 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2943 priv->active_rate, priv->active_rate_basic);
2946 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2947 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2948 * OFDM
2950 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2951 priv->staging_rxon.cck_basic_rates =
2952 ((priv->active_rate_basic &
2953 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2954 else
2955 priv->staging_rxon.cck_basic_rates =
2956 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2958 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2959 priv->staging_rxon.ofdm_basic_rates =
2960 ((priv->active_rate_basic &
2961 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2962 IWL_FIRST_OFDM_RATE) & 0xFF;
2963 else
2964 priv->staging_rxon.ofdm_basic_rates =
2965 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2968 static void iwl_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
2970 unsigned long flags;
2972 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2973 return;
2975 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2976 disable_radio ? "OFF" : "ON");
2978 if (disable_radio) {
2979 iwl_scan_cancel(priv);
2980 /* FIXME: This is a workaround for AP */
2981 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2982 spin_lock_irqsave(&priv->lock, flags);
2983 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
2984 CSR_UCODE_SW_BIT_RFKILL);
2985 spin_unlock_irqrestore(&priv->lock, flags);
2986 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
2987 set_bit(STATUS_RF_KILL_SW, &priv->status);
2989 return;
2992 spin_lock_irqsave(&priv->lock, flags);
2993 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2995 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2996 spin_unlock_irqrestore(&priv->lock, flags);
2998 /* wake up ucode */
2999 msleep(10);
3001 spin_lock_irqsave(&priv->lock, flags);
3002 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3003 if (!iwl_grab_restricted_access(priv))
3004 iwl_release_restricted_access(priv);
3005 spin_unlock_irqrestore(&priv->lock, flags);
3007 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3008 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3009 "disabled by HW switch\n");
3010 return;
3013 queue_work(priv->workqueue, &priv->restart);
3014 return;
3017 void iwl_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
3018 u32 decrypt_res, struct ieee80211_rx_status *stats)
3020 u16 fc =
3021 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3023 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3024 return;
3026 if (!(fc & IEEE80211_FCTL_PROTECTED))
3027 return;
3029 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3030 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3031 case RX_RES_STATUS_SEC_TYPE_TKIP:
3032 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3033 RX_RES_STATUS_BAD_ICV_MIC)
3034 stats->flag |= RX_FLAG_MMIC_ERROR;
3035 case RX_RES_STATUS_SEC_TYPE_WEP:
3036 case RX_RES_STATUS_SEC_TYPE_CCMP:
3037 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3038 RX_RES_STATUS_DECRYPT_OK) {
3039 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3040 stats->flag |= RX_FLAG_DECRYPTED;
3042 break;
3044 default:
3045 break;
3049 void iwl_handle_data_packet_monitor(struct iwl_priv *priv,
3050 struct iwl_rx_mem_buffer *rxb,
3051 void *data, short len,
3052 struct ieee80211_rx_status *stats,
3053 u16 phy_flags)
3055 struct iwl_rt_rx_hdr *iwl_rt;
3057 /* First cache any information we need before we overwrite
3058 * the information provided in the skb from the hardware */
3059 s8 signal = stats->ssi;
3060 s8 noise = 0;
3061 int rate = stats->rate;
3062 u64 tsf = stats->mactime;
3063 __le16 phy_flags_hw = cpu_to_le16(phy_flags);
3065 /* We received data from the HW, so stop the watchdog */
3066 if (len > IWL_RX_BUF_SIZE - sizeof(*iwl_rt)) {
3067 IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
3068 return;
3071 /* copy the frame data to write after where the radiotap header goes */
3072 iwl_rt = (void *)rxb->skb->data;
3073 memmove(iwl_rt->payload, data, len);
3075 iwl_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
3076 iwl_rt->rt_hdr.it_pad = 0; /* always good to zero */
3078 /* total header + data */
3079 iwl_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl_rt));
3081 /* Set the size of the skb to the size of the frame */
3082 skb_put(rxb->skb, sizeof(*iwl_rt) + len);
3084 /* Big bitfield of all the fields we provide in radiotap */
3085 iwl_rt->rt_hdr.it_present =
3086 cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
3087 (1 << IEEE80211_RADIOTAP_FLAGS) |
3088 (1 << IEEE80211_RADIOTAP_RATE) |
3089 (1 << IEEE80211_RADIOTAP_CHANNEL) |
3090 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
3091 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
3092 (1 << IEEE80211_RADIOTAP_ANTENNA));
3094 /* Zero the flags, we'll add to them as we go */
3095 iwl_rt->rt_flags = 0;
3097 iwl_rt->rt_tsf = cpu_to_le64(tsf);
3099 /* Convert to dBm */
3100 iwl_rt->rt_dbmsignal = signal;
3101 iwl_rt->rt_dbmnoise = noise;
3103 /* Convert the channel frequency and set the flags */
3104 iwl_rt->rt_channelMHz = cpu_to_le16(stats->freq);
3105 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
3106 iwl_rt->rt_chbitmask =
3107 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
3108 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
3109 iwl_rt->rt_chbitmask =
3110 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
3111 else /* 802.11g */
3112 iwl_rt->rt_chbitmask =
3113 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
3115 rate = iwl_rate_index_from_plcp(rate);
3116 if (rate == -1)
3117 iwl_rt->rt_rate = 0;
3118 else
3119 iwl_rt->rt_rate = iwl_rates[rate].ieee;
3121 /* antenna number */
3122 iwl_rt->rt_antenna =
3123 le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
3125 /* set the preamble flag if we have it */
3126 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
3127 iwl_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3129 IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
3131 stats->flag |= RX_FLAG_RADIOTAP;
3132 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3133 rxb->skb = NULL;
3137 #define IWL_PACKET_RETRY_TIME HZ
3139 int is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
3141 u16 sc = le16_to_cpu(header->seq_ctrl);
3142 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3143 u16 frag = sc & IEEE80211_SCTL_FRAG;
3144 u16 *last_seq, *last_frag;
3145 unsigned long *last_time;
3147 switch (priv->iw_mode) {
3148 case IEEE80211_IF_TYPE_IBSS:{
3149 struct list_head *p;
3150 struct iwl_ibss_seq *entry = NULL;
3151 u8 *mac = header->addr2;
3152 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3154 __list_for_each(p, &priv->ibss_mac_hash[index]) {
3155 entry =
3156 list_entry(p, struct iwl_ibss_seq, list);
3157 if (!compare_ether_addr(entry->mac, mac))
3158 break;
3160 if (p == &priv->ibss_mac_hash[index]) {
3161 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3162 if (!entry) {
3163 IWL_ERROR
3164 ("Cannot malloc new mac entry\n");
3165 return 0;
3167 memcpy(entry->mac, mac, ETH_ALEN);
3168 entry->seq_num = seq;
3169 entry->frag_num = frag;
3170 entry->packet_time = jiffies;
3171 list_add(&entry->list,
3172 &priv->ibss_mac_hash[index]);
3173 return 0;
3175 last_seq = &entry->seq_num;
3176 last_frag = &entry->frag_num;
3177 last_time = &entry->packet_time;
3178 break;
3180 case IEEE80211_IF_TYPE_STA:
3181 last_seq = &priv->last_seq_num;
3182 last_frag = &priv->last_frag_num;
3183 last_time = &priv->last_packet_time;
3184 break;
3185 default:
3186 return 0;
3188 if ((*last_seq == seq) &&
3189 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3190 if (*last_frag == frag)
3191 goto drop;
3192 if (*last_frag + 1 != frag)
3193 /* out-of-order fragment */
3194 goto drop;
3195 } else
3196 *last_seq = seq;
3198 *last_frag = frag;
3199 *last_time = jiffies;
3200 return 0;
3202 drop:
3203 return 1;
3206 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3208 #include "iwl-spectrum.h"
3210 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
3211 #define BEACON_TIME_MASK_HIGH 0xFF000000
3212 #define TIME_UNIT 1024
3215 * extended beacon time format
3216 * time in usec will be changed into a 32-bit value in 8:24 format
3217 * the high 1 byte is the beacon counts
3218 * the lower 3 bytes is the time in usec within one beacon interval
3221 static u32 iwl_usecs_to_beacons(u32 usec, u32 beacon_interval)
3223 u32 quot;
3224 u32 rem;
3225 u32 interval = beacon_interval * 1024;
3227 if (!interval || !usec)
3228 return 0;
3230 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3231 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3233 return (quot << 24) + rem;
3236 /* base is usually what we get from ucode with each received frame,
3237 * the same as HW timer counter counting down
3240 static __le32 iwl_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
3242 u32 base_low = base & BEACON_TIME_MASK_LOW;
3243 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3244 u32 interval = beacon_interval * TIME_UNIT;
3245 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3246 (addon & BEACON_TIME_MASK_HIGH);
3248 if (base_low > addon_low)
3249 res += base_low - addon_low;
3250 else if (base_low < addon_low) {
3251 res += interval + base_low - addon_low;
3252 res += (1 << 24);
3253 } else
3254 res += (1 << 24);
3256 return cpu_to_le32(res);
3259 static int iwl_get_measurement(struct iwl_priv *priv,
3260 struct ieee80211_measurement_params *params,
3261 u8 type)
3263 struct iwl_spectrum_cmd spectrum;
3264 struct iwl_rx_packet *res;
3265 struct iwl_host_cmd cmd = {
3266 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3267 .data = (void *)&spectrum,
3268 .meta.flags = CMD_WANT_SKB,
3270 u32 add_time = le64_to_cpu(params->start_time);
3271 int rc;
3272 int spectrum_resp_status;
3273 int duration = le16_to_cpu(params->duration);
3275 if (iwl_is_associated(priv))
3276 add_time =
3277 iwl_usecs_to_beacons(
3278 le64_to_cpu(params->start_time) - priv->last_tsf,
3279 le16_to_cpu(priv->rxon_timing.beacon_interval));
3281 memset(&spectrum, 0, sizeof(spectrum));
3283 spectrum.channel_count = cpu_to_le16(1);
3284 spectrum.flags =
3285 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3286 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3287 cmd.len = sizeof(spectrum);
3288 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3290 if (iwl_is_associated(priv))
3291 spectrum.start_time =
3292 iwl_add_beacon_time(priv->last_beacon_time,
3293 add_time,
3294 le16_to_cpu(priv->rxon_timing.beacon_interval));
3295 else
3296 spectrum.start_time = 0;
3298 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3299 spectrum.channels[0].channel = params->channel;
3300 spectrum.channels[0].type = type;
3301 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3302 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3303 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3305 rc = iwl_send_cmd_sync(priv, &cmd);
3306 if (rc)
3307 return rc;
3309 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
3310 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3311 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3312 rc = -EIO;
3315 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3316 switch (spectrum_resp_status) {
3317 case 0: /* Command will be handled */
3318 if (res->u.spectrum.id != 0xff) {
3319 IWL_DEBUG_INFO
3320 ("Replaced existing measurement: %d\n",
3321 res->u.spectrum.id);
3322 priv->measurement_status &= ~MEASUREMENT_READY;
3324 priv->measurement_status |= MEASUREMENT_ACTIVE;
3325 rc = 0;
3326 break;
3328 case 1: /* Command will not be handled */
3329 rc = -EAGAIN;
3330 break;
3333 dev_kfree_skb_any(cmd.meta.u.skb);
3335 return rc;
3337 #endif
3339 static void iwl_txstatus_to_ieee(struct iwl_priv *priv,
3340 struct iwl_tx_info *tx_sta)
3343 tx_sta->status.ack_signal = 0;
3344 tx_sta->status.excessive_retries = 0;
3345 tx_sta->status.queue_length = 0;
3346 tx_sta->status.queue_number = 0;
3348 if (in_interrupt())
3349 ieee80211_tx_status_irqsafe(priv->hw,
3350 tx_sta->skb[0], &(tx_sta->status));
3351 else
3352 ieee80211_tx_status(priv->hw,
3353 tx_sta->skb[0], &(tx_sta->status));
3355 tx_sta->skb[0] = NULL;
3359 * iwl_tx_queue_reclaim - Reclaim Tx queue entries no more used by NIC.
3361 * When FW advances 'R' index, all entries between old and
3362 * new 'R' index need to be reclaimed. As result, some free space
3363 * forms. If there is enough free space (> low mark), wake Tx queue.
3365 int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
3367 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3368 struct iwl_queue *q = &txq->q;
3369 int nfreed = 0;
3371 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3372 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3373 "is out of range [0-%d] %d %d.\n", txq_id,
3374 index, q->n_bd, q->first_empty, q->last_used);
3375 return 0;
3378 for (index = iwl_queue_inc_wrap(index, q->n_bd);
3379 q->last_used != index;
3380 q->last_used = iwl_queue_inc_wrap(q->last_used, q->n_bd)) {
3381 if (txq_id != IWL_CMD_QUEUE_NUM) {
3382 iwl_txstatus_to_ieee(priv,
3383 &(txq->txb[txq->q.last_used]));
3384 iwl_hw_txq_free_tfd(priv, txq);
3385 } else if (nfreed > 1) {
3386 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
3387 q->first_empty, q->last_used);
3388 queue_work(priv->workqueue, &priv->restart);
3390 nfreed++;
3393 if (iwl_queue_space(q) > q->low_mark && (txq_id >= 0) &&
3394 (txq_id != IWL_CMD_QUEUE_NUM) &&
3395 priv->mac80211_registered)
3396 ieee80211_wake_queue(priv->hw, txq_id);
3399 return nfreed;
3402 static int iwl_is_tx_success(u32 status)
3404 return (status & 0xFF) == 0x1;
3407 /******************************************************************************
3409 * Generic RX handler implementations
3411 ******************************************************************************/
3412 static void iwl_rx_reply_tx(struct iwl_priv *priv,
3413 struct iwl_rx_mem_buffer *rxb)
3415 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3416 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3417 int txq_id = SEQ_TO_QUEUE(sequence);
3418 int index = SEQ_TO_INDEX(sequence);
3419 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3420 struct ieee80211_tx_status *tx_status;
3421 struct iwl_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
3422 u32 status = le32_to_cpu(tx_resp->status);
3424 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3425 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3426 "is out of range [0-%d] %d %d\n", txq_id,
3427 index, txq->q.n_bd, txq->q.first_empty,
3428 txq->q.last_used);
3429 return;
3432 tx_status = &(txq->txb[txq->q.last_used].status);
3434 tx_status->retry_count = tx_resp->failure_frame;
3435 tx_status->queue_number = status;
3436 tx_status->queue_length = tx_resp->bt_kill_count;
3437 tx_status->queue_length |= tx_resp->failure_rts;
3439 tx_status->flags =
3440 iwl_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
3442 tx_status->control.tx_rate = iwl_rate_index_from_plcp(tx_resp->rate);
3444 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) plcp rate %d retries %d\n",
3445 txq_id, iwl_get_tx_fail_reason(status), status,
3446 tx_resp->rate, tx_resp->failure_frame);
3448 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3449 if (index != -1)
3450 iwl_tx_queue_reclaim(priv, txq_id, index);
3452 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3453 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3457 static void iwl_rx_reply_alive(struct iwl_priv *priv,
3458 struct iwl_rx_mem_buffer *rxb)
3460 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3461 struct iwl_alive_resp *palive;
3462 struct delayed_work *pwork;
3464 palive = &pkt->u.alive_frame;
3466 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3467 "0x%01X 0x%01X\n",
3468 palive->is_valid, palive->ver_type,
3469 palive->ver_subtype);
3471 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3472 IWL_DEBUG_INFO("Initialization Alive received.\n");
3473 memcpy(&priv->card_alive_init,
3474 &pkt->u.alive_frame,
3475 sizeof(struct iwl_init_alive_resp));
3476 pwork = &priv->init_alive_start;
3477 } else {
3478 IWL_DEBUG_INFO("Runtime Alive received.\n");
3479 memcpy(&priv->card_alive, &pkt->u.alive_frame,
3480 sizeof(struct iwl_alive_resp));
3481 pwork = &priv->alive_start;
3482 iwl_disable_events(priv);
3485 /* We delay the ALIVE response by 5ms to
3486 * give the HW RF Kill time to activate... */
3487 if (palive->is_valid == UCODE_VALID_OK)
3488 queue_delayed_work(priv->workqueue, pwork,
3489 msecs_to_jiffies(5));
3490 else
3491 IWL_WARNING("uCode did not respond OK.\n");
3494 static void iwl_rx_reply_add_sta(struct iwl_priv *priv,
3495 struct iwl_rx_mem_buffer *rxb)
3497 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3499 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3500 return;
3503 static void iwl_rx_reply_error(struct iwl_priv *priv,
3504 struct iwl_rx_mem_buffer *rxb)
3506 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3508 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3509 "seq 0x%04X ser 0x%08X\n",
3510 le32_to_cpu(pkt->u.err_resp.error_type),
3511 get_cmd_string(pkt->u.err_resp.cmd_id),
3512 pkt->u.err_resp.cmd_id,
3513 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3514 le32_to_cpu(pkt->u.err_resp.error_info));
3517 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3519 static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
3521 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3522 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
3523 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
3524 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3525 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3526 rxon->channel = csa->channel;
3527 priv->staging_rxon.channel = csa->channel;
3530 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
3531 struct iwl_rx_mem_buffer *rxb)
3533 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3534 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3535 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
3537 if (!report->state) {
3538 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3539 "Spectrum Measure Notification: Start\n");
3540 return;
3543 memcpy(&priv->measure_report, report, sizeof(*report));
3544 priv->measurement_status |= MEASUREMENT_READY;
3545 #endif
3548 static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
3549 struct iwl_rx_mem_buffer *rxb)
3551 #ifdef CONFIG_IWLWIFI_DEBUG
3552 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3553 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
3554 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3555 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3556 #endif
3559 static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
3560 struct iwl_rx_mem_buffer *rxb)
3562 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3563 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3564 "notification for %s:\n",
3565 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
3566 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
3569 static void iwl_bg_beacon_update(struct work_struct *work)
3571 struct iwl_priv *priv =
3572 container_of(work, struct iwl_priv, beacon_update);
3573 struct sk_buff *beacon;
3575 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3576 beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
3578 if (!beacon) {
3579 IWL_ERROR("update beacon failed\n");
3580 return;
3583 mutex_lock(&priv->mutex);
3584 /* new beacon skb is allocated every time; dispose previous.*/
3585 if (priv->ibss_beacon)
3586 dev_kfree_skb(priv->ibss_beacon);
3588 priv->ibss_beacon = beacon;
3589 mutex_unlock(&priv->mutex);
3591 iwl_send_beacon_cmd(priv);
3594 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
3595 struct iwl_rx_mem_buffer *rxb)
3597 #ifdef CONFIG_IWLWIFI_DEBUG
3598 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3599 struct iwl_beacon_notif *beacon = &(pkt->u.beacon_status);
3600 u8 rate = beacon->beacon_notify_hdr.rate;
3602 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3603 "tsf %d %d rate %d\n",
3604 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3605 beacon->beacon_notify_hdr.failure_frame,
3606 le32_to_cpu(beacon->ibss_mgr_status),
3607 le32_to_cpu(beacon->high_tsf),
3608 le32_to_cpu(beacon->low_tsf), rate);
3609 #endif
3611 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3612 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3613 queue_work(priv->workqueue, &priv->beacon_update);
3616 /* Service response to REPLY_SCAN_CMD (0x80) */
3617 static void iwl_rx_reply_scan(struct iwl_priv *priv,
3618 struct iwl_rx_mem_buffer *rxb)
3620 #ifdef CONFIG_IWLWIFI_DEBUG
3621 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3622 struct iwl_scanreq_notification *notif =
3623 (struct iwl_scanreq_notification *)pkt->u.raw;
3625 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3626 #endif
3629 /* Service SCAN_START_NOTIFICATION (0x82) */
3630 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
3631 struct iwl_rx_mem_buffer *rxb)
3633 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3634 struct iwl_scanstart_notification *notif =
3635 (struct iwl_scanstart_notification *)pkt->u.raw;
3636 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3637 IWL_DEBUG_SCAN("Scan start: "
3638 "%d [802.11%s] "
3639 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3640 notif->channel,
3641 notif->band ? "bg" : "a",
3642 notif->tsf_high,
3643 notif->tsf_low, notif->status, notif->beacon_timer);
3646 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
3647 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
3648 struct iwl_rx_mem_buffer *rxb)
3650 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3651 struct iwl_scanresults_notification *notif =
3652 (struct iwl_scanresults_notification *)pkt->u.raw;
3654 IWL_DEBUG_SCAN("Scan ch.res: "
3655 "%d [802.11%s] "
3656 "(TSF: 0x%08X:%08X) - %d "
3657 "elapsed=%lu usec (%dms since last)\n",
3658 notif->channel,
3659 notif->band ? "bg" : "a",
3660 le32_to_cpu(notif->tsf_high),
3661 le32_to_cpu(notif->tsf_low),
3662 le32_to_cpu(notif->statistics[0]),
3663 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3664 jiffies_to_msecs(elapsed_jiffies
3665 (priv->last_scan_jiffies, jiffies)));
3667 priv->last_scan_jiffies = jiffies;
3670 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
3671 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
3672 struct iwl_rx_mem_buffer *rxb)
3674 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3675 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
3677 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3678 scan_notif->scanned_channels,
3679 scan_notif->tsf_low,
3680 scan_notif->tsf_high, scan_notif->status);
3682 /* The HW is no longer scanning */
3683 clear_bit(STATUS_SCAN_HW, &priv->status);
3685 /* The scan completion notification came in, so kill that timer... */
3686 cancel_delayed_work(&priv->scan_check);
3688 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3689 (priv->scan_bands == 2) ? "2.4" : "5.2",
3690 jiffies_to_msecs(elapsed_jiffies
3691 (priv->scan_pass_start, jiffies)));
3693 /* Remove this scanned band from the list
3694 * of pending bands to scan */
3695 priv->scan_bands--;
3697 /* If a request to abort was given, or the scan did not succeed
3698 * then we reset the scan state machine and terminate,
3699 * re-queuing another scan if one has been requested */
3700 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3701 IWL_DEBUG_INFO("Aborted scan completed.\n");
3702 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3703 } else {
3704 /* If there are more bands on this scan pass reschedule */
3705 if (priv->scan_bands > 0)
3706 goto reschedule;
3709 priv->last_scan_jiffies = jiffies;
3710 IWL_DEBUG_INFO("Setting scan to off\n");
3712 clear_bit(STATUS_SCANNING, &priv->status);
3714 IWL_DEBUG_INFO("Scan took %dms\n",
3715 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3717 queue_work(priv->workqueue, &priv->scan_completed);
3719 return;
3721 reschedule:
3722 priv->scan_pass_start = jiffies;
3723 queue_work(priv->workqueue, &priv->request_scan);
3726 /* Handle notification from uCode that card's power state is changing
3727 * due to software, hardware, or critical temperature RFKILL */
3728 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
3729 struct iwl_rx_mem_buffer *rxb)
3731 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3732 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3733 unsigned long status = priv->status;
3735 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3736 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3737 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3739 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
3740 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3742 if (flags & HW_CARD_DISABLED)
3743 set_bit(STATUS_RF_KILL_HW, &priv->status);
3744 else
3745 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3748 if (flags & SW_CARD_DISABLED)
3749 set_bit(STATUS_RF_KILL_SW, &priv->status);
3750 else
3751 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3753 iwl_scan_cancel(priv);
3755 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3756 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3757 (test_bit(STATUS_RF_KILL_SW, &status) !=
3758 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3759 queue_work(priv->workqueue, &priv->rf_kill);
3760 else
3761 wake_up_interruptible(&priv->wait_command_queue);
3765 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
3767 * Setup the RX handlers for each of the reply types sent from the uCode
3768 * to the host.
3770 * This function chains into the hardware specific files for them to setup
3771 * any hardware specific handlers as well.
3773 static void iwl_setup_rx_handlers(struct iwl_priv *priv)
3775 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
3776 priv->rx_handlers[REPLY_ADD_STA] = iwl_rx_reply_add_sta;
3777 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
3778 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
3779 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
3780 iwl_rx_spectrum_measure_notif;
3781 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
3782 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
3783 iwl_rx_pm_debug_statistics_notif;
3784 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
3786 /* NOTE: iwl_rx_statistics is different based on whether
3787 * the build is for the 3945 or the 4965. See the
3788 * corresponding implementation in iwl-XXXX.c
3790 * The same handler is used for both the REPLY to a
3791 * discrete statistics request from the host as well as
3792 * for the periodic statistics notification from the uCode
3794 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_hw_rx_statistics;
3795 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_hw_rx_statistics;
3797 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
3798 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
3799 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
3800 iwl_rx_scan_results_notif;
3801 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
3802 iwl_rx_scan_complete_notif;
3803 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
3804 priv->rx_handlers[REPLY_TX] = iwl_rx_reply_tx;
3806 /* Setup hardware specific Rx handlers */
3807 iwl_hw_rx_handler_setup(priv);
3811 * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
3812 * @rxb: Rx buffer to reclaim
3814 * If an Rx buffer has an async callback associated with it the callback
3815 * will be executed. The attached skb (if present) will only be freed
3816 * if the callback returns 1
3818 static void iwl_tx_cmd_complete(struct iwl_priv *priv,
3819 struct iwl_rx_mem_buffer *rxb)
3821 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3822 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3823 int txq_id = SEQ_TO_QUEUE(sequence);
3824 int index = SEQ_TO_INDEX(sequence);
3825 int huge = sequence & SEQ_HUGE_FRAME;
3826 int cmd_index;
3827 struct iwl_cmd *cmd;
3829 /* If a Tx command is being handled and it isn't in the actual
3830 * command queue then there a command routing bug has been introduced
3831 * in the queue management code. */
3832 if (txq_id != IWL_CMD_QUEUE_NUM)
3833 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3834 txq_id, pkt->hdr.cmd);
3835 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3837 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3838 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3840 /* Input error checking is done when commands are added to queue. */
3841 if (cmd->meta.flags & CMD_WANT_SKB) {
3842 cmd->meta.source->u.skb = rxb->skb;
3843 rxb->skb = NULL;
3844 } else if (cmd->meta.u.callback &&
3845 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3846 rxb->skb = NULL;
3848 iwl_tx_queue_reclaim(priv, txq_id, index);
3850 if (!(cmd->meta.flags & CMD_ASYNC)) {
3851 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3852 wake_up_interruptible(&priv->wait_command_queue);
3856 /************************** RX-FUNCTIONS ****************************/
3858 * Rx theory of operation
3860 * The host allocates 32 DMA target addresses and passes the host address
3861 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3862 * 0 to 31
3864 * Rx Queue Indexes
3865 * The host/firmware share two index registers for managing the Rx buffers.
3867 * The READ index maps to the first position that the firmware may be writing
3868 * to -- the driver can read up to (but not including) this position and get
3869 * good data.
3870 * The READ index is managed by the firmware once the card is enabled.
3872 * The WRITE index maps to the last position the driver has read from -- the
3873 * position preceding WRITE is the last slot the firmware can place a packet.
3875 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3876 * WRITE = READ.
3878 * During initialization the host sets up the READ queue position to the first
3879 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3881 * When the firmware places a packet in a buffer it will advance the READ index
3882 * and fire the RX interrupt. The driver can then query the READ index and
3883 * process as many packets as possible, moving the WRITE index forward as it
3884 * resets the Rx queue buffers with new memory.
3886 * The management in the driver is as follows:
3887 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3888 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
3889 * to replensish the iwl->rxq->rx_free.
3890 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
3891 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3892 * 'processed' and 'read' driver indexes as well)
3893 * + A received packet is processed and handed to the kernel network stack,
3894 * detached from the iwl->rxq. The driver 'processed' index is updated.
3895 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3896 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3897 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3898 * were enough free buffers and RX_STALLED is set it is cleared.
3901 * Driver sequence:
3903 * iwl_rx_queue_alloc() Allocates rx_free
3904 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
3905 * iwl_rx_queue_restock
3906 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
3907 * queue, updates firmware pointers, and updates
3908 * the WRITE index. If insufficient rx_free buffers
3909 * are available, schedules iwl_rx_replenish
3911 * -- enable interrupts --
3912 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
3913 * READ INDEX, detaching the SKB from the pool.
3914 * Moves the packet buffer from queue to rx_used.
3915 * Calls iwl_rx_queue_restock to refill any empty
3916 * slots.
3917 * ...
3922 * iwl_rx_queue_space - Return number of free slots available in queue.
3924 static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
3926 int s = q->read - q->write;
3927 if (s <= 0)
3928 s += RX_QUEUE_SIZE;
3929 /* keep some buffer to not confuse full and empty queue */
3930 s -= 2;
3931 if (s < 0)
3932 s = 0;
3933 return s;
3937 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
3939 * NOTE: This function has 3945 and 4965 specific code sections
3940 * but is declared in base due to the majority of the
3941 * implementation being the same (only a numeric constant is
3942 * different)
3945 int iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
3947 u32 reg = 0;
3948 int rc = 0;
3949 unsigned long flags;
3951 spin_lock_irqsave(&q->lock, flags);
3953 if (q->need_update == 0)
3954 goto exit_unlock;
3956 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3957 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
3959 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3960 iwl_set_bit(priv, CSR_GP_CNTRL,
3961 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3962 goto exit_unlock;
3965 rc = iwl_grab_restricted_access(priv);
3966 if (rc)
3967 goto exit_unlock;
3969 iwl_write_restricted(priv, FH_RSCSR_CHNL0_WPTR,
3970 q->write & ~0x7);
3971 iwl_release_restricted_access(priv);
3972 } else
3973 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
3976 q->need_update = 0;
3978 exit_unlock:
3979 spin_unlock_irqrestore(&q->lock, flags);
3980 return rc;
3984 * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer pointer.
3986 * NOTE: This function has 3945 and 4965 specific code paths in it.
3988 static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
3989 dma_addr_t dma_addr)
3991 return cpu_to_le32((u32)dma_addr);
3995 * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
3997 * If there are slots in the RX queue that need to be restocked,
3998 * and we have free pre-allocated buffers, fill the ranks as much
3999 * as we can pulling from rx_free.
4001 * This moves the 'write' index forward to catch up with 'processed', and
4002 * also updates the memory address in the firmware to reference the new
4003 * target buffer.
4005 int iwl_rx_queue_restock(struct iwl_priv *priv)
4007 struct iwl_rx_queue *rxq = &priv->rxq;
4008 struct list_head *element;
4009 struct iwl_rx_mem_buffer *rxb;
4010 unsigned long flags;
4011 int write, rc;
4013 spin_lock_irqsave(&rxq->lock, flags);
4014 write = rxq->write & ~0x7;
4015 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
4016 element = rxq->rx_free.next;
4017 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4018 list_del(element);
4019 rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->dma_addr);
4020 rxq->queue[rxq->write] = rxb;
4021 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4022 rxq->free_count--;
4024 spin_unlock_irqrestore(&rxq->lock, flags);
4025 /* If the pre-allocated buffer pool is dropping low, schedule to
4026 * refill it */
4027 if (rxq->free_count <= RX_LOW_WATERMARK)
4028 queue_work(priv->workqueue, &priv->rx_replenish);
4031 /* If we've added more space for the firmware to place data, tell it */
4032 if ((write != (rxq->write & ~0x7))
4033 || (abs(rxq->write - rxq->read) > 7)) {
4034 spin_lock_irqsave(&rxq->lock, flags);
4035 rxq->need_update = 1;
4036 spin_unlock_irqrestore(&rxq->lock, flags);
4037 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
4038 if (rc)
4039 return rc;
4042 return 0;
4046 * iwl_rx_replensih - Move all used packet from rx_used to rx_free
4048 * When moving to rx_free an SKB is allocated for the slot.
4050 * Also restock the Rx queue via iwl_rx_queue_restock.
4051 * This is called as a scheduled work item (except for during intialization)
4053 void iwl_rx_replenish(void *data)
4055 struct iwl_priv *priv = data;
4056 struct iwl_rx_queue *rxq = &priv->rxq;
4057 struct list_head *element;
4058 struct iwl_rx_mem_buffer *rxb;
4059 unsigned long flags;
4060 spin_lock_irqsave(&rxq->lock, flags);
4061 while (!list_empty(&rxq->rx_used)) {
4062 element = rxq->rx_used.next;
4063 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4064 rxb->skb =
4065 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
4066 if (!rxb->skb) {
4067 if (net_ratelimit())
4068 printk(KERN_CRIT DRV_NAME
4069 ": Can not allocate SKB buffers\n");
4070 /* We don't reschedule replenish work here -- we will
4071 * call the restock method and if it still needs
4072 * more buffers it will schedule replenish */
4073 break;
4075 priv->alloc_rxb_skb++;
4076 list_del(element);
4077 rxb->dma_addr =
4078 pci_map_single(priv->pci_dev, rxb->skb->data,
4079 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4080 list_add_tail(&rxb->list, &rxq->rx_free);
4081 rxq->free_count++;
4083 spin_unlock_irqrestore(&rxq->lock, flags);
4085 spin_lock_irqsave(&priv->lock, flags);
4086 iwl_rx_queue_restock(priv);
4087 spin_unlock_irqrestore(&priv->lock, flags);
4090 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
4091 * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
4092 * This free routine walks the list of POOL entries and if SKB is set to
4093 * non NULL it is unmapped and freed
4095 void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4097 int i;
4098 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4099 if (rxq->pool[i].skb != NULL) {
4100 pci_unmap_single(priv->pci_dev,
4101 rxq->pool[i].dma_addr,
4102 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4103 dev_kfree_skb(rxq->pool[i].skb);
4107 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4108 rxq->dma_addr);
4109 rxq->bd = NULL;
4112 int iwl_rx_queue_alloc(struct iwl_priv *priv)
4114 struct iwl_rx_queue *rxq = &priv->rxq;
4115 struct pci_dev *dev = priv->pci_dev;
4116 int i;
4118 spin_lock_init(&rxq->lock);
4119 INIT_LIST_HEAD(&rxq->rx_free);
4120 INIT_LIST_HEAD(&rxq->rx_used);
4121 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4122 if (!rxq->bd)
4123 return -ENOMEM;
4124 /* Fill the rx_used queue with _all_ of the Rx buffers */
4125 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4126 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4127 /* Set us so that we have processed and used all buffers, but have
4128 * not restocked the Rx queue with fresh buffers */
4129 rxq->read = rxq->write = 0;
4130 rxq->free_count = 0;
4131 rxq->need_update = 0;
4132 return 0;
4135 void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4137 unsigned long flags;
4138 int i;
4139 spin_lock_irqsave(&rxq->lock, flags);
4140 INIT_LIST_HEAD(&rxq->rx_free);
4141 INIT_LIST_HEAD(&rxq->rx_used);
4142 /* Fill the rx_used queue with _all_ of the Rx buffers */
4143 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4144 /* In the reset function, these buffers may have been allocated
4145 * to an SKB, so we need to unmap and free potential storage */
4146 if (rxq->pool[i].skb != NULL) {
4147 pci_unmap_single(priv->pci_dev,
4148 rxq->pool[i].dma_addr,
4149 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4150 priv->alloc_rxb_skb--;
4151 dev_kfree_skb(rxq->pool[i].skb);
4152 rxq->pool[i].skb = NULL;
4154 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4157 /* Set us so that we have processed and used all buffers, but have
4158 * not restocked the Rx queue with fresh buffers */
4159 rxq->read = rxq->write = 0;
4160 rxq->free_count = 0;
4161 spin_unlock_irqrestore(&rxq->lock, flags);
4164 /* Convert linear signal-to-noise ratio into dB */
4165 static u8 ratio2dB[100] = {
4166 /* 0 1 2 3 4 5 6 7 8 9 */
4167 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4168 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4169 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4170 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4171 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4172 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4173 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4174 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4175 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4176 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4179 /* Calculates a relative dB value from a ratio of linear
4180 * (i.e. not dB) signal levels.
4181 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
4182 int iwl_calc_db_from_ratio(int sig_ratio)
4184 /* Anything above 1000:1 just report as 60 dB */
4185 if (sig_ratio > 1000)
4186 return 60;
4188 /* Above 100:1, divide by 10 and use table,
4189 * add 20 dB to make up for divide by 10 */
4190 if (sig_ratio > 100)
4191 return (20 + (int)ratio2dB[sig_ratio/10]);
4193 /* We shouldn't see this */
4194 if (sig_ratio < 1)
4195 return 0;
4197 /* Use table for ratios 1:1 - 99:1 */
4198 return (int)ratio2dB[sig_ratio];
4201 #define PERFECT_RSSI (-20) /* dBm */
4202 #define WORST_RSSI (-95) /* dBm */
4203 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4205 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
4206 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4207 * about formulas used below. */
4208 int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
4210 int sig_qual;
4211 int degradation = PERFECT_RSSI - rssi_dbm;
4213 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4214 * as indicator; formula is (signal dbm - noise dbm).
4215 * SNR at or above 40 is a great signal (100%).
4216 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4217 * Weakest usable signal is usually 10 - 15 dB SNR. */
4218 if (noise_dbm) {
4219 if (rssi_dbm - noise_dbm >= 40)
4220 return 100;
4221 else if (rssi_dbm < noise_dbm)
4222 return 0;
4223 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4225 /* Else use just the signal level.
4226 * This formula is a least squares fit of data points collected and
4227 * compared with a reference system that had a percentage (%) display
4228 * for signal quality. */
4229 } else
4230 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4231 (15 * RSSI_RANGE + 62 * degradation)) /
4232 (RSSI_RANGE * RSSI_RANGE);
4234 if (sig_qual > 100)
4235 sig_qual = 100;
4236 else if (sig_qual < 1)
4237 sig_qual = 0;
4239 return sig_qual;
4243 * iwl_rx_handle - Main entry function for receiving responses from the uCode
4245 * Uses the priv->rx_handlers callback function array to invoke
4246 * the appropriate handlers, including command responses,
4247 * frame-received notifications, and other notifications.
4249 static void iwl_rx_handle(struct iwl_priv *priv)
4251 struct iwl_rx_mem_buffer *rxb;
4252 struct iwl_rx_packet *pkt;
4253 struct iwl_rx_queue *rxq = &priv->rxq;
4254 u32 r, i;
4255 int reclaim;
4256 unsigned long flags;
4258 r = iwl_hw_get_rx_read(priv);
4259 i = rxq->read;
4261 /* Rx interrupt, but nothing sent from uCode */
4262 if (i == r)
4263 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4265 while (i != r) {
4266 rxb = rxq->queue[i];
4268 /* If an RXB doesn't have a queue slot associated with it
4269 * then a bug has been introduced in the queue refilling
4270 * routines -- catch it here */
4271 BUG_ON(rxb == NULL);
4273 rxq->queue[i] = NULL;
4275 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4276 IWL_RX_BUF_SIZE,
4277 PCI_DMA_FROMDEVICE);
4278 pkt = (struct iwl_rx_packet *)rxb->skb->data;
4280 /* Reclaim a command buffer only if this packet is a response
4281 * to a (driver-originated) command.
4282 * If the packet (e.g. Rx frame) originated from uCode,
4283 * there is no command buffer to reclaim.
4284 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4285 * but apparently a few don't get set; catch them here. */
4286 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4287 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4288 (pkt->hdr.cmd != REPLY_TX);
4290 /* Based on type of command response or notification,
4291 * handle those that need handling via function in
4292 * rx_handlers table. See iwl_setup_rx_handlers() */
4293 if (priv->rx_handlers[pkt->hdr.cmd]) {
4294 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4295 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4296 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4297 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4298 } else {
4299 /* No handling needed */
4300 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4301 "r %d i %d No handler needed for %s, 0x%02x\n",
4302 r, i, get_cmd_string(pkt->hdr.cmd),
4303 pkt->hdr.cmd);
4306 if (reclaim) {
4307 /* Invoke any callbacks, transfer the skb to caller,
4308 * and fire off the (possibly) blocking iwl_send_cmd()
4309 * as we reclaim the driver command queue */
4310 if (rxb && rxb->skb)
4311 iwl_tx_cmd_complete(priv, rxb);
4312 else
4313 IWL_WARNING("Claim null rxb?\n");
4316 /* For now we just don't re-use anything. We can tweak this
4317 * later to try and re-use notification packets and SKBs that
4318 * fail to Rx correctly */
4319 if (rxb->skb != NULL) {
4320 priv->alloc_rxb_skb--;
4321 dev_kfree_skb_any(rxb->skb);
4322 rxb->skb = NULL;
4325 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4326 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4327 spin_lock_irqsave(&rxq->lock, flags);
4328 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4329 spin_unlock_irqrestore(&rxq->lock, flags);
4330 i = (i + 1) & RX_QUEUE_MASK;
4333 /* Backtrack one entry */
4334 priv->rxq.read = i;
4335 iwl_rx_queue_restock(priv);
4338 int iwl_tx_queue_update_write_ptr(struct iwl_priv *priv,
4339 struct iwl_tx_queue *txq)
4341 u32 reg = 0;
4342 int rc = 0;
4343 int txq_id = txq->q.id;
4345 if (txq->need_update == 0)
4346 return rc;
4348 /* if we're trying to save power */
4349 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4350 /* wake up nic if it's powered down ...
4351 * uCode will wake up, and interrupt us again, so next
4352 * time we'll skip this part. */
4353 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4355 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4356 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
4357 iwl_set_bit(priv, CSR_GP_CNTRL,
4358 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4359 return rc;
4362 /* restore this queue's parameters in nic hardware. */
4363 rc = iwl_grab_restricted_access(priv);
4364 if (rc)
4365 return rc;
4366 iwl_write_restricted(priv, HBUS_TARG_WRPTR,
4367 txq->q.first_empty | (txq_id << 8));
4368 iwl_release_restricted_access(priv);
4370 /* else not in power-save mode, uCode will never sleep when we're
4371 * trying to tx (during RFKILL, we're not trying to tx). */
4372 } else
4373 iwl_write32(priv, HBUS_TARG_WRPTR,
4374 txq->q.first_empty | (txq_id << 8));
4376 txq->need_update = 0;
4378 return rc;
4381 #ifdef CONFIG_IWLWIFI_DEBUG
4382 static void iwl_print_rx_config_cmd(struct iwl_rxon_cmd *rxon)
4384 DECLARE_MAC_BUF(mac);
4386 IWL_DEBUG_RADIO("RX CONFIG:\n");
4387 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
4388 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4389 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4390 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4391 le32_to_cpu(rxon->filter_flags));
4392 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4393 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4394 rxon->ofdm_basic_rates);
4395 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
4396 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4397 print_mac(mac, rxon->node_addr));
4398 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4399 print_mac(mac, rxon->bssid_addr));
4400 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4402 #endif
4404 static void iwl_enable_interrupts(struct iwl_priv *priv)
4406 IWL_DEBUG_ISR("Enabling interrupts\n");
4407 set_bit(STATUS_INT_ENABLED, &priv->status);
4408 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
4411 static inline void iwl_disable_interrupts(struct iwl_priv *priv)
4413 clear_bit(STATUS_INT_ENABLED, &priv->status);
4415 /* disable interrupts from uCode/NIC to host */
4416 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
4418 /* acknowledge/clear/reset any interrupts still pending
4419 * from uCode or flow handler (Rx/Tx DMA) */
4420 iwl_write32(priv, CSR_INT, 0xffffffff);
4421 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
4422 IWL_DEBUG_ISR("Disabled interrupts\n");
4425 static const char *desc_lookup(int i)
4427 switch (i) {
4428 case 1:
4429 return "FAIL";
4430 case 2:
4431 return "BAD_PARAM";
4432 case 3:
4433 return "BAD_CHECKSUM";
4434 case 4:
4435 return "NMI_INTERRUPT";
4436 case 5:
4437 return "SYSASSERT";
4438 case 6:
4439 return "FATAL_ERROR";
4442 return "UNKNOWN";
4445 #define ERROR_START_OFFSET (1 * sizeof(u32))
4446 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
4448 static void iwl_dump_nic_error_log(struct iwl_priv *priv)
4450 u32 i;
4451 u32 desc, time, count, base, data1;
4452 u32 blink1, blink2, ilink1, ilink2;
4453 int rc;
4455 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4457 if (!iwl_hw_valid_rtc_data_addr(base)) {
4458 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4459 return;
4462 rc = iwl_grab_restricted_access(priv);
4463 if (rc) {
4464 IWL_WARNING("Can not read from adapter at this time.\n");
4465 return;
4468 count = iwl_read_restricted_mem(priv, base);
4470 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4471 IWL_ERROR("Start IWL Error Log Dump:\n");
4472 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4473 priv->status, priv->config, count);
4476 IWL_ERROR("Desc Time asrtPC blink2 "
4477 "ilink1 nmiPC Line\n");
4478 for (i = ERROR_START_OFFSET;
4479 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
4480 i += ERROR_ELEM_SIZE) {
4481 desc = iwl_read_restricted_mem(priv, base + i);
4482 time =
4483 iwl_read_restricted_mem(priv, base + i + 1 * sizeof(u32));
4484 blink1 =
4485 iwl_read_restricted_mem(priv, base + i + 2 * sizeof(u32));
4486 blink2 =
4487 iwl_read_restricted_mem(priv, base + i + 3 * sizeof(u32));
4488 ilink1 =
4489 iwl_read_restricted_mem(priv, base + i + 4 * sizeof(u32));
4490 ilink2 =
4491 iwl_read_restricted_mem(priv, base + i + 5 * sizeof(u32));
4492 data1 =
4493 iwl_read_restricted_mem(priv, base + i + 6 * sizeof(u32));
4495 IWL_ERROR
4496 ("%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
4497 desc_lookup(desc), desc, time, blink1, blink2,
4498 ilink1, ilink2, data1);
4501 iwl_release_restricted_access(priv);
4505 #define EVENT_START_OFFSET (4 * sizeof(u32))
4508 * iwl_print_event_log - Dump error event log to syslog
4510 * NOTE: Must be called with iwl_grab_restricted_access() already obtained!
4512 static void iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
4513 u32 num_events, u32 mode)
4515 u32 i;
4516 u32 base; /* SRAM byte address of event log header */
4517 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4518 u32 ptr; /* SRAM byte address of log data */
4519 u32 ev, time, data; /* event log data */
4521 if (num_events == 0)
4522 return;
4524 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4526 if (mode == 0)
4527 event_size = 2 * sizeof(u32);
4528 else
4529 event_size = 3 * sizeof(u32);
4531 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4533 /* "time" is actually "data" for mode 0 (no timestamp).
4534 * place event id # at far right for easier visual parsing. */
4535 for (i = 0; i < num_events; i++) {
4536 ev = iwl_read_restricted_mem(priv, ptr);
4537 ptr += sizeof(u32);
4538 time = iwl_read_restricted_mem(priv, ptr);
4539 ptr += sizeof(u32);
4540 if (mode == 0)
4541 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4542 else {
4543 data = iwl_read_restricted_mem(priv, ptr);
4544 ptr += sizeof(u32);
4545 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4550 static void iwl_dump_nic_event_log(struct iwl_priv *priv)
4552 int rc;
4553 u32 base; /* SRAM byte address of event log header */
4554 u32 capacity; /* event log capacity in # entries */
4555 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4556 u32 num_wraps; /* # times uCode wrapped to top of log */
4557 u32 next_entry; /* index of next entry to be written by uCode */
4558 u32 size; /* # entries that we'll print */
4560 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4561 if (!iwl_hw_valid_rtc_data_addr(base)) {
4562 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4563 return;
4566 rc = iwl_grab_restricted_access(priv);
4567 if (rc) {
4568 IWL_WARNING("Can not read from adapter at this time.\n");
4569 return;
4572 /* event log header */
4573 capacity = iwl_read_restricted_mem(priv, base);
4574 mode = iwl_read_restricted_mem(priv, base + (1 * sizeof(u32)));
4575 num_wraps = iwl_read_restricted_mem(priv, base + (2 * sizeof(u32)));
4576 next_entry = iwl_read_restricted_mem(priv, base + (3 * sizeof(u32)));
4578 size = num_wraps ? capacity : next_entry;
4580 /* bail out if nothing in log */
4581 if (size == 0) {
4582 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
4583 iwl_release_restricted_access(priv);
4584 return;
4587 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
4588 size, num_wraps);
4590 /* if uCode has wrapped back to top of log, start at the oldest entry,
4591 * i.e the next one that uCode would fill. */
4592 if (num_wraps)
4593 iwl_print_event_log(priv, next_entry,
4594 capacity - next_entry, mode);
4596 /* (then/else) start at top of log */
4597 iwl_print_event_log(priv, 0, next_entry, mode);
4599 iwl_release_restricted_access(priv);
4603 * iwl_irq_handle_error - called for HW or SW error interrupt from card
4605 static void iwl_irq_handle_error(struct iwl_priv *priv)
4607 /* Set the FW error flag -- cleared on iwl_down */
4608 set_bit(STATUS_FW_ERROR, &priv->status);
4610 /* Cancel currently queued command. */
4611 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4613 #ifdef CONFIG_IWLWIFI_DEBUG
4614 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
4615 iwl_dump_nic_error_log(priv);
4616 iwl_dump_nic_event_log(priv);
4617 iwl_print_rx_config_cmd(&priv->staging_rxon);
4619 #endif
4621 wake_up_interruptible(&priv->wait_command_queue);
4623 /* Keep the restart process from trying to send host
4624 * commands by clearing the INIT status bit */
4625 clear_bit(STATUS_READY, &priv->status);
4627 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4628 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4629 "Restarting adapter due to uCode error.\n");
4631 if (iwl_is_associated(priv)) {
4632 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4633 sizeof(priv->recovery_rxon));
4634 priv->error_recovering = 1;
4636 queue_work(priv->workqueue, &priv->restart);
4640 static void iwl_error_recovery(struct iwl_priv *priv)
4642 unsigned long flags;
4644 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4645 sizeof(priv->staging_rxon));
4646 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4647 iwl_commit_rxon(priv);
4649 iwl_add_station(priv, priv->bssid, 1, 0);
4651 spin_lock_irqsave(&priv->lock, flags);
4652 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4653 priv->error_recovering = 0;
4654 spin_unlock_irqrestore(&priv->lock, flags);
4657 static void iwl_irq_tasklet(struct iwl_priv *priv)
4659 u32 inta, handled = 0;
4660 u32 inta_fh;
4661 unsigned long flags;
4662 #ifdef CONFIG_IWLWIFI_DEBUG
4663 u32 inta_mask;
4664 #endif
4666 spin_lock_irqsave(&priv->lock, flags);
4668 /* Ack/clear/reset pending uCode interrupts.
4669 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4670 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
4671 inta = iwl_read32(priv, CSR_INT);
4672 iwl_write32(priv, CSR_INT, inta);
4674 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4675 * Any new interrupts that happen after this, either while we're
4676 * in this tasklet, or later, will show up in next ISR/tasklet. */
4677 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4678 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
4680 #ifdef CONFIG_IWLWIFI_DEBUG
4681 if (iwl_debug_level & IWL_DL_ISR) {
4682 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4683 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4684 inta, inta_mask, inta_fh);
4686 #endif
4688 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4689 * atomic, make sure that inta covers all the interrupts that
4690 * we've discovered, even if FH interrupt came in just after
4691 * reading CSR_INT. */
4692 if (inta_fh & CSR_FH_INT_RX_MASK)
4693 inta |= CSR_INT_BIT_FH_RX;
4694 if (inta_fh & CSR_FH_INT_TX_MASK)
4695 inta |= CSR_INT_BIT_FH_TX;
4697 /* Now service all interrupt bits discovered above. */
4698 if (inta & CSR_INT_BIT_HW_ERR) {
4699 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4701 /* Tell the device to stop sending interrupts */
4702 iwl_disable_interrupts(priv);
4704 iwl_irq_handle_error(priv);
4706 handled |= CSR_INT_BIT_HW_ERR;
4708 spin_unlock_irqrestore(&priv->lock, flags);
4710 return;
4713 #ifdef CONFIG_IWLWIFI_DEBUG
4714 if (iwl_debug_level & (IWL_DL_ISR)) {
4715 /* NIC fires this, but we don't use it, redundant with WAKEUP */
4716 if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
4717 IWL_DEBUG_ISR("Microcode started or stopped.\n");
4719 /* Alive notification via Rx interrupt will do the real work */
4720 if (inta & CSR_INT_BIT_ALIVE)
4721 IWL_DEBUG_ISR("Alive interrupt\n");
4723 #endif
4724 /* Safely ignore these bits for debug checks below */
4725 inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
4727 /* HW RF KILL switch toggled (4965 only) */
4728 if (inta & CSR_INT_BIT_RF_KILL) {
4729 int hw_rf_kill = 0;
4730 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
4731 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4732 hw_rf_kill = 1;
4734 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4735 "RF_KILL bit toggled to %s.\n",
4736 hw_rf_kill ? "disable radio":"enable radio");
4738 /* Queue restart only if RF_KILL switch was set to "kill"
4739 * when we loaded driver, and is now set to "enable".
4740 * After we're Alive, RF_KILL gets handled by
4741 * iwl_rx_card_state_notif() */
4742 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status))
4743 queue_work(priv->workqueue, &priv->restart);
4745 handled |= CSR_INT_BIT_RF_KILL;
4748 /* Chip got too hot and stopped itself (4965 only) */
4749 if (inta & CSR_INT_BIT_CT_KILL) {
4750 IWL_ERROR("Microcode CT kill error detected.\n");
4751 handled |= CSR_INT_BIT_CT_KILL;
4754 /* Error detected by uCode */
4755 if (inta & CSR_INT_BIT_SW_ERR) {
4756 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4757 inta);
4758 iwl_irq_handle_error(priv);
4759 handled |= CSR_INT_BIT_SW_ERR;
4762 /* uCode wakes up after power-down sleep */
4763 if (inta & CSR_INT_BIT_WAKEUP) {
4764 IWL_DEBUG_ISR("Wakeup interrupt\n");
4765 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
4766 iwl_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4767 iwl_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4768 iwl_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4769 iwl_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4770 iwl_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4771 iwl_tx_queue_update_write_ptr(priv, &priv->txq[5]);
4773 handled |= CSR_INT_BIT_WAKEUP;
4776 /* All uCode command responses, including Tx command responses,
4777 * Rx "responses" (frame-received notification), and other
4778 * notifications from uCode come through here*/
4779 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
4780 iwl_rx_handle(priv);
4781 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4784 if (inta & CSR_INT_BIT_FH_TX) {
4785 IWL_DEBUG_ISR("Tx interrupt\n");
4787 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
4788 if (!iwl_grab_restricted_access(priv)) {
4789 iwl_write_restricted(priv,
4790 FH_TCSR_CREDIT
4791 (ALM_FH_SRVC_CHNL), 0x0);
4792 iwl_release_restricted_access(priv);
4794 handled |= CSR_INT_BIT_FH_TX;
4797 if (inta & ~handled)
4798 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4800 if (inta & ~CSR_INI_SET_MASK) {
4801 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4802 inta & ~CSR_INI_SET_MASK);
4803 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4806 /* Re-enable all interrupts */
4807 iwl_enable_interrupts(priv);
4809 #ifdef CONFIG_IWLWIFI_DEBUG
4810 if (iwl_debug_level & (IWL_DL_ISR)) {
4811 inta = iwl_read32(priv, CSR_INT);
4812 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4813 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4814 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4815 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4817 #endif
4818 spin_unlock_irqrestore(&priv->lock, flags);
4821 static irqreturn_t iwl_isr(int irq, void *data)
4823 struct iwl_priv *priv = data;
4824 u32 inta, inta_mask;
4825 u32 inta_fh;
4826 if (!priv)
4827 return IRQ_NONE;
4829 spin_lock(&priv->lock);
4831 /* Disable (but don't clear!) interrupts here to avoid
4832 * back-to-back ISRs and sporadic interrupts from our NIC.
4833 * If we have something to service, the tasklet will re-enable ints.
4834 * If we *don't* have something, we'll re-enable before leaving here. */
4835 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4836 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
4838 /* Discover which interrupts are active/pending */
4839 inta = iwl_read32(priv, CSR_INT);
4840 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4842 /* Ignore interrupt if there's nothing in NIC to service.
4843 * This may be due to IRQ shared with another device,
4844 * or due to sporadic interrupts thrown from our NIC. */
4845 if (!inta && !inta_fh) {
4846 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4847 goto none;
4850 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4851 /* Hardware disappeared */
4852 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
4853 goto none;
4856 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4857 inta, inta_mask, inta_fh);
4859 /* iwl_irq_tasklet() will service interrupts and re-enable them */
4860 tasklet_schedule(&priv->irq_tasklet);
4861 spin_unlock(&priv->lock);
4863 return IRQ_HANDLED;
4865 none:
4866 /* re-enable interrupts here since we don't have anything to service. */
4867 iwl_enable_interrupts(priv);
4868 spin_unlock(&priv->lock);
4869 return IRQ_NONE;
4872 /************************** EEPROM BANDS ****************************
4874 * The iwl_eeprom_band definitions below provide the mapping from the
4875 * EEPROM contents to the specific channel number supported for each
4876 * band.
4878 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
4879 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4880 * The specific geography and calibration information for that channel
4881 * is contained in the eeprom map itself.
4883 * During init, we copy the eeprom information and channel map
4884 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4886 * channel_map_24/52 provides the index in the channel_info array for a
4887 * given channel. We have to have two separate maps as there is channel
4888 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4889 * band_2
4891 * A value of 0xff stored in the channel_map indicates that the channel
4892 * is not supported by the hardware at all.
4894 * A value of 0xfe in the channel_map indicates that the channel is not
4895 * valid for Tx with the current hardware. This means that
4896 * while the system can tune and receive on a given channel, it may not
4897 * be able to associate or transmit any frames on that
4898 * channel. There is no corresponding channel information for that
4899 * entry.
4901 *********************************************************************/
4903 /* 2.4 GHz */
4904 static const u8 iwl_eeprom_band_1[14] = {
4905 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4908 /* 5.2 GHz bands */
4909 static const u8 iwl_eeprom_band_2[] = {
4910 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4913 static const u8 iwl_eeprom_band_3[] = { /* 5205-5320MHz */
4914 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4917 static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
4918 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4921 static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
4922 145, 149, 153, 157, 161, 165
4925 static void iwl_init_band_reference(const struct iwl_priv *priv, int band,
4926 int *eeprom_ch_count,
4927 const struct iwl_eeprom_channel
4928 **eeprom_ch_info,
4929 const u8 **eeprom_ch_index)
4931 switch (band) {
4932 case 1: /* 2.4GHz band */
4933 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
4934 *eeprom_ch_info = priv->eeprom.band_1_channels;
4935 *eeprom_ch_index = iwl_eeprom_band_1;
4936 break;
4937 case 2: /* 5.2GHz band */
4938 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
4939 *eeprom_ch_info = priv->eeprom.band_2_channels;
4940 *eeprom_ch_index = iwl_eeprom_band_2;
4941 break;
4942 case 3: /* 5.2GHz band */
4943 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
4944 *eeprom_ch_info = priv->eeprom.band_3_channels;
4945 *eeprom_ch_index = iwl_eeprom_band_3;
4946 break;
4947 case 4: /* 5.2GHz band */
4948 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
4949 *eeprom_ch_info = priv->eeprom.band_4_channels;
4950 *eeprom_ch_index = iwl_eeprom_band_4;
4951 break;
4952 case 5: /* 5.2GHz band */
4953 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
4954 *eeprom_ch_info = priv->eeprom.band_5_channels;
4955 *eeprom_ch_index = iwl_eeprom_band_5;
4956 break;
4957 default:
4958 BUG();
4959 return;
4963 const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
4964 int phymode, u16 channel)
4966 int i;
4968 switch (phymode) {
4969 case MODE_IEEE80211A:
4970 for (i = 14; i < priv->channel_count; i++) {
4971 if (priv->channel_info[i].channel == channel)
4972 return &priv->channel_info[i];
4974 break;
4976 case MODE_IEEE80211B:
4977 case MODE_IEEE80211G:
4978 if (channel >= 1 && channel <= 14)
4979 return &priv->channel_info[channel - 1];
4980 break;
4984 return NULL;
4987 #define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4988 ? # x " " : "")
4990 static int iwl_init_channel_map(struct iwl_priv *priv)
4992 int eeprom_ch_count = 0;
4993 const u8 *eeprom_ch_index = NULL;
4994 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
4995 int band, ch;
4996 struct iwl_channel_info *ch_info;
4998 if (priv->channel_count) {
4999 IWL_DEBUG_INFO("Channel map already initialized.\n");
5000 return 0;
5003 if (priv->eeprom.version < 0x2f) {
5004 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5005 priv->eeprom.version);
5006 return -EINVAL;
5009 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5011 priv->channel_count =
5012 ARRAY_SIZE(iwl_eeprom_band_1) +
5013 ARRAY_SIZE(iwl_eeprom_band_2) +
5014 ARRAY_SIZE(iwl_eeprom_band_3) +
5015 ARRAY_SIZE(iwl_eeprom_band_4) +
5016 ARRAY_SIZE(iwl_eeprom_band_5);
5018 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5020 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
5021 priv->channel_count, GFP_KERNEL);
5022 if (!priv->channel_info) {
5023 IWL_ERROR("Could not allocate channel_info\n");
5024 priv->channel_count = 0;
5025 return -ENOMEM;
5028 ch_info = priv->channel_info;
5030 /* Loop through the 5 EEPROM bands adding them in order to the
5031 * channel map we maintain (that contains additional information than
5032 * what just in the EEPROM) */
5033 for (band = 1; band <= 5; band++) {
5035 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5036 &eeprom_ch_info, &eeprom_ch_index);
5038 /* Loop through each band adding each of the channels */
5039 for (ch = 0; ch < eeprom_ch_count; ch++) {
5040 ch_info->channel = eeprom_ch_index[ch];
5041 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5042 MODE_IEEE80211A;
5044 /* permanently store EEPROM's channel regulatory flags
5045 * and max power in channel info database. */
5046 ch_info->eeprom = eeprom_ch_info[ch];
5048 /* Copy the run-time flags so they are there even on
5049 * invalid channels */
5050 ch_info->flags = eeprom_ch_info[ch].flags;
5052 if (!(is_channel_valid(ch_info))) {
5053 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5054 "No traffic\n",
5055 ch_info->channel,
5056 ch_info->flags,
5057 is_channel_a_band(ch_info) ?
5058 "5.2" : "2.4");
5059 ch_info++;
5060 continue;
5063 /* Initialize regulatory-based run-time data */
5064 ch_info->max_power_avg = ch_info->curr_txpow =
5065 eeprom_ch_info[ch].max_power_avg;
5066 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5067 ch_info->min_power = 0;
5069 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5070 " %ddBm): Ad-Hoc %ssupported\n",
5071 ch_info->channel,
5072 is_channel_a_band(ch_info) ?
5073 "5.2" : "2.4",
5074 CHECK_AND_PRINT(IBSS),
5075 CHECK_AND_PRINT(ACTIVE),
5076 CHECK_AND_PRINT(RADAR),
5077 CHECK_AND_PRINT(WIDE),
5078 CHECK_AND_PRINT(NARROW),
5079 CHECK_AND_PRINT(DFS),
5080 eeprom_ch_info[ch].flags,
5081 eeprom_ch_info[ch].max_power_avg,
5082 ((eeprom_ch_info[ch].
5083 flags & EEPROM_CHANNEL_IBSS)
5084 && !(eeprom_ch_info[ch].
5085 flags & EEPROM_CHANNEL_RADAR))
5086 ? "" : "not ");
5088 /* Set the user_txpower_limit to the highest power
5089 * supported by any channel */
5090 if (eeprom_ch_info[ch].max_power_avg >
5091 priv->user_txpower_limit)
5092 priv->user_txpower_limit =
5093 eeprom_ch_info[ch].max_power_avg;
5095 ch_info++;
5099 if (iwl3945_txpower_set_from_eeprom(priv))
5100 return -EIO;
5102 return 0;
5105 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5106 * sending probe req. This should be set long enough to hear probe responses
5107 * from more than one AP. */
5108 #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5109 #define IWL_ACTIVE_DWELL_TIME_52 (10)
5111 /* For faster active scanning, scan will move to the next channel if fewer than
5112 * PLCP_QUIET_THRESH packets are heard on this channel within
5113 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5114 * time if it's a quiet channel (nothing responded to our probe, and there's
5115 * no other traffic).
5116 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5117 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5118 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5120 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5121 * Must be set longer than active dwell time.
5122 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5123 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5124 #define IWL_PASSIVE_DWELL_TIME_52 (10)
5125 #define IWL_PASSIVE_DWELL_BASE (100)
5126 #define IWL_CHANNEL_TUNE_TIME 5
5128 static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv, int phymode)
5130 if (phymode == MODE_IEEE80211A)
5131 return IWL_ACTIVE_DWELL_TIME_52;
5132 else
5133 return IWL_ACTIVE_DWELL_TIME_24;
5136 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, int phymode)
5138 u16 active = iwl_get_active_dwell_time(priv, phymode);
5139 u16 passive = (phymode != MODE_IEEE80211A) ?
5140 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5141 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5143 if (iwl_is_associated(priv)) {
5144 /* If we're associated, we clamp the maximum passive
5145 * dwell time to be 98% of the beacon interval (minus
5146 * 2 * channel tune time) */
5147 passive = priv->beacon_int;
5148 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5149 passive = IWL_PASSIVE_DWELL_BASE;
5150 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5153 if (passive <= active)
5154 passive = active + 1;
5156 return passive;
5159 static int iwl_get_channels_for_scan(struct iwl_priv *priv, int phymode,
5160 u8 is_active, u8 direct_mask,
5161 struct iwl_scan_channel *scan_ch)
5163 const struct ieee80211_channel *channels = NULL;
5164 const struct ieee80211_hw_mode *hw_mode;
5165 const struct iwl_channel_info *ch_info;
5166 u16 passive_dwell = 0;
5167 u16 active_dwell = 0;
5168 int added, i;
5170 hw_mode = iwl_get_hw_mode(priv, phymode);
5171 if (!hw_mode)
5172 return 0;
5174 channels = hw_mode->channels;
5176 active_dwell = iwl_get_active_dwell_time(priv, phymode);
5177 passive_dwell = iwl_get_passive_dwell_time(priv, phymode);
5179 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5180 if (channels[i].chan ==
5181 le16_to_cpu(priv->active_rxon.channel)) {
5182 if (iwl_is_associated(priv)) {
5183 IWL_DEBUG_SCAN
5184 ("Skipping current channel %d\n",
5185 le16_to_cpu(priv->active_rxon.channel));
5186 continue;
5188 } else if (priv->only_active_channel)
5189 continue;
5191 scan_ch->channel = channels[i].chan;
5193 ch_info = iwl_get_channel_info(priv, phymode, scan_ch->channel);
5194 if (!is_channel_valid(ch_info)) {
5195 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5196 scan_ch->channel);
5197 continue;
5200 if (!is_active || is_channel_passive(ch_info) ||
5201 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5202 scan_ch->type = 0; /* passive */
5203 else
5204 scan_ch->type = 1; /* active */
5206 if (scan_ch->type & 1)
5207 scan_ch->type |= (direct_mask << 1);
5209 if (is_channel_narrow(ch_info))
5210 scan_ch->type |= (1 << 7);
5212 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5213 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5215 /* Set power levels to defaults */
5216 scan_ch->tpc.dsp_atten = 110;
5217 /* scan_pwr_info->tpc.dsp_atten; */
5219 /*scan_pwr_info->tpc.tx_gain; */
5220 if (phymode == MODE_IEEE80211A)
5221 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5222 else {
5223 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5224 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
5225 * power level
5226 scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
5230 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5231 scan_ch->channel,
5232 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5233 (scan_ch->type & 1) ?
5234 active_dwell : passive_dwell);
5236 scan_ch++;
5237 added++;
5240 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5241 return added;
5244 static void iwl_reset_channel_flag(struct iwl_priv *priv)
5246 int i, j;
5247 for (i = 0; i < 3; i++) {
5248 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5249 for (j = 0; j < hw_mode->num_channels; j++)
5250 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5254 static void iwl_init_hw_rates(struct iwl_priv *priv,
5255 struct ieee80211_rate *rates)
5257 int i;
5259 for (i = 0; i < IWL_RATE_COUNT; i++) {
5260 rates[i].rate = iwl_rates[i].ieee * 5;
5261 rates[i].val = i; /* Rate scaling will work on indexes */
5262 rates[i].val2 = i;
5263 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5264 /* Only OFDM have the bits-per-symbol set */
5265 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5266 rates[i].flags |= IEEE80211_RATE_OFDM;
5267 else {
5269 * If CCK 1M then set rate flag to CCK else CCK_2
5270 * which is CCK | PREAMBLE2
5272 rates[i].flags |= (iwl_rates[i].plcp == 10) ?
5273 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5276 /* Set up which ones are basic rates... */
5277 if (IWL_BASIC_RATES_MASK & (1 << i))
5278 rates[i].flags |= IEEE80211_RATE_BASIC;
5283 * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
5285 static int iwl_init_geos(struct iwl_priv *priv)
5287 struct iwl_channel_info *ch;
5288 struct ieee80211_hw_mode *modes;
5289 struct ieee80211_channel *channels;
5290 struct ieee80211_channel *geo_ch;
5291 struct ieee80211_rate *rates;
5292 int i = 0;
5293 enum {
5294 A = 0,
5295 B = 1,
5296 G = 2,
5298 int mode_count = 3;
5300 if (priv->modes) {
5301 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5302 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5303 return 0;
5306 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5307 GFP_KERNEL);
5308 if (!modes)
5309 return -ENOMEM;
5311 channels = kzalloc(sizeof(struct ieee80211_channel) *
5312 priv->channel_count, GFP_KERNEL);
5313 if (!channels) {
5314 kfree(modes);
5315 return -ENOMEM;
5318 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5319 GFP_KERNEL);
5320 if (!rates) {
5321 kfree(modes);
5322 kfree(channels);
5323 return -ENOMEM;
5326 /* 0 = 802.11a
5327 * 1 = 802.11b
5328 * 2 = 802.11g
5331 /* 5.2GHz channels start after the 2.4GHz channels */
5332 modes[A].mode = MODE_IEEE80211A;
5333 modes[A].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
5334 modes[A].rates = rates;
5335 modes[A].num_rates = 8; /* just OFDM */
5336 modes[A].num_channels = 0;
5338 modes[B].mode = MODE_IEEE80211B;
5339 modes[B].channels = channels;
5340 modes[B].rates = &rates[8];
5341 modes[B].num_rates = 4; /* just CCK */
5342 modes[B].num_channels = 0;
5344 modes[G].mode = MODE_IEEE80211G;
5345 modes[G].channels = channels;
5346 modes[G].rates = rates;
5347 modes[G].num_rates = 12; /* OFDM & CCK */
5348 modes[G].num_channels = 0;
5350 priv->ieee_channels = channels;
5351 priv->ieee_rates = rates;
5353 iwl_init_hw_rates(priv, rates);
5355 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5356 ch = &priv->channel_info[i];
5358 if (!is_channel_valid(ch)) {
5359 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5360 "skipping.\n",
5361 ch->channel, is_channel_a_band(ch) ?
5362 "5.2" : "2.4");
5363 continue;
5366 if (is_channel_a_band(ch))
5367 geo_ch = &modes[A].channels[modes[A].num_channels++];
5368 else {
5369 geo_ch = &modes[B].channels[modes[B].num_channels++];
5370 modes[G].num_channels++;
5373 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5374 geo_ch->chan = ch->channel;
5375 geo_ch->power_level = ch->max_power_avg;
5376 geo_ch->antenna_max = 0xff;
5378 if (is_channel_valid(ch)) {
5379 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5380 if (ch->flags & EEPROM_CHANNEL_IBSS)
5381 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5383 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5384 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5386 if (ch->flags & EEPROM_CHANNEL_RADAR)
5387 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5389 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5390 priv->max_channel_txpower_limit =
5391 ch->max_power_avg;
5394 geo_ch->val = geo_ch->flag;
5397 if ((modes[A].num_channels == 0) && priv->is_abg) {
5398 printk(KERN_INFO DRV_NAME
5399 ": Incorrectly detected BG card as ABG. Please send "
5400 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5401 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5402 priv->is_abg = 0;
5405 printk(KERN_INFO DRV_NAME
5406 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5407 modes[G].num_channels, modes[A].num_channels);
5410 * NOTE: We register these in preference of order -- the
5411 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5412 * a phymode based on rates or AP capabilities but seems to
5413 * configure it purely on if the channel being configured
5414 * is supported by a mode -- and the first match is taken
5417 if (modes[G].num_channels)
5418 ieee80211_register_hwmode(priv->hw, &modes[G]);
5419 if (modes[B].num_channels)
5420 ieee80211_register_hwmode(priv->hw, &modes[B]);
5421 if (modes[A].num_channels)
5422 ieee80211_register_hwmode(priv->hw, &modes[A]);
5424 priv->modes = modes;
5425 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5427 return 0;
5430 /******************************************************************************
5432 * uCode download functions
5434 ******************************************************************************/
5436 static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
5438 if (priv->ucode_code.v_addr != NULL) {
5439 pci_free_consistent(priv->pci_dev,
5440 priv->ucode_code.len,
5441 priv->ucode_code.v_addr,
5442 priv->ucode_code.p_addr);
5443 priv->ucode_code.v_addr = NULL;
5445 if (priv->ucode_data.v_addr != NULL) {
5446 pci_free_consistent(priv->pci_dev,
5447 priv->ucode_data.len,
5448 priv->ucode_data.v_addr,
5449 priv->ucode_data.p_addr);
5450 priv->ucode_data.v_addr = NULL;
5452 if (priv->ucode_data_backup.v_addr != NULL) {
5453 pci_free_consistent(priv->pci_dev,
5454 priv->ucode_data_backup.len,
5455 priv->ucode_data_backup.v_addr,
5456 priv->ucode_data_backup.p_addr);
5457 priv->ucode_data_backup.v_addr = NULL;
5459 if (priv->ucode_init.v_addr != NULL) {
5460 pci_free_consistent(priv->pci_dev,
5461 priv->ucode_init.len,
5462 priv->ucode_init.v_addr,
5463 priv->ucode_init.p_addr);
5464 priv->ucode_init.v_addr = NULL;
5466 if (priv->ucode_init_data.v_addr != NULL) {
5467 pci_free_consistent(priv->pci_dev,
5468 priv->ucode_init_data.len,
5469 priv->ucode_init_data.v_addr,
5470 priv->ucode_init_data.p_addr);
5471 priv->ucode_init_data.v_addr = NULL;
5473 if (priv->ucode_boot.v_addr != NULL) {
5474 pci_free_consistent(priv->pci_dev,
5475 priv->ucode_boot.len,
5476 priv->ucode_boot.v_addr,
5477 priv->ucode_boot.p_addr);
5478 priv->ucode_boot.v_addr = NULL;
5483 * iwl_verify_inst_full - verify runtime uCode image in card vs. host,
5484 * looking at all data.
5486 static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 * image, u32 len)
5488 u32 val;
5489 u32 save_len = len;
5490 int rc = 0;
5491 u32 errcnt;
5493 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5495 rc = iwl_grab_restricted_access(priv);
5496 if (rc)
5497 return rc;
5499 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
5501 errcnt = 0;
5502 for (; len > 0; len -= sizeof(u32), image++) {
5503 /* read data comes through single port, auto-incr addr */
5504 /* NOTE: Use the debugless read so we don't flood kernel log
5505 * if IWL_DL_IO is set */
5506 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5507 if (val != le32_to_cpu(*image)) {
5508 IWL_ERROR("uCode INST section is invalid at "
5509 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5510 save_len - len, val, le32_to_cpu(*image));
5511 rc = -EIO;
5512 errcnt++;
5513 if (errcnt >= 20)
5514 break;
5518 iwl_release_restricted_access(priv);
5520 if (!errcnt)
5521 IWL_DEBUG_INFO
5522 ("ucode image in INSTRUCTION memory is good\n");
5524 return rc;
5529 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
5530 * using sample data 100 bytes apart. If these sample points are good,
5531 * it's a pretty good bet that everything between them is good, too.
5533 static int iwl_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
5535 u32 val;
5536 int rc = 0;
5537 u32 errcnt = 0;
5538 u32 i;
5540 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5542 rc = iwl_grab_restricted_access(priv);
5543 if (rc)
5544 return rc;
5546 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5547 /* read data comes through single port, auto-incr addr */
5548 /* NOTE: Use the debugless read so we don't flood kernel log
5549 * if IWL_DL_IO is set */
5550 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR,
5551 i + RTC_INST_LOWER_BOUND);
5552 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5553 if (val != le32_to_cpu(*image)) {
5554 #if 0 /* Enable this if you want to see details */
5555 IWL_ERROR("uCode INST section is invalid at "
5556 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5557 i, val, *image);
5558 #endif
5559 rc = -EIO;
5560 errcnt++;
5561 if (errcnt >= 3)
5562 break;
5566 iwl_release_restricted_access(priv);
5568 return rc;
5573 * iwl_verify_ucode - determine which instruction image is in SRAM,
5574 * and verify its contents
5576 static int iwl_verify_ucode(struct iwl_priv *priv)
5578 __le32 *image;
5579 u32 len;
5580 int rc = 0;
5582 /* Try bootstrap */
5583 image = (__le32 *)priv->ucode_boot.v_addr;
5584 len = priv->ucode_boot.len;
5585 rc = iwl_verify_inst_sparse(priv, image, len);
5586 if (rc == 0) {
5587 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5588 return 0;
5591 /* Try initialize */
5592 image = (__le32 *)priv->ucode_init.v_addr;
5593 len = priv->ucode_init.len;
5594 rc = iwl_verify_inst_sparse(priv, image, len);
5595 if (rc == 0) {
5596 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5597 return 0;
5600 /* Try runtime/protocol */
5601 image = (__le32 *)priv->ucode_code.v_addr;
5602 len = priv->ucode_code.len;
5603 rc = iwl_verify_inst_sparse(priv, image, len);
5604 if (rc == 0) {
5605 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5606 return 0;
5609 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5611 /* Show first several data entries in instruction SRAM.
5612 * Selection of bootstrap image is arbitrary. */
5613 image = (__le32 *)priv->ucode_boot.v_addr;
5614 len = priv->ucode_boot.len;
5615 rc = iwl_verify_inst_full(priv, image, len);
5617 return rc;
5621 /* check contents of special bootstrap uCode SRAM */
5622 static int iwl_verify_bsm(struct iwl_priv *priv)
5624 __le32 *image = priv->ucode_boot.v_addr;
5625 u32 len = priv->ucode_boot.len;
5626 u32 reg;
5627 u32 val;
5629 IWL_DEBUG_INFO("Begin verify bsm\n");
5631 /* verify BSM SRAM contents */
5632 val = iwl_read_restricted_reg(priv, BSM_WR_DWCOUNT_REG);
5633 for (reg = BSM_SRAM_LOWER_BOUND;
5634 reg < BSM_SRAM_LOWER_BOUND + len;
5635 reg += sizeof(u32), image ++) {
5636 val = iwl_read_restricted_reg(priv, reg);
5637 if (val != le32_to_cpu(*image)) {
5638 IWL_ERROR("BSM uCode verification failed at "
5639 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5640 BSM_SRAM_LOWER_BOUND,
5641 reg - BSM_SRAM_LOWER_BOUND, len,
5642 val, le32_to_cpu(*image));
5643 return -EIO;
5647 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5649 return 0;
5653 * iwl_load_bsm - Load bootstrap instructions
5655 * BSM operation:
5657 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5658 * in special SRAM that does not power down during RFKILL. When powering back
5659 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5660 * the bootstrap program into the on-board processor, and starts it.
5662 * The bootstrap program loads (via DMA) instructions and data for a new
5663 * program from host DRAM locations indicated by the host driver in the
5664 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5665 * automatically.
5667 * When initializing the NIC, the host driver points the BSM to the
5668 * "initialize" uCode image. This uCode sets up some internal data, then
5669 * notifies host via "initialize alive" that it is complete.
5671 * The host then replaces the BSM_DRAM_* pointer values to point to the
5672 * normal runtime uCode instructions and a backup uCode data cache buffer
5673 * (filled initially with starting data values for the on-board processor),
5674 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5675 * which begins normal operation.
5677 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5678 * the backup data cache in DRAM before SRAM is powered down.
5680 * When powering back up, the BSM loads the bootstrap program. This reloads
5681 * the runtime uCode instructions and the backup data cache into SRAM,
5682 * and re-launches the runtime uCode from where it left off.
5684 static int iwl_load_bsm(struct iwl_priv *priv)
5686 __le32 *image = priv->ucode_boot.v_addr;
5687 u32 len = priv->ucode_boot.len;
5688 dma_addr_t pinst;
5689 dma_addr_t pdata;
5690 u32 inst_len;
5691 u32 data_len;
5692 int rc;
5693 int i;
5694 u32 done;
5695 u32 reg_offset;
5697 IWL_DEBUG_INFO("Begin load bsm\n");
5699 /* make sure bootstrap program is no larger than BSM's SRAM size */
5700 if (len > IWL_MAX_BSM_SIZE)
5701 return -EINVAL;
5703 /* Tell bootstrap uCode where to find the "Initialize" uCode
5704 * in host DRAM ... bits 31:0 for 3945, bits 35:4 for 4965.
5705 * NOTE: iwl_initialize_alive_start() will replace these values,
5706 * after the "initialize" uCode has run, to point to
5707 * runtime/protocol instructions and backup data cache. */
5708 pinst = priv->ucode_init.p_addr;
5709 pdata = priv->ucode_init_data.p_addr;
5710 inst_len = priv->ucode_init.len;
5711 data_len = priv->ucode_init_data.len;
5713 rc = iwl_grab_restricted_access(priv);
5714 if (rc)
5715 return rc;
5717 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
5718 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5719 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5720 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
5722 /* Fill BSM memory with bootstrap instructions */
5723 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5724 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5725 reg_offset += sizeof(u32), image++)
5726 _iwl_write_restricted_reg(priv, reg_offset,
5727 le32_to_cpu(*image));
5729 rc = iwl_verify_bsm(priv);
5730 if (rc) {
5731 iwl_release_restricted_access(priv);
5732 return rc;
5735 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
5736 iwl_write_restricted_reg(priv, BSM_WR_MEM_SRC_REG, 0x0);
5737 iwl_write_restricted_reg(priv, BSM_WR_MEM_DST_REG,
5738 RTC_INST_LOWER_BOUND);
5739 iwl_write_restricted_reg(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
5741 /* Load bootstrap code into instruction SRAM now,
5742 * to prepare to load "initialize" uCode */
5743 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
5744 BSM_WR_CTRL_REG_BIT_START);
5746 /* Wait for load of bootstrap uCode to finish */
5747 for (i = 0; i < 100; i++) {
5748 done = iwl_read_restricted_reg(priv, BSM_WR_CTRL_REG);
5749 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5750 break;
5751 udelay(10);
5753 if (i < 100)
5754 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5755 else {
5756 IWL_ERROR("BSM write did not complete!\n");
5757 return -EIO;
5760 /* Enable future boot loads whenever power management unit triggers it
5761 * (e.g. when powering back up after power-save shutdown) */
5762 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
5763 BSM_WR_CTRL_REG_BIT_START_EN);
5765 iwl_release_restricted_access(priv);
5767 return 0;
5770 static void iwl_nic_start(struct iwl_priv *priv)
5772 /* Remove all resets to allow NIC to operate */
5773 iwl_write32(priv, CSR_RESET, 0);
5777 * iwl_read_ucode - Read uCode images from disk file.
5779 * Copy into buffers for card to fetch via bus-mastering
5781 static int iwl_read_ucode(struct iwl_priv *priv)
5783 struct iwl_ucode *ucode;
5784 int rc = 0;
5785 const struct firmware *ucode_raw;
5786 /* firmware file name contains uCode/driver compatibility version */
5787 const char *name = "iwlwifi-3945" IWL3945_UCODE_API ".ucode";
5788 u8 *src;
5789 size_t len;
5790 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5792 /* Ask kernel firmware_class module to get the boot firmware off disk.
5793 * request_firmware() is synchronous, file is in memory on return. */
5794 rc = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5795 if (rc < 0) {
5796 IWL_ERROR("%s firmware file req failed: Reason %d\n", name, rc);
5797 goto error;
5800 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5801 name, ucode_raw->size);
5803 /* Make sure that we got at least our header! */
5804 if (ucode_raw->size < sizeof(*ucode)) {
5805 IWL_ERROR("File size way too small!\n");
5806 rc = -EINVAL;
5807 goto err_release;
5810 /* Data from ucode file: header followed by uCode images */
5811 ucode = (void *)ucode_raw->data;
5813 ver = le32_to_cpu(ucode->ver);
5814 inst_size = le32_to_cpu(ucode->inst_size);
5815 data_size = le32_to_cpu(ucode->data_size);
5816 init_size = le32_to_cpu(ucode->init_size);
5817 init_data_size = le32_to_cpu(ucode->init_data_size);
5818 boot_size = le32_to_cpu(ucode->boot_size);
5820 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5821 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5822 inst_size);
5823 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5824 data_size);
5825 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5826 init_size);
5827 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5828 init_data_size);
5829 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5830 boot_size);
5832 /* Verify size of file vs. image size info in file's header */
5833 if (ucode_raw->size < sizeof(*ucode) +
5834 inst_size + data_size + init_size +
5835 init_data_size + boot_size) {
5837 IWL_DEBUG_INFO("uCode file size %d too small\n",
5838 (int)ucode_raw->size);
5839 rc = -EINVAL;
5840 goto err_release;
5843 /* Verify that uCode images will fit in card's SRAM */
5844 if (inst_size > IWL_MAX_INST_SIZE) {
5845 IWL_DEBUG_INFO("uCode instr len %d too large to fit in card\n",
5846 (int)inst_size);
5847 rc = -EINVAL;
5848 goto err_release;
5851 if (data_size > IWL_MAX_DATA_SIZE) {
5852 IWL_DEBUG_INFO("uCode data len %d too large to fit in card\n",
5853 (int)data_size);
5854 rc = -EINVAL;
5855 goto err_release;
5857 if (init_size > IWL_MAX_INST_SIZE) {
5858 IWL_DEBUG_INFO
5859 ("uCode init instr len %d too large to fit in card\n",
5860 (int)init_size);
5861 rc = -EINVAL;
5862 goto err_release;
5864 if (init_data_size > IWL_MAX_DATA_SIZE) {
5865 IWL_DEBUG_INFO
5866 ("uCode init data len %d too large to fit in card\n",
5867 (int)init_data_size);
5868 rc = -EINVAL;
5869 goto err_release;
5871 if (boot_size > IWL_MAX_BSM_SIZE) {
5872 IWL_DEBUG_INFO
5873 ("uCode boot instr len %d too large to fit in bsm\n",
5874 (int)boot_size);
5875 rc = -EINVAL;
5876 goto err_release;
5879 /* Allocate ucode buffers for card's bus-master loading ... */
5881 /* Runtime instructions and 2 copies of data:
5882 * 1) unmodified from disk
5883 * 2) backup cache for save/restore during power-downs */
5884 priv->ucode_code.len = inst_size;
5885 priv->ucode_code.v_addr =
5886 pci_alloc_consistent(priv->pci_dev,
5887 priv->ucode_code.len,
5888 &(priv->ucode_code.p_addr));
5890 priv->ucode_data.len = data_size;
5891 priv->ucode_data.v_addr =
5892 pci_alloc_consistent(priv->pci_dev,
5893 priv->ucode_data.len,
5894 &(priv->ucode_data.p_addr));
5896 priv->ucode_data_backup.len = data_size;
5897 priv->ucode_data_backup.v_addr =
5898 pci_alloc_consistent(priv->pci_dev,
5899 priv->ucode_data_backup.len,
5900 &(priv->ucode_data_backup.p_addr));
5903 /* Initialization instructions and data */
5904 priv->ucode_init.len = init_size;
5905 priv->ucode_init.v_addr =
5906 pci_alloc_consistent(priv->pci_dev,
5907 priv->ucode_init.len,
5908 &(priv->ucode_init.p_addr));
5910 priv->ucode_init_data.len = init_data_size;
5911 priv->ucode_init_data.v_addr =
5912 pci_alloc_consistent(priv->pci_dev,
5913 priv->ucode_init_data.len,
5914 &(priv->ucode_init_data.p_addr));
5916 /* Bootstrap (instructions only, no data) */
5917 priv->ucode_boot.len = boot_size;
5918 priv->ucode_boot.v_addr =
5919 pci_alloc_consistent(priv->pci_dev,
5920 priv->ucode_boot.len,
5921 &(priv->ucode_boot.p_addr));
5923 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
5924 !priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr ||
5925 !priv->ucode_boot.v_addr || !priv->ucode_data_backup.v_addr)
5926 goto err_pci_alloc;
5928 /* Copy images into buffers for card's bus-master reads ... */
5930 /* Runtime instructions (first block of data in file) */
5931 src = &ucode->data[0];
5932 len = priv->ucode_code.len;
5933 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %d\n",
5934 (int)len);
5935 memcpy(priv->ucode_code.v_addr, src, len);
5936 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5937 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5939 /* Runtime data (2nd block)
5940 * NOTE: Copy into backup buffer will be done in iwl_up() */
5941 src = &ucode->data[inst_size];
5942 len = priv->ucode_data.len;
5943 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %d\n",
5944 (int)len);
5945 memcpy(priv->ucode_data.v_addr, src, len);
5946 memcpy(priv->ucode_data_backup.v_addr, src, len);
5948 /* Initialization instructions (3rd block) */
5949 if (init_size) {
5950 src = &ucode->data[inst_size + data_size];
5951 len = priv->ucode_init.len;
5952 IWL_DEBUG_INFO("Copying (but not loading) init instr len %d\n",
5953 (int)len);
5954 memcpy(priv->ucode_init.v_addr, src, len);
5957 /* Initialization data (4th block) */
5958 if (init_data_size) {
5959 src = &ucode->data[inst_size + data_size + init_size];
5960 len = priv->ucode_init_data.len;
5961 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5962 (int)len);
5963 memcpy(priv->ucode_init_data.v_addr, src, len);
5966 /* Bootstrap instructions (5th block) */
5967 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5968 len = priv->ucode_boot.len;
5969 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5970 (int)len);
5971 memcpy(priv->ucode_boot.v_addr, src, len);
5973 /* We have our copies now, allow OS release its copies */
5974 release_firmware(ucode_raw);
5975 return 0;
5977 err_pci_alloc:
5978 IWL_ERROR("failed to allocate pci memory\n");
5979 rc = -ENOMEM;
5980 iwl_dealloc_ucode_pci(priv);
5982 err_release:
5983 release_firmware(ucode_raw);
5985 error:
5986 return rc;
5991 * iwl_set_ucode_ptrs - Set uCode address location
5993 * Tell initialization uCode where to find runtime uCode.
5995 * BSM registers initially contain pointers to initialization uCode.
5996 * We need to replace them to load runtime uCode inst and data,
5997 * and to save runtime data when powering down.
5999 static int iwl_set_ucode_ptrs(struct iwl_priv *priv)
6001 dma_addr_t pinst;
6002 dma_addr_t pdata;
6003 int rc = 0;
6004 unsigned long flags;
6006 /* bits 31:0 for 3945 */
6007 pinst = priv->ucode_code.p_addr;
6008 pdata = priv->ucode_data_backup.p_addr;
6010 spin_lock_irqsave(&priv->lock, flags);
6011 rc = iwl_grab_restricted_access(priv);
6012 if (rc) {
6013 spin_unlock_irqrestore(&priv->lock, flags);
6014 return rc;
6017 /* Tell bootstrap uCode where to find image to load */
6018 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
6019 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6020 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
6021 priv->ucode_data.len);
6023 /* Inst bytecount must be last to set up, bit 31 signals uCode
6024 * that all new ptr/size info is in place */
6025 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG,
6026 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6028 iwl_release_restricted_access(priv);
6030 spin_unlock_irqrestore(&priv->lock, flags);
6032 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6034 return rc;
6038 * iwl_init_alive_start - Called after REPLY_ALIVE notification receieved
6040 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6042 * The 4965 "initialize" ALIVE reply contains calibration data for:
6043 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6044 * (3945 does not contain this data).
6046 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6048 static void iwl_init_alive_start(struct iwl_priv *priv)
6050 /* Check alive response for "valid" sign from uCode */
6051 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6052 /* We had an error bringing up the hardware, so take it
6053 * all the way back down so we can try again */
6054 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6055 goto restart;
6058 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6059 * This is a paranoid check, because we would not have gotten the
6060 * "initialize" alive if code weren't properly loaded. */
6061 if (iwl_verify_ucode(priv)) {
6062 /* Runtime instruction load was bad;
6063 * take it all the way back down so we can try again */
6064 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6065 goto restart;
6068 /* Send pointers to protocol/runtime uCode image ... init code will
6069 * load and launch runtime uCode, which will send us another "Alive"
6070 * notification. */
6071 IWL_DEBUG_INFO("Initialization Alive received.\n");
6072 if (iwl_set_ucode_ptrs(priv)) {
6073 /* Runtime instruction load won't happen;
6074 * take it all the way back down so we can try again */
6075 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6076 goto restart;
6078 return;
6080 restart:
6081 queue_work(priv->workqueue, &priv->restart);
6086 * iwl_alive_start - called after REPLY_ALIVE notification received
6087 * from protocol/runtime uCode (initialization uCode's
6088 * Alive gets handled by iwl_init_alive_start()).
6090 static void iwl_alive_start(struct iwl_priv *priv)
6092 int rc = 0;
6093 int thermal_spin = 0;
6094 u32 rfkill;
6096 IWL_DEBUG_INFO("Runtime Alive received.\n");
6098 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6099 /* We had an error bringing up the hardware, so take it
6100 * all the way back down so we can try again */
6101 IWL_DEBUG_INFO("Alive failed.\n");
6102 goto restart;
6105 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6106 * This is a paranoid check, because we would not have gotten the
6107 * "runtime" alive if code weren't properly loaded. */
6108 if (iwl_verify_ucode(priv)) {
6109 /* Runtime instruction load was bad;
6110 * take it all the way back down so we can try again */
6111 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6112 goto restart;
6115 iwl_clear_stations_table(priv);
6117 rc = iwl_grab_restricted_access(priv);
6118 if (rc) {
6119 IWL_WARNING("Can not read rfkill status from adapter\n");
6120 return;
6123 rfkill = iwl_read_restricted_reg(priv, APMG_RFKILL_REG);
6124 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
6125 iwl_release_restricted_access(priv);
6127 if (rfkill & 0x1) {
6128 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6129 /* if rfkill is not on, then wait for thermal
6130 * sensor in adapter to kick in */
6131 while (iwl_hw_get_temperature(priv) == 0) {
6132 thermal_spin++;
6133 udelay(10);
6136 if (thermal_spin)
6137 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
6138 thermal_spin * 10);
6139 } else
6140 set_bit(STATUS_RF_KILL_HW, &priv->status);
6142 /* After the ALIVE response, we can process host commands */
6143 set_bit(STATUS_ALIVE, &priv->status);
6145 /* Clear out the uCode error bit if it is set */
6146 clear_bit(STATUS_FW_ERROR, &priv->status);
6148 rc = iwl_init_channel_map(priv);
6149 if (rc) {
6150 IWL_ERROR("initializing regulatory failed: %d\n", rc);
6151 return;
6154 iwl_init_geos(priv);
6156 if (iwl_is_rfkill(priv))
6157 return;
6159 if (!priv->mac80211_registered) {
6160 /* Unlock so any user space entry points can call back into
6161 * the driver without a deadlock... */
6162 mutex_unlock(&priv->mutex);
6163 iwl_rate_control_register(priv->hw);
6164 rc = ieee80211_register_hw(priv->hw);
6165 priv->hw->conf.beacon_int = 100;
6166 mutex_lock(&priv->mutex);
6168 if (rc) {
6169 IWL_ERROR("Failed to register network "
6170 "device (error %d)\n", rc);
6171 return;
6174 priv->mac80211_registered = 1;
6176 iwl_reset_channel_flag(priv);
6177 } else
6178 ieee80211_start_queues(priv->hw);
6180 priv->active_rate = priv->rates_mask;
6181 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6183 iwl_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
6185 if (iwl_is_associated(priv)) {
6186 struct iwl_rxon_cmd *active_rxon =
6187 (struct iwl_rxon_cmd *)(&priv->active_rxon);
6189 memcpy(&priv->staging_rxon, &priv->active_rxon,
6190 sizeof(priv->staging_rxon));
6191 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6192 } else {
6193 /* Initialize our rx_config data */
6194 iwl_connection_init_rx_config(priv);
6195 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6198 /* Configure BT coexistence */
6199 iwl_send_bt_config(priv);
6201 /* Configure the adapter for unassociated operation */
6202 iwl_commit_rxon(priv);
6204 /* At this point, the NIC is initialized and operational */
6205 priv->notif_missed_beacons = 0;
6206 set_bit(STATUS_READY, &priv->status);
6208 iwl3945_reg_txpower_periodic(priv);
6210 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6212 if (priv->error_recovering)
6213 iwl_error_recovery(priv);
6215 return;
6217 restart:
6218 queue_work(priv->workqueue, &priv->restart);
6221 static void iwl_cancel_deferred_work(struct iwl_priv *priv);
6223 static void __iwl_down(struct iwl_priv *priv)
6225 unsigned long flags;
6226 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6227 struct ieee80211_conf *conf = NULL;
6229 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6231 conf = ieee80211_get_hw_conf(priv->hw);
6233 if (!exit_pending)
6234 set_bit(STATUS_EXIT_PENDING, &priv->status);
6236 iwl_clear_stations_table(priv);
6238 /* Unblock any waiting calls */
6239 wake_up_interruptible_all(&priv->wait_command_queue);
6241 iwl_cancel_deferred_work(priv);
6243 /* Wipe out the EXIT_PENDING status bit if we are not actually
6244 * exiting the module */
6245 if (!exit_pending)
6246 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6248 /* stop and reset the on-board processor */
6249 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
6251 /* tell the device to stop sending interrupts */
6252 iwl_disable_interrupts(priv);
6254 if (priv->mac80211_registered)
6255 ieee80211_stop_queues(priv->hw);
6257 /* If we have not previously called iwl_init() then
6258 * clear all bits but the RF Kill and SUSPEND bits and return */
6259 if (!iwl_is_init(priv)) {
6260 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6261 STATUS_RF_KILL_HW |
6262 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6263 STATUS_RF_KILL_SW |
6264 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6265 STATUS_IN_SUSPEND;
6266 goto exit;
6269 /* ...otherwise clear out all the status bits but the RF Kill and
6270 * SUSPEND bits and continue taking the NIC down. */
6271 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6272 STATUS_RF_KILL_HW |
6273 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6274 STATUS_RF_KILL_SW |
6275 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6276 STATUS_IN_SUSPEND |
6277 test_bit(STATUS_FW_ERROR, &priv->status) <<
6278 STATUS_FW_ERROR;
6280 spin_lock_irqsave(&priv->lock, flags);
6281 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
6282 spin_unlock_irqrestore(&priv->lock, flags);
6284 iwl_hw_txq_ctx_stop(priv);
6285 iwl_hw_rxq_stop(priv);
6287 spin_lock_irqsave(&priv->lock, flags);
6288 if (!iwl_grab_restricted_access(priv)) {
6289 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
6290 APMG_CLK_VAL_DMA_CLK_RQT);
6291 iwl_release_restricted_access(priv);
6293 spin_unlock_irqrestore(&priv->lock, flags);
6295 udelay(5);
6297 iwl_hw_nic_stop_master(priv);
6298 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6299 iwl_hw_nic_reset(priv);
6301 exit:
6302 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
6304 if (priv->ibss_beacon)
6305 dev_kfree_skb(priv->ibss_beacon);
6306 priv->ibss_beacon = NULL;
6308 /* clear out any free frames */
6309 iwl_clear_free_frames(priv);
6312 static void iwl_down(struct iwl_priv *priv)
6314 mutex_lock(&priv->mutex);
6315 __iwl_down(priv);
6316 mutex_unlock(&priv->mutex);
6319 #define MAX_HW_RESTARTS 5
6321 static int __iwl_up(struct iwl_priv *priv)
6323 DECLARE_MAC_BUF(mac);
6324 int rc, i;
6326 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6327 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6328 return -EIO;
6331 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6332 IWL_WARNING("Radio disabled by SW RF kill (module "
6333 "parameter)\n");
6334 return 0;
6337 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6339 rc = iwl_hw_nic_init(priv);
6340 if (rc) {
6341 IWL_ERROR("Unable to int nic\n");
6342 return rc;
6345 /* make sure rfkill handshake bits are cleared */
6346 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6347 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
6348 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6350 /* clear (again), then enable host interrupts */
6351 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6352 iwl_enable_interrupts(priv);
6354 /* really make sure rfkill handshake bits are cleared */
6355 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6356 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6358 /* Copy original ucode data image from disk into backup cache.
6359 * This will be used to initialize the on-board processor's
6360 * data SRAM for a clean start when the runtime program first loads. */
6361 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6362 priv->ucode_data.len);
6364 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6366 iwl_clear_stations_table(priv);
6368 /* load bootstrap state machine,
6369 * load bootstrap program into processor's memory,
6370 * prepare to load the "initialize" uCode */
6371 rc = iwl_load_bsm(priv);
6373 if (rc) {
6374 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6375 continue;
6378 /* start card; "initialize" will load runtime ucode */
6379 iwl_nic_start(priv);
6381 /* MAC Address location in EEPROM same for 3945/4965 */
6382 get_eeprom_mac(priv, priv->mac_addr);
6383 IWL_DEBUG_INFO("MAC address: %s\n",
6384 print_mac(mac, priv->mac_addr));
6386 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6388 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6390 return 0;
6393 set_bit(STATUS_EXIT_PENDING, &priv->status);
6394 __iwl_down(priv);
6396 /* tried to restart and config the device for as long as our
6397 * patience could withstand */
6398 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6399 return -EIO;
6403 /*****************************************************************************
6405 * Workqueue callbacks
6407 *****************************************************************************/
6409 static void iwl_bg_init_alive_start(struct work_struct *data)
6411 struct iwl_priv *priv =
6412 container_of(data, struct iwl_priv, init_alive_start.work);
6414 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6415 return;
6417 mutex_lock(&priv->mutex);
6418 iwl_init_alive_start(priv);
6419 mutex_unlock(&priv->mutex);
6422 static void iwl_bg_alive_start(struct work_struct *data)
6424 struct iwl_priv *priv =
6425 container_of(data, struct iwl_priv, alive_start.work);
6427 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6428 return;
6430 mutex_lock(&priv->mutex);
6431 iwl_alive_start(priv);
6432 mutex_unlock(&priv->mutex);
6435 static void iwl_bg_rf_kill(struct work_struct *work)
6437 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
6439 wake_up_interruptible(&priv->wait_command_queue);
6441 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6442 return;
6444 mutex_lock(&priv->mutex);
6446 if (!iwl_is_rfkill(priv)) {
6447 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6448 "HW and/or SW RF Kill no longer active, restarting "
6449 "device\n");
6450 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6451 queue_work(priv->workqueue, &priv->restart);
6452 } else {
6454 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6455 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6456 "disabled by SW switch\n");
6457 else
6458 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6459 "Kill switch must be turned off for "
6460 "wireless networking to work.\n");
6462 mutex_unlock(&priv->mutex);
6465 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6467 static void iwl_bg_scan_check(struct work_struct *data)
6469 struct iwl_priv *priv =
6470 container_of(data, struct iwl_priv, scan_check.work);
6472 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6473 return;
6475 mutex_lock(&priv->mutex);
6476 if (test_bit(STATUS_SCANNING, &priv->status) ||
6477 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6478 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6479 "Scan completion watchdog resetting adapter (%dms)\n",
6480 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
6482 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6483 iwl_send_scan_abort(priv);
6485 mutex_unlock(&priv->mutex);
6488 static void iwl_bg_request_scan(struct work_struct *data)
6490 struct iwl_priv *priv =
6491 container_of(data, struct iwl_priv, request_scan);
6492 struct iwl_host_cmd cmd = {
6493 .id = REPLY_SCAN_CMD,
6494 .len = sizeof(struct iwl_scan_cmd),
6495 .meta.flags = CMD_SIZE_HUGE,
6497 int rc = 0;
6498 struct iwl_scan_cmd *scan;
6499 struct ieee80211_conf *conf = NULL;
6500 u8 direct_mask;
6501 int phymode;
6503 conf = ieee80211_get_hw_conf(priv->hw);
6505 mutex_lock(&priv->mutex);
6507 if (!iwl_is_ready(priv)) {
6508 IWL_WARNING("request scan called when driver not ready.\n");
6509 goto done;
6512 /* Make sure the scan wasn't cancelled before this queued work
6513 * was given the chance to run... */
6514 if (!test_bit(STATUS_SCANNING, &priv->status))
6515 goto done;
6517 /* This should never be called or scheduled if there is currently
6518 * a scan active in the hardware. */
6519 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6520 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6521 "Ignoring second request.\n");
6522 rc = -EIO;
6523 goto done;
6526 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6527 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6528 goto done;
6531 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6532 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6533 goto done;
6536 if (iwl_is_rfkill(priv)) {
6537 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6538 goto done;
6541 if (!test_bit(STATUS_READY, &priv->status)) {
6542 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6543 goto done;
6546 if (!priv->scan_bands) {
6547 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6548 goto done;
6551 if (!priv->scan) {
6552 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
6553 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6554 if (!priv->scan) {
6555 rc = -ENOMEM;
6556 goto done;
6559 scan = priv->scan;
6560 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
6562 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6563 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6565 if (iwl_is_associated(priv)) {
6566 u16 interval = 0;
6567 u32 extra;
6568 u32 suspend_time = 100;
6569 u32 scan_suspend_time = 100;
6570 unsigned long flags;
6572 IWL_DEBUG_INFO("Scanning while associated...\n");
6574 spin_lock_irqsave(&priv->lock, flags);
6575 interval = priv->beacon_int;
6576 spin_unlock_irqrestore(&priv->lock, flags);
6578 scan->suspend_time = 0;
6579 scan->max_out_time = cpu_to_le32(200 * 1024);
6580 if (!interval)
6581 interval = suspend_time;
6583 * suspend time format:
6584 * 0-19: beacon interval in usec (time before exec.)
6585 * 20-23: 0
6586 * 24-31: number of beacons (suspend between channels)
6589 extra = (suspend_time / interval) << 24;
6590 scan_suspend_time = 0xFF0FFFFF &
6591 (extra | ((suspend_time % interval) * 1024));
6593 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6594 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6595 scan_suspend_time, interval);
6598 /* We should add the ability for user to lock to PASSIVE ONLY */
6599 if (priv->one_direct_scan) {
6600 IWL_DEBUG_SCAN
6601 ("Kicking off one direct scan for '%s'\n",
6602 iwl_escape_essid(priv->direct_ssid,
6603 priv->direct_ssid_len));
6604 scan->direct_scan[0].id = WLAN_EID_SSID;
6605 scan->direct_scan[0].len = priv->direct_ssid_len;
6606 memcpy(scan->direct_scan[0].ssid,
6607 priv->direct_ssid, priv->direct_ssid_len);
6608 direct_mask = 1;
6609 } else if (!iwl_is_associated(priv) && priv->essid_len) {
6610 scan->direct_scan[0].id = WLAN_EID_SSID;
6611 scan->direct_scan[0].len = priv->essid_len;
6612 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6613 direct_mask = 1;
6614 } else
6615 direct_mask = 0;
6617 /* We don't build a direct scan probe request; the uCode will do
6618 * that based on the direct_mask added to each channel entry */
6619 scan->tx_cmd.len = cpu_to_le16(
6620 iwl_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
6621 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
6622 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6623 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6624 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6626 /* flags + rate selection */
6628 switch (priv->scan_bands) {
6629 case 2:
6630 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6631 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
6632 scan->good_CRC_th = 0;
6633 phymode = MODE_IEEE80211G;
6634 break;
6636 case 1:
6637 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
6638 scan->good_CRC_th = IWL_GOOD_CRC_TH;
6639 phymode = MODE_IEEE80211A;
6640 break;
6642 default:
6643 IWL_WARNING("Invalid scan band count\n");
6644 goto done;
6647 /* select Rx antennas */
6648 scan->flags |= iwl3945_get_antenna_flags(priv);
6650 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6651 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6653 if (direct_mask)
6654 IWL_DEBUG_SCAN
6655 ("Initiating direct scan for %s.\n",
6656 iwl_escape_essid(priv->essid, priv->essid_len));
6657 else
6658 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
6660 scan->channel_count =
6661 iwl_get_channels_for_scan(
6662 priv, phymode, 1, /* active */
6663 direct_mask,
6664 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6666 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
6667 scan->channel_count * sizeof(struct iwl_scan_channel);
6668 cmd.data = scan;
6669 scan->len = cpu_to_le16(cmd.len);
6671 set_bit(STATUS_SCAN_HW, &priv->status);
6672 rc = iwl_send_cmd_sync(priv, &cmd);
6673 if (rc)
6674 goto done;
6676 queue_delayed_work(priv->workqueue, &priv->scan_check,
6677 IWL_SCAN_CHECK_WATCHDOG);
6679 mutex_unlock(&priv->mutex);
6680 return;
6682 done:
6683 /* inform mac80211 sacn aborted */
6684 queue_work(priv->workqueue, &priv->scan_completed);
6685 mutex_unlock(&priv->mutex);
6688 static void iwl_bg_up(struct work_struct *data)
6690 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
6692 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6693 return;
6695 mutex_lock(&priv->mutex);
6696 __iwl_up(priv);
6697 mutex_unlock(&priv->mutex);
6700 static void iwl_bg_restart(struct work_struct *data)
6702 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
6704 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6705 return;
6707 iwl_down(priv);
6708 queue_work(priv->workqueue, &priv->up);
6711 static void iwl_bg_rx_replenish(struct work_struct *data)
6713 struct iwl_priv *priv =
6714 container_of(data, struct iwl_priv, rx_replenish);
6716 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6717 return;
6719 mutex_lock(&priv->mutex);
6720 iwl_rx_replenish(priv);
6721 mutex_unlock(&priv->mutex);
6724 static void iwl_bg_post_associate(struct work_struct *data)
6726 struct iwl_priv *priv = container_of(data, struct iwl_priv,
6727 post_associate.work);
6729 int rc = 0;
6730 struct ieee80211_conf *conf = NULL;
6731 DECLARE_MAC_BUF(mac);
6733 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6734 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6735 return;
6739 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6740 priv->assoc_id,
6741 print_mac(mac, priv->active_rxon.bssid_addr));
6743 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6744 return;
6746 mutex_lock(&priv->mutex);
6748 if (!priv->interface_id || !priv->is_open) {
6749 mutex_unlock(&priv->mutex);
6750 return;
6752 iwl_scan_cancel_timeout(priv, 200);
6754 conf = ieee80211_get_hw_conf(priv->hw);
6756 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6757 iwl_commit_rxon(priv);
6759 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
6760 iwl_setup_rxon_timing(priv);
6761 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
6762 sizeof(priv->rxon_timing), &priv->rxon_timing);
6763 if (rc)
6764 IWL_WARNING("REPLY_RXON_TIMING failed - "
6765 "Attempting to continue.\n");
6767 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6769 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6771 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6772 priv->assoc_id, priv->beacon_int);
6774 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6775 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6776 else
6777 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6779 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6780 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6781 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6782 else
6783 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6785 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6786 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6790 iwl_commit_rxon(priv);
6792 switch (priv->iw_mode) {
6793 case IEEE80211_IF_TYPE_STA:
6794 iwl_rate_scale_init(priv->hw, IWL_AP_ID);
6795 break;
6797 case IEEE80211_IF_TYPE_IBSS:
6799 /* clear out the station table */
6800 iwl_clear_stations_table(priv);
6802 iwl_add_station(priv, BROADCAST_ADDR, 0, 0);
6803 iwl_add_station(priv, priv->bssid, 0, 0);
6804 iwl3945_sync_sta(priv, IWL_STA_ID,
6805 (priv->phymode == MODE_IEEE80211A)?
6806 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
6807 CMD_ASYNC);
6808 iwl_rate_scale_init(priv->hw, IWL_STA_ID);
6809 iwl_send_beacon_cmd(priv);
6811 break;
6813 default:
6814 IWL_ERROR("%s Should not be called in %d mode\n",
6815 __FUNCTION__, priv->iw_mode);
6816 break;
6819 iwl_sequence_reset(priv);
6821 #ifdef CONFIG_IWLWIFI_QOS
6822 iwl_activate_qos(priv, 0);
6823 #endif /* CONFIG_IWLWIFI_QOS */
6824 mutex_unlock(&priv->mutex);
6827 static void iwl_bg_abort_scan(struct work_struct *work)
6829 struct iwl_priv *priv = container_of(work, struct iwl_priv,
6830 abort_scan);
6832 if (!iwl_is_ready(priv))
6833 return;
6835 mutex_lock(&priv->mutex);
6837 set_bit(STATUS_SCAN_ABORTING, &priv->status);
6838 iwl_send_scan_abort(priv);
6840 mutex_unlock(&priv->mutex);
6843 static void iwl_bg_scan_completed(struct work_struct *work)
6845 struct iwl_priv *priv =
6846 container_of(work, struct iwl_priv, scan_completed);
6848 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6850 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6851 return;
6853 ieee80211_scan_completed(priv->hw);
6855 /* Since setting the TXPOWER may have been deferred while
6856 * performing the scan, fire one off */
6857 mutex_lock(&priv->mutex);
6858 iwl_hw_reg_send_txpower(priv);
6859 mutex_unlock(&priv->mutex);
6862 /*****************************************************************************
6864 * mac80211 entry point functions
6866 *****************************************************************************/
6868 static int iwl_mac_start(struct ieee80211_hw *hw)
6870 struct iwl_priv *priv = hw->priv;
6872 IWL_DEBUG_MAC80211("enter\n");
6874 /* we should be verifying the device is ready to be opened */
6875 mutex_lock(&priv->mutex);
6877 priv->is_open = 1;
6879 if (!iwl_is_rfkill(priv))
6880 ieee80211_start_queues(priv->hw);
6882 mutex_unlock(&priv->mutex);
6883 IWL_DEBUG_MAC80211("leave\n");
6884 return 0;
6887 static void iwl_mac_stop(struct ieee80211_hw *hw)
6889 struct iwl_priv *priv = hw->priv;
6891 IWL_DEBUG_MAC80211("enter\n");
6894 mutex_lock(&priv->mutex);
6895 /* stop mac, cancel any scan request and clear
6896 * RXON_FILTER_ASSOC_MSK BIT
6898 priv->is_open = 0;
6899 iwl_scan_cancel_timeout(priv, 100);
6900 cancel_delayed_work(&priv->post_associate);
6901 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6902 iwl_commit_rxon(priv);
6903 mutex_unlock(&priv->mutex);
6905 IWL_DEBUG_MAC80211("leave\n");
6908 static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
6909 struct ieee80211_tx_control *ctl)
6911 struct iwl_priv *priv = hw->priv;
6913 IWL_DEBUG_MAC80211("enter\n");
6915 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6916 IWL_DEBUG_MAC80211("leave - monitor\n");
6917 return -1;
6920 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
6921 ctl->tx_rate);
6923 if (iwl_tx_skb(priv, skb, ctl))
6924 dev_kfree_skb_any(skb);
6926 IWL_DEBUG_MAC80211("leave\n");
6927 return 0;
6930 static int iwl_mac_add_interface(struct ieee80211_hw *hw,
6931 struct ieee80211_if_init_conf *conf)
6933 struct iwl_priv *priv = hw->priv;
6934 unsigned long flags;
6935 DECLARE_MAC_BUF(mac);
6937 IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
6938 if (conf->mac_addr)
6939 IWL_DEBUG_MAC80211("enter: MAC %s\n",
6940 print_mac(mac, conf->mac_addr));
6942 if (priv->interface_id) {
6943 IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
6944 return 0;
6947 spin_lock_irqsave(&priv->lock, flags);
6948 priv->interface_id = conf->if_id;
6950 spin_unlock_irqrestore(&priv->lock, flags);
6952 mutex_lock(&priv->mutex);
6953 iwl_set_mode(priv, conf->type);
6955 IWL_DEBUG_MAC80211("leave\n");
6956 mutex_unlock(&priv->mutex);
6958 return 0;
6962 * iwl_mac_config - mac80211 config callback
6964 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6965 * be set inappropriately and the driver currently sets the hardware up to
6966 * use it whenever needed.
6968 static int iwl_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
6970 struct iwl_priv *priv = hw->priv;
6971 const struct iwl_channel_info *ch_info;
6972 unsigned long flags;
6974 mutex_lock(&priv->mutex);
6975 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
6977 if (!iwl_is_ready(priv)) {
6978 IWL_DEBUG_MAC80211("leave - not ready\n");
6979 mutex_unlock(&priv->mutex);
6980 return -EIO;
6983 /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
6984 * what is exposed through include/ declrations */
6985 if (unlikely(!iwl_param_disable_hw_scan &&
6986 test_bit(STATUS_SCANNING, &priv->status))) {
6987 IWL_DEBUG_MAC80211("leave - scanning\n");
6988 mutex_unlock(&priv->mutex);
6989 return 0;
6992 spin_lock_irqsave(&priv->lock, flags);
6994 ch_info = iwl_get_channel_info(priv, conf->phymode, conf->channel);
6995 if (!is_channel_valid(ch_info)) {
6996 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
6997 conf->channel, conf->phymode);
6998 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6999 spin_unlock_irqrestore(&priv->lock, flags);
7000 mutex_unlock(&priv->mutex);
7001 return -EINVAL;
7004 iwl_set_rxon_channel(priv, conf->phymode, conf->channel);
7006 iwl_set_flags_for_phymode(priv, conf->phymode);
7008 /* The list of supported rates and rate mask can be different
7009 * for each phymode; since the phymode may have changed, reset
7010 * the rate mask to what mac80211 lists */
7011 iwl_set_rate(priv);
7013 spin_unlock_irqrestore(&priv->lock, flags);
7015 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
7016 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
7017 iwl_hw_channel_switch(priv, conf->channel);
7018 mutex_unlock(&priv->mutex);
7019 return 0;
7021 #endif
7023 iwl_radio_kill_sw(priv, !conf->radio_enabled);
7025 if (!conf->radio_enabled) {
7026 IWL_DEBUG_MAC80211("leave - radio disabled\n");
7027 mutex_unlock(&priv->mutex);
7028 return 0;
7031 if (iwl_is_rfkill(priv)) {
7032 IWL_DEBUG_MAC80211("leave - RF kill\n");
7033 mutex_unlock(&priv->mutex);
7034 return -EIO;
7037 iwl_set_rate(priv);
7039 if (memcmp(&priv->active_rxon,
7040 &priv->staging_rxon, sizeof(priv->staging_rxon)))
7041 iwl_commit_rxon(priv);
7042 else
7043 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7045 IWL_DEBUG_MAC80211("leave\n");
7047 mutex_unlock(&priv->mutex);
7049 return 0;
7052 static void iwl_config_ap(struct iwl_priv *priv)
7054 int rc = 0;
7056 if (priv->status & STATUS_EXIT_PENDING)
7057 return;
7059 /* The following should be done only at AP bring up */
7060 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7062 /* RXON - unassoc (to set timing command) */
7063 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7064 iwl_commit_rxon(priv);
7066 /* RXON Timing */
7067 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
7068 iwl_setup_rxon_timing(priv);
7069 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7070 sizeof(priv->rxon_timing), &priv->rxon_timing);
7071 if (rc)
7072 IWL_WARNING("REPLY_RXON_TIMING failed - "
7073 "Attempting to continue.\n");
7075 /* FIXME: what should be the assoc_id for AP? */
7076 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7077 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7078 priv->staging_rxon.flags |=
7079 RXON_FLG_SHORT_PREAMBLE_MSK;
7080 else
7081 priv->staging_rxon.flags &=
7082 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7084 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7085 if (priv->assoc_capability &
7086 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7087 priv->staging_rxon.flags |=
7088 RXON_FLG_SHORT_SLOT_MSK;
7089 else
7090 priv->staging_rxon.flags &=
7091 ~RXON_FLG_SHORT_SLOT_MSK;
7093 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7094 priv->staging_rxon.flags &=
7095 ~RXON_FLG_SHORT_SLOT_MSK;
7097 /* restore RXON assoc */
7098 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7099 iwl_commit_rxon(priv);
7100 iwl_add_station(priv, BROADCAST_ADDR, 0, 0);
7102 iwl_send_beacon_cmd(priv);
7104 /* FIXME - we need to add code here to detect a totally new
7105 * configuration, reset the AP, unassoc, rxon timing, assoc,
7106 * clear sta table, add BCAST sta... */
7109 static int iwl_mac_config_interface(struct ieee80211_hw *hw, int if_id,
7110 struct ieee80211_if_conf *conf)
7112 struct iwl_priv *priv = hw->priv;
7113 DECLARE_MAC_BUF(mac);
7114 unsigned long flags;
7115 int rc;
7117 if (conf == NULL)
7118 return -EIO;
7120 /* XXX: this MUST use conf->mac_addr */
7122 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7123 (!conf->beacon || !conf->ssid_len)) {
7124 IWL_DEBUG_MAC80211
7125 ("Leaving in AP mode because HostAPD is not ready.\n");
7126 return 0;
7129 mutex_lock(&priv->mutex);
7131 IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
7132 if (conf->bssid)
7133 IWL_DEBUG_MAC80211("bssid: %s\n",
7134 print_mac(mac, conf->bssid));
7137 * very dubious code was here; the probe filtering flag is never set:
7139 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7140 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
7142 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
7143 IWL_DEBUG_MAC80211("leave - scanning\n");
7144 mutex_unlock(&priv->mutex);
7145 return 0;
7148 if (priv->interface_id != if_id) {
7149 IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
7150 mutex_unlock(&priv->mutex);
7151 return 0;
7154 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7155 if (!conf->bssid) {
7156 conf->bssid = priv->mac_addr;
7157 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
7158 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7159 print_mac(mac, conf->bssid));
7161 if (priv->ibss_beacon)
7162 dev_kfree_skb(priv->ibss_beacon);
7164 priv->ibss_beacon = conf->beacon;
7167 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7168 !is_multicast_ether_addr(conf->bssid)) {
7169 /* If there is currently a HW scan going on in the background
7170 * then we need to cancel it else the RXON below will fail. */
7171 if (iwl_scan_cancel_timeout(priv, 100)) {
7172 IWL_WARNING("Aborted scan still in progress "
7173 "after 100ms\n");
7174 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7175 mutex_unlock(&priv->mutex);
7176 return -EAGAIN;
7178 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7180 /* TODO: Audit driver for usage of these members and see
7181 * if mac80211 deprecates them (priv->bssid looks like it
7182 * shouldn't be there, but I haven't scanned the IBSS code
7183 * to verify) - jpk */
7184 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7186 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7187 iwl_config_ap(priv);
7188 else {
7189 rc = iwl_commit_rxon(priv);
7190 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
7191 iwl_add_station(priv,
7192 priv->active_rxon.bssid_addr, 1, 0);
7195 } else {
7196 iwl_scan_cancel_timeout(priv, 100);
7197 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7198 iwl_commit_rxon(priv);
7201 spin_lock_irqsave(&priv->lock, flags);
7202 if (!conf->ssid_len)
7203 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7204 else
7205 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7207 priv->essid_len = conf->ssid_len;
7208 spin_unlock_irqrestore(&priv->lock, flags);
7210 IWL_DEBUG_MAC80211("leave\n");
7211 mutex_unlock(&priv->mutex);
7213 return 0;
7216 static void iwl_configure_filter(struct ieee80211_hw *hw,
7217 unsigned int changed_flags,
7218 unsigned int *total_flags,
7219 int mc_count, struct dev_addr_list *mc_list)
7222 * XXX: dummy
7223 * see also iwl_connection_init_rx_config
7225 *total_flags = 0;
7228 static void iwl_mac_remove_interface(struct ieee80211_hw *hw,
7229 struct ieee80211_if_init_conf *conf)
7231 struct iwl_priv *priv = hw->priv;
7233 IWL_DEBUG_MAC80211("enter\n");
7235 mutex_lock(&priv->mutex);
7237 iwl_scan_cancel_timeout(priv, 100);
7238 cancel_delayed_work(&priv->post_associate);
7239 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7240 iwl_commit_rxon(priv);
7242 if (priv->interface_id == conf->if_id) {
7243 priv->interface_id = 0;
7244 memset(priv->bssid, 0, ETH_ALEN);
7245 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7246 priv->essid_len = 0;
7248 mutex_unlock(&priv->mutex);
7250 IWL_DEBUG_MAC80211("leave\n");
7254 #define IWL_DELAY_NEXT_SCAN (HZ*2)
7255 static int iwl_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
7257 int rc = 0;
7258 unsigned long flags;
7259 struct iwl_priv *priv = hw->priv;
7261 IWL_DEBUG_MAC80211("enter\n");
7263 mutex_lock(&priv->mutex);
7264 spin_lock_irqsave(&priv->lock, flags);
7266 if (!iwl_is_ready_rf(priv)) {
7267 rc = -EIO;
7268 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7269 goto out_unlock;
7272 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7273 rc = -EIO;
7274 IWL_ERROR("ERROR: APs don't scan\n");
7275 goto out_unlock;
7278 /* if we just finished scan ask for delay */
7279 if (priv->last_scan_jiffies &&
7280 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
7281 jiffies)) {
7282 rc = -EAGAIN;
7283 goto out_unlock;
7285 if (len) {
7286 IWL_DEBUG_SCAN("direct scan for "
7287 "%s [%d]\n ",
7288 iwl_escape_essid(ssid, len), (int)len);
7290 priv->one_direct_scan = 1;
7291 priv->direct_ssid_len = (u8)
7292 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7293 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
7294 } else
7295 priv->one_direct_scan = 0;
7297 rc = iwl_scan_initiate(priv);
7299 IWL_DEBUG_MAC80211("leave\n");
7301 out_unlock:
7302 spin_unlock_irqrestore(&priv->lock, flags);
7303 mutex_unlock(&priv->mutex);
7305 return rc;
7308 static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
7309 const u8 *local_addr, const u8 *addr,
7310 struct ieee80211_key_conf *key)
7312 struct iwl_priv *priv = hw->priv;
7313 int rc = 0;
7314 u8 sta_id;
7316 IWL_DEBUG_MAC80211("enter\n");
7318 if (!iwl_param_hwcrypto) {
7319 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7320 return -EOPNOTSUPP;
7323 if (is_zero_ether_addr(addr))
7324 /* only support pairwise keys */
7325 return -EOPNOTSUPP;
7327 sta_id = iwl_hw_find_station(priv, addr);
7328 if (sta_id == IWL_INVALID_STATION) {
7329 DECLARE_MAC_BUF(mac);
7331 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7332 print_mac(mac, addr));
7333 return -EINVAL;
7336 mutex_lock(&priv->mutex);
7338 iwl_scan_cancel_timeout(priv, 100);
7340 switch (cmd) {
7341 case SET_KEY:
7342 rc = iwl_update_sta_key_info(priv, key, sta_id);
7343 if (!rc) {
7344 iwl_set_rxon_hwcrypto(priv, 1);
7345 iwl_commit_rxon(priv);
7346 key->hw_key_idx = sta_id;
7347 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7348 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7350 break;
7351 case DISABLE_KEY:
7352 rc = iwl_clear_sta_key_info(priv, sta_id);
7353 if (!rc) {
7354 iwl_set_rxon_hwcrypto(priv, 0);
7355 iwl_commit_rxon(priv);
7356 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7358 break;
7359 default:
7360 rc = -EINVAL;
7363 IWL_DEBUG_MAC80211("leave\n");
7364 mutex_unlock(&priv->mutex);
7366 return rc;
7369 static int iwl_mac_conf_tx(struct ieee80211_hw *hw, int queue,
7370 const struct ieee80211_tx_queue_params *params)
7372 struct iwl_priv *priv = hw->priv;
7373 #ifdef CONFIG_IWLWIFI_QOS
7374 unsigned long flags;
7375 int q;
7376 #endif /* CONFIG_IWL_QOS */
7378 IWL_DEBUG_MAC80211("enter\n");
7380 if (!iwl_is_ready_rf(priv)) {
7381 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7382 return -EIO;
7385 if (queue >= AC_NUM) {
7386 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7387 return 0;
7390 #ifdef CONFIG_IWLWIFI_QOS
7391 if (!priv->qos_data.qos_enable) {
7392 priv->qos_data.qos_active = 0;
7393 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7394 return 0;
7396 q = AC_NUM - 1 - queue;
7398 spin_lock_irqsave(&priv->lock, flags);
7400 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7401 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7402 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7403 priv->qos_data.def_qos_parm.ac[q].edca_txop =
7404 cpu_to_le16((params->burst_time * 100));
7406 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7407 priv->qos_data.qos_active = 1;
7409 spin_unlock_irqrestore(&priv->lock, flags);
7411 mutex_lock(&priv->mutex);
7412 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7413 iwl_activate_qos(priv, 1);
7414 else if (priv->assoc_id && iwl_is_associated(priv))
7415 iwl_activate_qos(priv, 0);
7417 mutex_unlock(&priv->mutex);
7419 #endif /*CONFIG_IWLWIFI_QOS */
7421 IWL_DEBUG_MAC80211("leave\n");
7422 return 0;
7425 static int iwl_mac_get_tx_stats(struct ieee80211_hw *hw,
7426 struct ieee80211_tx_queue_stats *stats)
7428 struct iwl_priv *priv = hw->priv;
7429 int i, avail;
7430 struct iwl_tx_queue *txq;
7431 struct iwl_queue *q;
7432 unsigned long flags;
7434 IWL_DEBUG_MAC80211("enter\n");
7436 if (!iwl_is_ready_rf(priv)) {
7437 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7438 return -EIO;
7441 spin_lock_irqsave(&priv->lock, flags);
7443 for (i = 0; i < AC_NUM; i++) {
7444 txq = &priv->txq[i];
7445 q = &txq->q;
7446 avail = iwl_queue_space(q);
7448 stats->data[i].len = q->n_window - avail;
7449 stats->data[i].limit = q->n_window - q->high_mark;
7450 stats->data[i].count = q->n_window;
7453 spin_unlock_irqrestore(&priv->lock, flags);
7455 IWL_DEBUG_MAC80211("leave\n");
7457 return 0;
7460 static int iwl_mac_get_stats(struct ieee80211_hw *hw,
7461 struct ieee80211_low_level_stats *stats)
7463 IWL_DEBUG_MAC80211("enter\n");
7464 IWL_DEBUG_MAC80211("leave\n");
7466 return 0;
7469 static u64 iwl_mac_get_tsf(struct ieee80211_hw *hw)
7471 IWL_DEBUG_MAC80211("enter\n");
7472 IWL_DEBUG_MAC80211("leave\n");
7474 return 0;
7477 static void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
7479 struct iwl_priv *priv = hw->priv;
7480 unsigned long flags;
7482 mutex_lock(&priv->mutex);
7483 IWL_DEBUG_MAC80211("enter\n");
7485 #ifdef CONFIG_IWLWIFI_QOS
7486 iwl_reset_qos(priv);
7487 #endif
7488 cancel_delayed_work(&priv->post_associate);
7490 spin_lock_irqsave(&priv->lock, flags);
7491 priv->assoc_id = 0;
7492 priv->assoc_capability = 0;
7493 priv->call_post_assoc_from_beacon = 0;
7495 /* new association get rid of ibss beacon skb */
7496 if (priv->ibss_beacon)
7497 dev_kfree_skb(priv->ibss_beacon);
7499 priv->ibss_beacon = NULL;
7501 priv->beacon_int = priv->hw->conf.beacon_int;
7502 priv->timestamp1 = 0;
7503 priv->timestamp0 = 0;
7504 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7505 priv->beacon_int = 0;
7507 spin_unlock_irqrestore(&priv->lock, flags);
7509 /* we are restarting association process
7510 * clear RXON_FILTER_ASSOC_MSK bit
7512 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
7513 iwl_scan_cancel_timeout(priv, 100);
7514 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7515 iwl_commit_rxon(priv);
7518 /* Per mac80211.h: This is only used in IBSS mode... */
7519 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7521 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7522 mutex_unlock(&priv->mutex);
7523 return;
7526 if (!iwl_is_ready_rf(priv)) {
7527 IWL_DEBUG_MAC80211("leave - not ready\n");
7528 mutex_unlock(&priv->mutex);
7529 return;
7532 priv->only_active_channel = 0;
7534 iwl_set_rate(priv);
7536 mutex_unlock(&priv->mutex);
7538 IWL_DEBUG_MAC80211("leave\n");
7542 static int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
7543 struct ieee80211_tx_control *control)
7545 struct iwl_priv *priv = hw->priv;
7546 unsigned long flags;
7548 mutex_lock(&priv->mutex);
7549 IWL_DEBUG_MAC80211("enter\n");
7551 if (!iwl_is_ready_rf(priv)) {
7552 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7553 mutex_unlock(&priv->mutex);
7554 return -EIO;
7557 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7558 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7559 mutex_unlock(&priv->mutex);
7560 return -EIO;
7563 spin_lock_irqsave(&priv->lock, flags);
7565 if (priv->ibss_beacon)
7566 dev_kfree_skb(priv->ibss_beacon);
7568 priv->ibss_beacon = skb;
7570 priv->assoc_id = 0;
7572 IWL_DEBUG_MAC80211("leave\n");
7573 spin_unlock_irqrestore(&priv->lock, flags);
7575 #ifdef CONFIG_IWLWIFI_QOS
7576 iwl_reset_qos(priv);
7577 #endif
7579 queue_work(priv->workqueue, &priv->post_associate.work);
7581 mutex_unlock(&priv->mutex);
7583 return 0;
7586 /*****************************************************************************
7588 * sysfs attributes
7590 *****************************************************************************/
7592 #ifdef CONFIG_IWLWIFI_DEBUG
7595 * The following adds a new attribute to the sysfs representation
7596 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7597 * used for controlling the debug level.
7599 * See the level definitions in iwl for details.
7602 static ssize_t show_debug_level(struct device_driver *d, char *buf)
7604 return sprintf(buf, "0x%08X\n", iwl_debug_level);
7606 static ssize_t store_debug_level(struct device_driver *d,
7607 const char *buf, size_t count)
7609 char *p = (char *)buf;
7610 u32 val;
7612 val = simple_strtoul(p, &p, 0);
7613 if (p == buf)
7614 printk(KERN_INFO DRV_NAME
7615 ": %s is not in hex or decimal form.\n", buf);
7616 else
7617 iwl_debug_level = val;
7619 return strnlen(buf, count);
7622 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7623 show_debug_level, store_debug_level);
7625 #endif /* CONFIG_IWLWIFI_DEBUG */
7627 static ssize_t show_rf_kill(struct device *d,
7628 struct device_attribute *attr, char *buf)
7631 * 0 - RF kill not enabled
7632 * 1 - SW based RF kill active (sysfs)
7633 * 2 - HW based RF kill active
7634 * 3 - Both HW and SW based RF kill active
7636 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7637 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
7638 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
7640 return sprintf(buf, "%i\n", val);
7643 static ssize_t store_rf_kill(struct device *d,
7644 struct device_attribute *attr,
7645 const char *buf, size_t count)
7647 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7649 mutex_lock(&priv->mutex);
7650 iwl_radio_kill_sw(priv, buf[0] == '1');
7651 mutex_unlock(&priv->mutex);
7653 return count;
7656 static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7658 static ssize_t show_temperature(struct device *d,
7659 struct device_attribute *attr, char *buf)
7661 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7663 if (!iwl_is_alive(priv))
7664 return -EAGAIN;
7666 return sprintf(buf, "%d\n", iwl_hw_get_temperature(priv));
7669 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7671 static ssize_t show_rs_window(struct device *d,
7672 struct device_attribute *attr,
7673 char *buf)
7675 struct iwl_priv *priv = d->driver_data;
7676 return iwl_fill_rs_info(priv->hw, buf, IWL_AP_ID);
7678 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7680 static ssize_t show_tx_power(struct device *d,
7681 struct device_attribute *attr, char *buf)
7683 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7684 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7687 static ssize_t store_tx_power(struct device *d,
7688 struct device_attribute *attr,
7689 const char *buf, size_t count)
7691 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7692 char *p = (char *)buf;
7693 u32 val;
7695 val = simple_strtoul(p, &p, 10);
7696 if (p == buf)
7697 printk(KERN_INFO DRV_NAME
7698 ": %s is not in decimal form.\n", buf);
7699 else
7700 iwl_hw_reg_set_txpower(priv, val);
7702 return count;
7705 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7707 static ssize_t show_flags(struct device *d,
7708 struct device_attribute *attr, char *buf)
7710 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7712 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7715 static ssize_t store_flags(struct device *d,
7716 struct device_attribute *attr,
7717 const char *buf, size_t count)
7719 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7720 u32 flags = simple_strtoul(buf, NULL, 0);
7722 mutex_lock(&priv->mutex);
7723 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7724 /* Cancel any currently running scans... */
7725 if (iwl_scan_cancel_timeout(priv, 100))
7726 IWL_WARNING("Could not cancel scan.\n");
7727 else {
7728 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7729 flags);
7730 priv->staging_rxon.flags = cpu_to_le32(flags);
7731 iwl_commit_rxon(priv);
7734 mutex_unlock(&priv->mutex);
7736 return count;
7739 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7741 static ssize_t show_filter_flags(struct device *d,
7742 struct device_attribute *attr, char *buf)
7744 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7746 return sprintf(buf, "0x%04X\n",
7747 le32_to_cpu(priv->active_rxon.filter_flags));
7750 static ssize_t store_filter_flags(struct device *d,
7751 struct device_attribute *attr,
7752 const char *buf, size_t count)
7754 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7755 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7757 mutex_lock(&priv->mutex);
7758 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7759 /* Cancel any currently running scans... */
7760 if (iwl_scan_cancel_timeout(priv, 100))
7761 IWL_WARNING("Could not cancel scan.\n");
7762 else {
7763 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7764 "0x%04X\n", filter_flags);
7765 priv->staging_rxon.filter_flags =
7766 cpu_to_le32(filter_flags);
7767 iwl_commit_rxon(priv);
7770 mutex_unlock(&priv->mutex);
7772 return count;
7775 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7776 store_filter_flags);
7778 static ssize_t show_tune(struct device *d,
7779 struct device_attribute *attr, char *buf)
7781 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7783 return sprintf(buf, "0x%04X\n",
7784 (priv->phymode << 8) |
7785 le16_to_cpu(priv->active_rxon.channel));
7788 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode);
7790 static ssize_t store_tune(struct device *d,
7791 struct device_attribute *attr,
7792 const char *buf, size_t count)
7794 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7795 char *p = (char *)buf;
7796 u16 tune = simple_strtoul(p, &p, 0);
7797 u8 phymode = (tune >> 8) & 0xff;
7798 u16 channel = tune & 0xff;
7800 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
7802 mutex_lock(&priv->mutex);
7803 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
7804 (priv->phymode != phymode)) {
7805 const struct iwl_channel_info *ch_info;
7807 ch_info = iwl_get_channel_info(priv, phymode, channel);
7808 if (!ch_info) {
7809 IWL_WARNING("Requested invalid phymode/channel "
7810 "combination: %d %d\n", phymode, channel);
7811 mutex_unlock(&priv->mutex);
7812 return -EINVAL;
7815 /* Cancel any currently running scans... */
7816 if (iwl_scan_cancel_timeout(priv, 100))
7817 IWL_WARNING("Could not cancel scan.\n");
7818 else {
7819 IWL_DEBUG_INFO("Committing phymode and "
7820 "rxon.channel = %d %d\n",
7821 phymode, channel);
7823 iwl_set_rxon_channel(priv, phymode, channel);
7824 iwl_set_flags_for_phymode(priv, phymode);
7826 iwl_set_rate(priv);
7827 iwl_commit_rxon(priv);
7830 mutex_unlock(&priv->mutex);
7832 return count;
7835 static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
7837 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
7839 static ssize_t show_measurement(struct device *d,
7840 struct device_attribute *attr, char *buf)
7842 struct iwl_priv *priv = dev_get_drvdata(d);
7843 struct iwl_spectrum_notification measure_report;
7844 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7845 u8 *data = (u8 *) & measure_report;
7846 unsigned long flags;
7848 spin_lock_irqsave(&priv->lock, flags);
7849 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7850 spin_unlock_irqrestore(&priv->lock, flags);
7851 return 0;
7853 memcpy(&measure_report, &priv->measure_report, size);
7854 priv->measurement_status = 0;
7855 spin_unlock_irqrestore(&priv->lock, flags);
7857 while (size && (PAGE_SIZE - len)) {
7858 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7859 PAGE_SIZE - len, 1);
7860 len = strlen(buf);
7861 if (PAGE_SIZE - len)
7862 buf[len++] = '\n';
7864 ofs += 16;
7865 size -= min(size, 16U);
7868 return len;
7871 static ssize_t store_measurement(struct device *d,
7872 struct device_attribute *attr,
7873 const char *buf, size_t count)
7875 struct iwl_priv *priv = dev_get_drvdata(d);
7876 struct ieee80211_measurement_params params = {
7877 .channel = le16_to_cpu(priv->active_rxon.channel),
7878 .start_time = cpu_to_le64(priv->last_tsf),
7879 .duration = cpu_to_le16(1),
7881 u8 type = IWL_MEASURE_BASIC;
7882 u8 buffer[32];
7883 u8 channel;
7885 if (count) {
7886 char *p = buffer;
7887 strncpy(buffer, buf, min(sizeof(buffer), count));
7888 channel = simple_strtoul(p, NULL, 0);
7889 if (channel)
7890 params.channel = channel;
7892 p = buffer;
7893 while (*p && *p != ' ')
7894 p++;
7895 if (*p)
7896 type = simple_strtoul(p + 1, NULL, 0);
7899 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7900 "channel %d (for '%s')\n", type, params.channel, buf);
7901 iwl_get_measurement(priv, &params, type);
7903 return count;
7906 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7907 show_measurement, store_measurement);
7908 #endif /* CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT */
7910 static ssize_t show_rate(struct device *d,
7911 struct device_attribute *attr, char *buf)
7913 struct iwl_priv *priv = dev_get_drvdata(d);
7914 unsigned long flags;
7915 int i;
7917 spin_lock_irqsave(&priv->sta_lock, flags);
7918 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
7919 i = priv->stations[IWL_AP_ID].current_rate.s.rate;
7920 else
7921 i = priv->stations[IWL_STA_ID].current_rate.s.rate;
7922 spin_unlock_irqrestore(&priv->sta_lock, flags);
7924 i = iwl_rate_index_from_plcp(i);
7925 if (i == -1)
7926 return sprintf(buf, "0\n");
7928 return sprintf(buf, "%d%s\n",
7929 (iwl_rates[i].ieee >> 1),
7930 (iwl_rates[i].ieee & 0x1) ? ".5" : "");
7933 static DEVICE_ATTR(rate, S_IRUSR, show_rate, NULL);
7935 static ssize_t store_retry_rate(struct device *d,
7936 struct device_attribute *attr,
7937 const char *buf, size_t count)
7939 struct iwl_priv *priv = dev_get_drvdata(d);
7941 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7942 if (priv->retry_rate <= 0)
7943 priv->retry_rate = 1;
7945 return count;
7948 static ssize_t show_retry_rate(struct device *d,
7949 struct device_attribute *attr, char *buf)
7951 struct iwl_priv *priv = dev_get_drvdata(d);
7952 return sprintf(buf, "%d", priv->retry_rate);
7955 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7956 store_retry_rate);
7958 static ssize_t store_power_level(struct device *d,
7959 struct device_attribute *attr,
7960 const char *buf, size_t count)
7962 struct iwl_priv *priv = dev_get_drvdata(d);
7963 int rc;
7964 int mode;
7966 mode = simple_strtoul(buf, NULL, 0);
7967 mutex_lock(&priv->mutex);
7969 if (!iwl_is_ready(priv)) {
7970 rc = -EAGAIN;
7971 goto out;
7974 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7975 mode = IWL_POWER_AC;
7976 else
7977 mode |= IWL_POWER_ENABLED;
7979 if (mode != priv->power_mode) {
7980 rc = iwl_send_power_mode(priv, IWL_POWER_LEVEL(mode));
7981 if (rc) {
7982 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7983 goto out;
7985 priv->power_mode = mode;
7988 rc = count;
7990 out:
7991 mutex_unlock(&priv->mutex);
7992 return rc;
7995 #define MAX_WX_STRING 80
7997 /* Values are in microsecond */
7998 static const s32 timeout_duration[] = {
7999 350000,
8000 250000,
8001 75000,
8002 37000,
8003 25000,
8005 static const s32 period_duration[] = {
8006 400000,
8007 700000,
8008 1000000,
8009 1000000,
8010 1000000
8013 static ssize_t show_power_level(struct device *d,
8014 struct device_attribute *attr, char *buf)
8016 struct iwl_priv *priv = dev_get_drvdata(d);
8017 int level = IWL_POWER_LEVEL(priv->power_mode);
8018 char *p = buf;
8020 p += sprintf(p, "%d ", level);
8021 switch (level) {
8022 case IWL_POWER_MODE_CAM:
8023 case IWL_POWER_AC:
8024 p += sprintf(p, "(AC)");
8025 break;
8026 case IWL_POWER_BATTERY:
8027 p += sprintf(p, "(BATTERY)");
8028 break;
8029 default:
8030 p += sprintf(p,
8031 "(Timeout %dms, Period %dms)",
8032 timeout_duration[level - 1] / 1000,
8033 period_duration[level - 1] / 1000);
8036 if (!(priv->power_mode & IWL_POWER_ENABLED))
8037 p += sprintf(p, " OFF\n");
8038 else
8039 p += sprintf(p, " \n");
8041 return (p - buf + 1);
8045 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8046 store_power_level);
8048 static ssize_t show_channels(struct device *d,
8049 struct device_attribute *attr, char *buf)
8051 struct iwl_priv *priv = dev_get_drvdata(d);
8052 int len = 0, i;
8053 struct ieee80211_channel *channels = NULL;
8054 const struct ieee80211_hw_mode *hw_mode = NULL;
8055 int count = 0;
8057 if (!iwl_is_ready(priv))
8058 return -EAGAIN;
8060 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211G);
8061 if (!hw_mode)
8062 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211B);
8063 if (hw_mode) {
8064 channels = hw_mode->channels;
8065 count = hw_mode->num_channels;
8068 len +=
8069 sprintf(&buf[len],
8070 "Displaying %d channels in 2.4GHz band "
8071 "(802.11bg):\n", count);
8073 for (i = 0; i < count; i++)
8074 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8075 channels[i].chan,
8076 channels[i].power_level,
8077 channels[i].
8078 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8079 " (IEEE 802.11h required)" : "",
8080 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8081 || (channels[i].
8082 flag &
8083 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8084 ", IBSS",
8085 channels[i].
8086 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8087 "active/passive" : "passive only");
8089 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211A);
8090 if (hw_mode) {
8091 channels = hw_mode->channels;
8092 count = hw_mode->num_channels;
8093 } else {
8094 channels = NULL;
8095 count = 0;
8098 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8099 "(802.11a):\n", count);
8101 for (i = 0; i < count; i++)
8102 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8103 channels[i].chan,
8104 channels[i].power_level,
8105 channels[i].
8106 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8107 " (IEEE 802.11h required)" : "",
8108 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8109 || (channels[i].
8110 flag &
8111 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8112 ", IBSS",
8113 channels[i].
8114 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8115 "active/passive" : "passive only");
8117 return len;
8120 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8122 static ssize_t show_statistics(struct device *d,
8123 struct device_attribute *attr, char *buf)
8125 struct iwl_priv *priv = dev_get_drvdata(d);
8126 u32 size = sizeof(struct iwl_notif_statistics);
8127 u32 len = 0, ofs = 0;
8128 u8 *data = (u8 *) & priv->statistics;
8129 int rc = 0;
8131 if (!iwl_is_alive(priv))
8132 return -EAGAIN;
8134 mutex_lock(&priv->mutex);
8135 rc = iwl_send_statistics_request(priv);
8136 mutex_unlock(&priv->mutex);
8138 if (rc) {
8139 len = sprintf(buf,
8140 "Error sending statistics request: 0x%08X\n", rc);
8141 return len;
8144 while (size && (PAGE_SIZE - len)) {
8145 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8146 PAGE_SIZE - len, 1);
8147 len = strlen(buf);
8148 if (PAGE_SIZE - len)
8149 buf[len++] = '\n';
8151 ofs += 16;
8152 size -= min(size, 16U);
8155 return len;
8158 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8160 static ssize_t show_antenna(struct device *d,
8161 struct device_attribute *attr, char *buf)
8163 struct iwl_priv *priv = dev_get_drvdata(d);
8165 if (!iwl_is_alive(priv))
8166 return -EAGAIN;
8168 return sprintf(buf, "%d\n", priv->antenna);
8171 static ssize_t store_antenna(struct device *d,
8172 struct device_attribute *attr,
8173 const char *buf, size_t count)
8175 int ant;
8176 struct iwl_priv *priv = dev_get_drvdata(d);
8178 if (count == 0)
8179 return 0;
8181 if (sscanf(buf, "%1i", &ant) != 1) {
8182 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8183 return count;
8186 if ((ant >= 0) && (ant <= 2)) {
8187 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
8188 priv->antenna = (enum iwl_antenna)ant;
8189 } else
8190 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8193 return count;
8196 static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8198 static ssize_t show_status(struct device *d,
8199 struct device_attribute *attr, char *buf)
8201 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8202 if (!iwl_is_alive(priv))
8203 return -EAGAIN;
8204 return sprintf(buf, "0x%08x\n", (int)priv->status);
8207 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8209 static ssize_t dump_error_log(struct device *d,
8210 struct device_attribute *attr,
8211 const char *buf, size_t count)
8213 char *p = (char *)buf;
8215 if (p[0] == '1')
8216 iwl_dump_nic_error_log((struct iwl_priv *)d->driver_data);
8218 return strnlen(buf, count);
8221 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8223 static ssize_t dump_event_log(struct device *d,
8224 struct device_attribute *attr,
8225 const char *buf, size_t count)
8227 char *p = (char *)buf;
8229 if (p[0] == '1')
8230 iwl_dump_nic_event_log((struct iwl_priv *)d->driver_data);
8232 return strnlen(buf, count);
8235 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8237 /*****************************************************************************
8239 * driver setup and teardown
8241 *****************************************************************************/
8243 static void iwl_setup_deferred_work(struct iwl_priv *priv)
8245 priv->workqueue = create_workqueue(DRV_NAME);
8247 init_waitqueue_head(&priv->wait_command_queue);
8249 INIT_WORK(&priv->up, iwl_bg_up);
8250 INIT_WORK(&priv->restart, iwl_bg_restart);
8251 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
8252 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
8253 INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
8254 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
8255 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
8256 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
8257 INIT_DELAYED_WORK(&priv->post_associate, iwl_bg_post_associate);
8258 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
8259 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
8260 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
8262 iwl_hw_setup_deferred_work(priv);
8264 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
8265 iwl_irq_tasklet, (unsigned long)priv);
8268 static void iwl_cancel_deferred_work(struct iwl_priv *priv)
8270 iwl_hw_cancel_deferred_work(priv);
8272 cancel_delayed_work(&priv->scan_check);
8273 cancel_delayed_work(&priv->alive_start);
8274 cancel_delayed_work(&priv->post_associate);
8275 cancel_work_sync(&priv->beacon_update);
8278 static struct attribute *iwl_sysfs_entries[] = {
8279 &dev_attr_antenna.attr,
8280 &dev_attr_channels.attr,
8281 &dev_attr_dump_errors.attr,
8282 &dev_attr_dump_events.attr,
8283 &dev_attr_flags.attr,
8284 &dev_attr_filter_flags.attr,
8285 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8286 &dev_attr_measurement.attr,
8287 #endif
8288 &dev_attr_power_level.attr,
8289 &dev_attr_rate.attr,
8290 &dev_attr_retry_rate.attr,
8291 &dev_attr_rf_kill.attr,
8292 &dev_attr_rs_window.attr,
8293 &dev_attr_statistics.attr,
8294 &dev_attr_status.attr,
8295 &dev_attr_temperature.attr,
8296 &dev_attr_tune.attr,
8297 &dev_attr_tx_power.attr,
8299 NULL
8302 static struct attribute_group iwl_attribute_group = {
8303 .name = NULL, /* put in device directory */
8304 .attrs = iwl_sysfs_entries,
8307 static struct ieee80211_ops iwl_hw_ops = {
8308 .tx = iwl_mac_tx,
8309 .start = iwl_mac_start,
8310 .stop = iwl_mac_stop,
8311 .add_interface = iwl_mac_add_interface,
8312 .remove_interface = iwl_mac_remove_interface,
8313 .config = iwl_mac_config,
8314 .config_interface = iwl_mac_config_interface,
8315 .configure_filter = iwl_configure_filter,
8316 .set_key = iwl_mac_set_key,
8317 .get_stats = iwl_mac_get_stats,
8318 .get_tx_stats = iwl_mac_get_tx_stats,
8319 .conf_tx = iwl_mac_conf_tx,
8320 .get_tsf = iwl_mac_get_tsf,
8321 .reset_tsf = iwl_mac_reset_tsf,
8322 .beacon_update = iwl_mac_beacon_update,
8323 .hw_scan = iwl_mac_hw_scan
8326 static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8328 int err = 0;
8329 u32 pci_id;
8330 struct iwl_priv *priv;
8331 struct ieee80211_hw *hw;
8332 int i;
8334 if (iwl_param_disable_hw_scan) {
8335 IWL_DEBUG_INFO("Disabling hw_scan\n");
8336 iwl_hw_ops.hw_scan = NULL;
8339 if ((iwl_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8340 (iwl_param_queues_num < IWL_MIN_NUM_QUEUES)) {
8341 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8342 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8343 err = -EINVAL;
8344 goto out;
8347 /* mac80211 allocates memory for this device instance, including
8348 * space for this driver's private structure */
8349 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl_hw_ops);
8350 if (hw == NULL) {
8351 IWL_ERROR("Can not allocate network device\n");
8352 err = -ENOMEM;
8353 goto out;
8355 SET_IEEE80211_DEV(hw, &pdev->dev);
8357 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8358 priv = hw->priv;
8359 priv->hw = hw;
8361 priv->pci_dev = pdev;
8362 priv->antenna = (enum iwl_antenna)iwl_param_antenna;
8363 #ifdef CONFIG_IWLWIFI_DEBUG
8364 iwl_debug_level = iwl_param_debug;
8365 atomic_set(&priv->restrict_refcnt, 0);
8366 #endif
8367 priv->retry_rate = 1;
8369 priv->ibss_beacon = NULL;
8371 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8372 * the range of signal quality values that we'll provide.
8373 * Negative values for level/noise indicate that we'll provide dBm.
8374 * For WE, at least, non-0 values here *enable* display of values
8375 * in app (iwconfig). */
8376 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8377 hw->max_noise = -20; /* noise level, negative indicates dBm */
8378 hw->max_signal = 100; /* link quality indication (%) */
8380 /* Tell mac80211 our Tx characteristics */
8381 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8383 hw->queues = 4;
8385 spin_lock_init(&priv->lock);
8386 spin_lock_init(&priv->power_data.lock);
8387 spin_lock_init(&priv->sta_lock);
8388 spin_lock_init(&priv->hcmd_lock);
8390 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8391 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8393 INIT_LIST_HEAD(&priv->free_frames);
8395 mutex_init(&priv->mutex);
8396 if (pci_enable_device(pdev)) {
8397 err = -ENODEV;
8398 goto out_ieee80211_free_hw;
8401 pci_set_master(pdev);
8403 iwl_clear_stations_table(priv);
8405 priv->data_retry_limit = -1;
8406 priv->ieee_channels = NULL;
8407 priv->ieee_rates = NULL;
8408 priv->phymode = -1;
8410 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8411 if (!err)
8412 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8413 if (err) {
8414 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
8415 goto out_pci_disable_device;
8418 pci_set_drvdata(pdev, priv);
8419 err = pci_request_regions(pdev, DRV_NAME);
8420 if (err)
8421 goto out_pci_disable_device;
8422 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8423 * PCI Tx retries from interfering with C3 CPU state */
8424 pci_write_config_byte(pdev, 0x41, 0x00);
8425 priv->hw_base = pci_iomap(pdev, 0, 0);
8426 if (!priv->hw_base) {
8427 err = -ENODEV;
8428 goto out_pci_release_regions;
8431 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8432 (unsigned long long) pci_resource_len(pdev, 0));
8433 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8435 /* Initialize module parameter values here */
8437 if (iwl_param_disable) {
8438 set_bit(STATUS_RF_KILL_SW, &priv->status);
8439 IWL_DEBUG_INFO("Radio disabled.\n");
8442 priv->iw_mode = IEEE80211_IF_TYPE_STA;
8444 pci_id =
8445 (priv->pci_dev->device << 16) | priv->pci_dev->subsystem_device;
8447 switch (pci_id) {
8448 case 0x42221005: /* 0x4222 0x8086 0x1005 is BG SKU */
8449 case 0x42221034: /* 0x4222 0x8086 0x1034 is BG SKU */
8450 case 0x42271014: /* 0x4227 0x8086 0x1014 is BG SKU */
8451 case 0x42221044: /* 0x4222 0x8086 0x1044 is BG SKU */
8452 priv->is_abg = 0;
8453 break;
8456 * Rest are assumed ABG SKU -- if this is not the
8457 * case then the card will get the wrong 'Detected'
8458 * line in the kernel log however the code that
8459 * initializes the GEO table will detect no A-band
8460 * channels and remove the is_abg mask.
8462 default:
8463 priv->is_abg = 1;
8464 break;
8467 printk(KERN_INFO DRV_NAME
8468 ": Detected Intel PRO/Wireless 3945%sBG Network Connection\n",
8469 priv->is_abg ? "A" : "");
8471 /* Device-specific setup */
8472 if (iwl_hw_set_hw_setting(priv)) {
8473 IWL_ERROR("failed to set hw settings\n");
8474 mutex_unlock(&priv->mutex);
8475 goto out_iounmap;
8478 #ifdef CONFIG_IWLWIFI_QOS
8479 if (iwl_param_qos_enable)
8480 priv->qos_data.qos_enable = 1;
8482 iwl_reset_qos(priv);
8484 priv->qos_data.qos_active = 0;
8485 priv->qos_data.qos_cap.val = 0;
8486 #endif /* CONFIG_IWLWIFI_QOS */
8488 iwl_set_rxon_channel(priv, MODE_IEEE80211G, 6);
8489 iwl_setup_deferred_work(priv);
8490 iwl_setup_rx_handlers(priv);
8492 priv->rates_mask = IWL_RATES_MASK;
8493 /* If power management is turned on, default to AC mode */
8494 priv->power_mode = IWL_POWER_AC;
8495 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
8497 pci_enable_msi(pdev);
8499 err = request_irq(pdev->irq, iwl_isr, IRQF_SHARED, DRV_NAME, priv);
8500 if (err) {
8501 IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
8502 goto out_disable_msi;
8505 mutex_lock(&priv->mutex);
8507 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
8508 if (err) {
8509 IWL_ERROR("failed to create sysfs device attributes\n");
8510 mutex_unlock(&priv->mutex);
8511 goto out_release_irq;
8514 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
8515 * ucode filename and max sizes are card-specific. */
8516 err = iwl_read_ucode(priv);
8517 if (err) {
8518 IWL_ERROR("Could not read microcode: %d\n", err);
8519 mutex_unlock(&priv->mutex);
8520 goto out_pci_alloc;
8523 mutex_unlock(&priv->mutex);
8525 IWL_DEBUG_INFO("Queing UP work.\n");
8527 queue_work(priv->workqueue, &priv->up);
8529 return 0;
8531 out_pci_alloc:
8532 iwl_dealloc_ucode_pci(priv);
8534 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
8536 out_release_irq:
8537 free_irq(pdev->irq, priv);
8539 out_disable_msi:
8540 pci_disable_msi(pdev);
8541 destroy_workqueue(priv->workqueue);
8542 priv->workqueue = NULL;
8543 iwl_unset_hw_setting(priv);
8545 out_iounmap:
8546 pci_iounmap(pdev, priv->hw_base);
8547 out_pci_release_regions:
8548 pci_release_regions(pdev);
8549 out_pci_disable_device:
8550 pci_disable_device(pdev);
8551 pci_set_drvdata(pdev, NULL);
8552 out_ieee80211_free_hw:
8553 ieee80211_free_hw(priv->hw);
8554 out:
8555 return err;
8558 static void iwl_pci_remove(struct pci_dev *pdev)
8560 struct iwl_priv *priv = pci_get_drvdata(pdev);
8561 struct list_head *p, *q;
8562 int i;
8564 if (!priv)
8565 return;
8567 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8569 mutex_lock(&priv->mutex);
8570 set_bit(STATUS_EXIT_PENDING, &priv->status);
8571 __iwl_down(priv);
8572 mutex_unlock(&priv->mutex);
8574 /* Free MAC hash list for ADHOC */
8575 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8576 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8577 list_del(p);
8578 kfree(list_entry(p, struct iwl_ibss_seq, list));
8582 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
8584 iwl_dealloc_ucode_pci(priv);
8586 if (priv->rxq.bd)
8587 iwl_rx_queue_free(priv, &priv->rxq);
8588 iwl_hw_txq_ctx_free(priv);
8590 iwl_unset_hw_setting(priv);
8591 iwl_clear_stations_table(priv);
8593 if (priv->mac80211_registered) {
8594 ieee80211_unregister_hw(priv->hw);
8595 iwl_rate_control_unregister(priv->hw);
8598 /*netif_stop_queue(dev); */
8599 flush_workqueue(priv->workqueue);
8601 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
8602 * priv->workqueue... so we can't take down the workqueue
8603 * until now... */
8604 destroy_workqueue(priv->workqueue);
8605 priv->workqueue = NULL;
8607 free_irq(pdev->irq, priv);
8608 pci_disable_msi(pdev);
8609 pci_iounmap(pdev, priv->hw_base);
8610 pci_release_regions(pdev);
8611 pci_disable_device(pdev);
8612 pci_set_drvdata(pdev, NULL);
8614 kfree(priv->channel_info);
8616 kfree(priv->ieee_channels);
8617 kfree(priv->ieee_rates);
8619 if (priv->ibss_beacon)
8620 dev_kfree_skb(priv->ibss_beacon);
8622 ieee80211_free_hw(priv->hw);
8625 #ifdef CONFIG_PM
8627 static int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
8629 struct iwl_priv *priv = pci_get_drvdata(pdev);
8631 mutex_lock(&priv->mutex);
8633 set_bit(STATUS_IN_SUSPEND, &priv->status);
8635 /* Take down the device; powers it off, etc. */
8636 __iwl_down(priv);
8638 if (priv->mac80211_registered)
8639 ieee80211_stop_queues(priv->hw);
8641 pci_save_state(pdev);
8642 pci_disable_device(pdev);
8643 pci_set_power_state(pdev, PCI_D3hot);
8645 mutex_unlock(&priv->mutex);
8647 return 0;
8650 static void iwl_resume(struct iwl_priv *priv)
8652 unsigned long flags;
8654 /* The following it a temporary work around due to the
8655 * suspend / resume not fully initializing the NIC correctly.
8656 * Without all of the following, resume will not attempt to take
8657 * down the NIC (it shouldn't really need to) and will just try
8658 * and bring the NIC back up. However that fails during the
8659 * ucode verification process. This then causes iwl_down to be
8660 * called *after* iwl_hw_nic_init() has succeeded -- which
8661 * then lets the next init sequence succeed. So, we've
8662 * replicated all of that NIC init code here... */
8664 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
8666 iwl_hw_nic_init(priv);
8668 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
8669 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
8670 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
8671 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
8672 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
8673 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
8675 /* tell the device to stop sending interrupts */
8676 iwl_disable_interrupts(priv);
8678 spin_lock_irqsave(&priv->lock, flags);
8679 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
8681 if (!iwl_grab_restricted_access(priv)) {
8682 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
8683 APMG_CLK_VAL_DMA_CLK_RQT);
8684 iwl_release_restricted_access(priv);
8686 spin_unlock_irqrestore(&priv->lock, flags);
8688 udelay(5);
8690 iwl_hw_nic_reset(priv);
8692 /* Bring the device back up */
8693 clear_bit(STATUS_IN_SUSPEND, &priv->status);
8694 queue_work(priv->workqueue, &priv->up);
8697 static int iwl_pci_resume(struct pci_dev *pdev)
8699 struct iwl_priv *priv = pci_get_drvdata(pdev);
8700 int err;
8702 printk(KERN_INFO "Coming out of suspend...\n");
8704 mutex_lock(&priv->mutex);
8706 pci_set_power_state(pdev, PCI_D0);
8707 err = pci_enable_device(pdev);
8708 pci_restore_state(pdev);
8711 * Suspend/Resume resets the PCI configuration space, so we have to
8712 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
8713 * from interfering with C3 CPU state. pci_restore_state won't help
8714 * here since it only restores the first 64 bytes pci config header.
8716 pci_write_config_byte(pdev, 0x41, 0x00);
8718 iwl_resume(priv);
8719 mutex_unlock(&priv->mutex);
8721 return 0;
8724 #endif /* CONFIG_PM */
8726 /*****************************************************************************
8728 * driver and module entry point
8730 *****************************************************************************/
8732 static struct pci_driver iwl_driver = {
8733 .name = DRV_NAME,
8734 .id_table = iwl_hw_card_ids,
8735 .probe = iwl_pci_probe,
8736 .remove = __devexit_p(iwl_pci_remove),
8737 #ifdef CONFIG_PM
8738 .suspend = iwl_pci_suspend,
8739 .resume = iwl_pci_resume,
8740 #endif
8743 static int __init iwl_init(void)
8746 int ret;
8747 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8748 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
8749 ret = pci_register_driver(&iwl_driver);
8750 if (ret) {
8751 IWL_ERROR("Unable to initialize PCI module\n");
8752 return ret;
8754 #ifdef CONFIG_IWLWIFI_DEBUG
8755 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
8756 if (ret) {
8757 IWL_ERROR("Unable to create driver sysfs file\n");
8758 pci_unregister_driver(&iwl_driver);
8759 return ret;
8761 #endif
8763 return ret;
8766 static void __exit iwl_exit(void)
8768 #ifdef CONFIG_IWLWIFI_DEBUG
8769 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
8770 #endif
8771 pci_unregister_driver(&iwl_driver);
8774 module_param_named(antenna, iwl_param_antenna, int, 0444);
8775 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
8776 module_param_named(disable, iwl_param_disable, int, 0444);
8777 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
8778 module_param_named(hwcrypto, iwl_param_hwcrypto, int, 0444);
8779 MODULE_PARM_DESC(hwcrypto,
8780 "using hardware crypto engine (default 0 [software])\n");
8781 module_param_named(debug, iwl_param_debug, int, 0444);
8782 MODULE_PARM_DESC(debug, "debug output mask");
8783 module_param_named(disable_hw_scan, iwl_param_disable_hw_scan, int, 0444);
8784 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8786 module_param_named(queues_num, iwl_param_queues_num, int, 0444);
8787 MODULE_PARM_DESC(queues_num, "number of hw queues.");
8789 /* QoS */
8790 module_param_named(qos_enable, iwl_param_qos_enable, int, 0444);
8791 MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
8793 module_exit(iwl_exit);
8794 module_init(iwl_init);