blob: 07089754b544b260672508629ecf619b38907249 [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
Zhu Yib481de92007-09-25 17:54:57 -070044#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
Zhu Yib481de92007-09-25 17:54:57 -070048#include "iwl-4965.h"
49#include "iwl-helpers.h"
50
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080051#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080052u32 iwl4965_debug_level;
Zhu Yib481de92007-09-25 17:54:57 -070053#endif
54
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080055static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
56 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080057
Zhu Yib481de92007-09-25 17:54:57 -070058/******************************************************************************
59 *
60 * module boiler plate
61 *
62 ******************************************************************************/
63
64/* module parameters */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080065static int iwl4965_param_disable_hw_scan; /* def: 0 = use 4965's h/w scan */
66static int iwl4965_param_debug; /* def: 0 = minimal debug log messages */
Ben Cahill9fbab512007-11-29 11:09:47 +080067static int iwl4965_param_disable; /* def: enable radio */
68static int iwl4965_param_antenna; /* def: 0 = both antennas (use diversity) */
69int iwl4965_param_hwcrypto; /* def: using software encryption */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080070static int iwl4965_param_qos_enable = 1; /* def: 1 = use quality of service */
71int iwl4965_param_queues_num = IWL_MAX_NUM_QUEUES; /* def: 16 Tx queues */
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +020072int iwl4965_param_amsdu_size_8K; /* def: enable 8K amsdu size */
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 "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
80
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080081#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070082#define VD "d"
83#else
84#define VD
85#endif
86
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080087#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070088#define VS "s"
89#else
90#define VS
91#endif
92
Zhu Yid1283942007-11-29 11:10:16 +080093#define IWLWIFI_VERSION "1.2.22k" VD VS
Zhu Yib481de92007-09-25 17:54:57 -070094#define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
95#define DRV_VERSION IWLWIFI_VERSION
96
97/* Change firmware file name, using "-" and incrementing number,
98 * *only* when uCode interface or architecture changes so that it
99 * is not compatible with earlier drivers.
100 * This number will also appear in << 8 position of 1st dword of uCode file */
101#define IWL4965_UCODE_API "-1"
102
103MODULE_DESCRIPTION(DRV_DESCRIPTION);
104MODULE_VERSION(DRV_VERSION);
105MODULE_AUTHOR(DRV_COPYRIGHT);
106MODULE_LICENSE("GPL");
107
108__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
109{
110 u16 fc = le16_to_cpu(hdr->frame_control);
111 int hdr_len = ieee80211_get_hdrlen(fc);
112
113 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
114 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
115 return NULL;
116}
117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800118static const struct ieee80211_hw_mode *iwl4965_get_hw_mode(
119 struct iwl4965_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -0700120{
121 int i;
122
123 for (i = 0; i < 3; i++)
124 if (priv->modes[i].mode == mode)
125 return &priv->modes[i];
126
127 return NULL;
128}
129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800130static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700131{
132 /* Single white space is for Linksys APs */
133 if (essid_len == 1 && essid[0] == ' ')
134 return 1;
135
136 /* Otherwise, if the entire essid is 0, we assume it is hidden */
137 while (essid_len) {
138 essid_len--;
139 if (essid[essid_len] != '\0')
140 return 0;
141 }
142
143 return 1;
144}
145
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800146static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700147{
148 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
149 const char *s = essid;
150 char *d = escaped;
151
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800152 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700153 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
154 return escaped;
155 }
156
157 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
158 while (essid_len--) {
159 if (*s == '\0') {
160 *d++ = '\\';
161 *d++ = '0';
162 s++;
163 } else
164 *d++ = *s++;
165 }
166 *d = '\0';
167 return escaped;
168}
169
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800170static void iwl4965_print_hex_dump(int level, void *p, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -0700171{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800172#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800173 if (!(iwl4965_debug_level & level))
Zhu Yib481de92007-09-25 17:54:57 -0700174 return;
175
176 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
177 p, len, 1);
178#endif
179}
180
181/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
182 * DMA services
183 *
184 * Theory of operation
185 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800186 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
187 * of buffer descriptors, each of which points to one or more data buffers for
188 * the device to read from or fill. Driver and device exchange status of each
189 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
190 * entries in each circular buffer, to protect against confusing empty and full
191 * queue states.
192 *
193 * The device reads or writes the data in the queues via the device's several
194 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700195 *
196 * For Tx queue, there are low mark and high mark limits. If, after queuing
197 * the packet for Tx, free space become < low mark, Tx queue stopped. When
198 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
199 * Tx queue resumed.
200 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800201 * The 4965 operates with up to 17 queues: One receive queue, one transmit
202 * queue (#4) for sending commands to the device firmware, and 15 other
203 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800204 *
205 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700206 ***************************************************/
207
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800208static int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700209{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800210 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700211
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800212 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700213 s -= q->n_bd;
214
215 if (s <= 0)
216 s += q->n_window;
217 /* keep some reserve to not confuse empty and full situations */
218 s -= 2;
219 if (s < 0)
220 s = 0;
221 return s;
222}
223
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800224/**
225 * iwl4965_queue_inc_wrap - increment queue index, wrap back to beginning
226 * @index -- current index
227 * @n_bd -- total number of entries in queue (must be power of 2)
228 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800229static inline int iwl4965_queue_inc_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700230{
231 return ++index & (n_bd - 1);
232}
233
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800234/**
235 * iwl4965_queue_dec_wrap - decrement queue index, wrap back to end
236 * @index -- current index
237 * @n_bd -- total number of entries in queue (must be power of 2)
238 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800239static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700240{
241 return --index & (n_bd - 1);
242}
243
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800244static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700245{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800246 return q->write_ptr > q->read_ptr ?
247 (i >= q->read_ptr && i < q->write_ptr) :
248 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700249}
250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800251static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700252{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800253 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700254 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800255 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700256
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800257 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700258 return index & (q->n_window - 1);
259}
260
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800261/**
262 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
263 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800264static int iwl4965_queue_init(struct iwl4965_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700265 int count, int slots_num, u32 id)
266{
267 q->n_bd = count;
268 q->n_window = slots_num;
269 q->id = id;
270
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800271 /* count must be power-of-two size, otherwise iwl4965_queue_inc_wrap
272 * and iwl4965_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700273 BUG_ON(!is_power_of_2(count));
274
275 /* slots_num must be power-of-two size, otherwise
276 * get_cmd_index is broken. */
277 BUG_ON(!is_power_of_2(slots_num));
278
279 q->low_mark = q->n_window / 4;
280 if (q->low_mark < 4)
281 q->low_mark = 4;
282
283 q->high_mark = q->n_window / 8;
284 if (q->high_mark < 2)
285 q->high_mark = 2;
286
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800287 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700288
289 return 0;
290}
291
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800292/**
293 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
294 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800295static int iwl4965_tx_queue_alloc(struct iwl4965_priv *priv,
296 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700297{
298 struct pci_dev *dev = priv->pci_dev;
299
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800300 /* Driver private data, only for Tx (not command) queues,
301 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700302 if (id != IWL_CMD_QUEUE_NUM) {
303 txq->txb = kmalloc(sizeof(txq->txb[0]) *
304 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
305 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800306 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700307 "structures failed\n");
308 goto error;
309 }
310 } else
311 txq->txb = NULL;
312
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800313 /* Circular buffer of transmit frame descriptors (TFDs),
314 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700315 txq->bd = pci_alloc_consistent(dev,
316 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
317 &txq->q.dma_addr);
318
319 if (!txq->bd) {
320 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
321 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
322 goto error;
323 }
324 txq->q.id = id;
325
326 return 0;
327
328 error:
329 if (txq->txb) {
330 kfree(txq->txb);
331 txq->txb = NULL;
332 }
333
334 return -ENOMEM;
335}
336
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800337/**
338 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
339 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800340int iwl4965_tx_queue_init(struct iwl4965_priv *priv,
341 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700342{
343 struct pci_dev *dev = priv->pci_dev;
344 int len;
345 int rc = 0;
346
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800347 /*
348 * Alloc buffer array for commands (Tx or other types of commands).
349 * For the command queue (#4), allocate command space + one big
350 * command for scan, since scan command is very huge; the system will
351 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200352 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800353 * space is needed.
354 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800355 len = sizeof(struct iwl4965_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700356 if (txq_id == IWL_CMD_QUEUE_NUM)
357 len += IWL_MAX_SCAN_SIZE;
358 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
359 if (!txq->cmd)
360 return -ENOMEM;
361
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800362 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800363 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700364 if (rc) {
365 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
366
367 return -ENOMEM;
368 }
369 txq->need_update = 0;
370
371 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800372 * iwl4965_queue_inc_wrap and iwl4965_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700373 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800374
375 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800376 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700377
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800378 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800379 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700380
381 return 0;
382}
383
384/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800385 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700386 * @txq: Transmit queue to deallocate.
387 *
388 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800389 * Free all buffers.
390 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700391 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800392void iwl4965_tx_queue_free(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700393{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800394 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700395 struct pci_dev *dev = priv->pci_dev;
396 int len;
397
398 if (q->n_bd == 0)
399 return;
400
401 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800402 for (; q->write_ptr != q->read_ptr;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800403 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd))
404 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700405
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800406 len = sizeof(struct iwl4965_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700407 if (q->id == IWL_CMD_QUEUE_NUM)
408 len += IWL_MAX_SCAN_SIZE;
409
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800410 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700411 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
412
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800413 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700414 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800415 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700416 txq->q.n_bd, txq->bd, txq->q.dma_addr);
417
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800418 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700419 if (txq->txb) {
420 kfree(txq->txb);
421 txq->txb = NULL;
422 }
423
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800424 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700425 memset(txq, 0, sizeof(*txq));
426}
427
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800428const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700429
430/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800431 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700432 * the functionality provided here
433 */
434
435/**************************************************************/
436
Ian Schram01ebd062007-10-25 17:15:22 +0800437#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800438/**
439 * iwl4965_remove_station - Remove driver's knowledge of station.
440 *
441 * NOTE: This does not remove station from device's station table.
442 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800443static u8 iwl4965_remove_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700444{
445 int index = IWL_INVALID_STATION;
446 int i;
447 unsigned long flags;
448
449 spin_lock_irqsave(&priv->sta_lock, flags);
450
451 if (is_ap)
452 index = IWL_AP_ID;
453 else if (is_broadcast_ether_addr(addr))
454 index = priv->hw_setting.bcast_sta_id;
455 else
456 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
457 if (priv->stations[i].used &&
458 !compare_ether_addr(priv->stations[i].sta.sta.addr,
459 addr)) {
460 index = i;
461 break;
462 }
463
464 if (unlikely(index == IWL_INVALID_STATION))
465 goto out;
466
467 if (priv->stations[index].used) {
468 priv->stations[index].used = 0;
469 priv->num_stations--;
470 }
471
472 BUG_ON(priv->num_stations < 0);
473
474out:
475 spin_unlock_irqrestore(&priv->sta_lock, flags);
476 return 0;
477}
Zhu Yi556f8db2007-09-27 11:27:33 +0800478#endif
Zhu Yib481de92007-09-25 17:54:57 -0700479
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800480/**
481 * iwl4965_clear_stations_table - Clear the driver's station table
482 *
483 * NOTE: This does not clear or otherwise alter the device's station table.
484 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800485static void iwl4965_clear_stations_table(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700486{
487 unsigned long flags;
488
489 spin_lock_irqsave(&priv->sta_lock, flags);
490
491 priv->num_stations = 0;
492 memset(priv->stations, 0, sizeof(priv->stations));
493
494 spin_unlock_irqrestore(&priv->sta_lock, flags);
495}
496
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800497/**
498 * iwl4965_add_station_flags - Add station to tables in driver and device
499 */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200500u8 iwl4965_add_station_flags(struct iwl4965_priv *priv, const u8 *addr,
501 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700502{
503 int i;
504 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800505 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700506 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700507 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700508
509 spin_lock_irqsave(&priv->sta_lock, flags_spin);
510 if (is_ap)
511 index = IWL_AP_ID;
512 else if (is_broadcast_ether_addr(addr))
513 index = priv->hw_setting.bcast_sta_id;
514 else
515 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
516 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
517 addr)) {
518 index = i;
519 break;
520 }
521
522 if (!priv->stations[i].used &&
523 index == IWL_INVALID_STATION)
524 index = i;
525 }
526
527
Ben Cahill9fbab512007-11-29 11:09:47 +0800528 /* These two conditions have the same outcome, but keep them separate
529 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700530 if (unlikely(index == IWL_INVALID_STATION)) {
531 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
532 return index;
533 }
534
535 if (priv->stations[index].used &&
536 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
537 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
538 return index;
539 }
540
541
Joe Perches0795af52007-10-03 17:59:30 -0700542 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700543 station = &priv->stations[index];
544 station->used = 1;
545 priv->num_stations++;
546
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800547 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800548 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700549 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
550 station->sta.mode = 0;
551 station->sta.sta.sta_id = index;
552 station->sta.station_flags = 0;
553
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800554#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700555 /* BCAST station and IBSS stations do not work in HT mode */
556 if (index != priv->hw_setting.bcast_sta_id &&
557 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200558 iwl4965_set_ht_add_station(priv, index,
559 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800560#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700561
562 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800563
564 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800565 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700566 return index;
567
568}
569
570/*************** DRIVER STATUS FUNCTIONS *****/
571
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800572static inline int iwl4965_is_ready(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700573{
574 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
575 * set but EXIT_PENDING is not */
576 return test_bit(STATUS_READY, &priv->status) &&
577 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
578 !test_bit(STATUS_EXIT_PENDING, &priv->status);
579}
580
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800581static inline int iwl4965_is_alive(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700582{
583 return test_bit(STATUS_ALIVE, &priv->status);
584}
585
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800586static inline int iwl4965_is_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700587{
588 return test_bit(STATUS_INIT, &priv->status);
589}
590
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800591static inline int iwl4965_is_rfkill(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700592{
593 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
594 test_bit(STATUS_RF_KILL_SW, &priv->status);
595}
596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800597static inline int iwl4965_is_ready_rf(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700598{
599
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800600 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700601 return 0;
602
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800603 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700604}
605
606/*************** HOST COMMAND QUEUE FUNCTIONS *****/
607
608#define IWL_CMD(x) case x : return #x
609
610static const char *get_cmd_string(u8 cmd)
611{
612 switch (cmd) {
613 IWL_CMD(REPLY_ALIVE);
614 IWL_CMD(REPLY_ERROR);
615 IWL_CMD(REPLY_RXON);
616 IWL_CMD(REPLY_RXON_ASSOC);
617 IWL_CMD(REPLY_QOS_PARAM);
618 IWL_CMD(REPLY_RXON_TIMING);
619 IWL_CMD(REPLY_ADD_STA);
620 IWL_CMD(REPLY_REMOVE_STA);
621 IWL_CMD(REPLY_REMOVE_ALL_STA);
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 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
653 IWL_CMD(SENSITIVITY_CMD);
654 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
655 IWL_CMD(REPLY_RX_PHY_CMD);
656 IWL_CMD(REPLY_RX_MPDU_CMD);
657 IWL_CMD(REPLY_4965_RX);
658 IWL_CMD(REPLY_COMPRESSED_BA);
659 default:
660 return "UNKNOWN";
661
662 }
663}
664
665#define HOST_COMPLETE_TIMEOUT (HZ / 2)
666
667/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800668 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700669 * @priv: device private data point
670 * @cmd: a point to the ucode command structure
671 *
672 * The function returns < 0 values to indicate the operation is
673 * failed. On success, it turns the index (> 0) of command in the
674 * command queue.
675 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800676static int iwl4965_enqueue_hcmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700677{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800678 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
679 struct iwl4965_queue *q = &txq->q;
680 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700681 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800682 struct iwl4965_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700683 u32 idx;
684 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
685 dma_addr_t phys_addr;
686 int ret;
687 unsigned long flags;
688
689 /* If any of the command structures end up being larger than
690 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
691 * we will need to increase the size of the TFD entries */
692 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
693 !(cmd->meta.flags & CMD_SIZE_HUGE));
694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800695 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700696 IWL_ERROR("No space for Tx\n");
697 return -ENOSPC;
698 }
699
700 spin_lock_irqsave(&priv->hcmd_lock, flags);
701
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800702 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700703 memset(tfd, 0, sizeof(*tfd));
704
705 control_flags = (u32 *) tfd;
706
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800707 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700708 out_cmd = &txq->cmd[idx];
709
710 out_cmd->hdr.cmd = cmd->id;
711 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
712 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
713
714 /* At this point, the out_cmd now has all of the incoming cmd
715 * information */
716
717 out_cmd->hdr.flags = 0;
718 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800719 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700720 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
721 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
722
723 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800724 offsetof(struct iwl4965_cmd, hdr);
725 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700726
727 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
728 "%d bytes at %d[%d]:%d\n",
729 get_cmd_string(out_cmd->hdr.cmd),
730 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800731 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700732
733 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800734
735 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700736 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800737
738 /* Increment and update queue's write index */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800739 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
740 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700741
742 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
743 return ret ? ret : idx;
744}
745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800746static int iwl4965_send_cmd_async(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700747{
748 int ret;
749
750 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
751
752 /* An asynchronous command can not expect an SKB to be set. */
753 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
754
755 /* An asynchronous command MUST have a callback. */
756 BUG_ON(!cmd->meta.u.callback);
757
758 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
759 return -EBUSY;
760
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800761 ret = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700762 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800763 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700764 get_cmd_string(cmd->id), ret);
765 return ret;
766 }
767 return 0;
768}
769
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800770static int iwl4965_send_cmd_sync(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700771{
772 int cmd_idx;
773 int ret;
774 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
775
776 BUG_ON(cmd->meta.flags & CMD_ASYNC);
777
778 /* A synchronous command can not have a callback set. */
779 BUG_ON(cmd->meta.u.callback != NULL);
780
781 if (atomic_xchg(&entry, 1)) {
782 IWL_ERROR("Error sending %s: Already sending a host command\n",
783 get_cmd_string(cmd->id));
784 return -EBUSY;
785 }
786
787 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
788
789 if (cmd->meta.flags & CMD_WANT_SKB)
790 cmd->meta.source = &cmd->meta;
791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800792 cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700793 if (cmd_idx < 0) {
794 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800795 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700796 get_cmd_string(cmd->id), ret);
797 goto out;
798 }
799
800 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
801 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
802 HOST_COMPLETE_TIMEOUT);
803 if (!ret) {
804 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
805 IWL_ERROR("Error sending %s: time out after %dms.\n",
806 get_cmd_string(cmd->id),
807 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
808
809 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
810 ret = -ETIMEDOUT;
811 goto cancel;
812 }
813 }
814
815 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
816 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
817 get_cmd_string(cmd->id));
818 ret = -ECANCELED;
819 goto fail;
820 }
821 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
822 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
823 get_cmd_string(cmd->id));
824 ret = -EIO;
825 goto fail;
826 }
827 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
828 IWL_ERROR("Error: Response NULL in '%s'\n",
829 get_cmd_string(cmd->id));
830 ret = -EIO;
831 goto out;
832 }
833
834 ret = 0;
835 goto out;
836
837cancel:
838 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800839 struct iwl4965_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700840
841 /* Cancel the CMD_WANT_SKB flag for the cmd in the
842 * TX cmd queue. Otherwise in case the cmd comes
843 * in later, it will possibly set an invalid
844 * address (cmd->meta.source). */
845 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
846 qcmd->meta.flags &= ~CMD_WANT_SKB;
847 }
848fail:
849 if (cmd->meta.u.skb) {
850 dev_kfree_skb_any(cmd->meta.u.skb);
851 cmd->meta.u.skb = NULL;
852 }
853out:
854 atomic_set(&entry, 0);
855 return ret;
856}
857
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800858int iwl4965_send_cmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700859{
Zhu Yib481de92007-09-25 17:54:57 -0700860 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800861 return iwl4965_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700862
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800863 return iwl4965_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700864}
865
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800866int iwl4965_send_cmd_pdu(struct iwl4965_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700867{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800868 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700869 .id = id,
870 .len = len,
871 .data = data,
872 };
873
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800874 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700875}
876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800877static int __must_check iwl4965_send_cmd_u32(struct iwl4965_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700878{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800879 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700880 .id = id,
881 .len = sizeof(val),
882 .data = &val,
883 };
884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800885 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700886}
887
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800888int iwl4965_send_statistics_request(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700889{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800890 return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700891}
892
893/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800894 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700895 *
896 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800897 * NOTE: mutex must be held before calling this fnction
898 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800899static int iwl4965_rxon_add_station(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700900 const u8 *addr, int is_ap)
901{
Zhu Yi556f8db2007-09-27 11:27:33 +0800902 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700903
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800904 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200905#ifdef CONFIG_IWL4965_HT
906 struct ieee80211_conf *conf = &priv->hw->conf;
907 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
908
909 if ((is_ap) &&
910 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
911 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
912 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
913 0, cur_ht_config);
914 else
915#endif /* CONFIG_IWL4965_HT */
916 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
917 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800918
919 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700920 iwl4965_add_station(priv, addr, is_ap);
921
Zhu Yi556f8db2007-09-27 11:27:33 +0800922 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700923}
924
925/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800926 * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
Zhu Yib481de92007-09-25 17:54:57 -0700927 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
928 * @channel: Any channel valid for the requested phymode
929
930 * In addition to setting the staging RXON, priv->phymode is also set.
931 *
932 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
933 * in the staging RXON flag structure based on the phymode
934 */
Ben Cahill9fbab512007-11-29 11:09:47 +0800935static int iwl4965_set_rxon_channel(struct iwl4965_priv *priv, u8 phymode,
936 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700937{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800938 if (!iwl4965_get_channel_info(priv, phymode, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700939 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
940 channel, phymode);
941 return -EINVAL;
942 }
943
944 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
945 (priv->phymode == phymode))
946 return 0;
947
948 priv->staging_rxon.channel = cpu_to_le16(channel);
949 if (phymode == MODE_IEEE80211A)
950 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
951 else
952 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
953
954 priv->phymode = phymode;
955
956 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
957
958 return 0;
959}
960
961/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800962 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700963 *
964 * NOTE: This is really only useful during development and can eventually
965 * be #ifdef'd out once the driver is stable and folks aren't actively
966 * making changes
967 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800968static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700969{
970 int error = 0;
971 int counter = 1;
972
973 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
974 error |= le32_to_cpu(rxon->flags &
975 (RXON_FLG_TGJ_NARROW_BAND_MSK |
976 RXON_FLG_RADAR_DETECT_MSK));
977 if (error)
978 IWL_WARNING("check 24G fields %d | %d\n",
979 counter++, error);
980 } else {
981 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
982 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
983 if (error)
984 IWL_WARNING("check 52 fields %d | %d\n",
985 counter++, error);
986 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
987 if (error)
988 IWL_WARNING("check 52 CCK %d | %d\n",
989 counter++, error);
990 }
991 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
992 if (error)
993 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
994
995 /* make sure basic rates 6Mbps and 1Mbps are supported */
996 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
997 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
998 if (error)
999 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
1000
1001 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
1002 if (error)
1003 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
1004
1005 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
1006 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
1007 if (error)
1008 IWL_WARNING("check CCK and short slot %d | %d\n",
1009 counter++, error);
1010
1011 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
1012 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
1013 if (error)
1014 IWL_WARNING("check CCK & auto detect %d | %d\n",
1015 counter++, error);
1016
1017 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
1018 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
1019 if (error)
1020 IWL_WARNING("check TGG and auto detect %d | %d\n",
1021 counter++, error);
1022
1023 if (error)
1024 IWL_WARNING("Tuning to channel %d\n",
1025 le16_to_cpu(rxon->channel));
1026
1027 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001028 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -07001029 return -1;
1030 }
1031 return 0;
1032}
1033
1034/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001035 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +08001036 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -07001037 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001038 * If the RXON structure is changing enough to require a new tune,
1039 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
1040 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -07001041 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001042static int iwl4965_full_rxon_required(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001043{
1044
1045 /* These items are only settable from the full RXON command */
1046 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
1047 compare_ether_addr(priv->staging_rxon.bssid_addr,
1048 priv->active_rxon.bssid_addr) ||
1049 compare_ether_addr(priv->staging_rxon.node_addr,
1050 priv->active_rxon.node_addr) ||
1051 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
1052 priv->active_rxon.wlap_bssid_addr) ||
1053 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
1054 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
1055 (priv->staging_rxon.air_propagation !=
1056 priv->active_rxon.air_propagation) ||
1057 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
1058 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
1059 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
1060 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
1061 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
1062 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1063 return 1;
1064
1065 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1066 * be updated with the RXON_ASSOC command -- however only some
1067 * flag transitions are allowed using RXON_ASSOC */
1068
1069 /* Check if we are not switching bands */
1070 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1071 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1072 return 1;
1073
1074 /* Check if we are switching association toggle */
1075 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1076 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1077 return 1;
1078
1079 return 0;
1080}
1081
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001082static int iwl4965_send_rxon_assoc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001083{
1084 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001085 struct iwl4965_rx_packet *res = NULL;
1086 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1087 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001088 .id = REPLY_RXON_ASSOC,
1089 .len = sizeof(rxon_assoc),
1090 .meta.flags = CMD_WANT_SKB,
1091 .data = &rxon_assoc,
1092 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001093 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1094 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001095
1096 if ((rxon1->flags == rxon2->flags) &&
1097 (rxon1->filter_flags == rxon2->filter_flags) &&
1098 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1099 (rxon1->ofdm_ht_single_stream_basic_rates ==
1100 rxon2->ofdm_ht_single_stream_basic_rates) &&
1101 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1102 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1103 (rxon1->rx_chain == rxon2->rx_chain) &&
1104 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1105 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1106 return 0;
1107 }
1108
1109 rxon_assoc.flags = priv->staging_rxon.flags;
1110 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1111 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1112 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1113 rxon_assoc.reserved = 0;
1114 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1115 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1116 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1117 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1118 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1119
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001120 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001121 if (rc)
1122 return rc;
1123
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001124 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001125 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1126 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1127 rc = -EIO;
1128 }
1129
1130 priv->alloc_rxb_skb--;
1131 dev_kfree_skb_any(cmd.meta.u.skb);
1132
1133 return rc;
1134}
1135
1136/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001137 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001138 *
Ian Schram01ebd062007-10-25 17:15:22 +08001139 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001140 * the active_rxon structure is updated with the new data. This
1141 * function correctly transitions out of the RXON_ASSOC_MSK state if
1142 * a HW tune is required based on the RXON structure changes.
1143 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001144static int iwl4965_commit_rxon(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001145{
1146 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001147 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001148 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001149 int rc = 0;
1150
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001151 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001152 return -1;
1153
1154 /* always get timestamp with Rx frame */
1155 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1156
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001157 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001158 if (rc) {
1159 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1160 return -EINVAL;
1161 }
1162
1163 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001164 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001165 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001166 if (!iwl4965_full_rxon_required(priv)) {
1167 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001168 if (rc) {
1169 IWL_ERROR("Error setting RXON_ASSOC "
1170 "configuration (%d).\n", rc);
1171 return rc;
1172 }
1173
1174 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1175
1176 return 0;
1177 }
1178
1179 /* station table will be cleared */
1180 priv->assoc_station_added = 0;
1181
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001182#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001183 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1184 if (!priv->error_recovering)
1185 priv->start_calib = 0;
1186
1187 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001188#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001189
1190 /* If we are currently associated and the new config requires
1191 * an RXON_ASSOC and the new config wants the associated mask enabled,
1192 * we must clear the associated from the active configuration
1193 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001194 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001195 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1196 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1197 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1198
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001199 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1200 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001201 &priv->active_rxon);
1202
1203 /* If the mask clearing failed then we set
1204 * active_rxon back to what it was previously */
1205 if (rc) {
1206 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1207 IWL_ERROR("Error clearing ASSOC_MSK on current "
1208 "configuration (%d).\n", rc);
1209 return rc;
1210 }
Zhu Yib481de92007-09-25 17:54:57 -07001211 }
1212
1213 IWL_DEBUG_INFO("Sending RXON\n"
1214 "* with%s RXON_FILTER_ASSOC_MSK\n"
1215 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001216 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001217 ((priv->staging_rxon.filter_flags &
1218 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1219 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001220 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001221
1222 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001223 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1224 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001225 if (rc) {
1226 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1227 return rc;
1228 }
1229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001230 iwl4965_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001231
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001232#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001233 if (!priv->error_recovering)
1234 priv->start_calib = 0;
1235
1236 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1237 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001238#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001239
1240 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1241
1242 /* If we issue a new RXON command which required a tune then we must
1243 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001244 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001245 if (rc) {
1246 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1247 return rc;
1248 }
1249
1250 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001251 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001252 IWL_INVALID_STATION) {
1253 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1254 return -EIO;
1255 }
1256
1257 /* If we have set the ASSOC_MSK and we are in BSS mode then
1258 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001259 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001260 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001261 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -07001262 == IWL_INVALID_STATION) {
1263 IWL_ERROR("Error adding AP address for transmit.\n");
1264 return -EIO;
1265 }
1266 priv->assoc_station_added = 1;
1267 }
1268
1269 return 0;
1270}
1271
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001272static int iwl4965_send_bt_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001273{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001274 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001275 .flags = 3,
1276 .lead_time = 0xAA,
1277 .max_kill = 1,
1278 .kill_ack_mask = 0,
1279 .kill_cts_mask = 0,
1280 };
1281
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001282 return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1283 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001284}
1285
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001286static int iwl4965_send_scan_abort(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001287{
1288 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001289 struct iwl4965_rx_packet *res;
1290 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001291 .id = REPLY_SCAN_ABORT_CMD,
1292 .meta.flags = CMD_WANT_SKB,
1293 };
1294
1295 /* If there isn't a scan actively going on in the hardware
1296 * then we are in between scan bands and not actually
1297 * actively scanning, so don't send the abort command */
1298 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1299 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1300 return 0;
1301 }
1302
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001303 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001304 if (rc) {
1305 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1306 return rc;
1307 }
1308
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001309 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001310 if (res->u.status != CAN_ABORT_STATUS) {
1311 /* The scan abort will return 1 for success or
1312 * 2 for "failure". A failure condition can be
1313 * due to simply not being in an active scan which
1314 * can occur if we send the scan abort before we
1315 * the microcode has notified us that a scan is
1316 * completed. */
1317 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1318 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1319 clear_bit(STATUS_SCAN_HW, &priv->status);
1320 }
1321
1322 dev_kfree_skb_any(cmd.meta.u.skb);
1323
1324 return rc;
1325}
1326
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001327static int iwl4965_card_state_sync_callback(struct iwl4965_priv *priv,
1328 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001329 struct sk_buff *skb)
1330{
1331 return 1;
1332}
1333
1334/*
1335 * CARD_STATE_CMD
1336 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001337 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001338 *
1339 * When in the 'enable' state the card operates as normal.
1340 * When in the 'disable' state, the card enters into a low power mode.
1341 * When in the 'halt' state, the card is shut down and must be fully
1342 * restarted to come back on.
1343 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001344static int iwl4965_send_card_state(struct iwl4965_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001345{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001346 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001347 .id = REPLY_CARD_STATE_CMD,
1348 .len = sizeof(u32),
1349 .data = &flags,
1350 .meta.flags = meta_flag,
1351 };
1352
1353 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001354 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001355
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001356 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001357}
1358
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001359static int iwl4965_add_sta_sync_callback(struct iwl4965_priv *priv,
1360 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001361{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001362 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001363
1364 if (!skb) {
1365 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1366 return 1;
1367 }
1368
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001369 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001370 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1371 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1372 res->hdr.flags);
1373 return 1;
1374 }
1375
1376 switch (res->u.add_sta.status) {
1377 case ADD_STA_SUCCESS_MSK:
1378 break;
1379 default:
1380 break;
1381 }
1382
1383 /* We didn't cache the SKB; let the caller free it */
1384 return 1;
1385}
1386
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001387int iwl4965_send_add_station(struct iwl4965_priv *priv,
1388 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001389{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001390 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001391 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001392 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001393 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001394 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001395 .meta.flags = flags,
1396 .data = sta,
1397 };
1398
1399 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001400 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001401 else
1402 cmd.meta.flags |= CMD_WANT_SKB;
1403
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001404 rc = iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001405
1406 if (rc || (flags & CMD_ASYNC))
1407 return rc;
1408
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001409 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001410 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1411 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1412 res->hdr.flags);
1413 rc = -EIO;
1414 }
1415
1416 if (rc == 0) {
1417 switch (res->u.add_sta.status) {
1418 case ADD_STA_SUCCESS_MSK:
1419 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1420 break;
1421 default:
1422 rc = -EIO;
1423 IWL_WARNING("REPLY_ADD_STA failed\n");
1424 break;
1425 }
1426 }
1427
1428 priv->alloc_rxb_skb--;
1429 dev_kfree_skb_any(cmd.meta.u.skb);
1430
1431 return rc;
1432}
1433
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001434static int iwl4965_update_sta_key_info(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001435 struct ieee80211_key_conf *keyconf,
1436 u8 sta_id)
1437{
1438 unsigned long flags;
1439 __le16 key_flags = 0;
1440
1441 switch (keyconf->alg) {
1442 case ALG_CCMP:
1443 key_flags |= STA_KEY_FLG_CCMP;
1444 key_flags |= cpu_to_le16(
1445 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1446 key_flags &= ~STA_KEY_FLG_INVALID;
1447 break;
1448 case ALG_TKIP:
1449 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001450 default:
1451 return -EINVAL;
1452 }
1453 spin_lock_irqsave(&priv->sta_lock, flags);
1454 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1455 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1456 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1457 keyconf->keylen);
1458
1459 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1460 keyconf->keylen);
1461 priv->stations[sta_id].sta.key.key_flags = key_flags;
1462 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1463 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1464
1465 spin_unlock_irqrestore(&priv->sta_lock, flags);
1466
1467 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001468 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001469 return 0;
1470}
1471
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001472static int iwl4965_clear_sta_key_info(struct iwl4965_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001473{
1474 unsigned long flags;
1475
1476 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001477 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1478 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001479 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1480 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1481 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1482 spin_unlock_irqrestore(&priv->sta_lock, flags);
1483
1484 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001485 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001486 return 0;
1487}
1488
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001489static void iwl4965_clear_free_frames(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001490{
1491 struct list_head *element;
1492
1493 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1494 priv->frames_count);
1495
1496 while (!list_empty(&priv->free_frames)) {
1497 element = priv->free_frames.next;
1498 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001499 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001500 priv->frames_count--;
1501 }
1502
1503 if (priv->frames_count) {
1504 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1505 priv->frames_count);
1506 priv->frames_count = 0;
1507 }
1508}
1509
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001510static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001511{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001512 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001513 struct list_head *element;
1514 if (list_empty(&priv->free_frames)) {
1515 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1516 if (!frame) {
1517 IWL_ERROR("Could not allocate frame!\n");
1518 return NULL;
1519 }
1520
1521 priv->frames_count++;
1522 return frame;
1523 }
1524
1525 element = priv->free_frames.next;
1526 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001527 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001528}
1529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001530static void iwl4965_free_frame(struct iwl4965_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001531{
1532 memset(frame, 0, sizeof(*frame));
1533 list_add(&frame->list, &priv->free_frames);
1534}
1535
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001536unsigned int iwl4965_fill_beacon_frame(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001537 struct ieee80211_hdr *hdr,
1538 const u8 *dest, int left)
1539{
1540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001541 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001542 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1543 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1544 return 0;
1545
1546 if (priv->ibss_beacon->len > left)
1547 return 0;
1548
1549 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1550
1551 return priv->ibss_beacon->len;
1552}
1553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001554int iwl4965_rate_index_from_plcp(int plcp)
Zhu Yib481de92007-09-25 17:54:57 -07001555{
1556 int i = 0;
1557
Ben Cahill77626352007-11-29 11:09:44 +08001558 /* 4965 HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -07001559 if (plcp & RATE_MCS_HT_MSK) {
1560 i = (plcp & 0xff);
1561
1562 if (i >= IWL_RATE_MIMO_6M_PLCP)
1563 i = i - IWL_RATE_MIMO_6M_PLCP;
1564
1565 i += IWL_FIRST_OFDM_RATE;
1566 /* skip 9M not supported in ht*/
1567 if (i >= IWL_RATE_9M_INDEX)
1568 i += 1;
1569 if ((i >= IWL_FIRST_OFDM_RATE) &&
1570 (i <= IWL_LAST_OFDM_RATE))
1571 return i;
Ben Cahill77626352007-11-29 11:09:44 +08001572
1573 /* 4965 legacy rate format, search for match in table */
Zhu Yib481de92007-09-25 17:54:57 -07001574 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001575 for (i = 0; i < ARRAY_SIZE(iwl4965_rates); i++)
1576 if (iwl4965_rates[i].plcp == (plcp &0xFF))
Zhu Yib481de92007-09-25 17:54:57 -07001577 return i;
1578 }
1579 return -1;
1580}
1581
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001582static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001583{
1584 u8 i;
1585
1586 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001587 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001588 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001589 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001590 }
1591
1592 return IWL_RATE_INVALID;
1593}
1594
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001595static int iwl4965_send_beacon_cmd(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001596{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001597 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001598 unsigned int frame_size;
1599 int rc;
1600 u8 rate;
1601
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001602 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001603
1604 if (!frame) {
1605 IWL_ERROR("Could not obtain free frame buffer for beacon "
1606 "command.\n");
1607 return -ENOMEM;
1608 }
1609
1610 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001611 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001612 0xFF0);
1613 if (rate == IWL_INVALID_RATE)
1614 rate = IWL_RATE_6M_PLCP;
1615 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001616 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001617 if (rate == IWL_INVALID_RATE)
1618 rate = IWL_RATE_1M_PLCP;
1619 }
1620
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001621 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001622
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001623 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001624 &frame->u.cmd[0]);
1625
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001626 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001627
1628 return rc;
1629}
1630
1631/******************************************************************************
1632 *
1633 * EEPROM related functions
1634 *
1635 ******************************************************************************/
1636
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001637static void get_eeprom_mac(struct iwl4965_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001638{
1639 memcpy(mac, priv->eeprom.mac_address, 6);
1640}
1641
1642/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001643 * iwl4965_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001644 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001645 * Load the EEPROM contents from adapter into priv->eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001646 *
1647 * NOTE: This routine uses the non-debug IO access functions.
1648 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001649int iwl4965_eeprom_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001650{
1651 u16 *e = (u16 *)&priv->eeprom;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001652 u32 gp = iwl4965_read32(priv, CSR_EEPROM_GP);
Zhu Yib481de92007-09-25 17:54:57 -07001653 u32 r;
1654 int sz = sizeof(priv->eeprom);
1655 int rc;
1656 int i;
1657 u16 addr;
1658
1659 /* The EEPROM structure has several padding buffers within it
1660 * and when adding new EEPROM maps is subject to programmer errors
1661 * which may be very difficult to identify without explicitly
1662 * checking the resulting size of the eeprom map. */
1663 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1664
1665 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1666 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1667 return -ENOENT;
1668 }
1669
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001670 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001671 rc = iwl4965_eeprom_acquire_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001672 if (rc < 0) {
Ian Schram91e17472007-10-25 17:15:23 +08001673 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001674 return -ENOENT;
1675 }
1676
1677 /* eeprom is an array of 16bit values */
1678 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001679 _iwl4965_write32(priv, CSR_EEPROM_REG, addr << 1);
1680 _iwl4965_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
Zhu Yib481de92007-09-25 17:54:57 -07001681
1682 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1683 i += IWL_EEPROM_ACCESS_DELAY) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001684 r = _iwl4965_read_direct32(priv, CSR_EEPROM_REG);
Zhu Yib481de92007-09-25 17:54:57 -07001685 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1686 break;
1687 udelay(IWL_EEPROM_ACCESS_DELAY);
1688 }
1689
1690 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1691 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1692 rc = -ETIMEDOUT;
1693 goto done;
1694 }
1695 e[addr / 2] = le16_to_cpu(r >> 16);
1696 }
1697 rc = 0;
1698
1699done:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001700 iwl4965_eeprom_release_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001701 return rc;
1702}
1703
1704/******************************************************************************
1705 *
1706 * Misc. internal state and helper functions
1707 *
1708 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001709#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001710
1711/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001712 * iwl4965_report_frame - dump frame to syslog during debug sessions
Zhu Yib481de92007-09-25 17:54:57 -07001713 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001714 * You may hack this function to show different aspects of received frames,
Zhu Yib481de92007-09-25 17:54:57 -07001715 * including selective frame dumps.
1716 * group100 parameter selects whether to show 1 out of 100 good frames.
1717 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001718 * TODO: This was originally written for 3945, need to audit for
1719 * proper operation with 4965.
Zhu Yib481de92007-09-25 17:54:57 -07001720 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001721void iwl4965_report_frame(struct iwl4965_priv *priv,
1722 struct iwl4965_rx_packet *pkt,
Zhu Yib481de92007-09-25 17:54:57 -07001723 struct ieee80211_hdr *header, int group100)
1724{
1725 u32 to_us;
1726 u32 print_summary = 0;
1727 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1728 u32 hundred = 0;
1729 u32 dataframe = 0;
1730 u16 fc;
1731 u16 seq_ctl;
1732 u16 channel;
1733 u16 phy_flags;
1734 int rate_sym;
1735 u16 length;
1736 u16 status;
1737 u16 bcn_tmr;
1738 u32 tsf_low;
1739 u64 tsf;
1740 u8 rssi;
1741 u8 agc;
1742 u16 sig_avg;
1743 u16 noise_diff;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001744 struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1745 struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1746 struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
Zhu Yib481de92007-09-25 17:54:57 -07001747 u8 *data = IWL_RX_DATA(pkt);
1748
1749 /* MAC header */
1750 fc = le16_to_cpu(header->frame_control);
1751 seq_ctl = le16_to_cpu(header->seq_ctrl);
1752
1753 /* metadata */
1754 channel = le16_to_cpu(rx_hdr->channel);
1755 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1756 rate_sym = rx_hdr->rate;
1757 length = le16_to_cpu(rx_hdr->len);
1758
1759 /* end-of-frame status and timestamp */
1760 status = le32_to_cpu(rx_end->status);
1761 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1762 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1763 tsf = le64_to_cpu(rx_end->timestamp);
1764
1765 /* signal statistics */
1766 rssi = rx_stats->rssi;
1767 agc = rx_stats->agc;
1768 sig_avg = le16_to_cpu(rx_stats->sig_avg);
1769 noise_diff = le16_to_cpu(rx_stats->noise_diff);
1770
1771 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1772
1773 /* if data frame is to us and all is good,
1774 * (optionally) print summary for only 1 out of every 100 */
1775 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1776 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1777 dataframe = 1;
1778 if (!group100)
1779 print_summary = 1; /* print each frame */
1780 else if (priv->framecnt_to_us < 100) {
1781 priv->framecnt_to_us++;
1782 print_summary = 0;
1783 } else {
1784 priv->framecnt_to_us = 0;
1785 print_summary = 1;
1786 hundred = 1;
1787 }
1788 } else {
1789 /* print summary for all other frames */
1790 print_summary = 1;
1791 }
1792
1793 if (print_summary) {
1794 char *title;
1795 u32 rate;
1796
1797 if (hundred)
1798 title = "100Frames";
1799 else if (fc & IEEE80211_FCTL_RETRY)
1800 title = "Retry";
1801 else if (ieee80211_is_assoc_response(fc))
1802 title = "AscRsp";
1803 else if (ieee80211_is_reassoc_response(fc))
1804 title = "RasRsp";
1805 else if (ieee80211_is_probe_response(fc)) {
1806 title = "PrbRsp";
1807 print_dump = 1; /* dump frame contents */
1808 } else if (ieee80211_is_beacon(fc)) {
1809 title = "Beacon";
1810 print_dump = 1; /* dump frame contents */
1811 } else if (ieee80211_is_atim(fc))
1812 title = "ATIM";
1813 else if (ieee80211_is_auth(fc))
1814 title = "Auth";
1815 else if (ieee80211_is_deauth(fc))
1816 title = "DeAuth";
1817 else if (ieee80211_is_disassoc(fc))
1818 title = "DisAssoc";
1819 else
1820 title = "Frame";
1821
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001822 rate = iwl4965_rate_index_from_plcp(rate_sym);
Zhu Yib481de92007-09-25 17:54:57 -07001823 if (rate == -1)
1824 rate = 0;
1825 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001826 rate = iwl4965_rates[rate].ieee / 2;
Zhu Yib481de92007-09-25 17:54:57 -07001827
1828 /* print frame summary.
1829 * MAC addresses show just the last byte (for brevity),
1830 * but you can hack it to show more, if you'd like to. */
1831 if (dataframe)
1832 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1833 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1834 title, fc, header->addr1[5],
1835 length, rssi, channel, rate);
1836 else {
1837 /* src/dst addresses assume managed mode */
1838 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1839 "src=0x%02x, rssi=%u, tim=%lu usec, "
1840 "phy=0x%02x, chnl=%d\n",
1841 title, fc, header->addr1[5],
1842 header->addr3[5], rssi,
1843 tsf_low - priv->scan_start_tsf,
1844 phy_flags, channel);
1845 }
1846 }
1847 if (print_dump)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001848 iwl4965_print_hex_dump(IWL_DL_RX, data, length);
Zhu Yib481de92007-09-25 17:54:57 -07001849}
1850#endif
1851
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001852static void iwl4965_unset_hw_setting(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001853{
1854 if (priv->hw_setting.shared_virt)
1855 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001856 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001857 priv->hw_setting.shared_virt,
1858 priv->hw_setting.shared_phys);
1859}
1860
1861/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001862 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001863 *
1864 * return : set the bit for each supported rate insert in ie
1865 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001866static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001867 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001868{
1869 u16 ret_rates = 0, bit;
1870 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001871 u8 *cnt = ie;
1872 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001873
1874 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1875 if (bit & supported_rate) {
1876 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001877 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001878 ((bit & basic_rate) ? 0x80 : 0x00);
1879 (*cnt)++;
1880 (*left)--;
1881 if ((*left <= 0) ||
1882 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001883 break;
1884 }
1885 }
1886
1887 return ret_rates;
1888}
1889
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001890#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001891void static iwl4965_set_ht_capab(struct ieee80211_hw *hw,
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001892 struct ieee80211_ht_cap *ht_cap,
1893 u8 use_current_config);
Zhu Yib481de92007-09-25 17:54:57 -07001894#endif
1895
1896/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001897 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001898 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001899static u16 iwl4965_fill_probe_req(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001900 struct ieee80211_mgmt *frame,
1901 int left, int is_direct)
1902{
1903 int len = 0;
1904 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001905 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001906#ifdef CONFIG_IWL4965_HT
1907 struct ieee80211_hw_mode *mode;
1908#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001909
1910 /* Make sure there is enough space for the probe request,
1911 * two mandatory IEs and the data */
1912 left -= 24;
1913 if (left < 0)
1914 return 0;
1915 len += 24;
1916
1917 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001918 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001919 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001920 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001921 frame->seq_ctrl = 0;
1922
1923 /* fill in our indirect SSID IE */
1924 /* ...next IE... */
1925
1926 left -= 2;
1927 if (left < 0)
1928 return 0;
1929 len += 2;
1930 pos = &(frame->u.probe_req.variable[0]);
1931 *pos++ = WLAN_EID_SSID;
1932 *pos++ = 0;
1933
1934 /* fill in our direct SSID IE... */
1935 if (is_direct) {
1936 /* ...next IE... */
1937 left -= 2 + priv->essid_len;
1938 if (left < 0)
1939 return 0;
1940 /* ... fill it in... */
1941 *pos++ = WLAN_EID_SSID;
1942 *pos++ = priv->essid_len;
1943 memcpy(pos, priv->essid, priv->essid_len);
1944 pos += priv->essid_len;
1945 len += 2 + priv->essid_len;
1946 }
1947
1948 /* fill in supported rate */
1949 /* ...next IE... */
1950 left -= 2;
1951 if (left < 0)
1952 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001953
Zhu Yib481de92007-09-25 17:54:57 -07001954 /* ... fill it in... */
1955 *pos++ = WLAN_EID_SUPP_RATES;
1956 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001957
mabbasbee488d2007-10-25 17:15:42 +08001958 /* exclude 60M rate */
1959 active_rates = priv->rates_mask;
1960 active_rates &= ~IWL_RATE_60M_MASK;
1961
1962 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001963
Tomas Winklerc7c46672007-10-18 02:04:15 +02001964 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001965 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001966 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001967 active_rates &= ~ret_rates;
1968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001969 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001970 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001971 active_rates &= ~ret_rates;
1972
Zhu Yib481de92007-09-25 17:54:57 -07001973 len += 2 + *pos;
1974 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001975 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001976 goto fill_end;
1977
1978 /* fill in supported extended rate */
1979 /* ...next IE... */
1980 left -= 2;
1981 if (left < 0)
1982 return 0;
1983 /* ... fill it in... */
1984 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1985 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001986 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001987 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001988 if (*pos > 0)
1989 len += 2 + *pos;
1990
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001991#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001992 mode = priv->hw->conf.mode;
1993 if (mode->ht_info.ht_supported) {
Zhu Yib481de92007-09-25 17:54:57 -07001994 pos += (*pos) + 1;
1995 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001996 *pos++ = sizeof(struct ieee80211_ht_cap);
1997 iwl4965_set_ht_capab(priv->hw,
1998 (struct ieee80211_ht_cap *)pos, 0);
1999 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07002000 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002001#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002002
2003 fill_end:
2004 return (u16)len;
2005}
2006
2007/*
2008 * QoS support
2009*/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002010#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002011static int iwl4965_send_qos_params_command(struct iwl4965_priv *priv,
2012 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07002013{
2014
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002015 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
2016 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07002017}
2018
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002019static void iwl4965_reset_qos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002020{
2021 u16 cw_min = 15;
2022 u16 cw_max = 1023;
2023 u8 aifs = 2;
2024 u8 is_legacy = 0;
2025 unsigned long flags;
2026 int i;
2027
2028 spin_lock_irqsave(&priv->lock, flags);
2029 priv->qos_data.qos_active = 0;
2030
2031 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
2032 if (priv->qos_data.qos_enable)
2033 priv->qos_data.qos_active = 1;
2034 if (!(priv->active_rate & 0xfff0)) {
2035 cw_min = 31;
2036 is_legacy = 1;
2037 }
2038 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2039 if (priv->qos_data.qos_enable)
2040 priv->qos_data.qos_active = 1;
2041 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
2042 cw_min = 31;
2043 is_legacy = 1;
2044 }
2045
2046 if (priv->qos_data.qos_active)
2047 aifs = 3;
2048
2049 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
2050 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
2051 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
2052 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
2053 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
2054
2055 if (priv->qos_data.qos_active) {
2056 i = 1;
2057 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
2058 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
2059 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
2060 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2061 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2062
2063 i = 2;
2064 priv->qos_data.def_qos_parm.ac[i].cw_min =
2065 cpu_to_le16((cw_min + 1) / 2 - 1);
2066 priv->qos_data.def_qos_parm.ac[i].cw_max =
2067 cpu_to_le16(cw_max);
2068 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2069 if (is_legacy)
2070 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2071 cpu_to_le16(6016);
2072 else
2073 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2074 cpu_to_le16(3008);
2075 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2076
2077 i = 3;
2078 priv->qos_data.def_qos_parm.ac[i].cw_min =
2079 cpu_to_le16((cw_min + 1) / 4 - 1);
2080 priv->qos_data.def_qos_parm.ac[i].cw_max =
2081 cpu_to_le16((cw_max + 1) / 2 - 1);
2082 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2083 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2084 if (is_legacy)
2085 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2086 cpu_to_le16(3264);
2087 else
2088 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2089 cpu_to_le16(1504);
2090 } else {
2091 for (i = 1; i < 4; i++) {
2092 priv->qos_data.def_qos_parm.ac[i].cw_min =
2093 cpu_to_le16(cw_min);
2094 priv->qos_data.def_qos_parm.ac[i].cw_max =
2095 cpu_to_le16(cw_max);
2096 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
2097 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2098 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2099 }
2100 }
2101 IWL_DEBUG_QOS("set QoS to default \n");
2102
2103 spin_unlock_irqrestore(&priv->lock, flags);
2104}
2105
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002106static void iwl4965_activate_qos(struct iwl4965_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07002107{
2108 unsigned long flags;
2109
Zhu Yib481de92007-09-25 17:54:57 -07002110 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2111 return;
2112
2113 if (!priv->qos_data.qos_enable)
2114 return;
2115
2116 spin_lock_irqsave(&priv->lock, flags);
2117 priv->qos_data.def_qos_parm.qos_flags = 0;
2118
2119 if (priv->qos_data.qos_cap.q_AP.queue_request &&
2120 !priv->qos_data.qos_cap.q_AP.txop_request)
2121 priv->qos_data.def_qos_parm.qos_flags |=
2122 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002123 if (priv->qos_data.qos_active)
2124 priv->qos_data.def_qos_parm.qos_flags |=
2125 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2126
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002127#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02002128 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08002129 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002130#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08002131
Zhu Yib481de92007-09-25 17:54:57 -07002132 spin_unlock_irqrestore(&priv->lock, flags);
2133
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002134 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08002135 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
2136 priv->qos_data.qos_active,
2137 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002138
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002139 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002140 &(priv->qos_data.def_qos_parm));
2141 }
2142}
2143
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002144#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07002145/*
2146 * Power management (not Tx power!) functions
2147 */
2148#define MSEC_TO_USEC 1024
2149
2150#define NOSLP __constant_cpu_to_le16(0), 0, 0
2151#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
2152#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2153#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2154 __constant_cpu_to_le32(X1), \
2155 __constant_cpu_to_le32(X2), \
2156 __constant_cpu_to_le32(X3), \
2157 __constant_cpu_to_le32(X4)}
2158
2159
2160/* default power management (not Tx power) table values */
2161/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002162static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07002163 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2164 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2165 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2166 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2167 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2168 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2169};
2170
2171/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002172static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07002173 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2174 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2175 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2176 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2177 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2178 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2179 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2180 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2181 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2182 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2183};
2184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002185int iwl4965_power_init_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002186{
2187 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002188 struct iwl4965_power_mgr *pow_data;
2189 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07002190 u16 pci_pm;
2191
2192 IWL_DEBUG_POWER("Initialize power \n");
2193
2194 pow_data = &(priv->power_data);
2195
2196 memset(pow_data, 0, sizeof(*pow_data));
2197
2198 pow_data->active_index = IWL_POWER_RANGE_0;
2199 pow_data->dtim_val = 0xffff;
2200
2201 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2202 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2203
2204 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2205 if (rc != 0)
2206 return 0;
2207 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002208 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002209
2210 IWL_DEBUG_POWER("adjust power command flags\n");
2211
2212 for (i = 0; i < IWL_POWER_AC; i++) {
2213 cmd = &pow_data->pwr_range_0[i].cmd;
2214
2215 if (pci_pm & 0x1)
2216 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2217 else
2218 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2219 }
2220 }
2221 return rc;
2222}
2223
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002224static int iwl4965_update_power_cmd(struct iwl4965_priv *priv,
2225 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002226{
2227 int rc = 0, i;
2228 u8 skip;
2229 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002230 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07002231 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002232 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07002233
2234 if (mode > IWL_POWER_INDEX_5) {
2235 IWL_DEBUG_POWER("Error invalid power mode \n");
2236 return -1;
2237 }
2238 pow_data = &(priv->power_data);
2239
2240 if (pow_data->active_index == IWL_POWER_RANGE_0)
2241 range = &pow_data->pwr_range_0[0];
2242 else
2243 range = &pow_data->pwr_range_1[1];
2244
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002245 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07002246
2247#ifdef IWL_MAC80211_DISABLE
2248 if (priv->assoc_network != NULL) {
2249 unsigned long flags;
2250
2251 period = priv->assoc_network->tim.tim_period;
2252 }
2253#endif /*IWL_MAC80211_DISABLE */
2254 skip = range[mode].no_dtim;
2255
2256 if (period == 0) {
2257 period = 1;
2258 skip = 0;
2259 }
2260
2261 if (skip == 0) {
2262 max_sleep = period;
2263 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2264 } else {
2265 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2266 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2267 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2268 }
2269
2270 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2271 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2272 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2273 }
2274
2275 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2276 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2277 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2278 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2279 le32_to_cpu(cmd->sleep_interval[0]),
2280 le32_to_cpu(cmd->sleep_interval[1]),
2281 le32_to_cpu(cmd->sleep_interval[2]),
2282 le32_to_cpu(cmd->sleep_interval[3]),
2283 le32_to_cpu(cmd->sleep_interval[4]));
2284
2285 return rc;
2286}
2287
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002288static int iwl4965_send_power_mode(struct iwl4965_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002289{
John W. Linville9a62f732007-11-15 16:27:36 -05002290 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002291 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002292 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002293
2294 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08002295 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07002296 * else user level */
2297 switch (mode) {
2298 case IWL_POWER_BATTERY:
2299 final_mode = IWL_POWER_INDEX_3;
2300 break;
2301 case IWL_POWER_AC:
2302 final_mode = IWL_POWER_MODE_CAM;
2303 break;
2304 default:
2305 final_mode = mode;
2306 break;
2307 }
2308
2309 cmd.keep_alive_beacons = 0;
2310
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002311 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002312
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002313 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002314
2315 if (final_mode == IWL_POWER_MODE_CAM)
2316 clear_bit(STATUS_POWER_PMI, &priv->status);
2317 else
2318 set_bit(STATUS_POWER_PMI, &priv->status);
2319
2320 return rc;
2321}
2322
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002323int iwl4965_is_network_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002324{
2325 /* Filter incoming packets to determine if they are targeted toward
2326 * this network, discarding packets coming from ourselves */
2327 switch (priv->iw_mode) {
2328 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2329 /* packets from our adapter are dropped (echo) */
2330 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2331 return 0;
2332 /* {broad,multi}cast packets to our IBSS go through */
2333 if (is_multicast_ether_addr(header->addr1))
2334 return !compare_ether_addr(header->addr3, priv->bssid);
2335 /* packets to our adapter go through */
2336 return !compare_ether_addr(header->addr1, priv->mac_addr);
2337 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2338 /* packets from our adapter are dropped (echo) */
2339 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2340 return 0;
2341 /* {broad,multi}cast packets to our BSS go through */
2342 if (is_multicast_ether_addr(header->addr1))
2343 return !compare_ether_addr(header->addr2, priv->bssid);
2344 /* packets to our adapter go through */
2345 return !compare_ether_addr(header->addr1, priv->mac_addr);
2346 }
2347
2348 return 1;
2349}
2350
2351#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002353static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002354{
2355 switch (status & TX_STATUS_MSK) {
2356 case TX_STATUS_SUCCESS:
2357 return "SUCCESS";
2358 TX_STATUS_ENTRY(SHORT_LIMIT);
2359 TX_STATUS_ENTRY(LONG_LIMIT);
2360 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2361 TX_STATUS_ENTRY(MGMNT_ABORT);
2362 TX_STATUS_ENTRY(NEXT_FRAG);
2363 TX_STATUS_ENTRY(LIFE_EXPIRE);
2364 TX_STATUS_ENTRY(DEST_PS);
2365 TX_STATUS_ENTRY(ABORTED);
2366 TX_STATUS_ENTRY(BT_RETRY);
2367 TX_STATUS_ENTRY(STA_INVALID);
2368 TX_STATUS_ENTRY(FRAG_DROPPED);
2369 TX_STATUS_ENTRY(TID_DISABLE);
2370 TX_STATUS_ENTRY(FRAME_FLUSHED);
2371 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2372 TX_STATUS_ENTRY(TX_LOCKED);
2373 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2374 }
2375
2376 return "UNKNOWN";
2377}
2378
2379/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002380 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002381 *
2382 * NOTE: priv->mutex is not required before calling this function
2383 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002384static int iwl4965_scan_cancel(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002385{
2386 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2387 clear_bit(STATUS_SCANNING, &priv->status);
2388 return 0;
2389 }
2390
2391 if (test_bit(STATUS_SCANNING, &priv->status)) {
2392 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2393 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2394 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2395 queue_work(priv->workqueue, &priv->abort_scan);
2396
2397 } else
2398 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2399
2400 return test_bit(STATUS_SCANNING, &priv->status);
2401 }
2402
2403 return 0;
2404}
2405
2406/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002407 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002408 * @ms: amount of time to wait (in milliseconds) for scan to abort
2409 *
2410 * NOTE: priv->mutex must be held before calling this function
2411 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002412static int iwl4965_scan_cancel_timeout(struct iwl4965_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002413{
2414 unsigned long now = jiffies;
2415 int ret;
2416
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002417 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002418 if (ret && ms) {
2419 mutex_unlock(&priv->mutex);
2420 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2421 test_bit(STATUS_SCANNING, &priv->status))
2422 msleep(1);
2423 mutex_lock(&priv->mutex);
2424
2425 return test_bit(STATUS_SCANNING, &priv->status);
2426 }
2427
2428 return ret;
2429}
2430
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002431static void iwl4965_sequence_reset(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002432{
2433 /* Reset ieee stats */
2434
2435 /* We don't reset the net_device_stats (ieee->stats) on
2436 * re-association */
2437
2438 priv->last_seq_num = -1;
2439 priv->last_frag_num = -1;
2440 priv->last_packet_time = 0;
2441
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002442 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002443}
2444
2445#define MAX_UCODE_BEACON_INTERVAL 4096
2446#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2447
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002448static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002449{
2450 u16 new_val = 0;
2451 u16 beacon_factor = 0;
2452
2453 beacon_factor =
2454 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2455 / MAX_UCODE_BEACON_INTERVAL;
2456 new_val = beacon_val / beacon_factor;
2457
2458 return cpu_to_le16(new_val);
2459}
2460
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002461static void iwl4965_setup_rxon_timing(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002462{
2463 u64 interval_tm_unit;
2464 u64 tsf, result;
2465 unsigned long flags;
2466 struct ieee80211_conf *conf = NULL;
2467 u16 beacon_int = 0;
2468
2469 conf = ieee80211_get_hw_conf(priv->hw);
2470
2471 spin_lock_irqsave(&priv->lock, flags);
2472 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2473 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2474
2475 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2476
2477 tsf = priv->timestamp1;
2478 tsf = ((tsf << 32) | priv->timestamp0);
2479
2480 beacon_int = priv->beacon_int;
2481 spin_unlock_irqrestore(&priv->lock, flags);
2482
2483 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2484 if (beacon_int == 0) {
2485 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2486 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2487 } else {
2488 priv->rxon_timing.beacon_interval =
2489 cpu_to_le16(beacon_int);
2490 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002491 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002492 le16_to_cpu(priv->rxon_timing.beacon_interval));
2493 }
2494
2495 priv->rxon_timing.atim_window = 0;
2496 } else {
2497 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002498 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002499 /* TODO: we need to get atim_window from upper stack
2500 * for now we set to 0 */
2501 priv->rxon_timing.atim_window = 0;
2502 }
2503
2504 interval_tm_unit =
2505 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2506 result = do_div(tsf, interval_tm_unit);
2507 priv->rxon_timing.beacon_init_val =
2508 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2509
2510 IWL_DEBUG_ASSOC
2511 ("beacon interval %d beacon timer %d beacon tim %d\n",
2512 le16_to_cpu(priv->rxon_timing.beacon_interval),
2513 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2514 le16_to_cpu(priv->rxon_timing.atim_window));
2515}
2516
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002517static int iwl4965_scan_initiate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002518{
2519 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2520 IWL_ERROR("APs don't scan.\n");
2521 return 0;
2522 }
2523
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002524 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002525 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2526 return -EIO;
2527 }
2528
2529 if (test_bit(STATUS_SCANNING, &priv->status)) {
2530 IWL_DEBUG_SCAN("Scan already in progress.\n");
2531 return -EAGAIN;
2532 }
2533
2534 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2535 IWL_DEBUG_SCAN("Scan request while abort pending. "
2536 "Queuing.\n");
2537 return -EAGAIN;
2538 }
2539
2540 IWL_DEBUG_INFO("Starting scan...\n");
2541 priv->scan_bands = 2;
2542 set_bit(STATUS_SCANNING, &priv->status);
2543 priv->scan_start = jiffies;
2544 priv->scan_pass_start = priv->scan_start;
2545
2546 queue_work(priv->workqueue, &priv->request_scan);
2547
2548 return 0;
2549}
2550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002551static int iwl4965_set_rxon_hwcrypto(struct iwl4965_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002552{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002553 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002554
2555 if (hw_decrypt)
2556 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2557 else
2558 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2559
2560 return 0;
2561}
2562
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002563static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode)
Zhu Yib481de92007-09-25 17:54:57 -07002564{
2565 if (phymode == MODE_IEEE80211A) {
2566 priv->staging_rxon.flags &=
2567 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2568 | RXON_FLG_CCK_MSK);
2569 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2570 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002571 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002572 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2573 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2574 else
2575 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2576
2577 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2578 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2579
2580 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2581 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2582 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2583 }
2584}
2585
2586/*
Ian Schram01ebd062007-10-25 17:15:22 +08002587 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002588 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002589static void iwl4965_connection_init_rx_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002590{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002591 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002592
2593 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2594
2595 switch (priv->iw_mode) {
2596 case IEEE80211_IF_TYPE_AP:
2597 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2598 break;
2599
2600 case IEEE80211_IF_TYPE_STA:
2601 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2602 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2603 break;
2604
2605 case IEEE80211_IF_TYPE_IBSS:
2606 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2607 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2608 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2609 RXON_FILTER_ACCEPT_GRP_MSK;
2610 break;
2611
2612 case IEEE80211_IF_TYPE_MNTR:
2613 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2614 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2615 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2616 break;
2617 }
2618
2619#if 0
2620 /* TODO: Figure out when short_preamble would be set and cache from
2621 * that */
2622 if (!hw_to_local(priv->hw)->short_preamble)
2623 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2624 else
2625 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2626#endif
2627
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002628 ch_info = iwl4965_get_channel_info(priv, priv->phymode,
Zhu Yib481de92007-09-25 17:54:57 -07002629 le16_to_cpu(priv->staging_rxon.channel));
2630
2631 if (!ch_info)
2632 ch_info = &priv->channel_info[0];
2633
2634 /*
2635 * in some case A channels are all non IBSS
2636 * in this case force B/G channel
2637 */
2638 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2639 !(is_channel_ibss(ch_info)))
2640 ch_info = &priv->channel_info[0];
2641
2642 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2643 if (is_channel_a_band(ch_info))
2644 priv->phymode = MODE_IEEE80211A;
2645 else
2646 priv->phymode = MODE_IEEE80211G;
2647
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002648 iwl4965_set_flags_for_phymode(priv, priv->phymode);
Zhu Yib481de92007-09-25 17:54:57 -07002649
2650 priv->staging_rxon.ofdm_basic_rates =
2651 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2652 priv->staging_rxon.cck_basic_rates =
2653 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2654
2655 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2656 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2657 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2658 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2659 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2660 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2661 iwl4965_set_rxon_chain(priv);
2662}
2663
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002664static int iwl4965_set_mode(struct iwl4965_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002665{
Zhu Yib481de92007-09-25 17:54:57 -07002666 if (mode == IEEE80211_IF_TYPE_IBSS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002667 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002668
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002669 ch_info = iwl4965_get_channel_info(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002670 priv->phymode,
2671 le16_to_cpu(priv->staging_rxon.channel));
2672
2673 if (!ch_info || !is_channel_ibss(ch_info)) {
2674 IWL_ERROR("channel %d not IBSS channel\n",
2675 le16_to_cpu(priv->staging_rxon.channel));
2676 return -EINVAL;
2677 }
2678 }
2679
Zhu Yib481de92007-09-25 17:54:57 -07002680 priv->iw_mode = mode;
2681
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002682 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002683 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2684
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002685 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002686
Mohamed Abbasfde35712007-11-29 11:10:15 +08002687 /* dont commit rxon if rf-kill is on*/
2688 if (!iwl4965_is_ready_rf(priv))
2689 return -EAGAIN;
2690
2691 cancel_delayed_work(&priv->scan_check);
2692 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2693 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2694 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2695 return -EAGAIN;
2696 }
2697
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002698 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002699
2700 return 0;
2701}
2702
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002703static void iwl4965_build_tx_cmd_hwcrypto(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002704 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002705 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002706 struct sk_buff *skb_frag,
2707 int last_frag)
2708{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002709 struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002710
2711 switch (keyinfo->alg) {
2712 case ALG_CCMP:
2713 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2714 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2715 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2716 break;
2717
2718 case ALG_TKIP:
2719#if 0
2720 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2721
2722 if (last_frag)
2723 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2724 8);
2725 else
2726 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2727#endif
2728 break;
2729
2730 case ALG_WEP:
2731 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2732 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2733
2734 if (keyinfo->keylen == 13)
2735 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2736
2737 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2738
2739 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2740 "with key %d\n", ctl->key_idx);
2741 break;
2742
Zhu Yib481de92007-09-25 17:54:57 -07002743 default:
2744 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2745 break;
2746 }
2747}
2748
2749/*
2750 * handle build REPLY_TX command notification.
2751 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002752static void iwl4965_build_tx_cmd_basic(struct iwl4965_priv *priv,
2753 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002754 struct ieee80211_tx_control *ctrl,
2755 struct ieee80211_hdr *hdr,
2756 int is_unicast, u8 std_id)
2757{
2758 __le16 *qc;
2759 u16 fc = le16_to_cpu(hdr->frame_control);
2760 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2761
2762 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2763 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2764 tx_flags |= TX_CMD_FLG_ACK_MSK;
2765 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2766 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2767 if (ieee80211_is_probe_response(fc) &&
2768 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2769 tx_flags |= TX_CMD_FLG_TSF_MSK;
2770 } else {
2771 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2772 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2773 }
2774
2775 cmd->cmd.tx.sta_id = std_id;
2776 if (ieee80211_get_morefrag(hdr))
2777 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2778
2779 qc = ieee80211_get_qos_ctrl(hdr);
2780 if (qc) {
2781 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2782 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2783 } else
2784 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2785
2786 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2787 tx_flags |= TX_CMD_FLG_RTS_MSK;
2788 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2789 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2790 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2791 tx_flags |= TX_CMD_FLG_CTS_MSK;
2792 }
2793
2794 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2795 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2796
2797 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2798 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2799 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2800 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002801 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002802 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002803 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002804 } else
2805 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2806
2807 cmd->cmd.tx.driver_txop = 0;
2808 cmd->cmd.tx.tx_flags = tx_flags;
2809 cmd->cmd.tx.next_frame_len = 0;
2810}
2811
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002812/**
2813 * iwl4965_get_sta_id - Find station's index within station table
2814 *
2815 * If new IBSS station, create new entry in station table
2816 */
Ben Cahill9fbab512007-11-29 11:09:47 +08002817static int iwl4965_get_sta_id(struct iwl4965_priv *priv,
2818 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002819{
2820 int sta_id;
2821 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002822 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002823
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002824 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002825 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2826 is_multicast_ether_addr(hdr->addr1))
2827 return priv->hw_setting.bcast_sta_id;
2828
2829 switch (priv->iw_mode) {
2830
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002831 /* If we are a client station in a BSS network, use the special
2832 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002833 case IEEE80211_IF_TYPE_STA:
2834 return IWL_AP_ID;
2835
2836 /* If we are an AP, then find the station, or use BCAST */
2837 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002838 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002839 if (sta_id != IWL_INVALID_STATION)
2840 return sta_id;
2841 return priv->hw_setting.bcast_sta_id;
2842
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002843 /* If this frame is going out to an IBSS network, find the station,
2844 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002845 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002846 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002847 if (sta_id != IWL_INVALID_STATION)
2848 return sta_id;
2849
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002850 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002851 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2852 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002853
2854 if (sta_id != IWL_INVALID_STATION)
2855 return sta_id;
2856
Joe Perches0795af52007-10-03 17:59:30 -07002857 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002858 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002859 print_mac(mac, hdr->addr1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002860 iwl4965_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002861 return priv->hw_setting.bcast_sta_id;
2862
2863 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002864 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002865 return priv->hw_setting.bcast_sta_id;
2866 }
2867}
2868
2869/*
2870 * start REPLY_TX command process
2871 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002872static int iwl4965_tx_skb(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002873 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2874{
2875 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002876 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002877 u32 *control_flags;
2878 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002879 struct iwl4965_tx_queue *txq = NULL;
2880 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002881 dma_addr_t phys_addr;
2882 dma_addr_t txcmd_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002883 struct iwl4965_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002884 u16 len, idx, len_org;
2885 u8 id, hdr_len, unicast;
2886 u8 sta_id;
2887 u16 seq_number = 0;
2888 u16 fc;
2889 __le16 *qc;
2890 u8 wait_write_ptr = 0;
2891 unsigned long flags;
2892 int rc;
2893
2894 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002895 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002896 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2897 goto drop_unlock;
2898 }
2899
2900 if (!priv->interface_id) {
2901 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2902 goto drop_unlock;
2903 }
2904
2905 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2906 IWL_ERROR("ERROR: No TX rate available.\n");
2907 goto drop_unlock;
2908 }
2909
2910 unicast = !is_multicast_ether_addr(hdr->addr1);
2911 id = 0;
2912
2913 fc = le16_to_cpu(hdr->frame_control);
2914
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002915#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002916 if (ieee80211_is_auth(fc))
2917 IWL_DEBUG_TX("Sending AUTH frame\n");
2918 else if (ieee80211_is_assoc_request(fc))
2919 IWL_DEBUG_TX("Sending ASSOC frame\n");
2920 else if (ieee80211_is_reassoc_request(fc))
2921 IWL_DEBUG_TX("Sending REASSOC frame\n");
2922#endif
2923
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002924 /* drop all data frame if we are not associated */
2925 if (!iwl4965_is_associated(priv) && !priv->assoc_id &&
Zhu Yib481de92007-09-25 17:54:57 -07002926 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002927 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002928 goto drop_unlock;
2929 }
2930
2931 spin_unlock_irqrestore(&priv->lock, flags);
2932
2933 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002934
2935 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002936 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002937 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002938 DECLARE_MAC_BUF(mac);
2939
2940 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2941 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002942 goto drop;
2943 }
2944
2945 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2946
2947 qc = ieee80211_get_qos_ctrl(hdr);
2948 if (qc) {
2949 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2950 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2951 IEEE80211_SCTL_SEQ;
2952 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2953 (hdr->seq_ctrl &
2954 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2955 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002956#ifdef CONFIG_IWL4965_HT
2957#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07002958 /* aggregation is on for this <sta,tid> */
2959 if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
2960 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002961#endif /* CONFIG_IWL4965_HT_AGG */
2962#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002963 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002964
2965 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002966 txq = &priv->txq[txq_id];
2967 q = &txq->q;
2968
2969 spin_lock_irqsave(&priv->lock, flags);
2970
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002971 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002972 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002973 memset(tfd, 0, sizeof(*tfd));
2974 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002975 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002976
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002977 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002978 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002979 txq->txb[q->write_ptr].skb[0] = skb;
2980 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002981 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002982
2983 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002984 out_cmd = &txq->cmd[idx];
2985 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2986 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002987
2988 /*
2989 * Set up the Tx-command (not MAC!) header.
2990 * Store the chosen Tx queue and TFD index within the sequence field;
2991 * after Tx, uCode's Tx response will return this value so driver can
2992 * locate the frame within the tx queue and do post-tx processing.
2993 */
Zhu Yib481de92007-09-25 17:54:57 -07002994 out_cmd->hdr.cmd = REPLY_TX;
2995 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002996 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002997
2998 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002999 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
3000
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003001 /*
3002 * Use the first empty entry in this queue's command buffer array
3003 * to contain the Tx command and MAC header concatenated together
3004 * (payload data will be in another buffer).
3005 * Size of this varies, due to varying MAC header length.
3006 * If end is not dword aligned, we'll have 2 extra bytes at the end
3007 * of the MAC header (device reads on dword boundaries).
3008 * We'll tell device about this padding later.
3009 */
Zhu Yib481de92007-09-25 17:54:57 -07003010 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003011 sizeof(struct iwl4965_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07003012
3013 len_org = len;
3014 len = (len + 3) & ~3;
3015
3016 if (len_org != len)
3017 len_org = 1;
3018 else
3019 len_org = 0;
3020
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003021 /* Physical address of this Tx command's header (not MAC header!),
3022 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003023 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
3024 offsetof(struct iwl4965_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003025
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003026 /* Add buffer containing Tx command and MAC(!) header to TFD's
3027 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003028 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07003029
3030 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003031 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003032
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003033 /* Set up TFD's 2nd entry to point directly to remainder of skb,
3034 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07003035 len = skb->len - hdr_len;
3036 if (len) {
3037 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
3038 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003039 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07003040 }
3041
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003042 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07003043 if (len_org)
3044 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
3045
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003046 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07003047 len = (u16)skb->len;
3048 out_cmd->cmd.tx.len = cpu_to_le16(len);
3049
3050 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003051 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07003052
3053 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003054 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003055
3056 iwl4965_tx_cmd(priv, out_cmd, sta_id, txcmd_phys,
3057 hdr, hdr_len, ctl, NULL);
3058
3059 if (!ieee80211_get_morefrag(hdr)) {
3060 txq->need_update = 1;
3061 if (qc) {
3062 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
3063 priv->stations[sta_id].tid[tid].seq_number = seq_number;
3064 }
3065 } else {
3066 wait_write_ptr = 1;
3067 txq->need_update = 0;
3068 }
3069
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003070 iwl4965_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07003071 sizeof(out_cmd->cmd.tx));
3072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003073 iwl4965_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07003074 ieee80211_get_hdrlen(fc));
3075
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003076 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07003077 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
3078
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003079 /* Tell device the write index *just past* this latest filled TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003080 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
3081 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003082 spin_unlock_irqrestore(&priv->lock, flags);
3083
3084 if (rc)
3085 return rc;
3086
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003087 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07003088 && priv->mac80211_registered) {
3089 if (wait_write_ptr) {
3090 spin_lock_irqsave(&priv->lock, flags);
3091 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003092 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003093 spin_unlock_irqrestore(&priv->lock, flags);
3094 }
3095
3096 ieee80211_stop_queue(priv->hw, ctl->queue);
3097 }
3098
3099 return 0;
3100
3101drop_unlock:
3102 spin_unlock_irqrestore(&priv->lock, flags);
3103drop:
3104 return -1;
3105}
3106
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003107static void iwl4965_set_rate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003108{
3109 const struct ieee80211_hw_mode *hw = NULL;
3110 struct ieee80211_rate *rate;
3111 int i;
3112
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003113 hw = iwl4965_get_hw_mode(priv, priv->phymode);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08003114 if (!hw) {
3115 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
3116 return;
3117 }
Zhu Yib481de92007-09-25 17:54:57 -07003118
3119 priv->active_rate = 0;
3120 priv->active_rate_basic = 0;
3121
3122 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
3123 hw->mode == MODE_IEEE80211A ?
3124 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
3125
3126 for (i = 0; i < hw->num_rates; i++) {
3127 rate = &(hw->rates[i]);
3128 if ((rate->val < IWL_RATE_COUNT) &&
3129 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
3130 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003131 rate->val, iwl4965_rates[rate->val].plcp,
Zhu Yib481de92007-09-25 17:54:57 -07003132 (rate->flags & IEEE80211_RATE_BASIC) ?
3133 "*" : "");
3134 priv->active_rate |= (1 << rate->val);
3135 if (rate->flags & IEEE80211_RATE_BASIC)
3136 priv->active_rate_basic |= (1 << rate->val);
3137 } else
3138 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003139 rate->val, iwl4965_rates[rate->val].plcp);
Zhu Yib481de92007-09-25 17:54:57 -07003140 }
3141
3142 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
3143 priv->active_rate, priv->active_rate_basic);
3144
3145 /*
3146 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
3147 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
3148 * OFDM
3149 */
3150 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
3151 priv->staging_rxon.cck_basic_rates =
3152 ((priv->active_rate_basic &
3153 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
3154 else
3155 priv->staging_rxon.cck_basic_rates =
3156 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
3157
3158 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
3159 priv->staging_rxon.ofdm_basic_rates =
3160 ((priv->active_rate_basic &
3161 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
3162 IWL_FIRST_OFDM_RATE) & 0xFF;
3163 else
3164 priv->staging_rxon.ofdm_basic_rates =
3165 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
3166}
3167
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003168static void iwl4965_radio_kill_sw(struct iwl4965_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07003169{
3170 unsigned long flags;
3171
3172 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
3173 return;
3174
3175 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
3176 disable_radio ? "OFF" : "ON");
3177
3178 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003179 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003180 /* FIXME: This is a workaround for AP */
3181 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3182 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003183 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003184 CSR_UCODE_SW_BIT_RFKILL);
3185 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003186 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003187 set_bit(STATUS_RF_KILL_SW, &priv->status);
3188 }
3189 return;
3190 }
3191
3192 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003193 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07003194
3195 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3196 spin_unlock_irqrestore(&priv->lock, flags);
3197
3198 /* wake up ucode */
3199 msleep(10);
3200
3201 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003202 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3203 if (!iwl4965_grab_nic_access(priv))
3204 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003205 spin_unlock_irqrestore(&priv->lock, flags);
3206
3207 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3208 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3209 "disabled by HW switch\n");
3210 return;
3211 }
3212
3213 queue_work(priv->workqueue, &priv->restart);
3214 return;
3215}
3216
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003217void iwl4965_set_decrypted_flag(struct iwl4965_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07003218 u32 decrypt_res, struct ieee80211_rx_status *stats)
3219{
3220 u16 fc =
3221 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3222
3223 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3224 return;
3225
3226 if (!(fc & IEEE80211_FCTL_PROTECTED))
3227 return;
3228
3229 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3230 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3231 case RX_RES_STATUS_SEC_TYPE_TKIP:
3232 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3233 RX_RES_STATUS_BAD_ICV_MIC)
3234 stats->flag |= RX_FLAG_MMIC_ERROR;
3235 case RX_RES_STATUS_SEC_TYPE_WEP:
3236 case RX_RES_STATUS_SEC_TYPE_CCMP:
3237 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3238 RX_RES_STATUS_DECRYPT_OK) {
3239 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3240 stats->flag |= RX_FLAG_DECRYPTED;
3241 }
3242 break;
3243
3244 default:
3245 break;
3246 }
3247}
3248
Zhu Yib481de92007-09-25 17:54:57 -07003249
3250#define IWL_PACKET_RETRY_TIME HZ
3251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003252int iwl4965_is_duplicate_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07003253{
3254 u16 sc = le16_to_cpu(header->seq_ctrl);
3255 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3256 u16 frag = sc & IEEE80211_SCTL_FRAG;
3257 u16 *last_seq, *last_frag;
3258 unsigned long *last_time;
3259
3260 switch (priv->iw_mode) {
3261 case IEEE80211_IF_TYPE_IBSS:{
3262 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003263 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003264 u8 *mac = header->addr2;
3265 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3266
3267 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003268 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07003269 if (!compare_ether_addr(entry->mac, mac))
3270 break;
3271 }
3272 if (p == &priv->ibss_mac_hash[index]) {
3273 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3274 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08003275 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07003276 return 0;
3277 }
3278 memcpy(entry->mac, mac, ETH_ALEN);
3279 entry->seq_num = seq;
3280 entry->frag_num = frag;
3281 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08003282 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07003283 return 0;
3284 }
3285 last_seq = &entry->seq_num;
3286 last_frag = &entry->frag_num;
3287 last_time = &entry->packet_time;
3288 break;
3289 }
3290 case IEEE80211_IF_TYPE_STA:
3291 last_seq = &priv->last_seq_num;
3292 last_frag = &priv->last_frag_num;
3293 last_time = &priv->last_packet_time;
3294 break;
3295 default:
3296 return 0;
3297 }
3298 if ((*last_seq == seq) &&
3299 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3300 if (*last_frag == frag)
3301 goto drop;
3302 if (*last_frag + 1 != frag)
3303 /* out-of-order fragment */
3304 goto drop;
3305 } else
3306 *last_seq = seq;
3307
3308 *last_frag = frag;
3309 *last_time = jiffies;
3310 return 0;
3311
3312 drop:
3313 return 1;
3314}
3315
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003316#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003317
3318#include "iwl-spectrum.h"
3319
3320#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3321#define BEACON_TIME_MASK_HIGH 0xFF000000
3322#define TIME_UNIT 1024
3323
3324/*
3325 * extended beacon time format
3326 * time in usec will be changed into a 32-bit value in 8:24 format
3327 * the high 1 byte is the beacon counts
3328 * the lower 3 bytes is the time in usec within one beacon interval
3329 */
3330
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003331static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003332{
3333 u32 quot;
3334 u32 rem;
3335 u32 interval = beacon_interval * 1024;
3336
3337 if (!interval || !usec)
3338 return 0;
3339
3340 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3341 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3342
3343 return (quot << 24) + rem;
3344}
3345
3346/* base is usually what we get from ucode with each received frame,
3347 * the same as HW timer counter counting down
3348 */
3349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003350static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003351{
3352 u32 base_low = base & BEACON_TIME_MASK_LOW;
3353 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3354 u32 interval = beacon_interval * TIME_UNIT;
3355 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3356 (addon & BEACON_TIME_MASK_HIGH);
3357
3358 if (base_low > addon_low)
3359 res += base_low - addon_low;
3360 else if (base_low < addon_low) {
3361 res += interval + base_low - addon_low;
3362 res += (1 << 24);
3363 } else
3364 res += (1 << 24);
3365
3366 return cpu_to_le32(res);
3367}
3368
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003369static int iwl4965_get_measurement(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003370 struct ieee80211_measurement_params *params,
3371 u8 type)
3372{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003373 struct iwl4965_spectrum_cmd spectrum;
3374 struct iwl4965_rx_packet *res;
3375 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003376 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3377 .data = (void *)&spectrum,
3378 .meta.flags = CMD_WANT_SKB,
3379 };
3380 u32 add_time = le64_to_cpu(params->start_time);
3381 int rc;
3382 int spectrum_resp_status;
3383 int duration = le16_to_cpu(params->duration);
3384
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003385 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003386 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003387 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003388 le64_to_cpu(params->start_time) - priv->last_tsf,
3389 le16_to_cpu(priv->rxon_timing.beacon_interval));
3390
3391 memset(&spectrum, 0, sizeof(spectrum));
3392
3393 spectrum.channel_count = cpu_to_le16(1);
3394 spectrum.flags =
3395 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3396 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3397 cmd.len = sizeof(spectrum);
3398 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3399
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003400 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003401 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003402 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003403 add_time,
3404 le16_to_cpu(priv->rxon_timing.beacon_interval));
3405 else
3406 spectrum.start_time = 0;
3407
3408 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3409 spectrum.channels[0].channel = params->channel;
3410 spectrum.channels[0].type = type;
3411 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3412 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3413 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3414
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003415 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003416 if (rc)
3417 return rc;
3418
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003419 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003420 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3421 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3422 rc = -EIO;
3423 }
3424
3425 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3426 switch (spectrum_resp_status) {
3427 case 0: /* Command will be handled */
3428 if (res->u.spectrum.id != 0xff) {
3429 IWL_DEBUG_INFO
3430 ("Replaced existing measurement: %d\n",
3431 res->u.spectrum.id);
3432 priv->measurement_status &= ~MEASUREMENT_READY;
3433 }
3434 priv->measurement_status |= MEASUREMENT_ACTIVE;
3435 rc = 0;
3436 break;
3437
3438 case 1: /* Command will not be handled */
3439 rc = -EAGAIN;
3440 break;
3441 }
3442
3443 dev_kfree_skb_any(cmd.meta.u.skb);
3444
3445 return rc;
3446}
3447#endif
3448
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003449static void iwl4965_txstatus_to_ieee(struct iwl4965_priv *priv,
3450 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003451{
3452
3453 tx_sta->status.ack_signal = 0;
3454 tx_sta->status.excessive_retries = 0;
3455 tx_sta->status.queue_length = 0;
3456 tx_sta->status.queue_number = 0;
3457
3458 if (in_interrupt())
3459 ieee80211_tx_status_irqsafe(priv->hw,
3460 tx_sta->skb[0], &(tx_sta->status));
3461 else
3462 ieee80211_tx_status(priv->hw,
3463 tx_sta->skb[0], &(tx_sta->status));
3464
3465 tx_sta->skb[0] = NULL;
3466}
3467
3468/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003469 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07003470 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003471 * When FW advances 'R' index, all entries between old and new 'R' index
3472 * need to be reclaimed. As result, some free space forms. If there is
3473 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07003474 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003475int iwl4965_tx_queue_reclaim(struct iwl4965_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003476{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003477 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3478 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003479 int nfreed = 0;
3480
3481 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3482 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3483 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003484 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003485 return 0;
3486 }
3487
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003488 for (index = iwl4965_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003489 q->read_ptr != index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003490 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003491 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003492 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003493 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003494 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003495 } else if (nfreed > 1) {
3496 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003497 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003498 queue_work(priv->workqueue, &priv->restart);
3499 }
3500 nfreed++;
3501 }
3502
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003503 if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003504 (txq_id != IWL_CMD_QUEUE_NUM) &&
3505 priv->mac80211_registered)
3506 ieee80211_wake_queue(priv->hw, txq_id);
3507
3508
3509 return nfreed;
3510}
3511
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003512static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003513{
3514 status &= TX_STATUS_MSK;
3515 return (status == TX_STATUS_SUCCESS)
3516 || (status == TX_STATUS_DIRECT_DONE);
3517}
3518
3519/******************************************************************************
3520 *
3521 * Generic RX handler implementations
3522 *
3523 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003524#ifdef CONFIG_IWL4965_HT
3525#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003526
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003527static inline int iwl4965_get_ra_sta_id(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003528 struct ieee80211_hdr *hdr)
3529{
3530 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3531 return IWL_AP_ID;
3532 else {
3533 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003534 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07003535 }
3536}
3537
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003538static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
3539 struct iwl4965_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07003540{
3541 if (priv->txq[txq_id].txb[idx].skb[0])
3542 return (struct ieee80211_hdr *)priv->txq[txq_id].
3543 txb[idx].skb[0]->data;
3544 return NULL;
3545}
3546
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003547static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003548{
3549 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3550 tx_resp->frame_count);
3551 return le32_to_cpu(*scd_ssn) & MAX_SN;
3552
3553}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003554
3555/**
3556 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3557 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003558static int iwl4965_tx_status_reply_tx(struct iwl4965_priv *priv,
3559 struct iwl4965_ht_agg *agg,
3560 struct iwl4965_tx_resp *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003561 u16 start_idx)
3562{
3563 u32 status;
3564 __le32 *frame_status = &tx_resp->status;
3565 struct ieee80211_tx_status *tx_status = NULL;
3566 struct ieee80211_hdr *hdr = NULL;
3567 int i, sh;
3568 int txq_id, idx;
3569 u16 seq;
3570
3571 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003572 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003573
3574 agg->frame_count = tx_resp->frame_count;
3575 agg->start_idx = start_idx;
3576 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3577 agg->bitmap0 = agg->bitmap1 = 0;
3578
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003579 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003580 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003581 /* Only one frame was attempted; no block-ack will arrive */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003582 struct iwl4965_tx_queue *txq ;
Zhu Yib481de92007-09-25 17:54:57 -07003583 status = le32_to_cpu(frame_status[0]);
3584
3585 txq_id = agg->txq_id;
3586 txq = &priv->txq[txq_id];
3587 /* FIXME: code repetition */
3588 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d \n",
3589 agg->frame_count, agg->start_idx);
3590
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003591 tx_status = &(priv->txq[txq_id].txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003592 tx_status->retry_count = tx_resp->failure_frame;
3593 tx_status->queue_number = status & 0xff;
3594 tx_status->queue_length = tx_resp->bt_kill_count;
3595 tx_status->queue_length |= tx_resp->failure_rts;
3596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003597 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003598 IEEE80211_TX_STATUS_ACK : 0;
3599 tx_status->control.tx_rate =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003600 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003601 /* FIXME: code repetition end */
3602
3603 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3604 status & 0xff, tx_resp->failure_frame);
3605 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003606 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003607
3608 agg->wait_for_ba = 0;
3609 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003610 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003611 u64 bitmap = 0;
3612 int start = agg->start_idx;
3613
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003614 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003615 for (i = 0; i < agg->frame_count; i++) {
3616 u16 sc;
3617 status = le32_to_cpu(frame_status[i]);
3618 seq = status >> 16;
3619 idx = SEQ_TO_INDEX(seq);
3620 txq_id = SEQ_TO_QUEUE(seq);
3621
3622 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3623 AGG_TX_STATE_ABORT_MSK))
3624 continue;
3625
3626 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3627 agg->frame_count, txq_id, idx);
3628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003629 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003630
3631 sc = le16_to_cpu(hdr->seq_ctrl);
3632 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3633 IWL_ERROR("BUG_ON idx doesn't match seq control"
3634 " idx=%d, seq_idx=%d, seq=%d\n",
3635 idx, SEQ_TO_SN(sc),
3636 hdr->seq_ctrl);
3637 return -1;
3638 }
3639
3640 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3641 i, idx, SEQ_TO_SN(sc));
3642
3643 sh = idx - start;
3644 if (sh > 64) {
3645 sh = (start - idx) + 0xff;
3646 bitmap = bitmap << sh;
3647 sh = 0;
3648 start = idx;
3649 } else if (sh < -64)
3650 sh = 0xff - (start - idx);
3651 else if (sh < 0) {
3652 sh = start - idx;
3653 start = idx;
3654 bitmap = bitmap << sh;
3655 sh = 0;
3656 }
3657 bitmap |= (1 << sh);
3658 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3659 start, (u32)(bitmap & 0xFFFFFFFF));
3660 }
3661
3662 agg->bitmap0 = bitmap & 0xFFFFFFFF;
3663 agg->bitmap1 = bitmap >> 32;
3664 agg->start_idx = start;
3665 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3666 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%x\n",
3667 agg->frame_count, agg->start_idx,
3668 agg->bitmap0);
3669
3670 if (bitmap)
3671 agg->wait_for_ba = 1;
3672 }
3673 return 0;
3674}
3675#endif
3676#endif
3677
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003678/**
3679 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3680 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003681static void iwl4965_rx_reply_tx(struct iwl4965_priv *priv,
3682 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003683{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003684 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003685 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3686 int txq_id = SEQ_TO_QUEUE(sequence);
3687 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003688 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003689 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003690 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003691 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003692#ifdef CONFIG_IWL4965_HT
3693#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003694 int tid, sta_id;
3695#endif
3696#endif
3697
3698 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3699 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3700 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003701 index, txq->q.n_bd, txq->q.write_ptr,
3702 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003703 return;
3704 }
3705
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003706#ifdef CONFIG_IWL4965_HT
3707#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003708 if (txq->sched_retry) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003709 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
Zhu Yib481de92007-09-25 17:54:57 -07003710 struct ieee80211_hdr *hdr =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003711 iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3712 struct iwl4965_ht_agg *agg = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003713 __le16 *qc = ieee80211_get_qos_ctrl(hdr);
3714
3715 if (qc == NULL) {
3716 IWL_ERROR("BUG_ON qc is null!!!!\n");
3717 return;
3718 }
3719
3720 tid = le16_to_cpu(*qc) & 0xf;
3721
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003722 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003723 if (unlikely(sta_id == IWL_INVALID_STATION)) {
3724 IWL_ERROR("Station not known for\n");
3725 return;
3726 }
3727
3728 agg = &priv->stations[sta_id].tid[tid].agg;
3729
3730 iwl4965_tx_status_reply_tx(priv, agg, tx_resp, index);
3731
3732 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003733 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003734 /* TODO: send BAR */
3735 }
3736
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003737 if ((txq->q.read_ptr != (scd_ssn & 0xff))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003738 index = iwl4965_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003739 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3740 "%d index %d\n", scd_ssn , index);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003741 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003742 }
3743 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003744#endif /* CONFIG_IWL4965_HT_AGG */
3745#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003746 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003747
3748 tx_status->retry_count = tx_resp->failure_frame;
3749 tx_status->queue_number = status;
3750 tx_status->queue_length = tx_resp->bt_kill_count;
3751 tx_status->queue_length |= tx_resp->failure_rts;
3752
3753 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003754 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Zhu Yib481de92007-09-25 17:54:57 -07003755
3756 tx_status->control.tx_rate =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003757 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003758
3759 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003760 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003761 status, le32_to_cpu(tx_resp->rate_n_flags),
3762 tx_resp->failure_frame);
3763
3764 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3765 if (index != -1)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003766 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003767#ifdef CONFIG_IWL4965_HT
3768#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003769 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003770#endif /* CONFIG_IWL4965_HT_AGG */
3771#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003772
3773 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3774 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3775}
3776
3777
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003778static void iwl4965_rx_reply_alive(struct iwl4965_priv *priv,
3779 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003780{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003781 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3782 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003783 struct delayed_work *pwork;
3784
3785 palive = &pkt->u.alive_frame;
3786
3787 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3788 "0x%01X 0x%01X\n",
3789 palive->is_valid, palive->ver_type,
3790 palive->ver_subtype);
3791
3792 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3793 IWL_DEBUG_INFO("Initialization Alive received.\n");
3794 memcpy(&priv->card_alive_init,
3795 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003796 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003797 pwork = &priv->init_alive_start;
3798 } else {
3799 IWL_DEBUG_INFO("Runtime Alive received.\n");
3800 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003801 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003802 pwork = &priv->alive_start;
3803 }
3804
3805 /* We delay the ALIVE response by 5ms to
3806 * give the HW RF Kill time to activate... */
3807 if (palive->is_valid == UCODE_VALID_OK)
3808 queue_delayed_work(priv->workqueue, pwork,
3809 msecs_to_jiffies(5));
3810 else
3811 IWL_WARNING("uCode did not respond OK.\n");
3812}
3813
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003814static void iwl4965_rx_reply_add_sta(struct iwl4965_priv *priv,
3815 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003816{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003817 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003818
3819 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3820 return;
3821}
3822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003823static void iwl4965_rx_reply_error(struct iwl4965_priv *priv,
3824 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003825{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003826 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003827
3828 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3829 "seq 0x%04X ser 0x%08X\n",
3830 le32_to_cpu(pkt->u.err_resp.error_type),
3831 get_cmd_string(pkt->u.err_resp.cmd_id),
3832 pkt->u.err_resp.cmd_id,
3833 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3834 le32_to_cpu(pkt->u.err_resp.error_info));
3835}
3836
3837#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3838
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003839static void iwl4965_rx_csa(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003840{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003841 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3842 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3843 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003844 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3845 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3846 rxon->channel = csa->channel;
3847 priv->staging_rxon.channel = csa->channel;
3848}
3849
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003850static void iwl4965_rx_spectrum_measure_notif(struct iwl4965_priv *priv,
3851 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003852{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003853#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003854 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3855 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003856
3857 if (!report->state) {
3858 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3859 "Spectrum Measure Notification: Start\n");
3860 return;
3861 }
3862
3863 memcpy(&priv->measure_report, report, sizeof(*report));
3864 priv->measurement_status |= MEASUREMENT_READY;
3865#endif
3866}
3867
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003868static void iwl4965_rx_pm_sleep_notif(struct iwl4965_priv *priv,
3869 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003870{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003871#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003872 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3873 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003874 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3875 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3876#endif
3877}
3878
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003879static void iwl4965_rx_pm_debug_statistics_notif(struct iwl4965_priv *priv,
3880 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003881{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003882 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003883 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3884 "notification for %s:\n",
3885 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003886 iwl4965_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003887}
3888
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003889static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003890{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003891 struct iwl4965_priv *priv =
3892 container_of(work, struct iwl4965_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003893 struct sk_buff *beacon;
3894
3895 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3896 beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
3897
3898 if (!beacon) {
3899 IWL_ERROR("update beacon failed\n");
3900 return;
3901 }
3902
3903 mutex_lock(&priv->mutex);
3904 /* new beacon skb is allocated every time; dispose previous.*/
3905 if (priv->ibss_beacon)
3906 dev_kfree_skb(priv->ibss_beacon);
3907
3908 priv->ibss_beacon = beacon;
3909 mutex_unlock(&priv->mutex);
3910
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003911 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003912}
3913
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003914static void iwl4965_rx_beacon_notif(struct iwl4965_priv *priv,
3915 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003916{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003917#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003918 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3919 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3920 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003921
3922 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3923 "tsf %d %d rate %d\n",
3924 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3925 beacon->beacon_notify_hdr.failure_frame,
3926 le32_to_cpu(beacon->ibss_mgr_status),
3927 le32_to_cpu(beacon->high_tsf),
3928 le32_to_cpu(beacon->low_tsf), rate);
3929#endif
3930
3931 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3932 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3933 queue_work(priv->workqueue, &priv->beacon_update);
3934}
3935
3936/* Service response to REPLY_SCAN_CMD (0x80) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003937static void iwl4965_rx_reply_scan(struct iwl4965_priv *priv,
3938 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003939{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003940#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003941 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3942 struct iwl4965_scanreq_notification *notif =
3943 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003944
3945 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3946#endif
3947}
3948
3949/* Service SCAN_START_NOTIFICATION (0x82) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003950static void iwl4965_rx_scan_start_notif(struct iwl4965_priv *priv,
3951 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003952{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003953 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3954 struct iwl4965_scanstart_notification *notif =
3955 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003956 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3957 IWL_DEBUG_SCAN("Scan start: "
3958 "%d [802.11%s] "
3959 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3960 notif->channel,
3961 notif->band ? "bg" : "a",
3962 notif->tsf_high,
3963 notif->tsf_low, notif->status, notif->beacon_timer);
3964}
3965
3966/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003967static void iwl4965_rx_scan_results_notif(struct iwl4965_priv *priv,
3968 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003969{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003970 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3971 struct iwl4965_scanresults_notification *notif =
3972 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003973
3974 IWL_DEBUG_SCAN("Scan ch.res: "
3975 "%d [802.11%s] "
3976 "(TSF: 0x%08X:%08X) - %d "
3977 "elapsed=%lu usec (%dms since last)\n",
3978 notif->channel,
3979 notif->band ? "bg" : "a",
3980 le32_to_cpu(notif->tsf_high),
3981 le32_to_cpu(notif->tsf_low),
3982 le32_to_cpu(notif->statistics[0]),
3983 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3984 jiffies_to_msecs(elapsed_jiffies
3985 (priv->last_scan_jiffies, jiffies)));
3986
3987 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003988 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003989}
3990
3991/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003992static void iwl4965_rx_scan_complete_notif(struct iwl4965_priv *priv,
3993 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003994{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003995 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3996 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003997
3998 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3999 scan_notif->scanned_channels,
4000 scan_notif->tsf_low,
4001 scan_notif->tsf_high, scan_notif->status);
4002
4003 /* The HW is no longer scanning */
4004 clear_bit(STATUS_SCAN_HW, &priv->status);
4005
4006 /* The scan completion notification came in, so kill that timer... */
4007 cancel_delayed_work(&priv->scan_check);
4008
4009 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
4010 (priv->scan_bands == 2) ? "2.4" : "5.2",
4011 jiffies_to_msecs(elapsed_jiffies
4012 (priv->scan_pass_start, jiffies)));
4013
4014 /* Remove this scanned band from the list
4015 * of pending bands to scan */
4016 priv->scan_bands--;
4017
4018 /* If a request to abort was given, or the scan did not succeed
4019 * then we reset the scan state machine and terminate,
4020 * re-queuing another scan if one has been requested */
4021 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4022 IWL_DEBUG_INFO("Aborted scan completed.\n");
4023 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
4024 } else {
4025 /* If there are more bands on this scan pass reschedule */
4026 if (priv->scan_bands > 0)
4027 goto reschedule;
4028 }
4029
4030 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004031 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004032 IWL_DEBUG_INFO("Setting scan to off\n");
4033
4034 clear_bit(STATUS_SCANNING, &priv->status);
4035
4036 IWL_DEBUG_INFO("Scan took %dms\n",
4037 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
4038
4039 queue_work(priv->workqueue, &priv->scan_completed);
4040
4041 return;
4042
4043reschedule:
4044 priv->scan_pass_start = jiffies;
4045 queue_work(priv->workqueue, &priv->request_scan);
4046}
4047
4048/* Handle notification from uCode that card's power state is changing
4049 * due to software, hardware, or critical temperature RFKILL */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004050static void iwl4965_rx_card_state_notif(struct iwl4965_priv *priv,
4051 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07004052{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004053 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004054 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
4055 unsigned long status = priv->status;
4056
4057 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
4058 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
4059 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
4060
4061 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
4062 RF_CARD_DISABLED)) {
4063
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004064 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07004065 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4066
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004067 if (!iwl4965_grab_nic_access(priv)) {
4068 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07004069 priv, HBUS_TARG_MBX_C,
4070 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4071
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004072 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004073 }
4074
4075 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004076 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07004077 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004078 if (!iwl4965_grab_nic_access(priv)) {
4079 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07004080 priv, HBUS_TARG_MBX_C,
4081 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4082
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004083 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004084 }
4085 }
4086
4087 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004088 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07004089 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004090 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
4091 if (!iwl4965_grab_nic_access(priv))
4092 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004093 }
4094 }
4095
4096 if (flags & HW_CARD_DISABLED)
4097 set_bit(STATUS_RF_KILL_HW, &priv->status);
4098 else
4099 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4100
4101
4102 if (flags & SW_CARD_DISABLED)
4103 set_bit(STATUS_RF_KILL_SW, &priv->status);
4104 else
4105 clear_bit(STATUS_RF_KILL_SW, &priv->status);
4106
4107 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004108 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004109
4110 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
4111 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
4112 (test_bit(STATUS_RF_KILL_SW, &status) !=
4113 test_bit(STATUS_RF_KILL_SW, &priv->status)))
4114 queue_work(priv->workqueue, &priv->rf_kill);
4115 else
4116 wake_up_interruptible(&priv->wait_command_queue);
4117}
4118
4119/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004120 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07004121 *
4122 * Setup the RX handlers for each of the reply types sent from the uCode
4123 * to the host.
4124 *
4125 * This function chains into the hardware specific files for them to setup
4126 * any hardware specific handlers as well.
4127 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004128static void iwl4965_setup_rx_handlers(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004129{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004130 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
4131 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
4132 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
4133 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07004134 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004135 iwl4965_rx_spectrum_measure_notif;
4136 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004137 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004138 iwl4965_rx_pm_debug_statistics_notif;
4139 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004140
Ben Cahill9fbab512007-11-29 11:09:47 +08004141 /*
4142 * The same handler is used for both the REPLY to a discrete
4143 * statistics request from the host as well as for the periodic
4144 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07004145 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004146 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
4147 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07004148
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004149 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
4150 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004151 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004152 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004153 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004154 iwl4965_rx_scan_complete_notif;
4155 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
4156 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07004157
Ben Cahill9fbab512007-11-29 11:09:47 +08004158 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004159 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004160}
4161
4162/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004163 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07004164 * @rxb: Rx buffer to reclaim
4165 *
4166 * If an Rx buffer has an async callback associated with it the callback
4167 * will be executed. The attached skb (if present) will only be freed
4168 * if the callback returns 1
4169 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004170static void iwl4965_tx_cmd_complete(struct iwl4965_priv *priv,
4171 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07004172{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004173 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004174 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
4175 int txq_id = SEQ_TO_QUEUE(sequence);
4176 int index = SEQ_TO_INDEX(sequence);
4177 int huge = sequence & SEQ_HUGE_FRAME;
4178 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004179 struct iwl4965_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07004180
4181 /* If a Tx command is being handled and it isn't in the actual
4182 * command queue then there a command routing bug has been introduced
4183 * in the queue management code. */
4184 if (txq_id != IWL_CMD_QUEUE_NUM)
4185 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
4186 txq_id, pkt->hdr.cmd);
4187 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
4188
4189 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
4190 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
4191
4192 /* Input error checking is done when commands are added to queue. */
4193 if (cmd->meta.flags & CMD_WANT_SKB) {
4194 cmd->meta.source->u.skb = rxb->skb;
4195 rxb->skb = NULL;
4196 } else if (cmd->meta.u.callback &&
4197 !cmd->meta.u.callback(priv, cmd, rxb->skb))
4198 rxb->skb = NULL;
4199
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004200 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07004201
4202 if (!(cmd->meta.flags & CMD_ASYNC)) {
4203 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4204 wake_up_interruptible(&priv->wait_command_queue);
4205 }
4206}
4207
4208/************************** RX-FUNCTIONS ****************************/
4209/*
4210 * Rx theory of operation
4211 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004212 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
4213 * each of which point to Receive Buffers to be filled by 4965. These get
4214 * used not only for Rx frames, but for any command response or notification
4215 * from the 4965. The driver and 4965 manage the Rx buffers by means
4216 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07004217 *
4218 * Rx Queue Indexes
4219 * The host/firmware share two index registers for managing the Rx buffers.
4220 *
4221 * The READ index maps to the first position that the firmware may be writing
4222 * to -- the driver can read up to (but not including) this position and get
4223 * good data.
4224 * The READ index is managed by the firmware once the card is enabled.
4225 *
4226 * The WRITE index maps to the last position the driver has read from -- the
4227 * position preceding WRITE is the last slot the firmware can place a packet.
4228 *
4229 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
4230 * WRITE = READ.
4231 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004232 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07004233 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4234 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004235 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07004236 * and fire the RX interrupt. The driver can then query the READ index and
4237 * process as many packets as possible, moving the WRITE index forward as it
4238 * resets the Rx queue buffers with new memory.
4239 *
4240 * The management in the driver is as follows:
4241 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
4242 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08004243 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004244 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07004245 * iwl->rxq is replenished and the READ INDEX is updated (updating the
4246 * 'processed' and 'read' driver indexes as well)
4247 * + A received packet is processed and handed to the kernel network stack,
4248 * detached from the iwl->rxq. The driver 'processed' index is updated.
4249 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
4250 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
4251 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
4252 * were enough free buffers and RX_STALLED is set it is cleared.
4253 *
4254 *
4255 * Driver sequence:
4256 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004257 * iwl4965_rx_queue_alloc() Allocates rx_free
4258 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004259 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08004260 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07004261 * queue, updates firmware pointers, and updates
4262 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004263 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07004264 *
4265 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08004266 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07004267 * READ INDEX, detaching the SKB from the pool.
4268 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004269 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07004270 * slots.
4271 * ...
4272 *
4273 */
4274
4275/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004276 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07004277 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004278static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004279{
4280 int s = q->read - q->write;
4281 if (s <= 0)
4282 s += RX_QUEUE_SIZE;
4283 /* keep some buffer to not confuse full and empty queue */
4284 s -= 2;
4285 if (s < 0)
4286 s = 0;
4287 return s;
4288}
4289
4290/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004291 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07004292 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004293int iwl4965_rx_queue_update_write_ptr(struct iwl4965_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004294{
4295 u32 reg = 0;
4296 int rc = 0;
4297 unsigned long flags;
4298
4299 spin_lock_irqsave(&q->lock, flags);
4300
4301 if (q->need_update == 0)
4302 goto exit_unlock;
4303
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004304 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07004305 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004306 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004307
4308 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004309 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004310 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4311 goto exit_unlock;
4312 }
4313
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004314 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004315 if (rc)
4316 goto exit_unlock;
4317
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004318 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004319 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07004320 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004321 iwl4965_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004322
4323 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07004324 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004325 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004326 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07004327
4328
4329 q->need_update = 0;
4330
4331 exit_unlock:
4332 spin_unlock_irqrestore(&q->lock, flags);
4333 return rc;
4334}
4335
4336/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004337 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07004338 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004339static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004340 dma_addr_t dma_addr)
4341{
4342 return cpu_to_le32((u32)(dma_addr >> 8));
4343}
4344
4345
4346/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004347 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07004348 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004349 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07004350 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08004351 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07004352 *
4353 * This moves the 'write' index forward to catch up with 'processed', and
4354 * also updates the memory address in the firmware to reference the new
4355 * target buffer.
4356 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004357static int iwl4965_rx_queue_restock(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004358{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004359 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004360 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004361 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004362 unsigned long flags;
4363 int write, rc;
4364
4365 spin_lock_irqsave(&rxq->lock, flags);
4366 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004367 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004368 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07004369 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004370 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004371 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004372
4373 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004374 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004375 rxq->queue[rxq->write] = rxb;
4376 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4377 rxq->free_count--;
4378 }
4379 spin_unlock_irqrestore(&rxq->lock, flags);
4380 /* If the pre-allocated buffer pool is dropping low, schedule to
4381 * refill it */
4382 if (rxq->free_count <= RX_LOW_WATERMARK)
4383 queue_work(priv->workqueue, &priv->rx_replenish);
4384
4385
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004386 /* If we've added more space for the firmware to place data, tell it.
4387 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07004388 if ((write != (rxq->write & ~0x7))
4389 || (abs(rxq->write - rxq->read) > 7)) {
4390 spin_lock_irqsave(&rxq->lock, flags);
4391 rxq->need_update = 1;
4392 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004393 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004394 if (rc)
4395 return rc;
4396 }
4397
4398 return 0;
4399}
4400
4401/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004402 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004403 *
4404 * When moving to rx_free an SKB is allocated for the slot.
4405 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004406 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004407 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004408 */
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004409static void iwl4965_rx_allocate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004410{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004411 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004412 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004413 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004414 unsigned long flags;
4415 spin_lock_irqsave(&rxq->lock, flags);
4416 while (!list_empty(&rxq->rx_used)) {
4417 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004418 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004419
4420 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07004421 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004422 alloc_skb(priv->hw_setting.rx_buf_size,
4423 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07004424 if (!rxb->skb) {
4425 if (net_ratelimit())
4426 printk(KERN_CRIT DRV_NAME
4427 ": Can not allocate SKB buffers\n");
4428 /* We don't reschedule replenish work here -- we will
4429 * call the restock method and if it still needs
4430 * more buffers it will schedule replenish */
4431 break;
4432 }
4433 priv->alloc_rxb_skb++;
4434 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004435
4436 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07004437 rxb->dma_addr =
4438 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004439 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004440 list_add_tail(&rxb->list, &rxq->rx_free);
4441 rxq->free_count++;
4442 }
4443 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004444}
4445
4446/*
4447 * this should be called while priv->lock is locked
4448*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02004449static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004450{
4451 struct iwl4965_priv *priv = data;
4452
4453 iwl4965_rx_allocate(priv);
4454 iwl4965_rx_queue_restock(priv);
4455}
4456
4457
4458void iwl4965_rx_replenish(void *data)
4459{
4460 struct iwl4965_priv *priv = data;
4461 unsigned long flags;
4462
4463 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004464
4465 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004466 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004467 spin_unlock_irqrestore(&priv->lock, flags);
4468}
4469
4470/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08004471 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07004472 * This free routine walks the list of POOL entries and if SKB is set to
4473 * non NULL it is unmapped and freed
4474 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004475static void iwl4965_rx_queue_free(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004476{
4477 int i;
4478 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4479 if (rxq->pool[i].skb != NULL) {
4480 pci_unmap_single(priv->pci_dev,
4481 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004482 priv->hw_setting.rx_buf_size,
4483 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004484 dev_kfree_skb(rxq->pool[i].skb);
4485 }
4486 }
4487
4488 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4489 rxq->dma_addr);
4490 rxq->bd = NULL;
4491}
4492
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004493int iwl4965_rx_queue_alloc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004494{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004495 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004496 struct pci_dev *dev = priv->pci_dev;
4497 int i;
4498
4499 spin_lock_init(&rxq->lock);
4500 INIT_LIST_HEAD(&rxq->rx_free);
4501 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004502
4503 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07004504 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4505 if (!rxq->bd)
4506 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004507
Zhu Yib481de92007-09-25 17:54:57 -07004508 /* Fill the rx_used queue with _all_ of the Rx buffers */
4509 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4510 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004511
Zhu Yib481de92007-09-25 17:54:57 -07004512 /* Set us so that we have processed and used all buffers, but have
4513 * not restocked the Rx queue with fresh buffers */
4514 rxq->read = rxq->write = 0;
4515 rxq->free_count = 0;
4516 rxq->need_update = 0;
4517 return 0;
4518}
4519
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004520void iwl4965_rx_queue_reset(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004521{
4522 unsigned long flags;
4523 int i;
4524 spin_lock_irqsave(&rxq->lock, flags);
4525 INIT_LIST_HEAD(&rxq->rx_free);
4526 INIT_LIST_HEAD(&rxq->rx_used);
4527 /* Fill the rx_used queue with _all_ of the Rx buffers */
4528 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4529 /* In the reset function, these buffers may have been allocated
4530 * to an SKB, so we need to unmap and free potential storage */
4531 if (rxq->pool[i].skb != NULL) {
4532 pci_unmap_single(priv->pci_dev,
4533 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004534 priv->hw_setting.rx_buf_size,
4535 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004536 priv->alloc_rxb_skb--;
4537 dev_kfree_skb(rxq->pool[i].skb);
4538 rxq->pool[i].skb = NULL;
4539 }
4540 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4541 }
4542
4543 /* Set us so that we have processed and used all buffers, but have
4544 * not restocked the Rx queue with fresh buffers */
4545 rxq->read = rxq->write = 0;
4546 rxq->free_count = 0;
4547 spin_unlock_irqrestore(&rxq->lock, flags);
4548}
4549
4550/* Convert linear signal-to-noise ratio into dB */
4551static u8 ratio2dB[100] = {
4552/* 0 1 2 3 4 5 6 7 8 9 */
4553 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4554 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4555 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4556 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4557 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4558 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4559 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4560 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4561 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4562 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4563};
4564
4565/* Calculates a relative dB value from a ratio of linear
4566 * (i.e. not dB) signal levels.
4567 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004568int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004569{
Adrian Bunkc899a572007-10-14 19:51:15 +02004570 /* 1000:1 or higher just report as 60 dB */
4571 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004572 return 60;
4573
Adrian Bunkc899a572007-10-14 19:51:15 +02004574 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004575 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004576 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004577 return (20 + (int)ratio2dB[sig_ratio/10]);
4578
4579 /* We shouldn't see this */
4580 if (sig_ratio < 1)
4581 return 0;
4582
4583 /* Use table for ratios 1:1 - 99:1 */
4584 return (int)ratio2dB[sig_ratio];
4585}
4586
4587#define PERFECT_RSSI (-20) /* dBm */
4588#define WORST_RSSI (-95) /* dBm */
4589#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4590
4591/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4592 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4593 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004594int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004595{
4596 int sig_qual;
4597 int degradation = PERFECT_RSSI - rssi_dbm;
4598
4599 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4600 * as indicator; formula is (signal dbm - noise dbm).
4601 * SNR at or above 40 is a great signal (100%).
4602 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4603 * Weakest usable signal is usually 10 - 15 dB SNR. */
4604 if (noise_dbm) {
4605 if (rssi_dbm - noise_dbm >= 40)
4606 return 100;
4607 else if (rssi_dbm < noise_dbm)
4608 return 0;
4609 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4610
4611 /* Else use just the signal level.
4612 * This formula is a least squares fit of data points collected and
4613 * compared with a reference system that had a percentage (%) display
4614 * for signal quality. */
4615 } else
4616 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4617 (15 * RSSI_RANGE + 62 * degradation)) /
4618 (RSSI_RANGE * RSSI_RANGE);
4619
4620 if (sig_qual > 100)
4621 sig_qual = 100;
4622 else if (sig_qual < 1)
4623 sig_qual = 0;
4624
4625 return sig_qual;
4626}
4627
4628/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004629 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004630 *
4631 * Uses the priv->rx_handlers callback function array to invoke
4632 * the appropriate handlers, including command responses,
4633 * frame-received notifications, and other notifications.
4634 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004635static void iwl4965_rx_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004636{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004637 struct iwl4965_rx_mem_buffer *rxb;
4638 struct iwl4965_rx_packet *pkt;
4639 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004640 u32 r, i;
4641 int reclaim;
4642 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004643 u8 fill_rx = 0;
4644 u32 count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004645
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004646 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4647 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004648 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004649 i = rxq->read;
4650
4651 /* Rx interrupt, but nothing sent from uCode */
4652 if (i == r)
4653 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4654
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004655 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4656 fill_rx = 1;
4657
Zhu Yib481de92007-09-25 17:54:57 -07004658 while (i != r) {
4659 rxb = rxq->queue[i];
4660
Ben Cahill9fbab512007-11-29 11:09:47 +08004661 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004662 * then a bug has been introduced in the queue refilling
4663 * routines -- catch it here */
4664 BUG_ON(rxb == NULL);
4665
4666 rxq->queue[i] = NULL;
4667
4668 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004669 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004670 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004671 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004672
4673 /* Reclaim a command buffer only if this packet is a response
4674 * to a (driver-originated) command.
4675 * If the packet (e.g. Rx frame) originated from uCode,
4676 * there is no command buffer to reclaim.
4677 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4678 * but apparently a few don't get set; catch them here. */
4679 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4680 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4681 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004682 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004683 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4684 (pkt->hdr.cmd != REPLY_TX);
4685
4686 /* Based on type of command response or notification,
4687 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004688 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004689 if (priv->rx_handlers[pkt->hdr.cmd]) {
4690 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4691 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4692 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4693 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4694 } else {
4695 /* No handling needed */
4696 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4697 "r %d i %d No handler needed for %s, 0x%02x\n",
4698 r, i, get_cmd_string(pkt->hdr.cmd),
4699 pkt->hdr.cmd);
4700 }
4701
4702 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004703 /* Invoke any callbacks, transfer the skb to caller, and
4704 * fire off the (possibly) blocking iwl4965_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004705 * as we reclaim the driver command queue */
4706 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004707 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004708 else
4709 IWL_WARNING("Claim null rxb?\n");
4710 }
4711
4712 /* For now we just don't re-use anything. We can tweak this
4713 * later to try and re-use notification packets and SKBs that
4714 * fail to Rx correctly */
4715 if (rxb->skb != NULL) {
4716 priv->alloc_rxb_skb--;
4717 dev_kfree_skb_any(rxb->skb);
4718 rxb->skb = NULL;
4719 }
4720
4721 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004722 priv->hw_setting.rx_buf_size,
4723 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004724 spin_lock_irqsave(&rxq->lock, flags);
4725 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4726 spin_unlock_irqrestore(&rxq->lock, flags);
4727 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004728 /* If there are a lot of unused frames,
4729 * restock the Rx queue so ucode wont assert. */
4730 if (fill_rx) {
4731 count++;
4732 if (count >= 8) {
4733 priv->rxq.read = i;
4734 __iwl4965_rx_replenish(priv);
4735 count = 0;
4736 }
4737 }
Zhu Yib481de92007-09-25 17:54:57 -07004738 }
4739
4740 /* Backtrack one entry */
4741 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004742 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004743}
4744
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004745/**
4746 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4747 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004748static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
4749 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004750{
4751 u32 reg = 0;
4752 int rc = 0;
4753 int txq_id = txq->q.id;
4754
4755 if (txq->need_update == 0)
4756 return rc;
4757
4758 /* if we're trying to save power */
4759 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4760 /* wake up nic if it's powered down ...
4761 * uCode will wake up, and interrupt us again, so next
4762 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004763 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004764
4765 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4766 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004767 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004768 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4769 return rc;
4770 }
4771
4772 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004773 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004774 if (rc)
4775 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004776 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004777 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004778 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004779
4780 /* else not in power-save mode, uCode will never sleep when we're
4781 * trying to tx (during RFKILL, we're not trying to tx). */
4782 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004783 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004784 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004785
4786 txq->need_update = 0;
4787
4788 return rc;
4789}
4790
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004791#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004792static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004793{
Joe Perches0795af52007-10-03 17:59:30 -07004794 DECLARE_MAC_BUF(mac);
4795
Zhu Yib481de92007-09-25 17:54:57 -07004796 IWL_DEBUG_RADIO("RX CONFIG:\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004797 iwl4965_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004798 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4799 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4800 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4801 le32_to_cpu(rxon->filter_flags));
4802 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4803 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4804 rxon->ofdm_basic_rates);
4805 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004806 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4807 print_mac(mac, rxon->node_addr));
4808 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4809 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004810 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4811}
4812#endif
4813
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004814static void iwl4965_enable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004815{
4816 IWL_DEBUG_ISR("Enabling interrupts\n");
4817 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004818 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004819}
4820
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004821static inline void iwl4965_disable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004822{
4823 clear_bit(STATUS_INT_ENABLED, &priv->status);
4824
4825 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004826 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004827
4828 /* acknowledge/clear/reset any interrupts still pending
4829 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004830 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4831 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004832 IWL_DEBUG_ISR("Disabled interrupts\n");
4833}
4834
4835static const char *desc_lookup(int i)
4836{
4837 switch (i) {
4838 case 1:
4839 return "FAIL";
4840 case 2:
4841 return "BAD_PARAM";
4842 case 3:
4843 return "BAD_CHECKSUM";
4844 case 4:
4845 return "NMI_INTERRUPT";
4846 case 5:
4847 return "SYSASSERT";
4848 case 6:
4849 return "FATAL_ERROR";
4850 }
4851
4852 return "UNKNOWN";
4853}
4854
4855#define ERROR_START_OFFSET (1 * sizeof(u32))
4856#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4857
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004858static void iwl4965_dump_nic_error_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004859{
4860 u32 data2, line;
4861 u32 desc, time, count, base, data1;
4862 u32 blink1, blink2, ilink1, ilink2;
4863 int rc;
4864
4865 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004867 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004868 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4869 return;
4870 }
4871
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004872 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004873 if (rc) {
4874 IWL_WARNING("Can not read from adapter at this time.\n");
4875 return;
4876 }
4877
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004878 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004879
4880 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4881 IWL_ERROR("Start IWL Error Log Dump:\n");
4882 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4883 priv->status, priv->config, count);
4884 }
4885
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004886 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4887 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4888 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4889 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4890 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4891 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4892 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4893 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4894 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004895
4896 IWL_ERROR("Desc Time "
4897 "data1 data2 line\n");
4898 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4899 desc_lookup(desc), desc, time, data1, data2, line);
4900 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4901 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4902 ilink1, ilink2);
4903
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004904 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004905}
4906
4907#define EVENT_START_OFFSET (4 * sizeof(u32))
4908
4909/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004910 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004911 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004912 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004913 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004914static void iwl4965_print_event_log(struct iwl4965_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004915 u32 num_events, u32 mode)
4916{
4917 u32 i;
4918 u32 base; /* SRAM byte address of event log header */
4919 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4920 u32 ptr; /* SRAM byte address of log data */
4921 u32 ev, time, data; /* event log data */
4922
4923 if (num_events == 0)
4924 return;
4925
4926 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4927
4928 if (mode == 0)
4929 event_size = 2 * sizeof(u32);
4930 else
4931 event_size = 3 * sizeof(u32);
4932
4933 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4934
4935 /* "time" is actually "data" for mode 0 (no timestamp).
4936 * place event id # at far right for easier visual parsing. */
4937 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004938 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004939 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004940 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004941 ptr += sizeof(u32);
4942 if (mode == 0)
4943 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4944 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004945 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004946 ptr += sizeof(u32);
4947 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4948 }
4949 }
4950}
4951
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004952static void iwl4965_dump_nic_event_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004953{
4954 int rc;
4955 u32 base; /* SRAM byte address of event log header */
4956 u32 capacity; /* event log capacity in # entries */
4957 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4958 u32 num_wraps; /* # times uCode wrapped to top of log */
4959 u32 next_entry; /* index of next entry to be written by uCode */
4960 u32 size; /* # entries that we'll print */
4961
4962 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004963 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004964 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4965 return;
4966 }
4967
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004968 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004969 if (rc) {
4970 IWL_WARNING("Can not read from adapter at this time.\n");
4971 return;
4972 }
4973
4974 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004975 capacity = iwl4965_read_targ_mem(priv, base);
4976 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4977 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4978 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004979
4980 size = num_wraps ? capacity : next_entry;
4981
4982 /* bail out if nothing in log */
4983 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004984 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004985 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004986 return;
4987 }
4988
Zhu Yi583fab32007-09-27 11:27:30 +08004989 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004990 size, num_wraps);
4991
4992 /* if uCode has wrapped back to top of log, start at the oldest entry,
4993 * i.e the next one that uCode would fill. */
4994 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004995 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004996 capacity - next_entry, mode);
4997
4998 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004999 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07005000
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005001 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005002}
5003
5004/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005005 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07005006 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005007static void iwl4965_irq_handle_error(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005008{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005009 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07005010 set_bit(STATUS_FW_ERROR, &priv->status);
5011
5012 /* Cancel currently queued command. */
5013 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
5014
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005015#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005016 if (iwl4965_debug_level & IWL_DL_FW_ERRORS) {
5017 iwl4965_dump_nic_error_log(priv);
5018 iwl4965_dump_nic_event_log(priv);
5019 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005020 }
5021#endif
5022
5023 wake_up_interruptible(&priv->wait_command_queue);
5024
5025 /* Keep the restart process from trying to send host
5026 * commands by clearing the INIT status bit */
5027 clear_bit(STATUS_READY, &priv->status);
5028
5029 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5030 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
5031 "Restarting adapter due to uCode error.\n");
5032
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005033 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005034 memcpy(&priv->recovery_rxon, &priv->active_rxon,
5035 sizeof(priv->recovery_rxon));
5036 priv->error_recovering = 1;
5037 }
5038 queue_work(priv->workqueue, &priv->restart);
5039 }
5040}
5041
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005042static void iwl4965_error_recovery(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005043{
5044 unsigned long flags;
5045
5046 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
5047 sizeof(priv->staging_rxon));
5048 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005049 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005051 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07005052
5053 spin_lock_irqsave(&priv->lock, flags);
5054 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
5055 priv->error_recovering = 0;
5056 spin_unlock_irqrestore(&priv->lock, flags);
5057}
5058
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005059static void iwl4965_irq_tasklet(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005060{
5061 u32 inta, handled = 0;
5062 u32 inta_fh;
5063 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005064#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07005065 u32 inta_mask;
5066#endif
5067
5068 spin_lock_irqsave(&priv->lock, flags);
5069
5070 /* Ack/clear/reset pending uCode interrupts.
5071 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
5072 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005073 inta = iwl4965_read32(priv, CSR_INT);
5074 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07005075
5076 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
5077 * Any new interrupts that happen after this, either while we're
5078 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005079 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
5080 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07005081
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005082#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005083 if (iwl4965_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08005084 /* just for debug */
5085 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07005086 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5087 inta, inta_mask, inta_fh);
5088 }
5089#endif
5090
5091 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
5092 * atomic, make sure that inta covers all the interrupts that
5093 * we've discovered, even if FH interrupt came in just after
5094 * reading CSR_INT. */
5095 if (inta_fh & CSR_FH_INT_RX_MASK)
5096 inta |= CSR_INT_BIT_FH_RX;
5097 if (inta_fh & CSR_FH_INT_TX_MASK)
5098 inta |= CSR_INT_BIT_FH_TX;
5099
5100 /* Now service all interrupt bits discovered above. */
5101 if (inta & CSR_INT_BIT_HW_ERR) {
5102 IWL_ERROR("Microcode HW error detected. Restarting.\n");
5103
5104 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005105 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005106
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005107 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005108
5109 handled |= CSR_INT_BIT_HW_ERR;
5110
5111 spin_unlock_irqrestore(&priv->lock, flags);
5112
5113 return;
5114 }
5115
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005116#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005117 if (iwl4965_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07005118 /* NIC fires this, but we don't use it, redundant with WAKEUP */
5119 if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
5120 IWL_DEBUG_ISR("Microcode started or stopped.\n");
5121
5122 /* Alive notification via Rx interrupt will do the real work */
5123 if (inta & CSR_INT_BIT_ALIVE)
5124 IWL_DEBUG_ISR("Alive interrupt\n");
5125 }
5126#endif
5127 /* Safely ignore these bits for debug checks below */
5128 inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
5129
Ben Cahill9fbab512007-11-29 11:09:47 +08005130 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07005131 if (inta & CSR_INT_BIT_RF_KILL) {
5132 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005133 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07005134 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
5135 hw_rf_kill = 1;
5136
5137 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
5138 "RF_KILL bit toggled to %s.\n",
5139 hw_rf_kill ? "disable radio":"enable radio");
5140
5141 /* Queue restart only if RF_KILL switch was set to "kill"
5142 * when we loaded driver, and is now set to "enable".
5143 * After we're Alive, RF_KILL gets handled by
5144 * iwl_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08005145 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
5146 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005147 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08005148 }
Zhu Yib481de92007-09-25 17:54:57 -07005149
5150 handled |= CSR_INT_BIT_RF_KILL;
5151 }
5152
Ben Cahill9fbab512007-11-29 11:09:47 +08005153 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07005154 if (inta & CSR_INT_BIT_CT_KILL) {
5155 IWL_ERROR("Microcode CT kill error detected.\n");
5156 handled |= CSR_INT_BIT_CT_KILL;
5157 }
5158
5159 /* Error detected by uCode */
5160 if (inta & CSR_INT_BIT_SW_ERR) {
5161 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
5162 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005163 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005164 handled |= CSR_INT_BIT_SW_ERR;
5165 }
5166
5167 /* uCode wakes up after power-down sleep */
5168 if (inta & CSR_INT_BIT_WAKEUP) {
5169 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005170 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
5171 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
5172 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
5173 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
5174 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
5175 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
5176 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07005177
5178 handled |= CSR_INT_BIT_WAKEUP;
5179 }
5180
5181 /* All uCode command responses, including Tx command responses,
5182 * Rx "responses" (frame-received notification), and other
5183 * notifications from uCode come through here*/
5184 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005185 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005186 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
5187 }
5188
5189 if (inta & CSR_INT_BIT_FH_TX) {
5190 IWL_DEBUG_ISR("Tx interrupt\n");
5191 handled |= CSR_INT_BIT_FH_TX;
5192 }
5193
5194 if (inta & ~handled)
5195 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
5196
5197 if (inta & ~CSR_INI_SET_MASK) {
5198 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
5199 inta & ~CSR_INI_SET_MASK);
5200 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
5201 }
5202
5203 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005204 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005205
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005206#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005207 if (iwl4965_debug_level & (IWL_DL_ISR)) {
5208 inta = iwl4965_read32(priv, CSR_INT);
5209 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
5210 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07005211 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
5212 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
5213 }
5214#endif
5215 spin_unlock_irqrestore(&priv->lock, flags);
5216}
5217
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005218static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07005219{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005220 struct iwl4965_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07005221 u32 inta, inta_mask;
5222 u32 inta_fh;
5223 if (!priv)
5224 return IRQ_NONE;
5225
5226 spin_lock(&priv->lock);
5227
5228 /* Disable (but don't clear!) interrupts here to avoid
5229 * back-to-back ISRs and sporadic interrupts from our NIC.
5230 * If we have something to service, the tasklet will re-enable ints.
5231 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005232 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
5233 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07005234
5235 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005236 inta = iwl4965_read32(priv, CSR_INT);
5237 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07005238
5239 /* Ignore interrupt if there's nothing in NIC to service.
5240 * This may be due to IRQ shared with another device,
5241 * or due to sporadic interrupts thrown from our NIC. */
5242 if (!inta && !inta_fh) {
5243 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
5244 goto none;
5245 }
5246
5247 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08005248 /* Hardware disappeared. It might have already raised
5249 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07005250 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08005251 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07005252 }
5253
5254 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5255 inta, inta_mask, inta_fh);
5256
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005257 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Zhu Yib481de92007-09-25 17:54:57 -07005258 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07005259
Oliver Neukum66fbb542007-11-15 09:31:10 +08005260 unplugged:
5261 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07005262 return IRQ_HANDLED;
5263
5264 none:
5265 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005266 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005267 spin_unlock(&priv->lock);
5268 return IRQ_NONE;
5269}
5270
5271/************************** EEPROM BANDS ****************************
5272 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005273 * The iwl4965_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07005274 * EEPROM contents to the specific channel number supported for each
5275 * band.
5276 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005277 * For example, iwl4965_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07005278 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5279 * The specific geography and calibration information for that channel
5280 * is contained in the eeprom map itself.
5281 *
5282 * During init, we copy the eeprom information and channel map
5283 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5284 *
5285 * channel_map_24/52 provides the index in the channel_info array for a
5286 * given channel. We have to have two separate maps as there is channel
5287 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5288 * band_2
5289 *
5290 * A value of 0xff stored in the channel_map indicates that the channel
5291 * is not supported by the hardware at all.
5292 *
5293 * A value of 0xfe in the channel_map indicates that the channel is not
5294 * valid for Tx with the current hardware. This means that
5295 * while the system can tune and receive on a given channel, it may not
5296 * be able to associate or transmit any frames on that
5297 * channel. There is no corresponding channel information for that
5298 * entry.
5299 *
5300 *********************************************************************/
5301
5302/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005303static const u8 iwl4965_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07005304 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5305};
5306
5307/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08005308static const u8 iwl4965_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005309 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5310};
5311
Ben Cahill9fbab512007-11-29 11:09:47 +08005312static const u8 iwl4965_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005313 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5314};
5315
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005316static const u8 iwl4965_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005317 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5318};
5319
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005320static const u8 iwl4965_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005321 145, 149, 153, 157, 161, 165
5322};
5323
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005324static u8 iwl4965_eeprom_band_6[] = { /* 2.4 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005325 1, 2, 3, 4, 5, 6, 7
5326};
5327
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005328static u8 iwl4965_eeprom_band_7[] = { /* 5.2 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005329 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5330};
5331
Ben Cahill9fbab512007-11-29 11:09:47 +08005332static void iwl4965_init_band_reference(const struct iwl4965_priv *priv,
5333 int band,
Zhu Yib481de92007-09-25 17:54:57 -07005334 int *eeprom_ch_count,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005335 const struct iwl4965_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07005336 **eeprom_ch_info,
5337 const u8 **eeprom_ch_index)
5338{
5339 switch (band) {
5340 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005341 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07005342 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005343 *eeprom_ch_index = iwl4965_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07005344 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005345 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005346 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07005347 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005348 *eeprom_ch_index = iwl4965_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07005349 break;
5350 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005351 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07005352 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005353 *eeprom_ch_index = iwl4965_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07005354 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005355 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005356 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07005357 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005358 *eeprom_ch_index = iwl4965_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07005359 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005360 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005361 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005362 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005363 *eeprom_ch_index = iwl4965_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07005364 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005365 case 6: /* 2.4GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005366 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_6);
Zhu Yib481de92007-09-25 17:54:57 -07005367 *eeprom_ch_info = priv->eeprom.band_24_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005368 *eeprom_ch_index = iwl4965_eeprom_band_6;
Zhu Yib481de92007-09-25 17:54:57 -07005369 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005370 case 7: /* 5 GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005371 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_7);
Zhu Yib481de92007-09-25 17:54:57 -07005372 *eeprom_ch_info = priv->eeprom.band_52_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005373 *eeprom_ch_index = iwl4965_eeprom_band_7;
Zhu Yib481de92007-09-25 17:54:57 -07005374 break;
5375 default:
5376 BUG();
5377 return;
5378 }
5379}
5380
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005381/**
5382 * iwl4965_get_channel_info - Find driver's private channel info
5383 *
5384 * Based on band and channel number.
5385 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005386const struct iwl4965_channel_info *iwl4965_get_channel_info(const struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005387 int phymode, u16 channel)
5388{
5389 int i;
5390
5391 switch (phymode) {
5392 case MODE_IEEE80211A:
5393 for (i = 14; i < priv->channel_count; i++) {
5394 if (priv->channel_info[i].channel == channel)
5395 return &priv->channel_info[i];
5396 }
5397 break;
5398
5399 case MODE_IEEE80211B:
5400 case MODE_IEEE80211G:
5401 if (channel >= 1 && channel <= 14)
5402 return &priv->channel_info[channel - 1];
5403 break;
5404
5405 }
5406
5407 return NULL;
5408}
5409
5410#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5411 ? # x " " : "")
5412
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005413/**
5414 * iwl4965_init_channel_map - Set up driver's info for all possible channels
5415 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005416static int iwl4965_init_channel_map(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005417{
5418 int eeprom_ch_count = 0;
5419 const u8 *eeprom_ch_index = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005420 const struct iwl4965_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005421 int band, ch;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005422 struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005423
5424 if (priv->channel_count) {
5425 IWL_DEBUG_INFO("Channel map already initialized.\n");
5426 return 0;
5427 }
5428
5429 if (priv->eeprom.version < 0x2f) {
5430 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5431 priv->eeprom.version);
5432 return -EINVAL;
5433 }
5434
5435 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5436
5437 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005438 ARRAY_SIZE(iwl4965_eeprom_band_1) +
5439 ARRAY_SIZE(iwl4965_eeprom_band_2) +
5440 ARRAY_SIZE(iwl4965_eeprom_band_3) +
5441 ARRAY_SIZE(iwl4965_eeprom_band_4) +
5442 ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005443
5444 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5445
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005446 priv->channel_info = kzalloc(sizeof(struct iwl4965_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07005447 priv->channel_count, GFP_KERNEL);
5448 if (!priv->channel_info) {
5449 IWL_ERROR("Could not allocate channel_info\n");
5450 priv->channel_count = 0;
5451 return -ENOMEM;
5452 }
5453
5454 ch_info = priv->channel_info;
5455
5456 /* Loop through the 5 EEPROM bands adding them in order to the
5457 * channel map we maintain (that contains additional information than
5458 * what just in the EEPROM) */
5459 for (band = 1; band <= 5; band++) {
5460
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005461 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005462 &eeprom_ch_info, &eeprom_ch_index);
5463
5464 /* Loop through each band adding each of the channels */
5465 for (ch = 0; ch < eeprom_ch_count; ch++) {
5466 ch_info->channel = eeprom_ch_index[ch];
5467 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5468 MODE_IEEE80211A;
5469
5470 /* permanently store EEPROM's channel regulatory flags
5471 * and max power in channel info database. */
5472 ch_info->eeprom = eeprom_ch_info[ch];
5473
5474 /* Copy the run-time flags so they are there even on
5475 * invalid channels */
5476 ch_info->flags = eeprom_ch_info[ch].flags;
5477
5478 if (!(is_channel_valid(ch_info))) {
5479 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5480 "No traffic\n",
5481 ch_info->channel,
5482 ch_info->flags,
5483 is_channel_a_band(ch_info) ?
5484 "5.2" : "2.4");
5485 ch_info++;
5486 continue;
5487 }
5488
5489 /* Initialize regulatory-based run-time data */
5490 ch_info->max_power_avg = ch_info->curr_txpow =
5491 eeprom_ch_info[ch].max_power_avg;
5492 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5493 ch_info->min_power = 0;
5494
5495 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5496 " %ddBm): Ad-Hoc %ssupported\n",
5497 ch_info->channel,
5498 is_channel_a_band(ch_info) ?
5499 "5.2" : "2.4",
5500 CHECK_AND_PRINT(IBSS),
5501 CHECK_AND_PRINT(ACTIVE),
5502 CHECK_AND_PRINT(RADAR),
5503 CHECK_AND_PRINT(WIDE),
5504 CHECK_AND_PRINT(NARROW),
5505 CHECK_AND_PRINT(DFS),
5506 eeprom_ch_info[ch].flags,
5507 eeprom_ch_info[ch].max_power_avg,
5508 ((eeprom_ch_info[ch].
5509 flags & EEPROM_CHANNEL_IBSS)
5510 && !(eeprom_ch_info[ch].
5511 flags & EEPROM_CHANNEL_RADAR))
5512 ? "" : "not ");
5513
5514 /* Set the user_txpower_limit to the highest power
5515 * supported by any channel */
5516 if (eeprom_ch_info[ch].max_power_avg >
5517 priv->user_txpower_limit)
5518 priv->user_txpower_limit =
5519 eeprom_ch_info[ch].max_power_avg;
5520
5521 ch_info++;
5522 }
5523 }
5524
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005525 /* Two additional EEPROM bands for 2.4 and 5 GHz FAT channels */
Zhu Yib481de92007-09-25 17:54:57 -07005526 for (band = 6; band <= 7; band++) {
5527 int phymode;
5528 u8 fat_extension_chan;
5529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005530 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005531 &eeprom_ch_info, &eeprom_ch_index);
5532
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005533 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
Zhu Yib481de92007-09-25 17:54:57 -07005534 phymode = (band == 6) ? MODE_IEEE80211B : MODE_IEEE80211A;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005535
Zhu Yib481de92007-09-25 17:54:57 -07005536 /* Loop through each band adding each of the channels */
5537 for (ch = 0; ch < eeprom_ch_count; ch++) {
5538
5539 if ((band == 6) &&
5540 ((eeprom_ch_index[ch] == 5) ||
5541 (eeprom_ch_index[ch] == 6) ||
5542 (eeprom_ch_index[ch] == 7)))
5543 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5544 else
5545 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5546
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005547 /* Set up driver's info for lower half */
Zhu Yib481de92007-09-25 17:54:57 -07005548 iwl4965_set_fat_chan_info(priv, phymode,
5549 eeprom_ch_index[ch],
5550 &(eeprom_ch_info[ch]),
5551 fat_extension_chan);
5552
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005553 /* Set up driver's info for upper half */
Zhu Yib481de92007-09-25 17:54:57 -07005554 iwl4965_set_fat_chan_info(priv, phymode,
5555 (eeprom_ch_index[ch] + 4),
5556 &(eeprom_ch_info[ch]),
5557 HT_IE_EXT_CHANNEL_BELOW);
5558 }
5559 }
5560
5561 return 0;
5562}
5563
5564/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5565 * sending probe req. This should be set long enough to hear probe responses
5566 * from more than one AP. */
5567#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5568#define IWL_ACTIVE_DWELL_TIME_52 (10)
5569
5570/* For faster active scanning, scan will move to the next channel if fewer than
5571 * PLCP_QUIET_THRESH packets are heard on this channel within
5572 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5573 * time if it's a quiet channel (nothing responded to our probe, and there's
5574 * no other traffic).
5575 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5576#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5577#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5578
5579/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5580 * Must be set longer than active dwell time.
5581 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5582#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5583#define IWL_PASSIVE_DWELL_TIME_52 (10)
5584#define IWL_PASSIVE_DWELL_BASE (100)
5585#define IWL_CHANNEL_TUNE_TIME 5
5586
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005587static inline u16 iwl4965_get_active_dwell_time(struct iwl4965_priv *priv, int phymode)
Zhu Yib481de92007-09-25 17:54:57 -07005588{
5589 if (phymode == MODE_IEEE80211A)
5590 return IWL_ACTIVE_DWELL_TIME_52;
5591 else
5592 return IWL_ACTIVE_DWELL_TIME_24;
5593}
5594
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005595static u16 iwl4965_get_passive_dwell_time(struct iwl4965_priv *priv, int phymode)
Zhu Yib481de92007-09-25 17:54:57 -07005596{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005597 u16 active = iwl4965_get_active_dwell_time(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005598 u16 passive = (phymode != MODE_IEEE80211A) ?
5599 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5600 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5601
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005602 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005603 /* If we're associated, we clamp the maximum passive
5604 * dwell time to be 98% of the beacon interval (minus
5605 * 2 * channel tune time) */
5606 passive = priv->beacon_int;
5607 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5608 passive = IWL_PASSIVE_DWELL_BASE;
5609 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5610 }
5611
5612 if (passive <= active)
5613 passive = active + 1;
5614
5615 return passive;
5616}
5617
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005618static int iwl4965_get_channels_for_scan(struct iwl4965_priv *priv, int phymode,
Zhu Yib481de92007-09-25 17:54:57 -07005619 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005620 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07005621{
5622 const struct ieee80211_channel *channels = NULL;
5623 const struct ieee80211_hw_mode *hw_mode;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005624 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005625 u16 passive_dwell = 0;
5626 u16 active_dwell = 0;
5627 int added, i;
5628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005629 hw_mode = iwl4965_get_hw_mode(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005630 if (!hw_mode)
5631 return 0;
5632
5633 channels = hw_mode->channels;
5634
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005635 active_dwell = iwl4965_get_active_dwell_time(priv, phymode);
5636 passive_dwell = iwl4965_get_passive_dwell_time(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005637
5638 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5639 if (channels[i].chan ==
5640 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005641 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005642 IWL_DEBUG_SCAN
5643 ("Skipping current channel %d\n",
5644 le16_to_cpu(priv->active_rxon.channel));
5645 continue;
5646 }
5647 } else if (priv->only_active_channel)
5648 continue;
5649
5650 scan_ch->channel = channels[i].chan;
5651
Ben Cahill9fbab512007-11-29 11:09:47 +08005652 ch_info = iwl4965_get_channel_info(priv, phymode,
5653 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005654 if (!is_channel_valid(ch_info)) {
5655 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5656 scan_ch->channel);
5657 continue;
5658 }
5659
5660 if (!is_active || is_channel_passive(ch_info) ||
5661 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5662 scan_ch->type = 0; /* passive */
5663 else
5664 scan_ch->type = 1; /* active */
5665
5666 if (scan_ch->type & 1)
5667 scan_ch->type |= (direct_mask << 1);
5668
5669 if (is_channel_narrow(ch_info))
5670 scan_ch->type |= (1 << 7);
5671
5672 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5673 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5674
Ben Cahill9fbab512007-11-29 11:09:47 +08005675 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07005676 scan_ch->tpc.dsp_atten = 110;
5677 /* scan_pwr_info->tpc.dsp_atten; */
5678
5679 /*scan_pwr_info->tpc.tx_gain; */
5680 if (phymode == MODE_IEEE80211A)
5681 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5682 else {
5683 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5684 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08005685 * power level:
5686 * scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07005687 */
5688 }
5689
5690 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5691 scan_ch->channel,
5692 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5693 (scan_ch->type & 1) ?
5694 active_dwell : passive_dwell);
5695
5696 scan_ch++;
5697 added++;
5698 }
5699
5700 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5701 return added;
5702}
5703
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005704static void iwl4965_reset_channel_flag(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005705{
5706 int i, j;
5707 for (i = 0; i < 3; i++) {
5708 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5709 for (j = 0; j < hw_mode->num_channels; j++)
5710 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5711 }
5712}
5713
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005714static void iwl4965_init_hw_rates(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005715 struct ieee80211_rate *rates)
5716{
5717 int i;
5718
5719 for (i = 0; i < IWL_RATE_COUNT; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005720 rates[i].rate = iwl4965_rates[i].ieee * 5;
Zhu Yib481de92007-09-25 17:54:57 -07005721 rates[i].val = i; /* Rate scaling will work on indexes */
5722 rates[i].val2 = i;
5723 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5724 /* Only OFDM have the bits-per-symbol set */
5725 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5726 rates[i].flags |= IEEE80211_RATE_OFDM;
5727 else {
5728 /*
5729 * If CCK 1M then set rate flag to CCK else CCK_2
5730 * which is CCK | PREAMBLE2
5731 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005732 rates[i].flags |= (iwl4965_rates[i].plcp == 10) ?
Zhu Yib481de92007-09-25 17:54:57 -07005733 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5734 }
5735
5736 /* Set up which ones are basic rates... */
5737 if (IWL_BASIC_RATES_MASK & (1 << i))
5738 rates[i].flags |= IEEE80211_RATE_BASIC;
5739 }
Zhu Yib481de92007-09-25 17:54:57 -07005740}
5741
5742/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005743 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005744 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005745static int iwl4965_init_geos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005746{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005747 struct iwl4965_channel_info *ch;
Zhu Yib481de92007-09-25 17:54:57 -07005748 struct ieee80211_hw_mode *modes;
5749 struct ieee80211_channel *channels;
5750 struct ieee80211_channel *geo_ch;
5751 struct ieee80211_rate *rates;
5752 int i = 0;
5753 enum {
5754 A = 0,
5755 B = 1,
5756 G = 2,
Zhu Yib481de92007-09-25 17:54:57 -07005757 };
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02005758 int mode_count = 3;
Zhu Yib481de92007-09-25 17:54:57 -07005759
5760 if (priv->modes) {
5761 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5762 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5763 return 0;
5764 }
5765
5766 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5767 GFP_KERNEL);
5768 if (!modes)
5769 return -ENOMEM;
5770
5771 channels = kzalloc(sizeof(struct ieee80211_channel) *
5772 priv->channel_count, GFP_KERNEL);
5773 if (!channels) {
5774 kfree(modes);
5775 return -ENOMEM;
5776 }
5777
5778 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5779 GFP_KERNEL);
5780 if (!rates) {
5781 kfree(modes);
5782 kfree(channels);
5783 return -ENOMEM;
5784 }
5785
5786 /* 0 = 802.11a
5787 * 1 = 802.11b
5788 * 2 = 802.11g
5789 */
5790
5791 /* 5.2GHz channels start after the 2.4GHz channels */
5792 modes[A].mode = MODE_IEEE80211A;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005793 modes[A].channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
Zhu Yib481de92007-09-25 17:54:57 -07005794 modes[A].rates = rates;
5795 modes[A].num_rates = 8; /* just OFDM */
5796 modes[A].rates = &rates[4];
5797 modes[A].num_channels = 0;
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02005798#ifdef CONFIG_IWL4965_HT
5799 iwl4965_init_ht_hw_capab(&modes[A].ht_info, MODE_IEEE80211A);
5800#endif
Zhu Yib481de92007-09-25 17:54:57 -07005801
5802 modes[B].mode = MODE_IEEE80211B;
5803 modes[B].channels = channels;
5804 modes[B].rates = rates;
5805 modes[B].num_rates = 4; /* just CCK */
5806 modes[B].num_channels = 0;
5807
5808 modes[G].mode = MODE_IEEE80211G;
5809 modes[G].channels = channels;
5810 modes[G].rates = rates;
5811 modes[G].num_rates = 12; /* OFDM & CCK */
5812 modes[G].num_channels = 0;
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02005813#ifdef CONFIG_IWL4965_HT
5814 iwl4965_init_ht_hw_capab(&modes[G].ht_info, MODE_IEEE80211G);
5815#endif
Zhu Yib481de92007-09-25 17:54:57 -07005816
5817 priv->ieee_channels = channels;
5818 priv->ieee_rates = rates;
5819
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005820 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005821
5822 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5823 ch = &priv->channel_info[i];
5824
5825 if (!is_channel_valid(ch)) {
5826 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5827 "skipping.\n",
5828 ch->channel, is_channel_a_band(ch) ?
5829 "5.2" : "2.4");
5830 continue;
5831 }
5832
5833 if (is_channel_a_band(ch)) {
5834 geo_ch = &modes[A].channels[modes[A].num_channels++];
Zhu Yib481de92007-09-25 17:54:57 -07005835 } else {
5836 geo_ch = &modes[B].channels[modes[B].num_channels++];
5837 modes[G].num_channels++;
Zhu Yib481de92007-09-25 17:54:57 -07005838 }
5839
5840 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5841 geo_ch->chan = ch->channel;
5842 geo_ch->power_level = ch->max_power_avg;
5843 geo_ch->antenna_max = 0xff;
5844
5845 if (is_channel_valid(ch)) {
5846 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5847 if (ch->flags & EEPROM_CHANNEL_IBSS)
5848 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5849
5850 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5851 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5852
5853 if (ch->flags & EEPROM_CHANNEL_RADAR)
5854 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5855
5856 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5857 priv->max_channel_txpower_limit =
5858 ch->max_power_avg;
5859 }
5860
5861 geo_ch->val = geo_ch->flag;
5862 }
5863
5864 if ((modes[A].num_channels == 0) && priv->is_abg) {
5865 printk(KERN_INFO DRV_NAME
5866 ": Incorrectly detected BG card as ABG. Please send "
5867 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5868 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5869 priv->is_abg = 0;
5870 }
5871
5872 printk(KERN_INFO DRV_NAME
5873 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5874 modes[G].num_channels, modes[A].num_channels);
5875
5876 /*
5877 * NOTE: We register these in preference of order -- the
5878 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5879 * a phymode based on rates or AP capabilities but seems to
5880 * configure it purely on if the channel being configured
5881 * is supported by a mode -- and the first match is taken
5882 */
5883
5884 if (modes[G].num_channels)
5885 ieee80211_register_hwmode(priv->hw, &modes[G]);
5886 if (modes[B].num_channels)
5887 ieee80211_register_hwmode(priv->hw, &modes[B]);
5888 if (modes[A].num_channels)
5889 ieee80211_register_hwmode(priv->hw, &modes[A]);
5890
5891 priv->modes = modes;
5892 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5893
5894 return 0;
5895}
5896
5897/******************************************************************************
5898 *
5899 * uCode download functions
5900 *
5901 ******************************************************************************/
5902
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005903static void iwl4965_dealloc_ucode_pci(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005904{
5905 if (priv->ucode_code.v_addr != NULL) {
5906 pci_free_consistent(priv->pci_dev,
5907 priv->ucode_code.len,
5908 priv->ucode_code.v_addr,
5909 priv->ucode_code.p_addr);
5910 priv->ucode_code.v_addr = NULL;
5911 }
5912 if (priv->ucode_data.v_addr != NULL) {
5913 pci_free_consistent(priv->pci_dev,
5914 priv->ucode_data.len,
5915 priv->ucode_data.v_addr,
5916 priv->ucode_data.p_addr);
5917 priv->ucode_data.v_addr = NULL;
5918 }
5919 if (priv->ucode_data_backup.v_addr != NULL) {
5920 pci_free_consistent(priv->pci_dev,
5921 priv->ucode_data_backup.len,
5922 priv->ucode_data_backup.v_addr,
5923 priv->ucode_data_backup.p_addr);
5924 priv->ucode_data_backup.v_addr = NULL;
5925 }
5926 if (priv->ucode_init.v_addr != NULL) {
5927 pci_free_consistent(priv->pci_dev,
5928 priv->ucode_init.len,
5929 priv->ucode_init.v_addr,
5930 priv->ucode_init.p_addr);
5931 priv->ucode_init.v_addr = NULL;
5932 }
5933 if (priv->ucode_init_data.v_addr != NULL) {
5934 pci_free_consistent(priv->pci_dev,
5935 priv->ucode_init_data.len,
5936 priv->ucode_init_data.v_addr,
5937 priv->ucode_init_data.p_addr);
5938 priv->ucode_init_data.v_addr = NULL;
5939 }
5940 if (priv->ucode_boot.v_addr != NULL) {
5941 pci_free_consistent(priv->pci_dev,
5942 priv->ucode_boot.len,
5943 priv->ucode_boot.v_addr,
5944 priv->ucode_boot.p_addr);
5945 priv->ucode_boot.v_addr = NULL;
5946 }
5947}
5948
5949/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005950 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005951 * looking at all data.
5952 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02005953static int iwl4965_verify_inst_full(struct iwl4965_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005954 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005955{
5956 u32 val;
5957 u32 save_len = len;
5958 int rc = 0;
5959 u32 errcnt;
5960
5961 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5962
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005963 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005964 if (rc)
5965 return rc;
5966
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005967 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005968
5969 errcnt = 0;
5970 for (; len > 0; len -= sizeof(u32), image++) {
5971 /* read data comes through single port, auto-incr addr */
5972 /* NOTE: Use the debugless read so we don't flood kernel log
5973 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005974 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005975 if (val != le32_to_cpu(*image)) {
5976 IWL_ERROR("uCode INST section is invalid at "
5977 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5978 save_len - len, val, le32_to_cpu(*image));
5979 rc = -EIO;
5980 errcnt++;
5981 if (errcnt >= 20)
5982 break;
5983 }
5984 }
5985
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005986 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005987
5988 if (!errcnt)
5989 IWL_DEBUG_INFO
5990 ("ucode image in INSTRUCTION memory is good\n");
5991
5992 return rc;
5993}
5994
5995
5996/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005997 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005998 * using sample data 100 bytes apart. If these sample points are good,
5999 * it's a pretty good bet that everything between them is good, too.
6000 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006001static int iwl4965_verify_inst_sparse(struct iwl4965_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07006002{
6003 u32 val;
6004 int rc = 0;
6005 u32 errcnt = 0;
6006 u32 i;
6007
6008 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
6009
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006010 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006011 if (rc)
6012 return rc;
6013
6014 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
6015 /* read data comes through single port, auto-incr addr */
6016 /* NOTE: Use the debugless read so we don't flood kernel log
6017 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006018 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07006019 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006020 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07006021 if (val != le32_to_cpu(*image)) {
6022#if 0 /* Enable this if you want to see details */
6023 IWL_ERROR("uCode INST section is invalid at "
6024 "offset 0x%x, is 0x%x, s/b 0x%x\n",
6025 i, val, *image);
6026#endif
6027 rc = -EIO;
6028 errcnt++;
6029 if (errcnt >= 3)
6030 break;
6031 }
6032 }
6033
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006034 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006035
6036 return rc;
6037}
6038
6039
6040/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006041 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07006042 * and verify its contents
6043 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006044static int iwl4965_verify_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006045{
6046 __le32 *image;
6047 u32 len;
6048 int rc = 0;
6049
6050 /* Try bootstrap */
6051 image = (__le32 *)priv->ucode_boot.v_addr;
6052 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006053 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07006054 if (rc == 0) {
6055 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
6056 return 0;
6057 }
6058
6059 /* Try initialize */
6060 image = (__le32 *)priv->ucode_init.v_addr;
6061 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006062 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07006063 if (rc == 0) {
6064 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
6065 return 0;
6066 }
6067
6068 /* Try runtime/protocol */
6069 image = (__le32 *)priv->ucode_code.v_addr;
6070 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006071 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07006072 if (rc == 0) {
6073 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
6074 return 0;
6075 }
6076
6077 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
6078
Ben Cahill9fbab512007-11-29 11:09:47 +08006079 /* Since nothing seems to match, show first several data entries in
6080 * instruction SRAM, so maybe visual inspection will give a clue.
6081 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07006082 image = (__le32 *)priv->ucode_boot.v_addr;
6083 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006084 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07006085
6086 return rc;
6087}
6088
6089
6090/* check contents of special bootstrap uCode SRAM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006091static int iwl4965_verify_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006092{
6093 __le32 *image = priv->ucode_boot.v_addr;
6094 u32 len = priv->ucode_boot.len;
6095 u32 reg;
6096 u32 val;
6097
6098 IWL_DEBUG_INFO("Begin verify bsm\n");
6099
6100 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006101 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07006102 for (reg = BSM_SRAM_LOWER_BOUND;
6103 reg < BSM_SRAM_LOWER_BOUND + len;
6104 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006105 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07006106 if (val != le32_to_cpu(*image)) {
6107 IWL_ERROR("BSM uCode verification failed at "
6108 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
6109 BSM_SRAM_LOWER_BOUND,
6110 reg - BSM_SRAM_LOWER_BOUND, len,
6111 val, le32_to_cpu(*image));
6112 return -EIO;
6113 }
6114 }
6115
6116 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
6117
6118 return 0;
6119}
6120
6121/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006122 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07006123 *
6124 * BSM operation:
6125 *
6126 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
6127 * in special SRAM that does not power down during RFKILL. When powering back
6128 * up after power-saving sleeps (or during initial uCode load), the BSM loads
6129 * the bootstrap program into the on-board processor, and starts it.
6130 *
6131 * The bootstrap program loads (via DMA) instructions and data for a new
6132 * program from host DRAM locations indicated by the host driver in the
6133 * BSM_DRAM_* registers. Once the new program is loaded, it starts
6134 * automatically.
6135 *
6136 * When initializing the NIC, the host driver points the BSM to the
6137 * "initialize" uCode image. This uCode sets up some internal data, then
6138 * notifies host via "initialize alive" that it is complete.
6139 *
6140 * The host then replaces the BSM_DRAM_* pointer values to point to the
6141 * normal runtime uCode instructions and a backup uCode data cache buffer
6142 * (filled initially with starting data values for the on-board processor),
6143 * then triggers the "initialize" uCode to load and launch the runtime uCode,
6144 * which begins normal operation.
6145 *
6146 * When doing a power-save shutdown, runtime uCode saves data SRAM into
6147 * the backup data cache in DRAM before SRAM is powered down.
6148 *
6149 * When powering back up, the BSM loads the bootstrap program. This reloads
6150 * the runtime uCode instructions and the backup data cache into SRAM,
6151 * and re-launches the runtime uCode from where it left off.
6152 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006153static int iwl4965_load_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006154{
6155 __le32 *image = priv->ucode_boot.v_addr;
6156 u32 len = priv->ucode_boot.len;
6157 dma_addr_t pinst;
6158 dma_addr_t pdata;
6159 u32 inst_len;
6160 u32 data_len;
6161 int rc;
6162 int i;
6163 u32 done;
6164 u32 reg_offset;
6165
6166 IWL_DEBUG_INFO("Begin load bsm\n");
6167
6168 /* make sure bootstrap program is no larger than BSM's SRAM size */
6169 if (len > IWL_MAX_BSM_SIZE)
6170 return -EINVAL;
6171
6172 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08006173 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006174 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07006175 * after the "initialize" uCode has run, to point to
6176 * runtime/protocol instructions and backup data cache. */
6177 pinst = priv->ucode_init.p_addr >> 4;
6178 pdata = priv->ucode_init_data.p_addr >> 4;
6179 inst_len = priv->ucode_init.len;
6180 data_len = priv->ucode_init_data.len;
6181
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006182 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006183 if (rc)
6184 return rc;
6185
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006186 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6187 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6188 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
6189 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07006190
6191 /* Fill BSM memory with bootstrap instructions */
6192 for (reg_offset = BSM_SRAM_LOWER_BOUND;
6193 reg_offset < BSM_SRAM_LOWER_BOUND + len;
6194 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006195 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07006196 le32_to_cpu(*image));
6197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006198 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006199 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006200 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006201 return rc;
6202 }
6203
6204 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006205 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
6206 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006207 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006208 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07006209
6210 /* Load bootstrap code into instruction SRAM now,
6211 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006212 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006213 BSM_WR_CTRL_REG_BIT_START);
6214
6215 /* Wait for load of bootstrap uCode to finish */
6216 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006217 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07006218 if (!(done & BSM_WR_CTRL_REG_BIT_START))
6219 break;
6220 udelay(10);
6221 }
6222 if (i < 100)
6223 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
6224 else {
6225 IWL_ERROR("BSM write did not complete!\n");
6226 return -EIO;
6227 }
6228
6229 /* Enable future boot loads whenever power management unit triggers it
6230 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006231 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006232 BSM_WR_CTRL_REG_BIT_START_EN);
6233
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006234 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006235
6236 return 0;
6237}
6238
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006239static void iwl4965_nic_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006240{
6241 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006242 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006243}
6244
Tomas Winkler90e759d2007-11-29 11:09:41 +08006245static int iwl4965_alloc_fw_desc(struct pci_dev *pci_dev, struct fw_desc *desc)
6246{
6247 desc->v_addr = pci_alloc_consistent(pci_dev, desc->len, &desc->p_addr);
6248 return (desc->v_addr != NULL) ? 0 : -ENOMEM;
6249}
6250
Zhu Yib481de92007-09-25 17:54:57 -07006251/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006252 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07006253 *
6254 * Copy into buffers for card to fetch via bus-mastering
6255 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006256static int iwl4965_read_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006257{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006258 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006259 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006260 const struct firmware *ucode_raw;
6261 const char *name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode";
6262 u8 *src;
6263 size_t len;
6264 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
6265
6266 /* Ask kernel firmware_class module to get the boot firmware off disk.
6267 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006268 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
6269 if (ret < 0) {
6270 IWL_ERROR("%s firmware file req failed: Reason %d\n",
6271 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07006272 goto error;
6273 }
6274
6275 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
6276 name, ucode_raw->size);
6277
6278 /* Make sure that we got at least our header! */
6279 if (ucode_raw->size < sizeof(*ucode)) {
6280 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006281 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006282 goto err_release;
6283 }
6284
6285 /* Data from ucode file: header followed by uCode images */
6286 ucode = (void *)ucode_raw->data;
6287
6288 ver = le32_to_cpu(ucode->ver);
6289 inst_size = le32_to_cpu(ucode->inst_size);
6290 data_size = le32_to_cpu(ucode->data_size);
6291 init_size = le32_to_cpu(ucode->init_size);
6292 init_data_size = le32_to_cpu(ucode->init_data_size);
6293 boot_size = le32_to_cpu(ucode->boot_size);
6294
6295 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
6296 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
6297 inst_size);
6298 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
6299 data_size);
6300 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
6301 init_size);
6302 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
6303 init_data_size);
6304 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
6305 boot_size);
6306
6307 /* Verify size of file vs. image size info in file's header */
6308 if (ucode_raw->size < sizeof(*ucode) +
6309 inst_size + data_size + init_size +
6310 init_data_size + boot_size) {
6311
6312 IWL_DEBUG_INFO("uCode file size %d too small\n",
6313 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08006314 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006315 goto err_release;
6316 }
6317
6318 /* Verify that uCode images will fit in card's SRAM */
6319 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08006320 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
6321 inst_size);
6322 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006323 goto err_release;
6324 }
6325
6326 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08006327 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
6328 data_size);
6329 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006330 goto err_release;
6331 }
6332 if (init_size > IWL_MAX_INST_SIZE) {
6333 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006334 ("uCode init instr len %d too large to fit in\n",
6335 init_size);
6336 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006337 goto err_release;
6338 }
6339 if (init_data_size > IWL_MAX_DATA_SIZE) {
6340 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006341 ("uCode init data len %d too large to fit in\n",
6342 init_data_size);
6343 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006344 goto err_release;
6345 }
6346 if (boot_size > IWL_MAX_BSM_SIZE) {
6347 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006348 ("uCode boot instr len %d too large to fit in\n",
6349 boot_size);
6350 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006351 goto err_release;
6352 }
6353
6354 /* Allocate ucode buffers for card's bus-master loading ... */
6355
6356 /* Runtime instructions and 2 copies of data:
6357 * 1) unmodified from disk
6358 * 2) backup cache for save/restore during power-downs */
6359 priv->ucode_code.len = inst_size;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006360 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07006361
6362 priv->ucode_data.len = data_size;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006363 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07006364
6365 priv->ucode_data_backup.len = data_size;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006366 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07006367
6368 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006369 if (init_size && init_data_size) {
6370 priv->ucode_init.len = init_size;
6371 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07006372
Tomas Winkler90e759d2007-11-29 11:09:41 +08006373 priv->ucode_init_data.len = init_data_size;
6374 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
6375
6376 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
6377 goto err_pci_alloc;
6378 }
Zhu Yib481de92007-09-25 17:54:57 -07006379
6380 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006381 if (boot_size) {
6382 priv->ucode_boot.len = boot_size;
6383 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07006384
Tomas Winkler90e759d2007-11-29 11:09:41 +08006385 if (!priv->ucode_boot.v_addr)
6386 goto err_pci_alloc;
6387 }
Zhu Yib481de92007-09-25 17:54:57 -07006388
6389 /* Copy images into buffers for card's bus-master reads ... */
6390
6391 /* Runtime instructions (first block of data in file) */
6392 src = &ucode->data[0];
6393 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006394 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006395 memcpy(priv->ucode_code.v_addr, src, len);
6396 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6397 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6398
6399 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006400 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07006401 src = &ucode->data[inst_size];
6402 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006403 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006404 memcpy(priv->ucode_data.v_addr, src, len);
6405 memcpy(priv->ucode_data_backup.v_addr, src, len);
6406
6407 /* Initialization instructions (3rd block) */
6408 if (init_size) {
6409 src = &ucode->data[inst_size + data_size];
6410 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006411 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
6412 len);
Zhu Yib481de92007-09-25 17:54:57 -07006413 memcpy(priv->ucode_init.v_addr, src, len);
6414 }
6415
6416 /* Initialization data (4th block) */
6417 if (init_data_size) {
6418 src = &ucode->data[inst_size + data_size + init_size];
6419 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006420 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
6421 len);
Zhu Yib481de92007-09-25 17:54:57 -07006422 memcpy(priv->ucode_init_data.v_addr, src, len);
6423 }
6424
6425 /* Bootstrap instructions (5th block) */
6426 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6427 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006428 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006429 memcpy(priv->ucode_boot.v_addr, src, len);
6430
6431 /* We have our copies now, allow OS release its copies */
6432 release_firmware(ucode_raw);
6433 return 0;
6434
6435 err_pci_alloc:
6436 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006437 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006438 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006439
6440 err_release:
6441 release_firmware(ucode_raw);
6442
6443 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08006444 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006445}
6446
6447
6448/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006449 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07006450 *
6451 * Tell initialization uCode where to find runtime uCode.
6452 *
6453 * BSM registers initially contain pointers to initialization uCode.
6454 * We need to replace them to load runtime uCode inst and data,
6455 * and to save runtime data when powering down.
6456 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006457static int iwl4965_set_ucode_ptrs(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006458{
6459 dma_addr_t pinst;
6460 dma_addr_t pdata;
6461 int rc = 0;
6462 unsigned long flags;
6463
6464 /* bits 35:4 for 4965 */
6465 pinst = priv->ucode_code.p_addr >> 4;
6466 pdata = priv->ucode_data_backup.p_addr >> 4;
6467
6468 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006469 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006470 if (rc) {
6471 spin_unlock_irqrestore(&priv->lock, flags);
6472 return rc;
6473 }
6474
6475 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006476 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6477 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6478 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006479 priv->ucode_data.len);
6480
6481 /* Inst bytecount must be last to set up, bit 31 signals uCode
6482 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006483 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006484 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6485
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006486 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006487
6488 spin_unlock_irqrestore(&priv->lock, flags);
6489
6490 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6491
6492 return rc;
6493}
6494
6495/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006496 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006497 *
6498 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6499 *
6500 * The 4965 "initialize" ALIVE reply contains calibration data for:
6501 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6502 * (3945 does not contain this data).
6503 *
6504 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6505*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006506static void iwl4965_init_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006507{
6508 /* Check alive response for "valid" sign from uCode */
6509 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6510 /* We had an error bringing up the hardware, so take it
6511 * all the way back down so we can try again */
6512 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6513 goto restart;
6514 }
6515
6516 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6517 * This is a paranoid check, because we would not have gotten the
6518 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006519 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006520 /* Runtime instruction load was bad;
6521 * take it all the way back down so we can try again */
6522 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6523 goto restart;
6524 }
6525
6526 /* Calculate temperature */
6527 priv->temperature = iwl4965_get_temperature(priv);
6528
6529 /* Send pointers to protocol/runtime uCode image ... init code will
6530 * load and launch runtime uCode, which will send us another "Alive"
6531 * notification. */
6532 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006533 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006534 /* Runtime instruction load won't happen;
6535 * take it all the way back down so we can try again */
6536 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6537 goto restart;
6538 }
6539 return;
6540
6541 restart:
6542 queue_work(priv->workqueue, &priv->restart);
6543}
6544
6545
6546/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006547 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006548 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006549 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07006550 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006551static void iwl4965_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006552{
6553 int rc = 0;
6554
6555 IWL_DEBUG_INFO("Runtime Alive received.\n");
6556
6557 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6558 /* We had an error bringing up the hardware, so take it
6559 * all the way back down so we can try again */
6560 IWL_DEBUG_INFO("Alive failed.\n");
6561 goto restart;
6562 }
6563
6564 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6565 * This is a paranoid check, because we would not have gotten the
6566 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006567 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006568 /* Runtime instruction load was bad;
6569 * take it all the way back down so we can try again */
6570 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6571 goto restart;
6572 }
6573
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006574 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006575
6576 rc = iwl4965_alive_notify(priv);
6577 if (rc) {
6578 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6579 rc);
6580 goto restart;
6581 }
6582
Ben Cahill9fbab512007-11-29 11:09:47 +08006583 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07006584 set_bit(STATUS_ALIVE, &priv->status);
6585
6586 /* Clear out the uCode error bit if it is set */
6587 clear_bit(STATUS_FW_ERROR, &priv->status);
6588
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006589 rc = iwl4965_init_channel_map(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006590 if (rc) {
6591 IWL_ERROR("initializing regulatory failed: %d\n", rc);
6592 return;
6593 }
6594
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006595 iwl4965_init_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006597 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006598 return;
6599
6600 if (!priv->mac80211_registered) {
6601 /* Unlock so any user space entry points can call back into
6602 * the driver without a deadlock... */
6603 mutex_unlock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006604 iwl4965_rate_control_register(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006605 rc = ieee80211_register_hw(priv->hw);
6606 priv->hw->conf.beacon_int = 100;
6607 mutex_lock(&priv->mutex);
6608
6609 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006610 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006611 IWL_ERROR("Failed to register network "
6612 "device (error %d)\n", rc);
6613 return;
6614 }
6615
6616 priv->mac80211_registered = 1;
6617
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006618 iwl4965_reset_channel_flag(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006619 } else
6620 ieee80211_start_queues(priv->hw);
6621
6622 priv->active_rate = priv->rates_mask;
6623 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6624
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006625 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07006626
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006627 if (iwl4965_is_associated(priv)) {
6628 struct iwl4965_rxon_cmd *active_rxon =
6629 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07006630
6631 memcpy(&priv->staging_rxon, &priv->active_rxon,
6632 sizeof(priv->staging_rxon));
6633 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6634 } else {
6635 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006636 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006637 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6638 }
6639
Ben Cahill9fbab512007-11-29 11:09:47 +08006640 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006641 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006642
6643 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006644 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006645
6646 /* At this point, the NIC is initialized and operational */
6647 priv->notif_missed_beacons = 0;
6648 set_bit(STATUS_READY, &priv->status);
6649
6650 iwl4965_rf_kill_ct_config(priv);
6651 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6652
6653 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006654 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006655
6656 return;
6657
6658 restart:
6659 queue_work(priv->workqueue, &priv->restart);
6660}
6661
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006662static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07006663
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006664static void __iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006665{
6666 unsigned long flags;
6667 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6668 struct ieee80211_conf *conf = NULL;
6669
6670 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6671
6672 conf = ieee80211_get_hw_conf(priv->hw);
6673
6674 if (!exit_pending)
6675 set_bit(STATUS_EXIT_PENDING, &priv->status);
6676
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006677 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006678
6679 /* Unblock any waiting calls */
6680 wake_up_interruptible_all(&priv->wait_command_queue);
6681
Zhu Yib481de92007-09-25 17:54:57 -07006682 /* Wipe out the EXIT_PENDING status bit if we are not actually
6683 * exiting the module */
6684 if (!exit_pending)
6685 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6686
6687 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006688 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07006689
6690 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006691 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006692
6693 if (priv->mac80211_registered)
6694 ieee80211_stop_queues(priv->hw);
6695
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006696 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07006697 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006698 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006699 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6700 STATUS_RF_KILL_HW |
6701 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6702 STATUS_RF_KILL_SW |
6703 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6704 STATUS_IN_SUSPEND;
6705 goto exit;
6706 }
6707
6708 /* ...otherwise clear out all the status bits but the RF Kill and
6709 * SUSPEND bits and continue taking the NIC down. */
6710 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6711 STATUS_RF_KILL_HW |
6712 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6713 STATUS_RF_KILL_SW |
6714 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6715 STATUS_IN_SUSPEND |
6716 test_bit(STATUS_FW_ERROR, &priv->status) <<
6717 STATUS_FW_ERROR;
6718
6719 spin_lock_irqsave(&priv->lock, flags);
Ben Cahill9fbab512007-11-29 11:09:47 +08006720 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
6721 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07006722 spin_unlock_irqrestore(&priv->lock, flags);
6723
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006724 iwl4965_hw_txq_ctx_stop(priv);
6725 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006726
6727 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006728 if (!iwl4965_grab_nic_access(priv)) {
6729 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006730 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006731 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006732 }
6733 spin_unlock_irqrestore(&priv->lock, flags);
6734
6735 udelay(5);
6736
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006737 iwl4965_hw_nic_stop_master(priv);
6738 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6739 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006740
6741 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006742 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07006743
6744 if (priv->ibss_beacon)
6745 dev_kfree_skb(priv->ibss_beacon);
6746 priv->ibss_beacon = NULL;
6747
6748 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006749 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006750}
6751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006752static void iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006753{
6754 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006755 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006756 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006757
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006758 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006759}
6760
6761#define MAX_HW_RESTARTS 5
6762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006763static int __iwl4965_up(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006764{
Joe Perches0795af52007-10-03 17:59:30 -07006765 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006766 int rc, i;
6767 u32 hw_rf_kill = 0;
6768
6769 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6770 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6771 return -EIO;
6772 }
6773
6774 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6775 IWL_WARNING("Radio disabled by SW RF kill (module "
6776 "parameter)\n");
6777 return 0;
6778 }
6779
Reinette Chatrea781cf92008-01-21 10:08:31 -08006780 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6781 IWL_ERROR("ucode not available for device bringup\n");
6782 return -EIO;
6783 }
6784
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006785 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006786
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006787 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006788 if (rc) {
6789 IWL_ERROR("Unable to int nic\n");
6790 return rc;
6791 }
6792
6793 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006794 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6795 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006796 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6797
6798 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006799 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6800 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006801
6802 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006803 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6804 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006805
6806 /* Copy original ucode data image from disk into backup cache.
6807 * This will be used to initialize the on-board processor's
6808 * data SRAM for a clean start when the runtime program first loads. */
6809 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6810 priv->ucode_data.len);
6811
6812 /* If platform's RF_KILL switch is set to KILL,
6813 * wait for BIT_INT_RF_KILL interrupt before loading uCode
6814 * and getting things started */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006815 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07006816 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
6817 hw_rf_kill = 1;
6818
6819 if (test_bit(STATUS_RF_KILL_HW, &priv->status) || hw_rf_kill) {
6820 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6821 return 0;
6822 }
6823
6824 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6825
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006826 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006827
6828 /* load bootstrap state machine,
6829 * load bootstrap program into processor's memory,
6830 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006831 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006832
6833 if (rc) {
6834 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6835 continue;
6836 }
6837
6838 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006839 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006840
Ben Cahill9fbab512007-11-29 11:09:47 +08006841 /* MAC Address location in EEPROM is same for 3945/4965 */
Zhu Yib481de92007-09-25 17:54:57 -07006842 get_eeprom_mac(priv, priv->mac_addr);
Joe Perches0795af52007-10-03 17:59:30 -07006843 IWL_DEBUG_INFO("MAC address: %s\n",
6844 print_mac(mac, priv->mac_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006845
6846 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6847
6848 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6849
6850 return 0;
6851 }
6852
6853 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006854 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006855
6856 /* tried to restart and config the device for as long as our
6857 * patience could withstand */
6858 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6859 return -EIO;
6860}
6861
6862
6863/*****************************************************************************
6864 *
6865 * Workqueue callbacks
6866 *
6867 *****************************************************************************/
6868
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006869static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006870{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006871 struct iwl4965_priv *priv =
6872 container_of(data, struct iwl4965_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006873
6874 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6875 return;
6876
6877 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006878 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006879 mutex_unlock(&priv->mutex);
6880}
6881
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006882static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006883{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006884 struct iwl4965_priv *priv =
6885 container_of(data, struct iwl4965_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006886
6887 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6888 return;
6889
6890 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006891 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006892 mutex_unlock(&priv->mutex);
6893}
6894
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006895static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006896{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006897 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006898
6899 wake_up_interruptible(&priv->wait_command_queue);
6900
6901 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6902 return;
6903
6904 mutex_lock(&priv->mutex);
6905
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006906 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006907 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6908 "HW and/or SW RF Kill no longer active, restarting "
6909 "device\n");
6910 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6911 queue_work(priv->workqueue, &priv->restart);
6912 } else {
6913
6914 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6915 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6916 "disabled by SW switch\n");
6917 else
6918 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6919 "Kill switch must be turned off for "
6920 "wireless networking to work.\n");
6921 }
6922 mutex_unlock(&priv->mutex);
6923}
6924
6925#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006927static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006928{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006929 struct iwl4965_priv *priv =
6930 container_of(data, struct iwl4965_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006931
6932 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6933 return;
6934
6935 mutex_lock(&priv->mutex);
6936 if (test_bit(STATUS_SCANNING, &priv->status) ||
6937 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6938 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6939 "Scan completion watchdog resetting adapter (%dms)\n",
6940 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006941
Zhu Yib481de92007-09-25 17:54:57 -07006942 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006943 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006944 }
6945 mutex_unlock(&priv->mutex);
6946}
6947
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006948static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006949{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006950 struct iwl4965_priv *priv =
6951 container_of(data, struct iwl4965_priv, request_scan);
6952 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006953 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006954 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006955 .meta.flags = CMD_SIZE_HUGE,
6956 };
6957 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006958 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006959 struct ieee80211_conf *conf = NULL;
6960 u8 direct_mask;
6961 int phymode;
6962
6963 conf = ieee80211_get_hw_conf(priv->hw);
6964
6965 mutex_lock(&priv->mutex);
6966
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006967 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006968 IWL_WARNING("request scan called when driver not ready.\n");
6969 goto done;
6970 }
6971
6972 /* Make sure the scan wasn't cancelled before this queued work
6973 * was given the chance to run... */
6974 if (!test_bit(STATUS_SCANNING, &priv->status))
6975 goto done;
6976
6977 /* This should never be called or scheduled if there is currently
6978 * a scan active in the hardware. */
6979 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6980 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6981 "Ignoring second request.\n");
6982 rc = -EIO;
6983 goto done;
6984 }
6985
6986 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6987 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6988 goto done;
6989 }
6990
6991 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6992 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6993 goto done;
6994 }
6995
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006996 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006997 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6998 goto done;
6999 }
7000
7001 if (!test_bit(STATUS_READY, &priv->status)) {
7002 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
7003 goto done;
7004 }
7005
7006 if (!priv->scan_bands) {
7007 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
7008 goto done;
7009 }
7010
7011 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007012 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07007013 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
7014 if (!priv->scan) {
7015 rc = -ENOMEM;
7016 goto done;
7017 }
7018 }
7019 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007020 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07007021
7022 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
7023 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
7024
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007025 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007026 u16 interval = 0;
7027 u32 extra;
7028 u32 suspend_time = 100;
7029 u32 scan_suspend_time = 100;
7030 unsigned long flags;
7031
7032 IWL_DEBUG_INFO("Scanning while associated...\n");
7033
7034 spin_lock_irqsave(&priv->lock, flags);
7035 interval = priv->beacon_int;
7036 spin_unlock_irqrestore(&priv->lock, flags);
7037
7038 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08007039 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07007040 if (!interval)
7041 interval = suspend_time;
7042
7043 extra = (suspend_time / interval) << 22;
7044 scan_suspend_time = (extra |
7045 ((suspend_time % interval) * 1024));
7046 scan->suspend_time = cpu_to_le32(scan_suspend_time);
7047 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
7048 scan_suspend_time, interval);
7049 }
7050
7051 /* We should add the ability for user to lock to PASSIVE ONLY */
7052 if (priv->one_direct_scan) {
7053 IWL_DEBUG_SCAN
7054 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007055 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07007056 priv->direct_ssid_len));
7057 scan->direct_scan[0].id = WLAN_EID_SSID;
7058 scan->direct_scan[0].len = priv->direct_ssid_len;
7059 memcpy(scan->direct_scan[0].ssid,
7060 priv->direct_ssid, priv->direct_ssid_len);
7061 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007062 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07007063 scan->direct_scan[0].id = WLAN_EID_SSID;
7064 scan->direct_scan[0].len = priv->essid_len;
7065 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
7066 direct_mask = 1;
7067 } else
7068 direct_mask = 0;
7069
7070 /* We don't build a direct scan probe request; the uCode will do
7071 * that based on the direct_mask added to each channel entry */
7072 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007073 iwl4965_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Zhu Yib481de92007-09-25 17:54:57 -07007074 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
7075 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
7076 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
7077 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
7078
7079 /* flags + rate selection */
7080
7081 scan->tx_cmd.tx_flags |= cpu_to_le32(0x200);
7082
7083 switch (priv->scan_bands) {
7084 case 2:
7085 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
7086 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007087 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07007088 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
7089
7090 scan->good_CRC_th = 0;
7091 phymode = MODE_IEEE80211G;
7092 break;
7093
7094 case 1:
7095 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007096 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07007097 RATE_MCS_ANT_B_MSK);
7098 scan->good_CRC_th = IWL_GOOD_CRC_TH;
7099 phymode = MODE_IEEE80211A;
7100 break;
7101
7102 default:
7103 IWL_WARNING("Invalid scan band count\n");
7104 goto done;
7105 }
7106
7107 /* select Rx chains */
7108
7109 /* Force use of chains B and C (0x6) for scan Rx.
7110 * Avoid A (0x1) because of its off-channel reception on A-band.
7111 * MIMO is not used here, but value is required to make uCode happy. */
7112 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
7113 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
7114 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
7115 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
7116
7117 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
7118 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
7119
7120 if (direct_mask)
7121 IWL_DEBUG_SCAN
7122 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007123 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07007124 else
7125 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
7126
7127 scan->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007128 iwl4965_get_channels_for_scan(
Zhu Yib481de92007-09-25 17:54:57 -07007129 priv, phymode, 1, /* active */
7130 direct_mask,
7131 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
7132
7133 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007134 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007135 cmd.data = scan;
7136 scan->len = cpu_to_le16(cmd.len);
7137
7138 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007139 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07007140 if (rc)
7141 goto done;
7142
7143 queue_delayed_work(priv->workqueue, &priv->scan_check,
7144 IWL_SCAN_CHECK_WATCHDOG);
7145
7146 mutex_unlock(&priv->mutex);
7147 return;
7148
7149 done:
Ian Schram01ebd062007-10-25 17:15:22 +08007150 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07007151 queue_work(priv->workqueue, &priv->scan_completed);
7152 mutex_unlock(&priv->mutex);
7153}
7154
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007155static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007156{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007157 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07007158
7159 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7160 return;
7161
7162 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007163 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007164 mutex_unlock(&priv->mutex);
7165}
7166
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007167static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007168{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007169 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07007170
7171 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7172 return;
7173
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007174 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007175 queue_work(priv->workqueue, &priv->up);
7176}
7177
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007178static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007179{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007180 struct iwl4965_priv *priv =
7181 container_of(data, struct iwl4965_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07007182
7183 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7184 return;
7185
7186 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007187 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007188 mutex_unlock(&priv->mutex);
7189}
7190
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007191#define IWL_DELAY_NEXT_SCAN (HZ*2)
7192
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007193static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007194{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007195 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv,
Zhu Yib481de92007-09-25 17:54:57 -07007196 post_associate.work);
7197
7198 int rc = 0;
7199 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07007200 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007201
7202 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7203 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
7204 return;
7205 }
7206
Joe Perches0795af52007-10-03 17:59:30 -07007207 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
7208 priv->assoc_id,
7209 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07007210
7211
7212 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7213 return;
7214
7215 mutex_lock(&priv->mutex);
7216
Mohamed Abbas948c1712007-10-25 17:15:45 +08007217 if (!priv->interface_id || !priv->is_open) {
7218 mutex_unlock(&priv->mutex);
7219 return;
7220 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007221 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08007222
Zhu Yib481de92007-09-25 17:54:57 -07007223 conf = ieee80211_get_hw_conf(priv->hw);
7224
7225 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007226 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007227
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007228 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7229 iwl4965_setup_rxon_timing(priv);
7230 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007231 sizeof(priv->rxon_timing), &priv->rxon_timing);
7232 if (rc)
7233 IWL_WARNING("REPLY_RXON_TIMING failed - "
7234 "Attempting to continue.\n");
7235
7236 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7237
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007238#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007239 if (priv->current_ht_config.is_ht)
7240 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007241#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007242 iwl4965_set_rxon_chain(priv);
7243 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7244
7245 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
7246 priv->assoc_id, priv->beacon_int);
7247
7248 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7249 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7250 else
7251 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7252
7253 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7254 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
7255 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
7256 else
7257 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7258
7259 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7260 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7261
7262 }
7263
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007264 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007265
7266 switch (priv->iw_mode) {
7267 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007268 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007269 break;
7270
7271 case IEEE80211_IF_TYPE_IBSS:
7272
7273 /* clear out the station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007274 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007275
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007276 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
7277 iwl4965_rxon_add_station(priv, priv->bssid, 0);
7278 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
7279 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007280
7281 break;
7282
7283 default:
7284 IWL_ERROR("%s Should not be called in %d mode\n",
7285 __FUNCTION__, priv->iw_mode);
7286 break;
7287 }
7288
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007289 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007290
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007291#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07007292 /* Enable Rx differential gain and sensitivity calibrations */
7293 iwl4965_chain_noise_reset(priv);
7294 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007295#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07007296
7297 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7298 priv->assoc_station_added = 1;
7299
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007300#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007301 iwl4965_activate_qos(priv, 0);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007302#endif /* CONFIG_IWL4965_QOS */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007303 /* we have just associated, don't start scan too early */
7304 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07007305 mutex_unlock(&priv->mutex);
7306}
7307
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007308static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07007309{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007310 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07007311
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007312 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007313 return;
7314
7315 mutex_lock(&priv->mutex);
7316
7317 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007318 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007319
7320 mutex_unlock(&priv->mutex);
7321}
7322
Zhu Yi76bb77e2007-11-22 10:53:22 +08007323static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
7324
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007325static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07007326{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007327 struct iwl4965_priv *priv =
7328 container_of(work, struct iwl4965_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07007329
7330 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
7331
7332 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7333 return;
7334
Zhu Yi76bb77e2007-11-22 10:53:22 +08007335 if (priv->cache_conf)
7336 iwl4965_mac_config(priv->hw, priv->cache_conf);
7337
Zhu Yib481de92007-09-25 17:54:57 -07007338 ieee80211_scan_completed(priv->hw);
7339
7340 /* Since setting the TXPOWER may have been deferred while
7341 * performing the scan, fire one off */
7342 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007343 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007344 mutex_unlock(&priv->mutex);
7345}
7346
7347/*****************************************************************************
7348 *
7349 * mac80211 entry point functions
7350 *
7351 *****************************************************************************/
7352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007353static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007354{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007355 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007356
7357 IWL_DEBUG_MAC80211("enter\n");
7358
7359 /* we should be verifying the device is ready to be opened */
7360 mutex_lock(&priv->mutex);
7361
7362 priv->is_open = 1;
7363
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007364 if (!iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007365 ieee80211_start_queues(priv->hw);
7366
7367 mutex_unlock(&priv->mutex);
7368 IWL_DEBUG_MAC80211("leave\n");
7369 return 0;
7370}
7371
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007372static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007373{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007374 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007375
7376 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08007377
7378
7379 mutex_lock(&priv->mutex);
7380 /* stop mac, cancel any scan request and clear
7381 * RXON_FILTER_ASSOC_MSK BIT
7382 */
Zhu Yib481de92007-09-25 17:54:57 -07007383 priv->is_open = 0;
Mohamed Abbasfde35712007-11-29 11:10:15 +08007384 if (!iwl4965_is_ready_rf(priv)) {
7385 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7386 mutex_unlock(&priv->mutex);
7387 return;
7388 }
7389
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007390 iwl4965_scan_cancel_timeout(priv, 100);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007391 cancel_delayed_work(&priv->post_associate);
7392 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007393 iwl4965_commit_rxon(priv);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007394 mutex_unlock(&priv->mutex);
7395
Zhu Yib481de92007-09-25 17:54:57 -07007396 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007397}
7398
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007399static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007400 struct ieee80211_tx_control *ctl)
7401{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007402 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007403
7404 IWL_DEBUG_MAC80211("enter\n");
7405
7406 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7407 IWL_DEBUG_MAC80211("leave - monitor\n");
7408 return -1;
7409 }
7410
7411 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
7412 ctl->tx_rate);
7413
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007414 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07007415 dev_kfree_skb_any(skb);
7416
7417 IWL_DEBUG_MAC80211("leave\n");
7418 return 0;
7419}
7420
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007421static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007422 struct ieee80211_if_init_conf *conf)
7423{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007424 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007425 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07007426 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007427
7428 IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007429
7430 if (priv->interface_id) {
7431 IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
7432 return 0;
7433 }
7434
7435 spin_lock_irqsave(&priv->lock, flags);
7436 priv->interface_id = conf->if_id;
7437
7438 spin_unlock_irqrestore(&priv->lock, flags);
7439
7440 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02007441
7442 if (conf->mac_addr) {
7443 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7444 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7445 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007446 iwl4965_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007447
7448 IWL_DEBUG_MAC80211("leave\n");
7449 mutex_unlock(&priv->mutex);
7450
7451 return 0;
7452}
7453
7454/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007455 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07007456 *
7457 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7458 * be set inappropriately and the driver currently sets the hardware up to
7459 * use it whenever needed.
7460 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007461static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007462{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007463 struct iwl4965_priv *priv = hw->priv;
7464 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07007465 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08007466 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007467
7468 mutex_lock(&priv->mutex);
7469 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
7470
Zhu Yi12342c42007-12-20 11:27:32 +08007471 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
7472
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007473 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007474 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007475 ret = -EIO;
7476 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007477 }
7478
7479 /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
Ian Schram01ebd062007-10-25 17:15:22 +08007480 * what is exposed through include/ declarations */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007481 if (unlikely(!iwl4965_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07007482 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yi76bb77e2007-11-22 10:53:22 +08007483
7484 if (unlikely(priv->cache_conf))
7485 IWL_DEBUG_MAC80211("leave - still scanning\n");
7486 else {
7487 /* Cache the configuration now so that we can
7488 * replay it after the hardware scan is finished. */
7489 priv->cache_conf = kmalloc(sizeof(*conf), GFP_KERNEL);
7490 if (priv->cache_conf) {
7491 memcpy(priv->cache_conf, conf, sizeof(*conf));
7492 IWL_DEBUG_MAC80211("leave - scanning\n");
7493 } else {
7494 IWL_DEBUG_MAC80211("leave - no memory\n");
7495 ret = -ENOMEM;
7496 }
7497 }
Zhu Yib481de92007-09-25 17:54:57 -07007498 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007499 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007500 }
7501
7502 spin_lock_irqsave(&priv->lock, flags);
7503
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007504 ch_info = iwl4965_get_channel_info(priv, conf->phymode, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07007505 if (!is_channel_valid(ch_info)) {
7506 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
7507 conf->channel, conf->phymode);
7508 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7509 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007510 ret = -EINVAL;
7511 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007512 }
7513
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007514#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007515 /* if we are switching fron ht to 2.4 clear flags
7516 * from any ht related info since 2.4 does not
7517 * support ht */
7518 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel)
7519#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7520 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7521#endif
7522 )
7523 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007524#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007525
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007526 iwl4965_set_rxon_channel(priv, conf->phymode, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07007527
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007528 iwl4965_set_flags_for_phymode(priv, conf->phymode);
Zhu Yib481de92007-09-25 17:54:57 -07007529
7530 /* The list of supported rates and rate mask can be different
7531 * for each phymode; since the phymode may have changed, reset
7532 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007533 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007534
7535 spin_unlock_irqrestore(&priv->lock, flags);
7536
7537#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7538 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007539 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007540 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007541 }
7542#endif
7543
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007544 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07007545
7546 if (!conf->radio_enabled) {
7547 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007548 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007549 }
7550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007551 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007552 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007553 ret = -EIO;
7554 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007555 }
7556
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007557 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007558
7559 if (memcmp(&priv->active_rxon,
7560 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007561 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007562 else
7563 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7564
7565 IWL_DEBUG_MAC80211("leave\n");
7566
Zhu Yi76bb77e2007-11-22 10:53:22 +08007567out:
7568 if (priv->cache_conf) {
7569 kfree(priv->cache_conf);
7570 priv->cache_conf = NULL;
7571 }
Zhu Yib481de92007-09-25 17:54:57 -07007572 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007573 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007574}
7575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007576static void iwl4965_config_ap(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007577{
7578 int rc = 0;
7579
7580 if (priv->status & STATUS_EXIT_PENDING)
7581 return;
7582
7583 /* The following should be done only at AP bring up */
7584 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7585
7586 /* RXON - unassoc (to set timing command) */
7587 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007588 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007589
7590 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007591 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7592 iwl4965_setup_rxon_timing(priv);
7593 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007594 sizeof(priv->rxon_timing), &priv->rxon_timing);
7595 if (rc)
7596 IWL_WARNING("REPLY_RXON_TIMING failed - "
7597 "Attempting to continue.\n");
7598
7599 iwl4965_set_rxon_chain(priv);
7600
7601 /* FIXME: what should be the assoc_id for AP? */
7602 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7603 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7604 priv->staging_rxon.flags |=
7605 RXON_FLG_SHORT_PREAMBLE_MSK;
7606 else
7607 priv->staging_rxon.flags &=
7608 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7609
7610 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7611 if (priv->assoc_capability &
7612 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7613 priv->staging_rxon.flags |=
7614 RXON_FLG_SHORT_SLOT_MSK;
7615 else
7616 priv->staging_rxon.flags &=
7617 ~RXON_FLG_SHORT_SLOT_MSK;
7618
7619 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7620 priv->staging_rxon.flags &=
7621 ~RXON_FLG_SHORT_SLOT_MSK;
7622 }
7623 /* restore RXON assoc */
7624 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007625 iwl4965_commit_rxon(priv);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007626#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007627 iwl4965_activate_qos(priv, 1);
Zhu Yib481de92007-09-25 17:54:57 -07007628#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007629 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08007630 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007631 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007632
7633 /* FIXME - we need to add code here to detect a totally new
7634 * configuration, reset the AP, unassoc, rxon timing, assoc,
7635 * clear sta table, add BCAST sta... */
7636}
7637
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007638static int iwl4965_mac_config_interface(struct ieee80211_hw *hw, int if_id,
Zhu Yib481de92007-09-25 17:54:57 -07007639 struct ieee80211_if_conf *conf)
7640{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007641 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007642 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007643 unsigned long flags;
7644 int rc;
7645
7646 if (conf == NULL)
7647 return -EIO;
7648
7649 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7650 (!conf->beacon || !conf->ssid_len)) {
7651 IWL_DEBUG_MAC80211
7652 ("Leaving in AP mode because HostAPD is not ready.\n");
7653 return 0;
7654 }
7655
7656 mutex_lock(&priv->mutex);
7657
7658 IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
7659 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007660 IWL_DEBUG_MAC80211("bssid: %s\n",
7661 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007662
Johannes Berg4150c572007-09-17 01:29:23 -04007663/*
7664 * very dubious code was here; the probe filtering flag is never set:
7665 *
Zhu Yib481de92007-09-25 17:54:57 -07007666 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7667 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007668 */
7669 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yib481de92007-09-25 17:54:57 -07007670 IWL_DEBUG_MAC80211("leave - scanning\n");
7671 mutex_unlock(&priv->mutex);
7672 return 0;
7673 }
7674
7675 if (priv->interface_id != if_id) {
7676 IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
7677 mutex_unlock(&priv->mutex);
7678 return 0;
7679 }
7680
7681 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7682 if (!conf->bssid) {
7683 conf->bssid = priv->mac_addr;
7684 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007685 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7686 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007687 }
7688 if (priv->ibss_beacon)
7689 dev_kfree_skb(priv->ibss_beacon);
7690
7691 priv->ibss_beacon = conf->beacon;
7692 }
7693
Mohamed Abbasfde35712007-11-29 11:10:15 +08007694 if (iwl4965_is_rfkill(priv))
7695 goto done;
7696
Zhu Yib481de92007-09-25 17:54:57 -07007697 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7698 !is_multicast_ether_addr(conf->bssid)) {
7699 /* If there is currently a HW scan going on in the background
7700 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007701 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007702 IWL_WARNING("Aborted scan still in progress "
7703 "after 100ms\n");
7704 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7705 mutex_unlock(&priv->mutex);
7706 return -EAGAIN;
7707 }
7708 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7709
7710 /* TODO: Audit driver for usage of these members and see
7711 * if mac80211 deprecates them (priv->bssid looks like it
7712 * shouldn't be there, but I haven't scanned the IBSS code
7713 * to verify) - jpk */
7714 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7715
7716 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007717 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007718 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007719 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007720 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007721 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07007722 priv, priv->active_rxon.bssid_addr, 1);
7723 }
7724
7725 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007726 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007727 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007728 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007729 }
7730
Mohamed Abbasfde35712007-11-29 11:10:15 +08007731 done:
Zhu Yib481de92007-09-25 17:54:57 -07007732 spin_lock_irqsave(&priv->lock, flags);
7733 if (!conf->ssid_len)
7734 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7735 else
7736 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7737
7738 priv->essid_len = conf->ssid_len;
7739 spin_unlock_irqrestore(&priv->lock, flags);
7740
7741 IWL_DEBUG_MAC80211("leave\n");
7742 mutex_unlock(&priv->mutex);
7743
7744 return 0;
7745}
7746
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007747static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007748 unsigned int changed_flags,
7749 unsigned int *total_flags,
7750 int mc_count, struct dev_addr_list *mc_list)
7751{
7752 /*
7753 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007754 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007755 */
7756 *total_flags = 0;
7757}
7758
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007759static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007760 struct ieee80211_if_init_conf *conf)
7761{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007762 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007763
7764 IWL_DEBUG_MAC80211("enter\n");
7765
7766 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007767
Mohamed Abbasfde35712007-11-29 11:10:15 +08007768 if (iwl4965_is_ready_rf(priv)) {
7769 iwl4965_scan_cancel_timeout(priv, 100);
7770 cancel_delayed_work(&priv->post_associate);
7771 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7772 iwl4965_commit_rxon(priv);
7773 }
Zhu Yib481de92007-09-25 17:54:57 -07007774 if (priv->interface_id == conf->if_id) {
7775 priv->interface_id = 0;
7776 memset(priv->bssid, 0, ETH_ALEN);
7777 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7778 priv->essid_len = 0;
7779 }
7780 mutex_unlock(&priv->mutex);
7781
7782 IWL_DEBUG_MAC80211("leave\n");
7783
7784}
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007785static void iwl4965_mac_erp_ie_changed(struct ieee80211_hw *hw,
Tomas Winkler220173b2007-10-16 00:50:25 +02007786 u8 changes, int cts_protection, int preamble)
7787{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007788 struct iwl4965_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02007789
7790 if (changes & IEEE80211_ERP_CHANGE_PREAMBLE) {
7791 if (preamble == WLAN_ERP_PREAMBLE_SHORT)
7792 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7793 else
7794 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7795 }
7796
7797 if (changes & IEEE80211_ERP_CHANGE_PROTECTION) {
Zhu Yi797a54c2007-11-12 11:37:43 +08007798 if (cts_protection && (priv->phymode != MODE_IEEE80211A))
Tomas Winkler220173b2007-10-16 00:50:25 +02007799 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7800 else
7801 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7802 }
7803
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007804 if (iwl4965_is_associated(priv))
7805 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02007806}
Zhu Yib481de92007-09-25 17:54:57 -07007807
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007808static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007809{
7810 int rc = 0;
7811 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007812 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007813
7814 IWL_DEBUG_MAC80211("enter\n");
7815
mabbas052c4b92007-10-25 17:15:43 +08007816 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007817 spin_lock_irqsave(&priv->lock, flags);
7818
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007819 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007820 rc = -EIO;
7821 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7822 goto out_unlock;
7823 }
7824
7825 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7826 rc = -EIO;
7827 IWL_ERROR("ERROR: APs don't scan\n");
7828 goto out_unlock;
7829 }
7830
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007831 /* we don't schedule scan within next_scan_jiffies period */
7832 if (priv->next_scan_jiffies &&
7833 time_after(priv->next_scan_jiffies, jiffies)) {
7834 rc = -EAGAIN;
7835 goto out_unlock;
7836 }
Zhu Yib481de92007-09-25 17:54:57 -07007837 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007838 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7839 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007840 rc = -EAGAIN;
7841 goto out_unlock;
7842 }
7843 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007844 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007845 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007846
7847 priv->one_direct_scan = 1;
7848 priv->direct_ssid_len = (u8)
7849 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7850 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007851 } else
7852 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007854 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007855
7856 IWL_DEBUG_MAC80211("leave\n");
7857
7858out_unlock:
7859 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007860 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007861
7862 return rc;
7863}
7864
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007865static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007866 const u8 *local_addr, const u8 *addr,
7867 struct ieee80211_key_conf *key)
7868{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007869 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007870 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007871 int rc = 0;
7872 u8 sta_id;
7873
7874 IWL_DEBUG_MAC80211("enter\n");
7875
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007876 if (!iwl4965_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007877 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7878 return -EOPNOTSUPP;
7879 }
7880
7881 if (is_zero_ether_addr(addr))
7882 /* only support pairwise keys */
7883 return -EOPNOTSUPP;
7884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007885 sta_id = iwl4965_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07007886 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007887 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7888 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007889 return -EINVAL;
7890 }
7891
7892 mutex_lock(&priv->mutex);
7893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007894 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007895
Zhu Yib481de92007-09-25 17:54:57 -07007896 switch (cmd) {
7897 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007898 rc = iwl4965_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007899 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007900 iwl4965_set_rxon_hwcrypto(priv, 1);
7901 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007902 key->hw_key_idx = sta_id;
7903 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7904 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7905 }
7906 break;
7907 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007908 rc = iwl4965_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007909 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007910 iwl4965_set_rxon_hwcrypto(priv, 0);
7911 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007912 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7913 }
7914 break;
7915 default:
7916 rc = -EINVAL;
7917 }
7918
7919 IWL_DEBUG_MAC80211("leave\n");
7920 mutex_unlock(&priv->mutex);
7921
7922 return rc;
7923}
7924
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007925static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007926 const struct ieee80211_tx_queue_params *params)
7927{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007928 struct iwl4965_priv *priv = hw->priv;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007929#ifdef CONFIG_IWL4965_QOS
Zhu Yib481de92007-09-25 17:54:57 -07007930 unsigned long flags;
7931 int q;
Reinette Chatre0054b342007-11-29 11:09:42 +08007932#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07007933
7934 IWL_DEBUG_MAC80211("enter\n");
7935
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007936 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007937 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7938 return -EIO;
7939 }
7940
7941 if (queue >= AC_NUM) {
7942 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7943 return 0;
7944 }
7945
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007946#ifdef CONFIG_IWL4965_QOS
Zhu Yib481de92007-09-25 17:54:57 -07007947 if (!priv->qos_data.qos_enable) {
7948 priv->qos_data.qos_active = 0;
7949 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7950 return 0;
7951 }
7952 q = AC_NUM - 1 - queue;
7953
7954 spin_lock_irqsave(&priv->lock, flags);
7955
7956 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7957 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7958 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7959 priv->qos_data.def_qos_parm.ac[q].edca_txop =
7960 cpu_to_le16((params->burst_time * 100));
7961
7962 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7963 priv->qos_data.qos_active = 1;
7964
7965 spin_unlock_irqrestore(&priv->lock, flags);
7966
7967 mutex_lock(&priv->mutex);
7968 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007969 iwl4965_activate_qos(priv, 1);
7970 else if (priv->assoc_id && iwl4965_is_associated(priv))
7971 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007972
7973 mutex_unlock(&priv->mutex);
7974
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007975#endif /*CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07007976
7977 IWL_DEBUG_MAC80211("leave\n");
7978 return 0;
7979}
7980
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007981static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007982 struct ieee80211_tx_queue_stats *stats)
7983{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007984 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007985 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007986 struct iwl4965_tx_queue *txq;
7987 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007988 unsigned long flags;
7989
7990 IWL_DEBUG_MAC80211("enter\n");
7991
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007992 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007993 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7994 return -EIO;
7995 }
7996
7997 spin_lock_irqsave(&priv->lock, flags);
7998
7999 for (i = 0; i < AC_NUM; i++) {
8000 txq = &priv->txq[i];
8001 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008002 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07008003
8004 stats->data[i].len = q->n_window - avail;
8005 stats->data[i].limit = q->n_window - q->high_mark;
8006 stats->data[i].count = q->n_window;
8007
8008 }
8009 spin_unlock_irqrestore(&priv->lock, flags);
8010
8011 IWL_DEBUG_MAC80211("leave\n");
8012
8013 return 0;
8014}
8015
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008016static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07008017 struct ieee80211_low_level_stats *stats)
8018{
8019 IWL_DEBUG_MAC80211("enter\n");
8020 IWL_DEBUG_MAC80211("leave\n");
8021
8022 return 0;
8023}
8024
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008025static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07008026{
8027 IWL_DEBUG_MAC80211("enter\n");
8028 IWL_DEBUG_MAC80211("leave\n");
8029
8030 return 0;
8031}
8032
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008033static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07008034{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008035 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07008036 unsigned long flags;
8037
8038 mutex_lock(&priv->mutex);
8039 IWL_DEBUG_MAC80211("enter\n");
8040
8041 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008042#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07008043 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008044 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07008045 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008046#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07008047/* if (priv->lq_mngr.agg_ctrl.granted_ba)
8048 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);*/
8049
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008050 memset(&(priv->lq_mngr.agg_ctrl), 0, sizeof(struct iwl4965_agg_control));
Zhu Yib481de92007-09-25 17:54:57 -07008051 priv->lq_mngr.agg_ctrl.tid_traffic_load_threshold = 10;
8052 priv->lq_mngr.agg_ctrl.ba_timeout = 5000;
8053 priv->lq_mngr.agg_ctrl.auto_agg = 1;
8054
8055 if (priv->lq_mngr.agg_ctrl.auto_agg)
8056 priv->lq_mngr.agg_ctrl.requested_ba = TID_ALL_ENABLED;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008057#endif /*CONFIG_IWL4965_HT_AGG */
8058#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07008059
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008060#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008061 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008062#endif
8063
8064 cancel_delayed_work(&priv->post_associate);
8065
8066 spin_lock_irqsave(&priv->lock, flags);
8067 priv->assoc_id = 0;
8068 priv->assoc_capability = 0;
8069 priv->call_post_assoc_from_beacon = 0;
8070 priv->assoc_station_added = 0;
8071
8072 /* new association get rid of ibss beacon skb */
8073 if (priv->ibss_beacon)
8074 dev_kfree_skb(priv->ibss_beacon);
8075
8076 priv->ibss_beacon = NULL;
8077
8078 priv->beacon_int = priv->hw->conf.beacon_int;
8079 priv->timestamp1 = 0;
8080 priv->timestamp0 = 0;
8081 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
8082 priv->beacon_int = 0;
8083
8084 spin_unlock_irqrestore(&priv->lock, flags);
8085
Mohamed Abbasfde35712007-11-29 11:10:15 +08008086 if (!iwl4965_is_ready_rf(priv)) {
8087 IWL_DEBUG_MAC80211("leave - not ready\n");
8088 mutex_unlock(&priv->mutex);
8089 return;
8090 }
8091
mabbas052c4b92007-10-25 17:15:43 +08008092 /* we are restarting association process
8093 * clear RXON_FILTER_ASSOC_MSK bit
8094 */
8095 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008096 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08008097 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008098 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08008099 }
8100
Zhu Yib481de92007-09-25 17:54:57 -07008101 /* Per mac80211.h: This is only used in IBSS mode... */
8102 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08008103
Zhu Yib481de92007-09-25 17:54:57 -07008104 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
8105 mutex_unlock(&priv->mutex);
8106 return;
8107 }
8108
Zhu Yib481de92007-09-25 17:54:57 -07008109 priv->only_active_channel = 0;
8110
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008111 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008112
8113 mutex_unlock(&priv->mutex);
8114
8115 IWL_DEBUG_MAC80211("leave\n");
8116
8117}
8118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008119static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07008120 struct ieee80211_tx_control *control)
8121{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008122 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07008123 unsigned long flags;
8124
8125 mutex_lock(&priv->mutex);
8126 IWL_DEBUG_MAC80211("enter\n");
8127
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008128 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008129 IWL_DEBUG_MAC80211("leave - RF not ready\n");
8130 mutex_unlock(&priv->mutex);
8131 return -EIO;
8132 }
8133
8134 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
8135 IWL_DEBUG_MAC80211("leave - not IBSS\n");
8136 mutex_unlock(&priv->mutex);
8137 return -EIO;
8138 }
8139
8140 spin_lock_irqsave(&priv->lock, flags);
8141
8142 if (priv->ibss_beacon)
8143 dev_kfree_skb(priv->ibss_beacon);
8144
8145 priv->ibss_beacon = skb;
8146
8147 priv->assoc_id = 0;
8148
8149 IWL_DEBUG_MAC80211("leave\n");
8150 spin_unlock_irqrestore(&priv->lock, flags);
8151
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008152#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008153 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008154#endif
8155
8156 queue_work(priv->workqueue, &priv->post_associate.work);
8157
8158 mutex_unlock(&priv->mutex);
8159
8160 return 0;
8161}
8162
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008163#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07008164
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008165static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
8166 struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008167{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008168 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
8169 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
8170 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07008171
8172 IWL_DEBUG_MAC80211("enter: \n");
8173
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008174 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
8175 iwl_conf->is_ht = 0;
8176 return;
Zhu Yib481de92007-09-25 17:54:57 -07008177 }
8178
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008179 iwl_conf->is_ht = 1;
8180 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07008181
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008182 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
8183 iwl_conf->sgf |= 0x1;
8184 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
8185 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07008186
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008187 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
8188 iwl_conf->max_amsdu_size =
8189 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
8190 iwl_conf->supported_chan_width =
8191 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
8192 iwl_conf->tx_mimo_ps_mode =
8193 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
8194 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07008195
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008196 iwl_conf->control_channel = ht_bss_conf->primary_channel;
8197 iwl_conf->extension_chan_offset =
8198 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
8199 iwl_conf->tx_chan_width =
8200 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
8201 iwl_conf->ht_protection =
8202 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
8203 iwl_conf->non_GF_STA_present =
8204 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07008205
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008206 IWL_DEBUG_MAC80211("control channel %d\n",
8207 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07008208 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07008209}
8210
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008211static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008212 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07008213{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008214 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07008215
8216 IWL_DEBUG_MAC80211("enter: \n");
8217
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008218 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07008219 iwl4965_set_rxon_chain(priv);
8220
8221 if (priv && priv->assoc_id &&
8222 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
8223 unsigned long flags;
8224
8225 spin_lock_irqsave(&priv->lock, flags);
8226 if (priv->beacon_int)
8227 queue_work(priv->workqueue, &priv->post_associate.work);
8228 else
8229 priv->call_post_assoc_from_beacon = 1;
8230 spin_unlock_irqrestore(&priv->lock, flags);
8231 }
8232
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008233 IWL_DEBUG_MAC80211("leave:\n");
8234 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07008235}
8236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008237static void iwl4965_set_ht_capab(struct ieee80211_hw *hw,
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02008238 struct ieee80211_ht_cap *ht_cap,
8239 u8 use_current_config)
Zhu Yib481de92007-09-25 17:54:57 -07008240{
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02008241 struct ieee80211_conf *conf = &hw->conf;
8242 struct ieee80211_hw_mode *mode = conf->mode;
Zhu Yib481de92007-09-25 17:54:57 -07008243
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02008244 if (use_current_config) {
8245 ht_cap->cap_info = cpu_to_le16(conf->ht_conf.cap);
8246 memcpy(ht_cap->supp_mcs_set,
8247 conf->ht_conf.supp_mcs_set, 16);
8248 } else {
8249 ht_cap->cap_info = cpu_to_le16(mode->ht_info.cap);
8250 memcpy(ht_cap->supp_mcs_set,
8251 mode->ht_info.supp_mcs_set, 16);
8252 }
8253 ht_cap->ampdu_params_info =
8254 (mode->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
8255 ((mode->ht_info.ampdu_density << 2) &
8256 IEEE80211_HT_CAP_AMPDU_DENSITY);
Zhu Yib481de92007-09-25 17:54:57 -07008257}
8258
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008259#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07008260
8261/*****************************************************************************
8262 *
8263 * sysfs attributes
8264 *
8265 *****************************************************************************/
8266
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008267#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07008268
8269/*
8270 * The following adds a new attribute to the sysfs representation
8271 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
8272 * used for controlling the debug level.
8273 *
8274 * See the level definitions in iwl for details.
8275 */
8276
8277static ssize_t show_debug_level(struct device_driver *d, char *buf)
8278{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008279 return sprintf(buf, "0x%08X\n", iwl4965_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008280}
8281static ssize_t store_debug_level(struct device_driver *d,
8282 const char *buf, size_t count)
8283{
8284 char *p = (char *)buf;
8285 u32 val;
8286
8287 val = simple_strtoul(p, &p, 0);
8288 if (p == buf)
8289 printk(KERN_INFO DRV_NAME
8290 ": %s is not in hex or decimal form.\n", buf);
8291 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008292 iwl4965_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07008293
8294 return strnlen(buf, count);
8295}
8296
8297static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
8298 show_debug_level, store_debug_level);
8299
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008300#endif /* CONFIG_IWL4965_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07008301
8302static ssize_t show_rf_kill(struct device *d,
8303 struct device_attribute *attr, char *buf)
8304{
8305 /*
8306 * 0 - RF kill not enabled
8307 * 1 - SW based RF kill active (sysfs)
8308 * 2 - HW based RF kill active
8309 * 3 - Both HW and SW based RF kill active
8310 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008311 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008312 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
8313 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
8314
8315 return sprintf(buf, "%i\n", val);
8316}
8317
8318static ssize_t store_rf_kill(struct device *d,
8319 struct device_attribute *attr,
8320 const char *buf, size_t count)
8321{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008322 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008323
8324 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008325 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07008326 mutex_unlock(&priv->mutex);
8327
8328 return count;
8329}
8330
8331static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
8332
8333static ssize_t show_temperature(struct device *d,
8334 struct device_attribute *attr, char *buf)
8335{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008336 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008338 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008339 return -EAGAIN;
8340
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008341 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07008342}
8343
8344static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8345
8346static ssize_t show_rs_window(struct device *d,
8347 struct device_attribute *attr,
8348 char *buf)
8349{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008350 struct iwl4965_priv *priv = d->driver_data;
8351 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07008352}
8353static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8354
8355static ssize_t show_tx_power(struct device *d,
8356 struct device_attribute *attr, char *buf)
8357{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008358 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008359 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8360}
8361
8362static ssize_t store_tx_power(struct device *d,
8363 struct device_attribute *attr,
8364 const char *buf, size_t count)
8365{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008366 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008367 char *p = (char *)buf;
8368 u32 val;
8369
8370 val = simple_strtoul(p, &p, 10);
8371 if (p == buf)
8372 printk(KERN_INFO DRV_NAME
8373 ": %s is not in decimal form.\n", buf);
8374 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008375 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07008376
8377 return count;
8378}
8379
8380static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8381
8382static ssize_t show_flags(struct device *d,
8383 struct device_attribute *attr, char *buf)
8384{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008385 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008386
8387 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8388}
8389
8390static ssize_t store_flags(struct device *d,
8391 struct device_attribute *attr,
8392 const char *buf, size_t count)
8393{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008394 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008395 u32 flags = simple_strtoul(buf, NULL, 0);
8396
8397 mutex_lock(&priv->mutex);
8398 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8399 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008400 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008401 IWL_WARNING("Could not cancel scan.\n");
8402 else {
8403 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8404 flags);
8405 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008406 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008407 }
8408 }
8409 mutex_unlock(&priv->mutex);
8410
8411 return count;
8412}
8413
8414static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8415
8416static ssize_t show_filter_flags(struct device *d,
8417 struct device_attribute *attr, char *buf)
8418{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008419 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008420
8421 return sprintf(buf, "0x%04X\n",
8422 le32_to_cpu(priv->active_rxon.filter_flags));
8423}
8424
8425static ssize_t store_filter_flags(struct device *d,
8426 struct device_attribute *attr,
8427 const char *buf, size_t count)
8428{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008429 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008430 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8431
8432 mutex_lock(&priv->mutex);
8433 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8434 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008435 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008436 IWL_WARNING("Could not cancel scan.\n");
8437 else {
8438 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8439 "0x%04X\n", filter_flags);
8440 priv->staging_rxon.filter_flags =
8441 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008442 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008443 }
8444 }
8445 mutex_unlock(&priv->mutex);
8446
8447 return count;
8448}
8449
8450static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8451 store_filter_flags);
8452
8453static ssize_t show_tune(struct device *d,
8454 struct device_attribute *attr, char *buf)
8455{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008456 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008457
8458 return sprintf(buf, "0x%04X\n",
8459 (priv->phymode << 8) |
8460 le16_to_cpu(priv->active_rxon.channel));
8461}
8462
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008463static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode);
Zhu Yib481de92007-09-25 17:54:57 -07008464
8465static ssize_t store_tune(struct device *d,
8466 struct device_attribute *attr,
8467 const char *buf, size_t count)
8468{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008469 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008470 char *p = (char *)buf;
8471 u16 tune = simple_strtoul(p, &p, 0);
8472 u8 phymode = (tune >> 8) & 0xff;
8473 u16 channel = tune & 0xff;
8474
8475 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
8476
8477 mutex_lock(&priv->mutex);
8478 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
8479 (priv->phymode != phymode)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008480 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07008481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008482 ch_info = iwl4965_get_channel_info(priv, phymode, channel);
Zhu Yib481de92007-09-25 17:54:57 -07008483 if (!ch_info) {
8484 IWL_WARNING("Requested invalid phymode/channel "
8485 "combination: %d %d\n", phymode, channel);
8486 mutex_unlock(&priv->mutex);
8487 return -EINVAL;
8488 }
8489
8490 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008491 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008492 IWL_WARNING("Could not cancel scan.\n");
8493 else {
8494 IWL_DEBUG_INFO("Committing phymode and "
8495 "rxon.channel = %d %d\n",
8496 phymode, channel);
8497
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008498 iwl4965_set_rxon_channel(priv, phymode, channel);
8499 iwl4965_set_flags_for_phymode(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07008500
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008501 iwl4965_set_rate(priv);
8502 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008503 }
8504 }
8505 mutex_unlock(&priv->mutex);
8506
8507 return count;
8508}
8509
8510static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
8511
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008512#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008513
8514static ssize_t show_measurement(struct device *d,
8515 struct device_attribute *attr, char *buf)
8516{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008517 struct iwl4965_priv *priv = dev_get_drvdata(d);
8518 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07008519 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8520 u8 *data = (u8 *) & measure_report;
8521 unsigned long flags;
8522
8523 spin_lock_irqsave(&priv->lock, flags);
8524 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8525 spin_unlock_irqrestore(&priv->lock, flags);
8526 return 0;
8527 }
8528 memcpy(&measure_report, &priv->measure_report, size);
8529 priv->measurement_status = 0;
8530 spin_unlock_irqrestore(&priv->lock, flags);
8531
8532 while (size && (PAGE_SIZE - len)) {
8533 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8534 PAGE_SIZE - len, 1);
8535 len = strlen(buf);
8536 if (PAGE_SIZE - len)
8537 buf[len++] = '\n';
8538
8539 ofs += 16;
8540 size -= min(size, 16U);
8541 }
8542
8543 return len;
8544}
8545
8546static ssize_t store_measurement(struct device *d,
8547 struct device_attribute *attr,
8548 const char *buf, size_t count)
8549{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008550 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008551 struct ieee80211_measurement_params params = {
8552 .channel = le16_to_cpu(priv->active_rxon.channel),
8553 .start_time = cpu_to_le64(priv->last_tsf),
8554 .duration = cpu_to_le16(1),
8555 };
8556 u8 type = IWL_MEASURE_BASIC;
8557 u8 buffer[32];
8558 u8 channel;
8559
8560 if (count) {
8561 char *p = buffer;
8562 strncpy(buffer, buf, min(sizeof(buffer), count));
8563 channel = simple_strtoul(p, NULL, 0);
8564 if (channel)
8565 params.channel = channel;
8566
8567 p = buffer;
8568 while (*p && *p != ' ')
8569 p++;
8570 if (*p)
8571 type = simple_strtoul(p + 1, NULL, 0);
8572 }
8573
8574 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8575 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008576 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07008577
8578 return count;
8579}
8580
8581static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8582 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008583#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07008584
8585static ssize_t store_retry_rate(struct device *d,
8586 struct device_attribute *attr,
8587 const char *buf, size_t count)
8588{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008589 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008590
8591 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8592 if (priv->retry_rate <= 0)
8593 priv->retry_rate = 1;
8594
8595 return count;
8596}
8597
8598static ssize_t show_retry_rate(struct device *d,
8599 struct device_attribute *attr, char *buf)
8600{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008601 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008602 return sprintf(buf, "%d", priv->retry_rate);
8603}
8604
8605static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8606 store_retry_rate);
8607
8608static ssize_t store_power_level(struct device *d,
8609 struct device_attribute *attr,
8610 const char *buf, size_t count)
8611{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008612 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008613 int rc;
8614 int mode;
8615
8616 mode = simple_strtoul(buf, NULL, 0);
8617 mutex_lock(&priv->mutex);
8618
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008619 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008620 rc = -EAGAIN;
8621 goto out;
8622 }
8623
8624 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8625 mode = IWL_POWER_AC;
8626 else
8627 mode |= IWL_POWER_ENABLED;
8628
8629 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008630 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07008631 if (rc) {
8632 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8633 goto out;
8634 }
8635 priv->power_mode = mode;
8636 }
8637
8638 rc = count;
8639
8640 out:
8641 mutex_unlock(&priv->mutex);
8642 return rc;
8643}
8644
8645#define MAX_WX_STRING 80
8646
8647/* Values are in microsecond */
8648static const s32 timeout_duration[] = {
8649 350000,
8650 250000,
8651 75000,
8652 37000,
8653 25000,
8654};
8655static const s32 period_duration[] = {
8656 400000,
8657 700000,
8658 1000000,
8659 1000000,
8660 1000000
8661};
8662
8663static ssize_t show_power_level(struct device *d,
8664 struct device_attribute *attr, char *buf)
8665{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008666 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008667 int level = IWL_POWER_LEVEL(priv->power_mode);
8668 char *p = buf;
8669
8670 p += sprintf(p, "%d ", level);
8671 switch (level) {
8672 case IWL_POWER_MODE_CAM:
8673 case IWL_POWER_AC:
8674 p += sprintf(p, "(AC)");
8675 break;
8676 case IWL_POWER_BATTERY:
8677 p += sprintf(p, "(BATTERY)");
8678 break;
8679 default:
8680 p += sprintf(p,
8681 "(Timeout %dms, Period %dms)",
8682 timeout_duration[level - 1] / 1000,
8683 period_duration[level - 1] / 1000);
8684 }
8685
8686 if (!(priv->power_mode & IWL_POWER_ENABLED))
8687 p += sprintf(p, " OFF\n");
8688 else
8689 p += sprintf(p, " \n");
8690
8691 return (p - buf + 1);
8692
8693}
8694
8695static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8696 store_power_level);
8697
8698static ssize_t show_channels(struct device *d,
8699 struct device_attribute *attr, char *buf)
8700{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008701 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008702 int len = 0, i;
8703 struct ieee80211_channel *channels = NULL;
8704 const struct ieee80211_hw_mode *hw_mode = NULL;
8705 int count = 0;
8706
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008707 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008708 return -EAGAIN;
8709
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008710 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211G);
Zhu Yib481de92007-09-25 17:54:57 -07008711 if (!hw_mode)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008712 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211B);
Zhu Yib481de92007-09-25 17:54:57 -07008713 if (hw_mode) {
8714 channels = hw_mode->channels;
8715 count = hw_mode->num_channels;
8716 }
8717
8718 len +=
8719 sprintf(&buf[len],
8720 "Displaying %d channels in 2.4GHz band "
8721 "(802.11bg):\n", count);
8722
8723 for (i = 0; i < count; i++)
8724 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8725 channels[i].chan,
8726 channels[i].power_level,
8727 channels[i].
8728 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8729 " (IEEE 802.11h required)" : "",
8730 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8731 || (channels[i].
8732 flag &
8733 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8734 ", IBSS",
8735 channels[i].
8736 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8737 "active/passive" : "passive only");
8738
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008739 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211A);
Zhu Yib481de92007-09-25 17:54:57 -07008740 if (hw_mode) {
8741 channels = hw_mode->channels;
8742 count = hw_mode->num_channels;
8743 } else {
8744 channels = NULL;
8745 count = 0;
8746 }
8747
8748 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8749 "(802.11a):\n", count);
8750
8751 for (i = 0; i < count; i++)
8752 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8753 channels[i].chan,
8754 channels[i].power_level,
8755 channels[i].
8756 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8757 " (IEEE 802.11h required)" : "",
8758 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8759 || (channels[i].
8760 flag &
8761 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8762 ", IBSS",
8763 channels[i].
8764 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8765 "active/passive" : "passive only");
8766
8767 return len;
8768}
8769
8770static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8771
8772static ssize_t show_statistics(struct device *d,
8773 struct device_attribute *attr, char *buf)
8774{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008775 struct iwl4965_priv *priv = dev_get_drvdata(d);
8776 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07008777 u32 len = 0, ofs = 0;
8778 u8 *data = (u8 *) & priv->statistics;
8779 int rc = 0;
8780
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008781 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008782 return -EAGAIN;
8783
8784 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008785 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008786 mutex_unlock(&priv->mutex);
8787
8788 if (rc) {
8789 len = sprintf(buf,
8790 "Error sending statistics request: 0x%08X\n", rc);
8791 return len;
8792 }
8793
8794 while (size && (PAGE_SIZE - len)) {
8795 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8796 PAGE_SIZE - len, 1);
8797 len = strlen(buf);
8798 if (PAGE_SIZE - len)
8799 buf[len++] = '\n';
8800
8801 ofs += 16;
8802 size -= min(size, 16U);
8803 }
8804
8805 return len;
8806}
8807
8808static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8809
8810static ssize_t show_antenna(struct device *d,
8811 struct device_attribute *attr, char *buf)
8812{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008813 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008814
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008815 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008816 return -EAGAIN;
8817
8818 return sprintf(buf, "%d\n", priv->antenna);
8819}
8820
8821static ssize_t store_antenna(struct device *d,
8822 struct device_attribute *attr,
8823 const char *buf, size_t count)
8824{
8825 int ant;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008826 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008827
8828 if (count == 0)
8829 return 0;
8830
8831 if (sscanf(buf, "%1i", &ant) != 1) {
8832 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8833 return count;
8834 }
8835
8836 if ((ant >= 0) && (ant <= 2)) {
8837 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008838 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07008839 } else
8840 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8841
8842
8843 return count;
8844}
8845
8846static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8847
8848static ssize_t show_status(struct device *d,
8849 struct device_attribute *attr, char *buf)
8850{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008851 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8852 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008853 return -EAGAIN;
8854 return sprintf(buf, "0x%08x\n", (int)priv->status);
8855}
8856
8857static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8858
8859static ssize_t dump_error_log(struct device *d,
8860 struct device_attribute *attr,
8861 const char *buf, size_t count)
8862{
8863 char *p = (char *)buf;
8864
8865 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008866 iwl4965_dump_nic_error_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008867
8868 return strnlen(buf, count);
8869}
8870
8871static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8872
8873static ssize_t dump_event_log(struct device *d,
8874 struct device_attribute *attr,
8875 const char *buf, size_t count)
8876{
8877 char *p = (char *)buf;
8878
8879 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008880 iwl4965_dump_nic_event_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008881
8882 return strnlen(buf, count);
8883}
8884
8885static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8886
8887/*****************************************************************************
8888 *
8889 * driver setup and teardown
8890 *
8891 *****************************************************************************/
8892
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008893static void iwl4965_setup_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008894{
8895 priv->workqueue = create_workqueue(DRV_NAME);
8896
8897 init_waitqueue_head(&priv->wait_command_queue);
8898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008899 INIT_WORK(&priv->up, iwl4965_bg_up);
8900 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8901 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8902 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8903 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8904 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8905 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8906 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8907 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8908 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8909 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8910 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008911
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008912 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008913
8914 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008915 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008916}
8917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008918static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008919{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008920 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008921
Joonwoo Park3ae6a052007-11-29 10:43:16 +09008922 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008923 cancel_delayed_work(&priv->scan_check);
8924 cancel_delayed_work(&priv->alive_start);
8925 cancel_delayed_work(&priv->post_associate);
8926 cancel_work_sync(&priv->beacon_update);
8927}
8928
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008929static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008930 &dev_attr_antenna.attr,
8931 &dev_attr_channels.attr,
8932 &dev_attr_dump_errors.attr,
8933 &dev_attr_dump_events.attr,
8934 &dev_attr_flags.attr,
8935 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008936#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008937 &dev_attr_measurement.attr,
8938#endif
8939 &dev_attr_power_level.attr,
8940 &dev_attr_retry_rate.attr,
8941 &dev_attr_rf_kill.attr,
8942 &dev_attr_rs_window.attr,
8943 &dev_attr_statistics.attr,
8944 &dev_attr_status.attr,
8945 &dev_attr_temperature.attr,
8946 &dev_attr_tune.attr,
8947 &dev_attr_tx_power.attr,
8948
8949 NULL
8950};
8951
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008952static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008953 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008954 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008955};
8956
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008957static struct ieee80211_ops iwl4965_hw_ops = {
8958 .tx = iwl4965_mac_tx,
8959 .start = iwl4965_mac_start,
8960 .stop = iwl4965_mac_stop,
8961 .add_interface = iwl4965_mac_add_interface,
8962 .remove_interface = iwl4965_mac_remove_interface,
8963 .config = iwl4965_mac_config,
8964 .config_interface = iwl4965_mac_config_interface,
8965 .configure_filter = iwl4965_configure_filter,
8966 .set_key = iwl4965_mac_set_key,
8967 .get_stats = iwl4965_mac_get_stats,
8968 .get_tx_stats = iwl4965_mac_get_tx_stats,
8969 .conf_tx = iwl4965_mac_conf_tx,
8970 .get_tsf = iwl4965_mac_get_tsf,
8971 .reset_tsf = iwl4965_mac_reset_tsf,
8972 .beacon_update = iwl4965_mac_beacon_update,
8973 .erp_ie_changed = iwl4965_mac_erp_ie_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008974#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008975 .conf_ht = iwl4965_mac_conf_ht,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008976#ifdef CONFIG_IWL4965_HT_AGG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008977 .ht_tx_agg_start = iwl4965_mac_ht_tx_agg_start,
8978 .ht_tx_agg_stop = iwl4965_mac_ht_tx_agg_stop,
8979 .ht_rx_agg_start = iwl4965_mac_ht_rx_agg_start,
8980 .ht_rx_agg_stop = iwl4965_mac_ht_rx_agg_stop,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008981#endif /* CONFIG_IWL4965_HT_AGG */
8982#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008983 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008984};
8985
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008986static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008987{
8988 int err = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008989 struct iwl4965_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008990 struct ieee80211_hw *hw;
8991 int i;
8992
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008993 /* Disabling hardware scan means that mac80211 will perform scans
8994 * "the hard way", rather than using device's scan. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008995 if (iwl4965_param_disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008996 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008997 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008998 }
8999
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009000 if ((iwl4965_param_queues_num > IWL_MAX_NUM_QUEUES) ||
9001 (iwl4965_param_queues_num < IWL_MIN_NUM_QUEUES)) {
Zhu Yib481de92007-09-25 17:54:57 -07009002 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
9003 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
9004 err = -EINVAL;
9005 goto out;
9006 }
9007
9008 /* mac80211 allocates memory for this device instance, including
9009 * space for this driver's private structure */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009010 hw = ieee80211_alloc_hw(sizeof(struct iwl4965_priv), &iwl4965_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07009011 if (hw == NULL) {
9012 IWL_ERROR("Can not allocate network device\n");
9013 err = -ENOMEM;
9014 goto out;
9015 }
9016 SET_IEEE80211_DEV(hw, &pdev->dev);
9017
Johannes Bergf51359a2007-10-28 14:53:36 +01009018 hw->rate_control_algorithm = "iwl-4965-rs";
9019
Zhu Yib481de92007-09-25 17:54:57 -07009020 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
9021 priv = hw->priv;
9022 priv->hw = hw;
9023
9024 priv->pci_dev = pdev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009025 priv->antenna = (enum iwl4965_antenna)iwl4965_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009026#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009027 iwl4965_debug_level = iwl4965_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07009028 atomic_set(&priv->restrict_refcnt, 0);
9029#endif
9030 priv->retry_rate = 1;
9031
9032 priv->ibss_beacon = NULL;
9033
9034 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
9035 * the range of signal quality values that we'll provide.
9036 * Negative values for level/noise indicate that we'll provide dBm.
9037 * For WE, at least, non-0 values here *enable* display of values
9038 * in app (iwconfig). */
9039 hw->max_rssi = -20; /* signal level, negative indicates dBm */
9040 hw->max_noise = -20; /* noise level, negative indicates dBm */
9041 hw->max_signal = 100; /* link quality indication (%) */
9042
9043 /* Tell mac80211 our Tx characteristics */
9044 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
9045
Cahill, Ben M6440adb2007-11-29 11:09:55 +08009046 /* Default value; 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07009047 hw->queues = 4;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009048#ifdef CONFIG_IWL4965_HT
9049#ifdef CONFIG_IWL4965_HT_AGG
Cahill, Ben M6440adb2007-11-29 11:09:55 +08009050 /* Enhanced value; more queues, to support 11n aggregation */
Zhu Yib481de92007-09-25 17:54:57 -07009051 hw->queues = 16;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009052#endif /* CONFIG_IWL4965_HT_AGG */
9053#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07009054
9055 spin_lock_init(&priv->lock);
9056 spin_lock_init(&priv->power_data.lock);
9057 spin_lock_init(&priv->sta_lock);
9058 spin_lock_init(&priv->hcmd_lock);
9059 spin_lock_init(&priv->lq_mngr.lock);
9060
9061 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
9062 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
9063
9064 INIT_LIST_HEAD(&priv->free_frames);
9065
9066 mutex_init(&priv->mutex);
9067 if (pci_enable_device(pdev)) {
9068 err = -ENODEV;
9069 goto out_ieee80211_free_hw;
9070 }
9071
9072 pci_set_master(pdev);
9073
Cahill, Ben M6440adb2007-11-29 11:09:55 +08009074 /* Clear the driver's (not device's) station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009075 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009076
9077 priv->data_retry_limit = -1;
9078 priv->ieee_channels = NULL;
9079 priv->ieee_rates = NULL;
9080 priv->phymode = -1;
9081
9082 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
9083 if (!err)
9084 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
9085 if (err) {
9086 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
9087 goto out_pci_disable_device;
9088 }
9089
9090 pci_set_drvdata(pdev, priv);
9091 err = pci_request_regions(pdev, DRV_NAME);
9092 if (err)
9093 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08009094
Zhu Yib481de92007-09-25 17:54:57 -07009095 /* We disable the RETRY_TIMEOUT register (0x41) to keep
9096 * PCI Tx retries from interfering with C3 CPU state */
9097 pci_write_config_byte(pdev, 0x41, 0x00);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08009098
Zhu Yib481de92007-09-25 17:54:57 -07009099 priv->hw_base = pci_iomap(pdev, 0, 0);
9100 if (!priv->hw_base) {
9101 err = -ENODEV;
9102 goto out_pci_release_regions;
9103 }
9104
9105 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
9106 (unsigned long long) pci_resource_len(pdev, 0));
9107 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
9108
9109 /* Initialize module parameter values here */
9110
Cahill, Ben M6440adb2007-11-29 11:09:55 +08009111 /* Disable radio (SW RF KILL) via parameter when loading driver */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009112 if (iwl4965_param_disable) {
Zhu Yib481de92007-09-25 17:54:57 -07009113 set_bit(STATUS_RF_KILL_SW, &priv->status);
9114 IWL_DEBUG_INFO("Radio disabled.\n");
9115 }
9116
9117 priv->iw_mode = IEEE80211_IF_TYPE_STA;
9118
9119 priv->ps_mode = 0;
9120 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
Zhu Yib481de92007-09-25 17:54:57 -07009121 priv->valid_antenna = 0x7; /* assume all 3 connected */
9122 priv->ps_mode = IWL_MIMO_PS_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07009123
Cahill, Ben M6440adb2007-11-29 11:09:55 +08009124 /* Choose which receivers/antennas to use */
Zhu Yib481de92007-09-25 17:54:57 -07009125 iwl4965_set_rxon_chain(priv);
9126
9127 printk(KERN_INFO DRV_NAME
9128 ": Detected Intel Wireless WiFi Link 4965AGN\n");
9129
9130 /* Device-specific setup */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009131 if (iwl4965_hw_set_hw_setting(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07009132 IWL_ERROR("failed to set hw settings\n");
9133 mutex_unlock(&priv->mutex);
9134 goto out_iounmap;
9135 }
9136
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009137#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009138 if (iwl4965_param_qos_enable)
Zhu Yib481de92007-09-25 17:54:57 -07009139 priv->qos_data.qos_enable = 1;
9140
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009141 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009142
9143 priv->qos_data.qos_active = 0;
9144 priv->qos_data.qos_cap.val = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009145#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07009146
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009147 iwl4965_set_rxon_channel(priv, MODE_IEEE80211G, 6);
9148 iwl4965_setup_deferred_work(priv);
9149 iwl4965_setup_rx_handlers(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009150
9151 priv->rates_mask = IWL_RATES_MASK;
9152 /* If power management is turned on, default to AC mode */
9153 priv->power_mode = IWL_POWER_AC;
9154 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
9155
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009156 iwl4965_disable_interrupts(priv);
Jes Sorensen49df2b32007-10-26 16:10:39 +02009157
Zhu Yib481de92007-09-25 17:54:57 -07009158 pci_enable_msi(pdev);
9159
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009160 err = request_irq(pdev->irq, iwl4965_isr, IRQF_SHARED, DRV_NAME, priv);
Zhu Yib481de92007-09-25 17:54:57 -07009161 if (err) {
9162 IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
9163 goto out_disable_msi;
9164 }
9165
9166 mutex_lock(&priv->mutex);
9167
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009168 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07009169 if (err) {
9170 IWL_ERROR("failed to create sysfs device attributes\n");
9171 mutex_unlock(&priv->mutex);
9172 goto out_release_irq;
9173 }
9174
9175 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
9176 * ucode filename and max sizes are card-specific. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009177 err = iwl4965_read_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009178 if (err) {
9179 IWL_ERROR("Could not read microcode: %d\n", err);
9180 mutex_unlock(&priv->mutex);
9181 goto out_pci_alloc;
9182 }
9183
9184 mutex_unlock(&priv->mutex);
9185
Ian Schram01ebd062007-10-25 17:15:22 +08009186 IWL_DEBUG_INFO("Queueing UP work.\n");
Zhu Yib481de92007-09-25 17:54:57 -07009187
9188 queue_work(priv->workqueue, &priv->up);
9189
9190 return 0;
9191
9192 out_pci_alloc:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009193 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009195 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07009196
9197 out_release_irq:
9198 free_irq(pdev->irq, priv);
9199
9200 out_disable_msi:
9201 pci_disable_msi(pdev);
9202 destroy_workqueue(priv->workqueue);
9203 priv->workqueue = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009204 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009205
9206 out_iounmap:
9207 pci_iounmap(pdev, priv->hw_base);
9208 out_pci_release_regions:
9209 pci_release_regions(pdev);
9210 out_pci_disable_device:
9211 pci_disable_device(pdev);
9212 pci_set_drvdata(pdev, NULL);
9213 out_ieee80211_free_hw:
9214 ieee80211_free_hw(priv->hw);
9215 out:
9216 return err;
9217}
9218
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009219static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07009220{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009221 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07009222 struct list_head *p, *q;
9223 int i;
9224
9225 if (!priv)
9226 return;
9227
9228 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
9229
Zhu Yib481de92007-09-25 17:54:57 -07009230 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08009231
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009232 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009233
9234 /* Free MAC hash list for ADHOC */
9235 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
9236 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
9237 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009238 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07009239 }
9240 }
9241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009242 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07009243
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009244 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009245
9246 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009247 iwl4965_rx_queue_free(priv, &priv->rxq);
9248 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009250 iwl4965_unset_hw_setting(priv);
9251 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009252
9253 if (priv->mac80211_registered) {
9254 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009255 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07009256 }
9257
Mohamed Abbas948c1712007-10-25 17:15:45 +08009258 /*netif_stop_queue(dev); */
9259 flush_workqueue(priv->workqueue);
9260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009261 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07009262 * priv->workqueue... so we can't take down the workqueue
9263 * until now... */
9264 destroy_workqueue(priv->workqueue);
9265 priv->workqueue = NULL;
9266
9267 free_irq(pdev->irq, priv);
9268 pci_disable_msi(pdev);
9269 pci_iounmap(pdev, priv->hw_base);
9270 pci_release_regions(pdev);
9271 pci_disable_device(pdev);
9272 pci_set_drvdata(pdev, NULL);
9273
9274 kfree(priv->channel_info);
9275
9276 kfree(priv->ieee_channels);
9277 kfree(priv->ieee_rates);
9278
9279 if (priv->ibss_beacon)
9280 dev_kfree_skb(priv->ibss_beacon);
9281
9282 ieee80211_free_hw(priv->hw);
9283}
9284
9285#ifdef CONFIG_PM
9286
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009287static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07009288{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009289 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07009290
Zhu Yib481de92007-09-25 17:54:57 -07009291 set_bit(STATUS_IN_SUSPEND, &priv->status);
9292
9293 /* Take down the device; powers it off, etc. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009294 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009295
9296 if (priv->mac80211_registered)
9297 ieee80211_stop_queues(priv->hw);
9298
9299 pci_save_state(pdev);
9300 pci_disable_device(pdev);
9301 pci_set_power_state(pdev, PCI_D3hot);
9302
Zhu Yib481de92007-09-25 17:54:57 -07009303 return 0;
9304}
9305
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009306static void iwl4965_resume(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07009307{
9308 unsigned long flags;
9309
9310 /* The following it a temporary work around due to the
9311 * suspend / resume not fully initializing the NIC correctly.
9312 * Without all of the following, resume will not attempt to take
9313 * down the NIC (it shouldn't really need to) and will just try
9314 * and bring the NIC back up. However that fails during the
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009315 * ucode verification process. This then causes iwl4965_down to be
9316 * called *after* iwl4965_hw_nic_init() has succeeded -- which
Zhu Yib481de92007-09-25 17:54:57 -07009317 * then lets the next init sequence succeed. So, we've
9318 * replicated all of that NIC init code here... */
9319
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009320 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07009321
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009322 iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009323
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009324 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9325 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07009326 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009327 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
9328 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9329 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07009330
9331 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009332 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009333
9334 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009335 iwl4965_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07009336
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009337 if (!iwl4965_grab_nic_access(priv)) {
9338 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Tomas Winklerac17a942007-10-25 17:15:37 +08009339 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009340 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009341 }
9342 spin_unlock_irqrestore(&priv->lock, flags);
9343
9344 udelay(5);
9345
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009346 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009347
9348 /* Bring the device back up */
9349 clear_bit(STATUS_IN_SUSPEND, &priv->status);
9350 queue_work(priv->workqueue, &priv->up);
9351}
9352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009353static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07009354{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009355 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07009356 int err;
9357
9358 printk(KERN_INFO "Coming out of suspend...\n");
9359
Zhu Yib481de92007-09-25 17:54:57 -07009360 pci_set_power_state(pdev, PCI_D0);
9361 err = pci_enable_device(pdev);
9362 pci_restore_state(pdev);
9363
9364 /*
9365 * Suspend/Resume resets the PCI configuration space, so we have to
9366 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
9367 * from interfering with C3 CPU state. pci_restore_state won't help
9368 * here since it only restores the first 64 bytes pci config header.
9369 */
9370 pci_write_config_byte(pdev, 0x41, 0x00);
9371
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009372 iwl4965_resume(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009373
9374 return 0;
9375}
9376
9377#endif /* CONFIG_PM */
9378
9379/*****************************************************************************
9380 *
9381 * driver and module entry point
9382 *
9383 *****************************************************************************/
9384
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009385static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07009386 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009387 .id_table = iwl4965_hw_card_ids,
9388 .probe = iwl4965_pci_probe,
9389 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07009390#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009391 .suspend = iwl4965_pci_suspend,
9392 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07009393#endif
9394};
9395
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009396static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07009397{
9398
9399 int ret;
9400 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
9401 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009402 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07009403 if (ret) {
9404 IWL_ERROR("Unable to initialize PCI module\n");
9405 return ret;
9406 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009407#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009408 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07009409 if (ret) {
9410 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009411 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07009412 return ret;
9413 }
9414#endif
9415
9416 return ret;
9417}
9418
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009419static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07009420{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009421#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009422 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07009423#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009424 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07009425}
9426
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009427module_param_named(antenna, iwl4965_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009428MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009429module_param_named(disable, iwl4965_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009430MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009431module_param_named(hwcrypto, iwl4965_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009432MODULE_PARM_DESC(hwcrypto,
9433 "using hardware crypto engine (default 0 [software])\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009434module_param_named(debug, iwl4965_param_debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009435MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009436module_param_named(disable_hw_scan, iwl4965_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009437MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
9438
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009439module_param_named(queues_num, iwl4965_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009440MODULE_PARM_DESC(queues_num, "number of hw queues.");
9441
9442/* QoS */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009443module_param_named(qos_enable, iwl4965_param_qos_enable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009444MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02009445module_param_named(amsdu_size_8K, iwl4965_param_amsdu_size_8K, int, 0444);
9446MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Zhu Yib481de92007-09-25 17:54:57 -07009447
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009448module_exit(iwl4965_exit);
9449module_init(iwl4965_init);