blob: 29e843108e9f0be55426a8a0de4b7e6c76acea82 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
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
Assaf Krauss6bc913b2008-03-11 16:17:18 -070048#include "iwl-eeprom.h"
Tomas Winkler82b9a122008-03-04 18:09:30 -080049#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070050#include "iwl-4965.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070051#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070052#include "iwl-helpers.h"
53
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070054static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080055 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080056
Zhu Yib481de92007-09-25 17:54:57 -070057/******************************************************************************
58 *
59 * module boiler plate
60 *
61 ******************************************************************************/
62
Zhu Yib481de92007-09-25 17:54:57 -070063/*
64 * module name, copyright, version, etc.
65 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
66 */
67
68#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
69
Tomas Winkler0a6857e2008-03-12 16:58:49 -070070#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070071#define VD "d"
72#else
73#define VD
74#endif
75
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080076#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070077#define VS "s"
78#else
79#define VS
80#endif
81
Tomas Winklerdf48c322008-03-06 10:40:19 -080082#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070083
Zhu Yib481de92007-09-25 17:54:57 -070084
85MODULE_DESCRIPTION(DRV_DESCRIPTION);
86MODULE_VERSION(DRV_VERSION);
87MODULE_AUTHOR(DRV_COPYRIGHT);
88MODULE_LICENSE("GPL");
89
90__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
91{
92 u16 fc = le16_to_cpu(hdr->frame_control);
93 int hdr_len = ieee80211_get_hdrlen(fc);
94
95 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
96 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
97 return NULL;
98}
99
Johannes Berg8318d782008-01-24 19:38:38 +0100100static const struct ieee80211_supported_band *iwl4965_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700101 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700102{
Johannes Berg8318d782008-01-24 19:38:38 +0100103 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700104}
105
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800106static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700107{
108 /* Single white space is for Linksys APs */
109 if (essid_len == 1 && essid[0] == ' ')
110 return 1;
111
112 /* Otherwise, if the entire essid is 0, we assume it is hidden */
113 while (essid_len) {
114 essid_len--;
115 if (essid[essid_len] != '\0')
116 return 0;
117 }
118
119 return 1;
120}
121
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800122static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700123{
124 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
125 const char *s = essid;
126 char *d = escaped;
127
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800128 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700129 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
130 return escaped;
131 }
132
133 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
134 while (essid_len--) {
135 if (*s == '\0') {
136 *d++ = '\\';
137 *d++ = '0';
138 s++;
139 } else
140 *d++ = *s++;
141 }
142 *d = '\0';
143 return escaped;
144}
145
Zhu Yib481de92007-09-25 17:54:57 -0700146/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
147 * DMA services
148 *
149 * Theory of operation
150 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800151 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
152 * of buffer descriptors, each of which points to one or more data buffers for
153 * the device to read from or fill. Driver and device exchange status of each
154 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
155 * entries in each circular buffer, to protect against confusing empty and full
156 * queue states.
157 *
158 * The device reads or writes the data in the queues via the device's several
159 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700160 *
161 * For Tx queue, there are low mark and high mark limits. If, after queuing
162 * the packet for Tx, free space become < low mark, Tx queue stopped. When
163 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
164 * Tx queue resumed.
165 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800166 * The 4965 operates with up to 17 queues: One receive queue, one transmit
167 * queue (#4) for sending commands to the device firmware, and 15 other
168 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800169 *
170 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700171 ***************************************************/
172
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200173int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700174{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800175 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700176
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800177 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700178 s -= q->n_bd;
179
180 if (s <= 0)
181 s += q->n_window;
182 /* keep some reserve to not confuse empty and full situations */
183 s -= 2;
184 if (s < 0)
185 s = 0;
186 return s;
187}
188
Zhu Yib481de92007-09-25 17:54:57 -0700189
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800190static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700191{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800192 return q->write_ptr > q->read_ptr ?
193 (i >= q->read_ptr && i < q->write_ptr) :
194 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700195}
196
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800197static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700198{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800199 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700200 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800201 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700202
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800203 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700204 return index & (q->n_window - 1);
205}
206
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800207/**
208 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
209 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700210static int iwl4965_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700211 int count, int slots_num, u32 id)
212{
213 q->n_bd = count;
214 q->n_window = slots_num;
215 q->id = id;
216
Tomas Winklerc54b6792008-03-06 17:36:53 -0800217 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
218 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700219 BUG_ON(!is_power_of_2(count));
220
221 /* slots_num must be power-of-two size, otherwise
222 * get_cmd_index is broken. */
223 BUG_ON(!is_power_of_2(slots_num));
224
225 q->low_mark = q->n_window / 4;
226 if (q->low_mark < 4)
227 q->low_mark = 4;
228
229 q->high_mark = q->n_window / 8;
230 if (q->high_mark < 2)
231 q->high_mark = 2;
232
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800233 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700234
235 return 0;
236}
237
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800238/**
239 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
240 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700241static int iwl4965_tx_queue_alloc(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800242 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700243{
244 struct pci_dev *dev = priv->pci_dev;
245
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800246 /* Driver private data, only for Tx (not command) queues,
247 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700248 if (id != IWL_CMD_QUEUE_NUM) {
249 txq->txb = kmalloc(sizeof(txq->txb[0]) *
250 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
251 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800252 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700253 "structures failed\n");
254 goto error;
255 }
256 } else
257 txq->txb = NULL;
258
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800259 /* Circular buffer of transmit frame descriptors (TFDs),
260 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700261 txq->bd = pci_alloc_consistent(dev,
262 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
263 &txq->q.dma_addr);
264
265 if (!txq->bd) {
266 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
267 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
268 goto error;
269 }
270 txq->q.id = id;
271
272 return 0;
273
274 error:
275 if (txq->txb) {
276 kfree(txq->txb);
277 txq->txb = NULL;
278 }
279
280 return -ENOMEM;
281}
282
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800283/**
284 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
285 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700286int iwl4965_tx_queue_init(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800287 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700288{
289 struct pci_dev *dev = priv->pci_dev;
290 int len;
291 int rc = 0;
292
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800293 /*
294 * Alloc buffer array for commands (Tx or other types of commands).
295 * For the command queue (#4), allocate command space + one big
296 * command for scan, since scan command is very huge; the system will
297 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200298 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800299 * space is needed.
300 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700301 len = sizeof(struct iwl_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700302 if (txq_id == IWL_CMD_QUEUE_NUM)
303 len += IWL_MAX_SCAN_SIZE;
304 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
305 if (!txq->cmd)
306 return -ENOMEM;
307
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800308 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800309 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700310 if (rc) {
311 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
312
313 return -ENOMEM;
314 }
315 txq->need_update = 0;
316
317 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800318 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700319 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800320
321 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800322 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700323
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800324 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800325 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700326
327 return 0;
328}
329
330/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800331 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700332 * @txq: Transmit queue to deallocate.
333 *
334 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800335 * Free all buffers.
336 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700337 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700338void iwl4965_tx_queue_free(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700339{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800340 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700341 struct pci_dev *dev = priv->pci_dev;
342 int len;
343
344 if (q->n_bd == 0)
345 return;
346
347 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800348 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800349 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800350 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700351
Tomas Winkler857485c2008-03-21 13:53:44 -0700352 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700353 if (q->id == IWL_CMD_QUEUE_NUM)
354 len += IWL_MAX_SCAN_SIZE;
355
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800356 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700357 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
358
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800359 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700360 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800361 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700362 txq->q.n_bd, txq->bd, txq->q.dma_addr);
363
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800364 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700365 if (txq->txb) {
366 kfree(txq->txb);
367 txq->txb = NULL;
368 }
369
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800370 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700371 memset(txq, 0, sizeof(*txq));
372}
373
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800374const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700375
376/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800377 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700378 * the functionality provided here
379 */
380
381/**************************************************************/
382
Ian Schram01ebd062007-10-25 17:15:22 +0800383#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800384/**
385 * iwl4965_remove_station - Remove driver's knowledge of station.
386 *
387 * NOTE: This does not remove station from device's station table.
388 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700389static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700390{
391 int index = IWL_INVALID_STATION;
392 int i;
393 unsigned long flags;
394
395 spin_lock_irqsave(&priv->sta_lock, flags);
396
397 if (is_ap)
398 index = IWL_AP_ID;
399 else if (is_broadcast_ether_addr(addr))
400 index = priv->hw_setting.bcast_sta_id;
401 else
402 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
403 if (priv->stations[i].used &&
404 !compare_ether_addr(priv->stations[i].sta.sta.addr,
405 addr)) {
406 index = i;
407 break;
408 }
409
410 if (unlikely(index == IWL_INVALID_STATION))
411 goto out;
412
413 if (priv->stations[index].used) {
414 priv->stations[index].used = 0;
415 priv->num_stations--;
416 }
417
418 BUG_ON(priv->num_stations < 0);
419
420out:
421 spin_unlock_irqrestore(&priv->sta_lock, flags);
422 return 0;
423}
Zhu Yi556f8db2007-09-27 11:27:33 +0800424#endif
Zhu Yib481de92007-09-25 17:54:57 -0700425
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800426/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800427 * iwl4965_add_station_flags - Add station to tables in driver and device
428 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700429u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200430 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700431{
432 int i;
433 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800434 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700435 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700436 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700437
438 spin_lock_irqsave(&priv->sta_lock, flags_spin);
439 if (is_ap)
440 index = IWL_AP_ID;
441 else if (is_broadcast_ether_addr(addr))
442 index = priv->hw_setting.bcast_sta_id;
443 else
444 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
445 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
446 addr)) {
447 index = i;
448 break;
449 }
450
451 if (!priv->stations[i].used &&
452 index == IWL_INVALID_STATION)
453 index = i;
454 }
455
456
Ben Cahill9fbab512007-11-29 11:09:47 +0800457 /* These two conditions have the same outcome, but keep them separate
458 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700459 if (unlikely(index == IWL_INVALID_STATION)) {
460 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
461 return index;
462 }
463
464 if (priv->stations[index].used &&
465 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
466 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
467 return index;
468 }
469
470
Joe Perches0795af52007-10-03 17:59:30 -0700471 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700472 station = &priv->stations[index];
473 station->used = 1;
474 priv->num_stations++;
475
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800476 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800477 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700478 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
479 station->sta.mode = 0;
480 station->sta.sta.sta_id = index;
481 station->sta.station_flags = 0;
482
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800483#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700484 /* BCAST station and IBSS stations do not work in HT mode */
485 if (index != priv->hw_setting.bcast_sta_id &&
486 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200487 iwl4965_set_ht_add_station(priv, index,
488 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800489#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700490
491 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800492
493 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800494 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700495 return index;
496
497}
498
499/*************** DRIVER STATUS FUNCTIONS *****/
500
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700501static inline int iwl4965_is_ready(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700502{
503 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
504 * set but EXIT_PENDING is not */
505 return test_bit(STATUS_READY, &priv->status) &&
506 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
507 !test_bit(STATUS_EXIT_PENDING, &priv->status);
508}
509
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700510static inline int iwl4965_is_alive(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700511{
512 return test_bit(STATUS_ALIVE, &priv->status);
513}
514
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700515static inline int iwl4965_is_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700516{
517 return test_bit(STATUS_INIT, &priv->status);
518}
519
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700520static inline int iwl4965_is_rfkill(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700521{
522 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
523 test_bit(STATUS_RF_KILL_SW, &priv->status);
524}
525
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700526static inline int iwl4965_is_ready_rf(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700527{
528
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800529 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700530 return 0;
531
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800532 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700533}
534
535/*************** HOST COMMAND QUEUE FUNCTIONS *****/
536
Zhu Yib481de92007-09-25 17:54:57 -0700537/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800538 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700539 * @priv: device private data point
540 * @cmd: a point to the ucode command structure
541 *
542 * The function returns < 0 values to indicate the operation is
543 * failed. On success, it turns the index (> 0) of command in the
544 * command queue.
545 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700546int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700547{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800548 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
549 struct iwl4965_queue *q = &txq->q;
550 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700551 u32 *control_flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700552 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700553 u32 idx;
554 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
555 dma_addr_t phys_addr;
556 int ret;
557 unsigned long flags;
558
559 /* If any of the command structures end up being larger than
560 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
561 * we will need to increase the size of the TFD entries */
562 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
563 !(cmd->meta.flags & CMD_SIZE_HUGE));
564
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800565 if (iwl4965_is_rfkill(priv)) {
566 IWL_DEBUG_INFO("Not sending command - RF KILL");
567 return -EIO;
568 }
569
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800570 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700571 IWL_ERROR("No space for Tx\n");
572 return -ENOSPC;
573 }
574
575 spin_lock_irqsave(&priv->hcmd_lock, flags);
576
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800577 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700578 memset(tfd, 0, sizeof(*tfd));
579
580 control_flags = (u32 *) tfd;
581
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800582 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700583 out_cmd = &txq->cmd[idx];
584
585 out_cmd->hdr.cmd = cmd->id;
586 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
587 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
588
589 /* At this point, the out_cmd now has all of the incoming cmd
590 * information */
591
592 out_cmd->hdr.flags = 0;
593 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800594 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700595 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
596 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
597
598 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700599 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800600 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700601
602 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
603 "%d bytes at %d[%d]:%d\n",
604 get_cmd_string(out_cmd->hdr.cmd),
605 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800606 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700607
608 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800609
610 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700611 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800612
613 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800614 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800615 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700616
617 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
618 return ret ? ret : idx;
619}
620
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700621static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
622{
623 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
624
625 if (hw_decrypt)
626 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
627 else
628 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
629
630}
631
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700632int iwl4965_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700633{
Tomas Winkler857485c2008-03-21 13:53:44 -0700634 u32 flags = 0;
635 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
636 sizeof(flags), &flags);
Zhu Yib481de92007-09-25 17:54:57 -0700637}
638
639/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800640 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700641 *
642 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800643 * NOTE: mutex must be held before calling this fnction
644 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700645static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700646 const u8 *addr, int is_ap)
647{
Zhu Yi556f8db2007-09-27 11:27:33 +0800648 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700649
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800650 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200651#ifdef CONFIG_IWL4965_HT
652 struct ieee80211_conf *conf = &priv->hw->conf;
653 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
654
655 if ((is_ap) &&
656 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
657 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
658 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
659 0, cur_ht_config);
660 else
661#endif /* CONFIG_IWL4965_HT */
662 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
663 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800664
665 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700666 iwl4965_add_station(priv, addr, is_ap);
667
Zhu Yi556f8db2007-09-27 11:27:33 +0800668 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700669}
670
671/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800672 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700673 *
674 * NOTE: This is really only useful during development and can eventually
675 * be #ifdef'd out once the driver is stable and folks aren't actively
676 * making changes
677 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800678static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700679{
680 int error = 0;
681 int counter = 1;
682
683 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
684 error |= le32_to_cpu(rxon->flags &
685 (RXON_FLG_TGJ_NARROW_BAND_MSK |
686 RXON_FLG_RADAR_DETECT_MSK));
687 if (error)
688 IWL_WARNING("check 24G fields %d | %d\n",
689 counter++, error);
690 } else {
691 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
692 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
693 if (error)
694 IWL_WARNING("check 52 fields %d | %d\n",
695 counter++, error);
696 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
697 if (error)
698 IWL_WARNING("check 52 CCK %d | %d\n",
699 counter++, error);
700 }
701 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
702 if (error)
703 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
704
705 /* make sure basic rates 6Mbps and 1Mbps are supported */
706 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
707 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
708 if (error)
709 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
710
711 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
712 if (error)
713 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
714
715 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
716 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
717 if (error)
718 IWL_WARNING("check CCK and short slot %d | %d\n",
719 counter++, error);
720
721 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
722 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
723 if (error)
724 IWL_WARNING("check CCK & auto detect %d | %d\n",
725 counter++, error);
726
727 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
728 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
729 if (error)
730 IWL_WARNING("check TGG and auto detect %d | %d\n",
731 counter++, error);
732
733 if (error)
734 IWL_WARNING("Tuning to channel %d\n",
735 le16_to_cpu(rxon->channel));
736
737 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800738 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700739 return -1;
740 }
741 return 0;
742}
743
744/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800745 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800746 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700747 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800748 * If the RXON structure is changing enough to require a new tune,
749 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
750 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700751 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700752static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700753{
754
755 /* These items are only settable from the full RXON command */
756 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
757 compare_ether_addr(priv->staging_rxon.bssid_addr,
758 priv->active_rxon.bssid_addr) ||
759 compare_ether_addr(priv->staging_rxon.node_addr,
760 priv->active_rxon.node_addr) ||
761 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
762 priv->active_rxon.wlap_bssid_addr) ||
763 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
764 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
765 (priv->staging_rxon.air_propagation !=
766 priv->active_rxon.air_propagation) ||
767 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
768 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
769 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
770 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
771 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
772 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
773 return 1;
774
775 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
776 * be updated with the RXON_ASSOC command -- however only some
777 * flag transitions are allowed using RXON_ASSOC */
778
779 /* Check if we are not switching bands */
780 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
781 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
782 return 1;
783
784 /* Check if we are switching association toggle */
785 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
786 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
787 return 1;
788
789 return 0;
790}
791
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700792static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700793{
794 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800795 struct iwl4965_rx_packet *res = NULL;
796 struct iwl4965_rxon_assoc_cmd rxon_assoc;
Tomas Winkler857485c2008-03-21 13:53:44 -0700797 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700798 .id = REPLY_RXON_ASSOC,
799 .len = sizeof(rxon_assoc),
800 .meta.flags = CMD_WANT_SKB,
801 .data = &rxon_assoc,
802 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800803 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
804 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700805
806 if ((rxon1->flags == rxon2->flags) &&
807 (rxon1->filter_flags == rxon2->filter_flags) &&
808 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
809 (rxon1->ofdm_ht_single_stream_basic_rates ==
810 rxon2->ofdm_ht_single_stream_basic_rates) &&
811 (rxon1->ofdm_ht_dual_stream_basic_rates ==
812 rxon2->ofdm_ht_dual_stream_basic_rates) &&
813 (rxon1->rx_chain == rxon2->rx_chain) &&
814 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
815 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
816 return 0;
817 }
818
819 rxon_assoc.flags = priv->staging_rxon.flags;
820 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
821 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
822 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
823 rxon_assoc.reserved = 0;
824 rxon_assoc.ofdm_ht_single_stream_basic_rates =
825 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
826 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
827 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
828 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
829
Tomas Winkler857485c2008-03-21 13:53:44 -0700830 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700831 if (rc)
832 return rc;
833
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800834 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700835 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
836 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
837 rc = -EIO;
838 }
839
840 priv->alloc_rxb_skb--;
841 dev_kfree_skb_any(cmd.meta.u.skb);
842
843 return rc;
844}
845
846/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800847 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700848 *
Ian Schram01ebd062007-10-25 17:15:22 +0800849 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700850 * the active_rxon structure is updated with the new data. This
851 * function correctly transitions out of the RXON_ASSOC_MSK state if
852 * a HW tune is required based on the RXON structure changes.
853 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700854static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700855{
856 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800857 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700858 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700859 int rc = 0;
860
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800861 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700862 return -1;
863
864 /* always get timestamp with Rx frame */
865 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800867 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700868 if (rc) {
869 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
870 return -EINVAL;
871 }
872
873 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800874 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700875 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800876 if (!iwl4965_full_rxon_required(priv)) {
877 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700878 if (rc) {
879 IWL_ERROR("Error setting RXON_ASSOC "
880 "configuration (%d).\n", rc);
881 return rc;
882 }
883
884 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
885
886 return 0;
887 }
888
889 /* station table will be cleared */
890 priv->assoc_station_added = 0;
891
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800892#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700893 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
894 if (!priv->error_recovering)
895 priv->start_calib = 0;
896
897 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800898#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700899
900 /* If we are currently associated and the new config requires
901 * an RXON_ASSOC and the new config wants the associated mask enabled,
902 * we must clear the associated from the active configuration
903 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800904 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700905 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
906 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
907 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
908
Tomas Winkler857485c2008-03-21 13:53:44 -0700909 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800910 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700911 &priv->active_rxon);
912
913 /* If the mask clearing failed then we set
914 * active_rxon back to what it was previously */
915 if (rc) {
916 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
917 IWL_ERROR("Error clearing ASSOC_MSK on current "
918 "configuration (%d).\n", rc);
919 return rc;
920 }
Zhu Yib481de92007-09-25 17:54:57 -0700921 }
922
923 IWL_DEBUG_INFO("Sending RXON\n"
924 "* with%s RXON_FILTER_ASSOC_MSK\n"
925 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700926 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700927 ((priv->staging_rxon.filter_flags &
928 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
929 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700930 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700931
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700932 iwl4965_set_rxon_hwcrypto(priv, priv->cfg->mod_params->hw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700933 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700934 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800935 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700936 if (rc) {
937 IWL_ERROR("Error setting new configuration (%d).\n", rc);
938 return rc;
939 }
940
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700941 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800942
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800943#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700944 if (!priv->error_recovering)
945 priv->start_calib = 0;
946
947 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
948 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800949#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700950
951 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
952
953 /* If we issue a new RXON command which required a tune then we must
954 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800955 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700956 if (rc) {
957 IWL_ERROR("Error setting Tx power (%d).\n", rc);
958 return rc;
959 }
960
961 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800962 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700963 IWL_INVALID_STATION) {
964 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
965 return -EIO;
966 }
967
968 /* If we have set the ASSOC_MSK and we are in BSS mode then
969 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800970 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700971 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800972 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700973 == IWL_INVALID_STATION) {
974 IWL_ERROR("Error adding AP address for transmit.\n");
975 return -EIO;
976 }
977 priv->assoc_station_added = 1;
978 }
979
980 return 0;
981}
982
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700983static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700984{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800985 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700986 .flags = 3,
987 .lead_time = 0xAA,
988 .max_kill = 1,
989 .kill_ack_mask = 0,
990 .kill_cts_mask = 0,
991 };
992
Tomas Winkler857485c2008-03-21 13:53:44 -0700993 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800994 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700995}
996
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700997static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700998{
999 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001000 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07001001 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001002 .id = REPLY_SCAN_ABORT_CMD,
1003 .meta.flags = CMD_WANT_SKB,
1004 };
1005
1006 /* If there isn't a scan actively going on in the hardware
1007 * then we are in between scan bands and not actually
1008 * actively scanning, so don't send the abort command */
1009 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1010 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1011 return 0;
1012 }
1013
Tomas Winkler857485c2008-03-21 13:53:44 -07001014 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001015 if (rc) {
1016 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1017 return rc;
1018 }
1019
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001020 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001021 if (res->u.status != CAN_ABORT_STATUS) {
1022 /* The scan abort will return 1 for success or
1023 * 2 for "failure". A failure condition can be
1024 * due to simply not being in an active scan which
1025 * can occur if we send the scan abort before we
1026 * the microcode has notified us that a scan is
1027 * completed. */
1028 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1029 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1030 clear_bit(STATUS_SCAN_HW, &priv->status);
1031 }
1032
1033 dev_kfree_skb_any(cmd.meta.u.skb);
1034
1035 return rc;
1036}
1037
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001038static int iwl4965_card_state_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001039 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001040 struct sk_buff *skb)
1041{
1042 return 1;
1043}
1044
1045/*
1046 * CARD_STATE_CMD
1047 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001048 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001049 *
1050 * When in the 'enable' state the card operates as normal.
1051 * When in the 'disable' state, the card enters into a low power mode.
1052 * When in the 'halt' state, the card is shut down and must be fully
1053 * restarted to come back on.
1054 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001055static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001056{
Tomas Winkler857485c2008-03-21 13:53:44 -07001057 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001058 .id = REPLY_CARD_STATE_CMD,
1059 .len = sizeof(u32),
1060 .data = &flags,
1061 .meta.flags = meta_flag,
1062 };
1063
1064 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001065 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001066
Tomas Winkler857485c2008-03-21 13:53:44 -07001067 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001068}
1069
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001070static int iwl4965_add_sta_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001071 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001072{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001073 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001074
1075 if (!skb) {
1076 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1077 return 1;
1078 }
1079
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001080 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001081 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1082 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1083 res->hdr.flags);
1084 return 1;
1085 }
1086
1087 switch (res->u.add_sta.status) {
1088 case ADD_STA_SUCCESS_MSK:
1089 break;
1090 default:
1091 break;
1092 }
1093
1094 /* We didn't cache the SKB; let the caller free it */
1095 return 1;
1096}
1097
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001098int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001099 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001100{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001101 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001102 int rc = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07001103 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001104 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001105 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001106 .meta.flags = flags,
1107 .data = sta,
1108 };
1109
1110 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001111 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001112 else
1113 cmd.meta.flags |= CMD_WANT_SKB;
1114
Tomas Winkler857485c2008-03-21 13:53:44 -07001115 rc = iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001116
1117 if (rc || (flags & CMD_ASYNC))
1118 return rc;
1119
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001120 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001121 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1122 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1123 res->hdr.flags);
1124 rc = -EIO;
1125 }
1126
1127 if (rc == 0) {
1128 switch (res->u.add_sta.status) {
1129 case ADD_STA_SUCCESS_MSK:
1130 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1131 break;
1132 default:
1133 rc = -EIO;
1134 IWL_WARNING("REPLY_ADD_STA failed\n");
1135 break;
1136 }
1137 }
1138
1139 priv->alloc_rxb_skb--;
1140 dev_kfree_skb_any(cmd.meta.u.skb);
1141
1142 return rc;
1143}
1144
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001145static int iwl4965_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001146 struct ieee80211_key_conf *keyconf,
1147 u8 sta_id)
1148{
1149 unsigned long flags;
1150 __le16 key_flags = 0;
1151
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001152 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
1153 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1154
1155 if (sta_id == priv->hw_setting.bcast_sta_id)
1156 key_flags |= STA_KEY_MULTICAST_MSK;
1157
1158 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1159 keyconf->hw_key_idx = keyconf->keyidx;
1160
1161 key_flags &= ~STA_KEY_FLG_INVALID;
1162
Zhu Yib481de92007-09-25 17:54:57 -07001163 spin_lock_irqsave(&priv->sta_lock, flags);
1164 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1165 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001166
Zhu Yib481de92007-09-25 17:54:57 -07001167 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1168 keyconf->keylen);
1169
1170 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1171 keyconf->keylen);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001172
1173 priv->stations[sta_id].sta.key.key_offset
1174 = (sta_id % STA_KEY_MAX_NUM);/*FIXME*/
Zhu Yib481de92007-09-25 17:54:57 -07001175 priv->stations[sta_id].sta.key.key_flags = key_flags;
1176 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1177 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1178
1179 spin_unlock_irqrestore(&priv->sta_lock, flags);
1180
1181 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001182 return iwl4965_send_add_station(priv,
1183 &priv->stations[sta_id].sta, CMD_ASYNC);
1184}
1185
1186static int iwl4965_set_tkip_dynamic_key_info(struct iwl_priv *priv,
1187 struct ieee80211_key_conf *keyconf,
1188 u8 sta_id)
1189{
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001190 unsigned long flags;
1191 int ret = 0;
1192
1193 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1194 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1195 keyconf->hw_key_idx = keyconf->keyidx;
1196
1197 spin_lock_irqsave(&priv->sta_lock, flags);
1198
1199 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1200 priv->stations[sta_id].keyinfo.conf = keyconf;
1201 priv->stations[sta_id].keyinfo.keylen = 16;
1202
1203 /* This copy is acutally not needed: we get the key with each TX */
1204 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
1205
1206 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1207
1208 spin_unlock_irqrestore(&priv->sta_lock, flags);
1209
1210 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001211}
1212
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001213static int iwl4965_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001214{
1215 unsigned long flags;
1216
1217 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001218 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1219 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001220 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1221 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1222 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1223 spin_unlock_irqrestore(&priv->sta_lock, flags);
1224
1225 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001226 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001227 return 0;
1228}
1229
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001230static int iwl4965_set_dynamic_key(struct iwl_priv *priv,
1231 struct ieee80211_key_conf *key, u8 sta_id)
1232{
1233 int ret;
1234
1235 switch (key->alg) {
1236 case ALG_CCMP:
1237 ret = iwl4965_set_ccmp_dynamic_key_info(priv, key, sta_id);
1238 break;
1239 case ALG_TKIP:
1240 ret = iwl4965_set_tkip_dynamic_key_info(priv, key, sta_id);
1241 break;
1242 case ALG_WEP:
1243 ret = -EOPNOTSUPP;
1244 break;
1245 default:
1246 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
1247 ret = -EINVAL;
1248 }
1249
1250 return ret;
1251}
1252
1253static int iwl4965_remove_static_key(struct iwl_priv *priv)
1254{
1255 int ret = -EOPNOTSUPP;
1256
1257 return ret;
1258}
1259
1260static int iwl4965_set_static_key(struct iwl_priv *priv,
1261 struct ieee80211_key_conf *key)
1262{
1263 if (key->alg == ALG_WEP)
1264 return -EOPNOTSUPP;
1265
1266 IWL_ERROR("Static key invalid: alg %d\n", key->alg);
1267 return -EINVAL;
1268}
1269
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001270static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001271{
1272 struct list_head *element;
1273
1274 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1275 priv->frames_count);
1276
1277 while (!list_empty(&priv->free_frames)) {
1278 element = priv->free_frames.next;
1279 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001280 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001281 priv->frames_count--;
1282 }
1283
1284 if (priv->frames_count) {
1285 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1286 priv->frames_count);
1287 priv->frames_count = 0;
1288 }
1289}
1290
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001291static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001292{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001293 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001294 struct list_head *element;
1295 if (list_empty(&priv->free_frames)) {
1296 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1297 if (!frame) {
1298 IWL_ERROR("Could not allocate frame!\n");
1299 return NULL;
1300 }
1301
1302 priv->frames_count++;
1303 return frame;
1304 }
1305
1306 element = priv->free_frames.next;
1307 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001308 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001309}
1310
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001311static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001312{
1313 memset(frame, 0, sizeof(*frame));
1314 list_add(&frame->list, &priv->free_frames);
1315}
1316
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001317unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001318 struct ieee80211_hdr *hdr,
1319 const u8 *dest, int left)
1320{
1321
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001322 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001323 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1324 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1325 return 0;
1326
1327 if (priv->ibss_beacon->len > left)
1328 return 0;
1329
1330 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1331
1332 return priv->ibss_beacon->len;
1333}
1334
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001335static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001336{
1337 u8 i;
1338
1339 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001340 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001341 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001342 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001343 }
1344
1345 return IWL_RATE_INVALID;
1346}
1347
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001348static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001349{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001350 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001351 unsigned int frame_size;
1352 int rc;
1353 u8 rate;
1354
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001355 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001356
1357 if (!frame) {
1358 IWL_ERROR("Could not obtain free frame buffer for beacon "
1359 "command.\n");
1360 return -ENOMEM;
1361 }
1362
1363 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001364 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001365 0xFF0);
1366 if (rate == IWL_INVALID_RATE)
1367 rate = IWL_RATE_6M_PLCP;
1368 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001369 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001370 if (rate == IWL_INVALID_RATE)
1371 rate = IWL_RATE_1M_PLCP;
1372 }
1373
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001374 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001375
Tomas Winkler857485c2008-03-21 13:53:44 -07001376 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001377 &frame->u.cmd[0]);
1378
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001379 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001380
1381 return rc;
1382}
1383
1384/******************************************************************************
1385 *
Zhu Yib481de92007-09-25 17:54:57 -07001386 * Misc. internal state and helper functions
1387 *
1388 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001389
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001390static void iwl4965_unset_hw_setting(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001391{
1392 if (priv->hw_setting.shared_virt)
1393 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001394 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001395 priv->hw_setting.shared_virt,
1396 priv->hw_setting.shared_phys);
1397}
1398
1399/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001400 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001401 *
1402 * return : set the bit for each supported rate insert in ie
1403 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001404static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001405 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001406{
1407 u16 ret_rates = 0, bit;
1408 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001409 u8 *cnt = ie;
1410 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001411
1412 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1413 if (bit & supported_rate) {
1414 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001415 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001416 ((bit & basic_rate) ? 0x80 : 0x00);
1417 (*cnt)++;
1418 (*left)--;
1419 if ((*left <= 0) ||
1420 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001421 break;
1422 }
1423 }
1424
1425 return ret_rates;
1426}
1427
Zhu Yib481de92007-09-25 17:54:57 -07001428/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001429 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001430 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001431static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001432 enum ieee80211_band band,
1433 struct ieee80211_mgmt *frame,
1434 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001435{
1436 int len = 0;
1437 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001438 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001439#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001440 const struct ieee80211_supported_band *sband =
1441 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001442#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001443
1444 /* Make sure there is enough space for the probe request,
1445 * two mandatory IEs and the data */
1446 left -= 24;
1447 if (left < 0)
1448 return 0;
1449 len += 24;
1450
1451 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001452 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001453 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001454 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001455 frame->seq_ctrl = 0;
1456
1457 /* fill in our indirect SSID IE */
1458 /* ...next IE... */
1459
1460 left -= 2;
1461 if (left < 0)
1462 return 0;
1463 len += 2;
1464 pos = &(frame->u.probe_req.variable[0]);
1465 *pos++ = WLAN_EID_SSID;
1466 *pos++ = 0;
1467
1468 /* fill in our direct SSID IE... */
1469 if (is_direct) {
1470 /* ...next IE... */
1471 left -= 2 + priv->essid_len;
1472 if (left < 0)
1473 return 0;
1474 /* ... fill it in... */
1475 *pos++ = WLAN_EID_SSID;
1476 *pos++ = priv->essid_len;
1477 memcpy(pos, priv->essid, priv->essid_len);
1478 pos += priv->essid_len;
1479 len += 2 + priv->essid_len;
1480 }
1481
1482 /* fill in supported rate */
1483 /* ...next IE... */
1484 left -= 2;
1485 if (left < 0)
1486 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001487
Zhu Yib481de92007-09-25 17:54:57 -07001488 /* ... fill it in... */
1489 *pos++ = WLAN_EID_SUPP_RATES;
1490 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001491
mabbasbee488d2007-10-25 17:15:42 +08001492 /* exclude 60M rate */
1493 active_rates = priv->rates_mask;
1494 active_rates &= ~IWL_RATE_60M_MASK;
1495
1496 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001497
Tomas Winklerc7c46672007-10-18 02:04:15 +02001498 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001499 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001500 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001501 active_rates &= ~ret_rates;
1502
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001503 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001504 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001505 active_rates &= ~ret_rates;
1506
Zhu Yib481de92007-09-25 17:54:57 -07001507 len += 2 + *pos;
1508 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001509 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001510 goto fill_end;
1511
1512 /* fill in supported extended rate */
1513 /* ...next IE... */
1514 left -= 2;
1515 if (left < 0)
1516 return 0;
1517 /* ... fill it in... */
1518 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1519 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001520 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001521 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001522 if (*pos > 0)
1523 len += 2 + *pos;
1524
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001525#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001526 if (sband && sband->ht_info.ht_supported) {
1527 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001528 pos += (*pos) + 1;
1529 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001530 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001531 ht_cap = (struct ieee80211_ht_cap *)pos;
1532 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1533 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1534 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1535 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1536 ((sband->ht_info.ampdu_density << 2) &
1537 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001538 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001539 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001540#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001541
1542 fill_end:
1543 return (u16)len;
1544}
1545
1546/*
1547 * QoS support
1548*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001549static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001550 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001551{
1552
Tomas Winkler857485c2008-03-21 13:53:44 -07001553 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001554 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001555}
1556
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001557static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001558{
1559 unsigned long flags;
1560
Zhu Yib481de92007-09-25 17:54:57 -07001561 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1562 return;
1563
1564 if (!priv->qos_data.qos_enable)
1565 return;
1566
1567 spin_lock_irqsave(&priv->lock, flags);
1568 priv->qos_data.def_qos_parm.qos_flags = 0;
1569
1570 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1571 !priv->qos_data.qos_cap.q_AP.txop_request)
1572 priv->qos_data.def_qos_parm.qos_flags |=
1573 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001574 if (priv->qos_data.qos_active)
1575 priv->qos_data.def_qos_parm.qos_flags |=
1576 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1577
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001578#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001579 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001580 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001581#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001582
Zhu Yib481de92007-09-25 17:54:57 -07001583 spin_unlock_irqrestore(&priv->lock, flags);
1584
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001585 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001586 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1587 priv->qos_data.qos_active,
1588 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001590 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001591 &(priv->qos_data.def_qos_parm));
1592 }
1593}
1594
Zhu Yib481de92007-09-25 17:54:57 -07001595/*
1596 * Power management (not Tx power!) functions
1597 */
1598#define MSEC_TO_USEC 1024
1599
1600#define NOSLP __constant_cpu_to_le16(0), 0, 0
1601#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1602#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1603#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1604 __constant_cpu_to_le32(X1), \
1605 __constant_cpu_to_le32(X2), \
1606 __constant_cpu_to_le32(X3), \
1607 __constant_cpu_to_le32(X4)}
1608
1609
1610/* default power management (not Tx power) table values */
1611/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001612static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001613 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1614 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1615 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1616 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1617 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1618 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1619};
1620
1621/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001622static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001623 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1624 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1625 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1626 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1627 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1628 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1629 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1630 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1631 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1632 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1633};
1634
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001635int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001636{
1637 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001638 struct iwl4965_power_mgr *pow_data;
1639 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001640 u16 pci_pm;
1641
1642 IWL_DEBUG_POWER("Initialize power \n");
1643
1644 pow_data = &(priv->power_data);
1645
1646 memset(pow_data, 0, sizeof(*pow_data));
1647
1648 pow_data->active_index = IWL_POWER_RANGE_0;
1649 pow_data->dtim_val = 0xffff;
1650
1651 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1652 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1653
1654 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1655 if (rc != 0)
1656 return 0;
1657 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001658 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001659
1660 IWL_DEBUG_POWER("adjust power command flags\n");
1661
1662 for (i = 0; i < IWL_POWER_AC; i++) {
1663 cmd = &pow_data->pwr_range_0[i].cmd;
1664
1665 if (pci_pm & 0x1)
1666 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1667 else
1668 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1669 }
1670 }
1671 return rc;
1672}
1673
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001674static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001675 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001676{
1677 int rc = 0, i;
1678 u8 skip;
1679 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001680 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001681 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001682 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001683
1684 if (mode > IWL_POWER_INDEX_5) {
1685 IWL_DEBUG_POWER("Error invalid power mode \n");
1686 return -1;
1687 }
1688 pow_data = &(priv->power_data);
1689
1690 if (pow_data->active_index == IWL_POWER_RANGE_0)
1691 range = &pow_data->pwr_range_0[0];
1692 else
1693 range = &pow_data->pwr_range_1[1];
1694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001695 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001696
1697#ifdef IWL_MAC80211_DISABLE
1698 if (priv->assoc_network != NULL) {
1699 unsigned long flags;
1700
1701 period = priv->assoc_network->tim.tim_period;
1702 }
1703#endif /*IWL_MAC80211_DISABLE */
1704 skip = range[mode].no_dtim;
1705
1706 if (period == 0) {
1707 period = 1;
1708 skip = 0;
1709 }
1710
1711 if (skip == 0) {
1712 max_sleep = period;
1713 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1714 } else {
1715 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1716 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1717 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1718 }
1719
1720 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1721 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1722 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1723 }
1724
1725 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1726 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1727 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1728 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1729 le32_to_cpu(cmd->sleep_interval[0]),
1730 le32_to_cpu(cmd->sleep_interval[1]),
1731 le32_to_cpu(cmd->sleep_interval[2]),
1732 le32_to_cpu(cmd->sleep_interval[3]),
1733 le32_to_cpu(cmd->sleep_interval[4]));
1734
1735 return rc;
1736}
1737
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001738static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001739{
John W. Linville9a62f732007-11-15 16:27:36 -05001740 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001741 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001742 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001743
1744 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001745 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001746 * else user level */
1747 switch (mode) {
1748 case IWL_POWER_BATTERY:
1749 final_mode = IWL_POWER_INDEX_3;
1750 break;
1751 case IWL_POWER_AC:
1752 final_mode = IWL_POWER_MODE_CAM;
1753 break;
1754 default:
1755 final_mode = mode;
1756 break;
1757 }
1758
1759 cmd.keep_alive_beacons = 0;
1760
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001761 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001762
Tomas Winkler857485c2008-03-21 13:53:44 -07001763 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001764
1765 if (final_mode == IWL_POWER_MODE_CAM)
1766 clear_bit(STATUS_POWER_PMI, &priv->status);
1767 else
1768 set_bit(STATUS_POWER_PMI, &priv->status);
1769
1770 return rc;
1771}
1772
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001773int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001774{
1775 /* Filter incoming packets to determine if they are targeted toward
1776 * this network, discarding packets coming from ourselves */
1777 switch (priv->iw_mode) {
1778 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1779 /* packets from our adapter are dropped (echo) */
1780 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1781 return 0;
1782 /* {broad,multi}cast packets to our IBSS go through */
1783 if (is_multicast_ether_addr(header->addr1))
1784 return !compare_ether_addr(header->addr3, priv->bssid);
1785 /* packets to our adapter go through */
1786 return !compare_ether_addr(header->addr1, priv->mac_addr);
1787 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1788 /* packets from our adapter are dropped (echo) */
1789 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1790 return 0;
1791 /* {broad,multi}cast packets to our BSS go through */
1792 if (is_multicast_ether_addr(header->addr1))
1793 return !compare_ether_addr(header->addr2, priv->bssid);
1794 /* packets to our adapter go through */
1795 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001796 default:
1797 break;
Zhu Yib481de92007-09-25 17:54:57 -07001798 }
1799
1800 return 1;
1801}
1802
1803#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1804
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001805static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001806{
1807 switch (status & TX_STATUS_MSK) {
1808 case TX_STATUS_SUCCESS:
1809 return "SUCCESS";
1810 TX_STATUS_ENTRY(SHORT_LIMIT);
1811 TX_STATUS_ENTRY(LONG_LIMIT);
1812 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1813 TX_STATUS_ENTRY(MGMNT_ABORT);
1814 TX_STATUS_ENTRY(NEXT_FRAG);
1815 TX_STATUS_ENTRY(LIFE_EXPIRE);
1816 TX_STATUS_ENTRY(DEST_PS);
1817 TX_STATUS_ENTRY(ABORTED);
1818 TX_STATUS_ENTRY(BT_RETRY);
1819 TX_STATUS_ENTRY(STA_INVALID);
1820 TX_STATUS_ENTRY(FRAG_DROPPED);
1821 TX_STATUS_ENTRY(TID_DISABLE);
1822 TX_STATUS_ENTRY(FRAME_FLUSHED);
1823 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1824 TX_STATUS_ENTRY(TX_LOCKED);
1825 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1826 }
1827
1828 return "UNKNOWN";
1829}
1830
1831/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001832 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001833 *
1834 * NOTE: priv->mutex is not required before calling this function
1835 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001836static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001837{
1838 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1839 clear_bit(STATUS_SCANNING, &priv->status);
1840 return 0;
1841 }
1842
1843 if (test_bit(STATUS_SCANNING, &priv->status)) {
1844 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1845 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1846 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1847 queue_work(priv->workqueue, &priv->abort_scan);
1848
1849 } else
1850 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1851
1852 return test_bit(STATUS_SCANNING, &priv->status);
1853 }
1854
1855 return 0;
1856}
1857
1858/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001859 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001860 * @ms: amount of time to wait (in milliseconds) for scan to abort
1861 *
1862 * NOTE: priv->mutex must be held before calling this function
1863 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001864static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001865{
1866 unsigned long now = jiffies;
1867 int ret;
1868
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001869 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001870 if (ret && ms) {
1871 mutex_unlock(&priv->mutex);
1872 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1873 test_bit(STATUS_SCANNING, &priv->status))
1874 msleep(1);
1875 mutex_lock(&priv->mutex);
1876
1877 return test_bit(STATUS_SCANNING, &priv->status);
1878 }
1879
1880 return ret;
1881}
1882
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001883static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001884{
1885 /* Reset ieee stats */
1886
1887 /* We don't reset the net_device_stats (ieee->stats) on
1888 * re-association */
1889
1890 priv->last_seq_num = -1;
1891 priv->last_frag_num = -1;
1892 priv->last_packet_time = 0;
1893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001894 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001895}
1896
1897#define MAX_UCODE_BEACON_INTERVAL 4096
1898#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1899
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001900static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001901{
1902 u16 new_val = 0;
1903 u16 beacon_factor = 0;
1904
1905 beacon_factor =
1906 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1907 / MAX_UCODE_BEACON_INTERVAL;
1908 new_val = beacon_val / beacon_factor;
1909
1910 return cpu_to_le16(new_val);
1911}
1912
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001913static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001914{
1915 u64 interval_tm_unit;
1916 u64 tsf, result;
1917 unsigned long flags;
1918 struct ieee80211_conf *conf = NULL;
1919 u16 beacon_int = 0;
1920
1921 conf = ieee80211_get_hw_conf(priv->hw);
1922
1923 spin_lock_irqsave(&priv->lock, flags);
1924 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
1925 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
1926
1927 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1928
1929 tsf = priv->timestamp1;
1930 tsf = ((tsf << 32) | priv->timestamp0);
1931
1932 beacon_int = priv->beacon_int;
1933 spin_unlock_irqrestore(&priv->lock, flags);
1934
1935 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1936 if (beacon_int == 0) {
1937 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1938 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1939 } else {
1940 priv->rxon_timing.beacon_interval =
1941 cpu_to_le16(beacon_int);
1942 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001943 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001944 le16_to_cpu(priv->rxon_timing.beacon_interval));
1945 }
1946
1947 priv->rxon_timing.atim_window = 0;
1948 } else {
1949 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001950 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001951 /* TODO: we need to get atim_window from upper stack
1952 * for now we set to 0 */
1953 priv->rxon_timing.atim_window = 0;
1954 }
1955
1956 interval_tm_unit =
1957 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1958 result = do_div(tsf, interval_tm_unit);
1959 priv->rxon_timing.beacon_init_val =
1960 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1961
1962 IWL_DEBUG_ASSOC
1963 ("beacon interval %d beacon timer %d beacon tim %d\n",
1964 le16_to_cpu(priv->rxon_timing.beacon_interval),
1965 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1966 le16_to_cpu(priv->rxon_timing.atim_window));
1967}
1968
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001969static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001970{
1971 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1972 IWL_ERROR("APs don't scan.\n");
1973 return 0;
1974 }
1975
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001976 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001977 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1978 return -EIO;
1979 }
1980
1981 if (test_bit(STATUS_SCANNING, &priv->status)) {
1982 IWL_DEBUG_SCAN("Scan already in progress.\n");
1983 return -EAGAIN;
1984 }
1985
1986 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1987 IWL_DEBUG_SCAN("Scan request while abort pending. "
1988 "Queuing.\n");
1989 return -EAGAIN;
1990 }
1991
1992 IWL_DEBUG_INFO("Starting scan...\n");
1993 priv->scan_bands = 2;
1994 set_bit(STATUS_SCANNING, &priv->status);
1995 priv->scan_start = jiffies;
1996 priv->scan_pass_start = priv->scan_start;
1997
1998 queue_work(priv->workqueue, &priv->request_scan);
1999
2000 return 0;
2001}
2002
Zhu Yib481de92007-09-25 17:54:57 -07002003
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002004static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002005 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002006{
Johannes Berg8318d782008-01-24 19:38:38 +01002007 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002008 priv->staging_rxon.flags &=
2009 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2010 | RXON_FLG_CCK_MSK);
2011 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2012 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002013 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002014 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2015 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2016 else
2017 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2018
2019 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2020 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2021
2022 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2023 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2024 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2025 }
2026}
2027
2028/*
Ian Schram01ebd062007-10-25 17:15:22 +08002029 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002030 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002031static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002032{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002033 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002034
2035 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2036
2037 switch (priv->iw_mode) {
2038 case IEEE80211_IF_TYPE_AP:
2039 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2040 break;
2041
2042 case IEEE80211_IF_TYPE_STA:
2043 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2044 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2045 break;
2046
2047 case IEEE80211_IF_TYPE_IBSS:
2048 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2049 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2050 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2051 RXON_FILTER_ACCEPT_GRP_MSK;
2052 break;
2053
2054 case IEEE80211_IF_TYPE_MNTR:
2055 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2056 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2057 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2058 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002059 default:
2060 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
2061 break;
Zhu Yib481de92007-09-25 17:54:57 -07002062 }
2063
2064#if 0
2065 /* TODO: Figure out when short_preamble would be set and cache from
2066 * that */
2067 if (!hw_to_local(priv->hw)->short_preamble)
2068 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2069 else
2070 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2071#endif
2072
Assaf Krauss8622e702008-03-21 13:53:43 -07002073 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002074 le16_to_cpu(priv->staging_rxon.channel));
2075
2076 if (!ch_info)
2077 ch_info = &priv->channel_info[0];
2078
2079 /*
2080 * in some case A channels are all non IBSS
2081 * in this case force B/G channel
2082 */
2083 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2084 !(is_channel_ibss(ch_info)))
2085 ch_info = &priv->channel_info[0];
2086
2087 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002088 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002089
Johannes Berg8318d782008-01-24 19:38:38 +01002090 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002091
2092 priv->staging_rxon.ofdm_basic_rates =
2093 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2094 priv->staging_rxon.cck_basic_rates =
2095 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2096
2097 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2098 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2099 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2100 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2101 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2102 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2103 iwl4965_set_rxon_chain(priv);
2104}
2105
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002106static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002107{
Zhu Yib481de92007-09-25 17:54:57 -07002108 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002109 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002110
Assaf Krauss8622e702008-03-21 13:53:43 -07002111 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002112 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002113 le16_to_cpu(priv->staging_rxon.channel));
2114
2115 if (!ch_info || !is_channel_ibss(ch_info)) {
2116 IWL_ERROR("channel %d not IBSS channel\n",
2117 le16_to_cpu(priv->staging_rxon.channel));
2118 return -EINVAL;
2119 }
2120 }
2121
Zhu Yib481de92007-09-25 17:54:57 -07002122 priv->iw_mode = mode;
2123
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002124 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002125 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2126
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002127 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002128
Mohamed Abbasfde35712007-11-29 11:10:15 +08002129 /* dont commit rxon if rf-kill is on*/
2130 if (!iwl4965_is_ready_rf(priv))
2131 return -EAGAIN;
2132
2133 cancel_delayed_work(&priv->scan_check);
2134 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2135 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2136 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2137 return -EAGAIN;
2138 }
2139
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002140 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002141
2142 return 0;
2143}
2144
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002145static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002146 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07002147 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002148 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002149 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002150{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002151 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002152
2153 switch (keyinfo->alg) {
2154 case ALG_CCMP:
2155 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2156 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07002157 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2158 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002159 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2160 break;
2161
2162 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07002163 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07002164 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
2165 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
2166 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002167 break;
2168
2169 case ALG_WEP:
2170 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2171 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2172
2173 if (keyinfo->keylen == 13)
2174 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2175
2176 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2177
2178 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2179 "with key %d\n", ctl->key_idx);
2180 break;
2181
Zhu Yib481de92007-09-25 17:54:57 -07002182 default:
2183 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2184 break;
2185 }
2186}
2187
2188/*
2189 * handle build REPLY_TX command notification.
2190 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002191static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07002192 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002193 struct ieee80211_tx_control *ctrl,
2194 struct ieee80211_hdr *hdr,
2195 int is_unicast, u8 std_id)
2196{
2197 __le16 *qc;
2198 u16 fc = le16_to_cpu(hdr->frame_control);
2199 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2200
2201 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2202 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2203 tx_flags |= TX_CMD_FLG_ACK_MSK;
2204 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2205 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2206 if (ieee80211_is_probe_response(fc) &&
2207 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2208 tx_flags |= TX_CMD_FLG_TSF_MSK;
2209 } else {
2210 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2211 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2212 }
2213
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002214 if (ieee80211_is_back_request(fc))
2215 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2216
2217
Zhu Yib481de92007-09-25 17:54:57 -07002218 cmd->cmd.tx.sta_id = std_id;
2219 if (ieee80211_get_morefrag(hdr))
2220 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2221
2222 qc = ieee80211_get_qos_ctrl(hdr);
2223 if (qc) {
2224 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2225 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2226 } else
2227 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2228
2229 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2230 tx_flags |= TX_CMD_FLG_RTS_MSK;
2231 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2232 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2233 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2234 tx_flags |= TX_CMD_FLG_CTS_MSK;
2235 }
2236
2237 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2238 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2239
2240 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2241 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2242 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2243 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002244 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002245 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002246 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002247 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002248 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002249 }
Zhu Yib481de92007-09-25 17:54:57 -07002250
2251 cmd->cmd.tx.driver_txop = 0;
2252 cmd->cmd.tx.tx_flags = tx_flags;
2253 cmd->cmd.tx.next_frame_len = 0;
2254}
Tomas Winkler19758be2008-03-12 16:58:51 -07002255static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2256{
2257 /* 0 - mgmt, 1 - cnt, 2 - data */
2258 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2259 priv->tx_stats[idx].cnt++;
2260 priv->tx_stats[idx].bytes += len;
2261}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002262/**
2263 * iwl4965_get_sta_id - Find station's index within station table
2264 *
2265 * If new IBSS station, create new entry in station table
2266 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002267static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002268 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002269{
2270 int sta_id;
2271 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002272 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002273
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002274 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002275 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2276 is_multicast_ether_addr(hdr->addr1))
2277 return priv->hw_setting.bcast_sta_id;
2278
2279 switch (priv->iw_mode) {
2280
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002281 /* If we are a client station in a BSS network, use the special
2282 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002283 case IEEE80211_IF_TYPE_STA:
2284 return IWL_AP_ID;
2285
2286 /* If we are an AP, then find the station, or use BCAST */
2287 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002288 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002289 if (sta_id != IWL_INVALID_STATION)
2290 return sta_id;
2291 return priv->hw_setting.bcast_sta_id;
2292
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002293 /* If this frame is going out to an IBSS network, find the station,
2294 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002295 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002296 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002297 if (sta_id != IWL_INVALID_STATION)
2298 return sta_id;
2299
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002300 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002301 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2302 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002303
2304 if (sta_id != IWL_INVALID_STATION)
2305 return sta_id;
2306
Joe Perches0795af52007-10-03 17:59:30 -07002307 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002308 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002309 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002310 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002311 return priv->hw_setting.bcast_sta_id;
2312
2313 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002314 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002315 return priv->hw_setting.bcast_sta_id;
2316 }
2317}
2318
2319/*
2320 * start REPLY_TX command process
2321 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002322static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002323 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2324{
2325 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002326 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002327 u32 *control_flags;
2328 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002329 struct iwl4965_tx_queue *txq = NULL;
2330 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002331 dma_addr_t phys_addr;
2332 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002333 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07002334 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002335 u16 len, idx, len_org;
2336 u8 id, hdr_len, unicast;
2337 u8 sta_id;
2338 u16 seq_number = 0;
2339 u16 fc;
2340 __le16 *qc;
2341 u8 wait_write_ptr = 0;
2342 unsigned long flags;
2343 int rc;
2344
2345 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002346 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002347 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2348 goto drop_unlock;
2349 }
2350
Johannes Berg32bfd352007-12-19 01:31:26 +01002351 if (!priv->vif) {
2352 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002353 goto drop_unlock;
2354 }
2355
Johannes Berg8318d782008-01-24 19:38:38 +01002356 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002357 IWL_ERROR("ERROR: No TX rate available.\n");
2358 goto drop_unlock;
2359 }
2360
2361 unicast = !is_multicast_ether_addr(hdr->addr1);
2362 id = 0;
2363
2364 fc = le16_to_cpu(hdr->frame_control);
2365
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002366#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002367 if (ieee80211_is_auth(fc))
2368 IWL_DEBUG_TX("Sending AUTH frame\n");
2369 else if (ieee80211_is_assoc_request(fc))
2370 IWL_DEBUG_TX("Sending ASSOC frame\n");
2371 else if (ieee80211_is_reassoc_request(fc))
2372 IWL_DEBUG_TX("Sending REASSOC frame\n");
2373#endif
2374
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002375 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002376 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2377 (!iwl4965_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002378 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002379 !priv->assoc_station_added)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002380 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002381 goto drop_unlock;
2382 }
2383
2384 spin_unlock_irqrestore(&priv->lock, flags);
2385
2386 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002387
2388 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002389 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002390 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002391 DECLARE_MAC_BUF(mac);
2392
2393 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2394 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002395 goto drop;
2396 }
2397
2398 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2399
2400 qc = ieee80211_get_qos_ctrl(hdr);
2401 if (qc) {
2402 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2403 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2404 IEEE80211_SCTL_SEQ;
2405 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2406 (hdr->seq_ctrl &
2407 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2408 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002409#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002410 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002411 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002412 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002413 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002414#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002415 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002416
2417 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002418 txq = &priv->txq[txq_id];
2419 q = &txq->q;
2420
2421 spin_lock_irqsave(&priv->lock, flags);
2422
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002423 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002424 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002425 memset(tfd, 0, sizeof(*tfd));
2426 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002427 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002428
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002429 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002430 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002431 txq->txb[q->write_ptr].skb[0] = skb;
2432 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002433 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002434
2435 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002436 out_cmd = &txq->cmd[idx];
2437 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2438 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002439
2440 /*
2441 * Set up the Tx-command (not MAC!) header.
2442 * Store the chosen Tx queue and TFD index within the sequence field;
2443 * after Tx, uCode's Tx response will return this value so driver can
2444 * locate the frame within the tx queue and do post-tx processing.
2445 */
Zhu Yib481de92007-09-25 17:54:57 -07002446 out_cmd->hdr.cmd = REPLY_TX;
2447 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002448 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002449
2450 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002451 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2452
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002453 /*
2454 * Use the first empty entry in this queue's command buffer array
2455 * to contain the Tx command and MAC header concatenated together
2456 * (payload data will be in another buffer).
2457 * Size of this varies, due to varying MAC header length.
2458 * If end is not dword aligned, we'll have 2 extra bytes at the end
2459 * of the MAC header (device reads on dword boundaries).
2460 * We'll tell device about this padding later.
2461 */
Zhu Yib481de92007-09-25 17:54:57 -07002462 len = priv->hw_setting.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002463 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002464
2465 len_org = len;
2466 len = (len + 3) & ~3;
2467
2468 if (len_org != len)
2469 len_org = 1;
2470 else
2471 len_org = 0;
2472
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002473 /* Physical address of this Tx command's header (not MAC header!),
2474 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002475 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2476 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002477
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002478 /* Add buffer containing Tx command and MAC(!) header to TFD's
2479 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002480 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002481
2482 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002483 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002484
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002485 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2486 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002487 len = skb->len - hdr_len;
2488 if (len) {
2489 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2490 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002491 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002492 }
2493
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002494 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002495 if (len_org)
2496 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2497
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002498 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002499 len = (u16)skb->len;
2500 out_cmd->cmd.tx.len = cpu_to_le16(len);
2501
2502 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002503 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002504
2505 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002506 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002507
Tomas Winkler19758be2008-03-12 16:58:51 -07002508 iwl_update_tx_stats(priv, fc, len);
2509
Tomas Winkler857485c2008-03-21 13:53:44 -07002510 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002511 offsetof(struct iwl4965_tx_cmd, scratch);
2512 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2513 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2514
Zhu Yib481de92007-09-25 17:54:57 -07002515 if (!ieee80211_get_morefrag(hdr)) {
2516 txq->need_update = 1;
2517 if (qc) {
2518 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2519 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2520 }
2521 } else {
2522 wait_write_ptr = 1;
2523 txq->need_update = 0;
2524 }
2525
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002526 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002527 sizeof(out_cmd->cmd.tx));
2528
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002529 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002530 ieee80211_get_hdrlen(fc));
2531
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002532 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002533 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2534
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002535 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002536 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002537 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002538 spin_unlock_irqrestore(&priv->lock, flags);
2539
2540 if (rc)
2541 return rc;
2542
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002543 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002544 && priv->mac80211_registered) {
2545 if (wait_write_ptr) {
2546 spin_lock_irqsave(&priv->lock, flags);
2547 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002548 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002549 spin_unlock_irqrestore(&priv->lock, flags);
2550 }
2551
2552 ieee80211_stop_queue(priv->hw, ctl->queue);
2553 }
2554
2555 return 0;
2556
2557drop_unlock:
2558 spin_unlock_irqrestore(&priv->lock, flags);
2559drop:
2560 return -1;
2561}
2562
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002563static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002564{
Johannes Berg8318d782008-01-24 19:38:38 +01002565 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002566 struct ieee80211_rate *rate;
2567 int i;
2568
Johannes Berg8318d782008-01-24 19:38:38 +01002569 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002570 if (!hw) {
2571 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2572 return;
2573 }
Zhu Yib481de92007-09-25 17:54:57 -07002574
2575 priv->active_rate = 0;
2576 priv->active_rate_basic = 0;
2577
Johannes Berg8318d782008-01-24 19:38:38 +01002578 for (i = 0; i < hw->n_bitrates; i++) {
2579 rate = &(hw->bitrates[i]);
2580 if (rate->hw_value < IWL_RATE_COUNT)
2581 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002582 }
2583
2584 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2585 priv->active_rate, priv->active_rate_basic);
2586
2587 /*
2588 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2589 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2590 * OFDM
2591 */
2592 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2593 priv->staging_rxon.cck_basic_rates =
2594 ((priv->active_rate_basic &
2595 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2596 else
2597 priv->staging_rxon.cck_basic_rates =
2598 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2599
2600 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2601 priv->staging_rxon.ofdm_basic_rates =
2602 ((priv->active_rate_basic &
2603 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2604 IWL_FIRST_OFDM_RATE) & 0xFF;
2605 else
2606 priv->staging_rxon.ofdm_basic_rates =
2607 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2608}
2609
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002610void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002611{
2612 unsigned long flags;
2613
2614 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2615 return;
2616
2617 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2618 disable_radio ? "OFF" : "ON");
2619
2620 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002621 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002622 /* FIXME: This is a workaround for AP */
2623 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2624 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002625 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002626 CSR_UCODE_SW_BIT_RFKILL);
2627 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002628 /* call the host command only if no hw rf-kill set */
2629 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
2630 iwl4965_send_card_state(priv,
2631 CARD_STATE_CMD_DISABLE,
2632 0);
Zhu Yib481de92007-09-25 17:54:57 -07002633 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002634
2635 /* make sure mac80211 stop sending Tx frame */
2636 if (priv->mac80211_registered)
2637 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002638 }
2639 return;
2640 }
2641
2642 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002643 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002644
2645 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2646 spin_unlock_irqrestore(&priv->lock, flags);
2647
2648 /* wake up ucode */
2649 msleep(10);
2650
2651 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002652 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2653 if (!iwl_grab_nic_access(priv))
2654 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002655 spin_unlock_irqrestore(&priv->lock, flags);
2656
2657 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2658 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2659 "disabled by HW switch\n");
2660 return;
2661 }
2662
2663 queue_work(priv->workqueue, &priv->restart);
2664 return;
2665}
2666
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002667void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002668 u32 decrypt_res, struct ieee80211_rx_status *stats)
2669{
2670 u16 fc =
2671 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2672
2673 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2674 return;
2675
2676 if (!(fc & IEEE80211_FCTL_PROTECTED))
2677 return;
2678
2679 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2680 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2681 case RX_RES_STATUS_SEC_TYPE_TKIP:
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002682 /* The uCode has got a bad phase 1 Key, pushes the packet.
2683 * Decryption will be done in SW. */
2684 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2685 RX_RES_STATUS_BAD_KEY_TTAK)
2686 break;
2687
Zhu Yib481de92007-09-25 17:54:57 -07002688 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2689 RX_RES_STATUS_BAD_ICV_MIC)
2690 stats->flag |= RX_FLAG_MMIC_ERROR;
2691 case RX_RES_STATUS_SEC_TYPE_WEP:
2692 case RX_RES_STATUS_SEC_TYPE_CCMP:
2693 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2694 RX_RES_STATUS_DECRYPT_OK) {
2695 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2696 stats->flag |= RX_FLAG_DECRYPTED;
2697 }
2698 break;
2699
2700 default:
2701 break;
2702 }
2703}
2704
Zhu Yib481de92007-09-25 17:54:57 -07002705
2706#define IWL_PACKET_RETRY_TIME HZ
2707
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002708int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002709{
2710 u16 sc = le16_to_cpu(header->seq_ctrl);
2711 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2712 u16 frag = sc & IEEE80211_SCTL_FRAG;
2713 u16 *last_seq, *last_frag;
2714 unsigned long *last_time;
2715
2716 switch (priv->iw_mode) {
2717 case IEEE80211_IF_TYPE_IBSS:{
2718 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002719 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002720 u8 *mac = header->addr2;
2721 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2722
2723 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002724 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002725 if (!compare_ether_addr(entry->mac, mac))
2726 break;
2727 }
2728 if (p == &priv->ibss_mac_hash[index]) {
2729 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2730 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002731 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002732 return 0;
2733 }
2734 memcpy(entry->mac, mac, ETH_ALEN);
2735 entry->seq_num = seq;
2736 entry->frag_num = frag;
2737 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002738 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002739 return 0;
2740 }
2741 last_seq = &entry->seq_num;
2742 last_frag = &entry->frag_num;
2743 last_time = &entry->packet_time;
2744 break;
2745 }
2746 case IEEE80211_IF_TYPE_STA:
2747 last_seq = &priv->last_seq_num;
2748 last_frag = &priv->last_frag_num;
2749 last_time = &priv->last_packet_time;
2750 break;
2751 default:
2752 return 0;
2753 }
2754 if ((*last_seq == seq) &&
2755 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2756 if (*last_frag == frag)
2757 goto drop;
2758 if (*last_frag + 1 != frag)
2759 /* out-of-order fragment */
2760 goto drop;
2761 } else
2762 *last_seq = seq;
2763
2764 *last_frag = frag;
2765 *last_time = jiffies;
2766 return 0;
2767
2768 drop:
2769 return 1;
2770}
2771
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002772#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002773
2774#include "iwl-spectrum.h"
2775
2776#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2777#define BEACON_TIME_MASK_HIGH 0xFF000000
2778#define TIME_UNIT 1024
2779
2780/*
2781 * extended beacon time format
2782 * time in usec will be changed into a 32-bit value in 8:24 format
2783 * the high 1 byte is the beacon counts
2784 * the lower 3 bytes is the time in usec within one beacon interval
2785 */
2786
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002787static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002788{
2789 u32 quot;
2790 u32 rem;
2791 u32 interval = beacon_interval * 1024;
2792
2793 if (!interval || !usec)
2794 return 0;
2795
2796 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2797 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2798
2799 return (quot << 24) + rem;
2800}
2801
2802/* base is usually what we get from ucode with each received frame,
2803 * the same as HW timer counter counting down
2804 */
2805
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002806static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002807{
2808 u32 base_low = base & BEACON_TIME_MASK_LOW;
2809 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2810 u32 interval = beacon_interval * TIME_UNIT;
2811 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2812 (addon & BEACON_TIME_MASK_HIGH);
2813
2814 if (base_low > addon_low)
2815 res += base_low - addon_low;
2816 else if (base_low < addon_low) {
2817 res += interval + base_low - addon_low;
2818 res += (1 << 24);
2819 } else
2820 res += (1 << 24);
2821
2822 return cpu_to_le32(res);
2823}
2824
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002825static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002826 struct ieee80211_measurement_params *params,
2827 u8 type)
2828{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002829 struct iwl4965_spectrum_cmd spectrum;
2830 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002831 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002832 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2833 .data = (void *)&spectrum,
2834 .meta.flags = CMD_WANT_SKB,
2835 };
2836 u32 add_time = le64_to_cpu(params->start_time);
2837 int rc;
2838 int spectrum_resp_status;
2839 int duration = le16_to_cpu(params->duration);
2840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002841 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002842 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002843 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002844 le64_to_cpu(params->start_time) - priv->last_tsf,
2845 le16_to_cpu(priv->rxon_timing.beacon_interval));
2846
2847 memset(&spectrum, 0, sizeof(spectrum));
2848
2849 spectrum.channel_count = cpu_to_le16(1);
2850 spectrum.flags =
2851 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2852 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2853 cmd.len = sizeof(spectrum);
2854 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2855
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002856 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002857 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002858 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002859 add_time,
2860 le16_to_cpu(priv->rxon_timing.beacon_interval));
2861 else
2862 spectrum.start_time = 0;
2863
2864 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2865 spectrum.channels[0].channel = params->channel;
2866 spectrum.channels[0].type = type;
2867 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2868 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2869 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2870
Tomas Winkler857485c2008-03-21 13:53:44 -07002871 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002872 if (rc)
2873 return rc;
2874
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002875 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002876 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2877 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2878 rc = -EIO;
2879 }
2880
2881 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2882 switch (spectrum_resp_status) {
2883 case 0: /* Command will be handled */
2884 if (res->u.spectrum.id != 0xff) {
2885 IWL_DEBUG_INFO
2886 ("Replaced existing measurement: %d\n",
2887 res->u.spectrum.id);
2888 priv->measurement_status &= ~MEASUREMENT_READY;
2889 }
2890 priv->measurement_status |= MEASUREMENT_ACTIVE;
2891 rc = 0;
2892 break;
2893
2894 case 1: /* Command will not be handled */
2895 rc = -EAGAIN;
2896 break;
2897 }
2898
2899 dev_kfree_skb_any(cmd.meta.u.skb);
2900
2901 return rc;
2902}
2903#endif
2904
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002905static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002906 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002907{
2908
2909 tx_sta->status.ack_signal = 0;
2910 tx_sta->status.excessive_retries = 0;
2911 tx_sta->status.queue_length = 0;
2912 tx_sta->status.queue_number = 0;
2913
2914 if (in_interrupt())
2915 ieee80211_tx_status_irqsafe(priv->hw,
2916 tx_sta->skb[0], &(tx_sta->status));
2917 else
2918 ieee80211_tx_status(priv->hw,
2919 tx_sta->skb[0], &(tx_sta->status));
2920
2921 tx_sta->skb[0] = NULL;
2922}
2923
2924/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002925 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002926 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002927 * When FW advances 'R' index, all entries between old and new 'R' index
2928 * need to be reclaimed. As result, some free space forms. If there is
2929 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002930 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002931int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002932{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002933 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2934 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002935 int nfreed = 0;
2936
2937 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2938 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2939 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002940 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002941 return 0;
2942 }
2943
Tomas Winklerc54b6792008-03-06 17:36:53 -08002944 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002945 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002946 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002947 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002948 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002949 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002950 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002951 } else if (nfreed > 1) {
2952 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002953 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002954 queue_work(priv->workqueue, &priv->restart);
2955 }
2956 nfreed++;
2957 }
2958
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002959/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002960 (txq_id != IWL_CMD_QUEUE_NUM) &&
2961 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002962 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002963
2964
2965 return nfreed;
2966}
2967
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002968static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002969{
2970 status &= TX_STATUS_MSK;
2971 return (status == TX_STATUS_SUCCESS)
2972 || (status == TX_STATUS_DIRECT_DONE);
2973}
2974
2975/******************************************************************************
2976 *
2977 * Generic RX handler implementations
2978 *
2979 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002980#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002981
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002982static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002983 struct ieee80211_hdr *hdr)
2984{
2985 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2986 return IWL_AP_ID;
2987 else {
2988 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002989 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002990 }
2991}
2992
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002993static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002994 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002995{
2996 if (priv->txq[txq_id].txb[idx].skb[0])
2997 return (struct ieee80211_hdr *)priv->txq[txq_id].
2998 txb[idx].skb[0]->data;
2999 return NULL;
3000}
3001
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003002static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003003{
3004 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3005 tx_resp->frame_count);
3006 return le32_to_cpu(*scd_ssn) & MAX_SN;
3007
3008}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003009
3010/**
3011 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3012 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003013static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003014 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003015 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003016 u16 start_idx)
3017{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003018 u16 status;
3019 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07003020 struct ieee80211_tx_status *tx_status = NULL;
3021 struct ieee80211_hdr *hdr = NULL;
3022 int i, sh;
3023 int txq_id, idx;
3024 u16 seq;
3025
3026 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003027 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003028
3029 agg->frame_count = tx_resp->frame_count;
3030 agg->start_idx = start_idx;
3031 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003032 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003033
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003034 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003035 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003036 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003037 status = le16_to_cpu(frame_status[0].status);
3038 seq = le16_to_cpu(frame_status[0].sequence);
3039 idx = SEQ_TO_INDEX(seq);
3040 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003041
Zhu Yib481de92007-09-25 17:54:57 -07003042 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003043 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3044 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003045
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003046 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003047 tx_status->retry_count = tx_resp->failure_frame;
3048 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003049 tx_status->queue_length = tx_resp->failure_rts;
3050 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003051 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003052 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003053 iwl4965_hwrate_to_tx_control(priv,
3054 le32_to_cpu(tx_resp->rate_n_flags),
3055 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003056 /* FIXME: code repetition end */
3057
3058 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3059 status & 0xff, tx_resp->failure_frame);
3060 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003061 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003062
3063 agg->wait_for_ba = 0;
3064 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003065 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003066 u64 bitmap = 0;
3067 int start = agg->start_idx;
3068
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003069 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003070 for (i = 0; i < agg->frame_count; i++) {
3071 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003072 status = le16_to_cpu(frame_status[i].status);
3073 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003074 idx = SEQ_TO_INDEX(seq);
3075 txq_id = SEQ_TO_QUEUE(seq);
3076
3077 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3078 AGG_TX_STATE_ABORT_MSK))
3079 continue;
3080
3081 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3082 agg->frame_count, txq_id, idx);
3083
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003084 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003085
3086 sc = le16_to_cpu(hdr->seq_ctrl);
3087 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3088 IWL_ERROR("BUG_ON idx doesn't match seq control"
3089 " idx=%d, seq_idx=%d, seq=%d\n",
3090 idx, SEQ_TO_SN(sc),
3091 hdr->seq_ctrl);
3092 return -1;
3093 }
3094
3095 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3096 i, idx, SEQ_TO_SN(sc));
3097
3098 sh = idx - start;
3099 if (sh > 64) {
3100 sh = (start - idx) + 0xff;
3101 bitmap = bitmap << sh;
3102 sh = 0;
3103 start = idx;
3104 } else if (sh < -64)
3105 sh = 0xff - (start - idx);
3106 else if (sh < 0) {
3107 sh = start - idx;
3108 start = idx;
3109 bitmap = bitmap << sh;
3110 sh = 0;
3111 }
3112 bitmap |= (1 << sh);
3113 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3114 start, (u32)(bitmap & 0xFFFFFFFF));
3115 }
3116
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003117 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003118 agg->start_idx = start;
3119 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003120 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003121 agg->frame_count, agg->start_idx,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003122 agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003123
3124 if (bitmap)
3125 agg->wait_for_ba = 1;
3126 }
3127 return 0;
3128}
3129#endif
Zhu Yib481de92007-09-25 17:54:57 -07003130
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003131/**
3132 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3133 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003134static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003135 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003136{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003137 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003138 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3139 int txq_id = SEQ_TO_QUEUE(sequence);
3140 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003141 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003142 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003143 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003144 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003145#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003146 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3147 struct ieee80211_hdr *hdr;
3148 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003149#endif
3150
3151 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3152 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3153 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003154 index, txq->q.n_bd, txq->q.write_ptr,
3155 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003156 return;
3157 }
3158
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003159#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003160 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3161 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003162
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003163 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003164 tid = le16_to_cpu(*qc) & 0xf;
3165
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003166 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3167 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3168 IWL_ERROR("Station not known\n");
3169 return;
3170 }
3171
3172 if (txq->sched_retry) {
3173 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3174 struct iwl4965_ht_agg *agg = NULL;
3175
3176 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003177 return;
Zhu Yib481de92007-09-25 17:54:57 -07003178
3179 agg = &priv->stations[sta_id].tid[tid].agg;
3180
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003181 iwl4965_tx_status_reply_tx(priv, agg,
3182 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003183
3184 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003185 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003186 /* TODO: send BAR */
3187 }
3188
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003189 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3190 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003191 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003192 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3193 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003194 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3195 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3196
3197 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3198 txq_id >= 0 && priv->mac80211_registered &&
3199 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3200 ieee80211_wake_queue(priv->hw, txq_id);
3201
3202 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003203 }
3204 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003205#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003206 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003207
3208 tx_status->retry_count = tx_resp->failure_frame;
3209 tx_status->queue_number = status;
3210 tx_status->queue_length = tx_resp->bt_kill_count;
3211 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003212 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003213 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003214 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3215 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003216
Zhu Yib481de92007-09-25 17:54:57 -07003217 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003218 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003219 status, le32_to_cpu(tx_resp->rate_n_flags),
3220 tx_resp->failure_frame);
3221
3222 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003223 if (index != -1) {
3224 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003225#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003226 if (tid != MAX_TID_COUNT)
3227 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3228 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3229 (txq_id >= 0) &&
3230 priv->mac80211_registered)
3231 ieee80211_wake_queue(priv->hw, txq_id);
3232 if (tid != MAX_TID_COUNT)
3233 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3234#endif
Zhu Yib481de92007-09-25 17:54:57 -07003235 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003236#ifdef CONFIG_IWL4965_HT
3237 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003238#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003239
3240 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3241 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3242}
3243
3244
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003245static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003246 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003247{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003248 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3249 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003250 struct delayed_work *pwork;
3251
3252 palive = &pkt->u.alive_frame;
3253
3254 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3255 "0x%01X 0x%01X\n",
3256 palive->is_valid, palive->ver_type,
3257 palive->ver_subtype);
3258
3259 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3260 IWL_DEBUG_INFO("Initialization Alive received.\n");
3261 memcpy(&priv->card_alive_init,
3262 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003263 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003264 pwork = &priv->init_alive_start;
3265 } else {
3266 IWL_DEBUG_INFO("Runtime Alive received.\n");
3267 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003268 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003269 pwork = &priv->alive_start;
3270 }
3271
3272 /* We delay the ALIVE response by 5ms to
3273 * give the HW RF Kill time to activate... */
3274 if (palive->is_valid == UCODE_VALID_OK)
3275 queue_delayed_work(priv->workqueue, pwork,
3276 msecs_to_jiffies(5));
3277 else
3278 IWL_WARNING("uCode did not respond OK.\n");
3279}
3280
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003281static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003282 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003283{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003284 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003285
3286 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3287 return;
3288}
3289
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003290static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003291 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003292{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003293 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003294
3295 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3296 "seq 0x%04X ser 0x%08X\n",
3297 le32_to_cpu(pkt->u.err_resp.error_type),
3298 get_cmd_string(pkt->u.err_resp.cmd_id),
3299 pkt->u.err_resp.cmd_id,
3300 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3301 le32_to_cpu(pkt->u.err_resp.error_info));
3302}
3303
3304#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3305
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003306static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003307{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003308 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3309 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3310 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003311 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3312 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3313 rxon->channel = csa->channel;
3314 priv->staging_rxon.channel = csa->channel;
3315}
3316
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003317static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003318 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003319{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003320#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003321 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3322 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003323
3324 if (!report->state) {
3325 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3326 "Spectrum Measure Notification: Start\n");
3327 return;
3328 }
3329
3330 memcpy(&priv->measure_report, report, sizeof(*report));
3331 priv->measurement_status |= MEASUREMENT_READY;
3332#endif
3333}
3334
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003335static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003336 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003337{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003338#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003339 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3340 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003341 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3342 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3343#endif
3344}
3345
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003346static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003347 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003348{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003349 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003350 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3351 "notification for %s:\n",
3352 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003353 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003354}
3355
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003356static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003357{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003358 struct iwl_priv *priv =
3359 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003360 struct sk_buff *beacon;
3361
3362 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003363 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003364
3365 if (!beacon) {
3366 IWL_ERROR("update beacon failed\n");
3367 return;
3368 }
3369
3370 mutex_lock(&priv->mutex);
3371 /* new beacon skb is allocated every time; dispose previous.*/
3372 if (priv->ibss_beacon)
3373 dev_kfree_skb(priv->ibss_beacon);
3374
3375 priv->ibss_beacon = beacon;
3376 mutex_unlock(&priv->mutex);
3377
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003378 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003379}
3380
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003381static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003382 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003383{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003384#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003385 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3386 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3387 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003388
3389 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3390 "tsf %d %d rate %d\n",
3391 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3392 beacon->beacon_notify_hdr.failure_frame,
3393 le32_to_cpu(beacon->ibss_mgr_status),
3394 le32_to_cpu(beacon->high_tsf),
3395 le32_to_cpu(beacon->low_tsf), rate);
3396#endif
3397
3398 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3399 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3400 queue_work(priv->workqueue, &priv->beacon_update);
3401}
3402
3403/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003404static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003405 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003406{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003407#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003408 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3409 struct iwl4965_scanreq_notification *notif =
3410 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003411
3412 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3413#endif
3414}
3415
3416/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003417static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003418 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003419{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003420 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3421 struct iwl4965_scanstart_notification *notif =
3422 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003423 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3424 IWL_DEBUG_SCAN("Scan start: "
3425 "%d [802.11%s] "
3426 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3427 notif->channel,
3428 notif->band ? "bg" : "a",
3429 notif->tsf_high,
3430 notif->tsf_low, notif->status, notif->beacon_timer);
3431}
3432
3433/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003434static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003435 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003436{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003437 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3438 struct iwl4965_scanresults_notification *notif =
3439 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003440
3441 IWL_DEBUG_SCAN("Scan ch.res: "
3442 "%d [802.11%s] "
3443 "(TSF: 0x%08X:%08X) - %d "
3444 "elapsed=%lu usec (%dms since last)\n",
3445 notif->channel,
3446 notif->band ? "bg" : "a",
3447 le32_to_cpu(notif->tsf_high),
3448 le32_to_cpu(notif->tsf_low),
3449 le32_to_cpu(notif->statistics[0]),
3450 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3451 jiffies_to_msecs(elapsed_jiffies
3452 (priv->last_scan_jiffies, jiffies)));
3453
3454 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003455 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003456}
3457
3458/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003459static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003460 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003461{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003462 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3463 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003464
3465 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3466 scan_notif->scanned_channels,
3467 scan_notif->tsf_low,
3468 scan_notif->tsf_high, scan_notif->status);
3469
3470 /* The HW is no longer scanning */
3471 clear_bit(STATUS_SCAN_HW, &priv->status);
3472
3473 /* The scan completion notification came in, so kill that timer... */
3474 cancel_delayed_work(&priv->scan_check);
3475
3476 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3477 (priv->scan_bands == 2) ? "2.4" : "5.2",
3478 jiffies_to_msecs(elapsed_jiffies
3479 (priv->scan_pass_start, jiffies)));
3480
3481 /* Remove this scanned band from the list
3482 * of pending bands to scan */
3483 priv->scan_bands--;
3484
3485 /* If a request to abort was given, or the scan did not succeed
3486 * then we reset the scan state machine and terminate,
3487 * re-queuing another scan if one has been requested */
3488 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3489 IWL_DEBUG_INFO("Aborted scan completed.\n");
3490 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3491 } else {
3492 /* If there are more bands on this scan pass reschedule */
3493 if (priv->scan_bands > 0)
3494 goto reschedule;
3495 }
3496
3497 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003498 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003499 IWL_DEBUG_INFO("Setting scan to off\n");
3500
3501 clear_bit(STATUS_SCANNING, &priv->status);
3502
3503 IWL_DEBUG_INFO("Scan took %dms\n",
3504 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3505
3506 queue_work(priv->workqueue, &priv->scan_completed);
3507
3508 return;
3509
3510reschedule:
3511 priv->scan_pass_start = jiffies;
3512 queue_work(priv->workqueue, &priv->request_scan);
3513}
3514
3515/* Handle notification from uCode that card's power state is changing
3516 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003517static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003518 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003519{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003520 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003521 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3522 unsigned long status = priv->status;
3523
3524 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3525 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3526 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3527
3528 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3529 RF_CARD_DISABLED)) {
3530
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003531 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003532 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3533
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003534 if (!iwl_grab_nic_access(priv)) {
3535 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003536 priv, HBUS_TARG_MBX_C,
3537 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3538
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003539 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003540 }
3541
3542 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003543 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003544 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003545 if (!iwl_grab_nic_access(priv)) {
3546 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003547 priv, HBUS_TARG_MBX_C,
3548 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3549
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003550 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003551 }
3552 }
3553
3554 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003555 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003556 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003557 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3558 if (!iwl_grab_nic_access(priv))
3559 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003560 }
3561 }
3562
3563 if (flags & HW_CARD_DISABLED)
3564 set_bit(STATUS_RF_KILL_HW, &priv->status);
3565 else
3566 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3567
3568
3569 if (flags & SW_CARD_DISABLED)
3570 set_bit(STATUS_RF_KILL_SW, &priv->status);
3571 else
3572 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3573
3574 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003575 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003576
3577 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3578 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3579 (test_bit(STATUS_RF_KILL_SW, &status) !=
3580 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3581 queue_work(priv->workqueue, &priv->rf_kill);
3582 else
3583 wake_up_interruptible(&priv->wait_command_queue);
3584}
3585
3586/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003587 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003588 *
3589 * Setup the RX handlers for each of the reply types sent from the uCode
3590 * to the host.
3591 *
3592 * This function chains into the hardware specific files for them to setup
3593 * any hardware specific handlers as well.
3594 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003595static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003596{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003597 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3598 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3599 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3600 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003601 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003602 iwl4965_rx_spectrum_measure_notif;
3603 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003604 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003605 iwl4965_rx_pm_debug_statistics_notif;
3606 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003607
Ben Cahill9fbab512007-11-29 11:09:47 +08003608 /*
3609 * The same handler is used for both the REPLY to a discrete
3610 * statistics request from the host as well as for the periodic
3611 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003612 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003613 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3614 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003615
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003616 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3617 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003618 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003619 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003620 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003621 iwl4965_rx_scan_complete_notif;
3622 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3623 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003624
Ben Cahill9fbab512007-11-29 11:09:47 +08003625 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003626 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003627}
3628
3629/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003630 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003631 * @rxb: Rx buffer to reclaim
3632 *
3633 * If an Rx buffer has an async callback associated with it the callback
3634 * will be executed. The attached skb (if present) will only be freed
3635 * if the callback returns 1
3636 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003637static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003638 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 = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003641 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3642 int txq_id = SEQ_TO_QUEUE(sequence);
3643 int index = SEQ_TO_INDEX(sequence);
3644 int huge = sequence & SEQ_HUGE_FRAME;
3645 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003646 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003647
3648 /* If a Tx command is being handled and it isn't in the actual
3649 * command queue then there a command routing bug has been introduced
3650 * in the queue management code. */
3651 if (txq_id != IWL_CMD_QUEUE_NUM)
3652 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3653 txq_id, pkt->hdr.cmd);
3654 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3655
3656 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3657 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3658
3659 /* Input error checking is done when commands are added to queue. */
3660 if (cmd->meta.flags & CMD_WANT_SKB) {
3661 cmd->meta.source->u.skb = rxb->skb;
3662 rxb->skb = NULL;
3663 } else if (cmd->meta.u.callback &&
3664 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3665 rxb->skb = NULL;
3666
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003667 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003668
3669 if (!(cmd->meta.flags & CMD_ASYNC)) {
3670 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3671 wake_up_interruptible(&priv->wait_command_queue);
3672 }
3673}
3674
3675/************************** RX-FUNCTIONS ****************************/
3676/*
3677 * Rx theory of operation
3678 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003679 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3680 * each of which point to Receive Buffers to be filled by 4965. These get
3681 * used not only for Rx frames, but for any command response or notification
3682 * from the 4965. The driver and 4965 manage the Rx buffers by means
3683 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003684 *
3685 * Rx Queue Indexes
3686 * The host/firmware share two index registers for managing the Rx buffers.
3687 *
3688 * The READ index maps to the first position that the firmware may be writing
3689 * to -- the driver can read up to (but not including) this position and get
3690 * good data.
3691 * The READ index is managed by the firmware once the card is enabled.
3692 *
3693 * The WRITE index maps to the last position the driver has read from -- the
3694 * position preceding WRITE is the last slot the firmware can place a packet.
3695 *
3696 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3697 * WRITE = READ.
3698 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003699 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003700 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3701 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003702 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003703 * and fire the RX interrupt. The driver can then query the READ index and
3704 * process as many packets as possible, moving the WRITE index forward as it
3705 * resets the Rx queue buffers with new memory.
3706 *
3707 * The management in the driver is as follows:
3708 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3709 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003710 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003711 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003712 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3713 * 'processed' and 'read' driver indexes as well)
3714 * + A received packet is processed and handed to the kernel network stack,
3715 * detached from the iwl->rxq. The driver 'processed' index is updated.
3716 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3717 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3718 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3719 * were enough free buffers and RX_STALLED is set it is cleared.
3720 *
3721 *
3722 * Driver sequence:
3723 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003724 * iwl4965_rx_queue_alloc() Allocates rx_free
3725 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003726 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003727 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003728 * queue, updates firmware pointers, and updates
3729 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003730 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003731 *
3732 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003733 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003734 * READ INDEX, detaching the SKB from the pool.
3735 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003736 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003737 * slots.
3738 * ...
3739 *
3740 */
3741
3742/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003743 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003744 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003745static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003746{
3747 int s = q->read - q->write;
3748 if (s <= 0)
3749 s += RX_QUEUE_SIZE;
3750 /* keep some buffer to not confuse full and empty queue */
3751 s -= 2;
3752 if (s < 0)
3753 s = 0;
3754 return s;
3755}
3756
3757/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003758 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003759 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003760int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003761{
3762 u32 reg = 0;
3763 int rc = 0;
3764 unsigned long flags;
3765
3766 spin_lock_irqsave(&q->lock, flags);
3767
3768 if (q->need_update == 0)
3769 goto exit_unlock;
3770
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003771 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003772 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003773 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003774
3775 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003776 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003777 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3778 goto exit_unlock;
3779 }
3780
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003781 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003782 if (rc)
3783 goto exit_unlock;
3784
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003785 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003786 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003787 q->write & ~0x7);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003788 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003789
3790 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003791 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003792 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003793 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003794
3795
3796 q->need_update = 0;
3797
3798 exit_unlock:
3799 spin_unlock_irqrestore(&q->lock, flags);
3800 return rc;
3801}
3802
3803/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003804 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003805 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003806static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003807 dma_addr_t dma_addr)
3808{
3809 return cpu_to_le32((u32)(dma_addr >> 8));
3810}
3811
3812
3813/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003814 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003815 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003816 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003817 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003818 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003819 *
3820 * This moves the 'write' index forward to catch up with 'processed', and
3821 * also updates the memory address in the firmware to reference the new
3822 * target buffer.
3823 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003824static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003825{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003826 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003827 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003828 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003829 unsigned long flags;
3830 int write, rc;
3831
3832 spin_lock_irqsave(&rxq->lock, flags);
3833 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003834 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003835 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003836 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003837 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003838 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003839
3840 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003841 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003842 rxq->queue[rxq->write] = rxb;
3843 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3844 rxq->free_count--;
3845 }
3846 spin_unlock_irqrestore(&rxq->lock, flags);
3847 /* If the pre-allocated buffer pool is dropping low, schedule to
3848 * refill it */
3849 if (rxq->free_count <= RX_LOW_WATERMARK)
3850 queue_work(priv->workqueue, &priv->rx_replenish);
3851
3852
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003853 /* If we've added more space for the firmware to place data, tell it.
3854 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003855 if ((write != (rxq->write & ~0x7))
3856 || (abs(rxq->write - rxq->read) > 7)) {
3857 spin_lock_irqsave(&rxq->lock, flags);
3858 rxq->need_update = 1;
3859 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003860 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003861 if (rc)
3862 return rc;
3863 }
3864
3865 return 0;
3866}
3867
3868/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003869 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003870 *
3871 * When moving to rx_free an SKB is allocated for the slot.
3872 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003873 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003874 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003875 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003876static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003877{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003878 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003879 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003880 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003881 unsigned long flags;
3882 spin_lock_irqsave(&rxq->lock, flags);
3883 while (!list_empty(&rxq->rx_used)) {
3884 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003885 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003886
3887 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003888 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003889 alloc_skb(priv->hw_setting.rx_buf_size,
3890 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003891 if (!rxb->skb) {
3892 if (net_ratelimit())
3893 printk(KERN_CRIT DRV_NAME
3894 ": Can not allocate SKB buffers\n");
3895 /* We don't reschedule replenish work here -- we will
3896 * call the restock method and if it still needs
3897 * more buffers it will schedule replenish */
3898 break;
3899 }
3900 priv->alloc_rxb_skb++;
3901 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003902
3903 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003904 rxb->dma_addr =
3905 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003906 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003907 list_add_tail(&rxb->list, &rxq->rx_free);
3908 rxq->free_count++;
3909 }
3910 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003911}
3912
3913/*
3914 * this should be called while priv->lock is locked
3915*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003916static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003917{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003918 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003919
3920 iwl4965_rx_allocate(priv);
3921 iwl4965_rx_queue_restock(priv);
3922}
3923
3924
3925void iwl4965_rx_replenish(void *data)
3926{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003927 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003928 unsigned long flags;
3929
3930 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003931
3932 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003933 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003934 spin_unlock_irqrestore(&priv->lock, flags);
3935}
3936
3937/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003938 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003939 * This free routine walks the list of POOL entries and if SKB is set to
3940 * non NULL it is unmapped and freed
3941 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003942static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003943{
3944 int i;
3945 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3946 if (rxq->pool[i].skb != NULL) {
3947 pci_unmap_single(priv->pci_dev,
3948 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003949 priv->hw_setting.rx_buf_size,
3950 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003951 dev_kfree_skb(rxq->pool[i].skb);
3952 }
3953 }
3954
3955 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3956 rxq->dma_addr);
3957 rxq->bd = NULL;
3958}
3959
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003960int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003961{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003962 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003963 struct pci_dev *dev = priv->pci_dev;
3964 int i;
3965
3966 spin_lock_init(&rxq->lock);
3967 INIT_LIST_HEAD(&rxq->rx_free);
3968 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003969
3970 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003971 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3972 if (!rxq->bd)
3973 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003974
Zhu Yib481de92007-09-25 17:54:57 -07003975 /* Fill the rx_used queue with _all_ of the Rx buffers */
3976 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3977 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003978
Zhu Yib481de92007-09-25 17:54:57 -07003979 /* Set us so that we have processed and used all buffers, but have
3980 * not restocked the Rx queue with fresh buffers */
3981 rxq->read = rxq->write = 0;
3982 rxq->free_count = 0;
3983 rxq->need_update = 0;
3984 return 0;
3985}
3986
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003987void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003988{
3989 unsigned long flags;
3990 int i;
3991 spin_lock_irqsave(&rxq->lock, flags);
3992 INIT_LIST_HEAD(&rxq->rx_free);
3993 INIT_LIST_HEAD(&rxq->rx_used);
3994 /* Fill the rx_used queue with _all_ of the Rx buffers */
3995 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3996 /* In the reset function, these buffers may have been allocated
3997 * to an SKB, so we need to unmap and free potential storage */
3998 if (rxq->pool[i].skb != NULL) {
3999 pci_unmap_single(priv->pci_dev,
4000 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004001 priv->hw_setting.rx_buf_size,
4002 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004003 priv->alloc_rxb_skb--;
4004 dev_kfree_skb(rxq->pool[i].skb);
4005 rxq->pool[i].skb = NULL;
4006 }
4007 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4008 }
4009
4010 /* Set us so that we have processed and used all buffers, but have
4011 * not restocked the Rx queue with fresh buffers */
4012 rxq->read = rxq->write = 0;
4013 rxq->free_count = 0;
4014 spin_unlock_irqrestore(&rxq->lock, flags);
4015}
4016
4017/* Convert linear signal-to-noise ratio into dB */
4018static u8 ratio2dB[100] = {
4019/* 0 1 2 3 4 5 6 7 8 9 */
4020 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4021 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4022 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4023 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4024 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4025 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4026 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4027 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4028 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4029 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4030};
4031
4032/* Calculates a relative dB value from a ratio of linear
4033 * (i.e. not dB) signal levels.
4034 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004035int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004036{
Adrian Bunkc899a572007-10-14 19:51:15 +02004037 /* 1000:1 or higher just report as 60 dB */
4038 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004039 return 60;
4040
Adrian Bunkc899a572007-10-14 19:51:15 +02004041 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004042 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004043 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004044 return (20 + (int)ratio2dB[sig_ratio/10]);
4045
4046 /* We shouldn't see this */
4047 if (sig_ratio < 1)
4048 return 0;
4049
4050 /* Use table for ratios 1:1 - 99:1 */
4051 return (int)ratio2dB[sig_ratio];
4052}
4053
4054#define PERFECT_RSSI (-20) /* dBm */
4055#define WORST_RSSI (-95) /* dBm */
4056#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4057
4058/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4059 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4060 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004061int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004062{
4063 int sig_qual;
4064 int degradation = PERFECT_RSSI - rssi_dbm;
4065
4066 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4067 * as indicator; formula is (signal dbm - noise dbm).
4068 * SNR at or above 40 is a great signal (100%).
4069 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4070 * Weakest usable signal is usually 10 - 15 dB SNR. */
4071 if (noise_dbm) {
4072 if (rssi_dbm - noise_dbm >= 40)
4073 return 100;
4074 else if (rssi_dbm < noise_dbm)
4075 return 0;
4076 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4077
4078 /* Else use just the signal level.
4079 * This formula is a least squares fit of data points collected and
4080 * compared with a reference system that had a percentage (%) display
4081 * for signal quality. */
4082 } else
4083 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4084 (15 * RSSI_RANGE + 62 * degradation)) /
4085 (RSSI_RANGE * RSSI_RANGE);
4086
4087 if (sig_qual > 100)
4088 sig_qual = 100;
4089 else if (sig_qual < 1)
4090 sig_qual = 0;
4091
4092 return sig_qual;
4093}
4094
4095/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004096 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004097 *
4098 * Uses the priv->rx_handlers callback function array to invoke
4099 * the appropriate handlers, including command responses,
4100 * frame-received notifications, and other notifications.
4101 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004102static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004103{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004104 struct iwl4965_rx_mem_buffer *rxb;
4105 struct iwl4965_rx_packet *pkt;
4106 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004107 u32 r, i;
4108 int reclaim;
4109 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004110 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004111 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004112
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004113 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4114 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004115 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004116 i = rxq->read;
4117
4118 /* Rx interrupt, but nothing sent from uCode */
4119 if (i == r)
4120 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4121
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004122 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4123 fill_rx = 1;
4124
Zhu Yib481de92007-09-25 17:54:57 -07004125 while (i != r) {
4126 rxb = rxq->queue[i];
4127
Ben Cahill9fbab512007-11-29 11:09:47 +08004128 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004129 * then a bug has been introduced in the queue refilling
4130 * routines -- catch it here */
4131 BUG_ON(rxb == NULL);
4132
4133 rxq->queue[i] = NULL;
4134
4135 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004136 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004137 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004138 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004139
4140 /* Reclaim a command buffer only if this packet is a response
4141 * to a (driver-originated) command.
4142 * If the packet (e.g. Rx frame) originated from uCode,
4143 * there is no command buffer to reclaim.
4144 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4145 * but apparently a few don't get set; catch them here. */
4146 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4147 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07004148 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004149 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004150 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4151 (pkt->hdr.cmd != REPLY_TX);
4152
4153 /* Based on type of command response or notification,
4154 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004155 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004156 if (priv->rx_handlers[pkt->hdr.cmd]) {
4157 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4158 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4159 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4160 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4161 } else {
4162 /* No handling needed */
4163 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4164 "r %d i %d No handler needed for %s, 0x%02x\n",
4165 r, i, get_cmd_string(pkt->hdr.cmd),
4166 pkt->hdr.cmd);
4167 }
4168
4169 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004170 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07004171 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004172 * as we reclaim the driver command queue */
4173 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004174 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004175 else
4176 IWL_WARNING("Claim null rxb?\n");
4177 }
4178
4179 /* For now we just don't re-use anything. We can tweak this
4180 * later to try and re-use notification packets and SKBs that
4181 * fail to Rx correctly */
4182 if (rxb->skb != NULL) {
4183 priv->alloc_rxb_skb--;
4184 dev_kfree_skb_any(rxb->skb);
4185 rxb->skb = NULL;
4186 }
4187
4188 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004189 priv->hw_setting.rx_buf_size,
4190 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004191 spin_lock_irqsave(&rxq->lock, flags);
4192 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4193 spin_unlock_irqrestore(&rxq->lock, flags);
4194 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004195 /* If there are a lot of unused frames,
4196 * restock the Rx queue so ucode wont assert. */
4197 if (fill_rx) {
4198 count++;
4199 if (count >= 8) {
4200 priv->rxq.read = i;
4201 __iwl4965_rx_replenish(priv);
4202 count = 0;
4203 }
4204 }
Zhu Yib481de92007-09-25 17:54:57 -07004205 }
4206
4207 /* Backtrack one entry */
4208 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004209 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004210}
4211
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004212/**
4213 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4214 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004215static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004216 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004217{
4218 u32 reg = 0;
4219 int rc = 0;
4220 int txq_id = txq->q.id;
4221
4222 if (txq->need_update == 0)
4223 return rc;
4224
4225 /* if we're trying to save power */
4226 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4227 /* wake up nic if it's powered down ...
4228 * uCode will wake up, and interrupt us again, so next
4229 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004230 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004231
4232 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4233 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004234 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004235 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4236 return rc;
4237 }
4238
4239 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004240 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004241 if (rc)
4242 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004243 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004244 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004245 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004246
4247 /* else not in power-save mode, uCode will never sleep when we're
4248 * trying to tx (during RFKILL, we're not trying to tx). */
4249 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004250 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004251 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004252
4253 txq->need_update = 0;
4254
4255 return rc;
4256}
4257
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004258#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004259static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004260{
Joe Perches0795af52007-10-03 17:59:30 -07004261 DECLARE_MAC_BUF(mac);
4262
Zhu Yib481de92007-09-25 17:54:57 -07004263 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004264 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004265 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4266 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4267 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4268 le32_to_cpu(rxon->filter_flags));
4269 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4270 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4271 rxon->ofdm_basic_rates);
4272 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004273 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4274 print_mac(mac, rxon->node_addr));
4275 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4276 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004277 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4278}
4279#endif
4280
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004281static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004282{
4283 IWL_DEBUG_ISR("Enabling interrupts\n");
4284 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004285 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004286}
4287
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004288/* call this function to flush any scheduled tasklet */
4289static inline void iwl_synchronize_irq(struct iwl_priv *priv)
4290{
4291 /* wait to make sure we flush pedding tasklet*/
4292 synchronize_irq(priv->pci_dev->irq);
4293 tasklet_kill(&priv->irq_tasklet);
4294}
4295
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004296static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004297{
4298 clear_bit(STATUS_INT_ENABLED, &priv->status);
4299
4300 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004301 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004302
4303 /* acknowledge/clear/reset any interrupts still pending
4304 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004305 iwl_write32(priv, CSR_INT, 0xffffffff);
4306 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004307 IWL_DEBUG_ISR("Disabled interrupts\n");
4308}
4309
4310static const char *desc_lookup(int i)
4311{
4312 switch (i) {
4313 case 1:
4314 return "FAIL";
4315 case 2:
4316 return "BAD_PARAM";
4317 case 3:
4318 return "BAD_CHECKSUM";
4319 case 4:
4320 return "NMI_INTERRUPT";
4321 case 5:
4322 return "SYSASSERT";
4323 case 6:
4324 return "FATAL_ERROR";
4325 }
4326
4327 return "UNKNOWN";
4328}
4329
4330#define ERROR_START_OFFSET (1 * sizeof(u32))
4331#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4332
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004333static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004334{
4335 u32 data2, line;
4336 u32 desc, time, count, base, data1;
4337 u32 blink1, blink2, ilink1, ilink2;
4338 int rc;
4339
4340 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4341
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004342 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004343 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4344 return;
4345 }
4346
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004347 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004348 if (rc) {
4349 IWL_WARNING("Can not read from adapter at this time.\n");
4350 return;
4351 }
4352
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004353 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004354
4355 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4356 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004357 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004358 }
4359
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004360 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
4361 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
4362 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
4363 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
4364 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
4365 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
4366 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
4367 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
4368 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004369
4370 IWL_ERROR("Desc Time "
4371 "data1 data2 line\n");
4372 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4373 desc_lookup(desc), desc, time, data1, data2, line);
4374 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4375 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4376 ilink1, ilink2);
4377
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004378 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004379}
4380
4381#define EVENT_START_OFFSET (4 * sizeof(u32))
4382
4383/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004384 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004385 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004386 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004387 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004388static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004389 u32 num_events, u32 mode)
4390{
4391 u32 i;
4392 u32 base; /* SRAM byte address of event log header */
4393 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4394 u32 ptr; /* SRAM byte address of log data */
4395 u32 ev, time, data; /* event log data */
4396
4397 if (num_events == 0)
4398 return;
4399
4400 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4401
4402 if (mode == 0)
4403 event_size = 2 * sizeof(u32);
4404 else
4405 event_size = 3 * sizeof(u32);
4406
4407 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4408
4409 /* "time" is actually "data" for mode 0 (no timestamp).
4410 * place event id # at far right for easier visual parsing. */
4411 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004412 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004413 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004414 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004415 ptr += sizeof(u32);
4416 if (mode == 0)
4417 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4418 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004419 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004420 ptr += sizeof(u32);
4421 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4422 }
4423 }
4424}
4425
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004426static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004427{
4428 int rc;
4429 u32 base; /* SRAM byte address of event log header */
4430 u32 capacity; /* event log capacity in # entries */
4431 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4432 u32 num_wraps; /* # times uCode wrapped to top of log */
4433 u32 next_entry; /* index of next entry to be written by uCode */
4434 u32 size; /* # entries that we'll print */
4435
4436 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004437 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004438 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4439 return;
4440 }
4441
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004442 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004443 if (rc) {
4444 IWL_WARNING("Can not read from adapter at this time.\n");
4445 return;
4446 }
4447
4448 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004449 capacity = iwl_read_targ_mem(priv, base);
4450 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4451 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4452 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004453
4454 size = num_wraps ? capacity : next_entry;
4455
4456 /* bail out if nothing in log */
4457 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004458 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004459 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004460 return;
4461 }
4462
Zhu Yi583fab32007-09-27 11:27:30 +08004463 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004464 size, num_wraps);
4465
4466 /* if uCode has wrapped back to top of log, start at the oldest entry,
4467 * i.e the next one that uCode would fill. */
4468 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004469 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004470 capacity - next_entry, mode);
4471
4472 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004473 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004474
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004475 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004476}
4477
4478/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004479 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004480 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004481static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004482{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004483 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004484 set_bit(STATUS_FW_ERROR, &priv->status);
4485
4486 /* Cancel currently queued command. */
4487 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4488
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004489#ifdef CONFIG_IWLWIFI_DEBUG
4490 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004491 iwl4965_dump_nic_error_log(priv);
4492 iwl4965_dump_nic_event_log(priv);
4493 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004494 }
4495#endif
4496
4497 wake_up_interruptible(&priv->wait_command_queue);
4498
4499 /* Keep the restart process from trying to send host
4500 * commands by clearing the INIT status bit */
4501 clear_bit(STATUS_READY, &priv->status);
4502
4503 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4504 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4505 "Restarting adapter due to uCode error.\n");
4506
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004507 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004508 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4509 sizeof(priv->recovery_rxon));
4510 priv->error_recovering = 1;
4511 }
4512 queue_work(priv->workqueue, &priv->restart);
4513 }
4514}
4515
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004516static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004517{
4518 unsigned long flags;
4519
4520 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4521 sizeof(priv->staging_rxon));
4522 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004523 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004525 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004526
4527 spin_lock_irqsave(&priv->lock, flags);
4528 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4529 priv->error_recovering = 0;
4530 spin_unlock_irqrestore(&priv->lock, flags);
4531}
4532
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004533static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004534{
4535 u32 inta, handled = 0;
4536 u32 inta_fh;
4537 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004538#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004539 u32 inta_mask;
4540#endif
4541
4542 spin_lock_irqsave(&priv->lock, flags);
4543
4544 /* Ack/clear/reset pending uCode interrupts.
4545 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4546 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004547 inta = iwl_read32(priv, CSR_INT);
4548 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004549
4550 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4551 * Any new interrupts that happen after this, either while we're
4552 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004553 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4554 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004555
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004556#ifdef CONFIG_IWLWIFI_DEBUG
4557 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004558 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004559 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004560 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4561 inta, inta_mask, inta_fh);
4562 }
4563#endif
4564
4565 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4566 * atomic, make sure that inta covers all the interrupts that
4567 * we've discovered, even if FH interrupt came in just after
4568 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004569 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004570 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004571 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004572 inta |= CSR_INT_BIT_FH_TX;
4573
4574 /* Now service all interrupt bits discovered above. */
4575 if (inta & CSR_INT_BIT_HW_ERR) {
4576 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4577
4578 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004579 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004580
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004581 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004582
4583 handled |= CSR_INT_BIT_HW_ERR;
4584
4585 spin_unlock_irqrestore(&priv->lock, flags);
4586
4587 return;
4588 }
4589
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004590#ifdef CONFIG_IWLWIFI_DEBUG
4591 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004592 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004593 if (inta & CSR_INT_BIT_SCD)
4594 IWL_DEBUG_ISR("Scheduler finished to transmit "
4595 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004596
4597 /* Alive notification via Rx interrupt will do the real work */
4598 if (inta & CSR_INT_BIT_ALIVE)
4599 IWL_DEBUG_ISR("Alive interrupt\n");
4600 }
4601#endif
4602 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004603 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004604
Ben Cahill9fbab512007-11-29 11:09:47 +08004605 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004606 if (inta & CSR_INT_BIT_RF_KILL) {
4607 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004608 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004609 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4610 hw_rf_kill = 1;
4611
4612 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4613 "RF_KILL bit toggled to %s.\n",
4614 hw_rf_kill ? "disable radio":"enable radio");
4615
4616 /* Queue restart only if RF_KILL switch was set to "kill"
4617 * when we loaded driver, and is now set to "enable".
4618 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004619 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004620 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4621 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004622 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004623 }
Zhu Yib481de92007-09-25 17:54:57 -07004624
4625 handled |= CSR_INT_BIT_RF_KILL;
4626 }
4627
Ben Cahill9fbab512007-11-29 11:09:47 +08004628 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004629 if (inta & CSR_INT_BIT_CT_KILL) {
4630 IWL_ERROR("Microcode CT kill error detected.\n");
4631 handled |= CSR_INT_BIT_CT_KILL;
4632 }
4633
4634 /* Error detected by uCode */
4635 if (inta & CSR_INT_BIT_SW_ERR) {
4636 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4637 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004638 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004639 handled |= CSR_INT_BIT_SW_ERR;
4640 }
4641
4642 /* uCode wakes up after power-down sleep */
4643 if (inta & CSR_INT_BIT_WAKEUP) {
4644 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004645 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4646 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4647 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4648 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4649 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4650 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4651 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004652
4653 handled |= CSR_INT_BIT_WAKEUP;
4654 }
4655
4656 /* All uCode command responses, including Tx command responses,
4657 * Rx "responses" (frame-received notification), and other
4658 * notifications from uCode come through here*/
4659 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004660 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004661 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4662 }
4663
4664 if (inta & CSR_INT_BIT_FH_TX) {
4665 IWL_DEBUG_ISR("Tx interrupt\n");
4666 handled |= CSR_INT_BIT_FH_TX;
4667 }
4668
4669 if (inta & ~handled)
4670 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4671
4672 if (inta & ~CSR_INI_SET_MASK) {
4673 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4674 inta & ~CSR_INI_SET_MASK);
4675 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4676 }
4677
4678 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004679 /* only Re-enable if diabled by irq */
4680 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4681 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004682
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004683#ifdef CONFIG_IWLWIFI_DEBUG
4684 if (iwl_debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004685 inta = iwl_read32(priv, CSR_INT);
4686 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4687 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004688 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4689 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4690 }
4691#endif
4692 spin_unlock_irqrestore(&priv->lock, flags);
4693}
4694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004695static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004696{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004697 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004698 u32 inta, inta_mask;
4699 u32 inta_fh;
4700 if (!priv)
4701 return IRQ_NONE;
4702
4703 spin_lock(&priv->lock);
4704
4705 /* Disable (but don't clear!) interrupts here to avoid
4706 * back-to-back ISRs and sporadic interrupts from our NIC.
4707 * If we have something to service, the tasklet will re-enable ints.
4708 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004709 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4710 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004711
4712 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004713 inta = iwl_read32(priv, CSR_INT);
4714 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004715
4716 /* Ignore interrupt if there's nothing in NIC to service.
4717 * This may be due to IRQ shared with another device,
4718 * or due to sporadic interrupts thrown from our NIC. */
4719 if (!inta && !inta_fh) {
4720 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4721 goto none;
4722 }
4723
4724 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004725 /* Hardware disappeared. It might have already raised
4726 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004727 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004728 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004729 }
4730
4731 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4732 inta, inta_mask, inta_fh);
4733
Joonwoo Park25c03d82008-01-23 10:15:20 -08004734 inta &= ~CSR_INT_BIT_SCD;
4735
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004736 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004737 if (likely(inta || inta_fh))
4738 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004739
Oliver Neukum66fbb542007-11-15 09:31:10 +08004740 unplugged:
4741 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004742 return IRQ_HANDLED;
4743
4744 none:
4745 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004746 /* only Re-enable if diabled by irq */
4747 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4748 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004749 spin_unlock(&priv->lock);
4750 return IRQ_NONE;
4751}
4752
Zhu Yib481de92007-09-25 17:54:57 -07004753/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4754 * sending probe req. This should be set long enough to hear probe responses
4755 * from more than one AP. */
4756#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4757#define IWL_ACTIVE_DWELL_TIME_52 (10)
4758
4759/* For faster active scanning, scan will move to the next channel if fewer than
4760 * PLCP_QUIET_THRESH packets are heard on this channel within
4761 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4762 * time if it's a quiet channel (nothing responded to our probe, and there's
4763 * no other traffic).
4764 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4765#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4766#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4767
4768/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4769 * Must be set longer than active dwell time.
4770 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4771#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4772#define IWL_PASSIVE_DWELL_TIME_52 (10)
4773#define IWL_PASSIVE_DWELL_BASE (100)
4774#define IWL_CHANNEL_TUNE_TIME 5
4775
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004776static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004777 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004778{
Johannes Berg8318d782008-01-24 19:38:38 +01004779 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004780 return IWL_ACTIVE_DWELL_TIME_52;
4781 else
4782 return IWL_ACTIVE_DWELL_TIME_24;
4783}
4784
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004785static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004786 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004787{
Johannes Berg8318d782008-01-24 19:38:38 +01004788 u16 active = iwl4965_get_active_dwell_time(priv, band);
4789 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004790 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4791 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004793 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004794 /* If we're associated, we clamp the maximum passive
4795 * dwell time to be 98% of the beacon interval (minus
4796 * 2 * channel tune time) */
4797 passive = priv->beacon_int;
4798 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4799 passive = IWL_PASSIVE_DWELL_BASE;
4800 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4801 }
4802
4803 if (passive <= active)
4804 passive = active + 1;
4805
4806 return passive;
4807}
4808
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004809static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004810 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004811 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004812 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004813{
4814 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004815 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004816 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004817 u16 passive_dwell = 0;
4818 u16 active_dwell = 0;
4819 int added, i;
4820
Johannes Berg8318d782008-01-24 19:38:38 +01004821 sband = iwl4965_get_hw_mode(priv, band);
4822 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004823 return 0;
4824
Johannes Berg8318d782008-01-24 19:38:38 +01004825 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004826
Johannes Berg8318d782008-01-24 19:38:38 +01004827 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4828 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004829
Johannes Berg8318d782008-01-24 19:38:38 +01004830 for (i = 0, added = 0; i < sband->n_channels; i++) {
4831 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07004832 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004833 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004834 IWL_DEBUG_SCAN
4835 ("Skipping current channel %d\n",
4836 le16_to_cpu(priv->active_rxon.channel));
4837 continue;
4838 }
4839 } else if (priv->only_active_channel)
4840 continue;
4841
Johannes Berg8318d782008-01-24 19:38:38 +01004842 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004843
Assaf Krauss8622e702008-03-21 13:53:43 -07004844 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004845 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004846 if (!is_channel_valid(ch_info)) {
4847 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4848 scan_ch->channel);
4849 continue;
4850 }
4851
4852 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004853 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004854 scan_ch->type = 0; /* passive */
4855 else
4856 scan_ch->type = 1; /* active */
4857
4858 if (scan_ch->type & 1)
4859 scan_ch->type |= (direct_mask << 1);
4860
4861 if (is_channel_narrow(ch_info))
4862 scan_ch->type |= (1 << 7);
4863
4864 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4865 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4866
Ben Cahill9fbab512007-11-29 11:09:47 +08004867 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004868 scan_ch->tpc.dsp_atten = 110;
4869 /* scan_pwr_info->tpc.dsp_atten; */
4870
4871 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004872 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004873 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4874 else {
4875 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4876 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004877 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004878 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004879 */
4880 }
4881
4882 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4883 scan_ch->channel,
4884 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4885 (scan_ch->type & 1) ?
4886 active_dwell : passive_dwell);
4887
4888 scan_ch++;
4889 added++;
4890 }
4891
4892 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4893 return added;
4894}
4895
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004896static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004897 struct ieee80211_rate *rates)
4898{
4899 int i;
4900
4901 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004902 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
4903 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4904 rates[i].hw_value_short = i;
4905 rates[i].flags = 0;
4906 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004907 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004908 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004909 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07004910 rates[i].flags |=
4911 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
4912 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004913 }
Zhu Yib481de92007-09-25 17:54:57 -07004914 }
Zhu Yib481de92007-09-25 17:54:57 -07004915}
4916
4917/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004918 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004919 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004920int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004921{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004922 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004923 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004924 struct ieee80211_channel *channels;
4925 struct ieee80211_channel *geo_ch;
4926 struct ieee80211_rate *rates;
4927 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004928
Johannes Berg8318d782008-01-24 19:38:38 +01004929 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4930 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004931 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4932 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4933 return 0;
4934 }
4935
Zhu Yib481de92007-09-25 17:54:57 -07004936 channels = kzalloc(sizeof(struct ieee80211_channel) *
4937 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004938 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004939 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004940
Tomas Winkler8211ef72008-03-02 01:36:04 +02004941 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004942 GFP_KERNEL);
4943 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004944 kfree(channels);
4945 return -ENOMEM;
4946 }
4947
Zhu Yib481de92007-09-25 17:54:57 -07004948 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004949 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004950 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02004951 /* just OFDM */
4952 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4953 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01004954
Assaf Krauss1ea87392008-03-18 14:57:50 -07004955 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004956
Tomas Winkler8211ef72008-03-02 01:36:04 +02004957 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4958 sband->channels = channels;
4959 /* OFDM & CCK */
4960 sband->bitrates = rates;
4961 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004962
Assaf Krauss1ea87392008-03-18 14:57:50 -07004963 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004964
Zhu Yib481de92007-09-25 17:54:57 -07004965 priv->ieee_channels = channels;
4966 priv->ieee_rates = rates;
4967
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004968 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004969
Tomas Winkler8211ef72008-03-02 01:36:04 +02004970 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004971 ch = &priv->channel_info[i];
4972
Tomas Winkler8211ef72008-03-02 01:36:04 +02004973 /* FIXME: might be removed if scan is OK */
4974 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004975 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004976
Tomas Winkler8211ef72008-03-02 01:36:04 +02004977 if (is_channel_a_band(ch))
4978 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4979 else
4980 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004981
Tomas Winkler8211ef72008-03-02 01:36:04 +02004982 geo_ch = &sband->channels[sband->n_channels++];
4983
4984 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004985 geo_ch->max_power = ch->max_power_avg;
4986 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004987 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004988
4989 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004990 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4991 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004992
Johannes Berg8318d782008-01-24 19:38:38 +01004993 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4994 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004995
4996 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004997 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004998
4999 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5000 priv->max_channel_txpower_limit =
5001 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005002 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01005003 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005004 }
5005
5006 /* Save flags for reg domain usage */
5007 geo_ch->orig_flags = geo_ch->flags;
5008
5009 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
5010 ch->channel, geo_ch->center_freq,
5011 is_channel_a_band(ch) ? "5.2" : "2.4",
5012 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
5013 "restricted" : "valid",
5014 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07005015 }
5016
Tomas Winkler82b9a122008-03-04 18:09:30 -08005017 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
5018 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07005019 printk(KERN_INFO DRV_NAME
5020 ": Incorrectly detected BG card as ABG. Please send "
5021 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5022 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08005023 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07005024 }
5025
5026 printk(KERN_INFO DRV_NAME
5027 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005028 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5029 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005030
John W. Linvillee0e0a672008-03-25 15:58:40 -04005031 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
5032 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5033 &priv->bands[IEEE80211_BAND_2GHZ];
5034 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
5035 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5036 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005037
Zhu Yib481de92007-09-25 17:54:57 -07005038 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5039
5040 return 0;
5041}
5042
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005043/*
5044 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5045 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005046void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005047{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005048 kfree(priv->ieee_channels);
5049 kfree(priv->ieee_rates);
5050 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5051}
5052
Zhu Yib481de92007-09-25 17:54:57 -07005053/******************************************************************************
5054 *
5055 * uCode download functions
5056 *
5057 ******************************************************************************/
5058
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005059static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005060{
Tomas Winkler98c92212008-01-14 17:46:20 -08005061 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5062 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5063 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5064 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5065 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5066 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005067}
5068
5069/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005070 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005071 * looking at all data.
5072 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005073static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005074 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005075{
5076 u32 val;
5077 u32 save_len = len;
5078 int rc = 0;
5079 u32 errcnt;
5080
5081 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5082
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005083 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005084 if (rc)
5085 return rc;
5086
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005087 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005088
5089 errcnt = 0;
5090 for (; len > 0; len -= sizeof(u32), image++) {
5091 /* read data comes through single port, auto-incr addr */
5092 /* NOTE: Use the debugless read so we don't flood kernel log
5093 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005094 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005095 if (val != le32_to_cpu(*image)) {
5096 IWL_ERROR("uCode INST section is invalid at "
5097 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5098 save_len - len, val, le32_to_cpu(*image));
5099 rc = -EIO;
5100 errcnt++;
5101 if (errcnt >= 20)
5102 break;
5103 }
5104 }
5105
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005106 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005107
5108 if (!errcnt)
5109 IWL_DEBUG_INFO
5110 ("ucode image in INSTRUCTION memory is good\n");
5111
5112 return rc;
5113}
5114
5115
5116/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005117 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005118 * using sample data 100 bytes apart. If these sample points are good,
5119 * it's a pretty good bet that everything between them is good, too.
5120 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005121static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005122{
5123 u32 val;
5124 int rc = 0;
5125 u32 errcnt = 0;
5126 u32 i;
5127
5128 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5129
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005130 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005131 if (rc)
5132 return rc;
5133
5134 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5135 /* read data comes through single port, auto-incr addr */
5136 /* NOTE: Use the debugless read so we don't flood kernel log
5137 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005138 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005139 i + RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005140 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005141 if (val != le32_to_cpu(*image)) {
5142#if 0 /* Enable this if you want to see details */
5143 IWL_ERROR("uCode INST section is invalid at "
5144 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5145 i, val, *image);
5146#endif
5147 rc = -EIO;
5148 errcnt++;
5149 if (errcnt >= 3)
5150 break;
5151 }
5152 }
5153
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005154 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005155
5156 return rc;
5157}
5158
5159
5160/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005161 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005162 * and verify its contents
5163 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005164static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005165{
5166 __le32 *image;
5167 u32 len;
5168 int rc = 0;
5169
5170 /* Try bootstrap */
5171 image = (__le32 *)priv->ucode_boot.v_addr;
5172 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005173 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005174 if (rc == 0) {
5175 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5176 return 0;
5177 }
5178
5179 /* Try initialize */
5180 image = (__le32 *)priv->ucode_init.v_addr;
5181 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005182 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005183 if (rc == 0) {
5184 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5185 return 0;
5186 }
5187
5188 /* Try runtime/protocol */
5189 image = (__le32 *)priv->ucode_code.v_addr;
5190 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005191 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005192 if (rc == 0) {
5193 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5194 return 0;
5195 }
5196
5197 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5198
Ben Cahill9fbab512007-11-29 11:09:47 +08005199 /* Since nothing seems to match, show first several data entries in
5200 * instruction SRAM, so maybe visual inspection will give a clue.
5201 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005202 image = (__le32 *)priv->ucode_boot.v_addr;
5203 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005204 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005205
5206 return rc;
5207}
5208
5209
5210/* check contents of special bootstrap uCode SRAM */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005211static int iwl4965_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005212{
5213 __le32 *image = priv->ucode_boot.v_addr;
5214 u32 len = priv->ucode_boot.len;
5215 u32 reg;
5216 u32 val;
5217
5218 IWL_DEBUG_INFO("Begin verify bsm\n");
5219
5220 /* verify BSM SRAM contents */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005221 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005222 for (reg = BSM_SRAM_LOWER_BOUND;
5223 reg < BSM_SRAM_LOWER_BOUND + len;
5224 reg += sizeof(u32), image ++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005225 val = iwl_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005226 if (val != le32_to_cpu(*image)) {
5227 IWL_ERROR("BSM uCode verification failed at "
5228 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5229 BSM_SRAM_LOWER_BOUND,
5230 reg - BSM_SRAM_LOWER_BOUND, len,
5231 val, le32_to_cpu(*image));
5232 return -EIO;
5233 }
5234 }
5235
5236 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5237
5238 return 0;
5239}
5240
5241/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005242 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005243 *
5244 * BSM operation:
5245 *
5246 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5247 * in special SRAM that does not power down during RFKILL. When powering back
5248 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5249 * the bootstrap program into the on-board processor, and starts it.
5250 *
5251 * The bootstrap program loads (via DMA) instructions and data for a new
5252 * program from host DRAM locations indicated by the host driver in the
5253 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5254 * automatically.
5255 *
5256 * When initializing the NIC, the host driver points the BSM to the
5257 * "initialize" uCode image. This uCode sets up some internal data, then
5258 * notifies host via "initialize alive" that it is complete.
5259 *
5260 * The host then replaces the BSM_DRAM_* pointer values to point to the
5261 * normal runtime uCode instructions and a backup uCode data cache buffer
5262 * (filled initially with starting data values for the on-board processor),
5263 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5264 * which begins normal operation.
5265 *
5266 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5267 * the backup data cache in DRAM before SRAM is powered down.
5268 *
5269 * When powering back up, the BSM loads the bootstrap program. This reloads
5270 * the runtime uCode instructions and the backup data cache into SRAM,
5271 * and re-launches the runtime uCode from where it left off.
5272 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005273static int iwl4965_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005274{
5275 __le32 *image = priv->ucode_boot.v_addr;
5276 u32 len = priv->ucode_boot.len;
5277 dma_addr_t pinst;
5278 dma_addr_t pdata;
5279 u32 inst_len;
5280 u32 data_len;
5281 int rc;
5282 int i;
5283 u32 done;
5284 u32 reg_offset;
5285
5286 IWL_DEBUG_INFO("Begin load bsm\n");
5287
5288 /* make sure bootstrap program is no larger than BSM's SRAM size */
5289 if (len > IWL_MAX_BSM_SIZE)
5290 return -EINVAL;
5291
5292 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005293 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005294 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005295 * after the "initialize" uCode has run, to point to
5296 * runtime/protocol instructions and backup data cache. */
5297 pinst = priv->ucode_init.p_addr >> 4;
5298 pdata = priv->ucode_init_data.p_addr >> 4;
5299 inst_len = priv->ucode_init.len;
5300 data_len = priv->ucode_init_data.len;
5301
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005302 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005303 if (rc)
5304 return rc;
5305
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005306 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5307 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5308 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5309 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005310
5311 /* Fill BSM memory with bootstrap instructions */
5312 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5313 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5314 reg_offset += sizeof(u32), image++)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005315 _iwl_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005316 le32_to_cpu(*image));
5317
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005318 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005319 if (rc) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005320 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005321 return rc;
5322 }
5323
5324 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005325 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5326 iwl_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005327 RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005328 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005329
5330 /* Load bootstrap code into instruction SRAM now,
5331 * to prepare to load "initialize" uCode */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005332 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005333 BSM_WR_CTRL_REG_BIT_START);
5334
5335 /* Wait for load of bootstrap uCode to finish */
5336 for (i = 0; i < 100; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005337 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005338 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5339 break;
5340 udelay(10);
5341 }
5342 if (i < 100)
5343 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5344 else {
5345 IWL_ERROR("BSM write did not complete!\n");
5346 return -EIO;
5347 }
5348
5349 /* Enable future boot loads whenever power management unit triggers it
5350 * (e.g. when powering back up after power-save shutdown) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005351 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005352 BSM_WR_CTRL_REG_BIT_START_EN);
5353
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005354 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005355
5356 return 0;
5357}
5358
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005359static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005360{
5361 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005362 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005363}
5364
Tomas Winkler90e759d2007-11-29 11:09:41 +08005365
Zhu Yib481de92007-09-25 17:54:57 -07005366/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005367 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005368 *
5369 * Copy into buffers for card to fetch via bus-mastering
5370 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005371static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005372{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005373 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005374 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005375 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005376 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005377 u8 *src;
5378 size_t len;
5379 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5380
5381 /* Ask kernel firmware_class module to get the boot firmware off disk.
5382 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005383 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5384 if (ret < 0) {
5385 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5386 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005387 goto error;
5388 }
5389
5390 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5391 name, ucode_raw->size);
5392
5393 /* Make sure that we got at least our header! */
5394 if (ucode_raw->size < sizeof(*ucode)) {
5395 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005396 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005397 goto err_release;
5398 }
5399
5400 /* Data from ucode file: header followed by uCode images */
5401 ucode = (void *)ucode_raw->data;
5402
5403 ver = le32_to_cpu(ucode->ver);
5404 inst_size = le32_to_cpu(ucode->inst_size);
5405 data_size = le32_to_cpu(ucode->data_size);
5406 init_size = le32_to_cpu(ucode->init_size);
5407 init_data_size = le32_to_cpu(ucode->init_data_size);
5408 boot_size = le32_to_cpu(ucode->boot_size);
5409
5410 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5411 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5412 inst_size);
5413 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5414 data_size);
5415 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5416 init_size);
5417 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5418 init_data_size);
5419 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5420 boot_size);
5421
5422 /* Verify size of file vs. image size info in file's header */
5423 if (ucode_raw->size < sizeof(*ucode) +
5424 inst_size + data_size + init_size +
5425 init_data_size + boot_size) {
5426
5427 IWL_DEBUG_INFO("uCode file size %d too small\n",
5428 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005429 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005430 goto err_release;
5431 }
5432
5433 /* Verify that uCode images will fit in card's SRAM */
5434 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005435 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5436 inst_size);
5437 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005438 goto err_release;
5439 }
5440
5441 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005442 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5443 data_size);
5444 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005445 goto err_release;
5446 }
5447 if (init_size > IWL_MAX_INST_SIZE) {
5448 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005449 ("uCode init instr len %d too large to fit in\n",
5450 init_size);
5451 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005452 goto err_release;
5453 }
5454 if (init_data_size > IWL_MAX_DATA_SIZE) {
5455 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005456 ("uCode init data len %d too large to fit in\n",
5457 init_data_size);
5458 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005459 goto err_release;
5460 }
5461 if (boot_size > IWL_MAX_BSM_SIZE) {
5462 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005463 ("uCode boot instr len %d too large to fit in\n",
5464 boot_size);
5465 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005466 goto err_release;
5467 }
5468
5469 /* Allocate ucode buffers for card's bus-master loading ... */
5470
5471 /* Runtime instructions and 2 copies of data:
5472 * 1) unmodified from disk
5473 * 2) backup cache for save/restore during power-downs */
5474 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005475 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005476
5477 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005478 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005479
5480 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005481 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005482
5483 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005484 if (init_size && init_data_size) {
5485 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005486 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07005487
Tomas Winkler90e759d2007-11-29 11:09:41 +08005488 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005489 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005490
5491 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5492 goto err_pci_alloc;
5493 }
Zhu Yib481de92007-09-25 17:54:57 -07005494
5495 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005496 if (boot_size) {
5497 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005498 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005499
Tomas Winkler90e759d2007-11-29 11:09:41 +08005500 if (!priv->ucode_boot.v_addr)
5501 goto err_pci_alloc;
5502 }
Zhu Yib481de92007-09-25 17:54:57 -07005503
5504 /* Copy images into buffers for card's bus-master reads ... */
5505
5506 /* Runtime instructions (first block of data in file) */
5507 src = &ucode->data[0];
5508 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005509 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005510 memcpy(priv->ucode_code.v_addr, src, len);
5511 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5512 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5513
5514 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005515 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005516 src = &ucode->data[inst_size];
5517 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005518 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005519 memcpy(priv->ucode_data.v_addr, src, len);
5520 memcpy(priv->ucode_data_backup.v_addr, src, len);
5521
5522 /* Initialization instructions (3rd block) */
5523 if (init_size) {
5524 src = &ucode->data[inst_size + data_size];
5525 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005526 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5527 len);
Zhu Yib481de92007-09-25 17:54:57 -07005528 memcpy(priv->ucode_init.v_addr, src, len);
5529 }
5530
5531 /* Initialization data (4th block) */
5532 if (init_data_size) {
5533 src = &ucode->data[inst_size + data_size + init_size];
5534 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005535 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
5536 len);
Zhu Yib481de92007-09-25 17:54:57 -07005537 memcpy(priv->ucode_init_data.v_addr, src, len);
5538 }
5539
5540 /* Bootstrap instructions (5th block) */
5541 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5542 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005543 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005544 memcpy(priv->ucode_boot.v_addr, src, len);
5545
5546 /* We have our copies now, allow OS release its copies */
5547 release_firmware(ucode_raw);
5548 return 0;
5549
5550 err_pci_alloc:
5551 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005552 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005553 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005554
5555 err_release:
5556 release_firmware(ucode_raw);
5557
5558 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005559 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005560}
5561
5562
5563/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005564 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005565 *
5566 * Tell initialization uCode where to find runtime uCode.
5567 *
5568 * BSM registers initially contain pointers to initialization uCode.
5569 * We need to replace them to load runtime uCode inst and data,
5570 * and to save runtime data when powering down.
5571 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005572static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005573{
5574 dma_addr_t pinst;
5575 dma_addr_t pdata;
5576 int rc = 0;
5577 unsigned long flags;
5578
5579 /* bits 35:4 for 4965 */
5580 pinst = priv->ucode_code.p_addr >> 4;
5581 pdata = priv->ucode_data_backup.p_addr >> 4;
5582
5583 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005584 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005585 if (rc) {
5586 spin_unlock_irqrestore(&priv->lock, flags);
5587 return rc;
5588 }
5589
5590 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005591 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5592 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5593 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005594 priv->ucode_data.len);
5595
5596 /* Inst bytecount must be last to set up, bit 31 signals uCode
5597 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005598 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005599 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5600
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005601 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005602
5603 spin_unlock_irqrestore(&priv->lock, flags);
5604
5605 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5606
5607 return rc;
5608}
5609
5610/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005611 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005612 *
5613 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5614 *
5615 * The 4965 "initialize" ALIVE reply contains calibration data for:
5616 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5617 * (3945 does not contain this data).
5618 *
5619 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5620*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005621static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005622{
5623 /* Check alive response for "valid" sign from uCode */
5624 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5625 /* We had an error bringing up the hardware, so take it
5626 * all the way back down so we can try again */
5627 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5628 goto restart;
5629 }
5630
5631 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5632 * This is a paranoid check, because we would not have gotten the
5633 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005634 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005635 /* Runtime instruction load was bad;
5636 * take it all the way back down so we can try again */
5637 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5638 goto restart;
5639 }
5640
5641 /* Calculate temperature */
5642 priv->temperature = iwl4965_get_temperature(priv);
5643
5644 /* Send pointers to protocol/runtime uCode image ... init code will
5645 * load and launch runtime uCode, which will send us another "Alive"
5646 * notification. */
5647 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005648 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005649 /* Runtime instruction load won't happen;
5650 * take it all the way back down so we can try again */
5651 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5652 goto restart;
5653 }
5654 return;
5655
5656 restart:
5657 queue_work(priv->workqueue, &priv->restart);
5658}
5659
5660
5661/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005662 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005663 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005664 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005665 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005666static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005667{
5668 int rc = 0;
5669
5670 IWL_DEBUG_INFO("Runtime Alive received.\n");
5671
5672 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5673 /* We had an error bringing up the hardware, so take it
5674 * all the way back down so we can try again */
5675 IWL_DEBUG_INFO("Alive failed.\n");
5676 goto restart;
5677 }
5678
5679 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5680 * This is a paranoid check, because we would not have gotten the
5681 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005682 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005683 /* Runtime instruction load was bad;
5684 * take it all the way back down so we can try again */
5685 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5686 goto restart;
5687 }
5688
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005689 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005690
5691 rc = iwl4965_alive_notify(priv);
5692 if (rc) {
5693 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
5694 rc);
5695 goto restart;
5696 }
5697
Ben Cahill9fbab512007-11-29 11:09:47 +08005698 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005699 set_bit(STATUS_ALIVE, &priv->status);
5700
5701 /* Clear out the uCode error bit if it is set */
5702 clear_bit(STATUS_FW_ERROR, &priv->status);
5703
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005704 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005705 return;
5706
Zhu Yi5a66926a2008-01-14 17:46:18 -08005707 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005708
5709 priv->active_rate = priv->rates_mask;
5710 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5711
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005712 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005713
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005714 if (iwl4965_is_associated(priv)) {
5715 struct iwl4965_rxon_cmd *active_rxon =
5716 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005717
5718 memcpy(&priv->staging_rxon, &priv->active_rxon,
5719 sizeof(priv->staging_rxon));
5720 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5721 } else {
5722 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005723 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005724 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5725 }
5726
Ben Cahill9fbab512007-11-29 11:09:47 +08005727 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005728 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005729
5730 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005731 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005732
5733 /* At this point, the NIC is initialized and operational */
5734 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005735
5736 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005737
Zhu Yib481de92007-09-25 17:54:57 -07005738 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005739 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005740 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005741
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005742 iwl_leds_register(priv);
5743
Zhu Yib481de92007-09-25 17:54:57 -07005744 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005745 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005746
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005747 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07005748 return;
5749
5750 restart:
5751 queue_work(priv->workqueue, &priv->restart);
5752}
5753
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005754static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005755
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005756static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005757{
5758 unsigned long flags;
5759 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5760 struct ieee80211_conf *conf = NULL;
5761
5762 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5763
5764 conf = ieee80211_get_hw_conf(priv->hw);
5765
5766 if (!exit_pending)
5767 set_bit(STATUS_EXIT_PENDING, &priv->status);
5768
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005769 iwl_leds_unregister(priv);
5770
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005771 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
5772
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005773 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005774
5775 /* Unblock any waiting calls */
5776 wake_up_interruptible_all(&priv->wait_command_queue);
5777
Zhu Yib481de92007-09-25 17:54:57 -07005778 /* Wipe out the EXIT_PENDING status bit if we are not actually
5779 * exiting the module */
5780 if (!exit_pending)
5781 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5782
5783 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005784 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005785
5786 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005787 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005788 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005789 spin_unlock_irqrestore(&priv->lock, flags);
5790 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005791
5792 if (priv->mac80211_registered)
5793 ieee80211_stop_queues(priv->hw);
5794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005795 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005796 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005797 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005798 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5799 STATUS_RF_KILL_HW |
5800 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5801 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005802 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5803 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005804 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5805 STATUS_IN_SUSPEND;
5806 goto exit;
5807 }
5808
5809 /* ...otherwise clear out all the status bits but the RF Kill and
5810 * SUSPEND bits and continue taking the NIC down. */
5811 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5812 STATUS_RF_KILL_HW |
5813 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5814 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005815 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5816 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005817 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5818 STATUS_IN_SUSPEND |
5819 test_bit(STATUS_FW_ERROR, &priv->status) <<
5820 STATUS_FW_ERROR;
5821
5822 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005823 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08005824 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005825 spin_unlock_irqrestore(&priv->lock, flags);
5826
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005827 iwl4965_hw_txq_ctx_stop(priv);
5828 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005829
5830 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005831 if (!iwl_grab_nic_access(priv)) {
5832 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005833 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005834 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005835 }
5836 spin_unlock_irqrestore(&priv->lock, flags);
5837
5838 udelay(5);
5839
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005840 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005841 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005842 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005843
5844 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005845 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005846
5847 if (priv->ibss_beacon)
5848 dev_kfree_skb(priv->ibss_beacon);
5849 priv->ibss_beacon = NULL;
5850
5851 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005852 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005853}
5854
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005855static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005856{
5857 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005858 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005859 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005860
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005861 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005862}
5863
5864#define MAX_HW_RESTARTS 5
5865
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005866static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005867{
5868 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07005869
5870 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5871 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5872 return -EIO;
5873 }
5874
5875 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5876 IWL_WARNING("Radio disabled by SW RF kill (module "
5877 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005878 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005879 return -ENODEV;
5880 }
5881
Reinette Chatree903fbd2008-01-30 22:05:15 -08005882 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5883 IWL_ERROR("ucode not available for device bringup\n");
5884 return -EIO;
5885 }
5886
Zhu Yie655b9f2008-01-24 02:19:38 -08005887 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005888 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005889 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5890 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5891 else {
5892 set_bit(STATUS_RF_KILL_HW, &priv->status);
5893 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005894 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005895 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5896 return -ENODEV;
5897 }
Zhu Yib481de92007-09-25 17:54:57 -07005898 }
5899
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005900 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005901 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005902
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005903 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005904 if (rc) {
5905 IWL_ERROR("Unable to int nic\n");
5906 return rc;
5907 }
5908
5909 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005910 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5911 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005912 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5913
5914 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005915 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005916 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005917
5918 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005919 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5920 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005921
5922 /* Copy original ucode data image from disk into backup cache.
5923 * This will be used to initialize the on-board processor's
5924 * data SRAM for a clean start when the runtime program first loads. */
5925 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08005926 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005927
Zhu Yie655b9f2008-01-24 02:19:38 -08005928 /* We return success when we resume from suspend and rf_kill is on. */
5929 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005930 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005931
5932 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5933
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005934 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005935
5936 /* load bootstrap state machine,
5937 * load bootstrap program into processor's memory,
5938 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005939 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005940
5941 if (rc) {
5942 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
5943 continue;
5944 }
5945
5946 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005947 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005948
Zhu Yib481de92007-09-25 17:54:57 -07005949 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5950
5951 return 0;
5952 }
5953
5954 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005955 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005956
5957 /* tried to restart and config the device for as long as our
5958 * patience could withstand */
5959 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5960 return -EIO;
5961}
5962
5963
5964/*****************************************************************************
5965 *
5966 * Workqueue callbacks
5967 *
5968 *****************************************************************************/
5969
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005970static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005971{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005972 struct iwl_priv *priv =
5973 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005974
5975 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5976 return;
5977
5978 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005979 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005980 mutex_unlock(&priv->mutex);
5981}
5982
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005983static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005984{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005985 struct iwl_priv *priv =
5986 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005987
5988 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5989 return;
5990
5991 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005992 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005993 mutex_unlock(&priv->mutex);
5994}
5995
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005996static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005997{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005998 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005999
6000 wake_up_interruptible(&priv->wait_command_queue);
6001
6002 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6003 return;
6004
6005 mutex_lock(&priv->mutex);
6006
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006007 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006008 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6009 "HW and/or SW RF Kill no longer active, restarting "
6010 "device\n");
6011 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6012 queue_work(priv->workqueue, &priv->restart);
6013 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07006014 /* make sure mac80211 stop sending Tx frame */
6015 if (priv->mac80211_registered)
6016 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006017
6018 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6019 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6020 "disabled by SW switch\n");
6021 else
6022 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6023 "Kill switch must be turned off for "
6024 "wireless networking to work.\n");
6025 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07006026 iwl_rfkill_set_hw_state(priv);
6027
Zhu Yib481de92007-09-25 17:54:57 -07006028 mutex_unlock(&priv->mutex);
6029}
6030
6031#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6032
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006033static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006034{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006035 struct iwl_priv *priv =
6036 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006037
6038 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6039 return;
6040
6041 mutex_lock(&priv->mutex);
6042 if (test_bit(STATUS_SCANNING, &priv->status) ||
6043 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6044 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6045 "Scan completion watchdog resetting adapter (%dms)\n",
6046 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006047
Zhu Yib481de92007-09-25 17:54:57 -07006048 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006049 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006050 }
6051 mutex_unlock(&priv->mutex);
6052}
6053
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006054static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006055{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006056 struct iwl_priv *priv =
6057 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07006058 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006059 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006060 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006061 .meta.flags = CMD_SIZE_HUGE,
6062 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006063 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006064 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006065 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006066 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006067 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07006068 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006069
6070 conf = ieee80211_get_hw_conf(priv->hw);
6071
6072 mutex_lock(&priv->mutex);
6073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006074 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006075 IWL_WARNING("request scan called when driver not ready.\n");
6076 goto done;
6077 }
6078
6079 /* Make sure the scan wasn't cancelled before this queued work
6080 * was given the chance to run... */
6081 if (!test_bit(STATUS_SCANNING, &priv->status))
6082 goto done;
6083
6084 /* This should never be called or scheduled if there is currently
6085 * a scan active in the hardware. */
6086 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6087 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6088 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07006089 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07006090 goto done;
6091 }
6092
6093 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6094 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6095 goto done;
6096 }
6097
6098 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6099 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6100 goto done;
6101 }
6102
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006103 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006104 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6105 goto done;
6106 }
6107
6108 if (!test_bit(STATUS_READY, &priv->status)) {
6109 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6110 goto done;
6111 }
6112
6113 if (!priv->scan_bands) {
6114 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6115 goto done;
6116 }
6117
6118 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006119 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006120 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6121 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07006122 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07006123 goto done;
6124 }
6125 }
6126 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006127 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006128
6129 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6130 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6131
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006132 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006133 u16 interval = 0;
6134 u32 extra;
6135 u32 suspend_time = 100;
6136 u32 scan_suspend_time = 100;
6137 unsigned long flags;
6138
6139 IWL_DEBUG_INFO("Scanning while associated...\n");
6140
6141 spin_lock_irqsave(&priv->lock, flags);
6142 interval = priv->beacon_int;
6143 spin_unlock_irqrestore(&priv->lock, flags);
6144
6145 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006146 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006147 if (!interval)
6148 interval = suspend_time;
6149
6150 extra = (suspend_time / interval) << 22;
6151 scan_suspend_time = (extra |
6152 ((suspend_time % interval) * 1024));
6153 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6154 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6155 scan_suspend_time, interval);
6156 }
6157
6158 /* We should add the ability for user to lock to PASSIVE ONLY */
6159 if (priv->one_direct_scan) {
6160 IWL_DEBUG_SCAN
6161 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006162 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006163 priv->direct_ssid_len));
6164 scan->direct_scan[0].id = WLAN_EID_SSID;
6165 scan->direct_scan[0].len = priv->direct_ssid_len;
6166 memcpy(scan->direct_scan[0].ssid,
6167 priv->direct_ssid, priv->direct_ssid_len);
6168 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006169 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006170 scan->direct_scan[0].id = WLAN_EID_SSID;
6171 scan->direct_scan[0].len = priv->essid_len;
6172 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6173 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07006174 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006175 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07006176 }
Zhu Yib481de92007-09-25 17:54:57 -07006177
Zhu Yib481de92007-09-25 17:54:57 -07006178 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6179 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6180 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6181
Zhu Yib481de92007-09-25 17:54:57 -07006182
6183 switch (priv->scan_bands) {
6184 case 2:
6185 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6186 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006187 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006188 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6189
6190 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006191 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006192 break;
6193
6194 case 1:
6195 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006196 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006197 RATE_MCS_ANT_B_MSK);
6198 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006199 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006200 break;
6201
6202 default:
6203 IWL_WARNING("Invalid scan band count\n");
6204 goto done;
6205 }
6206
Tomas Winkler78330fd2008-02-06 02:37:18 +02006207 /* We don't build a direct scan probe request; the uCode will do
6208 * that based on the direct_mask added to each channel entry */
6209 cmd_len = iwl4965_fill_probe_req(priv, band,
6210 (struct ieee80211_mgmt *)scan->data,
6211 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6212
6213 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006214 /* select Rx chains */
6215
6216 /* Force use of chains B and C (0x6) for scan Rx.
6217 * Avoid A (0x1) because of its off-channel reception on A-band.
6218 * MIMO is not used here, but value is required to make uCode happy. */
6219 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6220 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6221 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6222 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6223
6224 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6225 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6226
Reinette Chatre26c0f032008-03-11 16:17:15 -07006227 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07006228 IWL_DEBUG_SCAN
6229 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006230 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07006231 scan->channel_count =
6232 iwl4965_get_channels_for_scan(
6233 priv, band, 1, /* active */
6234 direct_mask,
6235 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6236 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006237 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07006238 scan->channel_count =
6239 iwl4965_get_channels_for_scan(
6240 priv, band, 0, /* passive */
6241 direct_mask,
6242 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6243 }
Zhu Yib481de92007-09-25 17:54:57 -07006244
6245 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006246 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006247 cmd.data = scan;
6248 scan->len = cpu_to_le16(cmd.len);
6249
6250 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07006251 ret = iwl_send_cmd_sync(priv, &cmd);
6252 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006253 goto done;
6254
6255 queue_delayed_work(priv->workqueue, &priv->scan_check,
6256 IWL_SCAN_CHECK_WATCHDOG);
6257
6258 mutex_unlock(&priv->mutex);
6259 return;
6260
6261 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006262 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006263 queue_work(priv->workqueue, &priv->scan_completed);
6264 mutex_unlock(&priv->mutex);
6265}
6266
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006267static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006268{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006269 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006270
6271 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6272 return;
6273
6274 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006275 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006276 mutex_unlock(&priv->mutex);
6277}
6278
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006279static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006280{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006281 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006282
6283 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6284 return;
6285
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006286 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006287 queue_work(priv->workqueue, &priv->up);
6288}
6289
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006290static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006291{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006292 struct iwl_priv *priv =
6293 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006294
6295 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6296 return;
6297
6298 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006299 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006300 mutex_unlock(&priv->mutex);
6301}
6302
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006303#define IWL_DELAY_NEXT_SCAN (HZ*2)
6304
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006305static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006306{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006307 struct iwl_priv *priv = container_of(data, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006308 post_associate.work);
Zhu Yib481de92007-09-25 17:54:57 -07006309 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07006310 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07006311 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006312
6313 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6314 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6315 return;
6316 }
6317
Joe Perches0795af52007-10-03 17:59:30 -07006318 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6319 priv->assoc_id,
6320 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006321
6322
6323 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6324 return;
6325
6326 mutex_lock(&priv->mutex);
6327
Johannes Berg32bfd352007-12-19 01:31:26 +01006328 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006329 mutex_unlock(&priv->mutex);
6330 return;
6331 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006332 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006333
Zhu Yib481de92007-09-25 17:54:57 -07006334 conf = ieee80211_get_hw_conf(priv->hw);
6335
6336 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006337 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006338
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006339 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6340 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006341 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006342 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006343 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006344 IWL_WARNING("REPLY_RXON_TIMING failed - "
6345 "Attempting to continue.\n");
6346
6347 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6348
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006349#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006350 if (priv->current_ht_config.is_ht)
6351 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006352#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006353 iwl4965_set_rxon_chain(priv);
6354 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6355
6356 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6357 priv->assoc_id, priv->beacon_int);
6358
6359 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6360 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6361 else
6362 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6363
6364 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6365 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6366 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6367 else
6368 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6369
6370 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6371 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6372
6373 }
6374
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006375 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006376
6377 switch (priv->iw_mode) {
6378 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006379 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006380 break;
6381
6382 case IEEE80211_IF_TYPE_IBSS:
6383
6384 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006385 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006386
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006387 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6388 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6389 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6390 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006391
6392 break;
6393
6394 default:
6395 IWL_ERROR("%s Should not be called in %d mode\n",
6396 __FUNCTION__, priv->iw_mode);
6397 break;
6398 }
6399
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006400 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006401
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006402#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006403 /* Enable Rx differential gain and sensitivity calibrations */
6404 iwl4965_chain_noise_reset(priv);
6405 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006406#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006407
6408 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6409 priv->assoc_station_added = 1;
6410
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006411 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006412
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006413 /* we have just associated, don't start scan too early */
6414 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006415 mutex_unlock(&priv->mutex);
6416}
6417
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006418static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006419{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006420 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006421
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006422 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006423 return;
6424
6425 mutex_lock(&priv->mutex);
6426
6427 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006428 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006429
6430 mutex_unlock(&priv->mutex);
6431}
6432
Zhu Yi76bb77e2007-11-22 10:53:22 +08006433static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006435static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006436{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006437 struct iwl_priv *priv =
6438 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006439
6440 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6441
6442 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6443 return;
6444
Zhu Yia0646472007-12-20 14:10:01 +08006445 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6446 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006447
Zhu Yib481de92007-09-25 17:54:57 -07006448 ieee80211_scan_completed(priv->hw);
6449
6450 /* Since setting the TXPOWER may have been deferred while
6451 * performing the scan, fire one off */
6452 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006453 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006454 mutex_unlock(&priv->mutex);
6455}
6456
6457/*****************************************************************************
6458 *
6459 * mac80211 entry point functions
6460 *
6461 *****************************************************************************/
6462
Zhu Yi5a66926a2008-01-14 17:46:18 -08006463#define UCODE_READY_TIMEOUT (2 * HZ)
6464
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006465static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006466{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006467 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006468 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006469
6470 IWL_DEBUG_MAC80211("enter\n");
6471
Zhu Yi5a66926a2008-01-14 17:46:18 -08006472 if (pci_enable_device(priv->pci_dev)) {
6473 IWL_ERROR("Fail to pci_enable_device\n");
6474 return -ENODEV;
6475 }
6476 pci_restore_state(priv->pci_dev);
6477 pci_enable_msi(priv->pci_dev);
6478
6479 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6480 DRV_NAME, priv);
6481 if (ret) {
6482 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6483 goto out_disable_msi;
6484 }
6485
Zhu Yib481de92007-09-25 17:54:57 -07006486 /* we should be verifying the device is ready to be opened */
6487 mutex_lock(&priv->mutex);
6488
Zhu Yi5a66926a2008-01-14 17:46:18 -08006489 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
6490 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6491 * ucode filename and max sizes are card-specific. */
6492
6493 if (!priv->ucode_code.len) {
6494 ret = iwl4965_read_ucode(priv);
6495 if (ret) {
6496 IWL_ERROR("Could not read microcode: %d\n", ret);
6497 mutex_unlock(&priv->mutex);
6498 goto out_release_irq;
6499 }
6500 }
6501
Zhu Yie655b9f2008-01-24 02:19:38 -08006502 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006503
Zhu Yib481de92007-09-25 17:54:57 -07006504 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006505
Zhu Yie655b9f2008-01-24 02:19:38 -08006506 if (ret)
6507 goto out_release_irq;
6508
6509 IWL_DEBUG_INFO("Start UP work done.\n");
6510
6511 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6512 return 0;
6513
Zhu Yi5a66926a2008-01-14 17:46:18 -08006514 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6515 * mac80211 will not be run successfully. */
6516 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6517 test_bit(STATUS_READY, &priv->status),
6518 UCODE_READY_TIMEOUT);
6519 if (!ret) {
6520 if (!test_bit(STATUS_READY, &priv->status)) {
6521 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6522 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6523 ret = -ETIMEDOUT;
6524 goto out_release_irq;
6525 }
6526 }
6527
Zhu Yie655b9f2008-01-24 02:19:38 -08006528 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006529 IWL_DEBUG_MAC80211("leave\n");
6530 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006531
6532out_release_irq:
6533 free_irq(priv->pci_dev->irq, priv);
6534out_disable_msi:
6535 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006536 pci_disable_device(priv->pci_dev);
6537 priv->is_open = 0;
6538 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006539 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006540}
6541
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006542static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006543{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006544 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006545
6546 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006547
Zhu Yie655b9f2008-01-24 02:19:38 -08006548 if (!priv->is_open) {
6549 IWL_DEBUG_MAC80211("leave - skip\n");
6550 return;
6551 }
6552
Zhu Yib481de92007-09-25 17:54:57 -07006553 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006554
6555 if (iwl4965_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006556 /* stop mac, cancel any scan request and clear
6557 * RXON_FILTER_ASSOC_MSK BIT
6558 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08006559 mutex_lock(&priv->mutex);
6560 iwl4965_scan_cancel_timeout(priv, 100);
6561 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006562 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006563 }
6564
Zhu Yi5a66926a2008-01-14 17:46:18 -08006565 iwl4965_down(priv);
6566
6567 flush_workqueue(priv->workqueue);
6568 free_irq(priv->pci_dev->irq, priv);
6569 pci_disable_msi(priv->pci_dev);
6570 pci_save_state(priv->pci_dev);
6571 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006572
Zhu Yib481de92007-09-25 17:54:57 -07006573 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006574}
6575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006576static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006577 struct ieee80211_tx_control *ctl)
6578{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006579 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006580
6581 IWL_DEBUG_MAC80211("enter\n");
6582
6583 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6584 IWL_DEBUG_MAC80211("leave - monitor\n");
6585 return -1;
6586 }
6587
6588 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006589 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006590
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006591 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006592 dev_kfree_skb_any(skb);
6593
6594 IWL_DEBUG_MAC80211("leave\n");
6595 return 0;
6596}
6597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006598static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006599 struct ieee80211_if_init_conf *conf)
6600{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006601 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006602 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006603 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006604
Johannes Berg32bfd352007-12-19 01:31:26 +01006605 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006606
Johannes Berg32bfd352007-12-19 01:31:26 +01006607 if (priv->vif) {
6608 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006609 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006610 }
6611
6612 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006613 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006614
6615 spin_unlock_irqrestore(&priv->lock, flags);
6616
6617 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006618
6619 if (conf->mac_addr) {
6620 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6621 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6622 }
Zhu Yib481de92007-09-25 17:54:57 -07006623
Zhu Yi5a66926a2008-01-14 17:46:18 -08006624 if (iwl4965_is_ready(priv))
6625 iwl4965_set_mode(priv, conf->type);
6626
Zhu Yib481de92007-09-25 17:54:57 -07006627 mutex_unlock(&priv->mutex);
6628
Zhu Yi5a66926a2008-01-14 17:46:18 -08006629 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006630 return 0;
6631}
6632
6633/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006634 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006635 *
6636 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6637 * be set inappropriately and the driver currently sets the hardware up to
6638 * use it whenever needed.
6639 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006640static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006641{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006642 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006643 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006644 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006645 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006646
6647 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006648 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006649
Zhu Yi12342c42007-12-20 11:27:32 +08006650 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6651
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006652 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006653 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006654 ret = -EIO;
6655 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006656 }
6657
Assaf Krauss1ea87392008-03-18 14:57:50 -07006658 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006659 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006660 IWL_DEBUG_MAC80211("leave - scanning\n");
6661 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006662 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006663 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006664 }
6665
6666 spin_lock_irqsave(&priv->lock, flags);
6667
Assaf Krauss8622e702008-03-21 13:53:43 -07006668 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006669 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006670 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006671 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6672 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006673 ret = -EINVAL;
6674 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006675 }
6676
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006677#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006678 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006679 * from any ht related info since 2.4 does not
6680 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006681 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006682#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6683 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6684#endif
6685 )
6686 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006687#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006688
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006689 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006690 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006691
Johannes Berg8318d782008-01-24 19:38:38 +01006692 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006693
6694 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006695 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006696 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006697 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006698
6699 spin_unlock_irqrestore(&priv->lock, flags);
6700
6701#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6702 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006703 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006704 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006705 }
6706#endif
6707
Mohamed Abbasad97edd2008-03-28 16:21:06 -07006708 if (priv->cfg->ops->lib->radio_kill_sw)
6709 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006710
6711 if (!conf->radio_enabled) {
6712 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006713 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006714 }
6715
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006716 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006717 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006718 ret = -EIO;
6719 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006720 }
6721
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006722 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006723
6724 if (memcmp(&priv->active_rxon,
6725 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006726 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006727 else
6728 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6729
6730 IWL_DEBUG_MAC80211("leave\n");
6731
Zhu Yia0646472007-12-20 14:10:01 +08006732out:
6733 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006734 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006735 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006736}
6737
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006738static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006739{
Tomas Winkler857485c2008-03-21 13:53:44 -07006740 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006741
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006742 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006743 return;
6744
6745 /* The following should be done only at AP bring up */
6746 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6747
6748 /* RXON - unassoc (to set timing command) */
6749 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006750 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006751
6752 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006753 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6754 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006755 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006756 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006757 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006758 IWL_WARNING("REPLY_RXON_TIMING failed - "
6759 "Attempting to continue.\n");
6760
6761 iwl4965_set_rxon_chain(priv);
6762
6763 /* FIXME: what should be the assoc_id for AP? */
6764 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6765 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6766 priv->staging_rxon.flags |=
6767 RXON_FLG_SHORT_PREAMBLE_MSK;
6768 else
6769 priv->staging_rxon.flags &=
6770 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6771
6772 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6773 if (priv->assoc_capability &
6774 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6775 priv->staging_rxon.flags |=
6776 RXON_FLG_SHORT_SLOT_MSK;
6777 else
6778 priv->staging_rxon.flags &=
6779 ~RXON_FLG_SHORT_SLOT_MSK;
6780
6781 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6782 priv->staging_rxon.flags &=
6783 ~RXON_FLG_SHORT_SLOT_MSK;
6784 }
6785 /* restore RXON assoc */
6786 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006787 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006788 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006789 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006790 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006791 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006792
6793 /* FIXME - we need to add code here to detect a totally new
6794 * configuration, reset the AP, unassoc, rxon timing, assoc,
6795 * clear sta table, add BCAST sta... */
6796}
6797
Johannes Berg32bfd352007-12-19 01:31:26 +01006798static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6799 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006800 struct ieee80211_if_conf *conf)
6801{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006802 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006803 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006804 unsigned long flags;
6805 int rc;
6806
6807 if (conf == NULL)
6808 return -EIO;
6809
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006810 if (priv->vif != vif) {
6811 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
6812 mutex_unlock(&priv->mutex);
6813 return 0;
6814 }
6815
Zhu Yib481de92007-09-25 17:54:57 -07006816 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6817 (!conf->beacon || !conf->ssid_len)) {
6818 IWL_DEBUG_MAC80211
6819 ("Leaving in AP mode because HostAPD is not ready.\n");
6820 return 0;
6821 }
6822
Zhu Yi5a66926a2008-01-14 17:46:18 -08006823 if (!iwl4965_is_alive(priv))
6824 return -EAGAIN;
6825
Zhu Yib481de92007-09-25 17:54:57 -07006826 mutex_lock(&priv->mutex);
6827
Zhu Yib481de92007-09-25 17:54:57 -07006828 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006829 IWL_DEBUG_MAC80211("bssid: %s\n",
6830 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006831
Johannes Berg4150c572007-09-17 01:29:23 -04006832/*
6833 * very dubious code was here; the probe filtering flag is never set:
6834 *
Zhu Yib481de92007-09-25 17:54:57 -07006835 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6836 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006837 */
Zhu Yib481de92007-09-25 17:54:57 -07006838
6839 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6840 if (!conf->bssid) {
6841 conf->bssid = priv->mac_addr;
6842 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006843 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6844 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006845 }
6846 if (priv->ibss_beacon)
6847 dev_kfree_skb(priv->ibss_beacon);
6848
6849 priv->ibss_beacon = conf->beacon;
6850 }
6851
Mohamed Abbasfde35712007-11-29 11:10:15 +08006852 if (iwl4965_is_rfkill(priv))
6853 goto done;
6854
Zhu Yib481de92007-09-25 17:54:57 -07006855 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6856 !is_multicast_ether_addr(conf->bssid)) {
6857 /* If there is currently a HW scan going on in the background
6858 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006859 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006860 IWL_WARNING("Aborted scan still in progress "
6861 "after 100ms\n");
6862 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6863 mutex_unlock(&priv->mutex);
6864 return -EAGAIN;
6865 }
6866 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6867
6868 /* TODO: Audit driver for usage of these members and see
6869 * if mac80211 deprecates them (priv->bssid looks like it
6870 * shouldn't be there, but I haven't scanned the IBSS code
6871 * to verify) - jpk */
6872 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6873
6874 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006875 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006876 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006877 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006878 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006879 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006880 priv, priv->active_rxon.bssid_addr, 1);
6881 }
6882
6883 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006884 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006885 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006886 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006887 }
6888
Mohamed Abbasfde35712007-11-29 11:10:15 +08006889 done:
Zhu Yib481de92007-09-25 17:54:57 -07006890 spin_lock_irqsave(&priv->lock, flags);
6891 if (!conf->ssid_len)
6892 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6893 else
6894 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6895
6896 priv->essid_len = conf->ssid_len;
6897 spin_unlock_irqrestore(&priv->lock, flags);
6898
6899 IWL_DEBUG_MAC80211("leave\n");
6900 mutex_unlock(&priv->mutex);
6901
6902 return 0;
6903}
6904
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006905static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006906 unsigned int changed_flags,
6907 unsigned int *total_flags,
6908 int mc_count, struct dev_addr_list *mc_list)
6909{
6910 /*
6911 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006912 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006913 */
6914 *total_flags = 0;
6915}
6916
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006917static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006918 struct ieee80211_if_init_conf *conf)
6919{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006920 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006921
6922 IWL_DEBUG_MAC80211("enter\n");
6923
6924 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006925
Mohamed Abbasfde35712007-11-29 11:10:15 +08006926 if (iwl4965_is_ready_rf(priv)) {
6927 iwl4965_scan_cancel_timeout(priv, 100);
6928 cancel_delayed_work(&priv->post_associate);
6929 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6930 iwl4965_commit_rxon(priv);
6931 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006932 if (priv->vif == conf->vif) {
6933 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006934 memset(priv->bssid, 0, ETH_ALEN);
6935 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6936 priv->essid_len = 0;
6937 }
6938 mutex_unlock(&priv->mutex);
6939
6940 IWL_DEBUG_MAC80211("leave\n");
6941
6942}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006943
6944static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6945 struct ieee80211_vif *vif,
6946 struct ieee80211_bss_conf *bss_conf,
6947 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006948{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006949 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006950
Johannes Berg471b3ef2007-12-28 14:32:58 +01006951 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6952 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006953 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6954 else
6955 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6956 }
6957
Johannes Berg471b3ef2007-12-28 14:32:58 +01006958 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Johannes Berg8318d782008-01-24 19:38:38 +01006959 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006960 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6961 else
6962 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6963 }
6964
Johannes Berg471b3ef2007-12-28 14:32:58 +01006965 if (changes & BSS_CHANGED_ASSOC) {
6966 /*
6967 * TODO:
6968 * do stuff instead of sniffing assoc resp
6969 */
6970 }
6971
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006972 if (iwl4965_is_associated(priv))
6973 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02006974}
Zhu Yib481de92007-09-25 17:54:57 -07006975
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006976static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006977{
6978 int rc = 0;
6979 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006980 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006981
6982 IWL_DEBUG_MAC80211("enter\n");
6983
mabbas052c4b92007-10-25 17:15:43 +08006984 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006985 spin_lock_irqsave(&priv->lock, flags);
6986
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006987 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006988 rc = -EIO;
6989 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6990 goto out_unlock;
6991 }
6992
6993 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
6994 rc = -EIO;
6995 IWL_ERROR("ERROR: APs don't scan\n");
6996 goto out_unlock;
6997 }
6998
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006999 /* we don't schedule scan within next_scan_jiffies period */
7000 if (priv->next_scan_jiffies &&
7001 time_after(priv->next_scan_jiffies, jiffies)) {
7002 rc = -EAGAIN;
7003 goto out_unlock;
7004 }
Zhu Yib481de92007-09-25 17:54:57 -07007005 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007006 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7007 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007008 rc = -EAGAIN;
7009 goto out_unlock;
7010 }
7011 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007012 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007013 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007014
7015 priv->one_direct_scan = 1;
7016 priv->direct_ssid_len = (u8)
7017 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7018 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007019 } else
7020 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007021
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007022 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007023
7024 IWL_DEBUG_MAC80211("leave\n");
7025
7026out_unlock:
7027 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007028 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007029
7030 return rc;
7031}
7032
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007033static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
7034 struct ieee80211_key_conf *keyconf, const u8 *addr,
7035 u32 iv32, u16 *phase1key)
7036{
7037 struct iwl_priv *priv = hw->priv;
7038 u8 sta_id = IWL_INVALID_STATION;
7039 unsigned long flags;
7040 __le16 key_flags = 0;
7041 int i;
7042 DECLARE_MAC_BUF(mac);
7043
7044 IWL_DEBUG_MAC80211("enter\n");
7045
7046 sta_id = iwl4965_hw_find_station(priv, addr);
7047 if (sta_id == IWL_INVALID_STATION) {
7048 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7049 print_mac(mac, addr));
7050 return;
7051 }
7052
7053 iwl4965_scan_cancel_timeout(priv, 100);
7054
7055 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
7056 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
7057 key_flags &= ~STA_KEY_FLG_INVALID;
7058
7059 if (sta_id == priv->hw_setting.bcast_sta_id)
7060 key_flags |= STA_KEY_MULTICAST_MSK;
7061
7062 spin_lock_irqsave(&priv->sta_lock, flags);
7063
7064 priv->stations[sta_id].sta.key.key_offset =
7065 (sta_id % STA_KEY_MAX_NUM);/* FIXME */
7066 priv->stations[sta_id].sta.key.key_flags = key_flags;
7067 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
7068
7069 for (i = 0; i < 5; i++)
7070 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
7071 cpu_to_le16(phase1key[i]);
7072
7073 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
7074 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
7075
7076 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
7077
7078 spin_unlock_irqrestore(&priv->sta_lock, flags);
7079
7080 IWL_DEBUG_MAC80211("leave\n");
7081}
7082
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007083static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007084 const u8 *local_addr, const u8 *addr,
7085 struct ieee80211_key_conf *key)
7086{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007087 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007088 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007089 int ret = 0;
7090 u8 sta_id = IWL_INVALID_STATION;
7091 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07007092
7093 IWL_DEBUG_MAC80211("enter\n");
7094
Assaf Krauss1ea87392008-03-18 14:57:50 -07007095 if (!priv->cfg->mod_params->hw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007096 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7097 return -EOPNOTSUPP;
7098 }
7099
7100 if (is_zero_ether_addr(addr))
7101 /* only support pairwise keys */
7102 return -EOPNOTSUPP;
7103
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007104 /* FIXME: need to differenciate between static and dynamic key
7105 * in the level of mac80211 */
7106 static_key = !iwl4965_is_associated(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007107
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007108 if (!static_key) {
7109 sta_id = iwl4965_hw_find_station(priv, addr);
7110 if (sta_id == IWL_INVALID_STATION) {
7111 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7112 print_mac(mac, addr));
7113 return -EINVAL;
7114 }
7115 }
Zhu Yib481de92007-09-25 17:54:57 -07007116
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007117 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007118
Zhu Yib481de92007-09-25 17:54:57 -07007119 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007120 case SET_KEY:
7121 if (static_key)
7122 ret = iwl4965_set_static_key(priv, key);
7123 else
7124 ret = iwl4965_set_dynamic_key(priv, key, sta_id);
7125
7126 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007127 break;
7128 case DISABLE_KEY:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007129 if (static_key)
7130 ret = iwl4965_remove_static_key(priv);
7131 else
7132 ret = iwl4965_clear_sta_key_info(priv, sta_id);
7133
7134 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007135 break;
7136 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007137 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07007138 }
7139
7140 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007141
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007142 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007143}
7144
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007145static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007146 const struct ieee80211_tx_queue_params *params)
7147{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007148 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007149 unsigned long flags;
7150 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007151
7152 IWL_DEBUG_MAC80211("enter\n");
7153
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007154 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007155 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7156 return -EIO;
7157 }
7158
7159 if (queue >= AC_NUM) {
7160 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7161 return 0;
7162 }
7163
Zhu Yib481de92007-09-25 17:54:57 -07007164 if (!priv->qos_data.qos_enable) {
7165 priv->qos_data.qos_active = 0;
7166 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7167 return 0;
7168 }
7169 q = AC_NUM - 1 - queue;
7170
7171 spin_lock_irqsave(&priv->lock, flags);
7172
7173 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7174 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7175 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7176 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007177 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007178
7179 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7180 priv->qos_data.qos_active = 1;
7181
7182 spin_unlock_irqrestore(&priv->lock, flags);
7183
7184 mutex_lock(&priv->mutex);
7185 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007186 iwl4965_activate_qos(priv, 1);
7187 else if (priv->assoc_id && iwl4965_is_associated(priv))
7188 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007189
7190 mutex_unlock(&priv->mutex);
7191
Zhu Yib481de92007-09-25 17:54:57 -07007192 IWL_DEBUG_MAC80211("leave\n");
7193 return 0;
7194}
7195
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007196static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007197 struct ieee80211_tx_queue_stats *stats)
7198{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007199 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007200 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007201 struct iwl4965_tx_queue *txq;
7202 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007203 unsigned long flags;
7204
7205 IWL_DEBUG_MAC80211("enter\n");
7206
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007207 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007208 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7209 return -EIO;
7210 }
7211
7212 spin_lock_irqsave(&priv->lock, flags);
7213
7214 for (i = 0; i < AC_NUM; i++) {
7215 txq = &priv->txq[i];
7216 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007217 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007218
7219 stats->data[i].len = q->n_window - avail;
7220 stats->data[i].limit = q->n_window - q->high_mark;
7221 stats->data[i].count = q->n_window;
7222
7223 }
7224 spin_unlock_irqrestore(&priv->lock, flags);
7225
7226 IWL_DEBUG_MAC80211("leave\n");
7227
7228 return 0;
7229}
7230
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007231static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007232 struct ieee80211_low_level_stats *stats)
7233{
7234 IWL_DEBUG_MAC80211("enter\n");
7235 IWL_DEBUG_MAC80211("leave\n");
7236
7237 return 0;
7238}
7239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007240static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007241{
7242 IWL_DEBUG_MAC80211("enter\n");
7243 IWL_DEBUG_MAC80211("leave\n");
7244
7245 return 0;
7246}
7247
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007248static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007249{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007250 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007251 unsigned long flags;
7252
7253 mutex_lock(&priv->mutex);
7254 IWL_DEBUG_MAC80211("enter\n");
7255
7256 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007257#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007258 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007259 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007260 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007261#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007262
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007263 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007264
7265 cancel_delayed_work(&priv->post_associate);
7266
7267 spin_lock_irqsave(&priv->lock, flags);
7268 priv->assoc_id = 0;
7269 priv->assoc_capability = 0;
7270 priv->call_post_assoc_from_beacon = 0;
7271 priv->assoc_station_added = 0;
7272
7273 /* new association get rid of ibss beacon skb */
7274 if (priv->ibss_beacon)
7275 dev_kfree_skb(priv->ibss_beacon);
7276
7277 priv->ibss_beacon = NULL;
7278
7279 priv->beacon_int = priv->hw->conf.beacon_int;
7280 priv->timestamp1 = 0;
7281 priv->timestamp0 = 0;
7282 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7283 priv->beacon_int = 0;
7284
7285 spin_unlock_irqrestore(&priv->lock, flags);
7286
Mohamed Abbasfde35712007-11-29 11:10:15 +08007287 if (!iwl4965_is_ready_rf(priv)) {
7288 IWL_DEBUG_MAC80211("leave - not ready\n");
7289 mutex_unlock(&priv->mutex);
7290 return;
7291 }
7292
mabbas052c4b92007-10-25 17:15:43 +08007293 /* we are restarting association process
7294 * clear RXON_FILTER_ASSOC_MSK bit
7295 */
7296 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007297 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007298 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007299 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007300 }
7301
Zhu Yib481de92007-09-25 17:54:57 -07007302 /* Per mac80211.h: This is only used in IBSS mode... */
7303 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007304
Zhu Yib481de92007-09-25 17:54:57 -07007305 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7306 mutex_unlock(&priv->mutex);
7307 return;
7308 }
7309
Zhu Yib481de92007-09-25 17:54:57 -07007310 priv->only_active_channel = 0;
7311
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007312 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007313
7314 mutex_unlock(&priv->mutex);
7315
7316 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007317}
7318
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007319static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007320 struct ieee80211_tx_control *control)
7321{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007322 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007323 unsigned long flags;
7324
7325 mutex_lock(&priv->mutex);
7326 IWL_DEBUG_MAC80211("enter\n");
7327
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007328 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007329 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7330 mutex_unlock(&priv->mutex);
7331 return -EIO;
7332 }
7333
7334 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7335 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7336 mutex_unlock(&priv->mutex);
7337 return -EIO;
7338 }
7339
7340 spin_lock_irqsave(&priv->lock, flags);
7341
7342 if (priv->ibss_beacon)
7343 dev_kfree_skb(priv->ibss_beacon);
7344
7345 priv->ibss_beacon = skb;
7346
7347 priv->assoc_id = 0;
7348
7349 IWL_DEBUG_MAC80211("leave\n");
7350 spin_unlock_irqrestore(&priv->lock, flags);
7351
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007352 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007353
7354 queue_work(priv->workqueue, &priv->post_associate.work);
7355
7356 mutex_unlock(&priv->mutex);
7357
7358 return 0;
7359}
7360
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007361#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007362
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007363static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007364 struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007365{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007366 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
7367 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
7368 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07007369
7370 IWL_DEBUG_MAC80211("enter: \n");
7371
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007372 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
7373 iwl_conf->is_ht = 0;
7374 return;
Zhu Yib481de92007-09-25 17:54:57 -07007375 }
7376
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007377 iwl_conf->is_ht = 1;
7378 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07007379
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007380 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
7381 iwl_conf->sgf |= 0x1;
7382 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
7383 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07007384
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007385 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
7386 iwl_conf->max_amsdu_size =
7387 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007388
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007389 iwl_conf->supported_chan_width =
7390 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007391 iwl_conf->extension_chan_offset =
7392 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
7393 /* If no above or below channel supplied disable FAT channel */
7394 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
7395 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
7396 iwl_conf->supported_chan_width = 0;
7397
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007398 iwl_conf->tx_mimo_ps_mode =
7399 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
7400 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07007401
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007402 iwl_conf->control_channel = ht_bss_conf->primary_channel;
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007403 iwl_conf->tx_chan_width =
7404 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
7405 iwl_conf->ht_protection =
7406 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
7407 iwl_conf->non_GF_STA_present =
7408 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07007409
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007410 IWL_DEBUG_MAC80211("control channel %d\n",
7411 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007412 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007413}
7414
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007415static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007416 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007417{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007418 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007419
7420 IWL_DEBUG_MAC80211("enter: \n");
7421
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007422 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007423 iwl4965_set_rxon_chain(priv);
7424
7425 if (priv && priv->assoc_id &&
7426 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
7427 unsigned long flags;
7428
7429 spin_lock_irqsave(&priv->lock, flags);
7430 if (priv->beacon_int)
7431 queue_work(priv->workqueue, &priv->post_associate.work);
7432 else
7433 priv->call_post_assoc_from_beacon = 1;
7434 spin_unlock_irqrestore(&priv->lock, flags);
7435 }
7436
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007437 IWL_DEBUG_MAC80211("leave:\n");
7438 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007439}
7440
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007441#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007442
7443/*****************************************************************************
7444 *
7445 * sysfs attributes
7446 *
7447 *****************************************************************************/
7448
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007449#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007450
7451/*
7452 * The following adds a new attribute to the sysfs representation
7453 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7454 * used for controlling the debug level.
7455 *
7456 * See the level definitions in iwl for details.
7457 */
7458
7459static ssize_t show_debug_level(struct device_driver *d, char *buf)
7460{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007461 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007462}
7463static ssize_t store_debug_level(struct device_driver *d,
7464 const char *buf, size_t count)
7465{
7466 char *p = (char *)buf;
7467 u32 val;
7468
7469 val = simple_strtoul(p, &p, 0);
7470 if (p == buf)
7471 printk(KERN_INFO DRV_NAME
7472 ": %s is not in hex or decimal form.\n", buf);
7473 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007474 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007475
7476 return strnlen(buf, count);
7477}
7478
7479static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7480 show_debug_level, store_debug_level);
7481
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007482#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007483
Zhu Yib481de92007-09-25 17:54:57 -07007484
7485static ssize_t show_temperature(struct device *d,
7486 struct device_attribute *attr, char *buf)
7487{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007488 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007489
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007490 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007491 return -EAGAIN;
7492
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007493 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007494}
7495
7496static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7497
7498static ssize_t show_rs_window(struct device *d,
7499 struct device_attribute *attr,
7500 char *buf)
7501{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007502 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007503 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007504}
7505static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7506
7507static ssize_t show_tx_power(struct device *d,
7508 struct device_attribute *attr, char *buf)
7509{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007510 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007511 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7512}
7513
7514static ssize_t store_tx_power(struct device *d,
7515 struct device_attribute *attr,
7516 const char *buf, size_t count)
7517{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007518 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007519 char *p = (char *)buf;
7520 u32 val;
7521
7522 val = simple_strtoul(p, &p, 10);
7523 if (p == buf)
7524 printk(KERN_INFO DRV_NAME
7525 ": %s is not in decimal form.\n", buf);
7526 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007527 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007528
7529 return count;
7530}
7531
7532static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7533
7534static ssize_t show_flags(struct device *d,
7535 struct device_attribute *attr, char *buf)
7536{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007537 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007538
7539 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7540}
7541
7542static ssize_t store_flags(struct device *d,
7543 struct device_attribute *attr,
7544 const char *buf, size_t count)
7545{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007546 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007547 u32 flags = simple_strtoul(buf, NULL, 0);
7548
7549 mutex_lock(&priv->mutex);
7550 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7551 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007552 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007553 IWL_WARNING("Could not cancel scan.\n");
7554 else {
7555 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7556 flags);
7557 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007558 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007559 }
7560 }
7561 mutex_unlock(&priv->mutex);
7562
7563 return count;
7564}
7565
7566static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7567
7568static ssize_t show_filter_flags(struct device *d,
7569 struct device_attribute *attr, char *buf)
7570{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007571 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007572
7573 return sprintf(buf, "0x%04X\n",
7574 le32_to_cpu(priv->active_rxon.filter_flags));
7575}
7576
7577static ssize_t store_filter_flags(struct device *d,
7578 struct device_attribute *attr,
7579 const char *buf, size_t count)
7580{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007581 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007582 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7583
7584 mutex_lock(&priv->mutex);
7585 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7586 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007587 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007588 IWL_WARNING("Could not cancel scan.\n");
7589 else {
7590 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7591 "0x%04X\n", filter_flags);
7592 priv->staging_rxon.filter_flags =
7593 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007594 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007595 }
7596 }
7597 mutex_unlock(&priv->mutex);
7598
7599 return count;
7600}
7601
7602static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7603 store_filter_flags);
7604
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007605#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007606
7607static ssize_t show_measurement(struct device *d,
7608 struct device_attribute *attr, char *buf)
7609{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007610 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007611 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007612 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7613 u8 *data = (u8 *) & measure_report;
7614 unsigned long flags;
7615
7616 spin_lock_irqsave(&priv->lock, flags);
7617 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7618 spin_unlock_irqrestore(&priv->lock, flags);
7619 return 0;
7620 }
7621 memcpy(&measure_report, &priv->measure_report, size);
7622 priv->measurement_status = 0;
7623 spin_unlock_irqrestore(&priv->lock, flags);
7624
7625 while (size && (PAGE_SIZE - len)) {
7626 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7627 PAGE_SIZE - len, 1);
7628 len = strlen(buf);
7629 if (PAGE_SIZE - len)
7630 buf[len++] = '\n';
7631
7632 ofs += 16;
7633 size -= min(size, 16U);
7634 }
7635
7636 return len;
7637}
7638
7639static ssize_t store_measurement(struct device *d,
7640 struct device_attribute *attr,
7641 const char *buf, size_t count)
7642{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007643 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007644 struct ieee80211_measurement_params params = {
7645 .channel = le16_to_cpu(priv->active_rxon.channel),
7646 .start_time = cpu_to_le64(priv->last_tsf),
7647 .duration = cpu_to_le16(1),
7648 };
7649 u8 type = IWL_MEASURE_BASIC;
7650 u8 buffer[32];
7651 u8 channel;
7652
7653 if (count) {
7654 char *p = buffer;
7655 strncpy(buffer, buf, min(sizeof(buffer), count));
7656 channel = simple_strtoul(p, NULL, 0);
7657 if (channel)
7658 params.channel = channel;
7659
7660 p = buffer;
7661 while (*p && *p != ' ')
7662 p++;
7663 if (*p)
7664 type = simple_strtoul(p + 1, NULL, 0);
7665 }
7666
7667 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7668 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007669 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007670
7671 return count;
7672}
7673
7674static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7675 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007676#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007677
7678static ssize_t store_retry_rate(struct device *d,
7679 struct device_attribute *attr,
7680 const char *buf, size_t count)
7681{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007682 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007683
7684 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7685 if (priv->retry_rate <= 0)
7686 priv->retry_rate = 1;
7687
7688 return count;
7689}
7690
7691static ssize_t show_retry_rate(struct device *d,
7692 struct device_attribute *attr, char *buf)
7693{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007694 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007695 return sprintf(buf, "%d", priv->retry_rate);
7696}
7697
7698static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7699 store_retry_rate);
7700
7701static ssize_t store_power_level(struct device *d,
7702 struct device_attribute *attr,
7703 const char *buf, size_t count)
7704{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007705 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007706 int rc;
7707 int mode;
7708
7709 mode = simple_strtoul(buf, NULL, 0);
7710 mutex_lock(&priv->mutex);
7711
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007712 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007713 rc = -EAGAIN;
7714 goto out;
7715 }
7716
7717 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7718 mode = IWL_POWER_AC;
7719 else
7720 mode |= IWL_POWER_ENABLED;
7721
7722 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007723 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007724 if (rc) {
7725 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7726 goto out;
7727 }
7728 priv->power_mode = mode;
7729 }
7730
7731 rc = count;
7732
7733 out:
7734 mutex_unlock(&priv->mutex);
7735 return rc;
7736}
7737
7738#define MAX_WX_STRING 80
7739
7740/* Values are in microsecond */
7741static const s32 timeout_duration[] = {
7742 350000,
7743 250000,
7744 75000,
7745 37000,
7746 25000,
7747};
7748static const s32 period_duration[] = {
7749 400000,
7750 700000,
7751 1000000,
7752 1000000,
7753 1000000
7754};
7755
7756static ssize_t show_power_level(struct device *d,
7757 struct device_attribute *attr, char *buf)
7758{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007759 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007760 int level = IWL_POWER_LEVEL(priv->power_mode);
7761 char *p = buf;
7762
7763 p += sprintf(p, "%d ", level);
7764 switch (level) {
7765 case IWL_POWER_MODE_CAM:
7766 case IWL_POWER_AC:
7767 p += sprintf(p, "(AC)");
7768 break;
7769 case IWL_POWER_BATTERY:
7770 p += sprintf(p, "(BATTERY)");
7771 break;
7772 default:
7773 p += sprintf(p,
7774 "(Timeout %dms, Period %dms)",
7775 timeout_duration[level - 1] / 1000,
7776 period_duration[level - 1] / 1000);
7777 }
7778
7779 if (!(priv->power_mode & IWL_POWER_ENABLED))
7780 p += sprintf(p, " OFF\n");
7781 else
7782 p += sprintf(p, " \n");
7783
7784 return (p - buf + 1);
7785
7786}
7787
7788static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7789 store_power_level);
7790
7791static ssize_t show_channels(struct device *d,
7792 struct device_attribute *attr, char *buf)
7793{
Johannes Berg8318d782008-01-24 19:38:38 +01007794 /* all this shit doesn't belong into sysfs anyway */
7795 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007796}
7797
7798static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7799
7800static ssize_t show_statistics(struct device *d,
7801 struct device_attribute *attr, char *buf)
7802{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007803 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007804 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007805 u32 len = 0, ofs = 0;
7806 u8 *data = (u8 *) & priv->statistics;
7807 int rc = 0;
7808
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007809 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007810 return -EAGAIN;
7811
7812 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007813 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007814 mutex_unlock(&priv->mutex);
7815
7816 if (rc) {
7817 len = sprintf(buf,
7818 "Error sending statistics request: 0x%08X\n", rc);
7819 return len;
7820 }
7821
7822 while (size && (PAGE_SIZE - len)) {
7823 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7824 PAGE_SIZE - len, 1);
7825 len = strlen(buf);
7826 if (PAGE_SIZE - len)
7827 buf[len++] = '\n';
7828
7829 ofs += 16;
7830 size -= min(size, 16U);
7831 }
7832
7833 return len;
7834}
7835
7836static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7837
7838static ssize_t show_antenna(struct device *d,
7839 struct device_attribute *attr, char *buf)
7840{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007841 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007842
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007843 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007844 return -EAGAIN;
7845
7846 return sprintf(buf, "%d\n", priv->antenna);
7847}
7848
7849static ssize_t store_antenna(struct device *d,
7850 struct device_attribute *attr,
7851 const char *buf, size_t count)
7852{
7853 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007854 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007855
7856 if (count == 0)
7857 return 0;
7858
7859 if (sscanf(buf, "%1i", &ant) != 1) {
7860 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7861 return count;
7862 }
7863
7864 if ((ant >= 0) && (ant <= 2)) {
7865 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007866 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007867 } else
7868 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7869
7870
7871 return count;
7872}
7873
7874static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7875
7876static ssize_t show_status(struct device *d,
7877 struct device_attribute *attr, char *buf)
7878{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007879 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007880 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007881 return -EAGAIN;
7882 return sprintf(buf, "0x%08x\n", (int)priv->status);
7883}
7884
7885static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7886
7887static ssize_t dump_error_log(struct device *d,
7888 struct device_attribute *attr,
7889 const char *buf, size_t count)
7890{
7891 char *p = (char *)buf;
7892
7893 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007894 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007895
7896 return strnlen(buf, count);
7897}
7898
7899static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7900
7901static ssize_t dump_event_log(struct device *d,
7902 struct device_attribute *attr,
7903 const char *buf, size_t count)
7904{
7905 char *p = (char *)buf;
7906
7907 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007908 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007909
7910 return strnlen(buf, count);
7911}
7912
7913static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7914
7915/*****************************************************************************
7916 *
7917 * driver setup and teardown
7918 *
7919 *****************************************************************************/
7920
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007921static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007922{
7923 priv->workqueue = create_workqueue(DRV_NAME);
7924
7925 init_waitqueue_head(&priv->wait_command_queue);
7926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007927 INIT_WORK(&priv->up, iwl4965_bg_up);
7928 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7929 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7930 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7931 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7932 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7933 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7934 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7935 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7936 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7937 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7938 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007939
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007940 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007941
7942 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007943 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007944}
7945
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007946static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007947{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007948 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007949
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007950 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007951 cancel_delayed_work(&priv->scan_check);
7952 cancel_delayed_work(&priv->alive_start);
7953 cancel_delayed_work(&priv->post_associate);
7954 cancel_work_sync(&priv->beacon_update);
7955}
7956
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007957static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007958 &dev_attr_antenna.attr,
7959 &dev_attr_channels.attr,
7960 &dev_attr_dump_errors.attr,
7961 &dev_attr_dump_events.attr,
7962 &dev_attr_flags.attr,
7963 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007964#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007965 &dev_attr_measurement.attr,
7966#endif
7967 &dev_attr_power_level.attr,
7968 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007969 &dev_attr_rs_window.attr,
7970 &dev_attr_statistics.attr,
7971 &dev_attr_status.attr,
7972 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007973 &dev_attr_tx_power.attr,
7974
7975 NULL
7976};
7977
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007978static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007979 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007980 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007981};
7982
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007983static struct ieee80211_ops iwl4965_hw_ops = {
7984 .tx = iwl4965_mac_tx,
7985 .start = iwl4965_mac_start,
7986 .stop = iwl4965_mac_stop,
7987 .add_interface = iwl4965_mac_add_interface,
7988 .remove_interface = iwl4965_mac_remove_interface,
7989 .config = iwl4965_mac_config,
7990 .config_interface = iwl4965_mac_config_interface,
7991 .configure_filter = iwl4965_configure_filter,
7992 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007993 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007994 .get_stats = iwl4965_mac_get_stats,
7995 .get_tx_stats = iwl4965_mac_get_tx_stats,
7996 .conf_tx = iwl4965_mac_conf_tx,
7997 .get_tsf = iwl4965_mac_get_tsf,
7998 .reset_tsf = iwl4965_mac_reset_tsf,
7999 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01008000 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008001#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008002 .conf_ht = iwl4965_mac_conf_ht,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02008003 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008004#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008005 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008006};
8007
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008008static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008009{
8010 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008011 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008012 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008013 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008014 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08008015 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008016
Assaf Krauss316c30d2008-03-14 10:38:46 -07008017 /************************
8018 * 1. Allocating HW data
8019 ************************/
8020
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008021 /* Disabling hardware scan means that mac80211 will perform scans
8022 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07008023 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008024 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008025 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008026 }
8027
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008028 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
8029 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07008030 err = -ENOMEM;
8031 goto out;
8032 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008033 priv = hw->priv;
8034 /* At this point both hw and priv are allocated. */
8035
Zhu Yib481de92007-09-25 17:54:57 -07008036 SET_IEEE80211_DEV(hw, &pdev->dev);
8037
8038 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08008039 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008040 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008041
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008042#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07008043 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07008044 atomic_set(&priv->restrict_refcnt, 0);
8045#endif
Zhu Yib481de92007-09-25 17:54:57 -07008046
Assaf Krauss316c30d2008-03-14 10:38:46 -07008047 /**************************
8048 * 2. Initializing PCI bus
8049 **************************/
8050 if (pci_enable_device(pdev)) {
8051 err = -ENODEV;
8052 goto out_ieee80211_free_hw;
8053 }
8054
8055 pci_set_master(pdev);
8056
8057 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8058 if (!err)
8059 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8060 if (err) {
8061 printk(KERN_WARNING DRV_NAME
8062 ": No suitable DMA available.\n");
8063 goto out_pci_disable_device;
8064 }
8065
8066 err = pci_request_regions(pdev, DRV_NAME);
8067 if (err)
8068 goto out_pci_disable_device;
8069
8070 pci_set_drvdata(pdev, priv);
8071
8072 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8073 * PCI Tx retries from interfering with C3 CPU state */
8074 pci_write_config_byte(pdev, 0x41, 0x00);
8075
8076 /***********************
8077 * 3. Read REV register
8078 ***********************/
8079 priv->hw_base = pci_iomap(pdev, 0, 0);
8080 if (!priv->hw_base) {
8081 err = -ENODEV;
8082 goto out_pci_release_regions;
8083 }
8084
8085 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8086 (unsigned long long) pci_resource_len(pdev, 0));
8087 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8088
8089 printk(KERN_INFO DRV_NAME
8090 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
8091
8092 /*****************
8093 * 4. Read EEPROM
8094 *****************/
8095 /* nic init */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07008096 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Assaf Krauss316c30d2008-03-14 10:38:46 -07008097 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8098
Tomas Winkler3395f6e2008-03-25 16:33:37 -07008099 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8100 err = iwl_poll_bit(priv, CSR_GP_CNTRL,
Assaf Krauss316c30d2008-03-14 10:38:46 -07008101 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8102 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8103 if (err < 0) {
8104 IWL_DEBUG_INFO("Failed to init the card\n");
8105 goto out_iounmap;
8106 }
8107 /* Read the EEPROM */
8108 err = iwl_eeprom_init(priv);
8109 if (err) {
8110 IWL_ERROR("Unable to init EEPROM\n");
8111 goto out_iounmap;
8112 }
8113 /* MAC Address location in EEPROM same for 3945/4965 */
8114 iwl_eeprom_get_mac(priv, priv->mac_addr);
8115 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8116 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8117
8118 /************************
8119 * 5. Setup HW constants
8120 ************************/
8121 /* Device-specific setup */
8122 if (iwl4965_hw_set_hw_setting(priv)) {
8123 IWL_ERROR("failed to set hw settings\n");
8124 goto out_iounmap;
8125 }
8126
8127 /*******************
8128 * 6. Setup hw/priv
8129 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07008130
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008131 err = iwl_setup(priv);
8132 if (err)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008133 goto out_unset_hw_settings;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008134 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07008135
8136 /**********************************
8137 * 7. Initialize module parameters
8138 **********************************/
8139
8140 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07008141 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07008142 set_bit(STATUS_RF_KILL_SW, &priv->status);
8143 IWL_DEBUG_INFO("Radio disabled.\n");
8144 }
8145
Assaf Krauss1ea87392008-03-18 14:57:50 -07008146 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008147 priv->qos_data.qos_enable = 1;
8148
8149 /********************
8150 * 8. Setup services
8151 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008152 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008153 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008154 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008155
8156 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
8157 if (err) {
8158 IWL_ERROR("failed to create sysfs device attributes\n");
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008159 goto out_unset_hw_settings;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008160 }
8161
8162 err = iwl_dbgfs_register(priv, DRV_NAME);
8163 if (err) {
8164 IWL_ERROR("failed to create debugfs files\n");
8165 goto out_remove_sysfs;
8166 }
8167
8168 iwl4965_setup_deferred_work(priv);
8169 iwl4965_setup_rx_handlers(priv);
8170
8171 /********************
8172 * 9. Conclude
8173 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08008174 pci_save_state(pdev);
8175 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008176
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07008177 /* notify iwlcore to init */
8178 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07008179 return 0;
8180
Assaf Krauss316c30d2008-03-14 10:38:46 -07008181 out_remove_sysfs:
8182 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008183 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008184 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008185 out_iounmap:
8186 pci_iounmap(pdev, priv->hw_base);
8187 out_pci_release_regions:
8188 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008189 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07008190 out_pci_disable_device:
8191 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008192 out_ieee80211_free_hw:
8193 ieee80211_free_hw(priv->hw);
8194 out:
8195 return err;
8196}
8197
Reinette Chatrec83dbf62008-03-21 13:53:41 -07008198static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008199{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008200 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008201 struct list_head *p, *q;
8202 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008203 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07008204
8205 if (!priv)
8206 return;
8207
8208 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8209
Zhu Yib481de92007-09-25 17:54:57 -07008210 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008211
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008212 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008213
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008214 /* make sure we flush any pending irq or
8215 * tasklet for the driver
8216 */
8217 spin_lock_irqsave(&priv->lock, flags);
8218 iwl4965_disable_interrupts(priv);
8219 spin_unlock_irqrestore(&priv->lock, flags);
8220
8221 iwl_synchronize_irq(priv);
8222
Zhu Yib481de92007-09-25 17:54:57 -07008223 /* Free MAC hash list for ADHOC */
8224 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8225 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8226 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008227 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008228 }
8229 }
8230
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07008231 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07008232 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008233 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008234
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008235 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008236
8237 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008238 iwl4965_rx_queue_free(priv, &priv->rxq);
8239 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008240
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008241 iwl4965_unset_hw_setting(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008242 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008243
8244 if (priv->mac80211_registered) {
8245 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008246 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008247 }
8248
Mohamed Abbas948c1712007-10-25 17:15:45 +08008249 /*netif_stop_queue(dev); */
8250 flush_workqueue(priv->workqueue);
8251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008252 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008253 * priv->workqueue... so we can't take down the workqueue
8254 * until now... */
8255 destroy_workqueue(priv->workqueue);
8256 priv->workqueue = NULL;
8257
Zhu Yib481de92007-09-25 17:54:57 -07008258 pci_iounmap(pdev, priv->hw_base);
8259 pci_release_regions(pdev);
8260 pci_disable_device(pdev);
8261 pci_set_drvdata(pdev, NULL);
8262
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008263 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008264 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008265
8266 if (priv->ibss_beacon)
8267 dev_kfree_skb(priv->ibss_beacon);
8268
8269 ieee80211_free_hw(priv->hw);
8270}
8271
8272#ifdef CONFIG_PM
8273
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008274static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008275{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008276 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008277
Zhu Yie655b9f2008-01-24 02:19:38 -08008278 if (priv->is_open) {
8279 set_bit(STATUS_IN_SUSPEND, &priv->status);
8280 iwl4965_mac_stop(priv->hw);
8281 priv->is_open = 1;
8282 }
Zhu Yib481de92007-09-25 17:54:57 -07008283
Zhu Yib481de92007-09-25 17:54:57 -07008284 pci_set_power_state(pdev, PCI_D3hot);
8285
Zhu Yib481de92007-09-25 17:54:57 -07008286 return 0;
8287}
8288
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008289static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008290{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008291 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008292
Zhu Yib481de92007-09-25 17:54:57 -07008293 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008294
Zhu Yie655b9f2008-01-24 02:19:38 -08008295 if (priv->is_open)
8296 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008297
Zhu Yie655b9f2008-01-24 02:19:38 -08008298 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008299 return 0;
8300}
8301
8302#endif /* CONFIG_PM */
8303
8304/*****************************************************************************
8305 *
8306 * driver and module entry point
8307 *
8308 *****************************************************************************/
8309
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008310static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008311 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008312 .id_table = iwl4965_hw_card_ids,
8313 .probe = iwl4965_pci_probe,
8314 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008315#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008316 .suspend = iwl4965_pci_suspend,
8317 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008318#endif
8319};
8320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008321static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008322{
8323
8324 int ret;
8325 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8326 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008327 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008328 if (ret) {
8329 IWL_ERROR("Unable to initialize PCI module\n");
8330 return ret;
8331 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008332#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008333 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008334 if (ret) {
8335 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008336 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008337 return ret;
8338 }
8339#endif
8340
8341 return ret;
8342}
8343
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008344static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008345{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008346#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008347 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008348#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008349 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008350}
8351
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008352module_exit(iwl4965_exit);
8353module_init(iwl4965_init);