blob: 3cccb3db6ff701a7863e5ae64f7de5db4a4f1482 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
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.
11 *
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.
16 *
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
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
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
27 *
28 *****************************************************************************/
29
30/*
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
34 *
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
38 */
39
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>
Zhu Yib481de92007-09-25 17:54:57 -070051#include <linux/etherdevice.h>
52#include <linux/if_arp.h>
53
54#include <net/ieee80211_radiotap.h>
55#include <net/mac80211.h>
56
57#include <asm/div64.h>
58
Zhu Yi1156b2c2007-09-25 19:34:09 -070059#define IWL 3945
60
Zhu Yib481de92007-09-25 17:54:57 -070061#include "iwlwifi.h"
62#include "iwl-3945.h"
63#include "iwl-helpers.h"
64
65#ifdef CONFIG_IWLWIFI_DEBUG
66u32 iwl_debug_level;
67#endif
68
Christoph Hellwig416e1432007-10-25 17:15:49 +080069static int iwl_tx_queue_update_write_ptr(struct iwl_priv *priv,
70 struct iwl_tx_queue *txq);
71
Zhu Yib481de92007-09-25 17:54:57 -070072/******************************************************************************
73 *
74 * module boiler plate
75 *
76 ******************************************************************************/
77
78/* module parameters */
Christoph Hellwig416e1432007-10-25 17:15:49 +080079static int iwl_param_disable_hw_scan;
80static int iwl_param_debug;
81static int iwl_param_disable; /* def: enable radio */
82static int iwl_param_antenna; /* def: 0 = both antennas (use diversity) */
Zhu Yib481de92007-09-25 17:54:57 -070083int iwl_param_hwcrypto; /* def: using software encryption */
Christoph Hellwig416e1432007-10-25 17:15:49 +080084static int iwl_param_qos_enable = 1;
Zhu Yib481de92007-09-25 17:54:57 -070085int iwl_param_queues_num = IWL_MAX_NUM_QUEUES;
86
87/*
88 * module name, copyright, version, etc.
89 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
90 */
91
92#define DRV_DESCRIPTION \
93"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
94
95#ifdef CONFIG_IWLWIFI_DEBUG
96#define VD "d"
97#else
98#define VD
99#endif
100
101#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
102#define VS "s"
103#else
104#define VS
105#endif
106
Zhu Yi80f3e022007-10-25 17:15:48 +0800107#define IWLWIFI_VERSION "1.1.19k" VD VS
Zhu Yib481de92007-09-25 17:54:57 -0700108#define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
109#define DRV_VERSION IWLWIFI_VERSION
110
111/* Change firmware file name, using "-" and incrementing number,
112 * *only* when uCode interface or architecture changes so that it
113 * is not compatible with earlier drivers.
114 * This number will also appear in << 8 position of 1st dword of uCode file */
115#define IWL3945_UCODE_API "-1"
116
117MODULE_DESCRIPTION(DRV_DESCRIPTION);
118MODULE_VERSION(DRV_VERSION);
119MODULE_AUTHOR(DRV_COPYRIGHT);
120MODULE_LICENSE("GPL");
121
Christoph Hellwig416e1432007-10-25 17:15:49 +0800122static __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -0700123{
124 u16 fc = le16_to_cpu(hdr->frame_control);
125 int hdr_len = ieee80211_get_hdrlen(fc);
126
127 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
128 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
129 return NULL;
130}
131
132static const struct ieee80211_hw_mode *iwl_get_hw_mode(
133 struct iwl_priv *priv, int mode)
134{
135 int i;
136
137 for (i = 0; i < 3; i++)
138 if (priv->modes[i].mode == mode)
139 return &priv->modes[i];
140
141 return NULL;
142}
143
144static int iwl_is_empty_essid(const char *essid, int essid_len)
145{
146 /* Single white space is for Linksys APs */
147 if (essid_len == 1 && essid[0] == ' ')
148 return 1;
149
150 /* Otherwise, if the entire essid is 0, we assume it is hidden */
151 while (essid_len) {
152 essid_len--;
153 if (essid[essid_len] != '\0')
154 return 0;
155 }
156
157 return 1;
158}
159
160static const char *iwl_escape_essid(const char *essid, u8 essid_len)
161{
162 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
163 const char *s = essid;
164 char *d = escaped;
165
166 if (iwl_is_empty_essid(essid, essid_len)) {
167 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
168 return escaped;
169 }
170
171 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
172 while (essid_len--) {
173 if (*s == '\0') {
174 *d++ = '\\';
175 *d++ = '0';
176 s++;
177 } else
178 *d++ = *s++;
179 }
180 *d = '\0';
181 return escaped;
182}
183
184static void iwl_print_hex_dump(int level, void *p, u32 len)
185{
186#ifdef CONFIG_IWLWIFI_DEBUG
187 if (!(iwl_debug_level & level))
188 return;
189
190 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
191 p, len, 1);
192#endif
193}
194
195/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
196 * DMA services
197 *
198 * Theory of operation
199 *
200 * A queue is a circular buffers with 'Read' and 'Write' pointers.
201 * 2 empty entries always kept in the buffer to protect from overflow.
202 *
203 * For Tx queue, there are low mark and high mark limits. If, after queuing
204 * the packet for Tx, free space become < low mark, Tx queue stopped. When
205 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
206 * Tx queue resumed.
207 *
Zhu Yi583fab32007-09-27 11:27:30 +0800208 * The IWL operates with six queues, one receive queue in the device's
Zhu Yib481de92007-09-25 17:54:57 -0700209 * sram, one transmit queue for sending commands to the device firmware,
210 * and four transmit queues for data.
211 ***************************************************/
212
213static int iwl_queue_space(const struct iwl_queue *q)
214{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800215 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700216
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800217 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700218 s -= q->n_bd;
219
220 if (s <= 0)
221 s += q->n_window;
222 /* keep some reserve to not confuse empty and full situations */
223 s -= 2;
224 if (s < 0)
225 s = 0;
226 return s;
227}
228
229/* XXX: n_bd must be power-of-two size */
230static inline int iwl_queue_inc_wrap(int index, int n_bd)
231{
232 return ++index & (n_bd - 1);
233}
234
235/* XXX: n_bd must be power-of-two size */
236static inline int iwl_queue_dec_wrap(int index, int n_bd)
237{
238 return --index & (n_bd - 1);
239}
240
241static inline int x2_queue_used(const struct iwl_queue *q, int i)
242{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800243 return q->write_ptr > q->read_ptr ?
244 (i >= q->read_ptr && i < q->write_ptr) :
245 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700246}
247
248static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
249{
250 if (is_huge)
251 return q->n_window;
252
253 return index & (q->n_window - 1);
254}
255
256static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
257 int count, int slots_num, u32 id)
258{
259 q->n_bd = count;
260 q->n_window = slots_num;
261 q->id = id;
262
263 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
264 * and iwl_queue_dec_wrap are broken. */
265 BUG_ON(!is_power_of_2(count));
266
267 /* slots_num must be power-of-two size, otherwise
268 * get_cmd_index is broken. */
269 BUG_ON(!is_power_of_2(slots_num));
270
271 q->low_mark = q->n_window / 4;
272 if (q->low_mark < 4)
273 q->low_mark = 4;
274
275 q->high_mark = q->n_window / 8;
276 if (q->high_mark < 2)
277 q->high_mark = 2;
278
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800279 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700280
281 return 0;
282}
283
284static int iwl_tx_queue_alloc(struct iwl_priv *priv,
285 struct iwl_tx_queue *txq, u32 id)
286{
287 struct pci_dev *dev = priv->pci_dev;
288
289 if (id != IWL_CMD_QUEUE_NUM) {
290 txq->txb = kmalloc(sizeof(txq->txb[0]) *
291 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
292 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800293 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700294 "structures failed\n");
295 goto error;
296 }
297 } else
298 txq->txb = NULL;
299
300 txq->bd = pci_alloc_consistent(dev,
301 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
302 &txq->q.dma_addr);
303
304 if (!txq->bd) {
305 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
306 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
307 goto error;
308 }
309 txq->q.id = id;
310
311 return 0;
312
313 error:
314 if (txq->txb) {
315 kfree(txq->txb);
316 txq->txb = NULL;
317 }
318
319 return -ENOMEM;
320}
321
322int iwl_tx_queue_init(struct iwl_priv *priv,
323 struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
324{
325 struct pci_dev *dev = priv->pci_dev;
326 int len;
327 int rc = 0;
328
Ian Schram01ebd062007-10-25 17:15:22 +0800329 /* allocate command space + one big command for scan since scan
Zhu Yib481de92007-09-25 17:54:57 -0700330 * command is very huge the system will not have two scan at the
331 * same time */
332 len = sizeof(struct iwl_cmd) * slots_num;
333 if (txq_id == IWL_CMD_QUEUE_NUM)
334 len += IWL_MAX_SCAN_SIZE;
335 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
336 if (!txq->cmd)
337 return -ENOMEM;
338
339 rc = iwl_tx_queue_alloc(priv, txq, txq_id);
340 if (rc) {
341 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
342
343 return -ENOMEM;
344 }
345 txq->need_update = 0;
346
347 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
348 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
349 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
350 iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
351
352 iwl_hw_tx_queue_init(priv, txq);
353
354 return 0;
355}
356
357/**
358 * iwl_tx_queue_free - Deallocate DMA queue.
359 * @txq: Transmit queue to deallocate.
360 *
361 * Empty queue by removing and destroying all BD's.
362 * Free all buffers. txq itself is not freed.
363 *
364 */
365void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
366{
367 struct iwl_queue *q = &txq->q;
368 struct pci_dev *dev = priv->pci_dev;
369 int len;
370
371 if (q->n_bd == 0)
372 return;
373
374 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800375 for (; q->write_ptr != q->read_ptr;
376 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Zhu Yib481de92007-09-25 17:54:57 -0700377 iwl_hw_txq_free_tfd(priv, txq);
378
379 len = sizeof(struct iwl_cmd) * q->n_window;
380 if (q->id == IWL_CMD_QUEUE_NUM)
381 len += IWL_MAX_SCAN_SIZE;
382
383 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
384
385 /* free buffers belonging to queue itself */
386 if (txq->q.n_bd)
387 pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
388 txq->q.n_bd, txq->bd, txq->q.dma_addr);
389
390 if (txq->txb) {
391 kfree(txq->txb);
392 txq->txb = NULL;
393 }
394
395 /* 0 fill whole structure */
396 memset(txq, 0, sizeof(*txq));
397}
398
399const u8 BROADCAST_ADDR[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
400
401/*************** STATION TABLE MANAGEMENT ****
402 *
403 * NOTE: This needs to be overhauled to better synchronize between
404 * how the iwl-4965.c is using iwl_hw_find_station vs. iwl-3945.c
405 *
406 * mac80211 should also be examined to determine if sta_info is duplicating
407 * the functionality provided here
408 */
409
410/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800411#if 0 /* temporary disable till we add real remove station */
Zhu Yib481de92007-09-25 17:54:57 -0700412static u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
413{
414 int index = IWL_INVALID_STATION;
415 int i;
416 unsigned long flags;
417
418 spin_lock_irqsave(&priv->sta_lock, flags);
419
420 if (is_ap)
421 index = IWL_AP_ID;
422 else if (is_broadcast_ether_addr(addr))
423 index = priv->hw_setting.bcast_sta_id;
424 else
425 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
426 if (priv->stations[i].used &&
427 !compare_ether_addr(priv->stations[i].sta.sta.addr,
428 addr)) {
429 index = i;
430 break;
431 }
432
433 if (unlikely(index == IWL_INVALID_STATION))
434 goto out;
435
436 if (priv->stations[index].used) {
437 priv->stations[index].used = 0;
438 priv->num_stations--;
439 }
440
441 BUG_ON(priv->num_stations < 0);
442
443out:
444 spin_unlock_irqrestore(&priv->sta_lock, flags);
445 return 0;
446}
Zhu Yi556f8db2007-09-27 11:27:33 +0800447#endif
Zhu Yib481de92007-09-25 17:54:57 -0700448static void iwl_clear_stations_table(struct iwl_priv *priv)
449{
450 unsigned long flags;
451
452 spin_lock_irqsave(&priv->sta_lock, flags);
453
454 priv->num_stations = 0;
455 memset(priv->stations, 0, sizeof(priv->stations));
456
457 spin_unlock_irqrestore(&priv->sta_lock, flags);
458}
459
460
461u8 iwl_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
462{
463 int i;
464 int index = IWL_INVALID_STATION;
465 struct iwl_station_entry *station;
466 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700467 DECLARE_MAC_BUF(mac);
Zhu Yic14c5212007-09-27 11:27:35 +0800468 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700469
470 spin_lock_irqsave(&priv->sta_lock, flags_spin);
471 if (is_ap)
472 index = IWL_AP_ID;
473 else if (is_broadcast_ether_addr(addr))
474 index = priv->hw_setting.bcast_sta_id;
475 else
476 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
477 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
478 addr)) {
479 index = i;
480 break;
481 }
482
483 if (!priv->stations[i].used &&
484 index == IWL_INVALID_STATION)
485 index = i;
486 }
487
Ian Schram01ebd062007-10-25 17:15:22 +0800488 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700489 since they have different meaning */
490 if (unlikely(index == IWL_INVALID_STATION)) {
491 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
492 return index;
493 }
494
495 if (priv->stations[index].used &&
496 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
497 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
498 return index;
499 }
500
Joe Perches0795af52007-10-03 17:59:30 -0700501 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700502 station = &priv->stations[index];
503 station->used = 1;
504 priv->num_stations++;
505
506 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
507 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
508 station->sta.mode = 0;
509 station->sta.sta.sta_id = index;
510 station->sta.station_flags = 0;
511
Tomas Winkler69946332007-10-25 17:15:27 +0800512 if (priv->phymode == MODE_IEEE80211A)
513 rate = IWL_RATE_6M_PLCP;
514 else
515 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800516
517 /* Turn on both antennas for the station... */
518 station->sta.rate_n_flags =
519 iwl_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
520 station->current_rate.rate_n_flags =
521 le16_to_cpu(station->sta.rate_n_flags);
522
Zhu Yib481de92007-09-25 17:54:57 -0700523 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
524 iwl_send_add_station(priv, &station->sta, flags);
525 return index;
526
527}
528
529/*************** DRIVER STATUS FUNCTIONS *****/
530
531static inline int iwl_is_ready(struct iwl_priv *priv)
532{
533 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
534 * set but EXIT_PENDING is not */
535 return test_bit(STATUS_READY, &priv->status) &&
536 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
537 !test_bit(STATUS_EXIT_PENDING, &priv->status);
538}
539
540static inline int iwl_is_alive(struct iwl_priv *priv)
541{
542 return test_bit(STATUS_ALIVE, &priv->status);
543}
544
545static inline int iwl_is_init(struct iwl_priv *priv)
546{
547 return test_bit(STATUS_INIT, &priv->status);
548}
549
550static inline int iwl_is_rfkill(struct iwl_priv *priv)
551{
552 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
553 test_bit(STATUS_RF_KILL_SW, &priv->status);
554}
555
556static inline int iwl_is_ready_rf(struct iwl_priv *priv)
557{
558
559 if (iwl_is_rfkill(priv))
560 return 0;
561
562 return iwl_is_ready(priv);
563}
564
565/*************** HOST COMMAND QUEUE FUNCTIONS *****/
566
567#define IWL_CMD(x) case x : return #x
568
569static const char *get_cmd_string(u8 cmd)
570{
571 switch (cmd) {
572 IWL_CMD(REPLY_ALIVE);
573 IWL_CMD(REPLY_ERROR);
574 IWL_CMD(REPLY_RXON);
575 IWL_CMD(REPLY_RXON_ASSOC);
576 IWL_CMD(REPLY_QOS_PARAM);
577 IWL_CMD(REPLY_RXON_TIMING);
578 IWL_CMD(REPLY_ADD_STA);
579 IWL_CMD(REPLY_REMOVE_STA);
580 IWL_CMD(REPLY_REMOVE_ALL_STA);
581 IWL_CMD(REPLY_3945_RX);
582 IWL_CMD(REPLY_TX);
583 IWL_CMD(REPLY_RATE_SCALE);
584 IWL_CMD(REPLY_LEDS_CMD);
585 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
586 IWL_CMD(RADAR_NOTIFICATION);
587 IWL_CMD(REPLY_QUIET_CMD);
588 IWL_CMD(REPLY_CHANNEL_SWITCH);
589 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
590 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
591 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
592 IWL_CMD(POWER_TABLE_CMD);
593 IWL_CMD(PM_SLEEP_NOTIFICATION);
594 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
595 IWL_CMD(REPLY_SCAN_CMD);
596 IWL_CMD(REPLY_SCAN_ABORT_CMD);
597 IWL_CMD(SCAN_START_NOTIFICATION);
598 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
599 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
600 IWL_CMD(BEACON_NOTIFICATION);
601 IWL_CMD(REPLY_TX_BEACON);
602 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
603 IWL_CMD(QUIET_NOTIFICATION);
604 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
605 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
606 IWL_CMD(REPLY_BT_CONFIG);
607 IWL_CMD(REPLY_STATISTICS_CMD);
608 IWL_CMD(STATISTICS_NOTIFICATION);
609 IWL_CMD(REPLY_CARD_STATE_CMD);
610 IWL_CMD(CARD_STATE_NOTIFICATION);
611 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
612 default:
613 return "UNKNOWN";
614
615 }
616}
617
618#define HOST_COMPLETE_TIMEOUT (HZ / 2)
619
620/**
621 * iwl_enqueue_hcmd - enqueue a uCode command
622 * @priv: device private data point
623 * @cmd: a point to the ucode command structure
624 *
625 * The function returns < 0 values to indicate the operation is
626 * failed. On success, it turns the index (> 0) of command in the
627 * command queue.
628 */
629static int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
630{
631 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
632 struct iwl_queue *q = &txq->q;
633 struct iwl_tfd_frame *tfd;
634 u32 *control_flags;
635 struct iwl_cmd *out_cmd;
636 u32 idx;
637 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
638 dma_addr_t phys_addr;
639 int pad;
640 u16 count;
641 int ret;
642 unsigned long flags;
643
644 /* If any of the command structures end up being larger than
645 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
646 * we will need to increase the size of the TFD entries */
647 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
648 !(cmd->meta.flags & CMD_SIZE_HUGE));
649
650 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
651 IWL_ERROR("No space for Tx\n");
652 return -ENOSPC;
653 }
654
655 spin_lock_irqsave(&priv->hcmd_lock, flags);
656
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800657 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700658 memset(tfd, 0, sizeof(*tfd));
659
660 control_flags = (u32 *) tfd;
661
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800662 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700663 out_cmd = &txq->cmd[idx];
664
665 out_cmd->hdr.cmd = cmd->id;
666 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
667 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
668
669 /* At this point, the out_cmd now has all of the incoming cmd
670 * information */
671
672 out_cmd->hdr.flags = 0;
673 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800674 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700675 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
676 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
677
678 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
679 offsetof(struct iwl_cmd, hdr);
680 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
681
682 pad = U32_PAD(cmd->len);
683 count = TFD_CTL_COUNT_GET(*control_flags);
684 *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
685
686 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
687 "%d bytes at %d[%d]:%d\n",
688 get_cmd_string(out_cmd->hdr.cmd),
689 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800690 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700691
692 txq->need_update = 1;
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800693 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Zhu Yib481de92007-09-25 17:54:57 -0700694 ret = iwl_tx_queue_update_write_ptr(priv, txq);
695
696 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
697 return ret ? ret : idx;
698}
699
Christoph Hellwig416e1432007-10-25 17:15:49 +0800700static int iwl_send_cmd_async(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700701{
702 int ret;
703
704 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
705
706 /* An asynchronous command can not expect an SKB to be set. */
707 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
708
709 /* An asynchronous command MUST have a callback. */
710 BUG_ON(!cmd->meta.u.callback);
711
712 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
713 return -EBUSY;
714
715 ret = iwl_enqueue_hcmd(priv, cmd);
716 if (ret < 0) {
717 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
718 get_cmd_string(cmd->id), ret);
719 return ret;
720 }
721 return 0;
722}
723
Christoph Hellwig416e1432007-10-25 17:15:49 +0800724static int iwl_send_cmd_sync(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700725{
726 int cmd_idx;
727 int ret;
728 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
729
730 BUG_ON(cmd->meta.flags & CMD_ASYNC);
731
732 /* A synchronous command can not have a callback set. */
733 BUG_ON(cmd->meta.u.callback != NULL);
734
735 if (atomic_xchg(&entry, 1)) {
736 IWL_ERROR("Error sending %s: Already sending a host command\n",
737 get_cmd_string(cmd->id));
738 return -EBUSY;
739 }
740
741 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
742
743 if (cmd->meta.flags & CMD_WANT_SKB)
744 cmd->meta.source = &cmd->meta;
745
746 cmd_idx = iwl_enqueue_hcmd(priv, cmd);
747 if (cmd_idx < 0) {
748 ret = cmd_idx;
749 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
750 get_cmd_string(cmd->id), ret);
751 goto out;
752 }
753
754 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
755 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
756 HOST_COMPLETE_TIMEOUT);
757 if (!ret) {
758 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
759 IWL_ERROR("Error sending %s: time out after %dms.\n",
760 get_cmd_string(cmd->id),
761 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
762
763 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
764 ret = -ETIMEDOUT;
765 goto cancel;
766 }
767 }
768
769 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
770 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
771 get_cmd_string(cmd->id));
772 ret = -ECANCELED;
773 goto fail;
774 }
775 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
776 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
777 get_cmd_string(cmd->id));
778 ret = -EIO;
779 goto fail;
780 }
781 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
782 IWL_ERROR("Error: Response NULL in '%s'\n",
783 get_cmd_string(cmd->id));
784 ret = -EIO;
785 goto out;
786 }
787
788 ret = 0;
789 goto out;
790
791cancel:
792 if (cmd->meta.flags & CMD_WANT_SKB) {
793 struct iwl_cmd *qcmd;
794
795 /* Cancel the CMD_WANT_SKB flag for the cmd in the
796 * TX cmd queue. Otherwise in case the cmd comes
797 * in later, it will possibly set an invalid
798 * address (cmd->meta.source). */
799 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
800 qcmd->meta.flags &= ~CMD_WANT_SKB;
801 }
802fail:
803 if (cmd->meta.u.skb) {
804 dev_kfree_skb_any(cmd->meta.u.skb);
805 cmd->meta.u.skb = NULL;
806 }
807out:
808 atomic_set(&entry, 0);
809 return ret;
810}
811
812int iwl_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
813{
Zhu Yib481de92007-09-25 17:54:57 -0700814 if (cmd->meta.flags & CMD_ASYNC)
815 return iwl_send_cmd_async(priv, cmd);
816
817 return iwl_send_cmd_sync(priv, cmd);
818}
819
820int iwl_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
821{
822 struct iwl_host_cmd cmd = {
823 .id = id,
824 .len = len,
825 .data = data,
826 };
827
828 return iwl_send_cmd_sync(priv, &cmd);
829}
830
831static int __must_check iwl_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
832{
833 struct iwl_host_cmd cmd = {
834 .id = id,
835 .len = sizeof(val),
836 .data = &val,
837 };
838
839 return iwl_send_cmd_sync(priv, &cmd);
840}
841
842int iwl_send_statistics_request(struct iwl_priv *priv)
843{
844 return iwl_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
845}
846
847/**
Zhu Yib481de92007-09-25 17:54:57 -0700848 * iwl_set_rxon_channel - Set the phymode and channel values in staging RXON
849 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
850 * @channel: Any channel valid for the requested phymode
851
852 * In addition to setting the staging RXON, priv->phymode is also set.
853 *
854 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
855 * in the staging RXON flag structure based on the phymode
856 */
857static int iwl_set_rxon_channel(struct iwl_priv *priv, u8 phymode, u16 channel)
858{
859 if (!iwl_get_channel_info(priv, phymode, channel)) {
860 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
861 channel, phymode);
862 return -EINVAL;
863 }
864
865 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
866 (priv->phymode == phymode))
867 return 0;
868
869 priv->staging_rxon.channel = cpu_to_le16(channel);
870 if (phymode == MODE_IEEE80211A)
871 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
872 else
873 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
874
875 priv->phymode = phymode;
876
877 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
878
879 return 0;
880}
881
882/**
883 * iwl_check_rxon_cmd - validate RXON structure is valid
884 *
885 * NOTE: This is really only useful during development and can eventually
886 * be #ifdef'd out once the driver is stable and folks aren't actively
887 * making changes
888 */
889static int iwl_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
890{
891 int error = 0;
892 int counter = 1;
893
894 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
895 error |= le32_to_cpu(rxon->flags &
896 (RXON_FLG_TGJ_NARROW_BAND_MSK |
897 RXON_FLG_RADAR_DETECT_MSK));
898 if (error)
899 IWL_WARNING("check 24G fields %d | %d\n",
900 counter++, error);
901 } else {
902 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
903 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
904 if (error)
905 IWL_WARNING("check 52 fields %d | %d\n",
906 counter++, error);
907 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
908 if (error)
909 IWL_WARNING("check 52 CCK %d | %d\n",
910 counter++, error);
911 }
912 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
913 if (error)
914 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
915
916 /* make sure basic rates 6Mbps and 1Mbps are supported */
917 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
918 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
919 if (error)
920 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
921
922 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
923 if (error)
924 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
925
926 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
927 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
928 if (error)
929 IWL_WARNING("check CCK and short slot %d | %d\n",
930 counter++, error);
931
932 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
933 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
934 if (error)
935 IWL_WARNING("check CCK & auto detect %d | %d\n",
936 counter++, error);
937
938 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
939 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
940 if (error)
941 IWL_WARNING("check TGG and auto detect %d | %d\n",
942 counter++, error);
943
944 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
945 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
946 RXON_FLG_ANT_A_MSK)) == 0);
947 if (error)
948 IWL_WARNING("check antenna %d %d\n", counter++, error);
949
950 if (error)
951 IWL_WARNING("Tuning to channel %d\n",
952 le16_to_cpu(rxon->channel));
953
954 if (error) {
955 IWL_ERROR("Not a valid iwl_rxon_assoc_cmd field values\n");
956 return -1;
957 }
958 return 0;
959}
960
961/**
962 * iwl_full_rxon_required - determine if RXON_ASSOC can be used in RXON commit
Ian Schram01ebd062007-10-25 17:15:22 +0800963 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700964 *
965 * If the RXON structure is changing sufficient to require a new
966 * tune or to clear and reset the RXON_FILTER_ASSOC_MSK then return 1
967 * to indicate a new tune is required.
968 */
969static int iwl_full_rxon_required(struct iwl_priv *priv)
970{
971
972 /* These items are only settable from the full RXON command */
973 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
974 compare_ether_addr(priv->staging_rxon.bssid_addr,
975 priv->active_rxon.bssid_addr) ||
976 compare_ether_addr(priv->staging_rxon.node_addr,
977 priv->active_rxon.node_addr) ||
978 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
979 priv->active_rxon.wlap_bssid_addr) ||
980 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
981 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
982 (priv->staging_rxon.air_propagation !=
983 priv->active_rxon.air_propagation) ||
984 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
985 return 1;
986
987 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
988 * be updated with the RXON_ASSOC command -- however only some
989 * flag transitions are allowed using RXON_ASSOC */
990
991 /* Check if we are not switching bands */
992 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
993 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
994 return 1;
995
996 /* Check if we are switching association toggle */
997 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
998 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
999 return 1;
1000
1001 return 0;
1002}
1003
1004static int iwl_send_rxon_assoc(struct iwl_priv *priv)
1005{
1006 int rc = 0;
1007 struct iwl_rx_packet *res = NULL;
1008 struct iwl_rxon_assoc_cmd rxon_assoc;
1009 struct iwl_host_cmd cmd = {
1010 .id = REPLY_RXON_ASSOC,
1011 .len = sizeof(rxon_assoc),
1012 .meta.flags = CMD_WANT_SKB,
1013 .data = &rxon_assoc,
1014 };
1015 const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
1016 const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
1017
1018 if ((rxon1->flags == rxon2->flags) &&
1019 (rxon1->filter_flags == rxon2->filter_flags) &&
1020 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1021 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1022 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1023 return 0;
1024 }
1025
1026 rxon_assoc.flags = priv->staging_rxon.flags;
1027 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1028 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1029 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1030 rxon_assoc.reserved = 0;
1031
1032 rc = iwl_send_cmd_sync(priv, &cmd);
1033 if (rc)
1034 return rc;
1035
1036 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1037 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1038 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1039 rc = -EIO;
1040 }
1041
1042 priv->alloc_rxb_skb--;
1043 dev_kfree_skb_any(cmd.meta.u.skb);
1044
1045 return rc;
1046}
1047
1048/**
1049 * iwl_commit_rxon - commit staging_rxon to hardware
1050 *
Ian Schram01ebd062007-10-25 17:15:22 +08001051 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001052 * the active_rxon structure is updated with the new data. This
1053 * function correctly transitions out of the RXON_ASSOC_MSK state if
1054 * a HW tune is required based on the RXON structure changes.
1055 */
1056static int iwl_commit_rxon(struct iwl_priv *priv)
1057{
1058 /* cast away the const for active_rxon in this function */
1059 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
1060 int rc = 0;
Joe Perches0795af52007-10-03 17:59:30 -07001061 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001062
1063 if (!iwl_is_alive(priv))
1064 return -1;
1065
1066 /* always get timestamp with Rx frame */
1067 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1068
1069 /* select antenna */
1070 priv->staging_rxon.flags &=
1071 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
1072 priv->staging_rxon.flags |= iwl3945_get_antenna_flags(priv);
1073
1074 rc = iwl_check_rxon_cmd(&priv->staging_rxon);
1075 if (rc) {
1076 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1077 return -EINVAL;
1078 }
1079
1080 /* If we don't need to send a full RXON, we can use
1081 * iwl_rxon_assoc_cmd which is used to reconfigure filter
1082 * and other flags for the current radio configuration. */
1083 if (!iwl_full_rxon_required(priv)) {
1084 rc = iwl_send_rxon_assoc(priv);
1085 if (rc) {
1086 IWL_ERROR("Error setting RXON_ASSOC "
1087 "configuration (%d).\n", rc);
1088 return rc;
1089 }
1090
1091 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1092
1093 return 0;
1094 }
1095
1096 /* If we are currently associated and the new config requires
1097 * an RXON_ASSOC and the new config wants the associated mask enabled,
1098 * we must clear the associated from the active configuration
1099 * before we apply the new config */
1100 if (iwl_is_associated(priv) &&
1101 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1102 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1103 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1104
1105 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1106 sizeof(struct iwl_rxon_cmd),
1107 &priv->active_rxon);
1108
1109 /* If the mask clearing failed then we set
1110 * active_rxon back to what it was previously */
1111 if (rc) {
1112 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1113 IWL_ERROR("Error clearing ASSOC_MSK on current "
1114 "configuration (%d).\n", rc);
1115 return rc;
1116 }
Zhu Yib481de92007-09-25 17:54:57 -07001117 }
1118
1119 IWL_DEBUG_INFO("Sending RXON\n"
1120 "* with%s RXON_FILTER_ASSOC_MSK\n"
1121 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001122 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001123 ((priv->staging_rxon.filter_flags &
1124 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1125 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001126 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001127
1128 /* Apply the new configuration */
1129 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1130 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
1131 if (rc) {
1132 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1133 return rc;
1134 }
1135
1136 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1137
Zhu Yi556f8db2007-09-27 11:27:33 +08001138 iwl_clear_stations_table(priv);
1139
Zhu Yib481de92007-09-25 17:54:57 -07001140 /* If we issue a new RXON command which required a tune then we must
1141 * send a new TXPOWER command or we won't be able to Tx any frames */
1142 rc = iwl_hw_reg_send_txpower(priv);
1143 if (rc) {
1144 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1145 return rc;
1146 }
1147
1148 /* Add the broadcast address so we can send broadcast frames */
Zhu Yi556f8db2007-09-27 11:27:33 +08001149 if (iwl_add_station(priv, BROADCAST_ADDR, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001150 IWL_INVALID_STATION) {
1151 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1152 return -EIO;
1153 }
1154
1155 /* If we have set the ASSOC_MSK and we are in BSS mode then
1156 * add the IWL_AP_ID to the station rate table */
1157 if (iwl_is_associated(priv) &&
1158 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
Zhu Yi556f8db2007-09-27 11:27:33 +08001159 if (iwl_add_station(priv, priv->active_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -07001160 == IWL_INVALID_STATION) {
1161 IWL_ERROR("Error adding AP address for transmit.\n");
1162 return -EIO;
1163 }
1164
1165 /* Init the hardware's rate fallback order based on the
1166 * phymode */
1167 rc = iwl3945_init_hw_rate_table(priv);
1168 if (rc) {
1169 IWL_ERROR("Error setting HW rate table: %02X\n", rc);
1170 return -EIO;
1171 }
1172
1173 return 0;
1174}
1175
1176static int iwl_send_bt_config(struct iwl_priv *priv)
1177{
1178 struct iwl_bt_cmd bt_cmd = {
1179 .flags = 3,
1180 .lead_time = 0xAA,
1181 .max_kill = 1,
1182 .kill_ack_mask = 0,
1183 .kill_cts_mask = 0,
1184 };
1185
1186 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1187 sizeof(struct iwl_bt_cmd), &bt_cmd);
1188}
1189
1190static int iwl_send_scan_abort(struct iwl_priv *priv)
1191{
1192 int rc = 0;
1193 struct iwl_rx_packet *res;
1194 struct iwl_host_cmd cmd = {
1195 .id = REPLY_SCAN_ABORT_CMD,
1196 .meta.flags = CMD_WANT_SKB,
1197 };
1198
1199 /* If there isn't a scan actively going on in the hardware
1200 * then we are in between scan bands and not actually
1201 * actively scanning, so don't send the abort command */
1202 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1203 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1204 return 0;
1205 }
1206
1207 rc = iwl_send_cmd_sync(priv, &cmd);
1208 if (rc) {
1209 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1210 return rc;
1211 }
1212
1213 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1214 if (res->u.status != CAN_ABORT_STATUS) {
1215 /* The scan abort will return 1 for success or
1216 * 2 for "failure". A failure condition can be
1217 * due to simply not being in an active scan which
1218 * can occur if we send the scan abort before we
1219 * the microcode has notified us that a scan is
1220 * completed. */
1221 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1222 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1223 clear_bit(STATUS_SCAN_HW, &priv->status);
1224 }
1225
1226 dev_kfree_skb_any(cmd.meta.u.skb);
1227
1228 return rc;
1229}
1230
1231static int iwl_card_state_sync_callback(struct iwl_priv *priv,
1232 struct iwl_cmd *cmd,
1233 struct sk_buff *skb)
1234{
1235 return 1;
1236}
1237
1238/*
1239 * CARD_STATE_CMD
1240 *
1241 * Use: Sets the internal card state to enable, disable, or halt
1242 *
1243 * When in the 'enable' state the card operates as normal.
1244 * When in the 'disable' state, the card enters into a low power mode.
1245 * When in the 'halt' state, the card is shut down and must be fully
1246 * restarted to come back on.
1247 */
1248static int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
1249{
1250 struct iwl_host_cmd cmd = {
1251 .id = REPLY_CARD_STATE_CMD,
1252 .len = sizeof(u32),
1253 .data = &flags,
1254 .meta.flags = meta_flag,
1255 };
1256
1257 if (meta_flag & CMD_ASYNC)
1258 cmd.meta.u.callback = iwl_card_state_sync_callback;
1259
1260 return iwl_send_cmd(priv, &cmd);
1261}
1262
1263static int iwl_add_sta_sync_callback(struct iwl_priv *priv,
1264 struct iwl_cmd *cmd, struct sk_buff *skb)
1265{
1266 struct iwl_rx_packet *res = NULL;
1267
1268 if (!skb) {
1269 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1270 return 1;
1271 }
1272
1273 res = (struct iwl_rx_packet *)skb->data;
1274 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1275 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1276 res->hdr.flags);
1277 return 1;
1278 }
1279
1280 switch (res->u.add_sta.status) {
1281 case ADD_STA_SUCCESS_MSK:
1282 break;
1283 default:
1284 break;
1285 }
1286
1287 /* We didn't cache the SKB; let the caller free it */
1288 return 1;
1289}
1290
1291int iwl_send_add_station(struct iwl_priv *priv,
1292 struct iwl_addsta_cmd *sta, u8 flags)
1293{
1294 struct iwl_rx_packet *res = NULL;
1295 int rc = 0;
1296 struct iwl_host_cmd cmd = {
1297 .id = REPLY_ADD_STA,
1298 .len = sizeof(struct iwl_addsta_cmd),
1299 .meta.flags = flags,
1300 .data = sta,
1301 };
1302
1303 if (flags & CMD_ASYNC)
1304 cmd.meta.u.callback = iwl_add_sta_sync_callback;
1305 else
1306 cmd.meta.flags |= CMD_WANT_SKB;
1307
1308 rc = iwl_send_cmd(priv, &cmd);
1309
1310 if (rc || (flags & CMD_ASYNC))
1311 return rc;
1312
1313 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1314 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1315 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1316 res->hdr.flags);
1317 rc = -EIO;
1318 }
1319
1320 if (rc == 0) {
1321 switch (res->u.add_sta.status) {
1322 case ADD_STA_SUCCESS_MSK:
1323 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1324 break;
1325 default:
1326 rc = -EIO;
1327 IWL_WARNING("REPLY_ADD_STA failed\n");
1328 break;
1329 }
1330 }
1331
1332 priv->alloc_rxb_skb--;
1333 dev_kfree_skb_any(cmd.meta.u.skb);
1334
1335 return rc;
1336}
1337
1338static int iwl_update_sta_key_info(struct iwl_priv *priv,
1339 struct ieee80211_key_conf *keyconf,
1340 u8 sta_id)
1341{
1342 unsigned long flags;
1343 __le16 key_flags = 0;
1344
1345 switch (keyconf->alg) {
1346 case ALG_CCMP:
1347 key_flags |= STA_KEY_FLG_CCMP;
1348 key_flags |= cpu_to_le16(
1349 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1350 key_flags &= ~STA_KEY_FLG_INVALID;
1351 break;
1352 case ALG_TKIP:
1353 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001354 default:
1355 return -EINVAL;
1356 }
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);
1362
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;
1368
1369 spin_unlock_irqrestore(&priv->sta_lock, flags);
1370
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;
1374}
1375
1376static int iwl_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
1377{
1378 unsigned long flags;
1379
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);
1387
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;
1391}
1392
1393static void iwl_clear_free_frames(struct iwl_priv *priv)
1394{
1395 struct list_head *element;
1396
1397 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1398 priv->frames_count);
1399
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--;
1405 }
1406
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;
1411 }
1412}
1413
1414static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
1415{
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;
1423 }
1424
1425 priv->frames_count++;
1426 return frame;
1427 }
1428
1429 element = priv->free_frames.next;
1430 list_del(element);
1431 return list_entry(element, struct iwl_frame, list);
1432}
1433
1434static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
1435{
1436 memset(frame, 0, sizeof(*frame));
1437 list_add(&frame->list, &priv->free_frames);
1438}
1439
1440unsigned int iwl_fill_beacon_frame(struct iwl_priv *priv,
1441 struct ieee80211_hdr *hdr,
1442 const u8 *dest, int left)
1443{
1444
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;
1449
1450 if (priv->ibss_beacon->len > left)
1451 return 0;
1452
1453 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1454
1455 return priv->ibss_beacon->len;
1456}
1457
1458static int iwl_rate_index_from_plcp(int plcp)
1459{
1460 int i = 0;
1461
1462 for (i = 0; i < IWL_RATE_COUNT; i++)
1463 if (iwl_rates[i].plcp == plcp)
1464 return i;
1465 return -1;
1466}
1467
1468static u8 iwl_rate_get_lowest_plcp(int rate_mask)
1469{
1470 u8 i;
1471
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;
1476 }
1477
1478 return IWL_RATE_INVALID;
1479}
1480
1481static int iwl_send_beacon_cmd(struct iwl_priv *priv)
1482{
1483 struct iwl_frame *frame;
1484 unsigned int frame_size;
1485 int rc;
1486 u8 rate;
1487
1488 frame = iwl_get_free_frame(priv);
1489
1490 if (!frame) {
1491 IWL_ERROR("Could not obtain free frame buffer for beacon "
1492 "command.\n");
1493 return -ENOMEM;
1494 }
1495
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;
1505 }
1506
1507 frame_size = iwl_hw_get_beacon_cmd(priv, frame, rate);
1508
1509 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
1510 &frame->u.cmd[0]);
1511
1512 iwl_free_frame(priv, frame);
1513
1514 return rc;
1515}
1516
1517/******************************************************************************
1518 *
1519 * EEPROM related functions
1520 *
1521 ******************************************************************************/
1522
1523static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
1524{
1525 memcpy(mac, priv->eeprom.mac_address, 6);
1526}
1527
1528/**
1529 * iwl_eeprom_init - read EEPROM contents
1530 *
1531 * Load the EEPROM from adapter into priv->eeprom
1532 *
1533 * NOTE: This routine uses the non-debug IO access functions.
1534 */
1535int iwl_eeprom_init(struct iwl_priv *priv)
1536{
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;
1544
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);
1550
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;
1554 }
1555
Ian Schram91e17472007-10-25 17:15:23 +08001556 rc = iwl_eeprom_acquire_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001557 if (rc < 0) {
Ian Schram91e17472007-10-25 17:15:23 +08001558 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001559 return -ENOENT;
1560 }
1561
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);
1566
1567 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1568 i += IWL_EEPROM_ACCESS_DELAY) {
Tomas Winklerac17a942007-10-25 17:15:37 +08001569 r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
Zhu Yib481de92007-09-25 17:54:57 -07001570 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1571 break;
1572 udelay(IWL_EEPROM_ACCESS_DELAY);
1573 }
1574
1575 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1576 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1577 return -ETIMEDOUT;
1578 }
1579 e[addr / 2] = le16_to_cpu(r >> 16);
1580 }
1581
1582 return 0;
1583}
1584
1585/******************************************************************************
1586 *
1587 * Misc. internal state and helper functions
1588 *
1589 ******************************************************************************/
1590#ifdef CONFIG_IWLWIFI_DEBUG
1591
1592/**
1593 * iwl_report_frame - dump frame to syslog during debug sessions
1594 *
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.
1598 *
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.
1603 */
1604void iwl_report_frame(struct iwl_priv *priv,
1605 struct iwl_rx_packet *pkt,
1606 struct ieee80211_hdr *header, int group100)
1607{
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);
1631
1632 /* MAC header */
1633 fc = le16_to_cpu(header->frame_control);
1634 seq_ctl = le16_to_cpu(header->seq_ctrl);
1635
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);
1641
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);
1647
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);
1653
1654 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1655
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;
1670 }
1671 } else {
1672 /* print summary for all other frames */
1673 print_summary = 1;
1674 }
1675
1676 if (print_summary) {
1677 char *title;
1678 u32 rate;
1679
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";
1704
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;
1710
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);
1728 }
1729 }
1730 if (print_dump)
1731 iwl_print_hex_dump(IWL_DL_RX, data, length);
1732}
1733#endif
1734
1735static void iwl_unset_hw_setting(struct iwl_priv *priv)
1736{
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);
1742}
1743
1744/**
1745 * iwl_supported_rate_to_ie - fill in the supported rate in IE field
1746 *
1747 * return : set the bit for each supported rate insert in ie
1748 */
1749static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001750 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001751{
1752 u16 ret_rates = 0, bit;
1753 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001754 u8 *cnt = ie;
1755 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001756
1757 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1758 if (bit & supported_rate) {
1759 ret_rates |= bit;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001760 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))
Zhu Yib481de92007-09-25 17:54:57 -07001766 break;
1767 }
1768 }
1769
1770 return ret_rates;
1771}
1772
1773/**
1774 * iwl_fill_probe_req - fill in all required fields and IE for probe request
1775 */
1776static u16 iwl_fill_probe_req(struct iwl_priv *priv,
1777 struct ieee80211_mgmt *frame,
1778 int left, int is_direct)
1779{
1780 int len = 0;
1781 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001782 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001783
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;
1790
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;
1796
1797 /* fill in our indirect SSID IE */
1798 /* ...next IE... */
1799
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;
1807
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;
1820 }
1821
1822 /* fill in supported rate */
1823 /* ...next IE... */
1824 left -= 2;
1825 if (left < 0)
1826 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001827
Zhu Yib481de92007-09-25 17:54:57 -07001828 /* ... fill it in... */
1829 *pos++ = WLAN_EID_SUPP_RATES;
1830 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001831
1832 priv->active_rate = priv->rates_mask;
1833 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001834 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1835
Tomas Winklerc7c46672007-10-18 02:04:15 +02001836 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;
1840
1841 ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
1842 priv->active_rate_basic, &left);
1843 active_rates &= ~ret_rates;
1844
Zhu Yib481de92007-09-25 17:54:57 -07001845 len += 2 + *pos;
1846 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001847 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001848 goto fill_end;
1849
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;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001858 iwl_supported_rate_to_ie(pos, active_rates,
1859 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001860 if (*pos > 0)
1861 len += 2 + *pos;
1862
1863 fill_end:
1864 return (u16)len;
1865}
1866
1867/*
1868 * QoS support
1869*/
1870#ifdef CONFIG_IWLWIFI_QOS
1871static int iwl_send_qos_params_command(struct iwl_priv *priv,
1872 struct iwl_qosparam_cmd *qos)
1873{
1874
1875 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1876 sizeof(struct iwl_qosparam_cmd), qos);
1877}
1878
1879static void iwl_reset_qos(struct iwl_priv *priv)
1880{
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;
1887
1888 spin_lock_irqsave(&priv->lock, flags);
1889 priv->qos_data.qos_active = 0;
1890
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;
1897 }
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;
1904 }
1905
1906 if (priv->qos_data.qos_active)
1907 aifs = 3;
1908
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;
1914
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;
1922
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;
1936
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;
1959 }
1960 }
1961 IWL_DEBUG_QOS("set QoS to default \n");
1962
1963 spin_unlock_irqrestore(&priv->lock, flags);
1964}
1965
1966static void iwl_activate_qos(struct iwl_priv *priv, u8 force)
1967{
1968 unsigned long flags;
1969
Zhu Yib481de92007-09-25 17:54:57 -07001970 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1971 return;
1972
1973 if (!priv->qos_data.qos_enable)
1974 return;
1975
1976 spin_lock_irqsave(&priv->lock, flags);
1977 priv->qos_data.def_qos_parm.qos_flags = 0;
1978
1979 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1980 !priv->qos_data.qos_cap.q_AP.txop_request)
1981 priv->qos_data.def_qos_parm.qos_flags |=
1982 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1983
1984 if (priv->qos_data.qos_active)
1985 priv->qos_data.def_qos_parm.qos_flags |=
1986 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1987
1988 spin_unlock_irqrestore(&priv->lock, flags);
1989
1990 if (force || iwl_is_associated(priv)) {
1991 IWL_DEBUG_QOS("send QoS cmd with Qos active %d \n",
1992 priv->qos_data.qos_active);
1993
1994 iwl_send_qos_params_command(priv,
1995 &(priv->qos_data.def_qos_parm));
1996 }
1997}
1998
1999#endif /* CONFIG_IWLWIFI_QOS */
2000/*
2001 * Power management (not Tx power!) functions
2002 */
2003#define MSEC_TO_USEC 1024
2004
2005#define NOSLP __constant_cpu_to_le32(0)
2006#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
2007#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2008#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2009 __constant_cpu_to_le32(X1), \
2010 __constant_cpu_to_le32(X2), \
2011 __constant_cpu_to_le32(X3), \
2012 __constant_cpu_to_le32(X4)}
2013
2014
2015/* default power management (not Tx power) table values */
2016/* for tim 0-10 */
2017static struct iwl_power_vec_entry range_0[IWL_POWER_AC] = {
2018 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2019 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2020 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2021 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2022 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2023 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2024};
2025
2026/* for tim > 10 */
2027static struct iwl_power_vec_entry range_1[IWL_POWER_AC] = {
2028 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2029 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2030 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2031 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2032 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2033 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2034 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2035 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2036 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2037 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2038};
2039
2040int iwl_power_init_handle(struct iwl_priv *priv)
2041{
2042 int rc = 0, i;
2043 struct iwl_power_mgr *pow_data;
2044 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
2045 u16 pci_pm;
2046
2047 IWL_DEBUG_POWER("Initialize power \n");
2048
2049 pow_data = &(priv->power_data);
2050
2051 memset(pow_data, 0, sizeof(*pow_data));
2052
2053 pow_data->active_index = IWL_POWER_RANGE_0;
2054 pow_data->dtim_val = 0xffff;
2055
2056 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2057 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2058
2059 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2060 if (rc != 0)
2061 return 0;
2062 else {
2063 struct iwl_powertable_cmd *cmd;
2064
2065 IWL_DEBUG_POWER("adjust power command flags\n");
2066
2067 for (i = 0; i < IWL_POWER_AC; i++) {
2068 cmd = &pow_data->pwr_range_0[i].cmd;
2069
2070 if (pci_pm & 0x1)
2071 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2072 else
2073 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2074 }
2075 }
2076 return rc;
2077}
2078
2079static int iwl_update_power_cmd(struct iwl_priv *priv,
2080 struct iwl_powertable_cmd *cmd, u32 mode)
2081{
2082 int rc = 0, i;
2083 u8 skip;
2084 u32 max_sleep = 0;
2085 struct iwl_power_vec_entry *range;
2086 u8 period = 0;
2087 struct iwl_power_mgr *pow_data;
2088
2089 if (mode > IWL_POWER_INDEX_5) {
2090 IWL_DEBUG_POWER("Error invalid power mode \n");
2091 return -1;
2092 }
2093 pow_data = &(priv->power_data);
2094
2095 if (pow_data->active_index == IWL_POWER_RANGE_0)
2096 range = &pow_data->pwr_range_0[0];
2097 else
2098 range = &pow_data->pwr_range_1[1];
2099
2100 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
2101
2102#ifdef IWL_MAC80211_DISABLE
2103 if (priv->assoc_network != NULL) {
2104 unsigned long flags;
2105
2106 period = priv->assoc_network->tim.tim_period;
2107 }
2108#endif /*IWL_MAC80211_DISABLE */
2109 skip = range[mode].no_dtim;
2110
2111 if (period == 0) {
2112 period = 1;
2113 skip = 0;
2114 }
2115
2116 if (skip == 0) {
2117 max_sleep = period;
2118 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2119 } else {
2120 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2121 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2122 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2123 }
2124
2125 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2126 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2127 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2128 }
2129
2130 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2131 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2132 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2133 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2134 le32_to_cpu(cmd->sleep_interval[0]),
2135 le32_to_cpu(cmd->sleep_interval[1]),
2136 le32_to_cpu(cmd->sleep_interval[2]),
2137 le32_to_cpu(cmd->sleep_interval[3]),
2138 le32_to_cpu(cmd->sleep_interval[4]));
2139
2140 return rc;
2141}
2142
2143static int iwl_send_power_mode(struct iwl_priv *priv, u32 mode)
2144{
2145 u32 final_mode = mode;
2146 int rc;
2147 struct iwl_powertable_cmd cmd;
2148
2149 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08002150 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07002151 * else user level */
2152 switch (mode) {
2153 case IWL_POWER_BATTERY:
2154 final_mode = IWL_POWER_INDEX_3;
2155 break;
2156 case IWL_POWER_AC:
2157 final_mode = IWL_POWER_MODE_CAM;
2158 break;
2159 default:
2160 final_mode = mode;
2161 break;
2162 }
2163
2164 iwl_update_power_cmd(priv, &cmd, final_mode);
2165
2166 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2167
2168 if (final_mode == IWL_POWER_MODE_CAM)
2169 clear_bit(STATUS_POWER_PMI, &priv->status);
2170 else
2171 set_bit(STATUS_POWER_PMI, &priv->status);
2172
2173 return rc;
2174}
2175
2176int iwl_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
2177{
2178 /* Filter incoming packets to determine if they are targeted toward
2179 * this network, discarding packets coming from ourselves */
2180 switch (priv->iw_mode) {
2181 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2182 /* packets from our adapter are dropped (echo) */
2183 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2184 return 0;
2185 /* {broad,multi}cast packets to our IBSS go through */
2186 if (is_multicast_ether_addr(header->addr1))
2187 return !compare_ether_addr(header->addr3, priv->bssid);
2188 /* packets to our adapter go through */
2189 return !compare_ether_addr(header->addr1, priv->mac_addr);
2190 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2191 /* packets from our adapter are dropped (echo) */
2192 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2193 return 0;
2194 /* {broad,multi}cast packets to our BSS go through */
2195 if (is_multicast_ether_addr(header->addr1))
2196 return !compare_ether_addr(header->addr2, priv->bssid);
2197 /* packets to our adapter go through */
2198 return !compare_ether_addr(header->addr1, priv->mac_addr);
2199 }
2200
2201 return 1;
2202}
2203
2204#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2205
Christoph Hellwig416e1432007-10-25 17:15:49 +08002206static const char *iwl_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002207{
2208 switch (status & TX_STATUS_MSK) {
2209 case TX_STATUS_SUCCESS:
2210 return "SUCCESS";
2211 TX_STATUS_ENTRY(SHORT_LIMIT);
2212 TX_STATUS_ENTRY(LONG_LIMIT);
2213 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2214 TX_STATUS_ENTRY(MGMNT_ABORT);
2215 TX_STATUS_ENTRY(NEXT_FRAG);
2216 TX_STATUS_ENTRY(LIFE_EXPIRE);
2217 TX_STATUS_ENTRY(DEST_PS);
2218 TX_STATUS_ENTRY(ABORTED);
2219 TX_STATUS_ENTRY(BT_RETRY);
2220 TX_STATUS_ENTRY(STA_INVALID);
2221 TX_STATUS_ENTRY(FRAG_DROPPED);
2222 TX_STATUS_ENTRY(TID_DISABLE);
2223 TX_STATUS_ENTRY(FRAME_FLUSHED);
2224 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2225 TX_STATUS_ENTRY(TX_LOCKED);
2226 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2227 }
2228
2229 return "UNKNOWN";
2230}
2231
2232/**
2233 * iwl_scan_cancel - Cancel any currently executing HW scan
2234 *
2235 * NOTE: priv->mutex is not required before calling this function
2236 */
2237static int iwl_scan_cancel(struct iwl_priv *priv)
2238{
2239 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2240 clear_bit(STATUS_SCANNING, &priv->status);
2241 return 0;
2242 }
2243
2244 if (test_bit(STATUS_SCANNING, &priv->status)) {
2245 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2246 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2247 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2248 queue_work(priv->workqueue, &priv->abort_scan);
2249
2250 } else
2251 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2252
2253 return test_bit(STATUS_SCANNING, &priv->status);
2254 }
2255
2256 return 0;
2257}
2258
2259/**
2260 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
2261 * @ms: amount of time to wait (in milliseconds) for scan to abort
2262 *
2263 * NOTE: priv->mutex must be held before calling this function
2264 */
2265static int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
2266{
2267 unsigned long now = jiffies;
2268 int ret;
2269
2270 ret = iwl_scan_cancel(priv);
2271 if (ret && ms) {
2272 mutex_unlock(&priv->mutex);
2273 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2274 test_bit(STATUS_SCANNING, &priv->status))
2275 msleep(1);
2276 mutex_lock(&priv->mutex);
2277
2278 return test_bit(STATUS_SCANNING, &priv->status);
2279 }
2280
2281 return ret;
2282}
2283
2284static void iwl_sequence_reset(struct iwl_priv *priv)
2285{
2286 /* Reset ieee stats */
2287
2288 /* We don't reset the net_device_stats (ieee->stats) on
2289 * re-association */
2290
2291 priv->last_seq_num = -1;
2292 priv->last_frag_num = -1;
2293 priv->last_packet_time = 0;
2294
2295 iwl_scan_cancel(priv);
2296}
2297
2298#define MAX_UCODE_BEACON_INTERVAL 1024
2299#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2300
2301static __le16 iwl_adjust_beacon_interval(u16 beacon_val)
2302{
2303 u16 new_val = 0;
2304 u16 beacon_factor = 0;
2305
2306 beacon_factor =
2307 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2308 / MAX_UCODE_BEACON_INTERVAL;
2309 new_val = beacon_val / beacon_factor;
2310
2311 return cpu_to_le16(new_val);
2312}
2313
2314static void iwl_setup_rxon_timing(struct iwl_priv *priv)
2315{
2316 u64 interval_tm_unit;
2317 u64 tsf, result;
2318 unsigned long flags;
2319 struct ieee80211_conf *conf = NULL;
2320 u16 beacon_int = 0;
2321
2322 conf = ieee80211_get_hw_conf(priv->hw);
2323
2324 spin_lock_irqsave(&priv->lock, flags);
2325 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2326 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2327
2328 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2329
2330 tsf = priv->timestamp1;
2331 tsf = ((tsf << 32) | priv->timestamp0);
2332
2333 beacon_int = priv->beacon_int;
2334 spin_unlock_irqrestore(&priv->lock, flags);
2335
2336 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2337 if (beacon_int == 0) {
2338 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2339 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2340 } else {
2341 priv->rxon_timing.beacon_interval =
2342 cpu_to_le16(beacon_int);
2343 priv->rxon_timing.beacon_interval =
2344 iwl_adjust_beacon_interval(
2345 le16_to_cpu(priv->rxon_timing.beacon_interval));
2346 }
2347
2348 priv->rxon_timing.atim_window = 0;
2349 } else {
2350 priv->rxon_timing.beacon_interval =
2351 iwl_adjust_beacon_interval(conf->beacon_int);
2352 /* TODO: we need to get atim_window from upper stack
2353 * for now we set to 0 */
2354 priv->rxon_timing.atim_window = 0;
2355 }
2356
2357 interval_tm_unit =
2358 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2359 result = do_div(tsf, interval_tm_unit);
2360 priv->rxon_timing.beacon_init_val =
2361 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2362
2363 IWL_DEBUG_ASSOC
2364 ("beacon interval %d beacon timer %d beacon tim %d\n",
2365 le16_to_cpu(priv->rxon_timing.beacon_interval),
2366 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2367 le16_to_cpu(priv->rxon_timing.atim_window));
2368}
2369
2370static int iwl_scan_initiate(struct iwl_priv *priv)
2371{
2372 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2373 IWL_ERROR("APs don't scan.\n");
2374 return 0;
2375 }
2376
2377 if (!iwl_is_ready_rf(priv)) {
2378 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2379 return -EIO;
2380 }
2381
2382 if (test_bit(STATUS_SCANNING, &priv->status)) {
2383 IWL_DEBUG_SCAN("Scan already in progress.\n");
2384 return -EAGAIN;
2385 }
2386
2387 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2388 IWL_DEBUG_SCAN("Scan request while abort pending. "
2389 "Queuing.\n");
2390 return -EAGAIN;
2391 }
2392
2393 IWL_DEBUG_INFO("Starting scan...\n");
2394 priv->scan_bands = 2;
2395 set_bit(STATUS_SCANNING, &priv->status);
2396 priv->scan_start = jiffies;
2397 priv->scan_pass_start = priv->scan_start;
2398
2399 queue_work(priv->workqueue, &priv->request_scan);
2400
2401 return 0;
2402}
2403
2404static int iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
2405{
2406 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
2407
2408 if (hw_decrypt)
2409 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2410 else
2411 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2412
2413 return 0;
2414}
2415
2416static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode)
2417{
2418 if (phymode == MODE_IEEE80211A) {
2419 priv->staging_rxon.flags &=
2420 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2421 | RXON_FLG_CCK_MSK);
2422 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2423 } else {
2424 /* Copied from iwl_bg_post_associate() */
2425 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2426 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2427 else
2428 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2429
2430 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2431 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2432
2433 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2434 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2435 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2436 }
2437}
2438
2439/*
Ian Schram01ebd062007-10-25 17:15:22 +08002440 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002441 */
2442static void iwl_connection_init_rx_config(struct iwl_priv *priv)
2443{
2444 const struct iwl_channel_info *ch_info;
2445
2446 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2447
2448 switch (priv->iw_mode) {
2449 case IEEE80211_IF_TYPE_AP:
2450 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2451 break;
2452
2453 case IEEE80211_IF_TYPE_STA:
2454 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2455 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2456 break;
2457
2458 case IEEE80211_IF_TYPE_IBSS:
2459 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2460 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2461 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2462 RXON_FILTER_ACCEPT_GRP_MSK;
2463 break;
2464
2465 case IEEE80211_IF_TYPE_MNTR:
2466 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2467 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2468 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2469 break;
2470 }
2471
2472#if 0
2473 /* TODO: Figure out when short_preamble would be set and cache from
2474 * that */
2475 if (!hw_to_local(priv->hw)->short_preamble)
2476 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2477 else
2478 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2479#endif
2480
2481 ch_info = iwl_get_channel_info(priv, priv->phymode,
2482 le16_to_cpu(priv->staging_rxon.channel));
2483
2484 if (!ch_info)
2485 ch_info = &priv->channel_info[0];
2486
2487 /*
2488 * in some case A channels are all non IBSS
2489 * in this case force B/G channel
2490 */
2491 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2492 !(is_channel_ibss(ch_info)))
2493 ch_info = &priv->channel_info[0];
2494
2495 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2496 if (is_channel_a_band(ch_info))
2497 priv->phymode = MODE_IEEE80211A;
2498 else
2499 priv->phymode = MODE_IEEE80211G;
2500
2501 iwl_set_flags_for_phymode(priv, priv->phymode);
2502
2503 priv->staging_rxon.ofdm_basic_rates =
2504 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2505 priv->staging_rxon.cck_basic_rates =
2506 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2507}
2508
2509static int iwl_set_mode(struct iwl_priv *priv, int mode)
2510{
2511 if (!iwl_is_ready_rf(priv))
2512 return -EAGAIN;
2513
2514 if (mode == IEEE80211_IF_TYPE_IBSS) {
2515 const struct iwl_channel_info *ch_info;
2516
2517 ch_info = iwl_get_channel_info(priv,
2518 priv->phymode,
2519 le16_to_cpu(priv->staging_rxon.channel));
2520
2521 if (!ch_info || !is_channel_ibss(ch_info)) {
2522 IWL_ERROR("channel %d not IBSS channel\n",
2523 le16_to_cpu(priv->staging_rxon.channel));
2524 return -EINVAL;
2525 }
2526 }
2527
2528 cancel_delayed_work(&priv->scan_check);
2529 if (iwl_scan_cancel_timeout(priv, 100)) {
2530 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2531 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2532 return -EAGAIN;
2533 }
2534
2535 priv->iw_mode = mode;
2536
2537 iwl_connection_init_rx_config(priv);
2538 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2539
2540 iwl_clear_stations_table(priv);
2541
2542 iwl_commit_rxon(priv);
2543
2544 return 0;
2545}
2546
2547static void iwl_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
2548 struct ieee80211_tx_control *ctl,
2549 struct iwl_cmd *cmd,
2550 struct sk_buff *skb_frag,
2551 int last_frag)
2552{
2553 struct iwl_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2554
2555 switch (keyinfo->alg) {
2556 case ALG_CCMP:
2557 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2558 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2559 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2560 break;
2561
2562 case ALG_TKIP:
2563#if 0
2564 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2565
2566 if (last_frag)
2567 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2568 8);
2569 else
2570 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2571#endif
2572 break;
2573
2574 case ALG_WEP:
2575 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2576 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2577
2578 if (keyinfo->keylen == 13)
2579 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2580
2581 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2582
2583 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2584 "with key %d\n", ctl->key_idx);
2585 break;
2586
Zhu Yib481de92007-09-25 17:54:57 -07002587 default:
2588 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2589 break;
2590 }
2591}
2592
2593/*
2594 * handle build REPLY_TX command notification.
2595 */
2596static void iwl_build_tx_cmd_basic(struct iwl_priv *priv,
2597 struct iwl_cmd *cmd,
2598 struct ieee80211_tx_control *ctrl,
2599 struct ieee80211_hdr *hdr,
2600 int is_unicast, u8 std_id)
2601{
2602 __le16 *qc;
2603 u16 fc = le16_to_cpu(hdr->frame_control);
2604 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2605
2606 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2607 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2608 tx_flags |= TX_CMD_FLG_ACK_MSK;
2609 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2610 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2611 if (ieee80211_is_probe_response(fc) &&
2612 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2613 tx_flags |= TX_CMD_FLG_TSF_MSK;
2614 } else {
2615 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2616 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2617 }
2618
2619 cmd->cmd.tx.sta_id = std_id;
2620 if (ieee80211_get_morefrag(hdr))
2621 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2622
2623 qc = ieee80211_get_qos_ctrl(hdr);
2624 if (qc) {
2625 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2626 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2627 } else
2628 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2629
2630 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2631 tx_flags |= TX_CMD_FLG_RTS_MSK;
2632 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2633 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2634 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2635 tx_flags |= TX_CMD_FLG_CTS_MSK;
2636 }
2637
2638 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2639 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2640
2641 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2642 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2643 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2644 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002645 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002646 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002647 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002648 } else
2649 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2650
2651 cmd->cmd.tx.driver_txop = 0;
2652 cmd->cmd.tx.tx_flags = tx_flags;
2653 cmd->cmd.tx.next_frame_len = 0;
2654}
2655
2656static int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
2657{
2658 int sta_id;
2659 u16 fc = le16_to_cpu(hdr->frame_control);
2660
2661 /* If this frame is broadcast or not data then use the broadcast
2662 * station id */
2663 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2664 is_multicast_ether_addr(hdr->addr1))
2665 return priv->hw_setting.bcast_sta_id;
2666
2667 switch (priv->iw_mode) {
2668
2669 /* If this frame is part of a BSS network (we're a station), then
2670 * we use the AP's station id */
2671 case IEEE80211_IF_TYPE_STA:
2672 return IWL_AP_ID;
2673
2674 /* If we are an AP, then find the station, or use BCAST */
2675 case IEEE80211_IF_TYPE_AP:
2676 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2677 if (sta_id != IWL_INVALID_STATION)
2678 return sta_id;
2679 return priv->hw_setting.bcast_sta_id;
2680
2681 /* If this frame is part of a IBSS network, then we use the
2682 * target specific station id */
Joe Perches0795af52007-10-03 17:59:30 -07002683 case IEEE80211_IF_TYPE_IBSS: {
2684 DECLARE_MAC_BUF(mac);
2685
Zhu Yib481de92007-09-25 17:54:57 -07002686 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2687 if (sta_id != IWL_INVALID_STATION)
2688 return sta_id;
2689
2690 sta_id = iwl_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
2691
2692 if (sta_id != IWL_INVALID_STATION)
2693 return sta_id;
2694
Joe Perches0795af52007-10-03 17:59:30 -07002695 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002696 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002697 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002698 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2699 return priv->hw_setting.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002700 }
Zhu Yib481de92007-09-25 17:54:57 -07002701 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002702 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002703 return priv->hw_setting.bcast_sta_id;
2704 }
2705}
2706
2707/*
2708 * start REPLY_TX command process
2709 */
2710static int iwl_tx_skb(struct iwl_priv *priv,
2711 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2712{
2713 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2714 struct iwl_tfd_frame *tfd;
2715 u32 *control_flags;
2716 int txq_id = ctl->queue;
2717 struct iwl_tx_queue *txq = NULL;
2718 struct iwl_queue *q = NULL;
2719 dma_addr_t phys_addr;
2720 dma_addr_t txcmd_phys;
2721 struct iwl_cmd *out_cmd = NULL;
2722 u16 len, idx, len_org;
2723 u8 id, hdr_len, unicast;
2724 u8 sta_id;
2725 u16 seq_number = 0;
2726 u16 fc;
2727 __le16 *qc;
2728 u8 wait_write_ptr = 0;
2729 unsigned long flags;
2730 int rc;
2731
2732 spin_lock_irqsave(&priv->lock, flags);
2733 if (iwl_is_rfkill(priv)) {
2734 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2735 goto drop_unlock;
2736 }
2737
2738 if (!priv->interface_id) {
2739 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2740 goto drop_unlock;
2741 }
2742
2743 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2744 IWL_ERROR("ERROR: No TX rate available.\n");
2745 goto drop_unlock;
2746 }
2747
2748 unicast = !is_multicast_ether_addr(hdr->addr1);
2749 id = 0;
2750
2751 fc = le16_to_cpu(hdr->frame_control);
2752
2753#ifdef CONFIG_IWLWIFI_DEBUG
2754 if (ieee80211_is_auth(fc))
2755 IWL_DEBUG_TX("Sending AUTH frame\n");
2756 else if (ieee80211_is_assoc_request(fc))
2757 IWL_DEBUG_TX("Sending ASSOC frame\n");
2758 else if (ieee80211_is_reassoc_request(fc))
2759 IWL_DEBUG_TX("Sending REASSOC frame\n");
2760#endif
2761
2762 if (!iwl_is_associated(priv) &&
2763 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
2764 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
2765 goto drop_unlock;
2766 }
2767
2768 spin_unlock_irqrestore(&priv->lock, flags);
2769
2770 hdr_len = ieee80211_get_hdrlen(fc);
2771 sta_id = iwl_get_sta_id(priv, hdr);
2772 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002773 DECLARE_MAC_BUF(mac);
2774
2775 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2776 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002777 goto drop;
2778 }
2779
2780 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2781
2782 qc = ieee80211_get_qos_ctrl(hdr);
2783 if (qc) {
2784 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2785 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2786 IEEE80211_SCTL_SEQ;
2787 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2788 (hdr->seq_ctrl &
2789 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2790 seq_number += 0x10;
2791 }
2792 txq = &priv->txq[txq_id];
2793 q = &txq->q;
2794
2795 spin_lock_irqsave(&priv->lock, flags);
2796
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002797 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002798 memset(tfd, 0, sizeof(*tfd));
2799 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002800 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002801
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002802 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
2803 txq->txb[q->write_ptr].skb[0] = skb;
2804 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002805 ctl, sizeof(struct ieee80211_tx_control));
2806 out_cmd = &txq->cmd[idx];
2807 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2808 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2809 out_cmd->hdr.cmd = REPLY_TX;
2810 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002811 INDEX_TO_SEQ(q->write_ptr)));
Zhu Yib481de92007-09-25 17:54:57 -07002812 /* copy frags header */
2813 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2814
2815 /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
2816 len = priv->hw_setting.tx_cmd_len +
2817 sizeof(struct iwl_cmd_header) + hdr_len;
2818
2819 len_org = len;
2820 len = (len + 3) & ~3;
2821
2822 if (len_org != len)
2823 len_org = 1;
2824 else
2825 len_org = 0;
2826
2827 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2828 offsetof(struct iwl_cmd, hdr);
2829
2830 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
2831
2832 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
2833 iwl_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
2834
2835 /* 802.11 null functions have no payload... */
2836 len = skb->len - hdr_len;
2837 if (len) {
2838 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2839 len, PCI_DMA_TODEVICE);
2840 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
2841 }
2842
2843 /* If there is no payload, then only one TFD is used */
2844 if (!len)
2845 *control_flags = TFD_CTL_COUNT_SET(1);
2846 else
2847 *control_flags = TFD_CTL_COUNT_SET(2) |
2848 TFD_CTL_PAD_SET(U32_PAD(len));
2849
2850 len = (u16)skb->len;
2851 out_cmd->cmd.tx.len = cpu_to_le16(len);
2852
2853 /* TODO need this for burst mode later on */
2854 iwl_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
2855
2856 /* set is_hcca to 0; it probably will never be implemented */
2857 iwl_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
2858
2859 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2860 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2861
2862 if (!ieee80211_get_morefrag(hdr)) {
2863 txq->need_update = 1;
2864 if (qc) {
2865 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2866 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2867 }
2868 } else {
2869 wait_write_ptr = 1;
2870 txq->need_update = 0;
2871 }
2872
2873 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
2874 sizeof(out_cmd->cmd.tx));
2875
2876 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
2877 ieee80211_get_hdrlen(fc));
2878
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002879 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07002880 rc = iwl_tx_queue_update_write_ptr(priv, txq);
2881 spin_unlock_irqrestore(&priv->lock, flags);
2882
2883 if (rc)
2884 return rc;
2885
2886 if ((iwl_queue_space(q) < q->high_mark)
2887 && priv->mac80211_registered) {
2888 if (wait_write_ptr) {
2889 spin_lock_irqsave(&priv->lock, flags);
2890 txq->need_update = 1;
2891 iwl_tx_queue_update_write_ptr(priv, txq);
2892 spin_unlock_irqrestore(&priv->lock, flags);
2893 }
2894
2895 ieee80211_stop_queue(priv->hw, ctl->queue);
2896 }
2897
2898 return 0;
2899
2900drop_unlock:
2901 spin_unlock_irqrestore(&priv->lock, flags);
2902drop:
2903 return -1;
2904}
2905
2906static void iwl_set_rate(struct iwl_priv *priv)
2907{
2908 const struct ieee80211_hw_mode *hw = NULL;
2909 struct ieee80211_rate *rate;
2910 int i;
2911
2912 hw = iwl_get_hw_mode(priv, priv->phymode);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002913 if (!hw) {
2914 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2915 return;
2916 }
Zhu Yib481de92007-09-25 17:54:57 -07002917
2918 priv->active_rate = 0;
2919 priv->active_rate_basic = 0;
2920
2921 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
2922 hw->mode == MODE_IEEE80211A ?
2923 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
2924
2925 for (i = 0; i < hw->num_rates; i++) {
2926 rate = &(hw->rates[i]);
2927 if ((rate->val < IWL_RATE_COUNT) &&
2928 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
2929 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
2930 rate->val, iwl_rates[rate->val].plcp,
2931 (rate->flags & IEEE80211_RATE_BASIC) ?
2932 "*" : "");
2933 priv->active_rate |= (1 << rate->val);
2934 if (rate->flags & IEEE80211_RATE_BASIC)
2935 priv->active_rate_basic |= (1 << rate->val);
2936 } else
2937 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
2938 rate->val, iwl_rates[rate->val].plcp);
2939 }
2940
2941 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2942 priv->active_rate, priv->active_rate_basic);
2943
2944 /*
2945 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2946 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2947 * OFDM
2948 */
2949 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2950 priv->staging_rxon.cck_basic_rates =
2951 ((priv->active_rate_basic &
2952 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2953 else
2954 priv->staging_rxon.cck_basic_rates =
2955 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2956
2957 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2958 priv->staging_rxon.ofdm_basic_rates =
2959 ((priv->active_rate_basic &
2960 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2961 IWL_FIRST_OFDM_RATE) & 0xFF;
2962 else
2963 priv->staging_rxon.ofdm_basic_rates =
2964 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2965}
2966
2967static void iwl_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
2968{
2969 unsigned long flags;
2970
2971 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2972 return;
2973
2974 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2975 disable_radio ? "OFF" : "ON");
2976
2977 if (disable_radio) {
2978 iwl_scan_cancel(priv);
2979 /* FIXME: This is a workaround for AP */
2980 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2981 spin_lock_irqsave(&priv->lock, flags);
2982 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
2983 CSR_UCODE_SW_BIT_RFKILL);
2984 spin_unlock_irqrestore(&priv->lock, flags);
2985 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
2986 set_bit(STATUS_RF_KILL_SW, &priv->status);
2987 }
2988 return;
2989 }
2990
2991 spin_lock_irqsave(&priv->lock, flags);
2992 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2993
2994 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2995 spin_unlock_irqrestore(&priv->lock, flags);
2996
2997 /* wake up ucode */
2998 msleep(10);
2999
3000 spin_lock_irqsave(&priv->lock, flags);
3001 iwl_read32(priv, CSR_UCODE_DRV_GP1);
Tomas Winklerac17a942007-10-25 17:15:37 +08003002 if (!iwl_grab_nic_access(priv))
3003 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003004 spin_unlock_irqrestore(&priv->lock, flags);
3005
3006 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3007 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3008 "disabled by HW switch\n");
3009 return;
3010 }
3011
3012 queue_work(priv->workqueue, &priv->restart);
3013 return;
3014}
3015
3016void iwl_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
3017 u32 decrypt_res, struct ieee80211_rx_status *stats)
3018{
3019 u16 fc =
3020 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3021
3022 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3023 return;
3024
3025 if (!(fc & IEEE80211_FCTL_PROTECTED))
3026 return;
3027
3028 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3029 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3030 case RX_RES_STATUS_SEC_TYPE_TKIP:
3031 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3032 RX_RES_STATUS_BAD_ICV_MIC)
3033 stats->flag |= RX_FLAG_MMIC_ERROR;
3034 case RX_RES_STATUS_SEC_TYPE_WEP:
3035 case RX_RES_STATUS_SEC_TYPE_CCMP:
3036 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3037 RX_RES_STATUS_DECRYPT_OK) {
3038 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3039 stats->flag |= RX_FLAG_DECRYPTED;
3040 }
3041 break;
3042
3043 default:
3044 break;
3045 }
3046}
3047
3048void iwl_handle_data_packet_monitor(struct iwl_priv *priv,
3049 struct iwl_rx_mem_buffer *rxb,
3050 void *data, short len,
3051 struct ieee80211_rx_status *stats,
3052 u16 phy_flags)
3053{
3054 struct iwl_rt_rx_hdr *iwl_rt;
3055
3056 /* First cache any information we need before we overwrite
3057 * the information provided in the skb from the hardware */
3058 s8 signal = stats->ssi;
3059 s8 noise = 0;
3060 int rate = stats->rate;
3061 u64 tsf = stats->mactime;
3062 __le16 phy_flags_hw = cpu_to_le16(phy_flags);
3063
3064 /* We received data from the HW, so stop the watchdog */
3065 if (len > IWL_RX_BUF_SIZE - sizeof(*iwl_rt)) {
3066 IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
3067 return;
3068 }
3069
3070 /* copy the frame data to write after where the radiotap header goes */
3071 iwl_rt = (void *)rxb->skb->data;
3072 memmove(iwl_rt->payload, data, len);
3073
3074 iwl_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
3075 iwl_rt->rt_hdr.it_pad = 0; /* always good to zero */
3076
3077 /* total header + data */
3078 iwl_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl_rt));
3079
3080 /* Set the size of the skb to the size of the frame */
3081 skb_put(rxb->skb, sizeof(*iwl_rt) + len);
3082
3083 /* Big bitfield of all the fields we provide in radiotap */
3084 iwl_rt->rt_hdr.it_present =
3085 cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
3086 (1 << IEEE80211_RADIOTAP_FLAGS) |
3087 (1 << IEEE80211_RADIOTAP_RATE) |
3088 (1 << IEEE80211_RADIOTAP_CHANNEL) |
3089 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
3090 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
3091 (1 << IEEE80211_RADIOTAP_ANTENNA));
3092
3093 /* Zero the flags, we'll add to them as we go */
3094 iwl_rt->rt_flags = 0;
3095
3096 iwl_rt->rt_tsf = cpu_to_le64(tsf);
3097
3098 /* Convert to dBm */
3099 iwl_rt->rt_dbmsignal = signal;
3100 iwl_rt->rt_dbmnoise = noise;
3101
3102 /* Convert the channel frequency and set the flags */
3103 iwl_rt->rt_channelMHz = cpu_to_le16(stats->freq);
3104 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
3105 iwl_rt->rt_chbitmask =
3106 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
3107 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
3108 iwl_rt->rt_chbitmask =
3109 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
3110 else /* 802.11g */
3111 iwl_rt->rt_chbitmask =
3112 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
3113
3114 rate = iwl_rate_index_from_plcp(rate);
3115 if (rate == -1)
3116 iwl_rt->rt_rate = 0;
3117 else
3118 iwl_rt->rt_rate = iwl_rates[rate].ieee;
3119
3120 /* antenna number */
3121 iwl_rt->rt_antenna =
3122 le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
3123
3124 /* set the preamble flag if we have it */
3125 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
3126 iwl_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3127
3128 IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
3129
3130 stats->flag |= RX_FLAG_RADIOTAP;
3131 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3132 rxb->skb = NULL;
3133}
3134
3135
3136#define IWL_PACKET_RETRY_TIME HZ
3137
3138int is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
3139{
3140 u16 sc = le16_to_cpu(header->seq_ctrl);
3141 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3142 u16 frag = sc & IEEE80211_SCTL_FRAG;
3143 u16 *last_seq, *last_frag;
3144 unsigned long *last_time;
3145
3146 switch (priv->iw_mode) {
3147 case IEEE80211_IF_TYPE_IBSS:{
3148 struct list_head *p;
3149 struct iwl_ibss_seq *entry = NULL;
3150 u8 *mac = header->addr2;
3151 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3152
3153 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Ian Schrambc434dd2007-10-25 17:15:29 +08003154 entry = list_entry(p, struct iwl_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07003155 if (!compare_ether_addr(entry->mac, mac))
3156 break;
3157 }
3158 if (p == &priv->ibss_mac_hash[index]) {
3159 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3160 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08003161 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07003162 return 0;
3163 }
3164 memcpy(entry->mac, mac, ETH_ALEN);
3165 entry->seq_num = seq;
3166 entry->frag_num = frag;
3167 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08003168 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07003169 return 0;
3170 }
3171 last_seq = &entry->seq_num;
3172 last_frag = &entry->frag_num;
3173 last_time = &entry->packet_time;
3174 break;
3175 }
3176 case IEEE80211_IF_TYPE_STA:
3177 last_seq = &priv->last_seq_num;
3178 last_frag = &priv->last_frag_num;
3179 last_time = &priv->last_packet_time;
3180 break;
3181 default:
3182 return 0;
3183 }
3184 if ((*last_seq == seq) &&
3185 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3186 if (*last_frag == frag)
3187 goto drop;
3188 if (*last_frag + 1 != frag)
3189 /* out-of-order fragment */
3190 goto drop;
3191 } else
3192 *last_seq = seq;
3193
3194 *last_frag = frag;
3195 *last_time = jiffies;
3196 return 0;
3197
3198 drop:
3199 return 1;
3200}
3201
3202#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3203
3204#include "iwl-spectrum.h"
3205
3206#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3207#define BEACON_TIME_MASK_HIGH 0xFF000000
3208#define TIME_UNIT 1024
3209
3210/*
3211 * extended beacon time format
3212 * time in usec will be changed into a 32-bit value in 8:24 format
3213 * the high 1 byte is the beacon counts
3214 * the lower 3 bytes is the time in usec within one beacon interval
3215 */
3216
3217static u32 iwl_usecs_to_beacons(u32 usec, u32 beacon_interval)
3218{
3219 u32 quot;
3220 u32 rem;
3221 u32 interval = beacon_interval * 1024;
3222
3223 if (!interval || !usec)
3224 return 0;
3225
3226 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3227 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3228
3229 return (quot << 24) + rem;
3230}
3231
3232/* base is usually what we get from ucode with each received frame,
3233 * the same as HW timer counter counting down
3234 */
3235
3236static __le32 iwl_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
3237{
3238 u32 base_low = base & BEACON_TIME_MASK_LOW;
3239 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3240 u32 interval = beacon_interval * TIME_UNIT;
3241 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3242 (addon & BEACON_TIME_MASK_HIGH);
3243
3244 if (base_low > addon_low)
3245 res += base_low - addon_low;
3246 else if (base_low < addon_low) {
3247 res += interval + base_low - addon_low;
3248 res += (1 << 24);
3249 } else
3250 res += (1 << 24);
3251
3252 return cpu_to_le32(res);
3253}
3254
3255static int iwl_get_measurement(struct iwl_priv *priv,
3256 struct ieee80211_measurement_params *params,
3257 u8 type)
3258{
3259 struct iwl_spectrum_cmd spectrum;
3260 struct iwl_rx_packet *res;
3261 struct iwl_host_cmd cmd = {
3262 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3263 .data = (void *)&spectrum,
3264 .meta.flags = CMD_WANT_SKB,
3265 };
3266 u32 add_time = le64_to_cpu(params->start_time);
3267 int rc;
3268 int spectrum_resp_status;
3269 int duration = le16_to_cpu(params->duration);
3270
3271 if (iwl_is_associated(priv))
3272 add_time =
3273 iwl_usecs_to_beacons(
3274 le64_to_cpu(params->start_time) - priv->last_tsf,
3275 le16_to_cpu(priv->rxon_timing.beacon_interval));
3276
3277 memset(&spectrum, 0, sizeof(spectrum));
3278
3279 spectrum.channel_count = cpu_to_le16(1);
3280 spectrum.flags =
3281 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3282 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3283 cmd.len = sizeof(spectrum);
3284 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3285
3286 if (iwl_is_associated(priv))
3287 spectrum.start_time =
3288 iwl_add_beacon_time(priv->last_beacon_time,
3289 add_time,
3290 le16_to_cpu(priv->rxon_timing.beacon_interval));
3291 else
3292 spectrum.start_time = 0;
3293
3294 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3295 spectrum.channels[0].channel = params->channel;
3296 spectrum.channels[0].type = type;
3297 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3298 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3299 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3300
3301 rc = iwl_send_cmd_sync(priv, &cmd);
3302 if (rc)
3303 return rc;
3304
3305 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
3306 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3307 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3308 rc = -EIO;
3309 }
3310
3311 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3312 switch (spectrum_resp_status) {
3313 case 0: /* Command will be handled */
3314 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08003315 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
3316 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07003317 priv->measurement_status &= ~MEASUREMENT_READY;
3318 }
3319 priv->measurement_status |= MEASUREMENT_ACTIVE;
3320 rc = 0;
3321 break;
3322
3323 case 1: /* Command will not be handled */
3324 rc = -EAGAIN;
3325 break;
3326 }
3327
3328 dev_kfree_skb_any(cmd.meta.u.skb);
3329
3330 return rc;
3331}
3332#endif
3333
3334static void iwl_txstatus_to_ieee(struct iwl_priv *priv,
3335 struct iwl_tx_info *tx_sta)
3336{
3337
3338 tx_sta->status.ack_signal = 0;
3339 tx_sta->status.excessive_retries = 0;
3340 tx_sta->status.queue_length = 0;
3341 tx_sta->status.queue_number = 0;
3342
3343 if (in_interrupt())
3344 ieee80211_tx_status_irqsafe(priv->hw,
3345 tx_sta->skb[0], &(tx_sta->status));
3346 else
3347 ieee80211_tx_status(priv->hw,
3348 tx_sta->skb[0], &(tx_sta->status));
3349
3350 tx_sta->skb[0] = NULL;
3351}
3352
3353/**
3354 * iwl_tx_queue_reclaim - Reclaim Tx queue entries no more used by NIC.
3355 *
3356 * When FW advances 'R' index, all entries between old and
3357 * new 'R' index need to be reclaimed. As result, some free space
3358 * forms. If there is enough free space (> low mark), wake Tx queue.
3359 */
Christoph Hellwig416e1432007-10-25 17:15:49 +08003360static int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003361{
3362 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3363 struct iwl_queue *q = &txq->q;
3364 int nfreed = 0;
3365
3366 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3367 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3368 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003369 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003370 return 0;
3371 }
3372
3373 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003374 q->read_ptr != index;
3375 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003376 if (txq_id != IWL_CMD_QUEUE_NUM) {
3377 iwl_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003378 &(txq->txb[txq->q.read_ptr]));
Zhu Yib481de92007-09-25 17:54:57 -07003379 iwl_hw_txq_free_tfd(priv, txq);
3380 } else if (nfreed > 1) {
3381 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003382 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003383 queue_work(priv->workqueue, &priv->restart);
3384 }
3385 nfreed++;
3386 }
3387
3388 if (iwl_queue_space(q) > q->low_mark && (txq_id >= 0) &&
3389 (txq_id != IWL_CMD_QUEUE_NUM) &&
3390 priv->mac80211_registered)
3391 ieee80211_wake_queue(priv->hw, txq_id);
3392
3393
3394 return nfreed;
3395}
3396
3397static int iwl_is_tx_success(u32 status)
3398{
3399 return (status & 0xFF) == 0x1;
3400}
3401
3402/******************************************************************************
3403 *
3404 * Generic RX handler implementations
3405 *
3406 ******************************************************************************/
3407static void iwl_rx_reply_tx(struct iwl_priv *priv,
3408 struct iwl_rx_mem_buffer *rxb)
3409{
3410 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3411 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3412 int txq_id = SEQ_TO_QUEUE(sequence);
3413 int index = SEQ_TO_INDEX(sequence);
3414 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3415 struct ieee80211_tx_status *tx_status;
3416 struct iwl_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
3417 u32 status = le32_to_cpu(tx_resp->status);
3418
3419 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3420 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3421 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003422 index, txq->q.n_bd, txq->q.write_ptr,
3423 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003424 return;
3425 }
3426
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003427 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003428
3429 tx_status->retry_count = tx_resp->failure_frame;
3430 tx_status->queue_number = status;
3431 tx_status->queue_length = tx_resp->bt_kill_count;
3432 tx_status->queue_length |= tx_resp->failure_rts;
3433
3434 tx_status->flags =
3435 iwl_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
3436
3437 tx_status->control.tx_rate = iwl_rate_index_from_plcp(tx_resp->rate);
3438
3439 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) plcp rate %d retries %d\n",
3440 txq_id, iwl_get_tx_fail_reason(status), status,
3441 tx_resp->rate, tx_resp->failure_frame);
3442
3443 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3444 if (index != -1)
3445 iwl_tx_queue_reclaim(priv, txq_id, index);
3446
3447 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3448 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3449}
3450
3451
3452static void iwl_rx_reply_alive(struct iwl_priv *priv,
3453 struct iwl_rx_mem_buffer *rxb)
3454{
3455 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3456 struct iwl_alive_resp *palive;
3457 struct delayed_work *pwork;
3458
3459 palive = &pkt->u.alive_frame;
3460
3461 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3462 "0x%01X 0x%01X\n",
3463 palive->is_valid, palive->ver_type,
3464 palive->ver_subtype);
3465
3466 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3467 IWL_DEBUG_INFO("Initialization Alive received.\n");
3468 memcpy(&priv->card_alive_init,
3469 &pkt->u.alive_frame,
3470 sizeof(struct iwl_init_alive_resp));
3471 pwork = &priv->init_alive_start;
3472 } else {
3473 IWL_DEBUG_INFO("Runtime Alive received.\n");
3474 memcpy(&priv->card_alive, &pkt->u.alive_frame,
3475 sizeof(struct iwl_alive_resp));
3476 pwork = &priv->alive_start;
3477 iwl_disable_events(priv);
3478 }
3479
3480 /* We delay the ALIVE response by 5ms to
3481 * give the HW RF Kill time to activate... */
3482 if (palive->is_valid == UCODE_VALID_OK)
3483 queue_delayed_work(priv->workqueue, pwork,
3484 msecs_to_jiffies(5));
3485 else
3486 IWL_WARNING("uCode did not respond OK.\n");
3487}
3488
3489static void iwl_rx_reply_add_sta(struct iwl_priv *priv,
3490 struct iwl_rx_mem_buffer *rxb)
3491{
3492 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3493
3494 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3495 return;
3496}
3497
3498static void iwl_rx_reply_error(struct iwl_priv *priv,
3499 struct iwl_rx_mem_buffer *rxb)
3500{
3501 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3502
3503 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3504 "seq 0x%04X ser 0x%08X\n",
3505 le32_to_cpu(pkt->u.err_resp.error_type),
3506 get_cmd_string(pkt->u.err_resp.cmd_id),
3507 pkt->u.err_resp.cmd_id,
3508 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3509 le32_to_cpu(pkt->u.err_resp.error_info));
3510}
3511
3512#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3513
3514static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
3515{
3516 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3517 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
3518 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
3519 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3520 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3521 rxon->channel = csa->channel;
3522 priv->staging_rxon.channel = csa->channel;
3523}
3524
3525static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
3526 struct iwl_rx_mem_buffer *rxb)
3527{
3528#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3529 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3530 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
3531
3532 if (!report->state) {
3533 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3534 "Spectrum Measure Notification: Start\n");
3535 return;
3536 }
3537
3538 memcpy(&priv->measure_report, report, sizeof(*report));
3539 priv->measurement_status |= MEASUREMENT_READY;
3540#endif
3541}
3542
3543static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
3544 struct iwl_rx_mem_buffer *rxb)
3545{
3546#ifdef CONFIG_IWLWIFI_DEBUG
3547 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3548 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
3549 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3550 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3551#endif
3552}
3553
3554static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
3555 struct iwl_rx_mem_buffer *rxb)
3556{
3557 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3558 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3559 "notification for %s:\n",
3560 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
3561 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
3562}
3563
3564static void iwl_bg_beacon_update(struct work_struct *work)
3565{
3566 struct iwl_priv *priv =
3567 container_of(work, struct iwl_priv, beacon_update);
3568 struct sk_buff *beacon;
3569
3570 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3571 beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
3572
3573 if (!beacon) {
3574 IWL_ERROR("update beacon failed\n");
3575 return;
3576 }
3577
3578 mutex_lock(&priv->mutex);
3579 /* new beacon skb is allocated every time; dispose previous.*/
3580 if (priv->ibss_beacon)
3581 dev_kfree_skb(priv->ibss_beacon);
3582
3583 priv->ibss_beacon = beacon;
3584 mutex_unlock(&priv->mutex);
3585
3586 iwl_send_beacon_cmd(priv);
3587}
3588
3589static void iwl_rx_beacon_notif(struct iwl_priv *priv,
3590 struct iwl_rx_mem_buffer *rxb)
3591{
3592#ifdef CONFIG_IWLWIFI_DEBUG
3593 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3594 struct iwl_beacon_notif *beacon = &(pkt->u.beacon_status);
3595 u8 rate = beacon->beacon_notify_hdr.rate;
3596
3597 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3598 "tsf %d %d rate %d\n",
3599 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3600 beacon->beacon_notify_hdr.failure_frame,
3601 le32_to_cpu(beacon->ibss_mgr_status),
3602 le32_to_cpu(beacon->high_tsf),
3603 le32_to_cpu(beacon->low_tsf), rate);
3604#endif
3605
3606 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3607 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3608 queue_work(priv->workqueue, &priv->beacon_update);
3609}
3610
3611/* Service response to REPLY_SCAN_CMD (0x80) */
3612static void iwl_rx_reply_scan(struct iwl_priv *priv,
3613 struct iwl_rx_mem_buffer *rxb)
3614{
3615#ifdef CONFIG_IWLWIFI_DEBUG
3616 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3617 struct iwl_scanreq_notification *notif =
3618 (struct iwl_scanreq_notification *)pkt->u.raw;
3619
3620 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3621#endif
3622}
3623
3624/* Service SCAN_START_NOTIFICATION (0x82) */
3625static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
3626 struct iwl_rx_mem_buffer *rxb)
3627{
3628 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3629 struct iwl_scanstart_notification *notif =
3630 (struct iwl_scanstart_notification *)pkt->u.raw;
3631 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3632 IWL_DEBUG_SCAN("Scan start: "
3633 "%d [802.11%s] "
3634 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3635 notif->channel,
3636 notif->band ? "bg" : "a",
3637 notif->tsf_high,
3638 notif->tsf_low, notif->status, notif->beacon_timer);
3639}
3640
3641/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
3642static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
3643 struct iwl_rx_mem_buffer *rxb)
3644{
3645 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3646 struct iwl_scanresults_notification *notif =
3647 (struct iwl_scanresults_notification *)pkt->u.raw;
3648
3649 IWL_DEBUG_SCAN("Scan ch.res: "
3650 "%d [802.11%s] "
3651 "(TSF: 0x%08X:%08X) - %d "
3652 "elapsed=%lu usec (%dms since last)\n",
3653 notif->channel,
3654 notif->band ? "bg" : "a",
3655 le32_to_cpu(notif->tsf_high),
3656 le32_to_cpu(notif->tsf_low),
3657 le32_to_cpu(notif->statistics[0]),
3658 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3659 jiffies_to_msecs(elapsed_jiffies
3660 (priv->last_scan_jiffies, jiffies)));
3661
3662 priv->last_scan_jiffies = jiffies;
3663}
3664
3665/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
3666static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
3667 struct iwl_rx_mem_buffer *rxb)
3668{
3669 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3670 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
3671
3672 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3673 scan_notif->scanned_channels,
3674 scan_notif->tsf_low,
3675 scan_notif->tsf_high, scan_notif->status);
3676
3677 /* The HW is no longer scanning */
3678 clear_bit(STATUS_SCAN_HW, &priv->status);
3679
3680 /* The scan completion notification came in, so kill that timer... */
3681 cancel_delayed_work(&priv->scan_check);
3682
3683 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3684 (priv->scan_bands == 2) ? "2.4" : "5.2",
3685 jiffies_to_msecs(elapsed_jiffies
3686 (priv->scan_pass_start, jiffies)));
3687
3688 /* Remove this scanned band from the list
3689 * of pending bands to scan */
3690 priv->scan_bands--;
3691
3692 /* If a request to abort was given, or the scan did not succeed
3693 * then we reset the scan state machine and terminate,
3694 * re-queuing another scan if one has been requested */
3695 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3696 IWL_DEBUG_INFO("Aborted scan completed.\n");
3697 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3698 } else {
3699 /* If there are more bands on this scan pass reschedule */
3700 if (priv->scan_bands > 0)
3701 goto reschedule;
3702 }
3703
3704 priv->last_scan_jiffies = jiffies;
3705 IWL_DEBUG_INFO("Setting scan to off\n");
3706
3707 clear_bit(STATUS_SCANNING, &priv->status);
3708
3709 IWL_DEBUG_INFO("Scan took %dms\n",
3710 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3711
3712 queue_work(priv->workqueue, &priv->scan_completed);
3713
3714 return;
3715
3716reschedule:
3717 priv->scan_pass_start = jiffies;
3718 queue_work(priv->workqueue, &priv->request_scan);
3719}
3720
3721/* Handle notification from uCode that card's power state is changing
3722 * due to software, hardware, or critical temperature RFKILL */
3723static void iwl_rx_card_state_notif(struct iwl_priv *priv,
3724 struct iwl_rx_mem_buffer *rxb)
3725{
3726 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3727 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3728 unsigned long status = priv->status;
3729
3730 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3731 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3732 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3733
3734 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
3735 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3736
3737 if (flags & HW_CARD_DISABLED)
3738 set_bit(STATUS_RF_KILL_HW, &priv->status);
3739 else
3740 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3741
3742
3743 if (flags & SW_CARD_DISABLED)
3744 set_bit(STATUS_RF_KILL_SW, &priv->status);
3745 else
3746 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3747
3748 iwl_scan_cancel(priv);
3749
3750 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3751 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3752 (test_bit(STATUS_RF_KILL_SW, &status) !=
3753 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3754 queue_work(priv->workqueue, &priv->rf_kill);
3755 else
3756 wake_up_interruptible(&priv->wait_command_queue);
3757}
3758
3759/**
3760 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
3761 *
3762 * Setup the RX handlers for each of the reply types sent from the uCode
3763 * to the host.
3764 *
3765 * This function chains into the hardware specific files for them to setup
3766 * any hardware specific handlers as well.
3767 */
3768static void iwl_setup_rx_handlers(struct iwl_priv *priv)
3769{
3770 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
3771 priv->rx_handlers[REPLY_ADD_STA] = iwl_rx_reply_add_sta;
3772 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
3773 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
3774 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
3775 iwl_rx_spectrum_measure_notif;
3776 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
3777 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
3778 iwl_rx_pm_debug_statistics_notif;
3779 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
3780
3781 /* NOTE: iwl_rx_statistics is different based on whether
3782 * the build is for the 3945 or the 4965. See the
3783 * corresponding implementation in iwl-XXXX.c
3784 *
3785 * The same handler is used for both the REPLY to a
3786 * discrete statistics request from the host as well as
3787 * for the periodic statistics notification from the uCode
3788 */
3789 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_hw_rx_statistics;
3790 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_hw_rx_statistics;
3791
3792 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
3793 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
3794 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
3795 iwl_rx_scan_results_notif;
3796 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
3797 iwl_rx_scan_complete_notif;
3798 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
3799 priv->rx_handlers[REPLY_TX] = iwl_rx_reply_tx;
3800
3801 /* Setup hardware specific Rx handlers */
3802 iwl_hw_rx_handler_setup(priv);
3803}
3804
3805/**
3806 * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
3807 * @rxb: Rx buffer to reclaim
3808 *
3809 * If an Rx buffer has an async callback associated with it the callback
3810 * will be executed. The attached skb (if present) will only be freed
3811 * if the callback returns 1
3812 */
3813static void iwl_tx_cmd_complete(struct iwl_priv *priv,
3814 struct iwl_rx_mem_buffer *rxb)
3815{
3816 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3817 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3818 int txq_id = SEQ_TO_QUEUE(sequence);
3819 int index = SEQ_TO_INDEX(sequence);
3820 int huge = sequence & SEQ_HUGE_FRAME;
3821 int cmd_index;
3822 struct iwl_cmd *cmd;
3823
3824 /* If a Tx command is being handled and it isn't in the actual
3825 * command queue then there a command routing bug has been introduced
3826 * in the queue management code. */
3827 if (txq_id != IWL_CMD_QUEUE_NUM)
3828 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3829 txq_id, pkt->hdr.cmd);
3830 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3831
3832 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3833 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3834
3835 /* Input error checking is done when commands are added to queue. */
3836 if (cmd->meta.flags & CMD_WANT_SKB) {
3837 cmd->meta.source->u.skb = rxb->skb;
3838 rxb->skb = NULL;
3839 } else if (cmd->meta.u.callback &&
3840 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3841 rxb->skb = NULL;
3842
3843 iwl_tx_queue_reclaim(priv, txq_id, index);
3844
3845 if (!(cmd->meta.flags & CMD_ASYNC)) {
3846 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3847 wake_up_interruptible(&priv->wait_command_queue);
3848 }
3849}
3850
3851/************************** RX-FUNCTIONS ****************************/
3852/*
3853 * Rx theory of operation
3854 *
3855 * The host allocates 32 DMA target addresses and passes the host address
3856 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3857 * 0 to 31
3858 *
3859 * Rx Queue Indexes
3860 * The host/firmware share two index registers for managing the Rx buffers.
3861 *
3862 * The READ index maps to the first position that the firmware may be writing
3863 * to -- the driver can read up to (but not including) this position and get
3864 * good data.
3865 * The READ index is managed by the firmware once the card is enabled.
3866 *
3867 * The WRITE index maps to the last position the driver has read from -- the
3868 * position preceding WRITE is the last slot the firmware can place a packet.
3869 *
3870 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3871 * WRITE = READ.
3872 *
3873 * During initialization the host sets up the READ queue position to the first
3874 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3875 *
3876 * When the firmware places a packet in a buffer it will advance the READ index
3877 * and fire the RX interrupt. The driver can then query the READ index and
3878 * process as many packets as possible, moving the WRITE index forward as it
3879 * resets the Rx queue buffers with new memory.
3880 *
3881 * The management in the driver is as follows:
3882 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3883 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003884 * to replenish the iwl->rxq->rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003885 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
3886 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3887 * 'processed' and 'read' driver indexes as well)
3888 * + A received packet is processed and handed to the kernel network stack,
3889 * detached from the iwl->rxq. The driver 'processed' index is updated.
3890 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3891 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3892 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3893 * were enough free buffers and RX_STALLED is set it is cleared.
3894 *
3895 *
3896 * Driver sequence:
3897 *
3898 * iwl_rx_queue_alloc() Allocates rx_free
3899 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
3900 * iwl_rx_queue_restock
3901 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
3902 * queue, updates firmware pointers, and updates
3903 * the WRITE index. If insufficient rx_free buffers
3904 * are available, schedules iwl_rx_replenish
3905 *
3906 * -- enable interrupts --
3907 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
3908 * READ INDEX, detaching the SKB from the pool.
3909 * Moves the packet buffer from queue to rx_used.
3910 * Calls iwl_rx_queue_restock to refill any empty
3911 * slots.
3912 * ...
3913 *
3914 */
3915
3916/**
3917 * iwl_rx_queue_space - Return number of free slots available in queue.
3918 */
3919static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
3920{
3921 int s = q->read - q->write;
3922 if (s <= 0)
3923 s += RX_QUEUE_SIZE;
3924 /* keep some buffer to not confuse full and empty queue */
3925 s -= 2;
3926 if (s < 0)
3927 s = 0;
3928 return s;
3929}
3930
3931/**
3932 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
3933 *
3934 * NOTE: This function has 3945 and 4965 specific code sections
3935 * but is declared in base due to the majority of the
3936 * implementation being the same (only a numeric constant is
3937 * different)
3938 *
3939 */
3940int iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
3941{
3942 u32 reg = 0;
3943 int rc = 0;
3944 unsigned long flags;
3945
3946 spin_lock_irqsave(&q->lock, flags);
3947
3948 if (q->need_update == 0)
3949 goto exit_unlock;
3950
3951 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3952 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
3953
3954 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3955 iwl_set_bit(priv, CSR_GP_CNTRL,
3956 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3957 goto exit_unlock;
3958 }
3959
Tomas Winklerac17a942007-10-25 17:15:37 +08003960 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003961 if (rc)
3962 goto exit_unlock;
3963
Tomas Winklerac17a942007-10-25 17:15:37 +08003964 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003965 q->write & ~0x7);
Tomas Winklerac17a942007-10-25 17:15:37 +08003966 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003967 } else
3968 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
3969
3970
3971 q->need_update = 0;
3972
3973 exit_unlock:
3974 spin_unlock_irqrestore(&q->lock, flags);
3975 return rc;
3976}
3977
3978/**
3979 * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer pointer.
3980 *
3981 * NOTE: This function has 3945 and 4965 specific code paths in it.
3982 */
3983static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
3984 dma_addr_t dma_addr)
3985{
3986 return cpu_to_le32((u32)dma_addr);
3987}
3988
3989/**
3990 * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
3991 *
3992 * If there are slots in the RX queue that need to be restocked,
3993 * and we have free pre-allocated buffers, fill the ranks as much
3994 * as we can pulling from rx_free.
3995 *
3996 * This moves the 'write' index forward to catch up with 'processed', and
3997 * also updates the memory address in the firmware to reference the new
3998 * target buffer.
3999 */
Christoph Hellwig416e1432007-10-25 17:15:49 +08004000static int iwl_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004001{
4002 struct iwl_rx_queue *rxq = &priv->rxq;
4003 struct list_head *element;
4004 struct iwl_rx_mem_buffer *rxb;
4005 unsigned long flags;
4006 int write, rc;
4007
4008 spin_lock_irqsave(&rxq->lock, flags);
4009 write = rxq->write & ~0x7;
4010 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
4011 element = rxq->rx_free.next;
4012 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4013 list_del(element);
4014 rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->dma_addr);
4015 rxq->queue[rxq->write] = rxb;
4016 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4017 rxq->free_count--;
4018 }
4019 spin_unlock_irqrestore(&rxq->lock, flags);
4020 /* If the pre-allocated buffer pool is dropping low, schedule to
4021 * refill it */
4022 if (rxq->free_count <= RX_LOW_WATERMARK)
4023 queue_work(priv->workqueue, &priv->rx_replenish);
4024
4025
4026 /* If we've added more space for the firmware to place data, tell it */
4027 if ((write != (rxq->write & ~0x7))
4028 || (abs(rxq->write - rxq->read) > 7)) {
4029 spin_lock_irqsave(&rxq->lock, flags);
4030 rxq->need_update = 1;
4031 spin_unlock_irqrestore(&rxq->lock, flags);
4032 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
4033 if (rc)
4034 return rc;
4035 }
4036
4037 return 0;
4038}
4039
4040/**
Ian Schram01ebd062007-10-25 17:15:22 +08004041 * iwl_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004042 *
4043 * When moving to rx_free an SKB is allocated for the slot.
4044 *
4045 * Also restock the Rx queue via iwl_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004046 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004047 */
4048void iwl_rx_replenish(void *data)
4049{
4050 struct iwl_priv *priv = data;
4051 struct iwl_rx_queue *rxq = &priv->rxq;
4052 struct list_head *element;
4053 struct iwl_rx_mem_buffer *rxb;
4054 unsigned long flags;
4055 spin_lock_irqsave(&rxq->lock, flags);
4056 while (!list_empty(&rxq->rx_used)) {
4057 element = rxq->rx_used.next;
4058 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4059 rxb->skb =
4060 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
4061 if (!rxb->skb) {
4062 if (net_ratelimit())
4063 printk(KERN_CRIT DRV_NAME
4064 ": Can not allocate SKB buffers\n");
4065 /* We don't reschedule replenish work here -- we will
4066 * call the restock method and if it still needs
4067 * more buffers it will schedule replenish */
4068 break;
4069 }
4070 priv->alloc_rxb_skb++;
4071 list_del(element);
4072 rxb->dma_addr =
4073 pci_map_single(priv->pci_dev, rxb->skb->data,
4074 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4075 list_add_tail(&rxb->list, &rxq->rx_free);
4076 rxq->free_count++;
4077 }
4078 spin_unlock_irqrestore(&rxq->lock, flags);
4079
4080 spin_lock_irqsave(&priv->lock, flags);
4081 iwl_rx_queue_restock(priv);
4082 spin_unlock_irqrestore(&priv->lock, flags);
4083}
4084
4085/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
4086 * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
4087 * This free routine walks the list of POOL entries and if SKB is set to
4088 * non NULL it is unmapped and freed
4089 */
Christoph Hellwig416e1432007-10-25 17:15:49 +08004090static void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004091{
4092 int i;
4093 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4094 if (rxq->pool[i].skb != NULL) {
4095 pci_unmap_single(priv->pci_dev,
4096 rxq->pool[i].dma_addr,
4097 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4098 dev_kfree_skb(rxq->pool[i].skb);
4099 }
4100 }
4101
4102 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4103 rxq->dma_addr);
4104 rxq->bd = NULL;
4105}
4106
4107int iwl_rx_queue_alloc(struct iwl_priv *priv)
4108{
4109 struct iwl_rx_queue *rxq = &priv->rxq;
4110 struct pci_dev *dev = priv->pci_dev;
4111 int i;
4112
4113 spin_lock_init(&rxq->lock);
4114 INIT_LIST_HEAD(&rxq->rx_free);
4115 INIT_LIST_HEAD(&rxq->rx_used);
4116 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4117 if (!rxq->bd)
4118 return -ENOMEM;
4119 /* Fill the rx_used queue with _all_ of the Rx buffers */
4120 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4121 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4122 /* Set us so that we have processed and used all buffers, but have
4123 * not restocked the Rx queue with fresh buffers */
4124 rxq->read = rxq->write = 0;
4125 rxq->free_count = 0;
4126 rxq->need_update = 0;
4127 return 0;
4128}
4129
4130void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4131{
4132 unsigned long flags;
4133 int i;
4134 spin_lock_irqsave(&rxq->lock, flags);
4135 INIT_LIST_HEAD(&rxq->rx_free);
4136 INIT_LIST_HEAD(&rxq->rx_used);
4137 /* Fill the rx_used queue with _all_ of the Rx buffers */
4138 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4139 /* In the reset function, these buffers may have been allocated
4140 * to an SKB, so we need to unmap and free potential storage */
4141 if (rxq->pool[i].skb != NULL) {
4142 pci_unmap_single(priv->pci_dev,
4143 rxq->pool[i].dma_addr,
4144 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4145 priv->alloc_rxb_skb--;
4146 dev_kfree_skb(rxq->pool[i].skb);
4147 rxq->pool[i].skb = NULL;
4148 }
4149 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4150 }
4151
4152 /* Set us so that we have processed and used all buffers, but have
4153 * not restocked the Rx queue with fresh buffers */
4154 rxq->read = rxq->write = 0;
4155 rxq->free_count = 0;
4156 spin_unlock_irqrestore(&rxq->lock, flags);
4157}
4158
4159/* Convert linear signal-to-noise ratio into dB */
4160static u8 ratio2dB[100] = {
4161/* 0 1 2 3 4 5 6 7 8 9 */
4162 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4163 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4164 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4165 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4166 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4167 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4168 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4169 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4170 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4171 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4172};
4173
4174/* Calculates a relative dB value from a ratio of linear
4175 * (i.e. not dB) signal levels.
4176 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
4177int iwl_calc_db_from_ratio(int sig_ratio)
4178{
4179 /* Anything above 1000:1 just report as 60 dB */
4180 if (sig_ratio > 1000)
4181 return 60;
4182
4183 /* Above 100:1, divide by 10 and use table,
4184 * add 20 dB to make up for divide by 10 */
4185 if (sig_ratio > 100)
4186 return (20 + (int)ratio2dB[sig_ratio/10]);
4187
4188 /* We shouldn't see this */
4189 if (sig_ratio < 1)
4190 return 0;
4191
4192 /* Use table for ratios 1:1 - 99:1 */
4193 return (int)ratio2dB[sig_ratio];
4194}
4195
4196#define PERFECT_RSSI (-20) /* dBm */
4197#define WORST_RSSI (-95) /* dBm */
4198#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4199
4200/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4201 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4202 * about formulas used below. */
4203int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
4204{
4205 int sig_qual;
4206 int degradation = PERFECT_RSSI - rssi_dbm;
4207
4208 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4209 * as indicator; formula is (signal dbm - noise dbm).
4210 * SNR at or above 40 is a great signal (100%).
4211 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4212 * Weakest usable signal is usually 10 - 15 dB SNR. */
4213 if (noise_dbm) {
4214 if (rssi_dbm - noise_dbm >= 40)
4215 return 100;
4216 else if (rssi_dbm < noise_dbm)
4217 return 0;
4218 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4219
4220 /* Else use just the signal level.
4221 * This formula is a least squares fit of data points collected and
4222 * compared with a reference system that had a percentage (%) display
4223 * for signal quality. */
4224 } else
4225 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4226 (15 * RSSI_RANGE + 62 * degradation)) /
4227 (RSSI_RANGE * RSSI_RANGE);
4228
4229 if (sig_qual > 100)
4230 sig_qual = 100;
4231 else if (sig_qual < 1)
4232 sig_qual = 0;
4233
4234 return sig_qual;
4235}
4236
4237/**
4238 * iwl_rx_handle - Main entry function for receiving responses from the uCode
4239 *
4240 * Uses the priv->rx_handlers callback function array to invoke
4241 * the appropriate handlers, including command responses,
4242 * frame-received notifications, and other notifications.
4243 */
4244static void iwl_rx_handle(struct iwl_priv *priv)
4245{
4246 struct iwl_rx_mem_buffer *rxb;
4247 struct iwl_rx_packet *pkt;
4248 struct iwl_rx_queue *rxq = &priv->rxq;
4249 u32 r, i;
4250 int reclaim;
4251 unsigned long flags;
4252
4253 r = iwl_hw_get_rx_read(priv);
4254 i = rxq->read;
4255
4256 /* Rx interrupt, but nothing sent from uCode */
4257 if (i == r)
4258 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4259
4260 while (i != r) {
4261 rxb = rxq->queue[i];
4262
4263 /* If an RXB doesn't have a queue slot associated with it
4264 * then a bug has been introduced in the queue refilling
4265 * routines -- catch it here */
4266 BUG_ON(rxb == NULL);
4267
4268 rxq->queue[i] = NULL;
4269
4270 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4271 IWL_RX_BUF_SIZE,
4272 PCI_DMA_FROMDEVICE);
4273 pkt = (struct iwl_rx_packet *)rxb->skb->data;
4274
4275 /* Reclaim a command buffer only if this packet is a response
4276 * to a (driver-originated) command.
4277 * If the packet (e.g. Rx frame) originated from uCode,
4278 * there is no command buffer to reclaim.
4279 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4280 * but apparently a few don't get set; catch them here. */
4281 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4282 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4283 (pkt->hdr.cmd != REPLY_TX);
4284
4285 /* Based on type of command response or notification,
4286 * handle those that need handling via function in
4287 * rx_handlers table. See iwl_setup_rx_handlers() */
4288 if (priv->rx_handlers[pkt->hdr.cmd]) {
4289 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4290 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4291 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4292 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4293 } else {
4294 /* No handling needed */
4295 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4296 "r %d i %d No handler needed for %s, 0x%02x\n",
4297 r, i, get_cmd_string(pkt->hdr.cmd),
4298 pkt->hdr.cmd);
4299 }
4300
4301 if (reclaim) {
4302 /* Invoke any callbacks, transfer the skb to caller,
4303 * and fire off the (possibly) blocking iwl_send_cmd()
4304 * as we reclaim the driver command queue */
4305 if (rxb && rxb->skb)
4306 iwl_tx_cmd_complete(priv, rxb);
4307 else
4308 IWL_WARNING("Claim null rxb?\n");
4309 }
4310
4311 /* For now we just don't re-use anything. We can tweak this
4312 * later to try and re-use notification packets and SKBs that
4313 * fail to Rx correctly */
4314 if (rxb->skb != NULL) {
4315 priv->alloc_rxb_skb--;
4316 dev_kfree_skb_any(rxb->skb);
4317 rxb->skb = NULL;
4318 }
4319
4320 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4321 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4322 spin_lock_irqsave(&rxq->lock, flags);
4323 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4324 spin_unlock_irqrestore(&rxq->lock, flags);
4325 i = (i + 1) & RX_QUEUE_MASK;
4326 }
4327
4328 /* Backtrack one entry */
4329 priv->rxq.read = i;
4330 iwl_rx_queue_restock(priv);
4331}
4332
Christoph Hellwig416e1432007-10-25 17:15:49 +08004333static int iwl_tx_queue_update_write_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004334 struct iwl_tx_queue *txq)
4335{
4336 u32 reg = 0;
4337 int rc = 0;
4338 int txq_id = txq->q.id;
4339
4340 if (txq->need_update == 0)
4341 return rc;
4342
4343 /* if we're trying to save power */
4344 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4345 /* wake up nic if it's powered down ...
4346 * uCode will wake up, and interrupt us again, so next
4347 * time we'll skip this part. */
4348 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4349
4350 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4351 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
4352 iwl_set_bit(priv, CSR_GP_CNTRL,
4353 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4354 return rc;
4355 }
4356
4357 /* restore this queue's parameters in nic hardware. */
Tomas Winklerac17a942007-10-25 17:15:37 +08004358 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004359 if (rc)
4360 return rc;
Tomas Winklerac17a942007-10-25 17:15:37 +08004361 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004362 txq->q.write_ptr | (txq_id << 8));
Tomas Winklerac17a942007-10-25 17:15:37 +08004363 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004364
4365 /* else not in power-save mode, uCode will never sleep when we're
4366 * trying to tx (during RFKILL, we're not trying to tx). */
4367 } else
4368 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004369 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004370
4371 txq->need_update = 0;
4372
4373 return rc;
4374}
4375
4376#ifdef CONFIG_IWLWIFI_DEBUG
4377static void iwl_print_rx_config_cmd(struct iwl_rxon_cmd *rxon)
4378{
Joe Perches0795af52007-10-03 17:59:30 -07004379 DECLARE_MAC_BUF(mac);
4380
Zhu Yib481de92007-09-25 17:54:57 -07004381 IWL_DEBUG_RADIO("RX CONFIG:\n");
4382 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
4383 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4384 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4385 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4386 le32_to_cpu(rxon->filter_flags));
4387 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4388 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4389 rxon->ofdm_basic_rates);
4390 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004391 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4392 print_mac(mac, rxon->node_addr));
4393 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4394 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004395 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4396}
4397#endif
4398
4399static void iwl_enable_interrupts(struct iwl_priv *priv)
4400{
4401 IWL_DEBUG_ISR("Enabling interrupts\n");
4402 set_bit(STATUS_INT_ENABLED, &priv->status);
4403 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
4404}
4405
4406static inline void iwl_disable_interrupts(struct iwl_priv *priv)
4407{
4408 clear_bit(STATUS_INT_ENABLED, &priv->status);
4409
4410 /* disable interrupts from uCode/NIC to host */
4411 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
4412
4413 /* acknowledge/clear/reset any interrupts still pending
4414 * from uCode or flow handler (Rx/Tx DMA) */
4415 iwl_write32(priv, CSR_INT, 0xffffffff);
4416 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
4417 IWL_DEBUG_ISR("Disabled interrupts\n");
4418}
4419
4420static const char *desc_lookup(int i)
4421{
4422 switch (i) {
4423 case 1:
4424 return "FAIL";
4425 case 2:
4426 return "BAD_PARAM";
4427 case 3:
4428 return "BAD_CHECKSUM";
4429 case 4:
4430 return "NMI_INTERRUPT";
4431 case 5:
4432 return "SYSASSERT";
4433 case 6:
4434 return "FATAL_ERROR";
4435 }
4436
4437 return "UNKNOWN";
4438}
4439
4440#define ERROR_START_OFFSET (1 * sizeof(u32))
4441#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4442
4443static void iwl_dump_nic_error_log(struct iwl_priv *priv)
4444{
4445 u32 i;
4446 u32 desc, time, count, base, data1;
4447 u32 blink1, blink2, ilink1, ilink2;
4448 int rc;
4449
4450 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4451
4452 if (!iwl_hw_valid_rtc_data_addr(base)) {
4453 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4454 return;
4455 }
4456
Tomas Winklerac17a942007-10-25 17:15:37 +08004457 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004458 if (rc) {
4459 IWL_WARNING("Can not read from adapter at this time.\n");
4460 return;
4461 }
4462
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004463 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004464
4465 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4466 IWL_ERROR("Start IWL Error Log Dump:\n");
4467 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4468 priv->status, priv->config, count);
4469 }
4470
4471 IWL_ERROR("Desc Time asrtPC blink2 "
4472 "ilink1 nmiPC Line\n");
4473 for (i = ERROR_START_OFFSET;
4474 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
4475 i += ERROR_ELEM_SIZE) {
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004476 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07004477 time =
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004478 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004479 blink1 =
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004480 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004481 blink2 =
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004482 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004483 ilink1 =
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004484 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004485 ilink2 =
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004486 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004487 data1 =
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004488 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004489
4490 IWL_ERROR
4491 ("%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
4492 desc_lookup(desc), desc, time, blink1, blink2,
4493 ilink1, ilink2, data1);
4494 }
4495
Tomas Winklerac17a942007-10-25 17:15:37 +08004496 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004497
4498}
4499
4500#define EVENT_START_OFFSET (4 * sizeof(u32))
4501
4502/**
4503 * iwl_print_event_log - Dump error event log to syslog
4504 *
Tomas Winklerac17a942007-10-25 17:15:37 +08004505 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004506 */
4507static void iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
4508 u32 num_events, u32 mode)
4509{
4510 u32 i;
4511 u32 base; /* SRAM byte address of event log header */
4512 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4513 u32 ptr; /* SRAM byte address of log data */
4514 u32 ev, time, data; /* event log data */
4515
4516 if (num_events == 0)
4517 return;
4518
4519 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4520
4521 if (mode == 0)
4522 event_size = 2 * sizeof(u32);
4523 else
4524 event_size = 3 * sizeof(u32);
4525
4526 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4527
4528 /* "time" is actually "data" for mode 0 (no timestamp).
4529 * place event id # at far right for easier visual parsing. */
4530 for (i = 0; i < num_events; i++) {
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004531 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004532 ptr += sizeof(u32);
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004533 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004534 ptr += sizeof(u32);
4535 if (mode == 0)
4536 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4537 else {
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004538 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004539 ptr += sizeof(u32);
4540 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4541 }
4542 }
4543}
4544
4545static void iwl_dump_nic_event_log(struct iwl_priv *priv)
4546{
4547 int rc;
4548 u32 base; /* SRAM byte address of event log header */
4549 u32 capacity; /* event log capacity in # entries */
4550 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4551 u32 num_wraps; /* # times uCode wrapped to top of log */
4552 u32 next_entry; /* index of next entry to be written by uCode */
4553 u32 size; /* # entries that we'll print */
4554
4555 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4556 if (!iwl_hw_valid_rtc_data_addr(base)) {
4557 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4558 return;
4559 }
4560
Tomas Winklerac17a942007-10-25 17:15:37 +08004561 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004562 if (rc) {
4563 IWL_WARNING("Can not read from adapter at this time.\n");
4564 return;
4565 }
4566
4567 /* event log header */
Tomas Winkleraf7cca22007-10-25 17:15:36 +08004568 capacity = iwl_read_targ_mem(priv, base);
4569 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4570 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4571 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004572
4573 size = num_wraps ? capacity : next_entry;
4574
4575 /* bail out if nothing in log */
4576 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004577 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winklerac17a942007-10-25 17:15:37 +08004578 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004579 return;
4580 }
4581
Zhu Yi583fab32007-09-27 11:27:30 +08004582 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004583 size, num_wraps);
4584
4585 /* if uCode has wrapped back to top of log, start at the oldest entry,
4586 * i.e the next one that uCode would fill. */
4587 if (num_wraps)
4588 iwl_print_event_log(priv, next_entry,
4589 capacity - next_entry, mode);
4590
4591 /* (then/else) start at top of log */
4592 iwl_print_event_log(priv, 0, next_entry, mode);
4593
Tomas Winklerac17a942007-10-25 17:15:37 +08004594 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004595}
4596
4597/**
4598 * iwl_irq_handle_error - called for HW or SW error interrupt from card
4599 */
4600static void iwl_irq_handle_error(struct iwl_priv *priv)
4601{
4602 /* Set the FW error flag -- cleared on iwl_down */
4603 set_bit(STATUS_FW_ERROR, &priv->status);
4604
4605 /* Cancel currently queued command. */
4606 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4607
4608#ifdef CONFIG_IWLWIFI_DEBUG
4609 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
4610 iwl_dump_nic_error_log(priv);
4611 iwl_dump_nic_event_log(priv);
4612 iwl_print_rx_config_cmd(&priv->staging_rxon);
4613 }
4614#endif
4615
4616 wake_up_interruptible(&priv->wait_command_queue);
4617
4618 /* Keep the restart process from trying to send host
4619 * commands by clearing the INIT status bit */
4620 clear_bit(STATUS_READY, &priv->status);
4621
4622 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4623 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4624 "Restarting adapter due to uCode error.\n");
4625
4626 if (iwl_is_associated(priv)) {
4627 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4628 sizeof(priv->recovery_rxon));
4629 priv->error_recovering = 1;
4630 }
4631 queue_work(priv->workqueue, &priv->restart);
4632 }
4633}
4634
4635static void iwl_error_recovery(struct iwl_priv *priv)
4636{
4637 unsigned long flags;
4638
4639 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4640 sizeof(priv->staging_rxon));
4641 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4642 iwl_commit_rxon(priv);
4643
Zhu Yi556f8db2007-09-27 11:27:33 +08004644 iwl_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004645
4646 spin_lock_irqsave(&priv->lock, flags);
4647 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4648 priv->error_recovering = 0;
4649 spin_unlock_irqrestore(&priv->lock, flags);
4650}
4651
4652static void iwl_irq_tasklet(struct iwl_priv *priv)
4653{
4654 u32 inta, handled = 0;
4655 u32 inta_fh;
4656 unsigned long flags;
4657#ifdef CONFIG_IWLWIFI_DEBUG
4658 u32 inta_mask;
4659#endif
4660
4661 spin_lock_irqsave(&priv->lock, flags);
4662
4663 /* Ack/clear/reset pending uCode interrupts.
4664 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4665 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
4666 inta = iwl_read32(priv, CSR_INT);
4667 iwl_write32(priv, CSR_INT, inta);
4668
4669 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4670 * Any new interrupts that happen after this, either while we're
4671 * in this tasklet, or later, will show up in next ISR/tasklet. */
4672 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4673 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
4674
4675#ifdef CONFIG_IWLWIFI_DEBUG
4676 if (iwl_debug_level & IWL_DL_ISR) {
4677 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4678 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4679 inta, inta_mask, inta_fh);
4680 }
4681#endif
4682
4683 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4684 * atomic, make sure that inta covers all the interrupts that
4685 * we've discovered, even if FH interrupt came in just after
4686 * reading CSR_INT. */
4687 if (inta_fh & CSR_FH_INT_RX_MASK)
4688 inta |= CSR_INT_BIT_FH_RX;
4689 if (inta_fh & CSR_FH_INT_TX_MASK)
4690 inta |= CSR_INT_BIT_FH_TX;
4691
4692 /* Now service all interrupt bits discovered above. */
4693 if (inta & CSR_INT_BIT_HW_ERR) {
4694 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4695
4696 /* Tell the device to stop sending interrupts */
4697 iwl_disable_interrupts(priv);
4698
4699 iwl_irq_handle_error(priv);
4700
4701 handled |= CSR_INT_BIT_HW_ERR;
4702
4703 spin_unlock_irqrestore(&priv->lock, flags);
4704
4705 return;
4706 }
4707
4708#ifdef CONFIG_IWLWIFI_DEBUG
4709 if (iwl_debug_level & (IWL_DL_ISR)) {
4710 /* NIC fires this, but we don't use it, redundant with WAKEUP */
4711 if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
4712 IWL_DEBUG_ISR("Microcode started or stopped.\n");
4713
4714 /* Alive notification via Rx interrupt will do the real work */
4715 if (inta & CSR_INT_BIT_ALIVE)
4716 IWL_DEBUG_ISR("Alive interrupt\n");
4717 }
4718#endif
4719 /* Safely ignore these bits for debug checks below */
4720 inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
4721
4722 /* HW RF KILL switch toggled (4965 only) */
4723 if (inta & CSR_INT_BIT_RF_KILL) {
4724 int hw_rf_kill = 0;
4725 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
4726 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4727 hw_rf_kill = 1;
4728
4729 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4730 "RF_KILL bit toggled to %s.\n",
4731 hw_rf_kill ? "disable radio":"enable radio");
4732
4733 /* Queue restart only if RF_KILL switch was set to "kill"
4734 * when we loaded driver, and is now set to "enable".
4735 * After we're Alive, RF_KILL gets handled by
4736 * iwl_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004737 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4738 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004739 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004740 }
Zhu Yib481de92007-09-25 17:54:57 -07004741
4742 handled |= CSR_INT_BIT_RF_KILL;
4743 }
4744
4745 /* Chip got too hot and stopped itself (4965 only) */
4746 if (inta & CSR_INT_BIT_CT_KILL) {
4747 IWL_ERROR("Microcode CT kill error detected.\n");
4748 handled |= CSR_INT_BIT_CT_KILL;
4749 }
4750
4751 /* Error detected by uCode */
4752 if (inta & CSR_INT_BIT_SW_ERR) {
4753 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4754 inta);
4755 iwl_irq_handle_error(priv);
4756 handled |= CSR_INT_BIT_SW_ERR;
4757 }
4758
4759 /* uCode wakes up after power-down sleep */
4760 if (inta & CSR_INT_BIT_WAKEUP) {
4761 IWL_DEBUG_ISR("Wakeup interrupt\n");
4762 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
4763 iwl_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4764 iwl_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4765 iwl_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4766 iwl_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4767 iwl_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4768 iwl_tx_queue_update_write_ptr(priv, &priv->txq[5]);
4769
4770 handled |= CSR_INT_BIT_WAKEUP;
4771 }
4772
4773 /* All uCode command responses, including Tx command responses,
4774 * Rx "responses" (frame-received notification), and other
4775 * notifications from uCode come through here*/
4776 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
4777 iwl_rx_handle(priv);
4778 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4779 }
4780
4781 if (inta & CSR_INT_BIT_FH_TX) {
4782 IWL_DEBUG_ISR("Tx interrupt\n");
4783
4784 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
Tomas Winklerac17a942007-10-25 17:15:37 +08004785 if (!iwl_grab_nic_access(priv)) {
4786 iwl_write_direct32(priv,
Zhu Yib481de92007-09-25 17:54:57 -07004787 FH_TCSR_CREDIT
4788 (ALM_FH_SRVC_CHNL), 0x0);
Tomas Winklerac17a942007-10-25 17:15:37 +08004789 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004790 }
4791 handled |= CSR_INT_BIT_FH_TX;
4792 }
4793
4794 if (inta & ~handled)
4795 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4796
4797 if (inta & ~CSR_INI_SET_MASK) {
4798 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4799 inta & ~CSR_INI_SET_MASK);
4800 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4801 }
4802
4803 /* Re-enable all interrupts */
4804 iwl_enable_interrupts(priv);
4805
4806#ifdef CONFIG_IWLWIFI_DEBUG
4807 if (iwl_debug_level & (IWL_DL_ISR)) {
4808 inta = iwl_read32(priv, CSR_INT);
4809 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4810 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4811 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4812 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4813 }
4814#endif
4815 spin_unlock_irqrestore(&priv->lock, flags);
4816}
4817
4818static irqreturn_t iwl_isr(int irq, void *data)
4819{
4820 struct iwl_priv *priv = data;
4821 u32 inta, inta_mask;
4822 u32 inta_fh;
4823 if (!priv)
4824 return IRQ_NONE;
4825
4826 spin_lock(&priv->lock);
4827
4828 /* Disable (but don't clear!) interrupts here to avoid
4829 * back-to-back ISRs and sporadic interrupts from our NIC.
4830 * If we have something to service, the tasklet will re-enable ints.
4831 * If we *don't* have something, we'll re-enable before leaving here. */
4832 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4833 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
4834
4835 /* Discover which interrupts are active/pending */
4836 inta = iwl_read32(priv, CSR_INT);
4837 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4838
4839 /* Ignore interrupt if there's nothing in NIC to service.
4840 * This may be due to IRQ shared with another device,
4841 * or due to sporadic interrupts thrown from our NIC. */
4842 if (!inta && !inta_fh) {
4843 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4844 goto none;
4845 }
4846
4847 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4848 /* Hardware disappeared */
4849 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004850 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004851 }
4852
4853 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4854 inta, inta_mask, inta_fh);
4855
4856 /* iwl_irq_tasklet() will service interrupts and re-enable them */
4857 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004858unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004859 spin_unlock(&priv->lock);
4860
4861 return IRQ_HANDLED;
4862
4863 none:
4864 /* re-enable interrupts here since we don't have anything to service. */
4865 iwl_enable_interrupts(priv);
4866 spin_unlock(&priv->lock);
4867 return IRQ_NONE;
4868}
4869
4870/************************** EEPROM BANDS ****************************
4871 *
4872 * The iwl_eeprom_band definitions below provide the mapping from the
4873 * EEPROM contents to the specific channel number supported for each
4874 * band.
4875 *
4876 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
4877 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4878 * The specific geography and calibration information for that channel
4879 * is contained in the eeprom map itself.
4880 *
4881 * During init, we copy the eeprom information and channel map
4882 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4883 *
4884 * channel_map_24/52 provides the index in the channel_info array for a
4885 * given channel. We have to have two separate maps as there is channel
4886 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4887 * band_2
4888 *
4889 * A value of 0xff stored in the channel_map indicates that the channel
4890 * is not supported by the hardware at all.
4891 *
4892 * A value of 0xfe in the channel_map indicates that the channel is not
4893 * valid for Tx with the current hardware. This means that
4894 * while the system can tune and receive on a given channel, it may not
4895 * be able to associate or transmit any frames on that
4896 * channel. There is no corresponding channel information for that
4897 * entry.
4898 *
4899 *********************************************************************/
4900
4901/* 2.4 GHz */
4902static const u8 iwl_eeprom_band_1[14] = {
4903 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4904};
4905
4906/* 5.2 GHz bands */
4907static const u8 iwl_eeprom_band_2[] = {
4908 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4909};
4910
4911static const u8 iwl_eeprom_band_3[] = { /* 5205-5320MHz */
4912 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4913};
4914
4915static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
4916 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4917};
4918
4919static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
4920 145, 149, 153, 157, 161, 165
4921};
4922
4923static void iwl_init_band_reference(const struct iwl_priv *priv, int band,
4924 int *eeprom_ch_count,
4925 const struct iwl_eeprom_channel
4926 **eeprom_ch_info,
4927 const u8 **eeprom_ch_index)
4928{
4929 switch (band) {
4930 case 1: /* 2.4GHz band */
4931 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
4932 *eeprom_ch_info = priv->eeprom.band_1_channels;
4933 *eeprom_ch_index = iwl_eeprom_band_1;
4934 break;
4935 case 2: /* 5.2GHz band */
4936 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
4937 *eeprom_ch_info = priv->eeprom.band_2_channels;
4938 *eeprom_ch_index = iwl_eeprom_band_2;
4939 break;
4940 case 3: /* 5.2GHz band */
4941 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
4942 *eeprom_ch_info = priv->eeprom.band_3_channels;
4943 *eeprom_ch_index = iwl_eeprom_band_3;
4944 break;
4945 case 4: /* 5.2GHz band */
4946 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
4947 *eeprom_ch_info = priv->eeprom.band_4_channels;
4948 *eeprom_ch_index = iwl_eeprom_band_4;
4949 break;
4950 case 5: /* 5.2GHz band */
4951 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
4952 *eeprom_ch_info = priv->eeprom.band_5_channels;
4953 *eeprom_ch_index = iwl_eeprom_band_5;
4954 break;
4955 default:
4956 BUG();
4957 return;
4958 }
4959}
4960
4961const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
4962 int phymode, u16 channel)
4963{
4964 int i;
4965
4966 switch (phymode) {
4967 case MODE_IEEE80211A:
4968 for (i = 14; i < priv->channel_count; i++) {
4969 if (priv->channel_info[i].channel == channel)
4970 return &priv->channel_info[i];
4971 }
4972 break;
4973
4974 case MODE_IEEE80211B:
4975 case MODE_IEEE80211G:
4976 if (channel >= 1 && channel <= 14)
4977 return &priv->channel_info[channel - 1];
4978 break;
4979
4980 }
4981
4982 return NULL;
4983}
4984
4985#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4986 ? # x " " : "")
4987
4988static int iwl_init_channel_map(struct iwl_priv *priv)
4989{
4990 int eeprom_ch_count = 0;
4991 const u8 *eeprom_ch_index = NULL;
4992 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
4993 int band, ch;
4994 struct iwl_channel_info *ch_info;
4995
4996 if (priv->channel_count) {
4997 IWL_DEBUG_INFO("Channel map already initialized.\n");
4998 return 0;
4999 }
5000
5001 if (priv->eeprom.version < 0x2f) {
5002 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5003 priv->eeprom.version);
5004 return -EINVAL;
5005 }
5006
5007 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5008
5009 priv->channel_count =
5010 ARRAY_SIZE(iwl_eeprom_band_1) +
5011 ARRAY_SIZE(iwl_eeprom_band_2) +
5012 ARRAY_SIZE(iwl_eeprom_band_3) +
5013 ARRAY_SIZE(iwl_eeprom_band_4) +
5014 ARRAY_SIZE(iwl_eeprom_band_5);
5015
5016 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5017
5018 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
5019 priv->channel_count, GFP_KERNEL);
5020 if (!priv->channel_info) {
5021 IWL_ERROR("Could not allocate channel_info\n");
5022 priv->channel_count = 0;
5023 return -ENOMEM;
5024 }
5025
5026 ch_info = priv->channel_info;
5027
5028 /* Loop through the 5 EEPROM bands adding them in order to the
5029 * channel map we maintain (that contains additional information than
5030 * what just in the EEPROM) */
5031 for (band = 1; band <= 5; band++) {
5032
5033 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5034 &eeprom_ch_info, &eeprom_ch_index);
5035
5036 /* Loop through each band adding each of the channels */
5037 for (ch = 0; ch < eeprom_ch_count; ch++) {
5038 ch_info->channel = eeprom_ch_index[ch];
5039 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5040 MODE_IEEE80211A;
5041
5042 /* permanently store EEPROM's channel regulatory flags
5043 * and max power in channel info database. */
5044 ch_info->eeprom = eeprom_ch_info[ch];
5045
5046 /* Copy the run-time flags so they are there even on
5047 * invalid channels */
5048 ch_info->flags = eeprom_ch_info[ch].flags;
5049
5050 if (!(is_channel_valid(ch_info))) {
5051 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5052 "No traffic\n",
5053 ch_info->channel,
5054 ch_info->flags,
5055 is_channel_a_band(ch_info) ?
5056 "5.2" : "2.4");
5057 ch_info++;
5058 continue;
5059 }
5060
5061 /* Initialize regulatory-based run-time data */
5062 ch_info->max_power_avg = ch_info->curr_txpow =
5063 eeprom_ch_info[ch].max_power_avg;
5064 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5065 ch_info->min_power = 0;
5066
5067 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5068 " %ddBm): Ad-Hoc %ssupported\n",
5069 ch_info->channel,
5070 is_channel_a_band(ch_info) ?
5071 "5.2" : "2.4",
5072 CHECK_AND_PRINT(IBSS),
5073 CHECK_AND_PRINT(ACTIVE),
5074 CHECK_AND_PRINT(RADAR),
5075 CHECK_AND_PRINT(WIDE),
5076 CHECK_AND_PRINT(NARROW),
5077 CHECK_AND_PRINT(DFS),
5078 eeprom_ch_info[ch].flags,
5079 eeprom_ch_info[ch].max_power_avg,
5080 ((eeprom_ch_info[ch].
5081 flags & EEPROM_CHANNEL_IBSS)
5082 && !(eeprom_ch_info[ch].
5083 flags & EEPROM_CHANNEL_RADAR))
5084 ? "" : "not ");
5085
5086 /* Set the user_txpower_limit to the highest power
5087 * supported by any channel */
5088 if (eeprom_ch_info[ch].max_power_avg >
5089 priv->user_txpower_limit)
5090 priv->user_txpower_limit =
5091 eeprom_ch_info[ch].max_power_avg;
5092
5093 ch_info++;
5094 }
5095 }
5096
5097 if (iwl3945_txpower_set_from_eeprom(priv))
5098 return -EIO;
5099
5100 return 0;
5101}
5102
5103/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5104 * sending probe req. This should be set long enough to hear probe responses
5105 * from more than one AP. */
5106#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5107#define IWL_ACTIVE_DWELL_TIME_52 (10)
5108
5109/* For faster active scanning, scan will move to the next channel if fewer than
5110 * PLCP_QUIET_THRESH packets are heard on this channel within
5111 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5112 * time if it's a quiet channel (nothing responded to our probe, and there's
5113 * no other traffic).
5114 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5115#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5116#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5117
5118/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5119 * Must be set longer than active dwell time.
5120 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5121#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5122#define IWL_PASSIVE_DWELL_TIME_52 (10)
5123#define IWL_PASSIVE_DWELL_BASE (100)
5124#define IWL_CHANNEL_TUNE_TIME 5
5125
5126static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv, int phymode)
5127{
5128 if (phymode == MODE_IEEE80211A)
5129 return IWL_ACTIVE_DWELL_TIME_52;
5130 else
5131 return IWL_ACTIVE_DWELL_TIME_24;
5132}
5133
5134static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, int phymode)
5135{
5136 u16 active = iwl_get_active_dwell_time(priv, phymode);
5137 u16 passive = (phymode != MODE_IEEE80211A) ?
5138 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5139 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5140
5141 if (iwl_is_associated(priv)) {
5142 /* If we're associated, we clamp the maximum passive
5143 * dwell time to be 98% of the beacon interval (minus
5144 * 2 * channel tune time) */
5145 passive = priv->beacon_int;
5146 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5147 passive = IWL_PASSIVE_DWELL_BASE;
5148 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5149 }
5150
5151 if (passive <= active)
5152 passive = active + 1;
5153
5154 return passive;
5155}
5156
5157static int iwl_get_channels_for_scan(struct iwl_priv *priv, int phymode,
5158 u8 is_active, u8 direct_mask,
5159 struct iwl_scan_channel *scan_ch)
5160{
5161 const struct ieee80211_channel *channels = NULL;
5162 const struct ieee80211_hw_mode *hw_mode;
5163 const struct iwl_channel_info *ch_info;
5164 u16 passive_dwell = 0;
5165 u16 active_dwell = 0;
5166 int added, i;
5167
5168 hw_mode = iwl_get_hw_mode(priv, phymode);
5169 if (!hw_mode)
5170 return 0;
5171
5172 channels = hw_mode->channels;
5173
5174 active_dwell = iwl_get_active_dwell_time(priv, phymode);
5175 passive_dwell = iwl_get_passive_dwell_time(priv, phymode);
5176
5177 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5178 if (channels[i].chan ==
5179 le16_to_cpu(priv->active_rxon.channel)) {
5180 if (iwl_is_associated(priv)) {
5181 IWL_DEBUG_SCAN
5182 ("Skipping current channel %d\n",
5183 le16_to_cpu(priv->active_rxon.channel));
5184 continue;
5185 }
5186 } else if (priv->only_active_channel)
5187 continue;
5188
5189 scan_ch->channel = channels[i].chan;
5190
5191 ch_info = iwl_get_channel_info(priv, phymode, scan_ch->channel);
5192 if (!is_channel_valid(ch_info)) {
5193 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5194 scan_ch->channel);
5195 continue;
5196 }
5197
5198 if (!is_active || is_channel_passive(ch_info) ||
5199 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5200 scan_ch->type = 0; /* passive */
5201 else
5202 scan_ch->type = 1; /* active */
5203
5204 if (scan_ch->type & 1)
5205 scan_ch->type |= (direct_mask << 1);
5206
5207 if (is_channel_narrow(ch_info))
5208 scan_ch->type |= (1 << 7);
5209
5210 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5211 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5212
5213 /* Set power levels to defaults */
5214 scan_ch->tpc.dsp_atten = 110;
5215 /* scan_pwr_info->tpc.dsp_atten; */
5216
5217 /*scan_pwr_info->tpc.tx_gain; */
5218 if (phymode == MODE_IEEE80211A)
5219 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5220 else {
5221 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5222 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
5223 * power level
5224 scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
5225 */
5226 }
5227
5228 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5229 scan_ch->channel,
5230 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5231 (scan_ch->type & 1) ?
5232 active_dwell : passive_dwell);
5233
5234 scan_ch++;
5235 added++;
5236 }
5237
5238 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5239 return added;
5240}
5241
5242static void iwl_reset_channel_flag(struct iwl_priv *priv)
5243{
5244 int i, j;
5245 for (i = 0; i < 3; i++) {
5246 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5247 for (j = 0; j < hw_mode->num_channels; j++)
5248 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5249 }
5250}
5251
5252static void iwl_init_hw_rates(struct iwl_priv *priv,
5253 struct ieee80211_rate *rates)
5254{
5255 int i;
5256
5257 for (i = 0; i < IWL_RATE_COUNT; i++) {
5258 rates[i].rate = iwl_rates[i].ieee * 5;
5259 rates[i].val = i; /* Rate scaling will work on indexes */
5260 rates[i].val2 = i;
5261 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5262 /* Only OFDM have the bits-per-symbol set */
5263 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5264 rates[i].flags |= IEEE80211_RATE_OFDM;
5265 else {
5266 /*
5267 * If CCK 1M then set rate flag to CCK else CCK_2
5268 * which is CCK | PREAMBLE2
5269 */
5270 rates[i].flags |= (iwl_rates[i].plcp == 10) ?
5271 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5272 }
5273
5274 /* Set up which ones are basic rates... */
5275 if (IWL_BASIC_RATES_MASK & (1 << i))
5276 rates[i].flags |= IEEE80211_RATE_BASIC;
5277 }
5278}
5279
5280/**
5281 * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
5282 */
5283static int iwl_init_geos(struct iwl_priv *priv)
5284{
5285 struct iwl_channel_info *ch;
5286 struct ieee80211_hw_mode *modes;
5287 struct ieee80211_channel *channels;
5288 struct ieee80211_channel *geo_ch;
5289 struct ieee80211_rate *rates;
5290 int i = 0;
5291 enum {
5292 A = 0,
5293 B = 1,
5294 G = 2,
5295 };
5296 int mode_count = 3;
5297
5298 if (priv->modes) {
5299 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5300 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5301 return 0;
5302 }
5303
5304 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5305 GFP_KERNEL);
5306 if (!modes)
5307 return -ENOMEM;
5308
5309 channels = kzalloc(sizeof(struct ieee80211_channel) *
5310 priv->channel_count, GFP_KERNEL);
5311 if (!channels) {
5312 kfree(modes);
5313 return -ENOMEM;
5314 }
5315
5316 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5317 GFP_KERNEL);
5318 if (!rates) {
5319 kfree(modes);
5320 kfree(channels);
5321 return -ENOMEM;
5322 }
5323
5324 /* 0 = 802.11a
5325 * 1 = 802.11b
5326 * 2 = 802.11g
5327 */
5328
5329 /* 5.2GHz channels start after the 2.4GHz channels */
5330 modes[A].mode = MODE_IEEE80211A;
5331 modes[A].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Mohamed Abbas14577f22007-11-12 11:37:42 +08005332 modes[A].rates = &rates[4];
Zhu Yib481de92007-09-25 17:54:57 -07005333 modes[A].num_rates = 8; /* just OFDM */
5334 modes[A].num_channels = 0;
5335
5336 modes[B].mode = MODE_IEEE80211B;
5337 modes[B].channels = channels;
Mohamed Abbas14577f22007-11-12 11:37:42 +08005338 modes[B].rates = rates;
Zhu Yib481de92007-09-25 17:54:57 -07005339 modes[B].num_rates = 4; /* just CCK */
5340 modes[B].num_channels = 0;
5341
5342 modes[G].mode = MODE_IEEE80211G;
5343 modes[G].channels = channels;
5344 modes[G].rates = rates;
5345 modes[G].num_rates = 12; /* OFDM & CCK */
5346 modes[G].num_channels = 0;
5347
5348 priv->ieee_channels = channels;
5349 priv->ieee_rates = rates;
5350
5351 iwl_init_hw_rates(priv, rates);
5352
5353 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5354 ch = &priv->channel_info[i];
5355
5356 if (!is_channel_valid(ch)) {
5357 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5358 "skipping.\n",
5359 ch->channel, is_channel_a_band(ch) ?
5360 "5.2" : "2.4");
5361 continue;
5362 }
5363
5364 if (is_channel_a_band(ch))
5365 geo_ch = &modes[A].channels[modes[A].num_channels++];
5366 else {
5367 geo_ch = &modes[B].channels[modes[B].num_channels++];
5368 modes[G].num_channels++;
5369 }
5370
5371 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5372 geo_ch->chan = ch->channel;
5373 geo_ch->power_level = ch->max_power_avg;
5374 geo_ch->antenna_max = 0xff;
5375
5376 if (is_channel_valid(ch)) {
5377 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5378 if (ch->flags & EEPROM_CHANNEL_IBSS)
5379 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5380
5381 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5382 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5383
5384 if (ch->flags & EEPROM_CHANNEL_RADAR)
5385 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5386
5387 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5388 priv->max_channel_txpower_limit =
5389 ch->max_power_avg;
5390 }
5391
5392 geo_ch->val = geo_ch->flag;
5393 }
5394
5395 if ((modes[A].num_channels == 0) && priv->is_abg) {
5396 printk(KERN_INFO DRV_NAME
5397 ": Incorrectly detected BG card as ABG. Please send "
5398 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5399 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5400 priv->is_abg = 0;
5401 }
5402
5403 printk(KERN_INFO DRV_NAME
5404 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5405 modes[G].num_channels, modes[A].num_channels);
5406
5407 /*
5408 * NOTE: We register these in preference of order -- the
5409 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5410 * a phymode based on rates or AP capabilities but seems to
5411 * configure it purely on if the channel being configured
5412 * is supported by a mode -- and the first match is taken
5413 */
5414
5415 if (modes[G].num_channels)
5416 ieee80211_register_hwmode(priv->hw, &modes[G]);
5417 if (modes[B].num_channels)
5418 ieee80211_register_hwmode(priv->hw, &modes[B]);
5419 if (modes[A].num_channels)
5420 ieee80211_register_hwmode(priv->hw, &modes[A]);
5421
5422 priv->modes = modes;
5423 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5424
5425 return 0;
5426}
5427
5428/******************************************************************************
5429 *
5430 * uCode download functions
5431 *
5432 ******************************************************************************/
5433
5434static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
5435{
5436 if (priv->ucode_code.v_addr != NULL) {
5437 pci_free_consistent(priv->pci_dev,
5438 priv->ucode_code.len,
5439 priv->ucode_code.v_addr,
5440 priv->ucode_code.p_addr);
5441 priv->ucode_code.v_addr = NULL;
5442 }
5443 if (priv->ucode_data.v_addr != NULL) {
5444 pci_free_consistent(priv->pci_dev,
5445 priv->ucode_data.len,
5446 priv->ucode_data.v_addr,
5447 priv->ucode_data.p_addr);
5448 priv->ucode_data.v_addr = NULL;
5449 }
5450 if (priv->ucode_data_backup.v_addr != NULL) {
5451 pci_free_consistent(priv->pci_dev,
5452 priv->ucode_data_backup.len,
5453 priv->ucode_data_backup.v_addr,
5454 priv->ucode_data_backup.p_addr);
5455 priv->ucode_data_backup.v_addr = NULL;
5456 }
5457 if (priv->ucode_init.v_addr != NULL) {
5458 pci_free_consistent(priv->pci_dev,
5459 priv->ucode_init.len,
5460 priv->ucode_init.v_addr,
5461 priv->ucode_init.p_addr);
5462 priv->ucode_init.v_addr = NULL;
5463 }
5464 if (priv->ucode_init_data.v_addr != NULL) {
5465 pci_free_consistent(priv->pci_dev,
5466 priv->ucode_init_data.len,
5467 priv->ucode_init_data.v_addr,
5468 priv->ucode_init_data.p_addr);
5469 priv->ucode_init_data.v_addr = NULL;
5470 }
5471 if (priv->ucode_boot.v_addr != NULL) {
5472 pci_free_consistent(priv->pci_dev,
5473 priv->ucode_boot.len,
5474 priv->ucode_boot.v_addr,
5475 priv->ucode_boot.p_addr);
5476 priv->ucode_boot.v_addr = NULL;
5477 }
5478}
5479
5480/**
5481 * iwl_verify_inst_full - verify runtime uCode image in card vs. host,
5482 * looking at all data.
5483 */
5484static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 * image, u32 len)
5485{
5486 u32 val;
5487 u32 save_len = len;
5488 int rc = 0;
5489 u32 errcnt;
5490
5491 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5492
Tomas Winklerac17a942007-10-25 17:15:37 +08005493 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005494 if (rc)
5495 return rc;
5496
Tomas Winklerac17a942007-10-25 17:15:37 +08005497 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005498
5499 errcnt = 0;
5500 for (; len > 0; len -= sizeof(u32), image++) {
5501 /* read data comes through single port, auto-incr addr */
5502 /* NOTE: Use the debugless read so we don't flood kernel log
5503 * if IWL_DL_IO is set */
Tomas Winklerac17a942007-10-25 17:15:37 +08005504 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005505 if (val != le32_to_cpu(*image)) {
5506 IWL_ERROR("uCode INST section is invalid at "
5507 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5508 save_len - len, val, le32_to_cpu(*image));
5509 rc = -EIO;
5510 errcnt++;
5511 if (errcnt >= 20)
5512 break;
5513 }
5514 }
5515
Tomas Winklerac17a942007-10-25 17:15:37 +08005516 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005517
5518 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08005519 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07005520
5521 return rc;
5522}
5523
5524
5525/**
5526 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
5527 * using sample data 100 bytes apart. If these sample points are good,
5528 * it's a pretty good bet that everything between them is good, too.
5529 */
5530static int iwl_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
5531{
5532 u32 val;
5533 int rc = 0;
5534 u32 errcnt = 0;
5535 u32 i;
5536
5537 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5538
Tomas Winklerac17a942007-10-25 17:15:37 +08005539 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005540 if (rc)
5541 return rc;
5542
5543 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5544 /* read data comes through single port, auto-incr addr */
5545 /* NOTE: Use the debugless read so we don't flood kernel log
5546 * if IWL_DL_IO is set */
Tomas Winklerac17a942007-10-25 17:15:37 +08005547 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005548 i + RTC_INST_LOWER_BOUND);
Tomas Winklerac17a942007-10-25 17:15:37 +08005549 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005550 if (val != le32_to_cpu(*image)) {
5551#if 0 /* Enable this if you want to see details */
5552 IWL_ERROR("uCode INST section is invalid at "
5553 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5554 i, val, *image);
5555#endif
5556 rc = -EIO;
5557 errcnt++;
5558 if (errcnt >= 3)
5559 break;
5560 }
5561 }
5562
Tomas Winklerac17a942007-10-25 17:15:37 +08005563 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005564
5565 return rc;
5566}
5567
5568
5569/**
5570 * iwl_verify_ucode - determine which instruction image is in SRAM,
5571 * and verify its contents
5572 */
5573static int iwl_verify_ucode(struct iwl_priv *priv)
5574{
5575 __le32 *image;
5576 u32 len;
5577 int rc = 0;
5578
5579 /* Try bootstrap */
5580 image = (__le32 *)priv->ucode_boot.v_addr;
5581 len = priv->ucode_boot.len;
5582 rc = iwl_verify_inst_sparse(priv, image, len);
5583 if (rc == 0) {
5584 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5585 return 0;
5586 }
5587
5588 /* Try initialize */
5589 image = (__le32 *)priv->ucode_init.v_addr;
5590 len = priv->ucode_init.len;
5591 rc = iwl_verify_inst_sparse(priv, image, len);
5592 if (rc == 0) {
5593 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5594 return 0;
5595 }
5596
5597 /* Try runtime/protocol */
5598 image = (__le32 *)priv->ucode_code.v_addr;
5599 len = priv->ucode_code.len;
5600 rc = iwl_verify_inst_sparse(priv, image, len);
5601 if (rc == 0) {
5602 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5603 return 0;
5604 }
5605
5606 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5607
5608 /* Show first several data entries in instruction SRAM.
5609 * Selection of bootstrap image is arbitrary. */
5610 image = (__le32 *)priv->ucode_boot.v_addr;
5611 len = priv->ucode_boot.len;
5612 rc = iwl_verify_inst_full(priv, image, len);
5613
5614 return rc;
5615}
5616
5617
5618/* check contents of special bootstrap uCode SRAM */
5619static int iwl_verify_bsm(struct iwl_priv *priv)
5620{
5621 __le32 *image = priv->ucode_boot.v_addr;
5622 u32 len = priv->ucode_boot.len;
5623 u32 reg;
5624 u32 val;
5625
5626 IWL_DEBUG_INFO("Begin verify bsm\n");
5627
5628 /* verify BSM SRAM contents */
Tomas Winklerd8609652007-10-25 17:15:35 +08005629 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005630 for (reg = BSM_SRAM_LOWER_BOUND;
5631 reg < BSM_SRAM_LOWER_BOUND + len;
5632 reg += sizeof(u32), image ++) {
Tomas Winklerd8609652007-10-25 17:15:35 +08005633 val = iwl_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005634 if (val != le32_to_cpu(*image)) {
5635 IWL_ERROR("BSM uCode verification failed at "
5636 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5637 BSM_SRAM_LOWER_BOUND,
5638 reg - BSM_SRAM_LOWER_BOUND, len,
5639 val, le32_to_cpu(*image));
5640 return -EIO;
5641 }
5642 }
5643
5644 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5645
5646 return 0;
5647}
5648
5649/**
5650 * iwl_load_bsm - Load bootstrap instructions
5651 *
5652 * BSM operation:
5653 *
5654 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5655 * in special SRAM that does not power down during RFKILL. When powering back
5656 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5657 * the bootstrap program into the on-board processor, and starts it.
5658 *
5659 * The bootstrap program loads (via DMA) instructions and data for a new
5660 * program from host DRAM locations indicated by the host driver in the
5661 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5662 * automatically.
5663 *
5664 * When initializing the NIC, the host driver points the BSM to the
5665 * "initialize" uCode image. This uCode sets up some internal data, then
5666 * notifies host via "initialize alive" that it is complete.
5667 *
5668 * The host then replaces the BSM_DRAM_* pointer values to point to the
5669 * normal runtime uCode instructions and a backup uCode data cache buffer
5670 * (filled initially with starting data values for the on-board processor),
5671 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5672 * which begins normal operation.
5673 *
5674 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5675 * the backup data cache in DRAM before SRAM is powered down.
5676 *
5677 * When powering back up, the BSM loads the bootstrap program. This reloads
5678 * the runtime uCode instructions and the backup data cache into SRAM,
5679 * and re-launches the runtime uCode from where it left off.
5680 */
5681static int iwl_load_bsm(struct iwl_priv *priv)
5682{
5683 __le32 *image = priv->ucode_boot.v_addr;
5684 u32 len = priv->ucode_boot.len;
5685 dma_addr_t pinst;
5686 dma_addr_t pdata;
5687 u32 inst_len;
5688 u32 data_len;
5689 int rc;
5690 int i;
5691 u32 done;
5692 u32 reg_offset;
5693
5694 IWL_DEBUG_INFO("Begin load bsm\n");
5695
5696 /* make sure bootstrap program is no larger than BSM's SRAM size */
5697 if (len > IWL_MAX_BSM_SIZE)
5698 return -EINVAL;
5699
5700 /* Tell bootstrap uCode where to find the "Initialize" uCode
5701 * in host DRAM ... bits 31:0 for 3945, bits 35:4 for 4965.
5702 * NOTE: iwl_initialize_alive_start() will replace these values,
5703 * after the "initialize" uCode has run, to point to
5704 * runtime/protocol instructions and backup data cache. */
5705 pinst = priv->ucode_init.p_addr;
5706 pdata = priv->ucode_init_data.p_addr;
5707 inst_len = priv->ucode_init.len;
5708 data_len = priv->ucode_init_data.len;
5709
Tomas Winklerac17a942007-10-25 17:15:37 +08005710 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005711 if (rc)
5712 return rc;
5713
Tomas Winklerd8609652007-10-25 17:15:35 +08005714 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5715 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5716 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5717 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005718
5719 /* Fill BSM memory with bootstrap instructions */
5720 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5721 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5722 reg_offset += sizeof(u32), image++)
Tomas Winklerd8609652007-10-25 17:15:35 +08005723 _iwl_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005724 le32_to_cpu(*image));
5725
5726 rc = iwl_verify_bsm(priv);
5727 if (rc) {
Tomas Winklerac17a942007-10-25 17:15:37 +08005728 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005729 return rc;
5730 }
5731
5732 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Tomas Winklerd8609652007-10-25 17:15:35 +08005733 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5734 iwl_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005735 RTC_INST_LOWER_BOUND);
Tomas Winklerd8609652007-10-25 17:15:35 +08005736 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005737
5738 /* Load bootstrap code into instruction SRAM now,
5739 * to prepare to load "initialize" uCode */
Tomas Winklerd8609652007-10-25 17:15:35 +08005740 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005741 BSM_WR_CTRL_REG_BIT_START);
5742
5743 /* Wait for load of bootstrap uCode to finish */
5744 for (i = 0; i < 100; i++) {
Tomas Winklerd8609652007-10-25 17:15:35 +08005745 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005746 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5747 break;
5748 udelay(10);
5749 }
5750 if (i < 100)
5751 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5752 else {
5753 IWL_ERROR("BSM write did not complete!\n");
5754 return -EIO;
5755 }
5756
5757 /* Enable future boot loads whenever power management unit triggers it
5758 * (e.g. when powering back up after power-save shutdown) */
Tomas Winklerd8609652007-10-25 17:15:35 +08005759 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005760 BSM_WR_CTRL_REG_BIT_START_EN);
5761
Tomas Winklerac17a942007-10-25 17:15:37 +08005762 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005763
5764 return 0;
5765}
5766
5767static void iwl_nic_start(struct iwl_priv *priv)
5768{
5769 /* Remove all resets to allow NIC to operate */
5770 iwl_write32(priv, CSR_RESET, 0);
5771}
5772
5773/**
5774 * iwl_read_ucode - Read uCode images from disk file.
5775 *
5776 * Copy into buffers for card to fetch via bus-mastering
5777 */
5778static int iwl_read_ucode(struct iwl_priv *priv)
5779{
5780 struct iwl_ucode *ucode;
5781 int rc = 0;
5782 const struct firmware *ucode_raw;
5783 /* firmware file name contains uCode/driver compatibility version */
5784 const char *name = "iwlwifi-3945" IWL3945_UCODE_API ".ucode";
5785 u8 *src;
5786 size_t len;
5787 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5788
5789 /* Ask kernel firmware_class module to get the boot firmware off disk.
5790 * request_firmware() is synchronous, file is in memory on return. */
5791 rc = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5792 if (rc < 0) {
5793 IWL_ERROR("%s firmware file req failed: Reason %d\n", name, rc);
5794 goto error;
5795 }
5796
5797 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5798 name, ucode_raw->size);
5799
5800 /* Make sure that we got at least our header! */
5801 if (ucode_raw->size < sizeof(*ucode)) {
5802 IWL_ERROR("File size way too small!\n");
5803 rc = -EINVAL;
5804 goto err_release;
5805 }
5806
5807 /* Data from ucode file: header followed by uCode images */
5808 ucode = (void *)ucode_raw->data;
5809
5810 ver = le32_to_cpu(ucode->ver);
5811 inst_size = le32_to_cpu(ucode->inst_size);
5812 data_size = le32_to_cpu(ucode->data_size);
5813 init_size = le32_to_cpu(ucode->init_size);
5814 init_data_size = le32_to_cpu(ucode->init_data_size);
5815 boot_size = le32_to_cpu(ucode->boot_size);
5816
5817 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08005818 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
5819 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
5820 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
5821 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
5822 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07005823
5824 /* Verify size of file vs. image size info in file's header */
5825 if (ucode_raw->size < sizeof(*ucode) +
5826 inst_size + data_size + init_size +
5827 init_data_size + boot_size) {
5828
5829 IWL_DEBUG_INFO("uCode file size %d too small\n",
5830 (int)ucode_raw->size);
5831 rc = -EINVAL;
5832 goto err_release;
5833 }
5834
5835 /* Verify that uCode images will fit in card's SRAM */
5836 if (inst_size > IWL_MAX_INST_SIZE) {
5837 IWL_DEBUG_INFO("uCode instr len %d too large to fit in card\n",
5838 (int)inst_size);
5839 rc = -EINVAL;
5840 goto err_release;
5841 }
5842
5843 if (data_size > IWL_MAX_DATA_SIZE) {
5844 IWL_DEBUG_INFO("uCode data len %d too large to fit in card\n",
5845 (int)data_size);
5846 rc = -EINVAL;
5847 goto err_release;
5848 }
5849 if (init_size > IWL_MAX_INST_SIZE) {
5850 IWL_DEBUG_INFO
5851 ("uCode init instr len %d too large to fit in card\n",
5852 (int)init_size);
5853 rc = -EINVAL;
5854 goto err_release;
5855 }
5856 if (init_data_size > IWL_MAX_DATA_SIZE) {
5857 IWL_DEBUG_INFO
5858 ("uCode init data len %d too large to fit in card\n",
5859 (int)init_data_size);
5860 rc = -EINVAL;
5861 goto err_release;
5862 }
5863 if (boot_size > IWL_MAX_BSM_SIZE) {
5864 IWL_DEBUG_INFO
5865 ("uCode boot instr len %d too large to fit in bsm\n",
5866 (int)boot_size);
5867 rc = -EINVAL;
5868 goto err_release;
5869 }
5870
5871 /* Allocate ucode buffers for card's bus-master loading ... */
5872
5873 /* Runtime instructions and 2 copies of data:
5874 * 1) unmodified from disk
5875 * 2) backup cache for save/restore during power-downs */
5876 priv->ucode_code.len = inst_size;
5877 priv->ucode_code.v_addr =
5878 pci_alloc_consistent(priv->pci_dev,
5879 priv->ucode_code.len,
5880 &(priv->ucode_code.p_addr));
5881
5882 priv->ucode_data.len = data_size;
5883 priv->ucode_data.v_addr =
5884 pci_alloc_consistent(priv->pci_dev,
5885 priv->ucode_data.len,
5886 &(priv->ucode_data.p_addr));
5887
5888 priv->ucode_data_backup.len = data_size;
5889 priv->ucode_data_backup.v_addr =
5890 pci_alloc_consistent(priv->pci_dev,
5891 priv->ucode_data_backup.len,
5892 &(priv->ucode_data_backup.p_addr));
5893
5894
5895 /* Initialization instructions and data */
5896 priv->ucode_init.len = init_size;
5897 priv->ucode_init.v_addr =
5898 pci_alloc_consistent(priv->pci_dev,
5899 priv->ucode_init.len,
5900 &(priv->ucode_init.p_addr));
5901
5902 priv->ucode_init_data.len = init_data_size;
5903 priv->ucode_init_data.v_addr =
5904 pci_alloc_consistent(priv->pci_dev,
5905 priv->ucode_init_data.len,
5906 &(priv->ucode_init_data.p_addr));
5907
5908 /* Bootstrap (instructions only, no data) */
5909 priv->ucode_boot.len = boot_size;
5910 priv->ucode_boot.v_addr =
5911 pci_alloc_consistent(priv->pci_dev,
5912 priv->ucode_boot.len,
5913 &(priv->ucode_boot.p_addr));
5914
5915 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
5916 !priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr ||
5917 !priv->ucode_boot.v_addr || !priv->ucode_data_backup.v_addr)
5918 goto err_pci_alloc;
5919
5920 /* Copy images into buffers for card's bus-master reads ... */
5921
5922 /* Runtime instructions (first block of data in file) */
5923 src = &ucode->data[0];
5924 len = priv->ucode_code.len;
5925 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %d\n",
5926 (int)len);
5927 memcpy(priv->ucode_code.v_addr, src, len);
5928 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5929 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5930
5931 /* Runtime data (2nd block)
5932 * NOTE: Copy into backup buffer will be done in iwl_up() */
5933 src = &ucode->data[inst_size];
5934 len = priv->ucode_data.len;
5935 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %d\n",
5936 (int)len);
5937 memcpy(priv->ucode_data.v_addr, src, len);
5938 memcpy(priv->ucode_data_backup.v_addr, src, len);
5939
5940 /* Initialization instructions (3rd block) */
5941 if (init_size) {
5942 src = &ucode->data[inst_size + data_size];
5943 len = priv->ucode_init.len;
5944 IWL_DEBUG_INFO("Copying (but not loading) init instr len %d\n",
5945 (int)len);
5946 memcpy(priv->ucode_init.v_addr, src, len);
5947 }
5948
5949 /* Initialization data (4th block) */
5950 if (init_data_size) {
5951 src = &ucode->data[inst_size + data_size + init_size];
5952 len = priv->ucode_init_data.len;
5953 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5954 (int)len);
5955 memcpy(priv->ucode_init_data.v_addr, src, len);
5956 }
5957
5958 /* Bootstrap instructions (5th block) */
5959 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5960 len = priv->ucode_boot.len;
5961 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5962 (int)len);
5963 memcpy(priv->ucode_boot.v_addr, src, len);
5964
5965 /* We have our copies now, allow OS release its copies */
5966 release_firmware(ucode_raw);
5967 return 0;
5968
5969 err_pci_alloc:
5970 IWL_ERROR("failed to allocate pci memory\n");
5971 rc = -ENOMEM;
5972 iwl_dealloc_ucode_pci(priv);
5973
5974 err_release:
5975 release_firmware(ucode_raw);
5976
5977 error:
5978 return rc;
5979}
5980
5981
5982/**
5983 * iwl_set_ucode_ptrs - Set uCode address location
5984 *
5985 * Tell initialization uCode where to find runtime uCode.
5986 *
5987 * BSM registers initially contain pointers to initialization uCode.
5988 * We need to replace them to load runtime uCode inst and data,
5989 * and to save runtime data when powering down.
5990 */
5991static int iwl_set_ucode_ptrs(struct iwl_priv *priv)
5992{
5993 dma_addr_t pinst;
5994 dma_addr_t pdata;
5995 int rc = 0;
5996 unsigned long flags;
5997
5998 /* bits 31:0 for 3945 */
5999 pinst = priv->ucode_code.p_addr;
6000 pdata = priv->ucode_data_backup.p_addr;
6001
6002 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerac17a942007-10-25 17:15:37 +08006003 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006004 if (rc) {
6005 spin_unlock_irqrestore(&priv->lock, flags);
6006 return rc;
6007 }
6008
6009 /* Tell bootstrap uCode where to find image to load */
Tomas Winklerd8609652007-10-25 17:15:35 +08006010 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6011 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6012 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006013 priv->ucode_data.len);
6014
6015 /* Inst bytecount must be last to set up, bit 31 signals uCode
6016 * that all new ptr/size info is in place */
Tomas Winklerd8609652007-10-25 17:15:35 +08006017 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006018 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6019
Tomas Winklerac17a942007-10-25 17:15:37 +08006020 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006021
6022 spin_unlock_irqrestore(&priv->lock, flags);
6023
6024 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6025
6026 return rc;
6027}
6028
6029/**
Ian Schram01ebd062007-10-25 17:15:22 +08006030 * iwl_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006031 *
6032 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6033 *
6034 * The 4965 "initialize" ALIVE reply contains calibration data for:
6035 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6036 * (3945 does not contain this data).
6037 *
6038 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6039*/
6040static void iwl_init_alive_start(struct iwl_priv *priv)
6041{
6042 /* Check alive response for "valid" sign from uCode */
6043 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6044 /* We had an error bringing up the hardware, so take it
6045 * all the way back down so we can try again */
6046 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6047 goto restart;
6048 }
6049
6050 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6051 * This is a paranoid check, because we would not have gotten the
6052 * "initialize" alive if code weren't properly loaded. */
6053 if (iwl_verify_ucode(priv)) {
6054 /* Runtime instruction load was bad;
6055 * take it all the way back down so we can try again */
6056 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6057 goto restart;
6058 }
6059
6060 /* Send pointers to protocol/runtime uCode image ... init code will
6061 * load and launch runtime uCode, which will send us another "Alive"
6062 * notification. */
6063 IWL_DEBUG_INFO("Initialization Alive received.\n");
6064 if (iwl_set_ucode_ptrs(priv)) {
6065 /* Runtime instruction load won't happen;
6066 * take it all the way back down so we can try again */
6067 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6068 goto restart;
6069 }
6070 return;
6071
6072 restart:
6073 queue_work(priv->workqueue, &priv->restart);
6074}
6075
6076
6077/**
6078 * iwl_alive_start - called after REPLY_ALIVE notification received
6079 * from protocol/runtime uCode (initialization uCode's
6080 * Alive gets handled by iwl_init_alive_start()).
6081 */
6082static void iwl_alive_start(struct iwl_priv *priv)
6083{
6084 int rc = 0;
6085 int thermal_spin = 0;
6086 u32 rfkill;
6087
6088 IWL_DEBUG_INFO("Runtime Alive received.\n");
6089
6090 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6091 /* We had an error bringing up the hardware, so take it
6092 * all the way back down so we can try again */
6093 IWL_DEBUG_INFO("Alive failed.\n");
6094 goto restart;
6095 }
6096
6097 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6098 * This is a paranoid check, because we would not have gotten the
6099 * "runtime" alive if code weren't properly loaded. */
6100 if (iwl_verify_ucode(priv)) {
6101 /* Runtime instruction load was bad;
6102 * take it all the way back down so we can try again */
6103 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6104 goto restart;
6105 }
6106
6107 iwl_clear_stations_table(priv);
6108
Tomas Winklerac17a942007-10-25 17:15:37 +08006109 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006110 if (rc) {
6111 IWL_WARNING("Can not read rfkill status from adapter\n");
6112 return;
6113 }
6114
Tomas Winklerd8609652007-10-25 17:15:35 +08006115 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07006116 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Tomas Winklerac17a942007-10-25 17:15:37 +08006117 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006118
6119 if (rfkill & 0x1) {
6120 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6121 /* if rfkill is not on, then wait for thermal
6122 * sensor in adapter to kick in */
6123 while (iwl_hw_get_temperature(priv) == 0) {
6124 thermal_spin++;
6125 udelay(10);
6126 }
6127
6128 if (thermal_spin)
6129 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
6130 thermal_spin * 10);
6131 } else
6132 set_bit(STATUS_RF_KILL_HW, &priv->status);
6133
6134 /* After the ALIVE response, we can process host commands */
6135 set_bit(STATUS_ALIVE, &priv->status);
6136
6137 /* Clear out the uCode error bit if it is set */
6138 clear_bit(STATUS_FW_ERROR, &priv->status);
6139
6140 rc = iwl_init_channel_map(priv);
6141 if (rc) {
6142 IWL_ERROR("initializing regulatory failed: %d\n", rc);
6143 return;
6144 }
6145
6146 iwl_init_geos(priv);
6147
6148 if (iwl_is_rfkill(priv))
6149 return;
6150
6151 if (!priv->mac80211_registered) {
6152 /* Unlock so any user space entry points can call back into
6153 * the driver without a deadlock... */
6154 mutex_unlock(&priv->mutex);
6155 iwl_rate_control_register(priv->hw);
6156 rc = ieee80211_register_hw(priv->hw);
6157 priv->hw->conf.beacon_int = 100;
6158 mutex_lock(&priv->mutex);
6159
6160 if (rc) {
Cyrill Gorcunova5acc372007-12-13 15:52:12 -08006161 iwl_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006162 IWL_ERROR("Failed to register network "
6163 "device (error %d)\n", rc);
6164 return;
6165 }
6166
6167 priv->mac80211_registered = 1;
6168
6169 iwl_reset_channel_flag(priv);
6170 } else
6171 ieee80211_start_queues(priv->hw);
6172
6173 priv->active_rate = priv->rates_mask;
6174 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6175
6176 iwl_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
6177
6178 if (iwl_is_associated(priv)) {
6179 struct iwl_rxon_cmd *active_rxon =
6180 (struct iwl_rxon_cmd *)(&priv->active_rxon);
6181
6182 memcpy(&priv->staging_rxon, &priv->active_rxon,
6183 sizeof(priv->staging_rxon));
6184 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6185 } else {
6186 /* Initialize our rx_config data */
6187 iwl_connection_init_rx_config(priv);
6188 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6189 }
6190
6191 /* Configure BT coexistence */
6192 iwl_send_bt_config(priv);
6193
6194 /* Configure the adapter for unassociated operation */
6195 iwl_commit_rxon(priv);
6196
6197 /* At this point, the NIC is initialized and operational */
6198 priv->notif_missed_beacons = 0;
6199 set_bit(STATUS_READY, &priv->status);
6200
6201 iwl3945_reg_txpower_periodic(priv);
6202
6203 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6204
6205 if (priv->error_recovering)
6206 iwl_error_recovery(priv);
6207
6208 return;
6209
6210 restart:
6211 queue_work(priv->workqueue, &priv->restart);
6212}
6213
6214static void iwl_cancel_deferred_work(struct iwl_priv *priv);
6215
6216static void __iwl_down(struct iwl_priv *priv)
6217{
6218 unsigned long flags;
6219 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6220 struct ieee80211_conf *conf = NULL;
6221
6222 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6223
6224 conf = ieee80211_get_hw_conf(priv->hw);
6225
6226 if (!exit_pending)
6227 set_bit(STATUS_EXIT_PENDING, &priv->status);
6228
6229 iwl_clear_stations_table(priv);
6230
6231 /* Unblock any waiting calls */
6232 wake_up_interruptible_all(&priv->wait_command_queue);
6233
Zhu Yib481de92007-09-25 17:54:57 -07006234 /* Wipe out the EXIT_PENDING status bit if we are not actually
6235 * exiting the module */
6236 if (!exit_pending)
6237 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6238
6239 /* stop and reset the on-board processor */
6240 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
6241
6242 /* tell the device to stop sending interrupts */
6243 iwl_disable_interrupts(priv);
6244
6245 if (priv->mac80211_registered)
6246 ieee80211_stop_queues(priv->hw);
6247
6248 /* If we have not previously called iwl_init() then
6249 * clear all bits but the RF Kill and SUSPEND bits and return */
6250 if (!iwl_is_init(priv)) {
6251 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6252 STATUS_RF_KILL_HW |
6253 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6254 STATUS_RF_KILL_SW |
6255 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6256 STATUS_IN_SUSPEND;
6257 goto exit;
6258 }
6259
6260 /* ...otherwise clear out all the status bits but the RF Kill and
6261 * SUSPEND bits and continue taking the NIC down. */
6262 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6263 STATUS_RF_KILL_HW |
6264 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6265 STATUS_RF_KILL_SW |
6266 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6267 STATUS_IN_SUSPEND |
6268 test_bit(STATUS_FW_ERROR, &priv->status) <<
6269 STATUS_FW_ERROR;
6270
6271 spin_lock_irqsave(&priv->lock, flags);
6272 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
6273 spin_unlock_irqrestore(&priv->lock, flags);
6274
6275 iwl_hw_txq_ctx_stop(priv);
6276 iwl_hw_rxq_stop(priv);
6277
6278 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerac17a942007-10-25 17:15:37 +08006279 if (!iwl_grab_nic_access(priv)) {
Tomas Winklerd8609652007-10-25 17:15:35 +08006280 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006281 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winklerac17a942007-10-25 17:15:37 +08006282 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006283 }
6284 spin_unlock_irqrestore(&priv->lock, flags);
6285
6286 udelay(5);
6287
6288 iwl_hw_nic_stop_master(priv);
6289 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6290 iwl_hw_nic_reset(priv);
6291
6292 exit:
6293 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
6294
6295 if (priv->ibss_beacon)
6296 dev_kfree_skb(priv->ibss_beacon);
6297 priv->ibss_beacon = NULL;
6298
6299 /* clear out any free frames */
6300 iwl_clear_free_frames(priv);
6301}
6302
6303static void iwl_down(struct iwl_priv *priv)
6304{
6305 mutex_lock(&priv->mutex);
6306 __iwl_down(priv);
6307 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006308
6309 iwl_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006310}
6311
6312#define MAX_HW_RESTARTS 5
6313
6314static int __iwl_up(struct iwl_priv *priv)
6315{
Joe Perches0795af52007-10-03 17:59:30 -07006316 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006317 int rc, i;
6318
6319 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6320 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6321 return -EIO;
6322 }
6323
6324 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6325 IWL_WARNING("Radio disabled by SW RF kill (module "
6326 "parameter)\n");
6327 return 0;
6328 }
6329
Reinette Chatrea781cf92008-01-21 10:08:31 -08006330 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6331 IWL_ERROR("ucode not available for device bringup\n");
6332 return -EIO;
6333 }
6334
Zhu Yib481de92007-09-25 17:54:57 -07006335 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6336
6337 rc = iwl_hw_nic_init(priv);
6338 if (rc) {
6339 IWL_ERROR("Unable to int nic\n");
6340 return rc;
6341 }
6342
6343 /* make sure rfkill handshake bits are cleared */
6344 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6345 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
6346 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6347
6348 /* clear (again), then enable host interrupts */
6349 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6350 iwl_enable_interrupts(priv);
6351
6352 /* really make sure rfkill handshake bits are cleared */
6353 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6354 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6355
6356 /* Copy original ucode data image from disk into backup cache.
6357 * This will be used to initialize the on-board processor's
6358 * data SRAM for a clean start when the runtime program first loads. */
6359 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6360 priv->ucode_data.len);
6361
6362 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6363
6364 iwl_clear_stations_table(priv);
6365
6366 /* load bootstrap state machine,
6367 * load bootstrap program into processor's memory,
6368 * prepare to load the "initialize" uCode */
6369 rc = iwl_load_bsm(priv);
6370
6371 if (rc) {
6372 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6373 continue;
6374 }
6375
6376 /* start card; "initialize" will load runtime ucode */
6377 iwl_nic_start(priv);
6378
6379 /* MAC Address location in EEPROM same for 3945/4965 */
6380 get_eeprom_mac(priv, priv->mac_addr);
Joe Perches0795af52007-10-03 17:59:30 -07006381 IWL_DEBUG_INFO("MAC address: %s\n",
6382 print_mac(mac, priv->mac_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006383
6384 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6385
6386 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6387
6388 return 0;
6389 }
6390
6391 set_bit(STATUS_EXIT_PENDING, &priv->status);
6392 __iwl_down(priv);
6393
6394 /* tried to restart and config the device for as long as our
6395 * patience could withstand */
6396 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6397 return -EIO;
6398}
6399
6400
6401/*****************************************************************************
6402 *
6403 * Workqueue callbacks
6404 *
6405 *****************************************************************************/
6406
6407static void iwl_bg_init_alive_start(struct work_struct *data)
6408{
6409 struct iwl_priv *priv =
6410 container_of(data, struct iwl_priv, init_alive_start.work);
6411
6412 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6413 return;
6414
6415 mutex_lock(&priv->mutex);
6416 iwl_init_alive_start(priv);
6417 mutex_unlock(&priv->mutex);
6418}
6419
6420static void iwl_bg_alive_start(struct work_struct *data)
6421{
6422 struct iwl_priv *priv =
6423 container_of(data, struct iwl_priv, alive_start.work);
6424
6425 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6426 return;
6427
6428 mutex_lock(&priv->mutex);
6429 iwl_alive_start(priv);
6430 mutex_unlock(&priv->mutex);
6431}
6432
6433static void iwl_bg_rf_kill(struct work_struct *work)
6434{
6435 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
6436
6437 wake_up_interruptible(&priv->wait_command_queue);
6438
6439 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6440 return;
6441
6442 mutex_lock(&priv->mutex);
6443
6444 if (!iwl_is_rfkill(priv)) {
6445 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6446 "HW and/or SW RF Kill no longer active, restarting "
6447 "device\n");
6448 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6449 queue_work(priv->workqueue, &priv->restart);
6450 } else {
6451
6452 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6453 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6454 "disabled by SW switch\n");
6455 else
6456 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6457 "Kill switch must be turned off for "
6458 "wireless networking to work.\n");
6459 }
6460 mutex_unlock(&priv->mutex);
6461}
6462
6463#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6464
6465static void iwl_bg_scan_check(struct work_struct *data)
6466{
6467 struct iwl_priv *priv =
6468 container_of(data, struct iwl_priv, scan_check.work);
6469
6470 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6471 return;
6472
6473 mutex_lock(&priv->mutex);
6474 if (test_bit(STATUS_SCANNING, &priv->status) ||
6475 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6476 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6477 "Scan completion watchdog resetting adapter (%dms)\n",
6478 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006479
Zhu Yib481de92007-09-25 17:54:57 -07006480 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006481 iwl_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006482 }
6483 mutex_unlock(&priv->mutex);
6484}
6485
6486static void iwl_bg_request_scan(struct work_struct *data)
6487{
6488 struct iwl_priv *priv =
6489 container_of(data, struct iwl_priv, request_scan);
6490 struct iwl_host_cmd cmd = {
6491 .id = REPLY_SCAN_CMD,
6492 .len = sizeof(struct iwl_scan_cmd),
6493 .meta.flags = CMD_SIZE_HUGE,
6494 };
6495 int rc = 0;
6496 struct iwl_scan_cmd *scan;
6497 struct ieee80211_conf *conf = NULL;
6498 u8 direct_mask;
6499 int phymode;
6500
6501 conf = ieee80211_get_hw_conf(priv->hw);
6502
6503 mutex_lock(&priv->mutex);
6504
6505 if (!iwl_is_ready(priv)) {
6506 IWL_WARNING("request scan called when driver not ready.\n");
6507 goto done;
6508 }
6509
6510 /* Make sure the scan wasn't cancelled before this queued work
6511 * was given the chance to run... */
6512 if (!test_bit(STATUS_SCANNING, &priv->status))
6513 goto done;
6514
6515 /* This should never be called or scheduled if there is currently
6516 * a scan active in the hardware. */
6517 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6518 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6519 "Ignoring second request.\n");
6520 rc = -EIO;
6521 goto done;
6522 }
6523
6524 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6525 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6526 goto done;
6527 }
6528
6529 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6530 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6531 goto done;
6532 }
6533
6534 if (iwl_is_rfkill(priv)) {
6535 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6536 goto done;
6537 }
6538
6539 if (!test_bit(STATUS_READY, &priv->status)) {
6540 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6541 goto done;
6542 }
6543
6544 if (!priv->scan_bands) {
6545 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6546 goto done;
6547 }
6548
6549 if (!priv->scan) {
6550 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
6551 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6552 if (!priv->scan) {
6553 rc = -ENOMEM;
6554 goto done;
6555 }
6556 }
6557 scan = priv->scan;
6558 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
6559
6560 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6561 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6562
6563 if (iwl_is_associated(priv)) {
6564 u16 interval = 0;
6565 u32 extra;
6566 u32 suspend_time = 100;
6567 u32 scan_suspend_time = 100;
6568 unsigned long flags;
6569
6570 IWL_DEBUG_INFO("Scanning while associated...\n");
6571
6572 spin_lock_irqsave(&priv->lock, flags);
6573 interval = priv->beacon_int;
6574 spin_unlock_irqrestore(&priv->lock, flags);
6575
6576 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006577 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006578 if (!interval)
6579 interval = suspend_time;
6580 /*
6581 * suspend time format:
6582 * 0-19: beacon interval in usec (time before exec.)
6583 * 20-23: 0
6584 * 24-31: number of beacons (suspend between channels)
6585 */
6586
6587 extra = (suspend_time / interval) << 24;
6588 scan_suspend_time = 0xFF0FFFFF &
6589 (extra | ((suspend_time % interval) * 1024));
6590
6591 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6592 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6593 scan_suspend_time, interval);
6594 }
6595
6596 /* We should add the ability for user to lock to PASSIVE ONLY */
6597 if (priv->one_direct_scan) {
6598 IWL_DEBUG_SCAN
6599 ("Kicking off one direct scan for '%s'\n",
6600 iwl_escape_essid(priv->direct_ssid,
6601 priv->direct_ssid_len));
6602 scan->direct_scan[0].id = WLAN_EID_SSID;
6603 scan->direct_scan[0].len = priv->direct_ssid_len;
6604 memcpy(scan->direct_scan[0].ssid,
6605 priv->direct_ssid, priv->direct_ssid_len);
6606 direct_mask = 1;
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006607 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006608 scan->direct_scan[0].id = WLAN_EID_SSID;
6609 scan->direct_scan[0].len = priv->essid_len;
6610 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6611 direct_mask = 1;
6612 } else
6613 direct_mask = 0;
6614
6615 /* We don't build a direct scan probe request; the uCode will do
6616 * that based on the direct_mask added to each channel entry */
6617 scan->tx_cmd.len = cpu_to_le16(
6618 iwl_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
6619 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
6620 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6621 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6622 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6623
6624 /* flags + rate selection */
6625
6626 switch (priv->scan_bands) {
6627 case 2:
6628 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6629 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
6630 scan->good_CRC_th = 0;
6631 phymode = MODE_IEEE80211G;
6632 break;
6633
6634 case 1:
6635 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
6636 scan->good_CRC_th = IWL_GOOD_CRC_TH;
6637 phymode = MODE_IEEE80211A;
6638 break;
6639
6640 default:
6641 IWL_WARNING("Invalid scan band count\n");
6642 goto done;
6643 }
6644
6645 /* select Rx antennas */
6646 scan->flags |= iwl3945_get_antenna_flags(priv);
6647
6648 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6649 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6650
6651 if (direct_mask)
6652 IWL_DEBUG_SCAN
6653 ("Initiating direct scan for %s.\n",
6654 iwl_escape_essid(priv->essid, priv->essid_len));
6655 else
6656 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
6657
6658 scan->channel_count =
6659 iwl_get_channels_for_scan(
6660 priv, phymode, 1, /* active */
6661 direct_mask,
6662 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6663
6664 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
6665 scan->channel_count * sizeof(struct iwl_scan_channel);
6666 cmd.data = scan;
6667 scan->len = cpu_to_le16(cmd.len);
6668
6669 set_bit(STATUS_SCAN_HW, &priv->status);
6670 rc = iwl_send_cmd_sync(priv, &cmd);
6671 if (rc)
6672 goto done;
6673
6674 queue_delayed_work(priv->workqueue, &priv->scan_check,
6675 IWL_SCAN_CHECK_WATCHDOG);
6676
6677 mutex_unlock(&priv->mutex);
6678 return;
6679
6680 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006681 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006682 queue_work(priv->workqueue, &priv->scan_completed);
6683 mutex_unlock(&priv->mutex);
6684}
6685
6686static void iwl_bg_up(struct work_struct *data)
6687{
6688 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
6689
6690 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6691 return;
6692
6693 mutex_lock(&priv->mutex);
6694 __iwl_up(priv);
6695 mutex_unlock(&priv->mutex);
6696}
6697
6698static void iwl_bg_restart(struct work_struct *data)
6699{
6700 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
6701
6702 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6703 return;
6704
6705 iwl_down(priv);
6706 queue_work(priv->workqueue, &priv->up);
6707}
6708
6709static void iwl_bg_rx_replenish(struct work_struct *data)
6710{
6711 struct iwl_priv *priv =
6712 container_of(data, struct iwl_priv, rx_replenish);
6713
6714 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6715 return;
6716
6717 mutex_lock(&priv->mutex);
6718 iwl_rx_replenish(priv);
6719 mutex_unlock(&priv->mutex);
6720}
6721
6722static void iwl_bg_post_associate(struct work_struct *data)
6723{
6724 struct iwl_priv *priv = container_of(data, struct iwl_priv,
6725 post_associate.work);
6726
6727 int rc = 0;
6728 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07006729 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006730
6731 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6732 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6733 return;
6734 }
6735
6736
Joe Perches0795af52007-10-03 17:59:30 -07006737 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6738 priv->assoc_id,
6739 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006740
6741 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6742 return;
6743
6744 mutex_lock(&priv->mutex);
6745
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006746 if (!priv->interface_id || !priv->is_open) {
6747 mutex_unlock(&priv->mutex);
6748 return;
6749 }
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006750 iwl_scan_cancel_timeout(priv, 200);
6751
Zhu Yib481de92007-09-25 17:54:57 -07006752 conf = ieee80211_get_hw_conf(priv->hw);
6753
6754 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6755 iwl_commit_rxon(priv);
6756
6757 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
6758 iwl_setup_rxon_timing(priv);
6759 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
6760 sizeof(priv->rxon_timing), &priv->rxon_timing);
6761 if (rc)
6762 IWL_WARNING("REPLY_RXON_TIMING failed - "
6763 "Attempting to continue.\n");
6764
6765 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6766
6767 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6768
6769 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6770 priv->assoc_id, priv->beacon_int);
6771
6772 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6773 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6774 else
6775 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6776
6777 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6778 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6779 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6780 else
6781 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6782
6783 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6784 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6785
6786 }
6787
6788 iwl_commit_rxon(priv);
6789
6790 switch (priv->iw_mode) {
6791 case IEEE80211_IF_TYPE_STA:
6792 iwl_rate_scale_init(priv->hw, IWL_AP_ID);
6793 break;
6794
6795 case IEEE80211_IF_TYPE_IBSS:
6796
6797 /* clear out the station table */
6798 iwl_clear_stations_table(priv);
6799
Zhu Yi556f8db2007-09-27 11:27:33 +08006800 iwl_add_station(priv, BROADCAST_ADDR, 0, 0);
6801 iwl_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006802 iwl3945_sync_sta(priv, IWL_STA_ID,
6803 (priv->phymode == MODE_IEEE80211A)?
6804 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
6805 CMD_ASYNC);
6806 iwl_rate_scale_init(priv->hw, IWL_STA_ID);
6807 iwl_send_beacon_cmd(priv);
6808
6809 break;
6810
6811 default:
6812 IWL_ERROR("%s Should not be called in %d mode\n",
Ian Schrambc434dd2007-10-25 17:15:29 +08006813 __FUNCTION__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07006814 break;
6815 }
6816
6817 iwl_sequence_reset(priv);
6818
6819#ifdef CONFIG_IWLWIFI_QOS
6820 iwl_activate_qos(priv, 0);
6821#endif /* CONFIG_IWLWIFI_QOS */
6822 mutex_unlock(&priv->mutex);
6823}
6824
6825static void iwl_bg_abort_scan(struct work_struct *work)
6826{
Ian Schrambc434dd2007-10-25 17:15:29 +08006827 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006828
6829 if (!iwl_is_ready(priv))
6830 return;
6831
6832 mutex_lock(&priv->mutex);
6833
6834 set_bit(STATUS_SCAN_ABORTING, &priv->status);
6835 iwl_send_scan_abort(priv);
6836
6837 mutex_unlock(&priv->mutex);
6838}
6839
6840static void iwl_bg_scan_completed(struct work_struct *work)
6841{
6842 struct iwl_priv *priv =
6843 container_of(work, struct iwl_priv, scan_completed);
6844
6845 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6846
6847 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6848 return;
6849
6850 ieee80211_scan_completed(priv->hw);
6851
6852 /* Since setting the TXPOWER may have been deferred while
6853 * performing the scan, fire one off */
6854 mutex_lock(&priv->mutex);
6855 iwl_hw_reg_send_txpower(priv);
6856 mutex_unlock(&priv->mutex);
6857}
6858
6859/*****************************************************************************
6860 *
6861 * mac80211 entry point functions
6862 *
6863 *****************************************************************************/
6864
Johannes Berg4150c572007-09-17 01:29:23 -04006865static int iwl_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006866{
6867 struct iwl_priv *priv = hw->priv;
6868
6869 IWL_DEBUG_MAC80211("enter\n");
6870
6871 /* we should be verifying the device is ready to be opened */
6872 mutex_lock(&priv->mutex);
6873
6874 priv->is_open = 1;
6875
6876 if (!iwl_is_rfkill(priv))
6877 ieee80211_start_queues(priv->hw);
6878
6879 mutex_unlock(&priv->mutex);
6880 IWL_DEBUG_MAC80211("leave\n");
6881 return 0;
6882}
6883
Johannes Berg4150c572007-09-17 01:29:23 -04006884static void iwl_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006885{
6886 struct iwl_priv *priv = hw->priv;
6887
6888 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006889
6890
6891 mutex_lock(&priv->mutex);
6892 /* stop mac, cancel any scan request and clear
6893 * RXON_FILTER_ASSOC_MSK BIT
6894 */
Zhu Yib481de92007-09-25 17:54:57 -07006895 priv->is_open = 0;
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006896 iwl_scan_cancel_timeout(priv, 100);
6897 cancel_delayed_work(&priv->post_associate);
6898 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6899 iwl_commit_rxon(priv);
6900 mutex_unlock(&priv->mutex);
6901
Zhu Yib481de92007-09-25 17:54:57 -07006902 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006903}
6904
6905static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
6906 struct ieee80211_tx_control *ctl)
6907{
6908 struct iwl_priv *priv = hw->priv;
6909
6910 IWL_DEBUG_MAC80211("enter\n");
6911
6912 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6913 IWL_DEBUG_MAC80211("leave - monitor\n");
6914 return -1;
6915 }
6916
6917 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
6918 ctl->tx_rate);
6919
6920 if (iwl_tx_skb(priv, skb, ctl))
6921 dev_kfree_skb_any(skb);
6922
6923 IWL_DEBUG_MAC80211("leave\n");
6924 return 0;
6925}
6926
6927static int iwl_mac_add_interface(struct ieee80211_hw *hw,
6928 struct ieee80211_if_init_conf *conf)
6929{
6930 struct iwl_priv *priv = hw->priv;
6931 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006932 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006933
6934 IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006935
6936 if (priv->interface_id) {
6937 IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006938 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006939 }
6940
6941 spin_lock_irqsave(&priv->lock, flags);
6942 priv->interface_id = conf->if_id;
6943
6944 spin_unlock_irqrestore(&priv->lock, flags);
6945
6946 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006947
6948 if (conf->mac_addr) {
6949 IWL_DEBUG_MAC80211("Set: %s\n", print_mac(mac, conf->mac_addr));
6950 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6951 }
6952
Zhu Yib481de92007-09-25 17:54:57 -07006953 iwl_set_mode(priv, conf->type);
6954
6955 IWL_DEBUG_MAC80211("leave\n");
6956 mutex_unlock(&priv->mutex);
6957
6958 return 0;
6959}
6960
6961/**
6962 * iwl_mac_config - mac80211 config callback
6963 *
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.
6967 */
6968static int iwl_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
6969{
6970 struct iwl_priv *priv = hw->priv;
6971 const struct iwl_channel_info *ch_info;
6972 unsigned long flags;
6973
6974 mutex_lock(&priv->mutex);
6975 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
6976
6977 if (!iwl_is_ready(priv)) {
6978 IWL_DEBUG_MAC80211("leave - not ready\n");
6979 mutex_unlock(&priv->mutex);
6980 return -EIO;
6981 }
6982
6983 /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
Ian Schram01ebd062007-10-25 17:15:22 +08006984 * what is exposed through include/ declarations */
Zhu Yib481de92007-09-25 17:54:57 -07006985 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;
6990 }
6991
6992 spin_lock_irqsave(&priv->lock, flags);
6993
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;
7002 }
7003
7004 iwl_set_rxon_channel(priv, conf->phymode, conf->channel);
7005
7006 iwl_set_flags_for_phymode(priv, conf->phymode);
7007
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);
7012
7013 spin_unlock_irqrestore(&priv->lock, flags);
7014
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;
7020 }
7021#endif
7022
7023 iwl_radio_kill_sw(priv, !conf->radio_enabled);
7024
7025 if (!conf->radio_enabled) {
7026 IWL_DEBUG_MAC80211("leave - radio disabled\n");
7027 mutex_unlock(&priv->mutex);
7028 return 0;
7029 }
7030
7031 if (iwl_is_rfkill(priv)) {
7032 IWL_DEBUG_MAC80211("leave - RF kill\n");
7033 mutex_unlock(&priv->mutex);
7034 return -EIO;
7035 }
7036
7037 iwl_set_rate(priv);
7038
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");
7044
7045 IWL_DEBUG_MAC80211("leave\n");
7046
7047 mutex_unlock(&priv->mutex);
7048
7049 return 0;
7050}
7051
7052static void iwl_config_ap(struct iwl_priv *priv)
7053{
7054 int rc = 0;
7055
7056 if (priv->status & STATUS_EXIT_PENDING)
7057 return;
7058
7059 /* The following should be done only at AP bring up */
7060 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7061
7062 /* RXON - unassoc (to set timing command) */
7063 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7064 iwl_commit_rxon(priv);
7065
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");
7074
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;
7083
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;
7092
7093 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7094 priv->staging_rxon.flags &=
7095 ~RXON_FLG_SHORT_SLOT_MSK;
7096 }
7097 /* restore RXON assoc */
7098 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7099 iwl_commit_rxon(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08007100 iwl_add_station(priv, BROADCAST_ADDR, 0, 0);
7101 }
7102 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007103
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... */
7107}
7108
7109static int iwl_mac_config_interface(struct ieee80211_hw *hw, int if_id,
7110 struct ieee80211_if_conf *conf)
7111{
7112 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007113 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007114 unsigned long flags;
7115 int rc;
7116
7117 if (conf == NULL)
7118 return -EIO;
7119
Johannes Berg4150c572007-09-17 01:29:23 -04007120 /* XXX: this MUST use conf->mac_addr */
7121
Zhu Yib481de92007-09-25 17:54:57 -07007122 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;
7127 }
7128
7129 mutex_lock(&priv->mutex);
7130
7131 IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
7132 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007133 IWL_DEBUG_MAC80211("bssid: %s\n",
7134 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007135
Johannes Berg4150c572007-09-17 01:29:23 -04007136/*
7137 * very dubious code was here; the probe filtering flag is never set:
7138 *
Zhu Yib481de92007-09-25 17:54:57 -07007139 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7140 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007141 */
7142 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yib481de92007-09-25 17:54:57 -07007143 IWL_DEBUG_MAC80211("leave - scanning\n");
7144 mutex_unlock(&priv->mutex);
7145 return 0;
7146 }
7147
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;
7152 }
7153
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);
Joe Perches0795af52007-10-03 17:59:30 -07007158 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7159 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007160 }
7161 if (priv->ibss_beacon)
7162 dev_kfree_skb(priv->ibss_beacon);
7163
7164 priv->ibss_beacon = conf->beacon;
7165 }
7166
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;
7177 }
7178 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7179
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);
7185
7186 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7187 iwl_config_ap(priv);
7188 else {
Zhu Yib481de92007-09-25 17:54:57 -07007189 rc = iwl_commit_rxon(priv);
7190 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Zhu Yi556f8db2007-09-27 11:27:33 +08007191 iwl_add_station(priv,
7192 priv->active_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007193 }
7194
7195 } else {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007196 iwl_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007197 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7198 iwl_commit_rxon(priv);
7199 }
7200
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);
7206
7207 priv->essid_len = conf->ssid_len;
7208 spin_unlock_irqrestore(&priv->lock, flags);
7209
7210 IWL_DEBUG_MAC80211("leave\n");
7211 mutex_unlock(&priv->mutex);
7212
7213 return 0;
7214}
7215
Johannes Berg4150c572007-09-17 01:29:23 -04007216static 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)
7220{
7221 /*
7222 * XXX: dummy
7223 * see also iwl_connection_init_rx_config
7224 */
7225 *total_flags = 0;
7226}
7227
Zhu Yib481de92007-09-25 17:54:57 -07007228static void iwl_mac_remove_interface(struct ieee80211_hw *hw,
7229 struct ieee80211_if_init_conf *conf)
7230{
7231 struct iwl_priv *priv = hw->priv;
7232
7233 IWL_DEBUG_MAC80211("enter\n");
7234
7235 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007236
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);
7241
Zhu Yib481de92007-09-25 17:54:57 -07007242 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;
7247 }
7248 mutex_unlock(&priv->mutex);
7249
7250 IWL_DEBUG_MAC80211("leave\n");
7251
7252}
7253
7254#define IWL_DELAY_NEXT_SCAN (HZ*2)
7255static int iwl_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
7256{
7257 int rc = 0;
7258 unsigned long flags;
7259 struct iwl_priv *priv = hw->priv;
7260
7261 IWL_DEBUG_MAC80211("enter\n");
7262
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007263 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007264 spin_lock_irqsave(&priv->lock, flags);
7265
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;
7270 }
7271
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;
7276 }
7277
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;
7284 }
7285 if (len) {
7286 IWL_DEBUG_SCAN("direct scan for "
7287 "%s [%d]\n ",
7288 iwl_escape_essid(ssid, len), (int)len);
7289
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);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007294 } else
7295 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007296
7297 rc = iwl_scan_initiate(priv);
7298
7299 IWL_DEBUG_MAC80211("leave\n");
7300
7301out_unlock:
7302 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007303 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007304
7305 return rc;
7306}
7307
Johannes Bergea49c352007-09-18 17:29:21 -04007308static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007309 const u8 *local_addr, const u8 *addr,
7310 struct ieee80211_key_conf *key)
7311{
7312 struct iwl_priv *priv = hw->priv;
7313 int rc = 0;
7314 u8 sta_id;
7315
7316 IWL_DEBUG_MAC80211("enter\n");
7317
7318 if (!iwl_param_hwcrypto) {
7319 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7320 return -EOPNOTSUPP;
7321 }
7322
7323 if (is_zero_ether_addr(addr))
7324 /* only support pairwise keys */
7325 return -EOPNOTSUPP;
7326
7327 sta_id = iwl_hw_find_station(priv, addr);
7328 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007329 DECLARE_MAC_BUF(mac);
7330
7331 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7332 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007333 return -EINVAL;
7334 }
7335
7336 mutex_lock(&priv->mutex);
7337
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007338 iwl_scan_cancel_timeout(priv, 100);
7339
Zhu Yib481de92007-09-25 17:54:57 -07007340 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;
7349 }
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");
7357 }
7358 break;
7359 default:
7360 rc = -EINVAL;
7361 }
7362
7363 IWL_DEBUG_MAC80211("leave\n");
7364 mutex_unlock(&priv->mutex);
7365
7366 return rc;
7367}
7368
7369static int iwl_mac_conf_tx(struct ieee80211_hw *hw, int queue,
7370 const struct ieee80211_tx_queue_params *params)
7371{
7372 struct iwl_priv *priv = hw->priv;
7373#ifdef CONFIG_IWLWIFI_QOS
7374 unsigned long flags;
7375 int q;
7376#endif /* CONFIG_IWL_QOS */
7377
7378 IWL_DEBUG_MAC80211("enter\n");
7379
7380 if (!iwl_is_ready_rf(priv)) {
7381 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7382 return -EIO;
7383 }
7384
7385 if (queue >= AC_NUM) {
7386 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7387 return 0;
7388 }
7389
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;
7395 }
7396 q = AC_NUM - 1 - queue;
7397
7398 spin_lock_irqsave(&priv->lock, flags);
7399
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));
7405
7406 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7407 priv->qos_data.qos_active = 1;
7408
7409 spin_unlock_irqrestore(&priv->lock, flags);
7410
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);
7416
7417 mutex_unlock(&priv->mutex);
7418
7419#endif /*CONFIG_IWLWIFI_QOS */
7420
7421 IWL_DEBUG_MAC80211("leave\n");
7422 return 0;
7423}
7424
7425static int iwl_mac_get_tx_stats(struct ieee80211_hw *hw,
7426 struct ieee80211_tx_queue_stats *stats)
7427{
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;
7433
7434 IWL_DEBUG_MAC80211("enter\n");
7435
7436 if (!iwl_is_ready_rf(priv)) {
7437 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7438 return -EIO;
7439 }
7440
7441 spin_lock_irqsave(&priv->lock, flags);
7442
7443 for (i = 0; i < AC_NUM; i++) {
7444 txq = &priv->txq[i];
7445 q = &txq->q;
7446 avail = iwl_queue_space(q);
7447
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;
7451
7452 }
7453 spin_unlock_irqrestore(&priv->lock, flags);
7454
7455 IWL_DEBUG_MAC80211("leave\n");
7456
7457 return 0;
7458}
7459
7460static int iwl_mac_get_stats(struct ieee80211_hw *hw,
7461 struct ieee80211_low_level_stats *stats)
7462{
7463 IWL_DEBUG_MAC80211("enter\n");
7464 IWL_DEBUG_MAC80211("leave\n");
7465
7466 return 0;
7467}
7468
7469static u64 iwl_mac_get_tsf(struct ieee80211_hw *hw)
7470{
7471 IWL_DEBUG_MAC80211("enter\n");
7472 IWL_DEBUG_MAC80211("leave\n");
7473
7474 return 0;
7475}
7476
7477static void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
7478{
7479 struct iwl_priv *priv = hw->priv;
7480 unsigned long flags;
7481
7482 mutex_lock(&priv->mutex);
7483 IWL_DEBUG_MAC80211("enter\n");
7484
7485#ifdef CONFIG_IWLWIFI_QOS
7486 iwl_reset_qos(priv);
7487#endif
7488 cancel_delayed_work(&priv->post_associate);
7489
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;
7494
7495 /* new association get rid of ibss beacon skb */
7496 if (priv->ibss_beacon)
7497 dev_kfree_skb(priv->ibss_beacon);
7498
7499 priv->ibss_beacon = NULL;
7500
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;
7506
7507 spin_unlock_irqrestore(&priv->lock, flags);
7508
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007509 /* we are restarting association process
7510 * clear RXON_FILTER_ASSOC_MSK bit
7511 */
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);
7516 }
7517
Zhu Yib481de92007-09-25 17:54:57 -07007518 /* Per mac80211.h: This is only used in IBSS mode... */
7519 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007520
Zhu Yib481de92007-09-25 17:54:57 -07007521 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7522 mutex_unlock(&priv->mutex);
7523 return;
7524 }
7525
7526 if (!iwl_is_ready_rf(priv)) {
7527 IWL_DEBUG_MAC80211("leave - not ready\n");
7528 mutex_unlock(&priv->mutex);
7529 return;
7530 }
7531
7532 priv->only_active_channel = 0;
7533
7534 iwl_set_rate(priv);
7535
7536 mutex_unlock(&priv->mutex);
7537
7538 IWL_DEBUG_MAC80211("leave\n");
7539
7540}
7541
7542static int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
7543 struct ieee80211_tx_control *control)
7544{
7545 struct iwl_priv *priv = hw->priv;
7546 unsigned long flags;
7547
7548 mutex_lock(&priv->mutex);
7549 IWL_DEBUG_MAC80211("enter\n");
7550
7551 if (!iwl_is_ready_rf(priv)) {
7552 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7553 mutex_unlock(&priv->mutex);
7554 return -EIO;
7555 }
7556
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;
7561 }
7562
7563 spin_lock_irqsave(&priv->lock, flags);
7564
7565 if (priv->ibss_beacon)
7566 dev_kfree_skb(priv->ibss_beacon);
7567
7568 priv->ibss_beacon = skb;
7569
7570 priv->assoc_id = 0;
7571
7572 IWL_DEBUG_MAC80211("leave\n");
7573 spin_unlock_irqrestore(&priv->lock, flags);
7574
7575#ifdef CONFIG_IWLWIFI_QOS
7576 iwl_reset_qos(priv);
7577#endif
7578
7579 queue_work(priv->workqueue, &priv->post_associate.work);
7580
7581 mutex_unlock(&priv->mutex);
7582
7583 return 0;
7584}
7585
7586/*****************************************************************************
7587 *
7588 * sysfs attributes
7589 *
7590 *****************************************************************************/
7591
7592#ifdef CONFIG_IWLWIFI_DEBUG
7593
7594/*
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.
7598 *
7599 * See the level definitions in iwl for details.
7600 */
7601
7602static ssize_t show_debug_level(struct device_driver *d, char *buf)
7603{
7604 return sprintf(buf, "0x%08X\n", iwl_debug_level);
7605}
7606static ssize_t store_debug_level(struct device_driver *d,
7607 const char *buf, size_t count)
7608{
7609 char *p = (char *)buf;
7610 u32 val;
7611
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;
7618
7619 return strnlen(buf, count);
7620}
7621
7622static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7623 show_debug_level, store_debug_level);
7624
7625#endif /* CONFIG_IWLWIFI_DEBUG */
7626
7627static ssize_t show_rf_kill(struct device *d,
7628 struct device_attribute *attr, char *buf)
7629{
7630 /*
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
7635 */
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);
7639
7640 return sprintf(buf, "%i\n", val);
7641}
7642
7643static ssize_t store_rf_kill(struct device *d,
7644 struct device_attribute *attr,
7645 const char *buf, size_t count)
7646{
7647 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7648
7649 mutex_lock(&priv->mutex);
7650 iwl_radio_kill_sw(priv, buf[0] == '1');
7651 mutex_unlock(&priv->mutex);
7652
7653 return count;
7654}
7655
7656static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7657
7658static ssize_t show_temperature(struct device *d,
7659 struct device_attribute *attr, char *buf)
7660{
7661 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7662
7663 if (!iwl_is_alive(priv))
7664 return -EAGAIN;
7665
7666 return sprintf(buf, "%d\n", iwl_hw_get_temperature(priv));
7667}
7668
7669static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7670
7671static ssize_t show_rs_window(struct device *d,
7672 struct device_attribute *attr,
7673 char *buf)
7674{
7675 struct iwl_priv *priv = d->driver_data;
7676 return iwl_fill_rs_info(priv->hw, buf, IWL_AP_ID);
7677}
7678static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7679
7680static ssize_t show_tx_power(struct device *d,
7681 struct device_attribute *attr, char *buf)
7682{
7683 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7684 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7685}
7686
7687static ssize_t store_tx_power(struct device *d,
7688 struct device_attribute *attr,
7689 const char *buf, size_t count)
7690{
7691 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7692 char *p = (char *)buf;
7693 u32 val;
7694
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);
7701
7702 return count;
7703}
7704
7705static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7706
7707static ssize_t show_flags(struct device *d,
7708 struct device_attribute *attr, char *buf)
7709{
7710 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7711
7712 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7713}
7714
7715static ssize_t store_flags(struct device *d,
7716 struct device_attribute *attr,
7717 const char *buf, size_t count)
7718{
7719 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7720 u32 flags = simple_strtoul(buf, NULL, 0);
7721
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);
7732 }
7733 }
7734 mutex_unlock(&priv->mutex);
7735
7736 return count;
7737}
7738
7739static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7740
7741static ssize_t show_filter_flags(struct device *d,
7742 struct device_attribute *attr, char *buf)
7743{
7744 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7745
7746 return sprintf(buf, "0x%04X\n",
7747 le32_to_cpu(priv->active_rxon.filter_flags));
7748}
7749
7750static ssize_t store_filter_flags(struct device *d,
7751 struct device_attribute *attr,
7752 const char *buf, size_t count)
7753{
7754 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7755 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7756
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);
7768 }
7769 }
7770 mutex_unlock(&priv->mutex);
7771
7772 return count;
7773}
7774
7775static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7776 store_filter_flags);
7777
7778static ssize_t show_tune(struct device *d,
7779 struct device_attribute *attr, char *buf)
7780{
7781 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7782
7783 return sprintf(buf, "0x%04X\n",
7784 (priv->phymode << 8) |
7785 le16_to_cpu(priv->active_rxon.channel));
7786}
7787
7788static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode);
7789
7790static ssize_t store_tune(struct device *d,
7791 struct device_attribute *attr,
7792 const char *buf, size_t count)
7793{
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;
7799
7800 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
7801
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;
7806
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;
7813 }
7814
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);
7822
7823 iwl_set_rxon_channel(priv, phymode, channel);
7824 iwl_set_flags_for_phymode(priv, phymode);
7825
7826 iwl_set_rate(priv);
7827 iwl_commit_rxon(priv);
7828 }
7829 }
7830 mutex_unlock(&priv->mutex);
7831
7832 return count;
7833}
7834
7835static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
7836
7837#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
7838
7839static ssize_t show_measurement(struct device *d,
7840 struct device_attribute *attr, char *buf)
7841{
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;
7847
7848 spin_lock_irqsave(&priv->lock, flags);
7849 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7850 spin_unlock_irqrestore(&priv->lock, flags);
7851 return 0;
7852 }
7853 memcpy(&measure_report, &priv->measure_report, size);
7854 priv->measurement_status = 0;
7855 spin_unlock_irqrestore(&priv->lock, flags);
7856
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';
7863
7864 ofs += 16;
7865 size -= min(size, 16U);
7866 }
7867
7868 return len;
7869}
7870
7871static ssize_t store_measurement(struct device *d,
7872 struct device_attribute *attr,
7873 const char *buf, size_t count)
7874{
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),
7880 };
7881 u8 type = IWL_MEASURE_BASIC;
7882 u8 buffer[32];
7883 u8 channel;
7884
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;
7891
7892 p = buffer;
7893 while (*p && *p != ' ')
7894 p++;
7895 if (*p)
7896 type = simple_strtoul(p + 1, NULL, 0);
7897 }
7898
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);
7902
7903 return count;
7904}
7905
7906static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7907 show_measurement, store_measurement);
7908#endif /* CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT */
7909
7910static ssize_t show_rate(struct device *d,
7911 struct device_attribute *attr, char *buf)
7912{
7913 struct iwl_priv *priv = dev_get_drvdata(d);
7914 unsigned long flags;
7915 int i;
7916
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);
7923
7924 i = iwl_rate_index_from_plcp(i);
7925 if (i == -1)
7926 return sprintf(buf, "0\n");
7927
7928 return sprintf(buf, "%d%s\n",
7929 (iwl_rates[i].ieee >> 1),
7930 (iwl_rates[i].ieee & 0x1) ? ".5" : "");
7931}
7932
7933static DEVICE_ATTR(rate, S_IRUSR, show_rate, NULL);
7934
7935static ssize_t store_retry_rate(struct device *d,
7936 struct device_attribute *attr,
7937 const char *buf, size_t count)
7938{
7939 struct iwl_priv *priv = dev_get_drvdata(d);
7940
7941 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7942 if (priv->retry_rate <= 0)
7943 priv->retry_rate = 1;
7944
7945 return count;
7946}
7947
7948static ssize_t show_retry_rate(struct device *d,
7949 struct device_attribute *attr, char *buf)
7950{
7951 struct iwl_priv *priv = dev_get_drvdata(d);
7952 return sprintf(buf, "%d", priv->retry_rate);
7953}
7954
7955static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7956 store_retry_rate);
7957
7958static ssize_t store_power_level(struct device *d,
7959 struct device_attribute *attr,
7960 const char *buf, size_t count)
7961{
7962 struct iwl_priv *priv = dev_get_drvdata(d);
7963 int rc;
7964 int mode;
7965
7966 mode = simple_strtoul(buf, NULL, 0);
7967 mutex_lock(&priv->mutex);
7968
7969 if (!iwl_is_ready(priv)) {
7970 rc = -EAGAIN;
7971 goto out;
7972 }
7973
7974 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7975 mode = IWL_POWER_AC;
7976 else
7977 mode |= IWL_POWER_ENABLED;
7978
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;
7984 }
7985 priv->power_mode = mode;
7986 }
7987
7988 rc = count;
7989
7990 out:
7991 mutex_unlock(&priv->mutex);
7992 return rc;
7993}
7994
7995#define MAX_WX_STRING 80
7996
7997/* Values are in microsecond */
7998static const s32 timeout_duration[] = {
7999 350000,
8000 250000,
8001 75000,
8002 37000,
8003 25000,
8004};
8005static const s32 period_duration[] = {
8006 400000,
8007 700000,
8008 1000000,
8009 1000000,
8010 1000000
8011};
8012
8013static ssize_t show_power_level(struct device *d,
8014 struct device_attribute *attr, char *buf)
8015{
8016 struct iwl_priv *priv = dev_get_drvdata(d);
8017 int level = IWL_POWER_LEVEL(priv->power_mode);
8018 char *p = buf;
8019
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);
8034 }
8035
8036 if (!(priv->power_mode & IWL_POWER_ENABLED))
8037 p += sprintf(p, " OFF\n");
8038 else
8039 p += sprintf(p, " \n");
8040
8041 return (p - buf + 1);
8042
8043}
8044
8045static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8046 store_power_level);
8047
8048static ssize_t show_channels(struct device *d,
8049 struct device_attribute *attr, char *buf)
8050{
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;
8056
8057 if (!iwl_is_ready(priv))
8058 return -EAGAIN;
8059
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;
8066 }
8067
8068 len +=
8069 sprintf(&buf[len],
8070 "Displaying %d channels in 2.4GHz band "
8071 "(802.11bg):\n", count);
8072
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");
8088
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;
8096 }
8097
8098 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8099 "(802.11a):\n", count);
8100
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");
8116
8117 return len;
8118}
8119
8120static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8121
8122static ssize_t show_statistics(struct device *d,
8123 struct device_attribute *attr, char *buf)
8124{
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;
8130
8131 if (!iwl_is_alive(priv))
8132 return -EAGAIN;
8133
8134 mutex_lock(&priv->mutex);
8135 rc = iwl_send_statistics_request(priv);
8136 mutex_unlock(&priv->mutex);
8137
8138 if (rc) {
8139 len = sprintf(buf,
8140 "Error sending statistics request: 0x%08X\n", rc);
8141 return len;
8142 }
8143
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';
8150
8151 ofs += 16;
8152 size -= min(size, 16U);
8153 }
8154
8155 return len;
8156}
8157
8158static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8159
8160static ssize_t show_antenna(struct device *d,
8161 struct device_attribute *attr, char *buf)
8162{
8163 struct iwl_priv *priv = dev_get_drvdata(d);
8164
8165 if (!iwl_is_alive(priv))
8166 return -EAGAIN;
8167
8168 return sprintf(buf, "%d\n", priv->antenna);
8169}
8170
8171static ssize_t store_antenna(struct device *d,
8172 struct device_attribute *attr,
8173 const char *buf, size_t count)
8174{
8175 int ant;
8176 struct iwl_priv *priv = dev_get_drvdata(d);
8177
8178 if (count == 0)
8179 return 0;
8180
8181 if (sscanf(buf, "%1i", &ant) != 1) {
8182 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8183 return count;
8184 }
8185
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);
8191
8192
8193 return count;
8194}
8195
8196static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8197
8198static ssize_t show_status(struct device *d,
8199 struct device_attribute *attr, char *buf)
8200{
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);
8205}
8206
8207static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8208
8209static ssize_t dump_error_log(struct device *d,
8210 struct device_attribute *attr,
8211 const char *buf, size_t count)
8212{
8213 char *p = (char *)buf;
8214
8215 if (p[0] == '1')
8216 iwl_dump_nic_error_log((struct iwl_priv *)d->driver_data);
8217
8218 return strnlen(buf, count);
8219}
8220
8221static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8222
8223static ssize_t dump_event_log(struct device *d,
8224 struct device_attribute *attr,
8225 const char *buf, size_t count)
8226{
8227 char *p = (char *)buf;
8228
8229 if (p[0] == '1')
8230 iwl_dump_nic_event_log((struct iwl_priv *)d->driver_data);
8231
8232 return strnlen(buf, count);
8233}
8234
8235static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8236
8237/*****************************************************************************
8238 *
8239 * driver setup and teardown
8240 *
8241 *****************************************************************************/
8242
8243static void iwl_setup_deferred_work(struct iwl_priv *priv)
8244{
8245 priv->workqueue = create_workqueue(DRV_NAME);
8246
8247 init_waitqueue_head(&priv->wait_command_queue);
8248
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);
8261
8262 iwl_hw_setup_deferred_work(priv);
8263
8264 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
8265 iwl_irq_tasklet, (unsigned long)priv);
8266}
8267
8268static void iwl_cancel_deferred_work(struct iwl_priv *priv)
8269{
8270 iwl_hw_cancel_deferred_work(priv);
8271
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09008272 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008273 cancel_delayed_work(&priv->scan_check);
8274 cancel_delayed_work(&priv->alive_start);
8275 cancel_delayed_work(&priv->post_associate);
8276 cancel_work_sync(&priv->beacon_update);
8277}
8278
8279static struct attribute *iwl_sysfs_entries[] = {
8280 &dev_attr_antenna.attr,
8281 &dev_attr_channels.attr,
8282 &dev_attr_dump_errors.attr,
8283 &dev_attr_dump_events.attr,
8284 &dev_attr_flags.attr,
8285 &dev_attr_filter_flags.attr,
8286#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8287 &dev_attr_measurement.attr,
8288#endif
8289 &dev_attr_power_level.attr,
8290 &dev_attr_rate.attr,
8291 &dev_attr_retry_rate.attr,
8292 &dev_attr_rf_kill.attr,
8293 &dev_attr_rs_window.attr,
8294 &dev_attr_statistics.attr,
8295 &dev_attr_status.attr,
8296 &dev_attr_temperature.attr,
8297 &dev_attr_tune.attr,
8298 &dev_attr_tx_power.attr,
8299
8300 NULL
8301};
8302
8303static struct attribute_group iwl_attribute_group = {
8304 .name = NULL, /* put in device directory */
8305 .attrs = iwl_sysfs_entries,
8306};
8307
8308static struct ieee80211_ops iwl_hw_ops = {
8309 .tx = iwl_mac_tx,
Johannes Berg4150c572007-09-17 01:29:23 -04008310 .start = iwl_mac_start,
Zhu Yib481de92007-09-25 17:54:57 -07008311 .stop = iwl_mac_stop,
8312 .add_interface = iwl_mac_add_interface,
8313 .remove_interface = iwl_mac_remove_interface,
8314 .config = iwl_mac_config,
8315 .config_interface = iwl_mac_config_interface,
Johannes Berg4150c572007-09-17 01:29:23 -04008316 .configure_filter = iwl_configure_filter,
Zhu Yib481de92007-09-25 17:54:57 -07008317 .set_key = iwl_mac_set_key,
8318 .get_stats = iwl_mac_get_stats,
8319 .get_tx_stats = iwl_mac_get_tx_stats,
8320 .conf_tx = iwl_mac_conf_tx,
8321 .get_tsf = iwl_mac_get_tsf,
8322 .reset_tsf = iwl_mac_reset_tsf,
8323 .beacon_update = iwl_mac_beacon_update,
8324 .hw_scan = iwl_mac_hw_scan
8325};
8326
8327static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8328{
8329 int err = 0;
8330 u32 pci_id;
8331 struct iwl_priv *priv;
8332 struct ieee80211_hw *hw;
8333 int i;
8334
8335 if (iwl_param_disable_hw_scan) {
8336 IWL_DEBUG_INFO("Disabling hw_scan\n");
8337 iwl_hw_ops.hw_scan = NULL;
8338 }
8339
8340 if ((iwl_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8341 (iwl_param_queues_num < IWL_MIN_NUM_QUEUES)) {
8342 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8343 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8344 err = -EINVAL;
8345 goto out;
8346 }
8347
8348 /* mac80211 allocates memory for this device instance, including
8349 * space for this driver's private structure */
8350 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl_hw_ops);
8351 if (hw == NULL) {
8352 IWL_ERROR("Can not allocate network device\n");
8353 err = -ENOMEM;
8354 goto out;
8355 }
8356 SET_IEEE80211_DEV(hw, &pdev->dev);
8357
Johannes Bergf51359a2007-10-28 14:53:36 +01008358 hw->rate_control_algorithm = "iwl-3945-rs";
8359
Zhu Yib481de92007-09-25 17:54:57 -07008360 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8361 priv = hw->priv;
8362 priv->hw = hw;
8363
8364 priv->pci_dev = pdev;
8365 priv->antenna = (enum iwl_antenna)iwl_param_antenna;
8366#ifdef CONFIG_IWLWIFI_DEBUG
8367 iwl_debug_level = iwl_param_debug;
8368 atomic_set(&priv->restrict_refcnt, 0);
8369#endif
8370 priv->retry_rate = 1;
8371
8372 priv->ibss_beacon = NULL;
8373
8374 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8375 * the range of signal quality values that we'll provide.
8376 * Negative values for level/noise indicate that we'll provide dBm.
8377 * For WE, at least, non-0 values here *enable* display of values
8378 * in app (iwconfig). */
8379 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8380 hw->max_noise = -20; /* noise level, negative indicates dBm */
8381 hw->max_signal = 100; /* link quality indication (%) */
8382
8383 /* Tell mac80211 our Tx characteristics */
8384 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8385
8386 hw->queues = 4;
8387
8388 spin_lock_init(&priv->lock);
8389 spin_lock_init(&priv->power_data.lock);
8390 spin_lock_init(&priv->sta_lock);
8391 spin_lock_init(&priv->hcmd_lock);
8392
8393 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8394 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8395
8396 INIT_LIST_HEAD(&priv->free_frames);
8397
8398 mutex_init(&priv->mutex);
8399 if (pci_enable_device(pdev)) {
8400 err = -ENODEV;
8401 goto out_ieee80211_free_hw;
8402 }
8403
8404 pci_set_master(pdev);
8405
8406 iwl_clear_stations_table(priv);
8407
8408 priv->data_retry_limit = -1;
8409 priv->ieee_channels = NULL;
8410 priv->ieee_rates = NULL;
8411 priv->phymode = -1;
8412
8413 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8414 if (!err)
8415 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8416 if (err) {
8417 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
8418 goto out_pci_disable_device;
8419 }
8420
8421 pci_set_drvdata(pdev, priv);
8422 err = pci_request_regions(pdev, DRV_NAME);
8423 if (err)
8424 goto out_pci_disable_device;
8425 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8426 * PCI Tx retries from interfering with C3 CPU state */
8427 pci_write_config_byte(pdev, 0x41, 0x00);
8428 priv->hw_base = pci_iomap(pdev, 0, 0);
8429 if (!priv->hw_base) {
8430 err = -ENODEV;
8431 goto out_pci_release_regions;
8432 }
8433
8434 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8435 (unsigned long long) pci_resource_len(pdev, 0));
8436 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8437
8438 /* Initialize module parameter values here */
8439
8440 if (iwl_param_disable) {
8441 set_bit(STATUS_RF_KILL_SW, &priv->status);
8442 IWL_DEBUG_INFO("Radio disabled.\n");
8443 }
8444
8445 priv->iw_mode = IEEE80211_IF_TYPE_STA;
8446
8447 pci_id =
8448 (priv->pci_dev->device << 16) | priv->pci_dev->subsystem_device;
8449
8450 switch (pci_id) {
8451 case 0x42221005: /* 0x4222 0x8086 0x1005 is BG SKU */
8452 case 0x42221034: /* 0x4222 0x8086 0x1034 is BG SKU */
8453 case 0x42271014: /* 0x4227 0x8086 0x1014 is BG SKU */
8454 case 0x42221044: /* 0x4222 0x8086 0x1044 is BG SKU */
8455 priv->is_abg = 0;
8456 break;
8457
8458 /*
8459 * Rest are assumed ABG SKU -- if this is not the
8460 * case then the card will get the wrong 'Detected'
8461 * line in the kernel log however the code that
8462 * initializes the GEO table will detect no A-band
8463 * channels and remove the is_abg mask.
8464 */
8465 default:
8466 priv->is_abg = 1;
8467 break;
8468 }
8469
8470 printk(KERN_INFO DRV_NAME
8471 ": Detected Intel PRO/Wireless 3945%sBG Network Connection\n",
8472 priv->is_abg ? "A" : "");
8473
8474 /* Device-specific setup */
8475 if (iwl_hw_set_hw_setting(priv)) {
8476 IWL_ERROR("failed to set hw settings\n");
8477 mutex_unlock(&priv->mutex);
8478 goto out_iounmap;
8479 }
8480
8481#ifdef CONFIG_IWLWIFI_QOS
8482 if (iwl_param_qos_enable)
8483 priv->qos_data.qos_enable = 1;
8484
8485 iwl_reset_qos(priv);
8486
8487 priv->qos_data.qos_active = 0;
8488 priv->qos_data.qos_cap.val = 0;
8489#endif /* CONFIG_IWLWIFI_QOS */
8490
8491 iwl_set_rxon_channel(priv, MODE_IEEE80211G, 6);
8492 iwl_setup_deferred_work(priv);
8493 iwl_setup_rx_handlers(priv);
8494
8495 priv->rates_mask = IWL_RATES_MASK;
8496 /* If power management is turned on, default to AC mode */
8497 priv->power_mode = IWL_POWER_AC;
8498 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
8499
8500 pci_enable_msi(pdev);
8501
8502 err = request_irq(pdev->irq, iwl_isr, IRQF_SHARED, DRV_NAME, priv);
8503 if (err) {
8504 IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
8505 goto out_disable_msi;
8506 }
8507
8508 mutex_lock(&priv->mutex);
8509
8510 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
8511 if (err) {
8512 IWL_ERROR("failed to create sysfs device attributes\n");
8513 mutex_unlock(&priv->mutex);
8514 goto out_release_irq;
8515 }
8516
8517 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
8518 * ucode filename and max sizes are card-specific. */
8519 err = iwl_read_ucode(priv);
8520 if (err) {
8521 IWL_ERROR("Could not read microcode: %d\n", err);
8522 mutex_unlock(&priv->mutex);
8523 goto out_pci_alloc;
8524 }
8525
8526 mutex_unlock(&priv->mutex);
8527
Ian Schram01ebd062007-10-25 17:15:22 +08008528 IWL_DEBUG_INFO("Queueing UP work.\n");
Zhu Yib481de92007-09-25 17:54:57 -07008529
8530 queue_work(priv->workqueue, &priv->up);
8531
8532 return 0;
8533
8534 out_pci_alloc:
8535 iwl_dealloc_ucode_pci(priv);
8536
8537 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
8538
8539 out_release_irq:
8540 free_irq(pdev->irq, priv);
8541
8542 out_disable_msi:
8543 pci_disable_msi(pdev);
8544 destroy_workqueue(priv->workqueue);
8545 priv->workqueue = NULL;
8546 iwl_unset_hw_setting(priv);
8547
8548 out_iounmap:
8549 pci_iounmap(pdev, priv->hw_base);
8550 out_pci_release_regions:
8551 pci_release_regions(pdev);
8552 out_pci_disable_device:
8553 pci_disable_device(pdev);
8554 pci_set_drvdata(pdev, NULL);
8555 out_ieee80211_free_hw:
8556 ieee80211_free_hw(priv->hw);
8557 out:
8558 return err;
8559}
8560
8561static void iwl_pci_remove(struct pci_dev *pdev)
8562{
8563 struct iwl_priv *priv = pci_get_drvdata(pdev);
8564 struct list_head *p, *q;
8565 int i;
8566
8567 if (!priv)
8568 return;
8569
8570 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8571
Zhu Yib481de92007-09-25 17:54:57 -07008572 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008573
8574 iwl_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008575
8576 /* Free MAC hash list for ADHOC */
8577 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8578 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8579 list_del(p);
8580 kfree(list_entry(p, struct iwl_ibss_seq, list));
8581 }
8582 }
8583
8584 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
8585
8586 iwl_dealloc_ucode_pci(priv);
8587
8588 if (priv->rxq.bd)
8589 iwl_rx_queue_free(priv, &priv->rxq);
8590 iwl_hw_txq_ctx_free(priv);
8591
8592 iwl_unset_hw_setting(priv);
8593 iwl_clear_stations_table(priv);
8594
8595 if (priv->mac80211_registered) {
8596 ieee80211_unregister_hw(priv->hw);
8597 iwl_rate_control_unregister(priv->hw);
8598 }
8599
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08008600 /*netif_stop_queue(dev); */
8601 flush_workqueue(priv->workqueue);
8602
Zhu Yib481de92007-09-25 17:54:57 -07008603 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
8604 * priv->workqueue... so we can't take down the workqueue
8605 * until now... */
8606 destroy_workqueue(priv->workqueue);
8607 priv->workqueue = NULL;
8608
8609 free_irq(pdev->irq, priv);
8610 pci_disable_msi(pdev);
8611 pci_iounmap(pdev, priv->hw_base);
8612 pci_release_regions(pdev);
8613 pci_disable_device(pdev);
8614 pci_set_drvdata(pdev, NULL);
8615
8616 kfree(priv->channel_info);
8617
8618 kfree(priv->ieee_channels);
8619 kfree(priv->ieee_rates);
8620
8621 if (priv->ibss_beacon)
8622 dev_kfree_skb(priv->ibss_beacon);
8623
8624 ieee80211_free_hw(priv->hw);
8625}
8626
8627#ifdef CONFIG_PM
8628
8629static int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
8630{
8631 struct iwl_priv *priv = pci_get_drvdata(pdev);
8632
Zhu Yib481de92007-09-25 17:54:57 -07008633 set_bit(STATUS_IN_SUSPEND, &priv->status);
8634
8635 /* Take down the device; powers it off, etc. */
Zhu Yib24d22b2007-12-19 13:59:52 +08008636 iwl_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008637
8638 if (priv->mac80211_registered)
8639 ieee80211_stop_queues(priv->hw);
8640
8641 pci_save_state(pdev);
8642 pci_disable_device(pdev);
8643 pci_set_power_state(pdev, PCI_D3hot);
8644
Zhu Yib481de92007-09-25 17:54:57 -07008645 return 0;
8646}
8647
8648static void iwl_resume(struct iwl_priv *priv)
8649{
8650 unsigned long flags;
8651
8652 /* The following it a temporary work around due to the
8653 * suspend / resume not fully initializing the NIC correctly.
8654 * Without all of the following, resume will not attempt to take
8655 * down the NIC (it shouldn't really need to) and will just try
8656 * and bring the NIC back up. However that fails during the
8657 * ucode verification process. This then causes iwl_down to be
8658 * called *after* iwl_hw_nic_init() has succeeded -- which
8659 * then lets the next init sequence succeed. So, we've
8660 * replicated all of that NIC init code here... */
8661
8662 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
8663
8664 iwl_hw_nic_init(priv);
8665
8666 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
8667 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
8668 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
8669 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
8670 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
8671 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
8672
8673 /* tell the device to stop sending interrupts */
8674 iwl_disable_interrupts(priv);
8675
8676 spin_lock_irqsave(&priv->lock, flags);
8677 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
8678
Tomas Winklerac17a942007-10-25 17:15:37 +08008679 if (!iwl_grab_nic_access(priv)) {
Tomas Winklerd8609652007-10-25 17:15:35 +08008680 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07008681 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winklerac17a942007-10-25 17:15:37 +08008682 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008683 }
8684 spin_unlock_irqrestore(&priv->lock, flags);
8685
8686 udelay(5);
8687
8688 iwl_hw_nic_reset(priv);
8689
8690 /* Bring the device back up */
8691 clear_bit(STATUS_IN_SUSPEND, &priv->status);
8692 queue_work(priv->workqueue, &priv->up);
8693}
8694
8695static int iwl_pci_resume(struct pci_dev *pdev)
8696{
8697 struct iwl_priv *priv = pci_get_drvdata(pdev);
8698 int err;
8699
8700 printk(KERN_INFO "Coming out of suspend...\n");
8701
Zhu Yib481de92007-09-25 17:54:57 -07008702 pci_set_power_state(pdev, PCI_D0);
8703 err = pci_enable_device(pdev);
8704 pci_restore_state(pdev);
8705
8706 /*
8707 * Suspend/Resume resets the PCI configuration space, so we have to
8708 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
8709 * from interfering with C3 CPU state. pci_restore_state won't help
8710 * here since it only restores the first 64 bytes pci config header.
8711 */
8712 pci_write_config_byte(pdev, 0x41, 0x00);
8713
8714 iwl_resume(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008715
8716 return 0;
8717}
8718
8719#endif /* CONFIG_PM */
8720
8721/*****************************************************************************
8722 *
8723 * driver and module entry point
8724 *
8725 *****************************************************************************/
8726
8727static struct pci_driver iwl_driver = {
8728 .name = DRV_NAME,
8729 .id_table = iwl_hw_card_ids,
8730 .probe = iwl_pci_probe,
8731 .remove = __devexit_p(iwl_pci_remove),
8732#ifdef CONFIG_PM
8733 .suspend = iwl_pci_suspend,
8734 .resume = iwl_pci_resume,
8735#endif
8736};
8737
8738static int __init iwl_init(void)
8739{
8740
8741 int ret;
8742 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8743 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
8744 ret = pci_register_driver(&iwl_driver);
8745 if (ret) {
8746 IWL_ERROR("Unable to initialize PCI module\n");
8747 return ret;
8748 }
8749#ifdef CONFIG_IWLWIFI_DEBUG
8750 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
8751 if (ret) {
8752 IWL_ERROR("Unable to create driver sysfs file\n");
8753 pci_unregister_driver(&iwl_driver);
8754 return ret;
8755 }
8756#endif
8757
8758 return ret;
8759}
8760
8761static void __exit iwl_exit(void)
8762{
8763#ifdef CONFIG_IWLWIFI_DEBUG
8764 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
8765#endif
8766 pci_unregister_driver(&iwl_driver);
8767}
8768
8769module_param_named(antenna, iwl_param_antenna, int, 0444);
8770MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
8771module_param_named(disable, iwl_param_disable, int, 0444);
8772MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
8773module_param_named(hwcrypto, iwl_param_hwcrypto, int, 0444);
8774MODULE_PARM_DESC(hwcrypto,
8775 "using hardware crypto engine (default 0 [software])\n");
8776module_param_named(debug, iwl_param_debug, int, 0444);
8777MODULE_PARM_DESC(debug, "debug output mask");
8778module_param_named(disable_hw_scan, iwl_param_disable_hw_scan, int, 0444);
8779MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8780
8781module_param_named(queues_num, iwl_param_queues_num, int, 0444);
8782MODULE_PARM_DESC(queues_num, "number of hw queues.");
8783
8784/* QoS */
8785module_param_named(qos_enable, iwl_param_qos_enable, int, 0444);
8786MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
8787
8788module_exit(iwl_exit);
8789module_init(iwl_init);