blob: af95626a611229739bf78c2fc6d42f05af1e81c5 [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
Tomas Winkler82b9a122008-03-04 18:09:30 -080048#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070049#include "iwl-4965.h"
50#include "iwl-helpers.h"
51
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080052static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
53 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080054
Zhu Yib481de92007-09-25 17:54:57 -070055/******************************************************************************
56 *
57 * module boiler plate
58 *
59 ******************************************************************************/
60
61/* module parameters */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080062static int iwl4965_param_disable_hw_scan; /* def: 0 = use 4965's h/w scan */
63static int iwl4965_param_debug; /* def: 0 = minimal debug log messages */
Ben Cahill9fbab512007-11-29 11:09:47 +080064static int iwl4965_param_disable; /* def: enable radio */
65static int iwl4965_param_antenna; /* def: 0 = both antennas (use diversity) */
66int iwl4965_param_hwcrypto; /* def: using software encryption */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080067static int iwl4965_param_qos_enable = 1; /* def: 1 = use quality of service */
68int iwl4965_param_queues_num = IWL_MAX_NUM_QUEUES; /* def: 16 Tx queues */
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +020069int iwl4965_param_amsdu_size_8K; /* def: enable 8K amsdu size */
Zhu Yib481de92007-09-25 17:54:57 -070070
71/*
72 * module name, copyright, version, etc.
73 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
74 */
75
76#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
77
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080078#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070079#define VD "d"
80#else
81#define VD
82#endif
83
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080084#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070085#define VS "s"
86#else
87#define VS
88#endif
89
Tomas Winklerdf48c322008-03-06 10:40:19 -080090#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070091
Zhu Yib481de92007-09-25 17:54:57 -070092
93MODULE_DESCRIPTION(DRV_DESCRIPTION);
94MODULE_VERSION(DRV_VERSION);
95MODULE_AUTHOR(DRV_COPYRIGHT);
96MODULE_LICENSE("GPL");
97
98__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
99{
100 u16 fc = le16_to_cpu(hdr->frame_control);
101 int hdr_len = ieee80211_get_hdrlen(fc);
102
103 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
104 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
105 return NULL;
106}
107
Johannes Berg8318d782008-01-24 19:38:38 +0100108static const struct ieee80211_supported_band *iwl4965_get_hw_mode(
109 struct iwl4965_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700110{
Johannes Berg8318d782008-01-24 19:38:38 +0100111 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700112}
113
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800114static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700115{
116 /* Single white space is for Linksys APs */
117 if (essid_len == 1 && essid[0] == ' ')
118 return 1;
119
120 /* Otherwise, if the entire essid is 0, we assume it is hidden */
121 while (essid_len) {
122 essid_len--;
123 if (essid[essid_len] != '\0')
124 return 0;
125 }
126
127 return 1;
128}
129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800130static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700131{
132 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
133 const char *s = essid;
134 char *d = escaped;
135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800136 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700137 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
138 return escaped;
139 }
140
141 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
142 while (essid_len--) {
143 if (*s == '\0') {
144 *d++ = '\\';
145 *d++ = '0';
146 s++;
147 } else
148 *d++ = *s++;
149 }
150 *d = '\0';
151 return escaped;
152}
153
Zhu Yib481de92007-09-25 17:54:57 -0700154/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
155 * DMA services
156 *
157 * Theory of operation
158 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800159 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
160 * of buffer descriptors, each of which points to one or more data buffers for
161 * the device to read from or fill. Driver and device exchange status of each
162 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
163 * entries in each circular buffer, to protect against confusing empty and full
164 * queue states.
165 *
166 * The device reads or writes the data in the queues via the device's several
167 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700168 *
169 * For Tx queue, there are low mark and high mark limits. If, after queuing
170 * the packet for Tx, free space become < low mark, Tx queue stopped. When
171 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
172 * Tx queue resumed.
173 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800174 * The 4965 operates with up to 17 queues: One receive queue, one transmit
175 * queue (#4) for sending commands to the device firmware, and 15 other
176 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800177 *
178 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700179 ***************************************************/
180
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200181int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700182{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800183 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700184
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800185 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700186 s -= q->n_bd;
187
188 if (s <= 0)
189 s += q->n_window;
190 /* keep some reserve to not confuse empty and full situations */
191 s -= 2;
192 if (s < 0)
193 s = 0;
194 return s;
195}
196
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800197/**
198 * iwl4965_queue_inc_wrap - increment queue index, wrap back to beginning
199 * @index -- current index
200 * @n_bd -- total number of entries in queue (must be power of 2)
201 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800202static inline int iwl4965_queue_inc_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700203{
204 return ++index & (n_bd - 1);
205}
206
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800207/**
208 * iwl4965_queue_dec_wrap - decrement queue index, wrap back to end
209 * @index -- current index
210 * @n_bd -- total number of entries in queue (must be power of 2)
211 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800212static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700213{
214 return --index & (n_bd - 1);
215}
216
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800217static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700218{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800219 return q->write_ptr > q->read_ptr ?
220 (i >= q->read_ptr && i < q->write_ptr) :
221 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700222}
223
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800224static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700225{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800226 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700227 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800228 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700229
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800230 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700231 return index & (q->n_window - 1);
232}
233
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800234/**
235 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
236 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800237static int iwl4965_queue_init(struct iwl4965_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700238 int count, int slots_num, u32 id)
239{
240 q->n_bd = count;
241 q->n_window = slots_num;
242 q->id = id;
243
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800244 /* count must be power-of-two size, otherwise iwl4965_queue_inc_wrap
245 * and iwl4965_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700246 BUG_ON(!is_power_of_2(count));
247
248 /* slots_num must be power-of-two size, otherwise
249 * get_cmd_index is broken. */
250 BUG_ON(!is_power_of_2(slots_num));
251
252 q->low_mark = q->n_window / 4;
253 if (q->low_mark < 4)
254 q->low_mark = 4;
255
256 q->high_mark = q->n_window / 8;
257 if (q->high_mark < 2)
258 q->high_mark = 2;
259
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800260 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700261
262 return 0;
263}
264
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800265/**
266 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
267 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800268static int iwl4965_tx_queue_alloc(struct iwl4965_priv *priv,
269 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700270{
271 struct pci_dev *dev = priv->pci_dev;
272
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800273 /* Driver private data, only for Tx (not command) queues,
274 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700275 if (id != IWL_CMD_QUEUE_NUM) {
276 txq->txb = kmalloc(sizeof(txq->txb[0]) *
277 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
278 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800279 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700280 "structures failed\n");
281 goto error;
282 }
283 } else
284 txq->txb = NULL;
285
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800286 /* Circular buffer of transmit frame descriptors (TFDs),
287 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700288 txq->bd = pci_alloc_consistent(dev,
289 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
290 &txq->q.dma_addr);
291
292 if (!txq->bd) {
293 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
294 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
295 goto error;
296 }
297 txq->q.id = id;
298
299 return 0;
300
301 error:
302 if (txq->txb) {
303 kfree(txq->txb);
304 txq->txb = NULL;
305 }
306
307 return -ENOMEM;
308}
309
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800310/**
311 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
312 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800313int iwl4965_tx_queue_init(struct iwl4965_priv *priv,
314 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700315{
316 struct pci_dev *dev = priv->pci_dev;
317 int len;
318 int rc = 0;
319
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800320 /*
321 * Alloc buffer array for commands (Tx or other types of commands).
322 * For the command queue (#4), allocate command space + one big
323 * command for scan, since scan command is very huge; the system will
324 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200325 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800326 * space is needed.
327 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800328 len = sizeof(struct iwl4965_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700329 if (txq_id == IWL_CMD_QUEUE_NUM)
330 len += IWL_MAX_SCAN_SIZE;
331 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
332 if (!txq->cmd)
333 return -ENOMEM;
334
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800335 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800336 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700337 if (rc) {
338 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
339
340 return -ENOMEM;
341 }
342 txq->need_update = 0;
343
344 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800345 * iwl4965_queue_inc_wrap and iwl4965_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700346 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800347
348 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800349 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700350
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800351 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800352 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700353
354 return 0;
355}
356
357/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800358 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700359 * @txq: Transmit queue to deallocate.
360 *
361 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800362 * Free all buffers.
363 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700364 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800365void iwl4965_tx_queue_free(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700366{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800367 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700368 struct pci_dev *dev = priv->pci_dev;
369 int len;
370
371 if (q->n_bd == 0)
372 return;
373
374 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800375 for (; q->write_ptr != q->read_ptr;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800376 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd))
377 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700378
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800379 len = sizeof(struct iwl4965_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700380 if (q->id == IWL_CMD_QUEUE_NUM)
381 len += IWL_MAX_SCAN_SIZE;
382
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800383 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700384 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
385
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800386 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700387 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800388 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700389 txq->q.n_bd, txq->bd, txq->q.dma_addr);
390
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800391 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700392 if (txq->txb) {
393 kfree(txq->txb);
394 txq->txb = NULL;
395 }
396
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800397 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700398 memset(txq, 0, sizeof(*txq));
399}
400
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800401const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700402
403/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800404 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700405 * the functionality provided here
406 */
407
408/**************************************************************/
409
Ian Schram01ebd062007-10-25 17:15:22 +0800410#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800411/**
412 * iwl4965_remove_station - Remove driver's knowledge of station.
413 *
414 * NOTE: This does not remove station from device's station table.
415 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800416static u8 iwl4965_remove_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700417{
418 int index = IWL_INVALID_STATION;
419 int i;
420 unsigned long flags;
421
422 spin_lock_irqsave(&priv->sta_lock, flags);
423
424 if (is_ap)
425 index = IWL_AP_ID;
426 else if (is_broadcast_ether_addr(addr))
427 index = priv->hw_setting.bcast_sta_id;
428 else
429 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
430 if (priv->stations[i].used &&
431 !compare_ether_addr(priv->stations[i].sta.sta.addr,
432 addr)) {
433 index = i;
434 break;
435 }
436
437 if (unlikely(index == IWL_INVALID_STATION))
438 goto out;
439
440 if (priv->stations[index].used) {
441 priv->stations[index].used = 0;
442 priv->num_stations--;
443 }
444
445 BUG_ON(priv->num_stations < 0);
446
447out:
448 spin_unlock_irqrestore(&priv->sta_lock, flags);
449 return 0;
450}
Zhu Yi556f8db2007-09-27 11:27:33 +0800451#endif
Zhu Yib481de92007-09-25 17:54:57 -0700452
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800453/**
454 * iwl4965_clear_stations_table - Clear the driver's station table
455 *
456 * NOTE: This does not clear or otherwise alter the device's station table.
457 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800458static void iwl4965_clear_stations_table(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700459{
460 unsigned long flags;
461
462 spin_lock_irqsave(&priv->sta_lock, flags);
463
464 priv->num_stations = 0;
465 memset(priv->stations, 0, sizeof(priv->stations));
466
467 spin_unlock_irqrestore(&priv->sta_lock, flags);
468}
469
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800470/**
471 * iwl4965_add_station_flags - Add station to tables in driver and device
472 */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200473u8 iwl4965_add_station_flags(struct iwl4965_priv *priv, const u8 *addr,
474 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700475{
476 int i;
477 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800478 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700479 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700480 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700481
482 spin_lock_irqsave(&priv->sta_lock, flags_spin);
483 if (is_ap)
484 index = IWL_AP_ID;
485 else if (is_broadcast_ether_addr(addr))
486 index = priv->hw_setting.bcast_sta_id;
487 else
488 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
489 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
490 addr)) {
491 index = i;
492 break;
493 }
494
495 if (!priv->stations[i].used &&
496 index == IWL_INVALID_STATION)
497 index = i;
498 }
499
500
Ben Cahill9fbab512007-11-29 11:09:47 +0800501 /* These two conditions have the same outcome, but keep them separate
502 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700503 if (unlikely(index == IWL_INVALID_STATION)) {
504 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
505 return index;
506 }
507
508 if (priv->stations[index].used &&
509 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
510 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
511 return index;
512 }
513
514
Joe Perches0795af52007-10-03 17:59:30 -0700515 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700516 station = &priv->stations[index];
517 station->used = 1;
518 priv->num_stations++;
519
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800520 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800521 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700522 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
523 station->sta.mode = 0;
524 station->sta.sta.sta_id = index;
525 station->sta.station_flags = 0;
526
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800527#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700528 /* BCAST station and IBSS stations do not work in HT mode */
529 if (index != priv->hw_setting.bcast_sta_id &&
530 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200531 iwl4965_set_ht_add_station(priv, index,
532 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800533#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700534
535 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800536
537 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800538 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700539 return index;
540
541}
542
543/*************** DRIVER STATUS FUNCTIONS *****/
544
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800545static inline int iwl4965_is_ready(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700546{
547 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
548 * set but EXIT_PENDING is not */
549 return test_bit(STATUS_READY, &priv->status) &&
550 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
551 !test_bit(STATUS_EXIT_PENDING, &priv->status);
552}
553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800554static inline int iwl4965_is_alive(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700555{
556 return test_bit(STATUS_ALIVE, &priv->status);
557}
558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800559static inline int iwl4965_is_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700560{
561 return test_bit(STATUS_INIT, &priv->status);
562}
563
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800564static inline int iwl4965_is_rfkill(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700565{
566 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
567 test_bit(STATUS_RF_KILL_SW, &priv->status);
568}
569
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800570static inline int iwl4965_is_ready_rf(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700571{
572
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800573 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700574 return 0;
575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800576 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700577}
578
579/*************** HOST COMMAND QUEUE FUNCTIONS *****/
580
581#define IWL_CMD(x) case x : return #x
582
583static const char *get_cmd_string(u8 cmd)
584{
585 switch (cmd) {
586 IWL_CMD(REPLY_ALIVE);
587 IWL_CMD(REPLY_ERROR);
588 IWL_CMD(REPLY_RXON);
589 IWL_CMD(REPLY_RXON_ASSOC);
590 IWL_CMD(REPLY_QOS_PARAM);
591 IWL_CMD(REPLY_RXON_TIMING);
592 IWL_CMD(REPLY_ADD_STA);
593 IWL_CMD(REPLY_REMOVE_STA);
594 IWL_CMD(REPLY_REMOVE_ALL_STA);
595 IWL_CMD(REPLY_TX);
596 IWL_CMD(REPLY_RATE_SCALE);
597 IWL_CMD(REPLY_LEDS_CMD);
598 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
599 IWL_CMD(RADAR_NOTIFICATION);
600 IWL_CMD(REPLY_QUIET_CMD);
601 IWL_CMD(REPLY_CHANNEL_SWITCH);
602 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
603 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
604 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
605 IWL_CMD(POWER_TABLE_CMD);
606 IWL_CMD(PM_SLEEP_NOTIFICATION);
607 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
608 IWL_CMD(REPLY_SCAN_CMD);
609 IWL_CMD(REPLY_SCAN_ABORT_CMD);
610 IWL_CMD(SCAN_START_NOTIFICATION);
611 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
612 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
613 IWL_CMD(BEACON_NOTIFICATION);
614 IWL_CMD(REPLY_TX_BEACON);
615 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
616 IWL_CMD(QUIET_NOTIFICATION);
617 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
618 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
619 IWL_CMD(REPLY_BT_CONFIG);
620 IWL_CMD(REPLY_STATISTICS_CMD);
621 IWL_CMD(STATISTICS_NOTIFICATION);
622 IWL_CMD(REPLY_CARD_STATE_CMD);
623 IWL_CMD(CARD_STATE_NOTIFICATION);
624 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
625 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
626 IWL_CMD(SENSITIVITY_CMD);
627 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
628 IWL_CMD(REPLY_RX_PHY_CMD);
629 IWL_CMD(REPLY_RX_MPDU_CMD);
630 IWL_CMD(REPLY_4965_RX);
631 IWL_CMD(REPLY_COMPRESSED_BA);
632 default:
633 return "UNKNOWN";
634
635 }
636}
637
638#define HOST_COMPLETE_TIMEOUT (HZ / 2)
639
640/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800641 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700642 * @priv: device private data point
643 * @cmd: a point to the ucode command structure
644 *
645 * The function returns < 0 values to indicate the operation is
646 * failed. On success, it turns the index (> 0) of command in the
647 * command queue.
648 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800649static int iwl4965_enqueue_hcmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700650{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800651 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
652 struct iwl4965_queue *q = &txq->q;
653 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700654 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800655 struct iwl4965_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700656 u32 idx;
657 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
658 dma_addr_t phys_addr;
659 int ret;
660 unsigned long flags;
661
662 /* If any of the command structures end up being larger than
663 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
664 * we will need to increase the size of the TFD entries */
665 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
666 !(cmd->meta.flags & CMD_SIZE_HUGE));
667
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800668 if (iwl4965_is_rfkill(priv)) {
669 IWL_DEBUG_INFO("Not sending command - RF KILL");
670 return -EIO;
671 }
672
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800673 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700674 IWL_ERROR("No space for Tx\n");
675 return -ENOSPC;
676 }
677
678 spin_lock_irqsave(&priv->hcmd_lock, flags);
679
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800680 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700681 memset(tfd, 0, sizeof(*tfd));
682
683 control_flags = (u32 *) tfd;
684
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800685 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700686 out_cmd = &txq->cmd[idx];
687
688 out_cmd->hdr.cmd = cmd->id;
689 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
690 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
691
692 /* At this point, the out_cmd now has all of the incoming cmd
693 * information */
694
695 out_cmd->hdr.flags = 0;
696 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800697 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700698 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
699 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
700
701 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800702 offsetof(struct iwl4965_cmd, hdr);
703 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700704
705 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
706 "%d bytes at %d[%d]:%d\n",
707 get_cmd_string(out_cmd->hdr.cmd),
708 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800709 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700710
711 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800712
713 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700714 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800715
716 /* Increment and update queue's write index */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800717 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
718 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700719
720 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
721 return ret ? ret : idx;
722}
723
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800724static int iwl4965_send_cmd_async(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700725{
726 int ret;
727
728 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
729
730 /* An asynchronous command can not expect an SKB to be set. */
731 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
732
733 /* An asynchronous command MUST have a callback. */
734 BUG_ON(!cmd->meta.u.callback);
735
736 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
737 return -EBUSY;
738
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800739 ret = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700740 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800741 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700742 get_cmd_string(cmd->id), ret);
743 return ret;
744 }
745 return 0;
746}
747
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800748static int iwl4965_send_cmd_sync(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700749{
750 int cmd_idx;
751 int ret;
752 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
753
754 BUG_ON(cmd->meta.flags & CMD_ASYNC);
755
756 /* A synchronous command can not have a callback set. */
757 BUG_ON(cmd->meta.u.callback != NULL);
758
759 if (atomic_xchg(&entry, 1)) {
760 IWL_ERROR("Error sending %s: Already sending a host command\n",
761 get_cmd_string(cmd->id));
762 return -EBUSY;
763 }
764
765 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
766
767 if (cmd->meta.flags & CMD_WANT_SKB)
768 cmd->meta.source = &cmd->meta;
769
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800770 cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700771 if (cmd_idx < 0) {
772 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800773 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700774 get_cmd_string(cmd->id), ret);
775 goto out;
776 }
777
778 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
779 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
780 HOST_COMPLETE_TIMEOUT);
781 if (!ret) {
782 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
783 IWL_ERROR("Error sending %s: time out after %dms.\n",
784 get_cmd_string(cmd->id),
785 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
786
787 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
788 ret = -ETIMEDOUT;
789 goto cancel;
790 }
791 }
792
793 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
794 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
795 get_cmd_string(cmd->id));
796 ret = -ECANCELED;
797 goto fail;
798 }
799 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
800 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
801 get_cmd_string(cmd->id));
802 ret = -EIO;
803 goto fail;
804 }
805 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
806 IWL_ERROR("Error: Response NULL in '%s'\n",
807 get_cmd_string(cmd->id));
808 ret = -EIO;
809 goto out;
810 }
811
812 ret = 0;
813 goto out;
814
815cancel:
816 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800817 struct iwl4965_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700818
819 /* Cancel the CMD_WANT_SKB flag for the cmd in the
820 * TX cmd queue. Otherwise in case the cmd comes
821 * in later, it will possibly set an invalid
822 * address (cmd->meta.source). */
823 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
824 qcmd->meta.flags &= ~CMD_WANT_SKB;
825 }
826fail:
827 if (cmd->meta.u.skb) {
828 dev_kfree_skb_any(cmd->meta.u.skb);
829 cmd->meta.u.skb = NULL;
830 }
831out:
832 atomic_set(&entry, 0);
833 return ret;
834}
835
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800836int iwl4965_send_cmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700837{
Zhu Yib481de92007-09-25 17:54:57 -0700838 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800839 return iwl4965_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800841 return iwl4965_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700842}
843
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800844int iwl4965_send_cmd_pdu(struct iwl4965_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700845{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800846 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700847 .id = id,
848 .len = len,
849 .data = data,
850 };
851
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800852 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700853}
854
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800855static int __must_check iwl4965_send_cmd_u32(struct iwl4965_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700856{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800857 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700858 .id = id,
859 .len = sizeof(val),
860 .data = &val,
861 };
862
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_statistics_request(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700867{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800868 return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700869}
870
871/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800872 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700873 *
874 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800875 * NOTE: mutex must be held before calling this fnction
876 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800877static int iwl4965_rxon_add_station(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700878 const u8 *addr, int is_ap)
879{
Zhu Yi556f8db2007-09-27 11:27:33 +0800880 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700881
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800882 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200883#ifdef CONFIG_IWL4965_HT
884 struct ieee80211_conf *conf = &priv->hw->conf;
885 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
886
887 if ((is_ap) &&
888 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
889 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
890 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
891 0, cur_ht_config);
892 else
893#endif /* CONFIG_IWL4965_HT */
894 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
895 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800896
897 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700898 iwl4965_add_station(priv, addr, is_ap);
899
Zhu Yi556f8db2007-09-27 11:27:33 +0800900 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700901}
902
903/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800904 * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
Zhu Yib481de92007-09-25 17:54:57 -0700905 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
906 * @channel: Any channel valid for the requested phymode
907
908 * In addition to setting the staging RXON, priv->phymode is also set.
909 *
910 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
911 * in the staging RXON flag structure based on the phymode
912 */
Johannes Berg8318d782008-01-24 19:38:38 +0100913static int iwl4965_set_rxon_channel(struct iwl4965_priv *priv,
914 enum ieee80211_band band,
Ben Cahill9fbab512007-11-29 11:09:47 +0800915 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700916{
Johannes Berg8318d782008-01-24 19:38:38 +0100917 if (!iwl4965_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700918 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100919 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700920 return -EINVAL;
921 }
922
923 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100924 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700925 return 0;
926
927 priv->staging_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100928 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700929 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
930 else
931 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
932
Johannes Berg8318d782008-01-24 19:38:38 +0100933 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700934
Johannes Berg8318d782008-01-24 19:38:38 +0100935 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700936
937 return 0;
938}
939
940/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800941 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700942 *
943 * NOTE: This is really only useful during development and can eventually
944 * be #ifdef'd out once the driver is stable and folks aren't actively
945 * making changes
946 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800947static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700948{
949 int error = 0;
950 int counter = 1;
951
952 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
953 error |= le32_to_cpu(rxon->flags &
954 (RXON_FLG_TGJ_NARROW_BAND_MSK |
955 RXON_FLG_RADAR_DETECT_MSK));
956 if (error)
957 IWL_WARNING("check 24G fields %d | %d\n",
958 counter++, error);
959 } else {
960 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
961 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
962 if (error)
963 IWL_WARNING("check 52 fields %d | %d\n",
964 counter++, error);
965 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
966 if (error)
967 IWL_WARNING("check 52 CCK %d | %d\n",
968 counter++, error);
969 }
970 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
971 if (error)
972 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
973
974 /* make sure basic rates 6Mbps and 1Mbps are supported */
975 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
976 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
977 if (error)
978 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
979
980 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
981 if (error)
982 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
983
984 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
985 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
986 if (error)
987 IWL_WARNING("check CCK and short slot %d | %d\n",
988 counter++, error);
989
990 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
991 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
992 if (error)
993 IWL_WARNING("check CCK & auto detect %d | %d\n",
994 counter++, error);
995
996 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
997 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
998 if (error)
999 IWL_WARNING("check TGG and auto detect %d | %d\n",
1000 counter++, error);
1001
1002 if (error)
1003 IWL_WARNING("Tuning to channel %d\n",
1004 le16_to_cpu(rxon->channel));
1005
1006 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001007 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -07001008 return -1;
1009 }
1010 return 0;
1011}
1012
1013/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001014 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +08001015 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -07001016 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001017 * If the RXON structure is changing enough to require a new tune,
1018 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
1019 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -07001020 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001021static int iwl4965_full_rxon_required(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001022{
1023
1024 /* These items are only settable from the full RXON command */
1025 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
1026 compare_ether_addr(priv->staging_rxon.bssid_addr,
1027 priv->active_rxon.bssid_addr) ||
1028 compare_ether_addr(priv->staging_rxon.node_addr,
1029 priv->active_rxon.node_addr) ||
1030 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
1031 priv->active_rxon.wlap_bssid_addr) ||
1032 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
1033 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
1034 (priv->staging_rxon.air_propagation !=
1035 priv->active_rxon.air_propagation) ||
1036 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
1037 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
1038 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
1039 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
1040 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
1041 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1042 return 1;
1043
1044 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1045 * be updated with the RXON_ASSOC command -- however only some
1046 * flag transitions are allowed using RXON_ASSOC */
1047
1048 /* Check if we are not switching bands */
1049 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1050 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1051 return 1;
1052
1053 /* Check if we are switching association toggle */
1054 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1055 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1056 return 1;
1057
1058 return 0;
1059}
1060
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001061static int iwl4965_send_rxon_assoc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001062{
1063 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001064 struct iwl4965_rx_packet *res = NULL;
1065 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1066 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001067 .id = REPLY_RXON_ASSOC,
1068 .len = sizeof(rxon_assoc),
1069 .meta.flags = CMD_WANT_SKB,
1070 .data = &rxon_assoc,
1071 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001072 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1073 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001074
1075 if ((rxon1->flags == rxon2->flags) &&
1076 (rxon1->filter_flags == rxon2->filter_flags) &&
1077 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1078 (rxon1->ofdm_ht_single_stream_basic_rates ==
1079 rxon2->ofdm_ht_single_stream_basic_rates) &&
1080 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1081 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1082 (rxon1->rx_chain == rxon2->rx_chain) &&
1083 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1084 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1085 return 0;
1086 }
1087
1088 rxon_assoc.flags = priv->staging_rxon.flags;
1089 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1090 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1091 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1092 rxon_assoc.reserved = 0;
1093 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1094 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1095 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1096 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1097 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1098
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001099 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001100 if (rc)
1101 return rc;
1102
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001103 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001104 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1105 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1106 rc = -EIO;
1107 }
1108
1109 priv->alloc_rxb_skb--;
1110 dev_kfree_skb_any(cmd.meta.u.skb);
1111
1112 return rc;
1113}
1114
1115/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001116 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001117 *
Ian Schram01ebd062007-10-25 17:15:22 +08001118 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001119 * the active_rxon structure is updated with the new data. This
1120 * function correctly transitions out of the RXON_ASSOC_MSK state if
1121 * a HW tune is required based on the RXON structure changes.
1122 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001123static int iwl4965_commit_rxon(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001124{
1125 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001126 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001127 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001128 int rc = 0;
1129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001130 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001131 return -1;
1132
1133 /* always get timestamp with Rx frame */
1134 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001136 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001137 if (rc) {
1138 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1139 return -EINVAL;
1140 }
1141
1142 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001143 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001144 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001145 if (!iwl4965_full_rxon_required(priv)) {
1146 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001147 if (rc) {
1148 IWL_ERROR("Error setting RXON_ASSOC "
1149 "configuration (%d).\n", rc);
1150 return rc;
1151 }
1152
1153 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1154
1155 return 0;
1156 }
1157
1158 /* station table will be cleared */
1159 priv->assoc_station_added = 0;
1160
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001161#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001162 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1163 if (!priv->error_recovering)
1164 priv->start_calib = 0;
1165
1166 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001167#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001168
1169 /* If we are currently associated and the new config requires
1170 * an RXON_ASSOC and the new config wants the associated mask enabled,
1171 * we must clear the associated from the active configuration
1172 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001173 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001174 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1175 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1176 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1177
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001178 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1179 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001180 &priv->active_rxon);
1181
1182 /* If the mask clearing failed then we set
1183 * active_rxon back to what it was previously */
1184 if (rc) {
1185 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1186 IWL_ERROR("Error clearing ASSOC_MSK on current "
1187 "configuration (%d).\n", rc);
1188 return rc;
1189 }
Zhu Yib481de92007-09-25 17:54:57 -07001190 }
1191
1192 IWL_DEBUG_INFO("Sending RXON\n"
1193 "* with%s RXON_FILTER_ASSOC_MSK\n"
1194 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001195 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001196 ((priv->staging_rxon.filter_flags &
1197 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1198 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001199 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001200
1201 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001202 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1203 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001204 if (rc) {
1205 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1206 return rc;
1207 }
1208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001209 iwl4965_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001210
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001211#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001212 if (!priv->error_recovering)
1213 priv->start_calib = 0;
1214
1215 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1216 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001217#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001218
1219 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1220
1221 /* If we issue a new RXON command which required a tune then we must
1222 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001223 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001224 if (rc) {
1225 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1226 return rc;
1227 }
1228
1229 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001230 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001231 IWL_INVALID_STATION) {
1232 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1233 return -EIO;
1234 }
1235
1236 /* If we have set the ASSOC_MSK and we are in BSS mode then
1237 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001238 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001239 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001240 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -07001241 == IWL_INVALID_STATION) {
1242 IWL_ERROR("Error adding AP address for transmit.\n");
1243 return -EIO;
1244 }
1245 priv->assoc_station_added = 1;
1246 }
1247
1248 return 0;
1249}
1250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001251static int iwl4965_send_bt_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001252{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001253 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001254 .flags = 3,
1255 .lead_time = 0xAA,
1256 .max_kill = 1,
1257 .kill_ack_mask = 0,
1258 .kill_cts_mask = 0,
1259 };
1260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001261 return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1262 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001263}
1264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001265static int iwl4965_send_scan_abort(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001266{
1267 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001268 struct iwl4965_rx_packet *res;
1269 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001270 .id = REPLY_SCAN_ABORT_CMD,
1271 .meta.flags = CMD_WANT_SKB,
1272 };
1273
1274 /* If there isn't a scan actively going on in the hardware
1275 * then we are in between scan bands and not actually
1276 * actively scanning, so don't send the abort command */
1277 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1278 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1279 return 0;
1280 }
1281
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001282 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001283 if (rc) {
1284 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1285 return rc;
1286 }
1287
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001288 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001289 if (res->u.status != CAN_ABORT_STATUS) {
1290 /* The scan abort will return 1 for success or
1291 * 2 for "failure". A failure condition can be
1292 * due to simply not being in an active scan which
1293 * can occur if we send the scan abort before we
1294 * the microcode has notified us that a scan is
1295 * completed. */
1296 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1297 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1298 clear_bit(STATUS_SCAN_HW, &priv->status);
1299 }
1300
1301 dev_kfree_skb_any(cmd.meta.u.skb);
1302
1303 return rc;
1304}
1305
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001306static int iwl4965_card_state_sync_callback(struct iwl4965_priv *priv,
1307 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001308 struct sk_buff *skb)
1309{
1310 return 1;
1311}
1312
1313/*
1314 * CARD_STATE_CMD
1315 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001316 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001317 *
1318 * When in the 'enable' state the card operates as normal.
1319 * When in the 'disable' state, the card enters into a low power mode.
1320 * When in the 'halt' state, the card is shut down and must be fully
1321 * restarted to come back on.
1322 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001323static int iwl4965_send_card_state(struct iwl4965_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001324{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001325 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001326 .id = REPLY_CARD_STATE_CMD,
1327 .len = sizeof(u32),
1328 .data = &flags,
1329 .meta.flags = meta_flag,
1330 };
1331
1332 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001333 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001334
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001335 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001336}
1337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001338static int iwl4965_add_sta_sync_callback(struct iwl4965_priv *priv,
1339 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001340{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001341 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001342
1343 if (!skb) {
1344 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1345 return 1;
1346 }
1347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001348 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001349 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1350 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1351 res->hdr.flags);
1352 return 1;
1353 }
1354
1355 switch (res->u.add_sta.status) {
1356 case ADD_STA_SUCCESS_MSK:
1357 break;
1358 default:
1359 break;
1360 }
1361
1362 /* We didn't cache the SKB; let the caller free it */
1363 return 1;
1364}
1365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001366int iwl4965_send_add_station(struct iwl4965_priv *priv,
1367 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001368{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001369 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001370 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001371 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001372 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001373 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001374 .meta.flags = flags,
1375 .data = sta,
1376 };
1377
1378 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001379 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001380 else
1381 cmd.meta.flags |= CMD_WANT_SKB;
1382
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001383 rc = iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001384
1385 if (rc || (flags & CMD_ASYNC))
1386 return rc;
1387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001388 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001389 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1390 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1391 res->hdr.flags);
1392 rc = -EIO;
1393 }
1394
1395 if (rc == 0) {
1396 switch (res->u.add_sta.status) {
1397 case ADD_STA_SUCCESS_MSK:
1398 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1399 break;
1400 default:
1401 rc = -EIO;
1402 IWL_WARNING("REPLY_ADD_STA failed\n");
1403 break;
1404 }
1405 }
1406
1407 priv->alloc_rxb_skb--;
1408 dev_kfree_skb_any(cmd.meta.u.skb);
1409
1410 return rc;
1411}
1412
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001413static int iwl4965_update_sta_key_info(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001414 struct ieee80211_key_conf *keyconf,
1415 u8 sta_id)
1416{
1417 unsigned long flags;
1418 __le16 key_flags = 0;
1419
1420 switch (keyconf->alg) {
1421 case ALG_CCMP:
1422 key_flags |= STA_KEY_FLG_CCMP;
1423 key_flags |= cpu_to_le16(
1424 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1425 key_flags &= ~STA_KEY_FLG_INVALID;
1426 break;
1427 case ALG_TKIP:
1428 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001429 default:
1430 return -EINVAL;
1431 }
1432 spin_lock_irqsave(&priv->sta_lock, flags);
1433 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1434 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1435 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1436 keyconf->keylen);
1437
1438 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1439 keyconf->keylen);
1440 priv->stations[sta_id].sta.key.key_flags = key_flags;
1441 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1442 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1443
1444 spin_unlock_irqrestore(&priv->sta_lock, flags);
1445
1446 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001447 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001448 return 0;
1449}
1450
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001451static int iwl4965_clear_sta_key_info(struct iwl4965_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001452{
1453 unsigned long flags;
1454
1455 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001456 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1457 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001458 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1459 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1460 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1461 spin_unlock_irqrestore(&priv->sta_lock, flags);
1462
1463 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001464 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001465 return 0;
1466}
1467
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001468static void iwl4965_clear_free_frames(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001469{
1470 struct list_head *element;
1471
1472 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1473 priv->frames_count);
1474
1475 while (!list_empty(&priv->free_frames)) {
1476 element = priv->free_frames.next;
1477 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001478 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001479 priv->frames_count--;
1480 }
1481
1482 if (priv->frames_count) {
1483 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1484 priv->frames_count);
1485 priv->frames_count = 0;
1486 }
1487}
1488
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001489static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001490{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001491 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001492 struct list_head *element;
1493 if (list_empty(&priv->free_frames)) {
1494 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1495 if (!frame) {
1496 IWL_ERROR("Could not allocate frame!\n");
1497 return NULL;
1498 }
1499
1500 priv->frames_count++;
1501 return frame;
1502 }
1503
1504 element = priv->free_frames.next;
1505 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001506 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001507}
1508
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001509static void iwl4965_free_frame(struct iwl4965_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001510{
1511 memset(frame, 0, sizeof(*frame));
1512 list_add(&frame->list, &priv->free_frames);
1513}
1514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001515unsigned int iwl4965_fill_beacon_frame(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001516 struct ieee80211_hdr *hdr,
1517 const u8 *dest, int left)
1518{
1519
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001520 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001521 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1522 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1523 return 0;
1524
1525 if (priv->ibss_beacon->len > left)
1526 return 0;
1527
1528 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1529
1530 return priv->ibss_beacon->len;
1531}
1532
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001533static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001534{
1535 u8 i;
1536
1537 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001538 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001539 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001540 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001541 }
1542
1543 return IWL_RATE_INVALID;
1544}
1545
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001546static int iwl4965_send_beacon_cmd(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001547{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001548 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001549 unsigned int frame_size;
1550 int rc;
1551 u8 rate;
1552
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001553 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001554
1555 if (!frame) {
1556 IWL_ERROR("Could not obtain free frame buffer for beacon "
1557 "command.\n");
1558 return -ENOMEM;
1559 }
1560
1561 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001562 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001563 0xFF0);
1564 if (rate == IWL_INVALID_RATE)
1565 rate = IWL_RATE_6M_PLCP;
1566 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001567 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001568 if (rate == IWL_INVALID_RATE)
1569 rate = IWL_RATE_1M_PLCP;
1570 }
1571
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001572 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001573
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001574 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001575 &frame->u.cmd[0]);
1576
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001577 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001578
1579 return rc;
1580}
1581
1582/******************************************************************************
1583 *
1584 * EEPROM related functions
1585 *
1586 ******************************************************************************/
1587
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001588static void get_eeprom_mac(struct iwl4965_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001589{
1590 memcpy(mac, priv->eeprom.mac_address, 6);
1591}
1592
Reinette Chatre74a3a252008-01-23 10:15:19 -08001593static inline void iwl4965_eeprom_release_semaphore(struct iwl4965_priv *priv)
1594{
1595 iwl4965_clear_bit(priv, CSR_HW_IF_CONFIG_REG,
1596 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
1597}
1598
Zhu Yib481de92007-09-25 17:54:57 -07001599/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001600 * iwl4965_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001601 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001602 * Load the EEPROM contents from adapter into priv->eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001603 *
1604 * NOTE: This routine uses the non-debug IO access functions.
1605 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001606int iwl4965_eeprom_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001607{
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001608 u16 *e = (u16 *)&priv->eeprom;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001609 u32 gp = iwl4965_read32(priv, CSR_EEPROM_GP);
Zhu Yib481de92007-09-25 17:54:57 -07001610 u32 r;
1611 int sz = sizeof(priv->eeprom);
1612 int rc;
1613 int i;
1614 u16 addr;
1615
1616 /* The EEPROM structure has several padding buffers within it
1617 * and when adding new EEPROM maps is subject to programmer errors
1618 * which may be very difficult to identify without explicitly
1619 * checking the resulting size of the eeprom map. */
1620 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1621
1622 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1623 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1624 return -ENOENT;
1625 }
1626
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001627 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001628 rc = iwl4965_eeprom_acquire_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001629 if (rc < 0) {
Ian Schram91e17472007-10-25 17:15:23 +08001630 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001631 return -ENOENT;
1632 }
1633
1634 /* eeprom is an array of 16bit values */
1635 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001636 _iwl4965_write32(priv, CSR_EEPROM_REG, addr << 1);
1637 _iwl4965_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
Zhu Yib481de92007-09-25 17:54:57 -07001638
1639 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1640 i += IWL_EEPROM_ACCESS_DELAY) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001641 r = _iwl4965_read_direct32(priv, CSR_EEPROM_REG);
Zhu Yib481de92007-09-25 17:54:57 -07001642 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1643 break;
1644 udelay(IWL_EEPROM_ACCESS_DELAY);
1645 }
1646
1647 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1648 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1649 rc = -ETIMEDOUT;
1650 goto done;
1651 }
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001652 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001653 }
1654 rc = 0;
1655
1656done:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001657 iwl4965_eeprom_release_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001658 return rc;
1659}
1660
1661/******************************************************************************
1662 *
1663 * Misc. internal state and helper functions
1664 *
1665 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001666
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001667static void iwl4965_unset_hw_setting(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001668{
1669 if (priv->hw_setting.shared_virt)
1670 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001671 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001672 priv->hw_setting.shared_virt,
1673 priv->hw_setting.shared_phys);
1674}
1675
1676/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001677 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001678 *
1679 * return : set the bit for each supported rate insert in ie
1680 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001681static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001682 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001683{
1684 u16 ret_rates = 0, bit;
1685 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001686 u8 *cnt = ie;
1687 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001688
1689 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1690 if (bit & supported_rate) {
1691 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001692 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001693 ((bit & basic_rate) ? 0x80 : 0x00);
1694 (*cnt)++;
1695 (*left)--;
1696 if ((*left <= 0) ||
1697 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001698 break;
1699 }
1700 }
1701
1702 return ret_rates;
1703}
1704
Zhu Yib481de92007-09-25 17:54:57 -07001705/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001706 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001707 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001708static u16 iwl4965_fill_probe_req(struct iwl4965_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001709 enum ieee80211_band band,
1710 struct ieee80211_mgmt *frame,
1711 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001712{
1713 int len = 0;
1714 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001715 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001716#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001717 const struct ieee80211_supported_band *sband =
1718 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001719#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001720
1721 /* Make sure there is enough space for the probe request,
1722 * two mandatory IEs and the data */
1723 left -= 24;
1724 if (left < 0)
1725 return 0;
1726 len += 24;
1727
1728 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001729 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001730 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001731 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001732 frame->seq_ctrl = 0;
1733
1734 /* fill in our indirect SSID IE */
1735 /* ...next IE... */
1736
1737 left -= 2;
1738 if (left < 0)
1739 return 0;
1740 len += 2;
1741 pos = &(frame->u.probe_req.variable[0]);
1742 *pos++ = WLAN_EID_SSID;
1743 *pos++ = 0;
1744
1745 /* fill in our direct SSID IE... */
1746 if (is_direct) {
1747 /* ...next IE... */
1748 left -= 2 + priv->essid_len;
1749 if (left < 0)
1750 return 0;
1751 /* ... fill it in... */
1752 *pos++ = WLAN_EID_SSID;
1753 *pos++ = priv->essid_len;
1754 memcpy(pos, priv->essid, priv->essid_len);
1755 pos += priv->essid_len;
1756 len += 2 + priv->essid_len;
1757 }
1758
1759 /* fill in supported rate */
1760 /* ...next IE... */
1761 left -= 2;
1762 if (left < 0)
1763 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001764
Zhu Yib481de92007-09-25 17:54:57 -07001765 /* ... fill it in... */
1766 *pos++ = WLAN_EID_SUPP_RATES;
1767 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001768
mabbasbee488d2007-10-25 17:15:42 +08001769 /* exclude 60M rate */
1770 active_rates = priv->rates_mask;
1771 active_rates &= ~IWL_RATE_60M_MASK;
1772
1773 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001774
Tomas Winklerc7c46672007-10-18 02:04:15 +02001775 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001776 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001777 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001778 active_rates &= ~ret_rates;
1779
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001780 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001781 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001782 active_rates &= ~ret_rates;
1783
Zhu Yib481de92007-09-25 17:54:57 -07001784 len += 2 + *pos;
1785 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001786 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001787 goto fill_end;
1788
1789 /* fill in supported extended rate */
1790 /* ...next IE... */
1791 left -= 2;
1792 if (left < 0)
1793 return 0;
1794 /* ... fill it in... */
1795 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1796 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001797 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001798 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001799 if (*pos > 0)
1800 len += 2 + *pos;
1801
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001802#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001803 if (sband && sband->ht_info.ht_supported) {
1804 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001805 pos += (*pos) + 1;
1806 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001807 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001808 ht_cap = (struct ieee80211_ht_cap *)pos;
1809 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1810 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1811 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1812 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1813 ((sband->ht_info.ampdu_density << 2) &
1814 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001815 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001816 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001817#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001818
1819 fill_end:
1820 return (u16)len;
1821}
1822
1823/*
1824 * QoS support
1825*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001826static int iwl4965_send_qos_params_command(struct iwl4965_priv *priv,
1827 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001828{
1829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001830 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1831 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001832}
1833
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001834static void iwl4965_reset_qos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001835{
1836 u16 cw_min = 15;
1837 u16 cw_max = 1023;
1838 u8 aifs = 2;
1839 u8 is_legacy = 0;
1840 unsigned long flags;
1841 int i;
1842
1843 spin_lock_irqsave(&priv->lock, flags);
1844 priv->qos_data.qos_active = 0;
1845
1846 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1847 if (priv->qos_data.qos_enable)
1848 priv->qos_data.qos_active = 1;
1849 if (!(priv->active_rate & 0xfff0)) {
1850 cw_min = 31;
1851 is_legacy = 1;
1852 }
1853 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1854 if (priv->qos_data.qos_enable)
1855 priv->qos_data.qos_active = 1;
1856 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1857 cw_min = 31;
1858 is_legacy = 1;
1859 }
1860
1861 if (priv->qos_data.qos_active)
1862 aifs = 3;
1863
1864 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1865 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1866 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1867 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1868 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1869
1870 if (priv->qos_data.qos_active) {
1871 i = 1;
1872 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1873 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1874 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1875 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1876 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1877
1878 i = 2;
1879 priv->qos_data.def_qos_parm.ac[i].cw_min =
1880 cpu_to_le16((cw_min + 1) / 2 - 1);
1881 priv->qos_data.def_qos_parm.ac[i].cw_max =
1882 cpu_to_le16(cw_max);
1883 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1884 if (is_legacy)
1885 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1886 cpu_to_le16(6016);
1887 else
1888 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1889 cpu_to_le16(3008);
1890 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1891
1892 i = 3;
1893 priv->qos_data.def_qos_parm.ac[i].cw_min =
1894 cpu_to_le16((cw_min + 1) / 4 - 1);
1895 priv->qos_data.def_qos_parm.ac[i].cw_max =
1896 cpu_to_le16((cw_max + 1) / 2 - 1);
1897 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1898 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1899 if (is_legacy)
1900 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1901 cpu_to_le16(3264);
1902 else
1903 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1904 cpu_to_le16(1504);
1905 } else {
1906 for (i = 1; i < 4; i++) {
1907 priv->qos_data.def_qos_parm.ac[i].cw_min =
1908 cpu_to_le16(cw_min);
1909 priv->qos_data.def_qos_parm.ac[i].cw_max =
1910 cpu_to_le16(cw_max);
1911 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1912 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1913 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1914 }
1915 }
1916 IWL_DEBUG_QOS("set QoS to default \n");
1917
1918 spin_unlock_irqrestore(&priv->lock, flags);
1919}
1920
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001921static void iwl4965_activate_qos(struct iwl4965_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001922{
1923 unsigned long flags;
1924
Zhu Yib481de92007-09-25 17:54:57 -07001925 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1926 return;
1927
1928 if (!priv->qos_data.qos_enable)
1929 return;
1930
1931 spin_lock_irqsave(&priv->lock, flags);
1932 priv->qos_data.def_qos_parm.qos_flags = 0;
1933
1934 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1935 !priv->qos_data.qos_cap.q_AP.txop_request)
1936 priv->qos_data.def_qos_parm.qos_flags |=
1937 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001938 if (priv->qos_data.qos_active)
1939 priv->qos_data.def_qos_parm.qos_flags |=
1940 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1941
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001942#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001943 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001944 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001945#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001946
Zhu Yib481de92007-09-25 17:54:57 -07001947 spin_unlock_irqrestore(&priv->lock, flags);
1948
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001949 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001950 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1951 priv->qos_data.qos_active,
1952 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001953
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001954 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001955 &(priv->qos_data.def_qos_parm));
1956 }
1957}
1958
Zhu Yib481de92007-09-25 17:54:57 -07001959/*
1960 * Power management (not Tx power!) functions
1961 */
1962#define MSEC_TO_USEC 1024
1963
1964#define NOSLP __constant_cpu_to_le16(0), 0, 0
1965#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1966#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1967#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1968 __constant_cpu_to_le32(X1), \
1969 __constant_cpu_to_le32(X2), \
1970 __constant_cpu_to_le32(X3), \
1971 __constant_cpu_to_le32(X4)}
1972
1973
1974/* default power management (not Tx power) table values */
1975/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001976static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001977 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1978 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1979 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1980 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1981 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1982 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1983};
1984
1985/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001986static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001987 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1988 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1989 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1990 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1991 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1992 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1993 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1994 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1995 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1996 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1997};
1998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001999int iwl4965_power_init_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002000{
2001 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002002 struct iwl4965_power_mgr *pow_data;
2003 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07002004 u16 pci_pm;
2005
2006 IWL_DEBUG_POWER("Initialize power \n");
2007
2008 pow_data = &(priv->power_data);
2009
2010 memset(pow_data, 0, sizeof(*pow_data));
2011
2012 pow_data->active_index = IWL_POWER_RANGE_0;
2013 pow_data->dtim_val = 0xffff;
2014
2015 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2016 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2017
2018 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2019 if (rc != 0)
2020 return 0;
2021 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002022 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002023
2024 IWL_DEBUG_POWER("adjust power command flags\n");
2025
2026 for (i = 0; i < IWL_POWER_AC; i++) {
2027 cmd = &pow_data->pwr_range_0[i].cmd;
2028
2029 if (pci_pm & 0x1)
2030 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2031 else
2032 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2033 }
2034 }
2035 return rc;
2036}
2037
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002038static int iwl4965_update_power_cmd(struct iwl4965_priv *priv,
2039 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002040{
2041 int rc = 0, i;
2042 u8 skip;
2043 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002044 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07002045 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002046 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07002047
2048 if (mode > IWL_POWER_INDEX_5) {
2049 IWL_DEBUG_POWER("Error invalid power mode \n");
2050 return -1;
2051 }
2052 pow_data = &(priv->power_data);
2053
2054 if (pow_data->active_index == IWL_POWER_RANGE_0)
2055 range = &pow_data->pwr_range_0[0];
2056 else
2057 range = &pow_data->pwr_range_1[1];
2058
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002059 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07002060
2061#ifdef IWL_MAC80211_DISABLE
2062 if (priv->assoc_network != NULL) {
2063 unsigned long flags;
2064
2065 period = priv->assoc_network->tim.tim_period;
2066 }
2067#endif /*IWL_MAC80211_DISABLE */
2068 skip = range[mode].no_dtim;
2069
2070 if (period == 0) {
2071 period = 1;
2072 skip = 0;
2073 }
2074
2075 if (skip == 0) {
2076 max_sleep = period;
2077 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2078 } else {
2079 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2080 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2081 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2082 }
2083
2084 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2085 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2086 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2087 }
2088
2089 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2090 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2091 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2092 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2093 le32_to_cpu(cmd->sleep_interval[0]),
2094 le32_to_cpu(cmd->sleep_interval[1]),
2095 le32_to_cpu(cmd->sleep_interval[2]),
2096 le32_to_cpu(cmd->sleep_interval[3]),
2097 le32_to_cpu(cmd->sleep_interval[4]));
2098
2099 return rc;
2100}
2101
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002102static int iwl4965_send_power_mode(struct iwl4965_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002103{
John W. Linville9a62f732007-11-15 16:27:36 -05002104 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002105 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002106 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002107
2108 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08002109 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07002110 * else user level */
2111 switch (mode) {
2112 case IWL_POWER_BATTERY:
2113 final_mode = IWL_POWER_INDEX_3;
2114 break;
2115 case IWL_POWER_AC:
2116 final_mode = IWL_POWER_MODE_CAM;
2117 break;
2118 default:
2119 final_mode = mode;
2120 break;
2121 }
2122
2123 cmd.keep_alive_beacons = 0;
2124
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002125 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002126
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002127 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002128
2129 if (final_mode == IWL_POWER_MODE_CAM)
2130 clear_bit(STATUS_POWER_PMI, &priv->status);
2131 else
2132 set_bit(STATUS_POWER_PMI, &priv->status);
2133
2134 return rc;
2135}
2136
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002137int iwl4965_is_network_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002138{
2139 /* Filter incoming packets to determine if they are targeted toward
2140 * this network, discarding packets coming from ourselves */
2141 switch (priv->iw_mode) {
2142 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2143 /* packets from our adapter are dropped (echo) */
2144 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2145 return 0;
2146 /* {broad,multi}cast packets to our IBSS go through */
2147 if (is_multicast_ether_addr(header->addr1))
2148 return !compare_ether_addr(header->addr3, priv->bssid);
2149 /* packets to our adapter go through */
2150 return !compare_ether_addr(header->addr1, priv->mac_addr);
2151 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2152 /* packets from our adapter are dropped (echo) */
2153 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2154 return 0;
2155 /* {broad,multi}cast packets to our BSS go through */
2156 if (is_multicast_ether_addr(header->addr1))
2157 return !compare_ether_addr(header->addr2, priv->bssid);
2158 /* packets to our adapter go through */
2159 return !compare_ether_addr(header->addr1, priv->mac_addr);
2160 }
2161
2162 return 1;
2163}
2164
2165#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2166
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002167static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002168{
2169 switch (status & TX_STATUS_MSK) {
2170 case TX_STATUS_SUCCESS:
2171 return "SUCCESS";
2172 TX_STATUS_ENTRY(SHORT_LIMIT);
2173 TX_STATUS_ENTRY(LONG_LIMIT);
2174 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2175 TX_STATUS_ENTRY(MGMNT_ABORT);
2176 TX_STATUS_ENTRY(NEXT_FRAG);
2177 TX_STATUS_ENTRY(LIFE_EXPIRE);
2178 TX_STATUS_ENTRY(DEST_PS);
2179 TX_STATUS_ENTRY(ABORTED);
2180 TX_STATUS_ENTRY(BT_RETRY);
2181 TX_STATUS_ENTRY(STA_INVALID);
2182 TX_STATUS_ENTRY(FRAG_DROPPED);
2183 TX_STATUS_ENTRY(TID_DISABLE);
2184 TX_STATUS_ENTRY(FRAME_FLUSHED);
2185 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2186 TX_STATUS_ENTRY(TX_LOCKED);
2187 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2188 }
2189
2190 return "UNKNOWN";
2191}
2192
2193/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002194 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002195 *
2196 * NOTE: priv->mutex is not required before calling this function
2197 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002198static int iwl4965_scan_cancel(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002199{
2200 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2201 clear_bit(STATUS_SCANNING, &priv->status);
2202 return 0;
2203 }
2204
2205 if (test_bit(STATUS_SCANNING, &priv->status)) {
2206 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2207 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2208 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2209 queue_work(priv->workqueue, &priv->abort_scan);
2210
2211 } else
2212 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2213
2214 return test_bit(STATUS_SCANNING, &priv->status);
2215 }
2216
2217 return 0;
2218}
2219
2220/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002221 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002222 * @ms: amount of time to wait (in milliseconds) for scan to abort
2223 *
2224 * NOTE: priv->mutex must be held before calling this function
2225 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002226static int iwl4965_scan_cancel_timeout(struct iwl4965_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002227{
2228 unsigned long now = jiffies;
2229 int ret;
2230
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002231 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002232 if (ret && ms) {
2233 mutex_unlock(&priv->mutex);
2234 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2235 test_bit(STATUS_SCANNING, &priv->status))
2236 msleep(1);
2237 mutex_lock(&priv->mutex);
2238
2239 return test_bit(STATUS_SCANNING, &priv->status);
2240 }
2241
2242 return ret;
2243}
2244
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002245static void iwl4965_sequence_reset(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002246{
2247 /* Reset ieee stats */
2248
2249 /* We don't reset the net_device_stats (ieee->stats) on
2250 * re-association */
2251
2252 priv->last_seq_num = -1;
2253 priv->last_frag_num = -1;
2254 priv->last_packet_time = 0;
2255
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002256 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002257}
2258
2259#define MAX_UCODE_BEACON_INTERVAL 4096
2260#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2261
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002262static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002263{
2264 u16 new_val = 0;
2265 u16 beacon_factor = 0;
2266
2267 beacon_factor =
2268 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2269 / MAX_UCODE_BEACON_INTERVAL;
2270 new_val = beacon_val / beacon_factor;
2271
2272 return cpu_to_le16(new_val);
2273}
2274
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002275static void iwl4965_setup_rxon_timing(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002276{
2277 u64 interval_tm_unit;
2278 u64 tsf, result;
2279 unsigned long flags;
2280 struct ieee80211_conf *conf = NULL;
2281 u16 beacon_int = 0;
2282
2283 conf = ieee80211_get_hw_conf(priv->hw);
2284
2285 spin_lock_irqsave(&priv->lock, flags);
2286 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2287 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2288
2289 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2290
2291 tsf = priv->timestamp1;
2292 tsf = ((tsf << 32) | priv->timestamp0);
2293
2294 beacon_int = priv->beacon_int;
2295 spin_unlock_irqrestore(&priv->lock, flags);
2296
2297 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2298 if (beacon_int == 0) {
2299 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2300 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2301 } else {
2302 priv->rxon_timing.beacon_interval =
2303 cpu_to_le16(beacon_int);
2304 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002305 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002306 le16_to_cpu(priv->rxon_timing.beacon_interval));
2307 }
2308
2309 priv->rxon_timing.atim_window = 0;
2310 } else {
2311 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002312 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002313 /* TODO: we need to get atim_window from upper stack
2314 * for now we set to 0 */
2315 priv->rxon_timing.atim_window = 0;
2316 }
2317
2318 interval_tm_unit =
2319 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2320 result = do_div(tsf, interval_tm_unit);
2321 priv->rxon_timing.beacon_init_val =
2322 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2323
2324 IWL_DEBUG_ASSOC
2325 ("beacon interval %d beacon timer %d beacon tim %d\n",
2326 le16_to_cpu(priv->rxon_timing.beacon_interval),
2327 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2328 le16_to_cpu(priv->rxon_timing.atim_window));
2329}
2330
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002331static int iwl4965_scan_initiate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002332{
2333 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2334 IWL_ERROR("APs don't scan.\n");
2335 return 0;
2336 }
2337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002338 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002339 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2340 return -EIO;
2341 }
2342
2343 if (test_bit(STATUS_SCANNING, &priv->status)) {
2344 IWL_DEBUG_SCAN("Scan already in progress.\n");
2345 return -EAGAIN;
2346 }
2347
2348 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2349 IWL_DEBUG_SCAN("Scan request while abort pending. "
2350 "Queuing.\n");
2351 return -EAGAIN;
2352 }
2353
2354 IWL_DEBUG_INFO("Starting scan...\n");
2355 priv->scan_bands = 2;
2356 set_bit(STATUS_SCANNING, &priv->status);
2357 priv->scan_start = jiffies;
2358 priv->scan_pass_start = priv->scan_start;
2359
2360 queue_work(priv->workqueue, &priv->request_scan);
2361
2362 return 0;
2363}
2364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002365static int iwl4965_set_rxon_hwcrypto(struct iwl4965_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002366{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002367 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002368
2369 if (hw_decrypt)
2370 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2371 else
2372 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2373
2374 return 0;
2375}
2376
Johannes Berg8318d782008-01-24 19:38:38 +01002377static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv,
2378 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002379{
Johannes Berg8318d782008-01-24 19:38:38 +01002380 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002381 priv->staging_rxon.flags &=
2382 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2383 | RXON_FLG_CCK_MSK);
2384 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2385 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002386 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002387 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2388 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2389 else
2390 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2391
2392 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2393 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2394
2395 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2396 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2397 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2398 }
2399}
2400
2401/*
Ian Schram01ebd062007-10-25 17:15:22 +08002402 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002403 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002404static void iwl4965_connection_init_rx_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002405{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002406 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002407
2408 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2409
2410 switch (priv->iw_mode) {
2411 case IEEE80211_IF_TYPE_AP:
2412 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2413 break;
2414
2415 case IEEE80211_IF_TYPE_STA:
2416 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2417 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2418 break;
2419
2420 case IEEE80211_IF_TYPE_IBSS:
2421 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2422 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2423 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2424 RXON_FILTER_ACCEPT_GRP_MSK;
2425 break;
2426
2427 case IEEE80211_IF_TYPE_MNTR:
2428 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2429 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2430 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2431 break;
2432 }
2433
2434#if 0
2435 /* TODO: Figure out when short_preamble would be set and cache from
2436 * that */
2437 if (!hw_to_local(priv->hw)->short_preamble)
2438 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2439 else
2440 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2441#endif
2442
Johannes Berg8318d782008-01-24 19:38:38 +01002443 ch_info = iwl4965_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002444 le16_to_cpu(priv->staging_rxon.channel));
2445
2446 if (!ch_info)
2447 ch_info = &priv->channel_info[0];
2448
2449 /*
2450 * in some case A channels are all non IBSS
2451 * in this case force B/G channel
2452 */
2453 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2454 !(is_channel_ibss(ch_info)))
2455 ch_info = &priv->channel_info[0];
2456
2457 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002458 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002459
Johannes Berg8318d782008-01-24 19:38:38 +01002460 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002461
2462 priv->staging_rxon.ofdm_basic_rates =
2463 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2464 priv->staging_rxon.cck_basic_rates =
2465 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2466
2467 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2468 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2469 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2470 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2471 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2472 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2473 iwl4965_set_rxon_chain(priv);
2474}
2475
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002476static int iwl4965_set_mode(struct iwl4965_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002477{
Zhu Yib481de92007-09-25 17:54:57 -07002478 if (mode == IEEE80211_IF_TYPE_IBSS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002479 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002480
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002481 ch_info = iwl4965_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002482 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002483 le16_to_cpu(priv->staging_rxon.channel));
2484
2485 if (!ch_info || !is_channel_ibss(ch_info)) {
2486 IWL_ERROR("channel %d not IBSS channel\n",
2487 le16_to_cpu(priv->staging_rxon.channel));
2488 return -EINVAL;
2489 }
2490 }
2491
Zhu Yib481de92007-09-25 17:54:57 -07002492 priv->iw_mode = mode;
2493
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002494 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002495 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2496
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002497 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002498
Mohamed Abbasfde35712007-11-29 11:10:15 +08002499 /* dont commit rxon if rf-kill is on*/
2500 if (!iwl4965_is_ready_rf(priv))
2501 return -EAGAIN;
2502
2503 cancel_delayed_work(&priv->scan_check);
2504 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2505 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2506 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2507 return -EAGAIN;
2508 }
2509
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002510 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002511
2512 return 0;
2513}
2514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002515static void iwl4965_build_tx_cmd_hwcrypto(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002516 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002517 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002518 struct sk_buff *skb_frag,
2519 int last_frag)
2520{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002521 struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002522
2523 switch (keyinfo->alg) {
2524 case ALG_CCMP:
2525 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2526 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2527 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2528 break;
2529
2530 case ALG_TKIP:
2531#if 0
2532 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2533
2534 if (last_frag)
2535 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2536 8);
2537 else
2538 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2539#endif
2540 break;
2541
2542 case ALG_WEP:
2543 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2544 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2545
2546 if (keyinfo->keylen == 13)
2547 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2548
2549 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2550
2551 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2552 "with key %d\n", ctl->key_idx);
2553 break;
2554
Zhu Yib481de92007-09-25 17:54:57 -07002555 default:
2556 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2557 break;
2558 }
2559}
2560
2561/*
2562 * handle build REPLY_TX command notification.
2563 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002564static void iwl4965_build_tx_cmd_basic(struct iwl4965_priv *priv,
2565 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002566 struct ieee80211_tx_control *ctrl,
2567 struct ieee80211_hdr *hdr,
2568 int is_unicast, u8 std_id)
2569{
2570 __le16 *qc;
2571 u16 fc = le16_to_cpu(hdr->frame_control);
2572 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2573
2574 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2575 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2576 tx_flags |= TX_CMD_FLG_ACK_MSK;
2577 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2578 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2579 if (ieee80211_is_probe_response(fc) &&
2580 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2581 tx_flags |= TX_CMD_FLG_TSF_MSK;
2582 } else {
2583 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2584 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2585 }
2586
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002587 if (ieee80211_is_back_request(fc))
2588 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2589
2590
Zhu Yib481de92007-09-25 17:54:57 -07002591 cmd->cmd.tx.sta_id = std_id;
2592 if (ieee80211_get_morefrag(hdr))
2593 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2594
2595 qc = ieee80211_get_qos_ctrl(hdr);
2596 if (qc) {
2597 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2598 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2599 } else
2600 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2601
2602 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2603 tx_flags |= TX_CMD_FLG_RTS_MSK;
2604 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2605 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2606 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2607 tx_flags |= TX_CMD_FLG_CTS_MSK;
2608 }
2609
2610 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2611 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2612
2613 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2614 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2615 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2616 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002617 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002618 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002619 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002620 } else
2621 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2622
2623 cmd->cmd.tx.driver_txop = 0;
2624 cmd->cmd.tx.tx_flags = tx_flags;
2625 cmd->cmd.tx.next_frame_len = 0;
2626}
2627
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002628/**
2629 * iwl4965_get_sta_id - Find station's index within station table
2630 *
2631 * If new IBSS station, create new entry in station table
2632 */
Ben Cahill9fbab512007-11-29 11:09:47 +08002633static int iwl4965_get_sta_id(struct iwl4965_priv *priv,
2634 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002635{
2636 int sta_id;
2637 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002638 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002639
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002640 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002641 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2642 is_multicast_ether_addr(hdr->addr1))
2643 return priv->hw_setting.bcast_sta_id;
2644
2645 switch (priv->iw_mode) {
2646
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002647 /* If we are a client station in a BSS network, use the special
2648 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002649 case IEEE80211_IF_TYPE_STA:
2650 return IWL_AP_ID;
2651
2652 /* If we are an AP, then find the station, or use BCAST */
2653 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002654 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002655 if (sta_id != IWL_INVALID_STATION)
2656 return sta_id;
2657 return priv->hw_setting.bcast_sta_id;
2658
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002659 /* If this frame is going out to an IBSS network, find the station,
2660 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002661 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002662 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002663 if (sta_id != IWL_INVALID_STATION)
2664 return sta_id;
2665
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002666 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002667 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2668 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002669
2670 if (sta_id != IWL_INVALID_STATION)
2671 return sta_id;
2672
Joe Perches0795af52007-10-03 17:59:30 -07002673 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002674 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002675 print_mac(mac, hdr->addr1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002676 iwl4965_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002677 return priv->hw_setting.bcast_sta_id;
2678
2679 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002680 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002681 return priv->hw_setting.bcast_sta_id;
2682 }
2683}
2684
2685/*
2686 * start REPLY_TX command process
2687 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002688static int iwl4965_tx_skb(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002689 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2690{
2691 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002692 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002693 u32 *control_flags;
2694 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002695 struct iwl4965_tx_queue *txq = NULL;
2696 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002697 dma_addr_t phys_addr;
2698 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002699 dma_addr_t scratch_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002700 struct iwl4965_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002701 u16 len, idx, len_org;
2702 u8 id, hdr_len, unicast;
2703 u8 sta_id;
2704 u16 seq_number = 0;
2705 u16 fc;
2706 __le16 *qc;
2707 u8 wait_write_ptr = 0;
2708 unsigned long flags;
2709 int rc;
2710
2711 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002712 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002713 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2714 goto drop_unlock;
2715 }
2716
Johannes Berg32bfd352007-12-19 01:31:26 +01002717 if (!priv->vif) {
2718 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002719 goto drop_unlock;
2720 }
2721
Johannes Berg8318d782008-01-24 19:38:38 +01002722 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002723 IWL_ERROR("ERROR: No TX rate available.\n");
2724 goto drop_unlock;
2725 }
2726
2727 unicast = !is_multicast_ether_addr(hdr->addr1);
2728 id = 0;
2729
2730 fc = le16_to_cpu(hdr->frame_control);
2731
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002732#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002733 if (ieee80211_is_auth(fc))
2734 IWL_DEBUG_TX("Sending AUTH frame\n");
2735 else if (ieee80211_is_assoc_request(fc))
2736 IWL_DEBUG_TX("Sending ASSOC frame\n");
2737 else if (ieee80211_is_reassoc_request(fc))
2738 IWL_DEBUG_TX("Sending REASSOC frame\n");
2739#endif
2740
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002741 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002742 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2743 (!iwl4965_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002744 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002745 !priv->assoc_station_added)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002746 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002747 goto drop_unlock;
2748 }
2749
2750 spin_unlock_irqrestore(&priv->lock, flags);
2751
2752 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002753
2754 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002755 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002756 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002757 DECLARE_MAC_BUF(mac);
2758
2759 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2760 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002761 goto drop;
2762 }
2763
2764 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2765
2766 qc = ieee80211_get_qos_ctrl(hdr);
2767 if (qc) {
2768 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2769 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2770 IEEE80211_SCTL_SEQ;
2771 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2772 (hdr->seq_ctrl &
2773 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2774 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002775#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002776 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002777 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002778 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002779 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002780#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002781 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002782
2783 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002784 txq = &priv->txq[txq_id];
2785 q = &txq->q;
2786
2787 spin_lock_irqsave(&priv->lock, flags);
2788
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002789 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002790 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002791 memset(tfd, 0, sizeof(*tfd));
2792 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002793 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002794
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002795 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002796 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002797 txq->txb[q->write_ptr].skb[0] = skb;
2798 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002799 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002800
2801 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002802 out_cmd = &txq->cmd[idx];
2803 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2804 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002805
2806 /*
2807 * Set up the Tx-command (not MAC!) header.
2808 * Store the chosen Tx queue and TFD index within the sequence field;
2809 * after Tx, uCode's Tx response will return this value so driver can
2810 * locate the frame within the tx queue and do post-tx processing.
2811 */
Zhu Yib481de92007-09-25 17:54:57 -07002812 out_cmd->hdr.cmd = REPLY_TX;
2813 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002814 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002815
2816 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002817 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2818
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002819 /*
2820 * Use the first empty entry in this queue's command buffer array
2821 * to contain the Tx command and MAC header concatenated together
2822 * (payload data will be in another buffer).
2823 * Size of this varies, due to varying MAC header length.
2824 * If end is not dword aligned, we'll have 2 extra bytes at the end
2825 * of the MAC header (device reads on dword boundaries).
2826 * We'll tell device about this padding later.
2827 */
Zhu Yib481de92007-09-25 17:54:57 -07002828 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002829 sizeof(struct iwl4965_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002830
2831 len_org = len;
2832 len = (len + 3) & ~3;
2833
2834 if (len_org != len)
2835 len_org = 1;
2836 else
2837 len_org = 0;
2838
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002839 /* Physical address of this Tx command's header (not MAC header!),
2840 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002841 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
2842 offsetof(struct iwl4965_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002843
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002844 /* Add buffer containing Tx command and MAC(!) header to TFD's
2845 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002846 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002847
2848 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002849 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002850
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002851 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2852 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002853 len = skb->len - hdr_len;
2854 if (len) {
2855 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2856 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002857 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002858 }
2859
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002860 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002861 if (len_org)
2862 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2863
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002864 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002865 len = (u16)skb->len;
2866 out_cmd->cmd.tx.len = cpu_to_le16(len);
2867
2868 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002869 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002870
2871 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002872 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002873
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002874 scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
2875 offsetof(struct iwl4965_tx_cmd, scratch);
2876 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2877 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2878
Zhu Yib481de92007-09-25 17:54:57 -07002879 if (!ieee80211_get_morefrag(hdr)) {
2880 txq->need_update = 1;
2881 if (qc) {
2882 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2883 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2884 }
2885 } else {
2886 wait_write_ptr = 1;
2887 txq->need_update = 0;
2888 }
2889
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002890 iwl4965_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002891 sizeof(out_cmd->cmd.tx));
2892
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002893 iwl4965_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002894 ieee80211_get_hdrlen(fc));
2895
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002896 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002897 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2898
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002899 /* Tell device the write index *just past* this latest filled TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002900 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
2901 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002902 spin_unlock_irqrestore(&priv->lock, flags);
2903
2904 if (rc)
2905 return rc;
2906
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002907 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002908 && priv->mac80211_registered) {
2909 if (wait_write_ptr) {
2910 spin_lock_irqsave(&priv->lock, flags);
2911 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002912 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002913 spin_unlock_irqrestore(&priv->lock, flags);
2914 }
2915
2916 ieee80211_stop_queue(priv->hw, ctl->queue);
2917 }
2918
2919 return 0;
2920
2921drop_unlock:
2922 spin_unlock_irqrestore(&priv->lock, flags);
2923drop:
2924 return -1;
2925}
2926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002927static void iwl4965_set_rate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002928{
Johannes Berg8318d782008-01-24 19:38:38 +01002929 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002930 struct ieee80211_rate *rate;
2931 int i;
2932
Johannes Berg8318d782008-01-24 19:38:38 +01002933 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002934 if (!hw) {
2935 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2936 return;
2937 }
Zhu Yib481de92007-09-25 17:54:57 -07002938
2939 priv->active_rate = 0;
2940 priv->active_rate_basic = 0;
2941
Johannes Berg8318d782008-01-24 19:38:38 +01002942 for (i = 0; i < hw->n_bitrates; i++) {
2943 rate = &(hw->bitrates[i]);
2944 if (rate->hw_value < IWL_RATE_COUNT)
2945 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002946 }
2947
2948 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2949 priv->active_rate, priv->active_rate_basic);
2950
2951 /*
2952 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2953 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2954 * OFDM
2955 */
2956 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2957 priv->staging_rxon.cck_basic_rates =
2958 ((priv->active_rate_basic &
2959 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2960 else
2961 priv->staging_rxon.cck_basic_rates =
2962 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2963
2964 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2965 priv->staging_rxon.ofdm_basic_rates =
2966 ((priv->active_rate_basic &
2967 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2968 IWL_FIRST_OFDM_RATE) & 0xFF;
2969 else
2970 priv->staging_rxon.ofdm_basic_rates =
2971 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2972}
2973
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002974static void iwl4965_radio_kill_sw(struct iwl4965_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002975{
2976 unsigned long flags;
2977
2978 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2979 return;
2980
2981 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2982 disable_radio ? "OFF" : "ON");
2983
2984 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002985 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002986 /* FIXME: This is a workaround for AP */
2987 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2988 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002989 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002990 CSR_UCODE_SW_BIT_RFKILL);
2991 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002992 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002993 set_bit(STATUS_RF_KILL_SW, &priv->status);
2994 }
2995 return;
2996 }
2997
2998 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002999 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07003000
3001 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3002 spin_unlock_irqrestore(&priv->lock, flags);
3003
3004 /* wake up ucode */
3005 msleep(10);
3006
3007 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003008 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3009 if (!iwl4965_grab_nic_access(priv))
3010 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003011 spin_unlock_irqrestore(&priv->lock, flags);
3012
3013 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3014 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3015 "disabled by HW switch\n");
3016 return;
3017 }
3018
3019 queue_work(priv->workqueue, &priv->restart);
3020 return;
3021}
3022
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003023void iwl4965_set_decrypted_flag(struct iwl4965_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07003024 u32 decrypt_res, struct ieee80211_rx_status *stats)
3025{
3026 u16 fc =
3027 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3028
3029 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3030 return;
3031
3032 if (!(fc & IEEE80211_FCTL_PROTECTED))
3033 return;
3034
3035 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3036 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3037 case RX_RES_STATUS_SEC_TYPE_TKIP:
3038 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3039 RX_RES_STATUS_BAD_ICV_MIC)
3040 stats->flag |= RX_FLAG_MMIC_ERROR;
3041 case RX_RES_STATUS_SEC_TYPE_WEP:
3042 case RX_RES_STATUS_SEC_TYPE_CCMP:
3043 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3044 RX_RES_STATUS_DECRYPT_OK) {
3045 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3046 stats->flag |= RX_FLAG_DECRYPTED;
3047 }
3048 break;
3049
3050 default:
3051 break;
3052 }
3053}
3054
Zhu Yib481de92007-09-25 17:54:57 -07003055
3056#define IWL_PACKET_RETRY_TIME HZ
3057
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003058int iwl4965_is_duplicate_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07003059{
3060 u16 sc = le16_to_cpu(header->seq_ctrl);
3061 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3062 u16 frag = sc & IEEE80211_SCTL_FRAG;
3063 u16 *last_seq, *last_frag;
3064 unsigned long *last_time;
3065
3066 switch (priv->iw_mode) {
3067 case IEEE80211_IF_TYPE_IBSS:{
3068 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003069 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003070 u8 *mac = header->addr2;
3071 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3072
3073 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003074 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07003075 if (!compare_ether_addr(entry->mac, mac))
3076 break;
3077 }
3078 if (p == &priv->ibss_mac_hash[index]) {
3079 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3080 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08003081 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07003082 return 0;
3083 }
3084 memcpy(entry->mac, mac, ETH_ALEN);
3085 entry->seq_num = seq;
3086 entry->frag_num = frag;
3087 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08003088 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07003089 return 0;
3090 }
3091 last_seq = &entry->seq_num;
3092 last_frag = &entry->frag_num;
3093 last_time = &entry->packet_time;
3094 break;
3095 }
3096 case IEEE80211_IF_TYPE_STA:
3097 last_seq = &priv->last_seq_num;
3098 last_frag = &priv->last_frag_num;
3099 last_time = &priv->last_packet_time;
3100 break;
3101 default:
3102 return 0;
3103 }
3104 if ((*last_seq == seq) &&
3105 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3106 if (*last_frag == frag)
3107 goto drop;
3108 if (*last_frag + 1 != frag)
3109 /* out-of-order fragment */
3110 goto drop;
3111 } else
3112 *last_seq = seq;
3113
3114 *last_frag = frag;
3115 *last_time = jiffies;
3116 return 0;
3117
3118 drop:
3119 return 1;
3120}
3121
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003122#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003123
3124#include "iwl-spectrum.h"
3125
3126#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3127#define BEACON_TIME_MASK_HIGH 0xFF000000
3128#define TIME_UNIT 1024
3129
3130/*
3131 * extended beacon time format
3132 * time in usec will be changed into a 32-bit value in 8:24 format
3133 * the high 1 byte is the beacon counts
3134 * the lower 3 bytes is the time in usec within one beacon interval
3135 */
3136
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003137static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003138{
3139 u32 quot;
3140 u32 rem;
3141 u32 interval = beacon_interval * 1024;
3142
3143 if (!interval || !usec)
3144 return 0;
3145
3146 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3147 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3148
3149 return (quot << 24) + rem;
3150}
3151
3152/* base is usually what we get from ucode with each received frame,
3153 * the same as HW timer counter counting down
3154 */
3155
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003156static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003157{
3158 u32 base_low = base & BEACON_TIME_MASK_LOW;
3159 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3160 u32 interval = beacon_interval * TIME_UNIT;
3161 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3162 (addon & BEACON_TIME_MASK_HIGH);
3163
3164 if (base_low > addon_low)
3165 res += base_low - addon_low;
3166 else if (base_low < addon_low) {
3167 res += interval + base_low - addon_low;
3168 res += (1 << 24);
3169 } else
3170 res += (1 << 24);
3171
3172 return cpu_to_le32(res);
3173}
3174
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003175static int iwl4965_get_measurement(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003176 struct ieee80211_measurement_params *params,
3177 u8 type)
3178{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003179 struct iwl4965_spectrum_cmd spectrum;
3180 struct iwl4965_rx_packet *res;
3181 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003182 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3183 .data = (void *)&spectrum,
3184 .meta.flags = CMD_WANT_SKB,
3185 };
3186 u32 add_time = le64_to_cpu(params->start_time);
3187 int rc;
3188 int spectrum_resp_status;
3189 int duration = le16_to_cpu(params->duration);
3190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003191 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003192 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003193 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003194 le64_to_cpu(params->start_time) - priv->last_tsf,
3195 le16_to_cpu(priv->rxon_timing.beacon_interval));
3196
3197 memset(&spectrum, 0, sizeof(spectrum));
3198
3199 spectrum.channel_count = cpu_to_le16(1);
3200 spectrum.flags =
3201 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3202 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3203 cmd.len = sizeof(spectrum);
3204 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3205
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003206 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003207 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003208 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003209 add_time,
3210 le16_to_cpu(priv->rxon_timing.beacon_interval));
3211 else
3212 spectrum.start_time = 0;
3213
3214 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3215 spectrum.channels[0].channel = params->channel;
3216 spectrum.channels[0].type = type;
3217 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3218 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3219 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3220
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003221 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003222 if (rc)
3223 return rc;
3224
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003225 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003226 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3227 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3228 rc = -EIO;
3229 }
3230
3231 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3232 switch (spectrum_resp_status) {
3233 case 0: /* Command will be handled */
3234 if (res->u.spectrum.id != 0xff) {
3235 IWL_DEBUG_INFO
3236 ("Replaced existing measurement: %d\n",
3237 res->u.spectrum.id);
3238 priv->measurement_status &= ~MEASUREMENT_READY;
3239 }
3240 priv->measurement_status |= MEASUREMENT_ACTIVE;
3241 rc = 0;
3242 break;
3243
3244 case 1: /* Command will not be handled */
3245 rc = -EAGAIN;
3246 break;
3247 }
3248
3249 dev_kfree_skb_any(cmd.meta.u.skb);
3250
3251 return rc;
3252}
3253#endif
3254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003255static void iwl4965_txstatus_to_ieee(struct iwl4965_priv *priv,
3256 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003257{
3258
3259 tx_sta->status.ack_signal = 0;
3260 tx_sta->status.excessive_retries = 0;
3261 tx_sta->status.queue_length = 0;
3262 tx_sta->status.queue_number = 0;
3263
3264 if (in_interrupt())
3265 ieee80211_tx_status_irqsafe(priv->hw,
3266 tx_sta->skb[0], &(tx_sta->status));
3267 else
3268 ieee80211_tx_status(priv->hw,
3269 tx_sta->skb[0], &(tx_sta->status));
3270
3271 tx_sta->skb[0] = NULL;
3272}
3273
3274/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003275 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07003276 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003277 * When FW advances 'R' index, all entries between old and new 'R' index
3278 * need to be reclaimed. As result, some free space forms. If there is
3279 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07003280 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003281int iwl4965_tx_queue_reclaim(struct iwl4965_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003282{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003283 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3284 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003285 int nfreed = 0;
3286
3287 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3288 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3289 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003290 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003291 return 0;
3292 }
3293
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003294 for (index = iwl4965_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003295 q->read_ptr != index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003296 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003297 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003298 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003299 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003300 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003301 } else if (nfreed > 1) {
3302 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003303 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003304 queue_work(priv->workqueue, &priv->restart);
3305 }
3306 nfreed++;
3307 }
3308
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003309/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003310 (txq_id != IWL_CMD_QUEUE_NUM) &&
3311 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003312 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07003313
3314
3315 return nfreed;
3316}
3317
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003318static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003319{
3320 status &= TX_STATUS_MSK;
3321 return (status == TX_STATUS_SUCCESS)
3322 || (status == TX_STATUS_DIRECT_DONE);
3323}
3324
3325/******************************************************************************
3326 *
3327 * Generic RX handler implementations
3328 *
3329 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003330#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003331
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003332static inline int iwl4965_get_ra_sta_id(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003333 struct ieee80211_hdr *hdr)
3334{
3335 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3336 return IWL_AP_ID;
3337 else {
3338 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003339 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07003340 }
3341}
3342
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003343static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
3344 struct iwl4965_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07003345{
3346 if (priv->txq[txq_id].txb[idx].skb[0])
3347 return (struct ieee80211_hdr *)priv->txq[txq_id].
3348 txb[idx].skb[0]->data;
3349 return NULL;
3350}
3351
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003352static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003353{
3354 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3355 tx_resp->frame_count);
3356 return le32_to_cpu(*scd_ssn) & MAX_SN;
3357
3358}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003359
3360/**
3361 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3362 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003363static int iwl4965_tx_status_reply_tx(struct iwl4965_priv *priv,
3364 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003365 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003366 u16 start_idx)
3367{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003368 u16 status;
3369 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07003370 struct ieee80211_tx_status *tx_status = NULL;
3371 struct ieee80211_hdr *hdr = NULL;
3372 int i, sh;
3373 int txq_id, idx;
3374 u16 seq;
3375
3376 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003377 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003378
3379 agg->frame_count = tx_resp->frame_count;
3380 agg->start_idx = start_idx;
3381 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003382 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003383
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003384 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003385 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003386 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003387 status = le16_to_cpu(frame_status[0].status);
3388 seq = le16_to_cpu(frame_status[0].sequence);
3389 idx = SEQ_TO_INDEX(seq);
3390 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003391
Zhu Yib481de92007-09-25 17:54:57 -07003392 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003393 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3394 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003395
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003396 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003397 tx_status->retry_count = tx_resp->failure_frame;
3398 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003399 tx_status->queue_length = tx_resp->failure_rts;
3400 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003401 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003402 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003403 iwl4965_hwrate_to_tx_control(priv,
3404 le32_to_cpu(tx_resp->rate_n_flags),
3405 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003406 /* FIXME: code repetition end */
3407
3408 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3409 status & 0xff, tx_resp->failure_frame);
3410 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003411 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003412
3413 agg->wait_for_ba = 0;
3414 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003415 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003416 u64 bitmap = 0;
3417 int start = agg->start_idx;
3418
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003419 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003420 for (i = 0; i < agg->frame_count; i++) {
3421 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003422 status = le16_to_cpu(frame_status[i].status);
3423 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003424 idx = SEQ_TO_INDEX(seq);
3425 txq_id = SEQ_TO_QUEUE(seq);
3426
3427 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3428 AGG_TX_STATE_ABORT_MSK))
3429 continue;
3430
3431 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3432 agg->frame_count, txq_id, idx);
3433
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003434 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003435
3436 sc = le16_to_cpu(hdr->seq_ctrl);
3437 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3438 IWL_ERROR("BUG_ON idx doesn't match seq control"
3439 " idx=%d, seq_idx=%d, seq=%d\n",
3440 idx, SEQ_TO_SN(sc),
3441 hdr->seq_ctrl);
3442 return -1;
3443 }
3444
3445 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3446 i, idx, SEQ_TO_SN(sc));
3447
3448 sh = idx - start;
3449 if (sh > 64) {
3450 sh = (start - idx) + 0xff;
3451 bitmap = bitmap << sh;
3452 sh = 0;
3453 start = idx;
3454 } else if (sh < -64)
3455 sh = 0xff - (start - idx);
3456 else if (sh < 0) {
3457 sh = start - idx;
3458 start = idx;
3459 bitmap = bitmap << sh;
3460 sh = 0;
3461 }
3462 bitmap |= (1 << sh);
3463 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3464 start, (u32)(bitmap & 0xFFFFFFFF));
3465 }
3466
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003467 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003468 agg->start_idx = start;
3469 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003470 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003471 agg->frame_count, agg->start_idx,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003472 agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003473
3474 if (bitmap)
3475 agg->wait_for_ba = 1;
3476 }
3477 return 0;
3478}
3479#endif
Zhu Yib481de92007-09-25 17:54:57 -07003480
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003481/**
3482 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3483 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003484static void iwl4965_rx_reply_tx(struct iwl4965_priv *priv,
3485 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003486{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003487 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003488 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3489 int txq_id = SEQ_TO_QUEUE(sequence);
3490 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003491 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003492 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003493 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003494 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003495#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003496 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3497 struct ieee80211_hdr *hdr;
3498 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003499#endif
3500
3501 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3502 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3503 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003504 index, txq->q.n_bd, txq->q.write_ptr,
3505 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003506 return;
3507 }
3508
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003509#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003510 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3511 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003512
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003513 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003514 tid = le16_to_cpu(*qc) & 0xf;
3515
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003516 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3517 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3518 IWL_ERROR("Station not known\n");
3519 return;
3520 }
3521
3522 if (txq->sched_retry) {
3523 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3524 struct iwl4965_ht_agg *agg = NULL;
3525
3526 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003527 return;
Zhu Yib481de92007-09-25 17:54:57 -07003528
3529 agg = &priv->stations[sta_id].tid[tid].agg;
3530
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003531 iwl4965_tx_status_reply_tx(priv, agg,
3532 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003533
3534 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003535 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003536 /* TODO: send BAR */
3537 }
3538
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003539 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3540 int freed;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003541 index = iwl4965_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003542 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3543 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003544 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3545 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3546
3547 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3548 txq_id >= 0 && priv->mac80211_registered &&
3549 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3550 ieee80211_wake_queue(priv->hw, txq_id);
3551
3552 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003553 }
3554 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003555#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003556 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003557
3558 tx_status->retry_count = tx_resp->failure_frame;
3559 tx_status->queue_number = status;
3560 tx_status->queue_length = tx_resp->bt_kill_count;
3561 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003562 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003563 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003564 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3565 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003566
Zhu Yib481de92007-09-25 17:54:57 -07003567 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003568 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003569 status, le32_to_cpu(tx_resp->rate_n_flags),
3570 tx_resp->failure_frame);
3571
3572 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003573 if (index != -1) {
3574 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003575#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003576 if (tid != MAX_TID_COUNT)
3577 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3578 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3579 (txq_id >= 0) &&
3580 priv->mac80211_registered)
3581 ieee80211_wake_queue(priv->hw, txq_id);
3582 if (tid != MAX_TID_COUNT)
3583 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3584#endif
Zhu Yib481de92007-09-25 17:54:57 -07003585 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003586#ifdef CONFIG_IWL4965_HT
3587 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003588#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003589
3590 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3591 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3592}
3593
3594
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003595static void iwl4965_rx_reply_alive(struct iwl4965_priv *priv,
3596 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003597{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003598 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3599 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003600 struct delayed_work *pwork;
3601
3602 palive = &pkt->u.alive_frame;
3603
3604 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3605 "0x%01X 0x%01X\n",
3606 palive->is_valid, palive->ver_type,
3607 palive->ver_subtype);
3608
3609 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3610 IWL_DEBUG_INFO("Initialization Alive received.\n");
3611 memcpy(&priv->card_alive_init,
3612 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003613 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003614 pwork = &priv->init_alive_start;
3615 } else {
3616 IWL_DEBUG_INFO("Runtime Alive received.\n");
3617 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003618 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003619 pwork = &priv->alive_start;
3620 }
3621
3622 /* We delay the ALIVE response by 5ms to
3623 * give the HW RF Kill time to activate... */
3624 if (palive->is_valid == UCODE_VALID_OK)
3625 queue_delayed_work(priv->workqueue, pwork,
3626 msecs_to_jiffies(5));
3627 else
3628 IWL_WARNING("uCode did not respond OK.\n");
3629}
3630
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003631static void iwl4965_rx_reply_add_sta(struct iwl4965_priv *priv,
3632 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003633{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003634 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003635
3636 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3637 return;
3638}
3639
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003640static void iwl4965_rx_reply_error(struct iwl4965_priv *priv,
3641 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003642{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003643 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003644
3645 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3646 "seq 0x%04X ser 0x%08X\n",
3647 le32_to_cpu(pkt->u.err_resp.error_type),
3648 get_cmd_string(pkt->u.err_resp.cmd_id),
3649 pkt->u.err_resp.cmd_id,
3650 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3651 le32_to_cpu(pkt->u.err_resp.error_info));
3652}
3653
3654#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3655
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003656static void iwl4965_rx_csa(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003657{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003658 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3659 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3660 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003661 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3662 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3663 rxon->channel = csa->channel;
3664 priv->staging_rxon.channel = csa->channel;
3665}
3666
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003667static void iwl4965_rx_spectrum_measure_notif(struct iwl4965_priv *priv,
3668 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003669{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003670#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003671 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3672 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003673
3674 if (!report->state) {
3675 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3676 "Spectrum Measure Notification: Start\n");
3677 return;
3678 }
3679
3680 memcpy(&priv->measure_report, report, sizeof(*report));
3681 priv->measurement_status |= MEASUREMENT_READY;
3682#endif
3683}
3684
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003685static void iwl4965_rx_pm_sleep_notif(struct iwl4965_priv *priv,
3686 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003687{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003688#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003689 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3690 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003691 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3692 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3693#endif
3694}
3695
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003696static void iwl4965_rx_pm_debug_statistics_notif(struct iwl4965_priv *priv,
3697 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003698{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003699 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003700 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3701 "notification for %s:\n",
3702 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003703 iwl4965_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003704}
3705
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003706static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003707{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003708 struct iwl4965_priv *priv =
3709 container_of(work, struct iwl4965_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003710 struct sk_buff *beacon;
3711
3712 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003713 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003714
3715 if (!beacon) {
3716 IWL_ERROR("update beacon failed\n");
3717 return;
3718 }
3719
3720 mutex_lock(&priv->mutex);
3721 /* new beacon skb is allocated every time; dispose previous.*/
3722 if (priv->ibss_beacon)
3723 dev_kfree_skb(priv->ibss_beacon);
3724
3725 priv->ibss_beacon = beacon;
3726 mutex_unlock(&priv->mutex);
3727
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003728 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003729}
3730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003731static void iwl4965_rx_beacon_notif(struct iwl4965_priv *priv,
3732 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003733{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003734#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003735 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3736 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3737 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003738
3739 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3740 "tsf %d %d rate %d\n",
3741 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3742 beacon->beacon_notify_hdr.failure_frame,
3743 le32_to_cpu(beacon->ibss_mgr_status),
3744 le32_to_cpu(beacon->high_tsf),
3745 le32_to_cpu(beacon->low_tsf), rate);
3746#endif
3747
3748 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3749 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3750 queue_work(priv->workqueue, &priv->beacon_update);
3751}
3752
3753/* Service response to REPLY_SCAN_CMD (0x80) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003754static void iwl4965_rx_reply_scan(struct iwl4965_priv *priv,
3755 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003756{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003757#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003758 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3759 struct iwl4965_scanreq_notification *notif =
3760 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003761
3762 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3763#endif
3764}
3765
3766/* Service SCAN_START_NOTIFICATION (0x82) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003767static void iwl4965_rx_scan_start_notif(struct iwl4965_priv *priv,
3768 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003769{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003770 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3771 struct iwl4965_scanstart_notification *notif =
3772 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003773 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3774 IWL_DEBUG_SCAN("Scan start: "
3775 "%d [802.11%s] "
3776 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3777 notif->channel,
3778 notif->band ? "bg" : "a",
3779 notif->tsf_high,
3780 notif->tsf_low, notif->status, notif->beacon_timer);
3781}
3782
3783/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003784static void iwl4965_rx_scan_results_notif(struct iwl4965_priv *priv,
3785 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003786{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003787 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3788 struct iwl4965_scanresults_notification *notif =
3789 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003790
3791 IWL_DEBUG_SCAN("Scan ch.res: "
3792 "%d [802.11%s] "
3793 "(TSF: 0x%08X:%08X) - %d "
3794 "elapsed=%lu usec (%dms since last)\n",
3795 notif->channel,
3796 notif->band ? "bg" : "a",
3797 le32_to_cpu(notif->tsf_high),
3798 le32_to_cpu(notif->tsf_low),
3799 le32_to_cpu(notif->statistics[0]),
3800 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3801 jiffies_to_msecs(elapsed_jiffies
3802 (priv->last_scan_jiffies, jiffies)));
3803
3804 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003805 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003806}
3807
3808/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003809static void iwl4965_rx_scan_complete_notif(struct iwl4965_priv *priv,
3810 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003811{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003812 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3813 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003814
3815 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3816 scan_notif->scanned_channels,
3817 scan_notif->tsf_low,
3818 scan_notif->tsf_high, scan_notif->status);
3819
3820 /* The HW is no longer scanning */
3821 clear_bit(STATUS_SCAN_HW, &priv->status);
3822
3823 /* The scan completion notification came in, so kill that timer... */
3824 cancel_delayed_work(&priv->scan_check);
3825
3826 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3827 (priv->scan_bands == 2) ? "2.4" : "5.2",
3828 jiffies_to_msecs(elapsed_jiffies
3829 (priv->scan_pass_start, jiffies)));
3830
3831 /* Remove this scanned band from the list
3832 * of pending bands to scan */
3833 priv->scan_bands--;
3834
3835 /* If a request to abort was given, or the scan did not succeed
3836 * then we reset the scan state machine and terminate,
3837 * re-queuing another scan if one has been requested */
3838 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3839 IWL_DEBUG_INFO("Aborted scan completed.\n");
3840 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3841 } else {
3842 /* If there are more bands on this scan pass reschedule */
3843 if (priv->scan_bands > 0)
3844 goto reschedule;
3845 }
3846
3847 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003848 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003849 IWL_DEBUG_INFO("Setting scan to off\n");
3850
3851 clear_bit(STATUS_SCANNING, &priv->status);
3852
3853 IWL_DEBUG_INFO("Scan took %dms\n",
3854 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3855
3856 queue_work(priv->workqueue, &priv->scan_completed);
3857
3858 return;
3859
3860reschedule:
3861 priv->scan_pass_start = jiffies;
3862 queue_work(priv->workqueue, &priv->request_scan);
3863}
3864
3865/* Handle notification from uCode that card's power state is changing
3866 * due to software, hardware, or critical temperature RFKILL */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003867static void iwl4965_rx_card_state_notif(struct iwl4965_priv *priv,
3868 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003869{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003870 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003871 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3872 unsigned long status = priv->status;
3873
3874 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3875 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3876 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3877
3878 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3879 RF_CARD_DISABLED)) {
3880
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003881 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003882 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3883
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003884 if (!iwl4965_grab_nic_access(priv)) {
3885 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003886 priv, HBUS_TARG_MBX_C,
3887 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3888
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003889 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003890 }
3891
3892 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003893 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003894 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003895 if (!iwl4965_grab_nic_access(priv)) {
3896 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003897 priv, HBUS_TARG_MBX_C,
3898 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3899
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003900 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003901 }
3902 }
3903
3904 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003905 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003906 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003907 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3908 if (!iwl4965_grab_nic_access(priv))
3909 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003910 }
3911 }
3912
3913 if (flags & HW_CARD_DISABLED)
3914 set_bit(STATUS_RF_KILL_HW, &priv->status);
3915 else
3916 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3917
3918
3919 if (flags & SW_CARD_DISABLED)
3920 set_bit(STATUS_RF_KILL_SW, &priv->status);
3921 else
3922 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3923
3924 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003925 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003926
3927 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3928 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3929 (test_bit(STATUS_RF_KILL_SW, &status) !=
3930 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3931 queue_work(priv->workqueue, &priv->rf_kill);
3932 else
3933 wake_up_interruptible(&priv->wait_command_queue);
3934}
3935
3936/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003937 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003938 *
3939 * Setup the RX handlers for each of the reply types sent from the uCode
3940 * to the host.
3941 *
3942 * This function chains into the hardware specific files for them to setup
3943 * any hardware specific handlers as well.
3944 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003945static void iwl4965_setup_rx_handlers(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003946{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003947 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3948 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3949 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3950 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003951 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003952 iwl4965_rx_spectrum_measure_notif;
3953 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003954 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003955 iwl4965_rx_pm_debug_statistics_notif;
3956 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003957
Ben Cahill9fbab512007-11-29 11:09:47 +08003958 /*
3959 * The same handler is used for both the REPLY to a discrete
3960 * statistics request from the host as well as for the periodic
3961 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003962 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003963 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3964 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003965
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003966 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3967 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003968 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003969 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003970 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003971 iwl4965_rx_scan_complete_notif;
3972 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3973 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003974
Ben Cahill9fbab512007-11-29 11:09:47 +08003975 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003976 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003977}
3978
3979/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003980 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003981 * @rxb: Rx buffer to reclaim
3982 *
3983 * If an Rx buffer has an async callback associated with it the callback
3984 * will be executed. The attached skb (if present) will only be freed
3985 * if the callback returns 1
3986 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003987static void iwl4965_tx_cmd_complete(struct iwl4965_priv *priv,
3988 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003989{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003990 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003991 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3992 int txq_id = SEQ_TO_QUEUE(sequence);
3993 int index = SEQ_TO_INDEX(sequence);
3994 int huge = sequence & SEQ_HUGE_FRAME;
3995 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003996 struct iwl4965_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003997
3998 /* If a Tx command is being handled and it isn't in the actual
3999 * command queue then there a command routing bug has been introduced
4000 * in the queue management code. */
4001 if (txq_id != IWL_CMD_QUEUE_NUM)
4002 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
4003 txq_id, pkt->hdr.cmd);
4004 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
4005
4006 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
4007 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
4008
4009 /* Input error checking is done when commands are added to queue. */
4010 if (cmd->meta.flags & CMD_WANT_SKB) {
4011 cmd->meta.source->u.skb = rxb->skb;
4012 rxb->skb = NULL;
4013 } else if (cmd->meta.u.callback &&
4014 !cmd->meta.u.callback(priv, cmd, rxb->skb))
4015 rxb->skb = NULL;
4016
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004017 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07004018
4019 if (!(cmd->meta.flags & CMD_ASYNC)) {
4020 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4021 wake_up_interruptible(&priv->wait_command_queue);
4022 }
4023}
4024
4025/************************** RX-FUNCTIONS ****************************/
4026/*
4027 * Rx theory of operation
4028 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004029 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
4030 * each of which point to Receive Buffers to be filled by 4965. These get
4031 * used not only for Rx frames, but for any command response or notification
4032 * from the 4965. The driver and 4965 manage the Rx buffers by means
4033 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07004034 *
4035 * Rx Queue Indexes
4036 * The host/firmware share two index registers for managing the Rx buffers.
4037 *
4038 * The READ index maps to the first position that the firmware may be writing
4039 * to -- the driver can read up to (but not including) this position and get
4040 * good data.
4041 * The READ index is managed by the firmware once the card is enabled.
4042 *
4043 * The WRITE index maps to the last position the driver has read from -- the
4044 * position preceding WRITE is the last slot the firmware can place a packet.
4045 *
4046 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
4047 * WRITE = READ.
4048 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004049 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07004050 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4051 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004052 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07004053 * and fire the RX interrupt. The driver can then query the READ index and
4054 * process as many packets as possible, moving the WRITE index forward as it
4055 * resets the Rx queue buffers with new memory.
4056 *
4057 * The management in the driver is as follows:
4058 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
4059 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08004060 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004061 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07004062 * iwl->rxq is replenished and the READ INDEX is updated (updating the
4063 * 'processed' and 'read' driver indexes as well)
4064 * + A received packet is processed and handed to the kernel network stack,
4065 * detached from the iwl->rxq. The driver 'processed' index is updated.
4066 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
4067 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
4068 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
4069 * were enough free buffers and RX_STALLED is set it is cleared.
4070 *
4071 *
4072 * Driver sequence:
4073 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004074 * iwl4965_rx_queue_alloc() Allocates rx_free
4075 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004076 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08004077 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07004078 * queue, updates firmware pointers, and updates
4079 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004080 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07004081 *
4082 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08004083 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07004084 * READ INDEX, detaching the SKB from the pool.
4085 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004086 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07004087 * slots.
4088 * ...
4089 *
4090 */
4091
4092/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004093 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07004094 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004095static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004096{
4097 int s = q->read - q->write;
4098 if (s <= 0)
4099 s += RX_QUEUE_SIZE;
4100 /* keep some buffer to not confuse full and empty queue */
4101 s -= 2;
4102 if (s < 0)
4103 s = 0;
4104 return s;
4105}
4106
4107/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004108 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07004109 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004110int iwl4965_rx_queue_update_write_ptr(struct iwl4965_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004111{
4112 u32 reg = 0;
4113 int rc = 0;
4114 unsigned long flags;
4115
4116 spin_lock_irqsave(&q->lock, flags);
4117
4118 if (q->need_update == 0)
4119 goto exit_unlock;
4120
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004121 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07004122 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004123 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004124
4125 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004126 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004127 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4128 goto exit_unlock;
4129 }
4130
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004131 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004132 if (rc)
4133 goto exit_unlock;
4134
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004135 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004136 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07004137 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004138 iwl4965_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004139
4140 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07004141 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004142 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004143 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07004144
4145
4146 q->need_update = 0;
4147
4148 exit_unlock:
4149 spin_unlock_irqrestore(&q->lock, flags);
4150 return rc;
4151}
4152
4153/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004154 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07004155 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004156static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004157 dma_addr_t dma_addr)
4158{
4159 return cpu_to_le32((u32)(dma_addr >> 8));
4160}
4161
4162
4163/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004164 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07004165 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004166 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07004167 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08004168 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07004169 *
4170 * This moves the 'write' index forward to catch up with 'processed', and
4171 * also updates the memory address in the firmware to reference the new
4172 * target buffer.
4173 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004174static int iwl4965_rx_queue_restock(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004175{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004176 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004177 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004178 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004179 unsigned long flags;
4180 int write, rc;
4181
4182 spin_lock_irqsave(&rxq->lock, flags);
4183 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004184 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004185 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07004186 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004187 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004188 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004189
4190 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004191 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004192 rxq->queue[rxq->write] = rxb;
4193 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4194 rxq->free_count--;
4195 }
4196 spin_unlock_irqrestore(&rxq->lock, flags);
4197 /* If the pre-allocated buffer pool is dropping low, schedule to
4198 * refill it */
4199 if (rxq->free_count <= RX_LOW_WATERMARK)
4200 queue_work(priv->workqueue, &priv->rx_replenish);
4201
4202
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004203 /* If we've added more space for the firmware to place data, tell it.
4204 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07004205 if ((write != (rxq->write & ~0x7))
4206 || (abs(rxq->write - rxq->read) > 7)) {
4207 spin_lock_irqsave(&rxq->lock, flags);
4208 rxq->need_update = 1;
4209 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004210 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004211 if (rc)
4212 return rc;
4213 }
4214
4215 return 0;
4216}
4217
4218/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004219 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004220 *
4221 * When moving to rx_free an SKB is allocated for the slot.
4222 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004223 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004224 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004225 */
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004226static void iwl4965_rx_allocate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004227{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004228 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004229 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004230 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004231 unsigned long flags;
4232 spin_lock_irqsave(&rxq->lock, flags);
4233 while (!list_empty(&rxq->rx_used)) {
4234 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004235 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004236
4237 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07004238 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004239 alloc_skb(priv->hw_setting.rx_buf_size,
4240 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07004241 if (!rxb->skb) {
4242 if (net_ratelimit())
4243 printk(KERN_CRIT DRV_NAME
4244 ": Can not allocate SKB buffers\n");
4245 /* We don't reschedule replenish work here -- we will
4246 * call the restock method and if it still needs
4247 * more buffers it will schedule replenish */
4248 break;
4249 }
4250 priv->alloc_rxb_skb++;
4251 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004252
4253 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07004254 rxb->dma_addr =
4255 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004256 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004257 list_add_tail(&rxb->list, &rxq->rx_free);
4258 rxq->free_count++;
4259 }
4260 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004261}
4262
4263/*
4264 * this should be called while priv->lock is locked
4265*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02004266static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004267{
4268 struct iwl4965_priv *priv = data;
4269
4270 iwl4965_rx_allocate(priv);
4271 iwl4965_rx_queue_restock(priv);
4272}
4273
4274
4275void iwl4965_rx_replenish(void *data)
4276{
4277 struct iwl4965_priv *priv = data;
4278 unsigned long flags;
4279
4280 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004281
4282 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004283 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004284 spin_unlock_irqrestore(&priv->lock, flags);
4285}
4286
4287/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08004288 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07004289 * This free routine walks the list of POOL entries and if SKB is set to
4290 * non NULL it is unmapped and freed
4291 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004292static void iwl4965_rx_queue_free(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004293{
4294 int i;
4295 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4296 if (rxq->pool[i].skb != NULL) {
4297 pci_unmap_single(priv->pci_dev,
4298 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004299 priv->hw_setting.rx_buf_size,
4300 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004301 dev_kfree_skb(rxq->pool[i].skb);
4302 }
4303 }
4304
4305 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4306 rxq->dma_addr);
4307 rxq->bd = NULL;
4308}
4309
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004310int iwl4965_rx_queue_alloc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004311{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004312 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004313 struct pci_dev *dev = priv->pci_dev;
4314 int i;
4315
4316 spin_lock_init(&rxq->lock);
4317 INIT_LIST_HEAD(&rxq->rx_free);
4318 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004319
4320 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07004321 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4322 if (!rxq->bd)
4323 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004324
Zhu Yib481de92007-09-25 17:54:57 -07004325 /* Fill the rx_used queue with _all_ of the Rx buffers */
4326 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4327 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004328
Zhu Yib481de92007-09-25 17:54:57 -07004329 /* Set us so that we have processed and used all buffers, but have
4330 * not restocked the Rx queue with fresh buffers */
4331 rxq->read = rxq->write = 0;
4332 rxq->free_count = 0;
4333 rxq->need_update = 0;
4334 return 0;
4335}
4336
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004337void iwl4965_rx_queue_reset(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004338{
4339 unsigned long flags;
4340 int i;
4341 spin_lock_irqsave(&rxq->lock, flags);
4342 INIT_LIST_HEAD(&rxq->rx_free);
4343 INIT_LIST_HEAD(&rxq->rx_used);
4344 /* Fill the rx_used queue with _all_ of the Rx buffers */
4345 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4346 /* In the reset function, these buffers may have been allocated
4347 * to an SKB, so we need to unmap and free potential storage */
4348 if (rxq->pool[i].skb != NULL) {
4349 pci_unmap_single(priv->pci_dev,
4350 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004351 priv->hw_setting.rx_buf_size,
4352 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004353 priv->alloc_rxb_skb--;
4354 dev_kfree_skb(rxq->pool[i].skb);
4355 rxq->pool[i].skb = NULL;
4356 }
4357 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4358 }
4359
4360 /* Set us so that we have processed and used all buffers, but have
4361 * not restocked the Rx queue with fresh buffers */
4362 rxq->read = rxq->write = 0;
4363 rxq->free_count = 0;
4364 spin_unlock_irqrestore(&rxq->lock, flags);
4365}
4366
4367/* Convert linear signal-to-noise ratio into dB */
4368static u8 ratio2dB[100] = {
4369/* 0 1 2 3 4 5 6 7 8 9 */
4370 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4371 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4372 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4373 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4374 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4375 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4376 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4377 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4378 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4379 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4380};
4381
4382/* Calculates a relative dB value from a ratio of linear
4383 * (i.e. not dB) signal levels.
4384 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004385int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004386{
Adrian Bunkc899a572007-10-14 19:51:15 +02004387 /* 1000:1 or higher just report as 60 dB */
4388 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004389 return 60;
4390
Adrian Bunkc899a572007-10-14 19:51:15 +02004391 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004392 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004393 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004394 return (20 + (int)ratio2dB[sig_ratio/10]);
4395
4396 /* We shouldn't see this */
4397 if (sig_ratio < 1)
4398 return 0;
4399
4400 /* Use table for ratios 1:1 - 99:1 */
4401 return (int)ratio2dB[sig_ratio];
4402}
4403
4404#define PERFECT_RSSI (-20) /* dBm */
4405#define WORST_RSSI (-95) /* dBm */
4406#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4407
4408/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4409 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4410 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004411int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004412{
4413 int sig_qual;
4414 int degradation = PERFECT_RSSI - rssi_dbm;
4415
4416 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4417 * as indicator; formula is (signal dbm - noise dbm).
4418 * SNR at or above 40 is a great signal (100%).
4419 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4420 * Weakest usable signal is usually 10 - 15 dB SNR. */
4421 if (noise_dbm) {
4422 if (rssi_dbm - noise_dbm >= 40)
4423 return 100;
4424 else if (rssi_dbm < noise_dbm)
4425 return 0;
4426 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4427
4428 /* Else use just the signal level.
4429 * This formula is a least squares fit of data points collected and
4430 * compared with a reference system that had a percentage (%) display
4431 * for signal quality. */
4432 } else
4433 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4434 (15 * RSSI_RANGE + 62 * degradation)) /
4435 (RSSI_RANGE * RSSI_RANGE);
4436
4437 if (sig_qual > 100)
4438 sig_qual = 100;
4439 else if (sig_qual < 1)
4440 sig_qual = 0;
4441
4442 return sig_qual;
4443}
4444
4445/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004446 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004447 *
4448 * Uses the priv->rx_handlers callback function array to invoke
4449 * the appropriate handlers, including command responses,
4450 * frame-received notifications, and other notifications.
4451 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004452static void iwl4965_rx_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004453{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004454 struct iwl4965_rx_mem_buffer *rxb;
4455 struct iwl4965_rx_packet *pkt;
4456 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004457 u32 r, i;
4458 int reclaim;
4459 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004460 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004461 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004462
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004463 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4464 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004465 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004466 i = rxq->read;
4467
4468 /* Rx interrupt, but nothing sent from uCode */
4469 if (i == r)
4470 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4471
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004472 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4473 fill_rx = 1;
4474
Zhu Yib481de92007-09-25 17:54:57 -07004475 while (i != r) {
4476 rxb = rxq->queue[i];
4477
Ben Cahill9fbab512007-11-29 11:09:47 +08004478 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004479 * then a bug has been introduced in the queue refilling
4480 * routines -- catch it here */
4481 BUG_ON(rxb == NULL);
4482
4483 rxq->queue[i] = NULL;
4484
4485 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004486 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004487 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004488 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004489
4490 /* Reclaim a command buffer only if this packet is a response
4491 * to a (driver-originated) command.
4492 * If the packet (e.g. Rx frame) originated from uCode,
4493 * there is no command buffer to reclaim.
4494 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4495 * but apparently a few don't get set; catch them here. */
4496 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4497 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4498 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004499 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004500 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4501 (pkt->hdr.cmd != REPLY_TX);
4502
4503 /* Based on type of command response or notification,
4504 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004505 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004506 if (priv->rx_handlers[pkt->hdr.cmd]) {
4507 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4508 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4509 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4510 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4511 } else {
4512 /* No handling needed */
4513 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4514 "r %d i %d No handler needed for %s, 0x%02x\n",
4515 r, i, get_cmd_string(pkt->hdr.cmd),
4516 pkt->hdr.cmd);
4517 }
4518
4519 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004520 /* Invoke any callbacks, transfer the skb to caller, and
4521 * fire off the (possibly) blocking iwl4965_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004522 * as we reclaim the driver command queue */
4523 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004524 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004525 else
4526 IWL_WARNING("Claim null rxb?\n");
4527 }
4528
4529 /* For now we just don't re-use anything. We can tweak this
4530 * later to try and re-use notification packets and SKBs that
4531 * fail to Rx correctly */
4532 if (rxb->skb != NULL) {
4533 priv->alloc_rxb_skb--;
4534 dev_kfree_skb_any(rxb->skb);
4535 rxb->skb = NULL;
4536 }
4537
4538 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004539 priv->hw_setting.rx_buf_size,
4540 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004541 spin_lock_irqsave(&rxq->lock, flags);
4542 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4543 spin_unlock_irqrestore(&rxq->lock, flags);
4544 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004545 /* If there are a lot of unused frames,
4546 * restock the Rx queue so ucode wont assert. */
4547 if (fill_rx) {
4548 count++;
4549 if (count >= 8) {
4550 priv->rxq.read = i;
4551 __iwl4965_rx_replenish(priv);
4552 count = 0;
4553 }
4554 }
Zhu Yib481de92007-09-25 17:54:57 -07004555 }
4556
4557 /* Backtrack one entry */
4558 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004559 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004560}
4561
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004562/**
4563 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4564 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004565static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
4566 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004567{
4568 u32 reg = 0;
4569 int rc = 0;
4570 int txq_id = txq->q.id;
4571
4572 if (txq->need_update == 0)
4573 return rc;
4574
4575 /* if we're trying to save power */
4576 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4577 /* wake up nic if it's powered down ...
4578 * uCode will wake up, and interrupt us again, so next
4579 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004580 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004581
4582 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4583 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004584 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004585 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4586 return rc;
4587 }
4588
4589 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004590 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004591 if (rc)
4592 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004593 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004594 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004595 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004596
4597 /* else not in power-save mode, uCode will never sleep when we're
4598 * trying to tx (during RFKILL, we're not trying to tx). */
4599 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004600 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004601 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004602
4603 txq->need_update = 0;
4604
4605 return rc;
4606}
4607
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004608#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004609static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004610{
Joe Perches0795af52007-10-03 17:59:30 -07004611 DECLARE_MAC_BUF(mac);
4612
Zhu Yib481de92007-09-25 17:54:57 -07004613 IWL_DEBUG_RADIO("RX CONFIG:\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004614 iwl4965_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004615 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4616 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4617 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4618 le32_to_cpu(rxon->filter_flags));
4619 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4620 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4621 rxon->ofdm_basic_rates);
4622 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004623 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4624 print_mac(mac, rxon->node_addr));
4625 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4626 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004627 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4628}
4629#endif
4630
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004631static void iwl4965_enable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004632{
4633 IWL_DEBUG_ISR("Enabling interrupts\n");
4634 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004635 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004636}
4637
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004638static inline void iwl4965_disable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004639{
4640 clear_bit(STATUS_INT_ENABLED, &priv->status);
4641
4642 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004643 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004644
4645 /* acknowledge/clear/reset any interrupts still pending
4646 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004647 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4648 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004649 IWL_DEBUG_ISR("Disabled interrupts\n");
4650}
4651
4652static const char *desc_lookup(int i)
4653{
4654 switch (i) {
4655 case 1:
4656 return "FAIL";
4657 case 2:
4658 return "BAD_PARAM";
4659 case 3:
4660 return "BAD_CHECKSUM";
4661 case 4:
4662 return "NMI_INTERRUPT";
4663 case 5:
4664 return "SYSASSERT";
4665 case 6:
4666 return "FATAL_ERROR";
4667 }
4668
4669 return "UNKNOWN";
4670}
4671
4672#define ERROR_START_OFFSET (1 * sizeof(u32))
4673#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4674
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004675static void iwl4965_dump_nic_error_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004676{
4677 u32 data2, line;
4678 u32 desc, time, count, base, data1;
4679 u32 blink1, blink2, ilink1, ilink2;
4680 int rc;
4681
4682 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4683
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004684 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004685 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4686 return;
4687 }
4688
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004689 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004690 if (rc) {
4691 IWL_WARNING("Can not read from adapter at this time.\n");
4692 return;
4693 }
4694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004695 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004696
4697 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4698 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004699 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004700 }
4701
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004702 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4703 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4704 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4705 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4706 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4707 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4708 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4709 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4710 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004711
4712 IWL_ERROR("Desc Time "
4713 "data1 data2 line\n");
4714 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4715 desc_lookup(desc), desc, time, data1, data2, line);
4716 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4717 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4718 ilink1, ilink2);
4719
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004720 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004721}
4722
4723#define EVENT_START_OFFSET (4 * sizeof(u32))
4724
4725/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004726 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004727 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004728 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004729 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004730static void iwl4965_print_event_log(struct iwl4965_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004731 u32 num_events, u32 mode)
4732{
4733 u32 i;
4734 u32 base; /* SRAM byte address of event log header */
4735 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4736 u32 ptr; /* SRAM byte address of log data */
4737 u32 ev, time, data; /* event log data */
4738
4739 if (num_events == 0)
4740 return;
4741
4742 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4743
4744 if (mode == 0)
4745 event_size = 2 * sizeof(u32);
4746 else
4747 event_size = 3 * sizeof(u32);
4748
4749 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4750
4751 /* "time" is actually "data" for mode 0 (no timestamp).
4752 * place event id # at far right for easier visual parsing. */
4753 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004754 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004755 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004756 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004757 ptr += sizeof(u32);
4758 if (mode == 0)
4759 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4760 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004761 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004762 ptr += sizeof(u32);
4763 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4764 }
4765 }
4766}
4767
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004768static void iwl4965_dump_nic_event_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004769{
4770 int rc;
4771 u32 base; /* SRAM byte address of event log header */
4772 u32 capacity; /* event log capacity in # entries */
4773 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4774 u32 num_wraps; /* # times uCode wrapped to top of log */
4775 u32 next_entry; /* index of next entry to be written by uCode */
4776 u32 size; /* # entries that we'll print */
4777
4778 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004779 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004780 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4781 return;
4782 }
4783
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004784 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004785 if (rc) {
4786 IWL_WARNING("Can not read from adapter at this time.\n");
4787 return;
4788 }
4789
4790 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004791 capacity = iwl4965_read_targ_mem(priv, base);
4792 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4793 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4794 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004795
4796 size = num_wraps ? capacity : next_entry;
4797
4798 /* bail out if nothing in log */
4799 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004800 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004801 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004802 return;
4803 }
4804
Zhu Yi583fab32007-09-27 11:27:30 +08004805 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004806 size, num_wraps);
4807
4808 /* if uCode has wrapped back to top of log, start at the oldest entry,
4809 * i.e the next one that uCode would fill. */
4810 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004811 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004812 capacity - next_entry, mode);
4813
4814 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004815 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004816
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004817 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004818}
4819
4820/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004821 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004822 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004823static void iwl4965_irq_handle_error(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004824{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004825 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004826 set_bit(STATUS_FW_ERROR, &priv->status);
4827
4828 /* Cancel currently queued command. */
4829 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4830
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004831#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004832 if (iwl4965_debug_level & IWL_DL_FW_ERRORS) {
4833 iwl4965_dump_nic_error_log(priv);
4834 iwl4965_dump_nic_event_log(priv);
4835 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004836 }
4837#endif
4838
4839 wake_up_interruptible(&priv->wait_command_queue);
4840
4841 /* Keep the restart process from trying to send host
4842 * commands by clearing the INIT status bit */
4843 clear_bit(STATUS_READY, &priv->status);
4844
4845 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4846 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4847 "Restarting adapter due to uCode error.\n");
4848
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004849 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004850 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4851 sizeof(priv->recovery_rxon));
4852 priv->error_recovering = 1;
4853 }
4854 queue_work(priv->workqueue, &priv->restart);
4855 }
4856}
4857
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004858static void iwl4965_error_recovery(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004859{
4860 unsigned long flags;
4861
4862 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4863 sizeof(priv->staging_rxon));
4864 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004865 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004867 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004868
4869 spin_lock_irqsave(&priv->lock, flags);
4870 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4871 priv->error_recovering = 0;
4872 spin_unlock_irqrestore(&priv->lock, flags);
4873}
4874
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004875static void iwl4965_irq_tasklet(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004876{
4877 u32 inta, handled = 0;
4878 u32 inta_fh;
4879 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004880#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004881 u32 inta_mask;
4882#endif
4883
4884 spin_lock_irqsave(&priv->lock, flags);
4885
4886 /* Ack/clear/reset pending uCode interrupts.
4887 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4888 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004889 inta = iwl4965_read32(priv, CSR_INT);
4890 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004891
4892 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4893 * Any new interrupts that happen after this, either while we're
4894 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004895 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
4896 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004897
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004898#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004899 if (iwl4965_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004900 /* just for debug */
4901 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004902 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4903 inta, inta_mask, inta_fh);
4904 }
4905#endif
4906
4907 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4908 * atomic, make sure that inta covers all the interrupts that
4909 * we've discovered, even if FH interrupt came in just after
4910 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004911 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004912 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004913 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004914 inta |= CSR_INT_BIT_FH_TX;
4915
4916 /* Now service all interrupt bits discovered above. */
4917 if (inta & CSR_INT_BIT_HW_ERR) {
4918 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4919
4920 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004921 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004922
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004923 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004924
4925 handled |= CSR_INT_BIT_HW_ERR;
4926
4927 spin_unlock_irqrestore(&priv->lock, flags);
4928
4929 return;
4930 }
4931
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004932#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004933 if (iwl4965_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004934 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004935 if (inta & CSR_INT_BIT_SCD)
4936 IWL_DEBUG_ISR("Scheduler finished to transmit "
4937 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004938
4939 /* Alive notification via Rx interrupt will do the real work */
4940 if (inta & CSR_INT_BIT_ALIVE)
4941 IWL_DEBUG_ISR("Alive interrupt\n");
4942 }
4943#endif
4944 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004945 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004946
Ben Cahill9fbab512007-11-29 11:09:47 +08004947 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004948 if (inta & CSR_INT_BIT_RF_KILL) {
4949 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004950 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004951 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4952 hw_rf_kill = 1;
4953
4954 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4955 "RF_KILL bit toggled to %s.\n",
4956 hw_rf_kill ? "disable radio":"enable radio");
4957
4958 /* Queue restart only if RF_KILL switch was set to "kill"
4959 * when we loaded driver, and is now set to "enable".
4960 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004961 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004962 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4963 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004964 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004965 }
Zhu Yib481de92007-09-25 17:54:57 -07004966
4967 handled |= CSR_INT_BIT_RF_KILL;
4968 }
4969
Ben Cahill9fbab512007-11-29 11:09:47 +08004970 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004971 if (inta & CSR_INT_BIT_CT_KILL) {
4972 IWL_ERROR("Microcode CT kill error detected.\n");
4973 handled |= CSR_INT_BIT_CT_KILL;
4974 }
4975
4976 /* Error detected by uCode */
4977 if (inta & CSR_INT_BIT_SW_ERR) {
4978 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4979 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004980 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004981 handled |= CSR_INT_BIT_SW_ERR;
4982 }
4983
4984 /* uCode wakes up after power-down sleep */
4985 if (inta & CSR_INT_BIT_WAKEUP) {
4986 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004987 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4988 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4989 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4990 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4991 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4992 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4993 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004994
4995 handled |= CSR_INT_BIT_WAKEUP;
4996 }
4997
4998 /* All uCode command responses, including Tx command responses,
4999 * Rx "responses" (frame-received notification), and other
5000 * notifications from uCode come through here*/
5001 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005002 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005003 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
5004 }
5005
5006 if (inta & CSR_INT_BIT_FH_TX) {
5007 IWL_DEBUG_ISR("Tx interrupt\n");
5008 handled |= CSR_INT_BIT_FH_TX;
5009 }
5010
5011 if (inta & ~handled)
5012 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
5013
5014 if (inta & ~CSR_INI_SET_MASK) {
5015 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
5016 inta & ~CSR_INI_SET_MASK);
5017 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
5018 }
5019
5020 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005021 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005022
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005023#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005024 if (iwl4965_debug_level & (IWL_DL_ISR)) {
5025 inta = iwl4965_read32(priv, CSR_INT);
5026 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
5027 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07005028 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
5029 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
5030 }
5031#endif
5032 spin_unlock_irqrestore(&priv->lock, flags);
5033}
5034
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005035static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07005036{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005037 struct iwl4965_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07005038 u32 inta, inta_mask;
5039 u32 inta_fh;
5040 if (!priv)
5041 return IRQ_NONE;
5042
5043 spin_lock(&priv->lock);
5044
5045 /* Disable (but don't clear!) interrupts here to avoid
5046 * back-to-back ISRs and sporadic interrupts from our NIC.
5047 * If we have something to service, the tasklet will re-enable ints.
5048 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005049 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
5050 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07005051
5052 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005053 inta = iwl4965_read32(priv, CSR_INT);
5054 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07005055
5056 /* Ignore interrupt if there's nothing in NIC to service.
5057 * This may be due to IRQ shared with another device,
5058 * or due to sporadic interrupts thrown from our NIC. */
5059 if (!inta && !inta_fh) {
5060 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
5061 goto none;
5062 }
5063
5064 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08005065 /* Hardware disappeared. It might have already raised
5066 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07005067 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08005068 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07005069 }
5070
5071 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5072 inta, inta_mask, inta_fh);
5073
Joonwoo Park25c03d82008-01-23 10:15:20 -08005074 inta &= ~CSR_INT_BIT_SCD;
5075
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005076 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08005077 if (likely(inta || inta_fh))
5078 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07005079
Oliver Neukum66fbb542007-11-15 09:31:10 +08005080 unplugged:
5081 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07005082 return IRQ_HANDLED;
5083
5084 none:
5085 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005086 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005087 spin_unlock(&priv->lock);
5088 return IRQ_NONE;
5089}
5090
5091/************************** EEPROM BANDS ****************************
5092 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005093 * The iwl4965_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07005094 * EEPROM contents to the specific channel number supported for each
5095 * band.
5096 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005097 * For example, iwl4965_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07005098 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5099 * The specific geography and calibration information for that channel
5100 * is contained in the eeprom map itself.
5101 *
5102 * During init, we copy the eeprom information and channel map
5103 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5104 *
5105 * channel_map_24/52 provides the index in the channel_info array for a
5106 * given channel. We have to have two separate maps as there is channel
5107 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5108 * band_2
5109 *
5110 * A value of 0xff stored in the channel_map indicates that the channel
5111 * is not supported by the hardware at all.
5112 *
5113 * A value of 0xfe in the channel_map indicates that the channel is not
5114 * valid for Tx with the current hardware. This means that
5115 * while the system can tune and receive on a given channel, it may not
5116 * be able to associate or transmit any frames on that
5117 * channel. There is no corresponding channel information for that
5118 * entry.
5119 *
5120 *********************************************************************/
5121
5122/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005123static const u8 iwl4965_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07005124 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5125};
5126
5127/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08005128static const u8 iwl4965_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005129 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5130};
5131
Ben Cahill9fbab512007-11-29 11:09:47 +08005132static const u8 iwl4965_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005133 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5134};
5135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005136static const u8 iwl4965_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005137 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5138};
5139
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005140static const u8 iwl4965_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005141 145, 149, 153, 157, 161, 165
5142};
5143
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005144static u8 iwl4965_eeprom_band_6[] = { /* 2.4 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005145 1, 2, 3, 4, 5, 6, 7
5146};
5147
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005148static u8 iwl4965_eeprom_band_7[] = { /* 5.2 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005149 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5150};
5151
Ben Cahill9fbab512007-11-29 11:09:47 +08005152static void iwl4965_init_band_reference(const struct iwl4965_priv *priv,
5153 int band,
Zhu Yib481de92007-09-25 17:54:57 -07005154 int *eeprom_ch_count,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005155 const struct iwl4965_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07005156 **eeprom_ch_info,
5157 const u8 **eeprom_ch_index)
5158{
5159 switch (band) {
5160 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005161 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07005162 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005163 *eeprom_ch_index = iwl4965_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07005164 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005165 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005166 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07005167 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005168 *eeprom_ch_index = iwl4965_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07005169 break;
5170 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005171 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07005172 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005173 *eeprom_ch_index = iwl4965_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07005174 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005175 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005176 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07005177 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005178 *eeprom_ch_index = iwl4965_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07005179 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005180 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005181 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005182 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005183 *eeprom_ch_index = iwl4965_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07005184 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005185 case 6: /* 2.4GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005186 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_6);
Zhu Yib481de92007-09-25 17:54:57 -07005187 *eeprom_ch_info = priv->eeprom.band_24_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005188 *eeprom_ch_index = iwl4965_eeprom_band_6;
Zhu Yib481de92007-09-25 17:54:57 -07005189 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005190 case 7: /* 5 GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005191 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_7);
Zhu Yib481de92007-09-25 17:54:57 -07005192 *eeprom_ch_info = priv->eeprom.band_52_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005193 *eeprom_ch_index = iwl4965_eeprom_band_7;
Zhu Yib481de92007-09-25 17:54:57 -07005194 break;
5195 default:
5196 BUG();
5197 return;
5198 }
5199}
5200
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005201/**
5202 * iwl4965_get_channel_info - Find driver's private channel info
5203 *
5204 * Based on band and channel number.
5205 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005206const struct iwl4965_channel_info *iwl4965_get_channel_info(const struct iwl4965_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005207 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07005208{
5209 int i;
5210
Johannes Berg8318d782008-01-24 19:38:38 +01005211 switch (band) {
5212 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07005213 for (i = 14; i < priv->channel_count; i++) {
5214 if (priv->channel_info[i].channel == channel)
5215 return &priv->channel_info[i];
5216 }
5217 break;
Johannes Berg8318d782008-01-24 19:38:38 +01005218 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07005219 if (channel >= 1 && channel <= 14)
5220 return &priv->channel_info[channel - 1];
5221 break;
Johannes Berg8318d782008-01-24 19:38:38 +01005222 default:
5223 BUG();
Zhu Yib481de92007-09-25 17:54:57 -07005224 }
5225
5226 return NULL;
5227}
5228
5229#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5230 ? # x " " : "")
5231
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005232/**
5233 * iwl4965_init_channel_map - Set up driver's info for all possible channels
5234 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005235static int iwl4965_init_channel_map(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005236{
5237 int eeprom_ch_count = 0;
5238 const u8 *eeprom_ch_index = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005239 const struct iwl4965_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005240 int band, ch;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005241 struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005242
5243 if (priv->channel_count) {
5244 IWL_DEBUG_INFO("Channel map already initialized.\n");
5245 return 0;
5246 }
5247
5248 if (priv->eeprom.version < 0x2f) {
5249 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5250 priv->eeprom.version);
5251 return -EINVAL;
5252 }
5253
5254 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5255
5256 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005257 ARRAY_SIZE(iwl4965_eeprom_band_1) +
5258 ARRAY_SIZE(iwl4965_eeprom_band_2) +
5259 ARRAY_SIZE(iwl4965_eeprom_band_3) +
5260 ARRAY_SIZE(iwl4965_eeprom_band_4) +
5261 ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005262
5263 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005265 priv->channel_info = kzalloc(sizeof(struct iwl4965_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07005266 priv->channel_count, GFP_KERNEL);
5267 if (!priv->channel_info) {
5268 IWL_ERROR("Could not allocate channel_info\n");
5269 priv->channel_count = 0;
5270 return -ENOMEM;
5271 }
5272
5273 ch_info = priv->channel_info;
5274
5275 /* Loop through the 5 EEPROM bands adding them in order to the
5276 * channel map we maintain (that contains additional information than
5277 * what just in the EEPROM) */
5278 for (band = 1; band <= 5; band++) {
5279
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005280 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005281 &eeprom_ch_info, &eeprom_ch_index);
5282
5283 /* Loop through each band adding each of the channels */
5284 for (ch = 0; ch < eeprom_ch_count; ch++) {
5285 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01005286 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
5287 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005288
5289 /* permanently store EEPROM's channel regulatory flags
5290 * and max power in channel info database. */
5291 ch_info->eeprom = eeprom_ch_info[ch];
5292
5293 /* Copy the run-time flags so they are there even on
5294 * invalid channels */
5295 ch_info->flags = eeprom_ch_info[ch].flags;
5296
5297 if (!(is_channel_valid(ch_info))) {
5298 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5299 "No traffic\n",
5300 ch_info->channel,
5301 ch_info->flags,
5302 is_channel_a_band(ch_info) ?
5303 "5.2" : "2.4");
5304 ch_info++;
5305 continue;
5306 }
5307
5308 /* Initialize regulatory-based run-time data */
5309 ch_info->max_power_avg = ch_info->curr_txpow =
5310 eeprom_ch_info[ch].max_power_avg;
5311 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5312 ch_info->min_power = 0;
5313
Tomas Winkler8211ef72008-03-02 01:36:04 +02005314 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07005315 " %ddBm): Ad-Hoc %ssupported\n",
5316 ch_info->channel,
5317 is_channel_a_band(ch_info) ?
5318 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02005319 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07005320 CHECK_AND_PRINT(IBSS),
5321 CHECK_AND_PRINT(ACTIVE),
5322 CHECK_AND_PRINT(RADAR),
5323 CHECK_AND_PRINT(WIDE),
5324 CHECK_AND_PRINT(NARROW),
5325 CHECK_AND_PRINT(DFS),
5326 eeprom_ch_info[ch].flags,
5327 eeprom_ch_info[ch].max_power_avg,
5328 ((eeprom_ch_info[ch].
5329 flags & EEPROM_CHANNEL_IBSS)
5330 && !(eeprom_ch_info[ch].
5331 flags & EEPROM_CHANNEL_RADAR))
5332 ? "" : "not ");
5333
5334 /* Set the user_txpower_limit to the highest power
5335 * supported by any channel */
5336 if (eeprom_ch_info[ch].max_power_avg >
5337 priv->user_txpower_limit)
5338 priv->user_txpower_limit =
5339 eeprom_ch_info[ch].max_power_avg;
5340
5341 ch_info++;
5342 }
5343 }
5344
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005345 /* Two additional EEPROM bands for 2.4 and 5 GHz FAT channels */
Zhu Yib481de92007-09-25 17:54:57 -07005346 for (band = 6; band <= 7; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005347 enum ieee80211_band ieeeband;
Zhu Yib481de92007-09-25 17:54:57 -07005348 u8 fat_extension_chan;
5349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005350 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005351 &eeprom_ch_info, &eeprom_ch_index);
5352
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005353 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
Johannes Berg8318d782008-01-24 19:38:38 +01005354 ieeeband = (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005355
Zhu Yib481de92007-09-25 17:54:57 -07005356 /* Loop through each band adding each of the channels */
5357 for (ch = 0; ch < eeprom_ch_count; ch++) {
5358
5359 if ((band == 6) &&
5360 ((eeprom_ch_index[ch] == 5) ||
5361 (eeprom_ch_index[ch] == 6) ||
5362 (eeprom_ch_index[ch] == 7)))
5363 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5364 else
5365 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5366
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005367 /* Set up driver's info for lower half */
Johannes Berg8318d782008-01-24 19:38:38 +01005368 iwl4965_set_fat_chan_info(priv, ieeeband,
Zhu Yib481de92007-09-25 17:54:57 -07005369 eeprom_ch_index[ch],
5370 &(eeprom_ch_info[ch]),
5371 fat_extension_chan);
5372
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005373 /* Set up driver's info for upper half */
Johannes Berg8318d782008-01-24 19:38:38 +01005374 iwl4965_set_fat_chan_info(priv, ieeeband,
Zhu Yib481de92007-09-25 17:54:57 -07005375 (eeprom_ch_index[ch] + 4),
5376 &(eeprom_ch_info[ch]),
5377 HT_IE_EXT_CHANNEL_BELOW);
5378 }
5379 }
5380
5381 return 0;
5382}
5383
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005384/*
5385 * iwl4965_free_channel_map - undo allocations in iwl4965_init_channel_map
5386 */
5387static void iwl4965_free_channel_map(struct iwl4965_priv *priv)
5388{
5389 kfree(priv->channel_info);
5390 priv->channel_count = 0;
5391}
5392
Zhu Yib481de92007-09-25 17:54:57 -07005393/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5394 * sending probe req. This should be set long enough to hear probe responses
5395 * from more than one AP. */
5396#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5397#define IWL_ACTIVE_DWELL_TIME_52 (10)
5398
5399/* For faster active scanning, scan will move to the next channel if fewer than
5400 * PLCP_QUIET_THRESH packets are heard on this channel within
5401 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5402 * time if it's a quiet channel (nothing responded to our probe, and there's
5403 * no other traffic).
5404 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5405#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5406#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5407
5408/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5409 * Must be set longer than active dwell time.
5410 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5411#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5412#define IWL_PASSIVE_DWELL_TIME_52 (10)
5413#define IWL_PASSIVE_DWELL_BASE (100)
5414#define IWL_CHANNEL_TUNE_TIME 5
5415
Johannes Berg8318d782008-01-24 19:38:38 +01005416static inline u16 iwl4965_get_active_dwell_time(struct iwl4965_priv *priv,
5417 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07005418{
Johannes Berg8318d782008-01-24 19:38:38 +01005419 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005420 return IWL_ACTIVE_DWELL_TIME_52;
5421 else
5422 return IWL_ACTIVE_DWELL_TIME_24;
5423}
5424
Johannes Berg8318d782008-01-24 19:38:38 +01005425static u16 iwl4965_get_passive_dwell_time(struct iwl4965_priv *priv,
5426 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07005427{
Johannes Berg8318d782008-01-24 19:38:38 +01005428 u16 active = iwl4965_get_active_dwell_time(priv, band);
5429 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005430 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5431 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5432
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005433 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005434 /* If we're associated, we clamp the maximum passive
5435 * dwell time to be 98% of the beacon interval (minus
5436 * 2 * channel tune time) */
5437 passive = priv->beacon_int;
5438 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5439 passive = IWL_PASSIVE_DWELL_BASE;
5440 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5441 }
5442
5443 if (passive <= active)
5444 passive = active + 1;
5445
5446 return passive;
5447}
5448
Johannes Berg8318d782008-01-24 19:38:38 +01005449static int iwl4965_get_channels_for_scan(struct iwl4965_priv *priv,
5450 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07005451 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005452 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07005453{
5454 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01005455 const struct ieee80211_supported_band *sband;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005456 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005457 u16 passive_dwell = 0;
5458 u16 active_dwell = 0;
5459 int added, i;
5460
Johannes Berg8318d782008-01-24 19:38:38 +01005461 sband = iwl4965_get_hw_mode(priv, band);
5462 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07005463 return 0;
5464
Johannes Berg8318d782008-01-24 19:38:38 +01005465 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07005466
Johannes Berg8318d782008-01-24 19:38:38 +01005467 active_dwell = iwl4965_get_active_dwell_time(priv, band);
5468 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07005469
Johannes Berg8318d782008-01-24 19:38:38 +01005470 for (i = 0, added = 0; i < sband->n_channels; i++) {
5471 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07005472 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005473 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005474 IWL_DEBUG_SCAN
5475 ("Skipping current channel %d\n",
5476 le16_to_cpu(priv->active_rxon.channel));
5477 continue;
5478 }
5479 } else if (priv->only_active_channel)
5480 continue;
5481
Johannes Berg8318d782008-01-24 19:38:38 +01005482 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07005483
Johannes Berg8318d782008-01-24 19:38:38 +01005484 ch_info = iwl4965_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08005485 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005486 if (!is_channel_valid(ch_info)) {
5487 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5488 scan_ch->channel);
5489 continue;
5490 }
5491
5492 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01005493 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07005494 scan_ch->type = 0; /* passive */
5495 else
5496 scan_ch->type = 1; /* active */
5497
5498 if (scan_ch->type & 1)
5499 scan_ch->type |= (direct_mask << 1);
5500
5501 if (is_channel_narrow(ch_info))
5502 scan_ch->type |= (1 << 7);
5503
5504 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5505 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5506
Ben Cahill9fbab512007-11-29 11:09:47 +08005507 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07005508 scan_ch->tpc.dsp_atten = 110;
5509 /* scan_pwr_info->tpc.dsp_atten; */
5510
5511 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01005512 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005513 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5514 else {
5515 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5516 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08005517 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08005518 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07005519 */
5520 }
5521
5522 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5523 scan_ch->channel,
5524 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5525 (scan_ch->type & 1) ?
5526 active_dwell : passive_dwell);
5527
5528 scan_ch++;
5529 added++;
5530 }
5531
5532 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5533 return added;
5534}
5535
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005536static void iwl4965_init_hw_rates(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005537 struct ieee80211_rate *rates)
5538{
5539 int i;
5540
5541 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005542 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
5543 rates[i].hw_value = i; /* Rate scaling will work on indexes */
5544 rates[i].hw_value_short = i;
5545 rates[i].flags = 0;
5546 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07005547 /*
Johannes Berg8318d782008-01-24 19:38:38 +01005548 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07005549 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005550 rates[i].flags |= (iwl4965_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01005551 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07005552 }
Zhu Yib481de92007-09-25 17:54:57 -07005553 }
Zhu Yib481de92007-09-25 17:54:57 -07005554}
5555
5556/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005557 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005558 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005559static int iwl4965_init_geos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005560{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005561 struct iwl4965_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005562 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07005563 struct ieee80211_channel *channels;
5564 struct ieee80211_channel *geo_ch;
5565 struct ieee80211_rate *rates;
5566 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005567
Johannes Berg8318d782008-01-24 19:38:38 +01005568 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
5569 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07005570 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5571 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5572 return 0;
5573 }
5574
Zhu Yib481de92007-09-25 17:54:57 -07005575 channels = kzalloc(sizeof(struct ieee80211_channel) *
5576 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01005577 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07005578 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005579
Tomas Winkler8211ef72008-03-02 01:36:04 +02005580 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07005581 GFP_KERNEL);
5582 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07005583 kfree(channels);
5584 return -ENOMEM;
5585 }
5586
Zhu Yib481de92007-09-25 17:54:57 -07005587 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02005588 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5589 sband->channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
5590 /* just OFDM */
5591 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
5592 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01005593
Tomas Winkler8211ef72008-03-02 01:36:04 +02005594 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005595
Tomas Winkler8211ef72008-03-02 01:36:04 +02005596 sband = &priv->bands[IEEE80211_BAND_2GHZ];
5597 sband->channels = channels;
5598 /* OFDM & CCK */
5599 sband->bitrates = rates;
5600 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07005601
Tomas Winkler8211ef72008-03-02 01:36:04 +02005602 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005603
Zhu Yib481de92007-09-25 17:54:57 -07005604 priv->ieee_channels = channels;
5605 priv->ieee_rates = rates;
5606
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005607 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005608
Tomas Winkler8211ef72008-03-02 01:36:04 +02005609 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07005610 ch = &priv->channel_info[i];
5611
Tomas Winkler8211ef72008-03-02 01:36:04 +02005612 /* FIXME: might be removed if scan is OK */
5613 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07005614 continue;
Zhu Yib481de92007-09-25 17:54:57 -07005615
Tomas Winkler8211ef72008-03-02 01:36:04 +02005616 if (is_channel_a_band(ch))
5617 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5618 else
5619 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005620
Tomas Winkler8211ef72008-03-02 01:36:04 +02005621 geo_ch = &sband->channels[sband->n_channels++];
5622
5623 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01005624 geo_ch->max_power = ch->max_power_avg;
5625 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08005626 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07005627
5628 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01005629 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
5630 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07005631
Johannes Berg8318d782008-01-24 19:38:38 +01005632 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
5633 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07005634
5635 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01005636 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07005637
5638 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5639 priv->max_channel_txpower_limit =
5640 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005641 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01005642 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005643 }
5644
5645 /* Save flags for reg domain usage */
5646 geo_ch->orig_flags = geo_ch->flags;
5647
5648 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
5649 ch->channel, geo_ch->center_freq,
5650 is_channel_a_band(ch) ? "5.2" : "2.4",
5651 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
5652 "restricted" : "valid",
5653 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07005654 }
5655
Tomas Winkler82b9a122008-03-04 18:09:30 -08005656 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
5657 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07005658 printk(KERN_INFO DRV_NAME
5659 ": Incorrectly detected BG card as ABG. Please send "
5660 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5661 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08005662 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07005663 }
5664
5665 printk(KERN_INFO DRV_NAME
5666 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005667 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5668 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005669
Johannes Berg8318d782008-01-24 19:38:38 +01005670 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->bands[IEEE80211_BAND_2GHZ];
5671 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005672
Zhu Yib481de92007-09-25 17:54:57 -07005673 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5674
5675 return 0;
5676}
5677
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005678/*
5679 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5680 */
5681static void iwl4965_free_geos(struct iwl4965_priv *priv)
5682{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005683 kfree(priv->ieee_channels);
5684 kfree(priv->ieee_rates);
5685 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5686}
5687
Zhu Yib481de92007-09-25 17:54:57 -07005688/******************************************************************************
5689 *
5690 * uCode download functions
5691 *
5692 ******************************************************************************/
5693
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005694static void iwl4965_dealloc_ucode_pci(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005695{
Tomas Winkler98c92212008-01-14 17:46:20 -08005696 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5697 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5698 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5699 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5700 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5701 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005702}
5703
5704/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005705 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005706 * looking at all data.
5707 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02005708static int iwl4965_verify_inst_full(struct iwl4965_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005709 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005710{
5711 u32 val;
5712 u32 save_len = len;
5713 int rc = 0;
5714 u32 errcnt;
5715
5716 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5717
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005718 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005719 if (rc)
5720 return rc;
5721
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005722 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005723
5724 errcnt = 0;
5725 for (; len > 0; len -= sizeof(u32), image++) {
5726 /* read data comes through single port, auto-incr addr */
5727 /* NOTE: Use the debugless read so we don't flood kernel log
5728 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005729 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005730 if (val != le32_to_cpu(*image)) {
5731 IWL_ERROR("uCode INST section is invalid at "
5732 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5733 save_len - len, val, le32_to_cpu(*image));
5734 rc = -EIO;
5735 errcnt++;
5736 if (errcnt >= 20)
5737 break;
5738 }
5739 }
5740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005741 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005742
5743 if (!errcnt)
5744 IWL_DEBUG_INFO
5745 ("ucode image in INSTRUCTION memory is good\n");
5746
5747 return rc;
5748}
5749
5750
5751/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005752 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005753 * using sample data 100 bytes apart. If these sample points are good,
5754 * it's a pretty good bet that everything between them is good, too.
5755 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005756static int iwl4965_verify_inst_sparse(struct iwl4965_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005757{
5758 u32 val;
5759 int rc = 0;
5760 u32 errcnt = 0;
5761 u32 i;
5762
5763 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5764
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005765 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005766 if (rc)
5767 return rc;
5768
5769 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5770 /* read data comes through single port, auto-incr addr */
5771 /* NOTE: Use the debugless read so we don't flood kernel log
5772 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005773 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005774 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005775 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005776 if (val != le32_to_cpu(*image)) {
5777#if 0 /* Enable this if you want to see details */
5778 IWL_ERROR("uCode INST section is invalid at "
5779 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5780 i, val, *image);
5781#endif
5782 rc = -EIO;
5783 errcnt++;
5784 if (errcnt >= 3)
5785 break;
5786 }
5787 }
5788
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005789 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005790
5791 return rc;
5792}
5793
5794
5795/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005796 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005797 * and verify its contents
5798 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005799static int iwl4965_verify_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005800{
5801 __le32 *image;
5802 u32 len;
5803 int rc = 0;
5804
5805 /* Try bootstrap */
5806 image = (__le32 *)priv->ucode_boot.v_addr;
5807 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005808 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005809 if (rc == 0) {
5810 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5811 return 0;
5812 }
5813
5814 /* Try initialize */
5815 image = (__le32 *)priv->ucode_init.v_addr;
5816 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005817 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005818 if (rc == 0) {
5819 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5820 return 0;
5821 }
5822
5823 /* Try runtime/protocol */
5824 image = (__le32 *)priv->ucode_code.v_addr;
5825 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005826 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005827 if (rc == 0) {
5828 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5829 return 0;
5830 }
5831
5832 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5833
Ben Cahill9fbab512007-11-29 11:09:47 +08005834 /* Since nothing seems to match, show first several data entries in
5835 * instruction SRAM, so maybe visual inspection will give a clue.
5836 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005837 image = (__le32 *)priv->ucode_boot.v_addr;
5838 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005839 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005840
5841 return rc;
5842}
5843
5844
5845/* check contents of special bootstrap uCode SRAM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005846static int iwl4965_verify_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005847{
5848 __le32 *image = priv->ucode_boot.v_addr;
5849 u32 len = priv->ucode_boot.len;
5850 u32 reg;
5851 u32 val;
5852
5853 IWL_DEBUG_INFO("Begin verify bsm\n");
5854
5855 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005856 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005857 for (reg = BSM_SRAM_LOWER_BOUND;
5858 reg < BSM_SRAM_LOWER_BOUND + len;
5859 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005860 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005861 if (val != le32_to_cpu(*image)) {
5862 IWL_ERROR("BSM uCode verification failed at "
5863 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5864 BSM_SRAM_LOWER_BOUND,
5865 reg - BSM_SRAM_LOWER_BOUND, len,
5866 val, le32_to_cpu(*image));
5867 return -EIO;
5868 }
5869 }
5870
5871 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5872
5873 return 0;
5874}
5875
5876/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005877 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005878 *
5879 * BSM operation:
5880 *
5881 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5882 * in special SRAM that does not power down during RFKILL. When powering back
5883 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5884 * the bootstrap program into the on-board processor, and starts it.
5885 *
5886 * The bootstrap program loads (via DMA) instructions and data for a new
5887 * program from host DRAM locations indicated by the host driver in the
5888 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5889 * automatically.
5890 *
5891 * When initializing the NIC, the host driver points the BSM to the
5892 * "initialize" uCode image. This uCode sets up some internal data, then
5893 * notifies host via "initialize alive" that it is complete.
5894 *
5895 * The host then replaces the BSM_DRAM_* pointer values to point to the
5896 * normal runtime uCode instructions and a backup uCode data cache buffer
5897 * (filled initially with starting data values for the on-board processor),
5898 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5899 * which begins normal operation.
5900 *
5901 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5902 * the backup data cache in DRAM before SRAM is powered down.
5903 *
5904 * When powering back up, the BSM loads the bootstrap program. This reloads
5905 * the runtime uCode instructions and the backup data cache into SRAM,
5906 * and re-launches the runtime uCode from where it left off.
5907 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005908static int iwl4965_load_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005909{
5910 __le32 *image = priv->ucode_boot.v_addr;
5911 u32 len = priv->ucode_boot.len;
5912 dma_addr_t pinst;
5913 dma_addr_t pdata;
5914 u32 inst_len;
5915 u32 data_len;
5916 int rc;
5917 int i;
5918 u32 done;
5919 u32 reg_offset;
5920
5921 IWL_DEBUG_INFO("Begin load bsm\n");
5922
5923 /* make sure bootstrap program is no larger than BSM's SRAM size */
5924 if (len > IWL_MAX_BSM_SIZE)
5925 return -EINVAL;
5926
5927 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005928 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005929 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005930 * after the "initialize" uCode has run, to point to
5931 * runtime/protocol instructions and backup data cache. */
5932 pinst = priv->ucode_init.p_addr >> 4;
5933 pdata = priv->ucode_init_data.p_addr >> 4;
5934 inst_len = priv->ucode_init.len;
5935 data_len = priv->ucode_init_data.len;
5936
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005937 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005938 if (rc)
5939 return rc;
5940
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005941 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5942 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5943 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5944 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005945
5946 /* Fill BSM memory with bootstrap instructions */
5947 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5948 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5949 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005950 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005951 le32_to_cpu(*image));
5952
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005953 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005954 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005955 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005956 return rc;
5957 }
5958
5959 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005960 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5961 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005962 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005963 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005964
5965 /* Load bootstrap code into instruction SRAM now,
5966 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005967 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005968 BSM_WR_CTRL_REG_BIT_START);
5969
5970 /* Wait for load of bootstrap uCode to finish */
5971 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005972 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005973 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5974 break;
5975 udelay(10);
5976 }
5977 if (i < 100)
5978 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5979 else {
5980 IWL_ERROR("BSM write did not complete!\n");
5981 return -EIO;
5982 }
5983
5984 /* Enable future boot loads whenever power management unit triggers it
5985 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005986 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005987 BSM_WR_CTRL_REG_BIT_START_EN);
5988
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005989 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005990
5991 return 0;
5992}
5993
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005994static void iwl4965_nic_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005995{
5996 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005997 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005998}
5999
Tomas Winkler90e759d2007-11-29 11:09:41 +08006000
Zhu Yib481de92007-09-25 17:54:57 -07006001/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006002 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07006003 *
6004 * Copy into buffers for card to fetch via bus-mastering
6005 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006006static int iwl4965_read_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006007{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006008 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006009 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006010 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08006011 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07006012 u8 *src;
6013 size_t len;
6014 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
6015
6016 /* Ask kernel firmware_class module to get the boot firmware off disk.
6017 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006018 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
6019 if (ret < 0) {
6020 IWL_ERROR("%s firmware file req failed: Reason %d\n",
6021 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07006022 goto error;
6023 }
6024
6025 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
6026 name, ucode_raw->size);
6027
6028 /* Make sure that we got at least our header! */
6029 if (ucode_raw->size < sizeof(*ucode)) {
6030 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006031 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006032 goto err_release;
6033 }
6034
6035 /* Data from ucode file: header followed by uCode images */
6036 ucode = (void *)ucode_raw->data;
6037
6038 ver = le32_to_cpu(ucode->ver);
6039 inst_size = le32_to_cpu(ucode->inst_size);
6040 data_size = le32_to_cpu(ucode->data_size);
6041 init_size = le32_to_cpu(ucode->init_size);
6042 init_data_size = le32_to_cpu(ucode->init_data_size);
6043 boot_size = le32_to_cpu(ucode->boot_size);
6044
6045 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
6046 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
6047 inst_size);
6048 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
6049 data_size);
6050 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
6051 init_size);
6052 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
6053 init_data_size);
6054 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
6055 boot_size);
6056
6057 /* Verify size of file vs. image size info in file's header */
6058 if (ucode_raw->size < sizeof(*ucode) +
6059 inst_size + data_size + init_size +
6060 init_data_size + boot_size) {
6061
6062 IWL_DEBUG_INFO("uCode file size %d too small\n",
6063 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08006064 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006065 goto err_release;
6066 }
6067
6068 /* Verify that uCode images will fit in card's SRAM */
6069 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08006070 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
6071 inst_size);
6072 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006073 goto err_release;
6074 }
6075
6076 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08006077 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
6078 data_size);
6079 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006080 goto err_release;
6081 }
6082 if (init_size > IWL_MAX_INST_SIZE) {
6083 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006084 ("uCode init instr len %d too large to fit in\n",
6085 init_size);
6086 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006087 goto err_release;
6088 }
6089 if (init_data_size > IWL_MAX_DATA_SIZE) {
6090 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006091 ("uCode init data len %d too large to fit in\n",
6092 init_data_size);
6093 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006094 goto err_release;
6095 }
6096 if (boot_size > IWL_MAX_BSM_SIZE) {
6097 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006098 ("uCode boot instr len %d too large to fit in\n",
6099 boot_size);
6100 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006101 goto err_release;
6102 }
6103
6104 /* Allocate ucode buffers for card's bus-master loading ... */
6105
6106 /* Runtime instructions and 2 copies of data:
6107 * 1) unmodified from disk
6108 * 2) backup cache for save/restore during power-downs */
6109 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006110 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07006111
6112 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006113 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07006114
6115 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006116 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07006117
6118 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006119 if (init_size && init_data_size) {
6120 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006121 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07006122
Tomas Winkler90e759d2007-11-29 11:09:41 +08006123 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006124 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08006125
6126 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
6127 goto err_pci_alloc;
6128 }
Zhu Yib481de92007-09-25 17:54:57 -07006129
6130 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006131 if (boot_size) {
6132 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006133 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07006134
Tomas Winkler90e759d2007-11-29 11:09:41 +08006135 if (!priv->ucode_boot.v_addr)
6136 goto err_pci_alloc;
6137 }
Zhu Yib481de92007-09-25 17:54:57 -07006138
6139 /* Copy images into buffers for card's bus-master reads ... */
6140
6141 /* Runtime instructions (first block of data in file) */
6142 src = &ucode->data[0];
6143 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006144 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006145 memcpy(priv->ucode_code.v_addr, src, len);
6146 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6147 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6148
6149 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006150 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07006151 src = &ucode->data[inst_size];
6152 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006153 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006154 memcpy(priv->ucode_data.v_addr, src, len);
6155 memcpy(priv->ucode_data_backup.v_addr, src, len);
6156
6157 /* Initialization instructions (3rd block) */
6158 if (init_size) {
6159 src = &ucode->data[inst_size + data_size];
6160 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006161 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
6162 len);
Zhu Yib481de92007-09-25 17:54:57 -07006163 memcpy(priv->ucode_init.v_addr, src, len);
6164 }
6165
6166 /* Initialization data (4th block) */
6167 if (init_data_size) {
6168 src = &ucode->data[inst_size + data_size + init_size];
6169 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006170 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
6171 len);
Zhu Yib481de92007-09-25 17:54:57 -07006172 memcpy(priv->ucode_init_data.v_addr, src, len);
6173 }
6174
6175 /* Bootstrap instructions (5th block) */
6176 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6177 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006178 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006179 memcpy(priv->ucode_boot.v_addr, src, len);
6180
6181 /* We have our copies now, allow OS release its copies */
6182 release_firmware(ucode_raw);
6183 return 0;
6184
6185 err_pci_alloc:
6186 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006187 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006188 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006189
6190 err_release:
6191 release_firmware(ucode_raw);
6192
6193 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08006194 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006195}
6196
6197
6198/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006199 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07006200 *
6201 * Tell initialization uCode where to find runtime uCode.
6202 *
6203 * BSM registers initially contain pointers to initialization uCode.
6204 * We need to replace them to load runtime uCode inst and data,
6205 * and to save runtime data when powering down.
6206 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006207static int iwl4965_set_ucode_ptrs(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006208{
6209 dma_addr_t pinst;
6210 dma_addr_t pdata;
6211 int rc = 0;
6212 unsigned long flags;
6213
6214 /* bits 35:4 for 4965 */
6215 pinst = priv->ucode_code.p_addr >> 4;
6216 pdata = priv->ucode_data_backup.p_addr >> 4;
6217
6218 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006219 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006220 if (rc) {
6221 spin_unlock_irqrestore(&priv->lock, flags);
6222 return rc;
6223 }
6224
6225 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006226 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6227 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6228 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006229 priv->ucode_data.len);
6230
6231 /* Inst bytecount must be last to set up, bit 31 signals uCode
6232 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006233 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006234 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6235
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006236 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006237
6238 spin_unlock_irqrestore(&priv->lock, flags);
6239
6240 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6241
6242 return rc;
6243}
6244
6245/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006246 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006247 *
6248 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6249 *
6250 * The 4965 "initialize" ALIVE reply contains calibration data for:
6251 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6252 * (3945 does not contain this data).
6253 *
6254 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6255*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006256static void iwl4965_init_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006257{
6258 /* Check alive response for "valid" sign from uCode */
6259 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6260 /* We had an error bringing up the hardware, so take it
6261 * all the way back down so we can try again */
6262 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6263 goto restart;
6264 }
6265
6266 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6267 * This is a paranoid check, because we would not have gotten the
6268 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006269 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006270 /* Runtime instruction load was bad;
6271 * take it all the way back down so we can try again */
6272 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6273 goto restart;
6274 }
6275
6276 /* Calculate temperature */
6277 priv->temperature = iwl4965_get_temperature(priv);
6278
6279 /* Send pointers to protocol/runtime uCode image ... init code will
6280 * load and launch runtime uCode, which will send us another "Alive"
6281 * notification. */
6282 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006283 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006284 /* Runtime instruction load won't happen;
6285 * take it all the way back down so we can try again */
6286 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6287 goto restart;
6288 }
6289 return;
6290
6291 restart:
6292 queue_work(priv->workqueue, &priv->restart);
6293}
6294
6295
6296/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006297 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006298 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006299 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07006300 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006301static void iwl4965_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006302{
6303 int rc = 0;
6304
6305 IWL_DEBUG_INFO("Runtime Alive received.\n");
6306
6307 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6308 /* We had an error bringing up the hardware, so take it
6309 * all the way back down so we can try again */
6310 IWL_DEBUG_INFO("Alive failed.\n");
6311 goto restart;
6312 }
6313
6314 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6315 * This is a paranoid check, because we would not have gotten the
6316 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006317 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006318 /* Runtime instruction load was bad;
6319 * take it all the way back down so we can try again */
6320 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6321 goto restart;
6322 }
6323
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006324 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006325
6326 rc = iwl4965_alive_notify(priv);
6327 if (rc) {
6328 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6329 rc);
6330 goto restart;
6331 }
6332
Ben Cahill9fbab512007-11-29 11:09:47 +08006333 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07006334 set_bit(STATUS_ALIVE, &priv->status);
6335
6336 /* Clear out the uCode error bit if it is set */
6337 clear_bit(STATUS_FW_ERROR, &priv->status);
6338
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006339 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006340 return;
6341
Zhu Yi5a66926a2008-01-14 17:46:18 -08006342 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006343
6344 priv->active_rate = priv->rates_mask;
6345 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6346
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006347 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07006348
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006349 if (iwl4965_is_associated(priv)) {
6350 struct iwl4965_rxon_cmd *active_rxon =
6351 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07006352
6353 memcpy(&priv->staging_rxon, &priv->active_rxon,
6354 sizeof(priv->staging_rxon));
6355 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6356 } else {
6357 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006358 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006359 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6360 }
6361
Ben Cahill9fbab512007-11-29 11:09:47 +08006362 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006363 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006364
6365 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006366 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006367
6368 /* At this point, the NIC is initialized and operational */
6369 priv->notif_missed_beacons = 0;
6370 set_bit(STATUS_READY, &priv->status);
6371
6372 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006373
Zhu Yib481de92007-09-25 17:54:57 -07006374 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006375 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07006376
6377 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006378 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006379
6380 return;
6381
6382 restart:
6383 queue_work(priv->workqueue, &priv->restart);
6384}
6385
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006386static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07006387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006388static void __iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006389{
6390 unsigned long flags;
6391 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6392 struct ieee80211_conf *conf = NULL;
6393
6394 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6395
6396 conf = ieee80211_get_hw_conf(priv->hw);
6397
6398 if (!exit_pending)
6399 set_bit(STATUS_EXIT_PENDING, &priv->status);
6400
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006401 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006402
6403 /* Unblock any waiting calls */
6404 wake_up_interruptible_all(&priv->wait_command_queue);
6405
Zhu Yib481de92007-09-25 17:54:57 -07006406 /* Wipe out the EXIT_PENDING status bit if we are not actually
6407 * exiting the module */
6408 if (!exit_pending)
6409 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6410
6411 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006412 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07006413
6414 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006415 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006416
6417 if (priv->mac80211_registered)
6418 ieee80211_stop_queues(priv->hw);
6419
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006420 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07006421 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006422 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006423 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6424 STATUS_RF_KILL_HW |
6425 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6426 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08006427 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6428 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07006429 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6430 STATUS_IN_SUSPEND;
6431 goto exit;
6432 }
6433
6434 /* ...otherwise clear out all the status bits but the RF Kill and
6435 * SUSPEND bits and continue taking the NIC down. */
6436 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6437 STATUS_RF_KILL_HW |
6438 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6439 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08006440 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6441 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07006442 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6443 STATUS_IN_SUSPEND |
6444 test_bit(STATUS_FW_ERROR, &priv->status) <<
6445 STATUS_FW_ERROR;
6446
6447 spin_lock_irqsave(&priv->lock, flags);
Ben Cahill9fbab512007-11-29 11:09:47 +08006448 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
6449 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07006450 spin_unlock_irqrestore(&priv->lock, flags);
6451
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006452 iwl4965_hw_txq_ctx_stop(priv);
6453 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006454
6455 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006456 if (!iwl4965_grab_nic_access(priv)) {
6457 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006458 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006459 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006460 }
6461 spin_unlock_irqrestore(&priv->lock, flags);
6462
6463 udelay(5);
6464
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006465 iwl4965_hw_nic_stop_master(priv);
6466 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6467 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006468
6469 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006470 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07006471
6472 if (priv->ibss_beacon)
6473 dev_kfree_skb(priv->ibss_beacon);
6474 priv->ibss_beacon = NULL;
6475
6476 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006477 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006478}
6479
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006480static void iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006481{
6482 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006483 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006484 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006485
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006486 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006487}
6488
6489#define MAX_HW_RESTARTS 5
6490
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006491static int __iwl4965_up(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006492{
6493 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07006494
6495 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6496 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6497 return -EIO;
6498 }
6499
6500 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6501 IWL_WARNING("Radio disabled by SW RF kill (module "
6502 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08006503 return -ENODEV;
6504 }
6505
Reinette Chatree903fbd2008-01-30 22:05:15 -08006506 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6507 IWL_ERROR("ucode not available for device bringup\n");
6508 return -EIO;
6509 }
6510
Zhu Yie655b9f2008-01-24 02:19:38 -08006511 /* If platform's RF_KILL switch is NOT set to KILL */
6512 if (iwl4965_read32(priv, CSR_GP_CNTRL) &
6513 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
6514 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6515 else {
6516 set_bit(STATUS_RF_KILL_HW, &priv->status);
6517 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
6518 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6519 return -ENODEV;
6520 }
Zhu Yib481de92007-09-25 17:54:57 -07006521 }
6522
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006523 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006525 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006526 if (rc) {
6527 IWL_ERROR("Unable to int nic\n");
6528 return rc;
6529 }
6530
6531 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006532 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6533 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006534 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6535
6536 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006537 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6538 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006539
6540 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006541 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6542 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006543
6544 /* Copy original ucode data image from disk into backup cache.
6545 * This will be used to initialize the on-board processor's
6546 * data SRAM for a clean start when the runtime program first loads. */
6547 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08006548 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07006549
Zhu Yie655b9f2008-01-24 02:19:38 -08006550 /* We return success when we resume from suspend and rf_kill is on. */
6551 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006552 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006553
6554 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006556 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006557
6558 /* load bootstrap state machine,
6559 * load bootstrap program into processor's memory,
6560 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006561 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006562
6563 if (rc) {
6564 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6565 continue;
6566 }
6567
6568 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006569 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006570
Zhu Yib481de92007-09-25 17:54:57 -07006571 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6572
6573 return 0;
6574 }
6575
6576 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006577 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006578
6579 /* tried to restart and config the device for as long as our
6580 * patience could withstand */
6581 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6582 return -EIO;
6583}
6584
6585
6586/*****************************************************************************
6587 *
6588 * Workqueue callbacks
6589 *
6590 *****************************************************************************/
6591
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006592static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006593{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006594 struct iwl4965_priv *priv =
6595 container_of(data, struct iwl4965_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006596
6597 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6598 return;
6599
6600 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006601 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006602 mutex_unlock(&priv->mutex);
6603}
6604
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006605static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006606{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006607 struct iwl4965_priv *priv =
6608 container_of(data, struct iwl4965_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006609
6610 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6611 return;
6612
6613 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006614 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006615 mutex_unlock(&priv->mutex);
6616}
6617
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006618static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006619{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006620 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006621
6622 wake_up_interruptible(&priv->wait_command_queue);
6623
6624 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6625 return;
6626
6627 mutex_lock(&priv->mutex);
6628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006629 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006630 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6631 "HW and/or SW RF Kill no longer active, restarting "
6632 "device\n");
6633 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6634 queue_work(priv->workqueue, &priv->restart);
6635 } else {
6636
6637 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6638 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6639 "disabled by SW switch\n");
6640 else
6641 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6642 "Kill switch must be turned off for "
6643 "wireless networking to work.\n");
6644 }
6645 mutex_unlock(&priv->mutex);
6646}
6647
6648#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6649
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006650static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006651{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006652 struct iwl4965_priv *priv =
6653 container_of(data, struct iwl4965_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006654
6655 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6656 return;
6657
6658 mutex_lock(&priv->mutex);
6659 if (test_bit(STATUS_SCANNING, &priv->status) ||
6660 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6661 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6662 "Scan completion watchdog resetting adapter (%dms)\n",
6663 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006664
Zhu Yib481de92007-09-25 17:54:57 -07006665 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006666 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006667 }
6668 mutex_unlock(&priv->mutex);
6669}
6670
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006671static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006672{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006673 struct iwl4965_priv *priv =
6674 container_of(data, struct iwl4965_priv, request_scan);
6675 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006676 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006677 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006678 .meta.flags = CMD_SIZE_HUGE,
6679 };
6680 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006681 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006682 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006683 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006684 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006685 u8 direct_mask;
Zhu Yib481de92007-09-25 17:54:57 -07006686
6687 conf = ieee80211_get_hw_conf(priv->hw);
6688
6689 mutex_lock(&priv->mutex);
6690
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006691 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006692 IWL_WARNING("request scan called when driver not ready.\n");
6693 goto done;
6694 }
6695
6696 /* Make sure the scan wasn't cancelled before this queued work
6697 * was given the chance to run... */
6698 if (!test_bit(STATUS_SCANNING, &priv->status))
6699 goto done;
6700
6701 /* This should never be called or scheduled if there is currently
6702 * a scan active in the hardware. */
6703 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6704 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6705 "Ignoring second request.\n");
6706 rc = -EIO;
6707 goto done;
6708 }
6709
6710 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6711 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6712 goto done;
6713 }
6714
6715 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6716 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6717 goto done;
6718 }
6719
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006720 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006721 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6722 goto done;
6723 }
6724
6725 if (!test_bit(STATUS_READY, &priv->status)) {
6726 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6727 goto done;
6728 }
6729
6730 if (!priv->scan_bands) {
6731 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6732 goto done;
6733 }
6734
6735 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006736 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006737 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6738 if (!priv->scan) {
6739 rc = -ENOMEM;
6740 goto done;
6741 }
6742 }
6743 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006744 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006745
6746 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6747 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6748
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006749 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006750 u16 interval = 0;
6751 u32 extra;
6752 u32 suspend_time = 100;
6753 u32 scan_suspend_time = 100;
6754 unsigned long flags;
6755
6756 IWL_DEBUG_INFO("Scanning while associated...\n");
6757
6758 spin_lock_irqsave(&priv->lock, flags);
6759 interval = priv->beacon_int;
6760 spin_unlock_irqrestore(&priv->lock, flags);
6761
6762 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006763 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006764 if (!interval)
6765 interval = suspend_time;
6766
6767 extra = (suspend_time / interval) << 22;
6768 scan_suspend_time = (extra |
6769 ((suspend_time % interval) * 1024));
6770 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6771 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6772 scan_suspend_time, interval);
6773 }
6774
6775 /* We should add the ability for user to lock to PASSIVE ONLY */
6776 if (priv->one_direct_scan) {
6777 IWL_DEBUG_SCAN
6778 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006779 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006780 priv->direct_ssid_len));
6781 scan->direct_scan[0].id = WLAN_EID_SSID;
6782 scan->direct_scan[0].len = priv->direct_ssid_len;
6783 memcpy(scan->direct_scan[0].ssid,
6784 priv->direct_ssid, priv->direct_ssid_len);
6785 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006786 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006787 scan->direct_scan[0].id = WLAN_EID_SSID;
6788 scan->direct_scan[0].len = priv->essid_len;
6789 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6790 direct_mask = 1;
6791 } else
6792 direct_mask = 0;
6793
Zhu Yib481de92007-09-25 17:54:57 -07006794 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6795 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6796 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6797
Zhu Yib481de92007-09-25 17:54:57 -07006798
6799 switch (priv->scan_bands) {
6800 case 2:
6801 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6802 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006803 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006804 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6805
6806 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006807 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006808 break;
6809
6810 case 1:
6811 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006812 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006813 RATE_MCS_ANT_B_MSK);
6814 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006815 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006816 break;
6817
6818 default:
6819 IWL_WARNING("Invalid scan band count\n");
6820 goto done;
6821 }
6822
Tomas Winkler78330fd2008-02-06 02:37:18 +02006823 /* We don't build a direct scan probe request; the uCode will do
6824 * that based on the direct_mask added to each channel entry */
6825 cmd_len = iwl4965_fill_probe_req(priv, band,
6826 (struct ieee80211_mgmt *)scan->data,
6827 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6828
6829 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006830 /* select Rx chains */
6831
6832 /* Force use of chains B and C (0x6) for scan Rx.
6833 * Avoid A (0x1) because of its off-channel reception on A-band.
6834 * MIMO is not used here, but value is required to make uCode happy. */
6835 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6836 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6837 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6838 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6839
6840 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6841 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6842
6843 if (direct_mask)
6844 IWL_DEBUG_SCAN
6845 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006846 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07006847 else
6848 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
6849
6850 scan->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006851 iwl4965_get_channels_for_scan(
Johannes Berg8318d782008-01-24 19:38:38 +01006852 priv, band, 1, /* active */
Zhu Yib481de92007-09-25 17:54:57 -07006853 direct_mask,
6854 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6855
6856 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006857 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006858 cmd.data = scan;
6859 scan->len = cpu_to_le16(cmd.len);
6860
6861 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006862 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006863 if (rc)
6864 goto done;
6865
6866 queue_delayed_work(priv->workqueue, &priv->scan_check,
6867 IWL_SCAN_CHECK_WATCHDOG);
6868
6869 mutex_unlock(&priv->mutex);
6870 return;
6871
6872 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006873 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006874 queue_work(priv->workqueue, &priv->scan_completed);
6875 mutex_unlock(&priv->mutex);
6876}
6877
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006878static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006879{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006880 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006881
6882 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6883 return;
6884
6885 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006886 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006887 mutex_unlock(&priv->mutex);
6888}
6889
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006890static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006891{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006892 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006893
6894 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6895 return;
6896
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006897 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006898 queue_work(priv->workqueue, &priv->up);
6899}
6900
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006901static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006902{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006903 struct iwl4965_priv *priv =
6904 container_of(data, struct iwl4965_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006905
6906 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6907 return;
6908
6909 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006910 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006911 mutex_unlock(&priv->mutex);
6912}
6913
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006914#define IWL_DELAY_NEXT_SCAN (HZ*2)
6915
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006916static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006917{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006918 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006919 post_associate.work);
6920
6921 int rc = 0;
6922 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07006923 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006924
6925 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6926 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6927 return;
6928 }
6929
Joe Perches0795af52007-10-03 17:59:30 -07006930 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6931 priv->assoc_id,
6932 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006933
6934
6935 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6936 return;
6937
6938 mutex_lock(&priv->mutex);
6939
Johannes Berg32bfd352007-12-19 01:31:26 +01006940 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006941 mutex_unlock(&priv->mutex);
6942 return;
6943 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006944 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006945
Zhu Yib481de92007-09-25 17:54:57 -07006946 conf = ieee80211_get_hw_conf(priv->hw);
6947
6948 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006949 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006950
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006951 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6952 iwl4965_setup_rxon_timing(priv);
6953 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006954 sizeof(priv->rxon_timing), &priv->rxon_timing);
6955 if (rc)
6956 IWL_WARNING("REPLY_RXON_TIMING failed - "
6957 "Attempting to continue.\n");
6958
6959 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6960
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006961#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006962 if (priv->current_ht_config.is_ht)
6963 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006964#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006965 iwl4965_set_rxon_chain(priv);
6966 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6967
6968 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6969 priv->assoc_id, priv->beacon_int);
6970
6971 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6972 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6973 else
6974 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6975
6976 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6977 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6978 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6979 else
6980 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6981
6982 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6983 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6984
6985 }
6986
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006987 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006988
6989 switch (priv->iw_mode) {
6990 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006991 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006992 break;
6993
6994 case IEEE80211_IF_TYPE_IBSS:
6995
6996 /* clear out the station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006997 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006999 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
7000 iwl4965_rxon_add_station(priv, priv->bssid, 0);
7001 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
7002 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007003
7004 break;
7005
7006 default:
7007 IWL_ERROR("%s Should not be called in %d mode\n",
7008 __FUNCTION__, priv->iw_mode);
7009 break;
7010 }
7011
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007012 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007013
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007014#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07007015 /* Enable Rx differential gain and sensitivity calibrations */
7016 iwl4965_chain_noise_reset(priv);
7017 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007018#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07007019
7020 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7021 priv->assoc_station_added = 1;
7022
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007023 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08007024
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007025 /* we have just associated, don't start scan too early */
7026 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07007027 mutex_unlock(&priv->mutex);
7028}
7029
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007030static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07007031{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007032 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07007033
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007034 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007035 return;
7036
7037 mutex_lock(&priv->mutex);
7038
7039 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007040 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007041
7042 mutex_unlock(&priv->mutex);
7043}
7044
Zhu Yi76bb77e2007-11-22 10:53:22 +08007045static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
7046
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007047static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07007048{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007049 struct iwl4965_priv *priv =
7050 container_of(work, struct iwl4965_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07007051
7052 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
7053
7054 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7055 return;
7056
Zhu Yia0646472007-12-20 14:10:01 +08007057 if (test_bit(STATUS_CONF_PENDING, &priv->status))
7058 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08007059
Zhu Yib481de92007-09-25 17:54:57 -07007060 ieee80211_scan_completed(priv->hw);
7061
7062 /* Since setting the TXPOWER may have been deferred while
7063 * performing the scan, fire one off */
7064 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007065 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007066 mutex_unlock(&priv->mutex);
7067}
7068
7069/*****************************************************************************
7070 *
7071 * mac80211 entry point functions
7072 *
7073 *****************************************************************************/
7074
Zhu Yi5a66926a2008-01-14 17:46:18 -08007075#define UCODE_READY_TIMEOUT (2 * HZ)
7076
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007077static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007078{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007079 struct iwl4965_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007080 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07007081
7082 IWL_DEBUG_MAC80211("enter\n");
7083
Zhu Yi5a66926a2008-01-14 17:46:18 -08007084 if (pci_enable_device(priv->pci_dev)) {
7085 IWL_ERROR("Fail to pci_enable_device\n");
7086 return -ENODEV;
7087 }
7088 pci_restore_state(priv->pci_dev);
7089 pci_enable_msi(priv->pci_dev);
7090
7091 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
7092 DRV_NAME, priv);
7093 if (ret) {
7094 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
7095 goto out_disable_msi;
7096 }
7097
Zhu Yib481de92007-09-25 17:54:57 -07007098 /* we should be verifying the device is ready to be opened */
7099 mutex_lock(&priv->mutex);
7100
Zhu Yi5a66926a2008-01-14 17:46:18 -08007101 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
7102 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
7103 * ucode filename and max sizes are card-specific. */
7104
7105 if (!priv->ucode_code.len) {
7106 ret = iwl4965_read_ucode(priv);
7107 if (ret) {
7108 IWL_ERROR("Could not read microcode: %d\n", ret);
7109 mutex_unlock(&priv->mutex);
7110 goto out_release_irq;
7111 }
7112 }
7113
Zhu Yie655b9f2008-01-24 02:19:38 -08007114 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007115
Zhu Yib481de92007-09-25 17:54:57 -07007116 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007117
Zhu Yie655b9f2008-01-24 02:19:38 -08007118 if (ret)
7119 goto out_release_irq;
7120
7121 IWL_DEBUG_INFO("Start UP work done.\n");
7122
7123 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
7124 return 0;
7125
Zhu Yi5a66926a2008-01-14 17:46:18 -08007126 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
7127 * mac80211 will not be run successfully. */
7128 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
7129 test_bit(STATUS_READY, &priv->status),
7130 UCODE_READY_TIMEOUT);
7131 if (!ret) {
7132 if (!test_bit(STATUS_READY, &priv->status)) {
7133 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
7134 jiffies_to_msecs(UCODE_READY_TIMEOUT));
7135 ret = -ETIMEDOUT;
7136 goto out_release_irq;
7137 }
7138 }
7139
Zhu Yie655b9f2008-01-24 02:19:38 -08007140 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07007141 IWL_DEBUG_MAC80211("leave\n");
7142 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007143
7144out_release_irq:
7145 free_irq(priv->pci_dev->irq, priv);
7146out_disable_msi:
7147 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08007148 pci_disable_device(priv->pci_dev);
7149 priv->is_open = 0;
7150 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08007151 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007152}
7153
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007154static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007155{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007156 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007157
7158 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08007159
Zhu Yie655b9f2008-01-24 02:19:38 -08007160 if (!priv->is_open) {
7161 IWL_DEBUG_MAC80211("leave - skip\n");
7162 return;
7163 }
7164
Zhu Yib481de92007-09-25 17:54:57 -07007165 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007166
7167 if (iwl4965_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08007168 /* stop mac, cancel any scan request and clear
7169 * RXON_FILTER_ASSOC_MSK BIT
7170 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08007171 mutex_lock(&priv->mutex);
7172 iwl4965_scan_cancel_timeout(priv, 100);
7173 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08007174 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08007175 }
7176
Zhu Yi5a66926a2008-01-14 17:46:18 -08007177 iwl4965_down(priv);
7178
7179 flush_workqueue(priv->workqueue);
7180 free_irq(priv->pci_dev->irq, priv);
7181 pci_disable_msi(priv->pci_dev);
7182 pci_save_state(priv->pci_dev);
7183 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007184
Zhu Yib481de92007-09-25 17:54:57 -07007185 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007186}
7187
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007188static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007189 struct ieee80211_tx_control *ctl)
7190{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007191 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007192
7193 IWL_DEBUG_MAC80211("enter\n");
7194
7195 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7196 IWL_DEBUG_MAC80211("leave - monitor\n");
7197 return -1;
7198 }
7199
7200 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01007201 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07007202
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007203 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07007204 dev_kfree_skb_any(skb);
7205
7206 IWL_DEBUG_MAC80211("leave\n");
7207 return 0;
7208}
7209
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007210static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007211 struct ieee80211_if_init_conf *conf)
7212{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007213 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007214 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07007215 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007216
Johannes Berg32bfd352007-12-19 01:31:26 +01007217 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007218
Johannes Berg32bfd352007-12-19 01:31:26 +01007219 if (priv->vif) {
7220 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08007221 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07007222 }
7223
7224 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01007225 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07007226
7227 spin_unlock_irqrestore(&priv->lock, flags);
7228
7229 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02007230
7231 if (conf->mac_addr) {
7232 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7233 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7234 }
Zhu Yib481de92007-09-25 17:54:57 -07007235
Zhu Yi5a66926a2008-01-14 17:46:18 -08007236 if (iwl4965_is_ready(priv))
7237 iwl4965_set_mode(priv, conf->type);
7238
Zhu Yib481de92007-09-25 17:54:57 -07007239 mutex_unlock(&priv->mutex);
7240
Zhu Yi5a66926a2008-01-14 17:46:18 -08007241 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007242 return 0;
7243}
7244
7245/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007246 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07007247 *
7248 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7249 * be set inappropriately and the driver currently sets the hardware up to
7250 * use it whenever needed.
7251 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007252static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007253{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007254 struct iwl4965_priv *priv = hw->priv;
7255 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07007256 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08007257 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007258
7259 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01007260 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07007261
Zhu Yi12342c42007-12-20 11:27:32 +08007262 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
7263
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007264 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007265 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007266 ret = -EIO;
7267 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007268 }
7269
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007270 if (unlikely(!iwl4965_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07007271 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08007272 IWL_DEBUG_MAC80211("leave - scanning\n");
7273 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007274 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08007275 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007276 }
7277
7278 spin_lock_irqsave(&priv->lock, flags);
7279
Johannes Berg8318d782008-01-24 19:38:38 +01007280 ch_info = iwl4965_get_channel_info(priv, conf->channel->band,
7281 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07007282 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07007283 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7284 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007285 ret = -EINVAL;
7286 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007287 }
7288
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007289#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02007290 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07007291 * from any ht related info since 2.4 does not
7292 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02007293 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07007294#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7295 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7296#endif
7297 )
7298 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007299#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007300
Johannes Berg8318d782008-01-24 19:38:38 +01007301 iwl4965_set_rxon_channel(priv, conf->channel->band,
7302 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07007303
Johannes Berg8318d782008-01-24 19:38:38 +01007304 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07007305
7306 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01007307 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07007308 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007309 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007310
7311 spin_unlock_irqrestore(&priv->lock, flags);
7312
7313#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7314 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007315 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007316 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007317 }
7318#endif
7319
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007320 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07007321
7322 if (!conf->radio_enabled) {
7323 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007324 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007325 }
7326
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007327 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007328 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007329 ret = -EIO;
7330 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007331 }
7332
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007333 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007334
7335 if (memcmp(&priv->active_rxon,
7336 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007337 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007338 else
7339 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7340
7341 IWL_DEBUG_MAC80211("leave\n");
7342
Zhu Yia0646472007-12-20 14:10:01 +08007343out:
7344 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007345 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007346 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007347}
7348
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007349static void iwl4965_config_ap(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007350{
7351 int rc = 0;
7352
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08007353 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07007354 return;
7355
7356 /* The following should be done only at AP bring up */
7357 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7358
7359 /* RXON - unassoc (to set timing command) */
7360 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007361 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007362
7363 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007364 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7365 iwl4965_setup_rxon_timing(priv);
7366 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007367 sizeof(priv->rxon_timing), &priv->rxon_timing);
7368 if (rc)
7369 IWL_WARNING("REPLY_RXON_TIMING failed - "
7370 "Attempting to continue.\n");
7371
7372 iwl4965_set_rxon_chain(priv);
7373
7374 /* FIXME: what should be the assoc_id for AP? */
7375 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7376 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7377 priv->staging_rxon.flags |=
7378 RXON_FLG_SHORT_PREAMBLE_MSK;
7379 else
7380 priv->staging_rxon.flags &=
7381 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7382
7383 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7384 if (priv->assoc_capability &
7385 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7386 priv->staging_rxon.flags |=
7387 RXON_FLG_SHORT_SLOT_MSK;
7388 else
7389 priv->staging_rxon.flags &=
7390 ~RXON_FLG_SHORT_SLOT_MSK;
7391
7392 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7393 priv->staging_rxon.flags &=
7394 ~RXON_FLG_SHORT_SLOT_MSK;
7395 }
7396 /* restore RXON assoc */
7397 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007398 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007399 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007400 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08007401 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007402 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007403
7404 /* FIXME - we need to add code here to detect a totally new
7405 * configuration, reset the AP, unassoc, rxon timing, assoc,
7406 * clear sta table, add BCAST sta... */
7407}
7408
Johannes Berg32bfd352007-12-19 01:31:26 +01007409static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
7410 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07007411 struct ieee80211_if_conf *conf)
7412{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007413 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007414 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007415 unsigned long flags;
7416 int rc;
7417
7418 if (conf == NULL)
7419 return -EIO;
7420
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08007421 if (priv->vif != vif) {
7422 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
7423 mutex_unlock(&priv->mutex);
7424 return 0;
7425 }
7426
Zhu Yib481de92007-09-25 17:54:57 -07007427 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7428 (!conf->beacon || !conf->ssid_len)) {
7429 IWL_DEBUG_MAC80211
7430 ("Leaving in AP mode because HostAPD is not ready.\n");
7431 return 0;
7432 }
7433
Zhu Yi5a66926a2008-01-14 17:46:18 -08007434 if (!iwl4965_is_alive(priv))
7435 return -EAGAIN;
7436
Zhu Yib481de92007-09-25 17:54:57 -07007437 mutex_lock(&priv->mutex);
7438
Zhu Yib481de92007-09-25 17:54:57 -07007439 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007440 IWL_DEBUG_MAC80211("bssid: %s\n",
7441 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007442
Johannes Berg4150c572007-09-17 01:29:23 -04007443/*
7444 * very dubious code was here; the probe filtering flag is never set:
7445 *
Zhu Yib481de92007-09-25 17:54:57 -07007446 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7447 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007448 */
Zhu Yib481de92007-09-25 17:54:57 -07007449
7450 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7451 if (!conf->bssid) {
7452 conf->bssid = priv->mac_addr;
7453 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007454 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7455 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007456 }
7457 if (priv->ibss_beacon)
7458 dev_kfree_skb(priv->ibss_beacon);
7459
7460 priv->ibss_beacon = conf->beacon;
7461 }
7462
Mohamed Abbasfde35712007-11-29 11:10:15 +08007463 if (iwl4965_is_rfkill(priv))
7464 goto done;
7465
Zhu Yib481de92007-09-25 17:54:57 -07007466 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7467 !is_multicast_ether_addr(conf->bssid)) {
7468 /* If there is currently a HW scan going on in the background
7469 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007470 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007471 IWL_WARNING("Aborted scan still in progress "
7472 "after 100ms\n");
7473 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7474 mutex_unlock(&priv->mutex);
7475 return -EAGAIN;
7476 }
7477 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7478
7479 /* TODO: Audit driver for usage of these members and see
7480 * if mac80211 deprecates them (priv->bssid looks like it
7481 * shouldn't be there, but I haven't scanned the IBSS code
7482 * to verify) - jpk */
7483 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7484
7485 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007486 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007487 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007488 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007489 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007490 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07007491 priv, priv->active_rxon.bssid_addr, 1);
7492 }
7493
7494 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007495 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007496 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007497 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007498 }
7499
Mohamed Abbasfde35712007-11-29 11:10:15 +08007500 done:
Zhu Yib481de92007-09-25 17:54:57 -07007501 spin_lock_irqsave(&priv->lock, flags);
7502 if (!conf->ssid_len)
7503 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7504 else
7505 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7506
7507 priv->essid_len = conf->ssid_len;
7508 spin_unlock_irqrestore(&priv->lock, flags);
7509
7510 IWL_DEBUG_MAC80211("leave\n");
7511 mutex_unlock(&priv->mutex);
7512
7513 return 0;
7514}
7515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007516static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007517 unsigned int changed_flags,
7518 unsigned int *total_flags,
7519 int mc_count, struct dev_addr_list *mc_list)
7520{
7521 /*
7522 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007523 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007524 */
7525 *total_flags = 0;
7526}
7527
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007528static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007529 struct ieee80211_if_init_conf *conf)
7530{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007531 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007532
7533 IWL_DEBUG_MAC80211("enter\n");
7534
7535 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007536
Mohamed Abbasfde35712007-11-29 11:10:15 +08007537 if (iwl4965_is_ready_rf(priv)) {
7538 iwl4965_scan_cancel_timeout(priv, 100);
7539 cancel_delayed_work(&priv->post_associate);
7540 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7541 iwl4965_commit_rxon(priv);
7542 }
Johannes Berg32bfd352007-12-19 01:31:26 +01007543 if (priv->vif == conf->vif) {
7544 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007545 memset(priv->bssid, 0, ETH_ALEN);
7546 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7547 priv->essid_len = 0;
7548 }
7549 mutex_unlock(&priv->mutex);
7550
7551 IWL_DEBUG_MAC80211("leave\n");
7552
7553}
Johannes Berg471b3ef2007-12-28 14:32:58 +01007554
7555static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
7556 struct ieee80211_vif *vif,
7557 struct ieee80211_bss_conf *bss_conf,
7558 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02007559{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007560 struct iwl4965_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02007561
Johannes Berg471b3ef2007-12-28 14:32:58 +01007562 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
7563 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02007564 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7565 else
7566 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7567 }
7568
Johannes Berg471b3ef2007-12-28 14:32:58 +01007569 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Johannes Berg8318d782008-01-24 19:38:38 +01007570 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02007571 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7572 else
7573 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7574 }
7575
Johannes Berg471b3ef2007-12-28 14:32:58 +01007576 if (changes & BSS_CHANGED_ASSOC) {
7577 /*
7578 * TODO:
7579 * do stuff instead of sniffing assoc resp
7580 */
7581 }
7582
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007583 if (iwl4965_is_associated(priv))
7584 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02007585}
Zhu Yib481de92007-09-25 17:54:57 -07007586
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007587static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007588{
7589 int rc = 0;
7590 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007591 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007592
7593 IWL_DEBUG_MAC80211("enter\n");
7594
mabbas052c4b92007-10-25 17:15:43 +08007595 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007596 spin_lock_irqsave(&priv->lock, flags);
7597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007598 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007599 rc = -EIO;
7600 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7601 goto out_unlock;
7602 }
7603
7604 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7605 rc = -EIO;
7606 IWL_ERROR("ERROR: APs don't scan\n");
7607 goto out_unlock;
7608 }
7609
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007610 /* we don't schedule scan within next_scan_jiffies period */
7611 if (priv->next_scan_jiffies &&
7612 time_after(priv->next_scan_jiffies, jiffies)) {
7613 rc = -EAGAIN;
7614 goto out_unlock;
7615 }
Zhu Yib481de92007-09-25 17:54:57 -07007616 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007617 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7618 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007619 rc = -EAGAIN;
7620 goto out_unlock;
7621 }
7622 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007623 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007624 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007625
7626 priv->one_direct_scan = 1;
7627 priv->direct_ssid_len = (u8)
7628 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7629 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007630 } else
7631 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007632
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007633 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007634
7635 IWL_DEBUG_MAC80211("leave\n");
7636
7637out_unlock:
7638 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007639 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007640
7641 return rc;
7642}
7643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007644static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007645 const u8 *local_addr, const u8 *addr,
7646 struct ieee80211_key_conf *key)
7647{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007648 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007649 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007650 int rc = 0;
7651 u8 sta_id;
7652
7653 IWL_DEBUG_MAC80211("enter\n");
7654
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007655 if (!iwl4965_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007656 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7657 return -EOPNOTSUPP;
7658 }
7659
7660 if (is_zero_ether_addr(addr))
7661 /* only support pairwise keys */
7662 return -EOPNOTSUPP;
7663
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007664 sta_id = iwl4965_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07007665 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007666 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7667 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007668 return -EINVAL;
7669 }
7670
7671 mutex_lock(&priv->mutex);
7672
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007673 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007674
Zhu Yib481de92007-09-25 17:54:57 -07007675 switch (cmd) {
7676 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007677 rc = iwl4965_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007678 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007679 iwl4965_set_rxon_hwcrypto(priv, 1);
7680 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007681 key->hw_key_idx = sta_id;
7682 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7683 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7684 }
7685 break;
7686 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007687 rc = iwl4965_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007688 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007689 iwl4965_set_rxon_hwcrypto(priv, 0);
7690 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007691 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7692 }
7693 break;
7694 default:
7695 rc = -EINVAL;
7696 }
7697
7698 IWL_DEBUG_MAC80211("leave\n");
7699 mutex_unlock(&priv->mutex);
7700
7701 return rc;
7702}
7703
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007704static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007705 const struct ieee80211_tx_queue_params *params)
7706{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007707 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007708 unsigned long flags;
7709 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007710
7711 IWL_DEBUG_MAC80211("enter\n");
7712
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007713 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007714 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7715 return -EIO;
7716 }
7717
7718 if (queue >= AC_NUM) {
7719 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7720 return 0;
7721 }
7722
Zhu Yib481de92007-09-25 17:54:57 -07007723 if (!priv->qos_data.qos_enable) {
7724 priv->qos_data.qos_active = 0;
7725 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7726 return 0;
7727 }
7728 q = AC_NUM - 1 - queue;
7729
7730 spin_lock_irqsave(&priv->lock, flags);
7731
7732 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7733 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7734 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7735 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007736 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007737
7738 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7739 priv->qos_data.qos_active = 1;
7740
7741 spin_unlock_irqrestore(&priv->lock, flags);
7742
7743 mutex_lock(&priv->mutex);
7744 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007745 iwl4965_activate_qos(priv, 1);
7746 else if (priv->assoc_id && iwl4965_is_associated(priv))
7747 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007748
7749 mutex_unlock(&priv->mutex);
7750
Zhu Yib481de92007-09-25 17:54:57 -07007751 IWL_DEBUG_MAC80211("leave\n");
7752 return 0;
7753}
7754
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007755static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007756 struct ieee80211_tx_queue_stats *stats)
7757{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007758 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007759 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007760 struct iwl4965_tx_queue *txq;
7761 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007762 unsigned long flags;
7763
7764 IWL_DEBUG_MAC80211("enter\n");
7765
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007766 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007767 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7768 return -EIO;
7769 }
7770
7771 spin_lock_irqsave(&priv->lock, flags);
7772
7773 for (i = 0; i < AC_NUM; i++) {
7774 txq = &priv->txq[i];
7775 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007776 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007777
7778 stats->data[i].len = q->n_window - avail;
7779 stats->data[i].limit = q->n_window - q->high_mark;
7780 stats->data[i].count = q->n_window;
7781
7782 }
7783 spin_unlock_irqrestore(&priv->lock, flags);
7784
7785 IWL_DEBUG_MAC80211("leave\n");
7786
7787 return 0;
7788}
7789
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007790static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007791 struct ieee80211_low_level_stats *stats)
7792{
7793 IWL_DEBUG_MAC80211("enter\n");
7794 IWL_DEBUG_MAC80211("leave\n");
7795
7796 return 0;
7797}
7798
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007799static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007800{
7801 IWL_DEBUG_MAC80211("enter\n");
7802 IWL_DEBUG_MAC80211("leave\n");
7803
7804 return 0;
7805}
7806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007807static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007808{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007809 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007810 unsigned long flags;
7811
7812 mutex_lock(&priv->mutex);
7813 IWL_DEBUG_MAC80211("enter\n");
7814
7815 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007816#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007817 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007818 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007819 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007820#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007821
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007822 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007823
7824 cancel_delayed_work(&priv->post_associate);
7825
7826 spin_lock_irqsave(&priv->lock, flags);
7827 priv->assoc_id = 0;
7828 priv->assoc_capability = 0;
7829 priv->call_post_assoc_from_beacon = 0;
7830 priv->assoc_station_added = 0;
7831
7832 /* new association get rid of ibss beacon skb */
7833 if (priv->ibss_beacon)
7834 dev_kfree_skb(priv->ibss_beacon);
7835
7836 priv->ibss_beacon = NULL;
7837
7838 priv->beacon_int = priv->hw->conf.beacon_int;
7839 priv->timestamp1 = 0;
7840 priv->timestamp0 = 0;
7841 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7842 priv->beacon_int = 0;
7843
7844 spin_unlock_irqrestore(&priv->lock, flags);
7845
Mohamed Abbasfde35712007-11-29 11:10:15 +08007846 if (!iwl4965_is_ready_rf(priv)) {
7847 IWL_DEBUG_MAC80211("leave - not ready\n");
7848 mutex_unlock(&priv->mutex);
7849 return;
7850 }
7851
mabbas052c4b92007-10-25 17:15:43 +08007852 /* we are restarting association process
7853 * clear RXON_FILTER_ASSOC_MSK bit
7854 */
7855 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007856 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007857 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007858 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007859 }
7860
Zhu Yib481de92007-09-25 17:54:57 -07007861 /* Per mac80211.h: This is only used in IBSS mode... */
7862 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007863
Zhu Yib481de92007-09-25 17:54:57 -07007864 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7865 mutex_unlock(&priv->mutex);
7866 return;
7867 }
7868
Zhu Yib481de92007-09-25 17:54:57 -07007869 priv->only_active_channel = 0;
7870
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007871 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007872
7873 mutex_unlock(&priv->mutex);
7874
7875 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007876}
7877
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007878static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007879 struct ieee80211_tx_control *control)
7880{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007881 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007882 unsigned long flags;
7883
7884 mutex_lock(&priv->mutex);
7885 IWL_DEBUG_MAC80211("enter\n");
7886
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007887 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007888 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7889 mutex_unlock(&priv->mutex);
7890 return -EIO;
7891 }
7892
7893 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7894 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7895 mutex_unlock(&priv->mutex);
7896 return -EIO;
7897 }
7898
7899 spin_lock_irqsave(&priv->lock, flags);
7900
7901 if (priv->ibss_beacon)
7902 dev_kfree_skb(priv->ibss_beacon);
7903
7904 priv->ibss_beacon = skb;
7905
7906 priv->assoc_id = 0;
7907
7908 IWL_DEBUG_MAC80211("leave\n");
7909 spin_unlock_irqrestore(&priv->lock, flags);
7910
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007911 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007912
7913 queue_work(priv->workqueue, &priv->post_associate.work);
7914
7915 mutex_unlock(&priv->mutex);
7916
7917 return 0;
7918}
7919
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007920#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007921
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007922static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
7923 struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007924{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007925 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
7926 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
7927 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07007928
7929 IWL_DEBUG_MAC80211("enter: \n");
7930
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007931 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
7932 iwl_conf->is_ht = 0;
7933 return;
Zhu Yib481de92007-09-25 17:54:57 -07007934 }
7935
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007936 iwl_conf->is_ht = 1;
7937 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07007938
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007939 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
7940 iwl_conf->sgf |= 0x1;
7941 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
7942 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07007943
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007944 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
7945 iwl_conf->max_amsdu_size =
7946 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007947
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007948 iwl_conf->supported_chan_width =
7949 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007950 iwl_conf->extension_chan_offset =
7951 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
7952 /* If no above or below channel supplied disable FAT channel */
7953 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
7954 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
7955 iwl_conf->supported_chan_width = 0;
7956
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007957 iwl_conf->tx_mimo_ps_mode =
7958 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
7959 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07007960
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007961 iwl_conf->control_channel = ht_bss_conf->primary_channel;
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007962 iwl_conf->tx_chan_width =
7963 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
7964 iwl_conf->ht_protection =
7965 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
7966 iwl_conf->non_GF_STA_present =
7967 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07007968
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007969 IWL_DEBUG_MAC80211("control channel %d\n",
7970 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007971 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007972}
7973
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007974static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007975 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007976{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007977 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007978
7979 IWL_DEBUG_MAC80211("enter: \n");
7980
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007981 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007982 iwl4965_set_rxon_chain(priv);
7983
7984 if (priv && priv->assoc_id &&
7985 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
7986 unsigned long flags;
7987
7988 spin_lock_irqsave(&priv->lock, flags);
7989 if (priv->beacon_int)
7990 queue_work(priv->workqueue, &priv->post_associate.work);
7991 else
7992 priv->call_post_assoc_from_beacon = 1;
7993 spin_unlock_irqrestore(&priv->lock, flags);
7994 }
7995
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007996 IWL_DEBUG_MAC80211("leave:\n");
7997 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007998}
7999
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008000#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07008001
8002/*****************************************************************************
8003 *
8004 * sysfs attributes
8005 *
8006 *****************************************************************************/
8007
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008008#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07008009
8010/*
8011 * The following adds a new attribute to the sysfs representation
8012 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
8013 * used for controlling the debug level.
8014 *
8015 * See the level definitions in iwl for details.
8016 */
8017
8018static ssize_t show_debug_level(struct device_driver *d, char *buf)
8019{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008020 return sprintf(buf, "0x%08X\n", iwl4965_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008021}
8022static ssize_t store_debug_level(struct device_driver *d,
8023 const char *buf, size_t count)
8024{
8025 char *p = (char *)buf;
8026 u32 val;
8027
8028 val = simple_strtoul(p, &p, 0);
8029 if (p == buf)
8030 printk(KERN_INFO DRV_NAME
8031 ": %s is not in hex or decimal form.\n", buf);
8032 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008033 iwl4965_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07008034
8035 return strnlen(buf, count);
8036}
8037
8038static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
8039 show_debug_level, store_debug_level);
8040
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008041#endif /* CONFIG_IWL4965_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07008042
8043static ssize_t show_rf_kill(struct device *d,
8044 struct device_attribute *attr, char *buf)
8045{
8046 /*
8047 * 0 - RF kill not enabled
8048 * 1 - SW based RF kill active (sysfs)
8049 * 2 - HW based RF kill active
8050 * 3 - Both HW and SW based RF kill active
8051 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008052 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008053 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
8054 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
8055
8056 return sprintf(buf, "%i\n", val);
8057}
8058
8059static ssize_t store_rf_kill(struct device *d,
8060 struct device_attribute *attr,
8061 const char *buf, size_t count)
8062{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008063 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008064
8065 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008066 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07008067 mutex_unlock(&priv->mutex);
8068
8069 return count;
8070}
8071
8072static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
8073
8074static ssize_t show_temperature(struct device *d,
8075 struct device_attribute *attr, char *buf)
8076{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008077 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008078
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008079 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008080 return -EAGAIN;
8081
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008082 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07008083}
8084
8085static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8086
8087static ssize_t show_rs_window(struct device *d,
8088 struct device_attribute *attr,
8089 char *buf)
8090{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008091 struct iwl4965_priv *priv = d->driver_data;
8092 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07008093}
8094static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8095
8096static ssize_t show_tx_power(struct device *d,
8097 struct device_attribute *attr, char *buf)
8098{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008099 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008100 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8101}
8102
8103static ssize_t store_tx_power(struct device *d,
8104 struct device_attribute *attr,
8105 const char *buf, size_t count)
8106{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008107 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008108 char *p = (char *)buf;
8109 u32 val;
8110
8111 val = simple_strtoul(p, &p, 10);
8112 if (p == buf)
8113 printk(KERN_INFO DRV_NAME
8114 ": %s is not in decimal form.\n", buf);
8115 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008116 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07008117
8118 return count;
8119}
8120
8121static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8122
8123static ssize_t show_flags(struct device *d,
8124 struct device_attribute *attr, char *buf)
8125{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008126 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008127
8128 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8129}
8130
8131static ssize_t store_flags(struct device *d,
8132 struct device_attribute *attr,
8133 const char *buf, size_t count)
8134{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008135 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008136 u32 flags = simple_strtoul(buf, NULL, 0);
8137
8138 mutex_lock(&priv->mutex);
8139 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8140 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008141 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008142 IWL_WARNING("Could not cancel scan.\n");
8143 else {
8144 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8145 flags);
8146 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008147 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008148 }
8149 }
8150 mutex_unlock(&priv->mutex);
8151
8152 return count;
8153}
8154
8155static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8156
8157static ssize_t show_filter_flags(struct device *d,
8158 struct device_attribute *attr, char *buf)
8159{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008160 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008161
8162 return sprintf(buf, "0x%04X\n",
8163 le32_to_cpu(priv->active_rxon.filter_flags));
8164}
8165
8166static ssize_t store_filter_flags(struct device *d,
8167 struct device_attribute *attr,
8168 const char *buf, size_t count)
8169{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008170 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008171 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8172
8173 mutex_lock(&priv->mutex);
8174 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8175 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008176 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008177 IWL_WARNING("Could not cancel scan.\n");
8178 else {
8179 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8180 "0x%04X\n", filter_flags);
8181 priv->staging_rxon.filter_flags =
8182 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008183 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008184 }
8185 }
8186 mutex_unlock(&priv->mutex);
8187
8188 return count;
8189}
8190
8191static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8192 store_filter_flags);
8193
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008194#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008195
8196static ssize_t show_measurement(struct device *d,
8197 struct device_attribute *attr, char *buf)
8198{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008199 struct iwl4965_priv *priv = dev_get_drvdata(d);
8200 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07008201 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8202 u8 *data = (u8 *) & measure_report;
8203 unsigned long flags;
8204
8205 spin_lock_irqsave(&priv->lock, flags);
8206 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8207 spin_unlock_irqrestore(&priv->lock, flags);
8208 return 0;
8209 }
8210 memcpy(&measure_report, &priv->measure_report, size);
8211 priv->measurement_status = 0;
8212 spin_unlock_irqrestore(&priv->lock, flags);
8213
8214 while (size && (PAGE_SIZE - len)) {
8215 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8216 PAGE_SIZE - len, 1);
8217 len = strlen(buf);
8218 if (PAGE_SIZE - len)
8219 buf[len++] = '\n';
8220
8221 ofs += 16;
8222 size -= min(size, 16U);
8223 }
8224
8225 return len;
8226}
8227
8228static ssize_t store_measurement(struct device *d,
8229 struct device_attribute *attr,
8230 const char *buf, size_t count)
8231{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008232 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008233 struct ieee80211_measurement_params params = {
8234 .channel = le16_to_cpu(priv->active_rxon.channel),
8235 .start_time = cpu_to_le64(priv->last_tsf),
8236 .duration = cpu_to_le16(1),
8237 };
8238 u8 type = IWL_MEASURE_BASIC;
8239 u8 buffer[32];
8240 u8 channel;
8241
8242 if (count) {
8243 char *p = buffer;
8244 strncpy(buffer, buf, min(sizeof(buffer), count));
8245 channel = simple_strtoul(p, NULL, 0);
8246 if (channel)
8247 params.channel = channel;
8248
8249 p = buffer;
8250 while (*p && *p != ' ')
8251 p++;
8252 if (*p)
8253 type = simple_strtoul(p + 1, NULL, 0);
8254 }
8255
8256 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8257 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008258 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07008259
8260 return count;
8261}
8262
8263static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8264 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008265#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07008266
8267static ssize_t store_retry_rate(struct device *d,
8268 struct device_attribute *attr,
8269 const char *buf, size_t count)
8270{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008271 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008272
8273 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8274 if (priv->retry_rate <= 0)
8275 priv->retry_rate = 1;
8276
8277 return count;
8278}
8279
8280static ssize_t show_retry_rate(struct device *d,
8281 struct device_attribute *attr, char *buf)
8282{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008283 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008284 return sprintf(buf, "%d", priv->retry_rate);
8285}
8286
8287static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8288 store_retry_rate);
8289
8290static ssize_t store_power_level(struct device *d,
8291 struct device_attribute *attr,
8292 const char *buf, size_t count)
8293{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008294 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008295 int rc;
8296 int mode;
8297
8298 mode = simple_strtoul(buf, NULL, 0);
8299 mutex_lock(&priv->mutex);
8300
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008301 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008302 rc = -EAGAIN;
8303 goto out;
8304 }
8305
8306 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8307 mode = IWL_POWER_AC;
8308 else
8309 mode |= IWL_POWER_ENABLED;
8310
8311 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008312 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07008313 if (rc) {
8314 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8315 goto out;
8316 }
8317 priv->power_mode = mode;
8318 }
8319
8320 rc = count;
8321
8322 out:
8323 mutex_unlock(&priv->mutex);
8324 return rc;
8325}
8326
8327#define MAX_WX_STRING 80
8328
8329/* Values are in microsecond */
8330static const s32 timeout_duration[] = {
8331 350000,
8332 250000,
8333 75000,
8334 37000,
8335 25000,
8336};
8337static const s32 period_duration[] = {
8338 400000,
8339 700000,
8340 1000000,
8341 1000000,
8342 1000000
8343};
8344
8345static ssize_t show_power_level(struct device *d,
8346 struct device_attribute *attr, char *buf)
8347{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008348 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008349 int level = IWL_POWER_LEVEL(priv->power_mode);
8350 char *p = buf;
8351
8352 p += sprintf(p, "%d ", level);
8353 switch (level) {
8354 case IWL_POWER_MODE_CAM:
8355 case IWL_POWER_AC:
8356 p += sprintf(p, "(AC)");
8357 break;
8358 case IWL_POWER_BATTERY:
8359 p += sprintf(p, "(BATTERY)");
8360 break;
8361 default:
8362 p += sprintf(p,
8363 "(Timeout %dms, Period %dms)",
8364 timeout_duration[level - 1] / 1000,
8365 period_duration[level - 1] / 1000);
8366 }
8367
8368 if (!(priv->power_mode & IWL_POWER_ENABLED))
8369 p += sprintf(p, " OFF\n");
8370 else
8371 p += sprintf(p, " \n");
8372
8373 return (p - buf + 1);
8374
8375}
8376
8377static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8378 store_power_level);
8379
8380static ssize_t show_channels(struct device *d,
8381 struct device_attribute *attr, char *buf)
8382{
Johannes Berg8318d782008-01-24 19:38:38 +01008383 /* all this shit doesn't belong into sysfs anyway */
8384 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07008385}
8386
8387static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8388
8389static ssize_t show_statistics(struct device *d,
8390 struct device_attribute *attr, char *buf)
8391{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008392 struct iwl4965_priv *priv = dev_get_drvdata(d);
8393 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07008394 u32 len = 0, ofs = 0;
8395 u8 *data = (u8 *) & priv->statistics;
8396 int rc = 0;
8397
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008398 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008399 return -EAGAIN;
8400
8401 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008402 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008403 mutex_unlock(&priv->mutex);
8404
8405 if (rc) {
8406 len = sprintf(buf,
8407 "Error sending statistics request: 0x%08X\n", rc);
8408 return len;
8409 }
8410
8411 while (size && (PAGE_SIZE - len)) {
8412 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8413 PAGE_SIZE - len, 1);
8414 len = strlen(buf);
8415 if (PAGE_SIZE - len)
8416 buf[len++] = '\n';
8417
8418 ofs += 16;
8419 size -= min(size, 16U);
8420 }
8421
8422 return len;
8423}
8424
8425static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8426
8427static ssize_t show_antenna(struct device *d,
8428 struct device_attribute *attr, char *buf)
8429{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008430 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008431
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008432 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008433 return -EAGAIN;
8434
8435 return sprintf(buf, "%d\n", priv->antenna);
8436}
8437
8438static ssize_t store_antenna(struct device *d,
8439 struct device_attribute *attr,
8440 const char *buf, size_t count)
8441{
8442 int ant;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008443 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008444
8445 if (count == 0)
8446 return 0;
8447
8448 if (sscanf(buf, "%1i", &ant) != 1) {
8449 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8450 return count;
8451 }
8452
8453 if ((ant >= 0) && (ant <= 2)) {
8454 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008455 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07008456 } else
8457 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8458
8459
8460 return count;
8461}
8462
8463static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8464
8465static ssize_t show_status(struct device *d,
8466 struct device_attribute *attr, char *buf)
8467{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008468 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8469 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008470 return -EAGAIN;
8471 return sprintf(buf, "0x%08x\n", (int)priv->status);
8472}
8473
8474static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8475
8476static ssize_t dump_error_log(struct device *d,
8477 struct device_attribute *attr,
8478 const char *buf, size_t count)
8479{
8480 char *p = (char *)buf;
8481
8482 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008483 iwl4965_dump_nic_error_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008484
8485 return strnlen(buf, count);
8486}
8487
8488static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8489
8490static ssize_t dump_event_log(struct device *d,
8491 struct device_attribute *attr,
8492 const char *buf, size_t count)
8493{
8494 char *p = (char *)buf;
8495
8496 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008497 iwl4965_dump_nic_event_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008498
8499 return strnlen(buf, count);
8500}
8501
8502static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8503
8504/*****************************************************************************
8505 *
8506 * driver setup and teardown
8507 *
8508 *****************************************************************************/
8509
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008510static void iwl4965_setup_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008511{
8512 priv->workqueue = create_workqueue(DRV_NAME);
8513
8514 init_waitqueue_head(&priv->wait_command_queue);
8515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008516 INIT_WORK(&priv->up, iwl4965_bg_up);
8517 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8518 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8519 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8520 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8521 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8522 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8523 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8524 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8525 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8526 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8527 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008528
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008529 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008530
8531 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008532 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008533}
8534
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008535static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008536{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008537 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008538
Joonwoo Park3ae6a052007-11-29 10:43:16 +09008539 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008540 cancel_delayed_work(&priv->scan_check);
8541 cancel_delayed_work(&priv->alive_start);
8542 cancel_delayed_work(&priv->post_associate);
8543 cancel_work_sync(&priv->beacon_update);
8544}
8545
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008546static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008547 &dev_attr_antenna.attr,
8548 &dev_attr_channels.attr,
8549 &dev_attr_dump_errors.attr,
8550 &dev_attr_dump_events.attr,
8551 &dev_attr_flags.attr,
8552 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008553#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008554 &dev_attr_measurement.attr,
8555#endif
8556 &dev_attr_power_level.attr,
8557 &dev_attr_retry_rate.attr,
8558 &dev_attr_rf_kill.attr,
8559 &dev_attr_rs_window.attr,
8560 &dev_attr_statistics.attr,
8561 &dev_attr_status.attr,
8562 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07008563 &dev_attr_tx_power.attr,
8564
8565 NULL
8566};
8567
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008568static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008569 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008570 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008571};
8572
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008573static struct ieee80211_ops iwl4965_hw_ops = {
8574 .tx = iwl4965_mac_tx,
8575 .start = iwl4965_mac_start,
8576 .stop = iwl4965_mac_stop,
8577 .add_interface = iwl4965_mac_add_interface,
8578 .remove_interface = iwl4965_mac_remove_interface,
8579 .config = iwl4965_mac_config,
8580 .config_interface = iwl4965_mac_config_interface,
8581 .configure_filter = iwl4965_configure_filter,
8582 .set_key = iwl4965_mac_set_key,
8583 .get_stats = iwl4965_mac_get_stats,
8584 .get_tx_stats = iwl4965_mac_get_tx_stats,
8585 .conf_tx = iwl4965_mac_conf_tx,
8586 .get_tsf = iwl4965_mac_get_tsf,
8587 .reset_tsf = iwl4965_mac_reset_tsf,
8588 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01008589 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008590#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008591 .conf_ht = iwl4965_mac_conf_ht,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02008592 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008593#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008594 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008595};
8596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008597static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008598{
8599 int err = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008600 struct iwl4965_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008601 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008602 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008603 int i;
Zhu Yi5a66926a2008-01-14 17:46:18 -08008604 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008605
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008606 /* Disabling hardware scan means that mac80211 will perform scans
8607 * "the hard way", rather than using device's scan. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008608 if (iwl4965_param_disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008609 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008610 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008611 }
8612
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008613 if ((iwl4965_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8614 (iwl4965_param_queues_num < IWL_MIN_NUM_QUEUES)) {
Zhu Yib481de92007-09-25 17:54:57 -07008615 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8616 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8617 err = -EINVAL;
8618 goto out;
8619 }
8620
8621 /* mac80211 allocates memory for this device instance, including
8622 * space for this driver's private structure */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008623 hw = ieee80211_alloc_hw(sizeof(struct iwl4965_priv), &iwl4965_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07008624 if (hw == NULL) {
8625 IWL_ERROR("Can not allocate network device\n");
8626 err = -ENOMEM;
8627 goto out;
8628 }
8629 SET_IEEE80211_DEV(hw, &pdev->dev);
8630
Johannes Bergf51359a2007-10-28 14:53:36 +01008631 hw->rate_control_algorithm = "iwl-4965-rs";
8632
Zhu Yib481de92007-09-25 17:54:57 -07008633 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8634 priv = hw->priv;
8635 priv->hw = hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008636 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008637
8638 priv->pci_dev = pdev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008639 priv->antenna = (enum iwl4965_antenna)iwl4965_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008640#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008641 iwl4965_debug_level = iwl4965_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008642 atomic_set(&priv->restrict_refcnt, 0);
8643#endif
8644 priv->retry_rate = 1;
8645
8646 priv->ibss_beacon = NULL;
8647
8648 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8649 * the range of signal quality values that we'll provide.
8650 * Negative values for level/noise indicate that we'll provide dBm.
8651 * For WE, at least, non-0 values here *enable* display of values
8652 * in app (iwconfig). */
8653 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8654 hw->max_noise = -20; /* noise level, negative indicates dBm */
8655 hw->max_signal = 100; /* link quality indication (%) */
8656
8657 /* Tell mac80211 our Tx characteristics */
8658 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8659
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008660 /* Default value; 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07008661 hw->queues = 4;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008662#ifdef CONFIG_IWL4965_HT
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008663 /* Enhanced value; more queues, to support 11n aggregation */
Zhu Yib481de92007-09-25 17:54:57 -07008664 hw->queues = 16;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008665#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07008666
8667 spin_lock_init(&priv->lock);
8668 spin_lock_init(&priv->power_data.lock);
8669 spin_lock_init(&priv->sta_lock);
8670 spin_lock_init(&priv->hcmd_lock);
8671 spin_lock_init(&priv->lq_mngr.lock);
8672
8673 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8674 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8675
8676 INIT_LIST_HEAD(&priv->free_frames);
8677
8678 mutex_init(&priv->mutex);
8679 if (pci_enable_device(pdev)) {
8680 err = -ENODEV;
8681 goto out_ieee80211_free_hw;
8682 }
8683
8684 pci_set_master(pdev);
8685
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008686 /* Clear the driver's (not device's) station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008687 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008688
8689 priv->data_retry_limit = -1;
8690 priv->ieee_channels = NULL;
8691 priv->ieee_rates = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01008692 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07008693
8694 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8695 if (!err)
8696 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8697 if (err) {
8698 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
8699 goto out_pci_disable_device;
8700 }
8701
8702 pci_set_drvdata(pdev, priv);
8703 err = pci_request_regions(pdev, DRV_NAME);
8704 if (err)
8705 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008706
Zhu Yib481de92007-09-25 17:54:57 -07008707 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8708 * PCI Tx retries from interfering with C3 CPU state */
8709 pci_write_config_byte(pdev, 0x41, 0x00);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008710
Zhu Yib481de92007-09-25 17:54:57 -07008711 priv->hw_base = pci_iomap(pdev, 0, 0);
8712 if (!priv->hw_base) {
8713 err = -ENODEV;
8714 goto out_pci_release_regions;
8715 }
8716
8717 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8718 (unsigned long long) pci_resource_len(pdev, 0));
8719 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8720
8721 /* Initialize module parameter values here */
8722
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008723 /* Disable radio (SW RF KILL) via parameter when loading driver */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008724 if (iwl4965_param_disable) {
Zhu Yib481de92007-09-25 17:54:57 -07008725 set_bit(STATUS_RF_KILL_SW, &priv->status);
8726 IWL_DEBUG_INFO("Radio disabled.\n");
8727 }
8728
8729 priv->iw_mode = IEEE80211_IF_TYPE_STA;
8730
8731 priv->ps_mode = 0;
8732 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
Zhu Yib481de92007-09-25 17:54:57 -07008733 priv->valid_antenna = 0x7; /* assume all 3 connected */
8734 priv->ps_mode = IWL_MIMO_PS_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07008735
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008736 /* Choose which receivers/antennas to use */
Zhu Yib481de92007-09-25 17:54:57 -07008737 iwl4965_set_rxon_chain(priv);
8738
Tomas Winkler82b9a122008-03-04 18:09:30 -08008739
Zhu Yib481de92007-09-25 17:54:57 -07008740 printk(KERN_INFO DRV_NAME
Tomas Winkler82b9a122008-03-04 18:09:30 -08008741 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
Zhu Yib481de92007-09-25 17:54:57 -07008742
8743 /* Device-specific setup */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008744 if (iwl4965_hw_set_hw_setting(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008745 IWL_ERROR("failed to set hw settings\n");
Zhu Yib481de92007-09-25 17:54:57 -07008746 goto out_iounmap;
8747 }
8748
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008749 if (iwl4965_param_qos_enable)
Zhu Yib481de92007-09-25 17:54:57 -07008750 priv->qos_data.qos_enable = 1;
8751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008752 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008753
8754 priv->qos_data.qos_active = 0;
8755 priv->qos_data.qos_cap.val = 0;
Zhu Yib481de92007-09-25 17:54:57 -07008756
Johannes Berg8318d782008-01-24 19:38:38 +01008757 iwl4965_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008758 iwl4965_setup_deferred_work(priv);
8759 iwl4965_setup_rx_handlers(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008760
8761 priv->rates_mask = IWL_RATES_MASK;
8762 /* If power management is turned on, default to AC mode */
8763 priv->power_mode = IWL_POWER_AC;
8764 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
8765
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008766 iwl4965_disable_interrupts(priv);
Jes Sorensen49df2b32007-10-26 16:10:39 +02008767
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008768 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008769 if (err) {
8770 IWL_ERROR("failed to create sysfs device attributes\n");
Zhu Yib481de92007-09-25 17:54:57 -07008771 goto out_release_irq;
8772 }
8773
Zhu Yi5a66926a2008-01-14 17:46:18 -08008774 /* nic init */
8775 iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
8776 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8777
8778 iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8779 err = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
8780 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8781 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8782 if (err < 0) {
8783 IWL_DEBUG_INFO("Failed to init the card\n");
8784 goto out_remove_sysfs;
8785 }
8786 /* Read the EEPROM */
8787 err = iwl4965_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008788 if (err) {
Zhu Yi5a66926a2008-01-14 17:46:18 -08008789 IWL_ERROR("Unable to init EEPROM\n");
8790 goto out_remove_sysfs;
8791 }
8792 /* MAC Address location in EEPROM same for 3945/4965 */
8793 get_eeprom_mac(priv, priv->mac_addr);
8794 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8795 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8796
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008797 err = iwl4965_init_channel_map(priv);
8798 if (err) {
8799 IWL_ERROR("initializing regulatory failed: %d\n", err);
8800 goto out_remove_sysfs;
8801 }
8802
8803 err = iwl4965_init_geos(priv);
8804 if (err) {
8805 IWL_ERROR("initializing geos failed: %d\n", err);
8806 goto out_free_channel_map;
8807 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008808
Zhu Yi5a66926a2008-01-14 17:46:18 -08008809 iwl4965_rate_control_register(priv->hw);
8810 err = ieee80211_register_hw(priv->hw);
8811 if (err) {
8812 IWL_ERROR("Failed to register network device (error %d)\n", err);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008813 goto out_free_geos;
Zhu Yib481de92007-09-25 17:54:57 -07008814 }
8815
Zhu Yi5a66926a2008-01-14 17:46:18 -08008816 priv->hw->conf.beacon_int = 100;
8817 priv->mac80211_registered = 1;
8818 pci_save_state(pdev);
8819 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008820
8821 return 0;
8822
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008823 out_free_geos:
8824 iwl4965_free_geos(priv);
8825 out_free_channel_map:
8826 iwl4965_free_channel_map(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08008827 out_remove_sysfs:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008828 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008829
8830 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07008831 destroy_workqueue(priv->workqueue);
8832 priv->workqueue = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008833 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008834
8835 out_iounmap:
8836 pci_iounmap(pdev, priv->hw_base);
8837 out_pci_release_regions:
8838 pci_release_regions(pdev);
8839 out_pci_disable_device:
8840 pci_disable_device(pdev);
8841 pci_set_drvdata(pdev, NULL);
8842 out_ieee80211_free_hw:
8843 ieee80211_free_hw(priv->hw);
8844 out:
8845 return err;
8846}
8847
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008848static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008849{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008850 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008851 struct list_head *p, *q;
8852 int i;
8853
8854 if (!priv)
8855 return;
8856
8857 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8858
Zhu Yib481de92007-09-25 17:54:57 -07008859 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008860
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008861 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008862
8863 /* Free MAC hash list for ADHOC */
8864 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8865 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8866 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008867 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008868 }
8869 }
8870
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008871 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008872
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008873 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008874
8875 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008876 iwl4965_rx_queue_free(priv, &priv->rxq);
8877 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008878
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008879 iwl4965_unset_hw_setting(priv);
8880 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008881
8882 if (priv->mac80211_registered) {
8883 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008884 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008885 }
8886
Mohamed Abbas948c1712007-10-25 17:15:45 +08008887 /*netif_stop_queue(dev); */
8888 flush_workqueue(priv->workqueue);
8889
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008890 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008891 * priv->workqueue... so we can't take down the workqueue
8892 * until now... */
8893 destroy_workqueue(priv->workqueue);
8894 priv->workqueue = NULL;
8895
Zhu Yib481de92007-09-25 17:54:57 -07008896 pci_iounmap(pdev, priv->hw_base);
8897 pci_release_regions(pdev);
8898 pci_disable_device(pdev);
8899 pci_set_drvdata(pdev, NULL);
8900
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008901 iwl4965_free_channel_map(priv);
8902 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008903
8904 if (priv->ibss_beacon)
8905 dev_kfree_skb(priv->ibss_beacon);
8906
8907 ieee80211_free_hw(priv->hw);
8908}
8909
8910#ifdef CONFIG_PM
8911
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008912static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008913{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008914 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008915
Zhu Yie655b9f2008-01-24 02:19:38 -08008916 if (priv->is_open) {
8917 set_bit(STATUS_IN_SUSPEND, &priv->status);
8918 iwl4965_mac_stop(priv->hw);
8919 priv->is_open = 1;
8920 }
Zhu Yib481de92007-09-25 17:54:57 -07008921
Zhu Yib481de92007-09-25 17:54:57 -07008922 pci_set_power_state(pdev, PCI_D3hot);
8923
Zhu Yib481de92007-09-25 17:54:57 -07008924 return 0;
8925}
8926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008927static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008928{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008929 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008930
Zhu Yib481de92007-09-25 17:54:57 -07008931 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008932
Zhu Yie655b9f2008-01-24 02:19:38 -08008933 if (priv->is_open)
8934 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008935
Zhu Yie655b9f2008-01-24 02:19:38 -08008936 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008937 return 0;
8938}
8939
8940#endif /* CONFIG_PM */
8941
8942/*****************************************************************************
8943 *
8944 * driver and module entry point
8945 *
8946 *****************************************************************************/
8947
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008948static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008949 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008950 .id_table = iwl4965_hw_card_ids,
8951 .probe = iwl4965_pci_probe,
8952 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008953#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008954 .suspend = iwl4965_pci_suspend,
8955 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008956#endif
8957};
8958
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008959static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008960{
8961
8962 int ret;
8963 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8964 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008965 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008966 if (ret) {
8967 IWL_ERROR("Unable to initialize PCI module\n");
8968 return ret;
8969 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008970#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008971 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008972 if (ret) {
8973 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008974 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008975 return ret;
8976 }
8977#endif
8978
8979 return ret;
8980}
8981
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008982static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008983{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008984#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008985 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008986#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008987 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008988}
8989
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008990module_param_named(antenna, iwl4965_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008991MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008992module_param_named(disable, iwl4965_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008993MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008994module_param_named(hwcrypto, iwl4965_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008995MODULE_PARM_DESC(hwcrypto,
8996 "using hardware crypto engine (default 0 [software])\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008997module_param_named(debug, iwl4965_param_debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008998MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008999module_param_named(disable_hw_scan, iwl4965_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009000MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
9001
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009002module_param_named(queues_num, iwl4965_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009003MODULE_PARM_DESC(queues_num, "number of hw queues.");
9004
9005/* QoS */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009006module_param_named(qos_enable, iwl4965_param_qos_enable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009007MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02009008module_param_named(amsdu_size_8K, iwl4965_param_amsdu_size_8K, int, 0444);
9009MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Zhu Yib481de92007-09-25 17:54:57 -07009010
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009011module_exit(iwl4965_exit);
9012module_init(iwl4965_init);