blob: f644c6bba85fadbfee838adeec0babf1f799384b [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
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/version.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#include <linux/skbuff.h>
38#include <linux/netdevice.h>
39#include <linux/wireless.h>
40#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
44#include <net/ieee80211_radiotap.h>
45#include <net/mac80211.h>
46
47#include <asm/div64.h>
48
Zhu Yib481de92007-09-25 17:54:57 -070049#include "iwl-3945.h"
50#include "iwl-helpers.h"
51
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080052#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080053u32 iwl3945_debug_level;
Zhu Yib481de92007-09-25 17:54:57 -070054#endif
55
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080056static int iwl3945_tx_queue_update_write_ptr(struct iwl3945_priv *priv,
57 struct iwl3945_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080058
Zhu Yib481de92007-09-25 17:54:57 -070059/******************************************************************************
60 *
61 * module boiler plate
62 *
63 ******************************************************************************/
64
65/* module parameters */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080066static int iwl3945_param_disable_hw_scan; /* def: 0 = use 3945's h/w scan */
67static int iwl3945_param_debug; /* def: 0 = minimal debug log messages */
68static int iwl3945_param_disable; /* def: 0 = enable radio */
Ben Cahill9fbab512007-11-29 11:09:47 +080069static int iwl3945_param_antenna; /* def: 0 = both antennas (use diversity) */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080070int iwl3945_param_hwcrypto; /* def: 0 = use software encryption */
71static int iwl3945_param_qos_enable = 1; /* def: 1 = use quality of service */
72int iwl3945_param_queues_num = IWL_MAX_NUM_QUEUES; /* def: 8 Tx queues */
Zhu Yib481de92007-09-25 17:54:57 -070073
74/*
75 * module name, copyright, version, etc.
76 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
77 */
78
79#define DRV_DESCRIPTION \
80"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
81
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080082#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070083#define VD "d"
84#else
85#define VD
86#endif
87
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080088#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070089#define VS "s"
90#else
91#define VS
92#endif
93
Zhu Yi71972662008-01-14 17:46:23 -080094#define IWLWIFI_VERSION "1.2.23k" VD VS
Zhu Yib481de92007-09-25 17:54:57 -070095#define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
96#define DRV_VERSION IWLWIFI_VERSION
97
98/* Change firmware file name, using "-" and incrementing number,
99 * *only* when uCode interface or architecture changes so that it
100 * is not compatible with earlier drivers.
101 * This number will also appear in << 8 position of 1st dword of uCode file */
102#define IWL3945_UCODE_API "-1"
103
104MODULE_DESCRIPTION(DRV_DESCRIPTION);
105MODULE_VERSION(DRV_VERSION);
106MODULE_AUTHOR(DRV_COPYRIGHT);
107MODULE_LICENSE("GPL");
108
Christoph Hellwig416e1432007-10-25 17:15:49 +0800109static __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -0700110{
111 u16 fc = le16_to_cpu(hdr->frame_control);
112 int hdr_len = ieee80211_get_hdrlen(fc);
113
114 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
115 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
116 return NULL;
117}
118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800119static const struct ieee80211_hw_mode *iwl3945_get_hw_mode(
120 struct iwl3945_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -0700121{
122 int i;
123
124 for (i = 0; i < 3; i++)
125 if (priv->modes[i].mode == mode)
126 return &priv->modes[i];
127
128 return NULL;
129}
130
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800131static int iwl3945_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700132{
133 /* Single white space is for Linksys APs */
134 if (essid_len == 1 && essid[0] == ' ')
135 return 1;
136
137 /* Otherwise, if the entire essid is 0, we assume it is hidden */
138 while (essid_len) {
139 essid_len--;
140 if (essid[essid_len] != '\0')
141 return 0;
142 }
143
144 return 1;
145}
146
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800147static const char *iwl3945_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700148{
149 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
150 const char *s = essid;
151 char *d = escaped;
152
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800153 if (iwl3945_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700154 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
155 return escaped;
156 }
157
158 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
159 while (essid_len--) {
160 if (*s == '\0') {
161 *d++ = '\\';
162 *d++ = '0';
163 s++;
164 } else
165 *d++ = *s++;
166 }
167 *d = '\0';
168 return escaped;
169}
170
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800171static void iwl3945_print_hex_dump(int level, void *p, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -0700172{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800173#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800174 if (!(iwl3945_debug_level & level))
Zhu Yib481de92007-09-25 17:54:57 -0700175 return;
176
177 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
178 p, len, 1);
179#endif
180}
181
182/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
183 * DMA services
184 *
185 * Theory of operation
186 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800187 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
188 * of buffer descriptors, each of which points to one or more data buffers for
189 * the device to read from or fill. Driver and device exchange status of each
190 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
191 * entries in each circular buffer, to protect against confusing empty and full
192 * queue states.
193 *
194 * The device reads or writes the data in the queues via the device's several
195 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700196 *
197 * For Tx queue, there are low mark and high mark limits. If, after queuing
198 * the packet for Tx, free space become < low mark, Tx queue stopped. When
199 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
200 * Tx queue resumed.
201 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800202 * The 3945 operates with six queues: One receive queue, one transmit queue
203 * (#4) for sending commands to the device firmware, and four transmit queues
204 * (#0-3) for data tx via EDCA. An additional 2 HCCA queues are unused.
Zhu Yib481de92007-09-25 17:54:57 -0700205 ***************************************************/
206
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800207static int iwl3945_queue_space(const struct iwl3945_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700208{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800209 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700210
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800211 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700212 s -= q->n_bd;
213
214 if (s <= 0)
215 s += q->n_window;
216 /* keep some reserve to not confuse empty and full situations */
217 s -= 2;
218 if (s < 0)
219 s = 0;
220 return s;
221}
222
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800223/**
224 * iwl3945_queue_inc_wrap - increment queue index, wrap back to beginning
225 * @index -- current index
226 * @n_bd -- total number of entries in queue (must be power of 2)
227 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800228static inline int iwl3945_queue_inc_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700229{
230 return ++index & (n_bd - 1);
231}
232
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800233/**
234 * iwl3945_queue_dec_wrap - increment queue index, wrap back to end
235 * @index -- current index
236 * @n_bd -- total number of entries in queue (must be power of 2)
237 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800238static inline int iwl3945_queue_dec_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700239{
240 return --index & (n_bd - 1);
241}
242
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800243static inline int x2_queue_used(const struct iwl3945_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700244{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800245 return q->write_ptr > q->read_ptr ?
246 (i >= q->read_ptr && i < q->write_ptr) :
247 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700248}
249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800250static inline u8 get_cmd_index(struct iwl3945_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700251{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800252 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700253 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800254 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700255
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800256 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700257 return index & (q->n_window - 1);
258}
259
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800260/**
261 * iwl3945_queue_init - Initialize queue's high/low-water and read/write indexes
262 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800263static int iwl3945_queue_init(struct iwl3945_priv *priv, struct iwl3945_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700264 int count, int slots_num, u32 id)
265{
266 q->n_bd = count;
267 q->n_window = slots_num;
268 q->id = id;
269
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800270 /* count must be power-of-two size, otherwise iwl3945_queue_inc_wrap
271 * and iwl3945_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700272 BUG_ON(!is_power_of_2(count));
273
274 /* slots_num must be power-of-two size, otherwise
275 * get_cmd_index is broken. */
276 BUG_ON(!is_power_of_2(slots_num));
277
278 q->low_mark = q->n_window / 4;
279 if (q->low_mark < 4)
280 q->low_mark = 4;
281
282 q->high_mark = q->n_window / 8;
283 if (q->high_mark < 2)
284 q->high_mark = 2;
285
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800286 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700287
288 return 0;
289}
290
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800291/**
292 * iwl3945_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
293 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800294static int iwl3945_tx_queue_alloc(struct iwl3945_priv *priv,
295 struct iwl3945_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700296{
297 struct pci_dev *dev = priv->pci_dev;
298
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800299 /* Driver private data, only for Tx (not command) queues,
300 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700301 if (id != IWL_CMD_QUEUE_NUM) {
302 txq->txb = kmalloc(sizeof(txq->txb[0]) *
303 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
304 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800305 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700306 "structures failed\n");
307 goto error;
308 }
309 } else
310 txq->txb = NULL;
311
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800312 /* Circular buffer of transmit frame descriptors (TFDs),
313 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700314 txq->bd = pci_alloc_consistent(dev,
315 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
316 &txq->q.dma_addr);
317
318 if (!txq->bd) {
319 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
320 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
321 goto error;
322 }
323 txq->q.id = id;
324
325 return 0;
326
327 error:
328 if (txq->txb) {
329 kfree(txq->txb);
330 txq->txb = NULL;
331 }
332
333 return -ENOMEM;
334}
335
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800336/**
337 * iwl3945_tx_queue_init - Allocate and initialize one tx/cmd queue
338 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800339int iwl3945_tx_queue_init(struct iwl3945_priv *priv,
340 struct iwl3945_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700341{
342 struct pci_dev *dev = priv->pci_dev;
343 int len;
344 int rc = 0;
345
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800346 /*
347 * Alloc buffer array for commands (Tx or other types of commands).
348 * For the command queue (#4), allocate command space + one big
349 * command for scan, since scan command is very huge; the system will
350 * not have two scans at the same time, so only one is needed.
351 * For data Tx queues (all other queues), no super-size command
352 * space is needed.
353 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800354 len = sizeof(struct iwl3945_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700355 if (txq_id == IWL_CMD_QUEUE_NUM)
356 len += IWL_MAX_SCAN_SIZE;
357 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
358 if (!txq->cmd)
359 return -ENOMEM;
360
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800361 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800362 rc = iwl3945_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700363 if (rc) {
364 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
365
366 return -ENOMEM;
367 }
368 txq->need_update = 0;
369
370 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800371 * iwl3945_queue_inc_wrap and iwl3945_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700372 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800373
374 /* Initialize queue high/low-water, head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800375 iwl3945_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700376
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800377 /* Tell device where to find queue, enable DMA channel. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800378 iwl3945_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700379
380 return 0;
381}
382
383/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800384 * iwl3945_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700385 * @txq: Transmit queue to deallocate.
386 *
387 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800388 * Free all buffers.
389 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700390 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800391void iwl3945_tx_queue_free(struct iwl3945_priv *priv, struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700392{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800393 struct iwl3945_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700394 struct pci_dev *dev = priv->pci_dev;
395 int len;
396
397 if (q->n_bd == 0)
398 return;
399
400 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800401 for (; q->write_ptr != q->read_ptr;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800402 q->read_ptr = iwl3945_queue_inc_wrap(q->read_ptr, q->n_bd))
403 iwl3945_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800405 len = sizeof(struct iwl3945_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700406 if (q->id == IWL_CMD_QUEUE_NUM)
407 len += IWL_MAX_SCAN_SIZE;
408
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800409 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700410 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
411
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800412 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700413 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800414 pci_free_consistent(dev, sizeof(struct iwl3945_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700415 txq->q.n_bd, txq->bd, txq->q.dma_addr);
416
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800417 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700418 if (txq->txb) {
419 kfree(txq->txb);
420 txq->txb = NULL;
421 }
422
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800423 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700424 memset(txq, 0, sizeof(*txq));
425}
426
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800427const u8 iwl3945_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700428
429/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800430 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700431 * the functionality provided here
432 */
433
434/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800435#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800436/**
437 * iwl3945_remove_station - Remove driver's knowledge of station.
438 *
439 * NOTE: This does not remove station from device's station table.
440 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800441static u8 iwl3945_remove_station(struct iwl3945_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700442{
443 int index = IWL_INVALID_STATION;
444 int i;
445 unsigned long flags;
446
447 spin_lock_irqsave(&priv->sta_lock, flags);
448
449 if (is_ap)
450 index = IWL_AP_ID;
451 else if (is_broadcast_ether_addr(addr))
452 index = priv->hw_setting.bcast_sta_id;
453 else
454 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
455 if (priv->stations[i].used &&
456 !compare_ether_addr(priv->stations[i].sta.sta.addr,
457 addr)) {
458 index = i;
459 break;
460 }
461
462 if (unlikely(index == IWL_INVALID_STATION))
463 goto out;
464
465 if (priv->stations[index].used) {
466 priv->stations[index].used = 0;
467 priv->num_stations--;
468 }
469
470 BUG_ON(priv->num_stations < 0);
471
472out:
473 spin_unlock_irqrestore(&priv->sta_lock, flags);
474 return 0;
475}
Zhu Yi556f8db2007-09-27 11:27:33 +0800476#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800477
478/**
479 * iwl3945_clear_stations_table - Clear the driver's station table
480 *
481 * NOTE: This does not clear or otherwise alter the device's station table.
482 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800483static void iwl3945_clear_stations_table(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700484{
485 unsigned long flags;
486
487 spin_lock_irqsave(&priv->sta_lock, flags);
488
489 priv->num_stations = 0;
490 memset(priv->stations, 0, sizeof(priv->stations));
491
492 spin_unlock_irqrestore(&priv->sta_lock, flags);
493}
494
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800495/**
496 * iwl3945_add_station - Add station to station tables in driver and device
497 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800498u8 iwl3945_add_station(struct iwl3945_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700499{
500 int i;
501 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800502 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700503 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700504 DECLARE_MAC_BUF(mac);
Zhu Yic14c5212007-09-27 11:27:35 +0800505 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700506
507 spin_lock_irqsave(&priv->sta_lock, flags_spin);
508 if (is_ap)
509 index = IWL_AP_ID;
510 else if (is_broadcast_ether_addr(addr))
511 index = priv->hw_setting.bcast_sta_id;
512 else
513 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
514 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
515 addr)) {
516 index = i;
517 break;
518 }
519
520 if (!priv->stations[i].used &&
521 index == IWL_INVALID_STATION)
522 index = i;
523 }
524
Ian Schram01ebd062007-10-25 17:15:22 +0800525 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700526 since they have different meaning */
527 if (unlikely(index == IWL_INVALID_STATION)) {
528 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
529 return index;
530 }
531
532 if (priv->stations[index].used &&
533 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
534 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
535 return index;
536 }
537
Joe Perches0795af52007-10-03 17:59:30 -0700538 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700539 station = &priv->stations[index];
540 station->used = 1;
541 priv->num_stations++;
542
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800543 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800544 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700545 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
546 station->sta.mode = 0;
547 station->sta.sta.sta_id = index;
548 station->sta.station_flags = 0;
549
Tomas Winkler69946332007-10-25 17:15:27 +0800550 if (priv->phymode == MODE_IEEE80211A)
551 rate = IWL_RATE_6M_PLCP;
552 else
553 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800554
555 /* Turn on both antennas for the station... */
556 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800557 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800558 station->current_rate.rate_n_flags =
559 le16_to_cpu(station->sta.rate_n_flags);
560
Zhu Yib481de92007-09-25 17:54:57 -0700561 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800562
563 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800564 iwl3945_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700565 return index;
566
567}
568
569/*************** DRIVER STATUS FUNCTIONS *****/
570
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800571static inline int iwl3945_is_ready(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700572{
573 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
574 * set but EXIT_PENDING is not */
575 return test_bit(STATUS_READY, &priv->status) &&
576 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
577 !test_bit(STATUS_EXIT_PENDING, &priv->status);
578}
579
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800580static inline int iwl3945_is_alive(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700581{
582 return test_bit(STATUS_ALIVE, &priv->status);
583}
584
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800585static inline int iwl3945_is_init(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700586{
587 return test_bit(STATUS_INIT, &priv->status);
588}
589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800590static inline int iwl3945_is_rfkill(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700591{
592 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
593 test_bit(STATUS_RF_KILL_SW, &priv->status);
594}
595
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800596static inline int iwl3945_is_ready_rf(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700597{
598
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800599 if (iwl3945_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700600 return 0;
601
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800602 return iwl3945_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700603}
604
605/*************** HOST COMMAND QUEUE FUNCTIONS *****/
606
607#define IWL_CMD(x) case x : return #x
608
609static const char *get_cmd_string(u8 cmd)
610{
611 switch (cmd) {
612 IWL_CMD(REPLY_ALIVE);
613 IWL_CMD(REPLY_ERROR);
614 IWL_CMD(REPLY_RXON);
615 IWL_CMD(REPLY_RXON_ASSOC);
616 IWL_CMD(REPLY_QOS_PARAM);
617 IWL_CMD(REPLY_RXON_TIMING);
618 IWL_CMD(REPLY_ADD_STA);
619 IWL_CMD(REPLY_REMOVE_STA);
620 IWL_CMD(REPLY_REMOVE_ALL_STA);
621 IWL_CMD(REPLY_3945_RX);
622 IWL_CMD(REPLY_TX);
623 IWL_CMD(REPLY_RATE_SCALE);
624 IWL_CMD(REPLY_LEDS_CMD);
625 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
626 IWL_CMD(RADAR_NOTIFICATION);
627 IWL_CMD(REPLY_QUIET_CMD);
628 IWL_CMD(REPLY_CHANNEL_SWITCH);
629 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
630 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
631 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
632 IWL_CMD(POWER_TABLE_CMD);
633 IWL_CMD(PM_SLEEP_NOTIFICATION);
634 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
635 IWL_CMD(REPLY_SCAN_CMD);
636 IWL_CMD(REPLY_SCAN_ABORT_CMD);
637 IWL_CMD(SCAN_START_NOTIFICATION);
638 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
639 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
640 IWL_CMD(BEACON_NOTIFICATION);
641 IWL_CMD(REPLY_TX_BEACON);
642 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
643 IWL_CMD(QUIET_NOTIFICATION);
644 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
645 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
646 IWL_CMD(REPLY_BT_CONFIG);
647 IWL_CMD(REPLY_STATISTICS_CMD);
648 IWL_CMD(STATISTICS_NOTIFICATION);
649 IWL_CMD(REPLY_CARD_STATE_CMD);
650 IWL_CMD(CARD_STATE_NOTIFICATION);
651 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
652 default:
653 return "UNKNOWN";
654
655 }
656}
657
658#define HOST_COMPLETE_TIMEOUT (HZ / 2)
659
660/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800661 * iwl3945_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700662 * @priv: device private data point
663 * @cmd: a point to the ucode command structure
664 *
665 * The function returns < 0 values to indicate the operation is
666 * failed. On success, it turns the index (> 0) of command in the
667 * command queue.
668 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800669static int iwl3945_enqueue_hcmd(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700670{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800671 struct iwl3945_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
672 struct iwl3945_queue *q = &txq->q;
673 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700674 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800675 struct iwl3945_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700676 u32 idx;
677 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
678 dma_addr_t phys_addr;
679 int pad;
680 u16 count;
681 int ret;
682 unsigned long flags;
683
684 /* If any of the command structures end up being larger than
685 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
686 * we will need to increase the size of the TFD entries */
687 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
688 !(cmd->meta.flags & CMD_SIZE_HUGE));
689
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800690 if (iwl3945_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700691 IWL_ERROR("No space for Tx\n");
692 return -ENOSPC;
693 }
694
695 spin_lock_irqsave(&priv->hcmd_lock, flags);
696
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800697 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700698 memset(tfd, 0, sizeof(*tfd));
699
700 control_flags = (u32 *) tfd;
701
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800702 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700703 out_cmd = &txq->cmd[idx];
704
705 out_cmd->hdr.cmd = cmd->id;
706 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
707 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
708
709 /* At this point, the out_cmd now has all of the incoming cmd
710 * information */
711
712 out_cmd->hdr.flags = 0;
713 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800714 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700715 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
716 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
717
718 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800719 offsetof(struct iwl3945_cmd, hdr);
720 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700721
722 pad = U32_PAD(cmd->len);
723 count = TFD_CTL_COUNT_GET(*control_flags);
724 *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
725
726 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
727 "%d bytes at %d[%d]:%d\n",
728 get_cmd_string(out_cmd->hdr.cmd),
729 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800730 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700731
732 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800733
734 /* Increment and update queue's write index */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800735 q->write_ptr = iwl3945_queue_inc_wrap(q->write_ptr, q->n_bd);
736 ret = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700737
738 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
739 return ret ? ret : idx;
740}
741
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800742static int iwl3945_send_cmd_async(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700743{
744 int ret;
745
746 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
747
748 /* An asynchronous command can not expect an SKB to be set. */
749 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
750
751 /* An asynchronous command MUST have a callback. */
752 BUG_ON(!cmd->meta.u.callback);
753
754 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
755 return -EBUSY;
756
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800757 ret = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700758 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800759 IWL_ERROR("Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700760 get_cmd_string(cmd->id), ret);
761 return ret;
762 }
763 return 0;
764}
765
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800766static int iwl3945_send_cmd_sync(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700767{
768 int cmd_idx;
769 int ret;
770 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
771
772 BUG_ON(cmd->meta.flags & CMD_ASYNC);
773
774 /* A synchronous command can not have a callback set. */
775 BUG_ON(cmd->meta.u.callback != NULL);
776
777 if (atomic_xchg(&entry, 1)) {
778 IWL_ERROR("Error sending %s: Already sending a host command\n",
779 get_cmd_string(cmd->id));
780 return -EBUSY;
781 }
782
783 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
784
785 if (cmd->meta.flags & CMD_WANT_SKB)
786 cmd->meta.source = &cmd->meta;
787
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800788 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700789 if (cmd_idx < 0) {
790 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800791 IWL_ERROR("Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700792 get_cmd_string(cmd->id), ret);
793 goto out;
794 }
795
796 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
797 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
798 HOST_COMPLETE_TIMEOUT);
799 if (!ret) {
800 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
801 IWL_ERROR("Error sending %s: time out after %dms.\n",
802 get_cmd_string(cmd->id),
803 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
804
805 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
806 ret = -ETIMEDOUT;
807 goto cancel;
808 }
809 }
810
811 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
812 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
813 get_cmd_string(cmd->id));
814 ret = -ECANCELED;
815 goto fail;
816 }
817 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
818 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
819 get_cmd_string(cmd->id));
820 ret = -EIO;
821 goto fail;
822 }
823 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
824 IWL_ERROR("Error: Response NULL in '%s'\n",
825 get_cmd_string(cmd->id));
826 ret = -EIO;
827 goto out;
828 }
829
830 ret = 0;
831 goto out;
832
833cancel:
834 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800835 struct iwl3945_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700836
837 /* Cancel the CMD_WANT_SKB flag for the cmd in the
838 * TX cmd queue. Otherwise in case the cmd comes
839 * in later, it will possibly set an invalid
840 * address (cmd->meta.source). */
841 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
842 qcmd->meta.flags &= ~CMD_WANT_SKB;
843 }
844fail:
845 if (cmd->meta.u.skb) {
846 dev_kfree_skb_any(cmd->meta.u.skb);
847 cmd->meta.u.skb = NULL;
848 }
849out:
850 atomic_set(&entry, 0);
851 return ret;
852}
853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800854int iwl3945_send_cmd(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700855{
Zhu Yib481de92007-09-25 17:54:57 -0700856 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800857 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700858
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800859 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700860}
861
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800862int iwl3945_send_cmd_pdu(struct iwl3945_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700863{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800864 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700865 .id = id,
866 .len = len,
867 .data = data,
868 };
869
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800870 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700871}
872
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800873static int __must_check iwl3945_send_cmd_u32(struct iwl3945_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700874{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800875 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700876 .id = id,
877 .len = sizeof(val),
878 .data = &val,
879 };
880
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800881 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700882}
883
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800884int iwl3945_send_statistics_request(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700885{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800886 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700887}
888
889/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800890 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Zhu Yib481de92007-09-25 17:54:57 -0700891 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
892 * @channel: Any channel valid for the requested phymode
893
894 * In addition to setting the staging RXON, priv->phymode is also set.
895 *
896 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
897 * in the staging RXON flag structure based on the phymode
898 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800899static int iwl3945_set_rxon_channel(struct iwl3945_priv *priv, u8 phymode, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700900{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800901 if (!iwl3945_get_channel_info(priv, phymode, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700902 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
903 channel, phymode);
904 return -EINVAL;
905 }
906
907 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
908 (priv->phymode == phymode))
909 return 0;
910
911 priv->staging_rxon.channel = cpu_to_le16(channel);
912 if (phymode == MODE_IEEE80211A)
913 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
914 else
915 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
916
917 priv->phymode = phymode;
918
919 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
920
921 return 0;
922}
923
924/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800925 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700926 *
927 * NOTE: This is really only useful during development and can eventually
928 * be #ifdef'd out once the driver is stable and folks aren't actively
929 * making changes
930 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800931static int iwl3945_check_rxon_cmd(struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700932{
933 int error = 0;
934 int counter = 1;
935
936 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
937 error |= le32_to_cpu(rxon->flags &
938 (RXON_FLG_TGJ_NARROW_BAND_MSK |
939 RXON_FLG_RADAR_DETECT_MSK));
940 if (error)
941 IWL_WARNING("check 24G fields %d | %d\n",
942 counter++, error);
943 } else {
944 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
945 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
946 if (error)
947 IWL_WARNING("check 52 fields %d | %d\n",
948 counter++, error);
949 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
950 if (error)
951 IWL_WARNING("check 52 CCK %d | %d\n",
952 counter++, error);
953 }
954 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
955 if (error)
956 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
957
958 /* make sure basic rates 6Mbps and 1Mbps are supported */
959 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
960 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
961 if (error)
962 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
963
964 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
965 if (error)
966 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
967
968 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
969 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
970 if (error)
971 IWL_WARNING("check CCK and short slot %d | %d\n",
972 counter++, error);
973
974 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
975 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
976 if (error)
977 IWL_WARNING("check CCK & auto detect %d | %d\n",
978 counter++, error);
979
980 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
981 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
982 if (error)
983 IWL_WARNING("check TGG and auto detect %d | %d\n",
984 counter++, error);
985
986 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
987 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
988 RXON_FLG_ANT_A_MSK)) == 0);
989 if (error)
990 IWL_WARNING("check antenna %d %d\n", counter++, error);
991
992 if (error)
993 IWL_WARNING("Tuning to channel %d\n",
994 le16_to_cpu(rxon->channel));
995
996 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800997 IWL_ERROR("Not a valid iwl3945_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700998 return -1;
999 }
1000 return 0;
1001}
1002
1003/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001004 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +08001005 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -07001006 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001007 * If the RXON structure is changing enough to require a new tune,
1008 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
1009 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -07001010 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001011static int iwl3945_full_rxon_required(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001012{
1013
1014 /* These items are only settable from the full RXON command */
1015 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
1016 compare_ether_addr(priv->staging_rxon.bssid_addr,
1017 priv->active_rxon.bssid_addr) ||
1018 compare_ether_addr(priv->staging_rxon.node_addr,
1019 priv->active_rxon.node_addr) ||
1020 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
1021 priv->active_rxon.wlap_bssid_addr) ||
1022 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
1023 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
1024 (priv->staging_rxon.air_propagation !=
1025 priv->active_rxon.air_propagation) ||
1026 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1027 return 1;
1028
1029 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1030 * be updated with the RXON_ASSOC command -- however only some
1031 * flag transitions are allowed using RXON_ASSOC */
1032
1033 /* Check if we are not switching bands */
1034 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1035 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1036 return 1;
1037
1038 /* Check if we are switching association toggle */
1039 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1040 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1041 return 1;
1042
1043 return 0;
1044}
1045
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001046static int iwl3945_send_rxon_assoc(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001047{
1048 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001049 struct iwl3945_rx_packet *res = NULL;
1050 struct iwl3945_rxon_assoc_cmd rxon_assoc;
1051 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001052 .id = REPLY_RXON_ASSOC,
1053 .len = sizeof(rxon_assoc),
1054 .meta.flags = CMD_WANT_SKB,
1055 .data = &rxon_assoc,
1056 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001057 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging_rxon;
1058 const struct iwl3945_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001059
1060 if ((rxon1->flags == rxon2->flags) &&
1061 (rxon1->filter_flags == rxon2->filter_flags) &&
1062 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1063 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1064 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1065 return 0;
1066 }
1067
1068 rxon_assoc.flags = priv->staging_rxon.flags;
1069 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1070 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1071 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1072 rxon_assoc.reserved = 0;
1073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001074 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001075 if (rc)
1076 return rc;
1077
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001078 res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001079 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1080 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1081 rc = -EIO;
1082 }
1083
1084 priv->alloc_rxb_skb--;
1085 dev_kfree_skb_any(cmd.meta.u.skb);
1086
1087 return rc;
1088}
1089
1090/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001091 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001092 *
Ian Schram01ebd062007-10-25 17:15:22 +08001093 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001094 * the active_rxon structure is updated with the new data. This
1095 * function correctly transitions out of the RXON_ASSOC_MSK state if
1096 * a HW tune is required based on the RXON structure changes.
1097 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001098static int iwl3945_commit_rxon(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001099{
1100 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001101 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001102 int rc = 0;
Joe Perches0795af52007-10-03 17:59:30 -07001103 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001104
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001105 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001106 return -1;
1107
1108 /* always get timestamp with Rx frame */
1109 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1110
1111 /* select antenna */
1112 priv->staging_rxon.flags &=
1113 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
1114 priv->staging_rxon.flags |= iwl3945_get_antenna_flags(priv);
1115
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001116 rc = iwl3945_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001117 if (rc) {
1118 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1119 return -EINVAL;
1120 }
1121
1122 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001123 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001124 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001125 if (!iwl3945_full_rxon_required(priv)) {
1126 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001127 if (rc) {
1128 IWL_ERROR("Error setting RXON_ASSOC "
1129 "configuration (%d).\n", rc);
1130 return rc;
1131 }
1132
1133 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1134
1135 return 0;
1136 }
1137
1138 /* If we are currently associated and the new config requires
1139 * an RXON_ASSOC and the new config wants the associated mask enabled,
1140 * we must clear the associated from the active configuration
1141 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001142 if (iwl3945_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001143 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1144 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1145 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1146
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001147 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
1148 sizeof(struct iwl3945_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001149 &priv->active_rxon);
1150
1151 /* If the mask clearing failed then we set
1152 * active_rxon back to what it was previously */
1153 if (rc) {
1154 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1155 IWL_ERROR("Error clearing ASSOC_MSK on current "
1156 "configuration (%d).\n", rc);
1157 return rc;
1158 }
Zhu Yib481de92007-09-25 17:54:57 -07001159 }
1160
1161 IWL_DEBUG_INFO("Sending RXON\n"
1162 "* with%s RXON_FILTER_ASSOC_MSK\n"
1163 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001164 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001165 ((priv->staging_rxon.filter_flags &
1166 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1167 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001168 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001169
1170 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001171 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
1172 sizeof(struct iwl3945_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001173 if (rc) {
1174 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1175 return rc;
1176 }
1177
1178 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1179
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001180 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001181
Zhu Yib481de92007-09-25 17:54:57 -07001182 /* If we issue a new RXON command which required a tune then we must
1183 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001184 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001185 if (rc) {
1186 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1187 return rc;
1188 }
1189
1190 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001191 if (iwl3945_add_station(priv, iwl3945_broadcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001192 IWL_INVALID_STATION) {
1193 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1194 return -EIO;
1195 }
1196
1197 /* If we have set the ASSOC_MSK and we are in BSS mode then
1198 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001199 if (iwl3945_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001200 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001201 if (iwl3945_add_station(priv, priv->active_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -07001202 == IWL_INVALID_STATION) {
1203 IWL_ERROR("Error adding AP address for transmit.\n");
1204 return -EIO;
1205 }
1206
1207 /* Init the hardware's rate fallback order based on the
1208 * phymode */
1209 rc = iwl3945_init_hw_rate_table(priv);
1210 if (rc) {
1211 IWL_ERROR("Error setting HW rate table: %02X\n", rc);
1212 return -EIO;
1213 }
1214
1215 return 0;
1216}
1217
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001218static int iwl3945_send_bt_config(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001219{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001220 struct iwl3945_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001221 .flags = 3,
1222 .lead_time = 0xAA,
1223 .max_kill = 1,
1224 .kill_ack_mask = 0,
1225 .kill_cts_mask = 0,
1226 };
1227
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001228 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1229 sizeof(struct iwl3945_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001230}
1231
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001232static int iwl3945_send_scan_abort(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001233{
1234 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001235 struct iwl3945_rx_packet *res;
1236 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001237 .id = REPLY_SCAN_ABORT_CMD,
1238 .meta.flags = CMD_WANT_SKB,
1239 };
1240
1241 /* If there isn't a scan actively going on in the hardware
1242 * then we are in between scan bands and not actually
1243 * actively scanning, so don't send the abort command */
1244 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1245 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1246 return 0;
1247 }
1248
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001249 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001250 if (rc) {
1251 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1252 return rc;
1253 }
1254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001255 res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001256 if (res->u.status != CAN_ABORT_STATUS) {
1257 /* The scan abort will return 1 for success or
1258 * 2 for "failure". A failure condition can be
1259 * due to simply not being in an active scan which
1260 * can occur if we send the scan abort before we
1261 * the microcode has notified us that a scan is
1262 * completed. */
1263 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1264 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1265 clear_bit(STATUS_SCAN_HW, &priv->status);
1266 }
1267
1268 dev_kfree_skb_any(cmd.meta.u.skb);
1269
1270 return rc;
1271}
1272
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001273static int iwl3945_card_state_sync_callback(struct iwl3945_priv *priv,
1274 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001275 struct sk_buff *skb)
1276{
1277 return 1;
1278}
1279
1280/*
1281 * CARD_STATE_CMD
1282 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001283 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001284 *
1285 * When in the 'enable' state the card operates as normal.
1286 * When in the 'disable' state, the card enters into a low power mode.
1287 * When in the 'halt' state, the card is shut down and must be fully
1288 * restarted to come back on.
1289 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001290static int iwl3945_send_card_state(struct iwl3945_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001291{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001292 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001293 .id = REPLY_CARD_STATE_CMD,
1294 .len = sizeof(u32),
1295 .data = &flags,
1296 .meta.flags = meta_flag,
1297 };
1298
1299 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001300 cmd.meta.u.callback = iwl3945_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001301
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001302 return iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001303}
1304
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001305static int iwl3945_add_sta_sync_callback(struct iwl3945_priv *priv,
1306 struct iwl3945_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001307{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001308 struct iwl3945_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001309
1310 if (!skb) {
1311 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1312 return 1;
1313 }
1314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001315 res = (struct iwl3945_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001316 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1317 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1318 res->hdr.flags);
1319 return 1;
1320 }
1321
1322 switch (res->u.add_sta.status) {
1323 case ADD_STA_SUCCESS_MSK:
1324 break;
1325 default:
1326 break;
1327 }
1328
1329 /* We didn't cache the SKB; let the caller free it */
1330 return 1;
1331}
1332
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001333int iwl3945_send_add_station(struct iwl3945_priv *priv,
1334 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001335{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001336 struct iwl3945_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001337 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001338 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001339 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001340 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001341 .meta.flags = flags,
1342 .data = sta,
1343 };
1344
1345 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001346 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001347 else
1348 cmd.meta.flags |= CMD_WANT_SKB;
1349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001350 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001351
1352 if (rc || (flags & CMD_ASYNC))
1353 return rc;
1354
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001355 res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001356 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1357 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1358 res->hdr.flags);
1359 rc = -EIO;
1360 }
1361
1362 if (rc == 0) {
1363 switch (res->u.add_sta.status) {
1364 case ADD_STA_SUCCESS_MSK:
1365 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1366 break;
1367 default:
1368 rc = -EIO;
1369 IWL_WARNING("REPLY_ADD_STA failed\n");
1370 break;
1371 }
1372 }
1373
1374 priv->alloc_rxb_skb--;
1375 dev_kfree_skb_any(cmd.meta.u.skb);
1376
1377 return rc;
1378}
1379
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001380static int iwl3945_update_sta_key_info(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001381 struct ieee80211_key_conf *keyconf,
1382 u8 sta_id)
1383{
1384 unsigned long flags;
1385 __le16 key_flags = 0;
1386
1387 switch (keyconf->alg) {
1388 case ALG_CCMP:
1389 key_flags |= STA_KEY_FLG_CCMP;
1390 key_flags |= cpu_to_le16(
1391 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1392 key_flags &= ~STA_KEY_FLG_INVALID;
1393 break;
1394 case ALG_TKIP:
1395 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001396 default:
1397 return -EINVAL;
1398 }
1399 spin_lock_irqsave(&priv->sta_lock, flags);
1400 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1401 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1402 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1403 keyconf->keylen);
1404
1405 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1406 keyconf->keylen);
1407 priv->stations[sta_id].sta.key.key_flags = key_flags;
1408 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1409 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1410
1411 spin_unlock_irqrestore(&priv->sta_lock, flags);
1412
1413 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001414 iwl3945_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001415 return 0;
1416}
1417
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001418static int iwl3945_clear_sta_key_info(struct iwl3945_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001419{
1420 unsigned long flags;
1421
1422 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001423 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
1424 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl3945_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001425 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1426 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1427 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1428 spin_unlock_irqrestore(&priv->sta_lock, flags);
1429
1430 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001431 iwl3945_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001432 return 0;
1433}
1434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001435static void iwl3945_clear_free_frames(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001436{
1437 struct list_head *element;
1438
1439 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1440 priv->frames_count);
1441
1442 while (!list_empty(&priv->free_frames)) {
1443 element = priv->free_frames.next;
1444 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001445 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001446 priv->frames_count--;
1447 }
1448
1449 if (priv->frames_count) {
1450 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1451 priv->frames_count);
1452 priv->frames_count = 0;
1453 }
1454}
1455
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001456static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001457{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001458 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001459 struct list_head *element;
1460 if (list_empty(&priv->free_frames)) {
1461 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1462 if (!frame) {
1463 IWL_ERROR("Could not allocate frame!\n");
1464 return NULL;
1465 }
1466
1467 priv->frames_count++;
1468 return frame;
1469 }
1470
1471 element = priv->free_frames.next;
1472 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001473 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001474}
1475
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001476static void iwl3945_free_frame(struct iwl3945_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001477{
1478 memset(frame, 0, sizeof(*frame));
1479 list_add(&frame->list, &priv->free_frames);
1480}
1481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001482unsigned int iwl3945_fill_beacon_frame(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001483 struct ieee80211_hdr *hdr,
1484 const u8 *dest, int left)
1485{
1486
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001487 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001488 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1489 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1490 return 0;
1491
1492 if (priv->ibss_beacon->len > left)
1493 return 0;
1494
1495 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1496
1497 return priv->ibss_beacon->len;
1498}
1499
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001500static u8 iwl3945_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001501{
1502 u8 i;
1503
1504 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001505 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001506 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001507 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001508 }
1509
1510 return IWL_RATE_INVALID;
1511}
1512
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001513static int iwl3945_send_beacon_cmd(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001514{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001515 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001516 unsigned int frame_size;
1517 int rc;
1518 u8 rate;
1519
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001520 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001521
1522 if (!frame) {
1523 IWL_ERROR("Could not obtain free frame buffer for beacon "
1524 "command.\n");
1525 return -ENOMEM;
1526 }
1527
1528 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001529 rate = iwl3945_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001530 0xFF0);
1531 if (rate == IWL_INVALID_RATE)
1532 rate = IWL_RATE_6M_PLCP;
1533 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001534 rate = iwl3945_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001535 if (rate == IWL_INVALID_RATE)
1536 rate = IWL_RATE_1M_PLCP;
1537 }
1538
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001539 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001541 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001542 &frame->u.cmd[0]);
1543
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001544 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001545
1546 return rc;
1547}
1548
1549/******************************************************************************
1550 *
1551 * EEPROM related functions
1552 *
1553 ******************************************************************************/
1554
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001555static void get_eeprom_mac(struct iwl3945_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001556{
1557 memcpy(mac, priv->eeprom.mac_address, 6);
1558}
1559
Reinette Chatre74a3a252008-01-23 10:15:19 -08001560/*
1561 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1562 * embedded controller) as EEPROM reader; each read is a series of pulses
1563 * to/from the EEPROM chip, not a single event, so even reads could conflict
1564 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1565 * simply claims ownership, which should be safe when this function is called
1566 * (i.e. before loading uCode!).
1567 */
1568static inline int iwl3945_eeprom_acquire_semaphore(struct iwl3945_priv *priv)
1569{
1570 _iwl3945_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
1571 return 0;
1572}
1573
Zhu Yib481de92007-09-25 17:54:57 -07001574/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001575 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001576 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001577 * Load the EEPROM contents from adapter into priv->eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001578 *
1579 * NOTE: This routine uses the non-debug IO access functions.
1580 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001581int iwl3945_eeprom_init(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001582{
Al Viro0e5ce1f2007-12-22 13:45:50 -05001583 __le16 *e = (__le16 *)&priv->eeprom;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001584 u32 gp = iwl3945_read32(priv, CSR_EEPROM_GP);
Zhu Yib481de92007-09-25 17:54:57 -07001585 u32 r;
1586 int sz = sizeof(priv->eeprom);
1587 int rc;
1588 int i;
1589 u16 addr;
1590
1591 /* The EEPROM structure has several padding buffers within it
1592 * and when adding new EEPROM maps is subject to programmer errors
1593 * which may be very difficult to identify without explicitly
1594 * checking the resulting size of the eeprom map. */
1595 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1596
1597 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1598 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1599 return -ENOENT;
1600 }
1601
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001602 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001603 rc = iwl3945_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 _iwl3945_write32(priv, CSR_EEPROM_REG, addr << 1);
1612 _iwl3945_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 = _iwl3945_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 return -ETIMEDOUT;
1625 }
Al Viro0e5ce1f2007-12-22 13:45:50 -05001626 e[addr / 2] = cpu_to_le16(r >> 16);
Zhu Yib481de92007-09-25 17:54:57 -07001627 }
1628
1629 return 0;
1630}
1631
1632/******************************************************************************
1633 *
1634 * Misc. internal state and helper functions
1635 *
1636 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001637#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001638
1639/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001640 * iwl3945_report_frame - dump frame to syslog during debug sessions
Zhu Yib481de92007-09-25 17:54:57 -07001641 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001642 * You may hack this function to show different aspects of received frames,
Zhu Yib481de92007-09-25 17:54:57 -07001643 * including selective frame dumps.
1644 * group100 parameter selects whether to show 1 out of 100 good frames.
Zhu Yib481de92007-09-25 17:54:57 -07001645 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001646void iwl3945_report_frame(struct iwl3945_priv *priv,
1647 struct iwl3945_rx_packet *pkt,
Zhu Yib481de92007-09-25 17:54:57 -07001648 struct ieee80211_hdr *header, int group100)
1649{
1650 u32 to_us;
1651 u32 print_summary = 0;
1652 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1653 u32 hundred = 0;
1654 u32 dataframe = 0;
1655 u16 fc;
1656 u16 seq_ctl;
1657 u16 channel;
1658 u16 phy_flags;
1659 int rate_sym;
1660 u16 length;
1661 u16 status;
1662 u16 bcn_tmr;
1663 u32 tsf_low;
1664 u64 tsf;
1665 u8 rssi;
1666 u8 agc;
1667 u16 sig_avg;
1668 u16 noise_diff;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001669 struct iwl3945_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1670 struct iwl3945_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1671 struct iwl3945_rx_frame_end *rx_end = IWL_RX_END(pkt);
Zhu Yib481de92007-09-25 17:54:57 -07001672 u8 *data = IWL_RX_DATA(pkt);
1673
1674 /* MAC header */
1675 fc = le16_to_cpu(header->frame_control);
1676 seq_ctl = le16_to_cpu(header->seq_ctrl);
1677
1678 /* metadata */
1679 channel = le16_to_cpu(rx_hdr->channel);
1680 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1681 rate_sym = rx_hdr->rate;
1682 length = le16_to_cpu(rx_hdr->len);
1683
1684 /* end-of-frame status and timestamp */
1685 status = le32_to_cpu(rx_end->status);
1686 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1687 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1688 tsf = le64_to_cpu(rx_end->timestamp);
1689
1690 /* signal statistics */
1691 rssi = rx_stats->rssi;
1692 agc = rx_stats->agc;
1693 sig_avg = le16_to_cpu(rx_stats->sig_avg);
1694 noise_diff = le16_to_cpu(rx_stats->noise_diff);
1695
1696 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1697
1698 /* if data frame is to us and all is good,
1699 * (optionally) print summary for only 1 out of every 100 */
1700 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1701 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1702 dataframe = 1;
1703 if (!group100)
1704 print_summary = 1; /* print each frame */
1705 else if (priv->framecnt_to_us < 100) {
1706 priv->framecnt_to_us++;
1707 print_summary = 0;
1708 } else {
1709 priv->framecnt_to_us = 0;
1710 print_summary = 1;
1711 hundred = 1;
1712 }
1713 } else {
1714 /* print summary for all other frames */
1715 print_summary = 1;
1716 }
1717
1718 if (print_summary) {
1719 char *title;
1720 u32 rate;
1721
1722 if (hundred)
1723 title = "100Frames";
1724 else if (fc & IEEE80211_FCTL_RETRY)
1725 title = "Retry";
1726 else if (ieee80211_is_assoc_response(fc))
1727 title = "AscRsp";
1728 else if (ieee80211_is_reassoc_response(fc))
1729 title = "RasRsp";
1730 else if (ieee80211_is_probe_response(fc)) {
1731 title = "PrbRsp";
1732 print_dump = 1; /* dump frame contents */
1733 } else if (ieee80211_is_beacon(fc)) {
1734 title = "Beacon";
1735 print_dump = 1; /* dump frame contents */
1736 } else if (ieee80211_is_atim(fc))
1737 title = "ATIM";
1738 else if (ieee80211_is_auth(fc))
1739 title = "Auth";
1740 else if (ieee80211_is_deauth(fc))
1741 title = "DeAuth";
1742 else if (ieee80211_is_disassoc(fc))
1743 title = "DisAssoc";
1744 else
1745 title = "Frame";
1746
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001747 rate = iwl3945_rate_index_from_plcp(rate_sym);
Zhu Yib481de92007-09-25 17:54:57 -07001748 if (rate == -1)
1749 rate = 0;
1750 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001751 rate = iwl3945_rates[rate].ieee / 2;
Zhu Yib481de92007-09-25 17:54:57 -07001752
1753 /* print frame summary.
1754 * MAC addresses show just the last byte (for brevity),
1755 * but you can hack it to show more, if you'd like to. */
1756 if (dataframe)
1757 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1758 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1759 title, fc, header->addr1[5],
1760 length, rssi, channel, rate);
1761 else {
1762 /* src/dst addresses assume managed mode */
1763 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1764 "src=0x%02x, rssi=%u, tim=%lu usec, "
1765 "phy=0x%02x, chnl=%d\n",
1766 title, fc, header->addr1[5],
1767 header->addr3[5], rssi,
1768 tsf_low - priv->scan_start_tsf,
1769 phy_flags, channel);
1770 }
1771 }
1772 if (print_dump)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001773 iwl3945_print_hex_dump(IWL_DL_RX, data, length);
Zhu Yib481de92007-09-25 17:54:57 -07001774}
1775#endif
1776
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001777static void iwl3945_unset_hw_setting(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001778{
1779 if (priv->hw_setting.shared_virt)
1780 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001781 sizeof(struct iwl3945_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001782 priv->hw_setting.shared_virt,
1783 priv->hw_setting.shared_phys);
1784}
1785
1786/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001787 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001788 *
1789 * return : set the bit for each supported rate insert in ie
1790 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001791static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001792 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001793{
1794 u16 ret_rates = 0, bit;
1795 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001796 u8 *cnt = ie;
1797 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001798
1799 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1800 if (bit & supported_rate) {
1801 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001802 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001803 ((bit & basic_rate) ? 0x80 : 0x00);
1804 (*cnt)++;
1805 (*left)--;
1806 if ((*left <= 0) ||
1807 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001808 break;
1809 }
1810 }
1811
1812 return ret_rates;
1813}
1814
1815/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001816 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001817 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001818static u16 iwl3945_fill_probe_req(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001819 struct ieee80211_mgmt *frame,
1820 int left, int is_direct)
1821{
1822 int len = 0;
1823 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001824 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001825
1826 /* Make sure there is enough space for the probe request,
1827 * two mandatory IEs and the data */
1828 left -= 24;
1829 if (left < 0)
1830 return 0;
1831 len += 24;
1832
1833 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001834 memcpy(frame->da, iwl3945_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001835 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001836 memcpy(frame->bssid, iwl3945_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001837 frame->seq_ctrl = 0;
1838
1839 /* fill in our indirect SSID IE */
1840 /* ...next IE... */
1841
1842 left -= 2;
1843 if (left < 0)
1844 return 0;
1845 len += 2;
1846 pos = &(frame->u.probe_req.variable[0]);
1847 *pos++ = WLAN_EID_SSID;
1848 *pos++ = 0;
1849
1850 /* fill in our direct SSID IE... */
1851 if (is_direct) {
1852 /* ...next IE... */
1853 left -= 2 + priv->essid_len;
1854 if (left < 0)
1855 return 0;
1856 /* ... fill it in... */
1857 *pos++ = WLAN_EID_SSID;
1858 *pos++ = priv->essid_len;
1859 memcpy(pos, priv->essid, priv->essid_len);
1860 pos += priv->essid_len;
1861 len += 2 + priv->essid_len;
1862 }
1863
1864 /* fill in supported rate */
1865 /* ...next IE... */
1866 left -= 2;
1867 if (left < 0)
1868 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001869
Zhu Yib481de92007-09-25 17:54:57 -07001870 /* ... fill it in... */
1871 *pos++ = WLAN_EID_SUPP_RATES;
1872 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001873
1874 priv->active_rate = priv->rates_mask;
1875 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001876 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1877
Tomas Winklerc7c46672007-10-18 02:04:15 +02001878 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001879 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001880 priv->active_rate_basic, &left);
1881 active_rates &= ~ret_rates;
1882
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001883 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001884 priv->active_rate_basic, &left);
1885 active_rates &= ~ret_rates;
1886
Zhu Yib481de92007-09-25 17:54:57 -07001887 len += 2 + *pos;
1888 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001889 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001890 goto fill_end;
1891
1892 /* fill in supported extended rate */
1893 /* ...next IE... */
1894 left -= 2;
1895 if (left < 0)
1896 return 0;
1897 /* ... fill it in... */
1898 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1899 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001900 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001901 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001902 if (*pos > 0)
1903 len += 2 + *pos;
1904
1905 fill_end:
1906 return (u16)len;
1907}
1908
1909/*
1910 * QoS support
1911*/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001912#ifdef CONFIG_IWL3945_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001913static int iwl3945_send_qos_params_command(struct iwl3945_priv *priv,
1914 struct iwl3945_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001915{
1916
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001917 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1918 sizeof(struct iwl3945_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001919}
1920
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001921static void iwl3945_reset_qos(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001922{
1923 u16 cw_min = 15;
1924 u16 cw_max = 1023;
1925 u8 aifs = 2;
1926 u8 is_legacy = 0;
1927 unsigned long flags;
1928 int i;
1929
1930 spin_lock_irqsave(&priv->lock, flags);
1931 priv->qos_data.qos_active = 0;
1932
1933 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1934 if (priv->qos_data.qos_enable)
1935 priv->qos_data.qos_active = 1;
1936 if (!(priv->active_rate & 0xfff0)) {
1937 cw_min = 31;
1938 is_legacy = 1;
1939 }
1940 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1941 if (priv->qos_data.qos_enable)
1942 priv->qos_data.qos_active = 1;
1943 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1944 cw_min = 31;
1945 is_legacy = 1;
1946 }
1947
1948 if (priv->qos_data.qos_active)
1949 aifs = 3;
1950
1951 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1952 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1953 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1954 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1955 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1956
1957 if (priv->qos_data.qos_active) {
1958 i = 1;
1959 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1960 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1961 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1962 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1963 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1964
1965 i = 2;
1966 priv->qos_data.def_qos_parm.ac[i].cw_min =
1967 cpu_to_le16((cw_min + 1) / 2 - 1);
1968 priv->qos_data.def_qos_parm.ac[i].cw_max =
1969 cpu_to_le16(cw_max);
1970 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1971 if (is_legacy)
1972 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1973 cpu_to_le16(6016);
1974 else
1975 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1976 cpu_to_le16(3008);
1977 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1978
1979 i = 3;
1980 priv->qos_data.def_qos_parm.ac[i].cw_min =
1981 cpu_to_le16((cw_min + 1) / 4 - 1);
1982 priv->qos_data.def_qos_parm.ac[i].cw_max =
1983 cpu_to_le16((cw_max + 1) / 2 - 1);
1984 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1985 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1986 if (is_legacy)
1987 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1988 cpu_to_le16(3264);
1989 else
1990 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1991 cpu_to_le16(1504);
1992 } else {
1993 for (i = 1; i < 4; i++) {
1994 priv->qos_data.def_qos_parm.ac[i].cw_min =
1995 cpu_to_le16(cw_min);
1996 priv->qos_data.def_qos_parm.ac[i].cw_max =
1997 cpu_to_le16(cw_max);
1998 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1999 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2000 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2001 }
2002 }
2003 IWL_DEBUG_QOS("set QoS to default \n");
2004
2005 spin_unlock_irqrestore(&priv->lock, flags);
2006}
2007
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002008static void iwl3945_activate_qos(struct iwl3945_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07002009{
2010 unsigned long flags;
2011
Zhu Yib481de92007-09-25 17:54:57 -07002012 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2013 return;
2014
2015 if (!priv->qos_data.qos_enable)
2016 return;
2017
2018 spin_lock_irqsave(&priv->lock, flags);
2019 priv->qos_data.def_qos_parm.qos_flags = 0;
2020
2021 if (priv->qos_data.qos_cap.q_AP.queue_request &&
2022 !priv->qos_data.qos_cap.q_AP.txop_request)
2023 priv->qos_data.def_qos_parm.qos_flags |=
2024 QOS_PARAM_FLG_TXOP_TYPE_MSK;
2025
2026 if (priv->qos_data.qos_active)
2027 priv->qos_data.def_qos_parm.qos_flags |=
2028 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2029
2030 spin_unlock_irqrestore(&priv->lock, flags);
2031
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002032 if (force || iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002033 IWL_DEBUG_QOS("send QoS cmd with Qos active %d \n",
2034 priv->qos_data.qos_active);
2035
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002036 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002037 &(priv->qos_data.def_qos_parm));
2038 }
2039}
2040
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002041#endif /* CONFIG_IWL3945_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07002042/*
2043 * Power management (not Tx power!) functions
2044 */
2045#define MSEC_TO_USEC 1024
2046
2047#define NOSLP __constant_cpu_to_le32(0)
2048#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
2049#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2050#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2051 __constant_cpu_to_le32(X1), \
2052 __constant_cpu_to_le32(X2), \
2053 __constant_cpu_to_le32(X3), \
2054 __constant_cpu_to_le32(X4)}
2055
2056
2057/* default power management (not Tx power) table values */
2058/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002059static struct iwl3945_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07002060 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2061 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2062 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2063 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2064 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2065 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2066};
2067
2068/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002069static struct iwl3945_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07002070 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2071 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2072 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2073 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2074 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2075 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2076 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2077 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2078 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2079 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2080};
2081
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002082int iwl3945_power_init_handle(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002083{
2084 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002085 struct iwl3945_power_mgr *pow_data;
2086 int size = sizeof(struct iwl3945_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07002087 u16 pci_pm;
2088
2089 IWL_DEBUG_POWER("Initialize power \n");
2090
2091 pow_data = &(priv->power_data);
2092
2093 memset(pow_data, 0, sizeof(*pow_data));
2094
2095 pow_data->active_index = IWL_POWER_RANGE_0;
2096 pow_data->dtim_val = 0xffff;
2097
2098 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2099 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2100
2101 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2102 if (rc != 0)
2103 return 0;
2104 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002105 struct iwl3945_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002106
2107 IWL_DEBUG_POWER("adjust power command flags\n");
2108
2109 for (i = 0; i < IWL_POWER_AC; i++) {
2110 cmd = &pow_data->pwr_range_0[i].cmd;
2111
2112 if (pci_pm & 0x1)
2113 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2114 else
2115 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2116 }
2117 }
2118 return rc;
2119}
2120
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002121static int iwl3945_update_power_cmd(struct iwl3945_priv *priv,
2122 struct iwl3945_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002123{
2124 int rc = 0, i;
2125 u8 skip;
2126 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002127 struct iwl3945_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07002128 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002129 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07002130
2131 if (mode > IWL_POWER_INDEX_5) {
2132 IWL_DEBUG_POWER("Error invalid power mode \n");
2133 return -1;
2134 }
2135 pow_data = &(priv->power_data);
2136
2137 if (pow_data->active_index == IWL_POWER_RANGE_0)
2138 range = &pow_data->pwr_range_0[0];
2139 else
2140 range = &pow_data->pwr_range_1[1];
2141
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002142 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07002143
2144#ifdef IWL_MAC80211_DISABLE
2145 if (priv->assoc_network != NULL) {
2146 unsigned long flags;
2147
2148 period = priv->assoc_network->tim.tim_period;
2149 }
2150#endif /*IWL_MAC80211_DISABLE */
2151 skip = range[mode].no_dtim;
2152
2153 if (period == 0) {
2154 period = 1;
2155 skip = 0;
2156 }
2157
2158 if (skip == 0) {
2159 max_sleep = period;
2160 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2161 } else {
2162 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2163 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2164 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2165 }
2166
2167 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2168 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2169 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2170 }
2171
2172 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2173 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2174 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2175 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2176 le32_to_cpu(cmd->sleep_interval[0]),
2177 le32_to_cpu(cmd->sleep_interval[1]),
2178 le32_to_cpu(cmd->sleep_interval[2]),
2179 le32_to_cpu(cmd->sleep_interval[3]),
2180 le32_to_cpu(cmd->sleep_interval[4]));
2181
2182 return rc;
2183}
2184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002185static int iwl3945_send_power_mode(struct iwl3945_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002186{
John W. Linville9a62f732007-11-15 16:27:36 -05002187 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002188 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002189 struct iwl3945_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002190
2191 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08002192 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07002193 * else user level */
2194 switch (mode) {
2195 case IWL_POWER_BATTERY:
2196 final_mode = IWL_POWER_INDEX_3;
2197 break;
2198 case IWL_POWER_AC:
2199 final_mode = IWL_POWER_MODE_CAM;
2200 break;
2201 default:
2202 final_mode = mode;
2203 break;
2204 }
2205
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002206 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002207
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002208 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002209
2210 if (final_mode == IWL_POWER_MODE_CAM)
2211 clear_bit(STATUS_POWER_PMI, &priv->status);
2212 else
2213 set_bit(STATUS_POWER_PMI, &priv->status);
2214
2215 return rc;
2216}
2217
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002218int iwl3945_is_network_packet(struct iwl3945_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002219{
2220 /* Filter incoming packets to determine if they are targeted toward
2221 * this network, discarding packets coming from ourselves */
2222 switch (priv->iw_mode) {
2223 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2224 /* packets from our adapter are dropped (echo) */
2225 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2226 return 0;
2227 /* {broad,multi}cast packets to our IBSS go through */
2228 if (is_multicast_ether_addr(header->addr1))
2229 return !compare_ether_addr(header->addr3, priv->bssid);
2230 /* packets to our adapter go through */
2231 return !compare_ether_addr(header->addr1, priv->mac_addr);
2232 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2233 /* packets from our adapter are dropped (echo) */
2234 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2235 return 0;
2236 /* {broad,multi}cast packets to our BSS go through */
2237 if (is_multicast_ether_addr(header->addr1))
2238 return !compare_ether_addr(header->addr2, priv->bssid);
2239 /* packets to our adapter go through */
2240 return !compare_ether_addr(header->addr1, priv->mac_addr);
2241 }
2242
2243 return 1;
2244}
2245
2246#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2247
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002248static const char *iwl3945_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002249{
2250 switch (status & TX_STATUS_MSK) {
2251 case TX_STATUS_SUCCESS:
2252 return "SUCCESS";
2253 TX_STATUS_ENTRY(SHORT_LIMIT);
2254 TX_STATUS_ENTRY(LONG_LIMIT);
2255 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2256 TX_STATUS_ENTRY(MGMNT_ABORT);
2257 TX_STATUS_ENTRY(NEXT_FRAG);
2258 TX_STATUS_ENTRY(LIFE_EXPIRE);
2259 TX_STATUS_ENTRY(DEST_PS);
2260 TX_STATUS_ENTRY(ABORTED);
2261 TX_STATUS_ENTRY(BT_RETRY);
2262 TX_STATUS_ENTRY(STA_INVALID);
2263 TX_STATUS_ENTRY(FRAG_DROPPED);
2264 TX_STATUS_ENTRY(TID_DISABLE);
2265 TX_STATUS_ENTRY(FRAME_FLUSHED);
2266 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2267 TX_STATUS_ENTRY(TX_LOCKED);
2268 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2269 }
2270
2271 return "UNKNOWN";
2272}
2273
2274/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002275 * iwl3945_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002276 *
2277 * NOTE: priv->mutex is not required before calling this function
2278 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002279static int iwl3945_scan_cancel(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002280{
2281 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2282 clear_bit(STATUS_SCANNING, &priv->status);
2283 return 0;
2284 }
2285
2286 if (test_bit(STATUS_SCANNING, &priv->status)) {
2287 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2288 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2289 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2290 queue_work(priv->workqueue, &priv->abort_scan);
2291
2292 } else
2293 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2294
2295 return test_bit(STATUS_SCANNING, &priv->status);
2296 }
2297
2298 return 0;
2299}
2300
2301/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002302 * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002303 * @ms: amount of time to wait (in milliseconds) for scan to abort
2304 *
2305 * NOTE: priv->mutex must be held before calling this function
2306 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002307static int iwl3945_scan_cancel_timeout(struct iwl3945_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002308{
2309 unsigned long now = jiffies;
2310 int ret;
2311
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002312 ret = iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002313 if (ret && ms) {
2314 mutex_unlock(&priv->mutex);
2315 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2316 test_bit(STATUS_SCANNING, &priv->status))
2317 msleep(1);
2318 mutex_lock(&priv->mutex);
2319
2320 return test_bit(STATUS_SCANNING, &priv->status);
2321 }
2322
2323 return ret;
2324}
2325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002326static void iwl3945_sequence_reset(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002327{
2328 /* Reset ieee stats */
2329
2330 /* We don't reset the net_device_stats (ieee->stats) on
2331 * re-association */
2332
2333 priv->last_seq_num = -1;
2334 priv->last_frag_num = -1;
2335 priv->last_packet_time = 0;
2336
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002337 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002338}
2339
2340#define MAX_UCODE_BEACON_INTERVAL 1024
2341#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2342
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002343static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002344{
2345 u16 new_val = 0;
2346 u16 beacon_factor = 0;
2347
2348 beacon_factor =
2349 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2350 / MAX_UCODE_BEACON_INTERVAL;
2351 new_val = beacon_val / beacon_factor;
2352
2353 return cpu_to_le16(new_val);
2354}
2355
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002356static void iwl3945_setup_rxon_timing(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002357{
2358 u64 interval_tm_unit;
2359 u64 tsf, result;
2360 unsigned long flags;
2361 struct ieee80211_conf *conf = NULL;
2362 u16 beacon_int = 0;
2363
2364 conf = ieee80211_get_hw_conf(priv->hw);
2365
2366 spin_lock_irqsave(&priv->lock, flags);
2367 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2368 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2369
2370 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2371
2372 tsf = priv->timestamp1;
2373 tsf = ((tsf << 32) | priv->timestamp0);
2374
2375 beacon_int = priv->beacon_int;
2376 spin_unlock_irqrestore(&priv->lock, flags);
2377
2378 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2379 if (beacon_int == 0) {
2380 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2381 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2382 } else {
2383 priv->rxon_timing.beacon_interval =
2384 cpu_to_le16(beacon_int);
2385 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002386 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002387 le16_to_cpu(priv->rxon_timing.beacon_interval));
2388 }
2389
2390 priv->rxon_timing.atim_window = 0;
2391 } else {
2392 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002393 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002394 /* TODO: we need to get atim_window from upper stack
2395 * for now we set to 0 */
2396 priv->rxon_timing.atim_window = 0;
2397 }
2398
2399 interval_tm_unit =
2400 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2401 result = do_div(tsf, interval_tm_unit);
2402 priv->rxon_timing.beacon_init_val =
2403 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2404
2405 IWL_DEBUG_ASSOC
2406 ("beacon interval %d beacon timer %d beacon tim %d\n",
2407 le16_to_cpu(priv->rxon_timing.beacon_interval),
2408 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2409 le16_to_cpu(priv->rxon_timing.atim_window));
2410}
2411
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002412static int iwl3945_scan_initiate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002413{
2414 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2415 IWL_ERROR("APs don't scan.\n");
2416 return 0;
2417 }
2418
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002419 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002420 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2421 return -EIO;
2422 }
2423
2424 if (test_bit(STATUS_SCANNING, &priv->status)) {
2425 IWL_DEBUG_SCAN("Scan already in progress.\n");
2426 return -EAGAIN;
2427 }
2428
2429 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2430 IWL_DEBUG_SCAN("Scan request while abort pending. "
2431 "Queuing.\n");
2432 return -EAGAIN;
2433 }
2434
2435 IWL_DEBUG_INFO("Starting scan...\n");
2436 priv->scan_bands = 2;
2437 set_bit(STATUS_SCANNING, &priv->status);
2438 priv->scan_start = jiffies;
2439 priv->scan_pass_start = priv->scan_start;
2440
2441 queue_work(priv->workqueue, &priv->request_scan);
2442
2443 return 0;
2444}
2445
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002446static int iwl3945_set_rxon_hwcrypto(struct iwl3945_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002447{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002448 struct iwl3945_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002449
2450 if (hw_decrypt)
2451 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2452 else
2453 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2454
2455 return 0;
2456}
2457
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002458static void iwl3945_set_flags_for_phymode(struct iwl3945_priv *priv, u8 phymode)
Zhu Yib481de92007-09-25 17:54:57 -07002459{
2460 if (phymode == MODE_IEEE80211A) {
2461 priv->staging_rxon.flags &=
2462 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2463 | RXON_FLG_CCK_MSK);
2464 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2465 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002466 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002467 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2468 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2469 else
2470 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2471
2472 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2473 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2474
2475 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2476 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2477 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2478 }
2479}
2480
2481/*
Ian Schram01ebd062007-10-25 17:15:22 +08002482 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002483 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002484static void iwl3945_connection_init_rx_config(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002485{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002486 const struct iwl3945_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002487
2488 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2489
2490 switch (priv->iw_mode) {
2491 case IEEE80211_IF_TYPE_AP:
2492 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2493 break;
2494
2495 case IEEE80211_IF_TYPE_STA:
2496 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2497 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2498 break;
2499
2500 case IEEE80211_IF_TYPE_IBSS:
2501 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2502 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2503 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2504 RXON_FILTER_ACCEPT_GRP_MSK;
2505 break;
2506
2507 case IEEE80211_IF_TYPE_MNTR:
2508 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2509 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2510 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2511 break;
2512 }
2513
2514#if 0
2515 /* TODO: Figure out when short_preamble would be set and cache from
2516 * that */
2517 if (!hw_to_local(priv->hw)->short_preamble)
2518 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2519 else
2520 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2521#endif
2522
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002523 ch_info = iwl3945_get_channel_info(priv, priv->phymode,
Zhu Yib481de92007-09-25 17:54:57 -07002524 le16_to_cpu(priv->staging_rxon.channel));
2525
2526 if (!ch_info)
2527 ch_info = &priv->channel_info[0];
2528
2529 /*
2530 * in some case A channels are all non IBSS
2531 * in this case force B/G channel
2532 */
2533 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2534 !(is_channel_ibss(ch_info)))
2535 ch_info = &priv->channel_info[0];
2536
2537 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2538 if (is_channel_a_band(ch_info))
2539 priv->phymode = MODE_IEEE80211A;
2540 else
2541 priv->phymode = MODE_IEEE80211G;
2542
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002543 iwl3945_set_flags_for_phymode(priv, priv->phymode);
Zhu Yib481de92007-09-25 17:54:57 -07002544
2545 priv->staging_rxon.ofdm_basic_rates =
2546 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2547 priv->staging_rxon.cck_basic_rates =
2548 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2549}
2550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002551static int iwl3945_set_mode(struct iwl3945_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002552{
Zhu Yib481de92007-09-25 17:54:57 -07002553 if (mode == IEEE80211_IF_TYPE_IBSS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002554 const struct iwl3945_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002556 ch_info = iwl3945_get_channel_info(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002557 priv->phymode,
2558 le16_to_cpu(priv->staging_rxon.channel));
2559
2560 if (!ch_info || !is_channel_ibss(ch_info)) {
2561 IWL_ERROR("channel %d not IBSS channel\n",
2562 le16_to_cpu(priv->staging_rxon.channel));
2563 return -EINVAL;
2564 }
2565 }
2566
Zhu Yib481de92007-09-25 17:54:57 -07002567 priv->iw_mode = mode;
2568
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002569 iwl3945_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002570 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2571
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002572 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002573
Mohamed Abbasfde35712007-11-29 11:10:15 +08002574 /* dont commit rxon if rf-kill is on*/
2575 if (!iwl3945_is_ready_rf(priv))
2576 return -EAGAIN;
2577
2578 cancel_delayed_work(&priv->scan_check);
2579 if (iwl3945_scan_cancel_timeout(priv, 100)) {
2580 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2581 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2582 return -EAGAIN;
2583 }
2584
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002585 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002586
2587 return 0;
2588}
2589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002590static void iwl3945_build_tx_cmd_hwcrypto(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002591 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002592 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002593 struct sk_buff *skb_frag,
2594 int last_frag)
2595{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002596 struct iwl3945_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002597
2598 switch (keyinfo->alg) {
2599 case ALG_CCMP:
2600 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2601 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2602 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2603 break;
2604
2605 case ALG_TKIP:
2606#if 0
2607 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2608
2609 if (last_frag)
2610 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2611 8);
2612 else
2613 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2614#endif
2615 break;
2616
2617 case ALG_WEP:
2618 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2619 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2620
2621 if (keyinfo->keylen == 13)
2622 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2623
2624 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2625
2626 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2627 "with key %d\n", ctl->key_idx);
2628 break;
2629
Zhu Yib481de92007-09-25 17:54:57 -07002630 default:
2631 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2632 break;
2633 }
2634}
2635
2636/*
2637 * handle build REPLY_TX command notification.
2638 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002639static void iwl3945_build_tx_cmd_basic(struct iwl3945_priv *priv,
2640 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002641 struct ieee80211_tx_control *ctrl,
2642 struct ieee80211_hdr *hdr,
2643 int is_unicast, u8 std_id)
2644{
2645 __le16 *qc;
2646 u16 fc = le16_to_cpu(hdr->frame_control);
2647 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2648
2649 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2650 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2651 tx_flags |= TX_CMD_FLG_ACK_MSK;
2652 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2653 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2654 if (ieee80211_is_probe_response(fc) &&
2655 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2656 tx_flags |= TX_CMD_FLG_TSF_MSK;
2657 } else {
2658 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2659 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2660 }
2661
2662 cmd->cmd.tx.sta_id = std_id;
2663 if (ieee80211_get_morefrag(hdr))
2664 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2665
2666 qc = ieee80211_get_qos_ctrl(hdr);
2667 if (qc) {
2668 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2669 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2670 } else
2671 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2672
2673 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2674 tx_flags |= TX_CMD_FLG_RTS_MSK;
2675 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2676 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2677 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2678 tx_flags |= TX_CMD_FLG_CTS_MSK;
2679 }
2680
2681 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2682 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2683
2684 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2685 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2686 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2687 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002688 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002689 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002690 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002691 } else
2692 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2693
2694 cmd->cmd.tx.driver_txop = 0;
2695 cmd->cmd.tx.tx_flags = tx_flags;
2696 cmd->cmd.tx.next_frame_len = 0;
2697}
2698
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002699/**
2700 * iwl3945_get_sta_id - Find station's index within station table
2701 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002702static int iwl3945_get_sta_id(struct iwl3945_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002703{
2704 int sta_id;
2705 u16 fc = le16_to_cpu(hdr->frame_control);
2706
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002707 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002708 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2709 is_multicast_ether_addr(hdr->addr1))
2710 return priv->hw_setting.bcast_sta_id;
2711
2712 switch (priv->iw_mode) {
2713
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002714 /* If we are a client station in a BSS network, use the special
2715 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002716 case IEEE80211_IF_TYPE_STA:
2717 return IWL_AP_ID;
2718
2719 /* If we are an AP, then find the station, or use BCAST */
2720 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002721 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002722 if (sta_id != IWL_INVALID_STATION)
2723 return sta_id;
2724 return priv->hw_setting.bcast_sta_id;
2725
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002726 /* If this frame is going out to an IBSS network, find the station,
2727 * or create a new station table entry */
Joe Perches0795af52007-10-03 17:59:30 -07002728 case IEEE80211_IF_TYPE_IBSS: {
2729 DECLARE_MAC_BUF(mac);
2730
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002731 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002732 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002733 if (sta_id != IWL_INVALID_STATION)
2734 return sta_id;
2735
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002736 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002737
2738 if (sta_id != IWL_INVALID_STATION)
2739 return sta_id;
2740
Joe Perches0795af52007-10-03 17:59:30 -07002741 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002742 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002743 print_mac(mac, hdr->addr1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002744 iwl3945_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002745 return priv->hw_setting.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002746 }
Zhu Yib481de92007-09-25 17:54:57 -07002747 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002748 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002749 return priv->hw_setting.bcast_sta_id;
2750 }
2751}
2752
2753/*
2754 * start REPLY_TX command process
2755 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002756static int iwl3945_tx_skb(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002757 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2758{
2759 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002760 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002761 u32 *control_flags;
2762 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002763 struct iwl3945_tx_queue *txq = NULL;
2764 struct iwl3945_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002765 dma_addr_t phys_addr;
2766 dma_addr_t txcmd_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002767 struct iwl3945_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002768 u16 len, idx, len_org;
2769 u8 id, hdr_len, unicast;
2770 u8 sta_id;
2771 u16 seq_number = 0;
2772 u16 fc;
2773 __le16 *qc;
2774 u8 wait_write_ptr = 0;
2775 unsigned long flags;
2776 int rc;
2777
2778 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002779 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002780 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2781 goto drop_unlock;
2782 }
2783
Johannes Berg32bfd352007-12-19 01:31:26 +01002784 if (!priv->vif) {
2785 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002786 goto drop_unlock;
2787 }
2788
2789 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2790 IWL_ERROR("ERROR: No TX rate available.\n");
2791 goto drop_unlock;
2792 }
2793
2794 unicast = !is_multicast_ether_addr(hdr->addr1);
2795 id = 0;
2796
2797 fc = le16_to_cpu(hdr->frame_control);
2798
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002799#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002800 if (ieee80211_is_auth(fc))
2801 IWL_DEBUG_TX("Sending AUTH frame\n");
2802 else if (ieee80211_is_assoc_request(fc))
2803 IWL_DEBUG_TX("Sending ASSOC frame\n");
2804 else if (ieee80211_is_reassoc_request(fc))
2805 IWL_DEBUG_TX("Sending REASSOC frame\n");
2806#endif
2807
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002808 /* drop all data frame if we are not associated */
2809 if (!iwl3945_is_associated(priv) && !priv->assoc_id &&
Zhu Yib481de92007-09-25 17:54:57 -07002810 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002811 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002812 goto drop_unlock;
2813 }
2814
2815 spin_unlock_irqrestore(&priv->lock, flags);
2816
2817 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002818
2819 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002820 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002821 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002822 DECLARE_MAC_BUF(mac);
2823
2824 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2825 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002826 goto drop;
2827 }
2828
2829 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2830
2831 qc = ieee80211_get_qos_ctrl(hdr);
2832 if (qc) {
2833 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2834 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2835 IEEE80211_SCTL_SEQ;
2836 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2837 (hdr->seq_ctrl &
2838 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2839 seq_number += 0x10;
2840 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002841
2842 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002843 txq = &priv->txq[txq_id];
2844 q = &txq->q;
2845
2846 spin_lock_irqsave(&priv->lock, flags);
2847
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002848 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002849 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002850 memset(tfd, 0, sizeof(*tfd));
2851 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002852 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002853
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002854 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002855 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl3945_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002856 txq->txb[q->write_ptr].skb[0] = skb;
2857 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002858 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002859
2860 /* Init first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002861 out_cmd = &txq->cmd[idx];
2862 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2863 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002864
2865 /*
2866 * Set up the Tx-command (not MAC!) header.
2867 * Store the chosen Tx queue and TFD index within the sequence field;
2868 * after Tx, uCode's Tx response will return this value so driver can
2869 * locate the frame within the tx queue and do post-tx processing.
2870 */
Zhu Yib481de92007-09-25 17:54:57 -07002871 out_cmd->hdr.cmd = REPLY_TX;
2872 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002873 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002874
2875 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002876 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2877
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002878 /*
2879 * Use the first empty entry in this queue's command buffer array
2880 * to contain the Tx command and MAC header concatenated together
2881 * (payload data will be in another buffer).
2882 * Size of this varies, due to varying MAC header length.
2883 * If end is not dword aligned, we'll have 2 extra bytes at the end
2884 * of the MAC header (device reads on dword boundaries).
2885 * We'll tell device about this padding later.
2886 */
Zhu Yib481de92007-09-25 17:54:57 -07002887 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002888 sizeof(struct iwl3945_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002889
2890 len_org = len;
2891 len = (len + 3) & ~3;
2892
2893 if (len_org != len)
2894 len_org = 1;
2895 else
2896 len_org = 0;
2897
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002898 /* Physical address of this Tx command's header (not MAC header!),
2899 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002900 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl3945_cmd) * idx +
2901 offsetof(struct iwl3945_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002902
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002903 /* Add buffer containing Tx command and MAC(!) header to TFD's
2904 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002905 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002906
2907 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002908 iwl3945_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002909
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002910 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2911 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002912 len = skb->len - hdr_len;
2913 if (len) {
2914 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2915 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002916 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002917 }
2918
Zhu Yib481de92007-09-25 17:54:57 -07002919 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002920 /* If there is no payload, then we use only one Tx buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002921 *control_flags = TFD_CTL_COUNT_SET(1);
2922 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002923 /* Else use 2 buffers.
2924 * Tell 3945 about any padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002925 *control_flags = TFD_CTL_COUNT_SET(2) |
2926 TFD_CTL_PAD_SET(U32_PAD(len));
2927
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002928 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002929 len = (u16)skb->len;
2930 out_cmd->cmd.tx.len = cpu_to_le16(len);
2931
2932 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002933 iwl3945_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002934
2935 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002936 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002937
2938 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2939 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2940
2941 if (!ieee80211_get_morefrag(hdr)) {
2942 txq->need_update = 1;
2943 if (qc) {
2944 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2945 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2946 }
2947 } else {
2948 wait_write_ptr = 1;
2949 txq->need_update = 0;
2950 }
2951
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002952 iwl3945_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002953 sizeof(out_cmd->cmd.tx));
2954
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002955 iwl3945_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002956 ieee80211_get_hdrlen(fc));
2957
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002958 /* Tell device the write index *just past* this latest filled TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002959 q->write_ptr = iwl3945_queue_inc_wrap(q->write_ptr, q->n_bd);
2960 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002961 spin_unlock_irqrestore(&priv->lock, flags);
2962
2963 if (rc)
2964 return rc;
2965
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002966 if ((iwl3945_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002967 && priv->mac80211_registered) {
2968 if (wait_write_ptr) {
2969 spin_lock_irqsave(&priv->lock, flags);
2970 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002971 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002972 spin_unlock_irqrestore(&priv->lock, flags);
2973 }
2974
2975 ieee80211_stop_queue(priv->hw, ctl->queue);
2976 }
2977
2978 return 0;
2979
2980drop_unlock:
2981 spin_unlock_irqrestore(&priv->lock, flags);
2982drop:
2983 return -1;
2984}
2985
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002986static void iwl3945_set_rate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002987{
2988 const struct ieee80211_hw_mode *hw = NULL;
2989 struct ieee80211_rate *rate;
2990 int i;
2991
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002992 hw = iwl3945_get_hw_mode(priv, priv->phymode);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002993 if (!hw) {
2994 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2995 return;
2996 }
Zhu Yib481de92007-09-25 17:54:57 -07002997
2998 priv->active_rate = 0;
2999 priv->active_rate_basic = 0;
3000
3001 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
3002 hw->mode == MODE_IEEE80211A ?
3003 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
3004
3005 for (i = 0; i < hw->num_rates; i++) {
3006 rate = &(hw->rates[i]);
3007 if ((rate->val < IWL_RATE_COUNT) &&
3008 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
3009 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003010 rate->val, iwl3945_rates[rate->val].plcp,
Zhu Yib481de92007-09-25 17:54:57 -07003011 (rate->flags & IEEE80211_RATE_BASIC) ?
3012 "*" : "");
3013 priv->active_rate |= (1 << rate->val);
3014 if (rate->flags & IEEE80211_RATE_BASIC)
3015 priv->active_rate_basic |= (1 << rate->val);
3016 } else
3017 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003018 rate->val, iwl3945_rates[rate->val].plcp);
Zhu Yib481de92007-09-25 17:54:57 -07003019 }
3020
3021 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
3022 priv->active_rate, priv->active_rate_basic);
3023
3024 /*
3025 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
3026 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
3027 * OFDM
3028 */
3029 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
3030 priv->staging_rxon.cck_basic_rates =
3031 ((priv->active_rate_basic &
3032 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
3033 else
3034 priv->staging_rxon.cck_basic_rates =
3035 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
3036
3037 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
3038 priv->staging_rxon.ofdm_basic_rates =
3039 ((priv->active_rate_basic &
3040 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
3041 IWL_FIRST_OFDM_RATE) & 0xFF;
3042 else
3043 priv->staging_rxon.ofdm_basic_rates =
3044 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
3045}
3046
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003047static void iwl3945_radio_kill_sw(struct iwl3945_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07003048{
3049 unsigned long flags;
3050
3051 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
3052 return;
3053
3054 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
3055 disable_radio ? "OFF" : "ON");
3056
3057 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003058 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003059 /* FIXME: This is a workaround for AP */
3060 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3061 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003062 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003063 CSR_UCODE_SW_BIT_RFKILL);
3064 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003065 iwl3945_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003066 set_bit(STATUS_RF_KILL_SW, &priv->status);
3067 }
3068 return;
3069 }
3070
3071 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003072 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07003073
3074 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3075 spin_unlock_irqrestore(&priv->lock, flags);
3076
3077 /* wake up ucode */
3078 msleep(10);
3079
3080 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003081 iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
3082 if (!iwl3945_grab_nic_access(priv))
3083 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003084 spin_unlock_irqrestore(&priv->lock, flags);
3085
3086 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3087 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3088 "disabled by HW switch\n");
3089 return;
3090 }
3091
3092 queue_work(priv->workqueue, &priv->restart);
3093 return;
3094}
3095
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003096void iwl3945_set_decrypted_flag(struct iwl3945_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07003097 u32 decrypt_res, struct ieee80211_rx_status *stats)
3098{
3099 u16 fc =
3100 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3101
3102 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3103 return;
3104
3105 if (!(fc & IEEE80211_FCTL_PROTECTED))
3106 return;
3107
3108 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3109 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3110 case RX_RES_STATUS_SEC_TYPE_TKIP:
3111 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3112 RX_RES_STATUS_BAD_ICV_MIC)
3113 stats->flag |= RX_FLAG_MMIC_ERROR;
3114 case RX_RES_STATUS_SEC_TYPE_WEP:
3115 case RX_RES_STATUS_SEC_TYPE_CCMP:
3116 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3117 RX_RES_STATUS_DECRYPT_OK) {
3118 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3119 stats->flag |= RX_FLAG_DECRYPTED;
3120 }
3121 break;
3122
3123 default:
3124 break;
3125 }
3126}
3127
Zhu Yib481de92007-09-25 17:54:57 -07003128#define IWL_PACKET_RETRY_TIME HZ
3129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003130int iwl3945_is_duplicate_packet(struct iwl3945_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07003131{
3132 u16 sc = le16_to_cpu(header->seq_ctrl);
3133 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3134 u16 frag = sc & IEEE80211_SCTL_FRAG;
3135 u16 *last_seq, *last_frag;
3136 unsigned long *last_time;
3137
3138 switch (priv->iw_mode) {
3139 case IEEE80211_IF_TYPE_IBSS:{
3140 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003141 struct iwl3945_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003142 u8 *mac = header->addr2;
3143 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3144
3145 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003146 entry = list_entry(p, struct iwl3945_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07003147 if (!compare_ether_addr(entry->mac, mac))
3148 break;
3149 }
3150 if (p == &priv->ibss_mac_hash[index]) {
3151 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3152 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08003153 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07003154 return 0;
3155 }
3156 memcpy(entry->mac, mac, ETH_ALEN);
3157 entry->seq_num = seq;
3158 entry->frag_num = frag;
3159 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08003160 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07003161 return 0;
3162 }
3163 last_seq = &entry->seq_num;
3164 last_frag = &entry->frag_num;
3165 last_time = &entry->packet_time;
3166 break;
3167 }
3168 case IEEE80211_IF_TYPE_STA:
3169 last_seq = &priv->last_seq_num;
3170 last_frag = &priv->last_frag_num;
3171 last_time = &priv->last_packet_time;
3172 break;
3173 default:
3174 return 0;
3175 }
3176 if ((*last_seq == seq) &&
3177 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3178 if (*last_frag == frag)
3179 goto drop;
3180 if (*last_frag + 1 != frag)
3181 /* out-of-order fragment */
3182 goto drop;
3183 } else
3184 *last_seq = seq;
3185
3186 *last_frag = frag;
3187 *last_time = jiffies;
3188 return 0;
3189
3190 drop:
3191 return 1;
3192}
3193
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003194#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003195
3196#include "iwl-spectrum.h"
3197
3198#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3199#define BEACON_TIME_MASK_HIGH 0xFF000000
3200#define TIME_UNIT 1024
3201
3202/*
3203 * extended beacon time format
3204 * time in usec will be changed into a 32-bit value in 8:24 format
3205 * the high 1 byte is the beacon counts
3206 * the lower 3 bytes is the time in usec within one beacon interval
3207 */
3208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003209static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003210{
3211 u32 quot;
3212 u32 rem;
3213 u32 interval = beacon_interval * 1024;
3214
3215 if (!interval || !usec)
3216 return 0;
3217
3218 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3219 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3220
3221 return (quot << 24) + rem;
3222}
3223
3224/* base is usually what we get from ucode with each received frame,
3225 * the same as HW timer counter counting down
3226 */
3227
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003228static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003229{
3230 u32 base_low = base & BEACON_TIME_MASK_LOW;
3231 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3232 u32 interval = beacon_interval * TIME_UNIT;
3233 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3234 (addon & BEACON_TIME_MASK_HIGH);
3235
3236 if (base_low > addon_low)
3237 res += base_low - addon_low;
3238 else if (base_low < addon_low) {
3239 res += interval + base_low - addon_low;
3240 res += (1 << 24);
3241 } else
3242 res += (1 << 24);
3243
3244 return cpu_to_le32(res);
3245}
3246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003247static int iwl3945_get_measurement(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003248 struct ieee80211_measurement_params *params,
3249 u8 type)
3250{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003251 struct iwl3945_spectrum_cmd spectrum;
3252 struct iwl3945_rx_packet *res;
3253 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003254 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3255 .data = (void *)&spectrum,
3256 .meta.flags = CMD_WANT_SKB,
3257 };
3258 u32 add_time = le64_to_cpu(params->start_time);
3259 int rc;
3260 int spectrum_resp_status;
3261 int duration = le16_to_cpu(params->duration);
3262
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003263 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003264 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003265 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003266 le64_to_cpu(params->start_time) - priv->last_tsf,
3267 le16_to_cpu(priv->rxon_timing.beacon_interval));
3268
3269 memset(&spectrum, 0, sizeof(spectrum));
3270
3271 spectrum.channel_count = cpu_to_le16(1);
3272 spectrum.flags =
3273 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3274 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3275 cmd.len = sizeof(spectrum);
3276 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3277
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003278 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003279 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003280 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003281 add_time,
3282 le16_to_cpu(priv->rxon_timing.beacon_interval));
3283 else
3284 spectrum.start_time = 0;
3285
3286 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3287 spectrum.channels[0].channel = params->channel;
3288 spectrum.channels[0].type = type;
3289 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3290 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3291 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3292
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003293 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003294 if (rc)
3295 return rc;
3296
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003297 res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003298 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3299 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3300 rc = -EIO;
3301 }
3302
3303 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3304 switch (spectrum_resp_status) {
3305 case 0: /* Command will be handled */
3306 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08003307 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
3308 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07003309 priv->measurement_status &= ~MEASUREMENT_READY;
3310 }
3311 priv->measurement_status |= MEASUREMENT_ACTIVE;
3312 rc = 0;
3313 break;
3314
3315 case 1: /* Command will not be handled */
3316 rc = -EAGAIN;
3317 break;
3318 }
3319
3320 dev_kfree_skb_any(cmd.meta.u.skb);
3321
3322 return rc;
3323}
3324#endif
3325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003326static void iwl3945_txstatus_to_ieee(struct iwl3945_priv *priv,
3327 struct iwl3945_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003328{
3329
3330 tx_sta->status.ack_signal = 0;
3331 tx_sta->status.excessive_retries = 0;
3332 tx_sta->status.queue_length = 0;
3333 tx_sta->status.queue_number = 0;
3334
3335 if (in_interrupt())
3336 ieee80211_tx_status_irqsafe(priv->hw,
3337 tx_sta->skb[0], &(tx_sta->status));
3338 else
3339 ieee80211_tx_status(priv->hw,
3340 tx_sta->skb[0], &(tx_sta->status));
3341
3342 tx_sta->skb[0] = NULL;
3343}
3344
3345/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003346 * iwl3945_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07003347 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003348 * When FW advances 'R' index, all entries between old and new 'R' index
3349 * need to be reclaimed. As result, some free space forms. If there is
3350 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07003351 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003352static int iwl3945_tx_queue_reclaim(struct iwl3945_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003353{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003354 struct iwl3945_tx_queue *txq = &priv->txq[txq_id];
3355 struct iwl3945_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003356 int nfreed = 0;
3357
3358 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3359 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3360 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003361 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003362 return 0;
3363 }
3364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003365 for (index = iwl3945_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003366 q->read_ptr != index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003367 q->read_ptr = iwl3945_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003368 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003369 iwl3945_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003370 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003371 iwl3945_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003372 } else if (nfreed > 1) {
3373 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003374 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003375 queue_work(priv->workqueue, &priv->restart);
3376 }
3377 nfreed++;
3378 }
3379
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003380 if (iwl3945_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003381 (txq_id != IWL_CMD_QUEUE_NUM) &&
3382 priv->mac80211_registered)
3383 ieee80211_wake_queue(priv->hw, txq_id);
3384
3385
3386 return nfreed;
3387}
3388
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003389static int iwl3945_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003390{
3391 return (status & 0xFF) == 0x1;
3392}
3393
3394/******************************************************************************
3395 *
3396 * Generic RX handler implementations
3397 *
3398 ******************************************************************************/
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003399/**
3400 * iwl3945_rx_reply_tx - Handle Tx response
3401 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003402static void iwl3945_rx_reply_tx(struct iwl3945_priv *priv,
3403 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003404{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003405 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003406 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3407 int txq_id = SEQ_TO_QUEUE(sequence);
3408 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003409 struct iwl3945_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003410 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003411 struct iwl3945_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003412 u32 status = le32_to_cpu(tx_resp->status);
3413
3414 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3415 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3416 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003417 index, txq->q.n_bd, txq->q.write_ptr,
3418 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003419 return;
3420 }
3421
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003422 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003423
3424 tx_status->retry_count = tx_resp->failure_frame;
3425 tx_status->queue_number = status;
3426 tx_status->queue_length = tx_resp->bt_kill_count;
3427 tx_status->queue_length |= tx_resp->failure_rts;
3428
3429 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003430 iwl3945_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Zhu Yib481de92007-09-25 17:54:57 -07003431
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003432 tx_status->control.tx_rate = iwl3945_rate_index_from_plcp(tx_resp->rate);
Zhu Yib481de92007-09-25 17:54:57 -07003433
3434 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) plcp rate %d retries %d\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003435 txq_id, iwl3945_get_tx_fail_reason(status), status,
Zhu Yib481de92007-09-25 17:54:57 -07003436 tx_resp->rate, tx_resp->failure_frame);
3437
3438 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3439 if (index != -1)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003440 iwl3945_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003441
3442 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3443 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3444}
3445
3446
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003447static void iwl3945_rx_reply_alive(struct iwl3945_priv *priv,
3448 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003449{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003450 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3451 struct iwl3945_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003452 struct delayed_work *pwork;
3453
3454 palive = &pkt->u.alive_frame;
3455
3456 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3457 "0x%01X 0x%01X\n",
3458 palive->is_valid, palive->ver_type,
3459 palive->ver_subtype);
3460
3461 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3462 IWL_DEBUG_INFO("Initialization Alive received.\n");
3463 memcpy(&priv->card_alive_init,
3464 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003465 sizeof(struct iwl3945_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003466 pwork = &priv->init_alive_start;
3467 } else {
3468 IWL_DEBUG_INFO("Runtime Alive received.\n");
3469 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003470 sizeof(struct iwl3945_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003471 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003472 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003473 }
3474
3475 /* We delay the ALIVE response by 5ms to
3476 * give the HW RF Kill time to activate... */
3477 if (palive->is_valid == UCODE_VALID_OK)
3478 queue_delayed_work(priv->workqueue, pwork,
3479 msecs_to_jiffies(5));
3480 else
3481 IWL_WARNING("uCode did not respond OK.\n");
3482}
3483
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003484static void iwl3945_rx_reply_add_sta(struct iwl3945_priv *priv,
3485 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003486{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003487 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003488
3489 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3490 return;
3491}
3492
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003493static void iwl3945_rx_reply_error(struct iwl3945_priv *priv,
3494 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003495{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003496 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003497
3498 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3499 "seq 0x%04X ser 0x%08X\n",
3500 le32_to_cpu(pkt->u.err_resp.error_type),
3501 get_cmd_string(pkt->u.err_resp.cmd_id),
3502 pkt->u.err_resp.cmd_id,
3503 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3504 le32_to_cpu(pkt->u.err_resp.error_info));
3505}
3506
3507#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3508
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003509static void iwl3945_rx_csa(struct iwl3945_priv *priv, struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003510{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003511 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3512 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active_rxon;
3513 struct iwl3945_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003514 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3515 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3516 rxon->channel = csa->channel;
3517 priv->staging_rxon.channel = csa->channel;
3518}
3519
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003520static void iwl3945_rx_spectrum_measure_notif(struct iwl3945_priv *priv,
3521 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003522{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003523#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003524 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3525 struct iwl3945_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003526
3527 if (!report->state) {
3528 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3529 "Spectrum Measure Notification: Start\n");
3530 return;
3531 }
3532
3533 memcpy(&priv->measure_report, report, sizeof(*report));
3534 priv->measurement_status |= MEASUREMENT_READY;
3535#endif
3536}
3537
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003538static void iwl3945_rx_pm_sleep_notif(struct iwl3945_priv *priv,
3539 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003540{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003541#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003542 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3543 struct iwl3945_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003544 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3545 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3546#endif
3547}
3548
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003549static void iwl3945_rx_pm_debug_statistics_notif(struct iwl3945_priv *priv,
3550 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003551{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003552 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003553 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3554 "notification for %s:\n",
3555 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003556 iwl3945_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003557}
3558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003559static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003560{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003561 struct iwl3945_priv *priv =
3562 container_of(work, struct iwl3945_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003563 struct sk_buff *beacon;
3564
3565 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003566 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003567
3568 if (!beacon) {
3569 IWL_ERROR("update beacon failed\n");
3570 return;
3571 }
3572
3573 mutex_lock(&priv->mutex);
3574 /* new beacon skb is allocated every time; dispose previous.*/
3575 if (priv->ibss_beacon)
3576 dev_kfree_skb(priv->ibss_beacon);
3577
3578 priv->ibss_beacon = beacon;
3579 mutex_unlock(&priv->mutex);
3580
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003581 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003582}
3583
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003584static void iwl3945_rx_beacon_notif(struct iwl3945_priv *priv,
3585 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003586{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003587#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003588 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3589 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07003590 u8 rate = beacon->beacon_notify_hdr.rate;
3591
3592 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3593 "tsf %d %d rate %d\n",
3594 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3595 beacon->beacon_notify_hdr.failure_frame,
3596 le32_to_cpu(beacon->ibss_mgr_status),
3597 le32_to_cpu(beacon->high_tsf),
3598 le32_to_cpu(beacon->low_tsf), rate);
3599#endif
3600
3601 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3602 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3603 queue_work(priv->workqueue, &priv->beacon_update);
3604}
3605
3606/* Service response to REPLY_SCAN_CMD (0x80) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003607static void iwl3945_rx_reply_scan(struct iwl3945_priv *priv,
3608 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003609{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003610#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003611 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3612 struct iwl3945_scanreq_notification *notif =
3613 (struct iwl3945_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003614
3615 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3616#endif
3617}
3618
3619/* Service SCAN_START_NOTIFICATION (0x82) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003620static void iwl3945_rx_scan_start_notif(struct iwl3945_priv *priv,
3621 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003622{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003623 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3624 struct iwl3945_scanstart_notification *notif =
3625 (struct iwl3945_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003626 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3627 IWL_DEBUG_SCAN("Scan start: "
3628 "%d [802.11%s] "
3629 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3630 notif->channel,
3631 notif->band ? "bg" : "a",
3632 notif->tsf_high,
3633 notif->tsf_low, notif->status, notif->beacon_timer);
3634}
3635
3636/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003637static void iwl3945_rx_scan_results_notif(struct iwl3945_priv *priv,
3638 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003639{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003640 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3641 struct iwl3945_scanresults_notification *notif =
3642 (struct iwl3945_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003643
3644 IWL_DEBUG_SCAN("Scan ch.res: "
3645 "%d [802.11%s] "
3646 "(TSF: 0x%08X:%08X) - %d "
3647 "elapsed=%lu usec (%dms since last)\n",
3648 notif->channel,
3649 notif->band ? "bg" : "a",
3650 le32_to_cpu(notif->tsf_high),
3651 le32_to_cpu(notif->tsf_low),
3652 le32_to_cpu(notif->statistics[0]),
3653 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3654 jiffies_to_msecs(elapsed_jiffies
3655 (priv->last_scan_jiffies, jiffies)));
3656
3657 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003658 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003659}
3660
3661/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003662static void iwl3945_rx_scan_complete_notif(struct iwl3945_priv *priv,
3663 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003664{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003665 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3666 struct iwl3945_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003667
3668 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3669 scan_notif->scanned_channels,
3670 scan_notif->tsf_low,
3671 scan_notif->tsf_high, scan_notif->status);
3672
3673 /* The HW is no longer scanning */
3674 clear_bit(STATUS_SCAN_HW, &priv->status);
3675
3676 /* The scan completion notification came in, so kill that timer... */
3677 cancel_delayed_work(&priv->scan_check);
3678
3679 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3680 (priv->scan_bands == 2) ? "2.4" : "5.2",
3681 jiffies_to_msecs(elapsed_jiffies
3682 (priv->scan_pass_start, jiffies)));
3683
3684 /* Remove this scanned band from the list
3685 * of pending bands to scan */
3686 priv->scan_bands--;
3687
3688 /* If a request to abort was given, or the scan did not succeed
3689 * then we reset the scan state machine and terminate,
3690 * re-queuing another scan if one has been requested */
3691 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3692 IWL_DEBUG_INFO("Aborted scan completed.\n");
3693 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3694 } else {
3695 /* If there are more bands on this scan pass reschedule */
3696 if (priv->scan_bands > 0)
3697 goto reschedule;
3698 }
3699
3700 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003701 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003702 IWL_DEBUG_INFO("Setting scan to off\n");
3703
3704 clear_bit(STATUS_SCANNING, &priv->status);
3705
3706 IWL_DEBUG_INFO("Scan took %dms\n",
3707 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3708
3709 queue_work(priv->workqueue, &priv->scan_completed);
3710
3711 return;
3712
3713reschedule:
3714 priv->scan_pass_start = jiffies;
3715 queue_work(priv->workqueue, &priv->request_scan);
3716}
3717
3718/* Handle notification from uCode that card's power state is changing
3719 * due to software, hardware, or critical temperature RFKILL */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003720static void iwl3945_rx_card_state_notif(struct iwl3945_priv *priv,
3721 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003722{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003723 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003724 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3725 unsigned long status = priv->status;
3726
3727 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3728 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3729 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003731 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003732 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3733
3734 if (flags & HW_CARD_DISABLED)
3735 set_bit(STATUS_RF_KILL_HW, &priv->status);
3736 else
3737 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3738
3739
3740 if (flags & SW_CARD_DISABLED)
3741 set_bit(STATUS_RF_KILL_SW, &priv->status);
3742 else
3743 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3744
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003745 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003746
3747 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3748 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3749 (test_bit(STATUS_RF_KILL_SW, &status) !=
3750 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3751 queue_work(priv->workqueue, &priv->rf_kill);
3752 else
3753 wake_up_interruptible(&priv->wait_command_queue);
3754}
3755
3756/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003757 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003758 *
3759 * Setup the RX handlers for each of the reply types sent from the uCode
3760 * to the host.
3761 *
3762 * This function chains into the hardware specific files for them to setup
3763 * any hardware specific handlers as well.
3764 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003765static void iwl3945_setup_rx_handlers(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003766{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003767 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3768 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3769 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3770 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003771 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003772 iwl3945_rx_spectrum_measure_notif;
3773 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003774 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003775 iwl3945_rx_pm_debug_statistics_notif;
3776 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003777
Ben Cahill9fbab512007-11-29 11:09:47 +08003778 /*
3779 * The same handler is used for both the REPLY to a discrete
3780 * statistics request from the host as well as for the periodic
3781 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003782 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003783 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3784 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003785
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003786 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3787 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003788 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003789 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003790 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003791 iwl3945_rx_scan_complete_notif;
3792 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
3793 priv->rx_handlers[REPLY_TX] = iwl3945_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003794
Ben Cahill9fbab512007-11-29 11:09:47 +08003795 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003796 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003797}
3798
3799/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003800 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003801 * @rxb: Rx buffer to reclaim
3802 *
3803 * If an Rx buffer has an async callback associated with it the callback
3804 * will be executed. The attached skb (if present) will only be freed
3805 * if the callback returns 1
3806 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003807static void iwl3945_tx_cmd_complete(struct iwl3945_priv *priv,
3808 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003809{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003810 struct iwl3945_rx_packet *pkt = (struct iwl3945_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003811 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3812 int txq_id = SEQ_TO_QUEUE(sequence);
3813 int index = SEQ_TO_INDEX(sequence);
3814 int huge = sequence & SEQ_HUGE_FRAME;
3815 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003816 struct iwl3945_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003817
3818 /* If a Tx command is being handled and it isn't in the actual
3819 * command queue then there a command routing bug has been introduced
3820 * in the queue management code. */
3821 if (txq_id != IWL_CMD_QUEUE_NUM)
3822 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3823 txq_id, pkt->hdr.cmd);
3824 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3825
3826 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3827 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3828
3829 /* Input error checking is done when commands are added to queue. */
3830 if (cmd->meta.flags & CMD_WANT_SKB) {
3831 cmd->meta.source->u.skb = rxb->skb;
3832 rxb->skb = NULL;
3833 } else if (cmd->meta.u.callback &&
3834 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3835 rxb->skb = NULL;
3836
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003837 iwl3945_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003838
3839 if (!(cmd->meta.flags & CMD_ASYNC)) {
3840 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3841 wake_up_interruptible(&priv->wait_command_queue);
3842 }
3843}
3844
3845/************************** RX-FUNCTIONS ****************************/
3846/*
3847 * Rx theory of operation
3848 *
3849 * The host allocates 32 DMA target addresses and passes the host address
3850 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3851 * 0 to 31
3852 *
3853 * Rx Queue Indexes
3854 * The host/firmware share two index registers for managing the Rx buffers.
3855 *
3856 * The READ index maps to the first position that the firmware may be writing
3857 * to -- the driver can read up to (but not including) this position and get
3858 * good data.
3859 * The READ index is managed by the firmware once the card is enabled.
3860 *
3861 * The WRITE index maps to the last position the driver has read from -- the
3862 * position preceding WRITE is the last slot the firmware can place a packet.
3863 *
3864 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3865 * WRITE = READ.
3866 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003867 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003868 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3869 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003870 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003871 * and fire the RX interrupt. The driver can then query the READ index and
3872 * process as many packets as possible, moving the WRITE index forward as it
3873 * resets the Rx queue buffers with new memory.
3874 *
3875 * The management in the driver is as follows:
3876 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3877 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003878 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003879 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003880 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3881 * 'processed' and 'read' driver indexes as well)
3882 * + A received packet is processed and handed to the kernel network stack,
3883 * detached from the iwl->rxq. The driver 'processed' index is updated.
3884 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3885 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3886 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3887 * were enough free buffers and RX_STALLED is set it is cleared.
3888 *
3889 *
3890 * Driver sequence:
3891 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003892 * iwl3945_rx_queue_alloc() Allocates rx_free
3893 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003894 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003895 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003896 * queue, updates firmware pointers, and updates
3897 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003898 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003899 *
3900 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003901 * ISR - iwl3945_rx() Detach iwl3945_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003902 * READ INDEX, detaching the SKB from the pool.
3903 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003904 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003905 * slots.
3906 * ...
3907 *
3908 */
3909
3910/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003911 * iwl3945_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003912 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003913static int iwl3945_rx_queue_space(const struct iwl3945_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003914{
3915 int s = q->read - q->write;
3916 if (s <= 0)
3917 s += RX_QUEUE_SIZE;
3918 /* keep some buffer to not confuse full and empty queue */
3919 s -= 2;
3920 if (s < 0)
3921 s = 0;
3922 return s;
3923}
3924
3925/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003926 * iwl3945_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003927 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003928int iwl3945_rx_queue_update_write_ptr(struct iwl3945_priv *priv, struct iwl3945_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003929{
3930 u32 reg = 0;
3931 int rc = 0;
3932 unsigned long flags;
3933
3934 spin_lock_irqsave(&q->lock, flags);
3935
3936 if (q->need_update == 0)
3937 goto exit_unlock;
3938
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003939 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003940 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003941 reg = iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003942
3943 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003944 iwl3945_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003945 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3946 goto exit_unlock;
3947 }
3948
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003949 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003950 if (rc)
3951 goto exit_unlock;
3952
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003953 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003954 iwl3945_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003955 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003956 iwl3945_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003957
3958 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003959 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003960 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003961 iwl3945_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003962
3963
3964 q->need_update = 0;
3965
3966 exit_unlock:
3967 spin_unlock_irqrestore(&q->lock, flags);
3968 return rc;
3969}
3970
3971/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003972 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003973 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003974static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003975 dma_addr_t dma_addr)
3976{
3977 return cpu_to_le32((u32)dma_addr);
3978}
3979
3980/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003981 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003982 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003983 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003984 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003985 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003986 *
3987 * This moves the 'write' index forward to catch up with 'processed', and
3988 * also updates the memory address in the firmware to reference the new
3989 * target buffer.
3990 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003991static int iwl3945_rx_queue_restock(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003992{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003993 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003994 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003995 struct iwl3945_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003996 unsigned long flags;
3997 int write, rc;
3998
3999 spin_lock_irqsave(&rxq->lock, flags);
4000 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004001 while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004002 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07004003 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004004 rxb = list_entry(element, struct iwl3945_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004005 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004006
4007 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004008 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004009 rxq->queue[rxq->write] = rxb;
4010 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4011 rxq->free_count--;
4012 }
4013 spin_unlock_irqrestore(&rxq->lock, flags);
4014 /* If the pre-allocated buffer pool is dropping low, schedule to
4015 * refill it */
4016 if (rxq->free_count <= RX_LOW_WATERMARK)
4017 queue_work(priv->workqueue, &priv->rx_replenish);
4018
4019
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004020 /* If we've added more space for the firmware to place data, tell it.
4021 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07004022 if ((write != (rxq->write & ~0x7))
4023 || (abs(rxq->write - rxq->read) > 7)) {
4024 spin_lock_irqsave(&rxq->lock, flags);
4025 rxq->need_update = 1;
4026 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004027 rc = iwl3945_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004028 if (rc)
4029 return rc;
4030 }
4031
4032 return 0;
4033}
4034
4035/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004036 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004037 *
4038 * When moving to rx_free an SKB is allocated for the slot.
4039 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004040 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004041 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004042 */
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004043static void iwl3945_rx_allocate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004044{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004045 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004046 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004047 struct iwl3945_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004048 unsigned long flags;
4049 spin_lock_irqsave(&rxq->lock, flags);
4050 while (!list_empty(&rxq->rx_used)) {
4051 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004052 rxb = list_entry(element, struct iwl3945_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004053
4054 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07004055 rxb->skb =
4056 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
4057 if (!rxb->skb) {
4058 if (net_ratelimit())
4059 printk(KERN_CRIT DRV_NAME
4060 ": Can not allocate SKB buffers\n");
4061 /* We don't reschedule replenish work here -- we will
4062 * call the restock method and if it still needs
4063 * more buffers it will schedule replenish */
4064 break;
4065 }
Zhu Yi12342c42007-12-20 11:27:32 +08004066
4067 /* If radiotap head is required, reserve some headroom here.
4068 * The physical head count is a variable rx_stats->phy_count.
4069 * We reserve 4 bytes here. Plus these extra bytes, the
4070 * headroom of the physical head should be enough for the
4071 * radiotap head that iwl3945 supported. See iwl3945_rt.
4072 */
4073 skb_reserve(rxb->skb, 4);
4074
Zhu Yib481de92007-09-25 17:54:57 -07004075 priv->alloc_rxb_skb++;
4076 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004077
4078 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07004079 rxb->dma_addr =
4080 pci_map_single(priv->pci_dev, rxb->skb->data,
4081 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4082 list_add_tail(&rxb->list, &rxq->rx_free);
4083 rxq->free_count++;
4084 }
4085 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004086}
4087
4088/*
4089 * this should be called while priv->lock is locked
4090 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02004091static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004092{
4093 struct iwl3945_priv *priv = data;
4094
4095 iwl3945_rx_allocate(priv);
4096 iwl3945_rx_queue_restock(priv);
4097}
4098
4099
4100void iwl3945_rx_replenish(void *data)
4101{
4102 struct iwl3945_priv *priv = data;
4103 unsigned long flags;
4104
4105 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004106
4107 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004108 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004109 spin_unlock_irqrestore(&priv->lock, flags);
4110}
4111
4112/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08004113 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07004114 * This free routine walks the list of POOL entries and if SKB is set to
4115 * non NULL it is unmapped and freed
4116 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004117static void iwl3945_rx_queue_free(struct iwl3945_priv *priv, struct iwl3945_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004118{
4119 int i;
4120 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4121 if (rxq->pool[i].skb != NULL) {
4122 pci_unmap_single(priv->pci_dev,
4123 rxq->pool[i].dma_addr,
4124 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4125 dev_kfree_skb(rxq->pool[i].skb);
4126 }
4127 }
4128
4129 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4130 rxq->dma_addr);
4131 rxq->bd = NULL;
4132}
4133
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004134int iwl3945_rx_queue_alloc(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004135{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004136 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004137 struct pci_dev *dev = priv->pci_dev;
4138 int i;
4139
4140 spin_lock_init(&rxq->lock);
4141 INIT_LIST_HEAD(&rxq->rx_free);
4142 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004143
4144 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07004145 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4146 if (!rxq->bd)
4147 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004148
Zhu Yib481de92007-09-25 17:54:57 -07004149 /* Fill the rx_used queue with _all_ of the Rx buffers */
4150 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4151 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004152
Zhu Yib481de92007-09-25 17:54:57 -07004153 /* Set us so that we have processed and used all buffers, but have
4154 * not restocked the Rx queue with fresh buffers */
4155 rxq->read = rxq->write = 0;
4156 rxq->free_count = 0;
4157 rxq->need_update = 0;
4158 return 0;
4159}
4160
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004161void iwl3945_rx_queue_reset(struct iwl3945_priv *priv, struct iwl3945_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004162{
4163 unsigned long flags;
4164 int i;
4165 spin_lock_irqsave(&rxq->lock, flags);
4166 INIT_LIST_HEAD(&rxq->rx_free);
4167 INIT_LIST_HEAD(&rxq->rx_used);
4168 /* Fill the rx_used queue with _all_ of the Rx buffers */
4169 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4170 /* In the reset function, these buffers may have been allocated
4171 * to an SKB, so we need to unmap and free potential storage */
4172 if (rxq->pool[i].skb != NULL) {
4173 pci_unmap_single(priv->pci_dev,
4174 rxq->pool[i].dma_addr,
4175 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4176 priv->alloc_rxb_skb--;
4177 dev_kfree_skb(rxq->pool[i].skb);
4178 rxq->pool[i].skb = NULL;
4179 }
4180 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4181 }
4182
4183 /* Set us so that we have processed and used all buffers, but have
4184 * not restocked the Rx queue with fresh buffers */
4185 rxq->read = rxq->write = 0;
4186 rxq->free_count = 0;
4187 spin_unlock_irqrestore(&rxq->lock, flags);
4188}
4189
4190/* Convert linear signal-to-noise ratio into dB */
4191static u8 ratio2dB[100] = {
4192/* 0 1 2 3 4 5 6 7 8 9 */
4193 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4194 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4195 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4196 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4197 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4198 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4199 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4200 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4201 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4202 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4203};
4204
4205/* Calculates a relative dB value from a ratio of linear
4206 * (i.e. not dB) signal levels.
4207 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004208int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004209{
4210 /* Anything above 1000:1 just report as 60 dB */
4211 if (sig_ratio > 1000)
4212 return 60;
4213
4214 /* Above 100:1, divide by 10 and use table,
4215 * add 20 dB to make up for divide by 10 */
4216 if (sig_ratio > 100)
4217 return (20 + (int)ratio2dB[sig_ratio/10]);
4218
4219 /* We shouldn't see this */
4220 if (sig_ratio < 1)
4221 return 0;
4222
4223 /* Use table for ratios 1:1 - 99:1 */
4224 return (int)ratio2dB[sig_ratio];
4225}
4226
4227#define PERFECT_RSSI (-20) /* dBm */
4228#define WORST_RSSI (-95) /* dBm */
4229#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4230
4231/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4232 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4233 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004234int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004235{
4236 int sig_qual;
4237 int degradation = PERFECT_RSSI - rssi_dbm;
4238
4239 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4240 * as indicator; formula is (signal dbm - noise dbm).
4241 * SNR at or above 40 is a great signal (100%).
4242 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4243 * Weakest usable signal is usually 10 - 15 dB SNR. */
4244 if (noise_dbm) {
4245 if (rssi_dbm - noise_dbm >= 40)
4246 return 100;
4247 else if (rssi_dbm < noise_dbm)
4248 return 0;
4249 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4250
4251 /* Else use just the signal level.
4252 * This formula is a least squares fit of data points collected and
4253 * compared with a reference system that had a percentage (%) display
4254 * for signal quality. */
4255 } else
4256 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4257 (15 * RSSI_RANGE + 62 * degradation)) /
4258 (RSSI_RANGE * RSSI_RANGE);
4259
4260 if (sig_qual > 100)
4261 sig_qual = 100;
4262 else if (sig_qual < 1)
4263 sig_qual = 0;
4264
4265 return sig_qual;
4266}
4267
4268/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004269 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004270 *
4271 * Uses the priv->rx_handlers callback function array to invoke
4272 * the appropriate handlers, including command responses,
4273 * frame-received notifications, and other notifications.
4274 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004275static void iwl3945_rx_handle(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004276{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004277 struct iwl3945_rx_mem_buffer *rxb;
4278 struct iwl3945_rx_packet *pkt;
4279 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004280 u32 r, i;
4281 int reclaim;
4282 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004283 u8 fill_rx = 0;
4284 u32 count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004285
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004286 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4287 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004288 r = iwl3945_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004289 i = rxq->read;
4290
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004291 if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4292 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07004293 /* Rx interrupt, but nothing sent from uCode */
4294 if (i == r)
4295 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4296
4297 while (i != r) {
4298 rxb = rxq->queue[i];
4299
Ben Cahill9fbab512007-11-29 11:09:47 +08004300 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004301 * then a bug has been introduced in the queue refilling
4302 * routines -- catch it here */
4303 BUG_ON(rxb == NULL);
4304
4305 rxq->queue[i] = NULL;
4306
4307 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4308 IWL_RX_BUF_SIZE,
4309 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004310 pkt = (struct iwl3945_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004311
4312 /* Reclaim a command buffer only if this packet is a response
4313 * to a (driver-originated) command.
4314 * If the packet (e.g. Rx frame) originated from uCode,
4315 * there is no command buffer to reclaim.
4316 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4317 * but apparently a few don't get set; catch them here. */
4318 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4319 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4320 (pkt->hdr.cmd != REPLY_TX);
4321
4322 /* Based on type of command response or notification,
4323 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004324 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004325 if (priv->rx_handlers[pkt->hdr.cmd]) {
4326 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4327 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4328 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4329 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4330 } else {
4331 /* No handling needed */
4332 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4333 "r %d i %d No handler needed for %s, 0x%02x\n",
4334 r, i, get_cmd_string(pkt->hdr.cmd),
4335 pkt->hdr.cmd);
4336 }
4337
4338 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004339 /* Invoke any callbacks, transfer the skb to caller, and
4340 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004341 * as we reclaim the driver command queue */
4342 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004343 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004344 else
4345 IWL_WARNING("Claim null rxb?\n");
4346 }
4347
4348 /* For now we just don't re-use anything. We can tweak this
4349 * later to try and re-use notification packets and SKBs that
4350 * fail to Rx correctly */
4351 if (rxb->skb != NULL) {
4352 priv->alloc_rxb_skb--;
4353 dev_kfree_skb_any(rxb->skb);
4354 rxb->skb = NULL;
4355 }
4356
4357 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4358 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4359 spin_lock_irqsave(&rxq->lock, flags);
4360 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4361 spin_unlock_irqrestore(&rxq->lock, flags);
4362 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004363 /* If there are a lot of unused frames,
4364 * restock the Rx queue so ucode won't assert. */
4365 if (fill_rx) {
4366 count++;
4367 if (count >= 8) {
4368 priv->rxq.read = i;
4369 __iwl3945_rx_replenish(priv);
4370 count = 0;
4371 }
4372 }
Zhu Yib481de92007-09-25 17:54:57 -07004373 }
4374
4375 /* Backtrack one entry */
4376 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004377 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004378}
4379
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004380/**
4381 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
4382 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004383static int iwl3945_tx_queue_update_write_ptr(struct iwl3945_priv *priv,
4384 struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004385{
4386 u32 reg = 0;
4387 int rc = 0;
4388 int txq_id = txq->q.id;
4389
4390 if (txq->need_update == 0)
4391 return rc;
4392
4393 /* if we're trying to save power */
4394 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4395 /* wake up nic if it's powered down ...
4396 * uCode will wake up, and interrupt us again, so next
4397 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004398 reg = iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004399
4400 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4401 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004402 iwl3945_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004403 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4404 return rc;
4405 }
4406
4407 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004408 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004409 if (rc)
4410 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004411 iwl3945_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004412 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004413 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004414
4415 /* else not in power-save mode, uCode will never sleep when we're
4416 * trying to tx (during RFKILL, we're not trying to tx). */
4417 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004418 iwl3945_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004419 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004420
4421 txq->need_update = 0;
4422
4423 return rc;
4424}
4425
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004426#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004427static void iwl3945_print_rx_config_cmd(struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004428{
Joe Perches0795af52007-10-03 17:59:30 -07004429 DECLARE_MAC_BUF(mac);
4430
Zhu Yib481de92007-09-25 17:54:57 -07004431 IWL_DEBUG_RADIO("RX CONFIG:\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004432 iwl3945_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004433 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4434 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4435 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4436 le32_to_cpu(rxon->filter_flags));
4437 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4438 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4439 rxon->ofdm_basic_rates);
4440 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004441 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4442 print_mac(mac, rxon->node_addr));
4443 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4444 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004445 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4446}
4447#endif
4448
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004449static void iwl3945_enable_interrupts(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004450{
4451 IWL_DEBUG_ISR("Enabling interrupts\n");
4452 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004453 iwl3945_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004454}
4455
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004456static inline void iwl3945_disable_interrupts(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004457{
4458 clear_bit(STATUS_INT_ENABLED, &priv->status);
4459
4460 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004461 iwl3945_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004462
4463 /* acknowledge/clear/reset any interrupts still pending
4464 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004465 iwl3945_write32(priv, CSR_INT, 0xffffffff);
4466 iwl3945_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004467 IWL_DEBUG_ISR("Disabled interrupts\n");
4468}
4469
4470static const char *desc_lookup(int i)
4471{
4472 switch (i) {
4473 case 1:
4474 return "FAIL";
4475 case 2:
4476 return "BAD_PARAM";
4477 case 3:
4478 return "BAD_CHECKSUM";
4479 case 4:
4480 return "NMI_INTERRUPT";
4481 case 5:
4482 return "SYSASSERT";
4483 case 6:
4484 return "FATAL_ERROR";
4485 }
4486
4487 return "UNKNOWN";
4488}
4489
4490#define ERROR_START_OFFSET (1 * sizeof(u32))
4491#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4492
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004493static void iwl3945_dump_nic_error_log(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004494{
4495 u32 i;
4496 u32 desc, time, count, base, data1;
4497 u32 blink1, blink2, ilink1, ilink2;
4498 int rc;
4499
4500 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004502 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004503 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4504 return;
4505 }
4506
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004507 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004508 if (rc) {
4509 IWL_WARNING("Can not read from adapter at this time.\n");
4510 return;
4511 }
4512
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004513 count = iwl3945_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004514
4515 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4516 IWL_ERROR("Start IWL Error Log Dump:\n");
4517 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4518 priv->status, priv->config, count);
4519 }
4520
4521 IWL_ERROR("Desc Time asrtPC blink2 "
4522 "ilink1 nmiPC Line\n");
4523 for (i = ERROR_START_OFFSET;
4524 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
4525 i += ERROR_ELEM_SIZE) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004526 desc = iwl3945_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07004527 time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004528 iwl3945_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004529 blink1 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004530 iwl3945_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004531 blink2 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004532 iwl3945_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004533 ilink1 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004534 iwl3945_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004535 ilink2 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004536 iwl3945_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004537 data1 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004538 iwl3945_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004539
4540 IWL_ERROR
4541 ("%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
4542 desc_lookup(desc), desc, time, blink1, blink2,
4543 ilink1, ilink2, data1);
4544 }
4545
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004546 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004547
4548}
4549
Ben Cahillf58177b2007-11-29 11:09:43 +08004550#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07004551
4552/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004553 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004554 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004555 * NOTE: Must be called with iwl3945_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004556 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004557static void iwl3945_print_event_log(struct iwl3945_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004558 u32 num_events, u32 mode)
4559{
4560 u32 i;
4561 u32 base; /* SRAM byte address of event log header */
4562 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4563 u32 ptr; /* SRAM byte address of log data */
4564 u32 ev, time, data; /* event log data */
4565
4566 if (num_events == 0)
4567 return;
4568
4569 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4570
4571 if (mode == 0)
4572 event_size = 2 * sizeof(u32);
4573 else
4574 event_size = 3 * sizeof(u32);
4575
4576 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4577
4578 /* "time" is actually "data" for mode 0 (no timestamp).
4579 * place event id # at far right for easier visual parsing. */
4580 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004581 ev = iwl3945_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004582 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004583 time = iwl3945_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004584 ptr += sizeof(u32);
4585 if (mode == 0)
4586 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4587 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004588 data = iwl3945_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004589 ptr += sizeof(u32);
4590 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4591 }
4592 }
4593}
4594
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004595static void iwl3945_dump_nic_event_log(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004596{
4597 int rc;
4598 u32 base; /* SRAM byte address of event log header */
4599 u32 capacity; /* event log capacity in # entries */
4600 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4601 u32 num_wraps; /* # times uCode wrapped to top of log */
4602 u32 next_entry; /* index of next entry to be written by uCode */
4603 u32 size; /* # entries that we'll print */
4604
4605 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004606 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004607 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4608 return;
4609 }
4610
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004611 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004612 if (rc) {
4613 IWL_WARNING("Can not read from adapter at this time.\n");
4614 return;
4615 }
4616
4617 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004618 capacity = iwl3945_read_targ_mem(priv, base);
4619 mode = iwl3945_read_targ_mem(priv, base + (1 * sizeof(u32)));
4620 num_wraps = iwl3945_read_targ_mem(priv, base + (2 * sizeof(u32)));
4621 next_entry = iwl3945_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004622
4623 size = num_wraps ? capacity : next_entry;
4624
4625 /* bail out if nothing in log */
4626 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004627 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004628 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004629 return;
4630 }
4631
Zhu Yi583fab32007-09-27 11:27:30 +08004632 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004633 size, num_wraps);
4634
4635 /* if uCode has wrapped back to top of log, start at the oldest entry,
4636 * i.e the next one that uCode would fill. */
4637 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004638 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004639 capacity - next_entry, mode);
4640
4641 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004642 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004644 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004645}
4646
4647/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004648 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004649 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004650static void iwl3945_irq_handle_error(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004651{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004652 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07004653 set_bit(STATUS_FW_ERROR, &priv->status);
4654
4655 /* Cancel currently queued command. */
4656 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4657
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004658#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004659 if (iwl3945_debug_level & IWL_DL_FW_ERRORS) {
4660 iwl3945_dump_nic_error_log(priv);
4661 iwl3945_dump_nic_event_log(priv);
4662 iwl3945_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004663 }
4664#endif
4665
4666 wake_up_interruptible(&priv->wait_command_queue);
4667
4668 /* Keep the restart process from trying to send host
4669 * commands by clearing the INIT status bit */
4670 clear_bit(STATUS_READY, &priv->status);
4671
4672 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4673 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4674 "Restarting adapter due to uCode error.\n");
4675
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004676 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004677 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4678 sizeof(priv->recovery_rxon));
4679 priv->error_recovering = 1;
4680 }
4681 queue_work(priv->workqueue, &priv->restart);
4682 }
4683}
4684
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004685static void iwl3945_error_recovery(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004686{
4687 unsigned long flags;
4688
4689 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4690 sizeof(priv->staging_rxon));
4691 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004692 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004693
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004694 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004695
4696 spin_lock_irqsave(&priv->lock, flags);
4697 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4698 priv->error_recovering = 0;
4699 spin_unlock_irqrestore(&priv->lock, flags);
4700}
4701
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004702static void iwl3945_irq_tasklet(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004703{
4704 u32 inta, handled = 0;
4705 u32 inta_fh;
4706 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004707#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004708 u32 inta_mask;
4709#endif
4710
4711 spin_lock_irqsave(&priv->lock, flags);
4712
4713 /* Ack/clear/reset pending uCode interrupts.
4714 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4715 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004716 inta = iwl3945_read32(priv, CSR_INT);
4717 iwl3945_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004718
4719 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4720 * Any new interrupts that happen after this, either while we're
4721 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004722 inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
4723 iwl3945_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004724
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004725#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004726 if (iwl3945_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004727 /* just for debug */
4728 inta_mask = iwl3945_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004729 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4730 inta, inta_mask, inta_fh);
4731 }
4732#endif
4733
4734 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4735 * atomic, make sure that inta covers all the interrupts that
4736 * we've discovered, even if FH interrupt came in just after
4737 * reading CSR_INT. */
4738 if (inta_fh & CSR_FH_INT_RX_MASK)
4739 inta |= CSR_INT_BIT_FH_RX;
4740 if (inta_fh & CSR_FH_INT_TX_MASK)
4741 inta |= CSR_INT_BIT_FH_TX;
4742
4743 /* Now service all interrupt bits discovered above. */
4744 if (inta & CSR_INT_BIT_HW_ERR) {
4745 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4746
4747 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004748 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004750 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004751
4752 handled |= CSR_INT_BIT_HW_ERR;
4753
4754 spin_unlock_irqrestore(&priv->lock, flags);
4755
4756 return;
4757 }
4758
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004759#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004760 if (iwl3945_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004761 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004762 if (inta & CSR_INT_BIT_SCD)
4763 IWL_DEBUG_ISR("Scheduler finished to transmit "
4764 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004765
4766 /* Alive notification via Rx interrupt will do the real work */
4767 if (inta & CSR_INT_BIT_ALIVE)
4768 IWL_DEBUG_ISR("Alive interrupt\n");
4769 }
4770#endif
4771 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004772 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004773
4774 /* HW RF KILL switch toggled (4965 only) */
4775 if (inta & CSR_INT_BIT_RF_KILL) {
4776 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004777 if (!(iwl3945_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004778 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4779 hw_rf_kill = 1;
4780
4781 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4782 "RF_KILL bit toggled to %s.\n",
4783 hw_rf_kill ? "disable radio":"enable radio");
4784
4785 /* Queue restart only if RF_KILL switch was set to "kill"
4786 * when we loaded driver, and is now set to "enable".
4787 * After we're Alive, RF_KILL gets handled by
4788 * iwl_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004789 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4790 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004791 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004792 }
Zhu Yib481de92007-09-25 17:54:57 -07004793
4794 handled |= CSR_INT_BIT_RF_KILL;
4795 }
4796
4797 /* Chip got too hot and stopped itself (4965 only) */
4798 if (inta & CSR_INT_BIT_CT_KILL) {
4799 IWL_ERROR("Microcode CT kill error detected.\n");
4800 handled |= CSR_INT_BIT_CT_KILL;
4801 }
4802
4803 /* Error detected by uCode */
4804 if (inta & CSR_INT_BIT_SW_ERR) {
4805 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4806 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004807 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004808 handled |= CSR_INT_BIT_SW_ERR;
4809 }
4810
4811 /* uCode wakes up after power-down sleep */
4812 if (inta & CSR_INT_BIT_WAKEUP) {
4813 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004814 iwl3945_rx_queue_update_write_ptr(priv, &priv->rxq);
4815 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4816 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4817 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4818 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4819 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4820 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004821
4822 handled |= CSR_INT_BIT_WAKEUP;
4823 }
4824
4825 /* All uCode command responses, including Tx command responses,
4826 * Rx "responses" (frame-received notification), and other
4827 * notifications from uCode come through here*/
4828 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004829 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004830 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4831 }
4832
4833 if (inta & CSR_INT_BIT_FH_TX) {
4834 IWL_DEBUG_ISR("Tx interrupt\n");
4835
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004836 iwl3945_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
4837 if (!iwl3945_grab_nic_access(priv)) {
4838 iwl3945_write_direct32(priv,
Zhu Yib481de92007-09-25 17:54:57 -07004839 FH_TCSR_CREDIT
4840 (ALM_FH_SRVC_CHNL), 0x0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004841 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004842 }
4843 handled |= CSR_INT_BIT_FH_TX;
4844 }
4845
4846 if (inta & ~handled)
4847 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4848
4849 if (inta & ~CSR_INI_SET_MASK) {
4850 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4851 inta & ~CSR_INI_SET_MASK);
4852 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4853 }
4854
4855 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004856 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004857
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004858#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004859 if (iwl3945_debug_level & (IWL_DL_ISR)) {
4860 inta = iwl3945_read32(priv, CSR_INT);
4861 inta_mask = iwl3945_read32(priv, CSR_INT_MASK);
4862 inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004863 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4864 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4865 }
4866#endif
4867 spin_unlock_irqrestore(&priv->lock, flags);
4868}
4869
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004870static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004871{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004872 struct iwl3945_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004873 u32 inta, inta_mask;
4874 u32 inta_fh;
4875 if (!priv)
4876 return IRQ_NONE;
4877
4878 spin_lock(&priv->lock);
4879
4880 /* Disable (but don't clear!) interrupts here to avoid
4881 * back-to-back ISRs and sporadic interrupts from our NIC.
4882 * If we have something to service, the tasklet will re-enable ints.
4883 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004884 inta_mask = iwl3945_read32(priv, CSR_INT_MASK); /* just for debug */
4885 iwl3945_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004886
4887 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004888 inta = iwl3945_read32(priv, CSR_INT);
4889 inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004890
4891 /* Ignore interrupt if there's nothing in NIC to service.
4892 * This may be due to IRQ shared with another device,
4893 * or due to sporadic interrupts thrown from our NIC. */
4894 if (!inta && !inta_fh) {
4895 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4896 goto none;
4897 }
4898
4899 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4900 /* Hardware disappeared */
4901 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004902 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004903 }
4904
4905 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4906 inta, inta_mask, inta_fh);
4907
Joonwoo Park25c03d82008-01-23 10:15:20 -08004908 inta &= ~CSR_INT_BIT_SCD;
4909
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004910 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004911 if (likely(inta || inta_fh))
4912 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004913unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004914 spin_unlock(&priv->lock);
4915
4916 return IRQ_HANDLED;
4917
4918 none:
4919 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004920 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004921 spin_unlock(&priv->lock);
4922 return IRQ_NONE;
4923}
4924
4925/************************** EEPROM BANDS ****************************
4926 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004927 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004928 * EEPROM contents to the specific channel number supported for each
4929 * band.
4930 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004931 * For example, iwl3945_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004932 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4933 * The specific geography and calibration information for that channel
4934 * is contained in the eeprom map itself.
4935 *
4936 * During init, we copy the eeprom information and channel map
4937 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4938 *
4939 * channel_map_24/52 provides the index in the channel_info array for a
4940 * given channel. We have to have two separate maps as there is channel
4941 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4942 * band_2
4943 *
4944 * A value of 0xff stored in the channel_map indicates that the channel
4945 * is not supported by the hardware at all.
4946 *
4947 * A value of 0xfe in the channel_map indicates that the channel is not
4948 * valid for Tx with the current hardware. This means that
4949 * while the system can tune and receive on a given channel, it may not
4950 * be able to associate or transmit any frames on that
4951 * channel. There is no corresponding channel information for that
4952 * entry.
4953 *
4954 *********************************************************************/
4955
4956/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004957static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004958 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4959};
4960
4961/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004962static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004963 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4964};
4965
Ben Cahill9fbab512007-11-29 11:09:47 +08004966static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004967 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4968};
4969
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004970static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004971 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4972};
4973
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004974static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004975 145, 149, 153, 157, 161, 165
4976};
4977
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004978static void iwl3945_init_band_reference(const struct iwl3945_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004979 int *eeprom_ch_count,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004980 const struct iwl3945_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004981 **eeprom_ch_info,
4982 const u8 **eeprom_ch_index)
4983{
4984 switch (band) {
4985 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004986 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07004987 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004988 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004989 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004990 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004991 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07004992 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004993 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004994 break;
4995 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004996 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07004997 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004998 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004999 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005000 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005001 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07005002 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005003 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07005004 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005005 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005006 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005007 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005008 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07005009 break;
5010 default:
5011 BUG();
5012 return;
5013 }
5014}
5015
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005016/**
5017 * iwl3945_get_channel_info - Find driver's private channel info
5018 *
5019 * Based on band and channel number.
5020 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005021const struct iwl3945_channel_info *iwl3945_get_channel_info(const struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005022 int phymode, u16 channel)
5023{
5024 int i;
5025
5026 switch (phymode) {
5027 case MODE_IEEE80211A:
5028 for (i = 14; i < priv->channel_count; i++) {
5029 if (priv->channel_info[i].channel == channel)
5030 return &priv->channel_info[i];
5031 }
5032 break;
5033
5034 case MODE_IEEE80211B:
5035 case MODE_IEEE80211G:
5036 if (channel >= 1 && channel <= 14)
5037 return &priv->channel_info[channel - 1];
5038 break;
5039
5040 }
5041
5042 return NULL;
5043}
5044
5045#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5046 ? # x " " : "")
5047
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005048/**
5049 * iwl3945_init_channel_map - Set up driver's info for all possible channels
5050 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005051static int iwl3945_init_channel_map(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005052{
5053 int eeprom_ch_count = 0;
5054 const u8 *eeprom_ch_index = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005055 const struct iwl3945_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005056 int band, ch;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005057 struct iwl3945_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005058
5059 if (priv->channel_count) {
5060 IWL_DEBUG_INFO("Channel map already initialized.\n");
5061 return 0;
5062 }
5063
5064 if (priv->eeprom.version < 0x2f) {
5065 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5066 priv->eeprom.version);
5067 return -EINVAL;
5068 }
5069
5070 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5071
5072 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005073 ARRAY_SIZE(iwl3945_eeprom_band_1) +
5074 ARRAY_SIZE(iwl3945_eeprom_band_2) +
5075 ARRAY_SIZE(iwl3945_eeprom_band_3) +
5076 ARRAY_SIZE(iwl3945_eeprom_band_4) +
5077 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005078
5079 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5080
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005081 priv->channel_info = kzalloc(sizeof(struct iwl3945_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07005082 priv->channel_count, GFP_KERNEL);
5083 if (!priv->channel_info) {
5084 IWL_ERROR("Could not allocate channel_info\n");
5085 priv->channel_count = 0;
5086 return -ENOMEM;
5087 }
5088
5089 ch_info = priv->channel_info;
5090
5091 /* Loop through the 5 EEPROM bands adding them in order to the
5092 * channel map we maintain (that contains additional information than
5093 * what just in the EEPROM) */
5094 for (band = 1; band <= 5; band++) {
5095
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005096 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005097 &eeprom_ch_info, &eeprom_ch_index);
5098
5099 /* Loop through each band adding each of the channels */
5100 for (ch = 0; ch < eeprom_ch_count; ch++) {
5101 ch_info->channel = eeprom_ch_index[ch];
5102 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5103 MODE_IEEE80211A;
5104
5105 /* permanently store EEPROM's channel regulatory flags
5106 * and max power in channel info database. */
5107 ch_info->eeprom = eeprom_ch_info[ch];
5108
5109 /* Copy the run-time flags so they are there even on
5110 * invalid channels */
5111 ch_info->flags = eeprom_ch_info[ch].flags;
5112
5113 if (!(is_channel_valid(ch_info))) {
5114 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5115 "No traffic\n",
5116 ch_info->channel,
5117 ch_info->flags,
5118 is_channel_a_band(ch_info) ?
5119 "5.2" : "2.4");
5120 ch_info++;
5121 continue;
5122 }
5123
5124 /* Initialize regulatory-based run-time data */
5125 ch_info->max_power_avg = ch_info->curr_txpow =
5126 eeprom_ch_info[ch].max_power_avg;
5127 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5128 ch_info->min_power = 0;
5129
5130 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5131 " %ddBm): Ad-Hoc %ssupported\n",
5132 ch_info->channel,
5133 is_channel_a_band(ch_info) ?
5134 "5.2" : "2.4",
5135 CHECK_AND_PRINT(IBSS),
5136 CHECK_AND_PRINT(ACTIVE),
5137 CHECK_AND_PRINT(RADAR),
5138 CHECK_AND_PRINT(WIDE),
5139 CHECK_AND_PRINT(NARROW),
5140 CHECK_AND_PRINT(DFS),
5141 eeprom_ch_info[ch].flags,
5142 eeprom_ch_info[ch].max_power_avg,
5143 ((eeprom_ch_info[ch].
5144 flags & EEPROM_CHANNEL_IBSS)
5145 && !(eeprom_ch_info[ch].
5146 flags & EEPROM_CHANNEL_RADAR))
5147 ? "" : "not ");
5148
5149 /* Set the user_txpower_limit to the highest power
5150 * supported by any channel */
5151 if (eeprom_ch_info[ch].max_power_avg >
5152 priv->user_txpower_limit)
5153 priv->user_txpower_limit =
5154 eeprom_ch_info[ch].max_power_avg;
5155
5156 ch_info++;
5157 }
5158 }
5159
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005160 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07005161 if (iwl3945_txpower_set_from_eeprom(priv))
5162 return -EIO;
5163
5164 return 0;
5165}
5166
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005167/*
5168 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
5169 */
5170static void iwl3945_free_channel_map(struct iwl3945_priv *priv)
5171{
5172 kfree(priv->channel_info);
5173 priv->channel_count = 0;
5174}
5175
Zhu Yib481de92007-09-25 17:54:57 -07005176/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5177 * sending probe req. This should be set long enough to hear probe responses
5178 * from more than one AP. */
5179#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5180#define IWL_ACTIVE_DWELL_TIME_52 (10)
5181
5182/* For faster active scanning, scan will move to the next channel if fewer than
5183 * PLCP_QUIET_THRESH packets are heard on this channel within
5184 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5185 * time if it's a quiet channel (nothing responded to our probe, and there's
5186 * no other traffic).
5187 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5188#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5189#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5190
5191/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5192 * Must be set longer than active dwell time.
5193 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5194#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5195#define IWL_PASSIVE_DWELL_TIME_52 (10)
5196#define IWL_PASSIVE_DWELL_BASE (100)
5197#define IWL_CHANNEL_TUNE_TIME 5
5198
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005199static inline u16 iwl3945_get_active_dwell_time(struct iwl3945_priv *priv, int phymode)
Zhu Yib481de92007-09-25 17:54:57 -07005200{
5201 if (phymode == MODE_IEEE80211A)
5202 return IWL_ACTIVE_DWELL_TIME_52;
5203 else
5204 return IWL_ACTIVE_DWELL_TIME_24;
5205}
5206
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005207static u16 iwl3945_get_passive_dwell_time(struct iwl3945_priv *priv, int phymode)
Zhu Yib481de92007-09-25 17:54:57 -07005208{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005209 u16 active = iwl3945_get_active_dwell_time(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005210 u16 passive = (phymode != MODE_IEEE80211A) ?
5211 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5212 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5213
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005214 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005215 /* If we're associated, we clamp the maximum passive
5216 * dwell time to be 98% of the beacon interval (minus
5217 * 2 * channel tune time) */
5218 passive = priv->beacon_int;
5219 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5220 passive = IWL_PASSIVE_DWELL_BASE;
5221 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5222 }
5223
5224 if (passive <= active)
5225 passive = active + 1;
5226
5227 return passive;
5228}
5229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005230static int iwl3945_get_channels_for_scan(struct iwl3945_priv *priv, int phymode,
Zhu Yib481de92007-09-25 17:54:57 -07005231 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005232 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07005233{
5234 const struct ieee80211_channel *channels = NULL;
5235 const struct ieee80211_hw_mode *hw_mode;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005236 const struct iwl3945_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005237 u16 passive_dwell = 0;
5238 u16 active_dwell = 0;
5239 int added, i;
5240
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005241 hw_mode = iwl3945_get_hw_mode(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005242 if (!hw_mode)
5243 return 0;
5244
5245 channels = hw_mode->channels;
5246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005247 active_dwell = iwl3945_get_active_dwell_time(priv, phymode);
5248 passive_dwell = iwl3945_get_passive_dwell_time(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005249
5250 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5251 if (channels[i].chan ==
5252 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005253 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005254 IWL_DEBUG_SCAN
5255 ("Skipping current channel %d\n",
5256 le16_to_cpu(priv->active_rxon.channel));
5257 continue;
5258 }
5259 } else if (priv->only_active_channel)
5260 continue;
5261
5262 scan_ch->channel = channels[i].chan;
5263
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005264 ch_info = iwl3945_get_channel_info(priv, phymode, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005265 if (!is_channel_valid(ch_info)) {
5266 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5267 scan_ch->channel);
5268 continue;
5269 }
5270
5271 if (!is_active || is_channel_passive(ch_info) ||
5272 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5273 scan_ch->type = 0; /* passive */
5274 else
5275 scan_ch->type = 1; /* active */
5276
5277 if (scan_ch->type & 1)
5278 scan_ch->type |= (direct_mask << 1);
5279
5280 if (is_channel_narrow(ch_info))
5281 scan_ch->type |= (1 << 7);
5282
5283 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5284 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5285
Ben Cahill9fbab512007-11-29 11:09:47 +08005286 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07005287 scan_ch->tpc.dsp_atten = 110;
5288 /* scan_pwr_info->tpc.dsp_atten; */
5289
5290 /*scan_pwr_info->tpc.tx_gain; */
5291 if (phymode == MODE_IEEE80211A)
5292 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5293 else {
5294 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5295 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08005296 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08005297 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07005298 */
5299 }
5300
5301 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5302 scan_ch->channel,
5303 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5304 (scan_ch->type & 1) ?
5305 active_dwell : passive_dwell);
5306
5307 scan_ch++;
5308 added++;
5309 }
5310
5311 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5312 return added;
5313}
5314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005315static void iwl3945_reset_channel_flag(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005316{
5317 int i, j;
5318 for (i = 0; i < 3; i++) {
5319 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5320 for (j = 0; j < hw_mode->num_channels; j++)
5321 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5322 }
5323}
5324
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005325static void iwl3945_init_hw_rates(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005326 struct ieee80211_rate *rates)
5327{
5328 int i;
5329
5330 for (i = 0; i < IWL_RATE_COUNT; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005331 rates[i].rate = iwl3945_rates[i].ieee * 5;
Zhu Yib481de92007-09-25 17:54:57 -07005332 rates[i].val = i; /* Rate scaling will work on indexes */
5333 rates[i].val2 = i;
5334 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5335 /* Only OFDM have the bits-per-symbol set */
5336 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5337 rates[i].flags |= IEEE80211_RATE_OFDM;
5338 else {
5339 /*
5340 * If CCK 1M then set rate flag to CCK else CCK_2
5341 * which is CCK | PREAMBLE2
5342 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005343 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Zhu Yib481de92007-09-25 17:54:57 -07005344 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5345 }
5346
5347 /* Set up which ones are basic rates... */
5348 if (IWL_BASIC_RATES_MASK & (1 << i))
5349 rates[i].flags |= IEEE80211_RATE_BASIC;
5350 }
5351}
5352
5353/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005354 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005355 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005356static int iwl3945_init_geos(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005357{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005358 struct iwl3945_channel_info *ch;
Zhu Yib481de92007-09-25 17:54:57 -07005359 struct ieee80211_hw_mode *modes;
5360 struct ieee80211_channel *channels;
5361 struct ieee80211_channel *geo_ch;
5362 struct ieee80211_rate *rates;
5363 int i = 0;
5364 enum {
5365 A = 0,
5366 B = 1,
5367 G = 2,
5368 };
5369 int mode_count = 3;
5370
5371 if (priv->modes) {
5372 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5373 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5374 return 0;
5375 }
5376
5377 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5378 GFP_KERNEL);
5379 if (!modes)
5380 return -ENOMEM;
5381
5382 channels = kzalloc(sizeof(struct ieee80211_channel) *
5383 priv->channel_count, GFP_KERNEL);
5384 if (!channels) {
5385 kfree(modes);
5386 return -ENOMEM;
5387 }
5388
5389 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5390 GFP_KERNEL);
5391 if (!rates) {
5392 kfree(modes);
5393 kfree(channels);
5394 return -ENOMEM;
5395 }
5396
5397 /* 0 = 802.11a
5398 * 1 = 802.11b
5399 * 2 = 802.11g
5400 */
5401
5402 /* 5.2GHz channels start after the 2.4GHz channels */
5403 modes[A].mode = MODE_IEEE80211A;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005404 modes[A].channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
Mohamed Abbas14577f22007-11-12 11:37:42 +08005405 modes[A].rates = &rates[4];
Zhu Yib481de92007-09-25 17:54:57 -07005406 modes[A].num_rates = 8; /* just OFDM */
5407 modes[A].num_channels = 0;
5408
5409 modes[B].mode = MODE_IEEE80211B;
5410 modes[B].channels = channels;
Mohamed Abbas14577f22007-11-12 11:37:42 +08005411 modes[B].rates = rates;
Zhu Yib481de92007-09-25 17:54:57 -07005412 modes[B].num_rates = 4; /* just CCK */
5413 modes[B].num_channels = 0;
5414
5415 modes[G].mode = MODE_IEEE80211G;
5416 modes[G].channels = channels;
5417 modes[G].rates = rates;
5418 modes[G].num_rates = 12; /* OFDM & CCK */
5419 modes[G].num_channels = 0;
5420
5421 priv->ieee_channels = channels;
5422 priv->ieee_rates = rates;
5423
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005424 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005425
5426 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5427 ch = &priv->channel_info[i];
5428
5429 if (!is_channel_valid(ch)) {
5430 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5431 "skipping.\n",
5432 ch->channel, is_channel_a_band(ch) ?
5433 "5.2" : "2.4");
5434 continue;
5435 }
5436
5437 if (is_channel_a_band(ch))
5438 geo_ch = &modes[A].channels[modes[A].num_channels++];
5439 else {
5440 geo_ch = &modes[B].channels[modes[B].num_channels++];
5441 modes[G].num_channels++;
5442 }
5443
5444 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5445 geo_ch->chan = ch->channel;
5446 geo_ch->power_level = ch->max_power_avg;
5447 geo_ch->antenna_max = 0xff;
5448
5449 if (is_channel_valid(ch)) {
5450 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5451 if (ch->flags & EEPROM_CHANNEL_IBSS)
5452 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5453
5454 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5455 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5456
5457 if (ch->flags & EEPROM_CHANNEL_RADAR)
5458 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5459
5460 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5461 priv->max_channel_txpower_limit =
5462 ch->max_power_avg;
5463 }
5464
5465 geo_ch->val = geo_ch->flag;
5466 }
5467
5468 if ((modes[A].num_channels == 0) && priv->is_abg) {
5469 printk(KERN_INFO DRV_NAME
5470 ": Incorrectly detected BG card as ABG. Please send "
5471 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5472 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5473 priv->is_abg = 0;
5474 }
5475
5476 printk(KERN_INFO DRV_NAME
5477 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5478 modes[G].num_channels, modes[A].num_channels);
5479
5480 /*
5481 * NOTE: We register these in preference of order -- the
5482 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5483 * a phymode based on rates or AP capabilities but seems to
5484 * configure it purely on if the channel being configured
5485 * is supported by a mode -- and the first match is taken
5486 */
5487
5488 if (modes[G].num_channels)
5489 ieee80211_register_hwmode(priv->hw, &modes[G]);
5490 if (modes[B].num_channels)
5491 ieee80211_register_hwmode(priv->hw, &modes[B]);
5492 if (modes[A].num_channels)
5493 ieee80211_register_hwmode(priv->hw, &modes[A]);
5494
5495 priv->modes = modes;
5496 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5497
5498 return 0;
5499}
5500
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005501/*
5502 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
5503 */
5504static void iwl3945_free_geos(struct iwl3945_priv *priv)
5505{
5506 kfree(priv->modes);
5507 kfree(priv->ieee_channels);
5508 kfree(priv->ieee_rates);
5509 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5510}
5511
Zhu Yib481de92007-09-25 17:54:57 -07005512/******************************************************************************
5513 *
5514 * uCode download functions
5515 *
5516 ******************************************************************************/
5517
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005518static void iwl3945_dealloc_ucode_pci(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005519{
Tomas Winkler98c92212008-01-14 17:46:20 -08005520 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5521 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5522 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5523 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5524 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5525 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005526}
5527
5528/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005529 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005530 * looking at all data.
5531 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005532static int iwl3945_verify_inst_full(struct iwl3945_priv *priv, __le32 * image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005533{
5534 u32 val;
5535 u32 save_len = len;
5536 int rc = 0;
5537 u32 errcnt;
5538
5539 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005541 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005542 if (rc)
5543 return rc;
5544
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005545 iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005546
5547 errcnt = 0;
5548 for (; len > 0; len -= sizeof(u32), image++) {
5549 /* read data comes through single port, auto-incr addr */
5550 /* NOTE: Use the debugless read so we don't flood kernel log
5551 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005552 val = _iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005553 if (val != le32_to_cpu(*image)) {
5554 IWL_ERROR("uCode INST section is invalid at "
5555 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5556 save_len - len, val, le32_to_cpu(*image));
5557 rc = -EIO;
5558 errcnt++;
5559 if (errcnt >= 20)
5560 break;
5561 }
5562 }
5563
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005564 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005565
5566 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08005567 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07005568
5569 return rc;
5570}
5571
5572
5573/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005574 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005575 * using sample data 100 bytes apart. If these sample points are good,
5576 * it's a pretty good bet that everything between them is good, too.
5577 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005578static int iwl3945_verify_inst_sparse(struct iwl3945_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005579{
5580 u32 val;
5581 int rc = 0;
5582 u32 errcnt = 0;
5583 u32 i;
5584
5585 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5586
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005587 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005588 if (rc)
5589 return rc;
5590
5591 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5592 /* read data comes through single port, auto-incr addr */
5593 /* NOTE: Use the debugless read so we don't flood kernel log
5594 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005595 iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005596 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005597 val = _iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005598 if (val != le32_to_cpu(*image)) {
5599#if 0 /* Enable this if you want to see details */
5600 IWL_ERROR("uCode INST section is invalid at "
5601 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5602 i, val, *image);
5603#endif
5604 rc = -EIO;
5605 errcnt++;
5606 if (errcnt >= 3)
5607 break;
5608 }
5609 }
5610
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005611 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005612
5613 return rc;
5614}
5615
5616
5617/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005618 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005619 * and verify its contents
5620 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005621static int iwl3945_verify_ucode(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005622{
5623 __le32 *image;
5624 u32 len;
5625 int rc = 0;
5626
5627 /* Try bootstrap */
5628 image = (__le32 *)priv->ucode_boot.v_addr;
5629 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005630 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005631 if (rc == 0) {
5632 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5633 return 0;
5634 }
5635
5636 /* Try initialize */
5637 image = (__le32 *)priv->ucode_init.v_addr;
5638 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005639 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005640 if (rc == 0) {
5641 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5642 return 0;
5643 }
5644
5645 /* Try runtime/protocol */
5646 image = (__le32 *)priv->ucode_code.v_addr;
5647 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005648 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005649 if (rc == 0) {
5650 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5651 return 0;
5652 }
5653
5654 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5655
Ben Cahill9fbab512007-11-29 11:09:47 +08005656 /* Since nothing seems to match, show first several data entries in
5657 * instruction SRAM, so maybe visual inspection will give a clue.
5658 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005659 image = (__le32 *)priv->ucode_boot.v_addr;
5660 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005661 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005662
5663 return rc;
5664}
5665
5666
5667/* check contents of special bootstrap uCode SRAM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005668static int iwl3945_verify_bsm(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005669{
5670 __le32 *image = priv->ucode_boot.v_addr;
5671 u32 len = priv->ucode_boot.len;
5672 u32 reg;
5673 u32 val;
5674
5675 IWL_DEBUG_INFO("Begin verify bsm\n");
5676
5677 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005678 val = iwl3945_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005679 for (reg = BSM_SRAM_LOWER_BOUND;
5680 reg < BSM_SRAM_LOWER_BOUND + len;
5681 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005682 val = iwl3945_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005683 if (val != le32_to_cpu(*image)) {
5684 IWL_ERROR("BSM uCode verification failed at "
5685 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5686 BSM_SRAM_LOWER_BOUND,
5687 reg - BSM_SRAM_LOWER_BOUND, len,
5688 val, le32_to_cpu(*image));
5689 return -EIO;
5690 }
5691 }
5692
5693 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5694
5695 return 0;
5696}
5697
5698/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005699 * iwl3945_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005700 *
5701 * BSM operation:
5702 *
5703 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5704 * in special SRAM that does not power down during RFKILL. When powering back
5705 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5706 * the bootstrap program into the on-board processor, and starts it.
5707 *
5708 * The bootstrap program loads (via DMA) instructions and data for a new
5709 * program from host DRAM locations indicated by the host driver in the
5710 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5711 * automatically.
5712 *
5713 * When initializing the NIC, the host driver points the BSM to the
5714 * "initialize" uCode image. This uCode sets up some internal data, then
5715 * notifies host via "initialize alive" that it is complete.
5716 *
5717 * The host then replaces the BSM_DRAM_* pointer values to point to the
5718 * normal runtime uCode instructions and a backup uCode data cache buffer
5719 * (filled initially with starting data values for the on-board processor),
5720 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5721 * which begins normal operation.
5722 *
5723 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5724 * the backup data cache in DRAM before SRAM is powered down.
5725 *
5726 * When powering back up, the BSM loads the bootstrap program. This reloads
5727 * the runtime uCode instructions and the backup data cache into SRAM,
5728 * and re-launches the runtime uCode from where it left off.
5729 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005730static int iwl3945_load_bsm(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005731{
5732 __le32 *image = priv->ucode_boot.v_addr;
5733 u32 len = priv->ucode_boot.len;
5734 dma_addr_t pinst;
5735 dma_addr_t pdata;
5736 u32 inst_len;
5737 u32 data_len;
5738 int rc;
5739 int i;
5740 u32 done;
5741 u32 reg_offset;
5742
5743 IWL_DEBUG_INFO("Begin load bsm\n");
5744
5745 /* make sure bootstrap program is no larger than BSM's SRAM size */
5746 if (len > IWL_MAX_BSM_SIZE)
5747 return -EINVAL;
5748
5749 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005750 * in host DRAM ... host DRAM physical address bits 31:0 for 3945.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005751 * NOTE: iwl3945_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005752 * after the "initialize" uCode has run, to point to
5753 * runtime/protocol instructions and backup data cache. */
5754 pinst = priv->ucode_init.p_addr;
5755 pdata = priv->ucode_init_data.p_addr;
5756 inst_len = priv->ucode_init.len;
5757 data_len = priv->ucode_init_data.len;
5758
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005759 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005760 if (rc)
5761 return rc;
5762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005763 iwl3945_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5764 iwl3945_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5765 iwl3945_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5766 iwl3945_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005767
5768 /* Fill BSM memory with bootstrap instructions */
5769 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5770 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5771 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005772 _iwl3945_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005773 le32_to_cpu(*image));
5774
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005775 rc = iwl3945_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005776 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005777 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005778 return rc;
5779 }
5780
5781 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005782 iwl3945_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5783 iwl3945_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005784 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005785 iwl3945_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005786
5787 /* Load bootstrap code into instruction SRAM now,
5788 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005789 iwl3945_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005790 BSM_WR_CTRL_REG_BIT_START);
5791
5792 /* Wait for load of bootstrap uCode to finish */
5793 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005794 done = iwl3945_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005795 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5796 break;
5797 udelay(10);
5798 }
5799 if (i < 100)
5800 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5801 else {
5802 IWL_ERROR("BSM write did not complete!\n");
5803 return -EIO;
5804 }
5805
5806 /* Enable future boot loads whenever power management unit triggers it
5807 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005808 iwl3945_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005809 BSM_WR_CTRL_REG_BIT_START_EN);
5810
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005811 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005812
5813 return 0;
5814}
5815
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005816static void iwl3945_nic_start(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005817{
5818 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005819 iwl3945_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005820}
5821
5822/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005823 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005824 *
5825 * Copy into buffers for card to fetch via bus-mastering
5826 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005827static int iwl3945_read_ucode(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005828{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005829 struct iwl3945_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005830 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005831 const struct firmware *ucode_raw;
5832 /* firmware file name contains uCode/driver compatibility version */
5833 const char *name = "iwlwifi-3945" IWL3945_UCODE_API ".ucode";
5834 u8 *src;
5835 size_t len;
5836 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5837
5838 /* Ask kernel firmware_class module to get the boot firmware off disk.
5839 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005840 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5841 if (ret < 0) {
5842 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5843 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005844 goto error;
5845 }
5846
5847 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5848 name, ucode_raw->size);
5849
5850 /* Make sure that we got at least our header! */
5851 if (ucode_raw->size < sizeof(*ucode)) {
5852 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005853 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005854 goto err_release;
5855 }
5856
5857 /* Data from ucode file: header followed by uCode images */
5858 ucode = (void *)ucode_raw->data;
5859
5860 ver = le32_to_cpu(ucode->ver);
5861 inst_size = le32_to_cpu(ucode->inst_size);
5862 data_size = le32_to_cpu(ucode->data_size);
5863 init_size = le32_to_cpu(ucode->init_size);
5864 init_data_size = le32_to_cpu(ucode->init_data_size);
5865 boot_size = le32_to_cpu(ucode->boot_size);
5866
5867 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08005868 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
5869 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
5870 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
5871 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
5872 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07005873
5874 /* Verify size of file vs. image size info in file's header */
5875 if (ucode_raw->size < sizeof(*ucode) +
5876 inst_size + data_size + init_size +
5877 init_data_size + boot_size) {
5878
5879 IWL_DEBUG_INFO("uCode file size %d too small\n",
5880 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005881 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005882 goto err_release;
5883 }
5884
5885 /* Verify that uCode images will fit in card's SRAM */
5886 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005887 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5888 inst_size);
5889 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005890 goto err_release;
5891 }
5892
5893 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005894 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5895 data_size);
5896 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005897 goto err_release;
5898 }
5899 if (init_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005900 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
5901 init_size);
5902 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005903 goto err_release;
5904 }
5905 if (init_data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005906 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
5907 init_data_size);
5908 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005909 goto err_release;
5910 }
5911 if (boot_size > IWL_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005912 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
5913 boot_size);
5914 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005915 goto err_release;
5916 }
5917
5918 /* Allocate ucode buffers for card's bus-master loading ... */
5919
5920 /* Runtime instructions and 2 copies of data:
5921 * 1) unmodified from disk
5922 * 2) backup cache for save/restore during power-downs */
5923 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005924 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005925
5926 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005927 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005928
5929 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005930 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005931
5932 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08005933 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07005934 goto err_pci_alloc;
5935
Tomas Winkler90e759d2007-11-29 11:09:41 +08005936 /* Initialization instructions and data */
5937 if (init_size && init_data_size) {
5938 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005939 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005940
5941 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005942 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005943
5944 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5945 goto err_pci_alloc;
5946 }
5947
5948 /* Bootstrap (instructions only, no data) */
5949 if (boot_size) {
5950 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005951 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005952
5953 if (!priv->ucode_boot.v_addr)
5954 goto err_pci_alloc;
5955 }
5956
Zhu Yib481de92007-09-25 17:54:57 -07005957 /* Copy images into buffers for card's bus-master reads ... */
5958
5959 /* Runtime instructions (first block of data in file) */
5960 src = &ucode->data[0];
5961 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005962 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005963 memcpy(priv->ucode_code.v_addr, src, len);
5964 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5965 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5966
5967 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005968 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005969 src = &ucode->data[inst_size];
5970 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005971 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005972 memcpy(priv->ucode_data.v_addr, src, len);
5973 memcpy(priv->ucode_data_backup.v_addr, src, len);
5974
5975 /* Initialization instructions (3rd block) */
5976 if (init_size) {
5977 src = &ucode->data[inst_size + data_size];
5978 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005979 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5980 len);
Zhu Yib481de92007-09-25 17:54:57 -07005981 memcpy(priv->ucode_init.v_addr, src, len);
5982 }
5983
5984 /* Initialization data (4th block) */
5985 if (init_data_size) {
5986 src = &ucode->data[inst_size + data_size + init_size];
5987 len = priv->ucode_init_data.len;
5988 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5989 (int)len);
5990 memcpy(priv->ucode_init_data.v_addr, src, len);
5991 }
5992
5993 /* Bootstrap instructions (5th block) */
5994 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5995 len = priv->ucode_boot.len;
5996 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5997 (int)len);
5998 memcpy(priv->ucode_boot.v_addr, src, len);
5999
6000 /* We have our copies now, allow OS release its copies */
6001 release_firmware(ucode_raw);
6002 return 0;
6003
6004 err_pci_alloc:
6005 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006006 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006007 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006008
6009 err_release:
6010 release_firmware(ucode_raw);
6011
6012 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08006013 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006014}
6015
6016
6017/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006018 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07006019 *
6020 * Tell initialization uCode where to find runtime uCode.
6021 *
6022 * BSM registers initially contain pointers to initialization uCode.
6023 * We need to replace them to load runtime uCode inst and data,
6024 * and to save runtime data when powering down.
6025 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006026static int iwl3945_set_ucode_ptrs(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006027{
6028 dma_addr_t pinst;
6029 dma_addr_t pdata;
6030 int rc = 0;
6031 unsigned long flags;
6032
6033 /* bits 31:0 for 3945 */
6034 pinst = priv->ucode_code.p_addr;
6035 pdata = priv->ucode_data_backup.p_addr;
6036
6037 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006038 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006039 if (rc) {
6040 spin_unlock_irqrestore(&priv->lock, flags);
6041 return rc;
6042 }
6043
6044 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006045 iwl3945_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6046 iwl3945_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6047 iwl3945_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006048 priv->ucode_data.len);
6049
6050 /* Inst bytecount must be last to set up, bit 31 signals uCode
6051 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006052 iwl3945_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006053 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6054
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006055 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006056
6057 spin_unlock_irqrestore(&priv->lock, flags);
6058
6059 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6060
6061 return rc;
6062}
6063
6064/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006065 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006066 *
6067 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6068 *
Zhu Yib481de92007-09-25 17:54:57 -07006069 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08006070 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006071static void iwl3945_init_alive_start(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006072{
6073 /* Check alive response for "valid" sign from uCode */
6074 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6075 /* We had an error bringing up the hardware, so take it
6076 * all the way back down so we can try again */
6077 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6078 goto restart;
6079 }
6080
6081 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6082 * This is a paranoid check, because we would not have gotten the
6083 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006084 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006085 /* Runtime instruction load was bad;
6086 * take it all the way back down so we can try again */
6087 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6088 goto restart;
6089 }
6090
6091 /* Send pointers to protocol/runtime uCode image ... init code will
6092 * load and launch runtime uCode, which will send us another "Alive"
6093 * notification. */
6094 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006095 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006096 /* Runtime instruction load won't happen;
6097 * take it all the way back down so we can try again */
6098 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6099 goto restart;
6100 }
6101 return;
6102
6103 restart:
6104 queue_work(priv->workqueue, &priv->restart);
6105}
6106
6107
6108/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006109 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006110 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006111 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07006112 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006113static void iwl3945_alive_start(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006114{
6115 int rc = 0;
6116 int thermal_spin = 0;
6117 u32 rfkill;
6118
6119 IWL_DEBUG_INFO("Runtime Alive received.\n");
6120
6121 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6122 /* We had an error bringing up the hardware, so take it
6123 * all the way back down so we can try again */
6124 IWL_DEBUG_INFO("Alive failed.\n");
6125 goto restart;
6126 }
6127
6128 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6129 * This is a paranoid check, because we would not have gotten the
6130 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006131 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006132 /* Runtime instruction load was bad;
6133 * take it all the way back down so we can try again */
6134 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6135 goto restart;
6136 }
6137
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006138 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006139
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006140 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006141 if (rc) {
6142 IWL_WARNING("Can not read rfkill status from adapter\n");
6143 return;
6144 }
6145
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006146 rfkill = iwl3945_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07006147 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006148 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006149
6150 if (rfkill & 0x1) {
6151 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6152 /* if rfkill is not on, then wait for thermal
6153 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006154 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07006155 thermal_spin++;
6156 udelay(10);
6157 }
6158
6159 if (thermal_spin)
6160 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
6161 thermal_spin * 10);
6162 } else
6163 set_bit(STATUS_RF_KILL_HW, &priv->status);
6164
Ben Cahill9fbab512007-11-29 11:09:47 +08006165 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07006166 set_bit(STATUS_ALIVE, &priv->status);
6167
6168 /* Clear out the uCode error bit if it is set */
6169 clear_bit(STATUS_FW_ERROR, &priv->status);
6170
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006171 if (iwl3945_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006172 return;
6173
Zhu Yi5a669262008-01-14 17:46:18 -08006174 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006175
6176 priv->active_rate = priv->rates_mask;
6177 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6178
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006179 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07006180
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006181 if (iwl3945_is_associated(priv)) {
6182 struct iwl3945_rxon_cmd *active_rxon =
6183 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07006184
6185 memcpy(&priv->staging_rxon, &priv->active_rxon,
6186 sizeof(priv->staging_rxon));
6187 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6188 } else {
6189 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006190 iwl3945_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006191 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6192 }
6193
Ben Cahill9fbab512007-11-29 11:09:47 +08006194 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006195 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006196
6197 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006198 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006199
6200 /* At this point, the NIC is initialized and operational */
6201 priv->notif_missed_beacons = 0;
6202 set_bit(STATUS_READY, &priv->status);
6203
6204 iwl3945_reg_txpower_periodic(priv);
6205
6206 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006207 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07006208
6209 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006210 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006211
6212 return;
6213
6214 restart:
6215 queue_work(priv->workqueue, &priv->restart);
6216}
6217
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006218static void iwl3945_cancel_deferred_work(struct iwl3945_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07006219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006220static void __iwl3945_down(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006221{
6222 unsigned long flags;
6223 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6224 struct ieee80211_conf *conf = NULL;
6225
6226 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6227
6228 conf = ieee80211_get_hw_conf(priv->hw);
6229
6230 if (!exit_pending)
6231 set_bit(STATUS_EXIT_PENDING, &priv->status);
6232
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006233 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006234
6235 /* Unblock any waiting calls */
6236 wake_up_interruptible_all(&priv->wait_command_queue);
6237
Zhu Yib481de92007-09-25 17:54:57 -07006238 /* Wipe out the EXIT_PENDING status bit if we are not actually
6239 * exiting the module */
6240 if (!exit_pending)
6241 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6242
6243 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006244 iwl3945_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07006245
6246 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006247 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006248
6249 if (priv->mac80211_registered)
6250 ieee80211_stop_queues(priv->hw);
6251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006252 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07006253 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006254 if (!iwl3945_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006255 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6256 STATUS_RF_KILL_HW |
6257 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6258 STATUS_RF_KILL_SW |
6259 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6260 STATUS_IN_SUSPEND;
6261 goto exit;
6262 }
6263
6264 /* ...otherwise clear out all the status bits but the RF Kill and
6265 * SUSPEND bits and continue taking the NIC down. */
6266 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6267 STATUS_RF_KILL_HW |
6268 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6269 STATUS_RF_KILL_SW |
6270 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6271 STATUS_IN_SUSPEND |
6272 test_bit(STATUS_FW_ERROR, &priv->status) <<
6273 STATUS_FW_ERROR;
6274
6275 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006276 iwl3945_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07006277 spin_unlock_irqrestore(&priv->lock, flags);
6278
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006279 iwl3945_hw_txq_ctx_stop(priv);
6280 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006281
6282 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006283 if (!iwl3945_grab_nic_access(priv)) {
6284 iwl3945_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006285 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006286 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006287 }
6288 spin_unlock_irqrestore(&priv->lock, flags);
6289
6290 udelay(5);
6291
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006292 iwl3945_hw_nic_stop_master(priv);
6293 iwl3945_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6294 iwl3945_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006295
6296 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006297 memset(&priv->card_alive, 0, sizeof(struct iwl3945_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07006298
6299 if (priv->ibss_beacon)
6300 dev_kfree_skb(priv->ibss_beacon);
6301 priv->ibss_beacon = NULL;
6302
6303 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006304 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006305}
6306
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006307static void iwl3945_down(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006308{
6309 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006310 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006311 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006312
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006313 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006314}
6315
6316#define MAX_HW_RESTARTS 5
6317
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006318static int __iwl3945_up(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006319{
6320 int rc, i;
6321
6322 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6323 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6324 return -EIO;
6325 }
6326
6327 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6328 IWL_WARNING("Radio disabled by SW RF kill (module "
6329 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08006330 return -ENODEV;
6331 }
6332
6333 /* If platform's RF_KILL switch is NOT set to KILL */
6334 if (iwl3945_read32(priv, CSR_GP_CNTRL) &
6335 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
6336 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6337 else {
6338 set_bit(STATUS_RF_KILL_HW, &priv->status);
6339 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
6340 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6341 return -ENODEV;
6342 }
Zhu Yib481de92007-09-25 17:54:57 -07006343 }
6344
Reinette Chatrea781cf92008-01-21 10:08:31 -08006345 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6346 IWL_ERROR("ucode not available for device bringup\n");
6347 return -EIO;
6348 }
6349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006350 iwl3945_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006351
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006352 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006353 if (rc) {
6354 IWL_ERROR("Unable to int nic\n");
6355 return rc;
6356 }
6357
6358 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006359 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6360 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006361 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6362
6363 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006364 iwl3945_write32(priv, CSR_INT, 0xFFFFFFFF);
6365 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006366
6367 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006368 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6369 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006370
6371 /* Copy original ucode data image from disk into backup cache.
6372 * This will be used to initialize the on-board processor's
6373 * data SRAM for a clean start when the runtime program first loads. */
6374 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08006375 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07006376
Zhu Yie655b9f2008-01-24 02:19:38 -08006377 /* We return success when we resume from suspend and rf_kill is on. */
6378 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
6379 return 0;
6380
Zhu Yib481de92007-09-25 17:54:57 -07006381 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6382
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006383 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006384
6385 /* load bootstrap state machine,
6386 * load bootstrap program into processor's memory,
6387 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006388 rc = iwl3945_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006389
6390 if (rc) {
6391 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6392 continue;
6393 }
6394
6395 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006396 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006397
Zhu Yib481de92007-09-25 17:54:57 -07006398 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6399
6400 return 0;
6401 }
6402
6403 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006404 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006405
6406 /* tried to restart and config the device for as long as our
6407 * patience could withstand */
6408 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6409 return -EIO;
6410}
6411
6412
6413/*****************************************************************************
6414 *
6415 * Workqueue callbacks
6416 *
6417 *****************************************************************************/
6418
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006419static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006420{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006421 struct iwl3945_priv *priv =
6422 container_of(data, struct iwl3945_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006423
6424 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6425 return;
6426
6427 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006428 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006429 mutex_unlock(&priv->mutex);
6430}
6431
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006432static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006433{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006434 struct iwl3945_priv *priv =
6435 container_of(data, struct iwl3945_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006436
6437 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6438 return;
6439
6440 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006441 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006442 mutex_unlock(&priv->mutex);
6443}
6444
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006445static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006446{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006447 struct iwl3945_priv *priv = container_of(work, struct iwl3945_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006448
6449 wake_up_interruptible(&priv->wait_command_queue);
6450
6451 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6452 return;
6453
6454 mutex_lock(&priv->mutex);
6455
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006456 if (!iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006457 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6458 "HW and/or SW RF Kill no longer active, restarting "
6459 "device\n");
6460 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6461 queue_work(priv->workqueue, &priv->restart);
6462 } else {
6463
6464 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6465 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6466 "disabled by SW switch\n");
6467 else
6468 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6469 "Kill switch must be turned off for "
6470 "wireless networking to work.\n");
6471 }
6472 mutex_unlock(&priv->mutex);
6473}
6474
6475#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6476
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006477static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006478{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006479 struct iwl3945_priv *priv =
6480 container_of(data, struct iwl3945_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006481
6482 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6483 return;
6484
6485 mutex_lock(&priv->mutex);
6486 if (test_bit(STATUS_SCANNING, &priv->status) ||
6487 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6488 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6489 "Scan completion watchdog resetting adapter (%dms)\n",
6490 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006491
Zhu Yib481de92007-09-25 17:54:57 -07006492 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006493 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006494 }
6495 mutex_unlock(&priv->mutex);
6496}
6497
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006498static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006499{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006500 struct iwl3945_priv *priv =
6501 container_of(data, struct iwl3945_priv, request_scan);
6502 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006503 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006504 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006505 .meta.flags = CMD_SIZE_HUGE,
6506 };
6507 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006508 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006509 struct ieee80211_conf *conf = NULL;
6510 u8 direct_mask;
6511 int phymode;
6512
6513 conf = ieee80211_get_hw_conf(priv->hw);
6514
6515 mutex_lock(&priv->mutex);
6516
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006517 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006518 IWL_WARNING("request scan called when driver not ready.\n");
6519 goto done;
6520 }
6521
6522 /* Make sure the scan wasn't cancelled before this queued work
6523 * was given the chance to run... */
6524 if (!test_bit(STATUS_SCANNING, &priv->status))
6525 goto done;
6526
6527 /* This should never be called or scheduled if there is currently
6528 * a scan active in the hardware. */
6529 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6530 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6531 "Ignoring second request.\n");
6532 rc = -EIO;
6533 goto done;
6534 }
6535
6536 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6537 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6538 goto done;
6539 }
6540
6541 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6542 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6543 goto done;
6544 }
6545
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006546 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006547 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6548 goto done;
6549 }
6550
6551 if (!test_bit(STATUS_READY, &priv->status)) {
6552 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6553 goto done;
6554 }
6555
6556 if (!priv->scan_bands) {
6557 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6558 goto done;
6559 }
6560
6561 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006562 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006563 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6564 if (!priv->scan) {
6565 rc = -ENOMEM;
6566 goto done;
6567 }
6568 }
6569 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006570 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006571
6572 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6573 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6574
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006575 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006576 u16 interval = 0;
6577 u32 extra;
6578 u32 suspend_time = 100;
6579 u32 scan_suspend_time = 100;
6580 unsigned long flags;
6581
6582 IWL_DEBUG_INFO("Scanning while associated...\n");
6583
6584 spin_lock_irqsave(&priv->lock, flags);
6585 interval = priv->beacon_int;
6586 spin_unlock_irqrestore(&priv->lock, flags);
6587
6588 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006589 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006590 if (!interval)
6591 interval = suspend_time;
6592 /*
6593 * suspend time format:
6594 * 0-19: beacon interval in usec (time before exec.)
6595 * 20-23: 0
6596 * 24-31: number of beacons (suspend between channels)
6597 */
6598
6599 extra = (suspend_time / interval) << 24;
6600 scan_suspend_time = 0xFF0FFFFF &
6601 (extra | ((suspend_time % interval) * 1024));
6602
6603 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6604 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6605 scan_suspend_time, interval);
6606 }
6607
6608 /* We should add the ability for user to lock to PASSIVE ONLY */
6609 if (priv->one_direct_scan) {
6610 IWL_DEBUG_SCAN
6611 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006612 iwl3945_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006613 priv->direct_ssid_len));
6614 scan->direct_scan[0].id = WLAN_EID_SSID;
6615 scan->direct_scan[0].len = priv->direct_ssid_len;
6616 memcpy(scan->direct_scan[0].ssid,
6617 priv->direct_ssid, priv->direct_ssid_len);
6618 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006619 } else if (!iwl3945_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006620 scan->direct_scan[0].id = WLAN_EID_SSID;
6621 scan->direct_scan[0].len = priv->essid_len;
6622 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6623 direct_mask = 1;
6624 } else
6625 direct_mask = 0;
6626
6627 /* We don't build a direct scan probe request; the uCode will do
6628 * that based on the direct_mask added to each channel entry */
6629 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006630 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Zhu Yib481de92007-09-25 17:54:57 -07006631 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
6632 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6633 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6634 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6635
6636 /* flags + rate selection */
6637
6638 switch (priv->scan_bands) {
6639 case 2:
6640 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6641 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
6642 scan->good_CRC_th = 0;
6643 phymode = MODE_IEEE80211G;
6644 break;
6645
6646 case 1:
6647 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
6648 scan->good_CRC_th = IWL_GOOD_CRC_TH;
6649 phymode = MODE_IEEE80211A;
6650 break;
6651
6652 default:
6653 IWL_WARNING("Invalid scan band count\n");
6654 goto done;
6655 }
6656
6657 /* select Rx antennas */
6658 scan->flags |= iwl3945_get_antenna_flags(priv);
6659
6660 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6661 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6662
6663 if (direct_mask)
6664 IWL_DEBUG_SCAN
6665 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006666 iwl3945_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07006667 else
6668 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
6669
6670 scan->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006671 iwl3945_get_channels_for_scan(
Zhu Yib481de92007-09-25 17:54:57 -07006672 priv, phymode, 1, /* active */
6673 direct_mask,
6674 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6675
6676 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006677 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006678 cmd.data = scan;
6679 scan->len = cpu_to_le16(cmd.len);
6680
6681 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006682 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006683 if (rc)
6684 goto done;
6685
6686 queue_delayed_work(priv->workqueue, &priv->scan_check,
6687 IWL_SCAN_CHECK_WATCHDOG);
6688
6689 mutex_unlock(&priv->mutex);
6690 return;
6691
6692 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006693 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006694 queue_work(priv->workqueue, &priv->scan_completed);
6695 mutex_unlock(&priv->mutex);
6696}
6697
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006698static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006699{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006700 struct iwl3945_priv *priv = container_of(data, struct iwl3945_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006701
6702 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6703 return;
6704
6705 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006706 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006707 mutex_unlock(&priv->mutex);
6708}
6709
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006710static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006711{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006712 struct iwl3945_priv *priv = container_of(data, struct iwl3945_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006713
6714 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6715 return;
6716
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006717 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006718 queue_work(priv->workqueue, &priv->up);
6719}
6720
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006721static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006722{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006723 struct iwl3945_priv *priv =
6724 container_of(data, struct iwl3945_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006725
6726 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6727 return;
6728
6729 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006730 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006731 mutex_unlock(&priv->mutex);
6732}
6733
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006734#define IWL_DELAY_NEXT_SCAN (HZ*2)
6735
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006736static void iwl3945_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006737{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006738 struct iwl3945_priv *priv = container_of(data, struct iwl3945_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006739 post_associate.work);
6740
6741 int rc = 0;
6742 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07006743 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006744
6745 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6746 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6747 return;
6748 }
6749
6750
Joe Perches0795af52007-10-03 17:59:30 -07006751 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6752 priv->assoc_id,
6753 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006754
6755 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6756 return;
6757
6758 mutex_lock(&priv->mutex);
6759
Johannes Berg32bfd352007-12-19 01:31:26 +01006760 if (!priv->vif || !priv->is_open) {
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006761 mutex_unlock(&priv->mutex);
6762 return;
6763 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006764 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006765
Zhu Yib481de92007-09-25 17:54:57 -07006766 conf = ieee80211_get_hw_conf(priv->hw);
6767
6768 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006769 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006771 memset(&priv->rxon_timing, 0, sizeof(struct iwl3945_rxon_time_cmd));
6772 iwl3945_setup_rxon_timing(priv);
6773 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006774 sizeof(priv->rxon_timing), &priv->rxon_timing);
6775 if (rc)
6776 IWL_WARNING("REPLY_RXON_TIMING failed - "
6777 "Attempting to continue.\n");
6778
6779 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6780
6781 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6782
6783 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6784 priv->assoc_id, priv->beacon_int);
6785
6786 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6787 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6788 else
6789 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6790
6791 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6792 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6793 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6794 else
6795 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6796
6797 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6798 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6799
6800 }
6801
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006802 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006803
6804 switch (priv->iw_mode) {
6805 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006806 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006807 break;
6808
6809 case IEEE80211_IF_TYPE_IBSS:
6810
6811 /* clear out the station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006812 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006813
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006814 iwl3945_add_station(priv, iwl3945_broadcast_addr, 0, 0);
6815 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006816 iwl3945_sync_sta(priv, IWL_STA_ID,
6817 (priv->phymode == MODE_IEEE80211A)?
6818 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
6819 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006820 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
6821 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006822
6823 break;
6824
6825 default:
6826 IWL_ERROR("%s Should not be called in %d mode\n",
Ian Schrambc434dd2007-10-25 17:15:29 +08006827 __FUNCTION__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07006828 break;
6829 }
6830
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006831 iwl3945_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006832
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006833#ifdef CONFIG_IWL3945_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006834 iwl3945_activate_qos(priv, 0);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006835#endif /* CONFIG_IWL3945_QOS */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006836 /* we have just associated, don't start scan too early */
6837 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006838 mutex_unlock(&priv->mutex);
6839}
6840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006841static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006842{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006843 struct iwl3945_priv *priv = container_of(work, struct iwl3945_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006844
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006845 if (!iwl3945_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006846 return;
6847
6848 mutex_lock(&priv->mutex);
6849
6850 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006851 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006852
6853 mutex_unlock(&priv->mutex);
6854}
6855
Zhu Yi76bb77e2007-11-22 10:53:22 +08006856static int iwl3945_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6857
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006858static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006859{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006860 struct iwl3945_priv *priv =
6861 container_of(work, struct iwl3945_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006862
6863 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6864
6865 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6866 return;
6867
Zhu Yia0646472007-12-20 14:10:01 +08006868 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6869 iwl3945_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006870
Zhu Yib481de92007-09-25 17:54:57 -07006871 ieee80211_scan_completed(priv->hw);
6872
6873 /* Since setting the TXPOWER may have been deferred while
6874 * performing the scan, fire one off */
6875 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006876 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006877 mutex_unlock(&priv->mutex);
6878}
6879
6880/*****************************************************************************
6881 *
6882 * mac80211 entry point functions
6883 *
6884 *****************************************************************************/
6885
Zhu Yi5a669262008-01-14 17:46:18 -08006886#define UCODE_READY_TIMEOUT (2 * HZ)
6887
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006888static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006889{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006890 struct iwl3945_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08006891 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006892
6893 IWL_DEBUG_MAC80211("enter\n");
6894
Zhu Yi5a669262008-01-14 17:46:18 -08006895 if (pci_enable_device(priv->pci_dev)) {
6896 IWL_ERROR("Fail to pci_enable_device\n");
6897 return -ENODEV;
6898 }
6899 pci_restore_state(priv->pci_dev);
6900 pci_enable_msi(priv->pci_dev);
6901
6902 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
6903 DRV_NAME, priv);
6904 if (ret) {
6905 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6906 goto out_disable_msi;
6907 }
6908
Zhu Yib481de92007-09-25 17:54:57 -07006909 /* we should be verifying the device is ready to be opened */
6910 mutex_lock(&priv->mutex);
6911
Zhu Yi5a669262008-01-14 17:46:18 -08006912 memset(&priv->staging_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
6913 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6914 * ucode filename and max sizes are card-specific. */
6915
6916 if (!priv->ucode_code.len) {
6917 ret = iwl3945_read_ucode(priv);
6918 if (ret) {
6919 IWL_ERROR("Could not read microcode: %d\n", ret);
6920 mutex_unlock(&priv->mutex);
6921 goto out_release_irq;
6922 }
6923 }
6924
Zhu Yie655b9f2008-01-24 02:19:38 -08006925 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08006926
Zhu Yib481de92007-09-25 17:54:57 -07006927 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08006928
Zhu Yie655b9f2008-01-24 02:19:38 -08006929 if (ret)
6930 goto out_release_irq;
6931
6932 IWL_DEBUG_INFO("Start UP work.\n");
6933
6934 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6935 return 0;
6936
Zhu Yi5a669262008-01-14 17:46:18 -08006937 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6938 * mac80211 will not be run successfully. */
6939 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6940 test_bit(STATUS_READY, &priv->status),
6941 UCODE_READY_TIMEOUT);
6942 if (!ret) {
6943 if (!test_bit(STATUS_READY, &priv->status)) {
6944 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6945 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6946 ret = -ETIMEDOUT;
6947 goto out_release_irq;
6948 }
6949 }
6950
Zhu Yie655b9f2008-01-24 02:19:38 -08006951 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006952 IWL_DEBUG_MAC80211("leave\n");
6953 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006954
6955out_release_irq:
6956 free_irq(priv->pci_dev->irq, priv);
6957out_disable_msi:
6958 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006959 pci_disable_device(priv->pci_dev);
6960 priv->is_open = 0;
6961 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006962 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006963}
6964
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006965static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006966{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006967 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006968
6969 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006970
Zhu Yie655b9f2008-01-24 02:19:38 -08006971 if (!priv->is_open) {
6972 IWL_DEBUG_MAC80211("leave - skip\n");
6973 return;
6974 }
6975
Zhu Yib481de92007-09-25 17:54:57 -07006976 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006977
6978 if (iwl3945_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006979 /* stop mac, cancel any scan request and clear
6980 * RXON_FILTER_ASSOC_MSK BIT
6981 */
Zhu Yi5a669262008-01-14 17:46:18 -08006982 mutex_lock(&priv->mutex);
6983 iwl3945_scan_cancel_timeout(priv, 100);
6984 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006985 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006986 }
6987
Zhu Yi5a669262008-01-14 17:46:18 -08006988 iwl3945_down(priv);
6989
6990 flush_workqueue(priv->workqueue);
6991 free_irq(priv->pci_dev->irq, priv);
6992 pci_disable_msi(priv->pci_dev);
6993 pci_save_state(priv->pci_dev);
6994 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006995
Zhu Yib481de92007-09-25 17:54:57 -07006996 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006997}
6998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006999static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007000 struct ieee80211_tx_control *ctl)
7001{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007002 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007003
7004 IWL_DEBUG_MAC80211("enter\n");
7005
7006 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7007 IWL_DEBUG_MAC80211("leave - monitor\n");
7008 return -1;
7009 }
7010
7011 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
7012 ctl->tx_rate);
7013
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007014 if (iwl3945_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07007015 dev_kfree_skb_any(skb);
7016
7017 IWL_DEBUG_MAC80211("leave\n");
7018 return 0;
7019}
7020
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007021static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007022 struct ieee80211_if_init_conf *conf)
7023{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007024 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007025 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07007026 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007027
Johannes Berg32bfd352007-12-19 01:31:26 +01007028 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007029
Johannes Berg32bfd352007-12-19 01:31:26 +01007030 if (priv->vif) {
7031 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02007032 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07007033 }
7034
7035 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01007036 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07007037
7038 spin_unlock_irqrestore(&priv->lock, flags);
7039
7040 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02007041
7042 if (conf->mac_addr) {
7043 IWL_DEBUG_MAC80211("Set: %s\n", print_mac(mac, conf->mac_addr));
7044 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7045 }
7046
Zhu Yi5a669262008-01-14 17:46:18 -08007047 if (iwl3945_is_ready(priv))
7048 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007049
Zhu Yib481de92007-09-25 17:54:57 -07007050 mutex_unlock(&priv->mutex);
7051
Zhu Yi5a669262008-01-14 17:46:18 -08007052 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007053 return 0;
7054}
7055
7056/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007057 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07007058 *
7059 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7060 * be set inappropriately and the driver currently sets the hardware up to
7061 * use it whenever needed.
7062 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007063static int iwl3945_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007064{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007065 struct iwl3945_priv *priv = hw->priv;
7066 const struct iwl3945_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07007067 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08007068 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007069
7070 mutex_lock(&priv->mutex);
7071 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
7072
Zhu Yi12342c42007-12-20 11:27:32 +08007073 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
7074
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007075 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007076 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007077 ret = -EIO;
7078 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007079 }
7080
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007081 if (unlikely(!iwl3945_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07007082 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08007083 IWL_DEBUG_MAC80211("leave - scanning\n");
7084 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007085 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08007086 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007087 }
7088
7089 spin_lock_irqsave(&priv->lock, flags);
7090
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007091 ch_info = iwl3945_get_channel_info(priv, conf->phymode, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07007092 if (!is_channel_valid(ch_info)) {
7093 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
7094 conf->channel, conf->phymode);
7095 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7096 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007097 ret = -EINVAL;
7098 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007099 }
7100
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007101 iwl3945_set_rxon_channel(priv, conf->phymode, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07007102
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007103 iwl3945_set_flags_for_phymode(priv, conf->phymode);
Zhu Yib481de92007-09-25 17:54:57 -07007104
7105 /* The list of supported rates and rate mask can be different
7106 * for each phymode; since the phymode may have changed, reset
7107 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007108 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007109
7110 spin_unlock_irqrestore(&priv->lock, flags);
7111
7112#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7113 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007114 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007115 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007116 }
7117#endif
7118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007119 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07007120
7121 if (!conf->radio_enabled) {
7122 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007123 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007124 }
7125
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007126 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007127 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007128 ret = -EIO;
7129 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007130 }
7131
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007132 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007133
7134 if (memcmp(&priv->active_rxon,
7135 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007136 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007137 else
7138 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7139
7140 IWL_DEBUG_MAC80211("leave\n");
7141
Zhu Yi76bb77e2007-11-22 10:53:22 +08007142out:
Zhu Yia0646472007-12-20 14:10:01 +08007143 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007144 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007145 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007146}
7147
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007148static void iwl3945_config_ap(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007149{
7150 int rc = 0;
7151
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08007152 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07007153 return;
7154
7155 /* The following should be done only at AP bring up */
7156 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7157
7158 /* RXON - unassoc (to set timing command) */
7159 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007160 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007161
7162 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007163 memset(&priv->rxon_timing, 0, sizeof(struct iwl3945_rxon_time_cmd));
7164 iwl3945_setup_rxon_timing(priv);
7165 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007166 sizeof(priv->rxon_timing), &priv->rxon_timing);
7167 if (rc)
7168 IWL_WARNING("REPLY_RXON_TIMING failed - "
7169 "Attempting to continue.\n");
7170
7171 /* FIXME: what should be the assoc_id for AP? */
7172 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7173 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7174 priv->staging_rxon.flags |=
7175 RXON_FLG_SHORT_PREAMBLE_MSK;
7176 else
7177 priv->staging_rxon.flags &=
7178 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7179
7180 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7181 if (priv->assoc_capability &
7182 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7183 priv->staging_rxon.flags |=
7184 RXON_FLG_SHORT_SLOT_MSK;
7185 else
7186 priv->staging_rxon.flags &=
7187 ~RXON_FLG_SHORT_SLOT_MSK;
7188
7189 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7190 priv->staging_rxon.flags &=
7191 ~RXON_FLG_SHORT_SLOT_MSK;
7192 }
7193 /* restore RXON assoc */
7194 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007195 iwl3945_commit_rxon(priv);
7196 iwl3945_add_station(priv, iwl3945_broadcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08007197 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007198 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007199
7200 /* FIXME - we need to add code here to detect a totally new
7201 * configuration, reset the AP, unassoc, rxon timing, assoc,
7202 * clear sta table, add BCAST sta... */
7203}
7204
Johannes Berg32bfd352007-12-19 01:31:26 +01007205static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
7206 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07007207 struct ieee80211_if_conf *conf)
7208{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007209 struct iwl3945_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007210 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007211 unsigned long flags;
7212 int rc;
7213
7214 if (conf == NULL)
7215 return -EIO;
7216
Johannes Berg4150c572007-09-17 01:29:23 -04007217 /* XXX: this MUST use conf->mac_addr */
7218
Zhu Yib481de92007-09-25 17:54:57 -07007219 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7220 (!conf->beacon || !conf->ssid_len)) {
7221 IWL_DEBUG_MAC80211
7222 ("Leaving in AP mode because HostAPD is not ready.\n");
7223 return 0;
7224 }
7225
Zhu Yi5a669262008-01-14 17:46:18 -08007226 if (!iwl3945_is_alive(priv))
7227 return -EAGAIN;
7228
Zhu Yib481de92007-09-25 17:54:57 -07007229 mutex_lock(&priv->mutex);
7230
Zhu Yib481de92007-09-25 17:54:57 -07007231 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007232 IWL_DEBUG_MAC80211("bssid: %s\n",
7233 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007234
Johannes Berg4150c572007-09-17 01:29:23 -04007235/*
7236 * very dubious code was here; the probe filtering flag is never set:
7237 *
Zhu Yib481de92007-09-25 17:54:57 -07007238 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7239 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007240 */
7241 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yib481de92007-09-25 17:54:57 -07007242 IWL_DEBUG_MAC80211("leave - scanning\n");
7243 mutex_unlock(&priv->mutex);
7244 return 0;
7245 }
7246
Johannes Berg32bfd352007-12-19 01:31:26 +01007247 if (priv->vif != vif) {
7248 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07007249 mutex_unlock(&priv->mutex);
7250 return 0;
7251 }
7252
7253 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7254 if (!conf->bssid) {
7255 conf->bssid = priv->mac_addr;
7256 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007257 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7258 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007259 }
7260 if (priv->ibss_beacon)
7261 dev_kfree_skb(priv->ibss_beacon);
7262
7263 priv->ibss_beacon = conf->beacon;
7264 }
7265
Mohamed Abbasfde35712007-11-29 11:10:15 +08007266 if (iwl3945_is_rfkill(priv))
7267 goto done;
7268
Zhu Yib481de92007-09-25 17:54:57 -07007269 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7270 !is_multicast_ether_addr(conf->bssid)) {
7271 /* If there is currently a HW scan going on in the background
7272 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007273 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007274 IWL_WARNING("Aborted scan still in progress "
7275 "after 100ms\n");
7276 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7277 mutex_unlock(&priv->mutex);
7278 return -EAGAIN;
7279 }
7280 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7281
7282 /* TODO: Audit driver for usage of these members and see
7283 * if mac80211 deprecates them (priv->bssid looks like it
7284 * shouldn't be there, but I haven't scanned the IBSS code
7285 * to verify) - jpk */
7286 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7287
7288 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007289 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007290 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007291 rc = iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007292 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007293 iwl3945_add_station(priv,
Zhu Yi556f8db2007-09-27 11:27:33 +08007294 priv->active_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007295 }
7296
7297 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007298 iwl3945_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007299 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007300 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007301 }
7302
Mohamed Abbasfde35712007-11-29 11:10:15 +08007303 done:
Zhu Yib481de92007-09-25 17:54:57 -07007304 spin_lock_irqsave(&priv->lock, flags);
7305 if (!conf->ssid_len)
7306 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7307 else
7308 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7309
7310 priv->essid_len = conf->ssid_len;
7311 spin_unlock_irqrestore(&priv->lock, flags);
7312
7313 IWL_DEBUG_MAC80211("leave\n");
7314 mutex_unlock(&priv->mutex);
7315
7316 return 0;
7317}
7318
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007319static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007320 unsigned int changed_flags,
7321 unsigned int *total_flags,
7322 int mc_count, struct dev_addr_list *mc_list)
7323{
7324 /*
7325 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007326 * see also iwl3945_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007327 */
7328 *total_flags = 0;
7329}
7330
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007331static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007332 struct ieee80211_if_init_conf *conf)
7333{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007334 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007335
7336 IWL_DEBUG_MAC80211("enter\n");
7337
7338 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007339
Mohamed Abbasfde35712007-11-29 11:10:15 +08007340 if (iwl3945_is_ready_rf(priv)) {
7341 iwl3945_scan_cancel_timeout(priv, 100);
7342 cancel_delayed_work(&priv->post_associate);
7343 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7344 iwl3945_commit_rxon(priv);
7345 }
Johannes Berg32bfd352007-12-19 01:31:26 +01007346 if (priv->vif == conf->vif) {
7347 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007348 memset(priv->bssid, 0, ETH_ALEN);
7349 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7350 priv->essid_len = 0;
7351 }
7352 mutex_unlock(&priv->mutex);
7353
7354 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007355}
7356
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007357static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007358{
7359 int rc = 0;
7360 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007361 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007362
7363 IWL_DEBUG_MAC80211("enter\n");
7364
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007365 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007366 spin_lock_irqsave(&priv->lock, flags);
7367
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007368 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007369 rc = -EIO;
7370 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7371 goto out_unlock;
7372 }
7373
7374 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7375 rc = -EIO;
7376 IWL_ERROR("ERROR: APs don't scan\n");
7377 goto out_unlock;
7378 }
7379
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007380 /* we don't schedule scan within next_scan_jiffies period */
7381 if (priv->next_scan_jiffies &&
7382 time_after(priv->next_scan_jiffies, jiffies)) {
7383 rc = -EAGAIN;
7384 goto out_unlock;
7385 }
Zhu Yib481de92007-09-25 17:54:57 -07007386 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007387 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7388 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007389 rc = -EAGAIN;
7390 goto out_unlock;
7391 }
7392 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007393 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007394 iwl3945_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007395
7396 priv->one_direct_scan = 1;
7397 priv->direct_ssid_len = (u8)
7398 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7399 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007400 } else
7401 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007402
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007403 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007404
7405 IWL_DEBUG_MAC80211("leave\n");
7406
7407out_unlock:
7408 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007409 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007410
7411 return rc;
7412}
7413
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007414static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007415 const u8 *local_addr, const u8 *addr,
7416 struct ieee80211_key_conf *key)
7417{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007418 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007419 int rc = 0;
7420 u8 sta_id;
7421
7422 IWL_DEBUG_MAC80211("enter\n");
7423
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007424 if (!iwl3945_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007425 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7426 return -EOPNOTSUPP;
7427 }
7428
7429 if (is_zero_ether_addr(addr))
7430 /* only support pairwise keys */
7431 return -EOPNOTSUPP;
7432
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007433 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07007434 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007435 DECLARE_MAC_BUF(mac);
7436
7437 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7438 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007439 return -EINVAL;
7440 }
7441
7442 mutex_lock(&priv->mutex);
7443
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007444 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007445
Zhu Yib481de92007-09-25 17:54:57 -07007446 switch (cmd) {
7447 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007448 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007449 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007450 iwl3945_set_rxon_hwcrypto(priv, 1);
7451 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007452 key->hw_key_idx = sta_id;
7453 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7454 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7455 }
7456 break;
7457 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007458 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007459 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007460 iwl3945_set_rxon_hwcrypto(priv, 0);
7461 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007462 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7463 }
7464 break;
7465 default:
7466 rc = -EINVAL;
7467 }
7468
7469 IWL_DEBUG_MAC80211("leave\n");
7470 mutex_unlock(&priv->mutex);
7471
7472 return rc;
7473}
7474
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007475static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007476 const struct ieee80211_tx_queue_params *params)
7477{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007478 struct iwl3945_priv *priv = hw->priv;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007479#ifdef CONFIG_IWL3945_QOS
Zhu Yib481de92007-09-25 17:54:57 -07007480 unsigned long flags;
7481 int q;
Reinette Chatre0054b342007-11-29 11:09:42 +08007482#endif /* CONFIG_IWL3945_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07007483
7484 IWL_DEBUG_MAC80211("enter\n");
7485
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007486 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007487 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7488 return -EIO;
7489 }
7490
7491 if (queue >= AC_NUM) {
7492 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7493 return 0;
7494 }
7495
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007496#ifdef CONFIG_IWL3945_QOS
Zhu Yib481de92007-09-25 17:54:57 -07007497 if (!priv->qos_data.qos_enable) {
7498 priv->qos_data.qos_active = 0;
7499 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7500 return 0;
7501 }
7502 q = AC_NUM - 1 - queue;
7503
7504 spin_lock_irqsave(&priv->lock, flags);
7505
7506 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7507 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7508 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7509 priv->qos_data.def_qos_parm.ac[q].edca_txop =
7510 cpu_to_le16((params->burst_time * 100));
7511
7512 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7513 priv->qos_data.qos_active = 1;
7514
7515 spin_unlock_irqrestore(&priv->lock, flags);
7516
7517 mutex_lock(&priv->mutex);
7518 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007519 iwl3945_activate_qos(priv, 1);
7520 else if (priv->assoc_id && iwl3945_is_associated(priv))
7521 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007522
7523 mutex_unlock(&priv->mutex);
7524
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007525#endif /*CONFIG_IWL3945_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07007526
7527 IWL_DEBUG_MAC80211("leave\n");
7528 return 0;
7529}
7530
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007531static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007532 struct ieee80211_tx_queue_stats *stats)
7533{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007534 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007535 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007536 struct iwl3945_tx_queue *txq;
7537 struct iwl3945_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007538 unsigned long flags;
7539
7540 IWL_DEBUG_MAC80211("enter\n");
7541
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007542 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007543 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7544 return -EIO;
7545 }
7546
7547 spin_lock_irqsave(&priv->lock, flags);
7548
7549 for (i = 0; i < AC_NUM; i++) {
7550 txq = &priv->txq[i];
7551 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007552 avail = iwl3945_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007553
7554 stats->data[i].len = q->n_window - avail;
7555 stats->data[i].limit = q->n_window - q->high_mark;
7556 stats->data[i].count = q->n_window;
7557
7558 }
7559 spin_unlock_irqrestore(&priv->lock, flags);
7560
7561 IWL_DEBUG_MAC80211("leave\n");
7562
7563 return 0;
7564}
7565
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007566static int iwl3945_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007567 struct ieee80211_low_level_stats *stats)
7568{
7569 IWL_DEBUG_MAC80211("enter\n");
7570 IWL_DEBUG_MAC80211("leave\n");
7571
7572 return 0;
7573}
7574
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007575static u64 iwl3945_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007576{
7577 IWL_DEBUG_MAC80211("enter\n");
7578 IWL_DEBUG_MAC80211("leave\n");
7579
7580 return 0;
7581}
7582
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007583static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007584{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007585 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007586 unsigned long flags;
7587
7588 mutex_lock(&priv->mutex);
7589 IWL_DEBUG_MAC80211("enter\n");
7590
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007591#ifdef CONFIG_IWL3945_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007592 iwl3945_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007593#endif
7594 cancel_delayed_work(&priv->post_associate);
7595
7596 spin_lock_irqsave(&priv->lock, flags);
7597 priv->assoc_id = 0;
7598 priv->assoc_capability = 0;
7599 priv->call_post_assoc_from_beacon = 0;
7600
7601 /* new association get rid of ibss beacon skb */
7602 if (priv->ibss_beacon)
7603 dev_kfree_skb(priv->ibss_beacon);
7604
7605 priv->ibss_beacon = NULL;
7606
7607 priv->beacon_int = priv->hw->conf.beacon_int;
7608 priv->timestamp1 = 0;
7609 priv->timestamp0 = 0;
7610 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7611 priv->beacon_int = 0;
7612
7613 spin_unlock_irqrestore(&priv->lock, flags);
7614
Mohamed Abbasfde35712007-11-29 11:10:15 +08007615 if (!iwl3945_is_ready_rf(priv)) {
7616 IWL_DEBUG_MAC80211("leave - not ready\n");
7617 mutex_unlock(&priv->mutex);
7618 return;
7619 }
7620
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007621 /* we are restarting association process
7622 * clear RXON_FILTER_ASSOC_MSK bit
7623 */
7624 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007625 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007626 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007627 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007628 }
7629
Zhu Yib481de92007-09-25 17:54:57 -07007630 /* Per mac80211.h: This is only used in IBSS mode... */
7631 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007632
Zhu Yib481de92007-09-25 17:54:57 -07007633 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7634 mutex_unlock(&priv->mutex);
7635 return;
7636 }
7637
Zhu Yib481de92007-09-25 17:54:57 -07007638 priv->only_active_channel = 0;
7639
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007640 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007641
7642 mutex_unlock(&priv->mutex);
7643
7644 IWL_DEBUG_MAC80211("leave\n");
7645
7646}
7647
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007648static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007649 struct ieee80211_tx_control *control)
7650{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007651 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007652 unsigned long flags;
7653
7654 mutex_lock(&priv->mutex);
7655 IWL_DEBUG_MAC80211("enter\n");
7656
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007657 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007658 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7659 mutex_unlock(&priv->mutex);
7660 return -EIO;
7661 }
7662
7663 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7664 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7665 mutex_unlock(&priv->mutex);
7666 return -EIO;
7667 }
7668
7669 spin_lock_irqsave(&priv->lock, flags);
7670
7671 if (priv->ibss_beacon)
7672 dev_kfree_skb(priv->ibss_beacon);
7673
7674 priv->ibss_beacon = skb;
7675
7676 priv->assoc_id = 0;
7677
7678 IWL_DEBUG_MAC80211("leave\n");
7679 spin_unlock_irqrestore(&priv->lock, flags);
7680
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007681#ifdef CONFIG_IWL3945_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007682 iwl3945_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007683#endif
7684
7685 queue_work(priv->workqueue, &priv->post_associate.work);
7686
7687 mutex_unlock(&priv->mutex);
7688
7689 return 0;
7690}
7691
7692/*****************************************************************************
7693 *
7694 * sysfs attributes
7695 *
7696 *****************************************************************************/
7697
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007698#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007699
7700/*
7701 * The following adds a new attribute to the sysfs representation
7702 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7703 * used for controlling the debug level.
7704 *
7705 * See the level definitions in iwl for details.
7706 */
7707
7708static ssize_t show_debug_level(struct device_driver *d, char *buf)
7709{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007710 return sprintf(buf, "0x%08X\n", iwl3945_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007711}
7712static ssize_t store_debug_level(struct device_driver *d,
7713 const char *buf, size_t count)
7714{
7715 char *p = (char *)buf;
7716 u32 val;
7717
7718 val = simple_strtoul(p, &p, 0);
7719 if (p == buf)
7720 printk(KERN_INFO DRV_NAME
7721 ": %s is not in hex or decimal form.\n", buf);
7722 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007723 iwl3945_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007724
7725 return strnlen(buf, count);
7726}
7727
7728static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7729 show_debug_level, store_debug_level);
7730
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007731#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007732
7733static ssize_t show_rf_kill(struct device *d,
7734 struct device_attribute *attr, char *buf)
7735{
7736 /*
7737 * 0 - RF kill not enabled
7738 * 1 - SW based RF kill active (sysfs)
7739 * 2 - HW based RF kill active
7740 * 3 - Both HW and SW based RF kill active
7741 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007742 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007743 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
7744 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
7745
7746 return sprintf(buf, "%i\n", val);
7747}
7748
7749static ssize_t store_rf_kill(struct device *d,
7750 struct device_attribute *attr,
7751 const char *buf, size_t count)
7752{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007753 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007754
7755 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007756 iwl3945_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07007757 mutex_unlock(&priv->mutex);
7758
7759 return count;
7760}
7761
7762static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7763
7764static ssize_t show_temperature(struct device *d,
7765 struct device_attribute *attr, char *buf)
7766{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007767 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007768
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007769 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007770 return -EAGAIN;
7771
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007772 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007773}
7774
7775static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7776
7777static ssize_t show_rs_window(struct device *d,
7778 struct device_attribute *attr,
7779 char *buf)
7780{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007781 struct iwl3945_priv *priv = d->driver_data;
7782 return iwl3945_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007783}
7784static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7785
7786static ssize_t show_tx_power(struct device *d,
7787 struct device_attribute *attr, char *buf)
7788{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007789 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007790 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7791}
7792
7793static ssize_t store_tx_power(struct device *d,
7794 struct device_attribute *attr,
7795 const char *buf, size_t count)
7796{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007797 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007798 char *p = (char *)buf;
7799 u32 val;
7800
7801 val = simple_strtoul(p, &p, 10);
7802 if (p == buf)
7803 printk(KERN_INFO DRV_NAME
7804 ": %s is not in decimal form.\n", buf);
7805 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007806 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007807
7808 return count;
7809}
7810
7811static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7812
7813static ssize_t show_flags(struct device *d,
7814 struct device_attribute *attr, char *buf)
7815{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007816 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007817
7818 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7819}
7820
7821static ssize_t store_flags(struct device *d,
7822 struct device_attribute *attr,
7823 const char *buf, size_t count)
7824{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007825 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007826 u32 flags = simple_strtoul(buf, NULL, 0);
7827
7828 mutex_lock(&priv->mutex);
7829 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7830 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007831 if (iwl3945_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007832 IWL_WARNING("Could not cancel scan.\n");
7833 else {
7834 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7835 flags);
7836 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007837 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007838 }
7839 }
7840 mutex_unlock(&priv->mutex);
7841
7842 return count;
7843}
7844
7845static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7846
7847static ssize_t show_filter_flags(struct device *d,
7848 struct device_attribute *attr, char *buf)
7849{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007850 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007851
7852 return sprintf(buf, "0x%04X\n",
7853 le32_to_cpu(priv->active_rxon.filter_flags));
7854}
7855
7856static ssize_t store_filter_flags(struct device *d,
7857 struct device_attribute *attr,
7858 const char *buf, size_t count)
7859{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007860 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007861 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7862
7863 mutex_lock(&priv->mutex);
7864 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7865 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007866 if (iwl3945_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007867 IWL_WARNING("Could not cancel scan.\n");
7868 else {
7869 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7870 "0x%04X\n", filter_flags);
7871 priv->staging_rxon.filter_flags =
7872 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007873 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007874 }
7875 }
7876 mutex_unlock(&priv->mutex);
7877
7878 return count;
7879}
7880
7881static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7882 store_filter_flags);
7883
7884static ssize_t show_tune(struct device *d,
7885 struct device_attribute *attr, char *buf)
7886{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007887 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007888
7889 return sprintf(buf, "0x%04X\n",
7890 (priv->phymode << 8) |
7891 le16_to_cpu(priv->active_rxon.channel));
7892}
7893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007894static void iwl3945_set_flags_for_phymode(struct iwl3945_priv *priv, u8 phymode);
Zhu Yib481de92007-09-25 17:54:57 -07007895
7896static ssize_t store_tune(struct device *d,
7897 struct device_attribute *attr,
7898 const char *buf, size_t count)
7899{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007900 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007901 char *p = (char *)buf;
7902 u16 tune = simple_strtoul(p, &p, 0);
7903 u8 phymode = (tune >> 8) & 0xff;
7904 u16 channel = tune & 0xff;
7905
7906 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
7907
7908 mutex_lock(&priv->mutex);
7909 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
7910 (priv->phymode != phymode)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007911 const struct iwl3945_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07007912
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007913 ch_info = iwl3945_get_channel_info(priv, phymode, channel);
Zhu Yib481de92007-09-25 17:54:57 -07007914 if (!ch_info) {
7915 IWL_WARNING("Requested invalid phymode/channel "
7916 "combination: %d %d\n", phymode, channel);
7917 mutex_unlock(&priv->mutex);
7918 return -EINVAL;
7919 }
7920
7921 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007922 if (iwl3945_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007923 IWL_WARNING("Could not cancel scan.\n");
7924 else {
7925 IWL_DEBUG_INFO("Committing phymode and "
7926 "rxon.channel = %d %d\n",
7927 phymode, channel);
7928
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007929 iwl3945_set_rxon_channel(priv, phymode, channel);
7930 iwl3945_set_flags_for_phymode(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07007931
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007932 iwl3945_set_rate(priv);
7933 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007934 }
7935 }
7936 mutex_unlock(&priv->mutex);
7937
7938 return count;
7939}
7940
7941static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
7942
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007943#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007944
7945static ssize_t show_measurement(struct device *d,
7946 struct device_attribute *attr, char *buf)
7947{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007948 struct iwl3945_priv *priv = dev_get_drvdata(d);
7949 struct iwl3945_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007950 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7951 u8 *data = (u8 *) & measure_report;
7952 unsigned long flags;
7953
7954 spin_lock_irqsave(&priv->lock, flags);
7955 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7956 spin_unlock_irqrestore(&priv->lock, flags);
7957 return 0;
7958 }
7959 memcpy(&measure_report, &priv->measure_report, size);
7960 priv->measurement_status = 0;
7961 spin_unlock_irqrestore(&priv->lock, flags);
7962
7963 while (size && (PAGE_SIZE - len)) {
7964 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7965 PAGE_SIZE - len, 1);
7966 len = strlen(buf);
7967 if (PAGE_SIZE - len)
7968 buf[len++] = '\n';
7969
7970 ofs += 16;
7971 size -= min(size, 16U);
7972 }
7973
7974 return len;
7975}
7976
7977static ssize_t store_measurement(struct device *d,
7978 struct device_attribute *attr,
7979 const char *buf, size_t count)
7980{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007981 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007982 struct ieee80211_measurement_params params = {
7983 .channel = le16_to_cpu(priv->active_rxon.channel),
7984 .start_time = cpu_to_le64(priv->last_tsf),
7985 .duration = cpu_to_le16(1),
7986 };
7987 u8 type = IWL_MEASURE_BASIC;
7988 u8 buffer[32];
7989 u8 channel;
7990
7991 if (count) {
7992 char *p = buffer;
7993 strncpy(buffer, buf, min(sizeof(buffer), count));
7994 channel = simple_strtoul(p, NULL, 0);
7995 if (channel)
7996 params.channel = channel;
7997
7998 p = buffer;
7999 while (*p && *p != ' ')
8000 p++;
8001 if (*p)
8002 type = simple_strtoul(p + 1, NULL, 0);
8003 }
8004
8005 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8006 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008007 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07008008
8009 return count;
8010}
8011
8012static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8013 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008014#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07008015
8016static ssize_t show_rate(struct device *d,
8017 struct device_attribute *attr, char *buf)
8018{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008019 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008020 unsigned long flags;
8021 int i;
8022
8023 spin_lock_irqsave(&priv->sta_lock, flags);
8024 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
8025 i = priv->stations[IWL_AP_ID].current_rate.s.rate;
8026 else
8027 i = priv->stations[IWL_STA_ID].current_rate.s.rate;
8028 spin_unlock_irqrestore(&priv->sta_lock, flags);
8029
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008030 i = iwl3945_rate_index_from_plcp(i);
Zhu Yib481de92007-09-25 17:54:57 -07008031 if (i == -1)
8032 return sprintf(buf, "0\n");
8033
8034 return sprintf(buf, "%d%s\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008035 (iwl3945_rates[i].ieee >> 1),
8036 (iwl3945_rates[i].ieee & 0x1) ? ".5" : "");
Zhu Yib481de92007-09-25 17:54:57 -07008037}
8038
8039static DEVICE_ATTR(rate, S_IRUSR, show_rate, NULL);
8040
8041static ssize_t store_retry_rate(struct device *d,
8042 struct device_attribute *attr,
8043 const char *buf, size_t count)
8044{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008045 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008046
8047 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8048 if (priv->retry_rate <= 0)
8049 priv->retry_rate = 1;
8050
8051 return count;
8052}
8053
8054static ssize_t show_retry_rate(struct device *d,
8055 struct device_attribute *attr, char *buf)
8056{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008057 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008058 return sprintf(buf, "%d", priv->retry_rate);
8059}
8060
8061static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8062 store_retry_rate);
8063
8064static ssize_t store_power_level(struct device *d,
8065 struct device_attribute *attr,
8066 const char *buf, size_t count)
8067{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008068 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008069 int rc;
8070 int mode;
8071
8072 mode = simple_strtoul(buf, NULL, 0);
8073 mutex_lock(&priv->mutex);
8074
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008075 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008076 rc = -EAGAIN;
8077 goto out;
8078 }
8079
8080 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8081 mode = IWL_POWER_AC;
8082 else
8083 mode |= IWL_POWER_ENABLED;
8084
8085 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008086 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07008087 if (rc) {
8088 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8089 goto out;
8090 }
8091 priv->power_mode = mode;
8092 }
8093
8094 rc = count;
8095
8096 out:
8097 mutex_unlock(&priv->mutex);
8098 return rc;
8099}
8100
8101#define MAX_WX_STRING 80
8102
8103/* Values are in microsecond */
8104static const s32 timeout_duration[] = {
8105 350000,
8106 250000,
8107 75000,
8108 37000,
8109 25000,
8110};
8111static const s32 period_duration[] = {
8112 400000,
8113 700000,
8114 1000000,
8115 1000000,
8116 1000000
8117};
8118
8119static ssize_t show_power_level(struct device *d,
8120 struct device_attribute *attr, char *buf)
8121{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008122 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008123 int level = IWL_POWER_LEVEL(priv->power_mode);
8124 char *p = buf;
8125
8126 p += sprintf(p, "%d ", level);
8127 switch (level) {
8128 case IWL_POWER_MODE_CAM:
8129 case IWL_POWER_AC:
8130 p += sprintf(p, "(AC)");
8131 break;
8132 case IWL_POWER_BATTERY:
8133 p += sprintf(p, "(BATTERY)");
8134 break;
8135 default:
8136 p += sprintf(p,
8137 "(Timeout %dms, Period %dms)",
8138 timeout_duration[level - 1] / 1000,
8139 period_duration[level - 1] / 1000);
8140 }
8141
8142 if (!(priv->power_mode & IWL_POWER_ENABLED))
8143 p += sprintf(p, " OFF\n");
8144 else
8145 p += sprintf(p, " \n");
8146
8147 return (p - buf + 1);
8148
8149}
8150
8151static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8152 store_power_level);
8153
8154static ssize_t show_channels(struct device *d,
8155 struct device_attribute *attr, char *buf)
8156{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008157 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008158 int len = 0, i;
8159 struct ieee80211_channel *channels = NULL;
8160 const struct ieee80211_hw_mode *hw_mode = NULL;
8161 int count = 0;
8162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008163 if (!iwl3945_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008164 return -EAGAIN;
8165
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008166 hw_mode = iwl3945_get_hw_mode(priv, MODE_IEEE80211G);
Zhu Yib481de92007-09-25 17:54:57 -07008167 if (!hw_mode)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008168 hw_mode = iwl3945_get_hw_mode(priv, MODE_IEEE80211B);
Zhu Yib481de92007-09-25 17:54:57 -07008169 if (hw_mode) {
8170 channels = hw_mode->channels;
8171 count = hw_mode->num_channels;
8172 }
8173
8174 len +=
8175 sprintf(&buf[len],
8176 "Displaying %d channels in 2.4GHz band "
8177 "(802.11bg):\n", count);
8178
8179 for (i = 0; i < count; i++)
8180 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8181 channels[i].chan,
8182 channels[i].power_level,
8183 channels[i].
8184 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8185 " (IEEE 802.11h required)" : "",
8186 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8187 || (channels[i].
8188 flag &
8189 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8190 ", IBSS",
8191 channels[i].
8192 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8193 "active/passive" : "passive only");
8194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008195 hw_mode = iwl3945_get_hw_mode(priv, MODE_IEEE80211A);
Zhu Yib481de92007-09-25 17:54:57 -07008196 if (hw_mode) {
8197 channels = hw_mode->channels;
8198 count = hw_mode->num_channels;
8199 } else {
8200 channels = NULL;
8201 count = 0;
8202 }
8203
8204 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8205 "(802.11a):\n", count);
8206
8207 for (i = 0; i < count; i++)
8208 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8209 channels[i].chan,
8210 channels[i].power_level,
8211 channels[i].
8212 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8213 " (IEEE 802.11h required)" : "",
8214 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8215 || (channels[i].
8216 flag &
8217 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8218 ", IBSS",
8219 channels[i].
8220 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8221 "active/passive" : "passive only");
8222
8223 return len;
8224}
8225
8226static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8227
8228static ssize_t show_statistics(struct device *d,
8229 struct device_attribute *attr, char *buf)
8230{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008231 struct iwl3945_priv *priv = dev_get_drvdata(d);
8232 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07008233 u32 len = 0, ofs = 0;
8234 u8 *data = (u8 *) & priv->statistics;
8235 int rc = 0;
8236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008237 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008238 return -EAGAIN;
8239
8240 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008241 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008242 mutex_unlock(&priv->mutex);
8243
8244 if (rc) {
8245 len = sprintf(buf,
8246 "Error sending statistics request: 0x%08X\n", rc);
8247 return len;
8248 }
8249
8250 while (size && (PAGE_SIZE - len)) {
8251 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8252 PAGE_SIZE - len, 1);
8253 len = strlen(buf);
8254 if (PAGE_SIZE - len)
8255 buf[len++] = '\n';
8256
8257 ofs += 16;
8258 size -= min(size, 16U);
8259 }
8260
8261 return len;
8262}
8263
8264static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8265
8266static ssize_t show_antenna(struct device *d,
8267 struct device_attribute *attr, char *buf)
8268{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008269 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008270
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008271 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008272 return -EAGAIN;
8273
8274 return sprintf(buf, "%d\n", priv->antenna);
8275}
8276
8277static ssize_t store_antenna(struct device *d,
8278 struct device_attribute *attr,
8279 const char *buf, size_t count)
8280{
8281 int ant;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008282 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008283
8284 if (count == 0)
8285 return 0;
8286
8287 if (sscanf(buf, "%1i", &ant) != 1) {
8288 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8289 return count;
8290 }
8291
8292 if ((ant >= 0) && (ant <= 2)) {
8293 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008294 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07008295 } else
8296 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8297
8298
8299 return count;
8300}
8301
8302static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8303
8304static ssize_t show_status(struct device *d,
8305 struct device_attribute *attr, char *buf)
8306{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008307 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
8308 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008309 return -EAGAIN;
8310 return sprintf(buf, "0x%08x\n", (int)priv->status);
8311}
8312
8313static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8314
8315static ssize_t dump_error_log(struct device *d,
8316 struct device_attribute *attr,
8317 const char *buf, size_t count)
8318{
8319 char *p = (char *)buf;
8320
8321 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008322 iwl3945_dump_nic_error_log((struct iwl3945_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008323
8324 return strnlen(buf, count);
8325}
8326
8327static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8328
8329static ssize_t dump_event_log(struct device *d,
8330 struct device_attribute *attr,
8331 const char *buf, size_t count)
8332{
8333 char *p = (char *)buf;
8334
8335 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008336 iwl3945_dump_nic_event_log((struct iwl3945_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008337
8338 return strnlen(buf, count);
8339}
8340
8341static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8342
8343/*****************************************************************************
8344 *
8345 * driver setup and teardown
8346 *
8347 *****************************************************************************/
8348
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008349static void iwl3945_setup_deferred_work(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008350{
8351 priv->workqueue = create_workqueue(DRV_NAME);
8352
8353 init_waitqueue_head(&priv->wait_command_queue);
8354
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008355 INIT_WORK(&priv->up, iwl3945_bg_up);
8356 INIT_WORK(&priv->restart, iwl3945_bg_restart);
8357 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
8358 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
8359 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
8360 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
8361 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
8362 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
8363 INIT_DELAYED_WORK(&priv->post_associate, iwl3945_bg_post_associate);
8364 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
8365 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
8366 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008367
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008368 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008369
8370 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008371 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008372}
8373
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008374static void iwl3945_cancel_deferred_work(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008375{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008376 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008377
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09008378 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008379 cancel_delayed_work(&priv->scan_check);
8380 cancel_delayed_work(&priv->alive_start);
8381 cancel_delayed_work(&priv->post_associate);
8382 cancel_work_sync(&priv->beacon_update);
8383}
8384
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008385static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008386 &dev_attr_antenna.attr,
8387 &dev_attr_channels.attr,
8388 &dev_attr_dump_errors.attr,
8389 &dev_attr_dump_events.attr,
8390 &dev_attr_flags.attr,
8391 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008392#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008393 &dev_attr_measurement.attr,
8394#endif
8395 &dev_attr_power_level.attr,
8396 &dev_attr_rate.attr,
8397 &dev_attr_retry_rate.attr,
8398 &dev_attr_rf_kill.attr,
8399 &dev_attr_rs_window.attr,
8400 &dev_attr_statistics.attr,
8401 &dev_attr_status.attr,
8402 &dev_attr_temperature.attr,
8403 &dev_attr_tune.attr,
8404 &dev_attr_tx_power.attr,
8405
8406 NULL
8407};
8408
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008409static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008410 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008411 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008412};
8413
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008414static struct ieee80211_ops iwl3945_hw_ops = {
8415 .tx = iwl3945_mac_tx,
8416 .start = iwl3945_mac_start,
8417 .stop = iwl3945_mac_stop,
8418 .add_interface = iwl3945_mac_add_interface,
8419 .remove_interface = iwl3945_mac_remove_interface,
8420 .config = iwl3945_mac_config,
8421 .config_interface = iwl3945_mac_config_interface,
8422 .configure_filter = iwl3945_configure_filter,
8423 .set_key = iwl3945_mac_set_key,
8424 .get_stats = iwl3945_mac_get_stats,
8425 .get_tx_stats = iwl3945_mac_get_tx_stats,
8426 .conf_tx = iwl3945_mac_conf_tx,
8427 .get_tsf = iwl3945_mac_get_tsf,
8428 .reset_tsf = iwl3945_mac_reset_tsf,
8429 .beacon_update = iwl3945_mac_beacon_update,
8430 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008431};
8432
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008433static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008434{
8435 int err = 0;
8436 u32 pci_id;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008437 struct iwl3945_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008438 struct ieee80211_hw *hw;
8439 int i;
Zhu Yi5a669262008-01-14 17:46:18 -08008440 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008441
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008442 /* Disabling hardware scan means that mac80211 will perform scans
8443 * "the hard way", rather than using device's scan. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008444 if (iwl3945_param_disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008445 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008446 iwl3945_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008447 }
8448
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008449 if ((iwl3945_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8450 (iwl3945_param_queues_num < IWL_MIN_NUM_QUEUES)) {
Zhu Yib481de92007-09-25 17:54:57 -07008451 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8452 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8453 err = -EINVAL;
8454 goto out;
8455 }
8456
8457 /* mac80211 allocates memory for this device instance, including
8458 * space for this driver's private structure */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008459 hw = ieee80211_alloc_hw(sizeof(struct iwl3945_priv), &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07008460 if (hw == NULL) {
8461 IWL_ERROR("Can not allocate network device\n");
8462 err = -ENOMEM;
8463 goto out;
8464 }
8465 SET_IEEE80211_DEV(hw, &pdev->dev);
8466
Johannes Bergf51359a2007-10-28 14:53:36 +01008467 hw->rate_control_algorithm = "iwl-3945-rs";
8468
Zhu Yib481de92007-09-25 17:54:57 -07008469 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8470 priv = hw->priv;
8471 priv->hw = hw;
8472
8473 priv->pci_dev = pdev;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008474
8475 /* Select antenna (may be helpful if only one antenna is connected) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008476 priv->antenna = (enum iwl3945_antenna)iwl3945_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008477#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008478 iwl3945_debug_level = iwl3945_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008479 atomic_set(&priv->restrict_refcnt, 0);
8480#endif
8481 priv->retry_rate = 1;
8482
8483 priv->ibss_beacon = NULL;
8484
8485 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8486 * the range of signal quality values that we'll provide.
8487 * Negative values for level/noise indicate that we'll provide dBm.
8488 * For WE, at least, non-0 values here *enable* display of values
8489 * in app (iwconfig). */
8490 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8491 hw->max_noise = -20; /* noise level, negative indicates dBm */
8492 hw->max_signal = 100; /* link quality indication (%) */
8493
8494 /* Tell mac80211 our Tx characteristics */
8495 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8496
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008497 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07008498 hw->queues = 4;
8499
8500 spin_lock_init(&priv->lock);
8501 spin_lock_init(&priv->power_data.lock);
8502 spin_lock_init(&priv->sta_lock);
8503 spin_lock_init(&priv->hcmd_lock);
8504
8505 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8506 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8507
8508 INIT_LIST_HEAD(&priv->free_frames);
8509
8510 mutex_init(&priv->mutex);
8511 if (pci_enable_device(pdev)) {
8512 err = -ENODEV;
8513 goto out_ieee80211_free_hw;
8514 }
8515
8516 pci_set_master(pdev);
8517
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008518 /* Clear the driver's (not device's) station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008519 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008520
8521 priv->data_retry_limit = -1;
8522 priv->ieee_channels = NULL;
8523 priv->ieee_rates = NULL;
8524 priv->phymode = -1;
8525
8526 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8527 if (!err)
8528 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8529 if (err) {
8530 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
8531 goto out_pci_disable_device;
8532 }
8533
8534 pci_set_drvdata(pdev, priv);
8535 err = pci_request_regions(pdev, DRV_NAME);
8536 if (err)
8537 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008538
Zhu Yib481de92007-09-25 17:54:57 -07008539 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8540 * PCI Tx retries from interfering with C3 CPU state */
8541 pci_write_config_byte(pdev, 0x41, 0x00);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008542
Zhu Yib481de92007-09-25 17:54:57 -07008543 priv->hw_base = pci_iomap(pdev, 0, 0);
8544 if (!priv->hw_base) {
8545 err = -ENODEV;
8546 goto out_pci_release_regions;
8547 }
8548
8549 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8550 (unsigned long long) pci_resource_len(pdev, 0));
8551 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8552
8553 /* Initialize module parameter values here */
8554
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008555 /* Disable radio (SW RF KILL) via parameter when loading driver */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008556 if (iwl3945_param_disable) {
Zhu Yib481de92007-09-25 17:54:57 -07008557 set_bit(STATUS_RF_KILL_SW, &priv->status);
8558 IWL_DEBUG_INFO("Radio disabled.\n");
8559 }
8560
8561 priv->iw_mode = IEEE80211_IF_TYPE_STA;
8562
8563 pci_id =
8564 (priv->pci_dev->device << 16) | priv->pci_dev->subsystem_device;
8565
8566 switch (pci_id) {
8567 case 0x42221005: /* 0x4222 0x8086 0x1005 is BG SKU */
8568 case 0x42221034: /* 0x4222 0x8086 0x1034 is BG SKU */
8569 case 0x42271014: /* 0x4227 0x8086 0x1014 is BG SKU */
8570 case 0x42221044: /* 0x4222 0x8086 0x1044 is BG SKU */
8571 priv->is_abg = 0;
8572 break;
8573
8574 /*
8575 * Rest are assumed ABG SKU -- if this is not the
8576 * case then the card will get the wrong 'Detected'
8577 * line in the kernel log however the code that
8578 * initializes the GEO table will detect no A-band
8579 * channels and remove the is_abg mask.
8580 */
8581 default:
8582 priv->is_abg = 1;
8583 break;
8584 }
8585
8586 printk(KERN_INFO DRV_NAME
8587 ": Detected Intel PRO/Wireless 3945%sBG Network Connection\n",
8588 priv->is_abg ? "A" : "");
8589
8590 /* Device-specific setup */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008591 if (iwl3945_hw_set_hw_setting(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008592 IWL_ERROR("failed to set hw settings\n");
Zhu Yib481de92007-09-25 17:54:57 -07008593 goto out_iounmap;
8594 }
8595
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008596#ifdef CONFIG_IWL3945_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008597 if (iwl3945_param_qos_enable)
Zhu Yib481de92007-09-25 17:54:57 -07008598 priv->qos_data.qos_enable = 1;
8599
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008600 iwl3945_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008601
8602 priv->qos_data.qos_active = 0;
8603 priv->qos_data.qos_cap.val = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008604#endif /* CONFIG_IWL3945_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07008605
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008606 iwl3945_set_rxon_channel(priv, MODE_IEEE80211G, 6);
8607 iwl3945_setup_deferred_work(priv);
8608 iwl3945_setup_rx_handlers(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008609
8610 priv->rates_mask = IWL_RATES_MASK;
8611 /* If power management is turned on, default to AC mode */
8612 priv->power_mode = IWL_POWER_AC;
8613 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
8614
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008615 iwl3945_disable_interrupts(priv);
Jes Sorensen49df2b32007-10-26 16:10:39 +02008616
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008617 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008618 if (err) {
8619 IWL_ERROR("failed to create sysfs device attributes\n");
Zhu Yib481de92007-09-25 17:54:57 -07008620 goto out_release_irq;
8621 }
8622
Zhu Yi5a669262008-01-14 17:46:18 -08008623 /* nic init */
8624 iwl3945_set_bit(priv, CSR_GIO_CHICKEN_BITS,
8625 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8626
8627 iwl3945_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8628 err = iwl3945_poll_bit(priv, CSR_GP_CNTRL,
8629 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8630 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8631 if (err < 0) {
8632 IWL_DEBUG_INFO("Failed to init the card\n");
8633 goto out_remove_sysfs;
8634 }
8635 /* Read the EEPROM */
8636 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008637 if (err) {
Zhu Yi5a669262008-01-14 17:46:18 -08008638 IWL_ERROR("Unable to init EEPROM\n");
8639 goto out_remove_sysfs;
8640 }
8641 /* MAC Address location in EEPROM same for 3945/4965 */
8642 get_eeprom_mac(priv, priv->mac_addr);
8643 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8644 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8645
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008646 err = iwl3945_init_channel_map(priv);
8647 if (err) {
8648 IWL_ERROR("initializing regulatory failed: %d\n", err);
8649 goto out_remove_sysfs;
8650 }
8651
8652 err = iwl3945_init_geos(priv);
8653 if (err) {
8654 IWL_ERROR("initializing geos failed: %d\n", err);
8655 goto out_free_channel_map;
8656 }
8657 iwl3945_reset_channel_flag(priv);
8658
Zhu Yi5a669262008-01-14 17:46:18 -08008659 iwl3945_rate_control_register(priv->hw);
8660 err = ieee80211_register_hw(priv->hw);
8661 if (err) {
8662 IWL_ERROR("Failed to register network device (error %d)\n", err);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008663 goto out_free_geos;
Zhu Yib481de92007-09-25 17:54:57 -07008664 }
8665
Zhu Yi5a669262008-01-14 17:46:18 -08008666 priv->hw->conf.beacon_int = 100;
8667 priv->mac80211_registered = 1;
8668 pci_save_state(pdev);
8669 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008670
8671 return 0;
8672
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008673 out_free_geos:
8674 iwl3945_free_geos(priv);
8675 out_free_channel_map:
8676 iwl3945_free_channel_map(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08008677 out_remove_sysfs:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008678 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008679
8680 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07008681 destroy_workqueue(priv->workqueue);
8682 priv->workqueue = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008683 iwl3945_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008684
8685 out_iounmap:
8686 pci_iounmap(pdev, priv->hw_base);
8687 out_pci_release_regions:
8688 pci_release_regions(pdev);
8689 out_pci_disable_device:
8690 pci_disable_device(pdev);
8691 pci_set_drvdata(pdev, NULL);
8692 out_ieee80211_free_hw:
8693 ieee80211_free_hw(priv->hw);
8694 out:
8695 return err;
8696}
8697
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008698static void iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008699{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008700 struct iwl3945_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008701 struct list_head *p, *q;
8702 int i;
8703
8704 if (!priv)
8705 return;
8706
8707 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8708
Zhu Yib481de92007-09-25 17:54:57 -07008709 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008710
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008711 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008712
8713 /* Free MAC hash list for ADHOC */
8714 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8715 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8716 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008717 kfree(list_entry(p, struct iwl3945_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008718 }
8719 }
8720
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008721 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008722
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008723 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008724
8725 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008726 iwl3945_rx_queue_free(priv, &priv->rxq);
8727 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008728
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008729 iwl3945_unset_hw_setting(priv);
8730 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008731
8732 if (priv->mac80211_registered) {
8733 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008734 iwl3945_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008735 }
8736
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08008737 /*netif_stop_queue(dev); */
8738 flush_workqueue(priv->workqueue);
8739
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008740 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008741 * priv->workqueue... so we can't take down the workqueue
8742 * until now... */
8743 destroy_workqueue(priv->workqueue);
8744 priv->workqueue = NULL;
8745
Zhu Yib481de92007-09-25 17:54:57 -07008746 pci_iounmap(pdev, priv->hw_base);
8747 pci_release_regions(pdev);
8748 pci_disable_device(pdev);
8749 pci_set_drvdata(pdev, NULL);
8750
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008751 iwl3945_free_channel_map(priv);
8752 iwl3945_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008753
8754 if (priv->ibss_beacon)
8755 dev_kfree_skb(priv->ibss_beacon);
8756
8757 ieee80211_free_hw(priv->hw);
8758}
8759
8760#ifdef CONFIG_PM
8761
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008762static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008763{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008764 struct iwl3945_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008765
Zhu Yie655b9f2008-01-24 02:19:38 -08008766 if (priv->is_open) {
8767 set_bit(STATUS_IN_SUSPEND, &priv->status);
8768 iwl3945_mac_stop(priv->hw);
8769 priv->is_open = 1;
8770 }
Zhu Yib481de92007-09-25 17:54:57 -07008771
Zhu Yib481de92007-09-25 17:54:57 -07008772 pci_set_power_state(pdev, PCI_D3hot);
8773
Zhu Yib481de92007-09-25 17:54:57 -07008774 return 0;
8775}
8776
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008777static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008778{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008779 struct iwl3945_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008780
Zhu Yib481de92007-09-25 17:54:57 -07008781 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008782
Zhu Yie655b9f2008-01-24 02:19:38 -08008783 if (priv->is_open)
8784 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008785
Zhu Yie655b9f2008-01-24 02:19:38 -08008786 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008787 return 0;
8788}
8789
8790#endif /* CONFIG_PM */
8791
8792/*****************************************************************************
8793 *
8794 * driver and module entry point
8795 *
8796 *****************************************************************************/
8797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008798static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008799 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008800 .id_table = iwl3945_hw_card_ids,
8801 .probe = iwl3945_pci_probe,
8802 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008803#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008804 .suspend = iwl3945_pci_suspend,
8805 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008806#endif
8807};
8808
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008809static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008810{
8811
8812 int ret;
8813 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8814 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008815 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008816 if (ret) {
8817 IWL_ERROR("Unable to initialize PCI module\n");
8818 return ret;
8819 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008820#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008821 ret = driver_create_file(&iwl3945_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008822 if (ret) {
8823 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008824 pci_unregister_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008825 return ret;
8826 }
8827#endif
8828
8829 return ret;
8830}
8831
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008832static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008833{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008834#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008835 driver_remove_file(&iwl3945_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008836#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008837 pci_unregister_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008838}
8839
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008840module_param_named(antenna, iwl3945_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008841MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008842module_param_named(disable, iwl3945_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008843MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008844module_param_named(hwcrypto, iwl3945_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008845MODULE_PARM_DESC(hwcrypto,
8846 "using hardware crypto engine (default 0 [software])\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008847module_param_named(debug, iwl3945_param_debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008848MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008849module_param_named(disable_hw_scan, iwl3945_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008850MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8851
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008852module_param_named(queues_num, iwl3945_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008853MODULE_PARM_DESC(queues_num, "number of hw queues.");
8854
8855/* QoS */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008856module_param_named(qos_enable, iwl3945_param_qos_enable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008857MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
8858
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008859module_exit(iwl3945_exit);
8860module_init(iwl3945_init);