blob: 33eace7574355e828b4f026052981f4cf34beb79 [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 Yib481de92007-09-25 17:54:57 -070059#include "iwl-4965.h"
60#include "iwl-helpers.h"
61
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080062#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080063u32 iwl4965_debug_level;
Zhu Yib481de92007-09-25 17:54:57 -070064#endif
65
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080066static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
67 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080068
Zhu Yib481de92007-09-25 17:54:57 -070069/******************************************************************************
70 *
71 * module boiler plate
72 *
73 ******************************************************************************/
74
75/* module parameters */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080076static int iwl4965_param_disable_hw_scan;
77static int iwl4965_param_debug;
78static int iwl4965_param_disable; /* def: enable radio */
79static int iwl4965_param_antenna; /* def: 0 = both antennas (use diversity) */
80int iwl4965_param_hwcrypto; /* def: using software encryption */
81static int iwl4965_param_qos_enable = 1;
82int iwl4965_param_queues_num = IWL_MAX_NUM_QUEUES;
Zhu Yib481de92007-09-25 17:54:57 -070083
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
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080091#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070092#define VD "d"
93#else
94#define VD
95#endif
96
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080097#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070098#define VS "s"
99#else
100#define VS
101#endif
102
Zhu Yi80f3e022007-10-25 17:15:48 +0800103#define IWLWIFI_VERSION "1.1.19k" 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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800128static const struct ieee80211_hw_mode *iwl4965_get_hw_mode(
129 struct iwl4965_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -0700130{
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800140static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700141{
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800156static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700157{
158 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
159 const char *s = essid;
160 char *d = escaped;
161
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800162 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700163 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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800180static void iwl4965_print_hex_dump(int level, void *p, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -0700181{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800182#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800183 if (!(iwl4965_debug_level & level))
Zhu Yib481de92007-09-25 17:54:57 -0700184 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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800209static int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700210{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800211 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700212
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800213 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700214 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 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800226static inline int iwl4965_queue_inc_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700227{
228 return ++index & (n_bd - 1);
229}
230
231/* XXX: n_bd must be power-of-two size */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800232static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700233{
234 return --index & (n_bd - 1);
235}
236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800237static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700238{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800239 return q->write_ptr > q->read_ptr ?
240 (i >= q->read_ptr && i < q->write_ptr) :
241 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700242}
243
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800244static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700245{
246 if (is_huge)
247 return q->n_window;
248
249 return index & (q->n_window - 1);
250}
251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800252static int iwl4965_queue_init(struct iwl4965_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700253 int count, int slots_num, u32 id)
254{
255 q->n_bd = count;
256 q->n_window = slots_num;
257 q->id = id;
258
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800259 /* count must be power-of-two size, otherwise iwl4965_queue_inc_wrap
260 * and iwl4965_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700261 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
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800275 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700276
277 return 0;
278}
279
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800280static int iwl4965_tx_queue_alloc(struct iwl4965_priv *priv,
281 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700282{
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) {
Ian Schram01ebd062007-10-25 17:15:22 +0800289 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700290 "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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800318int iwl4965_tx_queue_init(struct iwl4965_priv *priv,
319 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700320{
321 struct pci_dev *dev = priv->pci_dev;
322 int len;
323 int rc = 0;
324
Ian Schram01ebd062007-10-25 17:15:22 +0800325 /* allocate command space + one big command for scan since scan
Zhu Yib481de92007-09-25 17:54:57 -0700326 * command is very huge the system will not have two scan at the
327 * same time */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800328 len = sizeof(struct iwl4965_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700329 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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800335 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700336 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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800344 * iwl4965_queue_inc_wrap and iwl4965_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700345 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800346 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800348 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700349
350 return 0;
351}
352
353/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800354 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700355 * @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 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800361void iwl4965_tx_queue_free(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700362{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800363 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700364 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 */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800371 for (; q->write_ptr != q->read_ptr;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800372 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd))
373 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700374
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800375 len = sizeof(struct iwl4965_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700376 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)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800383 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700384 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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800395const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700396
397/*************** STATION TABLE MANAGEMENT ****
398 *
399 * NOTE: This needs to be overhauled to better synchronize between
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800400 * how the iwl-4965.c is using iwl4965_hw_find_station vs. iwl-3945.c
Zhu Yib481de92007-09-25 17:54:57 -0700401 *
402 * mac80211 should also be examined to determine if sta_info is duplicating
403 * the functionality provided here
404 */
405
406/**************************************************************/
407
Ian Schram01ebd062007-10-25 17:15:22 +0800408#if 0 /* temporary disable till we add real remove station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800409static u8 iwl4965_remove_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700410{
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800446static void iwl4965_clear_stations_table(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700447{
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800458u8 iwl4965_add_station_flags(struct iwl4965_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700459{
460 int i;
461 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800462 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700463 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
Ian Schram01ebd062007-10-25 17:15:22 +0800485 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700486 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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800504 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700505 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
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800510#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700511 /* 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);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800515#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700516
517 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800518 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700519 return index;
520
521}
522
523/*************** DRIVER STATUS FUNCTIONS *****/
524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800525static inline int iwl4965_is_ready(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700526{
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800534static inline int iwl4965_is_alive(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700535{
536 return test_bit(STATUS_ALIVE, &priv->status);
537}
538
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800539static inline int iwl4965_is_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700540{
541 return test_bit(STATUS_INIT, &priv->status);
542}
543
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800544static inline int iwl4965_is_rfkill(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700545{
546 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
547 test_bit(STATUS_RF_KILL_SW, &priv->status);
548}
549
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800550static inline int iwl4965_is_ready_rf(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700551{
552
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800553 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700554 return 0;
555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800556 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700557}
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/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800621 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700622 * @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 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800629static int iwl4965_enqueue_hcmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700630{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800631 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
632 struct iwl4965_queue *q = &txq->q;
633 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700634 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800635 struct iwl4965_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700636 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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800648 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700649 IWL_ERROR("No space for Tx\n");
650 return -ENOSPC;
651 }
652
653 spin_lock_irqsave(&priv->hcmd_lock, flags);
654
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800655 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700656 memset(tfd, 0, sizeof(*tfd));
657
658 control_flags = (u32 *) tfd;
659
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800660 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700661 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) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800672 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700673 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 +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800677 offsetof(struct iwl4965_cmd, hdr);
678 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700679
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),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800684 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700685
686 txq->need_update = 1;
687 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800688 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
689 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700690
691 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
692 return ret ? ret : idx;
693}
694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800695static int iwl4965_send_cmd_async(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700696{
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800710 ret = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700711 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800712 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700713 get_cmd_string(cmd->id), ret);
714 return ret;
715 }
716 return 0;
717}
718
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800719static int iwl4965_send_cmd_sync(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700720{
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800741 cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700742 if (cmd_idx < 0) {
743 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800744 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700745 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) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800788 struct iwl4965_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700789
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800807int iwl4965_send_cmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700808{
Zhu Yib481de92007-09-25 17:54:57 -0700809 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800810 return iwl4965_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700811
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800812 return iwl4965_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700813}
814
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800815int iwl4965_send_cmd_pdu(struct iwl4965_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700816{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800817 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700818 .id = id,
819 .len = len,
820 .data = data,
821 };
822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700824}
825
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800826static int __must_check iwl4965_send_cmd_u32(struct iwl4965_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700827{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800828 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700829 .id = id,
830 .len = sizeof(val),
831 .data = &val,
832 };
833
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800834 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700835}
836
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800837int iwl4965_send_statistics_request(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700838{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800839 return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700840}
841
842/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800843 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700844 *
845 * there is only one AP station with id= IWL_AP_ID
846 * NOTE: mutex must be held before calling the this fnction
847*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800848static int iwl4965_rxon_add_station(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700849 const u8 *addr, int is_ap)
850{
Zhu Yi556f8db2007-09-27 11:27:33 +0800851 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700852
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800853 sta_id = iwl4965_add_station_flags(priv, addr, is_ap, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700854 iwl4965_add_station(priv, addr, is_ap);
855
Zhu Yi556f8db2007-09-27 11:27:33 +0800856 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700857}
858
859/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800860 * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
Zhu Yib481de92007-09-25 17:54:57 -0700861 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
862 * @channel: Any channel valid for the requested phymode
863
864 * In addition to setting the staging RXON, priv->phymode is also set.
865 *
866 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
867 * in the staging RXON flag structure based on the phymode
868 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800869static int iwl4965_set_rxon_channel(struct iwl4965_priv *priv, u8 phymode, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700870{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800871 if (!iwl4965_get_channel_info(priv, phymode, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700872 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
873 channel, phymode);
874 return -EINVAL;
875 }
876
877 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
878 (priv->phymode == phymode))
879 return 0;
880
881 priv->staging_rxon.channel = cpu_to_le16(channel);
882 if (phymode == MODE_IEEE80211A)
883 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
884 else
885 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
886
887 priv->phymode = phymode;
888
889 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
890
891 return 0;
892}
893
894/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800895 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700896 *
897 * NOTE: This is really only useful during development and can eventually
898 * be #ifdef'd out once the driver is stable and folks aren't actively
899 * making changes
900 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800901static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700902{
903 int error = 0;
904 int counter = 1;
905
906 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
907 error |= le32_to_cpu(rxon->flags &
908 (RXON_FLG_TGJ_NARROW_BAND_MSK |
909 RXON_FLG_RADAR_DETECT_MSK));
910 if (error)
911 IWL_WARNING("check 24G fields %d | %d\n",
912 counter++, error);
913 } else {
914 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
915 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
916 if (error)
917 IWL_WARNING("check 52 fields %d | %d\n",
918 counter++, error);
919 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
920 if (error)
921 IWL_WARNING("check 52 CCK %d | %d\n",
922 counter++, error);
923 }
924 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
925 if (error)
926 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
927
928 /* make sure basic rates 6Mbps and 1Mbps are supported */
929 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
930 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
931 if (error)
932 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
933
934 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
935 if (error)
936 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
937
938 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
939 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
940 if (error)
941 IWL_WARNING("check CCK and short slot %d | %d\n",
942 counter++, error);
943
944 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
945 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
946 if (error)
947 IWL_WARNING("check CCK & auto detect %d | %d\n",
948 counter++, error);
949
950 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
951 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
952 if (error)
953 IWL_WARNING("check TGG and auto detect %d | %d\n",
954 counter++, error);
955
956 if (error)
957 IWL_WARNING("Tuning to channel %d\n",
958 le16_to_cpu(rxon->channel));
959
960 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800961 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700962 return -1;
963 }
964 return 0;
965}
966
967/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800968 * iwl4965_full_rxon_required - determine if RXON_ASSOC can be used in RXON commit
Ian Schram01ebd062007-10-25 17:15:22 +0800969 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700970 *
971 * If the RXON structure is changing sufficient to require a new
972 * tune or to clear and reset the RXON_FILTER_ASSOC_MSK then return 1
973 * to indicate a new tune is required.
974 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800975static int iwl4965_full_rxon_required(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700976{
977
978 /* These items are only settable from the full RXON command */
979 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
980 compare_ether_addr(priv->staging_rxon.bssid_addr,
981 priv->active_rxon.bssid_addr) ||
982 compare_ether_addr(priv->staging_rxon.node_addr,
983 priv->active_rxon.node_addr) ||
984 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
985 priv->active_rxon.wlap_bssid_addr) ||
986 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
987 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
988 (priv->staging_rxon.air_propagation !=
989 priv->active_rxon.air_propagation) ||
990 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
991 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
992 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
993 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
994 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
995 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
996 return 1;
997
998 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
999 * be updated with the RXON_ASSOC command -- however only some
1000 * flag transitions are allowed using RXON_ASSOC */
1001
1002 /* Check if we are not switching bands */
1003 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1004 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1005 return 1;
1006
1007 /* Check if we are switching association toggle */
1008 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1009 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1010 return 1;
1011
1012 return 0;
1013}
1014
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001015static int iwl4965_send_rxon_assoc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001016{
1017 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001018 struct iwl4965_rx_packet *res = NULL;
1019 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1020 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001021 .id = REPLY_RXON_ASSOC,
1022 .len = sizeof(rxon_assoc),
1023 .meta.flags = CMD_WANT_SKB,
1024 .data = &rxon_assoc,
1025 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001026 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1027 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001028
1029 if ((rxon1->flags == rxon2->flags) &&
1030 (rxon1->filter_flags == rxon2->filter_flags) &&
1031 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1032 (rxon1->ofdm_ht_single_stream_basic_rates ==
1033 rxon2->ofdm_ht_single_stream_basic_rates) &&
1034 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1035 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1036 (rxon1->rx_chain == rxon2->rx_chain) &&
1037 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1038 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1039 return 0;
1040 }
1041
1042 rxon_assoc.flags = priv->staging_rxon.flags;
1043 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1044 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1045 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1046 rxon_assoc.reserved = 0;
1047 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1048 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1049 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1050 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1051 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001053 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001054 if (rc)
1055 return rc;
1056
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001057 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001058 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1059 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1060 rc = -EIO;
1061 }
1062
1063 priv->alloc_rxb_skb--;
1064 dev_kfree_skb_any(cmd.meta.u.skb);
1065
1066 return rc;
1067}
1068
1069/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001070 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001071 *
Ian Schram01ebd062007-10-25 17:15:22 +08001072 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001073 * the active_rxon structure is updated with the new data. This
1074 * function correctly transitions out of the RXON_ASSOC_MSK state if
1075 * a HW tune is required based on the RXON structure changes.
1076 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001077static int iwl4965_commit_rxon(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001078{
1079 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001080 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001081 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001082 int rc = 0;
1083
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001084 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001085 return -1;
1086
1087 /* always get timestamp with Rx frame */
1088 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1089
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001090 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001091 if (rc) {
1092 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1093 return -EINVAL;
1094 }
1095
1096 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001097 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001098 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001099 if (!iwl4965_full_rxon_required(priv)) {
1100 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001101 if (rc) {
1102 IWL_ERROR("Error setting RXON_ASSOC "
1103 "configuration (%d).\n", rc);
1104 return rc;
1105 }
1106
1107 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1108
1109 return 0;
1110 }
1111
1112 /* station table will be cleared */
1113 priv->assoc_station_added = 0;
1114
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001115#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001116 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1117 if (!priv->error_recovering)
1118 priv->start_calib = 0;
1119
1120 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001121#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001122
1123 /* If we are currently associated and the new config requires
1124 * an RXON_ASSOC and the new config wants the associated mask enabled,
1125 * we must clear the associated from the active configuration
1126 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001127 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001128 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1129 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1130 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1131
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001132 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1133 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001134 &priv->active_rxon);
1135
1136 /* If the mask clearing failed then we set
1137 * active_rxon back to what it was previously */
1138 if (rc) {
1139 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1140 IWL_ERROR("Error clearing ASSOC_MSK on current "
1141 "configuration (%d).\n", rc);
1142 return rc;
1143 }
Zhu Yib481de92007-09-25 17:54:57 -07001144 }
1145
1146 IWL_DEBUG_INFO("Sending RXON\n"
1147 "* with%s RXON_FILTER_ASSOC_MSK\n"
1148 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001149 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001150 ((priv->staging_rxon.filter_flags &
1151 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1152 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001153 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001154
1155 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001156 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1157 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001158 if (rc) {
1159 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1160 return rc;
1161 }
1162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001163 iwl4965_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001164
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001165#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001166 if (!priv->error_recovering)
1167 priv->start_calib = 0;
1168
1169 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1170 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001171#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001172
1173 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1174
1175 /* If we issue a new RXON command which required a tune then we must
1176 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001177 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001178 if (rc) {
1179 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1180 return rc;
1181 }
1182
1183 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001184 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001185 IWL_INVALID_STATION) {
1186 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1187 return -EIO;
1188 }
1189
1190 /* If we have set the ASSOC_MSK and we are in BSS mode then
1191 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001192 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001193 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001194 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -07001195 == IWL_INVALID_STATION) {
1196 IWL_ERROR("Error adding AP address for transmit.\n");
1197 return -EIO;
1198 }
1199 priv->assoc_station_added = 1;
1200 }
1201
1202 return 0;
1203}
1204
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001205static int iwl4965_send_bt_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001206{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001207 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001208 .flags = 3,
1209 .lead_time = 0xAA,
1210 .max_kill = 1,
1211 .kill_ack_mask = 0,
1212 .kill_cts_mask = 0,
1213 };
1214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001215 return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1216 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001217}
1218
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001219static int iwl4965_send_scan_abort(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001220{
1221 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001222 struct iwl4965_rx_packet *res;
1223 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001224 .id = REPLY_SCAN_ABORT_CMD,
1225 .meta.flags = CMD_WANT_SKB,
1226 };
1227
1228 /* If there isn't a scan actively going on in the hardware
1229 * then we are in between scan bands and not actually
1230 * actively scanning, so don't send the abort command */
1231 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1232 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1233 return 0;
1234 }
1235
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001236 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001237 if (rc) {
1238 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1239 return rc;
1240 }
1241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001242 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001243 if (res->u.status != CAN_ABORT_STATUS) {
1244 /* The scan abort will return 1 for success or
1245 * 2 for "failure". A failure condition can be
1246 * due to simply not being in an active scan which
1247 * can occur if we send the scan abort before we
1248 * the microcode has notified us that a scan is
1249 * completed. */
1250 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1251 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1252 clear_bit(STATUS_SCAN_HW, &priv->status);
1253 }
1254
1255 dev_kfree_skb_any(cmd.meta.u.skb);
1256
1257 return rc;
1258}
1259
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001260static int iwl4965_card_state_sync_callback(struct iwl4965_priv *priv,
1261 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001262 struct sk_buff *skb)
1263{
1264 return 1;
1265}
1266
1267/*
1268 * CARD_STATE_CMD
1269 *
1270 * Use: Sets the internal card state to enable, disable, or halt
1271 *
1272 * When in the 'enable' state the card operates as normal.
1273 * When in the 'disable' state, the card enters into a low power mode.
1274 * When in the 'halt' state, the card is shut down and must be fully
1275 * restarted to come back on.
1276 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001277static int iwl4965_send_card_state(struct iwl4965_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001278{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001279 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001280 .id = REPLY_CARD_STATE_CMD,
1281 .len = sizeof(u32),
1282 .data = &flags,
1283 .meta.flags = meta_flag,
1284 };
1285
1286 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001287 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001288
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001289 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001290}
1291
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001292static int iwl4965_add_sta_sync_callback(struct iwl4965_priv *priv,
1293 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001294{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001295 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001296
1297 if (!skb) {
1298 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1299 return 1;
1300 }
1301
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001302 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001303 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1304 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1305 res->hdr.flags);
1306 return 1;
1307 }
1308
1309 switch (res->u.add_sta.status) {
1310 case ADD_STA_SUCCESS_MSK:
1311 break;
1312 default:
1313 break;
1314 }
1315
1316 /* We didn't cache the SKB; let the caller free it */
1317 return 1;
1318}
1319
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001320int iwl4965_send_add_station(struct iwl4965_priv *priv,
1321 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001322{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001323 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001324 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001325 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001326 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001327 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001328 .meta.flags = flags,
1329 .data = sta,
1330 };
1331
1332 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001333 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001334 else
1335 cmd.meta.flags |= CMD_WANT_SKB;
1336
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001337 rc = iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001338
1339 if (rc || (flags & CMD_ASYNC))
1340 return rc;
1341
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001342 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001343 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1344 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1345 res->hdr.flags);
1346 rc = -EIO;
1347 }
1348
1349 if (rc == 0) {
1350 switch (res->u.add_sta.status) {
1351 case ADD_STA_SUCCESS_MSK:
1352 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1353 break;
1354 default:
1355 rc = -EIO;
1356 IWL_WARNING("REPLY_ADD_STA failed\n");
1357 break;
1358 }
1359 }
1360
1361 priv->alloc_rxb_skb--;
1362 dev_kfree_skb_any(cmd.meta.u.skb);
1363
1364 return rc;
1365}
1366
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001367static int iwl4965_update_sta_key_info(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001368 struct ieee80211_key_conf *keyconf,
1369 u8 sta_id)
1370{
1371 unsigned long flags;
1372 __le16 key_flags = 0;
1373
1374 switch (keyconf->alg) {
1375 case ALG_CCMP:
1376 key_flags |= STA_KEY_FLG_CCMP;
1377 key_flags |= cpu_to_le16(
1378 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1379 key_flags &= ~STA_KEY_FLG_INVALID;
1380 break;
1381 case ALG_TKIP:
1382 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001383 default:
1384 return -EINVAL;
1385 }
1386 spin_lock_irqsave(&priv->sta_lock, flags);
1387 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1388 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1389 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1390 keyconf->keylen);
1391
1392 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1393 keyconf->keylen);
1394 priv->stations[sta_id].sta.key.key_flags = key_flags;
1395 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1396 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1397
1398 spin_unlock_irqrestore(&priv->sta_lock, flags);
1399
1400 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001401 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001402 return 0;
1403}
1404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001405static int iwl4965_clear_sta_key_info(struct iwl4965_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001406{
1407 unsigned long flags;
1408
1409 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001410 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1411 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001412 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1413 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1414 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1415 spin_unlock_irqrestore(&priv->sta_lock, flags);
1416
1417 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001418 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001419 return 0;
1420}
1421
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001422static void iwl4965_clear_free_frames(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001423{
1424 struct list_head *element;
1425
1426 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1427 priv->frames_count);
1428
1429 while (!list_empty(&priv->free_frames)) {
1430 element = priv->free_frames.next;
1431 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001432 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001433 priv->frames_count--;
1434 }
1435
1436 if (priv->frames_count) {
1437 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1438 priv->frames_count);
1439 priv->frames_count = 0;
1440 }
1441}
1442
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001443static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001444{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001445 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001446 struct list_head *element;
1447 if (list_empty(&priv->free_frames)) {
1448 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1449 if (!frame) {
1450 IWL_ERROR("Could not allocate frame!\n");
1451 return NULL;
1452 }
1453
1454 priv->frames_count++;
1455 return frame;
1456 }
1457
1458 element = priv->free_frames.next;
1459 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001460 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001461}
1462
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001463static void iwl4965_free_frame(struct iwl4965_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001464{
1465 memset(frame, 0, sizeof(*frame));
1466 list_add(&frame->list, &priv->free_frames);
1467}
1468
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001469unsigned int iwl4965_fill_beacon_frame(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001470 struct ieee80211_hdr *hdr,
1471 const u8 *dest, int left)
1472{
1473
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001474 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001475 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1476 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1477 return 0;
1478
1479 if (priv->ibss_beacon->len > left)
1480 return 0;
1481
1482 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1483
1484 return priv->ibss_beacon->len;
1485}
1486
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001487int iwl4965_rate_index_from_plcp(int plcp)
Zhu Yib481de92007-09-25 17:54:57 -07001488{
1489 int i = 0;
1490
Ben Cahill77626352007-11-29 11:09:44 +08001491 /* 4965 HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -07001492 if (plcp & RATE_MCS_HT_MSK) {
1493 i = (plcp & 0xff);
1494
1495 if (i >= IWL_RATE_MIMO_6M_PLCP)
1496 i = i - IWL_RATE_MIMO_6M_PLCP;
1497
1498 i += IWL_FIRST_OFDM_RATE;
1499 /* skip 9M not supported in ht*/
1500 if (i >= IWL_RATE_9M_INDEX)
1501 i += 1;
1502 if ((i >= IWL_FIRST_OFDM_RATE) &&
1503 (i <= IWL_LAST_OFDM_RATE))
1504 return i;
Ben Cahill77626352007-11-29 11:09:44 +08001505
1506 /* 4965 legacy rate format, search for match in table */
Zhu Yib481de92007-09-25 17:54:57 -07001507 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001508 for (i = 0; i < ARRAY_SIZE(iwl4965_rates); i++)
1509 if (iwl4965_rates[i].plcp == (plcp &0xFF))
Zhu Yib481de92007-09-25 17:54:57 -07001510 return i;
1511 }
1512 return -1;
1513}
1514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001515static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001516{
1517 u8 i;
1518
1519 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001520 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001521 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001522 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001523 }
1524
1525 return IWL_RATE_INVALID;
1526}
1527
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001528static int iwl4965_send_beacon_cmd(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001529{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001530 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001531 unsigned int frame_size;
1532 int rc;
1533 u8 rate;
1534
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001535 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001536
1537 if (!frame) {
1538 IWL_ERROR("Could not obtain free frame buffer for beacon "
1539 "command.\n");
1540 return -ENOMEM;
1541 }
1542
1543 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001544 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001545 0xFF0);
1546 if (rate == IWL_INVALID_RATE)
1547 rate = IWL_RATE_6M_PLCP;
1548 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001549 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001550 if (rate == IWL_INVALID_RATE)
1551 rate = IWL_RATE_1M_PLCP;
1552 }
1553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001554 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001556 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001557 &frame->u.cmd[0]);
1558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001559 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001560
1561 return rc;
1562}
1563
1564/******************************************************************************
1565 *
1566 * EEPROM related functions
1567 *
1568 ******************************************************************************/
1569
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001570static void get_eeprom_mac(struct iwl4965_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001571{
1572 memcpy(mac, priv->eeprom.mac_address, 6);
1573}
1574
1575/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001576 * iwl4965_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001577 *
1578 * Load the EEPROM from adapter into priv->eeprom
1579 *
1580 * NOTE: This routine uses the non-debug IO access functions.
1581 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001582int iwl4965_eeprom_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001583{
1584 u16 *e = (u16 *)&priv->eeprom;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001585 u32 gp = iwl4965_read32(priv, CSR_EEPROM_GP);
Zhu Yib481de92007-09-25 17:54:57 -07001586 u32 r;
1587 int sz = sizeof(priv->eeprom);
1588 int rc;
1589 int i;
1590 u16 addr;
1591
1592 /* The EEPROM structure has several padding buffers within it
1593 * and when adding new EEPROM maps is subject to programmer errors
1594 * which may be very difficult to identify without explicitly
1595 * checking the resulting size of the eeprom map. */
1596 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1597
1598 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1599 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1600 return -ENOENT;
1601 }
1602
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001603 rc = iwl4965_eeprom_acquire_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001604 if (rc < 0) {
Ian Schram91e17472007-10-25 17:15:23 +08001605 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001606 return -ENOENT;
1607 }
1608
1609 /* eeprom is an array of 16bit values */
1610 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001611 _iwl4965_write32(priv, CSR_EEPROM_REG, addr << 1);
1612 _iwl4965_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
Zhu Yib481de92007-09-25 17:54:57 -07001613
1614 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1615 i += IWL_EEPROM_ACCESS_DELAY) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001616 r = _iwl4965_read_direct32(priv, CSR_EEPROM_REG);
Zhu Yib481de92007-09-25 17:54:57 -07001617 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1618 break;
1619 udelay(IWL_EEPROM_ACCESS_DELAY);
1620 }
1621
1622 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1623 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1624 rc = -ETIMEDOUT;
1625 goto done;
1626 }
1627 e[addr / 2] = le16_to_cpu(r >> 16);
1628 }
1629 rc = 0;
1630
1631done:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001632 iwl4965_eeprom_release_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001633 return rc;
1634}
1635
1636/******************************************************************************
1637 *
1638 * Misc. internal state and helper functions
1639 *
1640 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001641#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001642
1643/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001644 * iwl4965_report_frame - dump frame to syslog during debug sessions
Zhu Yib481de92007-09-25 17:54:57 -07001645 *
1646 * hack this function to show different aspects of received frames,
1647 * including selective frame dumps.
1648 * group100 parameter selects whether to show 1 out of 100 good frames.
1649 *
1650 * TODO: ieee80211_hdr stuff is common to 3945 and 4965, so frame type
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001651 * info output is okay, but some of this stuff (e.g. iwl4965_rx_frame_stats)
Zhu Yib481de92007-09-25 17:54:57 -07001652 * is 3945-specific and gives bad output for 4965. Need to split the
1653 * functionality, keep common stuff here.
1654 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001655void iwl4965_report_frame(struct iwl4965_priv *priv,
1656 struct iwl4965_rx_packet *pkt,
Zhu Yib481de92007-09-25 17:54:57 -07001657 struct ieee80211_hdr *header, int group100)
1658{
1659 u32 to_us;
1660 u32 print_summary = 0;
1661 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1662 u32 hundred = 0;
1663 u32 dataframe = 0;
1664 u16 fc;
1665 u16 seq_ctl;
1666 u16 channel;
1667 u16 phy_flags;
1668 int rate_sym;
1669 u16 length;
1670 u16 status;
1671 u16 bcn_tmr;
1672 u32 tsf_low;
1673 u64 tsf;
1674 u8 rssi;
1675 u8 agc;
1676 u16 sig_avg;
1677 u16 noise_diff;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001678 struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1679 struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1680 struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
Zhu Yib481de92007-09-25 17:54:57 -07001681 u8 *data = IWL_RX_DATA(pkt);
1682
1683 /* MAC header */
1684 fc = le16_to_cpu(header->frame_control);
1685 seq_ctl = le16_to_cpu(header->seq_ctrl);
1686
1687 /* metadata */
1688 channel = le16_to_cpu(rx_hdr->channel);
1689 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1690 rate_sym = rx_hdr->rate;
1691 length = le16_to_cpu(rx_hdr->len);
1692
1693 /* end-of-frame status and timestamp */
1694 status = le32_to_cpu(rx_end->status);
1695 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1696 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1697 tsf = le64_to_cpu(rx_end->timestamp);
1698
1699 /* signal statistics */
1700 rssi = rx_stats->rssi;
1701 agc = rx_stats->agc;
1702 sig_avg = le16_to_cpu(rx_stats->sig_avg);
1703 noise_diff = le16_to_cpu(rx_stats->noise_diff);
1704
1705 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1706
1707 /* if data frame is to us and all is good,
1708 * (optionally) print summary for only 1 out of every 100 */
1709 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1710 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1711 dataframe = 1;
1712 if (!group100)
1713 print_summary = 1; /* print each frame */
1714 else if (priv->framecnt_to_us < 100) {
1715 priv->framecnt_to_us++;
1716 print_summary = 0;
1717 } else {
1718 priv->framecnt_to_us = 0;
1719 print_summary = 1;
1720 hundred = 1;
1721 }
1722 } else {
1723 /* print summary for all other frames */
1724 print_summary = 1;
1725 }
1726
1727 if (print_summary) {
1728 char *title;
1729 u32 rate;
1730
1731 if (hundred)
1732 title = "100Frames";
1733 else if (fc & IEEE80211_FCTL_RETRY)
1734 title = "Retry";
1735 else if (ieee80211_is_assoc_response(fc))
1736 title = "AscRsp";
1737 else if (ieee80211_is_reassoc_response(fc))
1738 title = "RasRsp";
1739 else if (ieee80211_is_probe_response(fc)) {
1740 title = "PrbRsp";
1741 print_dump = 1; /* dump frame contents */
1742 } else if (ieee80211_is_beacon(fc)) {
1743 title = "Beacon";
1744 print_dump = 1; /* dump frame contents */
1745 } else if (ieee80211_is_atim(fc))
1746 title = "ATIM";
1747 else if (ieee80211_is_auth(fc))
1748 title = "Auth";
1749 else if (ieee80211_is_deauth(fc))
1750 title = "DeAuth";
1751 else if (ieee80211_is_disassoc(fc))
1752 title = "DisAssoc";
1753 else
1754 title = "Frame";
1755
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001756 rate = iwl4965_rate_index_from_plcp(rate_sym);
Zhu Yib481de92007-09-25 17:54:57 -07001757 if (rate == -1)
1758 rate = 0;
1759 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001760 rate = iwl4965_rates[rate].ieee / 2;
Zhu Yib481de92007-09-25 17:54:57 -07001761
1762 /* print frame summary.
1763 * MAC addresses show just the last byte (for brevity),
1764 * but you can hack it to show more, if you'd like to. */
1765 if (dataframe)
1766 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1767 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1768 title, fc, header->addr1[5],
1769 length, rssi, channel, rate);
1770 else {
1771 /* src/dst addresses assume managed mode */
1772 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1773 "src=0x%02x, rssi=%u, tim=%lu usec, "
1774 "phy=0x%02x, chnl=%d\n",
1775 title, fc, header->addr1[5],
1776 header->addr3[5], rssi,
1777 tsf_low - priv->scan_start_tsf,
1778 phy_flags, channel);
1779 }
1780 }
1781 if (print_dump)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001782 iwl4965_print_hex_dump(IWL_DL_RX, data, length);
Zhu Yib481de92007-09-25 17:54:57 -07001783}
1784#endif
1785
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001786static void iwl4965_unset_hw_setting(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001787{
1788 if (priv->hw_setting.shared_virt)
1789 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001790 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001791 priv->hw_setting.shared_virt,
1792 priv->hw_setting.shared_phys);
1793}
1794
1795/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001796 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001797 *
1798 * return : set the bit for each supported rate insert in ie
1799 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001800static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001801 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001802{
1803 u16 ret_rates = 0, bit;
1804 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001805 u8 *cnt = ie;
1806 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001807
1808 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1809 if (bit & supported_rate) {
1810 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001811 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001812 ((bit & basic_rate) ? 0x80 : 0x00);
1813 (*cnt)++;
1814 (*left)--;
1815 if ((*left <= 0) ||
1816 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001817 break;
1818 }
1819 }
1820
1821 return ret_rates;
1822}
1823
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001824#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001825void static iwl4965_set_ht_capab(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07001826 struct ieee80211_ht_capability *ht_cap,
1827 u8 use_wide_chan);
1828#endif
1829
1830/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001831 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001832 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001833static u16 iwl4965_fill_probe_req(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001834 struct ieee80211_mgmt *frame,
1835 int left, int is_direct)
1836{
1837 int len = 0;
1838 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001839 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Zhu Yib481de92007-09-25 17:54:57 -07001840
1841 /* Make sure there is enough space for the probe request,
1842 * two mandatory IEs and the data */
1843 left -= 24;
1844 if (left < 0)
1845 return 0;
1846 len += 24;
1847
1848 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001849 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001850 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001851 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001852 frame->seq_ctrl = 0;
1853
1854 /* fill in our indirect SSID IE */
1855 /* ...next IE... */
1856
1857 left -= 2;
1858 if (left < 0)
1859 return 0;
1860 len += 2;
1861 pos = &(frame->u.probe_req.variable[0]);
1862 *pos++ = WLAN_EID_SSID;
1863 *pos++ = 0;
1864
1865 /* fill in our direct SSID IE... */
1866 if (is_direct) {
1867 /* ...next IE... */
1868 left -= 2 + priv->essid_len;
1869 if (left < 0)
1870 return 0;
1871 /* ... fill it in... */
1872 *pos++ = WLAN_EID_SSID;
1873 *pos++ = priv->essid_len;
1874 memcpy(pos, priv->essid, priv->essid_len);
1875 pos += priv->essid_len;
1876 len += 2 + priv->essid_len;
1877 }
1878
1879 /* fill in supported rate */
1880 /* ...next IE... */
1881 left -= 2;
1882 if (left < 0)
1883 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001884
Zhu Yib481de92007-09-25 17:54:57 -07001885 /* ... fill it in... */
1886 *pos++ = WLAN_EID_SUPP_RATES;
1887 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001888
mabbasbee488d2007-10-25 17:15:42 +08001889 /* exclude 60M rate */
1890 active_rates = priv->rates_mask;
1891 active_rates &= ~IWL_RATE_60M_MASK;
1892
1893 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001894
Tomas Winklerc7c46672007-10-18 02:04:15 +02001895 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001896 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001897 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001898 active_rates &= ~ret_rates;
1899
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001900 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001901 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001902 active_rates &= ~ret_rates;
1903
Zhu Yib481de92007-09-25 17:54:57 -07001904 len += 2 + *pos;
1905 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001906 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001907 goto fill_end;
1908
1909 /* fill in supported extended rate */
1910 /* ...next IE... */
1911 left -= 2;
1912 if (left < 0)
1913 return 0;
1914 /* ... fill it in... */
1915 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1916 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001917 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001918 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001919 if (*pos > 0)
1920 len += 2 + *pos;
1921
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001922#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001923 if (is_direct && priv->is_ht_enabled) {
1924 u8 use_wide_chan = 1;
1925
1926 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
1927 use_wide_chan = 0;
1928 pos += (*pos) + 1;
1929 *pos++ = WLAN_EID_HT_CAPABILITY;
1930 *pos++ = sizeof(struct ieee80211_ht_capability);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001931 iwl4965_set_ht_capab(NULL, (struct ieee80211_ht_capability *)pos,
Zhu Yib481de92007-09-25 17:54:57 -07001932 use_wide_chan);
1933 len += 2 + sizeof(struct ieee80211_ht_capability);
1934 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001935#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001936
1937 fill_end:
1938 return (u16)len;
1939}
1940
1941/*
1942 * QoS support
1943*/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001944#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001945static int iwl4965_send_qos_params_command(struct iwl4965_priv *priv,
1946 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001947{
1948
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001949 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1950 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001951}
1952
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001953static void iwl4965_reset_qos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001954{
1955 u16 cw_min = 15;
1956 u16 cw_max = 1023;
1957 u8 aifs = 2;
1958 u8 is_legacy = 0;
1959 unsigned long flags;
1960 int i;
1961
1962 spin_lock_irqsave(&priv->lock, flags);
1963 priv->qos_data.qos_active = 0;
1964
1965 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1966 if (priv->qos_data.qos_enable)
1967 priv->qos_data.qos_active = 1;
1968 if (!(priv->active_rate & 0xfff0)) {
1969 cw_min = 31;
1970 is_legacy = 1;
1971 }
1972 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1973 if (priv->qos_data.qos_enable)
1974 priv->qos_data.qos_active = 1;
1975 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1976 cw_min = 31;
1977 is_legacy = 1;
1978 }
1979
1980 if (priv->qos_data.qos_active)
1981 aifs = 3;
1982
1983 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1984 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1985 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1986 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1987 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1988
1989 if (priv->qos_data.qos_active) {
1990 i = 1;
1991 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1992 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1993 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1994 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1995 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1996
1997 i = 2;
1998 priv->qos_data.def_qos_parm.ac[i].cw_min =
1999 cpu_to_le16((cw_min + 1) / 2 - 1);
2000 priv->qos_data.def_qos_parm.ac[i].cw_max =
2001 cpu_to_le16(cw_max);
2002 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2003 if (is_legacy)
2004 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2005 cpu_to_le16(6016);
2006 else
2007 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2008 cpu_to_le16(3008);
2009 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2010
2011 i = 3;
2012 priv->qos_data.def_qos_parm.ac[i].cw_min =
2013 cpu_to_le16((cw_min + 1) / 4 - 1);
2014 priv->qos_data.def_qos_parm.ac[i].cw_max =
2015 cpu_to_le16((cw_max + 1) / 2 - 1);
2016 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2017 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2018 if (is_legacy)
2019 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2020 cpu_to_le16(3264);
2021 else
2022 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2023 cpu_to_le16(1504);
2024 } else {
2025 for (i = 1; i < 4; i++) {
2026 priv->qos_data.def_qos_parm.ac[i].cw_min =
2027 cpu_to_le16(cw_min);
2028 priv->qos_data.def_qos_parm.ac[i].cw_max =
2029 cpu_to_le16(cw_max);
2030 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
2031 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2032 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2033 }
2034 }
2035 IWL_DEBUG_QOS("set QoS to default \n");
2036
2037 spin_unlock_irqrestore(&priv->lock, flags);
2038}
2039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002040static void iwl4965_activate_qos(struct iwl4965_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07002041{
2042 unsigned long flags;
2043
Zhu Yib481de92007-09-25 17:54:57 -07002044 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2045 return;
2046
2047 if (!priv->qos_data.qos_enable)
2048 return;
2049
2050 spin_lock_irqsave(&priv->lock, flags);
2051 priv->qos_data.def_qos_parm.qos_flags = 0;
2052
2053 if (priv->qos_data.qos_cap.q_AP.queue_request &&
2054 !priv->qos_data.qos_cap.q_AP.txop_request)
2055 priv->qos_data.def_qos_parm.qos_flags |=
2056 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002057 if (priv->qos_data.qos_active)
2058 priv->qos_data.def_qos_parm.qos_flags |=
2059 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2060
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002061#ifdef CONFIG_IWL4965_HT
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08002062 if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
2063 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002064#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08002065
Zhu Yib481de92007-09-25 17:54:57 -07002066 spin_unlock_irqrestore(&priv->lock, flags);
2067
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002068 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08002069 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
2070 priv->qos_data.qos_active,
2071 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002073 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002074 &(priv->qos_data.def_qos_parm));
2075 }
2076}
2077
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002078#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07002079/*
2080 * Power management (not Tx power!) functions
2081 */
2082#define MSEC_TO_USEC 1024
2083
2084#define NOSLP __constant_cpu_to_le16(0), 0, 0
2085#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
2086#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2087#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2088 __constant_cpu_to_le32(X1), \
2089 __constant_cpu_to_le32(X2), \
2090 __constant_cpu_to_le32(X3), \
2091 __constant_cpu_to_le32(X4)}
2092
2093
2094/* default power management (not Tx power) table values */
2095/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002096static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07002097 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2098 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2099 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2100 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2101 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2102 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2103};
2104
2105/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002106static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07002107 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2108 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2109 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2110 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2111 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2112 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2113 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2114 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2115 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2116 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2117};
2118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002119int iwl4965_power_init_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002120{
2121 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002122 struct iwl4965_power_mgr *pow_data;
2123 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07002124 u16 pci_pm;
2125
2126 IWL_DEBUG_POWER("Initialize power \n");
2127
2128 pow_data = &(priv->power_data);
2129
2130 memset(pow_data, 0, sizeof(*pow_data));
2131
2132 pow_data->active_index = IWL_POWER_RANGE_0;
2133 pow_data->dtim_val = 0xffff;
2134
2135 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2136 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2137
2138 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2139 if (rc != 0)
2140 return 0;
2141 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002142 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002143
2144 IWL_DEBUG_POWER("adjust power command flags\n");
2145
2146 for (i = 0; i < IWL_POWER_AC; i++) {
2147 cmd = &pow_data->pwr_range_0[i].cmd;
2148
2149 if (pci_pm & 0x1)
2150 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2151 else
2152 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2153 }
2154 }
2155 return rc;
2156}
2157
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002158static int iwl4965_update_power_cmd(struct iwl4965_priv *priv,
2159 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002160{
2161 int rc = 0, i;
2162 u8 skip;
2163 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002164 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07002165 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002166 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07002167
2168 if (mode > IWL_POWER_INDEX_5) {
2169 IWL_DEBUG_POWER("Error invalid power mode \n");
2170 return -1;
2171 }
2172 pow_data = &(priv->power_data);
2173
2174 if (pow_data->active_index == IWL_POWER_RANGE_0)
2175 range = &pow_data->pwr_range_0[0];
2176 else
2177 range = &pow_data->pwr_range_1[1];
2178
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002179 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07002180
2181#ifdef IWL_MAC80211_DISABLE
2182 if (priv->assoc_network != NULL) {
2183 unsigned long flags;
2184
2185 period = priv->assoc_network->tim.tim_period;
2186 }
2187#endif /*IWL_MAC80211_DISABLE */
2188 skip = range[mode].no_dtim;
2189
2190 if (period == 0) {
2191 period = 1;
2192 skip = 0;
2193 }
2194
2195 if (skip == 0) {
2196 max_sleep = period;
2197 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2198 } else {
2199 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2200 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2201 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2202 }
2203
2204 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2205 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2206 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2207 }
2208
2209 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2210 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2211 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2212 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2213 le32_to_cpu(cmd->sleep_interval[0]),
2214 le32_to_cpu(cmd->sleep_interval[1]),
2215 le32_to_cpu(cmd->sleep_interval[2]),
2216 le32_to_cpu(cmd->sleep_interval[3]),
2217 le32_to_cpu(cmd->sleep_interval[4]));
2218
2219 return rc;
2220}
2221
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002222static int iwl4965_send_power_mode(struct iwl4965_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002223{
John W. Linville9a62f732007-11-15 16:27:36 -05002224 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002225 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002226 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002227
2228 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08002229 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07002230 * else user level */
2231 switch (mode) {
2232 case IWL_POWER_BATTERY:
2233 final_mode = IWL_POWER_INDEX_3;
2234 break;
2235 case IWL_POWER_AC:
2236 final_mode = IWL_POWER_MODE_CAM;
2237 break;
2238 default:
2239 final_mode = mode;
2240 break;
2241 }
2242
2243 cmd.keep_alive_beacons = 0;
2244
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002245 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002247 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002248
2249 if (final_mode == IWL_POWER_MODE_CAM)
2250 clear_bit(STATUS_POWER_PMI, &priv->status);
2251 else
2252 set_bit(STATUS_POWER_PMI, &priv->status);
2253
2254 return rc;
2255}
2256
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002257int iwl4965_is_network_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002258{
2259 /* Filter incoming packets to determine if they are targeted toward
2260 * this network, discarding packets coming from ourselves */
2261 switch (priv->iw_mode) {
2262 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2263 /* packets from our adapter are dropped (echo) */
2264 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2265 return 0;
2266 /* {broad,multi}cast packets to our IBSS go through */
2267 if (is_multicast_ether_addr(header->addr1))
2268 return !compare_ether_addr(header->addr3, priv->bssid);
2269 /* packets to our adapter go through */
2270 return !compare_ether_addr(header->addr1, priv->mac_addr);
2271 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2272 /* packets from our adapter are dropped (echo) */
2273 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2274 return 0;
2275 /* {broad,multi}cast packets to our BSS go through */
2276 if (is_multicast_ether_addr(header->addr1))
2277 return !compare_ether_addr(header->addr2, priv->bssid);
2278 /* packets to our adapter go through */
2279 return !compare_ether_addr(header->addr1, priv->mac_addr);
2280 }
2281
2282 return 1;
2283}
2284
2285#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2286
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002287static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002288{
2289 switch (status & TX_STATUS_MSK) {
2290 case TX_STATUS_SUCCESS:
2291 return "SUCCESS";
2292 TX_STATUS_ENTRY(SHORT_LIMIT);
2293 TX_STATUS_ENTRY(LONG_LIMIT);
2294 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2295 TX_STATUS_ENTRY(MGMNT_ABORT);
2296 TX_STATUS_ENTRY(NEXT_FRAG);
2297 TX_STATUS_ENTRY(LIFE_EXPIRE);
2298 TX_STATUS_ENTRY(DEST_PS);
2299 TX_STATUS_ENTRY(ABORTED);
2300 TX_STATUS_ENTRY(BT_RETRY);
2301 TX_STATUS_ENTRY(STA_INVALID);
2302 TX_STATUS_ENTRY(FRAG_DROPPED);
2303 TX_STATUS_ENTRY(TID_DISABLE);
2304 TX_STATUS_ENTRY(FRAME_FLUSHED);
2305 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2306 TX_STATUS_ENTRY(TX_LOCKED);
2307 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2308 }
2309
2310 return "UNKNOWN";
2311}
2312
2313/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002314 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002315 *
2316 * NOTE: priv->mutex is not required before calling this function
2317 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002318static int iwl4965_scan_cancel(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002319{
2320 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2321 clear_bit(STATUS_SCANNING, &priv->status);
2322 return 0;
2323 }
2324
2325 if (test_bit(STATUS_SCANNING, &priv->status)) {
2326 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2327 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2328 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2329 queue_work(priv->workqueue, &priv->abort_scan);
2330
2331 } else
2332 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2333
2334 return test_bit(STATUS_SCANNING, &priv->status);
2335 }
2336
2337 return 0;
2338}
2339
2340/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002341 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002342 * @ms: amount of time to wait (in milliseconds) for scan to abort
2343 *
2344 * NOTE: priv->mutex must be held before calling this function
2345 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002346static int iwl4965_scan_cancel_timeout(struct iwl4965_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002347{
2348 unsigned long now = jiffies;
2349 int ret;
2350
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002351 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002352 if (ret && ms) {
2353 mutex_unlock(&priv->mutex);
2354 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2355 test_bit(STATUS_SCANNING, &priv->status))
2356 msleep(1);
2357 mutex_lock(&priv->mutex);
2358
2359 return test_bit(STATUS_SCANNING, &priv->status);
2360 }
2361
2362 return ret;
2363}
2364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002365static void iwl4965_sequence_reset(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002366{
2367 /* Reset ieee stats */
2368
2369 /* We don't reset the net_device_stats (ieee->stats) on
2370 * re-association */
2371
2372 priv->last_seq_num = -1;
2373 priv->last_frag_num = -1;
2374 priv->last_packet_time = 0;
2375
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002376 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002377}
2378
2379#define MAX_UCODE_BEACON_INTERVAL 4096
2380#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2381
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002382static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002383{
2384 u16 new_val = 0;
2385 u16 beacon_factor = 0;
2386
2387 beacon_factor =
2388 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2389 / MAX_UCODE_BEACON_INTERVAL;
2390 new_val = beacon_val / beacon_factor;
2391
2392 return cpu_to_le16(new_val);
2393}
2394
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002395static void iwl4965_setup_rxon_timing(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002396{
2397 u64 interval_tm_unit;
2398 u64 tsf, result;
2399 unsigned long flags;
2400 struct ieee80211_conf *conf = NULL;
2401 u16 beacon_int = 0;
2402
2403 conf = ieee80211_get_hw_conf(priv->hw);
2404
2405 spin_lock_irqsave(&priv->lock, flags);
2406 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2407 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2408
2409 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2410
2411 tsf = priv->timestamp1;
2412 tsf = ((tsf << 32) | priv->timestamp0);
2413
2414 beacon_int = priv->beacon_int;
2415 spin_unlock_irqrestore(&priv->lock, flags);
2416
2417 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2418 if (beacon_int == 0) {
2419 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2420 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2421 } else {
2422 priv->rxon_timing.beacon_interval =
2423 cpu_to_le16(beacon_int);
2424 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002425 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002426 le16_to_cpu(priv->rxon_timing.beacon_interval));
2427 }
2428
2429 priv->rxon_timing.atim_window = 0;
2430 } else {
2431 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002432 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002433 /* TODO: we need to get atim_window from upper stack
2434 * for now we set to 0 */
2435 priv->rxon_timing.atim_window = 0;
2436 }
2437
2438 interval_tm_unit =
2439 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2440 result = do_div(tsf, interval_tm_unit);
2441 priv->rxon_timing.beacon_init_val =
2442 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2443
2444 IWL_DEBUG_ASSOC
2445 ("beacon interval %d beacon timer %d beacon tim %d\n",
2446 le16_to_cpu(priv->rxon_timing.beacon_interval),
2447 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2448 le16_to_cpu(priv->rxon_timing.atim_window));
2449}
2450
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002451static int iwl4965_scan_initiate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002452{
2453 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2454 IWL_ERROR("APs don't scan.\n");
2455 return 0;
2456 }
2457
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002458 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002459 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2460 return -EIO;
2461 }
2462
2463 if (test_bit(STATUS_SCANNING, &priv->status)) {
2464 IWL_DEBUG_SCAN("Scan already in progress.\n");
2465 return -EAGAIN;
2466 }
2467
2468 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2469 IWL_DEBUG_SCAN("Scan request while abort pending. "
2470 "Queuing.\n");
2471 return -EAGAIN;
2472 }
2473
2474 IWL_DEBUG_INFO("Starting scan...\n");
2475 priv->scan_bands = 2;
2476 set_bit(STATUS_SCANNING, &priv->status);
2477 priv->scan_start = jiffies;
2478 priv->scan_pass_start = priv->scan_start;
2479
2480 queue_work(priv->workqueue, &priv->request_scan);
2481
2482 return 0;
2483}
2484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002485static int iwl4965_set_rxon_hwcrypto(struct iwl4965_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002486{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002487 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002488
2489 if (hw_decrypt)
2490 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2491 else
2492 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2493
2494 return 0;
2495}
2496
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002497static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode)
Zhu Yib481de92007-09-25 17:54:57 -07002498{
2499 if (phymode == MODE_IEEE80211A) {
2500 priv->staging_rxon.flags &=
2501 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2502 | RXON_FLG_CCK_MSK);
2503 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2504 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002505 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002506 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2507 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2508 else
2509 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2510
2511 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2512 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2513
2514 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2515 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2516 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2517 }
2518}
2519
2520/*
Ian Schram01ebd062007-10-25 17:15:22 +08002521 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002522 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002523static void iwl4965_connection_init_rx_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002524{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002525 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002526
2527 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2528
2529 switch (priv->iw_mode) {
2530 case IEEE80211_IF_TYPE_AP:
2531 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2532 break;
2533
2534 case IEEE80211_IF_TYPE_STA:
2535 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2536 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2537 break;
2538
2539 case IEEE80211_IF_TYPE_IBSS:
2540 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2541 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2542 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2543 RXON_FILTER_ACCEPT_GRP_MSK;
2544 break;
2545
2546 case IEEE80211_IF_TYPE_MNTR:
2547 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2548 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2549 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2550 break;
2551 }
2552
2553#if 0
2554 /* TODO: Figure out when short_preamble would be set and cache from
2555 * that */
2556 if (!hw_to_local(priv->hw)->short_preamble)
2557 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2558 else
2559 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2560#endif
2561
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002562 ch_info = iwl4965_get_channel_info(priv, priv->phymode,
Zhu Yib481de92007-09-25 17:54:57 -07002563 le16_to_cpu(priv->staging_rxon.channel));
2564
2565 if (!ch_info)
2566 ch_info = &priv->channel_info[0];
2567
2568 /*
2569 * in some case A channels are all non IBSS
2570 * in this case force B/G channel
2571 */
2572 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2573 !(is_channel_ibss(ch_info)))
2574 ch_info = &priv->channel_info[0];
2575
2576 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2577 if (is_channel_a_band(ch_info))
2578 priv->phymode = MODE_IEEE80211A;
2579 else
2580 priv->phymode = MODE_IEEE80211G;
2581
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002582 iwl4965_set_flags_for_phymode(priv, priv->phymode);
Zhu Yib481de92007-09-25 17:54:57 -07002583
2584 priv->staging_rxon.ofdm_basic_rates =
2585 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2586 priv->staging_rxon.cck_basic_rates =
2587 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2588
2589 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2590 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2591 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2592 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2593 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2594 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2595 iwl4965_set_rxon_chain(priv);
2596}
2597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002598static int iwl4965_set_mode(struct iwl4965_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002599{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002600 if (!iwl4965_is_ready_rf(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002601 return -EAGAIN;
2602
2603 if (mode == IEEE80211_IF_TYPE_IBSS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002604 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002605
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002606 ch_info = iwl4965_get_channel_info(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002607 priv->phymode,
2608 le16_to_cpu(priv->staging_rxon.channel));
2609
2610 if (!ch_info || !is_channel_ibss(ch_info)) {
2611 IWL_ERROR("channel %d not IBSS channel\n",
2612 le16_to_cpu(priv->staging_rxon.channel));
2613 return -EINVAL;
2614 }
2615 }
2616
2617 cancel_delayed_work(&priv->scan_check);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002618 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07002619 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2620 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2621 return -EAGAIN;
2622 }
2623
2624 priv->iw_mode = mode;
2625
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002626 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002627 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002629 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002630
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002631 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002632
2633 return 0;
2634}
2635
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002636static void iwl4965_build_tx_cmd_hwcrypto(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002637 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002638 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002639 struct sk_buff *skb_frag,
2640 int last_frag)
2641{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002642 struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002643
2644 switch (keyinfo->alg) {
2645 case ALG_CCMP:
2646 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2647 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2648 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2649 break;
2650
2651 case ALG_TKIP:
2652#if 0
2653 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2654
2655 if (last_frag)
2656 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2657 8);
2658 else
2659 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2660#endif
2661 break;
2662
2663 case ALG_WEP:
2664 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2665 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2666
2667 if (keyinfo->keylen == 13)
2668 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2669
2670 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2671
2672 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2673 "with key %d\n", ctl->key_idx);
2674 break;
2675
Zhu Yib481de92007-09-25 17:54:57 -07002676 default:
2677 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2678 break;
2679 }
2680}
2681
2682/*
2683 * handle build REPLY_TX command notification.
2684 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002685static void iwl4965_build_tx_cmd_basic(struct iwl4965_priv *priv,
2686 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002687 struct ieee80211_tx_control *ctrl,
2688 struct ieee80211_hdr *hdr,
2689 int is_unicast, u8 std_id)
2690{
2691 __le16 *qc;
2692 u16 fc = le16_to_cpu(hdr->frame_control);
2693 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2694
2695 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2696 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2697 tx_flags |= TX_CMD_FLG_ACK_MSK;
2698 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2699 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2700 if (ieee80211_is_probe_response(fc) &&
2701 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2702 tx_flags |= TX_CMD_FLG_TSF_MSK;
2703 } else {
2704 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2705 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2706 }
2707
2708 cmd->cmd.tx.sta_id = std_id;
2709 if (ieee80211_get_morefrag(hdr))
2710 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2711
2712 qc = ieee80211_get_qos_ctrl(hdr);
2713 if (qc) {
2714 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2715 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2716 } else
2717 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2718
2719 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2720 tx_flags |= TX_CMD_FLG_RTS_MSK;
2721 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2722 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2723 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2724 tx_flags |= TX_CMD_FLG_CTS_MSK;
2725 }
2726
2727 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2728 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2729
2730 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2731 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2732 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2733 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002734 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002735 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002736 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002737 } else
2738 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2739
2740 cmd->cmd.tx.driver_txop = 0;
2741 cmd->cmd.tx.tx_flags = tx_flags;
2742 cmd->cmd.tx.next_frame_len = 0;
2743}
2744
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002745static int iwl4965_get_sta_id(struct iwl4965_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002746{
2747 int sta_id;
2748 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002749 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002750
2751 /* If this frame is broadcast or not data then use the broadcast
2752 * station id */
2753 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2754 is_multicast_ether_addr(hdr->addr1))
2755 return priv->hw_setting.bcast_sta_id;
2756
2757 switch (priv->iw_mode) {
2758
2759 /* If this frame is part of a BSS network (we're a station), then
2760 * we use the AP's station id */
2761 case IEEE80211_IF_TYPE_STA:
2762 return IWL_AP_ID;
2763
2764 /* If we are an AP, then find the station, or use BCAST */
2765 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002766 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002767 if (sta_id != IWL_INVALID_STATION)
2768 return sta_id;
2769 return priv->hw_setting.bcast_sta_id;
2770
2771 /* If this frame is part of a IBSS network, then we use the
2772 * target specific station id */
2773 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002774 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002775 if (sta_id != IWL_INVALID_STATION)
2776 return sta_id;
2777
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002778 sta_id = iwl4965_add_station_flags(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002779
2780 if (sta_id != IWL_INVALID_STATION)
2781 return sta_id;
2782
Joe Perches0795af52007-10-03 17:59:30 -07002783 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002784 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002785 print_mac(mac, hdr->addr1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002786 iwl4965_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002787 return priv->hw_setting.bcast_sta_id;
2788
2789 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002790 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002791 return priv->hw_setting.bcast_sta_id;
2792 }
2793}
2794
2795/*
2796 * start REPLY_TX command process
2797 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002798static int iwl4965_tx_skb(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002799 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2800{
2801 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002802 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002803 u32 *control_flags;
2804 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002805 struct iwl4965_tx_queue *txq = NULL;
2806 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002807 dma_addr_t phys_addr;
2808 dma_addr_t txcmd_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002809 struct iwl4965_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002810 u16 len, idx, len_org;
2811 u8 id, hdr_len, unicast;
2812 u8 sta_id;
2813 u16 seq_number = 0;
2814 u16 fc;
2815 __le16 *qc;
2816 u8 wait_write_ptr = 0;
2817 unsigned long flags;
2818 int rc;
2819
2820 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002821 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002822 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2823 goto drop_unlock;
2824 }
2825
2826 if (!priv->interface_id) {
2827 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2828 goto drop_unlock;
2829 }
2830
2831 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2832 IWL_ERROR("ERROR: No TX rate available.\n");
2833 goto drop_unlock;
2834 }
2835
2836 unicast = !is_multicast_ether_addr(hdr->addr1);
2837 id = 0;
2838
2839 fc = le16_to_cpu(hdr->frame_control);
2840
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002841#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002842 if (ieee80211_is_auth(fc))
2843 IWL_DEBUG_TX("Sending AUTH frame\n");
2844 else if (ieee80211_is_assoc_request(fc))
2845 IWL_DEBUG_TX("Sending ASSOC frame\n");
2846 else if (ieee80211_is_reassoc_request(fc))
2847 IWL_DEBUG_TX("Sending REASSOC frame\n");
2848#endif
2849
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002850 if (!iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07002851 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002852 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002853 goto drop_unlock;
2854 }
2855
2856 spin_unlock_irqrestore(&priv->lock, flags);
2857
2858 hdr_len = ieee80211_get_hdrlen(fc);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002859 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002860 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002861 DECLARE_MAC_BUF(mac);
2862
2863 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2864 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002865 goto drop;
2866 }
2867
2868 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2869
2870 qc = ieee80211_get_qos_ctrl(hdr);
2871 if (qc) {
2872 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2873 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2874 IEEE80211_SCTL_SEQ;
2875 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2876 (hdr->seq_ctrl &
2877 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2878 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002879#ifdef CONFIG_IWL4965_HT
2880#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07002881 /* aggregation is on for this <sta,tid> */
2882 if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
2883 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002884#endif /* CONFIG_IWL4965_HT_AGG */
2885#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002886 }
2887 txq = &priv->txq[txq_id];
2888 q = &txq->q;
2889
2890 spin_lock_irqsave(&priv->lock, flags);
2891
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002892 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002893 memset(tfd, 0, sizeof(*tfd));
2894 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002895 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002896
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002897 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002898 txq->txb[q->write_ptr].skb[0] = skb;
2899 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002900 ctl, sizeof(struct ieee80211_tx_control));
2901 out_cmd = &txq->cmd[idx];
2902 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2903 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2904 out_cmd->hdr.cmd = REPLY_TX;
2905 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002906 INDEX_TO_SEQ(q->write_ptr)));
Zhu Yib481de92007-09-25 17:54:57 -07002907 /* copy frags header */
2908 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2909
2910 /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
2911 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002912 sizeof(struct iwl4965_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002913
2914 len_org = len;
2915 len = (len + 3) & ~3;
2916
2917 if (len_org != len)
2918 len_org = 1;
2919 else
2920 len_org = 0;
2921
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002922 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
2923 offsetof(struct iwl4965_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002924
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002925 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002926
2927 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002928 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002929
2930 /* 802.11 null functions have no payload... */
2931 len = skb->len - hdr_len;
2932 if (len) {
2933 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2934 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002935 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002936 }
2937
2938 if (len_org)
2939 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2940
2941 len = (u16)skb->len;
2942 out_cmd->cmd.tx.len = cpu_to_le16(len);
2943
2944 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002945 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002946
2947 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002948 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002949
2950 iwl4965_tx_cmd(priv, out_cmd, sta_id, txcmd_phys,
2951 hdr, hdr_len, ctl, NULL);
2952
2953 if (!ieee80211_get_morefrag(hdr)) {
2954 txq->need_update = 1;
2955 if (qc) {
2956 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2957 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2958 }
2959 } else {
2960 wait_write_ptr = 1;
2961 txq->need_update = 0;
2962 }
2963
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002964 iwl4965_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002965 sizeof(out_cmd->cmd.tx));
2966
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002967 iwl4965_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002968 ieee80211_get_hdrlen(fc));
2969
2970 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2971
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002972 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
2973 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002974 spin_unlock_irqrestore(&priv->lock, flags);
2975
2976 if (rc)
2977 return rc;
2978
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002979 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002980 && priv->mac80211_registered) {
2981 if (wait_write_ptr) {
2982 spin_lock_irqsave(&priv->lock, flags);
2983 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002984 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002985 spin_unlock_irqrestore(&priv->lock, flags);
2986 }
2987
2988 ieee80211_stop_queue(priv->hw, ctl->queue);
2989 }
2990
2991 return 0;
2992
2993drop_unlock:
2994 spin_unlock_irqrestore(&priv->lock, flags);
2995drop:
2996 return -1;
2997}
2998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002999static void iwl4965_set_rate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003000{
3001 const struct ieee80211_hw_mode *hw = NULL;
3002 struct ieee80211_rate *rate;
3003 int i;
3004
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003005 hw = iwl4965_get_hw_mode(priv, priv->phymode);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08003006 if (!hw) {
3007 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
3008 return;
3009 }
Zhu Yib481de92007-09-25 17:54:57 -07003010
3011 priv->active_rate = 0;
3012 priv->active_rate_basic = 0;
3013
3014 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
3015 hw->mode == MODE_IEEE80211A ?
3016 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
3017
3018 for (i = 0; i < hw->num_rates; i++) {
3019 rate = &(hw->rates[i]);
3020 if ((rate->val < IWL_RATE_COUNT) &&
3021 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
3022 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003023 rate->val, iwl4965_rates[rate->val].plcp,
Zhu Yib481de92007-09-25 17:54:57 -07003024 (rate->flags & IEEE80211_RATE_BASIC) ?
3025 "*" : "");
3026 priv->active_rate |= (1 << rate->val);
3027 if (rate->flags & IEEE80211_RATE_BASIC)
3028 priv->active_rate_basic |= (1 << rate->val);
3029 } else
3030 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003031 rate->val, iwl4965_rates[rate->val].plcp);
Zhu Yib481de92007-09-25 17:54:57 -07003032 }
3033
3034 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
3035 priv->active_rate, priv->active_rate_basic);
3036
3037 /*
3038 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
3039 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
3040 * OFDM
3041 */
3042 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
3043 priv->staging_rxon.cck_basic_rates =
3044 ((priv->active_rate_basic &
3045 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
3046 else
3047 priv->staging_rxon.cck_basic_rates =
3048 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
3049
3050 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
3051 priv->staging_rxon.ofdm_basic_rates =
3052 ((priv->active_rate_basic &
3053 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
3054 IWL_FIRST_OFDM_RATE) & 0xFF;
3055 else
3056 priv->staging_rxon.ofdm_basic_rates =
3057 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
3058}
3059
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003060static void iwl4965_radio_kill_sw(struct iwl4965_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07003061{
3062 unsigned long flags;
3063
3064 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
3065 return;
3066
3067 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
3068 disable_radio ? "OFF" : "ON");
3069
3070 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003071 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003072 /* FIXME: This is a workaround for AP */
3073 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3074 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003075 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003076 CSR_UCODE_SW_BIT_RFKILL);
3077 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003078 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003079 set_bit(STATUS_RF_KILL_SW, &priv->status);
3080 }
3081 return;
3082 }
3083
3084 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003085 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07003086
3087 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3088 spin_unlock_irqrestore(&priv->lock, flags);
3089
3090 /* wake up ucode */
3091 msleep(10);
3092
3093 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003094 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3095 if (!iwl4965_grab_nic_access(priv))
3096 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003097 spin_unlock_irqrestore(&priv->lock, flags);
3098
3099 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3100 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3101 "disabled by HW switch\n");
3102 return;
3103 }
3104
3105 queue_work(priv->workqueue, &priv->restart);
3106 return;
3107}
3108
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003109void iwl4965_set_decrypted_flag(struct iwl4965_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07003110 u32 decrypt_res, struct ieee80211_rx_status *stats)
3111{
3112 u16 fc =
3113 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3114
3115 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3116 return;
3117
3118 if (!(fc & IEEE80211_FCTL_PROTECTED))
3119 return;
3120
3121 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3122 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3123 case RX_RES_STATUS_SEC_TYPE_TKIP:
3124 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3125 RX_RES_STATUS_BAD_ICV_MIC)
3126 stats->flag |= RX_FLAG_MMIC_ERROR;
3127 case RX_RES_STATUS_SEC_TYPE_WEP:
3128 case RX_RES_STATUS_SEC_TYPE_CCMP:
3129 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3130 RX_RES_STATUS_DECRYPT_OK) {
3131 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3132 stats->flag |= RX_FLAG_DECRYPTED;
3133 }
3134 break;
3135
3136 default:
3137 break;
3138 }
3139}
3140
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003141void iwl4965_handle_data_packet_monitor(struct iwl4965_priv *priv,
3142 struct iwl4965_rx_mem_buffer *rxb,
Zhu Yib481de92007-09-25 17:54:57 -07003143 void *data, short len,
3144 struct ieee80211_rx_status *stats,
3145 u16 phy_flags)
3146{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003147 struct iwl4965_rt_rx_hdr *iwl4965_rt;
Zhu Yib481de92007-09-25 17:54:57 -07003148
3149 /* First cache any information we need before we overwrite
3150 * the information provided in the skb from the hardware */
3151 s8 signal = stats->ssi;
3152 s8 noise = 0;
3153 int rate = stats->rate;
3154 u64 tsf = stats->mactime;
3155 __le16 phy_flags_hw = cpu_to_le16(phy_flags);
3156
3157 /* We received data from the HW, so stop the watchdog */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003158 if (len > IWL_RX_BUF_SIZE - sizeof(*iwl4965_rt)) {
Zhu Yib481de92007-09-25 17:54:57 -07003159 IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
3160 return;
3161 }
3162
3163 /* copy the frame data to write after where the radiotap header goes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003164 iwl4965_rt = (void *)rxb->skb->data;
3165 memmove(iwl4965_rt->payload, data, len);
Zhu Yib481de92007-09-25 17:54:57 -07003166
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003167 iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
3168 iwl4965_rt->rt_hdr.it_pad = 0; /* always good to zero */
Zhu Yib481de92007-09-25 17:54:57 -07003169
3170 /* total header + data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003171 iwl4965_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl4965_rt));
Zhu Yib481de92007-09-25 17:54:57 -07003172
3173 /* Set the size of the skb to the size of the frame */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003174 skb_put(rxb->skb, sizeof(*iwl4965_rt) + len);
Zhu Yib481de92007-09-25 17:54:57 -07003175
3176 /* Big bitfield of all the fields we provide in radiotap */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003177 iwl4965_rt->rt_hdr.it_present =
Zhu Yib481de92007-09-25 17:54:57 -07003178 cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
3179 (1 << IEEE80211_RADIOTAP_FLAGS) |
3180 (1 << IEEE80211_RADIOTAP_RATE) |
3181 (1 << IEEE80211_RADIOTAP_CHANNEL) |
3182 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
3183 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
3184 (1 << IEEE80211_RADIOTAP_ANTENNA));
3185
3186 /* Zero the flags, we'll add to them as we go */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003187 iwl4965_rt->rt_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003188
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003189 iwl4965_rt->rt_tsf = cpu_to_le64(tsf);
Zhu Yib481de92007-09-25 17:54:57 -07003190
3191 /* Convert to dBm */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003192 iwl4965_rt->rt_dbmsignal = signal;
3193 iwl4965_rt->rt_dbmnoise = noise;
Zhu Yib481de92007-09-25 17:54:57 -07003194
3195 /* Convert the channel frequency and set the flags */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003196 iwl4965_rt->rt_channelMHz = cpu_to_le16(stats->freq);
Zhu Yib481de92007-09-25 17:54:57 -07003197 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003198 iwl4965_rt->rt_chbitmask =
Zhu Yib481de92007-09-25 17:54:57 -07003199 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
3200 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003201 iwl4965_rt->rt_chbitmask =
Zhu Yib481de92007-09-25 17:54:57 -07003202 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
3203 else /* 802.11g */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003204 iwl4965_rt->rt_chbitmask =
Zhu Yib481de92007-09-25 17:54:57 -07003205 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
3206
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003207 rate = iwl4965_rate_index_from_plcp(rate);
Zhu Yib481de92007-09-25 17:54:57 -07003208 if (rate == -1)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003209 iwl4965_rt->rt_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003210 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003211 iwl4965_rt->rt_rate = iwl4965_rates[rate].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07003212
3213 /* antenna number */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003214 iwl4965_rt->rt_antenna =
Zhu Yib481de92007-09-25 17:54:57 -07003215 le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
3216
3217 /* set the preamble flag if we have it */
3218 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003219 iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
Zhu Yib481de92007-09-25 17:54:57 -07003220
3221 IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
3222
3223 stats->flag |= RX_FLAG_RADIOTAP;
3224 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3225 rxb->skb = NULL;
3226}
3227
3228
3229#define IWL_PACKET_RETRY_TIME HZ
3230
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003231int iwl4965_is_duplicate_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07003232{
3233 u16 sc = le16_to_cpu(header->seq_ctrl);
3234 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3235 u16 frag = sc & IEEE80211_SCTL_FRAG;
3236 u16 *last_seq, *last_frag;
3237 unsigned long *last_time;
3238
3239 switch (priv->iw_mode) {
3240 case IEEE80211_IF_TYPE_IBSS:{
3241 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003242 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003243 u8 *mac = header->addr2;
3244 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3245
3246 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003247 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07003248 if (!compare_ether_addr(entry->mac, mac))
3249 break;
3250 }
3251 if (p == &priv->ibss_mac_hash[index]) {
3252 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3253 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08003254 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07003255 return 0;
3256 }
3257 memcpy(entry->mac, mac, ETH_ALEN);
3258 entry->seq_num = seq;
3259 entry->frag_num = frag;
3260 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08003261 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07003262 return 0;
3263 }
3264 last_seq = &entry->seq_num;
3265 last_frag = &entry->frag_num;
3266 last_time = &entry->packet_time;
3267 break;
3268 }
3269 case IEEE80211_IF_TYPE_STA:
3270 last_seq = &priv->last_seq_num;
3271 last_frag = &priv->last_frag_num;
3272 last_time = &priv->last_packet_time;
3273 break;
3274 default:
3275 return 0;
3276 }
3277 if ((*last_seq == seq) &&
3278 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3279 if (*last_frag == frag)
3280 goto drop;
3281 if (*last_frag + 1 != frag)
3282 /* out-of-order fragment */
3283 goto drop;
3284 } else
3285 *last_seq = seq;
3286
3287 *last_frag = frag;
3288 *last_time = jiffies;
3289 return 0;
3290
3291 drop:
3292 return 1;
3293}
3294
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003295#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003296
3297#include "iwl-spectrum.h"
3298
3299#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3300#define BEACON_TIME_MASK_HIGH 0xFF000000
3301#define TIME_UNIT 1024
3302
3303/*
3304 * extended beacon time format
3305 * time in usec will be changed into a 32-bit value in 8:24 format
3306 * the high 1 byte is the beacon counts
3307 * the lower 3 bytes is the time in usec within one beacon interval
3308 */
3309
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003310static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003311{
3312 u32 quot;
3313 u32 rem;
3314 u32 interval = beacon_interval * 1024;
3315
3316 if (!interval || !usec)
3317 return 0;
3318
3319 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3320 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3321
3322 return (quot << 24) + rem;
3323}
3324
3325/* base is usually what we get from ucode with each received frame,
3326 * the same as HW timer counter counting down
3327 */
3328
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003329static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003330{
3331 u32 base_low = base & BEACON_TIME_MASK_LOW;
3332 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3333 u32 interval = beacon_interval * TIME_UNIT;
3334 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3335 (addon & BEACON_TIME_MASK_HIGH);
3336
3337 if (base_low > addon_low)
3338 res += base_low - addon_low;
3339 else if (base_low < addon_low) {
3340 res += interval + base_low - addon_low;
3341 res += (1 << 24);
3342 } else
3343 res += (1 << 24);
3344
3345 return cpu_to_le32(res);
3346}
3347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003348static int iwl4965_get_measurement(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003349 struct ieee80211_measurement_params *params,
3350 u8 type)
3351{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003352 struct iwl4965_spectrum_cmd spectrum;
3353 struct iwl4965_rx_packet *res;
3354 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003355 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3356 .data = (void *)&spectrum,
3357 .meta.flags = CMD_WANT_SKB,
3358 };
3359 u32 add_time = le64_to_cpu(params->start_time);
3360 int rc;
3361 int spectrum_resp_status;
3362 int duration = le16_to_cpu(params->duration);
3363
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003364 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003365 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003366 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003367 le64_to_cpu(params->start_time) - priv->last_tsf,
3368 le16_to_cpu(priv->rxon_timing.beacon_interval));
3369
3370 memset(&spectrum, 0, sizeof(spectrum));
3371
3372 spectrum.channel_count = cpu_to_le16(1);
3373 spectrum.flags =
3374 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3375 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3376 cmd.len = sizeof(spectrum);
3377 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3378
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003379 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003380 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003381 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003382 add_time,
3383 le16_to_cpu(priv->rxon_timing.beacon_interval));
3384 else
3385 spectrum.start_time = 0;
3386
3387 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3388 spectrum.channels[0].channel = params->channel;
3389 spectrum.channels[0].type = type;
3390 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3391 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3392 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3393
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003394 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003395 if (rc)
3396 return rc;
3397
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003398 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003399 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3400 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3401 rc = -EIO;
3402 }
3403
3404 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3405 switch (spectrum_resp_status) {
3406 case 0: /* Command will be handled */
3407 if (res->u.spectrum.id != 0xff) {
3408 IWL_DEBUG_INFO
3409 ("Replaced existing measurement: %d\n",
3410 res->u.spectrum.id);
3411 priv->measurement_status &= ~MEASUREMENT_READY;
3412 }
3413 priv->measurement_status |= MEASUREMENT_ACTIVE;
3414 rc = 0;
3415 break;
3416
3417 case 1: /* Command will not be handled */
3418 rc = -EAGAIN;
3419 break;
3420 }
3421
3422 dev_kfree_skb_any(cmd.meta.u.skb);
3423
3424 return rc;
3425}
3426#endif
3427
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003428static void iwl4965_txstatus_to_ieee(struct iwl4965_priv *priv,
3429 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003430{
3431
3432 tx_sta->status.ack_signal = 0;
3433 tx_sta->status.excessive_retries = 0;
3434 tx_sta->status.queue_length = 0;
3435 tx_sta->status.queue_number = 0;
3436
3437 if (in_interrupt())
3438 ieee80211_tx_status_irqsafe(priv->hw,
3439 tx_sta->skb[0], &(tx_sta->status));
3440 else
3441 ieee80211_tx_status(priv->hw,
3442 tx_sta->skb[0], &(tx_sta->status));
3443
3444 tx_sta->skb[0] = NULL;
3445}
3446
3447/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003448 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries no more used by NIC.
Zhu Yib481de92007-09-25 17:54:57 -07003449 *
3450 * When FW advances 'R' index, all entries between old and
3451 * new 'R' index need to be reclaimed. As result, some free space
3452 * forms. If there is enough free space (> low mark), wake Tx queue.
3453 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003454int iwl4965_tx_queue_reclaim(struct iwl4965_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003455{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003456 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3457 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003458 int nfreed = 0;
3459
3460 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3461 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3462 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003463 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003464 return 0;
3465 }
3466
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003467 for (index = iwl4965_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003468 q->read_ptr != index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003469 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003470 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003471 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003472 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003473 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003474 } else if (nfreed > 1) {
3475 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003476 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003477 queue_work(priv->workqueue, &priv->restart);
3478 }
3479 nfreed++;
3480 }
3481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003482 if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003483 (txq_id != IWL_CMD_QUEUE_NUM) &&
3484 priv->mac80211_registered)
3485 ieee80211_wake_queue(priv->hw, txq_id);
3486
3487
3488 return nfreed;
3489}
3490
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003491static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003492{
3493 status &= TX_STATUS_MSK;
3494 return (status == TX_STATUS_SUCCESS)
3495 || (status == TX_STATUS_DIRECT_DONE);
3496}
3497
3498/******************************************************************************
3499 *
3500 * Generic RX handler implementations
3501 *
3502 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003503#ifdef CONFIG_IWL4965_HT
3504#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003505
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003506static inline int iwl4965_get_ra_sta_id(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003507 struct ieee80211_hdr *hdr)
3508{
3509 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3510 return IWL_AP_ID;
3511 else {
3512 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003513 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07003514 }
3515}
3516
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003517static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
3518 struct iwl4965_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07003519{
3520 if (priv->txq[txq_id].txb[idx].skb[0])
3521 return (struct ieee80211_hdr *)priv->txq[txq_id].
3522 txb[idx].skb[0]->data;
3523 return NULL;
3524}
3525
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003526static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003527{
3528 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3529 tx_resp->frame_count);
3530 return le32_to_cpu(*scd_ssn) & MAX_SN;
3531
3532}
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003533static int iwl4965_tx_status_reply_tx(struct iwl4965_priv *priv,
3534 struct iwl4965_ht_agg *agg,
3535 struct iwl4965_tx_resp *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003536 u16 start_idx)
3537{
3538 u32 status;
3539 __le32 *frame_status = &tx_resp->status;
3540 struct ieee80211_tx_status *tx_status = NULL;
3541 struct ieee80211_hdr *hdr = NULL;
3542 int i, sh;
3543 int txq_id, idx;
3544 u16 seq;
3545
3546 if (agg->wait_for_ba)
3547 IWL_DEBUG_TX_REPLY("got tx repsons w/o back\n");
3548
3549 agg->frame_count = tx_resp->frame_count;
3550 agg->start_idx = start_idx;
3551 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3552 agg->bitmap0 = agg->bitmap1 = 0;
3553
3554 if (agg->frame_count == 1) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003555 struct iwl4965_tx_queue *txq ;
Zhu Yib481de92007-09-25 17:54:57 -07003556 status = le32_to_cpu(frame_status[0]);
3557
3558 txq_id = agg->txq_id;
3559 txq = &priv->txq[txq_id];
3560 /* FIXME: code repetition */
3561 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d \n",
3562 agg->frame_count, agg->start_idx);
3563
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003564 tx_status = &(priv->txq[txq_id].txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003565 tx_status->retry_count = tx_resp->failure_frame;
3566 tx_status->queue_number = status & 0xff;
3567 tx_status->queue_length = tx_resp->bt_kill_count;
3568 tx_status->queue_length |= tx_resp->failure_rts;
3569
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003570 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003571 IEEE80211_TX_STATUS_ACK : 0;
3572 tx_status->control.tx_rate =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003573 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003574 /* FIXME: code repetition end */
3575
3576 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3577 status & 0xff, tx_resp->failure_frame);
3578 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003579 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003580
3581 agg->wait_for_ba = 0;
3582 } else {
3583 u64 bitmap = 0;
3584 int start = agg->start_idx;
3585
3586 for (i = 0; i < agg->frame_count; i++) {
3587 u16 sc;
3588 status = le32_to_cpu(frame_status[i]);
3589 seq = status >> 16;
3590 idx = SEQ_TO_INDEX(seq);
3591 txq_id = SEQ_TO_QUEUE(seq);
3592
3593 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3594 AGG_TX_STATE_ABORT_MSK))
3595 continue;
3596
3597 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3598 agg->frame_count, txq_id, idx);
3599
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003600 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003601
3602 sc = le16_to_cpu(hdr->seq_ctrl);
3603 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3604 IWL_ERROR("BUG_ON idx doesn't match seq control"
3605 " idx=%d, seq_idx=%d, seq=%d\n",
3606 idx, SEQ_TO_SN(sc),
3607 hdr->seq_ctrl);
3608 return -1;
3609 }
3610
3611 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3612 i, idx, SEQ_TO_SN(sc));
3613
3614 sh = idx - start;
3615 if (sh > 64) {
3616 sh = (start - idx) + 0xff;
3617 bitmap = bitmap << sh;
3618 sh = 0;
3619 start = idx;
3620 } else if (sh < -64)
3621 sh = 0xff - (start - idx);
3622 else if (sh < 0) {
3623 sh = start - idx;
3624 start = idx;
3625 bitmap = bitmap << sh;
3626 sh = 0;
3627 }
3628 bitmap |= (1 << sh);
3629 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3630 start, (u32)(bitmap & 0xFFFFFFFF));
3631 }
3632
3633 agg->bitmap0 = bitmap & 0xFFFFFFFF;
3634 agg->bitmap1 = bitmap >> 32;
3635 agg->start_idx = start;
3636 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3637 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%x\n",
3638 agg->frame_count, agg->start_idx,
3639 agg->bitmap0);
3640
3641 if (bitmap)
3642 agg->wait_for_ba = 1;
3643 }
3644 return 0;
3645}
3646#endif
3647#endif
3648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003649static void iwl4965_rx_reply_tx(struct iwl4965_priv *priv,
3650 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003651{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003652 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003653 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3654 int txq_id = SEQ_TO_QUEUE(sequence);
3655 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003656 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003657 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003658 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003659 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003660#ifdef CONFIG_IWL4965_HT
3661#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003662 int tid, sta_id;
3663#endif
3664#endif
3665
3666 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3667 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3668 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003669 index, txq->q.n_bd, txq->q.write_ptr,
3670 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003671 return;
3672 }
3673
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003674#ifdef CONFIG_IWL4965_HT
3675#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003676 if (txq->sched_retry) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003677 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
Zhu Yib481de92007-09-25 17:54:57 -07003678 struct ieee80211_hdr *hdr =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003679 iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3680 struct iwl4965_ht_agg *agg = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003681 __le16 *qc = ieee80211_get_qos_ctrl(hdr);
3682
3683 if (qc == NULL) {
3684 IWL_ERROR("BUG_ON qc is null!!!!\n");
3685 return;
3686 }
3687
3688 tid = le16_to_cpu(*qc) & 0xf;
3689
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003690 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003691 if (unlikely(sta_id == IWL_INVALID_STATION)) {
3692 IWL_ERROR("Station not known for\n");
3693 return;
3694 }
3695
3696 agg = &priv->stations[sta_id].tid[tid].agg;
3697
3698 iwl4965_tx_status_reply_tx(priv, agg, tx_resp, index);
3699
3700 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003701 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003702 /* TODO: send BAR */
3703 }
3704
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003705 if ((txq->q.read_ptr != (scd_ssn & 0xff))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003706 index = iwl4965_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003707 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3708 "%d index %d\n", scd_ssn , index);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003709 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003710 }
3711 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003712#endif /* CONFIG_IWL4965_HT_AGG */
3713#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003714 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003715
3716 tx_status->retry_count = tx_resp->failure_frame;
3717 tx_status->queue_number = status;
3718 tx_status->queue_length = tx_resp->bt_kill_count;
3719 tx_status->queue_length |= tx_resp->failure_rts;
3720
3721 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003722 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Zhu Yib481de92007-09-25 17:54:57 -07003723
3724 tx_status->control.tx_rate =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003725 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003726
3727 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003728 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003729 status, le32_to_cpu(tx_resp->rate_n_flags),
3730 tx_resp->failure_frame);
3731
3732 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3733 if (index != -1)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003734 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003735#ifdef CONFIG_IWL4965_HT
3736#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003737 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003738#endif /* CONFIG_IWL4965_HT_AGG */
3739#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003740
3741 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3742 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3743}
3744
3745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003746static void iwl4965_rx_reply_alive(struct iwl4965_priv *priv,
3747 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003748{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003749 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3750 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003751 struct delayed_work *pwork;
3752
3753 palive = &pkt->u.alive_frame;
3754
3755 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3756 "0x%01X 0x%01X\n",
3757 palive->is_valid, palive->ver_type,
3758 palive->ver_subtype);
3759
3760 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3761 IWL_DEBUG_INFO("Initialization Alive received.\n");
3762 memcpy(&priv->card_alive_init,
3763 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003764 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003765 pwork = &priv->init_alive_start;
3766 } else {
3767 IWL_DEBUG_INFO("Runtime Alive received.\n");
3768 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003769 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003770 pwork = &priv->alive_start;
3771 }
3772
3773 /* We delay the ALIVE response by 5ms to
3774 * give the HW RF Kill time to activate... */
3775 if (palive->is_valid == UCODE_VALID_OK)
3776 queue_delayed_work(priv->workqueue, pwork,
3777 msecs_to_jiffies(5));
3778 else
3779 IWL_WARNING("uCode did not respond OK.\n");
3780}
3781
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003782static void iwl4965_rx_reply_add_sta(struct iwl4965_priv *priv,
3783 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003784{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003785 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003786
3787 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3788 return;
3789}
3790
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003791static void iwl4965_rx_reply_error(struct iwl4965_priv *priv,
3792 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003793{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003794 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003795
3796 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3797 "seq 0x%04X ser 0x%08X\n",
3798 le32_to_cpu(pkt->u.err_resp.error_type),
3799 get_cmd_string(pkt->u.err_resp.cmd_id),
3800 pkt->u.err_resp.cmd_id,
3801 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3802 le32_to_cpu(pkt->u.err_resp.error_info));
3803}
3804
3805#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003807static void iwl4965_rx_csa(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003808{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003809 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3810 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3811 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003812 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3813 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3814 rxon->channel = csa->channel;
3815 priv->staging_rxon.channel = csa->channel;
3816}
3817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003818static void iwl4965_rx_spectrum_measure_notif(struct iwl4965_priv *priv,
3819 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003820{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003821#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003822 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3823 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003824
3825 if (!report->state) {
3826 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3827 "Spectrum Measure Notification: Start\n");
3828 return;
3829 }
3830
3831 memcpy(&priv->measure_report, report, sizeof(*report));
3832 priv->measurement_status |= MEASUREMENT_READY;
3833#endif
3834}
3835
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003836static void iwl4965_rx_pm_sleep_notif(struct iwl4965_priv *priv,
3837 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003838{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003839#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003840 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3841 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003842 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3843 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3844#endif
3845}
3846
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003847static void iwl4965_rx_pm_debug_statistics_notif(struct iwl4965_priv *priv,
3848 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003849{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003850 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003851 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3852 "notification for %s:\n",
3853 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003854 iwl4965_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003855}
3856
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003857static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003858{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003859 struct iwl4965_priv *priv =
3860 container_of(work, struct iwl4965_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003861 struct sk_buff *beacon;
3862
3863 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3864 beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
3865
3866 if (!beacon) {
3867 IWL_ERROR("update beacon failed\n");
3868 return;
3869 }
3870
3871 mutex_lock(&priv->mutex);
3872 /* new beacon skb is allocated every time; dispose previous.*/
3873 if (priv->ibss_beacon)
3874 dev_kfree_skb(priv->ibss_beacon);
3875
3876 priv->ibss_beacon = beacon;
3877 mutex_unlock(&priv->mutex);
3878
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003879 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003880}
3881
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003882static void iwl4965_rx_beacon_notif(struct iwl4965_priv *priv,
3883 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003884{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003885#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003886 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3887 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3888 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003889
3890 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3891 "tsf %d %d rate %d\n",
3892 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3893 beacon->beacon_notify_hdr.failure_frame,
3894 le32_to_cpu(beacon->ibss_mgr_status),
3895 le32_to_cpu(beacon->high_tsf),
3896 le32_to_cpu(beacon->low_tsf), rate);
3897#endif
3898
3899 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3900 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3901 queue_work(priv->workqueue, &priv->beacon_update);
3902}
3903
3904/* Service response to REPLY_SCAN_CMD (0x80) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003905static void iwl4965_rx_reply_scan(struct iwl4965_priv *priv,
3906 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003907{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003908#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003909 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3910 struct iwl4965_scanreq_notification *notif =
3911 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003912
3913 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3914#endif
3915}
3916
3917/* Service SCAN_START_NOTIFICATION (0x82) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003918static void iwl4965_rx_scan_start_notif(struct iwl4965_priv *priv,
3919 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003920{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003921 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3922 struct iwl4965_scanstart_notification *notif =
3923 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003924 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3925 IWL_DEBUG_SCAN("Scan start: "
3926 "%d [802.11%s] "
3927 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3928 notif->channel,
3929 notif->band ? "bg" : "a",
3930 notif->tsf_high,
3931 notif->tsf_low, notif->status, notif->beacon_timer);
3932}
3933
3934/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003935static void iwl4965_rx_scan_results_notif(struct iwl4965_priv *priv,
3936 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003937{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003938 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3939 struct iwl4965_scanresults_notification *notif =
3940 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003941
3942 IWL_DEBUG_SCAN("Scan ch.res: "
3943 "%d [802.11%s] "
3944 "(TSF: 0x%08X:%08X) - %d "
3945 "elapsed=%lu usec (%dms since last)\n",
3946 notif->channel,
3947 notif->band ? "bg" : "a",
3948 le32_to_cpu(notif->tsf_high),
3949 le32_to_cpu(notif->tsf_low),
3950 le32_to_cpu(notif->statistics[0]),
3951 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3952 jiffies_to_msecs(elapsed_jiffies
3953 (priv->last_scan_jiffies, jiffies)));
3954
3955 priv->last_scan_jiffies = jiffies;
3956}
3957
3958/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003959static void iwl4965_rx_scan_complete_notif(struct iwl4965_priv *priv,
3960 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003961{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003962 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3963 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003964
3965 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3966 scan_notif->scanned_channels,
3967 scan_notif->tsf_low,
3968 scan_notif->tsf_high, scan_notif->status);
3969
3970 /* The HW is no longer scanning */
3971 clear_bit(STATUS_SCAN_HW, &priv->status);
3972
3973 /* The scan completion notification came in, so kill that timer... */
3974 cancel_delayed_work(&priv->scan_check);
3975
3976 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3977 (priv->scan_bands == 2) ? "2.4" : "5.2",
3978 jiffies_to_msecs(elapsed_jiffies
3979 (priv->scan_pass_start, jiffies)));
3980
3981 /* Remove this scanned band from the list
3982 * of pending bands to scan */
3983 priv->scan_bands--;
3984
3985 /* If a request to abort was given, or the scan did not succeed
3986 * then we reset the scan state machine and terminate,
3987 * re-queuing another scan if one has been requested */
3988 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3989 IWL_DEBUG_INFO("Aborted scan completed.\n");
3990 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3991 } else {
3992 /* If there are more bands on this scan pass reschedule */
3993 if (priv->scan_bands > 0)
3994 goto reschedule;
3995 }
3996
3997 priv->last_scan_jiffies = jiffies;
3998 IWL_DEBUG_INFO("Setting scan to off\n");
3999
4000 clear_bit(STATUS_SCANNING, &priv->status);
4001
4002 IWL_DEBUG_INFO("Scan took %dms\n",
4003 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
4004
4005 queue_work(priv->workqueue, &priv->scan_completed);
4006
4007 return;
4008
4009reschedule:
4010 priv->scan_pass_start = jiffies;
4011 queue_work(priv->workqueue, &priv->request_scan);
4012}
4013
4014/* Handle notification from uCode that card's power state is changing
4015 * due to software, hardware, or critical temperature RFKILL */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004016static void iwl4965_rx_card_state_notif(struct iwl4965_priv *priv,
4017 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07004018{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004019 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004020 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
4021 unsigned long status = priv->status;
4022
4023 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
4024 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
4025 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
4026
4027 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
4028 RF_CARD_DISABLED)) {
4029
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004030 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07004031 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4032
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004033 if (!iwl4965_grab_nic_access(priv)) {
4034 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07004035 priv, HBUS_TARG_MBX_C,
4036 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4037
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004038 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004039 }
4040
4041 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004042 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07004043 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004044 if (!iwl4965_grab_nic_access(priv)) {
4045 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07004046 priv, HBUS_TARG_MBX_C,
4047 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4048
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004049 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004050 }
4051 }
4052
4053 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004054 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07004055 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004056 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
4057 if (!iwl4965_grab_nic_access(priv))
4058 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004059 }
4060 }
4061
4062 if (flags & HW_CARD_DISABLED)
4063 set_bit(STATUS_RF_KILL_HW, &priv->status);
4064 else
4065 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4066
4067
4068 if (flags & SW_CARD_DISABLED)
4069 set_bit(STATUS_RF_KILL_SW, &priv->status);
4070 else
4071 clear_bit(STATUS_RF_KILL_SW, &priv->status);
4072
4073 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004074 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004075
4076 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
4077 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
4078 (test_bit(STATUS_RF_KILL_SW, &status) !=
4079 test_bit(STATUS_RF_KILL_SW, &priv->status)))
4080 queue_work(priv->workqueue, &priv->rf_kill);
4081 else
4082 wake_up_interruptible(&priv->wait_command_queue);
4083}
4084
4085/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004086 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07004087 *
4088 * Setup the RX handlers for each of the reply types sent from the uCode
4089 * to the host.
4090 *
4091 * This function chains into the hardware specific files for them to setup
4092 * any hardware specific handlers as well.
4093 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004094static void iwl4965_setup_rx_handlers(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004095{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004096 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
4097 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
4098 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
4099 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07004100 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004101 iwl4965_rx_spectrum_measure_notif;
4102 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004103 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004104 iwl4965_rx_pm_debug_statistics_notif;
4105 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004106
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004107 /* NOTE: iwl4965_rx_statistics is different based on whether
Zhu Yib481de92007-09-25 17:54:57 -07004108 * the build is for the 3945 or the 4965. See the
4109 * corresponding implementation in iwl-XXXX.c
4110 *
4111 * The same handler is used for both the REPLY to a
4112 * discrete statistics request from the host as well as
4113 * for the periodic statistics notification from the uCode
4114 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004115 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
4116 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07004117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004118 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
4119 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004120 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004121 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004122 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004123 iwl4965_rx_scan_complete_notif;
4124 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
4125 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07004126
4127 /* Setup hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004128 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004129}
4130
4131/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004132 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07004133 * @rxb: Rx buffer to reclaim
4134 *
4135 * If an Rx buffer has an async callback associated with it the callback
4136 * will be executed. The attached skb (if present) will only be freed
4137 * if the callback returns 1
4138 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004139static void iwl4965_tx_cmd_complete(struct iwl4965_priv *priv,
4140 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07004141{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004142 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004143 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
4144 int txq_id = SEQ_TO_QUEUE(sequence);
4145 int index = SEQ_TO_INDEX(sequence);
4146 int huge = sequence & SEQ_HUGE_FRAME;
4147 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004148 struct iwl4965_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07004149
4150 /* If a Tx command is being handled and it isn't in the actual
4151 * command queue then there a command routing bug has been introduced
4152 * in the queue management code. */
4153 if (txq_id != IWL_CMD_QUEUE_NUM)
4154 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
4155 txq_id, pkt->hdr.cmd);
4156 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
4157
4158 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
4159 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
4160
4161 /* Input error checking is done when commands are added to queue. */
4162 if (cmd->meta.flags & CMD_WANT_SKB) {
4163 cmd->meta.source->u.skb = rxb->skb;
4164 rxb->skb = NULL;
4165 } else if (cmd->meta.u.callback &&
4166 !cmd->meta.u.callback(priv, cmd, rxb->skb))
4167 rxb->skb = NULL;
4168
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004169 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07004170
4171 if (!(cmd->meta.flags & CMD_ASYNC)) {
4172 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4173 wake_up_interruptible(&priv->wait_command_queue);
4174 }
4175}
4176
4177/************************** RX-FUNCTIONS ****************************/
4178/*
4179 * Rx theory of operation
4180 *
4181 * The host allocates 32 DMA target addresses and passes the host address
4182 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
4183 * 0 to 31
4184 *
4185 * Rx Queue Indexes
4186 * The host/firmware share two index registers for managing the Rx buffers.
4187 *
4188 * The READ index maps to the first position that the firmware may be writing
4189 * to -- the driver can read up to (but not including) this position and get
4190 * good data.
4191 * The READ index is managed by the firmware once the card is enabled.
4192 *
4193 * The WRITE index maps to the last position the driver has read from -- the
4194 * position preceding WRITE is the last slot the firmware can place a packet.
4195 *
4196 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
4197 * WRITE = READ.
4198 *
4199 * During initialization the host sets up the READ queue position to the first
4200 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4201 *
4202 * When the firmware places a packet in a buffer it will advance the READ index
4203 * and fire the RX interrupt. The driver can then query the READ index and
4204 * process as many packets as possible, moving the WRITE index forward as it
4205 * resets the Rx queue buffers with new memory.
4206 *
4207 * The management in the driver is as follows:
4208 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
4209 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08004210 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004211 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07004212 * iwl->rxq is replenished and the READ INDEX is updated (updating the
4213 * 'processed' and 'read' driver indexes as well)
4214 * + A received packet is processed and handed to the kernel network stack,
4215 * detached from the iwl->rxq. The driver 'processed' index is updated.
4216 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
4217 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
4218 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
4219 * were enough free buffers and RX_STALLED is set it is cleared.
4220 *
4221 *
4222 * Driver sequence:
4223 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004224 * iwl4965_rx_queue_alloc() Allocates rx_free
4225 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
4226 * iwl4965_rx_queue_restock
4227 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07004228 * queue, updates firmware pointers, and updates
4229 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004230 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07004231 *
4232 * -- enable interrupts --
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004233 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07004234 * READ INDEX, detaching the SKB from the pool.
4235 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004236 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07004237 * slots.
4238 * ...
4239 *
4240 */
4241
4242/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004243 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07004244 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004245static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004246{
4247 int s = q->read - q->write;
4248 if (s <= 0)
4249 s += RX_QUEUE_SIZE;
4250 /* keep some buffer to not confuse full and empty queue */
4251 s -= 2;
4252 if (s < 0)
4253 s = 0;
4254 return s;
4255}
4256
4257/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004258 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07004259 *
4260 * NOTE: This function has 3945 and 4965 specific code sections
4261 * but is declared in base due to the majority of the
4262 * implementation being the same (only a numeric constant is
4263 * different)
4264 *
4265 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004266int iwl4965_rx_queue_update_write_ptr(struct iwl4965_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004267{
4268 u32 reg = 0;
4269 int rc = 0;
4270 unsigned long flags;
4271
4272 spin_lock_irqsave(&q->lock, flags);
4273
4274 if (q->need_update == 0)
4275 goto exit_unlock;
4276
4277 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004278 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004279
4280 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004281 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004282 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4283 goto exit_unlock;
4284 }
4285
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004286 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004287 if (rc)
4288 goto exit_unlock;
4289
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004290 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07004291 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004292 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004293 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004294 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07004295
4296
4297 q->need_update = 0;
4298
4299 exit_unlock:
4300 spin_unlock_irqrestore(&q->lock, flags);
4301 return rc;
4302}
4303
4304/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004305 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer pointer.
Zhu Yib481de92007-09-25 17:54:57 -07004306 *
4307 * NOTE: This function has 3945 and 4965 specific code paths in it.
4308 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004309static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004310 dma_addr_t dma_addr)
4311{
4312 return cpu_to_le32((u32)(dma_addr >> 8));
4313}
4314
4315
4316/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004317 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07004318 *
4319 * If there are slots in the RX queue that need to be restocked,
4320 * and we have free pre-allocated buffers, fill the ranks as much
4321 * as we can pulling from rx_free.
4322 *
4323 * This moves the 'write' index forward to catch up with 'processed', and
4324 * also updates the memory address in the firmware to reference the new
4325 * target buffer.
4326 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004327static int iwl4965_rx_queue_restock(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004328{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004329 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004330 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004331 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004332 unsigned long flags;
4333 int write, rc;
4334
4335 spin_lock_irqsave(&rxq->lock, flags);
4336 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004337 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Zhu Yib481de92007-09-25 17:54:57 -07004338 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004339 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004340 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004341 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004342 rxq->queue[rxq->write] = rxb;
4343 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4344 rxq->free_count--;
4345 }
4346 spin_unlock_irqrestore(&rxq->lock, flags);
4347 /* If the pre-allocated buffer pool is dropping low, schedule to
4348 * refill it */
4349 if (rxq->free_count <= RX_LOW_WATERMARK)
4350 queue_work(priv->workqueue, &priv->rx_replenish);
4351
4352
4353 /* If we've added more space for the firmware to place data, tell it */
4354 if ((write != (rxq->write & ~0x7))
4355 || (abs(rxq->write - rxq->read) > 7)) {
4356 spin_lock_irqsave(&rxq->lock, flags);
4357 rxq->need_update = 1;
4358 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004359 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004360 if (rc)
4361 return rc;
4362 }
4363
4364 return 0;
4365}
4366
4367/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004368 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004369 *
4370 * When moving to rx_free an SKB is allocated for the slot.
4371 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004372 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004373 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004374 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004375void iwl4965_rx_replenish(void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004376{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004377 struct iwl4965_priv *priv = data;
4378 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004379 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004380 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004381 unsigned long flags;
4382 spin_lock_irqsave(&rxq->lock, flags);
4383 while (!list_empty(&rxq->rx_used)) {
4384 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004385 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004386 rxb->skb =
4387 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
4388 if (!rxb->skb) {
4389 if (net_ratelimit())
4390 printk(KERN_CRIT DRV_NAME
4391 ": Can not allocate SKB buffers\n");
4392 /* We don't reschedule replenish work here -- we will
4393 * call the restock method and if it still needs
4394 * more buffers it will schedule replenish */
4395 break;
4396 }
4397 priv->alloc_rxb_skb++;
4398 list_del(element);
4399 rxb->dma_addr =
4400 pci_map_single(priv->pci_dev, rxb->skb->data,
4401 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4402 list_add_tail(&rxb->list, &rxq->rx_free);
4403 rxq->free_count++;
4404 }
4405 spin_unlock_irqrestore(&rxq->lock, flags);
4406
4407 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004408 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004409 spin_unlock_irqrestore(&priv->lock, flags);
4410}
4411
4412/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
4413 * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
4414 * This free routine walks the list of POOL entries and if SKB is set to
4415 * non NULL it is unmapped and freed
4416 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004417static void iwl4965_rx_queue_free(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004418{
4419 int i;
4420 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4421 if (rxq->pool[i].skb != NULL) {
4422 pci_unmap_single(priv->pci_dev,
4423 rxq->pool[i].dma_addr,
4424 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4425 dev_kfree_skb(rxq->pool[i].skb);
4426 }
4427 }
4428
4429 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4430 rxq->dma_addr);
4431 rxq->bd = NULL;
4432}
4433
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004434int iwl4965_rx_queue_alloc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004435{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004436 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004437 struct pci_dev *dev = priv->pci_dev;
4438 int i;
4439
4440 spin_lock_init(&rxq->lock);
4441 INIT_LIST_HEAD(&rxq->rx_free);
4442 INIT_LIST_HEAD(&rxq->rx_used);
4443 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4444 if (!rxq->bd)
4445 return -ENOMEM;
4446 /* Fill the rx_used queue with _all_ of the Rx buffers */
4447 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4448 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4449 /* Set us so that we have processed and used all buffers, but have
4450 * not restocked the Rx queue with fresh buffers */
4451 rxq->read = rxq->write = 0;
4452 rxq->free_count = 0;
4453 rxq->need_update = 0;
4454 return 0;
4455}
4456
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004457void iwl4965_rx_queue_reset(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004458{
4459 unsigned long flags;
4460 int i;
4461 spin_lock_irqsave(&rxq->lock, flags);
4462 INIT_LIST_HEAD(&rxq->rx_free);
4463 INIT_LIST_HEAD(&rxq->rx_used);
4464 /* Fill the rx_used queue with _all_ of the Rx buffers */
4465 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4466 /* In the reset function, these buffers may have been allocated
4467 * to an SKB, so we need to unmap and free potential storage */
4468 if (rxq->pool[i].skb != NULL) {
4469 pci_unmap_single(priv->pci_dev,
4470 rxq->pool[i].dma_addr,
4471 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4472 priv->alloc_rxb_skb--;
4473 dev_kfree_skb(rxq->pool[i].skb);
4474 rxq->pool[i].skb = NULL;
4475 }
4476 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4477 }
4478
4479 /* Set us so that we have processed and used all buffers, but have
4480 * not restocked the Rx queue with fresh buffers */
4481 rxq->read = rxq->write = 0;
4482 rxq->free_count = 0;
4483 spin_unlock_irqrestore(&rxq->lock, flags);
4484}
4485
4486/* Convert linear signal-to-noise ratio into dB */
4487static u8 ratio2dB[100] = {
4488/* 0 1 2 3 4 5 6 7 8 9 */
4489 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4490 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4491 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4492 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4493 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4494 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4495 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4496 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4497 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4498 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4499};
4500
4501/* Calculates a relative dB value from a ratio of linear
4502 * (i.e. not dB) signal levels.
4503 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004504int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004505{
Adrian Bunkc899a572007-10-14 19:51:15 +02004506 /* 1000:1 or higher just report as 60 dB */
4507 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004508 return 60;
4509
Adrian Bunkc899a572007-10-14 19:51:15 +02004510 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004511 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004512 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004513 return (20 + (int)ratio2dB[sig_ratio/10]);
4514
4515 /* We shouldn't see this */
4516 if (sig_ratio < 1)
4517 return 0;
4518
4519 /* Use table for ratios 1:1 - 99:1 */
4520 return (int)ratio2dB[sig_ratio];
4521}
4522
4523#define PERFECT_RSSI (-20) /* dBm */
4524#define WORST_RSSI (-95) /* dBm */
4525#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4526
4527/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4528 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4529 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004530int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004531{
4532 int sig_qual;
4533 int degradation = PERFECT_RSSI - rssi_dbm;
4534
4535 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4536 * as indicator; formula is (signal dbm - noise dbm).
4537 * SNR at or above 40 is a great signal (100%).
4538 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4539 * Weakest usable signal is usually 10 - 15 dB SNR. */
4540 if (noise_dbm) {
4541 if (rssi_dbm - noise_dbm >= 40)
4542 return 100;
4543 else if (rssi_dbm < noise_dbm)
4544 return 0;
4545 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4546
4547 /* Else use just the signal level.
4548 * This formula is a least squares fit of data points collected and
4549 * compared with a reference system that had a percentage (%) display
4550 * for signal quality. */
4551 } else
4552 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4553 (15 * RSSI_RANGE + 62 * degradation)) /
4554 (RSSI_RANGE * RSSI_RANGE);
4555
4556 if (sig_qual > 100)
4557 sig_qual = 100;
4558 else if (sig_qual < 1)
4559 sig_qual = 0;
4560
4561 return sig_qual;
4562}
4563
4564/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004565 * iwl4965_rx_handle - Main entry function for receiving responses from the uCode
Zhu Yib481de92007-09-25 17:54:57 -07004566 *
4567 * Uses the priv->rx_handlers callback function array to invoke
4568 * the appropriate handlers, including command responses,
4569 * frame-received notifications, and other notifications.
4570 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004571static void iwl4965_rx_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004572{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004573 struct iwl4965_rx_mem_buffer *rxb;
4574 struct iwl4965_rx_packet *pkt;
4575 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004576 u32 r, i;
4577 int reclaim;
4578 unsigned long flags;
4579
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004580 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004581 i = rxq->read;
4582
4583 /* Rx interrupt, but nothing sent from uCode */
4584 if (i == r)
4585 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4586
4587 while (i != r) {
4588 rxb = rxq->queue[i];
4589
4590 /* If an RXB doesn't have a queue slot associated with it
4591 * then a bug has been introduced in the queue refilling
4592 * routines -- catch it here */
4593 BUG_ON(rxb == NULL);
4594
4595 rxq->queue[i] = NULL;
4596
4597 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4598 IWL_RX_BUF_SIZE,
4599 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004600 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004601
4602 /* Reclaim a command buffer only if this packet is a response
4603 * to a (driver-originated) command.
4604 * If the packet (e.g. Rx frame) originated from uCode,
4605 * there is no command buffer to reclaim.
4606 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4607 * but apparently a few don't get set; catch them here. */
4608 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4609 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4610 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004611 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004612 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4613 (pkt->hdr.cmd != REPLY_TX);
4614
4615 /* Based on type of command response or notification,
4616 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004617 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004618 if (priv->rx_handlers[pkt->hdr.cmd]) {
4619 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4620 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4621 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4622 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4623 } else {
4624 /* No handling needed */
4625 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4626 "r %d i %d No handler needed for %s, 0x%02x\n",
4627 r, i, get_cmd_string(pkt->hdr.cmd),
4628 pkt->hdr.cmd);
4629 }
4630
4631 if (reclaim) {
4632 /* Invoke any callbacks, transfer the skb to caller,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004633 * and fire off the (possibly) blocking iwl4965_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004634 * as we reclaim the driver command queue */
4635 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004636 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004637 else
4638 IWL_WARNING("Claim null rxb?\n");
4639 }
4640
4641 /* For now we just don't re-use anything. We can tweak this
4642 * later to try and re-use notification packets and SKBs that
4643 * fail to Rx correctly */
4644 if (rxb->skb != NULL) {
4645 priv->alloc_rxb_skb--;
4646 dev_kfree_skb_any(rxb->skb);
4647 rxb->skb = NULL;
4648 }
4649
4650 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4651 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4652 spin_lock_irqsave(&rxq->lock, flags);
4653 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4654 spin_unlock_irqrestore(&rxq->lock, flags);
4655 i = (i + 1) & RX_QUEUE_MASK;
4656 }
4657
4658 /* Backtrack one entry */
4659 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004660 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004661}
4662
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004663static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
4664 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004665{
4666 u32 reg = 0;
4667 int rc = 0;
4668 int txq_id = txq->q.id;
4669
4670 if (txq->need_update == 0)
4671 return rc;
4672
4673 /* if we're trying to save power */
4674 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4675 /* wake up nic if it's powered down ...
4676 * uCode will wake up, and interrupt us again, so next
4677 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004678 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004679
4680 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4681 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004682 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004683 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4684 return rc;
4685 }
4686
4687 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004688 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004689 if (rc)
4690 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004691 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004692 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004693 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004694
4695 /* else not in power-save mode, uCode will never sleep when we're
4696 * trying to tx (during RFKILL, we're not trying to tx). */
4697 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004698 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004699 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004700
4701 txq->need_update = 0;
4702
4703 return rc;
4704}
4705
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004706#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004707static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004708{
Joe Perches0795af52007-10-03 17:59:30 -07004709 DECLARE_MAC_BUF(mac);
4710
Zhu Yib481de92007-09-25 17:54:57 -07004711 IWL_DEBUG_RADIO("RX CONFIG:\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004712 iwl4965_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004713 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4714 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4715 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4716 le32_to_cpu(rxon->filter_flags));
4717 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4718 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4719 rxon->ofdm_basic_rates);
4720 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004721 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4722 print_mac(mac, rxon->node_addr));
4723 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4724 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004725 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4726}
4727#endif
4728
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004729static void iwl4965_enable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004730{
4731 IWL_DEBUG_ISR("Enabling interrupts\n");
4732 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004733 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004734}
4735
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004736static inline void iwl4965_disable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004737{
4738 clear_bit(STATUS_INT_ENABLED, &priv->status);
4739
4740 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004741 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004742
4743 /* acknowledge/clear/reset any interrupts still pending
4744 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004745 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4746 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004747 IWL_DEBUG_ISR("Disabled interrupts\n");
4748}
4749
4750static const char *desc_lookup(int i)
4751{
4752 switch (i) {
4753 case 1:
4754 return "FAIL";
4755 case 2:
4756 return "BAD_PARAM";
4757 case 3:
4758 return "BAD_CHECKSUM";
4759 case 4:
4760 return "NMI_INTERRUPT";
4761 case 5:
4762 return "SYSASSERT";
4763 case 6:
4764 return "FATAL_ERROR";
4765 }
4766
4767 return "UNKNOWN";
4768}
4769
4770#define ERROR_START_OFFSET (1 * sizeof(u32))
4771#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004773static void iwl4965_dump_nic_error_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004774{
4775 u32 data2, line;
4776 u32 desc, time, count, base, data1;
4777 u32 blink1, blink2, ilink1, ilink2;
4778 int rc;
4779
4780 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4781
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004782 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004783 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4784 return;
4785 }
4786
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004787 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004788 if (rc) {
4789 IWL_WARNING("Can not read from adapter at this time.\n");
4790 return;
4791 }
4792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004793 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004794
4795 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4796 IWL_ERROR("Start IWL Error Log Dump:\n");
4797 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4798 priv->status, priv->config, count);
4799 }
4800
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004801 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4802 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4803 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4804 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4805 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4806 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4807 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4808 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4809 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004810
4811 IWL_ERROR("Desc Time "
4812 "data1 data2 line\n");
4813 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4814 desc_lookup(desc), desc, time, data1, data2, line);
4815 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4816 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4817 ilink1, ilink2);
4818
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004819 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004820}
4821
4822#define EVENT_START_OFFSET (4 * sizeof(u32))
4823
4824/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004825 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004826 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004827 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004828 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004829static void iwl4965_print_event_log(struct iwl4965_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004830 u32 num_events, u32 mode)
4831{
4832 u32 i;
4833 u32 base; /* SRAM byte address of event log header */
4834 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4835 u32 ptr; /* SRAM byte address of log data */
4836 u32 ev, time, data; /* event log data */
4837
4838 if (num_events == 0)
4839 return;
4840
4841 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4842
4843 if (mode == 0)
4844 event_size = 2 * sizeof(u32);
4845 else
4846 event_size = 3 * sizeof(u32);
4847
4848 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4849
4850 /* "time" is actually "data" for mode 0 (no timestamp).
4851 * place event id # at far right for easier visual parsing. */
4852 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004853 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004854 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004855 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004856 ptr += sizeof(u32);
4857 if (mode == 0)
4858 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4859 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004860 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004861 ptr += sizeof(u32);
4862 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4863 }
4864 }
4865}
4866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004867static void iwl4965_dump_nic_event_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004868{
4869 int rc;
4870 u32 base; /* SRAM byte address of event log header */
4871 u32 capacity; /* event log capacity in # entries */
4872 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4873 u32 num_wraps; /* # times uCode wrapped to top of log */
4874 u32 next_entry; /* index of next entry to be written by uCode */
4875 u32 size; /* # entries that we'll print */
4876
4877 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004878 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004879 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4880 return;
4881 }
4882
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004883 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004884 if (rc) {
4885 IWL_WARNING("Can not read from adapter at this time.\n");
4886 return;
4887 }
4888
4889 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004890 capacity = iwl4965_read_targ_mem(priv, base);
4891 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4892 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4893 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004894
4895 size = num_wraps ? capacity : next_entry;
4896
4897 /* bail out if nothing in log */
4898 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004899 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004900 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004901 return;
4902 }
4903
Zhu Yi583fab32007-09-27 11:27:30 +08004904 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004905 size, num_wraps);
4906
4907 /* if uCode has wrapped back to top of log, start at the oldest entry,
4908 * i.e the next one that uCode would fill. */
4909 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004910 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004911 capacity - next_entry, mode);
4912
4913 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004914 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004915
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004916 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004917}
4918
4919/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004920 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004921 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004922static void iwl4965_irq_handle_error(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004923{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004924 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004925 set_bit(STATUS_FW_ERROR, &priv->status);
4926
4927 /* Cancel currently queued command. */
4928 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4929
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004930#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004931 if (iwl4965_debug_level & IWL_DL_FW_ERRORS) {
4932 iwl4965_dump_nic_error_log(priv);
4933 iwl4965_dump_nic_event_log(priv);
4934 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004935 }
4936#endif
4937
4938 wake_up_interruptible(&priv->wait_command_queue);
4939
4940 /* Keep the restart process from trying to send host
4941 * commands by clearing the INIT status bit */
4942 clear_bit(STATUS_READY, &priv->status);
4943
4944 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4945 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4946 "Restarting adapter due to uCode error.\n");
4947
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004948 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004949 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4950 sizeof(priv->recovery_rxon));
4951 priv->error_recovering = 1;
4952 }
4953 queue_work(priv->workqueue, &priv->restart);
4954 }
4955}
4956
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004957static void iwl4965_error_recovery(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004958{
4959 unsigned long flags;
4960
4961 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4962 sizeof(priv->staging_rxon));
4963 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004964 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004965
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004966 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004967
4968 spin_lock_irqsave(&priv->lock, flags);
4969 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4970 priv->error_recovering = 0;
4971 spin_unlock_irqrestore(&priv->lock, flags);
4972}
4973
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004974static void iwl4965_irq_tasklet(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004975{
4976 u32 inta, handled = 0;
4977 u32 inta_fh;
4978 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004979#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004980 u32 inta_mask;
4981#endif
4982
4983 spin_lock_irqsave(&priv->lock, flags);
4984
4985 /* Ack/clear/reset pending uCode interrupts.
4986 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4987 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004988 inta = iwl4965_read32(priv, CSR_INT);
4989 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004990
4991 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4992 * Any new interrupts that happen after this, either while we're
4993 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004994 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
4995 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004996
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004997#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004998 if (iwl4965_debug_level & IWL_DL_ISR) {
4999 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
Zhu Yib481de92007-09-25 17:54:57 -07005000 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5001 inta, inta_mask, inta_fh);
5002 }
5003#endif
5004
5005 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
5006 * atomic, make sure that inta covers all the interrupts that
5007 * we've discovered, even if FH interrupt came in just after
5008 * reading CSR_INT. */
5009 if (inta_fh & CSR_FH_INT_RX_MASK)
5010 inta |= CSR_INT_BIT_FH_RX;
5011 if (inta_fh & CSR_FH_INT_TX_MASK)
5012 inta |= CSR_INT_BIT_FH_TX;
5013
5014 /* Now service all interrupt bits discovered above. */
5015 if (inta & CSR_INT_BIT_HW_ERR) {
5016 IWL_ERROR("Microcode HW error detected. Restarting.\n");
5017
5018 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005019 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005020
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005021 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005022
5023 handled |= CSR_INT_BIT_HW_ERR;
5024
5025 spin_unlock_irqrestore(&priv->lock, flags);
5026
5027 return;
5028 }
5029
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005030#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005031 if (iwl4965_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07005032 /* NIC fires this, but we don't use it, redundant with WAKEUP */
5033 if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
5034 IWL_DEBUG_ISR("Microcode started or stopped.\n");
5035
5036 /* Alive notification via Rx interrupt will do the real work */
5037 if (inta & CSR_INT_BIT_ALIVE)
5038 IWL_DEBUG_ISR("Alive interrupt\n");
5039 }
5040#endif
5041 /* Safely ignore these bits for debug checks below */
5042 inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
5043
5044 /* HW RF KILL switch toggled (4965 only) */
5045 if (inta & CSR_INT_BIT_RF_KILL) {
5046 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005047 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07005048 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
5049 hw_rf_kill = 1;
5050
5051 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
5052 "RF_KILL bit toggled to %s.\n",
5053 hw_rf_kill ? "disable radio":"enable radio");
5054
5055 /* Queue restart only if RF_KILL switch was set to "kill"
5056 * when we loaded driver, and is now set to "enable".
5057 * After we're Alive, RF_KILL gets handled by
5058 * iwl_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08005059 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
5060 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005061 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08005062 }
Zhu Yib481de92007-09-25 17:54:57 -07005063
5064 handled |= CSR_INT_BIT_RF_KILL;
5065 }
5066
5067 /* Chip got too hot and stopped itself (4965 only) */
5068 if (inta & CSR_INT_BIT_CT_KILL) {
5069 IWL_ERROR("Microcode CT kill error detected.\n");
5070 handled |= CSR_INT_BIT_CT_KILL;
5071 }
5072
5073 /* Error detected by uCode */
5074 if (inta & CSR_INT_BIT_SW_ERR) {
5075 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
5076 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005077 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005078 handled |= CSR_INT_BIT_SW_ERR;
5079 }
5080
5081 /* uCode wakes up after power-down sleep */
5082 if (inta & CSR_INT_BIT_WAKEUP) {
5083 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005084 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
5085 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
5086 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
5087 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
5088 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
5089 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
5090 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07005091
5092 handled |= CSR_INT_BIT_WAKEUP;
5093 }
5094
5095 /* All uCode command responses, including Tx command responses,
5096 * Rx "responses" (frame-received notification), and other
5097 * notifications from uCode come through here*/
5098 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005099 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005100 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
5101 }
5102
5103 if (inta & CSR_INT_BIT_FH_TX) {
5104 IWL_DEBUG_ISR("Tx interrupt\n");
5105 handled |= CSR_INT_BIT_FH_TX;
5106 }
5107
5108 if (inta & ~handled)
5109 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
5110
5111 if (inta & ~CSR_INI_SET_MASK) {
5112 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
5113 inta & ~CSR_INI_SET_MASK);
5114 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
5115 }
5116
5117 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005118 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005119
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005120#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005121 if (iwl4965_debug_level & (IWL_DL_ISR)) {
5122 inta = iwl4965_read32(priv, CSR_INT);
5123 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
5124 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07005125 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
5126 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
5127 }
5128#endif
5129 spin_unlock_irqrestore(&priv->lock, flags);
5130}
5131
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005132static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07005133{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005134 struct iwl4965_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07005135 u32 inta, inta_mask;
5136 u32 inta_fh;
5137 if (!priv)
5138 return IRQ_NONE;
5139
5140 spin_lock(&priv->lock);
5141
5142 /* Disable (but don't clear!) interrupts here to avoid
5143 * back-to-back ISRs and sporadic interrupts from our NIC.
5144 * If we have something to service, the tasklet will re-enable ints.
5145 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005146 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
5147 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07005148
5149 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005150 inta = iwl4965_read32(priv, CSR_INT);
5151 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07005152
5153 /* Ignore interrupt if there's nothing in NIC to service.
5154 * This may be due to IRQ shared with another device,
5155 * or due to sporadic interrupts thrown from our NIC. */
5156 if (!inta && !inta_fh) {
5157 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
5158 goto none;
5159 }
5160
5161 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08005162 /* Hardware disappeared. It might have already raised
5163 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07005164 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08005165 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07005166 }
5167
5168 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5169 inta, inta_mask, inta_fh);
5170
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005171 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Zhu Yib481de92007-09-25 17:54:57 -07005172 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07005173
Oliver Neukum66fbb542007-11-15 09:31:10 +08005174 unplugged:
5175 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07005176 return IRQ_HANDLED;
5177
5178 none:
5179 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005180 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005181 spin_unlock(&priv->lock);
5182 return IRQ_NONE;
5183}
5184
5185/************************** EEPROM BANDS ****************************
5186 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005187 * The iwl4965_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07005188 * EEPROM contents to the specific channel number supported for each
5189 * band.
5190 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005191 * For example, iwl4965_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07005192 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5193 * The specific geography and calibration information for that channel
5194 * is contained in the eeprom map itself.
5195 *
5196 * During init, we copy the eeprom information and channel map
5197 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5198 *
5199 * channel_map_24/52 provides the index in the channel_info array for a
5200 * given channel. We have to have two separate maps as there is channel
5201 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5202 * band_2
5203 *
5204 * A value of 0xff stored in the channel_map indicates that the channel
5205 * is not supported by the hardware at all.
5206 *
5207 * A value of 0xfe in the channel_map indicates that the channel is not
5208 * valid for Tx with the current hardware. This means that
5209 * while the system can tune and receive on a given channel, it may not
5210 * be able to associate or transmit any frames on that
5211 * channel. There is no corresponding channel information for that
5212 * entry.
5213 *
5214 *********************************************************************/
5215
5216/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005217static const u8 iwl4965_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07005218 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5219};
5220
5221/* 5.2 GHz bands */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005222static const u8 iwl4965_eeprom_band_2[] = {
Zhu Yib481de92007-09-25 17:54:57 -07005223 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5224};
5225
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005226static const u8 iwl4965_eeprom_band_3[] = { /* 5205-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005227 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5228};
5229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005230static const u8 iwl4965_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005231 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5232};
5233
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005234static const u8 iwl4965_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005235 145, 149, 153, 157, 161, 165
5236};
5237
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005238static u8 iwl4965_eeprom_band_6[] = { /* 2.4 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005239 1, 2, 3, 4, 5, 6, 7
5240};
5241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005242static u8 iwl4965_eeprom_band_7[] = { /* 5.2 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005243 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5244};
5245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005246static void iwl4965_init_band_reference(const struct iwl4965_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07005247 int *eeprom_ch_count,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005248 const struct iwl4965_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07005249 **eeprom_ch_info,
5250 const u8 **eeprom_ch_index)
5251{
5252 switch (band) {
5253 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005254 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07005255 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005256 *eeprom_ch_index = iwl4965_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07005257 break;
5258 case 2: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005259 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07005260 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005261 *eeprom_ch_index = iwl4965_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07005262 break;
5263 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005264 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07005265 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005266 *eeprom_ch_index = iwl4965_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07005267 break;
5268 case 4: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005269 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07005270 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005271 *eeprom_ch_index = iwl4965_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07005272 break;
5273 case 5: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005274 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005275 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005276 *eeprom_ch_index = iwl4965_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07005277 break;
5278 case 6:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005279 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_6);
Zhu Yib481de92007-09-25 17:54:57 -07005280 *eeprom_ch_info = priv->eeprom.band_24_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005281 *eeprom_ch_index = iwl4965_eeprom_band_6;
Zhu Yib481de92007-09-25 17:54:57 -07005282 break;
5283 case 7:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005284 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_7);
Zhu Yib481de92007-09-25 17:54:57 -07005285 *eeprom_ch_info = priv->eeprom.band_52_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005286 *eeprom_ch_index = iwl4965_eeprom_band_7;
Zhu Yib481de92007-09-25 17:54:57 -07005287 break;
5288 default:
5289 BUG();
5290 return;
5291 }
5292}
5293
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005294const struct iwl4965_channel_info *iwl4965_get_channel_info(const struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005295 int phymode, u16 channel)
5296{
5297 int i;
5298
5299 switch (phymode) {
5300 case MODE_IEEE80211A:
5301 for (i = 14; i < priv->channel_count; i++) {
5302 if (priv->channel_info[i].channel == channel)
5303 return &priv->channel_info[i];
5304 }
5305 break;
5306
5307 case MODE_IEEE80211B:
5308 case MODE_IEEE80211G:
5309 if (channel >= 1 && channel <= 14)
5310 return &priv->channel_info[channel - 1];
5311 break;
5312
5313 }
5314
5315 return NULL;
5316}
5317
5318#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5319 ? # x " " : "")
5320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005321static int iwl4965_init_channel_map(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005322{
5323 int eeprom_ch_count = 0;
5324 const u8 *eeprom_ch_index = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005325 const struct iwl4965_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005326 int band, ch;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005327 struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005328
5329 if (priv->channel_count) {
5330 IWL_DEBUG_INFO("Channel map already initialized.\n");
5331 return 0;
5332 }
5333
5334 if (priv->eeprom.version < 0x2f) {
5335 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5336 priv->eeprom.version);
5337 return -EINVAL;
5338 }
5339
5340 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5341
5342 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005343 ARRAY_SIZE(iwl4965_eeprom_band_1) +
5344 ARRAY_SIZE(iwl4965_eeprom_band_2) +
5345 ARRAY_SIZE(iwl4965_eeprom_band_3) +
5346 ARRAY_SIZE(iwl4965_eeprom_band_4) +
5347 ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005348
5349 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5350
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005351 priv->channel_info = kzalloc(sizeof(struct iwl4965_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07005352 priv->channel_count, GFP_KERNEL);
5353 if (!priv->channel_info) {
5354 IWL_ERROR("Could not allocate channel_info\n");
5355 priv->channel_count = 0;
5356 return -ENOMEM;
5357 }
5358
5359 ch_info = priv->channel_info;
5360
5361 /* Loop through the 5 EEPROM bands adding them in order to the
5362 * channel map we maintain (that contains additional information than
5363 * what just in the EEPROM) */
5364 for (band = 1; band <= 5; band++) {
5365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005366 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005367 &eeprom_ch_info, &eeprom_ch_index);
5368
5369 /* Loop through each band adding each of the channels */
5370 for (ch = 0; ch < eeprom_ch_count; ch++) {
5371 ch_info->channel = eeprom_ch_index[ch];
5372 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5373 MODE_IEEE80211A;
5374
5375 /* permanently store EEPROM's channel regulatory flags
5376 * and max power in channel info database. */
5377 ch_info->eeprom = eeprom_ch_info[ch];
5378
5379 /* Copy the run-time flags so they are there even on
5380 * invalid channels */
5381 ch_info->flags = eeprom_ch_info[ch].flags;
5382
5383 if (!(is_channel_valid(ch_info))) {
5384 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5385 "No traffic\n",
5386 ch_info->channel,
5387 ch_info->flags,
5388 is_channel_a_band(ch_info) ?
5389 "5.2" : "2.4");
5390 ch_info++;
5391 continue;
5392 }
5393
5394 /* Initialize regulatory-based run-time data */
5395 ch_info->max_power_avg = ch_info->curr_txpow =
5396 eeprom_ch_info[ch].max_power_avg;
5397 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5398 ch_info->min_power = 0;
5399
5400 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5401 " %ddBm): Ad-Hoc %ssupported\n",
5402 ch_info->channel,
5403 is_channel_a_band(ch_info) ?
5404 "5.2" : "2.4",
5405 CHECK_AND_PRINT(IBSS),
5406 CHECK_AND_PRINT(ACTIVE),
5407 CHECK_AND_PRINT(RADAR),
5408 CHECK_AND_PRINT(WIDE),
5409 CHECK_AND_PRINT(NARROW),
5410 CHECK_AND_PRINT(DFS),
5411 eeprom_ch_info[ch].flags,
5412 eeprom_ch_info[ch].max_power_avg,
5413 ((eeprom_ch_info[ch].
5414 flags & EEPROM_CHANNEL_IBSS)
5415 && !(eeprom_ch_info[ch].
5416 flags & EEPROM_CHANNEL_RADAR))
5417 ? "" : "not ");
5418
5419 /* Set the user_txpower_limit to the highest power
5420 * supported by any channel */
5421 if (eeprom_ch_info[ch].max_power_avg >
5422 priv->user_txpower_limit)
5423 priv->user_txpower_limit =
5424 eeprom_ch_info[ch].max_power_avg;
5425
5426 ch_info++;
5427 }
5428 }
5429
5430 for (band = 6; band <= 7; band++) {
5431 int phymode;
5432 u8 fat_extension_chan;
5433
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005434 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005435 &eeprom_ch_info, &eeprom_ch_index);
5436
5437 phymode = (band == 6) ? MODE_IEEE80211B : MODE_IEEE80211A;
5438 /* Loop through each band adding each of the channels */
5439 for (ch = 0; ch < eeprom_ch_count; ch++) {
5440
5441 if ((band == 6) &&
5442 ((eeprom_ch_index[ch] == 5) ||
5443 (eeprom_ch_index[ch] == 6) ||
5444 (eeprom_ch_index[ch] == 7)))
5445 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5446 else
5447 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5448
5449 iwl4965_set_fat_chan_info(priv, phymode,
5450 eeprom_ch_index[ch],
5451 &(eeprom_ch_info[ch]),
5452 fat_extension_chan);
5453
5454 iwl4965_set_fat_chan_info(priv, phymode,
5455 (eeprom_ch_index[ch] + 4),
5456 &(eeprom_ch_info[ch]),
5457 HT_IE_EXT_CHANNEL_BELOW);
5458 }
5459 }
5460
5461 return 0;
5462}
5463
5464/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5465 * sending probe req. This should be set long enough to hear probe responses
5466 * from more than one AP. */
5467#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5468#define IWL_ACTIVE_DWELL_TIME_52 (10)
5469
5470/* For faster active scanning, scan will move to the next channel if fewer than
5471 * PLCP_QUIET_THRESH packets are heard on this channel within
5472 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5473 * time if it's a quiet channel (nothing responded to our probe, and there's
5474 * no other traffic).
5475 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5476#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5477#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5478
5479/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5480 * Must be set longer than active dwell time.
5481 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5482#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5483#define IWL_PASSIVE_DWELL_TIME_52 (10)
5484#define IWL_PASSIVE_DWELL_BASE (100)
5485#define IWL_CHANNEL_TUNE_TIME 5
5486
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005487static inline u16 iwl4965_get_active_dwell_time(struct iwl4965_priv *priv, int phymode)
Zhu Yib481de92007-09-25 17:54:57 -07005488{
5489 if (phymode == MODE_IEEE80211A)
5490 return IWL_ACTIVE_DWELL_TIME_52;
5491 else
5492 return IWL_ACTIVE_DWELL_TIME_24;
5493}
5494
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005495static u16 iwl4965_get_passive_dwell_time(struct iwl4965_priv *priv, int phymode)
Zhu Yib481de92007-09-25 17:54:57 -07005496{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005497 u16 active = iwl4965_get_active_dwell_time(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005498 u16 passive = (phymode != MODE_IEEE80211A) ?
5499 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5500 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005502 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005503 /* If we're associated, we clamp the maximum passive
5504 * dwell time to be 98% of the beacon interval (minus
5505 * 2 * channel tune time) */
5506 passive = priv->beacon_int;
5507 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5508 passive = IWL_PASSIVE_DWELL_BASE;
5509 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5510 }
5511
5512 if (passive <= active)
5513 passive = active + 1;
5514
5515 return passive;
5516}
5517
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005518static int iwl4965_get_channels_for_scan(struct iwl4965_priv *priv, int phymode,
Zhu Yib481de92007-09-25 17:54:57 -07005519 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005520 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07005521{
5522 const struct ieee80211_channel *channels = NULL;
5523 const struct ieee80211_hw_mode *hw_mode;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005524 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005525 u16 passive_dwell = 0;
5526 u16 active_dwell = 0;
5527 int added, i;
5528
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005529 hw_mode = iwl4965_get_hw_mode(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005530 if (!hw_mode)
5531 return 0;
5532
5533 channels = hw_mode->channels;
5534
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005535 active_dwell = iwl4965_get_active_dwell_time(priv, phymode);
5536 passive_dwell = iwl4965_get_passive_dwell_time(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005537
5538 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5539 if (channels[i].chan ==
5540 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005541 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005542 IWL_DEBUG_SCAN
5543 ("Skipping current channel %d\n",
5544 le16_to_cpu(priv->active_rxon.channel));
5545 continue;
5546 }
5547 } else if (priv->only_active_channel)
5548 continue;
5549
5550 scan_ch->channel = channels[i].chan;
5551
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005552 ch_info = iwl4965_get_channel_info(priv, phymode, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005553 if (!is_channel_valid(ch_info)) {
5554 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5555 scan_ch->channel);
5556 continue;
5557 }
5558
5559 if (!is_active || is_channel_passive(ch_info) ||
5560 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5561 scan_ch->type = 0; /* passive */
5562 else
5563 scan_ch->type = 1; /* active */
5564
5565 if (scan_ch->type & 1)
5566 scan_ch->type |= (direct_mask << 1);
5567
5568 if (is_channel_narrow(ch_info))
5569 scan_ch->type |= (1 << 7);
5570
5571 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5572 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5573
5574 /* Set power levels to defaults */
5575 scan_ch->tpc.dsp_atten = 110;
5576 /* scan_pwr_info->tpc.dsp_atten; */
5577
5578 /*scan_pwr_info->tpc.tx_gain; */
5579 if (phymode == MODE_IEEE80211A)
5580 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5581 else {
5582 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5583 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
5584 * power level
5585 scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
5586 */
5587 }
5588
5589 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5590 scan_ch->channel,
5591 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5592 (scan_ch->type & 1) ?
5593 active_dwell : passive_dwell);
5594
5595 scan_ch++;
5596 added++;
5597 }
5598
5599 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5600 return added;
5601}
5602
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005603static void iwl4965_reset_channel_flag(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005604{
5605 int i, j;
5606 for (i = 0; i < 3; i++) {
5607 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5608 for (j = 0; j < hw_mode->num_channels; j++)
5609 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5610 }
5611}
5612
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005613static void iwl4965_init_hw_rates(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005614 struct ieee80211_rate *rates)
5615{
5616 int i;
5617
5618 for (i = 0; i < IWL_RATE_COUNT; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005619 rates[i].rate = iwl4965_rates[i].ieee * 5;
Zhu Yib481de92007-09-25 17:54:57 -07005620 rates[i].val = i; /* Rate scaling will work on indexes */
5621 rates[i].val2 = i;
5622 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5623 /* Only OFDM have the bits-per-symbol set */
5624 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5625 rates[i].flags |= IEEE80211_RATE_OFDM;
5626 else {
5627 /*
5628 * If CCK 1M then set rate flag to CCK else CCK_2
5629 * which is CCK | PREAMBLE2
5630 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005631 rates[i].flags |= (iwl4965_rates[i].plcp == 10) ?
Zhu Yib481de92007-09-25 17:54:57 -07005632 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5633 }
5634
5635 /* Set up which ones are basic rates... */
5636 if (IWL_BASIC_RATES_MASK & (1 << i))
5637 rates[i].flags |= IEEE80211_RATE_BASIC;
5638 }
Zhu Yib481de92007-09-25 17:54:57 -07005639}
5640
5641/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005642 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005643 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005644static int iwl4965_init_geos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005645{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005646 struct iwl4965_channel_info *ch;
Zhu Yib481de92007-09-25 17:54:57 -07005647 struct ieee80211_hw_mode *modes;
5648 struct ieee80211_channel *channels;
5649 struct ieee80211_channel *geo_ch;
5650 struct ieee80211_rate *rates;
5651 int i = 0;
5652 enum {
5653 A = 0,
5654 B = 1,
5655 G = 2,
5656 A_11N = 3,
5657 G_11N = 4,
5658 };
5659 int mode_count = 5;
5660
5661 if (priv->modes) {
5662 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5663 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5664 return 0;
5665 }
5666
5667 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5668 GFP_KERNEL);
5669 if (!modes)
5670 return -ENOMEM;
5671
5672 channels = kzalloc(sizeof(struct ieee80211_channel) *
5673 priv->channel_count, GFP_KERNEL);
5674 if (!channels) {
5675 kfree(modes);
5676 return -ENOMEM;
5677 }
5678
5679 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5680 GFP_KERNEL);
5681 if (!rates) {
5682 kfree(modes);
5683 kfree(channels);
5684 return -ENOMEM;
5685 }
5686
5687 /* 0 = 802.11a
5688 * 1 = 802.11b
5689 * 2 = 802.11g
5690 */
5691
5692 /* 5.2GHz channels start after the 2.4GHz channels */
5693 modes[A].mode = MODE_IEEE80211A;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005694 modes[A].channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
Zhu Yib481de92007-09-25 17:54:57 -07005695 modes[A].rates = rates;
5696 modes[A].num_rates = 8; /* just OFDM */
5697 modes[A].rates = &rates[4];
5698 modes[A].num_channels = 0;
5699
5700 modes[B].mode = MODE_IEEE80211B;
5701 modes[B].channels = channels;
5702 modes[B].rates = rates;
5703 modes[B].num_rates = 4; /* just CCK */
5704 modes[B].num_channels = 0;
5705
5706 modes[G].mode = MODE_IEEE80211G;
5707 modes[G].channels = channels;
5708 modes[G].rates = rates;
5709 modes[G].num_rates = 12; /* OFDM & CCK */
5710 modes[G].num_channels = 0;
5711
5712 modes[G_11N].mode = MODE_IEEE80211G;
5713 modes[G_11N].channels = channels;
5714 modes[G_11N].num_rates = 13; /* OFDM & CCK */
5715 modes[G_11N].rates = rates;
5716 modes[G_11N].num_channels = 0;
5717
5718 modes[A_11N].mode = MODE_IEEE80211A;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005719 modes[A_11N].channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
Zhu Yib481de92007-09-25 17:54:57 -07005720 modes[A_11N].rates = &rates[4];
5721 modes[A_11N].num_rates = 9; /* just OFDM */
5722 modes[A_11N].num_channels = 0;
5723
5724 priv->ieee_channels = channels;
5725 priv->ieee_rates = rates;
5726
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005727 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005728
5729 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5730 ch = &priv->channel_info[i];
5731
5732 if (!is_channel_valid(ch)) {
5733 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5734 "skipping.\n",
5735 ch->channel, is_channel_a_band(ch) ?
5736 "5.2" : "2.4");
5737 continue;
5738 }
5739
5740 if (is_channel_a_band(ch)) {
5741 geo_ch = &modes[A].channels[modes[A].num_channels++];
5742 modes[A_11N].num_channels++;
5743 } else {
5744 geo_ch = &modes[B].channels[modes[B].num_channels++];
5745 modes[G].num_channels++;
5746 modes[G_11N].num_channels++;
5747 }
5748
5749 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5750 geo_ch->chan = ch->channel;
5751 geo_ch->power_level = ch->max_power_avg;
5752 geo_ch->antenna_max = 0xff;
5753
5754 if (is_channel_valid(ch)) {
5755 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5756 if (ch->flags & EEPROM_CHANNEL_IBSS)
5757 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5758
5759 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5760 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5761
5762 if (ch->flags & EEPROM_CHANNEL_RADAR)
5763 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5764
5765 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5766 priv->max_channel_txpower_limit =
5767 ch->max_power_avg;
5768 }
5769
5770 geo_ch->val = geo_ch->flag;
5771 }
5772
5773 if ((modes[A].num_channels == 0) && priv->is_abg) {
5774 printk(KERN_INFO DRV_NAME
5775 ": Incorrectly detected BG card as ABG. Please send "
5776 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5777 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5778 priv->is_abg = 0;
5779 }
5780
5781 printk(KERN_INFO DRV_NAME
5782 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5783 modes[G].num_channels, modes[A].num_channels);
5784
5785 /*
5786 * NOTE: We register these in preference of order -- the
5787 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5788 * a phymode based on rates or AP capabilities but seems to
5789 * configure it purely on if the channel being configured
5790 * is supported by a mode -- and the first match is taken
5791 */
5792
5793 if (modes[G].num_channels)
5794 ieee80211_register_hwmode(priv->hw, &modes[G]);
5795 if (modes[B].num_channels)
5796 ieee80211_register_hwmode(priv->hw, &modes[B]);
5797 if (modes[A].num_channels)
5798 ieee80211_register_hwmode(priv->hw, &modes[A]);
5799
5800 priv->modes = modes;
5801 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5802
5803 return 0;
5804}
5805
5806/******************************************************************************
5807 *
5808 * uCode download functions
5809 *
5810 ******************************************************************************/
5811
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005812static void iwl4965_dealloc_ucode_pci(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005813{
5814 if (priv->ucode_code.v_addr != NULL) {
5815 pci_free_consistent(priv->pci_dev,
5816 priv->ucode_code.len,
5817 priv->ucode_code.v_addr,
5818 priv->ucode_code.p_addr);
5819 priv->ucode_code.v_addr = NULL;
5820 }
5821 if (priv->ucode_data.v_addr != NULL) {
5822 pci_free_consistent(priv->pci_dev,
5823 priv->ucode_data.len,
5824 priv->ucode_data.v_addr,
5825 priv->ucode_data.p_addr);
5826 priv->ucode_data.v_addr = NULL;
5827 }
5828 if (priv->ucode_data_backup.v_addr != NULL) {
5829 pci_free_consistent(priv->pci_dev,
5830 priv->ucode_data_backup.len,
5831 priv->ucode_data_backup.v_addr,
5832 priv->ucode_data_backup.p_addr);
5833 priv->ucode_data_backup.v_addr = NULL;
5834 }
5835 if (priv->ucode_init.v_addr != NULL) {
5836 pci_free_consistent(priv->pci_dev,
5837 priv->ucode_init.len,
5838 priv->ucode_init.v_addr,
5839 priv->ucode_init.p_addr);
5840 priv->ucode_init.v_addr = NULL;
5841 }
5842 if (priv->ucode_init_data.v_addr != NULL) {
5843 pci_free_consistent(priv->pci_dev,
5844 priv->ucode_init_data.len,
5845 priv->ucode_init_data.v_addr,
5846 priv->ucode_init_data.p_addr);
5847 priv->ucode_init_data.v_addr = NULL;
5848 }
5849 if (priv->ucode_boot.v_addr != NULL) {
5850 pci_free_consistent(priv->pci_dev,
5851 priv->ucode_boot.len,
5852 priv->ucode_boot.v_addr,
5853 priv->ucode_boot.p_addr);
5854 priv->ucode_boot.v_addr = NULL;
5855 }
5856}
5857
5858/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005859 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005860 * looking at all data.
5861 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005862static int iwl4965_verify_inst_full(struct iwl4965_priv *priv, __le32 * image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005863{
5864 u32 val;
5865 u32 save_len = len;
5866 int rc = 0;
5867 u32 errcnt;
5868
5869 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5870
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005871 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005872 if (rc)
5873 return rc;
5874
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005875 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005876
5877 errcnt = 0;
5878 for (; len > 0; len -= sizeof(u32), image++) {
5879 /* read data comes through single port, auto-incr addr */
5880 /* NOTE: Use the debugless read so we don't flood kernel log
5881 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005882 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005883 if (val != le32_to_cpu(*image)) {
5884 IWL_ERROR("uCode INST section is invalid at "
5885 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5886 save_len - len, val, le32_to_cpu(*image));
5887 rc = -EIO;
5888 errcnt++;
5889 if (errcnt >= 20)
5890 break;
5891 }
5892 }
5893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005894 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005895
5896 if (!errcnt)
5897 IWL_DEBUG_INFO
5898 ("ucode image in INSTRUCTION memory is good\n");
5899
5900 return rc;
5901}
5902
5903
5904/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005905 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005906 * using sample data 100 bytes apart. If these sample points are good,
5907 * it's a pretty good bet that everything between them is good, too.
5908 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005909static int iwl4965_verify_inst_sparse(struct iwl4965_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005910{
5911 u32 val;
5912 int rc = 0;
5913 u32 errcnt = 0;
5914 u32 i;
5915
5916 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005918 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005919 if (rc)
5920 return rc;
5921
5922 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5923 /* read data comes through single port, auto-incr addr */
5924 /* NOTE: Use the debugless read so we don't flood kernel log
5925 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005926 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005927 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005928 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005929 if (val != le32_to_cpu(*image)) {
5930#if 0 /* Enable this if you want to see details */
5931 IWL_ERROR("uCode INST section is invalid at "
5932 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5933 i, val, *image);
5934#endif
5935 rc = -EIO;
5936 errcnt++;
5937 if (errcnt >= 3)
5938 break;
5939 }
5940 }
5941
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005942 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005943
5944 return rc;
5945}
5946
5947
5948/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005949 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005950 * and verify its contents
5951 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005952static int iwl4965_verify_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005953{
5954 __le32 *image;
5955 u32 len;
5956 int rc = 0;
5957
5958 /* Try bootstrap */
5959 image = (__le32 *)priv->ucode_boot.v_addr;
5960 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005961 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005962 if (rc == 0) {
5963 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5964 return 0;
5965 }
5966
5967 /* Try initialize */
5968 image = (__le32 *)priv->ucode_init.v_addr;
5969 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005970 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005971 if (rc == 0) {
5972 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5973 return 0;
5974 }
5975
5976 /* Try runtime/protocol */
5977 image = (__le32 *)priv->ucode_code.v_addr;
5978 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005979 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005980 if (rc == 0) {
5981 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5982 return 0;
5983 }
5984
5985 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5986
5987 /* Show first several data entries in instruction SRAM.
5988 * Selection of bootstrap image is arbitrary. */
5989 image = (__le32 *)priv->ucode_boot.v_addr;
5990 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005991 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005992
5993 return rc;
5994}
5995
5996
5997/* check contents of special bootstrap uCode SRAM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005998static int iwl4965_verify_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005999{
6000 __le32 *image = priv->ucode_boot.v_addr;
6001 u32 len = priv->ucode_boot.len;
6002 u32 reg;
6003 u32 val;
6004
6005 IWL_DEBUG_INFO("Begin verify bsm\n");
6006
6007 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006008 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07006009 for (reg = BSM_SRAM_LOWER_BOUND;
6010 reg < BSM_SRAM_LOWER_BOUND + len;
6011 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006012 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07006013 if (val != le32_to_cpu(*image)) {
6014 IWL_ERROR("BSM uCode verification failed at "
6015 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
6016 BSM_SRAM_LOWER_BOUND,
6017 reg - BSM_SRAM_LOWER_BOUND, len,
6018 val, le32_to_cpu(*image));
6019 return -EIO;
6020 }
6021 }
6022
6023 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
6024
6025 return 0;
6026}
6027
6028/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006029 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07006030 *
6031 * BSM operation:
6032 *
6033 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
6034 * in special SRAM that does not power down during RFKILL. When powering back
6035 * up after power-saving sleeps (or during initial uCode load), the BSM loads
6036 * the bootstrap program into the on-board processor, and starts it.
6037 *
6038 * The bootstrap program loads (via DMA) instructions and data for a new
6039 * program from host DRAM locations indicated by the host driver in the
6040 * BSM_DRAM_* registers. Once the new program is loaded, it starts
6041 * automatically.
6042 *
6043 * When initializing the NIC, the host driver points the BSM to the
6044 * "initialize" uCode image. This uCode sets up some internal data, then
6045 * notifies host via "initialize alive" that it is complete.
6046 *
6047 * The host then replaces the BSM_DRAM_* pointer values to point to the
6048 * normal runtime uCode instructions and a backup uCode data cache buffer
6049 * (filled initially with starting data values for the on-board processor),
6050 * then triggers the "initialize" uCode to load and launch the runtime uCode,
6051 * which begins normal operation.
6052 *
6053 * When doing a power-save shutdown, runtime uCode saves data SRAM into
6054 * the backup data cache in DRAM before SRAM is powered down.
6055 *
6056 * When powering back up, the BSM loads the bootstrap program. This reloads
6057 * the runtime uCode instructions and the backup data cache into SRAM,
6058 * and re-launches the runtime uCode from where it left off.
6059 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006060static int iwl4965_load_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006061{
6062 __le32 *image = priv->ucode_boot.v_addr;
6063 u32 len = priv->ucode_boot.len;
6064 dma_addr_t pinst;
6065 dma_addr_t pdata;
6066 u32 inst_len;
6067 u32 data_len;
6068 int rc;
6069 int i;
6070 u32 done;
6071 u32 reg_offset;
6072
6073 IWL_DEBUG_INFO("Begin load bsm\n");
6074
6075 /* make sure bootstrap program is no larger than BSM's SRAM size */
6076 if (len > IWL_MAX_BSM_SIZE)
6077 return -EINVAL;
6078
6079 /* Tell bootstrap uCode where to find the "Initialize" uCode
6080 * in host DRAM ... bits 31:0 for 3945, bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006081 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07006082 * after the "initialize" uCode has run, to point to
6083 * runtime/protocol instructions and backup data cache. */
6084 pinst = priv->ucode_init.p_addr >> 4;
6085 pdata = priv->ucode_init_data.p_addr >> 4;
6086 inst_len = priv->ucode_init.len;
6087 data_len = priv->ucode_init_data.len;
6088
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006089 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006090 if (rc)
6091 return rc;
6092
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006093 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6094 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6095 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
6096 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07006097
6098 /* Fill BSM memory with bootstrap instructions */
6099 for (reg_offset = BSM_SRAM_LOWER_BOUND;
6100 reg_offset < BSM_SRAM_LOWER_BOUND + len;
6101 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006102 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07006103 le32_to_cpu(*image));
6104
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006105 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006106 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006107 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006108 return rc;
6109 }
6110
6111 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006112 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
6113 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006114 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006115 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07006116
6117 /* Load bootstrap code into instruction SRAM now,
6118 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006119 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006120 BSM_WR_CTRL_REG_BIT_START);
6121
6122 /* Wait for load of bootstrap uCode to finish */
6123 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006124 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07006125 if (!(done & BSM_WR_CTRL_REG_BIT_START))
6126 break;
6127 udelay(10);
6128 }
6129 if (i < 100)
6130 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
6131 else {
6132 IWL_ERROR("BSM write did not complete!\n");
6133 return -EIO;
6134 }
6135
6136 /* Enable future boot loads whenever power management unit triggers it
6137 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006138 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006139 BSM_WR_CTRL_REG_BIT_START_EN);
6140
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006141 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006142
6143 return 0;
6144}
6145
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006146static void iwl4965_nic_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006147{
6148 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006149 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006150}
6151
Tomas Winkler90e759d2007-11-29 11:09:41 +08006152static int iwl4965_alloc_fw_desc(struct pci_dev *pci_dev, struct fw_desc *desc)
6153{
6154 desc->v_addr = pci_alloc_consistent(pci_dev, desc->len, &desc->p_addr);
6155 return (desc->v_addr != NULL) ? 0 : -ENOMEM;
6156}
6157
Zhu Yib481de92007-09-25 17:54:57 -07006158/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006159 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07006160 *
6161 * Copy into buffers for card to fetch via bus-mastering
6162 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006163static int iwl4965_read_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006164{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006165 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006166 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006167 const struct firmware *ucode_raw;
6168 const char *name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode";
6169 u8 *src;
6170 size_t len;
6171 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
6172
6173 /* Ask kernel firmware_class module to get the boot firmware off disk.
6174 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006175 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
6176 if (ret < 0) {
6177 IWL_ERROR("%s firmware file req failed: Reason %d\n",
6178 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07006179 goto error;
6180 }
6181
6182 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
6183 name, ucode_raw->size);
6184
6185 /* Make sure that we got at least our header! */
6186 if (ucode_raw->size < sizeof(*ucode)) {
6187 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006188 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006189 goto err_release;
6190 }
6191
6192 /* Data from ucode file: header followed by uCode images */
6193 ucode = (void *)ucode_raw->data;
6194
6195 ver = le32_to_cpu(ucode->ver);
6196 inst_size = le32_to_cpu(ucode->inst_size);
6197 data_size = le32_to_cpu(ucode->data_size);
6198 init_size = le32_to_cpu(ucode->init_size);
6199 init_data_size = le32_to_cpu(ucode->init_data_size);
6200 boot_size = le32_to_cpu(ucode->boot_size);
6201
6202 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
6203 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
6204 inst_size);
6205 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
6206 data_size);
6207 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
6208 init_size);
6209 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
6210 init_data_size);
6211 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
6212 boot_size);
6213
6214 /* Verify size of file vs. image size info in file's header */
6215 if (ucode_raw->size < sizeof(*ucode) +
6216 inst_size + data_size + init_size +
6217 init_data_size + boot_size) {
6218
6219 IWL_DEBUG_INFO("uCode file size %d too small\n",
6220 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08006221 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006222 goto err_release;
6223 }
6224
6225 /* Verify that uCode images will fit in card's SRAM */
6226 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08006227 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
6228 inst_size);
6229 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006230 goto err_release;
6231 }
6232
6233 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08006234 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
6235 data_size);
6236 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006237 goto err_release;
6238 }
6239 if (init_size > IWL_MAX_INST_SIZE) {
6240 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006241 ("uCode init instr len %d too large to fit in\n",
6242 init_size);
6243 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006244 goto err_release;
6245 }
6246 if (init_data_size > IWL_MAX_DATA_SIZE) {
6247 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006248 ("uCode init data len %d too large to fit in\n",
6249 init_data_size);
6250 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006251 goto err_release;
6252 }
6253 if (boot_size > IWL_MAX_BSM_SIZE) {
6254 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006255 ("uCode boot instr len %d too large to fit in\n",
6256 boot_size);
6257 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006258 goto err_release;
6259 }
6260
6261 /* Allocate ucode buffers for card's bus-master loading ... */
6262
6263 /* Runtime instructions and 2 copies of data:
6264 * 1) unmodified from disk
6265 * 2) backup cache for save/restore during power-downs */
6266 priv->ucode_code.len = inst_size;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006267 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07006268
6269 priv->ucode_data.len = data_size;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006270 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07006271
6272 priv->ucode_data_backup.len = data_size;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006273 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07006274
6275 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006276 if (init_size && init_data_size) {
6277 priv->ucode_init.len = init_size;
6278 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07006279
Tomas Winkler90e759d2007-11-29 11:09:41 +08006280 priv->ucode_init_data.len = init_data_size;
6281 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
6282
6283 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
6284 goto err_pci_alloc;
6285 }
Zhu Yib481de92007-09-25 17:54:57 -07006286
6287 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006288 if (boot_size) {
6289 priv->ucode_boot.len = boot_size;
6290 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07006291
Tomas Winkler90e759d2007-11-29 11:09:41 +08006292 if (!priv->ucode_boot.v_addr)
6293 goto err_pci_alloc;
6294 }
Zhu Yib481de92007-09-25 17:54:57 -07006295
6296 /* Copy images into buffers for card's bus-master reads ... */
6297
6298 /* Runtime instructions (first block of data in file) */
6299 src = &ucode->data[0];
6300 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006301 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006302 memcpy(priv->ucode_code.v_addr, src, len);
6303 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6304 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6305
6306 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006307 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07006308 src = &ucode->data[inst_size];
6309 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006310 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006311 memcpy(priv->ucode_data.v_addr, src, len);
6312 memcpy(priv->ucode_data_backup.v_addr, src, len);
6313
6314 /* Initialization instructions (3rd block) */
6315 if (init_size) {
6316 src = &ucode->data[inst_size + data_size];
6317 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006318 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
6319 len);
Zhu Yib481de92007-09-25 17:54:57 -07006320 memcpy(priv->ucode_init.v_addr, src, len);
6321 }
6322
6323 /* Initialization data (4th block) */
6324 if (init_data_size) {
6325 src = &ucode->data[inst_size + data_size + init_size];
6326 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006327 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
6328 len);
Zhu Yib481de92007-09-25 17:54:57 -07006329 memcpy(priv->ucode_init_data.v_addr, src, len);
6330 }
6331
6332 /* Bootstrap instructions (5th block) */
6333 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6334 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006335 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006336 memcpy(priv->ucode_boot.v_addr, src, len);
6337
6338 /* We have our copies now, allow OS release its copies */
6339 release_firmware(ucode_raw);
6340 return 0;
6341
6342 err_pci_alloc:
6343 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006344 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006345 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006346
6347 err_release:
6348 release_firmware(ucode_raw);
6349
6350 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08006351 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006352}
6353
6354
6355/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006356 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07006357 *
6358 * Tell initialization uCode where to find runtime uCode.
6359 *
6360 * BSM registers initially contain pointers to initialization uCode.
6361 * We need to replace them to load runtime uCode inst and data,
6362 * and to save runtime data when powering down.
6363 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006364static int iwl4965_set_ucode_ptrs(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006365{
6366 dma_addr_t pinst;
6367 dma_addr_t pdata;
6368 int rc = 0;
6369 unsigned long flags;
6370
6371 /* bits 35:4 for 4965 */
6372 pinst = priv->ucode_code.p_addr >> 4;
6373 pdata = priv->ucode_data_backup.p_addr >> 4;
6374
6375 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006376 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006377 if (rc) {
6378 spin_unlock_irqrestore(&priv->lock, flags);
6379 return rc;
6380 }
6381
6382 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006383 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6384 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6385 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006386 priv->ucode_data.len);
6387
6388 /* Inst bytecount must be last to set up, bit 31 signals uCode
6389 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006390 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006391 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6392
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006393 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006394
6395 spin_unlock_irqrestore(&priv->lock, flags);
6396
6397 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6398
6399 return rc;
6400}
6401
6402/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006403 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006404 *
6405 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6406 *
6407 * The 4965 "initialize" ALIVE reply contains calibration data for:
6408 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6409 * (3945 does not contain this data).
6410 *
6411 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6412*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006413static void iwl4965_init_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006414{
6415 /* Check alive response for "valid" sign from uCode */
6416 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6417 /* We had an error bringing up the hardware, so take it
6418 * all the way back down so we can try again */
6419 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6420 goto restart;
6421 }
6422
6423 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6424 * This is a paranoid check, because we would not have gotten the
6425 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006426 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006427 /* Runtime instruction load was bad;
6428 * take it all the way back down so we can try again */
6429 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6430 goto restart;
6431 }
6432
6433 /* Calculate temperature */
6434 priv->temperature = iwl4965_get_temperature(priv);
6435
6436 /* Send pointers to protocol/runtime uCode image ... init code will
6437 * load and launch runtime uCode, which will send us another "Alive"
6438 * notification. */
6439 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006440 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006441 /* Runtime instruction load won't happen;
6442 * take it all the way back down so we can try again */
6443 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6444 goto restart;
6445 }
6446 return;
6447
6448 restart:
6449 queue_work(priv->workqueue, &priv->restart);
6450}
6451
6452
6453/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006454 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006455 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006456 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07006457 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006458static void iwl4965_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006459{
6460 int rc = 0;
6461
6462 IWL_DEBUG_INFO("Runtime Alive received.\n");
6463
6464 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6465 /* We had an error bringing up the hardware, so take it
6466 * all the way back down so we can try again */
6467 IWL_DEBUG_INFO("Alive failed.\n");
6468 goto restart;
6469 }
6470
6471 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6472 * This is a paranoid check, because we would not have gotten the
6473 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006474 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006475 /* Runtime instruction load was bad;
6476 * take it all the way back down so we can try again */
6477 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6478 goto restart;
6479 }
6480
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006481 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006482
6483 rc = iwl4965_alive_notify(priv);
6484 if (rc) {
6485 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6486 rc);
6487 goto restart;
6488 }
6489
6490 /* After the ALIVE response, we can process host commands */
6491 set_bit(STATUS_ALIVE, &priv->status);
6492
6493 /* Clear out the uCode error bit if it is set */
6494 clear_bit(STATUS_FW_ERROR, &priv->status);
6495
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006496 rc = iwl4965_init_channel_map(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006497 if (rc) {
6498 IWL_ERROR("initializing regulatory failed: %d\n", rc);
6499 return;
6500 }
6501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006502 iwl4965_init_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006503
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006504 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006505 return;
6506
6507 if (!priv->mac80211_registered) {
6508 /* Unlock so any user space entry points can call back into
6509 * the driver without a deadlock... */
6510 mutex_unlock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006511 iwl4965_rate_control_register(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006512 rc = ieee80211_register_hw(priv->hw);
6513 priv->hw->conf.beacon_int = 100;
6514 mutex_lock(&priv->mutex);
6515
6516 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006517 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006518 IWL_ERROR("Failed to register network "
6519 "device (error %d)\n", rc);
6520 return;
6521 }
6522
6523 priv->mac80211_registered = 1;
6524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006525 iwl4965_reset_channel_flag(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006526 } else
6527 ieee80211_start_queues(priv->hw);
6528
6529 priv->active_rate = priv->rates_mask;
6530 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6531
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006532 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07006533
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006534 if (iwl4965_is_associated(priv)) {
6535 struct iwl4965_rxon_cmd *active_rxon =
6536 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07006537
6538 memcpy(&priv->staging_rxon, &priv->active_rxon,
6539 sizeof(priv->staging_rxon));
6540 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6541 } else {
6542 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006543 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006544 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6545 }
6546
6547 /* Configure BT coexistence */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006548 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006549
6550 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006551 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006552
6553 /* At this point, the NIC is initialized and operational */
6554 priv->notif_missed_beacons = 0;
6555 set_bit(STATUS_READY, &priv->status);
6556
6557 iwl4965_rf_kill_ct_config(priv);
6558 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6559
6560 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006561 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006562
6563 return;
6564
6565 restart:
6566 queue_work(priv->workqueue, &priv->restart);
6567}
6568
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006569static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07006570
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006571static void __iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006572{
6573 unsigned long flags;
6574 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6575 struct ieee80211_conf *conf = NULL;
6576
6577 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6578
6579 conf = ieee80211_get_hw_conf(priv->hw);
6580
6581 if (!exit_pending)
6582 set_bit(STATUS_EXIT_PENDING, &priv->status);
6583
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006584 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006585
6586 /* Unblock any waiting calls */
6587 wake_up_interruptible_all(&priv->wait_command_queue);
6588
Zhu Yib481de92007-09-25 17:54:57 -07006589 /* Wipe out the EXIT_PENDING status bit if we are not actually
6590 * exiting the module */
6591 if (!exit_pending)
6592 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6593
6594 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006595 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07006596
6597 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006598 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006599
6600 if (priv->mac80211_registered)
6601 ieee80211_stop_queues(priv->hw);
6602
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006603 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07006604 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006605 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006606 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6607 STATUS_RF_KILL_HW |
6608 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6609 STATUS_RF_KILL_SW |
6610 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6611 STATUS_IN_SUSPEND;
6612 goto exit;
6613 }
6614
6615 /* ...otherwise clear out all the status bits but the RF Kill and
6616 * SUSPEND bits and continue taking the NIC down. */
6617 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6618 STATUS_RF_KILL_HW |
6619 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6620 STATUS_RF_KILL_SW |
6621 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6622 STATUS_IN_SUSPEND |
6623 test_bit(STATUS_FW_ERROR, &priv->status) <<
6624 STATUS_FW_ERROR;
6625
6626 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006627 iwl4965_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07006628 spin_unlock_irqrestore(&priv->lock, flags);
6629
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006630 iwl4965_hw_txq_ctx_stop(priv);
6631 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006632
6633 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006634 if (!iwl4965_grab_nic_access(priv)) {
6635 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006636 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006637 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006638 }
6639 spin_unlock_irqrestore(&priv->lock, flags);
6640
6641 udelay(5);
6642
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006643 iwl4965_hw_nic_stop_master(priv);
6644 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6645 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006646
6647 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006648 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07006649
6650 if (priv->ibss_beacon)
6651 dev_kfree_skb(priv->ibss_beacon);
6652 priv->ibss_beacon = NULL;
6653
6654 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006655 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006656}
6657
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006658static void iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006659{
6660 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006661 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006662 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006663
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006664 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006665}
6666
6667#define MAX_HW_RESTARTS 5
6668
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006669static int __iwl4965_up(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006670{
Joe Perches0795af52007-10-03 17:59:30 -07006671 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006672 int rc, i;
6673 u32 hw_rf_kill = 0;
6674
6675 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6676 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6677 return -EIO;
6678 }
6679
6680 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6681 IWL_WARNING("Radio disabled by SW RF kill (module "
6682 "parameter)\n");
6683 return 0;
6684 }
6685
Reinette Chatrea781cf92008-01-21 10:08:31 -08006686 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6687 IWL_ERROR("ucode not available for device bringup\n");
6688 return -EIO;
6689 }
6690
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006691 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006692
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006693 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006694 if (rc) {
6695 IWL_ERROR("Unable to int nic\n");
6696 return rc;
6697 }
6698
6699 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006700 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6701 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006702 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6703
6704 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006705 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6706 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006707
6708 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006709 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6710 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006711
6712 /* Copy original ucode data image from disk into backup cache.
6713 * This will be used to initialize the on-board processor's
6714 * data SRAM for a clean start when the runtime program first loads. */
6715 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6716 priv->ucode_data.len);
6717
6718 /* If platform's RF_KILL switch is set to KILL,
6719 * wait for BIT_INT_RF_KILL interrupt before loading uCode
6720 * and getting things started */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006721 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07006722 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
6723 hw_rf_kill = 1;
6724
6725 if (test_bit(STATUS_RF_KILL_HW, &priv->status) || hw_rf_kill) {
6726 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6727 return 0;
6728 }
6729
6730 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6731
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006732 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006733
6734 /* load bootstrap state machine,
6735 * load bootstrap program into processor's memory,
6736 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006737 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006738
6739 if (rc) {
6740 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6741 continue;
6742 }
6743
6744 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006745 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006746
6747 /* MAC Address location in EEPROM same for 3945/4965 */
6748 get_eeprom_mac(priv, priv->mac_addr);
Joe Perches0795af52007-10-03 17:59:30 -07006749 IWL_DEBUG_INFO("MAC address: %s\n",
6750 print_mac(mac, priv->mac_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006751
6752 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6753
6754 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6755
6756 return 0;
6757 }
6758
6759 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006760 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006761
6762 /* tried to restart and config the device for as long as our
6763 * patience could withstand */
6764 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6765 return -EIO;
6766}
6767
6768
6769/*****************************************************************************
6770 *
6771 * Workqueue callbacks
6772 *
6773 *****************************************************************************/
6774
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006775static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006776{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006777 struct iwl4965_priv *priv =
6778 container_of(data, struct iwl4965_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006779
6780 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6781 return;
6782
6783 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006784 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006785 mutex_unlock(&priv->mutex);
6786}
6787
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006788static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006789{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006790 struct iwl4965_priv *priv =
6791 container_of(data, struct iwl4965_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006792
6793 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6794 return;
6795
6796 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006797 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006798 mutex_unlock(&priv->mutex);
6799}
6800
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006801static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006802{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006803 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006804
6805 wake_up_interruptible(&priv->wait_command_queue);
6806
6807 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6808 return;
6809
6810 mutex_lock(&priv->mutex);
6811
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006812 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006813 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6814 "HW and/or SW RF Kill no longer active, restarting "
6815 "device\n");
6816 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6817 queue_work(priv->workqueue, &priv->restart);
6818 } else {
6819
6820 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6821 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6822 "disabled by SW switch\n");
6823 else
6824 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6825 "Kill switch must be turned off for "
6826 "wireless networking to work.\n");
6827 }
6828 mutex_unlock(&priv->mutex);
6829}
6830
6831#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006833static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006834{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006835 struct iwl4965_priv *priv =
6836 container_of(data, struct iwl4965_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006837
6838 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6839 return;
6840
6841 mutex_lock(&priv->mutex);
6842 if (test_bit(STATUS_SCANNING, &priv->status) ||
6843 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6844 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6845 "Scan completion watchdog resetting adapter (%dms)\n",
6846 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006847
Zhu Yib481de92007-09-25 17:54:57 -07006848 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006849 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006850 }
6851 mutex_unlock(&priv->mutex);
6852}
6853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006854static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006855{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006856 struct iwl4965_priv *priv =
6857 container_of(data, struct iwl4965_priv, request_scan);
6858 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006859 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006860 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006861 .meta.flags = CMD_SIZE_HUGE,
6862 };
6863 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006864 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006865 struct ieee80211_conf *conf = NULL;
6866 u8 direct_mask;
6867 int phymode;
6868
6869 conf = ieee80211_get_hw_conf(priv->hw);
6870
6871 mutex_lock(&priv->mutex);
6872
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006873 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006874 IWL_WARNING("request scan called when driver not ready.\n");
6875 goto done;
6876 }
6877
6878 /* Make sure the scan wasn't cancelled before this queued work
6879 * was given the chance to run... */
6880 if (!test_bit(STATUS_SCANNING, &priv->status))
6881 goto done;
6882
6883 /* This should never be called or scheduled if there is currently
6884 * a scan active in the hardware. */
6885 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6886 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6887 "Ignoring second request.\n");
6888 rc = -EIO;
6889 goto done;
6890 }
6891
6892 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6893 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6894 goto done;
6895 }
6896
6897 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6898 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6899 goto done;
6900 }
6901
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006902 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006903 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6904 goto done;
6905 }
6906
6907 if (!test_bit(STATUS_READY, &priv->status)) {
6908 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6909 goto done;
6910 }
6911
6912 if (!priv->scan_bands) {
6913 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6914 goto done;
6915 }
6916
6917 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006918 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006919 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6920 if (!priv->scan) {
6921 rc = -ENOMEM;
6922 goto done;
6923 }
6924 }
6925 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006926 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006927
6928 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6929 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6930
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006931 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006932 u16 interval = 0;
6933 u32 extra;
6934 u32 suspend_time = 100;
6935 u32 scan_suspend_time = 100;
6936 unsigned long flags;
6937
6938 IWL_DEBUG_INFO("Scanning while associated...\n");
6939
6940 spin_lock_irqsave(&priv->lock, flags);
6941 interval = priv->beacon_int;
6942 spin_unlock_irqrestore(&priv->lock, flags);
6943
6944 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006945 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006946 if (!interval)
6947 interval = suspend_time;
6948
6949 extra = (suspend_time / interval) << 22;
6950 scan_suspend_time = (extra |
6951 ((suspend_time % interval) * 1024));
6952 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6953 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6954 scan_suspend_time, interval);
6955 }
6956
6957 /* We should add the ability for user to lock to PASSIVE ONLY */
6958 if (priv->one_direct_scan) {
6959 IWL_DEBUG_SCAN
6960 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006961 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006962 priv->direct_ssid_len));
6963 scan->direct_scan[0].id = WLAN_EID_SSID;
6964 scan->direct_scan[0].len = priv->direct_ssid_len;
6965 memcpy(scan->direct_scan[0].ssid,
6966 priv->direct_ssid, priv->direct_ssid_len);
6967 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006968 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006969 scan->direct_scan[0].id = WLAN_EID_SSID;
6970 scan->direct_scan[0].len = priv->essid_len;
6971 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6972 direct_mask = 1;
6973 } else
6974 direct_mask = 0;
6975
6976 /* We don't build a direct scan probe request; the uCode will do
6977 * that based on the direct_mask added to each channel entry */
6978 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006979 iwl4965_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Zhu Yib481de92007-09-25 17:54:57 -07006980 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
6981 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6982 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6983 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6984
6985 /* flags + rate selection */
6986
6987 scan->tx_cmd.tx_flags |= cpu_to_le32(0x200);
6988
6989 switch (priv->scan_bands) {
6990 case 2:
6991 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6992 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006993 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006994 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6995
6996 scan->good_CRC_th = 0;
6997 phymode = MODE_IEEE80211G;
6998 break;
6999
7000 case 1:
7001 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007002 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07007003 RATE_MCS_ANT_B_MSK);
7004 scan->good_CRC_th = IWL_GOOD_CRC_TH;
7005 phymode = MODE_IEEE80211A;
7006 break;
7007
7008 default:
7009 IWL_WARNING("Invalid scan band count\n");
7010 goto done;
7011 }
7012
7013 /* select Rx chains */
7014
7015 /* Force use of chains B and C (0x6) for scan Rx.
7016 * Avoid A (0x1) because of its off-channel reception on A-band.
7017 * MIMO is not used here, but value is required to make uCode happy. */
7018 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
7019 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
7020 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
7021 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
7022
7023 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
7024 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
7025
7026 if (direct_mask)
7027 IWL_DEBUG_SCAN
7028 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007029 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07007030 else
7031 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
7032
7033 scan->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007034 iwl4965_get_channels_for_scan(
Zhu Yib481de92007-09-25 17:54:57 -07007035 priv, phymode, 1, /* active */
7036 direct_mask,
7037 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
7038
7039 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007040 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007041 cmd.data = scan;
7042 scan->len = cpu_to_le16(cmd.len);
7043
7044 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007045 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07007046 if (rc)
7047 goto done;
7048
7049 queue_delayed_work(priv->workqueue, &priv->scan_check,
7050 IWL_SCAN_CHECK_WATCHDOG);
7051
7052 mutex_unlock(&priv->mutex);
7053 return;
7054
7055 done:
Ian Schram01ebd062007-10-25 17:15:22 +08007056 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07007057 queue_work(priv->workqueue, &priv->scan_completed);
7058 mutex_unlock(&priv->mutex);
7059}
7060
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007061static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007062{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007063 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07007064
7065 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7066 return;
7067
7068 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007069 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007070 mutex_unlock(&priv->mutex);
7071}
7072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007073static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007074{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007075 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07007076
7077 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7078 return;
7079
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007080 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007081 queue_work(priv->workqueue, &priv->up);
7082}
7083
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007084static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007085{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007086 struct iwl4965_priv *priv =
7087 container_of(data, struct iwl4965_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07007088
7089 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7090 return;
7091
7092 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007093 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007094 mutex_unlock(&priv->mutex);
7095}
7096
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007097static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007098{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007099 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv,
Zhu Yib481de92007-09-25 17:54:57 -07007100 post_associate.work);
7101
7102 int rc = 0;
7103 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07007104 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007105
7106 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7107 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
7108 return;
7109 }
7110
Joe Perches0795af52007-10-03 17:59:30 -07007111 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
7112 priv->assoc_id,
7113 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07007114
7115
7116 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7117 return;
7118
7119 mutex_lock(&priv->mutex);
7120
Mohamed Abbas948c1712007-10-25 17:15:45 +08007121 if (!priv->interface_id || !priv->is_open) {
7122 mutex_unlock(&priv->mutex);
7123 return;
7124 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007125 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08007126
Zhu Yib481de92007-09-25 17:54:57 -07007127 conf = ieee80211_get_hw_conf(priv->hw);
7128
7129 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007130 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007131
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007132 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7133 iwl4965_setup_rxon_timing(priv);
7134 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007135 sizeof(priv->rxon_timing), &priv->rxon_timing);
7136 if (rc)
7137 IWL_WARNING("REPLY_RXON_TIMING failed - "
7138 "Attempting to continue.\n");
7139
7140 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7141
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007142#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007143 if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
7144 iwl4965_set_rxon_ht(priv, &priv->current_assoc_ht);
7145 else {
7146 priv->active_rate_ht[0] = 0;
7147 priv->active_rate_ht[1] = 0;
7148 priv->current_channel_width = IWL_CHANNEL_WIDTH_20MHZ;
7149 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007150#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007151 iwl4965_set_rxon_chain(priv);
7152 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7153
7154 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
7155 priv->assoc_id, priv->beacon_int);
7156
7157 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7158 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7159 else
7160 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7161
7162 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7163 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
7164 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
7165 else
7166 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7167
7168 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7169 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7170
7171 }
7172
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007173 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007174
7175 switch (priv->iw_mode) {
7176 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007177 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007178 break;
7179
7180 case IEEE80211_IF_TYPE_IBSS:
7181
7182 /* clear out the station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007183 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007185 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
7186 iwl4965_rxon_add_station(priv, priv->bssid, 0);
7187 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
7188 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007189
7190 break;
7191
7192 default:
7193 IWL_ERROR("%s Should not be called in %d mode\n",
7194 __FUNCTION__, priv->iw_mode);
7195 break;
7196 }
7197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007198 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007199
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007200#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07007201 /* Enable Rx differential gain and sensitivity calibrations */
7202 iwl4965_chain_noise_reset(priv);
7203 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007204#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07007205
7206 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7207 priv->assoc_station_added = 1;
7208
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007209#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007210 iwl4965_activate_qos(priv, 0);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007211#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07007212 mutex_unlock(&priv->mutex);
7213}
7214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007215static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07007216{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007217 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07007218
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007219 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007220 return;
7221
7222 mutex_lock(&priv->mutex);
7223
7224 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007225 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007226
7227 mutex_unlock(&priv->mutex);
7228}
7229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007230static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07007231{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007232 struct iwl4965_priv *priv =
7233 container_of(work, struct iwl4965_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07007234
7235 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
7236
7237 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7238 return;
7239
7240 ieee80211_scan_completed(priv->hw);
7241
7242 /* Since setting the TXPOWER may have been deferred while
7243 * performing the scan, fire one off */
7244 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007245 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007246 mutex_unlock(&priv->mutex);
7247}
7248
7249/*****************************************************************************
7250 *
7251 * mac80211 entry point functions
7252 *
7253 *****************************************************************************/
7254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007255static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007256{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007257 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007258
7259 IWL_DEBUG_MAC80211("enter\n");
7260
7261 /* we should be verifying the device is ready to be opened */
7262 mutex_lock(&priv->mutex);
7263
7264 priv->is_open = 1;
7265
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007266 if (!iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007267 ieee80211_start_queues(priv->hw);
7268
7269 mutex_unlock(&priv->mutex);
7270 IWL_DEBUG_MAC80211("leave\n");
7271 return 0;
7272}
7273
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007274static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007275{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007276 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007277
7278 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08007279
7280
7281 mutex_lock(&priv->mutex);
7282 /* stop mac, cancel any scan request and clear
7283 * RXON_FILTER_ASSOC_MSK BIT
7284 */
Zhu Yib481de92007-09-25 17:54:57 -07007285 priv->is_open = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007286 iwl4965_scan_cancel_timeout(priv, 100);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007287 cancel_delayed_work(&priv->post_associate);
7288 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007289 iwl4965_commit_rxon(priv);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007290 mutex_unlock(&priv->mutex);
7291
Zhu Yib481de92007-09-25 17:54:57 -07007292 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007293}
7294
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007295static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007296 struct ieee80211_tx_control *ctl)
7297{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007298 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007299
7300 IWL_DEBUG_MAC80211("enter\n");
7301
7302 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7303 IWL_DEBUG_MAC80211("leave - monitor\n");
7304 return -1;
7305 }
7306
7307 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
7308 ctl->tx_rate);
7309
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007310 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07007311 dev_kfree_skb_any(skb);
7312
7313 IWL_DEBUG_MAC80211("leave\n");
7314 return 0;
7315}
7316
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007317static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007318 struct ieee80211_if_init_conf *conf)
7319{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007320 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007321 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07007322 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007323
7324 IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007325
7326 if (priv->interface_id) {
7327 IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
7328 return 0;
7329 }
7330
7331 spin_lock_irqsave(&priv->lock, flags);
7332 priv->interface_id = conf->if_id;
7333
7334 spin_unlock_irqrestore(&priv->lock, flags);
7335
7336 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02007337
7338 if (conf->mac_addr) {
7339 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7340 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7341 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007342 iwl4965_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007343
7344 IWL_DEBUG_MAC80211("leave\n");
7345 mutex_unlock(&priv->mutex);
7346
7347 return 0;
7348}
7349
7350/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007351 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07007352 *
7353 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7354 * be set inappropriately and the driver currently sets the hardware up to
7355 * use it whenever needed.
7356 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007357static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007358{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007359 struct iwl4965_priv *priv = hw->priv;
7360 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07007361 unsigned long flags;
7362
7363 mutex_lock(&priv->mutex);
7364 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
7365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007366 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007367 IWL_DEBUG_MAC80211("leave - not ready\n");
7368 mutex_unlock(&priv->mutex);
7369 return -EIO;
7370 }
7371
7372 /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
Ian Schram01ebd062007-10-25 17:15:22 +08007373 * what is exposed through include/ declarations */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007374 if (unlikely(!iwl4965_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07007375 test_bit(STATUS_SCANNING, &priv->status))) {
7376 IWL_DEBUG_MAC80211("leave - scanning\n");
7377 mutex_unlock(&priv->mutex);
7378 return 0;
7379 }
7380
7381 spin_lock_irqsave(&priv->lock, flags);
7382
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007383 ch_info = iwl4965_get_channel_info(priv, conf->phymode, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07007384 if (!is_channel_valid(ch_info)) {
7385 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
7386 conf->channel, conf->phymode);
7387 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7388 spin_unlock_irqrestore(&priv->lock, flags);
7389 mutex_unlock(&priv->mutex);
7390 return -EINVAL;
7391 }
7392
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007393#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007394 /* if we are switching fron ht to 2.4 clear flags
7395 * from any ht related info since 2.4 does not
7396 * support ht */
7397 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel)
7398#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7399 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7400#endif
7401 )
7402 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007403#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007405 iwl4965_set_rxon_channel(priv, conf->phymode, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07007406
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007407 iwl4965_set_flags_for_phymode(priv, conf->phymode);
Zhu Yib481de92007-09-25 17:54:57 -07007408
7409 /* The list of supported rates and rate mask can be different
7410 * for each phymode; since the phymode may have changed, reset
7411 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007412 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007413
7414 spin_unlock_irqrestore(&priv->lock, flags);
7415
7416#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7417 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007418 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07007419 mutex_unlock(&priv->mutex);
7420 return 0;
7421 }
7422#endif
7423
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007424 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07007425
7426 if (!conf->radio_enabled) {
7427 IWL_DEBUG_MAC80211("leave - radio disabled\n");
7428 mutex_unlock(&priv->mutex);
7429 return 0;
7430 }
7431
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007432 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007433 IWL_DEBUG_MAC80211("leave - RF kill\n");
7434 mutex_unlock(&priv->mutex);
7435 return -EIO;
7436 }
7437
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007438 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007439
7440 if (memcmp(&priv->active_rxon,
7441 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007442 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007443 else
7444 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7445
7446 IWL_DEBUG_MAC80211("leave\n");
7447
7448 mutex_unlock(&priv->mutex);
7449
7450 return 0;
7451}
7452
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007453static void iwl4965_config_ap(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007454{
7455 int rc = 0;
7456
7457 if (priv->status & STATUS_EXIT_PENDING)
7458 return;
7459
7460 /* The following should be done only at AP bring up */
7461 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7462
7463 /* RXON - unassoc (to set timing command) */
7464 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007465 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007466
7467 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007468 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7469 iwl4965_setup_rxon_timing(priv);
7470 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007471 sizeof(priv->rxon_timing), &priv->rxon_timing);
7472 if (rc)
7473 IWL_WARNING("REPLY_RXON_TIMING failed - "
7474 "Attempting to continue.\n");
7475
7476 iwl4965_set_rxon_chain(priv);
7477
7478 /* FIXME: what should be the assoc_id for AP? */
7479 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7480 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7481 priv->staging_rxon.flags |=
7482 RXON_FLG_SHORT_PREAMBLE_MSK;
7483 else
7484 priv->staging_rxon.flags &=
7485 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7486
7487 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7488 if (priv->assoc_capability &
7489 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7490 priv->staging_rxon.flags |=
7491 RXON_FLG_SHORT_SLOT_MSK;
7492 else
7493 priv->staging_rxon.flags &=
7494 ~RXON_FLG_SHORT_SLOT_MSK;
7495
7496 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7497 priv->staging_rxon.flags &=
7498 ~RXON_FLG_SHORT_SLOT_MSK;
7499 }
7500 /* restore RXON assoc */
7501 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007502 iwl4965_commit_rxon(priv);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007503#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007504 iwl4965_activate_qos(priv, 1);
Zhu Yib481de92007-09-25 17:54:57 -07007505#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007506 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08007507 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007508 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007509
7510 /* FIXME - we need to add code here to detect a totally new
7511 * configuration, reset the AP, unassoc, rxon timing, assoc,
7512 * clear sta table, add BCAST sta... */
7513}
7514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007515static int iwl4965_mac_config_interface(struct ieee80211_hw *hw, int if_id,
Zhu Yib481de92007-09-25 17:54:57 -07007516 struct ieee80211_if_conf *conf)
7517{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007518 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007519 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007520 unsigned long flags;
7521 int rc;
7522
7523 if (conf == NULL)
7524 return -EIO;
7525
7526 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7527 (!conf->beacon || !conf->ssid_len)) {
7528 IWL_DEBUG_MAC80211
7529 ("Leaving in AP mode because HostAPD is not ready.\n");
7530 return 0;
7531 }
7532
7533 mutex_lock(&priv->mutex);
7534
7535 IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
7536 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007537 IWL_DEBUG_MAC80211("bssid: %s\n",
7538 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007539
Johannes Berg4150c572007-09-17 01:29:23 -04007540/*
7541 * very dubious code was here; the probe filtering flag is never set:
7542 *
Zhu Yib481de92007-09-25 17:54:57 -07007543 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7544 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007545 */
7546 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yib481de92007-09-25 17:54:57 -07007547 IWL_DEBUG_MAC80211("leave - scanning\n");
7548 mutex_unlock(&priv->mutex);
7549 return 0;
7550 }
7551
7552 if (priv->interface_id != if_id) {
7553 IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
7554 mutex_unlock(&priv->mutex);
7555 return 0;
7556 }
7557
7558 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7559 if (!conf->bssid) {
7560 conf->bssid = priv->mac_addr;
7561 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007562 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7563 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007564 }
7565 if (priv->ibss_beacon)
7566 dev_kfree_skb(priv->ibss_beacon);
7567
7568 priv->ibss_beacon = conf->beacon;
7569 }
7570
7571 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7572 !is_multicast_ether_addr(conf->bssid)) {
7573 /* If there is currently a HW scan going on in the background
7574 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007575 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007576 IWL_WARNING("Aborted scan still in progress "
7577 "after 100ms\n");
7578 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7579 mutex_unlock(&priv->mutex);
7580 return -EAGAIN;
7581 }
7582 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7583
7584 /* TODO: Audit driver for usage of these members and see
7585 * if mac80211 deprecates them (priv->bssid looks like it
7586 * shouldn't be there, but I haven't scanned the IBSS code
7587 * to verify) - jpk */
7588 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7589
7590 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007591 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007592 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007593 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007594 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007595 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07007596 priv, priv->active_rxon.bssid_addr, 1);
7597 }
7598
7599 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007600 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007601 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007602 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007603 }
7604
7605 spin_lock_irqsave(&priv->lock, flags);
7606 if (!conf->ssid_len)
7607 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7608 else
7609 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7610
7611 priv->essid_len = conf->ssid_len;
7612 spin_unlock_irqrestore(&priv->lock, flags);
7613
7614 IWL_DEBUG_MAC80211("leave\n");
7615 mutex_unlock(&priv->mutex);
7616
7617 return 0;
7618}
7619
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007620static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007621 unsigned int changed_flags,
7622 unsigned int *total_flags,
7623 int mc_count, struct dev_addr_list *mc_list)
7624{
7625 /*
7626 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007627 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007628 */
7629 *total_flags = 0;
7630}
7631
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007632static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007633 struct ieee80211_if_init_conf *conf)
7634{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007635 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007636
7637 IWL_DEBUG_MAC80211("enter\n");
7638
7639 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007640
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007641 iwl4965_scan_cancel_timeout(priv, 100);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007642 cancel_delayed_work(&priv->post_associate);
7643 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007644 iwl4965_commit_rxon(priv);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007645
Zhu Yib481de92007-09-25 17:54:57 -07007646 if (priv->interface_id == conf->if_id) {
7647 priv->interface_id = 0;
7648 memset(priv->bssid, 0, ETH_ALEN);
7649 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7650 priv->essid_len = 0;
7651 }
7652 mutex_unlock(&priv->mutex);
7653
7654 IWL_DEBUG_MAC80211("leave\n");
7655
7656}
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007657static void iwl4965_mac_erp_ie_changed(struct ieee80211_hw *hw,
Tomas Winkler220173b2007-10-16 00:50:25 +02007658 u8 changes, int cts_protection, int preamble)
7659{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007660 struct iwl4965_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02007661
7662 if (changes & IEEE80211_ERP_CHANGE_PREAMBLE) {
7663 if (preamble == WLAN_ERP_PREAMBLE_SHORT)
7664 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7665 else
7666 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7667 }
7668
7669 if (changes & IEEE80211_ERP_CHANGE_PROTECTION) {
Zhu Yi797a54c2007-11-12 11:37:43 +08007670 if (cts_protection && (priv->phymode != MODE_IEEE80211A))
Tomas Winkler220173b2007-10-16 00:50:25 +02007671 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7672 else
7673 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7674 }
7675
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007676 if (iwl4965_is_associated(priv))
7677 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02007678}
Zhu Yib481de92007-09-25 17:54:57 -07007679
7680#define IWL_DELAY_NEXT_SCAN (HZ*2)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007681static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007682{
7683 int rc = 0;
7684 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007685 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007686
7687 IWL_DEBUG_MAC80211("enter\n");
7688
mabbas052c4b92007-10-25 17:15:43 +08007689 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007690 spin_lock_irqsave(&priv->lock, flags);
7691
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007692 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007693 rc = -EIO;
7694 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7695 goto out_unlock;
7696 }
7697
7698 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7699 rc = -EIO;
7700 IWL_ERROR("ERROR: APs don't scan\n");
7701 goto out_unlock;
7702 }
7703
7704 /* if we just finished scan ask for delay */
7705 if (priv->last_scan_jiffies &&
7706 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
7707 jiffies)) {
7708 rc = -EAGAIN;
7709 goto out_unlock;
7710 }
7711 if (len) {
7712 IWL_DEBUG_SCAN("direct scan for "
7713 "%s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007714 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007715
7716 priv->one_direct_scan = 1;
7717 priv->direct_ssid_len = (u8)
7718 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7719 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007720 } else
7721 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007722
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007723 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007724
7725 IWL_DEBUG_MAC80211("leave\n");
7726
7727out_unlock:
7728 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007729 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007730
7731 return rc;
7732}
7733
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007734static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007735 const u8 *local_addr, const u8 *addr,
7736 struct ieee80211_key_conf *key)
7737{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007738 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007739 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007740 int rc = 0;
7741 u8 sta_id;
7742
7743 IWL_DEBUG_MAC80211("enter\n");
7744
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007745 if (!iwl4965_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007746 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7747 return -EOPNOTSUPP;
7748 }
7749
7750 if (is_zero_ether_addr(addr))
7751 /* only support pairwise keys */
7752 return -EOPNOTSUPP;
7753
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007754 sta_id = iwl4965_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07007755 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007756 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7757 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007758 return -EINVAL;
7759 }
7760
7761 mutex_lock(&priv->mutex);
7762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007763 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007764
Zhu Yib481de92007-09-25 17:54:57 -07007765 switch (cmd) {
7766 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007767 rc = iwl4965_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007768 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007769 iwl4965_set_rxon_hwcrypto(priv, 1);
7770 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007771 key->hw_key_idx = sta_id;
7772 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7773 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7774 }
7775 break;
7776 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007777 rc = iwl4965_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007778 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007779 iwl4965_set_rxon_hwcrypto(priv, 0);
7780 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007781 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7782 }
7783 break;
7784 default:
7785 rc = -EINVAL;
7786 }
7787
7788 IWL_DEBUG_MAC80211("leave\n");
7789 mutex_unlock(&priv->mutex);
7790
7791 return rc;
7792}
7793
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007794static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007795 const struct ieee80211_tx_queue_params *params)
7796{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007797 struct iwl4965_priv *priv = hw->priv;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007798#ifdef CONFIG_IWL4965_QOS
Zhu Yib481de92007-09-25 17:54:57 -07007799 unsigned long flags;
7800 int q;
Reinette Chatre0054b342007-11-29 11:09:42 +08007801#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07007802
7803 IWL_DEBUG_MAC80211("enter\n");
7804
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007805 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007806 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7807 return -EIO;
7808 }
7809
7810 if (queue >= AC_NUM) {
7811 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7812 return 0;
7813 }
7814
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007815#ifdef CONFIG_IWL4965_QOS
Zhu Yib481de92007-09-25 17:54:57 -07007816 if (!priv->qos_data.qos_enable) {
7817 priv->qos_data.qos_active = 0;
7818 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7819 return 0;
7820 }
7821 q = AC_NUM - 1 - queue;
7822
7823 spin_lock_irqsave(&priv->lock, flags);
7824
7825 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7826 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7827 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7828 priv->qos_data.def_qos_parm.ac[q].edca_txop =
7829 cpu_to_le16((params->burst_time * 100));
7830
7831 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7832 priv->qos_data.qos_active = 1;
7833
7834 spin_unlock_irqrestore(&priv->lock, flags);
7835
7836 mutex_lock(&priv->mutex);
7837 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007838 iwl4965_activate_qos(priv, 1);
7839 else if (priv->assoc_id && iwl4965_is_associated(priv))
7840 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007841
7842 mutex_unlock(&priv->mutex);
7843
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007844#endif /*CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07007845
7846 IWL_DEBUG_MAC80211("leave\n");
7847 return 0;
7848}
7849
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007850static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007851 struct ieee80211_tx_queue_stats *stats)
7852{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007853 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007854 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007855 struct iwl4965_tx_queue *txq;
7856 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007857 unsigned long flags;
7858
7859 IWL_DEBUG_MAC80211("enter\n");
7860
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007861 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007862 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7863 return -EIO;
7864 }
7865
7866 spin_lock_irqsave(&priv->lock, flags);
7867
7868 for (i = 0; i < AC_NUM; i++) {
7869 txq = &priv->txq[i];
7870 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007871 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007872
7873 stats->data[i].len = q->n_window - avail;
7874 stats->data[i].limit = q->n_window - q->high_mark;
7875 stats->data[i].count = q->n_window;
7876
7877 }
7878 spin_unlock_irqrestore(&priv->lock, flags);
7879
7880 IWL_DEBUG_MAC80211("leave\n");
7881
7882 return 0;
7883}
7884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007885static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007886 struct ieee80211_low_level_stats *stats)
7887{
7888 IWL_DEBUG_MAC80211("enter\n");
7889 IWL_DEBUG_MAC80211("leave\n");
7890
7891 return 0;
7892}
7893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007894static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007895{
7896 IWL_DEBUG_MAC80211("enter\n");
7897 IWL_DEBUG_MAC80211("leave\n");
7898
7899 return 0;
7900}
7901
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007902static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007903{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007904 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007905 unsigned long flags;
7906
7907 mutex_lock(&priv->mutex);
7908 IWL_DEBUG_MAC80211("enter\n");
7909
7910 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007911#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007912 spin_lock_irqsave(&priv->lock, flags);
7913 memset(&priv->current_assoc_ht, 0, sizeof(struct sta_ht_info));
7914 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007915#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07007916/* if (priv->lq_mngr.agg_ctrl.granted_ba)
7917 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);*/
7918
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007919 memset(&(priv->lq_mngr.agg_ctrl), 0, sizeof(struct iwl4965_agg_control));
Zhu Yib481de92007-09-25 17:54:57 -07007920 priv->lq_mngr.agg_ctrl.tid_traffic_load_threshold = 10;
7921 priv->lq_mngr.agg_ctrl.ba_timeout = 5000;
7922 priv->lq_mngr.agg_ctrl.auto_agg = 1;
7923
7924 if (priv->lq_mngr.agg_ctrl.auto_agg)
7925 priv->lq_mngr.agg_ctrl.requested_ba = TID_ALL_ENABLED;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007926#endif /*CONFIG_IWL4965_HT_AGG */
7927#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007928
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007929#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007930 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007931#endif
7932
7933 cancel_delayed_work(&priv->post_associate);
7934
7935 spin_lock_irqsave(&priv->lock, flags);
7936 priv->assoc_id = 0;
7937 priv->assoc_capability = 0;
7938 priv->call_post_assoc_from_beacon = 0;
7939 priv->assoc_station_added = 0;
7940
7941 /* new association get rid of ibss beacon skb */
7942 if (priv->ibss_beacon)
7943 dev_kfree_skb(priv->ibss_beacon);
7944
7945 priv->ibss_beacon = NULL;
7946
7947 priv->beacon_int = priv->hw->conf.beacon_int;
7948 priv->timestamp1 = 0;
7949 priv->timestamp0 = 0;
7950 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7951 priv->beacon_int = 0;
7952
7953 spin_unlock_irqrestore(&priv->lock, flags);
7954
mabbas052c4b92007-10-25 17:15:43 +08007955 /* we are restarting association process
7956 * clear RXON_FILTER_ASSOC_MSK bit
7957 */
7958 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007959 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007960 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007961 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007962 }
7963
Zhu Yib481de92007-09-25 17:54:57 -07007964 /* Per mac80211.h: This is only used in IBSS mode... */
7965 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007966
Zhu Yib481de92007-09-25 17:54:57 -07007967 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7968 mutex_unlock(&priv->mutex);
7969 return;
7970 }
7971
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007972 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007973 IWL_DEBUG_MAC80211("leave - not ready\n");
7974 mutex_unlock(&priv->mutex);
7975 return;
7976 }
7977
7978 priv->only_active_channel = 0;
7979
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007980 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007981
7982 mutex_unlock(&priv->mutex);
7983
7984 IWL_DEBUG_MAC80211("leave\n");
7985
7986}
7987
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007988static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007989 struct ieee80211_tx_control *control)
7990{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007991 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007992 unsigned long flags;
7993
7994 mutex_lock(&priv->mutex);
7995 IWL_DEBUG_MAC80211("enter\n");
7996
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007997 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007998 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7999 mutex_unlock(&priv->mutex);
8000 return -EIO;
8001 }
8002
8003 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
8004 IWL_DEBUG_MAC80211("leave - not IBSS\n");
8005 mutex_unlock(&priv->mutex);
8006 return -EIO;
8007 }
8008
8009 spin_lock_irqsave(&priv->lock, flags);
8010
8011 if (priv->ibss_beacon)
8012 dev_kfree_skb(priv->ibss_beacon);
8013
8014 priv->ibss_beacon = skb;
8015
8016 priv->assoc_id = 0;
8017
8018 IWL_DEBUG_MAC80211("leave\n");
8019 spin_unlock_irqrestore(&priv->lock, flags);
8020
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008021#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008022 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008023#endif
8024
8025 queue_work(priv->workqueue, &priv->post_associate.work);
8026
8027 mutex_unlock(&priv->mutex);
8028
8029 return 0;
8030}
8031
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008032#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07008033union ht_cap_info {
8034 struct {
8035 u16 advanced_coding_cap :1;
8036 u16 supported_chan_width_set :1;
8037 u16 mimo_power_save_mode :2;
8038 u16 green_field :1;
8039 u16 short_GI20 :1;
8040 u16 short_GI40 :1;
8041 u16 tx_stbc :1;
8042 u16 rx_stbc :1;
8043 u16 beam_forming :1;
8044 u16 delayed_ba :1;
8045 u16 maximal_amsdu_size :1;
8046 u16 cck_mode_at_40MHz :1;
8047 u16 psmp_support :1;
8048 u16 stbc_ctrl_frame_support :1;
8049 u16 sig_txop_protection_support :1;
8050 };
8051 u16 val;
8052} __attribute__ ((packed));
8053
8054union ht_param_info{
8055 struct {
8056 u8 max_rx_ampdu_factor :2;
8057 u8 mpdu_density :3;
8058 u8 reserved :3;
8059 };
8060 u8 val;
8061} __attribute__ ((packed));
8062
8063union ht_exra_param_info {
8064 struct {
8065 u8 ext_chan_offset :2;
8066 u8 tx_chan_width :1;
8067 u8 rifs_mode :1;
8068 u8 controlled_access_only :1;
8069 u8 service_interval_granularity :3;
8070 };
8071 u8 val;
8072} __attribute__ ((packed));
8073
8074union ht_operation_mode{
8075 struct {
8076 u16 op_mode :2;
8077 u16 non_GF :1;
8078 u16 reserved :13;
8079 };
8080 u16 val;
8081} __attribute__ ((packed));
8082
8083
8084static int sta_ht_info_init(struct ieee80211_ht_capability *ht_cap,
8085 struct ieee80211_ht_additional_info *ht_extra,
8086 struct sta_ht_info *ht_info_ap,
8087 struct sta_ht_info *ht_info)
8088{
8089 union ht_cap_info cap;
8090 union ht_operation_mode op_mode;
8091 union ht_param_info param_info;
8092 union ht_exra_param_info extra_param_info;
8093
8094 IWL_DEBUG_MAC80211("enter: \n");
8095
8096 if (!ht_info) {
8097 IWL_DEBUG_MAC80211("leave: ht_info is NULL\n");
8098 return -1;
8099 }
8100
8101 if (ht_cap) {
8102 cap.val = (u16) le16_to_cpu(ht_cap->capabilities_info);
8103 param_info.val = ht_cap->mac_ht_params_info;
8104 ht_info->is_ht = 1;
8105 if (cap.short_GI20)
8106 ht_info->sgf |= 0x1;
8107 if (cap.short_GI40)
8108 ht_info->sgf |= 0x2;
8109 ht_info->is_green_field = cap.green_field;
8110 ht_info->max_amsdu_size = cap.maximal_amsdu_size;
8111 ht_info->supported_chan_width = cap.supported_chan_width_set;
8112 ht_info->tx_mimo_ps_mode = cap.mimo_power_save_mode;
8113 memcpy(ht_info->supp_rates, ht_cap->supported_mcs_set, 16);
8114
8115 ht_info->ampdu_factor = param_info.max_rx_ampdu_factor;
8116 ht_info->mpdu_density = param_info.mpdu_density;
8117
8118 IWL_DEBUG_MAC80211("SISO mask 0x%X MIMO mask 0x%X \n",
8119 ht_cap->supported_mcs_set[0],
8120 ht_cap->supported_mcs_set[1]);
8121
8122 if (ht_info_ap) {
8123 ht_info->control_channel = ht_info_ap->control_channel;
8124 ht_info->extension_chan_offset =
8125 ht_info_ap->extension_chan_offset;
8126 ht_info->tx_chan_width = ht_info_ap->tx_chan_width;
8127 ht_info->operating_mode = ht_info_ap->operating_mode;
8128 }
8129
8130 if (ht_extra) {
8131 extra_param_info.val = ht_extra->ht_param;
8132 ht_info->control_channel = ht_extra->control_chan;
8133 ht_info->extension_chan_offset =
8134 extra_param_info.ext_chan_offset;
8135 ht_info->tx_chan_width = extra_param_info.tx_chan_width;
8136 op_mode.val = (u16)
8137 le16_to_cpu(ht_extra->operation_mode);
8138 ht_info->operating_mode = op_mode.op_mode;
8139 IWL_DEBUG_MAC80211("control channel %d\n",
8140 ht_extra->control_chan);
8141 }
8142 } else
8143 ht_info->is_ht = 0;
8144
8145 IWL_DEBUG_MAC80211("leave\n");
8146 return 0;
8147}
8148
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008149static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07008150 struct ieee80211_ht_capability *ht_cap,
8151 struct ieee80211_ht_additional_info *ht_extra)
8152{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008153 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07008154 int rs;
8155
8156 IWL_DEBUG_MAC80211("enter: \n");
8157
8158 rs = sta_ht_info_init(ht_cap, ht_extra, NULL, &priv->current_assoc_ht);
8159 iwl4965_set_rxon_chain(priv);
8160
8161 if (priv && priv->assoc_id &&
8162 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
8163 unsigned long flags;
8164
8165 spin_lock_irqsave(&priv->lock, flags);
8166 if (priv->beacon_int)
8167 queue_work(priv->workqueue, &priv->post_associate.work);
8168 else
8169 priv->call_post_assoc_from_beacon = 1;
8170 spin_unlock_irqrestore(&priv->lock, flags);
8171 }
8172
8173 IWL_DEBUG_MAC80211("leave: control channel %d\n",
8174 ht_extra->control_chan);
8175 return rs;
8176
8177}
8178
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008179static void iwl4965_set_ht_capab(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07008180 struct ieee80211_ht_capability *ht_cap,
8181 u8 use_wide_chan)
8182{
8183 union ht_cap_info cap;
8184 union ht_param_info param_info;
8185
8186 memset(&cap, 0, sizeof(union ht_cap_info));
8187 memset(&param_info, 0, sizeof(union ht_param_info));
8188
8189 cap.maximal_amsdu_size = HT_IE_MAX_AMSDU_SIZE_4K;
8190 cap.green_field = 1;
8191 cap.short_GI20 = 1;
8192 cap.short_GI40 = 1;
8193 cap.supported_chan_width_set = use_wide_chan;
8194 cap.mimo_power_save_mode = 0x3;
8195
8196 param_info.max_rx_ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
8197 param_info.mpdu_density = CFG_HT_MPDU_DENSITY_DEF;
8198 ht_cap->capabilities_info = (__le16) cpu_to_le16(cap.val);
8199 ht_cap->mac_ht_params_info = (u8) param_info.val;
8200
8201 ht_cap->supported_mcs_set[0] = 0xff;
8202 ht_cap->supported_mcs_set[1] = 0xff;
8203 ht_cap->supported_mcs_set[4] =
8204 (cap.supported_chan_width_set) ? 0x1: 0x0;
8205}
8206
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008207static void iwl4965_mac_get_ht_capab(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07008208 struct ieee80211_ht_capability *ht_cap)
8209{
8210 u8 use_wide_channel = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008211 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07008212
8213 IWL_DEBUG_MAC80211("enter: \n");
8214 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
8215 use_wide_channel = 0;
8216
8217 /* no fat tx allowed on 2.4GHZ */
8218 if (priv->phymode != MODE_IEEE80211A)
8219 use_wide_channel = 0;
8220
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008221 iwl4965_set_ht_capab(hw, ht_cap, use_wide_channel);
Zhu Yib481de92007-09-25 17:54:57 -07008222 IWL_DEBUG_MAC80211("leave: \n");
8223}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008224#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07008225
8226/*****************************************************************************
8227 *
8228 * sysfs attributes
8229 *
8230 *****************************************************************************/
8231
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008232#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07008233
8234/*
8235 * The following adds a new attribute to the sysfs representation
8236 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
8237 * used for controlling the debug level.
8238 *
8239 * See the level definitions in iwl for details.
8240 */
8241
8242static ssize_t show_debug_level(struct device_driver *d, char *buf)
8243{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008244 return sprintf(buf, "0x%08X\n", iwl4965_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008245}
8246static ssize_t store_debug_level(struct device_driver *d,
8247 const char *buf, size_t count)
8248{
8249 char *p = (char *)buf;
8250 u32 val;
8251
8252 val = simple_strtoul(p, &p, 0);
8253 if (p == buf)
8254 printk(KERN_INFO DRV_NAME
8255 ": %s is not in hex or decimal form.\n", buf);
8256 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008257 iwl4965_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07008258
8259 return strnlen(buf, count);
8260}
8261
8262static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
8263 show_debug_level, store_debug_level);
8264
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008265#endif /* CONFIG_IWL4965_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07008266
8267static ssize_t show_rf_kill(struct device *d,
8268 struct device_attribute *attr, char *buf)
8269{
8270 /*
8271 * 0 - RF kill not enabled
8272 * 1 - SW based RF kill active (sysfs)
8273 * 2 - HW based RF kill active
8274 * 3 - Both HW and SW based RF kill active
8275 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008276 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008277 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
8278 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
8279
8280 return sprintf(buf, "%i\n", val);
8281}
8282
8283static ssize_t store_rf_kill(struct device *d,
8284 struct device_attribute *attr,
8285 const char *buf, size_t count)
8286{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008287 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008288
8289 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008290 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07008291 mutex_unlock(&priv->mutex);
8292
8293 return count;
8294}
8295
8296static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
8297
8298static ssize_t show_temperature(struct device *d,
8299 struct device_attribute *attr, char *buf)
8300{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008301 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008302
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008303 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008304 return -EAGAIN;
8305
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008306 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07008307}
8308
8309static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8310
8311static ssize_t show_rs_window(struct device *d,
8312 struct device_attribute *attr,
8313 char *buf)
8314{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008315 struct iwl4965_priv *priv = d->driver_data;
8316 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07008317}
8318static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8319
8320static ssize_t show_tx_power(struct device *d,
8321 struct device_attribute *attr, char *buf)
8322{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008323 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008324 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8325}
8326
8327static ssize_t store_tx_power(struct device *d,
8328 struct device_attribute *attr,
8329 const char *buf, size_t count)
8330{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008331 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008332 char *p = (char *)buf;
8333 u32 val;
8334
8335 val = simple_strtoul(p, &p, 10);
8336 if (p == buf)
8337 printk(KERN_INFO DRV_NAME
8338 ": %s is not in decimal form.\n", buf);
8339 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008340 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07008341
8342 return count;
8343}
8344
8345static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8346
8347static ssize_t show_flags(struct device *d,
8348 struct device_attribute *attr, char *buf)
8349{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008350 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008351
8352 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8353}
8354
8355static ssize_t store_flags(struct device *d,
8356 struct device_attribute *attr,
8357 const char *buf, size_t count)
8358{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008359 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008360 u32 flags = simple_strtoul(buf, NULL, 0);
8361
8362 mutex_lock(&priv->mutex);
8363 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8364 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008365 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008366 IWL_WARNING("Could not cancel scan.\n");
8367 else {
8368 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8369 flags);
8370 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008371 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008372 }
8373 }
8374 mutex_unlock(&priv->mutex);
8375
8376 return count;
8377}
8378
8379static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8380
8381static ssize_t show_filter_flags(struct device *d,
8382 struct device_attribute *attr, char *buf)
8383{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008384 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008385
8386 return sprintf(buf, "0x%04X\n",
8387 le32_to_cpu(priv->active_rxon.filter_flags));
8388}
8389
8390static ssize_t store_filter_flags(struct device *d,
8391 struct device_attribute *attr,
8392 const char *buf, size_t count)
8393{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008394 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008395 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8396
8397 mutex_lock(&priv->mutex);
8398 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8399 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008400 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008401 IWL_WARNING("Could not cancel scan.\n");
8402 else {
8403 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8404 "0x%04X\n", filter_flags);
8405 priv->staging_rxon.filter_flags =
8406 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008407 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008408 }
8409 }
8410 mutex_unlock(&priv->mutex);
8411
8412 return count;
8413}
8414
8415static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8416 store_filter_flags);
8417
8418static ssize_t show_tune(struct device *d,
8419 struct device_attribute *attr, char *buf)
8420{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008421 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008422
8423 return sprintf(buf, "0x%04X\n",
8424 (priv->phymode << 8) |
8425 le16_to_cpu(priv->active_rxon.channel));
8426}
8427
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008428static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode);
Zhu Yib481de92007-09-25 17:54:57 -07008429
8430static ssize_t store_tune(struct device *d,
8431 struct device_attribute *attr,
8432 const char *buf, size_t count)
8433{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008434 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008435 char *p = (char *)buf;
8436 u16 tune = simple_strtoul(p, &p, 0);
8437 u8 phymode = (tune >> 8) & 0xff;
8438 u16 channel = tune & 0xff;
8439
8440 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
8441
8442 mutex_lock(&priv->mutex);
8443 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
8444 (priv->phymode != phymode)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008445 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07008446
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008447 ch_info = iwl4965_get_channel_info(priv, phymode, channel);
Zhu Yib481de92007-09-25 17:54:57 -07008448 if (!ch_info) {
8449 IWL_WARNING("Requested invalid phymode/channel "
8450 "combination: %d %d\n", phymode, channel);
8451 mutex_unlock(&priv->mutex);
8452 return -EINVAL;
8453 }
8454
8455 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008456 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008457 IWL_WARNING("Could not cancel scan.\n");
8458 else {
8459 IWL_DEBUG_INFO("Committing phymode and "
8460 "rxon.channel = %d %d\n",
8461 phymode, channel);
8462
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008463 iwl4965_set_rxon_channel(priv, phymode, channel);
8464 iwl4965_set_flags_for_phymode(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07008465
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008466 iwl4965_set_rate(priv);
8467 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008468 }
8469 }
8470 mutex_unlock(&priv->mutex);
8471
8472 return count;
8473}
8474
8475static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
8476
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008477#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008478
8479static ssize_t show_measurement(struct device *d,
8480 struct device_attribute *attr, char *buf)
8481{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008482 struct iwl4965_priv *priv = dev_get_drvdata(d);
8483 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07008484 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8485 u8 *data = (u8 *) & measure_report;
8486 unsigned long flags;
8487
8488 spin_lock_irqsave(&priv->lock, flags);
8489 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8490 spin_unlock_irqrestore(&priv->lock, flags);
8491 return 0;
8492 }
8493 memcpy(&measure_report, &priv->measure_report, size);
8494 priv->measurement_status = 0;
8495 spin_unlock_irqrestore(&priv->lock, flags);
8496
8497 while (size && (PAGE_SIZE - len)) {
8498 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8499 PAGE_SIZE - len, 1);
8500 len = strlen(buf);
8501 if (PAGE_SIZE - len)
8502 buf[len++] = '\n';
8503
8504 ofs += 16;
8505 size -= min(size, 16U);
8506 }
8507
8508 return len;
8509}
8510
8511static ssize_t store_measurement(struct device *d,
8512 struct device_attribute *attr,
8513 const char *buf, size_t count)
8514{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008515 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008516 struct ieee80211_measurement_params params = {
8517 .channel = le16_to_cpu(priv->active_rxon.channel),
8518 .start_time = cpu_to_le64(priv->last_tsf),
8519 .duration = cpu_to_le16(1),
8520 };
8521 u8 type = IWL_MEASURE_BASIC;
8522 u8 buffer[32];
8523 u8 channel;
8524
8525 if (count) {
8526 char *p = buffer;
8527 strncpy(buffer, buf, min(sizeof(buffer), count));
8528 channel = simple_strtoul(p, NULL, 0);
8529 if (channel)
8530 params.channel = channel;
8531
8532 p = buffer;
8533 while (*p && *p != ' ')
8534 p++;
8535 if (*p)
8536 type = simple_strtoul(p + 1, NULL, 0);
8537 }
8538
8539 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8540 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008541 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07008542
8543 return count;
8544}
8545
8546static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8547 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008548#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07008549
8550static ssize_t store_retry_rate(struct device *d,
8551 struct device_attribute *attr,
8552 const char *buf, size_t count)
8553{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008554 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008555
8556 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8557 if (priv->retry_rate <= 0)
8558 priv->retry_rate = 1;
8559
8560 return count;
8561}
8562
8563static ssize_t show_retry_rate(struct device *d,
8564 struct device_attribute *attr, char *buf)
8565{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008566 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008567 return sprintf(buf, "%d", priv->retry_rate);
8568}
8569
8570static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8571 store_retry_rate);
8572
8573static ssize_t store_power_level(struct device *d,
8574 struct device_attribute *attr,
8575 const char *buf, size_t count)
8576{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008577 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008578 int rc;
8579 int mode;
8580
8581 mode = simple_strtoul(buf, NULL, 0);
8582 mutex_lock(&priv->mutex);
8583
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008584 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008585 rc = -EAGAIN;
8586 goto out;
8587 }
8588
8589 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8590 mode = IWL_POWER_AC;
8591 else
8592 mode |= IWL_POWER_ENABLED;
8593
8594 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008595 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07008596 if (rc) {
8597 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8598 goto out;
8599 }
8600 priv->power_mode = mode;
8601 }
8602
8603 rc = count;
8604
8605 out:
8606 mutex_unlock(&priv->mutex);
8607 return rc;
8608}
8609
8610#define MAX_WX_STRING 80
8611
8612/* Values are in microsecond */
8613static const s32 timeout_duration[] = {
8614 350000,
8615 250000,
8616 75000,
8617 37000,
8618 25000,
8619};
8620static const s32 period_duration[] = {
8621 400000,
8622 700000,
8623 1000000,
8624 1000000,
8625 1000000
8626};
8627
8628static ssize_t show_power_level(struct device *d,
8629 struct device_attribute *attr, char *buf)
8630{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008631 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008632 int level = IWL_POWER_LEVEL(priv->power_mode);
8633 char *p = buf;
8634
8635 p += sprintf(p, "%d ", level);
8636 switch (level) {
8637 case IWL_POWER_MODE_CAM:
8638 case IWL_POWER_AC:
8639 p += sprintf(p, "(AC)");
8640 break;
8641 case IWL_POWER_BATTERY:
8642 p += sprintf(p, "(BATTERY)");
8643 break;
8644 default:
8645 p += sprintf(p,
8646 "(Timeout %dms, Period %dms)",
8647 timeout_duration[level - 1] / 1000,
8648 period_duration[level - 1] / 1000);
8649 }
8650
8651 if (!(priv->power_mode & IWL_POWER_ENABLED))
8652 p += sprintf(p, " OFF\n");
8653 else
8654 p += sprintf(p, " \n");
8655
8656 return (p - buf + 1);
8657
8658}
8659
8660static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8661 store_power_level);
8662
8663static ssize_t show_channels(struct device *d,
8664 struct device_attribute *attr, char *buf)
8665{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008666 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008667 int len = 0, i;
8668 struct ieee80211_channel *channels = NULL;
8669 const struct ieee80211_hw_mode *hw_mode = NULL;
8670 int count = 0;
8671
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008672 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008673 return -EAGAIN;
8674
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008675 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211G);
Zhu Yib481de92007-09-25 17:54:57 -07008676 if (!hw_mode)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008677 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211B);
Zhu Yib481de92007-09-25 17:54:57 -07008678 if (hw_mode) {
8679 channels = hw_mode->channels;
8680 count = hw_mode->num_channels;
8681 }
8682
8683 len +=
8684 sprintf(&buf[len],
8685 "Displaying %d channels in 2.4GHz band "
8686 "(802.11bg):\n", count);
8687
8688 for (i = 0; i < count; i++)
8689 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8690 channels[i].chan,
8691 channels[i].power_level,
8692 channels[i].
8693 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8694 " (IEEE 802.11h required)" : "",
8695 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8696 || (channels[i].
8697 flag &
8698 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8699 ", IBSS",
8700 channels[i].
8701 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8702 "active/passive" : "passive only");
8703
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008704 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211A);
Zhu Yib481de92007-09-25 17:54:57 -07008705 if (hw_mode) {
8706 channels = hw_mode->channels;
8707 count = hw_mode->num_channels;
8708 } else {
8709 channels = NULL;
8710 count = 0;
8711 }
8712
8713 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8714 "(802.11a):\n", count);
8715
8716 for (i = 0; i < count; i++)
8717 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8718 channels[i].chan,
8719 channels[i].power_level,
8720 channels[i].
8721 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8722 " (IEEE 802.11h required)" : "",
8723 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8724 || (channels[i].
8725 flag &
8726 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8727 ", IBSS",
8728 channels[i].
8729 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8730 "active/passive" : "passive only");
8731
8732 return len;
8733}
8734
8735static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8736
8737static ssize_t show_statistics(struct device *d,
8738 struct device_attribute *attr, char *buf)
8739{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008740 struct iwl4965_priv *priv = dev_get_drvdata(d);
8741 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07008742 u32 len = 0, ofs = 0;
8743 u8 *data = (u8 *) & priv->statistics;
8744 int rc = 0;
8745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008746 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008747 return -EAGAIN;
8748
8749 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008750 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008751 mutex_unlock(&priv->mutex);
8752
8753 if (rc) {
8754 len = sprintf(buf,
8755 "Error sending statistics request: 0x%08X\n", rc);
8756 return len;
8757 }
8758
8759 while (size && (PAGE_SIZE - len)) {
8760 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8761 PAGE_SIZE - len, 1);
8762 len = strlen(buf);
8763 if (PAGE_SIZE - len)
8764 buf[len++] = '\n';
8765
8766 ofs += 16;
8767 size -= min(size, 16U);
8768 }
8769
8770 return len;
8771}
8772
8773static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8774
8775static ssize_t show_antenna(struct device *d,
8776 struct device_attribute *attr, char *buf)
8777{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008778 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008779
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008780 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008781 return -EAGAIN;
8782
8783 return sprintf(buf, "%d\n", priv->antenna);
8784}
8785
8786static ssize_t store_antenna(struct device *d,
8787 struct device_attribute *attr,
8788 const char *buf, size_t count)
8789{
8790 int ant;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008791 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008792
8793 if (count == 0)
8794 return 0;
8795
8796 if (sscanf(buf, "%1i", &ant) != 1) {
8797 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8798 return count;
8799 }
8800
8801 if ((ant >= 0) && (ant <= 2)) {
8802 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008803 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07008804 } else
8805 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8806
8807
8808 return count;
8809}
8810
8811static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8812
8813static ssize_t show_status(struct device *d,
8814 struct device_attribute *attr, char *buf)
8815{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008816 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8817 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008818 return -EAGAIN;
8819 return sprintf(buf, "0x%08x\n", (int)priv->status);
8820}
8821
8822static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8823
8824static ssize_t dump_error_log(struct device *d,
8825 struct device_attribute *attr,
8826 const char *buf, size_t count)
8827{
8828 char *p = (char *)buf;
8829
8830 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008831 iwl4965_dump_nic_error_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008832
8833 return strnlen(buf, count);
8834}
8835
8836static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8837
8838static ssize_t dump_event_log(struct device *d,
8839 struct device_attribute *attr,
8840 const char *buf, size_t count)
8841{
8842 char *p = (char *)buf;
8843
8844 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008845 iwl4965_dump_nic_event_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008846
8847 return strnlen(buf, count);
8848}
8849
8850static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8851
8852/*****************************************************************************
8853 *
8854 * driver setup and teardown
8855 *
8856 *****************************************************************************/
8857
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008858static void iwl4965_setup_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008859{
8860 priv->workqueue = create_workqueue(DRV_NAME);
8861
8862 init_waitqueue_head(&priv->wait_command_queue);
8863
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008864 INIT_WORK(&priv->up, iwl4965_bg_up);
8865 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8866 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8867 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8868 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8869 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8870 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8871 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8872 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8873 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8874 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8875 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008877 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008878
8879 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008880 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008881}
8882
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008883static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008884{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008885 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008886
Joonwoo Park3ae6a052007-11-29 10:43:16 +09008887 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008888 cancel_delayed_work(&priv->scan_check);
8889 cancel_delayed_work(&priv->alive_start);
8890 cancel_delayed_work(&priv->post_associate);
8891 cancel_work_sync(&priv->beacon_update);
8892}
8893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008894static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008895 &dev_attr_antenna.attr,
8896 &dev_attr_channels.attr,
8897 &dev_attr_dump_errors.attr,
8898 &dev_attr_dump_events.attr,
8899 &dev_attr_flags.attr,
8900 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008901#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008902 &dev_attr_measurement.attr,
8903#endif
8904 &dev_attr_power_level.attr,
8905 &dev_attr_retry_rate.attr,
8906 &dev_attr_rf_kill.attr,
8907 &dev_attr_rs_window.attr,
8908 &dev_attr_statistics.attr,
8909 &dev_attr_status.attr,
8910 &dev_attr_temperature.attr,
8911 &dev_attr_tune.attr,
8912 &dev_attr_tx_power.attr,
8913
8914 NULL
8915};
8916
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008917static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008918 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008919 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008920};
8921
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008922static struct ieee80211_ops iwl4965_hw_ops = {
8923 .tx = iwl4965_mac_tx,
8924 .start = iwl4965_mac_start,
8925 .stop = iwl4965_mac_stop,
8926 .add_interface = iwl4965_mac_add_interface,
8927 .remove_interface = iwl4965_mac_remove_interface,
8928 .config = iwl4965_mac_config,
8929 .config_interface = iwl4965_mac_config_interface,
8930 .configure_filter = iwl4965_configure_filter,
8931 .set_key = iwl4965_mac_set_key,
8932 .get_stats = iwl4965_mac_get_stats,
8933 .get_tx_stats = iwl4965_mac_get_tx_stats,
8934 .conf_tx = iwl4965_mac_conf_tx,
8935 .get_tsf = iwl4965_mac_get_tsf,
8936 .reset_tsf = iwl4965_mac_reset_tsf,
8937 .beacon_update = iwl4965_mac_beacon_update,
8938 .erp_ie_changed = iwl4965_mac_erp_ie_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008939#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008940 .conf_ht = iwl4965_mac_conf_ht,
8941 .get_ht_capab = iwl4965_mac_get_ht_capab,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008942#ifdef CONFIG_IWL4965_HT_AGG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008943 .ht_tx_agg_start = iwl4965_mac_ht_tx_agg_start,
8944 .ht_tx_agg_stop = iwl4965_mac_ht_tx_agg_stop,
8945 .ht_rx_agg_start = iwl4965_mac_ht_rx_agg_start,
8946 .ht_rx_agg_stop = iwl4965_mac_ht_rx_agg_stop,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008947#endif /* CONFIG_IWL4965_HT_AGG */
8948#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008949 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008950};
8951
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008952static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008953{
8954 int err = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008955 struct iwl4965_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008956 struct ieee80211_hw *hw;
8957 int i;
8958
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008959 if (iwl4965_param_disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008960 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008961 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008962 }
8963
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008964 if ((iwl4965_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8965 (iwl4965_param_queues_num < IWL_MIN_NUM_QUEUES)) {
Zhu Yib481de92007-09-25 17:54:57 -07008966 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8967 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8968 err = -EINVAL;
8969 goto out;
8970 }
8971
8972 /* mac80211 allocates memory for this device instance, including
8973 * space for this driver's private structure */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008974 hw = ieee80211_alloc_hw(sizeof(struct iwl4965_priv), &iwl4965_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07008975 if (hw == NULL) {
8976 IWL_ERROR("Can not allocate network device\n");
8977 err = -ENOMEM;
8978 goto out;
8979 }
8980 SET_IEEE80211_DEV(hw, &pdev->dev);
8981
Johannes Bergf51359a2007-10-28 14:53:36 +01008982 hw->rate_control_algorithm = "iwl-4965-rs";
8983
Zhu Yib481de92007-09-25 17:54:57 -07008984 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8985 priv = hw->priv;
8986 priv->hw = hw;
8987
8988 priv->pci_dev = pdev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008989 priv->antenna = (enum iwl4965_antenna)iwl4965_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008990#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008991 iwl4965_debug_level = iwl4965_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008992 atomic_set(&priv->restrict_refcnt, 0);
8993#endif
8994 priv->retry_rate = 1;
8995
8996 priv->ibss_beacon = NULL;
8997
8998 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8999 * the range of signal quality values that we'll provide.
9000 * Negative values for level/noise indicate that we'll provide dBm.
9001 * For WE, at least, non-0 values here *enable* display of values
9002 * in app (iwconfig). */
9003 hw->max_rssi = -20; /* signal level, negative indicates dBm */
9004 hw->max_noise = -20; /* noise level, negative indicates dBm */
9005 hw->max_signal = 100; /* link quality indication (%) */
9006
9007 /* Tell mac80211 our Tx characteristics */
9008 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
9009
9010 hw->queues = 4;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009011#ifdef CONFIG_IWL4965_HT
9012#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07009013 hw->queues = 16;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009014#endif /* CONFIG_IWL4965_HT_AGG */
9015#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07009016
9017 spin_lock_init(&priv->lock);
9018 spin_lock_init(&priv->power_data.lock);
9019 spin_lock_init(&priv->sta_lock);
9020 spin_lock_init(&priv->hcmd_lock);
9021 spin_lock_init(&priv->lq_mngr.lock);
9022
9023 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
9024 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
9025
9026 INIT_LIST_HEAD(&priv->free_frames);
9027
9028 mutex_init(&priv->mutex);
9029 if (pci_enable_device(pdev)) {
9030 err = -ENODEV;
9031 goto out_ieee80211_free_hw;
9032 }
9033
9034 pci_set_master(pdev);
9035
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009036 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009037
9038 priv->data_retry_limit = -1;
9039 priv->ieee_channels = NULL;
9040 priv->ieee_rates = NULL;
9041 priv->phymode = -1;
9042
9043 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
9044 if (!err)
9045 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
9046 if (err) {
9047 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
9048 goto out_pci_disable_device;
9049 }
9050
9051 pci_set_drvdata(pdev, priv);
9052 err = pci_request_regions(pdev, DRV_NAME);
9053 if (err)
9054 goto out_pci_disable_device;
9055 /* We disable the RETRY_TIMEOUT register (0x41) to keep
9056 * PCI Tx retries from interfering with C3 CPU state */
9057 pci_write_config_byte(pdev, 0x41, 0x00);
9058 priv->hw_base = pci_iomap(pdev, 0, 0);
9059 if (!priv->hw_base) {
9060 err = -ENODEV;
9061 goto out_pci_release_regions;
9062 }
9063
9064 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
9065 (unsigned long long) pci_resource_len(pdev, 0));
9066 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
9067
9068 /* Initialize module parameter values here */
9069
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009070 if (iwl4965_param_disable) {
Zhu Yib481de92007-09-25 17:54:57 -07009071 set_bit(STATUS_RF_KILL_SW, &priv->status);
9072 IWL_DEBUG_INFO("Radio disabled.\n");
9073 }
9074
9075 priv->iw_mode = IEEE80211_IF_TYPE_STA;
9076
9077 priv->ps_mode = 0;
9078 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
9079 priv->is_ht_enabled = 1;
9080 priv->channel_width = IWL_CHANNEL_WIDTH_40MHZ;
9081 priv->valid_antenna = 0x7; /* assume all 3 connected */
9082 priv->ps_mode = IWL_MIMO_PS_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07009083
9084 iwl4965_set_rxon_chain(priv);
9085
9086 printk(KERN_INFO DRV_NAME
9087 ": Detected Intel Wireless WiFi Link 4965AGN\n");
9088
9089 /* Device-specific setup */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009090 if (iwl4965_hw_set_hw_setting(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07009091 IWL_ERROR("failed to set hw settings\n");
9092 mutex_unlock(&priv->mutex);
9093 goto out_iounmap;
9094 }
9095
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009096#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009097 if (iwl4965_param_qos_enable)
Zhu Yib481de92007-09-25 17:54:57 -07009098 priv->qos_data.qos_enable = 1;
9099
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009100 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009101
9102 priv->qos_data.qos_active = 0;
9103 priv->qos_data.qos_cap.val = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009104#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07009105
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009106 iwl4965_set_rxon_channel(priv, MODE_IEEE80211G, 6);
9107 iwl4965_setup_deferred_work(priv);
9108 iwl4965_setup_rx_handlers(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009109
9110 priv->rates_mask = IWL_RATES_MASK;
9111 /* If power management is turned on, default to AC mode */
9112 priv->power_mode = IWL_POWER_AC;
9113 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
9114
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009115 iwl4965_disable_interrupts(priv);
Jes Sorensen49df2b32007-10-26 16:10:39 +02009116
Zhu Yib481de92007-09-25 17:54:57 -07009117 pci_enable_msi(pdev);
9118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009119 err = request_irq(pdev->irq, iwl4965_isr, IRQF_SHARED, DRV_NAME, priv);
Zhu Yib481de92007-09-25 17:54:57 -07009120 if (err) {
9121 IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
9122 goto out_disable_msi;
9123 }
9124
9125 mutex_lock(&priv->mutex);
9126
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009127 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07009128 if (err) {
9129 IWL_ERROR("failed to create sysfs device attributes\n");
9130 mutex_unlock(&priv->mutex);
9131 goto out_release_irq;
9132 }
9133
9134 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
9135 * ucode filename and max sizes are card-specific. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009136 err = iwl4965_read_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009137 if (err) {
9138 IWL_ERROR("Could not read microcode: %d\n", err);
9139 mutex_unlock(&priv->mutex);
9140 goto out_pci_alloc;
9141 }
9142
9143 mutex_unlock(&priv->mutex);
9144
Ian Schram01ebd062007-10-25 17:15:22 +08009145 IWL_DEBUG_INFO("Queueing UP work.\n");
Zhu Yib481de92007-09-25 17:54:57 -07009146
9147 queue_work(priv->workqueue, &priv->up);
9148
9149 return 0;
9150
9151 out_pci_alloc:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009152 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009153
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009154 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07009155
9156 out_release_irq:
9157 free_irq(pdev->irq, priv);
9158
9159 out_disable_msi:
9160 pci_disable_msi(pdev);
9161 destroy_workqueue(priv->workqueue);
9162 priv->workqueue = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009163 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009164
9165 out_iounmap:
9166 pci_iounmap(pdev, priv->hw_base);
9167 out_pci_release_regions:
9168 pci_release_regions(pdev);
9169 out_pci_disable_device:
9170 pci_disable_device(pdev);
9171 pci_set_drvdata(pdev, NULL);
9172 out_ieee80211_free_hw:
9173 ieee80211_free_hw(priv->hw);
9174 out:
9175 return err;
9176}
9177
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009178static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07009179{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009180 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07009181 struct list_head *p, *q;
9182 int i;
9183
9184 if (!priv)
9185 return;
9186
9187 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
9188
Zhu Yib481de92007-09-25 17:54:57 -07009189 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08009190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009191 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009192
9193 /* Free MAC hash list for ADHOC */
9194 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
9195 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
9196 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009197 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07009198 }
9199 }
9200
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009201 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07009202
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009203 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009204
9205 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009206 iwl4965_rx_queue_free(priv, &priv->rxq);
9207 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009209 iwl4965_unset_hw_setting(priv);
9210 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009211
9212 if (priv->mac80211_registered) {
9213 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009214 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07009215 }
9216
Mohamed Abbas948c1712007-10-25 17:15:45 +08009217 /*netif_stop_queue(dev); */
9218 flush_workqueue(priv->workqueue);
9219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009220 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07009221 * priv->workqueue... so we can't take down the workqueue
9222 * until now... */
9223 destroy_workqueue(priv->workqueue);
9224 priv->workqueue = NULL;
9225
9226 free_irq(pdev->irq, priv);
9227 pci_disable_msi(pdev);
9228 pci_iounmap(pdev, priv->hw_base);
9229 pci_release_regions(pdev);
9230 pci_disable_device(pdev);
9231 pci_set_drvdata(pdev, NULL);
9232
9233 kfree(priv->channel_info);
9234
9235 kfree(priv->ieee_channels);
9236 kfree(priv->ieee_rates);
9237
9238 if (priv->ibss_beacon)
9239 dev_kfree_skb(priv->ibss_beacon);
9240
9241 ieee80211_free_hw(priv->hw);
9242}
9243
9244#ifdef CONFIG_PM
9245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009246static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07009247{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009248 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07009249
Zhu Yib481de92007-09-25 17:54:57 -07009250 set_bit(STATUS_IN_SUSPEND, &priv->status);
9251
9252 /* Take down the device; powers it off, etc. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009253 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009254
9255 if (priv->mac80211_registered)
9256 ieee80211_stop_queues(priv->hw);
9257
9258 pci_save_state(pdev);
9259 pci_disable_device(pdev);
9260 pci_set_power_state(pdev, PCI_D3hot);
9261
Zhu Yib481de92007-09-25 17:54:57 -07009262 return 0;
9263}
9264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009265static void iwl4965_resume(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07009266{
9267 unsigned long flags;
9268
9269 /* The following it a temporary work around due to the
9270 * suspend / resume not fully initializing the NIC correctly.
9271 * Without all of the following, resume will not attempt to take
9272 * down the NIC (it shouldn't really need to) and will just try
9273 * and bring the NIC back up. However that fails during the
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009274 * ucode verification process. This then causes iwl4965_down to be
9275 * called *after* iwl4965_hw_nic_init() has succeeded -- which
Zhu Yib481de92007-09-25 17:54:57 -07009276 * then lets the next init sequence succeed. So, we've
9277 * replicated all of that NIC init code here... */
9278
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009279 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07009280
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009281 iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009282
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009283 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9284 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07009285 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009286 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
9287 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9288 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07009289
9290 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009291 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009292
9293 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009294 iwl4965_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07009295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009296 if (!iwl4965_grab_nic_access(priv)) {
9297 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Tomas Winklerac17a942007-10-25 17:15:37 +08009298 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009299 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009300 }
9301 spin_unlock_irqrestore(&priv->lock, flags);
9302
9303 udelay(5);
9304
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009305 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009306
9307 /* Bring the device back up */
9308 clear_bit(STATUS_IN_SUSPEND, &priv->status);
9309 queue_work(priv->workqueue, &priv->up);
9310}
9311
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009312static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07009313{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009314 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07009315 int err;
9316
9317 printk(KERN_INFO "Coming out of suspend...\n");
9318
Zhu Yib481de92007-09-25 17:54:57 -07009319 pci_set_power_state(pdev, PCI_D0);
9320 err = pci_enable_device(pdev);
9321 pci_restore_state(pdev);
9322
9323 /*
9324 * Suspend/Resume resets the PCI configuration space, so we have to
9325 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
9326 * from interfering with C3 CPU state. pci_restore_state won't help
9327 * here since it only restores the first 64 bytes pci config header.
9328 */
9329 pci_write_config_byte(pdev, 0x41, 0x00);
9330
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009331 iwl4965_resume(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009332
9333 return 0;
9334}
9335
9336#endif /* CONFIG_PM */
9337
9338/*****************************************************************************
9339 *
9340 * driver and module entry point
9341 *
9342 *****************************************************************************/
9343
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009344static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07009345 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009346 .id_table = iwl4965_hw_card_ids,
9347 .probe = iwl4965_pci_probe,
9348 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07009349#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009350 .suspend = iwl4965_pci_suspend,
9351 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07009352#endif
9353};
9354
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009355static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07009356{
9357
9358 int ret;
9359 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
9360 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009361 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07009362 if (ret) {
9363 IWL_ERROR("Unable to initialize PCI module\n");
9364 return ret;
9365 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009366#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009367 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07009368 if (ret) {
9369 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009370 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07009371 return ret;
9372 }
9373#endif
9374
9375 return ret;
9376}
9377
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009378static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07009379{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009380#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009381 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07009382#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009383 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07009384}
9385
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009386module_param_named(antenna, iwl4965_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009387MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009388module_param_named(disable, iwl4965_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009389MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009390module_param_named(hwcrypto, iwl4965_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009391MODULE_PARM_DESC(hwcrypto,
9392 "using hardware crypto engine (default 0 [software])\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009393module_param_named(debug, iwl4965_param_debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009394MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009395module_param_named(disable_hw_scan, iwl4965_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009396MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
9397
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009398module_param_named(queues_num, iwl4965_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009399MODULE_PARM_DESC(queues_num, "number of hw queues.");
9400
9401/* QoS */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009402module_param_named(qos_enable, iwl4965_param_qos_enable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009403MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
9404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009405module_exit(iwl4965_exit);
9406module_init(iwl4965_init);