blob: 5005c9513007671aa5dcfe9dd0d3cb379d990dc5 [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 Hellwigc8b0e6e2007-10-25 17:15:51 +080052#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080053u32 iwl4965_debug_level;
Zhu Yib481de92007-09-25 17:54:57 -070054#endif
55
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080056static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
57 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080058
Zhu Yib481de92007-09-25 17:54:57 -070059/******************************************************************************
60 *
61 * module boiler plate
62 *
63 ******************************************************************************/
64
65/* module parameters */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080066static int iwl4965_param_disable_hw_scan; /* def: 0 = use 4965's h/w scan */
67static int iwl4965_param_debug; /* def: 0 = minimal debug log messages */
Ben Cahill9fbab512007-11-29 11:09:47 +080068static int iwl4965_param_disable; /* def: enable radio */
69static int iwl4965_param_antenna; /* def: 0 = both antennas (use diversity) */
70int iwl4965_param_hwcrypto; /* def: using software encryption */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080071static int iwl4965_param_qos_enable = 1; /* def: 1 = use quality of service */
72int iwl4965_param_queues_num = IWL_MAX_NUM_QUEUES; /* def: 16 Tx queues */
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +020073int iwl4965_param_amsdu_size_8K; /* def: enable 8K amsdu size */
Zhu Yib481de92007-09-25 17:54:57 -070074
75/*
76 * module name, copyright, version, etc.
77 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
78 */
79
80#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
81
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080082#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070083#define VD "d"
84#else
85#define VD
86#endif
87
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080088#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070089#define VS "s"
90#else
91#define VS
92#endif
93
Reinette Chatreb9e0b442008-02-08 16:39:11 -080094#define IWLWIFI_VERSION "1.2.26k" VD VS
Zhu Yib481de92007-09-25 17:54:57 -070095#define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
96#define DRV_VERSION IWLWIFI_VERSION
97
Zhu Yib481de92007-09-25 17:54:57 -070098
99MODULE_DESCRIPTION(DRV_DESCRIPTION);
100MODULE_VERSION(DRV_VERSION);
101MODULE_AUTHOR(DRV_COPYRIGHT);
102MODULE_LICENSE("GPL");
103
104__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
105{
106 u16 fc = le16_to_cpu(hdr->frame_control);
107 int hdr_len = ieee80211_get_hdrlen(fc);
108
109 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
110 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
111 return NULL;
112}
113
Johannes Berg8318d782008-01-24 19:38:38 +0100114static const struct ieee80211_supported_band *iwl4965_get_hw_mode(
115 struct iwl4965_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700116{
Johannes Berg8318d782008-01-24 19:38:38 +0100117 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700118}
119
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800120static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700121{
122 /* Single white space is for Linksys APs */
123 if (essid_len == 1 && essid[0] == ' ')
124 return 1;
125
126 /* Otherwise, if the entire essid is 0, we assume it is hidden */
127 while (essid_len) {
128 essid_len--;
129 if (essid[essid_len] != '\0')
130 return 0;
131 }
132
133 return 1;
134}
135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800136static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700137{
138 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
139 const char *s = essid;
140 char *d = escaped;
141
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800142 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700143 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
144 return escaped;
145 }
146
147 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
148 while (essid_len--) {
149 if (*s == '\0') {
150 *d++ = '\\';
151 *d++ = '0';
152 s++;
153 } else
154 *d++ = *s++;
155 }
156 *d = '\0';
157 return escaped;
158}
159
Zhu Yib481de92007-09-25 17:54:57 -0700160/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
161 * DMA services
162 *
163 * Theory of operation
164 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800165 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
166 * of buffer descriptors, each of which points to one or more data buffers for
167 * the device to read from or fill. Driver and device exchange status of each
168 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
169 * entries in each circular buffer, to protect against confusing empty and full
170 * queue states.
171 *
172 * The device reads or writes the data in the queues via the device's several
173 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700174 *
175 * For Tx queue, there are low mark and high mark limits. If, after queuing
176 * the packet for Tx, free space become < low mark, Tx queue stopped. When
177 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
178 * Tx queue resumed.
179 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800180 * The 4965 operates with up to 17 queues: One receive queue, one transmit
181 * queue (#4) for sending commands to the device firmware, and 15 other
182 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800183 *
184 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700185 ***************************************************/
186
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200187int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700188{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800189 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700190
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800191 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700192 s -= q->n_bd;
193
194 if (s <= 0)
195 s += q->n_window;
196 /* keep some reserve to not confuse empty and full situations */
197 s -= 2;
198 if (s < 0)
199 s = 0;
200 return s;
201}
202
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800203/**
204 * iwl4965_queue_inc_wrap - increment queue index, wrap back to beginning
205 * @index -- current index
206 * @n_bd -- total number of entries in queue (must be power of 2)
207 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800208static inline int iwl4965_queue_inc_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700209{
210 return ++index & (n_bd - 1);
211}
212
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800213/**
214 * iwl4965_queue_dec_wrap - decrement queue index, wrap back to end
215 * @index -- current index
216 * @n_bd -- total number of entries in queue (must be power of 2)
217 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800218static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700219{
220 return --index & (n_bd - 1);
221}
222
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800223static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700224{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800225 return q->write_ptr > q->read_ptr ?
226 (i >= q->read_ptr && i < q->write_ptr) :
227 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700228}
229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800230static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700231{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800232 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700233 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800234 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700235
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800236 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700237 return index & (q->n_window - 1);
238}
239
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800240/**
241 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
242 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800243static int iwl4965_queue_init(struct iwl4965_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700244 int count, int slots_num, u32 id)
245{
246 q->n_bd = count;
247 q->n_window = slots_num;
248 q->id = id;
249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800250 /* count must be power-of-two size, otherwise iwl4965_queue_inc_wrap
251 * and iwl4965_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700252 BUG_ON(!is_power_of_2(count));
253
254 /* slots_num must be power-of-two size, otherwise
255 * get_cmd_index is broken. */
256 BUG_ON(!is_power_of_2(slots_num));
257
258 q->low_mark = q->n_window / 4;
259 if (q->low_mark < 4)
260 q->low_mark = 4;
261
262 q->high_mark = q->n_window / 8;
263 if (q->high_mark < 2)
264 q->high_mark = 2;
265
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800266 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700267
268 return 0;
269}
270
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800271/**
272 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
273 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800274static int iwl4965_tx_queue_alloc(struct iwl4965_priv *priv,
275 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700276{
277 struct pci_dev *dev = priv->pci_dev;
278
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800279 /* Driver private data, only for Tx (not command) queues,
280 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700281 if (id != IWL_CMD_QUEUE_NUM) {
282 txq->txb = kmalloc(sizeof(txq->txb[0]) *
283 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
284 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800285 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700286 "structures failed\n");
287 goto error;
288 }
289 } else
290 txq->txb = NULL;
291
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800292 /* Circular buffer of transmit frame descriptors (TFDs),
293 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700294 txq->bd = pci_alloc_consistent(dev,
295 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
296 &txq->q.dma_addr);
297
298 if (!txq->bd) {
299 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
300 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
301 goto error;
302 }
303 txq->q.id = id;
304
305 return 0;
306
307 error:
308 if (txq->txb) {
309 kfree(txq->txb);
310 txq->txb = NULL;
311 }
312
313 return -ENOMEM;
314}
315
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800316/**
317 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
318 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800319int iwl4965_tx_queue_init(struct iwl4965_priv *priv,
320 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700321{
322 struct pci_dev *dev = priv->pci_dev;
323 int len;
324 int rc = 0;
325
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800326 /*
327 * Alloc buffer array for commands (Tx or other types of commands).
328 * For the command queue (#4), allocate command space + one big
329 * command for scan, since scan command is very huge; the system will
330 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200331 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800332 * space is needed.
333 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800334 len = sizeof(struct iwl4965_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700335 if (txq_id == IWL_CMD_QUEUE_NUM)
336 len += IWL_MAX_SCAN_SIZE;
337 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
338 if (!txq->cmd)
339 return -ENOMEM;
340
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800341 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800342 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700343 if (rc) {
344 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
345
346 return -ENOMEM;
347 }
348 txq->need_update = 0;
349
350 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800351 * iwl4965_queue_inc_wrap and iwl4965_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700352 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800353
354 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800355 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700356
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800357 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800358 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700359
360 return 0;
361}
362
363/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800364 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700365 * @txq: Transmit queue to deallocate.
366 *
367 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800368 * Free all buffers.
369 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700370 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800371void iwl4965_tx_queue_free(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700372{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800373 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700374 struct pci_dev *dev = priv->pci_dev;
375 int len;
376
377 if (q->n_bd == 0)
378 return;
379
380 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800381 for (; q->write_ptr != q->read_ptr;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800382 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd))
383 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700384
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800385 len = sizeof(struct iwl4965_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700386 if (q->id == IWL_CMD_QUEUE_NUM)
387 len += IWL_MAX_SCAN_SIZE;
388
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800389 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700390 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
391
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800392 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700393 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800394 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700395 txq->q.n_bd, txq->bd, txq->q.dma_addr);
396
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800397 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700398 if (txq->txb) {
399 kfree(txq->txb);
400 txq->txb = NULL;
401 }
402
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800403 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700404 memset(txq, 0, sizeof(*txq));
405}
406
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800407const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700408
409/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800410 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700411 * the functionality provided here
412 */
413
414/**************************************************************/
415
Ian Schram01ebd062007-10-25 17:15:22 +0800416#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800417/**
418 * iwl4965_remove_station - Remove driver's knowledge of station.
419 *
420 * NOTE: This does not remove station from device's station table.
421 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800422static u8 iwl4965_remove_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700423{
424 int index = IWL_INVALID_STATION;
425 int i;
426 unsigned long flags;
427
428 spin_lock_irqsave(&priv->sta_lock, flags);
429
430 if (is_ap)
431 index = IWL_AP_ID;
432 else if (is_broadcast_ether_addr(addr))
433 index = priv->hw_setting.bcast_sta_id;
434 else
435 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
436 if (priv->stations[i].used &&
437 !compare_ether_addr(priv->stations[i].sta.sta.addr,
438 addr)) {
439 index = i;
440 break;
441 }
442
443 if (unlikely(index == IWL_INVALID_STATION))
444 goto out;
445
446 if (priv->stations[index].used) {
447 priv->stations[index].used = 0;
448 priv->num_stations--;
449 }
450
451 BUG_ON(priv->num_stations < 0);
452
453out:
454 spin_unlock_irqrestore(&priv->sta_lock, flags);
455 return 0;
456}
Zhu Yi556f8db2007-09-27 11:27:33 +0800457#endif
Zhu Yib481de92007-09-25 17:54:57 -0700458
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800459/**
460 * iwl4965_clear_stations_table - Clear the driver's station table
461 *
462 * NOTE: This does not clear or otherwise alter the device's station table.
463 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800464static void iwl4965_clear_stations_table(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700465{
466 unsigned long flags;
467
468 spin_lock_irqsave(&priv->sta_lock, flags);
469
470 priv->num_stations = 0;
471 memset(priv->stations, 0, sizeof(priv->stations));
472
473 spin_unlock_irqrestore(&priv->sta_lock, flags);
474}
475
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800476/**
477 * iwl4965_add_station_flags - Add station to tables in driver and device
478 */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200479u8 iwl4965_add_station_flags(struct iwl4965_priv *priv, const u8 *addr,
480 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700481{
482 int i;
483 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800484 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700485 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700486 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700487
488 spin_lock_irqsave(&priv->sta_lock, flags_spin);
489 if (is_ap)
490 index = IWL_AP_ID;
491 else if (is_broadcast_ether_addr(addr))
492 index = priv->hw_setting.bcast_sta_id;
493 else
494 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
495 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
496 addr)) {
497 index = i;
498 break;
499 }
500
501 if (!priv->stations[i].used &&
502 index == IWL_INVALID_STATION)
503 index = i;
504 }
505
506
Ben Cahill9fbab512007-11-29 11:09:47 +0800507 /* These two conditions have the same outcome, but keep them separate
508 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700509 if (unlikely(index == IWL_INVALID_STATION)) {
510 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
511 return index;
512 }
513
514 if (priv->stations[index].used &&
515 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
516 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
517 return index;
518 }
519
520
Joe Perches0795af52007-10-03 17:59:30 -0700521 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700522 station = &priv->stations[index];
523 station->used = 1;
524 priv->num_stations++;
525
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800526 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800527 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700528 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
529 station->sta.mode = 0;
530 station->sta.sta.sta_id = index;
531 station->sta.station_flags = 0;
532
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800533#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700534 /* BCAST station and IBSS stations do not work in HT mode */
535 if (index != priv->hw_setting.bcast_sta_id &&
536 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200537 iwl4965_set_ht_add_station(priv, index,
538 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800539#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700540
541 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800542
543 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800544 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700545 return index;
546
547}
548
549/*************** DRIVER STATUS FUNCTIONS *****/
550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800551static inline int iwl4965_is_ready(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700552{
553 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
554 * set but EXIT_PENDING is not */
555 return test_bit(STATUS_READY, &priv->status) &&
556 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
557 !test_bit(STATUS_EXIT_PENDING, &priv->status);
558}
559
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800560static inline int iwl4965_is_alive(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700561{
562 return test_bit(STATUS_ALIVE, &priv->status);
563}
564
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800565static inline int iwl4965_is_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700566{
567 return test_bit(STATUS_INIT, &priv->status);
568}
569
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800570static inline int iwl4965_is_rfkill(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700571{
572 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
573 test_bit(STATUS_RF_KILL_SW, &priv->status);
574}
575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800576static inline int iwl4965_is_ready_rf(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700577{
578
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800579 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700580 return 0;
581
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800582 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700583}
584
585/*************** HOST COMMAND QUEUE FUNCTIONS *****/
586
587#define IWL_CMD(x) case x : return #x
588
589static const char *get_cmd_string(u8 cmd)
590{
591 switch (cmd) {
592 IWL_CMD(REPLY_ALIVE);
593 IWL_CMD(REPLY_ERROR);
594 IWL_CMD(REPLY_RXON);
595 IWL_CMD(REPLY_RXON_ASSOC);
596 IWL_CMD(REPLY_QOS_PARAM);
597 IWL_CMD(REPLY_RXON_TIMING);
598 IWL_CMD(REPLY_ADD_STA);
599 IWL_CMD(REPLY_REMOVE_STA);
600 IWL_CMD(REPLY_REMOVE_ALL_STA);
601 IWL_CMD(REPLY_TX);
602 IWL_CMD(REPLY_RATE_SCALE);
603 IWL_CMD(REPLY_LEDS_CMD);
604 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
605 IWL_CMD(RADAR_NOTIFICATION);
606 IWL_CMD(REPLY_QUIET_CMD);
607 IWL_CMD(REPLY_CHANNEL_SWITCH);
608 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
609 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
610 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
611 IWL_CMD(POWER_TABLE_CMD);
612 IWL_CMD(PM_SLEEP_NOTIFICATION);
613 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
614 IWL_CMD(REPLY_SCAN_CMD);
615 IWL_CMD(REPLY_SCAN_ABORT_CMD);
616 IWL_CMD(SCAN_START_NOTIFICATION);
617 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
618 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
619 IWL_CMD(BEACON_NOTIFICATION);
620 IWL_CMD(REPLY_TX_BEACON);
621 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
622 IWL_CMD(QUIET_NOTIFICATION);
623 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
624 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
625 IWL_CMD(REPLY_BT_CONFIG);
626 IWL_CMD(REPLY_STATISTICS_CMD);
627 IWL_CMD(STATISTICS_NOTIFICATION);
628 IWL_CMD(REPLY_CARD_STATE_CMD);
629 IWL_CMD(CARD_STATE_NOTIFICATION);
630 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
631 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
632 IWL_CMD(SENSITIVITY_CMD);
633 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
634 IWL_CMD(REPLY_RX_PHY_CMD);
635 IWL_CMD(REPLY_RX_MPDU_CMD);
636 IWL_CMD(REPLY_4965_RX);
637 IWL_CMD(REPLY_COMPRESSED_BA);
638 default:
639 return "UNKNOWN";
640
641 }
642}
643
644#define HOST_COMPLETE_TIMEOUT (HZ / 2)
645
646/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800647 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700648 * @priv: device private data point
649 * @cmd: a point to the ucode command structure
650 *
651 * The function returns < 0 values to indicate the operation is
652 * failed. On success, it turns the index (> 0) of command in the
653 * command queue.
654 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800655static int iwl4965_enqueue_hcmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700656{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800657 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
658 struct iwl4965_queue *q = &txq->q;
659 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700660 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800661 struct iwl4965_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700662 u32 idx;
663 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
664 dma_addr_t phys_addr;
665 int ret;
666 unsigned long flags;
667
668 /* If any of the command structures end up being larger than
669 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
670 * we will need to increase the size of the TFD entries */
671 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
672 !(cmd->meta.flags & CMD_SIZE_HUGE));
673
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800674 if (iwl4965_is_rfkill(priv)) {
675 IWL_DEBUG_INFO("Not sending command - RF KILL");
676 return -EIO;
677 }
678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800679 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700680 IWL_ERROR("No space for Tx\n");
681 return -ENOSPC;
682 }
683
684 spin_lock_irqsave(&priv->hcmd_lock, flags);
685
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800686 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700687 memset(tfd, 0, sizeof(*tfd));
688
689 control_flags = (u32 *) tfd;
690
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800691 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700692 out_cmd = &txq->cmd[idx];
693
694 out_cmd->hdr.cmd = cmd->id;
695 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
696 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
697
698 /* At this point, the out_cmd now has all of the incoming cmd
699 * information */
700
701 out_cmd->hdr.flags = 0;
702 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800703 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700704 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
705 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
706
707 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800708 offsetof(struct iwl4965_cmd, hdr);
709 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700710
711 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
712 "%d bytes at %d[%d]:%d\n",
713 get_cmd_string(out_cmd->hdr.cmd),
714 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800715 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700716
717 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800718
719 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700720 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800721
722 /* Increment and update queue's write index */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800723 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
724 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700725
726 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
727 return ret ? ret : idx;
728}
729
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800730static int iwl4965_send_cmd_async(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700731{
732 int ret;
733
734 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
735
736 /* An asynchronous command can not expect an SKB to be set. */
737 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
738
739 /* An asynchronous command MUST have a callback. */
740 BUG_ON(!cmd->meta.u.callback);
741
742 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
743 return -EBUSY;
744
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800745 ret = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700746 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800747 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700748 get_cmd_string(cmd->id), ret);
749 return ret;
750 }
751 return 0;
752}
753
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800754static int iwl4965_send_cmd_sync(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700755{
756 int cmd_idx;
757 int ret;
758 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
759
760 BUG_ON(cmd->meta.flags & CMD_ASYNC);
761
762 /* A synchronous command can not have a callback set. */
763 BUG_ON(cmd->meta.u.callback != NULL);
764
765 if (atomic_xchg(&entry, 1)) {
766 IWL_ERROR("Error sending %s: Already sending a host command\n",
767 get_cmd_string(cmd->id));
768 return -EBUSY;
769 }
770
771 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
772
773 if (cmd->meta.flags & CMD_WANT_SKB)
774 cmd->meta.source = &cmd->meta;
775
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800776 cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700777 if (cmd_idx < 0) {
778 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800779 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700780 get_cmd_string(cmd->id), ret);
781 goto out;
782 }
783
784 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
785 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
786 HOST_COMPLETE_TIMEOUT);
787 if (!ret) {
788 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
789 IWL_ERROR("Error sending %s: time out after %dms.\n",
790 get_cmd_string(cmd->id),
791 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
792
793 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
794 ret = -ETIMEDOUT;
795 goto cancel;
796 }
797 }
798
799 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
800 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
801 get_cmd_string(cmd->id));
802 ret = -ECANCELED;
803 goto fail;
804 }
805 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
806 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
807 get_cmd_string(cmd->id));
808 ret = -EIO;
809 goto fail;
810 }
811 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
812 IWL_ERROR("Error: Response NULL in '%s'\n",
813 get_cmd_string(cmd->id));
814 ret = -EIO;
815 goto out;
816 }
817
818 ret = 0;
819 goto out;
820
821cancel:
822 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823 struct iwl4965_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700824
825 /* Cancel the CMD_WANT_SKB flag for the cmd in the
826 * TX cmd queue. Otherwise in case the cmd comes
827 * in later, it will possibly set an invalid
828 * address (cmd->meta.source). */
829 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
830 qcmd->meta.flags &= ~CMD_WANT_SKB;
831 }
832fail:
833 if (cmd->meta.u.skb) {
834 dev_kfree_skb_any(cmd->meta.u.skb);
835 cmd->meta.u.skb = NULL;
836 }
837out:
838 atomic_set(&entry, 0);
839 return ret;
840}
841
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800842int iwl4965_send_cmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700843{
Zhu Yib481de92007-09-25 17:54:57 -0700844 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800845 return iwl4965_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700846
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800847 return iwl4965_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700848}
849
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800850int iwl4965_send_cmd_pdu(struct iwl4965_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700851{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800852 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700853 .id = id,
854 .len = len,
855 .data = data,
856 };
857
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800858 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700859}
860
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800861static int __must_check iwl4965_send_cmd_u32(struct iwl4965_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700862{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800863 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700864 .id = id,
865 .len = sizeof(val),
866 .data = &val,
867 };
868
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800869 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700870}
871
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800872int iwl4965_send_statistics_request(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700873{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800874 return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700875}
876
877/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800878 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700879 *
880 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800881 * NOTE: mutex must be held before calling this fnction
882 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800883static int iwl4965_rxon_add_station(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700884 const u8 *addr, int is_ap)
885{
Zhu Yi556f8db2007-09-27 11:27:33 +0800886 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700887
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800888 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200889#ifdef CONFIG_IWL4965_HT
890 struct ieee80211_conf *conf = &priv->hw->conf;
891 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
892
893 if ((is_ap) &&
894 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
895 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
896 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
897 0, cur_ht_config);
898 else
899#endif /* CONFIG_IWL4965_HT */
900 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
901 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800902
903 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700904 iwl4965_add_station(priv, addr, is_ap);
905
Zhu Yi556f8db2007-09-27 11:27:33 +0800906 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700907}
908
909/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800910 * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
Zhu Yib481de92007-09-25 17:54:57 -0700911 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
912 * @channel: Any channel valid for the requested phymode
913
914 * In addition to setting the staging RXON, priv->phymode is also set.
915 *
916 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
917 * in the staging RXON flag structure based on the phymode
918 */
Johannes Berg8318d782008-01-24 19:38:38 +0100919static int iwl4965_set_rxon_channel(struct iwl4965_priv *priv,
920 enum ieee80211_band band,
Ben Cahill9fbab512007-11-29 11:09:47 +0800921 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700922{
Johannes Berg8318d782008-01-24 19:38:38 +0100923 if (!iwl4965_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700924 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100925 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700926 return -EINVAL;
927 }
928
929 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100930 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700931 return 0;
932
933 priv->staging_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100934 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700935 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
936 else
937 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
938
Johannes Berg8318d782008-01-24 19:38:38 +0100939 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700940
Johannes Berg8318d782008-01-24 19:38:38 +0100941 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700942
943 return 0;
944}
945
946/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800947 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700948 *
949 * NOTE: This is really only useful during development and can eventually
950 * be #ifdef'd out once the driver is stable and folks aren't actively
951 * making changes
952 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800953static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700954{
955 int error = 0;
956 int counter = 1;
957
958 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
959 error |= le32_to_cpu(rxon->flags &
960 (RXON_FLG_TGJ_NARROW_BAND_MSK |
961 RXON_FLG_RADAR_DETECT_MSK));
962 if (error)
963 IWL_WARNING("check 24G fields %d | %d\n",
964 counter++, error);
965 } else {
966 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
967 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
968 if (error)
969 IWL_WARNING("check 52 fields %d | %d\n",
970 counter++, error);
971 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
972 if (error)
973 IWL_WARNING("check 52 CCK %d | %d\n",
974 counter++, error);
975 }
976 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
977 if (error)
978 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
979
980 /* make sure basic rates 6Mbps and 1Mbps are supported */
981 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
982 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
983 if (error)
984 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
985
986 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
987 if (error)
988 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
989
990 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
991 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
992 if (error)
993 IWL_WARNING("check CCK and short slot %d | %d\n",
994 counter++, error);
995
996 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
997 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
998 if (error)
999 IWL_WARNING("check CCK & auto detect %d | %d\n",
1000 counter++, error);
1001
1002 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
1003 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
1004 if (error)
1005 IWL_WARNING("check TGG and auto detect %d | %d\n",
1006 counter++, error);
1007
1008 if (error)
1009 IWL_WARNING("Tuning to channel %d\n",
1010 le16_to_cpu(rxon->channel));
1011
1012 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001013 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -07001014 return -1;
1015 }
1016 return 0;
1017}
1018
1019/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001020 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +08001021 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -07001022 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001023 * If the RXON structure is changing enough to require a new tune,
1024 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
1025 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -07001026 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001027static int iwl4965_full_rxon_required(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001028{
1029
1030 /* These items are only settable from the full RXON command */
1031 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
1032 compare_ether_addr(priv->staging_rxon.bssid_addr,
1033 priv->active_rxon.bssid_addr) ||
1034 compare_ether_addr(priv->staging_rxon.node_addr,
1035 priv->active_rxon.node_addr) ||
1036 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
1037 priv->active_rxon.wlap_bssid_addr) ||
1038 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
1039 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
1040 (priv->staging_rxon.air_propagation !=
1041 priv->active_rxon.air_propagation) ||
1042 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
1043 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
1044 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
1045 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
1046 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
1047 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1048 return 1;
1049
1050 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1051 * be updated with the RXON_ASSOC command -- however only some
1052 * flag transitions are allowed using RXON_ASSOC */
1053
1054 /* Check if we are not switching bands */
1055 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1056 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1057 return 1;
1058
1059 /* Check if we are switching association toggle */
1060 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1061 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1062 return 1;
1063
1064 return 0;
1065}
1066
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001067static int iwl4965_send_rxon_assoc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001068{
1069 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001070 struct iwl4965_rx_packet *res = NULL;
1071 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1072 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001073 .id = REPLY_RXON_ASSOC,
1074 .len = sizeof(rxon_assoc),
1075 .meta.flags = CMD_WANT_SKB,
1076 .data = &rxon_assoc,
1077 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001078 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1079 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001080
1081 if ((rxon1->flags == rxon2->flags) &&
1082 (rxon1->filter_flags == rxon2->filter_flags) &&
1083 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1084 (rxon1->ofdm_ht_single_stream_basic_rates ==
1085 rxon2->ofdm_ht_single_stream_basic_rates) &&
1086 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1087 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1088 (rxon1->rx_chain == rxon2->rx_chain) &&
1089 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1090 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1091 return 0;
1092 }
1093
1094 rxon_assoc.flags = priv->staging_rxon.flags;
1095 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1096 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1097 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1098 rxon_assoc.reserved = 0;
1099 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1100 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1101 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1102 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1103 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1104
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001105 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001106 if (rc)
1107 return rc;
1108
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001109 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001110 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1111 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1112 rc = -EIO;
1113 }
1114
1115 priv->alloc_rxb_skb--;
1116 dev_kfree_skb_any(cmd.meta.u.skb);
1117
1118 return rc;
1119}
1120
1121/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001122 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001123 *
Ian Schram01ebd062007-10-25 17:15:22 +08001124 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001125 * the active_rxon structure is updated with the new data. This
1126 * function correctly transitions out of the RXON_ASSOC_MSK state if
1127 * a HW tune is required based on the RXON structure changes.
1128 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001129static int iwl4965_commit_rxon(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001130{
1131 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001132 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001133 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001134 int rc = 0;
1135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001136 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001137 return -1;
1138
1139 /* always get timestamp with Rx frame */
1140 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1141
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001142 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001143 if (rc) {
1144 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1145 return -EINVAL;
1146 }
1147
1148 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001149 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001150 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001151 if (!iwl4965_full_rxon_required(priv)) {
1152 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001153 if (rc) {
1154 IWL_ERROR("Error setting RXON_ASSOC "
1155 "configuration (%d).\n", rc);
1156 return rc;
1157 }
1158
1159 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1160
1161 return 0;
1162 }
1163
1164 /* station table will be cleared */
1165 priv->assoc_station_added = 0;
1166
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001167#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001168 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1169 if (!priv->error_recovering)
1170 priv->start_calib = 0;
1171
1172 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001173#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001174
1175 /* If we are currently associated and the new config requires
1176 * an RXON_ASSOC and the new config wants the associated mask enabled,
1177 * we must clear the associated from the active configuration
1178 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001179 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001180 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1181 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1182 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1183
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001184 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1185 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001186 &priv->active_rxon);
1187
1188 /* If the mask clearing failed then we set
1189 * active_rxon back to what it was previously */
1190 if (rc) {
1191 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1192 IWL_ERROR("Error clearing ASSOC_MSK on current "
1193 "configuration (%d).\n", rc);
1194 return rc;
1195 }
Zhu Yib481de92007-09-25 17:54:57 -07001196 }
1197
1198 IWL_DEBUG_INFO("Sending RXON\n"
1199 "* with%s RXON_FILTER_ASSOC_MSK\n"
1200 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001201 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001202 ((priv->staging_rxon.filter_flags &
1203 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1204 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001205 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001206
1207 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001208 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1209 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001210 if (rc) {
1211 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1212 return rc;
1213 }
1214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001215 iwl4965_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001216
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001217#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001218 if (!priv->error_recovering)
1219 priv->start_calib = 0;
1220
1221 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1222 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001223#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001224
1225 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1226
1227 /* If we issue a new RXON command which required a tune then we must
1228 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001229 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001230 if (rc) {
1231 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1232 return rc;
1233 }
1234
1235 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001236 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001237 IWL_INVALID_STATION) {
1238 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1239 return -EIO;
1240 }
1241
1242 /* If we have set the ASSOC_MSK and we are in BSS mode then
1243 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001244 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001245 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001246 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -07001247 == IWL_INVALID_STATION) {
1248 IWL_ERROR("Error adding AP address for transmit.\n");
1249 return -EIO;
1250 }
1251 priv->assoc_station_added = 1;
1252 }
1253
1254 return 0;
1255}
1256
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001257static int iwl4965_send_bt_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001258{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001259 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001260 .flags = 3,
1261 .lead_time = 0xAA,
1262 .max_kill = 1,
1263 .kill_ack_mask = 0,
1264 .kill_cts_mask = 0,
1265 };
1266
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001267 return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1268 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001269}
1270
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001271static int iwl4965_send_scan_abort(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001272{
1273 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001274 struct iwl4965_rx_packet *res;
1275 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001276 .id = REPLY_SCAN_ABORT_CMD,
1277 .meta.flags = CMD_WANT_SKB,
1278 };
1279
1280 /* If there isn't a scan actively going on in the hardware
1281 * then we are in between scan bands and not actually
1282 * actively scanning, so don't send the abort command */
1283 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1284 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1285 return 0;
1286 }
1287
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001288 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001289 if (rc) {
1290 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1291 return rc;
1292 }
1293
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001294 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001295 if (res->u.status != CAN_ABORT_STATUS) {
1296 /* The scan abort will return 1 for success or
1297 * 2 for "failure". A failure condition can be
1298 * due to simply not being in an active scan which
1299 * can occur if we send the scan abort before we
1300 * the microcode has notified us that a scan is
1301 * completed. */
1302 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1303 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1304 clear_bit(STATUS_SCAN_HW, &priv->status);
1305 }
1306
1307 dev_kfree_skb_any(cmd.meta.u.skb);
1308
1309 return rc;
1310}
1311
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001312static int iwl4965_card_state_sync_callback(struct iwl4965_priv *priv,
1313 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001314 struct sk_buff *skb)
1315{
1316 return 1;
1317}
1318
1319/*
1320 * CARD_STATE_CMD
1321 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001322 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001323 *
1324 * When in the 'enable' state the card operates as normal.
1325 * When in the 'disable' state, the card enters into a low power mode.
1326 * When in the 'halt' state, the card is shut down and must be fully
1327 * restarted to come back on.
1328 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001329static int iwl4965_send_card_state(struct iwl4965_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001330{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001331 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001332 .id = REPLY_CARD_STATE_CMD,
1333 .len = sizeof(u32),
1334 .data = &flags,
1335 .meta.flags = meta_flag,
1336 };
1337
1338 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001339 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001340
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001341 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001342}
1343
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001344static int iwl4965_add_sta_sync_callback(struct iwl4965_priv *priv,
1345 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001346{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001347 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001348
1349 if (!skb) {
1350 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1351 return 1;
1352 }
1353
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001354 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001355 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1356 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1357 res->hdr.flags);
1358 return 1;
1359 }
1360
1361 switch (res->u.add_sta.status) {
1362 case ADD_STA_SUCCESS_MSK:
1363 break;
1364 default:
1365 break;
1366 }
1367
1368 /* We didn't cache the SKB; let the caller free it */
1369 return 1;
1370}
1371
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001372int iwl4965_send_add_station(struct iwl4965_priv *priv,
1373 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001374{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001375 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001376 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001377 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001378 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001379 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001380 .meta.flags = flags,
1381 .data = sta,
1382 };
1383
1384 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001385 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001386 else
1387 cmd.meta.flags |= CMD_WANT_SKB;
1388
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001389 rc = iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001390
1391 if (rc || (flags & CMD_ASYNC))
1392 return rc;
1393
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001394 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001395 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1396 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1397 res->hdr.flags);
1398 rc = -EIO;
1399 }
1400
1401 if (rc == 0) {
1402 switch (res->u.add_sta.status) {
1403 case ADD_STA_SUCCESS_MSK:
1404 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1405 break;
1406 default:
1407 rc = -EIO;
1408 IWL_WARNING("REPLY_ADD_STA failed\n");
1409 break;
1410 }
1411 }
1412
1413 priv->alloc_rxb_skb--;
1414 dev_kfree_skb_any(cmd.meta.u.skb);
1415
1416 return rc;
1417}
1418
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001419static int iwl4965_update_sta_key_info(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001420 struct ieee80211_key_conf *keyconf,
1421 u8 sta_id)
1422{
1423 unsigned long flags;
1424 __le16 key_flags = 0;
1425
1426 switch (keyconf->alg) {
1427 case ALG_CCMP:
1428 key_flags |= STA_KEY_FLG_CCMP;
1429 key_flags |= cpu_to_le16(
1430 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1431 key_flags &= ~STA_KEY_FLG_INVALID;
1432 break;
1433 case ALG_TKIP:
1434 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001435 default:
1436 return -EINVAL;
1437 }
1438 spin_lock_irqsave(&priv->sta_lock, flags);
1439 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1440 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1441 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1442 keyconf->keylen);
1443
1444 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1445 keyconf->keylen);
1446 priv->stations[sta_id].sta.key.key_flags = key_flags;
1447 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1448 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1449
1450 spin_unlock_irqrestore(&priv->sta_lock, flags);
1451
1452 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001453 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001454 return 0;
1455}
1456
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001457static int iwl4965_clear_sta_key_info(struct iwl4965_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001458{
1459 unsigned long flags;
1460
1461 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001462 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1463 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001464 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1465 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1466 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1467 spin_unlock_irqrestore(&priv->sta_lock, flags);
1468
1469 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001470 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001471 return 0;
1472}
1473
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001474static void iwl4965_clear_free_frames(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001475{
1476 struct list_head *element;
1477
1478 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1479 priv->frames_count);
1480
1481 while (!list_empty(&priv->free_frames)) {
1482 element = priv->free_frames.next;
1483 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001484 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001485 priv->frames_count--;
1486 }
1487
1488 if (priv->frames_count) {
1489 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1490 priv->frames_count);
1491 priv->frames_count = 0;
1492 }
1493}
1494
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001495static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001496{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001497 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001498 struct list_head *element;
1499 if (list_empty(&priv->free_frames)) {
1500 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1501 if (!frame) {
1502 IWL_ERROR("Could not allocate frame!\n");
1503 return NULL;
1504 }
1505
1506 priv->frames_count++;
1507 return frame;
1508 }
1509
1510 element = priv->free_frames.next;
1511 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001512 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001513}
1514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001515static void iwl4965_free_frame(struct iwl4965_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001516{
1517 memset(frame, 0, sizeof(*frame));
1518 list_add(&frame->list, &priv->free_frames);
1519}
1520
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001521unsigned int iwl4965_fill_beacon_frame(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001522 struct ieee80211_hdr *hdr,
1523 const u8 *dest, int left)
1524{
1525
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001526 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001527 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1528 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1529 return 0;
1530
1531 if (priv->ibss_beacon->len > left)
1532 return 0;
1533
1534 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1535
1536 return priv->ibss_beacon->len;
1537}
1538
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001539static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001540{
1541 u8 i;
1542
1543 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001544 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001545 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001546 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001547 }
1548
1549 return IWL_RATE_INVALID;
1550}
1551
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001552static int iwl4965_send_beacon_cmd(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001553{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001554 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001555 unsigned int frame_size;
1556 int rc;
1557 u8 rate;
1558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001559 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001560
1561 if (!frame) {
1562 IWL_ERROR("Could not obtain free frame buffer for beacon "
1563 "command.\n");
1564 return -ENOMEM;
1565 }
1566
1567 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001568 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001569 0xFF0);
1570 if (rate == IWL_INVALID_RATE)
1571 rate = IWL_RATE_6M_PLCP;
1572 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001573 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001574 if (rate == IWL_INVALID_RATE)
1575 rate = IWL_RATE_1M_PLCP;
1576 }
1577
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001578 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001579
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001580 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001581 &frame->u.cmd[0]);
1582
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001583 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001584
1585 return rc;
1586}
1587
1588/******************************************************************************
1589 *
1590 * EEPROM related functions
1591 *
1592 ******************************************************************************/
1593
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001594static void get_eeprom_mac(struct iwl4965_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001595{
1596 memcpy(mac, priv->eeprom.mac_address, 6);
1597}
1598
Reinette Chatre74a3a252008-01-23 10:15:19 -08001599static inline void iwl4965_eeprom_release_semaphore(struct iwl4965_priv *priv)
1600{
1601 iwl4965_clear_bit(priv, CSR_HW_IF_CONFIG_REG,
1602 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
1603}
1604
Zhu Yib481de92007-09-25 17:54:57 -07001605/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001606 * iwl4965_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001607 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001608 * Load the EEPROM contents from adapter into priv->eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001609 *
1610 * NOTE: This routine uses the non-debug IO access functions.
1611 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001612int iwl4965_eeprom_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001613{
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001614 u16 *e = (u16 *)&priv->eeprom;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001615 u32 gp = iwl4965_read32(priv, CSR_EEPROM_GP);
Zhu Yib481de92007-09-25 17:54:57 -07001616 u32 r;
1617 int sz = sizeof(priv->eeprom);
1618 int rc;
1619 int i;
1620 u16 addr;
1621
1622 /* The EEPROM structure has several padding buffers within it
1623 * and when adding new EEPROM maps is subject to programmer errors
1624 * which may be very difficult to identify without explicitly
1625 * checking the resulting size of the eeprom map. */
1626 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1627
1628 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1629 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1630 return -ENOENT;
1631 }
1632
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001633 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001634 rc = iwl4965_eeprom_acquire_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001635 if (rc < 0) {
Ian Schram91e17472007-10-25 17:15:23 +08001636 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001637 return -ENOENT;
1638 }
1639
1640 /* eeprom is an array of 16bit values */
1641 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001642 _iwl4965_write32(priv, CSR_EEPROM_REG, addr << 1);
1643 _iwl4965_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
Zhu Yib481de92007-09-25 17:54:57 -07001644
1645 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1646 i += IWL_EEPROM_ACCESS_DELAY) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001647 r = _iwl4965_read_direct32(priv, CSR_EEPROM_REG);
Zhu Yib481de92007-09-25 17:54:57 -07001648 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1649 break;
1650 udelay(IWL_EEPROM_ACCESS_DELAY);
1651 }
1652
1653 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1654 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1655 rc = -ETIMEDOUT;
1656 goto done;
1657 }
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001658 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001659 }
1660 rc = 0;
1661
1662done:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001663 iwl4965_eeprom_release_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001664 return rc;
1665}
1666
1667/******************************************************************************
1668 *
1669 * Misc. internal state and helper functions
1670 *
1671 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001672
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001673static void iwl4965_unset_hw_setting(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001674{
1675 if (priv->hw_setting.shared_virt)
1676 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001677 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001678 priv->hw_setting.shared_virt,
1679 priv->hw_setting.shared_phys);
1680}
1681
1682/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001683 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001684 *
1685 * return : set the bit for each supported rate insert in ie
1686 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001687static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001688 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001689{
1690 u16 ret_rates = 0, bit;
1691 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001692 u8 *cnt = ie;
1693 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001694
1695 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1696 if (bit & supported_rate) {
1697 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001698 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001699 ((bit & basic_rate) ? 0x80 : 0x00);
1700 (*cnt)++;
1701 (*left)--;
1702 if ((*left <= 0) ||
1703 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001704 break;
1705 }
1706 }
1707
1708 return ret_rates;
1709}
1710
Zhu Yib481de92007-09-25 17:54:57 -07001711/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001712 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001713 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001714static u16 iwl4965_fill_probe_req(struct iwl4965_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001715 enum ieee80211_band band,
1716 struct ieee80211_mgmt *frame,
1717 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001718{
1719 int len = 0;
1720 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001721 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001722#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001723 const struct ieee80211_supported_band *sband =
1724 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001725#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001726
1727 /* Make sure there is enough space for the probe request,
1728 * two mandatory IEs and the data */
1729 left -= 24;
1730 if (left < 0)
1731 return 0;
1732 len += 24;
1733
1734 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001735 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001736 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001737 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001738 frame->seq_ctrl = 0;
1739
1740 /* fill in our indirect SSID IE */
1741 /* ...next IE... */
1742
1743 left -= 2;
1744 if (left < 0)
1745 return 0;
1746 len += 2;
1747 pos = &(frame->u.probe_req.variable[0]);
1748 *pos++ = WLAN_EID_SSID;
1749 *pos++ = 0;
1750
1751 /* fill in our direct SSID IE... */
1752 if (is_direct) {
1753 /* ...next IE... */
1754 left -= 2 + priv->essid_len;
1755 if (left < 0)
1756 return 0;
1757 /* ... fill it in... */
1758 *pos++ = WLAN_EID_SSID;
1759 *pos++ = priv->essid_len;
1760 memcpy(pos, priv->essid, priv->essid_len);
1761 pos += priv->essid_len;
1762 len += 2 + priv->essid_len;
1763 }
1764
1765 /* fill in supported rate */
1766 /* ...next IE... */
1767 left -= 2;
1768 if (left < 0)
1769 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001770
Zhu Yib481de92007-09-25 17:54:57 -07001771 /* ... fill it in... */
1772 *pos++ = WLAN_EID_SUPP_RATES;
1773 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001774
mabbasbee488d2007-10-25 17:15:42 +08001775 /* exclude 60M rate */
1776 active_rates = priv->rates_mask;
1777 active_rates &= ~IWL_RATE_60M_MASK;
1778
1779 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001780
Tomas Winklerc7c46672007-10-18 02:04:15 +02001781 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001782 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001783 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001784 active_rates &= ~ret_rates;
1785
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001786 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001787 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001788 active_rates &= ~ret_rates;
1789
Zhu Yib481de92007-09-25 17:54:57 -07001790 len += 2 + *pos;
1791 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001792 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001793 goto fill_end;
1794
1795 /* fill in supported extended rate */
1796 /* ...next IE... */
1797 left -= 2;
1798 if (left < 0)
1799 return 0;
1800 /* ... fill it in... */
1801 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1802 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001803 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001804 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001805 if (*pos > 0)
1806 len += 2 + *pos;
1807
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001808#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001809 if (sband && sband->ht_info.ht_supported) {
1810 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001811 pos += (*pos) + 1;
1812 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001813 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001814 ht_cap = (struct ieee80211_ht_cap *)pos;
1815 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1816 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1817 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1818 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1819 ((sband->ht_info.ampdu_density << 2) &
1820 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001821 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001822 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001823#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001824
1825 fill_end:
1826 return (u16)len;
1827}
1828
1829/*
1830 * QoS support
1831*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001832static int iwl4965_send_qos_params_command(struct iwl4965_priv *priv,
1833 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001834{
1835
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001836 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1837 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001838}
1839
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001840static void iwl4965_reset_qos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001841{
1842 u16 cw_min = 15;
1843 u16 cw_max = 1023;
1844 u8 aifs = 2;
1845 u8 is_legacy = 0;
1846 unsigned long flags;
1847 int i;
1848
1849 spin_lock_irqsave(&priv->lock, flags);
1850 priv->qos_data.qos_active = 0;
1851
1852 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1853 if (priv->qos_data.qos_enable)
1854 priv->qos_data.qos_active = 1;
1855 if (!(priv->active_rate & 0xfff0)) {
1856 cw_min = 31;
1857 is_legacy = 1;
1858 }
1859 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1860 if (priv->qos_data.qos_enable)
1861 priv->qos_data.qos_active = 1;
1862 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1863 cw_min = 31;
1864 is_legacy = 1;
1865 }
1866
1867 if (priv->qos_data.qos_active)
1868 aifs = 3;
1869
1870 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1871 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1872 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1873 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1874 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1875
1876 if (priv->qos_data.qos_active) {
1877 i = 1;
1878 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1879 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1880 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1881 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1882 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1883
1884 i = 2;
1885 priv->qos_data.def_qos_parm.ac[i].cw_min =
1886 cpu_to_le16((cw_min + 1) / 2 - 1);
1887 priv->qos_data.def_qos_parm.ac[i].cw_max =
1888 cpu_to_le16(cw_max);
1889 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1890 if (is_legacy)
1891 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1892 cpu_to_le16(6016);
1893 else
1894 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1895 cpu_to_le16(3008);
1896 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1897
1898 i = 3;
1899 priv->qos_data.def_qos_parm.ac[i].cw_min =
1900 cpu_to_le16((cw_min + 1) / 4 - 1);
1901 priv->qos_data.def_qos_parm.ac[i].cw_max =
1902 cpu_to_le16((cw_max + 1) / 2 - 1);
1903 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1904 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1905 if (is_legacy)
1906 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1907 cpu_to_le16(3264);
1908 else
1909 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1910 cpu_to_le16(1504);
1911 } else {
1912 for (i = 1; i < 4; i++) {
1913 priv->qos_data.def_qos_parm.ac[i].cw_min =
1914 cpu_to_le16(cw_min);
1915 priv->qos_data.def_qos_parm.ac[i].cw_max =
1916 cpu_to_le16(cw_max);
1917 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1918 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1919 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1920 }
1921 }
1922 IWL_DEBUG_QOS("set QoS to default \n");
1923
1924 spin_unlock_irqrestore(&priv->lock, flags);
1925}
1926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001927static void iwl4965_activate_qos(struct iwl4965_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001928{
1929 unsigned long flags;
1930
Zhu Yib481de92007-09-25 17:54:57 -07001931 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1932 return;
1933
1934 if (!priv->qos_data.qos_enable)
1935 return;
1936
1937 spin_lock_irqsave(&priv->lock, flags);
1938 priv->qos_data.def_qos_parm.qos_flags = 0;
1939
1940 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1941 !priv->qos_data.qos_cap.q_AP.txop_request)
1942 priv->qos_data.def_qos_parm.qos_flags |=
1943 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001944 if (priv->qos_data.qos_active)
1945 priv->qos_data.def_qos_parm.qos_flags |=
1946 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1947
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001948#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001949 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001950 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001951#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001952
Zhu Yib481de92007-09-25 17:54:57 -07001953 spin_unlock_irqrestore(&priv->lock, flags);
1954
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001955 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001956 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1957 priv->qos_data.qos_active,
1958 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001959
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001960 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001961 &(priv->qos_data.def_qos_parm));
1962 }
1963}
1964
Zhu Yib481de92007-09-25 17:54:57 -07001965/*
1966 * Power management (not Tx power!) functions
1967 */
1968#define MSEC_TO_USEC 1024
1969
1970#define NOSLP __constant_cpu_to_le16(0), 0, 0
1971#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1972#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1973#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1974 __constant_cpu_to_le32(X1), \
1975 __constant_cpu_to_le32(X2), \
1976 __constant_cpu_to_le32(X3), \
1977 __constant_cpu_to_le32(X4)}
1978
1979
1980/* default power management (not Tx power) table values */
1981/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001982static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001983 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1984 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1985 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1986 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1987 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1988 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1989};
1990
1991/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001992static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001993 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1994 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1995 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1996 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1997 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1998 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1999 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2000 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2001 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2002 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2003};
2004
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002005int iwl4965_power_init_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002006{
2007 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002008 struct iwl4965_power_mgr *pow_data;
2009 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07002010 u16 pci_pm;
2011
2012 IWL_DEBUG_POWER("Initialize power \n");
2013
2014 pow_data = &(priv->power_data);
2015
2016 memset(pow_data, 0, sizeof(*pow_data));
2017
2018 pow_data->active_index = IWL_POWER_RANGE_0;
2019 pow_data->dtim_val = 0xffff;
2020
2021 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2022 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2023
2024 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2025 if (rc != 0)
2026 return 0;
2027 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002028 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002029
2030 IWL_DEBUG_POWER("adjust power command flags\n");
2031
2032 for (i = 0; i < IWL_POWER_AC; i++) {
2033 cmd = &pow_data->pwr_range_0[i].cmd;
2034
2035 if (pci_pm & 0x1)
2036 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2037 else
2038 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2039 }
2040 }
2041 return rc;
2042}
2043
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002044static int iwl4965_update_power_cmd(struct iwl4965_priv *priv,
2045 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002046{
2047 int rc = 0, i;
2048 u8 skip;
2049 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002050 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07002051 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002052 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07002053
2054 if (mode > IWL_POWER_INDEX_5) {
2055 IWL_DEBUG_POWER("Error invalid power mode \n");
2056 return -1;
2057 }
2058 pow_data = &(priv->power_data);
2059
2060 if (pow_data->active_index == IWL_POWER_RANGE_0)
2061 range = &pow_data->pwr_range_0[0];
2062 else
2063 range = &pow_data->pwr_range_1[1];
2064
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002065 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07002066
2067#ifdef IWL_MAC80211_DISABLE
2068 if (priv->assoc_network != NULL) {
2069 unsigned long flags;
2070
2071 period = priv->assoc_network->tim.tim_period;
2072 }
2073#endif /*IWL_MAC80211_DISABLE */
2074 skip = range[mode].no_dtim;
2075
2076 if (period == 0) {
2077 period = 1;
2078 skip = 0;
2079 }
2080
2081 if (skip == 0) {
2082 max_sleep = period;
2083 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2084 } else {
2085 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2086 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2087 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2088 }
2089
2090 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2091 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2092 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2093 }
2094
2095 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2096 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2097 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2098 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2099 le32_to_cpu(cmd->sleep_interval[0]),
2100 le32_to_cpu(cmd->sleep_interval[1]),
2101 le32_to_cpu(cmd->sleep_interval[2]),
2102 le32_to_cpu(cmd->sleep_interval[3]),
2103 le32_to_cpu(cmd->sleep_interval[4]));
2104
2105 return rc;
2106}
2107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002108static int iwl4965_send_power_mode(struct iwl4965_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002109{
John W. Linville9a62f732007-11-15 16:27:36 -05002110 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002111 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002112 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002113
2114 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08002115 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07002116 * else user level */
2117 switch (mode) {
2118 case IWL_POWER_BATTERY:
2119 final_mode = IWL_POWER_INDEX_3;
2120 break;
2121 case IWL_POWER_AC:
2122 final_mode = IWL_POWER_MODE_CAM;
2123 break;
2124 default:
2125 final_mode = mode;
2126 break;
2127 }
2128
2129 cmd.keep_alive_beacons = 0;
2130
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002131 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002132
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002133 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002134
2135 if (final_mode == IWL_POWER_MODE_CAM)
2136 clear_bit(STATUS_POWER_PMI, &priv->status);
2137 else
2138 set_bit(STATUS_POWER_PMI, &priv->status);
2139
2140 return rc;
2141}
2142
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002143int iwl4965_is_network_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002144{
2145 /* Filter incoming packets to determine if they are targeted toward
2146 * this network, discarding packets coming from ourselves */
2147 switch (priv->iw_mode) {
2148 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2149 /* packets from our adapter are dropped (echo) */
2150 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2151 return 0;
2152 /* {broad,multi}cast packets to our IBSS go through */
2153 if (is_multicast_ether_addr(header->addr1))
2154 return !compare_ether_addr(header->addr3, priv->bssid);
2155 /* packets to our adapter go through */
2156 return !compare_ether_addr(header->addr1, priv->mac_addr);
2157 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2158 /* packets from our adapter are dropped (echo) */
2159 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2160 return 0;
2161 /* {broad,multi}cast packets to our BSS go through */
2162 if (is_multicast_ether_addr(header->addr1))
2163 return !compare_ether_addr(header->addr2, priv->bssid);
2164 /* packets to our adapter go through */
2165 return !compare_ether_addr(header->addr1, priv->mac_addr);
2166 }
2167
2168 return 1;
2169}
2170
2171#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2172
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002173static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002174{
2175 switch (status & TX_STATUS_MSK) {
2176 case TX_STATUS_SUCCESS:
2177 return "SUCCESS";
2178 TX_STATUS_ENTRY(SHORT_LIMIT);
2179 TX_STATUS_ENTRY(LONG_LIMIT);
2180 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2181 TX_STATUS_ENTRY(MGMNT_ABORT);
2182 TX_STATUS_ENTRY(NEXT_FRAG);
2183 TX_STATUS_ENTRY(LIFE_EXPIRE);
2184 TX_STATUS_ENTRY(DEST_PS);
2185 TX_STATUS_ENTRY(ABORTED);
2186 TX_STATUS_ENTRY(BT_RETRY);
2187 TX_STATUS_ENTRY(STA_INVALID);
2188 TX_STATUS_ENTRY(FRAG_DROPPED);
2189 TX_STATUS_ENTRY(TID_DISABLE);
2190 TX_STATUS_ENTRY(FRAME_FLUSHED);
2191 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2192 TX_STATUS_ENTRY(TX_LOCKED);
2193 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2194 }
2195
2196 return "UNKNOWN";
2197}
2198
2199/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002200 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002201 *
2202 * NOTE: priv->mutex is not required before calling this function
2203 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002204static int iwl4965_scan_cancel(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002205{
2206 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2207 clear_bit(STATUS_SCANNING, &priv->status);
2208 return 0;
2209 }
2210
2211 if (test_bit(STATUS_SCANNING, &priv->status)) {
2212 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2213 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2214 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2215 queue_work(priv->workqueue, &priv->abort_scan);
2216
2217 } else
2218 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2219
2220 return test_bit(STATUS_SCANNING, &priv->status);
2221 }
2222
2223 return 0;
2224}
2225
2226/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002227 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002228 * @ms: amount of time to wait (in milliseconds) for scan to abort
2229 *
2230 * NOTE: priv->mutex must be held before calling this function
2231 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002232static int iwl4965_scan_cancel_timeout(struct iwl4965_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002233{
2234 unsigned long now = jiffies;
2235 int ret;
2236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002237 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002238 if (ret && ms) {
2239 mutex_unlock(&priv->mutex);
2240 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2241 test_bit(STATUS_SCANNING, &priv->status))
2242 msleep(1);
2243 mutex_lock(&priv->mutex);
2244
2245 return test_bit(STATUS_SCANNING, &priv->status);
2246 }
2247
2248 return ret;
2249}
2250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002251static void iwl4965_sequence_reset(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002252{
2253 /* Reset ieee stats */
2254
2255 /* We don't reset the net_device_stats (ieee->stats) on
2256 * re-association */
2257
2258 priv->last_seq_num = -1;
2259 priv->last_frag_num = -1;
2260 priv->last_packet_time = 0;
2261
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002262 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002263}
2264
2265#define MAX_UCODE_BEACON_INTERVAL 4096
2266#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2267
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002268static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002269{
2270 u16 new_val = 0;
2271 u16 beacon_factor = 0;
2272
2273 beacon_factor =
2274 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2275 / MAX_UCODE_BEACON_INTERVAL;
2276 new_val = beacon_val / beacon_factor;
2277
2278 return cpu_to_le16(new_val);
2279}
2280
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002281static void iwl4965_setup_rxon_timing(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002282{
2283 u64 interval_tm_unit;
2284 u64 tsf, result;
2285 unsigned long flags;
2286 struct ieee80211_conf *conf = NULL;
2287 u16 beacon_int = 0;
2288
2289 conf = ieee80211_get_hw_conf(priv->hw);
2290
2291 spin_lock_irqsave(&priv->lock, flags);
2292 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2293 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2294
2295 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2296
2297 tsf = priv->timestamp1;
2298 tsf = ((tsf << 32) | priv->timestamp0);
2299
2300 beacon_int = priv->beacon_int;
2301 spin_unlock_irqrestore(&priv->lock, flags);
2302
2303 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2304 if (beacon_int == 0) {
2305 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2306 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2307 } else {
2308 priv->rxon_timing.beacon_interval =
2309 cpu_to_le16(beacon_int);
2310 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002311 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002312 le16_to_cpu(priv->rxon_timing.beacon_interval));
2313 }
2314
2315 priv->rxon_timing.atim_window = 0;
2316 } else {
2317 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002318 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002319 /* TODO: we need to get atim_window from upper stack
2320 * for now we set to 0 */
2321 priv->rxon_timing.atim_window = 0;
2322 }
2323
2324 interval_tm_unit =
2325 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2326 result = do_div(tsf, interval_tm_unit);
2327 priv->rxon_timing.beacon_init_val =
2328 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2329
2330 IWL_DEBUG_ASSOC
2331 ("beacon interval %d beacon timer %d beacon tim %d\n",
2332 le16_to_cpu(priv->rxon_timing.beacon_interval),
2333 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2334 le16_to_cpu(priv->rxon_timing.atim_window));
2335}
2336
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002337static int iwl4965_scan_initiate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002338{
2339 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2340 IWL_ERROR("APs don't scan.\n");
2341 return 0;
2342 }
2343
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002344 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002345 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2346 return -EIO;
2347 }
2348
2349 if (test_bit(STATUS_SCANNING, &priv->status)) {
2350 IWL_DEBUG_SCAN("Scan already in progress.\n");
2351 return -EAGAIN;
2352 }
2353
2354 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2355 IWL_DEBUG_SCAN("Scan request while abort pending. "
2356 "Queuing.\n");
2357 return -EAGAIN;
2358 }
2359
2360 IWL_DEBUG_INFO("Starting scan...\n");
2361 priv->scan_bands = 2;
2362 set_bit(STATUS_SCANNING, &priv->status);
2363 priv->scan_start = jiffies;
2364 priv->scan_pass_start = priv->scan_start;
2365
2366 queue_work(priv->workqueue, &priv->request_scan);
2367
2368 return 0;
2369}
2370
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002371static int iwl4965_set_rxon_hwcrypto(struct iwl4965_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002372{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002373 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002374
2375 if (hw_decrypt)
2376 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2377 else
2378 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2379
2380 return 0;
2381}
2382
Johannes Berg8318d782008-01-24 19:38:38 +01002383static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv,
2384 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002385{
Johannes Berg8318d782008-01-24 19:38:38 +01002386 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002387 priv->staging_rxon.flags &=
2388 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2389 | RXON_FLG_CCK_MSK);
2390 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2391 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002392 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002393 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2394 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2395 else
2396 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2397
2398 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2399 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2400
2401 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2402 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2403 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2404 }
2405}
2406
2407/*
Ian Schram01ebd062007-10-25 17:15:22 +08002408 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002409 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002410static void iwl4965_connection_init_rx_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002411{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002412 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002413
2414 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2415
2416 switch (priv->iw_mode) {
2417 case IEEE80211_IF_TYPE_AP:
2418 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2419 break;
2420
2421 case IEEE80211_IF_TYPE_STA:
2422 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2423 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2424 break;
2425
2426 case IEEE80211_IF_TYPE_IBSS:
2427 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2428 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2429 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2430 RXON_FILTER_ACCEPT_GRP_MSK;
2431 break;
2432
2433 case IEEE80211_IF_TYPE_MNTR:
2434 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2435 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2436 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2437 break;
2438 }
2439
2440#if 0
2441 /* TODO: Figure out when short_preamble would be set and cache from
2442 * that */
2443 if (!hw_to_local(priv->hw)->short_preamble)
2444 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2445 else
2446 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2447#endif
2448
Johannes Berg8318d782008-01-24 19:38:38 +01002449 ch_info = iwl4965_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002450 le16_to_cpu(priv->staging_rxon.channel));
2451
2452 if (!ch_info)
2453 ch_info = &priv->channel_info[0];
2454
2455 /*
2456 * in some case A channels are all non IBSS
2457 * in this case force B/G channel
2458 */
2459 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2460 !(is_channel_ibss(ch_info)))
2461 ch_info = &priv->channel_info[0];
2462
2463 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002464 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002465
Johannes Berg8318d782008-01-24 19:38:38 +01002466 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002467
2468 priv->staging_rxon.ofdm_basic_rates =
2469 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2470 priv->staging_rxon.cck_basic_rates =
2471 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2472
2473 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2474 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2475 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2476 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2477 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2478 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2479 iwl4965_set_rxon_chain(priv);
2480}
2481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002482static int iwl4965_set_mode(struct iwl4965_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002483{
Zhu Yib481de92007-09-25 17:54:57 -07002484 if (mode == IEEE80211_IF_TYPE_IBSS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002485 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002486
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002487 ch_info = iwl4965_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002488 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002489 le16_to_cpu(priv->staging_rxon.channel));
2490
2491 if (!ch_info || !is_channel_ibss(ch_info)) {
2492 IWL_ERROR("channel %d not IBSS channel\n",
2493 le16_to_cpu(priv->staging_rxon.channel));
2494 return -EINVAL;
2495 }
2496 }
2497
Zhu Yib481de92007-09-25 17:54:57 -07002498 priv->iw_mode = mode;
2499
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002500 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002501 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2502
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002503 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002504
Mohamed Abbasfde35712007-11-29 11:10:15 +08002505 /* dont commit rxon if rf-kill is on*/
2506 if (!iwl4965_is_ready_rf(priv))
2507 return -EAGAIN;
2508
2509 cancel_delayed_work(&priv->scan_check);
2510 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2511 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2512 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2513 return -EAGAIN;
2514 }
2515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002516 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002517
2518 return 0;
2519}
2520
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002521static void iwl4965_build_tx_cmd_hwcrypto(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002522 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002523 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002524 struct sk_buff *skb_frag,
2525 int last_frag)
2526{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002527 struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002528
2529 switch (keyinfo->alg) {
2530 case ALG_CCMP:
2531 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2532 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2533 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2534 break;
2535
2536 case ALG_TKIP:
2537#if 0
2538 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2539
2540 if (last_frag)
2541 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2542 8);
2543 else
2544 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2545#endif
2546 break;
2547
2548 case ALG_WEP:
2549 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2550 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2551
2552 if (keyinfo->keylen == 13)
2553 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2554
2555 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2556
2557 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2558 "with key %d\n", ctl->key_idx);
2559 break;
2560
Zhu Yib481de92007-09-25 17:54:57 -07002561 default:
2562 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2563 break;
2564 }
2565}
2566
2567/*
2568 * handle build REPLY_TX command notification.
2569 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002570static void iwl4965_build_tx_cmd_basic(struct iwl4965_priv *priv,
2571 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002572 struct ieee80211_tx_control *ctrl,
2573 struct ieee80211_hdr *hdr,
2574 int is_unicast, u8 std_id)
2575{
2576 __le16 *qc;
2577 u16 fc = le16_to_cpu(hdr->frame_control);
2578 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2579
2580 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2581 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2582 tx_flags |= TX_CMD_FLG_ACK_MSK;
2583 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2584 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2585 if (ieee80211_is_probe_response(fc) &&
2586 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2587 tx_flags |= TX_CMD_FLG_TSF_MSK;
2588 } else {
2589 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2590 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2591 }
2592
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002593 if (ieee80211_is_back_request(fc))
2594 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2595
2596
Zhu Yib481de92007-09-25 17:54:57 -07002597 cmd->cmd.tx.sta_id = std_id;
2598 if (ieee80211_get_morefrag(hdr))
2599 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2600
2601 qc = ieee80211_get_qos_ctrl(hdr);
2602 if (qc) {
2603 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2604 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2605 } else
2606 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2607
2608 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2609 tx_flags |= TX_CMD_FLG_RTS_MSK;
2610 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2611 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2612 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2613 tx_flags |= TX_CMD_FLG_CTS_MSK;
2614 }
2615
2616 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2617 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2618
2619 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2620 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2621 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2622 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002623 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002624 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002625 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002626 } else
2627 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2628
2629 cmd->cmd.tx.driver_txop = 0;
2630 cmd->cmd.tx.tx_flags = tx_flags;
2631 cmd->cmd.tx.next_frame_len = 0;
2632}
2633
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002634/**
2635 * iwl4965_get_sta_id - Find station's index within station table
2636 *
2637 * If new IBSS station, create new entry in station table
2638 */
Ben Cahill9fbab512007-11-29 11:09:47 +08002639static int iwl4965_get_sta_id(struct iwl4965_priv *priv,
2640 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002641{
2642 int sta_id;
2643 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002644 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002645
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002646 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002647 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2648 is_multicast_ether_addr(hdr->addr1))
2649 return priv->hw_setting.bcast_sta_id;
2650
2651 switch (priv->iw_mode) {
2652
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002653 /* If we are a client station in a BSS network, use the special
2654 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002655 case IEEE80211_IF_TYPE_STA:
2656 return IWL_AP_ID;
2657
2658 /* If we are an AP, then find the station, or use BCAST */
2659 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002660 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002661 if (sta_id != IWL_INVALID_STATION)
2662 return sta_id;
2663 return priv->hw_setting.bcast_sta_id;
2664
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002665 /* If this frame is going out to an IBSS network, find the station,
2666 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002667 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002668 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002669 if (sta_id != IWL_INVALID_STATION)
2670 return sta_id;
2671
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002672 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002673 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2674 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002675
2676 if (sta_id != IWL_INVALID_STATION)
2677 return sta_id;
2678
Joe Perches0795af52007-10-03 17:59:30 -07002679 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002680 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002681 print_mac(mac, hdr->addr1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002682 iwl4965_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002683 return priv->hw_setting.bcast_sta_id;
2684
2685 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002686 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002687 return priv->hw_setting.bcast_sta_id;
2688 }
2689}
2690
2691/*
2692 * start REPLY_TX command process
2693 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002694static int iwl4965_tx_skb(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002695 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2696{
2697 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002698 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002699 u32 *control_flags;
2700 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002701 struct iwl4965_tx_queue *txq = NULL;
2702 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002703 dma_addr_t phys_addr;
2704 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002705 dma_addr_t scratch_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002706 struct iwl4965_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002707 u16 len, idx, len_org;
2708 u8 id, hdr_len, unicast;
2709 u8 sta_id;
2710 u16 seq_number = 0;
2711 u16 fc;
2712 __le16 *qc;
2713 u8 wait_write_ptr = 0;
2714 unsigned long flags;
2715 int rc;
2716
2717 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002718 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002719 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2720 goto drop_unlock;
2721 }
2722
Johannes Berg32bfd352007-12-19 01:31:26 +01002723 if (!priv->vif) {
2724 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002725 goto drop_unlock;
2726 }
2727
Johannes Berg8318d782008-01-24 19:38:38 +01002728 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002729 IWL_ERROR("ERROR: No TX rate available.\n");
2730 goto drop_unlock;
2731 }
2732
2733 unicast = !is_multicast_ether_addr(hdr->addr1);
2734 id = 0;
2735
2736 fc = le16_to_cpu(hdr->frame_control);
2737
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002738#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002739 if (ieee80211_is_auth(fc))
2740 IWL_DEBUG_TX("Sending AUTH frame\n");
2741 else if (ieee80211_is_assoc_request(fc))
2742 IWL_DEBUG_TX("Sending ASSOC frame\n");
2743 else if (ieee80211_is_reassoc_request(fc))
2744 IWL_DEBUG_TX("Sending REASSOC frame\n");
2745#endif
2746
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002747 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002748 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2749 (!iwl4965_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002750 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002751 !priv->assoc_station_added)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002752 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002753 goto drop_unlock;
2754 }
2755
2756 spin_unlock_irqrestore(&priv->lock, flags);
2757
2758 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002759
2760 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002761 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002762 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002763 DECLARE_MAC_BUF(mac);
2764
2765 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2766 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002767 goto drop;
2768 }
2769
2770 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2771
2772 qc = ieee80211_get_qos_ctrl(hdr);
2773 if (qc) {
2774 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2775 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2776 IEEE80211_SCTL_SEQ;
2777 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2778 (hdr->seq_ctrl &
2779 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2780 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002781#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002782 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002783 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002784 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002785 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002786#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002787 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002788
2789 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002790 txq = &priv->txq[txq_id];
2791 q = &txq->q;
2792
2793 spin_lock_irqsave(&priv->lock, flags);
2794
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002795 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002796 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002797 memset(tfd, 0, sizeof(*tfd));
2798 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002799 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002800
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002801 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002802 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002803 txq->txb[q->write_ptr].skb[0] = skb;
2804 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002805 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002806
2807 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002808 out_cmd = &txq->cmd[idx];
2809 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2810 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002811
2812 /*
2813 * Set up the Tx-command (not MAC!) header.
2814 * Store the chosen Tx queue and TFD index within the sequence field;
2815 * after Tx, uCode's Tx response will return this value so driver can
2816 * locate the frame within the tx queue and do post-tx processing.
2817 */
Zhu Yib481de92007-09-25 17:54:57 -07002818 out_cmd->hdr.cmd = REPLY_TX;
2819 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002820 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002821
2822 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002823 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2824
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002825 /*
2826 * Use the first empty entry in this queue's command buffer array
2827 * to contain the Tx command and MAC header concatenated together
2828 * (payload data will be in another buffer).
2829 * Size of this varies, due to varying MAC header length.
2830 * If end is not dword aligned, we'll have 2 extra bytes at the end
2831 * of the MAC header (device reads on dword boundaries).
2832 * We'll tell device about this padding later.
2833 */
Zhu Yib481de92007-09-25 17:54:57 -07002834 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002835 sizeof(struct iwl4965_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002836
2837 len_org = len;
2838 len = (len + 3) & ~3;
2839
2840 if (len_org != len)
2841 len_org = 1;
2842 else
2843 len_org = 0;
2844
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002845 /* Physical address of this Tx command's header (not MAC header!),
2846 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002847 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
2848 offsetof(struct iwl4965_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002849
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002850 /* Add buffer containing Tx command and MAC(!) header to TFD's
2851 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002852 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002853
2854 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002855 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002856
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002857 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2858 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002859 len = skb->len - hdr_len;
2860 if (len) {
2861 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2862 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002863 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002864 }
2865
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002866 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002867 if (len_org)
2868 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2869
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002870 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002871 len = (u16)skb->len;
2872 out_cmd->cmd.tx.len = cpu_to_le16(len);
2873
2874 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002875 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002876
2877 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002878 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002879
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002880 scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
2881 offsetof(struct iwl4965_tx_cmd, scratch);
2882 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2883 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2884
Zhu Yib481de92007-09-25 17:54:57 -07002885 if (!ieee80211_get_morefrag(hdr)) {
2886 txq->need_update = 1;
2887 if (qc) {
2888 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2889 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2890 }
2891 } else {
2892 wait_write_ptr = 1;
2893 txq->need_update = 0;
2894 }
2895
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002896 iwl4965_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002897 sizeof(out_cmd->cmd.tx));
2898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002899 iwl4965_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002900 ieee80211_get_hdrlen(fc));
2901
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002902 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002903 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2904
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002905 /* Tell device the write index *just past* this latest filled TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002906 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
2907 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002908 spin_unlock_irqrestore(&priv->lock, flags);
2909
2910 if (rc)
2911 return rc;
2912
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002913 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002914 && priv->mac80211_registered) {
2915 if (wait_write_ptr) {
2916 spin_lock_irqsave(&priv->lock, flags);
2917 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002918 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002919 spin_unlock_irqrestore(&priv->lock, flags);
2920 }
2921
2922 ieee80211_stop_queue(priv->hw, ctl->queue);
2923 }
2924
2925 return 0;
2926
2927drop_unlock:
2928 spin_unlock_irqrestore(&priv->lock, flags);
2929drop:
2930 return -1;
2931}
2932
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002933static void iwl4965_set_rate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002934{
Johannes Berg8318d782008-01-24 19:38:38 +01002935 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002936 struct ieee80211_rate *rate;
2937 int i;
2938
Johannes Berg8318d782008-01-24 19:38:38 +01002939 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002940 if (!hw) {
2941 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2942 return;
2943 }
Zhu Yib481de92007-09-25 17:54:57 -07002944
2945 priv->active_rate = 0;
2946 priv->active_rate_basic = 0;
2947
Johannes Berg8318d782008-01-24 19:38:38 +01002948 for (i = 0; i < hw->n_bitrates; i++) {
2949 rate = &(hw->bitrates[i]);
2950 if (rate->hw_value < IWL_RATE_COUNT)
2951 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002952 }
2953
2954 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2955 priv->active_rate, priv->active_rate_basic);
2956
2957 /*
2958 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2959 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2960 * OFDM
2961 */
2962 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2963 priv->staging_rxon.cck_basic_rates =
2964 ((priv->active_rate_basic &
2965 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2966 else
2967 priv->staging_rxon.cck_basic_rates =
2968 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2969
2970 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2971 priv->staging_rxon.ofdm_basic_rates =
2972 ((priv->active_rate_basic &
2973 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2974 IWL_FIRST_OFDM_RATE) & 0xFF;
2975 else
2976 priv->staging_rxon.ofdm_basic_rates =
2977 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2978}
2979
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002980static void iwl4965_radio_kill_sw(struct iwl4965_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002981{
2982 unsigned long flags;
2983
2984 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2985 return;
2986
2987 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2988 disable_radio ? "OFF" : "ON");
2989
2990 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002991 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002992 /* FIXME: This is a workaround for AP */
2993 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2994 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002995 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002996 CSR_UCODE_SW_BIT_RFKILL);
2997 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002998 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002999 set_bit(STATUS_RF_KILL_SW, &priv->status);
3000 }
3001 return;
3002 }
3003
3004 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003005 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07003006
3007 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3008 spin_unlock_irqrestore(&priv->lock, flags);
3009
3010 /* wake up ucode */
3011 msleep(10);
3012
3013 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003014 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3015 if (!iwl4965_grab_nic_access(priv))
3016 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003017 spin_unlock_irqrestore(&priv->lock, flags);
3018
3019 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3020 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3021 "disabled by HW switch\n");
3022 return;
3023 }
3024
3025 queue_work(priv->workqueue, &priv->restart);
3026 return;
3027}
3028
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003029void iwl4965_set_decrypted_flag(struct iwl4965_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07003030 u32 decrypt_res, struct ieee80211_rx_status *stats)
3031{
3032 u16 fc =
3033 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3034
3035 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3036 return;
3037
3038 if (!(fc & IEEE80211_FCTL_PROTECTED))
3039 return;
3040
3041 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3042 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3043 case RX_RES_STATUS_SEC_TYPE_TKIP:
3044 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3045 RX_RES_STATUS_BAD_ICV_MIC)
3046 stats->flag |= RX_FLAG_MMIC_ERROR;
3047 case RX_RES_STATUS_SEC_TYPE_WEP:
3048 case RX_RES_STATUS_SEC_TYPE_CCMP:
3049 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3050 RX_RES_STATUS_DECRYPT_OK) {
3051 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3052 stats->flag |= RX_FLAG_DECRYPTED;
3053 }
3054 break;
3055
3056 default:
3057 break;
3058 }
3059}
3060
Zhu Yib481de92007-09-25 17:54:57 -07003061
3062#define IWL_PACKET_RETRY_TIME HZ
3063
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003064int iwl4965_is_duplicate_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07003065{
3066 u16 sc = le16_to_cpu(header->seq_ctrl);
3067 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3068 u16 frag = sc & IEEE80211_SCTL_FRAG;
3069 u16 *last_seq, *last_frag;
3070 unsigned long *last_time;
3071
3072 switch (priv->iw_mode) {
3073 case IEEE80211_IF_TYPE_IBSS:{
3074 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003075 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003076 u8 *mac = header->addr2;
3077 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3078
3079 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003080 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07003081 if (!compare_ether_addr(entry->mac, mac))
3082 break;
3083 }
3084 if (p == &priv->ibss_mac_hash[index]) {
3085 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3086 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08003087 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07003088 return 0;
3089 }
3090 memcpy(entry->mac, mac, ETH_ALEN);
3091 entry->seq_num = seq;
3092 entry->frag_num = frag;
3093 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08003094 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07003095 return 0;
3096 }
3097 last_seq = &entry->seq_num;
3098 last_frag = &entry->frag_num;
3099 last_time = &entry->packet_time;
3100 break;
3101 }
3102 case IEEE80211_IF_TYPE_STA:
3103 last_seq = &priv->last_seq_num;
3104 last_frag = &priv->last_frag_num;
3105 last_time = &priv->last_packet_time;
3106 break;
3107 default:
3108 return 0;
3109 }
3110 if ((*last_seq == seq) &&
3111 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3112 if (*last_frag == frag)
3113 goto drop;
3114 if (*last_frag + 1 != frag)
3115 /* out-of-order fragment */
3116 goto drop;
3117 } else
3118 *last_seq = seq;
3119
3120 *last_frag = frag;
3121 *last_time = jiffies;
3122 return 0;
3123
3124 drop:
3125 return 1;
3126}
3127
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003128#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003129
3130#include "iwl-spectrum.h"
3131
3132#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3133#define BEACON_TIME_MASK_HIGH 0xFF000000
3134#define TIME_UNIT 1024
3135
3136/*
3137 * extended beacon time format
3138 * time in usec will be changed into a 32-bit value in 8:24 format
3139 * the high 1 byte is the beacon counts
3140 * the lower 3 bytes is the time in usec within one beacon interval
3141 */
3142
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003143static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003144{
3145 u32 quot;
3146 u32 rem;
3147 u32 interval = beacon_interval * 1024;
3148
3149 if (!interval || !usec)
3150 return 0;
3151
3152 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3153 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3154
3155 return (quot << 24) + rem;
3156}
3157
3158/* base is usually what we get from ucode with each received frame,
3159 * the same as HW timer counter counting down
3160 */
3161
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003162static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003163{
3164 u32 base_low = base & BEACON_TIME_MASK_LOW;
3165 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3166 u32 interval = beacon_interval * TIME_UNIT;
3167 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3168 (addon & BEACON_TIME_MASK_HIGH);
3169
3170 if (base_low > addon_low)
3171 res += base_low - addon_low;
3172 else if (base_low < addon_low) {
3173 res += interval + base_low - addon_low;
3174 res += (1 << 24);
3175 } else
3176 res += (1 << 24);
3177
3178 return cpu_to_le32(res);
3179}
3180
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003181static int iwl4965_get_measurement(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003182 struct ieee80211_measurement_params *params,
3183 u8 type)
3184{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003185 struct iwl4965_spectrum_cmd spectrum;
3186 struct iwl4965_rx_packet *res;
3187 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003188 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3189 .data = (void *)&spectrum,
3190 .meta.flags = CMD_WANT_SKB,
3191 };
3192 u32 add_time = le64_to_cpu(params->start_time);
3193 int rc;
3194 int spectrum_resp_status;
3195 int duration = le16_to_cpu(params->duration);
3196
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003197 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003198 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003199 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003200 le64_to_cpu(params->start_time) - priv->last_tsf,
3201 le16_to_cpu(priv->rxon_timing.beacon_interval));
3202
3203 memset(&spectrum, 0, sizeof(spectrum));
3204
3205 spectrum.channel_count = cpu_to_le16(1);
3206 spectrum.flags =
3207 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3208 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3209 cmd.len = sizeof(spectrum);
3210 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3211
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003212 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003213 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003214 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003215 add_time,
3216 le16_to_cpu(priv->rxon_timing.beacon_interval));
3217 else
3218 spectrum.start_time = 0;
3219
3220 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3221 spectrum.channels[0].channel = params->channel;
3222 spectrum.channels[0].type = type;
3223 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3224 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3225 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003227 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003228 if (rc)
3229 return rc;
3230
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003231 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003232 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3233 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3234 rc = -EIO;
3235 }
3236
3237 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3238 switch (spectrum_resp_status) {
3239 case 0: /* Command will be handled */
3240 if (res->u.spectrum.id != 0xff) {
3241 IWL_DEBUG_INFO
3242 ("Replaced existing measurement: %d\n",
3243 res->u.spectrum.id);
3244 priv->measurement_status &= ~MEASUREMENT_READY;
3245 }
3246 priv->measurement_status |= MEASUREMENT_ACTIVE;
3247 rc = 0;
3248 break;
3249
3250 case 1: /* Command will not be handled */
3251 rc = -EAGAIN;
3252 break;
3253 }
3254
3255 dev_kfree_skb_any(cmd.meta.u.skb);
3256
3257 return rc;
3258}
3259#endif
3260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003261static void iwl4965_txstatus_to_ieee(struct iwl4965_priv *priv,
3262 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003263{
3264
3265 tx_sta->status.ack_signal = 0;
3266 tx_sta->status.excessive_retries = 0;
3267 tx_sta->status.queue_length = 0;
3268 tx_sta->status.queue_number = 0;
3269
3270 if (in_interrupt())
3271 ieee80211_tx_status_irqsafe(priv->hw,
3272 tx_sta->skb[0], &(tx_sta->status));
3273 else
3274 ieee80211_tx_status(priv->hw,
3275 tx_sta->skb[0], &(tx_sta->status));
3276
3277 tx_sta->skb[0] = NULL;
3278}
3279
3280/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003281 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07003282 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003283 * When FW advances 'R' index, all entries between old and new 'R' index
3284 * need to be reclaimed. As result, some free space forms. If there is
3285 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07003286 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003287int iwl4965_tx_queue_reclaim(struct iwl4965_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003288{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003289 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3290 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003291 int nfreed = 0;
3292
3293 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3294 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3295 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003296 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003297 return 0;
3298 }
3299
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003300 for (index = iwl4965_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003301 q->read_ptr != index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003302 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003303 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003304 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003305 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003306 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003307 } else if (nfreed > 1) {
3308 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003309 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003310 queue_work(priv->workqueue, &priv->restart);
3311 }
3312 nfreed++;
3313 }
3314
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003315/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003316 (txq_id != IWL_CMD_QUEUE_NUM) &&
3317 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003318 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07003319
3320
3321 return nfreed;
3322}
3323
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003324static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003325{
3326 status &= TX_STATUS_MSK;
3327 return (status == TX_STATUS_SUCCESS)
3328 || (status == TX_STATUS_DIRECT_DONE);
3329}
3330
3331/******************************************************************************
3332 *
3333 * Generic RX handler implementations
3334 *
3335 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003336#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003338static inline int iwl4965_get_ra_sta_id(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003339 struct ieee80211_hdr *hdr)
3340{
3341 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3342 return IWL_AP_ID;
3343 else {
3344 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003345 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07003346 }
3347}
3348
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003349static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
3350 struct iwl4965_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07003351{
3352 if (priv->txq[txq_id].txb[idx].skb[0])
3353 return (struct ieee80211_hdr *)priv->txq[txq_id].
3354 txb[idx].skb[0]->data;
3355 return NULL;
3356}
3357
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003358static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003359{
3360 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3361 tx_resp->frame_count);
3362 return le32_to_cpu(*scd_ssn) & MAX_SN;
3363
3364}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003365
3366/**
3367 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3368 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003369static int iwl4965_tx_status_reply_tx(struct iwl4965_priv *priv,
3370 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003371 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003372 u16 start_idx)
3373{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003374 u16 status;
3375 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07003376 struct ieee80211_tx_status *tx_status = NULL;
3377 struct ieee80211_hdr *hdr = NULL;
3378 int i, sh;
3379 int txq_id, idx;
3380 u16 seq;
3381
3382 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003383 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003384
3385 agg->frame_count = tx_resp->frame_count;
3386 agg->start_idx = start_idx;
3387 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003388 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003389
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003390 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003391 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003392 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003393 status = le16_to_cpu(frame_status[0].status);
3394 seq = le16_to_cpu(frame_status[0].sequence);
3395 idx = SEQ_TO_INDEX(seq);
3396 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003397
Zhu Yib481de92007-09-25 17:54:57 -07003398 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003399 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3400 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003401
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003402 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003403 tx_status->retry_count = tx_resp->failure_frame;
3404 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003405 tx_status->queue_length = tx_resp->failure_rts;
3406 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003407 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003408 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003409 iwl4965_hwrate_to_tx_control(priv,
3410 le32_to_cpu(tx_resp->rate_n_flags),
3411 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003412 /* FIXME: code repetition end */
3413
3414 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3415 status & 0xff, tx_resp->failure_frame);
3416 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003417 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003418
3419 agg->wait_for_ba = 0;
3420 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003421 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003422 u64 bitmap = 0;
3423 int start = agg->start_idx;
3424
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003425 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003426 for (i = 0; i < agg->frame_count; i++) {
3427 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003428 status = le16_to_cpu(frame_status[i].status);
3429 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003430 idx = SEQ_TO_INDEX(seq);
3431 txq_id = SEQ_TO_QUEUE(seq);
3432
3433 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3434 AGG_TX_STATE_ABORT_MSK))
3435 continue;
3436
3437 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3438 agg->frame_count, txq_id, idx);
3439
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003440 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003441
3442 sc = le16_to_cpu(hdr->seq_ctrl);
3443 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3444 IWL_ERROR("BUG_ON idx doesn't match seq control"
3445 " idx=%d, seq_idx=%d, seq=%d\n",
3446 idx, SEQ_TO_SN(sc),
3447 hdr->seq_ctrl);
3448 return -1;
3449 }
3450
3451 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3452 i, idx, SEQ_TO_SN(sc));
3453
3454 sh = idx - start;
3455 if (sh > 64) {
3456 sh = (start - idx) + 0xff;
3457 bitmap = bitmap << sh;
3458 sh = 0;
3459 start = idx;
3460 } else if (sh < -64)
3461 sh = 0xff - (start - idx);
3462 else if (sh < 0) {
3463 sh = start - idx;
3464 start = idx;
3465 bitmap = bitmap << sh;
3466 sh = 0;
3467 }
3468 bitmap |= (1 << sh);
3469 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3470 start, (u32)(bitmap & 0xFFFFFFFF));
3471 }
3472
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003473 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003474 agg->start_idx = start;
3475 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003476 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003477 agg->frame_count, agg->start_idx,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003478 agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003479
3480 if (bitmap)
3481 agg->wait_for_ba = 1;
3482 }
3483 return 0;
3484}
3485#endif
Zhu Yib481de92007-09-25 17:54:57 -07003486
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003487/**
3488 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3489 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003490static void iwl4965_rx_reply_tx(struct iwl4965_priv *priv,
3491 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003492{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003493 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003494 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3495 int txq_id = SEQ_TO_QUEUE(sequence);
3496 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003497 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003498 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003499 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003500 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003501#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003502 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3503 struct ieee80211_hdr *hdr;
3504 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003505#endif
3506
3507 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3508 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3509 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003510 index, txq->q.n_bd, txq->q.write_ptr,
3511 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003512 return;
3513 }
3514
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003515#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003516 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3517 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003518
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003519 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003520 tid = le16_to_cpu(*qc) & 0xf;
3521
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003522 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3523 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3524 IWL_ERROR("Station not known\n");
3525 return;
3526 }
3527
3528 if (txq->sched_retry) {
3529 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3530 struct iwl4965_ht_agg *agg = NULL;
3531
3532 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003533 return;
Zhu Yib481de92007-09-25 17:54:57 -07003534
3535 agg = &priv->stations[sta_id].tid[tid].agg;
3536
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003537 iwl4965_tx_status_reply_tx(priv, agg,
3538 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003539
3540 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003541 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003542 /* TODO: send BAR */
3543 }
3544
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003545 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3546 int freed;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003547 index = iwl4965_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003548 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3549 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003550 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3551 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3552
3553 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3554 txq_id >= 0 && priv->mac80211_registered &&
3555 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3556 ieee80211_wake_queue(priv->hw, txq_id);
3557
3558 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003559 }
3560 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003561#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003562 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003563
3564 tx_status->retry_count = tx_resp->failure_frame;
3565 tx_status->queue_number = status;
3566 tx_status->queue_length = tx_resp->bt_kill_count;
3567 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003568 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003569 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003570 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3571 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003572
Zhu Yib481de92007-09-25 17:54:57 -07003573 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003574 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003575 status, le32_to_cpu(tx_resp->rate_n_flags),
3576 tx_resp->failure_frame);
3577
3578 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003579 if (index != -1) {
3580 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003581#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003582 if (tid != MAX_TID_COUNT)
3583 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3584 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3585 (txq_id >= 0) &&
3586 priv->mac80211_registered)
3587 ieee80211_wake_queue(priv->hw, txq_id);
3588 if (tid != MAX_TID_COUNT)
3589 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3590#endif
Zhu Yib481de92007-09-25 17:54:57 -07003591 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003592#ifdef CONFIG_IWL4965_HT
3593 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003594#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003595
3596 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3597 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3598}
3599
3600
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003601static void iwl4965_rx_reply_alive(struct iwl4965_priv *priv,
3602 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003603{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003604 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3605 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003606 struct delayed_work *pwork;
3607
3608 palive = &pkt->u.alive_frame;
3609
3610 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3611 "0x%01X 0x%01X\n",
3612 palive->is_valid, palive->ver_type,
3613 palive->ver_subtype);
3614
3615 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3616 IWL_DEBUG_INFO("Initialization Alive received.\n");
3617 memcpy(&priv->card_alive_init,
3618 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003619 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003620 pwork = &priv->init_alive_start;
3621 } else {
3622 IWL_DEBUG_INFO("Runtime Alive received.\n");
3623 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003624 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003625 pwork = &priv->alive_start;
3626 }
3627
3628 /* We delay the ALIVE response by 5ms to
3629 * give the HW RF Kill time to activate... */
3630 if (palive->is_valid == UCODE_VALID_OK)
3631 queue_delayed_work(priv->workqueue, pwork,
3632 msecs_to_jiffies(5));
3633 else
3634 IWL_WARNING("uCode did not respond OK.\n");
3635}
3636
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003637static void iwl4965_rx_reply_add_sta(struct iwl4965_priv *priv,
3638 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003639{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003640 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003641
3642 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3643 return;
3644}
3645
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003646static void iwl4965_rx_reply_error(struct iwl4965_priv *priv,
3647 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003648{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003649 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003650
3651 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3652 "seq 0x%04X ser 0x%08X\n",
3653 le32_to_cpu(pkt->u.err_resp.error_type),
3654 get_cmd_string(pkt->u.err_resp.cmd_id),
3655 pkt->u.err_resp.cmd_id,
3656 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3657 le32_to_cpu(pkt->u.err_resp.error_info));
3658}
3659
3660#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3661
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003662static void iwl4965_rx_csa(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003663{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003664 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3665 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3666 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003667 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3668 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3669 rxon->channel = csa->channel;
3670 priv->staging_rxon.channel = csa->channel;
3671}
3672
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003673static void iwl4965_rx_spectrum_measure_notif(struct iwl4965_priv *priv,
3674 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003675{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003676#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003677 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3678 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003679
3680 if (!report->state) {
3681 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3682 "Spectrum Measure Notification: Start\n");
3683 return;
3684 }
3685
3686 memcpy(&priv->measure_report, report, sizeof(*report));
3687 priv->measurement_status |= MEASUREMENT_READY;
3688#endif
3689}
3690
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003691static void iwl4965_rx_pm_sleep_notif(struct iwl4965_priv *priv,
3692 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003693{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003694#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003695 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3696 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003697 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3698 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3699#endif
3700}
3701
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003702static void iwl4965_rx_pm_debug_statistics_notif(struct iwl4965_priv *priv,
3703 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003704{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003705 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003706 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3707 "notification for %s:\n",
3708 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003709 iwl4965_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003710}
3711
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003712static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003713{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003714 struct iwl4965_priv *priv =
3715 container_of(work, struct iwl4965_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003716 struct sk_buff *beacon;
3717
3718 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003719 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003720
3721 if (!beacon) {
3722 IWL_ERROR("update beacon failed\n");
3723 return;
3724 }
3725
3726 mutex_lock(&priv->mutex);
3727 /* new beacon skb is allocated every time; dispose previous.*/
3728 if (priv->ibss_beacon)
3729 dev_kfree_skb(priv->ibss_beacon);
3730
3731 priv->ibss_beacon = beacon;
3732 mutex_unlock(&priv->mutex);
3733
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003734 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003735}
3736
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003737static void iwl4965_rx_beacon_notif(struct iwl4965_priv *priv,
3738 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003739{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003740#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003741 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3742 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3743 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003744
3745 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3746 "tsf %d %d rate %d\n",
3747 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3748 beacon->beacon_notify_hdr.failure_frame,
3749 le32_to_cpu(beacon->ibss_mgr_status),
3750 le32_to_cpu(beacon->high_tsf),
3751 le32_to_cpu(beacon->low_tsf), rate);
3752#endif
3753
3754 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3755 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3756 queue_work(priv->workqueue, &priv->beacon_update);
3757}
3758
3759/* Service response to REPLY_SCAN_CMD (0x80) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003760static void iwl4965_rx_reply_scan(struct iwl4965_priv *priv,
3761 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003762{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003763#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003764 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3765 struct iwl4965_scanreq_notification *notif =
3766 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003767
3768 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3769#endif
3770}
3771
3772/* Service SCAN_START_NOTIFICATION (0x82) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003773static void iwl4965_rx_scan_start_notif(struct iwl4965_priv *priv,
3774 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003775{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003776 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3777 struct iwl4965_scanstart_notification *notif =
3778 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003779 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3780 IWL_DEBUG_SCAN("Scan start: "
3781 "%d [802.11%s] "
3782 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3783 notif->channel,
3784 notif->band ? "bg" : "a",
3785 notif->tsf_high,
3786 notif->tsf_low, notif->status, notif->beacon_timer);
3787}
3788
3789/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003790static void iwl4965_rx_scan_results_notif(struct iwl4965_priv *priv,
3791 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003792{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003793 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3794 struct iwl4965_scanresults_notification *notif =
3795 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003796
3797 IWL_DEBUG_SCAN("Scan ch.res: "
3798 "%d [802.11%s] "
3799 "(TSF: 0x%08X:%08X) - %d "
3800 "elapsed=%lu usec (%dms since last)\n",
3801 notif->channel,
3802 notif->band ? "bg" : "a",
3803 le32_to_cpu(notif->tsf_high),
3804 le32_to_cpu(notif->tsf_low),
3805 le32_to_cpu(notif->statistics[0]),
3806 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3807 jiffies_to_msecs(elapsed_jiffies
3808 (priv->last_scan_jiffies, jiffies)));
3809
3810 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003811 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003812}
3813
3814/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003815static void iwl4965_rx_scan_complete_notif(struct iwl4965_priv *priv,
3816 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003817{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003818 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3819 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003820
3821 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3822 scan_notif->scanned_channels,
3823 scan_notif->tsf_low,
3824 scan_notif->tsf_high, scan_notif->status);
3825
3826 /* The HW is no longer scanning */
3827 clear_bit(STATUS_SCAN_HW, &priv->status);
3828
3829 /* The scan completion notification came in, so kill that timer... */
3830 cancel_delayed_work(&priv->scan_check);
3831
3832 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3833 (priv->scan_bands == 2) ? "2.4" : "5.2",
3834 jiffies_to_msecs(elapsed_jiffies
3835 (priv->scan_pass_start, jiffies)));
3836
3837 /* Remove this scanned band from the list
3838 * of pending bands to scan */
3839 priv->scan_bands--;
3840
3841 /* If a request to abort was given, or the scan did not succeed
3842 * then we reset the scan state machine and terminate,
3843 * re-queuing another scan if one has been requested */
3844 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3845 IWL_DEBUG_INFO("Aborted scan completed.\n");
3846 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3847 } else {
3848 /* If there are more bands on this scan pass reschedule */
3849 if (priv->scan_bands > 0)
3850 goto reschedule;
3851 }
3852
3853 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003854 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003855 IWL_DEBUG_INFO("Setting scan to off\n");
3856
3857 clear_bit(STATUS_SCANNING, &priv->status);
3858
3859 IWL_DEBUG_INFO("Scan took %dms\n",
3860 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3861
3862 queue_work(priv->workqueue, &priv->scan_completed);
3863
3864 return;
3865
3866reschedule:
3867 priv->scan_pass_start = jiffies;
3868 queue_work(priv->workqueue, &priv->request_scan);
3869}
3870
3871/* Handle notification from uCode that card's power state is changing
3872 * due to software, hardware, or critical temperature RFKILL */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003873static void iwl4965_rx_card_state_notif(struct iwl4965_priv *priv,
3874 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003875{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003876 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003877 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3878 unsigned long status = priv->status;
3879
3880 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3881 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3882 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3883
3884 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3885 RF_CARD_DISABLED)) {
3886
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003887 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003888 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3889
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003890 if (!iwl4965_grab_nic_access(priv)) {
3891 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003892 priv, HBUS_TARG_MBX_C,
3893 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3894
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003895 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003896 }
3897
3898 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003899 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003900 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003901 if (!iwl4965_grab_nic_access(priv)) {
3902 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003903 priv, HBUS_TARG_MBX_C,
3904 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3905
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003906 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003907 }
3908 }
3909
3910 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003911 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003912 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003913 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3914 if (!iwl4965_grab_nic_access(priv))
3915 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003916 }
3917 }
3918
3919 if (flags & HW_CARD_DISABLED)
3920 set_bit(STATUS_RF_KILL_HW, &priv->status);
3921 else
3922 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3923
3924
3925 if (flags & SW_CARD_DISABLED)
3926 set_bit(STATUS_RF_KILL_SW, &priv->status);
3927 else
3928 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3929
3930 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003931 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003932
3933 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3934 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3935 (test_bit(STATUS_RF_KILL_SW, &status) !=
3936 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3937 queue_work(priv->workqueue, &priv->rf_kill);
3938 else
3939 wake_up_interruptible(&priv->wait_command_queue);
3940}
3941
3942/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003943 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003944 *
3945 * Setup the RX handlers for each of the reply types sent from the uCode
3946 * to the host.
3947 *
3948 * This function chains into the hardware specific files for them to setup
3949 * any hardware specific handlers as well.
3950 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003951static void iwl4965_setup_rx_handlers(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003952{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003953 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3954 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3955 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3956 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003957 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003958 iwl4965_rx_spectrum_measure_notif;
3959 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003960 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003961 iwl4965_rx_pm_debug_statistics_notif;
3962 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003963
Ben Cahill9fbab512007-11-29 11:09:47 +08003964 /*
3965 * The same handler is used for both the REPLY to a discrete
3966 * statistics request from the host as well as for the periodic
3967 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003968 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003969 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3970 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003971
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003972 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3973 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003974 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003975 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003976 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003977 iwl4965_rx_scan_complete_notif;
3978 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3979 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003980
Ben Cahill9fbab512007-11-29 11:09:47 +08003981 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003982 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003983}
3984
3985/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003986 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003987 * @rxb: Rx buffer to reclaim
3988 *
3989 * If an Rx buffer has an async callback associated with it the callback
3990 * will be executed. The attached skb (if present) will only be freed
3991 * if the callback returns 1
3992 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003993static void iwl4965_tx_cmd_complete(struct iwl4965_priv *priv,
3994 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003995{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003996 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003997 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3998 int txq_id = SEQ_TO_QUEUE(sequence);
3999 int index = SEQ_TO_INDEX(sequence);
4000 int huge = sequence & SEQ_HUGE_FRAME;
4001 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004002 struct iwl4965_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07004003
4004 /* If a Tx command is being handled and it isn't in the actual
4005 * command queue then there a command routing bug has been introduced
4006 * in the queue management code. */
4007 if (txq_id != IWL_CMD_QUEUE_NUM)
4008 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
4009 txq_id, pkt->hdr.cmd);
4010 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
4011
4012 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
4013 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
4014
4015 /* Input error checking is done when commands are added to queue. */
4016 if (cmd->meta.flags & CMD_WANT_SKB) {
4017 cmd->meta.source->u.skb = rxb->skb;
4018 rxb->skb = NULL;
4019 } else if (cmd->meta.u.callback &&
4020 !cmd->meta.u.callback(priv, cmd, rxb->skb))
4021 rxb->skb = NULL;
4022
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004023 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07004024
4025 if (!(cmd->meta.flags & CMD_ASYNC)) {
4026 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4027 wake_up_interruptible(&priv->wait_command_queue);
4028 }
4029}
4030
4031/************************** RX-FUNCTIONS ****************************/
4032/*
4033 * Rx theory of operation
4034 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004035 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
4036 * each of which point to Receive Buffers to be filled by 4965. These get
4037 * used not only for Rx frames, but for any command response or notification
4038 * from the 4965. The driver and 4965 manage the Rx buffers by means
4039 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07004040 *
4041 * Rx Queue Indexes
4042 * The host/firmware share two index registers for managing the Rx buffers.
4043 *
4044 * The READ index maps to the first position that the firmware may be writing
4045 * to -- the driver can read up to (but not including) this position and get
4046 * good data.
4047 * The READ index is managed by the firmware once the card is enabled.
4048 *
4049 * The WRITE index maps to the last position the driver has read from -- the
4050 * position preceding WRITE is the last slot the firmware can place a packet.
4051 *
4052 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
4053 * WRITE = READ.
4054 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004055 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07004056 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4057 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004058 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07004059 * and fire the RX interrupt. The driver can then query the READ index and
4060 * process as many packets as possible, moving the WRITE index forward as it
4061 * resets the Rx queue buffers with new memory.
4062 *
4063 * The management in the driver is as follows:
4064 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
4065 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08004066 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004067 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07004068 * iwl->rxq is replenished and the READ INDEX is updated (updating the
4069 * 'processed' and 'read' driver indexes as well)
4070 * + A received packet is processed and handed to the kernel network stack,
4071 * detached from the iwl->rxq. The driver 'processed' index is updated.
4072 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
4073 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
4074 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
4075 * were enough free buffers and RX_STALLED is set it is cleared.
4076 *
4077 *
4078 * Driver sequence:
4079 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004080 * iwl4965_rx_queue_alloc() Allocates rx_free
4081 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004082 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08004083 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07004084 * queue, updates firmware pointers, and updates
4085 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004086 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07004087 *
4088 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08004089 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07004090 * READ INDEX, detaching the SKB from the pool.
4091 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004092 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07004093 * slots.
4094 * ...
4095 *
4096 */
4097
4098/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004099 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07004100 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004101static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004102{
4103 int s = q->read - q->write;
4104 if (s <= 0)
4105 s += RX_QUEUE_SIZE;
4106 /* keep some buffer to not confuse full and empty queue */
4107 s -= 2;
4108 if (s < 0)
4109 s = 0;
4110 return s;
4111}
4112
4113/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004114 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07004115 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004116int iwl4965_rx_queue_update_write_ptr(struct iwl4965_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004117{
4118 u32 reg = 0;
4119 int rc = 0;
4120 unsigned long flags;
4121
4122 spin_lock_irqsave(&q->lock, flags);
4123
4124 if (q->need_update == 0)
4125 goto exit_unlock;
4126
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004127 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07004128 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004129 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004130
4131 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004132 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004133 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4134 goto exit_unlock;
4135 }
4136
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004137 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004138 if (rc)
4139 goto exit_unlock;
4140
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004141 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004142 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07004143 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004144 iwl4965_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004145
4146 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07004147 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004148 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004149 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07004150
4151
4152 q->need_update = 0;
4153
4154 exit_unlock:
4155 spin_unlock_irqrestore(&q->lock, flags);
4156 return rc;
4157}
4158
4159/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004160 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07004161 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004162static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004163 dma_addr_t dma_addr)
4164{
4165 return cpu_to_le32((u32)(dma_addr >> 8));
4166}
4167
4168
4169/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004170 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07004171 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004172 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07004173 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08004174 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07004175 *
4176 * This moves the 'write' index forward to catch up with 'processed', and
4177 * also updates the memory address in the firmware to reference the new
4178 * target buffer.
4179 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004180static int iwl4965_rx_queue_restock(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004181{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004182 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004183 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004184 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004185 unsigned long flags;
4186 int write, rc;
4187
4188 spin_lock_irqsave(&rxq->lock, flags);
4189 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004190 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004191 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07004192 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004193 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004194 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004195
4196 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004197 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004198 rxq->queue[rxq->write] = rxb;
4199 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4200 rxq->free_count--;
4201 }
4202 spin_unlock_irqrestore(&rxq->lock, flags);
4203 /* If the pre-allocated buffer pool is dropping low, schedule to
4204 * refill it */
4205 if (rxq->free_count <= RX_LOW_WATERMARK)
4206 queue_work(priv->workqueue, &priv->rx_replenish);
4207
4208
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004209 /* If we've added more space for the firmware to place data, tell it.
4210 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07004211 if ((write != (rxq->write & ~0x7))
4212 || (abs(rxq->write - rxq->read) > 7)) {
4213 spin_lock_irqsave(&rxq->lock, flags);
4214 rxq->need_update = 1;
4215 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004216 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004217 if (rc)
4218 return rc;
4219 }
4220
4221 return 0;
4222}
4223
4224/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004225 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004226 *
4227 * When moving to rx_free an SKB is allocated for the slot.
4228 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004229 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004230 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004231 */
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004232static void iwl4965_rx_allocate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004233{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004234 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004235 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004236 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004237 unsigned long flags;
4238 spin_lock_irqsave(&rxq->lock, flags);
4239 while (!list_empty(&rxq->rx_used)) {
4240 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004241 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004242
4243 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07004244 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004245 alloc_skb(priv->hw_setting.rx_buf_size,
4246 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07004247 if (!rxb->skb) {
4248 if (net_ratelimit())
4249 printk(KERN_CRIT DRV_NAME
4250 ": Can not allocate SKB buffers\n");
4251 /* We don't reschedule replenish work here -- we will
4252 * call the restock method and if it still needs
4253 * more buffers it will schedule replenish */
4254 break;
4255 }
4256 priv->alloc_rxb_skb++;
4257 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004258
4259 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07004260 rxb->dma_addr =
4261 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004262 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004263 list_add_tail(&rxb->list, &rxq->rx_free);
4264 rxq->free_count++;
4265 }
4266 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004267}
4268
4269/*
4270 * this should be called while priv->lock is locked
4271*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02004272static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004273{
4274 struct iwl4965_priv *priv = data;
4275
4276 iwl4965_rx_allocate(priv);
4277 iwl4965_rx_queue_restock(priv);
4278}
4279
4280
4281void iwl4965_rx_replenish(void *data)
4282{
4283 struct iwl4965_priv *priv = data;
4284 unsigned long flags;
4285
4286 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004287
4288 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004289 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004290 spin_unlock_irqrestore(&priv->lock, flags);
4291}
4292
4293/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08004294 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07004295 * This free routine walks the list of POOL entries and if SKB is set to
4296 * non NULL it is unmapped and freed
4297 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004298static void iwl4965_rx_queue_free(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004299{
4300 int i;
4301 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4302 if (rxq->pool[i].skb != NULL) {
4303 pci_unmap_single(priv->pci_dev,
4304 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004305 priv->hw_setting.rx_buf_size,
4306 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004307 dev_kfree_skb(rxq->pool[i].skb);
4308 }
4309 }
4310
4311 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4312 rxq->dma_addr);
4313 rxq->bd = NULL;
4314}
4315
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004316int iwl4965_rx_queue_alloc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004317{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004318 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004319 struct pci_dev *dev = priv->pci_dev;
4320 int i;
4321
4322 spin_lock_init(&rxq->lock);
4323 INIT_LIST_HEAD(&rxq->rx_free);
4324 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004325
4326 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07004327 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4328 if (!rxq->bd)
4329 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004330
Zhu Yib481de92007-09-25 17:54:57 -07004331 /* Fill the rx_used queue with _all_ of the Rx buffers */
4332 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4333 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004334
Zhu Yib481de92007-09-25 17:54:57 -07004335 /* Set us so that we have processed and used all buffers, but have
4336 * not restocked the Rx queue with fresh buffers */
4337 rxq->read = rxq->write = 0;
4338 rxq->free_count = 0;
4339 rxq->need_update = 0;
4340 return 0;
4341}
4342
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004343void iwl4965_rx_queue_reset(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004344{
4345 unsigned long flags;
4346 int i;
4347 spin_lock_irqsave(&rxq->lock, flags);
4348 INIT_LIST_HEAD(&rxq->rx_free);
4349 INIT_LIST_HEAD(&rxq->rx_used);
4350 /* Fill the rx_used queue with _all_ of the Rx buffers */
4351 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4352 /* In the reset function, these buffers may have been allocated
4353 * to an SKB, so we need to unmap and free potential storage */
4354 if (rxq->pool[i].skb != NULL) {
4355 pci_unmap_single(priv->pci_dev,
4356 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004357 priv->hw_setting.rx_buf_size,
4358 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004359 priv->alloc_rxb_skb--;
4360 dev_kfree_skb(rxq->pool[i].skb);
4361 rxq->pool[i].skb = NULL;
4362 }
4363 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4364 }
4365
4366 /* Set us so that we have processed and used all buffers, but have
4367 * not restocked the Rx queue with fresh buffers */
4368 rxq->read = rxq->write = 0;
4369 rxq->free_count = 0;
4370 spin_unlock_irqrestore(&rxq->lock, flags);
4371}
4372
4373/* Convert linear signal-to-noise ratio into dB */
4374static u8 ratio2dB[100] = {
4375/* 0 1 2 3 4 5 6 7 8 9 */
4376 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4377 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4378 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4379 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4380 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4381 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4382 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4383 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4384 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4385 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4386};
4387
4388/* Calculates a relative dB value from a ratio of linear
4389 * (i.e. not dB) signal levels.
4390 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004391int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004392{
Adrian Bunkc899a572007-10-14 19:51:15 +02004393 /* 1000:1 or higher just report as 60 dB */
4394 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004395 return 60;
4396
Adrian Bunkc899a572007-10-14 19:51:15 +02004397 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004398 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004399 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004400 return (20 + (int)ratio2dB[sig_ratio/10]);
4401
4402 /* We shouldn't see this */
4403 if (sig_ratio < 1)
4404 return 0;
4405
4406 /* Use table for ratios 1:1 - 99:1 */
4407 return (int)ratio2dB[sig_ratio];
4408}
4409
4410#define PERFECT_RSSI (-20) /* dBm */
4411#define WORST_RSSI (-95) /* dBm */
4412#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4413
4414/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4415 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4416 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004417int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004418{
4419 int sig_qual;
4420 int degradation = PERFECT_RSSI - rssi_dbm;
4421
4422 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4423 * as indicator; formula is (signal dbm - noise dbm).
4424 * SNR at or above 40 is a great signal (100%).
4425 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4426 * Weakest usable signal is usually 10 - 15 dB SNR. */
4427 if (noise_dbm) {
4428 if (rssi_dbm - noise_dbm >= 40)
4429 return 100;
4430 else if (rssi_dbm < noise_dbm)
4431 return 0;
4432 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4433
4434 /* Else use just the signal level.
4435 * This formula is a least squares fit of data points collected and
4436 * compared with a reference system that had a percentage (%) display
4437 * for signal quality. */
4438 } else
4439 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4440 (15 * RSSI_RANGE + 62 * degradation)) /
4441 (RSSI_RANGE * RSSI_RANGE);
4442
4443 if (sig_qual > 100)
4444 sig_qual = 100;
4445 else if (sig_qual < 1)
4446 sig_qual = 0;
4447
4448 return sig_qual;
4449}
4450
4451/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004452 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004453 *
4454 * Uses the priv->rx_handlers callback function array to invoke
4455 * the appropriate handlers, including command responses,
4456 * frame-received notifications, and other notifications.
4457 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004458static void iwl4965_rx_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004459{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004460 struct iwl4965_rx_mem_buffer *rxb;
4461 struct iwl4965_rx_packet *pkt;
4462 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004463 u32 r, i;
4464 int reclaim;
4465 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004466 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004467 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004468
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004469 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4470 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004471 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004472 i = rxq->read;
4473
4474 /* Rx interrupt, but nothing sent from uCode */
4475 if (i == r)
4476 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4477
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004478 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4479 fill_rx = 1;
4480
Zhu Yib481de92007-09-25 17:54:57 -07004481 while (i != r) {
4482 rxb = rxq->queue[i];
4483
Ben Cahill9fbab512007-11-29 11:09:47 +08004484 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004485 * then a bug has been introduced in the queue refilling
4486 * routines -- catch it here */
4487 BUG_ON(rxb == NULL);
4488
4489 rxq->queue[i] = NULL;
4490
4491 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004492 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004493 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004494 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004495
4496 /* Reclaim a command buffer only if this packet is a response
4497 * to a (driver-originated) command.
4498 * If the packet (e.g. Rx frame) originated from uCode,
4499 * there is no command buffer to reclaim.
4500 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4501 * but apparently a few don't get set; catch them here. */
4502 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4503 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4504 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004505 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004506 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4507 (pkt->hdr.cmd != REPLY_TX);
4508
4509 /* Based on type of command response or notification,
4510 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004511 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004512 if (priv->rx_handlers[pkt->hdr.cmd]) {
4513 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4514 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4515 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4516 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4517 } else {
4518 /* No handling needed */
4519 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4520 "r %d i %d No handler needed for %s, 0x%02x\n",
4521 r, i, get_cmd_string(pkt->hdr.cmd),
4522 pkt->hdr.cmd);
4523 }
4524
4525 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004526 /* Invoke any callbacks, transfer the skb to caller, and
4527 * fire off the (possibly) blocking iwl4965_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004528 * as we reclaim the driver command queue */
4529 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004530 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004531 else
4532 IWL_WARNING("Claim null rxb?\n");
4533 }
4534
4535 /* For now we just don't re-use anything. We can tweak this
4536 * later to try and re-use notification packets and SKBs that
4537 * fail to Rx correctly */
4538 if (rxb->skb != NULL) {
4539 priv->alloc_rxb_skb--;
4540 dev_kfree_skb_any(rxb->skb);
4541 rxb->skb = NULL;
4542 }
4543
4544 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004545 priv->hw_setting.rx_buf_size,
4546 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004547 spin_lock_irqsave(&rxq->lock, flags);
4548 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4549 spin_unlock_irqrestore(&rxq->lock, flags);
4550 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004551 /* If there are a lot of unused frames,
4552 * restock the Rx queue so ucode wont assert. */
4553 if (fill_rx) {
4554 count++;
4555 if (count >= 8) {
4556 priv->rxq.read = i;
4557 __iwl4965_rx_replenish(priv);
4558 count = 0;
4559 }
4560 }
Zhu Yib481de92007-09-25 17:54:57 -07004561 }
4562
4563 /* Backtrack one entry */
4564 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004565 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004566}
4567
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004568/**
4569 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4570 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004571static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
4572 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004573{
4574 u32 reg = 0;
4575 int rc = 0;
4576 int txq_id = txq->q.id;
4577
4578 if (txq->need_update == 0)
4579 return rc;
4580
4581 /* if we're trying to save power */
4582 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4583 /* wake up nic if it's powered down ...
4584 * uCode will wake up, and interrupt us again, so next
4585 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004586 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004587
4588 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4589 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004590 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004591 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4592 return rc;
4593 }
4594
4595 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004596 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004597 if (rc)
4598 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004599 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004600 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004601 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004602
4603 /* else not in power-save mode, uCode will never sleep when we're
4604 * trying to tx (during RFKILL, we're not trying to tx). */
4605 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004606 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004607 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004608
4609 txq->need_update = 0;
4610
4611 return rc;
4612}
4613
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004614#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004615static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004616{
Joe Perches0795af52007-10-03 17:59:30 -07004617 DECLARE_MAC_BUF(mac);
4618
Zhu Yib481de92007-09-25 17:54:57 -07004619 IWL_DEBUG_RADIO("RX CONFIG:\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004620 iwl4965_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004621 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4622 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4623 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4624 le32_to_cpu(rxon->filter_flags));
4625 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4626 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4627 rxon->ofdm_basic_rates);
4628 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004629 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4630 print_mac(mac, rxon->node_addr));
4631 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4632 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004633 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4634}
4635#endif
4636
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004637static void iwl4965_enable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004638{
4639 IWL_DEBUG_ISR("Enabling interrupts\n");
4640 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004641 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004642}
4643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004644static inline void iwl4965_disable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004645{
4646 clear_bit(STATUS_INT_ENABLED, &priv->status);
4647
4648 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004649 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004650
4651 /* acknowledge/clear/reset any interrupts still pending
4652 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004653 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4654 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004655 IWL_DEBUG_ISR("Disabled interrupts\n");
4656}
4657
4658static const char *desc_lookup(int i)
4659{
4660 switch (i) {
4661 case 1:
4662 return "FAIL";
4663 case 2:
4664 return "BAD_PARAM";
4665 case 3:
4666 return "BAD_CHECKSUM";
4667 case 4:
4668 return "NMI_INTERRUPT";
4669 case 5:
4670 return "SYSASSERT";
4671 case 6:
4672 return "FATAL_ERROR";
4673 }
4674
4675 return "UNKNOWN";
4676}
4677
4678#define ERROR_START_OFFSET (1 * sizeof(u32))
4679#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4680
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004681static void iwl4965_dump_nic_error_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004682{
4683 u32 data2, line;
4684 u32 desc, time, count, base, data1;
4685 u32 blink1, blink2, ilink1, ilink2;
4686 int rc;
4687
4688 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4689
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004690 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004691 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4692 return;
4693 }
4694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004695 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004696 if (rc) {
4697 IWL_WARNING("Can not read from adapter at this time.\n");
4698 return;
4699 }
4700
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004701 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004702
4703 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4704 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004705 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004706 }
4707
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004708 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4709 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4710 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4711 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4712 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4713 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4714 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4715 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4716 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004717
4718 IWL_ERROR("Desc Time "
4719 "data1 data2 line\n");
4720 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4721 desc_lookup(desc), desc, time, data1, data2, line);
4722 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4723 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4724 ilink1, ilink2);
4725
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004726 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004727}
4728
4729#define EVENT_START_OFFSET (4 * sizeof(u32))
4730
4731/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004732 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004733 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004734 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004735 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004736static void iwl4965_print_event_log(struct iwl4965_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004737 u32 num_events, u32 mode)
4738{
4739 u32 i;
4740 u32 base; /* SRAM byte address of event log header */
4741 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4742 u32 ptr; /* SRAM byte address of log data */
4743 u32 ev, time, data; /* event log data */
4744
4745 if (num_events == 0)
4746 return;
4747
4748 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4749
4750 if (mode == 0)
4751 event_size = 2 * sizeof(u32);
4752 else
4753 event_size = 3 * sizeof(u32);
4754
4755 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4756
4757 /* "time" is actually "data" for mode 0 (no timestamp).
4758 * place event id # at far right for easier visual parsing. */
4759 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004760 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004761 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004762 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004763 ptr += sizeof(u32);
4764 if (mode == 0)
4765 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4766 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004767 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004768 ptr += sizeof(u32);
4769 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4770 }
4771 }
4772}
4773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004774static void iwl4965_dump_nic_event_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004775{
4776 int rc;
4777 u32 base; /* SRAM byte address of event log header */
4778 u32 capacity; /* event log capacity in # entries */
4779 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4780 u32 num_wraps; /* # times uCode wrapped to top of log */
4781 u32 next_entry; /* index of next entry to be written by uCode */
4782 u32 size; /* # entries that we'll print */
4783
4784 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004785 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004786 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4787 return;
4788 }
4789
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004790 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004791 if (rc) {
4792 IWL_WARNING("Can not read from adapter at this time.\n");
4793 return;
4794 }
4795
4796 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004797 capacity = iwl4965_read_targ_mem(priv, base);
4798 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4799 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4800 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004801
4802 size = num_wraps ? capacity : next_entry;
4803
4804 /* bail out if nothing in log */
4805 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004806 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004807 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004808 return;
4809 }
4810
Zhu Yi583fab32007-09-27 11:27:30 +08004811 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004812 size, num_wraps);
4813
4814 /* if uCode has wrapped back to top of log, start at the oldest entry,
4815 * i.e the next one that uCode would fill. */
4816 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004817 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004818 capacity - next_entry, mode);
4819
4820 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004821 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004823 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004824}
4825
4826/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004827 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004828 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004829static void iwl4965_irq_handle_error(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004830{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004831 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004832 set_bit(STATUS_FW_ERROR, &priv->status);
4833
4834 /* Cancel currently queued command. */
4835 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4836
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004837#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004838 if (iwl4965_debug_level & IWL_DL_FW_ERRORS) {
4839 iwl4965_dump_nic_error_log(priv);
4840 iwl4965_dump_nic_event_log(priv);
4841 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004842 }
4843#endif
4844
4845 wake_up_interruptible(&priv->wait_command_queue);
4846
4847 /* Keep the restart process from trying to send host
4848 * commands by clearing the INIT status bit */
4849 clear_bit(STATUS_READY, &priv->status);
4850
4851 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4852 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4853 "Restarting adapter due to uCode error.\n");
4854
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004855 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004856 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4857 sizeof(priv->recovery_rxon));
4858 priv->error_recovering = 1;
4859 }
4860 queue_work(priv->workqueue, &priv->restart);
4861 }
4862}
4863
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004864static void iwl4965_error_recovery(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004865{
4866 unsigned long flags;
4867
4868 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4869 sizeof(priv->staging_rxon));
4870 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004871 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004872
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004873 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004874
4875 spin_lock_irqsave(&priv->lock, flags);
4876 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4877 priv->error_recovering = 0;
4878 spin_unlock_irqrestore(&priv->lock, flags);
4879}
4880
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004881static void iwl4965_irq_tasklet(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004882{
4883 u32 inta, handled = 0;
4884 u32 inta_fh;
4885 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004886#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004887 u32 inta_mask;
4888#endif
4889
4890 spin_lock_irqsave(&priv->lock, flags);
4891
4892 /* Ack/clear/reset pending uCode interrupts.
4893 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4894 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004895 inta = iwl4965_read32(priv, CSR_INT);
4896 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004897
4898 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4899 * Any new interrupts that happen after this, either while we're
4900 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004901 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
4902 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004903
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004904#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004905 if (iwl4965_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004906 /* just for debug */
4907 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004908 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4909 inta, inta_mask, inta_fh);
4910 }
4911#endif
4912
4913 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4914 * atomic, make sure that inta covers all the interrupts that
4915 * we've discovered, even if FH interrupt came in just after
4916 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004917 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004918 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004919 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004920 inta |= CSR_INT_BIT_FH_TX;
4921
4922 /* Now service all interrupt bits discovered above. */
4923 if (inta & CSR_INT_BIT_HW_ERR) {
4924 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4925
4926 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004927 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004928
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004929 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004930
4931 handled |= CSR_INT_BIT_HW_ERR;
4932
4933 spin_unlock_irqrestore(&priv->lock, flags);
4934
4935 return;
4936 }
4937
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004938#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004939 if (iwl4965_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004940 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004941 if (inta & CSR_INT_BIT_SCD)
4942 IWL_DEBUG_ISR("Scheduler finished to transmit "
4943 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004944
4945 /* Alive notification via Rx interrupt will do the real work */
4946 if (inta & CSR_INT_BIT_ALIVE)
4947 IWL_DEBUG_ISR("Alive interrupt\n");
4948 }
4949#endif
4950 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004951 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004952
Ben Cahill9fbab512007-11-29 11:09:47 +08004953 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004954 if (inta & CSR_INT_BIT_RF_KILL) {
4955 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004956 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004957 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4958 hw_rf_kill = 1;
4959
4960 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4961 "RF_KILL bit toggled to %s.\n",
4962 hw_rf_kill ? "disable radio":"enable radio");
4963
4964 /* Queue restart only if RF_KILL switch was set to "kill"
4965 * when we loaded driver, and is now set to "enable".
4966 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004967 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004968 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4969 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004970 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004971 }
Zhu Yib481de92007-09-25 17:54:57 -07004972
4973 handled |= CSR_INT_BIT_RF_KILL;
4974 }
4975
Ben Cahill9fbab512007-11-29 11:09:47 +08004976 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004977 if (inta & CSR_INT_BIT_CT_KILL) {
4978 IWL_ERROR("Microcode CT kill error detected.\n");
4979 handled |= CSR_INT_BIT_CT_KILL;
4980 }
4981
4982 /* Error detected by uCode */
4983 if (inta & CSR_INT_BIT_SW_ERR) {
4984 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4985 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004986 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004987 handled |= CSR_INT_BIT_SW_ERR;
4988 }
4989
4990 /* uCode wakes up after power-down sleep */
4991 if (inta & CSR_INT_BIT_WAKEUP) {
4992 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004993 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4994 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4995 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4996 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4997 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4998 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4999 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07005000
5001 handled |= CSR_INT_BIT_WAKEUP;
5002 }
5003
5004 /* All uCode command responses, including Tx command responses,
5005 * Rx "responses" (frame-received notification), and other
5006 * notifications from uCode come through here*/
5007 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005008 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005009 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
5010 }
5011
5012 if (inta & CSR_INT_BIT_FH_TX) {
5013 IWL_DEBUG_ISR("Tx interrupt\n");
5014 handled |= CSR_INT_BIT_FH_TX;
5015 }
5016
5017 if (inta & ~handled)
5018 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
5019
5020 if (inta & ~CSR_INI_SET_MASK) {
5021 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
5022 inta & ~CSR_INI_SET_MASK);
5023 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
5024 }
5025
5026 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005027 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005028
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005029#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005030 if (iwl4965_debug_level & (IWL_DL_ISR)) {
5031 inta = iwl4965_read32(priv, CSR_INT);
5032 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
5033 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07005034 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
5035 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
5036 }
5037#endif
5038 spin_unlock_irqrestore(&priv->lock, flags);
5039}
5040
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005041static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07005042{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005043 struct iwl4965_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07005044 u32 inta, inta_mask;
5045 u32 inta_fh;
5046 if (!priv)
5047 return IRQ_NONE;
5048
5049 spin_lock(&priv->lock);
5050
5051 /* Disable (but don't clear!) interrupts here to avoid
5052 * back-to-back ISRs and sporadic interrupts from our NIC.
5053 * If we have something to service, the tasklet will re-enable ints.
5054 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005055 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
5056 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07005057
5058 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005059 inta = iwl4965_read32(priv, CSR_INT);
5060 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07005061
5062 /* Ignore interrupt if there's nothing in NIC to service.
5063 * This may be due to IRQ shared with another device,
5064 * or due to sporadic interrupts thrown from our NIC. */
5065 if (!inta && !inta_fh) {
5066 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
5067 goto none;
5068 }
5069
5070 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08005071 /* Hardware disappeared. It might have already raised
5072 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07005073 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08005074 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07005075 }
5076
5077 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5078 inta, inta_mask, inta_fh);
5079
Joonwoo Park25c03d82008-01-23 10:15:20 -08005080 inta &= ~CSR_INT_BIT_SCD;
5081
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005082 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08005083 if (likely(inta || inta_fh))
5084 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07005085
Oliver Neukum66fbb542007-11-15 09:31:10 +08005086 unplugged:
5087 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07005088 return IRQ_HANDLED;
5089
5090 none:
5091 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005092 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005093 spin_unlock(&priv->lock);
5094 return IRQ_NONE;
5095}
5096
5097/************************** EEPROM BANDS ****************************
5098 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005099 * The iwl4965_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07005100 * EEPROM contents to the specific channel number supported for each
5101 * band.
5102 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005103 * For example, iwl4965_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07005104 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5105 * The specific geography and calibration information for that channel
5106 * is contained in the eeprom map itself.
5107 *
5108 * During init, we copy the eeprom information and channel map
5109 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5110 *
5111 * channel_map_24/52 provides the index in the channel_info array for a
5112 * given channel. We have to have two separate maps as there is channel
5113 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5114 * band_2
5115 *
5116 * A value of 0xff stored in the channel_map indicates that the channel
5117 * is not supported by the hardware at all.
5118 *
5119 * A value of 0xfe in the channel_map indicates that the channel is not
5120 * valid for Tx with the current hardware. This means that
5121 * while the system can tune and receive on a given channel, it may not
5122 * be able to associate or transmit any frames on that
5123 * channel. There is no corresponding channel information for that
5124 * entry.
5125 *
5126 *********************************************************************/
5127
5128/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005129static const u8 iwl4965_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07005130 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5131};
5132
5133/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08005134static const u8 iwl4965_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005135 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5136};
5137
Ben Cahill9fbab512007-11-29 11:09:47 +08005138static const u8 iwl4965_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005139 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5140};
5141
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005142static const u8 iwl4965_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005143 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5144};
5145
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005146static const u8 iwl4965_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005147 145, 149, 153, 157, 161, 165
5148};
5149
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005150static u8 iwl4965_eeprom_band_6[] = { /* 2.4 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005151 1, 2, 3, 4, 5, 6, 7
5152};
5153
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005154static u8 iwl4965_eeprom_band_7[] = { /* 5.2 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005155 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5156};
5157
Ben Cahill9fbab512007-11-29 11:09:47 +08005158static void iwl4965_init_band_reference(const struct iwl4965_priv *priv,
5159 int band,
Zhu Yib481de92007-09-25 17:54:57 -07005160 int *eeprom_ch_count,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005161 const struct iwl4965_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07005162 **eeprom_ch_info,
5163 const u8 **eeprom_ch_index)
5164{
5165 switch (band) {
5166 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005167 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07005168 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005169 *eeprom_ch_index = iwl4965_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07005170 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005171 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005172 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07005173 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005174 *eeprom_ch_index = iwl4965_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07005175 break;
5176 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005177 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07005178 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005179 *eeprom_ch_index = iwl4965_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07005180 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005181 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005182 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07005183 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005184 *eeprom_ch_index = iwl4965_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07005185 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005186 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005187 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005188 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005189 *eeprom_ch_index = iwl4965_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07005190 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005191 case 6: /* 2.4GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005192 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_6);
Zhu Yib481de92007-09-25 17:54:57 -07005193 *eeprom_ch_info = priv->eeprom.band_24_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005194 *eeprom_ch_index = iwl4965_eeprom_band_6;
Zhu Yib481de92007-09-25 17:54:57 -07005195 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005196 case 7: /* 5 GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005197 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_7);
Zhu Yib481de92007-09-25 17:54:57 -07005198 *eeprom_ch_info = priv->eeprom.band_52_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005199 *eeprom_ch_index = iwl4965_eeprom_band_7;
Zhu Yib481de92007-09-25 17:54:57 -07005200 break;
5201 default:
5202 BUG();
5203 return;
5204 }
5205}
5206
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005207/**
5208 * iwl4965_get_channel_info - Find driver's private channel info
5209 *
5210 * Based on band and channel number.
5211 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005212const struct iwl4965_channel_info *iwl4965_get_channel_info(const struct iwl4965_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005213 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07005214{
5215 int i;
5216
Johannes Berg8318d782008-01-24 19:38:38 +01005217 switch (band) {
5218 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07005219 for (i = 14; i < priv->channel_count; i++) {
5220 if (priv->channel_info[i].channel == channel)
5221 return &priv->channel_info[i];
5222 }
5223 break;
Johannes Berg8318d782008-01-24 19:38:38 +01005224 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07005225 if (channel >= 1 && channel <= 14)
5226 return &priv->channel_info[channel - 1];
5227 break;
Johannes Berg8318d782008-01-24 19:38:38 +01005228 default:
5229 BUG();
Zhu Yib481de92007-09-25 17:54:57 -07005230 }
5231
5232 return NULL;
5233}
5234
5235#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5236 ? # x " " : "")
5237
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005238/**
5239 * iwl4965_init_channel_map - Set up driver's info for all possible channels
5240 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005241static int iwl4965_init_channel_map(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005242{
5243 int eeprom_ch_count = 0;
5244 const u8 *eeprom_ch_index = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005245 const struct iwl4965_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005246 int band, ch;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005247 struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005248
5249 if (priv->channel_count) {
5250 IWL_DEBUG_INFO("Channel map already initialized.\n");
5251 return 0;
5252 }
5253
5254 if (priv->eeprom.version < 0x2f) {
5255 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5256 priv->eeprom.version);
5257 return -EINVAL;
5258 }
5259
5260 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5261
5262 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005263 ARRAY_SIZE(iwl4965_eeprom_band_1) +
5264 ARRAY_SIZE(iwl4965_eeprom_band_2) +
5265 ARRAY_SIZE(iwl4965_eeprom_band_3) +
5266 ARRAY_SIZE(iwl4965_eeprom_band_4) +
5267 ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005268
5269 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5270
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005271 priv->channel_info = kzalloc(sizeof(struct iwl4965_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07005272 priv->channel_count, GFP_KERNEL);
5273 if (!priv->channel_info) {
5274 IWL_ERROR("Could not allocate channel_info\n");
5275 priv->channel_count = 0;
5276 return -ENOMEM;
5277 }
5278
5279 ch_info = priv->channel_info;
5280
5281 /* Loop through the 5 EEPROM bands adding them in order to the
5282 * channel map we maintain (that contains additional information than
5283 * what just in the EEPROM) */
5284 for (band = 1; band <= 5; band++) {
5285
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005286 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005287 &eeprom_ch_info, &eeprom_ch_index);
5288
5289 /* Loop through each band adding each of the channels */
5290 for (ch = 0; ch < eeprom_ch_count; ch++) {
5291 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01005292 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
5293 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005294
5295 /* permanently store EEPROM's channel regulatory flags
5296 * and max power in channel info database. */
5297 ch_info->eeprom = eeprom_ch_info[ch];
5298
5299 /* Copy the run-time flags so they are there even on
5300 * invalid channels */
5301 ch_info->flags = eeprom_ch_info[ch].flags;
5302
5303 if (!(is_channel_valid(ch_info))) {
5304 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5305 "No traffic\n",
5306 ch_info->channel,
5307 ch_info->flags,
5308 is_channel_a_band(ch_info) ?
5309 "5.2" : "2.4");
5310 ch_info++;
5311 continue;
5312 }
5313
5314 /* Initialize regulatory-based run-time data */
5315 ch_info->max_power_avg = ch_info->curr_txpow =
5316 eeprom_ch_info[ch].max_power_avg;
5317 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5318 ch_info->min_power = 0;
5319
Tomas Winkler8211ef72008-03-02 01:36:04 +02005320 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07005321 " %ddBm): Ad-Hoc %ssupported\n",
5322 ch_info->channel,
5323 is_channel_a_band(ch_info) ?
5324 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02005325 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07005326 CHECK_AND_PRINT(IBSS),
5327 CHECK_AND_PRINT(ACTIVE),
5328 CHECK_AND_PRINT(RADAR),
5329 CHECK_AND_PRINT(WIDE),
5330 CHECK_AND_PRINT(NARROW),
5331 CHECK_AND_PRINT(DFS),
5332 eeprom_ch_info[ch].flags,
5333 eeprom_ch_info[ch].max_power_avg,
5334 ((eeprom_ch_info[ch].
5335 flags & EEPROM_CHANNEL_IBSS)
5336 && !(eeprom_ch_info[ch].
5337 flags & EEPROM_CHANNEL_RADAR))
5338 ? "" : "not ");
5339
5340 /* Set the user_txpower_limit to the highest power
5341 * supported by any channel */
5342 if (eeprom_ch_info[ch].max_power_avg >
5343 priv->user_txpower_limit)
5344 priv->user_txpower_limit =
5345 eeprom_ch_info[ch].max_power_avg;
5346
5347 ch_info++;
5348 }
5349 }
5350
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005351 /* Two additional EEPROM bands for 2.4 and 5 GHz FAT channels */
Zhu Yib481de92007-09-25 17:54:57 -07005352 for (band = 6; band <= 7; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005353 enum ieee80211_band ieeeband;
Zhu Yib481de92007-09-25 17:54:57 -07005354 u8 fat_extension_chan;
5355
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005356 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005357 &eeprom_ch_info, &eeprom_ch_index);
5358
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005359 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
Johannes Berg8318d782008-01-24 19:38:38 +01005360 ieeeband = (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005361
Zhu Yib481de92007-09-25 17:54:57 -07005362 /* Loop through each band adding each of the channels */
5363 for (ch = 0; ch < eeprom_ch_count; ch++) {
5364
5365 if ((band == 6) &&
5366 ((eeprom_ch_index[ch] == 5) ||
5367 (eeprom_ch_index[ch] == 6) ||
5368 (eeprom_ch_index[ch] == 7)))
5369 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5370 else
5371 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5372
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005373 /* Set up driver's info for lower 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],
5376 &(eeprom_ch_info[ch]),
5377 fat_extension_chan);
5378
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005379 /* Set up driver's info for upper half */
Johannes Berg8318d782008-01-24 19:38:38 +01005380 iwl4965_set_fat_chan_info(priv, ieeeband,
Zhu Yib481de92007-09-25 17:54:57 -07005381 (eeprom_ch_index[ch] + 4),
5382 &(eeprom_ch_info[ch]),
5383 HT_IE_EXT_CHANNEL_BELOW);
5384 }
5385 }
5386
5387 return 0;
5388}
5389
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005390/*
5391 * iwl4965_free_channel_map - undo allocations in iwl4965_init_channel_map
5392 */
5393static void iwl4965_free_channel_map(struct iwl4965_priv *priv)
5394{
5395 kfree(priv->channel_info);
5396 priv->channel_count = 0;
5397}
5398
Zhu Yib481de92007-09-25 17:54:57 -07005399/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5400 * sending probe req. This should be set long enough to hear probe responses
5401 * from more than one AP. */
5402#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5403#define IWL_ACTIVE_DWELL_TIME_52 (10)
5404
5405/* For faster active scanning, scan will move to the next channel if fewer than
5406 * PLCP_QUIET_THRESH packets are heard on this channel within
5407 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5408 * time if it's a quiet channel (nothing responded to our probe, and there's
5409 * no other traffic).
5410 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5411#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5412#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5413
5414/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5415 * Must be set longer than active dwell time.
5416 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5417#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5418#define IWL_PASSIVE_DWELL_TIME_52 (10)
5419#define IWL_PASSIVE_DWELL_BASE (100)
5420#define IWL_CHANNEL_TUNE_TIME 5
5421
Johannes Berg8318d782008-01-24 19:38:38 +01005422static inline u16 iwl4965_get_active_dwell_time(struct iwl4965_priv *priv,
5423 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07005424{
Johannes Berg8318d782008-01-24 19:38:38 +01005425 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005426 return IWL_ACTIVE_DWELL_TIME_52;
5427 else
5428 return IWL_ACTIVE_DWELL_TIME_24;
5429}
5430
Johannes Berg8318d782008-01-24 19:38:38 +01005431static u16 iwl4965_get_passive_dwell_time(struct iwl4965_priv *priv,
5432 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07005433{
Johannes Berg8318d782008-01-24 19:38:38 +01005434 u16 active = iwl4965_get_active_dwell_time(priv, band);
5435 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005436 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5437 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5438
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005439 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005440 /* If we're associated, we clamp the maximum passive
5441 * dwell time to be 98% of the beacon interval (minus
5442 * 2 * channel tune time) */
5443 passive = priv->beacon_int;
5444 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5445 passive = IWL_PASSIVE_DWELL_BASE;
5446 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5447 }
5448
5449 if (passive <= active)
5450 passive = active + 1;
5451
5452 return passive;
5453}
5454
Johannes Berg8318d782008-01-24 19:38:38 +01005455static int iwl4965_get_channels_for_scan(struct iwl4965_priv *priv,
5456 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07005457 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005458 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07005459{
5460 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01005461 const struct ieee80211_supported_band *sband;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005462 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005463 u16 passive_dwell = 0;
5464 u16 active_dwell = 0;
5465 int added, i;
5466
Johannes Berg8318d782008-01-24 19:38:38 +01005467 sband = iwl4965_get_hw_mode(priv, band);
5468 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07005469 return 0;
5470
Johannes Berg8318d782008-01-24 19:38:38 +01005471 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07005472
Johannes Berg8318d782008-01-24 19:38:38 +01005473 active_dwell = iwl4965_get_active_dwell_time(priv, band);
5474 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07005475
Johannes Berg8318d782008-01-24 19:38:38 +01005476 for (i = 0, added = 0; i < sband->n_channels; i++) {
5477 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07005478 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005479 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005480 IWL_DEBUG_SCAN
5481 ("Skipping current channel %d\n",
5482 le16_to_cpu(priv->active_rxon.channel));
5483 continue;
5484 }
5485 } else if (priv->only_active_channel)
5486 continue;
5487
Johannes Berg8318d782008-01-24 19:38:38 +01005488 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07005489
Johannes Berg8318d782008-01-24 19:38:38 +01005490 ch_info = iwl4965_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08005491 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005492 if (!is_channel_valid(ch_info)) {
5493 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5494 scan_ch->channel);
5495 continue;
5496 }
5497
5498 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01005499 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07005500 scan_ch->type = 0; /* passive */
5501 else
5502 scan_ch->type = 1; /* active */
5503
5504 if (scan_ch->type & 1)
5505 scan_ch->type |= (direct_mask << 1);
5506
5507 if (is_channel_narrow(ch_info))
5508 scan_ch->type |= (1 << 7);
5509
5510 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5511 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5512
Ben Cahill9fbab512007-11-29 11:09:47 +08005513 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07005514 scan_ch->tpc.dsp_atten = 110;
5515 /* scan_pwr_info->tpc.dsp_atten; */
5516
5517 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01005518 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005519 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5520 else {
5521 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5522 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08005523 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08005524 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07005525 */
5526 }
5527
5528 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5529 scan_ch->channel,
5530 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5531 (scan_ch->type & 1) ?
5532 active_dwell : passive_dwell);
5533
5534 scan_ch++;
5535 added++;
5536 }
5537
5538 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5539 return added;
5540}
5541
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005542static void iwl4965_init_hw_rates(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005543 struct ieee80211_rate *rates)
5544{
5545 int i;
5546
5547 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005548 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
5549 rates[i].hw_value = i; /* Rate scaling will work on indexes */
5550 rates[i].hw_value_short = i;
5551 rates[i].flags = 0;
5552 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07005553 /*
Johannes Berg8318d782008-01-24 19:38:38 +01005554 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07005555 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005556 rates[i].flags |= (iwl4965_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01005557 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07005558 }
Zhu Yib481de92007-09-25 17:54:57 -07005559 }
Zhu Yib481de92007-09-25 17:54:57 -07005560}
5561
5562/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005563 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005564 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005565static int iwl4965_init_geos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005566{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005567 struct iwl4965_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005568 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07005569 struct ieee80211_channel *channels;
5570 struct ieee80211_channel *geo_ch;
5571 struct ieee80211_rate *rates;
5572 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005573
Johannes Berg8318d782008-01-24 19:38:38 +01005574 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
5575 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07005576 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5577 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5578 return 0;
5579 }
5580
Zhu Yib481de92007-09-25 17:54:57 -07005581 channels = kzalloc(sizeof(struct ieee80211_channel) *
5582 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01005583 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07005584 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005585
Tomas Winkler8211ef72008-03-02 01:36:04 +02005586 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07005587 GFP_KERNEL);
5588 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07005589 kfree(channels);
5590 return -ENOMEM;
5591 }
5592
Zhu Yib481de92007-09-25 17:54:57 -07005593 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02005594 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5595 sband->channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
5596 /* just OFDM */
5597 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
5598 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01005599
Tomas Winkler8211ef72008-03-02 01:36:04 +02005600 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005601
Tomas Winkler8211ef72008-03-02 01:36:04 +02005602 sband = &priv->bands[IEEE80211_BAND_2GHZ];
5603 sband->channels = channels;
5604 /* OFDM & CCK */
5605 sband->bitrates = rates;
5606 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07005607
Tomas Winkler8211ef72008-03-02 01:36:04 +02005608 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005609
Zhu Yib481de92007-09-25 17:54:57 -07005610 priv->ieee_channels = channels;
5611 priv->ieee_rates = rates;
5612
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005613 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005614
Tomas Winkler8211ef72008-03-02 01:36:04 +02005615 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07005616 ch = &priv->channel_info[i];
5617
Tomas Winkler8211ef72008-03-02 01:36:04 +02005618 /* FIXME: might be removed if scan is OK */
5619 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07005620 continue;
Zhu Yib481de92007-09-25 17:54:57 -07005621
Tomas Winkler8211ef72008-03-02 01:36:04 +02005622 if (is_channel_a_band(ch))
5623 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5624 else
5625 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005626
Tomas Winkler8211ef72008-03-02 01:36:04 +02005627 geo_ch = &sband->channels[sband->n_channels++];
5628
5629 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01005630 geo_ch->max_power = ch->max_power_avg;
5631 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08005632 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07005633
5634 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01005635 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
5636 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07005637
Johannes Berg8318d782008-01-24 19:38:38 +01005638 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
5639 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07005640
5641 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01005642 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07005643
5644 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5645 priv->max_channel_txpower_limit =
5646 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005647 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01005648 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005649 }
5650
5651 /* Save flags for reg domain usage */
5652 geo_ch->orig_flags = geo_ch->flags;
5653
5654 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
5655 ch->channel, geo_ch->center_freq,
5656 is_channel_a_band(ch) ? "5.2" : "2.4",
5657 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
5658 "restricted" : "valid",
5659 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07005660 }
5661
Tomas Winkler82b9a122008-03-04 18:09:30 -08005662 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
5663 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07005664 printk(KERN_INFO DRV_NAME
5665 ": Incorrectly detected BG card as ABG. Please send "
5666 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5667 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08005668 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07005669 }
5670
5671 printk(KERN_INFO DRV_NAME
5672 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005673 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5674 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005675
Johannes Berg8318d782008-01-24 19:38:38 +01005676 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->bands[IEEE80211_BAND_2GHZ];
5677 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005678
Zhu Yib481de92007-09-25 17:54:57 -07005679 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5680
5681 return 0;
5682}
5683
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005684/*
5685 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5686 */
5687static void iwl4965_free_geos(struct iwl4965_priv *priv)
5688{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005689 kfree(priv->ieee_channels);
5690 kfree(priv->ieee_rates);
5691 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5692}
5693
Zhu Yib481de92007-09-25 17:54:57 -07005694/******************************************************************************
5695 *
5696 * uCode download functions
5697 *
5698 ******************************************************************************/
5699
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005700static void iwl4965_dealloc_ucode_pci(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005701{
Tomas Winkler98c92212008-01-14 17:46:20 -08005702 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5703 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5704 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5705 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5706 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5707 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005708}
5709
5710/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005711 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005712 * looking at all data.
5713 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02005714static int iwl4965_verify_inst_full(struct iwl4965_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005715 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005716{
5717 u32 val;
5718 u32 save_len = len;
5719 int rc = 0;
5720 u32 errcnt;
5721
5722 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5723
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005724 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005725 if (rc)
5726 return rc;
5727
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005728 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005729
5730 errcnt = 0;
5731 for (; len > 0; len -= sizeof(u32), image++) {
5732 /* read data comes through single port, auto-incr addr */
5733 /* NOTE: Use the debugless read so we don't flood kernel log
5734 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005735 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005736 if (val != le32_to_cpu(*image)) {
5737 IWL_ERROR("uCode INST section is invalid at "
5738 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5739 save_len - len, val, le32_to_cpu(*image));
5740 rc = -EIO;
5741 errcnt++;
5742 if (errcnt >= 20)
5743 break;
5744 }
5745 }
5746
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005747 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005748
5749 if (!errcnt)
5750 IWL_DEBUG_INFO
5751 ("ucode image in INSTRUCTION memory is good\n");
5752
5753 return rc;
5754}
5755
5756
5757/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005758 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005759 * using sample data 100 bytes apart. If these sample points are good,
5760 * it's a pretty good bet that everything between them is good, too.
5761 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005762static int iwl4965_verify_inst_sparse(struct iwl4965_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005763{
5764 u32 val;
5765 int rc = 0;
5766 u32 errcnt = 0;
5767 u32 i;
5768
5769 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005771 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005772 if (rc)
5773 return rc;
5774
5775 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5776 /* read data comes through single port, auto-incr addr */
5777 /* NOTE: Use the debugless read so we don't flood kernel log
5778 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005779 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005780 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005781 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005782 if (val != le32_to_cpu(*image)) {
5783#if 0 /* Enable this if you want to see details */
5784 IWL_ERROR("uCode INST section is invalid at "
5785 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5786 i, val, *image);
5787#endif
5788 rc = -EIO;
5789 errcnt++;
5790 if (errcnt >= 3)
5791 break;
5792 }
5793 }
5794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005795 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005796
5797 return rc;
5798}
5799
5800
5801/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005802 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005803 * and verify its contents
5804 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005805static int iwl4965_verify_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005806{
5807 __le32 *image;
5808 u32 len;
5809 int rc = 0;
5810
5811 /* Try bootstrap */
5812 image = (__le32 *)priv->ucode_boot.v_addr;
5813 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005814 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005815 if (rc == 0) {
5816 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5817 return 0;
5818 }
5819
5820 /* Try initialize */
5821 image = (__le32 *)priv->ucode_init.v_addr;
5822 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005823 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005824 if (rc == 0) {
5825 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5826 return 0;
5827 }
5828
5829 /* Try runtime/protocol */
5830 image = (__le32 *)priv->ucode_code.v_addr;
5831 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005832 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005833 if (rc == 0) {
5834 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5835 return 0;
5836 }
5837
5838 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5839
Ben Cahill9fbab512007-11-29 11:09:47 +08005840 /* Since nothing seems to match, show first several data entries in
5841 * instruction SRAM, so maybe visual inspection will give a clue.
5842 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005843 image = (__le32 *)priv->ucode_boot.v_addr;
5844 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005845 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005846
5847 return rc;
5848}
5849
5850
5851/* check contents of special bootstrap uCode SRAM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005852static int iwl4965_verify_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005853{
5854 __le32 *image = priv->ucode_boot.v_addr;
5855 u32 len = priv->ucode_boot.len;
5856 u32 reg;
5857 u32 val;
5858
5859 IWL_DEBUG_INFO("Begin verify bsm\n");
5860
5861 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005862 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005863 for (reg = BSM_SRAM_LOWER_BOUND;
5864 reg < BSM_SRAM_LOWER_BOUND + len;
5865 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005866 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005867 if (val != le32_to_cpu(*image)) {
5868 IWL_ERROR("BSM uCode verification failed at "
5869 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5870 BSM_SRAM_LOWER_BOUND,
5871 reg - BSM_SRAM_LOWER_BOUND, len,
5872 val, le32_to_cpu(*image));
5873 return -EIO;
5874 }
5875 }
5876
5877 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5878
5879 return 0;
5880}
5881
5882/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005883 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005884 *
5885 * BSM operation:
5886 *
5887 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5888 * in special SRAM that does not power down during RFKILL. When powering back
5889 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5890 * the bootstrap program into the on-board processor, and starts it.
5891 *
5892 * The bootstrap program loads (via DMA) instructions and data for a new
5893 * program from host DRAM locations indicated by the host driver in the
5894 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5895 * automatically.
5896 *
5897 * When initializing the NIC, the host driver points the BSM to the
5898 * "initialize" uCode image. This uCode sets up some internal data, then
5899 * notifies host via "initialize alive" that it is complete.
5900 *
5901 * The host then replaces the BSM_DRAM_* pointer values to point to the
5902 * normal runtime uCode instructions and a backup uCode data cache buffer
5903 * (filled initially with starting data values for the on-board processor),
5904 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5905 * which begins normal operation.
5906 *
5907 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5908 * the backup data cache in DRAM before SRAM is powered down.
5909 *
5910 * When powering back up, the BSM loads the bootstrap program. This reloads
5911 * the runtime uCode instructions and the backup data cache into SRAM,
5912 * and re-launches the runtime uCode from where it left off.
5913 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005914static int iwl4965_load_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005915{
5916 __le32 *image = priv->ucode_boot.v_addr;
5917 u32 len = priv->ucode_boot.len;
5918 dma_addr_t pinst;
5919 dma_addr_t pdata;
5920 u32 inst_len;
5921 u32 data_len;
5922 int rc;
5923 int i;
5924 u32 done;
5925 u32 reg_offset;
5926
5927 IWL_DEBUG_INFO("Begin load bsm\n");
5928
5929 /* make sure bootstrap program is no larger than BSM's SRAM size */
5930 if (len > IWL_MAX_BSM_SIZE)
5931 return -EINVAL;
5932
5933 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005934 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005935 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005936 * after the "initialize" uCode has run, to point to
5937 * runtime/protocol instructions and backup data cache. */
5938 pinst = priv->ucode_init.p_addr >> 4;
5939 pdata = priv->ucode_init_data.p_addr >> 4;
5940 inst_len = priv->ucode_init.len;
5941 data_len = priv->ucode_init_data.len;
5942
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005943 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005944 if (rc)
5945 return rc;
5946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005947 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5948 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5949 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5950 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005951
5952 /* Fill BSM memory with bootstrap instructions */
5953 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5954 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5955 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005956 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005957 le32_to_cpu(*image));
5958
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005959 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005960 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005961 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005962 return rc;
5963 }
5964
5965 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005966 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5967 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005968 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005969 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005970
5971 /* Load bootstrap code into instruction SRAM now,
5972 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005973 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005974 BSM_WR_CTRL_REG_BIT_START);
5975
5976 /* Wait for load of bootstrap uCode to finish */
5977 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005978 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005979 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5980 break;
5981 udelay(10);
5982 }
5983 if (i < 100)
5984 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5985 else {
5986 IWL_ERROR("BSM write did not complete!\n");
5987 return -EIO;
5988 }
5989
5990 /* Enable future boot loads whenever power management unit triggers it
5991 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005992 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005993 BSM_WR_CTRL_REG_BIT_START_EN);
5994
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005995 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005996
5997 return 0;
5998}
5999
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006000static void iwl4965_nic_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006001{
6002 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006003 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006004}
6005
Tomas Winkler90e759d2007-11-29 11:09:41 +08006006
Zhu Yib481de92007-09-25 17:54:57 -07006007/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006008 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07006009 *
6010 * Copy into buffers for card to fetch via bus-mastering
6011 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006012static int iwl4965_read_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006013{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006014 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006015 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006016 const struct firmware *ucode_raw;
Tomas Winkler4bf775cd2008-03-04 18:09:31 -08006017 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07006018 u8 *src;
6019 size_t len;
6020 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
6021
6022 /* Ask kernel firmware_class module to get the boot firmware off disk.
6023 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006024 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
6025 if (ret < 0) {
6026 IWL_ERROR("%s firmware file req failed: Reason %d\n",
6027 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07006028 goto error;
6029 }
6030
6031 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
6032 name, ucode_raw->size);
6033
6034 /* Make sure that we got at least our header! */
6035 if (ucode_raw->size < sizeof(*ucode)) {
6036 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006037 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006038 goto err_release;
6039 }
6040
6041 /* Data from ucode file: header followed by uCode images */
6042 ucode = (void *)ucode_raw->data;
6043
6044 ver = le32_to_cpu(ucode->ver);
6045 inst_size = le32_to_cpu(ucode->inst_size);
6046 data_size = le32_to_cpu(ucode->data_size);
6047 init_size = le32_to_cpu(ucode->init_size);
6048 init_data_size = le32_to_cpu(ucode->init_data_size);
6049 boot_size = le32_to_cpu(ucode->boot_size);
6050
6051 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
6052 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
6053 inst_size);
6054 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
6055 data_size);
6056 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
6057 init_size);
6058 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
6059 init_data_size);
6060 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
6061 boot_size);
6062
6063 /* Verify size of file vs. image size info in file's header */
6064 if (ucode_raw->size < sizeof(*ucode) +
6065 inst_size + data_size + init_size +
6066 init_data_size + boot_size) {
6067
6068 IWL_DEBUG_INFO("uCode file size %d too small\n",
6069 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08006070 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006071 goto err_release;
6072 }
6073
6074 /* Verify that uCode images will fit in card's SRAM */
6075 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08006076 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
6077 inst_size);
6078 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006079 goto err_release;
6080 }
6081
6082 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08006083 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
6084 data_size);
6085 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006086 goto err_release;
6087 }
6088 if (init_size > IWL_MAX_INST_SIZE) {
6089 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006090 ("uCode init instr len %d too large to fit in\n",
6091 init_size);
6092 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006093 goto err_release;
6094 }
6095 if (init_data_size > IWL_MAX_DATA_SIZE) {
6096 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006097 ("uCode init data len %d too large to fit in\n",
6098 init_data_size);
6099 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006100 goto err_release;
6101 }
6102 if (boot_size > IWL_MAX_BSM_SIZE) {
6103 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006104 ("uCode boot instr len %d too large to fit in\n",
6105 boot_size);
6106 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006107 goto err_release;
6108 }
6109
6110 /* Allocate ucode buffers for card's bus-master loading ... */
6111
6112 /* Runtime instructions and 2 copies of data:
6113 * 1) unmodified from disk
6114 * 2) backup cache for save/restore during power-downs */
6115 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006116 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07006117
6118 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006119 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07006120
6121 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006122 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07006123
6124 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006125 if (init_size && init_data_size) {
6126 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006127 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07006128
Tomas Winkler90e759d2007-11-29 11:09:41 +08006129 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006130 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08006131
6132 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
6133 goto err_pci_alloc;
6134 }
Zhu Yib481de92007-09-25 17:54:57 -07006135
6136 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006137 if (boot_size) {
6138 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006139 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07006140
Tomas Winkler90e759d2007-11-29 11:09:41 +08006141 if (!priv->ucode_boot.v_addr)
6142 goto err_pci_alloc;
6143 }
Zhu Yib481de92007-09-25 17:54:57 -07006144
6145 /* Copy images into buffers for card's bus-master reads ... */
6146
6147 /* Runtime instructions (first block of data in file) */
6148 src = &ucode->data[0];
6149 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006150 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006151 memcpy(priv->ucode_code.v_addr, src, len);
6152 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6153 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6154
6155 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006156 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07006157 src = &ucode->data[inst_size];
6158 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006159 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006160 memcpy(priv->ucode_data.v_addr, src, len);
6161 memcpy(priv->ucode_data_backup.v_addr, src, len);
6162
6163 /* Initialization instructions (3rd block) */
6164 if (init_size) {
6165 src = &ucode->data[inst_size + data_size];
6166 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006167 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
6168 len);
Zhu Yib481de92007-09-25 17:54:57 -07006169 memcpy(priv->ucode_init.v_addr, src, len);
6170 }
6171
6172 /* Initialization data (4th block) */
6173 if (init_data_size) {
6174 src = &ucode->data[inst_size + data_size + init_size];
6175 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006176 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
6177 len);
Zhu Yib481de92007-09-25 17:54:57 -07006178 memcpy(priv->ucode_init_data.v_addr, src, len);
6179 }
6180
6181 /* Bootstrap instructions (5th block) */
6182 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6183 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006184 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006185 memcpy(priv->ucode_boot.v_addr, src, len);
6186
6187 /* We have our copies now, allow OS release its copies */
6188 release_firmware(ucode_raw);
6189 return 0;
6190
6191 err_pci_alloc:
6192 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006193 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006194 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006195
6196 err_release:
6197 release_firmware(ucode_raw);
6198
6199 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08006200 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006201}
6202
6203
6204/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006205 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07006206 *
6207 * Tell initialization uCode where to find runtime uCode.
6208 *
6209 * BSM registers initially contain pointers to initialization uCode.
6210 * We need to replace them to load runtime uCode inst and data,
6211 * and to save runtime data when powering down.
6212 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006213static int iwl4965_set_ucode_ptrs(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006214{
6215 dma_addr_t pinst;
6216 dma_addr_t pdata;
6217 int rc = 0;
6218 unsigned long flags;
6219
6220 /* bits 35:4 for 4965 */
6221 pinst = priv->ucode_code.p_addr >> 4;
6222 pdata = priv->ucode_data_backup.p_addr >> 4;
6223
6224 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006225 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006226 if (rc) {
6227 spin_unlock_irqrestore(&priv->lock, flags);
6228 return rc;
6229 }
6230
6231 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006232 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6233 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6234 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006235 priv->ucode_data.len);
6236
6237 /* Inst bytecount must be last to set up, bit 31 signals uCode
6238 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006239 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006240 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006242 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006243
6244 spin_unlock_irqrestore(&priv->lock, flags);
6245
6246 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6247
6248 return rc;
6249}
6250
6251/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006252 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006253 *
6254 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6255 *
6256 * The 4965 "initialize" ALIVE reply contains calibration data for:
6257 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6258 * (3945 does not contain this data).
6259 *
6260 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6261*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006262static void iwl4965_init_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006263{
6264 /* Check alive response for "valid" sign from uCode */
6265 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6266 /* We had an error bringing up the hardware, so take it
6267 * all the way back down so we can try again */
6268 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6269 goto restart;
6270 }
6271
6272 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6273 * This is a paranoid check, because we would not have gotten the
6274 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006275 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006276 /* Runtime instruction load was bad;
6277 * take it all the way back down so we can try again */
6278 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6279 goto restart;
6280 }
6281
6282 /* Calculate temperature */
6283 priv->temperature = iwl4965_get_temperature(priv);
6284
6285 /* Send pointers to protocol/runtime uCode image ... init code will
6286 * load and launch runtime uCode, which will send us another "Alive"
6287 * notification. */
6288 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006289 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006290 /* Runtime instruction load won't happen;
6291 * take it all the way back down so we can try again */
6292 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6293 goto restart;
6294 }
6295 return;
6296
6297 restart:
6298 queue_work(priv->workqueue, &priv->restart);
6299}
6300
6301
6302/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006303 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006304 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006305 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07006306 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006307static void iwl4965_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006308{
6309 int rc = 0;
6310
6311 IWL_DEBUG_INFO("Runtime Alive received.\n");
6312
6313 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6314 /* We had an error bringing up the hardware, so take it
6315 * all the way back down so we can try again */
6316 IWL_DEBUG_INFO("Alive failed.\n");
6317 goto restart;
6318 }
6319
6320 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6321 * This is a paranoid check, because we would not have gotten the
6322 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006323 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006324 /* Runtime instruction load was bad;
6325 * take it all the way back down so we can try again */
6326 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6327 goto restart;
6328 }
6329
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006330 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006331
6332 rc = iwl4965_alive_notify(priv);
6333 if (rc) {
6334 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6335 rc);
6336 goto restart;
6337 }
6338
Ben Cahill9fbab512007-11-29 11:09:47 +08006339 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07006340 set_bit(STATUS_ALIVE, &priv->status);
6341
6342 /* Clear out the uCode error bit if it is set */
6343 clear_bit(STATUS_FW_ERROR, &priv->status);
6344
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006345 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006346 return;
6347
Zhu Yi5a669262008-01-14 17:46:18 -08006348 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006349
6350 priv->active_rate = priv->rates_mask;
6351 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006353 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07006354
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006355 if (iwl4965_is_associated(priv)) {
6356 struct iwl4965_rxon_cmd *active_rxon =
6357 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07006358
6359 memcpy(&priv->staging_rxon, &priv->active_rxon,
6360 sizeof(priv->staging_rxon));
6361 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6362 } else {
6363 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006364 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006365 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6366 }
6367
Ben Cahill9fbab512007-11-29 11:09:47 +08006368 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006369 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006370
6371 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006372 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006373
6374 /* At this point, the NIC is initialized and operational */
6375 priv->notif_missed_beacons = 0;
6376 set_bit(STATUS_READY, &priv->status);
6377
6378 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08006379
Zhu Yib481de92007-09-25 17:54:57 -07006380 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006381 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07006382
6383 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006384 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006385
6386 return;
6387
6388 restart:
6389 queue_work(priv->workqueue, &priv->restart);
6390}
6391
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006392static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07006393
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006394static void __iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006395{
6396 unsigned long flags;
6397 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6398 struct ieee80211_conf *conf = NULL;
6399
6400 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6401
6402 conf = ieee80211_get_hw_conf(priv->hw);
6403
6404 if (!exit_pending)
6405 set_bit(STATUS_EXIT_PENDING, &priv->status);
6406
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006407 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006408
6409 /* Unblock any waiting calls */
6410 wake_up_interruptible_all(&priv->wait_command_queue);
6411
Zhu Yib481de92007-09-25 17:54:57 -07006412 /* Wipe out the EXIT_PENDING status bit if we are not actually
6413 * exiting the module */
6414 if (!exit_pending)
6415 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6416
6417 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006418 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07006419
6420 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006421 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006422
6423 if (priv->mac80211_registered)
6424 ieee80211_stop_queues(priv->hw);
6425
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006426 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07006427 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006428 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006429 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6430 STATUS_RF_KILL_HW |
6431 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6432 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08006433 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6434 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07006435 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6436 STATUS_IN_SUSPEND;
6437 goto exit;
6438 }
6439
6440 /* ...otherwise clear out all the status bits but the RF Kill and
6441 * SUSPEND bits and continue taking the NIC down. */
6442 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6443 STATUS_RF_KILL_HW |
6444 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6445 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08006446 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6447 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07006448 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6449 STATUS_IN_SUSPEND |
6450 test_bit(STATUS_FW_ERROR, &priv->status) <<
6451 STATUS_FW_ERROR;
6452
6453 spin_lock_irqsave(&priv->lock, flags);
Ben Cahill9fbab512007-11-29 11:09:47 +08006454 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
6455 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07006456 spin_unlock_irqrestore(&priv->lock, flags);
6457
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006458 iwl4965_hw_txq_ctx_stop(priv);
6459 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006460
6461 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006462 if (!iwl4965_grab_nic_access(priv)) {
6463 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006464 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006465 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006466 }
6467 spin_unlock_irqrestore(&priv->lock, flags);
6468
6469 udelay(5);
6470
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006471 iwl4965_hw_nic_stop_master(priv);
6472 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6473 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006474
6475 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006476 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07006477
6478 if (priv->ibss_beacon)
6479 dev_kfree_skb(priv->ibss_beacon);
6480 priv->ibss_beacon = NULL;
6481
6482 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006483 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006484}
6485
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006486static void iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006487{
6488 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006489 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006490 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006491
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006492 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006493}
6494
6495#define MAX_HW_RESTARTS 5
6496
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006497static int __iwl4965_up(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006498{
6499 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07006500
6501 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6502 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6503 return -EIO;
6504 }
6505
6506 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6507 IWL_WARNING("Radio disabled by SW RF kill (module "
6508 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08006509 return -ENODEV;
6510 }
6511
Reinette Chatree903fbd2008-01-30 22:05:15 -08006512 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6513 IWL_ERROR("ucode not available for device bringup\n");
6514 return -EIO;
6515 }
6516
Zhu Yie655b9f2008-01-24 02:19:38 -08006517 /* If platform's RF_KILL switch is NOT set to KILL */
6518 if (iwl4965_read32(priv, CSR_GP_CNTRL) &
6519 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
6520 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6521 else {
6522 set_bit(STATUS_RF_KILL_HW, &priv->status);
6523 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
6524 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6525 return -ENODEV;
6526 }
Zhu Yib481de92007-09-25 17:54:57 -07006527 }
6528
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006529 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006530
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006531 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006532 if (rc) {
6533 IWL_ERROR("Unable to int nic\n");
6534 return rc;
6535 }
6536
6537 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006538 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6539 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006540 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6541
6542 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006543 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6544 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006545
6546 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006547 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6548 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006549
6550 /* Copy original ucode data image from disk into backup cache.
6551 * This will be used to initialize the on-board processor's
6552 * data SRAM for a clean start when the runtime program first loads. */
6553 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08006554 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07006555
Zhu Yie655b9f2008-01-24 02:19:38 -08006556 /* We return success when we resume from suspend and rf_kill is on. */
6557 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006558 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006559
6560 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6561
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006562 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006563
6564 /* load bootstrap state machine,
6565 * load bootstrap program into processor's memory,
6566 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006567 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006568
6569 if (rc) {
6570 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6571 continue;
6572 }
6573
6574 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006575 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006576
Zhu Yib481de92007-09-25 17:54:57 -07006577 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6578
6579 return 0;
6580 }
6581
6582 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006583 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006584
6585 /* tried to restart and config the device for as long as our
6586 * patience could withstand */
6587 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6588 return -EIO;
6589}
6590
6591
6592/*****************************************************************************
6593 *
6594 * Workqueue callbacks
6595 *
6596 *****************************************************************************/
6597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006598static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006599{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006600 struct iwl4965_priv *priv =
6601 container_of(data, struct iwl4965_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006602
6603 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6604 return;
6605
6606 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006607 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006608 mutex_unlock(&priv->mutex);
6609}
6610
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006611static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006612{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006613 struct iwl4965_priv *priv =
6614 container_of(data, struct iwl4965_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006615
6616 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6617 return;
6618
6619 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006620 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006621 mutex_unlock(&priv->mutex);
6622}
6623
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006624static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006625{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006626 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006627
6628 wake_up_interruptible(&priv->wait_command_queue);
6629
6630 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6631 return;
6632
6633 mutex_lock(&priv->mutex);
6634
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006635 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006636 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6637 "HW and/or SW RF Kill no longer active, restarting "
6638 "device\n");
6639 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6640 queue_work(priv->workqueue, &priv->restart);
6641 } else {
6642
6643 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6644 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6645 "disabled by SW switch\n");
6646 else
6647 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6648 "Kill switch must be turned off for "
6649 "wireless networking to work.\n");
6650 }
6651 mutex_unlock(&priv->mutex);
6652}
6653
6654#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6655
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006656static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006657{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006658 struct iwl4965_priv *priv =
6659 container_of(data, struct iwl4965_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006660
6661 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6662 return;
6663
6664 mutex_lock(&priv->mutex);
6665 if (test_bit(STATUS_SCANNING, &priv->status) ||
6666 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6667 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6668 "Scan completion watchdog resetting adapter (%dms)\n",
6669 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006670
Zhu Yib481de92007-09-25 17:54:57 -07006671 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006672 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006673 }
6674 mutex_unlock(&priv->mutex);
6675}
6676
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006677static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006678{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006679 struct iwl4965_priv *priv =
6680 container_of(data, struct iwl4965_priv, request_scan);
6681 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006682 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006683 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006684 .meta.flags = CMD_SIZE_HUGE,
6685 };
6686 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006687 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006688 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006689 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006690 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006691 u8 direct_mask;
Zhu Yib481de92007-09-25 17:54:57 -07006692
6693 conf = ieee80211_get_hw_conf(priv->hw);
6694
6695 mutex_lock(&priv->mutex);
6696
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006697 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006698 IWL_WARNING("request scan called when driver not ready.\n");
6699 goto done;
6700 }
6701
6702 /* Make sure the scan wasn't cancelled before this queued work
6703 * was given the chance to run... */
6704 if (!test_bit(STATUS_SCANNING, &priv->status))
6705 goto done;
6706
6707 /* This should never be called or scheduled if there is currently
6708 * a scan active in the hardware. */
6709 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6710 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6711 "Ignoring second request.\n");
6712 rc = -EIO;
6713 goto done;
6714 }
6715
6716 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6717 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6718 goto done;
6719 }
6720
6721 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6722 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6723 goto done;
6724 }
6725
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006726 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006727 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6728 goto done;
6729 }
6730
6731 if (!test_bit(STATUS_READY, &priv->status)) {
6732 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6733 goto done;
6734 }
6735
6736 if (!priv->scan_bands) {
6737 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6738 goto done;
6739 }
6740
6741 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006742 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006743 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6744 if (!priv->scan) {
6745 rc = -ENOMEM;
6746 goto done;
6747 }
6748 }
6749 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006750 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006751
6752 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6753 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6754
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006755 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006756 u16 interval = 0;
6757 u32 extra;
6758 u32 suspend_time = 100;
6759 u32 scan_suspend_time = 100;
6760 unsigned long flags;
6761
6762 IWL_DEBUG_INFO("Scanning while associated...\n");
6763
6764 spin_lock_irqsave(&priv->lock, flags);
6765 interval = priv->beacon_int;
6766 spin_unlock_irqrestore(&priv->lock, flags);
6767
6768 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006769 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006770 if (!interval)
6771 interval = suspend_time;
6772
6773 extra = (suspend_time / interval) << 22;
6774 scan_suspend_time = (extra |
6775 ((suspend_time % interval) * 1024));
6776 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6777 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6778 scan_suspend_time, interval);
6779 }
6780
6781 /* We should add the ability for user to lock to PASSIVE ONLY */
6782 if (priv->one_direct_scan) {
6783 IWL_DEBUG_SCAN
6784 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006785 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006786 priv->direct_ssid_len));
6787 scan->direct_scan[0].id = WLAN_EID_SSID;
6788 scan->direct_scan[0].len = priv->direct_ssid_len;
6789 memcpy(scan->direct_scan[0].ssid,
6790 priv->direct_ssid, priv->direct_ssid_len);
6791 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006792 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006793 scan->direct_scan[0].id = WLAN_EID_SSID;
6794 scan->direct_scan[0].len = priv->essid_len;
6795 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6796 direct_mask = 1;
6797 } else
6798 direct_mask = 0;
6799
Zhu Yib481de92007-09-25 17:54:57 -07006800 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6801 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6802 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6803
Zhu Yib481de92007-09-25 17:54:57 -07006804
6805 switch (priv->scan_bands) {
6806 case 2:
6807 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6808 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006809 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006810 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6811
6812 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006813 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006814 break;
6815
6816 case 1:
6817 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006818 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006819 RATE_MCS_ANT_B_MSK);
6820 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006821 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006822 break;
6823
6824 default:
6825 IWL_WARNING("Invalid scan band count\n");
6826 goto done;
6827 }
6828
Tomas Winkler78330fd2008-02-06 02:37:18 +02006829 /* We don't build a direct scan probe request; the uCode will do
6830 * that based on the direct_mask added to each channel entry */
6831 cmd_len = iwl4965_fill_probe_req(priv, band,
6832 (struct ieee80211_mgmt *)scan->data,
6833 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6834
6835 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006836 /* select Rx chains */
6837
6838 /* Force use of chains B and C (0x6) for scan Rx.
6839 * Avoid A (0x1) because of its off-channel reception on A-band.
6840 * MIMO is not used here, but value is required to make uCode happy. */
6841 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6842 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6843 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6844 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6845
6846 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6847 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6848
6849 if (direct_mask)
6850 IWL_DEBUG_SCAN
6851 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006852 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07006853 else
6854 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
6855
6856 scan->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006857 iwl4965_get_channels_for_scan(
Johannes Berg8318d782008-01-24 19:38:38 +01006858 priv, band, 1, /* active */
Zhu Yib481de92007-09-25 17:54:57 -07006859 direct_mask,
6860 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6861
6862 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006863 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006864 cmd.data = scan;
6865 scan->len = cpu_to_le16(cmd.len);
6866
6867 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006868 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006869 if (rc)
6870 goto done;
6871
6872 queue_delayed_work(priv->workqueue, &priv->scan_check,
6873 IWL_SCAN_CHECK_WATCHDOG);
6874
6875 mutex_unlock(&priv->mutex);
6876 return;
6877
6878 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006879 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006880 queue_work(priv->workqueue, &priv->scan_completed);
6881 mutex_unlock(&priv->mutex);
6882}
6883
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006884static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006885{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006886 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006887
6888 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6889 return;
6890
6891 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006892 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006893 mutex_unlock(&priv->mutex);
6894}
6895
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006896static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006897{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006898 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006899
6900 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6901 return;
6902
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006903 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006904 queue_work(priv->workqueue, &priv->up);
6905}
6906
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006907static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006908{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006909 struct iwl4965_priv *priv =
6910 container_of(data, struct iwl4965_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006911
6912 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6913 return;
6914
6915 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006916 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006917 mutex_unlock(&priv->mutex);
6918}
6919
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006920#define IWL_DELAY_NEXT_SCAN (HZ*2)
6921
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006922static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006923{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006924 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006925 post_associate.work);
6926
6927 int rc = 0;
6928 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07006929 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006930
6931 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6932 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6933 return;
6934 }
6935
Joe Perches0795af52007-10-03 17:59:30 -07006936 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6937 priv->assoc_id,
6938 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006939
6940
6941 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6942 return;
6943
6944 mutex_lock(&priv->mutex);
6945
Johannes Berg32bfd352007-12-19 01:31:26 +01006946 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006947 mutex_unlock(&priv->mutex);
6948 return;
6949 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006950 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006951
Zhu Yib481de92007-09-25 17:54:57 -07006952 conf = ieee80211_get_hw_conf(priv->hw);
6953
6954 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006955 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006956
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006957 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6958 iwl4965_setup_rxon_timing(priv);
6959 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006960 sizeof(priv->rxon_timing), &priv->rxon_timing);
6961 if (rc)
6962 IWL_WARNING("REPLY_RXON_TIMING failed - "
6963 "Attempting to continue.\n");
6964
6965 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6966
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006967#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006968 if (priv->current_ht_config.is_ht)
6969 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006970#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006971 iwl4965_set_rxon_chain(priv);
6972 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6973
6974 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6975 priv->assoc_id, priv->beacon_int);
6976
6977 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6978 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6979 else
6980 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6981
6982 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6983 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6984 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6985 else
6986 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6987
6988 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6989 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6990
6991 }
6992
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006993 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006994
6995 switch (priv->iw_mode) {
6996 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006997 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006998 break;
6999
7000 case IEEE80211_IF_TYPE_IBSS:
7001
7002 /* clear out the station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007003 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007004
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007005 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
7006 iwl4965_rxon_add_station(priv, priv->bssid, 0);
7007 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
7008 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007009
7010 break;
7011
7012 default:
7013 IWL_ERROR("%s Should not be called in %d mode\n",
7014 __FUNCTION__, priv->iw_mode);
7015 break;
7016 }
7017
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007018 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007019
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007020#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07007021 /* Enable Rx differential gain and sensitivity calibrations */
7022 iwl4965_chain_noise_reset(priv);
7023 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007024#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07007025
7026 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7027 priv->assoc_station_added = 1;
7028
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007029 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08007030
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007031 /* we have just associated, don't start scan too early */
7032 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07007033 mutex_unlock(&priv->mutex);
7034}
7035
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007036static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07007037{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007038 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07007039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007040 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007041 return;
7042
7043 mutex_lock(&priv->mutex);
7044
7045 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007046 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007047
7048 mutex_unlock(&priv->mutex);
7049}
7050
Zhu Yi76bb77e2007-11-22 10:53:22 +08007051static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
7052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007053static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07007054{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007055 struct iwl4965_priv *priv =
7056 container_of(work, struct iwl4965_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07007057
7058 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
7059
7060 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7061 return;
7062
Zhu Yia0646472007-12-20 14:10:01 +08007063 if (test_bit(STATUS_CONF_PENDING, &priv->status))
7064 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08007065
Zhu Yib481de92007-09-25 17:54:57 -07007066 ieee80211_scan_completed(priv->hw);
7067
7068 /* Since setting the TXPOWER may have been deferred while
7069 * performing the scan, fire one off */
7070 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007071 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007072 mutex_unlock(&priv->mutex);
7073}
7074
7075/*****************************************************************************
7076 *
7077 * mac80211 entry point functions
7078 *
7079 *****************************************************************************/
7080
Zhu Yi5a669262008-01-14 17:46:18 -08007081#define UCODE_READY_TIMEOUT (2 * HZ)
7082
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007083static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007084{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007085 struct iwl4965_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08007086 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07007087
7088 IWL_DEBUG_MAC80211("enter\n");
7089
Zhu Yi5a669262008-01-14 17:46:18 -08007090 if (pci_enable_device(priv->pci_dev)) {
7091 IWL_ERROR("Fail to pci_enable_device\n");
7092 return -ENODEV;
7093 }
7094 pci_restore_state(priv->pci_dev);
7095 pci_enable_msi(priv->pci_dev);
7096
7097 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
7098 DRV_NAME, priv);
7099 if (ret) {
7100 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
7101 goto out_disable_msi;
7102 }
7103
Zhu Yib481de92007-09-25 17:54:57 -07007104 /* we should be verifying the device is ready to be opened */
7105 mutex_lock(&priv->mutex);
7106
Zhu Yi5a669262008-01-14 17:46:18 -08007107 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
7108 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
7109 * ucode filename and max sizes are card-specific. */
7110
7111 if (!priv->ucode_code.len) {
7112 ret = iwl4965_read_ucode(priv);
7113 if (ret) {
7114 IWL_ERROR("Could not read microcode: %d\n", ret);
7115 mutex_unlock(&priv->mutex);
7116 goto out_release_irq;
7117 }
7118 }
7119
Zhu Yie655b9f2008-01-24 02:19:38 -08007120 ret = __iwl4965_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08007121
Zhu Yib481de92007-09-25 17:54:57 -07007122 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08007123
Zhu Yie655b9f2008-01-24 02:19:38 -08007124 if (ret)
7125 goto out_release_irq;
7126
7127 IWL_DEBUG_INFO("Start UP work done.\n");
7128
7129 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
7130 return 0;
7131
Zhu Yi5a669262008-01-14 17:46:18 -08007132 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
7133 * mac80211 will not be run successfully. */
7134 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
7135 test_bit(STATUS_READY, &priv->status),
7136 UCODE_READY_TIMEOUT);
7137 if (!ret) {
7138 if (!test_bit(STATUS_READY, &priv->status)) {
7139 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
7140 jiffies_to_msecs(UCODE_READY_TIMEOUT));
7141 ret = -ETIMEDOUT;
7142 goto out_release_irq;
7143 }
7144 }
7145
Zhu Yie655b9f2008-01-24 02:19:38 -08007146 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07007147 IWL_DEBUG_MAC80211("leave\n");
7148 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08007149
7150out_release_irq:
7151 free_irq(priv->pci_dev->irq, priv);
7152out_disable_msi:
7153 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08007154 pci_disable_device(priv->pci_dev);
7155 priv->is_open = 0;
7156 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007157 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007158}
7159
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007160static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007161{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007162 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007163
7164 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08007165
Zhu Yie655b9f2008-01-24 02:19:38 -08007166 if (!priv->is_open) {
7167 IWL_DEBUG_MAC80211("leave - skip\n");
7168 return;
7169 }
7170
Zhu Yib481de92007-09-25 17:54:57 -07007171 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08007172
7173 if (iwl4965_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08007174 /* stop mac, cancel any scan request and clear
7175 * RXON_FILTER_ASSOC_MSK BIT
7176 */
Zhu Yi5a669262008-01-14 17:46:18 -08007177 mutex_lock(&priv->mutex);
7178 iwl4965_scan_cancel_timeout(priv, 100);
7179 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08007180 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08007181 }
7182
Zhu Yi5a669262008-01-14 17:46:18 -08007183 iwl4965_down(priv);
7184
7185 flush_workqueue(priv->workqueue);
7186 free_irq(priv->pci_dev->irq, priv);
7187 pci_disable_msi(priv->pci_dev);
7188 pci_save_state(priv->pci_dev);
7189 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007190
Zhu Yib481de92007-09-25 17:54:57 -07007191 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007192}
7193
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007194static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007195 struct ieee80211_tx_control *ctl)
7196{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007197 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007198
7199 IWL_DEBUG_MAC80211("enter\n");
7200
7201 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7202 IWL_DEBUG_MAC80211("leave - monitor\n");
7203 return -1;
7204 }
7205
7206 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01007207 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07007208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007209 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07007210 dev_kfree_skb_any(skb);
7211
7212 IWL_DEBUG_MAC80211("leave\n");
7213 return 0;
7214}
7215
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007216static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007217 struct ieee80211_if_init_conf *conf)
7218{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007219 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007220 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07007221 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007222
Johannes Berg32bfd352007-12-19 01:31:26 +01007223 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007224
Johannes Berg32bfd352007-12-19 01:31:26 +01007225 if (priv->vif) {
7226 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08007227 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07007228 }
7229
7230 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01007231 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07007232
7233 spin_unlock_irqrestore(&priv->lock, flags);
7234
7235 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02007236
7237 if (conf->mac_addr) {
7238 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7239 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7240 }
Zhu Yib481de92007-09-25 17:54:57 -07007241
Zhu Yi5a669262008-01-14 17:46:18 -08007242 if (iwl4965_is_ready(priv))
7243 iwl4965_set_mode(priv, conf->type);
7244
Zhu Yib481de92007-09-25 17:54:57 -07007245 mutex_unlock(&priv->mutex);
7246
Zhu Yi5a669262008-01-14 17:46:18 -08007247 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007248 return 0;
7249}
7250
7251/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007252 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07007253 *
7254 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7255 * be set inappropriately and the driver currently sets the hardware up to
7256 * use it whenever needed.
7257 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007258static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007259{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007260 struct iwl4965_priv *priv = hw->priv;
7261 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07007262 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08007263 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007264
7265 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01007266 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07007267
Zhu Yi12342c42007-12-20 11:27:32 +08007268 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
7269
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007270 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007271 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007272 ret = -EIO;
7273 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007274 }
7275
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007276 if (unlikely(!iwl4965_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07007277 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08007278 IWL_DEBUG_MAC80211("leave - scanning\n");
7279 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007280 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08007281 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007282 }
7283
7284 spin_lock_irqsave(&priv->lock, flags);
7285
Johannes Berg8318d782008-01-24 19:38:38 +01007286 ch_info = iwl4965_get_channel_info(priv, conf->channel->band,
7287 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07007288 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07007289 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7290 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007291 ret = -EINVAL;
7292 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007293 }
7294
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007295#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02007296 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07007297 * from any ht related info since 2.4 does not
7298 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02007299 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07007300#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7301 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7302#endif
7303 )
7304 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007305#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007306
Johannes Berg8318d782008-01-24 19:38:38 +01007307 iwl4965_set_rxon_channel(priv, conf->channel->band,
7308 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07007309
Johannes Berg8318d782008-01-24 19:38:38 +01007310 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07007311
7312 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01007313 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07007314 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007315 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007316
7317 spin_unlock_irqrestore(&priv->lock, flags);
7318
7319#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7320 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007321 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007322 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007323 }
7324#endif
7325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007326 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07007327
7328 if (!conf->radio_enabled) {
7329 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007330 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007331 }
7332
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007333 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007334 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007335 ret = -EIO;
7336 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007337 }
7338
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007339 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007340
7341 if (memcmp(&priv->active_rxon,
7342 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007343 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007344 else
7345 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7346
7347 IWL_DEBUG_MAC80211("leave\n");
7348
Zhu Yia0646472007-12-20 14:10:01 +08007349out:
7350 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08007351 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007352 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007353}
7354
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007355static void iwl4965_config_ap(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007356{
7357 int rc = 0;
7358
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08007359 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07007360 return;
7361
7362 /* The following should be done only at AP bring up */
7363 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7364
7365 /* RXON - unassoc (to set timing command) */
7366 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007367 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007368
7369 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007370 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7371 iwl4965_setup_rxon_timing(priv);
7372 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007373 sizeof(priv->rxon_timing), &priv->rxon_timing);
7374 if (rc)
7375 IWL_WARNING("REPLY_RXON_TIMING failed - "
7376 "Attempting to continue.\n");
7377
7378 iwl4965_set_rxon_chain(priv);
7379
7380 /* FIXME: what should be the assoc_id for AP? */
7381 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7382 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7383 priv->staging_rxon.flags |=
7384 RXON_FLG_SHORT_PREAMBLE_MSK;
7385 else
7386 priv->staging_rxon.flags &=
7387 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7388
7389 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7390 if (priv->assoc_capability &
7391 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7392 priv->staging_rxon.flags |=
7393 RXON_FLG_SHORT_SLOT_MSK;
7394 else
7395 priv->staging_rxon.flags &=
7396 ~RXON_FLG_SHORT_SLOT_MSK;
7397
7398 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7399 priv->staging_rxon.flags &=
7400 ~RXON_FLG_SHORT_SLOT_MSK;
7401 }
7402 /* restore RXON assoc */
7403 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007404 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007405 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007406 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08007407 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007408 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007409
7410 /* FIXME - we need to add code here to detect a totally new
7411 * configuration, reset the AP, unassoc, rxon timing, assoc,
7412 * clear sta table, add BCAST sta... */
7413}
7414
Johannes Berg32bfd352007-12-19 01:31:26 +01007415static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
7416 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07007417 struct ieee80211_if_conf *conf)
7418{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007419 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007420 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007421 unsigned long flags;
7422 int rc;
7423
7424 if (conf == NULL)
7425 return -EIO;
7426
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08007427 if (priv->vif != vif) {
7428 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
7429 mutex_unlock(&priv->mutex);
7430 return 0;
7431 }
7432
Zhu Yib481de92007-09-25 17:54:57 -07007433 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7434 (!conf->beacon || !conf->ssid_len)) {
7435 IWL_DEBUG_MAC80211
7436 ("Leaving in AP mode because HostAPD is not ready.\n");
7437 return 0;
7438 }
7439
Zhu Yi5a669262008-01-14 17:46:18 -08007440 if (!iwl4965_is_alive(priv))
7441 return -EAGAIN;
7442
Zhu Yib481de92007-09-25 17:54:57 -07007443 mutex_lock(&priv->mutex);
7444
Zhu Yib481de92007-09-25 17:54:57 -07007445 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007446 IWL_DEBUG_MAC80211("bssid: %s\n",
7447 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007448
Johannes Berg4150c572007-09-17 01:29:23 -04007449/*
7450 * very dubious code was here; the probe filtering flag is never set:
7451 *
Zhu Yib481de92007-09-25 17:54:57 -07007452 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7453 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007454 */
Zhu Yib481de92007-09-25 17:54:57 -07007455
7456 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7457 if (!conf->bssid) {
7458 conf->bssid = priv->mac_addr;
7459 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007460 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7461 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007462 }
7463 if (priv->ibss_beacon)
7464 dev_kfree_skb(priv->ibss_beacon);
7465
7466 priv->ibss_beacon = conf->beacon;
7467 }
7468
Mohamed Abbasfde35712007-11-29 11:10:15 +08007469 if (iwl4965_is_rfkill(priv))
7470 goto done;
7471
Zhu Yib481de92007-09-25 17:54:57 -07007472 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7473 !is_multicast_ether_addr(conf->bssid)) {
7474 /* If there is currently a HW scan going on in the background
7475 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007476 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007477 IWL_WARNING("Aborted scan still in progress "
7478 "after 100ms\n");
7479 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7480 mutex_unlock(&priv->mutex);
7481 return -EAGAIN;
7482 }
7483 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7484
7485 /* TODO: Audit driver for usage of these members and see
7486 * if mac80211 deprecates them (priv->bssid looks like it
7487 * shouldn't be there, but I haven't scanned the IBSS code
7488 * to verify) - jpk */
7489 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7490
7491 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007492 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007493 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007494 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007495 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007496 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07007497 priv, priv->active_rxon.bssid_addr, 1);
7498 }
7499
7500 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007501 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007502 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007503 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007504 }
7505
Mohamed Abbasfde35712007-11-29 11:10:15 +08007506 done:
Zhu Yib481de92007-09-25 17:54:57 -07007507 spin_lock_irqsave(&priv->lock, flags);
7508 if (!conf->ssid_len)
7509 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7510 else
7511 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7512
7513 priv->essid_len = conf->ssid_len;
7514 spin_unlock_irqrestore(&priv->lock, flags);
7515
7516 IWL_DEBUG_MAC80211("leave\n");
7517 mutex_unlock(&priv->mutex);
7518
7519 return 0;
7520}
7521
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007522static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007523 unsigned int changed_flags,
7524 unsigned int *total_flags,
7525 int mc_count, struct dev_addr_list *mc_list)
7526{
7527 /*
7528 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007529 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007530 */
7531 *total_flags = 0;
7532}
7533
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007534static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007535 struct ieee80211_if_init_conf *conf)
7536{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007537 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007538
7539 IWL_DEBUG_MAC80211("enter\n");
7540
7541 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007542
Mohamed Abbasfde35712007-11-29 11:10:15 +08007543 if (iwl4965_is_ready_rf(priv)) {
7544 iwl4965_scan_cancel_timeout(priv, 100);
7545 cancel_delayed_work(&priv->post_associate);
7546 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7547 iwl4965_commit_rxon(priv);
7548 }
Johannes Berg32bfd352007-12-19 01:31:26 +01007549 if (priv->vif == conf->vif) {
7550 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007551 memset(priv->bssid, 0, ETH_ALEN);
7552 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7553 priv->essid_len = 0;
7554 }
7555 mutex_unlock(&priv->mutex);
7556
7557 IWL_DEBUG_MAC80211("leave\n");
7558
7559}
Johannes Berg471b3ef2007-12-28 14:32:58 +01007560
7561static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
7562 struct ieee80211_vif *vif,
7563 struct ieee80211_bss_conf *bss_conf,
7564 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02007565{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007566 struct iwl4965_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02007567
Johannes Berg471b3ef2007-12-28 14:32:58 +01007568 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
7569 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02007570 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7571 else
7572 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7573 }
7574
Johannes Berg471b3ef2007-12-28 14:32:58 +01007575 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Johannes Berg8318d782008-01-24 19:38:38 +01007576 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02007577 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7578 else
7579 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7580 }
7581
Johannes Berg471b3ef2007-12-28 14:32:58 +01007582 if (changes & BSS_CHANGED_ASSOC) {
7583 /*
7584 * TODO:
7585 * do stuff instead of sniffing assoc resp
7586 */
7587 }
7588
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007589 if (iwl4965_is_associated(priv))
7590 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02007591}
Zhu Yib481de92007-09-25 17:54:57 -07007592
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007593static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007594{
7595 int rc = 0;
7596 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007597 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007598
7599 IWL_DEBUG_MAC80211("enter\n");
7600
mabbas052c4b92007-10-25 17:15:43 +08007601 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007602 spin_lock_irqsave(&priv->lock, flags);
7603
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007604 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007605 rc = -EIO;
7606 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7607 goto out_unlock;
7608 }
7609
7610 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7611 rc = -EIO;
7612 IWL_ERROR("ERROR: APs don't scan\n");
7613 goto out_unlock;
7614 }
7615
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007616 /* we don't schedule scan within next_scan_jiffies period */
7617 if (priv->next_scan_jiffies &&
7618 time_after(priv->next_scan_jiffies, jiffies)) {
7619 rc = -EAGAIN;
7620 goto out_unlock;
7621 }
Zhu Yib481de92007-09-25 17:54:57 -07007622 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007623 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7624 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007625 rc = -EAGAIN;
7626 goto out_unlock;
7627 }
7628 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007629 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007630 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007631
7632 priv->one_direct_scan = 1;
7633 priv->direct_ssid_len = (u8)
7634 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7635 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007636 } else
7637 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007638
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007639 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007640
7641 IWL_DEBUG_MAC80211("leave\n");
7642
7643out_unlock:
7644 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007645 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007646
7647 return rc;
7648}
7649
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007650static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007651 const u8 *local_addr, const u8 *addr,
7652 struct ieee80211_key_conf *key)
7653{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007654 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007655 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007656 int rc = 0;
7657 u8 sta_id;
7658
7659 IWL_DEBUG_MAC80211("enter\n");
7660
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007661 if (!iwl4965_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007662 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7663 return -EOPNOTSUPP;
7664 }
7665
7666 if (is_zero_ether_addr(addr))
7667 /* only support pairwise keys */
7668 return -EOPNOTSUPP;
7669
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007670 sta_id = iwl4965_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07007671 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007672 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7673 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007674 return -EINVAL;
7675 }
7676
7677 mutex_lock(&priv->mutex);
7678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007679 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007680
Zhu Yib481de92007-09-25 17:54:57 -07007681 switch (cmd) {
7682 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007683 rc = iwl4965_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007684 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007685 iwl4965_set_rxon_hwcrypto(priv, 1);
7686 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007687 key->hw_key_idx = sta_id;
7688 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7689 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7690 }
7691 break;
7692 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007693 rc = iwl4965_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007694 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007695 iwl4965_set_rxon_hwcrypto(priv, 0);
7696 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007697 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7698 }
7699 break;
7700 default:
7701 rc = -EINVAL;
7702 }
7703
7704 IWL_DEBUG_MAC80211("leave\n");
7705 mutex_unlock(&priv->mutex);
7706
7707 return rc;
7708}
7709
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007710static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007711 const struct ieee80211_tx_queue_params *params)
7712{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007713 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007714 unsigned long flags;
7715 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007716
7717 IWL_DEBUG_MAC80211("enter\n");
7718
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007719 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007720 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7721 return -EIO;
7722 }
7723
7724 if (queue >= AC_NUM) {
7725 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7726 return 0;
7727 }
7728
Zhu Yib481de92007-09-25 17:54:57 -07007729 if (!priv->qos_data.qos_enable) {
7730 priv->qos_data.qos_active = 0;
7731 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7732 return 0;
7733 }
7734 q = AC_NUM - 1 - queue;
7735
7736 spin_lock_irqsave(&priv->lock, flags);
7737
7738 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7739 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7740 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7741 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007742 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007743
7744 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7745 priv->qos_data.qos_active = 1;
7746
7747 spin_unlock_irqrestore(&priv->lock, flags);
7748
7749 mutex_lock(&priv->mutex);
7750 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007751 iwl4965_activate_qos(priv, 1);
7752 else if (priv->assoc_id && iwl4965_is_associated(priv))
7753 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007754
7755 mutex_unlock(&priv->mutex);
7756
Zhu Yib481de92007-09-25 17:54:57 -07007757 IWL_DEBUG_MAC80211("leave\n");
7758 return 0;
7759}
7760
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007761static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007762 struct ieee80211_tx_queue_stats *stats)
7763{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007764 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007765 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007766 struct iwl4965_tx_queue *txq;
7767 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007768 unsigned long flags;
7769
7770 IWL_DEBUG_MAC80211("enter\n");
7771
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007772 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007773 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7774 return -EIO;
7775 }
7776
7777 spin_lock_irqsave(&priv->lock, flags);
7778
7779 for (i = 0; i < AC_NUM; i++) {
7780 txq = &priv->txq[i];
7781 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007782 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007783
7784 stats->data[i].len = q->n_window - avail;
7785 stats->data[i].limit = q->n_window - q->high_mark;
7786 stats->data[i].count = q->n_window;
7787
7788 }
7789 spin_unlock_irqrestore(&priv->lock, flags);
7790
7791 IWL_DEBUG_MAC80211("leave\n");
7792
7793 return 0;
7794}
7795
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007796static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007797 struct ieee80211_low_level_stats *stats)
7798{
7799 IWL_DEBUG_MAC80211("enter\n");
7800 IWL_DEBUG_MAC80211("leave\n");
7801
7802 return 0;
7803}
7804
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007805static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007806{
7807 IWL_DEBUG_MAC80211("enter\n");
7808 IWL_DEBUG_MAC80211("leave\n");
7809
7810 return 0;
7811}
7812
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007813static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007814{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007815 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007816 unsigned long flags;
7817
7818 mutex_lock(&priv->mutex);
7819 IWL_DEBUG_MAC80211("enter\n");
7820
7821 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007822#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007823 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007824 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007825 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007826#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007827
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007828 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007829
7830 cancel_delayed_work(&priv->post_associate);
7831
7832 spin_lock_irqsave(&priv->lock, flags);
7833 priv->assoc_id = 0;
7834 priv->assoc_capability = 0;
7835 priv->call_post_assoc_from_beacon = 0;
7836 priv->assoc_station_added = 0;
7837
7838 /* new association get rid of ibss beacon skb */
7839 if (priv->ibss_beacon)
7840 dev_kfree_skb(priv->ibss_beacon);
7841
7842 priv->ibss_beacon = NULL;
7843
7844 priv->beacon_int = priv->hw->conf.beacon_int;
7845 priv->timestamp1 = 0;
7846 priv->timestamp0 = 0;
7847 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7848 priv->beacon_int = 0;
7849
7850 spin_unlock_irqrestore(&priv->lock, flags);
7851
Mohamed Abbasfde35712007-11-29 11:10:15 +08007852 if (!iwl4965_is_ready_rf(priv)) {
7853 IWL_DEBUG_MAC80211("leave - not ready\n");
7854 mutex_unlock(&priv->mutex);
7855 return;
7856 }
7857
mabbas052c4b92007-10-25 17:15:43 +08007858 /* we are restarting association process
7859 * clear RXON_FILTER_ASSOC_MSK bit
7860 */
7861 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007862 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007863 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007864 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007865 }
7866
Zhu Yib481de92007-09-25 17:54:57 -07007867 /* Per mac80211.h: This is only used in IBSS mode... */
7868 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007869
Zhu Yib481de92007-09-25 17:54:57 -07007870 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7871 mutex_unlock(&priv->mutex);
7872 return;
7873 }
7874
Zhu Yib481de92007-09-25 17:54:57 -07007875 priv->only_active_channel = 0;
7876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007877 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007878
7879 mutex_unlock(&priv->mutex);
7880
7881 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007882}
7883
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007884static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007885 struct ieee80211_tx_control *control)
7886{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007887 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007888 unsigned long flags;
7889
7890 mutex_lock(&priv->mutex);
7891 IWL_DEBUG_MAC80211("enter\n");
7892
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007893 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007894 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7895 mutex_unlock(&priv->mutex);
7896 return -EIO;
7897 }
7898
7899 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7900 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7901 mutex_unlock(&priv->mutex);
7902 return -EIO;
7903 }
7904
7905 spin_lock_irqsave(&priv->lock, flags);
7906
7907 if (priv->ibss_beacon)
7908 dev_kfree_skb(priv->ibss_beacon);
7909
7910 priv->ibss_beacon = skb;
7911
7912 priv->assoc_id = 0;
7913
7914 IWL_DEBUG_MAC80211("leave\n");
7915 spin_unlock_irqrestore(&priv->lock, flags);
7916
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007917 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007918
7919 queue_work(priv->workqueue, &priv->post_associate.work);
7920
7921 mutex_unlock(&priv->mutex);
7922
7923 return 0;
7924}
7925
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007926#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007927
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007928static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
7929 struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007930{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007931 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
7932 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
7933 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07007934
7935 IWL_DEBUG_MAC80211("enter: \n");
7936
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007937 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
7938 iwl_conf->is_ht = 0;
7939 return;
Zhu Yib481de92007-09-25 17:54:57 -07007940 }
7941
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007942 iwl_conf->is_ht = 1;
7943 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07007944
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007945 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
7946 iwl_conf->sgf |= 0x1;
7947 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
7948 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07007949
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007950 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
7951 iwl_conf->max_amsdu_size =
7952 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007953
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007954 iwl_conf->supported_chan_width =
7955 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007956 iwl_conf->extension_chan_offset =
7957 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
7958 /* If no above or below channel supplied disable FAT channel */
7959 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
7960 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
7961 iwl_conf->supported_chan_width = 0;
7962
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007963 iwl_conf->tx_mimo_ps_mode =
7964 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
7965 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07007966
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007967 iwl_conf->control_channel = ht_bss_conf->primary_channel;
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007968 iwl_conf->tx_chan_width =
7969 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
7970 iwl_conf->ht_protection =
7971 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
7972 iwl_conf->non_GF_STA_present =
7973 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07007974
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007975 IWL_DEBUG_MAC80211("control channel %d\n",
7976 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007977 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007978}
7979
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007980static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007981 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007982{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007983 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007984
7985 IWL_DEBUG_MAC80211("enter: \n");
7986
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007987 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007988 iwl4965_set_rxon_chain(priv);
7989
7990 if (priv && priv->assoc_id &&
7991 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
7992 unsigned long flags;
7993
7994 spin_lock_irqsave(&priv->lock, flags);
7995 if (priv->beacon_int)
7996 queue_work(priv->workqueue, &priv->post_associate.work);
7997 else
7998 priv->call_post_assoc_from_beacon = 1;
7999 spin_unlock_irqrestore(&priv->lock, flags);
8000 }
8001
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02008002 IWL_DEBUG_MAC80211("leave:\n");
8003 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07008004}
8005
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008006#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07008007
8008/*****************************************************************************
8009 *
8010 * sysfs attributes
8011 *
8012 *****************************************************************************/
8013
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008014#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07008015
8016/*
8017 * The following adds a new attribute to the sysfs representation
8018 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
8019 * used for controlling the debug level.
8020 *
8021 * See the level definitions in iwl for details.
8022 */
8023
8024static ssize_t show_debug_level(struct device_driver *d, char *buf)
8025{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008026 return sprintf(buf, "0x%08X\n", iwl4965_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008027}
8028static ssize_t store_debug_level(struct device_driver *d,
8029 const char *buf, size_t count)
8030{
8031 char *p = (char *)buf;
8032 u32 val;
8033
8034 val = simple_strtoul(p, &p, 0);
8035 if (p == buf)
8036 printk(KERN_INFO DRV_NAME
8037 ": %s is not in hex or decimal form.\n", buf);
8038 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008039 iwl4965_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07008040
8041 return strnlen(buf, count);
8042}
8043
8044static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
8045 show_debug_level, store_debug_level);
8046
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008047#endif /* CONFIG_IWL4965_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07008048
8049static ssize_t show_rf_kill(struct device *d,
8050 struct device_attribute *attr, char *buf)
8051{
8052 /*
8053 * 0 - RF kill not enabled
8054 * 1 - SW based RF kill active (sysfs)
8055 * 2 - HW based RF kill active
8056 * 3 - Both HW and SW based RF kill active
8057 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008058 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008059 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
8060 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
8061
8062 return sprintf(buf, "%i\n", val);
8063}
8064
8065static ssize_t store_rf_kill(struct device *d,
8066 struct device_attribute *attr,
8067 const char *buf, size_t count)
8068{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008069 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008070
8071 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008072 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07008073 mutex_unlock(&priv->mutex);
8074
8075 return count;
8076}
8077
8078static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
8079
8080static ssize_t show_temperature(struct device *d,
8081 struct device_attribute *attr, char *buf)
8082{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008083 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008084
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008085 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008086 return -EAGAIN;
8087
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008088 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07008089}
8090
8091static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8092
8093static ssize_t show_rs_window(struct device *d,
8094 struct device_attribute *attr,
8095 char *buf)
8096{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008097 struct iwl4965_priv *priv = d->driver_data;
8098 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07008099}
8100static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8101
8102static ssize_t show_tx_power(struct device *d,
8103 struct device_attribute *attr, char *buf)
8104{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008105 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008106 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8107}
8108
8109static ssize_t store_tx_power(struct device *d,
8110 struct device_attribute *attr,
8111 const char *buf, size_t count)
8112{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008113 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008114 char *p = (char *)buf;
8115 u32 val;
8116
8117 val = simple_strtoul(p, &p, 10);
8118 if (p == buf)
8119 printk(KERN_INFO DRV_NAME
8120 ": %s is not in decimal form.\n", buf);
8121 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008122 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07008123
8124 return count;
8125}
8126
8127static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8128
8129static ssize_t show_flags(struct device *d,
8130 struct device_attribute *attr, char *buf)
8131{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008132 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008133
8134 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8135}
8136
8137static ssize_t store_flags(struct device *d,
8138 struct device_attribute *attr,
8139 const char *buf, size_t count)
8140{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008141 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008142 u32 flags = simple_strtoul(buf, NULL, 0);
8143
8144 mutex_lock(&priv->mutex);
8145 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8146 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008147 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008148 IWL_WARNING("Could not cancel scan.\n");
8149 else {
8150 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8151 flags);
8152 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008153 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008154 }
8155 }
8156 mutex_unlock(&priv->mutex);
8157
8158 return count;
8159}
8160
8161static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8162
8163static ssize_t show_filter_flags(struct device *d,
8164 struct device_attribute *attr, char *buf)
8165{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008166 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008167
8168 return sprintf(buf, "0x%04X\n",
8169 le32_to_cpu(priv->active_rxon.filter_flags));
8170}
8171
8172static ssize_t store_filter_flags(struct device *d,
8173 struct device_attribute *attr,
8174 const char *buf, size_t count)
8175{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008176 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008177 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8178
8179 mutex_lock(&priv->mutex);
8180 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8181 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008182 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008183 IWL_WARNING("Could not cancel scan.\n");
8184 else {
8185 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8186 "0x%04X\n", filter_flags);
8187 priv->staging_rxon.filter_flags =
8188 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008189 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008190 }
8191 }
8192 mutex_unlock(&priv->mutex);
8193
8194 return count;
8195}
8196
8197static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8198 store_filter_flags);
8199
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008200#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008201
8202static ssize_t show_measurement(struct device *d,
8203 struct device_attribute *attr, char *buf)
8204{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008205 struct iwl4965_priv *priv = dev_get_drvdata(d);
8206 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07008207 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8208 u8 *data = (u8 *) & measure_report;
8209 unsigned long flags;
8210
8211 spin_lock_irqsave(&priv->lock, flags);
8212 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8213 spin_unlock_irqrestore(&priv->lock, flags);
8214 return 0;
8215 }
8216 memcpy(&measure_report, &priv->measure_report, size);
8217 priv->measurement_status = 0;
8218 spin_unlock_irqrestore(&priv->lock, flags);
8219
8220 while (size && (PAGE_SIZE - len)) {
8221 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8222 PAGE_SIZE - len, 1);
8223 len = strlen(buf);
8224 if (PAGE_SIZE - len)
8225 buf[len++] = '\n';
8226
8227 ofs += 16;
8228 size -= min(size, 16U);
8229 }
8230
8231 return len;
8232}
8233
8234static ssize_t store_measurement(struct device *d,
8235 struct device_attribute *attr,
8236 const char *buf, size_t count)
8237{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008238 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008239 struct ieee80211_measurement_params params = {
8240 .channel = le16_to_cpu(priv->active_rxon.channel),
8241 .start_time = cpu_to_le64(priv->last_tsf),
8242 .duration = cpu_to_le16(1),
8243 };
8244 u8 type = IWL_MEASURE_BASIC;
8245 u8 buffer[32];
8246 u8 channel;
8247
8248 if (count) {
8249 char *p = buffer;
8250 strncpy(buffer, buf, min(sizeof(buffer), count));
8251 channel = simple_strtoul(p, NULL, 0);
8252 if (channel)
8253 params.channel = channel;
8254
8255 p = buffer;
8256 while (*p && *p != ' ')
8257 p++;
8258 if (*p)
8259 type = simple_strtoul(p + 1, NULL, 0);
8260 }
8261
8262 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8263 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008264 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07008265
8266 return count;
8267}
8268
8269static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8270 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008271#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07008272
8273static ssize_t store_retry_rate(struct device *d,
8274 struct device_attribute *attr,
8275 const char *buf, size_t count)
8276{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008277 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008278
8279 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8280 if (priv->retry_rate <= 0)
8281 priv->retry_rate = 1;
8282
8283 return count;
8284}
8285
8286static ssize_t show_retry_rate(struct device *d,
8287 struct device_attribute *attr, char *buf)
8288{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008289 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008290 return sprintf(buf, "%d", priv->retry_rate);
8291}
8292
8293static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8294 store_retry_rate);
8295
8296static ssize_t store_power_level(struct device *d,
8297 struct device_attribute *attr,
8298 const char *buf, size_t count)
8299{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008300 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008301 int rc;
8302 int mode;
8303
8304 mode = simple_strtoul(buf, NULL, 0);
8305 mutex_lock(&priv->mutex);
8306
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008307 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008308 rc = -EAGAIN;
8309 goto out;
8310 }
8311
8312 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8313 mode = IWL_POWER_AC;
8314 else
8315 mode |= IWL_POWER_ENABLED;
8316
8317 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008318 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07008319 if (rc) {
8320 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8321 goto out;
8322 }
8323 priv->power_mode = mode;
8324 }
8325
8326 rc = count;
8327
8328 out:
8329 mutex_unlock(&priv->mutex);
8330 return rc;
8331}
8332
8333#define MAX_WX_STRING 80
8334
8335/* Values are in microsecond */
8336static const s32 timeout_duration[] = {
8337 350000,
8338 250000,
8339 75000,
8340 37000,
8341 25000,
8342};
8343static const s32 period_duration[] = {
8344 400000,
8345 700000,
8346 1000000,
8347 1000000,
8348 1000000
8349};
8350
8351static ssize_t show_power_level(struct device *d,
8352 struct device_attribute *attr, char *buf)
8353{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008354 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008355 int level = IWL_POWER_LEVEL(priv->power_mode);
8356 char *p = buf;
8357
8358 p += sprintf(p, "%d ", level);
8359 switch (level) {
8360 case IWL_POWER_MODE_CAM:
8361 case IWL_POWER_AC:
8362 p += sprintf(p, "(AC)");
8363 break;
8364 case IWL_POWER_BATTERY:
8365 p += sprintf(p, "(BATTERY)");
8366 break;
8367 default:
8368 p += sprintf(p,
8369 "(Timeout %dms, Period %dms)",
8370 timeout_duration[level - 1] / 1000,
8371 period_duration[level - 1] / 1000);
8372 }
8373
8374 if (!(priv->power_mode & IWL_POWER_ENABLED))
8375 p += sprintf(p, " OFF\n");
8376 else
8377 p += sprintf(p, " \n");
8378
8379 return (p - buf + 1);
8380
8381}
8382
8383static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8384 store_power_level);
8385
8386static ssize_t show_channels(struct device *d,
8387 struct device_attribute *attr, char *buf)
8388{
Johannes Berg8318d782008-01-24 19:38:38 +01008389 /* all this shit doesn't belong into sysfs anyway */
8390 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07008391}
8392
8393static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8394
8395static ssize_t show_statistics(struct device *d,
8396 struct device_attribute *attr, char *buf)
8397{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008398 struct iwl4965_priv *priv = dev_get_drvdata(d);
8399 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07008400 u32 len = 0, ofs = 0;
8401 u8 *data = (u8 *) & priv->statistics;
8402 int rc = 0;
8403
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008404 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008405 return -EAGAIN;
8406
8407 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008408 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008409 mutex_unlock(&priv->mutex);
8410
8411 if (rc) {
8412 len = sprintf(buf,
8413 "Error sending statistics request: 0x%08X\n", rc);
8414 return len;
8415 }
8416
8417 while (size && (PAGE_SIZE - len)) {
8418 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8419 PAGE_SIZE - len, 1);
8420 len = strlen(buf);
8421 if (PAGE_SIZE - len)
8422 buf[len++] = '\n';
8423
8424 ofs += 16;
8425 size -= min(size, 16U);
8426 }
8427
8428 return len;
8429}
8430
8431static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8432
8433static ssize_t show_antenna(struct device *d,
8434 struct device_attribute *attr, char *buf)
8435{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008436 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008437
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008438 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008439 return -EAGAIN;
8440
8441 return sprintf(buf, "%d\n", priv->antenna);
8442}
8443
8444static ssize_t store_antenna(struct device *d,
8445 struct device_attribute *attr,
8446 const char *buf, size_t count)
8447{
8448 int ant;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008449 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008450
8451 if (count == 0)
8452 return 0;
8453
8454 if (sscanf(buf, "%1i", &ant) != 1) {
8455 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8456 return count;
8457 }
8458
8459 if ((ant >= 0) && (ant <= 2)) {
8460 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008461 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07008462 } else
8463 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8464
8465
8466 return count;
8467}
8468
8469static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8470
8471static ssize_t show_status(struct device *d,
8472 struct device_attribute *attr, char *buf)
8473{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008474 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8475 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008476 return -EAGAIN;
8477 return sprintf(buf, "0x%08x\n", (int)priv->status);
8478}
8479
8480static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8481
8482static ssize_t dump_error_log(struct device *d,
8483 struct device_attribute *attr,
8484 const char *buf, size_t count)
8485{
8486 char *p = (char *)buf;
8487
8488 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008489 iwl4965_dump_nic_error_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008490
8491 return strnlen(buf, count);
8492}
8493
8494static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8495
8496static ssize_t dump_event_log(struct device *d,
8497 struct device_attribute *attr,
8498 const char *buf, size_t count)
8499{
8500 char *p = (char *)buf;
8501
8502 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008503 iwl4965_dump_nic_event_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008504
8505 return strnlen(buf, count);
8506}
8507
8508static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8509
8510/*****************************************************************************
8511 *
8512 * driver setup and teardown
8513 *
8514 *****************************************************************************/
8515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008516static void iwl4965_setup_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008517{
8518 priv->workqueue = create_workqueue(DRV_NAME);
8519
8520 init_waitqueue_head(&priv->wait_command_queue);
8521
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008522 INIT_WORK(&priv->up, iwl4965_bg_up);
8523 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8524 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8525 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8526 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8527 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8528 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8529 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8530 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8531 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8532 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8533 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008534
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008535 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008536
8537 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008538 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008539}
8540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008541static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008542{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008543 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008544
Joonwoo Park3ae6a052007-11-29 10:43:16 +09008545 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008546 cancel_delayed_work(&priv->scan_check);
8547 cancel_delayed_work(&priv->alive_start);
8548 cancel_delayed_work(&priv->post_associate);
8549 cancel_work_sync(&priv->beacon_update);
8550}
8551
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008552static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008553 &dev_attr_antenna.attr,
8554 &dev_attr_channels.attr,
8555 &dev_attr_dump_errors.attr,
8556 &dev_attr_dump_events.attr,
8557 &dev_attr_flags.attr,
8558 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008559#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008560 &dev_attr_measurement.attr,
8561#endif
8562 &dev_attr_power_level.attr,
8563 &dev_attr_retry_rate.attr,
8564 &dev_attr_rf_kill.attr,
8565 &dev_attr_rs_window.attr,
8566 &dev_attr_statistics.attr,
8567 &dev_attr_status.attr,
8568 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07008569 &dev_attr_tx_power.attr,
8570
8571 NULL
8572};
8573
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008574static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008575 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008576 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008577};
8578
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008579static struct ieee80211_ops iwl4965_hw_ops = {
8580 .tx = iwl4965_mac_tx,
8581 .start = iwl4965_mac_start,
8582 .stop = iwl4965_mac_stop,
8583 .add_interface = iwl4965_mac_add_interface,
8584 .remove_interface = iwl4965_mac_remove_interface,
8585 .config = iwl4965_mac_config,
8586 .config_interface = iwl4965_mac_config_interface,
8587 .configure_filter = iwl4965_configure_filter,
8588 .set_key = iwl4965_mac_set_key,
8589 .get_stats = iwl4965_mac_get_stats,
8590 .get_tx_stats = iwl4965_mac_get_tx_stats,
8591 .conf_tx = iwl4965_mac_conf_tx,
8592 .get_tsf = iwl4965_mac_get_tsf,
8593 .reset_tsf = iwl4965_mac_reset_tsf,
8594 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01008595 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008596#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008597 .conf_ht = iwl4965_mac_conf_ht,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02008598 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008599#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008600 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008601};
8602
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008603static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008604{
8605 int err = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008606 struct iwl4965_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008607 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008608 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008609 int i;
Zhu Yi5a669262008-01-14 17:46:18 -08008610 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008611
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008612 /* Disabling hardware scan means that mac80211 will perform scans
8613 * "the hard way", rather than using device's scan. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008614 if (iwl4965_param_disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008615 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008616 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008617 }
8618
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008619 if ((iwl4965_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8620 (iwl4965_param_queues_num < IWL_MIN_NUM_QUEUES)) {
Zhu Yib481de92007-09-25 17:54:57 -07008621 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8622 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8623 err = -EINVAL;
8624 goto out;
8625 }
8626
8627 /* mac80211 allocates memory for this device instance, including
8628 * space for this driver's private structure */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008629 hw = ieee80211_alloc_hw(sizeof(struct iwl4965_priv), &iwl4965_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07008630 if (hw == NULL) {
8631 IWL_ERROR("Can not allocate network device\n");
8632 err = -ENOMEM;
8633 goto out;
8634 }
8635 SET_IEEE80211_DEV(hw, &pdev->dev);
8636
Johannes Bergf51359a2007-10-28 14:53:36 +01008637 hw->rate_control_algorithm = "iwl-4965-rs";
8638
Zhu Yib481de92007-09-25 17:54:57 -07008639 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8640 priv = hw->priv;
8641 priv->hw = hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008642 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008643
8644 priv->pci_dev = pdev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008645 priv->antenna = (enum iwl4965_antenna)iwl4965_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008646#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008647 iwl4965_debug_level = iwl4965_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008648 atomic_set(&priv->restrict_refcnt, 0);
8649#endif
8650 priv->retry_rate = 1;
8651
8652 priv->ibss_beacon = NULL;
8653
8654 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8655 * the range of signal quality values that we'll provide.
8656 * Negative values for level/noise indicate that we'll provide dBm.
8657 * For WE, at least, non-0 values here *enable* display of values
8658 * in app (iwconfig). */
8659 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8660 hw->max_noise = -20; /* noise level, negative indicates dBm */
8661 hw->max_signal = 100; /* link quality indication (%) */
8662
8663 /* Tell mac80211 our Tx characteristics */
8664 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8665
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008666 /* Default value; 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07008667 hw->queues = 4;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008668#ifdef CONFIG_IWL4965_HT
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008669 /* Enhanced value; more queues, to support 11n aggregation */
Zhu Yib481de92007-09-25 17:54:57 -07008670 hw->queues = 16;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008671#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07008672
8673 spin_lock_init(&priv->lock);
8674 spin_lock_init(&priv->power_data.lock);
8675 spin_lock_init(&priv->sta_lock);
8676 spin_lock_init(&priv->hcmd_lock);
8677 spin_lock_init(&priv->lq_mngr.lock);
8678
8679 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8680 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8681
8682 INIT_LIST_HEAD(&priv->free_frames);
8683
8684 mutex_init(&priv->mutex);
8685 if (pci_enable_device(pdev)) {
8686 err = -ENODEV;
8687 goto out_ieee80211_free_hw;
8688 }
8689
8690 pci_set_master(pdev);
8691
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008692 /* Clear the driver's (not device's) station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008693 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008694
8695 priv->data_retry_limit = -1;
8696 priv->ieee_channels = NULL;
8697 priv->ieee_rates = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01008698 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07008699
8700 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8701 if (!err)
8702 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8703 if (err) {
8704 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
8705 goto out_pci_disable_device;
8706 }
8707
8708 pci_set_drvdata(pdev, priv);
8709 err = pci_request_regions(pdev, DRV_NAME);
8710 if (err)
8711 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008712
Zhu Yib481de92007-09-25 17:54:57 -07008713 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8714 * PCI Tx retries from interfering with C3 CPU state */
8715 pci_write_config_byte(pdev, 0x41, 0x00);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008716
Zhu Yib481de92007-09-25 17:54:57 -07008717 priv->hw_base = pci_iomap(pdev, 0, 0);
8718 if (!priv->hw_base) {
8719 err = -ENODEV;
8720 goto out_pci_release_regions;
8721 }
8722
8723 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8724 (unsigned long long) pci_resource_len(pdev, 0));
8725 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8726
8727 /* Initialize module parameter values here */
8728
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008729 /* Disable radio (SW RF KILL) via parameter when loading driver */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008730 if (iwl4965_param_disable) {
Zhu Yib481de92007-09-25 17:54:57 -07008731 set_bit(STATUS_RF_KILL_SW, &priv->status);
8732 IWL_DEBUG_INFO("Radio disabled.\n");
8733 }
8734
8735 priv->iw_mode = IEEE80211_IF_TYPE_STA;
8736
8737 priv->ps_mode = 0;
8738 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
Zhu Yib481de92007-09-25 17:54:57 -07008739 priv->valid_antenna = 0x7; /* assume all 3 connected */
8740 priv->ps_mode = IWL_MIMO_PS_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07008741
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008742 /* Choose which receivers/antennas to use */
Zhu Yib481de92007-09-25 17:54:57 -07008743 iwl4965_set_rxon_chain(priv);
8744
Tomas Winkler82b9a122008-03-04 18:09:30 -08008745
Zhu Yib481de92007-09-25 17:54:57 -07008746 printk(KERN_INFO DRV_NAME
Tomas Winkler82b9a122008-03-04 18:09:30 -08008747 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
Zhu Yib481de92007-09-25 17:54:57 -07008748
8749 /* Device-specific setup */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008750 if (iwl4965_hw_set_hw_setting(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008751 IWL_ERROR("failed to set hw settings\n");
Zhu Yib481de92007-09-25 17:54:57 -07008752 goto out_iounmap;
8753 }
8754
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008755 if (iwl4965_param_qos_enable)
Zhu Yib481de92007-09-25 17:54:57 -07008756 priv->qos_data.qos_enable = 1;
8757
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008758 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008759
8760 priv->qos_data.qos_active = 0;
8761 priv->qos_data.qos_cap.val = 0;
Zhu Yib481de92007-09-25 17:54:57 -07008762
Johannes Berg8318d782008-01-24 19:38:38 +01008763 iwl4965_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008764 iwl4965_setup_deferred_work(priv);
8765 iwl4965_setup_rx_handlers(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008766
8767 priv->rates_mask = IWL_RATES_MASK;
8768 /* If power management is turned on, default to AC mode */
8769 priv->power_mode = IWL_POWER_AC;
8770 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
8771
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008772 iwl4965_disable_interrupts(priv);
Jes Sorensen49df2b32007-10-26 16:10:39 +02008773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008774 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008775 if (err) {
8776 IWL_ERROR("failed to create sysfs device attributes\n");
Zhu Yib481de92007-09-25 17:54:57 -07008777 goto out_release_irq;
8778 }
8779
Zhu Yi5a669262008-01-14 17:46:18 -08008780 /* nic init */
8781 iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
8782 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8783
8784 iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8785 err = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
8786 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8787 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8788 if (err < 0) {
8789 IWL_DEBUG_INFO("Failed to init the card\n");
8790 goto out_remove_sysfs;
8791 }
8792 /* Read the EEPROM */
8793 err = iwl4965_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008794 if (err) {
Zhu Yi5a669262008-01-14 17:46:18 -08008795 IWL_ERROR("Unable to init EEPROM\n");
8796 goto out_remove_sysfs;
8797 }
8798 /* MAC Address location in EEPROM same for 3945/4965 */
8799 get_eeprom_mac(priv, priv->mac_addr);
8800 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8801 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8802
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008803 err = iwl4965_init_channel_map(priv);
8804 if (err) {
8805 IWL_ERROR("initializing regulatory failed: %d\n", err);
8806 goto out_remove_sysfs;
8807 }
8808
8809 err = iwl4965_init_geos(priv);
8810 if (err) {
8811 IWL_ERROR("initializing geos failed: %d\n", err);
8812 goto out_free_channel_map;
8813 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008814
Zhu Yi5a669262008-01-14 17:46:18 -08008815 iwl4965_rate_control_register(priv->hw);
8816 err = ieee80211_register_hw(priv->hw);
8817 if (err) {
8818 IWL_ERROR("Failed to register network device (error %d)\n", err);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008819 goto out_free_geos;
Zhu Yib481de92007-09-25 17:54:57 -07008820 }
8821
Zhu Yi5a669262008-01-14 17:46:18 -08008822 priv->hw->conf.beacon_int = 100;
8823 priv->mac80211_registered = 1;
8824 pci_save_state(pdev);
8825 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008826
8827 return 0;
8828
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008829 out_free_geos:
8830 iwl4965_free_geos(priv);
8831 out_free_channel_map:
8832 iwl4965_free_channel_map(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08008833 out_remove_sysfs:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008834 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008835
8836 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07008837 destroy_workqueue(priv->workqueue);
8838 priv->workqueue = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008839 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008840
8841 out_iounmap:
8842 pci_iounmap(pdev, priv->hw_base);
8843 out_pci_release_regions:
8844 pci_release_regions(pdev);
8845 out_pci_disable_device:
8846 pci_disable_device(pdev);
8847 pci_set_drvdata(pdev, NULL);
8848 out_ieee80211_free_hw:
8849 ieee80211_free_hw(priv->hw);
8850 out:
8851 return err;
8852}
8853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008854static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008855{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008856 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008857 struct list_head *p, *q;
8858 int i;
8859
8860 if (!priv)
8861 return;
8862
8863 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8864
Zhu Yib481de92007-09-25 17:54:57 -07008865 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008867 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008868
8869 /* Free MAC hash list for ADHOC */
8870 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8871 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8872 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008873 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008874 }
8875 }
8876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008877 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008878
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008879 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008880
8881 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008882 iwl4965_rx_queue_free(priv, &priv->rxq);
8883 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008885 iwl4965_unset_hw_setting(priv);
8886 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008887
8888 if (priv->mac80211_registered) {
8889 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008890 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008891 }
8892
Mohamed Abbas948c1712007-10-25 17:15:45 +08008893 /*netif_stop_queue(dev); */
8894 flush_workqueue(priv->workqueue);
8895
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008896 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008897 * priv->workqueue... so we can't take down the workqueue
8898 * until now... */
8899 destroy_workqueue(priv->workqueue);
8900 priv->workqueue = NULL;
8901
Zhu Yib481de92007-09-25 17:54:57 -07008902 pci_iounmap(pdev, priv->hw_base);
8903 pci_release_regions(pdev);
8904 pci_disable_device(pdev);
8905 pci_set_drvdata(pdev, NULL);
8906
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008907 iwl4965_free_channel_map(priv);
8908 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008909
8910 if (priv->ibss_beacon)
8911 dev_kfree_skb(priv->ibss_beacon);
8912
8913 ieee80211_free_hw(priv->hw);
8914}
8915
8916#ifdef CONFIG_PM
8917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008918static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008919{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008920 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008921
Zhu Yie655b9f2008-01-24 02:19:38 -08008922 if (priv->is_open) {
8923 set_bit(STATUS_IN_SUSPEND, &priv->status);
8924 iwl4965_mac_stop(priv->hw);
8925 priv->is_open = 1;
8926 }
Zhu Yib481de92007-09-25 17:54:57 -07008927
Zhu Yib481de92007-09-25 17:54:57 -07008928 pci_set_power_state(pdev, PCI_D3hot);
8929
Zhu Yib481de92007-09-25 17:54:57 -07008930 return 0;
8931}
8932
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008933static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008934{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008935 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008936
Zhu Yib481de92007-09-25 17:54:57 -07008937 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008938
Zhu Yie655b9f2008-01-24 02:19:38 -08008939 if (priv->is_open)
8940 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008941
Zhu Yie655b9f2008-01-24 02:19:38 -08008942 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008943 return 0;
8944}
8945
8946#endif /* CONFIG_PM */
8947
8948/*****************************************************************************
8949 *
8950 * driver and module entry point
8951 *
8952 *****************************************************************************/
8953
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008954static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008955 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008956 .id_table = iwl4965_hw_card_ids,
8957 .probe = iwl4965_pci_probe,
8958 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008959#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008960 .suspend = iwl4965_pci_suspend,
8961 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008962#endif
8963};
8964
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008965static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008966{
8967
8968 int ret;
8969 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8970 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008971 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008972 if (ret) {
8973 IWL_ERROR("Unable to initialize PCI module\n");
8974 return ret;
8975 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008976#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008977 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008978 if (ret) {
8979 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008980 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008981 return ret;
8982 }
8983#endif
8984
8985 return ret;
8986}
8987
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008988static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008989{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008990#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008991 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008992#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008993 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008994}
8995
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008996module_param_named(antenna, iwl4965_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008997MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008998module_param_named(disable, iwl4965_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008999MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009000module_param_named(hwcrypto, iwl4965_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009001MODULE_PARM_DESC(hwcrypto,
9002 "using hardware crypto engine (default 0 [software])\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009003module_param_named(debug, iwl4965_param_debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009004MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009005module_param_named(disable_hw_scan, iwl4965_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009006MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
9007
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009008module_param_named(queues_num, iwl4965_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009009MODULE_PARM_DESC(queues_num, "number of hw queues.");
9010
9011/* QoS */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009012module_param_named(qos_enable, iwl4965_param_qos_enable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009013MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02009014module_param_named(amsdu_size_8K, iwl4965_param_amsdu_size_8K, int, 0444);
9015MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Zhu Yib481de92007-09-25 17:54:57 -07009016
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009017module_exit(iwl4965_exit);
9018module_init(iwl4965_init);