blob: 9e49e8ce33484b0bceb76b41383e754364bdc7b1 [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);
1796 }
1797
1798 return 1;
1799}
1800
1801#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1802
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001803static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001804{
1805 switch (status & TX_STATUS_MSK) {
1806 case TX_STATUS_SUCCESS:
1807 return "SUCCESS";
1808 TX_STATUS_ENTRY(SHORT_LIMIT);
1809 TX_STATUS_ENTRY(LONG_LIMIT);
1810 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1811 TX_STATUS_ENTRY(MGMNT_ABORT);
1812 TX_STATUS_ENTRY(NEXT_FRAG);
1813 TX_STATUS_ENTRY(LIFE_EXPIRE);
1814 TX_STATUS_ENTRY(DEST_PS);
1815 TX_STATUS_ENTRY(ABORTED);
1816 TX_STATUS_ENTRY(BT_RETRY);
1817 TX_STATUS_ENTRY(STA_INVALID);
1818 TX_STATUS_ENTRY(FRAG_DROPPED);
1819 TX_STATUS_ENTRY(TID_DISABLE);
1820 TX_STATUS_ENTRY(FRAME_FLUSHED);
1821 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1822 TX_STATUS_ENTRY(TX_LOCKED);
1823 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1824 }
1825
1826 return "UNKNOWN";
1827}
1828
1829/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001830 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001831 *
1832 * NOTE: priv->mutex is not required before calling this function
1833 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001834static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001835{
1836 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1837 clear_bit(STATUS_SCANNING, &priv->status);
1838 return 0;
1839 }
1840
1841 if (test_bit(STATUS_SCANNING, &priv->status)) {
1842 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1843 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1844 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1845 queue_work(priv->workqueue, &priv->abort_scan);
1846
1847 } else
1848 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1849
1850 return test_bit(STATUS_SCANNING, &priv->status);
1851 }
1852
1853 return 0;
1854}
1855
1856/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001857 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001858 * @ms: amount of time to wait (in milliseconds) for scan to abort
1859 *
1860 * NOTE: priv->mutex must be held before calling this function
1861 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001862static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001863{
1864 unsigned long now = jiffies;
1865 int ret;
1866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001867 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001868 if (ret && ms) {
1869 mutex_unlock(&priv->mutex);
1870 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1871 test_bit(STATUS_SCANNING, &priv->status))
1872 msleep(1);
1873 mutex_lock(&priv->mutex);
1874
1875 return test_bit(STATUS_SCANNING, &priv->status);
1876 }
1877
1878 return ret;
1879}
1880
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001881static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001882{
1883 /* Reset ieee stats */
1884
1885 /* We don't reset the net_device_stats (ieee->stats) on
1886 * re-association */
1887
1888 priv->last_seq_num = -1;
1889 priv->last_frag_num = -1;
1890 priv->last_packet_time = 0;
1891
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001892 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001893}
1894
1895#define MAX_UCODE_BEACON_INTERVAL 4096
1896#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1897
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001898static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001899{
1900 u16 new_val = 0;
1901 u16 beacon_factor = 0;
1902
1903 beacon_factor =
1904 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1905 / MAX_UCODE_BEACON_INTERVAL;
1906 new_val = beacon_val / beacon_factor;
1907
1908 return cpu_to_le16(new_val);
1909}
1910
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001911static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001912{
1913 u64 interval_tm_unit;
1914 u64 tsf, result;
1915 unsigned long flags;
1916 struct ieee80211_conf *conf = NULL;
1917 u16 beacon_int = 0;
1918
1919 conf = ieee80211_get_hw_conf(priv->hw);
1920
1921 spin_lock_irqsave(&priv->lock, flags);
1922 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
1923 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
1924
1925 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1926
1927 tsf = priv->timestamp1;
1928 tsf = ((tsf << 32) | priv->timestamp0);
1929
1930 beacon_int = priv->beacon_int;
1931 spin_unlock_irqrestore(&priv->lock, flags);
1932
1933 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1934 if (beacon_int == 0) {
1935 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1936 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1937 } else {
1938 priv->rxon_timing.beacon_interval =
1939 cpu_to_le16(beacon_int);
1940 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001941 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001942 le16_to_cpu(priv->rxon_timing.beacon_interval));
1943 }
1944
1945 priv->rxon_timing.atim_window = 0;
1946 } else {
1947 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001948 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001949 /* TODO: we need to get atim_window from upper stack
1950 * for now we set to 0 */
1951 priv->rxon_timing.atim_window = 0;
1952 }
1953
1954 interval_tm_unit =
1955 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1956 result = do_div(tsf, interval_tm_unit);
1957 priv->rxon_timing.beacon_init_val =
1958 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1959
1960 IWL_DEBUG_ASSOC
1961 ("beacon interval %d beacon timer %d beacon tim %d\n",
1962 le16_to_cpu(priv->rxon_timing.beacon_interval),
1963 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1964 le16_to_cpu(priv->rxon_timing.atim_window));
1965}
1966
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001967static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001968{
1969 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1970 IWL_ERROR("APs don't scan.\n");
1971 return 0;
1972 }
1973
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001974 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001975 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1976 return -EIO;
1977 }
1978
1979 if (test_bit(STATUS_SCANNING, &priv->status)) {
1980 IWL_DEBUG_SCAN("Scan already in progress.\n");
1981 return -EAGAIN;
1982 }
1983
1984 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1985 IWL_DEBUG_SCAN("Scan request while abort pending. "
1986 "Queuing.\n");
1987 return -EAGAIN;
1988 }
1989
1990 IWL_DEBUG_INFO("Starting scan...\n");
1991 priv->scan_bands = 2;
1992 set_bit(STATUS_SCANNING, &priv->status);
1993 priv->scan_start = jiffies;
1994 priv->scan_pass_start = priv->scan_start;
1995
1996 queue_work(priv->workqueue, &priv->request_scan);
1997
1998 return 0;
1999}
2000
Zhu Yib481de92007-09-25 17:54:57 -07002001
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002002static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002003 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002004{
Johannes Berg8318d782008-01-24 19:38:38 +01002005 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002006 priv->staging_rxon.flags &=
2007 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2008 | RXON_FLG_CCK_MSK);
2009 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2010 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002011 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002012 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2013 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2014 else
2015 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2016
2017 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2018 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2019
2020 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2021 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2022 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2023 }
2024}
2025
2026/*
Ian Schram01ebd062007-10-25 17:15:22 +08002027 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002028 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002029static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002030{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002031 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002032
2033 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2034
2035 switch (priv->iw_mode) {
2036 case IEEE80211_IF_TYPE_AP:
2037 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2038 break;
2039
2040 case IEEE80211_IF_TYPE_STA:
2041 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2042 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2043 break;
2044
2045 case IEEE80211_IF_TYPE_IBSS:
2046 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2047 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2048 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2049 RXON_FILTER_ACCEPT_GRP_MSK;
2050 break;
2051
2052 case IEEE80211_IF_TYPE_MNTR:
2053 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2054 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2055 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2056 break;
2057 }
2058
2059#if 0
2060 /* TODO: Figure out when short_preamble would be set and cache from
2061 * that */
2062 if (!hw_to_local(priv->hw)->short_preamble)
2063 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2064 else
2065 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2066#endif
2067
Assaf Krauss8622e702008-03-21 13:53:43 -07002068 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002069 le16_to_cpu(priv->staging_rxon.channel));
2070
2071 if (!ch_info)
2072 ch_info = &priv->channel_info[0];
2073
2074 /*
2075 * in some case A channels are all non IBSS
2076 * in this case force B/G channel
2077 */
2078 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2079 !(is_channel_ibss(ch_info)))
2080 ch_info = &priv->channel_info[0];
2081
2082 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002083 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002084
Johannes Berg8318d782008-01-24 19:38:38 +01002085 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002086
2087 priv->staging_rxon.ofdm_basic_rates =
2088 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2089 priv->staging_rxon.cck_basic_rates =
2090 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2091
2092 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2093 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2094 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2095 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2096 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2097 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2098 iwl4965_set_rxon_chain(priv);
2099}
2100
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002101static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002102{
Zhu Yib481de92007-09-25 17:54:57 -07002103 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002104 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002105
Assaf Krauss8622e702008-03-21 13:53:43 -07002106 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002107 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002108 le16_to_cpu(priv->staging_rxon.channel));
2109
2110 if (!ch_info || !is_channel_ibss(ch_info)) {
2111 IWL_ERROR("channel %d not IBSS channel\n",
2112 le16_to_cpu(priv->staging_rxon.channel));
2113 return -EINVAL;
2114 }
2115 }
2116
Zhu Yib481de92007-09-25 17:54:57 -07002117 priv->iw_mode = mode;
2118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002119 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002120 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2121
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002122 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002123
Mohamed Abbasfde35712007-11-29 11:10:15 +08002124 /* dont commit rxon if rf-kill is on*/
2125 if (!iwl4965_is_ready_rf(priv))
2126 return -EAGAIN;
2127
2128 cancel_delayed_work(&priv->scan_check);
2129 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2130 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2131 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2132 return -EAGAIN;
2133 }
2134
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002135 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002136
2137 return 0;
2138}
2139
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002140static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002141 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07002142 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002143 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002144 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002145{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002146 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002147
2148 switch (keyinfo->alg) {
2149 case ALG_CCMP:
2150 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2151 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07002152 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2153 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002154 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2155 break;
2156
2157 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07002158 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07002159 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
2160 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
2161 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002162 break;
2163
2164 case ALG_WEP:
2165 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2166 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2167
2168 if (keyinfo->keylen == 13)
2169 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2170
2171 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2172
2173 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2174 "with key %d\n", ctl->key_idx);
2175 break;
2176
Zhu Yib481de92007-09-25 17:54:57 -07002177 default:
2178 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2179 break;
2180 }
2181}
2182
2183/*
2184 * handle build REPLY_TX command notification.
2185 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002186static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07002187 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002188 struct ieee80211_tx_control *ctrl,
2189 struct ieee80211_hdr *hdr,
2190 int is_unicast, u8 std_id)
2191{
2192 __le16 *qc;
2193 u16 fc = le16_to_cpu(hdr->frame_control);
2194 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2195
2196 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2197 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2198 tx_flags |= TX_CMD_FLG_ACK_MSK;
2199 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2200 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2201 if (ieee80211_is_probe_response(fc) &&
2202 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2203 tx_flags |= TX_CMD_FLG_TSF_MSK;
2204 } else {
2205 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2206 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2207 }
2208
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002209 if (ieee80211_is_back_request(fc))
2210 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2211
2212
Zhu Yib481de92007-09-25 17:54:57 -07002213 cmd->cmd.tx.sta_id = std_id;
2214 if (ieee80211_get_morefrag(hdr))
2215 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2216
2217 qc = ieee80211_get_qos_ctrl(hdr);
2218 if (qc) {
2219 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2220 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2221 } else
2222 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2223
2224 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2225 tx_flags |= TX_CMD_FLG_RTS_MSK;
2226 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2227 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2228 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2229 tx_flags |= TX_CMD_FLG_CTS_MSK;
2230 }
2231
2232 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2233 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2234
2235 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2236 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2237 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2238 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002239 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002240 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002241 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002242 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002243 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002244 }
Zhu Yib481de92007-09-25 17:54:57 -07002245
2246 cmd->cmd.tx.driver_txop = 0;
2247 cmd->cmd.tx.tx_flags = tx_flags;
2248 cmd->cmd.tx.next_frame_len = 0;
2249}
Tomas Winkler19758be2008-03-12 16:58:51 -07002250static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2251{
2252 /* 0 - mgmt, 1 - cnt, 2 - data */
2253 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2254 priv->tx_stats[idx].cnt++;
2255 priv->tx_stats[idx].bytes += len;
2256}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002257/**
2258 * iwl4965_get_sta_id - Find station's index within station table
2259 *
2260 * If new IBSS station, create new entry in station table
2261 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002262static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002263 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002264{
2265 int sta_id;
2266 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002267 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002268
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002269 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002270 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2271 is_multicast_ether_addr(hdr->addr1))
2272 return priv->hw_setting.bcast_sta_id;
2273
2274 switch (priv->iw_mode) {
2275
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002276 /* If we are a client station in a BSS network, use the special
2277 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002278 case IEEE80211_IF_TYPE_STA:
2279 return IWL_AP_ID;
2280
2281 /* If we are an AP, then find the station, or use BCAST */
2282 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002283 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002284 if (sta_id != IWL_INVALID_STATION)
2285 return sta_id;
2286 return priv->hw_setting.bcast_sta_id;
2287
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002288 /* If this frame is going out to an IBSS network, find the station,
2289 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002290 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002291 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002292 if (sta_id != IWL_INVALID_STATION)
2293 return sta_id;
2294
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002295 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002296 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2297 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002298
2299 if (sta_id != IWL_INVALID_STATION)
2300 return sta_id;
2301
Joe Perches0795af52007-10-03 17:59:30 -07002302 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002303 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002304 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002305 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002306 return priv->hw_setting.bcast_sta_id;
2307
2308 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002309 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002310 return priv->hw_setting.bcast_sta_id;
2311 }
2312}
2313
2314/*
2315 * start REPLY_TX command process
2316 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002317static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002318 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2319{
2320 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002321 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002322 u32 *control_flags;
2323 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002324 struct iwl4965_tx_queue *txq = NULL;
2325 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002326 dma_addr_t phys_addr;
2327 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002328 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07002329 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002330 u16 len, idx, len_org;
2331 u8 id, hdr_len, unicast;
2332 u8 sta_id;
2333 u16 seq_number = 0;
2334 u16 fc;
2335 __le16 *qc;
2336 u8 wait_write_ptr = 0;
2337 unsigned long flags;
2338 int rc;
2339
2340 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002341 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002342 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2343 goto drop_unlock;
2344 }
2345
Johannes Berg32bfd352007-12-19 01:31:26 +01002346 if (!priv->vif) {
2347 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002348 goto drop_unlock;
2349 }
2350
Johannes Berg8318d782008-01-24 19:38:38 +01002351 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002352 IWL_ERROR("ERROR: No TX rate available.\n");
2353 goto drop_unlock;
2354 }
2355
2356 unicast = !is_multicast_ether_addr(hdr->addr1);
2357 id = 0;
2358
2359 fc = le16_to_cpu(hdr->frame_control);
2360
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002361#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002362 if (ieee80211_is_auth(fc))
2363 IWL_DEBUG_TX("Sending AUTH frame\n");
2364 else if (ieee80211_is_assoc_request(fc))
2365 IWL_DEBUG_TX("Sending ASSOC frame\n");
2366 else if (ieee80211_is_reassoc_request(fc))
2367 IWL_DEBUG_TX("Sending REASSOC frame\n");
2368#endif
2369
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002370 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002371 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2372 (!iwl4965_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002373 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002374 !priv->assoc_station_added)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002375 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002376 goto drop_unlock;
2377 }
2378
2379 spin_unlock_irqrestore(&priv->lock, flags);
2380
2381 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002382
2383 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002384 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002385 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002386 DECLARE_MAC_BUF(mac);
2387
2388 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2389 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002390 goto drop;
2391 }
2392
2393 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2394
2395 qc = ieee80211_get_qos_ctrl(hdr);
2396 if (qc) {
2397 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2398 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2399 IEEE80211_SCTL_SEQ;
2400 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2401 (hdr->seq_ctrl &
2402 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2403 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002404#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002405 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002406 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002407 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002408 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002409#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002410 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002411
2412 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002413 txq = &priv->txq[txq_id];
2414 q = &txq->q;
2415
2416 spin_lock_irqsave(&priv->lock, flags);
2417
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002418 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002419 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002420 memset(tfd, 0, sizeof(*tfd));
2421 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002422 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002423
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002424 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002425 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002426 txq->txb[q->write_ptr].skb[0] = skb;
2427 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002428 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002429
2430 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002431 out_cmd = &txq->cmd[idx];
2432 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2433 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002434
2435 /*
2436 * Set up the Tx-command (not MAC!) header.
2437 * Store the chosen Tx queue and TFD index within the sequence field;
2438 * after Tx, uCode's Tx response will return this value so driver can
2439 * locate the frame within the tx queue and do post-tx processing.
2440 */
Zhu Yib481de92007-09-25 17:54:57 -07002441 out_cmd->hdr.cmd = REPLY_TX;
2442 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002443 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002444
2445 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002446 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2447
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002448 /*
2449 * Use the first empty entry in this queue's command buffer array
2450 * to contain the Tx command and MAC header concatenated together
2451 * (payload data will be in another buffer).
2452 * Size of this varies, due to varying MAC header length.
2453 * If end is not dword aligned, we'll have 2 extra bytes at the end
2454 * of the MAC header (device reads on dword boundaries).
2455 * We'll tell device about this padding later.
2456 */
Zhu Yib481de92007-09-25 17:54:57 -07002457 len = priv->hw_setting.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002458 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002459
2460 len_org = len;
2461 len = (len + 3) & ~3;
2462
2463 if (len_org != len)
2464 len_org = 1;
2465 else
2466 len_org = 0;
2467
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002468 /* Physical address of this Tx command's header (not MAC header!),
2469 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002470 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2471 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002472
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002473 /* Add buffer containing Tx command and MAC(!) header to TFD's
2474 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002475 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002476
2477 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002478 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002479
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002480 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2481 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002482 len = skb->len - hdr_len;
2483 if (len) {
2484 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2485 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002486 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002487 }
2488
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002489 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002490 if (len_org)
2491 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2492
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002493 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002494 len = (u16)skb->len;
2495 out_cmd->cmd.tx.len = cpu_to_le16(len);
2496
2497 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002498 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002499
2500 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002501 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002502
Tomas Winkler19758be2008-03-12 16:58:51 -07002503 iwl_update_tx_stats(priv, fc, len);
2504
Tomas Winkler857485c2008-03-21 13:53:44 -07002505 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002506 offsetof(struct iwl4965_tx_cmd, scratch);
2507 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2508 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2509
Zhu Yib481de92007-09-25 17:54:57 -07002510 if (!ieee80211_get_morefrag(hdr)) {
2511 txq->need_update = 1;
2512 if (qc) {
2513 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2514 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2515 }
2516 } else {
2517 wait_write_ptr = 1;
2518 txq->need_update = 0;
2519 }
2520
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002521 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002522 sizeof(out_cmd->cmd.tx));
2523
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002524 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002525 ieee80211_get_hdrlen(fc));
2526
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002527 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002528 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2529
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002530 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002531 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002532 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002533 spin_unlock_irqrestore(&priv->lock, flags);
2534
2535 if (rc)
2536 return rc;
2537
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002538 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002539 && priv->mac80211_registered) {
2540 if (wait_write_ptr) {
2541 spin_lock_irqsave(&priv->lock, flags);
2542 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002543 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002544 spin_unlock_irqrestore(&priv->lock, flags);
2545 }
2546
2547 ieee80211_stop_queue(priv->hw, ctl->queue);
2548 }
2549
2550 return 0;
2551
2552drop_unlock:
2553 spin_unlock_irqrestore(&priv->lock, flags);
2554drop:
2555 return -1;
2556}
2557
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002558static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002559{
Johannes Berg8318d782008-01-24 19:38:38 +01002560 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002561 struct ieee80211_rate *rate;
2562 int i;
2563
Johannes Berg8318d782008-01-24 19:38:38 +01002564 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002565 if (!hw) {
2566 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2567 return;
2568 }
Zhu Yib481de92007-09-25 17:54:57 -07002569
2570 priv->active_rate = 0;
2571 priv->active_rate_basic = 0;
2572
Johannes Berg8318d782008-01-24 19:38:38 +01002573 for (i = 0; i < hw->n_bitrates; i++) {
2574 rate = &(hw->bitrates[i]);
2575 if (rate->hw_value < IWL_RATE_COUNT)
2576 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002577 }
2578
2579 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2580 priv->active_rate, priv->active_rate_basic);
2581
2582 /*
2583 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2584 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2585 * OFDM
2586 */
2587 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2588 priv->staging_rxon.cck_basic_rates =
2589 ((priv->active_rate_basic &
2590 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2591 else
2592 priv->staging_rxon.cck_basic_rates =
2593 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2594
2595 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2596 priv->staging_rxon.ofdm_basic_rates =
2597 ((priv->active_rate_basic &
2598 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2599 IWL_FIRST_OFDM_RATE) & 0xFF;
2600 else
2601 priv->staging_rxon.ofdm_basic_rates =
2602 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2603}
2604
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002605static void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002606{
2607 unsigned long flags;
2608
2609 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2610 return;
2611
2612 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2613 disable_radio ? "OFF" : "ON");
2614
2615 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002616 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002617 /* FIXME: This is a workaround for AP */
2618 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2619 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002620 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002621 CSR_UCODE_SW_BIT_RFKILL);
2622 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002623 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002624 set_bit(STATUS_RF_KILL_SW, &priv->status);
2625 }
2626 return;
2627 }
2628
2629 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002630 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002631
2632 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2633 spin_unlock_irqrestore(&priv->lock, flags);
2634
2635 /* wake up ucode */
2636 msleep(10);
2637
2638 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002639 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2640 if (!iwl_grab_nic_access(priv))
2641 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002642 spin_unlock_irqrestore(&priv->lock, flags);
2643
2644 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2645 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2646 "disabled by HW switch\n");
2647 return;
2648 }
2649
2650 queue_work(priv->workqueue, &priv->restart);
2651 return;
2652}
2653
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002654void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002655 u32 decrypt_res, struct ieee80211_rx_status *stats)
2656{
2657 u16 fc =
2658 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2659
2660 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2661 return;
2662
2663 if (!(fc & IEEE80211_FCTL_PROTECTED))
2664 return;
2665
2666 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2667 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2668 case RX_RES_STATUS_SEC_TYPE_TKIP:
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002669 /* The uCode has got a bad phase 1 Key, pushes the packet.
2670 * Decryption will be done in SW. */
2671 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2672 RX_RES_STATUS_BAD_KEY_TTAK)
2673 break;
2674
Zhu Yib481de92007-09-25 17:54:57 -07002675 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2676 RX_RES_STATUS_BAD_ICV_MIC)
2677 stats->flag |= RX_FLAG_MMIC_ERROR;
2678 case RX_RES_STATUS_SEC_TYPE_WEP:
2679 case RX_RES_STATUS_SEC_TYPE_CCMP:
2680 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2681 RX_RES_STATUS_DECRYPT_OK) {
2682 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2683 stats->flag |= RX_FLAG_DECRYPTED;
2684 }
2685 break;
2686
2687 default:
2688 break;
2689 }
2690}
2691
Zhu Yib481de92007-09-25 17:54:57 -07002692
2693#define IWL_PACKET_RETRY_TIME HZ
2694
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002695int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002696{
2697 u16 sc = le16_to_cpu(header->seq_ctrl);
2698 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2699 u16 frag = sc & IEEE80211_SCTL_FRAG;
2700 u16 *last_seq, *last_frag;
2701 unsigned long *last_time;
2702
2703 switch (priv->iw_mode) {
2704 case IEEE80211_IF_TYPE_IBSS:{
2705 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002706 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002707 u8 *mac = header->addr2;
2708 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2709
2710 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002711 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002712 if (!compare_ether_addr(entry->mac, mac))
2713 break;
2714 }
2715 if (p == &priv->ibss_mac_hash[index]) {
2716 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2717 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002718 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002719 return 0;
2720 }
2721 memcpy(entry->mac, mac, ETH_ALEN);
2722 entry->seq_num = seq;
2723 entry->frag_num = frag;
2724 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002725 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002726 return 0;
2727 }
2728 last_seq = &entry->seq_num;
2729 last_frag = &entry->frag_num;
2730 last_time = &entry->packet_time;
2731 break;
2732 }
2733 case IEEE80211_IF_TYPE_STA:
2734 last_seq = &priv->last_seq_num;
2735 last_frag = &priv->last_frag_num;
2736 last_time = &priv->last_packet_time;
2737 break;
2738 default:
2739 return 0;
2740 }
2741 if ((*last_seq == seq) &&
2742 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2743 if (*last_frag == frag)
2744 goto drop;
2745 if (*last_frag + 1 != frag)
2746 /* out-of-order fragment */
2747 goto drop;
2748 } else
2749 *last_seq = seq;
2750
2751 *last_frag = frag;
2752 *last_time = jiffies;
2753 return 0;
2754
2755 drop:
2756 return 1;
2757}
2758
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002759#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002760
2761#include "iwl-spectrum.h"
2762
2763#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2764#define BEACON_TIME_MASK_HIGH 0xFF000000
2765#define TIME_UNIT 1024
2766
2767/*
2768 * extended beacon time format
2769 * time in usec will be changed into a 32-bit value in 8:24 format
2770 * the high 1 byte is the beacon counts
2771 * the lower 3 bytes is the time in usec within one beacon interval
2772 */
2773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002774static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002775{
2776 u32 quot;
2777 u32 rem;
2778 u32 interval = beacon_interval * 1024;
2779
2780 if (!interval || !usec)
2781 return 0;
2782
2783 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2784 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2785
2786 return (quot << 24) + rem;
2787}
2788
2789/* base is usually what we get from ucode with each received frame,
2790 * the same as HW timer counter counting down
2791 */
2792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002793static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002794{
2795 u32 base_low = base & BEACON_TIME_MASK_LOW;
2796 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2797 u32 interval = beacon_interval * TIME_UNIT;
2798 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2799 (addon & BEACON_TIME_MASK_HIGH);
2800
2801 if (base_low > addon_low)
2802 res += base_low - addon_low;
2803 else if (base_low < addon_low) {
2804 res += interval + base_low - addon_low;
2805 res += (1 << 24);
2806 } else
2807 res += (1 << 24);
2808
2809 return cpu_to_le32(res);
2810}
2811
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002812static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002813 struct ieee80211_measurement_params *params,
2814 u8 type)
2815{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002816 struct iwl4965_spectrum_cmd spectrum;
2817 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002818 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002819 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2820 .data = (void *)&spectrum,
2821 .meta.flags = CMD_WANT_SKB,
2822 };
2823 u32 add_time = le64_to_cpu(params->start_time);
2824 int rc;
2825 int spectrum_resp_status;
2826 int duration = le16_to_cpu(params->duration);
2827
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002828 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002829 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002830 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002831 le64_to_cpu(params->start_time) - priv->last_tsf,
2832 le16_to_cpu(priv->rxon_timing.beacon_interval));
2833
2834 memset(&spectrum, 0, sizeof(spectrum));
2835
2836 spectrum.channel_count = cpu_to_le16(1);
2837 spectrum.flags =
2838 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2839 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2840 cmd.len = sizeof(spectrum);
2841 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2842
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002843 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002844 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002845 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002846 add_time,
2847 le16_to_cpu(priv->rxon_timing.beacon_interval));
2848 else
2849 spectrum.start_time = 0;
2850
2851 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2852 spectrum.channels[0].channel = params->channel;
2853 spectrum.channels[0].type = type;
2854 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2855 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2856 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2857
Tomas Winkler857485c2008-03-21 13:53:44 -07002858 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002859 if (rc)
2860 return rc;
2861
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002862 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002863 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2864 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2865 rc = -EIO;
2866 }
2867
2868 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2869 switch (spectrum_resp_status) {
2870 case 0: /* Command will be handled */
2871 if (res->u.spectrum.id != 0xff) {
2872 IWL_DEBUG_INFO
2873 ("Replaced existing measurement: %d\n",
2874 res->u.spectrum.id);
2875 priv->measurement_status &= ~MEASUREMENT_READY;
2876 }
2877 priv->measurement_status |= MEASUREMENT_ACTIVE;
2878 rc = 0;
2879 break;
2880
2881 case 1: /* Command will not be handled */
2882 rc = -EAGAIN;
2883 break;
2884 }
2885
2886 dev_kfree_skb_any(cmd.meta.u.skb);
2887
2888 return rc;
2889}
2890#endif
2891
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002892static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002893 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002894{
2895
2896 tx_sta->status.ack_signal = 0;
2897 tx_sta->status.excessive_retries = 0;
2898 tx_sta->status.queue_length = 0;
2899 tx_sta->status.queue_number = 0;
2900
2901 if (in_interrupt())
2902 ieee80211_tx_status_irqsafe(priv->hw,
2903 tx_sta->skb[0], &(tx_sta->status));
2904 else
2905 ieee80211_tx_status(priv->hw,
2906 tx_sta->skb[0], &(tx_sta->status));
2907
2908 tx_sta->skb[0] = NULL;
2909}
2910
2911/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002912 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002913 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002914 * When FW advances 'R' index, all entries between old and new 'R' index
2915 * need to be reclaimed. As result, some free space forms. If there is
2916 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002917 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002918int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002919{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002920 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2921 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002922 int nfreed = 0;
2923
2924 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2925 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2926 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002927 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002928 return 0;
2929 }
2930
Tomas Winklerc54b6792008-03-06 17:36:53 -08002931 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002932 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002933 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002934 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002935 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002936 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002937 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002938 } else if (nfreed > 1) {
2939 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002940 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002941 queue_work(priv->workqueue, &priv->restart);
2942 }
2943 nfreed++;
2944 }
2945
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002946/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002947 (txq_id != IWL_CMD_QUEUE_NUM) &&
2948 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002949 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002950
2951
2952 return nfreed;
2953}
2954
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002955static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002956{
2957 status &= TX_STATUS_MSK;
2958 return (status == TX_STATUS_SUCCESS)
2959 || (status == TX_STATUS_DIRECT_DONE);
2960}
2961
2962/******************************************************************************
2963 *
2964 * Generic RX handler implementations
2965 *
2966 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002967#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002968
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002969static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002970 struct ieee80211_hdr *hdr)
2971{
2972 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2973 return IWL_AP_ID;
2974 else {
2975 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002976 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002977 }
2978}
2979
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002980static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002981 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002982{
2983 if (priv->txq[txq_id].txb[idx].skb[0])
2984 return (struct ieee80211_hdr *)priv->txq[txq_id].
2985 txb[idx].skb[0]->data;
2986 return NULL;
2987}
2988
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002989static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002990{
2991 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2992 tx_resp->frame_count);
2993 return le32_to_cpu(*scd_ssn) & MAX_SN;
2994
2995}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002996
2997/**
2998 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2999 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003000static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003001 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003002 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003003 u16 start_idx)
3004{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003005 u16 status;
3006 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07003007 struct ieee80211_tx_status *tx_status = NULL;
3008 struct ieee80211_hdr *hdr = NULL;
3009 int i, sh;
3010 int txq_id, idx;
3011 u16 seq;
3012
3013 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003014 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003015
3016 agg->frame_count = tx_resp->frame_count;
3017 agg->start_idx = start_idx;
3018 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003019 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003020
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003021 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003022 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003023 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003024 status = le16_to_cpu(frame_status[0].status);
3025 seq = le16_to_cpu(frame_status[0].sequence);
3026 idx = SEQ_TO_INDEX(seq);
3027 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003028
Zhu Yib481de92007-09-25 17:54:57 -07003029 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003030 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3031 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003032
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003033 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003034 tx_status->retry_count = tx_resp->failure_frame;
3035 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003036 tx_status->queue_length = tx_resp->failure_rts;
3037 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003038 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003039 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003040 iwl4965_hwrate_to_tx_control(priv,
3041 le32_to_cpu(tx_resp->rate_n_flags),
3042 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003043 /* FIXME: code repetition end */
3044
3045 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3046 status & 0xff, tx_resp->failure_frame);
3047 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003048 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003049
3050 agg->wait_for_ba = 0;
3051 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003052 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003053 u64 bitmap = 0;
3054 int start = agg->start_idx;
3055
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003056 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003057 for (i = 0; i < agg->frame_count; i++) {
3058 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003059 status = le16_to_cpu(frame_status[i].status);
3060 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003061 idx = SEQ_TO_INDEX(seq);
3062 txq_id = SEQ_TO_QUEUE(seq);
3063
3064 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3065 AGG_TX_STATE_ABORT_MSK))
3066 continue;
3067
3068 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3069 agg->frame_count, txq_id, idx);
3070
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003071 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003072
3073 sc = le16_to_cpu(hdr->seq_ctrl);
3074 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3075 IWL_ERROR("BUG_ON idx doesn't match seq control"
3076 " idx=%d, seq_idx=%d, seq=%d\n",
3077 idx, SEQ_TO_SN(sc),
3078 hdr->seq_ctrl);
3079 return -1;
3080 }
3081
3082 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3083 i, idx, SEQ_TO_SN(sc));
3084
3085 sh = idx - start;
3086 if (sh > 64) {
3087 sh = (start - idx) + 0xff;
3088 bitmap = bitmap << sh;
3089 sh = 0;
3090 start = idx;
3091 } else if (sh < -64)
3092 sh = 0xff - (start - idx);
3093 else if (sh < 0) {
3094 sh = start - idx;
3095 start = idx;
3096 bitmap = bitmap << sh;
3097 sh = 0;
3098 }
3099 bitmap |= (1 << sh);
3100 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3101 start, (u32)(bitmap & 0xFFFFFFFF));
3102 }
3103
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003104 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003105 agg->start_idx = start;
3106 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003107 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003108 agg->frame_count, agg->start_idx,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003109 agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003110
3111 if (bitmap)
3112 agg->wait_for_ba = 1;
3113 }
3114 return 0;
3115}
3116#endif
Zhu Yib481de92007-09-25 17:54:57 -07003117
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003118/**
3119 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3120 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003121static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003122 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003123{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003124 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003125 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3126 int txq_id = SEQ_TO_QUEUE(sequence);
3127 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003128 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003129 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003130 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003131 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003132#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003133 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3134 struct ieee80211_hdr *hdr;
3135 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003136#endif
3137
3138 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3139 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3140 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003141 index, txq->q.n_bd, txq->q.write_ptr,
3142 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003143 return;
3144 }
3145
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003146#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003147 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3148 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003149
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003150 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003151 tid = le16_to_cpu(*qc) & 0xf;
3152
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003153 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3154 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3155 IWL_ERROR("Station not known\n");
3156 return;
3157 }
3158
3159 if (txq->sched_retry) {
3160 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3161 struct iwl4965_ht_agg *agg = NULL;
3162
3163 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003164 return;
Zhu Yib481de92007-09-25 17:54:57 -07003165
3166 agg = &priv->stations[sta_id].tid[tid].agg;
3167
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003168 iwl4965_tx_status_reply_tx(priv, agg,
3169 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003170
3171 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003172 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003173 /* TODO: send BAR */
3174 }
3175
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003176 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3177 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003178 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003179 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3180 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003181 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3182 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3183
3184 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3185 txq_id >= 0 && priv->mac80211_registered &&
3186 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3187 ieee80211_wake_queue(priv->hw, txq_id);
3188
3189 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003190 }
3191 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003192#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003193 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003194
3195 tx_status->retry_count = tx_resp->failure_frame;
3196 tx_status->queue_number = status;
3197 tx_status->queue_length = tx_resp->bt_kill_count;
3198 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003199 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003200 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003201 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3202 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003203
Zhu Yib481de92007-09-25 17:54:57 -07003204 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003205 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003206 status, le32_to_cpu(tx_resp->rate_n_flags),
3207 tx_resp->failure_frame);
3208
3209 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003210 if (index != -1) {
3211 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003212#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003213 if (tid != MAX_TID_COUNT)
3214 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3215 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3216 (txq_id >= 0) &&
3217 priv->mac80211_registered)
3218 ieee80211_wake_queue(priv->hw, txq_id);
3219 if (tid != MAX_TID_COUNT)
3220 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3221#endif
Zhu Yib481de92007-09-25 17:54:57 -07003222 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003223#ifdef CONFIG_IWL4965_HT
3224 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003225#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003226
3227 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3228 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3229}
3230
3231
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003232static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003233 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003234{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003235 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3236 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003237 struct delayed_work *pwork;
3238
3239 palive = &pkt->u.alive_frame;
3240
3241 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3242 "0x%01X 0x%01X\n",
3243 palive->is_valid, palive->ver_type,
3244 palive->ver_subtype);
3245
3246 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3247 IWL_DEBUG_INFO("Initialization Alive received.\n");
3248 memcpy(&priv->card_alive_init,
3249 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003250 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003251 pwork = &priv->init_alive_start;
3252 } else {
3253 IWL_DEBUG_INFO("Runtime Alive received.\n");
3254 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003255 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003256 pwork = &priv->alive_start;
3257 }
3258
3259 /* We delay the ALIVE response by 5ms to
3260 * give the HW RF Kill time to activate... */
3261 if (palive->is_valid == UCODE_VALID_OK)
3262 queue_delayed_work(priv->workqueue, pwork,
3263 msecs_to_jiffies(5));
3264 else
3265 IWL_WARNING("uCode did not respond OK.\n");
3266}
3267
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003268static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003269 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003270{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003271 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003272
3273 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3274 return;
3275}
3276
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003277static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003278 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003279{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003280 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003281
3282 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3283 "seq 0x%04X ser 0x%08X\n",
3284 le32_to_cpu(pkt->u.err_resp.error_type),
3285 get_cmd_string(pkt->u.err_resp.cmd_id),
3286 pkt->u.err_resp.cmd_id,
3287 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3288 le32_to_cpu(pkt->u.err_resp.error_info));
3289}
3290
3291#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3292
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003293static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003294{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003295 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3296 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3297 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003298 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3299 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3300 rxon->channel = csa->channel;
3301 priv->staging_rxon.channel = csa->channel;
3302}
3303
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003304static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003305 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003306{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003307#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003308 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3309 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003310
3311 if (!report->state) {
3312 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3313 "Spectrum Measure Notification: Start\n");
3314 return;
3315 }
3316
3317 memcpy(&priv->measure_report, report, sizeof(*report));
3318 priv->measurement_status |= MEASUREMENT_READY;
3319#endif
3320}
3321
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003322static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003323 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003324{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003325#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003326 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3327 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003328 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3329 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3330#endif
3331}
3332
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003333static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003334 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003335{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003336 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003337 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3338 "notification for %s:\n",
3339 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003340 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003341}
3342
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003343static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003344{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003345 struct iwl_priv *priv =
3346 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003347 struct sk_buff *beacon;
3348
3349 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003350 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003351
3352 if (!beacon) {
3353 IWL_ERROR("update beacon failed\n");
3354 return;
3355 }
3356
3357 mutex_lock(&priv->mutex);
3358 /* new beacon skb is allocated every time; dispose previous.*/
3359 if (priv->ibss_beacon)
3360 dev_kfree_skb(priv->ibss_beacon);
3361
3362 priv->ibss_beacon = beacon;
3363 mutex_unlock(&priv->mutex);
3364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003365 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003366}
3367
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003368static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003369 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003370{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003371#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003372 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3373 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3374 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003375
3376 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3377 "tsf %d %d rate %d\n",
3378 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3379 beacon->beacon_notify_hdr.failure_frame,
3380 le32_to_cpu(beacon->ibss_mgr_status),
3381 le32_to_cpu(beacon->high_tsf),
3382 le32_to_cpu(beacon->low_tsf), rate);
3383#endif
3384
3385 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3386 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3387 queue_work(priv->workqueue, &priv->beacon_update);
3388}
3389
3390/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003391static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003392 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003393{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003394#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003395 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3396 struct iwl4965_scanreq_notification *notif =
3397 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003398
3399 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3400#endif
3401}
3402
3403/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003404static void iwl4965_rx_scan_start_notif(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{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003407 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3408 struct iwl4965_scanstart_notification *notif =
3409 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003410 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3411 IWL_DEBUG_SCAN("Scan start: "
3412 "%d [802.11%s] "
3413 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3414 notif->channel,
3415 notif->band ? "bg" : "a",
3416 notif->tsf_high,
3417 notif->tsf_low, notif->status, notif->beacon_timer);
3418}
3419
3420/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003421static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003422 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003423{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003424 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3425 struct iwl4965_scanresults_notification *notif =
3426 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003427
3428 IWL_DEBUG_SCAN("Scan ch.res: "
3429 "%d [802.11%s] "
3430 "(TSF: 0x%08X:%08X) - %d "
3431 "elapsed=%lu usec (%dms since last)\n",
3432 notif->channel,
3433 notif->band ? "bg" : "a",
3434 le32_to_cpu(notif->tsf_high),
3435 le32_to_cpu(notif->tsf_low),
3436 le32_to_cpu(notif->statistics[0]),
3437 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3438 jiffies_to_msecs(elapsed_jiffies
3439 (priv->last_scan_jiffies, jiffies)));
3440
3441 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003442 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003443}
3444
3445/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003446static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003447 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003448{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003449 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3450 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003451
3452 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3453 scan_notif->scanned_channels,
3454 scan_notif->tsf_low,
3455 scan_notif->tsf_high, scan_notif->status);
3456
3457 /* The HW is no longer scanning */
3458 clear_bit(STATUS_SCAN_HW, &priv->status);
3459
3460 /* The scan completion notification came in, so kill that timer... */
3461 cancel_delayed_work(&priv->scan_check);
3462
3463 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3464 (priv->scan_bands == 2) ? "2.4" : "5.2",
3465 jiffies_to_msecs(elapsed_jiffies
3466 (priv->scan_pass_start, jiffies)));
3467
3468 /* Remove this scanned band from the list
3469 * of pending bands to scan */
3470 priv->scan_bands--;
3471
3472 /* If a request to abort was given, or the scan did not succeed
3473 * then we reset the scan state machine and terminate,
3474 * re-queuing another scan if one has been requested */
3475 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3476 IWL_DEBUG_INFO("Aborted scan completed.\n");
3477 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3478 } else {
3479 /* If there are more bands on this scan pass reschedule */
3480 if (priv->scan_bands > 0)
3481 goto reschedule;
3482 }
3483
3484 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003485 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003486 IWL_DEBUG_INFO("Setting scan to off\n");
3487
3488 clear_bit(STATUS_SCANNING, &priv->status);
3489
3490 IWL_DEBUG_INFO("Scan took %dms\n",
3491 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3492
3493 queue_work(priv->workqueue, &priv->scan_completed);
3494
3495 return;
3496
3497reschedule:
3498 priv->scan_pass_start = jiffies;
3499 queue_work(priv->workqueue, &priv->request_scan);
3500}
3501
3502/* Handle notification from uCode that card's power state is changing
3503 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003504static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003505 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003506{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003507 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003508 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3509 unsigned long status = priv->status;
3510
3511 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3512 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3513 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3514
3515 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3516 RF_CARD_DISABLED)) {
3517
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003518 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003519 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3520
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003521 if (!iwl_grab_nic_access(priv)) {
3522 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003523 priv, HBUS_TARG_MBX_C,
3524 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3525
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003526 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003527 }
3528
3529 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003530 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003531 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003532 if (!iwl_grab_nic_access(priv)) {
3533 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003534 priv, HBUS_TARG_MBX_C,
3535 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3536
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003537 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003538 }
3539 }
3540
3541 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003542 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003543 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003544 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3545 if (!iwl_grab_nic_access(priv))
3546 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003547 }
3548 }
3549
3550 if (flags & HW_CARD_DISABLED)
3551 set_bit(STATUS_RF_KILL_HW, &priv->status);
3552 else
3553 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3554
3555
3556 if (flags & SW_CARD_DISABLED)
3557 set_bit(STATUS_RF_KILL_SW, &priv->status);
3558 else
3559 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3560
3561 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003562 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003563
3564 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3565 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3566 (test_bit(STATUS_RF_KILL_SW, &status) !=
3567 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3568 queue_work(priv->workqueue, &priv->rf_kill);
3569 else
3570 wake_up_interruptible(&priv->wait_command_queue);
3571}
3572
3573/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003574 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003575 *
3576 * Setup the RX handlers for each of the reply types sent from the uCode
3577 * to the host.
3578 *
3579 * This function chains into the hardware specific files for them to setup
3580 * any hardware specific handlers as well.
3581 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003582static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003583{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003584 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3585 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3586 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3587 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003588 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003589 iwl4965_rx_spectrum_measure_notif;
3590 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003591 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003592 iwl4965_rx_pm_debug_statistics_notif;
3593 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003594
Ben Cahill9fbab512007-11-29 11:09:47 +08003595 /*
3596 * The same handler is used for both the REPLY to a discrete
3597 * statistics request from the host as well as for the periodic
3598 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003599 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003600 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3601 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003602
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003603 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3604 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003605 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003606 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003607 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003608 iwl4965_rx_scan_complete_notif;
3609 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3610 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003611
Ben Cahill9fbab512007-11-29 11:09:47 +08003612 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003613 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003614}
3615
3616/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003617 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003618 * @rxb: Rx buffer to reclaim
3619 *
3620 * If an Rx buffer has an async callback associated with it the callback
3621 * will be executed. The attached skb (if present) will only be freed
3622 * if the callback returns 1
3623 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003624static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003625 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003626{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003627 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003628 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3629 int txq_id = SEQ_TO_QUEUE(sequence);
3630 int index = SEQ_TO_INDEX(sequence);
3631 int huge = sequence & SEQ_HUGE_FRAME;
3632 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003633 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003634
3635 /* If a Tx command is being handled and it isn't in the actual
3636 * command queue then there a command routing bug has been introduced
3637 * in the queue management code. */
3638 if (txq_id != IWL_CMD_QUEUE_NUM)
3639 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3640 txq_id, pkt->hdr.cmd);
3641 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3642
3643 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3644 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3645
3646 /* Input error checking is done when commands are added to queue. */
3647 if (cmd->meta.flags & CMD_WANT_SKB) {
3648 cmd->meta.source->u.skb = rxb->skb;
3649 rxb->skb = NULL;
3650 } else if (cmd->meta.u.callback &&
3651 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3652 rxb->skb = NULL;
3653
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003654 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003655
3656 if (!(cmd->meta.flags & CMD_ASYNC)) {
3657 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3658 wake_up_interruptible(&priv->wait_command_queue);
3659 }
3660}
3661
3662/************************** RX-FUNCTIONS ****************************/
3663/*
3664 * Rx theory of operation
3665 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003666 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3667 * each of which point to Receive Buffers to be filled by 4965. These get
3668 * used not only for Rx frames, but for any command response or notification
3669 * from the 4965. The driver and 4965 manage the Rx buffers by means
3670 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003671 *
3672 * Rx Queue Indexes
3673 * The host/firmware share two index registers for managing the Rx buffers.
3674 *
3675 * The READ index maps to the first position that the firmware may be writing
3676 * to -- the driver can read up to (but not including) this position and get
3677 * good data.
3678 * The READ index is managed by the firmware once the card is enabled.
3679 *
3680 * The WRITE index maps to the last position the driver has read from -- the
3681 * position preceding WRITE is the last slot the firmware can place a packet.
3682 *
3683 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3684 * WRITE = READ.
3685 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003686 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003687 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3688 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003689 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003690 * and fire the RX interrupt. The driver can then query the READ index and
3691 * process as many packets as possible, moving the WRITE index forward as it
3692 * resets the Rx queue buffers with new memory.
3693 *
3694 * The management in the driver is as follows:
3695 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3696 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003697 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003698 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003699 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3700 * 'processed' and 'read' driver indexes as well)
3701 * + A received packet is processed and handed to the kernel network stack,
3702 * detached from the iwl->rxq. The driver 'processed' index is updated.
3703 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3704 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3705 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3706 * were enough free buffers and RX_STALLED is set it is cleared.
3707 *
3708 *
3709 * Driver sequence:
3710 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003711 * iwl4965_rx_queue_alloc() Allocates rx_free
3712 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003713 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003714 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003715 * queue, updates firmware pointers, and updates
3716 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003717 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003718 *
3719 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003720 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003721 * READ INDEX, detaching the SKB from the pool.
3722 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003723 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003724 * slots.
3725 * ...
3726 *
3727 */
3728
3729/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003730 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003731 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003732static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003733{
3734 int s = q->read - q->write;
3735 if (s <= 0)
3736 s += RX_QUEUE_SIZE;
3737 /* keep some buffer to not confuse full and empty queue */
3738 s -= 2;
3739 if (s < 0)
3740 s = 0;
3741 return s;
3742}
3743
3744/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003745 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003746 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003747int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003748{
3749 u32 reg = 0;
3750 int rc = 0;
3751 unsigned long flags;
3752
3753 spin_lock_irqsave(&q->lock, flags);
3754
3755 if (q->need_update == 0)
3756 goto exit_unlock;
3757
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003758 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003759 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003760 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003761
3762 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003763 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003764 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3765 goto exit_unlock;
3766 }
3767
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003768 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003769 if (rc)
3770 goto exit_unlock;
3771
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003772 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003773 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003774 q->write & ~0x7);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003775 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003776
3777 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003778 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003779 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003780 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003781
3782
3783 q->need_update = 0;
3784
3785 exit_unlock:
3786 spin_unlock_irqrestore(&q->lock, flags);
3787 return rc;
3788}
3789
3790/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003791 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003792 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003793static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003794 dma_addr_t dma_addr)
3795{
3796 return cpu_to_le32((u32)(dma_addr >> 8));
3797}
3798
3799
3800/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003801 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003802 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003803 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003804 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003805 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003806 *
3807 * This moves the 'write' index forward to catch up with 'processed', and
3808 * also updates the memory address in the firmware to reference the new
3809 * target buffer.
3810 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003811static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003812{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003813 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003814 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003815 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003816 unsigned long flags;
3817 int write, rc;
3818
3819 spin_lock_irqsave(&rxq->lock, flags);
3820 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003821 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003822 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003823 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003824 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003825 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003826
3827 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003828 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003829 rxq->queue[rxq->write] = rxb;
3830 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3831 rxq->free_count--;
3832 }
3833 spin_unlock_irqrestore(&rxq->lock, flags);
3834 /* If the pre-allocated buffer pool is dropping low, schedule to
3835 * refill it */
3836 if (rxq->free_count <= RX_LOW_WATERMARK)
3837 queue_work(priv->workqueue, &priv->rx_replenish);
3838
3839
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003840 /* If we've added more space for the firmware to place data, tell it.
3841 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003842 if ((write != (rxq->write & ~0x7))
3843 || (abs(rxq->write - rxq->read) > 7)) {
3844 spin_lock_irqsave(&rxq->lock, flags);
3845 rxq->need_update = 1;
3846 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003847 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003848 if (rc)
3849 return rc;
3850 }
3851
3852 return 0;
3853}
3854
3855/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003856 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003857 *
3858 * When moving to rx_free an SKB is allocated for the slot.
3859 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003860 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003861 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003862 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003863static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003864{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003865 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003866 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003867 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003868 unsigned long flags;
3869 spin_lock_irqsave(&rxq->lock, flags);
3870 while (!list_empty(&rxq->rx_used)) {
3871 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003872 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003873
3874 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003875 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003876 alloc_skb(priv->hw_setting.rx_buf_size,
3877 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003878 if (!rxb->skb) {
3879 if (net_ratelimit())
3880 printk(KERN_CRIT DRV_NAME
3881 ": Can not allocate SKB buffers\n");
3882 /* We don't reschedule replenish work here -- we will
3883 * call the restock method and if it still needs
3884 * more buffers it will schedule replenish */
3885 break;
3886 }
3887 priv->alloc_rxb_skb++;
3888 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003889
3890 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003891 rxb->dma_addr =
3892 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003893 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003894 list_add_tail(&rxb->list, &rxq->rx_free);
3895 rxq->free_count++;
3896 }
3897 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003898}
3899
3900/*
3901 * this should be called while priv->lock is locked
3902*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003903static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003904{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003905 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003906
3907 iwl4965_rx_allocate(priv);
3908 iwl4965_rx_queue_restock(priv);
3909}
3910
3911
3912void iwl4965_rx_replenish(void *data)
3913{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003914 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003915 unsigned long flags;
3916
3917 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003918
3919 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003920 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003921 spin_unlock_irqrestore(&priv->lock, flags);
3922}
3923
3924/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003925 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003926 * This free routine walks the list of POOL entries and if SKB is set to
3927 * non NULL it is unmapped and freed
3928 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003929static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003930{
3931 int i;
3932 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3933 if (rxq->pool[i].skb != NULL) {
3934 pci_unmap_single(priv->pci_dev,
3935 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003936 priv->hw_setting.rx_buf_size,
3937 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003938 dev_kfree_skb(rxq->pool[i].skb);
3939 }
3940 }
3941
3942 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3943 rxq->dma_addr);
3944 rxq->bd = NULL;
3945}
3946
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003947int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003948{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003949 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003950 struct pci_dev *dev = priv->pci_dev;
3951 int i;
3952
3953 spin_lock_init(&rxq->lock);
3954 INIT_LIST_HEAD(&rxq->rx_free);
3955 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003956
3957 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003958 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3959 if (!rxq->bd)
3960 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003961
Zhu Yib481de92007-09-25 17:54:57 -07003962 /* Fill the rx_used queue with _all_ of the Rx buffers */
3963 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3964 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003965
Zhu Yib481de92007-09-25 17:54:57 -07003966 /* Set us so that we have processed and used all buffers, but have
3967 * not restocked the Rx queue with fresh buffers */
3968 rxq->read = rxq->write = 0;
3969 rxq->free_count = 0;
3970 rxq->need_update = 0;
3971 return 0;
3972}
3973
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003974void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003975{
3976 unsigned long flags;
3977 int i;
3978 spin_lock_irqsave(&rxq->lock, flags);
3979 INIT_LIST_HEAD(&rxq->rx_free);
3980 INIT_LIST_HEAD(&rxq->rx_used);
3981 /* Fill the rx_used queue with _all_ of the Rx buffers */
3982 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3983 /* In the reset function, these buffers may have been allocated
3984 * to an SKB, so we need to unmap and free potential storage */
3985 if (rxq->pool[i].skb != NULL) {
3986 pci_unmap_single(priv->pci_dev,
3987 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003988 priv->hw_setting.rx_buf_size,
3989 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003990 priv->alloc_rxb_skb--;
3991 dev_kfree_skb(rxq->pool[i].skb);
3992 rxq->pool[i].skb = NULL;
3993 }
3994 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3995 }
3996
3997 /* Set us so that we have processed and used all buffers, but have
3998 * not restocked the Rx queue with fresh buffers */
3999 rxq->read = rxq->write = 0;
4000 rxq->free_count = 0;
4001 spin_unlock_irqrestore(&rxq->lock, flags);
4002}
4003
4004/* Convert linear signal-to-noise ratio into dB */
4005static u8 ratio2dB[100] = {
4006/* 0 1 2 3 4 5 6 7 8 9 */
4007 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4008 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4009 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4010 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4011 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4012 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4013 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4014 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4015 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4016 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4017};
4018
4019/* Calculates a relative dB value from a ratio of linear
4020 * (i.e. not dB) signal levels.
4021 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004022int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004023{
Adrian Bunkc899a572007-10-14 19:51:15 +02004024 /* 1000:1 or higher just report as 60 dB */
4025 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004026 return 60;
4027
Adrian Bunkc899a572007-10-14 19:51:15 +02004028 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004029 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004030 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004031 return (20 + (int)ratio2dB[sig_ratio/10]);
4032
4033 /* We shouldn't see this */
4034 if (sig_ratio < 1)
4035 return 0;
4036
4037 /* Use table for ratios 1:1 - 99:1 */
4038 return (int)ratio2dB[sig_ratio];
4039}
4040
4041#define PERFECT_RSSI (-20) /* dBm */
4042#define WORST_RSSI (-95) /* dBm */
4043#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4044
4045/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4046 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4047 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004048int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004049{
4050 int sig_qual;
4051 int degradation = PERFECT_RSSI - rssi_dbm;
4052
4053 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4054 * as indicator; formula is (signal dbm - noise dbm).
4055 * SNR at or above 40 is a great signal (100%).
4056 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4057 * Weakest usable signal is usually 10 - 15 dB SNR. */
4058 if (noise_dbm) {
4059 if (rssi_dbm - noise_dbm >= 40)
4060 return 100;
4061 else if (rssi_dbm < noise_dbm)
4062 return 0;
4063 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4064
4065 /* Else use just the signal level.
4066 * This formula is a least squares fit of data points collected and
4067 * compared with a reference system that had a percentage (%) display
4068 * for signal quality. */
4069 } else
4070 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4071 (15 * RSSI_RANGE + 62 * degradation)) /
4072 (RSSI_RANGE * RSSI_RANGE);
4073
4074 if (sig_qual > 100)
4075 sig_qual = 100;
4076 else if (sig_qual < 1)
4077 sig_qual = 0;
4078
4079 return sig_qual;
4080}
4081
4082/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004083 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004084 *
4085 * Uses the priv->rx_handlers callback function array to invoke
4086 * the appropriate handlers, including command responses,
4087 * frame-received notifications, and other notifications.
4088 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004089static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004090{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004091 struct iwl4965_rx_mem_buffer *rxb;
4092 struct iwl4965_rx_packet *pkt;
4093 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004094 u32 r, i;
4095 int reclaim;
4096 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004097 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004098 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004099
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004100 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4101 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004102 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004103 i = rxq->read;
4104
4105 /* Rx interrupt, but nothing sent from uCode */
4106 if (i == r)
4107 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4108
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004109 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4110 fill_rx = 1;
4111
Zhu Yib481de92007-09-25 17:54:57 -07004112 while (i != r) {
4113 rxb = rxq->queue[i];
4114
Ben Cahill9fbab512007-11-29 11:09:47 +08004115 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004116 * then a bug has been introduced in the queue refilling
4117 * routines -- catch it here */
4118 BUG_ON(rxb == NULL);
4119
4120 rxq->queue[i] = NULL;
4121
4122 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004123 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004124 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004125 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004126
4127 /* Reclaim a command buffer only if this packet is a response
4128 * to a (driver-originated) command.
4129 * If the packet (e.g. Rx frame) originated from uCode,
4130 * there is no command buffer to reclaim.
4131 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4132 * but apparently a few don't get set; catch them here. */
4133 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4134 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07004135 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004136 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004137 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4138 (pkt->hdr.cmd != REPLY_TX);
4139
4140 /* Based on type of command response or notification,
4141 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004142 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004143 if (priv->rx_handlers[pkt->hdr.cmd]) {
4144 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4145 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4146 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4147 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4148 } else {
4149 /* No handling needed */
4150 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4151 "r %d i %d No handler needed for %s, 0x%02x\n",
4152 r, i, get_cmd_string(pkt->hdr.cmd),
4153 pkt->hdr.cmd);
4154 }
4155
4156 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004157 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07004158 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004159 * as we reclaim the driver command queue */
4160 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004161 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004162 else
4163 IWL_WARNING("Claim null rxb?\n");
4164 }
4165
4166 /* For now we just don't re-use anything. We can tweak this
4167 * later to try and re-use notification packets and SKBs that
4168 * fail to Rx correctly */
4169 if (rxb->skb != NULL) {
4170 priv->alloc_rxb_skb--;
4171 dev_kfree_skb_any(rxb->skb);
4172 rxb->skb = NULL;
4173 }
4174
4175 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004176 priv->hw_setting.rx_buf_size,
4177 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004178 spin_lock_irqsave(&rxq->lock, flags);
4179 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4180 spin_unlock_irqrestore(&rxq->lock, flags);
4181 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004182 /* If there are a lot of unused frames,
4183 * restock the Rx queue so ucode wont assert. */
4184 if (fill_rx) {
4185 count++;
4186 if (count >= 8) {
4187 priv->rxq.read = i;
4188 __iwl4965_rx_replenish(priv);
4189 count = 0;
4190 }
4191 }
Zhu Yib481de92007-09-25 17:54:57 -07004192 }
4193
4194 /* Backtrack one entry */
4195 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004196 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004197}
4198
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004199/**
4200 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4201 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004202static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004203 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004204{
4205 u32 reg = 0;
4206 int rc = 0;
4207 int txq_id = txq->q.id;
4208
4209 if (txq->need_update == 0)
4210 return rc;
4211
4212 /* if we're trying to save power */
4213 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4214 /* wake up nic if it's powered down ...
4215 * uCode will wake up, and interrupt us again, so next
4216 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004217 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004218
4219 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4220 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004221 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004222 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4223 return rc;
4224 }
4225
4226 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004227 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004228 if (rc)
4229 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004230 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004231 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004232 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004233
4234 /* else not in power-save mode, uCode will never sleep when we're
4235 * trying to tx (during RFKILL, we're not trying to tx). */
4236 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004237 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004238 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004239
4240 txq->need_update = 0;
4241
4242 return rc;
4243}
4244
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004245#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004246static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004247{
Joe Perches0795af52007-10-03 17:59:30 -07004248 DECLARE_MAC_BUF(mac);
4249
Zhu Yib481de92007-09-25 17:54:57 -07004250 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004251 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004252 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4253 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4254 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4255 le32_to_cpu(rxon->filter_flags));
4256 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4257 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4258 rxon->ofdm_basic_rates);
4259 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004260 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4261 print_mac(mac, rxon->node_addr));
4262 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4263 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004264 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4265}
4266#endif
4267
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004268static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004269{
4270 IWL_DEBUG_ISR("Enabling interrupts\n");
4271 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004272 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004273}
4274
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004275static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004276{
4277 clear_bit(STATUS_INT_ENABLED, &priv->status);
4278
4279 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004280 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004281
4282 /* acknowledge/clear/reset any interrupts still pending
4283 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004284 iwl_write32(priv, CSR_INT, 0xffffffff);
4285 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004286 IWL_DEBUG_ISR("Disabled interrupts\n");
4287}
4288
4289static const char *desc_lookup(int i)
4290{
4291 switch (i) {
4292 case 1:
4293 return "FAIL";
4294 case 2:
4295 return "BAD_PARAM";
4296 case 3:
4297 return "BAD_CHECKSUM";
4298 case 4:
4299 return "NMI_INTERRUPT";
4300 case 5:
4301 return "SYSASSERT";
4302 case 6:
4303 return "FATAL_ERROR";
4304 }
4305
4306 return "UNKNOWN";
4307}
4308
4309#define ERROR_START_OFFSET (1 * sizeof(u32))
4310#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4311
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004312static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004313{
4314 u32 data2, line;
4315 u32 desc, time, count, base, data1;
4316 u32 blink1, blink2, ilink1, ilink2;
4317 int rc;
4318
4319 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004321 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004322 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4323 return;
4324 }
4325
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004326 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004327 if (rc) {
4328 IWL_WARNING("Can not read from adapter at this time.\n");
4329 return;
4330 }
4331
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004332 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004333
4334 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4335 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004336 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004337 }
4338
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004339 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
4340 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
4341 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
4342 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
4343 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
4344 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
4345 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
4346 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
4347 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004348
4349 IWL_ERROR("Desc Time "
4350 "data1 data2 line\n");
4351 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4352 desc_lookup(desc), desc, time, data1, data2, line);
4353 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4354 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4355 ilink1, ilink2);
4356
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004357 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004358}
4359
4360#define EVENT_START_OFFSET (4 * sizeof(u32))
4361
4362/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004363 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004364 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004365 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004366 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004367static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004368 u32 num_events, u32 mode)
4369{
4370 u32 i;
4371 u32 base; /* SRAM byte address of event log header */
4372 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4373 u32 ptr; /* SRAM byte address of log data */
4374 u32 ev, time, data; /* event log data */
4375
4376 if (num_events == 0)
4377 return;
4378
4379 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4380
4381 if (mode == 0)
4382 event_size = 2 * sizeof(u32);
4383 else
4384 event_size = 3 * sizeof(u32);
4385
4386 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4387
4388 /* "time" is actually "data" for mode 0 (no timestamp).
4389 * place event id # at far right for easier visual parsing. */
4390 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004391 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004392 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004393 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004394 ptr += sizeof(u32);
4395 if (mode == 0)
4396 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4397 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004398 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004399 ptr += sizeof(u32);
4400 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4401 }
4402 }
4403}
4404
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004405static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004406{
4407 int rc;
4408 u32 base; /* SRAM byte address of event log header */
4409 u32 capacity; /* event log capacity in # entries */
4410 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4411 u32 num_wraps; /* # times uCode wrapped to top of log */
4412 u32 next_entry; /* index of next entry to be written by uCode */
4413 u32 size; /* # entries that we'll print */
4414
4415 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004416 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004417 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4418 return;
4419 }
4420
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004421 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004422 if (rc) {
4423 IWL_WARNING("Can not read from adapter at this time.\n");
4424 return;
4425 }
4426
4427 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004428 capacity = iwl_read_targ_mem(priv, base);
4429 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4430 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4431 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004432
4433 size = num_wraps ? capacity : next_entry;
4434
4435 /* bail out if nothing in log */
4436 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004437 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004438 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004439 return;
4440 }
4441
Zhu Yi583fab32007-09-27 11:27:30 +08004442 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004443 size, num_wraps);
4444
4445 /* if uCode has wrapped back to top of log, start at the oldest entry,
4446 * i.e the next one that uCode would fill. */
4447 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004448 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004449 capacity - next_entry, mode);
4450
4451 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004452 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004453
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004454 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004455}
4456
4457/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004458 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004459 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004460static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004461{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004462 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004463 set_bit(STATUS_FW_ERROR, &priv->status);
4464
4465 /* Cancel currently queued command. */
4466 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4467
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004468#ifdef CONFIG_IWLWIFI_DEBUG
4469 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004470 iwl4965_dump_nic_error_log(priv);
4471 iwl4965_dump_nic_event_log(priv);
4472 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004473 }
4474#endif
4475
4476 wake_up_interruptible(&priv->wait_command_queue);
4477
4478 /* Keep the restart process from trying to send host
4479 * commands by clearing the INIT status bit */
4480 clear_bit(STATUS_READY, &priv->status);
4481
4482 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4483 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4484 "Restarting adapter due to uCode error.\n");
4485
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004486 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004487 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4488 sizeof(priv->recovery_rxon));
4489 priv->error_recovering = 1;
4490 }
4491 queue_work(priv->workqueue, &priv->restart);
4492 }
4493}
4494
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004495static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004496{
4497 unsigned long flags;
4498
4499 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4500 sizeof(priv->staging_rxon));
4501 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004502 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004503
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004504 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004505
4506 spin_lock_irqsave(&priv->lock, flags);
4507 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4508 priv->error_recovering = 0;
4509 spin_unlock_irqrestore(&priv->lock, flags);
4510}
4511
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004512static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004513{
4514 u32 inta, handled = 0;
4515 u32 inta_fh;
4516 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004517#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004518 u32 inta_mask;
4519#endif
4520
4521 spin_lock_irqsave(&priv->lock, flags);
4522
4523 /* Ack/clear/reset pending uCode interrupts.
4524 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4525 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004526 inta = iwl_read32(priv, CSR_INT);
4527 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004528
4529 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4530 * Any new interrupts that happen after this, either while we're
4531 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004532 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4533 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004534
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004535#ifdef CONFIG_IWLWIFI_DEBUG
4536 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004537 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004538 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004539 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4540 inta, inta_mask, inta_fh);
4541 }
4542#endif
4543
4544 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4545 * atomic, make sure that inta covers all the interrupts that
4546 * we've discovered, even if FH interrupt came in just after
4547 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004548 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004549 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004550 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004551 inta |= CSR_INT_BIT_FH_TX;
4552
4553 /* Now service all interrupt bits discovered above. */
4554 if (inta & CSR_INT_BIT_HW_ERR) {
4555 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4556
4557 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004558 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004559
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004560 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004561
4562 handled |= CSR_INT_BIT_HW_ERR;
4563
4564 spin_unlock_irqrestore(&priv->lock, flags);
4565
4566 return;
4567 }
4568
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004569#ifdef CONFIG_IWLWIFI_DEBUG
4570 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004571 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004572 if (inta & CSR_INT_BIT_SCD)
4573 IWL_DEBUG_ISR("Scheduler finished to transmit "
4574 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004575
4576 /* Alive notification via Rx interrupt will do the real work */
4577 if (inta & CSR_INT_BIT_ALIVE)
4578 IWL_DEBUG_ISR("Alive interrupt\n");
4579 }
4580#endif
4581 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004582 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004583
Ben Cahill9fbab512007-11-29 11:09:47 +08004584 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004585 if (inta & CSR_INT_BIT_RF_KILL) {
4586 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004587 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004588 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4589 hw_rf_kill = 1;
4590
4591 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4592 "RF_KILL bit toggled to %s.\n",
4593 hw_rf_kill ? "disable radio":"enable radio");
4594
4595 /* Queue restart only if RF_KILL switch was set to "kill"
4596 * when we loaded driver, and is now set to "enable".
4597 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004598 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004599 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4600 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004601 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004602 }
Zhu Yib481de92007-09-25 17:54:57 -07004603
4604 handled |= CSR_INT_BIT_RF_KILL;
4605 }
4606
Ben Cahill9fbab512007-11-29 11:09:47 +08004607 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004608 if (inta & CSR_INT_BIT_CT_KILL) {
4609 IWL_ERROR("Microcode CT kill error detected.\n");
4610 handled |= CSR_INT_BIT_CT_KILL;
4611 }
4612
4613 /* Error detected by uCode */
4614 if (inta & CSR_INT_BIT_SW_ERR) {
4615 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4616 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004617 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004618 handled |= CSR_INT_BIT_SW_ERR;
4619 }
4620
4621 /* uCode wakes up after power-down sleep */
4622 if (inta & CSR_INT_BIT_WAKEUP) {
4623 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004624 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4625 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4626 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4627 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4628 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4629 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4630 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004631
4632 handled |= CSR_INT_BIT_WAKEUP;
4633 }
4634
4635 /* All uCode command responses, including Tx command responses,
4636 * Rx "responses" (frame-received notification), and other
4637 * notifications from uCode come through here*/
4638 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004639 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004640 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4641 }
4642
4643 if (inta & CSR_INT_BIT_FH_TX) {
4644 IWL_DEBUG_ISR("Tx interrupt\n");
4645 handled |= CSR_INT_BIT_FH_TX;
4646 }
4647
4648 if (inta & ~handled)
4649 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4650
4651 if (inta & ~CSR_INI_SET_MASK) {
4652 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4653 inta & ~CSR_INI_SET_MASK);
4654 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4655 }
4656
4657 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004658 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004659
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004660#ifdef CONFIG_IWLWIFI_DEBUG
4661 if (iwl_debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004662 inta = iwl_read32(priv, CSR_INT);
4663 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4664 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004665 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4666 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4667 }
4668#endif
4669 spin_unlock_irqrestore(&priv->lock, flags);
4670}
4671
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004672static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004673{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004674 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004675 u32 inta, inta_mask;
4676 u32 inta_fh;
4677 if (!priv)
4678 return IRQ_NONE;
4679
4680 spin_lock(&priv->lock);
4681
4682 /* Disable (but don't clear!) interrupts here to avoid
4683 * back-to-back ISRs and sporadic interrupts from our NIC.
4684 * If we have something to service, the tasklet will re-enable ints.
4685 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004686 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4687 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004688
4689 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004690 inta = iwl_read32(priv, CSR_INT);
4691 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004692
4693 /* Ignore interrupt if there's nothing in NIC to service.
4694 * This may be due to IRQ shared with another device,
4695 * or due to sporadic interrupts thrown from our NIC. */
4696 if (!inta && !inta_fh) {
4697 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4698 goto none;
4699 }
4700
4701 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004702 /* Hardware disappeared. It might have already raised
4703 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004704 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004705 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004706 }
4707
4708 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4709 inta, inta_mask, inta_fh);
4710
Joonwoo Park25c03d82008-01-23 10:15:20 -08004711 inta &= ~CSR_INT_BIT_SCD;
4712
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004713 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004714 if (likely(inta || inta_fh))
4715 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004716
Oliver Neukum66fbb542007-11-15 09:31:10 +08004717 unplugged:
4718 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004719 return IRQ_HANDLED;
4720
4721 none:
4722 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004723 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004724 spin_unlock(&priv->lock);
4725 return IRQ_NONE;
4726}
4727
Zhu Yib481de92007-09-25 17:54:57 -07004728/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4729 * sending probe req. This should be set long enough to hear probe responses
4730 * from more than one AP. */
4731#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4732#define IWL_ACTIVE_DWELL_TIME_52 (10)
4733
4734/* For faster active scanning, scan will move to the next channel if fewer than
4735 * PLCP_QUIET_THRESH packets are heard on this channel within
4736 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4737 * time if it's a quiet channel (nothing responded to our probe, and there's
4738 * no other traffic).
4739 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4740#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4741#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4742
4743/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4744 * Must be set longer than active dwell time.
4745 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4746#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4747#define IWL_PASSIVE_DWELL_TIME_52 (10)
4748#define IWL_PASSIVE_DWELL_BASE (100)
4749#define IWL_CHANNEL_TUNE_TIME 5
4750
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004751static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004752 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004753{
Johannes Berg8318d782008-01-24 19:38:38 +01004754 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004755 return IWL_ACTIVE_DWELL_TIME_52;
4756 else
4757 return IWL_ACTIVE_DWELL_TIME_24;
4758}
4759
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004760static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004761 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004762{
Johannes Berg8318d782008-01-24 19:38:38 +01004763 u16 active = iwl4965_get_active_dwell_time(priv, band);
4764 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004765 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4766 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4767
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004768 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004769 /* If we're associated, we clamp the maximum passive
4770 * dwell time to be 98% of the beacon interval (minus
4771 * 2 * channel tune time) */
4772 passive = priv->beacon_int;
4773 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4774 passive = IWL_PASSIVE_DWELL_BASE;
4775 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4776 }
4777
4778 if (passive <= active)
4779 passive = active + 1;
4780
4781 return passive;
4782}
4783
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004784static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004785 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004786 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004787 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004788{
4789 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004790 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004791 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004792 u16 passive_dwell = 0;
4793 u16 active_dwell = 0;
4794 int added, i;
4795
Johannes Berg8318d782008-01-24 19:38:38 +01004796 sband = iwl4965_get_hw_mode(priv, band);
4797 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004798 return 0;
4799
Johannes Berg8318d782008-01-24 19:38:38 +01004800 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004801
Johannes Berg8318d782008-01-24 19:38:38 +01004802 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4803 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004804
Johannes Berg8318d782008-01-24 19:38:38 +01004805 for (i = 0, added = 0; i < sband->n_channels; i++) {
4806 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07004807 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004808 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004809 IWL_DEBUG_SCAN
4810 ("Skipping current channel %d\n",
4811 le16_to_cpu(priv->active_rxon.channel));
4812 continue;
4813 }
4814 } else if (priv->only_active_channel)
4815 continue;
4816
Johannes Berg8318d782008-01-24 19:38:38 +01004817 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004818
Assaf Krauss8622e702008-03-21 13:53:43 -07004819 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004820 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004821 if (!is_channel_valid(ch_info)) {
4822 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4823 scan_ch->channel);
4824 continue;
4825 }
4826
4827 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004828 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004829 scan_ch->type = 0; /* passive */
4830 else
4831 scan_ch->type = 1; /* active */
4832
4833 if (scan_ch->type & 1)
4834 scan_ch->type |= (direct_mask << 1);
4835
4836 if (is_channel_narrow(ch_info))
4837 scan_ch->type |= (1 << 7);
4838
4839 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4840 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4841
Ben Cahill9fbab512007-11-29 11:09:47 +08004842 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004843 scan_ch->tpc.dsp_atten = 110;
4844 /* scan_pwr_info->tpc.dsp_atten; */
4845
4846 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004847 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004848 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4849 else {
4850 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4851 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004852 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004853 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004854 */
4855 }
4856
4857 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4858 scan_ch->channel,
4859 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4860 (scan_ch->type & 1) ?
4861 active_dwell : passive_dwell);
4862
4863 scan_ch++;
4864 added++;
4865 }
4866
4867 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4868 return added;
4869}
4870
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004871static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004872 struct ieee80211_rate *rates)
4873{
4874 int i;
4875
4876 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004877 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
4878 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4879 rates[i].hw_value_short = i;
4880 rates[i].flags = 0;
4881 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004882 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004883 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004884 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07004885 rates[i].flags |=
4886 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
4887 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004888 }
Zhu Yib481de92007-09-25 17:54:57 -07004889 }
Zhu Yib481de92007-09-25 17:54:57 -07004890}
4891
4892/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004893 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004894 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004895int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004896{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004897 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004898 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004899 struct ieee80211_channel *channels;
4900 struct ieee80211_channel *geo_ch;
4901 struct ieee80211_rate *rates;
4902 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004903
Johannes Berg8318d782008-01-24 19:38:38 +01004904 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4905 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004906 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4907 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4908 return 0;
4909 }
4910
Zhu Yib481de92007-09-25 17:54:57 -07004911 channels = kzalloc(sizeof(struct ieee80211_channel) *
4912 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004913 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004914 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004915
Tomas Winkler8211ef72008-03-02 01:36:04 +02004916 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004917 GFP_KERNEL);
4918 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004919 kfree(channels);
4920 return -ENOMEM;
4921 }
4922
Zhu Yib481de92007-09-25 17:54:57 -07004923 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004924 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004925 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02004926 /* just OFDM */
4927 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4928 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01004929
Assaf Krauss1ea87392008-03-18 14:57:50 -07004930 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004931
Tomas Winkler8211ef72008-03-02 01:36:04 +02004932 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4933 sband->channels = channels;
4934 /* OFDM & CCK */
4935 sband->bitrates = rates;
4936 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004937
Assaf Krauss1ea87392008-03-18 14:57:50 -07004938 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004939
Zhu Yib481de92007-09-25 17:54:57 -07004940 priv->ieee_channels = channels;
4941 priv->ieee_rates = rates;
4942
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004943 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004944
Tomas Winkler8211ef72008-03-02 01:36:04 +02004945 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004946 ch = &priv->channel_info[i];
4947
Tomas Winkler8211ef72008-03-02 01:36:04 +02004948 /* FIXME: might be removed if scan is OK */
4949 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004950 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004951
Tomas Winkler8211ef72008-03-02 01:36:04 +02004952 if (is_channel_a_band(ch))
4953 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4954 else
4955 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004956
Tomas Winkler8211ef72008-03-02 01:36:04 +02004957 geo_ch = &sband->channels[sband->n_channels++];
4958
4959 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004960 geo_ch->max_power = ch->max_power_avg;
4961 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004962 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004963
4964 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004965 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4966 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004967
Johannes Berg8318d782008-01-24 19:38:38 +01004968 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4969 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004970
4971 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004972 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004973
4974 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4975 priv->max_channel_txpower_limit =
4976 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004977 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004978 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004979 }
4980
4981 /* Save flags for reg domain usage */
4982 geo_ch->orig_flags = geo_ch->flags;
4983
4984 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4985 ch->channel, geo_ch->center_freq,
4986 is_channel_a_band(ch) ? "5.2" : "2.4",
4987 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4988 "restricted" : "valid",
4989 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004990 }
4991
Tomas Winkler82b9a122008-03-04 18:09:30 -08004992 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4993 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004994 printk(KERN_INFO DRV_NAME
4995 ": Incorrectly detected BG card as ABG. Please send "
4996 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4997 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004998 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004999 }
5000
5001 printk(KERN_INFO DRV_NAME
5002 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005003 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5004 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005005
John W. Linvillee0e0a672008-03-25 15:58:40 -04005006 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
5007 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5008 &priv->bands[IEEE80211_BAND_2GHZ];
5009 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
5010 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5011 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005012
Zhu Yib481de92007-09-25 17:54:57 -07005013 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5014
5015 return 0;
5016}
5017
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005018/*
5019 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5020 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005021void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005022{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005023 kfree(priv->ieee_channels);
5024 kfree(priv->ieee_rates);
5025 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5026}
5027
Zhu Yib481de92007-09-25 17:54:57 -07005028/******************************************************************************
5029 *
5030 * uCode download functions
5031 *
5032 ******************************************************************************/
5033
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005034static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005035{
Tomas Winkler98c92212008-01-14 17:46:20 -08005036 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5037 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5038 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5039 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5040 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5041 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005042}
5043
5044/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005045 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005046 * looking at all data.
5047 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005048static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005049 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005050{
5051 u32 val;
5052 u32 save_len = len;
5053 int rc = 0;
5054 u32 errcnt;
5055
5056 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5057
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005058 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005059 if (rc)
5060 return rc;
5061
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005062 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005063
5064 errcnt = 0;
5065 for (; len > 0; len -= sizeof(u32), image++) {
5066 /* read data comes through single port, auto-incr addr */
5067 /* NOTE: Use the debugless read so we don't flood kernel log
5068 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005069 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005070 if (val != le32_to_cpu(*image)) {
5071 IWL_ERROR("uCode INST section is invalid at "
5072 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5073 save_len - len, val, le32_to_cpu(*image));
5074 rc = -EIO;
5075 errcnt++;
5076 if (errcnt >= 20)
5077 break;
5078 }
5079 }
5080
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005081 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005082
5083 if (!errcnt)
5084 IWL_DEBUG_INFO
5085 ("ucode image in INSTRUCTION memory is good\n");
5086
5087 return rc;
5088}
5089
5090
5091/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005092 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005093 * using sample data 100 bytes apart. If these sample points are good,
5094 * it's a pretty good bet that everything between them is good, too.
5095 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005096static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005097{
5098 u32 val;
5099 int rc = 0;
5100 u32 errcnt = 0;
5101 u32 i;
5102
5103 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5104
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005105 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005106 if (rc)
5107 return rc;
5108
5109 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5110 /* read data comes through single port, auto-incr addr */
5111 /* NOTE: Use the debugless read so we don't flood kernel log
5112 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005113 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005114 i + RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005115 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005116 if (val != le32_to_cpu(*image)) {
5117#if 0 /* Enable this if you want to see details */
5118 IWL_ERROR("uCode INST section is invalid at "
5119 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5120 i, val, *image);
5121#endif
5122 rc = -EIO;
5123 errcnt++;
5124 if (errcnt >= 3)
5125 break;
5126 }
5127 }
5128
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005129 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005130
5131 return rc;
5132}
5133
5134
5135/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005136 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005137 * and verify its contents
5138 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005139static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005140{
5141 __le32 *image;
5142 u32 len;
5143 int rc = 0;
5144
5145 /* Try bootstrap */
5146 image = (__le32 *)priv->ucode_boot.v_addr;
5147 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005148 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005149 if (rc == 0) {
5150 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5151 return 0;
5152 }
5153
5154 /* Try initialize */
5155 image = (__le32 *)priv->ucode_init.v_addr;
5156 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005157 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005158 if (rc == 0) {
5159 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5160 return 0;
5161 }
5162
5163 /* Try runtime/protocol */
5164 image = (__le32 *)priv->ucode_code.v_addr;
5165 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005166 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005167 if (rc == 0) {
5168 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5169 return 0;
5170 }
5171
5172 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5173
Ben Cahill9fbab512007-11-29 11:09:47 +08005174 /* Since nothing seems to match, show first several data entries in
5175 * instruction SRAM, so maybe visual inspection will give a clue.
5176 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005177 image = (__le32 *)priv->ucode_boot.v_addr;
5178 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005179 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005180
5181 return rc;
5182}
5183
5184
5185/* check contents of special bootstrap uCode SRAM */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005186static int iwl4965_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005187{
5188 __le32 *image = priv->ucode_boot.v_addr;
5189 u32 len = priv->ucode_boot.len;
5190 u32 reg;
5191 u32 val;
5192
5193 IWL_DEBUG_INFO("Begin verify bsm\n");
5194
5195 /* verify BSM SRAM contents */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005196 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005197 for (reg = BSM_SRAM_LOWER_BOUND;
5198 reg < BSM_SRAM_LOWER_BOUND + len;
5199 reg += sizeof(u32), image ++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005200 val = iwl_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005201 if (val != le32_to_cpu(*image)) {
5202 IWL_ERROR("BSM uCode verification failed at "
5203 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5204 BSM_SRAM_LOWER_BOUND,
5205 reg - BSM_SRAM_LOWER_BOUND, len,
5206 val, le32_to_cpu(*image));
5207 return -EIO;
5208 }
5209 }
5210
5211 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5212
5213 return 0;
5214}
5215
5216/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005217 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005218 *
5219 * BSM operation:
5220 *
5221 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5222 * in special SRAM that does not power down during RFKILL. When powering back
5223 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5224 * the bootstrap program into the on-board processor, and starts it.
5225 *
5226 * The bootstrap program loads (via DMA) instructions and data for a new
5227 * program from host DRAM locations indicated by the host driver in the
5228 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5229 * automatically.
5230 *
5231 * When initializing the NIC, the host driver points the BSM to the
5232 * "initialize" uCode image. This uCode sets up some internal data, then
5233 * notifies host via "initialize alive" that it is complete.
5234 *
5235 * The host then replaces the BSM_DRAM_* pointer values to point to the
5236 * normal runtime uCode instructions and a backup uCode data cache buffer
5237 * (filled initially with starting data values for the on-board processor),
5238 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5239 * which begins normal operation.
5240 *
5241 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5242 * the backup data cache in DRAM before SRAM is powered down.
5243 *
5244 * When powering back up, the BSM loads the bootstrap program. This reloads
5245 * the runtime uCode instructions and the backup data cache into SRAM,
5246 * and re-launches the runtime uCode from where it left off.
5247 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005248static int iwl4965_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005249{
5250 __le32 *image = priv->ucode_boot.v_addr;
5251 u32 len = priv->ucode_boot.len;
5252 dma_addr_t pinst;
5253 dma_addr_t pdata;
5254 u32 inst_len;
5255 u32 data_len;
5256 int rc;
5257 int i;
5258 u32 done;
5259 u32 reg_offset;
5260
5261 IWL_DEBUG_INFO("Begin load bsm\n");
5262
5263 /* make sure bootstrap program is no larger than BSM's SRAM size */
5264 if (len > IWL_MAX_BSM_SIZE)
5265 return -EINVAL;
5266
5267 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005268 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005269 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005270 * after the "initialize" uCode has run, to point to
5271 * runtime/protocol instructions and backup data cache. */
5272 pinst = priv->ucode_init.p_addr >> 4;
5273 pdata = priv->ucode_init_data.p_addr >> 4;
5274 inst_len = priv->ucode_init.len;
5275 data_len = priv->ucode_init_data.len;
5276
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005277 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005278 if (rc)
5279 return rc;
5280
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005281 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5282 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5283 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5284 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005285
5286 /* Fill BSM memory with bootstrap instructions */
5287 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5288 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5289 reg_offset += sizeof(u32), image++)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005290 _iwl_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005291 le32_to_cpu(*image));
5292
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005293 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005294 if (rc) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005295 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005296 return rc;
5297 }
5298
5299 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005300 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5301 iwl_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005302 RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005303 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005304
5305 /* Load bootstrap code into instruction SRAM now,
5306 * to prepare to load "initialize" uCode */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005307 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005308 BSM_WR_CTRL_REG_BIT_START);
5309
5310 /* Wait for load of bootstrap uCode to finish */
5311 for (i = 0; i < 100; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005312 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005313 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5314 break;
5315 udelay(10);
5316 }
5317 if (i < 100)
5318 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5319 else {
5320 IWL_ERROR("BSM write did not complete!\n");
5321 return -EIO;
5322 }
5323
5324 /* Enable future boot loads whenever power management unit triggers it
5325 * (e.g. when powering back up after power-save shutdown) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005326 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005327 BSM_WR_CTRL_REG_BIT_START_EN);
5328
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005329 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005330
5331 return 0;
5332}
5333
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005334static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005335{
5336 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005337 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005338}
5339
Tomas Winkler90e759d2007-11-29 11:09:41 +08005340
Zhu Yib481de92007-09-25 17:54:57 -07005341/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005342 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005343 *
5344 * Copy into buffers for card to fetch via bus-mastering
5345 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005346static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005347{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005348 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005349 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005350 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005351 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005352 u8 *src;
5353 size_t len;
5354 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5355
5356 /* Ask kernel firmware_class module to get the boot firmware off disk.
5357 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005358 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5359 if (ret < 0) {
5360 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5361 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005362 goto error;
5363 }
5364
5365 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5366 name, ucode_raw->size);
5367
5368 /* Make sure that we got at least our header! */
5369 if (ucode_raw->size < sizeof(*ucode)) {
5370 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005371 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005372 goto err_release;
5373 }
5374
5375 /* Data from ucode file: header followed by uCode images */
5376 ucode = (void *)ucode_raw->data;
5377
5378 ver = le32_to_cpu(ucode->ver);
5379 inst_size = le32_to_cpu(ucode->inst_size);
5380 data_size = le32_to_cpu(ucode->data_size);
5381 init_size = le32_to_cpu(ucode->init_size);
5382 init_data_size = le32_to_cpu(ucode->init_data_size);
5383 boot_size = le32_to_cpu(ucode->boot_size);
5384
5385 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5386 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5387 inst_size);
5388 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5389 data_size);
5390 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5391 init_size);
5392 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5393 init_data_size);
5394 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5395 boot_size);
5396
5397 /* Verify size of file vs. image size info in file's header */
5398 if (ucode_raw->size < sizeof(*ucode) +
5399 inst_size + data_size + init_size +
5400 init_data_size + boot_size) {
5401
5402 IWL_DEBUG_INFO("uCode file size %d too small\n",
5403 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005404 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005405 goto err_release;
5406 }
5407
5408 /* Verify that uCode images will fit in card's SRAM */
5409 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005410 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5411 inst_size);
5412 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005413 goto err_release;
5414 }
5415
5416 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005417 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5418 data_size);
5419 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005420 goto err_release;
5421 }
5422 if (init_size > IWL_MAX_INST_SIZE) {
5423 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005424 ("uCode init instr len %d too large to fit in\n",
5425 init_size);
5426 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005427 goto err_release;
5428 }
5429 if (init_data_size > IWL_MAX_DATA_SIZE) {
5430 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005431 ("uCode init data len %d too large to fit in\n",
5432 init_data_size);
5433 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005434 goto err_release;
5435 }
5436 if (boot_size > IWL_MAX_BSM_SIZE) {
5437 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005438 ("uCode boot instr len %d too large to fit in\n",
5439 boot_size);
5440 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005441 goto err_release;
5442 }
5443
5444 /* Allocate ucode buffers for card's bus-master loading ... */
5445
5446 /* Runtime instructions and 2 copies of data:
5447 * 1) unmodified from disk
5448 * 2) backup cache for save/restore during power-downs */
5449 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005450 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005451
5452 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005453 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005454
5455 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005456 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005457
5458 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005459 if (init_size && init_data_size) {
5460 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005461 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07005462
Tomas Winkler90e759d2007-11-29 11:09:41 +08005463 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005464 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005465
5466 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5467 goto err_pci_alloc;
5468 }
Zhu Yib481de92007-09-25 17:54:57 -07005469
5470 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005471 if (boot_size) {
5472 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005473 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005474
Tomas Winkler90e759d2007-11-29 11:09:41 +08005475 if (!priv->ucode_boot.v_addr)
5476 goto err_pci_alloc;
5477 }
Zhu Yib481de92007-09-25 17:54:57 -07005478
5479 /* Copy images into buffers for card's bus-master reads ... */
5480
5481 /* Runtime instructions (first block of data in file) */
5482 src = &ucode->data[0];
5483 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005484 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005485 memcpy(priv->ucode_code.v_addr, src, len);
5486 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5487 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5488
5489 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005490 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005491 src = &ucode->data[inst_size];
5492 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005493 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005494 memcpy(priv->ucode_data.v_addr, src, len);
5495 memcpy(priv->ucode_data_backup.v_addr, src, len);
5496
5497 /* Initialization instructions (3rd block) */
5498 if (init_size) {
5499 src = &ucode->data[inst_size + data_size];
5500 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005501 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5502 len);
Zhu Yib481de92007-09-25 17:54:57 -07005503 memcpy(priv->ucode_init.v_addr, src, len);
5504 }
5505
5506 /* Initialization data (4th block) */
5507 if (init_data_size) {
5508 src = &ucode->data[inst_size + data_size + init_size];
5509 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005510 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
5511 len);
Zhu Yib481de92007-09-25 17:54:57 -07005512 memcpy(priv->ucode_init_data.v_addr, src, len);
5513 }
5514
5515 /* Bootstrap instructions (5th block) */
5516 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5517 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005518 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005519 memcpy(priv->ucode_boot.v_addr, src, len);
5520
5521 /* We have our copies now, allow OS release its copies */
5522 release_firmware(ucode_raw);
5523 return 0;
5524
5525 err_pci_alloc:
5526 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005527 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005528 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005529
5530 err_release:
5531 release_firmware(ucode_raw);
5532
5533 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005534 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005535}
5536
5537
5538/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005539 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005540 *
5541 * Tell initialization uCode where to find runtime uCode.
5542 *
5543 * BSM registers initially contain pointers to initialization uCode.
5544 * We need to replace them to load runtime uCode inst and data,
5545 * and to save runtime data when powering down.
5546 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005547static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005548{
5549 dma_addr_t pinst;
5550 dma_addr_t pdata;
5551 int rc = 0;
5552 unsigned long flags;
5553
5554 /* bits 35:4 for 4965 */
5555 pinst = priv->ucode_code.p_addr >> 4;
5556 pdata = priv->ucode_data_backup.p_addr >> 4;
5557
5558 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005559 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005560 if (rc) {
5561 spin_unlock_irqrestore(&priv->lock, flags);
5562 return rc;
5563 }
5564
5565 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005566 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5567 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5568 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005569 priv->ucode_data.len);
5570
5571 /* Inst bytecount must be last to set up, bit 31 signals uCode
5572 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005573 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005574 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5575
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005576 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005577
5578 spin_unlock_irqrestore(&priv->lock, flags);
5579
5580 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5581
5582 return rc;
5583}
5584
5585/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005586 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005587 *
5588 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5589 *
5590 * The 4965 "initialize" ALIVE reply contains calibration data for:
5591 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5592 * (3945 does not contain this data).
5593 *
5594 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5595*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005596static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005597{
5598 /* Check alive response for "valid" sign from uCode */
5599 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5600 /* We had an error bringing up the hardware, so take it
5601 * all the way back down so we can try again */
5602 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5603 goto restart;
5604 }
5605
5606 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5607 * This is a paranoid check, because we would not have gotten the
5608 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005609 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005610 /* Runtime instruction load was bad;
5611 * take it all the way back down so we can try again */
5612 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5613 goto restart;
5614 }
5615
5616 /* Calculate temperature */
5617 priv->temperature = iwl4965_get_temperature(priv);
5618
5619 /* Send pointers to protocol/runtime uCode image ... init code will
5620 * load and launch runtime uCode, which will send us another "Alive"
5621 * notification. */
5622 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005623 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005624 /* Runtime instruction load won't happen;
5625 * take it all the way back down so we can try again */
5626 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5627 goto restart;
5628 }
5629 return;
5630
5631 restart:
5632 queue_work(priv->workqueue, &priv->restart);
5633}
5634
5635
5636/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005637 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005638 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005639 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005640 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005641static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005642{
5643 int rc = 0;
5644
5645 IWL_DEBUG_INFO("Runtime Alive received.\n");
5646
5647 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5648 /* We had an error bringing up the hardware, so take it
5649 * all the way back down so we can try again */
5650 IWL_DEBUG_INFO("Alive failed.\n");
5651 goto restart;
5652 }
5653
5654 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5655 * This is a paranoid check, because we would not have gotten the
5656 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005657 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005658 /* Runtime instruction load was bad;
5659 * take it all the way back down so we can try again */
5660 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5661 goto restart;
5662 }
5663
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005664 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005665
5666 rc = iwl4965_alive_notify(priv);
5667 if (rc) {
5668 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
5669 rc);
5670 goto restart;
5671 }
5672
Ben Cahill9fbab512007-11-29 11:09:47 +08005673 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005674 set_bit(STATUS_ALIVE, &priv->status);
5675
5676 /* Clear out the uCode error bit if it is set */
5677 clear_bit(STATUS_FW_ERROR, &priv->status);
5678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005679 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005680 return;
5681
Zhu Yi5a66926a2008-01-14 17:46:18 -08005682 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005683
5684 priv->active_rate = priv->rates_mask;
5685 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5686
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005687 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005688
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005689 if (iwl4965_is_associated(priv)) {
5690 struct iwl4965_rxon_cmd *active_rxon =
5691 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005692
5693 memcpy(&priv->staging_rxon, &priv->active_rxon,
5694 sizeof(priv->staging_rxon));
5695 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5696 } else {
5697 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005698 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005699 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5700 }
5701
Ben Cahill9fbab512007-11-29 11:09:47 +08005702 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005703 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005704
5705 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005706 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005707
5708 /* At this point, the NIC is initialized and operational */
5709 priv->notif_missed_beacons = 0;
5710 set_bit(STATUS_READY, &priv->status);
5711
5712 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005713
Zhu Yib481de92007-09-25 17:54:57 -07005714 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08005715 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005716
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005717 iwl_leds_register(priv);
5718
Zhu Yib481de92007-09-25 17:54:57 -07005719 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005720 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005721
5722 return;
5723
5724 restart:
5725 queue_work(priv->workqueue, &priv->restart);
5726}
5727
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005728static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005729
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005730static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005731{
5732 unsigned long flags;
5733 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5734 struct ieee80211_conf *conf = NULL;
5735
5736 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5737
5738 conf = ieee80211_get_hw_conf(priv->hw);
5739
5740 if (!exit_pending)
5741 set_bit(STATUS_EXIT_PENDING, &priv->status);
5742
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005743 iwl_leds_unregister(priv);
5744
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005745 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005746
5747 /* Unblock any waiting calls */
5748 wake_up_interruptible_all(&priv->wait_command_queue);
5749
Zhu Yib481de92007-09-25 17:54:57 -07005750 /* Wipe out the EXIT_PENDING status bit if we are not actually
5751 * exiting the module */
5752 if (!exit_pending)
5753 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5754
5755 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005756 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005757
5758 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005759 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005760
5761 if (priv->mac80211_registered)
5762 ieee80211_stop_queues(priv->hw);
5763
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005764 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005765 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005766 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005767 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5768 STATUS_RF_KILL_HW |
5769 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5770 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005771 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5772 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005773 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5774 STATUS_IN_SUSPEND;
5775 goto exit;
5776 }
5777
5778 /* ...otherwise clear out all the status bits but the RF Kill and
5779 * SUSPEND bits and continue taking the NIC down. */
5780 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5781 STATUS_RF_KILL_HW |
5782 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5783 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005784 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5785 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005786 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5787 STATUS_IN_SUSPEND |
5788 test_bit(STATUS_FW_ERROR, &priv->status) <<
5789 STATUS_FW_ERROR;
5790
5791 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005792 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08005793 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005794 spin_unlock_irqrestore(&priv->lock, flags);
5795
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005796 iwl4965_hw_txq_ctx_stop(priv);
5797 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005798
5799 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005800 if (!iwl_grab_nic_access(priv)) {
5801 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005802 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005803 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005804 }
5805 spin_unlock_irqrestore(&priv->lock, flags);
5806
5807 udelay(5);
5808
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005809 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005810 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005811 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005812
5813 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005814 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005815
5816 if (priv->ibss_beacon)
5817 dev_kfree_skb(priv->ibss_beacon);
5818 priv->ibss_beacon = NULL;
5819
5820 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005821 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005822}
5823
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005824static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005825{
5826 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005827 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005828 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005830 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005831}
5832
5833#define MAX_HW_RESTARTS 5
5834
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005835static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005836{
5837 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07005838
5839 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5840 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5841 return -EIO;
5842 }
5843
5844 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5845 IWL_WARNING("Radio disabled by SW RF kill (module "
5846 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005847 return -ENODEV;
5848 }
5849
Reinette Chatree903fbd2008-01-30 22:05:15 -08005850 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5851 IWL_ERROR("ucode not available for device bringup\n");
5852 return -EIO;
5853 }
5854
Zhu Yie655b9f2008-01-24 02:19:38 -08005855 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005856 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005857 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5858 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5859 else {
5860 set_bit(STATUS_RF_KILL_HW, &priv->status);
5861 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
5862 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5863 return -ENODEV;
5864 }
Zhu Yib481de92007-09-25 17:54:57 -07005865 }
5866
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005867 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005868
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005869 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005870 if (rc) {
5871 IWL_ERROR("Unable to int nic\n");
5872 return rc;
5873 }
5874
5875 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005876 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5877 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005878 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5879
5880 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005881 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005882 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005883
5884 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005885 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5886 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005887
5888 /* Copy original ucode data image from disk into backup cache.
5889 * This will be used to initialize the on-board processor's
5890 * data SRAM for a clean start when the runtime program first loads. */
5891 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08005892 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005893
Zhu Yie655b9f2008-01-24 02:19:38 -08005894 /* We return success when we resume from suspend and rf_kill is on. */
5895 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005896 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005897
5898 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5899
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005900 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005901
5902 /* load bootstrap state machine,
5903 * load bootstrap program into processor's memory,
5904 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005905 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005906
5907 if (rc) {
5908 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
5909 continue;
5910 }
5911
5912 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005913 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005914
Zhu Yib481de92007-09-25 17:54:57 -07005915 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5916
5917 return 0;
5918 }
5919
5920 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005921 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005922
5923 /* tried to restart and config the device for as long as our
5924 * patience could withstand */
5925 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5926 return -EIO;
5927}
5928
5929
5930/*****************************************************************************
5931 *
5932 * Workqueue callbacks
5933 *
5934 *****************************************************************************/
5935
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005936static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005937{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005938 struct iwl_priv *priv =
5939 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005940
5941 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5942 return;
5943
5944 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005945 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005946 mutex_unlock(&priv->mutex);
5947}
5948
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005949static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005950{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005951 struct iwl_priv *priv =
5952 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005953
5954 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5955 return;
5956
5957 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005958 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005959 mutex_unlock(&priv->mutex);
5960}
5961
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005962static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005963{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005964 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005965
5966 wake_up_interruptible(&priv->wait_command_queue);
5967
5968 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5969 return;
5970
5971 mutex_lock(&priv->mutex);
5972
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005973 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005974 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5975 "HW and/or SW RF Kill no longer active, restarting "
5976 "device\n");
5977 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5978 queue_work(priv->workqueue, &priv->restart);
5979 } else {
5980
5981 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5982 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5983 "disabled by SW switch\n");
5984 else
5985 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5986 "Kill switch must be turned off for "
5987 "wireless networking to work.\n");
5988 }
5989 mutex_unlock(&priv->mutex);
5990}
5991
5992#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5993
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005994static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005995{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005996 struct iwl_priv *priv =
5997 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005998
5999 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6000 return;
6001
6002 mutex_lock(&priv->mutex);
6003 if (test_bit(STATUS_SCANNING, &priv->status) ||
6004 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6005 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6006 "Scan completion watchdog resetting adapter (%dms)\n",
6007 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006008
Zhu Yib481de92007-09-25 17:54:57 -07006009 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006010 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006011 }
6012 mutex_unlock(&priv->mutex);
6013}
6014
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006015static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006016{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006017 struct iwl_priv *priv =
6018 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07006019 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006020 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006021 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006022 .meta.flags = CMD_SIZE_HUGE,
6023 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006024 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006025 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006026 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006027 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006028 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07006029 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006030
6031 conf = ieee80211_get_hw_conf(priv->hw);
6032
6033 mutex_lock(&priv->mutex);
6034
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006035 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006036 IWL_WARNING("request scan called when driver not ready.\n");
6037 goto done;
6038 }
6039
6040 /* Make sure the scan wasn't cancelled before this queued work
6041 * was given the chance to run... */
6042 if (!test_bit(STATUS_SCANNING, &priv->status))
6043 goto done;
6044
6045 /* This should never be called or scheduled if there is currently
6046 * a scan active in the hardware. */
6047 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6048 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6049 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07006050 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07006051 goto done;
6052 }
6053
6054 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6055 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6056 goto done;
6057 }
6058
6059 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6060 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6061 goto done;
6062 }
6063
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006064 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006065 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6066 goto done;
6067 }
6068
6069 if (!test_bit(STATUS_READY, &priv->status)) {
6070 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6071 goto done;
6072 }
6073
6074 if (!priv->scan_bands) {
6075 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6076 goto done;
6077 }
6078
6079 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006080 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006081 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6082 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07006083 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07006084 goto done;
6085 }
6086 }
6087 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006088 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006089
6090 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6091 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6092
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006093 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006094 u16 interval = 0;
6095 u32 extra;
6096 u32 suspend_time = 100;
6097 u32 scan_suspend_time = 100;
6098 unsigned long flags;
6099
6100 IWL_DEBUG_INFO("Scanning while associated...\n");
6101
6102 spin_lock_irqsave(&priv->lock, flags);
6103 interval = priv->beacon_int;
6104 spin_unlock_irqrestore(&priv->lock, flags);
6105
6106 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006107 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006108 if (!interval)
6109 interval = suspend_time;
6110
6111 extra = (suspend_time / interval) << 22;
6112 scan_suspend_time = (extra |
6113 ((suspend_time % interval) * 1024));
6114 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6115 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6116 scan_suspend_time, interval);
6117 }
6118
6119 /* We should add the ability for user to lock to PASSIVE ONLY */
6120 if (priv->one_direct_scan) {
6121 IWL_DEBUG_SCAN
6122 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006123 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006124 priv->direct_ssid_len));
6125 scan->direct_scan[0].id = WLAN_EID_SSID;
6126 scan->direct_scan[0].len = priv->direct_ssid_len;
6127 memcpy(scan->direct_scan[0].ssid,
6128 priv->direct_ssid, priv->direct_ssid_len);
6129 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006130 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006131 scan->direct_scan[0].id = WLAN_EID_SSID;
6132 scan->direct_scan[0].len = priv->essid_len;
6133 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6134 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07006135 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006136 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07006137 }
Zhu Yib481de92007-09-25 17:54:57 -07006138
Zhu Yib481de92007-09-25 17:54:57 -07006139 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6140 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6141 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6142
Zhu Yib481de92007-09-25 17:54:57 -07006143
6144 switch (priv->scan_bands) {
6145 case 2:
6146 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6147 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006148 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006149 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6150
6151 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006152 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006153 break;
6154
6155 case 1:
6156 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006157 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006158 RATE_MCS_ANT_B_MSK);
6159 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006160 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006161 break;
6162
6163 default:
6164 IWL_WARNING("Invalid scan band count\n");
6165 goto done;
6166 }
6167
Tomas Winkler78330fd2008-02-06 02:37:18 +02006168 /* We don't build a direct scan probe request; the uCode will do
6169 * that based on the direct_mask added to each channel entry */
6170 cmd_len = iwl4965_fill_probe_req(priv, band,
6171 (struct ieee80211_mgmt *)scan->data,
6172 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6173
6174 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006175 /* select Rx chains */
6176
6177 /* Force use of chains B and C (0x6) for scan Rx.
6178 * Avoid A (0x1) because of its off-channel reception on A-band.
6179 * MIMO is not used here, but value is required to make uCode happy. */
6180 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6181 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6182 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6183 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6184
6185 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6186 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6187
Reinette Chatre26c0f032008-03-11 16:17:15 -07006188 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07006189 IWL_DEBUG_SCAN
6190 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006191 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07006192 scan->channel_count =
6193 iwl4965_get_channels_for_scan(
6194 priv, band, 1, /* active */
6195 direct_mask,
6196 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6197 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006198 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07006199 scan->channel_count =
6200 iwl4965_get_channels_for_scan(
6201 priv, band, 0, /* passive */
6202 direct_mask,
6203 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6204 }
Zhu Yib481de92007-09-25 17:54:57 -07006205
6206 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006207 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006208 cmd.data = scan;
6209 scan->len = cpu_to_le16(cmd.len);
6210
6211 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07006212 ret = iwl_send_cmd_sync(priv, &cmd);
6213 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006214 goto done;
6215
6216 queue_delayed_work(priv->workqueue, &priv->scan_check,
6217 IWL_SCAN_CHECK_WATCHDOG);
6218
6219 mutex_unlock(&priv->mutex);
6220 return;
6221
6222 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006223 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006224 queue_work(priv->workqueue, &priv->scan_completed);
6225 mutex_unlock(&priv->mutex);
6226}
6227
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006228static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006229{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006230 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006231
6232 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6233 return;
6234
6235 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006236 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006237 mutex_unlock(&priv->mutex);
6238}
6239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006240static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006241{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006242 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006243
6244 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6245 return;
6246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006247 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006248 queue_work(priv->workqueue, &priv->up);
6249}
6250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006251static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006252{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006253 struct iwl_priv *priv =
6254 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006255
6256 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6257 return;
6258
6259 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006260 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006261 mutex_unlock(&priv->mutex);
6262}
6263
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006264#define IWL_DELAY_NEXT_SCAN (HZ*2)
6265
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006266static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006267{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006268 struct iwl_priv *priv = container_of(data, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006269 post_associate.work);
Zhu Yib481de92007-09-25 17:54:57 -07006270 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07006271 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07006272 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006273
6274 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6275 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6276 return;
6277 }
6278
Joe Perches0795af52007-10-03 17:59:30 -07006279 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6280 priv->assoc_id,
6281 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006282
6283
6284 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6285 return;
6286
6287 mutex_lock(&priv->mutex);
6288
Johannes Berg32bfd352007-12-19 01:31:26 +01006289 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006290 mutex_unlock(&priv->mutex);
6291 return;
6292 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006293 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006294
Zhu Yib481de92007-09-25 17:54:57 -07006295 conf = ieee80211_get_hw_conf(priv->hw);
6296
6297 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006298 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006299
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006300 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6301 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006302 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006303 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006304 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006305 IWL_WARNING("REPLY_RXON_TIMING failed - "
6306 "Attempting to continue.\n");
6307
6308 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6309
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006310#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006311 if (priv->current_ht_config.is_ht)
6312 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006313#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006314 iwl4965_set_rxon_chain(priv);
6315 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6316
6317 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6318 priv->assoc_id, priv->beacon_int);
6319
6320 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6321 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6322 else
6323 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6324
6325 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6326 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6327 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6328 else
6329 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6330
6331 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6332 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6333
6334 }
6335
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006336 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006337
6338 switch (priv->iw_mode) {
6339 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006340 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006341 break;
6342
6343 case IEEE80211_IF_TYPE_IBSS:
6344
6345 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006346 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006348 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6349 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6350 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6351 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006352
6353 break;
6354
6355 default:
6356 IWL_ERROR("%s Should not be called in %d mode\n",
6357 __FUNCTION__, priv->iw_mode);
6358 break;
6359 }
6360
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006361 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006362
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006363#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006364 /* Enable Rx differential gain and sensitivity calibrations */
6365 iwl4965_chain_noise_reset(priv);
6366 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006367#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006368
6369 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6370 priv->assoc_station_added = 1;
6371
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006372 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006373
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006374 /* we have just associated, don't start scan too early */
6375 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006376 mutex_unlock(&priv->mutex);
6377}
6378
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006379static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006380{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006381 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006382
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006383 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006384 return;
6385
6386 mutex_lock(&priv->mutex);
6387
6388 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006389 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006390
6391 mutex_unlock(&priv->mutex);
6392}
6393
Zhu Yi76bb77e2007-11-22 10:53:22 +08006394static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6395
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006396static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006397{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006398 struct iwl_priv *priv =
6399 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006400
6401 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6402
6403 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6404 return;
6405
Zhu Yia0646472007-12-20 14:10:01 +08006406 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6407 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006408
Zhu Yib481de92007-09-25 17:54:57 -07006409 ieee80211_scan_completed(priv->hw);
6410
6411 /* Since setting the TXPOWER may have been deferred while
6412 * performing the scan, fire one off */
6413 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006414 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006415 mutex_unlock(&priv->mutex);
6416}
6417
6418/*****************************************************************************
6419 *
6420 * mac80211 entry point functions
6421 *
6422 *****************************************************************************/
6423
Zhu Yi5a66926a2008-01-14 17:46:18 -08006424#define UCODE_READY_TIMEOUT (2 * HZ)
6425
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006426static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006427{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006428 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006429 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006430
6431 IWL_DEBUG_MAC80211("enter\n");
6432
Zhu Yi5a66926a2008-01-14 17:46:18 -08006433 if (pci_enable_device(priv->pci_dev)) {
6434 IWL_ERROR("Fail to pci_enable_device\n");
6435 return -ENODEV;
6436 }
6437 pci_restore_state(priv->pci_dev);
6438 pci_enable_msi(priv->pci_dev);
6439
6440 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6441 DRV_NAME, priv);
6442 if (ret) {
6443 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6444 goto out_disable_msi;
6445 }
6446
Zhu Yib481de92007-09-25 17:54:57 -07006447 /* we should be verifying the device is ready to be opened */
6448 mutex_lock(&priv->mutex);
6449
Zhu Yi5a66926a2008-01-14 17:46:18 -08006450 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
6451 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6452 * ucode filename and max sizes are card-specific. */
6453
6454 if (!priv->ucode_code.len) {
6455 ret = iwl4965_read_ucode(priv);
6456 if (ret) {
6457 IWL_ERROR("Could not read microcode: %d\n", ret);
6458 mutex_unlock(&priv->mutex);
6459 goto out_release_irq;
6460 }
6461 }
6462
Zhu Yie655b9f2008-01-24 02:19:38 -08006463 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006464
Zhu Yib481de92007-09-25 17:54:57 -07006465 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006466
Zhu Yie655b9f2008-01-24 02:19:38 -08006467 if (ret)
6468 goto out_release_irq;
6469
6470 IWL_DEBUG_INFO("Start UP work done.\n");
6471
6472 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6473 return 0;
6474
Zhu Yi5a66926a2008-01-14 17:46:18 -08006475 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6476 * mac80211 will not be run successfully. */
6477 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6478 test_bit(STATUS_READY, &priv->status),
6479 UCODE_READY_TIMEOUT);
6480 if (!ret) {
6481 if (!test_bit(STATUS_READY, &priv->status)) {
6482 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6483 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6484 ret = -ETIMEDOUT;
6485 goto out_release_irq;
6486 }
6487 }
6488
Zhu Yie655b9f2008-01-24 02:19:38 -08006489 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006490 IWL_DEBUG_MAC80211("leave\n");
6491 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006492
6493out_release_irq:
6494 free_irq(priv->pci_dev->irq, priv);
6495out_disable_msi:
6496 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006497 pci_disable_device(priv->pci_dev);
6498 priv->is_open = 0;
6499 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006500 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006501}
6502
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006503static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006504{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006505 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006506
6507 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006508
Zhu Yie655b9f2008-01-24 02:19:38 -08006509 if (!priv->is_open) {
6510 IWL_DEBUG_MAC80211("leave - skip\n");
6511 return;
6512 }
6513
Zhu Yib481de92007-09-25 17:54:57 -07006514 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006515
6516 if (iwl4965_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006517 /* stop mac, cancel any scan request and clear
6518 * RXON_FILTER_ASSOC_MSK BIT
6519 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08006520 mutex_lock(&priv->mutex);
6521 iwl4965_scan_cancel_timeout(priv, 100);
6522 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006523 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006524 }
6525
Zhu Yi5a66926a2008-01-14 17:46:18 -08006526 iwl4965_down(priv);
6527
6528 flush_workqueue(priv->workqueue);
6529 free_irq(priv->pci_dev->irq, priv);
6530 pci_disable_msi(priv->pci_dev);
6531 pci_save_state(priv->pci_dev);
6532 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006533
Zhu Yib481de92007-09-25 17:54:57 -07006534 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006535}
6536
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006537static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006538 struct ieee80211_tx_control *ctl)
6539{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006540 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006541
6542 IWL_DEBUG_MAC80211("enter\n");
6543
6544 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6545 IWL_DEBUG_MAC80211("leave - monitor\n");
6546 return -1;
6547 }
6548
6549 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006550 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006551
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006552 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006553 dev_kfree_skb_any(skb);
6554
6555 IWL_DEBUG_MAC80211("leave\n");
6556 return 0;
6557}
6558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006559static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006560 struct ieee80211_if_init_conf *conf)
6561{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006562 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006563 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006564 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006565
Johannes Berg32bfd352007-12-19 01:31:26 +01006566 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006567
Johannes Berg32bfd352007-12-19 01:31:26 +01006568 if (priv->vif) {
6569 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006570 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006571 }
6572
6573 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006574 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006575
6576 spin_unlock_irqrestore(&priv->lock, flags);
6577
6578 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006579
6580 if (conf->mac_addr) {
6581 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6582 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6583 }
Zhu Yib481de92007-09-25 17:54:57 -07006584
Zhu Yi5a66926a2008-01-14 17:46:18 -08006585 if (iwl4965_is_ready(priv))
6586 iwl4965_set_mode(priv, conf->type);
6587
Zhu Yib481de92007-09-25 17:54:57 -07006588 mutex_unlock(&priv->mutex);
6589
Zhu Yi5a66926a2008-01-14 17:46:18 -08006590 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006591 return 0;
6592}
6593
6594/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006595 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006596 *
6597 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6598 * be set inappropriately and the driver currently sets the hardware up to
6599 * use it whenever needed.
6600 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006601static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006602{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006603 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006604 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006605 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006606 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006607
6608 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006609 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006610
Zhu Yi12342c42007-12-20 11:27:32 +08006611 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6612
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006613 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006614 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006615 ret = -EIO;
6616 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006617 }
6618
Assaf Krauss1ea87392008-03-18 14:57:50 -07006619 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006620 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006621 IWL_DEBUG_MAC80211("leave - scanning\n");
6622 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006623 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006624 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006625 }
6626
6627 spin_lock_irqsave(&priv->lock, flags);
6628
Assaf Krauss8622e702008-03-21 13:53:43 -07006629 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006630 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006631 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006632 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6633 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006634 ret = -EINVAL;
6635 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006636 }
6637
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006638#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006639 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006640 * from any ht related info since 2.4 does not
6641 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006642 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006643#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6644 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6645#endif
6646 )
6647 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006648#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006649
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006650 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006651 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006652
Johannes Berg8318d782008-01-24 19:38:38 +01006653 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006654
6655 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006656 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006657 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006658 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006659
6660 spin_unlock_irqrestore(&priv->lock, flags);
6661
6662#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6663 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006664 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006665 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006666 }
6667#endif
6668
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006669 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006670
6671 if (!conf->radio_enabled) {
6672 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006673 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006674 }
6675
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006676 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006677 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006678 ret = -EIO;
6679 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006680 }
6681
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006682 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006683
6684 if (memcmp(&priv->active_rxon,
6685 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006686 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006687 else
6688 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6689
6690 IWL_DEBUG_MAC80211("leave\n");
6691
Zhu Yia0646472007-12-20 14:10:01 +08006692out:
6693 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006694 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006695 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006696}
6697
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006698static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006699{
Tomas Winkler857485c2008-03-21 13:53:44 -07006700 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006701
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006702 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006703 return;
6704
6705 /* The following should be done only at AP bring up */
6706 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6707
6708 /* RXON - unassoc (to set timing command) */
6709 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006710 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006711
6712 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006713 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6714 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006715 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006716 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006717 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006718 IWL_WARNING("REPLY_RXON_TIMING failed - "
6719 "Attempting to continue.\n");
6720
6721 iwl4965_set_rxon_chain(priv);
6722
6723 /* FIXME: what should be the assoc_id for AP? */
6724 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6725 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6726 priv->staging_rxon.flags |=
6727 RXON_FLG_SHORT_PREAMBLE_MSK;
6728 else
6729 priv->staging_rxon.flags &=
6730 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6731
6732 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6733 if (priv->assoc_capability &
6734 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6735 priv->staging_rxon.flags |=
6736 RXON_FLG_SHORT_SLOT_MSK;
6737 else
6738 priv->staging_rxon.flags &=
6739 ~RXON_FLG_SHORT_SLOT_MSK;
6740
6741 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6742 priv->staging_rxon.flags &=
6743 ~RXON_FLG_SHORT_SLOT_MSK;
6744 }
6745 /* restore RXON assoc */
6746 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006747 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006748 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006749 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006750 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006751 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006752
6753 /* FIXME - we need to add code here to detect a totally new
6754 * configuration, reset the AP, unassoc, rxon timing, assoc,
6755 * clear sta table, add BCAST sta... */
6756}
6757
Johannes Berg32bfd352007-12-19 01:31:26 +01006758static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6759 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006760 struct ieee80211_if_conf *conf)
6761{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006762 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006763 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006764 unsigned long flags;
6765 int rc;
6766
6767 if (conf == NULL)
6768 return -EIO;
6769
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006770 if (priv->vif != vif) {
6771 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
6772 mutex_unlock(&priv->mutex);
6773 return 0;
6774 }
6775
Zhu Yib481de92007-09-25 17:54:57 -07006776 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6777 (!conf->beacon || !conf->ssid_len)) {
6778 IWL_DEBUG_MAC80211
6779 ("Leaving in AP mode because HostAPD is not ready.\n");
6780 return 0;
6781 }
6782
Zhu Yi5a66926a2008-01-14 17:46:18 -08006783 if (!iwl4965_is_alive(priv))
6784 return -EAGAIN;
6785
Zhu Yib481de92007-09-25 17:54:57 -07006786 mutex_lock(&priv->mutex);
6787
Zhu Yib481de92007-09-25 17:54:57 -07006788 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006789 IWL_DEBUG_MAC80211("bssid: %s\n",
6790 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006791
Johannes Berg4150c572007-09-17 01:29:23 -04006792/*
6793 * very dubious code was here; the probe filtering flag is never set:
6794 *
Zhu Yib481de92007-09-25 17:54:57 -07006795 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6796 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006797 */
Zhu Yib481de92007-09-25 17:54:57 -07006798
6799 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6800 if (!conf->bssid) {
6801 conf->bssid = priv->mac_addr;
6802 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006803 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6804 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006805 }
6806 if (priv->ibss_beacon)
6807 dev_kfree_skb(priv->ibss_beacon);
6808
6809 priv->ibss_beacon = conf->beacon;
6810 }
6811
Mohamed Abbasfde35712007-11-29 11:10:15 +08006812 if (iwl4965_is_rfkill(priv))
6813 goto done;
6814
Zhu Yib481de92007-09-25 17:54:57 -07006815 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6816 !is_multicast_ether_addr(conf->bssid)) {
6817 /* If there is currently a HW scan going on in the background
6818 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006819 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006820 IWL_WARNING("Aborted scan still in progress "
6821 "after 100ms\n");
6822 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6823 mutex_unlock(&priv->mutex);
6824 return -EAGAIN;
6825 }
6826 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6827
6828 /* TODO: Audit driver for usage of these members and see
6829 * if mac80211 deprecates them (priv->bssid looks like it
6830 * shouldn't be there, but I haven't scanned the IBSS code
6831 * to verify) - jpk */
6832 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6833
6834 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006835 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006836 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006837 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006838 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006839 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006840 priv, priv->active_rxon.bssid_addr, 1);
6841 }
6842
6843 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006844 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006845 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006846 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006847 }
6848
Mohamed Abbasfde35712007-11-29 11:10:15 +08006849 done:
Zhu Yib481de92007-09-25 17:54:57 -07006850 spin_lock_irqsave(&priv->lock, flags);
6851 if (!conf->ssid_len)
6852 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6853 else
6854 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6855
6856 priv->essid_len = conf->ssid_len;
6857 spin_unlock_irqrestore(&priv->lock, flags);
6858
6859 IWL_DEBUG_MAC80211("leave\n");
6860 mutex_unlock(&priv->mutex);
6861
6862 return 0;
6863}
6864
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006865static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006866 unsigned int changed_flags,
6867 unsigned int *total_flags,
6868 int mc_count, struct dev_addr_list *mc_list)
6869{
6870 /*
6871 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006872 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006873 */
6874 *total_flags = 0;
6875}
6876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006877static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006878 struct ieee80211_if_init_conf *conf)
6879{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006880 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006881
6882 IWL_DEBUG_MAC80211("enter\n");
6883
6884 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006885
Mohamed Abbasfde35712007-11-29 11:10:15 +08006886 if (iwl4965_is_ready_rf(priv)) {
6887 iwl4965_scan_cancel_timeout(priv, 100);
6888 cancel_delayed_work(&priv->post_associate);
6889 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6890 iwl4965_commit_rxon(priv);
6891 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006892 if (priv->vif == conf->vif) {
6893 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006894 memset(priv->bssid, 0, ETH_ALEN);
6895 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6896 priv->essid_len = 0;
6897 }
6898 mutex_unlock(&priv->mutex);
6899
6900 IWL_DEBUG_MAC80211("leave\n");
6901
6902}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006903
6904static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6905 struct ieee80211_vif *vif,
6906 struct ieee80211_bss_conf *bss_conf,
6907 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006908{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006909 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006910
Johannes Berg471b3ef2007-12-28 14:32:58 +01006911 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6912 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006913 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6914 else
6915 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6916 }
6917
Johannes Berg471b3ef2007-12-28 14:32:58 +01006918 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Johannes Berg8318d782008-01-24 19:38:38 +01006919 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006920 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6921 else
6922 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6923 }
6924
Johannes Berg471b3ef2007-12-28 14:32:58 +01006925 if (changes & BSS_CHANGED_ASSOC) {
6926 /*
6927 * TODO:
6928 * do stuff instead of sniffing assoc resp
6929 */
6930 }
6931
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006932 if (iwl4965_is_associated(priv))
6933 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02006934}
Zhu Yib481de92007-09-25 17:54:57 -07006935
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006936static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006937{
6938 int rc = 0;
6939 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006940 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006941
6942 IWL_DEBUG_MAC80211("enter\n");
6943
mabbas052c4b92007-10-25 17:15:43 +08006944 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006945 spin_lock_irqsave(&priv->lock, flags);
6946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006947 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006948 rc = -EIO;
6949 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6950 goto out_unlock;
6951 }
6952
6953 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
6954 rc = -EIO;
6955 IWL_ERROR("ERROR: APs don't scan\n");
6956 goto out_unlock;
6957 }
6958
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006959 /* we don't schedule scan within next_scan_jiffies period */
6960 if (priv->next_scan_jiffies &&
6961 time_after(priv->next_scan_jiffies, jiffies)) {
6962 rc = -EAGAIN;
6963 goto out_unlock;
6964 }
Zhu Yib481de92007-09-25 17:54:57 -07006965 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006966 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
6967 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006968 rc = -EAGAIN;
6969 goto out_unlock;
6970 }
6971 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006972 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006973 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006974
6975 priv->one_direct_scan = 1;
6976 priv->direct_ssid_len = (u8)
6977 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6978 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006979 } else
6980 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006981
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006982 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006983
6984 IWL_DEBUG_MAC80211("leave\n");
6985
6986out_unlock:
6987 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08006988 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006989
6990 return rc;
6991}
6992
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006993static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
6994 struct ieee80211_key_conf *keyconf, const u8 *addr,
6995 u32 iv32, u16 *phase1key)
6996{
6997 struct iwl_priv *priv = hw->priv;
6998 u8 sta_id = IWL_INVALID_STATION;
6999 unsigned long flags;
7000 __le16 key_flags = 0;
7001 int i;
7002 DECLARE_MAC_BUF(mac);
7003
7004 IWL_DEBUG_MAC80211("enter\n");
7005
7006 sta_id = iwl4965_hw_find_station(priv, addr);
7007 if (sta_id == IWL_INVALID_STATION) {
7008 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7009 print_mac(mac, addr));
7010 return;
7011 }
7012
7013 iwl4965_scan_cancel_timeout(priv, 100);
7014
7015 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
7016 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
7017 key_flags &= ~STA_KEY_FLG_INVALID;
7018
7019 if (sta_id == priv->hw_setting.bcast_sta_id)
7020 key_flags |= STA_KEY_MULTICAST_MSK;
7021
7022 spin_lock_irqsave(&priv->sta_lock, flags);
7023
7024 priv->stations[sta_id].sta.key.key_offset =
7025 (sta_id % STA_KEY_MAX_NUM);/* FIXME */
7026 priv->stations[sta_id].sta.key.key_flags = key_flags;
7027 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
7028
7029 for (i = 0; i < 5; i++)
7030 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
7031 cpu_to_le16(phase1key[i]);
7032
7033 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
7034 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
7035
7036 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
7037
7038 spin_unlock_irqrestore(&priv->sta_lock, flags);
7039
7040 IWL_DEBUG_MAC80211("leave\n");
7041}
7042
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007043static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007044 const u8 *local_addr, const u8 *addr,
7045 struct ieee80211_key_conf *key)
7046{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007047 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007048 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007049 int ret = 0;
7050 u8 sta_id = IWL_INVALID_STATION;
7051 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07007052
7053 IWL_DEBUG_MAC80211("enter\n");
7054
Assaf Krauss1ea87392008-03-18 14:57:50 -07007055 if (!priv->cfg->mod_params->hw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007056 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7057 return -EOPNOTSUPP;
7058 }
7059
7060 if (is_zero_ether_addr(addr))
7061 /* only support pairwise keys */
7062 return -EOPNOTSUPP;
7063
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007064 /* FIXME: need to differenciate between static and dynamic key
7065 * in the level of mac80211 */
7066 static_key = !iwl4965_is_associated(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007067
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007068 if (!static_key) {
7069 sta_id = iwl4965_hw_find_station(priv, addr);
7070 if (sta_id == IWL_INVALID_STATION) {
7071 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7072 print_mac(mac, addr));
7073 return -EINVAL;
7074 }
7075 }
Zhu Yib481de92007-09-25 17:54:57 -07007076
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007077 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007078
Zhu Yib481de92007-09-25 17:54:57 -07007079 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007080 case SET_KEY:
7081 if (static_key)
7082 ret = iwl4965_set_static_key(priv, key);
7083 else
7084 ret = iwl4965_set_dynamic_key(priv, key, sta_id);
7085
7086 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007087 break;
7088 case DISABLE_KEY:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007089 if (static_key)
7090 ret = iwl4965_remove_static_key(priv);
7091 else
7092 ret = iwl4965_clear_sta_key_info(priv, sta_id);
7093
7094 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007095 break;
7096 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007097 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07007098 }
7099
7100 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007101
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007102 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007103}
7104
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007105static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007106 const struct ieee80211_tx_queue_params *params)
7107{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007108 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007109 unsigned long flags;
7110 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007111
7112 IWL_DEBUG_MAC80211("enter\n");
7113
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007114 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007115 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7116 return -EIO;
7117 }
7118
7119 if (queue >= AC_NUM) {
7120 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7121 return 0;
7122 }
7123
Zhu Yib481de92007-09-25 17:54:57 -07007124 if (!priv->qos_data.qos_enable) {
7125 priv->qos_data.qos_active = 0;
7126 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7127 return 0;
7128 }
7129 q = AC_NUM - 1 - queue;
7130
7131 spin_lock_irqsave(&priv->lock, flags);
7132
7133 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7134 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7135 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7136 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007137 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007138
7139 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7140 priv->qos_data.qos_active = 1;
7141
7142 spin_unlock_irqrestore(&priv->lock, flags);
7143
7144 mutex_lock(&priv->mutex);
7145 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007146 iwl4965_activate_qos(priv, 1);
7147 else if (priv->assoc_id && iwl4965_is_associated(priv))
7148 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007149
7150 mutex_unlock(&priv->mutex);
7151
Zhu Yib481de92007-09-25 17:54:57 -07007152 IWL_DEBUG_MAC80211("leave\n");
7153 return 0;
7154}
7155
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007156static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007157 struct ieee80211_tx_queue_stats *stats)
7158{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007159 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007160 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007161 struct iwl4965_tx_queue *txq;
7162 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007163 unsigned long flags;
7164
7165 IWL_DEBUG_MAC80211("enter\n");
7166
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007167 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007168 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7169 return -EIO;
7170 }
7171
7172 spin_lock_irqsave(&priv->lock, flags);
7173
7174 for (i = 0; i < AC_NUM; i++) {
7175 txq = &priv->txq[i];
7176 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007177 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007178
7179 stats->data[i].len = q->n_window - avail;
7180 stats->data[i].limit = q->n_window - q->high_mark;
7181 stats->data[i].count = q->n_window;
7182
7183 }
7184 spin_unlock_irqrestore(&priv->lock, flags);
7185
7186 IWL_DEBUG_MAC80211("leave\n");
7187
7188 return 0;
7189}
7190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007191static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007192 struct ieee80211_low_level_stats *stats)
7193{
7194 IWL_DEBUG_MAC80211("enter\n");
7195 IWL_DEBUG_MAC80211("leave\n");
7196
7197 return 0;
7198}
7199
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007200static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007201{
7202 IWL_DEBUG_MAC80211("enter\n");
7203 IWL_DEBUG_MAC80211("leave\n");
7204
7205 return 0;
7206}
7207
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007208static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007209{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007210 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007211 unsigned long flags;
7212
7213 mutex_lock(&priv->mutex);
7214 IWL_DEBUG_MAC80211("enter\n");
7215
7216 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007217#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007218 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007219 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007220 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007221#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007222
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007223 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007224
7225 cancel_delayed_work(&priv->post_associate);
7226
7227 spin_lock_irqsave(&priv->lock, flags);
7228 priv->assoc_id = 0;
7229 priv->assoc_capability = 0;
7230 priv->call_post_assoc_from_beacon = 0;
7231 priv->assoc_station_added = 0;
7232
7233 /* new association get rid of ibss beacon skb */
7234 if (priv->ibss_beacon)
7235 dev_kfree_skb(priv->ibss_beacon);
7236
7237 priv->ibss_beacon = NULL;
7238
7239 priv->beacon_int = priv->hw->conf.beacon_int;
7240 priv->timestamp1 = 0;
7241 priv->timestamp0 = 0;
7242 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7243 priv->beacon_int = 0;
7244
7245 spin_unlock_irqrestore(&priv->lock, flags);
7246
Mohamed Abbasfde35712007-11-29 11:10:15 +08007247 if (!iwl4965_is_ready_rf(priv)) {
7248 IWL_DEBUG_MAC80211("leave - not ready\n");
7249 mutex_unlock(&priv->mutex);
7250 return;
7251 }
7252
mabbas052c4b92007-10-25 17:15:43 +08007253 /* we are restarting association process
7254 * clear RXON_FILTER_ASSOC_MSK bit
7255 */
7256 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007257 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007258 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007259 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007260 }
7261
Zhu Yib481de92007-09-25 17:54:57 -07007262 /* Per mac80211.h: This is only used in IBSS mode... */
7263 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007264
Zhu Yib481de92007-09-25 17:54:57 -07007265 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7266 mutex_unlock(&priv->mutex);
7267 return;
7268 }
7269
Zhu Yib481de92007-09-25 17:54:57 -07007270 priv->only_active_channel = 0;
7271
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007272 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007273
7274 mutex_unlock(&priv->mutex);
7275
7276 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007277}
7278
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007279static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007280 struct ieee80211_tx_control *control)
7281{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007282 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007283 unsigned long flags;
7284
7285 mutex_lock(&priv->mutex);
7286 IWL_DEBUG_MAC80211("enter\n");
7287
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007288 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007289 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7290 mutex_unlock(&priv->mutex);
7291 return -EIO;
7292 }
7293
7294 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7295 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7296 mutex_unlock(&priv->mutex);
7297 return -EIO;
7298 }
7299
7300 spin_lock_irqsave(&priv->lock, flags);
7301
7302 if (priv->ibss_beacon)
7303 dev_kfree_skb(priv->ibss_beacon);
7304
7305 priv->ibss_beacon = skb;
7306
7307 priv->assoc_id = 0;
7308
7309 IWL_DEBUG_MAC80211("leave\n");
7310 spin_unlock_irqrestore(&priv->lock, flags);
7311
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007312 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007313
7314 queue_work(priv->workqueue, &priv->post_associate.work);
7315
7316 mutex_unlock(&priv->mutex);
7317
7318 return 0;
7319}
7320
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007321#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007322
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007323static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007324 struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007325{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007326 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
7327 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
7328 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07007329
7330 IWL_DEBUG_MAC80211("enter: \n");
7331
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007332 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
7333 iwl_conf->is_ht = 0;
7334 return;
Zhu Yib481de92007-09-25 17:54:57 -07007335 }
7336
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007337 iwl_conf->is_ht = 1;
7338 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07007339
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007340 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
7341 iwl_conf->sgf |= 0x1;
7342 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
7343 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07007344
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007345 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
7346 iwl_conf->max_amsdu_size =
7347 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007348
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007349 iwl_conf->supported_chan_width =
7350 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007351 iwl_conf->extension_chan_offset =
7352 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
7353 /* If no above or below channel supplied disable FAT channel */
7354 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
7355 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
7356 iwl_conf->supported_chan_width = 0;
7357
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007358 iwl_conf->tx_mimo_ps_mode =
7359 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
7360 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07007361
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007362 iwl_conf->control_channel = ht_bss_conf->primary_channel;
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007363 iwl_conf->tx_chan_width =
7364 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
7365 iwl_conf->ht_protection =
7366 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
7367 iwl_conf->non_GF_STA_present =
7368 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07007369
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007370 IWL_DEBUG_MAC80211("control channel %d\n",
7371 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007372 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007373}
7374
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007375static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007376 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007377{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007378 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007379
7380 IWL_DEBUG_MAC80211("enter: \n");
7381
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007382 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007383 iwl4965_set_rxon_chain(priv);
7384
7385 if (priv && priv->assoc_id &&
7386 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
7387 unsigned long flags;
7388
7389 spin_lock_irqsave(&priv->lock, flags);
7390 if (priv->beacon_int)
7391 queue_work(priv->workqueue, &priv->post_associate.work);
7392 else
7393 priv->call_post_assoc_from_beacon = 1;
7394 spin_unlock_irqrestore(&priv->lock, flags);
7395 }
7396
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007397 IWL_DEBUG_MAC80211("leave:\n");
7398 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007399}
7400
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007401#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007402
7403/*****************************************************************************
7404 *
7405 * sysfs attributes
7406 *
7407 *****************************************************************************/
7408
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007409#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007410
7411/*
7412 * The following adds a new attribute to the sysfs representation
7413 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7414 * used for controlling the debug level.
7415 *
7416 * See the level definitions in iwl for details.
7417 */
7418
7419static ssize_t show_debug_level(struct device_driver *d, char *buf)
7420{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007421 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007422}
7423static ssize_t store_debug_level(struct device_driver *d,
7424 const char *buf, size_t count)
7425{
7426 char *p = (char *)buf;
7427 u32 val;
7428
7429 val = simple_strtoul(p, &p, 0);
7430 if (p == buf)
7431 printk(KERN_INFO DRV_NAME
7432 ": %s is not in hex or decimal form.\n", buf);
7433 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007434 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007435
7436 return strnlen(buf, count);
7437}
7438
7439static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7440 show_debug_level, store_debug_level);
7441
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007442#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007443
7444static ssize_t show_rf_kill(struct device *d,
7445 struct device_attribute *attr, char *buf)
7446{
7447 /*
7448 * 0 - RF kill not enabled
7449 * 1 - SW based RF kill active (sysfs)
7450 * 2 - HW based RF kill active
7451 * 3 - Both HW and SW based RF kill active
7452 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007453 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007454 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
7455 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
7456
7457 return sprintf(buf, "%i\n", val);
7458}
7459
7460static ssize_t store_rf_kill(struct device *d,
7461 struct device_attribute *attr,
7462 const char *buf, size_t count)
7463{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007464 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007465
7466 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007467 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07007468 mutex_unlock(&priv->mutex);
7469
7470 return count;
7471}
7472
7473static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7474
7475static ssize_t show_temperature(struct device *d,
7476 struct device_attribute *attr, char *buf)
7477{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007478 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007479
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007480 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007481 return -EAGAIN;
7482
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007483 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007484}
7485
7486static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7487
7488static ssize_t show_rs_window(struct device *d,
7489 struct device_attribute *attr,
7490 char *buf)
7491{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007492 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007493 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007494}
7495static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7496
7497static ssize_t show_tx_power(struct device *d,
7498 struct device_attribute *attr, char *buf)
7499{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007500 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007501 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7502}
7503
7504static ssize_t store_tx_power(struct device *d,
7505 struct device_attribute *attr,
7506 const char *buf, size_t count)
7507{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007508 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007509 char *p = (char *)buf;
7510 u32 val;
7511
7512 val = simple_strtoul(p, &p, 10);
7513 if (p == buf)
7514 printk(KERN_INFO DRV_NAME
7515 ": %s is not in decimal form.\n", buf);
7516 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007517 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007518
7519 return count;
7520}
7521
7522static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7523
7524static ssize_t show_flags(struct device *d,
7525 struct device_attribute *attr, char *buf)
7526{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007527 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007528
7529 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7530}
7531
7532static ssize_t store_flags(struct device *d,
7533 struct device_attribute *attr,
7534 const char *buf, size_t count)
7535{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007536 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007537 u32 flags = simple_strtoul(buf, NULL, 0);
7538
7539 mutex_lock(&priv->mutex);
7540 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7541 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007542 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007543 IWL_WARNING("Could not cancel scan.\n");
7544 else {
7545 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7546 flags);
7547 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007548 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007549 }
7550 }
7551 mutex_unlock(&priv->mutex);
7552
7553 return count;
7554}
7555
7556static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7557
7558static ssize_t show_filter_flags(struct device *d,
7559 struct device_attribute *attr, char *buf)
7560{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007561 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007562
7563 return sprintf(buf, "0x%04X\n",
7564 le32_to_cpu(priv->active_rxon.filter_flags));
7565}
7566
7567static ssize_t store_filter_flags(struct device *d,
7568 struct device_attribute *attr,
7569 const char *buf, size_t count)
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 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7573
7574 mutex_lock(&priv->mutex);
7575 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7576 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007577 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007578 IWL_WARNING("Could not cancel scan.\n");
7579 else {
7580 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7581 "0x%04X\n", filter_flags);
7582 priv->staging_rxon.filter_flags =
7583 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007584 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007585 }
7586 }
7587 mutex_unlock(&priv->mutex);
7588
7589 return count;
7590}
7591
7592static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7593 store_filter_flags);
7594
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007595#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007596
7597static ssize_t show_measurement(struct device *d,
7598 struct device_attribute *attr, char *buf)
7599{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007600 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007601 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007602 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7603 u8 *data = (u8 *) & measure_report;
7604 unsigned long flags;
7605
7606 spin_lock_irqsave(&priv->lock, flags);
7607 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7608 spin_unlock_irqrestore(&priv->lock, flags);
7609 return 0;
7610 }
7611 memcpy(&measure_report, &priv->measure_report, size);
7612 priv->measurement_status = 0;
7613 spin_unlock_irqrestore(&priv->lock, flags);
7614
7615 while (size && (PAGE_SIZE - len)) {
7616 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7617 PAGE_SIZE - len, 1);
7618 len = strlen(buf);
7619 if (PAGE_SIZE - len)
7620 buf[len++] = '\n';
7621
7622 ofs += 16;
7623 size -= min(size, 16U);
7624 }
7625
7626 return len;
7627}
7628
7629static ssize_t store_measurement(struct device *d,
7630 struct device_attribute *attr,
7631 const char *buf, size_t count)
7632{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007633 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007634 struct ieee80211_measurement_params params = {
7635 .channel = le16_to_cpu(priv->active_rxon.channel),
7636 .start_time = cpu_to_le64(priv->last_tsf),
7637 .duration = cpu_to_le16(1),
7638 };
7639 u8 type = IWL_MEASURE_BASIC;
7640 u8 buffer[32];
7641 u8 channel;
7642
7643 if (count) {
7644 char *p = buffer;
7645 strncpy(buffer, buf, min(sizeof(buffer), count));
7646 channel = simple_strtoul(p, NULL, 0);
7647 if (channel)
7648 params.channel = channel;
7649
7650 p = buffer;
7651 while (*p && *p != ' ')
7652 p++;
7653 if (*p)
7654 type = simple_strtoul(p + 1, NULL, 0);
7655 }
7656
7657 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7658 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007659 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007660
7661 return count;
7662}
7663
7664static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7665 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007666#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007667
7668static ssize_t store_retry_rate(struct device *d,
7669 struct device_attribute *attr,
7670 const char *buf, size_t count)
7671{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007672 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007673
7674 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7675 if (priv->retry_rate <= 0)
7676 priv->retry_rate = 1;
7677
7678 return count;
7679}
7680
7681static ssize_t show_retry_rate(struct device *d,
7682 struct device_attribute *attr, char *buf)
7683{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007684 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007685 return sprintf(buf, "%d", priv->retry_rate);
7686}
7687
7688static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7689 store_retry_rate);
7690
7691static ssize_t store_power_level(struct device *d,
7692 struct device_attribute *attr,
7693 const char *buf, size_t count)
7694{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007695 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007696 int rc;
7697 int mode;
7698
7699 mode = simple_strtoul(buf, NULL, 0);
7700 mutex_lock(&priv->mutex);
7701
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007702 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007703 rc = -EAGAIN;
7704 goto out;
7705 }
7706
7707 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7708 mode = IWL_POWER_AC;
7709 else
7710 mode |= IWL_POWER_ENABLED;
7711
7712 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007713 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007714 if (rc) {
7715 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7716 goto out;
7717 }
7718 priv->power_mode = mode;
7719 }
7720
7721 rc = count;
7722
7723 out:
7724 mutex_unlock(&priv->mutex);
7725 return rc;
7726}
7727
7728#define MAX_WX_STRING 80
7729
7730/* Values are in microsecond */
7731static const s32 timeout_duration[] = {
7732 350000,
7733 250000,
7734 75000,
7735 37000,
7736 25000,
7737};
7738static const s32 period_duration[] = {
7739 400000,
7740 700000,
7741 1000000,
7742 1000000,
7743 1000000
7744};
7745
7746static ssize_t show_power_level(struct device *d,
7747 struct device_attribute *attr, char *buf)
7748{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007749 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007750 int level = IWL_POWER_LEVEL(priv->power_mode);
7751 char *p = buf;
7752
7753 p += sprintf(p, "%d ", level);
7754 switch (level) {
7755 case IWL_POWER_MODE_CAM:
7756 case IWL_POWER_AC:
7757 p += sprintf(p, "(AC)");
7758 break;
7759 case IWL_POWER_BATTERY:
7760 p += sprintf(p, "(BATTERY)");
7761 break;
7762 default:
7763 p += sprintf(p,
7764 "(Timeout %dms, Period %dms)",
7765 timeout_duration[level - 1] / 1000,
7766 period_duration[level - 1] / 1000);
7767 }
7768
7769 if (!(priv->power_mode & IWL_POWER_ENABLED))
7770 p += sprintf(p, " OFF\n");
7771 else
7772 p += sprintf(p, " \n");
7773
7774 return (p - buf + 1);
7775
7776}
7777
7778static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7779 store_power_level);
7780
7781static ssize_t show_channels(struct device *d,
7782 struct device_attribute *attr, char *buf)
7783{
Johannes Berg8318d782008-01-24 19:38:38 +01007784 /* all this shit doesn't belong into sysfs anyway */
7785 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007786}
7787
7788static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7789
7790static ssize_t show_statistics(struct device *d,
7791 struct device_attribute *attr, char *buf)
7792{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007793 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007794 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007795 u32 len = 0, ofs = 0;
7796 u8 *data = (u8 *) & priv->statistics;
7797 int rc = 0;
7798
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007799 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007800 return -EAGAIN;
7801
7802 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007803 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007804 mutex_unlock(&priv->mutex);
7805
7806 if (rc) {
7807 len = sprintf(buf,
7808 "Error sending statistics request: 0x%08X\n", rc);
7809 return len;
7810 }
7811
7812 while (size && (PAGE_SIZE - len)) {
7813 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7814 PAGE_SIZE - len, 1);
7815 len = strlen(buf);
7816 if (PAGE_SIZE - len)
7817 buf[len++] = '\n';
7818
7819 ofs += 16;
7820 size -= min(size, 16U);
7821 }
7822
7823 return len;
7824}
7825
7826static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7827
7828static ssize_t show_antenna(struct device *d,
7829 struct device_attribute *attr, char *buf)
7830{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007831 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007833 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007834 return -EAGAIN;
7835
7836 return sprintf(buf, "%d\n", priv->antenna);
7837}
7838
7839static ssize_t store_antenna(struct device *d,
7840 struct device_attribute *attr,
7841 const char *buf, size_t count)
7842{
7843 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007844 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007845
7846 if (count == 0)
7847 return 0;
7848
7849 if (sscanf(buf, "%1i", &ant) != 1) {
7850 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7851 return count;
7852 }
7853
7854 if ((ant >= 0) && (ant <= 2)) {
7855 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007856 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007857 } else
7858 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7859
7860
7861 return count;
7862}
7863
7864static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7865
7866static ssize_t show_status(struct device *d,
7867 struct device_attribute *attr, char *buf)
7868{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007869 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007870 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007871 return -EAGAIN;
7872 return sprintf(buf, "0x%08x\n", (int)priv->status);
7873}
7874
7875static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7876
7877static ssize_t dump_error_log(struct device *d,
7878 struct device_attribute *attr,
7879 const char *buf, size_t count)
7880{
7881 char *p = (char *)buf;
7882
7883 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007884 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007885
7886 return strnlen(buf, count);
7887}
7888
7889static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7890
7891static ssize_t dump_event_log(struct device *d,
7892 struct device_attribute *attr,
7893 const char *buf, size_t count)
7894{
7895 char *p = (char *)buf;
7896
7897 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007898 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007899
7900 return strnlen(buf, count);
7901}
7902
7903static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7904
7905/*****************************************************************************
7906 *
7907 * driver setup and teardown
7908 *
7909 *****************************************************************************/
7910
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007911static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007912{
7913 priv->workqueue = create_workqueue(DRV_NAME);
7914
7915 init_waitqueue_head(&priv->wait_command_queue);
7916
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007917 INIT_WORK(&priv->up, iwl4965_bg_up);
7918 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7919 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7920 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7921 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7922 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7923 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7924 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7925 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7926 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7927 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7928 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007929
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007930 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007931
7932 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007933 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007934}
7935
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007936static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007937{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007938 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007939
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007940 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007941 cancel_delayed_work(&priv->scan_check);
7942 cancel_delayed_work(&priv->alive_start);
7943 cancel_delayed_work(&priv->post_associate);
7944 cancel_work_sync(&priv->beacon_update);
7945}
7946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007947static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007948 &dev_attr_antenna.attr,
7949 &dev_attr_channels.attr,
7950 &dev_attr_dump_errors.attr,
7951 &dev_attr_dump_events.attr,
7952 &dev_attr_flags.attr,
7953 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007954#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007955 &dev_attr_measurement.attr,
7956#endif
7957 &dev_attr_power_level.attr,
7958 &dev_attr_retry_rate.attr,
7959 &dev_attr_rf_kill.attr,
7960 &dev_attr_rs_window.attr,
7961 &dev_attr_statistics.attr,
7962 &dev_attr_status.attr,
7963 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007964 &dev_attr_tx_power.attr,
7965
7966 NULL
7967};
7968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007969static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007970 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007971 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007972};
7973
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007974static struct ieee80211_ops iwl4965_hw_ops = {
7975 .tx = iwl4965_mac_tx,
7976 .start = iwl4965_mac_start,
7977 .stop = iwl4965_mac_stop,
7978 .add_interface = iwl4965_mac_add_interface,
7979 .remove_interface = iwl4965_mac_remove_interface,
7980 .config = iwl4965_mac_config,
7981 .config_interface = iwl4965_mac_config_interface,
7982 .configure_filter = iwl4965_configure_filter,
7983 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007984 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007985 .get_stats = iwl4965_mac_get_stats,
7986 .get_tx_stats = iwl4965_mac_get_tx_stats,
7987 .conf_tx = iwl4965_mac_conf_tx,
7988 .get_tsf = iwl4965_mac_get_tsf,
7989 .reset_tsf = iwl4965_mac_reset_tsf,
7990 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01007991 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007992#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007993 .conf_ht = iwl4965_mac_conf_ht,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02007994 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007995#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007996 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007997};
7998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007999static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008000{
8001 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008002 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008003 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008004 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Zhu Yi5a66926a2008-01-14 17:46:18 -08008005 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008006
Assaf Krauss316c30d2008-03-14 10:38:46 -07008007 /************************
8008 * 1. Allocating HW data
8009 ************************/
8010
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008011 /* Disabling hardware scan means that mac80211 will perform scans
8012 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07008013 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008014 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008015 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008016 }
8017
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008018 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
8019 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07008020 err = -ENOMEM;
8021 goto out;
8022 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008023 priv = hw->priv;
8024 /* At this point both hw and priv are allocated. */
8025
Zhu Yib481de92007-09-25 17:54:57 -07008026 SET_IEEE80211_DEV(hw, &pdev->dev);
8027
8028 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08008029 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008030 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008031
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008032#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07008033 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07008034 atomic_set(&priv->restrict_refcnt, 0);
8035#endif
Zhu Yib481de92007-09-25 17:54:57 -07008036
Assaf Krauss316c30d2008-03-14 10:38:46 -07008037 /**************************
8038 * 2. Initializing PCI bus
8039 **************************/
8040 if (pci_enable_device(pdev)) {
8041 err = -ENODEV;
8042 goto out_ieee80211_free_hw;
8043 }
8044
8045 pci_set_master(pdev);
8046
8047 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8048 if (!err)
8049 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8050 if (err) {
8051 printk(KERN_WARNING DRV_NAME
8052 ": No suitable DMA available.\n");
8053 goto out_pci_disable_device;
8054 }
8055
8056 err = pci_request_regions(pdev, DRV_NAME);
8057 if (err)
8058 goto out_pci_disable_device;
8059
8060 pci_set_drvdata(pdev, priv);
8061
8062 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8063 * PCI Tx retries from interfering with C3 CPU state */
8064 pci_write_config_byte(pdev, 0x41, 0x00);
8065
8066 /***********************
8067 * 3. Read REV register
8068 ***********************/
8069 priv->hw_base = pci_iomap(pdev, 0, 0);
8070 if (!priv->hw_base) {
8071 err = -ENODEV;
8072 goto out_pci_release_regions;
8073 }
8074
8075 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8076 (unsigned long long) pci_resource_len(pdev, 0));
8077 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8078
8079 printk(KERN_INFO DRV_NAME
8080 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
8081
8082 /*****************
8083 * 4. Read EEPROM
8084 *****************/
8085 /* nic init */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07008086 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Assaf Krauss316c30d2008-03-14 10:38:46 -07008087 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8088
Tomas Winkler3395f6e2008-03-25 16:33:37 -07008089 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8090 err = iwl_poll_bit(priv, CSR_GP_CNTRL,
Assaf Krauss316c30d2008-03-14 10:38:46 -07008091 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8092 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8093 if (err < 0) {
8094 IWL_DEBUG_INFO("Failed to init the card\n");
8095 goto out_iounmap;
8096 }
8097 /* Read the EEPROM */
8098 err = iwl_eeprom_init(priv);
8099 if (err) {
8100 IWL_ERROR("Unable to init EEPROM\n");
8101 goto out_iounmap;
8102 }
8103 /* MAC Address location in EEPROM same for 3945/4965 */
8104 iwl_eeprom_get_mac(priv, priv->mac_addr);
8105 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8106 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8107
8108 /************************
8109 * 5. Setup HW constants
8110 ************************/
8111 /* Device-specific setup */
8112 if (iwl4965_hw_set_hw_setting(priv)) {
8113 IWL_ERROR("failed to set hw settings\n");
8114 goto out_iounmap;
8115 }
8116
8117 /*******************
8118 * 6. Setup hw/priv
8119 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07008120
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008121 err = iwl_setup(priv);
8122 if (err)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008123 goto out_unset_hw_settings;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008124 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07008125
8126 /**********************************
8127 * 7. Initialize module parameters
8128 **********************************/
8129
8130 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07008131 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07008132 set_bit(STATUS_RF_KILL_SW, &priv->status);
8133 IWL_DEBUG_INFO("Radio disabled.\n");
8134 }
8135
Assaf Krauss1ea87392008-03-18 14:57:50 -07008136 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008137 priv->qos_data.qos_enable = 1;
8138
8139 /********************
8140 * 8. Setup services
8141 ********************/
8142 iwl4965_disable_interrupts(priv);
8143
8144 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
8145 if (err) {
8146 IWL_ERROR("failed to create sysfs device attributes\n");
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008147 goto out_unset_hw_settings;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008148 }
8149
8150 err = iwl_dbgfs_register(priv, DRV_NAME);
8151 if (err) {
8152 IWL_ERROR("failed to create debugfs files\n");
8153 goto out_remove_sysfs;
8154 }
8155
8156 iwl4965_setup_deferred_work(priv);
8157 iwl4965_setup_rx_handlers(priv);
8158
8159 /********************
8160 * 9. Conclude
8161 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08008162 pci_save_state(pdev);
8163 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008164
8165 return 0;
8166
Assaf Krauss316c30d2008-03-14 10:38:46 -07008167 out_remove_sysfs:
8168 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008169 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008170 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008171 out_iounmap:
8172 pci_iounmap(pdev, priv->hw_base);
8173 out_pci_release_regions:
8174 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008175 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07008176 out_pci_disable_device:
8177 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008178 out_ieee80211_free_hw:
8179 ieee80211_free_hw(priv->hw);
8180 out:
8181 return err;
8182}
8183
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008184static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008185{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008186 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008187 struct list_head *p, *q;
8188 int i;
8189
8190 if (!priv)
8191 return;
8192
8193 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8194
Zhu Yib481de92007-09-25 17:54:57 -07008195 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008196
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008197 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008198
8199 /* Free MAC hash list for ADHOC */
8200 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8201 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8202 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008203 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008204 }
8205 }
8206
Tomas Winkler712b6cf2008-03-12 16:58:52 -07008207 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008208 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008209
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008210 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008211
8212 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008213 iwl4965_rx_queue_free(priv, &priv->rxq);
8214 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008215
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008216 iwl4965_unset_hw_setting(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008217 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008218
8219 if (priv->mac80211_registered) {
8220 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008221 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008222 }
8223
Mohamed Abbas948c1712007-10-25 17:15:45 +08008224 /*netif_stop_queue(dev); */
8225 flush_workqueue(priv->workqueue);
8226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008227 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008228 * priv->workqueue... so we can't take down the workqueue
8229 * until now... */
8230 destroy_workqueue(priv->workqueue);
8231 priv->workqueue = NULL;
8232
Zhu Yib481de92007-09-25 17:54:57 -07008233 pci_iounmap(pdev, priv->hw_base);
8234 pci_release_regions(pdev);
8235 pci_disable_device(pdev);
8236 pci_set_drvdata(pdev, NULL);
8237
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008238 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008239 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008240
8241 if (priv->ibss_beacon)
8242 dev_kfree_skb(priv->ibss_beacon);
8243
8244 ieee80211_free_hw(priv->hw);
8245}
8246
8247#ifdef CONFIG_PM
8248
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008249static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008250{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008251 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008252
Zhu Yie655b9f2008-01-24 02:19:38 -08008253 if (priv->is_open) {
8254 set_bit(STATUS_IN_SUSPEND, &priv->status);
8255 iwl4965_mac_stop(priv->hw);
8256 priv->is_open = 1;
8257 }
Zhu Yib481de92007-09-25 17:54:57 -07008258
Zhu Yib481de92007-09-25 17:54:57 -07008259 pci_set_power_state(pdev, PCI_D3hot);
8260
Zhu Yib481de92007-09-25 17:54:57 -07008261 return 0;
8262}
8263
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008264static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008265{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008266 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008267
Zhu Yib481de92007-09-25 17:54:57 -07008268 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008269
Zhu Yie655b9f2008-01-24 02:19:38 -08008270 if (priv->is_open)
8271 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008272
Zhu Yie655b9f2008-01-24 02:19:38 -08008273 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008274 return 0;
8275}
8276
8277#endif /* CONFIG_PM */
8278
8279/*****************************************************************************
8280 *
8281 * driver and module entry point
8282 *
8283 *****************************************************************************/
8284
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008285static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008286 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008287 .id_table = iwl4965_hw_card_ids,
8288 .probe = iwl4965_pci_probe,
8289 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008290#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008291 .suspend = iwl4965_pci_suspend,
8292 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008293#endif
8294};
8295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008296static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008297{
8298
8299 int ret;
8300 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8301 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008302 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008303 if (ret) {
8304 IWL_ERROR("Unable to initialize PCI module\n");
8305 return ret;
8306 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008307#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008308 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008309 if (ret) {
8310 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008311 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008312 return ret;
8313 }
8314#endif
8315
8316 return ret;
8317}
8318
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008319static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008320{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008321#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008322 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008323#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008324 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008325}
8326
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008327module_exit(iwl4965_exit);
8328module_init(iwl4965_init);