blob: 7b0c602082b1fb202fe70b81cca33d5be96e0b92 [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 4965
60
Zhu Yib481de92007-09-25 17:54:57 -070061#include "iwlwifi.h"
62#include "iwl-4965.h"
63#include "iwl-helpers.h"
64
65#ifdef CONFIG_IWLWIFI_DEBUG
66u32 iwl_debug_level;
67#endif
68
69/******************************************************************************
70 *
71 * module boiler plate
72 *
73 ******************************************************************************/
74
75/* module parameters */
76int iwl_param_disable_hw_scan;
77int iwl_param_debug;
78int iwl_param_disable; /* def: enable radio */
79int iwl_param_antenna; /* def: 0 = both antennas (use diversity) */
80int iwl_param_hwcrypto; /* def: using software encryption */
81int iwl_param_qos_enable = 1;
82int iwl_param_queues_num = IWL_MAX_NUM_QUEUES;
83
84/*
85 * module name, copyright, version, etc.
86 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
87 */
88
89#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
90
91#ifdef CONFIG_IWLWIFI_DEBUG
92#define VD "d"
93#else
94#define VD
95#endif
96
97#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
98#define VS "s"
99#else
100#define VS
101#endif
102
Zhu Yi61f62252007-09-27 11:27:44 +0800103#define IWLWIFI_VERSION "1.1.17k" VD VS
Zhu Yib481de92007-09-25 17:54:57 -0700104#define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
105#define DRV_VERSION IWLWIFI_VERSION
106
107/* Change firmware file name, using "-" and incrementing number,
108 * *only* when uCode interface or architecture changes so that it
109 * is not compatible with earlier drivers.
110 * This number will also appear in << 8 position of 1st dword of uCode file */
111#define IWL4965_UCODE_API "-1"
112
113MODULE_DESCRIPTION(DRV_DESCRIPTION);
114MODULE_VERSION(DRV_VERSION);
115MODULE_AUTHOR(DRV_COPYRIGHT);
116MODULE_LICENSE("GPL");
117
118__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
119{
120 u16 fc = le16_to_cpu(hdr->frame_control);
121 int hdr_len = ieee80211_get_hdrlen(fc);
122
123 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
124 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
125 return NULL;
126}
127
128static const struct ieee80211_hw_mode *iwl_get_hw_mode(
129 struct iwl_priv *priv, int mode)
130{
131 int i;
132
133 for (i = 0; i < 3; i++)
134 if (priv->modes[i].mode == mode)
135 return &priv->modes[i];
136
137 return NULL;
138}
139
140static int iwl_is_empty_essid(const char *essid, int essid_len)
141{
142 /* Single white space is for Linksys APs */
143 if (essid_len == 1 && essid[0] == ' ')
144 return 1;
145
146 /* Otherwise, if the entire essid is 0, we assume it is hidden */
147 while (essid_len) {
148 essid_len--;
149 if (essid[essid_len] != '\0')
150 return 0;
151 }
152
153 return 1;
154}
155
156static const char *iwl_escape_essid(const char *essid, u8 essid_len)
157{
158 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
159 const char *s = essid;
160 char *d = escaped;
161
162 if (iwl_is_empty_essid(essid, essid_len)) {
163 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
164 return escaped;
165 }
166
167 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
168 while (essid_len--) {
169 if (*s == '\0') {
170 *d++ = '\\';
171 *d++ = '0';
172 s++;
173 } else
174 *d++ = *s++;
175 }
176 *d = '\0';
177 return escaped;
178}
179
180static void iwl_print_hex_dump(int level, void *p, u32 len)
181{
182#ifdef CONFIG_IWLWIFI_DEBUG
183 if (!(iwl_debug_level & level))
184 return;
185
186 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
187 p, len, 1);
188#endif
189}
190
191/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
192 * DMA services
193 *
194 * Theory of operation
195 *
196 * A queue is a circular buffers with 'Read' and 'Write' pointers.
197 * 2 empty entries always kept in the buffer to protect from overflow.
198 *
199 * For Tx queue, there are low mark and high mark limits. If, after queuing
200 * the packet for Tx, free space become < low mark, Tx queue stopped. When
201 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
202 * Tx queue resumed.
203 *
Zhu Yi583fab32007-09-27 11:27:30 +0800204 * The IWL operates with six queues, one receive queue in the device's
Zhu Yib481de92007-09-25 17:54:57 -0700205 * sram, one transmit queue for sending commands to the device firmware,
206 * and four transmit queues for data.
207 ***************************************************/
208
209static int iwl_queue_space(const struct iwl_queue *q)
210{
211 int s = q->last_used - q->first_empty;
212
213 if (q->last_used > q->first_empty)
214 s -= q->n_bd;
215
216 if (s <= 0)
217 s += q->n_window;
218 /* keep some reserve to not confuse empty and full situations */
219 s -= 2;
220 if (s < 0)
221 s = 0;
222 return s;
223}
224
225/* XXX: n_bd must be power-of-two size */
226static inline int iwl_queue_inc_wrap(int index, int n_bd)
227{
228 return ++index & (n_bd - 1);
229}
230
231/* XXX: n_bd must be power-of-two size */
232static inline int iwl_queue_dec_wrap(int index, int n_bd)
233{
234 return --index & (n_bd - 1);
235}
236
237static inline int x2_queue_used(const struct iwl_queue *q, int i)
238{
239 return q->first_empty > q->last_used ?
240 (i >= q->last_used && i < q->first_empty) :
241 !(i < q->last_used && i >= q->first_empty);
242}
243
244static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
245{
246 if (is_huge)
247 return q->n_window;
248
249 return index & (q->n_window - 1);
250}
251
252static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
253 int count, int slots_num, u32 id)
254{
255 q->n_bd = count;
256 q->n_window = slots_num;
257 q->id = id;
258
259 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
260 * and iwl_queue_dec_wrap are broken. */
261 BUG_ON(!is_power_of_2(count));
262
263 /* slots_num must be power-of-two size, otherwise
264 * get_cmd_index is broken. */
265 BUG_ON(!is_power_of_2(slots_num));
266
267 q->low_mark = q->n_window / 4;
268 if (q->low_mark < 4)
269 q->low_mark = 4;
270
271 q->high_mark = q->n_window / 8;
272 if (q->high_mark < 2)
273 q->high_mark = 2;
274
275 q->first_empty = q->last_used = 0;
276
277 return 0;
278}
279
280static int iwl_tx_queue_alloc(struct iwl_priv *priv,
281 struct iwl_tx_queue *txq, u32 id)
282{
283 struct pci_dev *dev = priv->pci_dev;
284
285 if (id != IWL_CMD_QUEUE_NUM) {
286 txq->txb = kmalloc(sizeof(txq->txb[0]) *
287 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
288 if (!txq->txb) {
289 IWL_ERROR("kmalloc for auxilary BD "
290 "structures failed\n");
291 goto error;
292 }
293 } else
294 txq->txb = NULL;
295
296 txq->bd = pci_alloc_consistent(dev,
297 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
298 &txq->q.dma_addr);
299
300 if (!txq->bd) {
301 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
302 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
303 goto error;
304 }
305 txq->q.id = id;
306
307 return 0;
308
309 error:
310 if (txq->txb) {
311 kfree(txq->txb);
312 txq->txb = NULL;
313 }
314
315 return -ENOMEM;
316}
317
318int iwl_tx_queue_init(struct iwl_priv *priv,
319 struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
320{
321 struct pci_dev *dev = priv->pci_dev;
322 int len;
323 int rc = 0;
324
325 /* alocate command space + one big command for scan since scan
326 * command is very huge the system will not have two scan at the
327 * same time */
328 len = sizeof(struct iwl_cmd) * slots_num;
329 if (txq_id == IWL_CMD_QUEUE_NUM)
330 len += IWL_MAX_SCAN_SIZE;
331 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
332 if (!txq->cmd)
333 return -ENOMEM;
334
335 rc = iwl_tx_queue_alloc(priv, txq, txq_id);
336 if (rc) {
337 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
338
339 return -ENOMEM;
340 }
341 txq->need_update = 0;
342
343 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
344 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
345 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
346 iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
347
348 iwl_hw_tx_queue_init(priv, txq);
349
350 return 0;
351}
352
353/**
354 * iwl_tx_queue_free - Deallocate DMA queue.
355 * @txq: Transmit queue to deallocate.
356 *
357 * Empty queue by removing and destroying all BD's.
358 * Free all buffers. txq itself is not freed.
359 *
360 */
361void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
362{
363 struct iwl_queue *q = &txq->q;
364 struct pci_dev *dev = priv->pci_dev;
365 int len;
366
367 if (q->n_bd == 0)
368 return;
369
370 /* first, empty all BD's */
371 for (; q->first_empty != q->last_used;
372 q->last_used = iwl_queue_inc_wrap(q->last_used, q->n_bd))
373 iwl_hw_txq_free_tfd(priv, txq);
374
375 len = sizeof(struct iwl_cmd) * q->n_window;
376 if (q->id == IWL_CMD_QUEUE_NUM)
377 len += IWL_MAX_SCAN_SIZE;
378
379 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
380
381 /* free buffers belonging to queue itself */
382 if (txq->q.n_bd)
383 pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
384 txq->q.n_bd, txq->bd, txq->q.dma_addr);
385
386 if (txq->txb) {
387 kfree(txq->txb);
388 txq->txb = NULL;
389 }
390
391 /* 0 fill whole structure */
392 memset(txq, 0, sizeof(*txq));
393}
394
395const u8 BROADCAST_ADDR[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
396
397/*************** STATION TABLE MANAGEMENT ****
398 *
399 * NOTE: This needs to be overhauled to better synchronize between
400 * how the iwl-4965.c is using iwl_hw_find_station vs. iwl-3945.c
401 *
402 * mac80211 should also be examined to determine if sta_info is duplicating
403 * the functionality provided here
404 */
405
406/**************************************************************/
407
Zhu Yi556f8db2007-09-27 11:27:33 +0800408#if 0 /* temparary disable till we add real remove station */
Zhu Yib481de92007-09-25 17:54:57 -0700409static u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
410{
411 int index = IWL_INVALID_STATION;
412 int i;
413 unsigned long flags;
414
415 spin_lock_irqsave(&priv->sta_lock, flags);
416
417 if (is_ap)
418 index = IWL_AP_ID;
419 else if (is_broadcast_ether_addr(addr))
420 index = priv->hw_setting.bcast_sta_id;
421 else
422 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
423 if (priv->stations[i].used &&
424 !compare_ether_addr(priv->stations[i].sta.sta.addr,
425 addr)) {
426 index = i;
427 break;
428 }
429
430 if (unlikely(index == IWL_INVALID_STATION))
431 goto out;
432
433 if (priv->stations[index].used) {
434 priv->stations[index].used = 0;
435 priv->num_stations--;
436 }
437
438 BUG_ON(priv->num_stations < 0);
439
440out:
441 spin_unlock_irqrestore(&priv->sta_lock, flags);
442 return 0;
443}
Zhu Yi556f8db2007-09-27 11:27:33 +0800444#endif
Zhu Yib481de92007-09-25 17:54:57 -0700445
446static void iwl_clear_stations_table(struct iwl_priv *priv)
447{
448 unsigned long flags;
449
450 spin_lock_irqsave(&priv->sta_lock, flags);
451
452 priv->num_stations = 0;
453 memset(priv->stations, 0, sizeof(priv->stations));
454
455 spin_unlock_irqrestore(&priv->sta_lock, flags);
456}
457
458u8 iwl_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
459{
460 int i;
461 int index = IWL_INVALID_STATION;
462 struct iwl_station_entry *station;
463 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700464 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700465
466 spin_lock_irqsave(&priv->sta_lock, flags_spin);
467 if (is_ap)
468 index = IWL_AP_ID;
469 else if (is_broadcast_ether_addr(addr))
470 index = priv->hw_setting.bcast_sta_id;
471 else
472 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
473 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
474 addr)) {
475 index = i;
476 break;
477 }
478
479 if (!priv->stations[i].used &&
480 index == IWL_INVALID_STATION)
481 index = i;
482 }
483
484
485 /* These twh conditions has the same outcome but keep them separate
486 since they have different meaning */
487 if (unlikely(index == IWL_INVALID_STATION)) {
488 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
489 return index;
490 }
491
492 if (priv->stations[index].used &&
493 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
494 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
495 return index;
496 }
497
498
Joe Perches0795af52007-10-03 17:59:30 -0700499 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700500 station = &priv->stations[index];
501 station->used = 1;
502 priv->num_stations++;
503
504 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
505 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
506 station->sta.mode = 0;
507 station->sta.sta.sta_id = index;
508 station->sta.station_flags = 0;
509
510#ifdef CONFIG_IWLWIFI_HT
511 /* BCAST station and IBSS stations do not work in HT mode */
512 if (index != priv->hw_setting.bcast_sta_id &&
513 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
514 iwl4965_set_ht_add_station(priv, index);
515#endif /*CONFIG_IWLWIFI_HT*/
516
517 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
518 iwl_send_add_station(priv, &station->sta, flags);
519 return index;
520
521}
522
523/*************** DRIVER STATUS FUNCTIONS *****/
524
525static inline int iwl_is_ready(struct iwl_priv *priv)
526{
527 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
528 * set but EXIT_PENDING is not */
529 return test_bit(STATUS_READY, &priv->status) &&
530 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
531 !test_bit(STATUS_EXIT_PENDING, &priv->status);
532}
533
534static inline int iwl_is_alive(struct iwl_priv *priv)
535{
536 return test_bit(STATUS_ALIVE, &priv->status);
537}
538
539static inline int iwl_is_init(struct iwl_priv *priv)
540{
541 return test_bit(STATUS_INIT, &priv->status);
542}
543
544static inline int iwl_is_rfkill(struct iwl_priv *priv)
545{
546 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
547 test_bit(STATUS_RF_KILL_SW, &priv->status);
548}
549
550static inline int iwl_is_ready_rf(struct iwl_priv *priv)
551{
552
553 if (iwl_is_rfkill(priv))
554 return 0;
555
556 return iwl_is_ready(priv);
557}
558
559/*************** HOST COMMAND QUEUE FUNCTIONS *****/
560
561#define IWL_CMD(x) case x : return #x
562
563static const char *get_cmd_string(u8 cmd)
564{
565 switch (cmd) {
566 IWL_CMD(REPLY_ALIVE);
567 IWL_CMD(REPLY_ERROR);
568 IWL_CMD(REPLY_RXON);
569 IWL_CMD(REPLY_RXON_ASSOC);
570 IWL_CMD(REPLY_QOS_PARAM);
571 IWL_CMD(REPLY_RXON_TIMING);
572 IWL_CMD(REPLY_ADD_STA);
573 IWL_CMD(REPLY_REMOVE_STA);
574 IWL_CMD(REPLY_REMOVE_ALL_STA);
575 IWL_CMD(REPLY_TX);
576 IWL_CMD(REPLY_RATE_SCALE);
577 IWL_CMD(REPLY_LEDS_CMD);
578 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
579 IWL_CMD(RADAR_NOTIFICATION);
580 IWL_CMD(REPLY_QUIET_CMD);
581 IWL_CMD(REPLY_CHANNEL_SWITCH);
582 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
583 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
584 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
585 IWL_CMD(POWER_TABLE_CMD);
586 IWL_CMD(PM_SLEEP_NOTIFICATION);
587 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
588 IWL_CMD(REPLY_SCAN_CMD);
589 IWL_CMD(REPLY_SCAN_ABORT_CMD);
590 IWL_CMD(SCAN_START_NOTIFICATION);
591 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
592 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
593 IWL_CMD(BEACON_NOTIFICATION);
594 IWL_CMD(REPLY_TX_BEACON);
595 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
596 IWL_CMD(QUIET_NOTIFICATION);
597 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
598 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
599 IWL_CMD(REPLY_BT_CONFIG);
600 IWL_CMD(REPLY_STATISTICS_CMD);
601 IWL_CMD(STATISTICS_NOTIFICATION);
602 IWL_CMD(REPLY_CARD_STATE_CMD);
603 IWL_CMD(CARD_STATE_NOTIFICATION);
604 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
605 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
606 IWL_CMD(SENSITIVITY_CMD);
607 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
608 IWL_CMD(REPLY_RX_PHY_CMD);
609 IWL_CMD(REPLY_RX_MPDU_CMD);
610 IWL_CMD(REPLY_4965_RX);
611 IWL_CMD(REPLY_COMPRESSED_BA);
612 default:
613 return "UNKNOWN";
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 ret;
640 unsigned long flags;
641
642 /* If any of the command structures end up being larger than
643 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
644 * we will need to increase the size of the TFD entries */
645 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
646 !(cmd->meta.flags & CMD_SIZE_HUGE));
647
648 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
649 IWL_ERROR("No space for Tx\n");
650 return -ENOSPC;
651 }
652
653 spin_lock_irqsave(&priv->hcmd_lock, flags);
654
655 tfd = &txq->bd[q->first_empty];
656 memset(tfd, 0, sizeof(*tfd));
657
658 control_flags = (u32 *) tfd;
659
660 idx = get_cmd_index(q, q->first_empty, cmd->meta.flags & CMD_SIZE_HUGE);
661 out_cmd = &txq->cmd[idx];
662
663 out_cmd->hdr.cmd = cmd->id;
664 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
665 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
666
667 /* At this point, the out_cmd now has all of the incoming cmd
668 * information */
669
670 out_cmd->hdr.flags = 0;
671 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
672 INDEX_TO_SEQ(q->first_empty));
673 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
674 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
675
676 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
677 offsetof(struct iwl_cmd, hdr);
678 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
679
680 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
681 "%d bytes at %d[%d]:%d\n",
682 get_cmd_string(out_cmd->hdr.cmd),
683 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
684 fix_size, q->first_empty, idx, IWL_CMD_QUEUE_NUM);
685
686 txq->need_update = 1;
687 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
688 q->first_empty = iwl_queue_inc_wrap(q->first_empty, q->n_bd);
689 iwl_tx_queue_update_write_ptr(priv, txq);
690
691 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
692 return ret ? ret : idx;
693}
694
695int iwl_send_cmd_async(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
696{
697 int ret;
698
699 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
700
701 /* An asynchronous command can not expect an SKB to be set. */
702 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
703
704 /* An asynchronous command MUST have a callback. */
705 BUG_ON(!cmd->meta.u.callback);
706
707 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
708 return -EBUSY;
709
710 ret = iwl_enqueue_hcmd(priv, cmd);
711 if (ret < 0) {
712 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
713 get_cmd_string(cmd->id), ret);
714 return ret;
715 }
716 return 0;
717}
718
719int iwl_send_cmd_sync(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
720{
721 int cmd_idx;
722 int ret;
723 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
724
725 BUG_ON(cmd->meta.flags & CMD_ASYNC);
726
727 /* A synchronous command can not have a callback set. */
728 BUG_ON(cmd->meta.u.callback != NULL);
729
730 if (atomic_xchg(&entry, 1)) {
731 IWL_ERROR("Error sending %s: Already sending a host command\n",
732 get_cmd_string(cmd->id));
733 return -EBUSY;
734 }
735
736 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
737
738 if (cmd->meta.flags & CMD_WANT_SKB)
739 cmd->meta.source = &cmd->meta;
740
741 cmd_idx = iwl_enqueue_hcmd(priv, cmd);
742 if (cmd_idx < 0) {
743 ret = cmd_idx;
744 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
745 get_cmd_string(cmd->id), ret);
746 goto out;
747 }
748
749 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
750 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
751 HOST_COMPLETE_TIMEOUT);
752 if (!ret) {
753 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
754 IWL_ERROR("Error sending %s: time out after %dms.\n",
755 get_cmd_string(cmd->id),
756 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
757
758 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
759 ret = -ETIMEDOUT;
760 goto cancel;
761 }
762 }
763
764 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
765 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
766 get_cmd_string(cmd->id));
767 ret = -ECANCELED;
768 goto fail;
769 }
770 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
771 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
772 get_cmd_string(cmd->id));
773 ret = -EIO;
774 goto fail;
775 }
776 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
777 IWL_ERROR("Error: Response NULL in '%s'\n",
778 get_cmd_string(cmd->id));
779 ret = -EIO;
780 goto out;
781 }
782
783 ret = 0;
784 goto out;
785
786cancel:
787 if (cmd->meta.flags & CMD_WANT_SKB) {
788 struct iwl_cmd *qcmd;
789
790 /* Cancel the CMD_WANT_SKB flag for the cmd in the
791 * TX cmd queue. Otherwise in case the cmd comes
792 * in later, it will possibly set an invalid
793 * address (cmd->meta.source). */
794 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
795 qcmd->meta.flags &= ~CMD_WANT_SKB;
796 }
797fail:
798 if (cmd->meta.u.skb) {
799 dev_kfree_skb_any(cmd->meta.u.skb);
800 cmd->meta.u.skb = NULL;
801 }
802out:
803 atomic_set(&entry, 0);
804 return ret;
805}
806
807int iwl_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
808{
809 /* A command can not be asynchronous AND expect an SKB to be set. */
810 BUG_ON((cmd->meta.flags & CMD_ASYNC) &&
811 (cmd->meta.flags & CMD_WANT_SKB));
812
813 if (cmd->meta.flags & CMD_ASYNC)
814 return iwl_send_cmd_async(priv, cmd);
815
816 return iwl_send_cmd_sync(priv, cmd);
817}
818
819int iwl_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
820{
821 struct iwl_host_cmd cmd = {
822 .id = id,
823 .len = len,
824 .data = data,
825 };
826
827 return iwl_send_cmd_sync(priv, &cmd);
828}
829
830static int __must_check iwl_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
831{
832 struct iwl_host_cmd cmd = {
833 .id = id,
834 .len = sizeof(val),
835 .data = &val,
836 };
837
838 return iwl_send_cmd_sync(priv, &cmd);
839}
840
841int iwl_send_statistics_request(struct iwl_priv *priv)
842{
843 return iwl_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
844}
845
846/**
847 * iwl_rxon_add_station - add station into station table.
848 *
849 * there is only one AP station with id= IWL_AP_ID
850 * NOTE: mutex must be held before calling the this fnction
851*/
852static int iwl_rxon_add_station(struct iwl_priv *priv,
853 const u8 *addr, int is_ap)
854{
Zhu Yi556f8db2007-09-27 11:27:33 +0800855 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700856
Zhu Yi556f8db2007-09-27 11:27:33 +0800857 sta_id = iwl_add_station(priv, addr, is_ap, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700858 iwl4965_add_station(priv, addr, is_ap);
859
Zhu Yi556f8db2007-09-27 11:27:33 +0800860 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700861}
862
863/**
864 * iwl_set_rxon_channel - Set the phymode and channel values in staging RXON
865 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
866 * @channel: Any channel valid for the requested phymode
867
868 * In addition to setting the staging RXON, priv->phymode is also set.
869 *
870 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
871 * in the staging RXON flag structure based on the phymode
872 */
873static int iwl_set_rxon_channel(struct iwl_priv *priv, u8 phymode, u16 channel)
874{
875 if (!iwl_get_channel_info(priv, phymode, channel)) {
876 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
877 channel, phymode);
878 return -EINVAL;
879 }
880
881 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
882 (priv->phymode == phymode))
883 return 0;
884
885 priv->staging_rxon.channel = cpu_to_le16(channel);
886 if (phymode == MODE_IEEE80211A)
887 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
888 else
889 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
890
891 priv->phymode = phymode;
892
893 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
894
895 return 0;
896}
897
898/**
899 * iwl_check_rxon_cmd - validate RXON structure is valid
900 *
901 * NOTE: This is really only useful during development and can eventually
902 * be #ifdef'd out once the driver is stable and folks aren't actively
903 * making changes
904 */
905static int iwl_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
906{
907 int error = 0;
908 int counter = 1;
909
910 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
911 error |= le32_to_cpu(rxon->flags &
912 (RXON_FLG_TGJ_NARROW_BAND_MSK |
913 RXON_FLG_RADAR_DETECT_MSK));
914 if (error)
915 IWL_WARNING("check 24G fields %d | %d\n",
916 counter++, error);
917 } else {
918 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
919 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
920 if (error)
921 IWL_WARNING("check 52 fields %d | %d\n",
922 counter++, error);
923 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
924 if (error)
925 IWL_WARNING("check 52 CCK %d | %d\n",
926 counter++, error);
927 }
928 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
929 if (error)
930 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
931
932 /* make sure basic rates 6Mbps and 1Mbps are supported */
933 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
934 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
935 if (error)
936 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
937
938 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
939 if (error)
940 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
941
942 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
943 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
944 if (error)
945 IWL_WARNING("check CCK and short slot %d | %d\n",
946 counter++, error);
947
948 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
949 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
950 if (error)
951 IWL_WARNING("check CCK & auto detect %d | %d\n",
952 counter++, error);
953
954 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
955 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
956 if (error)
957 IWL_WARNING("check TGG and auto detect %d | %d\n",
958 counter++, error);
959
960 if (error)
961 IWL_WARNING("Tuning to channel %d\n",
962 le16_to_cpu(rxon->channel));
963
964 if (error) {
965 IWL_ERROR("Not a valid iwl_rxon_assoc_cmd field values\n");
966 return -1;
967 }
968 return 0;
969}
970
971/**
972 * iwl_full_rxon_required - determine if RXON_ASSOC can be used in RXON commit
973 * @priv: staging_rxon is comapred to active_rxon
974 *
975 * If the RXON structure is changing sufficient to require a new
976 * tune or to clear and reset the RXON_FILTER_ASSOC_MSK then return 1
977 * to indicate a new tune is required.
978 */
979static int iwl_full_rxon_required(struct iwl_priv *priv)
980{
981
982 /* These items are only settable from the full RXON command */
983 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
984 compare_ether_addr(priv->staging_rxon.bssid_addr,
985 priv->active_rxon.bssid_addr) ||
986 compare_ether_addr(priv->staging_rxon.node_addr,
987 priv->active_rxon.node_addr) ||
988 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
989 priv->active_rxon.wlap_bssid_addr) ||
990 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
991 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
992 (priv->staging_rxon.air_propagation !=
993 priv->active_rxon.air_propagation) ||
994 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
995 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
996 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
997 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
998 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
999 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1000 return 1;
1001
1002 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1003 * be updated with the RXON_ASSOC command -- however only some
1004 * flag transitions are allowed using RXON_ASSOC */
1005
1006 /* Check if we are not switching bands */
1007 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1008 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1009 return 1;
1010
1011 /* Check if we are switching association toggle */
1012 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1013 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1014 return 1;
1015
1016 return 0;
1017}
1018
1019static int iwl_send_rxon_assoc(struct iwl_priv *priv)
1020{
1021 int rc = 0;
1022 struct iwl_rx_packet *res = NULL;
1023 struct iwl_rxon_assoc_cmd rxon_assoc;
1024 struct iwl_host_cmd cmd = {
1025 .id = REPLY_RXON_ASSOC,
1026 .len = sizeof(rxon_assoc),
1027 .meta.flags = CMD_WANT_SKB,
1028 .data = &rxon_assoc,
1029 };
1030 const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
1031 const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
1032
1033 if ((rxon1->flags == rxon2->flags) &&
1034 (rxon1->filter_flags == rxon2->filter_flags) &&
1035 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1036 (rxon1->ofdm_ht_single_stream_basic_rates ==
1037 rxon2->ofdm_ht_single_stream_basic_rates) &&
1038 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1039 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1040 (rxon1->rx_chain == rxon2->rx_chain) &&
1041 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1042 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1043 return 0;
1044 }
1045
1046 rxon_assoc.flags = priv->staging_rxon.flags;
1047 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1048 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1049 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1050 rxon_assoc.reserved = 0;
1051 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1052 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1053 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1054 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1055 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1056
1057 rc = iwl_send_cmd_sync(priv, &cmd);
1058 if (rc)
1059 return rc;
1060
1061 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1062 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1063 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1064 rc = -EIO;
1065 }
1066
1067 priv->alloc_rxb_skb--;
1068 dev_kfree_skb_any(cmd.meta.u.skb);
1069
1070 return rc;
1071}
1072
1073/**
1074 * iwl_commit_rxon - commit staging_rxon to hardware
1075 *
1076 * The RXON command in staging_rxon is commited to the hardware and
1077 * the active_rxon structure is updated with the new data. This
1078 * function correctly transitions out of the RXON_ASSOC_MSK state if
1079 * a HW tune is required based on the RXON structure changes.
1080 */
1081static int iwl_commit_rxon(struct iwl_priv *priv)
1082{
1083 /* cast away the const for active_rxon in this function */
1084 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001085 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001086 int rc = 0;
1087
1088 if (!iwl_is_alive(priv))
1089 return -1;
1090
1091 /* always get timestamp with Rx frame */
1092 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1093
1094 rc = iwl_check_rxon_cmd(&priv->staging_rxon);
1095 if (rc) {
1096 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1097 return -EINVAL;
1098 }
1099
1100 /* If we don't need to send a full RXON, we can use
1101 * iwl_rxon_assoc_cmd which is used to reconfigure filter
1102 * and other flags for the current radio configuration. */
1103 if (!iwl_full_rxon_required(priv)) {
1104 rc = iwl_send_rxon_assoc(priv);
1105 if (rc) {
1106 IWL_ERROR("Error setting RXON_ASSOC "
1107 "configuration (%d).\n", rc);
1108 return rc;
1109 }
1110
1111 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1112
1113 return 0;
1114 }
1115
1116 /* station table will be cleared */
1117 priv->assoc_station_added = 0;
1118
1119#ifdef CONFIG_IWLWIFI_SENSITIVITY
1120 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1121 if (!priv->error_recovering)
1122 priv->start_calib = 0;
1123
1124 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
1125#endif /* CONFIG_IWLWIFI_SENSITIVITY */
1126
1127 /* If we are currently associated and the new config requires
1128 * an RXON_ASSOC and the new config wants the associated mask enabled,
1129 * we must clear the associated from the active configuration
1130 * before we apply the new config */
1131 if (iwl_is_associated(priv) &&
1132 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1133 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1134 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1135
1136 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1137 sizeof(struct iwl_rxon_cmd),
1138 &priv->active_rxon);
1139
1140 /* If the mask clearing failed then we set
1141 * active_rxon back to what it was previously */
1142 if (rc) {
1143 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1144 IWL_ERROR("Error clearing ASSOC_MSK on current "
1145 "configuration (%d).\n", rc);
1146 return rc;
1147 }
Zhu Yib481de92007-09-25 17:54:57 -07001148 }
1149
1150 IWL_DEBUG_INFO("Sending RXON\n"
1151 "* with%s RXON_FILTER_ASSOC_MSK\n"
1152 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001153 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001154 ((priv->staging_rxon.filter_flags &
1155 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1156 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001157 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001158
1159 /* Apply the new configuration */
1160 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1161 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
1162 if (rc) {
1163 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1164 return rc;
1165 }
1166
Zhu Yi556f8db2007-09-27 11:27:33 +08001167 iwl_clear_stations_table(priv);
1168
Zhu Yib481de92007-09-25 17:54:57 -07001169#ifdef CONFIG_IWLWIFI_SENSITIVITY
1170 if (!priv->error_recovering)
1171 priv->start_calib = 0;
1172
1173 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1174 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
1175#endif /* CONFIG_IWLWIFI_SENSITIVITY */
1176
1177 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1178
1179 /* If we issue a new RXON command which required a tune then we must
1180 * send a new TXPOWER command or we won't be able to Tx any frames */
1181 rc = iwl_hw_reg_send_txpower(priv);
1182 if (rc) {
1183 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1184 return rc;
1185 }
1186
1187 /* Add the broadcast address so we can send broadcast frames */
1188 if (iwl_rxon_add_station(priv, BROADCAST_ADDR, 0) ==
1189 IWL_INVALID_STATION) {
1190 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1191 return -EIO;
1192 }
1193
1194 /* If we have set the ASSOC_MSK and we are in BSS mode then
1195 * add the IWL_AP_ID to the station rate table */
1196 if (iwl_is_associated(priv) &&
1197 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
1198 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
1199 == IWL_INVALID_STATION) {
1200 IWL_ERROR("Error adding AP address for transmit.\n");
1201 return -EIO;
1202 }
1203 priv->assoc_station_added = 1;
1204 }
1205
1206 return 0;
1207}
1208
1209static int iwl_send_bt_config(struct iwl_priv *priv)
1210{
1211 struct iwl_bt_cmd bt_cmd = {
1212 .flags = 3,
1213 .lead_time = 0xAA,
1214 .max_kill = 1,
1215 .kill_ack_mask = 0,
1216 .kill_cts_mask = 0,
1217 };
1218
1219 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1220 sizeof(struct iwl_bt_cmd), &bt_cmd);
1221}
1222
1223static int iwl_send_scan_abort(struct iwl_priv *priv)
1224{
1225 int rc = 0;
1226 struct iwl_rx_packet *res;
1227 struct iwl_host_cmd cmd = {
1228 .id = REPLY_SCAN_ABORT_CMD,
1229 .meta.flags = CMD_WANT_SKB,
1230 };
1231
1232 /* If there isn't a scan actively going on in the hardware
1233 * then we are in between scan bands and not actually
1234 * actively scanning, so don't send the abort command */
1235 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1236 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1237 return 0;
1238 }
1239
1240 rc = iwl_send_cmd_sync(priv, &cmd);
1241 if (rc) {
1242 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1243 return rc;
1244 }
1245
1246 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1247 if (res->u.status != CAN_ABORT_STATUS) {
1248 /* The scan abort will return 1 for success or
1249 * 2 for "failure". A failure condition can be
1250 * due to simply not being in an active scan which
1251 * can occur if we send the scan abort before we
1252 * the microcode has notified us that a scan is
1253 * completed. */
1254 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1255 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1256 clear_bit(STATUS_SCAN_HW, &priv->status);
1257 }
1258
1259 dev_kfree_skb_any(cmd.meta.u.skb);
1260
1261 return rc;
1262}
1263
1264static int iwl_card_state_sync_callback(struct iwl_priv *priv,
1265 struct iwl_cmd *cmd,
1266 struct sk_buff *skb)
1267{
1268 return 1;
1269}
1270
1271/*
1272 * CARD_STATE_CMD
1273 *
1274 * Use: Sets the internal card state to enable, disable, or halt
1275 *
1276 * When in the 'enable' state the card operates as normal.
1277 * When in the 'disable' state, the card enters into a low power mode.
1278 * When in the 'halt' state, the card is shut down and must be fully
1279 * restarted to come back on.
1280 */
1281static int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
1282{
1283 struct iwl_host_cmd cmd = {
1284 .id = REPLY_CARD_STATE_CMD,
1285 .len = sizeof(u32),
1286 .data = &flags,
1287 .meta.flags = meta_flag,
1288 };
1289
1290 if (meta_flag & CMD_ASYNC)
1291 cmd.meta.u.callback = iwl_card_state_sync_callback;
1292
1293 return iwl_send_cmd(priv, &cmd);
1294}
1295
1296static int iwl_add_sta_sync_callback(struct iwl_priv *priv,
1297 struct iwl_cmd *cmd, struct sk_buff *skb)
1298{
1299 struct iwl_rx_packet *res = NULL;
1300
1301 if (!skb) {
1302 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1303 return 1;
1304 }
1305
1306 res = (struct iwl_rx_packet *)skb->data;
1307 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1308 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1309 res->hdr.flags);
1310 return 1;
1311 }
1312
1313 switch (res->u.add_sta.status) {
1314 case ADD_STA_SUCCESS_MSK:
1315 break;
1316 default:
1317 break;
1318 }
1319
1320 /* We didn't cache the SKB; let the caller free it */
1321 return 1;
1322}
1323
1324int iwl_send_add_station(struct iwl_priv *priv,
1325 struct iwl_addsta_cmd *sta, u8 flags)
1326{
1327 struct iwl_rx_packet *res = NULL;
1328 int rc = 0;
1329 struct iwl_host_cmd cmd = {
1330 .id = REPLY_ADD_STA,
1331 .len = sizeof(struct iwl_addsta_cmd),
1332 .meta.flags = flags,
1333 .data = sta,
1334 };
1335
1336 if (flags & CMD_ASYNC)
1337 cmd.meta.u.callback = iwl_add_sta_sync_callback;
1338 else
1339 cmd.meta.flags |= CMD_WANT_SKB;
1340
1341 rc = iwl_send_cmd(priv, &cmd);
1342
1343 if (rc || (flags & CMD_ASYNC))
1344 return rc;
1345
1346 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1347 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1348 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1349 res->hdr.flags);
1350 rc = -EIO;
1351 }
1352
1353 if (rc == 0) {
1354 switch (res->u.add_sta.status) {
1355 case ADD_STA_SUCCESS_MSK:
1356 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1357 break;
1358 default:
1359 rc = -EIO;
1360 IWL_WARNING("REPLY_ADD_STA failed\n");
1361 break;
1362 }
1363 }
1364
1365 priv->alloc_rxb_skb--;
1366 dev_kfree_skb_any(cmd.meta.u.skb);
1367
1368 return rc;
1369}
1370
1371static int iwl_update_sta_key_info(struct iwl_priv *priv,
1372 struct ieee80211_key_conf *keyconf,
1373 u8 sta_id)
1374{
1375 unsigned long flags;
1376 __le16 key_flags = 0;
1377
1378 switch (keyconf->alg) {
1379 case ALG_CCMP:
1380 key_flags |= STA_KEY_FLG_CCMP;
1381 key_flags |= cpu_to_le16(
1382 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1383 key_flags &= ~STA_KEY_FLG_INVALID;
1384 break;
1385 case ALG_TKIP:
1386 case ALG_WEP:
1387 return -EINVAL;
1388 default:
1389 return -EINVAL;
1390 }
1391 spin_lock_irqsave(&priv->sta_lock, flags);
1392 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1393 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1394 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1395 keyconf->keylen);
1396
1397 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1398 keyconf->keylen);
1399 priv->stations[sta_id].sta.key.key_flags = key_flags;
1400 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1401 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1402
1403 spin_unlock_irqrestore(&priv->sta_lock, flags);
1404
1405 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
1406 iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1407 return 0;
1408}
1409
1410static int iwl_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
1411{
1412 unsigned long flags;
1413
1414 spin_lock_irqsave(&priv->sta_lock, flags);
1415 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
1416 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl_keyinfo));
1417 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1418 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1419 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1420 spin_unlock_irqrestore(&priv->sta_lock, flags);
1421
1422 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
1423 iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1424 return 0;
1425}
1426
1427static void iwl_clear_free_frames(struct iwl_priv *priv)
1428{
1429 struct list_head *element;
1430
1431 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1432 priv->frames_count);
1433
1434 while (!list_empty(&priv->free_frames)) {
1435 element = priv->free_frames.next;
1436 list_del(element);
1437 kfree(list_entry(element, struct iwl_frame, list));
1438 priv->frames_count--;
1439 }
1440
1441 if (priv->frames_count) {
1442 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1443 priv->frames_count);
1444 priv->frames_count = 0;
1445 }
1446}
1447
1448static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
1449{
1450 struct iwl_frame *frame;
1451 struct list_head *element;
1452 if (list_empty(&priv->free_frames)) {
1453 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1454 if (!frame) {
1455 IWL_ERROR("Could not allocate frame!\n");
1456 return NULL;
1457 }
1458
1459 priv->frames_count++;
1460 return frame;
1461 }
1462
1463 element = priv->free_frames.next;
1464 list_del(element);
1465 return list_entry(element, struct iwl_frame, list);
1466}
1467
1468static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
1469{
1470 memset(frame, 0, sizeof(*frame));
1471 list_add(&frame->list, &priv->free_frames);
1472}
1473
1474unsigned int iwl_fill_beacon_frame(struct iwl_priv *priv,
1475 struct ieee80211_hdr *hdr,
1476 const u8 *dest, int left)
1477{
1478
1479 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
1480 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1481 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1482 return 0;
1483
1484 if (priv->ibss_beacon->len > left)
1485 return 0;
1486
1487 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1488
1489 return priv->ibss_beacon->len;
1490}
1491
1492int iwl_rate_index_from_plcp(int plcp)
1493{
1494 int i = 0;
1495
1496 if (plcp & RATE_MCS_HT_MSK) {
1497 i = (plcp & 0xff);
1498
1499 if (i >= IWL_RATE_MIMO_6M_PLCP)
1500 i = i - IWL_RATE_MIMO_6M_PLCP;
1501
1502 i += IWL_FIRST_OFDM_RATE;
1503 /* skip 9M not supported in ht*/
1504 if (i >= IWL_RATE_9M_INDEX)
1505 i += 1;
1506 if ((i >= IWL_FIRST_OFDM_RATE) &&
1507 (i <= IWL_LAST_OFDM_RATE))
1508 return i;
1509 } else {
1510 for (i = 0; i < ARRAY_SIZE(iwl_rates); i++)
1511 if (iwl_rates[i].plcp == (plcp &0xFF))
1512 return i;
1513 }
1514 return -1;
1515}
1516
1517static u8 iwl_rate_get_lowest_plcp(int rate_mask)
1518{
1519 u8 i;
1520
1521 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
1522 i = iwl_rates[i].next_ieee) {
1523 if (rate_mask & (1 << i))
1524 return iwl_rates[i].plcp;
1525 }
1526
1527 return IWL_RATE_INVALID;
1528}
1529
1530static int iwl_send_beacon_cmd(struct iwl_priv *priv)
1531{
1532 struct iwl_frame *frame;
1533 unsigned int frame_size;
1534 int rc;
1535 u8 rate;
1536
1537 frame = iwl_get_free_frame(priv);
1538
1539 if (!frame) {
1540 IWL_ERROR("Could not obtain free frame buffer for beacon "
1541 "command.\n");
1542 return -ENOMEM;
1543 }
1544
1545 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
1546 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic &
1547 0xFF0);
1548 if (rate == IWL_INVALID_RATE)
1549 rate = IWL_RATE_6M_PLCP;
1550 } else {
1551 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
1552 if (rate == IWL_INVALID_RATE)
1553 rate = IWL_RATE_1M_PLCP;
1554 }
1555
1556 frame_size = iwl_hw_get_beacon_cmd(priv, frame, rate);
1557
1558 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
1559 &frame->u.cmd[0]);
1560
1561 iwl_free_frame(priv, frame);
1562
1563 return rc;
1564}
1565
1566/******************************************************************************
1567 *
1568 * EEPROM related functions
1569 *
1570 ******************************************************************************/
1571
1572static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
1573{
1574 memcpy(mac, priv->eeprom.mac_address, 6);
1575}
1576
1577/**
1578 * iwl_eeprom_init - read EEPROM contents
1579 *
1580 * Load the EEPROM from adapter into priv->eeprom
1581 *
1582 * NOTE: This routine uses the non-debug IO access functions.
1583 */
1584int iwl_eeprom_init(struct iwl_priv *priv)
1585{
1586 u16 *e = (u16 *)&priv->eeprom;
1587 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
1588 u32 r;
1589 int sz = sizeof(priv->eeprom);
1590 int rc;
1591 int i;
1592 u16 addr;
1593
1594 /* The EEPROM structure has several padding buffers within it
1595 * and when adding new EEPROM maps is subject to programmer errors
1596 * which may be very difficult to identify without explicitly
1597 * checking the resulting size of the eeprom map. */
1598 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1599
1600 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1601 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1602 return -ENOENT;
1603 }
1604
1605 rc = iwl_eeprom_aqcuire_semaphore(priv);
1606 if (rc < 0) {
1607 IWL_ERROR("Failed to aqcuire EEPROM semaphore.\n");
1608 return -ENOENT;
1609 }
1610
1611 /* eeprom is an array of 16bit values */
1612 for (addr = 0; addr < sz; addr += sizeof(u16)) {
1613 _iwl_write32(priv, CSR_EEPROM_REG, addr << 1);
1614 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1615
1616 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1617 i += IWL_EEPROM_ACCESS_DELAY) {
1618 r = _iwl_read_restricted(priv, CSR_EEPROM_REG);
1619 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1620 break;
1621 udelay(IWL_EEPROM_ACCESS_DELAY);
1622 }
1623
1624 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1625 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1626 rc = -ETIMEDOUT;
1627 goto done;
1628 }
1629 e[addr / 2] = le16_to_cpu(r >> 16);
1630 }
1631 rc = 0;
1632
1633done:
1634 iwl_eeprom_release_semaphore(priv);
1635 return rc;
1636}
1637
1638/******************************************************************************
1639 *
1640 * Misc. internal state and helper functions
1641 *
1642 ******************************************************************************/
1643#ifdef CONFIG_IWLWIFI_DEBUG
1644
1645/**
1646 * iwl_report_frame - dump frame to syslog during debug sessions
1647 *
1648 * hack this function to show different aspects of received frames,
1649 * including selective frame dumps.
1650 * group100 parameter selects whether to show 1 out of 100 good frames.
1651 *
1652 * TODO: ieee80211_hdr stuff is common to 3945 and 4965, so frame type
1653 * info output is okay, but some of this stuff (e.g. iwl_rx_frame_stats)
1654 * is 3945-specific and gives bad output for 4965. Need to split the
1655 * functionality, keep common stuff here.
1656 */
1657void iwl_report_frame(struct iwl_priv *priv,
1658 struct iwl_rx_packet *pkt,
1659 struct ieee80211_hdr *header, int group100)
1660{
1661 u32 to_us;
1662 u32 print_summary = 0;
1663 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1664 u32 hundred = 0;
1665 u32 dataframe = 0;
1666 u16 fc;
1667 u16 seq_ctl;
1668 u16 channel;
1669 u16 phy_flags;
1670 int rate_sym;
1671 u16 length;
1672 u16 status;
1673 u16 bcn_tmr;
1674 u32 tsf_low;
1675 u64 tsf;
1676 u8 rssi;
1677 u8 agc;
1678 u16 sig_avg;
1679 u16 noise_diff;
1680 struct iwl_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1681 struct iwl_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1682 struct iwl_rx_frame_end *rx_end = IWL_RX_END(pkt);
1683 u8 *data = IWL_RX_DATA(pkt);
1684
1685 /* MAC header */
1686 fc = le16_to_cpu(header->frame_control);
1687 seq_ctl = le16_to_cpu(header->seq_ctrl);
1688
1689 /* metadata */
1690 channel = le16_to_cpu(rx_hdr->channel);
1691 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1692 rate_sym = rx_hdr->rate;
1693 length = le16_to_cpu(rx_hdr->len);
1694
1695 /* end-of-frame status and timestamp */
1696 status = le32_to_cpu(rx_end->status);
1697 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1698 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1699 tsf = le64_to_cpu(rx_end->timestamp);
1700
1701 /* signal statistics */
1702 rssi = rx_stats->rssi;
1703 agc = rx_stats->agc;
1704 sig_avg = le16_to_cpu(rx_stats->sig_avg);
1705 noise_diff = le16_to_cpu(rx_stats->noise_diff);
1706
1707 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1708
1709 /* if data frame is to us and all is good,
1710 * (optionally) print summary for only 1 out of every 100 */
1711 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1712 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1713 dataframe = 1;
1714 if (!group100)
1715 print_summary = 1; /* print each frame */
1716 else if (priv->framecnt_to_us < 100) {
1717 priv->framecnt_to_us++;
1718 print_summary = 0;
1719 } else {
1720 priv->framecnt_to_us = 0;
1721 print_summary = 1;
1722 hundred = 1;
1723 }
1724 } else {
1725 /* print summary for all other frames */
1726 print_summary = 1;
1727 }
1728
1729 if (print_summary) {
1730 char *title;
1731 u32 rate;
1732
1733 if (hundred)
1734 title = "100Frames";
1735 else if (fc & IEEE80211_FCTL_RETRY)
1736 title = "Retry";
1737 else if (ieee80211_is_assoc_response(fc))
1738 title = "AscRsp";
1739 else if (ieee80211_is_reassoc_response(fc))
1740 title = "RasRsp";
1741 else if (ieee80211_is_probe_response(fc)) {
1742 title = "PrbRsp";
1743 print_dump = 1; /* dump frame contents */
1744 } else if (ieee80211_is_beacon(fc)) {
1745 title = "Beacon";
1746 print_dump = 1; /* dump frame contents */
1747 } else if (ieee80211_is_atim(fc))
1748 title = "ATIM";
1749 else if (ieee80211_is_auth(fc))
1750 title = "Auth";
1751 else if (ieee80211_is_deauth(fc))
1752 title = "DeAuth";
1753 else if (ieee80211_is_disassoc(fc))
1754 title = "DisAssoc";
1755 else
1756 title = "Frame";
1757
1758 rate = iwl_rate_index_from_plcp(rate_sym);
1759 if (rate == -1)
1760 rate = 0;
1761 else
1762 rate = iwl_rates[rate].ieee / 2;
1763
1764 /* print frame summary.
1765 * MAC addresses show just the last byte (for brevity),
1766 * but you can hack it to show more, if you'd like to. */
1767 if (dataframe)
1768 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1769 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1770 title, fc, header->addr1[5],
1771 length, rssi, channel, rate);
1772 else {
1773 /* src/dst addresses assume managed mode */
1774 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1775 "src=0x%02x, rssi=%u, tim=%lu usec, "
1776 "phy=0x%02x, chnl=%d\n",
1777 title, fc, header->addr1[5],
1778 header->addr3[5], rssi,
1779 tsf_low - priv->scan_start_tsf,
1780 phy_flags, channel);
1781 }
1782 }
1783 if (print_dump)
1784 iwl_print_hex_dump(IWL_DL_RX, data, length);
1785}
1786#endif
1787
1788static void iwl_unset_hw_setting(struct iwl_priv *priv)
1789{
1790 if (priv->hw_setting.shared_virt)
1791 pci_free_consistent(priv->pci_dev,
1792 sizeof(struct iwl_shared),
1793 priv->hw_setting.shared_virt,
1794 priv->hw_setting.shared_phys);
1795}
1796
1797/**
1798 * iwl_supported_rate_to_ie - fill in the supported rate in IE field
1799 *
1800 * return : set the bit for each supported rate insert in ie
1801 */
1802static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
1803 u16 basic_rate, int max_count)
1804{
1805 u16 ret_rates = 0, bit;
1806 int i;
1807 u8 *rates;
1808
1809 rates = &(ie[1]);
1810
1811 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1812 if (bit & supported_rate) {
1813 ret_rates |= bit;
1814 rates[*ie] = iwl_rates[i].ieee |
1815 ((bit & basic_rate) ? 0x80 : 0x00);
1816 *ie = *ie + 1;
1817 if (*ie >= max_count)
1818 break;
1819 }
1820 }
1821
1822 return ret_rates;
1823}
1824
1825#ifdef CONFIG_IWLWIFI_HT
1826void static iwl_set_ht_capab(struct ieee80211_hw *hw,
1827 struct ieee80211_ht_capability *ht_cap,
1828 u8 use_wide_chan);
1829#endif
1830
1831/**
1832 * iwl_fill_probe_req - fill in all required fields and IE for probe request
1833 */
1834static u16 iwl_fill_probe_req(struct iwl_priv *priv,
1835 struct ieee80211_mgmt *frame,
1836 int left, int is_direct)
1837{
1838 int len = 0;
1839 u8 *pos = NULL;
1840 u16 ret_rates;
1841
1842 /* Make sure there is enough space for the probe request,
1843 * two mandatory IEs and the data */
1844 left -= 24;
1845 if (left < 0)
1846 return 0;
1847 len += 24;
1848
1849 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1850 memcpy(frame->da, BROADCAST_ADDR, ETH_ALEN);
1851 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
1852 memcpy(frame->bssid, BROADCAST_ADDR, ETH_ALEN);
1853 frame->seq_ctrl = 0;
1854
1855 /* fill in our indirect SSID IE */
1856 /* ...next IE... */
1857
1858 left -= 2;
1859 if (left < 0)
1860 return 0;
1861 len += 2;
1862 pos = &(frame->u.probe_req.variable[0]);
1863 *pos++ = WLAN_EID_SSID;
1864 *pos++ = 0;
1865
1866 /* fill in our direct SSID IE... */
1867 if (is_direct) {
1868 /* ...next IE... */
1869 left -= 2 + priv->essid_len;
1870 if (left < 0)
1871 return 0;
1872 /* ... fill it in... */
1873 *pos++ = WLAN_EID_SSID;
1874 *pos++ = priv->essid_len;
1875 memcpy(pos, priv->essid, priv->essid_len);
1876 pos += priv->essid_len;
1877 len += 2 + priv->essid_len;
1878 }
1879
1880 /* fill in supported rate */
1881 /* ...next IE... */
1882 left -= 2;
1883 if (left < 0)
1884 return 0;
1885 /* ... fill it in... */
1886 *pos++ = WLAN_EID_SUPP_RATES;
1887 *pos = 0;
1888 ret_rates = priv->active_rate = priv->rates_mask;
1889 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1890
1891 iwl_supported_rate_to_ie(pos, priv->active_rate,
1892 priv->active_rate_basic, left);
1893 len += 2 + *pos;
1894 pos += (*pos) + 1;
1895 ret_rates = ~ret_rates & priv->active_rate;
1896
1897 if (ret_rates == 0)
1898 goto fill_end;
1899
1900 /* fill in supported extended rate */
1901 /* ...next IE... */
1902 left -= 2;
1903 if (left < 0)
1904 return 0;
1905 /* ... fill it in... */
1906 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1907 *pos = 0;
1908 iwl_supported_rate_to_ie(pos, ret_rates, priv->active_rate_basic, left);
1909 if (*pos > 0)
1910 len += 2 + *pos;
1911
1912#ifdef CONFIG_IWLWIFI_HT
1913 if (is_direct && priv->is_ht_enabled) {
1914 u8 use_wide_chan = 1;
1915
1916 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
1917 use_wide_chan = 0;
1918 pos += (*pos) + 1;
1919 *pos++ = WLAN_EID_HT_CAPABILITY;
1920 *pos++ = sizeof(struct ieee80211_ht_capability);
1921 iwl_set_ht_capab(NULL, (struct ieee80211_ht_capability *)pos,
1922 use_wide_chan);
1923 len += 2 + sizeof(struct ieee80211_ht_capability);
1924 }
1925#endif /*CONFIG_IWLWIFI_HT */
1926
1927 fill_end:
1928 return (u16)len;
1929}
1930
1931/*
1932 * QoS support
1933*/
1934#ifdef CONFIG_IWLWIFI_QOS
1935static int iwl_send_qos_params_command(struct iwl_priv *priv,
1936 struct iwl_qosparam_cmd *qos)
1937{
1938
1939 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1940 sizeof(struct iwl_qosparam_cmd), qos);
1941}
1942
1943static void iwl_reset_qos(struct iwl_priv *priv)
1944{
1945 u16 cw_min = 15;
1946 u16 cw_max = 1023;
1947 u8 aifs = 2;
1948 u8 is_legacy = 0;
1949 unsigned long flags;
1950 int i;
1951
1952 spin_lock_irqsave(&priv->lock, flags);
1953 priv->qos_data.qos_active = 0;
1954
1955 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1956 if (priv->qos_data.qos_enable)
1957 priv->qos_data.qos_active = 1;
1958 if (!(priv->active_rate & 0xfff0)) {
1959 cw_min = 31;
1960 is_legacy = 1;
1961 }
1962 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1963 if (priv->qos_data.qos_enable)
1964 priv->qos_data.qos_active = 1;
1965 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1966 cw_min = 31;
1967 is_legacy = 1;
1968 }
1969
1970 if (priv->qos_data.qos_active)
1971 aifs = 3;
1972
1973 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1974 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1975 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1976 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1977 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1978
1979 if (priv->qos_data.qos_active) {
1980 i = 1;
1981 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1982 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1983 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1984 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1985 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1986
1987 i = 2;
1988 priv->qos_data.def_qos_parm.ac[i].cw_min =
1989 cpu_to_le16((cw_min + 1) / 2 - 1);
1990 priv->qos_data.def_qos_parm.ac[i].cw_max =
1991 cpu_to_le16(cw_max);
1992 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1993 if (is_legacy)
1994 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1995 cpu_to_le16(6016);
1996 else
1997 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1998 cpu_to_le16(3008);
1999 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2000
2001 i = 3;
2002 priv->qos_data.def_qos_parm.ac[i].cw_min =
2003 cpu_to_le16((cw_min + 1) / 4 - 1);
2004 priv->qos_data.def_qos_parm.ac[i].cw_max =
2005 cpu_to_le16((cw_max + 1) / 2 - 1);
2006 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2007 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2008 if (is_legacy)
2009 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2010 cpu_to_le16(3264);
2011 else
2012 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2013 cpu_to_le16(1504);
2014 } else {
2015 for (i = 1; i < 4; i++) {
2016 priv->qos_data.def_qos_parm.ac[i].cw_min =
2017 cpu_to_le16(cw_min);
2018 priv->qos_data.def_qos_parm.ac[i].cw_max =
2019 cpu_to_le16(cw_max);
2020 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
2021 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2022 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2023 }
2024 }
2025 IWL_DEBUG_QOS("set QoS to default \n");
2026
2027 spin_unlock_irqrestore(&priv->lock, flags);
2028}
2029
2030static void iwl_activate_qos(struct iwl_priv *priv, u8 force)
2031{
2032 unsigned long flags;
2033
2034 if (priv == NULL)
2035 return;
2036
2037 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2038 return;
2039
2040 if (!priv->qos_data.qos_enable)
2041 return;
2042
2043 spin_lock_irqsave(&priv->lock, flags);
2044 priv->qos_data.def_qos_parm.qos_flags = 0;
2045
2046 if (priv->qos_data.qos_cap.q_AP.queue_request &&
2047 !priv->qos_data.qos_cap.q_AP.txop_request)
2048 priv->qos_data.def_qos_parm.qos_flags |=
2049 QOS_PARAM_FLG_TXOP_TYPE_MSK;
2050
2051 if (priv->qos_data.qos_active)
2052 priv->qos_data.def_qos_parm.qos_flags |=
2053 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2054
2055 spin_unlock_irqrestore(&priv->lock, flags);
2056
2057 if (force || iwl_is_associated(priv)) {
2058 IWL_DEBUG_QOS("send QoS cmd with Qos active %d \n",
2059 priv->qos_data.qos_active);
2060
2061 iwl_send_qos_params_command(priv,
2062 &(priv->qos_data.def_qos_parm));
2063 }
2064}
2065
2066#endif /* CONFIG_IWLWIFI_QOS */
2067/*
2068 * Power management (not Tx power!) functions
2069 */
2070#define MSEC_TO_USEC 1024
2071
2072#define NOSLP __constant_cpu_to_le16(0), 0, 0
2073#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
2074#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2075#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2076 __constant_cpu_to_le32(X1), \
2077 __constant_cpu_to_le32(X2), \
2078 __constant_cpu_to_le32(X3), \
2079 __constant_cpu_to_le32(X4)}
2080
2081
2082/* default power management (not Tx power) table values */
2083/* for tim 0-10 */
2084static struct iwl_power_vec_entry range_0[IWL_POWER_AC] = {
2085 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2086 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2087 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2088 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2089 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2090 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2091};
2092
2093/* for tim > 10 */
2094static struct iwl_power_vec_entry range_1[IWL_POWER_AC] = {
2095 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2096 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2097 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2098 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2099 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2100 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2101 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2102 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2103 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2104 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2105};
2106
2107int iwl_power_init_handle(struct iwl_priv *priv)
2108{
2109 int rc = 0, i;
2110 struct iwl_power_mgr *pow_data;
2111 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
2112 u16 pci_pm;
2113
2114 IWL_DEBUG_POWER("Initialize power \n");
2115
2116 pow_data = &(priv->power_data);
2117
2118 memset(pow_data, 0, sizeof(*pow_data));
2119
2120 pow_data->active_index = IWL_POWER_RANGE_0;
2121 pow_data->dtim_val = 0xffff;
2122
2123 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2124 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2125
2126 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2127 if (rc != 0)
2128 return 0;
2129 else {
2130 struct iwl_powertable_cmd *cmd;
2131
2132 IWL_DEBUG_POWER("adjust power command flags\n");
2133
2134 for (i = 0; i < IWL_POWER_AC; i++) {
2135 cmd = &pow_data->pwr_range_0[i].cmd;
2136
2137 if (pci_pm & 0x1)
2138 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2139 else
2140 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2141 }
2142 }
2143 return rc;
2144}
2145
2146static int iwl_update_power_cmd(struct iwl_priv *priv,
2147 struct iwl_powertable_cmd *cmd, u32 mode)
2148{
2149 int rc = 0, i;
2150 u8 skip;
2151 u32 max_sleep = 0;
2152 struct iwl_power_vec_entry *range;
2153 u8 period = 0;
2154 struct iwl_power_mgr *pow_data;
2155
2156 if (mode > IWL_POWER_INDEX_5) {
2157 IWL_DEBUG_POWER("Error invalid power mode \n");
2158 return -1;
2159 }
2160 pow_data = &(priv->power_data);
2161
2162 if (pow_data->active_index == IWL_POWER_RANGE_0)
2163 range = &pow_data->pwr_range_0[0];
2164 else
2165 range = &pow_data->pwr_range_1[1];
2166
2167 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
2168
2169#ifdef IWL_MAC80211_DISABLE
2170 if (priv->assoc_network != NULL) {
2171 unsigned long flags;
2172
2173 period = priv->assoc_network->tim.tim_period;
2174 }
2175#endif /*IWL_MAC80211_DISABLE */
2176 skip = range[mode].no_dtim;
2177
2178 if (period == 0) {
2179 period = 1;
2180 skip = 0;
2181 }
2182
2183 if (skip == 0) {
2184 max_sleep = period;
2185 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2186 } else {
2187 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2188 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2189 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2190 }
2191
2192 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2193 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2194 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2195 }
2196
2197 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2198 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2199 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2200 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2201 le32_to_cpu(cmd->sleep_interval[0]),
2202 le32_to_cpu(cmd->sleep_interval[1]),
2203 le32_to_cpu(cmd->sleep_interval[2]),
2204 le32_to_cpu(cmd->sleep_interval[3]),
2205 le32_to_cpu(cmd->sleep_interval[4]));
2206
2207 return rc;
2208}
2209
2210static int iwl_send_power_mode(struct iwl_priv *priv, u32 mode)
2211{
2212 u32 final_mode = mode;
2213 int rc;
2214 struct iwl_powertable_cmd cmd;
2215
2216 /* If on battery, set to 3,
2217 * if plugged into AC power, set to CAM ("continuosly aware mode"),
2218 * else user level */
2219 switch (mode) {
2220 case IWL_POWER_BATTERY:
2221 final_mode = IWL_POWER_INDEX_3;
2222 break;
2223 case IWL_POWER_AC:
2224 final_mode = IWL_POWER_MODE_CAM;
2225 break;
2226 default:
2227 final_mode = mode;
2228 break;
2229 }
2230
2231 cmd.keep_alive_beacons = 0;
2232
2233 iwl_update_power_cmd(priv, &cmd, final_mode);
2234
2235 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2236
2237 if (final_mode == IWL_POWER_MODE_CAM)
2238 clear_bit(STATUS_POWER_PMI, &priv->status);
2239 else
2240 set_bit(STATUS_POWER_PMI, &priv->status);
2241
2242 return rc;
2243}
2244
2245int iwl_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
2246{
2247 /* Filter incoming packets to determine if they are targeted toward
2248 * this network, discarding packets coming from ourselves */
2249 switch (priv->iw_mode) {
2250 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2251 /* packets from our adapter are dropped (echo) */
2252 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2253 return 0;
2254 /* {broad,multi}cast packets to our IBSS go through */
2255 if (is_multicast_ether_addr(header->addr1))
2256 return !compare_ether_addr(header->addr3, priv->bssid);
2257 /* packets to our adapter go through */
2258 return !compare_ether_addr(header->addr1, priv->mac_addr);
2259 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2260 /* packets from our adapter are dropped (echo) */
2261 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2262 return 0;
2263 /* {broad,multi}cast packets to our BSS go through */
2264 if (is_multicast_ether_addr(header->addr1))
2265 return !compare_ether_addr(header->addr2, priv->bssid);
2266 /* packets to our adapter go through */
2267 return !compare_ether_addr(header->addr1, priv->mac_addr);
2268 }
2269
2270 return 1;
2271}
2272
2273#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2274
2275const char *iwl_get_tx_fail_reason(u32 status)
2276{
2277 switch (status & TX_STATUS_MSK) {
2278 case TX_STATUS_SUCCESS:
2279 return "SUCCESS";
2280 TX_STATUS_ENTRY(SHORT_LIMIT);
2281 TX_STATUS_ENTRY(LONG_LIMIT);
2282 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2283 TX_STATUS_ENTRY(MGMNT_ABORT);
2284 TX_STATUS_ENTRY(NEXT_FRAG);
2285 TX_STATUS_ENTRY(LIFE_EXPIRE);
2286 TX_STATUS_ENTRY(DEST_PS);
2287 TX_STATUS_ENTRY(ABORTED);
2288 TX_STATUS_ENTRY(BT_RETRY);
2289 TX_STATUS_ENTRY(STA_INVALID);
2290 TX_STATUS_ENTRY(FRAG_DROPPED);
2291 TX_STATUS_ENTRY(TID_DISABLE);
2292 TX_STATUS_ENTRY(FRAME_FLUSHED);
2293 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2294 TX_STATUS_ENTRY(TX_LOCKED);
2295 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2296 }
2297
2298 return "UNKNOWN";
2299}
2300
2301/**
2302 * iwl_scan_cancel - Cancel any currently executing HW scan
2303 *
2304 * NOTE: priv->mutex is not required before calling this function
2305 */
2306static int iwl_scan_cancel(struct iwl_priv *priv)
2307{
2308 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2309 clear_bit(STATUS_SCANNING, &priv->status);
2310 return 0;
2311 }
2312
2313 if (test_bit(STATUS_SCANNING, &priv->status)) {
2314 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2315 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2316 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2317 queue_work(priv->workqueue, &priv->abort_scan);
2318
2319 } else
2320 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2321
2322 return test_bit(STATUS_SCANNING, &priv->status);
2323 }
2324
2325 return 0;
2326}
2327
2328/**
2329 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
2330 * @ms: amount of time to wait (in milliseconds) for scan to abort
2331 *
2332 * NOTE: priv->mutex must be held before calling this function
2333 */
2334static int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
2335{
2336 unsigned long now = jiffies;
2337 int ret;
2338
2339 ret = iwl_scan_cancel(priv);
2340 if (ret && ms) {
2341 mutex_unlock(&priv->mutex);
2342 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2343 test_bit(STATUS_SCANNING, &priv->status))
2344 msleep(1);
2345 mutex_lock(&priv->mutex);
2346
2347 return test_bit(STATUS_SCANNING, &priv->status);
2348 }
2349
2350 return ret;
2351}
2352
2353static void iwl_sequence_reset(struct iwl_priv *priv)
2354{
2355 /* Reset ieee stats */
2356
2357 /* We don't reset the net_device_stats (ieee->stats) on
2358 * re-association */
2359
2360 priv->last_seq_num = -1;
2361 priv->last_frag_num = -1;
2362 priv->last_packet_time = 0;
2363
2364 iwl_scan_cancel(priv);
2365}
2366
2367#define MAX_UCODE_BEACON_INTERVAL 4096
2368#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2369
2370static __le16 iwl_adjust_beacon_interval(u16 beacon_val)
2371{
2372 u16 new_val = 0;
2373 u16 beacon_factor = 0;
2374
2375 beacon_factor =
2376 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2377 / MAX_UCODE_BEACON_INTERVAL;
2378 new_val = beacon_val / beacon_factor;
2379
2380 return cpu_to_le16(new_val);
2381}
2382
2383static void iwl_setup_rxon_timing(struct iwl_priv *priv)
2384{
2385 u64 interval_tm_unit;
2386 u64 tsf, result;
2387 unsigned long flags;
2388 struct ieee80211_conf *conf = NULL;
2389 u16 beacon_int = 0;
2390
2391 conf = ieee80211_get_hw_conf(priv->hw);
2392
2393 spin_lock_irqsave(&priv->lock, flags);
2394 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2395 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2396
2397 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2398
2399 tsf = priv->timestamp1;
2400 tsf = ((tsf << 32) | priv->timestamp0);
2401
2402 beacon_int = priv->beacon_int;
2403 spin_unlock_irqrestore(&priv->lock, flags);
2404
2405 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2406 if (beacon_int == 0) {
2407 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2408 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2409 } else {
2410 priv->rxon_timing.beacon_interval =
2411 cpu_to_le16(beacon_int);
2412 priv->rxon_timing.beacon_interval =
2413 iwl_adjust_beacon_interval(
2414 le16_to_cpu(priv->rxon_timing.beacon_interval));
2415 }
2416
2417 priv->rxon_timing.atim_window = 0;
2418 } else {
2419 priv->rxon_timing.beacon_interval =
2420 iwl_adjust_beacon_interval(conf->beacon_int);
2421 /* TODO: we need to get atim_window from upper stack
2422 * for now we set to 0 */
2423 priv->rxon_timing.atim_window = 0;
2424 }
2425
2426 interval_tm_unit =
2427 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2428 result = do_div(tsf, interval_tm_unit);
2429 priv->rxon_timing.beacon_init_val =
2430 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2431
2432 IWL_DEBUG_ASSOC
2433 ("beacon interval %d beacon timer %d beacon tim %d\n",
2434 le16_to_cpu(priv->rxon_timing.beacon_interval),
2435 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2436 le16_to_cpu(priv->rxon_timing.atim_window));
2437}
2438
2439static int iwl_scan_initiate(struct iwl_priv *priv)
2440{
2441 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2442 IWL_ERROR("APs don't scan.\n");
2443 return 0;
2444 }
2445
2446 if (!iwl_is_ready_rf(priv)) {
2447 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2448 return -EIO;
2449 }
2450
2451 if (test_bit(STATUS_SCANNING, &priv->status)) {
2452 IWL_DEBUG_SCAN("Scan already in progress.\n");
2453 return -EAGAIN;
2454 }
2455
2456 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2457 IWL_DEBUG_SCAN("Scan request while abort pending. "
2458 "Queuing.\n");
2459 return -EAGAIN;
2460 }
2461
2462 IWL_DEBUG_INFO("Starting scan...\n");
2463 priv->scan_bands = 2;
2464 set_bit(STATUS_SCANNING, &priv->status);
2465 priv->scan_start = jiffies;
2466 priv->scan_pass_start = priv->scan_start;
2467
2468 queue_work(priv->workqueue, &priv->request_scan);
2469
2470 return 0;
2471}
2472
2473static int iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
2474{
2475 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
2476
2477 if (hw_decrypt)
2478 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2479 else
2480 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2481
2482 return 0;
2483}
2484
2485static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode)
2486{
2487 if (phymode == MODE_IEEE80211A) {
2488 priv->staging_rxon.flags &=
2489 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2490 | RXON_FLG_CCK_MSK);
2491 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2492 } else {
2493 /* Copied from iwl_bg_post_associate() */
2494 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2495 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2496 else
2497 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2498
2499 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2500 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2501
2502 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2503 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2504 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2505 }
2506}
2507
2508/*
2509 * initilize rxon structure with default values fromm eeprom
2510 */
2511static void iwl_connection_init_rx_config(struct iwl_priv *priv)
2512{
2513 const struct iwl_channel_info *ch_info;
2514
2515 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2516
2517 switch (priv->iw_mode) {
2518 case IEEE80211_IF_TYPE_AP:
2519 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2520 break;
2521
2522 case IEEE80211_IF_TYPE_STA:
2523 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2524 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2525 break;
2526
2527 case IEEE80211_IF_TYPE_IBSS:
2528 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2529 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2530 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2531 RXON_FILTER_ACCEPT_GRP_MSK;
2532 break;
2533
2534 case IEEE80211_IF_TYPE_MNTR:
2535 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2536 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2537 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2538 break;
2539 }
2540
2541#if 0
2542 /* TODO: Figure out when short_preamble would be set and cache from
2543 * that */
2544 if (!hw_to_local(priv->hw)->short_preamble)
2545 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2546 else
2547 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2548#endif
2549
2550 ch_info = iwl_get_channel_info(priv, priv->phymode,
2551 le16_to_cpu(priv->staging_rxon.channel));
2552
2553 if (!ch_info)
2554 ch_info = &priv->channel_info[0];
2555
2556 /*
2557 * in some case A channels are all non IBSS
2558 * in this case force B/G channel
2559 */
2560 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2561 !(is_channel_ibss(ch_info)))
2562 ch_info = &priv->channel_info[0];
2563
2564 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2565 if (is_channel_a_band(ch_info))
2566 priv->phymode = MODE_IEEE80211A;
2567 else
2568 priv->phymode = MODE_IEEE80211G;
2569
2570 iwl_set_flags_for_phymode(priv, priv->phymode);
2571
2572 priv->staging_rxon.ofdm_basic_rates =
2573 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2574 priv->staging_rxon.cck_basic_rates =
2575 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2576
2577 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2578 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2579 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2580 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2581 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2582 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2583 iwl4965_set_rxon_chain(priv);
2584}
2585
2586static int iwl_set_mode(struct iwl_priv *priv, int mode)
2587{
2588 if (!iwl_is_ready_rf(priv))
2589 return -EAGAIN;
2590
2591 if (mode == IEEE80211_IF_TYPE_IBSS) {
2592 const struct iwl_channel_info *ch_info;
2593
2594 ch_info = iwl_get_channel_info(priv,
2595 priv->phymode,
2596 le16_to_cpu(priv->staging_rxon.channel));
2597
2598 if (!ch_info || !is_channel_ibss(ch_info)) {
2599 IWL_ERROR("channel %d not IBSS channel\n",
2600 le16_to_cpu(priv->staging_rxon.channel));
2601 return -EINVAL;
2602 }
2603 }
2604
2605 cancel_delayed_work(&priv->scan_check);
2606 if (iwl_scan_cancel_timeout(priv, 100)) {
2607 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2608 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2609 return -EAGAIN;
2610 }
2611
2612 priv->iw_mode = mode;
2613
2614 iwl_connection_init_rx_config(priv);
2615 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2616
2617 iwl_clear_stations_table(priv);
2618
2619 iwl_commit_rxon(priv);
2620
2621 return 0;
2622}
2623
2624static void iwl_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
2625 struct ieee80211_tx_control *ctl,
2626 struct iwl_cmd *cmd,
2627 struct sk_buff *skb_frag,
2628 int last_frag)
2629{
2630 struct iwl_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2631
2632 switch (keyinfo->alg) {
2633 case ALG_CCMP:
2634 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2635 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2636 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2637 break;
2638
2639 case ALG_TKIP:
2640#if 0
2641 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2642
2643 if (last_frag)
2644 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2645 8);
2646 else
2647 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2648#endif
2649 break;
2650
2651 case ALG_WEP:
2652 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2653 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2654
2655 if (keyinfo->keylen == 13)
2656 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2657
2658 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2659
2660 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2661 "with key %d\n", ctl->key_idx);
2662 break;
2663
Zhu Yib481de92007-09-25 17:54:57 -07002664 default:
2665 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2666 break;
2667 }
2668}
2669
2670/*
2671 * handle build REPLY_TX command notification.
2672 */
2673static void iwl_build_tx_cmd_basic(struct iwl_priv *priv,
2674 struct iwl_cmd *cmd,
2675 struct ieee80211_tx_control *ctrl,
2676 struct ieee80211_hdr *hdr,
2677 int is_unicast, u8 std_id)
2678{
2679 __le16 *qc;
2680 u16 fc = le16_to_cpu(hdr->frame_control);
2681 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2682
2683 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2684 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2685 tx_flags |= TX_CMD_FLG_ACK_MSK;
2686 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2687 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2688 if (ieee80211_is_probe_response(fc) &&
2689 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2690 tx_flags |= TX_CMD_FLG_TSF_MSK;
2691 } else {
2692 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2693 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2694 }
2695
2696 cmd->cmd.tx.sta_id = std_id;
2697 if (ieee80211_get_morefrag(hdr))
2698 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2699
2700 qc = ieee80211_get_qos_ctrl(hdr);
2701 if (qc) {
2702 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2703 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2704 } else
2705 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2706
2707 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2708 tx_flags |= TX_CMD_FLG_RTS_MSK;
2709 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2710 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2711 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2712 tx_flags |= TX_CMD_FLG_CTS_MSK;
2713 }
2714
2715 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2716 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2717
2718 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2719 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2720 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2721 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
2722 cmd->cmd.tx.timeout.pm_frame_timeout =
2723 cpu_to_le16(3);
2724 else
2725 cmd->cmd.tx.timeout.pm_frame_timeout =
2726 cpu_to_le16(2);
2727 } else
2728 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2729
2730 cmd->cmd.tx.driver_txop = 0;
2731 cmd->cmd.tx.tx_flags = tx_flags;
2732 cmd->cmd.tx.next_frame_len = 0;
2733}
2734
2735static int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
2736{
2737 int sta_id;
2738 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002739 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002740
2741 /* If this frame is broadcast or not data then use the broadcast
2742 * station id */
2743 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2744 is_multicast_ether_addr(hdr->addr1))
2745 return priv->hw_setting.bcast_sta_id;
2746
2747 switch (priv->iw_mode) {
2748
2749 /* If this frame is part of a BSS network (we're a station), then
2750 * we use the AP's station id */
2751 case IEEE80211_IF_TYPE_STA:
2752 return IWL_AP_ID;
2753
2754 /* If we are an AP, then find the station, or use BCAST */
2755 case IEEE80211_IF_TYPE_AP:
2756 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2757 if (sta_id != IWL_INVALID_STATION)
2758 return sta_id;
2759 return priv->hw_setting.bcast_sta_id;
2760
2761 /* If this frame is part of a IBSS network, then we use the
2762 * target specific station id */
2763 case IEEE80211_IF_TYPE_IBSS:
2764 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2765 if (sta_id != IWL_INVALID_STATION)
2766 return sta_id;
2767
2768 sta_id = iwl_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
2769
2770 if (sta_id != IWL_INVALID_STATION)
2771 return sta_id;
2772
Joe Perches0795af52007-10-03 17:59:30 -07002773 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002774 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002775 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002776 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2777 return priv->hw_setting.bcast_sta_id;
2778
2779 default:
2780 IWL_WARNING("Unkown mode of operation: %d", priv->iw_mode);
2781 return priv->hw_setting.bcast_sta_id;
2782 }
2783}
2784
2785/*
2786 * start REPLY_TX command process
2787 */
2788static int iwl_tx_skb(struct iwl_priv *priv,
2789 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2790{
2791 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2792 struct iwl_tfd_frame *tfd;
2793 u32 *control_flags;
2794 int txq_id = ctl->queue;
2795 struct iwl_tx_queue *txq = NULL;
2796 struct iwl_queue *q = NULL;
2797 dma_addr_t phys_addr;
2798 dma_addr_t txcmd_phys;
2799 struct iwl_cmd *out_cmd = NULL;
2800 u16 len, idx, len_org;
2801 u8 id, hdr_len, unicast;
2802 u8 sta_id;
2803 u16 seq_number = 0;
2804 u16 fc;
2805 __le16 *qc;
2806 u8 wait_write_ptr = 0;
2807 unsigned long flags;
2808 int rc;
2809
2810 spin_lock_irqsave(&priv->lock, flags);
2811 if (iwl_is_rfkill(priv)) {
2812 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2813 goto drop_unlock;
2814 }
2815
2816 if (!priv->interface_id) {
2817 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2818 goto drop_unlock;
2819 }
2820
2821 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2822 IWL_ERROR("ERROR: No TX rate available.\n");
2823 goto drop_unlock;
2824 }
2825
2826 unicast = !is_multicast_ether_addr(hdr->addr1);
2827 id = 0;
2828
2829 fc = le16_to_cpu(hdr->frame_control);
2830
2831#ifdef CONFIG_IWLWIFI_DEBUG
2832 if (ieee80211_is_auth(fc))
2833 IWL_DEBUG_TX("Sending AUTH frame\n");
2834 else if (ieee80211_is_assoc_request(fc))
2835 IWL_DEBUG_TX("Sending ASSOC frame\n");
2836 else if (ieee80211_is_reassoc_request(fc))
2837 IWL_DEBUG_TX("Sending REASSOC frame\n");
2838#endif
2839
2840 if (!iwl_is_associated(priv) &&
2841 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
2842 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
2843 goto drop_unlock;
2844 }
2845
2846 spin_unlock_irqrestore(&priv->lock, flags);
2847
2848 hdr_len = ieee80211_get_hdrlen(fc);
2849 sta_id = iwl_get_sta_id(priv, hdr);
2850 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002851 DECLARE_MAC_BUF(mac);
2852
2853 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2854 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002855 goto drop;
2856 }
2857
2858 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2859
2860 qc = ieee80211_get_qos_ctrl(hdr);
2861 if (qc) {
2862 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2863 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2864 IEEE80211_SCTL_SEQ;
2865 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2866 (hdr->seq_ctrl &
2867 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2868 seq_number += 0x10;
2869#ifdef CONFIG_IWLWIFI_HT
2870#ifdef CONFIG_IWLWIFI_HT_AGG
2871 /* aggregation is on for this <sta,tid> */
2872 if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
2873 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
2874#endif /* CONFIG_IWLWIFI_HT_AGG */
2875#endif /* CONFIG_IWLWIFI_HT */
2876 }
2877 txq = &priv->txq[txq_id];
2878 q = &txq->q;
2879
2880 spin_lock_irqsave(&priv->lock, flags);
2881
2882 tfd = &txq->bd[q->first_empty];
2883 memset(tfd, 0, sizeof(*tfd));
2884 control_flags = (u32 *) tfd;
2885 idx = get_cmd_index(q, q->first_empty, 0);
2886
2887 memset(&(txq->txb[q->first_empty]), 0, sizeof(struct iwl_tx_info));
2888 txq->txb[q->first_empty].skb[0] = skb;
2889 memcpy(&(txq->txb[q->first_empty].status.control),
2890 ctl, sizeof(struct ieee80211_tx_control));
2891 out_cmd = &txq->cmd[idx];
2892 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2893 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2894 out_cmd->hdr.cmd = REPLY_TX;
2895 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2896 INDEX_TO_SEQ(q->first_empty)));
2897 /* copy frags header */
2898 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2899
2900 /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
2901 len = priv->hw_setting.tx_cmd_len +
2902 sizeof(struct iwl_cmd_header) + hdr_len;
2903
2904 len_org = len;
2905 len = (len + 3) & ~3;
2906
2907 if (len_org != len)
2908 len_org = 1;
2909 else
2910 len_org = 0;
2911
2912 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2913 offsetof(struct iwl_cmd, hdr);
2914
2915 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
2916
2917 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
2918 iwl_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
2919
2920 /* 802.11 null functions have no payload... */
2921 len = skb->len - hdr_len;
2922 if (len) {
2923 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2924 len, PCI_DMA_TODEVICE);
2925 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
2926 }
2927
2928 if (len_org)
2929 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2930
2931 len = (u16)skb->len;
2932 out_cmd->cmd.tx.len = cpu_to_le16(len);
2933
2934 /* TODO need this for burst mode later on */
2935 iwl_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
2936
2937 /* set is_hcca to 0; it probably will never be implemented */
2938 iwl_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
2939
2940 iwl4965_tx_cmd(priv, out_cmd, sta_id, txcmd_phys,
2941 hdr, hdr_len, ctl, NULL);
2942
2943 if (!ieee80211_get_morefrag(hdr)) {
2944 txq->need_update = 1;
2945 if (qc) {
2946 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2947 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2948 }
2949 } else {
2950 wait_write_ptr = 1;
2951 txq->need_update = 0;
2952 }
2953
2954 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
2955 sizeof(out_cmd->cmd.tx));
2956
2957 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
2958 ieee80211_get_hdrlen(fc));
2959
2960 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2961
2962 q->first_empty = iwl_queue_inc_wrap(q->first_empty, q->n_bd);
2963 rc = iwl_tx_queue_update_write_ptr(priv, txq);
2964 spin_unlock_irqrestore(&priv->lock, flags);
2965
2966 if (rc)
2967 return rc;
2968
2969 if ((iwl_queue_space(q) < q->high_mark)
2970 && priv->mac80211_registered) {
2971 if (wait_write_ptr) {
2972 spin_lock_irqsave(&priv->lock, flags);
2973 txq->need_update = 1;
2974 iwl_tx_queue_update_write_ptr(priv, txq);
2975 spin_unlock_irqrestore(&priv->lock, flags);
2976 }
2977
2978 ieee80211_stop_queue(priv->hw, ctl->queue);
2979 }
2980
2981 return 0;
2982
2983drop_unlock:
2984 spin_unlock_irqrestore(&priv->lock, flags);
2985drop:
2986 return -1;
2987}
2988
2989static void iwl_set_rate(struct iwl_priv *priv)
2990{
2991 const struct ieee80211_hw_mode *hw = NULL;
2992 struct ieee80211_rate *rate;
2993 int i;
2994
2995 hw = iwl_get_hw_mode(priv, priv->phymode);
2996
2997 priv->active_rate = 0;
2998 priv->active_rate_basic = 0;
2999
3000 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
3001 hw->mode == MODE_IEEE80211A ?
3002 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
3003
3004 for (i = 0; i < hw->num_rates; i++) {
3005 rate = &(hw->rates[i]);
3006 if ((rate->val < IWL_RATE_COUNT) &&
3007 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
3008 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
3009 rate->val, iwl_rates[rate->val].plcp,
3010 (rate->flags & IEEE80211_RATE_BASIC) ?
3011 "*" : "");
3012 priv->active_rate |= (1 << rate->val);
3013 if (rate->flags & IEEE80211_RATE_BASIC)
3014 priv->active_rate_basic |= (1 << rate->val);
3015 } else
3016 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
3017 rate->val, iwl_rates[rate->val].plcp);
3018 }
3019
3020 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
3021 priv->active_rate, priv->active_rate_basic);
3022
3023 /*
3024 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
3025 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
3026 * OFDM
3027 */
3028 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
3029 priv->staging_rxon.cck_basic_rates =
3030 ((priv->active_rate_basic &
3031 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
3032 else
3033 priv->staging_rxon.cck_basic_rates =
3034 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
3035
3036 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
3037 priv->staging_rxon.ofdm_basic_rates =
3038 ((priv->active_rate_basic &
3039 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
3040 IWL_FIRST_OFDM_RATE) & 0xFF;
3041 else
3042 priv->staging_rxon.ofdm_basic_rates =
3043 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
3044}
3045
3046static void iwl_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
3047{
3048 unsigned long flags;
3049
3050 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
3051 return;
3052
3053 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
3054 disable_radio ? "OFF" : "ON");
3055
3056 if (disable_radio) {
3057 iwl_scan_cancel(priv);
3058 /* FIXME: This is a workaround for AP */
3059 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3060 spin_lock_irqsave(&priv->lock, flags);
3061 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
3062 CSR_UCODE_SW_BIT_RFKILL);
3063 spin_unlock_irqrestore(&priv->lock, flags);
3064 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
3065 set_bit(STATUS_RF_KILL_SW, &priv->status);
3066 }
3067 return;
3068 }
3069
3070 spin_lock_irqsave(&priv->lock, flags);
3071 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3072
3073 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3074 spin_unlock_irqrestore(&priv->lock, flags);
3075
3076 /* wake up ucode */
3077 msleep(10);
3078
3079 spin_lock_irqsave(&priv->lock, flags);
3080 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3081 if (!iwl_grab_restricted_access(priv))
3082 iwl_release_restricted_access(priv);
3083 spin_unlock_irqrestore(&priv->lock, flags);
3084
3085 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3086 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3087 "disabled by HW switch\n");
3088 return;
3089 }
3090
3091 queue_work(priv->workqueue, &priv->restart);
3092 return;
3093}
3094
3095void iwl_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
3096 u32 decrypt_res, struct ieee80211_rx_status *stats)
3097{
3098 u16 fc =
3099 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3100
3101 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3102 return;
3103
3104 if (!(fc & IEEE80211_FCTL_PROTECTED))
3105 return;
3106
3107 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3108 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3109 case RX_RES_STATUS_SEC_TYPE_TKIP:
3110 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3111 RX_RES_STATUS_BAD_ICV_MIC)
3112 stats->flag |= RX_FLAG_MMIC_ERROR;
3113 case RX_RES_STATUS_SEC_TYPE_WEP:
3114 case RX_RES_STATUS_SEC_TYPE_CCMP:
3115 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3116 RX_RES_STATUS_DECRYPT_OK) {
3117 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3118 stats->flag |= RX_FLAG_DECRYPTED;
3119 }
3120 break;
3121
3122 default:
3123 break;
3124 }
3125}
3126
3127void iwl_handle_data_packet_monitor(struct iwl_priv *priv,
3128 struct iwl_rx_mem_buffer *rxb,
3129 void *data, short len,
3130 struct ieee80211_rx_status *stats,
3131 u16 phy_flags)
3132{
3133 struct iwl_rt_rx_hdr *iwl_rt;
3134
3135 /* First cache any information we need before we overwrite
3136 * the information provided in the skb from the hardware */
3137 s8 signal = stats->ssi;
3138 s8 noise = 0;
3139 int rate = stats->rate;
3140 u64 tsf = stats->mactime;
3141 __le16 phy_flags_hw = cpu_to_le16(phy_flags);
3142
3143 /* We received data from the HW, so stop the watchdog */
3144 if (len > IWL_RX_BUF_SIZE - sizeof(*iwl_rt)) {
3145 IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
3146 return;
3147 }
3148
3149 /* copy the frame data to write after where the radiotap header goes */
3150 iwl_rt = (void *)rxb->skb->data;
3151 memmove(iwl_rt->payload, data, len);
3152
3153 iwl_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
3154 iwl_rt->rt_hdr.it_pad = 0; /* always good to zero */
3155
3156 /* total header + data */
3157 iwl_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl_rt));
3158
3159 /* Set the size of the skb to the size of the frame */
3160 skb_put(rxb->skb, sizeof(*iwl_rt) + len);
3161
3162 /* Big bitfield of all the fields we provide in radiotap */
3163 iwl_rt->rt_hdr.it_present =
3164 cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
3165 (1 << IEEE80211_RADIOTAP_FLAGS) |
3166 (1 << IEEE80211_RADIOTAP_RATE) |
3167 (1 << IEEE80211_RADIOTAP_CHANNEL) |
3168 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
3169 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
3170 (1 << IEEE80211_RADIOTAP_ANTENNA));
3171
3172 /* Zero the flags, we'll add to them as we go */
3173 iwl_rt->rt_flags = 0;
3174
3175 iwl_rt->rt_tsf = cpu_to_le64(tsf);
3176
3177 /* Convert to dBm */
3178 iwl_rt->rt_dbmsignal = signal;
3179 iwl_rt->rt_dbmnoise = noise;
3180
3181 /* Convert the channel frequency and set the flags */
3182 iwl_rt->rt_channelMHz = cpu_to_le16(stats->freq);
3183 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
3184 iwl_rt->rt_chbitmask =
3185 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
3186 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
3187 iwl_rt->rt_chbitmask =
3188 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
3189 else /* 802.11g */
3190 iwl_rt->rt_chbitmask =
3191 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
3192
3193 rate = iwl_rate_index_from_plcp(rate);
3194 if (rate == -1)
3195 iwl_rt->rt_rate = 0;
3196 else
3197 iwl_rt->rt_rate = iwl_rates[rate].ieee;
3198
3199 /* antenna number */
3200 iwl_rt->rt_antenna =
3201 le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
3202
3203 /* set the preamble flag if we have it */
3204 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
3205 iwl_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3206
3207 IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
3208
3209 stats->flag |= RX_FLAG_RADIOTAP;
3210 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3211 rxb->skb = NULL;
3212}
3213
3214
3215#define IWL_PACKET_RETRY_TIME HZ
3216
3217int is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
3218{
3219 u16 sc = le16_to_cpu(header->seq_ctrl);
3220 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3221 u16 frag = sc & IEEE80211_SCTL_FRAG;
3222 u16 *last_seq, *last_frag;
3223 unsigned long *last_time;
3224
3225 switch (priv->iw_mode) {
3226 case IEEE80211_IF_TYPE_IBSS:{
3227 struct list_head *p;
3228 struct iwl_ibss_seq *entry = NULL;
3229 u8 *mac = header->addr2;
3230 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3231
3232 __list_for_each(p, &priv->ibss_mac_hash[index]) {
3233 entry =
3234 list_entry(p, struct iwl_ibss_seq, list);
3235 if (!compare_ether_addr(entry->mac, mac))
3236 break;
3237 }
3238 if (p == &priv->ibss_mac_hash[index]) {
3239 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3240 if (!entry) {
3241 IWL_ERROR
3242 ("Cannot malloc new mac entry\n");
3243 return 0;
3244 }
3245 memcpy(entry->mac, mac, ETH_ALEN);
3246 entry->seq_num = seq;
3247 entry->frag_num = frag;
3248 entry->packet_time = jiffies;
3249 list_add(&entry->list,
3250 &priv->ibss_mac_hash[index]);
3251 return 0;
3252 }
3253 last_seq = &entry->seq_num;
3254 last_frag = &entry->frag_num;
3255 last_time = &entry->packet_time;
3256 break;
3257 }
3258 case IEEE80211_IF_TYPE_STA:
3259 last_seq = &priv->last_seq_num;
3260 last_frag = &priv->last_frag_num;
3261 last_time = &priv->last_packet_time;
3262 break;
3263 default:
3264 return 0;
3265 }
3266 if ((*last_seq == seq) &&
3267 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3268 if (*last_frag == frag)
3269 goto drop;
3270 if (*last_frag + 1 != frag)
3271 /* out-of-order fragment */
3272 goto drop;
3273 } else
3274 *last_seq = seq;
3275
3276 *last_frag = frag;
3277 *last_time = jiffies;
3278 return 0;
3279
3280 drop:
3281 return 1;
3282}
3283
3284#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3285
3286#include "iwl-spectrum.h"
3287
3288#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3289#define BEACON_TIME_MASK_HIGH 0xFF000000
3290#define TIME_UNIT 1024
3291
3292/*
3293 * extended beacon time format
3294 * time in usec will be changed into a 32-bit value in 8:24 format
3295 * the high 1 byte is the beacon counts
3296 * the lower 3 bytes is the time in usec within one beacon interval
3297 */
3298
3299static u32 iwl_usecs_to_beacons(u32 usec, u32 beacon_interval)
3300{
3301 u32 quot;
3302 u32 rem;
3303 u32 interval = beacon_interval * 1024;
3304
3305 if (!interval || !usec)
3306 return 0;
3307
3308 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3309 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3310
3311 return (quot << 24) + rem;
3312}
3313
3314/* base is usually what we get from ucode with each received frame,
3315 * the same as HW timer counter counting down
3316 */
3317
3318static __le32 iwl_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
3319{
3320 u32 base_low = base & BEACON_TIME_MASK_LOW;
3321 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3322 u32 interval = beacon_interval * TIME_UNIT;
3323 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3324 (addon & BEACON_TIME_MASK_HIGH);
3325
3326 if (base_low > addon_low)
3327 res += base_low - addon_low;
3328 else if (base_low < addon_low) {
3329 res += interval + base_low - addon_low;
3330 res += (1 << 24);
3331 } else
3332 res += (1 << 24);
3333
3334 return cpu_to_le32(res);
3335}
3336
3337static int iwl_get_measurement(struct iwl_priv *priv,
3338 struct ieee80211_measurement_params *params,
3339 u8 type)
3340{
3341 struct iwl_spectrum_cmd spectrum;
3342 struct iwl_rx_packet *res;
3343 struct iwl_host_cmd cmd = {
3344 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3345 .data = (void *)&spectrum,
3346 .meta.flags = CMD_WANT_SKB,
3347 };
3348 u32 add_time = le64_to_cpu(params->start_time);
3349 int rc;
3350 int spectrum_resp_status;
3351 int duration = le16_to_cpu(params->duration);
3352
3353 if (iwl_is_associated(priv))
3354 add_time =
3355 iwl_usecs_to_beacons(
3356 le64_to_cpu(params->start_time) - priv->last_tsf,
3357 le16_to_cpu(priv->rxon_timing.beacon_interval));
3358
3359 memset(&spectrum, 0, sizeof(spectrum));
3360
3361 spectrum.channel_count = cpu_to_le16(1);
3362 spectrum.flags =
3363 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3364 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3365 cmd.len = sizeof(spectrum);
3366 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3367
3368 if (iwl_is_associated(priv))
3369 spectrum.start_time =
3370 iwl_add_beacon_time(priv->last_beacon_time,
3371 add_time,
3372 le16_to_cpu(priv->rxon_timing.beacon_interval));
3373 else
3374 spectrum.start_time = 0;
3375
3376 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3377 spectrum.channels[0].channel = params->channel;
3378 spectrum.channels[0].type = type;
3379 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3380 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3381 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3382
3383 rc = iwl_send_cmd_sync(priv, &cmd);
3384 if (rc)
3385 return rc;
3386
3387 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
3388 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3389 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3390 rc = -EIO;
3391 }
3392
3393 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3394 switch (spectrum_resp_status) {
3395 case 0: /* Command will be handled */
3396 if (res->u.spectrum.id != 0xff) {
3397 IWL_DEBUG_INFO
3398 ("Replaced existing measurement: %d\n",
3399 res->u.spectrum.id);
3400 priv->measurement_status &= ~MEASUREMENT_READY;
3401 }
3402 priv->measurement_status |= MEASUREMENT_ACTIVE;
3403 rc = 0;
3404 break;
3405
3406 case 1: /* Command will not be handled */
3407 rc = -EAGAIN;
3408 break;
3409 }
3410
3411 dev_kfree_skb_any(cmd.meta.u.skb);
3412
3413 return rc;
3414}
3415#endif
3416
3417static void iwl_txstatus_to_ieee(struct iwl_priv *priv,
3418 struct iwl_tx_info *tx_sta)
3419{
3420
3421 tx_sta->status.ack_signal = 0;
3422 tx_sta->status.excessive_retries = 0;
3423 tx_sta->status.queue_length = 0;
3424 tx_sta->status.queue_number = 0;
3425
3426 if (in_interrupt())
3427 ieee80211_tx_status_irqsafe(priv->hw,
3428 tx_sta->skb[0], &(tx_sta->status));
3429 else
3430 ieee80211_tx_status(priv->hw,
3431 tx_sta->skb[0], &(tx_sta->status));
3432
3433 tx_sta->skb[0] = NULL;
3434}
3435
3436/**
3437 * iwl_tx_queue_reclaim - Reclaim Tx queue entries no more used by NIC.
3438 *
3439 * When FW advances 'R' index, all entries between old and
3440 * new 'R' index need to be reclaimed. As result, some free space
3441 * forms. If there is enough free space (> low mark), wake Tx queue.
3442 */
3443int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
3444{
3445 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3446 struct iwl_queue *q = &txq->q;
3447 int nfreed = 0;
3448
3449 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3450 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3451 "is out of range [0-%d] %d %d.\n", txq_id,
3452 index, q->n_bd, q->first_empty, q->last_used);
3453 return 0;
3454 }
3455
3456 for (index = iwl_queue_inc_wrap(index, q->n_bd);
3457 q->last_used != index;
3458 q->last_used = iwl_queue_inc_wrap(q->last_used, q->n_bd)) {
3459 if (txq_id != IWL_CMD_QUEUE_NUM) {
3460 iwl_txstatus_to_ieee(priv,
3461 &(txq->txb[txq->q.last_used]));
3462 iwl_hw_txq_free_tfd(priv, txq);
3463 } else if (nfreed > 1) {
3464 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
3465 q->first_empty, q->last_used);
3466 queue_work(priv->workqueue, &priv->restart);
3467 }
3468 nfreed++;
3469 }
3470
3471 if (iwl_queue_space(q) > q->low_mark && (txq_id >= 0) &&
3472 (txq_id != IWL_CMD_QUEUE_NUM) &&
3473 priv->mac80211_registered)
3474 ieee80211_wake_queue(priv->hw, txq_id);
3475
3476
3477 return nfreed;
3478}
3479
3480static int iwl_is_tx_success(u32 status)
3481{
3482 status &= TX_STATUS_MSK;
3483 return (status == TX_STATUS_SUCCESS)
3484 || (status == TX_STATUS_DIRECT_DONE);
3485}
3486
3487/******************************************************************************
3488 *
3489 * Generic RX handler implementations
3490 *
3491 ******************************************************************************/
3492#ifdef CONFIG_IWLWIFI_HT
3493#ifdef CONFIG_IWLWIFI_HT_AGG
3494
3495static inline int iwl_get_ra_sta_id(struct iwl_priv *priv,
3496 struct ieee80211_hdr *hdr)
3497{
3498 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3499 return IWL_AP_ID;
3500 else {
3501 u8 *da = ieee80211_get_DA(hdr);
3502 return iwl_hw_find_station(priv, da);
3503 }
3504}
3505
3506static struct ieee80211_hdr *iwl_tx_queue_get_hdr(
3507 struct iwl_priv *priv, int txq_id, int idx)
3508{
3509 if (priv->txq[txq_id].txb[idx].skb[0])
3510 return (struct ieee80211_hdr *)priv->txq[txq_id].
3511 txb[idx].skb[0]->data;
3512 return NULL;
3513}
3514
3515static inline u32 iwl_get_scd_ssn(struct iwl_tx_resp *tx_resp)
3516{
3517 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3518 tx_resp->frame_count);
3519 return le32_to_cpu(*scd_ssn) & MAX_SN;
3520
3521}
3522static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
3523 struct iwl_ht_agg *agg,
3524 struct iwl_tx_resp *tx_resp,
3525 u16 start_idx)
3526{
3527 u32 status;
3528 __le32 *frame_status = &tx_resp->status;
3529 struct ieee80211_tx_status *tx_status = NULL;
3530 struct ieee80211_hdr *hdr = NULL;
3531 int i, sh;
3532 int txq_id, idx;
3533 u16 seq;
3534
3535 if (agg->wait_for_ba)
3536 IWL_DEBUG_TX_REPLY("got tx repsons w/o back\n");
3537
3538 agg->frame_count = tx_resp->frame_count;
3539 agg->start_idx = start_idx;
3540 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3541 agg->bitmap0 = agg->bitmap1 = 0;
3542
3543 if (agg->frame_count == 1) {
3544 struct iwl_tx_queue *txq ;
3545 status = le32_to_cpu(frame_status[0]);
3546
3547 txq_id = agg->txq_id;
3548 txq = &priv->txq[txq_id];
3549 /* FIXME: code repetition */
3550 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d \n",
3551 agg->frame_count, agg->start_idx);
3552
3553 tx_status = &(priv->txq[txq_id].txb[txq->q.last_used].status);
3554 tx_status->retry_count = tx_resp->failure_frame;
3555 tx_status->queue_number = status & 0xff;
3556 tx_status->queue_length = tx_resp->bt_kill_count;
3557 tx_status->queue_length |= tx_resp->failure_rts;
3558
3559 tx_status->flags = iwl_is_tx_success(status)?
3560 IEEE80211_TX_STATUS_ACK : 0;
3561 tx_status->control.tx_rate =
3562 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags);
3563 /* FIXME: code repetition end */
3564
3565 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3566 status & 0xff, tx_resp->failure_frame);
3567 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
3568 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags));
3569
3570 agg->wait_for_ba = 0;
3571 } else {
3572 u64 bitmap = 0;
3573 int start = agg->start_idx;
3574
3575 for (i = 0; i < agg->frame_count; i++) {
3576 u16 sc;
3577 status = le32_to_cpu(frame_status[i]);
3578 seq = status >> 16;
3579 idx = SEQ_TO_INDEX(seq);
3580 txq_id = SEQ_TO_QUEUE(seq);
3581
3582 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3583 AGG_TX_STATE_ABORT_MSK))
3584 continue;
3585
3586 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3587 agg->frame_count, txq_id, idx);
3588
3589 hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
3590
3591 sc = le16_to_cpu(hdr->seq_ctrl);
3592 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3593 IWL_ERROR("BUG_ON idx doesn't match seq control"
3594 " idx=%d, seq_idx=%d, seq=%d\n",
3595 idx, SEQ_TO_SN(sc),
3596 hdr->seq_ctrl);
3597 return -1;
3598 }
3599
3600 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3601 i, idx, SEQ_TO_SN(sc));
3602
3603 sh = idx - start;
3604 if (sh > 64) {
3605 sh = (start - idx) + 0xff;
3606 bitmap = bitmap << sh;
3607 sh = 0;
3608 start = idx;
3609 } else if (sh < -64)
3610 sh = 0xff - (start - idx);
3611 else if (sh < 0) {
3612 sh = start - idx;
3613 start = idx;
3614 bitmap = bitmap << sh;
3615 sh = 0;
3616 }
3617 bitmap |= (1 << sh);
3618 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3619 start, (u32)(bitmap & 0xFFFFFFFF));
3620 }
3621
3622 agg->bitmap0 = bitmap & 0xFFFFFFFF;
3623 agg->bitmap1 = bitmap >> 32;
3624 agg->start_idx = start;
3625 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3626 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%x\n",
3627 agg->frame_count, agg->start_idx,
3628 agg->bitmap0);
3629
3630 if (bitmap)
3631 agg->wait_for_ba = 1;
3632 }
3633 return 0;
3634}
3635#endif
3636#endif
3637
3638static void iwl_rx_reply_tx(struct iwl_priv *priv,
3639 struct iwl_rx_mem_buffer *rxb)
3640{
3641 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3642 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3643 int txq_id = SEQ_TO_QUEUE(sequence);
3644 int index = SEQ_TO_INDEX(sequence);
3645 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3646 struct ieee80211_tx_status *tx_status;
3647 struct iwl_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
3648 u32 status = le32_to_cpu(tx_resp->status);
3649#ifdef CONFIG_IWLWIFI_HT
3650#ifdef CONFIG_IWLWIFI_HT_AGG
3651 int tid, sta_id;
3652#endif
3653#endif
3654
3655 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3656 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3657 "is out of range [0-%d] %d %d\n", txq_id,
3658 index, txq->q.n_bd, txq->q.first_empty,
3659 txq->q.last_used);
3660 return;
3661 }
3662
3663#ifdef CONFIG_IWLWIFI_HT
3664#ifdef CONFIG_IWLWIFI_HT_AGG
3665 if (txq->sched_retry) {
3666 const u32 scd_ssn = iwl_get_scd_ssn(tx_resp);
3667 struct ieee80211_hdr *hdr =
3668 iwl_tx_queue_get_hdr(priv, txq_id, index);
3669 struct iwl_ht_agg *agg = NULL;
3670 __le16 *qc = ieee80211_get_qos_ctrl(hdr);
3671
3672 if (qc == NULL) {
3673 IWL_ERROR("BUG_ON qc is null!!!!\n");
3674 return;
3675 }
3676
3677 tid = le16_to_cpu(*qc) & 0xf;
3678
3679 sta_id = iwl_get_ra_sta_id(priv, hdr);
3680 if (unlikely(sta_id == IWL_INVALID_STATION)) {
3681 IWL_ERROR("Station not known for\n");
3682 return;
3683 }
3684
3685 agg = &priv->stations[sta_id].tid[tid].agg;
3686
3687 iwl4965_tx_status_reply_tx(priv, agg, tx_resp, index);
3688
3689 if ((tx_resp->frame_count == 1) &&
3690 !iwl_is_tx_success(status)) {
3691 /* TODO: send BAR */
3692 }
3693
3694 if ((txq->q.last_used != (scd_ssn & 0xff))) {
3695 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
3696 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3697 "%d index %d\n", scd_ssn , index);
3698 iwl_tx_queue_reclaim(priv, txq_id, index);
3699 }
3700 } else {
3701#endif /* CONFIG_IWLWIFI_HT_AGG */
3702#endif /* CONFIG_IWLWIFI_HT */
3703 tx_status = &(txq->txb[txq->q.last_used].status);
3704
3705 tx_status->retry_count = tx_resp->failure_frame;
3706 tx_status->queue_number = status;
3707 tx_status->queue_length = tx_resp->bt_kill_count;
3708 tx_status->queue_length |= tx_resp->failure_rts;
3709
3710 tx_status->flags =
3711 iwl_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
3712
3713 tx_status->control.tx_rate =
3714 iwl_hw_get_rate_n_flags(tx_resp->rate_n_flags);
3715
3716 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
3717 "retries %d\n", txq_id, iwl_get_tx_fail_reason(status),
3718 status, le32_to_cpu(tx_resp->rate_n_flags),
3719 tx_resp->failure_frame);
3720
3721 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3722 if (index != -1)
3723 iwl_tx_queue_reclaim(priv, txq_id, index);
3724#ifdef CONFIG_IWLWIFI_HT
3725#ifdef CONFIG_IWLWIFI_HT_AGG
3726 }
3727#endif /* CONFIG_IWLWIFI_HT_AGG */
3728#endif /* CONFIG_IWLWIFI_HT */
3729
3730 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3731 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3732}
3733
3734
3735static void iwl_rx_reply_alive(struct iwl_priv *priv,
3736 struct iwl_rx_mem_buffer *rxb)
3737{
3738 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3739 struct iwl_alive_resp *palive;
3740 struct delayed_work *pwork;
3741
3742 palive = &pkt->u.alive_frame;
3743
3744 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3745 "0x%01X 0x%01X\n",
3746 palive->is_valid, palive->ver_type,
3747 palive->ver_subtype);
3748
3749 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3750 IWL_DEBUG_INFO("Initialization Alive received.\n");
3751 memcpy(&priv->card_alive_init,
3752 &pkt->u.alive_frame,
3753 sizeof(struct iwl_init_alive_resp));
3754 pwork = &priv->init_alive_start;
3755 } else {
3756 IWL_DEBUG_INFO("Runtime Alive received.\n");
3757 memcpy(&priv->card_alive, &pkt->u.alive_frame,
3758 sizeof(struct iwl_alive_resp));
3759 pwork = &priv->alive_start;
3760 }
3761
3762 /* We delay the ALIVE response by 5ms to
3763 * give the HW RF Kill time to activate... */
3764 if (palive->is_valid == UCODE_VALID_OK)
3765 queue_delayed_work(priv->workqueue, pwork,
3766 msecs_to_jiffies(5));
3767 else
3768 IWL_WARNING("uCode did not respond OK.\n");
3769}
3770
3771static void iwl_rx_reply_add_sta(struct iwl_priv *priv,
3772 struct iwl_rx_mem_buffer *rxb)
3773{
3774 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3775
3776 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3777 return;
3778}
3779
3780static void iwl_rx_reply_error(struct iwl_priv *priv,
3781 struct iwl_rx_mem_buffer *rxb)
3782{
3783 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3784
3785 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3786 "seq 0x%04X ser 0x%08X\n",
3787 le32_to_cpu(pkt->u.err_resp.error_type),
3788 get_cmd_string(pkt->u.err_resp.cmd_id),
3789 pkt->u.err_resp.cmd_id,
3790 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3791 le32_to_cpu(pkt->u.err_resp.error_info));
3792}
3793
3794#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3795
3796static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
3797{
3798 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3799 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
3800 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
3801 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3802 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3803 rxon->channel = csa->channel;
3804 priv->staging_rxon.channel = csa->channel;
3805}
3806
3807static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
3808 struct iwl_rx_mem_buffer *rxb)
3809{
3810#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3811 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3812 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
3813
3814 if (!report->state) {
3815 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3816 "Spectrum Measure Notification: Start\n");
3817 return;
3818 }
3819
3820 memcpy(&priv->measure_report, report, sizeof(*report));
3821 priv->measurement_status |= MEASUREMENT_READY;
3822#endif
3823}
3824
3825static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
3826 struct iwl_rx_mem_buffer *rxb)
3827{
3828#ifdef CONFIG_IWLWIFI_DEBUG
3829 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3830 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
3831 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3832 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3833#endif
3834}
3835
3836static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
3837 struct iwl_rx_mem_buffer *rxb)
3838{
3839 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3840 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3841 "notification for %s:\n",
3842 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
3843 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
3844}
3845
3846static void iwl_bg_beacon_update(struct work_struct *work)
3847{
3848 struct iwl_priv *priv =
3849 container_of(work, struct iwl_priv, beacon_update);
3850 struct sk_buff *beacon;
3851
3852 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3853 beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
3854
3855 if (!beacon) {
3856 IWL_ERROR("update beacon failed\n");
3857 return;
3858 }
3859
3860 mutex_lock(&priv->mutex);
3861 /* new beacon skb is allocated every time; dispose previous.*/
3862 if (priv->ibss_beacon)
3863 dev_kfree_skb(priv->ibss_beacon);
3864
3865 priv->ibss_beacon = beacon;
3866 mutex_unlock(&priv->mutex);
3867
3868 iwl_send_beacon_cmd(priv);
3869}
3870
3871static void iwl_rx_beacon_notif(struct iwl_priv *priv,
3872 struct iwl_rx_mem_buffer *rxb)
3873{
3874#ifdef CONFIG_IWLWIFI_DEBUG
3875 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3876 struct iwl_beacon_notif *beacon = &(pkt->u.beacon_status);
3877 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
3878
3879 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3880 "tsf %d %d rate %d\n",
3881 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3882 beacon->beacon_notify_hdr.failure_frame,
3883 le32_to_cpu(beacon->ibss_mgr_status),
3884 le32_to_cpu(beacon->high_tsf),
3885 le32_to_cpu(beacon->low_tsf), rate);
3886#endif
3887
3888 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3889 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3890 queue_work(priv->workqueue, &priv->beacon_update);
3891}
3892
3893/* Service response to REPLY_SCAN_CMD (0x80) */
3894static void iwl_rx_reply_scan(struct iwl_priv *priv,
3895 struct iwl_rx_mem_buffer *rxb)
3896{
3897#ifdef CONFIG_IWLWIFI_DEBUG
3898 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3899 struct iwl_scanreq_notification *notif =
3900 (struct iwl_scanreq_notification *)pkt->u.raw;
3901
3902 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3903#endif
3904}
3905
3906/* Service SCAN_START_NOTIFICATION (0x82) */
3907static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
3908 struct iwl_rx_mem_buffer *rxb)
3909{
3910 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3911 struct iwl_scanstart_notification *notif =
3912 (struct iwl_scanstart_notification *)pkt->u.raw;
3913 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3914 IWL_DEBUG_SCAN("Scan start: "
3915 "%d [802.11%s] "
3916 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3917 notif->channel,
3918 notif->band ? "bg" : "a",
3919 notif->tsf_high,
3920 notif->tsf_low, notif->status, notif->beacon_timer);
3921}
3922
3923/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
3924static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
3925 struct iwl_rx_mem_buffer *rxb)
3926{
3927 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3928 struct iwl_scanresults_notification *notif =
3929 (struct iwl_scanresults_notification *)pkt->u.raw;
3930
3931 IWL_DEBUG_SCAN("Scan ch.res: "
3932 "%d [802.11%s] "
3933 "(TSF: 0x%08X:%08X) - %d "
3934 "elapsed=%lu usec (%dms since last)\n",
3935 notif->channel,
3936 notif->band ? "bg" : "a",
3937 le32_to_cpu(notif->tsf_high),
3938 le32_to_cpu(notif->tsf_low),
3939 le32_to_cpu(notif->statistics[0]),
3940 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3941 jiffies_to_msecs(elapsed_jiffies
3942 (priv->last_scan_jiffies, jiffies)));
3943
3944 priv->last_scan_jiffies = jiffies;
3945}
3946
3947/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
3948static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
3949 struct iwl_rx_mem_buffer *rxb)
3950{
3951 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3952 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
3953
3954 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3955 scan_notif->scanned_channels,
3956 scan_notif->tsf_low,
3957 scan_notif->tsf_high, scan_notif->status);
3958
3959 /* The HW is no longer scanning */
3960 clear_bit(STATUS_SCAN_HW, &priv->status);
3961
3962 /* The scan completion notification came in, so kill that timer... */
3963 cancel_delayed_work(&priv->scan_check);
3964
3965 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3966 (priv->scan_bands == 2) ? "2.4" : "5.2",
3967 jiffies_to_msecs(elapsed_jiffies
3968 (priv->scan_pass_start, jiffies)));
3969
3970 /* Remove this scanned band from the list
3971 * of pending bands to scan */
3972 priv->scan_bands--;
3973
3974 /* If a request to abort was given, or the scan did not succeed
3975 * then we reset the scan state machine and terminate,
3976 * re-queuing another scan if one has been requested */
3977 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3978 IWL_DEBUG_INFO("Aborted scan completed.\n");
3979 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3980 } else {
3981 /* If there are more bands on this scan pass reschedule */
3982 if (priv->scan_bands > 0)
3983 goto reschedule;
3984 }
3985
3986 priv->last_scan_jiffies = jiffies;
3987 IWL_DEBUG_INFO("Setting scan to off\n");
3988
3989 clear_bit(STATUS_SCANNING, &priv->status);
3990
3991 IWL_DEBUG_INFO("Scan took %dms\n",
3992 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3993
3994 queue_work(priv->workqueue, &priv->scan_completed);
3995
3996 return;
3997
3998reschedule:
3999 priv->scan_pass_start = jiffies;
4000 queue_work(priv->workqueue, &priv->request_scan);
4001}
4002
4003/* Handle notification from uCode that card's power state is changing
4004 * due to software, hardware, or critical temperature RFKILL */
4005static void iwl_rx_card_state_notif(struct iwl_priv *priv,
4006 struct iwl_rx_mem_buffer *rxb)
4007{
4008 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
4009 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
4010 unsigned long status = priv->status;
4011
4012 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
4013 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
4014 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
4015
4016 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
4017 RF_CARD_DISABLED)) {
4018
4019 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
4020 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4021
4022 if (!iwl_grab_restricted_access(priv)) {
4023 iwl_write_restricted(
4024 priv, HBUS_TARG_MBX_C,
4025 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4026
4027 iwl_release_restricted_access(priv);
4028 }
4029
4030 if (!(flags & RXON_CARD_DISABLED)) {
4031 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
4032 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4033 if (!iwl_grab_restricted_access(priv)) {
4034 iwl_write_restricted(
4035 priv, HBUS_TARG_MBX_C,
4036 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4037
4038 iwl_release_restricted_access(priv);
4039 }
4040 }
4041
4042 if (flags & RF_CARD_DISABLED) {
4043 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
4044 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
4045 iwl_read32(priv, CSR_UCODE_DRV_GP1);
4046 if (!iwl_grab_restricted_access(priv))
4047 iwl_release_restricted_access(priv);
4048 }
4049 }
4050
4051 if (flags & HW_CARD_DISABLED)
4052 set_bit(STATUS_RF_KILL_HW, &priv->status);
4053 else
4054 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4055
4056
4057 if (flags & SW_CARD_DISABLED)
4058 set_bit(STATUS_RF_KILL_SW, &priv->status);
4059 else
4060 clear_bit(STATUS_RF_KILL_SW, &priv->status);
4061
4062 if (!(flags & RXON_CARD_DISABLED))
4063 iwl_scan_cancel(priv);
4064
4065 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
4066 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
4067 (test_bit(STATUS_RF_KILL_SW, &status) !=
4068 test_bit(STATUS_RF_KILL_SW, &priv->status)))
4069 queue_work(priv->workqueue, &priv->rf_kill);
4070 else
4071 wake_up_interruptible(&priv->wait_command_queue);
4072}
4073
4074/**
4075 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
4076 *
4077 * Setup the RX handlers for each of the reply types sent from the uCode
4078 * to the host.
4079 *
4080 * This function chains into the hardware specific files for them to setup
4081 * any hardware specific handlers as well.
4082 */
4083static void iwl_setup_rx_handlers(struct iwl_priv *priv)
4084{
4085 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
4086 priv->rx_handlers[REPLY_ADD_STA] = iwl_rx_reply_add_sta;
4087 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
4088 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
4089 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
4090 iwl_rx_spectrum_measure_notif;
4091 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
4092 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
4093 iwl_rx_pm_debug_statistics_notif;
4094 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
4095
4096 /* NOTE: iwl_rx_statistics is different based on whether
4097 * the build is for the 3945 or the 4965. See the
4098 * corresponding implementation in iwl-XXXX.c
4099 *
4100 * The same handler is used for both the REPLY to a
4101 * discrete statistics request from the host as well as
4102 * for the periodic statistics notification from the uCode
4103 */
4104 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_hw_rx_statistics;
4105 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_hw_rx_statistics;
4106
4107 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
4108 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
4109 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
4110 iwl_rx_scan_results_notif;
4111 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
4112 iwl_rx_scan_complete_notif;
4113 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
4114 priv->rx_handlers[REPLY_TX] = iwl_rx_reply_tx;
4115
4116 /* Setup hardware specific Rx handlers */
4117 iwl_hw_rx_handler_setup(priv);
4118}
4119
4120/**
4121 * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
4122 * @rxb: Rx buffer to reclaim
4123 *
4124 * If an Rx buffer has an async callback associated with it the callback
4125 * will be executed. The attached skb (if present) will only be freed
4126 * if the callback returns 1
4127 */
4128static void iwl_tx_cmd_complete(struct iwl_priv *priv,
4129 struct iwl_rx_mem_buffer *rxb)
4130{
4131 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
4132 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
4133 int txq_id = SEQ_TO_QUEUE(sequence);
4134 int index = SEQ_TO_INDEX(sequence);
4135 int huge = sequence & SEQ_HUGE_FRAME;
4136 int cmd_index;
4137 struct iwl_cmd *cmd;
4138
4139 /* If a Tx command is being handled and it isn't in the actual
4140 * command queue then there a command routing bug has been introduced
4141 * in the queue management code. */
4142 if (txq_id != IWL_CMD_QUEUE_NUM)
4143 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
4144 txq_id, pkt->hdr.cmd);
4145 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
4146
4147 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
4148 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
4149
4150 /* Input error checking is done when commands are added to queue. */
4151 if (cmd->meta.flags & CMD_WANT_SKB) {
4152 cmd->meta.source->u.skb = rxb->skb;
4153 rxb->skb = NULL;
4154 } else if (cmd->meta.u.callback &&
4155 !cmd->meta.u.callback(priv, cmd, rxb->skb))
4156 rxb->skb = NULL;
4157
4158 iwl_tx_queue_reclaim(priv, txq_id, index);
4159
4160 if (!(cmd->meta.flags & CMD_ASYNC)) {
4161 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4162 wake_up_interruptible(&priv->wait_command_queue);
4163 }
4164}
4165
4166/************************** RX-FUNCTIONS ****************************/
4167/*
4168 * Rx theory of operation
4169 *
4170 * The host allocates 32 DMA target addresses and passes the host address
4171 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
4172 * 0 to 31
4173 *
4174 * Rx Queue Indexes
4175 * The host/firmware share two index registers for managing the Rx buffers.
4176 *
4177 * The READ index maps to the first position that the firmware may be writing
4178 * to -- the driver can read up to (but not including) this position and get
4179 * good data.
4180 * The READ index is managed by the firmware once the card is enabled.
4181 *
4182 * The WRITE index maps to the last position the driver has read from -- the
4183 * position preceding WRITE is the last slot the firmware can place a packet.
4184 *
4185 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
4186 * WRITE = READ.
4187 *
4188 * During initialization the host sets up the READ queue position to the first
4189 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4190 *
4191 * When the firmware places a packet in a buffer it will advance the READ index
4192 * and fire the RX interrupt. The driver can then query the READ index and
4193 * process as many packets as possible, moving the WRITE index forward as it
4194 * resets the Rx queue buffers with new memory.
4195 *
4196 * The management in the driver is as follows:
4197 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
4198 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
4199 * to replensish the iwl->rxq->rx_free.
4200 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
4201 * iwl->rxq is replenished and the READ INDEX is updated (updating the
4202 * 'processed' and 'read' driver indexes as well)
4203 * + A received packet is processed and handed to the kernel network stack,
4204 * detached from the iwl->rxq. The driver 'processed' index is updated.
4205 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
4206 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
4207 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
4208 * were enough free buffers and RX_STALLED is set it is cleared.
4209 *
4210 *
4211 * Driver sequence:
4212 *
4213 * iwl_rx_queue_alloc() Allocates rx_free
4214 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
4215 * iwl_rx_queue_restock
4216 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
4217 * queue, updates firmware pointers, and updates
4218 * the WRITE index. If insufficient rx_free buffers
4219 * are available, schedules iwl_rx_replenish
4220 *
4221 * -- enable interrupts --
4222 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
4223 * READ INDEX, detaching the SKB from the pool.
4224 * Moves the packet buffer from queue to rx_used.
4225 * Calls iwl_rx_queue_restock to refill any empty
4226 * slots.
4227 * ...
4228 *
4229 */
4230
4231/**
4232 * iwl_rx_queue_space - Return number of free slots available in queue.
4233 */
4234static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
4235{
4236 int s = q->read - q->write;
4237 if (s <= 0)
4238 s += RX_QUEUE_SIZE;
4239 /* keep some buffer to not confuse full and empty queue */
4240 s -= 2;
4241 if (s < 0)
4242 s = 0;
4243 return s;
4244}
4245
4246/**
4247 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
4248 *
4249 * NOTE: This function has 3945 and 4965 specific code sections
4250 * but is declared in base due to the majority of the
4251 * implementation being the same (only a numeric constant is
4252 * different)
4253 *
4254 */
4255int iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
4256{
4257 u32 reg = 0;
4258 int rc = 0;
4259 unsigned long flags;
4260
4261 spin_lock_irqsave(&q->lock, flags);
4262
4263 if (q->need_update == 0)
4264 goto exit_unlock;
4265
4266 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4267 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4268
4269 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4270 iwl_set_bit(priv, CSR_GP_CNTRL,
4271 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4272 goto exit_unlock;
4273 }
4274
4275 rc = iwl_grab_restricted_access(priv);
4276 if (rc)
4277 goto exit_unlock;
4278
4279 iwl_write_restricted(priv, FH_RSCSR_CHNL0_WPTR,
4280 q->write & ~0x7);
4281 iwl_release_restricted_access(priv);
4282 } else
4283 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
4284
4285
4286 q->need_update = 0;
4287
4288 exit_unlock:
4289 spin_unlock_irqrestore(&q->lock, flags);
4290 return rc;
4291}
4292
4293/**
4294 * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer pointer.
4295 *
4296 * NOTE: This function has 3945 and 4965 specific code paths in it.
4297 */
4298static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
4299 dma_addr_t dma_addr)
4300{
4301 return cpu_to_le32((u32)(dma_addr >> 8));
4302}
4303
4304
4305/**
4306 * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
4307 *
4308 * If there are slots in the RX queue that need to be restocked,
4309 * and we have free pre-allocated buffers, fill the ranks as much
4310 * as we can pulling from rx_free.
4311 *
4312 * This moves the 'write' index forward to catch up with 'processed', and
4313 * also updates the memory address in the firmware to reference the new
4314 * target buffer.
4315 */
4316int iwl_rx_queue_restock(struct iwl_priv *priv)
4317{
4318 struct iwl_rx_queue *rxq = &priv->rxq;
4319 struct list_head *element;
4320 struct iwl_rx_mem_buffer *rxb;
4321 unsigned long flags;
4322 int write, rc;
4323
4324 spin_lock_irqsave(&rxq->lock, flags);
4325 write = rxq->write & ~0x7;
4326 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
4327 element = rxq->rx_free.next;
4328 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4329 list_del(element);
4330 rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->dma_addr);
4331 rxq->queue[rxq->write] = rxb;
4332 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4333 rxq->free_count--;
4334 }
4335 spin_unlock_irqrestore(&rxq->lock, flags);
4336 /* If the pre-allocated buffer pool is dropping low, schedule to
4337 * refill it */
4338 if (rxq->free_count <= RX_LOW_WATERMARK)
4339 queue_work(priv->workqueue, &priv->rx_replenish);
4340
4341
4342 /* If we've added more space for the firmware to place data, tell it */
4343 if ((write != (rxq->write & ~0x7))
4344 || (abs(rxq->write - rxq->read) > 7)) {
4345 spin_lock_irqsave(&rxq->lock, flags);
4346 rxq->need_update = 1;
4347 spin_unlock_irqrestore(&rxq->lock, flags);
4348 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
4349 if (rc)
4350 return rc;
4351 }
4352
4353 return 0;
4354}
4355
4356/**
4357 * iwl_rx_replensih - Move all used packet from rx_used to rx_free
4358 *
4359 * When moving to rx_free an SKB is allocated for the slot.
4360 *
4361 * Also restock the Rx queue via iwl_rx_queue_restock.
4362 * This is called as a scheduled work item (except for during intialization)
4363 */
4364void iwl_rx_replenish(void *data)
4365{
4366 struct iwl_priv *priv = data;
4367 struct iwl_rx_queue *rxq = &priv->rxq;
4368 struct list_head *element;
4369 struct iwl_rx_mem_buffer *rxb;
4370 unsigned long flags;
4371 spin_lock_irqsave(&rxq->lock, flags);
4372 while (!list_empty(&rxq->rx_used)) {
4373 element = rxq->rx_used.next;
4374 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4375 rxb->skb =
4376 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
4377 if (!rxb->skb) {
4378 if (net_ratelimit())
4379 printk(KERN_CRIT DRV_NAME
4380 ": Can not allocate SKB buffers\n");
4381 /* We don't reschedule replenish work here -- we will
4382 * call the restock method and if it still needs
4383 * more buffers it will schedule replenish */
4384 break;
4385 }
4386 priv->alloc_rxb_skb++;
4387 list_del(element);
4388 rxb->dma_addr =
4389 pci_map_single(priv->pci_dev, rxb->skb->data,
4390 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4391 list_add_tail(&rxb->list, &rxq->rx_free);
4392 rxq->free_count++;
4393 }
4394 spin_unlock_irqrestore(&rxq->lock, flags);
4395
4396 spin_lock_irqsave(&priv->lock, flags);
4397 iwl_rx_queue_restock(priv);
4398 spin_unlock_irqrestore(&priv->lock, flags);
4399}
4400
4401/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
4402 * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
4403 * This free routine walks the list of POOL entries and if SKB is set to
4404 * non NULL it is unmapped and freed
4405 */
4406void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4407{
4408 int i;
4409 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4410 if (rxq->pool[i].skb != NULL) {
4411 pci_unmap_single(priv->pci_dev,
4412 rxq->pool[i].dma_addr,
4413 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4414 dev_kfree_skb(rxq->pool[i].skb);
4415 }
4416 }
4417
4418 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4419 rxq->dma_addr);
4420 rxq->bd = NULL;
4421}
4422
4423int iwl_rx_queue_alloc(struct iwl_priv *priv)
4424{
4425 struct iwl_rx_queue *rxq = &priv->rxq;
4426 struct pci_dev *dev = priv->pci_dev;
4427 int i;
4428
4429 spin_lock_init(&rxq->lock);
4430 INIT_LIST_HEAD(&rxq->rx_free);
4431 INIT_LIST_HEAD(&rxq->rx_used);
4432 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4433 if (!rxq->bd)
4434 return -ENOMEM;
4435 /* Fill the rx_used queue with _all_ of the Rx buffers */
4436 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4437 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4438 /* Set us so that we have processed and used all buffers, but have
4439 * not restocked the Rx queue with fresh buffers */
4440 rxq->read = rxq->write = 0;
4441 rxq->free_count = 0;
4442 rxq->need_update = 0;
4443 return 0;
4444}
4445
4446void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4447{
4448 unsigned long flags;
4449 int i;
4450 spin_lock_irqsave(&rxq->lock, flags);
4451 INIT_LIST_HEAD(&rxq->rx_free);
4452 INIT_LIST_HEAD(&rxq->rx_used);
4453 /* Fill the rx_used queue with _all_ of the Rx buffers */
4454 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4455 /* In the reset function, these buffers may have been allocated
4456 * to an SKB, so we need to unmap and free potential storage */
4457 if (rxq->pool[i].skb != NULL) {
4458 pci_unmap_single(priv->pci_dev,
4459 rxq->pool[i].dma_addr,
4460 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4461 priv->alloc_rxb_skb--;
4462 dev_kfree_skb(rxq->pool[i].skb);
4463 rxq->pool[i].skb = NULL;
4464 }
4465 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4466 }
4467
4468 /* Set us so that we have processed and used all buffers, but have
4469 * not restocked the Rx queue with fresh buffers */
4470 rxq->read = rxq->write = 0;
4471 rxq->free_count = 0;
4472 spin_unlock_irqrestore(&rxq->lock, flags);
4473}
4474
4475/* Convert linear signal-to-noise ratio into dB */
4476static u8 ratio2dB[100] = {
4477/* 0 1 2 3 4 5 6 7 8 9 */
4478 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4479 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4480 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4481 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4482 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4483 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4484 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4485 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4486 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4487 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4488};
4489
4490/* Calculates a relative dB value from a ratio of linear
4491 * (i.e. not dB) signal levels.
4492 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
4493int iwl_calc_db_from_ratio(int sig_ratio)
4494{
4495 /* Anything above 1000:1 just report as 60 dB */
4496 if (sig_ratio > 1000)
4497 return 60;
4498
4499 /* Above 100:1, divide by 10 and use table,
4500 * add 20 dB to make up for divide by 10 */
4501 if (sig_ratio > 100)
4502 return (20 + (int)ratio2dB[sig_ratio/10]);
4503
4504 /* We shouldn't see this */
4505 if (sig_ratio < 1)
4506 return 0;
4507
4508 /* Use table for ratios 1:1 - 99:1 */
4509 return (int)ratio2dB[sig_ratio];
4510}
4511
4512#define PERFECT_RSSI (-20) /* dBm */
4513#define WORST_RSSI (-95) /* dBm */
4514#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4515
4516/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4517 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4518 * about formulas used below. */
4519int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
4520{
4521 int sig_qual;
4522 int degradation = PERFECT_RSSI - rssi_dbm;
4523
4524 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4525 * as indicator; formula is (signal dbm - noise dbm).
4526 * SNR at or above 40 is a great signal (100%).
4527 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4528 * Weakest usable signal is usually 10 - 15 dB SNR. */
4529 if (noise_dbm) {
4530 if (rssi_dbm - noise_dbm >= 40)
4531 return 100;
4532 else if (rssi_dbm < noise_dbm)
4533 return 0;
4534 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4535
4536 /* Else use just the signal level.
4537 * This formula is a least squares fit of data points collected and
4538 * compared with a reference system that had a percentage (%) display
4539 * for signal quality. */
4540 } else
4541 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4542 (15 * RSSI_RANGE + 62 * degradation)) /
4543 (RSSI_RANGE * RSSI_RANGE);
4544
4545 if (sig_qual > 100)
4546 sig_qual = 100;
4547 else if (sig_qual < 1)
4548 sig_qual = 0;
4549
4550 return sig_qual;
4551}
4552
4553/**
4554 * iwl_rx_handle - Main entry function for receiving responses from the uCode
4555 *
4556 * Uses the priv->rx_handlers callback function array to invoke
4557 * the appropriate handlers, including command responses,
4558 * frame-received notifications, and other notifications.
4559 */
4560static void iwl_rx_handle(struct iwl_priv *priv)
4561{
4562 struct iwl_rx_mem_buffer *rxb;
4563 struct iwl_rx_packet *pkt;
4564 struct iwl_rx_queue *rxq = &priv->rxq;
4565 u32 r, i;
4566 int reclaim;
4567 unsigned long flags;
4568
4569 r = iwl_hw_get_rx_read(priv);
4570 i = rxq->read;
4571
4572 /* Rx interrupt, but nothing sent from uCode */
4573 if (i == r)
4574 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4575
4576 while (i != r) {
4577 rxb = rxq->queue[i];
4578
4579 /* If an RXB doesn't have a queue slot associated with it
4580 * then a bug has been introduced in the queue refilling
4581 * routines -- catch it here */
4582 BUG_ON(rxb == NULL);
4583
4584 rxq->queue[i] = NULL;
4585
4586 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4587 IWL_RX_BUF_SIZE,
4588 PCI_DMA_FROMDEVICE);
4589 pkt = (struct iwl_rx_packet *)rxb->skb->data;
4590
4591 /* Reclaim a command buffer only if this packet is a response
4592 * to a (driver-originated) command.
4593 * If the packet (e.g. Rx frame) originated from uCode,
4594 * there is no command buffer to reclaim.
4595 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4596 * but apparently a few don't get set; catch them here. */
4597 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4598 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4599 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004600 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004601 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4602 (pkt->hdr.cmd != REPLY_TX);
4603
4604 /* Based on type of command response or notification,
4605 * handle those that need handling via function in
4606 * rx_handlers table. See iwl_setup_rx_handlers() */
4607 if (priv->rx_handlers[pkt->hdr.cmd]) {
4608 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4609 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4610 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4611 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4612 } else {
4613 /* No handling needed */
4614 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4615 "r %d i %d No handler needed for %s, 0x%02x\n",
4616 r, i, get_cmd_string(pkt->hdr.cmd),
4617 pkt->hdr.cmd);
4618 }
4619
4620 if (reclaim) {
4621 /* Invoke any callbacks, transfer the skb to caller,
4622 * and fire off the (possibly) blocking iwl_send_cmd()
4623 * as we reclaim the driver command queue */
4624 if (rxb && rxb->skb)
4625 iwl_tx_cmd_complete(priv, rxb);
4626 else
4627 IWL_WARNING("Claim null rxb?\n");
4628 }
4629
4630 /* For now we just don't re-use anything. We can tweak this
4631 * later to try and re-use notification packets and SKBs that
4632 * fail to Rx correctly */
4633 if (rxb->skb != NULL) {
4634 priv->alloc_rxb_skb--;
4635 dev_kfree_skb_any(rxb->skb);
4636 rxb->skb = NULL;
4637 }
4638
4639 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4640 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4641 spin_lock_irqsave(&rxq->lock, flags);
4642 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4643 spin_unlock_irqrestore(&rxq->lock, flags);
4644 i = (i + 1) & RX_QUEUE_MASK;
4645 }
4646
4647 /* Backtrack one entry */
4648 priv->rxq.read = i;
4649 iwl_rx_queue_restock(priv);
4650}
4651
4652int iwl_tx_queue_update_write_ptr(struct iwl_priv *priv,
4653 struct iwl_tx_queue *txq)
4654{
4655 u32 reg = 0;
4656 int rc = 0;
4657 int txq_id = txq->q.id;
4658
4659 if (txq->need_update == 0)
4660 return rc;
4661
4662 /* if we're trying to save power */
4663 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4664 /* wake up nic if it's powered down ...
4665 * uCode will wake up, and interrupt us again, so next
4666 * time we'll skip this part. */
4667 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4668
4669 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4670 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
4671 iwl_set_bit(priv, CSR_GP_CNTRL,
4672 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4673 return rc;
4674 }
4675
4676 /* restore this queue's parameters in nic hardware. */
4677 rc = iwl_grab_restricted_access(priv);
4678 if (rc)
4679 return rc;
4680 iwl_write_restricted(priv, HBUS_TARG_WRPTR,
4681 txq->q.first_empty | (txq_id << 8));
4682 iwl_release_restricted_access(priv);
4683
4684 /* else not in power-save mode, uCode will never sleep when we're
4685 * trying to tx (during RFKILL, we're not trying to tx). */
4686 } else
4687 iwl_write32(priv, HBUS_TARG_WRPTR,
4688 txq->q.first_empty | (txq_id << 8));
4689
4690 txq->need_update = 0;
4691
4692 return rc;
4693}
4694
4695#ifdef CONFIG_IWLWIFI_DEBUG
4696static void iwl_print_rx_config_cmd(struct iwl_rxon_cmd *rxon)
4697{
Joe Perches0795af52007-10-03 17:59:30 -07004698 DECLARE_MAC_BUF(mac);
4699
Zhu Yib481de92007-09-25 17:54:57 -07004700 IWL_DEBUG_RADIO("RX CONFIG:\n");
4701 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
4702 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4703 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4704 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4705 le32_to_cpu(rxon->filter_flags));
4706 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4707 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4708 rxon->ofdm_basic_rates);
4709 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004710 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4711 print_mac(mac, rxon->node_addr));
4712 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4713 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004714 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4715}
4716#endif
4717
4718static void iwl_enable_interrupts(struct iwl_priv *priv)
4719{
4720 IWL_DEBUG_ISR("Enabling interrupts\n");
4721 set_bit(STATUS_INT_ENABLED, &priv->status);
4722 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
4723}
4724
4725static inline void iwl_disable_interrupts(struct iwl_priv *priv)
4726{
4727 clear_bit(STATUS_INT_ENABLED, &priv->status);
4728
4729 /* disable interrupts from uCode/NIC to host */
4730 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
4731
4732 /* acknowledge/clear/reset any interrupts still pending
4733 * from uCode or flow handler (Rx/Tx DMA) */
4734 iwl_write32(priv, CSR_INT, 0xffffffff);
4735 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
4736 IWL_DEBUG_ISR("Disabled interrupts\n");
4737}
4738
4739static const char *desc_lookup(int i)
4740{
4741 switch (i) {
4742 case 1:
4743 return "FAIL";
4744 case 2:
4745 return "BAD_PARAM";
4746 case 3:
4747 return "BAD_CHECKSUM";
4748 case 4:
4749 return "NMI_INTERRUPT";
4750 case 5:
4751 return "SYSASSERT";
4752 case 6:
4753 return "FATAL_ERROR";
4754 }
4755
4756 return "UNKNOWN";
4757}
4758
4759#define ERROR_START_OFFSET (1 * sizeof(u32))
4760#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4761
4762static void iwl_dump_nic_error_log(struct iwl_priv *priv)
4763{
4764 u32 data2, line;
4765 u32 desc, time, count, base, data1;
4766 u32 blink1, blink2, ilink1, ilink2;
4767 int rc;
4768
4769 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4770
4771 if (!iwl_hw_valid_rtc_data_addr(base)) {
4772 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4773 return;
4774 }
4775
4776 rc = iwl_grab_restricted_access(priv);
4777 if (rc) {
4778 IWL_WARNING("Can not read from adapter at this time.\n");
4779 return;
4780 }
4781
4782 count = iwl_read_restricted_mem(priv, base);
4783
4784 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4785 IWL_ERROR("Start IWL Error Log Dump:\n");
4786 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4787 priv->status, priv->config, count);
4788 }
4789
4790 desc = iwl_read_restricted_mem(priv, base + 1 * sizeof(u32));
4791 blink1 = iwl_read_restricted_mem(priv, base + 3 * sizeof(u32));
4792 blink2 = iwl_read_restricted_mem(priv, base + 4 * sizeof(u32));
4793 ilink1 = iwl_read_restricted_mem(priv, base + 5 * sizeof(u32));
4794 ilink2 = iwl_read_restricted_mem(priv, base + 6 * sizeof(u32));
4795 data1 = iwl_read_restricted_mem(priv, base + 7 * sizeof(u32));
4796 data2 = iwl_read_restricted_mem(priv, base + 8 * sizeof(u32));
4797 line = iwl_read_restricted_mem(priv, base + 9 * sizeof(u32));
4798 time = iwl_read_restricted_mem(priv, base + 11 * sizeof(u32));
4799
4800 IWL_ERROR("Desc Time "
4801 "data1 data2 line\n");
4802 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4803 desc_lookup(desc), desc, time, data1, data2, line);
4804 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4805 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4806 ilink1, ilink2);
4807
4808 iwl_release_restricted_access(priv);
4809}
4810
4811#define EVENT_START_OFFSET (4 * sizeof(u32))
4812
4813/**
4814 * iwl_print_event_log - Dump error event log to syslog
4815 *
4816 * NOTE: Must be called with iwl_grab_restricted_access() already obtained!
4817 */
4818static void iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
4819 u32 num_events, u32 mode)
4820{
4821 u32 i;
4822 u32 base; /* SRAM byte address of event log header */
4823 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4824 u32 ptr; /* SRAM byte address of log data */
4825 u32 ev, time, data; /* event log data */
4826
4827 if (num_events == 0)
4828 return;
4829
4830 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4831
4832 if (mode == 0)
4833 event_size = 2 * sizeof(u32);
4834 else
4835 event_size = 3 * sizeof(u32);
4836
4837 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4838
4839 /* "time" is actually "data" for mode 0 (no timestamp).
4840 * place event id # at far right for easier visual parsing. */
4841 for (i = 0; i < num_events; i++) {
4842 ev = iwl_read_restricted_mem(priv, ptr);
4843 ptr += sizeof(u32);
4844 time = iwl_read_restricted_mem(priv, ptr);
4845 ptr += sizeof(u32);
4846 if (mode == 0)
4847 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4848 else {
4849 data = iwl_read_restricted_mem(priv, ptr);
4850 ptr += sizeof(u32);
4851 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4852 }
4853 }
4854}
4855
4856static void iwl_dump_nic_event_log(struct iwl_priv *priv)
4857{
4858 int rc;
4859 u32 base; /* SRAM byte address of event log header */
4860 u32 capacity; /* event log capacity in # entries */
4861 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4862 u32 num_wraps; /* # times uCode wrapped to top of log */
4863 u32 next_entry; /* index of next entry to be written by uCode */
4864 u32 size; /* # entries that we'll print */
4865
4866 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4867 if (!iwl_hw_valid_rtc_data_addr(base)) {
4868 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4869 return;
4870 }
4871
4872 rc = iwl_grab_restricted_access(priv);
4873 if (rc) {
4874 IWL_WARNING("Can not read from adapter at this time.\n");
4875 return;
4876 }
4877
4878 /* event log header */
4879 capacity = iwl_read_restricted_mem(priv, base);
4880 mode = iwl_read_restricted_mem(priv, base + (1 * sizeof(u32)));
4881 num_wraps = iwl_read_restricted_mem(priv, base + (2 * sizeof(u32)));
4882 next_entry = iwl_read_restricted_mem(priv, base + (3 * sizeof(u32)));
4883
4884 size = num_wraps ? capacity : next_entry;
4885
4886 /* bail out if nothing in log */
4887 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004888 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Zhu Yib481de92007-09-25 17:54:57 -07004889 iwl_release_restricted_access(priv);
4890 return;
4891 }
4892
Zhu Yi583fab32007-09-27 11:27:30 +08004893 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004894 size, num_wraps);
4895
4896 /* if uCode has wrapped back to top of log, start at the oldest entry,
4897 * i.e the next one that uCode would fill. */
4898 if (num_wraps)
4899 iwl_print_event_log(priv, next_entry,
4900 capacity - next_entry, mode);
4901
4902 /* (then/else) start at top of log */
4903 iwl_print_event_log(priv, 0, next_entry, mode);
4904
4905 iwl_release_restricted_access(priv);
4906}
4907
4908/**
4909 * iwl_irq_handle_error - called for HW or SW error interrupt from card
4910 */
4911static void iwl_irq_handle_error(struct iwl_priv *priv)
4912{
4913 /* Set the FW error flag -- cleared on iwl_down */
4914 set_bit(STATUS_FW_ERROR, &priv->status);
4915
4916 /* Cancel currently queued command. */
4917 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4918
4919#ifdef CONFIG_IWLWIFI_DEBUG
4920 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
4921 iwl_dump_nic_error_log(priv);
4922 iwl_dump_nic_event_log(priv);
4923 iwl_print_rx_config_cmd(&priv->staging_rxon);
4924 }
4925#endif
4926
4927 wake_up_interruptible(&priv->wait_command_queue);
4928
4929 /* Keep the restart process from trying to send host
4930 * commands by clearing the INIT status bit */
4931 clear_bit(STATUS_READY, &priv->status);
4932
4933 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4934 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4935 "Restarting adapter due to uCode error.\n");
4936
4937 if (iwl_is_associated(priv)) {
4938 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4939 sizeof(priv->recovery_rxon));
4940 priv->error_recovering = 1;
4941 }
4942 queue_work(priv->workqueue, &priv->restart);
4943 }
4944}
4945
4946static void iwl_error_recovery(struct iwl_priv *priv)
4947{
4948 unsigned long flags;
4949
4950 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4951 sizeof(priv->staging_rxon));
4952 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4953 iwl_commit_rxon(priv);
4954
4955 iwl_rxon_add_station(priv, priv->bssid, 1);
4956
4957 spin_lock_irqsave(&priv->lock, flags);
4958 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4959 priv->error_recovering = 0;
4960 spin_unlock_irqrestore(&priv->lock, flags);
4961}
4962
4963static void iwl_irq_tasklet(struct iwl_priv *priv)
4964{
4965 u32 inta, handled = 0;
4966 u32 inta_fh;
4967 unsigned long flags;
4968#ifdef CONFIG_IWLWIFI_DEBUG
4969 u32 inta_mask;
4970#endif
4971
4972 spin_lock_irqsave(&priv->lock, flags);
4973
4974 /* Ack/clear/reset pending uCode interrupts.
4975 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4976 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
4977 inta = iwl_read32(priv, CSR_INT);
4978 iwl_write32(priv, CSR_INT, inta);
4979
4980 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4981 * Any new interrupts that happen after this, either while we're
4982 * in this tasklet, or later, will show up in next ISR/tasklet. */
4983 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4984 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
4985
4986#ifdef CONFIG_IWLWIFI_DEBUG
4987 if (iwl_debug_level & IWL_DL_ISR) {
4988 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4989 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4990 inta, inta_mask, inta_fh);
4991 }
4992#endif
4993
4994 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4995 * atomic, make sure that inta covers all the interrupts that
4996 * we've discovered, even if FH interrupt came in just after
4997 * reading CSR_INT. */
4998 if (inta_fh & CSR_FH_INT_RX_MASK)
4999 inta |= CSR_INT_BIT_FH_RX;
5000 if (inta_fh & CSR_FH_INT_TX_MASK)
5001 inta |= CSR_INT_BIT_FH_TX;
5002
5003 /* Now service all interrupt bits discovered above. */
5004 if (inta & CSR_INT_BIT_HW_ERR) {
5005 IWL_ERROR("Microcode HW error detected. Restarting.\n");
5006
5007 /* Tell the device to stop sending interrupts */
5008 iwl_disable_interrupts(priv);
5009
5010 iwl_irq_handle_error(priv);
5011
5012 handled |= CSR_INT_BIT_HW_ERR;
5013
5014 spin_unlock_irqrestore(&priv->lock, flags);
5015
5016 return;
5017 }
5018
5019#ifdef CONFIG_IWLWIFI_DEBUG
5020 if (iwl_debug_level & (IWL_DL_ISR)) {
5021 /* NIC fires this, but we don't use it, redundant with WAKEUP */
5022 if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
5023 IWL_DEBUG_ISR("Microcode started or stopped.\n");
5024
5025 /* Alive notification via Rx interrupt will do the real work */
5026 if (inta & CSR_INT_BIT_ALIVE)
5027 IWL_DEBUG_ISR("Alive interrupt\n");
5028 }
5029#endif
5030 /* Safely ignore these bits for debug checks below */
5031 inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
5032
5033 /* HW RF KILL switch toggled (4965 only) */
5034 if (inta & CSR_INT_BIT_RF_KILL) {
5035 int hw_rf_kill = 0;
5036 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
5037 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
5038 hw_rf_kill = 1;
5039
5040 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
5041 "RF_KILL bit toggled to %s.\n",
5042 hw_rf_kill ? "disable radio":"enable radio");
5043
5044 /* Queue restart only if RF_KILL switch was set to "kill"
5045 * when we loaded driver, and is now set to "enable".
5046 * After we're Alive, RF_KILL gets handled by
5047 * iwl_rx_card_state_notif() */
5048 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status))
5049 queue_work(priv->workqueue, &priv->restart);
5050
5051 handled |= CSR_INT_BIT_RF_KILL;
5052 }
5053
5054 /* Chip got too hot and stopped itself (4965 only) */
5055 if (inta & CSR_INT_BIT_CT_KILL) {
5056 IWL_ERROR("Microcode CT kill error detected.\n");
5057 handled |= CSR_INT_BIT_CT_KILL;
5058 }
5059
5060 /* Error detected by uCode */
5061 if (inta & CSR_INT_BIT_SW_ERR) {
5062 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
5063 inta);
5064 iwl_irq_handle_error(priv);
5065 handled |= CSR_INT_BIT_SW_ERR;
5066 }
5067
5068 /* uCode wakes up after power-down sleep */
5069 if (inta & CSR_INT_BIT_WAKEUP) {
5070 IWL_DEBUG_ISR("Wakeup interrupt\n");
5071 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
5072 iwl_tx_queue_update_write_ptr(priv, &priv->txq[0]);
5073 iwl_tx_queue_update_write_ptr(priv, &priv->txq[1]);
5074 iwl_tx_queue_update_write_ptr(priv, &priv->txq[2]);
5075 iwl_tx_queue_update_write_ptr(priv, &priv->txq[3]);
5076 iwl_tx_queue_update_write_ptr(priv, &priv->txq[4]);
5077 iwl_tx_queue_update_write_ptr(priv, &priv->txq[5]);
5078
5079 handled |= CSR_INT_BIT_WAKEUP;
5080 }
5081
5082 /* All uCode command responses, including Tx command responses,
5083 * Rx "responses" (frame-received notification), and other
5084 * notifications from uCode come through here*/
5085 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
5086 iwl_rx_handle(priv);
5087 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
5088 }
5089
5090 if (inta & CSR_INT_BIT_FH_TX) {
5091 IWL_DEBUG_ISR("Tx interrupt\n");
5092 handled |= CSR_INT_BIT_FH_TX;
5093 }
5094
5095 if (inta & ~handled)
5096 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
5097
5098 if (inta & ~CSR_INI_SET_MASK) {
5099 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
5100 inta & ~CSR_INI_SET_MASK);
5101 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
5102 }
5103
5104 /* Re-enable all interrupts */
5105 iwl_enable_interrupts(priv);
5106
5107#ifdef CONFIG_IWLWIFI_DEBUG
5108 if (iwl_debug_level & (IWL_DL_ISR)) {
5109 inta = iwl_read32(priv, CSR_INT);
5110 inta_mask = iwl_read32(priv, CSR_INT_MASK);
5111 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
5112 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
5113 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
5114 }
5115#endif
5116 spin_unlock_irqrestore(&priv->lock, flags);
5117}
5118
5119static irqreturn_t iwl_isr(int irq, void *data)
5120{
5121 struct iwl_priv *priv = data;
5122 u32 inta, inta_mask;
5123 u32 inta_fh;
5124 if (!priv)
5125 return IRQ_NONE;
5126
5127 spin_lock(&priv->lock);
5128
5129 /* Disable (but don't clear!) interrupts here to avoid
5130 * back-to-back ISRs and sporadic interrupts from our NIC.
5131 * If we have something to service, the tasklet will re-enable ints.
5132 * If we *don't* have something, we'll re-enable before leaving here. */
5133 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
5134 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
5135
5136 /* Discover which interrupts are active/pending */
5137 inta = iwl_read32(priv, CSR_INT);
5138 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
5139
5140 /* Ignore interrupt if there's nothing in NIC to service.
5141 * This may be due to IRQ shared with another device,
5142 * or due to sporadic interrupts thrown from our NIC. */
5143 if (!inta && !inta_fh) {
5144 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
5145 goto none;
5146 }
5147
5148 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
5149 /* Hardware disappeared */
5150 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
5151 goto none;
5152 }
5153
5154 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5155 inta, inta_mask, inta_fh);
5156
5157 /* iwl_irq_tasklet() will service interrupts and re-enable them */
5158 tasklet_schedule(&priv->irq_tasklet);
5159 spin_unlock(&priv->lock);
5160
5161 return IRQ_HANDLED;
5162
5163 none:
5164 /* re-enable interrupts here since we don't have anything to service. */
5165 iwl_enable_interrupts(priv);
5166 spin_unlock(&priv->lock);
5167 return IRQ_NONE;
5168}
5169
5170/************************** EEPROM BANDS ****************************
5171 *
5172 * The iwl_eeprom_band definitions below provide the mapping from the
5173 * EEPROM contents to the specific channel number supported for each
5174 * band.
5175 *
5176 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
5177 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5178 * The specific geography and calibration information for that channel
5179 * is contained in the eeprom map itself.
5180 *
5181 * During init, we copy the eeprom information and channel map
5182 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5183 *
5184 * channel_map_24/52 provides the index in the channel_info array for a
5185 * given channel. We have to have two separate maps as there is channel
5186 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5187 * band_2
5188 *
5189 * A value of 0xff stored in the channel_map indicates that the channel
5190 * is not supported by the hardware at all.
5191 *
5192 * A value of 0xfe in the channel_map indicates that the channel is not
5193 * valid for Tx with the current hardware. This means that
5194 * while the system can tune and receive on a given channel, it may not
5195 * be able to associate or transmit any frames on that
5196 * channel. There is no corresponding channel information for that
5197 * entry.
5198 *
5199 *********************************************************************/
5200
5201/* 2.4 GHz */
5202static const u8 iwl_eeprom_band_1[14] = {
5203 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5204};
5205
5206/* 5.2 GHz bands */
5207static const u8 iwl_eeprom_band_2[] = {
5208 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5209};
5210
5211static const u8 iwl_eeprom_band_3[] = { /* 5205-5320MHz */
5212 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5213};
5214
5215static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
5216 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5217};
5218
5219static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
5220 145, 149, 153, 157, 161, 165
5221};
5222
5223static u8 iwl_eeprom_band_6[] = { /* 2.4 FAT channel */
5224 1, 2, 3, 4, 5, 6, 7
5225};
5226
5227static u8 iwl_eeprom_band_7[] = { /* 5.2 FAT channel */
5228 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5229};
5230
5231static void iwl_init_band_reference(const struct iwl_priv *priv, int band,
5232 int *eeprom_ch_count,
5233 const struct iwl_eeprom_channel
5234 **eeprom_ch_info,
5235 const u8 **eeprom_ch_index)
5236{
5237 switch (band) {
5238 case 1: /* 2.4GHz band */
5239 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
5240 *eeprom_ch_info = priv->eeprom.band_1_channels;
5241 *eeprom_ch_index = iwl_eeprom_band_1;
5242 break;
5243 case 2: /* 5.2GHz band */
5244 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
5245 *eeprom_ch_info = priv->eeprom.band_2_channels;
5246 *eeprom_ch_index = iwl_eeprom_band_2;
5247 break;
5248 case 3: /* 5.2GHz band */
5249 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
5250 *eeprom_ch_info = priv->eeprom.band_3_channels;
5251 *eeprom_ch_index = iwl_eeprom_band_3;
5252 break;
5253 case 4: /* 5.2GHz band */
5254 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
5255 *eeprom_ch_info = priv->eeprom.band_4_channels;
5256 *eeprom_ch_index = iwl_eeprom_band_4;
5257 break;
5258 case 5: /* 5.2GHz band */
5259 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
5260 *eeprom_ch_info = priv->eeprom.band_5_channels;
5261 *eeprom_ch_index = iwl_eeprom_band_5;
5262 break;
5263 case 6:
5264 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_6);
5265 *eeprom_ch_info = priv->eeprom.band_24_channels;
5266 *eeprom_ch_index = iwl_eeprom_band_6;
5267 break;
5268 case 7:
5269 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_7);
5270 *eeprom_ch_info = priv->eeprom.band_52_channels;
5271 *eeprom_ch_index = iwl_eeprom_band_7;
5272 break;
5273 default:
5274 BUG();
5275 return;
5276 }
5277}
5278
5279const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
5280 int phymode, u16 channel)
5281{
5282 int i;
5283
5284 switch (phymode) {
5285 case MODE_IEEE80211A:
5286 for (i = 14; i < priv->channel_count; i++) {
5287 if (priv->channel_info[i].channel == channel)
5288 return &priv->channel_info[i];
5289 }
5290 break;
5291
5292 case MODE_IEEE80211B:
5293 case MODE_IEEE80211G:
5294 if (channel >= 1 && channel <= 14)
5295 return &priv->channel_info[channel - 1];
5296 break;
5297
5298 }
5299
5300 return NULL;
5301}
5302
5303#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5304 ? # x " " : "")
5305
5306static int iwl_init_channel_map(struct iwl_priv *priv)
5307{
5308 int eeprom_ch_count = 0;
5309 const u8 *eeprom_ch_index = NULL;
5310 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
5311 int band, ch;
5312 struct iwl_channel_info *ch_info;
5313
5314 if (priv->channel_count) {
5315 IWL_DEBUG_INFO("Channel map already initialized.\n");
5316 return 0;
5317 }
5318
5319 if (priv->eeprom.version < 0x2f) {
5320 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5321 priv->eeprom.version);
5322 return -EINVAL;
5323 }
5324
5325 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5326
5327 priv->channel_count =
5328 ARRAY_SIZE(iwl_eeprom_band_1) +
5329 ARRAY_SIZE(iwl_eeprom_band_2) +
5330 ARRAY_SIZE(iwl_eeprom_band_3) +
5331 ARRAY_SIZE(iwl_eeprom_band_4) +
5332 ARRAY_SIZE(iwl_eeprom_band_5);
5333
5334 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5335
5336 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
5337 priv->channel_count, GFP_KERNEL);
5338 if (!priv->channel_info) {
5339 IWL_ERROR("Could not allocate channel_info\n");
5340 priv->channel_count = 0;
5341 return -ENOMEM;
5342 }
5343
5344 ch_info = priv->channel_info;
5345
5346 /* Loop through the 5 EEPROM bands adding them in order to the
5347 * channel map we maintain (that contains additional information than
5348 * what just in the EEPROM) */
5349 for (band = 1; band <= 5; band++) {
5350
5351 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5352 &eeprom_ch_info, &eeprom_ch_index);
5353
5354 /* Loop through each band adding each of the channels */
5355 for (ch = 0; ch < eeprom_ch_count; ch++) {
5356 ch_info->channel = eeprom_ch_index[ch];
5357 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5358 MODE_IEEE80211A;
5359
5360 /* permanently store EEPROM's channel regulatory flags
5361 * and max power in channel info database. */
5362 ch_info->eeprom = eeprom_ch_info[ch];
5363
5364 /* Copy the run-time flags so they are there even on
5365 * invalid channels */
5366 ch_info->flags = eeprom_ch_info[ch].flags;
5367
5368 if (!(is_channel_valid(ch_info))) {
5369 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5370 "No traffic\n",
5371 ch_info->channel,
5372 ch_info->flags,
5373 is_channel_a_band(ch_info) ?
5374 "5.2" : "2.4");
5375 ch_info++;
5376 continue;
5377 }
5378
5379 /* Initialize regulatory-based run-time data */
5380 ch_info->max_power_avg = ch_info->curr_txpow =
5381 eeprom_ch_info[ch].max_power_avg;
5382 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5383 ch_info->min_power = 0;
5384
5385 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5386 " %ddBm): Ad-Hoc %ssupported\n",
5387 ch_info->channel,
5388 is_channel_a_band(ch_info) ?
5389 "5.2" : "2.4",
5390 CHECK_AND_PRINT(IBSS),
5391 CHECK_AND_PRINT(ACTIVE),
5392 CHECK_AND_PRINT(RADAR),
5393 CHECK_AND_PRINT(WIDE),
5394 CHECK_AND_PRINT(NARROW),
5395 CHECK_AND_PRINT(DFS),
5396 eeprom_ch_info[ch].flags,
5397 eeprom_ch_info[ch].max_power_avg,
5398 ((eeprom_ch_info[ch].
5399 flags & EEPROM_CHANNEL_IBSS)
5400 && !(eeprom_ch_info[ch].
5401 flags & EEPROM_CHANNEL_RADAR))
5402 ? "" : "not ");
5403
5404 /* Set the user_txpower_limit to the highest power
5405 * supported by any channel */
5406 if (eeprom_ch_info[ch].max_power_avg >
5407 priv->user_txpower_limit)
5408 priv->user_txpower_limit =
5409 eeprom_ch_info[ch].max_power_avg;
5410
5411 ch_info++;
5412 }
5413 }
5414
5415 for (band = 6; band <= 7; band++) {
5416 int phymode;
5417 u8 fat_extension_chan;
5418
5419 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5420 &eeprom_ch_info, &eeprom_ch_index);
5421
5422 phymode = (band == 6) ? MODE_IEEE80211B : MODE_IEEE80211A;
5423 /* Loop through each band adding each of the channels */
5424 for (ch = 0; ch < eeprom_ch_count; ch++) {
5425
5426 if ((band == 6) &&
5427 ((eeprom_ch_index[ch] == 5) ||
5428 (eeprom_ch_index[ch] == 6) ||
5429 (eeprom_ch_index[ch] == 7)))
5430 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5431 else
5432 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5433
5434 iwl4965_set_fat_chan_info(priv, phymode,
5435 eeprom_ch_index[ch],
5436 &(eeprom_ch_info[ch]),
5437 fat_extension_chan);
5438
5439 iwl4965_set_fat_chan_info(priv, phymode,
5440 (eeprom_ch_index[ch] + 4),
5441 &(eeprom_ch_info[ch]),
5442 HT_IE_EXT_CHANNEL_BELOW);
5443 }
5444 }
5445
5446 return 0;
5447}
5448
5449/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5450 * sending probe req. This should be set long enough to hear probe responses
5451 * from more than one AP. */
5452#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5453#define IWL_ACTIVE_DWELL_TIME_52 (10)
5454
5455/* For faster active scanning, scan will move to the next channel if fewer than
5456 * PLCP_QUIET_THRESH packets are heard on this channel within
5457 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5458 * time if it's a quiet channel (nothing responded to our probe, and there's
5459 * no other traffic).
5460 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5461#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5462#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5463
5464/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5465 * Must be set longer than active dwell time.
5466 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5467#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5468#define IWL_PASSIVE_DWELL_TIME_52 (10)
5469#define IWL_PASSIVE_DWELL_BASE (100)
5470#define IWL_CHANNEL_TUNE_TIME 5
5471
5472static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv, int phymode)
5473{
5474 if (phymode == MODE_IEEE80211A)
5475 return IWL_ACTIVE_DWELL_TIME_52;
5476 else
5477 return IWL_ACTIVE_DWELL_TIME_24;
5478}
5479
5480static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, int phymode)
5481{
5482 u16 active = iwl_get_active_dwell_time(priv, phymode);
5483 u16 passive = (phymode != MODE_IEEE80211A) ?
5484 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5485 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5486
5487 if (iwl_is_associated(priv)) {
5488 /* If we're associated, we clamp the maximum passive
5489 * dwell time to be 98% of the beacon interval (minus
5490 * 2 * channel tune time) */
5491 passive = priv->beacon_int;
5492 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5493 passive = IWL_PASSIVE_DWELL_BASE;
5494 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5495 }
5496
5497 if (passive <= active)
5498 passive = active + 1;
5499
5500 return passive;
5501}
5502
5503static int iwl_get_channels_for_scan(struct iwl_priv *priv, int phymode,
5504 u8 is_active, u8 direct_mask,
5505 struct iwl_scan_channel *scan_ch)
5506{
5507 const struct ieee80211_channel *channels = NULL;
5508 const struct ieee80211_hw_mode *hw_mode;
5509 const struct iwl_channel_info *ch_info;
5510 u16 passive_dwell = 0;
5511 u16 active_dwell = 0;
5512 int added, i;
5513
5514 hw_mode = iwl_get_hw_mode(priv, phymode);
5515 if (!hw_mode)
5516 return 0;
5517
5518 channels = hw_mode->channels;
5519
5520 active_dwell = iwl_get_active_dwell_time(priv, phymode);
5521 passive_dwell = iwl_get_passive_dwell_time(priv, phymode);
5522
5523 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5524 if (channels[i].chan ==
5525 le16_to_cpu(priv->active_rxon.channel)) {
5526 if (iwl_is_associated(priv)) {
5527 IWL_DEBUG_SCAN
5528 ("Skipping current channel %d\n",
5529 le16_to_cpu(priv->active_rxon.channel));
5530 continue;
5531 }
5532 } else if (priv->only_active_channel)
5533 continue;
5534
5535 scan_ch->channel = channels[i].chan;
5536
5537 ch_info = iwl_get_channel_info(priv, phymode, scan_ch->channel);
5538 if (!is_channel_valid(ch_info)) {
5539 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5540 scan_ch->channel);
5541 continue;
5542 }
5543
5544 if (!is_active || is_channel_passive(ch_info) ||
5545 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5546 scan_ch->type = 0; /* passive */
5547 else
5548 scan_ch->type = 1; /* active */
5549
5550 if (scan_ch->type & 1)
5551 scan_ch->type |= (direct_mask << 1);
5552
5553 if (is_channel_narrow(ch_info))
5554 scan_ch->type |= (1 << 7);
5555
5556 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5557 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5558
5559 /* Set power levels to defaults */
5560 scan_ch->tpc.dsp_atten = 110;
5561 /* scan_pwr_info->tpc.dsp_atten; */
5562
5563 /*scan_pwr_info->tpc.tx_gain; */
5564 if (phymode == MODE_IEEE80211A)
5565 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5566 else {
5567 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5568 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
5569 * power level
5570 scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
5571 */
5572 }
5573
5574 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5575 scan_ch->channel,
5576 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5577 (scan_ch->type & 1) ?
5578 active_dwell : passive_dwell);
5579
5580 scan_ch++;
5581 added++;
5582 }
5583
5584 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5585 return added;
5586}
5587
5588static void iwl_reset_channel_flag(struct iwl_priv *priv)
5589{
5590 int i, j;
5591 for (i = 0; i < 3; i++) {
5592 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5593 for (j = 0; j < hw_mode->num_channels; j++)
5594 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5595 }
5596}
5597
5598static void iwl_init_hw_rates(struct iwl_priv *priv,
5599 struct ieee80211_rate *rates)
5600{
5601 int i;
5602
5603 for (i = 0; i < IWL_RATE_COUNT; i++) {
5604 rates[i].rate = iwl_rates[i].ieee * 5;
5605 rates[i].val = i; /* Rate scaling will work on indexes */
5606 rates[i].val2 = i;
5607 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5608 /* Only OFDM have the bits-per-symbol set */
5609 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5610 rates[i].flags |= IEEE80211_RATE_OFDM;
5611 else {
5612 /*
5613 * If CCK 1M then set rate flag to CCK else CCK_2
5614 * which is CCK | PREAMBLE2
5615 */
5616 rates[i].flags |= (iwl_rates[i].plcp == 10) ?
5617 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5618 }
5619
5620 /* Set up which ones are basic rates... */
5621 if (IWL_BASIC_RATES_MASK & (1 << i))
5622 rates[i].flags |= IEEE80211_RATE_BASIC;
5623 }
5624
5625 iwl4965_init_hw_rates(priv, rates);
5626}
5627
5628/**
5629 * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
5630 */
5631static int iwl_init_geos(struct iwl_priv *priv)
5632{
5633 struct iwl_channel_info *ch;
5634 struct ieee80211_hw_mode *modes;
5635 struct ieee80211_channel *channels;
5636 struct ieee80211_channel *geo_ch;
5637 struct ieee80211_rate *rates;
5638 int i = 0;
5639 enum {
5640 A = 0,
5641 B = 1,
5642 G = 2,
5643 A_11N = 3,
5644 G_11N = 4,
5645 };
5646 int mode_count = 5;
5647
5648 if (priv->modes) {
5649 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5650 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5651 return 0;
5652 }
5653
5654 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5655 GFP_KERNEL);
5656 if (!modes)
5657 return -ENOMEM;
5658
5659 channels = kzalloc(sizeof(struct ieee80211_channel) *
5660 priv->channel_count, GFP_KERNEL);
5661 if (!channels) {
5662 kfree(modes);
5663 return -ENOMEM;
5664 }
5665
5666 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5667 GFP_KERNEL);
5668 if (!rates) {
5669 kfree(modes);
5670 kfree(channels);
5671 return -ENOMEM;
5672 }
5673
5674 /* 0 = 802.11a
5675 * 1 = 802.11b
5676 * 2 = 802.11g
5677 */
5678
5679 /* 5.2GHz channels start after the 2.4GHz channels */
5680 modes[A].mode = MODE_IEEE80211A;
5681 modes[A].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
5682 modes[A].rates = rates;
5683 modes[A].num_rates = 8; /* just OFDM */
5684 modes[A].rates = &rates[4];
5685 modes[A].num_channels = 0;
5686
5687 modes[B].mode = MODE_IEEE80211B;
5688 modes[B].channels = channels;
5689 modes[B].rates = rates;
5690 modes[B].num_rates = 4; /* just CCK */
5691 modes[B].num_channels = 0;
5692
5693 modes[G].mode = MODE_IEEE80211G;
5694 modes[G].channels = channels;
5695 modes[G].rates = rates;
5696 modes[G].num_rates = 12; /* OFDM & CCK */
5697 modes[G].num_channels = 0;
5698
5699 modes[G_11N].mode = MODE_IEEE80211G;
5700 modes[G_11N].channels = channels;
5701 modes[G_11N].num_rates = 13; /* OFDM & CCK */
5702 modes[G_11N].rates = rates;
5703 modes[G_11N].num_channels = 0;
5704
5705 modes[A_11N].mode = MODE_IEEE80211A;
5706 modes[A_11N].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
5707 modes[A_11N].rates = &rates[4];
5708 modes[A_11N].num_rates = 9; /* just OFDM */
5709 modes[A_11N].num_channels = 0;
5710
5711 priv->ieee_channels = channels;
5712 priv->ieee_rates = rates;
5713
5714 iwl_init_hw_rates(priv, rates);
5715
5716 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5717 ch = &priv->channel_info[i];
5718
5719 if (!is_channel_valid(ch)) {
5720 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5721 "skipping.\n",
5722 ch->channel, is_channel_a_band(ch) ?
5723 "5.2" : "2.4");
5724 continue;
5725 }
5726
5727 if (is_channel_a_band(ch)) {
5728 geo_ch = &modes[A].channels[modes[A].num_channels++];
5729 modes[A_11N].num_channels++;
5730 } else {
5731 geo_ch = &modes[B].channels[modes[B].num_channels++];
5732 modes[G].num_channels++;
5733 modes[G_11N].num_channels++;
5734 }
5735
5736 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5737 geo_ch->chan = ch->channel;
5738 geo_ch->power_level = ch->max_power_avg;
5739 geo_ch->antenna_max = 0xff;
5740
5741 if (is_channel_valid(ch)) {
5742 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5743 if (ch->flags & EEPROM_CHANNEL_IBSS)
5744 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5745
5746 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5747 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5748
5749 if (ch->flags & EEPROM_CHANNEL_RADAR)
5750 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5751
5752 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5753 priv->max_channel_txpower_limit =
5754 ch->max_power_avg;
5755 }
5756
5757 geo_ch->val = geo_ch->flag;
5758 }
5759
5760 if ((modes[A].num_channels == 0) && priv->is_abg) {
5761 printk(KERN_INFO DRV_NAME
5762 ": Incorrectly detected BG card as ABG. Please send "
5763 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5764 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5765 priv->is_abg = 0;
5766 }
5767
5768 printk(KERN_INFO DRV_NAME
5769 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5770 modes[G].num_channels, modes[A].num_channels);
5771
5772 /*
5773 * NOTE: We register these in preference of order -- the
5774 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5775 * a phymode based on rates or AP capabilities but seems to
5776 * configure it purely on if the channel being configured
5777 * is supported by a mode -- and the first match is taken
5778 */
5779
5780 if (modes[G].num_channels)
5781 ieee80211_register_hwmode(priv->hw, &modes[G]);
5782 if (modes[B].num_channels)
5783 ieee80211_register_hwmode(priv->hw, &modes[B]);
5784 if (modes[A].num_channels)
5785 ieee80211_register_hwmode(priv->hw, &modes[A]);
5786
5787 priv->modes = modes;
5788 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5789
5790 return 0;
5791}
5792
5793/******************************************************************************
5794 *
5795 * uCode download functions
5796 *
5797 ******************************************************************************/
5798
5799static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
5800{
5801 if (priv->ucode_code.v_addr != NULL) {
5802 pci_free_consistent(priv->pci_dev,
5803 priv->ucode_code.len,
5804 priv->ucode_code.v_addr,
5805 priv->ucode_code.p_addr);
5806 priv->ucode_code.v_addr = NULL;
5807 }
5808 if (priv->ucode_data.v_addr != NULL) {
5809 pci_free_consistent(priv->pci_dev,
5810 priv->ucode_data.len,
5811 priv->ucode_data.v_addr,
5812 priv->ucode_data.p_addr);
5813 priv->ucode_data.v_addr = NULL;
5814 }
5815 if (priv->ucode_data_backup.v_addr != NULL) {
5816 pci_free_consistent(priv->pci_dev,
5817 priv->ucode_data_backup.len,
5818 priv->ucode_data_backup.v_addr,
5819 priv->ucode_data_backup.p_addr);
5820 priv->ucode_data_backup.v_addr = NULL;
5821 }
5822 if (priv->ucode_init.v_addr != NULL) {
5823 pci_free_consistent(priv->pci_dev,
5824 priv->ucode_init.len,
5825 priv->ucode_init.v_addr,
5826 priv->ucode_init.p_addr);
5827 priv->ucode_init.v_addr = NULL;
5828 }
5829 if (priv->ucode_init_data.v_addr != NULL) {
5830 pci_free_consistent(priv->pci_dev,
5831 priv->ucode_init_data.len,
5832 priv->ucode_init_data.v_addr,
5833 priv->ucode_init_data.p_addr);
5834 priv->ucode_init_data.v_addr = NULL;
5835 }
5836 if (priv->ucode_boot.v_addr != NULL) {
5837 pci_free_consistent(priv->pci_dev,
5838 priv->ucode_boot.len,
5839 priv->ucode_boot.v_addr,
5840 priv->ucode_boot.p_addr);
5841 priv->ucode_boot.v_addr = NULL;
5842 }
5843}
5844
5845/**
5846 * iwl_verify_inst_full - verify runtime uCode image in card vs. host,
5847 * looking at all data.
5848 */
5849static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 * image, u32 len)
5850{
5851 u32 val;
5852 u32 save_len = len;
5853 int rc = 0;
5854 u32 errcnt;
5855
5856 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5857
5858 rc = iwl_grab_restricted_access(priv);
5859 if (rc)
5860 return rc;
5861
5862 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
5863
5864 errcnt = 0;
5865 for (; len > 0; len -= sizeof(u32), image++) {
5866 /* read data comes through single port, auto-incr addr */
5867 /* NOTE: Use the debugless read so we don't flood kernel log
5868 * if IWL_DL_IO is set */
5869 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5870 if (val != le32_to_cpu(*image)) {
5871 IWL_ERROR("uCode INST section is invalid at "
5872 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5873 save_len - len, val, le32_to_cpu(*image));
5874 rc = -EIO;
5875 errcnt++;
5876 if (errcnt >= 20)
5877 break;
5878 }
5879 }
5880
5881 iwl_release_restricted_access(priv);
5882
5883 if (!errcnt)
5884 IWL_DEBUG_INFO
5885 ("ucode image in INSTRUCTION memory is good\n");
5886
5887 return rc;
5888}
5889
5890
5891/**
5892 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
5893 * using sample data 100 bytes apart. If these sample points are good,
5894 * it's a pretty good bet that everything between them is good, too.
5895 */
5896static int iwl_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
5897{
5898 u32 val;
5899 int rc = 0;
5900 u32 errcnt = 0;
5901 u32 i;
5902
5903 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5904
5905 rc = iwl_grab_restricted_access(priv);
5906 if (rc)
5907 return rc;
5908
5909 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5910 /* read data comes through single port, auto-incr addr */
5911 /* NOTE: Use the debugless read so we don't flood kernel log
5912 * if IWL_DL_IO is set */
5913 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR,
5914 i + RTC_INST_LOWER_BOUND);
5915 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5916 if (val != le32_to_cpu(*image)) {
5917#if 0 /* Enable this if you want to see details */
5918 IWL_ERROR("uCode INST section is invalid at "
5919 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5920 i, val, *image);
5921#endif
5922 rc = -EIO;
5923 errcnt++;
5924 if (errcnt >= 3)
5925 break;
5926 }
5927 }
5928
5929 iwl_release_restricted_access(priv);
5930
5931 return rc;
5932}
5933
5934
5935/**
5936 * iwl_verify_ucode - determine which instruction image is in SRAM,
5937 * and verify its contents
5938 */
5939static int iwl_verify_ucode(struct iwl_priv *priv)
5940{
5941 __le32 *image;
5942 u32 len;
5943 int rc = 0;
5944
5945 /* Try bootstrap */
5946 image = (__le32 *)priv->ucode_boot.v_addr;
5947 len = priv->ucode_boot.len;
5948 rc = iwl_verify_inst_sparse(priv, image, len);
5949 if (rc == 0) {
5950 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5951 return 0;
5952 }
5953
5954 /* Try initialize */
5955 image = (__le32 *)priv->ucode_init.v_addr;
5956 len = priv->ucode_init.len;
5957 rc = iwl_verify_inst_sparse(priv, image, len);
5958 if (rc == 0) {
5959 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5960 return 0;
5961 }
5962
5963 /* Try runtime/protocol */
5964 image = (__le32 *)priv->ucode_code.v_addr;
5965 len = priv->ucode_code.len;
5966 rc = iwl_verify_inst_sparse(priv, image, len);
5967 if (rc == 0) {
5968 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5969 return 0;
5970 }
5971
5972 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5973
5974 /* Show first several data entries in instruction SRAM.
5975 * Selection of bootstrap image is arbitrary. */
5976 image = (__le32 *)priv->ucode_boot.v_addr;
5977 len = priv->ucode_boot.len;
5978 rc = iwl_verify_inst_full(priv, image, len);
5979
5980 return rc;
5981}
5982
5983
5984/* check contents of special bootstrap uCode SRAM */
5985static int iwl_verify_bsm(struct iwl_priv *priv)
5986{
5987 __le32 *image = priv->ucode_boot.v_addr;
5988 u32 len = priv->ucode_boot.len;
5989 u32 reg;
5990 u32 val;
5991
5992 IWL_DEBUG_INFO("Begin verify bsm\n");
5993
5994 /* verify BSM SRAM contents */
5995 val = iwl_read_restricted_reg(priv, BSM_WR_DWCOUNT_REG);
5996 for (reg = BSM_SRAM_LOWER_BOUND;
5997 reg < BSM_SRAM_LOWER_BOUND + len;
5998 reg += sizeof(u32), image ++) {
5999 val = iwl_read_restricted_reg(priv, reg);
6000 if (val != le32_to_cpu(*image)) {
6001 IWL_ERROR("BSM uCode verification failed at "
6002 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
6003 BSM_SRAM_LOWER_BOUND,
6004 reg - BSM_SRAM_LOWER_BOUND, len,
6005 val, le32_to_cpu(*image));
6006 return -EIO;
6007 }
6008 }
6009
6010 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
6011
6012 return 0;
6013}
6014
6015/**
6016 * iwl_load_bsm - Load bootstrap instructions
6017 *
6018 * BSM operation:
6019 *
6020 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
6021 * in special SRAM that does not power down during RFKILL. When powering back
6022 * up after power-saving sleeps (or during initial uCode load), the BSM loads
6023 * the bootstrap program into the on-board processor, and starts it.
6024 *
6025 * The bootstrap program loads (via DMA) instructions and data for a new
6026 * program from host DRAM locations indicated by the host driver in the
6027 * BSM_DRAM_* registers. Once the new program is loaded, it starts
6028 * automatically.
6029 *
6030 * When initializing the NIC, the host driver points the BSM to the
6031 * "initialize" uCode image. This uCode sets up some internal data, then
6032 * notifies host via "initialize alive" that it is complete.
6033 *
6034 * The host then replaces the BSM_DRAM_* pointer values to point to the
6035 * normal runtime uCode instructions and a backup uCode data cache buffer
6036 * (filled initially with starting data values for the on-board processor),
6037 * then triggers the "initialize" uCode to load and launch the runtime uCode,
6038 * which begins normal operation.
6039 *
6040 * When doing a power-save shutdown, runtime uCode saves data SRAM into
6041 * the backup data cache in DRAM before SRAM is powered down.
6042 *
6043 * When powering back up, the BSM loads the bootstrap program. This reloads
6044 * the runtime uCode instructions and the backup data cache into SRAM,
6045 * and re-launches the runtime uCode from where it left off.
6046 */
6047static int iwl_load_bsm(struct iwl_priv *priv)
6048{
6049 __le32 *image = priv->ucode_boot.v_addr;
6050 u32 len = priv->ucode_boot.len;
6051 dma_addr_t pinst;
6052 dma_addr_t pdata;
6053 u32 inst_len;
6054 u32 data_len;
6055 int rc;
6056 int i;
6057 u32 done;
6058 u32 reg_offset;
6059
6060 IWL_DEBUG_INFO("Begin load bsm\n");
6061
6062 /* make sure bootstrap program is no larger than BSM's SRAM size */
6063 if (len > IWL_MAX_BSM_SIZE)
6064 return -EINVAL;
6065
6066 /* Tell bootstrap uCode where to find the "Initialize" uCode
6067 * in host DRAM ... bits 31:0 for 3945, bits 35:4 for 4965.
6068 * NOTE: iwl_initialize_alive_start() will replace these values,
6069 * after the "initialize" uCode has run, to point to
6070 * runtime/protocol instructions and backup data cache. */
6071 pinst = priv->ucode_init.p_addr >> 4;
6072 pdata = priv->ucode_init_data.p_addr >> 4;
6073 inst_len = priv->ucode_init.len;
6074 data_len = priv->ucode_init_data.len;
6075
6076 rc = iwl_grab_restricted_access(priv);
6077 if (rc)
6078 return rc;
6079
6080 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
6081 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6082 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
6083 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
6084
6085 /* Fill BSM memory with bootstrap instructions */
6086 for (reg_offset = BSM_SRAM_LOWER_BOUND;
6087 reg_offset < BSM_SRAM_LOWER_BOUND + len;
6088 reg_offset += sizeof(u32), image++)
6089 _iwl_write_restricted_reg(priv, reg_offset,
6090 le32_to_cpu(*image));
6091
6092 rc = iwl_verify_bsm(priv);
6093 if (rc) {
6094 iwl_release_restricted_access(priv);
6095 return rc;
6096 }
6097
6098 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
6099 iwl_write_restricted_reg(priv, BSM_WR_MEM_SRC_REG, 0x0);
6100 iwl_write_restricted_reg(priv, BSM_WR_MEM_DST_REG,
6101 RTC_INST_LOWER_BOUND);
6102 iwl_write_restricted_reg(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
6103
6104 /* Load bootstrap code into instruction SRAM now,
6105 * to prepare to load "initialize" uCode */
6106 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
6107 BSM_WR_CTRL_REG_BIT_START);
6108
6109 /* Wait for load of bootstrap uCode to finish */
6110 for (i = 0; i < 100; i++) {
6111 done = iwl_read_restricted_reg(priv, BSM_WR_CTRL_REG);
6112 if (!(done & BSM_WR_CTRL_REG_BIT_START))
6113 break;
6114 udelay(10);
6115 }
6116 if (i < 100)
6117 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
6118 else {
6119 IWL_ERROR("BSM write did not complete!\n");
6120 return -EIO;
6121 }
6122
6123 /* Enable future boot loads whenever power management unit triggers it
6124 * (e.g. when powering back up after power-save shutdown) */
6125 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
6126 BSM_WR_CTRL_REG_BIT_START_EN);
6127
6128 iwl_release_restricted_access(priv);
6129
6130 return 0;
6131}
6132
6133static void iwl_nic_start(struct iwl_priv *priv)
6134{
6135 /* Remove all resets to allow NIC to operate */
6136 iwl_write32(priv, CSR_RESET, 0);
6137}
6138
6139/**
6140 * iwl_read_ucode - Read uCode images from disk file.
6141 *
6142 * Copy into buffers for card to fetch via bus-mastering
6143 */
6144static int iwl_read_ucode(struct iwl_priv *priv)
6145{
6146 struct iwl_ucode *ucode;
6147 int rc = 0;
6148 const struct firmware *ucode_raw;
6149 const char *name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode";
6150 u8 *src;
6151 size_t len;
6152 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
6153
6154 /* Ask kernel firmware_class module to get the boot firmware off disk.
6155 * request_firmware() is synchronous, file is in memory on return. */
6156 rc = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
6157 if (rc < 0) {
6158 IWL_ERROR("%s firmware file req failed: Reason %d\n", name, rc);
6159 goto error;
6160 }
6161
6162 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
6163 name, ucode_raw->size);
6164
6165 /* Make sure that we got at least our header! */
6166 if (ucode_raw->size < sizeof(*ucode)) {
6167 IWL_ERROR("File size way too small!\n");
6168 rc = -EINVAL;
6169 goto err_release;
6170 }
6171
6172 /* Data from ucode file: header followed by uCode images */
6173 ucode = (void *)ucode_raw->data;
6174
6175 ver = le32_to_cpu(ucode->ver);
6176 inst_size = le32_to_cpu(ucode->inst_size);
6177 data_size = le32_to_cpu(ucode->data_size);
6178 init_size = le32_to_cpu(ucode->init_size);
6179 init_data_size = le32_to_cpu(ucode->init_data_size);
6180 boot_size = le32_to_cpu(ucode->boot_size);
6181
6182 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
6183 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
6184 inst_size);
6185 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
6186 data_size);
6187 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
6188 init_size);
6189 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
6190 init_data_size);
6191 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
6192 boot_size);
6193
6194 /* Verify size of file vs. image size info in file's header */
6195 if (ucode_raw->size < sizeof(*ucode) +
6196 inst_size + data_size + init_size +
6197 init_data_size + boot_size) {
6198
6199 IWL_DEBUG_INFO("uCode file size %d too small\n",
6200 (int)ucode_raw->size);
6201 rc = -EINVAL;
6202 goto err_release;
6203 }
6204
6205 /* Verify that uCode images will fit in card's SRAM */
6206 if (inst_size > IWL_MAX_INST_SIZE) {
6207 IWL_DEBUG_INFO("uCode instr len %d too large to fit in card\n",
6208 (int)inst_size);
6209 rc = -EINVAL;
6210 goto err_release;
6211 }
6212
6213 if (data_size > IWL_MAX_DATA_SIZE) {
6214 IWL_DEBUG_INFO("uCode data len %d too large to fit in card\n",
6215 (int)data_size);
6216 rc = -EINVAL;
6217 goto err_release;
6218 }
6219 if (init_size > IWL_MAX_INST_SIZE) {
6220 IWL_DEBUG_INFO
6221 ("uCode init instr len %d too large to fit in card\n",
6222 (int)init_size);
6223 rc = -EINVAL;
6224 goto err_release;
6225 }
6226 if (init_data_size > IWL_MAX_DATA_SIZE) {
6227 IWL_DEBUG_INFO
6228 ("uCode init data len %d too large to fit in card\n",
6229 (int)init_data_size);
6230 rc = -EINVAL;
6231 goto err_release;
6232 }
6233 if (boot_size > IWL_MAX_BSM_SIZE) {
6234 IWL_DEBUG_INFO
6235 ("uCode boot instr len %d too large to fit in bsm\n",
6236 (int)boot_size);
6237 rc = -EINVAL;
6238 goto err_release;
6239 }
6240
6241 /* Allocate ucode buffers for card's bus-master loading ... */
6242
6243 /* Runtime instructions and 2 copies of data:
6244 * 1) unmodified from disk
6245 * 2) backup cache for save/restore during power-downs */
6246 priv->ucode_code.len = inst_size;
6247 priv->ucode_code.v_addr =
6248 pci_alloc_consistent(priv->pci_dev,
6249 priv->ucode_code.len,
6250 &(priv->ucode_code.p_addr));
6251
6252 priv->ucode_data.len = data_size;
6253 priv->ucode_data.v_addr =
6254 pci_alloc_consistent(priv->pci_dev,
6255 priv->ucode_data.len,
6256 &(priv->ucode_data.p_addr));
6257
6258 priv->ucode_data_backup.len = data_size;
6259 priv->ucode_data_backup.v_addr =
6260 pci_alloc_consistent(priv->pci_dev,
6261 priv->ucode_data_backup.len,
6262 &(priv->ucode_data_backup.p_addr));
6263
6264
6265 /* Initialization instructions and data */
6266 priv->ucode_init.len = init_size;
6267 priv->ucode_init.v_addr =
6268 pci_alloc_consistent(priv->pci_dev,
6269 priv->ucode_init.len,
6270 &(priv->ucode_init.p_addr));
6271
6272 priv->ucode_init_data.len = init_data_size;
6273 priv->ucode_init_data.v_addr =
6274 pci_alloc_consistent(priv->pci_dev,
6275 priv->ucode_init_data.len,
6276 &(priv->ucode_init_data.p_addr));
6277
6278 /* Bootstrap (instructions only, no data) */
6279 priv->ucode_boot.len = boot_size;
6280 priv->ucode_boot.v_addr =
6281 pci_alloc_consistent(priv->pci_dev,
6282 priv->ucode_boot.len,
6283 &(priv->ucode_boot.p_addr));
6284
6285 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
6286 !priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr ||
6287 !priv->ucode_boot.v_addr || !priv->ucode_data_backup.v_addr)
6288 goto err_pci_alloc;
6289
6290 /* Copy images into buffers for card's bus-master reads ... */
6291
6292 /* Runtime instructions (first block of data in file) */
6293 src = &ucode->data[0];
6294 len = priv->ucode_code.len;
6295 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %d\n",
6296 (int)len);
6297 memcpy(priv->ucode_code.v_addr, src, len);
6298 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6299 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6300
6301 /* Runtime data (2nd block)
6302 * NOTE: Copy into backup buffer will be done in iwl_up() */
6303 src = &ucode->data[inst_size];
6304 len = priv->ucode_data.len;
6305 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %d\n",
6306 (int)len);
6307 memcpy(priv->ucode_data.v_addr, src, len);
6308 memcpy(priv->ucode_data_backup.v_addr, src, len);
6309
6310 /* Initialization instructions (3rd block) */
6311 if (init_size) {
6312 src = &ucode->data[inst_size + data_size];
6313 len = priv->ucode_init.len;
6314 IWL_DEBUG_INFO("Copying (but not loading) init instr len %d\n",
6315 (int)len);
6316 memcpy(priv->ucode_init.v_addr, src, len);
6317 }
6318
6319 /* Initialization data (4th block) */
6320 if (init_data_size) {
6321 src = &ucode->data[inst_size + data_size + init_size];
6322 len = priv->ucode_init_data.len;
6323 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
6324 (int)len);
6325 memcpy(priv->ucode_init_data.v_addr, src, len);
6326 }
6327
6328 /* Bootstrap instructions (5th block) */
6329 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6330 len = priv->ucode_boot.len;
6331 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
6332 (int)len);
6333 memcpy(priv->ucode_boot.v_addr, src, len);
6334
6335 /* We have our copies now, allow OS release its copies */
6336 release_firmware(ucode_raw);
6337 return 0;
6338
6339 err_pci_alloc:
6340 IWL_ERROR("failed to allocate pci memory\n");
6341 rc = -ENOMEM;
6342 iwl_dealloc_ucode_pci(priv);
6343
6344 err_release:
6345 release_firmware(ucode_raw);
6346
6347 error:
6348 return rc;
6349}
6350
6351
6352/**
6353 * iwl_set_ucode_ptrs - Set uCode address location
6354 *
6355 * Tell initialization uCode where to find runtime uCode.
6356 *
6357 * BSM registers initially contain pointers to initialization uCode.
6358 * We need to replace them to load runtime uCode inst and data,
6359 * and to save runtime data when powering down.
6360 */
6361static int iwl_set_ucode_ptrs(struct iwl_priv *priv)
6362{
6363 dma_addr_t pinst;
6364 dma_addr_t pdata;
6365 int rc = 0;
6366 unsigned long flags;
6367
6368 /* bits 35:4 for 4965 */
6369 pinst = priv->ucode_code.p_addr >> 4;
6370 pdata = priv->ucode_data_backup.p_addr >> 4;
6371
6372 spin_lock_irqsave(&priv->lock, flags);
6373 rc = iwl_grab_restricted_access(priv);
6374 if (rc) {
6375 spin_unlock_irqrestore(&priv->lock, flags);
6376 return rc;
6377 }
6378
6379 /* Tell bootstrap uCode where to find image to load */
6380 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
6381 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6382 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
6383 priv->ucode_data.len);
6384
6385 /* Inst bytecount must be last to set up, bit 31 signals uCode
6386 * that all new ptr/size info is in place */
6387 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG,
6388 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6389
6390 iwl_release_restricted_access(priv);
6391
6392 spin_unlock_irqrestore(&priv->lock, flags);
6393
6394 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6395
6396 return rc;
6397}
6398
6399/**
6400 * iwl_init_alive_start - Called after REPLY_ALIVE notification receieved
6401 *
6402 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6403 *
6404 * The 4965 "initialize" ALIVE reply contains calibration data for:
6405 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6406 * (3945 does not contain this data).
6407 *
6408 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6409*/
6410static void iwl_init_alive_start(struct iwl_priv *priv)
6411{
6412 /* Check alive response for "valid" sign from uCode */
6413 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6414 /* We had an error bringing up the hardware, so take it
6415 * all the way back down so we can try again */
6416 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6417 goto restart;
6418 }
6419
6420 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6421 * This is a paranoid check, because we would not have gotten the
6422 * "initialize" alive if code weren't properly loaded. */
6423 if (iwl_verify_ucode(priv)) {
6424 /* Runtime instruction load was bad;
6425 * take it all the way back down so we can try again */
6426 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6427 goto restart;
6428 }
6429
6430 /* Calculate temperature */
6431 priv->temperature = iwl4965_get_temperature(priv);
6432
6433 /* Send pointers to protocol/runtime uCode image ... init code will
6434 * load and launch runtime uCode, which will send us another "Alive"
6435 * notification. */
6436 IWL_DEBUG_INFO("Initialization Alive received.\n");
6437 if (iwl_set_ucode_ptrs(priv)) {
6438 /* Runtime instruction load won't happen;
6439 * take it all the way back down so we can try again */
6440 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6441 goto restart;
6442 }
6443 return;
6444
6445 restart:
6446 queue_work(priv->workqueue, &priv->restart);
6447}
6448
6449
6450/**
6451 * iwl_alive_start - called after REPLY_ALIVE notification received
6452 * from protocol/runtime uCode (initialization uCode's
6453 * Alive gets handled by iwl_init_alive_start()).
6454 */
6455static void iwl_alive_start(struct iwl_priv *priv)
6456{
6457 int rc = 0;
6458
6459 IWL_DEBUG_INFO("Runtime Alive received.\n");
6460
6461 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6462 /* We had an error bringing up the hardware, so take it
6463 * all the way back down so we can try again */
6464 IWL_DEBUG_INFO("Alive failed.\n");
6465 goto restart;
6466 }
6467
6468 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6469 * This is a paranoid check, because we would not have gotten the
6470 * "runtime" alive if code weren't properly loaded. */
6471 if (iwl_verify_ucode(priv)) {
6472 /* Runtime instruction load was bad;
6473 * take it all the way back down so we can try again */
6474 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6475 goto restart;
6476 }
6477
6478 iwl_clear_stations_table(priv);
6479
6480 rc = iwl4965_alive_notify(priv);
6481 if (rc) {
6482 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6483 rc);
6484 goto restart;
6485 }
6486
6487 /* After the ALIVE response, we can process host commands */
6488 set_bit(STATUS_ALIVE, &priv->status);
6489
6490 /* Clear out the uCode error bit if it is set */
6491 clear_bit(STATUS_FW_ERROR, &priv->status);
6492
6493 rc = iwl_init_channel_map(priv);
6494 if (rc) {
6495 IWL_ERROR("initializing regulatory failed: %d\n", rc);
6496 return;
6497 }
6498
6499 iwl_init_geos(priv);
6500
6501 if (iwl_is_rfkill(priv))
6502 return;
6503
6504 if (!priv->mac80211_registered) {
6505 /* Unlock so any user space entry points can call back into
6506 * the driver without a deadlock... */
6507 mutex_unlock(&priv->mutex);
6508 iwl_rate_control_register(priv->hw);
6509 rc = ieee80211_register_hw(priv->hw);
6510 priv->hw->conf.beacon_int = 100;
6511 mutex_lock(&priv->mutex);
6512
6513 if (rc) {
6514 IWL_ERROR("Failed to register network "
6515 "device (error %d)\n", rc);
6516 return;
6517 }
6518
6519 priv->mac80211_registered = 1;
6520
6521 iwl_reset_channel_flag(priv);
6522 } else
6523 ieee80211_start_queues(priv->hw);
6524
6525 priv->active_rate = priv->rates_mask;
6526 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6527
6528 iwl_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
6529
6530 if (iwl_is_associated(priv)) {
6531 struct iwl_rxon_cmd *active_rxon =
6532 (struct iwl_rxon_cmd *)(&priv->active_rxon);
6533
6534 memcpy(&priv->staging_rxon, &priv->active_rxon,
6535 sizeof(priv->staging_rxon));
6536 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6537 } else {
6538 /* Initialize our rx_config data */
6539 iwl_connection_init_rx_config(priv);
6540 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6541 }
6542
6543 /* Configure BT coexistence */
6544 iwl_send_bt_config(priv);
6545
6546 /* Configure the adapter for unassociated operation */
6547 iwl_commit_rxon(priv);
6548
6549 /* At this point, the NIC is initialized and operational */
6550 priv->notif_missed_beacons = 0;
6551 set_bit(STATUS_READY, &priv->status);
6552
6553 iwl4965_rf_kill_ct_config(priv);
6554 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6555
6556 if (priv->error_recovering)
6557 iwl_error_recovery(priv);
6558
6559 return;
6560
6561 restart:
6562 queue_work(priv->workqueue, &priv->restart);
6563}
6564
6565static void iwl_cancel_deferred_work(struct iwl_priv *priv);
6566
6567static void __iwl_down(struct iwl_priv *priv)
6568{
6569 unsigned long flags;
6570 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6571 struct ieee80211_conf *conf = NULL;
6572
6573 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6574
6575 conf = ieee80211_get_hw_conf(priv->hw);
6576
6577 if (!exit_pending)
6578 set_bit(STATUS_EXIT_PENDING, &priv->status);
6579
6580 iwl_clear_stations_table(priv);
6581
6582 /* Unblock any waiting calls */
6583 wake_up_interruptible_all(&priv->wait_command_queue);
6584
6585 iwl_cancel_deferred_work(priv);
6586
6587 /* Wipe out the EXIT_PENDING status bit if we are not actually
6588 * exiting the module */
6589 if (!exit_pending)
6590 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6591
6592 /* stop and reset the on-board processor */
6593 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
6594
6595 /* tell the device to stop sending interrupts */
6596 iwl_disable_interrupts(priv);
6597
6598 if (priv->mac80211_registered)
6599 ieee80211_stop_queues(priv->hw);
6600
6601 /* If we have not previously called iwl_init() then
6602 * clear all bits but the RF Kill and SUSPEND bits and return */
6603 if (!iwl_is_init(priv)) {
6604 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6605 STATUS_RF_KILL_HW |
6606 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6607 STATUS_RF_KILL_SW |
6608 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6609 STATUS_IN_SUSPEND;
6610 goto exit;
6611 }
6612
6613 /* ...otherwise clear out all the status bits but the RF Kill and
6614 * SUSPEND bits and continue taking the NIC down. */
6615 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6616 STATUS_RF_KILL_HW |
6617 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6618 STATUS_RF_KILL_SW |
6619 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6620 STATUS_IN_SUSPEND |
6621 test_bit(STATUS_FW_ERROR, &priv->status) <<
6622 STATUS_FW_ERROR;
6623
6624 spin_lock_irqsave(&priv->lock, flags);
6625 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
6626 spin_unlock_irqrestore(&priv->lock, flags);
6627
6628 iwl_hw_txq_ctx_stop(priv);
6629 iwl_hw_rxq_stop(priv);
6630
6631 spin_lock_irqsave(&priv->lock, flags);
6632 if (!iwl_grab_restricted_access(priv)) {
6633 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
6634 APMG_CLK_VAL_DMA_CLK_RQT);
6635 iwl_release_restricted_access(priv);
6636 }
6637 spin_unlock_irqrestore(&priv->lock, flags);
6638
6639 udelay(5);
6640
6641 iwl_hw_nic_stop_master(priv);
6642 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6643 iwl_hw_nic_reset(priv);
6644
6645 exit:
6646 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
6647
6648 if (priv->ibss_beacon)
6649 dev_kfree_skb(priv->ibss_beacon);
6650 priv->ibss_beacon = NULL;
6651
6652 /* clear out any free frames */
6653 iwl_clear_free_frames(priv);
6654}
6655
6656static void iwl_down(struct iwl_priv *priv)
6657{
6658 mutex_lock(&priv->mutex);
6659 __iwl_down(priv);
6660 mutex_unlock(&priv->mutex);
6661}
6662
6663#define MAX_HW_RESTARTS 5
6664
6665static int __iwl_up(struct iwl_priv *priv)
6666{
Joe Perches0795af52007-10-03 17:59:30 -07006667 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006668 int rc, i;
6669 u32 hw_rf_kill = 0;
6670
6671 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6672 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6673 return -EIO;
6674 }
6675
6676 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6677 IWL_WARNING("Radio disabled by SW RF kill (module "
6678 "parameter)\n");
6679 return 0;
6680 }
6681
6682 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6683
6684 rc = iwl_hw_nic_init(priv);
6685 if (rc) {
6686 IWL_ERROR("Unable to int nic\n");
6687 return rc;
6688 }
6689
6690 /* make sure rfkill handshake bits are cleared */
6691 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6692 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
6693 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6694
6695 /* clear (again), then enable host interrupts */
6696 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6697 iwl_enable_interrupts(priv);
6698
6699 /* really make sure rfkill handshake bits are cleared */
6700 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6701 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6702
6703 /* Copy original ucode data image from disk into backup cache.
6704 * This will be used to initialize the on-board processor's
6705 * data SRAM for a clean start when the runtime program first loads. */
6706 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6707 priv->ucode_data.len);
6708
6709 /* If platform's RF_KILL switch is set to KILL,
6710 * wait for BIT_INT_RF_KILL interrupt before loading uCode
6711 * and getting things started */
6712 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
6713 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
6714 hw_rf_kill = 1;
6715
6716 if (test_bit(STATUS_RF_KILL_HW, &priv->status) || hw_rf_kill) {
6717 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6718 return 0;
6719 }
6720
6721 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6722
6723 iwl_clear_stations_table(priv);
6724
6725 /* load bootstrap state machine,
6726 * load bootstrap program into processor's memory,
6727 * prepare to load the "initialize" uCode */
6728 rc = iwl_load_bsm(priv);
6729
6730 if (rc) {
6731 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6732 continue;
6733 }
6734
6735 /* start card; "initialize" will load runtime ucode */
6736 iwl_nic_start(priv);
6737
6738 /* MAC Address location in EEPROM same for 3945/4965 */
6739 get_eeprom_mac(priv, priv->mac_addr);
Joe Perches0795af52007-10-03 17:59:30 -07006740 IWL_DEBUG_INFO("MAC address: %s\n",
6741 print_mac(mac, priv->mac_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006742
6743 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6744
6745 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6746
6747 return 0;
6748 }
6749
6750 set_bit(STATUS_EXIT_PENDING, &priv->status);
6751 __iwl_down(priv);
6752
6753 /* tried to restart and config the device for as long as our
6754 * patience could withstand */
6755 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6756 return -EIO;
6757}
6758
6759
6760/*****************************************************************************
6761 *
6762 * Workqueue callbacks
6763 *
6764 *****************************************************************************/
6765
6766static void iwl_bg_init_alive_start(struct work_struct *data)
6767{
6768 struct iwl_priv *priv =
6769 container_of(data, struct iwl_priv, init_alive_start.work);
6770
6771 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6772 return;
6773
6774 mutex_lock(&priv->mutex);
6775 iwl_init_alive_start(priv);
6776 mutex_unlock(&priv->mutex);
6777}
6778
6779static void iwl_bg_alive_start(struct work_struct *data)
6780{
6781 struct iwl_priv *priv =
6782 container_of(data, struct iwl_priv, alive_start.work);
6783
6784 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6785 return;
6786
6787 mutex_lock(&priv->mutex);
6788 iwl_alive_start(priv);
6789 mutex_unlock(&priv->mutex);
6790}
6791
6792static void iwl_bg_rf_kill(struct work_struct *work)
6793{
6794 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
6795
6796 wake_up_interruptible(&priv->wait_command_queue);
6797
6798 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6799 return;
6800
6801 mutex_lock(&priv->mutex);
6802
6803 if (!iwl_is_rfkill(priv)) {
6804 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6805 "HW and/or SW RF Kill no longer active, restarting "
6806 "device\n");
6807 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6808 queue_work(priv->workqueue, &priv->restart);
6809 } else {
6810
6811 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6812 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6813 "disabled by SW switch\n");
6814 else
6815 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6816 "Kill switch must be turned off for "
6817 "wireless networking to work.\n");
6818 }
6819 mutex_unlock(&priv->mutex);
6820}
6821
6822#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6823
6824static void iwl_bg_scan_check(struct work_struct *data)
6825{
6826 struct iwl_priv *priv =
6827 container_of(data, struct iwl_priv, scan_check.work);
6828
6829 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6830 return;
6831
6832 mutex_lock(&priv->mutex);
6833 if (test_bit(STATUS_SCANNING, &priv->status) ||
6834 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6835 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6836 "Scan completion watchdog resetting adapter (%dms)\n",
6837 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
6838 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6839 queue_work(priv->workqueue, &priv->restart);
6840 }
6841 mutex_unlock(&priv->mutex);
6842}
6843
6844static void iwl_bg_request_scan(struct work_struct *data)
6845{
6846 struct iwl_priv *priv =
6847 container_of(data, struct iwl_priv, request_scan);
6848 struct iwl_host_cmd cmd = {
6849 .id = REPLY_SCAN_CMD,
6850 .len = sizeof(struct iwl_scan_cmd),
6851 .meta.flags = CMD_SIZE_HUGE,
6852 };
6853 int rc = 0;
6854 struct iwl_scan_cmd *scan;
6855 struct ieee80211_conf *conf = NULL;
6856 u8 direct_mask;
6857 int phymode;
6858
6859 conf = ieee80211_get_hw_conf(priv->hw);
6860
6861 mutex_lock(&priv->mutex);
6862
6863 if (!iwl_is_ready(priv)) {
6864 IWL_WARNING("request scan called when driver not ready.\n");
6865 goto done;
6866 }
6867
6868 /* Make sure the scan wasn't cancelled before this queued work
6869 * was given the chance to run... */
6870 if (!test_bit(STATUS_SCANNING, &priv->status))
6871 goto done;
6872
6873 /* This should never be called or scheduled if there is currently
6874 * a scan active in the hardware. */
6875 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6876 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6877 "Ignoring second request.\n");
6878 rc = -EIO;
6879 goto done;
6880 }
6881
6882 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6883 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6884 goto done;
6885 }
6886
6887 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6888 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6889 goto done;
6890 }
6891
6892 if (iwl_is_rfkill(priv)) {
6893 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6894 goto done;
6895 }
6896
6897 if (!test_bit(STATUS_READY, &priv->status)) {
6898 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6899 goto done;
6900 }
6901
6902 if (!priv->scan_bands) {
6903 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6904 goto done;
6905 }
6906
6907 if (!priv->scan) {
6908 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
6909 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6910 if (!priv->scan) {
6911 rc = -ENOMEM;
6912 goto done;
6913 }
6914 }
6915 scan = priv->scan;
6916 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
6917
6918 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6919 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6920
6921 if (iwl_is_associated(priv)) {
6922 u16 interval = 0;
6923 u32 extra;
6924 u32 suspend_time = 100;
6925 u32 scan_suspend_time = 100;
6926 unsigned long flags;
6927
6928 IWL_DEBUG_INFO("Scanning while associated...\n");
6929
6930 spin_lock_irqsave(&priv->lock, flags);
6931 interval = priv->beacon_int;
6932 spin_unlock_irqrestore(&priv->lock, flags);
6933
6934 scan->suspend_time = 0;
6935 scan->max_out_time = cpu_to_le32(600 * 1024);
6936 if (!interval)
6937 interval = suspend_time;
6938
6939 extra = (suspend_time / interval) << 22;
6940 scan_suspend_time = (extra |
6941 ((suspend_time % interval) * 1024));
6942 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6943 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6944 scan_suspend_time, interval);
6945 }
6946
6947 /* We should add the ability for user to lock to PASSIVE ONLY */
6948 if (priv->one_direct_scan) {
6949 IWL_DEBUG_SCAN
6950 ("Kicking off one direct scan for '%s'\n",
6951 iwl_escape_essid(priv->direct_ssid,
6952 priv->direct_ssid_len));
6953 scan->direct_scan[0].id = WLAN_EID_SSID;
6954 scan->direct_scan[0].len = priv->direct_ssid_len;
6955 memcpy(scan->direct_scan[0].ssid,
6956 priv->direct_ssid, priv->direct_ssid_len);
6957 direct_mask = 1;
6958 } else if (!iwl_is_associated(priv)) {
6959 scan->direct_scan[0].id = WLAN_EID_SSID;
6960 scan->direct_scan[0].len = priv->essid_len;
6961 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6962 direct_mask = 1;
6963 } else
6964 direct_mask = 0;
6965
6966 /* We don't build a direct scan probe request; the uCode will do
6967 * that based on the direct_mask added to each channel entry */
6968 scan->tx_cmd.len = cpu_to_le16(
6969 iwl_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
6970 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
6971 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6972 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6973 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6974
6975 /* flags + rate selection */
6976
6977 scan->tx_cmd.tx_flags |= cpu_to_le32(0x200);
6978
6979 switch (priv->scan_bands) {
6980 case 2:
6981 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6982 scan->tx_cmd.rate_n_flags =
6983 iwl_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
6984 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6985
6986 scan->good_CRC_th = 0;
6987 phymode = MODE_IEEE80211G;
6988 break;
6989
6990 case 1:
6991 scan->tx_cmd.rate_n_flags =
6992 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
6993 RATE_MCS_ANT_B_MSK);
6994 scan->good_CRC_th = IWL_GOOD_CRC_TH;
6995 phymode = MODE_IEEE80211A;
6996 break;
6997
6998 default:
6999 IWL_WARNING("Invalid scan band count\n");
7000 goto done;
7001 }
7002
7003 /* select Rx chains */
7004
7005 /* Force use of chains B and C (0x6) for scan Rx.
7006 * Avoid A (0x1) because of its off-channel reception on A-band.
7007 * MIMO is not used here, but value is required to make uCode happy. */
7008 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
7009 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
7010 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
7011 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
7012
7013 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
7014 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
7015
7016 if (direct_mask)
7017 IWL_DEBUG_SCAN
7018 ("Initiating direct scan for %s.\n",
7019 iwl_escape_essid(priv->essid, priv->essid_len));
7020 else
7021 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
7022
7023 scan->channel_count =
7024 iwl_get_channels_for_scan(
7025 priv, phymode, 1, /* active */
7026 direct_mask,
7027 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
7028
7029 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
7030 scan->channel_count * sizeof(struct iwl_scan_channel);
7031 cmd.data = scan;
7032 scan->len = cpu_to_le16(cmd.len);
7033
7034 set_bit(STATUS_SCAN_HW, &priv->status);
7035 rc = iwl_send_cmd_sync(priv, &cmd);
7036 if (rc)
7037 goto done;
7038
7039 queue_delayed_work(priv->workqueue, &priv->scan_check,
7040 IWL_SCAN_CHECK_WATCHDOG);
7041
7042 mutex_unlock(&priv->mutex);
7043 return;
7044
7045 done:
7046 /* inform mac80211 sacn aborted */
7047 queue_work(priv->workqueue, &priv->scan_completed);
7048 mutex_unlock(&priv->mutex);
7049}
7050
7051static void iwl_bg_up(struct work_struct *data)
7052{
7053 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
7054
7055 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7056 return;
7057
7058 mutex_lock(&priv->mutex);
7059 __iwl_up(priv);
7060 mutex_unlock(&priv->mutex);
7061}
7062
7063static void iwl_bg_restart(struct work_struct *data)
7064{
7065 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
7066
7067 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7068 return;
7069
7070 iwl_down(priv);
7071 queue_work(priv->workqueue, &priv->up);
7072}
7073
7074static void iwl_bg_rx_replenish(struct work_struct *data)
7075{
7076 struct iwl_priv *priv =
7077 container_of(data, struct iwl_priv, rx_replenish);
7078
7079 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7080 return;
7081
7082 mutex_lock(&priv->mutex);
7083 iwl_rx_replenish(priv);
7084 mutex_unlock(&priv->mutex);
7085}
7086
7087static void iwl_bg_post_associate(struct work_struct *data)
7088{
7089 struct iwl_priv *priv = container_of(data, struct iwl_priv,
7090 post_associate.work);
7091
7092 int rc = 0;
7093 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07007094 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007095
7096 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7097 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
7098 return;
7099 }
7100
Joe Perches0795af52007-10-03 17:59:30 -07007101 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
7102 priv->assoc_id,
7103 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07007104
7105
7106 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7107 return;
7108
7109 mutex_lock(&priv->mutex);
7110
7111 conf = ieee80211_get_hw_conf(priv->hw);
7112
7113 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7114 iwl_commit_rxon(priv);
7115
7116 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
7117 iwl_setup_rxon_timing(priv);
7118 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7119 sizeof(priv->rxon_timing), &priv->rxon_timing);
7120 if (rc)
7121 IWL_WARNING("REPLY_RXON_TIMING failed - "
7122 "Attempting to continue.\n");
7123
7124 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7125
7126#ifdef CONFIG_IWLWIFI_HT
7127 if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
7128 iwl4965_set_rxon_ht(priv, &priv->current_assoc_ht);
7129 else {
7130 priv->active_rate_ht[0] = 0;
7131 priv->active_rate_ht[1] = 0;
7132 priv->current_channel_width = IWL_CHANNEL_WIDTH_20MHZ;
7133 }
7134#endif /* CONFIG_IWLWIFI_HT*/
7135 iwl4965_set_rxon_chain(priv);
7136 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7137
7138 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
7139 priv->assoc_id, priv->beacon_int);
7140
7141 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7142 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7143 else
7144 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7145
7146 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7147 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
7148 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
7149 else
7150 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7151
7152 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7153 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7154
7155 }
7156
7157 iwl_commit_rxon(priv);
7158
7159 switch (priv->iw_mode) {
7160 case IEEE80211_IF_TYPE_STA:
7161 iwl_rate_scale_init(priv->hw, IWL_AP_ID);
7162 break;
7163
7164 case IEEE80211_IF_TYPE_IBSS:
7165
7166 /* clear out the station table */
7167 iwl_clear_stations_table(priv);
7168
7169 iwl_rxon_add_station(priv, BROADCAST_ADDR, 0);
7170 iwl_rxon_add_station(priv, priv->bssid, 0);
7171 iwl_rate_scale_init(priv->hw, IWL_STA_ID);
7172 iwl_send_beacon_cmd(priv);
7173
7174 break;
7175
7176 default:
7177 IWL_ERROR("%s Should not be called in %d mode\n",
7178 __FUNCTION__, priv->iw_mode);
7179 break;
7180 }
7181
7182 iwl_sequence_reset(priv);
7183
7184#ifdef CONFIG_IWLWIFI_SENSITIVITY
7185 /* Enable Rx differential gain and sensitivity calibrations */
7186 iwl4965_chain_noise_reset(priv);
7187 priv->start_calib = 1;
7188#endif /* CONFIG_IWLWIFI_SENSITIVITY */
7189
7190 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7191 priv->assoc_station_added = 1;
7192
7193#ifdef CONFIG_IWLWIFI_QOS
7194 iwl_activate_qos(priv, 0);
7195#endif /* CONFIG_IWLWIFI_QOS */
7196 mutex_unlock(&priv->mutex);
7197}
7198
7199static void iwl_bg_abort_scan(struct work_struct *work)
7200{
7201 struct iwl_priv *priv = container_of(work, struct iwl_priv,
7202 abort_scan);
7203
7204 if (!iwl_is_ready(priv))
7205 return;
7206
7207 mutex_lock(&priv->mutex);
7208
7209 set_bit(STATUS_SCAN_ABORTING, &priv->status);
7210 iwl_send_scan_abort(priv);
7211
7212 mutex_unlock(&priv->mutex);
7213}
7214
7215static void iwl_bg_scan_completed(struct work_struct *work)
7216{
7217 struct iwl_priv *priv =
7218 container_of(work, struct iwl_priv, scan_completed);
7219
7220 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
7221
7222 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7223 return;
7224
7225 ieee80211_scan_completed(priv->hw);
7226
7227 /* Since setting the TXPOWER may have been deferred while
7228 * performing the scan, fire one off */
7229 mutex_lock(&priv->mutex);
7230 iwl_hw_reg_send_txpower(priv);
7231 mutex_unlock(&priv->mutex);
7232}
7233
7234/*****************************************************************************
7235 *
7236 * mac80211 entry point functions
7237 *
7238 *****************************************************************************/
7239
Johannes Berg4150c572007-09-17 01:29:23 -04007240static int iwl_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007241{
7242 struct iwl_priv *priv = hw->priv;
7243
7244 IWL_DEBUG_MAC80211("enter\n");
7245
7246 /* we should be verifying the device is ready to be opened */
7247 mutex_lock(&priv->mutex);
7248
7249 priv->is_open = 1;
7250
7251 if (!iwl_is_rfkill(priv))
7252 ieee80211_start_queues(priv->hw);
7253
7254 mutex_unlock(&priv->mutex);
7255 IWL_DEBUG_MAC80211("leave\n");
7256 return 0;
7257}
7258
Johannes Berg4150c572007-09-17 01:29:23 -04007259static void iwl_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007260{
7261 struct iwl_priv *priv = hw->priv;
7262
7263 IWL_DEBUG_MAC80211("enter\n");
7264 priv->is_open = 0;
7265 /*netif_stop_queue(dev); */
7266 flush_workqueue(priv->workqueue);
7267 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007268}
7269
7270static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
7271 struct ieee80211_tx_control *ctl)
7272{
7273 struct iwl_priv *priv = hw->priv;
7274
7275 IWL_DEBUG_MAC80211("enter\n");
7276
7277 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7278 IWL_DEBUG_MAC80211("leave - monitor\n");
7279 return -1;
7280 }
7281
7282 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
7283 ctl->tx_rate);
7284
7285 if (iwl_tx_skb(priv, skb, ctl))
7286 dev_kfree_skb_any(skb);
7287
7288 IWL_DEBUG_MAC80211("leave\n");
7289 return 0;
7290}
7291
7292static int iwl_mac_add_interface(struct ieee80211_hw *hw,
7293 struct ieee80211_if_init_conf *conf)
7294{
7295 struct iwl_priv *priv = hw->priv;
7296 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07007297 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007298
7299 IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
7300 if (conf->mac_addr)
Joe Perches0795af52007-10-03 17:59:30 -07007301 IWL_DEBUG_MAC80211("enter: MAC %s\n",
7302 print_mac(mac, conf->mac_addr));
Zhu Yib481de92007-09-25 17:54:57 -07007303
7304 if (priv->interface_id) {
7305 IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
7306 return 0;
7307 }
7308
7309 spin_lock_irqsave(&priv->lock, flags);
7310 priv->interface_id = conf->if_id;
7311
7312 spin_unlock_irqrestore(&priv->lock, flags);
7313
7314 mutex_lock(&priv->mutex);
7315 iwl_set_mode(priv, conf->type);
7316
7317 IWL_DEBUG_MAC80211("leave\n");
7318 mutex_unlock(&priv->mutex);
7319
7320 return 0;
7321}
7322
7323/**
7324 * iwl_mac_config - mac80211 config callback
7325 *
7326 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7327 * be set inappropriately and the driver currently sets the hardware up to
7328 * use it whenever needed.
7329 */
7330static int iwl_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
7331{
7332 struct iwl_priv *priv = hw->priv;
7333 const struct iwl_channel_info *ch_info;
7334 unsigned long flags;
7335
7336 mutex_lock(&priv->mutex);
7337 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
7338
7339 if (!iwl_is_ready(priv)) {
7340 IWL_DEBUG_MAC80211("leave - not ready\n");
7341 mutex_unlock(&priv->mutex);
7342 return -EIO;
7343 }
7344
7345 /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
7346 * what is exposed through include/ declrations */
7347 if (unlikely(!iwl_param_disable_hw_scan &&
7348 test_bit(STATUS_SCANNING, &priv->status))) {
7349 IWL_DEBUG_MAC80211("leave - scanning\n");
7350 mutex_unlock(&priv->mutex);
7351 return 0;
7352 }
7353
7354 spin_lock_irqsave(&priv->lock, flags);
7355
7356 ch_info = iwl_get_channel_info(priv, conf->phymode, conf->channel);
7357 if (!is_channel_valid(ch_info)) {
7358 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
7359 conf->channel, conf->phymode);
7360 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7361 spin_unlock_irqrestore(&priv->lock, flags);
7362 mutex_unlock(&priv->mutex);
7363 return -EINVAL;
7364 }
7365
7366#ifdef CONFIG_IWLWIFI_HT
7367 /* if we are switching fron ht to 2.4 clear flags
7368 * from any ht related info since 2.4 does not
7369 * support ht */
7370 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel)
7371#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7372 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7373#endif
7374 )
7375 priv->staging_rxon.flags = 0;
7376#endif /* CONFIG_IWLWIFI_HT */
7377
7378 iwl_set_rxon_channel(priv, conf->phymode, conf->channel);
7379
7380 iwl_set_flags_for_phymode(priv, conf->phymode);
7381
7382 /* The list of supported rates and rate mask can be different
7383 * for each phymode; since the phymode may have changed, reset
7384 * the rate mask to what mac80211 lists */
7385 iwl_set_rate(priv);
7386
7387 spin_unlock_irqrestore(&priv->lock, flags);
7388
7389#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7390 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
7391 iwl_hw_channel_switch(priv, conf->channel);
7392 mutex_unlock(&priv->mutex);
7393 return 0;
7394 }
7395#endif
7396
7397 iwl_radio_kill_sw(priv, !conf->radio_enabled);
7398
7399 if (!conf->radio_enabled) {
7400 IWL_DEBUG_MAC80211("leave - radio disabled\n");
7401 mutex_unlock(&priv->mutex);
7402 return 0;
7403 }
7404
7405 if (iwl_is_rfkill(priv)) {
7406 IWL_DEBUG_MAC80211("leave - RF kill\n");
7407 mutex_unlock(&priv->mutex);
7408 return -EIO;
7409 }
7410
7411 iwl_set_rate(priv);
7412
7413 if (memcmp(&priv->active_rxon,
7414 &priv->staging_rxon, sizeof(priv->staging_rxon)))
7415 iwl_commit_rxon(priv);
7416 else
7417 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7418
7419 IWL_DEBUG_MAC80211("leave\n");
7420
7421 mutex_unlock(&priv->mutex);
7422
7423 return 0;
7424}
7425
7426static void iwl_config_ap(struct iwl_priv *priv)
7427{
7428 int rc = 0;
7429
7430 if (priv->status & STATUS_EXIT_PENDING)
7431 return;
7432
7433 /* The following should be done only at AP bring up */
7434 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7435
7436 /* RXON - unassoc (to set timing command) */
7437 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7438 iwl_commit_rxon(priv);
7439
7440 /* RXON Timing */
7441 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
7442 iwl_setup_rxon_timing(priv);
7443 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7444 sizeof(priv->rxon_timing), &priv->rxon_timing);
7445 if (rc)
7446 IWL_WARNING("REPLY_RXON_TIMING failed - "
7447 "Attempting to continue.\n");
7448
7449 iwl4965_set_rxon_chain(priv);
7450
7451 /* FIXME: what should be the assoc_id for AP? */
7452 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7453 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7454 priv->staging_rxon.flags |=
7455 RXON_FLG_SHORT_PREAMBLE_MSK;
7456 else
7457 priv->staging_rxon.flags &=
7458 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7459
7460 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7461 if (priv->assoc_capability &
7462 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7463 priv->staging_rxon.flags |=
7464 RXON_FLG_SHORT_SLOT_MSK;
7465 else
7466 priv->staging_rxon.flags &=
7467 ~RXON_FLG_SHORT_SLOT_MSK;
7468
7469 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7470 priv->staging_rxon.flags &=
7471 ~RXON_FLG_SHORT_SLOT_MSK;
7472 }
7473 /* restore RXON assoc */
7474 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7475 iwl_commit_rxon(priv);
7476#ifdef CONFIG_IWLWIFI_QOS
7477 iwl_activate_qos(priv, 1);
7478#endif
7479 iwl_rxon_add_station(priv, BROADCAST_ADDR, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08007480 }
7481 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007482
7483 /* FIXME - we need to add code here to detect a totally new
7484 * configuration, reset the AP, unassoc, rxon timing, assoc,
7485 * clear sta table, add BCAST sta... */
7486}
7487
7488static int iwl_mac_config_interface(struct ieee80211_hw *hw, int if_id,
7489 struct ieee80211_if_conf *conf)
7490{
7491 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007492 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007493 unsigned long flags;
7494 int rc;
7495
7496 if (conf == NULL)
7497 return -EIO;
7498
7499 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7500 (!conf->beacon || !conf->ssid_len)) {
7501 IWL_DEBUG_MAC80211
7502 ("Leaving in AP mode because HostAPD is not ready.\n");
7503 return 0;
7504 }
7505
7506 mutex_lock(&priv->mutex);
7507
7508 IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
7509 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007510 IWL_DEBUG_MAC80211("bssid: %s\n",
7511 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007512
Johannes Berg4150c572007-09-17 01:29:23 -04007513/*
7514 * very dubious code was here; the probe filtering flag is never set:
7515 *
Zhu Yib481de92007-09-25 17:54:57 -07007516 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7517 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007518 */
7519 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yib481de92007-09-25 17:54:57 -07007520 IWL_DEBUG_MAC80211("leave - scanning\n");
7521 mutex_unlock(&priv->mutex);
7522 return 0;
7523 }
7524
7525 if (priv->interface_id != if_id) {
7526 IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
7527 mutex_unlock(&priv->mutex);
7528 return 0;
7529 }
7530
7531 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7532 if (!conf->bssid) {
7533 conf->bssid = priv->mac_addr;
7534 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007535 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7536 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007537 }
7538 if (priv->ibss_beacon)
7539 dev_kfree_skb(priv->ibss_beacon);
7540
7541 priv->ibss_beacon = conf->beacon;
7542 }
7543
7544 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7545 !is_multicast_ether_addr(conf->bssid)) {
7546 /* If there is currently a HW scan going on in the background
7547 * then we need to cancel it else the RXON below will fail. */
7548 if (iwl_scan_cancel_timeout(priv, 100)) {
7549 IWL_WARNING("Aborted scan still in progress "
7550 "after 100ms\n");
7551 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7552 mutex_unlock(&priv->mutex);
7553 return -EAGAIN;
7554 }
7555 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7556
7557 /* TODO: Audit driver for usage of these members and see
7558 * if mac80211 deprecates them (priv->bssid looks like it
7559 * shouldn't be there, but I haven't scanned the IBSS code
7560 * to verify) - jpk */
7561 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7562
7563 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7564 iwl_config_ap(priv);
7565 else {
7566 priv->staging_rxon.filter_flags |=
7567 RXON_FILTER_ASSOC_MSK;
7568 rc = iwl_commit_rxon(priv);
7569 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
7570 iwl_rxon_add_station(
7571 priv, priv->active_rxon.bssid_addr, 1);
7572 }
7573
7574 } else {
7575 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7576 iwl_commit_rxon(priv);
7577 }
7578
7579 spin_lock_irqsave(&priv->lock, flags);
7580 if (!conf->ssid_len)
7581 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7582 else
7583 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7584
7585 priv->essid_len = conf->ssid_len;
7586 spin_unlock_irqrestore(&priv->lock, flags);
7587
7588 IWL_DEBUG_MAC80211("leave\n");
7589 mutex_unlock(&priv->mutex);
7590
7591 return 0;
7592}
7593
Johannes Berg4150c572007-09-17 01:29:23 -04007594static void iwl_configure_filter(struct ieee80211_hw *hw,
7595 unsigned int changed_flags,
7596 unsigned int *total_flags,
7597 int mc_count, struct dev_addr_list *mc_list)
7598{
7599 /*
7600 * XXX: dummy
7601 * see also iwl_connection_init_rx_config
7602 */
7603 *total_flags = 0;
7604}
7605
Zhu Yib481de92007-09-25 17:54:57 -07007606static void iwl_mac_remove_interface(struct ieee80211_hw *hw,
7607 struct ieee80211_if_init_conf *conf)
7608{
7609 struct iwl_priv *priv = hw->priv;
7610
7611 IWL_DEBUG_MAC80211("enter\n");
7612
7613 mutex_lock(&priv->mutex);
7614 if (priv->interface_id == conf->if_id) {
7615 priv->interface_id = 0;
7616 memset(priv->bssid, 0, ETH_ALEN);
7617 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7618 priv->essid_len = 0;
7619 }
7620 mutex_unlock(&priv->mutex);
7621
7622 IWL_DEBUG_MAC80211("leave\n");
7623
7624}
7625
7626#define IWL_DELAY_NEXT_SCAN (HZ*2)
7627static int iwl_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
7628{
7629 int rc = 0;
7630 unsigned long flags;
7631 struct iwl_priv *priv = hw->priv;
7632
7633 IWL_DEBUG_MAC80211("enter\n");
7634
7635 spin_lock_irqsave(&priv->lock, flags);
7636
7637 if (!iwl_is_ready_rf(priv)) {
7638 rc = -EIO;
7639 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7640 goto out_unlock;
7641 }
7642
7643 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7644 rc = -EIO;
7645 IWL_ERROR("ERROR: APs don't scan\n");
7646 goto out_unlock;
7647 }
7648
7649 /* if we just finished scan ask for delay */
7650 if (priv->last_scan_jiffies &&
7651 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
7652 jiffies)) {
7653 rc = -EAGAIN;
7654 goto out_unlock;
7655 }
7656 if (len) {
7657 IWL_DEBUG_SCAN("direct scan for "
7658 "%s [%d]\n ",
7659 iwl_escape_essid(ssid, len), (int)len);
7660
7661 priv->one_direct_scan = 1;
7662 priv->direct_ssid_len = (u8)
7663 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7664 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
7665 }
7666
7667 rc = iwl_scan_initiate(priv);
7668
7669 IWL_DEBUG_MAC80211("leave\n");
7670
7671out_unlock:
7672 spin_unlock_irqrestore(&priv->lock, flags);
7673
7674 return rc;
7675}
7676
Johannes Bergea49c352007-09-18 17:29:21 -04007677static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007678 const u8 *local_addr, const u8 *addr,
7679 struct ieee80211_key_conf *key)
7680{
7681 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007682 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007683 int rc = 0;
7684 u8 sta_id;
7685
7686 IWL_DEBUG_MAC80211("enter\n");
7687
7688 if (!iwl_param_hwcrypto) {
7689 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7690 return -EOPNOTSUPP;
7691 }
7692
7693 if (is_zero_ether_addr(addr))
7694 /* only support pairwise keys */
7695 return -EOPNOTSUPP;
7696
7697 sta_id = iwl_hw_find_station(priv, addr);
7698 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007699 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7700 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007701 return -EINVAL;
7702 }
7703
7704 mutex_lock(&priv->mutex);
7705
7706 switch (cmd) {
7707 case SET_KEY:
7708 rc = iwl_update_sta_key_info(priv, key, sta_id);
7709 if (!rc) {
7710 iwl_set_rxon_hwcrypto(priv, 1);
7711 iwl_commit_rxon(priv);
7712 key->hw_key_idx = sta_id;
7713 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7714 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7715 }
7716 break;
7717 case DISABLE_KEY:
7718 rc = iwl_clear_sta_key_info(priv, sta_id);
7719 if (!rc) {
7720 iwl_set_rxon_hwcrypto(priv, 0);
7721 iwl_commit_rxon(priv);
7722 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7723 }
7724 break;
7725 default:
7726 rc = -EINVAL;
7727 }
7728
7729 IWL_DEBUG_MAC80211("leave\n");
7730 mutex_unlock(&priv->mutex);
7731
7732 return rc;
7733}
7734
7735static int iwl_mac_conf_tx(struct ieee80211_hw *hw, int queue,
7736 const struct ieee80211_tx_queue_params *params)
7737{
7738 struct iwl_priv *priv = hw->priv;
7739#ifdef CONFIG_IWLWIFI_QOS
7740 unsigned long flags;
7741 int q;
7742#endif /* CONFIG_IWL_QOS */
7743
7744 IWL_DEBUG_MAC80211("enter\n");
7745
7746 if (!iwl_is_ready_rf(priv)) {
7747 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7748 return -EIO;
7749 }
7750
7751 if (queue >= AC_NUM) {
7752 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7753 return 0;
7754 }
7755
7756#ifdef CONFIG_IWLWIFI_QOS
7757 if (!priv->qos_data.qos_enable) {
7758 priv->qos_data.qos_active = 0;
7759 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7760 return 0;
7761 }
7762 q = AC_NUM - 1 - queue;
7763
7764 spin_lock_irqsave(&priv->lock, flags);
7765
7766 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7767 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7768 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7769 priv->qos_data.def_qos_parm.ac[q].edca_txop =
7770 cpu_to_le16((params->burst_time * 100));
7771
7772 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7773 priv->qos_data.qos_active = 1;
7774
7775 spin_unlock_irqrestore(&priv->lock, flags);
7776
7777 mutex_lock(&priv->mutex);
7778 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7779 iwl_activate_qos(priv, 1);
7780 else if (priv->assoc_id && iwl_is_associated(priv))
7781 iwl_activate_qos(priv, 0);
7782
7783 mutex_unlock(&priv->mutex);
7784
7785#endif /*CONFIG_IWLWIFI_QOS */
7786
7787 IWL_DEBUG_MAC80211("leave\n");
7788 return 0;
7789}
7790
7791static int iwl_mac_get_tx_stats(struct ieee80211_hw *hw,
7792 struct ieee80211_tx_queue_stats *stats)
7793{
7794 struct iwl_priv *priv = hw->priv;
7795 int i, avail;
7796 struct iwl_tx_queue *txq;
7797 struct iwl_queue *q;
7798 unsigned long flags;
7799
7800 IWL_DEBUG_MAC80211("enter\n");
7801
7802 if (!iwl_is_ready_rf(priv)) {
7803 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7804 return -EIO;
7805 }
7806
7807 spin_lock_irqsave(&priv->lock, flags);
7808
7809 for (i = 0; i < AC_NUM; i++) {
7810 txq = &priv->txq[i];
7811 q = &txq->q;
7812 avail = iwl_queue_space(q);
7813
7814 stats->data[i].len = q->n_window - avail;
7815 stats->data[i].limit = q->n_window - q->high_mark;
7816 stats->data[i].count = q->n_window;
7817
7818 }
7819 spin_unlock_irqrestore(&priv->lock, flags);
7820
7821 IWL_DEBUG_MAC80211("leave\n");
7822
7823 return 0;
7824}
7825
7826static int iwl_mac_get_stats(struct ieee80211_hw *hw,
7827 struct ieee80211_low_level_stats *stats)
7828{
7829 IWL_DEBUG_MAC80211("enter\n");
7830 IWL_DEBUG_MAC80211("leave\n");
7831
7832 return 0;
7833}
7834
7835static u64 iwl_mac_get_tsf(struct ieee80211_hw *hw)
7836{
7837 IWL_DEBUG_MAC80211("enter\n");
7838 IWL_DEBUG_MAC80211("leave\n");
7839
7840 return 0;
7841}
7842
7843static void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
7844{
7845 struct iwl_priv *priv = hw->priv;
7846 unsigned long flags;
7847
7848 mutex_lock(&priv->mutex);
7849 IWL_DEBUG_MAC80211("enter\n");
7850
7851 priv->lq_mngr.lq_ready = 0;
7852#ifdef CONFIG_IWLWIFI_HT
7853 spin_lock_irqsave(&priv->lock, flags);
7854 memset(&priv->current_assoc_ht, 0, sizeof(struct sta_ht_info));
7855 spin_unlock_irqrestore(&priv->lock, flags);
7856#ifdef CONFIG_IWLWIFI_HT_AGG
7857/* if (priv->lq_mngr.agg_ctrl.granted_ba)
7858 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);*/
7859
7860 memset(&(priv->lq_mngr.agg_ctrl), 0, sizeof(struct iwl_agg_control));
7861 priv->lq_mngr.agg_ctrl.tid_traffic_load_threshold = 10;
7862 priv->lq_mngr.agg_ctrl.ba_timeout = 5000;
7863 priv->lq_mngr.agg_ctrl.auto_agg = 1;
7864
7865 if (priv->lq_mngr.agg_ctrl.auto_agg)
7866 priv->lq_mngr.agg_ctrl.requested_ba = TID_ALL_ENABLED;
7867#endif /*CONFIG_IWLWIFI_HT_AGG */
7868#endif /* CONFIG_IWLWIFI_HT */
7869
7870#ifdef CONFIG_IWLWIFI_QOS
7871 iwl_reset_qos(priv);
7872#endif
7873
7874 cancel_delayed_work(&priv->post_associate);
7875
7876 spin_lock_irqsave(&priv->lock, flags);
7877 priv->assoc_id = 0;
7878 priv->assoc_capability = 0;
7879 priv->call_post_assoc_from_beacon = 0;
7880 priv->assoc_station_added = 0;
7881
7882 /* new association get rid of ibss beacon skb */
7883 if (priv->ibss_beacon)
7884 dev_kfree_skb(priv->ibss_beacon);
7885
7886 priv->ibss_beacon = NULL;
7887
7888 priv->beacon_int = priv->hw->conf.beacon_int;
7889 priv->timestamp1 = 0;
7890 priv->timestamp0 = 0;
7891 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7892 priv->beacon_int = 0;
7893
7894 spin_unlock_irqrestore(&priv->lock, flags);
7895
7896 /* Per mac80211.h: This is only used in IBSS mode... */
7897 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7898 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7899 mutex_unlock(&priv->mutex);
7900 return;
7901 }
7902
7903 if (!iwl_is_ready_rf(priv)) {
7904 IWL_DEBUG_MAC80211("leave - not ready\n");
7905 mutex_unlock(&priv->mutex);
7906 return;
7907 }
7908
7909 priv->only_active_channel = 0;
7910
7911 iwl_set_rate(priv);
7912
7913 mutex_unlock(&priv->mutex);
7914
7915 IWL_DEBUG_MAC80211("leave\n");
7916
7917}
7918
7919static int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
7920 struct ieee80211_tx_control *control)
7921{
7922 struct iwl_priv *priv = hw->priv;
7923 unsigned long flags;
7924
7925 mutex_lock(&priv->mutex);
7926 IWL_DEBUG_MAC80211("enter\n");
7927
7928 if (!iwl_is_ready_rf(priv)) {
7929 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7930 mutex_unlock(&priv->mutex);
7931 return -EIO;
7932 }
7933
7934 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7935 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7936 mutex_unlock(&priv->mutex);
7937 return -EIO;
7938 }
7939
7940 spin_lock_irqsave(&priv->lock, flags);
7941
7942 if (priv->ibss_beacon)
7943 dev_kfree_skb(priv->ibss_beacon);
7944
7945 priv->ibss_beacon = skb;
7946
7947 priv->assoc_id = 0;
7948
7949 IWL_DEBUG_MAC80211("leave\n");
7950 spin_unlock_irqrestore(&priv->lock, flags);
7951
7952#ifdef CONFIG_IWLWIFI_QOS
7953 iwl_reset_qos(priv);
7954#endif
7955
7956 queue_work(priv->workqueue, &priv->post_associate.work);
7957
7958 mutex_unlock(&priv->mutex);
7959
7960 return 0;
7961}
7962
7963#ifdef CONFIG_IWLWIFI_HT
7964union ht_cap_info {
7965 struct {
7966 u16 advanced_coding_cap :1;
7967 u16 supported_chan_width_set :1;
7968 u16 mimo_power_save_mode :2;
7969 u16 green_field :1;
7970 u16 short_GI20 :1;
7971 u16 short_GI40 :1;
7972 u16 tx_stbc :1;
7973 u16 rx_stbc :1;
7974 u16 beam_forming :1;
7975 u16 delayed_ba :1;
7976 u16 maximal_amsdu_size :1;
7977 u16 cck_mode_at_40MHz :1;
7978 u16 psmp_support :1;
7979 u16 stbc_ctrl_frame_support :1;
7980 u16 sig_txop_protection_support :1;
7981 };
7982 u16 val;
7983} __attribute__ ((packed));
7984
7985union ht_param_info{
7986 struct {
7987 u8 max_rx_ampdu_factor :2;
7988 u8 mpdu_density :3;
7989 u8 reserved :3;
7990 };
7991 u8 val;
7992} __attribute__ ((packed));
7993
7994union ht_exra_param_info {
7995 struct {
7996 u8 ext_chan_offset :2;
7997 u8 tx_chan_width :1;
7998 u8 rifs_mode :1;
7999 u8 controlled_access_only :1;
8000 u8 service_interval_granularity :3;
8001 };
8002 u8 val;
8003} __attribute__ ((packed));
8004
8005union ht_operation_mode{
8006 struct {
8007 u16 op_mode :2;
8008 u16 non_GF :1;
8009 u16 reserved :13;
8010 };
8011 u16 val;
8012} __attribute__ ((packed));
8013
8014
8015static int sta_ht_info_init(struct ieee80211_ht_capability *ht_cap,
8016 struct ieee80211_ht_additional_info *ht_extra,
8017 struct sta_ht_info *ht_info_ap,
8018 struct sta_ht_info *ht_info)
8019{
8020 union ht_cap_info cap;
8021 union ht_operation_mode op_mode;
8022 union ht_param_info param_info;
8023 union ht_exra_param_info extra_param_info;
8024
8025 IWL_DEBUG_MAC80211("enter: \n");
8026
8027 if (!ht_info) {
8028 IWL_DEBUG_MAC80211("leave: ht_info is NULL\n");
8029 return -1;
8030 }
8031
8032 if (ht_cap) {
8033 cap.val = (u16) le16_to_cpu(ht_cap->capabilities_info);
8034 param_info.val = ht_cap->mac_ht_params_info;
8035 ht_info->is_ht = 1;
8036 if (cap.short_GI20)
8037 ht_info->sgf |= 0x1;
8038 if (cap.short_GI40)
8039 ht_info->sgf |= 0x2;
8040 ht_info->is_green_field = cap.green_field;
8041 ht_info->max_amsdu_size = cap.maximal_amsdu_size;
8042 ht_info->supported_chan_width = cap.supported_chan_width_set;
8043 ht_info->tx_mimo_ps_mode = cap.mimo_power_save_mode;
8044 memcpy(ht_info->supp_rates, ht_cap->supported_mcs_set, 16);
8045
8046 ht_info->ampdu_factor = param_info.max_rx_ampdu_factor;
8047 ht_info->mpdu_density = param_info.mpdu_density;
8048
8049 IWL_DEBUG_MAC80211("SISO mask 0x%X MIMO mask 0x%X \n",
8050 ht_cap->supported_mcs_set[0],
8051 ht_cap->supported_mcs_set[1]);
8052
8053 if (ht_info_ap) {
8054 ht_info->control_channel = ht_info_ap->control_channel;
8055 ht_info->extension_chan_offset =
8056 ht_info_ap->extension_chan_offset;
8057 ht_info->tx_chan_width = ht_info_ap->tx_chan_width;
8058 ht_info->operating_mode = ht_info_ap->operating_mode;
8059 }
8060
8061 if (ht_extra) {
8062 extra_param_info.val = ht_extra->ht_param;
8063 ht_info->control_channel = ht_extra->control_chan;
8064 ht_info->extension_chan_offset =
8065 extra_param_info.ext_chan_offset;
8066 ht_info->tx_chan_width = extra_param_info.tx_chan_width;
8067 op_mode.val = (u16)
8068 le16_to_cpu(ht_extra->operation_mode);
8069 ht_info->operating_mode = op_mode.op_mode;
8070 IWL_DEBUG_MAC80211("control channel %d\n",
8071 ht_extra->control_chan);
8072 }
8073 } else
8074 ht_info->is_ht = 0;
8075
8076 IWL_DEBUG_MAC80211("leave\n");
8077 return 0;
8078}
8079
8080static int iwl_mac_conf_ht(struct ieee80211_hw *hw,
8081 struct ieee80211_ht_capability *ht_cap,
8082 struct ieee80211_ht_additional_info *ht_extra)
8083{
8084 struct iwl_priv *priv = hw->priv;
8085 int rs;
8086
8087 IWL_DEBUG_MAC80211("enter: \n");
8088
8089 rs = sta_ht_info_init(ht_cap, ht_extra, NULL, &priv->current_assoc_ht);
8090 iwl4965_set_rxon_chain(priv);
8091
8092 if (priv && priv->assoc_id &&
8093 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
8094 unsigned long flags;
8095
8096 spin_lock_irqsave(&priv->lock, flags);
8097 if (priv->beacon_int)
8098 queue_work(priv->workqueue, &priv->post_associate.work);
8099 else
8100 priv->call_post_assoc_from_beacon = 1;
8101 spin_unlock_irqrestore(&priv->lock, flags);
8102 }
8103
8104 IWL_DEBUG_MAC80211("leave: control channel %d\n",
8105 ht_extra->control_chan);
8106 return rs;
8107
8108}
8109
8110static void iwl_set_ht_capab(struct ieee80211_hw *hw,
8111 struct ieee80211_ht_capability *ht_cap,
8112 u8 use_wide_chan)
8113{
8114 union ht_cap_info cap;
8115 union ht_param_info param_info;
8116
8117 memset(&cap, 0, sizeof(union ht_cap_info));
8118 memset(&param_info, 0, sizeof(union ht_param_info));
8119
8120 cap.maximal_amsdu_size = HT_IE_MAX_AMSDU_SIZE_4K;
8121 cap.green_field = 1;
8122 cap.short_GI20 = 1;
8123 cap.short_GI40 = 1;
8124 cap.supported_chan_width_set = use_wide_chan;
8125 cap.mimo_power_save_mode = 0x3;
8126
8127 param_info.max_rx_ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
8128 param_info.mpdu_density = CFG_HT_MPDU_DENSITY_DEF;
8129 ht_cap->capabilities_info = (__le16) cpu_to_le16(cap.val);
8130 ht_cap->mac_ht_params_info = (u8) param_info.val;
8131
8132 ht_cap->supported_mcs_set[0] = 0xff;
8133 ht_cap->supported_mcs_set[1] = 0xff;
8134 ht_cap->supported_mcs_set[4] =
8135 (cap.supported_chan_width_set) ? 0x1: 0x0;
8136}
8137
8138static void iwl_mac_get_ht_capab(struct ieee80211_hw *hw,
8139 struct ieee80211_ht_capability *ht_cap)
8140{
8141 u8 use_wide_channel = 1;
8142 struct iwl_priv *priv = hw->priv;
8143
8144 IWL_DEBUG_MAC80211("enter: \n");
8145 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
8146 use_wide_channel = 0;
8147
8148 /* no fat tx allowed on 2.4GHZ */
8149 if (priv->phymode != MODE_IEEE80211A)
8150 use_wide_channel = 0;
8151
8152 iwl_set_ht_capab(hw, ht_cap, use_wide_channel);
8153 IWL_DEBUG_MAC80211("leave: \n");
8154}
8155#endif /*CONFIG_IWLWIFI_HT*/
8156
8157/*****************************************************************************
8158 *
8159 * sysfs attributes
8160 *
8161 *****************************************************************************/
8162
8163#ifdef CONFIG_IWLWIFI_DEBUG
8164
8165/*
8166 * The following adds a new attribute to the sysfs representation
8167 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
8168 * used for controlling the debug level.
8169 *
8170 * See the level definitions in iwl for details.
8171 */
8172
8173static ssize_t show_debug_level(struct device_driver *d, char *buf)
8174{
8175 return sprintf(buf, "0x%08X\n", iwl_debug_level);
8176}
8177static ssize_t store_debug_level(struct device_driver *d,
8178 const char *buf, size_t count)
8179{
8180 char *p = (char *)buf;
8181 u32 val;
8182
8183 val = simple_strtoul(p, &p, 0);
8184 if (p == buf)
8185 printk(KERN_INFO DRV_NAME
8186 ": %s is not in hex or decimal form.\n", buf);
8187 else
8188 iwl_debug_level = val;
8189
8190 return strnlen(buf, count);
8191}
8192
8193static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
8194 show_debug_level, store_debug_level);
8195
8196#endif /* CONFIG_IWLWIFI_DEBUG */
8197
8198static ssize_t show_rf_kill(struct device *d,
8199 struct device_attribute *attr, char *buf)
8200{
8201 /*
8202 * 0 - RF kill not enabled
8203 * 1 - SW based RF kill active (sysfs)
8204 * 2 - HW based RF kill active
8205 * 3 - Both HW and SW based RF kill active
8206 */
8207 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8208 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
8209 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
8210
8211 return sprintf(buf, "%i\n", val);
8212}
8213
8214static ssize_t store_rf_kill(struct device *d,
8215 struct device_attribute *attr,
8216 const char *buf, size_t count)
8217{
8218 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8219
8220 mutex_lock(&priv->mutex);
8221 iwl_radio_kill_sw(priv, buf[0] == '1');
8222 mutex_unlock(&priv->mutex);
8223
8224 return count;
8225}
8226
8227static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
8228
8229static ssize_t show_temperature(struct device *d,
8230 struct device_attribute *attr, char *buf)
8231{
8232 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8233
8234 if (!iwl_is_alive(priv))
8235 return -EAGAIN;
8236
8237 return sprintf(buf, "%d\n", iwl_hw_get_temperature(priv));
8238}
8239
8240static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8241
8242static ssize_t show_rs_window(struct device *d,
8243 struct device_attribute *attr,
8244 char *buf)
8245{
8246 struct iwl_priv *priv = d->driver_data;
8247 return iwl_fill_rs_info(priv->hw, buf, IWL_AP_ID);
8248}
8249static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8250
8251static ssize_t show_tx_power(struct device *d,
8252 struct device_attribute *attr, char *buf)
8253{
8254 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8255 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8256}
8257
8258static ssize_t store_tx_power(struct device *d,
8259 struct device_attribute *attr,
8260 const char *buf, size_t count)
8261{
8262 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8263 char *p = (char *)buf;
8264 u32 val;
8265
8266 val = simple_strtoul(p, &p, 10);
8267 if (p == buf)
8268 printk(KERN_INFO DRV_NAME
8269 ": %s is not in decimal form.\n", buf);
8270 else
8271 iwl_hw_reg_set_txpower(priv, val);
8272
8273 return count;
8274}
8275
8276static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8277
8278static ssize_t show_flags(struct device *d,
8279 struct device_attribute *attr, char *buf)
8280{
8281 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8282
8283 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8284}
8285
8286static ssize_t store_flags(struct device *d,
8287 struct device_attribute *attr,
8288 const char *buf, size_t count)
8289{
8290 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8291 u32 flags = simple_strtoul(buf, NULL, 0);
8292
8293 mutex_lock(&priv->mutex);
8294 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8295 /* Cancel any currently running scans... */
8296 if (iwl_scan_cancel_timeout(priv, 100))
8297 IWL_WARNING("Could not cancel scan.\n");
8298 else {
8299 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8300 flags);
8301 priv->staging_rxon.flags = cpu_to_le32(flags);
8302 iwl_commit_rxon(priv);
8303 }
8304 }
8305 mutex_unlock(&priv->mutex);
8306
8307 return count;
8308}
8309
8310static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8311
8312static ssize_t show_filter_flags(struct device *d,
8313 struct device_attribute *attr, char *buf)
8314{
8315 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8316
8317 return sprintf(buf, "0x%04X\n",
8318 le32_to_cpu(priv->active_rxon.filter_flags));
8319}
8320
8321static ssize_t store_filter_flags(struct device *d,
8322 struct device_attribute *attr,
8323 const char *buf, size_t count)
8324{
8325 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8326 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8327
8328 mutex_lock(&priv->mutex);
8329 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8330 /* Cancel any currently running scans... */
8331 if (iwl_scan_cancel_timeout(priv, 100))
8332 IWL_WARNING("Could not cancel scan.\n");
8333 else {
8334 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8335 "0x%04X\n", filter_flags);
8336 priv->staging_rxon.filter_flags =
8337 cpu_to_le32(filter_flags);
8338 iwl_commit_rxon(priv);
8339 }
8340 }
8341 mutex_unlock(&priv->mutex);
8342
8343 return count;
8344}
8345
8346static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8347 store_filter_flags);
8348
8349static ssize_t show_tune(struct device *d,
8350 struct device_attribute *attr, char *buf)
8351{
8352 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8353
8354 return sprintf(buf, "0x%04X\n",
8355 (priv->phymode << 8) |
8356 le16_to_cpu(priv->active_rxon.channel));
8357}
8358
8359static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode);
8360
8361static ssize_t store_tune(struct device *d,
8362 struct device_attribute *attr,
8363 const char *buf, size_t count)
8364{
8365 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8366 char *p = (char *)buf;
8367 u16 tune = simple_strtoul(p, &p, 0);
8368 u8 phymode = (tune >> 8) & 0xff;
8369 u16 channel = tune & 0xff;
8370
8371 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
8372
8373 mutex_lock(&priv->mutex);
8374 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
8375 (priv->phymode != phymode)) {
8376 const struct iwl_channel_info *ch_info;
8377
8378 ch_info = iwl_get_channel_info(priv, phymode, channel);
8379 if (!ch_info) {
8380 IWL_WARNING("Requested invalid phymode/channel "
8381 "combination: %d %d\n", phymode, channel);
8382 mutex_unlock(&priv->mutex);
8383 return -EINVAL;
8384 }
8385
8386 /* Cancel any currently running scans... */
8387 if (iwl_scan_cancel_timeout(priv, 100))
8388 IWL_WARNING("Could not cancel scan.\n");
8389 else {
8390 IWL_DEBUG_INFO("Committing phymode and "
8391 "rxon.channel = %d %d\n",
8392 phymode, channel);
8393
8394 iwl_set_rxon_channel(priv, phymode, channel);
8395 iwl_set_flags_for_phymode(priv, phymode);
8396
8397 iwl_set_rate(priv);
8398 iwl_commit_rxon(priv);
8399 }
8400 }
8401 mutex_unlock(&priv->mutex);
8402
8403 return count;
8404}
8405
8406static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
8407
8408#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8409
8410static ssize_t show_measurement(struct device *d,
8411 struct device_attribute *attr, char *buf)
8412{
8413 struct iwl_priv *priv = dev_get_drvdata(d);
8414 struct iwl_spectrum_notification measure_report;
8415 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8416 u8 *data = (u8 *) & measure_report;
8417 unsigned long flags;
8418
8419 spin_lock_irqsave(&priv->lock, flags);
8420 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8421 spin_unlock_irqrestore(&priv->lock, flags);
8422 return 0;
8423 }
8424 memcpy(&measure_report, &priv->measure_report, size);
8425 priv->measurement_status = 0;
8426 spin_unlock_irqrestore(&priv->lock, flags);
8427
8428 while (size && (PAGE_SIZE - len)) {
8429 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8430 PAGE_SIZE - len, 1);
8431 len = strlen(buf);
8432 if (PAGE_SIZE - len)
8433 buf[len++] = '\n';
8434
8435 ofs += 16;
8436 size -= min(size, 16U);
8437 }
8438
8439 return len;
8440}
8441
8442static ssize_t store_measurement(struct device *d,
8443 struct device_attribute *attr,
8444 const char *buf, size_t count)
8445{
8446 struct iwl_priv *priv = dev_get_drvdata(d);
8447 struct ieee80211_measurement_params params = {
8448 .channel = le16_to_cpu(priv->active_rxon.channel),
8449 .start_time = cpu_to_le64(priv->last_tsf),
8450 .duration = cpu_to_le16(1),
8451 };
8452 u8 type = IWL_MEASURE_BASIC;
8453 u8 buffer[32];
8454 u8 channel;
8455
8456 if (count) {
8457 char *p = buffer;
8458 strncpy(buffer, buf, min(sizeof(buffer), count));
8459 channel = simple_strtoul(p, NULL, 0);
8460 if (channel)
8461 params.channel = channel;
8462
8463 p = buffer;
8464 while (*p && *p != ' ')
8465 p++;
8466 if (*p)
8467 type = simple_strtoul(p + 1, NULL, 0);
8468 }
8469
8470 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8471 "channel %d (for '%s')\n", type, params.channel, buf);
8472 iwl_get_measurement(priv, &params, type);
8473
8474 return count;
8475}
8476
8477static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8478 show_measurement, store_measurement);
8479#endif /* CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT */
8480
8481static ssize_t store_retry_rate(struct device *d,
8482 struct device_attribute *attr,
8483 const char *buf, size_t count)
8484{
8485 struct iwl_priv *priv = dev_get_drvdata(d);
8486
8487 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8488 if (priv->retry_rate <= 0)
8489 priv->retry_rate = 1;
8490
8491 return count;
8492}
8493
8494static ssize_t show_retry_rate(struct device *d,
8495 struct device_attribute *attr, char *buf)
8496{
8497 struct iwl_priv *priv = dev_get_drvdata(d);
8498 return sprintf(buf, "%d", priv->retry_rate);
8499}
8500
8501static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8502 store_retry_rate);
8503
8504static ssize_t store_power_level(struct device *d,
8505 struct device_attribute *attr,
8506 const char *buf, size_t count)
8507{
8508 struct iwl_priv *priv = dev_get_drvdata(d);
8509 int rc;
8510 int mode;
8511
8512 mode = simple_strtoul(buf, NULL, 0);
8513 mutex_lock(&priv->mutex);
8514
8515 if (!iwl_is_ready(priv)) {
8516 rc = -EAGAIN;
8517 goto out;
8518 }
8519
8520 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8521 mode = IWL_POWER_AC;
8522 else
8523 mode |= IWL_POWER_ENABLED;
8524
8525 if (mode != priv->power_mode) {
8526 rc = iwl_send_power_mode(priv, IWL_POWER_LEVEL(mode));
8527 if (rc) {
8528 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8529 goto out;
8530 }
8531 priv->power_mode = mode;
8532 }
8533
8534 rc = count;
8535
8536 out:
8537 mutex_unlock(&priv->mutex);
8538 return rc;
8539}
8540
8541#define MAX_WX_STRING 80
8542
8543/* Values are in microsecond */
8544static const s32 timeout_duration[] = {
8545 350000,
8546 250000,
8547 75000,
8548 37000,
8549 25000,
8550};
8551static const s32 period_duration[] = {
8552 400000,
8553 700000,
8554 1000000,
8555 1000000,
8556 1000000
8557};
8558
8559static ssize_t show_power_level(struct device *d,
8560 struct device_attribute *attr, char *buf)
8561{
8562 struct iwl_priv *priv = dev_get_drvdata(d);
8563 int level = IWL_POWER_LEVEL(priv->power_mode);
8564 char *p = buf;
8565
8566 p += sprintf(p, "%d ", level);
8567 switch (level) {
8568 case IWL_POWER_MODE_CAM:
8569 case IWL_POWER_AC:
8570 p += sprintf(p, "(AC)");
8571 break;
8572 case IWL_POWER_BATTERY:
8573 p += sprintf(p, "(BATTERY)");
8574 break;
8575 default:
8576 p += sprintf(p,
8577 "(Timeout %dms, Period %dms)",
8578 timeout_duration[level - 1] / 1000,
8579 period_duration[level - 1] / 1000);
8580 }
8581
8582 if (!(priv->power_mode & IWL_POWER_ENABLED))
8583 p += sprintf(p, " OFF\n");
8584 else
8585 p += sprintf(p, " \n");
8586
8587 return (p - buf + 1);
8588
8589}
8590
8591static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8592 store_power_level);
8593
8594static ssize_t show_channels(struct device *d,
8595 struct device_attribute *attr, char *buf)
8596{
8597 struct iwl_priv *priv = dev_get_drvdata(d);
8598 int len = 0, i;
8599 struct ieee80211_channel *channels = NULL;
8600 const struct ieee80211_hw_mode *hw_mode = NULL;
8601 int count = 0;
8602
8603 if (!iwl_is_ready(priv))
8604 return -EAGAIN;
8605
8606 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211G);
8607 if (!hw_mode)
8608 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211B);
8609 if (hw_mode) {
8610 channels = hw_mode->channels;
8611 count = hw_mode->num_channels;
8612 }
8613
8614 len +=
8615 sprintf(&buf[len],
8616 "Displaying %d channels in 2.4GHz band "
8617 "(802.11bg):\n", count);
8618
8619 for (i = 0; i < count; i++)
8620 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8621 channels[i].chan,
8622 channels[i].power_level,
8623 channels[i].
8624 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8625 " (IEEE 802.11h required)" : "",
8626 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8627 || (channels[i].
8628 flag &
8629 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8630 ", IBSS",
8631 channels[i].
8632 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8633 "active/passive" : "passive only");
8634
8635 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211A);
8636 if (hw_mode) {
8637 channels = hw_mode->channels;
8638 count = hw_mode->num_channels;
8639 } else {
8640 channels = NULL;
8641 count = 0;
8642 }
8643
8644 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8645 "(802.11a):\n", count);
8646
8647 for (i = 0; i < count; i++)
8648 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8649 channels[i].chan,
8650 channels[i].power_level,
8651 channels[i].
8652 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8653 " (IEEE 802.11h required)" : "",
8654 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8655 || (channels[i].
8656 flag &
8657 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8658 ", IBSS",
8659 channels[i].
8660 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8661 "active/passive" : "passive only");
8662
8663 return len;
8664}
8665
8666static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8667
8668static ssize_t show_statistics(struct device *d,
8669 struct device_attribute *attr, char *buf)
8670{
8671 struct iwl_priv *priv = dev_get_drvdata(d);
8672 u32 size = sizeof(struct iwl_notif_statistics);
8673 u32 len = 0, ofs = 0;
8674 u8 *data = (u8 *) & priv->statistics;
8675 int rc = 0;
8676
8677 if (!iwl_is_alive(priv))
8678 return -EAGAIN;
8679
8680 mutex_lock(&priv->mutex);
8681 rc = iwl_send_statistics_request(priv);
8682 mutex_unlock(&priv->mutex);
8683
8684 if (rc) {
8685 len = sprintf(buf,
8686 "Error sending statistics request: 0x%08X\n", rc);
8687 return len;
8688 }
8689
8690 while (size && (PAGE_SIZE - len)) {
8691 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8692 PAGE_SIZE - len, 1);
8693 len = strlen(buf);
8694 if (PAGE_SIZE - len)
8695 buf[len++] = '\n';
8696
8697 ofs += 16;
8698 size -= min(size, 16U);
8699 }
8700
8701 return len;
8702}
8703
8704static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8705
8706static ssize_t show_antenna(struct device *d,
8707 struct device_attribute *attr, char *buf)
8708{
8709 struct iwl_priv *priv = dev_get_drvdata(d);
8710
8711 if (!iwl_is_alive(priv))
8712 return -EAGAIN;
8713
8714 return sprintf(buf, "%d\n", priv->antenna);
8715}
8716
8717static ssize_t store_antenna(struct device *d,
8718 struct device_attribute *attr,
8719 const char *buf, size_t count)
8720{
8721 int ant;
8722 struct iwl_priv *priv = dev_get_drvdata(d);
8723
8724 if (count == 0)
8725 return 0;
8726
8727 if (sscanf(buf, "%1i", &ant) != 1) {
8728 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8729 return count;
8730 }
8731
8732 if ((ant >= 0) && (ant <= 2)) {
8733 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
8734 priv->antenna = (enum iwl_antenna)ant;
8735 } else
8736 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8737
8738
8739 return count;
8740}
8741
8742static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8743
8744static ssize_t show_status(struct device *d,
8745 struct device_attribute *attr, char *buf)
8746{
8747 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8748 if (!iwl_is_alive(priv))
8749 return -EAGAIN;
8750 return sprintf(buf, "0x%08x\n", (int)priv->status);
8751}
8752
8753static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8754
8755static ssize_t dump_error_log(struct device *d,
8756 struct device_attribute *attr,
8757 const char *buf, size_t count)
8758{
8759 char *p = (char *)buf;
8760
8761 if (p[0] == '1')
8762 iwl_dump_nic_error_log((struct iwl_priv *)d->driver_data);
8763
8764 return strnlen(buf, count);
8765}
8766
8767static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8768
8769static ssize_t dump_event_log(struct device *d,
8770 struct device_attribute *attr,
8771 const char *buf, size_t count)
8772{
8773 char *p = (char *)buf;
8774
8775 if (p[0] == '1')
8776 iwl_dump_nic_event_log((struct iwl_priv *)d->driver_data);
8777
8778 return strnlen(buf, count);
8779}
8780
8781static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8782
8783/*****************************************************************************
8784 *
8785 * driver setup and teardown
8786 *
8787 *****************************************************************************/
8788
8789static void iwl_setup_deferred_work(struct iwl_priv *priv)
8790{
8791 priv->workqueue = create_workqueue(DRV_NAME);
8792
8793 init_waitqueue_head(&priv->wait_command_queue);
8794
8795 INIT_WORK(&priv->up, iwl_bg_up);
8796 INIT_WORK(&priv->restart, iwl_bg_restart);
8797 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
8798 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
8799 INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
8800 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
8801 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
8802 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
8803 INIT_DELAYED_WORK(&priv->post_associate, iwl_bg_post_associate);
8804 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
8805 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
8806 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
8807
8808 iwl_hw_setup_deferred_work(priv);
8809
8810 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
8811 iwl_irq_tasklet, (unsigned long)priv);
8812}
8813
8814static void iwl_cancel_deferred_work(struct iwl_priv *priv)
8815{
8816 iwl_hw_cancel_deferred_work(priv);
8817
8818 cancel_delayed_work(&priv->scan_check);
8819 cancel_delayed_work(&priv->alive_start);
8820 cancel_delayed_work(&priv->post_associate);
8821 cancel_work_sync(&priv->beacon_update);
8822}
8823
8824static struct attribute *iwl_sysfs_entries[] = {
8825 &dev_attr_antenna.attr,
8826 &dev_attr_channels.attr,
8827 &dev_attr_dump_errors.attr,
8828 &dev_attr_dump_events.attr,
8829 &dev_attr_flags.attr,
8830 &dev_attr_filter_flags.attr,
8831#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8832 &dev_attr_measurement.attr,
8833#endif
8834 &dev_attr_power_level.attr,
8835 &dev_attr_retry_rate.attr,
8836 &dev_attr_rf_kill.attr,
8837 &dev_attr_rs_window.attr,
8838 &dev_attr_statistics.attr,
8839 &dev_attr_status.attr,
8840 &dev_attr_temperature.attr,
8841 &dev_attr_tune.attr,
8842 &dev_attr_tx_power.attr,
8843
8844 NULL
8845};
8846
8847static struct attribute_group iwl_attribute_group = {
8848 .name = NULL, /* put in device directory */
8849 .attrs = iwl_sysfs_entries,
8850};
8851
8852static struct ieee80211_ops iwl_hw_ops = {
8853 .tx = iwl_mac_tx,
Johannes Berg4150c572007-09-17 01:29:23 -04008854 .start = iwl_mac_start,
Zhu Yib481de92007-09-25 17:54:57 -07008855 .stop = iwl_mac_stop,
8856 .add_interface = iwl_mac_add_interface,
8857 .remove_interface = iwl_mac_remove_interface,
8858 .config = iwl_mac_config,
8859 .config_interface = iwl_mac_config_interface,
Johannes Berg4150c572007-09-17 01:29:23 -04008860 .configure_filter = iwl_configure_filter,
Zhu Yib481de92007-09-25 17:54:57 -07008861 .set_key = iwl_mac_set_key,
8862 .get_stats = iwl_mac_get_stats,
8863 .get_tx_stats = iwl_mac_get_tx_stats,
8864 .conf_tx = iwl_mac_conf_tx,
8865 .get_tsf = iwl_mac_get_tsf,
8866 .reset_tsf = iwl_mac_reset_tsf,
8867 .beacon_update = iwl_mac_beacon_update,
8868#ifdef CONFIG_IWLWIFI_HT
8869 .conf_ht = iwl_mac_conf_ht,
8870 .get_ht_capab = iwl_mac_get_ht_capab,
8871#ifdef CONFIG_IWLWIFI_HT_AGG
8872 .ht_tx_agg_start = iwl_mac_ht_tx_agg_start,
8873 .ht_tx_agg_stop = iwl_mac_ht_tx_agg_stop,
8874 .ht_rx_agg_start = iwl_mac_ht_rx_agg_start,
8875 .ht_rx_agg_stop = iwl_mac_ht_rx_agg_stop,
8876#endif /* CONFIG_IWLWIFI_HT_AGG */
8877#endif /* CONFIG_IWLWIFI_HT */
8878 .hw_scan = iwl_mac_hw_scan
8879};
8880
8881static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8882{
8883 int err = 0;
8884 struct iwl_priv *priv;
8885 struct ieee80211_hw *hw;
8886 int i;
8887
8888 if (iwl_param_disable_hw_scan) {
8889 IWL_DEBUG_INFO("Disabling hw_scan\n");
8890 iwl_hw_ops.hw_scan = NULL;
8891 }
8892
8893 if ((iwl_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8894 (iwl_param_queues_num < IWL_MIN_NUM_QUEUES)) {
8895 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8896 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8897 err = -EINVAL;
8898 goto out;
8899 }
8900
8901 /* mac80211 allocates memory for this device instance, including
8902 * space for this driver's private structure */
8903 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl_hw_ops);
8904 if (hw == NULL) {
8905 IWL_ERROR("Can not allocate network device\n");
8906 err = -ENOMEM;
8907 goto out;
8908 }
8909 SET_IEEE80211_DEV(hw, &pdev->dev);
8910
8911 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8912 priv = hw->priv;
8913 priv->hw = hw;
8914
8915 priv->pci_dev = pdev;
8916 priv->antenna = (enum iwl_antenna)iwl_param_antenna;
8917#ifdef CONFIG_IWLWIFI_DEBUG
8918 iwl_debug_level = iwl_param_debug;
8919 atomic_set(&priv->restrict_refcnt, 0);
8920#endif
8921 priv->retry_rate = 1;
8922
8923 priv->ibss_beacon = NULL;
8924
8925 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8926 * the range of signal quality values that we'll provide.
8927 * Negative values for level/noise indicate that we'll provide dBm.
8928 * For WE, at least, non-0 values here *enable* display of values
8929 * in app (iwconfig). */
8930 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8931 hw->max_noise = -20; /* noise level, negative indicates dBm */
8932 hw->max_signal = 100; /* link quality indication (%) */
8933
8934 /* Tell mac80211 our Tx characteristics */
8935 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8936
8937 hw->queues = 4;
8938#ifdef CONFIG_IWLWIFI_HT
8939#ifdef CONFIG_IWLWIFI_HT_AGG
8940 hw->queues = 16;
8941#endif /* CONFIG_IWLWIFI_HT_AGG */
8942#endif /* CONFIG_IWLWIFI_HT */
8943
8944 spin_lock_init(&priv->lock);
8945 spin_lock_init(&priv->power_data.lock);
8946 spin_lock_init(&priv->sta_lock);
8947 spin_lock_init(&priv->hcmd_lock);
8948 spin_lock_init(&priv->lq_mngr.lock);
8949
8950 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8951 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8952
8953 INIT_LIST_HEAD(&priv->free_frames);
8954
8955 mutex_init(&priv->mutex);
8956 if (pci_enable_device(pdev)) {
8957 err = -ENODEV;
8958 goto out_ieee80211_free_hw;
8959 }
8960
8961 pci_set_master(pdev);
8962
8963 iwl_clear_stations_table(priv);
8964
8965 priv->data_retry_limit = -1;
8966 priv->ieee_channels = NULL;
8967 priv->ieee_rates = NULL;
8968 priv->phymode = -1;
8969
8970 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8971 if (!err)
8972 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8973 if (err) {
8974 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
8975 goto out_pci_disable_device;
8976 }
8977
8978 pci_set_drvdata(pdev, priv);
8979 err = pci_request_regions(pdev, DRV_NAME);
8980 if (err)
8981 goto out_pci_disable_device;
8982 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8983 * PCI Tx retries from interfering with C3 CPU state */
8984 pci_write_config_byte(pdev, 0x41, 0x00);
8985 priv->hw_base = pci_iomap(pdev, 0, 0);
8986 if (!priv->hw_base) {
8987 err = -ENODEV;
8988 goto out_pci_release_regions;
8989 }
8990
8991 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8992 (unsigned long long) pci_resource_len(pdev, 0));
8993 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8994
8995 /* Initialize module parameter values here */
8996
8997 if (iwl_param_disable) {
8998 set_bit(STATUS_RF_KILL_SW, &priv->status);
8999 IWL_DEBUG_INFO("Radio disabled.\n");
9000 }
9001
9002 priv->iw_mode = IEEE80211_IF_TYPE_STA;
9003
9004 priv->ps_mode = 0;
9005 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
9006 priv->is_ht_enabled = 1;
9007 priv->channel_width = IWL_CHANNEL_WIDTH_40MHZ;
9008 priv->valid_antenna = 0x7; /* assume all 3 connected */
9009 priv->ps_mode = IWL_MIMO_PS_NONE;
9010 priv->cck_power_index_compensation = iwl_read32(
9011 priv, CSR_HW_REV_WA_REG);
9012
9013 iwl4965_set_rxon_chain(priv);
9014
9015 printk(KERN_INFO DRV_NAME
9016 ": Detected Intel Wireless WiFi Link 4965AGN\n");
9017
9018 /* Device-specific setup */
9019 if (iwl_hw_set_hw_setting(priv)) {
9020 IWL_ERROR("failed to set hw settings\n");
9021 mutex_unlock(&priv->mutex);
9022 goto out_iounmap;
9023 }
9024
9025#ifdef CONFIG_IWLWIFI_QOS
9026 if (iwl_param_qos_enable)
9027 priv->qos_data.qos_enable = 1;
9028
9029 iwl_reset_qos(priv);
9030
9031 priv->qos_data.qos_active = 0;
9032 priv->qos_data.qos_cap.val = 0;
9033#endif /* CONFIG_IWLWIFI_QOS */
9034
9035 iwl_set_rxon_channel(priv, MODE_IEEE80211G, 6);
9036 iwl_setup_deferred_work(priv);
9037 iwl_setup_rx_handlers(priv);
9038
9039 priv->rates_mask = IWL_RATES_MASK;
9040 /* If power management is turned on, default to AC mode */
9041 priv->power_mode = IWL_POWER_AC;
9042 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
9043
9044 pci_enable_msi(pdev);
9045
9046 err = request_irq(pdev->irq, iwl_isr, IRQF_SHARED, DRV_NAME, priv);
9047 if (err) {
9048 IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
9049 goto out_disable_msi;
9050 }
9051
9052 mutex_lock(&priv->mutex);
9053
9054 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
9055 if (err) {
9056 IWL_ERROR("failed to create sysfs device attributes\n");
9057 mutex_unlock(&priv->mutex);
9058 goto out_release_irq;
9059 }
9060
9061 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
9062 * ucode filename and max sizes are card-specific. */
9063 err = iwl_read_ucode(priv);
9064 if (err) {
9065 IWL_ERROR("Could not read microcode: %d\n", err);
9066 mutex_unlock(&priv->mutex);
9067 goto out_pci_alloc;
9068 }
9069
9070 mutex_unlock(&priv->mutex);
9071
9072 IWL_DEBUG_INFO("Queing UP work.\n");
9073
9074 queue_work(priv->workqueue, &priv->up);
9075
9076 return 0;
9077
9078 out_pci_alloc:
9079 iwl_dealloc_ucode_pci(priv);
9080
9081 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
9082
9083 out_release_irq:
9084 free_irq(pdev->irq, priv);
9085
9086 out_disable_msi:
9087 pci_disable_msi(pdev);
9088 destroy_workqueue(priv->workqueue);
9089 priv->workqueue = NULL;
9090 iwl_unset_hw_setting(priv);
9091
9092 out_iounmap:
9093 pci_iounmap(pdev, priv->hw_base);
9094 out_pci_release_regions:
9095 pci_release_regions(pdev);
9096 out_pci_disable_device:
9097 pci_disable_device(pdev);
9098 pci_set_drvdata(pdev, NULL);
9099 out_ieee80211_free_hw:
9100 ieee80211_free_hw(priv->hw);
9101 out:
9102 return err;
9103}
9104
9105static void iwl_pci_remove(struct pci_dev *pdev)
9106{
9107 struct iwl_priv *priv = pci_get_drvdata(pdev);
9108 struct list_head *p, *q;
9109 int i;
9110
9111 if (!priv)
9112 return;
9113
9114 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
9115
9116 mutex_lock(&priv->mutex);
9117 set_bit(STATUS_EXIT_PENDING, &priv->status);
9118 __iwl_down(priv);
9119 mutex_unlock(&priv->mutex);
9120
9121 /* Free MAC hash list for ADHOC */
9122 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
9123 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
9124 list_del(p);
9125 kfree(list_entry(p, struct iwl_ibss_seq, list));
9126 }
9127 }
9128
9129 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
9130
9131 iwl_dealloc_ucode_pci(priv);
9132
9133 if (priv->rxq.bd)
9134 iwl_rx_queue_free(priv, &priv->rxq);
9135 iwl_hw_txq_ctx_free(priv);
9136
9137 iwl_unset_hw_setting(priv);
9138 iwl_clear_stations_table(priv);
9139
9140 if (priv->mac80211_registered) {
9141 ieee80211_unregister_hw(priv->hw);
9142 iwl_rate_control_unregister(priv->hw);
9143 }
9144
9145 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
9146 * priv->workqueue... so we can't take down the workqueue
9147 * until now... */
9148 destroy_workqueue(priv->workqueue);
9149 priv->workqueue = NULL;
9150
9151 free_irq(pdev->irq, priv);
9152 pci_disable_msi(pdev);
9153 pci_iounmap(pdev, priv->hw_base);
9154 pci_release_regions(pdev);
9155 pci_disable_device(pdev);
9156 pci_set_drvdata(pdev, NULL);
9157
9158 kfree(priv->channel_info);
9159
9160 kfree(priv->ieee_channels);
9161 kfree(priv->ieee_rates);
9162
9163 if (priv->ibss_beacon)
9164 dev_kfree_skb(priv->ibss_beacon);
9165
9166 ieee80211_free_hw(priv->hw);
9167}
9168
9169#ifdef CONFIG_PM
9170
9171static int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
9172{
9173 struct iwl_priv *priv = pci_get_drvdata(pdev);
9174
9175 mutex_lock(&priv->mutex);
9176
9177 set_bit(STATUS_IN_SUSPEND, &priv->status);
9178
9179 /* Take down the device; powers it off, etc. */
9180 __iwl_down(priv);
9181
9182 if (priv->mac80211_registered)
9183 ieee80211_stop_queues(priv->hw);
9184
9185 pci_save_state(pdev);
9186 pci_disable_device(pdev);
9187 pci_set_power_state(pdev, PCI_D3hot);
9188
9189 mutex_unlock(&priv->mutex);
9190
9191 return 0;
9192}
9193
9194static void iwl_resume(struct iwl_priv *priv)
9195{
9196 unsigned long flags;
9197
9198 /* The following it a temporary work around due to the
9199 * suspend / resume not fully initializing the NIC correctly.
9200 * Without all of the following, resume will not attempt to take
9201 * down the NIC (it shouldn't really need to) and will just try
9202 * and bring the NIC back up. However that fails during the
9203 * ucode verification process. This then causes iwl_down to be
9204 * called *after* iwl_hw_nic_init() has succeeded -- which
9205 * then lets the next init sequence succeed. So, we've
9206 * replicated all of that NIC init code here... */
9207
9208 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
9209
9210 iwl_hw_nic_init(priv);
9211
9212 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9213 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
9214 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
9215 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
9216 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9217 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9218
9219 /* tell the device to stop sending interrupts */
9220 iwl_disable_interrupts(priv);
9221
9222 spin_lock_irqsave(&priv->lock, flags);
9223 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
9224
9225 if (!iwl_grab_restricted_access(priv)) {
9226 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
9227 APMG_CLK_VAL_DMA_CLK_RQT);
9228 iwl_release_restricted_access(priv);
9229 }
9230 spin_unlock_irqrestore(&priv->lock, flags);
9231
9232 udelay(5);
9233
9234 iwl_hw_nic_reset(priv);
9235
9236 /* Bring the device back up */
9237 clear_bit(STATUS_IN_SUSPEND, &priv->status);
9238 queue_work(priv->workqueue, &priv->up);
9239}
9240
9241static int iwl_pci_resume(struct pci_dev *pdev)
9242{
9243 struct iwl_priv *priv = pci_get_drvdata(pdev);
9244 int err;
9245
9246 printk(KERN_INFO "Coming out of suspend...\n");
9247
9248 mutex_lock(&priv->mutex);
9249
9250 pci_set_power_state(pdev, PCI_D0);
9251 err = pci_enable_device(pdev);
9252 pci_restore_state(pdev);
9253
9254 /*
9255 * Suspend/Resume resets the PCI configuration space, so we have to
9256 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
9257 * from interfering with C3 CPU state. pci_restore_state won't help
9258 * here since it only restores the first 64 bytes pci config header.
9259 */
9260 pci_write_config_byte(pdev, 0x41, 0x00);
9261
9262 iwl_resume(priv);
9263 mutex_unlock(&priv->mutex);
9264
9265 return 0;
9266}
9267
9268#endif /* CONFIG_PM */
9269
9270/*****************************************************************************
9271 *
9272 * driver and module entry point
9273 *
9274 *****************************************************************************/
9275
9276static struct pci_driver iwl_driver = {
9277 .name = DRV_NAME,
9278 .id_table = iwl_hw_card_ids,
9279 .probe = iwl_pci_probe,
9280 .remove = __devexit_p(iwl_pci_remove),
9281#ifdef CONFIG_PM
9282 .suspend = iwl_pci_suspend,
9283 .resume = iwl_pci_resume,
9284#endif
9285};
9286
9287static int __init iwl_init(void)
9288{
9289
9290 int ret;
9291 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
9292 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
9293 ret = pci_register_driver(&iwl_driver);
9294 if (ret) {
9295 IWL_ERROR("Unable to initialize PCI module\n");
9296 return ret;
9297 }
9298#ifdef CONFIG_IWLWIFI_DEBUG
9299 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
9300 if (ret) {
9301 IWL_ERROR("Unable to create driver sysfs file\n");
9302 pci_unregister_driver(&iwl_driver);
9303 return ret;
9304 }
9305#endif
9306
9307 return ret;
9308}
9309
9310static void __exit iwl_exit(void)
9311{
9312#ifdef CONFIG_IWLWIFI_DEBUG
9313 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
9314#endif
9315 pci_unregister_driver(&iwl_driver);
9316}
9317
9318module_param_named(antenna, iwl_param_antenna, int, 0444);
9319MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
9320module_param_named(disable, iwl_param_disable, int, 0444);
9321MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
9322module_param_named(hwcrypto, iwl_param_hwcrypto, int, 0444);
9323MODULE_PARM_DESC(hwcrypto,
9324 "using hardware crypto engine (default 0 [software])\n");
9325module_param_named(debug, iwl_param_debug, int, 0444);
9326MODULE_PARM_DESC(debug, "debug output mask");
9327module_param_named(disable_hw_scan, iwl_param_disable_hw_scan, int, 0444);
9328MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
9329
9330module_param_named(queues_num, iwl_param_queues_num, int, 0444);
9331MODULE_PARM_DESC(queues_num, "number of hw queues.");
9332
9333/* QoS */
9334module_param_named(qos_enable, iwl_param_qos_enable, int, 0444);
9335MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
9336
9337module_exit(iwl_exit);
9338module_init(iwl_init);